2 * Copyright (c) 2014-2016 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_inet6.h"
31 #include "opt_ipstealth.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
41 #include <net/if_var.h>
42 #include <net/route.h>
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>
58 ip6_findroute(struct nhop6_basic *pnh, const struct sockaddr_in6 *dst,
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);
70 if (pnh->nh_flags & NHF_BLACKHOLE) {
71 IP6STAT_INC(ip6s_cantforward);
73 return (EHOSTUNREACH);
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);
86 ip6_tryforward(struct mbuf *m)
88 struct sockaddr_in6 dst;
89 struct nhop6_basic nh;
90 struct m_tag *fwd_tag;
97 * Fallback conditions to ip6_input for slow path processing.
99 ip6 = mtod(m, struct ip6_hdr *);
100 if ((m->m_flags & (M_BCAST | M_MCAST)) != 0 ||
101 ip6->ip6_nxt == IPPROTO_HOPOPTS ||
102 IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
103 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst) ||
104 IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src) ||
105 IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) ||
106 in6_localip(&ip6->ip6_dst))
109 * Check that the amount of data in the buffers
110 * is as at least much as the IPv6 header would have us expect.
111 * Trim mbufs if longer than we expect.
112 * Drop packet if shorter than we expect.
114 rcvif = m->m_pkthdr.rcvif;
115 plen = ntohs(ip6->ip6_plen);
118 * Jumbograms must have hop-by-hop header and go via
121 IP6STAT_INC(ip6s_badoptions);
124 if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) {
125 IP6STAT_INC(ip6s_tooshort);
126 in6_ifstat_inc(rcvif, ifs6_in_truncated);
129 if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) {
130 if (m->m_len == m->m_pkthdr.len) {
131 m->m_len = sizeof(struct ip6_hdr) + plen;
132 m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen;
134 m_adj(m, sizeof(struct ip6_hdr) + plen -
144 if (ip6->ip6_hlim <= IPV6_HLIMDEC) {
145 icmp6_error(m, ICMP6_TIME_EXCEEDED,
146 ICMP6_TIME_EXCEED_TRANSIT, 0);
151 bzero(&dst, sizeof(dst));
152 dst.sin6_family = AF_INET6;
153 dst.sin6_len = sizeof(dst);
154 dst.sin6_addr = ip6->ip6_dst;
157 * Incoming packet firewall processing.
159 if (!PFIL_HOOKED_IN(V_inet6_pfil_head))
161 if (pfil_run_hooks(V_inet6_pfil_head, &m, rcvif, PFIL_IN, NULL) !=
165 * If packet filter sets the M_FASTFWD_OURS flag, this means
166 * that new destination or next hop is our local address.
167 * So, we can just go back to ip6_input.
168 * XXX: should we decrement ip6_hlim in such case?
170 * Also it can forward packet to another destination, e.g.
171 * M_IP6_NEXTHOP flag is set and fwd_tag is attached to mbuf.
173 if (m->m_flags & M_FASTFWD_OURS)
176 ip6 = mtod(m, struct ip6_hdr *);
177 if ((m->m_flags & M_IP6_NEXTHOP) &&
178 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
180 * Now we will find route to forwarded by pfil destination.
182 bcopy((fwd_tag + 1), &dst, sizeof(dst));
183 m->m_flags &= ~M_IP6_NEXTHOP;
184 m_tag_delete(m, fwd_tag);
186 /* Update dst since pfil could change it */
187 dst.sin6_addr = ip6->ip6_dst;
191 * Find route to destination.
193 if (ip6_findroute(&nh, &dst, m) != 0) {
195 in6_ifstat_inc(rcvif, ifs6_in_noroute);
198 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head)) {
199 if (m->m_pkthdr.len > nh.nh_mtu) {
200 in6_ifstat_inc(nh.nh_ifp, ifs6_in_toobig);
201 icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, nh.nh_mtu);
209 * Outgoing packet firewall processing.
211 if (pfil_run_hooks(V_inet6_pfil_head, &m, nh.nh_ifp, PFIL_OUT |
212 PFIL_FWD, NULL) != PFIL_PASS)
216 * We used slow path processing for packets with scoped addresses.
217 * So, scope checks aren't needed here.
219 if (m->m_pkthdr.len > nh.nh_mtu) {
220 in6_ifstat_inc(nh.nh_ifp, ifs6_in_toobig);
221 icmp6_error(m, ICMP6_PACKET_TOO_BIG, 0, nh.nh_mtu);
227 * If packet filter sets the M_FASTFWD_OURS flag, this means
228 * that new destination or next hop is our local address.
229 * So, we can just go back to ip6_input.
231 * Also it can forward packet to another destination, e.g.
232 * M_IP6_NEXTHOP flag is set and fwd_tag is attached to mbuf.
234 if (m->m_flags & M_FASTFWD_OURS) {
236 * XXX: we did one hop and should decrement hop limit. But
237 * now we are the destination and just don't pay attention.
242 * Again. A packet filter could change the destination address.
244 ip6 = mtod(m, struct ip6_hdr *);
245 if (m->m_flags & M_IP6_NEXTHOP)
246 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
250 if (fwd_tag != NULL ||
251 !IN6_ARE_ADDR_EQUAL(&dst.sin6_addr, &ip6->ip6_dst)) {
252 if (fwd_tag != NULL) {
253 bcopy((fwd_tag + 1), &dst, sizeof(dst));
254 m->m_flags &= ~M_IP6_NEXTHOP;
255 m_tag_delete(m, fwd_tag);
257 dst.sin6_addr = ip6->ip6_dst;
259 * Redo route lookup with new destination address
261 if (ip6_findroute(&nh, &dst, m) != 0) {
271 ip6->ip6_hlim -= IPV6_HLIMDEC;
274 m_clrprotoflags(m); /* Avoid confusing lower layers. */
275 IP_PROBE(send, NULL, NULL, ip6, nh.nh_ifp, NULL, ip6);
278 * XXX: we need to use destination address with embedded scope
279 * zone id, because LLTABLE uses such form of addresses for lookup.
281 dst.sin6_addr = nh.nh_addr;
282 if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
283 dst.sin6_addr.s6_addr16[1] = htons(nh.nh_ifp->if_index & 0xffff);
285 error = (*nh.nh_ifp->if_output)(nh.nh_ifp, m,
286 (struct sockaddr *)&dst, NULL);
288 in6_ifstat_inc(nh.nh_ifp, ifs6_out_discard);
289 IP6STAT_INC(ip6s_cantforward);
291 in6_ifstat_inc(nh.nh_ifp, ifs6_out_forward);
292 IP6STAT_INC(ip6s_forward);
296 in6_ifstat_inc(rcvif, ifs6_in_discard);
299 in6_ifstat_inc(nh.nh_ifp, ifs6_out_discard);