Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / sys / netproto / ns / ns.c
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 $
35  * $DragonFly: src/sys/netproto/ns/ns.c,v 1.9 2004/06/07 07:04:33 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/sockio.h>
43 #include <sys/protosw.h>
44 #include <sys/errno.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47
48 #include <net/if.h>
49 #include <net/route.h>
50
51 #include "ns.h"
52 #include "ns_if.h"
53
54 #include "opt_ns.h"
55
56 #ifdef NS
57
58 struct ns_ifaddr *ns_ifaddr;
59 int ns_interfaces;
60
61 extern struct sockaddr_ns ns_netmask, ns_hostmask;
62
63 /*
64  * Generic internet control operations (ioctl's).
65  */
66 /* ARGSUSED */
67 int
68 ns_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
69         struct thread *td)
70 {
71         struct ifreq *ifr = (struct ifreq *)data;
72         struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
73         struct ns_ifaddr *ia;
74         struct ifaddr *ifa = NULL; /* XXX used ininitialized ?*/
75         struct ns_ifaddr *oia;
76         int dstIsNew, hostIsNew;
77         int error = 0; /* initalize because of scoping */
78
79         /*
80          * Find address for this interface, if it exists.
81          */
82         if (ifp == 0)
83                 return (EADDRNOTAVAIL);
84         for (ia = ns_ifaddr; ia; ia = ia->ia_next)
85                 if (ia->ia_ifp == ifp)
86                         break;
87
88         switch (cmd) {
89
90         case SIOCGIFADDR:
91                 if (ia == (struct ns_ifaddr *)0)
92                         return (EADDRNOTAVAIL);
93                 *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr;
94                 return (0);
95
96
97         case SIOCGIFBRDADDR:
98                 if (ia == (struct ns_ifaddr *)0)
99                         return (EADDRNOTAVAIL);
100                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
101                         return (EINVAL);
102                 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
103                 return (0);
104
105         case SIOCGIFDSTADDR:
106                 if (ia == (struct ns_ifaddr *)0)
107                         return (EADDRNOTAVAIL);
108                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
109                         return (EINVAL);
110                 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
111                 return (0);
112         }
113
114 #ifdef NS_PRIV_SOCKETS
115         if ((so->so_state & SS_PRIV) == 0)
116                 return (EPERM);
117 #endif
118
119         switch (cmd) {
120         case SIOCAIFADDR:
121         case SIOCDIFADDR:
122                 if (ifra->ifra_addr.sns_family == AF_NS)
123                     for (oia = ia; ia; ia = ia->ia_next) {
124                         if (ia->ia_ifp == ifp  &&
125                             ns_neteq(ia->ia_addr.sns_addr,
126                                   ifra->ifra_addr.sns_addr))
127                             break;
128                     }
129                 if (cmd == SIOCDIFADDR && ia == 0)
130                         return (EADDRNOTAVAIL);
131                 /* FALLTHROUGH */
132
133         case SIOCSIFADDR:
134         case SIOCSIFDSTADDR:
135                 if (ia == (struct ns_ifaddr *)0) {
136                         oia = (struct ns_ifaddr *)
137                                 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
138                         if (oia == (struct ns_ifaddr *)NULL)
139                                 return (ENOBUFS);
140                         bzero((caddr_t)oia, sizeof(*oia));
141                         if ((ia = ns_ifaddr) != NULL) {
142                                 for ( ; ia->ia_next; ia = ia->ia_next)
143                                         ;
144                                 ia->ia_next = oia;
145                         } else
146                                 ns_ifaddr = oia;
147                         ia = oia;
148
149                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
150                         ia->ia_ifp = ifp;
151                         ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
152
153                         ia->ia_ifa.ifa_netmask =
154                                 (struct sockaddr *)&ns_netmask;
155
156                         ia->ia_ifa.ifa_dstaddr =
157                                 (struct sockaddr *)&ia->ia_dstaddr;
158                         if (ifp->if_flags & IFF_BROADCAST) {
159                                 ia->ia_broadaddr.sns_family = AF_NS;
160                                 ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr);
161                                 ia->ia_broadaddr.sns_addr.x_host = ns_broadhost;
162                         }
163                         ns_interfaces++;
164                 }
165         }
166
167         switch (cmd) {
168         case SIOCSIFDSTADDR:
169                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
170                         return (EINVAL);
171                 if (ia->ia_flags & IFA_ROUTE) {
172                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
173                         ia->ia_flags &= ~IFA_ROUTE;
174                 }
175                 if (ifp->if_ioctl) {
176                         error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 
177                                                         (caddr_t)ia,
178                                                         (struct ucred *)NULL);
179                         if (error)
180                                 return (error);
181                 }
182                 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
183                 return (0);
184
185         case SIOCSIFADDR:
186                 return (ns_ifinit(ifp, (struct ns_ifaddr *)ia,
187                                 (struct sockaddr_ns *)&ifr->ifr_addr, 1));
188
189         case SIOCDIFADDR:
190                 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
191                 /* XXX not on list */
192                 oia = ia;
193                 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
194                 if (oia == (ia = ns_ifaddr)) {
195                         ns_ifaddr = ia->ia_next;
196                 } else {
197                         while (ia->ia_next && (ia->ia_next != oia)) {
198                                 ia = ia->ia_next;
199                         }
200                         if (ia->ia_next)
201                             ia->ia_next = oia->ia_next;
202                         else
203                                 printf("Didn't unlink nsifadr from list\n");
204                 }
205                 IFAFREE((&oia->ia_ifa));
206                 if (0 == --ns_interfaces) {
207                         /*
208                          * We reset to virginity and start all over again
209                          */
210                         ns_thishost = ns_zerohost;
211                 }
212                 return (0);
213
214         case SIOCAIFADDR:
215                 dstIsNew = 0; hostIsNew = 1;
216                 if (ia->ia_addr.sns_family == AF_NS) {
217                         if (ifra->ifra_addr.sns_len == 0) {
218                                 ifra->ifra_addr = ia->ia_addr;
219                                 hostIsNew = 0;
220                         } else if (ns_neteq(ifra->ifra_addr.sns_addr,
221                                          ia->ia_addr.sns_addr))
222                                 hostIsNew = 0;
223                 }
224                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
225                     (ifra->ifra_dstaddr.sns_family == AF_NS)) {
226                         if (hostIsNew == 0)
227                                 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
228                         ia->ia_dstaddr = ifra->ifra_dstaddr;
229                         dstIsNew  = 1;
230                 }
231                 if (ifra->ifra_addr.sns_family == AF_NS &&
232                                             (hostIsNew || dstIsNew))
233                         error = ns_ifinit(ifp, (struct ns_ifaddr *)ia,
234                                                 &ifra->ifra_addr, 0);
235                 return (error);
236
237         default:
238                 if (ifp->if_ioctl == 0)
239                         return (EOPNOTSUPP);
240                 return ((*ifp->if_ioctl)(ifp, cmd, data, (struct ucred *)NULL));
241         }
242 }
243
244 /*
245 * Delete any previous route for an old address.
246 */
247 void
248 ns_ifscrub(ifp, ia)
249         struct ifnet *ifp;
250         struct ns_ifaddr *ia;
251 {
252         if (ia->ia_flags & IFA_ROUTE) {
253                 if (ifp->if_flags & IFF_POINTOPOINT) {
254                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
255                 } else
256                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
257                 ia->ia_flags &= ~IFA_ROUTE;
258         }
259 }
260 /*
261  * Initialize an interface's internet address
262  * and routing table entry.
263  */
264 int
265 ns_ifinit(ifp, ia, sns, scrub)
266         struct ifnet *ifp;
267         struct ns_ifaddr *ia;
268         struct sockaddr_ns *sns;
269         int scrub;
270 {
271         struct sockaddr_ns oldaddr;
272         union ns_host *h = &ia->ia_addr.sns_addr.x_host;
273         int s = splimp(), error;
274
275         /*
276          * Set up new addresses.
277          */
278         oldaddr = ia->ia_addr;
279         ia->ia_addr = *sns;
280         /*
281          * The convention we shall adopt for naming is that
282          * a supplied address of zero means that "we don't care".
283          * if there is a single interface, use the address of that
284          * interface as our 6 byte host address.
285          * if there are multiple interfaces, use any address already
286          * used.
287          *
288          * Give the interface a chance to initialize
289          * if this is its first address,
290          * and to validate the address if necessary.
291          */
292         if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
293                 if (ifp->if_ioctl &&
294                      (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, 
295                                                 (caddr_t)ia,
296                                                 (struct ucred *)NULL))) {
297                         ia->ia_addr = oldaddr;
298                         splx(s);
299                         return (error);
300                 }
301                 ns_thishost = *h;
302         } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
303             || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
304                 *h = ns_thishost;
305                 if (ifp->if_ioctl &&
306                      (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, 
307                                                 (caddr_t)ia,
308                                                 (struct ucred *)NULL))) {
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  */
345 struct ns_ifaddr *
346 ns_iaonnetof(dst)
347         struct ns_addr *dst;
348 {
349         struct ns_ifaddr *ia;
350         struct ns_addr *compare;
351         struct ifnet *ifp;
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) != NULL) {
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