xref: /csrg-svn/sys/net/route.c (revision 13049)
1 /*	route.c	4.21	83/06/12	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/protosw.h"
7 #include "../h/socket.h"
8 #include "../h/dir.h"
9 #include "../h/user.h"
10 #include "../h/ioctl.h"
11 #include "../h/errno.h"
12 
13 #include "../net/if.h"
14 #include "../net/af.h"
15 #include "../net/route.h"
16 
17 int	rttrash;		/* routes not in table but not freed */
18 struct	sockaddr wildcard;	/* zero valued cookie for wildcard searches */
19 
20 /*
21  * Packet routing routines.
22  */
23 rtalloc(ro)
24 	register struct route *ro;
25 {
26 	register struct rtentry *rt;
27 	register struct mbuf *m;
28 	register unsigned hash;
29 	struct sockaddr *dst = &ro->ro_dst;
30 	int (*match)(), doinghost;
31 	struct afhash h;
32 	u_int af = dst->sa_family;
33 	struct rtentry *rtmin;
34 	struct mbuf **table;
35 
36 	if (ro->ro_rt && ro->ro_rt->rt_ifp)			/* XXX */
37 		return;
38 	if (af >= AF_MAX)
39 		return;
40 	(*afswitch[af].af_hash)(dst, &h);
41 	rtmin = 0;
42 	match = afswitch[af].af_netmatch;
43 	hash = h.afh_hosthash, table = rthost, doinghost = 1;
44 again:
45 	for (m = table[hash % RTHASHSIZ]; m; m = m->m_next) {
46 		rt = mtod(m, struct rtentry *);
47 		if (rt->rt_hash != hash)
48 			continue;
49 		if ((rt->rt_flags & RTF_UP) == 0 ||
50 		    (rt->rt_ifp->if_flags & IFF_UP) == 0)
51 			continue;
52 		if (doinghost) {
53 			if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst,
54 			    sizeof (*dst)))
55 				continue;
56 		} else {
57 			if (rt->rt_dst.sa_family != af ||
58 			    !(*match)(&rt->rt_dst, dst))
59 				continue;
60 		}
61 		if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
62 			rtmin = rt;
63 	}
64 	if (rtmin == 0 && doinghost) {
65 		doinghost = 0;
66 		hash = h.afh_nethash, table = rtnet;
67 		goto again;
68 	}
69 	/*
70 	 * Check for wildcard gateway, by convention network 0.
71 	 */
72 	if (rtmin == 0 && dst != &wildcard) {
73 		dst = &wildcard, hash = 0;
74 		goto again;
75 	}
76 	ro->ro_rt = rtmin;
77 	if (rtmin == 0) {
78 		rtstat.rts_unreach++;
79 		return;
80 	}
81 	rtmin->rt_refcnt++;
82 	if (dst == &wildcard)
83 		rtstat.rts_wildcard++;
84 }
85 
86 rtfree(rt)
87 	register struct rtentry *rt;
88 {
89 
90 	if (rt == 0)
91 		panic("rtfree");
92 	rt->rt_refcnt--;
93 	if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) {
94 		rttrash--;
95 		(void) m_free(dtom(rt));
96 	}
97 }
98 
99 /*
100  * Force a routing table entry to the specified
101  * destination to go through the given gateway.
102  * Normally called as a result of a routing redirect
103  * message from the network layer.
104  *
105  * N.B.: must be called at splnet or higher
106  *
107  * Should notify all parties with a reference to
108  * the route that it's changed (so, for instance,
109  * current round trip time estimates could be flushed),
110  * but we have no back pointers at the moment.
111  */
112 rtredirect(dst, gateway)
113 	struct sockaddr *dst, *gateway;
114 {
115 	struct route ro;
116 	register struct rtentry *rt;
117 
118 	/* verify the gateway is directly reachable */
119 	if (if_ifwithnet(gateway) == 0) {
120 		rtstat.rts_badredirect++;
121 		return;
122 	}
123 	ro.ro_dst = *dst;
124 	ro.ro_rt = 0;
125 	rtalloc(&ro);
126 	rt = ro.ro_rt;
127 	/*
128 	 * Create a new entry if the lookup failed.
129 	 * This is necessary for hosts which use routing
130 	 * redirects generated by smart gateways to dynamically
131 	 * build the routing tables.
132 	 */
133 	if (rt == 0) {
134 		rtinit(dst, gateway, RTF_GATEWAY);
135 		rtstat.rts_dynamic++;
136 		return;
137 	}
138 	/*
139 	 * Don't listen to the redirect if it's
140 	 * for a route to an interface.
141 	 */
142 	if (rt->rt_flags & RTF_GATEWAY) {
143 		/*
144 		 * Smash the current notion of the gateway to
145 		 * this destination.  This is probably not right,
146 		 * as it's conceivable a flurry of redirects could
147 		 * cause the gateway value to fluctuate wildly during
148 		 * dynamic routing reconfiguration.
149 		 */
150 		rt->rt_gateway = *gateway;
151 		rtfree(rt);
152 		rtstat.rts_newgateway++;
153 		return;
154 	}
155 }
156 
157 /*
158  * Routing table ioctl interface.
159  */
160 rtioctl(cmd, data)
161 	int cmd;
162 	caddr_t data;
163 {
164 
165 	if (cmd != SIOCADDRT && cmd != SIOCDELRT)
166 		return (EINVAL);
167 	if (!suser())
168 		return (u.u_error);
169 	return (rtrequest(cmd, (struct rtentry *)data));
170 }
171 
172 /*
173  * Carry out a request to change the routing table.  Called by
174  * interfaces at boot time to make their ``local routes'' known,
175  * for ioctl's, and as the result of routing redirects.
176  */
177 rtrequest(req, entry)
178 	int req;
179 	register struct rtentry *entry;
180 {
181 	register struct mbuf *m, **mprev;
182 	register struct rtentry *rt;
183 	struct afhash h;
184 	int s, error = 0, hash, (*match)();
185 	u_int af;
186 	struct ifnet *ifp;
187 
188 	af = entry->rt_dst.sa_family;
189 	if (af >= AF_MAX)
190 		return (EAFNOSUPPORT);
191 	(*afswitch[af].af_hash)(&entry->rt_dst, &h);
192 	if (entry->rt_flags & RTF_HOST) {
193 		hash = h.afh_hosthash;
194 		mprev = &rthost[hash % RTHASHSIZ];
195 	} else {
196 		hash = h.afh_nethash;
197 		mprev = &rtnet[hash % RTHASHSIZ];
198 	}
199 	match = afswitch[af].af_netmatch;
200 	s = splimp();
201 	for (; m = *mprev; mprev = &m->m_next) {
202 		rt = mtod(m, struct rtentry *);
203 		if (rt->rt_hash != hash)
204 			continue;
205 		if (entry->rt_flags & RTF_HOST) {
206 #define	equal(a1, a2) \
207 	(bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0)
208 			if (!equal(&rt->rt_dst, &entry->rt_dst))
209 				continue;
210 		} else {
211 			if (rt->rt_dst.sa_family != entry->rt_dst.sa_family ||
212 			    (*match)(&rt->rt_dst, &entry->rt_dst) == 0)
213 				continue;
214 		}
215 		if (equal(&rt->rt_gateway, &entry->rt_gateway))
216 			break;
217 	}
218 	switch (req) {
219 
220 	case SIOCDELRT:
221 		if (m == 0) {
222 			error = ESRCH;
223 			goto bad;
224 		}
225 		*mprev = m->m_next;
226 		if (rt->rt_refcnt > 0) {
227 			rt->rt_flags &= ~RTF_UP;
228 			rttrash++;
229 			m->m_next = 0;
230 		} else
231 			(void) m_free(m);
232 		break;
233 
234 	case SIOCADDRT:
235 		if (m) {
236 			error = EEXIST;
237 			goto bad;
238 		}
239 		ifp = if_ifwithaddr(&entry->rt_gateway);
240 		if (ifp == 0) {
241 			ifp = if_ifwithnet(&entry->rt_gateway);
242 			if (ifp == 0) {
243 				error = ENETUNREACH;
244 				goto bad;
245 			}
246 		}
247 		m = m_get(M_DONTWAIT, MT_RTABLE);
248 		if (m == 0) {
249 			error = ENOBUFS;
250 			goto bad;
251 		}
252 		*mprev = m;
253 		m->m_off = MMINOFF;
254 		m->m_len = sizeof (struct rtentry);
255 		rt = mtod(m, struct rtentry *);
256 		rt->rt_hash = hash;
257 		rt->rt_dst = entry->rt_dst;
258 		rt->rt_gateway = entry->rt_gateway;
259 		rt->rt_flags =
260 		    RTF_UP | (entry->rt_flags & (RTF_HOST|RTF_GATEWAY));
261 		rt->rt_refcnt = 0;
262 		rt->rt_use = 0;
263 		rt->rt_ifp = ifp;
264 		break;
265 	}
266 bad:
267 	splx(s);
268 	return (error);
269 }
270 
271 /*
272  * Set up a routing table entry, normally
273  * for an interface.
274  */
275 rtinit(dst, gateway, flags)
276 	struct sockaddr *dst, *gateway;
277 	int flags;
278 {
279 	struct rtentry route;
280 	int cmd;
281 
282 	if (flags == -1) {
283 		cmd = (int)SIOCDELRT;
284 		flags = 0;
285 	} else {
286 		cmd = (int)SIOCADDRT;
287 	}
288 	bzero((caddr_t)&route, sizeof (route));
289 	route.rt_dst = *dst;
290 	route.rt_gateway = *gateway;
291 	route.rt_flags = flags;
292 	(void) rtrequest(cmd, &route);
293 }
294