xref: /freebsd/sys/net/route.c (revision 953a3198a35204535cc9d450f04da982a4fea59b)
1 /*
2  * Copyright (c) 1980, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)route.c	8.2 (Berkeley) 11/15/93
34  *	$Id: route.c,v 1.24 1995/07/10 15:22:37 wollman Exp $
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/ioctl.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 #include <net/raw_cb.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip_mroute.h>
55 
56 #define	SA(p) ((struct sockaddr *)(p))
57 
58 struct route_cb route_cb;
59 struct rtstat rtstat;
60 struct radix_node_head *rt_tables[AF_MAX+1];
61 
62 int	rttrash;		/* routes not in table but not freed */
63 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
64 
65 void
66 rtable_init(table)
67 	void **table;
68 {
69 	struct domain *dom;
70 	for (dom = domains; dom; dom = dom->dom_next)
71 		if (dom->dom_rtattach)
72 			dom->dom_rtattach(&table[dom->dom_family],
73 			    dom->dom_rtoffset);
74 }
75 
76 void
77 route_init()
78 {
79 	rn_init();	/* initialize all zeroes, all ones, mask table */
80 	rtable_init((void **)rt_tables);
81 }
82 
83 /*
84  * Packet routing routines.
85  */
86 void
87 rtalloc(ro)
88 	register struct route *ro;
89 {
90 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
91 		return;				 /* XXX */
92 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1, 0UL);
93 }
94 
95 void
96 rtalloc_ign(ro, ignore)
97 	register struct route *ro;
98 	u_long ignore;
99 {
100 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
101 		return;				 /* XXX */
102 	ro->ro_rt = rtalloc1(&ro->ro_dst, 1, ignore);
103 }
104 
105 struct rtentry *
106 rtalloc1(dst, report, ignflags)
107 	register struct sockaddr *dst;
108 	int report;
109 	u_long ignflags;
110 {
111 	register struct radix_node_head *rnh = rt_tables[dst->sa_family];
112 	register struct rtentry *rt;
113 	register struct radix_node *rn;
114 	struct rtentry *newrt = 0;
115 	struct rt_addrinfo info;
116 	u_long nflags;
117 	int  s = splnet(), err = 0, msgtype = RTM_MISS;
118 
119 	if (rnh && (rn = rnh->rnh_matchaddr((caddr_t)dst, rnh)) &&
120 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
121 		newrt = rt = (struct rtentry *)rn;
122 		nflags = rt->rt_flags & ~ignflags;
123 		if (report && (nflags & (RTF_CLONING | RTF_PRCLONING))) {
124 			err = rtrequest(RTM_RESOLVE, dst, SA(0),
125 					      SA(0), 0, &newrt);
126 			if (err) {
127 				newrt = rt;
128 				rt->rt_refcnt++;
129 				goto miss;
130 			}
131 			if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
132 				msgtype = RTM_RESOLVE;
133 				goto miss;
134 			}
135 		} else
136 			rt->rt_refcnt++;
137 	} else {
138 		rtstat.rts_unreach++;
139 	miss:	if (report) {
140 			bzero((caddr_t)&info, sizeof(info));
141 			info.rti_info[RTAX_DST] = dst;
142 			rt_missmsg(msgtype, &info, 0, err);
143 		}
144 	}
145 	splx(s);
146 	return (newrt);
147 }
148 
149 void
150 rtfree(rt)
151 	register struct rtentry *rt;
152 {
153 	register struct radix_node_head *rnh =
154 		rt_tables[rt_key(rt)->sa_family];
155 	register struct ifaddr *ifa;
156 
157 	if (rt == 0 || rnh == 0)
158 		panic("rtfree");
159 	rt->rt_refcnt--;
160 	if(rnh->rnh_close && rt->rt_refcnt == 0) {
161 		rnh->rnh_close((struct radix_node *)rt, rnh);
162 	}
163 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
164 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
165 			panic ("rtfree 2");
166 		rttrash--;
167 		if (rt->rt_refcnt < 0) {
168 			printf("rtfree: %p not freed (neg refs)\n", rt);
169 			return;
170 		}
171 		ifa = rt->rt_ifa;
172 		IFAFREE(ifa);
173 		if (rt->rt_parent) {
174 			RTFREE(rt->rt_parent);
175 		}
176 		Free(rt_key(rt));
177 		Free(rt);
178 	}
179 }
180 
181 void
182 ifafree(ifa)
183 	register struct ifaddr *ifa;
184 {
185 	if (ifa == NULL)
186 		panic("ifafree");
187 	if (ifa->ifa_refcnt == 0)
188 		free(ifa, M_IFADDR);
189 	else
190 		ifa->ifa_refcnt--;
191 }
192 
193 /*
194  * Force a routing table entry to the specified
195  * destination to go through the given gateway.
196  * Normally called as a result of a routing redirect
197  * message from the network layer.
198  *
199  * N.B.: must be called at splnet
200  *
201  */
202 void
203 rtredirect(dst, gateway, netmask, flags, src, rtp)
204 	struct sockaddr *dst, *gateway, *netmask, *src;
205 	int flags;
206 	struct rtentry **rtp;
207 {
208 	register struct rtentry *rt;
209 	int error = 0;
210 	short *stat = 0;
211 	struct rt_addrinfo info;
212 	struct ifaddr *ifa;
213 
214 	/* verify the gateway is directly reachable */
215 	if ((ifa = ifa_ifwithnet(gateway)) == 0) {
216 		error = ENETUNREACH;
217 		goto out;
218 	}
219 	rt = rtalloc1(dst, 0, 0UL);
220 	/*
221 	 * If the redirect isn't from our current router for this dst,
222 	 * it's either old or wrong.  If it redirects us to ourselves,
223 	 * we have a routing loop, perhaps as a result of an interface
224 	 * going down recently.
225 	 */
226 #define	equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
227 	if (!(flags & RTF_DONE) && rt &&
228 	     (!equal(src, rt->rt_gateway) || rt->rt_ifa != ifa))
229 		error = EINVAL;
230 	else if (ifa_ifwithaddr(gateway))
231 		error = EHOSTUNREACH;
232 	if (error)
233 		goto done;
234 	/*
235 	 * Create a new entry if we just got back a wildcard entry
236 	 * or the the lookup failed.  This is necessary for hosts
237 	 * which use routing redirects generated by smart gateways
238 	 * to dynamically build the routing tables.
239 	 */
240 	if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
241 		goto create;
242 	/*
243 	 * Don't listen to the redirect if it's
244 	 * for a route to an interface.
245 	 */
246 	if (rt->rt_flags & RTF_GATEWAY) {
247 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
248 			/*
249 			 * Changing from route to net => route to host.
250 			 * Create new route, rather than smashing route to net.
251 			 */
252 		create:
253 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
254 			error = rtrequest((int)RTM_ADD, dst, gateway,
255 				    netmask, flags,
256 				    (struct rtentry **)0);
257 			stat = &rtstat.rts_dynamic;
258 		} else {
259 			/*
260 			 * Smash the current notion of the gateway to
261 			 * this destination.  Should check about netmask!!!
262 			 */
263 			rt->rt_flags |= RTF_MODIFIED;
264 			flags |= RTF_MODIFIED;
265 			stat = &rtstat.rts_newgateway;
266 			rt_setgate(rt, rt_key(rt), gateway);
267 		}
268 	} else
269 		error = EHOSTUNREACH;
270 done:
271 	if (rt) {
272 		if (rtp && !error)
273 			*rtp = rt;
274 		else
275 			rtfree(rt);
276 	}
277 out:
278 	if (error)
279 		rtstat.rts_badredirect++;
280 	else if (stat != NULL)
281 		(*stat)++;
282 	bzero((caddr_t)&info, sizeof(info));
283 	info.rti_info[RTAX_DST] = dst;
284 	info.rti_info[RTAX_GATEWAY] = gateway;
285 	info.rti_info[RTAX_NETMASK] = netmask;
286 	info.rti_info[RTAX_AUTHOR] = src;
287 	rt_missmsg(RTM_REDIRECT, &info, flags, error);
288 }
289 
290 /*
291 * Routing table ioctl interface.
292 */
293 int
294 rtioctl(req, data, p)
295 	int req;
296 	caddr_t data;
297 	struct proc *p;
298 {
299 #ifdef INET
300 	/* Multicast goop, grrr... */
301 	return mrt_ioctl(req, data, p);
302 #else /* INET */
303 	return ENXIO;
304 #endif /* INET */
305 }
306 
307 struct ifaddr *
308 ifa_ifwithroute(flags, dst, gateway)
309 	int flags;
310 	struct sockaddr	*dst, *gateway;
311 {
312 	register struct ifaddr *ifa;
313 	if ((flags & RTF_GATEWAY) == 0) {
314 		/*
315 		 * If we are adding a route to an interface,
316 		 * and the interface is a pt to pt link
317 		 * we should search for the destination
318 		 * as our clue to the interface.  Otherwise
319 		 * we can use the local address.
320 		 */
321 		ifa = 0;
322 		if (flags & RTF_HOST) {
323 			ifa = ifa_ifwithdstaddr(dst);
324 		}
325 		if (ifa == 0)
326 			ifa = ifa_ifwithaddr(gateway);
327 	} else {
328 		/*
329 		 * If we are adding a route to a remote net
330 		 * or host, the gateway may still be on the
331 		 * other end of a pt to pt link.
332 		 */
333 		ifa = ifa_ifwithdstaddr(gateway);
334 	}
335 	if (ifa == 0)
336 		ifa = ifa_ifwithnet(gateway);
337 	if (ifa == 0) {
338 		struct rtentry *rt = rtalloc1(dst, 0, 0UL);
339 		if (rt == 0)
340 			return (0);
341 		rt->rt_refcnt--;
342 		if ((ifa = rt->rt_ifa) == 0)
343 			return (0);
344 	}
345 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
346 		struct ifaddr *oifa = ifa;
347 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
348 		if (ifa == 0)
349 			ifa = oifa;
350 	}
351 	return (ifa);
352 }
353 
354 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
355 
356 static int rt_fixdelete(struct radix_node *, void *);
357 static int rt_fixchange(struct radix_node *, void *);
358 
359 struct rtfc_arg {
360 	struct rtentry *rt0;
361 	struct radix_node_head *rnh;
362 };
363 
364 int
365 rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
366 	int req, flags;
367 	struct sockaddr *dst, *gateway, *netmask;
368 	struct rtentry **ret_nrt;
369 {
370 	int s = splnet(); int error = 0;
371 	register struct rtentry *rt;
372 	register struct radix_node *rn;
373 	register struct radix_node_head *rnh;
374 	struct ifaddr *ifa;
375 	struct sockaddr *ndst;
376 	u_long prflags = 0UL;
377 #define senderr(x) { error = x ; goto bad; }
378 
379 	if ((rnh = rt_tables[dst->sa_family]) == 0)
380 		senderr(ESRCH);
381 	if (flags & RTF_HOST)
382 		netmask = 0;
383 	switch (req) {
384 	case RTM_DELETE:
385 		if ((rn = rnh->rnh_deladdr(dst, netmask, rnh)) == 0)
386 			senderr(ESRCH);
387 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
388 			panic ("rtrequest delete");
389 		rt = (struct rtentry *)rn;
390 
391 		/*
392 		 * Now search what's left of the subtree for any cloned
393 		 * routes which might have been formed from this node.
394 		 */
395 		if ((rt->rt_flags & RTF_PRCLONING) && netmask) {
396 			rnh->rnh_walktree_from(rnh, dst, netmask,
397 					       rt_fixdelete, rt);
398 		}
399 
400 		/*
401 		 * NB: RTF_UP must be set during the search above,
402 		 * because we might delete the last ref, causing
403 		 * rt to get freed prematurely.
404 		 */
405 		rt->rt_flags &= ~RTF_UP;
406 
407 		if (rt->rt_gwroute) {
408 			rt = rt->rt_gwroute; RTFREE(rt);
409 			(rt = (struct rtentry *)rn)->rt_gwroute = 0;
410 		}
411 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
412 			ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
413 		rttrash++;
414 		if (ret_nrt)
415 			*ret_nrt = rt;
416 		else if (rt->rt_refcnt <= 0) {
417 			rt->rt_refcnt++;
418 			rtfree(rt);
419 		}
420 		break;
421 
422 	case RTM_RESOLVE:
423 		if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
424 			senderr(EINVAL);
425 		ifa = rt->rt_ifa;
426 		flags = rt->rt_flags &
427 		    ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
428 		flags |= RTF_WASCLONED;
429 		gateway = rt->rt_gateway;
430 		if ((netmask = rt->rt_genmask) == 0)
431 			flags |= RTF_HOST;
432 		goto makeroute;
433 
434 	case RTM_ADD:
435 		if ((flags & RTF_GATEWAY) && !gateway)
436 			panic("rtrequest: GATEWAY but no gateway");
437 
438 		if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
439 			senderr(ENETUNREACH);
440 
441 	makeroute:
442 		R_Malloc(rt, struct rtentry *, sizeof(*rt));
443 		if (rt == 0)
444 			senderr(ENOBUFS);
445 		Bzero(rt, sizeof(*rt));
446 		rt->rt_flags = RTF_UP | flags;
447 		if (rt_setgate(rt, dst, gateway)) {
448 			Free(rt);
449 			senderr(ENOBUFS);
450 		}
451 		ndst = rt_key(rt);
452 		if (netmask) {
453 			rt_maskedcopy(dst, ndst, netmask);
454 		} else
455 			Bcopy(dst, ndst, dst->sa_len);
456 
457 		/*
458 		 * This moved from below so that rnh->rnh_addaddr() can
459 		 * examine the ifa and ifp if it so desires.
460 		 */
461 		ifa->ifa_refcnt++;
462 		rt->rt_ifa = ifa;
463 		rt->rt_ifp = ifa->ifa_ifp;
464 
465 		rn = rnh->rnh_addaddr((caddr_t)ndst, (caddr_t)netmask,
466 					rnh, rt->rt_nodes);
467 		if (rn == 0) {
468 			if (rt->rt_gwroute)
469 				rtfree(rt->rt_gwroute);
470 			if (rt->rt_ifa) {
471 				IFAFREE(rt->rt_ifa);
472 			}
473 			Free(rt_key(rt));
474 			Free(rt);
475 			senderr(EEXIST);
476 		}
477 		rt->rt_parent = 0;
478 
479 		if (req == RTM_RESOLVE) {
480 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
481 			if ((*ret_nrt)->rt_flags & RTF_PRCLONING) {
482 				rt->rt_parent = (*ret_nrt);
483 				(*ret_nrt)->rt_refcnt++;
484 			}
485 		}
486 		if (ifa->ifa_rtrequest)
487 			ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
488 		/*
489 		 * We repeat the same procedure from rt_setgate() here because
490 		 * it doesn't fire when we call it there because the node
491 		 * hasn't been added to the tree yet.
492 		 */
493 		if (!(rt->rt_flags & RTF_HOST)) {
494 			struct rtfc_arg arg;
495 			arg.rnh = rnh;
496 			arg.rt0 = rt;
497 			rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
498 					       rt_fixchange, &arg);
499 		}
500 
501 		if (ret_nrt) {
502 			*ret_nrt = rt;
503 			rt->rt_refcnt++;
504 		}
505 		break;
506 	}
507 bad:
508 	splx(s);
509 	return (error);
510 }
511 
512 /*
513  * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
514  * (i.e., the routes related to it by the operation of cloning).  This
515  * routine is iterated over all potential former-child-routes by way of
516  * rnh->rnh_walktree_from() above, and those that actually are children of
517  * the late parent (passed in as VP here) are themselves deleted.
518  */
519 static int
520 rt_fixdelete(struct radix_node *rn, void *vp)
521 {
522 	struct rtentry *rt = (struct rtentry *)rn;
523 	struct rtentry *rt0 = vp;
524 
525 	if (rt->rt_parent == rt0 && !(rt->rt_flags & RTF_PINNED)) {
526 		return rtrequest(RTM_DELETE, rt_key(rt),
527 				 (struct sockaddr *)0, rt_mask(rt),
528 				 rt->rt_flags, (struct rtentry **)0);
529 	}
530 	return 0;
531 }
532 
533 /*
534  * This routine is called from rt_setgate() to do the analogous thing for
535  * adds and changes.  There is the added complication in this case of a
536  * middle insert; i.e., insertion of a new network route between an older
537  * network route and (cloned) host routes.  For this reason, a simple check
538  * of rt->rt_parent is insufficient; each candidate route must be tested
539  * against the (mask, value) of the new route (passed as before in vp)
540  * to see if the new route matches it.  Unfortunately, this has the obnoxious
541  * property of also triggering for insertion /above/ a pre-existing network
542  * route and clones.  Sigh.  This may be fixed some day.
543  *
544  * XXX - it may be possible to do fixdelete() for changes and reserve this
545  * routine just for adds.  I'm not sure why I thought it was necessary to do
546  * changes this way.
547  */
548 #ifdef DEBUG
549 int rtfcdebug = 0;
550 #endif
551 
552 static int
553 rt_fixchange(struct radix_node *rn, void *vp)
554 {
555 	struct rtentry *rt = (struct rtentry *)rn;
556 	struct rtfc_arg *ap = vp;
557 	struct rtentry *rt0 = ap->rt0;
558 	struct radix_node_head *rnh = ap->rnh;
559 	u_char *xk1, *xm1, *xk2;
560 	int i, len;
561 
562 #ifdef DEBUG
563 	if (rtfcdebug)
564 		printf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
565 #endif
566 
567 	if (!rt->rt_parent || (rt->rt_flags & RTF_PINNED)) {
568 #ifdef DEBUG
569 		if(rtfcdebug) printf("no parent or pinned\n");
570 #endif
571 		return 0;
572 	}
573 
574 	if (rt->rt_parent == rt0) {
575 #ifdef DEBUG
576 		if(rtfcdebug) printf("parent match\n");
577 #endif
578 		return rtrequest(RTM_DELETE, rt_key(rt),
579 				 (struct sockaddr *)0, rt_mask(rt),
580 				 rt->rt_flags, (struct rtentry **)0);
581 	}
582 
583 	/*
584 	 * There probably is a function somewhere which does this...
585 	 * if not, there should be.
586 	 */
587 	len = imin(((struct sockaddr *)rt_key(rt0))->sa_len,
588 		   ((struct sockaddr *)rt_key(rt))->sa_len);
589 
590 	xk1 = (u_char *)rt_key(rt0);
591 	xm1 = (u_char *)rt_mask(rt0);
592 	xk2 = (u_char *)rt_key(rt);
593 
594 	for (i = rnh->rnh_treetop->rn_off; i < len; i++) {
595 		if ((xk2[i] & xm1[i]) != xk1[i]) {
596 #ifdef DEBUG
597 			if(rtfcdebug) printf("no match\n");
598 #endif
599 			return 0;
600 		}
601 	}
602 
603 	/*
604 	 * OK, this node is a clone, and matches the node currently being
605 	 * changed/added under the node's mask.  So, get rid of it.
606 	 */
607 #ifdef DEBUG
608 	if(rtfcdebug) printf("deleting\n");
609 #endif
610 	return rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
611 			 rt_mask(rt), rt->rt_flags, (struct rtentry **)0);
612 }
613 
614 int
615 rt_setgate(rt0, dst, gate)
616 	struct rtentry *rt0;
617 	struct sockaddr *dst, *gate;
618 {
619 	caddr_t new, old;
620 	int dlen = ROUNDUP(dst->sa_len), glen = ROUNDUP(gate->sa_len);
621 	register struct rtentry *rt = rt0;
622 	struct radix_node_head *rnh = rt_tables[dst->sa_family];
623 
624 	if (rt->rt_gateway == 0 || glen > ROUNDUP(rt->rt_gateway->sa_len)) {
625 		old = (caddr_t)rt_key(rt);
626 		R_Malloc(new, caddr_t, dlen + glen);
627 		if (new == 0)
628 			return 1;
629 		rt->rt_nodes->rn_key = new;
630 	} else {
631 		new = rt->rt_nodes->rn_key;
632 		old = 0;
633 	}
634 	Bcopy(gate, (rt->rt_gateway = (struct sockaddr *)(new + dlen)), glen);
635 	if (old) {
636 		Bcopy(dst, new, dlen);
637 		Free(old);
638 	}
639 	if (rt->rt_gwroute) {
640 		rt = rt->rt_gwroute; RTFREE(rt);
641 		rt = rt0; rt->rt_gwroute = 0;
642 	}
643 	/*
644 	 * Cloning loop avoidance:
645 	 * In the presence of protocol-cloning and bad configuration,
646 	 * it is possible to get stuck in bottomless mutual recursion
647 	 * (rtrequest rt_setgate rtalloc1).  We avoid this by not allowing
648 	 * protocol-cloning to operate for gateways (which is probably the
649 	 * correct choice anyway), and avoid the resulting reference loops
650 	 * by disallowing any route to run through itself as a gateway.
651 	 * This is obviuosly mandatory when we get rt->rt_output().
652 	 */
653 	if (rt->rt_flags & RTF_GATEWAY) {
654 		rt->rt_gwroute = rtalloc1(gate, 1, RTF_PRCLONING);
655 		if (rt->rt_gwroute == rt) {
656 			RTFREE(rt->rt_gwroute);
657 			rt->rt_gwroute = 0;
658 			return 1; /* failure */
659 		}
660 	}
661 
662 	/*
663 	 * This isn't going to do anything useful for host routes, so
664 	 * don't bother.  Also make sure we have a reasonable mask
665 	 * (we don't yet have one during adds).
666 	 */
667 	if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != 0) {
668 		struct rtfc_arg arg;
669 		arg.rnh = rnh;
670 		arg.rt0 = rt;
671 		rnh->rnh_walktree_from(rnh, rt_key(rt), rt_mask(rt),
672 				       rt_fixchange, &arg);
673 	}
674 
675 	return 0;
676 }
677 
678 void
679 rt_maskedcopy(src, dst, netmask)
680 	struct sockaddr *src, *dst, *netmask;
681 {
682 	register u_char *cp1 = (u_char *)src;
683 	register u_char *cp2 = (u_char *)dst;
684 	register u_char *cp3 = (u_char *)netmask;
685 	u_char *cplim = cp2 + *cp3;
686 	u_char *cplim2 = cp2 + *cp1;
687 
688 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
689 	cp3 += 2;
690 	if (cplim > cplim2)
691 		cplim = cplim2;
692 	while (cp2 < cplim)
693 		*cp2++ = *cp1++ & *cp3++;
694 	if (cp2 < cplim2)
695 		bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
696 }
697 
698 /*
699  * Set up a routing table entry, normally
700  * for an interface.
701  */
702 int
703 rtinit(ifa, cmd, flags)
704 	register struct ifaddr *ifa;
705 	int cmd, flags;
706 {
707 	register struct rtentry *rt;
708 	register struct sockaddr *dst;
709 	register struct sockaddr *deldst;
710 	struct mbuf *m = 0;
711 	struct rtentry *nrt = 0;
712 	int error;
713 
714 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
715 	if (cmd == RTM_DELETE) {
716 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
717 			m = m_get(M_WAIT, MT_SONAME);
718 			deldst = mtod(m, struct sockaddr *);
719 			rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
720 			dst = deldst;
721 		}
722 		rt = rtalloc1(dst, 0, 0UL);
723 		if (rt) {
724 			rt->rt_refcnt--;
725 			if (rt->rt_ifa != ifa) {
726 				if (m)
727 					(void) m_free(m);
728 				return (flags & RTF_HOST ? EHOSTUNREACH
729 							: ENETUNREACH);
730 			}
731 		}
732 	}
733 	error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
734 			flags | ifa->ifa_flags, &nrt);
735 	if (m)
736 		(void) m_free(m);
737 	if (cmd == RTM_DELETE && error == 0 && (rt = nrt)) {
738 		rt_newaddrmsg(cmd, ifa, error, nrt);
739 		if (rt->rt_refcnt <= 0) {
740 			rt->rt_refcnt++;
741 			rtfree(rt);
742 		}
743 	}
744 	if (cmd == RTM_ADD && error == 0 && (rt = nrt)) {
745 		rt->rt_refcnt--;
746 		if (rt->rt_ifa != ifa) {
747 			printf("rtinit: wrong ifa (%p) was (%p)\n", ifa,
748 				rt->rt_ifa);
749 			if (rt->rt_ifa->ifa_rtrequest)
750 			    rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
751 			IFAFREE(rt->rt_ifa);
752 			rt->rt_ifa = ifa;
753 			rt->rt_ifp = ifa->ifa_ifp;
754 			ifa->ifa_refcnt++;
755 			if (ifa->ifa_rtrequest)
756 			    ifa->ifa_rtrequest(RTM_ADD, rt, SA(0));
757 		}
758 		rt_newaddrmsg(cmd, ifa, error, nrt);
759 	}
760 	return (error);
761 }
762