]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. | |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
14 | * | |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
22 | * | |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1980, 1986, 1991, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | * | |
57 | * @(#)route.c 8.2 (Berkeley) 11/15/93 | |
58 | * $FreeBSD: src/sys/net/route.c,v 1.59.2.3 2001/07/29 19:18:02 ume Exp $ | |
59 | */ | |
60 | ||
61 | #include <sys/param.h> | |
62 | #include <sys/systm.h> | |
63 | #include <sys/malloc.h> | |
64 | #include <sys/mbuf.h> | |
65 | #include <sys/socket.h> | |
66 | #include <sys/domain.h> | |
67 | #include <sys/syslog.h> | |
68 | ||
69 | #include <net/if.h> | |
70 | #include <net/route.h> | |
71 | ||
72 | #include <netinet/in.h> | |
73 | #include <netinet/ip_mroute.h> | |
74 | ||
75 | #include <net/if_dl.h> | |
76 | ||
77 | #define SA(p) ((struct sockaddr *)(p)) | |
78 | ||
79 | struct route_cb route_cb; | |
80 | static struct rtstat rtstat; | |
81 | struct radix_node_head *rt_tables[AF_MAX+1]; | |
82 | ||
83 | static int rttrash; /* routes not in table but not freed */ | |
84 | ||
85 | static void rt_maskedcopy __P((struct sockaddr *, | |
86 | struct sockaddr *, struct sockaddr *)); | |
87 | static void rtable_init __P((void **)); | |
88 | ||
89 | __private_extern__ u_long route_generation = 0; | |
90 | extern int use_routegenid; | |
91 | ||
92 | ||
93 | static void | |
94 | rtable_init(table) | |
95 | void **table; | |
96 | { | |
97 | struct domain *dom; | |
98 | for (dom = domains; dom; dom = dom->dom_next) | |
99 | if (dom->dom_rtattach) | |
100 | dom->dom_rtattach(&table[dom->dom_family], | |
101 | dom->dom_rtoffset); | |
102 | } | |
103 | ||
104 | void | |
105 | route_init() | |
106 | { | |
107 | rn_init(); /* initialize all zeroes, all ones, mask table */ | |
108 | rtable_init((void **)rt_tables); | |
109 | } | |
110 | ||
111 | /* | |
112 | * Packet routing routines. | |
113 | */ | |
114 | void | |
115 | rtalloc(ro) | |
116 | register struct route *ro; | |
117 | { | |
118 | rtalloc_ign(ro, 0UL); | |
119 | } | |
120 | ||
121 | void | |
122 | rtalloc_ign(ro, ignore) | |
123 | register struct route *ro; | |
124 | u_long ignore; | |
125 | { | |
126 | struct rtentry *rt; | |
127 | int s; | |
128 | ||
129 | if ((rt = ro->ro_rt) != NULL) { | |
130 | if (rt->rt_ifp != NULL && rt->rt_flags & RTF_UP) | |
131 | return; | |
132 | /* XXX - We are probably always at splnet here already. */ | |
133 | s = splnet(); | |
134 | rtfree(rt); | |
135 | ro->ro_rt = NULL; | |
136 | splx(s); | |
137 | } | |
138 | ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore); | |
139 | if (ro->ro_rt) | |
140 | ro->ro_rt->generation_id = route_generation; | |
141 | } | |
142 | ||
143 | /* | |
144 | * Look up the route that matches the address given | |
145 | * Or, at least try.. Create a cloned route if needed. | |
146 | */ | |
147 | struct rtentry * | |
148 | rtalloc1(dst, report, ignflags) | |
149 | register struct sockaddr *dst; | |
150 | int report; | |
151 | u_long ignflags; | |
152 | { | |
153 | register struct radix_node_head *rnh = rt_tables[dst->sa_family]; | |
154 | register struct rtentry *rt; | |
155 | register struct radix_node *rn; | |
156 | struct rtentry *newrt = 0; | |
157 | struct rt_addrinfo info; | |
158 | u_long nflags; | |
159 | int s = splnet(), err = 0, msgtype = RTM_MISS; | |
160 | ||
161 | /* | |
162 | * Look up the address in the table for that Address Family | |
163 | */ | |
164 | if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) && | |
165 | ((rn->rn_flags & RNF_ROOT) == 0)) { | |
166 | /* | |
167 | * If we find it and it's not the root node, then | |
168 | * get a refernce on the rtentry associated. | |
169 | */ | |
170 | newrt = rt = (struct rtentry *)rn; | |
171 | nflags = rt->rt_flags & ~ignflags; | |
172 | if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) { | |
173 | /* | |
174 | * We are apparently adding (report = 0 in delete). | |
175 | * If it requires that it be cloned, do so. | |
176 | * (This implies it wasn't a HOST route.) | |
177 | */ | |
178 | err = rtrequest(RTM_RESOLVE, dst, SA(0), | |
179 | SA(0), 0, &newrt); | |
180 | if (err) { | |
181 | /* | |
182 | * If the cloning didn't succeed, maybe | |
183 | * what we have will do. Return that. | |
184 | */ | |
185 | newrt = rt; | |
186 | rtref(rt); | |
187 | goto miss; | |
188 | } | |
189 | if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) { | |
190 | /* | |
191 | * If the new route specifies it be | |
192 | * externally resolved, then go do that. | |
193 | */ | |
194 | msgtype = RTM_RESOLVE; | |
195 | goto miss; | |
196 | } | |
197 | } else | |
198 | rtref(rt); | |
199 | } else { | |
200 | /* | |
201 | * Either we hit the root or couldn't find any match, | |
202 | * Which basically means | |
203 | * "caint get there frm here" | |
204 | */ | |
205 | rtstat.rts_unreach++; | |
206 | miss: if (report) { | |
207 | /* | |
208 | * If required, report the failure to the supervising | |
209 | * Authorities. | |
210 | * For a delete, this is not an error. (report == 0) | |
211 | */ | |
212 | bzero((caddr_t)&info, sizeof(info)); | |
213 | info.rti_info[RTAX_DST] = dst; | |
214 | rt_missmsg(msgtype, &info, 0, err); | |
215 | } | |
216 | } | |
217 | splx(s); | |
218 | return (newrt); | |
219 | } | |
220 | ||
221 | /* | |
222 | * Remove a reference count from an rtentry. | |
223 | * If the count gets low enough, take it out of the routing table | |
224 | */ | |
225 | void | |
226 | rtfree(rt) | |
227 | register struct rtentry *rt; | |
228 | { | |
229 | /* | |
230 | * find the tree for that address family | |
231 | * Note: in the case of igmp packets, there might not be an rnh | |
232 | */ | |
233 | register struct radix_node_head *rnh = | |
234 | rt_tables[rt_key(rt)->sa_family]; | |
235 | ||
236 | if (rt == 0) | |
237 | panic("rtfree"); | |
238 | ||
239 | /* | |
240 | * decrement the reference count by one and if it reaches 0, | |
241 | * and there is a close function defined, call the close function | |
242 | */ | |
243 | rt->rt_refcnt--; | |
244 | if(rnh && rnh->rnh_close && rt->rt_refcnt == 0) { | |
245 | rnh->rnh_close((struct radix_node *)rt, rnh); | |
246 | } | |
247 | ||
248 | /* | |
249 | * If we are no longer "up" (and ref == 0) | |
250 | * then we can free the resources associated | |
251 | * with the route. | |
252 | */ | |
253 | if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) { | |
254 | if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT)) | |
255 | panic ("rtfree 2"); | |
256 | /* | |
257 | * the rtentry must have been removed from the routing table | |
258 | * so it is represented in rttrash.. remove that now. | |
259 | */ | |
260 | rttrash--; | |
261 | ||
262 | #ifdef DIAGNOSTIC | |
263 | if (rt->rt_refcnt < 0) { | |
264 | printf("rtfree: %p not freed (neg refs)\n", rt); | |
265 | return; | |
266 | } | |
267 | #endif | |
268 | ||
269 | /* | |
270 | * release references on items we hold them on.. | |
271 | * e.g other routes and ifaddrs. | |
272 | */ | |
273 | if (rt->rt_parent) | |
274 | rtfree(rt->rt_parent); | |
275 | ||
276 | if(rt->rt_ifa && !(rt->rt_parent && rt->rt_parent->rt_ifa == rt->rt_ifa)) { | |
277 | /* | |
278 | * Only release the ifa if our parent doesn't hold it for us. | |
279 | * The parent route is responsible for holding a reference | |
280 | * to the ifa for us. Ifa refcounts are 16bit, if every | |
281 | * cloned route held a reference, the 16bit refcount may | |
282 | * rollover, making a mess :( | |
283 | * | |
284 | * FreeBSD solved this by making the ifa_refcount 32bits, but | |
285 | * we can't do that since it changes the size of the ifaddr struct. | |
286 | */ | |
287 | ifafree(rt->rt_ifa); | |
288 | } | |
289 | ||
290 | /* | |
291 | * The key is separatly alloc'd so free it (see rt_setgate()). | |
292 | * This also frees the gateway, as they are always malloc'd | |
293 | * together. | |
294 | */ | |
295 | Free(rt_key(rt)); | |
296 | ||
297 | /* | |
298 | * and the rtentry itself of course | |
299 | */ | |
300 | Free(rt); | |
301 | } | |
302 | } | |
303 | ||
304 | /* | |
305 | * Decrements the refcount but does not free the route when | |
306 | * the refcount reaches zero. Unless you have really good reason, | |
307 | * use rtfree not rtunref. | |
308 | */ | |
309 | void | |
310 | rtunref(struct rtentry* rt) | |
311 | { | |
312 | if (rt == NULL) | |
313 | panic("rtunref"); | |
314 | rt->rt_refcnt--; | |
315 | #if DEBUG | |
316 | if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) | |
317 | printf("rtunref - if rtfree were called, we would have freed route\n"); | |
318 | #endif | |
319 | } | |
320 | ||
321 | /* | |
322 | * Add a reference count from an rtentry. | |
323 | */ | |
324 | void | |
325 | rtref(struct rtentry* rt) | |
326 | { | |
327 | if (rt == NULL) | |
328 | panic("rtref"); | |
329 | ||
330 | rt->rt_refcnt++; | |
331 | } | |
332 | ||
333 | void | |
334 | rtsetifa(struct rtentry *rt, struct ifaddr* ifa) | |
335 | { | |
336 | if (rt == NULL) | |
337 | panic("rtsetifa"); | |
338 | ||
339 | if (rt->rt_ifa == ifa) | |
340 | return; | |
341 | ||
342 | /* Release the old ifa if it isn't our parent route's ifa */ | |
343 | if (rt->rt_ifa && !(rt->rt_parent && rt->rt_parent->rt_ifa == rt->rt_ifa)) | |
344 | ifafree(rt->rt_ifa); | |
345 | ||
346 | /* Set rt_ifa */ | |
347 | rt->rt_ifa = ifa; | |
348 | ||
349 | /* Take a reference to the ifa if it isn't our parent route's ifa */ | |
350 | if (rt->rt_ifa && !(rt->rt_parent && rt->rt_parent->rt_ifa == ifa)) | |
351 | ifaref(rt->rt_ifa); | |
352 | } | |
353 | ||
354 | void | |
355 | ifafree(ifa) | |
356 | register struct ifaddr *ifa; | |
357 | { | |
358 | if (ifa == NULL) | |
359 | panic("ifafree"); | |
360 | if (ifa->ifa_refcnt == 0) { | |
361 | #ifdef __APPLE__ | |
362 | /* Detect case where an ifa is being freed before it should */ | |
363 | struct ifnet* ifp; | |
364 | /* Verify this ifa isn't attached to an interface */ | |
365 | for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next) { | |
366 | struct ifaddr *ifaInUse; | |
367 | for (ifaInUse = ifp->if_addrhead.tqh_first; ifaInUse; ifaInUse = ifaInUse->ifa_link.tqe_next) { | |
368 | if (ifa == ifaInUse) { | |
369 | /* | |
370 | * This is an ugly hack done because we can't move to a 32 bit | |
371 | * refcnt like bsd has. We have to maintain binary compatibility | |
372 | * in our kernel, unlike FreeBSD. | |
373 | */ | |
374 | log(LOG_ERR, "ifa attached to ifp is being freed, leaking insted\n"); | |
375 | return; | |
376 | } | |
377 | } | |
378 | } | |
379 | #endif | |
380 | FREE(ifa, M_IFADDR); | |
381 | } | |
382 | else | |
383 | ifa->ifa_refcnt--; | |
384 | } | |
385 | ||
386 | #ifdef __APPLE__ | |
387 | void | |
388 | ifaref(struct ifaddr *ifa) | |
389 | { | |
390 | if (ifa == NULL) | |
391 | panic("ifaref"); | |
392 | ifa->ifa_refcnt++; | |
393 | } | |
394 | #endif | |
395 | ||
396 | /* | |
397 | * Force a routing table entry to the specified | |
398 | * destination to go through the given gateway. | |
399 | * Normally called as a result of a routing redirect | |
400 | * message from the network layer. | |
401 | * | |
402 | * N.B.: must be called at splnet | |
403 | * | |
404 | */ | |
405 | void | |
406 | rtredirect(dst, gateway, netmask, flags, src, rtp) | |
407 | struct sockaddr *dst, *gateway, *netmask, *src; | |
408 | int flags; | |
409 | struct rtentry **rtp; | |
410 | { | |
411 | register struct rtentry *rt; | |
412 | int error = 0; | |
413 | short *stat = 0; | |
414 | struct rt_addrinfo info; | |
415 | struct ifaddr *ifa; | |
416 | ||
417 | /* verify the gateway is directly reachable */ | |
418 | if ((ifa = ifa_ifwithnet(gateway)) == 0) { | |
419 | error = ENETUNREACH; | |
420 | goto out; | |
421 | } | |
422 | rt = rtalloc1(dst, 0, 0UL); | |
423 | /* | |
424 | * If the redirect isn't from our current router for this dst, | |
425 | * it's either old or wrong. If it redirects us to ourselves, | |
426 | * we have a routing loop, perhaps as a result of an interface | |
427 | * going down recently. | |
428 | */ | |
429 | #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0) | |
430 | if (!(flags & RTF_DONE) && rt && | |
431 | (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) | |
432 | error = EINVAL; | |
433 | else if (ifa_ifwithaddr(gateway)) | |
434 | error = EHOSTUNREACH; | |
435 | if (error) | |
436 | goto done; | |
437 | /* | |
438 | * Create a new entry if we just got back a wildcard entry | |
439 | * or the the lookup failed. This is necessary for hosts | |
440 | * which use routing redirects generated by smart gateways | |
441 | * to dynamically build the routing tables. | |
442 | */ | |
443 | if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2)) | |
444 | goto create; | |
445 | /* | |
446 | * Don't listen to the redirect if it's | |
447 | * for a route to an interface. | |
448 | */ | |
449 | if (rt->rt_flags & RTF_GATEWAY) { | |
450 | if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) { | |
451 | /* | |
452 | * Changing from route to net => route to host. | |
453 | * Create new route, rather than smashing route to net. | |
454 | */ | |
455 | create: | |
456 | flags |= RTF_GATEWAY | RTF_DYNAMIC; | |
457 | error = rtrequest((int)RTM_ADD, dst, gateway, | |
458 | netmask, flags, | |
459 | (struct rtentry **)0); | |
460 | stat = &rtstat.rts_dynamic; | |
461 | } else { | |
462 | /* | |
463 | * Smash the current notion of the gateway to | |
464 | * this destination. Should check about netmask!!! | |
465 | */ | |
466 | rt->rt_flags |= RTF_MODIFIED; | |
467 | flags |= RTF_MODIFIED; | |
468 | stat = &rtstat.rts_newgateway; | |
469 | /* | |
470 | * add the key and gateway (in one malloc'd chunk). | |
471 | */ | |
472 | rt_setgate(rt, rt_key(rt), gateway); | |
473 | } | |
474 | } else | |
475 | error = EHOSTUNREACH; | |
476 | done: | |
477 | if (rt) { | |
478 | if (rtp && !error) | |
479 | *rtp = rt; | |
480 | else | |
481 | rtfree(rt); | |
482 | } | |
483 | out: | |
484 | if (error) | |
485 | rtstat.rts_badredirect++; | |
486 | else if (stat != NULL) | |
487 | (*stat)++; | |
488 | bzero((caddr_t)&info, sizeof(info)); | |
489 | info.rti_info[RTAX_DST] = dst; | |
490 | info.rti_info[RTAX_GATEWAY] = gateway; | |
491 | info.rti_info[RTAX_NETMASK] = netmask; | |
492 | info.rti_info[RTAX_AUTHOR] = src; | |
493 | rt_missmsg(RTM_REDIRECT, &info, flags, error); | |
494 | } | |
495 | ||
496 | /* | |
497 | * Routing table ioctl interface. | |
498 | */ | |
499 | int | |
500 | rtioctl(req, data, p) | |
501 | int req; | |
502 | caddr_t data; | |
503 | struct proc *p; | |
504 | { | |
505 | #if INET | |
506 | /* Multicast goop, grrr... */ | |
507 | #if MROUTING | |
508 | return mrt_ioctl(req, data); | |
509 | #else | |
510 | return mrt_ioctl(req, data, p); | |
511 | #endif | |
512 | #else /* INET */ | |
513 | return ENXIO; | |
514 | #endif /* INET */ | |
515 | } | |
516 | ||
517 | struct ifaddr * | |
518 | ifa_ifwithroute(flags, dst, gateway) | |
519 | int flags; | |
520 | struct sockaddr *dst, *gateway; | |
521 | { | |
522 | register struct ifaddr *ifa; | |
523 | if ((flags & RTF_GATEWAY) == 0) { | |
524 | /* | |
525 | * If we are adding a route to an interface, | |
526 | * and the interface is a pt to pt link | |
527 | * we should search for the destination | |
528 | * as our clue to the interface. Otherwise | |
529 | * we can use the local address. | |
530 | */ | |
531 | ifa = 0; | |
532 | if (flags & RTF_HOST) { | |
533 | ifa = ifa_ifwithdstaddr(dst); | |
534 | } | |
535 | if (ifa == 0) | |
536 | ifa = ifa_ifwithaddr(gateway); | |
537 | } else { | |
538 | /* | |
539 | * If we are adding a route to a remote net | |
540 | * or host, the gateway may still be on the | |
541 | * other end of a pt to pt link. | |
542 | */ | |
543 | ifa = ifa_ifwithdstaddr(gateway); | |
544 | } | |
545 | if (ifa == 0) | |
546 | ifa = ifa_ifwithnet(gateway); | |
547 | if (ifa == 0) { | |
548 | struct rtentry *rt = rtalloc1(dst, 0, 0UL); | |
549 | if (rt == 0) | |
550 | return (0); | |
551 | rtunref(rt); | |
552 | if ((ifa = rt->rt_ifa) == 0) | |
553 | return (0); | |
554 | } | |
555 | if (ifa->ifa_addr->sa_family != dst->sa_family) { | |
556 | struct ifaddr *oifa = ifa; | |
557 | ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp); | |
558 | if (ifa == 0) | |
559 | ifa = oifa; | |
560 | } | |
561 | return (ifa); | |
562 | } | |
563 | ||
564 | #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | |
565 | ||
566 | static int rt_fixdelete __P((struct radix_node *, void *)); | |
567 | static int rt_fixchange __P((struct radix_node *, void *)); | |
568 | ||
569 | struct rtfc_arg { | |
570 | struct rtentry *rt0; | |
571 | struct radix_node_head *rnh; | |
572 | }; | |
573 | ||
574 | /* | |
575 | * Do appropriate manipulations of a routing tree given | |
576 | * all the bits of info needed | |
577 | */ | |
578 | int | |
579 | rtrequest(req, dst, gateway, netmask, flags, ret_nrt) | |
580 | int req, flags; | |
581 | struct sockaddr *dst, *gateway, *netmask; | |
582 | struct rtentry **ret_nrt; | |
583 | { | |
584 | int s = splnet(); int error = 0; | |
585 | register struct rtentry *rt; | |
586 | register struct radix_node *rn; | |
587 | register struct radix_node_head *rnh; | |
588 | struct ifaddr *ifa; | |
589 | struct sockaddr *ndst; | |
590 | #define senderr(x) { error = x ; goto bad; } | |
591 | ||
592 | /* | |
593 | * Find the correct routing tree to use for this Address Family | |
594 | */ | |
595 | if ((rnh = rt_tables[dst->sa_family]) == 0) | |
596 | senderr(ESRCH); | |
597 | /* | |
598 | * If we are adding a host route then we don't want to put | |
599 | * a netmask in the tree | |
600 | */ | |
601 | if (flags & RTF_HOST) | |
602 | netmask = 0; | |
603 | switch (req) { | |
604 | case RTM_DELETE: | |
605 | /* | |
606 | * Remove the item from the tree and return it. | |
607 | * Complain if it is not there and do no more processing. | |
608 | */ | |
609 | if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0) | |
610 | senderr(ESRCH); | |
611 | if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)) | |
612 | panic ("rtrequest delete"); | |
613 | rt = (struct rtentry *)rn; | |
614 | ||
615 | /* | |
616 | * Now search what's left of the subtree for any cloned | |
617 | * routes which might have been formed from this node. | |
618 | */ | |
619 | if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) && | |
620 | rt_mask(rt)) { | |
621 | rnh->rnh_walktree_from(rnh, dst, rt_mask(rt), | |
622 | rt_fixdelete, rt); | |
623 | } | |
624 | ||
625 | /* | |
626 | * Remove any external references we may have. | |
627 | * This might result in another rtentry being freed if | |
628 | * we held its last reference. | |
629 | */ | |
630 | if (rt->rt_gwroute) { | |
631 | rt = rt->rt_gwroute; | |
632 | rtfree(rt); | |
633 | (rt = (struct rtentry *)rn)->rt_gwroute = 0; | |
634 | } | |
635 | ||
636 | /* | |
637 | * NB: RTF_UP must be set during the search above, | |
638 | * because we might delete the last ref, causing | |
639 | * rt to get freed prematurely. | |
640 | * eh? then why not just add a reference? | |
641 | * I'm not sure how RTF_UP helps matters. (JRE) | |
642 | */ | |
643 | rt->rt_flags &= ~RTF_UP; | |
644 | ||
645 | /* | |
646 | * give the protocol a chance to keep things in sync. | |
647 | */ | |
648 | if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest) | |
649 | ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); | |
650 | ||
651 | /* | |
652 | * one more rtentry floating around that is not | |
653 | * linked to the routing table. | |
654 | */ | |
655 | rttrash++; | |
656 | ||
657 | /* | |
658 | * If the caller wants it, then it can have it, | |
659 | * but it's up to it to free the rtentry as we won't be | |
660 | * doing it. | |
661 | */ | |
662 | if (ret_nrt) | |
663 | *ret_nrt = rt; | |
664 | else if (rt->rt_refcnt <= 0) { | |
665 | rt->rt_refcnt++; /* make a 1->0 transition */ | |
666 | rtfree(rt); | |
667 | } | |
668 | break; | |
669 | ||
670 | case RTM_RESOLVE: | |
671 | if (ret_nrt == 0 || (rt = *ret_nrt) == 0) | |
672 | senderr(EINVAL); | |
673 | ifa = rt->rt_ifa; | |
674 | flags = rt->rt_flags & | |
675 | ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC); | |
676 | flags |= RTF_WASCLONED; | |
677 | gateway = rt->rt_gateway; | |
678 | if ((netmask = rt->rt_genmask) == 0) | |
679 | flags |= RTF_HOST; | |
680 | goto makeroute; | |
681 | ||
682 | case RTM_ADD: | |
683 | if ((flags & RTF_GATEWAY) && !gateway) | |
684 | panic("rtrequest: GATEWAY but no gateway"); | |
685 | ||
686 | if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0) | |
687 | senderr(ENETUNREACH); | |
688 | ||
689 | makeroute: | |
690 | R_Malloc(rt, struct rtentry *, sizeof(*rt)); | |
691 | if (rt == 0) | |
692 | senderr(ENOBUFS); | |
693 | Bzero(rt, sizeof(*rt)); | |
694 | rt->rt_flags = RTF_UP | flags; | |
695 | /* | |
696 | * Add the gateway. Possibly re-malloc-ing the storage for it | |
697 | * also add the rt_gwroute if possible. | |
698 | */ | |
699 | if ((error = rt_setgate(rt, dst, gateway)) != 0) { | |
700 | Free(rt); | |
701 | senderr(error); | |
702 | } | |
703 | ||
704 | /* | |
705 | * point to the (possibly newly malloc'd) dest address. | |
706 | */ | |
707 | ndst = rt_key(rt); | |
708 | ||
709 | /* | |
710 | * make sure it contains the value we want (masked if needed). | |
711 | */ | |
712 | if (netmask) { | |
713 | rt_maskedcopy(dst, ndst, netmask); | |
714 | } else | |
715 | Bcopy(dst, ndst, dst->sa_len); | |
716 | ||
717 | /* | |
718 | * Note that we now have a reference to the ifa. | |
719 | * This moved from below so that rnh->rnh_addaddr() can | |
720 | * examine the ifa and ifa->ifa_ifp if it so desires. | |
721 | */ | |
722 | /* | |
723 | * Note that we do not use rtsetifa here because | |
724 | * rt_parent has not been setup yet. | |
725 | */ | |
726 | ifaref(ifa); | |
727 | rt->rt_ifa = ifa; | |
728 | rt->rt_ifp = ifa->ifa_ifp; | |
729 | ||
730 | /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */ | |
731 | ||
732 | rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask, | |
733 | rnh, rt->rt_nodes); | |
734 | if (rn == 0) { | |
735 | struct rtentry *rt2; | |
736 | /* | |
737 | * Uh-oh, we already have one of these in the tree. | |
738 | * We do a special hack: if the route that's already | |
739 | * there was generated by the protocol-cloning | |
740 | * mechanism, then we just blow it away and retry | |
741 | * the insertion of the new one. | |
742 | */ | |
743 | rt2 = rtalloc1(dst, 0, RTF_PRCLONING); | |
744 | if (rt2 && rt2->rt_parent) { | |
745 | rtrequest(RTM_DELETE, | |
746 | (struct sockaddr *)rt_key(rt2), | |
747 | rt2->rt_gateway, | |
748 | rt_mask(rt2), rt2->rt_flags, 0); | |
749 | rtfree(rt2); | |
750 | rn = rnh->rnh_addaddr((caddr_t)ndst, | |
751 | (caddr_t)netmask, | |
752 | rnh, rt->rt_nodes); | |
753 | } else if (rt2) { | |
754 | /* undo the extra ref we got */ | |
755 | rtfree(rt2); | |
756 | } | |
757 | } | |
758 | ||
759 | /* | |
760 | * If it still failed to go into the tree, | |
761 | * then un-make it (this should be a function) | |
762 | */ | |
763 | if (rn == 0) { | |
764 | if (rt->rt_gwroute) | |
765 | rtfree(rt->rt_gwroute); | |
766 | if (rt->rt_ifa) { | |
767 | ifafree(rt->rt_ifa); | |
768 | } | |
769 | Free(rt_key(rt)); | |
770 | Free(rt); | |
771 | senderr(EEXIST); | |
772 | } | |
773 | ||
774 | rt->rt_parent = 0; | |
775 | ||
776 | /* | |
777 | * If we got here from RESOLVE, then we are cloning | |
778 | * so clone the rest, and note that we | |
779 | * are a clone (and increment the parent's references) | |
780 | */ | |
781 | if (req == RTM_RESOLVE) { | |
782 | rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */ | |
783 | if ((*ret_nrt)->rt_flags & (RTF_CLONING | RTF_PRCLONING)) { | |
784 | rt->rt_parent = (*ret_nrt); | |
785 | rtref(*ret_nrt); | |
786 | ||
787 | /* | |
788 | * If our parent is holding a reference to the same ifa, | |
789 | * free our reference and rely on the parent holding it. | |
790 | */ | |
791 | if (rt->rt_parent && rt->rt_parent->rt_ifa == rt->rt_ifa) | |
792 | ifafree(rt->rt_ifa); | |
793 | } | |
794 | } | |
795 | ||
796 | /* | |
797 | * if this protocol has something to add to this then | |
798 | * allow it to do that as well. | |
799 | */ | |
800 | if (ifa->ifa_rtrequest) | |
801 | ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0)); | |
802 | ||
803 | /* | |
804 | * We repeat the same procedure from rt_setgate() here because | |
805 | * it doesn't fire when we call it there because the node | |
806 | * hasn't been added to the tree yet. | |
807 | */ | |
808 | if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { | |
809 | struct rtfc_arg arg; | |
810 | arg.rnh = rnh; | |
811 | arg.rt0 = rt; | |
812 | rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), | |
813 | rt_fixchange, &arg); | |
814 | } | |
815 | ||
816 | /* | |
817 | * actually return a resultant rtentry and | |
818 | * give the caller a single reference. | |
819 | */ | |
820 | if (ret_nrt) { | |
821 | *ret_nrt = rt; | |
822 | rtref(rt); | |
823 | } | |
824 | break; | |
825 | } | |
826 | bad: | |
827 | splx(s); | |
828 | return (error); | |
829 | } | |
830 | ||
831 | /* | |
832 | * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family'' | |
833 | * (i.e., the routes related to it by the operation of cloning). This | |
834 | * routine is iterated over all potential former-child-routes by way of | |
835 | * rnh->rnh_walktree_from() above, and those that actually are children of | |
836 | * the late parent (passed in as VP here) are themselves deleted. | |
837 | */ | |
838 | static int | |
839 | rt_fixdelete(rn, vp) | |
840 | struct radix_node *rn; | |
841 | void *vp; | |
842 | { | |
843 | struct rtentry *rt = (struct rtentry *)rn; | |
844 | struct rtentry *rt0 = vp; | |
845 | ||
846 | if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) { | |
847 | return rtrequest(RTM_DELETE, rt_key(rt), | |
848 | (struct sockaddr *)0, rt_mask(rt), | |
849 | rt->rt_flags, (struct rtentry **)0); | |
850 | } | |
851 | return 0; | |
852 | } | |
853 | ||
854 | /* | |
855 | * This routine is called from rt_setgate() to do the analogous thing for | |
856 | * adds and changes. There is the added complication in this case of a | |
857 | * middle insert; i.e., insertion of a new network route between an older | |
858 | * network route and (cloned) host routes. For this reason, a simple check | |
859 | * of rt->rt_parent is insufficient; each candidate route must be tested | |
860 | * against the (mask, value) of the new route (passed as before in vp) | |
861 | * to see if the new route matches it. | |
862 | * | |
863 | * XXX - it may be possible to do fixdelete() for changes and reserve this | |
864 | * routine just for adds. I'm not sure why I thought it was necessary to do | |
865 | * changes this way. | |
866 | */ | |
867 | #ifdef DEBUG | |
868 | static int rtfcdebug = 0; | |
869 | #endif | |
870 | ||
871 | static int | |
872 | rt_fixchange(rn, vp) | |
873 | struct radix_node *rn; | |
874 | void *vp; | |
875 | { | |
876 | struct rtentry *rt = (struct rtentry *)rn; | |
877 | struct rtfc_arg *ap = vp; | |
878 | struct rtentry *rt0 = ap->rt0; | |
879 | struct radix_node_head *rnh = ap->rnh; | |
880 | u_char *xk1, *xm1, *xk2, *xmp; | |
881 | int i, len, mlen; | |
882 | ||
883 | #ifdef DEBUG | |
884 | if (rtfcdebug) | |
885 | printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0); | |
886 | #endif | |
887 | ||
888 | if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) { | |
889 | #ifdef DEBUG | |
890 | if(rtfcdebug) printf("no parent or pinned\n"); | |
891 | #endif | |
892 | return 0; | |
893 | } | |
894 | ||
895 | if (rt->rt_parent == rt0) { | |
896 | #ifdef DEBUG | |
897 | if(rtfcdebug) printf("parent match\n"); | |
898 | #endif | |
899 | return rtrequest(RTM_DELETE, rt_key(rt), | |
900 | (struct sockaddr *)0, rt_mask(rt), | |
901 | rt->rt_flags, (struct rtentry **)0); | |
902 | } | |
903 | ||
904 | /* | |
905 | * There probably is a function somewhere which does this... | |
906 | * if not, there should be. | |
907 | */ | |
908 | len = imin(((struct sockaddr *)rt_key(rt0))->sa_len, | |
909 | ((struct sockaddr *)rt_key(rt))->sa_len); | |
910 | ||
911 | xk1 = (u_char *)rt_key(rt0); | |
912 | xm1 = (u_char *)rt_mask(rt0); | |
913 | xk2 = (u_char *)rt_key(rt); | |
914 | ||
915 | /* avoid applying a less specific route */ | |
916 | xmp = (u_char *)rt_mask(rt->rt_parent); | |
917 | mlen = ((struct sockaddr *)rt_key(rt->rt_parent))->sa_len; | |
918 | if (mlen > ((struct sockaddr *)rt_key(rt0))->sa_len) { | |
919 | #if DEBUG | |
920 | if (rtfcdebug) | |
921 | printf("rt_fixchange: inserting a less " | |
922 | "specific route\n"); | |
923 | #endif | |
924 | return 0; | |
925 | } | |
926 | for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) { | |
927 | if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) { | |
928 | #if DEBUG | |
929 | if (rtfcdebug) | |
930 | printf("rt_fixchange: inserting a less " | |
931 | "specific route\n"); | |
932 | #endif | |
933 | return 0; | |
934 | } | |
935 | } | |
936 | ||
937 | for (i = rnh->rnh_treetop->rn_offset; i < len; i++) { | |
938 | if ((xk2[i] & xm1[i]) != xk1[i]) { | |
939 | #ifdef DEBUG | |
940 | if(rtfcdebug) printf("no match\n"); | |
941 | #endif | |
942 | return 0; | |
943 | } | |
944 | } | |
945 | ||
946 | /* | |
947 | * OK, this node is a clone, and matches the node currently being | |
948 | * changed/added under the node's mask. So, get rid of it. | |
949 | */ | |
950 | #ifdef DEBUG | |
951 | if(rtfcdebug) printf("deleting\n"); | |
952 | #endif | |
953 | return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, | |
954 | rt_mask(rt), rt->rt_flags, (struct rtentry **)0); | |
955 | } | |
956 | ||
957 | int | |
958 | rt_setgate(rt0, dst, gate) | |
959 | struct rtentry *rt0; | |
960 | struct sockaddr *dst, *gate; | |
961 | { | |
962 | caddr_t new, old; | |
963 | int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len); | |
964 | register struct rtentry *rt = rt0; | |
965 | struct radix_node_head *rnh = rt_tables[dst->sa_family]; | |
966 | extern void kdp_set_gateway_mac (void *gatewaymac); | |
967 | /* | |
968 | * A host route with the destination equal to the gateway | |
969 | * will interfere with keeping LLINFO in the routing | |
970 | * table, so disallow it. | |
971 | */ | |
972 | if (((rt0->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_LLINFO)) == | |
973 | (RTF_HOST|RTF_GATEWAY)) && | |
974 | (dst->sa_len == gate->sa_len) && | |
975 | (bcmp(dst, gate, dst->sa_len) == 0)) { | |
976 | /* | |
977 | * The route might already exist if this is an RTM_CHANGE | |
978 | * or a routing redirect, so try to delete it. | |
979 | */ | |
980 | if (rt_key(rt0)) | |
981 | rtrequest(RTM_DELETE, (struct sockaddr *)rt_key(rt0), | |
982 | rt0->rt_gateway, rt_mask(rt0), rt0->rt_flags, 0); | |
983 | return EADDRNOTAVAIL; | |
984 | } | |
985 | ||
986 | /* | |
987 | * Both dst and gateway are stored in the same malloc'd chunk | |
988 | * (If I ever get my hands on....) | |
989 | * if we need to malloc a new chunk, then keep the old one around | |
990 | * till we don't need it any more. | |
991 | */ | |
992 | if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) { | |
993 | old = (caddr_t)rt_key(rt); | |
994 | R_Malloc(new, caddr_t, dlen + glen); | |
995 | if (new == 0) | |
996 | return ENOBUFS; | |
997 | rt->rt_nodes->rn_key = new; | |
998 | } else { | |
999 | /* | |
1000 | * otherwise just overwrite the old one | |
1001 | */ | |
1002 | new = rt->rt_nodes->rn_key; | |
1003 | old = 0; | |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * copy the new gateway value into the memory chunk | |
1008 | */ | |
1009 | Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen); | |
1010 | ||
1011 | /* | |
1012 | * if we are replacing the chunk (or it's new) we need to | |
1013 | * replace the dst as well | |
1014 | */ | |
1015 | if (old) { | |
1016 | Bcopy(dst, new, dlen); | |
1017 | Free(old); | |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * If there is already a gwroute, it's now almost definitly wrong | |
1022 | * so drop it. | |
1023 | */ | |
1024 | if (rt->rt_gwroute) { | |
1025 | rt = rt->rt_gwroute; rtfree(rt); | |
1026 | rt = rt0; rt->rt_gwroute = 0; | |
1027 | } | |
1028 | /* | |
1029 | * Cloning loop avoidance: | |
1030 | * In the presence of protocol-cloning and bad configuration, | |
1031 | * it is possible to get stuck in bottomless mutual recursion | |
1032 | * (rtrequest rt_setgate rtalloc1). We avoid this by not allowing | |
1033 | * protocol-cloning to operate for gateways (which is probably the | |
1034 | * correct choice anyway), and avoid the resulting reference loops | |
1035 | * by disallowing any route to run through itself as a gateway. | |
1036 | * This is obviously mandatory when we get rt->rt_output(). | |
1037 | */ | |
1038 | if (rt->rt_flags & RTF_GATEWAY) { | |
1039 | rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING); | |
1040 | if (rt->rt_gwroute == rt) { | |
1041 | rtfree(rt->rt_gwroute); | |
1042 | rt->rt_gwroute = 0; | |
1043 | return EDQUOT; /* failure */ | |
1044 | } | |
1045 | /* Tell the kernel debugger about the new default gateway */ | |
1046 | if ((AF_INET == rt->rt_gateway->sa_family) && | |
1047 | rt->rt_gwroute && rt->rt_gwroute->rt_gateway && | |
1048 | (AF_LINK == rt->rt_gwroute->rt_gateway->sa_family)) { | |
1049 | kdp_set_gateway_mac(((struct sockaddr_dl *)rt0->rt_gwroute->rt_gateway)->sdl_data); | |
1050 | } | |
1051 | } | |
1052 | ||
1053 | /* | |
1054 | * This isn't going to do anything useful for host routes, so | |
1055 | * don't bother. Also make sure we have a reasonable mask | |
1056 | * (we don't yet have one during adds). | |
1057 | */ | |
1058 | if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) { | |
1059 | struct rtfc_arg arg; | |
1060 | arg.rnh = rnh; | |
1061 | arg.rt0 = rt; | |
1062 | rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt), | |
1063 | rt_fixchange, &arg); | |
1064 | } | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | ||
1069 | static void | |
1070 | rt_maskedcopy(src, dst, netmask) | |
1071 | struct sockaddr *src, *dst, *netmask; | |
1072 | { | |
1073 | register u_char *cp1 = (u_char *)src; | |
1074 | register u_char *cp2 = (u_char *)dst; | |
1075 | register u_char *cp3 = (u_char *)netmask; | |
1076 | u_char *cplim = cp2 + *cp3; | |
1077 | u_char *cplim2 = cp2 + *cp1; | |
1078 | ||
1079 | *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */ | |
1080 | cp3 += 2; | |
1081 | if (cplim > cplim2) | |
1082 | cplim = cplim2; | |
1083 | while (cp2 < cplim) | |
1084 | *cp2++ = *cp1++ & *cp3++; | |
1085 | if (cp2 < cplim2) | |
1086 | bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2)); | |
1087 | } | |
1088 | ||
1089 | /* | |
1090 | * Set up a routing table entry, normally | |
1091 | * for an interface. | |
1092 | */ | |
1093 | int | |
1094 | rtinit(ifa, cmd, flags) | |
1095 | register struct ifaddr *ifa; | |
1096 | int cmd, flags; | |
1097 | { | |
1098 | register struct rtentry *rt; | |
1099 | register struct sockaddr *dst; | |
1100 | register struct sockaddr *deldst; | |
1101 | struct mbuf *m = 0; | |
1102 | struct rtentry *nrt = 0; | |
1103 | int error; | |
1104 | ||
1105 | dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr; | |
1106 | /* | |
1107 | * If it's a delete, check that if it exists, it's on the correct | |
1108 | * interface or we might scrub a route to another ifa which would | |
1109 | * be confusing at best and possibly worse. | |
1110 | */ | |
1111 | if (cmd == RTM_DELETE) { | |
1112 | /* | |
1113 | * It's a delete, so it should already exist.. | |
1114 | * If it's a net, mask off the host bits | |
1115 | * (Assuming we have a mask) | |
1116 | */ | |
1117 | if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) { | |
1118 | m = m_get(M_DONTWAIT, MT_SONAME); | |
1119 | if (m == NULL) | |
1120 | return(ENOBUFS); | |
1121 | deldst = mtod(m, struct sockaddr *); | |
1122 | rt_maskedcopy(dst, deldst, ifa->ifa_netmask); | |
1123 | dst = deldst; | |
1124 | } | |
1125 | /* | |
1126 | * Get an rtentry that is in the routing tree and | |
1127 | * contains the correct info. (if this fails, can't get there). | |
1128 | * We set "report" to FALSE so that if it doesn't exist, | |
1129 | * it doesn't report an error or clone a route, etc. etc. | |
1130 | */ | |
1131 | rt = rtalloc1(dst, 0, 0UL); | |
1132 | if (rt) { | |
1133 | /* | |
1134 | * Ok so we found the rtentry. it has an extra reference | |
1135 | * for us at this stage. we won't need that so | |
1136 | * lop that off now. | |
1137 | */ | |
1138 | rtunref(rt); | |
1139 | if (rt->rt_ifa != ifa) { | |
1140 | /* | |
1141 | * If the interface in the rtentry doesn't match | |
1142 | * the interface we are using, then we don't | |
1143 | * want to delete it, so return an error. | |
1144 | * This seems to be the only point of | |
1145 | * this whole RTM_DELETE clause. | |
1146 | */ | |
1147 | if (m) | |
1148 | (void) m_free(m); | |
1149 | return (flags & RTF_HOST ? EHOSTUNREACH | |
1150 | : ENETUNREACH); | |
1151 | } | |
1152 | } | |
1153 | /* XXX */ | |
1154 | #if 0 | |
1155 | else { | |
1156 | /* | |
1157 | * One would think that as we are deleting, and we know | |
1158 | * it doesn't exist, we could just return at this point | |
1159 | * with an "ELSE" clause, but apparently not.. | |
1160 | */ | |
1161 | return (flags & RTF_HOST ? EHOSTUNREACH | |
1162 | : ENETUNREACH); | |
1163 | } | |
1164 | #endif | |
1165 | } | |
1166 | /* | |
1167 | * Do the actual request | |
1168 | */ | |
1169 | error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask, | |
1170 | flags | ifa->ifa_flags, &nrt); | |
1171 | if (m) | |
1172 | (void) m_free(m); | |
1173 | /* | |
1174 | * If we are deleting, and we found an entry, then | |
1175 | * it's been removed from the tree.. now throw it away. | |
1176 | */ | |
1177 | if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) { | |
1178 | /* | |
1179 | * notify any listenning routing agents of the change | |
1180 | */ | |
1181 | rt_newaddrmsg(cmd, ifa, error, nrt); | |
1182 | if (use_routegenid) | |
1183 | route_generation++; | |
1184 | if (rt->rt_refcnt <= 0) { | |
1185 | rt->rt_refcnt++; /* need a 1->0 transition to free */ | |
1186 | rtfree(rt); | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | /* | |
1191 | * We are adding, and we have a returned routing entry. | |
1192 | * We need to sanity check the result. | |
1193 | */ | |
1194 | if (cmd == RTM_ADD && error == 0 && (rt = nrt)) { | |
1195 | /* | |
1196 | * We just wanted to add it.. we don't actually need a reference | |
1197 | */ | |
1198 | rtunref(rt); | |
1199 | /* | |
1200 | * If it came back with an unexpected interface, then it must | |
1201 | * have already existed or something. (XXX) | |
1202 | */ | |
1203 | if (rt->rt_ifa != ifa) { | |
1204 | if (!(rt->rt_ifa->ifa_ifp->if_flags & | |
1205 | (IFF_POINTOPOINT|IFF_LOOPBACK))) | |
1206 | printf("rtinit: wrong ifa (%p) was (%p)\n", | |
1207 | ifa, rt->rt_ifa); | |
1208 | /* | |
1209 | * Ask that the protocol in question | |
1210 | * remove anything it has associated with | |
1211 | * this route and ifaddr. | |
1212 | */ | |
1213 | if (rt->rt_ifa->ifa_rtrequest) | |
1214 | rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0)); | |
1215 | /* | |
1216 | * Set the route's ifa. | |
1217 | */ | |
1218 | rtsetifa(rt, ifa); | |
1219 | /* | |
1220 | * And substitute in references to the ifaddr | |
1221 | * we are adding. | |
1222 | */ | |
1223 | rt->rt_ifp = ifa->ifa_ifp; | |
1224 | rt->rt_rmx.rmx_mtu = ifa->ifa_ifp->if_mtu; /*XXX*/ | |
1225 | /* | |
1226 | * Now ask the protocol to check if it needs | |
1227 | * any special processing in its new form. | |
1228 | */ | |
1229 | if (ifa->ifa_rtrequest) | |
1230 | ifa->ifa_rtrequest(RTM_ADD, rt, SA(0)); | |
1231 | } | |
1232 | /* | |
1233 | * notify any listenning routing agents of the change | |
1234 | */ | |
1235 | rt_newaddrmsg(cmd, ifa, error, nrt); | |
1236 | if (use_routegenid) | |
1237 | route_generation++; | |
1238 | } | |
1239 | return (error); | |
1240 | } |