]> The Tcpdump Group git mirrors - tcpdump/blob - print-lwres.c
More bounds checking when fetching addresses and converting to strings.
[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 fn_print_char(ndo, GET_U_1(p));
209 p++;
210 }
211 p++; /* skip terminating \0 */
212
213 return ND_BYTES_BETWEEN(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 = GET_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 = GET_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", GET_U_1(p));
255 p++;
256 }
257 return ND_BYTES_BETWEEN(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 = GET_BE_U_2(ap->length);
276 p += LWRES_ADDR_LEN;
277 ND_TCHECK_LEN(p, l);
278
279 switch (GET_BE_U_4(ap->family)) {
280 case 1: /* IPv4 */
281 if (l < 4)
282 return -1;
283 ND_PRINT(" %s", GET_IPADDR_STRING(p));
284 p += sizeof(nd_ipv4);
285 break;
286 case 2: /* IPv6 */
287 if (l < 16)
288 return -1;
289 ND_PRINT(" %s", GET_IP6ADDR_STRING(p));
290 p += sizeof(nd_ipv6);
291 break;
292 default:
293 ND_PRINT(" %u/", GET_BE_U_4(ap->family));
294 for (i = 0; i < l; i++) {
295 ND_PRINT("%02x", GET_U_1(p));
296 p++;
297 }
298 }
299
300 return ND_BYTES_BETWEEN(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 ndo->ndo_protocol = "lwres";
319 np = (const struct lwres_lwpacket *)bp;
320 ND_TCHECK_2(np->authlength);
321
322 ND_PRINT(" lwres");
323 v = GET_BE_U_2(np->version);
324 if (ndo->ndo_vflag || v != LWRES_LWPACKETVERSION_0)
325 ND_PRINT(" v%u", v);
326 if (v != LWRES_LWPACKETVERSION_0) {
327 s = bp + GET_BE_U_4(np->length);
328 goto tail;
329 }
330
331 response = GET_BE_U_2(np->pktflags) & LWRES_LWPACKETFLAG_RESPONSE;
332
333 /* opcode and pktflags */
334 v = GET_BE_U_4(np->opcode);
335 ND_PRINT(" %s%s", tok2str(opcode, "#0x%x", v), response ? "" : "?");
336
337 /* pktflags */
338 v = GET_BE_U_2(np->pktflags);
339 if (v & ~LWRES_LWPACKETFLAG_RESPONSE)
340 ND_PRINT("[0x%x]", v);
341
342 if (ndo->ndo_vflag > 1) {
343 ND_PRINT(" ("); /*)*/
344 ND_PRINT("serial:0x%x", GET_BE_U_4(np->serial));
345 ND_PRINT(" result:0x%x", GET_BE_U_4(np->result));
346 ND_PRINT(" recvlen:%u", GET_BE_U_4(np->recvlength));
347 /* BIND910: not used */
348 if (ndo->ndo_vflag > 2) {
349 ND_PRINT(" authtype:0x%x", GET_BE_U_2(np->authtype));
350 ND_PRINT(" authlen:%u", GET_BE_U_2(np->authlength));
351 }
352 /*(*/
353 ND_PRINT(")");
354 }
355
356 /* per-opcode content */
357 if (!response) {
358 /*
359 * queries
360 */
361 const lwres_gabnrequest_t *gabn;
362 const lwres_gnbarequest_t *gnba;
363 const lwres_grbnrequest_t *grbn;
364 uint32_t l;
365
366 gabn = NULL;
367 gnba = NULL;
368 grbn = NULL;
369
370 p = (const u_char *)(np + 1);
371 switch (GET_BE_U_4(np->opcode)) {
372 case LWRES_OPCODE_NOOP:
373 s = p;
374 break;
375 case LWRES_OPCODE_GETADDRSBYNAME:
376 gabn = (const lwres_gabnrequest_t *)p;
377 ND_TCHECK_2(gabn->namelen);
378
379 /* BIND910: not used */
380 if (ndo->ndo_vflag > 2) {
381 ND_PRINT(" flags:0x%x",
382 GET_BE_U_4(gabn->flags));
383 }
384
385 v = GET_BE_U_4(gabn->addrtypes);
386 switch (v & (LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6)) {
387 case LWRES_ADDRTYPE_V4:
388 ND_PRINT(" IPv4");
389 break;
390 case LWRES_ADDRTYPE_V6:
391 ND_PRINT(" IPv6");
392 break;
393 case LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6:
394 ND_PRINT(" IPv4/6");
395 break;
396 }
397 if (v & ~(LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6))
398 ND_PRINT("[0x%x]", v);
399
400 s = p + LWRES_GABNREQUEST_LEN;
401 l = GET_BE_U_2(gabn->namelen);
402 advance = lwres_printname(ndo, l, s);
403 if (advance < 0)
404 goto trunc;
405 s += advance;
406 break;
407 case LWRES_OPCODE_GETNAMEBYADDR:
408 gnba = (const lwres_gnbarequest_t *)p;
409 ND_TCHECK_4(gnba->flags);
410
411 /* BIND910: not used */
412 if (ndo->ndo_vflag > 2) {
413 ND_PRINT(" flags:0x%x",
414 GET_BE_U_4(gnba->flags));
415 }
416
417 s = p + LWRES_GNBAREQUEST_LEN;
418 advance = lwres_printaddr(ndo, s);
419 if (advance < 0)
420 goto trunc;
421 s += advance;
422 break;
423 case LWRES_OPCODE_GETRDATABYNAME:
424 /* XXX no trace, not tested */
425 grbn = (const lwres_grbnrequest_t *)p;
426 ND_TCHECK_2(grbn->namelen);
427
428 /* BIND910: not used */
429 if (ndo->ndo_vflag > 2) {
430 ND_PRINT(" flags:0x%x",
431 GET_BE_U_4(grbn->flags));
432 }
433
434 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u",
435 GET_BE_U_2(grbn->rdtype)));
436 if (GET_BE_U_2(grbn->rdclass) != C_IN) {
437 ND_PRINT(" %s", tok2str(ns_class2str, "Class%u",
438 GET_BE_U_2(grbn->rdclass)));
439 }
440
441 s = p + LWRES_GRBNREQUEST_LEN;
442 l = GET_BE_U_2(grbn->namelen);
443 advance = lwres_printname(ndo, l, s);
444 if (advance < 0)
445 goto trunc;
446 s += advance;
447 break;
448 default:
449 s = p;
450 unsupported++;
451 break;
452 }
453 } else {
454 /*
455 * responses
456 */
457 const lwres_gabnresponse_t *gabn;
458 const lwres_gnbaresponse_t *gnba;
459 const lwres_grbnresponse_t *grbn;
460 uint32_t l, na;
461 uint32_t i;
462
463 gabn = NULL;
464 gnba = NULL;
465 grbn = NULL;
466
467 p = (const u_char *)(np + 1);
468 switch (GET_BE_U_4(np->opcode)) {
469 case LWRES_OPCODE_NOOP:
470 s = p;
471 break;
472 case LWRES_OPCODE_GETADDRSBYNAME:
473 gabn = (const lwres_gabnresponse_t *)p;
474 ND_TCHECK_2(gabn->realnamelen);
475
476 /* BIND910: not used */
477 if (ndo->ndo_vflag > 2) {
478 ND_PRINT(" flags:0x%x",
479 GET_BE_U_4(gabn->flags));
480 }
481
482 ND_PRINT(" %u/%u", GET_BE_U_2(gabn->naliases),
483 GET_BE_U_2(gabn->naddrs));
484
485 s = p + LWRES_GABNRESPONSE_LEN;
486 l = GET_BE_U_2(gabn->realnamelen);
487 advance = lwres_printname(ndo, l, s);
488 if (advance < 0)
489 goto trunc;
490 s += advance;
491
492 /* aliases */
493 na = GET_BE_U_2(gabn->naliases);
494 for (i = 0; i < na; i++) {
495 advance = lwres_printnamelen(ndo, s);
496 if (advance < 0)
497 goto trunc;
498 s += advance;
499 }
500
501 /* addrs */
502 na = GET_BE_U_2(gabn->naddrs);
503 for (i = 0; i < na; i++) {
504 advance = lwres_printaddr(ndo, s);
505 if (advance < 0)
506 goto trunc;
507 s += advance;
508 }
509 break;
510 case LWRES_OPCODE_GETNAMEBYADDR:
511 gnba = (const lwres_gnbaresponse_t *)p;
512 ND_TCHECK_2(gnba->realnamelen);
513
514 /* BIND910: not used */
515 if (ndo->ndo_vflag > 2) {
516 ND_PRINT(" flags:0x%x",
517 GET_BE_U_4(gnba->flags));
518 }
519
520 ND_PRINT(" %u", GET_BE_U_2(gnba->naliases));
521
522 s = p + LWRES_GNBARESPONSE_LEN;
523 l = GET_BE_U_2(gnba->realnamelen);
524 advance = lwres_printname(ndo, l, s);
525 if (advance < 0)
526 goto trunc;
527 s += advance;
528
529 /* aliases */
530 na = GET_BE_U_2(gnba->naliases);
531 for (i = 0; i < na; i++) {
532 advance = lwres_printnamelen(ndo, s);
533 if (advance < 0)
534 goto trunc;
535 s += advance;
536 }
537 break;
538 case LWRES_OPCODE_GETRDATABYNAME:
539 /* XXX no trace, not tested */
540 grbn = (const lwres_grbnresponse_t *)p;
541 ND_TCHECK_2(grbn->nsigs);
542
543 /* BIND910: not used */
544 if (ndo->ndo_vflag > 2) {
545 ND_PRINT(" flags:0x%x",
546 GET_BE_U_4(grbn->flags));
547 }
548
549 ND_PRINT(" %s", tok2str(ns_type2str, "Type%u",
550 GET_BE_U_2(grbn->rdtype)));
551 if (GET_BE_U_2(grbn->rdclass) != C_IN) {
552 ND_PRINT(" %s", tok2str(ns_class2str, "Class%u",
553 GET_BE_U_2(grbn->rdclass)));
554 }
555 ND_PRINT(" TTL ");
556 unsigned_relts_print(ndo,
557 GET_BE_U_4(grbn->ttl));
558 ND_PRINT(" %u/%u", GET_BE_U_2(grbn->nrdatas),
559 GET_BE_U_2(grbn->nsigs));
560
561 s = p + LWRES_GRBNRESPONSE_LEN;
562 advance = lwres_printnamelen(ndo, s);
563 if (advance < 0)
564 goto trunc;
565 s += advance;
566
567 /* rdatas */
568 na = GET_BE_U_2(grbn->nrdatas);
569 for (i = 0; i < na; i++) {
570 /* XXX should decode resource data */
571 advance = lwres_printbinlen(ndo, s);
572 if (advance < 0)
573 goto trunc;
574 s += advance;
575 }
576
577 /* sigs */
578 na = GET_BE_U_2(grbn->nsigs);
579 for (i = 0; i < na; i++) {
580 /* XXX how should we print it? */
581 advance = lwres_printbinlen(ndo, s);
582 if (advance < 0)
583 goto trunc;
584 s += advance;
585 }
586 break;
587 default:
588 s = p;
589 unsupported++;
590 break;
591 }
592 }
593
594 tail:
595 /* length mismatch */
596 if (GET_BE_U_4(np->length) != length) {
597 ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np->length),
598 length);
599 }
600 if (!unsupported && s < bp + GET_BE_U_4(np->length))
601 ND_PRINT("[extra]");
602 return;
603
604 trunc:
605 nd_print_trunc(ndo);
606 }