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