__FreeBSD__ -> __DragonFly__
[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.5 2003/08/07 21:17:37 dillon 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, (void *)ia);
168                         if (error)
169                                 return (error);
170                 }
171                 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
172                 return (0);
173
174         case SIOCSIFADDR:
175                 return (ipx_ifinit(ifp, ia,
176                                 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
177
178         case SIOCDIFADDR:
179                 ipx_ifscrub(ifp, ia);
180                 ifa = (struct ifaddr *)ia;
181                 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
182                 oia = ia;
183                 if (oia == (ia = ipx_ifaddr)) {
184                         ipx_ifaddr = ia->ia_next;
185                 } else {
186                         while (ia->ia_next && (ia->ia_next != oia)) {
187                                 ia = ia->ia_next;
188                         }
189                         if (ia->ia_next)
190                             ia->ia_next = oia->ia_next;
191                         else
192                                 printf("Didn't unlink ipxifadr from list\n");
193                 }
194                 IFAFREE((&oia->ia_ifa));
195                 return (0);
196         
197         case SIOCAIFADDR:
198                 dstIsNew = 0;
199                 hostIsNew = 1;
200                 if (ia->ia_addr.sipx_family == AF_IPX) {
201                         if (ifra->ifra_addr.sipx_len == 0) {
202                                 ifra->ifra_addr = ia->ia_addr;
203                                 hostIsNew = 0;
204                         } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
205                                          ia->ia_addr.sipx_addr))
206                                 hostIsNew = 0;
207                 }
208                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
209                     (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
210                         if (hostIsNew == 0)
211                                 ipx_ifscrub(ifp, ia);
212                         ia->ia_dstaddr = ifra->ifra_dstaddr;
213                         dstIsNew  = 1;
214                 }
215                 if (ifra->ifra_addr.sipx_family == AF_IPX &&
216                                             (hostIsNew || dstIsNew))
217                         error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
218                 return (error);
219
220         default:
221                 if (ifp->if_ioctl == NULL)
222                         return (EOPNOTSUPP);
223                 return ((*ifp->if_ioctl)(ifp, cmd, data));
224         }
225 }
226
227 /*
228 * Delete any previous route for an old address.
229 */
230 static void
231 ipx_ifscrub(ifp, ia)
232         struct ifnet *ifp;
233         struct ipx_ifaddr *ia; 
234 {
235         if (ia->ia_flags & IFA_ROUTE) {
236                 if (ifp->if_flags & IFF_POINTOPOINT) {
237                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
238                 } else
239                         rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
240                 ia->ia_flags &= ~IFA_ROUTE;
241         }
242 }
243 /*
244  * Initialize an interface's internet address
245  * and routing table entry.
246  */
247 static int
248 ipx_ifinit(ifp, ia, sipx, scrub)
249         struct ifnet *ifp;
250         struct ipx_ifaddr *ia;
251         struct sockaddr_ipx *sipx;
252         int scrub;
253 {
254         struct sockaddr_ipx oldaddr;
255         int s = splimp(), 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         if (ifp->if_ioctl != NULL &&
275             (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia))) {
276                 ia->ia_addr = oldaddr;
277                 splx(s);
278                 return (error);
279         }
280         splx(s);
281         ia->ia_ifa.ifa_metric = ifp->if_metric;
282         /*
283          * Add route for the network.
284          */
285         if (scrub) {
286                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
287                 ipx_ifscrub(ifp, ia);
288                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
289         }
290         if (ifp->if_flags & IFF_POINTOPOINT)
291                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
292         else {
293                 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
294                 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
295         }
296         ia->ia_flags |= IFA_ROUTE;
297         return (0);
298 }
299
300 /*
301  * Return address info for specified internet network.
302  */
303 struct ipx_ifaddr *
304 ipx_iaonnetof(dst)
305         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(addr)
333 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                         printf("*.%x", port);
347                 else
348                         printf("*.*");
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                 snprintf(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                 snprintf(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                         snprintf(cport, sizeof(cport), "%x", port);
383                 } else
384                         snprintf(cport, sizeof(cport), ".%x", port);
385         } else
386                 *cport = 0;
387
388         printf("%s.%s%s", net, host, cport);
389 }