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