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
29 /* \summary: Home Networking Control Protocol (HNCP) printer */
35 #include "netdissect-stdinc.h"
40 #include "netdissect.h"
41 #include "addrtoname.h"
45 hncp_print_rec(netdissect_options
*ndo
,
46 const u_char
*cp
, u_int length
, int indent
);
49 hncp_print(netdissect_options
*ndo
,
50 const u_char
*cp
, u_int length
)
52 ndo
->ndo_protocol
= "hncp";
53 ND_PRINT("hncp (%u)", length
);
54 hncp_print_rec(ndo
, cp
, length
, 1);
58 #define DNCP_REQUEST_NETWORK_STATE 1
59 #define DNCP_REQUEST_NODE_STATE 2
60 #define DNCP_NODE_ENDPOINT 3
61 #define DNCP_NETWORK_STATE 4
62 #define DNCP_NODE_STATE 5
64 #define DNCP_KEEP_ALIVE_INTERVAL 9
65 #define DNCP_TRUST_VERDICT 10
68 #define HNCP_HNCP_VERSION 32
69 #define HNCP_EXTERNAL_CONNECTION 33
70 #define HNCP_DELEGATED_PREFIX 34
71 #define HNCP_PREFIX_POLICY 43
72 #define HNCP_DHCPV4_DATA 37 /* This is correct, see RFC 7788 Errata ID 5113. */
73 #define HNCP_DHCPV6_DATA 38 /* idem */
74 #define HNCP_ASSIGNED_PREFIX 35
75 #define HNCP_NODE_ADDRESS 36
76 #define HNCP_DNS_DELEGATED_ZONE 39
77 #define HNCP_DOMAIN_NAME 40
78 #define HNCP_NODE_NAME 41
79 #define HNCP_MANAGED_PSK 42
81 /* See type_mask in hncp_print_rec below */
82 #define RANGE_DNCP_RESERVED 0x10000
83 #define RANGE_HNCP_UNASSIGNED 0x10001
84 #define RANGE_DNCP_PRIVATE_USE 0x10002
85 #define RANGE_DNCP_FUTURE_USE 0x10003
87 static const struct tok type_values
[] = {
88 { DNCP_REQUEST_NETWORK_STATE
, "Request network state" },
89 { DNCP_REQUEST_NODE_STATE
, "Request node state" },
90 { DNCP_NODE_ENDPOINT
, "Node endpoint" },
91 { DNCP_NETWORK_STATE
, "Network state" },
92 { DNCP_NODE_STATE
, "Node state" },
93 { DNCP_PEER
, "Peer" },
94 { DNCP_KEEP_ALIVE_INTERVAL
, "Keep-alive interval" },
95 { DNCP_TRUST_VERDICT
, "Trust-Verdict" },
97 { HNCP_HNCP_VERSION
, "HNCP-Version" },
98 { HNCP_EXTERNAL_CONNECTION
, "External-Connection" },
99 { HNCP_DELEGATED_PREFIX
, "Delegated-Prefix" },
100 { HNCP_PREFIX_POLICY
, "Prefix-Policy" },
101 { HNCP_DHCPV4_DATA
, "DHCPv4-Data" },
102 { HNCP_DHCPV6_DATA
, "DHCPv6-Data" },
103 { HNCP_ASSIGNED_PREFIX
, "Assigned-Prefix" },
104 { HNCP_NODE_ADDRESS
, "Node-Address" },
105 { HNCP_DNS_DELEGATED_ZONE
, "DNS-Delegated-Zone" },
106 { HNCP_DOMAIN_NAME
, "Domain-Name" },
107 { HNCP_NODE_NAME
, "Node-Name" },
108 { HNCP_MANAGED_PSK
, "Managed-PSK" },
110 { RANGE_DNCP_RESERVED
, "Reserved" },
111 { RANGE_HNCP_UNASSIGNED
, "Unassigned" },
112 { RANGE_DNCP_PRIVATE_USE
, "Private use" },
113 { RANGE_DNCP_FUTURE_USE
, "Future use" },
118 #define DH4OPT_DNS_SERVERS 6 /* RFC2132 */
119 #define DH4OPT_NTP_SERVERS 42 /* RFC2132 */
120 #define DH4OPT_DOMAIN_SEARCH 119 /* RFC3397 */
122 static const struct tok dh4opt_str
[] = {
123 { DH4OPT_DNS_SERVERS
, "DNS-server" },
124 { DH4OPT_NTP_SERVERS
, "NTP-server"},
125 { DH4OPT_DOMAIN_SEARCH
, "DNS-search" },
129 #define DH6OPT_DNS_SERVERS 23 /* RFC3646 */
130 #define DH6OPT_DOMAIN_LIST 24 /* RFC3646 */
131 #define DH6OPT_SNTP_SERVERS 31 /* RFC4075 */
133 static const struct tok dh6opt_str
[] = {
134 { DH6OPT_DNS_SERVERS
, "DNS-server" },
135 { DH6OPT_DOMAIN_LIST
, "DNS-search-list" },
136 { DH6OPT_SNTP_SERVERS
, "SNTP-servers" },
141 * For IPv4-mapped IPv6 addresses, length of the prefix that precedes
142 * the 4 bytes of IPv4 address at the end of the IPv6 address.
144 #define IPV4_MAPPED_HEADING_LEN 12
147 * Is an IPv6 address an IPv4-mapped address?
150 is_ipv4_mapped_address(const u_char
*addr
)
152 /* The value of the prefix */
153 static const u_char ipv4_mapped_heading
[IPV4_MAPPED_HEADING_LEN
] =
154 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
156 return memcmp(addr
, ipv4_mapped_heading
, IPV4_MAPPED_HEADING_LEN
) == 0;
160 format_nid(netdissect_options
*ndo
, const u_char
*data
)
162 static char buf
[4][sizeof("01:01:01:01")];
165 snprintf(buf
[i
], sizeof(buf
[i
]), "%02x:%02x:%02x:%02x",
166 GET_U_1(data
), GET_U_1(data
+ 1), GET_U_1(data
+ 2),
172 format_256(netdissect_options
*ndo
, const u_char
*data
)
174 static char buf
[4][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")];
177 snprintf(buf
[i
], sizeof(buf
[i
]), "%016" PRIx64
"%016" PRIx64
"%016" PRIx64
"%016" PRIx64
,
179 GET_BE_U_8(data
+ 8),
180 GET_BE_U_8(data
+ 16),
181 GET_BE_U_8(data
+ 24)
187 format_interval(const uint32_t n
)
189 static char buf
[4][sizeof("0000000.000s")];
192 snprintf(buf
[i
], sizeof(buf
[i
]), "%u.%03us", n
/ 1000, n
% 1000);
197 format_ip6addr(netdissect_options
*ndo
, const u_char
*cp
)
199 if (is_ipv4_mapped_address(cp
))
200 return GET_IPADDR_STRING(cp
+ IPV4_MAPPED_HEADING_LEN
);
202 return GET_IP6ADDR_STRING(cp
);
206 print_prefix(netdissect_options
*ndo
, const u_char
*prefix
, u_int max_length
)
209 char buf
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::/128")];
211 if (GET_U_1(prefix
) >= 96 && max_length
>= IPV4_MAPPED_HEADING_LEN
+ 1 &&
212 is_ipv4_mapped_address(prefix
+ 1)) {
216 plen
= GET_U_1(prefix
) - 96;
221 memset(&addr
, 0, sizeof(addr
));
222 plenbytes
= (plen
+ 7) / 8;
223 if (max_length
< (u_int
)plenbytes
+ IPV4_MAPPED_HEADING_LEN
)
225 memcpy(&addr
, prefix
+ IPV4_MAPPED_HEADING_LEN
+ 1, plenbytes
);
227 ((u_char
*)&addr
)[plenbytes
- 1] &=
228 ((0xff00 >> (plen
% 8)) & 0xff);
230 snprintf(buf
, sizeof(buf
), "%s/%u", ipaddr_string(ndo
, (const u_char
*)&addr
), plen
);
231 plenbytes
+= 1 + IPV4_MAPPED_HEADING_LEN
;
233 plenbytes
= decode_prefix6(ndo
, prefix
, max_length
, buf
, sizeof(buf
));
243 print_dns_label(netdissect_options
*ndo
,
244 const u_char
*cp
, u_int max_length
, int print
)
247 while (length
< max_length
) {
248 u_int lab_length
= GET_U_1(cp
+ length
);
252 if (length
> 1 && print
)
254 if (length
+lab_length
> max_length
) {
256 (void)nd_printzp(ndo
, cp
+length
, max_length
-length
, NULL
);
260 (void)nd_printzp(ndo
, cp
+length
, lab_length
, NULL
);
261 length
+= lab_length
;
269 dhcpv4_print(netdissect_options
*ndo
,
270 const u_char
*cp
, u_int length
, int indent
)
273 const uint8_t *tlv
, *value
;
274 uint8_t type
, optlen
;
282 optlen
= GET_U_1(tlv
+ 1);
286 for (t
= indent
; t
> 0; t
--)
289 ND_PRINT("%s", tok2str(dh4opt_str
, "Unknown", type
));
290 ND_PRINT(" (%u)", optlen
+ 2 );
291 if (i
+ 2 + optlen
> length
)
295 case DH4OPT_DNS_SERVERS
:
296 case DH4OPT_NTP_SERVERS
: {
297 if (optlen
< 4 || optlen
% 4 != 0) {
300 for (t
= 0; t
< optlen
; t
+= 4)
301 ND_PRINT(" %s", GET_IPADDR_STRING(value
+ t
));
304 case DH4OPT_DOMAIN_SEARCH
: {
305 const u_char
*tp
= value
;
306 while (tp
< value
+ optlen
) {
308 if ((tp
= fqdn_print(ndo
, tp
, value
+ optlen
)) == NULL
)
321 dhcpv6_print(netdissect_options
*ndo
,
322 const u_char
*cp
, u_int length
, int indent
)
325 const u_char
*tlv
, *value
;
326 uint16_t type
, optlen
;
333 type
= GET_BE_U_2(tlv
);
334 optlen
= GET_BE_U_2(tlv
+ 2);
338 for (t
= indent
; t
> 0; t
--)
341 ND_PRINT("%s", tok2str(dh6opt_str
, "Unknown", type
));
342 ND_PRINT(" (%u)", optlen
+ 4 );
343 if (i
+ 4 + optlen
> length
)
347 case DH6OPT_DNS_SERVERS
:
348 case DH6OPT_SNTP_SERVERS
: {
349 if (optlen
% 16 != 0) {
350 nd_print_invalid(ndo
);
353 for (t
= 0; t
< optlen
; t
+= 16)
354 ND_PRINT(" %s", GET_IP6ADDR_STRING(value
+ t
));
357 case DH6OPT_DOMAIN_LIST
: {
358 const u_char
*tp
= value
;
359 while (tp
< value
+ optlen
) {
361 if ((tp
= fqdn_print(ndo
, tp
, value
+ optlen
)) == NULL
)
373 /* Determine in-line mode */
375 is_in_line(netdissect_options
*ndo
, int indent
)
377 return indent
- 1 >= ndo
->ndo_vflag
&& ndo
->ndo_vflag
< 3;
381 print_type_in_line(netdissect_options
*ndo
,
382 uint32_t type
, int count
, int indent
, int *first_one
)
390 for (t
= indent
; t
> 0; t
--)
398 ND_PRINT("%s", tok2str(type_values
, "Easter Egg", type
));
400 ND_PRINT(" (x%d)", count
);
405 hncp_print_rec(netdissect_options
*ndo
,
406 const u_char
*cp
, u_int length
, int indent
)
408 const int in_line
= is_in_line(ndo
, indent
);
413 uint32_t last_type_mask
= 0xffffffffU
;
414 int last_type_count
= -1;
416 const uint8_t *tlv
, *value
;
417 uint16_t type
, bodylen
;
426 for (t
= indent
; t
> 0; t
--)
434 type
= GET_BE_U_2(tlv
);
435 bodylen
= GET_BE_U_2(tlv
+ 2);
437 ND_TCHECK_LEN(value
, bodylen
);
438 if (i
+ bodylen
+ 4 > length
)
442 (type
== 0) ? RANGE_DNCP_RESERVED
:
443 (44 <= type
&& type
<= 511) ? RANGE_HNCP_UNASSIGNED
:
444 (768 <= type
&& type
<= 1023) ? RANGE_DNCP_PRIVATE_USE
:
445 RANGE_DNCP_FUTURE_USE
;
446 if (type
== 6 || type
== 7)
447 type_mask
= RANGE_DNCP_FUTURE_USE
;
453 u_int key
= type_values
[t
++].v
;
464 if (last_type_mask
== type_mask
) {
467 print_type_in_line(ndo
, last_type_mask
, last_type_count
, indent
, &first_one
);
468 last_type_mask
= type_mask
;
475 ND_PRINT("%s", tok2str(type_values
, "Easter Egg (42)", type_mask
) );
476 if (type_mask
> 0xffff)
477 ND_PRINT(": type=%u", type
);
478 ND_PRINT(" (%u)", bodylen
+ 4 );
482 case DNCP_REQUEST_NETWORK_STATE
: {
484 nd_print_invalid(ndo
);
488 case DNCP_REQUEST_NODE_STATE
: {
489 const char *node_identifier
;
491 nd_print_invalid(ndo
);
494 node_identifier
= format_nid(ndo
, value
);
495 ND_PRINT(" NID: %s", node_identifier
);
499 case DNCP_NODE_ENDPOINT
: {
500 const char *node_identifier
;
501 uint32_t endpoint_identifier
;
503 nd_print_invalid(ndo
);
506 node_identifier
= format_nid(ndo
, value
);
507 endpoint_identifier
= GET_BE_U_4(value
+ 4);
508 ND_PRINT(" NID: %s EPID: %08x",
515 case DNCP_NETWORK_STATE
: {
518 nd_print_invalid(ndo
);
521 hash
= GET_BE_U_8(value
);
522 ND_PRINT(" hash: %016" PRIx64
, hash
);
526 case DNCP_NODE_STATE
: {
527 const char *node_identifier
, *interval
;
528 uint32_t sequence_number
;
531 nd_print_invalid(ndo
);
534 node_identifier
= format_nid(ndo
, value
);
535 sequence_number
= GET_BE_U_4(value
+ 4);
536 interval
= format_interval(GET_BE_U_4(value
+ 8));
537 hash
= GET_BE_U_8(value
+ 12);
538 ND_PRINT(" NID: %s seqno: %u %s hash: %016" PRIx64
,
544 hncp_print_rec(ndo
, value
+20, bodylen
-20, indent
+1);
549 const char *peer_node_identifier
;
550 uint32_t peer_endpoint_identifier
, endpoint_identifier
;
552 nd_print_invalid(ndo
);
555 peer_node_identifier
= format_nid(ndo
, value
);
556 peer_endpoint_identifier
= GET_BE_U_4(value
+ 4);
557 endpoint_identifier
= GET_BE_U_4(value
+ 8);
558 ND_PRINT(" Peer-NID: %s Peer-EPID: %08x Local-EPID: %08x",
559 peer_node_identifier
,
560 peer_endpoint_identifier
,
566 case DNCP_KEEP_ALIVE_INTERVAL
: {
567 uint32_t endpoint_identifier
;
568 const char *interval
;
570 nd_print_invalid(ndo
);
573 endpoint_identifier
= GET_BE_U_4(value
);
574 interval
= format_interval(GET_BE_U_4(value
+ 4));
575 ND_PRINT(" EPID: %08x Interval: %s",
582 case DNCP_TRUST_VERDICT
: {
584 nd_print_invalid(ndo
);
587 ND_PRINT(" Verdict: %u Fingerprint: %s Common Name: ",
589 format_256(ndo
, value
+ 4));
590 (void)nd_printzp(ndo
, value
+ 36, bodylen
- 36, NULL
);
594 case HNCP_HNCP_VERSION
: {
595 uint16_t capabilities
;
598 nd_print_invalid(ndo
);
601 capabilities
= GET_BE_U_2(value
+ 2);
602 M
= (uint8_t)((capabilities
>> 12) & 0xf);
603 P
= (uint8_t)((capabilities
>> 8) & 0xf);
604 H
= (uint8_t)((capabilities
>> 4) & 0xf);
605 L
= (uint8_t)(capabilities
& 0xf);
606 ND_PRINT(" M: %u P: %u H: %u L: %u User-agent: ",
609 (void)nd_printzp(ndo
, value
+ 4, bodylen
- 4, NULL
);
613 case HNCP_EXTERNAL_CONNECTION
: {
615 hncp_print_rec(ndo
, value
, bodylen
, indent
+1);
619 case HNCP_DELEGATED_PREFIX
: {
621 if (bodylen
< 9 || bodylen
< 9 + (GET_U_1(value
+ 8) + 7) / 8) {
622 nd_print_invalid(ndo
);
625 ND_PRINT(" VLSO: %s PLSO: %s Prefix: ",
626 format_interval(GET_BE_U_4(value
)),
627 format_interval(GET_BE_U_4(value
+ 4))
629 l
= print_prefix(ndo
, value
+ 8, bodylen
- 8);
631 ND_PRINT("(length is invalid)");
636 * We've already checked that we've captured the
637 * entire TLV, based on its length, so this will
638 * either be -1, meaning "the prefix length is
639 * greater than the longest possible address of
640 * that type" (i.e., > 32 for IPv4 or > 128 for
641 * IPv6", or -3, meaning "the prefix runs past
642 * the end of the TLV".
644 nd_print_invalid(ndo
);
650 hncp_print_rec(ndo
, value
+ l
, bodylen
- l
, indent
+1);
654 case HNCP_PREFIX_POLICY
: {
658 nd_print_invalid(ndo
);
661 policy
= GET_U_1(value
);
665 nd_print_invalid(ndo
);
668 ND_PRINT("Internet connectivity");
669 } else if (policy
>= 1 && policy
<= 128) {
670 ND_PRINT("Dest-Prefix: ");
671 l
= print_prefix(ndo
, value
, bodylen
);
673 ND_PRINT("(length is invalid)");
678 * We've already checked that we've captured the
679 * entire TLV, based on its length, so this will
680 * either be -1, meaning "the prefix length is
681 * greater than the longest possible address of
682 * that type" (i.e., > 32 for IPv4 or > 128 for
683 * IPv6", or -3, meaning "the prefix runs past
684 * the end of the TLV".
686 nd_print_invalid(ndo
);
689 } else if (policy
== 129) {
690 ND_PRINT("DNS domain: ");
691 print_dns_label(ndo
, value
+1, bodylen
-1, 1);
692 } else if (policy
== 130) {
693 ND_PRINT("Opaque UTF-8: ");
694 (void)nd_printzp(ndo
, value
+ 1, bodylen
- 1, NULL
);
695 } else if (policy
== 131) {
697 nd_print_invalid(ndo
);
700 ND_PRINT("Restrictive assignment");
701 } else if (policy
>= 132) {
702 ND_PRINT("Unknown (%u)", policy
); /* Reserved for future additions */
707 case HNCP_DHCPV4_DATA
: {
709 nd_print_invalid(ndo
);
712 if (dhcpv4_print(ndo
, value
, bodylen
, indent
+1) != 0)
717 case HNCP_DHCPV6_DATA
: {
719 nd_print_invalid(ndo
);
722 if (dhcpv6_print(ndo
, value
, bodylen
, indent
+1) != 0) {
723 nd_print_invalid(ndo
);
729 case HNCP_ASSIGNED_PREFIX
: {
732 if (bodylen
< 6 || bodylen
< 6 + (GET_U_1(value
+ 5) + 7) / 8) {
733 nd_print_invalid(ndo
);
736 prty
= GET_U_1(value
+ 4) & 0xf;
737 ND_PRINT(" EPID: %08x Prty: %u",
741 ND_PRINT(" Prefix: ");
742 if ((l
= print_prefix(ndo
, value
+ 5, bodylen
- 5)) < 0) {
743 nd_print_invalid(ndo
);
750 hncp_print_rec(ndo
, value
+ l
, bodylen
- l
, indent
+1);
754 case HNCP_NODE_ADDRESS
: {
755 uint32_t endpoint_identifier
;
756 const char *ip_address
;
758 nd_print_invalid(ndo
);
761 endpoint_identifier
= GET_BE_U_4(value
);
762 ip_address
= format_ip6addr(ndo
, value
+ 4);
763 ND_PRINT(" EPID: %08x IP Address: %s",
768 hncp_print_rec(ndo
, value
+ 20, bodylen
- 20, indent
+1);
772 case HNCP_DNS_DELEGATED_ZONE
: {
773 const char *ip_address
;
776 nd_print_invalid(ndo
);
779 ip_address
= format_ip6addr(ndo
, value
);
780 ND_PRINT(" IP-Address: %s %c%c%c ",
782 (GET_U_1(value
+ 16) & 4) ? 'l' : '-',
783 (GET_U_1(value
+ 16) & 2) ? 'b' : '-',
784 (GET_U_1(value
+ 16) & 1) ? 's' : '-'
786 len
= print_dns_label(ndo
, value
+17, bodylen
-17, 1);
788 nd_print_invalid(ndo
);
794 hncp_print_rec(ndo
, value
+len
, bodylen
-len
, indent
+1);
798 case HNCP_DOMAIN_NAME
: {
800 nd_print_invalid(ndo
);
803 ND_PRINT(" Domain: ");
804 print_dns_label(ndo
, value
, bodylen
, 1);
808 case HNCP_NODE_NAME
: {
811 nd_print_invalid(ndo
);
814 l
= GET_U_1(value
+ 16);
815 if (bodylen
< 17 + l
) {
816 nd_print_invalid(ndo
);
819 ND_PRINT(" IP-Address: %s Name: ",
820 format_ip6addr(ndo
, value
)
824 (void)nd_printzp(ndo
, value
+ 17, l
, NULL
);
827 nd_print_invalid(ndo
);
832 hncp_print_rec(ndo
, value
+ l
, bodylen
- l
, indent
+1);
836 case HNCP_MANAGED_PSK
: {
838 nd_print_invalid(ndo
);
841 ND_PRINT(" PSK: %s", format_256(ndo
, value
));
842 hncp_print_rec(ndo
, value
+ 32, bodylen
- 32, indent
+1);
846 case RANGE_DNCP_RESERVED
:
847 case RANGE_HNCP_UNASSIGNED
:
848 case RANGE_DNCP_PRIVATE_USE
:
849 case RANGE_DNCP_FUTURE_USE
:
855 i
+= 4 + roundup2(bodylen
, 4);
857 print_type_in_line(ndo
, last_type_mask
, last_type_count
, indent
, &first_one
);
866 nd_print_invalid(ndo
);