2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2009 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Copyright (c) 1995, Mike Mitchell
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
67 #include <sys/param.h>
68 #include <sys/kernel.h>
69 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/rwlock.h>
74 #include <sys/sockio.h>
75 #include <sys/socket.h>
78 #include <net/if_var.h>
79 #include <net/route.h>
81 #include <netipx/ipx.h>
82 #include <netipx/ipx_if.h>
83 #include <netipx/ipx_var.h>
86 * The IPX-layer address list is protected by ipx_ifaddr_rw.
88 struct rwlock ipx_ifaddr_rw;
89 struct ipx_ifaddrhead ipx_ifaddrhead;
91 static void ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia);
92 static int ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
93 struct sockaddr_ipx *sipx, int scrub);
96 * Generic internet control operations (ioctl's).
99 ipx_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
102 struct ifreq *ifr = (struct ifreq *)data;
103 struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
104 struct ipx_ifaddr *ia;
106 int dstIsNew, hostIsNew;
110 * Find address for this interface, if it exists.
113 return (EADDRNOTAVAIL);
116 TAILQ_FOREACH(ia, &ipx_ifaddrhead, ia_link) {
117 if (ia->ia_ifp == ifp)
121 ifa_ref(&ia->ia_ifa);
122 IPX_IFADDR_RUNLOCK();
128 error = EADDRNOTAVAIL;
131 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
136 error = EADDRNOTAVAIL;
139 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
143 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
148 error = EADDRNOTAVAIL;
151 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
155 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
162 priv = (cmd == SIOCAIFADDR) ? PRIV_NET_ADDIFADDR :
164 if (td && (error = priv_check(td, priv)) != 0)
168 if (ifra->ifra_addr.sipx_family == AF_IPX) {
169 struct ipx_ifaddr *oia;
171 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
172 if (ia->ia_ifp == ifp &&
173 ipx_neteq(ia->ia_addr.sipx_addr,
174 ifra->ifra_addr.sipx_addr))
177 if (oia != NULL && oia != ia)
178 ifa_free(&oia->ia_ifa);
179 if (ia != NULL && oia != ia)
180 ifa_ref(&ia->ia_ifa);
182 IPX_IFADDR_RUNLOCK();
183 if (cmd == SIOCDIFADDR && ia == NULL) {
184 error = EADDRNOTAVAIL;
191 if (td && (error = priv_check(td, PRIV_NET_SETLLADDR)) != 0)
194 ifa = ifa_alloc(sizeof(struct ipx_ifaddr), M_WAITOK);
195 ia = (struct ipx_ifaddr *)ifa;
197 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
198 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
199 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
200 if (ifp->if_flags & IFF_BROADCAST) {
201 ia->ia_broadaddr.sipx_family = AF_IPX;
202 ia->ia_broadaddr.sipx_len =
204 ia->ia_broadaddr.sipx_addr.x_host =
207 ifa_ref(&ia->ia_ifa); /* ipx_ifaddrhead */
209 TAILQ_INSERT_TAIL(&ipx_ifaddrhead, ia, ia_link);
210 IPX_IFADDR_WUNLOCK();
212 ifa_ref(&ia->ia_ifa); /* if_addrhead */
214 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
215 IF_ADDR_WUNLOCK(ifp);
220 if (td && (error = priv_check(td, PRIV_NET_HWIOCTL)) != 0)
226 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
230 if (ia->ia_flags & IFA_ROUTE) {
231 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
232 ia->ia_flags &= ~IFA_ROUTE;
235 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
240 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
244 error = ipx_ifinit(ifp, ia,
245 (struct sockaddr_ipx *)&ifr->ifr_addr, 1);
249 ipx_ifscrub(ifp, ia);
250 ifa = (struct ifaddr *)ia;
253 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
254 IF_ADDR_WUNLOCK(ifp);
255 ifa_free(ifa); /* if_addrhead */
258 TAILQ_REMOVE(&ipx_ifaddrhead, ia, ia_link);
259 IPX_IFADDR_WUNLOCK();
260 ifa_free(&ia->ia_ifa); /* ipx_ifaddrhead */
266 if (ia->ia_addr.sipx_family == AF_IPX) {
267 if (ifra->ifra_addr.sipx_len == 0) {
268 ifra->ifra_addr = ia->ia_addr;
270 } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
271 ia->ia_addr.sipx_addr))
274 if ((ifp->if_flags & IFF_POINTOPOINT) &&
275 (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
277 ipx_ifscrub(ifp, ia);
278 ia->ia_dstaddr = ifra->ifra_dstaddr;
281 if (ifra->ifra_addr.sipx_family == AF_IPX &&
282 (hostIsNew || dstIsNew))
283 error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
287 if (ifp->if_ioctl == NULL) {
291 error = ((*ifp->if_ioctl)(ifp, cmd, data));
296 ifa_free(&ia->ia_ifa);
301 * Delete any previous route for an old address.
304 ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia)
307 if (ia->ia_flags & IFA_ROUTE) {
308 if (ifp->if_flags & IFF_POINTOPOINT) {
309 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
311 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
312 ia->ia_flags &= ~IFA_ROUTE;
317 * Initialize an interface's internet address and routing table entry.
320 ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
321 struct sockaddr_ipx *sipx, int scrub)
323 struct sockaddr_ipx oldaddr;
324 int s = splimp(), error;
327 * Set up new addresses.
329 oldaddr = ia->ia_addr;
333 * The convention we shall adopt for naming is that a supplied
334 * address of zero means that "we don't care". Use the MAC address
335 * of the interface. If it is an interface without a MAC address,
336 * like a serial line, the address must be supplied.
338 * Give the interface a chance to initialize if this is its first
339 * address, and to validate the address if necessary.
341 if (ifp->if_ioctl != NULL &&
342 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia))) {
343 ia->ia_addr = oldaddr;
348 ia->ia_ifa.ifa_metric = ifp->if_metric;
351 * Add route for the network.
354 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
355 ipx_ifscrub(ifp, ia);
356 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
358 if (ifp->if_flags & IFF_POINTOPOINT)
359 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
361 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
362 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
364 ia->ia_flags |= IFA_ROUTE;
369 * Return address info for specified internet network.
372 ipx_iaonnetof(struct ipx_addr *dst)
374 struct ipx_ifaddr *ia;
375 struct ipx_addr *compare;
377 struct ipx_ifaddr *ia_maybe = NULL;
378 union ipx_net net = dst->x_net;
380 IPX_IFADDR_LOCK_ASSERT();
382 TAILQ_FOREACH(ia, &ipx_ifaddrhead, ia_link) {
383 if ((ifp = ia->ia_ifp) != NULL) {
384 if (ifp->if_flags & IFF_POINTOPOINT) {
385 compare = &satoipx_addr(ia->ia_dstaddr);
386 if (ipx_hosteq(*dst, *compare))
389 ia->ia_addr.sipx_addr.x_net))
393 ia->ia_addr.sipx_addr.x_net))
402 ipx_printhost(struct ipx_addr *addr)
405 struct ipx_addr work = *addr;
407 char *net = "", *host = "";
408 char cport[10], chost[15], cnet[15];
410 port = ntohs(work.x_port);
412 if (ipx_nullnet(work) && ipx_nullhost(work)) {
414 printf("*.%x", port);
421 if (ipx_wildnet(work))
423 else if (ipx_nullnet(work))
426 q = work.x_net.c_net;
427 snprintf(cnet, sizeof(cnet), "%x%x%x%x",
428 q[0], q[1], q[2], q[3]);
429 for (p = cnet; *p == '0' && p < cnet + 8; p++)
434 if (ipx_wildhost(work))
436 else if (ipx_nullhost(work))
439 q = work.x_host.c_host;
440 snprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
441 q[0], q[1], q[2], q[3], q[4], q[5]);
442 for (p = chost; *p == '0' && p < chost + 12; p++)
448 if (strcmp(host, "*") == 0) {
450 snprintf(cport, sizeof(cport), "%x", port);
452 snprintf(cport, sizeof(cport), ".%x", port);
456 printf("%s.%s%s", net, host, cport);