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