2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
22 /* \summary: Domain Name System (DNS) printer */
28 #include <netdissect-stdinc.h>
34 #include "netdissect.h"
35 #include "addrtoname.h"
36 #include "addrtostr.h"
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",
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",
52 /* skip over a domain name */
54 ns_nskip(netdissect_options *ndo,
55 register const u_char *cp)
59 if (!ND_TTEST2(*cp, 1))
63 if ((i & INDIR_MASK) == INDIR_MASK)
65 if ((i & INDIR_MASK) == EDNS0_MASK) {
68 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
69 return(NULL); /* unknown ELT */
70 if (!ND_TTEST2(*cp, 1))
72 if ((bitlen = *cp++) == 0)
74 bytelen = (bitlen + 7) / 8;
78 if (!ND_TTEST2(*cp, 1))
85 /* print a <domain-name> */
87 blabel_print(netdissect_options *ndo,
91 const u_char *bitp, *lim;
94 if (!ND_TTEST2(*cp, 1))
96 if ((bitlen = *cp) == 0)
98 slen = (bitlen + 3) / 4;
101 /* print the bit string as a hex string */
102 ND_PRINT((ndo, "\\[x"));
103 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
105 ND_PRINT((ndo, "%02x", *bitp));
110 ND_PRINT((ndo, "%02x", tc & (0xff << (8 - b))));
114 ND_PRINT((ndo, "%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
116 ND_PRINT((ndo, "/%d]", bitlen));
119 ND_PRINT((ndo, ".../%d]", bitlen));
124 labellen(netdissect_options *ndo,
129 if (!ND_TTEST2(*cp, 1))
132 if ((i & INDIR_MASK) == EDNS0_MASK) {
134 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
135 ND_PRINT((ndo, "<ELT %d>", elt));
138 if (!ND_TTEST2(*(cp + 1), 1))
140 if ((bitlen = *(cp + 1)) == 0)
142 return(((bitlen + 7) / 8) + 1);
148 ns_nprint(netdissect_options *ndo,
149 register const u_char *cp, register const u_char *bp)
152 register const u_char *rp = NULL;
153 register int compress = 0;
155 u_int offset, max_offset;
157 if ((l = labellen(ndo, cp)) == (u_int)-1)
159 if (!ND_TTEST2(*cp, 1))
161 max_offset = (u_int)(cp - bp);
162 if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
168 while (i && cp < ndo->ndo_snapend) {
169 if ((i & INDIR_MASK) == INDIR_MASK) {
174 if (!ND_TTEST2(*cp, 1))
176 offset = (((i << 8) | *cp) & 0x3fff);
178 * This must move backwards in the packet.
179 * No RFC explicitly says that, but BIND's
180 * name decompression code requires it,
181 * as a way of preventing infinite loops
182 * and other bad behavior, and it's probably
183 * what was intended (compress by pointing
184 * to domain name suffixes already seen in
187 if (offset >= max_offset) {
188 ND_PRINT((ndo, "<BAD PTR>"));
193 if ((l = labellen(ndo, cp)) == (u_int)-1)
195 if (!ND_TTEST2(*cp, 1))
200 if ((i & INDIR_MASK) == EDNS0_MASK) {
201 elt = (i & ~INDIR_MASK);
203 case EDNS0_ELT_BITLABEL:
204 if (blabel_print(ndo, cp) == NULL)
209 ND_PRINT((ndo, "<ELT %d>", elt));
213 if (fn_printn(ndo, cp, l, ndo->ndo_snapend))
218 ND_PRINT((ndo, "."));
219 if ((l = labellen(ndo, cp)) == (u_int)-1)
221 if (!ND_TTEST2(*cp, 1))
228 ND_PRINT((ndo, "."));
232 /* print a <character-string> */
233 static const u_char *
234 ns_cprint(netdissect_options *ndo,
235 register const u_char *cp)
239 if (!ND_TTEST2(*cp, 1))
242 if (fn_printn(ndo, cp, i, ndo->ndo_snapend))
247 /* http://www.iana.org/assignments/dns-parameters */
248 const struct tok ns_type2str[] = {
249 { T_A, "A" }, /* RFC 1035 */
250 { T_NS, "NS" }, /* RFC 1035 */
251 { T_MD, "MD" }, /* RFC 1035 */
252 { T_MF, "MF" }, /* RFC 1035 */
253 { T_CNAME, "CNAME" }, /* RFC 1035 */
254 { T_SOA, "SOA" }, /* RFC 1035 */
255 { T_MB, "MB" }, /* RFC 1035 */
256 { T_MG, "MG" }, /* RFC 1035 */
257 { T_MR, "MR" }, /* RFC 1035 */
258 { T_NULL, "NULL" }, /* RFC 1035 */
259 { T_WKS, "WKS" }, /* RFC 1035 */
260 { T_PTR, "PTR" }, /* RFC 1035 */
261 { T_HINFO, "HINFO" }, /* RFC 1035 */
262 { T_MINFO, "MINFO" }, /* RFC 1035 */
263 { T_MX, "MX" }, /* RFC 1035 */
264 { T_TXT, "TXT" }, /* RFC 1035 */
265 { T_RP, "RP" }, /* RFC 1183 */
266 { T_AFSDB, "AFSDB" }, /* RFC 1183 */
267 { T_X25, "X25" }, /* RFC 1183 */
268 { T_ISDN, "ISDN" }, /* RFC 1183 */
269 { T_RT, "RT" }, /* RFC 1183 */
270 { T_NSAP, "NSAP" }, /* RFC 1706 */
271 { T_NSAP_PTR, "NSAP_PTR" },
272 { T_SIG, "SIG" }, /* RFC 2535 */
273 { T_KEY, "KEY" }, /* RFC 2535 */
274 { T_PX, "PX" }, /* RFC 2163 */
275 { T_GPOS, "GPOS" }, /* RFC 1712 */
276 { T_AAAA, "AAAA" }, /* RFC 1886 */
277 { T_LOC, "LOC" }, /* RFC 1876 */
278 { T_NXT, "NXT" }, /* RFC 2535 */
279 { T_EID, "EID" }, /* Nimrod */
280 { T_NIMLOC, "NIMLOC" }, /* Nimrod */
281 { T_SRV, "SRV" }, /* RFC 2782 */
282 { T_ATMA, "ATMA" }, /* ATM Forum */
283 { T_NAPTR, "NAPTR" }, /* RFC 2168, RFC 2915 */
284 { T_KX, "KX" }, /* RFC 2230 */
285 { T_CERT, "CERT" }, /* RFC 2538 */
286 { T_A6, "A6" }, /* RFC 2874 */
287 { T_DNAME, "DNAME" }, /* RFC 2672 */
289 { T_OPT, "OPT" }, /* RFC 2671 */
290 { T_APL, "APL" }, /* RFC 3123 */
291 { T_DS, "DS" }, /* RFC 4034 */
292 { T_SSHFP, "SSHFP" }, /* RFC 4255 */
293 { T_IPSECKEY, "IPSECKEY" }, /* RFC 4025 */
294 { T_RRSIG, "RRSIG" }, /* RFC 4034 */
295 { T_NSEC, "NSEC" }, /* RFC 4034 */
296 { T_DNSKEY, "DNSKEY" }, /* RFC 4034 */
297 { T_SPF, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
298 { T_UINFO, "UINFO" },
301 { T_UNSPEC, "UNSPEC" },
302 { T_UNSPECA, "UNSPECA" },
303 { T_TKEY, "TKEY" }, /* RFC 2930 */
304 { T_TSIG, "TSIG" }, /* RFC 2845 */
305 { T_IXFR, "IXFR" }, /* RFC 1995 */
306 { T_AXFR, "AXFR" }, /* RFC 1035 */
307 { T_MAILB, "MAILB" }, /* RFC 1035 */
308 { T_MAILA, "MAILA" }, /* RFC 1035 */
313 const struct tok ns_class2str[] = {
314 { C_IN, "IN" }, /* Not used */
315 { C_CHAOS, "CHAOS" },
322 static const u_char *
323 ns_qprint(netdissect_options *ndo,
324 register const u_char *cp, register const u_char *bp, int is_mdns)
326 register const u_char *np = cp;
327 register u_int i, class;
329 cp = ns_nskip(ndo, cp);
331 if (cp == NULL || !ND_TTEST2(*cp, 4))
334 /* print the qtype */
335 i = EXTRACT_16BITS(cp);
337 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", i)));
338 /* print the qclass (if it's not IN) */
339 i = EXTRACT_16BITS(cp);
346 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
348 ND_PRINT((ndo, i & C_QU ? " (QU)" : " (QM)"));
351 ND_PRINT((ndo, "? "));
352 cp = ns_nprint(ndo, np, bp);
353 return(cp ? cp + 4 : NULL);
357 static const u_char *
358 ns_rprint(netdissect_options *ndo,
359 register const u_char *cp, register const u_char *bp, int is_mdns)
361 register u_int i, class, opt_flags = 0;
362 register u_short typ, len;
363 register const u_char *rp;
365 if (ndo->ndo_vflag) {
366 ND_PRINT((ndo, " "));
367 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
370 cp = ns_nskip(ndo, cp);
372 if (cp == NULL || !ND_TTEST2(*cp, 10))
373 return (ndo->ndo_snapend);
375 /* print the type/qtype */
376 typ = EXTRACT_16BITS(cp);
378 /* print the class (if it's not IN and the type isn't OPT) */
379 i = EXTRACT_16BITS(cp);
382 class = (i & ~C_CACHE_FLUSH);
385 if (class != C_IN && typ != T_OPT)
386 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
388 if (i & C_CACHE_FLUSH)
389 ND_PRINT((ndo, " (Cache flush)"));
395 opt_flags = EXTRACT_16BITS(cp);
396 /* ignore rest of ttl field */
398 } else if (ndo->ndo_vflag > 2) {
400 ND_PRINT((ndo, " ["));
401 unsigned_relts_print(ndo, EXTRACT_32BITS(cp));
402 ND_PRINT((ndo, "]"));
409 len = EXTRACT_16BITS(cp);
414 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", typ)));
415 if (rp > ndo->ndo_snapend)
420 if (!ND_TTEST2(*cp, sizeof(struct in_addr)))
422 ND_PRINT((ndo, " %s", intoa(htonl(EXTRACT_32BITS(cp)))));
431 ND_PRINT((ndo, " "));
432 if (ns_nprint(ndo, cp, bp) == NULL)
439 ND_PRINT((ndo, " "));
440 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
442 ND_PRINT((ndo, " "));
443 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
445 if (!ND_TTEST2(*cp, 5 * 4))
447 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
449 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
451 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
453 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
455 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
459 ND_PRINT((ndo, " "));
460 if (!ND_TTEST2(*cp, 2))
462 if (ns_nprint(ndo, cp + 2, bp) == NULL)
464 ND_PRINT((ndo, " %d", EXTRACT_16BITS(cp)));
469 ND_PRINT((ndo, " \""));
470 cp = ns_cprint(ndo, cp);
473 ND_PRINT((ndo, "\""));
478 ND_PRINT((ndo, " "));
479 if (!ND_TTEST2(*cp, 6))
481 if (ns_nprint(ndo, cp + 6, bp) == NULL)
483 ND_PRINT((ndo, ":%d %d %d", EXTRACT_16BITS(cp + 4),
484 EXTRACT_16BITS(cp), EXTRACT_16BITS(cp + 2)));
489 char ntop_buf[INET6_ADDRSTRLEN];
491 if (!ND_TTEST2(*cp, sizeof(struct in6_addr)))
493 ND_PRINT((ndo, " %s",
494 addrtostr6(cp, ntop_buf, sizeof(ntop_buf))));
503 char ntop_buf[INET6_ADDRSTRLEN];
505 if (!ND_TTEST2(*cp, 1))
508 pbyte = (pbit & ~7) / 8;
510 ND_PRINT((ndo, " %u(bad plen)", pbit));
512 } else if (pbit < 128) {
513 if (!ND_TTEST2(*(cp + 1), sizeof(a) - pbyte))
515 memset(&a, 0, sizeof(a));
516 memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
517 ND_PRINT((ndo, " %u %s", pbit,
518 addrtostr6(&a, ntop_buf, sizeof(ntop_buf))));
521 ND_PRINT((ndo, " "));
522 if (ns_nprint(ndo, cp + 1 + sizeof(a) - pbyte, bp) == NULL)
529 ND_PRINT((ndo, " UDPsize=%u", class));
530 if (opt_flags & 0x8000)
531 ND_PRINT((ndo, " DO"));
534 case T_UNSPECA: /* One long string */
535 if (!ND_TTEST2(*cp, len))
537 if (fn_printn(ndo, cp, len, ndo->ndo_snapend))
543 if (cp + len > ndo->ndo_snapend)
547 ND_PRINT((ndo, " "));
548 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
551 if (!ND_TTEST2(*cp, 2))
553 ND_PRINT((ndo, " fudge=%u", EXTRACT_16BITS(cp)));
555 if (!ND_TTEST2(*cp, 2))
557 ND_PRINT((ndo, " maclen=%u", EXTRACT_16BITS(cp)));
558 cp += 2 + EXTRACT_16BITS(cp);
559 if (!ND_TTEST2(*cp, 2))
561 ND_PRINT((ndo, " origid=%u", EXTRACT_16BITS(cp)));
563 if (!ND_TTEST2(*cp, 2))
565 ND_PRINT((ndo, " error=%u", EXTRACT_16BITS(cp)));
567 if (!ND_TTEST2(*cp, 2))
569 ND_PRINT((ndo, " otherlen=%u", EXTRACT_16BITS(cp)));
573 return (rp); /* XXX This isn't always right */
577 ns_print(netdissect_options *ndo,
578 register const u_char *bp, u_int length, int is_mdns)
580 register const HEADER *np;
581 register int qdcount, ancount, nscount, arcount;
582 register const u_char *cp;
585 np = (const HEADER *)bp;
587 /* get the byte-order right */
588 qdcount = EXTRACT_16BITS(&np->qdcount);
589 ancount = EXTRACT_16BITS(&np->ancount);
590 nscount = EXTRACT_16BITS(&np->nscount);
591 arcount = EXTRACT_16BITS(&np->arcount);
594 /* this is a response */
595 ND_PRINT((ndo, "%d%s%s%s%s%s%s",
596 EXTRACT_16BITS(&np->id),
597 ns_ops[DNS_OPCODE(np)],
598 ns_resp[DNS_RCODE(np)],
599 DNS_AA(np)? "*" : "",
600 DNS_RA(np)? "" : "-",
601 DNS_TC(np)? "|" : "",
602 DNS_AD(np)? "$" : ""));
605 ND_PRINT((ndo, " [%dq]", qdcount));
606 /* Print QUESTION section on -vv */
607 cp = (const u_char *)(np + 1);
609 if (qdcount < EXTRACT_16BITS(&np->qdcount) - 1)
610 ND_PRINT((ndo, ","));
611 if (ndo->ndo_vflag > 1) {
612 ND_PRINT((ndo, " q:"));
613 if ((cp = ns_qprint(ndo, cp, bp, is_mdns)) == NULL)
616 if ((cp = ns_nskip(ndo, cp)) == NULL)
618 cp += 4; /* skip QTYPE and QCLASS */
621 ND_PRINT((ndo, " %d/%d/%d", ancount, nscount, arcount));
623 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
625 while (cp < ndo->ndo_snapend && ancount--) {
626 ND_PRINT((ndo, ","));
627 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
633 /* Print NS and AR sections on -vv */
634 if (ndo->ndo_vflag > 1) {
635 if (cp < ndo->ndo_snapend && nscount--) {
636 ND_PRINT((ndo, " ns:"));
637 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
639 while (cp < ndo->ndo_snapend && nscount--) {
640 ND_PRINT((ndo, ","));
641 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
647 if (cp < ndo->ndo_snapend && arcount--) {
648 ND_PRINT((ndo, " ar:"));
649 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
651 while (cp < ndo->ndo_snapend && arcount--) {
652 ND_PRINT((ndo, ","));
653 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
662 /* this is a request */
663 ND_PRINT((ndo, "%d%s%s%s", EXTRACT_16BITS(&np->id), ns_ops[DNS_OPCODE(np)],
664 DNS_RD(np) ? "+" : "",
665 DNS_CD(np) ? "%" : ""));
668 b2 = EXTRACT_16BITS(((const u_short *)np)+1);
670 ND_PRINT((ndo, " [b2&3=0x%x]", b2));
672 if (DNS_OPCODE(np) == IQUERY) {
674 ND_PRINT((ndo, " [%dq]", qdcount));
676 ND_PRINT((ndo, " [%da]", ancount));
680 ND_PRINT((ndo, " [%da]", ancount));
682 ND_PRINT((ndo, " [%dq]", qdcount));
685 ND_PRINT((ndo, " [%dn]", nscount));
687 ND_PRINT((ndo, " [%dau]", arcount));
689 cp = (const u_char *)(np + 1);
691 cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
694 while (cp < ndo->ndo_snapend && qdcount--) {
695 cp = ns_qprint(ndo, (const u_char *)cp,
705 /* Print remaining sections on -vv */
706 if (ndo->ndo_vflag > 1) {
708 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
710 while (cp < ndo->ndo_snapend && ancount--) {
711 ND_PRINT((ndo, ","));
712 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
718 if (cp < ndo->ndo_snapend && nscount--) {
719 ND_PRINT((ndo, " ns:"));
720 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
722 while (nscount-- && cp < ndo->ndo_snapend) {
723 ND_PRINT((ndo, ","));
724 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
730 if (cp < ndo->ndo_snapend && arcount--) {
731 ND_PRINT((ndo, " ar:"));
732 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
734 while (cp < ndo->ndo_snapend && arcount--) {
735 ND_PRINT((ndo, ","));
736 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
744 ND_PRINT((ndo, " (%d)", length));
748 ND_PRINT((ndo, "[|domain]"));