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