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