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