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.4 2003/08/07 21:17:38 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.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>
48 #include <net/route.h>
55 struct ns_ifaddr *ns_ifaddr;
57 extern struct sockaddr_ns ns_netmask, ns_hostmask;
60 * Generic internet control operations (ioctl's).
63 ns_control(so, cmd, data, ifp)
69 struct ifreq *ifr = (struct ifreq *)data;
70 struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
73 struct ns_ifaddr *oia;
74 int error, dstIsNew, hostIsNew;
77 * Find address for this interface, if it exists.
80 return (EADDRNOTAVAIL);
81 for (ia = ns_ifaddr; ia; ia = ia->ia_next)
82 if (ia->ia_ifp == ifp)
88 if (ia == (struct ns_ifaddr *)0)
89 return (EADDRNOTAVAIL);
90 *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr;
95 if (ia == (struct ns_ifaddr *)0)
96 return (EADDRNOTAVAIL);
97 if ((ifp->if_flags & IFF_BROADCAST) == 0)
99 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
103 if (ia == (struct ns_ifaddr *)0)
104 return (EADDRNOTAVAIL);
105 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
107 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
111 if ((so->so_state & SS_PRIV) == 0)
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))
124 if (cmd == SIOCDIFADDR && ia == 0)
125 return (EADDRNOTAVAIL);
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)
135 bzero((caddr_t)oia, sizeof(*oia));
136 if (ia = ns_ifaddr) {
137 for ( ; ia->ia_next; ia = ia->ia_next)
143 if (ifa = ifp->if_addrlist) {
144 for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
146 ifa->ifa_next = (struct ifaddr *) ia;
148 ifp->if_addrlist = (struct ifaddr *) ia;
150 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
152 ia->ia_ifa.ifa_netmask =
153 (struct sockaddr *)&ns_netmask;
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;
170 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
172 if (ia->ia_flags & IFA_ROUTE) {
173 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
174 ia->ia_flags &= ~IFA_ROUTE;
177 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ia);
181 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
185 return (ns_ifinit(ifp, ia,
186 (struct sockaddr_ns *)&ifr->ifr_addr, 1));
190 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
191 ifp->if_addrlist = ifa->ifa_next;
193 while (ifa->ifa_next &&
194 (ifa->ifa_next != (struct ifaddr *)ia))
197 ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
199 printf("Couldn't unlink nsifaddr from ifp\n");
202 if (oia == (ia = ns_ifaddr)) {
203 ns_ifaddr = ia->ia_next;
205 while (ia->ia_next && (ia->ia_next != oia)) {
209 ia->ia_next = oia->ia_next;
211 printf("Didn't unlink nsifadr from list\n");
213 IFAFREE((&oia->ia_ifa));
214 if (0 == --ns_interfaces) {
216 * We reset to virginity and start all over again
218 ns_thishost = ns_zerohost;
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;
228 } else if (ns_neteq(ifra->ifra_addr.sns_addr,
229 ia->ia_addr.sns_addr))
232 if ((ifp->if_flags & IFF_POINTOPOINT) &&
233 (ifra->ifra_dstaddr.sns_family == AF_NS)) {
236 ia->ia_dstaddr = ifra->ifra_dstaddr;
239 if (ifra->ifra_addr.sns_family == AF_NS &&
240 (hostIsNew || dstIsNew))
241 error = ns_ifinit(ifp, ia, &ifra->ifra_addr, 0);
245 if (ifp->if_ioctl == 0)
247 return ((*ifp->if_ioctl)(ifp, cmd, data));
252 * Delete any previous route for an old address.
256 struct ns_ifaddr *ia;
258 if (ia->ia_flags & IFA_ROUTE) {
259 if (ifp->if_flags & IFF_POINTOPOINT) {
260 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
262 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
263 ia->ia_flags &= ~IFA_ROUTE;
267 * Initialize an interface's internet address
268 * and routing table entry.
270 ns_ifinit(ifp, ia, sns, scrub)
272 struct ns_ifaddr *ia;
273 struct sockaddr_ns *sns;
275 struct sockaddr_ns oldaddr;
276 union ns_host *h = &ia->ia_addr.sns_addr.x_host;
277 int s = splimp(), error;
280 * Set up new addresses.
282 oldaddr = ia->ia_addr;
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
292 * Give the interface a chance to initialize
293 * if this is its first address,
294 * and to validate the address if necessary.
296 if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
298 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, ia))) {
299 ia->ia_addr = oldaddr;
304 } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
305 || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
308 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, ia))) {
309 ia->ia_addr = oldaddr;
313 if (!ns_hosteqnh(ns_thishost,*h)) {
314 ia->ia_addr = oldaddr;
319 ia->ia_addr = oldaddr;
323 ia->ia_ifa.ifa_metric = ifp->if_metric;
325 * Add route for the network.
328 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
330 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
332 if (ifp->if_flags & IFF_POINTOPOINT)
333 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
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);
338 ia->ia_flags |= IFA_ROUTE;
343 * Return address info for specified internet network.
349 struct ns_ifaddr *ia;
350 struct ns_addr *compare;
352 struct ns_ifaddr *ia_maybe = 0;
353 union ns_net net = dst->x_net;
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))
361 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
364 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))