xref: /csrg-svn/sys/net/route.c (revision 26212)
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.16 (Berkeley) 02/17/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, 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, SIOCADDRT, flags | RTF_DYNAMIC);
169 			rtstat.rts_dynamic++;
170 		} else {
171 			/*
172 			 * Smash the current notion of the gateway to
173 			 * this destination.
174 			 */
175 			rt->rt_gateway = *gateway;
176 		}
177 		rtstat.rts_newgateway++;
178 	} else
179 		rtstat.rts_badredirect++;
180 	rtfree(rt);
181 }
182 
183 /*
184  * Routing table ioctl interface.
185  */
186 rtioctl(cmd, data)
187 	int cmd;
188 	caddr_t data;
189 {
190 
191 	if (cmd != SIOCADDRT && cmd != SIOCDELRT)
192 		return (EINVAL);
193 	if (!suser())
194 		return (u.u_error);
195 	return (rtrequest(cmd, (struct rtentry *)data));
196 }
197 
198 /*
199  * Carry out a request to change the routing table.  Called by
200  * interfaces at boot time to make their ``local routes'' known,
201  * for ioctl's, and as the result of routing redirects.
202  */
203 rtrequest(req, entry)
204 	int req;
205 	register struct rtentry *entry;
206 {
207 	register struct mbuf *m, **mprev;
208 	struct mbuf **mfirst;
209 	register struct rtentry *rt;
210 	struct afhash h;
211 	int s, error = 0, (*match)();
212 	u_int af;
213 	u_long hash;
214 	struct ifaddr *ifa;
215 	struct ifaddr *ifa_ifwithdstaddr();
216 
217 	af = entry->rt_dst.sa_family;
218 	if (af >= AF_MAX)
219 		return (EAFNOSUPPORT);
220 	(*afswitch[af].af_hash)(&entry->rt_dst, &h);
221 	if (entry->rt_flags & RTF_HOST) {
222 		hash = h.afh_hosthash;
223 		mprev = &rthost[RTHASHMOD(hash)];
224 	} else {
225 		hash = h.afh_nethash;
226 		mprev = &rtnet[RTHASHMOD(hash)];
227 	}
228 	match = afswitch[af].af_netmatch;
229 	s = splimp();
230 	for (mfirst = mprev; m = *mprev; mprev = &m->m_next) {
231 		rt = mtod(m, struct rtentry *);
232 		if (rt->rt_hash != hash)
233 			continue;
234 		if (entry->rt_flags & RTF_HOST) {
235 			if (!equal(&rt->rt_dst, &entry->rt_dst))
236 				continue;
237 		} else {
238 			if (rt->rt_dst.sa_family != entry->rt_dst.sa_family ||
239 			    (*match)(&rt->rt_dst, &entry->rt_dst) == 0)
240 				continue;
241 		}
242 		if (equal(&rt->rt_gateway, &entry->rt_gateway))
243 			break;
244 	}
245 	switch (req) {
246 
247 	case SIOCDELRT:
248 		if (m == 0) {
249 			error = ESRCH;
250 			goto bad;
251 		}
252 		*mprev = m->m_next;
253 		if (rt->rt_refcnt > 0) {
254 			rt->rt_flags &= ~RTF_UP;
255 			rttrash++;
256 			m->m_next = 0;
257 		} else
258 			(void) m_free(m);
259 		break;
260 
261 	case SIOCADDRT:
262 		if (m) {
263 			error = EEXIST;
264 			goto bad;
265 		}
266 		if ((entry->rt_flags & RTF_GATEWAY) == 0) {
267 			/*
268 			 * If we are adding a route to an interface,
269 			 * and the interface is a pt to pt link
270 			 * we should search for the destination
271 			 * as our clue to the interface.  Otherwise
272 			 * we can use the local address.
273 			 */
274 			if (entry->rt_flags & RTF_HOST)
275 				ifa = ifa_ifwithdstaddr(&entry->rt_dst);
276 			else
277 				ifa = ifa_ifwithaddr(&entry->rt_gateway);
278 		} else {
279 			/*
280 			 * If we are adding a route to a remote net
281 			 * or host, the gateway may still be on the
282 			 * other end of a pt to pt link.
283 			 */
284 			ifa = ifa_ifwithdstaddr(&entry->rt_gateway);
285 		}
286 		if (ifa == 0) {
287 			ifa = ifa_ifwithnet(&entry->rt_gateway);
288 			if (ifa == 0) {
289 				error = ENETUNREACH;
290 				goto bad;
291 			}
292 		}
293 		m = m_get(M_DONTWAIT, MT_RTABLE);
294 		if (m == 0) {
295 			error = ENOBUFS;
296 			goto bad;
297 		}
298 		m->m_next = *mfirst;
299 		*mfirst = m;
300 		m->m_off = MMINOFF;
301 		m->m_len = sizeof (struct rtentry);
302 		rt = mtod(m, struct rtentry *);
303 		rt->rt_hash = hash;
304 		rt->rt_dst = entry->rt_dst;
305 		rt->rt_gateway = entry->rt_gateway;
306 		rt->rt_flags = RTF_UP |
307 		    (entry->rt_flags & (RTF_HOST|RTF_GATEWAY|RTF_DYNAMIC));
308 		rt->rt_refcnt = 0;
309 		rt->rt_use = 0;
310 		rt->rt_ifp = ifa->ifa_ifp;
311 		break;
312 	}
313 bad:
314 	splx(s);
315 	return (error);
316 }
317 
318 /*
319  * Set up a routing table entry, normally
320  * for an interface.
321  */
322 rtinit(dst, gateway, cmd, flags)
323 	struct sockaddr *dst, *gateway;
324 	int cmd, flags;
325 {
326 	struct rtentry route;
327 
328 	bzero((caddr_t)&route, sizeof (route));
329 	route.rt_dst = *dst;
330 	route.rt_gateway = *gateway;
331 	route.rt_flags = flags;
332 	(void) rtrequest(cmd, &route);
333 }
334