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 */
34 #include "netdissect-stdinc.h"
36 #define ND_LONGJMP_FROM_TCHECK
37 #include "netdissect.h"
38 #include "addrtoname.h"
43 /* BIND9 lib/lwres/include/lwres */
45 * Use nd_uint16_t for lwres_uint16_t
46 * Use nd_uint32_t for lwres_uint32_t
49 struct lwres_lwpacket
{
56 nd_uint32_t recvlength
;
58 nd_uint16_t authlength
;
61 #define LWRES_LWPACKETFLAG_RESPONSE 0x0001U /* if set, pkt is a response */
63 #define LWRES_LWPACKETVERSION_0 0
65 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
66 #define LWRES_FLAG_SECUREDATA 0x00000002U
71 #define LWRES_OPCODE_NOOP 0x00000000U
75 nd_uint16_t datalength
;
77 } lwres_nooprequest_t
;
81 nd_uint16_t datalength
;
83 } lwres_noopresponse_t
;
86 * get addresses by name
88 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
90 typedef struct lwres_addr lwres_addr_t
;
97 #define LWRES_ADDR_LEN 6
102 nd_uint32_t addrtypes
;
105 } lwres_gabnrequest_t
;
106 #define LWRES_GABNREQUEST_LEN 10
111 nd_uint16_t naliases
;
113 nd_uint16_t realnamelen
;
114 /* aliases follows */
116 /* realname follows */
117 } lwres_gabnresponse_t
;
118 #define LWRES_GABNRESPONSE_LEN 10
121 * get name by address
123 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
128 } lwres_gnbarequest_t
;
129 #define LWRES_GNBAREQUEST_LEN 4
134 nd_uint16_t naliases
;
135 nd_uint16_t realnamelen
;
136 /* aliases follows */
137 /* realname follows */
138 } lwres_gnbaresponse_t
;
139 #define LWRES_GNBARESPONSE_LEN 8
144 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
153 } lwres_grbnrequest_t
;
154 #define LWRES_GRBNREQUEST_LEN 10
164 /* realname here (len + name) */
165 /* rdata here (len + name) */
166 /* signatures here (len + name) */
167 } lwres_grbnresponse_t
;
168 #define LWRES_GRBNRESPONSE_LEN 16
170 #define LWRDATA_VALIDATED 0x00000001
172 #define LWRES_ADDRTYPE_V4 0x00000001U /* ipv4 */
173 #define LWRES_ADDRTYPE_V6 0x00000002U /* ipv6 */
175 #define LWRES_MAX_ALIASES 16 /* max # of aliases */
176 #define LWRES_MAX_ADDRS 64 /* max # of addrs */
178 static const struct tok opcode
[] = {
179 { LWRES_OPCODE_NOOP
, "noop", },
180 { LWRES_OPCODE_GETADDRSBYNAME
, "getaddrsbyname", },
181 { LWRES_OPCODE_GETNAMEBYADDR
, "getnamebyaddr", },
182 { LWRES_OPCODE_GETRDATABYNAME
, "getrdatabyname", },
187 extern const struct tok ns_type2str
[];
188 extern const struct tok ns_class2str
[];
191 lwres_printname(netdissect_options
*ndo
,
192 u_int l
, const u_char
*p0
)
195 nd_printjn(ndo
, p0
, l
);
198 ND_PRINT(" (not NUL-terminated!)");
203 lwres_printnamelen(netdissect_options
*ndo
,
210 advance
= lwres_printname(ndo
, l
, p
+ 2);
215 lwres_printbinlen(netdissect_options
*ndo
,
225 for (i
= 0; i
< l
; i
++) {
226 ND_PRINT("%02x", GET_U_1(p
));
233 lwres_printaddr(netdissect_options
*ndo
,
237 const lwres_addr_t
*ap
;
242 ap
= (const lwres_addr_t
*)p
;
243 l
= GET_BE_U_2(ap
->length
);
247 switch (GET_BE_U_4(ap
->family
)) {
251 ND_PRINT(" %s", GET_IPADDR_STRING(p
));
252 p
+= sizeof(nd_ipv4
);
257 ND_PRINT(" %s", GET_IP6ADDR_STRING(p
));
258 p
+= sizeof(nd_ipv6
);
261 ND_PRINT(" %u/", GET_BE_U_4(ap
->family
));
262 for (i
= 0; i
< l
; i
++) {
263 ND_PRINT("%02x", GET_U_1(p
));
268 return ND_BYTES_BETWEEN(p0
, p
);
272 lwres_print(netdissect_options
*ndo
,
273 const u_char
*bp
, u_int length
)
276 const struct lwres_lwpacket
*np
;
283 ndo
->ndo_protocol
= "lwres";
284 np
= (const struct lwres_lwpacket
*)bp
;
285 ND_TCHECK_2(np
->authlength
);
288 v
= GET_BE_U_2(np
->version
);
289 if (ndo
->ndo_vflag
|| v
!= LWRES_LWPACKETVERSION_0
)
291 if (v
!= LWRES_LWPACKETVERSION_0
) {
292 uint32_t pkt_len
= GET_BE_U_4(np
->length
);
293 ND_TCHECK_LEN(bp
, pkt_len
);
298 response
= GET_BE_U_2(np
->pktflags
) & LWRES_LWPACKETFLAG_RESPONSE
;
300 /* opcode and pktflags */
301 v
= GET_BE_U_4(np
->opcode
);
302 ND_PRINT(" %s%s", tok2str(opcode
, "#0x%x", v
), response
? "" : "?");
305 v
= GET_BE_U_2(np
->pktflags
);
306 if (v
& ~LWRES_LWPACKETFLAG_RESPONSE
)
307 ND_PRINT("[0x%x]", v
);
309 if (ndo
->ndo_vflag
> 1) {
310 ND_PRINT(" ("); /*)*/
311 ND_PRINT("serial:0x%x", GET_BE_U_4(np
->serial
));
312 ND_PRINT(" result:0x%x", GET_BE_U_4(np
->result
));
313 ND_PRINT(" recvlen:%u", GET_BE_U_4(np
->recvlength
));
314 /* BIND910: not used */
315 if (ndo
->ndo_vflag
> 2) {
316 ND_PRINT(" authtype:0x%x", GET_BE_U_2(np
->authtype
));
317 ND_PRINT(" authlen:%u", GET_BE_U_2(np
->authlength
));
323 /* per-opcode content */
328 const lwres_gabnrequest_t
*gabn
;
329 const lwres_gnbarequest_t
*gnba
;
330 const lwres_grbnrequest_t
*grbn
;
337 p
= (const u_char
*)(np
+ 1);
338 switch (GET_BE_U_4(np
->opcode
)) {
339 case LWRES_OPCODE_NOOP
:
342 case LWRES_OPCODE_GETADDRSBYNAME
:
343 gabn
= (const lwres_gabnrequest_t
*)p
;
344 ND_TCHECK_2(gabn
->namelen
);
346 /* BIND910: not used */
347 if (ndo
->ndo_vflag
> 2) {
348 ND_PRINT(" flags:0x%x",
349 GET_BE_U_4(gabn
->flags
));
352 v
= GET_BE_U_4(gabn
->addrtypes
);
353 switch (v
& (LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
)) {
354 case LWRES_ADDRTYPE_V4
:
357 case LWRES_ADDRTYPE_V6
:
360 case LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
:
364 if (v
& ~(LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
))
365 ND_PRINT("[0x%x]", v
);
367 s
= p
+ LWRES_GABNREQUEST_LEN
;
368 l
= GET_BE_U_2(gabn
->namelen
);
369 advance
= lwres_printname(ndo
, l
, s
);
372 case LWRES_OPCODE_GETNAMEBYADDR
:
373 gnba
= (const lwres_gnbarequest_t
*)p
;
374 ND_TCHECK_4(gnba
->flags
);
376 /* BIND910: not used */
377 if (ndo
->ndo_vflag
> 2) {
378 ND_PRINT(" flags:0x%x",
379 GET_BE_U_4(gnba
->flags
));
382 s
= p
+ LWRES_GNBAREQUEST_LEN
;
383 advance
= lwres_printaddr(ndo
, s
);
388 case LWRES_OPCODE_GETRDATABYNAME
:
389 /* XXX no trace, not tested */
390 grbn
= (const lwres_grbnrequest_t
*)p
;
391 ND_TCHECK_2(grbn
->namelen
);
393 /* BIND910: not used */
394 if (ndo
->ndo_vflag
> 2) {
395 ND_PRINT(" flags:0x%x",
396 GET_BE_U_4(grbn
->flags
));
399 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
400 GET_BE_U_2(grbn
->rdtype
)));
401 if (GET_BE_U_2(grbn
->rdclass
) != C_IN
) {
402 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
403 GET_BE_U_2(grbn
->rdclass
)));
406 s
= p
+ LWRES_GRBNREQUEST_LEN
;
407 l
= GET_BE_U_2(grbn
->namelen
);
408 advance
= lwres_printname(ndo
, l
, s
);
420 const lwres_gabnresponse_t
*gabn
;
421 const lwres_gnbaresponse_t
*gnba
;
422 const lwres_grbnresponse_t
*grbn
;
430 p
= (const u_char
*)(np
+ 1);
431 switch (GET_BE_U_4(np
->opcode
)) {
432 case LWRES_OPCODE_NOOP
:
435 case LWRES_OPCODE_GETADDRSBYNAME
:
436 gabn
= (const lwres_gabnresponse_t
*)p
;
437 ND_TCHECK_2(gabn
->realnamelen
);
439 /* BIND910: not used */
440 if (ndo
->ndo_vflag
> 2) {
441 ND_PRINT(" flags:0x%x",
442 GET_BE_U_4(gabn
->flags
));
445 ND_PRINT(" %u/%u", GET_BE_U_2(gabn
->naliases
),
446 GET_BE_U_2(gabn
->naddrs
));
448 s
= p
+ LWRES_GABNRESPONSE_LEN
;
449 l
= GET_BE_U_2(gabn
->realnamelen
);
450 advance
= lwres_printname(ndo
, l
, s
);
454 na
= GET_BE_U_2(gabn
->naliases
);
455 for (i
= 0; i
< na
; i
++) {
456 advance
= lwres_printnamelen(ndo
, s
);
461 na
= GET_BE_U_2(gabn
->naddrs
);
462 for (i
= 0; i
< na
; i
++) {
463 advance
= lwres_printaddr(ndo
, s
);
469 case LWRES_OPCODE_GETNAMEBYADDR
:
470 gnba
= (const lwres_gnbaresponse_t
*)p
;
471 ND_TCHECK_2(gnba
->realnamelen
);
473 /* BIND910: not used */
474 if (ndo
->ndo_vflag
> 2) {
475 ND_PRINT(" flags:0x%x",
476 GET_BE_U_4(gnba
->flags
));
479 ND_PRINT(" %u", GET_BE_U_2(gnba
->naliases
));
481 s
= p
+ LWRES_GNBARESPONSE_LEN
;
482 l
= GET_BE_U_2(gnba
->realnamelen
);
483 advance
= lwres_printname(ndo
, l
, s
);
487 na
= GET_BE_U_2(gnba
->naliases
);
488 for (i
= 0; i
< na
; i
++) {
489 advance
= lwres_printnamelen(ndo
, s
);
493 case LWRES_OPCODE_GETRDATABYNAME
:
494 /* XXX no trace, not tested */
495 grbn
= (const lwres_grbnresponse_t
*)p
;
496 ND_TCHECK_2(grbn
->nsigs
);
498 /* BIND910: not used */
499 if (ndo
->ndo_vflag
> 2) {
500 ND_PRINT(" flags:0x%x",
501 GET_BE_U_4(grbn
->flags
));
504 ND_PRINT(" %s", tok2str(ns_type2str
, "Type%u",
505 GET_BE_U_2(grbn
->rdtype
)));
506 if (GET_BE_U_2(grbn
->rdclass
) != C_IN
) {
507 ND_PRINT(" %s", tok2str(ns_class2str
, "Class%u",
508 GET_BE_U_2(grbn
->rdclass
)));
511 unsigned_relts_print(ndo
,
512 GET_BE_U_4(grbn
->ttl
));
513 ND_PRINT(" %u/%u", GET_BE_U_2(grbn
->nrdatas
),
514 GET_BE_U_2(grbn
->nsigs
));
516 s
= p
+ LWRES_GRBNRESPONSE_LEN
;
517 advance
= lwres_printnamelen(ndo
, s
);
521 na
= GET_BE_U_2(grbn
->nrdatas
);
522 for (i
= 0; i
< na
; i
++) {
523 /* XXX should decode resource data */
524 advance
= lwres_printbinlen(ndo
, s
);
529 na
= GET_BE_U_2(grbn
->nsigs
);
530 for (i
= 0; i
< na
; i
++) {
531 /* XXX how should we print it? */
532 advance
= lwres_printbinlen(ndo
, s
);
544 /* length mismatch */
545 if (GET_BE_U_4(np
->length
) != length
) {
546 ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np
->length
),
549 if (!unsupported
&& ND_BYTES_BETWEEN(bp
, s
) < GET_BE_U_4(np
->length
))
554 nd_print_invalid(ndo
);