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