]> The Tcpdump Group git mirrors - tcpdump/blob - print-hncp.c
Remove useless 'return' at end of void functions (style)
[tcpdump] / print-hncp.c
1 /*
2 * Copyright (c) 2016 Antonin Décimo, Jean-Raphaël Gaglione
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 /* \summary: Home Networking Control Protocol (HNCP) printer */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include "netdissect-stdinc.h"
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "netdissect.h"
41 #include "addrtoname.h"
42 #include "extract.h"
43
44 static void
45 hncp_print_rec(netdissect_options *ndo,
46 const u_char *cp, u_int length, int indent);
47
48 void
49 hncp_print(netdissect_options *ndo,
50 const u_char *cp, u_int length)
51 {
52 ndo->ndo_protocol = "hncp";
53 ND_PRINT("hncp (%u)", length);
54 hncp_print_rec(ndo, cp, length, 1);
55 }
56
57 /* RFC7787 */
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
63 #define DNCP_PEER 8
64 #define DNCP_KEEP_ALIVE_INTERVAL 9
65 #define DNCP_TRUST_VERDICT 10
66
67 /* RFC7788 */
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
80
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
86
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" },
96
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" },
109
110 { RANGE_DNCP_RESERVED, "Reserved" },
111 { RANGE_HNCP_UNASSIGNED, "Unassigned" },
112 { RANGE_DNCP_PRIVATE_USE, "Private use" },
113 { RANGE_DNCP_FUTURE_USE, "Future use" },
114
115 { 0, NULL}
116 };
117
118 #define DH4OPT_DNS_SERVERS 6 /* RFC2132 */
119 #define DH4OPT_NTP_SERVERS 42 /* RFC2132 */
120 #define DH4OPT_DOMAIN_SEARCH 119 /* RFC3397 */
121
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" },
126 { 0, NULL }
127 };
128
129 #define DH6OPT_DNS_SERVERS 23 /* RFC3646 */
130 #define DH6OPT_DOMAIN_LIST 24 /* RFC3646 */
131 #define DH6OPT_SNTP_SERVERS 31 /* RFC4075 */
132
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" },
137 { 0, NULL }
138 };
139
140 /*
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.
143 */
144 #define IPV4_MAPPED_HEADING_LEN 12
145
146 /*
147 * Is an IPv6 address an IPv4-mapped address?
148 */
149 static int
150 is_ipv4_mapped_address(const u_char *addr)
151 {
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 };
155
156 return memcmp(addr, ipv4_mapped_heading, IPV4_MAPPED_HEADING_LEN) == 0;
157 }
158
159 static const char *
160 format_nid(netdissect_options *ndo, const u_char *data)
161 {
162 static char buf[4][sizeof("01:01:01:01")];
163 static int i = 0;
164 i = (i + 1) % 4;
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),
167 GET_U_1(data + 3));
168 return buf[i];
169 }
170
171 static const char *
172 format_256(netdissect_options *ndo, const u_char *data)
173 {
174 static char buf[4][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")];
175 static int i = 0;
176 i = (i + 1) % 4;
177 snprintf(buf[i], sizeof(buf[i]), "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64,
178 GET_BE_U_8(data),
179 GET_BE_U_8(data + 8),
180 GET_BE_U_8(data + 16),
181 GET_BE_U_8(data + 24)
182 );
183 return buf[i];
184 }
185
186 static const char *
187 format_interval(const uint32_t n)
188 {
189 static char buf[4][sizeof("0000000.000s")];
190 static int i = 0;
191 i = (i + 1) % 4;
192 snprintf(buf[i], sizeof(buf[i]), "%u.%03us", n / 1000, n % 1000);
193 return buf[i];
194 }
195
196 static const char *
197 format_ip6addr(netdissect_options *ndo, const u_char *cp)
198 {
199 if (is_ipv4_mapped_address(cp))
200 return GET_IPADDR_STRING(cp + IPV4_MAPPED_HEADING_LEN);
201 else
202 return GET_IP6ADDR_STRING(cp);
203 }
204
205 static int
206 print_prefix(netdissect_options *ndo, const u_char *prefix, u_int max_length)
207 {
208 int plenbytes;
209 char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::/128")];
210
211 if (GET_U_1(prefix) >= 96 && max_length >= IPV4_MAPPED_HEADING_LEN + 1 &&
212 is_ipv4_mapped_address(prefix + 1)) {
213 nd_ipv4 addr;
214 u_int plen;
215
216 plen = GET_U_1(prefix) - 96;
217 if (32 < plen)
218 return -1;
219 max_length -= 1;
220
221 memset(&addr, 0, sizeof(addr));
222 plenbytes = (plen + 7) / 8;
223 if (max_length < (u_int)plenbytes + IPV4_MAPPED_HEADING_LEN)
224 return -3;
225 memcpy(&addr, prefix + IPV4_MAPPED_HEADING_LEN + 1, plenbytes);
226 if (plen % 8) {
227 ((u_char *)&addr)[plenbytes - 1] &=
228 ((0xff00 >> (plen % 8)) & 0xff);
229 }
230 snprintf(buf, sizeof(buf), "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen);
231 plenbytes += 1 + IPV4_MAPPED_HEADING_LEN;
232 } else {
233 plenbytes = decode_prefix6(ndo, prefix, max_length, buf, sizeof(buf));
234 if (plenbytes < 0)
235 return plenbytes;
236 }
237
238 ND_PRINT("%s", buf);
239 return plenbytes;
240 }
241
242 static int
243 print_dns_label(netdissect_options *ndo,
244 const u_char *cp, u_int max_length, int print)
245 {
246 u_int length = 0;
247 while (length < max_length) {
248 u_int lab_length = GET_U_1(cp + length);
249 length++;
250 if (lab_length == 0)
251 return (int)length;
252 if (length > 1 && print)
253 ND_PRINT(".");
254 if (length+lab_length > max_length) {
255 if (print)
256 (void)nd_printzp(ndo, cp+length, max_length-length, NULL);
257 break;
258 }
259 if (print)
260 (void)nd_printzp(ndo, cp+length, lab_length, NULL);
261 length += lab_length;
262 }
263 if (print)
264 ND_PRINT("[|DNS]");
265 return -1;
266 }
267
268 static int
269 dhcpv4_print(netdissect_options *ndo,
270 const u_char *cp, u_int length, int indent)
271 {
272 u_int i, t;
273 const uint8_t *tlv, *value;
274 uint8_t type, optlen;
275
276 i = 0;
277 while (i < length) {
278 if (i + 2 > length)
279 return -1;
280 tlv = cp + i;
281 type = GET_U_1(tlv);
282 optlen = GET_U_1(tlv + 1);
283 value = tlv + 2;
284
285 ND_PRINT("\n");
286 for (t = indent; t > 0; t--)
287 ND_PRINT("\t");
288
289 ND_PRINT("%s", tok2str(dh4opt_str, "Unknown", type));
290 ND_PRINT(" (%u)", optlen + 2 );
291 if (i + 2 + optlen > length)
292 return -1;
293
294 switch (type) {
295 case DH4OPT_DNS_SERVERS:
296 case DH4OPT_NTP_SERVERS: {
297 if (optlen < 4 || optlen % 4 != 0) {
298 return -1;
299 }
300 for (t = 0; t < optlen; t += 4)
301 ND_PRINT(" %s", GET_IPADDR_STRING(value + t));
302 }
303 break;
304 case DH4OPT_DOMAIN_SEARCH: {
305 const u_char *tp = value;
306 while (tp < value + optlen) {
307 ND_PRINT(" ");
308 if ((tp = fqdn_print(ndo, tp, value + optlen)) == NULL)
309 return -1;
310 }
311 }
312 break;
313 }
314
315 i += 2 + optlen;
316 }
317 return 0;
318 }
319
320 static int
321 dhcpv6_print(netdissect_options *ndo,
322 const u_char *cp, u_int length, int indent)
323 {
324 u_int i, t;
325 const u_char *tlv, *value;
326 uint16_t type, optlen;
327
328 i = 0;
329 while (i < length) {
330 if (i + 4 > length)
331 return -1;
332 tlv = cp + i;
333 type = GET_BE_U_2(tlv);
334 optlen = GET_BE_U_2(tlv + 2);
335 value = tlv + 4;
336
337 ND_PRINT("\n");
338 for (t = indent; t > 0; t--)
339 ND_PRINT("\t");
340
341 ND_PRINT("%s", tok2str(dh6opt_str, "Unknown", type));
342 ND_PRINT(" (%u)", optlen + 4 );
343 if (i + 4 + optlen > length)
344 return -1;
345
346 switch (type) {
347 case DH6OPT_DNS_SERVERS:
348 case DH6OPT_SNTP_SERVERS: {
349 if (optlen % 16 != 0) {
350 nd_print_invalid(ndo);
351 return -1;
352 }
353 for (t = 0; t < optlen; t += 16)
354 ND_PRINT(" %s", GET_IP6ADDR_STRING(value + t));
355 }
356 break;
357 case DH6OPT_DOMAIN_LIST: {
358 const u_char *tp = value;
359 while (tp < value + optlen) {
360 ND_PRINT(" ");
361 if ((tp = fqdn_print(ndo, tp, value + optlen)) == NULL)
362 return -1;
363 }
364 }
365 break;
366 }
367
368 i += 4 + optlen;
369 }
370 return 0;
371 }
372
373 /* Determine in-line mode */
374 static int
375 is_in_line(netdissect_options *ndo, int indent)
376 {
377 return indent - 1 >= ndo->ndo_vflag && ndo->ndo_vflag < 3;
378 }
379
380 static void
381 print_type_in_line(netdissect_options *ndo,
382 uint32_t type, int count, int indent, int *first_one)
383 {
384 if (count > 0) {
385 if (*first_one) {
386 *first_one = 0;
387 if (indent > 1) {
388 u_int t;
389 ND_PRINT("\n");
390 for (t = indent; t > 0; t--)
391 ND_PRINT("\t");
392 } else {
393 ND_PRINT(" ");
394 }
395 } else {
396 ND_PRINT(", ");
397 }
398 ND_PRINT("%s", tok2str(type_values, "Easter Egg", type));
399 if (count > 1)
400 ND_PRINT(" (x%d)", count);
401 }
402 }
403
404 static void
405 hncp_print_rec(netdissect_options *ndo,
406 const u_char *cp, u_int length, int indent)
407 {
408 const int in_line = is_in_line(ndo, indent);
409 int first_one = 1;
410
411 u_int i, t;
412
413 uint32_t last_type_mask = 0xffffffffU;
414 int last_type_count = -1;
415
416 const uint8_t *tlv, *value;
417 uint16_t type, bodylen;
418 uint32_t type_mask;
419
420 i = 0;
421 while (i < length) {
422 tlv = cp + i;
423
424 if (!in_line) {
425 ND_PRINT("\n");
426 for (t = indent; t > 0; t--)
427 ND_PRINT("\t");
428 }
429
430 ND_TCHECK_4(tlv);
431 if (i + 4 > length)
432 goto invalid;
433
434 type = GET_BE_U_2(tlv);
435 bodylen = GET_BE_U_2(tlv + 2);
436 value = tlv + 4;
437 ND_TCHECK_LEN(value, bodylen);
438 if (i + bodylen + 4 > length)
439 goto invalid;
440
441 type_mask =
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;
448
449 /* defined types */
450 {
451 t = 0;
452 while (1) {
453 u_int key = type_values[t++].v;
454 if (key > 0xffff)
455 break;
456 if (key == type) {
457 type_mask = type;
458 break;
459 }
460 }
461 }
462
463 if (in_line) {
464 if (last_type_mask == type_mask) {
465 last_type_count++;
466 } else {
467 print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
468 last_type_mask = type_mask;
469 last_type_count = 1;
470 }
471
472 goto skip_multiline;
473 }
474
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 );
479
480 switch (type_mask) {
481
482 case DNCP_REQUEST_NETWORK_STATE: {
483 if (bodylen != 0)
484 nd_print_invalid(ndo);
485 }
486 break;
487
488 case DNCP_REQUEST_NODE_STATE: {
489 const char *node_identifier;
490 if (bodylen != 4) {
491 nd_print_invalid(ndo);
492 break;
493 }
494 node_identifier = format_nid(ndo, value);
495 ND_PRINT(" NID: %s", node_identifier);
496 }
497 break;
498
499 case DNCP_NODE_ENDPOINT: {
500 const char *node_identifier;
501 uint32_t endpoint_identifier;
502 if (bodylen != 8) {
503 nd_print_invalid(ndo);
504 break;
505 }
506 node_identifier = format_nid(ndo, value);
507 endpoint_identifier = GET_BE_U_4(value + 4);
508 ND_PRINT(" NID: %s EPID: %08x",
509 node_identifier,
510 endpoint_identifier
511 );
512 }
513 break;
514
515 case DNCP_NETWORK_STATE: {
516 uint64_t hash;
517 if (bodylen != 8) {
518 nd_print_invalid(ndo);
519 break;
520 }
521 hash = GET_BE_U_8(value);
522 ND_PRINT(" hash: %016" PRIx64, hash);
523 }
524 break;
525
526 case DNCP_NODE_STATE: {
527 const char *node_identifier, *interval;
528 uint32_t sequence_number;
529 uint64_t hash;
530 if (bodylen < 20) {
531 nd_print_invalid(ndo);
532 break;
533 }
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,
539 node_identifier,
540 sequence_number,
541 interval,
542 hash
543 );
544 hncp_print_rec(ndo, value+20, bodylen-20, indent+1);
545 }
546 break;
547
548 case DNCP_PEER: {
549 const char *peer_node_identifier;
550 uint32_t peer_endpoint_identifier, endpoint_identifier;
551 if (bodylen != 12) {
552 nd_print_invalid(ndo);
553 break;
554 }
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,
561 endpoint_identifier
562 );
563 }
564 break;
565
566 case DNCP_KEEP_ALIVE_INTERVAL: {
567 uint32_t endpoint_identifier;
568 const char *interval;
569 if (bodylen < 8) {
570 nd_print_invalid(ndo);
571 break;
572 }
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",
576 endpoint_identifier,
577 interval
578 );
579 }
580 break;
581
582 case DNCP_TRUST_VERDICT: {
583 if (bodylen <= 36) {
584 nd_print_invalid(ndo);
585 break;
586 }
587 ND_PRINT(" Verdict: %u Fingerprint: %s Common Name: ",
588 GET_U_1(value),
589 format_256(ndo, value + 4));
590 (void)nd_printzp(ndo, value + 36, bodylen - 36, NULL);
591 }
592 break;
593
594 case HNCP_HNCP_VERSION: {
595 uint16_t capabilities;
596 uint8_t M, P, H, L;
597 if (bodylen < 5) {
598 nd_print_invalid(ndo);
599 break;
600 }
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: ",
607 M, P, H, L
608 );
609 (void)nd_printzp(ndo, value + 4, bodylen - 4, NULL);
610 }
611 break;
612
613 case HNCP_EXTERNAL_CONNECTION: {
614 /* Container TLV */
615 hncp_print_rec(ndo, value, bodylen, indent+1);
616 }
617 break;
618
619 case HNCP_DELEGATED_PREFIX: {
620 int l;
621 if (bodylen < 9 || bodylen < 9 + (GET_U_1(value + 8) + 7) / 8) {
622 nd_print_invalid(ndo);
623 break;
624 }
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))
628 );
629 l = print_prefix(ndo, value + 8, bodylen - 8);
630 if (l == -1) {
631 ND_PRINT("(length is invalid)");
632 break;
633 }
634 if (l < 0) {
635 /*
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".
643 */
644 nd_print_invalid(ndo);
645 break;
646 }
647 l += 8 + (-l & 3);
648
649 if (bodylen >= l)
650 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
651 }
652 break;
653
654 case HNCP_PREFIX_POLICY: {
655 uint8_t policy;
656 int l;
657 if (bodylen < 1) {
658 nd_print_invalid(ndo);
659 break;
660 }
661 policy = GET_U_1(value);
662 ND_PRINT(" type: ");
663 if (policy == 0) {
664 if (bodylen != 1) {
665 nd_print_invalid(ndo);
666 break;
667 }
668 ND_PRINT("Internet connectivity");
669 } else if (policy >= 1 && policy <= 128) {
670 ND_PRINT("Dest-Prefix: ");
671 l = print_prefix(ndo, value, bodylen);
672 if (l == -1) {
673 ND_PRINT("(length is invalid)");
674 break;
675 }
676 if (l < 0) {
677 /*
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".
685 */
686 nd_print_invalid(ndo);
687 break;
688 }
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) {
696 if (bodylen != 1) {
697 nd_print_invalid(ndo);
698 break;
699 }
700 ND_PRINT("Restrictive assignment");
701 } else if (policy >= 132) {
702 ND_PRINT("Unknown (%u)", policy); /* Reserved for future additions */
703 }
704 }
705 break;
706
707 case HNCP_DHCPV4_DATA: {
708 if (bodylen == 0) {
709 nd_print_invalid(ndo);
710 break;
711 }
712 if (dhcpv4_print(ndo, value, bodylen, indent+1) != 0)
713 goto invalid;
714 }
715 break;
716
717 case HNCP_DHCPV6_DATA: {
718 if (bodylen == 0) {
719 nd_print_invalid(ndo);
720 break;
721 }
722 if (dhcpv6_print(ndo, value, bodylen, indent+1) != 0) {
723 nd_print_invalid(ndo);
724 break;
725 }
726 }
727 break;
728
729 case HNCP_ASSIGNED_PREFIX: {
730 uint8_t prty;
731 int l;
732 if (bodylen < 6 || bodylen < 6 + (GET_U_1(value + 5) + 7) / 8) {
733 nd_print_invalid(ndo);
734 break;
735 }
736 prty = GET_U_1(value + 4) & 0xf;
737 ND_PRINT(" EPID: %08x Prty: %u",
738 GET_BE_U_4(value),
739 prty
740 );
741 ND_PRINT(" Prefix: ");
742 if ((l = print_prefix(ndo, value + 5, bodylen - 5)) < 0) {
743 nd_print_invalid(ndo);
744 break;
745 }
746 l += 5;
747 l += -l & 3;
748
749 if (bodylen >= l)
750 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
751 }
752 break;
753
754 case HNCP_NODE_ADDRESS: {
755 uint32_t endpoint_identifier;
756 const char *ip_address;
757 if (bodylen < 20) {
758 nd_print_invalid(ndo);
759 break;
760 }
761 endpoint_identifier = GET_BE_U_4(value);
762 ip_address = format_ip6addr(ndo, value + 4);
763 ND_PRINT(" EPID: %08x IP Address: %s",
764 endpoint_identifier,
765 ip_address
766 );
767
768 hncp_print_rec(ndo, value + 20, bodylen - 20, indent+1);
769 }
770 break;
771
772 case HNCP_DNS_DELEGATED_ZONE: {
773 const char *ip_address;
774 int len;
775 if (bodylen < 17) {
776 nd_print_invalid(ndo);
777 break;
778 }
779 ip_address = format_ip6addr(ndo, value);
780 ND_PRINT(" IP-Address: %s %c%c%c ",
781 ip_address,
782 (GET_U_1(value + 16) & 4) ? 'l' : '-',
783 (GET_U_1(value + 16) & 2) ? 'b' : '-',
784 (GET_U_1(value + 16) & 1) ? 's' : '-'
785 );
786 len = print_dns_label(ndo, value+17, bodylen-17, 1);
787 if (len < 0) {
788 nd_print_invalid(ndo);
789 break;
790 }
791 len += 17;
792 len += -len & 3;
793 if (bodylen >= len)
794 hncp_print_rec(ndo, value+len, bodylen-len, indent+1);
795 }
796 break;
797
798 case HNCP_DOMAIN_NAME: {
799 if (bodylen == 0) {
800 nd_print_invalid(ndo);
801 break;
802 }
803 ND_PRINT(" Domain: ");
804 print_dns_label(ndo, value, bodylen, 1);
805 }
806 break;
807
808 case HNCP_NODE_NAME: {
809 u_int l;
810 if (bodylen < 17) {
811 nd_print_invalid(ndo);
812 break;
813 }
814 l = GET_U_1(value + 16);
815 if (bodylen < 17 + l) {
816 nd_print_invalid(ndo);
817 break;
818 }
819 ND_PRINT(" IP-Address: %s Name: ",
820 format_ip6addr(ndo, value)
821 );
822 if (l < 64) {
823 ND_PRINT("\"");
824 (void)nd_printzp(ndo, value + 17, l, NULL);
825 ND_PRINT("\"");
826 } else {
827 nd_print_invalid(ndo);
828 }
829 l += 17;
830 l = roundup2(l, 4);
831 if (bodylen >= l)
832 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
833 }
834 break;
835
836 case HNCP_MANAGED_PSK: {
837 if (bodylen < 32) {
838 nd_print_invalid(ndo);
839 break;
840 }
841 ND_PRINT(" PSK: %s", format_256(ndo, value));
842 hncp_print_rec(ndo, value + 32, bodylen - 32, indent+1);
843 }
844 break;
845
846 case RANGE_DNCP_RESERVED:
847 case RANGE_HNCP_UNASSIGNED:
848 case RANGE_DNCP_PRIVATE_USE:
849 case RANGE_DNCP_FUTURE_USE:
850 break;
851
852 }
853 skip_multiline:
854
855 i += 4 + roundup2(bodylen, 4);
856 }
857 print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
858
859 return;
860
861 trunc:
862 nd_print_trunc(ndo);
863 return;
864
865 invalid:
866 nd_print_invalid(ndo);
867 }