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 */
49 * Use nd_uint16_t for lwres_uint16_t
50 * Use nd_uint32_t for lwres_uint32_t
53 struct lwres_lwpacket
{
60 nd_uint32_t recvlength
;
62 nd_uint16_t authlength
;
65 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
67 #define LWRES_LWPACKETVERSION_0 0
69 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
70 #define LWRES_FLAG_SECUREDATA 0x00000002U
75 #define LWRES_OPCODE_NOOP 0x00000000U
79 nd_uint16_t datalength
;
81 } lwres_nooprequest_t
;
85 nd_uint16_t datalength
;
87 } lwres_noopresponse_t
;
90 * get addresses by name
92 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
94 typedef struct lwres_addr lwres_addr_t
;
101 #define LWRES_ADDR_LEN 6
106 nd_uint32_t addrtypes
;
109 } lwres_gabnrequest_t
;
110 #define LWRES_GABNREQUEST_LEN 10
115 nd_uint16_t naliases
;
117 nd_uint16_t realnamelen
;
118 /* aliases follows */
120 /* realname follows */
121 } lwres_gabnresponse_t
;
122 #define LWRES_GABNRESPONSE_LEN 10
125 * get name by address
127 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
132 } lwres_gnbarequest_t
;
133 #define LWRES_GNBAREQUEST_LEN 4
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
148 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
157 } lwres_grbnrequest_t
;
158 #define LWRES_GRBNREQUEST_LEN 10
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
174 #define LWRDATA_VALIDATED 0x00000001
176 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
177 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
179 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
180 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
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", },
191 extern const struct tok ns_type2str
[];
192 extern const struct tok ns_class2str
[];
195 lwres_printname(netdissect_options
*ndo
,
196 size_t l
, const u_char
*p0
)
202 /* + 1 for terminating \0 */
203 if (p
+ l
+ 1 > ndo
->ndo_snapend
)
207 for (i
= 0; i
< l
; i
++)
208 safeputchar(ndo
, *p
++);
209 p
++; /* skip terminating \0 */
218 lwres_printnamelen(netdissect_options
*ndo
,
224 if (p
+ 2 > ndo
->ndo_snapend
)
226 l
= EXTRACT_BE_U_2(p
);
227 advance
= lwres_printname(ndo
, l
, p
+ 2);
237 lwres_printbinlen(netdissect_options
*ndo
,
245 if (p
+ 2 > ndo
->ndo_snapend
)
247 l
= EXTRACT_BE_U_2(p
);
248 if (p
+ 2 + l
> ndo
->ndo_snapend
)
251 for (i
= 0; i
< l
; i
++)
252 ND_PRINT("%02x", *p
++);
260 lwres_printaddr(netdissect_options
*ndo
,
264 const lwres_addr_t
*ap
;
269 ap
= (const lwres_addr_t
*)p
;
270 ND_TCHECK_2(ap
->length
);
271 l
= EXTRACT_BE_U_2(ap
->length
);
275 switch (EXTRACT_BE_U_4(ap
->family
)) {
279 ND_PRINT(" %s", ipaddr_string(ndo
, p
));
280 p
+= sizeof(struct in_addr
);
285 ND_PRINT(" %s", ip6addr_string(ndo
, p
));
286 p
+= sizeof(struct in6_addr
);
289 ND_PRINT(" %u/", EXTRACT_BE_U_4(ap
->family
));
290 for (i
= 0; i
< l
; i
++)
291 ND_PRINT("%02x", *p
++);
301 lwres_print(netdissect_options
*ndo
,
302 const u_char
*bp
, u_int length
)
305 const struct lwres_lwpacket
*np
;
312 np
= (const struct lwres_lwpacket
*)bp
;
313 ND_TCHECK_2(np
->authlength
);
316 v
= EXTRACT_BE_U_2(np
->version
);
317 if (ndo
->ndo_vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
319 if (v
!= LWRES_LWPACKETVERSION_0
) {
320 s
= bp
+ EXTRACT_BE_U_4(np
->length
);
324 response
= EXTRACT_BE_U_2(np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
326 /* opcode and pktflags */
327 v
= EXTRACT_BE_U_4(np
->opcode
);
328 ND_PRINT(" %s%s", tok2str(opcode
, "#0x%x", v
), response
? "" : "?");
331 v
= EXTRACT_BE_U_2(np
->pktflags
);
332 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
333 ND_PRINT("[0x%x]", v
);
335 if (ndo
->ndo_vflag
> 1) {
336 ND_PRINT(" ("); /*)*/
337 ND_PRINT("serial:0x%x", EXTRACT_BE_U_4(np
->serial
));
338 ND_PRINT(" result:0x%x", EXTRACT_BE_U_4(np
->result
));
339 ND_PRINT(" recvlen:%u", EXTRACT_BE_U_4(np
->recvlength
));
340 /* BIND910: not used */
341 if (ndo
->ndo_vflag
> 2) {
342 ND_PRINT(" authtype:0x%x", EXTRACT_BE_U_2(np
->authtype
));
343 ND_PRINT(" authlen:%u", EXTRACT_BE_U_2(np
->authlength
));
349 /* per-opcode content */
354 const lwres_gabnrequest_t
*gabn
;
355 const lwres_gnbarequest_t
*gnba
;
356 const lwres_grbnrequest_t
*grbn
;
363 p
= (const u_char
*)(np
+ 1);
364 switch (EXTRACT_BE_U_4(np
->opcode
)) {
365 case LWRES_OPCODE_NOOP
:
368 case LWRES_OPCODE_GETADDRSBYNAME
:
369 gabn
= (const lwres_gabnrequest_t
*)p
;
370 ND_TCHECK_2(gabn
->namelen
);
372 /* BIND910: not used */
373 if (ndo
->ndo_vflag
> 2) {
374 ND_PRINT(" flags:0x%x",
375 EXTRACT_BE_U_4(gabn
->flags
));
378 v
= EXTRACT_BE_U_4(gabn
->addrtypes
);
379 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
380 case LWRES_ADDRTYPE_V4
:
383 case LWRES_ADDRTYPE_V6
:
386 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
390 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
391 ND_PRINT("[0x%x]", v
);
393 s
= p
+ LWRES_GABNREQUEST_LEN
;
394 l
= EXTRACT_BE_U_2(gabn
->namelen
);
395 advance
= lwres_printname(ndo
, l
, s
);
400 case LWRES_OPCODE_GETNAMEBYADDR
:
401 gnba
= (const lwres_gnbarequest_t
*)p
;
402 ND_TCHECK_4(gnba
->flags
);
404 /* BIND910: not used */
405 if (ndo
->ndo_vflag
> 2) {
406 ND_PRINT(" flags:0x%x",
407 EXTRACT_BE_U_4(gnba
->flags
));
410 s
= p
+ LWRES_GNBAREQUEST_LEN
;
411 advance
= lwres_printaddr(ndo
, s
);
416 case LWRES_OPCODE_GETRDATABYNAME
:
417 /* XXX no trace, not tested */
418 grbn
= (const lwres_grbnrequest_t
*)p
;
419 ND_TCHECK_2(grbn
->namelen
);
421 /* BIND910: not used */
422 if (ndo
->ndo_vflag
> 2) {
423 ND_PRINT(" flags:0x%x",
424 EXTRACT_BE_U_4(grbn
->flags
));
427 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
428 EXTRACT_BE_U_2(grbn
->rdtype
)));
429 if (EXTRACT_BE_U_2(grbn
->rdclass
) != C_IN
) {
430 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
431 EXTRACT_BE_U_2(grbn
->rdclass
)));
434 s
= p
+ LWRES_GRBNREQUEST_LEN
;
435 l
= EXTRACT_BE_U_2(grbn
->namelen
);
436 advance
= lwres_printname(ndo
, l
, s
);
450 const lwres_gabnresponse_t
*gabn
;
451 const lwres_gnbaresponse_t
*gnba
;
452 const lwres_grbnresponse_t
*grbn
;
460 p
= (const u_char
*)(np
+ 1);
461 switch (EXTRACT_BE_U_4(np
->opcode
)) {
462 case LWRES_OPCODE_NOOP
:
465 case LWRES_OPCODE_GETADDRSBYNAME
:
466 gabn
= (const lwres_gabnresponse_t
*)p
;
467 ND_TCHECK_2(gabn
->realnamelen
);
469 /* BIND910: not used */
470 if (ndo
->ndo_vflag
> 2) {
471 ND_PRINT(" flags:0x%x",
472 EXTRACT_BE_U_4(gabn
->flags
));
475 ND_PRINT(" %u/%u", EXTRACT_BE_U_2(gabn
->naliases
),
476 EXTRACT_BE_U_2(gabn
->naddrs
));
478 s
= p
+ LWRES_GABNRESPONSE_LEN
;
479 l
= EXTRACT_BE_U_2(gabn
->realnamelen
);
480 advance
= lwres_printname(ndo
, l
, s
);
486 na
= EXTRACT_BE_U_2(gabn
->naliases
);
487 for (i
= 0; i
< na
; i
++) {
488 advance
= lwres_printnamelen(ndo
, s
);
495 na
= EXTRACT_BE_U_2(gabn
->naddrs
);
496 for (i
= 0; i
< na
; i
++) {
497 advance
= lwres_printaddr(ndo
, s
);
503 case LWRES_OPCODE_GETNAMEBYADDR
:
504 gnba
= (const lwres_gnbaresponse_t
*)p
;
505 ND_TCHECK_2(gnba
->realnamelen
);
507 /* BIND910: not used */
508 if (ndo
->ndo_vflag
> 2) {
509 ND_PRINT(" flags:0x%x",
510 EXTRACT_BE_U_4(gnba
->flags
));
513 ND_PRINT(" %u", EXTRACT_BE_U_2(gnba
->naliases
));
515 s
= p
+ LWRES_GNBARESPONSE_LEN
;
516 l
= EXTRACT_BE_U_2(gnba
->realnamelen
);
517 advance
= lwres_printname(ndo
, l
, s
);
523 na
= EXTRACT_BE_U_2(gnba
->naliases
);
524 for (i
= 0; i
< na
; i
++) {
525 advance
= lwres_printnamelen(ndo
, s
);
531 case LWRES_OPCODE_GETRDATABYNAME
:
532 /* XXX no trace, not tested */
533 grbn
= (const lwres_grbnresponse_t
*)p
;
534 ND_TCHECK_2(grbn
->nsigs
);
536 /* BIND910: not used */
537 if (ndo
->ndo_vflag
> 2) {
538 ND_PRINT(" flags:0x%x",
539 EXTRACT_BE_U_4(grbn
->flags
));
542 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
543 EXTRACT_BE_U_2(grbn
->rdtype
)));
544 if (EXTRACT_BE_U_2(grbn
->rdclass
) != C_IN
) {
545 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
546 EXTRACT_BE_U_2(grbn
->rdclass
)));
549 unsigned_relts_print(ndo
,
550 EXTRACT_BE_U_4(grbn
->ttl
));
551 ND_PRINT(" %u/%u", EXTRACT_BE_U_2(grbn
->nrdatas
),
552 EXTRACT_BE_U_2(grbn
->nsigs
));
554 s
= p
+ LWRES_GRBNRESPONSE_LEN
;
555 advance
= lwres_printnamelen(ndo
, s
);
561 na
= EXTRACT_BE_U_2(grbn
->nrdatas
);
562 for (i
= 0; i
< na
; i
++) {
563 /* XXX should decode resource data */
564 advance
= lwres_printbinlen(ndo
, s
);
571 na
= EXTRACT_BE_U_2(grbn
->nsigs
);
572 for (i
= 0; i
< na
; i
++) {
573 /* XXX how should we print it? */
574 advance
= lwres_printbinlen(ndo
, s
);
588 /* length mismatch */
589 if (EXTRACT_BE_U_4(np
->length
) != length
) {
590 ND_PRINT(" [len: %u != %u]", EXTRACT_BE_U_4(np
->length
),
593 if (!unsupported
&& s
< bp
+ EXTRACT_BE_U_4(np
->length
))
598 ND_PRINT("[|lwres]");