Home | History | Annotate | Line # | Download | only in netinet
ip_output.c revision 1.258
      1 /*	$NetBSD: ip_output.c,v 1.258 2016/06/21 03:28:27 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Public Access Networks Corporation ("Panix").  It was developed under
     38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59  * POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * Copyright (c) 1982, 1986, 1988, 1990, 1993
     64  *	The Regents of the University of California.  All rights reserved.
     65  *
     66  * Redistribution and use in source and binary forms, with or without
     67  * modification, are permitted provided that the following conditions
     68  * are met:
     69  * 1. Redistributions of source code must retain the above copyright
     70  *    notice, this list of conditions and the following disclaimer.
     71  * 2. Redistributions in binary form must reproduce the above copyright
     72  *    notice, this list of conditions and the following disclaimer in the
     73  *    documentation and/or other materials provided with the distribution.
     74  * 3. Neither the name of the University nor the names of its contributors
     75  *    may be used to endorse or promote products derived from this software
     76  *    without specific prior written permission.
     77  *
     78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     88  * SUCH DAMAGE.
     89  *
     90  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
     91  */
     92 
     93 #include <sys/cdefs.h>
     94 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.258 2016/06/21 03:28:27 ozaki-r Exp $");
     95 
     96 #ifdef _KERNEL_OPT
     97 #include "opt_inet.h"
     98 #include "opt_ipsec.h"
     99 #include "opt_mrouting.h"
    100 #include "opt_net_mpsafe.h"
    101 #include "opt_mpls.h"
    102 #endif
    103 
    104 #include <sys/param.h>
    105 #include <sys/kmem.h>
    106 #include <sys/mbuf.h>
    107 #include <sys/protosw.h>
    108 #include <sys/socket.h>
    109 #include <sys/socketvar.h>
    110 #include <sys/kauth.h>
    111 #ifdef IPSEC
    112 #include <sys/domain.h>
    113 #endif
    114 #include <sys/systm.h>
    115 
    116 #include <net/if.h>
    117 #include <net/if_types.h>
    118 #include <net/route.h>
    119 #include <net/pfil.h>
    120 
    121 #include <netinet/in.h>
    122 #include <netinet/in_systm.h>
    123 #include <netinet/ip.h>
    124 #include <netinet/in_pcb.h>
    125 #include <netinet/in_var.h>
    126 #include <netinet/ip_var.h>
    127 #include <netinet/ip_private.h>
    128 #include <netinet/in_offload.h>
    129 #include <netinet/portalgo.h>
    130 #include <netinet/udp.h>
    131 
    132 #ifdef INET6
    133 #include <netinet6/ip6_var.h>
    134 #endif
    135 
    136 #ifdef MROUTING
    137 #include <netinet/ip_mroute.h>
    138 #endif
    139 
    140 #ifdef IPSEC
    141 #include <netipsec/ipsec.h>
    142 #include <netipsec/key.h>
    143 #endif
    144 
    145 #ifdef MPLS
    146 #include <netmpls/mpls.h>
    147 #include <netmpls/mpls_var.h>
    148 #endif
    149 
    150 static int ip_pcbopts(struct inpcb *, const struct sockopt *);
    151 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
    152 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
    153 static void ip_mloopback(struct ifnet *, struct mbuf *,
    154     const struct sockaddr_in *);
    155 
    156 extern pfil_head_t *inet_pfil_hook;			/* XXX */
    157 
    158 int	ip_do_loopback_cksum = 0;
    159 
    160 static int
    161 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m,
    162     const struct rtentry *rt)
    163 {
    164 	int error = 0;
    165 #ifdef MPLS
    166 	union mpls_shim msh;
    167 
    168 	if (rt == NULL || rt_gettag(rt) == NULL ||
    169 	    rt_gettag(rt)->sa_family != AF_MPLS ||
    170 	    (m->m_flags & (M_MCAST | M_BCAST)) != 0 ||
    171 	    ifp->if_type != IFT_ETHER)
    172 		return 0;
    173 
    174 	msh.s_addr = MPLS_GETSADDR(rt);
    175 	if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
    176 		struct m_tag *mtag;
    177 		/*
    178 		 * XXX tentative solution to tell ether_output
    179 		 * it's MPLS. Need some more efficient solution.
    180 		 */
    181 		mtag = m_tag_get(PACKET_TAG_MPLS,
    182 		    sizeof(int) /* dummy */,
    183 		    M_NOWAIT);
    184 		if (mtag == NULL)
    185 			return ENOMEM;
    186 		m_tag_prepend(m, mtag);
    187 	}
    188 #endif
    189 	return error;
    190 }
    191 
    192 /*
    193  * Send an IP packet to a host.
    194  */
    195 int
    196 ip_if_output(struct ifnet * const ifp, struct mbuf * const m,
    197     const struct sockaddr * const dst, const struct rtentry *rt)
    198 {
    199 	int error = 0;
    200 
    201 	if (rt != NULL) {
    202 		error = rt_check_reject_route(rt, ifp);
    203 		if (error != 0) {
    204 			m_freem(m);
    205 			return error;
    206 		}
    207 	}
    208 
    209 	error = ip_mark_mpls(ifp, m, rt);
    210 	if (error != 0) {
    211 		m_freem(m);
    212 		return error;
    213 	}
    214 
    215 	error = if_output_lock(ifp, ifp, m, dst, rt);
    216 
    217 	return error;
    218 }
    219 
    220 /*
    221  * IP output.  The packet in mbuf chain m contains a skeletal IP
    222  * header (with len, off, ttl, proto, tos, src, dst).
    223  * The mbuf chain containing the packet will be freed.
    224  * The mbuf opt, if present, will not be freed.
    225  */
    226 int
    227 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
    228     struct ip_moptions *imo, struct socket *so)
    229 {
    230 	struct rtentry *rt;
    231 	struct ip *ip;
    232 	struct ifnet *ifp, *mifp = NULL;
    233 	struct mbuf *m = m0;
    234 	int hlen = sizeof (struct ip);
    235 	int len, error = 0;
    236 	struct route iproute;
    237 	const struct sockaddr_in *dst;
    238 	struct in_ifaddr *ia;
    239 	int isbroadcast;
    240 	int sw_csum;
    241 	u_long mtu;
    242 #ifdef IPSEC
    243 	struct secpolicy *sp = NULL;
    244 #endif
    245 	bool natt_frag = false;
    246 	bool rtmtu_nolock;
    247 	union {
    248 		struct sockaddr		dst;
    249 		struct sockaddr_in	dst4;
    250 	} u;
    251 	struct sockaddr *rdst = &u.dst;	/* real IP destination, as opposed
    252 					 * to the nexthop
    253 					 */
    254 	struct psref psref;
    255 	int bound;
    256 
    257 	len = 0;
    258 
    259 	MCLAIM(m, &ip_tx_mowner);
    260 
    261 	KASSERT((m->m_flags & M_PKTHDR) != 0);
    262 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
    263 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
    264 	    (M_CSUM_TCPv4|M_CSUM_UDPv4));
    265 
    266 	if (opt) {
    267 		m = ip_insertoptions(m, opt, &len);
    268 		if (len >= sizeof(struct ip))
    269 			hlen = len;
    270 	}
    271 	ip = mtod(m, struct ip *);
    272 
    273 	/*
    274 	 * Fill in IP header.
    275 	 */
    276 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
    277 		ip->ip_v = IPVERSION;
    278 		ip->ip_off = htons(0);
    279 		/* ip->ip_id filled in after we find out source ia */
    280 		ip->ip_hl = hlen >> 2;
    281 		IP_STATINC(IP_STAT_LOCALOUT);
    282 	} else {
    283 		hlen = ip->ip_hl << 2;
    284 	}
    285 
    286 	/*
    287 	 * Route packet.
    288 	 */
    289 	if (ro == NULL) {
    290 		memset(&iproute, 0, sizeof(iproute));
    291 		ro = &iproute;
    292 	}
    293 	sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
    294 	dst = satocsin(rtcache_getdst(ro));
    295 
    296 	/*
    297 	 * If there is a cached route, check that it is to the same
    298 	 * destination and is still up.  If not, free it and try again.
    299 	 * The address family should also be checked in case of sharing
    300 	 * the cache with IPv6.
    301 	 */
    302 	if (dst && (dst->sin_family != AF_INET ||
    303 	    !in_hosteq(dst->sin_addr, ip->ip_dst)))
    304 		rtcache_free(ro);
    305 
    306 	if ((rt = rtcache_validate(ro)) == NULL &&
    307 	    (rt = rtcache_update(ro, 1)) == NULL) {
    308 		dst = &u.dst4;
    309 		error = rtcache_setdst(ro, &u.dst);
    310 		if (error != 0)
    311 			goto bad;
    312 	}
    313 
    314 	/*
    315 	 * If routing to interface only, short circuit routing lookup.
    316 	 */
    317 	if (flags & IP_ROUTETOIF) {
    318 		if ((ia = ifatoia(ifa_ifwithladdr(sintocsa(dst)))) == NULL) {
    319 			IP_STATINC(IP_STAT_NOROUTE);
    320 			error = ENETUNREACH;
    321 			goto bad;
    322 		}
    323 		ifp = ia->ia_ifp;
    324 		mtu = ifp->if_mtu;
    325 		ip->ip_ttl = 1;
    326 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
    327 	} else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
    328 	    ip->ip_dst.s_addr == INADDR_BROADCAST) &&
    329 	    imo != NULL && imo->imo_multicast_if_index != 0) {
    330 		bound = curlwp_bind();
    331 		ifp = mifp = if_get_byindex(imo->imo_multicast_if_index, &psref);
    332 		if (ifp == NULL) {
    333 			curlwp_bindx(bound);
    334 			IP_STATINC(IP_STAT_NOROUTE);
    335 			error = ENETUNREACH;
    336 			goto bad;
    337 		}
    338 		mtu = ifp->if_mtu;
    339 		IFP_TO_IA(ifp, ia);
    340 		isbroadcast = 0;
    341 	} else {
    342 		if (rt == NULL)
    343 			rt = rtcache_init(ro);
    344 		if (rt == NULL) {
    345 			IP_STATINC(IP_STAT_NOROUTE);
    346 			error = EHOSTUNREACH;
    347 			goto bad;
    348 		}
    349 		ia = ifatoia(rt->rt_ifa);
    350 		ifp = rt->rt_ifp;
    351 		if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
    352 			mtu = ifp->if_mtu;
    353 		rt->rt_use++;
    354 		if (rt->rt_flags & RTF_GATEWAY)
    355 			dst = satosin(rt->rt_gateway);
    356 		if (rt->rt_flags & RTF_HOST)
    357 			isbroadcast = rt->rt_flags & RTF_BROADCAST;
    358 		else
    359 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
    360 	}
    361 	rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
    362 
    363 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    364 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
    365 		bool inmgroup;
    366 
    367 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
    368 		    M_BCAST : M_MCAST;
    369 		/*
    370 		 * See if the caller provided any multicast options
    371 		 */
    372 		if (imo != NULL)
    373 			ip->ip_ttl = imo->imo_multicast_ttl;
    374 		else
    375 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
    376 
    377 		/*
    378 		 * if we don't know the outgoing ifp yet, we can't generate
    379 		 * output
    380 		 */
    381 		if (!ifp) {
    382 			IP_STATINC(IP_STAT_NOROUTE);
    383 			error = ENETUNREACH;
    384 			goto bad;
    385 		}
    386 
    387 		/*
    388 		 * If the packet is multicast or broadcast, confirm that
    389 		 * the outgoing interface can transmit it.
    390 		 */
    391 		if (((m->m_flags & M_MCAST) &&
    392 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
    393 		    ((m->m_flags & M_BCAST) &&
    394 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
    395 			IP_STATINC(IP_STAT_NOROUTE);
    396 			error = ENETUNREACH;
    397 			goto bad;
    398 		}
    399 		/*
    400 		 * If source address not specified yet, use an address
    401 		 * of outgoing interface.
    402 		 */
    403 		if (in_nullhost(ip->ip_src)) {
    404 			struct in_ifaddr *xia;
    405 			struct ifaddr *xifa;
    406 
    407 			IFP_TO_IA(ifp, xia);
    408 			if (!xia) {
    409 				error = EADDRNOTAVAIL;
    410 				goto bad;
    411 			}
    412 			xifa = &xia->ia_ifa;
    413 			if (xifa->ifa_getifa != NULL) {
    414 				xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
    415 				if (xia == NULL) {
    416 					error = EADDRNOTAVAIL;
    417 					goto bad;
    418 				}
    419 			}
    420 			ip->ip_src = xia->ia_addr.sin_addr;
    421 		}
    422 
    423 		inmgroup = in_multi_group(ip->ip_dst, ifp, flags);
    424 		if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) {
    425 			/*
    426 			 * If we belong to the destination multicast group
    427 			 * on the outgoing interface, and the caller did not
    428 			 * forbid loopback, loop back a copy.
    429 			 */
    430 			ip_mloopback(ifp, m, &u.dst4);
    431 		}
    432 #ifdef MROUTING
    433 		else {
    434 			/*
    435 			 * If we are acting as a multicast router, perform
    436 			 * multicast forwarding as if the packet had just
    437 			 * arrived on the interface to which we are about
    438 			 * to send.  The multicast forwarding function
    439 			 * recursively calls this function, using the
    440 			 * IP_FORWARDING flag to prevent infinite recursion.
    441 			 *
    442 			 * Multicasts that are looped back by ip_mloopback(),
    443 			 * above, will be forwarded by the ip_input() routine,
    444 			 * if necessary.
    445 			 */
    446 			extern struct socket *ip_mrouter;
    447 
    448 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
    449 				if (ip_mforward(m, ifp) != 0) {
    450 					m_freem(m);
    451 					goto done;
    452 				}
    453 			}
    454 		}
    455 #endif
    456 		/*
    457 		 * Multicasts with a time-to-live of zero may be looped-
    458 		 * back, above, but must not be transmitted on a network.
    459 		 * Also, multicasts addressed to the loopback interface
    460 		 * are not sent -- the above call to ip_mloopback() will
    461 		 * loop back a copy if this host actually belongs to the
    462 		 * destination group on the loopback interface.
    463 		 */
    464 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
    465 			m_freem(m);
    466 			goto done;
    467 		}
    468 		goto sendit;
    469 	}
    470 
    471 	/*
    472 	 * If source address not specified yet, use address
    473 	 * of outgoing interface.
    474 	 */
    475 	if (in_nullhost(ip->ip_src)) {
    476 		struct ifaddr *xifa;
    477 
    478 		xifa = &ia->ia_ifa;
    479 		if (xifa->ifa_getifa != NULL) {
    480 			ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
    481 			if (ia == NULL) {
    482 				error = EADDRNOTAVAIL;
    483 				goto bad;
    484 			}
    485 		}
    486 		ip->ip_src = ia->ia_addr.sin_addr;
    487 	}
    488 
    489 	/*
    490 	 * packets with Class-D address as source are not valid per
    491 	 * RFC 1112
    492 	 */
    493 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
    494 		IP_STATINC(IP_STAT_ODROPPED);
    495 		error = EADDRNOTAVAIL;
    496 		goto bad;
    497 	}
    498 
    499 	/*
    500 	 * Look for broadcast address and and verify user is allowed to
    501 	 * send such a packet.
    502 	 */
    503 	if (isbroadcast) {
    504 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    505 			error = EADDRNOTAVAIL;
    506 			goto bad;
    507 		}
    508 		if ((flags & IP_ALLOWBROADCAST) == 0) {
    509 			error = EACCES;
    510 			goto bad;
    511 		}
    512 		/* don't allow broadcast messages to be fragmented */
    513 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
    514 			error = EMSGSIZE;
    515 			goto bad;
    516 		}
    517 		m->m_flags |= M_BCAST;
    518 	} else
    519 		m->m_flags &= ~M_BCAST;
    520 
    521 sendit:
    522 	if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
    523 		if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
    524 			ip->ip_id = 0;
    525 		} else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    526 			ip->ip_id = ip_newid(ia);
    527 		} else {
    528 
    529 			/*
    530 			 * TSO capable interfaces (typically?) increment
    531 			 * ip_id for each segment.
    532 			 * "allocate" enough ids here to increase the chance
    533 			 * for them to be unique.
    534 			 *
    535 			 * note that the following calculation is not
    536 			 * needed to be precise.  wasting some ip_id is fine.
    537 			 */
    538 
    539 			unsigned int segsz = m->m_pkthdr.segsz;
    540 			unsigned int datasz = ntohs(ip->ip_len) - hlen;
    541 			unsigned int num = howmany(datasz, segsz);
    542 
    543 			ip->ip_id = ip_newid_range(ia, num);
    544 		}
    545 	}
    546 
    547 	/*
    548 	 * If we're doing Path MTU Discovery, we need to set DF unless
    549 	 * the route's MTU is locked.
    550 	 */
    551 	if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) {
    552 		ip->ip_off |= htons(IP_DF);
    553 	}
    554 
    555 #ifdef IPSEC
    556 	if (ipsec_used) {
    557 		bool ipsec_done = false;
    558 
    559 		/* Perform IPsec processing, if any. */
    560 		error = ipsec4_output(m, so, flags, &sp, &mtu, &natt_frag,
    561 		    &ipsec_done);
    562 		if (error || ipsec_done)
    563 			goto done;
    564 	}
    565 #endif
    566 
    567 	/*
    568 	 * Run through list of hooks for output packets.
    569 	 */
    570 	error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT);
    571 	if (error)
    572 		goto done;
    573 	if (m == NULL)
    574 		goto done;
    575 
    576 	ip = mtod(m, struct ip *);
    577 	hlen = ip->ip_hl << 2;
    578 
    579 	m->m_pkthdr.csum_data |= hlen << 16;
    580 
    581 #if IFA_STATS
    582 	/*
    583 	 * search for the source address structure to
    584 	 * maintain output statistics.
    585 	 */
    586 	INADDR_TO_IA(ip->ip_src, ia);
    587 #endif
    588 
    589 	/* Maybe skip checksums on loopback interfaces. */
    590 	if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
    591 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
    592 	}
    593 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
    594 	/*
    595 	 * If small enough for mtu of path, or if using TCP segmentation
    596 	 * offload, can just send directly.
    597 	 */
    598 	if (ntohs(ip->ip_len) <= mtu ||
    599 	    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
    600 		const struct sockaddr *sa;
    601 
    602 #if IFA_STATS
    603 		if (ia)
    604 			ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
    605 #endif
    606 		/*
    607 		 * Always initialize the sum to 0!  Some HW assisted
    608 		 * checksumming requires this.
    609 		 */
    610 		ip->ip_sum = 0;
    611 
    612 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
    613 			/*
    614 			 * Perform any checksums that the hardware can't do
    615 			 * for us.
    616 			 *
    617 			 * XXX Does any hardware require the {th,uh}_sum
    618 			 * XXX fields to be 0?
    619 			 */
    620 			if (sw_csum & M_CSUM_IPv4) {
    621 				KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
    622 				ip->ip_sum = in_cksum(m, hlen);
    623 				m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    624 			}
    625 			if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    626 				if (IN_NEED_CHECKSUM(ifp,
    627 				    sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
    628 					in_delayed_cksum(m);
    629 				}
    630 				m->m_pkthdr.csum_flags &=
    631 				    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    632 			}
    633 		}
    634 
    635 		sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst);
    636 		if (__predict_true(
    637 		    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 ||
    638 		    (ifp->if_capenable & IFCAP_TSOv4) != 0)) {
    639 			error = ip_if_output(ifp, m, sa, rt);
    640 		} else {
    641 			error = ip_tso_output(ifp, m, sa, rt);
    642 		}
    643 		goto done;
    644 	}
    645 
    646 	/*
    647 	 * We can't use HW checksumming if we're about to
    648 	 * to fragment the packet.
    649 	 *
    650 	 * XXX Some hardware can do this.
    651 	 */
    652 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
    653 		if (IN_NEED_CHECKSUM(ifp,
    654 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
    655 			in_delayed_cksum(m);
    656 		}
    657 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
    658 	}
    659 
    660 	/*
    661 	 * Too large for interface; fragment if possible.
    662 	 * Must be able to put at least 8 bytes per fragment.
    663 	 */
    664 	if (ntohs(ip->ip_off) & IP_DF) {
    665 		if (flags & IP_RETURNMTU) {
    666 			struct inpcb *inp;
    667 
    668 			KASSERT(so && solocked(so));
    669 			inp = sotoinpcb(so);
    670 			inp->inp_errormtu = mtu;
    671 		}
    672 		error = EMSGSIZE;
    673 		IP_STATINC(IP_STAT_CANTFRAG);
    674 		goto bad;
    675 	}
    676 
    677 	error = ip_fragment(m, ifp, mtu);
    678 	if (error) {
    679 		m = NULL;
    680 		goto bad;
    681 	}
    682 
    683 	for (; m; m = m0) {
    684 		m0 = m->m_nextpkt;
    685 		m->m_nextpkt = 0;
    686 		if (error) {
    687 			m_freem(m);
    688 			continue;
    689 		}
    690 #if IFA_STATS
    691 		if (ia)
    692 			ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
    693 #endif
    694 		/*
    695 		 * If we get there, the packet has not been handled by
    696 		 * IPsec whereas it should have. Now that it has been
    697 		 * fragmented, re-inject it in ip_output so that IPsec
    698 		 * processing can occur.
    699 		 */
    700 		if (natt_frag) {
    701 			error = ip_output(m, opt, ro,
    702 			    flags | IP_RAWOUTPUT | IP_NOIPNEWID,
    703 			    imo, so);
    704 		} else {
    705 			KASSERT((m->m_pkthdr.csum_flags &
    706 			    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
    707 			error = ip_if_output(ifp, m,
    708 			    (m->m_flags & M_MCAST) ?
    709 			    sintocsa(rdst) : sintocsa(dst), rt);
    710 		}
    711 	}
    712 	if (error == 0) {
    713 		IP_STATINC(IP_STAT_FRAGMENTED);
    714 	}
    715 done:
    716 	if (ro == &iproute) {
    717 		rtcache_free(&iproute);
    718 	}
    719 #ifdef IPSEC
    720 	if (sp) {
    721 		KEY_FREESP(&sp);
    722 	}
    723 #endif
    724 	if (mifp != NULL) {
    725 		if_put(mifp, &psref);
    726 		curlwp_bindx(bound);
    727 	}
    728 	return error;
    729 bad:
    730 	m_freem(m);
    731 	goto done;
    732 }
    733 
    734 int
    735 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
    736 {
    737 	struct ip *ip, *mhip;
    738 	struct mbuf *m0;
    739 	int len, hlen, off;
    740 	int mhlen, firstlen;
    741 	struct mbuf **mnext;
    742 	int sw_csum = m->m_pkthdr.csum_flags;
    743 	int fragments = 0;
    744 	int s;
    745 	int error = 0;
    746 
    747 	ip = mtod(m, struct ip *);
    748 	hlen = ip->ip_hl << 2;
    749 	if (ifp != NULL)
    750 		sw_csum &= ~ifp->if_csum_flags_tx;
    751 
    752 	len = (mtu - hlen) &~ 7;
    753 	if (len < 8) {
    754 		m_freem(m);
    755 		return (EMSGSIZE);
    756 	}
    757 
    758 	firstlen = len;
    759 	mnext = &m->m_nextpkt;
    760 
    761 	/*
    762 	 * Loop through length of segment after first fragment,
    763 	 * make new header and copy data of each part and link onto chain.
    764 	 */
    765 	m0 = m;
    766 	mhlen = sizeof (struct ip);
    767 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
    768 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
    769 		if (m == 0) {
    770 			error = ENOBUFS;
    771 			IP_STATINC(IP_STAT_ODROPPED);
    772 			goto sendorfree;
    773 		}
    774 		MCLAIM(m, m0->m_owner);
    775 		*mnext = m;
    776 		mnext = &m->m_nextpkt;
    777 		m->m_data += max_linkhdr;
    778 		mhip = mtod(m, struct ip *);
    779 		*mhip = *ip;
    780 		/* we must inherit MCAST and BCAST flags */
    781 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
    782 		if (hlen > sizeof (struct ip)) {
    783 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
    784 			mhip->ip_hl = mhlen >> 2;
    785 		}
    786 		m->m_len = mhlen;
    787 		mhip->ip_off = ((off - hlen) >> 3) +
    788 		    (ntohs(ip->ip_off) & ~IP_MF);
    789 		if (ip->ip_off & htons(IP_MF))
    790 			mhip->ip_off |= IP_MF;
    791 		if (off + len >= ntohs(ip->ip_len))
    792 			len = ntohs(ip->ip_len) - off;
    793 		else
    794 			mhip->ip_off |= IP_MF;
    795 		HTONS(mhip->ip_off);
    796 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
    797 		m->m_next = m_copym(m0, off, len, M_DONTWAIT);
    798 		if (m->m_next == 0) {
    799 			error = ENOBUFS;	/* ??? */
    800 			IP_STATINC(IP_STAT_ODROPPED);
    801 			goto sendorfree;
    802 		}
    803 		m->m_pkthdr.len = mhlen + len;
    804 		m_reset_rcvif(m);
    805 		mhip->ip_sum = 0;
    806 		KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
    807 		if (sw_csum & M_CSUM_IPv4) {
    808 			mhip->ip_sum = in_cksum(m, mhlen);
    809 		} else {
    810 			/*
    811 			 * checksum is hw-offloaded or not necessary.
    812 			 */
    813 			m->m_pkthdr.csum_flags |=
    814 			    m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
    815 			m->m_pkthdr.csum_data |= mhlen << 16;
    816 			KASSERT(!(ifp != NULL &&
    817 			    IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
    818 			    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
    819 		}
    820 		IP_STATINC(IP_STAT_OFRAGMENTS);
    821 		fragments++;
    822 	}
    823 	/*
    824 	 * Update first fragment by trimming what's been copied out
    825 	 * and updating header, then send each fragment (in order).
    826 	 */
    827 	m = m0;
    828 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
    829 	m->m_pkthdr.len = hlen + firstlen;
    830 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
    831 	ip->ip_off |= htons(IP_MF);
    832 	ip->ip_sum = 0;
    833 	if (sw_csum & M_CSUM_IPv4) {
    834 		ip->ip_sum = in_cksum(m, hlen);
    835 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
    836 	} else {
    837 		/*
    838 		 * checksum is hw-offloaded or not necessary.
    839 		 */
    840 		KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
    841 		    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
    842 		KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
    843 		    sizeof(struct ip));
    844 	}
    845 sendorfree:
    846 	/*
    847 	 * If there is no room for all the fragments, don't queue
    848 	 * any of them.
    849 	 */
    850 	if (ifp != NULL) {
    851 		s = splnet();
    852 		if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
    853 		    error == 0) {
    854 			error = ENOBUFS;
    855 			IP_STATINC(IP_STAT_ODROPPED);
    856 			IFQ_INC_DROPS(&ifp->if_snd);
    857 		}
    858 		splx(s);
    859 	}
    860 	if (error) {
    861 		for (m = m0; m; m = m0) {
    862 			m0 = m->m_nextpkt;
    863 			m->m_nextpkt = NULL;
    864 			m_freem(m);
    865 		}
    866 	}
    867 	return (error);
    868 }
    869 
    870 /*
    871  * Process a delayed payload checksum calculation.
    872  */
    873 void
    874 in_delayed_cksum(struct mbuf *m)
    875 {
    876 	struct ip *ip;
    877 	u_int16_t csum, offset;
    878 
    879 	ip = mtod(m, struct ip *);
    880 	offset = ip->ip_hl << 2;
    881 	csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
    882 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
    883 		csum = 0xffff;
    884 
    885 	offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
    886 
    887 	if ((offset + sizeof(u_int16_t)) > m->m_len) {
    888 		/* This happen when ip options were inserted
    889 		printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
    890 		    m->m_len, offset, ip->ip_p);
    891 		 */
    892 		m_copyback(m, offset, sizeof(csum), (void *) &csum);
    893 	} else
    894 		*(u_int16_t *)(mtod(m, char *) + offset) = csum;
    895 }
    896 
    897 /*
    898  * Determine the maximum length of the options to be inserted;
    899  * we would far rather allocate too much space rather than too little.
    900  */
    901 
    902 u_int
    903 ip_optlen(struct inpcb *inp)
    904 {
    905 	struct mbuf *m = inp->inp_options;
    906 
    907 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
    908 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
    909 	}
    910 	return 0;
    911 }
    912 
    913 /*
    914  * Insert IP options into preformed packet.
    915  * Adjust IP destination as required for IP source routing,
    916  * as indicated by a non-zero in_addr at the start of the options.
    917  */
    918 static struct mbuf *
    919 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
    920 {
    921 	struct ipoption *p = mtod(opt, struct ipoption *);
    922 	struct mbuf *n;
    923 	struct ip *ip = mtod(m, struct ip *);
    924 	unsigned optlen;
    925 
    926 	optlen = opt->m_len - sizeof(p->ipopt_dst);
    927 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
    928 		return (m);		/* XXX should fail */
    929 	if (!in_nullhost(p->ipopt_dst))
    930 		ip->ip_dst = p->ipopt_dst;
    931 	if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
    932 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
    933 		if (n == 0)
    934 			return (m);
    935 		MCLAIM(n, m->m_owner);
    936 		M_MOVE_PKTHDR(n, m);
    937 		m->m_len -= sizeof(struct ip);
    938 		m->m_data += sizeof(struct ip);
    939 		n->m_next = m;
    940 		m = n;
    941 		m->m_len = optlen + sizeof(struct ip);
    942 		m->m_data += max_linkhdr;
    943 		bcopy((void *)ip, mtod(m, void *), sizeof(struct ip));
    944 	} else {
    945 		m->m_data -= optlen;
    946 		m->m_len += optlen;
    947 		memmove(mtod(m, void *), ip, sizeof(struct ip));
    948 	}
    949 	m->m_pkthdr.len += optlen;
    950 	ip = mtod(m, struct ip *);
    951 	bcopy((void *)p->ipopt_list, (void *)(ip + 1), (unsigned)optlen);
    952 	*phlen = sizeof(struct ip) + optlen;
    953 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
    954 	return (m);
    955 }
    956 
    957 /*
    958  * Copy options from ip to jp,
    959  * omitting those not copied during fragmentation.
    960  */
    961 int
    962 ip_optcopy(struct ip *ip, struct ip *jp)
    963 {
    964 	u_char *cp, *dp;
    965 	int opt, optlen, cnt;
    966 
    967 	cp = (u_char *)(ip + 1);
    968 	dp = (u_char *)(jp + 1);
    969 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
    970 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
    971 		opt = cp[0];
    972 		if (opt == IPOPT_EOL)
    973 			break;
    974 		if (opt == IPOPT_NOP) {
    975 			/* Preserve for IP mcast tunnel's LSRR alignment. */
    976 			*dp++ = IPOPT_NOP;
    977 			optlen = 1;
    978 			continue;
    979 		}
    980 
    981 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
    982 		optlen = cp[IPOPT_OLEN];
    983 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
    984 
    985 		/* Invalid lengths should have been caught by ip_dooptions. */
    986 		if (optlen > cnt)
    987 			optlen = cnt;
    988 		if (IPOPT_COPIED(opt)) {
    989 			bcopy((void *)cp, (void *)dp, (unsigned)optlen);
    990 			dp += optlen;
    991 		}
    992 	}
    993 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
    994 		*dp++ = IPOPT_EOL;
    995 	return (optlen);
    996 }
    997 
    998 /*
    999  * IP socket option processing.
   1000  */
   1001 int
   1002 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
   1003 {
   1004 	struct inpcb *inp = sotoinpcb(so);
   1005 	struct ip *ip = &inp->inp_ip;
   1006 	int inpflags = inp->inp_flags;
   1007 	int optval = 0, error = 0;
   1008 
   1009 	if (sopt->sopt_level != IPPROTO_IP) {
   1010 		if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
   1011 			return 0;
   1012 		return ENOPROTOOPT;
   1013 	}
   1014 
   1015 	switch (op) {
   1016 	case PRCO_SETOPT:
   1017 		switch (sopt->sopt_name) {
   1018 		case IP_OPTIONS:
   1019 #ifdef notyet
   1020 		case IP_RETOPTS:
   1021 #endif
   1022 			error = ip_pcbopts(inp, sopt);
   1023 			break;
   1024 
   1025 		case IP_TOS:
   1026 		case IP_TTL:
   1027 		case IP_MINTTL:
   1028 		case IP_PKTINFO:
   1029 		case IP_RECVOPTS:
   1030 		case IP_RECVRETOPTS:
   1031 		case IP_RECVDSTADDR:
   1032 		case IP_RECVIF:
   1033 		case IP_RECVPKTINFO:
   1034 		case IP_RECVTTL:
   1035 			error = sockopt_getint(sopt, &optval);
   1036 			if (error)
   1037 				break;
   1038 
   1039 			switch (sopt->sopt_name) {
   1040 			case IP_TOS:
   1041 				ip->ip_tos = optval;
   1042 				break;
   1043 
   1044 			case IP_TTL:
   1045 				ip->ip_ttl = optval;
   1046 				break;
   1047 
   1048 			case IP_MINTTL:
   1049 				if (optval > 0 && optval <= MAXTTL)
   1050 					inp->inp_ip_minttl = optval;
   1051 				else
   1052 					error = EINVAL;
   1053 				break;
   1054 #define	OPTSET(bit) \
   1055 	if (optval) \
   1056 		inpflags |= bit; \
   1057 	else \
   1058 		inpflags &= ~bit;
   1059 
   1060 			case IP_PKTINFO:
   1061 				OPTSET(INP_PKTINFO);
   1062 				break;
   1063 
   1064 			case IP_RECVOPTS:
   1065 				OPTSET(INP_RECVOPTS);
   1066 				break;
   1067 
   1068 			case IP_RECVPKTINFO:
   1069 				OPTSET(INP_RECVPKTINFO);
   1070 				break;
   1071 
   1072 			case IP_RECVRETOPTS:
   1073 				OPTSET(INP_RECVRETOPTS);
   1074 				break;
   1075 
   1076 			case IP_RECVDSTADDR:
   1077 				OPTSET(INP_RECVDSTADDR);
   1078 				break;
   1079 
   1080 			case IP_RECVIF:
   1081 				OPTSET(INP_RECVIF);
   1082 				break;
   1083 
   1084 			case IP_RECVTTL:
   1085 				OPTSET(INP_RECVTTL);
   1086 				break;
   1087 			}
   1088 		break;
   1089 #undef OPTSET
   1090 
   1091 		case IP_MULTICAST_IF:
   1092 		case IP_MULTICAST_TTL:
   1093 		case IP_MULTICAST_LOOP:
   1094 		case IP_ADD_MEMBERSHIP:
   1095 		case IP_DROP_MEMBERSHIP:
   1096 			error = ip_setmoptions(&inp->inp_moptions, sopt);
   1097 			break;
   1098 
   1099 		case IP_PORTRANGE:
   1100 			error = sockopt_getint(sopt, &optval);
   1101 			if (error)
   1102 				break;
   1103 
   1104 			switch (optval) {
   1105 			case IP_PORTRANGE_DEFAULT:
   1106 			case IP_PORTRANGE_HIGH:
   1107 				inpflags &= ~(INP_LOWPORT);
   1108 				break;
   1109 
   1110 			case IP_PORTRANGE_LOW:
   1111 				inpflags |= INP_LOWPORT;
   1112 				break;
   1113 
   1114 			default:
   1115 				error = EINVAL;
   1116 				break;
   1117 			}
   1118 			break;
   1119 
   1120 		case IP_PORTALGO:
   1121 			error = sockopt_getint(sopt, &optval);
   1122 			if (error)
   1123 				break;
   1124 
   1125 			error = portalgo_algo_index_select(
   1126 			    (struct inpcb_hdr *)inp, optval);
   1127 			break;
   1128 
   1129 #if defined(IPSEC)
   1130 		case IP_IPSEC_POLICY:
   1131 			if (ipsec_enabled) {
   1132 				error = ipsec4_set_policy(inp, sopt->sopt_name,
   1133 				    sopt->sopt_data, sopt->sopt_size,
   1134 				    curlwp->l_cred);
   1135 				break;
   1136 			}
   1137 			/*FALLTHROUGH*/
   1138 #endif /* IPSEC */
   1139 
   1140 		default:
   1141 			error = ENOPROTOOPT;
   1142 			break;
   1143 		}
   1144 		break;
   1145 
   1146 	case PRCO_GETOPT:
   1147 		switch (sopt->sopt_name) {
   1148 		case IP_OPTIONS:
   1149 		case IP_RETOPTS: {
   1150 			struct mbuf *mopts = inp->inp_options;
   1151 
   1152 			if (mopts) {
   1153 				struct mbuf *m;
   1154 
   1155 				m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
   1156 				if (m == NULL) {
   1157 					error = ENOBUFS;
   1158 					break;
   1159 				}
   1160 				error = sockopt_setmbuf(sopt, m);
   1161 			}
   1162 			break;
   1163 		}
   1164 		case IP_PKTINFO:
   1165 		case IP_TOS:
   1166 		case IP_TTL:
   1167 		case IP_MINTTL:
   1168 		case IP_RECVOPTS:
   1169 		case IP_RECVRETOPTS:
   1170 		case IP_RECVDSTADDR:
   1171 		case IP_RECVIF:
   1172 		case IP_RECVPKTINFO:
   1173 		case IP_RECVTTL:
   1174 		case IP_ERRORMTU:
   1175 			switch (sopt->sopt_name) {
   1176 			case IP_TOS:
   1177 				optval = ip->ip_tos;
   1178 				break;
   1179 
   1180 			case IP_TTL:
   1181 				optval = ip->ip_ttl;
   1182 				break;
   1183 
   1184 			case IP_MINTTL:
   1185 				optval = inp->inp_ip_minttl;
   1186 				break;
   1187 
   1188 			case IP_ERRORMTU:
   1189 				optval = inp->inp_errormtu;
   1190 				break;
   1191 
   1192 #define	OPTBIT(bit)	(inpflags & bit ? 1 : 0)
   1193 
   1194 			case IP_PKTINFO:
   1195 				optval = OPTBIT(INP_PKTINFO);
   1196 				break;
   1197 
   1198 			case IP_RECVOPTS:
   1199 				optval = OPTBIT(INP_RECVOPTS);
   1200 				break;
   1201 
   1202 			case IP_RECVPKTINFO:
   1203 				optval = OPTBIT(INP_RECVPKTINFO);
   1204 				break;
   1205 
   1206 			case IP_RECVRETOPTS:
   1207 				optval = OPTBIT(INP_RECVRETOPTS);
   1208 				break;
   1209 
   1210 			case IP_RECVDSTADDR:
   1211 				optval = OPTBIT(INP_RECVDSTADDR);
   1212 				break;
   1213 
   1214 			case IP_RECVIF:
   1215 				optval = OPTBIT(INP_RECVIF);
   1216 				break;
   1217 
   1218 			case IP_RECVTTL:
   1219 				optval = OPTBIT(INP_RECVTTL);
   1220 				break;
   1221 			}
   1222 			error = sockopt_setint(sopt, optval);
   1223 			break;
   1224 
   1225 #if 0	/* defined(IPSEC) */
   1226 		case IP_IPSEC_POLICY:
   1227 		{
   1228 			struct mbuf *m = NULL;
   1229 
   1230 			/* XXX this will return EINVAL as sopt is empty */
   1231 			error = ipsec4_get_policy(inp, sopt->sopt_data,
   1232 			    sopt->sopt_size, &m);
   1233 			if (error == 0)
   1234 				error = sockopt_setmbuf(sopt, m);
   1235 			break;
   1236 		}
   1237 #endif /*IPSEC*/
   1238 
   1239 		case IP_MULTICAST_IF:
   1240 		case IP_MULTICAST_TTL:
   1241 		case IP_MULTICAST_LOOP:
   1242 		case IP_ADD_MEMBERSHIP:
   1243 		case IP_DROP_MEMBERSHIP:
   1244 			error = ip_getmoptions(inp->inp_moptions, sopt);
   1245 			break;
   1246 
   1247 		case IP_PORTRANGE:
   1248 			if (inpflags & INP_LOWPORT)
   1249 				optval = IP_PORTRANGE_LOW;
   1250 			else
   1251 				optval = IP_PORTRANGE_DEFAULT;
   1252 			error = sockopt_setint(sopt, optval);
   1253 			break;
   1254 
   1255 		case IP_PORTALGO:
   1256 			optval = inp->inp_portalgo;
   1257 			error = sockopt_setint(sopt, optval);
   1258 			break;
   1259 
   1260 		default:
   1261 			error = ENOPROTOOPT;
   1262 			break;
   1263 		}
   1264 		break;
   1265 	}
   1266 
   1267 	if (!error) {
   1268 		inp->inp_flags = inpflags;
   1269 	}
   1270 	return error;
   1271 }
   1272 
   1273 /*
   1274  * Set up IP options in pcb for insertion in output packets.
   1275  * Store in mbuf with pointer in pcbopt, adding pseudo-option
   1276  * with destination address if source routed.
   1277  */
   1278 static int
   1279 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
   1280 {
   1281 	struct mbuf *m;
   1282 	const u_char *cp;
   1283 	u_char *dp;
   1284 	int cnt;
   1285 
   1286 	/* Turn off any old options. */
   1287 	if (inp->inp_options) {
   1288 		m_free(inp->inp_options);
   1289 	}
   1290 	inp->inp_options = NULL;
   1291 	if ((cnt = sopt->sopt_size) == 0) {
   1292 		/* Only turning off any previous options. */
   1293 		return 0;
   1294 	}
   1295 	cp = sopt->sopt_data;
   1296 
   1297 #ifndef	__vax__
   1298 	if (cnt % sizeof(int32_t))
   1299 		return (EINVAL);
   1300 #endif
   1301 
   1302 	m = m_get(M_DONTWAIT, MT_SOOPTS);
   1303 	if (m == NULL)
   1304 		return (ENOBUFS);
   1305 
   1306 	dp = mtod(m, u_char *);
   1307 	memset(dp, 0, sizeof(struct in_addr));
   1308 	dp += sizeof(struct in_addr);
   1309 	m->m_len = sizeof(struct in_addr);
   1310 
   1311 	/*
   1312 	 * IP option list according to RFC791. Each option is of the form
   1313 	 *
   1314 	 *	[optval] [olen] [(olen - 2) data bytes]
   1315 	 *
   1316 	 * We validate the list and copy options to an mbuf for prepending
   1317 	 * to data packets. The IP first-hop destination address will be
   1318 	 * stored before actual options and is zero if unset.
   1319 	 */
   1320 	while (cnt > 0) {
   1321 		uint8_t optval, olen, offset;
   1322 
   1323 		optval = cp[IPOPT_OPTVAL];
   1324 
   1325 		if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
   1326 			olen = 1;
   1327 		} else {
   1328 			if (cnt < IPOPT_OLEN + 1)
   1329 				goto bad;
   1330 
   1331 			olen = cp[IPOPT_OLEN];
   1332 			if (olen < IPOPT_OLEN + 1 || olen > cnt)
   1333 				goto bad;
   1334 		}
   1335 
   1336 		if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
   1337 			/*
   1338 			 * user process specifies route as:
   1339 			 *	->A->B->C->D
   1340 			 * D must be our final destination (but we can't
   1341 			 * check that since we may not have connected yet).
   1342 			 * A is first hop destination, which doesn't appear in
   1343 			 * actual IP option, but is stored before the options.
   1344 			 */
   1345 			if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
   1346 				goto bad;
   1347 
   1348 			offset = cp[IPOPT_OFFSET];
   1349 			memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
   1350 			    sizeof(struct in_addr));
   1351 
   1352 			cp += sizeof(struct in_addr);
   1353 			cnt -= sizeof(struct in_addr);
   1354 			olen -= sizeof(struct in_addr);
   1355 
   1356 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
   1357 				goto bad;
   1358 
   1359 			memcpy(dp, cp, olen);
   1360 			dp[IPOPT_OPTVAL] = optval;
   1361 			dp[IPOPT_OLEN] = olen;
   1362 			dp[IPOPT_OFFSET] = offset;
   1363 			break;
   1364 		} else {
   1365 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
   1366 				goto bad;
   1367 
   1368 			memcpy(dp, cp, olen);
   1369 			break;
   1370 		}
   1371 
   1372 		dp += olen;
   1373 		m->m_len += olen;
   1374 
   1375 		if (optval == IPOPT_EOL)
   1376 			break;
   1377 
   1378 		cp += olen;
   1379 		cnt -= olen;
   1380 	}
   1381 
   1382 	inp->inp_options = m;
   1383 	return 0;
   1384 bad:
   1385 	(void)m_free(m);
   1386 	return EINVAL;
   1387 }
   1388 
   1389 /*
   1390  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
   1391  */
   1392 static struct ifnet *
   1393 ip_multicast_if(struct in_addr *a, int *ifindexp)
   1394 {
   1395 	int ifindex;
   1396 	struct ifnet *ifp = NULL;
   1397 	struct in_ifaddr *ia;
   1398 
   1399 	if (ifindexp)
   1400 		*ifindexp = 0;
   1401 	if (ntohl(a->s_addr) >> 24 == 0) {
   1402 		ifindex = ntohl(a->s_addr) & 0xffffff;
   1403 		ifp = if_byindex(ifindex);
   1404 		if (!ifp)
   1405 			return NULL;
   1406 		if (ifindexp)
   1407 			*ifindexp = ifindex;
   1408 	} else {
   1409 		LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
   1410 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
   1411 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
   1412 				ifp = ia->ia_ifp;
   1413 				break;
   1414 			}
   1415 		}
   1416 	}
   1417 	return ifp;
   1418 }
   1419 
   1420 static int
   1421 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
   1422 {
   1423 	u_int tval;
   1424 	u_char cval;
   1425 	int error;
   1426 
   1427 	if (sopt == NULL)
   1428 		return EINVAL;
   1429 
   1430 	switch (sopt->sopt_size) {
   1431 	case sizeof(u_char):
   1432 		error = sockopt_get(sopt, &cval, sizeof(u_char));
   1433 		tval = cval;
   1434 		break;
   1435 
   1436 	case sizeof(u_int):
   1437 		error = sockopt_get(sopt, &tval, sizeof(u_int));
   1438 		break;
   1439 
   1440 	default:
   1441 		error = EINVAL;
   1442 	}
   1443 
   1444 	if (error)
   1445 		return error;
   1446 
   1447 	if (tval > maxval)
   1448 		return EINVAL;
   1449 
   1450 	*val = tval;
   1451 	return 0;
   1452 }
   1453 
   1454 static int
   1455 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
   1456     struct in_addr *ia, bool add)
   1457 {
   1458 	int error;
   1459 	struct ip_mreq mreq;
   1460 
   1461 	error = sockopt_get(sopt, &mreq, sizeof(mreq));
   1462 	if (error)
   1463 		return error;
   1464 
   1465 	if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
   1466 		return EINVAL;
   1467 
   1468 	memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
   1469 
   1470 	if (in_nullhost(mreq.imr_interface)) {
   1471 		union {
   1472 			struct sockaddr		dst;
   1473 			struct sockaddr_in	dst4;
   1474 		} u;
   1475 		struct route ro;
   1476 
   1477 		if (!add) {
   1478 			*ifp = NULL;
   1479 			return 0;
   1480 		}
   1481 		/*
   1482 		 * If no interface address was provided, use the interface of
   1483 		 * the route to the given multicast address.
   1484 		 */
   1485 		struct rtentry *rt;
   1486 		memset(&ro, 0, sizeof(ro));
   1487 
   1488 		sockaddr_in_init(&u.dst4, ia, 0);
   1489 		error = rtcache_setdst(&ro, &u.dst);
   1490 		if (error != 0)
   1491 			return error;
   1492 		*ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
   1493 		rtcache_free(&ro);
   1494 	} else {
   1495 		*ifp = ip_multicast_if(&mreq.imr_interface, NULL);
   1496 		if (!add && *ifp == NULL)
   1497 			return EADDRNOTAVAIL;
   1498 	}
   1499 	return 0;
   1500 }
   1501 
   1502 /*
   1503  * Add a multicast group membership.
   1504  * Group must be a valid IP multicast address.
   1505  */
   1506 static int
   1507 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
   1508 {
   1509 	struct ifnet *ifp = NULL;	// XXX: gcc [ppc]
   1510 	struct in_addr ia;
   1511 	int i, error;
   1512 
   1513 	if (sopt->sopt_size == sizeof(struct ip_mreq))
   1514 		error = ip_get_membership(sopt, &ifp, &ia, true);
   1515 	else
   1516 #ifdef INET6
   1517 		error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia));
   1518 #else
   1519 		return EINVAL;
   1520 #endif
   1521 
   1522 	if (error)
   1523 		return error;
   1524 
   1525 	/*
   1526 	 * See if we found an interface, and confirm that it
   1527 	 * supports multicast.
   1528 	 */
   1529 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
   1530 		return EADDRNOTAVAIL;
   1531 
   1532 	/*
   1533 	 * See if the membership already exists or if all the
   1534 	 * membership slots are full.
   1535 	 */
   1536 	for (i = 0; i < imo->imo_num_memberships; ++i) {
   1537 		if (imo->imo_membership[i]->inm_ifp == ifp &&
   1538 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
   1539 			break;
   1540 	}
   1541 	if (i < imo->imo_num_memberships)
   1542 		return EADDRINUSE;
   1543 
   1544 	if (i == IP_MAX_MEMBERSHIPS)
   1545 		return ETOOMANYREFS;
   1546 
   1547 	/*
   1548 	 * Everything looks good; add a new record to the multicast
   1549 	 * address list for the given interface.
   1550 	 */
   1551 	if ((imo->imo_membership[i] = in_addmulti(&ia, ifp)) == NULL)
   1552 		return ENOBUFS;
   1553 
   1554 	++imo->imo_num_memberships;
   1555 	return 0;
   1556 }
   1557 
   1558 /*
   1559  * Drop a multicast group membership.
   1560  * Group must be a valid IP multicast address.
   1561  */
   1562 static int
   1563 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
   1564 {
   1565 	struct in_addr ia = { .s_addr = 0 };	// XXX: gcc [ppc]
   1566 	struct ifnet *ifp = NULL;		// XXX: gcc [ppc]
   1567 	int i, error;
   1568 
   1569 	if (sopt->sopt_size == sizeof(struct ip_mreq))
   1570 		error = ip_get_membership(sopt, &ifp, &ia, false);
   1571 	else
   1572 #ifdef INET6
   1573 		error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia));
   1574 #else
   1575 		return EINVAL;
   1576 #endif
   1577 
   1578 	if (error)
   1579 		return error;
   1580 
   1581 	/*
   1582 	 * Find the membership in the membership array.
   1583 	 */
   1584 	for (i = 0; i < imo->imo_num_memberships; ++i) {
   1585 		if ((ifp == NULL ||
   1586 		     imo->imo_membership[i]->inm_ifp == ifp) &&
   1587 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
   1588 			break;
   1589 	}
   1590 	if (i == imo->imo_num_memberships)
   1591 		return EADDRNOTAVAIL;
   1592 
   1593 	/*
   1594 	 * Give up the multicast address record to which the
   1595 	 * membership points.
   1596 	 */
   1597 	in_delmulti(imo->imo_membership[i]);
   1598 
   1599 	/*
   1600 	 * Remove the gap in the membership array.
   1601 	 */
   1602 	for (++i; i < imo->imo_num_memberships; ++i)
   1603 		imo->imo_membership[i-1] = imo->imo_membership[i];
   1604 	--imo->imo_num_memberships;
   1605 	return 0;
   1606 }
   1607 
   1608 /*
   1609  * Set the IP multicast options in response to user setsockopt().
   1610  */
   1611 int
   1612 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
   1613 {
   1614 	struct ip_moptions *imo = *pimo;
   1615 	struct in_addr addr;
   1616 	struct ifnet *ifp;
   1617 	int ifindex, error = 0;
   1618 
   1619 	if (!imo) {
   1620 		/*
   1621 		 * No multicast option buffer attached to the pcb;
   1622 		 * allocate one and initialize to default values.
   1623 		 */
   1624 		imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
   1625 		if (imo == NULL)
   1626 			return ENOBUFS;
   1627 
   1628 		imo->imo_multicast_if_index = 0;
   1629 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1630 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
   1631 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
   1632 		imo->imo_num_memberships = 0;
   1633 		*pimo = imo;
   1634 	}
   1635 
   1636 	switch (sopt->sopt_name) {
   1637 	case IP_MULTICAST_IF:
   1638 		/*
   1639 		 * Select the interface for outgoing multicast packets.
   1640 		 */
   1641 		error = sockopt_get(sopt, &addr, sizeof(addr));
   1642 		if (error)
   1643 			break;
   1644 
   1645 		/*
   1646 		 * INADDR_ANY is used to remove a previous selection.
   1647 		 * When no interface is selected, a default one is
   1648 		 * chosen every time a multicast packet is sent.
   1649 		 */
   1650 		if (in_nullhost(addr)) {
   1651 			imo->imo_multicast_if_index = 0;
   1652 			break;
   1653 		}
   1654 		/*
   1655 		 * The selected interface is identified by its local
   1656 		 * IP address.  Find the interface and confirm that
   1657 		 * it supports multicasting.
   1658 		 */
   1659 		ifp = ip_multicast_if(&addr, &ifindex);
   1660 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
   1661 			error = EADDRNOTAVAIL;
   1662 			break;
   1663 		}
   1664 		imo->imo_multicast_if_index = ifp->if_index;
   1665 		if (ifindex)
   1666 			imo->imo_multicast_addr = addr;
   1667 		else
   1668 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
   1669 		break;
   1670 
   1671 	case IP_MULTICAST_TTL:
   1672 		/*
   1673 		 * Set the IP time-to-live for outgoing multicast packets.
   1674 		 */
   1675 		error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
   1676 		break;
   1677 
   1678 	case IP_MULTICAST_LOOP:
   1679 		/*
   1680 		 * Set the loopback flag for outgoing multicast packets.
   1681 		 * Must be zero or one.
   1682 		 */
   1683 		error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
   1684 		break;
   1685 
   1686 	case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
   1687 		error = ip_add_membership(imo, sopt);
   1688 		break;
   1689 
   1690 	case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
   1691 		error = ip_drop_membership(imo, sopt);
   1692 		break;
   1693 
   1694 	default:
   1695 		error = EOPNOTSUPP;
   1696 		break;
   1697 	}
   1698 
   1699 	/*
   1700 	 * If all options have default values, no need to keep the mbuf.
   1701 	 */
   1702 	if (imo->imo_multicast_if_index == 0 &&
   1703 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
   1704 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
   1705 	    imo->imo_num_memberships == 0) {
   1706 		kmem_free(imo, sizeof(*imo));
   1707 		*pimo = NULL;
   1708 	}
   1709 
   1710 	return error;
   1711 }
   1712 
   1713 /*
   1714  * Return the IP multicast options in response to user getsockopt().
   1715  */
   1716 int
   1717 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
   1718 {
   1719 	struct in_addr addr;
   1720 	uint8_t optval;
   1721 	int error = 0;
   1722 
   1723 	switch (sopt->sopt_name) {
   1724 	case IP_MULTICAST_IF:
   1725 		if (imo == NULL || imo->imo_multicast_if_index == 0)
   1726 			addr = zeroin_addr;
   1727 		else if (imo->imo_multicast_addr.s_addr) {
   1728 			/* return the value user has set */
   1729 			addr = imo->imo_multicast_addr;
   1730 		} else {
   1731 			struct ifnet *ifp;
   1732 			struct in_ifaddr *ia = NULL;
   1733 			int s = pserialize_read_enter();
   1734 
   1735 			ifp = if_byindex(imo->imo_multicast_if_index);
   1736 			if (ifp != NULL) {
   1737 				IFP_TO_IA(ifp, ia);
   1738 			}
   1739 			addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
   1740 			pserialize_read_exit(s);
   1741 		}
   1742 		error = sockopt_set(sopt, &addr, sizeof(addr));
   1743 		break;
   1744 
   1745 	case IP_MULTICAST_TTL:
   1746 		optval = imo ? imo->imo_multicast_ttl
   1747 		    : IP_DEFAULT_MULTICAST_TTL;
   1748 
   1749 		error = sockopt_set(sopt, &optval, sizeof(optval));
   1750 		break;
   1751 
   1752 	case IP_MULTICAST_LOOP:
   1753 		optval = imo ? imo->imo_multicast_loop
   1754 		    : IP_DEFAULT_MULTICAST_LOOP;
   1755 
   1756 		error = sockopt_set(sopt, &optval, sizeof(optval));
   1757 		break;
   1758 
   1759 	default:
   1760 		error = EOPNOTSUPP;
   1761 	}
   1762 
   1763 	return error;
   1764 }
   1765 
   1766 /*
   1767  * Discard the IP multicast options.
   1768  */
   1769 void
   1770 ip_freemoptions(struct ip_moptions *imo)
   1771 {
   1772 	int i;
   1773 
   1774 	if (imo != NULL) {
   1775 		for (i = 0; i < imo->imo_num_memberships; ++i)
   1776 			in_delmulti(imo->imo_membership[i]);
   1777 		kmem_free(imo, sizeof(*imo));
   1778 	}
   1779 }
   1780 
   1781 /*
   1782  * Routine called from ip_output() to loop back a copy of an IP multicast
   1783  * packet to the input queue of a specified interface.  Note that this
   1784  * calls the output routine of the loopback "driver", but with an interface
   1785  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
   1786  */
   1787 static void
   1788 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
   1789 {
   1790 	struct ip *ip;
   1791 	struct mbuf *copym;
   1792 
   1793 	copym = m_copypacket(m, M_DONTWAIT);
   1794 	if (copym != NULL &&
   1795 	    (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
   1796 		copym = m_pullup(copym, sizeof(struct ip));
   1797 	if (copym == NULL)
   1798 		return;
   1799 	/*
   1800 	 * We don't bother to fragment if the IP length is greater
   1801 	 * than the interface's MTU.  Can this possibly matter?
   1802 	 */
   1803 	ip = mtod(copym, struct ip *);
   1804 
   1805 	if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   1806 		in_delayed_cksum(copym);
   1807 		copym->m_pkthdr.csum_flags &=
   1808 		    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
   1809 	}
   1810 
   1811 	ip->ip_sum = 0;
   1812 	ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
   1813 #ifndef NET_MPSAFE
   1814 	KERNEL_LOCK(1, NULL);
   1815 #endif
   1816 	(void)looutput(ifp, copym, sintocsa(dst), NULL);
   1817 #ifndef NET_MPSAFE
   1818 	KERNEL_UNLOCK_ONE(NULL);
   1819 #endif
   1820 }
   1821