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