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