2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Copyright (c) 1995, Mike Mitchell
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
66 * Software interface driver for encapsulating IPX in IP.
74 #error The option IPXIP requires option INET.
77 #include <sys/param.h>
78 #include <sys/kernel.h>
79 #include <sys/systm.h>
80 #include <sys/malloc.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sockio.h>
88 #include <net/if_types.h>
89 #include <net/netisr.h>
90 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/ip_options.h>
99 #include <netipx/ipx.h>
100 #include <netipx/ipx_if.h>
101 #include <netipx/ipx_ip.h>
102 #include <netipx/ipx_var.h>
104 NET_NEEDS_GIANT("ipx_ip");
106 static struct ifnet ipxipif;
107 static int ipxipif_units;
109 /* List of all hosts and gateways or broadcast addrs. */
110 static struct ifnet_en *ipxip_list;
112 static struct ifreq ifr_ipxip = {"ipxip0"};
113 static struct mbuf *ipxip_badlen;
114 static struct mbuf *ipxip_lastin;
115 static int ipxip_hold_input;
117 static struct ifnet_en *ipxipattach(void);
118 static int ipxip_free(struct ifnet *ifp);
119 static int ipxipioctl(struct ifnet *ifp, u_long cmd,
121 static int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
122 struct sockaddr *dst, struct rtentry *rt);
123 static void ipxip_rtchange(struct in_addr *dst);
124 static void ipxipstart(struct ifnet *ifp);
126 static struct ifnet_en *
132 if (ipxipif.if_mtu == 0) {
134 if_initname(ifp, "ipxip", ipxipif_units);
136 ifp->if_ioctl = ipxipioctl;
137 ifp->if_output = ipxipoutput;
138 ifp->if_start = ipxipstart;
139 ifp->if_flags = IFF_POINTOPOINT;
142 MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
145 m->ifen_next = ipxip_list;
147 ifp = m->ifen_ifp = if_alloc(IFT_IPXIP);
153 if_initname(ifp, "ipxip", ipxipif_units++);
155 ifp->if_ioctl = ipxipioctl;
156 ifp->if_output = ipxipoutput;
157 ifp->if_start = ipxipstart;
158 ifp->if_flags = IFF_POINTOPOINT;
166 * Process an ioctl request.
169 ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
176 ifp->if_flags |= IFF_UP;
181 * Everything else is done at a higher level.
186 ifr = (struct ifreq *)data;
187 if ((ifr->ifr_flags & IFF_UP) == 0)
188 error = ipxip_free(ifp);
197 ipxip_input(struct mbuf *m, int hlen)
203 if (ipxip_hold_input) {
204 if (ipxip_lastin != NULL)
205 m_freem(ipxip_lastin);
206 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
210 * Get IP and IPX header together in first mbuf.
212 ipxipif.if_ipackets++;
213 s = sizeof(struct ip) + sizeof(struct ipx);
214 if (((m->m_flags & M_EXT) || m->m_len < s) &&
215 (m = m_pullup(m, s)) == NULL) {
216 ipxipif.if_ierrors++;
219 ip = mtod(m, struct ip *);
220 if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
221 ip_stripoptions(m, (struct mbuf *)NULL);
223 if ((m = m_pullup(m, s)) == NULL) {
224 ipxipif.if_ierrors++;
227 ip = mtod(m, struct ip *);
232 * Make mbuf data length reflect IPX length. If not enough data to
233 * reflect IPX length, drop.
235 m->m_data += sizeof(struct ip);
236 m->m_len -= sizeof(struct ip);
237 m->m_pkthdr.len -= sizeof(struct ip);
238 ipx = mtod(m, struct ipx *);
239 len = ntohs(ipx->ipx_len);
241 len++; /* Preserve Garbage Byte */
242 if (ip->ip_len != len) {
243 if (len > ip->ip_len) {
244 ipxipif.if_ierrors++;
246 m_freem(ipxip_badlen);
251 * Any extra will be trimmed off by the IPX routines.
258 netisr_dispatch(NETISR_IPX, m);
262 ipxipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
265 struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
266 struct route *ro = &(ifn->ifen_route);
267 struct ipx *ipx = mtod(m, struct ipx *);
272 ifn->ifen_ifp->if_opackets++;
273 ipxipif.if_opackets++;
276 * Calculate data length and make space for IP header.
278 len = ntohs(ipx->ipx_len);
280 len++; /* Preserve Garbage Byte */
283 * Following clause not necessary on VAX.
285 if (3 & (intptr_t)m->m_data) {
286 /* force longword alignment of ip hdr */
287 struct mbuf *m0 = m_gethdr(MT_DATA, M_DONTWAIT);
292 MH_ALIGN(m0, sizeof(struct ip));
293 m0->m_flags = m->m_flags & M_COPYFLAGS;
295 m0->m_len = sizeof(struct ip);
296 m0->m_pkthdr.len = m0->m_len + m->m_len;
297 m->m_flags &= ~M_PKTHDR;
300 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
308 ip = mtod(m, struct ip *);
310 ip->ip_p = IPPROTO_IDP;
311 ip->ip_src = ifn->ifen_src;
312 ip->ip_dst = ifn->ifen_dst;
313 ip->ip_len = (u_short)len + sizeof(struct ip);
317 * Output final datagram.
319 error = ip_output(m, (struct mbuf *)NULL, ro, SO_BROADCAST, NULL,
322 ifn->ifen_ifp->if_oerrors++;
323 ifn->ifen_ifp->if_ierrors = error;
329 ipxipstart(struct ifnet *ifp)
332 panic("ipxip_start called\n");
336 ipxip_route(struct socket *so, struct sockopt *sopt)
338 struct in_ifaddr *ia;
339 struct ifnet_en *ifn;
340 struct sockaddr_in *src;
342 struct sockaddr_ipx *ipx_dst;
343 struct sockaddr_in *ip_dst;
348 error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
351 ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
352 ip_dst = (struct sockaddr_in *)&rq.rq_ip;
355 * First, make sure we already have an IPX address:
357 if (ipx_ifaddr == NULL)
358 return (EADDRNOTAVAIL);
361 * Now, determine if we can get to the destination.
363 bzero((caddr_t)&ro, sizeof(ro));
364 ro.ro_dst = *(struct sockaddr *)ip_dst;
366 if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL)
367 return (ENETUNREACH);
370 * And see how he's going to get back to us: i.e., what return IP
373 ifp = ro.ro_rt->rt_ifp;
374 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL;
375 ia = TAILQ_NEXT(ia, ia_link))
376 if (ia->ia_ifp == ifp)
379 ia = TAILQ_FIRST(&in_ifaddrhead);
382 return (EADDRNOTAVAIL);
384 src = (struct sockaddr_in *)&ia->ia_addr;
387 * Is there a free (pseudo-)interface or space?
389 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
390 if ((ifn->ifen_ifp->if_flags & IFF_UP) == 0)
399 ifn->ifen_route = ro;
400 ifn->ifen_dst = ip_dst->sin_addr;
401 ifn->ifen_src = src->sin_addr;
404 * Now configure this as a point to point link.
406 ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
407 ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
408 ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
409 (struct ifnet *)ifn, sopt->sopt_td);
412 * Use any of our addresses.
414 satoipx_addr(ifr_ipxip.ifr_addr).x_host =
415 ipx_ifaddr->ia_addr.sipx_addr.x_host;
417 return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
418 (struct ifnet *)ifn, sopt->sopt_td));
422 ipxip_free(struct ifnet *ifp)
424 struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
425 struct route *ro = & ifn->ifen_route;
427 if (ro->ro_rt != NULL) {
431 ifp->if_flags &= ~IFF_UP;
436 ipxip_ctlinput(int cmd, struct sockaddr *sa, void *dummy)
438 struct sockaddr_in *sin;
440 if ((unsigned)cmd >= PRC_NCMDS)
442 if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
444 sin = (struct sockaddr_in *)sa;
445 if (sin->sin_addr.s_addr == INADDR_ANY)
450 case PRC_REDIRECT_NET:
451 case PRC_REDIRECT_HOST:
452 case PRC_REDIRECT_TOSNET:
453 case PRC_REDIRECT_TOSHOST:
454 ipxip_rtchange(&sin->sin_addr);
460 ipxip_rtchange(struct in_addr *dst)
462 struct ifnet_en *ifn;
464 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
465 if (ifn->ifen_dst.s_addr == dst->s_addr &&
466 ifn->ifen_route.ro_rt != NULL) {
467 RTFREE(ifn->ifen_route.ro_rt);
468 ifn->ifen_route.ro_rt = NULL;