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