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 fn_print_char(ndo
, GET_U_1(p
));
211 p
++; /* skip terminating \0 */
213 return ND_BYTES_BETWEEN(p
, p0
);
220 lwres_printnamelen(netdissect_options
*ndo
,
226 if (p
+ 2 > ndo
->ndo_snapend
)
229 advance
= lwres_printname(ndo
, l
, p
+ 2);
239 lwres_printbinlen(netdissect_options
*ndo
,
247 if (p
+ 2 > ndo
->ndo_snapend
)
250 if (p
+ 2 + l
> ndo
->ndo_snapend
)
253 for (i
= 0; i
< l
; i
++) {
254 ND_PRINT("%02x", GET_U_1(p
));
257 return ND_BYTES_BETWEEN(p
, p0
);
264 lwres_printaddr(netdissect_options
*ndo
,
268 const lwres_addr_t
*ap
;
273 ap
= (const lwres_addr_t
*)p
;
274 ND_TCHECK_2(ap
->length
);
275 l
= GET_BE_U_2(ap
->length
);
279 switch (GET_BE_U_4(ap
->family
)) {
283 ND_PRINT(" %s", GET_IPADDR_STRING(p
));
284 p
+= sizeof(nd_ipv4
);
289 ND_PRINT(" %s", GET_IP6ADDR_STRING(p
));
290 p
+= sizeof(nd_ipv6
);
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
));
300 return ND_BYTES_BETWEEN(p
, p0
);
307 lwres_print(netdissect_options
*ndo
,
308 const u_char
*bp
, u_int length
)
311 const struct lwres_lwpacket
*np
;
318 ndo
->ndo_protocol
= "lwres";
319 np
= (const struct lwres_lwpacket
*)bp
;
320 ND_TCHECK_2(np
->authlength
);
323 v
= GET_BE_U_2(np
->version
);
324 if (ndo
->ndo_vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
326 if (v
!= LWRES_LWPACKETVERSION_0
) {
327 s
= bp
+ GET_BE_U_4(np
->length
);
331 response
= GET_BE_U_2(np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
333 /* opcode and pktflags */
334 v
= GET_BE_U_4(np
->opcode
);
335 ND_PRINT(" %s%s", tok2str(opcode
, "#0x%x", v
), response
? "" : "?");
338 v
= GET_BE_U_2(np
->pktflags
);
339 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
340 ND_PRINT("[0x%x]", v
);
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
));
356 /* per-opcode content */
361 const lwres_gabnrequest_t
*gabn
;
362 const lwres_gnbarequest_t
*gnba
;
363 const lwres_grbnrequest_t
*grbn
;
370 p
= (const u_char
*)(np
+ 1);
371 switch (GET_BE_U_4(np
->opcode
)) {
372 case LWRES_OPCODE_NOOP
:
375 case LWRES_OPCODE_GETADDRSBYNAME
:
376 gabn
= (const lwres_gabnrequest_t
*)p
;
377 ND_TCHECK_2(gabn
->namelen
);
379 /* BIND910: not used */
380 if (ndo
->ndo_vflag
> 2) {
381 ND_PRINT(" flags:0x%x",
382 GET_BE_U_4(gabn
->flags
));
385 v
= GET_BE_U_4(gabn
->addrtypes
);
386 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
387 case LWRES_ADDRTYPE_V4
:
390 case LWRES_ADDRTYPE_V6
:
393 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
397 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
398 ND_PRINT("[0x%x]", v
);
400 s
= p
+ LWRES_GABNREQUEST_LEN
;
401 l
= GET_BE_U_2(gabn
->namelen
);
402 advance
= lwres_printname(ndo
, l
, s
);
407 case LWRES_OPCODE_GETNAMEBYADDR
:
408 gnba
= (const lwres_gnbarequest_t
*)p
;
409 ND_TCHECK_4(gnba
->flags
);
411 /* BIND910: not used */
412 if (ndo
->ndo_vflag
> 2) {
413 ND_PRINT(" flags:0x%x",
414 GET_BE_U_4(gnba
->flags
));
417 s
= p
+ LWRES_GNBAREQUEST_LEN
;
418 advance
= lwres_printaddr(ndo
, s
);
423 case LWRES_OPCODE_GETRDATABYNAME
:
424 /* XXX no trace, not tested */
425 grbn
= (const lwres_grbnrequest_t
*)p
;
426 ND_TCHECK_2(grbn
->namelen
);
428 /* BIND910: not used */
429 if (ndo
->ndo_vflag
> 2) {
430 ND_PRINT(" flags:0x%x",
431 GET_BE_U_4(grbn
->flags
));
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
)));
441 s
= p
+ LWRES_GRBNREQUEST_LEN
;
442 l
= GET_BE_U_2(grbn
->namelen
);
443 advance
= lwres_printname(ndo
, l
, s
);
457 const lwres_gabnresponse_t
*gabn
;
458 const lwres_gnbaresponse_t
*gnba
;
459 const lwres_grbnresponse_t
*grbn
;
467 p
= (const u_char
*)(np
+ 1);
468 switch (GET_BE_U_4(np
->opcode
)) {
469 case LWRES_OPCODE_NOOP
:
472 case LWRES_OPCODE_GETADDRSBYNAME
:
473 gabn
= (const lwres_gabnresponse_t
*)p
;
474 ND_TCHECK_2(gabn
->realnamelen
);
476 /* BIND910: not used */
477 if (ndo
->ndo_vflag
> 2) {
478 ND_PRINT(" flags:0x%x",
479 GET_BE_U_4(gabn
->flags
));
482 ND_PRINT(" %u/%u", GET_BE_U_2(gabn
->naliases
),
483 GET_BE_U_2(gabn
->naddrs
));
485 s
= p
+ LWRES_GABNRESPONSE_LEN
;
486 l
= GET_BE_U_2(gabn
->realnamelen
);
487 advance
= lwres_printname(ndo
, l
, s
);
493 na
= GET_BE_U_2(gabn
->naliases
);
494 for (i
= 0; i
< na
; i
++) {
495 advance
= lwres_printnamelen(ndo
, s
);
502 na
= GET_BE_U_2(gabn
->naddrs
);
503 for (i
= 0; i
< na
; i
++) {
504 advance
= lwres_printaddr(ndo
, s
);
510 case LWRES_OPCODE_GETNAMEBYADDR
:
511 gnba
= (const lwres_gnbaresponse_t
*)p
;
512 ND_TCHECK_2(gnba
->realnamelen
);
514 /* BIND910: not used */
515 if (ndo
->ndo_vflag
> 2) {
516 ND_PRINT(" flags:0x%x",
517 GET_BE_U_4(gnba
->flags
));
520 ND_PRINT(" %u", GET_BE_U_2(gnba
->naliases
));
522 s
= p
+ LWRES_GNBARESPONSE_LEN
;
523 l
= GET_BE_U_2(gnba
->realnamelen
);
524 advance
= lwres_printname(ndo
, l
, s
);
530 na
= GET_BE_U_2(gnba
->naliases
);
531 for (i
= 0; i
< na
; i
++) {
532 advance
= lwres_printnamelen(ndo
, s
);
538 case LWRES_OPCODE_GETRDATABYNAME
:
539 /* XXX no trace, not tested */
540 grbn
= (const lwres_grbnresponse_t
*)p
;
541 ND_TCHECK_2(grbn
->nsigs
);
543 /* BIND910: not used */
544 if (ndo
->ndo_vflag
> 2) {
545 ND_PRINT(" flags:0x%x",
546 GET_BE_U_4(grbn
->flags
));
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
)));
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
));
561 s
= p
+ LWRES_GRBNRESPONSE_LEN
;
562 advance
= lwres_printnamelen(ndo
, s
);
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
);
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
);
595 /* length mismatch */
596 if (GET_BE_U_4(np
->length
) != length
) {
597 ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np
->length
),
600 if (!unsupported
&& s
< bp
+ GET_BE_U_4(np
->length
))