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