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