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