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_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/rmlock.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 RIB_RLOCK_TRACKER; 175 struct rib_head *rh; 176 struct radix_node *rn; 177 struct sockaddr_in6 sin6; 178 struct rtentry *rte; 179 180 KASSERT((fibnum < rt_numfibs), ("fib6_lookup_nh_basic: bad fibnum")); 181 rh = rt_tables_get_rnh(fibnum, AF_INET6); 182 if (rh == NULL) 183 return (ENOENT); 184 185 /* Prepare lookup key */ 186 memset(&sin6, 0, sizeof(sin6)); 187 sin6.sin6_addr = *dst; 188 sin6.sin6_len = sizeof(struct sockaddr_in6); 189 /* Assume scopeid is valid and embed it directly */ 190 if (IN6_IS_SCOPE_LINKLOCAL(dst)) 191 sin6.sin6_addr.s6_addr16[1] = htons(scopeid & 0xffff); 192 193 RIB_RLOCK(rh); 194 rn = rh->rnh_matchaddr((void *)&sin6, &rh->head); 195 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 196 rte = RNTORT(rn); 197 /* Ensure route & ifp is UP */ 198 if (RT_LINK_IS_UP(rte->rt_ifp)) { 199 fib6_rte_to_nh_basic(rte, &sin6.sin6_addr, flags, pnh6); 200 RIB_RUNLOCK(rh); 201 return (0); 202 } 203 } 204 RIB_RUNLOCK(rh); 205 206 return (ENOENT); 207 } 208 209 /* 210 * Performs IPv6 route table lookup on @dst. Returns 0 on success. 211 * Stores extended nexthop info into provided @pnh6 structure. 212 * Note that 213 * - nh_ifp cannot be safely dereferenced unless NHR_REF is specified. 214 * - in that case you need to call fib6_free_nh_ext() 215 * - nh_ifp represents logical transmit interface (rt_ifp) by default 216 * - nh_ifp represents "address" interface if NHR_IFAIF flag is passed 217 * - mtu from logical transmit interface will be returned. 218 * - scope will be embedded in nh_addr 219 */ 220 int 221 fib6_lookup_nh_ext(uint32_t fibnum, const struct in6_addr *dst,uint32_t scopeid, 222 uint32_t flags, uint32_t flowid, struct nhop6_extended *pnh6) 223 { 224 RIB_RLOCK_TRACKER; 225 struct rib_head *rh; 226 struct radix_node *rn; 227 struct sockaddr_in6 sin6; 228 struct rtentry *rte; 229 230 KASSERT((fibnum < rt_numfibs), ("fib6_lookup_nh_ext: bad fibnum")); 231 rh = rt_tables_get_rnh(fibnum, AF_INET6); 232 if (rh == NULL) 233 return (ENOENT); 234 235 /* Prepare lookup key */ 236 memset(&sin6, 0, sizeof(sin6)); 237 sin6.sin6_len = sizeof(struct sockaddr_in6); 238 sin6.sin6_addr = *dst; 239 /* Assume scopeid is valid and embed it directly */ 240 if (IN6_IS_SCOPE_LINKLOCAL(dst)) 241 sin6.sin6_addr.s6_addr16[1] = htons(scopeid & 0xffff); 242 243 RIB_RLOCK(rh); 244 rn = rh->rnh_matchaddr((void *)&sin6, &rh->head); 245 if (rn != NULL && ((rn->rn_flags & RNF_ROOT) == 0)) { 246 rte = RNTORT(rn); 247 #ifdef RADIX_MPATH 248 rte = rt_mpath_select(rte, flowid); 249 if (rte == NULL) { 250 RIB_RUNLOCK(rh); 251 return (ENOENT); 252 } 253 #endif 254 /* Ensure route & ifp is UP */ 255 if (RT_LINK_IS_UP(rte->rt_ifp)) { 256 fib6_rte_to_nh_extended(rte, &sin6.sin6_addr, flags, 257 pnh6); 258 if ((flags & NHR_REF) != 0) { 259 /* TODO: Do lwref on egress ifp's */ 260 } 261 RIB_RUNLOCK(rh); 262 263 return (0); 264 } 265 } 266 RIB_RUNLOCK(rh); 267 268 return (ENOENT); 269 } 270 271 void 272 fib6_free_nh_ext(uint32_t fibnum, struct nhop6_extended *pnh6) 273 { 274 275 } 276 277 #endif 278 279