]>
The Tcpdump Group git mirrors - tcpdump/blob - print-domain.c
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"
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #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",
50 " BadVers", "Resp17", " Resp18", " Resp19",
51 " Resp20", "Resp21", " Resp22", " BadCookie",
55 ns_rcode(u_int rcode
) {
56 static char buf
[sizeof(" Resp4095")];
58 if (rcode
< sizeof(ns_resp
)/sizeof(ns_resp
[0])) {
59 return (ns_resp
[rcode
]);
61 snprintf(buf
, sizeof(buf
), " Resp%u", rcode
& 0xfff);
65 /* skip over a domain name */
67 ns_nskip(netdissect_options
*ndo
,
77 if ((i
& INDIR_MASK
) == INDIR_MASK
)
79 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
82 if ((i
& ~INDIR_MASK
) != EDNS0_ELT_BITLABEL
)
83 return(NULL
); /* unknown ELT */
86 if ((bitlen
= GET_U_1(cp
)) == 0)
89 bytelen
= (bitlen
+ 7) / 8;
101 static const u_char
*
102 blabel_print(netdissect_options
*ndo
,
105 u_int bitlen
, slen
, b
;
106 const u_char
*bitp
, *lim
;
111 if ((bitlen
= GET_U_1(cp
)) == 0)
113 slen
= (bitlen
+ 3) / 4;
116 /* print the bit string as a hex string */
118 for (bitp
= cp
+ 1, b
= bitlen
; bitp
< lim
&& b
> 7; b
-= 8, bitp
++) {
120 ND_PRINT("%02x", GET_U_1(bitp
));
126 ND_PRINT("%02x", tc
& (0xff << (8 - b
)));
131 ND_PRINT("%1x", ((tc
>> 4) & 0x0f) & (0x0f << (4 - b
)));
133 ND_PRINT("/%u]", bitlen
);
136 ND_PRINT(".../%u]", bitlen
);
141 labellen(netdissect_options
*ndo
,
149 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
151 if ((elt
= (i
& ~INDIR_MASK
)) != EDNS0_ELT_BITLABEL
) {
152 ND_PRINT("<ELT %d>", elt
);
155 if (!ND_TTEST_1(cp
+ 1))
157 if ((bitlen
= GET_U_1(cp
+ 1)) == 0)
159 return(((bitlen
+ 7) / 8) + 1);
164 /* print a <domain-name> */
166 fqdn_print(netdissect_options
*ndo
,
167 const u_char
*cp
, const u_char
*bp
)
170 const u_char
*rp
= NULL
;
173 u_int offset
, max_offset
;
175 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
179 max_offset
= (u_int
)(cp
- bp
);
182 if ((i
& INDIR_MASK
) != INDIR_MASK
) {
188 while (i
&& cp
< ndo
->ndo_snapend
) {
189 if ((i
& INDIR_MASK
) == INDIR_MASK
) {
196 offset
= (((i
<< 8) | GET_U_1(cp
)) & 0x3fff);
198 * This must move backwards in the packet.
199 * No RFC explicitly says that, but BIND's
200 * name decompression code requires it,
201 * as a way of preventing infinite loops
202 * and other bad behavior, and it's probably
203 * what was intended (compress by pointing
204 * to domain name suffixes already seen in
207 if (offset
>= max_offset
) {
208 ND_PRINT("<BAD PTR>");
213 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
221 if ((i
& INDIR_MASK
) == EDNS0_MASK
) {
222 elt
= (i
& ~INDIR_MASK
);
224 case EDNS0_ELT_BITLABEL
:
225 if (blabel_print(ndo
, cp
) == NULL
)
230 ND_PRINT("<ELT %u>", elt
);
234 if (nd_printn(ndo
, cp
, l
, ndo
->ndo_snapend
))
240 if ((l
= labellen(ndo
, cp
)) == (u_int
)-1)
254 /* print a <character-string> */
255 static const u_char
*
256 ns_cprint(netdissect_options
*ndo
,
265 if (nd_printn(ndo
, cp
, i
, ndo
->ndo_snapend
))
270 extern const struct tok ns_type2str
[];
272 /* http://www.iana.org/assignments/dns-parameters */
273 const struct tok ns_type2str
[] = {
274 { T_A
, "A" }, /* RFC 1035 */
275 { T_NS
, "NS" }, /* RFC 1035 */
276 { T_MD
, "MD" }, /* RFC 1035 */
277 { T_MF
, "MF" }, /* RFC 1035 */
278 { T_CNAME
, "CNAME" }, /* RFC 1035 */
279 { T_SOA
, "SOA" }, /* RFC 1035 */
280 { T_MB
, "MB" }, /* RFC 1035 */
281 { T_MG
, "MG" }, /* RFC 1035 */
282 { T_MR
, "MR" }, /* RFC 1035 */
283 { T_NULL
, "NULL" }, /* RFC 1035 */
284 { T_WKS
, "WKS" }, /* RFC 1035 */
285 { T_PTR
, "PTR" }, /* RFC 1035 */
286 { T_HINFO
, "HINFO" }, /* RFC 1035 */
287 { T_MINFO
, "MINFO" }, /* RFC 1035 */
288 { T_MX
, "MX" }, /* RFC 1035 */
289 { T_TXT
, "TXT" }, /* RFC 1035 */
290 { T_RP
, "RP" }, /* RFC 1183 */
291 { T_AFSDB
, "AFSDB" }, /* RFC 1183 */
292 { T_X25
, "X25" }, /* RFC 1183 */
293 { T_ISDN
, "ISDN" }, /* RFC 1183 */
294 { T_RT
, "RT" }, /* RFC 1183 */
295 { T_NSAP
, "NSAP" }, /* RFC 1706 */
296 { T_NSAP_PTR
, "NSAP_PTR" },
297 { T_SIG
, "SIG" }, /* RFC 2535 */
298 { T_KEY
, "KEY" }, /* RFC 2535 */
299 { T_PX
, "PX" }, /* RFC 2163 */
300 { T_GPOS
, "GPOS" }, /* RFC 1712 */
301 { T_AAAA
, "AAAA" }, /* RFC 1886 */
302 { T_LOC
, "LOC" }, /* RFC 1876 */
303 { T_NXT
, "NXT" }, /* RFC 2535 */
304 { T_EID
, "EID" }, /* Nimrod */
305 { T_NIMLOC
, "NIMLOC" }, /* Nimrod */
306 { T_SRV
, "SRV" }, /* RFC 2782 */
307 { T_ATMA
, "ATMA" }, /* ATM Forum */
308 { T_NAPTR
, "NAPTR" }, /* RFC 2168, RFC 2915 */
309 { T_KX
, "KX" }, /* RFC 2230 */
310 { T_CERT
, "CERT" }, /* RFC 2538 */
311 { T_A6
, "A6" }, /* RFC 2874 */
312 { T_DNAME
, "DNAME" }, /* RFC 2672 */
314 { T_OPT
, "OPT" }, /* RFC 2671 */
315 { T_APL
, "APL" }, /* RFC 3123 */
316 { T_DS
, "DS" }, /* RFC 4034 */
317 { T_SSHFP
, "SSHFP" }, /* RFC 4255 */
318 { T_IPSECKEY
, "IPSECKEY" }, /* RFC 4025 */
319 { T_RRSIG
, "RRSIG" }, /* RFC 4034 */
320 { T_NSEC
, "NSEC" }, /* RFC 4034 */
321 { T_DNSKEY
, "DNSKEY" }, /* RFC 4034 */
322 { T_SPF
, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
323 { T_UINFO
, "UINFO" },
326 { T_UNSPEC
, "UNSPEC" },
327 { T_UNSPECA
, "UNSPECA" },
328 { T_TKEY
, "TKEY" }, /* RFC 2930 */
329 { T_TSIG
, "TSIG" }, /* RFC 2845 */
330 { T_IXFR
, "IXFR" }, /* RFC 1995 */
331 { T_AXFR
, "AXFR" }, /* RFC 1035 */
332 { T_MAILB
, "MAILB" }, /* RFC 1035 */
333 { T_MAILA
, "MAILA" }, /* RFC 1035 */
335 { T_URI
, "URI" }, /* RFC 7553 */
339 extern const struct tok ns_class2str
[];
341 const struct tok ns_class2str
[] = {
342 { C_IN
, "IN" }, /* Not used */
343 { C_CHAOS
, "CHAOS" },
350 static const u_char
*
351 ns_qprint(netdissect_options
*ndo
,
352 const u_char
*cp
, const u_char
*bp
, int is_mdns
)
354 const u_char
*np
= cp
;
357 cp
= ns_nskip(ndo
, cp
);
359 if (cp
== NULL
|| !ND_TTEST_4(cp
))
362 /* print the qtype */
365 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u", i
));
366 /* print the qclass (if it's not IN) */
374 ND_PRINT(" %s", tok2str(ns_class2str
, "(Class %u)", class));
376 ND_PRINT(i
& C_QU
? " (QU)" : " (QM)");
380 cp
= fqdn_print(ndo
, np
, bp
);
381 return(cp
? cp
+ 4 : NULL
);
385 static const u_char
*
386 ns_rprint(netdissect_options
*ndo
,
387 const u_char
*cp
, const u_char
*bp
, int is_mdns
)
389 u_int i
, class, opt_flags
= 0;
393 if (ndo
->ndo_vflag
) {
395 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
398 cp
= ns_nskip(ndo
, cp
);
400 if (cp
== NULL
|| !ND_TTEST_LEN(cp
, 10))
401 return (ndo
->ndo_snapend
);
403 /* print the type/qtype */
404 typ
= GET_BE_U_2(cp
);
406 /* print the class (if it's not IN and the type isn't OPT) */
410 class = (i
& ~C_CACHE_FLUSH
);
413 if (class != C_IN
&& typ
!= T_OPT
)
414 ND_PRINT(" %s", tok2str(ns_class2str
, "(Class %u)", class));
416 if (i
& C_CACHE_FLUSH
)
417 ND_PRINT(" (Cache flush)");
423 opt_flags
= GET_BE_U_2(cp
);
424 /* ignore rest of ttl field */
426 } else if (ndo
->ndo_vflag
> 2) {
429 unsigned_relts_print(ndo
, GET_BE_U_4(cp
));
437 len
= GET_BE_U_2(cp
);
442 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u", typ
));
443 if (rp
> ndo
->ndo_snapend
)
448 if (!ND_TTEST_LEN(cp
, sizeof(nd_ipv4
)))
450 ND_PRINT(" %s", intoa(GET_IPV4_TO_NETWORK_ORDER(cp
)));
460 if (fqdn_print(ndo
, cp
, bp
) == NULL
)
468 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
471 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
473 if (!ND_TTEST_LEN(cp
, 5 * 4))
475 ND_PRINT(" %u", GET_BE_U_4(cp
));
477 ND_PRINT(" %u", GET_BE_U_4(cp
));
479 ND_PRINT(" %u", GET_BE_U_4(cp
));
481 ND_PRINT(" %u", GET_BE_U_4(cp
));
483 ND_PRINT(" %u", GET_BE_U_4(cp
));
490 if (fqdn_print(ndo
, cp
+ 2, bp
) == NULL
)
492 ND_PRINT(" %u", GET_BE_U_2(cp
));
498 cp
= ns_cprint(ndo
, cp
);
509 if (fqdn_print(ndo
, cp
+ 6, bp
) == NULL
)
511 ND_PRINT(":%u %u %u", GET_BE_U_2(cp
+ 4),
512 GET_BE_U_2(cp
), GET_BE_U_2(cp
+ 2));
517 char ntop_buf
[INET6_ADDRSTRLEN
];
519 if (!ND_TTEST_LEN(cp
, sizeof(nd_ipv6
)))
522 addrtostr6(cp
, ntop_buf
, sizeof(ntop_buf
)));
531 char ntop_buf
[INET6_ADDRSTRLEN
];
536 pbyte
= (pbit
& ~7) / 8;
538 ND_PRINT(" %u(bad plen)", pbit
);
540 } else if (pbit
< 128) {
541 if (!ND_TTEST_LEN(cp
+ 1, sizeof(a
) - pbyte
))
543 memset(&a
, 0, sizeof(a
));
544 memcpy(&a
.s6_addr
[pbyte
], cp
+ 1, sizeof(a
) - pbyte
);
545 ND_PRINT(" %u %s", pbit
,
546 addrtostr6(&a
, ntop_buf
, sizeof(ntop_buf
)));
550 if (fqdn_print(ndo
, cp
+ 1 + sizeof(a
) - pbyte
, bp
) == NULL
)
557 if (!ND_TTEST_LEN(cp
, len
))
559 ND_PRINT(" %u %u ", GET_BE_U_2(cp
), GET_BE_U_2(cp
+ 2));
560 if (nd_printn(ndo
, cp
+ 4, len
- 4, ndo
->ndo_snapend
))
565 ND_PRINT(" UDPsize=%u", class);
566 if (opt_flags
& 0x8000)
570 case T_UNSPECA
: /* One long string */
571 if (!ND_TTEST_LEN(cp
, len
))
573 if (nd_printn(ndo
, cp
, len
, ndo
->ndo_snapend
))
579 if (cp
+ len
> ndo
->ndo_snapend
)
584 if ((cp
= fqdn_print(ndo
, cp
, bp
)) == NULL
)
589 ND_PRINT(" fudge=%u", GET_BE_U_2(cp
));
593 ND_PRINT(" maclen=%u", GET_BE_U_2(cp
));
594 cp
+= 2 + GET_BE_U_2(cp
);
597 ND_PRINT(" origid=%u", GET_BE_U_2(cp
));
601 ND_PRINT(" error=%u", GET_BE_U_2(cp
));
605 ND_PRINT(" otherlen=%u", GET_BE_U_2(cp
));
609 return (rp
); /* XXX This isn't always right */
613 domain_print(netdissect_options
*ndo
,
614 const u_char
*bp
, u_int length
, int is_mdns
)
616 const dns_header_t
*np
;
617 uint16_t flags
, rcode
, rdlen
, type
;
618 u_int qdcount
, ancount
, nscount
, arcount
;
623 ndo
->ndo_protocol
= "domain";
624 np
= (const dns_header_t
*)bp
;
626 flags
= GET_BE_U_2(np
->flags
);
627 /* get the byte-order right */
628 qdcount
= GET_BE_U_2(np
->qdcount
);
629 ancount
= GET_BE_U_2(np
->ancount
);
630 nscount
= GET_BE_U_2(np
->nscount
);
631 arcount
= GET_BE_U_2(np
->arcount
);
633 /* find the opt record to extract extended rcode */
634 cp
= (const u_char
*)(np
+ 1);
635 rcode
= DNS_RCODE(flags
);
636 for (i
= 0; i
< qdcount
; i
++) {
637 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
639 cp
+= 4; /* skip QTYPE and QCLASS */
640 if (cp
>= ndo
->ndo_snapend
)
643 for (i
= 0; i
< ancount
+ nscount
; i
++) {
644 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
646 cp
+= 8; /* skip TYPE, CLASS and TTL */
647 if (cp
+ 2 > ndo
->ndo_snapend
)
649 rdlen
= GET_BE_U_2(cp
);
651 if (cp
>= ndo
->ndo_snapend
)
654 for (i
= 0; i
< arcount
; i
++) {
655 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
657 if (cp
+ 2 > ndo
->ndo_snapend
)
659 type
= GET_BE_U_2(cp
);
660 cp
+= 4; /* skip TYPE and CLASS */
661 if (cp
+ 1 > ndo
->ndo_snapend
)
668 if (cp
+ 2 > ndo
->ndo_snapend
)
670 rdlen
= GET_BE_U_2(cp
);
672 if (cp
>= ndo
->ndo_snapend
)
678 /* this is a response */
679 ND_PRINT("%u%s%s%s%s%s%s",
681 ns_ops
[DNS_OPCODE(flags
)],
683 DNS_AA(flags
)? "*" : "",
684 DNS_RA(flags
)? "" : "-",
685 DNS_TC(flags
)? "|" : "",
686 DNS_AD(flags
)? "$" : "");
689 ND_PRINT(" [%uq]", qdcount
);
690 /* Print QUESTION section on -vv */
691 cp
= (const u_char
*)(np
+ 1);
692 for (i
= 0; i
< qdcount
; i
++) {
695 if (ndo
->ndo_vflag
> 1) {
697 if ((cp
= ns_qprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
700 if ((cp
= ns_nskip(ndo
, cp
)) == NULL
)
702 cp
+= 4; /* skip QTYPE and QCLASS */
705 ND_PRINT(" %u/%u/%u", ancount
, nscount
, arcount
);
707 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
710 while (cp
< ndo
->ndo_snapend
&& ancount
) {
712 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
719 /* Print NS and AR sections on -vv */
720 if (ndo
->ndo_vflag
> 1) {
721 if (cp
< ndo
->ndo_snapend
&& nscount
) {
723 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
726 while (cp
< ndo
->ndo_snapend
&& nscount
) {
728 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
735 if (cp
< ndo
->ndo_snapend
&& arcount
) {
737 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
740 while (cp
< ndo
->ndo_snapend
&& arcount
) {
742 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
752 /* this is a request */
753 ND_PRINT("%u%s%s%s", GET_BE_U_2(np
->id
),
754 ns_ops
[DNS_OPCODE(flags
)],
755 DNS_RD(flags
) ? "+" : "",
756 DNS_CD(flags
) ? "%" : "");
759 b2
= GET_BE_U_2(((const u_short
*)np
) + 1);
761 ND_PRINT(" [b2&3=0x%x]", b2
);
763 if (DNS_OPCODE(flags
) == IQUERY
) {
765 ND_PRINT(" [%uq]", qdcount
);
767 ND_PRINT(" [%ua]", ancount
);
771 ND_PRINT(" [%ua]", ancount
);
773 ND_PRINT(" [%uq]", qdcount
);
776 ND_PRINT(" [%un]", nscount
);
778 ND_PRINT(" [%uau]", arcount
);
780 cp
= (const u_char
*)(np
+ 1);
782 cp
= ns_qprint(ndo
, cp
, (const u_char
*)np
, is_mdns
);
786 while (cp
< ndo
->ndo_snapend
&& qdcount
) {
787 cp
= ns_qprint(ndo
, (const u_char
*)cp
,
798 /* Print remaining sections on -vv */
799 if (ndo
->ndo_vflag
> 1) {
801 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
804 while (cp
< ndo
->ndo_snapend
&& ancount
) {
806 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
813 if (cp
< ndo
->ndo_snapend
&& nscount
) {
815 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
818 while (cp
< ndo
->ndo_snapend
&& nscount
) {
820 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
827 if (cp
< ndo
->ndo_snapend
&& arcount
) {
829 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
832 while (cp
< ndo
->ndo_snapend
&& arcount
) {
834 if ((cp
= ns_rprint(ndo
, cp
, bp
, is_mdns
)) == NULL
)
843 ND_PRINT(" (%u)", length
);