MFC:
[dragonfly.git] / sys / netproto / ipx / ipx.c
blob708d2a72cae6e84c6cc4afa832e2d1274b6e16d8
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.
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.
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.
34 * @(#)ipx.c
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.15 2008/03/07 11:34:21 sephe Exp $
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>
48 #include <net/if.h>
49 #include <net/route.h>
51 #include "ipx.h"
52 #include "ipx_if.h"
53 #include "ipx_var.h"
55 struct ipx_ifaddr *ipx_ifaddr;
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);
62 * Generic internet control operations (ioctl's).
64 int
65 ipx_control(struct socket *so, u_long cmd, caddr_t data,
66 struct ifnet *ifp, struct thread *td)
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;
77 * Find address for this interface, if it exists.
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;
85 switch (cmd) {
87 case SIOCGIFADDR:
88 if (ia == NULL)
89 return (EADDRNOTAVAIL);
90 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
91 return (0);
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);
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);
110 if ((error = suser(td)) != 0)
111 return (error);
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;
123 if (cmd == SIOCDIFADDR && ia == NULL)
124 return (EADDRNOTAVAIL);
125 /* FALLTHROUGH */
127 case SIOCSIFADDR:
128 case SIOCSIFDSTADDR:
129 if (ia == NULL) {
130 oia = ifa_create(sizeof(*ia), M_WAITOK);
131 if ((ia = ipx_ifaddr) != NULL) {
132 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
134 ia->ia_next = oia;
135 } else
136 ipx_ifaddr = oia;
137 ia = oia;
138 ifa = (struct ifaddr *)ia;
139 ifa_iflink(ifa, ifp, 1);
140 ia->ia_ifp = ifp;
141 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
143 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
145 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
146 if (ifp->if_flags & IFF_BROADCAST) {
147 ia->ia_broadaddr.sipx_family = AF_IPX;
148 ia->ia_broadaddr.sipx_len = sizeof(ia->ia_addr);
149 ia->ia_broadaddr.sipx_addr.x_host = ipx_broadhost;
154 switch (cmd) {
156 case SIOCSIFDSTADDR:
157 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
158 return (EINVAL);
159 if (ia->ia_flags & IFA_ROUTE) {
160 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
161 ia->ia_flags &= ~IFA_ROUTE;
163 if (ifp->if_ioctl) {
164 lwkt_serialize_enter(ifp->if_serializer);
165 error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR,
166 (void *)ia, td->td_proc->p_ucred);
167 lwkt_serialize_exit(ifp->if_serializer);
168 if (error)
169 return (error);
171 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
172 return (0);
174 case SIOCSIFADDR:
175 return (ipx_ifinit(ifp, ia,
176 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
178 case SIOCDIFADDR:
179 ipx_ifscrub(ifp, ia);
180 ifa = (struct ifaddr *)ia;
181 ifa_ifunlink(ifa, ifp);
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;
189 if (ia->ia_next)
190 ia->ia_next = oia->ia_next;
191 else
192 kprintf("Didn't unlink ipxifadr from list\n");
194 ifa_destroy(&oia->ia_ifa);
195 return (0);
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;
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;
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);
220 default:
221 if (ifp->if_ioctl == NULL)
222 return (EOPNOTSUPP);
223 lwkt_serialize_enter(ifp->if_serializer);
224 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
225 lwkt_serialize_exit(ifp->if_serializer);
226 return (error);
231 * Delete any previous route for an old address.
233 static void
234 ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia)
236 if (ia->ia_flags & IFA_ROUTE) {
237 if (ifp->if_flags & IFF_POINTOPOINT) {
238 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
239 } else
240 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
241 ia->ia_flags &= ~IFA_ROUTE;
245 * Initialize an interface's internet address
246 * and routing table entry.
248 static int
249 ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
250 struct sockaddr_ipx *sipx, int scrub)
252 struct sockaddr_ipx oldaddr;
253 int error;
256 * Set up new addresses.
258 oldaddr = ia->ia_addr;
259 ia->ia_addr = *sipx;
262 * The convention we shall adopt for naming is that
263 * a supplied address of zero means that "we don't care".
264 * Use the MAC address of the interface. If it is an
265 * interface without a MAC address, like a serial line, the
266 * address must be supplied.
268 * Give the interface a chance to initialize
269 * if this is its first address,
270 * and to validate the address if necessary.
272 lwkt_serialize_enter(ifp->if_serializer);
273 if (ifp->if_ioctl != NULL &&
274 (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (void *)ia,
275 (struct ucred *)NULL))) {
276 ia->ia_addr = oldaddr;
277 lwkt_serialize_exit(ifp->if_serializer);
278 return (error);
280 lwkt_serialize_exit(ifp->if_serializer);
281 ia->ia_ifa.ifa_metric = ifp->if_metric;
283 * Add route for the network.
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;
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);
296 ia->ia_flags |= IFA_ROUTE;
297 return (0);
301 * Return address info for specified internet network.
303 struct ipx_ifaddr *
304 ipx_iaonnetof(struct ipx_addr *dst)
306 struct ipx_ifaddr *ia;
307 struct ipx_addr *compare;
308 struct ifnet *ifp;
309 struct ipx_ifaddr *ia_maybe = NULL;
310 union ipx_net net = dst->x_net;
312 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
313 if ((ifp = ia->ia_ifp) != NULL) {
314 if (ifp->if_flags & IFF_POINTOPOINT) {
315 compare = &satoipx_addr(ia->ia_dstaddr);
316 if (ipx_hosteq(*dst, *compare))
317 return (ia);
318 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
319 ia_maybe = ia;
320 } else {
321 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
322 return (ia);
326 return (ia_maybe);
330 void
331 ipx_printhost(struct ipx_addr *addr)
333 u_short port;
334 struct ipx_addr work = *addr;
335 char *p; u_char *q;
336 char *net = "", *host = "";
337 char cport[10], chost[15], cnet[15];
339 port = ntohs(work.x_port);
341 if (ipx_nullnet(work) && ipx_nullhost(work)) {
343 if (port)
344 kprintf("*.%x", port);
345 else
346 kprintf("*.*");
348 return;
351 if (ipx_wildnet(work))
352 net = "any";
353 else if (ipx_nullnet(work))
354 net = "*";
355 else {
356 q = work.x_net.c_net;
357 ksnprintf(cnet, sizeof(cnet), "%x%x%x%x",
358 q[0], q[1], q[2], q[3]);
359 for (p = cnet; *p == '0' && p < cnet + 8; p++)
360 continue;
361 net = p;
364 if (ipx_wildhost(work))
365 host = "any";
366 else if (ipx_nullhost(work))
367 host = "*";
368 else {
369 q = work.x_host.c_host;
370 ksnprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
371 q[0], q[1], q[2], q[3], q[4], q[5]);
372 for (p = chost; *p == '0' && p < chost + 12; p++)
373 continue;
374 host = p;
377 if (port) {
378 if (strcmp(host, "*") == 0) {
379 host = "";
380 ksnprintf(cport, sizeof(cport), "%x", port);
381 } else
382 ksnprintf(cport, sizeof(cport), ".%x", port);
383 } else
384 *cport = 0;
386 kprintf("%s.%s%s", net, host, cport);