xref: /freebsd/sys/netinet/ip_output.c (revision 2f4afd21257ea1672763722dae9109bbaced5548)
1c398230bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
14df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
15df8bae1dSRodney W. Grimes  *    without specific prior written permission.
16df8bae1dSRodney W. Grimes  *
17df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30df8bae1dSRodney W. Grimes  */
31df8bae1dSRodney W. Grimes 
324b421e2dSMike Silbersack #include <sys/cdefs.h>
334b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
344b421e2dSMike Silbersack 
351f7e052cSJulian Elischer #include "opt_ipfw.h"
36be9347e3SAdrian Chadd #include "opt_inet.h"
376a800098SYoshinobu Inoue #include "opt_ipsec.h"
384ed84624SRobert Watson #include "opt_mac.h"
3953dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
40e440aed9SQing Li #include "opt_mpath.h"
412f4afd21SRandall Stewart #include "opt_sctp.h"
42fbd1372aSJoerg Wunsch 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
4426f9a767SRodney W. Grimes #include <sys/systm.h>
45f0f6d643SLuigi Rizzo #include <sys/kernel.h>
46df8bae1dSRodney W. Grimes #include <sys/malloc.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48acd3428bSRobert Watson #include <sys/priv.h>
49c26fe973SBjoern A. Zeeb #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/protosw.h>
51df8bae1dSRodney W. Grimes #include <sys/socket.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
539d9edc56SMike Silbersack #include <sys/sysctl.h>
54c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
55603724d3SBjoern A. Zeeb #include <sys/vimage.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <net/if.h>
589b932e9eSAndre Oppermann #include <net/netisr.h>
59c21fd232SAndre Oppermann #include <net/pfil.h>
60df8bae1dSRodney W. Grimes #include <net/route.h>
61e440aed9SQing Li #ifdef RADIX_MPATH
62e440aed9SQing Li #include <net/radix_mpath.h>
63e440aed9SQing Li #endif
644b79449eSBjoern A. Zeeb #include <net/vnet.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <netinet/in.h>
67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
68df8bae1dSRodney W. Grimes #include <netinet/ip.h>
69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
70df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
72ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
734b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
742f4afd21SRandall Stewart #ifdef SCTP
752f4afd21SRandall Stewart #include <netinet/sctp.h>
762f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
772f4afd21SRandall Stewart #endif
78df8bae1dSRodney W. Grimes 
79b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
802cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
811dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
82b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
836a800098SYoshinobu Inoue 
841dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
851dfcf0d2SAndre Oppermann 
86aed55708SRobert Watson #include <security/mac/mac_framework.h>
87aed55708SRobert Watson 
882b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
892b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
90f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
91f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
922b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
93f9e354dfSJulian Elischer 
9444e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
95f23b4c91SGarrett Wollman u_short ip_id;
9644e33a07SMarko Zec #endif
97f23b4c91SGarrett Wollman 
9853dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
999d9edc56SMike Silbersack int mbuf_frag_size = 0;
1009d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1019d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1029d9edc56SMike Silbersack #endif
1039d9edc56SMike Silbersack 
104be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND)
105be9347e3SAdrian Chadd static int ip_nonlocalok = 0;
106be9347e3SAdrian Chadd SYSCTL_INT(_net_inet_ip, OID_AUTO, nonlocalok,
107be9347e3SAdrian Chadd 	CTLFLAG_RW|CTLFLAG_SECURE, &ip_nonlocalok, 0, "");
108be9347e3SAdrian Chadd #endif
109be9347e3SAdrian Chadd 
110df8bae1dSRodney W. Grimes static void	ip_mloopback
1114d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
112afed1b49SDarren Reed 
113afed1b49SDarren Reed 
11493e0e116SJulian Elischer extern	struct protosw inetsw[];
11593e0e116SJulian Elischer 
116df8bae1dSRodney W. Grimes /*
117df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
118df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
119df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
120df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1213de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1223de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
123df8bae1dSRodney W. Grimes  */
124df8bae1dSRodney W. Grimes int
125f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
126f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
127df8bae1dSRodney W. Grimes {
1288b615593SMarko Zec 	INIT_VNET_NET(curvnet);
1298b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1301e78ac21SJeffrey Hsu 	struct ip *ip;
1312b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
132ac9d7e26SMax Laier 	struct mbuf *m0;
13323bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1343ae2ad08SJohn-Mark Gurney 	int mtu;
1359b932e9eSAndre Oppermann 	int len, error = 0;
1362b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1373956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
138db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
139e3f406b3SRuslan Ermilov 	struct route iproute;
1409b932e9eSAndre Oppermann 	struct in_addr odst;
1419b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1429b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1439b932e9eSAndre Oppermann #endif
144ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
14536e8826fSMax Laier 
14602c1c707SAndre Oppermann 	if (ro == NULL) {
14702c1c707SAndre Oppermann 		ro = &iproute;
14802c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
14902c1c707SAndre Oppermann 	}
15002c1c707SAndre Oppermann 
151bc97ba51SJulian Elischer 	if (inp != NULL) {
152bc97ba51SJulian Elischer 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
153baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
154bc97ba51SJulian Elischer 	}
1552b25acc1SLuigi Rizzo 
156df8bae1dSRodney W. Grimes 	if (opt) {
157cb7641e8SMaxim Konovalov 		len = 0;
158df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
159cb7641e8SMaxim Konovalov 		if (len != 0)
160df8bae1dSRodney W. Grimes 			hlen = len;
161df8bae1dSRodney W. Grimes 	}
162df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1633efc3014SJulian Elischer 
164df8bae1dSRodney W. Grimes 	/*
1656e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
166e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
167e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
168e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
169e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1706e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1716e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1726e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1734d09f5a0SGleb Smirnoff 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
174df8bae1dSRodney W. Grimes 	 */
175df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
17653be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
17753be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1781f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
179603724d3SBjoern A. Zeeb 		V_ipstat.ips_localout++;
180df8bae1dSRodney W. Grimes 	} else {
18153be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
182df8bae1dSRodney W. Grimes 	}
1839c9137eaSGarrett Wollman 
184df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1859b932e9eSAndre Oppermann again:
186df8bae1dSRodney W. Grimes 	/*
187df8bae1dSRodney W. Grimes 	 * If there is a cached route,
188df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
189df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
190a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
191a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
192df8bae1dSRodney W. Grimes 	 */
193df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
194a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1959b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
196df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
1973ae2ad08SJohn-Mark Gurney 		ro->ro_rt = (struct rtentry *)NULL;
198df8bae1dSRodney W. Grimes 	}
1999b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
2009b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
2019b932e9eSAndre Oppermann #else
2020b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
2039b932e9eSAndre Oppermann #endif
204a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
205df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
206df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2079b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
208df8bae1dSRodney W. Grimes 	}
209df8bae1dSRodney W. Grimes 	/*
210a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
211a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
212a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
213a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
214df8bae1dSRodney W. Grimes 	 */
215a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
216a3fd02d8SBruce M Simpson 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
217a3fd02d8SBruce M Simpson 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
218603724d3SBjoern A. Zeeb 			V_ipstat.ips_noroute++;
219a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
220a3fd02d8SBruce M Simpson 			goto bad;
221a3fd02d8SBruce M Simpson 		}
222a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
223a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
224a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
225a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
226a3fd02d8SBruce M Simpson 		isbroadcast = 1;
227a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
2280b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2290b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
230603724d3SBjoern A. Zeeb 			V_ipstat.ips_noroute++;
231df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
232df8bae1dSRodney W. Grimes 			goto bad;
233df8bae1dSRodney W. Grimes 		}
234df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
235df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2369f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2373afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
23838c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2393afefa39SDaniel C. Sobral 		/*
24038c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
24138c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2423afefa39SDaniel C. Sobral 		 */
24338c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
24438c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
24538c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
246df8bae1dSRodney W. Grimes 	} else {
2472c17fe93SGarrett Wollman 		/*
24897d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
24997d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
25097d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2512c17fe93SGarrett Wollman 		 */
2520b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
253e440aed9SQing Li #ifdef RADIX_MPATH
2548b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
2558b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
2568b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
257e440aed9SQing Li #else
2588b07e49aSJulian Elischer 			in_rtalloc_ign(ro, 0,
2598b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
260e440aed9SQing Li #endif
2610b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
262603724d3SBjoern A. Zeeb 			V_ipstat.ips_noroute++;
263df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
264df8bae1dSRodney W. Grimes 			goto bad;
265df8bae1dSRodney W. Grimes 		}
266df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
267df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
26897d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
269df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
270f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2719f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
272f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2739f9b3dc4SGarrett Wollman 		else
2749f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
275df8bae1dSRodney W. Grimes 	}
2763ae2ad08SJohn-Mark Gurney 	/*
2773ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
2783ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
2793ae2ad08SJohn-Mark Gurney 	 */
280384a05bfSAndre Oppermann 	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
2813ae2ad08SJohn-Mark Gurney 		/*
2823ae2ad08SJohn-Mark Gurney 		 * This case can happen if the user changed the MTU
2833ae2ad08SJohn-Mark Gurney 		 * of an interface after enabling IP on it.  Because
2843ae2ad08SJohn-Mark Gurney 		 * most netifs don't keep track of routes pointing to
2853ae2ad08SJohn-Mark Gurney 		 * them, there is no way for one to update all its
2863ae2ad08SJohn-Mark Gurney 		 * routes when the MTU is changed.
2873ae2ad08SJohn-Mark Gurney 		 */
2883ae2ad08SJohn-Mark Gurney 		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
2893ae2ad08SJohn-Mark Gurney 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
2903ae2ad08SJohn-Mark Gurney 		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
2913ae2ad08SJohn-Mark Gurney 	} else {
2923ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
2933ae2ad08SJohn-Mark Gurney 	}
2949b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
295df8bae1dSRodney W. Grimes 		struct in_multi *inm;
296df8bae1dSRodney W. Grimes 
297df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
298df8bae1dSRodney W. Grimes 		/*
299df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
300df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
301df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
302df8bae1dSRodney W. Grimes 		 */
303df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
304df8bae1dSRodney W. Grimes 		/*
305df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
306df8bae1dSRodney W. Grimes 		 */
307df8bae1dSRodney W. Grimes 		if (imo != NULL) {
308df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3091c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3101c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
311bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
312bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
313bbb4330bSLuigi Rizzo 				    INADDR_ANY;
314df8bae1dSRodney W. Grimes 		} else
315df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
316df8bae1dSRodney W. Grimes 		/*
317df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
318df8bae1dSRodney W. Grimes 		 */
3191c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
320df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
321603724d3SBjoern A. Zeeb 				V_ipstat.ips_noroute++;
322df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
323df8bae1dSRodney W. Grimes 				goto bad;
324df8bae1dSRodney W. Grimes 			}
3251c5de19aSGarrett Wollman 		}
326df8bae1dSRodney W. Grimes 		/*
327df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
328df8bae1dSRodney W. Grimes 		 * of outgoing interface.
329df8bae1dSRodney W. Grimes 		 */
330df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
33138c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
33238c1bc35SRuslan Ermilov 			if (ia != NULL)
33338c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
334df8bae1dSRodney W. Grimes 		}
335df8bae1dSRodney W. Grimes 
336dd5a318bSRobert Watson 		IN_MULTI_LOCK();
3379b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
338df8bae1dSRodney W. Grimes 		if (inm != NULL &&
339df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
340dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
341df8bae1dSRodney W. Grimes 			/*
342df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
343df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
344df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
345df8bae1dSRodney W. Grimes 			 */
34686b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
347df8bae1dSRodney W. Grimes 		}
348df8bae1dSRodney W. Grimes 		else {
349dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
350df8bae1dSRodney W. Grimes 			/*
351df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
352df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
353df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
354df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
355df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
356df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
357df8bae1dSRodney W. Grimes 			 *
358df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
359df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
360df8bae1dSRodney W. Grimes 			 * if necessary.
361df8bae1dSRodney W. Grimes 			 */
362603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
363f0068c4aSGarrett Wollman 				/*
364bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
365f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
366f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
367f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
368f0068c4aSGarrett Wollman 				 */
369603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
370f0068c4aSGarrett Wollman 					imo = NULL;
371bbb4330bSLuigi Rizzo 				if (ip_mforward &&
372bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
373df8bae1dSRodney W. Grimes 					m_freem(m);
374df8bae1dSRodney W. Grimes 					goto done;
375df8bae1dSRodney W. Grimes 				}
376df8bae1dSRodney W. Grimes 			}
377df8bae1dSRodney W. Grimes 		}
3785e9ae478SGarrett Wollman 
379df8bae1dSRodney W. Grimes 		/*
380df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
381df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
382df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
383df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
384df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
385df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
386df8bae1dSRodney W. Grimes 		 */
387f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
388df8bae1dSRodney W. Grimes 			m_freem(m);
389df8bae1dSRodney W. Grimes 			goto done;
390df8bae1dSRodney W. Grimes 		}
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes 		goto sendit;
393df8bae1dSRodney W. Grimes 	}
3946a7c943cSAndre Oppermann 
395df8bae1dSRodney W. Grimes 	/*
3962b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
397cb459254SJohn-Mark Gurney 	 * of the outoing interface.
398df8bae1dSRodney W. Grimes 	 */
399f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
40038c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
40138c1bc35SRuslan Ermilov 		if (ia != NULL) {
402df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
403f9e354dfSJulian Elischer 		}
40438c1bc35SRuslan Ermilov 	}
4056a7c943cSAndre Oppermann 
406ca7a789aSMax Laier 	/*
407ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
408ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
409ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
410ca7a789aSMax Laier 	 */
41102b199f1SMax Laier #ifdef ALTQ
412ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
4133ae2ad08SJohn-Mark Gurney 	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
414ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
415ca7a789aSMax Laier #else
4163ae2ad08SJohn-Mark Gurney 	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
417ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
418ca7a789aSMax Laier #endif /* ALTQ */
419ca7a789aSMax Laier 	{
420b5390296SDavid Greenman 		error = ENOBUFS;
421603724d3SBjoern A. Zeeb 		V_ipstat.ips_odropped++;
422bbce982bSGleb Smirnoff 		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
423b5390296SDavid Greenman 		goto bad;
424b5390296SDavid Greenman 	}
425b5390296SDavid Greenman 
426b5390296SDavid Greenman 	/*
427df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4288c3f5566SRuslan Ermilov 	 * verify user is allowed to send
429df8bae1dSRodney W. Grimes 	 * such a packet.
430df8bae1dSRodney W. Grimes 	 */
4319f9b3dc4SGarrett Wollman 	if (isbroadcast) {
432df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
433df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
434df8bae1dSRodney W. Grimes 			goto bad;
435df8bae1dSRodney W. Grimes 		}
436df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
437df8bae1dSRodney W. Grimes 			error = EACCES;
438df8bae1dSRodney W. Grimes 			goto bad;
439df8bae1dSRodney W. Grimes 		}
440df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4413ae2ad08SJohn-Mark Gurney 		if (ip->ip_len > mtu) {
442df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
443df8bae1dSRodney W. Grimes 			goto bad;
444df8bae1dSRodney W. Grimes 		}
445df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4469f9b3dc4SGarrett Wollman 	} else {
447df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4489f9b3dc4SGarrett Wollman 	}
449df8bae1dSRodney W. Grimes 
450f1743588SDarren Reed sendit:
451b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
4521dfcf0d2SAndre Oppermann 	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
4531dfcf0d2SAndre Oppermann 	case 1:
45433841545SHajimu UMEMOTO 		goto bad;
4551dfcf0d2SAndre Oppermann 	case -1:
4561dfcf0d2SAndre Oppermann 		goto done;
4571dfcf0d2SAndre Oppermann 	case 0:
45833841545SHajimu UMEMOTO 	default:
4591dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
46033841545SHajimu UMEMOTO 	}
4611dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
46233841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
46333841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
464b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
46533841545SHajimu UMEMOTO 
466c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
467604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
468c21fd232SAndre Oppermann 		goto passout;
469c21fd232SAndre Oppermann 
470c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
4719b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
472d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
473134ea224SSam Leffler 	if (error != 0 || m == NULL)
474c4ac87eaSDarren Reed 		goto done;
4759b932e9eSAndre Oppermann 
476c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
477fed1c7e9SSøren Schmidt 
4789b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
4799b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
4809b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
481f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
4829b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
4839b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
484f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
485603724d3SBjoern A. Zeeb 				m->m_pkthdr.rcvif = V_loif;
48620c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
48720c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
48820c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
489ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
49020c822f3SJonathan Lemon 			}
49120c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
49220c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4932f4afd21SRandall Stewart #ifdef SCTP
4942f4afd21SRandall Stewart 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
4952f4afd21SRandall Stewart 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
4962f4afd21SRandall Stewart #endif
4979b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
4989b932e9eSAndre Oppermann 			goto done;
4999b932e9eSAndre Oppermann 		} else
500f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
5019b932e9eSAndre Oppermann 	}
5029b932e9eSAndre Oppermann 
5039b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5049b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
5059b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5069b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
507603724d3SBjoern A. Zeeb 			m->m_pkthdr.rcvif = V_loif;
5089b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
5099b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
5109b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
5119b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
5129b932e9eSAndre Oppermann 		}
5132f4afd21SRandall Stewart #ifdef SCTP
5142f4afd21SRandall Stewart 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
5152f4afd21SRandall Stewart 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
5162f4afd21SRandall Stewart #endif
5179b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
5189b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
5199b932e9eSAndre Oppermann 
5209b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
521f9e354dfSJulian Elischer 		goto done;
522f9e354dfSJulian Elischer 	}
5239b932e9eSAndre Oppermann 	/* Or forward to some other address? */
5249b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
5259b932e9eSAndre Oppermann 	if (fwd_tag) {
5269b932e9eSAndre Oppermann 		dst = (struct sockaddr_in *)&ro->ro_dst;
5279b932e9eSAndre Oppermann 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
5289b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
5299b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
5309b932e9eSAndre Oppermann 		goto again;
531099dd043SAndre Oppermann 	}
532099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5332b25acc1SLuigi Rizzo 
534c21fd232SAndre Oppermann passout:
53551c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
53651c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
53751c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
53851c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
539603724d3SBjoern A. Zeeb 			V_ipstat.ips_badaddr++;
54051c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
54151c8ec4aSRuslan Ermilov 			goto bad;
54251c8ec4aSRuslan Ermilov 		}
54351c8ec4aSRuslan Ermilov 	}
54451c8ec4aSRuslan Ermilov 
545206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
546206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
547db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
548db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
549db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
550db4f9cc7SJonathan Lemon 	}
5512f4afd21SRandall Stewart #ifdef SCTP
5522f4afd21SRandall Stewart 	if (sw_csum & CSUM_SCTP) {
5532f4afd21SRandall Stewart 		sctp_delayed_cksum(m);
5542f4afd21SRandall Stewart 		sw_csum &= ~CSUM_SCTP;
5552f4afd21SRandall Stewart 	}
5562f4afd21SRandall Stewart #endif
557206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
558db4f9cc7SJonathan Lemon 
559e7319babSPoul-Henning Kamp 	/*
560db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
561233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
562df8bae1dSRodney W. Grimes 	 */
5633ae2ad08SJohn-Mark Gurney 	if (ip->ip_len <= mtu ||
564233dcce1SAndre Oppermann 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
565233dcce1SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
566fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
567fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
568df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
56953be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
570df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
5715da9f8faSJosef Karthauser 
572233dcce1SAndre Oppermann 		/*
573233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
574233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
575233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
576233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
577233dcce1SAndre Oppermann 		 */
57838c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
579233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
5803dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets +=
581233dcce1SAndre Oppermann 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
582233dcce1SAndre Oppermann 			else
5833dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5843dbee59bSBruce M Simpson 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
5855da9f8faSJosef Karthauser 		}
58653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
5873390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
5883390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
5899d9edc56SMike Silbersack #endif
590147f74d1SAndre Oppermann 		/*
591147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
592147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
593147f74d1SAndre Oppermann 		 */
594147f74d1SAndre Oppermann 		m->m_flags &= ~(M_PROTOFLAGS);
595df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
596df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
597df8bae1dSRodney W. Grimes 		goto done;
598df8bae1dSRodney W. Grimes 	}
5991e78ac21SJeffrey Hsu 
600233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
601233dcce1SAndre Oppermann 	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
602df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
603603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
604df8bae1dSRodney W. Grimes 		goto bad;
605df8bae1dSRodney W. Grimes 	}
6061e78ac21SJeffrey Hsu 
6071e78ac21SJeffrey Hsu 	/*
6081e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6091e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6101e78ac21SJeffrey Hsu 	 */
6113ae2ad08SJohn-Mark Gurney 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
6121e78ac21SJeffrey Hsu 	if (error)
613df8bae1dSRodney W. Grimes 		goto bad;
6141e78ac21SJeffrey Hsu 	for (; m; m = m0) {
615df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
616b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
61707203494SDaniel C. Sobral 		if (error == 0) {
618fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
61907203494SDaniel C. Sobral 			if (ia != NULL) {
6203dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
6213dbee59bSBruce M Simpson 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
62207203494SDaniel C. Sobral 			}
623147f74d1SAndre Oppermann 			/*
624147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
625147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
626147f74d1SAndre Oppermann 			 */
627147f74d1SAndre Oppermann 			m->m_flags &= ~(M_PROTOFLAGS);
628fe937674SJosef Karthauser 
629df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
630df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
631fe937674SJosef Karthauser 		} else
632df8bae1dSRodney W. Grimes 			m_freem(m);
633df8bae1dSRodney W. Grimes 	}
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes 	if (error == 0)
636603724d3SBjoern A. Zeeb 		V_ipstat.ips_fragmented++;
6371e78ac21SJeffrey Hsu 
638df8bae1dSRodney W. Grimes done:
6396a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
6406a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
641ac9d7e26SMax Laier 	}
642df8bae1dSRodney W. Grimes 	return (error);
643df8bae1dSRodney W. Grimes bad:
6443528d68fSBill Paul 	m_freem(m);
645df8bae1dSRodney W. Grimes 	goto done;
646df8bae1dSRodney W. Grimes }
647df8bae1dSRodney W. Grimes 
6481e78ac21SJeffrey Hsu /*
6491e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
6501e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
6511e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
6521e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
6531e78ac21SJeffrey Hsu  *
6541e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
6551e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
6561e78ac21SJeffrey Hsu  */
6571e78ac21SJeffrey Hsu int
6581e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
6591e78ac21SJeffrey Hsu     u_long if_hwassist_flags, int sw_csum)
6601e78ac21SJeffrey Hsu {
6618b615593SMarko Zec 	INIT_VNET_INET(curvnet);
6621e78ac21SJeffrey Hsu 	int error = 0;
6631e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
6641e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
6651e78ac21SJeffrey Hsu 	int off;
6661e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
6671e78ac21SJeffrey Hsu 	int firstlen;
6681e78ac21SJeffrey Hsu 	struct mbuf **mnext;
6691e78ac21SJeffrey Hsu 	int nfrags;
6701e78ac21SJeffrey Hsu 
6711e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
672603724d3SBjoern A. Zeeb 		V_ipstat.ips_cantfrag++;
6731e78ac21SJeffrey Hsu 		return EMSGSIZE;
6741e78ac21SJeffrey Hsu 	}
6751e78ac21SJeffrey Hsu 
6761e78ac21SJeffrey Hsu 	/*
6771e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
6781e78ac21SJeffrey Hsu 	 */
6791e78ac21SJeffrey Hsu 	if (len < 8)
6801e78ac21SJeffrey Hsu 		return EMSGSIZE;
6811e78ac21SJeffrey Hsu 
6821e78ac21SJeffrey Hsu 	/*
6831e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
6841e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
6851e78ac21SJeffrey Hsu 	 */
6861e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
6871e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
6881e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
6891e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
6901e78ac21SJeffrey Hsu 	}
6912f4afd21SRandall Stewart #ifdef SCTP
6922f4afd21SRandall Stewart 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP &&
6932f4afd21SRandall Stewart 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
6942f4afd21SRandall Stewart 		sctp_delayed_cksum(m0);
6952f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
6962f4afd21SRandall Stewart 	}
6972f4afd21SRandall Stewart #endif
6981e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
6991e78ac21SJeffrey Hsu 		/*
7001e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
7011e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
7021e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
7031e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
7041e78ac21SJeffrey Hsu 		 *
7051e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
7061e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
7071e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
7081e78ac21SJeffrey Hsu 		 */
7091e78ac21SJeffrey Hsu 		int newlen;
7101e78ac21SJeffrey Hsu 		struct mbuf *m;
7111e78ac21SJeffrey Hsu 
7121e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
7131e78ac21SJeffrey Hsu 			off += m->m_len;
7141e78ac21SJeffrey Hsu 
7151e78ac21SJeffrey Hsu 		/*
7161e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
7171e78ac21SJeffrey Hsu 		 * 8-byte boundary
7181e78ac21SJeffrey Hsu 		 */
7191e78ac21SJeffrey Hsu 		if (off < hlen)
7201e78ac21SJeffrey Hsu 			goto smart_frag_failure;
7211e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
7221e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
7231e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
7241e78ac21SJeffrey Hsu 			/* we failed, go back the default */
7251e78ac21SJeffrey Hsu smart_frag_failure:
7261e78ac21SJeffrey Hsu 			newlen = len;
7271e78ac21SJeffrey Hsu 			off = hlen + len;
7281e78ac21SJeffrey Hsu 		}
7291e78ac21SJeffrey Hsu 		len = newlen;
7301e78ac21SJeffrey Hsu 
7311e78ac21SJeffrey Hsu 	} else {
7321e78ac21SJeffrey Hsu 		off = hlen + len;
7331e78ac21SJeffrey Hsu 	}
7341e78ac21SJeffrey Hsu 
7351e78ac21SJeffrey Hsu 	firstlen = off - hlen;
7361e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
7371e78ac21SJeffrey Hsu 
7381e78ac21SJeffrey Hsu 	/*
7391e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
7401e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
7411e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
7421e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
7431e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
7441e78ac21SJeffrey Hsu 	 */
7451e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
7461e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
7471e78ac21SJeffrey Hsu 		struct mbuf *m;
7481e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
7491e78ac21SJeffrey Hsu 
75034333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
7510b17fba7SAndre Oppermann 		if (m == NULL) {
7521e78ac21SJeffrey Hsu 			error = ENOBUFS;
753603724d3SBjoern A. Zeeb 			V_ipstat.ips_odropped++;
7541e78ac21SJeffrey Hsu 			goto done;
7551e78ac21SJeffrey Hsu 		}
7561e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
7571e78ac21SJeffrey Hsu 		/*
7581e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
7591e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
7601e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
7611e78ac21SJeffrey Hsu 		 */
7621e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
7631e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
7641e78ac21SJeffrey Hsu 		*mhip = *ip;
7651e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
7661e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
7671e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
7681e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
7691e78ac21SJeffrey Hsu 		}
7701e78ac21SJeffrey Hsu 		m->m_len = mhlen;
7711e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
7721e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
7731e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
7741e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
7751e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
7761e78ac21SJeffrey Hsu 		} else
7771e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
7781e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
7791e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
7800b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
7811e78ac21SJeffrey Hsu 			m_free(m);
7821e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
783603724d3SBjoern A. Zeeb 			V_ipstat.ips_odropped++;
7841e78ac21SJeffrey Hsu 			goto done;
7851e78ac21SJeffrey Hsu 		}
7861e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
787fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
7881e78ac21SJeffrey Hsu #ifdef MAC
78930d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
7901e78ac21SJeffrey Hsu #endif
7911e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
7921e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
7931e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
7941e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
7951e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
7961e78ac21SJeffrey Hsu 		*mnext = m;
7971e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
7981e78ac21SJeffrey Hsu 	}
799603724d3SBjoern A. Zeeb 	V_ipstat.ips_ofragments += nfrags;
8001e78ac21SJeffrey Hsu 
8011e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
8021e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
8031e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
8041e78ac21SJeffrey Hsu 
8051e78ac21SJeffrey Hsu 	/*
8061e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
8071e78ac21SJeffrey Hsu 	 * and updating header.
8081e78ac21SJeffrey Hsu 	 */
8091e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
8101e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
8111e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
8121e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
8131e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
8141e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
8151e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
8161e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
8171e78ac21SJeffrey Hsu 
8181e78ac21SJeffrey Hsu done:
8191e78ac21SJeffrey Hsu 	*m_frag = m0;
8201e78ac21SJeffrey Hsu 	return error;
8211e78ac21SJeffrey Hsu }
8221e78ac21SJeffrey Hsu 
8231c238475SJonathan Lemon void
824db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
825db4f9cc7SJonathan Lemon {
826db4f9cc7SJonathan Lemon 	struct ip *ip;
827db4f9cc7SJonathan Lemon 	u_short csum, offset;
828db4f9cc7SJonathan Lemon 
829db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
83053be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
831db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
832206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
833206a3274SRuslan Ermilov 		csum = 0xffff;
834db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
835db4f9cc7SJonathan Lemon 
836db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
837db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
838db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
839db4f9cc7SJonathan Lemon 		/*
840db4f9cc7SJonathan Lemon 		 * XXX
841db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
842db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
8438f8d29f6SAndre Oppermann 		 * in the appropriate next mbuf in the chain.
844db4f9cc7SJonathan Lemon 		 */
8458f8d29f6SAndre Oppermann 		return;
846db4f9cc7SJonathan Lemon 	}
847db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
848db4f9cc7SJonathan Lemon }
849db4f9cc7SJonathan Lemon 
850df8bae1dSRodney W. Grimes /*
851df8bae1dSRodney W. Grimes  * IP socket option processing.
852df8bae1dSRodney W. Grimes  */
853df8bae1dSRodney W. Grimes int
854f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
855df8bae1dSRodney W. Grimes {
856cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
857cfe8b629SGarrett Wollman 	int	error, optval;
858df8bae1dSRodney W. Grimes 
859cfe8b629SGarrett Wollman 	error = optval = 0;
860cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
861bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
862bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
863bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
864bc97ba51SJulian Elischer 			return (0);
865bc97ba51SJulian Elischer 		}
866cfe8b629SGarrett Wollman 		return (EINVAL);
867cfe8b629SGarrett Wollman 	}
868df8bae1dSRodney W. Grimes 
869cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
870cfe8b629SGarrett Wollman 	case SOPT_SET:
871cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
872df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
873df8bae1dSRodney W. Grimes #ifdef notyet
874df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
875df8bae1dSRodney W. Grimes #endif
876cfe8b629SGarrett Wollman 		{
877cfe8b629SGarrett Wollman 			struct mbuf *m;
878cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
879cfe8b629SGarrett Wollman 				error = EMSGSIZE;
880cfe8b629SGarrett Wollman 				break;
881cfe8b629SGarrett Wollman 			}
882ea26d587SRuslan Ermilov 			MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
8830b17fba7SAndre Oppermann 			if (m == NULL) {
884cfe8b629SGarrett Wollman 				error = ENOBUFS;
885cfe8b629SGarrett Wollman 				break;
886cfe8b629SGarrett Wollman 			}
887cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
888cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
889cfe8b629SGarrett Wollman 					    m->m_len);
890635354c4SMaxim Konovalov 			if (error) {
891635354c4SMaxim Konovalov 				m_free(m);
892635354c4SMaxim Konovalov 				break;
893635354c4SMaxim Konovalov 			}
8948501a69cSRobert Watson 			INP_WLOCK(inp);
895993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
8968501a69cSRobert Watson 			INP_WUNLOCK(inp);
897993d9505SRobert Watson 			return (error);
898cfe8b629SGarrett Wollman 		}
899df8bae1dSRodney W. Grimes 
900be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND)
901be9347e3SAdrian Chadd 		case IP_NONLOCALOK:
902be9347e3SAdrian Chadd 			if (! ip_nonlocalok) {
903be9347e3SAdrian Chadd 				error = ENOPROTOOPT;
904be9347e3SAdrian Chadd 				break;
905be9347e3SAdrian Chadd 			}
906cef27294SAdrian Chadd 			/* FALLTHROUGH */
907be9347e3SAdrian Chadd #endif
908df8bae1dSRodney W. Grimes 		case IP_TOS:
909df8bae1dSRodney W. Grimes 		case IP_TTL:
910936cd18dSAndre Oppermann 		case IP_MINTTL:
911df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
912df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
913df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
9144957466bSMatthew N. Dodd 		case IP_RECVTTL:
91582c23ebaSBill Fenner 		case IP_RECVIF:
9166a800098SYoshinobu Inoue 		case IP_FAITH:
9178afa2304SBruce M Simpson 		case IP_ONESBCAST:
918b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
919cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
920cfe8b629SGarrett Wollman 					    sizeof optval);
921cfe8b629SGarrett Wollman 			if (error)
922cfe8b629SGarrett Wollman 				break;
923df8bae1dSRodney W. Grimes 
924cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
925df8bae1dSRodney W. Grimes 			case IP_TOS:
926ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
927df8bae1dSRodney W. Grimes 				break;
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 			case IP_TTL:
930ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
931df8bae1dSRodney W. Grimes 				break;
932936cd18dSAndre Oppermann 
933936cd18dSAndre Oppermann 			case IP_MINTTL:
934a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
935936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
936936cd18dSAndre Oppermann 				else
937936cd18dSAndre Oppermann 					error = EINVAL;
938936cd18dSAndre Oppermann 				break;
939936cd18dSAndre Oppermann 
940a138d217SRobert Watson #define	OPTSET(bit) do {						\
9418501a69cSRobert Watson 	INP_WLOCK(inp);							\
942df8bae1dSRodney W. Grimes 	if (optval)							\
943df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
944df8bae1dSRodney W. Grimes 	else								\
945a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
9468501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
947a138d217SRobert Watson } while (0)
948df8bae1dSRodney W. Grimes 
949df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
950df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
951df8bae1dSRodney W. Grimes 				break;
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
954df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
955df8bae1dSRodney W. Grimes 				break;
956df8bae1dSRodney W. Grimes 
957df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
958df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
959df8bae1dSRodney W. Grimes 				break;
96082c23ebaSBill Fenner 
9614957466bSMatthew N. Dodd 			case IP_RECVTTL:
9624957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
9634957466bSMatthew N. Dodd 				break;
9644957466bSMatthew N. Dodd 
96582c23ebaSBill Fenner 			case IP_RECVIF:
96682c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
96782c23ebaSBill Fenner 				break;
9686a800098SYoshinobu Inoue 
9696a800098SYoshinobu Inoue 			case IP_FAITH:
9706a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
9716a800098SYoshinobu Inoue 				break;
9728afa2304SBruce M Simpson 
9738afa2304SBruce M Simpson 			case IP_ONESBCAST:
9748afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
9758afa2304SBruce M Simpson 				break;
976b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
977b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
978b2828ad2SAndre Oppermann 				break;
979be9347e3SAdrian Chadd #if defined(IP_NONLOCALBIND)
980be9347e3SAdrian Chadd 			case IP_NONLOCALOK:
981be9347e3SAdrian Chadd 				OPTSET(INP_NONLOCALOK);
982be9347e3SAdrian Chadd 				break;
983be9347e3SAdrian Chadd #endif
984df8bae1dSRodney W. Grimes 			}
985df8bae1dSRodney W. Grimes 			break;
986df8bae1dSRodney W. Grimes #undef OPTSET
987df8bae1dSRodney W. Grimes 
98871498f30SBruce M Simpson 		/*
98971498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
99071498f30SBruce M Simpson 		 * module.
99171498f30SBruce M Simpson 		 */
992df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
993f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
994df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
995df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
996df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
997df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
99871498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
99971498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
100071498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
100171498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
100271498f30SBruce M Simpson 		case IP_MSFILTER:
100371498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
100471498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
100571498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
100671498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
100771498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
100871498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
100971498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1010df8bae1dSRodney W. Grimes 			break;
1011df8bae1dSRodney W. Grimes 
101233b3ac06SPeter Wemm 		case IP_PORTRANGE:
1013cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1014cfe8b629SGarrett Wollman 					    sizeof optval);
1015cfe8b629SGarrett Wollman 			if (error)
1016cfe8b629SGarrett Wollman 				break;
101733b3ac06SPeter Wemm 
10188501a69cSRobert Watson 			INP_WLOCK(inp);
101933b3ac06SPeter Wemm 			switch (optval) {
102033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
102133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
102233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
102333b3ac06SPeter Wemm 				break;
102433b3ac06SPeter Wemm 
102533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
102633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
102733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
102833b3ac06SPeter Wemm 				break;
102933b3ac06SPeter Wemm 
103033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
103133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
103233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
103333b3ac06SPeter Wemm 				break;
103433b3ac06SPeter Wemm 
103533b3ac06SPeter Wemm 			default:
103633b3ac06SPeter Wemm 				error = EINVAL;
103733b3ac06SPeter Wemm 				break;
103833b3ac06SPeter Wemm 			}
10398501a69cSRobert Watson 			INP_WUNLOCK(inp);
1040ce8c72b1SPeter Wemm 			break;
104133b3ac06SPeter Wemm 
1042b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
10436a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
10446a800098SYoshinobu Inoue 		{
10456a800098SYoshinobu Inoue 			caddr_t req;
10466a800098SYoshinobu Inoue 			struct mbuf *m;
10476a800098SYoshinobu Inoue 
10486a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
10496a800098SYoshinobu Inoue 				break;
10506a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
10516a800098SYoshinobu Inoue 				break;
10526a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1053c26fe973SBjoern A. Zeeb 			error = ipsec4_set_policy(inp, sopt->sopt_name, req,
1054c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1055c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
10566a800098SYoshinobu Inoue 			m_freem(m);
10576a800098SYoshinobu Inoue 			break;
10586a800098SYoshinobu Inoue 		}
1059b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
10606a800098SYoshinobu Inoue 
1061df8bae1dSRodney W. Grimes 		default:
1062df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1063df8bae1dSRodney W. Grimes 			break;
1064df8bae1dSRodney W. Grimes 		}
1065df8bae1dSRodney W. Grimes 		break;
1066df8bae1dSRodney W. Grimes 
1067cfe8b629SGarrett Wollman 	case SOPT_GET:
1068cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1069df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1070df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1071cfe8b629SGarrett Wollman 			if (inp->inp_options)
1072cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1073cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1074cfe8b629SGarrett Wollman 							  char *),
1075cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1076cfe8b629SGarrett Wollman 			else
1077cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1078df8bae1dSRodney W. Grimes 			break;
1079df8bae1dSRodney W. Grimes 
1080df8bae1dSRodney W. Grimes 		case IP_TOS:
1081df8bae1dSRodney W. Grimes 		case IP_TTL:
1082936cd18dSAndre Oppermann 		case IP_MINTTL:
1083df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1084df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1085df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10864957466bSMatthew N. Dodd 		case IP_RECVTTL:
108782c23ebaSBill Fenner 		case IP_RECVIF:
1088cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
10896a800098SYoshinobu Inoue 		case IP_FAITH:
10908afa2304SBruce M Simpson 		case IP_ONESBCAST:
1091b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1092cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes 			case IP_TOS:
1095ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1096df8bae1dSRodney W. Grimes 				break;
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 			case IP_TTL:
1099ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1100df8bae1dSRodney W. Grimes 				break;
1101df8bae1dSRodney W. Grimes 
1102936cd18dSAndre Oppermann 			case IP_MINTTL:
1103936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1104936cd18dSAndre Oppermann 				break;
1105936cd18dSAndre Oppermann 
1106df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1107df8bae1dSRodney W. Grimes 
1108df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1109df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1110df8bae1dSRodney W. Grimes 				break;
1111df8bae1dSRodney W. Grimes 
1112df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1113df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1114df8bae1dSRodney W. Grimes 				break;
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1117df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1118df8bae1dSRodney W. Grimes 				break;
111982c23ebaSBill Fenner 
11204957466bSMatthew N. Dodd 			case IP_RECVTTL:
11214957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
11224957466bSMatthew N. Dodd 				break;
11234957466bSMatthew N. Dodd 
112482c23ebaSBill Fenner 			case IP_RECVIF:
112582c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
112682c23ebaSBill Fenner 				break;
1127cfe8b629SGarrett Wollman 
1128cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1129cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1130cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1131cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1132cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1133cfe8b629SGarrett Wollman 				else
1134cfe8b629SGarrett Wollman 					optval = 0;
1135cfe8b629SGarrett Wollman 				break;
11366a800098SYoshinobu Inoue 
11376a800098SYoshinobu Inoue 			case IP_FAITH:
11386a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
11396a800098SYoshinobu Inoue 				break;
11408afa2304SBruce M Simpson 
11418afa2304SBruce M Simpson 			case IP_ONESBCAST:
11428afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
11438afa2304SBruce M Simpson 				break;
1144b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1145b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1146b2828ad2SAndre Oppermann 				break;
1147df8bae1dSRodney W. Grimes 			}
1148cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1149df8bae1dSRodney W. Grimes 			break;
1150df8bae1dSRodney W. Grimes 
115171498f30SBruce M Simpson 		/*
115271498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
115371498f30SBruce M Simpson 		 * module.
115471498f30SBruce M Simpson 		 */
1155df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1156f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1157df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1158df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
115971498f30SBruce M Simpson 		case IP_MSFILTER:
116071498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
116133b3ac06SPeter Wemm 			break;
116233b3ac06SPeter Wemm 
1163b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11646a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11656a800098SYoshinobu Inoue 		{
1166f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
11676a800098SYoshinobu Inoue 			caddr_t req = NULL;
1168686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
11696a800098SYoshinobu Inoue 
1170686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
11716a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1172686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1173686cdd19SJun-ichiro itojun Hagino 			}
1174686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
11756a800098SYoshinobu Inoue 			if (error == 0)
11766a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1177f63e7634SYoshinobu Inoue 			if (error == 0)
11786a800098SYoshinobu Inoue 				m_freem(m);
11796a800098SYoshinobu Inoue 			break;
11806a800098SYoshinobu Inoue 		}
1181b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
11826a800098SYoshinobu Inoue 
1183df8bae1dSRodney W. Grimes 		default:
1184df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1185df8bae1dSRodney W. Grimes 			break;
1186df8bae1dSRodney W. Grimes 		}
1187df8bae1dSRodney W. Grimes 		break;
1188df8bae1dSRodney W. Grimes 	}
1189df8bae1dSRodney W. Grimes 	return (error);
1190df8bae1dSRodney W. Grimes }
1191df8bae1dSRodney W. Grimes 
1192df8bae1dSRodney W. Grimes /*
1193df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1194df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1195df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1196f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1197f5fea3ddSPaul Traina  * replicating that code here.
1198df8bae1dSRodney W. Grimes  */
1199df8bae1dSRodney W. Grimes static void
1200f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1201f2565d68SRobert Watson     int hlen)
1202df8bae1dSRodney W. Grimes {
1203b375c9ecSLuigi Rizzo 	register struct ip *ip;
1204df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1205df8bae1dSRodney W. Grimes 
1206e4762f75SGeorge V. Neville-Neil 	/*
1207e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1208e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1209e4762f75SGeorge V. Neville-Neil 	 */
1210e4762f75SGeorge V. Neville-Neil 	copym = m_dup(m, M_DONTWAIT);
121186b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
121286b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1213df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1214390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1215390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1216390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1217390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1218390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1219390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1220390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1221390cdc6aSRuslan Ermilov 		}
1222df8bae1dSRodney W. Grimes 		/*
1223df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1224df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1225df8bae1dSRodney W. Grimes 		 */
1226df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1227fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1228fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1229df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
123086b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1231201c2527SJulian Elischer #if 1 /* XXX */
1232201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
12332b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1234201c2527SJulian Elischer 						dst->sin_family);
1235201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1236201c2527SJulian Elischer 		}
1237201c2527SJulian Elischer #endif
123806a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
1239df8bae1dSRodney W. Grimes 	}
1240df8bae1dSRodney W. Grimes }
1241