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