Home | History | Annotate | Line # | Download | only in net
if_gre.c revision 1.53
      1 /*	$NetBSD: if_gre.c,v 1.53 2004/12/04 18:31:43 peter 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.53 2004/12/04 18:31:43 peter 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 int	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 	snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
    144 	    ifc->ifc_name, unit);
    145 	sc->sc_if.if_softc = sc;
    146 	sc->sc_if.if_type = IFT_TUNNEL;
    147 	sc->sc_if.if_addrlen = 0;
    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 	sc->sc_if.if_flags |= IFF_LINK0;
    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 int
    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 	return (0);
    180 }
    181 
    182 /*
    183  * The output routine. Takes a packet and encapsulates it in the protocol
    184  * given by sc->g_proto. See also RFC 1701 and RFC 2004
    185  */
    186 int
    187 gre_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
    188 	   struct rtentry *rt)
    189 {
    190 	int error = 0;
    191 	struct gre_softc *sc = ifp->if_softc;
    192 	struct greip *gh;
    193 	struct ip *ip;
    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 		bpf_mtap_af(ifp->if_bpf, dst->sa_family, m);
    210 #endif
    211 
    212 	m->m_flags &= ~(M_BCAST|M_MCAST);
    213 
    214 	if (sc->g_proto == IPPROTO_MOBILE) {
    215 		if (dst->sa_family == AF_INET) {
    216 			struct mbuf *m0;
    217 			int msiz;
    218 
    219 			ip = mtod(m, struct ip *);
    220 
    221 			memset(&mob_h, 0, MOB_H_SIZ_L);
    222 			mob_h.proto = (ip->ip_p) << 8;
    223 			mob_h.odst = ip->ip_dst.s_addr;
    224 			ip->ip_dst.s_addr = sc->g_dst.s_addr;
    225 
    226 			/*
    227 			 * If the packet comes from our host, we only change
    228 			 * the destination address in the IP header.
    229 			 * Else we also need to save and change the source
    230 			 */
    231 			if (in_hosteq(ip->ip_src, sc->g_src)) {
    232 				msiz = MOB_H_SIZ_S;
    233 			} else {
    234 				mob_h.proto |= MOB_H_SBIT;
    235 				mob_h.osrc = ip->ip_src.s_addr;
    236 				ip->ip_src.s_addr = sc->g_src.s_addr;
    237 				msiz = MOB_H_SIZ_L;
    238 			}
    239 			HTONS(mob_h.proto);
    240 			mob_h.hcrc = gre_in_cksum((u_int16_t *)&mob_h, msiz);
    241 
    242 			if ((m->m_data - msiz) < m->m_pktdat) {
    243 				/* need new mbuf */
    244 				MGETHDR(m0, M_DONTWAIT, MT_HEADER);
    245 				if (m0 == NULL) {
    246 					IF_DROP(&ifp->if_snd);
    247 					m_freem(m);
    248 					error = ENOBUFS;
    249 					goto end;
    250 				}
    251 				m0->m_next = m;
    252 				m->m_data += sizeof(struct ip);
    253 				m->m_len -= sizeof(struct ip);
    254 				m0->m_pkthdr.len = m->m_pkthdr.len + msiz;
    255 				m0->m_len = msiz + sizeof(struct ip);
    256 				m0->m_data += max_linkhdr;
    257 				memcpy(mtod(m0, caddr_t), (caddr_t)ip,
    258 				       sizeof(struct ip));
    259 				m = m0;
    260 			} else {  /* we have some space left in the old one */
    261 				m->m_data -= msiz;
    262 				m->m_len += msiz;
    263 				m->m_pkthdr.len += msiz;
    264 				memmove(mtod(m, caddr_t), ip,
    265 					sizeof(struct ip));
    266 			}
    267 			ip = mtod(m, struct ip *);
    268 			memcpy((caddr_t)(ip + 1), &mob_h, (unsigned)msiz);
    269 			ip->ip_len = htons(ntohs(ip->ip_len) + msiz);
    270 		} else {  /* AF_INET */
    271 			IF_DROP(&ifp->if_snd);
    272 			m_freem(m);
    273 			error = EINVAL;
    274 			goto end;
    275 		}
    276 	} else if (sc->g_proto == IPPROTO_GRE) {
    277 		switch (dst->sa_family) {
    278 		case AF_INET:
    279 			ip = mtod(m, struct ip *);
    280 			etype = ETHERTYPE_IP;
    281 			break;
    282 #ifdef NETATALK
    283 		case AF_APPLETALK:
    284 			etype = ETHERTYPE_ATALK;
    285 			break;
    286 #endif
    287 #ifdef NS
    288 		case AF_NS:
    289 			etype = ETHERTYPE_NS;
    290 			break;
    291 #endif
    292 		default:
    293 			IF_DROP(&ifp->if_snd);
    294 			m_freem(m);
    295 			error = EAFNOSUPPORT;
    296 			goto end;
    297 		}
    298 		M_PREPEND(m, sizeof(struct greip), M_DONTWAIT);
    299 	} else {
    300 		IF_DROP(&ifp->if_snd);
    301 		m_freem(m);
    302 		error = EINVAL;
    303 		goto end;
    304 	}
    305 
    306 	if (m == NULL) {	/* impossible */
    307 		IF_DROP(&ifp->if_snd);
    308 		error = ENOBUFS;
    309 		goto end;
    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 = ip_gre_ttl;
    326 		((struct ip*)gh)->ip_tos = ip->ip_tos;
    327 		gh->gi_len = htons(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,
    334 	    (struct ip_moptions *)NULL, (struct socket *)NULL);
    335   end:
    336 	if (error)
    337 		ifp->if_oerrors++;
    338 	return (error);
    339 }
    340 
    341 int
    342 gre_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    343 {
    344 	struct proc *p = curproc;	/* XXX */
    345 	struct ifreq *ifr = (struct ifreq *)data;
    346 	struct if_laddrreq *lifr = (struct if_laddrreq *)data;
    347 	struct gre_softc *sc = ifp->if_softc;
    348 	int s;
    349 	struct sockaddr_in si;
    350 	struct sockaddr *sa = NULL;
    351 	int error;
    352 
    353 	error = 0;
    354 
    355 	s = splnet();
    356 	switch (cmd) {
    357 	case SIOCSIFADDR:
    358 		ifp->if_flags |= IFF_UP;
    359 		break;
    360 	case SIOCSIFDSTADDR:
    361 		break;
    362 	case SIOCSIFFLAGS:
    363 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    364 			break;
    365 		if ((ifr->ifr_flags & IFF_LINK0) != 0)
    366 			sc->g_proto = IPPROTO_GRE;
    367 		else
    368 			sc->g_proto = IPPROTO_MOBILE;
    369 		break;
    370 	case SIOCSIFMTU:
    371 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    372 			break;
    373 		if (ifr->ifr_mtu < 576) {
    374 			error = EINVAL;
    375 			break;
    376 		}
    377 		ifp->if_mtu = ifr->ifr_mtu;
    378 		break;
    379 	case SIOCGIFMTU:
    380 		ifr->ifr_mtu = sc->sc_if.if_mtu;
    381 		break;
    382 	case SIOCADDMULTI:
    383 	case SIOCDELMULTI:
    384 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    385 			break;
    386 		if (ifr == 0) {
    387 			error = EAFNOSUPPORT;
    388 			break;
    389 		}
    390 		switch (ifr->ifr_addr.sa_family) {
    391 #ifdef INET
    392 		case AF_INET:
    393 			break;
    394 #endif
    395 		default:
    396 			error = EAFNOSUPPORT;
    397 			break;
    398 		}
    399 		break;
    400 	case GRESPROTO:
    401 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    402 			break;
    403 		sc->g_proto = ifr->ifr_flags;
    404 		switch (sc->g_proto) {
    405 		case IPPROTO_GRE:
    406 			ifp->if_flags |= IFF_LINK0;
    407 			break;
    408 		case IPPROTO_MOBILE:
    409 			ifp->if_flags &= ~IFF_LINK0;
    410 			break;
    411 		default:
    412 			error = EPROTONOSUPPORT;
    413 			break;
    414 		}
    415 		break;
    416 	case GREGPROTO:
    417 		ifr->ifr_flags = sc->g_proto;
    418 		break;
    419 	case GRESADDRS:
    420 	case GRESADDRD:
    421 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    422 			break;
    423 		/*
    424 		 * set tunnel endpoints, compute a less specific route
    425 		 * to the remote end and mark if as up
    426 		 */
    427 		sa = &ifr->ifr_addr;
    428 		if (cmd == GRESADDRS)
    429 			sc->g_src = (satosin(sa))->sin_addr;
    430 		if (cmd == GRESADDRD)
    431 			sc->g_dst = (satosin(sa))->sin_addr;
    432 	recompute:
    433 		if ((sc->g_src.s_addr != INADDR_ANY) &&
    434 		    (sc->g_dst.s_addr != INADDR_ANY)) {
    435 			if (sc->route.ro_rt != 0) /* free old route */
    436 				RTFREE(sc->route.ro_rt);
    437 			if (gre_compute_route(sc) == 0)
    438 				ifp->if_flags |= IFF_RUNNING;
    439 			else
    440 				ifp->if_flags &= ~IFF_RUNNING;
    441 		}
    442 		break;
    443 	case GREGADDRS:
    444 		memset(&si, 0, sizeof(si));
    445 		si.sin_family = AF_INET;
    446 		si.sin_len = sizeof(struct sockaddr_in);
    447 		si.sin_addr.s_addr = sc->g_src.s_addr;
    448 		sa = sintosa(&si);
    449 		ifr->ifr_addr = *sa;
    450 		break;
    451 	case GREGADDRD:
    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_dst.s_addr;
    456 		sa = sintosa(&si);
    457 		ifr->ifr_addr = *sa;
    458 		break;
    459 	case SIOCSLIFPHYADDR:
    460 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    461 			break;
    462 		if (lifr->addr.ss_family != AF_INET ||
    463 		    lifr->dstaddr.ss_family != AF_INET) {
    464 			error = EAFNOSUPPORT;
    465 			break;
    466 		}
    467 		if (lifr->addr.ss_len != sizeof(si) ||
    468 		    lifr->dstaddr.ss_len != sizeof(si)) {
    469 			error = EINVAL;
    470 			break;
    471 		}
    472 		sc->g_src = (satosin((struct sockadrr *)&lifr->addr))->sin_addr;
    473 		sc->g_dst =
    474 		    (satosin((struct sockadrr *)&lifr->dstaddr))->sin_addr;
    475 		goto recompute;
    476 	case SIOCDIFPHYADDR:
    477 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    478 			break;
    479 		sc->g_src.s_addr = INADDR_ANY;
    480 		sc->g_dst.s_addr = INADDR_ANY;
    481 		break;
    482 	case SIOCGLIFPHYADDR:
    483 		if (sc->g_src.s_addr == INADDR_ANY ||
    484 		    sc->g_dst.s_addr == INADDR_ANY) {
    485 			error = EADDRNOTAVAIL;
    486 			break;
    487 		}
    488 		memset(&si, 0, sizeof(si));
    489 		si.sin_family = AF_INET;
    490 		si.sin_len = sizeof(struct sockaddr_in);
    491 		si.sin_addr.s_addr = sc->g_src.s_addr;
    492 		memcpy(&lifr->addr, &si, sizeof(si));
    493 		si.sin_addr.s_addr = sc->g_dst.s_addr;
    494 		memcpy(&lifr->dstaddr, &si, sizeof(si));
    495 		break;
    496 	default:
    497 		error = EINVAL;
    498 		break;
    499 	}
    500 
    501 	splx(s);
    502 	return (error);
    503 }
    504 
    505 /*
    506  * computes a route to our destination that is not the one
    507  * which would be taken by ip_output(), as this one will loop back to
    508  * us. If the interface is p2p as  a--->b, then a routing entry exists
    509  * If we now send a packet to b (e.g. ping b), this will come down here
    510  * gets src=a, dst=b tacked on and would from ip_output() sent back to
    511  * if_gre.
    512  * Goal here is to compute a route to b that is less specific than
    513  * a-->b. We know that this one exists as in normal operation we have
    514  * at least a default route which matches.
    515  */
    516 int
    517 gre_compute_route(struct gre_softc *sc)
    518 {
    519 	struct route *ro;
    520 	u_int32_t a, b, c;
    521 
    522 	ro = &sc->route;
    523 
    524 	memset(ro, 0, sizeof(struct route));
    525 	((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    526 	ro->ro_dst.sa_family = AF_INET;
    527 	ro->ro_dst.sa_len = sizeof(ro->ro_dst);
    528 
    529 	/*
    530 	 * toggle last bit, so our interface is not found, but a less
    531 	 * specific route. I'd rather like to specify a shorter mask,
    532 	 * but this is not possible. Should work though. XXX
    533 	 * there is a simpler way ...
    534 	 */
    535 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0) {
    536 		a = ntohl(sc->g_dst.s_addr);
    537 		b = a & 0x01;
    538 		c = a & 0xfffffffe;
    539 		b = b ^ 0x01;
    540 		a = b | c;
    541 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr.s_addr
    542 		    = htonl(a);
    543 	}
    544 
    545 #ifdef DIAGNOSTIC
    546 	printf("%s: searching for a route to %s", sc->sc_if.if_xname,
    547 	    inet_ntoa(((struct sockaddr_in *)&ro->ro_dst)->sin_addr));
    548 #endif
    549 
    550 	rtalloc(ro);
    551 
    552 	/*
    553 	 * check if this returned a route at all and this route is no
    554 	 * recursion to ourself
    555 	 */
    556 	if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp->if_softc == sc) {
    557 #ifdef DIAGNOSTIC
    558 		if (ro->ro_rt == NULL)
    559 			printf(" - no route found!\n");
    560 		else
    561 			printf(" - route loops back to ourself!\n");
    562 #endif
    563 		return EADDRNOTAVAIL;
    564 	}
    565 
    566 	/*
    567 	 * now change it back - else ip_output will just drop
    568 	 * the route and search one to this interface ...
    569 	 */
    570 	if ((sc->sc_if.if_flags & IFF_LINK1) == 0)
    571 		((struct sockaddr_in *)&ro->ro_dst)->sin_addr = sc->g_dst;
    572 
    573 #ifdef DIAGNOSTIC
    574 	printf(", choosing %s with gateway %s", ro->ro_rt->rt_ifp->if_xname,
    575 	    inet_ntoa(((struct sockaddr_in *)(ro->ro_rt->rt_gateway))->sin_addr));
    576 	printf("\n");
    577 #endif
    578 
    579 	return 0;
    580 }
    581 
    582 /*
    583  * do a checksum of a buffer - much like in_cksum, which operates on
    584  * mbufs.
    585  */
    586 u_int16_t
    587 gre_in_cksum(u_int16_t *p, u_int len)
    588 {
    589 	u_int32_t sum = 0;
    590 	int nwords = len >> 1;
    591 
    592 	while (nwords-- != 0)
    593 		sum += *p++;
    594 
    595 	if (len & 1) {
    596 		union {
    597 			u_short w;
    598 			u_char c[2];
    599 		} u;
    600 		u.c[0] = *(u_char *)p;
    601 		u.c[1] = 0;
    602 		sum += u.w;
    603 	}
    604 
    605 	/* end-around-carry */
    606 	sum = (sum >> 16) + (sum & 0xffff);
    607 	sum += (sum >> 16);
    608 	return (~sum);
    609 }
    610