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