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