xref: /freebsd/sys/netinet/in_fib.c (revision 780fb4a2)
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  * 3. 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_route.h"
35 #include "opt_mpath.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/lock.h>
40 #include <sys/rmlock.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/sysctl.h>
45 #include <sys/kernel.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/route.h>
51 #include <net/route_var.h>
52 #include <net/vnet.h>
53 
54 #ifdef RADIX_MPATH
55 #include <net/radix_mpath.h>
56 #endif
57 
58 #include <netinet/in.h>
59 #include <netinet/in_var.h>
60 #include <netinet/in_fib.h>
61 
62 #ifdef INET
63 static void fib4_rte_to_nh_basic(struct rtentry *rte, struct in_addr dst,
64     uint32_t flags, struct nhop4_basic *pnh4);
65 static void fib4_rte_to_nh_extended(struct rtentry *rte, struct in_addr dst,
66     uint32_t flags, struct nhop4_extended *pnh4);
67 
68 #define RNTORT(p)	((struct rtentry *)(p))
69 
70 static void
71 fib4_rte_to_nh_basic(struct rtentry *rte, struct in_addr dst,
72     uint32_t flags, struct nhop4_basic *pnh4)
73 {
74 	struct sockaddr_in *gw;
75 
76 	if ((flags & NHR_IFAIF) != 0)
77 		pnh4->nh_ifp = rte->rt_ifa->ifa_ifp;
78 	else
79 		pnh4->nh_ifp = rte->rt_ifp;
80 	pnh4->nh_mtu = min(rte->rt_mtu, rte->rt_ifp->if_mtu);
81 	if (rte->rt_flags & RTF_GATEWAY) {
82 		gw = (struct sockaddr_in *)rte->rt_gateway;
83 		pnh4->nh_addr = gw->sin_addr;
84 	} else
85 		pnh4->nh_addr = dst;
86 	/* Set flags */
87 	pnh4->nh_flags = fib_rte_to_nh_flags(rte->rt_flags);
88 	gw = (struct sockaddr_in *)rt_key(rte);
89 	if (gw->sin_addr.s_addr == 0)
90 		pnh4->nh_flags |= NHF_DEFAULT;
91 	/* TODO: Handle RTF_BROADCAST here */
92 }
93 
94 static void
95 fib4_rte_to_nh_extended(struct rtentry *rte, struct in_addr dst,
96     uint32_t flags, struct nhop4_extended *pnh4)
97 {
98 	struct sockaddr_in *gw;
99 	struct in_ifaddr *ia;
100 
101 	if ((flags & NHR_IFAIF) != 0)
102 		pnh4->nh_ifp = rte->rt_ifa->ifa_ifp;
103 	else
104 		pnh4->nh_ifp = rte->rt_ifp;
105 	pnh4->nh_mtu = min(rte->rt_mtu, rte->rt_ifp->if_mtu);
106 	if (rte->rt_flags & RTF_GATEWAY) {
107 		gw = (struct sockaddr_in *)rte->rt_gateway;
108 		pnh4->nh_addr = gw->sin_addr;
109 	} else
110 		pnh4->nh_addr = dst;
111 	/* Set flags */
112 	pnh4->nh_flags = fib_rte_to_nh_flags(rte->rt_flags);
113 	gw = (struct sockaddr_in *)rt_key(rte);
114 	if (gw->sin_addr.s_addr == 0)
115 		pnh4->nh_flags |= NHF_DEFAULT;
116 	/* XXX: Set RTF_BROADCAST if GW address is broadcast */
117 
118 	ia = ifatoia(rte->rt_ifa);
119 	pnh4->nh_src = IA_SIN(ia)->sin_addr;
120 }
121 
122 /*
123  * Performs IPv4 route table lookup on @dst. Returns 0 on success.
124  * Stores nexthop info provided @pnh4 structure.
125  * Note that
126  * - nh_ifp cannot be safely dereferenced
127  * - nh_ifp represents logical transmit interface (rt_ifp) (e.g. if
128  *   looking up address on interface "ix0" pointer to "lo0" interface
129  *   will be returned instead of "ix0")
130  * - nh_ifp represents "address" interface if NHR_IFAIF flag is passed
131  * - howewer mtu from "transmit" interface will be returned.
132  */
133 int
134 fib4_lookup_nh_basic(uint32_t fibnum, struct in_addr dst, uint32_t flags,
135     uint32_t flowid, struct nhop4_basic *pnh4)
136 {
137 	RIB_RLOCK_TRACKER;
138 	struct rib_head *rh;
139 	struct radix_node *rn;
140 	struct sockaddr_in sin;
141 	struct rtentry *rte;
142 
143 	KASSERT((fibnum < rt_numfibs), ("fib4_lookup_nh_basic: bad fibnum"));
144 	rh = rt_tables_get_rnh(fibnum, AF_INET);
145 	if (rh == NULL)
146 		return (ENOENT);
147 
148 	/* Prepare lookup key */
149 	memset(&sin, 0, sizeof(sin));
150 	sin.sin_len = sizeof(struct sockaddr_in);
151 	sin.sin_addr = dst;
152 
153 	RIB_RLOCK(rh);
154 	rn = rh->rnh_matchaddr((void *)&sin, &rh->head);
155 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
156 		rte = RNTORT(rn);
157 		/* Ensure route & ifp is UP */
158 		if (RT_LINK_IS_UP(rte->rt_ifp)) {
159 			fib4_rte_to_nh_basic(rte, dst, flags, pnh4);
160 			RIB_RUNLOCK(rh);
161 
162 			return (0);
163 		}
164 	}
165 	RIB_RUNLOCK(rh);
166 
167 	return (ENOENT);
168 }
169 
170 /*
171  * Performs IPv4 route table lookup on @dst. Returns 0 on success.
172  * Stores extende nexthop info provided @pnh4 structure.
173  * Note that
174  * - nh_ifp cannot be safely dereferenced unless NHR_REF is specified.
175  * - in that case you need to call fib4_free_nh_ext()
176  * - nh_ifp represents logical transmit interface (rt_ifp) (e.g. if
177  *   looking up address of interface "ix0" pointer to "lo0" interface
178  *   will be returned instead of "ix0")
179  * - nh_ifp represents "address" interface if NHR_IFAIF flag is passed
180  * - howewer mtu from "transmit" interface will be returned.
181  */
182 int
183 fib4_lookup_nh_ext(uint32_t fibnum, struct in_addr dst, uint32_t flags,
184     uint32_t flowid, struct nhop4_extended *pnh4)
185 {
186 	RIB_RLOCK_TRACKER;
187 	struct rib_head *rh;
188 	struct radix_node *rn;
189 	struct sockaddr_in sin;
190 	struct rtentry *rte;
191 
192 	KASSERT((fibnum < rt_numfibs), ("fib4_lookup_nh_ext: bad fibnum"));
193 	rh = rt_tables_get_rnh(fibnum, AF_INET);
194 	if (rh == NULL)
195 		return (ENOENT);
196 
197 	/* Prepare lookup key */
198 	memset(&sin, 0, sizeof(sin));
199 	sin.sin_len = sizeof(struct sockaddr_in);
200 	sin.sin_addr = dst;
201 
202 	RIB_RLOCK(rh);
203 	rn = rh->rnh_matchaddr((void *)&sin, &rh->head);
204 	if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) {
205 		rte = RNTORT(rn);
206 #ifdef RADIX_MPATH
207 		rte = rt_mpath_select(rte, flowid);
208 		if (rte == NULL) {
209 			RIB_RUNLOCK(rh);
210 			return (ENOENT);
211 		}
212 #endif
213 		/* Ensure route & ifp is UP */
214 		if (RT_LINK_IS_UP(rte->rt_ifp)) {
215 			fib4_rte_to_nh_extended(rte, dst, flags, pnh4);
216 			if ((flags & NHR_REF) != 0) {
217 				/* TODO: lwref on egress ifp's ? */
218 			}
219 			RIB_RUNLOCK(rh);
220 
221 			return (0);
222 		}
223 	}
224 	RIB_RUNLOCK(rh);
225 
226 	return (ENOENT);
227 }
228 
229 void
230 fib4_free_nh_ext(uint32_t fibnum, struct nhop4_extended *pnh4)
231 {
232 
233 }
234 
235 #endif
236