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.
6 * Copyright (c) 2013 The TCPDUMP project
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: print-ahcp.c,v 1.3 2015/03/31 21:59:35 christos Exp $");
37 #define NETDISSECT_REWORKED
42 #include <tcpdump-stdinc.h>
44 #include "interface.h"
46 #include "addrtoname.h"
48 static const char tstr
[] = " [|ahcp]";
49 static const char cstr
[] = "(corrupt)";
51 #define AHCP_MAGIC_NUMBER 43
52 #define AHCP_VERSION_1 1
53 #define AHCP1_HEADER_FIX_LEN 24
54 #define AHCP1_BODY_MIN_LEN 4
56 #define AHCP1_MSG_DISCOVER 0
57 #define AHCP1_MSG_OFFER 1
58 #define AHCP1_MSG_REQUEST 2
59 #define AHCP1_MSG_ACK 3
60 #define AHCP1_MSG_NACK 4
61 #define AHCP1_MSG_RELEASE 5
63 static const struct tok ahcp1_msg_str
[] = {
64 { AHCP1_MSG_DISCOVER
, "Discover" },
65 { AHCP1_MSG_OFFER
, "Offer" },
66 { AHCP1_MSG_REQUEST
, "Request" },
67 { AHCP1_MSG_ACK
, "Ack" },
68 { AHCP1_MSG_NACK
, "Nack" },
69 { AHCP1_MSG_RELEASE
, "Release" },
73 #define AHCP1_OPT_PAD 0
74 #define AHCP1_OPT_MANDATORY 1
75 #define AHCP1_OPT_ORIGIN_TIME 2
76 #define AHCP1_OPT_EXPIRES 3
77 #define AHCP1_OPT_MY_IPV6_ADDRESS 4
78 #define AHCP1_OPT_MY_IPV4_ADDRESS 5
79 #define AHCP1_OPT_IPV6_PREFIX 6
80 #define AHCP1_OPT_IPV4_PREFIX 7
81 #define AHCP1_OPT_IPV6_ADDRESS 8
82 #define AHCP1_OPT_IPV4_ADDRESS 9
83 #define AHCP1_OPT_IPV6_PREFIX_DELEGATION 10
84 #define AHCP1_OPT_IPV4_PREFIX_DELEGATION 11
85 #define AHCP1_OPT_NAME_SERVER 12
86 #define AHCP1_OPT_NTP_SERVER 13
87 #define AHCP1_OPT_MAX 13
89 static const struct tok ahcp1_opt_str
[] = {
90 { AHCP1_OPT_PAD
, "Pad" },
91 { AHCP1_OPT_MANDATORY
, "Mandatory" },
92 { AHCP1_OPT_ORIGIN_TIME
, "Origin Time" },
93 { AHCP1_OPT_EXPIRES
, "Expires" },
94 { AHCP1_OPT_MY_IPV6_ADDRESS
, "My-IPv6-Address" },
95 { AHCP1_OPT_MY_IPV4_ADDRESS
, "My-IPv4-Address" },
96 { AHCP1_OPT_IPV6_PREFIX
, "IPv6 Prefix" },
97 { AHCP1_OPT_IPV4_PREFIX
, "IPv4 Prefix" },
98 { AHCP1_OPT_IPV6_ADDRESS
, "IPv6 Address" },
99 { AHCP1_OPT_IPV4_ADDRESS
, "IPv4 Address" },
100 { AHCP1_OPT_IPV6_PREFIX_DELEGATION
, "IPv6 Prefix Delegation" },
101 { AHCP1_OPT_IPV4_PREFIX_DELEGATION
, "IPv4 Prefix Delegation" },
102 { AHCP1_OPT_NAME_SERVER
, "Name Server" },
103 { AHCP1_OPT_NTP_SERVER
, "NTP Server" },
108 ahcp_time_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
117 t
= EXTRACT_32BITS(cp
);
118 if (NULL
== (tm
= gmtime(&t
)))
119 ND_PRINT((ndo
, ": gmtime() error"));
120 else if (0 == strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
))
121 ND_PRINT((ndo
, ": strftime() error"));
123 ND_PRINT((ndo
, ": %s UTC", buf
));
127 ND_PRINT((ndo
, ": %s", cstr
));
128 ND_TCHECK2(*cp
, ep
- cp
);
131 ND_PRINT((ndo
, "%s", tstr
));
136 ahcp_seconds_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
141 ND_PRINT((ndo
, ": %us", EXTRACT_32BITS(cp
)));
145 ND_PRINT((ndo
, ": %s", cstr
));
146 ND_TCHECK2(*cp
, ep
- cp
);
149 ND_PRINT((ndo
, "%s", tstr
));
154 ahcp_ipv6_addresses_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
156 const char *sep
= ": ";
163 ND_PRINT((ndo
, "%s%s", sep
, ip6addr_string(ndo
, cp
)));
165 ND_PRINT((ndo
, "%s(compiled w/o IPv6)", sep
));
173 ND_PRINT((ndo
, ": %s", cstr
));
174 ND_TCHECK2(*cp
, ep
- cp
);
177 ND_PRINT((ndo
, "%s", tstr
));
182 ahcp_ipv4_addresses_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
184 const char *sep
= ": ";
190 ND_PRINT((ndo
, "%s%s", sep
, ipaddr_string(ndo
, cp
)));
197 ND_PRINT((ndo
, ": %s", cstr
));
198 ND_TCHECK2(*cp
, ep
- cp
);
201 ND_PRINT((ndo
, "%s", tstr
));
206 ahcp_ipv6_prefixes_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
208 const char *sep
= ": ";
215 ND_PRINT((ndo
, "%s%s/%u", sep
, ip6addr_string(ndo
, cp
), *(cp
+ 16)));
217 ND_PRINT((ndo
, "%s(compiled w/o IPv6)/%u", sep
, *(cp
+ 16)));
225 ND_PRINT((ndo
, ": %s", cstr
));
226 ND_TCHECK2(*cp
, ep
- cp
);
229 ND_PRINT((ndo
, "%s", tstr
));
234 ahcp_ipv4_prefixes_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
236 const char *sep
= ": ";
242 ND_PRINT((ndo
, "%s%s/%u", sep
, ipaddr_string(ndo
, cp
), *(cp
+ 4)));
249 ND_PRINT((ndo
, ": %s", cstr
));
250 ND_TCHECK2(*cp
, ep
- cp
);
253 ND_PRINT((ndo
, "%s", tstr
));
257 /* Data decoders signal truncated data with -1. */
259 (* const data_decoders
[AHCP1_OPT_MAX
+ 1])(netdissect_options
*, const u_char
*, const u_char
*) = {
260 /* [AHCP1_OPT_PAD] = */ NULL
,
261 /* [AHCP1_OPT_MANDATORY] = */ NULL
,
262 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print
,
263 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print
,
264 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
265 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
266 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print
,
267 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL
,
268 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
269 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
270 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print
,
271 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print
,
272 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print
,
273 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print
,
277 ahcp1_options_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
279 uint8_t option_no
, option_len
;
286 ND_PRINT((ndo
, "\n\t %s", tok2str(ahcp1_opt_str
, "Unknown-%u", option_no
)));
287 if (option_no
== AHCP1_OPT_PAD
|| option_no
== AHCP1_OPT_MANDATORY
)
295 if (cp
+ option_len
> ep
)
298 if (option_no
<= AHCP1_OPT_MAX
&& data_decoders
[option_no
] != NULL
) {
299 if (data_decoders
[option_no
](ndo
, cp
, cp
+ option_len
) < 0)
300 break; /* truncated and already marked up */
302 ND_PRINT((ndo
, " (Length %u)", option_len
));
303 ND_TCHECK2(*cp
, option_len
);
310 ND_PRINT((ndo
, " %s", cstr
));
311 ND_TCHECK2(*cp
, ep
- cp
);
314 ND_PRINT((ndo
, "%s", tstr
));
318 ahcp1_body_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
323 if (cp
+ AHCP1_BODY_MIN_LEN
> ep
)
335 body_len
= EXTRACT_16BITS(cp
);
338 if (ndo
->ndo_vflag
) {
339 ND_PRINT((ndo
, "\n\t%s", tok2str(ahcp1_msg_str
, "Unknown-%u", type
)));
341 ND_PRINT((ndo
, ", MBZ %u", mbz
));
342 ND_PRINT((ndo
, ", Length %u", body_len
));
344 if (cp
+ body_len
> ep
)
348 if (ndo
->ndo_vflag
>= 2)
349 ahcp1_options_print(ndo
, cp
, cp
+ body_len
); /* not ep (ignore extra data) */
351 ND_TCHECK2(*cp
, body_len
);
355 ND_PRINT((ndo
, " %s", cstr
));
356 ND_TCHECK2(*cp
, ep
- cp
);
359 ND_PRINT((ndo
, "%s", tstr
));
363 ahcp_print(netdissect_options
*ndo
, const u_char
*cp
, const u_int len
)
365 const u_char
*ep
= cp
+ len
;
368 ND_PRINT((ndo
, "AHCP"));
373 if (*cp
!= AHCP_MAGIC_NUMBER
)
381 case AHCP_VERSION_1
: {
382 ND_PRINT((ndo
, " Version 1"));
383 if (len
< AHCP1_HEADER_FIX_LEN
)
385 if (!ndo
->ndo_vflag
) {
386 ND_TCHECK2(*cp
, AHCP1_HEADER_FIX_LEN
- 2);
387 cp
+= AHCP1_HEADER_FIX_LEN
- 2;
391 ND_PRINT((ndo
, "\n\tHopcount %u", *cp
));
393 /* Original Hopcount */
395 ND_PRINT((ndo
, ", Original Hopcount %u", *cp
));
399 ND_PRINT((ndo
, ", Nonce 0x%08x", EXTRACT_32BITS(cp
)));
403 ND_PRINT((ndo
, ", Source Id %s", linkaddr_string(ndo
, cp
, 0, 8)));
407 ND_PRINT((ndo
, ", Destination Id %s", linkaddr_string(ndo
, cp
, 0, 8)));
411 ahcp1_body_print(ndo
, cp
, ep
);
415 ND_PRINT((ndo
, " Version %u (unknown)", version
));
421 ND_PRINT((ndo
, " %s", cstr
));
422 ND_TCHECK2(*cp
, ep
- cp
);
425 ND_PRINT((ndo
, "%s", tstr
));