Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1984, 1985, 1986, 1987 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 | * | |
42524852 | 33 | * from: @(#)ns.c 7.8 (Berkeley) 6/27/91 |
4c45483e | 34 | * $Id: ns.c,v 1.3 1993/11/07 17:50:23 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
38 | #include "mbuf.h" | |
39 | #include "ioctl.h" | |
40 | #include "protosw.h" | |
41 | #include "errno.h" | |
42 | #include "socket.h" | |
43 | #include "socketvar.h" | |
44 | ||
45 | ||
46 | #include "../net/if.h" | |
47 | #include "../net/route.h" | |
15637ed4 RG |
48 | |
49 | #include "ns.h" | |
50 | #include "ns_if.h" | |
51 | ||
52 | #ifdef NS | |
53 | ||
4c45483e GW |
54 | static void ns_ifscrub(struct ifnet *, struct ns_ifaddr *); |
55 | ||
15637ed4 | 56 | struct ns_ifaddr *ns_ifaddr; |
8ace4366 GW |
57 | struct ifqueue nsintrq; |
58 | ||
15637ed4 RG |
59 | int ns_interfaces; |
60 | extern struct sockaddr_ns ns_netmask, ns_hostmask; | |
8ace4366 GW |
61 | union ns_host ns_thishost; |
62 | union ns_host ns_zerohost; | |
63 | union ns_host ns_braodhost; | |
64 | union ns_net ns_zeronet; | |
65 | union ns_net ns_broadnet; | |
15637ed4 RG |
66 | |
67 | /* | |
68 | * Generic internet control operations (ioctl's). | |
69 | */ | |
70 | /* ARGSUSED */ | |
4c45483e | 71 | int |
15637ed4 RG |
72 | ns_control(so, cmd, data, ifp) |
73 | struct socket *so; | |
74 | int cmd; | |
75 | caddr_t data; | |
76 | register struct ifnet *ifp; | |
77 | { | |
78 | register struct ifreq *ifr = (struct ifreq *)data; | |
79 | register struct ns_aliasreq *ifra = (struct ns_aliasreq *)data; | |
80 | register struct ns_ifaddr *ia; | |
81 | struct ifaddr *ifa; | |
82 | struct ns_ifaddr *oia; | |
83 | struct mbuf *m; | |
84 | int error, dstIsNew, hostIsNew; | |
85 | ||
86 | /* | |
87 | * Find address for this interface, if it exists. | |
88 | */ | |
89 | if (ifp == 0) | |
90 | return (EADDRNOTAVAIL); | |
91 | for (ia = ns_ifaddr; ia; ia = ia->ia_next) | |
92 | if (ia->ia_ifp == ifp) | |
93 | break; | |
94 | ||
95 | switch (cmd) { | |
96 | ||
97 | case SIOCGIFADDR: | |
98 | if (ia == (struct ns_ifaddr *)0) | |
99 | return (EADDRNOTAVAIL); | |
100 | *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr; | |
101 | return (0); | |
102 | ||
103 | ||
104 | case SIOCGIFBRDADDR: | |
105 | if (ia == (struct ns_ifaddr *)0) | |
106 | return (EADDRNOTAVAIL); | |
107 | if ((ifp->if_flags & IFF_BROADCAST) == 0) | |
108 | return (EINVAL); | |
109 | *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr; | |
110 | return (0); | |
111 | ||
112 | case SIOCGIFDSTADDR: | |
113 | if (ia == (struct ns_ifaddr *)0) | |
114 | return (EADDRNOTAVAIL); | |
115 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) | |
116 | return (EINVAL); | |
117 | *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr; | |
118 | return (0); | |
119 | } | |
120 | ||
121 | if ((so->so_state & SS_PRIV) == 0) | |
122 | return (EPERM); | |
123 | ||
124 | switch (cmd) { | |
125 | case SIOCAIFADDR: | |
126 | case SIOCDIFADDR: | |
127 | if (ifra->ifra_addr.sns_family == AF_NS) | |
128 | for (oia = ia; ia; ia = ia->ia_next) { | |
129 | if (ia->ia_ifp == ifp && | |
130 | ns_neteq(ia->ia_addr.sns_addr, | |
131 | ifra->ifra_addr.sns_addr)) | |
132 | break; | |
133 | } | |
134 | if (cmd == SIOCDIFADDR && ia == 0) | |
135 | return (EADDRNOTAVAIL); | |
136 | /* FALLTHROUGH */ | |
137 | ||
138 | case SIOCSIFADDR: | |
139 | case SIOCSIFDSTADDR: | |
140 | if (ia == (struct ns_ifaddr *)0) { | |
141 | m = m_getclr(M_WAIT, MT_IFADDR); | |
142 | if (m == (struct mbuf *)NULL) | |
143 | return (ENOBUFS); | |
144 | if (ia = ns_ifaddr) { | |
145 | for ( ; ia->ia_next; ia = ia->ia_next) | |
146 | ; | |
147 | ia->ia_next = mtod(m, struct ns_ifaddr *); | |
148 | } else | |
149 | ns_ifaddr = mtod(m, struct ns_ifaddr *); | |
150 | ia = mtod(m, struct ns_ifaddr *); | |
151 | if (ifa = ifp->if_addrlist) { | |
152 | for ( ; ifa->ifa_next; ifa = ifa->ifa_next) | |
153 | ; | |
154 | ifa->ifa_next = (struct ifaddr *) ia; | |
155 | } else | |
156 | ifp->if_addrlist = (struct ifaddr *) ia; | |
157 | ia->ia_ifp = ifp; | |
158 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; | |
159 | ||
160 | ia->ia_ifa.ifa_netmask = | |
161 | (struct sockaddr *)&ns_netmask; | |
162 | ||
163 | ia->ia_ifa.ifa_dstaddr = | |
164 | (struct sockaddr *)&ia->ia_dstaddr; | |
165 | if (ifp->if_flags & IFF_BROADCAST) { | |
166 | ia->ia_broadaddr.sns_family = AF_NS; | |
167 | ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr); | |
168 | ia->ia_broadaddr.sns_addr.x_host = ns_broadhost; | |
169 | } | |
170 | ns_interfaces++; | |
171 | } | |
172 | } | |
173 | ||
174 | switch (cmd) { | |
175 | int error; | |
176 | ||
177 | case SIOCSIFDSTADDR: | |
178 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) | |
179 | return (EINVAL); | |
180 | if (ia->ia_flags & IFA_ROUTE) { | |
181 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); | |
182 | ia->ia_flags &= ~IFA_ROUTE; | |
183 | } | |
184 | if (ifp->if_ioctl) { | |
4c45483e GW |
185 | error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, |
186 | (caddr_t)ia); | |
15637ed4 RG |
187 | if (error) |
188 | return (error); | |
189 | } | |
190 | *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr; | |
191 | return (0); | |
192 | ||
193 | case SIOCSIFADDR: | |
194 | return (ns_ifinit(ifp, ia, | |
195 | (struct sockaddr_ns *)&ifr->ifr_addr, 1)); | |
196 | ||
197 | case SIOCDIFADDR: | |
4c45483e | 198 | error = 0; |
15637ed4 RG |
199 | ns_ifscrub(ifp, ia); |
200 | if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia) | |
201 | ifp->if_addrlist = ifa->ifa_next; | |
202 | else { | |
203 | while (ifa->ifa_next && | |
204 | (ifa->ifa_next != (struct ifaddr *)ia)) | |
205 | ifa = ifa->ifa_next; | |
206 | if (ifa->ifa_next) | |
207 | ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next; | |
208 | else | |
209 | printf("Couldn't unlink nsifaddr from ifp\n"); | |
210 | } | |
211 | oia = ia; | |
212 | if (oia == (ia = ns_ifaddr)) { | |
213 | ns_ifaddr = ia->ia_next; | |
214 | } else { | |
215 | while (ia->ia_next && (ia->ia_next != oia)) { | |
216 | ia = ia->ia_next; | |
217 | } | |
218 | if (ia->ia_next) | |
219 | ia->ia_next = oia->ia_next; | |
220 | else | |
221 | printf("Didn't unlink nsifadr from list\n"); | |
222 | } | |
223 | (void) m_free(dtom(oia)); | |
224 | if (0 == --ns_interfaces) { | |
225 | /* | |
226 | * We reset to virginity and start all over again | |
227 | */ | |
228 | ns_thishost = ns_zerohost; | |
229 | } | |
230 | return (0); | |
231 | ||
232 | case SIOCAIFADDR: | |
4c45483e | 233 | error = 0; |
15637ed4 RG |
234 | dstIsNew = 0; hostIsNew = 1; |
235 | if (ia->ia_addr.sns_family == AF_NS) { | |
236 | if (ifra->ifra_addr.sns_len == 0) { | |
237 | ifra->ifra_addr = ia->ia_addr; | |
238 | hostIsNew = 0; | |
239 | } else if (ns_neteq(ifra->ifra_addr.sns_addr, | |
240 | ia->ia_addr.sns_addr)) | |
241 | hostIsNew = 0; | |
242 | } | |
243 | if ((ifp->if_flags & IFF_POINTOPOINT) && | |
244 | (ifra->ifra_dstaddr.sns_family == AF_NS)) { | |
245 | if (hostIsNew == 0) | |
246 | ns_ifscrub(ifp, ia); | |
247 | ia->ia_dstaddr = ifra->ifra_dstaddr; | |
248 | dstIsNew = 1; | |
249 | } | |
250 | if (ifra->ifra_addr.sns_family == AF_NS && | |
251 | (hostIsNew || dstIsNew)) | |
252 | error = ns_ifinit(ifp, ia, &ifra->ifra_addr, 0); | |
253 | return (error); | |
254 | ||
255 | default: | |
256 | if (ifp->if_ioctl == 0) | |
257 | return (EOPNOTSUPP); | |
258 | return ((*ifp->if_ioctl)(ifp, cmd, data)); | |
259 | } | |
260 | } | |
261 | ||
262 | /* | |
263 | * Delete any previous route for an old address. | |
264 | */ | |
4c45483e | 265 | static void |
15637ed4 RG |
266 | ns_ifscrub(ifp, ia) |
267 | register struct ifnet *ifp; | |
268 | register struct ns_ifaddr *ia; | |
269 | { | |
270 | if (ia->ia_flags & IFA_ROUTE) { | |
271 | if (ifp->if_flags & IFF_POINTOPOINT) { | |
272 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); | |
273 | } else | |
274 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); | |
275 | ia->ia_flags &= ~IFA_ROUTE; | |
276 | } | |
277 | } | |
278 | /* | |
279 | * Initialize an interface's internet address | |
280 | * and routing table entry. | |
281 | */ | |
4c45483e | 282 | int |
15637ed4 RG |
283 | ns_ifinit(ifp, ia, sns, scrub) |
284 | register struct ifnet *ifp; | |
285 | register struct ns_ifaddr *ia; | |
286 | register struct sockaddr_ns *sns; | |
4c45483e | 287 | int scrub; |
15637ed4 RG |
288 | { |
289 | struct sockaddr_ns oldaddr; | |
290 | register union ns_host *h = &ia->ia_addr.sns_addr.x_host; | |
291 | int s = splimp(), error; | |
292 | ||
293 | /* | |
294 | * Set up new addresses. | |
295 | */ | |
296 | oldaddr = ia->ia_addr; | |
297 | ia->ia_addr = *sns; | |
298 | /* | |
299 | * The convention we shall adopt for naming is that | |
300 | * a supplied address of zero means that "we don't care". | |
301 | * if there is a single interface, use the address of that | |
302 | * interface as our 6 byte host address. | |
303 | * if there are multiple interfaces, use any address already | |
304 | * used. | |
305 | * | |
306 | * Give the interface a chance to initialize | |
307 | * if this is its first address, | |
308 | * and to validate the address if necessary. | |
309 | */ | |
310 | if (ns_hosteqnh(ns_thishost, ns_zerohost)) { | |
311 | if (ifp->if_ioctl && | |
4c45483e GW |
312 | (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, |
313 | (caddr_t)ia))) { | |
15637ed4 RG |
314 | ia->ia_addr = oldaddr; |
315 | splx(s); | |
316 | return (error); | |
317 | } | |
318 | ns_thishost = *h; | |
319 | } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost) | |
320 | || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) { | |
321 | *h = ns_thishost; | |
322 | if (ifp->if_ioctl && | |
4c45483e GW |
323 | (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, |
324 | (caddr_t)ia))) { | |
15637ed4 RG |
325 | ia->ia_addr = oldaddr; |
326 | splx(s); | |
327 | return (error); | |
328 | } | |
329 | if (!ns_hosteqnh(ns_thishost,*h)) { | |
330 | ia->ia_addr = oldaddr; | |
331 | splx(s); | |
332 | return (EINVAL); | |
333 | } | |
334 | } else { | |
335 | ia->ia_addr = oldaddr; | |
336 | splx(s); | |
337 | return (EINVAL); | |
338 | } | |
339 | /* | |
340 | * Add route for the network. | |
341 | */ | |
342 | if (scrub) { | |
343 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; | |
344 | ns_ifscrub(ifp, ia); | |
345 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; | |
346 | } | |
347 | if (ifp->if_flags & IFF_POINTOPOINT) | |
348 | rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); | |
349 | else { | |
350 | ia->ia_broadaddr.sns_addr.x_net = ia->ia_net; | |
351 | rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP); | |
352 | } | |
353 | ia->ia_flags |= IFA_ROUTE; | |
354 | return (0); | |
355 | } | |
356 | ||
357 | /* | |
358 | * Return address info for specified internet network. | |
359 | */ | |
360 | struct ns_ifaddr * | |
361 | ns_iaonnetof(dst) | |
362 | register struct ns_addr *dst; | |
363 | { | |
364 | register struct ns_ifaddr *ia; | |
365 | register struct ns_addr *compare; | |
366 | register struct ifnet *ifp; | |
367 | struct ns_ifaddr *ia_maybe = 0; | |
368 | union ns_net net = dst->x_net; | |
369 | ||
370 | for (ia = ns_ifaddr; ia; ia = ia->ia_next) { | |
371 | if (ifp = ia->ia_ifp) { | |
372 | if (ifp->if_flags & IFF_POINTOPOINT) { | |
373 | compare = &satons_addr(ia->ia_dstaddr); | |
374 | if (ns_hosteq(*dst, *compare)) | |
375 | return (ia); | |
376 | if (ns_neteqnn(net, ia->ia_net)) | |
377 | ia_maybe = ia; | |
378 | } else { | |
379 | if (ns_neteqnn(net, ia->ia_net)) | |
380 | return (ia); | |
381 | } | |
382 | } | |
383 | } | |
384 | return (ia_maybe); | |
385 | } | |
386 | #endif |