Commit | Line | Data |
---|---|---|
a9f3e174 | 1 | /* route.c 4.6 82/03/31 */ |
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 SL |
15 | |
16 | /* | |
17 | * Packet routing routines. | |
18 | */ | |
19 | ||
f6311fb6 | 20 | rtalloc(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 SL |
30 | COUNT(RTALLOC); |
31 | if (ro->ro_rt && ro->ro_rt->rt_ifp) /* XXX */ | |
9d03c806 SL |
32 | return; |
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; | |
39 | if (bcmp((caddr_t)&rt->rt_dst, (caddr_t)dst, sizeof (*dst))) | |
9d03c806 | 40 | continue; |
9d03c806 SL |
41 | if (rtmin == 0 || rt->rt_use < rtmin->rt_use) |
42 | rtmin = rt; | |
43 | } | |
fc74f0c9 SL |
44 | if (rtmin) |
45 | goto found; | |
46 | ||
47 | hash = h.afh_nethash; | |
48 | match = afswitch[af].af_netmatch; | |
49 | for (m = rtnet[hash % RTHASHSIZ]; m; m = m->m_next) { | |
50 | rt = mtod(m, struct rtentry *); | |
51 | if (rt->rt_hash != hash) | |
52 | continue; | |
53 | if (rt->rt_dst.sa_family != af || !(*match)(&rt->rt_dst, dst)) | |
54 | continue; | |
55 | if (rtmin == 0 || rt->rt_use < rtmin->rt_use) | |
56 | rtmin = rt; | |
9d03c806 | 57 | } |
fc74f0c9 SL |
58 | found: |
59 | ro->ro_rt = rtmin; | |
a9f3e174 SL |
60 | if (rtmin) |
61 | rtmin->rt_refcnt++; | |
9d03c806 SL |
62 | } |
63 | ||
f6311fb6 | 64 | rtfree(rt) |
c124e997 SL |
65 | register struct rtentry *rt; |
66 | { | |
f6311fb6 | 67 | |
c124e997 SL |
68 | if (rt == 0) |
69 | panic("freeroute"); | |
70 | rt->rt_refcnt--; | |
71 | /* on refcnt == 0 reclaim? notify someone? */ | |
72 | } | |
73 | ||
fc74f0c9 SL |
74 | #define equal(a1, a2) \ |
75 | (bcmp((caddr_t)(a1), (caddr_t)(a2), sizeof (struct sockaddr)) == 0) | |
9d03c806 | 76 | /* |
c124e997 SL |
77 | * Carry out a request to change the routing table. Called by |
78 | * interfaces at boot time to make their ``local routes'' known | |
79 | * and for ioctl's. | |
9d03c806 SL |
80 | */ |
81 | rtrequest(req, new) | |
82 | int req; | |
83 | register struct rtentry *new; | |
84 | { | |
85 | register struct rtentry *rt; | |
86 | register struct mbuf *m, **mprev; | |
fc74f0c9 SL |
87 | register int hash, (*match)(); |
88 | register struct sockaddr *sa = &new->rt_dst; | |
89 | register struct sockaddr *gate = &new->rt_gateway; | |
9d03c806 | 90 | struct afhash h; |
a9f3e174 | 91 | struct mbuf **oldmprev; |
c124e997 | 92 | int af = sa->sa_family, doinghost, s, error = 0; |
9d03c806 | 93 | |
c124e997 | 94 | COUNT(RTREQUEST); |
9d03c806 | 95 | (*afswitch[af].af_hash)(sa, &h); |
f6311fb6 | 96 | hash = h.afh_hosthash; |
fc74f0c9 | 97 | mprev = &rthost[hash % RTHASHSIZ]; |
9d03c806 | 98 | doinghost = 1; |
c124e997 | 99 | s = splimp(); |
9d03c806 SL |
100 | again: |
101 | for (; m = *mprev; mprev = &m->m_next) { | |
102 | rt = mtod(m, struct rtentry *); | |
fc74f0c9 | 103 | if (rt->rt_hash != hash) |
9d03c806 SL |
104 | continue; |
105 | if (doinghost) { | |
fc74f0c9 | 106 | if (!equal(&rt->rt_dst, sa)) |
9d03c806 SL |
107 | continue; |
108 | } else { | |
fc74f0c9 SL |
109 | if (rt->rt_dst.sa_family != sa->sa_family || |
110 | (*match)(&rt->rt_dst, sa) == 0) | |
9d03c806 SL |
111 | continue; |
112 | } | |
c124e997 | 113 | /* require full match on deletions */ |
fc74f0c9 | 114 | if (req == SIOCDELRT && !equal(&rt->rt_gateway, gate)) |
c124e997 SL |
115 | continue; |
116 | /* don't keep multiple identical entries */ | |
fc74f0c9 | 117 | if (req == SIOCADDRT && equal(&rt->rt_gateway, gate)) { |
c124e997 SL |
118 | error = EEXIST; |
119 | goto bad; | |
120 | } | |
9d03c806 SL |
121 | break; |
122 | } | |
123 | if (m == 0 && doinghost) { | |
f6311fb6 | 124 | hash = h.afh_nethash; |
a9f3e174 | 125 | oldmprev = mprev; |
fc74f0c9 SL |
126 | mprev = &rtnet[hash % RTHASHSIZ]; |
127 | match = afswitch[af].af_netmatch; | |
128 | doinghost = 0; | |
9d03c806 SL |
129 | goto again; |
130 | } | |
fc74f0c9 | 131 | |
c124e997 SL |
132 | if (m == 0 && req != SIOCADDRT) { |
133 | error = ESRCH; | |
134 | goto bad; | |
135 | } | |
fc74f0c9 | 136 | found: |
9d03c806 SL |
137 | switch (req) { |
138 | ||
139 | case SIOCDELRT: | |
140 | rt->rt_flags &= ~RTF_UP; | |
141 | if (rt->rt_refcnt > 0) /* should we notify protocols? */ | |
c124e997 SL |
142 | error = EBUSY; |
143 | else | |
144 | *mprev = m_free(m); | |
9d03c806 SL |
145 | break; |
146 | ||
147 | case SIOCCHGRT: | |
148 | rt->rt_flags = new->rt_flags; | |
149 | if (rt->rt_refcnt > 0) | |
c124e997 | 150 | error = EBUSY; |
fc74f0c9 | 151 | else if (!equal(&rt->rt_gateway, gate)) |
9d03c806 SL |
152 | goto newneighbor; |
153 | break; | |
154 | ||
155 | case SIOCADDRT: | |
f6311fb6 | 156 | m = m_get(M_DONTWAIT); |
c124e997 SL |
157 | if (m == 0) { |
158 | error = ENOBUFS; | |
159 | break; | |
160 | } | |
9d03c806 | 161 | m->m_off = MMINOFF; |
f6311fb6 | 162 | m->m_len = sizeof (struct rtentry); |
9d03c806 SL |
163 | rt = mtod(m, struct rtentry *); |
164 | *rt = *new; | |
a9f3e174 SL |
165 | if (new->rt_flags & RTF_HOST) { |
166 | rt->rt_hash = h.afh_hosthash; | |
167 | *oldmprev = m; | |
168 | } else { | |
169 | rt->rt_hash = h.afh_nethash; | |
170 | *mprev = m; | |
171 | } | |
fc74f0c9 SL |
172 | rt->rt_use = 0; |
173 | rt->rt_refcnt = 0; | |
9d03c806 | 174 | newneighbor: |
fc74f0c9 | 175 | rt->rt_ifp = if_ifwithnet(gate); |
9d03c806 SL |
176 | if (rt->rt_ifp == 0) |
177 | rt->rt_flags &= ~RTF_UP; | |
9d03c806 SL |
178 | break; |
179 | } | |
c124e997 SL |
180 | bad: |
181 | splx(s); | |
182 | return (error); | |
9d03c806 | 183 | } |
f6311fb6 SL |
184 | |
185 | /* | |
186 | * Set up a routing table entry, normally | |
187 | * for an interface. | |
188 | */ | |
189 | rtinit(dst, gateway, flags) | |
190 | struct sockaddr *dst, *gateway; | |
191 | int flags; | |
192 | { | |
193 | struct rtentry route; | |
194 | struct route ro; | |
195 | ||
196 | route.rt_dst = *dst; | |
197 | route.rt_gateway = *gateway; | |
198 | route.rt_flags = flags; | |
199 | route.rt_use = 0; | |
200 | (void) rtrequest(SIOCADDRT, &route); | |
201 | ro.ro_rt = 0; | |
202 | ro.ro_dst = *dst; | |
203 | rtalloc(&ro); | |
204 | } |