]> The Tcpdump Group git mirrors - tcpdump/blob - print-ahcp.c
Rename cstr[] to istr[] like invalid string
[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 <tcpdump-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 #ifdef INET6
157 ND_PRINT((ndo, "%s%s", sep, ip6addr_string(ndo, cp)));
158 #else
159 ND_PRINT((ndo, "%s(compiled w/o IPv6)", sep));
160 #endif /* INET6 */
161 cp += 16;
162 sep = ", ";
163 }
164 return 0;
165
166 invalid:
167 ND_PRINT((ndo, "%s", istr));
168 ND_TCHECK2(*cp, ep - cp);
169 return 0;
170 trunc:
171 ND_PRINT((ndo, "%s", tstr));
172 return -1;
173 }
174
175 static int
176 ahcp_ipv4_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
177 {
178 const char *sep = ": ";
179
180 while (cp < ep) {
181 if (cp + 4 > ep)
182 goto invalid;
183 ND_TCHECK2(*cp, 4);
184 ND_PRINT((ndo, "%s%s", sep, ipaddr_string(ndo, cp)));
185 cp += 4;
186 sep = ", ";
187 }
188 return 0;
189
190 invalid:
191 ND_PRINT((ndo, "%s", istr));
192 ND_TCHECK2(*cp, ep - cp);
193 return 0;
194 trunc:
195 ND_PRINT((ndo, "%s", tstr));
196 return -1;
197 }
198
199 static int
200 ahcp_ipv6_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
201 {
202 const char *sep = ": ";
203
204 while (cp < ep) {
205 if (cp + 17 > ep)
206 goto invalid;
207 ND_TCHECK2(*cp, 17);
208 #ifdef INET6
209 ND_PRINT((ndo, "%s%s/%u", sep, ip6addr_string(ndo, cp), *(cp + 16)));
210 #else
211 ND_PRINT((ndo, "%s(compiled w/o IPv6)/%u", sep, *(cp + 16)));
212 #endif /* INET6 */
213 cp += 17;
214 sep = ", ";
215 }
216 return 0;
217
218 invalid:
219 ND_PRINT((ndo, "%s", istr));
220 ND_TCHECK2(*cp, ep - cp);
221 return 0;
222 trunc:
223 ND_PRINT((ndo, "%s", tstr));
224 return -1;
225 }
226
227 static int
228 ahcp_ipv4_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
229 {
230 const char *sep = ": ";
231
232 while (cp < ep) {
233 if (cp + 5 > ep)
234 goto invalid;
235 ND_TCHECK2(*cp, 5);
236 ND_PRINT((ndo, "%s%s/%u", sep, ipaddr_string(ndo, cp), *(cp + 4)));
237 cp += 5;
238 sep = ", ";
239 }
240 return 0;
241
242 invalid:
243 ND_PRINT((ndo, "%s", istr));
244 ND_TCHECK2(*cp, ep - cp);
245 return 0;
246 trunc:
247 ND_PRINT((ndo, "%s", tstr));
248 return -1;
249 }
250
251 /* Data decoders signal truncated data with -1. */
252 static int
253 (* const data_decoders[AHCP1_OPT_MAX + 1])(netdissect_options *, const u_char *, const u_char *) = {
254 /* [AHCP1_OPT_PAD] = */ NULL,
255 /* [AHCP1_OPT_MANDATORY] = */ NULL,
256 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print,
257 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print,
258 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print,
259 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print,
260 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print,
261 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL,
262 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print,
263 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print,
264 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print,
265 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print,
266 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print,
267 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print,
268 };
269
270 static void
271 ahcp1_options_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
272 {
273 uint8_t option_no, option_len;
274
275 while (cp < ep) {
276 /* Option no */
277 ND_TCHECK2(*cp, 1);
278 option_no = *cp;
279 cp += 1;
280 ND_PRINT((ndo, "\n\t %s", tok2str(ahcp1_opt_str, "Unknown-%u", option_no)));
281 if (option_no == AHCP1_OPT_PAD || option_no == AHCP1_OPT_MANDATORY)
282 continue;
283 /* Length */
284 if (cp + 1 > ep)
285 goto invalid;
286 ND_TCHECK2(*cp, 1);
287 option_len = *cp;
288 cp += 1;
289 if (cp + option_len > ep)
290 goto invalid;
291 /* Value */
292 if (option_no <= AHCP1_OPT_MAX && data_decoders[option_no] != NULL) {
293 if (data_decoders[option_no](ndo, cp, cp + option_len) < 0)
294 break; /* truncated and already marked up */
295 } else {
296 ND_PRINT((ndo, " (Length %u)", option_len));
297 ND_TCHECK2(*cp, option_len);
298 }
299 cp += option_len;
300 }
301 return;
302
303 invalid:
304 ND_PRINT((ndo, "%s", istr));
305 ND_TCHECK2(*cp, ep - cp);
306 return;
307 trunc:
308 ND_PRINT((ndo, "%s", tstr));
309 }
310
311 static void
312 ahcp1_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
313 {
314 uint8_t type, mbz;
315 uint16_t body_len;
316
317 if (cp + AHCP1_BODY_MIN_LEN > ep)
318 goto invalid;
319 /* Type */
320 ND_TCHECK2(*cp, 1);
321 type = *cp;
322 cp += 1;
323 /* MBZ */
324 ND_TCHECK2(*cp, 1);
325 mbz = *cp;
326 cp += 1;
327 /* Length */
328 ND_TCHECK2(*cp, 2);
329 body_len = EXTRACT_16BITS(cp);
330 cp += 2;
331
332 if (ndo->ndo_vflag) {
333 ND_PRINT((ndo, "\n\t%s", tok2str(ahcp1_msg_str, "Unknown-%u", type)));
334 if (mbz != 0)
335 ND_PRINT((ndo, ", MBZ %u", mbz));
336 ND_PRINT((ndo, ", Length %u", body_len));
337 }
338 if (cp + body_len > ep)
339 goto invalid;
340
341 /* Options */
342 if (ndo->ndo_vflag >= 2)
343 ahcp1_options_print(ndo, cp, cp + body_len); /* not ep (ignore extra data) */
344 else
345 ND_TCHECK2(*cp, body_len);
346 return;
347
348 invalid:
349 ND_PRINT((ndo, "%s", istr));
350 ND_TCHECK2(*cp, ep - cp);
351 return;
352 trunc:
353 ND_PRINT((ndo, "%s", tstr));
354 }
355
356 void
357 ahcp_print(netdissect_options *ndo, const u_char *cp, const u_int len)
358 {
359 const u_char *ep = cp + len;
360 uint8_t version;
361
362 ND_PRINT((ndo, "AHCP"));
363 if (len < 2)
364 goto invalid;
365 /* Magic */
366 ND_TCHECK2(*cp, 1);
367 if (*cp != AHCP_MAGIC_NUMBER)
368 goto invalid;
369 cp += 1;
370 /* Version */
371 ND_TCHECK2(*cp, 1);
372 version = *cp;
373 cp += 1;
374 switch (version) {
375 case AHCP_VERSION_1: {
376 ND_PRINT((ndo, " Version 1"));
377 if (len < AHCP1_HEADER_FIX_LEN)
378 goto invalid;
379 if (!ndo->ndo_vflag) {
380 ND_TCHECK2(*cp, AHCP1_HEADER_FIX_LEN - 2);
381 cp += AHCP1_HEADER_FIX_LEN - 2;
382 } else {
383 /* Hopcount */
384 ND_TCHECK2(*cp, 1);
385 ND_PRINT((ndo, "\n\tHopcount %u", *cp));
386 cp += 1;
387 /* Original Hopcount */
388 ND_TCHECK2(*cp, 1);
389 ND_PRINT((ndo, ", Original Hopcount %u", *cp));
390 cp += 1;
391 /* Nonce */
392 ND_TCHECK2(*cp, 4);
393 ND_PRINT((ndo, ", Nonce 0x%08x", EXTRACT_32BITS(cp)));
394 cp += 4;
395 /* Source Id */
396 ND_TCHECK2(*cp, 8);
397 ND_PRINT((ndo, ", Source Id %s", linkaddr_string(ndo, cp, 0, 8)));
398 cp += 8;
399 /* Destination Id */
400 ND_TCHECK2(*cp, 8);
401 ND_PRINT((ndo, ", Destination Id %s", linkaddr_string(ndo, cp, 0, 8)));
402 cp += 8;
403 }
404 /* Body */
405 ahcp1_body_print(ndo, cp, ep);
406 break;
407 }
408 default:
409 ND_PRINT((ndo, " Version %u (unknown)", version));
410 break;
411 }
412 return;
413
414 invalid:
415 ND_PRINT((ndo, "%s", istr));
416 ND_TCHECK2(*cp, ep - cp);
417 return;
418 trunc:
419 ND_PRINT((ndo, "%s", tstr));
420 }