Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.21
      1 /*	$NetBSD: if_gre.c,v 1.21 2001/05/10 01:30:55 itojun Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Heiko W.Rupp <hwr (at) pilhuhn.de>
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Encapsulate L3 protocols into IP
     41  * See RFC 1701 and 1702 for more details.
     42  * If_gre is compatible with Cisco GRE tunnels, so you can
     43  * have a NetBSD box as the other end of a tunnel interface of a Cisco
     44  * router. See gre(4) for more details.
     45  * Also supported:  IP in IP encaps (proto 55) as of RFC 2004
     46  */
     47 
     48 #include "gre.h"
     49 #if NGRE > 0
     50 
     51 #include "opt_inet.h"
     52 #include "opt_ns.h"
     53 #include "bpfilter.h"
     54 
     55 #include <sys/param.h>
     56 #include <sys/malloc.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/proc.h>
     59 #include <sys/protosw.h>
     60 #include <sys/socket.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/queue.h>
     63 #if __NetBSD__
     64 #include <sys/systm.h>
     65 #endif
     66 
     67 #include <machine/cpu.h>
     68 
     69 #include <net/ethertypes.h>
     70 #include <net/if.h>
     71 #include <net/if_types.h>
     72 #include <net/netisr.h>
     73 #include <net/route.h>
     74 
     75 #ifdef INET
     76 #include <netinet/in.h>
     77 #include <netinet/in_systm.h>
     78 #include <netinet/in_var.h>
     79 #include <netinet/ip.h>
     80 #include <netinet/ip_var.h>
     81 #else
     82 #error "Huh? if_gre without inet?"
     83 #endif
     84 
     85 #ifdef NS
     86 #include <netns/ns.h>
     87 #include <netns/ns_if.h>
     88 #endif
     89 
     90 #ifdef NETATALK
     91 #include <netatalk/at.h>
     92 #include <netatalk/at_var.h>
     93 #include <netatalk/at_extern.h>
     94 #endif
     95 
     96 #if NBPFILTER > 0
     97 #include <sys/time.h>
     98 #include <net/bpf.h>
     99 #endif
    100 
    101 #include <net/if_gre.h>
    102 
    103 /*
    104  * XXX this is below the standard MTU of
    105  * 1500 Bytes, allowing for headers,
    106  * but we should possibly do path mtu discovery
    107  * before changing if state to up to find the
    108  * correct value
    109  */
    110 #define GREMTU 1450
    111 #define LINK_MASK (IFF_LINK0|IFF_LINK1|IFF_LINK2)
    112 
    113 struct gre_softc_head gre_softc_list;
    114 
    115 int	gre_clone_create __P((struct if_clone *, int));
    116 void	gre_clone_destroy __P((struct ifnet *));
    117 
    118 struct if_clone gre_cloner =
    119     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    120 
    121 void gre_compute_route(struct gre_softc *sc);
    122 
    123 void	greattach __P((int));
    124 
    125 /* ARGSUSED */
    126 void
    127 greattach(count)
    128 	int count;
    129 {
    130 
    131 	LIST_INIT(&gre_softc_list);
    132 	if_clone_attach(&gre_cloner);
    133 }
    134 
    135 int
    136 gre_clone_create(ifc, unit)
    137 	struct if_clone *ifc;
    138 	int unit;
    139 {
    140 	struct gre_softc *sc;
    141 
    142 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
    143 	memset(sc, 0, sizeof(struct gre_softc));
    144 
    145 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    146 	sc->sc_if.if_softc = sc;
    147 	sc->sc_if.if_type =  IFT_OTHER;
    148 	sc->sc_if.if_addrlen = 4;
    149 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
    150 	sc->sc_if.if_dlt = DLT_NULL;
    151 	sc->sc_if.if_mtu = GREMTU;
    152 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    153 	sc->sc_if.if_output = gre_output;
    154 	sc->sc_if.if_ioctl = gre_ioctl;
    155 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
    156 	sc->g_proto = IPPROTO_GRE;
    157 	if_attach(&sc->sc_if);
    158 	if_alloc_sadl(&sc->sc_if);
    159 #if NBPFILTER > 0
    160 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
    161 #endif
    162 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
    163 	return (0);
    164 }
    165 
    166 void
    167 gre_clone_destroy(ifp)
    168 	struct ifnet *ifp;
    169 {
    170 	struct gre_softc *sc = ifp->if_softc;
    171 
    172 	LIST_REMOVE(sc, sc_list);
    173 #if NBPFILTER > 0
    174 	bpfdetach(ifp);
    175 #endif
    176 	if_detach(ifp);
    177 	free(sc, M_DEVBUF);
    178 }
    179 
    180 /*
    181  * The output routine. Takes a packet and encapsulates it in the protocol
    182  * given by sc->g_proto. See also RFC 1701 and RFC 2004
    183  */
    184 int
    185 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    186 	   struct rtentry *rt)
    187 {
    188 	int error = 0;
    189 	struct gre_softc *sc = ifp->if_softc;
    190 	struct greip *gh;
    191 	struct ip *inp;
    192 	u_char ttl, osrc;
    193 	u_short etype = 0;
    194 	struct mobile_h mob_h;
    195 
    196 	gh = NULL;
    197 	inp = NULL;
    198 	osrc = 0;
    199 
    200 #if NBPFILTER >0
    201 	if (ifp->if_bpf) {
    202 		/* see comment of other if_foo.c files */
    203 		struct mbuf m0;
    204 		u_int32_t af = dst->sa_family;
    205 
    206 		m0.m_next = m;
    207 		m0.m_len = 4;
    208 		m0.m_data = (char *)&af;
    209 
    210 		bpf_mtap(ifp->if_bpf, &m0);
    211 	}
    212 #endif
    213 
    214 	ttl = 255;
    215 
    216 	if (sc->g_proto == IPPROTO_MOBILE) {
    217 		if (dst->sa_family == AF_INET) {
    218 			struct mbuf *m0;
    219 			int msiz;
    220 
    221 			inp = mtod(m, struct ip *);
    222 
    223 			memset(&mob_h, 0, MOB_H_SIZ_L);
    224 			mob_h.proto = (inp->ip_p) << 8;
    225 			mob_h.odst = inp->ip_dst.s_addr;
    226 			inp->ip_dst.s_addr = sc->g_dst.s_addr;
    227 
    228 			/*
    229 			 * If the packet comes from our host, we only change
    230 			 * the destination address in the IP header.
    231 			 * Else we also need to save and change the source
    232 			 */
    233 			if (in_hosteq(inp->ip_src, sc->g_src)) {
    234 				msiz = MOB_H_SIZ_S;
    235 			} else {
    236 				mob_h.proto |= MOB_H_SBIT;
    237 				mob_h.osrc = inp->ip_src.s_addr;
    238 				inp->ip_src.s_addr = sc->g_src.s_addr;
    239 				msiz = MOB_H_SIZ_L;
    240 			}
    241 			HTONS(mob_h.proto);
    242 			mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
    243 
    244 			if ((m->m_data - msiz) < m->m_pktdat) {
    245 				/* need new mbuf */
    246 				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
    247 				if (m0 == NULL) {
    248 					IF_DROP(&ifp->if_snd);
    249 					m_freem(m);
    250 					return (ENOBUFS);
    251 				}
    252 				m0->m_next = m;
    253 				m->m_data += sizeof(struct ip);
    254 				m->m_len -= sizeof(struct ip);
    255 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
    256 				m0->m_len = msiz + sizeof(struct ip);
    257 				m0->m_data += max_linkhdr;
    258 				memcpy(mtod(m0, caddr_t), (caddr_t)inp,
    259 				       sizeof(struct ip));
    260 				m = m0;
    261 			} else {  /* we have some space left in the old one */
    262 				m->m_data -= msiz;
    263 				m->m_len += msiz;
    264 				m->m_pkthdr.len += msiz;
    265 				memmove(mtod(m, caddr_t), inp,
    266 					sizeof(struct ip));
    267 			}
    268 			inp=mtod(m, struct ip *);
    269 			memcpy((caddr_t)(inp + 1), &mob_h, (unsigned)msiz);
    270 			NTOHS(inp->ip_len);
    271 			inp->ip_len += msiz;
    272 		} else {  /* AF_INET */
    273 			IF_DROP(&ifp->if_snd);
    274 			m_freem(m);
    275 			return (EINVAL);
    276 		}
    277 	} else if (sc->g_proto == IPPROTO_GRE) {
    278 		switch (dst->sa_family) {
    279 		case AF_INET:
    280 			inp = mtod(m, struct ip *);
    281 			ttl = inp->ip_ttl;
    282 			etype = ETHERTYPE_IP;
    283 			break;
    284 #ifdef NETATALK
    285 		case AF_APPLETALK:
    286 			etype = ETHERTYPE_ATALK;
    287 			break;
    288 #endif
    289 #ifdef NS
    290 		case AF_NS:
    291 			etype = ETHERTYPE_NS;
    292 			break;
    293 #endif
    294 		default:
    295 			IF_DROP(&ifp->if_snd);
    296 			m_freem(m);
    297 			return (EAFNOSUPPORT);
    298 		}
    299 		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
    300 	} else {
    301 		error = EINVAL;
    302 		IF_DROP(&ifp->if_snd);
    303 		m_freem(m);
    304 		return (error);
    305 	}
    306 
    307 	if (m == NULL) {
    308 		IF_DROP(&ifp->if_snd);
    309 		return (ENOBUFS);
    310 	}
    311 
    312 	gh = mtod(m, struct greip *);
    313 	if (sc->g_proto == IPPROTO_GRE) {
    314 		/* we don't have any GRE flags for now */
    315 
    316 		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
    317 		gh->gi_ptype = htons(etype);
    318 	}
    319 
    320 	gh->gi_pr = sc->g_proto;
    321 	if (sc->g_proto != IPPROTO_MOBILE) {
    322 		gh->gi_src = sc->g_src;
    323 		gh->gi_dst = sc->g_dst;
    324 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
    325 		((struct ip*)gh)->ip_ttl = ttl;
    326 		((struct ip*)gh)->ip_tos = inp->ip_tos;
    327 		gh->gi_len = m->m_pkthdr.len;
    328 	}
    329 
    330 	ifp->if_opackets++;
    331 	ifp->if_obytes += m->m_pkthdr.len;
    332 	/* send it off */
    333 	error = ip_output(m, NULL, &sc->route, 0, NULL);
    334 	if (error)
    335 		ifp->if_oerrors++;
    336 	return (error);
    337 }
    338 
    339 int
    340 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    341 {
    342 	struct proc *p = curproc;	/* XXX */
    343 	struct ifaddr *ifa = (struct ifaddr *)data;
    344 	struct ifreq *ifr = (struct ifreq *)data;
    345 	struct in_ifaddr *ia = (struct in_ifaddr *)data;
    346 	struct gre_softc *sc = ifp->if_softc;
    347 	int s;
    348 	struct sockaddr_in si;
    349 	struct sockaddr *sa = NULL;
    350 	int error;
    351 
    352 	error = 0;
    353 
    354 	s = splnet();
    355 	switch (cmd) {
    356 	case SIOCSIFADDR:
    357 	case SIOCSIFDSTADDR:
    358 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    359 			break;
    360 		/*
    361 		 * set tunnel endpoints in case that we "only"
    362 		 * have ip over ip encapsulation. This allows to
    363 		 * set tunnel endpoints with ifconfig.
    364 		 */
    365 		if (ifa->ifa_addr->sa_family == AF_INET) {
    366 			sa = ifa->ifa_addr;
    367 			sc->g_src = (satosin(sa))->sin_addr;
    368 			sc->g_dst = ia->ia_dstaddr.sin_addr;
    369 			if ((sc->g_src.s_addr != INADDR_ANY) &&
    370 			    (sc->g_dst.s_addr != INADDR_ANY)) {
    371 				if (sc->route.ro_rt != 0) /* free old route */
    372 					RTFREE(sc->route.ro_rt);
    373 				gre_compute_route(sc);
    374 				ifp->if_flags |= IFF_UP;
    375 			}
    376 		}
    377 		break;
    378 	case SIOCSIFFLAGS:
    379 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    380 			break;
    381 		if ((sc->g_dst.s_addr == INADDR_ANY) ||
    382 		    (sc->g_src.s_addr == INADDR_ANY))
    383 			ifp->if_flags &= ~IFF_UP;
    384 
    385 		switch (ifr->ifr_flags & LINK_MASK) {
    386 			case IFF_LINK0:
    387 				sc->g_proto = IPPROTO_GRE;
    388 				ifp->if_flags |= IFF_LINK0;
    389 				ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
    390 				break;
    391 			case IFF_LINK2:
    392 				sc->g_proto = IPPROTO_MOBILE;
    393 				ifp->if_flags |= IFF_LINK2;
    394 				ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1);
    395 				break;
    396 		}
    397 		break;
    398 	case SIOCSIFMTU:
    399 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    400 			break;
    401 		if (ifr->ifr_mtu > GREMTU || ifr->ifr_mtu < 576) {
    402 			error = EINVAL;
    403 			break;
    404 		}
    405 		ifp->if_mtu = ifr->ifr_mtu;
    406 		break;
    407 	case SIOCGIFMTU:
    408 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    409 		break;
    410 	case SIOCADDMULTI:
    411 	case SIOCDELMULTI:
    412 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    413 			break;
    414 		if (ifr == 0) {
    415 			error = EAFNOSUPPORT;
    416 			break;
    417 		}
    418 		switch (ifr->ifr_addr.sa_family) {
    419 #ifdef INET
    420 		case AF_INET:
    421 			break;
    422 #endif
    423 		default:
    424 			error = EAFNOSUPPORT;
    425 			break;
    426 		}
    427 		break;
    428 	case GRESPROTO:
    429 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    430 			break;
    431 		sc->g_proto = ifr->ifr_flags;
    432 		switch (sc->g_proto) {
    433 		case IPPROTO_GRE :
    434 			ifp->if_flags |= IFF_LINK0;
    435 			ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
    436 			break;
    437 		case IPPROTO_MOBILE :
    438 			ifp->if_flags |= IFF_LINK2;
    439 			ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
    440 			break;
    441 		default:
    442 			ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2);
    443 		}
    444 		break;
    445 	case GREGPROTO:
    446 		ifr->ifr_flags = sc->g_proto;
    447 		break;
    448 	case GRESADDRS:
    449 	case GRESADDRD:
    450 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    451 			break;
    452 		/*
    453 		 * set tunnel endpoints, compute a less specific route
    454 		 * to the remote end and mark if as up
    455 		 */
    456 		sa = &ifr->ifr_addr;
    457 		if (cmd == GRESADDRS )
    458 			sc->g_src = (satosin(sa))->sin_addr;
    459 		if (cmd == GRESADDRD )
    460 			sc->g_dst = (satosin(sa))->sin_addr;
    461 		if ((sc->g_src.s_addr != INADDR_ANY) &&
    462 		    (sc->g_dst.s_addr != INADDR_ANY)) {
    463 			if (sc->route.ro_rt != 0) /* free old route */
    464 				RTFREE(sc->route.ro_rt);
    465 			gre_compute_route(sc);
    466 			ifp->if_flags |= IFF_UP;
    467 		}
    468 		break;
    469 	case GREGADDRS:
    470 		si.sin_addr.s_addr = sc->g_src.s_addr;
    471 		sa = sintosa(&si);
    472 		ifr->ifr_addr = *sa;
    473 		break;
    474 	case GREGADDRD:
    475 		si.sin_addr.s_addr = sc->g_dst.s_addr;
    476 		sa = sintosa(&si);
    477 		ifr->ifr_addr = *sa;
    478 		break;
    479 	default:
    480 		error = EINVAL;
    481 	}
    482 
    483 	splx(s);
    484 	return (error);
    485 }
    486 
    487 /*
    488  * computes a route to our destination that is not the one
    489  * which would be taken by ip_output(), as this one will loop back to
    490  * us. If the interface is p2p as  a--->b, then a routing entry exists
    491  * If we now send a packet to b (e.g. ping b), this will come down here
    492  * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
    493  * if_gre.
    494  * Goal here is to compute a route to b that is less specific than
    495  * a-->b. We know that this one exists as in normal operation we have
    496  * at least a default route which matches.
    497  */
    498 void
    499 gre_compute_route(struct gre_softc *sc)
    500 {
    501 	struct route *ro;
    502 	u_int32_t a, b, c;
    503 
    504 	ro = &sc->route;
    505 
    506 	memset(ro, 0, sizeof(struct route));
    507 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    508 	ro->ro_dst.sa_family = AF_INET;
    509 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
    510 
    511 	/*
    512 	 * toggle last bit, so our interface is not found, but a less
    513 	 * specific route. I'd rather like to specify a shorter mask,
    514 	 * but this is not possible. Should work though. XXX
    515 	 * there is a simpler way ...
    516 	 */
    517 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
    518 		a = ntohl(sc->g_dst.s_addr);
    519 		b = a & 0x01;
    520 		c = a & 0xfffffffe;
    521 		b = b ^ 0x01;
    522 		a = b | c;
    523 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
    524 		    = htonl(a);
    525 	}
    526 
    527 #ifdef DIAGNOSTIC
    528 	printf("%s: searching a route to %s", sc->sc_if.if_xname,
    529 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
    530 #endif
    531 
    532 	rtalloc(ro);
    533 
    534 	/*
    535 	 * now change it back - else ip_output will just drop
    536 	 * the route and search one to this interface ...
    537 	 */
    538 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
    539 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    540 
    541 #ifdef DIAGNOSTIC
    542 	printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
    543 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
    544 	printf("\n");
    545 #endif
    546 }
    547 
    548 /*
    549  * do a checksum of a buffer - much like in_cksum, which operates on
    550  * mbufs.
    551  */
    552 u_short
    553 gre_in_cksum(u_short *p, u_int len)
    554 {
    555 	u_int sum = 0;
    556 	int nwords = len >> 1;
    557 
    558 	while (nwords-- != 0)
    559 		sum += *p++;
    560 
    561 	if (len & 1) {
    562 		union {
    563 			u_short w;
    564 			u_char c[2];
    565 		} u;
    566 		u.c[0] = *(u_char *)p;
    567 		u.c[1] = 0;
    568 		sum += u.w;
    569 	}
    570 
    571 	/* end-around-carry */
    572 	sum = (sum >> 16) + (sum & 0xffff);
    573 	sum += (sum >> 16);
    574 	return (~sum);
    575 }
    576 #endif
    577