Make all network interrupt service routines MPSAFE part 1/3.
[dragonfly.git] / sys / netproto / ipx / ipx.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)ipx.c
35 *
36 * $FreeBSD: src/sys/netipx/ipx.c,v 1.17.2.3 2003/04/04 09:35:43 tjr Exp $
78195a76 37 * $DragonFly: src/sys/netproto/ipx/ipx.c,v 1.9 2005/11/28 17:13:46 dillon Exp $
984263bc
MD
38 */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/sockio.h>
44#include <sys/proc.h>
45#include <sys/socket.h>
68b13d6b 46#include <sys/thread2.h>
984263bc
MD
47
48#include <net/if.h>
49#include <net/route.h>
50
1f2de5d4
MD
51#include "ipx.h"
52#include "ipx_if.h"
53#include "ipx_var.h"
984263bc
MD
54
55struct ipx_ifaddr *ipx_ifaddr;
56
57static void ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia);
58static int ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
59 struct sockaddr_ipx *sipx, int scrub);
60
61/*
62 * Generic internet control operations (ioctl's).
63 */
64int
dadab5e9
MD
65ipx_control(struct socket *so, u_long cmd, caddr_t data,
66 struct ifnet *ifp, struct thread *td)
984263bc 67{
34ee9825
RG
68 struct ifreq *ifr = (struct ifreq *)data;
69 struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
70 struct ipx_ifaddr *ia;
984263bc
MD
71 struct ifaddr *ifa;
72 struct ipx_ifaddr *oia;
73 int dstIsNew, hostIsNew;
74 int error = 0;
75
76 /*
77 * Find address for this interface, if it exists.
78 */
79 if (ifp == NULL)
80 return (EADDRNOTAVAIL);
81 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
82 if (ia->ia_ifp == ifp)
83 break;
84
85 switch (cmd) {
86
87 case SIOCGIFADDR:
88 if (ia == NULL)
89 return (EADDRNOTAVAIL);
90 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
91 return (0);
92
93 case SIOCGIFBRDADDR:
94 if (ia == NULL)
95 return (EADDRNOTAVAIL);
96 if ((ifp->if_flags & IFF_BROADCAST) == 0)
97 return (EINVAL);
98 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
99 return (0);
100
101 case SIOCGIFDSTADDR:
102 if (ia == NULL)
103 return (EADDRNOTAVAIL);
104 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
105 return (EINVAL);
106 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
107 return (0);
108 }
109
dadab5e9 110 if ((error = suser(td)) != 0)
984263bc
MD
111 return (error);
112
113 switch (cmd) {
114 case SIOCAIFADDR:
115 case SIOCDIFADDR:
116 if (ifra->ifra_addr.sipx_family == AF_IPX)
117 for (oia = ia; ia != NULL; ia = ia->ia_next) {
118 if (ia->ia_ifp == ifp &&
119 ipx_neteq(ia->ia_addr.sipx_addr,
120 ifra->ifra_addr.sipx_addr))
121 break;
122 }
123 if (cmd == SIOCDIFADDR && ia == NULL)
124 return (EADDRNOTAVAIL);
125 /* FALLTHROUGH */
126
127 case SIOCSIFADDR:
128 case SIOCSIFDSTADDR:
129 if (ia == NULL) {
130 oia = (struct ipx_ifaddr *)
131 malloc(sizeof(*ia), M_IFADDR,
132 M_WAITOK | M_ZERO);
133 if (oia == NULL)
134 return (ENOBUFS);
135 if ((ia = ipx_ifaddr) != NULL) {
136 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
137 ;
138 ia->ia_next = oia;
139 } else
140 ipx_ifaddr = oia;
141 ia = oia;
142 ifa = (struct ifaddr *)ia;
143 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
144 ia->ia_ifp = ifp;
145 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
146
147 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
148
149 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
150 if (ifp->if_flags & IFF_BROADCAST) {
151 ia->ia_broadaddr.sipx_family = AF_IPX;
152 ia->ia_broadaddr.sipx_len = sizeof(ia->ia_addr);
153 ia->ia_broadaddr.sipx_addr.x_host = ipx_broadhost;
154 }
155 }
156 }
157
158 switch (cmd) {
159
160 case SIOCSIFDSTADDR:
161 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
162 return (EINVAL);
163 if (ia->ia_flags & IFA_ROUTE) {
164 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
165 ia->ia_flags &= ~IFA_ROUTE;
166 }
167 if (ifp->if_ioctl) {
78195a76
MD
168 lwkt_serialize_enter(ifp->if_serializer);
169 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR,
170 (void *)ia, td->td_proc->p_ucred);
171 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
172 if (error)
173 return (error);
174 }
175 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
176 return (0);
177
178 case SIOCSIFADDR:
179 return (ipx_ifinit(ifp, ia,
180 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
181
182 case SIOCDIFADDR:
183 ipx_ifscrub(ifp, ia);
184 ifa = (struct ifaddr *)ia;
185 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
186 oia = ia;
187 if (oia == (ia = ipx_ifaddr)) {
188 ipx_ifaddr = ia->ia_next;
189 } else {
190 while (ia->ia_next && (ia->ia_next != oia)) {
191 ia = ia->ia_next;
192 }
193 if (ia->ia_next)
194 ia->ia_next = oia->ia_next;
195 else
196 printf("Didn't unlink ipxifadr from list\n");
197 }
198 IFAFREE((&oia->ia_ifa));
199 return (0);
200
201 case SIOCAIFADDR:
202 dstIsNew = 0;
203 hostIsNew = 1;
204 if (ia->ia_addr.sipx_family == AF_IPX) {
205 if (ifra->ifra_addr.sipx_len == 0) {
206 ifra->ifra_addr = ia->ia_addr;
207 hostIsNew = 0;
208 } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
209 ia->ia_addr.sipx_addr))
210 hostIsNew = 0;
211 }
212 if ((ifp->if_flags & IFF_POINTOPOINT) &&
213 (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
214 if (hostIsNew == 0)
215 ipx_ifscrub(ifp, ia);
216 ia->ia_dstaddr = ifra->ifra_dstaddr;
217 dstIsNew = 1;
218 }
219 if (ifra->ifra_addr.sipx_family == AF_IPX &&
220 (hostIsNew || dstIsNew))
221 error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
222 return (error);
223
224 default:
225 if (ifp->if_ioctl == NULL)
226 return (EOPNOTSUPP);
78195a76
MD
227 lwkt_serialize_enter(ifp->if_serializer);
228 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
229 lwkt_serialize_exit(ifp->if_serializer);
230 return (error);
984263bc
MD
231 }
232}
233
234/*
235* Delete any previous route for an old address.
236*/
237static void
238ipx_ifscrub(ifp, ia)
34ee9825
RG
239 struct ifnet *ifp;
240 struct ipx_ifaddr *ia;
984263bc
MD
241{
242 if (ia->ia_flags & IFA_ROUTE) {
243 if (ifp->if_flags & IFF_POINTOPOINT) {
244 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
245 } else
246 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
247 ia->ia_flags &= ~IFA_ROUTE;
248 }
249}
250/*
251 * Initialize an interface's internet address
252 * and routing table entry.
253 */
254static int
255ipx_ifinit(ifp, ia, sipx, scrub)
34ee9825
RG
256 struct ifnet *ifp;
257 struct ipx_ifaddr *ia;
258 struct sockaddr_ipx *sipx;
984263bc
MD
259 int scrub;
260{
261 struct sockaddr_ipx oldaddr;
68b13d6b
MD
262 int error;
263
984263bc
MD
264 /*
265 * Set up new addresses.
266 */
267 oldaddr = ia->ia_addr;
268 ia->ia_addr = *sipx;
269
270 /*
271 * The convention we shall adopt for naming is that
272 * a supplied address of zero means that "we don't care".
273 * Use the MAC address of the interface. If it is an
274 * interface without a MAC address, like a serial line, the
275 * address must be supplied.
276 *
277 * Give the interface a chance to initialize
278 * if this is its first address,
279 * and to validate the address if necessary.
280 */
78195a76 281 lwkt_serialize_enter(ifp->if_serializer);
984263bc 282 if (ifp->if_ioctl != NULL &&
78195a76 283 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (void *)ia,
bd4539cc 284 (struct ucred *)NULL))) {
984263bc 285 ia->ia_addr = oldaddr;
78195a76 286 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
287 return (error);
288 }
78195a76 289 lwkt_serialize_exit(ifp->if_serializer);
984263bc
MD
290 ia->ia_ifa.ifa_metric = ifp->if_metric;
291 /*
292 * Add route for the network.
293 */
294 if (scrub) {
295 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
296 ipx_ifscrub(ifp, ia);
297 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
298 }
299 if (ifp->if_flags & IFF_POINTOPOINT)
300 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
301 else {
302 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
303 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
304 }
305 ia->ia_flags |= IFA_ROUTE;
306 return (0);
307}
308
309/*
310 * Return address info for specified internet network.
311 */
312struct ipx_ifaddr *
313ipx_iaonnetof(dst)
34ee9825 314 struct ipx_addr *dst;
984263bc 315{
34ee9825
RG
316 struct ipx_ifaddr *ia;
317 struct ipx_addr *compare;
318 struct ifnet *ifp;
984263bc
MD
319 struct ipx_ifaddr *ia_maybe = NULL;
320 union ipx_net net = dst->x_net;
321
322 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
323 if ((ifp = ia->ia_ifp) != NULL) {
324 if (ifp->if_flags & IFF_POINTOPOINT) {
325 compare = &satoipx_addr(ia->ia_dstaddr);
326 if (ipx_hosteq(*dst, *compare))
327 return (ia);
328 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
329 ia_maybe = ia;
330 } else {
331 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
332 return (ia);
333 }
334 }
335 }
336 return (ia_maybe);
337}
338
339
340void
341ipx_printhost(addr)
34ee9825 342struct ipx_addr *addr;
984263bc
MD
343{
344 u_short port;
345 struct ipx_addr work = *addr;
34ee9825
RG
346 char *p; u_char *q;
347 char *net = "", *host = "";
984263bc
MD
348 char cport[10], chost[15], cnet[15];
349
350 port = ntohs(work.x_port);
351
352 if (ipx_nullnet(work) && ipx_nullhost(work)) {
353
354 if (port)
355 printf("*.%x", port);
356 else
357 printf("*.*");
358
359 return;
360 }
361
362 if (ipx_wildnet(work))
363 net = "any";
364 else if (ipx_nullnet(work))
365 net = "*";
366 else {
367 q = work.x_net.c_net;
368 snprintf(cnet, sizeof(cnet), "%x%x%x%x",
369 q[0], q[1], q[2], q[3]);
370 for (p = cnet; *p == '0' && p < cnet + 8; p++)
371 continue;
372 net = p;
373 }
374
375 if (ipx_wildhost(work))
376 host = "any";
377 else if (ipx_nullhost(work))
378 host = "*";
379 else {
380 q = work.x_host.c_host;
381 snprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
382 q[0], q[1], q[2], q[3], q[4], q[5]);
383 for (p = chost; *p == '0' && p < chost + 12; p++)
384 continue;
385 host = p;
386 }
387
388 if (port) {
389 if (strcmp(host, "*") == 0) {
390 host = "";
391 snprintf(cport, sizeof(cport), "%x", port);
392 } else
393 snprintf(cport, sizeof(cport), ".%x", port);
394 } else
395 *cport = 0;
396
397 printf("%s.%s%s", net, host, cport);
398}