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 l
= GET_BE_U_2(ap
->length
);
278 switch (GET_BE_U_4(ap
->family
)) {
282 ND_PRINT(" %s", GET_IPADDR_STRING(p
));
283 p
+= sizeof(nd_ipv4
);
288 ND_PRINT(" %s", GET_IP6ADDR_STRING(p
));
289 p
+= sizeof(nd_ipv6
);
292 ND_PRINT(" %u/", GET_BE_U_4(ap
->family
));
293 for (i
= 0; i
< l
; i
++) {
294 ND_PRINT("%02x", GET_U_1(p
));
299 return ND_BYTES_BETWEEN(p
, p0
);
306 lwres_print(netdissect_options
*ndo
,
307 const u_char
*bp
, u_int length
)
310 const struct lwres_lwpacket
*np
;
317 ndo
->ndo_protocol
= "lwres";
318 np
= (const struct lwres_lwpacket
*)bp
;
319 ND_TCHECK_2(np
->authlength
);
322 v
= GET_BE_U_2(np
->version
);
323 if (ndo
->ndo_vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
325 if (v
!= LWRES_LWPACKETVERSION_0
) {
326 s
= bp
+ GET_BE_U_4(np
->length
);
330 response
= GET_BE_U_2(np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
332 /* opcode and pktflags */
333 v
= GET_BE_U_4(np
->opcode
);
334 ND_PRINT(" %s%s", tok2str(opcode
, "#0x%x", v
), response
? "" : "?");
337 v
= GET_BE_U_2(np
->pktflags
);
338 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
339 ND_PRINT("[0x%x]", v
);
341 if (ndo
->ndo_vflag
> 1) {
342 ND_PRINT(" ("); /*)*/
343 ND_PRINT("serial:0x%x", GET_BE_U_4(np
->serial
));
344 ND_PRINT(" result:0x%x", GET_BE_U_4(np
->result
));
345 ND_PRINT(" recvlen:%u", GET_BE_U_4(np
->recvlength
));
346 /* BIND910: not used */
347 if (ndo
->ndo_vflag
> 2) {
348 ND_PRINT(" authtype:0x%x", GET_BE_U_2(np
->authtype
));
349 ND_PRINT(" authlen:%u", GET_BE_U_2(np
->authlength
));
355 /* per-opcode content */
360 const lwres_gabnrequest_t
*gabn
;
361 const lwres_gnbarequest_t
*gnba
;
362 const lwres_grbnrequest_t
*grbn
;
369 p
= (const u_char
*)(np
+ 1);
370 switch (GET_BE_U_4(np
->opcode
)) {
371 case LWRES_OPCODE_NOOP
:
374 case LWRES_OPCODE_GETADDRSBYNAME
:
375 gabn
= (const lwres_gabnrequest_t
*)p
;
376 ND_TCHECK_2(gabn
->namelen
);
378 /* BIND910: not used */
379 if (ndo
->ndo_vflag
> 2) {
380 ND_PRINT(" flags:0x%x",
381 GET_BE_U_4(gabn
->flags
));
384 v
= GET_BE_U_4(gabn
->addrtypes
);
385 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
386 case LWRES_ADDRTYPE_V4
:
389 case LWRES_ADDRTYPE_V6
:
392 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
396 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
397 ND_PRINT("[0x%x]", v
);
399 s
= p
+ LWRES_GABNREQUEST_LEN
;
400 l
= GET_BE_U_2(gabn
->namelen
);
401 advance
= lwres_printname(ndo
, l
, s
);
406 case LWRES_OPCODE_GETNAMEBYADDR
:
407 gnba
= (const lwres_gnbarequest_t
*)p
;
408 ND_TCHECK_4(gnba
->flags
);
410 /* BIND910: not used */
411 if (ndo
->ndo_vflag
> 2) {
412 ND_PRINT(" flags:0x%x",
413 GET_BE_U_4(gnba
->flags
));
416 s
= p
+ LWRES_GNBAREQUEST_LEN
;
417 advance
= lwres_printaddr(ndo
, s
);
422 case LWRES_OPCODE_GETRDATABYNAME
:
423 /* XXX no trace, not tested */
424 grbn
= (const lwres_grbnrequest_t
*)p
;
425 ND_TCHECK_2(grbn
->namelen
);
427 /* BIND910: not used */
428 if (ndo
->ndo_vflag
> 2) {
429 ND_PRINT(" flags:0x%x",
430 GET_BE_U_4(grbn
->flags
));
433 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
434 GET_BE_U_2(grbn
->rdtype
)));
435 if (GET_BE_U_2(grbn
->rdclass
) != C_IN
) {
436 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
437 GET_BE_U_2(grbn
->rdclass
)));
440 s
= p
+ LWRES_GRBNREQUEST_LEN
;
441 l
= GET_BE_U_2(grbn
->namelen
);
442 advance
= lwres_printname(ndo
, l
, s
);
456 const lwres_gabnresponse_t
*gabn
;
457 const lwres_gnbaresponse_t
*gnba
;
458 const lwres_grbnresponse_t
*grbn
;
466 p
= (const u_char
*)(np
+ 1);
467 switch (GET_BE_U_4(np
->opcode
)) {
468 case LWRES_OPCODE_NOOP
:
471 case LWRES_OPCODE_GETADDRSBYNAME
:
472 gabn
= (const lwres_gabnresponse_t
*)p
;
473 ND_TCHECK_2(gabn
->realnamelen
);
475 /* BIND910: not used */
476 if (ndo
->ndo_vflag
> 2) {
477 ND_PRINT(" flags:0x%x",
478 GET_BE_U_4(gabn
->flags
));
481 ND_PRINT(" %u/%u", GET_BE_U_2(gabn
->naliases
),
482 GET_BE_U_2(gabn
->naddrs
));
484 s
= p
+ LWRES_GABNRESPONSE_LEN
;
485 l
= GET_BE_U_2(gabn
->realnamelen
);
486 advance
= lwres_printname(ndo
, l
, s
);
492 na
= GET_BE_U_2(gabn
->naliases
);
493 for (i
= 0; i
< na
; i
++) {
494 advance
= lwres_printnamelen(ndo
, s
);
501 na
= GET_BE_U_2(gabn
->naddrs
);
502 for (i
= 0; i
< na
; i
++) {
503 advance
= lwres_printaddr(ndo
, s
);
509 case LWRES_OPCODE_GETNAMEBYADDR
:
510 gnba
= (const lwres_gnbaresponse_t
*)p
;
511 ND_TCHECK_2(gnba
->realnamelen
);
513 /* BIND910: not used */
514 if (ndo
->ndo_vflag
> 2) {
515 ND_PRINT(" flags:0x%x",
516 GET_BE_U_4(gnba
->flags
));
519 ND_PRINT(" %u", GET_BE_U_2(gnba
->naliases
));
521 s
= p
+ LWRES_GNBARESPONSE_LEN
;
522 l
= GET_BE_U_2(gnba
->realnamelen
);
523 advance
= lwres_printname(ndo
, l
, s
);
529 na
= GET_BE_U_2(gnba
->naliases
);
530 for (i
= 0; i
< na
; i
++) {
531 advance
= lwres_printnamelen(ndo
, s
);
537 case LWRES_OPCODE_GETRDATABYNAME
:
538 /* XXX no trace, not tested */
539 grbn
= (const lwres_grbnresponse_t
*)p
;
540 ND_TCHECK_2(grbn
->nsigs
);
542 /* BIND910: not used */
543 if (ndo
->ndo_vflag
> 2) {
544 ND_PRINT(" flags:0x%x",
545 GET_BE_U_4(grbn
->flags
));
548 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
549 GET_BE_U_2(grbn
->rdtype
)));
550 if (GET_BE_U_2(grbn
->rdclass
) != C_IN
) {
551 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
552 GET_BE_U_2(grbn
->rdclass
)));
555 unsigned_relts_print(ndo
,
556 GET_BE_U_4(grbn
->ttl
));
557 ND_PRINT(" %u/%u", GET_BE_U_2(grbn
->nrdatas
),
558 GET_BE_U_2(grbn
->nsigs
));
560 s
= p
+ LWRES_GRBNRESPONSE_LEN
;
561 advance
= lwres_printnamelen(ndo
, s
);
567 na
= GET_BE_U_2(grbn
->nrdatas
);
568 for (i
= 0; i
< na
; i
++) {
569 /* XXX should decode resource data */
570 advance
= lwres_printbinlen(ndo
, s
);
577 na
= GET_BE_U_2(grbn
->nsigs
);
578 for (i
= 0; i
< na
; i
++) {
579 /* XXX how should we print it? */
580 advance
= lwres_printbinlen(ndo
, s
);
594 /* length mismatch */
595 if (GET_BE_U_4(np
->length
) != length
) {
596 ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np
->length
),
599 if (!unsupported
&& s
< bp
+ GET_BE_U_4(np
->length
))