]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
ICMP: Replace a 'struct in_addr' member type with a 'nd_ipv4' one
[tcpdump] / print-lwres.c
1 /*
2 * Copyright (C) 2001 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /* \summary: BIND9 Lightweight Resolver protocol printer */
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "netdissect-stdinc.h"
37
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "netdissect.h"
42 #include "addrtoname.h"
43 #include "extract.h"
44
45 #include "nameser.h"
46
47 /* BIND9 lib/lwres/include/lwres */
48 /*
49 * Use nd_uint16_t for lwres_uint16_t
50 * Use nd_uint32_t for lwres_uint32_t
51 */
52
53 struct lwres_lwpacket {
54 nd_uint32_t length;
55 nd_uint16_t version;
56 nd_uint16_t pktflags;
57 nd_uint32_t serial;
58 nd_uint32_t opcode;
59 nd_uint32_t result;
60 nd_uint32_t recvlength;
61 nd_uint16_t authtype;
62 nd_uint16_t authlength;
63 };
64
65 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
66
67 #define LWRES_LWPACKETVERSION_0 0
68
69 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
70 #define LWRES_FLAG_SECUREDATA 0x00000002U
71
72 /*
73 * no-op
74 */
75 #define LWRES_OPCODE_NOOP 0x00000000U
76
77 typedef struct {
78 /* public */
79 nd_uint16_t datalength;
80 /* data follows */
81 } lwres_nooprequest_t;
82
83 typedef struct {
84 /* public */
85 nd_uint16_t datalength;
86 /* data follows */
87 } lwres_noopresponse_t;
88
89 /*
90 * get addresses by name
91 */
92 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
93
94 typedef struct lwres_addr lwres_addr_t;
95
96 struct lwres_addr {
97 nd_uint32_t family;
98 nd_uint16_t length;
99 /* address folows */
100 };
101 #define LWRES_ADDR_LEN 6
102
103 typedef struct {
104 /* public */
105 nd_uint32_t flags;
106 nd_uint32_t addrtypes;
107 nd_uint16_t namelen;
108 /* name follows */
109 } lwres_gabnrequest_t;
110 #define LWRES_GABNREQUEST_LEN 10
111
112 typedef struct {
113 /* public */
114 nd_uint32_t flags;
115 nd_uint16_t naliases;
116 nd_uint16_t naddrs;
117 nd_uint16_t realnamelen;
118 /* aliases follows */
119 /* addrs follows */
120 /* realname follows */
121 } lwres_gabnresponse_t;
122 #define LWRES_GABNRESPONSE_LEN 10
123
124 /*
125 * get name by address
126 */
127 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
128 typedef struct {
129 /* public */
130 nd_uint32_t flags;
131 /* addr follows */
132 } lwres_gnbarequest_t;
133 #define LWRES_GNBAREQUEST_LEN 4
134
135 typedef struct {
136 /* public */
137 nd_uint32_t flags;
138 nd_uint16_t naliases;
139 nd_uint16_t realnamelen;
140 /* aliases follows */
141 /* realname follows */
142 } lwres_gnbaresponse_t;
143 #define LWRES_GNBARESPONSE_LEN 8
144
145 /*
146 * get rdata by name
147 */
148 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
149
150 typedef struct {
151 /* public */
152 nd_uint32_t flags;
153 nd_uint16_t rdclass;
154 nd_uint16_t rdtype;
155 nd_uint16_t namelen;
156 /* name follows */
157 } lwres_grbnrequest_t;
158 #define LWRES_GRBNREQUEST_LEN 10
159
160 typedef struct {
161 /* public */
162 nd_uint32_t flags;
163 nd_uint16_t rdclass;
164 nd_uint16_t rdtype;
165 nd_uint32_t ttl;
166 nd_uint16_t nrdatas;
167 nd_uint16_t nsigs;
168 /* realname here (len + name) */
169 /* rdata here (len + name) */
170 /* signatures here (len + name) */
171 } lwres_grbnresponse_t;
172 #define LWRES_GRBNRESPONSE_LEN 16
173
174 #define LWRDATA_VALIDATED 0x00000001
175
176 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
177 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
178
179 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
180 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
181
182 static const struct tok opcode[] = {
183 { LWRES_OPCODE_NOOP, "noop", },
184 { LWRES_OPCODE_GETADDRSBYNAME, "getaddrsbyname", },
185 { LWRES_OPCODE_GETNAMEBYADDR, "getnamebyaddr", },
186 { LWRES_OPCODE_GETRDATABYNAME, "getrdatabyname", },
187 { 0, NULL, },
188 };
189
190 /* print-domain.c */
191 extern const struct tok ns_type2str[];
192 extern const struct tok ns_class2str[];
193
194 static int
195 lwres_printname(netdissect_options *ndo,
196 size_t l, const u_char *p0)
197 {
198 const u_char *p;
199 size_t i;
200
201 p = p0;
202 /* + 1 for terminating \0 */
203 if (p + l + 1 > ndo->ndo_snapend)
204 goto trunc;
205
206 ND_PRINT(" ");
207 for (i = 0; i < l; i++) {
208 safeputchar(ndo, EXTRACT_U_1(p));
209 p++;
210 }
211 p++; /* skip terminating \0 */
212
213 return p - p0;
214
215 trunc:
216 return -1;
217 }
218
219 static int
220 lwres_printnamelen(netdissect_options *ndo,
221 const u_char *p)
222 {
223 uint16_t l;
224 int advance;
225
226 if (p + 2 > ndo->ndo_snapend)
227 goto trunc;
228 l = EXTRACT_BE_U_2(p);
229 advance = lwres_printname(ndo, l, p + 2);
230 if (advance < 0)
231 goto trunc;
232 return 2 + advance;
233
234 trunc:
235 return -1;
236 }
237
238 static int
239 lwres_printbinlen(netdissect_options *ndo,
240 const u_char *p0)
241 {
242 const u_char *p;
243 uint16_t l;
244 int i;
245
246 p = p0;
247 if (p + 2 > ndo->ndo_snapend)
248 goto trunc;
249 l = EXTRACT_BE_U_2(p);
250 if (p + 2 + l > ndo->ndo_snapend)
251 goto trunc;
252 p += 2;
253 for (i = 0; i < l; i++) {
254 ND_PRINT("%02x", EXTRACT_U_1(p));
255 p++;
256 }
257 return p - p0;
258
259 trunc:
260 return -1;
261 }
262
263 static int
264 lwres_printaddr(netdissect_options *ndo,
265 const u_char *p0)
266 {
267 const u_char *p;
268 const lwres_addr_t *ap;
269 uint16_t l;
270 int i;
271
272 p = p0;
273 ap = (const lwres_addr_t *)p;
274 ND_TCHECK_2(ap->length);
275 l = EXTRACT_BE_U_2(ap->length);
276 p += LWRES_ADDR_LEN;
277 ND_TCHECK_LEN(p, l);
278
279 switch (EXTRACT_BE_U_4(ap->family)) {
280 case 1: /* IPv4 */
281 if (l < 4)
282 return -1;
283 ND_PRINT(" %s", ipaddr_string(ndo, p));
284 p += sizeof(struct in_addr);
285 break;
286 case 2: /* IPv6 */
287 if (l < 16)
288 return -1;
289 ND_PRINT(" %s", ip6addr_string(ndo, p));
290 p += sizeof(struct in6_addr);
291 break;
292 default:
293 ND_PRINT(" %u/", EXTRACT_BE_U_4(ap->family));
294 for (i = 0; i < l; i++) {
295 ND_PRINT("%02x", EXTRACT_U_1(p));
296 p++;
297 }
298 }
299
300 return p - p0;
301
302 trunc:
303 return -1;
304 }
305
306 void
307 lwres_print(netdissect_options *ndo,
308 const u_char *bp, u_int length)
309 {
310 const u_char *p;
311 const struct lwres_lwpacket *np;
312 uint32_t v;
313 const u_char *s;
314 int response;
315 int advance;
316 int unsupported = 0;
317
318 np = (const struct lwres_lwpacket *)bp;
319 ND_TCHECK_2(np->authlength);
320
321 ND_PRINT(" lwres");
322 v = EXTRACT_BE_U_2(np->version);
323 if (ndo->ndo_vflag || v != LWRES_LWPACKETVERSION_0)
324 ND_PRINT(" v%u", v);
325 if (v != LWRES_LWPACKETVERSION_0) {
326 s = bp + EXTRACT_BE_U_4(np->length);
327 goto tail;
328 }
329
330 response = EXTRACT_BE_U_2(np->pktflags) & LWRES_LWPACKETFLAG_RESPONSE;
331
332 /* opcode and pktflags */
333 v = EXTRACT_BE_U_4(np->opcode);
334 ND_PRINT(" %s%s", tok2str(opcode, "#0x%x", v), response ? "" : "?");
335
336 /* pktflags */
337 v = EXTRACT_BE_U_2(np->pktflags);
338 if (v & ~LWRES_LWPACKETFLAG_RESPONSE)
339 ND_PRINT("[0x%x]", v);
340
341 if (ndo->ndo_vflag > 1) {
342 ND_PRINT(" ("); /*)*/
343 ND_PRINT("serial:0x%x", EXTRACT_BE_U_4(np->serial));
344 ND_PRINT(" result:0x%x", EXTRACT_BE_U_4(np->result));
345 ND_PRINT(" recvlen:%u", EXTRACT_BE_U_4(np->recvlength));
346 /* BIND910: not used */
347 if (ndo->ndo_vflag > 2) {
348 ND_PRINT(" authtype:0x%x", EXTRACT_BE_U_2(np->authtype));
349 ND_PRINT(" authlen:%u", EXTRACT_BE_U_2(np->authlength));
350 }
351 /*(*/
352 ND_PRINT(")");
353 }
354
355 /* per-opcode content */
356 if (!response) {
357 /*
358 * queries
359 */
360 const lwres_gabnrequest_t *gabn;
361 const lwres_gnbarequest_t *gnba;
362 const lwres_grbnrequest_t *grbn;
363 uint32_t l;
364
365 gabn = NULL;
366 gnba = NULL;
367 grbn = NULL;
368
369 p = (const u_char *)(np + 1);
370 switch (EXTRACT_BE_U_4(np->opcode)) {
371 case LWRES_OPCODE_NOOP:
372 s = p;
373 break;
374 case LWRES_OPCODE_GETADDRSBYNAME:
375 gabn = (const lwres_gabnrequest_t *)p;
376 ND_TCHECK_2(gabn->namelen);
377
378 /* BIND910: not used */
379 if (ndo->ndo_vflag > 2) {
380 ND_PRINT(" flags:0x%x",
381 EXTRACT_BE_U_4(gabn->flags));
382 }
383
384 v = EXTRACT_BE_U_4(gabn->addrtypes);
385 switch (v & (LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6)) {
386 case LWRES_ADDRTYPE_V4:
387 ND_PRINT(" IPv4");
388 break;
389 case LWRES_ADDRTYPE_V6:
390 ND_PRINT(" IPv6");
391 break;
392 case LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6:
393 ND_PRINT(" IPv4/6");
394 break;
395 }
396 if (v & ~(LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6))
397 ND_PRINT("[0x%x]", v);
398
399 s = p + LWRES_GABNREQUEST_LEN;
400 l = EXTRACT_BE_U_2(gabn->namelen);
401 advance = lwres_printname(ndo, l, s);
402 if (advance < 0)
403 goto trunc;
404 s += advance;
405 break;
406 case LWRES_OPCODE_GETNAMEBYADDR:
407 gnba = (const lwres_gnbarequest_t *)p;
408 ND_TCHECK_4(gnba->flags);
409
410 /* BIND910: not used */
411 if (ndo->ndo_vflag > 2) {
412 ND_PRINT(" flags:0x%x",
413 EXTRACT_BE_U_4(gnba->flags));
414 }
415
416 s = p + LWRES_GNBAREQUEST_LEN;
417 advance = lwres_printaddr(ndo, s);
418 if (advance < 0)
419 goto trunc;
420 s += advance;
421 break;
422 case LWRES_OPCODE_GETRDATABYNAME:
423 /* XXX no trace, not tested */
424 grbn = (const lwres_grbnrequest_t *)p;
425 ND_TCHECK_2(grbn->namelen);
426
427 /* BIND910: not used */
428 if (ndo->ndo_vflag > 2) {
429 ND_PRINT(" flags:0x%x",
430 EXTRACT_BE_U_4(grbn->flags));
431 }
432
433 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u",
434 EXTRACT_BE_U_2(grbn->rdtype)));
435 if (EXTRACT_BE_U_2(grbn->rdclass) != C_IN) {
436 ND_PRINT(" %s", tok2str(ns_class2str, "Class%u",
437 EXTRACT_BE_U_2(grbn->rdclass)));
438 }
439
440 s = p + LWRES_GRBNREQUEST_LEN;
441 l = EXTRACT_BE_U_2(grbn->namelen);
442 advance = lwres_printname(ndo, l, s);
443 if (advance < 0)
444 goto trunc;
445 s += advance;
446 break;
447 default:
448 s = p;
449 unsupported++;
450 break;
451 }
452 } else {
453 /*
454 * responses
455 */
456 const lwres_gabnresponse_t *gabn;
457 const lwres_gnbaresponse_t *gnba;
458 const lwres_grbnresponse_t *grbn;
459 uint32_t l, na;
460 uint32_t i;
461
462 gabn = NULL;
463 gnba = NULL;
464 grbn = NULL;
465
466 p = (const u_char *)(np + 1);
467 switch (EXTRACT_BE_U_4(np->opcode)) {
468 case LWRES_OPCODE_NOOP:
469 s = p;
470 break;
471 case LWRES_OPCODE_GETADDRSBYNAME:
472 gabn = (const lwres_gabnresponse_t *)p;
473 ND_TCHECK_2(gabn->realnamelen);
474
475 /* BIND910: not used */
476 if (ndo->ndo_vflag > 2) {
477 ND_PRINT(" flags:0x%x",
478 EXTRACT_BE_U_4(gabn->flags));
479 }
480
481 ND_PRINT(" %u/%u", EXTRACT_BE_U_2(gabn->naliases),
482 EXTRACT_BE_U_2(gabn->naddrs));
483
484 s = p + LWRES_GABNRESPONSE_LEN;
485 l = EXTRACT_BE_U_2(gabn->realnamelen);
486 advance = lwres_printname(ndo, l, s);
487 if (advance < 0)
488 goto trunc;
489 s += advance;
490
491 /* aliases */
492 na = EXTRACT_BE_U_2(gabn->naliases);
493 for (i = 0; i < na; i++) {
494 advance = lwres_printnamelen(ndo, s);
495 if (advance < 0)
496 goto trunc;
497 s += advance;
498 }
499
500 /* addrs */
501 na = EXTRACT_BE_U_2(gabn->naddrs);
502 for (i = 0; i < na; i++) {
503 advance = lwres_printaddr(ndo, s);
504 if (advance < 0)
505 goto trunc;
506 s += advance;
507 }
508 break;
509 case LWRES_OPCODE_GETNAMEBYADDR:
510 gnba = (const lwres_gnbaresponse_t *)p;
511 ND_TCHECK_2(gnba->realnamelen);
512
513 /* BIND910: not used */
514 if (ndo->ndo_vflag > 2) {
515 ND_PRINT(" flags:0x%x",
516 EXTRACT_BE_U_4(gnba->flags));
517 }
518
519 ND_PRINT(" %u", EXTRACT_BE_U_2(gnba->naliases));
520
521 s = p + LWRES_GNBARESPONSE_LEN;
522 l = EXTRACT_BE_U_2(gnba->realnamelen);
523 advance = lwres_printname(ndo, l, s);
524 if (advance < 0)
525 goto trunc;
526 s += advance;
527
528 /* aliases */
529 na = EXTRACT_BE_U_2(gnba->naliases);
530 for (i = 0; i < na; i++) {
531 advance = lwres_printnamelen(ndo, s);
532 if (advance < 0)
533 goto trunc;
534 s += advance;
535 }
536 break;
537 case LWRES_OPCODE_GETRDATABYNAME:
538 /* XXX no trace, not tested */
539 grbn = (const lwres_grbnresponse_t *)p;
540 ND_TCHECK_2(grbn->nsigs);
541
542 /* BIND910: not used */
543 if (ndo->ndo_vflag > 2) {
544 ND_PRINT(" flags:0x%x",
545 EXTRACT_BE_U_4(grbn->flags));
546 }
547
548 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u",
549 EXTRACT_BE_U_2(grbn->rdtype)));
550 if (EXTRACT_BE_U_2(grbn->rdclass) != C_IN) {
551 ND_PRINT(" %s", tok2str(ns_class2str, "Class%u",
552 EXTRACT_BE_U_2(grbn->rdclass)));
553 }
554 ND_PRINT(" TTL ");
555 unsigned_relts_print(ndo,
556 EXTRACT_BE_U_4(grbn->ttl));
557 ND_PRINT(" %u/%u", EXTRACT_BE_U_2(grbn->nrdatas),
558 EXTRACT_BE_U_2(grbn->nsigs));
559
560 s = p + LWRES_GRBNRESPONSE_LEN;
561 advance = lwres_printnamelen(ndo, s);
562 if (advance < 0)
563 goto trunc;
564 s += advance;
565
566 /* rdatas */
567 na = EXTRACT_BE_U_2(grbn->nrdatas);
568 for (i = 0; i < na; i++) {
569 /* XXX should decode resource data */
570 advance = lwres_printbinlen(ndo, s);
571 if (advance < 0)
572 goto trunc;
573 s += advance;
574 }
575
576 /* sigs */
577 na = EXTRACT_BE_U_2(grbn->nsigs);
578 for (i = 0; i < na; i++) {
579 /* XXX how should we print it? */
580 advance = lwres_printbinlen(ndo, s);
581 if (advance < 0)
582 goto trunc;
583 s += advance;
584 }
585 break;
586 default:
587 s = p;
588 unsupported++;
589 break;
590 }
591 }
592
593 tail:
594 /* length mismatch */
595 if (EXTRACT_BE_U_4(np->length) != length) {
596 ND_PRINT(" [len: %u != %u]", EXTRACT_BE_U_4(np->length),
597 length);
598 }
599 if (!unsupported && s < bp + EXTRACT_BE_U_4(np->length))
600 ND_PRINT("[extra]");
601 return;
602
603 trunc:
604 ND_PRINT("[|lwres]");
605 }