xref: /netbsd-src/external/bsd/tcpdump/dist/print-ripng.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /*
2  * Copyright (c) 1989, 1990, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 #include <sys/cdefs.h>
23 #ifndef lint
24 __RCSID("$NetBSD: print-ripng.c,v 1.9 2023/08/17 20:19:40 christos Exp $");
25 #endif
26 
27 /* \summary: IPv6 Routing Information Protocol (RIPng) printer */
28 
29 /* specification: RFC 2080 */
30 
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34 
35 #include "netdissect-stdinc.h"
36 
37 #include "netdissect.h"
38 #include "addrtoname.h"
39 #include "extract.h"
40 
41 /*
42  * Copyright (C) 1995, 1996, 1997 and 1998 WIDE Project.
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the project nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 #define	RIP6_VERSION	1
70 
71 #define	RIP6_REQUEST	1
72 #define	RIP6_RESPONSE	2
73 
74 struct netinfo6 {
75 	nd_ipv6		rip6_dest;
76 	nd_uint16_t	rip6_tag;
77 	nd_uint8_t	rip6_plen;
78 	nd_uint8_t	rip6_metric;
79 };
80 
81 struct	rip6 {
82 	nd_uint8_t	rip6_cmd;
83 	nd_uint8_t	rip6_vers;
84 	nd_byte		rip6_res1[2];
85 	struct netinfo6	rip6_nets[1];
86 };
87 
88 #define	HOPCNT_INFINITY6	16
89 
90 static int ND_IN6_IS_ADDR_UNSPECIFIED(const nd_ipv6 *addr)
91 {
92     static const nd_ipv6 in6addr_any_val = { 0 };        /* :: */
93     return (memcmp(addr, &in6addr_any_val, sizeof(*addr)) == 0);
94 }
95 
96 static void
97 rip6_entry_print(netdissect_options *ndo,
98                  const struct netinfo6 *ni, const u_int print_metric)
99 {
100 	uint16_t tag;
101 	uint8_t metric;
102 
103 	ND_PRINT("%s/%u", GET_IP6ADDR_STRING(ni->rip6_dest),
104 	         GET_U_1(ni->rip6_plen));
105 	tag = GET_BE_U_2(ni->rip6_tag);
106 	if (tag)
107 		ND_PRINT(" [%u]", tag);
108 	metric = GET_U_1(ni->rip6_metric);
109 	if (metric && print_metric)
110 		ND_PRINT(" (%u)", metric);
111 }
112 
113 UNALIGNED_OK
114 void
115 ripng_print(netdissect_options *ndo, const u_char *dat, unsigned int length)
116 {
117 	const struct rip6 *rp = (const struct rip6 *)dat;
118 	uint8_t cmd, vers;
119 	const struct netinfo6 *ni;
120 	unsigned int length_left;
121 	u_int j;
122 
123 	ndo->ndo_protocol = "ripng";
124 	vers = GET_U_1(rp->rip6_vers);
125 	if (vers != RIP6_VERSION) {
126 		nd_print_protocol(ndo);
127 		ND_PRINT(" [version %u, must be %u]", vers, RIP6_VERSION);
128 		goto invalid;
129 	}
130 	cmd = GET_U_1(rp->rip6_cmd);
131 	switch (cmd) {
132 
133 	case RIP6_REQUEST:
134 		length_left = length;
135 		if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
136 			goto invalid;
137 		length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
138 		j = length_left / sizeof(*ni);
139 		if (j == 1) {
140 			if (GET_U_1(rp->rip6_nets->rip6_metric) == HOPCNT_INFINITY6
141 			    && ND_IN6_IS_ADDR_UNSPECIFIED(&rp->rip6_nets->rip6_dest)) {
142 				ND_PRINT(" ripng-req dump");
143 				break;
144 			}
145 		}
146 		if (j * sizeof(*ni) != length_left)
147 			ND_PRINT(" ripng-req %u[%u]:", j, length);
148 		else
149 			ND_PRINT(" ripng-req %u:", j);
150 		for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
151 		    length_left -= sizeof(*ni), ++ni) {
152 			if (ndo->ndo_vflag > 1)
153 				ND_PRINT("\n\t");
154 			else
155 				ND_PRINT(" ");
156 			rip6_entry_print(ndo, ni, FALSE);
157 		}
158 		if (length_left != 0)
159 			goto invalid;
160 		break;
161 	case RIP6_RESPONSE:
162 		length_left = length;
163 		if (length_left < (sizeof(struct rip6) - sizeof(struct netinfo6)))
164 			goto invalid;
165 		length_left -= (sizeof(struct rip6) - sizeof(struct netinfo6));
166 		j = length_left / sizeof(*ni);
167 		if (j * sizeof(*ni) != length_left)
168 			ND_PRINT(" ripng-resp %u[%u]:", j, length);
169 		else
170 			ND_PRINT(" ripng-resp %u:", j);
171 		for (ni = rp->rip6_nets; length_left >= sizeof(*ni);
172 		    length_left -= sizeof(*ni), ++ni) {
173 			if (ndo->ndo_vflag > 1)
174 				ND_PRINT("\n\t");
175 			else
176 				ND_PRINT(" ");
177 			rip6_entry_print(ndo, ni, TRUE);
178 		}
179 		if (length_left != 0)
180 			goto invalid;
181 		break;
182 	default:
183 		ND_PRINT(" ripng-%u ?? %u", cmd, length);
184 		goto invalid;
185 	}
186 	return;
187 
188 invalid:
189 	nd_print_invalid(ndo);
190 }
191