ip6_forward.c revision 1.5 1 1.5 itojun /* $NetBSD: ip6_forward.c,v 1.5 1999/12/13 15:17:22 itojun Exp $ */
2 1.3 thorpej
3 1.2 itojun /*
4 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.2 itojun * All rights reserved.
6 1.2 itojun *
7 1.2 itojun * Redistribution and use in source and binary forms, with or without
8 1.2 itojun * modification, are permitted provided that the following conditions
9 1.2 itojun * are met:
10 1.2 itojun * 1. Redistributions of source code must retain the above copyright
11 1.2 itojun * notice, this list of conditions and the following disclaimer.
12 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 itojun * notice, this list of conditions and the following disclaimer in the
14 1.2 itojun * documentation and/or other materials provided with the distribution.
15 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.2 itojun * may be used to endorse or promote products derived from this software
17 1.2 itojun * without specific prior written permission.
18 1.2 itojun *
19 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.2 itojun * SUCH DAMAGE.
30 1.2 itojun */
31 1.2 itojun
32 1.5 itojun #if (defined(__FreeBSD__) && __FreeBSD__ >= 3)
33 1.5 itojun #include "opt_ip6fw.h"
34 1.5 itojun #include "opt_inet.h"
35 1.5 itojun #endif
36 1.5 itojun
37 1.2 itojun #include <sys/param.h>
38 1.2 itojun #include <sys/systm.h>
39 1.2 itojun #include <sys/malloc.h>
40 1.2 itojun #include <sys/mbuf.h>
41 1.2 itojun #include <sys/domain.h>
42 1.2 itojun #include <sys/protosw.h>
43 1.2 itojun #include <sys/socket.h>
44 1.2 itojun #include <sys/errno.h>
45 1.2 itojun #include <sys/time.h>
46 1.2 itojun #include <sys/kernel.h>
47 1.2 itojun #include <sys/syslog.h>
48 1.2 itojun
49 1.2 itojun #include <net/if.h>
50 1.2 itojun #include <net/route.h>
51 1.2 itojun
52 1.2 itojun #include <netinet/in.h>
53 1.2 itojun #include <netinet/in_var.h>
54 1.2 itojun #include <netinet6/ip6.h>
55 1.2 itojun #include <netinet6/ip6_var.h>
56 1.2 itojun #include <netinet6/icmp6.h>
57 1.5 itojun #include <netinet6/nd6.h>
58 1.5 itojun
59 1.5 itojun #ifdef __OpenBSD__ /*KAME IPSEC*/
60 1.5 itojun #undef IPSEC
61 1.5 itojun #endif
62 1.5 itojun
63 1.5 itojun #ifdef IPSEC_IPV6FWD
64 1.5 itojun #include <netinet6/ipsec.h>
65 1.5 itojun #include <netkey/key.h>
66 1.5 itojun #include <netkey/key_debug.h>
67 1.5 itojun #endif /* IPSEC_IPV6FWD */
68 1.5 itojun
69 1.5 itojun #ifdef IPV6FIREWALL
70 1.5 itojun #include <netinet6/ip6_fw.h>
71 1.5 itojun #endif
72 1.5 itojun
73 1.5 itojun #include <net/net_osdep.h>
74 1.2 itojun
75 1.2 itojun struct route_in6 ip6_forward_rt;
76 1.2 itojun
77 1.2 itojun /*
78 1.2 itojun * Forward a packet. If some error occurs return the sender
79 1.2 itojun * an icmp packet. Note we can't always generate a meaningful
80 1.2 itojun * icmp message because icmp doesn't have a large enough repertoire
81 1.2 itojun * of codes and types.
82 1.2 itojun *
83 1.2 itojun * If not forwarding, just drop the packet. This could be confusing
84 1.2 itojun * if ipforwarding was zero but some routing protocol was advancing
85 1.2 itojun * us as a gateway to somewhere. However, we must let the routing
86 1.2 itojun * protocol deal with that.
87 1.2 itojun *
88 1.2 itojun */
89 1.2 itojun
90 1.2 itojun void
91 1.2 itojun ip6_forward(m, srcrt)
92 1.2 itojun struct mbuf *m;
93 1.2 itojun int srcrt;
94 1.2 itojun {
95 1.5 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
96 1.2 itojun register struct sockaddr_in6 *dst;
97 1.2 itojun register struct rtentry *rt;
98 1.2 itojun int error, type = 0, code = 0;
99 1.5 itojun struct mbuf *mcopy = NULL;
100 1.5 itojun #ifdef IPSEC_IPV6FWD
101 1.5 itojun struct secpolicy *sp = NULL;
102 1.5 itojun #endif
103 1.5 itojun #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
104 1.5 itojun long time_second = time.tv_sec;
105 1.5 itojun #endif
106 1.5 itojun
107 1.5 itojun #ifdef IPSEC_IPV6FWD
108 1.5 itojun /*
109 1.5 itojun * Check AH/ESP integrity.
110 1.5 itojun */
111 1.5 itojun /*
112 1.5 itojun * Don't increment ip6s_cantforward because this is the check
113 1.5 itojun * before forwarding packet actually.
114 1.5 itojun */
115 1.5 itojun if (ipsec6_in_reject(m, NULL)) {
116 1.5 itojun ipsec6stat.in_polvio++;
117 1.5 itojun m_freem(m);
118 1.5 itojun return;
119 1.5 itojun }
120 1.5 itojun #endif /*IPSEC_IPV6FWD*/
121 1.2 itojun
122 1.2 itojun if (m->m_flags & (M_BCAST|M_MCAST) ||
123 1.2 itojun in6_canforward(&ip6->ip6_src, &ip6->ip6_dst) == 0) {
124 1.2 itojun ip6stat.ip6s_cantforward++;
125 1.2 itojun ip6stat.ip6s_badscope++;
126 1.5 itojun /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
127 1.2 itojun if (ip6_log_time + ip6_log_interval < time_second) {
128 1.5 itojun char addr[INET6_ADDRSTRLEN];
129 1.2 itojun ip6_log_time = time_second;
130 1.5 itojun strncpy(addr, ip6_sprintf(&ip6->ip6_src), sizeof(addr));
131 1.2 itojun log(LOG_DEBUG,
132 1.2 itojun "cannot forward "
133 1.2 itojun "from %s to %s nxt %d received on %s\n",
134 1.5 itojun addr, ip6_sprintf(&ip6->ip6_dst),
135 1.2 itojun ip6->ip6_nxt,
136 1.5 itojun if_name(m->m_pkthdr.rcvif));
137 1.2 itojun }
138 1.2 itojun m_freem(m);
139 1.2 itojun return;
140 1.2 itojun }
141 1.2 itojun
142 1.2 itojun if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
143 1.5 itojun /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
144 1.2 itojun icmp6_error(m, ICMP6_TIME_EXCEEDED,
145 1.2 itojun ICMP6_TIME_EXCEED_TRANSIT, 0);
146 1.2 itojun return;
147 1.2 itojun }
148 1.2 itojun ip6->ip6_hlim -= IPV6_HLIMDEC;
149 1.2 itojun
150 1.5 itojun /*
151 1.5 itojun * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
152 1.5 itojun * size of IPv6 + ICMPv6 headers) bytes of the packet in case
153 1.5 itojun * we need to generate an ICMP6 message to the src.
154 1.5 itojun * Thanks to M_EXT, in most cases copy will not occur.
155 1.5 itojun *
156 1.5 itojun * It is important to save it before IPsec processing as IPsec
157 1.5 itojun * processing may modify the mbuf.
158 1.5 itojun */
159 1.5 itojun mcopy = m_copy(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN));
160 1.5 itojun
161 1.5 itojun #ifdef IPSEC_IPV6FWD
162 1.5 itojun /* get a security policy for this packet */
163 1.5 itojun sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
164 1.5 itojun if (sp == NULL) {
165 1.5 itojun ipsec6stat.out_inval++;
166 1.5 itojun ip6stat.ip6s_cantforward++;
167 1.5 itojun if (mcopy) {
168 1.5 itojun #if 0
169 1.5 itojun /* XXX: what icmp ? */
170 1.5 itojun #else
171 1.5 itojun m_freem(mcopy);
172 1.5 itojun #endif
173 1.5 itojun }
174 1.5 itojun m_freem(m);
175 1.5 itojun return;
176 1.5 itojun }
177 1.5 itojun
178 1.5 itojun error = 0;
179 1.5 itojun
180 1.5 itojun /* check policy */
181 1.5 itojun switch (sp->policy) {
182 1.5 itojun case IPSEC_POLICY_DISCARD:
183 1.5 itojun /*
184 1.5 itojun * This packet is just discarded.
185 1.5 itojun */
186 1.5 itojun ipsec6stat.out_polvio++;
187 1.5 itojun ip6stat.ip6s_cantforward++;
188 1.5 itojun key_freesp(sp);
189 1.5 itojun if (mcopy) {
190 1.5 itojun #if 0
191 1.5 itojun /* XXX: what icmp ? */
192 1.5 itojun #else
193 1.5 itojun m_freem(mcopy);
194 1.5 itojun #endif
195 1.5 itojun }
196 1.5 itojun m_freem(m);
197 1.5 itojun return;
198 1.5 itojun
199 1.5 itojun case IPSEC_POLICY_BYPASS:
200 1.5 itojun case IPSEC_POLICY_NONE:
201 1.5 itojun /* no need to do IPsec. */
202 1.5 itojun key_freesp(sp);
203 1.5 itojun goto skip_ipsec;
204 1.5 itojun
205 1.5 itojun case IPSEC_POLICY_IPSEC:
206 1.5 itojun if (sp->req == NULL) {
207 1.5 itojun /* XXX should be panic ? */
208 1.5 itojun printf("ip6_forward: No IPsec request specified.\n");
209 1.5 itojun ip6stat.ip6s_cantforward++;
210 1.5 itojun key_freesp(sp);
211 1.5 itojun if (mcopy) {
212 1.5 itojun #if 0
213 1.5 itojun /* XXX: what icmp ? */
214 1.5 itojun #else
215 1.5 itojun m_freem(mcopy);
216 1.5 itojun #endif
217 1.5 itojun }
218 1.5 itojun m_freem(m);
219 1.5 itojun return;
220 1.5 itojun }
221 1.5 itojun /* do IPsec */
222 1.5 itojun break;
223 1.5 itojun
224 1.5 itojun case IPSEC_POLICY_ENTRUST:
225 1.5 itojun default:
226 1.5 itojun /* should be panic ?? */
227 1.5 itojun printf("ip6_forward: Invalid policy found. %d\n", sp->policy);
228 1.5 itojun key_freesp(sp);
229 1.5 itojun goto skip_ipsec;
230 1.5 itojun }
231 1.5 itojun
232 1.5 itojun {
233 1.5 itojun struct ipsec_output_state state;
234 1.5 itojun
235 1.5 itojun /*
236 1.5 itojun * All the extension headers will become inaccessible
237 1.5 itojun * (since they can be encrypted).
238 1.5 itojun * Don't panic, we need no more updates to extension headers
239 1.5 itojun * on inner IPv6 packet (since they are now encapsulated).
240 1.5 itojun *
241 1.5 itojun * IPv6 [ESP|AH] IPv6 [extension headers] payload
242 1.5 itojun */
243 1.5 itojun bzero(&state, sizeof(state));
244 1.5 itojun state.m = m;
245 1.5 itojun state.ro = NULL; /* update at ipsec6_output_tunnel() */
246 1.5 itojun state.dst = NULL; /* update at ipsec6_output_tunnel() */
247 1.5 itojun
248 1.5 itojun error = ipsec6_output_tunnel(&state, sp, 0);
249 1.5 itojun
250 1.5 itojun m = state.m;
251 1.5 itojun #if 0 /* XXX allocate a route (ro, dst) again later */
252 1.5 itojun ro = (struct route_in6 *)state.ro;
253 1.5 itojun dst = (struct sockaddr_in6 *)state.dst;
254 1.5 itojun #endif
255 1.5 itojun key_freesp(sp);
256 1.5 itojun
257 1.5 itojun if (error) {
258 1.5 itojun /* mbuf is already reclaimed in ipsec6_output_tunnel. */
259 1.5 itojun switch (error) {
260 1.5 itojun case EHOSTUNREACH:
261 1.5 itojun case ENETUNREACH:
262 1.5 itojun case EMSGSIZE:
263 1.5 itojun case ENOBUFS:
264 1.5 itojun case ENOMEM:
265 1.5 itojun break;
266 1.5 itojun default:
267 1.5 itojun printf("ip6_output (ipsec): error code %d\n", error);
268 1.5 itojun /*fall through*/
269 1.5 itojun case ENOENT:
270 1.5 itojun /* don't show these error codes to the user */
271 1.5 itojun break;
272 1.5 itojun }
273 1.5 itojun ip6stat.ip6s_cantforward++;
274 1.5 itojun if (mcopy) {
275 1.5 itojun #if 0
276 1.5 itojun /* XXX: what icmp ? */
277 1.5 itojun #else
278 1.5 itojun m_freem(mcopy);
279 1.5 itojun #endif
280 1.5 itojun }
281 1.5 itojun m_freem(m);
282 1.5 itojun return;
283 1.5 itojun }
284 1.5 itojun }
285 1.5 itojun skip_ipsec:
286 1.5 itojun #endif /* IPSEC_IPV6FWD */
287 1.5 itojun
288 1.2 itojun dst = &ip6_forward_rt.ro_dst;
289 1.2 itojun if (!srcrt) {
290 1.2 itojun /*
291 1.2 itojun * ip6_forward_rt.ro_dst.sin6_addr is equal to ip6->ip6_dst
292 1.2 itojun */
293 1.2 itojun if (ip6_forward_rt.ro_rt == 0 ||
294 1.2 itojun (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) == 0) {
295 1.2 itojun if (ip6_forward_rt.ro_rt) {
296 1.2 itojun RTFREE(ip6_forward_rt.ro_rt);
297 1.2 itojun ip6_forward_rt.ro_rt = 0;
298 1.2 itojun }
299 1.2 itojun /* this probably fails but give it a try again */
300 1.5 itojun #ifdef __FreeBSD__
301 1.2 itojun rtalloc_ign((struct route *)&ip6_forward_rt,
302 1.2 itojun RTF_PRCLONING);
303 1.5 itojun #else
304 1.5 itojun rtalloc((struct route *)&ip6_forward_rt);
305 1.2 itojun #endif
306 1.2 itojun }
307 1.2 itojun
308 1.2 itojun if (ip6_forward_rt.ro_rt == 0) {
309 1.2 itojun ip6stat.ip6s_noroute++;
310 1.5 itojun /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
311 1.5 itojun if (mcopy) {
312 1.5 itojun icmp6_error(mcopy, ICMP6_DST_UNREACH,
313 1.5 itojun ICMP6_DST_UNREACH_NOROUTE, 0);
314 1.5 itojun }
315 1.5 itojun m_freem(m);
316 1.2 itojun return;
317 1.2 itojun }
318 1.2 itojun } else if ((rt = ip6_forward_rt.ro_rt) == 0 ||
319 1.2 itojun !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &dst->sin6_addr)) {
320 1.2 itojun if (ip6_forward_rt.ro_rt) {
321 1.2 itojun RTFREE(ip6_forward_rt.ro_rt);
322 1.2 itojun ip6_forward_rt.ro_rt = 0;
323 1.2 itojun }
324 1.2 itojun bzero(dst, sizeof(*dst));
325 1.2 itojun dst->sin6_len = sizeof(struct sockaddr_in6);
326 1.2 itojun dst->sin6_family = AF_INET6;
327 1.2 itojun dst->sin6_addr = ip6->ip6_dst;
328 1.2 itojun
329 1.5 itojun #ifdef __FreeBSD__
330 1.5 itojun rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING);
331 1.5 itojun #else
332 1.2 itojun rtalloc((struct route *)&ip6_forward_rt);
333 1.2 itojun #endif
334 1.2 itojun if (ip6_forward_rt.ro_rt == 0) {
335 1.2 itojun ip6stat.ip6s_noroute++;
336 1.5 itojun /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_noroute) */
337 1.5 itojun if (mcopy) {
338 1.5 itojun icmp6_error(mcopy, ICMP6_DST_UNREACH,
339 1.5 itojun ICMP6_DST_UNREACH_NOROUTE, 0);
340 1.5 itojun }
341 1.5 itojun m_freem(m);
342 1.2 itojun return;
343 1.2 itojun }
344 1.2 itojun }
345 1.2 itojun rt = ip6_forward_rt.ro_rt;
346 1.5 itojun if (m->m_pkthdr.len > rt->rt_ifp->if_mtu) {
347 1.5 itojun in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
348 1.5 itojun if (mcopy) {
349 1.5 itojun u_long mtu;
350 1.5 itojun
351 1.5 itojun mtu = rt->rt_ifp->if_mtu;
352 1.5 itojun icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0, mtu);
353 1.5 itojun }
354 1.5 itojun m_freem(m);
355 1.2 itojun return;
356 1.2 itojun }
357 1.2 itojun
358 1.2 itojun if (rt->rt_flags & RTF_GATEWAY)
359 1.2 itojun dst = (struct sockaddr_in6 *)rt->rt_gateway;
360 1.2 itojun
361 1.2 itojun /*
362 1.2 itojun * If we are to forward the packet using the same interface
363 1.2 itojun * as one we got the packet from, perhaps we should send a redirect
364 1.2 itojun * to sender to shortcut a hop.
365 1.2 itojun * Only send redirect if source is sending directly to us,
366 1.2 itojun * and if packet was not source routed (or has any options).
367 1.2 itojun * Also, don't send redirect if forwarding using a route
368 1.2 itojun * modified by a redirect.
369 1.2 itojun */
370 1.2 itojun if (rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
371 1.2 itojun (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
372 1.2 itojun type = ND_REDIRECT;
373 1.2 itojun
374 1.5 itojun #ifdef IPV6FIREWALL
375 1.5 itojun /*
376 1.5 itojun * Check with the firewall...
377 1.5 itojun */
378 1.5 itojun if (ip6_fw_chk_ptr) {
379 1.5 itojun u_short port = 0;
380 1.5 itojun /* If ipfw says divert, we have to just drop packet */
381 1.5 itojun if ((*ip6_fw_chk_ptr)(&ip6, rt->rt_ifp, &port, &m)) {
382 1.5 itojun m_freem(m);
383 1.5 itojun goto freecopy;
384 1.5 itojun }
385 1.5 itojun if (!m)
386 1.5 itojun goto freecopy;
387 1.5 itojun }
388 1.5 itojun #endif
389 1.5 itojun
390 1.5 itojun #ifdef OLDIP6OUTPUT
391 1.2 itojun error = (*rt->rt_ifp->if_output)(rt->rt_ifp, m,
392 1.2 itojun (struct sockaddr *)dst,
393 1.2 itojun ip6_forward_rt.ro_rt);
394 1.5 itojun #else
395 1.5 itojun error = nd6_output(rt->rt_ifp, m, dst, rt);
396 1.5 itojun #endif
397 1.5 itojun if (error) {
398 1.5 itojun in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
399 1.2 itojun ip6stat.ip6s_cantforward++;
400 1.5 itojun } else {
401 1.2 itojun ip6stat.ip6s_forward++;
402 1.5 itojun in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
403 1.2 itojun if (type)
404 1.2 itojun ip6stat.ip6s_redirectsent++;
405 1.2 itojun else {
406 1.2 itojun if (mcopy)
407 1.2 itojun goto freecopy;
408 1.2 itojun }
409 1.2 itojun }
410 1.2 itojun if (mcopy == NULL)
411 1.2 itojun return;
412 1.2 itojun
413 1.2 itojun switch (error) {
414 1.2 itojun case 0:
415 1.2 itojun #if 1
416 1.2 itojun if (type == ND_REDIRECT) {
417 1.2 itojun icmp6_redirect_output(mcopy, rt);
418 1.2 itojun return;
419 1.2 itojun }
420 1.2 itojun #endif
421 1.2 itojun goto freecopy;
422 1.2 itojun
423 1.2 itojun case EMSGSIZE:
424 1.2 itojun /* xxx MTU is constant in PPP? */
425 1.2 itojun goto freecopy;
426 1.2 itojun
427 1.2 itojun case ENOBUFS:
428 1.2 itojun /* Tell source to slow down like source quench in IP? */
429 1.2 itojun goto freecopy;
430 1.2 itojun
431 1.2 itojun case ENETUNREACH: /* shouldn't happen, checked above */
432 1.2 itojun case EHOSTUNREACH:
433 1.2 itojun case ENETDOWN:
434 1.2 itojun case EHOSTDOWN:
435 1.2 itojun default:
436 1.2 itojun type = ICMP6_DST_UNREACH;
437 1.2 itojun code = ICMP6_DST_UNREACH_ADDR;
438 1.2 itojun break;
439 1.2 itojun }
440 1.2 itojun icmp6_error(mcopy, type, code, 0);
441 1.2 itojun return;
442 1.2 itojun
443 1.2 itojun freecopy:
444 1.2 itojun m_freem(mcopy);
445 1.5 itojun return;
446 1.2 itojun }
447