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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
36 * $FreeBSD: src/sys/netipx/ipx_ip.c,v 1.24.2.2 2003/01/23 21:06:48 sam Exp $
37 * $DragonFly: src/sys/netproto/ipx/ipx_ip.c,v 1.18 2008/06/08 08:38:05 sephe Exp $
41 * Software interface driver for encapsulating IPX in IP.
49 #error The option IPXIP requires option INET.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sockio.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
76 static struct ifnet ipxipif;
77 static int ipxipif_units;
79 /* list of all hosts and gateways or broadcast addrs */
80 static struct ifnet_en *ipxip_list;
82 static struct ifnet_en *ipxipattach(void);
83 static int ipxip_free(struct ifnet *ifp);
84 static int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
86 static int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
87 struct sockaddr *dst, struct rtentry *rt);
88 static void ipxip_rtchange(struct in_addr *dst);
89 static void ipxipstart(struct ifnet *ifp);
91 static struct ifnet_en *
97 if (ipxipif.if_mtu == 0) {
99 if_initname(ifp, "ipxip", ipxipif_units);
101 ifp->if_ioctl = ipxipioctl;
102 ifp->if_output = ipxipoutput;
103 ifp->if_start = ipxipstart;
104 ifp->if_flags = IFF_POINTOPOINT;
107 MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_WAITOK | M_ZERO);
108 m->ifen_next = ipxip_list;
110 ifp = &m->ifen_ifnet;
112 if_initname(ifp, "ipxip", ipxipif_units++);
114 ifp->if_ioctl = ipxipioctl;
115 ifp->if_output = ipxipoutput;
116 ifp->if_start = ipxipstart;
117 ifp->if_flags = IFF_POINTOPOINT;
118 if_attach(ifp, NULL);
125 * Process an ioctl request.
128 ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
136 ifp->if_flags |= IFF_UP;
141 * Everything else is done at a higher level.
146 ifr = (struct ifreq *)data;
147 if ((ifr->ifr_flags & IFF_UP) == 0)
148 error = ipxip_free(ifp);
157 static struct mbuf *ipxip_badlen;
158 static struct mbuf *ipxip_lastin;
159 static int ipxip_hold_input;
162 ipxip_input(struct mbuf **mp, int *offp, int proto)
164 struct mbuf *m = *mp;
169 if (ipxip_hold_input) {
170 if (ipxip_lastin != NULL) {
171 m_freem(ipxip_lastin);
173 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, MB_DONTWAIT);
176 * Get IP and IPX header together in first mbuf.
178 ipxipif.if_ipackets++;
179 s = sizeof(struct ip) + sizeof(struct ipx);
180 if (((m->m_flags & M_EXT) || m->m_len < s) &&
181 (m = m_pullup(m, s)) == NULL) {
182 ipxipif.if_ierrors++;
183 return(IPPROTO_DONE);
185 ip = mtod(m, struct ip *);
186 if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
189 if ((m = m_pullup(m, s)) == NULL) {
190 ipxipif.if_ierrors++;
191 return(IPPROTO_DONE);
193 ip = mtod(m, struct ip *);
198 * Make mbuf data length reflect IPX length.
199 * If not enough data to reflect IPX length, drop.
201 m->m_data += sizeof(struct ip);
202 m->m_len -= sizeof(struct ip);
203 m->m_pkthdr.len -= sizeof(struct ip);
204 ipx = mtod(m, struct ipx *);
205 len = ntohs(ipx->ipx_len);
207 len++; /* Preserve Garbage Byte */
208 if (ip->ip_len != len) {
209 if (len > ip->ip_len) {
210 ipxipif.if_ierrors++;
212 m_freem(ipxip_badlen);
214 return(IPPROTO_DONE);
216 /* Any extra will be trimmed off by the IPX routines */
222 netisr_queue(NETISR_IPX, m);
223 return(IPPROTO_DONE);
227 ipxipoutput_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
230 struct ifnet_en *ifn = (struct ifnet_en *)ifp;
232 struct route *ro = &(ifn->ifen_route);
234 struct ipx *ipx = mtod(m, struct ipx *);
237 ifn->ifen_ifnet.if_opackets++;
238 ipxipif.if_opackets++;
241 * Calculate data length and make space
244 len = ntohs(ipx->ipx_len);
246 len++; /* Preserve Garbage Byte */
247 /* following clause not necessary on vax */
248 if (3 & (int)m->m_data) {
249 /* force longword alignment of ip hdr */
250 struct mbuf *m0 = m_gethdr(MT_HEADER, MB_DONTWAIT);
255 MH_ALIGN(m0, sizeof(struct ip));
256 m0->m_flags = m->m_flags & M_COPYFLAGS;
258 m0->m_len = sizeof(struct ip);
259 m0->m_pkthdr.len = m0->m_len + m->m_len;
262 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
269 ip = mtod(m, struct ip *);
271 ip->ip_p = IPPROTO_IDP;
272 ip->ip_src = ifn->ifen_src;
273 ip->ip_dst = ifn->ifen_dst;
274 ip->ip_len = (u_short)len + sizeof(struct ip);
278 * Output final datagram.
280 error = (ip_output(m, NULL, ro, SO_BROADCAST, NULL, NULL));
282 ifn->ifen_ifnet.if_oerrors++;
283 ifn->ifen_ifnet.if_ierrors = error;
287 return (ENETUNREACH);
291 ipxipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
296 ifnet_serialize_tx(ifp);
297 error = ipxipoutput_serialized(ifp, m, dst, rt);
298 ifnet_deserialize_tx(ifp);
304 ipxipstart(struct ifnet *ifp)
306 panic("ipxip_start called\n");
309 static struct ifreq ifr_ipxip = {"ipxip0"};
312 ipxip_route(struct socket *so, struct sockopt *sopt)
315 struct ifnet_en *ifn;
316 struct sockaddr_in *src;
318 struct sockaddr_ipx *ipx_dst;
319 struct sockaddr_in *ip_dst;
322 error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
325 ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
326 ip_dst = (struct sockaddr_in *)&rq.rq_ip;
329 * First, make sure we already have an IPX address:
331 if (ipx_ifaddr == NULL)
332 return (EADDRNOTAVAIL);
334 * Now, determine if we can get to the destination
336 bzero((caddr_t)&ro, sizeof(ro));
337 ro.ro_dst = *(struct sockaddr *)ip_dst;
339 if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
340 return (ENETUNREACH);
344 * And see how he's going to get back to us:
345 * i.e., what return ip address do we use?
348 struct in_ifaddr *ia;
349 struct in_ifaddr_container *iac;
350 struct ifnet *ifp = ro.ro_rt->rt_ifp;
353 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
354 if (iac->ia->ia_ifp == ifp) {
359 if (ia == NULL && !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
360 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
363 return (EADDRNOTAVAIL);
365 src = (struct sockaddr_in *)&ia->ia_addr;
369 * Is there a free (pseudo-)interface or space?
371 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
372 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
381 ifn->ifen_route = ro;
382 ifn->ifen_dst = ip_dst->sin_addr;
383 ifn->ifen_src = src->sin_addr;
386 * now configure this as a point to point link
388 ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
389 ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
390 ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
391 (struct ifnet *)ifn, sopt->sopt_td);
393 /* use any of our addresses */
394 satoipx_addr(ifr_ipxip.ifr_addr).x_host =
395 ipx_ifaddr->ia_addr.sipx_addr.x_host;
397 return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
398 (struct ifnet *)ifn, sopt->sopt_td));
402 ipxip_free(struct ifnet *ifp)
404 struct ifnet_en *ifn = (struct ifnet_en *)ifp;
405 struct route *ro = & ifn->ifen_route;
407 if (ro->ro_rt != NULL) {
411 ifp->if_flags &= ~IFF_UP;
416 ipxip_ctlinput(netmsg_t msg)
418 int cmd = msg->ctlinput.nm_cmd;
419 struct sockaddr *sa = msg->ctlinput.nm_data;
420 struct sockaddr_in *sin;
422 if ((unsigned)cmd >= PRC_NCMDS)
424 if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
426 sin = (struct sockaddr_in *)sa;
427 if (sin->sin_addr.s_addr == INADDR_ANY)
433 case PRC_REDIRECT_NET:
434 case PRC_REDIRECT_HOST:
435 case PRC_REDIRECT_TOSNET:
436 case PRC_REDIRECT_TOSHOST:
437 ipxip_rtchange(&sin->sin_addr);
441 lwkt_replymsg(&msg->lmsg, 0);
445 ipxip_rtchange(struct in_addr *dst)
447 struct ifnet_en *ifn;
449 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
450 if (ifn->ifen_dst.s_addr == dst->s_addr &&
451 ifn->ifen_route.ro_rt != NULL) {
452 RTFREE(ifn->ifen_route.ro_rt);
453 ifn->ifen_route.ro_rt = NULL;