date and time created 83/02/10 21:57:52 by sam
[unix-history] / usr / src / sys / net / route.c
CommitLineData
4ab982ac 1/* route.c 4.15 83/02/02 */
9d03c806
SL
2
3#include "../h/param.h"
ee787340 4#include "../h/systm.h"
9d03c806
SL
5#include "../h/mbuf.h"
6#include "../h/protosw.h"
7#include "../h/socket.h"
ee787340 8#include "../h/ioctl.h"
ee787340 9#include "../net/if.h"
9d03c806
SL
10#include "../net/af.h"
11#include "../net/route.h"
c124e997 12#include <errno.h>
9d03c806 13
a13c006d 14int rttrash; /* routes not in table but not freed */
9d03c806
SL
15/*
16 * Packet routing routines.
17 */
f6311fb6 18rtalloc(ro)
9d03c806
SL
19 register struct route *ro;
20{
21 register struct rtentry *rt, *rtmin;
22 register struct mbuf *m;
9ccc7370
SL
23 register unsigned hash;
24 register int (*match)();
9d03c806
SL
25 struct afhash h;
26 struct sockaddr *dst = &ro->ro_dst;
fc74f0c9 27 int af = dst->sa_family;
9d03c806 28
f6311fb6 29 if (ro->ro_rt && ro->ro_rt->rt_ifp) /* XXX */
9d03c806 30 return;
e65dcd4c
SL
31 if (af >= AF_MAX)
32 return;
9d03c806 33 (*afswitch[af].af_hash)(dst, &h);
fc74f0c9
SL
34 rtmin = 0, hash = h.afh_hosthash;
35 for (m = rthost[hash % RTHASHSIZ]; m; m = m->m_next) {
9d03c806 36 rt = mtod(m, struct rtentry *);
fc74f0c9
SL
37 if (rt->rt_hash != hash)
38 continue;
90137845
SL
39 if ((rt->rt_flags & RTF_UP) == 0 ||
40 (rt->rt_ifp->if_flags & IFF_UP) == 0)
41 continue;
fc74f0c9 42 if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst)))
9d03c806 43 continue;
9d03c806
SL
44 if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
45 rtmin = rt;
46 }
fc74f0c9
SL
47 if (rtmin)
48 goto found;
49
50 hash = h.afh_nethash;
51 match = afswitch[af].af_netmatch;
52 for (m = rtnet[hash % RTHASHSIZ]; m; m = m->m_next) {
53 rt = mtod(m, struct rtentry *);
54 if (rt->rt_hash != hash)
55 continue;
90137845
SL
56 if ((rt->rt_flags & RTF_UP) == 0 ||
57 (rt->rt_ifp->if_flags & IFF_UP) == 0)
58 continue;
fc74f0c9
SL
59 if (rt->rt_dst.sa_family != af || !(*match)(&rt->rt_dst, dst))
60 continue;
61 if (rtmin == 0 || rt->rt_use < rtmin->rt_use)
62 rtmin = rt;
9d03c806 63 }
fc74f0c9
SL
64found:
65 ro->ro_rt = rtmin;
a9f3e174
SL
66 if (rtmin)
67 rtmin->rt_refcnt++;
9d03c806
SL
68}
69
f6311fb6 70rtfree(rt)
c124e997
SL
71 register struct rtentry *rt;
72{
f6311fb6 73
c124e997 74 if (rt == 0)
a1edc12b 75 panic("rtfree");
c124e997 76 rt->rt_refcnt--;
a13c006d
BJ
77 if (rt->rt_refcnt == 0 && (rt->rt_flags&RTF_UP) == 0) {
78 rttrash--;
79 (void) m_free(dtom(rt));
80 }
c124e997
SL
81}
82
9d03c806 83/*
c124e997
SL
84 * Carry out a request to change the routing table. Called by
85 * interfaces at boot time to make their ``local routes'' known
86 * and for ioctl's.
9d03c806 87 */
a13c006d 88rtrequest(req, entry)
9d03c806 89 int req;
a13c006d 90 register struct rtentry *entry;
9d03c806 91{
9d03c806 92 register struct mbuf *m, **mprev;
a13c006d 93 register struct rtentry *rt;
9d03c806 94 struct afhash h;
a13c006d
BJ
95 int af, s, error = 0, hash, (*match)();
96 struct ifnet *ifp;
9d03c806 97
a13c006d 98 af = entry->rt_dst.sa_family;
e65dcd4c
SL
99 if (af >= AF_MAX)
100 return (EAFNOSUPPORT);
a13c006d
BJ
101 (*afswitch[af].af_hash)(&entry->rt_dst, &h);
102 if (entry->rt_flags & RTF_HOST) {
d7887ae9
BJ
103 hash = h.afh_hosthash;
104 mprev = &rthost[hash % RTHASHSIZ];
105 } else {
106 hash = h.afh_nethash;
107 mprev = &rtnet[hash % RTHASHSIZ];
108 }
109 match = afswitch[af].af_netmatch;
c124e997 110 s = splimp();
9d03c806
SL
111 for (; m = *mprev; mprev = &m->m_next) {
112 rt = mtod(m, struct rtentry *);
fc74f0c9 113 if (rt->rt_hash != hash)
9d03c806 114 continue;
a13c006d
BJ
115 if (entry->rt_flags & RTF_HOST) {
116#define equal(a1, a2) \
117 (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0)
118 if (!equal(&rt->rt_dst, &entry->rt_dst))
9d03c806
SL
119 continue;
120 } else {
a13c006d
BJ
121 if (rt->rt_dst.sa_family != entry->rt_dst.sa_family ||
122 (*match)(&rt->rt_dst, &entry->rt_dst) == 0)
9d03c806
SL
123 continue;
124 }
a13c006d 125 if (equal(&rt->rt_gateway, &entry->rt_gateway))
d7887ae9 126 break;
c124e997 127 }
9d03c806
SL
128 switch (req) {
129
130 case SIOCDELRT:
d7887ae9
BJ
131 if (m == 0) {
132 error = ESRCH;
133 goto bad;
134 }
a13c006d 135 *mprev = m->m_next;
d7887ae9 136 if (rt->rt_refcnt > 0) {
a13c006d
BJ
137 rt->rt_flags &= ~RTF_UP;
138 rttrash++;
139 m->m_next = 0;
140 } else
141 (void) m_free(m);
9d03c806
SL
142 break;
143
144 case SIOCADDRT:
a13c006d 145 if (m) {
d7887ae9
BJ
146 error = EEXIST;
147 goto bad;
148 }
a13c006d
BJ
149 ifp = if_ifwithaddr(&entry->rt_gateway);
150 if (ifp == 0) {
151 ifp = if_ifwithnet(&entry->rt_gateway);
152 if (ifp == 0) {
153 error = ENETUNREACH;
154 goto bad;
155 }
156 }
cce93e4b 157 m = m_get(M_DONTWAIT, MT_RTABLE);
c124e997
SL
158 if (m == 0) {
159 error = ENOBUFS;
d7887ae9 160 goto bad;
c124e997 161 }
a13c006d 162 *mprev = m;
9d03c806 163 m->m_off = MMINOFF;
f6311fb6 164 m->m_len = sizeof (struct rtentry);
9d03c806 165 rt = mtod(m, struct rtentry *);
d7887ae9 166 rt->rt_hash = hash;
a13c006d
BJ
167 rt->rt_dst = entry->rt_dst;
168 rt->rt_gateway = entry->rt_gateway;
169 rt->rt_flags =
170 RTF_UP | (entry->rt_flags & (RTF_HOST|RTF_GATEWAY));
fc74f0c9 171 rt->rt_refcnt = 0;
a13c006d
BJ
172 rt->rt_use = 0;
173 rt->rt_ifp = ifp;
9d03c806
SL
174 break;
175 }
c124e997
SL
176bad:
177 splx(s);
178 return (error);
9d03c806 179}
f6311fb6
SL
180
181/*
182 * Set up a routing table entry, normally
183 * for an interface.
184 */
185rtinit(dst, gateway, flags)
186 struct sockaddr *dst, *gateway;
187 int flags;
188{
189 struct rtentry route;
f6311fb6 190
a13c006d 191 bzero((caddr_t)&route, sizeof (route));
f6311fb6
SL
192 route.rt_dst = *dst;
193 route.rt_gateway = *gateway;
194 route.rt_flags = flags;
a1edc12b 195 (void) rtrequest((int)SIOCADDRT, &route);
f6311fb6 196}