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