Move around some #ifdefs to silence warnings.
[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.7 2004/03/24 01:58:01 hsu 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
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include "ipx.h"
51 #include "ipx_if.h"
52 #include "ipx_var.h"
53
54 struct ipx_ifaddr *ipx_ifaddr;
55
56 static  void ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia);
57 static  int ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
58                        struct sockaddr_ipx *sipx, int scrub);
59
60 /*
61  * Generic internet control operations (ioctl's).
62  */
63 int
64 ipx_control(struct socket *so, u_long cmd, caddr_t data,
65         struct ifnet *ifp, struct thread *td)
66 {
67         struct ifreq *ifr = (struct ifreq *)data;
68         struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
69         struct ipx_ifaddr *ia;
70         struct ifaddr *ifa;
71         struct ipx_ifaddr *oia;
72         int dstIsNew, hostIsNew;
73         int error = 0;
74
75         /*
76          * Find address for this interface, if it exists.
77          */
78         if (ifp == NULL)
79                 return (EADDRNOTAVAIL);
80         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
81                 if (ia->ia_ifp == ifp)
82                         break;
83
84         switch (cmd) {
85
86         case SIOCGIFADDR:
87                 if (ia == NULL)
88                         return (EADDRNOTAVAIL);
89                 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
90                 return (0);
91
92         case SIOCGIFBRDADDR:
93                 if (ia == NULL)
94                         return (EADDRNOTAVAIL);
95                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
96                         return (EINVAL);
97                 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
98                 return (0);
99
100         case SIOCGIFDSTADDR:
101                 if (ia == NULL)
102                         return (EADDRNOTAVAIL);
103                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
104                         return (EINVAL);
105                 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
106                 return (0);
107         }
108
109         if ((error = suser(td)) != 0)
110                 return (error);
111
112         switch (cmd) {
113         case SIOCAIFADDR:
114         case SIOCDIFADDR:
115                 if (ifra->ifra_addr.sipx_family == AF_IPX)
116                     for (oia = ia; ia != NULL; ia = ia->ia_next) {
117                         if (ia->ia_ifp == ifp  &&
118                             ipx_neteq(ia->ia_addr.sipx_addr,
119                                   ifra->ifra_addr.sipx_addr))
120                             break;
121                     }
122                 if (cmd == SIOCDIFADDR && ia == NULL)
123                         return (EADDRNOTAVAIL);
124                 /* FALLTHROUGH */
125
126         case SIOCSIFADDR:
127         case SIOCSIFDSTADDR:
128                 if (ia == NULL) {
129                         oia = (struct ipx_ifaddr *)
130                                 malloc(sizeof(*ia), M_IFADDR,
131                                 M_WAITOK | M_ZERO);
132                         if (oia == NULL)
133                                 return (ENOBUFS);
134                         if ((ia = ipx_ifaddr) != NULL) {
135                                 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
136                                         ;
137                                 ia->ia_next = oia;
138                         } else
139                                 ipx_ifaddr = oia;
140                         ia = oia;
141                         ifa = (struct ifaddr *)ia;
142                         TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
143                         ia->ia_ifp = ifp;
144                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
145
146                         ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
147
148                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
149                         if (ifp->if_flags & IFF_BROADCAST) {
150                                 ia->ia_broadaddr.sipx_family = AF_IPX;
151                                 ia->ia_broadaddr.sipx_len = sizeof(ia->ia_addr);
152                                 ia->ia_broadaddr.sipx_addr.x_host = ipx_broadhost;
153                         }
154                 }
155         }
156
157         switch (cmd) {
158
159         case SIOCSIFDSTADDR:
160                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
161                         return (EINVAL);
162                 if (ia->ia_flags & IFA_ROUTE) {
163                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
164                         ia->ia_flags &= ~IFA_ROUTE;
165                 }
166                 if (ifp->if_ioctl) {
167                         error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
168                                                  (void *)ia,
169                                                  td->td_proc->p_ucred);
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                                 printf("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                 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
226         }
227 }
228
229 /*
230 * Delete any previous route for an old address.
231 */
232 static void
233 ipx_ifscrub(ifp, ia)
234         struct ifnet *ifp;
235         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(ifp, ia, sipx, scrub)
251         struct ifnet *ifp;
252         struct ipx_ifaddr *ia;
253         struct sockaddr_ipx *sipx;
254         int scrub;
255 {
256         struct sockaddr_ipx oldaddr;
257         int s = splimp(), error;
258
259         /*
260          * Set up new addresses.
261          */
262         oldaddr = ia->ia_addr;
263         ia->ia_addr = *sipx;
264
265         /*
266          * The convention we shall adopt for naming is that
267          * a supplied address of zero means that "we don't care".
268          * Use the MAC address of the interface. If it is an
269          * interface without a MAC address, like a serial line, the
270          * address must be supplied.
271          *
272          * Give the interface a chance to initialize
273          * if this is its first address,
274          * and to validate the address if necessary.
275          */
276         if (ifp->if_ioctl != NULL &&
277             (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia,
278                                       (struct ucred *)NULL))) {
279                 ia->ia_addr = oldaddr;
280                 splx(s);
281                 return (error);
282         }
283         splx(s);
284         ia->ia_ifa.ifa_metric = ifp->if_metric;
285         /*
286          * Add route for the network.
287          */
288         if (scrub) {
289                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
290                 ipx_ifscrub(ifp, ia);
291                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
292         }
293         if (ifp->if_flags & IFF_POINTOPOINT)
294                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
295         else {
296                 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
297                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
298         }
299         ia->ia_flags |= IFA_ROUTE;
300         return (0);
301 }
302
303 /*
304  * Return address info for specified internet network.
305  */
306 struct ipx_ifaddr *
307 ipx_iaonnetof(dst)
308         struct ipx_addr *dst;
309 {
310         struct ipx_ifaddr *ia;
311         struct ipx_addr *compare;
312         struct ifnet *ifp;
313         struct ipx_ifaddr *ia_maybe = NULL;
314         union ipx_net net = dst->x_net;
315
316         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
317                 if ((ifp = ia->ia_ifp) != NULL) {
318                         if (ifp->if_flags & IFF_POINTOPOINT) {
319                                 compare = &satoipx_addr(ia->ia_dstaddr);
320                                 if (ipx_hosteq(*dst, *compare))
321                                         return (ia);
322                                 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
323                                         ia_maybe = ia;
324                         } else {
325                                 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
326                                         return (ia);
327                         }
328                 }
329         }
330         return (ia_maybe);
331 }
332
333
334 void
335 ipx_printhost(addr)
336 struct ipx_addr *addr;
337 {
338         u_short port;
339         struct ipx_addr work = *addr;
340         char *p; u_char *q;
341         char *net = "", *host = "";
342         char cport[10], chost[15], cnet[15];
343
344         port = ntohs(work.x_port);
345
346         if (ipx_nullnet(work) && ipx_nullhost(work)) {
347
348                 if (port)
349                         printf("*.%x", port);
350                 else
351                         printf("*.*");
352
353                 return;
354         }
355
356         if (ipx_wildnet(work))
357                 net = "any";
358         else if (ipx_nullnet(work))
359                 net = "*";
360         else {
361                 q = work.x_net.c_net;
362                 snprintf(cnet, sizeof(cnet), "%x%x%x%x",
363                         q[0], q[1], q[2], q[3]);
364                 for (p = cnet; *p == '0' && p < cnet + 8; p++)
365                         continue;
366                 net = p;
367         }
368
369         if (ipx_wildhost(work))
370                 host = "any";
371         else if (ipx_nullhost(work))
372                 host = "*";
373         else {
374                 q = work.x_host.c_host;
375                 snprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
376                         q[0], q[1], q[2], q[3], q[4], q[5]);
377                 for (p = chost; *p == '0' && p < chost + 12; p++)
378                         continue;
379                 host = p;
380         }
381
382         if (port) {
383                 if (strcmp(host, "*") == 0) {
384                         host = "";
385                         snprintf(cport, sizeof(cport), "%x", port);
386                 } else
387                         snprintf(cport, sizeof(cport), ".%x", port);
388         } else
389                 *cport = 0;
390
391         printf("%s.%s%s", net, host, cport);
392 }