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 #define ND_LONGJMP_FROM_TCHECK
42 #include "netdissect.h"
43 #include "addrtoname.h"
48 /* BIND9 lib/lwres/include/lwres */
50 * Use nd_uint16_t for lwres_uint16_t
51 * Use nd_uint32_t for lwres_uint32_t
54 struct lwres_lwpacket
{
61 nd_uint32_t recvlength
;
63 nd_uint16_t authlength
;
66 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
68 #define LWRES_LWPACKETVERSION_0 0
70 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
71 #define LWRES_FLAG_SECUREDATA 0x00000002U
76 #define LWRES_OPCODE_NOOP 0x00000000U
80 nd_uint16_t datalength
;
82 } lwres_nooprequest_t
;
86 nd_uint16_t datalength
;
88 } lwres_noopresponse_t
;
91 * get addresses by name
93 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
95 typedef struct lwres_addr lwres_addr_t
;
102 #define LWRES_ADDR_LEN 6
107 nd_uint32_t addrtypes
;
110 } lwres_gabnrequest_t
;
111 #define LWRES_GABNREQUEST_LEN 10
116 nd_uint16_t naliases
;
118 nd_uint16_t realnamelen
;
119 /* aliases follows */
121 /* realname follows */
122 } lwres_gabnresponse_t
;
123 #define LWRES_GABNRESPONSE_LEN 10
126 * get name by address
128 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
133 } lwres_gnbarequest_t
;
134 #define LWRES_GNBAREQUEST_LEN 4
139 nd_uint16_t naliases
;
140 nd_uint16_t realnamelen
;
141 /* aliases follows */
142 /* realname follows */
143 } lwres_gnbaresponse_t
;
144 #define LWRES_GNBARESPONSE_LEN 8
149 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
158 } lwres_grbnrequest_t
;
159 #define LWRES_GRBNREQUEST_LEN 10
169 /* realname here (len + name) */
170 /* rdata here (len + name) */
171 /* signatures here (len + name) */
172 } lwres_grbnresponse_t
;
173 #define LWRES_GRBNRESPONSE_LEN 16
175 #define LWRDATA_VALIDATED 0x00000001
177 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
178 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
180 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
181 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
183 static const struct tok opcode
[] = {
184 { LWRES_OPCODE_NOOP
, "noop", },
185 { LWRES_OPCODE_GETADDRSBYNAME
, "getaddrsbyname", },
186 { LWRES_OPCODE_GETNAMEBYADDR
, "getnamebyaddr", },
187 { LWRES_OPCODE_GETRDATABYNAME
, "getrdatabyname", },
192 extern const struct tok ns_type2str
[];
193 extern const struct tok ns_class2str
[];
196 lwres_printname(netdissect_options
*ndo
,
197 size_t l
, const u_char
*p0
)
200 (void)nd_printn(ndo
, p0
, l
, NULL
);
203 ND_PRINT(" (not NUL-terminated!)");
208 lwres_printnamelen(netdissect_options
*ndo
,
215 advance
= lwres_printname(ndo
, l
, p
+ 2);
220 lwres_printbinlen(netdissect_options
*ndo
,
230 for (i
= 0; i
< l
; i
++) {
231 ND_PRINT("%02x", GET_U_1(p
));
238 lwres_printaddr(netdissect_options
*ndo
,
242 const lwres_addr_t
*ap
;
247 ap
= (const lwres_addr_t
*)p
;
248 l
= GET_BE_U_2(ap
->length
);
252 switch (GET_BE_U_4(ap
->family
)) {
256 ND_PRINT(" %s", GET_IPADDR_STRING(p
));
257 p
+= sizeof(nd_ipv4
);
262 ND_PRINT(" %s", GET_IP6ADDR_STRING(p
));
263 p
+= sizeof(nd_ipv6
);
266 ND_PRINT(" %u/", GET_BE_U_4(ap
->family
));
267 for (i
= 0; i
< l
; i
++) {
268 ND_PRINT("%02x", GET_U_1(p
));
273 return ND_BYTES_BETWEEN(p
, p0
);
277 lwres_print(netdissect_options
*ndo
,
278 const u_char
*bp
, u_int length
)
281 const struct lwres_lwpacket
*np
;
288 ndo
->ndo_protocol
= "lwres";
289 np
= (const struct lwres_lwpacket
*)bp
;
290 ND_TCHECK_2(np
->authlength
);
293 v
= GET_BE_U_2(np
->version
);
294 if (ndo
->ndo_vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
296 if (v
!= LWRES_LWPACKETVERSION_0
) {
297 s
= bp
+ GET_BE_U_4(np
->length
);
301 response
= GET_BE_U_2(np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
303 /* opcode and pktflags */
304 v
= GET_BE_U_4(np
->opcode
);
305 ND_PRINT(" %s%s", tok2str(opcode
, "#0x%x", v
), response
? "" : "?");
308 v
= GET_BE_U_2(np
->pktflags
);
309 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
310 ND_PRINT("[0x%x]", v
);
312 if (ndo
->ndo_vflag
> 1) {
313 ND_PRINT(" ("); /*)*/
314 ND_PRINT("serial:0x%x", GET_BE_U_4(np
->serial
));
315 ND_PRINT(" result:0x%x", GET_BE_U_4(np
->result
));
316 ND_PRINT(" recvlen:%u", GET_BE_U_4(np
->recvlength
));
317 /* BIND910: not used */
318 if (ndo
->ndo_vflag
> 2) {
319 ND_PRINT(" authtype:0x%x", GET_BE_U_2(np
->authtype
));
320 ND_PRINT(" authlen:%u", GET_BE_U_2(np
->authlength
));
326 /* per-opcode content */
331 const lwres_gabnrequest_t
*gabn
;
332 const lwres_gnbarequest_t
*gnba
;
333 const lwres_grbnrequest_t
*grbn
;
340 p
= (const u_char
*)(np
+ 1);
341 switch (GET_BE_U_4(np
->opcode
)) {
342 case LWRES_OPCODE_NOOP
:
345 case LWRES_OPCODE_GETADDRSBYNAME
:
346 gabn
= (const lwres_gabnrequest_t
*)p
;
347 ND_TCHECK_2(gabn
->namelen
);
349 /* BIND910: not used */
350 if (ndo
->ndo_vflag
> 2) {
351 ND_PRINT(" flags:0x%x",
352 GET_BE_U_4(gabn
->flags
));
355 v
= GET_BE_U_4(gabn
->addrtypes
);
356 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
357 case LWRES_ADDRTYPE_V4
:
360 case LWRES_ADDRTYPE_V6
:
363 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
367 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
368 ND_PRINT("[0x%x]", v
);
370 s
= p
+ LWRES_GABNREQUEST_LEN
;
371 l
= GET_BE_U_2(gabn
->namelen
);
372 advance
= lwres_printname(ndo
, l
, s
);
375 case LWRES_OPCODE_GETNAMEBYADDR
:
376 gnba
= (const lwres_gnbarequest_t
*)p
;
377 ND_TCHECK_4(gnba
->flags
);
379 /* BIND910: not used */
380 if (ndo
->ndo_vflag
> 2) {
381 ND_PRINT(" flags:0x%x",
382 GET_BE_U_4(gnba
->flags
));
385 s
= p
+ LWRES_GNBAREQUEST_LEN
;
386 advance
= lwres_printaddr(ndo
, s
);
391 case LWRES_OPCODE_GETRDATABYNAME
:
392 /* XXX no trace, not tested */
393 grbn
= (const lwres_grbnrequest_t
*)p
;
394 ND_TCHECK_2(grbn
->namelen
);
396 /* BIND910: not used */
397 if (ndo
->ndo_vflag
> 2) {
398 ND_PRINT(" flags:0x%x",
399 GET_BE_U_4(grbn
->flags
));
402 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
403 GET_BE_U_2(grbn
->rdtype
)));
404 if (GET_BE_U_2(grbn
->rdclass
) != C_IN
) {
405 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
406 GET_BE_U_2(grbn
->rdclass
)));
409 s
= p
+ LWRES_GRBNREQUEST_LEN
;
410 l
= GET_BE_U_2(grbn
->namelen
);
411 advance
= lwres_printname(ndo
, l
, s
);
423 const lwres_gabnresponse_t
*gabn
;
424 const lwres_gnbaresponse_t
*gnba
;
425 const lwres_grbnresponse_t
*grbn
;
433 p
= (const u_char
*)(np
+ 1);
434 switch (GET_BE_U_4(np
->opcode
)) {
435 case LWRES_OPCODE_NOOP
:
438 case LWRES_OPCODE_GETADDRSBYNAME
:
439 gabn
= (const lwres_gabnresponse_t
*)p
;
440 ND_TCHECK_2(gabn
->realnamelen
);
442 /* BIND910: not used */
443 if (ndo
->ndo_vflag
> 2) {
444 ND_PRINT(" flags:0x%x",
445 GET_BE_U_4(gabn
->flags
));
448 ND_PRINT(" %u/%u", GET_BE_U_2(gabn
->naliases
),
449 GET_BE_U_2(gabn
->naddrs
));
451 s
= p
+ LWRES_GABNRESPONSE_LEN
;
452 l
= GET_BE_U_2(gabn
->realnamelen
);
453 advance
= lwres_printname(ndo
, l
, s
);
457 na
= GET_BE_U_2(gabn
->naliases
);
458 for (i
= 0; i
< na
; i
++) {
459 advance
= lwres_printnamelen(ndo
, s
);
464 na
= GET_BE_U_2(gabn
->naddrs
);
465 for (i
= 0; i
< na
; i
++) {
466 advance
= lwres_printaddr(ndo
, s
);
472 case LWRES_OPCODE_GETNAMEBYADDR
:
473 gnba
= (const lwres_gnbaresponse_t
*)p
;
474 ND_TCHECK_2(gnba
->realnamelen
);
476 /* BIND910: not used */
477 if (ndo
->ndo_vflag
> 2) {
478 ND_PRINT(" flags:0x%x",
479 GET_BE_U_4(gnba
->flags
));
482 ND_PRINT(" %u", GET_BE_U_2(gnba
->naliases
));
484 s
= p
+ LWRES_GNBARESPONSE_LEN
;
485 l
= GET_BE_U_2(gnba
->realnamelen
);
486 advance
= lwres_printname(ndo
, l
, s
);
490 na
= GET_BE_U_2(gnba
->naliases
);
491 for (i
= 0; i
< na
; i
++) {
492 advance
= lwres_printnamelen(ndo
, s
);
496 case LWRES_OPCODE_GETRDATABYNAME
:
497 /* XXX no trace, not tested */
498 grbn
= (const lwres_grbnresponse_t
*)p
;
499 ND_TCHECK_2(grbn
->nsigs
);
501 /* BIND910: not used */
502 if (ndo
->ndo_vflag
> 2) {
503 ND_PRINT(" flags:0x%x",
504 GET_BE_U_4(grbn
->flags
));
507 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
508 GET_BE_U_2(grbn
->rdtype
)));
509 if (GET_BE_U_2(grbn
->rdclass
) != C_IN
) {
510 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
511 GET_BE_U_2(grbn
->rdclass
)));
514 unsigned_relts_print(ndo
,
515 GET_BE_U_4(grbn
->ttl
));
516 ND_PRINT(" %u/%u", GET_BE_U_2(grbn
->nrdatas
),
517 GET_BE_U_2(grbn
->nsigs
));
519 s
= p
+ LWRES_GRBNRESPONSE_LEN
;
520 advance
= lwres_printnamelen(ndo
, s
);
524 na
= GET_BE_U_2(grbn
->nrdatas
);
525 for (i
= 0; i
< na
; i
++) {
526 /* XXX should decode resource data */
527 advance
= lwres_printbinlen(ndo
, s
);
532 na
= GET_BE_U_2(grbn
->nsigs
);
533 for (i
= 0; i
< na
; i
++) {
534 /* XXX how should we print it? */
535 advance
= lwres_printbinlen(ndo
, s
);
547 /* length mismatch */
548 if (GET_BE_U_4(np
->length
) != length
) {
549 ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np
->length
),
552 if (!unsupported
&& s
< bp
+ GET_BE_U_4(np
->length
))
557 nd_print_invalid(ndo
);