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