xref: /freebsd/sys/netinet6/in6_fib.c (revision 7453645f2a9411a3f9d982b768bcc323f41cf906)
1 /*-
2  * Copyright (c) 2015
3  * 	Alexander V. Chernikov <melifaro@FreeBSD.org>
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_route.h"
36 #include "opt_mpath.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/lock.h>
41 #include <sys/rwlock.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
46 #include <sys/kernel.h>
47 
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/route.h>
52 #include <net/route_var.h>
53 #include <net/vnet.h>
54 
55 #ifdef RADIX_MPATH
56 #include <net/radix_mpath.h>
57 #endif
58 
59 #include <netinet/in.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_mroute.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/in6_fib.h>
64 #include <netinet6/in6_var.h>
65 #include <netinet6/nd6.h>
66 #include <netinet6/scope6_var.h>
67 
68 #include <net/if_types.h>
69 
70 #ifdef INET6
71 static void fib6_rte_to_nh_extended(struct rtentry *rte,
72     const struct in6_addr *dst, uint32_t flags, struct nhop6_extended *pnh6);
73 static void fib6_rte_to_nh_basic(struct rtentry *rte, const struct in6_addr *dst,
74     uint32_t flags, struct nhop6_basic *pnh6);
75 static struct ifnet *fib6_get_ifaifp(struct rtentry *rte);
76 #define RNTORT(p)	((struct rtentry *)(p))
77 
78 /*
79  * Gets real interface for the @rte.
80  * Returns rt_ifp for !IFF_LOOPBACK routers.
81  * Extracts "real" address interface from interface address
82  * loopback routes.
83  */
84 static struct ifnet *
85 fib6_get_ifaifp(struct rtentry *rte)
86 {
87 	struct ifnet *ifp;
88 	struct sockaddr_dl *sdl;
89 
90 	ifp = rte->rt_ifp;
91 	if ((ifp->if_flags & IFF_LOOPBACK) &&
92 	    rte->rt_gateway->sa_family == AF_LINK) {
93 		sdl = (struct sockaddr_dl *)rte->rt_gateway;
94 		return (ifnet_byindex(sdl->sdl_index));
95 	}
96 
97 	return (ifp);
98 }
99 
100 static void
101 fib6_rte_to_nh_basic(struct rtentry *rte, const struct in6_addr *dst,
102     uint32_t flags, struct nhop6_basic *pnh6)
103 {
104 	struct sockaddr_in6 *gw;
105 
106 	/* Do explicit nexthop zero unless we're copying it */
107 	memset(pnh6, 0, sizeof(*pnh6));
108 
109 	if ((flags & NHR_IFAIF) != 0)
110 		pnh6->nh_ifp = fib6_get_ifaifp(rte);
111 	else
112 		pnh6->nh_ifp = rte->rt_ifp;
113 
114 	pnh6->nh_mtu = min(rte->rt_mtu, IN6_LINKMTU(rte->rt_ifp));
115 	if (rte->rt_flags & RTF_GATEWAY) {
116 		gw = (struct sockaddr_in6 *)rte->rt_gateway;
117 		pnh6->nh_addr = gw->sin6_addr;
118 		in6_clearscope(&pnh6->nh_addr);
119 	} else
120 		pnh6->nh_addr = *dst;
121 	/* Set flags */
122 	pnh6->nh_flags = fib_rte_to_nh_flags(rte->rt_flags);
123 	gw = (struct sockaddr_in6 *)rt_key(rte);
124 	if (IN6_IS_ADDR_UNSPECIFIED(&gw->sin6_addr))
125 		pnh6->nh_flags |= NHF_DEFAULT;
126 }
127 
128 static void
129 fib6_rte_to_nh_extended(struct rtentry *rte, const struct in6_addr *dst,
130     uint32_t flags, struct nhop6_extended *pnh6)
131 {
132 	struct sockaddr_in6 *gw;
133 
134 	/* Do explicit nexthop zero unless we're copying it */
135 	memset(pnh6, 0, sizeof(*pnh6));
136 
137 	if ((flags & NHR_IFAIF) != 0)
138 		pnh6->nh_ifp = fib6_get_ifaifp(rte);
139 	else
140 		pnh6->nh_ifp = rte->rt_ifp;
141 
142 	pnh6->nh_mtu = min(rte->rt_mtu, IN6_LINKMTU(rte->rt_ifp));
143 	if (rte->rt_flags & RTF_GATEWAY) {
144 		gw = (struct sockaddr_in6 *)rte->rt_gateway;
145 		pnh6->nh_addr = gw->sin6_addr;
146 		in6_clearscope(&pnh6->nh_addr);
147 	} else
148 		pnh6->nh_addr = *dst;
149 	/* Set flags */
150 	pnh6->nh_flags = fib_rte_to_nh_flags(rte->rt_flags);
151 	gw = (struct sockaddr_in6 *)rt_key(rte);
152 	if (IN6_IS_ADDR_UNSPECIFIED(&gw->sin6_addr))
153 		pnh6->nh_flags |= NHF_DEFAULT;
154 }
155 
156 /*
157  * Performs IPv6 route table lookup on @dst. Returns 0 on success.
158  * Stores basic nexthop info into provided @pnh6 structure.
159  * Note that
160  * - nh_ifp represents logical transmit interface (rt_ifp) by default
161  * - nh_ifp represents "address" interface if NHR_IFAIF flag is passed
162  * - mtu from logical transmit interface will be returned.
163  * - nh_ifp cannot be safely dereferenced
164  * - nh_ifp represents rt_ifp (e.g. if looking up address on
165  *   interface "ix0" pointer to "ix0" interface will be returned instead
166  *   of "lo0")
167  * - howewer mtu from "transmit" interface will be returned.
168  * - scope will be embedded in nh_addr
169  */
170 int
171 fib6_lookup_nh_basic(uint32_t fibnum, const struct in6_addr *dst, uint32_t scopeid,
172     uint32_t flags, uint32_t flowid, struct nhop6_basic *pnh6)
173 {
174 	struct rib_head *rh;
175 	struct radix_node *rn;
176 	struct sockaddr_in6 sin6;
177 	struct rtentry *rte;
178 
179 	KASSERT((fibnum < rt_numfibs), ("fib6_lookup_nh_basic: bad fibnum"));
180 	rh = rt_tables_get_rnh(fibnum, AF_INET6);
181 	if (rh == NULL)
182 		return (ENOENT);
183 
184 	/* Prepare lookup key */
185 	memset(&sin6, 0, sizeof(sin6));
186 	sin6.sin6_addr = *dst;
187 	sin6.sin6_len = sizeof(struct sockaddr_in6);
188 	/* Assume scopeid is valid and embed it directly */
189 	if (IN6_IS_SCOPE_LINKLOCAL(dst))
190 		sin6.sin6_addr.s6_addr16[1] = htons(scopeid & 0xffff);
191 
192 	RIB_RLOCK(rh);
193 	rn = rh->rnh_matchaddr((void *)&sin6, &rh->head);
194 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
195 		rte = RNTORT(rn);
196 		/* Ensure route & ifp is UP */
197 		if (RT_LINK_IS_UP(rte->rt_ifp)) {
198 			fib6_rte_to_nh_basic(rte, &sin6.sin6_addr, flags, pnh6);
199 			RIB_RUNLOCK(rh);
200 			return (0);
201 		}
202 	}
203 	RIB_RUNLOCK(rh);
204 
205 	return (ENOENT);
206 }
207 
208 /*
209  * Performs IPv6 route table lookup on @dst. Returns 0 on success.
210  * Stores extended nexthop info into provided @pnh6 structure.
211  * Note that
212  * - nh_ifp cannot be safely dereferenced unless NHR_REF is specified.
213  * - in that case you need to call fib6_free_nh_ext()
214  * - nh_ifp represents logical transmit interface (rt_ifp) by default
215  * - nh_ifp represents "address" interface if NHR_IFAIF flag is passed
216  * - mtu from logical transmit interface will be returned.
217  * - scope will be embedded in nh_addr
218  */
219 int
220 fib6_lookup_nh_ext(uint32_t fibnum, const struct in6_addr *dst,uint32_t scopeid,
221     uint32_t flags, uint32_t flowid, struct nhop6_extended *pnh6)
222 {
223 	struct rib_head *rh;
224 	struct radix_node *rn;
225 	struct sockaddr_in6 sin6;
226 	struct rtentry *rte;
227 
228 	KASSERT((fibnum < rt_numfibs), ("fib6_lookup_nh_ext: bad fibnum"));
229 	rh = rt_tables_get_rnh(fibnum, AF_INET6);
230 	if (rh == NULL)
231 		return (ENOENT);
232 
233 	/* Prepare lookup key */
234 	memset(&sin6, 0, sizeof(sin6));
235 	sin6.sin6_len = sizeof(struct sockaddr_in6);
236 	sin6.sin6_addr = *dst;
237 	/* Assume scopeid is valid and embed it directly */
238 	if (IN6_IS_SCOPE_LINKLOCAL(dst))
239 		sin6.sin6_addr.s6_addr16[1] = htons(scopeid & 0xffff);
240 
241 	RIB_RLOCK(rh);
242 	rn = rh->rnh_matchaddr((void *)&sin6, &rh->head);
243 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
244 		rte = RNTORT(rn);
245 #ifdef RADIX_MPATH
246 		rte = rt_mpath_select(rte, flowid);
247 		if (rte == NULL) {
248 			RIB_RUNLOCK(rh);
249 			return (ENOENT);
250 		}
251 #endif
252 		/* Ensure route & ifp is UP */
253 		if (RT_LINK_IS_UP(rte->rt_ifp)) {
254 			fib6_rte_to_nh_extended(rte, &sin6.sin6_addr, flags,
255 			    pnh6);
256 			if ((flags & NHR_REF) != 0) {
257 				/* TODO: Do lwref on egress ifp's */
258 			}
259 			RIB_RUNLOCK(rh);
260 
261 			return (0);
262 		}
263 	}
264 	RIB_RUNLOCK(rh);
265 
266 	return (ENOENT);
267 }
268 
269 void
270 fib6_free_nh_ext(uint32_t fibnum, struct nhop6_extended *pnh6)
271 {
272 
273 }
274 
275 #endif
276 
277