FreeBSD/Linux Kernel Cross Reference
sys/netipx/ipx.c
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.
5 *
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.
21 *
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.
33 *
34 * @(#)ipx.c
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/5.2/sys/netipx/ipx.c 116189 2003-06-11 05:37:42Z obrien $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/sockio.h>
44 #include <sys/socket.h>
45
46 #include <net/if.h>
47 #include <net/route.h>
48
49 #include <netipx/ipx.h>
50 #include <netipx/ipx_if.h>
51 #include <netipx/ipx_var.h>
52
53 struct ipx_ifaddr *ipx_ifaddr;
54
55 static void ipx_ifscrub(struct ifnet *ifp, struct ipx_ifaddr *ia);
56 static int ipx_ifinit(struct ifnet *ifp, struct ipx_ifaddr *ia,
57 struct sockaddr_ipx *sipx, int scrub);
58
59 /*
60 * Generic internet control operations (ioctl's).
61 */
62 int
63 ipx_control(so, cmd, data, ifp, td)
64 struct socket *so;
65 u_long cmd;
66 caddr_t data;
67 register struct ifnet *ifp;
68 struct thread *td;
69 {
70 register struct ifreq *ifr = (struct ifreq *)data;
71 register struct ipx_aliasreq *ifra = (struct ipx_aliasreq *)data;
72 register struct ipx_ifaddr *ia;
73 struct ifaddr *ifa;
74 struct ipx_ifaddr *oia;
75 int dstIsNew, hostIsNew;
76 int error = 0;
77
78 /*
79 * Find address for this interface, if it exists.
80 */
81 if (ifp == NULL)
82 return (EADDRNOTAVAIL);
83 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
84 if (ia->ia_ifp == ifp)
85 break;
86
87 switch (cmd) {
88
89 case SIOCGIFADDR:
90 if (ia == NULL)
91 return (EADDRNOTAVAIL);
92 *(struct sockaddr_ipx *)&ifr->ifr_addr = ia->ia_addr;
93 return (0);
94
95 case SIOCGIFBRDADDR:
96 if (ia == NULL)
97 return (EADDRNOTAVAIL);
98 if ((ifp->if_flags & IFF_BROADCAST) == 0)
99 return (EINVAL);
100 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
101 return (0);
102
103 case SIOCGIFDSTADDR:
104 if (ia == NULL)
105 return (EADDRNOTAVAIL);
106 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
107 return (EINVAL);
108 *(struct sockaddr_ipx *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
109 return (0);
110 }
111
112 if (td && (error = suser(td)) != 0)
113 return (error);
114
115 switch (cmd) {
116 case SIOCAIFADDR:
117 case SIOCDIFADDR:
118 if (ifra->ifra_addr.sipx_family == AF_IPX)
119 for (oia = ia; ia != NULL; ia = ia->ia_next) {
120 if (ia->ia_ifp == ifp &&
121 ipx_neteq(ia->ia_addr.sipx_addr,
122 ifra->ifra_addr.sipx_addr))
123 break;
124 }
125 if (cmd == SIOCDIFADDR && ia == NULL)
126 return (EADDRNOTAVAIL);
127 /* FALLTHROUGH */
128
129 case SIOCSIFADDR:
130 case SIOCSIFDSTADDR:
131 if (ia == NULL) {
132 oia = (struct ipx_ifaddr *)
133 malloc(sizeof(*ia), M_IFADDR,
134 M_WAITOK | M_ZERO);
135 if (oia == NULL)
136 return (ENOBUFS);
137 if ((ia = ipx_ifaddr) != NULL) {
138 for ( ; ia->ia_next != NULL; ia = ia->ia_next)
139 ;
140 ia->ia_next = oia;
141 } else
142 ipx_ifaddr = oia;
143 ia = oia;
144 ifa = (struct ifaddr *)ia;
145 IFA_LOCK_INIT(ifa);
146 ifa->ifa_refcnt = 1;
147 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
148 ia->ia_ifp = ifp;
149 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
150
151 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
152
153 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
154 if (ifp->if_flags & IFF_BROADCAST) {
155 ia->ia_broadaddr.sipx_family = AF_IPX;
156 ia->ia_broadaddr.sipx_len = sizeof(ia->ia_addr);
157 ia->ia_broadaddr.sipx_addr.x_host = ipx_broadhost;
158 }
159 }
160 }
161
162 switch (cmd) {
163
164 case SIOCSIFDSTADDR:
165 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
166 return (EINVAL);
167 if (ia->ia_flags & IFA_ROUTE) {
168 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
169 ia->ia_flags &= ~IFA_ROUTE;
170 }
171 if (ifp->if_ioctl) {
172 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (void *)ia);
173 if (error)
174 return (error);
175 }
176 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
177 return (0);
178
179 case SIOCSIFADDR:
180 return (ipx_ifinit(ifp, ia,
181 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
182
183 case SIOCDIFADDR:
184 ipx_ifscrub(ifp, ia);
185 ifa = (struct ifaddr *)ia;
186 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
187 oia = ia;
188 if (oia == (ia = ipx_ifaddr)) {
189 ipx_ifaddr = ia->ia_next;
190 } else {
191 while (ia->ia_next && (ia->ia_next != oia)) {
192 ia = ia->ia_next;
193 }
194 if (ia->ia_next)
195 ia->ia_next = oia->ia_next;
196 else
197 printf("Didn't unlink ipxifadr from list\n");
198 }
199 IFAFREE((&oia->ia_ifa));
200 return (0);
201
202 case SIOCAIFADDR:
203 dstIsNew = 0;
204 hostIsNew = 1;
205 if (ia->ia_addr.sipx_family == AF_IPX) {
206 if (ifra->ifra_addr.sipx_len == 0) {
207 ifra->ifra_addr = ia->ia_addr;
208 hostIsNew = 0;
209 } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
210 ia->ia_addr.sipx_addr))
211 hostIsNew = 0;
212 }
213 if ((ifp->if_flags & IFF_POINTOPOINT) &&
214 (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
215 if (hostIsNew == 0)
216 ipx_ifscrub(ifp, ia);
217 ia->ia_dstaddr = ifra->ifra_dstaddr;
218 dstIsNew = 1;
219 }
220 if (ifra->ifra_addr.sipx_family == AF_IPX &&
221 (hostIsNew || dstIsNew))
222 error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
223 return (error);
224
225 default:
226 if (ifp->if_ioctl == NULL)
227 return (EOPNOTSUPP);
228 return ((*ifp->if_ioctl)(ifp, cmd, data));
229 }
230 }
231
232 /*
233 * Delete any previous route for an old address.
234 */
235 static void
236 ipx_ifscrub(ifp, ia)
237 register struct ifnet *ifp;
238 register struct ipx_ifaddr *ia;
239 {
240 if (ia->ia_flags & IFA_ROUTE) {
241 if (ifp->if_flags & IFF_POINTOPOINT) {
242 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
243 } else
244 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
245 ia->ia_flags &= ~IFA_ROUTE;
246 }
247 }
248 /*
249 * Initialize an interface's internet address
250 * and routing table entry.
251 */
252 static int
253 ipx_ifinit(ifp, ia, sipx, scrub)
254 register struct ifnet *ifp;
255 register struct ipx_ifaddr *ia;
256 register struct sockaddr_ipx *sipx;
257 int scrub;
258 {
259 struct sockaddr_ipx oldaddr;
260 int s = splimp(), error;
261
262 /*
263 * Set up new addresses.
264 */
265 oldaddr = ia->ia_addr;
266 ia->ia_addr = *sipx;
267
268 /*
269 * The convention we shall adopt for naming is that
270 * a supplied address of zero means that "we don't care".
271 * Use the MAC address of the interface. If it is an
272 * interface without a MAC address, like a serial line, the
273 * address must be supplied.
274 *
275 * Give the interface a chance to initialize
276 * if this is its first address,
277 * and to validate the address if necessary.
278 */
279 if (ifp->if_ioctl != NULL &&
280 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia))) {
281 ia->ia_addr = oldaddr;
282 splx(s);
283 return (error);
284 }
285 splx(s);
286 ia->ia_ifa.ifa_metric = ifp->if_metric;
287 /*
288 * Add route for the network.
289 */
290 if (scrub) {
291 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
292 ipx_ifscrub(ifp, ia);
293 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
294 }
295 if (ifp->if_flags & IFF_POINTOPOINT)
296 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
297 else {
298 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
299 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
300 }
301 ia->ia_flags |= IFA_ROUTE;
302 return (0);
303 }
304
305 /*
306 * Return address info for specified internet network.
307 */
308 struct ipx_ifaddr *
309 ipx_iaonnetof(dst)
310 register struct ipx_addr *dst;
311 {
312 register struct ipx_ifaddr *ia;
313 register struct ipx_addr *compare;
314 register struct ifnet *ifp;
315 struct ipx_ifaddr *ia_maybe = NULL;
316 union ipx_net net = dst->x_net;
317
318 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
319 if ((ifp = ia->ia_ifp) != NULL) {
320 if (ifp->if_flags & IFF_POINTOPOINT) {
321 compare = &satoipx_addr(ia->ia_dstaddr);
322 if (ipx_hosteq(*dst, *compare))
323 return (ia);
324 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
325 ia_maybe = ia;
326 } else {
327 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
328 return (ia);
329 }
330 }
331 }
332 return (ia_maybe);
333 }
334
335
336 void
337 ipx_printhost(addr)
338 register struct ipx_addr *addr;
339 {
340 u_short port;
341 struct ipx_addr work = *addr;
342 register char *p; register u_char *q;
343 register char *net = "", *host = "";
344 char cport[10], chost[15], cnet[15];
345
346 port = ntohs(work.x_port);
347
348 if (ipx_nullnet(work) && ipx_nullhost(work)) {
349
350 if (port)
351 printf("*.%x", port);
352 else
353 printf("*.*");
354
355 return;
356 }
357
358 if (ipx_wildnet(work))
359 net = "any";
360 else if (ipx_nullnet(work))
361 net = "*";
362 else {
363 q = work.x_net.c_net;
364 snprintf(cnet, sizeof(cnet), "%x%x%x%x",
365 q[0], q[1], q[2], q[3]);
366 for (p = cnet; *p == '' && p < cnet + 8; p++)
367 continue;
368 net = p;
369 }
370
371 if (ipx_wildhost(work))
372 host = "any";
373 else if (ipx_nullhost(work))
374 host = "*";
375 else {
376 q = work.x_host.c_host;
377 snprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
378 q[0], q[1], q[2], q[3], q[4], q[5]);
379 for (p = chost; *p == '' && p < chost + 12; p++)
380 continue;
381 host = p;
382 }
383
384 if (port) {
385 if (strcmp(host, "*") == 0) {
386 host = "";
387 snprintf(cport, sizeof(cport), "%x", port);
388 } else
389 snprintf(cport, sizeof(cport), ".%x", port);
390 } else
391 *cport = 0;
392
393 printf("%s.%s%s", net, host, cport);
394 }
Cache object: 51355b5c64316d524ea5c52bfbff3ba3
|