]> The Tcpdump Group git mirrors - tcpdump/blob - print-hncp.c
Add more nd_print_trunc() calls
[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(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 nd_snprintf(buf[i], sizeof(buf[i]), "%02x:%02x:%02x:%02x",
166 EXTRACT_U_1(data), EXTRACT_U_1(data + 1), EXTRACT_U_1(data + 2),
167 EXTRACT_U_1(data + 3));
168 return buf[i];
169 }
170
171 static const char *
172 format_256(const u_char *data)
173 {
174 static char buf[4][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")];
175 static int i = 0;
176 i = (i + 1) % 4;
177 nd_snprintf(buf[i], sizeof(buf[i]), "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64,
178 EXTRACT_BE_U_8(data),
179 EXTRACT_BE_U_8(data + 8),
180 EXTRACT_BE_U_8(data + 16),
181 EXTRACT_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 nd_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 ipaddr_string(ndo, cp + IPV4_MAPPED_HEADING_LEN);
201 else
202 return ip6addr_string(ndo, 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 (EXTRACT_U_1(prefix) >= 96 && max_length >= IPV4_MAPPED_HEADING_LEN + 1 &&
212 is_ipv4_mapped_address(prefix + 1)) {
213 struct in_addr addr;
214 u_int plen;
215
216 plen = EXTRACT_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 nd_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 }
235
236 ND_PRINT("%s", buf);
237 return plenbytes;
238 }
239
240 static int
241 print_dns_label(netdissect_options *ndo,
242 const u_char *cp, u_int max_length, int print)
243 {
244 u_int length = 0;
245 while (length < max_length) {
246 u_int lab_length = EXTRACT_U_1(cp + length);
247 length++;
248 if (lab_length == 0)
249 return (int)length;
250 if (length > 1 && print)
251 ND_PRINT(".");
252 if (length+lab_length > max_length) {
253 if (print)
254 (void)nd_printzp(ndo, cp+length, max_length-length, NULL);
255 break;
256 }
257 if (print)
258 (void)nd_printzp(ndo, cp+length, lab_length, NULL);
259 length += lab_length;
260 }
261 if (print)
262 ND_PRINT("[|DNS]");
263 return -1;
264 }
265
266 static int
267 dhcpv4_print(netdissect_options *ndo,
268 const u_char *cp, u_int length, int indent)
269 {
270 u_int i, t;
271 const uint8_t *tlv, *value;
272 uint8_t type, optlen;
273
274 i = 0;
275 while (i < length) {
276 if (i + 2 > length)
277 return -1;
278 tlv = cp + i;
279 type = EXTRACT_U_1(tlv);
280 optlen = EXTRACT_U_1(tlv + 1);
281 value = tlv + 2;
282
283 ND_PRINT("\n");
284 for (t = indent; t > 0; t--)
285 ND_PRINT("\t");
286
287 ND_PRINT("%s", tok2str(dh4opt_str, "Unknown", type));
288 ND_PRINT(" (%u)", optlen + 2 );
289 if (i + 2 + optlen > length)
290 return -1;
291
292 switch (type) {
293 case DH4OPT_DNS_SERVERS:
294 case DH4OPT_NTP_SERVERS: {
295 if (optlen < 4 || optlen % 4 != 0) {
296 return -1;
297 }
298 for (t = 0; t < optlen; t += 4)
299 ND_PRINT(" %s", ipaddr_string(ndo, value + t));
300 }
301 break;
302 case DH4OPT_DOMAIN_SEARCH: {
303 const u_char *tp = value;
304 while (tp < value + optlen) {
305 ND_PRINT(" ");
306 if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL)
307 return -1;
308 }
309 }
310 break;
311 }
312
313 i += 2 + optlen;
314 }
315 return 0;
316 }
317
318 static int
319 dhcpv6_print(netdissect_options *ndo,
320 const u_char *cp, u_int length, int indent)
321 {
322 u_int i, t;
323 const u_char *tlv, *value;
324 uint16_t type, optlen;
325
326 i = 0;
327 while (i < length) {
328 if (i + 4 > length)
329 return -1;
330 tlv = cp + i;
331 type = EXTRACT_BE_U_2(tlv);
332 optlen = EXTRACT_BE_U_2(tlv + 2);
333 value = tlv + 4;
334
335 ND_PRINT("\n");
336 for (t = indent; t > 0; t--)
337 ND_PRINT("\t");
338
339 ND_PRINT("%s", tok2str(dh6opt_str, "Unknown", type));
340 ND_PRINT(" (%u)", optlen + 4 );
341 if (i + 4 + optlen > length)
342 return -1;
343
344 switch (type) {
345 case DH6OPT_DNS_SERVERS:
346 case DH6OPT_SNTP_SERVERS: {
347 if (optlen % 16 != 0) {
348 ND_PRINT("%s", istr);
349 return -1;
350 }
351 for (t = 0; t < optlen; t += 16)
352 ND_PRINT(" %s", ip6addr_string(ndo, value + t));
353 }
354 break;
355 case DH6OPT_DOMAIN_LIST: {
356 const u_char *tp = value;
357 while (tp < value + optlen) {
358 ND_PRINT(" ");
359 if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL)
360 return -1;
361 }
362 }
363 break;
364 }
365
366 i += 4 + optlen;
367 }
368 return 0;
369 }
370
371 /* Determine in-line mode */
372 static int
373 is_in_line(netdissect_options *ndo, int indent)
374 {
375 return indent - 1 >= ndo->ndo_vflag && ndo->ndo_vflag < 3;
376 }
377
378 static void
379 print_type_in_line(netdissect_options *ndo,
380 uint32_t type, int count, int indent, int *first_one)
381 {
382 if (count > 0) {
383 if (*first_one) {
384 *first_one = 0;
385 if (indent > 1) {
386 u_int t;
387 ND_PRINT("\n");
388 for (t = indent; t > 0; t--)
389 ND_PRINT("\t");
390 } else {
391 ND_PRINT(" ");
392 }
393 } else {
394 ND_PRINT(", ");
395 }
396 ND_PRINT("%s", tok2str(type_values, "Easter Egg", type));
397 if (count > 1)
398 ND_PRINT(" (x%d)", count);
399 }
400 }
401
402 static void
403 hncp_print_rec(netdissect_options *ndo,
404 const u_char *cp, u_int length, int indent)
405 {
406 const int in_line = is_in_line(ndo, indent);
407 int first_one = 1;
408
409 u_int i, t;
410
411 uint32_t last_type_mask = 0xffffffffU;
412 int last_type_count = -1;
413
414 const uint8_t *tlv, *value;
415 uint16_t type, bodylen;
416 uint32_t type_mask;
417
418 i = 0;
419 while (i < length) {
420 tlv = cp + i;
421
422 if (!in_line) {
423 ND_PRINT("\n");
424 for (t = indent; t > 0; t--)
425 ND_PRINT("\t");
426 }
427
428 ND_TCHECK_4(tlv);
429 if (i + 4 > length)
430 goto invalid;
431
432 type = EXTRACT_BE_U_2(tlv);
433 bodylen = EXTRACT_BE_U_2(tlv + 2);
434 value = tlv + 4;
435 ND_TCHECK_LEN(value, bodylen);
436 if (i + bodylen + 4 > length)
437 goto invalid;
438
439 type_mask =
440 (type == 0) ? RANGE_DNCP_RESERVED:
441 (44 <= type && type <= 511) ? RANGE_HNCP_UNASSIGNED:
442 (768 <= type && type <= 1023) ? RANGE_DNCP_PRIVATE_USE:
443 RANGE_DNCP_FUTURE_USE;
444 if (type == 6 || type == 7)
445 type_mask = RANGE_DNCP_FUTURE_USE;
446
447 /* defined types */
448 {
449 t = 0;
450 while (1) {
451 u_int key = type_values[t++].v;
452 if (key > 0xffff)
453 break;
454 if (key == type) {
455 type_mask = type;
456 break;
457 }
458 }
459 }
460
461 if (in_line) {
462 if (last_type_mask == type_mask) {
463 last_type_count++;
464 } else {
465 print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
466 last_type_mask = type_mask;
467 last_type_count = 1;
468 }
469
470 goto skip_multiline;
471 }
472
473 ND_PRINT("%s", tok2str(type_values, "Easter Egg (42)", type_mask) );
474 if (type_mask > 0xffff)
475 ND_PRINT(": type=%u", type );
476 ND_PRINT(" (%u)", bodylen + 4 );
477
478 switch (type_mask) {
479
480 case DNCP_REQUEST_NETWORK_STATE: {
481 if (bodylen != 0)
482 ND_PRINT("%s", istr);
483 }
484 break;
485
486 case DNCP_REQUEST_NODE_STATE: {
487 const char *node_identifier;
488 if (bodylen != 4) {
489 ND_PRINT("%s", istr);
490 break;
491 }
492 node_identifier = format_nid(value);
493 ND_PRINT(" NID: %s", node_identifier);
494 }
495 break;
496
497 case DNCP_NODE_ENDPOINT: {
498 const char *node_identifier;
499 uint32_t endpoint_identifier;
500 if (bodylen != 8) {
501 ND_PRINT("%s", istr);
502 break;
503 }
504 node_identifier = format_nid(value);
505 endpoint_identifier = EXTRACT_BE_U_4(value + 4);
506 ND_PRINT(" NID: %s EPID: %08x",
507 node_identifier,
508 endpoint_identifier
509 );
510 }
511 break;
512
513 case DNCP_NETWORK_STATE: {
514 uint64_t hash;
515 if (bodylen != 8) {
516 ND_PRINT("%s", istr);
517 break;
518 }
519 hash = EXTRACT_BE_U_8(value);
520 ND_PRINT(" hash: %016" PRIx64, hash);
521 }
522 break;
523
524 case DNCP_NODE_STATE: {
525 const char *node_identifier, *interval;
526 uint32_t sequence_number;
527 uint64_t hash;
528 if (bodylen < 20) {
529 ND_PRINT("%s", istr);
530 break;
531 }
532 node_identifier = format_nid(value);
533 sequence_number = EXTRACT_BE_U_4(value + 4);
534 interval = format_interval(EXTRACT_BE_U_4(value + 8));
535 hash = EXTRACT_BE_U_8(value + 12);
536 ND_PRINT(" NID: %s seqno: %u %s hash: %016" PRIx64,
537 node_identifier,
538 sequence_number,
539 interval,
540 hash
541 );
542 hncp_print_rec(ndo, value+20, bodylen-20, indent+1);
543 }
544 break;
545
546 case DNCP_PEER: {
547 const char *peer_node_identifier;
548 uint32_t peer_endpoint_identifier, endpoint_identifier;
549 if (bodylen != 12) {
550 ND_PRINT("%s", istr);
551 break;
552 }
553 peer_node_identifier = format_nid(value);
554 peer_endpoint_identifier = EXTRACT_BE_U_4(value + 4);
555 endpoint_identifier = EXTRACT_BE_U_4(value + 8);
556 ND_PRINT(" Peer-NID: %s Peer-EPID: %08x Local-EPID: %08x",
557 peer_node_identifier,
558 peer_endpoint_identifier,
559 endpoint_identifier
560 );
561 }
562 break;
563
564 case DNCP_KEEP_ALIVE_INTERVAL: {
565 uint32_t endpoint_identifier;
566 const char *interval;
567 if (bodylen < 8) {
568 ND_PRINT("%s", istr);
569 break;
570 }
571 endpoint_identifier = EXTRACT_BE_U_4(value);
572 interval = format_interval(EXTRACT_BE_U_4(value + 4));
573 ND_PRINT(" EPID: %08x Interval: %s",
574 endpoint_identifier,
575 interval
576 );
577 }
578 break;
579
580 case DNCP_TRUST_VERDICT: {
581 if (bodylen <= 36) {
582 ND_PRINT("%s", istr);
583 break;
584 }
585 ND_PRINT(" Verdict: %u Fingerprint: %s Common Name: ",
586 EXTRACT_U_1(value),
587 format_256(value + 4));
588 (void)nd_printzp(ndo, value + 36, bodylen - 36, NULL);
589 }
590 break;
591
592 case HNCP_HNCP_VERSION: {
593 uint16_t capabilities;
594 uint8_t M, P, H, L;
595 if (bodylen < 5) {
596 ND_PRINT("%s", istr);
597 break;
598 }
599 capabilities = EXTRACT_BE_U_2(value + 2);
600 M = (uint8_t)((capabilities >> 12) & 0xf);
601 P = (uint8_t)((capabilities >> 8) & 0xf);
602 H = (uint8_t)((capabilities >> 4) & 0xf);
603 L = (uint8_t)(capabilities & 0xf);
604 ND_PRINT(" M: %u P: %u H: %u L: %u User-agent: ",
605 M, P, H, L
606 );
607 (void)nd_printzp(ndo, value + 4, bodylen - 4, NULL);
608 }
609 break;
610
611 case HNCP_EXTERNAL_CONNECTION: {
612 /* Container TLV */
613 hncp_print_rec(ndo, value, bodylen, indent+1);
614 }
615 break;
616
617 case HNCP_DELEGATED_PREFIX: {
618 int l;
619 if (bodylen < 9 || bodylen < 9 + (EXTRACT_U_1(value + 8) + 7) / 8) {
620 ND_PRINT("%s", istr);
621 break;
622 }
623 ND_PRINT(" VLSO: %s PLSO: %s Prefix: ",
624 format_interval(EXTRACT_BE_U_4(value)),
625 format_interval(EXTRACT_BE_U_4(value + 4))
626 );
627 l = print_prefix(ndo, value + 8, bodylen - 8);
628 if (l == -1) {
629 ND_PRINT("(length is invalid)");
630 break;
631 }
632 if (l < 0) {
633 /*
634 * We've already checked that we've captured the
635 * entire TLV, based on its length, so this will
636 * either be -1, meaning "the prefix length is
637 * greater than the longest possible address of
638 * that type" (i.e., > 32 for IPv4 or > 128 for
639 * IPv6", or -3, meaning "the prefix runs past
640 * the end of the TLV".
641 */
642 ND_PRINT("%s", istr);
643 break;
644 }
645 l += 8 + (-l & 3);
646
647 if (bodylen >= l)
648 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
649 }
650 break;
651
652 case HNCP_PREFIX_POLICY: {
653 uint8_t policy;
654 int l;
655 if (bodylen < 1) {
656 ND_PRINT("%s", istr);
657 break;
658 }
659 policy = EXTRACT_U_1(value);
660 ND_PRINT(" type: ");
661 if (policy == 0) {
662 if (bodylen != 1) {
663 ND_PRINT("%s", istr);
664 break;
665 }
666 ND_PRINT("Internet connectivity");
667 } else if (policy >= 1 && policy <= 128) {
668 ND_PRINT("Dest-Prefix: ");
669 l = print_prefix(ndo, value, bodylen);
670 if (l == -1) {
671 ND_PRINT("(length is invalid)");
672 break;
673 }
674 if (l < 0) {
675 /*
676 * We've already checked that we've captured the
677 * entire TLV, based on its length, so this will
678 * either be -1, meaning "the prefix length is
679 * greater than the longest possible address of
680 * that type" (i.e., > 32 for IPv4 or > 128 for
681 * IPv6", or -3, meaning "the prefix runs past
682 * the end of the TLV".
683 */
684 ND_PRINT("%s", istr);
685 break;
686 }
687 } else if (policy == 129) {
688 ND_PRINT("DNS domain: ");
689 print_dns_label(ndo, value+1, bodylen-1, 1);
690 } else if (policy == 130) {
691 ND_PRINT("Opaque UTF-8: ");
692 (void)nd_printzp(ndo, value + 1, bodylen - 1, NULL);
693 } else if (policy == 131) {
694 if (bodylen != 1) {
695 ND_PRINT("%s", istr);
696 break;
697 }
698 ND_PRINT("Restrictive assignment");
699 } else if (policy >= 132) {
700 ND_PRINT("Unknown (%u)", policy); /* Reserved for future additions */
701 }
702 }
703 break;
704
705 case HNCP_DHCPV4_DATA: {
706 if (bodylen == 0) {
707 ND_PRINT("%s", istr);
708 break;
709 }
710 if (dhcpv4_print(ndo, value, bodylen, indent+1) != 0)
711 goto invalid;
712 }
713 break;
714
715 case HNCP_DHCPV6_DATA: {
716 if (bodylen == 0) {
717 ND_PRINT("%s", istr);
718 break;
719 }
720 if (dhcpv6_print(ndo, value, bodylen, indent+1) != 0) {
721 ND_PRINT("%s", istr);
722 break;
723 }
724 }
725 break;
726
727 case HNCP_ASSIGNED_PREFIX: {
728 uint8_t prty;
729 int l;
730 if (bodylen < 6 || bodylen < 6 + (EXTRACT_U_1(value + 5) + 7) / 8) {
731 ND_PRINT("%s", istr);
732 break;
733 }
734 prty = EXTRACT_U_1(value + 4) & 0xf;
735 ND_PRINT(" EPID: %08x Prty: %u",
736 EXTRACT_BE_U_4(value),
737 prty
738 );
739 ND_PRINT(" Prefix: ");
740 if ((l = print_prefix(ndo, value + 5, bodylen - 5)) < 0) {
741 ND_PRINT("%s", istr);
742 break;
743 }
744 l += 5;
745 l += -l & 3;
746
747 if (bodylen >= l)
748 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
749 }
750 break;
751
752 case HNCP_NODE_ADDRESS: {
753 uint32_t endpoint_identifier;
754 const char *ip_address;
755 if (bodylen < 20) {
756 ND_PRINT("%s", istr);
757 break;
758 }
759 endpoint_identifier = EXTRACT_BE_U_4(value);
760 ip_address = format_ip6addr(ndo, value + 4);
761 ND_PRINT(" EPID: %08x IP Address: %s",
762 endpoint_identifier,
763 ip_address
764 );
765
766 hncp_print_rec(ndo, value + 20, bodylen - 20, indent+1);
767 }
768 break;
769
770 case HNCP_DNS_DELEGATED_ZONE: {
771 const char *ip_address;
772 int len;
773 if (bodylen < 17) {
774 ND_PRINT("%s", istr);
775 break;
776 }
777 ip_address = format_ip6addr(ndo, value);
778 ND_PRINT(" IP-Address: %s %c%c%c ",
779 ip_address,
780 (EXTRACT_U_1(value + 16) & 4) ? 'l' : '-',
781 (EXTRACT_U_1(value + 16) & 2) ? 'b' : '-',
782 (EXTRACT_U_1(value + 16) & 1) ? 's' : '-'
783 );
784 len = print_dns_label(ndo, value+17, bodylen-17, 1);
785 if (len < 0) {
786 ND_PRINT("%s", istr);
787 break;
788 }
789 len += 17;
790 len += -len & 3;
791 if (bodylen >= len)
792 hncp_print_rec(ndo, value+len, bodylen-len, indent+1);
793 }
794 break;
795
796 case HNCP_DOMAIN_NAME: {
797 if (bodylen == 0) {
798 ND_PRINT("%s", istr);
799 break;
800 }
801 ND_PRINT(" Domain: ");
802 print_dns_label(ndo, value, bodylen, 1);
803 }
804 break;
805
806 case HNCP_NODE_NAME: {
807 u_int l;
808 if (bodylen < 17) {
809 ND_PRINT("%s", istr);
810 break;
811 }
812 l = EXTRACT_U_1(value + 16);
813 if (bodylen < 17 + l) {
814 ND_PRINT("%s", istr);
815 break;
816 }
817 ND_PRINT(" IP-Address: %s Name: ",
818 format_ip6addr(ndo, value)
819 );
820 if (l < 64) {
821 ND_PRINT("\"");
822 (void)nd_printzp(ndo, value + 17, l, NULL);
823 ND_PRINT("\"");
824 } else {
825 ND_PRINT("%s", istr);
826 }
827 l += 17;
828 l += -l & 3;
829 if (bodylen >= l)
830 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
831 }
832 break;
833
834 case HNCP_MANAGED_PSK: {
835 if (bodylen < 32) {
836 ND_PRINT("%s", istr);
837 break;
838 }
839 ND_PRINT(" PSK: %s", format_256(value));
840 hncp_print_rec(ndo, value + 32, bodylen - 32, indent+1);
841 }
842 break;
843
844 case RANGE_DNCP_RESERVED:
845 case RANGE_HNCP_UNASSIGNED:
846 case RANGE_DNCP_PRIVATE_USE:
847 case RANGE_DNCP_FUTURE_USE:
848 break;
849
850 }
851 skip_multiline:
852
853 i += 4 + bodylen + (-bodylen & 3);
854 }
855 print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
856
857 return;
858
859 trunc:
860 nd_print_trunc(ndo);
861 return;
862
863 invalid:
864 ND_PRINT("%s", istr);
865 return;
866 }