xref: /netbsd-src/external/bsd/tcpdump/dist/print-domain.c (revision cc576e1d8e4f4078fd4e81238abca9fca216f6ec)
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 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-domain.c,v 1.6 2017/01/24 23:29:14 christos Exp $");
25 #endif
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <netdissect-stdinc.h>
32 
33 #include "nameser.h"
34 
35 #include <string.h>
36 
37 #include "netdissect.h"
38 #include "addrtoname.h"
39 #include "addrtostr.h"
40 #include "extract.h"
41 
42 static const char *ns_ops[] = {
43 	"", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
44 	" op8", " updateA", " updateD", " updateDA",
45 	" updateM", " updateMA", " zoneInit", " zoneRef",
46 };
47 
48 static const char *ns_resp[] = {
49 	"", " FormErr", " ServFail", " NXDomain",
50 	" NotImp", " Refused", " YXDomain", " YXRRSet",
51 	" NXRRSet", " NotAuth", " NotZone", " Resp11",
52 	" Resp12", " Resp13", " Resp14", " NoChange",
53 };
54 
55 /* skip over a domain name */
56 static const u_char *
57 ns_nskip(netdissect_options *ndo,
58          register const u_char *cp)
59 {
60 	register u_char i;
61 
62 	if (!ND_TTEST2(*cp, 1))
63 		return (NULL);
64 	i = *cp++;
65 	while (i) {
66 		if ((i & INDIR_MASK) == INDIR_MASK)
67 			return (cp + 1);
68 		if ((i & INDIR_MASK) == EDNS0_MASK) {
69 			int bitlen, bytelen;
70 
71 			if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
72 				return(NULL); /* unknown ELT */
73 			if (!ND_TTEST2(*cp, 1))
74 				return (NULL);
75 			if ((bitlen = *cp++) == 0)
76 				bitlen = 256;
77 			bytelen = (bitlen + 7) / 8;
78 			cp += bytelen;
79 		} else
80 			cp += i;
81 		if (!ND_TTEST2(*cp, 1))
82 			return (NULL);
83 		i = *cp++;
84 	}
85 	return (cp);
86 }
87 
88 /* print a <domain-name> */
89 static const u_char *
90 blabel_print(netdissect_options *ndo,
91              const u_char *cp)
92 {
93 	int bitlen, slen, b;
94 	const u_char *bitp, *lim;
95 	char tc;
96 
97 	if (!ND_TTEST2(*cp, 1))
98 		return(NULL);
99 	if ((bitlen = *cp) == 0)
100 		bitlen = 256;
101 	slen = (bitlen + 3) / 4;
102 	lim = cp + 1 + slen;
103 
104 	/* print the bit string as a hex string */
105 	ND_PRINT((ndo, "\\[x"));
106 	for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
107 		ND_TCHECK(*bitp);
108 		ND_PRINT((ndo, "%02x", *bitp));
109 	}
110 	if (b > 4) {
111 		ND_TCHECK(*bitp);
112 		tc = *bitp++;
113 		ND_PRINT((ndo, "%02x", tc & (0xff << (8 - b))));
114 	} else if (b > 0) {
115 		ND_TCHECK(*bitp);
116 		tc = *bitp++;
117 		ND_PRINT((ndo, "%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
118 	}
119 	ND_PRINT((ndo, "/%d]", bitlen));
120 	return lim;
121 trunc:
122 	ND_PRINT((ndo, ".../%d]", bitlen));
123 	return NULL;
124 }
125 
126 static int
127 labellen(netdissect_options *ndo,
128          const u_char *cp)
129 {
130 	register u_int i;
131 
132 	if (!ND_TTEST2(*cp, 1))
133 		return(-1);
134 	i = *cp;
135 	if ((i & INDIR_MASK) == EDNS0_MASK) {
136 		int bitlen, elt;
137 		if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
138 			ND_PRINT((ndo, "<ELT %d>", elt));
139 			return(-1);
140 		}
141 		if (!ND_TTEST2(*(cp + 1), 1))
142 			return(-1);
143 		if ((bitlen = *(cp + 1)) == 0)
144 			bitlen = 256;
145 		return(((bitlen + 7) / 8) + 1);
146 	} else
147 		return(i);
148 }
149 
150 const u_char *
151 ns_nprint(netdissect_options *ndo,
152           register const u_char *cp, register const u_char *bp)
153 {
154 	register u_int i, l;
155 	register const u_char *rp = NULL;
156 	register int compress = 0;
157 	int chars_processed;
158 	int elt;
159 	int data_size = ndo->ndo_snapend - bp;
160 
161 	if ((l = labellen(ndo, cp)) == (u_int)-1)
162 		return(NULL);
163 	if (!ND_TTEST2(*cp, 1))
164 		return(NULL);
165 	chars_processed = 1;
166 	if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
167 		compress = 0;
168 		rp = cp + l;
169 	}
170 
171 	if (i != 0)
172 		while (i && cp < ndo->ndo_snapend) {
173 			if ((i & INDIR_MASK) == INDIR_MASK) {
174 				if (!compress) {
175 					rp = cp + 1;
176 					compress = 1;
177 				}
178 				if (!ND_TTEST2(*cp, 1))
179 					return(NULL);
180 				cp = bp + (((i << 8) | *cp) & 0x3fff);
181 				if ((l = labellen(ndo, cp)) == (u_int)-1)
182 					return(NULL);
183 				if (!ND_TTEST2(*cp, 1))
184 					return(NULL);
185 				i = *cp++;
186 				chars_processed++;
187 
188 				/*
189 				 * If we've looked at every character in
190 				 * the message, this pointer will make
191 				 * us look at some character again,
192 				 * which means we're looping.
193 				 */
194 				if (chars_processed >= data_size) {
195 					ND_PRINT((ndo, "<LOOP>"));
196 					return (NULL);
197 				}
198 				continue;
199 			}
200 			if ((i & INDIR_MASK) == EDNS0_MASK) {
201 				elt = (i & ~INDIR_MASK);
202 				switch(elt) {
203 				case EDNS0_ELT_BITLABEL:
204 					if (blabel_print(ndo, cp) == NULL)
205 						return (NULL);
206 					break;
207 				default:
208 					/* unknown ELT */
209 					ND_PRINT((ndo, "<ELT %d>", elt));
210 					return(NULL);
211 				}
212 			} else {
213 				if (fn_printn(ndo, cp, l, ndo->ndo_snapend))
214 					return(NULL);
215 			}
216 
217 			cp += l;
218 			chars_processed += l;
219 			ND_PRINT((ndo, "."));
220 			if ((l = labellen(ndo, cp)) == (u_int)-1)
221 				return(NULL);
222 			if (!ND_TTEST2(*cp, 1))
223 				return(NULL);
224 			i = *cp++;
225 			chars_processed++;
226 			if (!compress)
227 				rp += l + 1;
228 		}
229 	else
230 		ND_PRINT((ndo, "."));
231 	return (rp);
232 }
233 
234 /* print a <character-string> */
235 static const u_char *
236 ns_cprint(netdissect_options *ndo,
237           register const u_char *cp)
238 {
239 	register u_int i;
240 
241 	if (!ND_TTEST2(*cp, 1))
242 		return (NULL);
243 	i = *cp++;
244 	if (fn_printn(ndo, cp, i, ndo->ndo_snapend))
245 		return (NULL);
246 	return (cp + i);
247 }
248 
249 /* http://www.iana.org/assignments/dns-parameters */
250 const struct tok ns_type2str[] = {
251 	{ T_A,		"A" },			/* RFC 1035 */
252 	{ T_NS,		"NS" },			/* RFC 1035 */
253 	{ T_MD,		"MD" },			/* RFC 1035 */
254 	{ T_MF,		"MF" },			/* RFC 1035 */
255 	{ T_CNAME,	"CNAME" },		/* RFC 1035 */
256 	{ T_SOA,	"SOA" },		/* RFC 1035 */
257 	{ T_MB,		"MB" },			/* RFC 1035 */
258 	{ T_MG,		"MG" },			/* RFC 1035 */
259 	{ T_MR,		"MR" },			/* RFC 1035 */
260 	{ T_NULL,	"NULL" },		/* RFC 1035 */
261 	{ T_WKS,	"WKS" },		/* RFC 1035 */
262 	{ T_PTR,	"PTR" },		/* RFC 1035 */
263 	{ T_HINFO,	"HINFO" },		/* RFC 1035 */
264 	{ T_MINFO,	"MINFO" },		/* RFC 1035 */
265 	{ T_MX,		"MX" },			/* RFC 1035 */
266 	{ T_TXT,	"TXT" },		/* RFC 1035 */
267 	{ T_RP,		"RP" },			/* RFC 1183 */
268 	{ T_AFSDB,	"AFSDB" },		/* RFC 1183 */
269 	{ T_X25,	"X25" },		/* RFC 1183 */
270 	{ T_ISDN,	"ISDN" },		/* RFC 1183 */
271 	{ T_RT,		"RT" },			/* RFC 1183 */
272 	{ T_NSAP,	"NSAP" },		/* RFC 1706 */
273 	{ T_NSAP_PTR,	"NSAP_PTR" },
274 	{ T_SIG,	"SIG" },		/* RFC 2535 */
275 	{ T_KEY,	"KEY" },		/* RFC 2535 */
276 	{ T_PX,		"PX" },			/* RFC 2163 */
277 	{ T_GPOS,	"GPOS" },		/* RFC 1712 */
278 	{ T_AAAA,	"AAAA" },		/* RFC 1886 */
279 	{ T_LOC,	"LOC" },		/* RFC 1876 */
280 	{ T_NXT,	"NXT" },		/* RFC 2535 */
281 	{ T_EID,	"EID" },		/* Nimrod */
282 	{ T_NIMLOC,	"NIMLOC" },		/* Nimrod */
283 	{ T_SRV,	"SRV" },		/* RFC 2782 */
284 	{ T_ATMA,	"ATMA" },		/* ATM Forum */
285 	{ T_NAPTR,	"NAPTR" },		/* RFC 2168, RFC 2915 */
286 	{ T_KX,		"KX" },			/* RFC 2230 */
287 	{ T_CERT,	"CERT" },		/* RFC 2538 */
288 	{ T_A6,		"A6" },			/* RFC 2874 */
289 	{ T_DNAME,	"DNAME" },		/* RFC 2672 */
290 	{ T_SINK, 	"SINK" },
291 	{ T_OPT,	"OPT" },		/* RFC 2671 */
292 	{ T_APL, 	"APL" },		/* RFC 3123 */
293 	{ T_DS,		"DS" },			/* RFC 4034 */
294 	{ T_SSHFP,	"SSHFP" },		/* RFC 4255 */
295 	{ T_IPSECKEY,	"IPSECKEY" },		/* RFC 4025 */
296 	{ T_RRSIG, 	"RRSIG" },		/* RFC 4034 */
297 	{ T_NSEC,	"NSEC" },		/* RFC 4034 */
298 	{ T_DNSKEY,	"DNSKEY" },		/* RFC 4034 */
299 	{ T_SPF,	"SPF" },		/* RFC-schlitt-spf-classic-02.txt */
300 	{ T_UINFO,	"UINFO" },
301 	{ T_UID,	"UID" },
302 	{ T_GID,	"GID" },
303 	{ T_UNSPEC,	"UNSPEC" },
304 	{ T_UNSPECA,	"UNSPECA" },
305 	{ T_TKEY,	"TKEY" },		/* RFC 2930 */
306 	{ T_TSIG,	"TSIG" },		/* RFC 2845 */
307 	{ T_IXFR,	"IXFR" },		/* RFC 1995 */
308 	{ T_AXFR,	"AXFR" },		/* RFC 1035 */
309 	{ T_MAILB,	"MAILB" },		/* RFC 1035 */
310 	{ T_MAILA,	"MAILA" },		/* RFC 1035 */
311 	{ T_ANY,	"ANY" },
312 	{ 0,		NULL }
313 };
314 
315 const struct tok ns_class2str[] = {
316 	{ C_IN,		"IN" },		/* Not used */
317 	{ C_CHAOS,	"CHAOS" },
318 	{ C_HS,		"HS" },
319 	{ C_ANY,	"ANY" },
320 	{ 0,		NULL }
321 };
322 
323 /* print a query */
324 static const u_char *
325 ns_qprint(netdissect_options *ndo,
326           register const u_char *cp, register const u_char *bp, int is_mdns)
327 {
328 	register const u_char *np = cp;
329 	register u_int i, class;
330 
331 	cp = ns_nskip(ndo, cp);
332 
333 	if (cp == NULL || !ND_TTEST2(*cp, 4))
334 		return(NULL);
335 
336 	/* print the qtype */
337 	i = EXTRACT_16BITS(cp);
338 	cp += 2;
339 	ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", i)));
340 	/* print the qclass (if it's not IN) */
341 	i = EXTRACT_16BITS(cp);
342 	cp += 2;
343 	if (is_mdns)
344 		class = (i & ~C_QU);
345 	else
346 		class = i;
347 	if (class != C_IN)
348 		ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
349 	if (is_mdns) {
350 		ND_PRINT((ndo, i & C_QU ? " (QU)" : " (QM)"));
351 	}
352 
353 	ND_PRINT((ndo, "? "));
354 	cp = ns_nprint(ndo, np, bp);
355 	return(cp ? cp + 4 : NULL);
356 }
357 
358 /* print a reply */
359 static const u_char *
360 ns_rprint(netdissect_options *ndo,
361           register const u_char *cp, register const u_char *bp, int is_mdns)
362 {
363 	register u_int i, class, opt_flags = 0;
364 	register u_short typ, len;
365 	register const u_char *rp;
366 
367 	if (ndo->ndo_vflag) {
368 		ND_PRINT((ndo, " "));
369 		if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
370 			return NULL;
371 	} else
372 		cp = ns_nskip(ndo, cp);
373 
374 	if (cp == NULL || !ND_TTEST2(*cp, 10))
375 		return (ndo->ndo_snapend);
376 
377 	/* print the type/qtype */
378 	typ = EXTRACT_16BITS(cp);
379 	cp += 2;
380 	/* print the class (if it's not IN and the type isn't OPT) */
381 	i = EXTRACT_16BITS(cp);
382 	cp += 2;
383 	if (is_mdns)
384 		class = (i & ~C_CACHE_FLUSH);
385 	else
386 		class = i;
387 	if (class != C_IN && typ != T_OPT)
388 		ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
389 	if (is_mdns) {
390 		if (i & C_CACHE_FLUSH)
391 			ND_PRINT((ndo, " (Cache flush)"));
392 	}
393 
394 	if (typ == T_OPT) {
395 		/* get opt flags */
396 		cp += 2;
397 		opt_flags = EXTRACT_16BITS(cp);
398 		/* ignore rest of ttl field */
399 		cp += 2;
400 	} else if (ndo->ndo_vflag > 2) {
401 		/* print ttl */
402 		ND_PRINT((ndo, " ["));
403 		relts_print(ndo, EXTRACT_32BITS(cp));
404 		ND_PRINT((ndo, "]"));
405 		cp += 4;
406 	} else {
407 		/* ignore ttl */
408 		cp += 4;
409 	}
410 
411 	len = EXTRACT_16BITS(cp);
412 	cp += 2;
413 
414 	rp = cp + len;
415 
416 	ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", typ)));
417 	if (rp > ndo->ndo_snapend)
418 		return(NULL);
419 
420 	switch (typ) {
421 	case T_A:
422 		if (!ND_TTEST2(*cp, sizeof(struct in_addr)))
423 			return(NULL);
424 		ND_PRINT((ndo, " %s", intoa(htonl(EXTRACT_32BITS(cp)))));
425 		break;
426 
427 	case T_NS:
428 	case T_CNAME:
429 	case T_PTR:
430 #ifdef T_DNAME
431 	case T_DNAME:
432 #endif
433 		ND_PRINT((ndo, " "));
434 		if (ns_nprint(ndo, cp, bp) == NULL)
435 			return(NULL);
436 		break;
437 
438 	case T_SOA:
439 		if (!ndo->ndo_vflag)
440 			break;
441 		ND_PRINT((ndo, " "));
442 		if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
443 			return(NULL);
444 		ND_PRINT((ndo, " "));
445 		if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
446 			return(NULL);
447 		if (!ND_TTEST2(*cp, 5 * 4))
448 			return(NULL);
449 		ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
450 		cp += 4;
451 		ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
452 		cp += 4;
453 		ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
454 		cp += 4;
455 		ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
456 		cp += 4;
457 		ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
458 		cp += 4;
459 		break;
460 	case T_MX:
461 		ND_PRINT((ndo, " "));
462 		if (!ND_TTEST2(*cp, 2))
463 			return(NULL);
464 		if (ns_nprint(ndo, cp + 2, bp) == NULL)
465 			return(NULL);
466 		ND_PRINT((ndo, " %d", EXTRACT_16BITS(cp)));
467 		break;
468 
469 	case T_TXT:
470 		while (cp < rp) {
471 			ND_PRINT((ndo, " \""));
472 			cp = ns_cprint(ndo, cp);
473 			if (cp == NULL)
474 				return(NULL);
475 			ND_PRINT((ndo, "\""));
476 		}
477 		break;
478 
479 	case T_SRV:
480 		ND_PRINT((ndo, " "));
481 		if (!ND_TTEST2(*cp, 6))
482 			return(NULL);
483 		if (ns_nprint(ndo, cp + 6, bp) == NULL)
484 			return(NULL);
485 		ND_PRINT((ndo, ":%d %d %d", EXTRACT_16BITS(cp + 4),
486 			EXTRACT_16BITS(cp), EXTRACT_16BITS(cp + 2)));
487 		break;
488 
489 	case T_AAAA:
490 	    {
491 		char ntop_buf[INET6_ADDRSTRLEN];
492 
493 		if (!ND_TTEST2(*cp, sizeof(struct in6_addr)))
494 			return(NULL);
495 		ND_PRINT((ndo, " %s",
496 		    addrtostr6(cp, ntop_buf, sizeof(ntop_buf))));
497 
498 		break;
499 	    }
500 
501 	case T_A6:
502 	    {
503 		struct in6_addr a;
504 		int pbit, pbyte;
505 		char ntop_buf[INET6_ADDRSTRLEN];
506 
507 		if (!ND_TTEST2(*cp, 1))
508 			return(NULL);
509 		pbit = *cp;
510 		pbyte = (pbit & ~7) / 8;
511 		if (pbit > 128) {
512 			ND_PRINT((ndo, " %u(bad plen)", pbit));
513 			break;
514 		} else if (pbit < 128) {
515 			if (!ND_TTEST2(*(cp + 1), sizeof(a) - pbyte))
516 				return(NULL);
517 			memset(&a, 0, sizeof(a));
518 			memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
519 			ND_PRINT((ndo, " %u %s", pbit,
520 			    addrtostr6(&a, ntop_buf, sizeof(ntop_buf))));
521 		}
522 		if (pbit > 0) {
523 			ND_PRINT((ndo, " "));
524 			if (ns_nprint(ndo, cp + 1 + sizeof(a) - pbyte, bp) == NULL)
525 				return(NULL);
526 		}
527 		break;
528 	    }
529 
530 	case T_OPT:
531 		ND_PRINT((ndo, " UDPsize=%u", class));
532 		if (opt_flags & 0x8000)
533 			ND_PRINT((ndo, " DO"));
534 		break;
535 
536 	case T_UNSPECA:		/* One long string */
537 		if (!ND_TTEST2(*cp, len))
538 			return(NULL);
539 		if (fn_printn(ndo, cp, len, ndo->ndo_snapend))
540 			return(NULL);
541 		break;
542 
543 	case T_TSIG:
544 	    {
545 		if (cp + len > ndo->ndo_snapend)
546 			return(NULL);
547 		if (!ndo->ndo_vflag)
548 			break;
549 		ND_PRINT((ndo, " "));
550 		if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
551 			return(NULL);
552 		cp += 6;
553 		if (!ND_TTEST2(*cp, 2))
554 			return(NULL);
555 		ND_PRINT((ndo, " fudge=%u", EXTRACT_16BITS(cp)));
556 		cp += 2;
557 		if (!ND_TTEST2(*cp, 2))
558 			return(NULL);
559 		ND_PRINT((ndo, " maclen=%u", EXTRACT_16BITS(cp)));
560 		cp += 2 + EXTRACT_16BITS(cp);
561 		if (!ND_TTEST2(*cp, 2))
562 			return(NULL);
563 		ND_PRINT((ndo, " origid=%u", EXTRACT_16BITS(cp)));
564 		cp += 2;
565 		if (!ND_TTEST2(*cp, 2))
566 			return(NULL);
567 		ND_PRINT((ndo, " error=%u", EXTRACT_16BITS(cp)));
568 		cp += 2;
569 		if (!ND_TTEST2(*cp, 2))
570 			return(NULL);
571 		ND_PRINT((ndo, " otherlen=%u", EXTRACT_16BITS(cp)));
572 		cp += 2;
573 	    }
574 	}
575 	return (rp);		/* XXX This isn't always right */
576 }
577 
578 void
579 ns_print(netdissect_options *ndo,
580          register const u_char *bp, u_int length, int is_mdns)
581 {
582 	register const HEADER *np;
583 	register int qdcount, ancount, nscount, arcount;
584 	register const u_char *cp;
585 	uint16_t b2;
586 
587 	np = (const HEADER *)bp;
588 	ND_TCHECK(*np);
589 	/* get the byte-order right */
590 	qdcount = EXTRACT_16BITS(&np->qdcount);
591 	ancount = EXTRACT_16BITS(&np->ancount);
592 	nscount = EXTRACT_16BITS(&np->nscount);
593 	arcount = EXTRACT_16BITS(&np->arcount);
594 
595 	if (DNS_QR(np)) {
596 		/* this is a response */
597 		ND_PRINT((ndo, "%d%s%s%s%s%s%s",
598 			EXTRACT_16BITS(&np->id),
599 			ns_ops[DNS_OPCODE(np)],
600 			ns_resp[DNS_RCODE(np)],
601 			DNS_AA(np)? "*" : "",
602 			DNS_RA(np)? "" : "-",
603 			DNS_TC(np)? "|" : "",
604 			DNS_AD(np)? "$" : ""));
605 
606 		if (qdcount != 1)
607 			ND_PRINT((ndo, " [%dq]", qdcount));
608 		/* Print QUESTION section on -vv */
609 		cp = (const u_char *)(np + 1);
610 		while (qdcount--) {
611 			if (qdcount < EXTRACT_16BITS(&np->qdcount) - 1)
612 				ND_PRINT((ndo, ","));
613 			if (ndo->ndo_vflag > 1) {
614 				ND_PRINT((ndo, " q:"));
615 				if ((cp = ns_qprint(ndo, cp, bp, is_mdns)) == NULL)
616 					goto trunc;
617 			} else {
618 				if ((cp = ns_nskip(ndo, cp)) == NULL)
619 					goto trunc;
620 				cp += 4;	/* skip QTYPE and QCLASS */
621 			}
622 		}
623 		ND_PRINT((ndo, " %d/%d/%d", ancount, nscount, arcount));
624 		if (ancount--) {
625 			if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
626 				goto trunc;
627 			while (cp < ndo->ndo_snapend && ancount--) {
628 				ND_PRINT((ndo, ","));
629 				if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
630 					goto trunc;
631 			}
632 		}
633 		if (ancount > 0)
634 			goto trunc;
635 		/* Print NS and AR sections on -vv */
636 		if (ndo->ndo_vflag > 1) {
637 			if (cp < ndo->ndo_snapend && nscount--) {
638 				ND_PRINT((ndo, " ns:"));
639 				if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
640 					goto trunc;
641 				while (cp < ndo->ndo_snapend && nscount--) {
642 					ND_PRINT((ndo, ","));
643 					if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
644 						goto trunc;
645 				}
646 			}
647 			if (nscount > 0)
648 				goto trunc;
649 			if (cp < ndo->ndo_snapend && arcount--) {
650 				ND_PRINT((ndo, " ar:"));
651 				if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
652 					goto trunc;
653 				while (cp < ndo->ndo_snapend && arcount--) {
654 					ND_PRINT((ndo, ","));
655 					if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
656 						goto trunc;
657 				}
658 			}
659 			if (arcount > 0)
660 				goto trunc;
661 		}
662 	}
663 	else {
664 		/* this is a request */
665 		ND_PRINT((ndo, "%d%s%s%s", EXTRACT_16BITS(&np->id), ns_ops[DNS_OPCODE(np)],
666 		    DNS_RD(np) ? "+" : "",
667 		    DNS_CD(np) ? "%" : ""));
668 
669 		/* any weirdness? */
670 		b2 = EXTRACT_16BITS(((const u_short *)np)+1);
671 		if (b2 & 0x6cf)
672 			ND_PRINT((ndo, " [b2&3=0x%x]", b2));
673 
674 		if (DNS_OPCODE(np) == IQUERY) {
675 			if (qdcount)
676 				ND_PRINT((ndo, " [%dq]", qdcount));
677 			if (ancount != 1)
678 				ND_PRINT((ndo, " [%da]", ancount));
679 		}
680 		else {
681 			if (ancount)
682 				ND_PRINT((ndo, " [%da]", ancount));
683 			if (qdcount != 1)
684 				ND_PRINT((ndo, " [%dq]", qdcount));
685 		}
686 		if (nscount)
687 			ND_PRINT((ndo, " [%dn]", nscount));
688 		if (arcount)
689 			ND_PRINT((ndo, " [%dau]", arcount));
690 
691 		cp = (const u_char *)(np + 1);
692 		if (qdcount--) {
693 			cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
694 			if (!cp)
695 				goto trunc;
696 			while (cp < ndo->ndo_snapend && qdcount--) {
697 				cp = ns_qprint(ndo, (const u_char *)cp,
698 					       (const u_char *)np,
699 					       is_mdns);
700 				if (!cp)
701 					goto trunc;
702 			}
703 		}
704 		if (qdcount > 0)
705 			goto trunc;
706 
707 		/* Print remaining sections on -vv */
708 		if (ndo->ndo_vflag > 1) {
709 			if (ancount--) {
710 				if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
711 					goto trunc;
712 				while (cp < ndo->ndo_snapend && ancount--) {
713 					ND_PRINT((ndo, ","));
714 					if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
715 						goto trunc;
716 				}
717 			}
718 			if (ancount > 0)
719 				goto trunc;
720 			if (cp < ndo->ndo_snapend && nscount--) {
721 				ND_PRINT((ndo, " ns:"));
722 				if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
723 					goto trunc;
724 				while (nscount-- && cp < ndo->ndo_snapend) {
725 					ND_PRINT((ndo, ","));
726 					if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
727 						goto trunc;
728 				}
729 			}
730 			if (nscount > 0)
731 				goto trunc;
732 			if (cp < ndo->ndo_snapend && arcount--) {
733 				ND_PRINT((ndo, " ar:"));
734 				if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
735 					goto trunc;
736 				while (cp < ndo->ndo_snapend && arcount--) {
737 					ND_PRINT((ndo, ","));
738 					if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
739 						goto trunc;
740 				}
741 			}
742 			if (arcount > 0)
743 				goto trunc;
744 		}
745 	}
746 	ND_PRINT((ndo, " (%d)", length));
747 	return;
748 
749   trunc:
750 	ND_PRINT((ndo, "[|domain]"));
751 }
752