]> The Tcpdump Group git mirrors - tcpdump/blob - print-hncp.c
Use more the ND_TCHECK_1() macro
[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 ND_PRINT((ndo, "hncp (%d)", length));
53 hncp_print_rec(ndo, cp, length, 1);
54 }
55
56 /* RFC7787 */
57 #define DNCP_REQUEST_NETWORK_STATE 1
58 #define DNCP_REQUEST_NODE_STATE 2
59 #define DNCP_NODE_ENDPOINT 3
60 #define DNCP_NETWORK_STATE 4
61 #define DNCP_NODE_STATE 5
62 #define DNCP_PEER 8
63 #define DNCP_KEEP_ALIVE_INTERVAL 9
64 #define DNCP_TRUST_VERDICT 10
65
66 /* RFC7788 */
67 #define HNCP_HNCP_VERSION 32
68 #define HNCP_EXTERNAL_CONNECTION 33
69 #define HNCP_DELEGATED_PREFIX 34
70 #define HNCP_PREFIX_POLICY 43
71 #define HNCP_DHCPV4_DATA 37 /* This is correct, see RFC 7788 Errata ID 5113. */
72 #define HNCP_DHCPV6_DATA 38 /* idem */
73 #define HNCP_ASSIGNED_PREFIX 35
74 #define HNCP_NODE_ADDRESS 36
75 #define HNCP_DNS_DELEGATED_ZONE 39
76 #define HNCP_DOMAIN_NAME 40
77 #define HNCP_NODE_NAME 41
78 #define HNCP_MANAGED_PSK 42
79
80 /* See type_mask in hncp_print_rec below */
81 #define RANGE_DNCP_RESERVED 0x10000
82 #define RANGE_HNCP_UNASSIGNED 0x10001
83 #define RANGE_DNCP_PRIVATE_USE 0x10002
84 #define RANGE_DNCP_FUTURE_USE 0x10003
85
86 static const struct tok type_values[] = {
87 { DNCP_REQUEST_NETWORK_STATE, "Request network state" },
88 { DNCP_REQUEST_NODE_STATE, "Request node state" },
89 { DNCP_NODE_ENDPOINT, "Node endpoint" },
90 { DNCP_NETWORK_STATE, "Network state" },
91 { DNCP_NODE_STATE, "Node state" },
92 { DNCP_PEER, "Peer" },
93 { DNCP_KEEP_ALIVE_INTERVAL, "Keep-alive interval" },
94 { DNCP_TRUST_VERDICT, "Trust-Verdict" },
95
96 { HNCP_HNCP_VERSION, "HNCP-Version" },
97 { HNCP_EXTERNAL_CONNECTION, "External-Connection" },
98 { HNCP_DELEGATED_PREFIX, "Delegated-Prefix" },
99 { HNCP_PREFIX_POLICY, "Prefix-Policy" },
100 { HNCP_DHCPV4_DATA, "DHCPv4-Data" },
101 { HNCP_DHCPV6_DATA, "DHCPv6-Data" },
102 { HNCP_ASSIGNED_PREFIX, "Assigned-Prefix" },
103 { HNCP_NODE_ADDRESS, "Node-Address" },
104 { HNCP_DNS_DELEGATED_ZONE, "DNS-Delegated-Zone" },
105 { HNCP_DOMAIN_NAME, "Domain-Name" },
106 { HNCP_NODE_NAME, "Node-Name" },
107 { HNCP_MANAGED_PSK, "Managed-PSK" },
108
109 { RANGE_DNCP_RESERVED, "Reserved" },
110 { RANGE_HNCP_UNASSIGNED, "Unassigned" },
111 { RANGE_DNCP_PRIVATE_USE, "Private use" },
112 { RANGE_DNCP_FUTURE_USE, "Future use" },
113
114 { 0, NULL}
115 };
116
117 #define DH4OPT_DNS_SERVERS 6 /* RFC2132 */
118 #define DH4OPT_NTP_SERVERS 42 /* RFC2132 */
119 #define DH4OPT_DOMAIN_SEARCH 119 /* RFC3397 */
120
121 static const struct tok dh4opt_str[] = {
122 { DH4OPT_DNS_SERVERS, "DNS-server" },
123 { DH4OPT_NTP_SERVERS, "NTP-server"},
124 { DH4OPT_DOMAIN_SEARCH, "DNS-search" },
125 { 0, NULL }
126 };
127
128 #define DH6OPT_DNS_SERVERS 23 /* RFC3646 */
129 #define DH6OPT_DOMAIN_LIST 24 /* RFC3646 */
130 #define DH6OPT_SNTP_SERVERS 31 /* RFC4075 */
131
132 static const struct tok dh6opt_str[] = {
133 { DH6OPT_DNS_SERVERS, "DNS-server" },
134 { DH6OPT_DOMAIN_LIST, "DNS-search-list" },
135 { DH6OPT_SNTP_SERVERS, "SNTP-servers" },
136 { 0, NULL }
137 };
138
139 /*
140 * For IPv4-mapped IPv6 addresses, length of the prefix that precedes
141 * the 4 bytes of IPv4 address at the end of the IPv6 address.
142 */
143 #define IPV4_MAPPED_HEADING_LEN 12
144
145 /*
146 * Is an IPv6 address an IPv4-mapped address?
147 */
148 static inline int
149 is_ipv4_mapped_address(const u_char *addr)
150 {
151 /* The value of the prefix */
152 static const u_char ipv4_mapped_heading[IPV4_MAPPED_HEADING_LEN] =
153 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
154
155 return memcmp(addr, ipv4_mapped_heading, IPV4_MAPPED_HEADING_LEN) == 0;
156 }
157
158 static const char *
159 format_nid(const u_char *data)
160 {
161 static char buf[4][sizeof("01:01:01:01")];
162 static int i = 0;
163 i = (i + 1) % 4;
164 snprintf(buf[i], sizeof(buf[i]), "%02x:%02x:%02x:%02x",
165 EXTRACT_U_1(data), EXTRACT_U_1(data + 1), EXTRACT_U_1(data + 2),
166 EXTRACT_U_1(data + 3));
167 return buf[i];
168 }
169
170 static const char *
171 format_256(const u_char *data)
172 {
173 static char buf[4][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")];
174 static int i = 0;
175 i = (i + 1) % 4;
176 snprintf(buf[i], sizeof(buf[i]), "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64,
177 EXTRACT_BE_U_8(data),
178 EXTRACT_BE_U_8(data + 8),
179 EXTRACT_BE_U_8(data + 16),
180 EXTRACT_BE_U_8(data + 24)
181 );
182 return buf[i];
183 }
184
185 static const char *
186 format_interval(const uint32_t n)
187 {
188 static char buf[4][sizeof("0000000.000s")];
189 static int i = 0;
190 i = (i + 1) % 4;
191 snprintf(buf[i], sizeof(buf[i]), "%u.%03us", n / 1000, n % 1000);
192 return buf[i];
193 }
194
195 static const char *
196 format_ip6addr(netdissect_options *ndo, const u_char *cp)
197 {
198 if (is_ipv4_mapped_address(cp))
199 return ipaddr_string(ndo, cp + IPV4_MAPPED_HEADING_LEN);
200 else
201 return ip6addr_string(ndo, cp);
202 }
203
204 static int
205 print_prefix(netdissect_options *ndo, const u_char *prefix, u_int max_length)
206 {
207 int plenbytes;
208 char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::/128")];
209
210 if (EXTRACT_U_1(prefix) >= 96 && max_length >= IPV4_MAPPED_HEADING_LEN + 1 &&
211 is_ipv4_mapped_address(prefix + 1)) {
212 struct in_addr addr;
213 u_int plen;
214
215 plen = prefix[0]-96;
216 if (32 < plen)
217 return -1;
218 max_length -= 1;
219
220 memset(&addr, 0, sizeof(addr));
221 plenbytes = (plen + 7) / 8;
222 if (max_length < (u_int)plenbytes + IPV4_MAPPED_HEADING_LEN)
223 return -3;
224 memcpy(&addr, prefix + IPV4_MAPPED_HEADING_LEN + 1, plenbytes);
225 if (plen % 8) {
226 ((u_char *)&addr)[plenbytes - 1] &=
227 ((0xff00 >> (plen % 8)) & 0xff);
228 }
229 snprintf(buf, sizeof(buf), "%s/%d", ipaddr_string(ndo, &addr), plen);
230 plenbytes += 1 + IPV4_MAPPED_HEADING_LEN;
231 } else {
232 plenbytes = decode_prefix6(ndo, prefix, max_length, buf, sizeof(buf));
233 }
234
235 ND_PRINT((ndo, "%s", buf));
236 return plenbytes;
237 }
238
239 static int
240 print_dns_label(netdissect_options *ndo,
241 const u_char *cp, u_int max_length, int print)
242 {
243 u_int length = 0;
244 while (length < max_length) {
245 u_int lab_length = cp[length++];
246 if (lab_length == 0)
247 return (int)length;
248 if (length > 1 && print)
249 safeputchar(ndo, '.');
250 if (length+lab_length > max_length) {
251 if (print)
252 safeputs(ndo, cp+length, max_length-length);
253 break;
254 }
255 if (print)
256 safeputs(ndo, cp+length, lab_length);
257 length += lab_length;
258 }
259 if (print)
260 ND_PRINT((ndo, "[|DNS]"));
261 return -1;
262 }
263
264 static int
265 dhcpv4_print(netdissect_options *ndo,
266 const u_char *cp, u_int length, int indent)
267 {
268 u_int i, t;
269 const uint8_t *tlv, *value;
270 uint8_t type, optlen;
271
272 i = 0;
273 while (i < length) {
274 if (i + 2 > length)
275 return -1;
276 tlv = cp + i;
277 type = tlv[0];
278 optlen = tlv[1];
279 value = tlv + 2;
280
281 ND_PRINT((ndo, "\n"));
282 for (t = indent; t > 0; t--)
283 ND_PRINT((ndo, "\t"));
284
285 ND_PRINT((ndo, "%s", tok2str(dh4opt_str, "Unknown", type)));
286 ND_PRINT((ndo," (%u)", optlen + 2 ));
287 if (i + 2 + optlen > length)
288 return -1;
289
290 switch (type) {
291 case DH4OPT_DNS_SERVERS:
292 case DH4OPT_NTP_SERVERS: {
293 if (optlen < 4 || optlen % 4 != 0) {
294 return -1;
295 }
296 for (t = 0; t < optlen; t += 4)
297 ND_PRINT((ndo, " %s", ipaddr_string(ndo, value + t)));
298 }
299 break;
300 case DH4OPT_DOMAIN_SEARCH: {
301 const u_char *tp = value;
302 while (tp < value + optlen) {
303 ND_PRINT((ndo, " "));
304 if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL)
305 return -1;
306 }
307 }
308 break;
309 }
310
311 i += 2 + optlen;
312 }
313 return 0;
314 }
315
316 static int
317 dhcpv6_print(netdissect_options *ndo,
318 const u_char *cp, u_int length, int indent)
319 {
320 u_int i, t;
321 const u_char *tlv, *value;
322 uint16_t type, optlen;
323
324 i = 0;
325 while (i < length) {
326 if (i + 4 > length)
327 return -1;
328 tlv = cp + i;
329 type = EXTRACT_BE_U_2(tlv);
330 optlen = EXTRACT_BE_U_2(tlv + 2);
331 value = tlv + 4;
332
333 ND_PRINT((ndo, "\n"));
334 for (t = indent; t > 0; t--)
335 ND_PRINT((ndo, "\t"));
336
337 ND_PRINT((ndo, "%s", tok2str(dh6opt_str, "Unknown", type)));
338 ND_PRINT((ndo," (%u)", optlen + 4 ));
339 if (i + 4 + optlen > length)
340 return -1;
341
342 switch (type) {
343 case DH6OPT_DNS_SERVERS:
344 case DH6OPT_SNTP_SERVERS: {
345 if (optlen % 16 != 0) {
346 ND_PRINT((ndo, " %s", istr));
347 return -1;
348 }
349 for (t = 0; t < optlen; t += 16)
350 ND_PRINT((ndo, " %s", ip6addr_string(ndo, value + t)));
351 }
352 break;
353 case DH6OPT_DOMAIN_LIST: {
354 const u_char *tp = value;
355 while (tp < value + optlen) {
356 ND_PRINT((ndo, " "));
357 if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL)
358 return -1;
359 }
360 }
361 break;
362 }
363
364 i += 4 + optlen;
365 }
366 return 0;
367 }
368
369 /* Determine in-line mode */
370 static int
371 is_in_line(netdissect_options *ndo, int indent)
372 {
373 return indent - 1 >= ndo->ndo_vflag && ndo->ndo_vflag < 3;
374 }
375
376 static void
377 print_type_in_line(netdissect_options *ndo,
378 uint32_t type, int count, int indent, int *first_one)
379 {
380 if (count > 0) {
381 if (*first_one) {
382 *first_one = 0;
383 if (indent > 1) {
384 u_int t;
385 ND_PRINT((ndo, "\n"));
386 for (t = indent; t > 0; t--)
387 ND_PRINT((ndo, "\t"));
388 } else {
389 ND_PRINT((ndo, " "));
390 }
391 } else {
392 ND_PRINT((ndo, ", "));
393 }
394 ND_PRINT((ndo, "%s", tok2str(type_values, "Easter Egg", type)));
395 if (count > 1)
396 ND_PRINT((ndo, " (x%d)", count));
397 }
398 }
399
400 static void
401 hncp_print_rec(netdissect_options *ndo,
402 const u_char *cp, u_int length, int indent)
403 {
404 const int in_line = is_in_line(ndo, indent);
405 int first_one = 1;
406
407 u_int i, t;
408
409 uint32_t last_type_mask = 0xffffffffU;
410 int last_type_count = -1;
411
412 const uint8_t *tlv, *value;
413 uint16_t type, bodylen;
414 uint32_t type_mask;
415
416 i = 0;
417 while (i < length) {
418 tlv = cp + i;
419
420 if (!in_line) {
421 ND_PRINT((ndo, "\n"));
422 for (t = indent; t > 0; t--)
423 ND_PRINT((ndo, "\t"));
424 }
425
426 ND_TCHECK_4(tlv);
427 if (i + 4 > length)
428 goto invalid;
429
430 type = EXTRACT_BE_U_2(tlv);
431 bodylen = EXTRACT_BE_U_2(tlv + 2);
432 value = tlv + 4;
433 ND_TCHECK2(*value, bodylen);
434 if (i + bodylen + 4 > length)
435 goto invalid;
436
437 type_mask =
438 (type == 0) ? RANGE_DNCP_RESERVED:
439 (44 <= type && type <= 511) ? RANGE_HNCP_UNASSIGNED:
440 (768 <= type && type <= 1023) ? RANGE_DNCP_PRIVATE_USE:
441 RANGE_DNCP_FUTURE_USE;
442 if (type == 6 || type == 7)
443 type_mask = RANGE_DNCP_FUTURE_USE;
444
445 /* defined types */
446 {
447 t = 0;
448 while (1) {
449 u_int key = type_values[t++].v;
450 if (key > 0xffff)
451 break;
452 if (key == type) {
453 type_mask = type;
454 break;
455 }
456 }
457 }
458
459 if (in_line) {
460 if (last_type_mask == type_mask) {
461 last_type_count++;
462 } else {
463 print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
464 last_type_mask = type_mask;
465 last_type_count = 1;
466 }
467
468 goto skip_multiline;
469 }
470
471 ND_PRINT((ndo,"%s", tok2str(type_values, "Easter Egg (42)", type_mask) ));
472 if (type_mask > 0xffff)
473 ND_PRINT((ndo,": type=%u", type ));
474 ND_PRINT((ndo," (%u)", bodylen + 4 ));
475
476 switch (type_mask) {
477
478 case DNCP_REQUEST_NETWORK_STATE: {
479 if (bodylen != 0)
480 ND_PRINT((ndo, " %s", istr));
481 }
482 break;
483
484 case DNCP_REQUEST_NODE_STATE: {
485 const char *node_identifier;
486 if (bodylen != 4) {
487 ND_PRINT((ndo, " %s", istr));
488 break;
489 }
490 node_identifier = format_nid(value);
491 ND_PRINT((ndo, " NID: %s", node_identifier));
492 }
493 break;
494
495 case DNCP_NODE_ENDPOINT: {
496 const char *node_identifier;
497 uint32_t endpoint_identifier;
498 if (bodylen != 8) {
499 ND_PRINT((ndo, " %s", istr));
500 break;
501 }
502 node_identifier = format_nid(value);
503 endpoint_identifier = EXTRACT_BE_U_4(value + 4);
504 ND_PRINT((ndo, " NID: %s EPID: %08x",
505 node_identifier,
506 endpoint_identifier
507 ));
508 }
509 break;
510
511 case DNCP_NETWORK_STATE: {
512 uint64_t hash;
513 if (bodylen != 8) {
514 ND_PRINT((ndo, " %s", istr));
515 break;
516 }
517 hash = EXTRACT_BE_U_8(value);
518 ND_PRINT((ndo, " hash: %016" PRIx64, hash));
519 }
520 break;
521
522 case DNCP_NODE_STATE: {
523 const char *node_identifier, *interval;
524 uint32_t sequence_number;
525 uint64_t hash;
526 if (bodylen < 20) {
527 ND_PRINT((ndo, " %s", istr));
528 break;
529 }
530 node_identifier = format_nid(value);
531 sequence_number = EXTRACT_BE_U_4(value + 4);
532 interval = format_interval(EXTRACT_BE_U_4(value + 8));
533 hash = EXTRACT_BE_U_8(value + 12);
534 ND_PRINT((ndo, " NID: %s seqno: %u %s hash: %016" PRIx64,
535 node_identifier,
536 sequence_number,
537 interval,
538 hash
539 ));
540 hncp_print_rec(ndo, value+20, bodylen-20, indent+1);
541 }
542 break;
543
544 case DNCP_PEER: {
545 const char *peer_node_identifier;
546 uint32_t peer_endpoint_identifier, endpoint_identifier;
547 if (bodylen != 12) {
548 ND_PRINT((ndo, " %s", istr));
549 break;
550 }
551 peer_node_identifier = format_nid(value);
552 peer_endpoint_identifier = EXTRACT_BE_U_4(value + 4);
553 endpoint_identifier = EXTRACT_BE_U_4(value + 8);
554 ND_PRINT((ndo, " Peer-NID: %s Peer-EPID: %08x Local-EPID: %08x",
555 peer_node_identifier,
556 peer_endpoint_identifier,
557 endpoint_identifier
558 ));
559 }
560 break;
561
562 case DNCP_KEEP_ALIVE_INTERVAL: {
563 uint32_t endpoint_identifier;
564 const char *interval;
565 if (bodylen < 8) {
566 ND_PRINT((ndo, " %s", istr));
567 break;
568 }
569 endpoint_identifier = EXTRACT_BE_U_4(value);
570 interval = format_interval(EXTRACT_BE_U_4(value + 4));
571 ND_PRINT((ndo, " EPID: %08x Interval: %s",
572 endpoint_identifier,
573 interval
574 ));
575 }
576 break;
577
578 case DNCP_TRUST_VERDICT: {
579 if (bodylen <= 36) {
580 ND_PRINT((ndo, " %s", istr));
581 break;
582 }
583 ND_PRINT((ndo, " Verdict: %u Fingerprint: %s Common Name: ",
584 EXTRACT_U_1(value),
585 format_256(value + 4)));
586 safeputs(ndo, value + 36, bodylen - 36);
587 }
588 break;
589
590 case HNCP_HNCP_VERSION: {
591 uint16_t capabilities;
592 uint8_t M, P, H, L;
593 if (bodylen < 5) {
594 ND_PRINT((ndo, " %s", istr));
595 break;
596 }
597 capabilities = EXTRACT_BE_U_2(value + 2);
598 M = (uint8_t)((capabilities >> 12) & 0xf);
599 P = (uint8_t)((capabilities >> 8) & 0xf);
600 H = (uint8_t)((capabilities >> 4) & 0xf);
601 L = (uint8_t)(capabilities & 0xf);
602 ND_PRINT((ndo, " M: %u P: %u H: %u L: %u User-agent: ",
603 M, P, H, L
604 ));
605 safeputs(ndo, value + 4, bodylen - 4);
606 }
607 break;
608
609 case HNCP_EXTERNAL_CONNECTION: {
610 /* Container TLV */
611 hncp_print_rec(ndo, value, bodylen, indent+1);
612 }
613 break;
614
615 case HNCP_DELEGATED_PREFIX: {
616 int l;
617 if (bodylen < 9 || bodylen < 9 + (EXTRACT_U_1(value + 8) + 7) / 8) {
618 ND_PRINT((ndo, " %s", istr));
619 break;
620 }
621 ND_PRINT((ndo, " VLSO: %s PLSO: %s Prefix: ",
622 format_interval(EXTRACT_BE_U_4(value)),
623 format_interval(EXTRACT_BE_U_4(value + 4))
624 ));
625 l = print_prefix(ndo, value + 8, bodylen - 8);
626 if (l == -1) {
627 ND_PRINT((ndo, "(length is invalid)"));
628 break;
629 }
630 if (l < 0) {
631 /*
632 * We've already checked that we've captured the
633 * entire TLV, based on its length, so this will
634 * either be -1, meaning "the prefix length is
635 * greater than the longest possible address of
636 * that type" (i.e., > 32 for IPv4 or > 128 for
637 * IPv6", or -3, meaning "the prefix runs past
638 * the end of the TLV".
639 */
640 ND_PRINT((ndo, " %s", istr));
641 break;
642 }
643 l += 8 + (-l & 3);
644
645 if (bodylen >= l)
646 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
647 }
648 break;
649
650 case HNCP_PREFIX_POLICY: {
651 uint8_t policy;
652 int l;
653 if (bodylen < 1) {
654 ND_PRINT((ndo, " %s", istr));
655 break;
656 }
657 policy = value[0];
658 ND_PRINT((ndo, " type: "));
659 if (policy == 0) {
660 if (bodylen != 1) {
661 ND_PRINT((ndo, " %s", istr));
662 break;
663 }
664 ND_PRINT((ndo, "Internet connectivity"));
665 } else if (policy >= 1 && policy <= 128) {
666 ND_PRINT((ndo, "Dest-Prefix: "));
667 l = print_prefix(ndo, value, bodylen);
668 if (l == -1) {
669 ND_PRINT((ndo, "(length is invalid)"));
670 break;
671 }
672 if (l < 0) {
673 /*
674 * We've already checked that we've captured the
675 * entire TLV, based on its length, so this will
676 * either be -1, meaning "the prefix length is
677 * greater than the longest possible address of
678 * that type" (i.e., > 32 for IPv4 or > 128 for
679 * IPv6", or -3, meaning "the prefix runs past
680 * the end of the TLV".
681 */
682 ND_PRINT((ndo, " %s", istr));
683 break;
684 }
685 } else if (policy == 129) {
686 ND_PRINT((ndo, "DNS domain: "));
687 print_dns_label(ndo, value+1, bodylen-1, 1);
688 } else if (policy == 130) {
689 ND_PRINT((ndo, "Opaque UTF-8: "));
690 safeputs(ndo, value + 1, bodylen - 1);
691 } else if (policy == 131) {
692 if (bodylen != 1) {
693 ND_PRINT((ndo, " %s", istr));
694 break;
695 }
696 ND_PRINT((ndo, "Restrictive assignment"));
697 } else if (policy >= 132) {
698 ND_PRINT((ndo, "Unknown (%u)", policy)); /* Reserved for future additions */
699 }
700 }
701 break;
702
703 case HNCP_DHCPV4_DATA: {
704 if (bodylen == 0) {
705 ND_PRINT((ndo, " %s", istr));
706 break;
707 }
708 if (dhcpv4_print(ndo, value, bodylen, indent+1) != 0)
709 goto invalid;
710 }
711 break;
712
713 case HNCP_DHCPV6_DATA: {
714 if (bodylen == 0) {
715 ND_PRINT((ndo, " %s", istr));
716 break;
717 }
718 if (dhcpv6_print(ndo, value, bodylen, indent+1) != 0) {
719 ND_PRINT((ndo, " %s", istr));
720 break;
721 }
722 }
723 break;
724
725 case HNCP_ASSIGNED_PREFIX: {
726 uint8_t prty;
727 int l;
728 if (bodylen < 6 || bodylen < 6 + (EXTRACT_U_1(value + 5) + 7) / 8) {
729 ND_PRINT((ndo, " %s", istr));
730 break;
731 }
732 prty = value[4] & 0xf;
733 ND_PRINT((ndo, " EPID: %08x Prty: %u",
734 EXTRACT_BE_U_4(value),
735 prty
736 ));
737 ND_PRINT((ndo, " Prefix: "));
738 if ((l = print_prefix(ndo, value + 5, bodylen - 5)) < 0) {
739 ND_PRINT((ndo, " %s", istr));
740 break;
741 }
742 l += 5;
743 l += -l & 3;
744
745 if (bodylen >= l)
746 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
747 }
748 break;
749
750 case HNCP_NODE_ADDRESS: {
751 uint32_t endpoint_identifier;
752 const char *ip_address;
753 if (bodylen < 20) {
754 ND_PRINT((ndo, " %s", istr));
755 break;
756 }
757 endpoint_identifier = EXTRACT_BE_U_4(value);
758 ip_address = format_ip6addr(ndo, value + 4);
759 ND_PRINT((ndo, " EPID: %08x IP Address: %s",
760 endpoint_identifier,
761 ip_address
762 ));
763
764 hncp_print_rec(ndo, value + 20, bodylen - 20, indent+1);
765 }
766 break;
767
768 case HNCP_DNS_DELEGATED_ZONE: {
769 const char *ip_address;
770 int len;
771 if (bodylen < 17) {
772 ND_PRINT((ndo, " %s", istr));
773 break;
774 }
775 ip_address = format_ip6addr(ndo, value);
776 ND_PRINT((ndo, " IP-Address: %s %c%c%c ",
777 ip_address,
778 (EXTRACT_U_1(value + 16) & 4) ? 'l' : '-',
779 (EXTRACT_U_1(value + 16) & 2) ? 'b' : '-',
780 (EXTRACT_U_1(value + 16) & 1) ? 's' : '-'
781 ));
782 len = print_dns_label(ndo, value+17, bodylen-17, 1);
783 if (len < 0) {
784 ND_PRINT((ndo, " %s", istr));
785 break;
786 }
787 len += 17;
788 len += -len & 3;
789 if (bodylen >= len)
790 hncp_print_rec(ndo, value+len, bodylen-len, indent+1);
791 }
792 break;
793
794 case HNCP_DOMAIN_NAME: {
795 if (bodylen == 0) {
796 ND_PRINT((ndo, " %s", istr));
797 break;
798 }
799 ND_PRINT((ndo, " Domain: "));
800 print_dns_label(ndo, value, bodylen, 1);
801 }
802 break;
803
804 case HNCP_NODE_NAME: {
805 u_int l;
806 if (bodylen < 17) {
807 ND_PRINT((ndo, " %s", istr));
808 break;
809 }
810 l = value[16];
811 if (bodylen < 17 + l) {
812 ND_PRINT((ndo, " %s", istr));
813 break;
814 }
815 ND_PRINT((ndo, " IP-Address: %s Name: ",
816 format_ip6addr(ndo, value)
817 ));
818 if (l < 64) {
819 safeputchar(ndo, '"');
820 safeputs(ndo, value + 17, l);
821 safeputchar(ndo, '"');
822 } else {
823 ND_PRINT((ndo, "%s", istr));
824 }
825 l += 17;
826 l += -l & 3;
827 if (bodylen >= l)
828 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
829 }
830 break;
831
832 case HNCP_MANAGED_PSK: {
833 if (bodylen < 32) {
834 ND_PRINT((ndo, " %s", istr));
835 break;
836 }
837 ND_PRINT((ndo, " PSK: %s", format_256(value)));
838 hncp_print_rec(ndo, value + 32, bodylen - 32, indent+1);
839 }
840 break;
841
842 case RANGE_DNCP_RESERVED:
843 case RANGE_HNCP_UNASSIGNED:
844 case RANGE_DNCP_PRIVATE_USE:
845 case RANGE_DNCP_FUTURE_USE:
846 break;
847
848 }
849 skip_multiline:
850
851 i += 4 + bodylen + (-bodylen & 3);
852 }
853 print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
854
855 return;
856
857 trunc:
858 ND_PRINT((ndo, "%s", "[|hncp]"));
859 return;
860
861 invalid:
862 ND_PRINT((ndo, "%s", istr));
863 return;
864 }