2 * Copyright (c) 2013 The TCPDUMP project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 /* \summary: Ad Hoc Configuration Protocol (AHCP) printer */
30 /* Based on draft-chroboczek-ahcp-00 and source code of ahcpd-0.53 */
36 #include "netdissect-stdinc.h"
38 #define ND_LONGJMP_FROM_TCHECK
39 #include "netdissect.h"
41 #include "addrtoname.h"
44 #define AHCP_MAGIC_NUMBER 43
45 #define AHCP_VERSION_1 1
46 #define AHCP1_HEADER_FIX_LEN 24
47 #define AHCP1_BODY_MIN_LEN 4
49 #define AHCP1_MSG_DISCOVER 0
50 #define AHCP1_MSG_OFFER 1
51 #define AHCP1_MSG_REQUEST 2
52 #define AHCP1_MSG_ACK 3
53 #define AHCP1_MSG_NACK 4
54 #define AHCP1_MSG_RELEASE 5
56 static const struct tok ahcp1_msg_str
[] = {
57 { AHCP1_MSG_DISCOVER
, "Discover" },
58 { AHCP1_MSG_OFFER
, "Offer" },
59 { AHCP1_MSG_REQUEST
, "Request" },
60 { AHCP1_MSG_ACK
, "Ack" },
61 { AHCP1_MSG_NACK
, "Nack" },
62 { AHCP1_MSG_RELEASE
, "Release" },
66 #define AHCP1_OPT_PAD 0
67 #define AHCP1_OPT_MANDATORY 1
68 #define AHCP1_OPT_ORIGIN_TIME 2
69 #define AHCP1_OPT_EXPIRES 3
70 #define AHCP1_OPT_MY_IPV6_ADDRESS 4
71 #define AHCP1_OPT_MY_IPV4_ADDRESS 5
72 #define AHCP1_OPT_IPV6_PREFIX 6
73 #define AHCP1_OPT_IPV4_PREFIX 7
74 #define AHCP1_OPT_IPV6_ADDRESS 8
75 #define AHCP1_OPT_IPV4_ADDRESS 9
76 #define AHCP1_OPT_IPV6_PREFIX_DELEGATION 10
77 #define AHCP1_OPT_IPV4_PREFIX_DELEGATION 11
78 #define AHCP1_OPT_NAME_SERVER 12
79 #define AHCP1_OPT_NTP_SERVER 13
80 #define AHCP1_OPT_MAX 13
82 static const struct tok ahcp1_opt_str
[] = {
83 { AHCP1_OPT_PAD
, "Pad" },
84 { AHCP1_OPT_MANDATORY
, "Mandatory" },
85 { AHCP1_OPT_ORIGIN_TIME
, "Origin Time" },
86 { AHCP1_OPT_EXPIRES
, "Expires" },
87 { AHCP1_OPT_MY_IPV6_ADDRESS
, "My-IPv6-Address" },
88 { AHCP1_OPT_MY_IPV4_ADDRESS
, "My-IPv4-Address" },
89 { AHCP1_OPT_IPV6_PREFIX
, "IPv6 Prefix" },
90 { AHCP1_OPT_IPV4_PREFIX
, "IPv4 Prefix" },
91 { AHCP1_OPT_IPV6_ADDRESS
, "IPv6 Address" },
92 { AHCP1_OPT_IPV4_ADDRESS
, "IPv4 Address" },
93 { AHCP1_OPT_IPV6_PREFIX_DELEGATION
, "IPv6 Prefix Delegation" },
94 { AHCP1_OPT_IPV4_PREFIX_DELEGATION
, "IPv4 Prefix Delegation" },
95 { AHCP1_OPT_NAME_SERVER
, "Name Server" },
96 { AHCP1_OPT_NTP_SERVER
, "NTP Server" },
101 ahcp_time_print(netdissect_options
*ndo
,
102 const u_char
*cp
, uint8_t len
)
111 if (NULL
== (tm
= gmtime(&t
)))
112 ND_PRINT(": gmtime() error");
113 else if (0 == strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
))
114 ND_PRINT(": strftime() error");
116 ND_PRINT(": %s UTC", buf
);
120 nd_print_invalid(ndo
);
121 ND_TCHECK_LEN(cp
, len
);
125 ahcp_seconds_print(netdissect_options
*ndo
,
126 const u_char
*cp
, uint8_t len
)
130 ND_PRINT(": %us", GET_BE_U_4(cp
));
134 nd_print_invalid(ndo
);
135 ND_TCHECK_LEN(cp
, len
);
139 ahcp_ipv6_addresses_print(netdissect_options
*ndo
,
140 const u_char
*cp
, uint8_t len
)
142 const char *sep
= ": ";
147 ND_PRINT("%s%s", sep
, GET_IP6ADDR_STRING(cp
));
155 nd_print_invalid(ndo
);
156 ND_TCHECK_LEN(cp
, len
);
160 ahcp_ipv4_addresses_print(netdissect_options
*ndo
,
161 const u_char
*cp
, uint8_t len
)
163 const char *sep
= ": ";
168 ND_PRINT("%s%s", sep
, GET_IPADDR_STRING(cp
));
176 nd_print_invalid(ndo
);
177 ND_TCHECK_LEN(cp
, len
);
181 ahcp_ipv6_prefixes_print(netdissect_options
*ndo
,
182 const u_char
*cp
, uint8_t len
)
184 const char *sep
= ": ";
189 ND_PRINT("%s%s/%u", sep
, GET_IP6ADDR_STRING(cp
), GET_U_1(cp
+ 16));
197 nd_print_invalid(ndo
);
198 ND_TCHECK_LEN(cp
, len
);
202 ahcp_ipv4_prefixes_print(netdissect_options
*ndo
,
203 const u_char
*cp
, uint8_t len
)
205 const char *sep
= ": ";
210 ND_PRINT("%s%s/%u", sep
, GET_IPADDR_STRING(cp
), GET_U_1(cp
+ 4));
218 nd_print_invalid(ndo
);
219 ND_TCHECK_LEN(cp
, len
);
223 (* const data_decoders
[AHCP1_OPT_MAX
+ 1])(netdissect_options
*, const u_char
*, uint8_t) = {
224 /* [AHCP1_OPT_PAD] = */ NULL
,
225 /* [AHCP1_OPT_MANDATORY] = */ NULL
,
226 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print
,
227 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print
,
228 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
229 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
230 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print
,
231 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL
,
232 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
233 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
234 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print
,
235 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print
,
236 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print
,
237 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print
,
241 ahcp1_options_print(netdissect_options
*ndo
,
242 const u_char
*cp
, uint16_t len
)
245 uint8_t option_no
, option_len
;
248 option_no
= GET_U_1(cp
);
251 ND_PRINT("\n\t %s", tok2str(ahcp1_opt_str
, "Unknown-%u", option_no
));
252 if (option_no
== AHCP1_OPT_PAD
|| option_no
== AHCP1_OPT_MANDATORY
)
257 option_len
= GET_U_1(cp
);
260 if (option_len
> len
)
263 if (option_no
<= AHCP1_OPT_MAX
&& data_decoders
[option_no
] != NULL
) {
264 data_decoders
[option_no
](ndo
, cp
, option_len
);
266 ND_PRINT(" (Length %u)", option_len
);
267 ND_TCHECK_LEN(cp
, option_len
);
275 nd_print_invalid(ndo
);
276 ND_TCHECK_LEN(cp
, len
);
280 ahcp1_body_print(netdissect_options
*ndo
,
281 const u_char
*cp
, u_int len
)
286 if (len
< AHCP1_BODY_MIN_LEN
)
297 body_len
= GET_BE_U_2(cp
);
301 if (ndo
->ndo_vflag
) {
302 ND_PRINT("\n\t%s", tok2str(ahcp1_msg_str
, "Unknown-%u", type
));
304 ND_PRINT(", MBZ %u", mbz
);
305 ND_PRINT(", Length %u", body_len
);
311 /* Here use "body_len", not "len" (ignore any extra data). */
312 if (ndo
->ndo_vflag
>= 2)
313 ahcp1_options_print(ndo
, cp
, body_len
);
315 ND_TCHECK_LEN(cp
, body_len
);
319 nd_print_invalid(ndo
);
320 ND_TCHECK_LEN(cp
, len
);
325 ahcp_print(netdissect_options
*ndo
,
326 const u_char
*cp
, u_int len
)
330 ndo
->ndo_protocol
= "ahcp";
331 nd_print_protocol_caps(ndo
);
335 if (GET_U_1(cp
) != AHCP_MAGIC_NUMBER
)
340 version
= GET_U_1(cp
);
344 case AHCP_VERSION_1
: {
345 ND_PRINT(" Version 1");
346 if (len
< AHCP1_HEADER_FIX_LEN
- 2)
348 if (!ndo
->ndo_vflag
) {
349 ND_TCHECK_LEN(cp
, AHCP1_HEADER_FIX_LEN
- 2);
350 cp
+= AHCP1_HEADER_FIX_LEN
- 2;
351 len
-= AHCP1_HEADER_FIX_LEN
- 2;
354 ND_PRINT("\n\tHopcount %u", GET_U_1(cp
));
357 /* Original Hopcount */
358 ND_PRINT(", Original Hopcount %u", GET_U_1(cp
));
362 ND_PRINT(", Nonce 0x%08x", GET_BE_U_4(cp
));
366 ND_PRINT(", Source Id %s", GET_LINKADDR_STRING(cp
, LINKADDR_OTHER
, 8));
370 ND_PRINT(", Destination Id %s", GET_LINKADDR_STRING(cp
, LINKADDR_OTHER
, 8));
375 ahcp1_body_print(ndo
, cp
, len
);
379 ND_PRINT(" Version %u (unknown)", version
);
380 ND_TCHECK_LEN(cp
, len
);
386 nd_print_invalid(ndo
);
387 ND_TCHECK_LEN(cp
, len
);