xref: /386bsd/usr/src/kernel/route/route.c (revision dc8b130e)
1 /*
2  * Copyright (c) 1980, 1986, 1991 Regents of the University of California.
3  * 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  * $Id: route.c,v 1.1 94/10/20 10:55:41 root Exp Locker: bill $
34  */
35 #include "sys/param.h"
36 #include "sys/file.h"
37 #include "sys/ioctl.h"
38 #include "sys/errno.h"
39 #include "privilege.h"
40 #include "proc.h"
41 #include "mbuf.h"
42 #include "socketvar.h"
43 #include "domain.h"
44 #include "protosw.h"
45 #include "modconfig.h"
46 #include "prototypes.h"
47 #include "esym.h"
48 
49 #include "if.h"
50 #include "af.h"
51 #define	_ROUTE_PROTOTYPES
52 #include "route.h"
53 #undef	_ROUTE_PROTOTYPES
54 #include "raw_cb.h"
55 
56 #include "in.h"
57 #include "in_var.h"
58 
59 #ifdef NS
60 #include "ns.h"
61 #endif
62 #include "netisr.h"
63 
64 #define	SA(p) ((struct sockaddr *)(p))
65 
66 int rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst,
67 	struct sockaddr *netmask);
68 
69 int	rttrash;		/* routes not in table but not freed */
70 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
71 int	rthashsize = RTHASHSIZ;	/* for netstat, etc. */
72 struct rtstat rtstat;
73 
74 static int rtinits_done = 0;
75 struct radix_node_head *ns_rnhead, *in_rnhead;
76 struct radix_node *rn_match(), *rn_delete(), *rn_addroute();
77 
78 /*extern void
79 rt_missmsg(int type, struct sockaddr *dst, struct sockaddr *gate,
80 	struct sockaddr *mask, struct sockaddr *src, int flags, int error) asm("rtmissmsg");*/
81 
82 
rtinitheads()83 rtinitheads()
84 {
85 	if (rtinits_done == 0 &&
86 #ifdef NS
87 	    rn_inithead(&ns_rnhead, 16, AF_NS) &&
88 #endif
89 	    rn_inithead(&in_rnhead, 32, AF_INET))
90 		rtinits_done = 1;
91 }
92 
93 struct rtentry *rtalloc1(struct sockaddr *dst, int report);
94 
95 /*
96  * Packet routing routines.
97  */
98 void
rtalloc(struct route * ro)99 rtalloc(struct route *ro)
100 {
101 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
102 		return;				 /* XXX */
103 	ro->ro_rt = rtalloc1(&ro->ro_dst, 0);
104 }
105 
106 struct rtentry *
rtalloc1(struct sockaddr * dst,int report)107 rtalloc1(struct sockaddr *dst, int report)
108 {
109 	register struct radix_node_head *rnh;
110 	register struct rtentry *rt;
111 	register struct radix_node *rn;
112 	struct rtentry *newrt = 0;
113 	int  s = splnet(), err = 0, msgtype = RTM_MISS;
114 
115 	for (rnh = radix_node_head; rnh && (dst->sa_family != rnh->rnh_af); )
116 		rnh = rnh->rnh_next;
117 	if (rnh && rnh->rnh_treetop &&
118 	    (rn = rn_match((caddr_t)dst, rnh->rnh_treetop)) &&
119 	    ((rn->rn_flags & RNF_ROOT) == 0)) {
120 		newrt = rt = (struct rtentry *)rn;
121 		if (report && (rt->rt_flags & RTF_CLONING)) {
122 			if ((err = rtrequest(RTM_RESOLVE, dst, SA(0),
123 					      SA(0), 0, &newrt)) ||
124 			    ((rt->rt_flags & RTF_XRESOLVE)
125 			      && (msgtype = RTM_RESOLVE))) /* intended! */
126 			    goto miss;
127 		} else
128 			rt->rt_refcnt++;
129 	} else {
130 		rtstat.rts_unreach++;
131 	miss:	if (report)
132 			rtmissmsg(msgtype, dst, SA(0), SA(0), SA(0), 0, err);
133 	}
134 	splx(s);
135 	return (newrt);
136 }
137 
138 void
rtfree(struct rtentry * rt)139 rtfree(struct rtentry *rt)
140 {
141 	struct ifaddr *ifa;
142 
143 	if (rt == 0)
144 		panic("rtfree");
145 
146 	rt->rt_refcnt--;
147 	if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
148 		rttrash--;
149 		if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
150 			panic ("rtfree 2");
151 		free((caddr_t)rt, M_RTABLE);
152 	}
153 }
154 
155 /*
156  * Force a routing table entry to the specified
157  * destination to go through the given gateway.
158  * Normally called as a result of a routing redirect
159  * message from the network layer.
160  *
161  * N.B.: must be called at splnet
162  *
163  */
164 void
rtredirect(struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct sockaddr * src,struct rtentry ** rtp)165 rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
166     struct sockaddr *netmask, int flags, struct sockaddr *src,
167     struct rtentry **rtp)
168 {
169 	struct rtentry *rt = 0;
170 	int error = 0;
171 	short *stat = 0;
172 
173 	/* verify the gateway is directly reachable */
174 	if (ifa_ifwithnet(gateway) == 0) {
175 		error = ENETUNREACH;
176 		goto done;
177 	}
178 	rt = rtalloc1(dst, 0);
179 	/*
180 	 * If the redirect isn't from our current router for this dst,
181 	 * it's either old or wrong.  If it redirects us to ourselves,
182 	 * we have a routing loop, perhaps as a result of an interface
183 	 * going down recently.
184 	 */
185 #define	equal(a1, a2) (memcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
186 	if (!(flags & RTF_DONE) && rt && !equal(src, rt->rt_gateway))
187 		error = EINVAL;
188 	else if (ifa_ifwithaddr(gateway))
189 		error = EHOSTUNREACH;
190 	if (error)
191 		goto done;
192 	/*
193 	 * Create a new entry if we just got back a wildcard entry
194 	 * or the the lookup failed.  This is necessary for hosts
195 	 * which use routing redirects generated by smart gateways
196 	 * to dynamically build the routing tables.
197 	 */
198 	if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
199 		goto create;
200 	/*
201 	 * Don't listen to the redirect if it's
202 	 * for a route to an interface.
203 	 */
204 	if (rt->rt_flags & RTF_GATEWAY) {
205 		if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
206 			/*
207 			 * Changing from route to net => route to host.
208 			 * Create new route, rather than smashing route to net.
209 			 */
210 		create:
211 			flags |=  RTF_GATEWAY | RTF_DYNAMIC;
212 			error = rtrequest((int)RTM_ADD, dst, gateway,
213 				    SA(0), flags,
214 				    (struct rtentry **)0);
215 			stat = &rtstat.rts_dynamic;
216 		} else {
217 			/*
218 			 * Smash the current notion of the gateway to
219 			 * this destination.  Should check about netmask!!!
220 			 */
221 			if (gateway->sa_len <= rt->rt_gateway->sa_len) {
222 				Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
223 				rt->rt_flags |= RTF_MODIFIED;
224 				flags |= RTF_MODIFIED;
225 				stat = &rtstat.rts_newgateway;
226 			} else
227 				error = ENOSPC;
228 		}
229 	} else
230 		error = EHOSTUNREACH;
231 done:
232 	if (rt) {
233 		if (rtp && !error)
234 			*rtp = rt;
235 		else
236 			rtfree(rt);
237 	}
238 	if (error)
239 		rtstat.rts_badredirect++;
240 	else
241 		(stat && (*stat)++);
242 	rtmissmsg(RTM_REDIRECT, dst, gateway, netmask, src, flags, error);
243 }
244 
245 /*
246 * Routing table ioctl interface.
247 */
248 int
rtioctl(int req,caddr_t data,struct proc * p)249 rtioctl(int req, caddr_t data, struct proc *p)
250 {
251 #ifndef COMPAT_43
252 	return (EOPNOTSUPP);
253 #else
254 	struct ortentry *entry = (struct ortentry *)data;
255 	int error;
256 	struct sockaddr *netmask = 0;
257 
258 	if (req == SIOCADDRT)
259 		req = RTM_ADD;
260 	else if (req == SIOCDELRT)
261 		req = RTM_DELETE;
262 	else
263 		return (EINVAL);
264 
265 	if (error = use_priv(p->p_ucred, PRV_RTIOCTL, p))
266 		return (error);
267 #if BYTE_ORDER != BIG_ENDIAN
268 	if (entry->rt_dst.sa_family == 0 && entry->rt_dst.sa_len < 16) {
269 		entry->rt_dst.sa_family = entry->rt_dst.sa_len;
270 		entry->rt_dst.sa_len = 16;
271 	}
272 	if (entry->rt_gateway.sa_family == 0 && entry->rt_gateway.sa_len < 16) {
273 		entry->rt_gateway.sa_family = entry->rt_gateway.sa_len;
274 		entry->rt_gateway.sa_len = 16;
275 	}
276 #else
277 	if (entry->rt_dst.sa_len == 0)
278 		entry->rt_dst.sa_len = 16;
279 	if (entry->rt_gateway.sa_len == 0)
280 		entry->rt_gateway.sa_len = 16;
281 #endif
282 	if ((entry->rt_flags & RTF_HOST) == 0)
283 		switch (entry->rt_dst.sa_family) {
284 #ifdef INET
285 		case AF_INET:
286 			{
287 				struct sockaddr_in icmpmask;
288 				struct sockaddr_in *dst_in =
289 					(struct sockaddr_in *)&entry->rt_dst;
290 
291 				in_sockmaskof(dst_in->sin_addr, &icmpmask);
292 				netmask = (struct sockaddr *)&icmpmask;
293 			}
294 			break;
295 #endif
296 #ifdef NS
297 		case AF_NS:
298 			{
299 				extern struct sockaddr_ns ns_netmask;
300 				netmask = (struct sockaddr *)&ns_netmask;
301 			}
302 #endif
303 		}
304 	error =  rtrequest(req, &(entry->rt_dst), &(entry->rt_gateway), netmask,
305 				entry->rt_flags, (struct rtentry **)0);
306 	rtmissmsg((req == RTM_ADD ? RTM_OLDADD : RTM_OLDDEL),
307 		   &(entry->rt_dst), &(entry->rt_gateway),
308 		   netmask, SA(0), entry->rt_flags, error);
309 	return (error);
310 #endif
311 }
312 
313 /* find the interface to use with a destination or gateway, otherwise return 0 */
314 struct ifaddr *
ifa_ifwithroute(int flags,struct sockaddr * dst,struct sockaddr * gateway)315 ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
316 {
317 	struct ifaddr *ifa;
318 
319 	if ((flags & RTF_GATEWAY) == 0) {
320 		/*
321 		 * If we are adding a route to an interface,
322 		 * and the interface is a pt to pt link
323 		 * we should search for the destination
324 		 * as our clue to the interface.  Otherwise
325 		 * we can use the local address.
326 		 */
327 		ifa = 0;
328 		if (flags & RTF_HOST)
329 			ifa = ifa_ifwithdstaddr(dst);
330 		if (ifa == 0)
331 			ifa = ifa_ifwithaddr(gateway);
332 	} else {
333 		/*
334 		 * If we are adding a route to a remote net
335 		 * or host, the gateway may still be on the
336 		 * other end of a pt to pt link.
337 		 */
338 		ifa = ifa_ifwithdstaddr(gateway);
339 	}
340 	if (ifa == 0)
341 		ifa = ifa_ifwithnet(gateway);
342 	if (ifa == 0) {
343 		struct rtentry *rt = rtalloc1(dst, 0);
344 		if (rt == 0)
345 			return (0);
346 		rt->rt_refcnt--;
347 		if ((ifa = rt->rt_ifa) == 0)
348 			return (0);
349 	}
350 	if (ifa->ifa_addr->sa_family != dst->sa_family) {
351 		struct ifaddr *oifa = ifa, *ifaof_ifpforaddr();
352 		ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
353 		if (ifa == 0)
354 			ifa = oifa;
355 	}
356 	return (ifa);
357 }
358 
359 #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
360 
361 int
rtrequest(int req,struct sockaddr * dst,struct sockaddr * gateway,struct sockaddr * netmask,int flags,struct rtentry ** ret_nrt)362 rtrequest(int req, struct sockaddr *dst, struct sockaddr *gateway,
363    struct sockaddr *netmask, int flags, struct rtentry **ret_nrt)
364 {
365 	int s = splnet(), len, error = 0;
366 	struct rtentry *rt;
367 	struct radix_node *rn;
368 	struct radix_node_head *rnh;
369 	struct ifaddr *ifa, *ifa_ifwithdstaddr();
370 	struct sockaddr *ndst;
371 	u_char af = dst->sa_family;
372 #define senderr(x) { error = x ; goto bad; }
373 
374 	if (rtinits_done == 0)
375 		rtinitheads();
376 	for (rnh = radix_node_head; rnh && (af != rnh->rnh_af); )
377 		rnh = rnh->rnh_next;
378 	if (rnh == 0)
379 		senderr(ESRCH);
380 	if (flags & RTF_HOST)
381 		netmask = 0;
382 	switch (req) {
383 	case RTM_DELETE:
384 		if (ret_nrt && (rt = *ret_nrt)) {
385 			RTFREE_(rt);
386 			*ret_nrt = 0;
387 		}
388 		if ((rn = rn_delete((caddr_t)dst, (caddr_t)netmask,
389 					rnh->rnh_treetop)) == 0)
390 			senderr(ESRCH);
391 		if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
392 			panic ("rtrequest delete");
393 		rt = (struct rtentry *)rn;
394 		rt->rt_flags &= ~RTF_UP;
395 		if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
396 			ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
397 		rttrash++;
398 		if (rt->rt_refcnt <= 0)
399 			rtfree(rt);
400 		break;
401 
402 	case RTM_RESOLVE:
403 		if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
404 			senderr(EINVAL);
405 		ifa = rt->rt_ifa;
406 		flags = rt->rt_flags & ~RTF_CLONING;
407 		gateway = rt->rt_gateway;
408 		if ((netmask = rt->rt_genmask) == 0)
409 			flags |= RTF_HOST;
410 		goto makeroute;
411 
412 	case RTM_ADD:
413 		if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
414 			senderr(ENETUNREACH);
415 	makeroute:
416 		len = sizeof (*rt) + ROUNDUP(gateway->sa_len)
417 		    + ROUNDUP(dst->sa_len);
418 		R_Malloc(rt, struct rtentry *, len);
419 		if (rt == 0)
420 			senderr(ENOBUFS);
421 		Bzero(rt, len);
422 		ndst = (struct sockaddr *)(rt + 1);
423 		if (netmask) {
424 			rt_maskedcopy(dst, ndst, netmask);
425 		} else
426 			Bcopy(dst, ndst, dst->sa_len);
427 		rn = rn_addroute((caddr_t)ndst, (caddr_t)netmask,
428 					rnh->rnh_treetop, rt->rt_nodes);
429 		if (rn == 0) {
430 			free((caddr_t)rt, M_RTABLE);
431 			senderr(EEXIST);
432 		}
433 		rt->rt_ifa = ifa;
434 		rt->rt_ifp = ifa->ifa_ifp;
435 		rt->rt_flags = RTF_UP | flags;
436 		rt->rt_gateway = (struct sockaddr *)
437 					(rn->rn_key + ROUNDUP(dst->sa_len));
438 		Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
439 		if (req == RTM_RESOLVE)
440 			rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
441 		if (ifa->ifa_rtrequest)
442 			ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
443 		if (ret_nrt) {
444 			*ret_nrt = rt;
445 			rt->rt_refcnt++;
446 		}
447 		break;
448 	}
449 bad:
450 	splx(s);
451 	return (error);
452 }
453 
454 int
rt_maskedcopy(struct sockaddr * src,struct sockaddr * dst,struct sockaddr * netmask)455 rt_maskedcopy(struct sockaddr *src, struct sockaddr *dst,
456 	struct sockaddr *netmask)
457 {
458 	register u_char *cp1 = (u_char *)src;
459 	register u_char *cp2 = (u_char *)dst;
460 	register u_char *cp3 = (u_char *)netmask;
461 	u_char *cplim = cp2 + *cp3;
462 	u_char *cplim2 = cp2 + *cp1;
463 
464 	*cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
465 	cp3 += 2;
466 	if (cplim > cplim2)
467 		cplim = cplim2;
468 	while (cp2 < cplim)
469 		*cp2++ = *cp1++ & *cp3++;
470 	if (cp2 < cplim2)
471 		(void) memset((caddr_t)cp2, 0, (unsigned)(cplim2 - cp2));
472 }
473 /*
474  * Set up a routing table entry, normally
475  * for an interface.
476  */
477 int
rtinit(struct ifaddr * ifa,int cmd,int flags)478 rtinit(struct ifaddr *ifa, int cmd, int flags)
479 {
480 	struct rtentry *rt;
481 	struct sockaddr *dst, *deldst;
482 	struct mbuf *m = 0;
483 	int error;
484 
485 	dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
486 	if (ifa->ifa_flags & IFA_ROUTE) {
487 		if ((rt = ifa->ifa_rt) && (rt->rt_flags & RTF_UP) == 0) {
488 			RTFREE_(rt);
489 			ifa->ifa_rt = 0;
490 		}
491 	}
492 	if (cmd == RTM_DELETE) {
493 		if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
494 			m = m_get(M_WAIT, MT_SONAME);
495 			deldst = mtod(m, struct sockaddr *);
496 			rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
497 			dst = deldst;
498 		}
499 		if (rt = rtalloc1(dst, 0)) {
500 			rt->rt_refcnt--;
501 			if (rt->rt_ifa != ifa) {
502 				if (m)
503 					(void) m_free(m);
504 				return (flags & RTF_HOST ? EHOSTUNREACH
505 							: ENETUNREACH);
506 			}
507 		}
508 	}
509 	error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
510 			flags | ifa->ifa_flags, &ifa->ifa_rt);
511 	if (m)
512 		(void) m_free(m);
513 	if (cmd == RTM_ADD && error == 0 && (rt = ifa->ifa_rt)
514 						&& rt->rt_ifa != ifa) {
515 		rt->rt_ifa = ifa;
516 		rt->rt_ifp = ifa->ifa_ifp;
517 	}
518 	return (error);
519 }
520 
521 /*static struct route_ops router = {
522 	"radix",
523 	rtinit, rtalloc, rtfree,
524 	rtrequest, rtredirect, rt_missmsg, rtioctl
525 }; */
526 
KERNEL_MODCONFIG()527 KERNEL_MODCONFIG() {
528 
529 	/*_router_ = router;*/
530 	esym_bind(rtinit);
531 	esym_bind(rtalloc);
532 	esym_bind(rtfree);
533 	esym_bind(rtredirect);
534 	esym_bind(rtmissmsg);
535 	esym_bind(rtioctl);
536 }
537