e7c03b8fceea71afe8deab0bc3e7a8cebd5b2828
[dragonfly.git] / sys / netproto / ipx / ipx.c
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 $
37  * $DragonFly: src/sys/netproto/ipx/ipx.c,v 1.15 2008/03/07 11:34:21 sephe Exp $
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/priv.h>
46 #include <sys/socket.h>
47 #include <sys/thread2.h>
48
49 #include <net/if.h>
50 #include <net/route.h>
51
52 #include "ipx.h"
53 #include "ipx_if.h"
54 #include "ipx_var.h"
55
56 struct ipx_ifaddr *ipx_ifaddr;
57
58 static  void ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia);
59 static  int ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
60                        struct sockaddr_ipx *sipx, int scrub);
61
62 /*
63  * Generic internet control operations (ioctl's).
64  */
65 int
66 ipx_control(struct socket *so, u_long cmd, caddr_t data,
67         struct ifnet *ifp, struct thread *td)
68 {
69         struct ifreq *ifr = (struct ifreq *)data;
70         struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
71         struct ipx_ifaddr *ia;
72         struct ifaddr *ifa;
73         struct ipx_ifaddr *oia;
74         int dstIsNew, hostIsNew;
75         int error = 0;
76
77         /*
78          * Find address for this interface, if it exists.
79          */
80         if (ifp == NULL)
81                 return (EADDRNOTAVAIL);
82         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
83                 if (ia->ia_ifp == ifp)
84                         break;
85
86         switch (cmd) {
87
88         case SIOCGIFADDR:
89                 if (ia == NULL)
90                         return (EADDRNOTAVAIL);
91                 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
92                 return (0);
93
94         case SIOCGIFBRDADDR:
95                 if (ia == NULL)
96                         return (EADDRNOTAVAIL);
97                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
98                         return (EINVAL);
99                 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
100                 return (0);
101
102         case SIOCGIFDSTADDR:
103                 if (ia == NULL)
104                         return (EADDRNOTAVAIL);
105                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
106                         return (EINVAL);
107                 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
108                 return (0);
109         }
110
111         if ((error = priv_check(td, PRIV_ROOT)) != 0)
112                 return (error);
113
114         switch (cmd) {
115         case SIOCAIFADDR:
116         case SIOCDIFADDR:
117                 if (ifra->ifra_addr.sipx_family == AF_IPX)
118                     for (oia = ia; ia != NULL; ia = ia->ia_next) {
119                         if (ia->ia_ifp == ifp  &&
120                             ipx_neteq(ia->ia_addr.sipx_addr,
121                                   ifra->ifra_addr.sipx_addr))
122                             break;
123                     }
124                 if (cmd == SIOCDIFADDR && ia == NULL)
125                         return (EADDRNOTAVAIL);
126                 /* FALLTHROUGH */
127
128         case SIOCSIFADDR:
129         case SIOCSIFDSTADDR:
130                 if (ia == NULL) {
131                         oia = ifa_create(sizeof(*ia), M_WAITOK);
132                         if ((ia = ipx_ifaddr) != NULL) {
133                                 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
134                                         ;
135                                 ia->ia_next = oia;
136                         } else
137                                 ipx_ifaddr = oia;
138                         ia = oia;
139                         ifa = (struct ifaddr *)ia;
140                         ifa_iflink(ifa, ifp, 1);
141                         ia->ia_ifp = ifp;
142                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
143
144                         ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
145
146                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
147                         if (ifp->if_flags & IFF_BROADCAST) {
148                                 ia->ia_broadaddr.sipx_family = AF_IPX;
149                                 ia->ia_broadaddr.sipx_len = sizeof(ia->ia_addr);
150                                 ia->ia_broadaddr.sipx_addr.x_host = ipx_broadhost;
151                         }
152                 }
153         }
154
155         switch (cmd) {
156
157         case SIOCSIFDSTADDR:
158                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
159                         return (EINVAL);
160                 if (ia->ia_flags & IFA_ROUTE) {
161                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
162                         ia->ia_flags &= ~IFA_ROUTE;
163                 }
164                 if (ifp->if_ioctl) {
165                         lwkt_serialize_enter(ifp->if_serializer);
166                         error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR,
167                                               (void *)ia, td->td_proc->p_ucred);
168                         lwkt_serialize_exit(ifp->if_serializer);
169                         if (error)
170                                 return (error);
171                 }
172                 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
173                 return (0);
174
175         case SIOCSIFADDR:
176                 return (ipx_ifinit(ifp, ia,
177                                 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
178
179         case SIOCDIFADDR:
180                 ipx_ifscrub(ifp, ia);
181                 ifa = (struct ifaddr *)ia;
182                 ifa_ifunlink(ifa, ifp);
183                 oia = ia;
184                 if (oia == (ia = ipx_ifaddr)) {
185                         ipx_ifaddr = ia->ia_next;
186                 } else {
187                         while (ia->ia_next && (ia->ia_next != oia)) {
188                                 ia = ia->ia_next;
189                         }
190                         if (ia->ia_next)
191                             ia->ia_next = oia->ia_next;
192                         else
193                                 kprintf("Didn't unlink ipxifadr from list\n");
194                 }
195                 ifa_destroy(&oia->ia_ifa);
196                 return (0);
197         
198         case SIOCAIFADDR:
199                 dstIsNew = 0;
200                 hostIsNew = 1;
201                 if (ia->ia_addr.sipx_family == AF_IPX) {
202                         if (ifra->ifra_addr.sipx_len == 0) {
203                                 ifra->ifra_addr = ia->ia_addr;
204                                 hostIsNew = 0;
205                         } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
206                                          ia->ia_addr.sipx_addr))
207                                 hostIsNew = 0;
208                 }
209                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
210                     (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
211                         if (hostIsNew == 0)
212                                 ipx_ifscrub(ifp, ia);
213                         ia->ia_dstaddr = ifra->ifra_dstaddr;
214                         dstIsNew  = 1;
215                 }
216                 if (ifra->ifra_addr.sipx_family == AF_IPX &&
217                                             (hostIsNew || dstIsNew))
218                         error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
219                 return (error);
220
221         default:
222                 if (ifp->if_ioctl == NULL)
223                         return (EOPNOTSUPP);
224                 lwkt_serialize_enter(ifp->if_serializer);
225                 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
226                 lwkt_serialize_exit(ifp->if_serializer);
227                 return (error);
228         }
229 }
230
231 /*
232 * Delete any previous route for an old address.
233 */
234 static void
235 ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia)
236 {
237         if (ia->ia_flags & IFA_ROUTE) {
238                 if (ifp->if_flags & IFF_POINTOPOINT) {
239                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
240                 } else
241                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
242                 ia->ia_flags &= ~IFA_ROUTE;
243         }
244 }
245 /*
246  * Initialize an interface's internet address
247  * and routing table entry.
248  */
249 static int
250 ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
251            struct sockaddr_ipx *sipx, int scrub)
252 {
253         struct sockaddr_ipx oldaddr;
254         int error;
255
256         /*
257          * Set up new addresses.
258          */
259         oldaddr = ia->ia_addr;
260         ia->ia_addr = *sipx;
261
262         /*
263          * The convention we shall adopt for naming is that
264          * a supplied address of zero means that "we don't care".
265          * Use the MAC address of the interface. If it is an
266          * interface without a MAC address, like a serial line, the
267          * address must be supplied.
268          *
269          * Give the interface a chance to initialize
270          * if this is its first address,
271          * and to validate the address if necessary.
272          */
273         lwkt_serialize_enter(ifp->if_serializer);
274         if (ifp->if_ioctl != NULL &&
275             (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (void *)ia,
276                                       (struct ucred *)NULL))) {
277                 ia->ia_addr = oldaddr;
278                 lwkt_serialize_exit(ifp->if_serializer);
279                 return (error);
280         }
281         lwkt_serialize_exit(ifp->if_serializer);
282         ia->ia_ifa.ifa_metric = ifp->if_metric;
283         /*
284          * Add route for the network.
285          */
286         if (scrub) {
287                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
288                 ipx_ifscrub(ifp, ia);
289                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
290         }
291         if (ifp->if_flags & IFF_POINTOPOINT)
292                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
293         else {
294                 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
295                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
296         }
297         ia->ia_flags |= IFA_ROUTE;
298         return (0);
299 }
300
301 /*
302  * Return address info for specified internet network.
303  */
304 struct ipx_ifaddr *
305 ipx_iaonnetof(struct ipx_addr *dst)
306 {
307         struct ipx_ifaddr *ia;
308         struct ipx_addr *compare;
309         struct ifnet *ifp;
310         struct ipx_ifaddr *ia_maybe = NULL;
311         union ipx_net net = dst->x_net;
312
313         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
314                 if ((ifp = ia->ia_ifp) != NULL) {
315                         if (ifp->if_flags & IFF_POINTOPOINT) {
316                                 compare = &satoipx_addr(ia->ia_dstaddr);
317                                 if (ipx_hosteq(*dst, *compare))
318                                         return (ia);
319                                 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
320                                         ia_maybe = ia;
321                         } else {
322                                 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
323                                         return (ia);
324                         }
325                 }
326         }
327         return (ia_maybe);
328 }
329
330
331 void
332 ipx_printhost(struct ipx_addr *addr)
333 {
334         u_short port;
335         struct ipx_addr work = *addr;
336         char *p; u_char *q;
337         char *net = "", *host = "";
338         char cport[10], chost[15], cnet[15];
339
340         port = ntohs(work.x_port);
341
342         if (ipx_nullnet(work) && ipx_nullhost(work)) {
343
344                 if (port)
345                         kprintf("*.%x", port);
346                 else
347                         kprintf("*.*");
348
349                 return;
350         }
351
352         if (ipx_wildnet(work))
353                 net = "any";
354         else if (ipx_nullnet(work))
355                 net = "*";
356         else {
357                 q = work.x_net.c_net;
358                 ksnprintf(cnet, sizeof(cnet), "%x%x%x%x",
359                         q[0], q[1], q[2], q[3]);
360                 for (p = cnet; *p == '0' && p < cnet + 8; p++)
361                         continue;
362                 net = p;
363         }
364
365         if (ipx_wildhost(work))
366                 host = "any";
367         else if (ipx_nullhost(work))
368                 host = "*";
369         else {
370                 q = work.x_host.c_host;
371                 ksnprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
372                         q[0], q[1], q[2], q[3], q[4], q[5]);
373                 for (p = chost; *p == '0' && p < chost + 12; p++)
374                         continue;
375                 host = p;
376         }
377
378         if (port) {
379                 if (strcmp(host, "*") == 0) {
380                         host = "";
381                         ksnprintf(cport, sizeof(cport), "%x", port);
382                 } else
383                         ksnprintf(cport, sizeof(cport), ".%x", port);
384         } else
385                 *cport = 0;
386
387         kprintf("%s.%s%s", net, host, cport);
388 }