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.
36 #include <tcpdump-stdinc.h>
38 #include "interface.h"
40 #include "addrtoname.h"
42 static const char *corrupt_str
= "(corrupt)";
43 static const char *trunc_str
= "[|ahcp]";
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
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
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" },
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
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" },
102 ahcp_time_print(const u_char
*cp
, const u_char
*ep
) {
110 t
= EXTRACT_32BITS(cp
);
111 if (NULL
== (tm
= gmtime(&t
)))
112 printf(": gmtime() error");
113 else if (0 == strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
))
114 printf(": strftime() error");
116 printf(": %s UTC", buf
);
120 printf(": %s", corrupt_str
);
121 TCHECK2(*cp
, ep
- cp
);
124 printf(" %s", trunc_str
);
129 ahcp_seconds_print(const u_char
*cp
, const u_char
*ep
) {
133 printf(": %us", EXTRACT_32BITS(cp
));
137 printf(": %s", corrupt_str
);
138 TCHECK2(*cp
, ep
- cp
);
141 printf(" %s", trunc_str
);
146 ahcp_ipv6_addresses_print(const u_char
*cp
, const u_char
*ep
) {
147 const char *sep
= ": ";
157 "(compiled w/o IPv6)"
166 printf(": %s", corrupt_str
);
167 TCHECK2(*cp
, ep
- cp
);
170 printf(" %s", trunc_str
);
175 ahcp_ipv4_addresses_print(const u_char
*cp
, const u_char
*ep
) {
176 const char *sep
= ": ";
182 printf("%s%s", sep
, ipaddr_string(cp
));
189 printf(": %s", corrupt_str
);
190 TCHECK2(*cp
, ep
- cp
);
193 printf(" %s", trunc_str
);
198 ahcp_ipv6_prefixes_print(const u_char
*cp
, const u_char
*ep
) {
199 const char *sep
= ": ";
205 printf("%s%s/%u", sep
,
209 "(compiled w/o IPv6)",
218 printf(": %s", corrupt_str
);
219 TCHECK2(*cp
, ep
- cp
);
222 printf(" %s", trunc_str
);
227 ahcp_ipv4_prefixes_print(const u_char
*cp
, const u_char
*ep
) {
228 const char *sep
= ": ";
234 printf("%s%s/%u", sep
, ipaddr_string(cp
), *(cp
+ 4));
241 printf(": %s", corrupt_str
);
242 TCHECK2(*cp
, ep
- cp
);
245 printf(" %s", trunc_str
);
249 /* Data decoders signal truncated data with -1. */
251 (* const data_decoders
[AHCP1_OPT_MAX
+ 1])(const u_char
*, const u_char
*) = {
252 /* [AHCP1_OPT_PAD] = */ NULL
,
253 /* [AHCP1_OPT_MANDATORY] = */ NULL
,
254 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print
,
255 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print
,
256 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
257 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
258 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print
,
259 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL
,
260 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
261 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
262 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print
,
263 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print
,
264 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print
,
265 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print
,
269 ahcp1_options_print(const u_char
*cp
, const u_char
*ep
) {
270 uint8_t option_no
, option_len
;
277 printf("\n\t %s", tok2str(ahcp1_opt_str
, "Unknown-%u", option_no
));
278 if (option_no
== AHCP1_OPT_PAD
|| option_no
== AHCP1_OPT_MANDATORY
)
286 if (cp
+ option_len
> ep
)
289 if (option_no
<= AHCP1_OPT_MAX
&& data_decoders
[option_no
] != NULL
) {
290 if (data_decoders
[option_no
](cp
, cp
+ option_len
) < 0)
291 break; /* truncated and already marked up */
293 printf(" (Length %u)", option_len
);
294 TCHECK2(*cp
, option_len
);
301 printf(" %s", corrupt_str
);
302 TCHECK2(*cp
, ep
- cp
);
305 printf(" %s", trunc_str
);
309 ahcp1_body_print(const u_char
*cp
, const u_char
*ep
) {
313 if (cp
+ AHCP1_BODY_MIN_LEN
> ep
)
325 body_len
= EXTRACT_16BITS(cp
);
329 printf("\n\t%s", tok2str(ahcp1_msg_str
, "Unknown-%u", type
));
331 printf(", MBZ %u", mbz
);
332 printf(", Length %u", body_len
);
334 if (cp
+ body_len
> ep
)
339 ahcp1_options_print(cp
, cp
+ body_len
); /* not ep (ignore extra data) */
341 TCHECK2(*cp
, body_len
);
345 printf(" %s", corrupt_str
);
346 TCHECK2(*cp
, ep
- cp
);
349 printf(" %s", trunc_str
);
353 ahcp_print(const u_char
*cp
, const u_int len
) {
354 const u_char
*ep
= cp
+ len
;
362 if (*cp
!= AHCP_MAGIC_NUMBER
)
370 case AHCP_VERSION_1
: {
371 printf(" Version 1");
372 if (len
< AHCP1_HEADER_FIX_LEN
)
375 TCHECK2(*cp
, AHCP1_HEADER_FIX_LEN
- 2);
376 cp
+= AHCP1_HEADER_FIX_LEN
- 2;
380 printf("\n\tHopcount %u", *cp
);
382 /* Original Hopcount */
384 printf(", Original Hopcount %u", *cp
);
388 printf(", Nonce 0x%08x", EXTRACT_32BITS(cp
));
392 printf(", Source Id %s", linkaddr_string(cp
, 0, 8));
396 printf(", Destination Id %s", linkaddr_string(cp
, 0, 8));
400 ahcp1_body_print(cp
, ep
);
404 printf(" Version %u (unknown)", version
);
410 printf(" %s", corrupt_str
);
411 TCHECK2(*cp
, ep
- cp
);
414 printf(" %s", trunc_str
);