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