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