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