FreeBSD/Linux Kernel Cross Reference
sys/netipx/ipx_ip.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_ip.c
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: releng/6.4/sys/netipx/ipx_ip.c 157262 2006-03-29 12:42:43Z rwatson $");
39
40 /*
41 * Software interface driver for encapsulating IPX in IP.
42 */
43
44 #include "opt_inet.h"
45 #include "opt_ipx.h"
46
47 #ifdef IPXIP
48 #ifndef INET
49 #error The option IPXIP requires option INET.
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/kernel.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/protosw.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
60 #include <sys/sockio.h>
61
62 #include <net/if.h>
63 #include <net/if_types.h>
64 #include <net/netisr.h>
65 #include <net/route.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
71 #include <netinet/ip_var.h>
72
73 #include <netipx/ipx.h>
74 #include <netipx/ipx_if.h>
75 #include <netipx/ipx_ip.h>
76 #include <netipx/ipx_var.h>
77
78 NET_NEEDS_GIANT("ipx_ip");
79
80 static struct ifnet ipxipif;
81 static int ipxipif_units;
82
83 /* list of all hosts and gateways or broadcast addrs */
84 static struct ifnet_en *ipxip_list;
85
86 static struct ifnet_en *ipxipattach(void);
87 static int ipxip_free(struct ifnet *ifp);
88 static int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
89 static int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
90 struct sockaddr *dst, struct rtentry *rt);
91 static void ipxip_rtchange(struct in_addr *dst);
92 static void ipxipstart(struct ifnet *ifp);
93
94 static struct ifnet_en *
95 ipxipattach()
96 {
97 register struct ifnet_en *m;
98 register struct ifnet *ifp;
99
100 if (ipxipif.if_mtu == 0) {
101 ifp = &ipxipif;
102 if_initname(ifp, "ipxip", ipxipif_units);
103 ifp->if_mtu = LOMTU;
104 ifp->if_ioctl = ipxipioctl;
105 ifp->if_output = ipxipoutput;
106 ifp->if_start = ipxipstart;
107 ifp->if_flags = IFF_POINTOPOINT;
108 }
109
110 MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
111 if (m == NULL)
112 return (NULL);
113 m->ifen_next = ipxip_list;
114 ipxip_list = m;
115 ifp = m->ifen_ifp = if_alloc(IFT_IPXIP);
116 if (ifp == NULL) {
117 FREE(m, M_PCB);
118 return (NULL);
119 }
120
121 if_initname(ifp, "ipxip", ipxipif_units++);
122 ifp->if_mtu = LOMTU;
123 ifp->if_ioctl = ipxipioctl;
124 ifp->if_output = ipxipoutput;
125 ifp->if_start = ipxipstart;
126 ifp->if_flags = IFF_POINTOPOINT;
127 ifp->if_softc = m;
128 if_attach(ifp);
129
130 return (m);
131 }
132
133
134 /*
135 * Process an ioctl request.
136 */
137 static int
138 ipxipioctl(ifp, cmd, data)
139 register struct ifnet *ifp;
140 u_long cmd;
141 caddr_t data;
142 {
143 int error = 0;
144 struct ifreq *ifr;
145
146 switch (cmd) {
147
148 case SIOCSIFADDR:
149 ifp->if_flags |= IFF_UP;
150 /* FALLTHROUGH */
151
152 case SIOCSIFDSTADDR:
153 /*
154 * Everything else is done at a higher level.
155 */
156 break;
157
158 case SIOCSIFFLAGS:
159 ifr = (struct ifreq *)data;
160 if ((ifr->ifr_flags & IFF_UP) == 0)
161 error = ipxip_free(ifp);
162
163
164 default:
165 error = EINVAL;
166 }
167 return (error);
168 }
169
170 static struct mbuf *ipxip_badlen;
171 static struct mbuf *ipxip_lastin;
172 static int ipxip_hold_input;
173
174 void
175 ipxip_input(m, hlen)
176 register struct mbuf *m;
177 int hlen;
178 {
179 register struct ip *ip;
180 register struct ipx *ipx;
181 int len, s;
182
183 if (ipxip_hold_input) {
184 if (ipxip_lastin != NULL) {
185 m_freem(ipxip_lastin);
186 }
187 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
188 }
189 /*
190 * Get IP and IPX header together in first mbuf.
191 */
192 ipxipif.if_ipackets++;
193 s = sizeof(struct ip) + sizeof(struct ipx);
194 if (((m->m_flags & M_EXT) || m->m_len < s) &&
195 (m = m_pullup(m, s)) == NULL) {
196 ipxipif.if_ierrors++;
197 return;
198 }
199 ip = mtod(m, struct ip *);
200 if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
201 ip_stripoptions(m, (struct mbuf *)NULL);
202 if (m->m_len < s) {
203 if ((m = m_pullup(m, s)) == NULL) {
204 ipxipif.if_ierrors++;
205 return;
206 }
207 ip = mtod(m, struct ip *);
208 }
209 }
210
211 /*
212 * Make mbuf data length reflect IPX length.
213 * If not enough data to reflect IPX length, drop.
214 */
215 m->m_data += sizeof(struct ip);
216 m->m_len -= sizeof(struct ip);
217 m->m_pkthdr.len -= sizeof(struct ip);
218 ipx = mtod(m, struct ipx *);
219 len = ntohs(ipx->ipx_len);
220 if (len & 1)
221 len++; /* Preserve Garbage Byte */
222 if (ip->ip_len != len) {
223 if (len > ip->ip_len) {
224 ipxipif.if_ierrors++;
225 if (ipxip_badlen)
226 m_freem(ipxip_badlen);
227 ipxip_badlen = m;
228 return;
229 }
230 /* Any extra will be trimmed off by the IPX routines */
231 }
232
233 /*
234 * Deliver to IPX
235 */
236 netisr_dispatch(NETISR_IPX, m);
237 }
238
239 static int
240 ipxipoutput(ifp, m, dst, rt)
241 struct ifnet *ifp;
242 struct mbuf *m;
243 struct sockaddr *dst;
244 struct rtentry *rt;
245 {
246 register struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
247 register struct ip *ip;
248 register struct route *ro = &(ifn->ifen_route);
249 register int len = 0;
250 register struct ipx *ipx = mtod(m, struct ipx *);
251 int error;
252
253 ifn->ifen_ifp->if_opackets++;
254 ipxipif.if_opackets++;
255
256 /*
257 * Calculate data length and make space
258 * for IP header.
259 */
260 len = ntohs(ipx->ipx_len);
261 if (len & 1)
262 len++; /* Preserve Garbage Byte */
263 /* following clause not necessary on vax */
264 if (3 & (intptr_t)m->m_data) {
265 /* force longword alignment of ip hdr */
266 struct mbuf *m0 = m_gethdr(MT_HEADER, M_DONTWAIT);
267 if (m0 == NULL) {
268 m_freem(m);
269 return (ENOBUFS);
270 }
271 MH_ALIGN(m0, sizeof(struct ip));
272 m0->m_flags = m->m_flags & M_COPYFLAGS;
273 m0->m_next = m;
274 m0->m_len = sizeof(struct ip);
275 m0->m_pkthdr.len = m0->m_len + m->m_len;
276 m->m_flags &= ~M_PKTHDR;
277 m = m0;
278 } else {
279 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
280 if (m == NULL)
281 return (ENOBUFS);
282 }
283 /*
284 * Fill in IP header.
285 */
286 ip = mtod(m, struct ip *);
287 *(long *)ip = 0;
288 ip->ip_p = IPPROTO_IDP;
289 ip->ip_src = ifn->ifen_src;
290 ip->ip_dst = ifn->ifen_dst;
291 ip->ip_len = (u_short)len + sizeof(struct ip);
292 ip->ip_ttl = MAXTTL;
293
294 /*
295 * Output final datagram.
296 */
297 error = (ip_output(m, (struct mbuf *)NULL, ro, SO_BROADCAST, NULL, NULL));
298 if (error) {
299 ifn->ifen_ifp->if_oerrors++;
300 ifn->ifen_ifp->if_ierrors = error;
301 }
302 return (error);
303 m_freem(m);
304 return (ENETUNREACH);
305 }
306
307 static void
308 ipxipstart(ifp)
309 struct ifnet *ifp;
310 {
311 panic("ipxip_start called\n");
312 }
313
314 static struct ifreq ifr_ipxip = {"ipxip0"};
315
316 int
317 ipxip_route(so, sopt)
318 struct socket *so;
319 struct sockopt *sopt;
320 {
321 int error;
322 struct ifnet_en *ifn;
323 struct sockaddr_in *src;
324 struct ipxip_req rq;
325 struct sockaddr_ipx *ipx_dst;
326 struct sockaddr_in *ip_dst;
327 struct route ro;
328
329 error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
330 if (error)
331 return (error);
332 ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
333 ip_dst = (struct sockaddr_in *)&rq.rq_ip;
334
335 /*
336 * First, make sure we already have an IPX address:
337 */
338 if (ipx_ifaddr == NULL)
339 return (EADDRNOTAVAIL);
340 /*
341 * Now, determine if we can get to the destination
342 */
343 bzero((caddr_t)&ro, sizeof(ro));
344 ro.ro_dst = *(struct sockaddr *)ip_dst;
345 rtalloc_ign(&ro, 0);
346 if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
347 return (ENETUNREACH);
348 }
349
350 /*
351 * And see how he's going to get back to us:
352 * i.e., what return ip address do we use?
353 */
354 {
355 register struct in_ifaddr *ia;
356 struct ifnet *ifp = ro.ro_rt->rt_ifp;
357
358 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL;
359 ia = TAILQ_NEXT(ia, ia_link))
360 if (ia->ia_ifp == ifp)
361 break;
362 if (ia == NULL)
363 ia = TAILQ_FIRST(&in_ifaddrhead);
364 if (ia == NULL) {
365 RTFREE(ro.ro_rt);
366 return (EADDRNOTAVAIL);
367 }
368 src = (struct sockaddr_in *)&ia->ia_addr;
369 }
370
371 /*
372 * Is there a free (pseudo-)interface or space?
373 */
374 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
375 if ((ifn->ifen_ifp->if_flags & IFF_UP) == 0)
376 break;
377 }
378 if (ifn == NULL)
379 ifn = ipxipattach();
380 if (ifn == NULL) {
381 RTFREE(ro.ro_rt);
382 return (ENOBUFS);
383 }
384 ifn->ifen_route = ro;
385 ifn->ifen_dst = ip_dst->sin_addr;
386 ifn->ifen_src = src->sin_addr;
387
388 /*
389 * now configure this as a point to point link
390 */
391 ifr_ipxip.ifr_name[4] = '' + ipxipif_units - 1;
392 ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
393 ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
394 (struct ifnet *)ifn, sopt->sopt_td);
395
396 /* use any of our addresses */
397 satoipx_addr(ifr_ipxip.ifr_addr).x_host =
398 ipx_ifaddr->ia_addr.sipx_addr.x_host;
399
400 return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
401 (struct ifnet *)ifn, sopt->sopt_td));
402 }
403
404 static int
405 ipxip_free(ifp)
406 struct ifnet *ifp;
407 {
408 register struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
409 struct route *ro = & ifn->ifen_route;
410
411 if (ro->ro_rt != NULL) {
412 RTFREE(ro->ro_rt);
413 ro->ro_rt = NULL;
414 }
415 ifp->if_flags &= ~IFF_UP;
416 return (0);
417 }
418
419 void
420 ipxip_ctlinput(cmd, sa, dummy)
421 int cmd;
422 struct sockaddr *sa;
423 void *dummy;
424 {
425 struct sockaddr_in *sin;
426
427 if ((unsigned)cmd >= PRC_NCMDS)
428 return;
429 if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
430 return;
431 sin = (struct sockaddr_in *)sa;
432 if (sin->sin_addr.s_addr == INADDR_ANY)
433 return;
434
435 switch (cmd) {
436
437 case PRC_ROUTEDEAD:
438 case PRC_REDIRECT_NET:
439 case PRC_REDIRECT_HOST:
440 case PRC_REDIRECT_TOSNET:
441 case PRC_REDIRECT_TOSHOST:
442 ipxip_rtchange(&sin->sin_addr);
443 break;
444 }
445 }
446
447 static void
448 ipxip_rtchange(dst)
449 register struct in_addr *dst;
450 {
451 register struct ifnet_en *ifn;
452
453 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
454 if (ifn->ifen_dst.s_addr == dst->s_addr &&
455 ifn->ifen_route.ro_rt != NULL) {
456 RTFREE(ifn->ifen_route.ro_rt);
457 ifn->ifen_route.ro_rt = NULL;
458 }
459 }
460 }
461 #endif /* IPXIP */
Cache object: 0bed137481b48281db4068b454ad0a44
|