xref: /netbsd-src/sys/netinet6/ip6_forward.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: ip6_forward.c,v 1.14 2000/07/06 12:51:41 itojun Exp $	*/
2 /*	$KAME: ip6_forward.c,v 1.39 2000/07/03 13:23:28 itojun 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 <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/domain.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
41 #include <sys/time.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 
45 #include <net/if.h>
46 #include <net/route.h>
47 
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip6.h>
52 #include <netinet6/ip6_var.h>
53 #include <netinet/icmp6.h>
54 #include <netinet6/nd6.h>
55 
56 #ifdef IPSEC_IPV6FWD
57 #include <netinet6/ipsec.h>
58 #include <netkey/key.h>
59 #endif /* IPSEC_IPV6FWD */
60 
61 #ifdef IPV6FIREWALL
62 #include <netinet6/ip6_fw.h>
63 #endif
64 
65 #include <net/net_osdep.h>
66 
67 struct	route_in6 ip6_forward_rt;
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(m, srcrt)
84 	struct mbuf *m;
85 	int srcrt;
86 {
87 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
88 	register struct sockaddr_in6 *dst;
89 	register struct rtentry *rt;
90 	int error, type = 0, code = 0;
91 	struct mbuf *mcopy = NULL;
92 	struct ifnet *origifp;	/* maybe unnecessary */
93 #ifdef IPSEC_IPV6FWD
94 	struct secpolicy *sp = NULL;
95 #endif
96 	long time_second = time.tv_sec;
97 
98 #ifdef IPSEC_IPV6FWD
99 	/*
100 	 * Check AH/ESP integrity.
101 	 */
102 	/*
103 	 * Don't increment ip6s_cantforward because this is the check
104 	 * before forwarding packet actually.
105 	 */
106 	if (ipsec6_in_reject(m, NULL)) {
107 		ipsec6stat.in_polvio++;
108 		m_freem(m);
109 		return;
110 	}
111 #endif /*IPSEC_IPV6FWD*/
112 
113 	if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
114 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
115 		ip6stat.ip6s_cantforward++;
116 		/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
117 		if (ip6_log_time + ip6_log_interval < time_second) {
118 			ip6_log_time = time_second;
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 			    if_name(m->m_pkthdr.rcvif));
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 	/*
140 	 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
141 	 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
142 	 * we need to generate an ICMP6 message to the src.
143 	 * Thanks to M_EXT, in most cases copy will not occur.
144 	 *
145 	 * It is important to save it before IPsec processing as IPsec
146 	 * processing may modify the mbuf.
147 	 */
148 	mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
149 
150 #ifdef IPSEC_IPV6FWD
151 	/* get a security policy for this packet */
152 	sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
153 	if (sp == NULL) {
154 		ipsec6stat.out_inval++;
155 		ip6stat.ip6s_cantforward++;
156 		if (mcopy) {
157 #if 0
158 			/* XXX: what icmp ? */
159 #else
160 			m_freem(mcopy);
161 #endif
162 		}
163 		m_freem(m);
164 		return;
165 	}
166 
167 	error = 0;
168 
169 	/* check policy */
170 	switch (sp->policy) {
171 	case IPSEC_POLICY_DISCARD:
172 		/*
173 		 * This packet is just discarded.
174 		 */
175 		ipsec6stat.out_polvio++;
176 		ip6stat.ip6s_cantforward++;
177 		key_freesp(sp);
178 		if (mcopy) {
179 #if 0
180 			/* XXX: what icmp ? */
181 #else
182 			m_freem(mcopy);
183 #endif
184 		}
185 		m_freem(m);
186 		return;
187 
188 	case IPSEC_POLICY_BYPASS:
189 	case IPSEC_POLICY_NONE:
190 		/* no need to do IPsec. */
191 		key_freesp(sp);
192 		goto skip_ipsec;
193 
194 	case IPSEC_POLICY_IPSEC:
195 		if (sp->req == NULL) {
196 			/* XXX should be panic ? */
197 			printf("ip6_forward: No IPsec request specified.\n");
198 			ip6stat.ip6s_cantforward++;
199 			key_freesp(sp);
200 			if (mcopy) {
201 #if 0
202 				/* XXX: what icmp ? */
203 #else
204 				m_freem(mcopy);
205 #endif
206 			}
207 			m_freem(m);
208 			return;
209 		}
210 		/* do IPsec */
211 		break;
212 
213 	case IPSEC_POLICY_ENTRUST:
214 	default:
215 		/* should be panic ?? */
216 		printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
217 		key_freesp(sp);
218 		goto skip_ipsec;
219 	}
220 
221     {
222 	struct ipsec_output_state state;
223 
224 	/*
225 	 * All the extension headers will become inaccessible
226 	 * (since they can be encrypted).
227 	 * Don't panic, we need no more updates to extension headers
228 	 * on inner IPv6 packet (since they are now encapsulated).
229 	 *
230 	 * IPv6 [ESP|AH] IPv6 [extension headers] payload
231 	 */
232 	bzero(&state, sizeof(state));
233 	state.m = m;
234 	state.ro = NULL;	/* update at ipsec6_output_tunnel() */
235 	state.dst = NULL;	/* update at ipsec6_output_tunnel() */
236 
237 	error = ipsec6_output_tunnel(&state, sp, 0);
238 
239 	m = state.m;
240 #if 0	/* XXX allocate a route (ro, dst) again later */
241 	ro = (struct route_in6 *)state.ro;
242 	dst = (struct sockaddr_in6 *)state.dst;
243 #endif
244 	key_freesp(sp);
245 
246 	if (error) {
247 		/* mbuf is already reclaimed in ipsec6_output_tunnel. */
248 		switch (error) {
249 		case EHOSTUNREACH:
250 		case ENETUNREACH:
251 		case EMSGSIZE:
252 		case ENOBUFS:
253 		case ENOMEM:
254 			break;
255 		default:
256 			printf("ip6_output (ipsec): error code %d\n", error);
257 			/*fall through*/
258 		case ENOENT:
259 			/* don't show these error codes to the user */
260 			break;
261 		}
262 		ip6stat.ip6s_cantforward++;
263 		if (mcopy) {
264 #if 0
265 			/* XXX: what icmp ? */
266 #else
267 			m_freem(mcopy);
268 #endif
269 		}
270 		m_freem(m);
271 		return;
272 	}
273     }
274     skip_ipsec:
275 #endif /* IPSEC_IPV6FWD */
276 
277 	dst = &ip6_forward_rt.ro_dst;
278 	if (!srcrt) {
279 		/*
280 		 * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
281 		 */
282 		if (ip6_forward_rt.ro_rt == 0 ||
283 		    (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
284 			if (ip6_forward_rt.ro_rt) {
285 				RTFREE(ip6_forward_rt.ro_rt);
286 				ip6_forward_rt.ro_rt = 0;
287 			}
288 			/* this probably fails but give it a try again */
289 			rtalloc((struct route *)&ip6_forward_rt);
290 		}
291 
292 		if (ip6_forward_rt.ro_rt == 0) {
293 			ip6stat.ip6s_noroute++;
294 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
295 			if (mcopy) {
296 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
297 					    ICMP6_DST_UNREACH_NOROUTE, 0);
298 			}
299 			m_freem(m);
300 			return;
301 		}
302 	} else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
303 		 !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
304 		if (ip6_forward_rt.ro_rt) {
305 			RTFREE(ip6_forward_rt.ro_rt);
306 			ip6_forward_rt.ro_rt = 0;
307 		}
308 		bzero(dst, sizeof(*dst));
309 		dst->sin6_len = sizeof(struct sockaddr_in6);
310 		dst->sin6_family = AF_INET6;
311 		dst->sin6_addr = ip6->ip6_dst;
312 
313 		rtalloc((struct route *)&ip6_forward_rt);
314 		if (ip6_forward_rt.ro_rt == 0) {
315 			ip6stat.ip6s_noroute++;
316 			/* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
317 			if (mcopy) {
318 				icmp6_error(mcopy, ICMP6_DST_UNREACH,
319 					    ICMP6_DST_UNREACH_NOROUTE, 0);
320 			}
321 			m_freem(m);
322 			return;
323 		}
324 	}
325 	rt = ip6_forward_rt.ro_rt;
326 
327 	/*
328 	 * Scope check: if a packet can't be delivered to its destination
329 	 * for the reason that the destination is beyond the scope of the
330 	 * source address, discard the packet and return an icmp6 destination
331 	 * unreachable error with Code 2 (beyond scope of source address).
332 	 * [draft-ietf-ipngwg-icmp-v3-00.txt, Section 3.1]
333 	 */
334 	if (in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_src) !=
335 	    in6_addr2scopeid(rt->rt_ifp, &ip6->ip6_src)) {
336 		ip6stat.ip6s_cantforward++;
337 		ip6stat.ip6s_badscope++;
338 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
339 
340 		if (ip6_log_time + ip6_log_interval < time_second) {
341 			ip6_log_time = time_second;
342 			log(LOG_DEBUG,
343 			    "cannot forward "
344 			    "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
345 			    ip6_sprintf(&ip6->ip6_src),
346 			    ip6_sprintf(&ip6->ip6_dst),
347 			    ip6->ip6_nxt,
348 			    if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
349 		}
350 		if (mcopy)
351 			icmp6_error(mcopy, ICMP6_DST_UNREACH,
352 				    ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
353 		m_freem(m);
354 		return;
355 	}
356 
357 	if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
358 		in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
359 		if (mcopy) {
360 			u_long mtu;
361 #ifdef IPSEC_IPV6FWD
362 			struct secpolicy *sp;
363 			int ipsecerror;
364 			size_t ipsechdrsiz;
365 #endif
366 
367 			mtu = rt->rt_ifp->if_mtu;
368 #ifdef IPSEC_IPV6FWD
369 			/*
370 			 * When we do IPsec tunnel ingress, we need to play
371 			 * with if_mtu value (decrement IPsec header size
372 			 * from mtu value).  The code is much simpler than v4
373 			 * case, as we have the outgoing interface for
374 			 * encapsulated packet as "rt->rt_ifp".
375 			 */
376 			sp = ipsec6_getpolicybyaddr(mcopy, IPSEC_DIR_OUTBOUND,
377 				IP_FORWARDING, &ipsecerror);
378 			if (sp) {
379 				ipsechdrsiz = ipsec6_hdrsiz(mcopy,
380 					IPSEC_DIR_OUTBOUND, NULL);
381 				if (ipsechdrsiz < mtu)
382 					mtu -= ipsechdrsiz;
383 			}
384 
385 			/*
386 			 * if mtu becomes less than minimum MTU,
387 			 * tell minimum MTU (and I'll need to fragment it).
388 			 */
389 			if (mtu < IPV6_MMTU)
390 				mtu = IPV6_MMTU;
391 #endif
392 			icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
393 		}
394 		m_freem(m);
395 		return;
396  	}
397 
398 	if (rt->rt_flags & RTF_GATEWAY)
399 		dst = (struct sockaddr_in6 *)rt->rt_gateway;
400 
401 	/*
402 	 * If we are to forward the packet using the same interface
403 	 * as one we got the packet from, perhaps we should send a redirect
404 	 * to sender to shortcut a hop.
405 	 * Only send redirect if source is sending directly to us,
406 	 * and if packet was not source routed (or has any options).
407 	 * Also, don't send redirect if forwarding using a route
408 	 * modified by a redirect.
409 	 */
410 	if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
411 	    (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
412 		type = ND_REDIRECT;
413 
414 #ifdef IPV6FIREWALL
415 	/*
416 	 * Check with the firewall...
417 	 */
418 	if (ip6_fw_chk_ptr) {
419 		u_short port = 0;
420 		/* If ipfw says divert, we have to just drop packet */
421 		if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
422 			m_freem(m);
423 			goto freecopy;
424 		}
425 		if (!m)
426 			goto freecopy;
427 	}
428 #endif
429 
430 	/*
431 	 * Fake scoped addresses. Note that even link-local source or
432 	 * destinaion can appear, if the originating node just sends the
433 	 * packet to us (without address resolution for the destination).
434 	 * Since both icmp6_error and icmp6_redirect_output fill the embedded
435 	 * link identifiers, we can do this stuff after make a copy for
436 	 * returning error.
437 	 */
438 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
439 		/*
440 		 * See corresponding comments in ip6_output.
441 		 * XXX: but is it possible that ip6_forward() sends a packet
442 		 *      to a loopback interface? I don't think so, and thus
443 		 *      I bark here. (jinmei@kame.net)
444 		 * XXX: it is common to route invalid packets to loopback.
445 		 *	also, the codepath will be visited on use of ::1 in
446 		 *	rthdr. (itojun)
447 		 */
448 #if 1
449 		if (0)
450 #else
451 		if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
452 #endif
453 		{
454 			printf("ip6_forward: outgoing interface is loopback. "
455 			       "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
456 			       ip6_sprintf(&ip6->ip6_src),
457 			       ip6_sprintf(&ip6->ip6_dst),
458 			       ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
459 			       if_name(rt->rt_ifp));
460 		}
461 
462 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
463 			origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
464 		else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
465 			origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
466 		else
467 			origifp = rt->rt_ifp;
468 	}
469 	else
470 		origifp = rt->rt_ifp;
471 #ifndef FAKE_LOOPBACK_IF
472 	if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
473 #else
474 	if (1)
475 #endif
476 	{
477 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
478 			ip6->ip6_src.s6_addr16[1] = 0;
479 		if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
480 			ip6->ip6_dst.s6_addr16[1] = 0;
481 	}
482 
483 #ifdef OLDIP6OUTPUT
484 	error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
485 					 (struct sockaddr *)dst,
486 					 ip6_forward_rt.ro_rt);
487 #else
488 	error = nd6_output(rt->rt_ifp, origifp, m, dst, rt);
489 #endif
490 	if (error) {
491 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
492 		ip6stat.ip6s_cantforward++;
493 	} else {
494 		ip6stat.ip6s_forward++;
495 		in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
496 		if (type)
497 			ip6stat.ip6s_redirectsent++;
498 		else {
499 			if (mcopy)
500 				goto freecopy;
501 		}
502 	}
503 	if (mcopy == NULL)
504 		return;
505 
506 	switch (error) {
507 	case 0:
508 #if 1
509 		if (type == ND_REDIRECT) {
510 			icmp6_redirect_output(mcopy, rt);
511 			return;
512 		}
513 #endif
514 		goto freecopy;
515 
516 	case EMSGSIZE:
517 		/* xxx MTU is constant in PPP? */
518 		goto freecopy;
519 
520 	case ENOBUFS:
521 		/* Tell source to slow down like source quench in IP? */
522 		goto freecopy;
523 
524 	case ENETUNREACH:	/* shouldn't happen, checked above */
525 	case EHOSTUNREACH:
526 	case ENETDOWN:
527 	case EHOSTDOWN:
528 	default:
529 		type = ICMP6_DST_UNREACH;
530 		code = ICMP6_DST_UNREACH_ADDR;
531 		break;
532 	}
533 	icmp6_error(mcopy, type, code, 0);
534 	return;
535 
536  freecopy:
537 	m_freem(mcopy);
538 	return;
539 }
540