xref: /freebsd/sys/netinet6/ip6_fastfwd.c (revision 64a0982bee3db2236df43357e70ce8dddbc21d48)
1 /*-
2  * Copyright (c) 2014-2016 Andrey V. Elsukov <ae@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_inet6.h"
31 #include "opt_ipstealth.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/mbuf.h>
36 #include <sys/socket.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/route.h>
43 #include <net/pfil.h>
44 #include <net/vnet.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_kdtrace.h>
48 #include <netinet/in_var.h>
49 #include <netinet/ip_var.h>
50 #include <netinet/ip6.h>
51 #include <netinet/icmp6.h>
52 #include <netinet6/in6_var.h>
53 #include <netinet6/in6_fib.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet6/nd6.h>
56 
57 static int
58 ip6_findroute(struct nhop6_basic *pnh, const struct sockaddr_in6 *dst,
59     struct mbuf *m)
60 {
61 
62 	if (fib6_lookup_nh_basic(M_GETFIB(m), &dst->sin6_addr,
63 	    dst->sin6_scope_id, 0, dst->sin6_flowinfo, pnh) != 0) {
64 		IP6STAT_INC(ip6s_noroute);
65 		IP6STAT_INC(ip6s_cantforward);
66 		icmp6_error(m, ICMP6_DST_UNREACH,
67 		    ICMP6_DST_UNREACH_NOROUTE, 0);
68 		return (EHOSTUNREACH);
69 	}
70 	if (pnh->nh_flags & NHF_BLACKHOLE) {
71 		IP6STAT_INC(ip6s_cantforward);
72 		m_freem(m);
73 		return (EHOSTUNREACH);
74 	}
75 
76 	if (pnh->nh_flags & NHF_REJECT) {
77 		IP6STAT_INC(ip6s_cantforward);
78 		icmp6_error(m, ICMP6_DST_UNREACH,
79 		    ICMP6_DST_UNREACH_REJECT, 0);
80 		return (EHOSTUNREACH);
81 	}
82 	return (0);
83 }
84 
85 struct mbuf*
86 ip6_tryforward(struct mbuf *m)
87 {
88 	struct sockaddr_in6 dst;
89 	struct nhop6_basic nh;
90 	struct m_tag *fwd_tag;
91 	struct ip6_hdr *ip6;
92 	struct ifnet *rcvif;
93 	uint32_t plen;
94 	int error;
95 
96 	/*
97 	 * Fallback conditions to ip6_input for slow path processing.
98 	 */
99 	ip6 = mtod(m, struct ip6_hdr *);
100 	if (ip6->ip6_nxt == IPPROTO_HOPOPTS ||
101 	    IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
102 	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst) ||
103 	    IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src) ||
104 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) ||
105 	    in6_localip(&ip6->ip6_dst))
106 		return (m);
107 	/*
108 	 * Check that the amount of data in the buffers
109 	 * is as at least much as the IPv6 header would have us expect.
110 	 * Trim mbufs if longer than we expect.
111 	 * Drop packet if shorter than we expect.
112 	 */
113 	rcvif = m->m_pkthdr.rcvif;
114 	plen = ntohs(ip6->ip6_plen);
115 	if (plen == 0) {
116 		/*
117 		 * Jumbograms must have hop-by-hop header and go via
118 		 * slow path.
119 		 */
120 		IP6STAT_INC(ip6s_badoptions);
121 		goto dropin;
122 	}
123 	if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
124 		IP6STAT_INC(ip6s_tooshort);
125 		in6_ifstat_inc(rcvif, ifs6_in_truncated);
126 		goto dropin;
127 	}
128 	if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
129 		if (m->m_len == m->m_pkthdr.len) {
130 			m->m_len = sizeof(struct ip6_hdr) + plen;
131 			m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
132 		} else
133 			m_adj(m, sizeof(struct ip6_hdr) + plen -
134 			    m->m_pkthdr.len);
135 	}
136 
137 	/*
138 	 * Hop limit.
139 	 */
140 #ifdef IPSTEALTH
141 	if (!V_ip6stealth)
142 #endif
143 	if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
144 		icmp6_error(m, ICMP6_TIME_EXCEEDED,
145 		    ICMP6_TIME_EXCEED_TRANSIT, 0);
146 		m = NULL;
147 		goto dropin;
148 	}
149 
150 	bzero(&dst, sizeof(dst));
151 	dst.sin6_family = AF_INET6;
152 	dst.sin6_len = sizeof(dst);
153 	dst.sin6_addr = ip6->ip6_dst;
154 
155 	/*
156 	 * Incoming packet firewall processing.
157 	 */
158 	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
159 		goto passin;
160 	if (pfil_run_hooks(&V_inet6_pfil_hook, &m, rcvif, PFIL_IN,
161 	    NULL) != 0 || m == NULL)
162 		goto dropin;
163 	/*
164 	 * If packet filter sets the M_FASTFWD_OURS flag, this means
165 	 * that new destination or next hop is our local address.
166 	 * So, we can just go back to ip6_input.
167 	 * XXX: should we decrement ip6_hlim in such case?
168 	 *
169 	 * Also it can forward packet to another destination, e.g.
170 	 * M_IP6_NEXTHOP flag is set and fwd_tag is attached to mbuf.
171 	 */
172 	if (m->m_flags & M_FASTFWD_OURS)
173 		return (m);
174 
175 	ip6 = mtod(m, struct ip6_hdr *);
176 	if ((m->m_flags & M_IP6_NEXTHOP) &&
177 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
178 		/*
179 		 * Now we will find route to forwarded by pfil destination.
180 		 */
181 		bcopy((fwd_tag + 1), &dst, sizeof(dst));
182 		m->m_flags &= ~M_IP6_NEXTHOP;
183 		m_tag_delete(m, fwd_tag);
184 	} else {
185 		/* Update dst since pfil could change it */
186 		dst.sin6_addr = ip6->ip6_dst;
187 	}
188 passin:
189 	/*
190 	 * Find route to destination.
191 	 */
192 	if (ip6_findroute(&nh, &dst, m) != 0) {
193 		m = NULL;
194 		in6_ifstat_inc(rcvif, ifs6_in_noroute);
195 		goto dropin;
196 	}
197 	/*
198 	 * We used slow path processing for packets with scoped addresses.
199 	 * So, scope checks aren't needed here.
200 	 */
201 	if (m->m_pkthdr.len > nh.nh_mtu) {
202 		in6_ifstat_inc(nh.nh_ifp, ifs6_in_toobig);
203 		icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, nh.nh_mtu);
204 		m = NULL;
205 		goto dropout;
206 	}
207 
208 	/*
209 	 * Outgoing packet firewall processing.
210 	 */
211 	if (!PFIL_HOOKED(&V_inet6_pfil_hook))
212 		goto passout;
213 	if (pfil_run_hooks(&V_inet6_pfil_hook, &m, nh.nh_ifp, PFIL_OUT,
214 	    NULL) != 0 || m == NULL)
215 		goto dropout;
216 	/*
217 	 * If packet filter sets the M_FASTFWD_OURS flag, this means
218 	 * that new destination or next hop is our local address.
219 	 * So, we can just go back to ip6_input.
220 	 *
221 	 * Also it can forward packet to another destination, e.g.
222 	 * M_IP6_NEXTHOP flag is set and fwd_tag is attached to mbuf.
223 	 */
224 	if (m->m_flags & M_FASTFWD_OURS) {
225 		/*
226 		 * XXX: we did one hop and should decrement hop limit. But
227 		 * now we are the destination and just don't pay attention.
228 		 */
229 		return (m);
230 	}
231 	/*
232 	 * Again. A packet filter could change the destination address.
233 	 */
234 	ip6 = mtod(m, struct ip6_hdr *);
235 	if (m->m_flags & M_IP6_NEXTHOP)
236 		fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
237 	else
238 		fwd_tag = NULL;
239 
240 	if (fwd_tag != NULL ||
241 	    !IN6_ARE_ADDR_EQUAL(&dst.sin6_addr, &ip6->ip6_dst)) {
242 		if (fwd_tag != NULL) {
243 			bcopy((fwd_tag + 1), &dst, sizeof(dst));
244 			m->m_flags &= ~M_IP6_NEXTHOP;
245 			m_tag_delete(m, fwd_tag);
246 		} else
247 			dst.sin6_addr = ip6->ip6_dst;
248 		/*
249 		 * Redo route lookup with new destination address
250 		 */
251 		if (ip6_findroute(&nh, &dst, m) != 0) {
252 			m = NULL;
253 			goto dropout;
254 		}
255 	}
256 passout:
257 #ifdef IPSTEALTH
258 	if (!V_ip6stealth)
259 #endif
260 	{
261 		ip6->ip6_hlim -= IPV6_HLIMDEC;
262 	}
263 
264 	m_clrprotoflags(m);	/* Avoid confusing lower layers. */
265 	IP_PROBE(send, NULL, NULL, ip6, nh.nh_ifp, NULL, ip6);
266 
267 	/*
268 	 * XXX: we need to use destination address with embedded scope
269 	 * zone id, because LLTABLE uses such form of addresses for lookup.
270 	 */
271 	dst.sin6_addr = nh.nh_addr;
272 	if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
273 		dst.sin6_addr.s6_addr16[1] = htons(nh.nh_ifp->if_index & 0xffff);
274 
275 	error = (*nh.nh_ifp->if_output)(nh.nh_ifp, m,
276 	    (struct sockaddr *)&dst, NULL);
277 	if (error != 0) {
278 		in6_ifstat_inc(nh.nh_ifp, ifs6_out_discard);
279 		IP6STAT_INC(ip6s_cantforward);
280 	} else {
281 		in6_ifstat_inc(nh.nh_ifp, ifs6_out_forward);
282 		IP6STAT_INC(ip6s_forward);
283 	}
284 	return (NULL);
285 dropin:
286 	in6_ifstat_inc(rcvif, ifs6_in_discard);
287 	goto drop;
288 dropout:
289 	in6_ifstat_inc(nh.nh_ifp, ifs6_out_discard);
290 drop:
291 	if (m != NULL)
292 		m_freem(m);
293 	return (NULL);
294 }
295 
296