route.c revision 1.89 1 1.89 xtraeme /* $NetBSD: route.c,v 1.89 2007/04/22 13:05:21 xtraeme 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.89 xtraeme __KERNEL_RCSID(0, "$NetBSD: route.c,v 1.89 2007/04/22 13:05:21 xtraeme Exp $");
102 1.2 cgd
103 1.5 mycroft #include <sys/param.h>
104 1.5 mycroft #include <sys/systm.h>
105 1.35 thorpej #include <sys/callout.h>
106 1.5 mycroft #include <sys/proc.h>
107 1.5 mycroft #include <sys/mbuf.h>
108 1.5 mycroft #include <sys/socket.h>
109 1.5 mycroft #include <sys/socketvar.h>
110 1.5 mycroft #include <sys/domain.h>
111 1.5 mycroft #include <sys/protosw.h>
112 1.18 kml #include <sys/kernel.h>
113 1.5 mycroft #include <sys/ioctl.h>
114 1.22 thorpej #include <sys/pool.h>
115 1.1 cgd
116 1.5 mycroft #include <net/if.h>
117 1.5 mycroft #include <net/route.h>
118 1.5 mycroft #include <net/raw_cb.h>
119 1.1 cgd
120 1.5 mycroft #include <netinet/in.h>
121 1.5 mycroft #include <netinet/in_var.h>
122 1.1 cgd
123 1.5 mycroft
124 1.52 matt struct route_cb route_cb;
125 1.52 matt struct rtstat rtstat;
126 1.52 matt struct radix_node_head *rt_tables[AF_MAX+1];
127 1.1 cgd
128 1.1 cgd int rttrash; /* routes not in table but not freed */
129 1.1 cgd struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
130 1.1 cgd
131 1.88 ad POOL_INIT(rtentry_pool, sizeof(struct rtentry), 0, 0, 0, "rtentpl", NULL,
132 1.88 ad IPL_SOFTNET);
133 1.88 ad POOL_INIT(rttimer_pool, sizeof(struct rttimer), 0, 0, 0, "rttmrpl", NULL,
134 1.88 ad IPL_SOFTNET);
135 1.22 thorpej
136 1.35 thorpej struct callout rt_timer_ch; /* callout for rt_timer_timer() */
137 1.35 thorpej
138 1.60 matt static int rtdeletemsg(struct rtentry *);
139 1.60 matt static int rtflushclone1(struct radix_node *, void *);
140 1.60 matt static void rtflushclone(struct radix_node_head *, struct rtentry *);
141 1.40 itojun
142 1.81 joerg struct ifaddr *
143 1.81 joerg rt_get_ifa(struct rtentry *rt)
144 1.81 joerg {
145 1.81 joerg struct ifaddr *ifa;
146 1.81 joerg
147 1.81 joerg if ((ifa = rt->rt_ifa) == NULL)
148 1.81 joerg return ifa;
149 1.81 joerg else if (ifa->ifa_getifa == NULL)
150 1.81 joerg return ifa;
151 1.81 joerg #if 0
152 1.81 joerg else if (ifa->ifa_seqno != NULL && *ifa->ifa_seqno == rt->rt_ifa_seqno)
153 1.81 joerg return ifa;
154 1.81 joerg #endif
155 1.81 joerg else {
156 1.81 joerg ifa = (*ifa->ifa_getifa)(ifa, rt_key(rt));
157 1.81 joerg rt_replace_ifa(rt, ifa);
158 1.81 joerg return ifa;
159 1.81 joerg }
160 1.81 joerg }
161 1.81 joerg
162 1.80 joerg static void
163 1.80 joerg rt_set_ifa1(struct rtentry *rt, struct ifaddr *ifa)
164 1.80 joerg {
165 1.80 joerg rt->rt_ifa = ifa;
166 1.80 joerg if (ifa->ifa_seqno != NULL)
167 1.80 joerg rt->rt_ifa_seqno = *ifa->ifa_seqno;
168 1.80 joerg }
169 1.80 joerg
170 1.80 joerg void
171 1.80 joerg rt_replace_ifa(struct rtentry *rt, struct ifaddr *ifa)
172 1.80 joerg {
173 1.80 joerg IFAREF(ifa);
174 1.80 joerg IFAFREE(rt->rt_ifa);
175 1.80 joerg rt_set_ifa1(rt, ifa);
176 1.80 joerg }
177 1.80 joerg
178 1.80 joerg static void
179 1.80 joerg rt_set_ifa(struct rtentry *rt, struct ifaddr *ifa)
180 1.80 joerg {
181 1.80 joerg IFAREF(ifa);
182 1.80 joerg rt_set_ifa1(rt, ifa);
183 1.80 joerg }
184 1.80 joerg
185 1.10 mycroft void
186 1.60 matt rtable_init(void **table)
187 1.10 mycroft {
188 1.10 mycroft struct domain *dom;
189 1.64 matt DOMAIN_FOREACH(dom)
190 1.10 mycroft if (dom->dom_rtattach)
191 1.10 mycroft dom->dom_rtattach(&table[dom->dom_family],
192 1.10 mycroft dom->dom_rtoffset);
193 1.10 mycroft }
194 1.1 cgd
195 1.9 mycroft void
196 1.60 matt route_init(void)
197 1.1 cgd {
198 1.22 thorpej
199 1.10 mycroft rn_init(); /* initialize all zeroes, all ones, mask table */
200 1.10 mycroft rtable_init((void **)rt_tables);
201 1.1 cgd }
202 1.1 cgd
203 1.82 dyoung void
204 1.82 dyoung rtflushall(int family)
205 1.82 dyoung {
206 1.86 dyoung const struct domain *dom;
207 1.82 dyoung
208 1.82 dyoung if ((dom = pffinddomain(family)) != NULL && dom->dom_rtflushall != NULL)
209 1.82 dyoung (*dom->dom_rtflushall)();
210 1.82 dyoung }
211 1.82 dyoung
212 1.82 dyoung void
213 1.82 dyoung rtflush(struct route *ro)
214 1.82 dyoung {
215 1.86 dyoung const struct domain *dom;
216 1.82 dyoung
217 1.82 dyoung KASSERT(ro->ro_rt != NULL);
218 1.82 dyoung
219 1.82 dyoung RTFREE(ro->ro_rt);
220 1.82 dyoung ro->ro_rt = NULL;
221 1.82 dyoung
222 1.86 dyoung if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) != NULL &&
223 1.82 dyoung dom->dom_rtflush != NULL)
224 1.82 dyoung (*dom->dom_rtflush)(ro);
225 1.82 dyoung }
226 1.82 dyoung
227 1.82 dyoung void
228 1.82 dyoung rtcache(struct route *ro)
229 1.82 dyoung {
230 1.86 dyoung const struct domain *dom;
231 1.82 dyoung
232 1.82 dyoung KASSERT(ro->ro_rt != NULL);
233 1.82 dyoung
234 1.86 dyoung if ((dom = pffinddomain(rtcache_getdst(ro)->sa_family)) != NULL &&
235 1.82 dyoung dom->dom_rtcache != NULL)
236 1.82 dyoung (*dom->dom_rtcache)(ro);
237 1.82 dyoung }
238 1.82 dyoung
239 1.1 cgd /*
240 1.1 cgd * Packet routing routines.
241 1.1 cgd */
242 1.9 mycroft void
243 1.60 matt rtalloc(struct route *ro)
244 1.1 cgd {
245 1.75 dyoung if (ro->ro_rt != NULL) {
246 1.75 dyoung if (ro->ro_rt->rt_ifp != NULL &&
247 1.75 dyoung (ro->ro_rt->rt_flags & RTF_UP) != 0)
248 1.75 dyoung return;
249 1.82 dyoung rtflush(ro);
250 1.75 dyoung }
251 1.86 dyoung if ((ro->ro_rt = rtalloc1(rtcache_getdst(ro), 1)) == NULL)
252 1.82 dyoung return;
253 1.82 dyoung rtcache(ro);
254 1.1 cgd }
255 1.25 itojun
256 1.1 cgd struct rtentry *
257 1.60 matt rtalloc1(const struct sockaddr *dst, int report)
258 1.1 cgd {
259 1.36 augustss struct radix_node_head *rnh = rt_tables[dst->sa_family];
260 1.36 augustss struct rtentry *rt;
261 1.36 augustss struct radix_node *rn;
262 1.68 christos struct rtentry *newrt = NULL;
263 1.10 mycroft struct rt_addrinfo info;
264 1.13 mycroft int s = splsoftnet(), err = 0, msgtype = RTM_MISS;
265 1.1 cgd
266 1.66 christos if (rnh && (rn = rnh->rnh_matchaddr(dst, rnh)) &&
267 1.1 cgd ((rn->rn_flags & RNF_ROOT) == 0)) {
268 1.1 cgd newrt = rt = (struct rtentry *)rn;
269 1.1 cgd if (report && (rt->rt_flags & RTF_CLONING)) {
270 1.68 christos err = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
271 1.68 christos &newrt);
272 1.8 cgd if (err) {
273 1.8 cgd newrt = rt;
274 1.8 cgd rt->rt_refcnt++;
275 1.8 cgd goto miss;
276 1.8 cgd }
277 1.69 christos KASSERT(newrt != NULL);
278 1.8 cgd if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
279 1.8 cgd msgtype = RTM_RESOLVE;
280 1.8 cgd goto miss;
281 1.8 cgd }
282 1.39 itojun /* Inform listeners of the new route */
283 1.44 thorpej memset(&info, 0, sizeof(info));
284 1.39 itojun info.rti_info[RTAX_DST] = rt_key(rt);
285 1.39 itojun info.rti_info[RTAX_NETMASK] = rt_mask(rt);
286 1.39 itojun info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
287 1.39 itojun if (rt->rt_ifp != NULL) {
288 1.65 perry info.rti_info[RTAX_IFP] =
289 1.49 matt TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr;
290 1.39 itojun info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
291 1.39 itojun }
292 1.39 itojun rt_missmsg(RTM_ADD, &info, rt->rt_flags, 0);
293 1.1 cgd } else
294 1.1 cgd rt->rt_refcnt++;
295 1.1 cgd } else {
296 1.1 cgd rtstat.rts_unreach++;
297 1.10 mycroft miss: if (report) {
298 1.87 christos memset((void *)&info, 0, sizeof(info));
299 1.10 mycroft info.rti_info[RTAX_DST] = dst;
300 1.10 mycroft rt_missmsg(msgtype, &info, 0, err);
301 1.10 mycroft }
302 1.1 cgd }
303 1.1 cgd splx(s);
304 1.1 cgd return (newrt);
305 1.1 cgd }
306 1.1 cgd
307 1.9 mycroft void
308 1.60 matt rtfree(struct rtentry *rt)
309 1.1 cgd {
310 1.36 augustss struct ifaddr *ifa;
311 1.10 mycroft
312 1.68 christos if (rt == NULL)
313 1.1 cgd panic("rtfree");
314 1.1 cgd rt->rt_refcnt--;
315 1.1 cgd if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
316 1.1 cgd if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
317 1.1 cgd panic ("rtfree 2");
318 1.10 mycroft rttrash--;
319 1.10 mycroft if (rt->rt_refcnt < 0) {
320 1.16 christos printf("rtfree: %p not freed (neg refs)\n", rt);
321 1.10 mycroft return;
322 1.10 mycroft }
323 1.54 itojun rt_timer_remove_all(rt, 0);
324 1.10 mycroft ifa = rt->rt_ifa;
325 1.78 dyoung rt->rt_ifa = NULL;
326 1.10 mycroft IFAFREE(ifa);
327 1.78 dyoung rt->rt_ifp = NULL;
328 1.10 mycroft Free(rt_key(rt));
329 1.22 thorpej pool_put(&rtentry_pool, rt);
330 1.1 cgd }
331 1.1 cgd }
332 1.1 cgd
333 1.10 mycroft void
334 1.60 matt ifafree(struct ifaddr *ifa)
335 1.10 mycroft {
336 1.30 thorpej
337 1.30 thorpej #ifdef DIAGNOSTIC
338 1.10 mycroft if (ifa == NULL)
339 1.30 thorpej panic("ifafree: null ifa");
340 1.30 thorpej if (ifa->ifa_refcnt != 0)
341 1.30 thorpej panic("ifafree: ifa_refcnt != 0 (%d)", ifa->ifa_refcnt);
342 1.30 thorpej #endif
343 1.31 thorpej #ifdef IFAREF_DEBUG
344 1.31 thorpej printf("ifafree: freeing ifaddr %p\n", ifa);
345 1.31 thorpej #endif
346 1.30 thorpej free(ifa, M_IFADDR);
347 1.10 mycroft }
348 1.10 mycroft
349 1.1 cgd /*
350 1.1 cgd * Force a routing table entry to the specified
351 1.1 cgd * destination to go through the given gateway.
352 1.1 cgd * Normally called as a result of a routing redirect
353 1.1 cgd * message from the network layer.
354 1.1 cgd *
355 1.13 mycroft * N.B.: must be called at splsoftnet
356 1.1 cgd */
357 1.14 christos void
358 1.60 matt rtredirect(const struct sockaddr *dst, const struct sockaddr *gateway,
359 1.60 matt const struct sockaddr *netmask, int flags, const struct sockaddr *src,
360 1.60 matt struct rtentry **rtp)
361 1.1 cgd {
362 1.36 augustss struct rtentry *rt;
363 1.1 cgd int error = 0;
364 1.68 christos u_quad_t *stat = NULL;
365 1.10 mycroft struct rt_addrinfo info;
366 1.10 mycroft struct ifaddr *ifa;
367 1.1 cgd
368 1.1 cgd /* verify the gateway is directly reachable */
369 1.68 christos if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
370 1.1 cgd error = ENETUNREACH;
371 1.8 cgd goto out;
372 1.1 cgd }
373 1.1 cgd rt = rtalloc1(dst, 0);
374 1.1 cgd /*
375 1.1 cgd * If the redirect isn't from our current router for this dst,
376 1.1 cgd * it's either old or wrong. If it redirects us to ourselves,
377 1.1 cgd * we have a routing loop, perhaps as a result of an interface
378 1.1 cgd * going down recently.
379 1.1 cgd */
380 1.45 itojun #define equal(a1, a2) \
381 1.45 itojun ((a1)->sa_len == (a2)->sa_len && \
382 1.66 christos memcmp((a1), (a2), (a1)->sa_len) == 0)
383 1.10 mycroft if (!(flags & RTF_DONE) && rt &&
384 1.10 mycroft (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
385 1.1 cgd error = EINVAL;
386 1.1 cgd else if (ifa_ifwithaddr(gateway))
387 1.1 cgd error = EHOSTUNREACH;
388 1.1 cgd if (error)
389 1.1 cgd goto done;
390 1.1 cgd /*
391 1.1 cgd * Create a new entry if we just got back a wildcard entry
392 1.33 soren * or the lookup failed. This is necessary for hosts
393 1.1 cgd * which use routing redirects generated by smart gateways
394 1.1 cgd * to dynamically build the routing tables.
395 1.1 cgd */
396 1.68 christos if ((rt == NULL) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
397 1.1 cgd goto create;
398 1.1 cgd /*
399 1.1 cgd * Don't listen to the redirect if it's
400 1.65 perry * for a route to an interface.
401 1.1 cgd */
402 1.1 cgd if (rt->rt_flags & RTF_GATEWAY) {
403 1.1 cgd if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
404 1.1 cgd /*
405 1.1 cgd * Changing from route to net => route to host.
406 1.1 cgd * Create new route, rather than smashing route to net.
407 1.1 cgd */
408 1.1 cgd create:
409 1.39 itojun if (rt)
410 1.39 itojun rtfree(rt);
411 1.1 cgd flags |= RTF_GATEWAY | RTF_DYNAMIC;
412 1.39 itojun info.rti_info[RTAX_DST] = dst;
413 1.39 itojun info.rti_info[RTAX_GATEWAY] = gateway;
414 1.39 itojun info.rti_info[RTAX_NETMASK] = netmask;
415 1.39 itojun info.rti_ifa = ifa;
416 1.39 itojun info.rti_flags = flags;
417 1.39 itojun rt = NULL;
418 1.39 itojun error = rtrequest1(RTM_ADD, &info, &rt);
419 1.39 itojun if (rt != NULL)
420 1.39 itojun flags = rt->rt_flags;
421 1.1 cgd stat = &rtstat.rts_dynamic;
422 1.1 cgd } else {
423 1.1 cgd /*
424 1.1 cgd * Smash the current notion of the gateway to
425 1.1 cgd * this destination. Should check about netmask!!!
426 1.1 cgd */
427 1.10 mycroft rt->rt_flags |= RTF_MODIFIED;
428 1.10 mycroft flags |= RTF_MODIFIED;
429 1.10 mycroft stat = &rtstat.rts_newgateway;
430 1.10 mycroft rt_setgate(rt, rt_key(rt), gateway);
431 1.1 cgd }
432 1.1 cgd } else
433 1.1 cgd error = EHOSTUNREACH;
434 1.1 cgd done:
435 1.1 cgd if (rt) {
436 1.1 cgd if (rtp && !error)
437 1.1 cgd *rtp = rt;
438 1.1 cgd else
439 1.1 cgd rtfree(rt);
440 1.1 cgd }
441 1.8 cgd out:
442 1.1 cgd if (error)
443 1.1 cgd rtstat.rts_badredirect++;
444 1.8 cgd else if (stat != NULL)
445 1.8 cgd (*stat)++;
446 1.87 christos memset((void *)&info, 0, sizeof(info));
447 1.10 mycroft info.rti_info[RTAX_DST] = dst;
448 1.10 mycroft info.rti_info[RTAX_GATEWAY] = gateway;
449 1.10 mycroft info.rti_info[RTAX_NETMASK] = netmask;
450 1.10 mycroft info.rti_info[RTAX_AUTHOR] = src;
451 1.10 mycroft rt_missmsg(RTM_REDIRECT, &info, flags, error);
452 1.1 cgd }
453 1.1 cgd
454 1.1 cgd /*
455 1.40 itojun * Delete a route and generate a message
456 1.40 itojun */
457 1.40 itojun static int
458 1.60 matt rtdeletemsg(struct rtentry *rt)
459 1.40 itojun {
460 1.40 itojun int error;
461 1.40 itojun struct rt_addrinfo info;
462 1.40 itojun
463 1.40 itojun /*
464 1.40 itojun * Request the new route so that the entry is not actually
465 1.40 itojun * deleted. That will allow the information being reported to
466 1.40 itojun * be accurate (and consistent with route_output()).
467 1.40 itojun */
468 1.87 christos memset((void *)&info, 0, sizeof(info));
469 1.40 itojun info.rti_info[RTAX_DST] = rt_key(rt);
470 1.40 itojun info.rti_info[RTAX_NETMASK] = rt_mask(rt);
471 1.40 itojun info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
472 1.40 itojun info.rti_flags = rt->rt_flags;
473 1.40 itojun error = rtrequest1(RTM_DELETE, &info, &rt);
474 1.40 itojun
475 1.40 itojun rt_missmsg(RTM_DELETE, &info, info.rti_flags, error);
476 1.40 itojun
477 1.40 itojun /* Adjust the refcount */
478 1.40 itojun if (error == 0 && rt->rt_refcnt <= 0) {
479 1.40 itojun rt->rt_refcnt++;
480 1.40 itojun rtfree(rt);
481 1.40 itojun }
482 1.40 itojun return (error);
483 1.40 itojun }
484 1.40 itojun
485 1.41 itojun static int
486 1.60 matt rtflushclone1(struct radix_node *rn, void *arg)
487 1.41 itojun {
488 1.41 itojun struct rtentry *rt, *parent;
489 1.41 itojun
490 1.41 itojun rt = (struct rtentry *)rn;
491 1.41 itojun parent = (struct rtentry *)arg;
492 1.41 itojun if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent == parent)
493 1.41 itojun rtdeletemsg(rt);
494 1.41 itojun return 0;
495 1.41 itojun }
496 1.41 itojun
497 1.41 itojun static void
498 1.60 matt rtflushclone(struct radix_node_head *rnh, struct rtentry *parent)
499 1.41 itojun {
500 1.41 itojun
501 1.41 itojun #ifdef DIAGNOSTIC
502 1.41 itojun if (!parent || (parent->rt_flags & RTF_CLONING) == 0)
503 1.41 itojun panic("rtflushclone: called with a non-cloning route");
504 1.41 itojun if (!rnh->rnh_walktree)
505 1.41 itojun panic("rtflushclone: no rnh_walktree");
506 1.41 itojun #endif
507 1.42 itojun rnh->rnh_walktree(rnh, rtflushclone1, (void *)parent);
508 1.41 itojun }
509 1.41 itojun
510 1.40 itojun /*
511 1.22 thorpej * Routing table ioctl interface.
512 1.22 thorpej */
513 1.9 mycroft int
514 1.87 christos rtioctl(u_long req, void *data, struct lwp *l)
515 1.1 cgd {
516 1.1 cgd return (EOPNOTSUPP);
517 1.1 cgd }
518 1.1 cgd
519 1.1 cgd struct ifaddr *
520 1.60 matt ifa_ifwithroute(int flags, const struct sockaddr *dst,
521 1.60 matt const struct sockaddr *gateway)
522 1.1 cgd {
523 1.36 augustss struct ifaddr *ifa;
524 1.1 cgd if ((flags & RTF_GATEWAY) == 0) {
525 1.1 cgd /*
526 1.1 cgd * If we are adding a route to an interface,
527 1.1 cgd * and the interface is a pt to pt link
528 1.1 cgd * we should search for the destination
529 1.1 cgd * as our clue to the interface. Otherwise
530 1.1 cgd * we can use the local address.
531 1.1 cgd */
532 1.68 christos ifa = NULL;
533 1.65 perry if (flags & RTF_HOST)
534 1.1 cgd ifa = ifa_ifwithdstaddr(dst);
535 1.68 christos if (ifa == NULL)
536 1.1 cgd ifa = ifa_ifwithaddr(gateway);
537 1.1 cgd } else {
538 1.1 cgd /*
539 1.1 cgd * If we are adding a route to a remote net
540 1.1 cgd * or host, the gateway may still be on the
541 1.1 cgd * other end of a pt to pt link.
542 1.1 cgd */
543 1.1 cgd ifa = ifa_ifwithdstaddr(gateway);
544 1.1 cgd }
545 1.68 christos if (ifa == NULL)
546 1.1 cgd ifa = ifa_ifwithnet(gateway);
547 1.68 christos if (ifa == NULL) {
548 1.1 cgd struct rtentry *rt = rtalloc1(dst, 0);
549 1.68 christos if (rt == NULL)
550 1.68 christos return NULL;
551 1.1 cgd rt->rt_refcnt--;
552 1.68 christos if ((ifa = rt->rt_ifa) == NULL)
553 1.68 christos return NULL;
554 1.1 cgd }
555 1.1 cgd if (ifa->ifa_addr->sa_family != dst->sa_family) {
556 1.10 mycroft struct ifaddr *oifa = ifa;
557 1.1 cgd ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
558 1.1 cgd if (ifa == 0)
559 1.1 cgd ifa = oifa;
560 1.1 cgd }
561 1.1 cgd return (ifa);
562 1.1 cgd }
563 1.1 cgd
564 1.1 cgd #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
565 1.1 cgd
566 1.9 mycroft int
567 1.60 matt rtrequest(int req, const struct sockaddr *dst, const struct sockaddr *gateway,
568 1.60 matt const struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
569 1.1 cgd {
570 1.39 itojun struct rt_addrinfo info;
571 1.39 itojun
572 1.44 thorpej memset(&info, 0, sizeof(info));
573 1.39 itojun info.rti_flags = flags;
574 1.39 itojun info.rti_info[RTAX_DST] = dst;
575 1.39 itojun info.rti_info[RTAX_GATEWAY] = gateway;
576 1.39 itojun info.rti_info[RTAX_NETMASK] = netmask;
577 1.39 itojun return rtrequest1(req, &info, ret_nrt);
578 1.39 itojun }
579 1.39 itojun
580 1.39 itojun int
581 1.60 matt rt_getifa(struct rt_addrinfo *info)
582 1.39 itojun {
583 1.39 itojun struct ifaddr *ifa;
584 1.68 christos const struct sockaddr *dst = info->rti_info[RTAX_DST];
585 1.68 christos const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
586 1.68 christos const struct sockaddr *ifaaddr = info->rti_info[RTAX_IFA];
587 1.68 christos const struct sockaddr *ifpaddr = info->rti_info[RTAX_IFP];
588 1.68 christos int flags = info->rti_flags;
589 1.39 itojun
590 1.39 itojun /*
591 1.39 itojun * ifp may be specified by sockaddr_dl when protocol address
592 1.39 itojun * is ambiguous
593 1.39 itojun */
594 1.39 itojun if (info->rti_ifp == NULL && ifpaddr != NULL
595 1.39 itojun && ifpaddr->sa_family == AF_LINK &&
596 1.59 matt (ifa = ifa_ifwithnet((const struct sockaddr *)ifpaddr)) != NULL)
597 1.39 itojun info->rti_ifp = ifa->ifa_ifp;
598 1.39 itojun if (info->rti_ifa == NULL && ifaaddr != NULL)
599 1.39 itojun info->rti_ifa = ifa_ifwithaddr(ifaaddr);
600 1.39 itojun if (info->rti_ifa == NULL) {
601 1.59 matt const struct sockaddr *sa;
602 1.39 itojun
603 1.39 itojun sa = ifaaddr != NULL ? ifaaddr :
604 1.39 itojun (gateway != NULL ? gateway : dst);
605 1.39 itojun if (sa != NULL && info->rti_ifp != NULL)
606 1.39 itojun info->rti_ifa = ifaof_ifpforaddr(sa, info->rti_ifp);
607 1.39 itojun else if (dst != NULL && gateway != NULL)
608 1.39 itojun info->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
609 1.39 itojun else if (sa != NULL)
610 1.39 itojun info->rti_ifa = ifa_ifwithroute(flags, sa, sa);
611 1.39 itojun }
612 1.74 dyoung if ((ifa = info->rti_ifa) == NULL)
613 1.74 dyoung return ENETUNREACH;
614 1.74 dyoung if (ifa->ifa_getifa != NULL)
615 1.74 dyoung info->rti_ifa = ifa = (*ifa->ifa_getifa)(ifa, dst);
616 1.74 dyoung if (info->rti_ifp == NULL)
617 1.74 dyoung info->rti_ifp = ifa->ifa_ifp;
618 1.74 dyoung return 0;
619 1.39 itojun }
620 1.39 itojun
621 1.39 itojun int
622 1.60 matt rtrequest1(int req, struct rt_addrinfo *info, struct rtentry **ret_nrt)
623 1.39 itojun {
624 1.60 matt int s = splsoftnet();
625 1.60 matt int error = 0;
626 1.40 itojun struct rtentry *rt, *crt;
627 1.36 augustss struct radix_node *rn;
628 1.36 augustss struct radix_node_head *rnh;
629 1.10 mycroft struct ifaddr *ifa;
630 1.63 christos struct sockaddr_storage deldst;
631 1.68 christos const struct sockaddr *dst = info->rti_info[RTAX_DST];
632 1.68 christos const struct sockaddr *gateway = info->rti_info[RTAX_GATEWAY];
633 1.68 christos const struct sockaddr *netmask = info->rti_info[RTAX_NETMASK];
634 1.68 christos int flags = info->rti_flags;
635 1.1 cgd #define senderr(x) { error = x ; goto bad; }
636 1.1 cgd
637 1.68 christos if ((rnh = rt_tables[dst->sa_family]) == NULL)
638 1.1 cgd senderr(ESRCH);
639 1.1 cgd if (flags & RTF_HOST)
640 1.68 christos netmask = NULL;
641 1.1 cgd switch (req) {
642 1.1 cgd case RTM_DELETE:
643 1.63 christos if (netmask) {
644 1.63 christos rt_maskedcopy(dst, (struct sockaddr *)&deldst, netmask);
645 1.63 christos dst = (struct sockaddr *)&deldst;
646 1.63 christos }
647 1.68 christos if ((rn = rnh->rnh_lookup(dst, netmask, rnh)) == NULL)
648 1.41 itojun senderr(ESRCH);
649 1.41 itojun rt = (struct rtentry *)rn;
650 1.41 itojun if ((rt->rt_flags & RTF_CLONING) != 0) {
651 1.41 itojun /* clean up any cloned children */
652 1.41 itojun rtflushclone(rnh, rt);
653 1.41 itojun }
654 1.68 christos if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == NULL)
655 1.1 cgd senderr(ESRCH);
656 1.1 cgd if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
657 1.1 cgd panic ("rtrequest delete");
658 1.1 cgd rt = (struct rtentry *)rn;
659 1.10 mycroft if (rt->rt_gwroute) {
660 1.68 christos RTFREE(rt->rt_gwroute);
661 1.68 christos rt->rt_gwroute = NULL;
662 1.48 itojun }
663 1.48 itojun if (rt->rt_parent) {
664 1.48 itojun rt->rt_parent->rt_refcnt--;
665 1.48 itojun rt->rt_parent = NULL;
666 1.10 mycroft }
667 1.28 erh rt->rt_flags &= ~RTF_UP;
668 1.1 cgd if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
669 1.39 itojun ifa->ifa_rtrequest(RTM_DELETE, rt, info);
670 1.1 cgd rttrash++;
671 1.10 mycroft if (ret_nrt)
672 1.10 mycroft *ret_nrt = rt;
673 1.10 mycroft else if (rt->rt_refcnt <= 0) {
674 1.10 mycroft rt->rt_refcnt++;
675 1.1 cgd rtfree(rt);
676 1.10 mycroft }
677 1.1 cgd break;
678 1.1 cgd
679 1.1 cgd case RTM_RESOLVE:
680 1.68 christos if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
681 1.1 cgd senderr(EINVAL);
682 1.40 itojun if ((rt->rt_flags & RTF_CLONING) == 0)
683 1.40 itojun senderr(EINVAL);
684 1.1 cgd ifa = rt->rt_ifa;
685 1.40 itojun flags = rt->rt_flags & ~(RTF_CLONING | RTF_STATIC);
686 1.40 itojun flags |= RTF_CLONED;
687 1.1 cgd gateway = rt->rt_gateway;
688 1.68 christos if ((netmask = rt->rt_genmask) == NULL)
689 1.1 cgd flags |= RTF_HOST;
690 1.1 cgd goto makeroute;
691 1.1 cgd
692 1.1 cgd case RTM_ADD:
693 1.68 christos if (info->rti_ifa == NULL && (error = rt_getifa(info)))
694 1.39 itojun senderr(error);
695 1.39 itojun ifa = info->rti_ifa;
696 1.1 cgd makeroute:
697 1.72 tls /* Already at splsoftnet() so pool_get/pool_put are safe */
698 1.22 thorpej rt = pool_get(&rtentry_pool, PR_NOWAIT);
699 1.68 christos if (rt == NULL)
700 1.1 cgd senderr(ENOBUFS);
701 1.10 mycroft Bzero(rt, sizeof(*rt));
702 1.10 mycroft rt->rt_flags = RTF_UP | flags;
703 1.18 kml LIST_INIT(&rt->rt_timer);
704 1.10 mycroft if (rt_setgate(rt, dst, gateway)) {
705 1.22 thorpej pool_put(&rtentry_pool, rt);
706 1.10 mycroft senderr(ENOBUFS);
707 1.10 mycroft }
708 1.1 cgd if (netmask) {
709 1.79 dyoung rt_maskedcopy(dst, rt_key(rt), netmask);
710 1.1 cgd } else
711 1.79 dyoung Bcopy(dst, rt_key(rt), dst->sa_len);
712 1.74 dyoung rt_set_ifa(rt, ifa);
713 1.1 cgd rt->rt_ifp = ifa->ifa_ifp;
714 1.27 matt if (req == RTM_RESOLVE) {
715 1.1 cgd rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
716 1.41 itojun rt->rt_parent = *ret_nrt;
717 1.41 itojun rt->rt_parent->rt_refcnt++;
718 1.40 itojun }
719 1.79 dyoung rn = rnh->rnh_addaddr(rt_key(rt), netmask, rnh, rt->rt_nodes);
720 1.79 dyoung if (rn == NULL && (crt = rtalloc1(rt_key(rt), 0)) != NULL) {
721 1.40 itojun /* overwrite cloned route */
722 1.40 itojun if ((crt->rt_flags & RTF_CLONED) != 0) {
723 1.40 itojun rtdeletemsg(crt);
724 1.79 dyoung rn = rnh->rnh_addaddr(rt_key(rt),
725 1.66 christos netmask, rnh, rt->rt_nodes);
726 1.40 itojun }
727 1.40 itojun RTFREE(crt);
728 1.40 itojun }
729 1.68 christos if (rn == NULL) {
730 1.40 itojun IFAFREE(ifa);
731 1.41 itojun if ((rt->rt_flags & RTF_CLONED) != 0 && rt->rt_parent)
732 1.41 itojun rtfree(rt->rt_parent);
733 1.40 itojun if (rt->rt_gwroute)
734 1.40 itojun rtfree(rt->rt_gwroute);
735 1.40 itojun Free(rt_key(rt));
736 1.40 itojun pool_put(&rtentry_pool, rt);
737 1.40 itojun senderr(EEXIST);
738 1.27 matt }
739 1.1 cgd if (ifa->ifa_rtrequest)
740 1.39 itojun ifa->ifa_rtrequest(req, rt, info);
741 1.1 cgd if (ret_nrt) {
742 1.1 cgd *ret_nrt = rt;
743 1.1 cgd rt->rt_refcnt++;
744 1.41 itojun }
745 1.41 itojun if ((rt->rt_flags & RTF_CLONING) != 0) {
746 1.41 itojun /* clean up any cloned children */
747 1.41 itojun rtflushclone(rnh, rt);
748 1.1 cgd }
749 1.82 dyoung rtflushall(dst->sa_family);
750 1.1 cgd break;
751 1.1 cgd }
752 1.1 cgd bad:
753 1.1 cgd splx(s);
754 1.1 cgd return (error);
755 1.1 cgd }
756 1.1 cgd
757 1.10 mycroft int
758 1.60 matt rt_setgate( struct rtentry *rt0, const struct sockaddr *dst,
759 1.60 matt const struct sockaddr *gate)
760 1.10 mycroft {
761 1.59 matt char *new, *old;
762 1.53 thorpej u_int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
763 1.36 augustss struct rtentry *rt = rt0;
764 1.10 mycroft
765 1.68 christos if (rt->rt_gateway == NULL || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
766 1.87 christos old = (void *)rt_key(rt);
767 1.87 christos R_Malloc(new, void *, dlen + glen);
768 1.68 christos if (new == NULL)
769 1.10 mycroft return 1;
770 1.32 itojun Bzero(new, dlen + glen);
771 1.10 mycroft rt->rt_nodes->rn_key = new;
772 1.10 mycroft } else {
773 1.66 christos new = __UNCONST(rt->rt_nodes->rn_key); /*XXXUNCONST*/
774 1.68 christos old = NULL;
775 1.10 mycroft }
776 1.10 mycroft Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
777 1.10 mycroft if (old) {
778 1.10 mycroft Bcopy(dst, new, dlen);
779 1.10 mycroft Free(old);
780 1.10 mycroft }
781 1.10 mycroft if (rt->rt_gwroute) {
782 1.68 christos RTFREE(rt->rt_gwroute);
783 1.68 christos rt->rt_gwroute = NULL;
784 1.10 mycroft }
785 1.10 mycroft if (rt->rt_flags & RTF_GATEWAY) {
786 1.10 mycroft rt->rt_gwroute = rtalloc1(gate, 1);
787 1.27 matt /*
788 1.27 matt * If we switched gateways, grab the MTU from the new
789 1.47 itojun * gateway route if the current MTU, if the current MTU is
790 1.47 itojun * greater than the MTU of gateway.
791 1.47 itojun * Note that, if the MTU of gateway is 0, we will reset the
792 1.47 itojun * MTU of the route to run PMTUD again from scratch. XXX
793 1.27 matt */
794 1.27 matt if (rt->rt_gwroute
795 1.27 matt && !(rt->rt_rmx.rmx_locks & RTV_MTU)
796 1.47 itojun && rt->rt_rmx.rmx_mtu
797 1.47 itojun && rt->rt_rmx.rmx_mtu > rt->rt_gwroute->rt_rmx.rmx_mtu) {
798 1.27 matt rt->rt_rmx.rmx_mtu = rt->rt_gwroute->rt_rmx.rmx_mtu;
799 1.27 matt }
800 1.10 mycroft }
801 1.10 mycroft return 0;
802 1.10 mycroft }
803 1.10 mycroft
804 1.9 mycroft void
805 1.60 matt rt_maskedcopy(const struct sockaddr *src, struct sockaddr *dst,
806 1.60 matt const struct sockaddr *netmask)
807 1.1 cgd {
808 1.66 christos const u_char *cp1 = (const u_char *)src;
809 1.36 augustss u_char *cp2 = (u_char *)dst;
810 1.66 christos const u_char *cp3 = (const u_char *)netmask;
811 1.1 cgd u_char *cplim = cp2 + *cp3;
812 1.1 cgd u_char *cplim2 = cp2 + *cp1;
813 1.1 cgd
814 1.1 cgd *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
815 1.1 cgd cp3 += 2;
816 1.1 cgd if (cplim > cplim2)
817 1.1 cgd cplim = cplim2;
818 1.1 cgd while (cp2 < cplim)
819 1.1 cgd *cp2++ = *cp1++ & *cp3++;
820 1.1 cgd if (cp2 < cplim2)
821 1.66 christos memset(cp2, 0, (unsigned)(cplim2 - cp2));
822 1.1 cgd }
823 1.10 mycroft
824 1.1 cgd /*
825 1.29 sommerfe * Set up or tear down a routing table entry, normally
826 1.1 cgd * for an interface.
827 1.1 cgd */
828 1.9 mycroft int
829 1.60 matt rtinit(struct ifaddr *ifa, int cmd, int flags)
830 1.1 cgd {
831 1.36 augustss struct rtentry *rt;
832 1.36 augustss struct sockaddr *dst, *odst;
833 1.29 sommerfe struct sockaddr_storage deldst;
834 1.68 christos struct rtentry *nrt = NULL;
835 1.1 cgd int error;
836 1.39 itojun struct rt_addrinfo info;
837 1.1 cgd
838 1.1 cgd dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
839 1.1 cgd if (cmd == RTM_DELETE) {
840 1.1 cgd if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
841 1.29 sommerfe /* Delete subnet route for this interface */
842 1.29 sommerfe odst = dst;
843 1.29 sommerfe dst = (struct sockaddr *)&deldst;
844 1.29 sommerfe rt_maskedcopy(odst, dst, ifa->ifa_netmask);
845 1.1 cgd }
846 1.14 christos if ((rt = rtalloc1(dst, 0)) != NULL) {
847 1.1 cgd rt->rt_refcnt--;
848 1.29 sommerfe if (rt->rt_ifa != ifa)
849 1.85 dyoung return (flags & RTF_HOST) ? EHOSTUNREACH
850 1.85 dyoung : ENETUNREACH;
851 1.1 cgd }
852 1.1 cgd }
853 1.44 thorpej memset(&info, 0, sizeof(info));
854 1.39 itojun info.rti_ifa = ifa;
855 1.39 itojun info.rti_flags = flags | ifa->ifa_flags;
856 1.39 itojun info.rti_info[RTAX_DST] = dst;
857 1.39 itojun info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
858 1.39 itojun /*
859 1.39 itojun * XXX here, it seems that we are assuming that ifa_netmask is NULL
860 1.39 itojun * for RTF_HOST. bsdi4 passes NULL explicitly (via intermediate
861 1.39 itojun * variable) when RTF_HOST is 1. still not sure if i can safely
862 1.39 itojun * change it to meet bsdi4 behavior.
863 1.39 itojun */
864 1.39 itojun info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
865 1.39 itojun error = rtrequest1(cmd, &info, &nrt);
866 1.10 mycroft if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
867 1.10 mycroft rt_newaddrmsg(cmd, ifa, error, nrt);
868 1.10 mycroft if (rt->rt_refcnt <= 0) {
869 1.10 mycroft rt->rt_refcnt++;
870 1.10 mycroft rtfree(rt);
871 1.10 mycroft }
872 1.10 mycroft }
873 1.10 mycroft if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
874 1.10 mycroft rt->rt_refcnt--;
875 1.10 mycroft if (rt->rt_ifa != ifa) {
876 1.17 christos printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
877 1.17 christos rt->rt_ifa);
878 1.10 mycroft if (rt->rt_ifa->ifa_rtrequest)
879 1.39 itojun rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, NULL);
880 1.74 dyoung rt_replace_ifa(rt, ifa);
881 1.10 mycroft rt->rt_ifp = ifa->ifa_ifp;
882 1.10 mycroft if (ifa->ifa_rtrequest)
883 1.39 itojun ifa->ifa_rtrequest(RTM_ADD, rt, NULL);
884 1.10 mycroft }
885 1.10 mycroft rt_newaddrmsg(cmd, ifa, error, nrt);
886 1.1 cgd }
887 1.85 dyoung return error;
888 1.18 kml }
889 1.18 kml
890 1.18 kml /*
891 1.18 kml * Route timer routines. These routes allow functions to be called
892 1.18 kml * for various routes at any time. This is useful in supporting
893 1.18 kml * path MTU discovery and redirect route deletion.
894 1.18 kml *
895 1.18 kml * This is similar to some BSDI internal functions, but it provides
896 1.18 kml * for multiple queues for efficiency's sake...
897 1.18 kml */
898 1.18 kml
899 1.18 kml LIST_HEAD(, rttimer_queue) rttimer_queue_head;
900 1.18 kml static int rt_init_done = 0;
901 1.18 kml
902 1.60 matt #define RTTIMER_CALLOUT(r) do { \
903 1.60 matt if (r->rtt_func != NULL) { \
904 1.60 matt (*r->rtt_func)(r->rtt_rt, r); \
905 1.60 matt } else { \
906 1.60 matt rtrequest((int) RTM_DELETE, \
907 1.60 matt (struct sockaddr *)rt_key(r->rtt_rt), \
908 1.60 matt 0, 0, 0, 0); \
909 1.60 matt } \
910 1.60 matt } while (/*CONSTCOND*/0)
911 1.18 kml
912 1.65 perry /*
913 1.18 kml * Some subtle order problems with domain initialization mean that
914 1.18 kml * we cannot count on this being run from rt_init before various
915 1.18 kml * protocol initializations are done. Therefore, we make sure
916 1.18 kml * that this is run when the first queue is added...
917 1.18 kml */
918 1.18 kml
919 1.65 perry void
920 1.60 matt rt_timer_init(void)
921 1.18 kml {
922 1.18 kml assert(rt_init_done == 0);
923 1.18 kml
924 1.18 kml LIST_INIT(&rttimer_queue_head);
925 1.35 thorpej callout_init(&rt_timer_ch);
926 1.35 thorpej callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
927 1.18 kml rt_init_done = 1;
928 1.18 kml }
929 1.18 kml
930 1.18 kml struct rttimer_queue *
931 1.60 matt rt_timer_queue_create(u_int timeout)
932 1.18 kml {
933 1.18 kml struct rttimer_queue *rtq;
934 1.18 kml
935 1.18 kml if (rt_init_done == 0)
936 1.18 kml rt_timer_init();
937 1.18 kml
938 1.18 kml R_Malloc(rtq, struct rttimer_queue *, sizeof *rtq);
939 1.18 kml if (rtq == NULL)
940 1.85 dyoung return NULL;
941 1.32 itojun Bzero(rtq, sizeof *rtq);
942 1.18 kml
943 1.18 kml rtq->rtq_timeout = timeout;
944 1.37 itojun rtq->rtq_count = 0;
945 1.24 thorpej TAILQ_INIT(&rtq->rtq_head);
946 1.18 kml LIST_INSERT_HEAD(&rttimer_queue_head, rtq, rtq_link);
947 1.18 kml
948 1.85 dyoung return rtq;
949 1.18 kml }
950 1.18 kml
951 1.18 kml void
952 1.60 matt rt_timer_queue_change(struct rttimer_queue *rtq, long timeout)
953 1.18 kml {
954 1.24 thorpej
955 1.18 kml rtq->rtq_timeout = timeout;
956 1.18 kml }
957 1.18 kml
958 1.18 kml void
959 1.60 matt rt_timer_queue_remove_all(struct rttimer_queue *rtq, int destroy)
960 1.18 kml {
961 1.24 thorpej struct rttimer *r;
962 1.18 kml
963 1.24 thorpej while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL) {
964 1.18 kml LIST_REMOVE(r, rtt_link);
965 1.24 thorpej TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
966 1.24 thorpej if (destroy)
967 1.18 kml RTTIMER_CALLOUT(r);
968 1.72 tls /* we are already at splsoftnet */
969 1.22 thorpej pool_put(&rttimer_pool, r);
970 1.37 itojun if (rtq->rtq_count > 0)
971 1.37 itojun rtq->rtq_count--;
972 1.37 itojun else
973 1.55 itojun printf("rt_timer_queue_remove_all: "
974 1.55 itojun "rtq_count reached 0\n");
975 1.18 kml }
976 1.55 itojun }
977 1.55 itojun
978 1.55 itojun void
979 1.60 matt rt_timer_queue_destroy(struct rttimer_queue *rtq, int destroy)
980 1.55 itojun {
981 1.55 itojun
982 1.55 itojun rt_timer_queue_remove_all(rtq, destroy);
983 1.18 kml
984 1.18 kml LIST_REMOVE(rtq, rtq_link);
985 1.22 thorpej
986 1.22 thorpej /*
987 1.22 thorpej * Caller is responsible for freeing the rttimer_queue structure.
988 1.22 thorpej */
989 1.18 kml }
990 1.18 kml
991 1.37 itojun unsigned long
992 1.60 matt rt_timer_count(struct rttimer_queue *rtq)
993 1.37 itojun {
994 1.37 itojun return rtq->rtq_count;
995 1.37 itojun }
996 1.37 itojun
997 1.65 perry void
998 1.60 matt rt_timer_remove_all(struct rtentry *rt, int destroy)
999 1.18 kml {
1000 1.24 thorpej struct rttimer *r;
1001 1.18 kml
1002 1.24 thorpej while ((r = LIST_FIRST(&rt->rt_timer)) != NULL) {
1003 1.18 kml LIST_REMOVE(r, rtt_link);
1004 1.24 thorpej TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1005 1.54 itojun if (destroy)
1006 1.54 itojun RTTIMER_CALLOUT(r);
1007 1.37 itojun if (r->rtt_queue->rtq_count > 0)
1008 1.37 itojun r->rtt_queue->rtq_count--;
1009 1.37 itojun else
1010 1.37 itojun printf("rt_timer_remove_all: rtq_count reached 0\n");
1011 1.72 tls /* we are already at splsoftnet */
1012 1.38 itojun pool_put(&rttimer_pool, r);
1013 1.18 kml }
1014 1.18 kml }
1015 1.18 kml
1016 1.65 perry int
1017 1.60 matt rt_timer_add(struct rtentry *rt,
1018 1.60 matt void (*func)(struct rtentry *, struct rttimer *),
1019 1.60 matt struct rttimer_queue *queue)
1020 1.18 kml {
1021 1.24 thorpej struct rttimer *r;
1022 1.72 tls int s;
1023 1.18 kml
1024 1.24 thorpej /*
1025 1.24 thorpej * If there's already a timer with this action, destroy it before
1026 1.24 thorpej * we add a new one.
1027 1.24 thorpej */
1028 1.85 dyoung LIST_FOREACH(r, &rt->rt_timer, rtt_link) {
1029 1.85 dyoung if (r->rtt_func == func)
1030 1.85 dyoung break;
1031 1.85 dyoung }
1032 1.85 dyoung if (r != NULL) {
1033 1.85 dyoung LIST_REMOVE(r, rtt_link);
1034 1.85 dyoung TAILQ_REMOVE(&r->rtt_queue->rtq_head, r, rtt_next);
1035 1.85 dyoung if (r->rtt_queue->rtq_count > 0)
1036 1.85 dyoung r->rtt_queue->rtq_count--;
1037 1.85 dyoung else
1038 1.85 dyoung printf("rt_timer_add: rtq_count reached 0\n");
1039 1.85 dyoung } else {
1040 1.85 dyoung s = splsoftnet();
1041 1.85 dyoung r = pool_get(&rttimer_pool, PR_NOWAIT);
1042 1.85 dyoung splx(s);
1043 1.85 dyoung if (r == NULL)
1044 1.85 dyoung return ENOBUFS;
1045 1.18 kml }
1046 1.18 kml
1047 1.85 dyoung memset(r, 0, sizeof(*r));
1048 1.24 thorpej
1049 1.24 thorpej r->rtt_rt = rt;
1050 1.70 kardel r->rtt_time = time_uptime;
1051 1.24 thorpej r->rtt_func = func;
1052 1.24 thorpej r->rtt_queue = queue;
1053 1.24 thorpej LIST_INSERT_HEAD(&rt->rt_timer, r, rtt_link);
1054 1.24 thorpej TAILQ_INSERT_TAIL(&queue->rtq_head, r, rtt_next);
1055 1.37 itojun r->rtt_queue->rtq_count++;
1056 1.65 perry
1057 1.24 thorpej return (0);
1058 1.18 kml }
1059 1.18 kml
1060 1.18 kml /* ARGSUSED */
1061 1.18 kml void
1062 1.76 christos rt_timer_timer(void *arg)
1063 1.18 kml {
1064 1.24 thorpej struct rttimer_queue *rtq;
1065 1.24 thorpej struct rttimer *r;
1066 1.24 thorpej int s;
1067 1.21 kml
1068 1.24 thorpej s = splsoftnet();
1069 1.85 dyoung LIST_FOREACH(rtq, &rttimer_queue_head, rtq_link) {
1070 1.24 thorpej while ((r = TAILQ_FIRST(&rtq->rtq_head)) != NULL &&
1071 1.70 kardel (r->rtt_time + rtq->rtq_timeout) < time_uptime) {
1072 1.24 thorpej LIST_REMOVE(r, rtt_link);
1073 1.24 thorpej TAILQ_REMOVE(&rtq->rtq_head, r, rtt_next);
1074 1.24 thorpej RTTIMER_CALLOUT(r);
1075 1.24 thorpej pool_put(&rttimer_pool, r);
1076 1.37 itojun if (rtq->rtq_count > 0)
1077 1.37 itojun rtq->rtq_count--;
1078 1.37 itojun else
1079 1.37 itojun printf("rt_timer_timer: rtq_count reached 0\n");
1080 1.18 kml }
1081 1.18 kml }
1082 1.24 thorpej splx(s);
1083 1.18 kml
1084 1.35 thorpej callout_reset(&rt_timer_ch, hz, rt_timer_timer, NULL);
1085 1.1 cgd }
1086 1.83 joerg
1087 1.84 joerg #ifdef RTCACHE_DEBUG
1088 1.84 joerg #ifndef RTCACHE_DEBUG_SIZE
1089 1.84 joerg #define RTCACHE_DEBUG_SIZE (1024 * 1024)
1090 1.84 joerg #endif
1091 1.84 joerg static const char *cache_caller[RTCACHE_DEBUG_SIZE];
1092 1.84 joerg static struct route *cache_entry[RTCACHE_DEBUG_SIZE];
1093 1.84 joerg size_t cache_cur;
1094 1.84 joerg #endif
1095 1.84 joerg
1096 1.84 joerg #ifdef RTCACHE_DEBUG
1097 1.84 joerg static void
1098 1.84 joerg _rtcache_init_debug(const char *caller, struct route *ro, int flag)
1099 1.84 joerg #else
1100 1.84 joerg static void
1101 1.84 joerg _rtcache_init(struct route *ro, int flag)
1102 1.84 joerg #endif
1103 1.84 joerg {
1104 1.84 joerg #ifdef RTCACHE_DEBUG
1105 1.84 joerg size_t i;
1106 1.84 joerg for (i = 0; i < cache_cur; ++i) {
1107 1.84 joerg if (cache_entry[i] == ro)
1108 1.84 joerg panic("Reinit of route %p, initialised from %s", ro, cache_caller[i]);
1109 1.84 joerg }
1110 1.84 joerg #endif
1111 1.84 joerg
1112 1.86 dyoung ro->ro_rt = rtalloc1(rtcache_getdst(ro), flag);
1113 1.84 joerg if (ro->ro_rt != NULL) {
1114 1.84 joerg #ifdef RTCACHE_DEBUG
1115 1.84 joerg if (cache_cur == RTCACHE_DEBUG_SIZE)
1116 1.84 joerg panic("Route cache debug overflow");
1117 1.84 joerg cache_caller[cache_cur] = caller;
1118 1.84 joerg cache_entry[cache_cur] = ro;
1119 1.84 joerg ++cache_cur;
1120 1.84 joerg #endif
1121 1.84 joerg rtcache(ro);
1122 1.84 joerg }
1123 1.84 joerg }
1124 1.84 joerg
1125 1.84 joerg #ifdef RTCACHE_DEBUG
1126 1.84 joerg void
1127 1.84 joerg rtcache_init_debug(const char *caller, struct route *ro)
1128 1.84 joerg {
1129 1.84 joerg _rtcache_init_debug(caller, ro, 1);
1130 1.84 joerg }
1131 1.84 joerg
1132 1.84 joerg void
1133 1.84 joerg rtcache_init_noclone_debug(const char *caller, struct route *ro)
1134 1.84 joerg {
1135 1.84 joerg _rtcache_init_debug(caller, ro, 0);
1136 1.84 joerg }
1137 1.84 joerg
1138 1.84 joerg #else
1139 1.83 joerg void
1140 1.83 joerg rtcache_init(struct route *ro)
1141 1.83 joerg {
1142 1.84 joerg _rtcache_init(ro, 1);
1143 1.83 joerg }
1144 1.83 joerg
1145 1.83 joerg void
1146 1.83 joerg rtcache_init_noclone(struct route *ro)
1147 1.83 joerg {
1148 1.84 joerg _rtcache_init(ro, 0);
1149 1.83 joerg }
1150 1.84 joerg #endif
1151 1.83 joerg
1152 1.84 joerg #ifdef RTCACHE_DEBUG
1153 1.84 joerg void
1154 1.86 dyoung rtcache_copy_debug(const char *caller, struct route *new_ro, const struct route *old_ro, size_t new_len)
1155 1.84 joerg #else
1156 1.83 joerg void
1157 1.86 dyoung rtcache_copy(struct route *new_ro, const struct route *old_ro, size_t new_len)
1158 1.84 joerg #endif
1159 1.83 joerg {
1160 1.84 joerg #ifdef RTCACHE_DEBUG
1161 1.84 joerg size_t i;
1162 1.84 joerg
1163 1.84 joerg for (i = 0; i < cache_cur; ++i) {
1164 1.86 dyoung if (cache_entry[i] == new_ro)
1165 1.86 dyoung panic("Copy to initalised route %p (before %s)", new_ro, cache_caller[i]);
1166 1.84 joerg }
1167 1.84 joerg #endif
1168 1.84 joerg
1169 1.86 dyoung memset(new_ro, 0, new_len);
1170 1.86 dyoung #if 0
1171 1.86 dyoung if (old_ro->ro_sa != NULL)
1172 1.86 dyoung new_ro->ro_sa = sockaddr_dup(old_ro->ro_sa);
1173 1.86 dyoung #else
1174 1.86 dyoung if (old_ro->ro_dst.sa_len + offsetof(struct route, ro_dst) > new_len)
1175 1.83 joerg panic("rtcache_copy: dst address will overflow new route");
1176 1.86 dyoung memcpy(&new_ro->ro_dst, &old_ro->ro_dst, old_ro->ro_dst.sa_len);
1177 1.86 dyoung #endif
1178 1.86 dyoung new_ro->ro_rt = old_ro->ro_rt;
1179 1.86 dyoung if (new_ro->ro_rt != NULL) {
1180 1.84 joerg #ifdef RTCACHE_DEBUG
1181 1.84 joerg if (cache_cur == RTCACHE_DEBUG_SIZE)
1182 1.84 joerg panic("Route cache debug overflow");
1183 1.84 joerg cache_caller[cache_cur] = caller;
1184 1.86 dyoung cache_entry[cache_cur] = new_ro;
1185 1.84 joerg ++cache_cur;
1186 1.84 joerg #endif
1187 1.86 dyoung rtcache(new_ro);
1188 1.86 dyoung ++new_ro->ro_rt->rt_refcnt;
1189 1.83 joerg }
1190 1.83 joerg }
1191 1.83 joerg
1192 1.89 xtraeme void
1193 1.86 dyoung rtcache_clear(struct route *ro)
1194 1.83 joerg {
1195 1.84 joerg #ifdef RTCACHE_DEBUG
1196 1.84 joerg size_t j, i = cache_cur;
1197 1.84 joerg for (i = j = 0; i < cache_cur; ++i, ++j) {
1198 1.84 joerg if (cache_entry[i] == ro) {
1199 1.84 joerg if (ro->ro_rt == NULL)
1200 1.84 joerg panic("Route cache manipulated (allocated by %s)", cache_caller[i]);
1201 1.84 joerg --j;
1202 1.84 joerg } else {
1203 1.84 joerg cache_caller[j] = cache_caller[i];
1204 1.84 joerg cache_entry[j] = cache_entry[i];
1205 1.84 joerg }
1206 1.84 joerg }
1207 1.83 joerg if (ro->ro_rt != NULL) {
1208 1.84 joerg if (i != j + 1)
1209 1.84 joerg panic("Wrong entries after rtcache_free: %zu (expected %zu)", j, i - 1);
1210 1.84 joerg --cache_cur;
1211 1.84 joerg }
1212 1.84 joerg #endif
1213 1.84 joerg
1214 1.84 joerg if (ro->ro_rt != NULL)
1215 1.83 joerg rtflush(ro);
1216 1.83 joerg ro->ro_rt = NULL;
1217 1.83 joerg }
1218 1.83 joerg
1219 1.83 joerg void
1220 1.86 dyoung rtcache_free(struct route *ro)
1221 1.86 dyoung {
1222 1.86 dyoung rtcache_clear(ro);
1223 1.86 dyoung #if 0
1224 1.86 dyoung if (ro->ro_sa != NULL) {
1225 1.86 dyoung sockaddr_free(ro->ro_sa);
1226 1.86 dyoung ro->ro_sa = NULL;
1227 1.86 dyoung }
1228 1.86 dyoung #endif
1229 1.86 dyoung }
1230 1.86 dyoung
1231 1.86 dyoung void
1232 1.83 joerg rtcache_update(struct route *ro)
1233 1.83 joerg {
1234 1.86 dyoung rtcache_clear(ro);
1235 1.83 joerg rtcache_init(ro);
1236 1.83 joerg }
1237