xref: /freebsd/contrib/tcpdump/print-ripng.c (revision ee67461e56828dd1f8de165947ba83f6d9148a87)
1b0453382SBill Fenner /*
2b0453382SBill Fenner  * Copyright (c) 1989, 1990, 1991, 1993, 1994
3b0453382SBill Fenner  *	The Regents of the University of California.  All rights reserved.
4b0453382SBill Fenner  *
5b0453382SBill Fenner  * Redistribution and use in source and binary forms, with or without
6b0453382SBill Fenner  * modification, are permitted provided that: (1) source code distributions
7b0453382SBill Fenner  * retain the above copyright notice and this paragraph in its entirety, (2)
8b0453382SBill Fenner  * distributions including binary code include the above copyright notice and
9b0453382SBill Fenner  * this paragraph in its entirety in the documentation or other materials
10b0453382SBill Fenner  * provided with the distribution, and (3) all advertising materials mentioning
11b0453382SBill Fenner  * features or use of this software display the following acknowledgement:
12b0453382SBill Fenner  * ``This product includes software developed by the University of California,
13b0453382SBill Fenner  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14b0453382SBill Fenner  * the University nor the names of its contributors may be used to endorse
15b0453382SBill Fenner  * or promote products derived from this software without specific prior
16b0453382SBill Fenner  * written permission.
17b0453382SBill Fenner  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18b0453382SBill Fenner  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19b0453382SBill Fenner  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20b0453382SBill Fenner  */
21b0453382SBill Fenner 
223340d773SGleb Smirnoff /* \summary: IPv6 Routing Information Protocol (RIPng) printer */
233340d773SGleb Smirnoff 
24*ee67461eSJoseph Mingrone /* specification: RFC 2080 */
25*ee67461eSJoseph Mingrone 
26b0453382SBill Fenner #ifdef HAVE_CONFIG_H
27*ee67461eSJoseph Mingrone #include <config.h>
28b0453382SBill Fenner #endif
29b0453382SBill Fenner 
30*ee67461eSJoseph Mingrone #include "netdissect-stdinc.h"
31b0453382SBill Fenner 
323340d773SGleb Smirnoff #include "netdissect.h"
33b0453382SBill Fenner #include "addrtoname.h"
345b0fe478SBruce M Simpson #include "extract.h"
35b0453382SBill Fenner 
363c602fabSXin LI /*
373c602fabSXin LI  * Copyright (C) 1995, 1996, 1997 and 1998 WIDE Project.
383c602fabSXin LI  * All rights reserved.
393c602fabSXin LI  *
403c602fabSXin LI  * Redistribution and use in source and binary forms, with or without
413c602fabSXin LI  * modification, are permitted provided that the following conditions
423c602fabSXin LI  * are met:
433c602fabSXin LI  * 1. Redistributions of source code must retain the above copyright
443c602fabSXin LI  *    notice, this list of conditions and the following disclaimer.
453c602fabSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
463c602fabSXin LI  *    notice, this list of conditions and the following disclaimer in the
473c602fabSXin LI  *    documentation and/or other materials provided with the distribution.
483c602fabSXin LI  * 3. Neither the name of the project nor the names of its contributors
493c602fabSXin LI  *    may be used to endorse or promote products derived from this software
503c602fabSXin LI  *    without specific prior written permission.
513c602fabSXin LI  *
523c602fabSXin LI  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
533c602fabSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
543c602fabSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
553c602fabSXin LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
563c602fabSXin LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
573c602fabSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
583c602fabSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
593c602fabSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
603c602fabSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
613c602fabSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
623c602fabSXin LI  * SUCH DAMAGE.
633c602fabSXin LI  */
643c602fabSXin LI #define	RIP6_VERSION	1
653c602fabSXin LI 
663c602fabSXin LI #define	RIP6_REQUEST	1
673c602fabSXin LI #define	RIP6_RESPONSE	2
683c602fabSXin LI 
693c602fabSXin LI struct netinfo6 {
70*ee67461eSJoseph Mingrone 	nd_ipv6		rip6_dest;
71*ee67461eSJoseph Mingrone 	nd_uint16_t	rip6_tag;
72*ee67461eSJoseph Mingrone 	nd_uint8_t	rip6_plen;
73*ee67461eSJoseph Mingrone 	nd_uint8_t	rip6_metric;
743c602fabSXin LI };
753c602fabSXin LI 
763c602fabSXin LI struct	rip6 {
77*ee67461eSJoseph Mingrone 	nd_uint8_t	rip6_cmd;
78*ee67461eSJoseph Mingrone 	nd_uint8_t	rip6_vers;
79*ee67461eSJoseph Mingrone 	nd_byte		rip6_res1[2];
80*ee67461eSJoseph Mingrone 	struct netinfo6	rip6_nets[1];
813c602fabSXin LI };
823c602fabSXin LI 
833c602fabSXin LI #define	HOPCNT_INFINITY6	16
843c602fabSXin LI 
85*ee67461eSJoseph Mingrone static int ND_IN6_IS_ADDR_UNSPECIFIED(const nd_ipv6 *addr)
861de50e9fSSam Leffler {
87*ee67461eSJoseph Mingrone     static const nd_ipv6 in6addr_any_val = { 0 };        /* :: */
88*ee67461eSJoseph Mingrone     return (memcmp(addr, &in6addr_any_val, sizeof(*addr)) == 0);
891de50e9fSSam Leffler }
901de50e9fSSam Leffler 
91*ee67461eSJoseph Mingrone static void
92*ee67461eSJoseph Mingrone rip6_entry_print(netdissect_options *ndo,
93*ee67461eSJoseph Mingrone                  const struct netinfo6 *ni, const u_int print_metric)
94b0453382SBill Fenner {
95*ee67461eSJoseph Mingrone 	uint16_t tag;
96*ee67461eSJoseph Mingrone 	uint8_t metric;
97*ee67461eSJoseph Mingrone 
98*ee67461eSJoseph Mingrone 	ND_PRINT("%s/%u", GET_IP6ADDR_STRING(ni->rip6_dest),
99*ee67461eSJoseph Mingrone 	         GET_U_1(ni->rip6_plen));
100*ee67461eSJoseph Mingrone 	tag = GET_BE_U_2(ni->rip6_tag);
101*ee67461eSJoseph Mingrone 	if (tag)
102*ee67461eSJoseph Mingrone 		ND_PRINT(" [%u]", tag);
103*ee67461eSJoseph Mingrone 	metric = GET_U_1(ni->rip6_metric);
104*ee67461eSJoseph Mingrone 	if (metric && print_metric)
105*ee67461eSJoseph Mingrone 		ND_PRINT(" (%u)", metric);
106b0453382SBill Fenner }
107b0453382SBill Fenner 
108b0453382SBill Fenner void
1093c602fabSXin LI ripng_print(netdissect_options *ndo, const u_char *dat, unsigned int length)
110b0453382SBill Fenner {
111*ee67461eSJoseph Mingrone 	const struct rip6 *rp = (const struct rip6 *)dat;
112*ee67461eSJoseph Mingrone 	uint8_t cmd, vers;
113*ee67461eSJoseph Mingrone 	const struct netinfo6 *ni;
1140bff6a5aSEd Maste 	unsigned int length_left;
1150bff6a5aSEd Maste 	u_int j;
116b0453382SBill Fenner 
117*ee67461eSJoseph Mingrone 	ndo->ndo_protocol = "ripng";
118*ee67461eSJoseph Mingrone 	vers = GET_U_1(rp->rip6_vers);
119*ee67461eSJoseph Mingrone 	if (vers != RIP6_VERSION) {
120*ee67461eSJoseph Mingrone 		nd_print_protocol(ndo);
121*ee67461eSJoseph Mingrone 		ND_PRINT(" [version %u, must be %u]", vers, RIP6_VERSION);
122*ee67461eSJoseph Mingrone 		goto invalid;
123*ee67461eSJoseph Mingrone 	}
124*ee67461eSJoseph Mingrone 	cmd = GET_U_1(rp->rip6_cmd);
125*ee67461eSJoseph Mingrone 	switch (cmd) {
126b0453382SBill Fenner 
127b0453382SBill Fenner 	case RIP6_REQUEST:
1280bff6a5aSEd Maste 		length_left = length;
1290bff6a5aSEd Maste 		if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
130*ee67461eSJoseph Mingrone 			goto invalid;
1310bff6a5aSEd Maste 		length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
1320bff6a5aSEd Maste 		j = length_left / sizeof(*ni);
1330bff6a5aSEd Maste 		if (j == 1) {
134*ee67461eSJoseph Mingrone 			if (GET_U_1(rp->rip6_nets->rip6_metric) == HOPCNT_INFINITY6
135*ee67461eSJoseph Mingrone 			    && ND_IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
136*ee67461eSJoseph Mingrone 				ND_PRINT(" ripng-req dump");
137b0453382SBill Fenner 				break;
138b0453382SBill Fenner 			}
1390bff6a5aSEd Maste 		}
1400bff6a5aSEd Maste 		if (j * sizeof(*ni) != length_left)
141*ee67461eSJoseph Mingrone 			ND_PRINT(" ripng-req %u[%u]:", j, length);
142b0453382SBill Fenner 		else
143*ee67461eSJoseph Mingrone 			ND_PRINT(" ripng-req %u:", j);
1440bff6a5aSEd Maste 		for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
1450bff6a5aSEd Maste 		    length_left -= sizeof(*ni), ++ni) {
1463c602fabSXin LI 			if (ndo->ndo_vflag > 1)
147*ee67461eSJoseph Mingrone 				ND_PRINT("\n\t");
148b0453382SBill Fenner 			else
149*ee67461eSJoseph Mingrone 				ND_PRINT(" ");
150*ee67461eSJoseph Mingrone 			rip6_entry_print(ndo, ni, FALSE);
151b0453382SBill Fenner 		}
1520bff6a5aSEd Maste 		if (length_left != 0)
153*ee67461eSJoseph Mingrone 			goto invalid;
154b0453382SBill Fenner 		break;
155b0453382SBill Fenner 	case RIP6_RESPONSE:
1560bff6a5aSEd Maste 		length_left = length;
1570bff6a5aSEd Maste 		if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
158*ee67461eSJoseph Mingrone 			goto invalid;
1590bff6a5aSEd Maste 		length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
1600bff6a5aSEd Maste 		j = length_left / sizeof(*ni);
1610bff6a5aSEd Maste 		if (j * sizeof(*ni) != length_left)
162*ee67461eSJoseph Mingrone 			ND_PRINT(" ripng-resp %u[%u]:", j, length);
163b0453382SBill Fenner 		else
164*ee67461eSJoseph Mingrone 			ND_PRINT(" ripng-resp %u:", j);
1650bff6a5aSEd Maste 		for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
1660bff6a5aSEd Maste 		    length_left -= sizeof(*ni), ++ni) {
1673c602fabSXin LI 			if (ndo->ndo_vflag > 1)
168*ee67461eSJoseph Mingrone 				ND_PRINT("\n\t");
169b0453382SBill Fenner 			else
170*ee67461eSJoseph Mingrone 				ND_PRINT(" ");
171*ee67461eSJoseph Mingrone 			rip6_entry_print(ndo, ni, TRUE);
172b0453382SBill Fenner 		}
1730bff6a5aSEd Maste 		if (length_left != 0)
174*ee67461eSJoseph Mingrone 			goto invalid;
175b0453382SBill Fenner 		break;
176b0453382SBill Fenner 	default:
177*ee67461eSJoseph Mingrone 		ND_PRINT(" ripng-%u ?? %u", cmd, length);
178*ee67461eSJoseph Mingrone 		goto invalid;
179b0453382SBill Fenner 	}
1800bff6a5aSEd Maste 	return;
1810bff6a5aSEd Maste 
182*ee67461eSJoseph Mingrone invalid:
183*ee67461eSJoseph Mingrone 	nd_print_invalid(ndo);
184b0453382SBill Fenner }
185