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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/netipx/ipx_ip.c,v 1.24.2.2 2003/01/23 21:06:48 sam Exp $
36 * Software interface driver for encapsulating IPX in IP.
44 #error The option IPXIP requires option INET.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sockio.h>
56 #include <sys/msgport2.h>
59 #include <net/ifq_var.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
74 static struct ifnet ipxipif;
75 static int ipxipif_units;
77 /* list of all hosts and gateways or broadcast addrs */
78 static struct ifnet_en *ipxip_list;
80 static struct ifnet_en *ipxipattach(void);
81 static int ipxip_free(struct ifnet *ifp);
82 static int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
84 static int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
85 struct sockaddr *dst, struct rtentry *rt);
86 static void ipxip_rtchange(struct in_addr *dst);
87 static void ipxipstart(struct ifnet *ifp, struct ifaltq_subque *);
89 static struct ifnet_en *
95 if (ipxipif.if_mtu == 0) {
97 if_initname(ifp, "ipxip", ipxipif_units);
99 ifp->if_ioctl = ipxipioctl;
100 ifp->if_output = ipxipoutput;
101 ifp->if_start = ipxipstart;
102 ifp->if_flags = IFF_POINTOPOINT;
105 (m) = kmalloc(sizeof(*m), M_PCB, M_WAITOK | M_ZERO);
106 m->ifen_next = ipxip_list;
108 ifp = &m->ifen_ifnet;
110 if_initname(ifp, "ipxip", ipxipif_units++);
112 ifp->if_ioctl = ipxipioctl;
113 ifp->if_output = ipxipoutput;
114 ifp->if_start = ipxipstart;
115 ifp->if_flags = IFF_POINTOPOINT;
116 if_attach(ifp, NULL);
123 * Process an ioctl request.
126 ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
134 ifp->if_flags |= IFF_UP;
139 * Everything else is done at a higher level.
144 ifr = (struct ifreq *)data;
145 if ((ifr->ifr_flags & IFF_UP) == 0)
146 error = ipxip_free(ifp);
155 static struct mbuf *ipxip_badlen;
156 static struct mbuf *ipxip_lastin;
157 static int ipxip_hold_input;
160 ipxip_input(struct mbuf **mp, int *offp, int proto)
162 struct mbuf *m = *mp;
167 if (ipxip_hold_input) {
168 if (ipxip_lastin != NULL) {
169 m_freem(ipxip_lastin);
171 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, MB_DONTWAIT);
174 * Get IP and IPX header together in first mbuf.
176 IFNET_STAT_INC(&ipxipif, ipackets, 1);
177 s = sizeof(struct ip) + sizeof(struct ipx);
178 if (((m->m_flags & M_EXT) || m->m_len < s) &&
179 (m = m_pullup(m, s)) == NULL) {
180 IFNET_STAT_INC(&ipxipif, ierrors, 1);
181 return(IPPROTO_DONE);
183 ip = mtod(m, struct ip *);
184 if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
187 if ((m = m_pullup(m, s)) == NULL) {
188 IFNET_STAT_INC(&ipxipif, ierrors, 1);
189 return(IPPROTO_DONE);
191 ip = mtod(m, struct ip *);
196 * Make mbuf data length reflect IPX length.
197 * If not enough data to reflect IPX length, drop.
199 m->m_data += sizeof(struct ip);
200 m->m_len -= sizeof(struct ip);
201 m->m_pkthdr.len -= sizeof(struct ip);
202 ipx = mtod(m, struct ipx *);
203 len = ntohs(ipx->ipx_len);
205 len++; /* Preserve Garbage Byte */
206 if (ip->ip_len != len) {
207 if (len > ip->ip_len) {
208 IFNET_STAT_INC(&ipxipif, ierrors, 1);
210 m_freem(ipxip_badlen);
212 return(IPPROTO_DONE);
214 /* Any extra will be trimmed off by the IPX routines */
220 netisr_queue(NETISR_IPX, m);
221 return(IPPROTO_DONE);
225 ipxipoutput_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
228 struct ifnet_en *ifn = (struct ifnet_en *)ifp;
230 struct route *ro = &(ifn->ifen_route);
232 struct ipx *ipx = mtod(m, struct ipx *);
235 IFNET_STAT_INC(&ifn->ifen_ifnet, opackets, 1);
236 IFNET_STAT_INC(&ipxipif, opackets, 1);
239 * Calculate data length and make space
242 len = ntohs(ipx->ipx_len);
244 len++; /* Preserve Garbage Byte */
245 /* following clause not necessary on vax */
246 if (3 & (intptr_t)m->m_data) {
247 /* force longword alignment of ip hdr */
248 struct mbuf *m0 = m_gethdr(MT_HEADER, MB_DONTWAIT);
253 MH_ALIGN(m0, sizeof(struct ip));
254 m0->m_flags = m->m_flags & M_COPYFLAGS;
256 m0->m_len = sizeof(struct ip);
257 m0->m_pkthdr.len = m0->m_len + m->m_len;
260 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
267 ip = mtod(m, struct ip *);
269 ip->ip_p = IPPROTO_IDP;
270 ip->ip_src = ifn->ifen_src;
271 ip->ip_dst = ifn->ifen_dst;
272 ip->ip_len = (u_short)len + sizeof(struct ip);
276 * Output final datagram.
278 error = (ip_output(m, NULL, ro, SO_BROADCAST, NULL, NULL));
280 IFNET_STAT_INC(&ifn->ifen_ifnet, oerrors, 1);
281 IFNET_STAT_SET(&ifn->ifen_ifnet, ierrors, error);
285 return (ENETUNREACH);
289 ipxipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
292 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
295 ifsq_serialize_hw(ifsq);
296 error = ipxipoutput_serialized(ifp, m, dst, rt);
297 ifsq_deserialize_hw(ifsq);
303 ipxipstart(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
305 panic("ipxip_start called");
308 static struct ifreq ifr_ipxip = {"ipxip0"};
311 ipxip_route(struct socket *so, struct sockopt *sopt)
314 struct ifnet_en *ifn;
315 struct sockaddr_in *src;
317 struct sockaddr_ipx *ipx_dst;
318 struct sockaddr_in *ip_dst;
321 error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
324 ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
325 ip_dst = (struct sockaddr_in *)&rq.rq_ip;
328 * First, make sure we already have an IPX address:
330 if (ipx_ifaddr == NULL)
331 return (EADDRNOTAVAIL);
333 * Now, determine if we can get to the destination
335 bzero((caddr_t)&ro, sizeof(ro));
336 ro.ro_dst = *(struct sockaddr *)ip_dst;
338 if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
339 return (ENETUNREACH);
343 * And see how he's going to get back to us:
344 * i.e., what return ip address do we use?
347 struct in_ifaddr *ia;
348 struct in_ifaddr_container *iac;
349 struct ifnet *ifp = ro.ro_rt->rt_ifp;
352 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
353 if (iac->ia->ia_ifp == ifp) {
358 if (ia == NULL && !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
359 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
362 return (EADDRNOTAVAIL);
364 src = (struct sockaddr_in *)&ia->ia_addr;
368 * Is there a free (pseudo-)interface or space?
370 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
371 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
380 ifn->ifen_route = ro;
381 ifn->ifen_dst = ip_dst->sin_addr;
382 ifn->ifen_src = src->sin_addr;
385 * now configure this as a point to point link
387 ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
388 ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
389 ipx_control_oncpu(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
390 (struct ifnet *)ifn, sopt->sopt_td);
392 /* use any of our addresses */
393 satoipx_addr(ifr_ipxip.ifr_addr).x_host =
394 ipx_ifaddr->ia_addr.sipx_addr.x_host;
396 return (ipx_control_oncpu(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
397 (struct ifnet *)ifn, sopt->sopt_td));
401 ipxip_free(struct ifnet *ifp)
403 struct ifnet_en *ifn = (struct ifnet_en *)ifp;
404 struct route *ro = & ifn->ifen_route;
406 if (ro->ro_rt != NULL) {
410 ifp->if_flags &= ~IFF_UP;
415 ipxip_ctlinput(netmsg_t msg)
417 int cmd = msg->ctlinput.nm_cmd;
418 struct sockaddr *sa = msg->ctlinput.nm_arg;
419 struct sockaddr_in *sin;
421 if ((unsigned)cmd >= PRC_NCMDS)
423 if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
425 sin = (struct sockaddr_in *)sa;
426 if (sin->sin_addr.s_addr == INADDR_ANY)
432 case PRC_REDIRECT_NET:
433 case PRC_REDIRECT_HOST:
434 case PRC_REDIRECT_TOSNET:
435 case PRC_REDIRECT_TOSHOST:
436 ipxip_rtchange(&sin->sin_addr);
440 lwkt_replymsg(&msg->lmsg, 0);
444 ipxip_rtchange(struct in_addr *dst)
446 struct ifnet_en *ifn;
448 for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
449 if (ifn->ifen_dst.s_addr == dst->s_addr &&
450 ifn->ifen_route.ro_rt != NULL) {
451 RTFREE(ifn->ifen_route.ro_rt);
452 ifn->ifen_route.ro_rt = NULL;