blob: d0b6996fda2f047ed451e76a23b91467153e49aa [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
Elliott Hughese2e3bd12017-05-15 10:59:29 -070022/* \summary: Domain Name System (DNS) printer */
23
The Android Open Source Project2949f582009-03-03 19:30:46 -080024#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
Elliott Hughese2e3bd12017-05-15 10:59:29 -070028#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080029
30#include "nameser.h"
31
The Android Open Source Project2949f582009-03-03 19:30:46 -080032#include <string.h>
33
Elliott Hughese2e3bd12017-05-15 10:59:29 -070034#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080035#include "addrtoname.h"
Elliott Hughese2e3bd12017-05-15 10:59:29 -070036#include "addrtostr.h"
37#include "extract.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080038
39static const char *ns_ops[] = {
40 "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
Elliott Hughes892a68b2015-10-19 14:43:53 -070041 " op8", " updateA", " updateD", " updateDA",
The Android Open Source Project2949f582009-03-03 19:30:46 -080042 " updateM", " updateMA", " zoneInit", " zoneRef",
43};
44
45static const char *ns_resp[] = {
46 "", " FormErr", " ServFail", " NXDomain",
47 " NotImp", " Refused", " YXDomain", " YXRRSet",
48 " NXRRSet", " NotAuth", " NotZone", " Resp11",
49 " Resp12", " Resp13", " Resp14", " NoChange",
50};
51
52/* skip over a domain name */
53static const u_char *
Elliott Hughes892a68b2015-10-19 14:43:53 -070054ns_nskip(netdissect_options *ndo,
55 register const u_char *cp)
The Android Open Source Project2949f582009-03-03 19:30:46 -080056{
57 register u_char i;
58
Elliott Hughes892a68b2015-10-19 14:43:53 -070059 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -080060 return (NULL);
61 i = *cp++;
62 while (i) {
63 if ((i & INDIR_MASK) == INDIR_MASK)
64 return (cp + 1);
65 if ((i & INDIR_MASK) == EDNS0_MASK) {
66 int bitlen, bytelen;
67
68 if ((i & ~INDIR_MASK) != EDNS0_ELT_BITLABEL)
69 return(NULL); /* unknown ELT */
Elliott Hughes892a68b2015-10-19 14:43:53 -070070 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -080071 return (NULL);
72 if ((bitlen = *cp++) == 0)
73 bitlen = 256;
74 bytelen = (bitlen + 7) / 8;
75 cp += bytelen;
76 } else
77 cp += i;
Elliott Hughes892a68b2015-10-19 14:43:53 -070078 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -080079 return (NULL);
80 i = *cp++;
81 }
82 return (cp);
83}
84
85/* print a <domain-name> */
86static const u_char *
Elliott Hughes892a68b2015-10-19 14:43:53 -070087blabel_print(netdissect_options *ndo,
88 const u_char *cp)
The Android Open Source Project2949f582009-03-03 19:30:46 -080089{
90 int bitlen, slen, b;
91 const u_char *bitp, *lim;
92 char tc;
93
Elliott Hughes892a68b2015-10-19 14:43:53 -070094 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -080095 return(NULL);
96 if ((bitlen = *cp) == 0)
97 bitlen = 256;
98 slen = (bitlen + 3) / 4;
99 lim = cp + 1 + slen;
100
101 /* print the bit string as a hex string */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700102 ND_PRINT((ndo, "\\[x"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800103 for (bitp = cp + 1, b = bitlen; bitp < lim && b > 7; b -= 8, bitp++) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700104 ND_TCHECK(*bitp);
105 ND_PRINT((ndo, "%02x", *bitp));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800106 }
107 if (b > 4) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700108 ND_TCHECK(*bitp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800109 tc = *bitp++;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700110 ND_PRINT((ndo, "%02x", tc & (0xff << (8 - b))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800111 } else if (b > 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700112 ND_TCHECK(*bitp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800113 tc = *bitp++;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700114 ND_PRINT((ndo, "%1x", ((tc >> 4) & 0x0f) & (0x0f << (4 - b))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700116 ND_PRINT((ndo, "/%d]", bitlen));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800117 return lim;
118trunc:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700119 ND_PRINT((ndo, ".../%d]", bitlen));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800120 return NULL;
121}
122
123static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700124labellen(netdissect_options *ndo,
125 const u_char *cp)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800126{
127 register u_int i;
128
Elliott Hughes892a68b2015-10-19 14:43:53 -0700129 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800130 return(-1);
131 i = *cp;
132 if ((i & INDIR_MASK) == EDNS0_MASK) {
133 int bitlen, elt;
134 if ((elt = (i & ~INDIR_MASK)) != EDNS0_ELT_BITLABEL) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700135 ND_PRINT((ndo, "<ELT %d>", elt));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800136 return(-1);
137 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700138 if (!ND_TTEST2(*(cp + 1), 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800139 return(-1);
140 if ((bitlen = *(cp + 1)) == 0)
141 bitlen = 256;
142 return(((bitlen + 7) / 8) + 1);
143 } else
144 return(i);
145}
146
JP Abgrall53f17a92014-02-12 14:02:41 -0800147const u_char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700148ns_nprint(netdissect_options *ndo,
149 register const u_char *cp, register const u_char *bp)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800150{
151 register u_int i, l;
152 register const u_char *rp = NULL;
153 register int compress = 0;
154 int chars_processed;
155 int elt;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700156 int data_size = ndo->ndo_snapend - bp;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800157
Elliott Hughes892a68b2015-10-19 14:43:53 -0700158 if ((l = labellen(ndo, cp)) == (u_int)-1)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800159 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700160 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800161 return(NULL);
162 chars_processed = 1;
163 if (((i = *cp++) & INDIR_MASK) != INDIR_MASK) {
164 compress = 0;
165 rp = cp + l;
166 }
167
168 if (i != 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700169 while (i && cp < ndo->ndo_snapend) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800170 if ((i & INDIR_MASK) == INDIR_MASK) {
171 if (!compress) {
172 rp = cp + 1;
173 compress = 1;
174 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700175 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800176 return(NULL);
177 cp = bp + (((i << 8) | *cp) & 0x3fff);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700178 if ((l = labellen(ndo, cp)) == (u_int)-1)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800179 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700180 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800181 return(NULL);
182 i = *cp++;
183 chars_processed++;
184
185 /*
186 * If we've looked at every character in
187 * the message, this pointer will make
188 * us look at some character again,
189 * which means we're looping.
190 */
191 if (chars_processed >= data_size) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700192 ND_PRINT((ndo, "<LOOP>"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800193 return (NULL);
194 }
195 continue;
196 }
197 if ((i & INDIR_MASK) == EDNS0_MASK) {
198 elt = (i & ~INDIR_MASK);
199 switch(elt) {
200 case EDNS0_ELT_BITLABEL:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700201 if (blabel_print(ndo, cp) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800202 return (NULL);
203 break;
204 default:
205 /* unknown ELT */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700206 ND_PRINT((ndo, "<ELT %d>", elt));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800207 return(NULL);
208 }
209 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700210 if (fn_printn(ndo, cp, l, ndo->ndo_snapend))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800211 return(NULL);
212 }
213
214 cp += l;
215 chars_processed += l;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700216 ND_PRINT((ndo, "."));
217 if ((l = labellen(ndo, cp)) == (u_int)-1)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800218 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700219 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800220 return(NULL);
221 i = *cp++;
222 chars_processed++;
223 if (!compress)
224 rp += l + 1;
225 }
226 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700227 ND_PRINT((ndo, "."));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800228 return (rp);
229}
230
231/* print a <character-string> */
232static const u_char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700233ns_cprint(netdissect_options *ndo,
234 register const u_char *cp)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800235{
236 register u_int i;
237
Elliott Hughes892a68b2015-10-19 14:43:53 -0700238 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800239 return (NULL);
240 i = *cp++;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700241 if (fn_printn(ndo, cp, i, ndo->ndo_snapend))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800242 return (NULL);
243 return (cp + i);
244}
245
246/* http://www.iana.org/assignments/dns-parameters */
JP Abgrall53f17a92014-02-12 14:02:41 -0800247const struct tok ns_type2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800248 { T_A, "A" }, /* RFC 1035 */
249 { T_NS, "NS" }, /* RFC 1035 */
250 { T_MD, "MD" }, /* RFC 1035 */
251 { T_MF, "MF" }, /* RFC 1035 */
252 { T_CNAME, "CNAME" }, /* RFC 1035 */
253 { T_SOA, "SOA" }, /* RFC 1035 */
254 { T_MB, "MB" }, /* RFC 1035 */
255 { T_MG, "MG" }, /* RFC 1035 */
256 { T_MR, "MR" }, /* RFC 1035 */
257 { T_NULL, "NULL" }, /* RFC 1035 */
258 { T_WKS, "WKS" }, /* RFC 1035 */
259 { T_PTR, "PTR" }, /* RFC 1035 */
260 { T_HINFO, "HINFO" }, /* RFC 1035 */
261 { T_MINFO, "MINFO" }, /* RFC 1035 */
262 { T_MX, "MX" }, /* RFC 1035 */
263 { T_TXT, "TXT" }, /* RFC 1035 */
264 { T_RP, "RP" }, /* RFC 1183 */
265 { T_AFSDB, "AFSDB" }, /* RFC 1183 */
266 { T_X25, "X25" }, /* RFC 1183 */
267 { T_ISDN, "ISDN" }, /* RFC 1183 */
268 { T_RT, "RT" }, /* RFC 1183 */
269 { T_NSAP, "NSAP" }, /* RFC 1706 */
270 { T_NSAP_PTR, "NSAP_PTR" },
271 { T_SIG, "SIG" }, /* RFC 2535 */
272 { T_KEY, "KEY" }, /* RFC 2535 */
273 { T_PX, "PX" }, /* RFC 2163 */
274 { T_GPOS, "GPOS" }, /* RFC 1712 */
275 { T_AAAA, "AAAA" }, /* RFC 1886 */
276 { T_LOC, "LOC" }, /* RFC 1876 */
277 { T_NXT, "NXT" }, /* RFC 2535 */
278 { T_EID, "EID" }, /* Nimrod */
279 { T_NIMLOC, "NIMLOC" }, /* Nimrod */
280 { T_SRV, "SRV" }, /* RFC 2782 */
281 { T_ATMA, "ATMA" }, /* ATM Forum */
282 { T_NAPTR, "NAPTR" }, /* RFC 2168, RFC 2915 */
283 { T_KX, "KX" }, /* RFC 2230 */
284 { T_CERT, "CERT" }, /* RFC 2538 */
285 { T_A6, "A6" }, /* RFC 2874 */
286 { T_DNAME, "DNAME" }, /* RFC 2672 */
287 { T_SINK, "SINK" },
288 { T_OPT, "OPT" }, /* RFC 2671 */
289 { T_APL, "APL" }, /* RFC 3123 */
290 { T_DS, "DS" }, /* RFC 4034 */
291 { T_SSHFP, "SSHFP" }, /* RFC 4255 */
292 { T_IPSECKEY, "IPSECKEY" }, /* RFC 4025 */
293 { T_RRSIG, "RRSIG" }, /* RFC 4034 */
294 { T_NSEC, "NSEC" }, /* RFC 4034 */
295 { T_DNSKEY, "DNSKEY" }, /* RFC 4034 */
296 { T_SPF, "SPF" }, /* RFC-schlitt-spf-classic-02.txt */
297 { T_UINFO, "UINFO" },
298 { T_UID, "UID" },
299 { T_GID, "GID" },
300 { T_UNSPEC, "UNSPEC" },
301 { T_UNSPECA, "UNSPECA" },
302 { T_TKEY, "TKEY" }, /* RFC 2930 */
303 { T_TSIG, "TSIG" }, /* RFC 2845 */
304 { T_IXFR, "IXFR" }, /* RFC 1995 */
305 { T_AXFR, "AXFR" }, /* RFC 1035 */
306 { T_MAILB, "MAILB" }, /* RFC 1035 */
307 { T_MAILA, "MAILA" }, /* RFC 1035 */
308 { T_ANY, "ANY" },
309 { 0, NULL }
310};
311
JP Abgrall53f17a92014-02-12 14:02:41 -0800312const struct tok ns_class2str[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800313 { C_IN, "IN" }, /* Not used */
314 { C_CHAOS, "CHAOS" },
315 { C_HS, "HS" },
316 { C_ANY, "ANY" },
317 { 0, NULL }
318};
319
320/* print a query */
321static const u_char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700322ns_qprint(netdissect_options *ndo,
323 register const u_char *cp, register const u_char *bp, int is_mdns)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800324{
325 register const u_char *np = cp;
326 register u_int i, class;
327
Elliott Hughes892a68b2015-10-19 14:43:53 -0700328 cp = ns_nskip(ndo, cp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800329
Elliott Hughes892a68b2015-10-19 14:43:53 -0700330 if (cp == NULL || !ND_TTEST2(*cp, 4))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800331 return(NULL);
332
333 /* print the qtype */
334 i = EXTRACT_16BITS(cp);
335 cp += 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700336 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", i)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800337 /* print the qclass (if it's not IN) */
338 i = EXTRACT_16BITS(cp);
339 cp += 2;
340 if (is_mdns)
341 class = (i & ~C_QU);
342 else
343 class = i;
344 if (class != C_IN)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700345 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800346 if (is_mdns) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700347 ND_PRINT((ndo, i & C_QU ? " (QU)" : " (QM)"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800348 }
349
Elliott Hughes892a68b2015-10-19 14:43:53 -0700350 ND_PRINT((ndo, "? "));
351 cp = ns_nprint(ndo, np, bp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800352 return(cp ? cp + 4 : NULL);
353}
354
355/* print a reply */
356static const u_char *
Elliott Hughes892a68b2015-10-19 14:43:53 -0700357ns_rprint(netdissect_options *ndo,
358 register const u_char *cp, register const u_char *bp, int is_mdns)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800359{
360 register u_int i, class, opt_flags = 0;
361 register u_short typ, len;
362 register const u_char *rp;
363
Elliott Hughes892a68b2015-10-19 14:43:53 -0700364 if (ndo->ndo_vflag) {
365 ND_PRINT((ndo, " "));
366 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800367 return NULL;
368 } else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700369 cp = ns_nskip(ndo, cp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800370
Elliott Hughes892a68b2015-10-19 14:43:53 -0700371 if (cp == NULL || !ND_TTEST2(*cp, 10))
372 return (ndo->ndo_snapend);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800373
374 /* print the type/qtype */
375 typ = EXTRACT_16BITS(cp);
376 cp += 2;
377 /* print the class (if it's not IN and the type isn't OPT) */
378 i = EXTRACT_16BITS(cp);
379 cp += 2;
380 if (is_mdns)
381 class = (i & ~C_CACHE_FLUSH);
382 else
383 class = i;
384 if (class != C_IN && typ != T_OPT)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700385 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "(Class %d)", class)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800386 if (is_mdns) {
387 if (i & C_CACHE_FLUSH)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700388 ND_PRINT((ndo, " (Cache flush)"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800389 }
390
JP Abgrall53f17a92014-02-12 14:02:41 -0800391 if (typ == T_OPT) {
392 /* get opt flags */
393 cp += 2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800394 opt_flags = EXTRACT_16BITS(cp);
JP Abgrall53f17a92014-02-12 14:02:41 -0800395 /* ignore rest of ttl field */
396 cp += 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700397 } else if (ndo->ndo_vflag > 2) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800398 /* print ttl */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700399 ND_PRINT((ndo, " ["));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700400 unsigned_relts_print(ndo, EXTRACT_32BITS(cp));
Elliott Hughes892a68b2015-10-19 14:43:53 -0700401 ND_PRINT((ndo, "]"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800402 cp += 4;
403 } else {
404 /* ignore ttl */
405 cp += 4;
406 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800407
408 len = EXTRACT_16BITS(cp);
409 cp += 2;
410
411 rp = cp + len;
412
Elliott Hughes892a68b2015-10-19 14:43:53 -0700413 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d", typ)));
414 if (rp > ndo->ndo_snapend)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800415 return(NULL);
416
417 switch (typ) {
418 case T_A:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700419 if (!ND_TTEST2(*cp, sizeof(struct in_addr)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800420 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700421 ND_PRINT((ndo, " %s", intoa(htonl(EXTRACT_32BITS(cp)))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800422 break;
423
424 case T_NS:
425 case T_CNAME:
426 case T_PTR:
427#ifdef T_DNAME
428 case T_DNAME:
429#endif
Elliott Hughes892a68b2015-10-19 14:43:53 -0700430 ND_PRINT((ndo, " "));
431 if (ns_nprint(ndo, cp, bp) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800432 return(NULL);
433 break;
434
435 case T_SOA:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700436 if (!ndo->ndo_vflag)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800437 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700438 ND_PRINT((ndo, " "));
439 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800440 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700441 ND_PRINT((ndo, " "));
442 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800443 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700444 if (!ND_TTEST2(*cp, 5 * 4))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800445 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700446 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800447 cp += 4;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700448 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800449 cp += 4;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700450 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800451 cp += 4;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700452 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800453 cp += 4;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700454 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800455 cp += 4;
456 break;
457 case T_MX:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700458 ND_PRINT((ndo, " "));
459 if (!ND_TTEST2(*cp, 2))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800460 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700461 if (ns_nprint(ndo, cp + 2, bp) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800462 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700463 ND_PRINT((ndo, " %d", EXTRACT_16BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800464 break;
465
466 case T_TXT:
467 while (cp < rp) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700468 ND_PRINT((ndo, " \""));
469 cp = ns_cprint(ndo, cp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800470 if (cp == NULL)
471 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700472 ND_PRINT((ndo, "\""));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800473 }
474 break;
475
476 case T_SRV:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700477 ND_PRINT((ndo, " "));
478 if (!ND_TTEST2(*cp, 6))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800479 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700480 if (ns_nprint(ndo, cp + 6, bp) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800481 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700482 ND_PRINT((ndo, ":%d %d %d", EXTRACT_16BITS(cp + 4),
483 EXTRACT_16BITS(cp), EXTRACT_16BITS(cp + 2)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800484 break;
485
The Android Open Source Project2949f582009-03-03 19:30:46 -0800486 case T_AAAA:
JP Abgrall53f17a92014-02-12 14:02:41 -0800487 {
JP Abgrall53f17a92014-02-12 14:02:41 -0800488 char ntop_buf[INET6_ADDRSTRLEN];
489
Elliott Hughes892a68b2015-10-19 14:43:53 -0700490 if (!ND_TTEST2(*cp, sizeof(struct in6_addr)))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800491 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700492 ND_PRINT((ndo, " %s",
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700493 addrtostr6(cp, ntop_buf, sizeof(ntop_buf))));
JP Abgrall53f17a92014-02-12 14:02:41 -0800494
The Android Open Source Project2949f582009-03-03 19:30:46 -0800495 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800496 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800497
498 case T_A6:
499 {
500 struct in6_addr a;
501 int pbit, pbyte;
JP Abgrall53f17a92014-02-12 14:02:41 -0800502 char ntop_buf[INET6_ADDRSTRLEN];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800503
Elliott Hughes892a68b2015-10-19 14:43:53 -0700504 if (!ND_TTEST2(*cp, 1))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800505 return(NULL);
506 pbit = *cp;
507 pbyte = (pbit & ~7) / 8;
508 if (pbit > 128) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700509 ND_PRINT((ndo, " %u(bad plen)", pbit));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800510 break;
511 } else if (pbit < 128) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700512 if (!ND_TTEST2(*(cp + 1), sizeof(a) - pbyte))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800513 return(NULL);
514 memset(&a, 0, sizeof(a));
515 memcpy(&a.s6_addr[pbyte], cp + 1, sizeof(a) - pbyte);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700516 ND_PRINT((ndo, " %u %s", pbit,
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700517 addrtostr6(&a, ntop_buf, sizeof(ntop_buf))));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800518 }
519 if (pbit > 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700520 ND_PRINT((ndo, " "));
521 if (ns_nprint(ndo, cp + 1 + sizeof(a) - pbyte, bp) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800522 return(NULL);
523 }
524 break;
525 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800526
527 case T_OPT:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700528 ND_PRINT((ndo, " UDPsize=%u", class));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800529 if (opt_flags & 0x8000)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700530 ND_PRINT((ndo, " DO"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800531 break;
532
533 case T_UNSPECA: /* One long string */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700534 if (!ND_TTEST2(*cp, len))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800535 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700536 if (fn_printn(ndo, cp, len, ndo->ndo_snapend))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800537 return(NULL);
538 break;
539
540 case T_TSIG:
541 {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700542 if (cp + len > ndo->ndo_snapend)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800543 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700544 if (!ndo->ndo_vflag)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800545 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700546 ND_PRINT((ndo, " "));
547 if ((cp = ns_nprint(ndo, cp, bp)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800548 return(NULL);
549 cp += 6;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700550 if (!ND_TTEST2(*cp, 2))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800551 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700552 ND_PRINT((ndo, " fudge=%u", EXTRACT_16BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800553 cp += 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700554 if (!ND_TTEST2(*cp, 2))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800555 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700556 ND_PRINT((ndo, " maclen=%u", EXTRACT_16BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800557 cp += 2 + EXTRACT_16BITS(cp);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700558 if (!ND_TTEST2(*cp, 2))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800559 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700560 ND_PRINT((ndo, " origid=%u", EXTRACT_16BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800561 cp += 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700562 if (!ND_TTEST2(*cp, 2))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800563 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700564 ND_PRINT((ndo, " error=%u", EXTRACT_16BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800565 cp += 2;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700566 if (!ND_TTEST2(*cp, 2))
The Android Open Source Project2949f582009-03-03 19:30:46 -0800567 return(NULL);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700568 ND_PRINT((ndo, " otherlen=%u", EXTRACT_16BITS(cp)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800569 cp += 2;
570 }
571 }
572 return (rp); /* XXX This isn't always right */
573}
574
575void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700576ns_print(netdissect_options *ndo,
577 register const u_char *bp, u_int length, int is_mdns)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800578{
579 register const HEADER *np;
580 register int qdcount, ancount, nscount, arcount;
581 register const u_char *cp;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700582 uint16_t b2;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800583
584 np = (const HEADER *)bp;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700585 ND_TCHECK(*np);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800586 /* get the byte-order right */
587 qdcount = EXTRACT_16BITS(&np->qdcount);
588 ancount = EXTRACT_16BITS(&np->ancount);
589 nscount = EXTRACT_16BITS(&np->nscount);
590 arcount = EXTRACT_16BITS(&np->arcount);
591
592 if (DNS_QR(np)) {
593 /* this is a response */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700594 ND_PRINT((ndo, "%d%s%s%s%s%s%s",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800595 EXTRACT_16BITS(&np->id),
596 ns_ops[DNS_OPCODE(np)],
597 ns_resp[DNS_RCODE(np)],
598 DNS_AA(np)? "*" : "",
599 DNS_RA(np)? "" : "-",
600 DNS_TC(np)? "|" : "",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700601 DNS_AD(np)? "$" : ""));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800602
603 if (qdcount != 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700604 ND_PRINT((ndo, " [%dq]", qdcount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800605 /* Print QUESTION section on -vv */
606 cp = (const u_char *)(np + 1);
607 while (qdcount--) {
608 if (qdcount < EXTRACT_16BITS(&np->qdcount) - 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700609 ND_PRINT((ndo, ","));
610 if (ndo->ndo_vflag > 1) {
611 ND_PRINT((ndo, " q:"));
612 if ((cp = ns_qprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800613 goto trunc;
614 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700615 if ((cp = ns_nskip(ndo, cp)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800616 goto trunc;
617 cp += 4; /* skip QTYPE and QCLASS */
618 }
619 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700620 ND_PRINT((ndo, " %d/%d/%d", ancount, nscount, arcount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800621 if (ancount--) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700622 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800623 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700624 while (cp < ndo->ndo_snapend && ancount--) {
625 ND_PRINT((ndo, ","));
626 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800627 goto trunc;
628 }
629 }
630 if (ancount > 0)
631 goto trunc;
632 /* Print NS and AR sections on -vv */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700633 if (ndo->ndo_vflag > 1) {
634 if (cp < ndo->ndo_snapend && nscount--) {
635 ND_PRINT((ndo, " ns:"));
636 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800637 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700638 while (cp < ndo->ndo_snapend && nscount--) {
639 ND_PRINT((ndo, ","));
640 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800641 goto trunc;
642 }
643 }
644 if (nscount > 0)
645 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700646 if (cp < ndo->ndo_snapend && arcount--) {
647 ND_PRINT((ndo, " ar:"));
648 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800649 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700650 while (cp < ndo->ndo_snapend && arcount--) {
651 ND_PRINT((ndo, ","));
652 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800653 goto trunc;
654 }
655 }
656 if (arcount > 0)
657 goto trunc;
658 }
659 }
660 else {
661 /* this is a request */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700662 ND_PRINT((ndo, "%d%s%s%s", EXTRACT_16BITS(&np->id), ns_ops[DNS_OPCODE(np)],
The Android Open Source Project2949f582009-03-03 19:30:46 -0800663 DNS_RD(np) ? "+" : "",
Elliott Hughes892a68b2015-10-19 14:43:53 -0700664 DNS_CD(np) ? "%" : ""));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800665
666 /* any weirdness? */
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700667 b2 = EXTRACT_16BITS(((const u_short *)np)+1);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800668 if (b2 & 0x6cf)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700669 ND_PRINT((ndo, " [b2&3=0x%x]", b2));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800670
671 if (DNS_OPCODE(np) == IQUERY) {
672 if (qdcount)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700673 ND_PRINT((ndo, " [%dq]", qdcount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800674 if (ancount != 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700675 ND_PRINT((ndo, " [%da]", ancount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800676 }
677 else {
678 if (ancount)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700679 ND_PRINT((ndo, " [%da]", ancount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800680 if (qdcount != 1)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700681 ND_PRINT((ndo, " [%dq]", qdcount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800682 }
683 if (nscount)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700684 ND_PRINT((ndo, " [%dn]", nscount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800685 if (arcount)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700686 ND_PRINT((ndo, " [%dau]", arcount));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800687
688 cp = (const u_char *)(np + 1);
689 if (qdcount--) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700690 cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800691 if (!cp)
692 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700693 while (cp < ndo->ndo_snapend && qdcount--) {
694 cp = ns_qprint(ndo, (const u_char *)cp,
The Android Open Source Project2949f582009-03-03 19:30:46 -0800695 (const u_char *)np,
696 is_mdns);
697 if (!cp)
698 goto trunc;
699 }
700 }
701 if (qdcount > 0)
702 goto trunc;
703
704 /* Print remaining sections on -vv */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700705 if (ndo->ndo_vflag > 1) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800706 if (ancount--) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700707 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800708 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700709 while (cp < ndo->ndo_snapend && ancount--) {
710 ND_PRINT((ndo, ","));
711 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800712 goto trunc;
713 }
714 }
715 if (ancount > 0)
716 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700717 if (cp < ndo->ndo_snapend && nscount--) {
718 ND_PRINT((ndo, " ns:"));
719 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800720 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700721 while (nscount-- && cp < ndo->ndo_snapend) {
722 ND_PRINT((ndo, ","));
723 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800724 goto trunc;
725 }
726 }
727 if (nscount > 0)
728 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700729 if (cp < ndo->ndo_snapend && arcount--) {
730 ND_PRINT((ndo, " ar:"));
731 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800732 goto trunc;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700733 while (cp < ndo->ndo_snapend && arcount--) {
734 ND_PRINT((ndo, ","));
735 if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800736 goto trunc;
737 }
738 }
739 if (arcount > 0)
740 goto trunc;
741 }
742 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700743 ND_PRINT((ndo, " (%d)", length));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800744 return;
745
746 trunc:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700747 ND_PRINT((ndo, "[|domain]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800748}