2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
35 * $DragonFly: src/sys/netproto/ns/ns.c,v 1.5 2003/09/06 21:51:12 drhodus Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/ioctl.h>
43 #include <sys/protosw.h>
44 #include <sys/errno.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
49 #include <net/route.h>
58 struct ns_ifaddr *ns_ifaddr;
60 extern struct sockaddr_ns ns_netmask, ns_hostmask;
63 * Generic internet control operations (ioctl's).
67 ns_control(so, cmd, data, ifp)
73 struct ifreq *ifr = (struct ifreq *)data;
74 struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
76 struct ifaddr *ifa = NULL; /* XXX used ininitialized ?*/
77 struct ns_ifaddr *oia;
78 int dstIsNew, hostIsNew;
79 int error = 0; /* initalize because of scoping */
82 * Find address for this interface, if it exists.
85 return (EADDRNOTAVAIL);
86 for (ia = ns_ifaddr; ia; ia = ia->ia_next)
87 if (ia->ia_ifp == ifp)
93 if (ia == (struct ns_ifaddr *)0)
94 return (EADDRNOTAVAIL);
95 *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr;
100 if (ia == (struct ns_ifaddr *)0)
101 return (EADDRNOTAVAIL);
102 if ((ifp->if_flags & IFF_BROADCAST) == 0)
104 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
108 if (ia == (struct ns_ifaddr *)0)
109 return (EADDRNOTAVAIL);
110 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
112 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
116 #ifdef NS_PRIV_SOCKETS
117 if ((so->so_state & SS_PRIV) == 0)
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))
131 if (cmd == SIOCDIFADDR && ia == 0)
132 return (EADDRNOTAVAIL);
137 if (ia == (struct ns_ifaddr *)0) {
138 oia = (struct ns_ifaddr *)
139 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
140 if (oia == (struct ns_ifaddr *)NULL)
142 bzero((caddr_t)oia, sizeof(*oia));
143 if ((ia = ns_ifaddr) != NULL) {
144 for ( ; ia->ia_next; ia = ia->ia_next)
151 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
153 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
155 ia->ia_ifa.ifa_netmask =
156 (struct sockaddr *)&ns_netmask;
158 ia->ia_ifa.ifa_dstaddr =
159 (struct sockaddr *)&ia->ia_dstaddr;
160 if (ifp->if_flags & IFF_BROADCAST) {
161 ia->ia_broadaddr.sns_family = AF_NS;
162 ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr);
163 ia->ia_broadaddr.sns_addr.x_host = ns_broadhost;
171 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
173 if (ia->ia_flags & IFA_ROUTE) {
174 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
175 ia->ia_flags &= ~IFA_ROUTE;
178 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
183 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
187 return (ns_ifinit(ifp, (struct ns_ifaddr *)ia,
188 (struct sockaddr_ns *)&ifr->ifr_addr, 1));
191 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
192 /* XXX not on list */
194 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
195 if (oia == (ia = ns_ifaddr)) {
196 ns_ifaddr = ia->ia_next;
198 while (ia->ia_next && (ia->ia_next != oia)) {
202 ia->ia_next = oia->ia_next;
204 printf("Didn't unlink nsifadr from list\n");
206 IFAFREE((&oia->ia_ifa));
207 if (0 == --ns_interfaces) {
209 * We reset to virginity and start all over again
211 ns_thishost = ns_zerohost;
216 dstIsNew = 0; hostIsNew = 1;
217 if (ia->ia_addr.sns_family == AF_NS) {
218 if (ifra->ifra_addr.sns_len == 0) {
219 ifra->ifra_addr = ia->ia_addr;
221 } else if (ns_neteq(ifra->ifra_addr.sns_addr,
222 ia->ia_addr.sns_addr))
225 if ((ifp->if_flags & IFF_POINTOPOINT) &&
226 (ifra->ifra_dstaddr.sns_family == AF_NS)) {
228 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
229 ia->ia_dstaddr = ifra->ifra_dstaddr;
232 if (ifra->ifra_addr.sns_family == AF_NS &&
233 (hostIsNew || dstIsNew))
234 error = ns_ifinit(ifp, (struct ns_ifra_addr *)ia,
235 &ifra->ifra_addr, 0);
239 if (ifp->if_ioctl == 0)
241 return ((*ifp->if_ioctl)(ifp, cmd, data));
246 * Delete any previous route for an old address.
251 struct ns_ifaddr *ia;
253 if (ia->ia_flags & IFA_ROUTE) {
254 if (ifp->if_flags & IFF_POINTOPOINT) {
255 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
257 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
258 ia->ia_flags &= ~IFA_ROUTE;
262 * Initialize an interface's internet address
263 * and routing table entry.
266 ns_ifinit(ifp, ia, sns, scrub)
268 struct ns_ifaddr *ia;
269 struct sockaddr_ns *sns;
272 struct sockaddr_ns oldaddr;
273 union ns_host *h = &ia->ia_addr.sns_addr.x_host;
274 int s = splimp(), error;
277 * Set up new addresses.
279 oldaddr = ia->ia_addr;
282 * The convention we shall adopt for naming is that
283 * a supplied address of zero means that "we don't care".
284 * if there is a single interface, use the address of that
285 * interface as our 6 byte host address.
286 * if there are multiple interfaces, use any address already
289 * Give the interface a chance to initialize
290 * if this is its first address,
291 * and to validate the address if necessary.
293 if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
295 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR,
297 ia->ia_addr = oldaddr;
302 } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
303 || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
306 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR,
308 ia->ia_addr = oldaddr;
312 if (!ns_hosteqnh(ns_thishost,*h)) {
313 ia->ia_addr = oldaddr;
318 ia->ia_addr = oldaddr;
322 ia->ia_ifa.ifa_metric = ifp->if_metric;
324 * Add route for the network.
327 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
329 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
331 if (ifp->if_flags & IFF_POINTOPOINT)
332 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
334 ia->ia_broadaddr.sns_addr.x_net = ia->ia_addr.sns_addr.x_net;
335 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
337 ia->ia_flags |= IFA_ROUTE;
342 * Return address info for specified internet network.
348 struct ns_ifaddr *ia;
349 struct ns_addr *compare;
351 struct ns_ifaddr *ia_maybe = 0;
352 union ns_net net = dst->x_net;
354 for (ia = ns_ifaddr; ia; ia = ia->ia_next) {
355 if ((ifp = ia->ia_ifp) != NULL) {
356 if (ifp->if_flags & IFF_POINTOPOINT) {
357 compare = &satons_addr(ia->ia_dstaddr);
358 if (ns_hosteq(*dst, *compare))
360 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
363 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))