]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
f252275fc34ec8d54fbdcb7fbabbae3e3b11029e
[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 #define ND_LONGJMP_FROM_TCHECK
39 #include "netdissect.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 #include "nameser.h"
44
45 /* BIND9 lib/lwres/include/lwres */
46 /*
47 * Use nd_uint16_t for lwres_uint16_t
48 * Use nd_uint32_t for lwres_uint32_t
49 */
50
51 struct lwres_lwpacket {
52 nd_uint32_t length;
53 nd_uint16_t version;
54 nd_uint16_t pktflags;
55 nd_uint32_t serial;
56 nd_uint32_t opcode;
57 nd_uint32_t result;
58 nd_uint32_t recvlength;
59 nd_uint16_t authtype;
60 nd_uint16_t authlength;
61 };
62
63 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
64
65 #define LWRES_LWPACKETVERSION_0 0
66
67 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
68 #define LWRES_FLAG_SECUREDATA 0x00000002U
69
70 /*
71 * no-op
72 */
73 #define LWRES_OPCODE_NOOP 0x00000000U
74
75 typedef struct {
76 /* public */
77 nd_uint16_t datalength;
78 /* data follows */
79 } lwres_nooprequest_t;
80
81 typedef struct {
82 /* public */
83 nd_uint16_t datalength;
84 /* data follows */
85 } lwres_noopresponse_t;
86
87 /*
88 * get addresses by name
89 */
90 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
91
92 typedef struct lwres_addr lwres_addr_t;
93
94 struct lwres_addr {
95 nd_uint32_t family;
96 nd_uint16_t length;
97 /* address follows */
98 };
99 #define LWRES_ADDR_LEN 6
100
101 typedef struct {
102 /* public */
103 nd_uint32_t flags;
104 nd_uint32_t addrtypes;
105 nd_uint16_t namelen;
106 /* name follows */
107 } lwres_gabnrequest_t;
108 #define LWRES_GABNREQUEST_LEN 10
109
110 typedef struct {
111 /* public */
112 nd_uint32_t flags;
113 nd_uint16_t naliases;
114 nd_uint16_t naddrs;
115 nd_uint16_t realnamelen;
116 /* aliases follows */
117 /* addrs follows */
118 /* realname follows */
119 } lwres_gabnresponse_t;
120 #define LWRES_GABNRESPONSE_LEN 10
121
122 /*
123 * get name by address
124 */
125 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
126 typedef struct {
127 /* public */
128 nd_uint32_t flags;
129 /* addr follows */
130 } lwres_gnbarequest_t;
131 #define LWRES_GNBAREQUEST_LEN 4
132
133 typedef struct {
134 /* public */
135 nd_uint32_t flags;
136 nd_uint16_t naliases;
137 nd_uint16_t realnamelen;
138 /* aliases follows */
139 /* realname follows */
140 } lwres_gnbaresponse_t;
141 #define LWRES_GNBARESPONSE_LEN 8
142
143 /*
144 * get rdata by name
145 */
146 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
147
148 typedef struct {
149 /* public */
150 nd_uint32_t flags;
151 nd_uint16_t rdclass;
152 nd_uint16_t rdtype;
153 nd_uint16_t namelen;
154 /* name follows */
155 } lwres_grbnrequest_t;
156 #define LWRES_GRBNREQUEST_LEN 10
157
158 typedef struct {
159 /* public */
160 nd_uint32_t flags;
161 nd_uint16_t rdclass;
162 nd_uint16_t rdtype;
163 nd_uint32_t ttl;
164 nd_uint16_t nrdatas;
165 nd_uint16_t nsigs;
166 /* realname here (len + name) */
167 /* rdata here (len + name) */
168 /* signatures here (len + name) */
169 } lwres_grbnresponse_t;
170 #define LWRES_GRBNRESPONSE_LEN 16
171
172 #define LWRDATA_VALIDATED 0x00000001
173
174 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
175 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
176
177 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
178 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
179
180 static const struct tok opcode[] = {
181 { LWRES_OPCODE_NOOP, "noop", },
182 { LWRES_OPCODE_GETADDRSBYNAME, "getaddrsbyname", },
183 { LWRES_OPCODE_GETNAMEBYADDR, "getnamebyaddr", },
184 { LWRES_OPCODE_GETRDATABYNAME, "getrdatabyname", },
185 { 0, NULL, },
186 };
187
188 /* print-domain.c */
189 extern const struct tok ns_type2str[];
190 extern const struct tok ns_class2str[];
191
192 static unsigned
193 lwres_printname(netdissect_options *ndo,
194 u_int l, const u_char *p0)
195 {
196 ND_PRINT(" ");
197 nd_printjn(ndo, p0, l);
198 p0 += l;
199 if (GET_U_1(p0))
200 ND_PRINT(" (not NUL-terminated!)");
201 return l + 1;
202 }
203
204 static unsigned
205 lwres_printnamelen(netdissect_options *ndo,
206 const u_char *p)
207 {
208 uint16_t l;
209 int advance;
210
211 l = GET_BE_U_2(p);
212 advance = lwres_printname(ndo, l, p + 2);
213 return 2 + advance;
214 }
215
216 static unsigned
217 lwres_printbinlen(netdissect_options *ndo,
218 const u_char *p0)
219 {
220 const u_char *p;
221 uint16_t l;
222 int i;
223
224 p = p0;
225 l = GET_BE_U_2(p);
226 p += 2;
227 for (i = 0; i < l; i++) {
228 ND_PRINT("%02x", GET_U_1(p));
229 p++;
230 }
231 return 2 + l;
232 }
233
234 static int
235 lwres_printaddr(netdissect_options *ndo,
236 const u_char *p0)
237 {
238 const u_char *p;
239 const lwres_addr_t *ap;
240 uint16_t l;
241 int i;
242
243 p = p0;
244 ap = (const lwres_addr_t *)p;
245 l = GET_BE_U_2(ap->length);
246 p += LWRES_ADDR_LEN;
247 ND_TCHECK_LEN(p, l);
248
249 switch (GET_BE_U_4(ap->family)) {
250 case 1: /* IPv4 */
251 if (l < 4)
252 return -1;
253 ND_PRINT(" %s", GET_IPADDR_STRING(p));
254 p += sizeof(nd_ipv4);
255 break;
256 case 2: /* IPv6 */
257 if (l < 16)
258 return -1;
259 ND_PRINT(" %s", GET_IP6ADDR_STRING(p));
260 p += sizeof(nd_ipv6);
261 break;
262 default:
263 ND_PRINT(" %u/", GET_BE_U_4(ap->family));
264 for (i = 0; i < l; i++) {
265 ND_PRINT("%02x", GET_U_1(p));
266 p++;
267 }
268 }
269
270 return ND_BYTES_BETWEEN(p0, p);
271 }
272
273 void
274 lwres_print(netdissect_options *ndo,
275 const u_char *bp, u_int length)
276 {
277 const u_char *p;
278 const struct lwres_lwpacket *np;
279 uint32_t v;
280 const u_char *s;
281 int response;
282 int advance;
283 int unsupported = 0;
284
285 ndo->ndo_protocol = "lwres";
286 np = (const struct lwres_lwpacket *)bp;
287 ND_TCHECK_2(np->authlength);
288
289 ND_PRINT(" lwres");
290 v = GET_BE_U_2(np->version);
291 if (ndo->ndo_vflag || v != LWRES_LWPACKETVERSION_0)
292 ND_PRINT(" v%u", v);
293 if (v != LWRES_LWPACKETVERSION_0) {
294 uint32_t pkt_len = GET_BE_U_4(np->length);
295 ND_TCHECK_LEN(bp, pkt_len);
296 s = bp + pkt_len;
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 && ND_BYTES_BETWEEN(bp, s) < GET_BE_U_4(np->length))
552 ND_PRINT("[extra]");
553 return;
554
555 invalid:
556 nd_print_invalid(ndo);
557 }