2 * Copyright (C) 2001 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 /* \summary: BIND9 Lightweight Resolver protocol printer */
36 #include "netdissect-stdinc.h"
41 #include "netdissect.h"
42 #include "addrtoname.h"
47 /* BIND9 lib/lwres/include/lwres */
48 typedef nd_uint32_t lwres_uint32_t
;
49 typedef nd_uint16_t lwres_uint16_t
;
50 typedef nd_uint8_t lwres_uint8_t
;
52 struct lwres_lwpacket
{
53 lwres_uint32_t length
;
54 lwres_uint16_t version
;
55 lwres_uint16_t pktflags
;
56 lwres_uint32_t serial
;
57 lwres_uint32_t opcode
;
58 lwres_uint32_t result
;
59 lwres_uint32_t recvlength
;
60 lwres_uint16_t authtype
;
61 lwres_uint16_t authlength
;
64 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
66 #define LWRES_LWPACKETVERSION_0 0
68 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
69 #define LWRES_FLAG_SECUREDATA 0x00000002U
74 #define LWRES_OPCODE_NOOP 0x00000000U
78 lwres_uint16_t datalength
;
80 } lwres_nooprequest_t
;
84 lwres_uint16_t datalength
;
86 } lwres_noopresponse_t
;
89 * get addresses by name
91 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
93 typedef struct lwres_addr lwres_addr_t
;
96 lwres_uint32_t family
;
97 lwres_uint16_t length
;
100 #define LWRES_ADDR_LEN 6
104 lwres_uint32_t flags
;
105 lwres_uint32_t addrtypes
;
106 lwres_uint16_t namelen
;
108 } lwres_gabnrequest_t
;
109 #define LWRES_GABNREQUEST_LEN 10
113 lwres_uint32_t flags
;
114 lwres_uint16_t naliases
;
115 lwres_uint16_t naddrs
;
116 lwres_uint16_t realnamelen
;
117 /* aliases follows */
119 /* realname follows */
120 } lwres_gabnresponse_t
;
121 #define LWRES_GABNRESPONSE_LEN 10
124 * get name by address
126 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
129 lwres_uint32_t flags
;
131 } lwres_gnbarequest_t
;
132 #define LWRES_GNBAREQUEST_LEN 4
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 #define LWRES_GNBARESPONSE_LEN 8
147 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
151 lwres_uint32_t flags
;
152 lwres_uint16_t rdclass
;
153 lwres_uint16_t rdtype
;
154 lwres_uint16_t namelen
;
156 } lwres_grbnrequest_t
;
157 #define LWRES_GRBNREQUEST_LEN 10
161 lwres_uint32_t flags
;
162 lwres_uint16_t rdclass
;
163 lwres_uint16_t rdtype
;
165 lwres_uint16_t nrdatas
;
166 lwres_uint16_t nsigs
;
167 /* realname here (len + name) */
168 /* rdata here (len + name) */
169 /* signatures here (len + name) */
170 } lwres_grbnresponse_t
;
171 #define LWRES_GRBNRESPONSE_LEN 16
173 #define LWRDATA_VALIDATED 0x00000001
175 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
176 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
178 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
179 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
181 static const struct tok opcode
[] = {
182 { LWRES_OPCODE_NOOP
, "noop", },
183 { LWRES_OPCODE_GETADDRSBYNAME
, "getaddrsbyname", },
184 { LWRES_OPCODE_GETNAMEBYADDR
, "getnamebyaddr", },
185 { LWRES_OPCODE_GETRDATABYNAME
, "getrdatabyname", },
190 extern const struct tok ns_type2str
[];
191 extern const struct tok ns_class2str
[];
194 lwres_printname(netdissect_options
*ndo
,
195 size_t l
, const u_char
*p0
)
201 /* + 1 for terminating \0 */
202 if (p
+ l
+ 1 > ndo
->ndo_snapend
)
206 for (i
= 0; i
< l
; i
++)
207 safeputchar(ndo
, *p
++);
208 p
++; /* skip terminating \0 */
217 lwres_printnamelen(netdissect_options
*ndo
,
223 if (p
+ 2 > ndo
->ndo_snapend
)
225 l
= EXTRACT_BE_U_2(p
);
226 advance
= lwres_printname(ndo
, l
, p
+ 2);
236 lwres_printbinlen(netdissect_options
*ndo
,
244 if (p
+ 2 > ndo
->ndo_snapend
)
246 l
= EXTRACT_BE_U_2(p
);
247 if (p
+ 2 + l
> ndo
->ndo_snapend
)
250 for (i
= 0; i
< l
; i
++)
251 ND_PRINT("%02x", *p
++);
259 lwres_printaddr(netdissect_options
*ndo
,
263 const lwres_addr_t
*ap
;
268 ap
= (const lwres_addr_t
*)p
;
269 ND_TCHECK(ap
->length
);
270 l
= EXTRACT_BE_U_2(ap
->length
);
274 switch (EXTRACT_BE_U_4(ap
->family
)) {
278 ND_PRINT(" %s", ipaddr_string(ndo
, p
));
279 p
+= sizeof(struct in_addr
);
284 ND_PRINT(" %s", ip6addr_string(ndo
, p
));
285 p
+= sizeof(struct in6_addr
);
288 ND_PRINT(" %u/", EXTRACT_BE_U_4(ap
->family
));
289 for (i
= 0; i
< l
; i
++)
290 ND_PRINT("%02x", *p
++);
300 lwres_print(netdissect_options
*ndo
,
301 const u_char
*bp
, u_int length
)
304 const struct lwres_lwpacket
*np
;
311 np
= (const struct lwres_lwpacket
*)bp
;
312 ND_TCHECK(np
->authlength
);
315 v
= EXTRACT_BE_U_2(np
->version
);
316 if (ndo
->ndo_vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
318 if (v
!= LWRES_LWPACKETVERSION_0
) {
319 s
= bp
+ EXTRACT_BE_U_4(np
->length
);
323 response
= EXTRACT_BE_U_2(np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
325 /* opcode and pktflags */
326 v
= EXTRACT_BE_U_4(np
->opcode
);
327 ND_PRINT(" %s%s", tok2str(opcode
, "#0x%x", v
), response
? "" : "?");
330 v
= EXTRACT_BE_U_2(np
->pktflags
);
331 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
332 ND_PRINT("[0x%x]", v
);
334 if (ndo
->ndo_vflag
> 1) {
335 ND_PRINT(" ("); /*)*/
336 ND_PRINT("serial:0x%x", EXTRACT_BE_U_4(np
->serial
));
337 ND_PRINT(" result:0x%x", EXTRACT_BE_U_4(np
->result
));
338 ND_PRINT(" recvlen:%u", EXTRACT_BE_U_4(np
->recvlength
));
339 /* BIND910: not used */
340 if (ndo
->ndo_vflag
> 2) {
341 ND_PRINT(" authtype:0x%x", EXTRACT_BE_U_2(np
->authtype
));
342 ND_PRINT(" authlen:%u", EXTRACT_BE_U_2(np
->authlength
));
348 /* per-opcode content */
353 const lwres_gabnrequest_t
*gabn
;
354 const lwres_gnbarequest_t
*gnba
;
355 const lwres_grbnrequest_t
*grbn
;
362 p
= (const u_char
*)(np
+ 1);
363 switch (EXTRACT_BE_U_4(np
->opcode
)) {
364 case LWRES_OPCODE_NOOP
:
367 case LWRES_OPCODE_GETADDRSBYNAME
:
368 gabn
= (const lwres_gabnrequest_t
*)p
;
369 ND_TCHECK(gabn
->namelen
);
371 /* BIND910: not used */
372 if (ndo
->ndo_vflag
> 2) {
373 ND_PRINT(" flags:0x%x",
374 EXTRACT_BE_U_4(gabn
->flags
));
377 v
= EXTRACT_BE_U_4(gabn
->addrtypes
);
378 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
379 case LWRES_ADDRTYPE_V4
:
382 case LWRES_ADDRTYPE_V6
:
385 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
389 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
390 ND_PRINT("[0x%x]", v
);
392 s
= p
+ LWRES_GABNREQUEST_LEN
;
393 l
= EXTRACT_BE_U_2(gabn
->namelen
);
394 advance
= lwres_printname(ndo
, l
, s
);
399 case LWRES_OPCODE_GETNAMEBYADDR
:
400 gnba
= (const lwres_gnbarequest_t
*)p
;
401 ND_TCHECK(gnba
->flags
);
403 /* BIND910: not used */
404 if (ndo
->ndo_vflag
> 2) {
405 ND_PRINT(" flags:0x%x",
406 EXTRACT_BE_U_4(gnba
->flags
));
409 s
= p
+ LWRES_GNBAREQUEST_LEN
;
410 advance
= lwres_printaddr(ndo
, s
);
415 case LWRES_OPCODE_GETRDATABYNAME
:
416 /* XXX no trace, not tested */
417 grbn
= (const lwres_grbnrequest_t
*)p
;
418 ND_TCHECK(grbn
->namelen
);
420 /* BIND910: not used */
421 if (ndo
->ndo_vflag
> 2) {
422 ND_PRINT(" flags:0x%x",
423 EXTRACT_BE_U_4(grbn
->flags
));
426 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
427 EXTRACT_BE_U_2(grbn
->rdtype
)));
428 if (EXTRACT_BE_U_2(grbn
->rdclass
) != C_IN
) {
429 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
430 EXTRACT_BE_U_2(grbn
->rdclass
)));
433 s
= p
+ LWRES_GRBNREQUEST_LEN
;
434 l
= EXTRACT_BE_U_2(grbn
->namelen
);
435 advance
= lwres_printname(ndo
, l
, s
);
449 const lwres_gabnresponse_t
*gabn
;
450 const lwres_gnbaresponse_t
*gnba
;
451 const lwres_grbnresponse_t
*grbn
;
459 p
= (const u_char
*)(np
+ 1);
460 switch (EXTRACT_BE_U_4(np
->opcode
)) {
461 case LWRES_OPCODE_NOOP
:
464 case LWRES_OPCODE_GETADDRSBYNAME
:
465 gabn
= (const lwres_gabnresponse_t
*)p
;
466 ND_TCHECK(gabn
->realnamelen
);
468 /* BIND910: not used */
469 if (ndo
->ndo_vflag
> 2) {
470 ND_PRINT(" flags:0x%x",
471 EXTRACT_BE_U_4(gabn
->flags
));
474 ND_PRINT(" %u/%u", EXTRACT_BE_U_2(gabn
->naliases
),
475 EXTRACT_BE_U_2(gabn
->naddrs
));
477 s
= p
+ LWRES_GABNRESPONSE_LEN
;
478 l
= EXTRACT_BE_U_2(gabn
->realnamelen
);
479 advance
= lwres_printname(ndo
, l
, s
);
485 na
= EXTRACT_BE_U_2(gabn
->naliases
);
486 for (i
= 0; i
< na
; i
++) {
487 advance
= lwres_printnamelen(ndo
, s
);
494 na
= EXTRACT_BE_U_2(gabn
->naddrs
);
495 for (i
= 0; i
< na
; i
++) {
496 advance
= lwres_printaddr(ndo
, s
);
502 case LWRES_OPCODE_GETNAMEBYADDR
:
503 gnba
= (const lwres_gnbaresponse_t
*)p
;
504 ND_TCHECK(gnba
->realnamelen
);
506 /* BIND910: not used */
507 if (ndo
->ndo_vflag
> 2) {
508 ND_PRINT(" flags:0x%x",
509 EXTRACT_BE_U_4(gnba
->flags
));
512 ND_PRINT(" %u", EXTRACT_BE_U_2(gnba
->naliases
));
514 s
= p
+ LWRES_GNBARESPONSE_LEN
;
515 l
= EXTRACT_BE_U_2(gnba
->realnamelen
);
516 advance
= lwres_printname(ndo
, l
, s
);
522 na
= EXTRACT_BE_U_2(gnba
->naliases
);
523 for (i
= 0; i
< na
; i
++) {
524 advance
= lwres_printnamelen(ndo
, s
);
530 case LWRES_OPCODE_GETRDATABYNAME
:
531 /* XXX no trace, not tested */
532 grbn
= (const lwres_grbnresponse_t
*)p
;
533 ND_TCHECK(grbn
->nsigs
);
535 /* BIND910: not used */
536 if (ndo
->ndo_vflag
> 2) {
537 ND_PRINT(" flags:0x%x",
538 EXTRACT_BE_U_4(grbn
->flags
));
541 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
542 EXTRACT_BE_U_2(grbn
->rdtype
)));
543 if (EXTRACT_BE_U_2(grbn
->rdclass
) != C_IN
) {
544 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
545 EXTRACT_BE_U_2(grbn
->rdclass
)));
548 unsigned_relts_print(ndo
,
549 EXTRACT_BE_U_4(grbn
->ttl
));
550 ND_PRINT(" %u/%u", EXTRACT_BE_U_2(grbn
->nrdatas
),
551 EXTRACT_BE_U_2(grbn
->nsigs
));
553 s
= p
+ LWRES_GRBNRESPONSE_LEN
;
554 advance
= lwres_printnamelen(ndo
, s
);
560 na
= EXTRACT_BE_U_2(grbn
->nrdatas
);
561 for (i
= 0; i
< na
; i
++) {
562 /* XXX should decode resource data */
563 advance
= lwres_printbinlen(ndo
, s
);
570 na
= EXTRACT_BE_U_2(grbn
->nsigs
);
571 for (i
= 0; i
< na
; i
++) {
572 /* XXX how should we print it? */
573 advance
= lwres_printbinlen(ndo
, s
);
587 /* length mismatch */
588 if (EXTRACT_BE_U_4(np
->length
) != length
) {
589 ND_PRINT(" [len: %u != %u]", EXTRACT_BE_U_4(np
->length
),
592 if (!unsupported
&& s
< bp
+ EXTRACT_BE_U_4(np
->length
))
597 ND_PRINT("[|lwres]");