xref: /freebsd/contrib/tcpdump/print-ripng.c (revision 3c602fabf9b894ff79f08a80cbb7ad3b1eb84e62)
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 
22*3c602fabSXin LI #define NETDISSECT_REWORKED
23b0453382SBill Fenner #ifdef HAVE_CONFIG_H
24b0453382SBill Fenner #include "config.h"
25b0453382SBill Fenner #endif
26b0453382SBill Fenner 
27b0453382SBill Fenner #ifdef INET6
28b0453382SBill Fenner 
295b0fe478SBruce M Simpson #include <tcpdump-stdinc.h>
30b0453382SBill Fenner 
31b0453382SBill Fenner #include "interface.h"
32b0453382SBill Fenner #include "addrtoname.h"
335b0fe478SBruce M Simpson #include "extract.h"
34b0453382SBill Fenner 
35*3c602fabSXin LI /*
36*3c602fabSXin LI  * Copyright (C) 1995, 1996, 1997 and 1998 WIDE Project.
37*3c602fabSXin LI  * All rights reserved.
38*3c602fabSXin LI  *
39*3c602fabSXin LI  * Redistribution and use in source and binary forms, with or without
40*3c602fabSXin LI  * modification, are permitted provided that the following conditions
41*3c602fabSXin LI  * are met:
42*3c602fabSXin LI  * 1. Redistributions of source code must retain the above copyright
43*3c602fabSXin LI  *    notice, this list of conditions and the following disclaimer.
44*3c602fabSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
45*3c602fabSXin LI  *    notice, this list of conditions and the following disclaimer in the
46*3c602fabSXin LI  *    documentation and/or other materials provided with the distribution.
47*3c602fabSXin LI  * 3. Neither the name of the project nor the names of its contributors
48*3c602fabSXin LI  *    may be used to endorse or promote products derived from this software
49*3c602fabSXin LI  *    without specific prior written permission.
50*3c602fabSXin LI  *
51*3c602fabSXin LI  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
52*3c602fabSXin LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*3c602fabSXin LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*3c602fabSXin LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
55*3c602fabSXin LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*3c602fabSXin LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*3c602fabSXin LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*3c602fabSXin LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*3c602fabSXin LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*3c602fabSXin LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*3c602fabSXin LI  * SUCH DAMAGE.
62*3c602fabSXin LI  */
63*3c602fabSXin LI #define	RIP6_VERSION	1
64*3c602fabSXin LI 
65*3c602fabSXin LI #define	RIP6_REQUEST	1
66*3c602fabSXin LI #define	RIP6_RESPONSE	2
67*3c602fabSXin LI 
68*3c602fabSXin LI struct netinfo6 {
69*3c602fabSXin LI 	struct in6_addr	rip6_dest;
70*3c602fabSXin LI 	uint16_t	rip6_tag;
71*3c602fabSXin LI 	uint8_t		rip6_plen;
72*3c602fabSXin LI 	uint8_t		rip6_metric;
73*3c602fabSXin LI };
74*3c602fabSXin LI 
75*3c602fabSXin LI struct	rip6 {
76*3c602fabSXin LI 	uint8_t		rip6_cmd;
77*3c602fabSXin LI 	uint8_t		rip6_vers;
78*3c602fabSXin LI 	uint8_t		rip6_res1[2];
79*3c602fabSXin LI 	union {
80*3c602fabSXin LI 		struct	netinfo6	ru6_nets[1];
81*3c602fabSXin LI 		char	ru6_tracefile[1];
82*3c602fabSXin LI 	} rip6un;
83*3c602fabSXin LI #define	rip6_nets	rip6un.ru6_nets
84*3c602fabSXin LI #define	rip6_tracefile	rip6un.ru6_tracefile
85*3c602fabSXin LI };
86*3c602fabSXin LI 
87*3c602fabSXin LI #define	HOPCNT_INFINITY6	16
88*3c602fabSXin LI 
891de50e9fSSam Leffler #if !defined(IN6_IS_ADDR_UNSPECIFIED) && !defined(_MSC_VER) /* MSVC inline */
901de50e9fSSam Leffler static int IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *addr)
911de50e9fSSam Leffler {
921de50e9fSSam Leffler     static const struct in6_addr in6addr_any;        /* :: */
931de50e9fSSam Leffler     return (memcmp(addr, &in6addr_any, sizeof(*addr)) == 0);
941de50e9fSSam Leffler }
951de50e9fSSam Leffler #endif
961de50e9fSSam Leffler 
97b0453382SBill Fenner static int
98*3c602fabSXin LI rip6_entry_print(netdissect_options *ndo, register const struct netinfo6 *ni, int metric)
99b0453382SBill Fenner {
100b0453382SBill Fenner 	int l;
101*3c602fabSXin LI 	l = ND_PRINT((ndo, "%s/%d", ip6addr_string(ndo, &ni->rip6_dest), ni->rip6_plen));
102b0453382SBill Fenner 	if (ni->rip6_tag)
103*3c602fabSXin LI 		l += ND_PRINT((ndo, " [%d]", EXTRACT_16BITS(&ni->rip6_tag)));
104b0453382SBill Fenner 	if (metric)
105*3c602fabSXin LI 		l += ND_PRINT((ndo, " (%d)", ni->rip6_metric));
106b0453382SBill Fenner 	return l;
107b0453382SBill Fenner }
108b0453382SBill Fenner 
109b0453382SBill Fenner void
110*3c602fabSXin LI ripng_print(netdissect_options *ndo, const u_char *dat, unsigned int length)
111b0453382SBill Fenner {
112b0453382SBill Fenner 	register const struct rip6 *rp = (struct rip6 *)dat;
113b0453382SBill Fenner 	register const struct netinfo6 *ni;
1145b0fe478SBruce M Simpson 	register u_int amt;
1155b0fe478SBruce M Simpson 	register u_int i;
116b0453382SBill Fenner 	int j;
117b0453382SBill Fenner 	int trunc;
118b0453382SBill Fenner 
119*3c602fabSXin LI 	if (ndo->ndo_snapend < dat)
120b0453382SBill Fenner 		return;
121*3c602fabSXin LI 	amt = ndo->ndo_snapend - dat;
1225b0fe478SBruce M Simpson 	i = min(length, amt);
1235b0fe478SBruce M Simpson 	if (i < (sizeof(struct rip6) - sizeof(struct netinfo6)))
1245b0fe478SBruce M Simpson 		return;
1255b0fe478SBruce M Simpson 	i -= (sizeof(struct rip6) - sizeof(struct netinfo6));
126b0453382SBill Fenner 
127b0453382SBill Fenner 	switch (rp->rip6_cmd) {
128b0453382SBill Fenner 
129b0453382SBill Fenner 	case RIP6_REQUEST:
130b0453382SBill Fenner 		j = length / sizeof(*ni);
131b0453382SBill Fenner 		if (j == 1
132b0453382SBill Fenner 		    &&  rp->rip6_nets->rip6_metric == HOPCNT_INFINITY6
133b0453382SBill Fenner 		    &&  IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
134*3c602fabSXin LI 			ND_PRINT((ndo, " ripng-req dump"));
135b0453382SBill Fenner 			break;
136b0453382SBill Fenner 		}
137b0453382SBill Fenner 		if (j * sizeof(*ni) != length - 4)
138*3c602fabSXin LI 			ND_PRINT((ndo, " ripng-req %d[%u]:", j, length));
139b0453382SBill Fenner 		else
140*3c602fabSXin LI 			ND_PRINT((ndo, " ripng-req %d:", j));
141b0453382SBill Fenner 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
1425b0fe478SBruce M Simpson 		for (ni = rp->rip6_nets; i >= sizeof(*ni);
1435b0fe478SBruce M Simpson 		    i -= sizeof(*ni), ++ni) {
144*3c602fabSXin LI 			if (ndo->ndo_vflag > 1)
145*3c602fabSXin LI 				ND_PRINT((ndo, "\n\t"));
146b0453382SBill Fenner 			else
147*3c602fabSXin LI 				ND_PRINT((ndo, " "));
148*3c602fabSXin LI 			rip6_entry_print(ndo, ni, 0);
149b0453382SBill Fenner 		}
150b0453382SBill Fenner 		break;
151b0453382SBill Fenner 	case RIP6_RESPONSE:
152b0453382SBill Fenner 		j = length / sizeof(*ni);
153b0453382SBill Fenner 		if (j * sizeof(*ni) != length - 4)
154*3c602fabSXin LI 			ND_PRINT((ndo, " ripng-resp %d[%u]:", j, length));
155b0453382SBill Fenner 		else
156*3c602fabSXin LI 			ND_PRINT((ndo, " ripng-resp %d:", j));
157b0453382SBill Fenner 		trunc = ((i / sizeof(*ni)) * sizeof(*ni) != i);
1585b0fe478SBruce M Simpson 		for (ni = rp->rip6_nets; i >= sizeof(*ni);
1595b0fe478SBruce M Simpson 		    i -= sizeof(*ni), ++ni) {
160*3c602fabSXin LI 			if (ndo->ndo_vflag > 1)
161*3c602fabSXin LI 				ND_PRINT((ndo, "\n\t"));
162b0453382SBill Fenner 			else
163*3c602fabSXin LI 				ND_PRINT((ndo, " "));
164*3c602fabSXin LI 			rip6_entry_print(ndo, ni, ni->rip6_metric);
165b0453382SBill Fenner 		}
166b0453382SBill Fenner 		if (trunc)
167*3c602fabSXin LI 			ND_PRINT((ndo, "[|ripng]"));
168b0453382SBill Fenner 		break;
169b0453382SBill Fenner 	default:
170*3c602fabSXin LI 		ND_PRINT((ndo, " ripng-%d ?? %u", rp->rip6_cmd, length));
171b0453382SBill Fenner 		break;
172b0453382SBill Fenner 	}
173b0453382SBill Fenner 	if (rp->rip6_vers != RIP6_VERSION)
174*3c602fabSXin LI 		ND_PRINT((ndo, " [vers %d]", rp->rip6_vers));
175b0453382SBill Fenner }
176b0453382SBill Fenner #endif /* INET6 */
177