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