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
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.
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
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 $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/sockio.h>
42 #include <sys/socket.h>
43 #include <sys/thread2.h>
46 #include <net/route.h>
52 struct ipx_ifaddr *ipx_ifaddr;
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);
59 * Generic internet control operations (ioctl's).
62 ipx_control_oncpu(struct socket *so, u_long cmd, caddr_t data,
63 struct ifnet *ifp, struct thread *td)
65 struct ifreq *ifr = (struct ifreq *)data;
66 struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
67 struct ipx_ifaddr *ia;
69 struct ipx_ifaddr *oia;
70 int dstIsNew, hostIsNew;
74 * Find address for this interface, if it exists.
77 return (EADDRNOTAVAIL);
78 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
79 if (ia->ia_ifp == ifp)
86 return (EADDRNOTAVAIL);
87 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
92 return (EADDRNOTAVAIL);
93 if ((ifp->if_flags & IFF_BROADCAST) == 0)
95 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
100 return (EADDRNOTAVAIL);
101 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
103 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
107 if ((error = priv_check(td, PRIV_ROOT)) != 0)
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))
120 if (cmd == SIOCDIFADDR && ia == NULL)
121 return (EADDRNOTAVAIL);
127 oia = ifa_create(sizeof(*ia), M_WAITOK);
128 if ((ia = ipx_ifaddr) != NULL) {
129 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
135 ifa = (struct ifaddr *)ia;
136 ifa_iflink(ifa, ifp, 1);
138 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
140 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
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;
154 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
156 if (ia->ia_flags & IFA_ROUTE) {
157 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
158 ia->ia_flags &= ~IFA_ROUTE;
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);
168 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
172 return (ipx_ifinit(ifp, ia,
173 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
176 ipx_ifscrub(ifp, ia);
177 ifa = (struct ifaddr *)ia;
178 ifa_ifunlink(ifa, ifp);
180 if (oia == (ia = ipx_ifaddr)) {
181 ipx_ifaddr = ia->ia_next;
183 while (ia->ia_next && (ia->ia_next != oia)) {
187 ia->ia_next = oia->ia_next;
189 kprintf("Didn't unlink ipxifadr from list\n");
191 ifa_destroy(&oia->ia_ifa);
197 if (ia->ia_addr.sipx_family == AF_IPX) {
198 if (ifra->ifra_addr.sipx_len == 0) {
199 ifra->ifra_addr = ia->ia_addr;
201 } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
202 ia->ia_addr.sipx_addr))
205 if ((ifp->if_flags & IFF_POINTOPOINT) &&
206 (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
208 ipx_ifscrub(ifp, ia);
209 ia->ia_dstaddr = ifra->ifra_dstaddr;
212 if (ifra->ifra_addr.sipx_family == AF_IPX &&
213 (hostIsNew || dstIsNew))
214 error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
218 if (ifp->if_ioctl == NULL)
220 ifnet_serialize_all(ifp);
221 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
222 ifnet_deserialize_all(ifp);
228 * Delete any previous route for an old address.
231 ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia)
233 if (ia->ia_flags & IFA_ROUTE) {
234 if (ifp->if_flags & IFF_POINTOPOINT) {
235 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
237 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
238 ia->ia_flags &= ~IFA_ROUTE;
242 * Initialize an interface's internet address
243 * and routing table entry.
246 ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
247 struct sockaddr_ipx *sipx, int scrub)
249 struct sockaddr_ipx oldaddr;
253 * Set up new addresses.
255 oldaddr = ia->ia_addr;
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.
265 * Give the interface a chance to initialize
266 * if this is its first address,
267 * and to validate the address if necessary.
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);
276 ifnet_deserialize_all(ifp);
277 ia->ia_ifa.ifa_metric = ifp->if_metric;
279 * Add route for the network.
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;
286 if (ifp->if_flags & IFF_POINTOPOINT)
287 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
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);
292 ia->ia_flags |= IFA_ROUTE;
297 * Return address info for specified internet network.
300 ipx_iaonnetof(struct ipx_addr *dst)
302 struct ipx_ifaddr *ia;
303 struct ipx_addr *compare;
305 struct ipx_ifaddr *ia_maybe = NULL;
306 union ipx_net net = dst->x_net;
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))
314 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
317 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
327 ipx_printhost(struct ipx_addr *addr)
330 struct ipx_addr work = *addr;
332 char *net = "", *host = "";
333 char cport[10], chost[15], cnet[15];
335 port = ntohs(work.x_port);
337 if (ipx_nullnet(work) && ipx_nullhost(work)) {
340 kprintf("*.%x", port);
347 if (ipx_wildnet(work))
349 else if (ipx_nullnet(work))
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++)
360 if (ipx_wildhost(work))
362 else if (ipx_nullhost(work))
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++)
374 if (strcmp(host, "*") == 0) {
376 ksnprintf(cport, sizeof(cport), "%x", port);
378 ksnprintf(cport, sizeof(cport), ".%x", port);
382 kprintf("%s.%s%s", net, host, cport);