For kmalloc(), MALLOC() and contigmalloc(), use M_ZERO instead of
[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.15 2008/01/05 14:02:40 swildner 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 #include <sys/thread2.h>
48
49 #include <net/if.h>
50 #include <net/route.h>
51
52 #include "ns.h"
53 #include "ns_if.h"
54
55 #include "opt_ns.h"
56
57 #ifdef NS
58
59 struct ns_ifaddr *ns_ifaddr;
60 int ns_interfaces;
61
62 extern struct sockaddr_ns ns_netmask, ns_hostmask;
63
64 /*
65  * Generic internet control operations (ioctl's).
66  */
67 /* ARGSUSED */
68 int
69 ns_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
70         struct thread *td)
71 {
72         struct ifreq *ifr = (struct ifreq *)data;
73         struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
74         struct ns_ifaddr *ia;
75         struct ifaddr *ifa = NULL; /* XXX used ininitialized ?*/
76         struct ns_ifaddr *oia;
77         int dstIsNew, hostIsNew;
78         int error = 0; /* initalize because of scoping */
79
80         /*
81          * Find address for this interface, if it exists.
82          */
83         if (ifp == 0)
84                 return (EADDRNOTAVAIL);
85         for (ia = ns_ifaddr; ia; ia = ia->ia_next)
86                 if (ia->ia_ifp == ifp)
87                         break;
88
89         switch (cmd) {
90
91         case SIOCGIFADDR:
92                 if (ia == (struct ns_ifaddr *)0)
93                         return (EADDRNOTAVAIL);
94                 *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr;
95                 return (0);
96
97
98         case SIOCGIFBRDADDR:
99                 if (ia == (struct ns_ifaddr *)0)
100                         return (EADDRNOTAVAIL);
101                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
102                         return (EINVAL);
103                 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
104                 return (0);
105
106         case SIOCGIFDSTADDR:
107                 if (ia == (struct ns_ifaddr *)0)
108                         return (EADDRNOTAVAIL);
109                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
110                         return (EINVAL);
111                 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
112                 return (0);
113         }
114
115 #ifdef NS_PRIV_SOCKETS
116         if ((so->so_state & SS_PRIV) == 0)
117                 return (EPERM);
118 #endif
119
120         switch (cmd) {
121         case SIOCAIFADDR:
122         case SIOCDIFADDR:
123                 if (ifra->ifra_addr.sns_family == AF_NS)
124                     for (oia = ia; ia; ia = ia->ia_next) {
125                         if (ia->ia_ifp == ifp  &&
126                             ns_neteq(ia->ia_addr.sns_addr,
127                                   ifra->ifra_addr.sns_addr))
128                             break;
129                     }
130                 if (cmd == SIOCDIFADDR && ia == 0)
131                         return (EADDRNOTAVAIL);
132                 /* FALLTHROUGH */
133
134         case SIOCSIFADDR:
135         case SIOCSIFDSTADDR:
136                 if (ia == (struct ns_ifaddr *)0) {
137                         oia = (struct ns_ifaddr *)
138                                 kmalloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
139                         if (oia == (struct ns_ifaddr *)NULL)
140                                 return (ENOBUFS);
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                         lwkt_serialize_enter(ifp->if_serializer);
177                         error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, 
178                                                         (caddr_t)ia,
179                                                         (struct ucred *)NULL);
180                         lwkt_serialize_exit(ifp->if_serializer);
181                         if (error)
182                                 return (error);
183                 }
184                 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
185                 return (0);
186
187         case SIOCSIFADDR:
188                 return (ns_ifinit(ifp, (struct ns_ifaddr *)ia,
189                                 (struct sockaddr_ns *)&ifr->ifr_addr, 1));
190
191         case SIOCDIFADDR:
192                 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
193                 /* XXX not on list */
194                 oia = ia;
195                 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
196                 if (oia == (ia = ns_ifaddr)) {
197                         ns_ifaddr = ia->ia_next;
198                 } else {
199                         while (ia->ia_next && (ia->ia_next != oia)) {
200                                 ia = ia->ia_next;
201                         }
202                         if (ia->ia_next)
203                             ia->ia_next = oia->ia_next;
204                         else
205                                 kprintf("Didn't unlink nsifadr from list\n");
206                 }
207                 IFAFREE((&oia->ia_ifa));
208                 if (0 == --ns_interfaces) {
209                         /*
210                          * We reset to virginity and start all over again
211                          */
212                         ns_thishost = ns_zerohost;
213                 }
214                 return (0);
215
216         case SIOCAIFADDR:
217                 dstIsNew = 0; hostIsNew = 1;
218                 if (ia->ia_addr.sns_family == AF_NS) {
219                         if (ifra->ifra_addr.sns_len == 0) {
220                                 ifra->ifra_addr = ia->ia_addr;
221                                 hostIsNew = 0;
222                         } else if (ns_neteq(ifra->ifra_addr.sns_addr,
223                                          ia->ia_addr.sns_addr))
224                                 hostIsNew = 0;
225                 }
226                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
227                     (ifra->ifra_dstaddr.sns_family == AF_NS)) {
228                         if (hostIsNew == 0)
229                                 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
230                         ia->ia_dstaddr = ifra->ifra_dstaddr;
231                         dstIsNew  = 1;
232                 }
233                 if (ifra->ifra_addr.sns_family == AF_NS &&
234                                             (hostIsNew || dstIsNew))
235                         error = ns_ifinit(ifp, (struct ns_ifaddr *)ia,
236                                                 &ifra->ifra_addr, 0);
237                 return (error);
238
239         default:
240                 if (ifp->if_ioctl == 0)
241                         return (EOPNOTSUPP);
242                 lwkt_serialize_enter(ifp->if_serializer);
243                 error = ifp->if_ioctl(ifp, cmd, data, (struct ucred *)NULL);
244                 lwkt_serialize_exit(ifp->if_serializer);
245                 return (error);
246         }
247 }
248
249 /*
250 * Delete any previous route for an old address.
251 */
252 void
253 ns_ifscrub(struct ifnet *ifp, struct ns_ifaddr *ia)
254 {
255         if (ia->ia_flags & IFA_ROUTE) {
256                 if (ifp->if_flags & IFF_POINTOPOINT) {
257                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
258                 } else
259                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
260                 ia->ia_flags &= ~IFA_ROUTE;
261         }
262 }
263 /*
264  * Initialize an interface's internet address
265  * and routing table entry.
266  */
267 int
268 ns_ifinit(struct ifnet *ifp, struct ns_ifaddr *ia, struct sockaddr_ns *sns, int scrub)
269 {
270         struct sockaddr_ns oldaddr;
271         union ns_host *h = &ia->ia_addr.sns_addr.x_host;
272         int error;
273
274         crit_enter();
275
276         /*
277          * Set up new addresses.
278          */
279         oldaddr = ia->ia_addr;
280         ia->ia_addr = *sns;
281         /*
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
287          * used.
288          *
289          * Give the interface a chance to initialize
290          * if this is its first address,
291          * and to validate the address if necessary.
292          */
293         if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
294                 lwkt_serialize_enter(ifp->if_serializer);
295                 if (ifp->if_ioctl &&
296                      (error = ifp->if_ioctl(ifp, SIOCSIFADDR, 
297                                                 (caddr_t)ia,
298                                                 (struct ucred *)NULL))) {
299                         ia->ia_addr = oldaddr;
300                         lwkt_serialize_exit(ifp->if_serializer);
301                         crit_exit();
302                         return (error);
303                 }
304                 lwkt_serialize_exit(ifp->if_serializer);
305                 ns_thishost = *h;
306         } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
307             || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
308                 *h = ns_thishost;
309                 lwkt_serialize_enter(ifp->if_serializer);
310                 if (ifp->if_ioctl &&
311                      (error = ifp->if_ioctl(ifp, SIOCSIFADDR, 
312                                                 (caddr_t)ia,
313                                                 (struct ucred *)NULL))) {
314                         ia->ia_addr = oldaddr;
315                         lwkt_serialize_exit(ifp->if_serializer);
316                         crit_exit();
317                         return (error);
318                 }
319                 lwkt_serialize_exit(ifp->if_serializer);
320                 if (!ns_hosteqnh(ns_thishost,*h)) {
321                         ia->ia_addr = oldaddr;
322                         crit_exit();
323                         return (EINVAL);
324                 }
325         } else {
326                 ia->ia_addr = oldaddr;
327                 crit_exit();
328                 return (EINVAL);
329         }
330         ia->ia_ifa.ifa_metric = ifp->if_metric;
331         /*
332          * Add route for the network.
333          */
334         if (scrub) {
335                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
336                 ns_ifscrub(ifp, ia);
337                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
338         }
339         if (ifp->if_flags & IFF_POINTOPOINT)
340                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
341         else {
342                 ia->ia_broadaddr.sns_addr.x_net = ia->ia_addr.sns_addr.x_net;
343                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
344         }
345         ia->ia_flags |= IFA_ROUTE;
346         return (0);
347 }
348
349 /*
350  * Return address info for specified internet network.
351  */
352 struct ns_ifaddr *
353 ns_iaonnetof(struct ns_addr *dst)
354 {
355         struct ns_ifaddr *ia;
356         struct ns_addr *compare;
357         struct ifnet *ifp;
358         struct ns_ifaddr *ia_maybe = 0;
359         union ns_net net = dst->x_net;
360
361         for (ia = ns_ifaddr; ia; ia = ia->ia_next) {
362                 if ((ifp = ia->ia_ifp) != NULL) {
363                         if (ifp->if_flags & IFF_POINTOPOINT) {
364                                 compare = &satons_addr(ia->ia_dstaddr);
365                                 if (ns_hosteq(*dst, *compare))
366                                         return (ia);
367                                 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
368                                         ia_maybe = ia;
369                         } else {
370                                 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
371                                         return (ia);
372                         }
373                 }
374         }
375         return (ia_maybe);
376 }
377 #endif