xref: /openbsd-src/sys/netinet6/ip6_forward.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: ip6_forward.c,v 1.26 2003/06/24 07:47:54 itojun 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/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/domain.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46 
47 #include <net/if.h>
48 #include <net/route.h>
49 
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet/icmp6.h>
56 #include <netinet6/nd6.h>
57 
58 #if NPF > 0
59 #include <net/pfvar.h>
60 #endif
61 
62 #ifdef IPSEC
63 #include <netinet/ip_ah.h>
64 #include <netinet/ip_esp.h>
65 #include <netinet/udp.h>
66 #include <netinet/tcp.h>
67 #include <net/pfkeyv2.h>
68 #endif
69 
70 struct	route_in6 ip6_forward_rt;
71 
72 /*
73  * Forward a packet.  If some error occurs return the sender
74  * an icmp packet.  Note we can't always generate a meaningful
75  * icmp message because icmp doesn't have a large enough repertoire
76  * of codes and types.
77  *
78  * If not forwarding, just drop the packet.  This could be confusing
79  * if ipforwarding was zero but some routing protocol was advancing
80  * us as a gateway to somewhere.  However, we must let the routing
81  * protocol deal with that.
82  *
83  */
84 
85 void
86 ip6_forward(m, srcrt)
87 	struct mbuf *m;
88 	int srcrt;
89 {
90 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
91 	struct sockaddr_in6 *dst;
92 	struct rtentry *rt;
93 	int error, type = 0, code = 0;
94 	struct mbuf *mcopy = NULL;
95 	struct ifnet *origifp;	/* maybe unnecessary */
96 #ifdef IPSEC
97 	u_int8_t sproto = 0;
98 	struct m_tag *mtag;
99 	union sockaddr_union sdst;
100 	struct tdb_ident *tdbi;
101 	u_int32_t sspi;
102 	struct tdb *tdb;
103 	int s;
104 #endif /* IPSEC */
105 
106 	/*
107 	 * Do not forward packets to multicast destination (should be handled
108 	 * by ip6_mforward().
109 	 * Do not forward packets with unspecified source.  It was discussed
110 	 * in July 2000, on ipngwg mailing list.
111 	 */
112 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
113 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
114 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
115 		ip6stat.ip6s_cantforward++;
116 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
117 		if (ip6_log_time + ip6_log_interval < time.tv_sec) {
118 			ip6_log_time = time.tv_sec;
119 			log(LOG_DEBUG,
120 			    "cannot forward "
121 			    "from %s to %s nxt %d received on %s\n",
122 			    ip6_sprintf(&ip6->ip6_src),
123 			    ip6_sprintf(&ip6->ip6_dst),
124 			    ip6->ip6_nxt,
125 			    m->m_pkthdr.rcvif->if_xname);
126 		}
127 		m_freem(m);
128 		return;
129 	}
130 
131 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
132 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
133 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
134 				ICMP6_TIME_EXCEED_TRANSIT, 0);
135 		return;
136 	}
137 	ip6->ip6_hlim -= IPV6_HLIMDEC;
138 
139 #ifdef IPSEC
140 	s = splnet();
141 
142 	/*
143 	 * Check if there was an outgoing SA bound to the flow
144 	 * from a transport protocol.
145 	 */
146 
147 	/* Do we have any pending SAs to apply ? */
148 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
149 	if (mtag != NULL) {
150 #ifdef DIAGNOSTIC
151 		if (mtag->m_tag_len != sizeof (struct tdb_ident))
152 			panic("ip6_forward: tag of length %d (should be %d",
153 			    mtag->m_tag_len, sizeof (struct tdb_ident));
154 #endif
155 		tdbi = (struct tdb_ident *)(mtag + 1);
156 		tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
157 		if (tdb == NULL)
158 			error = -EINVAL;
159 		m_tag_delete(m, mtag);
160 	} else
161 		tdb = ipsp_spd_lookup(m, AF_INET6, sizeof(struct ip6_hdr),
162 		    &error, IPSP_DIRECTION_OUT, NULL, NULL);
163 
164 	if (tdb == NULL) {
165 	        splx(s);
166 
167 		if (error == 0) {
168 		        /*
169 			 * No IPsec processing required, we'll just send the
170 			 * packet out.
171 			 */
172 		        sproto = 0;
173 
174 			/* Fall through to routing/multicast handling */
175 		} else {
176 		        /*
177 			 * -EINVAL is used to indicate that the packet should
178 			 * be silently dropped, typically because we've asked
179 			 * key management for an SA.
180 			 */
181 		        if (error == -EINVAL) /* Should silently drop packet */
182 				error = 0;
183 
184 			goto freecopy;
185 		}
186 	} else {
187 		/* Loop detection */
188 		for (mtag = m_tag_first(m); mtag != NULL;
189 		    mtag = m_tag_next(m, mtag)) {
190 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
191 			    mtag->m_tag_id !=
192 			    PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
193 				continue;
194 			tdbi = (struct tdb_ident *)(mtag + 1);
195 			if (tdbi->spi == tdb->tdb_spi &&
196 			    tdbi->proto == tdb->tdb_sproto &&
197 			    !bcmp(&tdbi->dst, &tdb->tdb_dst,
198 			    sizeof(union sockaddr_union))) {
199 				splx(s);
200 				sproto = 0; /* mark as no-IPsec-needed */
201 				goto done_spd;
202 			}
203 		}
204 
205 	        /* We need to do IPsec */
206 	        bcopy(&tdb->tdb_dst, &sdst, sizeof(sdst));
207 		sspi = tdb->tdb_spi;
208 		sproto = tdb->tdb_sproto;
209 	        splx(s);
210 	}
211 
212 	/* Fall through to the routing/multicast handling code */
213  done_spd:
214 #endif /* IPSEC */
215 
216 	/*
217 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
218 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
219 	 * we need to generate an ICMP6 message to the src.
220 	 * Thanks to M_EXT, in most cases copy will not occur.
221 	 *
222 	 * It is important to save it before IPsec processing as IPsec
223 	 * processing may modify the mbuf.
224 	 */
225 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
226 
227 	dst = &ip6_forward_rt.ro_dst;
228 	if (!srcrt) {
229 		/*
230 		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
231 		 */
232 		if (ip6_forward_rt.ro_rt == 0 ||
233 		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
234 			if (ip6_forward_rt.ro_rt) {
235 				RTFREE(ip6_forward_rt.ro_rt);
236 				ip6_forward_rt.ro_rt = 0;
237 			}
238 			/* this probably fails but give it a try again */
239 			rtalloc((struct route *)&ip6_forward_rt);
240 		}
241 
242 		if (ip6_forward_rt.ro_rt == 0) {
243 			ip6stat.ip6s_noroute++;
244 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
245 			if (mcopy) {
246 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
247 					    ICMP6_DST_UNREACH_NOROUTE, 0);
248 			}
249 			m_freem(m);
250 			return;
251 		}
252 	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
253 		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
254 		if (ip6_forward_rt.ro_rt) {
255 			RTFREE(ip6_forward_rt.ro_rt);
256 			ip6_forward_rt.ro_rt = 0;
257 		}
258 		bzero(dst, sizeof(*dst));
259 		dst->sin6_len = sizeof(struct sockaddr_in6);
260 		dst->sin6_family = AF_INET6;
261 		dst->sin6_addr = ip6->ip6_dst;
262 
263 		rtalloc((struct route *)&ip6_forward_rt);
264 
265 		if (ip6_forward_rt.ro_rt == 0) {
266 			ip6stat.ip6s_noroute++;
267 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
268 			if (mcopy) {
269 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
270 					    ICMP6_DST_UNREACH_NOROUTE, 0);
271 			}
272 			m_freem(m);
273 			return;
274 		}
275 	}
276 	rt = ip6_forward_rt.ro_rt;
277 
278 	/*
279 	 * Scope check: if a packet can't be delivered to its destination
280 	 * for the reason that the destination is beyond the scope of the
281 	 * source address, discard the packet and return an icmp6 destination
282 	 * unreachable error with Code 2 (beyond scope of source address).
283 	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
284 	 */
285 	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
286 	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
287 		ip6stat.ip6s_cantforward++;
288 		ip6stat.ip6s_badscope++;
289 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
290 
291 		if (ip6_log_time + ip6_log_interval < time.tv_sec) {
292 			ip6_log_time = time.tv_sec;
293 			log(LOG_DEBUG,
294 			    "cannot forward "
295 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
296 			    ip6_sprintf(&ip6->ip6_src),
297 			    ip6_sprintf(&ip6->ip6_dst),
298 			    ip6->ip6_nxt,
299 			    m->m_pkthdr.rcvif->if_xname, rt->rt_ifp->if_xname);
300 		}
301 		if (mcopy)
302 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
303 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
304 		m_freem(m);
305 		return;
306 	}
307 
308 #ifdef IPSEC
309 	/*
310 	 * Check if the packet needs encapsulation.
311 	 * ipsp_process_packet will never come back to here.
312 	 * XXX ipsp_process_packet() calls ip6_output(), and there'll be no
313 	 * PMTU notification.  is it okay?
314 	 */
315 	if (sproto != 0) {
316 		s = splnet();
317 
318 		tdb = gettdb(sspi, &sdst, sproto);
319 		if (tdb == NULL) {
320 			splx(s);
321 			error = EHOSTUNREACH;
322 			m_freem(m);
323 			goto senderr;	/*XXX*/
324 		}
325 
326 		m->m_flags &= ~(M_BCAST | M_MCAST);	/* just in case */
327 
328 		/* Callee frees mbuf */
329 		error = ipsp_process_packet(m, tdb, AF_INET6, 0);
330 		splx(s);
331 		m_freem(mcopy);
332 		return;  /* Nothing more to be done */
333 	}
334 #endif /* IPSEC */
335 
336 	if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
337 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
338 		if (mcopy) {
339 			u_long mtu;
340 
341 			mtu = IN6_LINKMTU(rt->rt_ifp);
342 
343 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
344 		}
345 		m_freem(m);
346 		return;
347 	}
348 
349 	if (rt->rt_flags & RTF_GATEWAY)
350 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
351 
352 	/*
353 	 * If we are to forward the packet using the same interface
354 	 * as one we got the packet from, perhaps we should send a redirect
355 	 * to sender to shortcut a hop.
356 	 * Only send redirect if source is sending directly to us,
357 	 * and if packet was not source routed (or has any options).
358 	 * Also, don't send redirect if forwarding using a route
359 	 * modified by a redirect.
360 	 */
361 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
362 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0) {
363 		if ((rt->rt_ifp->if_flags & IFF_POINTOPOINT) &&
364 		    nd6_is_addr_neighbor((struct sockaddr_in6 *)&ip6_forward_rt.ro_dst, rt->rt_ifp)) {
365 			/*
366 			 * If the incoming interface is equal to the outgoing
367 			 * one, the link attached to the interface is
368 			 * point-to-point, and the IPv6 destination is
369 			 * regarded as on-link on the link, then it will be
370 			 * highly probable that the destination address does
371 			 * not exist on the link and that the packet is going
372 			 * to loop.  Thus, we immediately drop the packet and
373 			 * send an ICMPv6 error message.
374 			 * For other routing loops, we dare to let the packet
375 			 * go to the loop, so that a remote diagnosing host
376 			 * can detect the loop by traceroute.
377 			 * type/code is based on suggestion by Rich Draves.
378 			 * not sure if it is the best pick.
379 			 */
380 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
381 				    ICMP6_DST_UNREACH_ADDR, 0);
382 			m_freem(m);
383 			return;
384 		}
385 		type = ND_REDIRECT;
386 	}
387 
388 	/*
389 	 * Fake scoped addresses. Note that even link-local source or
390 	 * destinaion can appear, if the originating node just sends the
391 	 * packet to us (without address resolution for the destination).
392 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
393 	 * link identifiers, we can do this stuff after making a copy for
394 	 * returning an error.
395 	 */
396 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
397 		/*
398 		 * See corresponding comments in ip6_output.
399 		 * XXX: but is it possible that ip6_forward() sends a packet
400 		 *      to a loopback interface? I don't think so, and thus
401 		 *      I bark here. (jinmei@kame.net)
402 		 * XXX: it is common to route invalid packets to loopback.
403 		 *	also, the codepath will be visited on use of ::1 in
404 		 *	rthdr. (itojun)
405 		 */
406 #if 1
407 		if (0)
408 #else
409 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
410 #endif
411 		{
412 			printf("ip6_forward: outgoing interface is loopback. "
413 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
414 			       ip6_sprintf(&ip6->ip6_src),
415 			       ip6_sprintf(&ip6->ip6_dst),
416 			       ip6->ip6_nxt, m->m_pkthdr.rcvif->if_xname,
417 			       rt->rt_ifp->if_xname);
418 		}
419 
420 		/* we can just use rcvif in forwarding. */
421 		origifp = m->m_pkthdr.rcvif;
422 	}
423 	else
424 		origifp = rt->rt_ifp;
425 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
426 		ip6->ip6_src.s6_addr16[1] = 0;
427 	if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
428 		ip6->ip6_dst.s6_addr16[1] = 0;
429 
430 #if NPF > 0
431 	if (pf_test6(PF_OUT, rt->rt_ifp, &m) != PF_PASS) {
432 		m_freem(m);
433 		goto senderr;
434 	}
435 	if (m == NULL)
436 		goto senderr;
437 
438 	ip6 = mtod(m, struct ip6_hdr *);
439 #endif
440 
441 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
442 	if (error) {
443 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
444 		ip6stat.ip6s_cantforward++;
445 	} else {
446 		ip6stat.ip6s_forward++;
447 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
448 		if (type)
449 			ip6stat.ip6s_redirectsent++;
450 		else {
451 			if (mcopy)
452 				goto freecopy;
453 		}
454 	}
455 
456 #if NPF > 0
457 senderr:
458 #endif
459 	if (mcopy == NULL)
460 		return;
461 	switch (error) {
462 	case 0:
463 		if (type == ND_REDIRECT) {
464 			icmp6_redirect_output(mcopy, rt);
465 			return;
466 		}
467 		goto freecopy;
468 
469 	case EMSGSIZE:
470 		/* xxx MTU is constant in PPP? */
471 		goto freecopy;
472 
473 	case ENOBUFS:
474 		/* Tell source to slow down like source quench in IP? */
475 		goto freecopy;
476 
477 	case ENETUNREACH:	/* shouldn't happen, checked above */
478 	case EHOSTUNREACH:
479 	case ENETDOWN:
480 	case EHOSTDOWN:
481 	default:
482 		type = ICMP6_DST_UNREACH;
483 		code = ICMP6_DST_UNREACH_ADDR;
484 		break;
485 	}
486 	icmp6_error(mcopy, type, code, 0);
487 	return;
488 
489  freecopy:
490 	m_freem(mcopy);
491 	return;
492 }
493