2 * Copyright (c) 2016 Antonin Décimo, Jean-Raphaël Gaglione
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the project nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <netdissect-stdinc.h>
38 #include "netdissect.h"
39 #include "addrtoname.h"
43 hncp_print_rec(netdissect_options
*ndo
,
44 const u_char
*cp
, u_int length
, int indent
);
47 hncp_print(netdissect_options
*ndo
,
48 const u_char
*cp
, u_int length
)
50 ND_PRINT((ndo
, "hncp (%d)", length
));
51 hncp_print_rec(ndo
, cp
, length
, 1);
55 #define DNCP_REQUEST_NETWORK_STATE 1
56 #define DNCP_REQUEST_NODE_STATE 2
57 #define DNCP_NODE_ENDPOINT 3
58 #define DNCP_NETWORK_STATE 4
59 #define DNCP_NODE_STATE 5
61 #define DNCP_KEEP_ALIVE_INTERVAL 9
62 #define DNCP_TRUST_VERDICT 10
65 #define HNCP_HNCP_VERSION 32
66 #define HNCP_EXTERNAL_CONNECTION 33
67 #define HNCP_DELEGATED_PREFIX 34
68 #define HNCP_PREFIX_POLICY 43
69 #define HNCP_DHCPV4_DATA 37
70 #define HNCP_DHCPV6_DATA 38
71 #define HNCP_ASSIGNED_PREFIX 35
72 #define HNCP_NODE_ADDRESS 36
73 #define HNCP_DNS_DELEGATED_ZONE 39
74 #define HNCP_DOMAIN_NAME 40
75 #define HNCP_NODE_NAME 41
76 #define HNCP_MANAGED_PSK 42
78 /* See type_mask in hncp_print_rec below */
79 #define RANGE_DNCP_RESERVED 0x10000
80 #define RANGE_HNCP_UNASSIGNED 0x10001
81 #define RANGE_DNCP_PRIVATE_USE 0x10002
82 #define RANGE_DNCP_FUTURE_USE 0x10003
84 static const struct tok type_values
[] = {
85 { DNCP_REQUEST_NETWORK_STATE
, "Request network state" },
86 { DNCP_REQUEST_NODE_STATE
, "Request node state" },
87 { DNCP_NODE_ENDPOINT
, "Node endpoint" },
88 { DNCP_NETWORK_STATE
, "Network state" },
89 { DNCP_NODE_STATE
, "Node state" },
90 { DNCP_PEER
, "Peer" },
91 { DNCP_KEEP_ALIVE_INTERVAL
, "Keep-alive interval" },
92 { DNCP_TRUST_VERDICT
, "Trust-Verdict" },
94 { HNCP_HNCP_VERSION
, "HNCP-Version" },
95 { HNCP_EXTERNAL_CONNECTION
, "External-Connection" },
96 { HNCP_DELEGATED_PREFIX
, "Delegated-Prefix" },
97 { HNCP_PREFIX_POLICY
, "Prefix-Policy" },
98 { HNCP_DHCPV4_DATA
, "DHCPv4-Data" },
99 { HNCP_DHCPV6_DATA
, "DHCPv6-Data" },
100 { HNCP_ASSIGNED_PREFIX
, "Assigned-Prefix" },
101 { HNCP_NODE_ADDRESS
, "Node-Address" },
102 { HNCP_DNS_DELEGATED_ZONE
, "DNS-Delegated-Zone" },
103 { HNCP_DOMAIN_NAME
, "Domain-Name" },
104 { HNCP_NODE_NAME
, "Node-Name" },
105 { HNCP_MANAGED_PSK
, "Managed-PSK" },
107 { RANGE_DNCP_RESERVED
, "Reserved" },
108 { RANGE_HNCP_UNASSIGNED
, "Unassigned" },
109 { RANGE_DNCP_PRIVATE_USE
, "Private use" },
110 { RANGE_DNCP_FUTURE_USE
, "Future use" },
115 #define DH4OPT_DNS_SERVERS 6 /* RFC2132 */
116 #define DH4OPT_NTP_SERVERS 42 /* RFC2132 */
117 #define DH4OPT_DOMAIN_SEARCH 119 /* RFC3397 */
119 static const struct tok dh4opt_str
[] = {
120 { DH4OPT_DNS_SERVERS
, "DNS-server" },
121 { DH4OPT_NTP_SERVERS
, "NTP-server"},
122 { DH4OPT_DOMAIN_SEARCH
, "DNS-search" },
126 #define DH6OPT_DNS_SERVERS 23 /* RFC3646 */
127 #define DH6OPT_DOMAIN_LIST 24 /* RFC3646 */
128 #define DH6OPT_SNTP_SERVERS 31 /* RFC4075 */
130 static const struct tok dh6opt_str
[] = {
131 { DH6OPT_DNS_SERVERS
, "DNS-server" },
132 { DH6OPT_DOMAIN_LIST
, "DNS-search-list" },
133 { DH6OPT_SNTP_SERVERS
, "SNTP-servers" },
138 format_nid(const u_char
*data
)
140 static char buf
[4][11+5];
143 snprintf(buf
[i
], 16, "%02x:%02x:%02x:%02x",
144 data
[0], data
[1], data
[2], data
[3]);
149 format_256(const u_char
*data
)
151 static char buf
[4][64+5];
154 snprintf(buf
[i
], 28, "%016lx%016lx%016lx%016lx",
155 EXTRACT_64BITS(data
),
156 EXTRACT_64BITS(data
+ 8),
157 EXTRACT_64BITS(data
+ 16),
158 EXTRACT_64BITS(data
+ 24)
164 format_interval(const uint16_t n
)
166 static char buf
[4][sizeof("000.00s")];
170 return "0.0s (bogus)";
171 snprintf(buf
[i
], sizeof(buf
[i
]), "%u.%02us", n
/ 100, n
% 100);
176 format_ip6addr(netdissect_options
*ndo
, const u_char
*cp
)
178 if (EXTRACT_64BITS(cp
) == 0x0 && EXTRACT_32BITS(cp
+8) == 0xffff)
179 return ipaddr_string(ndo
, cp
+ 12);
181 return ip6addr_string(ndo
, cp
);
185 print_prefix(netdissect_options
*ndo
, const u_char
*prefix
, u_int max_length
)
188 static char buf
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::/128")];
189 static u_char prefix_v4
[5];
190 if (prefix
[0] >= 96 && max_length
>= 13 && EXTRACT_64BITS(prefix
+1) == 0x0 && EXTRACT_32BITS(prefix
+9) == 0xffff) {
191 prefix_v4
[0] = prefix
[0]-96;
192 prefix_v4
[1] = prefix
[13];
193 prefix_v4
[2] = prefix
[14];
194 prefix_v4
[3] = prefix
[15];
195 prefix_v4
[4] = prefix
[16];
196 plenbytes
= 12 + decode_prefix4(ndo
, (const u_char
*)prefix_v4
, max_length
-12, buf
, 45);
198 plenbytes
= decode_prefix6(ndo
, prefix
, max_length
, buf
, 45);
201 safeputs(ndo
, (const u_char
*)buf
, 45);
206 print_dns_label(netdissect_options
*ndo
,
207 const u_char
*cp
, u_int max_length
, int print
)
210 while (length
< max_length
) {
211 u_int lab_length
= cp
[length
++];
214 if (length
> 1 && print
)
215 safeputchar(ndo
, '.');
216 if (length
+lab_length
> max_length
) {
218 safeputs(ndo
, cp
+length
, max_length
-length
);
222 safeputs(ndo
, cp
+length
, lab_length
);
223 length
+= lab_length
;
226 ND_PRINT((ndo
, "[|DNS]"));
231 dhcpv4_print(netdissect_options
*ndo
,
232 const u_char
*cp
, u_int length
, int indent
)
235 const u_char
*tlv
, *value
;
236 uint8_t type
, optlen
;
241 type
= (uint8_t)tlv
[0];
242 optlen
= (uint8_t)tlv
[1];
245 ND_PRINT((ndo
, "\n"));
246 for (t
= indent
; t
> 0; t
--)
247 ND_PRINT((ndo
, "\t"));
249 ND_PRINT((ndo
, "%s", tok2str(dh4opt_str
, "Unknown", type
)));
250 ND_PRINT((ndo
," (%u)", optlen
+ 2 ));
253 case DH4OPT_DNS_SERVERS
:
254 case DH4OPT_NTP_SERVERS
: {
255 if (optlen
< 4 || optlen
% 4 != 0) {
258 for (t
= 0; t
< optlen
; t
+= 4)
259 ND_PRINT((ndo
, " %s", ipaddr_string(ndo
, value
+ t
)));
262 case DH4OPT_DOMAIN_SEARCH
: {
263 const u_char
*tp
= value
;
264 while (tp
< value
+ optlen
) {
265 ND_PRINT((ndo
, " "));
266 if ((tp
= ns_nprint(ndo
, tp
, value
+ optlen
)) == NULL
)
279 dhcpv6_print(netdissect_options
*ndo
,
280 const u_char
*cp
, u_int length
, int indent
)
283 const u_char
*tlv
, *value
;
284 uint16_t type
, optlen
;
289 type
= EXTRACT_16BITS(tlv
);
290 optlen
= EXTRACT_16BITS(tlv
+ 2);
293 ND_PRINT((ndo
, "\n"));
294 for (t
= indent
; t
> 0; t
--)
295 ND_PRINT((ndo
, "\t"));
297 ND_PRINT((ndo
, "%s", tok2str(dh6opt_str
, "Unknown", type
)));
298 ND_PRINT((ndo
," (%u)", optlen
+ 4 ));
301 case DH6OPT_DNS_SERVERS
:
302 case DH6OPT_SNTP_SERVERS
: {
303 if (optlen
% 16 != 0) {
304 ND_PRINT((ndo
, " %s", istr
));
307 for (t
= 0; t
< optlen
; t
+= 16)
308 ND_PRINT((ndo
, " %s", ip6addr_string(ndo
, value
+ t
)));
311 case DH6OPT_DOMAIN_LIST
: {
312 const u_char
*tp
= value
;
313 while (tp
< value
+ optlen
) {
314 ND_PRINT((ndo
, " "));
315 if ((tp
= ns_nprint(ndo
, tp
, value
+ optlen
)) == NULL
)
327 /* Determine in-line mode */
329 is_in_line(netdissect_options
*ndo
, int indent
)
331 return indent
- 1 >= ndo
->ndo_vflag
&& ndo
->ndo_vflag
< 3;
335 print_type_in_line(netdissect_options
*ndo
,
336 uint32_t type
, int count
, int indent
, int *first_one
)
343 ND_PRINT((ndo
, "\n"));
344 for (t
= indent
; t
> 0; t
--)
345 ND_PRINT((ndo
, "\t"));
347 ND_PRINT((ndo
, " "));
350 ND_PRINT((ndo
, ", "));
352 ND_PRINT((ndo
, "%s", tok2str(type_values
, "Easter Egg", type
)));
354 ND_PRINT((ndo
, " (x%d)", count
));
359 hncp_print_rec(netdissect_options
*ndo
,
360 const u_char
*cp
, u_int length
, int indent
)
362 const int in_line
= is_in_line(ndo
, indent
);
367 uint32_t last_type_mask
= -1;
368 int last_type_count
= -1;
370 const u_char
*tlv
, *value
;
371 uint16_t type
, bodylen
;
379 ND_PRINT((ndo
, "\n"));
380 for (t
= indent
; t
> 0; t
--)
381 ND_PRINT((ndo
, "\t"));
388 type
= EXTRACT_16BITS(tlv
);
389 bodylen
= EXTRACT_16BITS(tlv
+ 2);
391 ND_TCHECK2(*value
, bodylen
);
392 if (i
+ bodylen
+ 4 > length
)
396 (type
== 0) ? RANGE_DNCP_RESERVED
:
397 (44 <= type
&& type
<= 511) ? RANGE_HNCP_UNASSIGNED
:
398 (768 <= type
&& type
<= 1023) ? RANGE_DNCP_PRIVATE_USE
:
399 RANGE_DNCP_FUTURE_USE
;
400 if (type
== 6 || type
== 7)
401 type_mask
= RANGE_DNCP_FUTURE_USE
;
407 u_int key
= type_values
[t
++].v
;
418 if (last_type_mask
== type_mask
) {
421 print_type_in_line(ndo
, last_type_mask
, last_type_count
, indent
, &first_one
);
422 last_type_mask
= type_mask
;
429 ND_PRINT((ndo
,"%s", tok2str(type_values
, "Easter Egg (42)", type_mask
) ));
430 if (type_mask
> 0xffff)
431 ND_PRINT((ndo
,": type=%u", type
));
432 ND_PRINT((ndo
," (%u)", bodylen
+ 4 ));
436 case DNCP_REQUEST_NETWORK_STATE
: {
438 ND_PRINT((ndo
, " %s", istr
));
442 case DNCP_REQUEST_NODE_STATE
: {
443 const char *node_identifier
;
446 node_identifier
= format_nid(value
);
447 ND_PRINT((ndo
, " NID: %s", node_identifier
));
451 case DNCP_NODE_ENDPOINT
: {
452 const char *node_identifier
;
453 uint32_t endpoint_identifier
;
456 node_identifier
= format_nid(value
);
457 endpoint_identifier
= EXTRACT_32BITS(value
+ 4);
458 ND_PRINT((ndo
, " NID: %s EPID: %08x",
465 case DNCP_NETWORK_STATE
: {
469 hash
= EXTRACT_64BITS(value
);
470 ND_PRINT((ndo
, " hash: %016lx", hash
));
474 case DNCP_NODE_STATE
: {
475 const char *node_identifier
, *time
;
476 uint32_t sequence_number
;
480 node_identifier
= format_nid(value
);
481 sequence_number
= EXTRACT_32BITS(value
+ 4);
482 time
= format_interval(EXTRACT_32BITS(value
+ 8));
483 hash
= EXTRACT_64BITS(value
+ 12);
484 ND_PRINT((ndo
, " NID: %s seqno: %u %s hash: %016lx",
490 hncp_print_rec(ndo
, value
+20, bodylen
-20, indent
+1);
495 const char *peer_node_identifier
;
496 uint32_t peer_endpoint_identifier
, endpoint_identifier
;
499 peer_node_identifier
= format_nid(value
);
500 peer_endpoint_identifier
= EXTRACT_32BITS(value
+ 4);
501 endpoint_identifier
= EXTRACT_32BITS(value
+ 8);
502 ND_PRINT((ndo
, " Peer-NID: %s Peer-EPID: %08x Local-EPID: %08x",
503 peer_node_identifier
,
504 peer_endpoint_identifier
,
510 case DNCP_KEEP_ALIVE_INTERVAL
: {
511 uint32_t endpoint_identifier
;
512 const char *interval
;
515 endpoint_identifier
= EXTRACT_32BITS(value
);
516 interval
= format_interval(EXTRACT_32BITS(value
+ 4));
517 ND_PRINT((ndo
, " EPID: %08x Interval: %s",
524 case DNCP_TRUST_VERDICT
: {
527 ND_PRINT((ndo
, " Verdict: %u Fingerprint: %s Common Name: ",
529 format_256(value
+ 4)));
530 safeputs(ndo
, value
+ 36, bodylen
- 36);
534 case HNCP_HNCP_VERSION
: {
535 uint16_t capabilities
;
539 capabilities
= EXTRACT_16BITS(value
+ 2);
540 M
= (uint8_t)((capabilities
>> 12) & 0xf);
541 P
= (uint8_t)((capabilities
>> 8) & 0xf);
542 H
= (uint8_t)((capabilities
>> 4) & 0xf);
543 L
= (uint8_t)(capabilities
& 0xf);
544 ND_PRINT((ndo
, " M: %u P: %u H: %u L: %u User-agent: ",
547 safeputs(ndo
, value
+ 4, bodylen
- 4);
551 case HNCP_EXTERNAL_CONNECTION
: {
553 hncp_print_rec(ndo
, value
, bodylen
, indent
+1);
557 case HNCP_DELEGATED_PREFIX
: {
559 if (bodylen
< 9 || bodylen
< 9 + (value
[8] + 7) / 8)
561 ND_PRINT((ndo
, " VLSO: %s PLSO: %s Prefix: ",
562 format_interval(EXTRACT_32BITS(value
)),
563 format_interval(EXTRACT_32BITS(value
+ 4))
565 if ((l
= print_prefix(ndo
, value
+ 8, bodylen
- 8)) < 0)
570 hncp_print_rec(ndo
, value
+ l
, bodylen
- l
, indent
+1);
574 case HNCP_PREFIX_POLICY
: {
579 ND_PRINT((ndo
, " type: "));
583 ND_PRINT((ndo
, "Internet connectivity"));
584 } else if (policy
>= 1 && policy
<= 128) {
585 ND_PRINT((ndo
, "Dest-Prefix: "));
586 print_prefix(ndo
, value
, bodylen
);
587 } else if (policy
== 129) {
588 ND_PRINT((ndo
, "DNS domain: "));
589 print_dns_label(ndo
, value
+1, bodylen
-1, 1);
590 } else if (policy
== 130) {
591 ND_PRINT((ndo
, "Opaque UTF-8: "));
592 safeputs(ndo
, value
+ 1, bodylen
- 1);
593 } else if (policy
== 131) {
596 ND_PRINT((ndo
, "Restrictive assignment"));
597 } else if (policy
>= 132) {
598 ND_PRINT((ndo
, "Unknown (%u)", policy
)); /* Reserved for future additions */
603 case HNCP_DHCPV4_DATA
: {
606 if (dhcpv4_print(ndo
, value
, bodylen
, indent
+1) != 0)
611 case HNCP_DHCPV6_DATA
: {
614 if (dhcpv6_print(ndo
, value
, bodylen
, indent
+1) != 0)
619 case HNCP_ASSIGNED_PREFIX
: {
622 if (bodylen
< 6 || bodylen
< 6 + (value
[5] + 7) / 8)
624 prty
= (uint8_t)(value
[4] & 0xf);
625 ND_PRINT((ndo
, " EPID: %08x Prty: %u",
626 EXTRACT_32BITS(value
),
629 ND_PRINT((ndo
, " Prefix: "));
630 if ((l
= print_prefix(ndo
, value
+ 5, bodylen
- 5)) < 0)
636 hncp_print_rec(ndo
, value
+ l
, bodylen
- l
, indent
+1);
640 case HNCP_NODE_ADDRESS
: {
641 uint32_t endpoint_identifier
;
642 const char *ip_address
;
645 endpoint_identifier
= EXTRACT_32BITS(value
);
646 ip_address
= format_ip6addr(ndo
, value
+ 4);
647 ND_PRINT((ndo
, " EPID: %08x IP Address: %s",
652 hncp_print_rec(ndo
, value
+ 20, bodylen
- 20, indent
+1);
656 case HNCP_DNS_DELEGATED_ZONE
: {
657 const char *ip_address
;
661 ip_address
= format_ip6addr(ndo
, value
);
662 ND_PRINT((ndo
, " IP-Address: %s %c%c%c ",
664 (value
[16] & 4) ? 'l' : '-',
665 (value
[16] & 2) ? 'b' : '-',
666 (value
[16] & 1) ? 's' : '-'
668 len
= print_dns_label(ndo
, value
+17, bodylen
-17, 1);
674 hncp_print_rec(ndo
, value
+len
, bodylen
-len
, indent
+1);
678 case HNCP_DOMAIN_NAME
: {
681 ND_PRINT((ndo
, " Domain: "));
682 print_dns_label(ndo
, value
, bodylen
, 1);
686 case HNCP_NODE_NAME
: {
691 if (bodylen
< 17 + l
)
693 ND_PRINT((ndo
, " IP-Address: %s Name: ",
694 format_ip6addr(ndo
, value
)
697 safeputchar(ndo
, '"');
698 safeputs(ndo
, value
+ 17, l
);
699 safeputchar(ndo
, '"');
701 ND_PRINT((ndo
, "%s", istr
));
706 hncp_print_rec(ndo
, value
+ l
, bodylen
- l
, indent
+1);
710 case HNCP_MANAGED_PSK
: {
713 ND_PRINT((ndo
, " PSK: %s", format_256(value
)));
714 hncp_print_rec(ndo
, value
+ 32, bodylen
- 32, indent
+1);
718 case RANGE_DNCP_RESERVED
:
719 case RANGE_HNCP_UNASSIGNED
:
720 case RANGE_DNCP_PRIVATE_USE
:
721 case RANGE_DNCP_FUTURE_USE
:
727 i
+= 4 + bodylen
+ (-bodylen
& 3);
729 print_type_in_line(ndo
, last_type_mask
, last_type_count
, indent
, &first_one
);
734 ND_PRINT((ndo
, "%s", "[|hncp]"));
738 ND_PRINT((ndo
, "%s", istr
));