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 */
34 #include "netdissect-stdinc.h"
36 #define ND_LONGJMP_FROM_TCHECK
37 #include "netdissect.h"
39 #include "addrtoname.h"
42 #define AHCP_MAGIC_NUMBER 43
43 #define AHCP_VERSION_1 1
44 #define AHCP1_HEADER_FIX_LEN 24
45 #define AHCP1_BODY_MIN_LEN 4
47 #define AHCP1_MSG_DISCOVER 0
48 #define AHCP1_MSG_OFFER 1
49 #define AHCP1_MSG_REQUEST 2
50 #define AHCP1_MSG_ACK 3
51 #define AHCP1_MSG_NACK 4
52 #define AHCP1_MSG_RELEASE 5
54 static const struct tok ahcp1_msg_str
[] = {
55 { AHCP1_MSG_DISCOVER
, "Discover" },
56 { AHCP1_MSG_OFFER
, "Offer" },
57 { AHCP1_MSG_REQUEST
, "Request" },
58 { AHCP1_MSG_ACK
, "Ack" },
59 { AHCP1_MSG_NACK
, "Nack" },
60 { AHCP1_MSG_RELEASE
, "Release" },
64 #define AHCP1_OPT_PAD 0
65 #define AHCP1_OPT_MANDATORY 1
66 #define AHCP1_OPT_ORIGIN_TIME 2
67 #define AHCP1_OPT_EXPIRES 3
68 #define AHCP1_OPT_MY_IPV6_ADDRESS 4
69 #define AHCP1_OPT_MY_IPV4_ADDRESS 5
70 #define AHCP1_OPT_IPV6_PREFIX 6
71 #define AHCP1_OPT_IPV4_PREFIX 7
72 #define AHCP1_OPT_IPV6_ADDRESS 8
73 #define AHCP1_OPT_IPV4_ADDRESS 9
74 #define AHCP1_OPT_IPV6_PREFIX_DELEGATION 10
75 #define AHCP1_OPT_IPV4_PREFIX_DELEGATION 11
76 #define AHCP1_OPT_NAME_SERVER 12
77 #define AHCP1_OPT_NTP_SERVER 13
78 #define AHCP1_OPT_MAX 13
80 static const struct tok ahcp1_opt_str
[] = {
81 { AHCP1_OPT_PAD
, "Pad" },
82 { AHCP1_OPT_MANDATORY
, "Mandatory" },
83 { AHCP1_OPT_ORIGIN_TIME
, "Origin Time" },
84 { AHCP1_OPT_EXPIRES
, "Expires" },
85 { AHCP1_OPT_MY_IPV6_ADDRESS
, "My-IPv6-Address" },
86 { AHCP1_OPT_MY_IPV4_ADDRESS
, "My-IPv4-Address" },
87 { AHCP1_OPT_IPV6_PREFIX
, "IPv6 Prefix" },
88 { AHCP1_OPT_IPV4_PREFIX
, "IPv4 Prefix" },
89 { AHCP1_OPT_IPV6_ADDRESS
, "IPv6 Address" },
90 { AHCP1_OPT_IPV4_ADDRESS
, "IPv4 Address" },
91 { AHCP1_OPT_IPV6_PREFIX_DELEGATION
, "IPv6 Prefix Delegation" },
92 { AHCP1_OPT_IPV4_PREFIX_DELEGATION
, "IPv4 Prefix Delegation" },
93 { AHCP1_OPT_NAME_SERVER
, "Name Server" },
94 { AHCP1_OPT_NTP_SERVER
, "NTP Server" },
99 ahcp_time_print(netdissect_options
*ndo
,
100 const u_char
*cp
, uint8_t len
)
103 char buf
[sizeof("-yyyyyyyyyy-mm-dd hh:mm:ss UTC")];
109 nd_format_time(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S UTC",
114 nd_print_invalid(ndo
);
115 ND_TCHECK_LEN(cp
, len
);
119 ahcp_seconds_print(netdissect_options
*ndo
,
120 const u_char
*cp
, uint8_t len
)
124 ND_PRINT(": %us", GET_BE_U_4(cp
));
128 nd_print_invalid(ndo
);
129 ND_TCHECK_LEN(cp
, len
);
133 ahcp_ipv6_addresses_print(netdissect_options
*ndo
,
134 const u_char
*cp
, uint8_t len
)
136 const char *sep
= ": ";
141 ND_PRINT("%s%s", sep
, GET_IP6ADDR_STRING(cp
));
149 nd_print_invalid(ndo
);
150 ND_TCHECK_LEN(cp
, len
);
154 ahcp_ipv4_addresses_print(netdissect_options
*ndo
,
155 const u_char
*cp
, uint8_t len
)
157 const char *sep
= ": ";
162 ND_PRINT("%s%s", sep
, GET_IPADDR_STRING(cp
));
170 nd_print_invalid(ndo
);
171 ND_TCHECK_LEN(cp
, len
);
175 ahcp_ipv6_prefixes_print(netdissect_options
*ndo
,
176 const u_char
*cp
, uint8_t len
)
178 const char *sep
= ": ";
183 ND_PRINT("%s%s/%u", sep
, GET_IP6ADDR_STRING(cp
), GET_U_1(cp
+ 16));
191 nd_print_invalid(ndo
);
192 ND_TCHECK_LEN(cp
, len
);
196 ahcp_ipv4_prefixes_print(netdissect_options
*ndo
,
197 const u_char
*cp
, uint8_t len
)
199 const char *sep
= ": ";
204 ND_PRINT("%s%s/%u", sep
, GET_IPADDR_STRING(cp
), GET_U_1(cp
+ 4));
212 nd_print_invalid(ndo
);
213 ND_TCHECK_LEN(cp
, len
);
217 (* const data_decoders
[AHCP1_OPT_MAX
+ 1])(netdissect_options
*, const u_char
*, uint8_t) = {
218 /* [AHCP1_OPT_PAD] = */ NULL
,
219 /* [AHCP1_OPT_MANDATORY] = */ NULL
,
220 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print
,
221 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print
,
222 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
223 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
224 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print
,
225 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL
,
226 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
227 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
228 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print
,
229 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print
,
230 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print
,
231 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print
,
235 ahcp1_options_print(netdissect_options
*ndo
,
236 const u_char
*cp
, uint16_t len
)
239 uint8_t option_no
, option_len
;
242 option_no
= GET_U_1(cp
);
245 ND_PRINT("\n\t %s", tok2str(ahcp1_opt_str
, "Unknown-%u", option_no
));
246 if (option_no
== AHCP1_OPT_PAD
|| option_no
== AHCP1_OPT_MANDATORY
)
251 option_len
= GET_U_1(cp
);
254 if (option_len
> len
)
257 if (option_no
<= AHCP1_OPT_MAX
&& data_decoders
[option_no
] != NULL
) {
258 data_decoders
[option_no
](ndo
, cp
, option_len
);
260 ND_PRINT(" (Length %u)", option_len
);
261 ND_TCHECK_LEN(cp
, option_len
);
269 nd_print_invalid(ndo
);
270 ND_TCHECK_LEN(cp
, len
);
274 ahcp1_body_print(netdissect_options
*ndo
,
275 const u_char
*cp
, u_int len
)
280 if (len
< AHCP1_BODY_MIN_LEN
)
291 body_len
= GET_BE_U_2(cp
);
295 if (ndo
->ndo_vflag
) {
296 ND_PRINT("\n\t%s", tok2str(ahcp1_msg_str
, "Unknown-%u", type
));
298 ND_PRINT(", MBZ %u", mbz
);
299 ND_PRINT(", Length %u", body_len
);
305 /* Here use "body_len", not "len" (ignore any extra data). */
306 if (ndo
->ndo_vflag
>= 2)
307 ahcp1_options_print(ndo
, cp
, body_len
);
309 ND_TCHECK_LEN(cp
, body_len
);
313 nd_print_invalid(ndo
);
314 ND_TCHECK_LEN(cp
, len
);
319 ahcp_print(netdissect_options
*ndo
,
320 const u_char
*cp
, u_int len
)
324 ndo
->ndo_protocol
= "ahcp";
325 nd_print_protocol_caps(ndo
);
329 if (GET_U_1(cp
) != AHCP_MAGIC_NUMBER
)
334 version
= GET_U_1(cp
);
338 case AHCP_VERSION_1
: {
339 ND_PRINT(" Version 1");
340 if (len
< AHCP1_HEADER_FIX_LEN
- 2)
342 if (!ndo
->ndo_vflag
) {
343 ND_TCHECK_LEN(cp
, AHCP1_HEADER_FIX_LEN
- 2);
344 cp
+= AHCP1_HEADER_FIX_LEN
- 2;
345 len
-= AHCP1_HEADER_FIX_LEN
- 2;
348 ND_PRINT("\n\tHopcount %u", GET_U_1(cp
));
351 /* Original Hopcount */
352 ND_PRINT(", Original Hopcount %u", GET_U_1(cp
));
356 ND_PRINT(", Nonce 0x%08x", GET_BE_U_4(cp
));
360 ND_PRINT(", Source Id %s", GET_LINKADDR_STRING(cp
, LINKADDR_OTHER
, 8));
364 ND_PRINT(", Destination Id %s", GET_LINKADDR_STRING(cp
, LINKADDR_OTHER
, 8));
369 ahcp1_body_print(ndo
, cp
, len
);
373 ND_PRINT(" Version %u (unknown)", version
);
374 ND_TCHECK_LEN(cp
, len
);
380 nd_print_invalid(ndo
);
381 ND_TCHECK_LEN(cp
, len
);