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 #include "netdissect.h"
40 #include "addrtoname.h"
42 static const char tstr
[] = " [|ahcp]";
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
, const u_char
*cp
, const u_char
*ep
)
110 t
= EXTRACT_32BITS(cp
);
111 if (NULL
== (tm
= gmtime(&t
)))
112 ND_PRINT((ndo
, ": gmtime() error"));
113 else if (0 == strftime(buf
, sizeof(buf
), "%Y-%m-%d %H:%M:%S", tm
))
114 ND_PRINT((ndo
, ": strftime() error"));
116 ND_PRINT((ndo
, ": %s UTC", buf
));
120 ND_PRINT((ndo
, "%s", istr
));
121 ND_TCHECK2(*cp
, ep
- cp
);
124 ND_PRINT((ndo
, "%s", tstr
));
129 ahcp_seconds_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
134 ND_PRINT((ndo
, ": %us", EXTRACT_32BITS(cp
)));
138 ND_PRINT((ndo
, "%s", istr
));
139 ND_TCHECK2(*cp
, ep
- cp
);
142 ND_PRINT((ndo
, "%s", tstr
));
147 ahcp_ipv6_addresses_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
149 const char *sep
= ": ";
155 ND_PRINT((ndo
, "%s%s", sep
, ip6addr_string(ndo
, cp
)));
162 ND_PRINT((ndo
, "%s", istr
));
163 ND_TCHECK2(*cp
, ep
- cp
);
166 ND_PRINT((ndo
, "%s", tstr
));
171 ahcp_ipv4_addresses_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
173 const char *sep
= ": ";
179 ND_PRINT((ndo
, "%s%s", sep
, ipaddr_string(ndo
, cp
)));
186 ND_PRINT((ndo
, "%s", istr
));
187 ND_TCHECK2(*cp
, ep
- cp
);
190 ND_PRINT((ndo
, "%s", tstr
));
195 ahcp_ipv6_prefixes_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
197 const char *sep
= ": ";
203 ND_PRINT((ndo
, "%s%s/%u", sep
, ip6addr_string(ndo
, cp
), *(cp
+ 16)));
210 ND_PRINT((ndo
, "%s", istr
));
211 ND_TCHECK2(*cp
, ep
- cp
);
214 ND_PRINT((ndo
, "%s", tstr
));
219 ahcp_ipv4_prefixes_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
221 const char *sep
= ": ";
227 ND_PRINT((ndo
, "%s%s/%u", sep
, ipaddr_string(ndo
, cp
), *(cp
+ 4)));
234 ND_PRINT((ndo
, "%s", istr
));
235 ND_TCHECK2(*cp
, ep
- cp
);
238 ND_PRINT((ndo
, "%s", tstr
));
242 /* Data decoders signal truncated data with -1. */
244 (* const data_decoders
[AHCP1_OPT_MAX
+ 1])(netdissect_options
*, const u_char
*, const u_char
*) = {
245 /* [AHCP1_OPT_PAD] = */ NULL
,
246 /* [AHCP1_OPT_MANDATORY] = */ NULL
,
247 /* [AHCP1_OPT_ORIGIN_TIME] = */ ahcp_time_print
,
248 /* [AHCP1_OPT_EXPIRES] = */ ahcp_seconds_print
,
249 /* [AHCP1_OPT_MY_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
250 /* [AHCP1_OPT_MY_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
251 /* [AHCP1_OPT_IPV6_PREFIX] = */ ahcp_ipv6_prefixes_print
,
252 /* [AHCP1_OPT_IPV4_PREFIX] = */ NULL
,
253 /* [AHCP1_OPT_IPV6_ADDRESS] = */ ahcp_ipv6_addresses_print
,
254 /* [AHCP1_OPT_IPV4_ADDRESS] = */ ahcp_ipv4_addresses_print
,
255 /* [AHCP1_OPT_IPV6_PREFIX_DELEGATION] = */ ahcp_ipv6_prefixes_print
,
256 /* [AHCP1_OPT_IPV4_PREFIX_DELEGATION] = */ ahcp_ipv4_prefixes_print
,
257 /* [AHCP1_OPT_NAME_SERVER] = */ ahcp_ipv6_addresses_print
,
258 /* [AHCP1_OPT_NTP_SERVER] = */ ahcp_ipv6_addresses_print
,
262 ahcp1_options_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
264 uint8_t option_no
, option_len
;
271 ND_PRINT((ndo
, "\n\t %s", tok2str(ahcp1_opt_str
, "Unknown-%u", option_no
)));
272 if (option_no
== AHCP1_OPT_PAD
|| option_no
== AHCP1_OPT_MANDATORY
)
280 if (cp
+ option_len
> ep
)
283 if (option_no
<= AHCP1_OPT_MAX
&& data_decoders
[option_no
] != NULL
) {
284 if (data_decoders
[option_no
](ndo
, cp
, cp
+ option_len
) < 0)
285 break; /* truncated and already marked up */
287 ND_PRINT((ndo
, " (Length %u)", option_len
));
288 ND_TCHECK2(*cp
, option_len
);
295 ND_PRINT((ndo
, "%s", istr
));
296 ND_TCHECK2(*cp
, ep
- cp
);
299 ND_PRINT((ndo
, "%s", tstr
));
303 ahcp1_body_print(netdissect_options
*ndo
, const u_char
*cp
, const u_char
*ep
)
308 if (cp
+ AHCP1_BODY_MIN_LEN
> ep
)
320 body_len
= EXTRACT_16BITS(cp
);
323 if (ndo
->ndo_vflag
) {
324 ND_PRINT((ndo
, "\n\t%s", tok2str(ahcp1_msg_str
, "Unknown-%u", type
)));
326 ND_PRINT((ndo
, ", MBZ %u", mbz
));
327 ND_PRINT((ndo
, ", Length %u", body_len
));
329 if (cp
+ body_len
> ep
)
333 if (ndo
->ndo_vflag
>= 2)
334 ahcp1_options_print(ndo
, cp
, cp
+ body_len
); /* not ep (ignore extra data) */
336 ND_TCHECK2(*cp
, body_len
);
340 ND_PRINT((ndo
, "%s", istr
));
341 ND_TCHECK2(*cp
, ep
- cp
);
344 ND_PRINT((ndo
, "%s", tstr
));
348 ahcp_print(netdissect_options
*ndo
, const u_char
*cp
, const u_int len
)
350 const u_char
*ep
= cp
+ len
;
353 ND_PRINT((ndo
, "AHCP"));
358 if (*cp
!= AHCP_MAGIC_NUMBER
)
366 case AHCP_VERSION_1
: {
367 ND_PRINT((ndo
, " Version 1"));
368 if (len
< AHCP1_HEADER_FIX_LEN
)
370 if (!ndo
->ndo_vflag
) {
371 ND_TCHECK2(*cp
, AHCP1_HEADER_FIX_LEN
- 2);
372 cp
+= AHCP1_HEADER_FIX_LEN
- 2;
376 ND_PRINT((ndo
, "\n\tHopcount %u", *cp
));
378 /* Original Hopcount */
380 ND_PRINT((ndo
, ", Original Hopcount %u", *cp
));
384 ND_PRINT((ndo
, ", Nonce 0x%08x", EXTRACT_32BITS(cp
)));
388 ND_PRINT((ndo
, ", Source Id %s", linkaddr_string(ndo
, cp
, 0, 8)));
392 ND_PRINT((ndo
, ", Destination Id %s", linkaddr_string(ndo
, cp
, 0, 8)));
396 ahcp1_body_print(ndo
, cp
, ep
);
400 ND_PRINT((ndo
, " Version %u (unknown)", version
));
406 ND_PRINT((ndo
, "%s", istr
));
407 ND_TCHECK2(*cp
, ep
- cp
);
410 ND_PRINT((ndo
, "%s", tstr
));