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. 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
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.7 2004/03/24 01:58:01 hsu Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/sockio.h>
45 #include <sys/socket.h>
48 #include <net/route.h>
54 struct ipx_ifaddr *ipx_ifaddr;
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);
61 * Generic internet control operations (ioctl's).
64 ipx_control(struct socket *so, u_long cmd, caddr_t data,
65 struct ifnet *ifp, struct thread *td)
67 struct ifreq *ifr = (struct ifreq *)data;
68 struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
69 struct ipx_ifaddr *ia;
71 struct ipx_ifaddr *oia;
72 int dstIsNew, hostIsNew;
76 * Find address for this interface, if it exists.
79 return (EADDRNOTAVAIL);
80 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
81 if (ia->ia_ifp == ifp)
88 return (EADDRNOTAVAIL);
89 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
94 return (EADDRNOTAVAIL);
95 if ((ifp->if_flags & IFF_BROADCAST) == 0)
97 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
102 return (EADDRNOTAVAIL);
103 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
105 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
109 if ((error = suser(td)) != 0)
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))
122 if (cmd == SIOCDIFADDR && ia == NULL)
123 return (EADDRNOTAVAIL);
129 oia = (struct ipx_ifaddr *)
130 malloc(sizeof(*ia), M_IFADDR,
134 if ((ia = ipx_ifaddr) != NULL) {
135 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
141 ifa = (struct ifaddr *)ia;
142 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
144 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
146 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
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;
160 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
162 if (ia->ia_flags & IFA_ROUTE) {
163 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
164 ia->ia_flags &= ~IFA_ROUTE;
167 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
169 td->td_proc->p_ucred);
173 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
177 return (ipx_ifinit(ifp, ia,
178 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
181 ipx_ifscrub(ifp, ia);
182 ifa = (struct ifaddr *)ia;
183 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
185 if (oia == (ia = ipx_ifaddr)) {
186 ipx_ifaddr = ia->ia_next;
188 while (ia->ia_next && (ia->ia_next != oia)) {
192 ia->ia_next = oia->ia_next;
194 printf("Didn't unlink ipxifadr from list\n");
196 IFAFREE((&oia->ia_ifa));
202 if (ia->ia_addr.sipx_family == AF_IPX) {
203 if (ifra->ifra_addr.sipx_len == 0) {
204 ifra->ifra_addr = ia->ia_addr;
206 } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
207 ia->ia_addr.sipx_addr))
210 if ((ifp->if_flags & IFF_POINTOPOINT) &&
211 (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
213 ipx_ifscrub(ifp, ia);
214 ia->ia_dstaddr = ifra->ifra_dstaddr;
217 if (ifra->ifra_addr.sipx_family == AF_IPX &&
218 (hostIsNew || dstIsNew))
219 error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
223 if (ifp->if_ioctl == NULL)
225 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
230 * Delete any previous route for an old address.
235 struct ipx_ifaddr *ia;
237 if (ia->ia_flags & IFA_ROUTE) {
238 if (ifp->if_flags & IFF_POINTOPOINT) {
239 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
241 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
242 ia->ia_flags &= ~IFA_ROUTE;
246 * Initialize an interface's internet address
247 * and routing table entry.
250 ipx_ifinit(ifp, ia, sipx, scrub)
252 struct ipx_ifaddr *ia;
253 struct sockaddr_ipx *sipx;
256 struct sockaddr_ipx oldaddr;
257 int s = splimp(), error;
260 * Set up new addresses.
262 oldaddr = ia->ia_addr;
266 * The convention we shall adopt for naming is that
267 * a supplied address of zero means that "we don't care".
268 * Use the MAC address of the interface. If it is an
269 * interface without a MAC address, like a serial line, the
270 * address must be supplied.
272 * Give the interface a chance to initialize
273 * if this is its first address,
274 * and to validate the address if necessary.
276 if (ifp->if_ioctl != NULL &&
277 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia,
278 (struct ucred *)NULL))) {
279 ia->ia_addr = oldaddr;
284 ia->ia_ifa.ifa_metric = ifp->if_metric;
286 * Add route for the network.
289 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
290 ipx_ifscrub(ifp, ia);
291 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
293 if (ifp->if_flags & IFF_POINTOPOINT)
294 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
296 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
297 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
299 ia->ia_flags |= IFA_ROUTE;
304 * Return address info for specified internet network.
308 struct ipx_addr *dst;
310 struct ipx_ifaddr *ia;
311 struct ipx_addr *compare;
313 struct ipx_ifaddr *ia_maybe = NULL;
314 union ipx_net net = dst->x_net;
316 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
317 if ((ifp = ia->ia_ifp) != NULL) {
318 if (ifp->if_flags & IFF_POINTOPOINT) {
319 compare = &satoipx_addr(ia->ia_dstaddr);
320 if (ipx_hosteq(*dst, *compare))
322 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
325 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
336 struct ipx_addr *addr;
339 struct ipx_addr work = *addr;
341 char *net = "", *host = "";
342 char cport[10], chost[15], cnet[15];
344 port = ntohs(work.x_port);
346 if (ipx_nullnet(work) && ipx_nullhost(work)) {
349 printf("*.%x", port);
356 if (ipx_wildnet(work))
358 else if (ipx_nullnet(work))
361 q = work.x_net.c_net;
362 snprintf(cnet, sizeof(cnet), "%x%x%x%x",
363 q[0], q[1], q[2], q[3]);
364 for (p = cnet; *p == '0' && p < cnet + 8; p++)
369 if (ipx_wildhost(work))
371 else if (ipx_nullhost(work))
374 q = work.x_host.c_host;
375 snprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
376 q[0], q[1], q[2], q[3], q[4], q[5]);
377 for (p = chost; *p == '0' && p < chost + 12; p++)
383 if (strcmp(host, "*") == 0) {
385 snprintf(cport, sizeof(cport), "%x", port);
387 snprintf(cport, sizeof(cport), ".%x", port);
391 printf("%s.%s%s", net, host, cport);