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