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