1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 * $KAME: ip6_forward.c,v 1.69 2001/05/17 03:48:30 itojun Exp $
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40 #include "opt_ipstealth.h"
41 #include "opt_sctp.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/pfil.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip_var.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
69 #include <netinet6/scope6_var.h>
70 #include <netinet/icmp6.h>
71 #include <netinet6/nd6.h>
72
73 #include <netinet/in_pcb.h>
74
75 #include <netipsec/ipsec_support.h>
76
77 /*
78 * Forward a packet. If some error occurs return the sender
79 * an icmp packet. Note we can't always generate a meaningful
80 * icmp message because icmp doesn't have a large enough repertoire
81 * of codes and types.
82 *
83 * If not forwarding, just drop the packet. This could be confusing
84 * if ipforwarding was zero but some routing protocol was advancing
85 * us as a gateway to somewhere. However, we must let the routing
86 * protocol deal with that.
87 *
88 */
89 void
90 ip6_forward(struct mbuf *m, int srcrt)
91 {
92 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
93 struct sockaddr_in6 *dst = NULL;
94 struct rtentry *rt = NULL;
95 struct route_in6 rin6;
96 int error, type = 0, code = 0;
97 struct mbuf *mcopy = NULL;
98 struct ifnet *origifp; /* maybe unnecessary */
99 u_int32_t inzone, outzone;
100 struct in6_addr src_in6, dst_in6, odst;
101 struct m_tag *fwd_tag;
102 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
103
104 /*
105 * Do not forward packets to multicast destination (should be handled
106 * by ip6_mforward().
107 * Do not forward packets with unspecified source. It was discussed
108 * in July 2000, on the ipngwg mailing list.
109 */
110 if ((m->m_flags & (M_BCAST|M_MCAST)) != 0 ||
111 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
112 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
113 IP6STAT_INC(ip6s_cantforward);
114 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
115 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
116 V_ip6_log_time = time_uptime;
117 log(LOG_DEBUG,
118 "cannot forward "
119 "from %s to %s nxt %d received on %s\n",
120 ip6_sprintf(ip6bufs, &ip6->ip6_src),
121 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
122 ip6->ip6_nxt,
123 if_name(m->m_pkthdr.rcvif));
124 }
125 m_freem(m);
126 return;
127 }
128
129 if (
130 #ifdef IPSTEALTH
131 V_ip6stealth == 0 &&
132 #endif
133 ip6->ip6_hlim <= IPV6_HLIMDEC) {
134 /* XXX in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard) */
135 icmp6_error(m, ICMP6_TIME_EXCEEDED,
136 ICMP6_TIME_EXCEED_TRANSIT, 0);
137 return;
138 }
139
140 /*
141 * Save at most ICMPV6_PLD_MAXLEN (= the min IPv6 MTU -
142 * size of IPv6 + ICMPv6 headers) bytes of the packet in case
143 * we need to generate an ICMP6 message to the src.
144 * Thanks to M_EXT, in most cases copy will not occur.
145 *
146 * It is important to save it before IPsec processing as IPsec
147 * processing may modify the mbuf.
148 */
149 mcopy = m_copym(m, 0, imin(m->m_pkthdr.len, ICMPV6_PLD_MAXLEN),
150 M_NOWAIT);
151 #ifdef IPSTEALTH
152 if (V_ip6stealth == 0)
153 #endif
154 ip6->ip6_hlim -= IPV6_HLIMDEC;
155
156 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
157 if (IPSEC_ENABLED(ipv6)) {
158 if ((error = IPSEC_FORWARD(ipv6, m)) != 0) {
159 /* mbuf consumed by IPsec */
160 m_freem(mcopy);
161 if (error != EINPROGRESS)
162 IP6STAT_INC(ip6s_cantforward);
163 return;
164 }
165 /* No IPsec processing required */
166 }
167 #endif
168 again:
169 bzero(&rin6, sizeof(struct route_in6));
170 dst = (struct sockaddr_in6 *)&rin6.ro_dst;
171 dst->sin6_len = sizeof(struct sockaddr_in6);
172 dst->sin6_family = AF_INET6;
173 dst->sin6_addr = ip6->ip6_dst;
174 again2:
175 rin6.ro_rt = in6_rtalloc1((struct sockaddr *)dst, 0, 0, M_GETFIB(m));
176 rt = rin6.ro_rt;
177 if (rin6.ro_rt != NULL)
178 RT_UNLOCK(rin6.ro_rt);
179 else {
180 IP6STAT_INC(ip6s_noroute);
181 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_noroute);
182 if (mcopy) {
183 icmp6_error(mcopy, ICMP6_DST_UNREACH,
184 ICMP6_DST_UNREACH_NOROUTE, 0);
185 }
186 goto bad;
187 }
188
189 /*
190 * Source scope check: if a packet can't be delivered to its
191 * destination for the reason that the destination is beyond the scope
192 * of the source address, discard the packet and return an icmp6
193 * destination unreachable error with Code 2 (beyond scope of source
194 * address). We use a local copy of ip6_src, since in6_setscope()
195 * will possibly modify its first argument.
196 * [draft-ietf-ipngwg-icmp-v3-04.txt, Section 3.1]
197 */
198 src_in6 = ip6->ip6_src;
199 if (in6_setscope(&src_in6, rt->rt_ifp, &outzone)) {
200 /* XXX: this should not happen */
201 IP6STAT_INC(ip6s_cantforward);
202 IP6STAT_INC(ip6s_badscope);
203 goto bad;
204 }
205 if (in6_setscope(&src_in6, m->m_pkthdr.rcvif, &inzone)) {
206 IP6STAT_INC(ip6s_cantforward);
207 IP6STAT_INC(ip6s_badscope);
208 goto bad;
209 }
210 if (inzone != outzone) {
211 IP6STAT_INC(ip6s_cantforward);
212 IP6STAT_INC(ip6s_badscope);
213 in6_ifstat_inc(rt->rt_ifp, ifs6_in_discard);
214
215 if (V_ip6_log_time + V_ip6_log_interval < time_uptime) {
216 V_ip6_log_time = time_uptime;
217 log(LOG_DEBUG,
218 "cannot forward "
219 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
220 ip6_sprintf(ip6bufs, &ip6->ip6_src),
221 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
222 ip6->ip6_nxt,
223 if_name(m->m_pkthdr.rcvif), if_name(rt->rt_ifp));
224 }
225 if (mcopy)
226 icmp6_error(mcopy, ICMP6_DST_UNREACH,
227 ICMP6_DST_UNREACH_BEYONDSCOPE, 0);
228 goto bad;
229 }
230
231 /*
232 * Destination scope check: if a packet is going to break the scope
233 * zone of packet's destination address, discard it. This case should
234 * usually be prevented by appropriately-configured routing table, but
235 * we need an explicit check because we may mistakenly forward the
236 * packet to a different zone by (e.g.) a default route.
237 */
238 dst_in6 = ip6->ip6_dst;
239 if (in6_setscope(&dst_in6, m->m_pkthdr.rcvif, &inzone) != 0 ||
240 in6_setscope(&dst_in6, rt->rt_ifp, &outzone) != 0 ||
241 inzone != outzone) {
242 IP6STAT_INC(ip6s_cantforward);
243 IP6STAT_INC(ip6s_badscope);
244 goto bad;
245 }
246
247 if (rt->rt_flags & RTF_GATEWAY)
248 dst = (struct sockaddr_in6 *)rt->rt_gateway;
249
250 /*
251 * If we are to forward the packet using the same interface
252 * as one we got the packet from, perhaps we should send a redirect
253 * to sender to shortcut a hop.
254 * Only send redirect if source is sending directly to us,
255 * and if packet was not source routed (or has any options).
256 * Also, don't send redirect if forwarding using a route
257 * modified by a redirect.
258 */
259 if (V_ip6_sendredirects && rt->rt_ifp == m->m_pkthdr.rcvif && !srcrt &&
260 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0)
261 type = ND_REDIRECT;
262
263 /*
264 * Fake scoped addresses. Note that even link-local source or
265 * destinaion can appear, if the originating node just sends the
266 * packet to us (without address resolution for the destination).
267 * Since both icmp6_error and icmp6_redirect_output fill the embedded
268 * link identifiers, we can do this stuff after making a copy for
269 * returning an error.
270 */
271 if ((rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
272 /*
273 * See corresponding comments in ip6_output.
274 * XXX: but is it possible that ip6_forward() sends a packet
275 * to a loopback interface? I don't think so, and thus
276 * I bark here. (jinmei@kame.net)
277 * XXX: it is common to route invalid packets to loopback.
278 * also, the codepath will be visited on use of ::1 in
279 * rthdr. (itojun)
280 */
281 #if 1
282 if (0)
283 #else
284 if ((rt->rt_flags & (RTF_BLACKHOLE|RTF_REJECT)) == 0)
285 #endif
286 {
287 printf("ip6_forward: outgoing interface is loopback. "
288 "src %s, dst %s, nxt %d, rcvif %s, outif %s\n",
289 ip6_sprintf(ip6bufs, &ip6->ip6_src),
290 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
291 ip6->ip6_nxt, if_name(m->m_pkthdr.rcvif),
292 if_name(rt->rt_ifp));
293 }
294
295 /* we can just use rcvif in forwarding. */
296 origifp = m->m_pkthdr.rcvif;
297 }
298 else
299 origifp = rt->rt_ifp;
300 /*
301 * clear embedded scope identifiers if necessary.
302 * in6_clearscope will touch the addresses only when necessary.
303 */
304 in6_clearscope(&ip6->ip6_src);
305 in6_clearscope(&ip6->ip6_dst);
306
307 /* Jump over all PFIL processing if hooks are not active. */
308 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
309 goto pass;
310
311 odst = ip6->ip6_dst;
312 /* Run through list of hooks for forwarded packets. */
313 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, rt->rt_ifp, PFIL_OUT,
314 PFIL_FWD, NULL);
315 if (error != 0 || m == NULL)
316 goto freecopy; /* consumed by filter */
317 ip6 = mtod(m, struct ip6_hdr *);
318
319 /* See if destination IP address was changed by packet filter. */
320 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
321 m->m_flags |= M_SKIP_FIREWALL;
322 /* If destination is now ourself drop to ip6_input(). */
323 if (in6_localip(&ip6->ip6_dst))
324 m->m_flags |= M_FASTFWD_OURS;
325 else {
326 RTFREE(rt);
327 goto again; /* Redo the routing table lookup. */
328 }
329 }
330
331 /* See if local, if yes, send it to netisr. */
332 if (m->m_flags & M_FASTFWD_OURS) {
333 if (m->m_pkthdr.rcvif == NULL)
334 m->m_pkthdr.rcvif = V_loif;
335 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
336 m->m_pkthdr.csum_flags |=
337 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
338 m->m_pkthdr.csum_data = 0xffff;
339 }
340 #if defined(SCTP) || defined(SCTP_SUPPORT)
341 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
342 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
343 #endif
344 error = netisr_queue(NETISR_IPV6, m);
345 goto out;
346 }
347 /* Or forward to some other address? */
348 if ((m->m_flags & M_IP6_NEXTHOP) &&
349 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
350 dst = (struct sockaddr_in6 *)&rin6.ro_dst;
351 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
352 m->m_flags |= M_SKIP_FIREWALL;
353 m->m_flags &= ~M_IP6_NEXTHOP;
354 m_tag_delete(m, fwd_tag);
355 RTFREE(rt);
356 goto again2;
357 }
358
359 pass:
360 /* See if the size was changed by the packet filter. */
361 if (m->m_pkthdr.len > IN6_LINKMTU(rt->rt_ifp)) {
362 in6_ifstat_inc(rt->rt_ifp, ifs6_in_toobig);
363 if (mcopy)
364 icmp6_error(mcopy, ICMP6_PACKET_TOO_BIG, 0,
365 IN6_LINKMTU(rt->rt_ifp));
366 goto bad;
367 }
368
369 error = nd6_output_ifp(rt->rt_ifp, origifp, m, dst, NULL);
370 if (error) {
371 in6_ifstat_inc(rt->rt_ifp, ifs6_out_discard);
372 IP6STAT_INC(ip6s_cantforward);
373 } else {
374 IP6STAT_INC(ip6s_forward);
375 in6_ifstat_inc(rt->rt_ifp, ifs6_out_forward);
376 if (type)
377 IP6STAT_INC(ip6s_redirectsent);
378 else {
379 if (mcopy)
380 goto freecopy;
381 }
382 }
383
384 if (mcopy == NULL)
385 goto out;
386 switch (error) {
387 case 0:
388 if (type == ND_REDIRECT) {
389 icmp6_redirect_output(mcopy, rt);
390 goto out;
391 }
392 goto freecopy;
393
394 case EMSGSIZE:
395 /* xxx MTU is constant in PPP? */
396 goto freecopy;
397
398 case ENOBUFS:
399 /* Tell source to slow down like source quench in IP? */
400 goto freecopy;
401
402 case ENETUNREACH: /* shouldn't happen, checked above */
403 case EHOSTUNREACH:
404 case ENETDOWN:
405 case EHOSTDOWN:
406 default:
407 type = ICMP6_DST_UNREACH;
408 code = ICMP6_DST_UNREACH_ADDR;
409 break;
410 }
411 icmp6_error(mcopy, type, code, 0);
412 goto out;
413
414 freecopy:
415 m_freem(mcopy);
416 goto out;
417 bad:
418 m_freem(m);
419 out:
420 if (rt != NULL)
421 RTFREE(rt);
422 }
Cache object: 49d5d6a45afbab821c314d8f1d8f30dd
|