xref: /csrg-svn/sys/net/route.c (revision 26457)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)route.c	6.17 (Berkeley) 03/04/86
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "mbuf.h"
12 #include "protosw.h"
13 #include "socket.h"
14 #include "dir.h"
15 #include "user.h"
16 #include "ioctl.h"
17 #include "errno.h"
18 
19 #include "if.h"
20 #include "af.h"
21 #include "route.h"
22 
23 int	rttrash;		/* routes not in table but not freed */
24 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
25 int	rthashsize = RTHASHSIZ;	/* for netstat, etc. */
26 
27 /*
28  * Packet routing routines.
29  */
30 rtalloc(ro)
31 	register struct route *ro;
32 {
33 	register struct rtentry *rt;
34 	register struct mbuf *m;
35 	register u_long hash;
36 	struct sockaddr *dst = &ro->ro_dst;
37 	int (*match)(), doinghost, s;
38 	struct afhash h;
39 	u_int af = dst->sa_family;
40 	struct mbuf **table;
41 
42 	if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
43 		return;				 /* XXX */
44 	if (af >= AF_MAX)
45 		return;
46 	(*afswitch[af].af_hash)(dst, &h);
47 	match = afswitch[af].af_netmatch;
48 	hash = h.afh_hosthash, table = rthost, doinghost = 1;
49 	s = splnet();
50 again:
51 	for (m = table[RTHASHMOD(hash)]; m; m = m->m_next) {
52 		rt = mtod(m, struct rtentry *);
53 		if (rt->rt_hash != hash)
54 			continue;
55 		if ((rt->rt_flags & RTF_UP) == 0 ||
56 		    (rt->rt_ifp->if_flags & IFF_UP) == 0)
57 			continue;
58 		if (doinghost) {
59 			if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst,
60 			    sizeof (*dst)))
61 				continue;
62 		} else {
63 			if (rt->rt_dst.sa_family != af ||
64 			    !(*match)(&rt->rt_dst, dst))
65 				continue;
66 		}
67 		rt->rt_refcnt++;
68 		splx(s);
69 		if (dst == &wildcard)
70 			rtstat.rts_wildcard++;
71 		ro->ro_rt = rt;
72 		return;
73 	}
74 	if (doinghost) {
75 		doinghost = 0;
76 		hash = h.afh_nethash, table = rtnet;
77 		goto again;
78 	}
79 	/*
80 	 * Check for wildcard gateway, by convention network 0.
81 	 */
82 	if (dst != &wildcard) {
83 		dst = &wildcard, hash = 0;
84 		goto again;
85 	}
86 	splx(s);
87 	rtstat.rts_unreach++;
88 }
89 
90 rtfree(rt)
91 	register struct rtentry *rt;
92 {
93 
94 	if (rt == 0)
95 		panic("rtfree");
96 	rt->rt_refcnt--;
97 	if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) {
98 		rttrash--;
99 		(void) m_free(dtom(rt));
100 	}
101 }
102 
103 /*
104  * Force a routing table entry to the specified
105  * destination to go through the given gateway.
106  * Normally called as a result of a routing redirect
107  * message from the network layer.
108  *
109  * N.B.: must be called at splnet or higher
110  *
111  */
112 rtredirect(dst, gateway, flags, src)
113 	struct sockaddr *dst, *gateway, *src;
114 	int flags;
115 {
116 	struct route ro;
117 	register struct rtentry *rt;
118 
119 	/* verify the gateway is directly reachable */
120 	if (ifa_ifwithnet(gateway) == 0) {
121 		rtstat.rts_badredirect++;
122 		return;
123 	}
124 	ro.ro_dst = *dst;
125 	ro.ro_rt = 0;
126 	rtalloc(&ro);
127 	rt = ro.ro_rt;
128 #define	equal(a1, a2) \
129 	(bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof(struct sockaddr)) == 0)
130 	/*
131 	 * If the redirect isn't from our current router for this dst,
132 	 * it's either old or wrong.  If it redirects us to ourselves,
133 	 * we have a routing loop, perhaps as a result of an interface
134 	 * going down recently.
135 	 */
136 	if ((rt && !equal(src, &rt->rt_gateway)) || ifa_ifwithaddr(gateway)) {
137 		rtstat.rts_badredirect++;
138 		rtfree(rt);
139 		return;
140 	}
141 	/*
142 	 * Create a new entry if we just got back a wildcard entry
143 	 * or the the lookup failed.  This is necessary for hosts
144 	 * which use routing redirects generated by smart gateways
145 	 * to dynamically build the routing tables.
146 	 */
147 	if (rt &&
148 	    (*afswitch[dst->sa_family].af_netmatch)(&wildcard, &rt->rt_dst)) {
149 		rtfree(rt);
150 		rt = 0;
151 	}
152 	if (rt == 0) {
153 		rtinit(dst, gateway, (int)SIOCADDRT,
154 		    (flags & RTF_HOST) | RTF_GATEWAY | RTF_DYNAMIC);
155 		rtstat.rts_dynamic++;
156 		return;
157 	}
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 			rtinit(dst, gateway, (int)SIOCADDRT,
169 			    flags | RTF_DYNAMIC);
170 			rtstat.rts_dynamic++;
171 		} else {
172 			/*
173 			 * Smash the current notion of the gateway to
174 			 * this destination.
175 			 */
176 			rt->rt_gateway = *gateway;
177 		}
178 		rtstat.rts_newgateway++;
179 	} else
180 		rtstat.rts_badredirect++;
181 	rtfree(rt);
182 }
183 
184 /*
185  * Routing table ioctl interface.
186  */
187 rtioctl(cmd, data)
188 	int cmd;
189 	caddr_t data;
190 {
191 
192 	if (cmd != SIOCADDRT && cmd != SIOCDELRT)
193 		return (EINVAL);
194 	if (!suser())
195 		return (u.u_error);
196 	return (rtrequest(cmd, (struct rtentry *)data));
197 }
198 
199 /*
200  * Carry out a request to change the routing table.  Called by
201  * interfaces at boot time to make their ``local routes'' known,
202  * for ioctl's, and as the result of routing redirects.
203  */
204 rtrequest(req, entry)
205 	int req;
206 	register struct rtentry *entry;
207 {
208 	register struct mbuf *m, **mprev;
209 	struct mbuf **mfirst;
210 	register struct rtentry *rt;
211 	struct afhash h;
212 	int s, error = 0, (*match)();
213 	u_int af;
214 	u_long hash;
215 	struct ifaddr *ifa;
216 	struct ifaddr *ifa_ifwithdstaddr();
217 
218 	af = entry->rt_dst.sa_family;
219 	if (af >= AF_MAX)
220 		return (EAFNOSUPPORT);
221 	(*afswitch[af].af_hash)(&entry->rt_dst, &h);
222 	if (entry->rt_flags & RTF_HOST) {
223 		hash = h.afh_hosthash;
224 		mprev = &rthost[RTHASHMOD(hash)];
225 	} else {
226 		hash = h.afh_nethash;
227 		mprev = &rtnet[RTHASHMOD(hash)];
228 	}
229 	match = afswitch[af].af_netmatch;
230 	s = splimp();
231 	for (mfirst = mprev; m = *mprev; mprev = &m->m_next) {
232 		rt = mtod(m, struct rtentry *);
233 		if (rt->rt_hash != hash)
234 			continue;
235 		if (entry->rt_flags & RTF_HOST) {
236 			if (!equal(&rt->rt_dst, &entry->rt_dst))
237 				continue;
238 		} else {
239 			if (rt->rt_dst.sa_family != entry->rt_dst.sa_family ||
240 			    (*match)(&rt->rt_dst, &entry->rt_dst) == 0)
241 				continue;
242 		}
243 		if (equal(&rt->rt_gateway, &entry->rt_gateway))
244 			break;
245 	}
246 	switch (req) {
247 
248 	case SIOCDELRT:
249 		if (m == 0) {
250 			error = ESRCH;
251 			goto bad;
252 		}
253 		*mprev = m->m_next;
254 		if (rt->rt_refcnt > 0) {
255 			rt->rt_flags &= ~RTF_UP;
256 			rttrash++;
257 			m->m_next = 0;
258 		} else
259 			(void) m_free(m);
260 		break;
261 
262 	case SIOCADDRT:
263 		if (m) {
264 			error = EEXIST;
265 			goto bad;
266 		}
267 		if ((entry->rt_flags & RTF_GATEWAY) == 0) {
268 			/*
269 			 * If we are adding a route to an interface,
270 			 * and the interface is a pt to pt link
271 			 * we should search for the destination
272 			 * as our clue to the interface.  Otherwise
273 			 * we can use the local address.
274 			 */
275 			if (entry->rt_flags & RTF_HOST)
276 				ifa = ifa_ifwithdstaddr(&entry->rt_dst);
277 			else
278 				ifa = ifa_ifwithaddr(&entry->rt_gateway);
279 		} else {
280 			/*
281 			 * If we are adding a route to a remote net
282 			 * or host, the gateway may still be on the
283 			 * other end of a pt to pt link.
284 			 */
285 			ifa = ifa_ifwithdstaddr(&entry->rt_gateway);
286 		}
287 		if (ifa == 0) {
288 			ifa = ifa_ifwithnet(&entry->rt_gateway);
289 			if (ifa == 0) {
290 				error = ENETUNREACH;
291 				goto bad;
292 			}
293 		}
294 		m = m_get(M_DONTWAIT, MT_RTABLE);
295 		if (m == 0) {
296 			error = ENOBUFS;
297 			goto bad;
298 		}
299 		m->m_next = *mfirst;
300 		*mfirst = m;
301 		m->m_off = MMINOFF;
302 		m->m_len = sizeof (struct rtentry);
303 		rt = mtod(m, struct rtentry *);
304 		rt->rt_hash = hash;
305 		rt->rt_dst = entry->rt_dst;
306 		rt->rt_gateway = entry->rt_gateway;
307 		rt->rt_flags = RTF_UP |
308 		    (entry->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_DYNAMIC));
309 		rt->rt_refcnt = 0;
310 		rt->rt_use = 0;
311 		rt->rt_ifp = ifa->ifa_ifp;
312 		break;
313 	}
314 bad:
315 	splx(s);
316 	return (error);
317 }
318 
319 /*
320  * Set up a routing table entry, normally
321  * for an interface.
322  */
323 rtinit(dst, gateway, cmd, flags)
324 	struct sockaddr *dst, *gateway;
325 	int cmd, flags;
326 {
327 	struct rtentry route;
328 
329 	bzero((caddr_t)&route, sizeof (route));
330 	route.rt_dst = *dst;
331 	route.rt_gateway = *gateway;
332 	route.rt_flags = flags;
333 	(void) rtrequest(cmd, &route);
334 }
335