route.c revision 1.68 1 1.68 christos /* $NetBSD: route.c,v 1.68 2006/04/10 19:06:37 christos Exp $ */
2 1.18 kml
3 1.18 kml /*-
4 1.18 kml * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.18 kml * All rights reserved.
6 1.18 kml *
7 1.18 kml * This code is derived from software contributed to The NetBSD Foundation
8 1.18 kml * by Kevin M. Lahey of the Numerical Aerospace Simulation Facility,
9 1.18 kml * NASA Ames Research Center.
10 1.18 kml *
11 1.18 kml * Redistribution and use in source and binary forms, with or without
12 1.18 kml * modification, are permitted provided that the following conditions
13 1.18 kml * are met:
14 1.18 kml * 1. Redistributions of source code must retain the above copyright
15 1.18 kml * notice, this list of conditions and the following disclaimer.
16 1.18 kml * 2. Redistributions in binary form must reproduce the above copyright
17 1.18 kml * notice, this list of conditions and the following disclaimer in the
18 1.18 kml * documentation and/or other materials provided with the distribution.
19 1.18 kml * 3. All advertising materials mentioning features or use of this software
20 1.18 kml * must display the following acknowledgement:
21 1.18 kml * This product includes software developed by the NetBSD
22 1.18 kml * Foundation, Inc. and its contributors.
23 1.18 kml * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.18 kml * contributors may be used to endorse or promote products derived
25 1.18 kml * from this software without specific prior written permission.
26 1.18 kml *
27 1.18 kml * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.18 kml * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.18 kml * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.18 kml * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.18 kml * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.18 kml * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.18 kml * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.18 kml * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.18 kml * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.18 kml * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.18 kml * POSSIBILITY OF SUCH DAMAGE.
38 1.18 kml */
39 1.11 cgd
40 1.1 cgd /*
41 1.25 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
42 1.25 itojun * All rights reserved.
43 1.65 perry *
44 1.25 itojun * Redistribution and use in source and binary forms, with or without
45 1.25 itojun * modification, are permitted provided that the following conditions
46 1.25 itojun * are met:
47 1.25 itojun * 1. Redistributions of source code must retain the above copyright
48 1.25 itojun * notice, this list of conditions and the following disclaimer.
49 1.25 itojun * 2. Redistributions in binary form must reproduce the above copyright
50 1.25 itojun * notice, this list of conditions and the following disclaimer in the
51 1.25 itojun * documentation and/or other materials provided with the distribution.
52 1.25 itojun * 3. Neither the name of the project nor the names of its contributors
53 1.25 itojun * may be used to endorse or promote products derived from this software
54 1.25 itojun * without specific prior written permission.
55 1.65 perry *
56 1.25 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
57 1.25 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 1.25 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 1.25 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
60 1.25 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 1.25 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 1.25 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 1.25 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 1.25 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 1.25 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 1.25 itojun * SUCH DAMAGE.
67 1.25 itojun */
68 1.25 itojun
69 1.25 itojun /*
70 1.10 mycroft * Copyright (c) 1980, 1986, 1991, 1993
71 1.10 mycroft * The Regents of the University of California. All rights reserved.
72 1.1 cgd *
73 1.1 cgd * Redistribution and use in source and binary forms, with or without
74 1.1 cgd * modification, are permitted provided that the following conditions
75 1.1 cgd * are met:
76 1.1 cgd * 1. Redistributions of source code must retain the above copyright
77 1.1 cgd * notice, this list of conditions and the following disclaimer.
78 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
79 1.1 cgd * notice, this list of conditions and the following disclaimer in the
80 1.1 cgd * documentation and/or other materials provided with the distribution.
81 1.58 agc * 3. Neither the name of the University nor the names of its contributors
82 1.1 cgd * may be used to endorse or promote products derived from this software
83 1.1 cgd * without specific prior written permission.
84 1.1 cgd *
85 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 1.1 cgd * SUCH DAMAGE.
96 1.1 cgd *
97 1.17 christos * @(#)route.c 8.3 (Berkeley) 1/9/95
98 1.1 cgd */
99 1.50 lukem
100 1.50 lukem #include <sys/cdefs.h>
101 1.68 christos __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.68 2006/04/10 19:06:37 christos Exp $");
102 1.19 jonathan
103 1.19 jonathan #include "opt_ns.h"
104 1.2 cgd
105 1.5 mycroft #include <sys/param.h>
106 1.5 mycroft #include <sys/systm.h>
107 1.35 thorpej #include <sys/callout.h>
108 1.5 mycroft #include <sys/proc.h>
109 1.5 mycroft #include <sys/mbuf.h>
110 1.5 mycroft #include <sys/socket.h>
111 1.5 mycroft #include <sys/socketvar.h>
112 1.5 mycroft #include <sys/domain.h>
113 1.5 mycroft #include <sys/protosw.h>
114 1.18 kml #include <sys/kernel.h>
115 1.5 mycroft #include <sys/ioctl.h>
116 1.22 thorpej #include <sys/pool.h>
117 1.1 cgd
118 1.5 mycroft #include <net/if.h>
119 1.5 mycroft #include <net/route.h>
120 1.5 mycroft #include <net/raw_cb.h>
121 1.1 cgd
122 1.5 mycroft #include <netinet/in.h>
123 1.5 mycroft #include <netinet/in_var.h>
124 1.1 cgd
125 1.1 cgd #ifdef NS
126 1.5 mycroft #include <netns/ns.h>
127 1.1 cgd #endif
128 1.5 mycroft
129 1.52 matt struct route_cb route_cb;
130 1.52 matt struct rtstat rtstat;
131 1.52 matt struct radix_node_head *rt_tables[AF_MAX+1];
132 1.1 cgd
133 1.1 cgd int rttrash; /* routes not in table but not freed */
134 1.1 cgd struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
135 1.1 cgd
136 1.61 simonb POOL_INIT(rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl", NULL);
137 1.61 simonb POOL_INIT(rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl", NULL);
138 1.22 thorpej
139 1.35 thorpej struct callout rt_timer_ch; /* callout for rt_timer_timer() */
140 1.35 thorpej
141 1.60 matt static int rtdeletemsg(struct rtentry *);
142 1.60 matt static int rtflushclone1(struct radix_node *, void *);
143 1.60 matt static void rtflushclone(struct radix_node_head *, struct rtentry *);
144 1.40 itojun
145 1.10 mycroft void
146 1.60 matt rtable_init(void **table)
147 1.10 mycroft {
148 1.10 mycroft struct domain *dom;
149 1.64 matt DOMAIN_FOREACH(dom)
150 1.10 mycroft if (dom->dom_rtattach)
151 1.10 mycroft dom->dom_rtattach(&table[dom->dom_family],
152 1.10 mycroft dom->dom_rtoffset);
153 1.10 mycroft }
154 1.1 cgd
155 1.9 mycroft void
156 1.60 matt route_init(void)
157 1.1 cgd {
158 1.22 thorpej
159 1.10 mycroft rn_init(); /* initialize all zeroes, all ones, mask table */
160 1.10 mycroft rtable_init((void **)rt_tables);
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd /*
164 1.1 cgd * Packet routing routines.
165 1.1 cgd */
166 1.9 mycroft void
167 1.60 matt rtalloc(struct route *ro)
168 1.1 cgd {
169 1.1 cgd if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
170 1.1 cgd return; /* XXX */
171 1.1 cgd ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
172 1.1 cgd }
173 1.25 itojun
174 1.1 cgd struct rtentry *
175 1.60 matt rtalloc1(const struct sockaddr *dst, int report)
176 1.1 cgd {
177 1.36 augustss struct radix_node_head *rnh = rt_tables[dst->sa_family];
178 1.36 augustss struct rtentry *rt;
179 1.36 augustss struct radix_node *rn;
180 1.68 christos struct rtentry *newrt = NULL;
181 1.10 mycroft struct rt_addrinfo info;
182 1.13 mycroft int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
183 1.1 cgd
184 1.66 christos if (rnh && (rn = rnh->rnh_matchaddr(dst, rnh)) &&
185 1.1 cgd ((rn->rn_flags & RNF_ROOT) == 0)) {
186 1.1 cgd newrt = rt = (struct rtentry *)rn;
187 1.1 cgd if (report && (rt->rt_flags & RTF_CLONING)) {
188 1.68 christos err = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
189 1.68 christos &newrt);
190 1.8 cgd if (err) {
191 1.8 cgd newrt = rt;
192 1.8 cgd rt->rt_refcnt++;
193 1.8 cgd goto miss;
194 1.8 cgd }
195 1.8 cgd if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
196 1.8 cgd msgtype = RTM_RESOLVE;
197 1.8 cgd goto miss;
198 1.8 cgd }
199 1.39 itojun /* Inform listeners of the new route */
200 1.44 thorpej memset(&info, 0, sizeof(info));
201 1.39 itojun info.rti_info[RTAX_DST] = rt_key(rt);
202 1.39 itojun info.rti_info[RTAX_NETMASK] = rt_mask(rt);
203 1.39 itojun info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
204 1.39 itojun if (rt->rt_ifp != NULL) {
205 1.65 perry info.rti_info[RTAX_IFP] =
206 1.49 matt TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
207 1.39 itojun info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
208 1.39 itojun }
209 1.39 itojun rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
210 1.1 cgd } else
211 1.1 cgd rt->rt_refcnt++;
212 1.1 cgd } else {
213 1.1 cgd rtstat.rts_unreach++;
214 1.10 mycroft miss: if (report) {
215 1.44 thorpej memset((caddr_t)&info, 0, sizeof(info));
216 1.10 mycroft info.rti_info[RTAX_DST] = dst;
217 1.10 mycroft rt_missmsg(msgtype, &info, 0, err);
218 1.10 mycroft }
219 1.1 cgd }
220 1.1 cgd splx(s);
221 1.1 cgd return (newrt);
222 1.1 cgd }
223 1.1 cgd
224 1.9 mycroft void
225 1.60 matt rtfree(struct rtentry *rt)
226 1.1 cgd {
227 1.36 augustss struct ifaddr *ifa;
228 1.10 mycroft
229 1.68 christos if (rt == NULL)
230 1.1 cgd panic("rtfree");
231 1.1 cgd rt->rt_refcnt--;
232 1.1 cgd if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
233 1.1 cgd if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
234 1.1 cgd panic ("rtfree 2");
235 1.10 mycroft rttrash--;
236 1.10 mycroft if (rt->rt_refcnt < 0) {
237 1.16 christos printf("rtfree: %p not freed (neg refs)\n", rt);
238 1.10 mycroft return;
239 1.10 mycroft }
240 1.54 itojun rt_timer_remove_all(rt, 0);
241 1.10 mycroft ifa = rt->rt_ifa;
242 1.10 mycroft IFAFREE(ifa);
243 1.10 mycroft Free(rt_key(rt));
244 1.22 thorpej pool_put(&rtentry_pool, rt);
245 1.1 cgd }
246 1.1 cgd }
247 1.1 cgd
248 1.10 mycroft void
249 1.60 matt ifafree(struct ifaddr *ifa)
250 1.10 mycroft {
251 1.30 thorpej
252 1.30 thorpej #ifdef DIAGNOSTIC
253 1.10 mycroft if (ifa == NULL)
254 1.30 thorpej panic("ifafree: null ifa");
255 1.30 thorpej if (ifa->ifa_refcnt != 0)
256 1.30 thorpej panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
257 1.30 thorpej #endif
258 1.31 thorpej #ifdef IFAREF_DEBUG
259 1.31 thorpej printf("ifafree: freeing ifaddr %p\n", ifa);
260 1.31 thorpej #endif
261 1.30 thorpej free(ifa, M_IFADDR);
262 1.10 mycroft }
263 1.10 mycroft
264 1.1 cgd /*
265 1.1 cgd * Force a routing table entry to the specified
266 1.1 cgd * destination to go through the given gateway.
267 1.1 cgd * Normally called as a result of a routing redirect
268 1.1 cgd * message from the network layer.
269 1.1 cgd *
270 1.13 mycroft * N.B.: must be called at splsoftnet
271 1.1 cgd */
272 1.14 christos void
273 1.60 matt rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
274 1.60 matt const struct sockaddr *netmask, int flags, const struct sockaddr *src,
275 1.60 matt struct rtentry **rtp)
276 1.1 cgd {
277 1.36 augustss struct rtentry *rt;
278 1.1 cgd int error = 0;
279 1.68 christos u_quad_t *stat = NULL;
280 1.10 mycroft struct rt_addrinfo info;
281 1.10 mycroft struct ifaddr *ifa;
282 1.1 cgd
283 1.1 cgd /* verify the gateway is directly reachable */
284 1.68 christos if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
285 1.1 cgd error = ENETUNREACH;
286 1.8 cgd goto out;
287 1.1 cgd }
288 1.1 cgd rt = rtalloc1(dst, 0);
289 1.1 cgd /*
290 1.1 cgd * If the redirect isn't from our current router for this dst,
291 1.1 cgd * it's either old or wrong. If it redirects us to ourselves,
292 1.1 cgd * we have a routing loop, perhaps as a result of an interface
293 1.1 cgd * going down recently.
294 1.1 cgd */
295 1.45 itojun #define equal(a1, a2) \
296 1.45 itojun ((a1)->sa_len == (a2)->sa_len && \
297 1.66 christos memcmp((a1), (a2), (a1)->sa_len) == 0)
298 1.10 mycroft if (!(flags & RTF_DONE) && rt &&
299 1.10 mycroft (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
300 1.1 cgd error = EINVAL;
301 1.1 cgd else if (ifa_ifwithaddr(gateway))
302 1.1 cgd error = EHOSTUNREACH;
303 1.1 cgd if (error)
304 1.1 cgd goto done;
305 1.1 cgd /*
306 1.1 cgd * Create a new entry if we just got back a wildcard entry
307 1.33 soren * or the lookup failed. This is necessary for hosts
308 1.1 cgd * which use routing redirects generated by smart gateways
309 1.1 cgd * to dynamically build the routing tables.
310 1.1 cgd */
311 1.68 christos if ((rt == NULL) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
312 1.1 cgd goto create;
313 1.1 cgd /*
314 1.1 cgd * Don't listen to the redirect if it's
315 1.65 perry * for a route to an interface.
316 1.1 cgd */
317 1.1 cgd if (rt->rt_flags & RTF_GATEWAY) {
318 1.1 cgd if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
319 1.1 cgd /*
320 1.1 cgd * Changing from route to net => route to host.
321 1.1 cgd * Create new route, rather than smashing route to net.
322 1.1 cgd */
323 1.1 cgd create:
324 1.39 itojun if (rt)
325 1.39 itojun rtfree(rt);
326 1.1 cgd flags |= RTF_GATEWAY | RTF_DYNAMIC;
327 1.39 itojun info.rti_info[RTAX_DST] = dst;
328 1.39 itojun info.rti_info[RTAX_GATEWAY] = gateway;
329 1.39 itojun info.rti_info[RTAX_NETMASK] = netmask;
330 1.39 itojun info.rti_ifa = ifa;
331 1.39 itojun info.rti_flags = flags;
332 1.39 itojun rt = NULL;
333 1.39 itojun error = rtrequest1(RTM_ADD, &info, &rt);
334 1.39 itojun if (rt != NULL)
335 1.39 itojun flags = rt->rt_flags;
336 1.1 cgd stat = &rtstat.rts_dynamic;
337 1.1 cgd } else {
338 1.1 cgd /*
339 1.1 cgd * Smash the current notion of the gateway to
340 1.1 cgd * this destination. Should check about netmask!!!
341 1.1 cgd */
342 1.10 mycroft rt->rt_flags |= RTF_MODIFIED;
343 1.10 mycroft flags |= RTF_MODIFIED;
344 1.10 mycroft stat = &rtstat.rts_newgateway;
345 1.10 mycroft rt_setgate(rt, rt_key(rt), gateway);
346 1.1 cgd }
347 1.1 cgd } else
348 1.1 cgd error = EHOSTUNREACH;
349 1.1 cgd done:
350 1.1 cgd if (rt) {
351 1.1 cgd if (rtp && !error)
352 1.1 cgd *rtp = rt;
353 1.1 cgd else
354 1.1 cgd rtfree(rt);
355 1.1 cgd }
356 1.8 cgd out:
357 1.1 cgd if (error)
358 1.1 cgd rtstat.rts_badredirect++;
359 1.8 cgd else if (stat != NULL)
360 1.8 cgd (*stat)++;
361 1.44 thorpej memset((caddr_t)&info, 0, sizeof(info));
362 1.10 mycroft info.rti_info[RTAX_DST] = dst;
363 1.10 mycroft info.rti_info[RTAX_GATEWAY] = gateway;
364 1.10 mycroft info.rti_info[RTAX_NETMASK] = netmask;
365 1.10 mycroft info.rti_info[RTAX_AUTHOR] = src;
366 1.10 mycroft rt_missmsg(RTM_REDIRECT, &info, flags, error);
367 1.1 cgd }
368 1.1 cgd
369 1.1 cgd /*
370 1.40 itojun * Delete a route and generate a message
371 1.40 itojun */
372 1.40 itojun static int
373 1.60 matt rtdeletemsg(struct rtentry *rt)
374 1.40 itojun {
375 1.40 itojun int error;
376 1.40 itojun struct rt_addrinfo info;
377 1.40 itojun
378 1.40 itojun /*
379 1.40 itojun * Request the new route so that the entry is not actually
380 1.40 itojun * deleted. That will allow the information being reported to
381 1.40 itojun * be accurate (and consistent with route_output()).
382 1.40 itojun */
383 1.44 thorpej memset((caddr_t)&info, 0, sizeof(info));
384 1.40 itojun info.rti_info[RTAX_DST] = rt_key(rt);
385 1.40 itojun info.rti_info[RTAX_NETMASK] = rt_mask(rt);
386 1.40 itojun info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
387 1.40 itojun info.rti_flags = rt->rt_flags;
388 1.40 itojun error = rtrequest1(RTM_DELETE, &info, &rt);
389 1.40 itojun
390 1.40 itojun rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
391 1.40 itojun
392 1.40 itojun /* Adjust the refcount */
393 1.40 itojun if (error == 0 && rt->rt_refcnt <= 0) {
394 1.40 itojun rt->rt_refcnt++;
395 1.40 itojun rtfree(rt);
396 1.40 itojun }
397 1.40 itojun return (error);
398 1.40 itojun }
399 1.40 itojun
400 1.41 itojun static int
401 1.60 matt rtflushclone1(struct radix_node *rn, void *arg)
402 1.41 itojun {
403 1.41 itojun struct rtentry *rt, *parent;
404 1.41 itojun
405 1.41 itojun rt = (struct rtentry *)rn;
406 1.41 itojun parent = (struct rtentry *)arg;
407 1.41 itojun if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
408 1.41 itojun rtdeletemsg(rt);
409 1.41 itojun return 0;
410 1.41 itojun }
411 1.41 itojun
412 1.41 itojun static void
413 1.60 matt rtflushclone(struct radix_node_head *rnh, struct rtentry *parent)
414 1.41 itojun {
415 1.41 itojun
416 1.41 itojun #ifdef DIAGNOSTIC
417 1.41 itojun if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
418 1.41 itojun panic("rtflushclone: called with a non-cloning route");
419 1.41 itojun if (!rnh->rnh_walktree)
420 1.41 itojun panic("rtflushclone: no rnh_walktree");
421 1.41 itojun #endif
422 1.42 itojun rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
423 1.41 itojun }
424 1.41 itojun
425 1.40 itojun /*
426 1.22 thorpej * Routing table ioctl interface.
427 1.22 thorpej */
428 1.9 mycroft int
429 1.67 christos rtioctl(u_long req, caddr_t data, struct lwp *l)
430 1.1 cgd {
431 1.1 cgd return (EOPNOTSUPP);
432 1.1 cgd }
433 1.1 cgd
434 1.1 cgd struct ifaddr *
435 1.60 matt ifa_ifwithroute(int flags, const struct sockaddr *dst,
436 1.60 matt const struct sockaddr *gateway)
437 1.1 cgd {
438 1.36 augustss struct ifaddr *ifa;
439 1.1 cgd if ((flags & RTF_GATEWAY) == 0) {
440 1.1 cgd /*
441 1.1 cgd * If we are adding a route to an interface,
442 1.1 cgd * and the interface is a pt to pt link
443 1.1 cgd * we should search for the destination
444 1.1 cgd * as our clue to the interface. Otherwise
445 1.1 cgd * we can use the local address.
446 1.1 cgd */
447 1.68 christos ifa = NULL;
448 1.65 perry if (flags & RTF_HOST)
449 1.1 cgd ifa = ifa_ifwithdstaddr(dst);
450 1.68 christos if (ifa == NULL)
451 1.1 cgd ifa = ifa_ifwithaddr(gateway);
452 1.1 cgd } else {
453 1.1 cgd /*
454 1.1 cgd * If we are adding a route to a remote net
455 1.1 cgd * or host, the gateway may still be on the
456 1.1 cgd * other end of a pt to pt link.
457 1.1 cgd */
458 1.1 cgd ifa = ifa_ifwithdstaddr(gateway);
459 1.1 cgd }
460 1.68 christos if (ifa == NULL)
461 1.1 cgd ifa = ifa_ifwithnet(gateway);
462 1.68 christos if (ifa == NULL) {
463 1.1 cgd struct rtentry *rt = rtalloc1(dst, 0);
464 1.68 christos if (rt == NULL)
465 1.68 christos return NULL;
466 1.1 cgd rt->rt_refcnt--;
467 1.68 christos if ((ifa = rt->rt_ifa) == NULL)
468 1.68 christos return NULL;
469 1.1 cgd }
470 1.1 cgd if (ifa->ifa_addr->sa_family != dst->sa_family) {
471 1.10 mycroft struct ifaddr *oifa = ifa;
472 1.1 cgd ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
473 1.1 cgd if (ifa == 0)
474 1.1 cgd ifa = oifa;
475 1.1 cgd }
476 1.1 cgd return (ifa);
477 1.1 cgd }
478 1.1 cgd
479 1.1 cgd #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
480 1.1 cgd
481 1.9 mycroft int
482 1.60 matt rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
483 1.60 matt const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
484 1.1 cgd {
485 1.39 itojun struct rt_addrinfo info;
486 1.39 itojun
487 1.44 thorpej memset(&info, 0, sizeof(info));
488 1.39 itojun info.rti_flags = flags;
489 1.39 itojun info.rti_info[RTAX_DST] = dst;
490 1.39 itojun info.rti_info[RTAX_GATEWAY] = gateway;
491 1.39 itojun info.rti_info[RTAX_NETMASK] = netmask;
492 1.39 itojun return rtrequest1(req, &info, ret_nrt);
493 1.39 itojun }
494 1.39 itojun
495 1.39 itojun int
496 1.60 matt rt_getifa(struct rt_addrinfo *info)
497 1.39 itojun {
498 1.39 itojun struct ifaddr *ifa;
499 1.39 itojun int error = 0;
500 1.68 christos const struct sockaddr *dst = info->rti_info[RTAX_DST];
501 1.68 christos const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
502 1.68 christos const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
503 1.68 christos const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
504 1.68 christos int flags = info->rti_flags;
505 1.39 itojun
506 1.39 itojun /*
507 1.39 itojun * ifp may be specified by sockaddr_dl when protocol address
508 1.39 itojun * is ambiguous
509 1.39 itojun */
510 1.39 itojun if (info->rti_ifp == NULL && ifpaddr != NULL
511 1.39 itojun && ifpaddr->sa_family == AF_LINK &&
512 1.59 matt (ifa = ifa_ifwithnet((const struct sockaddr *)ifpaddr)) != NULL)
513 1.39 itojun info->rti_ifp = ifa->ifa_ifp;
514 1.39 itojun if (info->rti_ifa == NULL && ifaaddr != NULL)
515 1.39 itojun info->rti_ifa = ifa_ifwithaddr(ifaaddr);
516 1.39 itojun if (info->rti_ifa == NULL) {
517 1.59 matt const struct sockaddr *sa;
518 1.39 itojun
519 1.39 itojun sa = ifaaddr != NULL ? ifaaddr :
520 1.39 itojun (gateway != NULL ? gateway : dst);
521 1.39 itojun if (sa != NULL && info->rti_ifp != NULL)
522 1.39 itojun info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
523 1.39 itojun else if (dst != NULL && gateway != NULL)
524 1.39 itojun info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
525 1.39 itojun else if (sa != NULL)
526 1.39 itojun info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
527 1.39 itojun }
528 1.39 itojun if ((ifa = info->rti_ifa) != NULL) {
529 1.39 itojun if (info->rti_ifp == NULL)
530 1.39 itojun info->rti_ifp = ifa->ifa_ifp;
531 1.39 itojun } else
532 1.39 itojun error = ENETUNREACH;
533 1.39 itojun return (error);
534 1.39 itojun }
535 1.39 itojun
536 1.39 itojun int
537 1.60 matt rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
538 1.39 itojun {
539 1.60 matt int s = splsoftnet();
540 1.60 matt int error = 0;
541 1.40 itojun struct rtentry *rt, *crt;
542 1.36 augustss struct radix_node *rn;
543 1.36 augustss struct radix_node_head *rnh;
544 1.10 mycroft struct ifaddr *ifa;
545 1.1 cgd struct sockaddr *ndst;
546 1.63 christos struct sockaddr_storage deldst;
547 1.68 christos const struct sockaddr *dst = info->rti_info[RTAX_DST];
548 1.68 christos const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
549 1.68 christos const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
550 1.68 christos int flags = info->rti_flags;
551 1.1 cgd #define senderr(x) { error = x ; goto bad; }
552 1.1 cgd
553 1.68 christos if ((rnh = rt_tables[dst->sa_family]) == NULL)
554 1.1 cgd senderr(ESRCH);
555 1.1 cgd if (flags & RTF_HOST)
556 1.68 christos netmask = NULL;
557 1.1 cgd switch (req) {
558 1.1 cgd case RTM_DELETE:
559 1.63 christos if (netmask) {
560 1.63 christos rt_maskedcopy(dst, (struct sockaddr *)&deldst, netmask);
561 1.63 christos dst = (struct sockaddr *)&deldst;
562 1.63 christos }
563 1.68 christos if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL)
564 1.41 itojun senderr(ESRCH);
565 1.41 itojun rt = (struct rtentry *)rn;
566 1.41 itojun if ((rt->rt_flags & RTF_CLONING) != 0) {
567 1.41 itojun /* clean up any cloned children */
568 1.41 itojun rtflushclone(rnh, rt);
569 1.41 itojun }
570 1.68 christos if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL)
571 1.1 cgd senderr(ESRCH);
572 1.1 cgd if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
573 1.1 cgd panic ("rtrequest delete");
574 1.1 cgd rt = (struct rtentry *)rn;
575 1.10 mycroft if (rt->rt_gwroute) {
576 1.68 christos RTFREE(rt->rt_gwroute);
577 1.68 christos rt->rt_gwroute = NULL;
578 1.48 itojun }
579 1.48 itojun if (rt->rt_parent) {
580 1.48 itojun rt->rt_parent->rt_refcnt--;
581 1.48 itojun rt->rt_parent = NULL;
582 1.10 mycroft }
583 1.28 erh rt->rt_flags &= ~RTF_UP;
584 1.1 cgd if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
585 1.39 itojun ifa->ifa_rtrequest(RTM_DELETE, rt, info);
586 1.1 cgd rttrash++;
587 1.10 mycroft if (ret_nrt)
588 1.10 mycroft *ret_nrt = rt;
589 1.10 mycroft else if (rt->rt_refcnt <= 0) {
590 1.10 mycroft rt->rt_refcnt++;
591 1.1 cgd rtfree(rt);
592 1.10 mycroft }
593 1.1 cgd break;
594 1.1 cgd
595 1.1 cgd case RTM_RESOLVE:
596 1.68 christos if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
597 1.1 cgd senderr(EINVAL);
598 1.40 itojun if ((rt->rt_flags & RTF_CLONING) == 0)
599 1.40 itojun senderr(EINVAL);
600 1.1 cgd ifa = rt->rt_ifa;
601 1.40 itojun flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
602 1.40 itojun flags |= RTF_CLONED;
603 1.1 cgd gateway = rt->rt_gateway;
604 1.68 christos if ((netmask = rt->rt_genmask) == NULL)
605 1.1 cgd flags |= RTF_HOST;
606 1.1 cgd goto makeroute;
607 1.1 cgd
608 1.1 cgd case RTM_ADD:
609 1.68 christos if (info->rti_ifa == NULL && (error = rt_getifa(info)))
610 1.39 itojun senderr(error);
611 1.39 itojun ifa = info->rti_ifa;
612 1.1 cgd makeroute:
613 1.22 thorpej rt = pool_get(&rtentry_pool, PR_NOWAIT);
614 1.68 christos if (rt == NULL)
615 1.1 cgd senderr(ENOBUFS);
616 1.10 mycroft Bzero(rt, sizeof(*rt));
617 1.10 mycroft rt->rt_flags = RTF_UP | flags;
618 1.18 kml LIST_INIT(&rt->rt_timer);
619 1.10 mycroft if (rt_setgate(rt, dst, gateway)) {
620 1.22 thorpej pool_put(&rtentry_pool, rt);
621 1.10 mycroft senderr(ENOBUFS);
622 1.10 mycroft }
623 1.10 mycroft ndst = rt_key(rt);
624 1.1 cgd if (netmask) {
625 1.1 cgd rt_maskedcopy(dst, ndst, netmask);
626 1.1 cgd } else
627 1.1 cgd Bcopy(dst, ndst, dst->sa_len);
628 1.30 thorpej IFAREF(ifa);
629 1.1 cgd rt->rt_ifa = ifa;
630 1.1 cgd rt->rt_ifp = ifa->ifa_ifp;
631 1.27 matt if (req == RTM_RESOLVE) {
632 1.1 cgd rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
633 1.41 itojun rt->rt_parent = *ret_nrt;
634 1.41 itojun rt->rt_parent->rt_refcnt++;
635 1.40 itojun }
636 1.66 christos rn = rnh->rnh_addaddr(ndst, netmask, rnh, rt->rt_nodes);
637 1.40 itojun if (rn == NULL && (crt = rtalloc1(ndst, 0)) != NULL) {
638 1.40 itojun /* overwrite cloned route */
639 1.40 itojun if ((crt->rt_flags & RTF_CLONED) != 0) {
640 1.40 itojun rtdeletemsg(crt);
641 1.66 christos rn = rnh->rnh_addaddr(ndst,
642 1.66 christos netmask, rnh, rt->rt_nodes);
643 1.40 itojun }
644 1.40 itojun RTFREE(crt);
645 1.40 itojun }
646 1.68 christos if (rn == NULL) {
647 1.40 itojun IFAFREE(ifa);
648 1.41 itojun if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
649 1.41 itojun rtfree(rt->rt_parent);
650 1.40 itojun if (rt->rt_gwroute)
651 1.40 itojun rtfree(rt->rt_gwroute);
652 1.40 itojun Free(rt_key(rt));
653 1.40 itojun pool_put(&rtentry_pool, rt);
654 1.40 itojun senderr(EEXIST);
655 1.27 matt }
656 1.1 cgd if (ifa->ifa_rtrequest)
657 1.39 itojun ifa->ifa_rtrequest(req, rt, info);
658 1.1 cgd if (ret_nrt) {
659 1.1 cgd *ret_nrt = rt;
660 1.1 cgd rt->rt_refcnt++;
661 1.41 itojun }
662 1.41 itojun if ((rt->rt_flags & RTF_CLONING) != 0) {
663 1.41 itojun /* clean up any cloned children */
664 1.41 itojun rtflushclone(rnh, rt);
665 1.1 cgd }
666 1.1 cgd break;
667 1.1 cgd }
668 1.1 cgd bad:
669 1.1 cgd splx(s);
670 1.1 cgd return (error);
671 1.1 cgd }
672 1.1 cgd
673 1.10 mycroft int
674 1.60 matt rt_setgate( struct rtentry *rt0, const struct sockaddr *dst,
675 1.60 matt const struct sockaddr *gate)
676 1.10 mycroft {
677 1.59 matt char *new, *old;
678 1.53 thorpej u_int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
679 1.36 augustss struct rtentry *rt = rt0;
680 1.10 mycroft
681 1.68 christos if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
682 1.10 mycroft old = (caddr_t)rt_key(rt);
683 1.10 mycroft R_Malloc(new, caddr_t, dlen + glen);
684 1.68 christos if (new == NULL)
685 1.10 mycroft return 1;
686 1.32 itojun Bzero(new, dlen + glen);
687 1.10 mycroft rt->rt_nodes->rn_key = new;
688 1.10 mycroft } else {
689 1.66 christos new = __UNCONST(rt->rt_nodes->rn_key); /*XXXUNCONST*/
690 1.68 christos old = NULL;
691 1.10 mycroft }
692 1.10 mycroft Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
693 1.10 mycroft if (old) {
694 1.10 mycroft Bcopy(dst, new, dlen);
695 1.10 mycroft Free(old);
696 1.10 mycroft }
697 1.10 mycroft if (rt->rt_gwroute) {
698 1.68 christos RTFREE(rt->rt_gwroute);
699 1.68 christos rt->rt_gwroute = NULL;
700 1.10 mycroft }
701 1.10 mycroft if (rt->rt_flags & RTF_GATEWAY) {
702 1.10 mycroft rt->rt_gwroute = rtalloc1(gate, 1);
703 1.27 matt /*
704 1.27 matt * If we switched gateways, grab the MTU from the new
705 1.47 itojun * gateway route if the current MTU, if the current MTU is
706 1.47 itojun * greater than the MTU of gateway.
707 1.47 itojun * Note that, if the MTU of gateway is 0, we will reset the
708 1.47 itojun * MTU of the route to run PMTUD again from scratch. XXX
709 1.27 matt */
710 1.27 matt if (rt->rt_gwroute
711 1.27 matt && !(rt->rt_rmx.rmx_locks & RTV_MTU)
712 1.47 itojun && rt->rt_rmx.rmx_mtu
713 1.47 itojun && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
714 1.27 matt rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
715 1.27 matt }
716 1.10 mycroft }
717 1.10 mycroft return 0;
718 1.10 mycroft }
719 1.10 mycroft
720 1.9 mycroft void
721 1.60 matt rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
722 1.60 matt const struct sockaddr *netmask)
723 1.1 cgd {
724 1.66 christos const u_char *cp1 = (const u_char *)src;
725 1.36 augustss u_char *cp2 = (u_char *)dst;
726 1.66 christos const u_char *cp3 = (const u_char *)netmask;
727 1.1 cgd u_char *cplim = cp2 + *cp3;
728 1.1 cgd u_char *cplim2 = cp2 + *cp1;
729 1.1 cgd
730 1.1 cgd *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
731 1.1 cgd cp3 += 2;
732 1.1 cgd if (cplim > cplim2)
733 1.1 cgd cplim = cplim2;
734 1.1 cgd while (cp2 < cplim)
735 1.1 cgd *cp2++ = *cp1++ & *cp3++;
736 1.1 cgd if (cp2 < cplim2)
737 1.66 christos memset(cp2, 0, (unsigned)(cplim2 - cp2));
738 1.1 cgd }
739 1.10 mycroft
740 1.1 cgd /*
741 1.29 sommerfe * Set up or tear down a routing table entry, normally
742 1.1 cgd * for an interface.
743 1.1 cgd */
744 1.9 mycroft int
745 1.60 matt rtinit(struct ifaddr *ifa, int cmd, int flags)
746 1.1 cgd {
747 1.36 augustss struct rtentry *rt;
748 1.36 augustss struct sockaddr *dst, *odst;
749 1.29 sommerfe struct sockaddr_storage deldst;
750 1.68 christos struct rtentry *nrt = NULL;
751 1.1 cgd int error;
752 1.39 itojun struct rt_addrinfo info;
753 1.1 cgd
754 1.1 cgd dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
755 1.1 cgd if (cmd == RTM_DELETE) {
756 1.1 cgd if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
757 1.29 sommerfe /* Delete subnet route for this interface */
758 1.29 sommerfe odst = dst;
759 1.29 sommerfe dst = (struct sockaddr *)&deldst;
760 1.29 sommerfe rt_maskedcopy(odst, dst, ifa->ifa_netmask);
761 1.1 cgd }
762 1.14 christos if ((rt = rtalloc1(dst, 0)) != NULL) {
763 1.1 cgd rt->rt_refcnt--;
764 1.29 sommerfe if (rt->rt_ifa != ifa)
765 1.1 cgd return (flags & RTF_HOST ? EHOSTUNREACH
766 1.1 cgd : ENETUNREACH);
767 1.1 cgd }
768 1.1 cgd }
769 1.44 thorpej memset(&info, 0, sizeof(info));
770 1.39 itojun info.rti_ifa = ifa;
771 1.39 itojun info.rti_flags = flags | ifa->ifa_flags;
772 1.39 itojun info.rti_info[RTAX_DST] = dst;
773 1.39 itojun info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
774 1.39 itojun /*
775 1.39 itojun * XXX here, it seems that we are assuming that ifa_netmask is NULL
776 1.39 itojun * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate
777 1.39 itojun * variable) when RTF_HOST is 1. still not sure if i can safely
778 1.39 itojun * change it to meet bsdi4 behavior.
779 1.39 itojun */
780 1.39 itojun info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
781 1.39 itojun error = rtrequest1(cmd, &info, &nrt);
782 1.10 mycroft if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
783 1.10 mycroft rt_newaddrmsg(cmd, ifa, error, nrt);
784 1.10 mycroft if (rt->rt_refcnt <= 0) {
785 1.10 mycroft rt->rt_refcnt++;
786 1.10 mycroft rtfree(rt);
787 1.10 mycroft }
788 1.10 mycroft }
789 1.10 mycroft if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
790 1.10 mycroft rt->rt_refcnt--;
791 1.10 mycroft if (rt->rt_ifa != ifa) {
792 1.17 christos printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
793 1.17 christos rt->rt_ifa);
794 1.10 mycroft if (rt->rt_ifa->ifa_rtrequest)
795 1.39 itojun rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
796 1.10 mycroft IFAFREE(rt->rt_ifa);
797 1.10 mycroft rt->rt_ifa = ifa;
798 1.10 mycroft rt->rt_ifp = ifa->ifa_ifp;
799 1.30 thorpej IFAREF(ifa);
800 1.10 mycroft if (ifa->ifa_rtrequest)
801 1.39 itojun ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
802 1.10 mycroft }
803 1.10 mycroft rt_newaddrmsg(cmd, ifa, error, nrt);
804 1.1 cgd }
805 1.1 cgd return (error);
806 1.18 kml }
807 1.18 kml
808 1.18 kml /*
809 1.18 kml * Route timer routines. These routes allow functions to be called
810 1.18 kml * for various routes at any time. This is useful in supporting
811 1.18 kml * path MTU discovery and redirect route deletion.
812 1.18 kml *
813 1.18 kml * This is similar to some BSDI internal functions, but it provides
814 1.18 kml * for multiple queues for efficiency's sake...
815 1.18 kml */
816 1.18 kml
817 1.18 kml LIST_HEAD(, rttimer_queue) rttimer_queue_head;
818 1.18 kml static int rt_init_done = 0;
819 1.18 kml
820 1.60 matt #define RTTIMER_CALLOUT(r) do { \
821 1.60 matt if (r->rtt_func != NULL) { \
822 1.60 matt (*r->rtt_func)(r->rtt_rt, r); \
823 1.60 matt } else { \
824 1.60 matt rtrequest((int) RTM_DELETE, \
825 1.60 matt (struct sockaddr *)rt_key(r->rtt_rt), \
826 1.60 matt 0, 0, 0, 0); \
827 1.60 matt } \
828 1.60 matt } while (/*CONSTCOND*/0)
829 1.18 kml
830 1.65 perry /*
831 1.18 kml * Some subtle order problems with domain initialization mean that
832 1.18 kml * we cannot count on this being run from rt_init before various
833 1.18 kml * protocol initializations are done. Therefore, we make sure
834 1.18 kml * that this is run when the first queue is added...
835 1.18 kml */
836 1.18 kml
837 1.65 perry void
838 1.60 matt rt_timer_init(void)
839 1.18 kml {
840 1.18 kml assert(rt_init_done == 0);
841 1.18 kml
842 1.18 kml LIST_INIT(&rttimer_queue_head);
843 1.35 thorpej callout_init(&rt_timer_ch);
844 1.35 thorpej callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
845 1.18 kml rt_init_done = 1;
846 1.18 kml }
847 1.18 kml
848 1.18 kml struct rttimer_queue *
849 1.60 matt rt_timer_queue_create(u_int timeout)
850 1.18 kml {
851 1.18 kml struct rttimer_queue *rtq;
852 1.18 kml
853 1.18 kml if (rt_init_done == 0)
854 1.18 kml rt_timer_init();
855 1.18 kml
856 1.18 kml R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
857 1.18 kml if (rtq == NULL)
858 1.65 perry return (NULL);
859 1.32 itojun Bzero(rtq, sizeof *rtq);
860 1.18 kml
861 1.18 kml rtq->rtq_timeout = timeout;
862 1.37 itojun rtq->rtq_count = 0;
863 1.24 thorpej TAILQ_INIT(&rtq->rtq_head);
864 1.18 kml LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
865 1.18 kml
866 1.24 thorpej return (rtq);
867 1.18 kml }
868 1.18 kml
869 1.18 kml void
870 1.60 matt rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
871 1.18 kml {
872 1.24 thorpej
873 1.18 kml rtq->rtq_timeout = timeout;
874 1.18 kml }
875 1.18 kml
876 1.18 kml void
877 1.60 matt rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
878 1.18 kml {
879 1.24 thorpej struct rttimer *r;
880 1.18 kml
881 1.24 thorpej while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
882 1.18 kml LIST_REMOVE(r, rtt_link);
883 1.24 thorpej TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
884 1.24 thorpej if (destroy)
885 1.18 kml RTTIMER_CALLOUT(r);
886 1.22 thorpej pool_put(&rttimer_pool, r);
887 1.37 itojun if (rtq->rtq_count > 0)
888 1.37 itojun rtq->rtq_count--;
889 1.37 itojun else
890 1.55 itojun printf("rt_timer_queue_remove_all: "
891 1.55 itojun "rtq_count reached 0\n");
892 1.18 kml }
893 1.55 itojun }
894 1.55 itojun
895 1.55 itojun void
896 1.60 matt rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
897 1.55 itojun {
898 1.55 itojun
899 1.55 itojun rt_timer_queue_remove_all(rtq, destroy);
900 1.18 kml
901 1.18 kml LIST_REMOVE(rtq, rtq_link);
902 1.22 thorpej
903 1.22 thorpej /*
904 1.22 thorpej * Caller is responsible for freeing the rttimer_queue structure.
905 1.22 thorpej */
906 1.18 kml }
907 1.18 kml
908 1.37 itojun unsigned long
909 1.60 matt rt_timer_count(struct rttimer_queue *rtq)
910 1.37 itojun {
911 1.37 itojun return rtq->rtq_count;
912 1.37 itojun }
913 1.37 itojun
914 1.65 perry void
915 1.60 matt rt_timer_remove_all(struct rtentry *rt, int destroy)
916 1.18 kml {
917 1.24 thorpej struct rttimer *r;
918 1.18 kml
919 1.24 thorpej while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
920 1.18 kml LIST_REMOVE(r, rtt_link);
921 1.24 thorpej TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
922 1.54 itojun if (destroy)
923 1.54 itojun RTTIMER_CALLOUT(r);
924 1.37 itojun if (r->rtt_queue->rtq_count > 0)
925 1.37 itojun r->rtt_queue->rtq_count--;
926 1.37 itojun else
927 1.37 itojun printf("rt_timer_remove_all: rtq_count reached 0\n");
928 1.38 itojun pool_put(&rttimer_pool, r);
929 1.18 kml }
930 1.18 kml }
931 1.18 kml
932 1.65 perry int
933 1.60 matt rt_timer_add(struct rtentry *rt,
934 1.60 matt void (*func)(struct rtentry *, struct rttimer *),
935 1.60 matt struct rttimer_queue *queue)
936 1.18 kml {
937 1.24 thorpej struct rttimer *r;
938 1.24 thorpej long current_time;
939 1.18 kml int s;
940 1.18 kml
941 1.18 kml s = splclock();
942 1.18 kml current_time = mono_time.tv_sec;
943 1.18 kml splx(s);
944 1.18 kml
945 1.24 thorpej /*
946 1.24 thorpej * If there's already a timer with this action, destroy it before
947 1.24 thorpej * we add a new one.
948 1.24 thorpej */
949 1.24 thorpej for (r = LIST_FIRST(&rt->rt_timer); r != NULL;
950 1.24 thorpej r = LIST_NEXT(r, rtt_link)) {
951 1.18 kml if (r->rtt_func == func) {
952 1.18 kml LIST_REMOVE(r, rtt_link);
953 1.24 thorpej TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
954 1.37 itojun if (r->rtt_queue->rtq_count > 0)
955 1.37 itojun r->rtt_queue->rtq_count--;
956 1.37 itojun else
957 1.37 itojun printf("rt_timer_add: rtq_count reached 0\n");
958 1.38 itojun pool_put(&rttimer_pool, r);
959 1.18 kml break; /* only one per list, so we can quit... */
960 1.18 kml }
961 1.18 kml }
962 1.18 kml
963 1.24 thorpej r = pool_get(&rttimer_pool, PR_NOWAIT);
964 1.24 thorpej if (r == NULL)
965 1.24 thorpej return (ENOBUFS);
966 1.32 itojun Bzero(r, sizeof(*r));
967 1.24 thorpej
968 1.24 thorpej r->rtt_rt = rt;
969 1.24 thorpej r->rtt_time = current_time;
970 1.24 thorpej r->rtt_func = func;
971 1.24 thorpej r->rtt_queue = queue;
972 1.24 thorpej LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
973 1.24 thorpej TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
974 1.37 itojun r->rtt_queue->rtq_count++;
975 1.65 perry
976 1.24 thorpej return (0);
977 1.18 kml }
978 1.18 kml
979 1.18 kml /* ARGSUSED */
980 1.18 kml void
981 1.60 matt rt_timer_timer(void *arg)
982 1.18 kml {
983 1.24 thorpej struct rttimer_queue *rtq;
984 1.24 thorpej struct rttimer *r;
985 1.18 kml long current_time;
986 1.24 thorpej int s;
987 1.21 kml
988 1.24 thorpej s = splclock();
989 1.18 kml current_time = mono_time.tv_sec;
990 1.24 thorpej splx(s);
991 1.18 kml
992 1.24 thorpej s = splsoftnet();
993 1.65 perry for (rtq = LIST_FIRST(&rttimer_queue_head); rtq != NULL;
994 1.18 kml rtq = LIST_NEXT(rtq, rtq_link)) {
995 1.24 thorpej while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
996 1.24 thorpej (r->rtt_time + rtq->rtq_timeout) < current_time) {
997 1.24 thorpej LIST_REMOVE(r, rtt_link);
998 1.24 thorpej TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
999 1.24 thorpej RTTIMER_CALLOUT(r);
1000 1.24 thorpej pool_put(&rttimer_pool, r);
1001 1.37 itojun if (rtq->rtq_count > 0)
1002 1.37 itojun rtq->rtq_count--;
1003 1.37 itojun else
1004 1.37 itojun printf("rt_timer_timer: rtq_count reached 0\n");
1005 1.18 kml }
1006 1.18 kml }
1007 1.24 thorpej splx(s);
1008 1.18 kml
1009 1.35 thorpej callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1010 1.1 cgd }
1011