]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
b3afcc77e6de8125c65d6a6b7d44f02da743af2d
[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
40 #define ND_LONGJMP_FROM_TCHECK
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 unsigned
195 lwres_printname(netdissect_options *ndo,
196 size_t l, const u_char *p0)
197 {
198 ND_PRINT(" ");
199 (void)nd_printn(ndo, p0, l, NULL);
200 p0 += l;
201 if (GET_U_1(p0))
202 ND_PRINT(" (not NUL-terminated!)");
203 return l + 1;
204 }
205
206 static unsigned
207 lwres_printnamelen(netdissect_options *ndo,
208 const u_char *p)
209 {
210 uint16_t l;
211 int advance;
212
213 l = GET_BE_U_2(p);
214 advance = lwres_printname(ndo, l, p + 2);
215 return 2 + advance;
216 }
217
218 static unsigned
219 lwres_printbinlen(netdissect_options *ndo,
220 const u_char *p0)
221 {
222 const u_char *p;
223 uint16_t l;
224 int i;
225
226 p = p0;
227 l = GET_BE_U_2(p);
228 p += 2;
229 for (i = 0; i < l; i++) {
230 ND_PRINT("%02x", GET_U_1(p));
231 p++;
232 }
233 return 2 + l;
234 }
235
236 static int
237 lwres_printaddr(netdissect_options *ndo,
238 const u_char *p0)
239 {
240 const u_char *p;
241 const lwres_addr_t *ap;
242 uint16_t l;
243 int i;
244
245 p = p0;
246 ap = (const lwres_addr_t *)p;
247 l = GET_BE_U_2(ap->length);
248 p += LWRES_ADDR_LEN;
249 ND_TCHECK_LEN(p, l);
250
251 switch (GET_BE_U_4(ap->family)) {
252 case 1: /* IPv4 */
253 if (l < 4)
254 return -1;
255 ND_PRINT(" %s", GET_IPADDR_STRING(p));
256 p += sizeof(nd_ipv4);
257 break;
258 case 2: /* IPv6 */
259 if (l < 16)
260 return -1;
261 ND_PRINT(" %s", GET_IP6ADDR_STRING(p));
262 p += sizeof(nd_ipv6);
263 break;
264 default:
265 ND_PRINT(" %u/", GET_BE_U_4(ap->family));
266 for (i = 0; i < l; i++) {
267 ND_PRINT("%02x", GET_U_1(p));
268 p++;
269 }
270 }
271
272 return ND_BYTES_BETWEEN(p, p0);
273 }
274
275 void
276 lwres_print(netdissect_options *ndo,
277 const u_char *bp, u_int length)
278 {
279 const u_char *p;
280 const struct lwres_lwpacket *np;
281 uint32_t v;
282 const u_char *s;
283 int response;
284 int advance;
285 int unsupported = 0;
286
287 ndo->ndo_protocol = "lwres";
288 np = (const struct lwres_lwpacket *)bp;
289 ND_TCHECK_2(np->authlength);
290
291 ND_PRINT(" lwres");
292 v = GET_BE_U_2(np->version);
293 if (ndo->ndo_vflag || v != LWRES_LWPACKETVERSION_0)
294 ND_PRINT(" v%u", v);
295 if (v != LWRES_LWPACKETVERSION_0) {
296 s = bp + GET_BE_U_4(np->length);
297 goto tail;
298 }
299
300 response = GET_BE_U_2(np->pktflags) & LWRES_LWPACKETFLAG_RESPONSE;
301
302 /* opcode and pktflags */
303 v = GET_BE_U_4(np->opcode);
304 ND_PRINT(" %s%s", tok2str(opcode, "#0x%x", v), response ? "" : "?");
305
306 /* pktflags */
307 v = GET_BE_U_2(np->pktflags);
308 if (v & ~LWRES_LWPACKETFLAG_RESPONSE)
309 ND_PRINT("[0x%x]", v);
310
311 if (ndo->ndo_vflag > 1) {
312 ND_PRINT(" ("); /*)*/
313 ND_PRINT("serial:0x%x", GET_BE_U_4(np->serial));
314 ND_PRINT(" result:0x%x", GET_BE_U_4(np->result));
315 ND_PRINT(" recvlen:%u", GET_BE_U_4(np->recvlength));
316 /* BIND910: not used */
317 if (ndo->ndo_vflag > 2) {
318 ND_PRINT(" authtype:0x%x", GET_BE_U_2(np->authtype));
319 ND_PRINT(" authlen:%u", GET_BE_U_2(np->authlength));
320 }
321 /*(*/
322 ND_PRINT(")");
323 }
324
325 /* per-opcode content */
326 if (!response) {
327 /*
328 * queries
329 */
330 const lwres_gabnrequest_t *gabn;
331 const lwres_gnbarequest_t *gnba;
332 const lwres_grbnrequest_t *grbn;
333 uint32_t l;
334
335 gabn = NULL;
336 gnba = NULL;
337 grbn = NULL;
338
339 p = (const u_char *)(np + 1);
340 switch (GET_BE_U_4(np->opcode)) {
341 case LWRES_OPCODE_NOOP:
342 s = p;
343 break;
344 case LWRES_OPCODE_GETADDRSBYNAME:
345 gabn = (const lwres_gabnrequest_t *)p;
346 ND_TCHECK_2(gabn->namelen);
347
348 /* BIND910: not used */
349 if (ndo->ndo_vflag > 2) {
350 ND_PRINT(" flags:0x%x",
351 GET_BE_U_4(gabn->flags));
352 }
353
354 v = GET_BE_U_4(gabn->addrtypes);
355 switch (v & (LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6)) {
356 case LWRES_ADDRTYPE_V4:
357 ND_PRINT(" IPv4");
358 break;
359 case LWRES_ADDRTYPE_V6:
360 ND_PRINT(" IPv6");
361 break;
362 case LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6:
363 ND_PRINT(" IPv4/6");
364 break;
365 }
366 if (v & ~(LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6))
367 ND_PRINT("[0x%x]", v);
368
369 s = p + LWRES_GABNREQUEST_LEN;
370 l = GET_BE_U_2(gabn->namelen);
371 advance = lwres_printname(ndo, l, s);
372 s += advance;
373 break;
374 case LWRES_OPCODE_GETNAMEBYADDR:
375 gnba = (const lwres_gnbarequest_t *)p;
376 ND_TCHECK_4(gnba->flags);
377
378 /* BIND910: not used */
379 if (ndo->ndo_vflag > 2) {
380 ND_PRINT(" flags:0x%x",
381 GET_BE_U_4(gnba->flags));
382 }
383
384 s = p + LWRES_GNBAREQUEST_LEN;
385 advance = lwres_printaddr(ndo, s);
386 if (advance < 0)
387 goto invalid;
388 s += advance;
389 break;
390 case LWRES_OPCODE_GETRDATABYNAME:
391 /* XXX no trace, not tested */
392 grbn = (const lwres_grbnrequest_t *)p;
393 ND_TCHECK_2(grbn->namelen);
394
395 /* BIND910: not used */
396 if (ndo->ndo_vflag > 2) {
397 ND_PRINT(" flags:0x%x",
398 GET_BE_U_4(grbn->flags));
399 }
400
401 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u",
402 GET_BE_U_2(grbn->rdtype)));
403 if (GET_BE_U_2(grbn->rdclass) != C_IN) {
404 ND_PRINT(" %s", tok2str(ns_class2str, "Class%u",
405 GET_BE_U_2(grbn->rdclass)));
406 }
407
408 s = p + LWRES_GRBNREQUEST_LEN;
409 l = GET_BE_U_2(grbn->namelen);
410 advance = lwres_printname(ndo, l, s);
411 s += advance;
412 break;
413 default:
414 s = p;
415 unsupported++;
416 break;
417 }
418 } else {
419 /*
420 * responses
421 */
422 const lwres_gabnresponse_t *gabn;
423 const lwres_gnbaresponse_t *gnba;
424 const lwres_grbnresponse_t *grbn;
425 uint32_t l, na;
426 uint32_t i;
427
428 gabn = NULL;
429 gnba = NULL;
430 grbn = NULL;
431
432 p = (const u_char *)(np + 1);
433 switch (GET_BE_U_4(np->opcode)) {
434 case LWRES_OPCODE_NOOP:
435 s = p;
436 break;
437 case LWRES_OPCODE_GETADDRSBYNAME:
438 gabn = (const lwres_gabnresponse_t *)p;
439 ND_TCHECK_2(gabn->realnamelen);
440
441 /* BIND910: not used */
442 if (ndo->ndo_vflag > 2) {
443 ND_PRINT(" flags:0x%x",
444 GET_BE_U_4(gabn->flags));
445 }
446
447 ND_PRINT(" %u/%u", GET_BE_U_2(gabn->naliases),
448 GET_BE_U_2(gabn->naddrs));
449
450 s = p + LWRES_GABNRESPONSE_LEN;
451 l = GET_BE_U_2(gabn->realnamelen);
452 advance = lwres_printname(ndo, l, s);
453 s += advance;
454
455 /* aliases */
456 na = GET_BE_U_2(gabn->naliases);
457 for (i = 0; i < na; i++) {
458 advance = lwres_printnamelen(ndo, s);
459 s += advance;
460 }
461
462 /* addrs */
463 na = GET_BE_U_2(gabn->naddrs);
464 for (i = 0; i < na; i++) {
465 advance = lwres_printaddr(ndo, s);
466 if (advance < 0)
467 goto invalid;
468 s += advance;
469 }
470 break;
471 case LWRES_OPCODE_GETNAMEBYADDR:
472 gnba = (const lwres_gnbaresponse_t *)p;
473 ND_TCHECK_2(gnba->realnamelen);
474
475 /* BIND910: not used */
476 if (ndo->ndo_vflag > 2) {
477 ND_PRINT(" flags:0x%x",
478 GET_BE_U_4(gnba->flags));
479 }
480
481 ND_PRINT(" %u", GET_BE_U_2(gnba->naliases));
482
483 s = p + LWRES_GNBARESPONSE_LEN;
484 l = GET_BE_U_2(gnba->realnamelen);
485 advance = lwres_printname(ndo, l, s);
486 s += advance;
487
488 /* aliases */
489 na = GET_BE_U_2(gnba->naliases);
490 for (i = 0; i < na; i++) {
491 advance = lwres_printnamelen(ndo, s);
492 s += advance;
493 }
494 break;
495 case LWRES_OPCODE_GETRDATABYNAME:
496 /* XXX no trace, not tested */
497 grbn = (const lwres_grbnresponse_t *)p;
498 ND_TCHECK_2(grbn->nsigs);
499
500 /* BIND910: not used */
501 if (ndo->ndo_vflag > 2) {
502 ND_PRINT(" flags:0x%x",
503 GET_BE_U_4(grbn->flags));
504 }
505
506 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u",
507 GET_BE_U_2(grbn->rdtype)));
508 if (GET_BE_U_2(grbn->rdclass) != C_IN) {
509 ND_PRINT(" %s", tok2str(ns_class2str, "Class%u",
510 GET_BE_U_2(grbn->rdclass)));
511 }
512 ND_PRINT(" TTL ");
513 unsigned_relts_print(ndo,
514 GET_BE_U_4(grbn->ttl));
515 ND_PRINT(" %u/%u", GET_BE_U_2(grbn->nrdatas),
516 GET_BE_U_2(grbn->nsigs));
517
518 s = p + LWRES_GRBNRESPONSE_LEN;
519 advance = lwres_printnamelen(ndo, s);
520 s += advance;
521
522 /* rdatas */
523 na = GET_BE_U_2(grbn->nrdatas);
524 for (i = 0; i < na; i++) {
525 /* XXX should decode resource data */
526 advance = lwres_printbinlen(ndo, s);
527 s += advance;
528 }
529
530 /* sigs */
531 na = GET_BE_U_2(grbn->nsigs);
532 for (i = 0; i < na; i++) {
533 /* XXX how should we print it? */
534 advance = lwres_printbinlen(ndo, s);
535 s += advance;
536 }
537 break;
538 default:
539 s = p;
540 unsupported++;
541 break;
542 }
543 }
544
545 tail:
546 /* length mismatch */
547 if (GET_BE_U_4(np->length) != length) {
548 ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np->length),
549 length);
550 }
551 if (!unsupported && s < bp + GET_BE_U_4(np->length))
552 ND_PRINT("[extra]");
553 return;
554
555 invalid:
556 nd_print_invalid(ndo);
557 }