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