]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
updated version
[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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <netdissect-stdinc.h>
35
36 #include "nameser.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 /* BIND9 lib/lwres/include/lwres */
46 typedef uint32_t lwres_uint32_t;
47 typedef uint16_t lwres_uint16_t;
48 typedef uint8_t lwres_uint8_t;
49
50 struct lwres_lwpacket {
51 lwres_uint32_t length;
52 lwres_uint16_t version;
53 lwres_uint16_t pktflags;
54 lwres_uint32_t serial;
55 lwres_uint32_t opcode;
56 lwres_uint32_t result;
57 lwres_uint32_t recvlength;
58 lwres_uint16_t authtype;
59 lwres_uint16_t authlength;
60 };
61
62 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
63
64 #define LWRES_LWPACKETVERSION_0 0
65
66 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
67 #define LWRES_FLAG_SECUREDATA 0x00000002U
68
69 /*
70 * no-op
71 */
72 #define LWRES_OPCODE_NOOP 0x00000000U
73
74 typedef struct {
75 /* public */
76 lwres_uint16_t datalength;
77 /* data follows */
78 } lwres_nooprequest_t;
79
80 typedef struct {
81 /* public */
82 lwres_uint16_t datalength;
83 /* data follows */
84 } lwres_noopresponse_t;
85
86 /*
87 * get addresses by name
88 */
89 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
90
91 typedef struct lwres_addr lwres_addr_t;
92
93 struct lwres_addr {
94 lwres_uint32_t family;
95 lwres_uint16_t length;
96 /* address folows */
97 };
98
99 typedef struct {
100 /* public */
101 lwres_uint32_t flags;
102 lwres_uint32_t addrtypes;
103 lwres_uint16_t namelen;
104 /* name follows */
105 } lwres_gabnrequest_t;
106
107 typedef struct {
108 /* public */
109 lwres_uint32_t flags;
110 lwres_uint16_t naliases;
111 lwres_uint16_t naddrs;
112 lwres_uint16_t realnamelen;
113 /* aliases follows */
114 /* addrs follows */
115 /* realname follows */
116 } lwres_gabnresponse_t;
117
118 /*
119 * get name by address
120 */
121 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
122 typedef struct {
123 /* public */
124 lwres_uint32_t flags;
125 lwres_addr_t addr;
126 /* addr body follows */
127 } lwres_gnbarequest_t;
128
129 typedef struct {
130 /* public */
131 lwres_uint32_t flags;
132 lwres_uint16_t naliases;
133 lwres_uint16_t realnamelen;
134 /* aliases follows */
135 /* realname follows */
136 } lwres_gnbaresponse_t;
137
138 /*
139 * get rdata by name
140 */
141 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
142
143 typedef struct {
144 /* public */
145 lwres_uint32_t flags;
146 lwres_uint16_t rdclass;
147 lwres_uint16_t rdtype;
148 lwres_uint16_t namelen;
149 /* name follows */
150 } lwres_grbnrequest_t;
151
152 typedef struct {
153 /* public */
154 lwres_uint32_t flags;
155 lwres_uint16_t rdclass;
156 lwres_uint16_t rdtype;
157 lwres_uint32_t ttl;
158 lwres_uint16_t nrdatas;
159 lwres_uint16_t nsigs;
160 /* realname here (len + name) */
161 /* rdata here (len + name) */
162 /* signatures here (len + name) */
163 } lwres_grbnresponse_t;
164
165 #define LWRDATA_VALIDATED 0x00000001
166
167 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
168 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
169
170 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
171 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
172
173 static const struct tok opcode[] = {
174 { LWRES_OPCODE_NOOP, "noop", },
175 { LWRES_OPCODE_GETADDRSBYNAME, "getaddrsbyname", },
176 { LWRES_OPCODE_GETNAMEBYADDR, "getnamebyaddr", },
177 { LWRES_OPCODE_GETRDATABYNAME, "getrdatabyname", },
178 { 0, NULL, },
179 };
180
181 /* print-domain.c */
182 extern const struct tok ns_type2str[];
183 extern const struct tok ns_class2str[];
184
185 static int
186 lwres_printname(netdissect_options *ndo,
187 size_t l, const char *p0)
188 {
189 const char *p;
190 size_t i;
191
192 p = p0;
193 /* + 1 for terminating \0 */
194 if (p + l + 1 > (const char *)ndo->ndo_snapend)
195 goto trunc;
196
197 ND_PRINT((ndo, " "));
198 for (i = 0; i < l; i++)
199 safeputchar(ndo, *p++);
200 p++; /* skip terminating \0 */
201
202 return p - p0;
203
204 trunc:
205 return -1;
206 }
207
208 static int
209 lwres_printnamelen(netdissect_options *ndo,
210 const char *p)
211 {
212 uint16_t l;
213 int advance;
214
215 if (p + 2 > (const char *)ndo->ndo_snapend)
216 goto trunc;
217 l = EXTRACT_16BITS(p);
218 advance = lwres_printname(ndo, l, p + 2);
219 if (advance < 0)
220 goto trunc;
221 return 2 + advance;
222
223 trunc:
224 return -1;
225 }
226
227 static int
228 lwres_printbinlen(netdissect_options *ndo,
229 const char *p0)
230 {
231 const char *p;
232 uint16_t l;
233 int i;
234
235 p = p0;
236 if (p + 2 > (const char *)ndo->ndo_snapend)
237 goto trunc;
238 l = EXTRACT_16BITS(p);
239 if (p + 2 + l > (const char *)ndo->ndo_snapend)
240 goto trunc;
241 p += 2;
242 for (i = 0; i < l; i++)
243 ND_PRINT((ndo, "%02x", *p++));
244 return p - p0;
245
246 trunc:
247 return -1;
248 }
249
250 static int
251 lwres_printaddr(netdissect_options *ndo,
252 const lwres_addr_t *ap)
253 {
254 uint16_t l;
255 const char *p;
256 int i;
257
258 ND_TCHECK(ap->length);
259 l = EXTRACT_16BITS(&ap->length);
260 /* XXX ap points to packed struct */
261 p = (const char *)&ap->length + sizeof(ap->length);
262 ND_TCHECK2(*p, l);
263
264 switch (EXTRACT_32BITS(&ap->family)) {
265 case 1: /* IPv4 */
266 if (l < 4)
267 return -1;
268 ND_PRINT((ndo, " %s", ipaddr_string(ndo, p)));
269 p += sizeof(struct in_addr);
270 break;
271 case 2: /* IPv6 */
272 if (l < 16)
273 return -1;
274 ND_PRINT((ndo, " %s", ip6addr_string(ndo, p)));
275 p += sizeof(struct in6_addr);
276 break;
277 default:
278 ND_PRINT((ndo, " %u/", EXTRACT_32BITS(&ap->family)));
279 for (i = 0; i < l; i++)
280 ND_PRINT((ndo, "%02x", *p++));
281 }
282
283 return p - (const char *)ap;
284
285 trunc:
286 return -1;
287 }
288
289 void
290 lwres_print(netdissect_options *ndo,
291 register const u_char *bp, u_int length)
292 {
293 const struct lwres_lwpacket *np;
294 uint32_t v;
295 const char *s;
296 int response;
297 int advance;
298 int unsupported = 0;
299
300 np = (const struct lwres_lwpacket *)bp;
301 ND_TCHECK(np->authlength);
302
303 ND_PRINT((ndo, " lwres"));
304 v = EXTRACT_16BITS(&np->version);
305 if (ndo->ndo_vflag || v != LWRES_LWPACKETVERSION_0)
306 ND_PRINT((ndo, " v%u", v));
307 if (v != LWRES_LWPACKETVERSION_0) {
308 s = (const char *)np + EXTRACT_32BITS(&np->length);
309 goto tail;
310 }
311
312 response = EXTRACT_16BITS(&np->pktflags) & LWRES_LWPACKETFLAG_RESPONSE;
313
314 /* opcode and pktflags */
315 v = EXTRACT_32BITS(&np->opcode);
316 s = tok2str(opcode, "#0x%x", v);
317 ND_PRINT((ndo, " %s%s", s, response ? "" : "?"));
318
319 /* pktflags */
320 v = EXTRACT_16BITS(&np->pktflags);
321 if (v & ~LWRES_LWPACKETFLAG_RESPONSE)
322 ND_PRINT((ndo, "[0x%x]", v));
323
324 if (ndo->ndo_vflag > 1) {
325 ND_PRINT((ndo, " (")); /*)*/
326 ND_PRINT((ndo, "serial:0x%x", EXTRACT_32BITS(&np->serial)));
327 ND_PRINT((ndo, " result:0x%x", EXTRACT_32BITS(&np->result)));
328 ND_PRINT((ndo, " recvlen:%u", EXTRACT_32BITS(&np->recvlength)));
329 /* BIND910: not used */
330 if (ndo->ndo_vflag > 2) {
331 ND_PRINT((ndo, " authtype:0x%x", EXTRACT_16BITS(&np->authtype)));
332 ND_PRINT((ndo, " authlen:%u", EXTRACT_16BITS(&np->authlength)));
333 }
334 /*(*/
335 ND_PRINT((ndo, ")"));
336 }
337
338 /* per-opcode content */
339 if (!response) {
340 /*
341 * queries
342 */
343 const lwres_gabnrequest_t *gabn;
344 const lwres_gnbarequest_t *gnba;
345 const lwres_grbnrequest_t *grbn;
346 uint32_t l;
347
348 gabn = NULL;
349 gnba = NULL;
350 grbn = NULL;
351
352 switch (EXTRACT_32BITS(&np->opcode)) {
353 case LWRES_OPCODE_NOOP:
354 break;
355 case LWRES_OPCODE_GETADDRSBYNAME:
356 gabn = (const lwres_gabnrequest_t *)(np + 1);
357 ND_TCHECK(gabn->namelen);
358 /* XXX gabn points to packed struct */
359 s = (const char *)&gabn->namelen +
360 sizeof(gabn->namelen);
361 l = EXTRACT_16BITS(&gabn->namelen);
362
363 /* BIND910: not used */
364 if (ndo->ndo_vflag > 2) {
365 ND_PRINT((ndo, " flags:0x%x",
366 EXTRACT_32BITS(&gabn->flags)));
367 }
368
369 v = EXTRACT_32BITS(&gabn->addrtypes);
370 switch (v & (LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6)) {
371 case LWRES_ADDRTYPE_V4:
372 ND_PRINT((ndo, " IPv4"));
373 break;
374 case LWRES_ADDRTYPE_V6:
375 ND_PRINT((ndo, " IPv6"));
376 break;
377 case LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6:
378 ND_PRINT((ndo, " IPv4/6"));
379 break;
380 }
381 if (v & ~(LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6))
382 ND_PRINT((ndo, "[0x%x]", v));
383
384 advance = lwres_printname(ndo, l, s);
385 if (advance < 0)
386 goto trunc;
387 s += advance;
388 break;
389 case LWRES_OPCODE_GETNAMEBYADDR:
390 gnba = (const lwres_gnbarequest_t *)(np + 1);
391 ND_TCHECK(gnba->addr);
392
393 /* BIND910: not used */
394 if (ndo->ndo_vflag > 2) {
395 ND_PRINT((ndo, " flags:0x%x",
396 EXTRACT_32BITS(&gnba->flags)));
397 }
398
399 s = (const char *)&gnba->addr;
400
401 advance = lwres_printaddr(ndo, &gnba->addr);
402 if (advance < 0)
403 goto trunc;
404 s += advance;
405 break;
406 case LWRES_OPCODE_GETRDATABYNAME:
407 /* XXX no trace, not tested */
408 grbn = (const lwres_grbnrequest_t *)(np + 1);
409 ND_TCHECK(grbn->namelen);
410
411 /* BIND910: not used */
412 if (ndo->ndo_vflag > 2) {
413 ND_PRINT((ndo, " flags:0x%x",
414 EXTRACT_32BITS(&grbn->flags)));
415 }
416
417 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d",
418 EXTRACT_16BITS(&grbn->rdtype))));
419 if (EXTRACT_16BITS(&grbn->rdclass) != C_IN) {
420 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "Class%d",
421 EXTRACT_16BITS(&grbn->rdclass))));
422 }
423
424 /* XXX grbn points to packed struct */
425 s = (const char *)&grbn->namelen +
426 sizeof(grbn->namelen);
427 l = EXTRACT_16BITS(&grbn->namelen);
428
429 advance = lwres_printname(ndo, l, s);
430 if (advance < 0)
431 goto trunc;
432 s += advance;
433 break;
434 default:
435 unsupported++;
436 break;
437 }
438 } else {
439 /*
440 * responses
441 */
442 const lwres_gabnresponse_t *gabn;
443 const lwres_gnbaresponse_t *gnba;
444 const lwres_grbnresponse_t *grbn;
445 uint32_t l, na;
446 uint32_t i;
447
448 gabn = NULL;
449 gnba = NULL;
450 grbn = NULL;
451
452 switch (EXTRACT_32BITS(&np->opcode)) {
453 case LWRES_OPCODE_NOOP:
454 break;
455 case LWRES_OPCODE_GETADDRSBYNAME:
456 gabn = (const lwres_gabnresponse_t *)(np + 1);
457 ND_TCHECK(gabn->realnamelen);
458 /* XXX gabn points to packed struct */
459 s = (const char *)&gabn->realnamelen +
460 sizeof(gabn->realnamelen);
461 l = EXTRACT_16BITS(&gabn->realnamelen);
462
463 /* BIND910: not used */
464 if (ndo->ndo_vflag > 2) {
465 ND_PRINT((ndo, " flags:0x%x",
466 EXTRACT_32BITS(&gabn->flags)));
467 }
468
469 ND_PRINT((ndo, " %u/%u", EXTRACT_16BITS(&gabn->naliases),
470 EXTRACT_16BITS(&gabn->naddrs)));
471
472 advance = lwres_printname(ndo, l, s);
473 if (advance < 0)
474 goto trunc;
475 s += advance;
476
477 /* aliases */
478 na = EXTRACT_16BITS(&gabn->naliases);
479 for (i = 0; i < na; i++) {
480 advance = lwres_printnamelen(ndo, s);
481 if (advance < 0)
482 goto trunc;
483 s += advance;
484 }
485
486 /* addrs */
487 na = EXTRACT_16BITS(&gabn->naddrs);
488 for (i = 0; i < na; i++) {
489 advance = lwres_printaddr(ndo, (const lwres_addr_t *)s);
490 if (advance < 0)
491 goto trunc;
492 s += advance;
493 }
494 break;
495 case LWRES_OPCODE_GETNAMEBYADDR:
496 gnba = (const lwres_gnbaresponse_t *)(np + 1);
497 ND_TCHECK(gnba->realnamelen);
498 /* XXX gnba points to packed struct */
499 s = (const char *)&gnba->realnamelen +
500 sizeof(gnba->realnamelen);
501 l = EXTRACT_16BITS(&gnba->realnamelen);
502
503 /* BIND910: not used */
504 if (ndo->ndo_vflag > 2) {
505 ND_PRINT((ndo, " flags:0x%x",
506 EXTRACT_32BITS(&gnba->flags)));
507 }
508
509 ND_PRINT((ndo, " %u", EXTRACT_16BITS(&gnba->naliases)));
510
511 advance = lwres_printname(ndo, l, s);
512 if (advance < 0)
513 goto trunc;
514 s += advance;
515
516 /* aliases */
517 na = EXTRACT_16BITS(&gnba->naliases);
518 for (i = 0; i < na; i++) {
519 advance = lwres_printnamelen(ndo, s);
520 if (advance < 0)
521 goto trunc;
522 s += advance;
523 }
524 break;
525 case LWRES_OPCODE_GETRDATABYNAME:
526 /* XXX no trace, not tested */
527 grbn = (const lwres_grbnresponse_t *)(np + 1);
528 ND_TCHECK(grbn->nsigs);
529
530 /* BIND910: not used */
531 if (ndo->ndo_vflag > 2) {
532 ND_PRINT((ndo, " flags:0x%x",
533 EXTRACT_32BITS(&grbn->flags)));
534 }
535
536 ND_PRINT((ndo, " %s", tok2str(ns_type2str, "Type%d",
537 EXTRACT_16BITS(&grbn->rdtype))));
538 if (EXTRACT_16BITS(&grbn->rdclass) != C_IN) {
539 ND_PRINT((ndo, " %s", tok2str(ns_class2str, "Class%d",
540 EXTRACT_16BITS(&grbn->rdclass))));
541 }
542 ND_PRINT((ndo, " TTL "));
543 relts_print(ndo, EXTRACT_32BITS(&grbn->ttl));
544 ND_PRINT((ndo, " %u/%u", EXTRACT_16BITS(&grbn->nrdatas),
545 EXTRACT_16BITS(&grbn->nsigs)));
546
547 /* XXX grbn points to packed struct */
548 s = (const char *)&grbn->nsigs+ sizeof(grbn->nsigs);
549
550 advance = lwres_printnamelen(ndo, s);
551 if (advance < 0)
552 goto trunc;
553 s += advance;
554
555 /* rdatas */
556 na = EXTRACT_16BITS(&grbn->nrdatas);
557 for (i = 0; i < na; i++) {
558 /* XXX should decode resource data */
559 advance = lwres_printbinlen(ndo, s);
560 if (advance < 0)
561 goto trunc;
562 s += advance;
563 }
564
565 /* sigs */
566 na = EXTRACT_16BITS(&grbn->nsigs);
567 for (i = 0; i < na; i++) {
568 /* XXX how should we print it? */
569 advance = lwres_printbinlen(ndo, s);
570 if (advance < 0)
571 goto trunc;
572 s += advance;
573 }
574 break;
575 default:
576 unsupported++;
577 break;
578 }
579 }
580
581 tail:
582 /* length mismatch */
583 if (EXTRACT_32BITS(&np->length) != length) {
584 ND_PRINT((ndo, " [len: %u != %u]", EXTRACT_32BITS(&np->length),
585 length));
586 }
587 if (!unsupported && s < (const char *)np + EXTRACT_32BITS(&np->length))
588 ND_PRINT((ndo, "[extra]"));
589 return;
590
591 trunc:
592 ND_PRINT((ndo, "[|lwres]"));
593 }