Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.33
      1 /*	$NetBSD: if_gre.c,v 1.33 2002/06/09 17:32:54 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 <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: if_gre.c,v 1.33 2002/06/09 17:32:54 itojun Exp $");
     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  * It is not easy to calculate the right value for a GRE MTU.
    105  * We leave this task to the admin and use the same default that
    106  * other vendors use.
    107  */
    108 #define GREMTU 1476
    109 #define LINK_MASK (IFF_LINK0|IFF_LINK1|IFF_LINK2)
    110 
    111 struct gre_softc_head gre_softc_list;
    112 int ip_gre_ttl = GRE_TTL;
    113 
    114 int	gre_clone_create __P((struct if_clone *, int));
    115 void	gre_clone_destroy __P((struct ifnet *));
    116 
    117 struct if_clone gre_cloner =
    118     IF_CLONE_INITIALIZER("gre", gre_clone_create, gre_clone_destroy);
    119 
    120 int gre_compute_route(struct gre_softc *sc);
    121 
    122 void	greattach __P((int));
    123 
    124 /* ARGSUSED */
    125 void
    126 greattach(count)
    127 	int count;
    128 {
    129 
    130 	LIST_INIT(&gre_softc_list);
    131 	if_clone_attach(&gre_cloner);
    132 }
    133 
    134 int
    135 gre_clone_create(ifc, unit)
    136 	struct if_clone *ifc;
    137 	int unit;
    138 {
    139 	struct gre_softc *sc;
    140 
    141 	sc = malloc(sizeof(struct gre_softc), M_DEVBUF, M_WAITOK);
    142 	memset(sc, 0, sizeof(struct gre_softc));
    143 
    144 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    145 	sc->sc_if.if_softc = sc;
    146 	sc->sc_if.if_type = IFT_OTHER;
    147 	sc->sc_if.if_addrlen = 4;
    148 	sc->sc_if.if_hdrlen = 24; /* IP + GRE */
    149 	sc->sc_if.if_dlt = DLT_NULL;
    150 	sc->sc_if.if_mtu = GREMTU;
    151 	sc->sc_if.if_flags = IFF_POINTOPOINT|IFF_MULTICAST;
    152 	sc->sc_if.if_output = gre_output;
    153 	sc->sc_if.if_ioctl = gre_ioctl;
    154 	sc->g_dst.s_addr = sc->g_src.s_addr = INADDR_ANY;
    155 	sc->g_proto = IPPROTO_GRE;
    156 	if_attach(&sc->sc_if);
    157 	if_alloc_sadl(&sc->sc_if);
    158 #if NBPFILTER > 0
    159 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int32_t));
    160 #endif
    161 	LIST_INSERT_HEAD(&gre_softc_list, sc, sc_list);
    162 	return (0);
    163 }
    164 
    165 void
    166 gre_clone_destroy(ifp)
    167 	struct ifnet *ifp;
    168 {
    169 	struct gre_softc *sc = ifp->if_softc;
    170 
    171 	LIST_REMOVE(sc, sc_list);
    172 #if NBPFILTER > 0
    173 	bpfdetach(ifp);
    174 #endif
    175 	if_detach(ifp);
    176 	free(sc, M_DEVBUF);
    177 }
    178 
    179 /*
    180  * The output routine. Takes a packet and encapsulates it in the protocol
    181  * given by sc->g_proto. See also RFC 1701 and RFC 2004
    182  */
    183 int
    184 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    185 	   struct rtentry *rt)
    186 {
    187 	int error = 0;
    188 	struct gre_softc *sc = ifp->if_softc;
    189 	struct greip *gh;
    190 	struct ip *inp;
    191 	u_char osrc;
    192 	u_short etype = 0;
    193 	struct mobile_h mob_h;
    194 
    195 	if ((ifp->if_flags & IFF_UP) == 0)
    196 		return ENETDOWN;
    197 
    198 	gh = NULL;
    199 	inp = NULL;
    200 	osrc = 0;
    201 
    202 #if NBPFILTER >0
    203 	if (ifp->if_bpf) {
    204 		/* see comment of other if_foo.c files */
    205 		struct mbuf m0;
    206 		u_int32_t af = dst->sa_family;
    207 
    208 		m0.m_next = m;
    209 		m0.m_len = 4;
    210 		m0.m_data = (char *)&af;
    211 
    212 		bpf_mtap(ifp->if_bpf, &m0);
    213 	}
    214 #endif
    215 
    216 	m->m_flags &= ~(M_BCAST|M_MCAST);
    217 
    218 	if (sc->g_proto == IPPROTO_MOBILE) {
    219 		if (dst->sa_family == AF_INET) {
    220 			struct mbuf *m0;
    221 			int msiz;
    222 
    223 			inp = mtod(m, struct ip *);
    224 
    225 			memset(&mob_h, 0, MOB_H_SIZ_L);
    226 			mob_h.proto = (inp->ip_p) << 8;
    227 			mob_h.odst = inp->ip_dst.s_addr;
    228 			inp->ip_dst.s_addr = sc->g_dst.s_addr;
    229 
    230 			/*
    231 			 * If the packet comes from our host, we only change
    232 			 * the destination address in the IP header.
    233 			 * Else we also need to save and change the source
    234 			 */
    235 			if (in_hosteq(inp->ip_src, sc->g_src)) {
    236 				msiz = MOB_H_SIZ_S;
    237 			} else {
    238 				mob_h.proto |= MOB_H_SBIT;
    239 				mob_h.osrc = inp->ip_src.s_addr;
    240 				inp->ip_src.s_addr = sc->g_src.s_addr;
    241 				msiz = MOB_H_SIZ_L;
    242 			}
    243 			HTONS(mob_h.proto);
    244 			mob_h.hcrc = gre_in_cksum((u_short *)&mob_h, msiz);
    245 
    246 			if ((m->m_data - msiz) < m->m_pktdat) {
    247 				/* need new mbuf */
    248 				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
    249 				if (m0 == NULL) {
    250 					IF_DROP(&ifp->if_snd);
    251 					m_freem(m);
    252 					return (ENOBUFS);
    253 				}
    254 				m0->m_next = m;
    255 				m->m_data += sizeof(struct ip);
    256 				m->m_len -= sizeof(struct ip);
    257 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
    258 				m0->m_len = msiz + sizeof(struct ip);
    259 				m0->m_data += max_linkhdr;
    260 				memcpy(mtod(m0, caddr_t), (caddr_t)inp,
    261 				       sizeof(struct ip));
    262 				m = m0;
    263 			} else {  /* we have some space left in the old one */
    264 				m->m_data -= msiz;
    265 				m->m_len += msiz;
    266 				m->m_pkthdr.len += msiz;
    267 				memmove(mtod(m, caddr_t), inp,
    268 					sizeof(struct ip));
    269 			}
    270 			inp=mtod(m, struct ip *);
    271 			memcpy((caddr_t)(inp + 1), &mob_h, (unsigned)msiz);
    272 			NTOHS(inp->ip_len);
    273 			inp->ip_len += msiz;
    274 		} else {  /* AF_INET */
    275 			IF_DROP(&ifp->if_snd);
    276 			m_freem(m);
    277 			return (EINVAL);
    278 		}
    279 	} else if (sc->g_proto == IPPROTO_GRE) {
    280 		switch (dst->sa_family) {
    281 		case AF_INET:
    282 			inp = mtod(m, struct ip *);
    283 			etype = ETHERTYPE_IP;
    284 			break;
    285 #ifdef NETATALK
    286 		case AF_APPLETALK:
    287 			etype = ETHERTYPE_ATALK;
    288 			break;
    289 #endif
    290 #ifdef NS
    291 		case AF_NS:
    292 			etype = ETHERTYPE_NS;
    293 			break;
    294 #endif
    295 		default:
    296 			IF_DROP(&ifp->if_snd);
    297 			m_freem(m);
    298 			return (EAFNOSUPPORT);
    299 		}
    300 		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
    301 	} else {
    302 		error = EINVAL;
    303 		IF_DROP(&ifp->if_snd);
    304 		m_freem(m);
    305 		return (error);
    306 	}
    307 
    308 	if (m == NULL) {
    309 		IF_DROP(&ifp->if_snd);
    310 		return (ENOBUFS);
    311 	}
    312 
    313 	gh = mtod(m, struct greip *);
    314 	if (sc->g_proto == IPPROTO_GRE) {
    315 		/* we don't have any GRE flags for now */
    316 
    317 		memset((void *)&gh->gi_g, 0, sizeof(struct gre_h));
    318 		gh->gi_ptype = htons(etype);
    319 	}
    320 
    321 	gh->gi_pr = sc->g_proto;
    322 	if (sc->g_proto != IPPROTO_MOBILE) {
    323 		gh->gi_src = sc->g_src;
    324 		gh->gi_dst = sc->g_dst;
    325 		((struct ip*)gh)->ip_hl = (sizeof(struct ip)) >> 2;
    326 		((struct ip*)gh)->ip_ttl = ip_gre_ttl;
    327 		((struct ip*)gh)->ip_tos = inp->ip_tos;
    328 		gh->gi_len = m->m_pkthdr.len;
    329 	}
    330 
    331 	ifp->if_opackets++;
    332 	ifp->if_obytes += m->m_pkthdr.len;
    333 	/* send it off */
    334 	error = ip_output(m, NULL, &sc->route, 0, NULL);
    335 	if (error)
    336 		ifp->if_oerrors++;
    337 	return (error);
    338 }
    339 
    340 int
    341 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    342 {
    343 	struct proc *p = curproc;	/* XXX */
    344 	struct ifaddr *ifa = (struct ifaddr *)data;
    345 	struct ifreq *ifr = (struct ifreq *)data;
    346 	struct in_ifaddr *ia = (struct in_ifaddr *)data;
    347 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    348 	struct gre_softc *sc = ifp->if_softc;
    349 	int s;
    350 	struct sockaddr_in si;
    351 	struct sockaddr *sa = NULL;
    352 	int error;
    353 
    354 	error = 0;
    355 
    356 	s = splnet();
    357 	switch (cmd) {
    358 	case SIOCSIFADDR:
    359 	case SIOCSIFDSTADDR:
    360 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    361 			break;
    362 		/*
    363 		 * set tunnel endpoints in case that we "only"
    364 		 * have ip over ip encapsulation. This allows to
    365 		 * set tunnel endpoints with ifconfig.
    366 		 */
    367 		if (ifa->ifa_addr->sa_family == AF_INET) {
    368 			sa = ifa->ifa_addr;
    369 			sc->g_src = (satosin(sa))->sin_addr;
    370 			sc->g_dst = ia->ia_dstaddr.sin_addr;
    371 			if ((sc->g_src.s_addr != INADDR_ANY) &&
    372 			    (sc->g_dst.s_addr != INADDR_ANY)) {
    373 				if (sc->route.ro_rt != 0) /* free old route */
    374 					RTFREE(sc->route.ro_rt);
    375 				if (gre_compute_route(sc) == 0)
    376 					ifp->if_flags |= IFF_UP;
    377 			}
    378 		}
    379 		break;
    380 	case SIOCSIFFLAGS:
    381 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    382 			break;
    383 		if ((sc->g_dst.s_addr == INADDR_ANY) ||
    384 		    (sc->g_src.s_addr == INADDR_ANY))
    385 			ifp->if_flags &= ~IFF_UP;
    386 
    387 		switch (ifr->ifr_flags & LINK_MASK) {
    388 			case IFF_LINK0:
    389 				sc->g_proto = IPPROTO_GRE;
    390 				ifp->if_flags |= IFF_LINK0;
    391 				ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
    392 				break;
    393 			case IFF_LINK2:
    394 				sc->g_proto = IPPROTO_MOBILE;
    395 				ifp->if_flags |= IFF_LINK2;
    396 				ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1);
    397 				break;
    398 		}
    399 		break;
    400 	case SIOCSIFMTU:
    401 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    402 			break;
    403 		if (ifr->ifr_mtu < 576) {
    404 			error = EINVAL;
    405 			break;
    406 		}
    407 		ifp->if_mtu = ifr->ifr_mtu;
    408 		break;
    409 	case SIOCGIFMTU:
    410 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    411 		break;
    412 	case SIOCADDMULTI:
    413 	case SIOCDELMULTI:
    414 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    415 			break;
    416 		if (ifr == 0) {
    417 			error = EAFNOSUPPORT;
    418 			break;
    419 		}
    420 		switch (ifr->ifr_addr.sa_family) {
    421 #ifdef INET
    422 		case AF_INET:
    423 			break;
    424 #endif
    425 		default:
    426 			error = EAFNOSUPPORT;
    427 			break;
    428 		}
    429 		break;
    430 	case GRESPROTO:
    431 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    432 			break;
    433 		sc->g_proto = ifr->ifr_flags;
    434 		switch (sc->g_proto) {
    435 		case IPPROTO_GRE :
    436 			ifp->if_flags |= IFF_LINK0;
    437 			ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
    438 			break;
    439 		case IPPROTO_MOBILE :
    440 			ifp->if_flags |= IFF_LINK2;
    441 			ifp->if_flags &= ~(IFF_LINK1|IFF_LINK2);
    442 			break;
    443 		default:
    444 			ifp->if_flags &= ~(IFF_LINK0|IFF_LINK1|IFF_LINK2);
    445 		}
    446 		break;
    447 	case GREGPROTO:
    448 		ifr->ifr_flags = sc->g_proto;
    449 		break;
    450 	case GRESADDRS:
    451 	case GRESADDRD:
    452 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    453 			break;
    454 		/*
    455 		 * set tunnel endpoints, compute a less specific route
    456 		 * to the remote end and mark if as up
    457 		 */
    458 		sa = &ifr->ifr_addr;
    459 		if (cmd == GRESADDRS)
    460 			sc->g_src = (satosin(sa))->sin_addr;
    461 		if (cmd == GRESADDRD)
    462 			sc->g_dst = (satosin(sa))->sin_addr;
    463 	recompute:
    464 		if ((sc->g_src.s_addr != INADDR_ANY) &&
    465 		    (sc->g_dst.s_addr != INADDR_ANY)) {
    466 			if (sc->route.ro_rt != 0) /* free old route */
    467 				RTFREE(sc->route.ro_rt);
    468 			if (gre_compute_route(sc) == 0)
    469 				ifp->if_flags |= IFF_UP;
    470 		}
    471 		break;
    472 	case GREGADDRS:
    473 		memset(&si, 0, sizeof(si));
    474 		si.sin_family = AF_INET;
    475 		si.sin_len = sizeof(struct sockaddr_in);
    476 		si.sin_addr.s_addr = sc->g_src.s_addr;
    477 		sa = sintosa(&si);
    478 		ifr->ifr_addr = *sa;
    479 		break;
    480 	case GREGADDRD:
    481 		memset(&si, 0, sizeof(si));
    482 		si.sin_family = AF_INET;
    483 		si.sin_len = sizeof(struct sockaddr_in);
    484 		si.sin_addr.s_addr = sc->g_dst.s_addr;
    485 		sa = sintosa(&si);
    486 		ifr->ifr_addr = *sa;
    487 		break;
    488 	case SIOCSLIFPHYADDR:
    489 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    490 			break;
    491 		if (lifr->addr.ss_family != AF_INET ||
    492 		    lifr->dstaddr.ss_family != AF_INET) {
    493 			error = EAFNOSUPPORT;
    494 			break;
    495 		}
    496 		if (lifr->addr.ss_len != sizeof(si) ||
    497 		    lifr->dstaddr.ss_len != sizeof(si)) {
    498 			error = EINVAL;
    499 			break;
    500 		}
    501 		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
    502 		sc->g_dst =
    503 		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
    504 		goto recompute;
    505 	case SIOCDIFPHYADDR:
    506 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    507 			break;
    508 		sc->g_src.s_addr = INADDR_ANY;
    509 		sc->g_dst.s_addr = INADDR_ANY;
    510 		break;
    511 	case SIOCGLIFPHYADDR:
    512 		if (sc->g_src.s_addr == INADDR_ANY ||
    513 		    sc->g_dst.s_addr == INADDR_ANY) {
    514 			error = EADDRNOTAVAIL;
    515 			break;
    516 		}
    517 		memset(&si, 0, sizeof(si));
    518 		si.sin_family = AF_INET;
    519 		si.sin_len = sizeof(struct sockaddr_in);
    520 		si.sin_addr.s_addr = sc->g_src.s_addr;
    521 		memcpy(&lifr->addr, &si, sizeof(si));
    522 		si.sin_addr.s_addr = sc->g_dst.s_addr;
    523 		memcpy(&lifr->dstaddr, &si, sizeof(si));
    524 		break;
    525 	default:
    526 		error = EINVAL;
    527 		break;
    528 	}
    529 
    530 	splx(s);
    531 	return (error);
    532 }
    533 
    534 /*
    535  * computes a route to our destination that is not the one
    536  * which would be taken by ip_output(), as this one will loop back to
    537  * us. If the interface is p2p as  a--->b, then a routing entry exists
    538  * If we now send a packet to b (e.g. ping b), this will come down here
    539  * gets src=a, dst=b tacked on and would from ip_ouput() sent back to
    540  * if_gre.
    541  * Goal here is to compute a route to b that is less specific than
    542  * a-->b. We know that this one exists as in normal operation we have
    543  * at least a default route which matches.
    544  */
    545 int
    546 gre_compute_route(struct gre_softc *sc)
    547 {
    548 	struct route *ro;
    549 	u_int32_t a, b, c;
    550 
    551 	ro = &sc->route;
    552 
    553 	memset(ro, 0, sizeof(struct route));
    554 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    555 	ro->ro_dst.sa_family = AF_INET;
    556 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
    557 
    558 	/*
    559 	 * toggle last bit, so our interface is not found, but a less
    560 	 * specific route. I'd rather like to specify a shorter mask,
    561 	 * but this is not possible. Should work though. XXX
    562 	 * there is a simpler way ...
    563 	 */
    564 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
    565 		a = ntohl(sc->g_dst.s_addr);
    566 		b = a & 0x01;
    567 		c = a & 0xfffffffe;
    568 		b = b ^ 0x01;
    569 		a = b | c;
    570 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
    571 		    = htonl(a);
    572 	}
    573 
    574 #ifdef DIAGNOSTIC
    575 	printf("%s: searching a route to %s", sc->sc_if.if_xname,
    576 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
    577 #endif
    578 
    579 	rtalloc(ro);
    580 
    581 	/*
    582 	 * check if this returned a route at all and this route is no
    583 	 * recursion to ourself
    584 	 */
    585 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
    586 #ifdef DIAGNOSTIC
    587 		if (ro->ro_rt == NULL)
    588 			printf(" - no route found!\n");
    589 		else
    590 			printf(" - route loops back to ourself!\n");
    591 #endif
    592 		return EADDRNOTAVAIL;
    593 	}
    594 
    595 	/*
    596 	 * now change it back - else ip_output will just drop
    597 	 * the route and search one to this interface ...
    598 	 */
    599 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
    600 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    601 
    602 #ifdef DIAGNOSTIC
    603 	printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
    604 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
    605 	printf("\n");
    606 #endif
    607 
    608 	return 0;
    609 }
    610 
    611 /*
    612  * do a checksum of a buffer - much like in_cksum, which operates on
    613  * mbufs.
    614  */
    615 u_short
    616 gre_in_cksum(u_short *p, u_int len)
    617 {
    618 	u_int sum = 0;
    619 	int nwords = len >> 1;
    620 
    621 	while (nwords-- != 0)
    622 		sum += *p++;
    623 
    624 	if (len & 1) {
    625 		union {
    626 			u_short w;
    627 			u_char c[2];
    628 		} u;
    629 		u.c[0] = *(u_char *)p;
    630 		u.c[1] = 0;
    631 		sum += u.w;
    632 	}
    633 
    634 	/* end-around-carry */
    635 	sum = (sum >> 16) + (sum & 0xffff);
    636 	sum += (sum >> 16);
    637 	return (~sum);
    638 }
    639