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