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