xref: /openbsd-src/sys/netinet6/ip6_forward.c (revision fc405d53b73a2d73393cb97f684863d17b583e38)
1 /*	$OpenBSD: ip6_forward.c,v 1.109 2023/04/05 13:56:31 kn Exp $	*/
2 /*	$KAME: ip6_forward.c,v 1.75 2001/06/29 12:42:13 jinmei Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include "pf.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/errno.h>
40 #include <sys/time.h>
41 #include <sys/kernel.h>
42 #include <sys/syslog.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_enc.h>
47 #include <net/route.h>
48 
49 #include <netinet/in.h>
50 #include <netinet/ip_var.h>
51 #include <netinet6/in6_var.h>
52 #include <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/icmp6.h>
55 #include <netinet6/nd6.h>
56 
57 #if NPF > 0
58 #include <net/pfvar.h>
59 #endif
60 
61 #ifdef IPSEC
62 #include <netinet/ip_ipsp.h>
63 #include <netinet/ip_ah.h>
64 #include <netinet/ip_esp.h>
65 #include <netinet/udp.h>
66 #include <netinet/tcp.h>
67 #endif
68 
69 /*
70  * Forward a packet.  If some error occurs return the sender
71  * an icmp packet.  Note we can't always generate a meaningful
72  * icmp message because icmp doesn't have a large enough repertoire
73  * of codes and types.
74  *
75  * If not forwarding, just drop the packet.  This could be confusing
76  * if ipforwarding was zero but some routing protocol was advancing
77  * us as a gateway to somewhere.  However, we must let the routing
78  * protocol deal with that.
79  *
80  */
81 
82 void
83 ip6_forward(struct mbuf *m, struct rtentry *rt, int srcrt)
84 {
85 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
86 	struct sockaddr_in6 *sin6;
87 	struct route_in6 ro;
88 	struct ifnet *ifp = NULL;
89 	int error = 0, type = 0, code = 0, destmtu = 0;
90 	struct mbuf *mcopy = NULL;
91 #ifdef IPSEC
92 	struct tdb *tdb = NULL;
93 #endif /* IPSEC */
94 	char src6[INET6_ADDRSTRLEN], dst6[INET6_ADDRSTRLEN];
95 
96 	/*
97 	 * Do not forward packets to multicast destination (should be handled
98 	 * by ip6_mforward().
99 	 * Do not forward packets with unspecified source.  It was discussed
100 	 * in July 2000, on ipngwg mailing list.
101 	 */
102 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
103 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
104 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
105 		time_t uptime;
106 
107 		ip6stat_inc(ip6s_cantforward);
108 		uptime = getuptime();
109 
110 		if (ip6_log_time + ip6_log_interval < uptime) {
111 			ip6_log_time = uptime;
112 			inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6));
113 			inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6));
114 			log(LOG_DEBUG,
115 			    "cannot forward "
116 			    "from %s to %s nxt %d received on interface %u\n",
117 			    src6, dst6,
118 			    ip6->ip6_nxt,
119 			    m->m_pkthdr.ph_ifidx);
120 		}
121 		m_freem(m);
122 		goto out;
123 	}
124 
125 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
126 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
127 				ICMP6_TIME_EXCEED_TRANSIT, 0);
128 		goto out;
129 	}
130 	ip6->ip6_hlim -= IPV6_HLIMDEC;
131 
132 	/*
133 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
134 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
135 	 * we need to generate an ICMP6 message to the src.
136 	 * Thanks to M_EXT, in most cases copy will not occur.
137 	 *
138 	 * It is important to save it before IPsec processing as IPsec
139 	 * processing may modify the mbuf.
140 	 */
141 	mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
142 	    M_NOWAIT);
143 
144 #if NPF > 0
145 reroute:
146 #endif
147 
148 #ifdef IPSEC
149 	if (ipsec_in_use) {
150 		error = ip6_output_ipsec_lookup(m, NULL, &tdb);
151 		if (error) {
152 			/*
153 			 * -EINVAL is used to indicate that the packet should
154 			 * be silently dropped, typically because we've asked
155 			 * key management for an SA.
156 			 */
157 			if (error == -EINVAL) /* Should silently drop packet */
158 				error = 0;
159 
160 			m_freem(m);
161 			goto freecopy;
162 		}
163 	}
164 #endif /* IPSEC */
165 
166 	memset(&ro, 0, sizeof(ro));
167 	sin6 = &ro.ro_dst;
168 	sin6->sin6_family = AF_INET6;
169 	sin6->sin6_len = sizeof(*sin6);
170 	sin6->sin6_addr = ip6->ip6_dst;
171 
172 	if (!rtisvalid(rt)) {
173 		rtfree(rt);
174 		rt = rtalloc_mpath(sin6tosa(sin6), &ip6->ip6_src.s6_addr32[0],
175 		    m->m_pkthdr.ph_rtableid);
176 		if (rt == NULL) {
177 			ip6stat_inc(ip6s_noroute);
178 			if (mcopy) {
179 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
180 					    ICMP6_DST_UNREACH_NOROUTE, 0);
181 			}
182 			m_freem(m);
183 			goto out;
184 		}
185 	}
186 
187 	/*
188 	 * Scope check: if a packet can't be delivered to its destination
189 	 * for the reason that the destination is beyond the scope of the
190 	 * source address, discard the packet and return an icmp6 destination
191 	 * unreachable error with Code 2 (beyond scope of source address).
192 	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
193 	 */
194 	if (in6_addr2scopeid(m->m_pkthdr.ph_ifidx, &ip6->ip6_src) !=
195 	    in6_addr2scopeid(rt->rt_ifidx, &ip6->ip6_src)) {
196 		time_t uptime;
197 
198 		ip6stat_inc(ip6s_cantforward);
199 		ip6stat_inc(ip6s_badscope);
200 		uptime = getuptime();
201 
202 		if (ip6_log_time + ip6_log_interval < uptime) {
203 			ip6_log_time = uptime;
204 			inet_ntop(AF_INET6, &ip6->ip6_src, src6, sizeof(src6));
205 			inet_ntop(AF_INET6, &ip6->ip6_dst, dst6, sizeof(dst6));
206 			log(LOG_DEBUG,
207 			    "cannot forward "
208 			    "src %s, dst %s, nxt %d, rcvif %u, outif %u\n",
209 			    src6, dst6,
210 			    ip6->ip6_nxt,
211 			    m->m_pkthdr.ph_ifidx, rt->rt_ifidx);
212 		}
213 		if (mcopy)
214 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
215 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
216 		m_freem(m);
217 		goto out;
218 	}
219 
220 #ifdef IPSEC
221 	/*
222 	 * Check if the packet needs encapsulation.
223 	 * ipsp_process_packet will never come back to here.
224 	 */
225 	if (tdb != NULL) {
226 		/* Callee frees mbuf */
227 		ro.ro_rt = rt;
228 		ro.ro_tableid = m->m_pkthdr.ph_rtableid;
229 		error = ip6_output_ipsec_send(tdb, m, &ro, 0, 1);
230 		rt = ro.ro_rt;
231 		if (error)
232 			goto senderr;
233 		goto freecopy;
234 	}
235 #endif /* IPSEC */
236 
237 	if (rt->rt_flags & RTF_GATEWAY)
238 		sin6 = satosin6(rt->rt_gateway);
239 
240 	/*
241 	 * If we are to forward the packet using the same interface
242 	 * as one we got the packet from, perhaps we should send a redirect
243 	 * to sender to shortcut a hop.
244 	 * Only send redirect if source is sending directly to us,
245 	 * and if packet was not source routed (or has any options).
246 	 * Also, don't send redirect if forwarding using a route
247 	 * modified by a redirect.
248 	 */
249 	ifp = if_get(rt->rt_ifidx);
250 	if (ifp == NULL) {
251 		m_freem(m);
252 		goto freecopy;
253 	}
254 	if (rt->rt_ifidx == m->m_pkthdr.ph_ifidx && !srcrt &&
255 	    ip6_sendredirects &&
256 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
257 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
258 		    nd6_is_addr_neighbor(&ro.ro_dst, ifp)) {
259 			/*
260 			 * If the incoming interface is equal to the outgoing
261 			 * one, the link attached to the interface is
262 			 * point-to-point, and the IPv6 destination is
263 			 * regarded as on-link on the link, then it will be
264 			 * highly probable that the destination address does
265 			 * not exist on the link and that the packet is going
266 			 * to loop.  Thus, we immediately drop the packet and
267 			 * send an ICMPv6 error message.
268 			 * For other routing loops, we dare to let the packet
269 			 * go to the loop, so that a remote diagnosing host
270 			 * can detect the loop by traceroute.
271 			 * type/code is based on suggestion by Rich Draves.
272 			 * not sure if it is the best pick.
273 			 */
274 			if (mcopy)
275 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
276 				    ICMP6_DST_UNREACH_ADDR, 0);
277 			m_freem(m);
278 			goto out;
279 		}
280 		type = ND_REDIRECT;
281 	}
282 
283 	/*
284 	 * Fake scoped addresses. Note that even link-local source or
285 	 * destination can appear, if the originating node just sends the
286 	 * packet to us (without address resolution for the destination).
287 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
288 	 * link identifiers, we can do this stuff after making a copy for
289 	 * returning an error.
290 	 */
291 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src))
292 		ip6->ip6_src.s6_addr16[1] = 0;
293 	if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst))
294 		ip6->ip6_dst.s6_addr16[1] = 0;
295 
296 #if NPF > 0
297 	if (pf_test(AF_INET6, PF_FWD, ifp, &m) != PF_PASS) {
298 		m_freem(m);
299 		goto senderr;
300 	}
301 	if (m == NULL)
302 		goto senderr;
303 	ip6 = mtod(m, struct ip6_hdr *);
304 	if ((m->m_pkthdr.pf.flags & (PF_TAG_REROUTE | PF_TAG_GENERATED)) ==
305 	    (PF_TAG_REROUTE | PF_TAG_GENERATED)) {
306 		/* already rerun the route lookup, go on */
307 		m->m_pkthdr.pf.flags &= ~(PF_TAG_GENERATED | PF_TAG_REROUTE);
308 	} else if (m->m_pkthdr.pf.flags & PF_TAG_REROUTE) {
309 		/* tag as generated to skip over pf_test on rerun */
310 		m->m_pkthdr.pf.flags |= PF_TAG_GENERATED;
311 		srcrt = 1;
312 		rtfree(rt);
313 		rt = NULL;
314 		if_put(ifp);
315 		ifp = NULL;
316 		goto reroute;
317 	}
318 #endif
319 	in6_proto_cksum_out(m, ifp);
320 
321 	/* Check the size after pf_test to give pf a chance to refragment. */
322 	if (m->m_pkthdr.len > ifp->if_mtu) {
323 		if (mcopy)
324 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
325 			    ifp->if_mtu);
326 		m_freem(m);
327 		goto out;
328 	}
329 
330 	error = ifp->if_output(ifp, m, sin6tosa(sin6), rt);
331 	if (error) {
332 		ip6stat_inc(ip6s_cantforward);
333 	} else {
334 		ip6stat_inc(ip6s_forward);
335 		if (type)
336 			ip6stat_inc(ip6s_redirectsent);
337 		else {
338 			if (mcopy)
339 				goto freecopy;
340 		}
341 	}
342 
343 #if NPF > 0 || defined(IPSEC)
344 senderr:
345 #endif
346 	if (mcopy == NULL)
347 		goto out;
348 
349 	switch (error) {
350 	case 0:
351 		if (type == ND_REDIRECT) {
352 			icmp6_redirect_output(mcopy, rt);
353 			goto out;
354 		}
355 		goto freecopy;
356 
357 	case EMSGSIZE:
358 		type = ICMP6_PACKET_TOO_BIG;
359 		code = 0;
360 		if (rt != NULL) {
361 			if (rt->rt_mtu) {
362 				destmtu = rt->rt_mtu;
363 			} else {
364 				struct ifnet *destifp;
365 
366 				destifp = if_get(rt->rt_ifidx);
367 				if (destifp != NULL)
368 					destmtu = destifp->if_mtu;
369 				if_put(destifp);
370 			}
371 		}
372 		ip6stat_inc(ip6s_cantfrag);
373 		if (destmtu == 0)
374 			goto freecopy;
375 		break;
376 
377 	case EACCES:
378 		/*
379 		 * pf(4) blocked the packet. There is no need to send an ICMP
380 		 * packet back since pf(4) takes care of it.
381 		 */
382 		goto freecopy;
383 
384 	case ENOBUFS:
385 		/* Tell source to slow down like source quench in IP? */
386 		goto freecopy;
387 
388 	case ENETUNREACH:	/* shouldn't happen, checked above */
389 	case EHOSTUNREACH:
390 	case ENETDOWN:
391 	case EHOSTDOWN:
392 	default:
393 		type = ICMP6_DST_UNREACH;
394 		code = ICMP6_DST_UNREACH_ADDR;
395 		break;
396 	}
397 	icmp6_error(mcopy, type, code, destmtu);
398 	goto out;
399 
400 freecopy:
401 	m_freem(mcopy);
402 out:
403 	rtfree(rt);
404 	if_put(ifp);
405 #ifdef IPSEC
406 	tdb_unref(tdb);
407 #endif /* IPSEC */
408 }
409