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