]> The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
OpenFlow: Have a function for each message type.
[tcpdump] / print-domain.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 /* \summary: Domain Name System (DNS) printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "addrtostr.h"
35 #include "extract.h"
36
37 #include "nameser.h"
38
39 static const char *ns_ops[] = {
40 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
41 " op8", " updateA", " updateD", " updateDA",
42 " updateM", " updateMA", " zoneInit", " zoneRef",
43 };
44
45 static const char *ns_resp[] = {
46 "", " FormErr", " ServFail", " NXDomain",
47 " NotImp", " Refused", " YXDomain", " YXRRSet",
48 " NXRRSet", " NotAuth", " NotZone", " Resp11",
49 " Resp12", " Resp13", " Resp14", " NoChange",
50 " BadVers", "Resp17", " Resp18", " Resp19",
51 " Resp20", "Resp21", " Resp22", " BadCookie",
52 };
53
54 static const char *
55 ns_rcode(u_int rcode) {
56 static char buf[sizeof(" Resp4095")];
57
58 if (rcode < sizeof(ns_resp)/sizeof(ns_resp[0])) {
59 return (ns_resp[rcode]);
60 }
61 snprintf(buf, sizeof(buf), " Resp%u", rcode & 0xfff);
62 return (buf);
63 }
64
65 /* skip over a domain name */
66 static const u_char *
67 ns_nskip(netdissect_options *ndo,
68 const u_char *cp)
69 {
70 u_char i;
71
72 if (!ND_TTEST_1(cp))
73 return (NULL);
74 i = GET_U_1(cp);
75 cp++;
76 while (i) {
77 if ((i & INDIR_MASK) == INDIR_MASK)
78 return (cp + 1);
79 if ((i & INDIR_MASK) == EDNS0_MASK) {
80 int bitlen, bytelen;
81
82 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
83 return(NULL); /* unknown ELT */
84 if (!ND_TTEST_1(cp))
85 return (NULL);
86 if ((bitlen = GET_U_1(cp)) == 0)
87 bitlen = 256;
88 cp++;
89 bytelen = (bitlen + 7) / 8;
90 cp += bytelen;
91 } else
92 cp += i;
93 if (!ND_TTEST_1(cp))
94 return (NULL);
95 i = GET_U_1(cp);
96 cp++;
97 }
98 return (cp);
99 }
100
101 static const u_char *
102 blabel_print(netdissect_options *ndo,
103 const u_char *cp)
104 {
105 u_int bitlen, slen, b;
106 const u_char *bitp, *lim;
107 uint8_t tc;
108
109 if (!ND_TTEST_1(cp))
110 return(NULL);
111 if ((bitlen = GET_U_1(cp)) == 0)
112 bitlen = 256;
113 slen = (bitlen + 3) / 4;
114 lim = cp + 1 + slen;
115
116 /* print the bit string as a hex string */
117 ND_PRINT("\\[x");
118 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
119 ND_PRINT("%02x", GET_U_1(bitp));
120 }
121 if (b > 4) {
122 tc = GET_U_1(bitp);
123 bitp++;
124 ND_PRINT("%02x", tc & (0xff << (8 - b)));
125 } else if (b > 0) {
126 tc = GET_U_1(bitp);
127 bitp++;
128 ND_PRINT("%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b)));
129 }
130 ND_PRINT("/%u]", bitlen);
131 return lim;
132 }
133
134 static int
135 labellen(netdissect_options *ndo,
136 const u_char *cp)
137 {
138 u_int i;
139
140 if (!ND_TTEST_1(cp))
141 return(-1);
142 i = GET_U_1(cp);
143 if ((i & INDIR_MASK) == EDNS0_MASK) {
144 u_int bitlen, elt;
145 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
146 ND_PRINT("<ELT %d>", elt);
147 return(-1);
148 }
149 if (!ND_TTEST_1(cp + 1))
150 return(-1);
151 if ((bitlen = GET_U_1(cp + 1)) == 0)
152 bitlen = 256;
153 return(((bitlen + 7) / 8) + 1);
154 } else
155 return(i);
156 }
157
158 /* print a <domain-name> */
159 const u_char *
160 fqdn_print(netdissect_options *ndo,
161 const u_char *cp, const u_char *bp)
162 {
163 u_int i, l;
164 const u_char *rp = NULL;
165 int compress = 0;
166 u_int elt;
167 u_int offset, max_offset;
168
169 if ((l = labellen(ndo, cp)) == (u_int)-1)
170 return(NULL);
171 if (!ND_TTEST_1(cp))
172 return(NULL);
173 max_offset = (u_int)(cp - bp);
174 i = GET_U_1(cp);
175 cp++;
176 if ((i & INDIR_MASK) != INDIR_MASK) {
177 compress = 0;
178 rp = cp + l;
179 }
180
181 if (i != 0)
182 while (i && cp < ndo->ndo_snapend) {
183 if ((i & INDIR_MASK) == INDIR_MASK) {
184 if (!compress) {
185 rp = cp + 1;
186 compress = 1;
187 }
188 if (!ND_TTEST_1(cp))
189 return(NULL);
190 offset = (((i << 8) | GET_U_1(cp)) & 0x3fff);
191 /*
192 * This must move backwards in the packet.
193 * No RFC explicitly says that, but BIND's
194 * name decompression code requires it,
195 * as a way of preventing infinite loops
196 * and other bad behavior, and it's probably
197 * what was intended (compress by pointing
198 * to domain name suffixes already seen in
199 * the packet).
200 */
201 if (offset >= max_offset) {
202 ND_PRINT("<BAD PTR>");
203 return(NULL);
204 }
205 max_offset = offset;
206 cp = bp + offset;
207 if ((l = labellen(ndo, cp)) == (u_int)-1)
208 return(NULL);
209 if (!ND_TTEST_1(cp))
210 return(NULL);
211 i = GET_U_1(cp);
212 cp++;
213 continue;
214 }
215 if ((i & INDIR_MASK) == EDNS0_MASK) {
216 elt = (i & ~INDIR_MASK);
217 switch(elt) {
218 case EDNS0_ELT_BITLABEL:
219 if (blabel_print(ndo, cp) == NULL)
220 return (NULL);
221 break;
222 default:
223 /* unknown ELT */
224 ND_PRINT("<ELT %u>", elt);
225 return(NULL);
226 }
227 } else {
228 if (nd_printn(ndo, cp, l, ndo->ndo_snapend))
229 return(NULL);
230 }
231
232 cp += l;
233 ND_PRINT(".");
234 if ((l = labellen(ndo, cp)) == (u_int)-1)
235 return(NULL);
236 if (!ND_TTEST_1(cp))
237 return(NULL);
238 i = GET_U_1(cp);
239 cp++;
240 if (!compress)
241 rp += l + 1;
242 }
243 else
244 ND_PRINT(".");
245 return (rp);
246 }
247
248 /* print a <character-string> */
249 static const u_char *
250 ns_cprint(netdissect_options *ndo,
251 const u_char *cp)
252 {
253 u_int i;
254
255 if (!ND_TTEST_1(cp))
256 return (NULL);
257 i = GET_U_1(cp);
258 cp++;
259 if (nd_printn(ndo, cp, i, ndo->ndo_snapend))
260 return (NULL);
261 return (cp + i);
262 }
263
264 static void
265 print_eopt_ecs(netdissect_options *ndo, const u_char *cp,
266 u_int data_len)
267 {
268 u_int family, addr_bits, src_len, scope_len;
269
270 u_char padded[32];
271 char addr[INET6_ADDRSTRLEN];
272
273 /* ecs option must at least contain family, src len, and scope len */
274 if (data_len < 4) {
275 nd_print_invalid(ndo);
276 return;
277 }
278
279 family = GET_BE_U_2(cp);
280 cp += 2;
281 src_len = GET_U_1(cp);
282 cp += 1;
283 scope_len = GET_U_1(cp);
284 cp += 1;
285
286 if (family == 1)
287 addr_bits = 32;
288 else if (family == 2)
289 addr_bits = 128;
290 else {
291 nd_print_invalid(ndo);
292 return;
293 }
294
295 if (data_len - 4 > (addr_bits / 8)) {
296 nd_print_invalid(ndo);
297 return;
298 }
299 /* checks for invalid ecs scope or source length */
300 if (src_len > addr_bits || scope_len > addr_bits || ((src_len + 7) / 8) != (data_len - 4)) {
301 nd_print_invalid(ndo);
302 return;
303 }
304
305 /* pad the truncated address from ecs with zeros */
306 memset(padded, 0, sizeof(padded));
307 memcpy(padded, cp, data_len - 4);
308
309
310 if (family == 1)
311 ND_PRINT("%s/%d/%d", addrtostr(padded, addr, INET_ADDRSTRLEN),
312 src_len, scope_len);
313 else
314 ND_PRINT("%s/%d/%d", addrtostr6(padded, addr, INET6_ADDRSTRLEN),
315 src_len, scope_len);
316
317 }
318
319 extern const struct tok edns_opt2str[];
320 extern const struct tok dau_alg2str[];
321 extern const struct tok dhu_alg2str[];
322 extern const struct tok n3u_alg2str[];
323
324
325 /* print an <EDNS-option> */
326 static const u_char *
327 eopt_print(netdissect_options *ndo,
328 const u_char *cp)
329 {
330 u_int opt, data_len, i;
331
332 if (!ND_TTEST_2(cp))
333 return (NULL);
334 opt = GET_BE_U_2(cp);
335 cp += 2;
336 ND_PRINT("%s", tok2str(edns_opt2str, "Opt%u", opt));
337 if (!ND_TTEST_2(cp))
338 return (NULL);
339 data_len = GET_BE_U_2(cp);
340 cp += 2;
341
342 ND_TCHECK_LEN(cp, data_len);
343
344 if (data_len > 0) {
345 ND_PRINT(" ");
346 switch (opt) {
347
348 case E_ECS:
349 print_eopt_ecs(ndo, cp, data_len);
350 break;
351 case E_COOKIE:
352 if (data_len < 8 || (data_len > 8 && data_len < 16) || data_len > 40)
353 nd_print_invalid(ndo);
354 else {
355 for (i = 0; i < data_len; ++i) {
356 /* split client and server cookie */
357 if (i == 8)
358 ND_PRINT(" ");
359 ND_PRINT("%02x", GET_U_1(cp + i));
360 }
361 }
362 break;
363 case E_KEEPALIVE:
364 if (data_len != 2)
365 nd_print_invalid(ndo);
366 else
367 /* keepalive is in increments of 100ms. Convert to seconds */
368 ND_PRINT("%0.1f sec", (GET_BE_U_2(cp) / 10.0));
369 break;
370 case E_EXPIRE:
371 if (data_len != 4)
372 nd_print_invalid(ndo);
373 else
374 ND_PRINT("%u sec", GET_BE_U_4(cp));
375 break;
376 case E_PADDING:
377 /* ignore contents and just print length */
378 ND_PRINT("(%u)", data_len);
379 break;
380 case E_KEYTAG:
381 if (data_len % 2 != 0)
382 nd_print_invalid(ndo);
383 else
384 for (i = 0; i < data_len; i += 2) {
385 if (i > 0)
386 ND_PRINT(" ");
387 ND_PRINT("%u", GET_BE_U_2(cp + i));
388 }
389 break;
390 case E_DAU:
391 for (i = 0; i < data_len; ++i) {
392 if (i > 0)
393 ND_PRINT(" ");
394 ND_PRINT("%s", tok2str(dau_alg2str, "Alg_%u", GET_U_1(cp + i)));
395 }
396 break;
397 case E_DHU:
398 for (i = 0; i < data_len; ++i) {
399 if (i > 0)
400 ND_PRINT(" ");
401 ND_PRINT("%s", tok2str(dhu_alg2str, "Alg_%u", GET_U_1(cp + i)));
402 }
403 break;
404 case E_N3U:
405 for (i = 0; i < data_len; ++i) {
406 if (i > 0)
407 ND_PRINT(" ");
408 ND_PRINT("%s", tok2str(n3u_alg2str, "Alg_%u", GET_U_1(cp + i)));
409 }
410 break;
411 case E_CHAIN:
412 fqdn_print(ndo, cp, cp + data_len);
413 break;
414 case E_NSID:
415 /* intentional fall-through. NSID is an undefined byte string */
416 default:
417 for (i = 0; i < data_len; ++i)
418 ND_PRINT("%02x", GET_U_1(cp + i));
419 break;
420 }
421 }
422 return (cp + data_len);
423
424 trunc:
425 return (NULL);
426
427 }
428
429
430
431 extern const struct tok ns_type2str[];
432
433 /* https://www.iana.org/assignments/dns-parameters */
434 const struct tok ns_type2str[] = {
435 { T_A, "A" }, /* RFC 1035 */
436 { T_NS, "NS" }, /* RFC 1035 */
437 { T_MD, "MD" }, /* RFC 1035 */
438 { T_MF, "MF" }, /* RFC 1035 */
439 { T_CNAME, "CNAME" }, /* RFC 1035 */
440 { T_SOA, "SOA" }, /* RFC 1035 */
441 { T_MB, "MB" }, /* RFC 1035 */
442 { T_MG, "MG" }, /* RFC 1035 */
443 { T_MR, "MR" }, /* RFC 1035 */
444 { T_NULL, "NULL" }, /* RFC 1035 */
445 { T_WKS, "WKS" }, /* RFC 1035 */
446 { T_PTR, "PTR" }, /* RFC 1035 */
447 { T_HINFO, "HINFO" }, /* RFC 1035 */
448 { T_MINFO, "MINFO" }, /* RFC 1035 */
449 { T_MX, "MX" }, /* RFC 1035 */
450 { T_TXT, "TXT" }, /* RFC 1035 */
451 { T_RP, "RP" }, /* RFC 1183 */
452 { T_AFSDB, "AFSDB" }, /* RFC 1183 */
453 { T_X25, "X25" }, /* RFC 1183 */
454 { T_ISDN, "ISDN" }, /* RFC 1183 */
455 { T_RT, "RT" }, /* RFC 1183 */
456 { T_NSAP, "NSAP" }, /* RFC 1706 */
457 { T_NSAP_PTR, "NSAP_PTR" },
458 { T_SIG, "SIG" }, /* RFC 2535 */
459 { T_KEY, "KEY" }, /* RFC 2535 */
460 { T_PX, "PX" }, /* RFC 2163 */
461 { T_GPOS, "GPOS" }, /* RFC 1712 */
462 { T_AAAA, "AAAA" }, /* RFC 1886 */
463 { T_LOC, "LOC" }, /* RFC 1876 */
464 { T_NXT, "NXT" }, /* RFC 2535 */
465 { T_EID, "EID" }, /* Nimrod */
466 { T_NIMLOC, "NIMLOC" }, /* Nimrod */
467 { T_SRV, "SRV" }, /* RFC 2782 */
468 { T_ATMA, "ATMA" }, /* ATM Forum */
469 { T_NAPTR, "NAPTR" }, /* RFC 2168, RFC 2915 */
470 { T_KX, "KX" }, /* RFC 2230 */
471 { T_CERT, "CERT" }, /* RFC 2538 */
472 { T_A6, "A6" }, /* RFC 2874 */
473 { T_DNAME, "DNAME" }, /* RFC 2672 */
474 { T_SINK, "SINK" },
475 { T_OPT, "OPT" }, /* RFC 2671 */
476 { T_APL, "APL" }, /* RFC 3123 */
477 { T_DS, "DS" }, /* RFC 4034 */
478 { T_SSHFP, "SSHFP" }, /* RFC 4255 */
479 { T_IPSECKEY, "IPSECKEY" }, /* RFC 4025 */
480 { T_RRSIG, "RRSIG" }, /* RFC 4034 */
481 { T_NSEC, "NSEC" }, /* RFC 4034 */
482 { T_DNSKEY, "DNSKEY" }, /* RFC 4034 */
483 { T_SPF, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
484 { T_UINFO, "UINFO" },
485 { T_UID, "UID" },
486 { T_GID, "GID" },
487 { T_UNSPEC, "UNSPEC" },
488 { T_UNSPECA, "UNSPECA" },
489 { T_TKEY, "TKEY" }, /* RFC 2930 */
490 { T_TSIG, "TSIG" }, /* RFC 2845 */
491 { T_IXFR, "IXFR" }, /* RFC 1995 */
492 { T_AXFR, "AXFR" }, /* RFC 1035 */
493 { T_MAILB, "MAILB" }, /* RFC 1035 */
494 { T_MAILA, "MAILA" }, /* RFC 1035 */
495 { T_ANY, "ANY" },
496 { T_URI, "URI" }, /* RFC 7553 */
497 { 0, NULL }
498 };
499
500 extern const struct tok ns_class2str[];
501
502 const struct tok ns_class2str[] = {
503 { C_IN, "IN" }, /* Not used */
504 { C_CHAOS, "CHAOS" },
505 { C_HS, "HS" },
506 { C_ANY, "ANY" },
507 { 0, NULL }
508 };
509
510 const struct tok edns_opt2str[] = {
511 { E_LLQ, "LLQ" },
512 { E_UL, "UL" },
513 { E_NSID, "NSID" },
514 { E_DAU, "DAU" },
515 { E_DHU, "DHU" },
516 { E_N3U, "N3U" },
517 { E_ECS, "ECS" },
518 { E_EXPIRE, "EXPIRE" },
519 { E_COOKIE, "COOKIE" },
520 { E_KEEPALIVE, "KEEPALIVE" },
521 { E_PADDING, "PADDING" },
522 { E_CHAIN, "CHAIN" },
523 { E_KEYTAG, "KEY-TAG" },
524 { E_CLIENTTAG, "CLIENT-TAG" },
525 { E_SERVERTAG, "SERVER-TAG" },
526 { 0, NULL }
527 };
528
529 const struct tok dau_alg2str[] = {
530 { A_DELETE, "DELETE" },
531 { A_RSAMD5, "RSAMD5" },
532 { A_DH, "DH" },
533 { A_DSA, "DS" },
534 { A_RSASHA1, "RSASHA1" },
535 { A_DSA_NSEC3_SHA1, "DSA-NSEC3-SHA1" },
536 { A_RSASHA1_NSEC3_SHA1, "RSASHA1-NSEC3-SHA1" },
537 { A_RSASHA256, "RSASHA256" },
538 { A_RSASHA512, "RSASHA512" },
539 { A_ECC_GOST, "ECC-GOST" },
540 { A_ECDSAP256SHA256, "ECDSAP256SHA256" },
541 { A_ECDSAP384SHA384, "ECDSAP384SHA384" },
542 { A_ED25519, "ED25519" },
543 { A_ED448, "ED448" },
544 { A_INDIRECT, "INDIRECT" },
545 { A_PRIVATEDNS, "PRIVATEDNS" },
546 { A_PRIVATEOID, "PRIVATEOID" },
547 { 0, NULL }
548 };
549
550 const struct tok dhu_alg2str[] = {
551 { DS_SHA1, "SHA-1" },
552 { DS_SHA256,"SHA-256" },
553 { DS_GOST, "GOST_R_34.11-94" },
554 { DS_SHA384,"SHA-384" },
555 { 0, NULL }
556 };
557
558 const struct tok n3u_alg2str[] = {
559 { NSEC_SHA1,"SHA-1" },
560 { 0, NULL }
561 };
562
563 /* print a query */
564 static const u_char *
565 ns_qprint(netdissect_options *ndo,
566 const u_char *cp, const u_char *bp, int is_mdns)
567 {
568 const u_char *np = cp;
569 u_int i, class;
570
571 cp = ns_nskip(ndo, cp);
572
573 if (cp == NULL || !ND_TTEST_4(cp))
574 return(NULL);
575
576 /* print the qtype */
577 i = GET_BE_U_2(cp);
578 cp += 2;
579 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u", i));
580 /* print the qclass (if it's not IN) */
581 i = GET_BE_U_2(cp);
582 cp += 2;
583 if (is_mdns)
584 class = (i & ~C_QU);
585 else
586 class = i;
587 if (class != C_IN)
588 ND_PRINT(" %s", tok2str(ns_class2str, "(Class %u)", class));
589 if (is_mdns) {
590 ND_PRINT(i & C_QU ? " (QU)" : " (QM)");
591 }
592
593 ND_PRINT("? ");
594 cp = fqdn_print(ndo, np, bp);
595 return(cp ? cp + 4 : NULL);
596 }
597
598 /* print a reply */
599 static const u_char *
600 ns_rprint(netdissect_options *ndo,
601 const u_char *cp, const u_char *bp, int is_mdns)
602 {
603 u_int i, class, opt_flags = 0;
604 u_short typ, len;
605 const u_char *rp;
606
607 if (ndo->ndo_vflag) {
608 ND_PRINT(" ");
609 if ((cp = fqdn_print(ndo, cp, bp)) == NULL)
610 return NULL;
611 } else
612 cp = ns_nskip(ndo, cp);
613
614 if (cp == NULL || !ND_TTEST_LEN(cp, 10))
615 return (ndo->ndo_snapend);
616
617 /* print the type/qtype */
618 typ = GET_BE_U_2(cp);
619 cp += 2;
620 /* print the class (if it's not IN and the type isn't OPT) */
621 i = GET_BE_U_2(cp);
622 cp += 2;
623 if (is_mdns)
624 class = (i & ~C_CACHE_FLUSH);
625 else
626 class = i;
627 if (class != C_IN && typ != T_OPT)
628 ND_PRINT(" %s", tok2str(ns_class2str, "(Class %u)", class));
629 if (is_mdns) {
630 if (i & C_CACHE_FLUSH)
631 ND_PRINT(" (Cache flush)");
632 }
633
634 if (typ == T_OPT) {
635 /* get opt flags */
636 cp += 2;
637 opt_flags = GET_BE_U_2(cp);
638 /* ignore rest of ttl field */
639 cp += 2;
640 } else if (ndo->ndo_vflag > 2) {
641 /* print ttl */
642 ND_PRINT(" [");
643 unsigned_relts_print(ndo, GET_BE_U_4(cp));
644 ND_PRINT("]");
645 cp += 4;
646 } else {
647 /* ignore ttl */
648 cp += 4;
649 }
650
651 len = GET_BE_U_2(cp);
652 cp += 2;
653
654 rp = cp + len;
655
656 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u", typ));
657 if (rp > ndo->ndo_snapend)
658 return(NULL);
659
660 switch (typ) {
661 case T_A:
662 if (!ND_TTEST_LEN(cp, sizeof(nd_ipv4)))
663 return(NULL);
664 ND_PRINT(" %s", intoa(GET_IPV4_TO_NETWORK_ORDER(cp)));
665 break;
666
667 case T_NS:
668 case T_CNAME:
669 case T_PTR:
670 case T_DNAME:
671 ND_PRINT(" ");
672 if (fqdn_print(ndo, cp, bp) == NULL)
673 return(NULL);
674 break;
675
676 case T_SOA:
677 if (!ndo->ndo_vflag)
678 break;
679 ND_PRINT(" ");
680 if ((cp = fqdn_print(ndo, cp, bp)) == NULL)
681 return(NULL);
682 ND_PRINT(" ");
683 if ((cp = fqdn_print(ndo, cp, bp)) == NULL)
684 return(NULL);
685 if (!ND_TTEST_LEN(cp, 5 * 4))
686 return(NULL);
687 ND_PRINT(" %u", GET_BE_U_4(cp));
688 cp += 4;
689 ND_PRINT(" %u", GET_BE_U_4(cp));
690 cp += 4;
691 ND_PRINT(" %u", GET_BE_U_4(cp));
692 cp += 4;
693 ND_PRINT(" %u", GET_BE_U_4(cp));
694 cp += 4;
695 ND_PRINT(" %u", GET_BE_U_4(cp));
696 cp += 4;
697 break;
698 case T_MX:
699 ND_PRINT(" ");
700 if (!ND_TTEST_2(cp))
701 return(NULL);
702 if (fqdn_print(ndo, cp + 2, bp) == NULL)
703 return(NULL);
704 ND_PRINT(" %u", GET_BE_U_2(cp));
705 break;
706
707 case T_TXT:
708 while (cp < rp) {
709 ND_PRINT(" \"");
710 cp = ns_cprint(ndo, cp);
711 if (cp == NULL)
712 return(NULL);
713 ND_PRINT("\"");
714 }
715 break;
716
717 case T_SRV:
718 ND_PRINT(" ");
719 if (!ND_TTEST_6(cp))
720 return(NULL);
721 if (fqdn_print(ndo, cp + 6, bp) == NULL)
722 return(NULL);
723 ND_PRINT(":%u %u %u", GET_BE_U_2(cp + 4),
724 GET_BE_U_2(cp), GET_BE_U_2(cp + 2));
725 break;
726
727 case T_AAAA:
728 {
729 char ntop_buf[INET6_ADDRSTRLEN];
730
731 if (!ND_TTEST_LEN(cp, sizeof(nd_ipv6)))
732 return(NULL);
733 ND_PRINT(" %s",
734 addrtostr6(cp, ntop_buf, sizeof(ntop_buf)));
735
736 break;
737 }
738
739 case T_A6:
740 {
741 nd_ipv6 a;
742 int pbit, pbyte;
743 char ntop_buf[INET6_ADDRSTRLEN];
744
745 if (!ND_TTEST_1(cp))
746 return(NULL);
747 pbit = GET_U_1(cp);
748 pbyte = (pbit & ~7) / 8;
749 if (pbit > 128) {
750 ND_PRINT(" %u(bad plen)", pbit);
751 break;
752 } else if (pbit < 128) {
753 if (!ND_TTEST_LEN(cp + 1, sizeof(a) - pbyte))
754 return(NULL);
755 memset(a, 0, sizeof(a));
756 memcpy(a + pbyte, cp + 1, sizeof(a) - pbyte);
757 ND_PRINT(" %u %s", pbit,
758 addrtostr6(&a, ntop_buf, sizeof(ntop_buf)));
759 }
760 if (pbit > 0) {
761 ND_PRINT(" ");
762 if (fqdn_print(ndo, cp + 1 + sizeof(a) - pbyte, bp) == NULL)
763 return(NULL);
764 }
765 break;
766 }
767
768 case T_URI:
769 if (!ND_TTEST_LEN(cp, len))
770 return(NULL);
771 ND_PRINT(" %u %u ", GET_BE_U_2(cp), GET_BE_U_2(cp + 2));
772 if (nd_printn(ndo, cp + 4, len - 4, ndo->ndo_snapend))
773 return(NULL);
774 break;
775
776 case T_OPT:
777 ND_PRINT(" UDPsize=%u", class);
778 if (opt_flags & 0x8000)
779 ND_PRINT(" DO");
780 if (cp < rp) {
781 ND_PRINT(" [");
782 while (cp < rp) {
783 cp = eopt_print(ndo, cp);
784 if (cp == NULL)
785 return(NULL);
786 if (cp < rp)
787 ND_PRINT(",");
788 }
789 ND_PRINT("]");
790 }
791 break;
792
793 case T_UNSPECA: /* One long string */
794 if (!ND_TTEST_LEN(cp, len))
795 return(NULL);
796 if (nd_printn(ndo, cp, len, ndo->ndo_snapend))
797 return(NULL);
798 break;
799
800 case T_TSIG:
801 {
802 if (cp + len > ndo->ndo_snapend)
803 return(NULL);
804 if (!ndo->ndo_vflag)
805 break;
806 ND_PRINT(" ");
807 if ((cp = fqdn_print(ndo, cp, bp)) == NULL)
808 return(NULL);
809 cp += 6;
810 if (!ND_TTEST_2(cp))
811 return(NULL);
812 ND_PRINT(" fudge=%u", GET_BE_U_2(cp));
813 cp += 2;
814 if (!ND_TTEST_2(cp))
815 return(NULL);
816 ND_PRINT(" maclen=%u", GET_BE_U_2(cp));
817 cp += 2 + GET_BE_U_2(cp);
818 if (!ND_TTEST_2(cp))
819 return(NULL);
820 ND_PRINT(" origid=%u", GET_BE_U_2(cp));
821 cp += 2;
822 if (!ND_TTEST_2(cp))
823 return(NULL);
824 ND_PRINT(" error=%u", GET_BE_U_2(cp));
825 cp += 2;
826 if (!ND_TTEST_2(cp))
827 return(NULL);
828 ND_PRINT(" otherlen=%u", GET_BE_U_2(cp));
829 cp += 2;
830 }
831 }
832 return (rp); /* XXX This isn't always right */
833 }
834
835 void
836 domain_print(netdissect_options *ndo,
837 const u_char *bp, u_int length, int over_tcp, int is_mdns)
838 {
839 const dns_header_t *np;
840 uint16_t flags, rcode, rdlen, type;
841 u_int qdcount, ancount, nscount, arcount;
842 u_int i;
843 const u_char *cp;
844 uint16_t b2;
845
846 ndo->ndo_protocol = "domain";
847
848 if (over_tcp) {
849 /*
850 * The message is prefixed with a two byte length field
851 * which gives the message length, excluding the two byte
852 * length field. (RFC 1035 - 4.2.2. TCP usage)
853 */
854 if (length < 2) {
855 ND_PRINT(" [DNS over TCP: length %u < 2]", length);
856 nd_print_invalid(ndo);
857 return;
858 } else {
859 length -= 2; /* excluding the two byte length field */
860 if (GET_BE_U_2(bp) != length) {
861 ND_PRINT(" [prefix length(%u) != length(%u)]",
862 GET_BE_U_2(bp), length);
863 nd_print_invalid(ndo);
864 return;
865 } else {
866 bp += 2;
867 /* in over TCP case, we need to prepend a space
868 * (not needed in over UDP case)
869 */
870 ND_PRINT(" ");
871 }
872 }
873 }
874
875 np = (const dns_header_t *)bp;
876
877 if(length < sizeof(*np)) {
878 nd_print_protocol(ndo);
879 ND_PRINT(" [length %u < %zu]", length, sizeof(*np));
880 nd_print_invalid(ndo);
881 return;
882 }
883
884 ND_TCHECK_SIZE(np);
885 flags = GET_BE_U_2(np->flags);
886 /* get the byte-order right */
887 qdcount = GET_BE_U_2(np->qdcount);
888 ancount = GET_BE_U_2(np->ancount);
889 nscount = GET_BE_U_2(np->nscount);
890 arcount = GET_BE_U_2(np->arcount);
891
892 /* find the opt record to extract extended rcode */
893 cp = (const u_char *)(np + 1);
894 rcode = DNS_RCODE(flags);
895 for (i = 0; i < qdcount; i++) {
896 if ((cp = ns_nskip(ndo, cp)) == NULL)
897 goto print;
898 cp += 4; /* skip QTYPE and QCLASS */
899 if (cp >= ndo->ndo_snapend)
900 goto print;
901 }
902 for (i = 0; i < ancount + nscount; i++) {
903 if ((cp = ns_nskip(ndo, cp)) == NULL)
904 goto print;
905 cp += 8; /* skip TYPE, CLASS and TTL */
906 if (cp + 2 > ndo->ndo_snapend)
907 goto print;
908 rdlen = GET_BE_U_2(cp);
909 cp += 2 + rdlen;
910 if (cp >= ndo->ndo_snapend)
911 goto print;
912 }
913 for (i = 0; i < arcount; i++) {
914 if ((cp = ns_nskip(ndo, cp)) == NULL)
915 goto print;
916 if (cp + 2 > ndo->ndo_snapend)
917 goto print;
918 type = GET_BE_U_2(cp);
919 cp += 4; /* skip TYPE and CLASS */
920 if (cp + 1 > ndo->ndo_snapend)
921 goto print;
922 if (type == T_OPT) {
923 rcode |= (GET_U_1(cp) << 4);
924 goto print;
925 }
926 cp += 4;
927 if (cp + 2 > ndo->ndo_snapend)
928 goto print;
929 rdlen = GET_BE_U_2(cp);
930 cp += 2 + rdlen;
931 if (cp >= ndo->ndo_snapend)
932 goto print;
933 }
934
935 print:
936 if (DNS_QR(flags)) {
937 /* this is a response */
938 ND_PRINT("%u%s%s%s%s%s%s",
939 GET_BE_U_2(np->id),
940 ns_ops[DNS_OPCODE(flags)],
941 ns_rcode(rcode),
942 DNS_AA(flags)? "*" : "",
943 DNS_RA(flags)? "" : "-",
944 DNS_TC(flags)? "|" : "",
945 DNS_AD(flags)? "$" : "");
946
947 if (qdcount != 1)
948 ND_PRINT(" [%uq]", qdcount);
949 /* Print QUESTION section on -vv */
950 cp = (const u_char *)(np + 1);
951 for (i = 0; i < qdcount; i++) {
952 if (i != 0)
953 ND_PRINT(",");
954 if (ndo->ndo_vflag > 1) {
955 ND_PRINT(" q:");
956 if ((cp = ns_qprint(ndo, cp, bp, is_mdns)) == NULL)
957 goto trunc;
958 } else {
959 if ((cp = ns_nskip(ndo, cp)) == NULL)
960 goto trunc;
961 cp += 4; /* skip QTYPE and QCLASS */
962 }
963 }
964 ND_PRINT(" %u/%u/%u", ancount, nscount, arcount);
965 if (ancount) {
966 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
967 goto trunc;
968 ancount--;
969 while (cp < ndo->ndo_snapend && ancount) {
970 ND_PRINT(",");
971 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
972 goto trunc;
973 ancount--;
974 }
975 }
976 if (ancount)
977 goto trunc;
978 /* Print NS and AR sections on -vv */
979 if (ndo->ndo_vflag > 1) {
980 if (cp < ndo->ndo_snapend && nscount) {
981 ND_PRINT(" ns:");
982 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
983 goto trunc;
984 nscount--;
985 while (cp < ndo->ndo_snapend && nscount) {
986 ND_PRINT(",");
987 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
988 goto trunc;
989 nscount--;
990 }
991 }
992 if (nscount)
993 goto trunc;
994 if (cp < ndo->ndo_snapend && arcount) {
995 ND_PRINT(" ar:");
996 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
997 goto trunc;
998 arcount--;
999 while (cp < ndo->ndo_snapend && arcount) {
1000 ND_PRINT(",");
1001 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1002 goto trunc;
1003 arcount--;
1004 }
1005 }
1006 if (arcount)
1007 goto trunc;
1008 }
1009 }
1010 else {
1011 /* this is a request */
1012 ND_PRINT("%u%s%s%s", GET_BE_U_2(np->id),
1013 ns_ops[DNS_OPCODE(flags)],
1014 DNS_RD(flags) ? "+" : "",
1015 DNS_CD(flags) ? "%" : "");
1016
1017 /* any weirdness? */
1018 b2 = GET_BE_U_2(((const u_short *)np) + 1);
1019 if (b2 & 0x6cf)
1020 ND_PRINT(" [b2&3=0x%x]", b2);
1021
1022 if (DNS_OPCODE(flags) == IQUERY) {
1023 if (qdcount)
1024 ND_PRINT(" [%uq]", qdcount);
1025 if (ancount != 1)
1026 ND_PRINT(" [%ua]", ancount);
1027 }
1028 else {
1029 if (ancount)
1030 ND_PRINT(" [%ua]", ancount);
1031 if (qdcount != 1)
1032 ND_PRINT(" [%uq]", qdcount);
1033 }
1034 if (nscount)
1035 ND_PRINT(" [%un]", nscount);
1036 if (arcount)
1037 ND_PRINT(" [%uau]", arcount);
1038
1039 cp = (const u_char *)(np + 1);
1040 if (qdcount) {
1041 cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
1042 if (!cp)
1043 goto trunc;
1044 qdcount--;
1045 while (cp < ndo->ndo_snapend && qdcount) {
1046 cp = ns_qprint(ndo, (const u_char *)cp,
1047 (const u_char *)np,
1048 is_mdns);
1049 if (!cp)
1050 goto trunc;
1051 qdcount--;
1052 }
1053 }
1054 if (qdcount)
1055 goto trunc;
1056
1057 /* Print remaining sections on -vv */
1058 if (ndo->ndo_vflag > 1) {
1059 if (ancount) {
1060 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1061 goto trunc;
1062 ancount--;
1063 while (cp < ndo->ndo_snapend && ancount) {
1064 ND_PRINT(",");
1065 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1066 goto trunc;
1067 ancount--;
1068 }
1069 }
1070 if (ancount)
1071 goto trunc;
1072 if (cp < ndo->ndo_snapend && nscount) {
1073 ND_PRINT(" ns:");
1074 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1075 goto trunc;
1076 nscount--;
1077 while (cp < ndo->ndo_snapend && nscount) {
1078 ND_PRINT(",");
1079 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1080 goto trunc;
1081 nscount--;
1082 }
1083 }
1084 if (nscount > 0)
1085 goto trunc;
1086 if (cp < ndo->ndo_snapend && arcount) {
1087 ND_PRINT(" ar:");
1088 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1089 goto trunc;
1090 arcount--;
1091 while (cp < ndo->ndo_snapend && arcount) {
1092 ND_PRINT(",");
1093 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
1094 goto trunc;
1095 arcount--;
1096 }
1097 }
1098 if (arcount)
1099 goto trunc;
1100 }
1101 }
1102 ND_PRINT(" (%u)", length);
1103 return;
1104
1105 trunc:
1106 nd_print_trunc(ndo);
1107 }