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