Make all network interrupt service routines MPSAFE part 1/3.
[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 $
78195a76 35 * $DragonFly: src/sys/netproto/ns/ns.c,v 1.11 2005/11/28 17:13:47 dillon Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
7b51e07b 40#include <sys/malloc.h>
984263bc 41#include <sys/mbuf.h>
f7c07ac9 42#include <sys/sockio.h>
984263bc
MD
43#include <sys/protosw.h>
44#include <sys/errno.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
636ae7e8 47#include <sys/thread2.h>
984263bc
MD
48
49#include <net/if.h>
50#include <net/route.h>
51
1f2de5d4
MD
52#include "ns.h"
53#include "ns_if.h"
984263bc 54
7b51e07b
DR
55#include "opt_ns.h"
56
984263bc
MD
57#ifdef NS
58
59struct ns_ifaddr *ns_ifaddr;
60int ns_interfaces;
57106eda 61
984263bc
MD
62extern struct sockaddr_ns ns_netmask, ns_hostmask;
63
64/*
65 * Generic internet control operations (ioctl's).
66 */
67/* ARGSUSED */
7b51e07b 68int
4dea1ffd
MD
69ns_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
70 struct thread *td)
984263bc 71{
34ee9825
RG
72 struct ifreq *ifr = (struct ifreq *)data;
73 struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
74 struct ns_ifaddr *ia;
7b51e07b 75 struct ifaddr *ifa = NULL; /* XXX used ininitialized ?*/
984263bc 76 struct ns_ifaddr *oia;
7b51e07b
DR
77 int dstIsNew, hostIsNew;
78 int error = 0; /* initalize because of scoping */
984263bc
MD
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
7b51e07b 115#ifdef NS_PRIV_SOCKETS
984263bc
MD
116 if ((so->so_state & SS_PRIV) == 0)
117 return (EPERM);
7b51e07b 118#endif
984263bc
MD
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 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
139 if (oia == (struct ns_ifaddr *)NULL)
140 return (ENOBUFS);
141 bzero((caddr_t)oia, sizeof(*oia));
7b51e07b 142 if ((ia = ns_ifaddr) != NULL) {
984263bc
MD
143 for ( ; ia->ia_next; ia = ia->ia_next)
144 ;
145 ia->ia_next = oia;
146 } else
147 ns_ifaddr = oia;
148 ia = oia;
7b51e07b
DR
149
150 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
984263bc
MD
151 ia->ia_ifp = ifp;
152 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
153
154 ia->ia_ifa.ifa_netmask =
155 (struct sockaddr *)&ns_netmask;
156
157 ia->ia_ifa.ifa_dstaddr =
158 (struct sockaddr *)&ia->ia_dstaddr;
159 if (ifp->if_flags & IFF_BROADCAST) {
160 ia->ia_broadaddr.sns_family = AF_NS;
161 ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr);
162 ia->ia_broadaddr.sns_addr.x_host = ns_broadhost;
163 }
164 ns_interfaces++;
165 }
166 }
167
168 switch (cmd) {
984263bc
MD
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) {
78195a76
MD
177 lwkt_serialize_enter(ifp->if_serializer);
178 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR,
bd4539cc
JH
179 (caddr_t)ia,
180 (struct ucred *)NULL);
78195a76 181 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
182 if (error)
183 return (error);
184 }
185 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
186 return (0);
187
188 case SIOCSIFADDR:
7b51e07b 189 return (ns_ifinit(ifp, (struct ns_ifaddr *)ia,
984263bc
MD
190 (struct sockaddr_ns *)&ifr->ifr_addr, 1));
191
192 case SIOCDIFADDR:
7b51e07b
DR
193 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
194 /* XXX not on list */
984263bc 195 oia = ia;
7b51e07b
DR
196 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
197 if (oia == (ia = ns_ifaddr)) {
198 ns_ifaddr = ia->ia_next;
199 } else {
200 while (ia->ia_next && (ia->ia_next != oia)) {
201 ia = ia->ia_next;
202 }
203 if (ia->ia_next)
204 ia->ia_next = oia->ia_next;
205 else
206 printf("Didn't unlink nsifadr from list\n");
207 }
984263bc
MD
208 IFAFREE((&oia->ia_ifa));
209 if (0 == --ns_interfaces) {
210 /*
211 * We reset to virginity and start all over again
212 */
213 ns_thishost = ns_zerohost;
214 }
215 return (0);
216
217 case SIOCAIFADDR:
218 dstIsNew = 0; hostIsNew = 1;
219 if (ia->ia_addr.sns_family == AF_NS) {
220 if (ifra->ifra_addr.sns_len == 0) {
221 ifra->ifra_addr = ia->ia_addr;
222 hostIsNew = 0;
223 } else if (ns_neteq(ifra->ifra_addr.sns_addr,
224 ia->ia_addr.sns_addr))
225 hostIsNew = 0;
226 }
227 if ((ifp->if_flags & IFF_POINTOPOINT) &&
228 (ifra->ifra_dstaddr.sns_family == AF_NS)) {
229 if (hostIsNew == 0)
7b51e07b 230 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
984263bc
MD
231 ia->ia_dstaddr = ifra->ifra_dstaddr;
232 dstIsNew = 1;
233 }
234 if (ifra->ifra_addr.sns_family == AF_NS &&
235 (hostIsNew || dstIsNew))
f7c07ac9 236 error = ns_ifinit(ifp, (struct ns_ifaddr *)ia,
7b51e07b 237 &ifra->ifra_addr, 0);
984263bc
MD
238 return (error);
239
240 default:
241 if (ifp->if_ioctl == 0)
242 return (EOPNOTSUPP);
78195a76
MD
243 lwkt_serialize_enter(ifp->if_serializer);
244 error = ifp->if_ioctl(ifp, cmd, data, (struct ucred *)NULL);
245 lwkt_serialize_exit(ifp->if_serializer);
246 return (error);
984263bc
MD
247 }
248}
249
250/*
251* Delete any previous route for an old address.
252*/
7b51e07b 253void
984263bc 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 */
7b51e07b 270int
984263bc 271ns_ifinit(ifp, ia, sns, scrub)
34ee9825
RG
272 struct ifnet *ifp;
273 struct ns_ifaddr *ia;
274 struct sockaddr_ns *sns;
7b51e07b 275 int scrub;
984263bc
MD
276{
277 struct sockaddr_ns oldaddr;
34ee9825 278 union ns_host *h = &ia->ia_addr.sns_addr.x_host;
636ae7e8
MD
279 int error;
280
281 crit_enter();
984263bc
MD
282
283 /*
284 * Set up new addresses.
285 */
286 oldaddr = ia->ia_addr;
287 ia->ia_addr = *sns;
288 /*
289 * The convention we shall adopt for naming is that
290 * a supplied address of zero means that "we don't care".
291 * if there is a single interface, use the address of that
292 * interface as our 6 byte host address.
293 * if there are multiple interfaces, use any address already
294 * used.
295 *
296 * Give the interface a chance to initialize
297 * if this is its first address,
298 * and to validate the address if necessary.
299 */
300 if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
78195a76 301 lwkt_serialize_enter(ifp->if_serializer);
984263bc 302 if (ifp->if_ioctl &&
78195a76 303 (error = ifp->if_ioctl(ifp, SIOCSIFADDR,
bd4539cc
JH
304 (caddr_t)ia,
305 (struct ucred *)NULL))) {
984263bc 306 ia->ia_addr = oldaddr;
78195a76 307 lwkt_serialize_exit(ifp->if_serializer);
636ae7e8 308 crit_exit();
984263bc
MD
309 return (error);
310 }
78195a76 311 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
312 ns_thishost = *h;
313 } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
314 || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
315 *h = ns_thishost;
78195a76 316 lwkt_serialize_enter(ifp->if_serializer);
984263bc 317 if (ifp->if_ioctl &&
78195a76 318 (error = ifp->if_ioctl(ifp, SIOCSIFADDR,
bd4539cc
JH
319 (caddr_t)ia,
320 (struct ucred *)NULL))) {
984263bc 321 ia->ia_addr = oldaddr;
78195a76 322 lwkt_serialize_exit(ifp->if_serializer);
636ae7e8 323 crit_exit();
984263bc
MD
324 return (error);
325 }
78195a76 326 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
327 if (!ns_hosteqnh(ns_thishost,*h)) {
328 ia->ia_addr = oldaddr;
636ae7e8 329 crit_exit();
984263bc
MD
330 return (EINVAL);
331 }
332 } else {
333 ia->ia_addr = oldaddr;
636ae7e8 334 crit_exit();
984263bc
MD
335 return (EINVAL);
336 }
337 ia->ia_ifa.ifa_metric = ifp->if_metric;
338 /*
339 * Add route for the network.
340 */
341 if (scrub) {
342 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
343 ns_ifscrub(ifp, ia);
344 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
345 }
346 if (ifp->if_flags & IFF_POINTOPOINT)
347 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
348 else {
349 ia->ia_broadaddr.sns_addr.x_net = ia->ia_addr.sns_addr.x_net;
350 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
351 }
352 ia->ia_flags |= IFA_ROUTE;
353 return (0);
354}
355
356/*
357 * Return address info for specified internet network.
358 */
359struct ns_ifaddr *
360ns_iaonnetof(dst)
34ee9825 361 struct ns_addr *dst;
984263bc 362{
34ee9825
RG
363 struct ns_ifaddr *ia;
364 struct ns_addr *compare;
365 struct ifnet *ifp;
984263bc
MD
366 struct ns_ifaddr *ia_maybe = 0;
367 union ns_net net = dst->x_net;
368
369 for (ia = ns_ifaddr; ia; ia = ia->ia_next) {
7b51e07b 370 if ((ifp = ia->ia_ifp) != NULL) {
984263bc
MD
371 if (ifp->if_flags & IFF_POINTOPOINT) {
372 compare = &satons_addr(ia->ia_dstaddr);
373 if (ns_hosteq(*dst, *compare))
374 return (ia);
375 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
376 ia_maybe = ia;
377 } else {
378 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
379 return (ia);
380 }
381 }
382 }
383 return (ia_maybe);
384}
385#endif