1 /* $NetBSD: rtbl.c,v 1.6 2017/01/11 13:08:29 ozaki-r Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2008, 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Kevin M. Lahey of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the project nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 */ 61 62 /* 63 * Copyright (c) 1980, 1986, 1991, 1993 64 * The Regents of the University of California. All rights reserved. 65 * 66 * Redistribution and use in source and binary forms, with or without 67 * modification, are permitted provided that the following conditions 68 * are met: 69 * 1. Redistributions of source code must retain the above copyright 70 * notice, this list of conditions and the following disclaimer. 71 * 2. Redistributions in binary form must reproduce the above copyright 72 * notice, this list of conditions and the following disclaimer in the 73 * documentation and/or other materials provided with the distribution. 74 * 3. Neither the name of the University nor the names of its contributors 75 * may be used to endorse or promote products derived from this software 76 * without specific prior written permission. 77 * 78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 88 * SUCH DAMAGE. 89 * 90 * @(#)route.c 8.3 (Berkeley) 1/9/95 91 */ 92 93 #if defined(_KERNEL) && defined(_KERNEL_OPT) 94 #include "opt_route.h" 95 #endif /* _KERNEL && _KERNEL_OPT */ 96 97 #include <sys/cdefs.h> 98 __KERNEL_RCSID(0, "$NetBSD: rtbl.c,v 1.6 2017/01/11 13:08:29 ozaki-r Exp $"); 99 100 #include <sys/param.h> 101 #include <sys/kmem.h> 102 #include <sys/sysctl.h> 103 #include <sys/systm.h> 104 #include <sys/callout.h> 105 #include <sys/proc.h> 106 #include <sys/mbuf.h> 107 #include <sys/socket.h> 108 #include <sys/socketvar.h> 109 #include <sys/domain.h> 110 #include <sys/kernel.h> 111 #include <sys/ioctl.h> 112 #include <sys/pool.h> 113 #include <sys/kauth.h> 114 115 #include <net/if.h> 116 #include <net/if_dl.h> 117 #include <net/route.h> 118 #include <net/raw_cb.h> 119 120 static rtbl_t *rt_tables[AF_MAX+1]; 121 122 int 123 rt_inithead(rtbl_t **tp, int off) 124 { 125 rtbl_t *t; 126 if (*tp != NULL) 127 return 1; 128 if ((t = kmem_alloc(sizeof(*t), KM_SLEEP)) == NULL) 129 return 0; 130 *tp = t; 131 return rn_inithead0(&t->t_rnh, off); 132 } 133 134 struct rtentry * 135 rt_matchaddr(rtbl_t *t, const struct sockaddr *dst) 136 { 137 struct radix_node_head *rnh = &t->t_rnh; 138 struct radix_node *rn; 139 140 rn = rnh->rnh_matchaddr(dst, rnh); 141 if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0) 142 return NULL; 143 return (struct rtentry *)rn; 144 } 145 146 int 147 rt_addaddr(rtbl_t *t, struct rtentry *rt, const struct sockaddr *netmask) 148 { 149 struct radix_node_head *rnh = &t->t_rnh; 150 struct radix_node *rn; 151 152 rn = rnh->rnh_addaddr(rt_getkey(rt), netmask, rnh, rt->rt_nodes); 153 154 return (rn == NULL) ? EEXIST : 0; 155 } 156 157 struct rtentry * 158 rt_lookup(rtbl_t *t, const struct sockaddr *dst, const struct sockaddr *netmask) 159 { 160 struct radix_node_head *rnh = &t->t_rnh; 161 struct radix_node *rn; 162 163 rn = rnh->rnh_lookup(dst, netmask, rnh); 164 if (rn == NULL || (rn->rn_flags & RNF_ROOT) != 0) 165 return NULL; 166 return (struct rtentry *)rn; 167 } 168 169 struct rtentry * 170 rt_deladdr(rtbl_t *t, const struct sockaddr *dst, 171 const struct sockaddr *netmask) 172 { 173 struct radix_node_head *rnh = &t->t_rnh; 174 struct radix_node *rn; 175 176 if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL) 177 return NULL; 178 if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 179 panic("%s", __func__); 180 return (struct rtentry *)rn; 181 } 182 183 static int 184 rt_walktree_visitor(struct radix_node *rn, void *v) 185 { 186 struct rtwalk *rw = (struct rtwalk *)v; 187 188 return (*rw->rw_f)((struct rtentry *)rn, rw->rw_v); 189 } 190 191 int 192 rtbl_walktree(sa_family_t family, int (*f)(struct rtentry *, void *), void *v) 193 { 194 rtbl_t *t = rt_tables[family]; 195 struct rtwalk rw; 196 197 if (t == NULL) 198 return 0; 199 200 rw.rw_f = f; 201 rw.rw_v = v; 202 203 return rn_walktree(&t->t_rnh, rt_walktree_visitor, &rw); 204 } 205 206 struct rtentry * 207 rtbl_search_matched_entry(sa_family_t family, 208 int (*f)(struct rtentry *, void *), void *v) 209 { 210 rtbl_t *t = rt_tables[family]; 211 struct rtwalk rw; 212 213 if (t == NULL) 214 return 0; 215 216 rw.rw_f = f; 217 rw.rw_v = v; 218 219 return (struct rtentry *) 220 rn_search_matched(&t->t_rnh, rt_walktree_visitor, &rw); 221 } 222 223 rtbl_t * 224 rt_gettable(sa_family_t af) 225 { 226 if (af >= __arraycount(rt_tables)) 227 return NULL; 228 return rt_tables[af]; 229 } 230 231 void 232 rtbl_init(void) 233 { 234 struct domain *dom; 235 DOMAIN_FOREACH(dom) 236 if (dom->dom_rtattach) 237 dom->dom_rtattach(&rt_tables[dom->dom_family], 238 dom->dom_rtoffset); 239 } 240 241 void 242 rt_assert_inactive(const struct rtentry *rt) 243 { 244 if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) 245 panic ("rtfree 2"); 246 } 247 248 int 249 rt_refines(const struct sockaddr *m_sa, const struct sockaddr *n_sa) 250 { 251 252 return rn_refines(m_sa, n_sa); 253 } 254