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