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