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