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.1/sys/netipx/ipx.c 111487 2003-02-25 15:10:23Z tjr $
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 IFA_LOCK_INIT(ifa);
145 ifa->ifa_refcnt = 1;
146 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
147 ia->ia_ifp = ifp;
148 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
149
150 ifa->ifa_netmask = (struct sockaddr *)&ipx_netmask;
151
152 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
153 if (ifp->if_flags & IFF_BROADCAST) {
154 ia->ia_broadaddr.sipx_family = AF_IPX;
155 ia->ia_broadaddr.sipx_len = sizeof(ia->ia_addr);
156 ia->ia_broadaddr.sipx_addr.x_host = ipx_broadhost;
157 }
158 }
159 }
160
161 switch (cmd) {
162
163 case SIOCSIFDSTADDR:
164 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
165 return (EINVAL);
166 if (ia->ia_flags & IFA_ROUTE) {
167 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
168 ia->ia_flags &= ~IFA_ROUTE;
169 }
170 if (ifp->if_ioctl) {
171 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (void *)ia);
172 if (error)
173 return (error);
174 }
175 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
176 return (0);
177
178 case SIOCSIFADDR:
179 return (ipx_ifinit(ifp, ia,
180 (struct sockaddr_ipx *)&ifr->ifr_addr, 1));
181
182 case SIOCDIFADDR:
183 ipx_ifscrub(ifp, ia);
184 ifa = (struct ifaddr *)ia;
185 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
186 oia = ia;
187 if (oia == (ia = ipx_ifaddr)) {
188 ipx_ifaddr = ia->ia_next;
189 } else {
190 while (ia->ia_next && (ia->ia_next != oia)) {
191 ia = ia->ia_next;
192 }
193 if (ia->ia_next)
194 ia->ia_next = oia->ia_next;
195 else
196 printf("Didn't unlink ipxifadr from list\n");
197 }
198 IFAFREE((&oia->ia_ifa));
199 return (0);
200
201 case SIOCAIFADDR:
202 dstIsNew = 0;
203 hostIsNew = 1;
204 if (ia->ia_addr.sipx_family == AF_IPX) {
205 if (ifra->ifra_addr.sipx_len == 0) {
206 ifra->ifra_addr = ia->ia_addr;
207 hostIsNew = 0;
208 } else if (ipx_neteq(ifra->ifra_addr.sipx_addr,
209 ia->ia_addr.sipx_addr))
210 hostIsNew = 0;
211 }
212 if ((ifp->if_flags & IFF_POINTOPOINT) &&
213 (ifra->ifra_dstaddr.sipx_family == AF_IPX)) {
214 if (hostIsNew == 0)
215 ipx_ifscrub(ifp, ia);
216 ia->ia_dstaddr = ifra->ifra_dstaddr;
217 dstIsNew = 1;
218 }
219 if (ifra->ifra_addr.sipx_family == AF_IPX &&
220 (hostIsNew || dstIsNew))
221 error = ipx_ifinit(ifp, ia, &ifra->ifra_addr, 0);
222 return (error);
223
224 default:
225 if (ifp->if_ioctl == NULL)
226 return (EOPNOTSUPP);
227 return ((*ifp->if_ioctl)(ifp, cmd, data));
228 }
229 }
230
231 /*
232 * Delete any previous route for an old address.
233 */
234 static void
235 ipx_ifscrub(ifp, ia)
236 register struct ifnet *ifp;
237 register struct ipx_ifaddr *ia;
238 {
239 if (ia->ia_flags & IFA_ROUTE) {
240 if (ifp->if_flags & IFF_POINTOPOINT) {
241 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
242 } else
243 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
244 ia->ia_flags &= ~IFA_ROUTE;
245 }
246 }
247 /*
248 * Initialize an interface's internet address
249 * and routing table entry.
250 */
251 static int
252 ipx_ifinit(ifp, ia, sipx, scrub)
253 register struct ifnet *ifp;
254 register struct ipx_ifaddr *ia;
255 register struct sockaddr_ipx *sipx;
256 int scrub;
257 {
258 struct sockaddr_ipx oldaddr;
259 int s = splimp(), error;
260
261 /*
262 * Set up new addresses.
263 */
264 oldaddr = ia->ia_addr;
265 ia->ia_addr = *sipx;
266
267 /*
268 * The convention we shall adopt for naming is that
269 * a supplied address of zero means that "we don't care".
270 * Use the MAC address of the interface. If it is an
271 * interface without a MAC address, like a serial line, the
272 * address must be supplied.
273 *
274 * Give the interface a chance to initialize
275 * if this is its first address,
276 * and to validate the address if necessary.
277 */
278 if (ifp->if_ioctl != NULL &&
279 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (void *)ia))) {
280 ia->ia_addr = oldaddr;
281 splx(s);
282 return (error);
283 }
284 splx(s);
285 ia->ia_ifa.ifa_metric = ifp->if_metric;
286 /*
287 * Add route for the network.
288 */
289 if (scrub) {
290 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
291 ipx_ifscrub(ifp, ia);
292 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
293 }
294 if (ifp->if_flags & IFF_POINTOPOINT)
295 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
296 else {
297 ia->ia_broadaddr.sipx_addr.x_net = ia->ia_addr.sipx_addr.x_net;
298 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
299 }
300 ia->ia_flags |= IFA_ROUTE;
301 return (0);
302 }
303
304 /*
305 * Return address info for specified internet network.
306 */
307 struct ipx_ifaddr *
308 ipx_iaonnetof(dst)
309 register struct ipx_addr *dst;
310 {
311 register struct ipx_ifaddr *ia;
312 register struct ipx_addr *compare;
313 register struct ifnet *ifp;
314 struct ipx_ifaddr *ia_maybe = NULL;
315 union ipx_net net = dst->x_net;
316
317 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next) {
318 if ((ifp = ia->ia_ifp) != NULL) {
319 if (ifp->if_flags & IFF_POINTOPOINT) {
320 compare = &satoipx_addr(ia->ia_dstaddr);
321 if (ipx_hosteq(*dst, *compare))
322 return (ia);
323 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
324 ia_maybe = ia;
325 } else {
326 if (ipx_neteqnn(net, ia->ia_addr.sipx_addr.x_net))
327 return (ia);
328 }
329 }
330 }
331 return (ia_maybe);
332 }
333
334
335 void
336 ipx_printhost(addr)
337 register struct ipx_addr *addr;
338 {
339 u_short port;
340 struct ipx_addr work = *addr;
341 register char *p; register u_char *q;
342 register char *net = "", *host = "";
343 char cport[10], chost[15], cnet[15];
344
345 port = ntohs(work.x_port);
346
347 if (ipx_nullnet(work) && ipx_nullhost(work)) {
348
349 if (port)
350 printf("*.%x", port);
351 else
352 printf("*.*");
353
354 return;
355 }
356
357 if (ipx_wildnet(work))
358 net = "any";
359 else if (ipx_nullnet(work))
360 net = "*";
361 else {
362 q = work.x_net.c_net;
363 snprintf(cnet, sizeof(cnet), "%x%x%x%x",
364 q[0], q[1], q[2], q[3]);
365 for (p = cnet; *p == '' && p < cnet + 8; p++)
366 continue;
367 net = p;
368 }
369
370 if (ipx_wildhost(work))
371 host = "any";
372 else if (ipx_nullhost(work))
373 host = "*";
374 else {
375 q = work.x_host.c_host;
376 snprintf(chost, sizeof(chost), "%x%x%x%x%x%x",
377 q[0], q[1], q[2], q[3], q[4], q[5]);
378 for (p = chost; *p == '' && p < chost + 12; p++)
379 continue;
380 host = p;
381 }
382
383 if (port) {
384 if (strcmp(host, "*") == 0) {
385 host = "";
386 snprintf(cport, sizeof(cport), "%x", port);
387 } else
388 snprintf(cport, sizeof(cport), ".%x", port);
389 } else
390 *cport = 0;
391
392 printf("%s.%s%s", net, host, cport);
393 }
Cache object: fd743c01c92cb470959540f234d92a31
|