]> The Tcpdump Group git mirrors - tcpdump/blob - print-ahcp.c
Don't require IPv6 library support in order to support IPv6 addresses.
[tcpdump] / print-ahcp.c
1 /*
2 * This module implements decoding of AHCP (Ad Hoc Configuration Protocol) based
3 * on draft-chroboczek-ahcp-00 and source code of ahcpd-0.53.
4 *
5 *
6 * Copyright (c) 2013 The TCPDUMP project
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <netdissect-stdinc.h>
37
38 #include "netdissect.h"
39 #include "extract.h"
40 #include "addrtoname.h"
41
42 static const char tstr[] = " [|ahcp]";
43 static const char istr[] = " (invalid)";
44
45 #define AHCP_MAGIC_NUMBER 43
46 #define AHCP_VERSION_1 1
47 #define AHCP1_HEADER_FIX_LEN 24
48 #define AHCP1_BODY_MIN_LEN 4
49
50 #define AHCP1_MSG_DISCOVER 0
51 #define AHCP1_MSG_OFFER 1
52 #define AHCP1_MSG_REQUEST 2
53 #define AHCP1_MSG_ACK 3
54 #define AHCP1_MSG_NACK 4
55 #define AHCP1_MSG_RELEASE 5
56
57 static const struct tok ahcp1_msg_str[] = {
58 { AHCP1_MSG_DISCOVER, "Discover" },
59 { AHCP1_MSG_OFFER, "Offer" },
60 { AHCP1_MSG_REQUEST, "Request" },
61 { AHCP1_MSG_ACK, "Ack" },
62 { AHCP1_MSG_NACK, "Nack" },
63 { AHCP1_MSG_RELEASE, "Release" },
64 { 0, NULL }
65 };
66
67 #define AHCP1_OPT_PAD 0
68 #define AHCP1_OPT_MANDATORY 1
69 #define AHCP1_OPT_ORIGIN_TIME 2
70 #define AHCP1_OPT_EXPIRES 3
71 #define AHCP1_OPT_MY_IPV6_ADDRESS 4
72 #define AHCP1_OPT_MY_IPV4_ADDRESS 5
73 #define AHCP1_OPT_IPV6_PREFIX 6
74 #define AHCP1_OPT_IPV4_PREFIX 7
75 #define AHCP1_OPT_IPV6_ADDRESS 8
76 #define AHCP1_OPT_IPV4_ADDRESS 9
77 #define AHCP1_OPT_IPV6_PREFIX_DELEGATION 10
78 #define AHCP1_OPT_IPV4_PREFIX_DELEGATION 11
79 #define AHCP1_OPT_NAME_SERVER 12
80 #define AHCP1_OPT_NTP_SERVER 13
81 #define AHCP1_OPT_MAX 13
82
83 static const struct tok ahcp1_opt_str[] = {
84 { AHCP1_OPT_PAD, "Pad" },
85 { AHCP1_OPT_MANDATORY, "Mandatory" },
86 { AHCP1_OPT_ORIGIN_TIME, "Origin Time" },
87 { AHCP1_OPT_EXPIRES, "Expires" },
88 { AHCP1_OPT_MY_IPV6_ADDRESS, "My-IPv6-Address" },
89 { AHCP1_OPT_MY_IPV4_ADDRESS, "My-IPv4-Address" },
90 { AHCP1_OPT_IPV6_PREFIX, "IPv6 Prefix" },
91 { AHCP1_OPT_IPV4_PREFIX, "IPv4 Prefix" },
92 { AHCP1_OPT_IPV6_ADDRESS, "IPv6 Address" },
93 { AHCP1_OPT_IPV4_ADDRESS, "IPv4 Address" },
94 { AHCP1_OPT_IPV6_PREFIX_DELEGATION, "IPv6 Prefix Delegation" },
95 { AHCP1_OPT_IPV4_PREFIX_DELEGATION, "IPv4 Prefix Delegation" },
96 { AHCP1_OPT_NAME_SERVER, "Name Server" },
97 { AHCP1_OPT_NTP_SERVER, "NTP Server" },
98 { 0, NULL }
99 };
100
101 static int
102 ahcp_time_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
103 {
104 time_t t;
105 struct tm *tm;
106 char buf[BUFSIZE];
107
108 if (cp + 4 != ep)
109 goto invalid;
110 ND_TCHECK2(*cp, 4);
111 t = EXTRACT_32BITS(cp);
112 if (NULL == (tm = gmtime(&t)))
113 ND_PRINT((ndo, ": gmtime() error"));
114 else if (0 == strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm))
115 ND_PRINT((ndo, ": strftime() error"));
116 else
117 ND_PRINT((ndo, ": %s UTC", buf));
118 return 0;
119
120 invalid:
121 ND_PRINT((ndo, "%s", istr));
122 ND_TCHECK2(*cp, ep - cp);
123 return 0;
124 trunc:
125 ND_PRINT((ndo, "%s", tstr));
126 return -1;
127 }
128
129 static int
130 ahcp_seconds_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
131 {
132 if (cp + 4 != ep)
133 goto invalid;
134 ND_TCHECK2(*cp, 4);
135 ND_PRINT((ndo, ": %us", EXTRACT_32BITS(cp)));
136 return 0;
137
138 invalid:
139 ND_PRINT((ndo, "%s", istr));
140 ND_TCHECK2(*cp, ep - cp);
141 return 0;
142 trunc:
143 ND_PRINT((ndo, "%s", tstr));
144 return -1;
145 }
146
147 static int
148 ahcp_ipv6_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
149 {
150 const char *sep = ": ";
151
152 while (cp < ep) {
153 if (cp + 16 > ep)
154 goto invalid;
155 ND_TCHECK2(*cp, 16);
156 ND_PRINT((ndo, "%s%s", sep, ip6addr_string(ndo, cp)));
157 cp += 16;
158 sep = ", ";
159 }
160 return 0;
161
162 invalid:
163 ND_PRINT((ndo, "%s", istr));
164 ND_TCHECK2(*cp, ep - cp);
165 return 0;
166 trunc:
167 ND_PRINT((ndo, "%s", tstr));
168 return -1;
169 }
170
171 static int
172 ahcp_ipv4_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
173 {
174 const char *sep = ": ";
175
176 while (cp < ep) {
177 if (cp + 4 > ep)
178 goto invalid;
179 ND_TCHECK2(*cp, 4);
180 ND_PRINT((ndo, "%s%s", sep, ipaddr_string(ndo, cp)));
181 cp += 4;
182 sep = ", ";
183 }
184 return 0;
185
186 invalid:
187 ND_PRINT((ndo, "%s", istr));
188 ND_TCHECK2(*cp, ep - cp);
189 return 0;
190 trunc:
191 ND_PRINT((ndo, "%s", tstr));
192 return -1;
193 }
194
195 static int
196 ahcp_ipv6_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
197 {
198 const char *sep = ": ";
199
200 while (cp < ep) {
201 if (cp + 17 > ep)
202 goto invalid;
203 ND_TCHECK2(*cp, 17);
204 ND_PRINT((ndo, "%s%s/%u", sep, ip6addr_string(ndo, cp), *(cp + 16)));
205 cp += 17;
206 sep = ", ";
207 }
208 return 0;
209
210 invalid:
211 ND_PRINT((ndo, "%s", istr));
212 ND_TCHECK2(*cp, ep - cp);
213 return 0;
214 trunc:
215 ND_PRINT((ndo, "%s", tstr));
216 return -1;
217 }
218
219 static int
220 ahcp_ipv4_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
221 {
222 const char *sep = ": ";
223
224 while (cp < ep) {
225 if (cp + 5 > ep)
226 goto invalid;
227 ND_TCHECK2(*cp, 5);
228 ND_PRINT((ndo, "%s%s/%u", sep, ipaddr_string(ndo, cp), *(cp + 4)));
229 cp += 5;
230 sep = ", ";
231 }
232 return 0;
233
234 invalid:
235 ND_PRINT((ndo, "%s", istr));
236 ND_TCHECK2(*cp, ep - cp);
237 return 0;
238 trunc:
239 ND_PRINT((ndo, "%s", tstr));
240 return -1;
241 }
242
243 /* Data decoders signal truncated data with -1. */
244 static int
245 (* const data_decoders[AHCP1_OPT_MAX + 1])(netdissect_options *, const u_char *, const u_char *) = {
246 /* [AHCP1_OPT_PAD] = */ NULL,
247 /* [AHCP1_OPT_MANDATORY] = */ NULL,
248 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print,
249 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print,
250 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print,
251 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print,
252 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print,
253 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL,
254 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print,
255 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print,
256 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print,
257 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print,
258 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print,
259 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print,
260 };
261
262 static void
263 ahcp1_options_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
264 {
265 uint8_t option_no, option_len;
266
267 while (cp < ep) {
268 /* Option no */
269 ND_TCHECK2(*cp, 1);
270 option_no = *cp;
271 cp += 1;
272 ND_PRINT((ndo, "\n\t %s", tok2str(ahcp1_opt_str, "Unknown-%u", option_no)));
273 if (option_no == AHCP1_OPT_PAD || option_no == AHCP1_OPT_MANDATORY)
274 continue;
275 /* Length */
276 if (cp + 1 > ep)
277 goto invalid;
278 ND_TCHECK2(*cp, 1);
279 option_len = *cp;
280 cp += 1;
281 if (cp + option_len > ep)
282 goto invalid;
283 /* Value */
284 if (option_no <= AHCP1_OPT_MAX && data_decoders[option_no] != NULL) {
285 if (data_decoders[option_no](ndo, cp, cp + option_len) < 0)
286 break; /* truncated and already marked up */
287 } else {
288 ND_PRINT((ndo, " (Length %u)", option_len));
289 ND_TCHECK2(*cp, option_len);
290 }
291 cp += option_len;
292 }
293 return;
294
295 invalid:
296 ND_PRINT((ndo, "%s", istr));
297 ND_TCHECK2(*cp, ep - cp);
298 return;
299 trunc:
300 ND_PRINT((ndo, "%s", tstr));
301 }
302
303 static void
304 ahcp1_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
305 {
306 uint8_t type, mbz;
307 uint16_t body_len;
308
309 if (cp + AHCP1_BODY_MIN_LEN > ep)
310 goto invalid;
311 /* Type */
312 ND_TCHECK2(*cp, 1);
313 type = *cp;
314 cp += 1;
315 /* MBZ */
316 ND_TCHECK2(*cp, 1);
317 mbz = *cp;
318 cp += 1;
319 /* Length */
320 ND_TCHECK2(*cp, 2);
321 body_len = EXTRACT_16BITS(cp);
322 cp += 2;
323
324 if (ndo->ndo_vflag) {
325 ND_PRINT((ndo, "\n\t%s", tok2str(ahcp1_msg_str, "Unknown-%u", type)));
326 if (mbz != 0)
327 ND_PRINT((ndo, ", MBZ %u", mbz));
328 ND_PRINT((ndo, ", Length %u", body_len));
329 }
330 if (cp + body_len > ep)
331 goto invalid;
332
333 /* Options */
334 if (ndo->ndo_vflag >= 2)
335 ahcp1_options_print(ndo, cp, cp + body_len); /* not ep (ignore extra data) */
336 else
337 ND_TCHECK2(*cp, body_len);
338 return;
339
340 invalid:
341 ND_PRINT((ndo, "%s", istr));
342 ND_TCHECK2(*cp, ep - cp);
343 return;
344 trunc:
345 ND_PRINT((ndo, "%s", tstr));
346 }
347
348 void
349 ahcp_print(netdissect_options *ndo, const u_char *cp, const u_int len)
350 {
351 const u_char *ep = cp + len;
352 uint8_t version;
353
354 ND_PRINT((ndo, "AHCP"));
355 if (len < 2)
356 goto invalid;
357 /* Magic */
358 ND_TCHECK2(*cp, 1);
359 if (*cp != AHCP_MAGIC_NUMBER)
360 goto invalid;
361 cp += 1;
362 /* Version */
363 ND_TCHECK2(*cp, 1);
364 version = *cp;
365 cp += 1;
366 switch (version) {
367 case AHCP_VERSION_1: {
368 ND_PRINT((ndo, " Version 1"));
369 if (len < AHCP1_HEADER_FIX_LEN)
370 goto invalid;
371 if (!ndo->ndo_vflag) {
372 ND_TCHECK2(*cp, AHCP1_HEADER_FIX_LEN - 2);
373 cp += AHCP1_HEADER_FIX_LEN - 2;
374 } else {
375 /* Hopcount */
376 ND_TCHECK2(*cp, 1);
377 ND_PRINT((ndo, "\n\tHopcount %u", *cp));
378 cp += 1;
379 /* Original Hopcount */
380 ND_TCHECK2(*cp, 1);
381 ND_PRINT((ndo, ", Original Hopcount %u", *cp));
382 cp += 1;
383 /* Nonce */
384 ND_TCHECK2(*cp, 4);
385 ND_PRINT((ndo, ", Nonce 0x%08x", EXTRACT_32BITS(cp)));
386 cp += 4;
387 /* Source Id */
388 ND_TCHECK2(*cp, 8);
389 ND_PRINT((ndo, ", Source Id %s", linkaddr_string(ndo, cp, 0, 8)));
390 cp += 8;
391 /* Destination Id */
392 ND_TCHECK2(*cp, 8);
393 ND_PRINT((ndo, ", Destination Id %s", linkaddr_string(ndo, cp, 0, 8)));
394 cp += 8;
395 }
396 /* Body */
397 ahcp1_body_print(ndo, cp, ep);
398 break;
399 }
400 default:
401 ND_PRINT((ndo, " Version %u (unknown)", version));
402 break;
403 }
404 return;
405
406 invalid:
407 ND_PRINT((ndo, "%s", istr));
408 ND_TCHECK2(*cp, ep - cp);
409 return;
410 trunc:
411 ND_PRINT((ndo, "%s", tstr));
412 }