xref: /freebsd/sys/netinet/ip_output.c (revision f44270e764dc8b896c138a9d8d19ead6572c3a99)
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"
366a800098SYoshinobu Inoue #include "opt_ipsec.h"
3733553d6eSBjoern A. Zeeb #include "opt_route.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>
6165111ec7SKip Macy #include <net/flowtable.h>
62e440aed9SQing Li #ifdef RADIX_MPATH
63e440aed9SQing Li #include <net/radix_mpath.h>
64e440aed9SQing Li #endif
654b79449eSBjoern A. Zeeb #include <net/vnet.h>
66df8bae1dSRodney W. Grimes 
67df8bae1dSRodney W. Grimes #include <netinet/in.h>
68df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip.h>
70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
71df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
72df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
73ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
744b79449eSBjoern A. Zeeb #include <netinet/vinet.h>
752f4afd21SRandall Stewart #ifdef SCTP
762f4afd21SRandall Stewart #include <netinet/sctp.h>
772f4afd21SRandall Stewart #include <netinet/sctp_crc32.h>
782f4afd21SRandall Stewart #endif
79df8bae1dSRodney W. Grimes 
80b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
812cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
821dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
83b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
846a800098SYoshinobu Inoue 
851dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
861dfcf0d2SAndre Oppermann 
87aed55708SRobert Watson #include <security/mac/mac_framework.h>
88aed55708SRobert Watson 
892b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
902b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
91f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
92f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
932b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
94f9e354dfSJulian Elischer 
9544e33a07SMarko Zec #ifdef VIMAGE_GLOBALS
96f23b4c91SGarrett Wollman u_short ip_id;
9744e33a07SMarko Zec #endif
98f23b4c91SGarrett Wollman 
9953dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
1009d9edc56SMike Silbersack int mbuf_frag_size = 0;
1019d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
1029d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
1039d9edc56SMike Silbersack #endif
1049d9edc56SMike Silbersack 
105df8bae1dSRodney W. Grimes static void	ip_mloopback
1064d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
107afed1b49SDarren Reed 
108afed1b49SDarren Reed 
1098b889dbbSBruce M Simpson extern int in_mcast_loop;
11093e0e116SJulian Elischer extern	struct protosw inetsw[];
11193e0e116SJulian Elischer 
112df8bae1dSRodney W. Grimes /*
113df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
114df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
115df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
116df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1173de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1183de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
119df8bae1dSRodney W. Grimes  */
120df8bae1dSRodney W. Grimes int
121f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
122f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
123df8bae1dSRodney W. Grimes {
1248b615593SMarko Zec 	INIT_VNET_NET(curvnet);
1258b615593SMarko Zec 	INIT_VNET_INET(curvnet);
1261e78ac21SJeffrey Hsu 	struct ip *ip;
1272b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
128ac9d7e26SMax Laier 	struct mbuf *m0;
12923bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1303ae2ad08SJohn-Mark Gurney 	int mtu;
1319b932e9eSAndre Oppermann 	int len, error = 0;
13265111ec7SKip Macy 	int nortfree = 0;
1332b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1343956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
135db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
136e3f406b3SRuslan Ermilov 	struct route iproute;
1379b932e9eSAndre Oppermann 	struct in_addr odst;
1389b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1399b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1409b932e9eSAndre Oppermann #endif
1411a499816SEdward Tomasz Napierala #ifdef IPSEC
1421a499816SEdward Tomasz Napierala 	int no_route_but_check_spd = 0;
1431a499816SEdward Tomasz Napierala #endif
144ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
14536e8826fSMax Laier 
146bc97ba51SJulian Elischer 	if (inp != NULL) {
147baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
14862e1ba83SRobert Watson 		M_SETFIB(m, inp->inp_inc.inc_fibnum);
14980cb9f21SKip Macy 		if (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) {
15080cb9f21SKip Macy 			m->m_pkthdr.flowid = inp->inp_flowid;
15180cb9f21SKip Macy 			m->m_flags |= M_FLOWID;
15280cb9f21SKip Macy 		}
153bc97ba51SJulian Elischer 	}
15462e1ba83SRobert Watson 
15562e1ba83SRobert Watson 	if (ro == NULL) {
15662e1ba83SRobert Watson 		ro = &iproute;
15762e1ba83SRobert Watson 		bzero(ro, sizeof (*ro));
15862e1ba83SRobert Watson 
15962e1ba83SRobert Watson 		/*
16062e1ba83SRobert Watson 		 * The flow table returns route entries valid for up to 30
16162e1ba83SRobert Watson 		 * seconds; we rely on the remainder of ip_output() taking no
16262e1ba83SRobert Watson 		 * longer than that long for the stability of ro_rt.  The
16362e1ba83SRobert Watson 		 * flow ID assignment must have happened before this point.
16462e1ba83SRobert Watson 		 */
16565111ec7SKip Macy 		if (flowtable_lookup(ip_ft, m, ro) == 0)
16665111ec7SKip Macy 			nortfree = 1;
16765111ec7SKip Macy 	}
1682b25acc1SLuigi Rizzo 
169df8bae1dSRodney W. Grimes 	if (opt) {
170cb7641e8SMaxim Konovalov 		len = 0;
171df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
172cb7641e8SMaxim Konovalov 		if (len != 0)
173df8bae1dSRodney W. Grimes 			hlen = len;
174df8bae1dSRodney W. Grimes 	}
175df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1763efc3014SJulian Elischer 
177df8bae1dSRodney W. Grimes 	/*
1786e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
179e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
180e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
181e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
182e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1836e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1846e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1856e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1864d09f5a0SGleb Smirnoff 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
187df8bae1dSRodney W. Grimes 	 */
188df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
18953be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
19053be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1911f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
19286425c62SRobert Watson 		IPSTAT_INC(ips_localout);
193df8bae1dSRodney W. Grimes 	} else {
19453be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
195df8bae1dSRodney W. Grimes 	}
1969c9137eaSGarrett Wollman 
197df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1989b932e9eSAndre Oppermann again:
199df8bae1dSRodney W. Grimes 	/*
200df8bae1dSRodney W. Grimes 	 * If there is a cached route,
201df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
202df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
203a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
204a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
205df8bae1dSRodney W. Grimes 	 */
206df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
207a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
2089b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
20965111ec7SKip Macy 		if (!nortfree)
210df8bae1dSRodney W. Grimes 			RTFREE(ro->ro_rt);
2113ae2ad08SJohn-Mark Gurney 		ro->ro_rt = (struct rtentry *)NULL;
212df8bae1dSRodney W. Grimes 	}
2139b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
2149b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
2159b932e9eSAndre Oppermann #else
2160b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
2179b932e9eSAndre Oppermann #endif
218a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
219df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
220df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
2219b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
222df8bae1dSRodney W. Grimes 	}
223df8bae1dSRodney W. Grimes 	/*
224a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
225a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
226a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
227a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
228df8bae1dSRodney W. Grimes 	 */
229a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
230a3fd02d8SBruce M Simpson 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
231a3fd02d8SBruce M Simpson 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
23286425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
233a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
234a3fd02d8SBruce M Simpson 			goto bad;
235a3fd02d8SBruce M Simpson 		}
236a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
237a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
238a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
239a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
240a3fd02d8SBruce M Simpson 		isbroadcast = 1;
241a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
2420b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2430b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
24486425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
245df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
246df8bae1dSRodney W. Grimes 			goto bad;
247df8bae1dSRodney W. Grimes 		}
248df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
249df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2509f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2513afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
25238c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2533afefa39SDaniel C. Sobral 		/*
25438c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
25538c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2563afefa39SDaniel C. Sobral 		 */
25738c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
25838c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
25938c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
260df8bae1dSRodney W. Grimes 	} else {
2612c17fe93SGarrett Wollman 		/*
26297d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
26397d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
26497d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2652c17fe93SGarrett Wollman 		 */
2660b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
267e440aed9SQing Li #ifdef RADIX_MPATH
2688b07e49aSJulian Elischer 			rtalloc_mpath_fib(ro,
2698b07e49aSJulian Elischer 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
2708b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
271e440aed9SQing Li #else
2728b07e49aSJulian Elischer 			in_rtalloc_ign(ro, 0,
2738b07e49aSJulian Elischer 			    inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
274e440aed9SQing Li #endif
2750b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
2761a499816SEdward Tomasz Napierala #ifdef IPSEC
2771a499816SEdward Tomasz Napierala 			/*
2781a499816SEdward Tomasz Napierala 			 * There is no route for this packet, but it is
2791a499816SEdward Tomasz Napierala 			 * possible that a matching SPD entry exists.
2801a499816SEdward Tomasz Napierala 			 */
2811a499816SEdward Tomasz Napierala 			no_route_but_check_spd = 1;
2821a499816SEdward Tomasz Napierala 			mtu = 0; /* Silence GCC warning. */
2831a499816SEdward Tomasz Napierala 			goto sendit;
2841a499816SEdward Tomasz Napierala #endif
28586425c62SRobert Watson 			IPSTAT_INC(ips_noroute);
286df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
287df8bae1dSRodney W. Grimes 			goto bad;
288df8bae1dSRodney W. Grimes 		}
289df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
290df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
29197d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
292df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
293f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2949f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
295f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2969f9b3dc4SGarrett Wollman 		else
2979f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
298df8bae1dSRodney W. Grimes 	}
2993ae2ad08SJohn-Mark Gurney 	/*
3003ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
3013ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
3023ae2ad08SJohn-Mark Gurney 	 */
303384a05bfSAndre Oppermann 	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
3043ae2ad08SJohn-Mark Gurney 		/*
3053ae2ad08SJohn-Mark Gurney 		 * This case can happen if the user changed the MTU
3063ae2ad08SJohn-Mark Gurney 		 * of an interface after enabling IP on it.  Because
3073ae2ad08SJohn-Mark Gurney 		 * most netifs don't keep track of routes pointing to
3083ae2ad08SJohn-Mark Gurney 		 * them, there is no way for one to update all its
3093ae2ad08SJohn-Mark Gurney 		 * routes when the MTU is changed.
3103ae2ad08SJohn-Mark Gurney 		 */
3113ae2ad08SJohn-Mark Gurney 		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
3123ae2ad08SJohn-Mark Gurney 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
3133ae2ad08SJohn-Mark Gurney 		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
3143ae2ad08SJohn-Mark Gurney 	} else {
3153ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
3163ae2ad08SJohn-Mark Gurney 	}
3179b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
318df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
319df8bae1dSRodney W. Grimes 		/*
320df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
321df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
322df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
323df8bae1dSRodney W. Grimes 		 */
324df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
325df8bae1dSRodney W. Grimes 		/*
326df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
327df8bae1dSRodney W. Grimes 		 */
328df8bae1dSRodney W. Grimes 		if (imo != NULL) {
329df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
3301c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
3311c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
332bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
333bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
334bbb4330bSLuigi Rizzo 				    INADDR_ANY;
335df8bae1dSRodney W. Grimes 		} else
336df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
337df8bae1dSRodney W. Grimes 		/*
338df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
339df8bae1dSRodney W. Grimes 		 */
3401c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
341df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
34286425c62SRobert Watson 				IPSTAT_INC(ips_noroute);
343df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
344df8bae1dSRodney W. Grimes 				goto bad;
345df8bae1dSRodney W. Grimes 			}
3461c5de19aSGarrett Wollman 		}
347df8bae1dSRodney W. Grimes 		/*
348df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
349df8bae1dSRodney W. Grimes 		 * of outgoing interface.
350df8bae1dSRodney W. Grimes 		 */
351df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
35238c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
35338c1bc35SRuslan Ermilov 			if (ia != NULL)
35438c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
355df8bae1dSRodney W. Grimes 		}
356df8bae1dSRodney W. Grimes 
3578b889dbbSBruce M Simpson 		if ((imo == NULL && in_mcast_loop) ||
3588b889dbbSBruce M Simpson 		    (imo && imo->imo_multicast_loop)) {
359df8bae1dSRodney W. Grimes 			/*
3608b889dbbSBruce M Simpson 			 * Loop back multicast datagram if not expressly
3618b889dbbSBruce M Simpson 			 * forbidden to do so, even if we are not a member
3628b889dbbSBruce M Simpson 			 * of the group; ip_input() will filter it later,
3638b889dbbSBruce M Simpson 			 * thus deferring a hash lookup and mutex acquisition
3648b889dbbSBruce M Simpson 			 * at the expense of a cheap copy using m_copym().
365df8bae1dSRodney W. Grimes 			 */
36686b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
3678b889dbbSBruce M Simpson 		} else {
368df8bae1dSRodney W. Grimes 			/*
369df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
370df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
371df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
372df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
373df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
374df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
375df8bae1dSRodney W. Grimes 			 *
376df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
377df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
378df8bae1dSRodney W. Grimes 			 * if necessary.
379df8bae1dSRodney W. Grimes 			 */
380603724d3SBjoern A. Zeeb 			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
381f0068c4aSGarrett Wollman 				/*
382bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
383f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
384f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
385f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
386f0068c4aSGarrett Wollman 				 */
387603724d3SBjoern A. Zeeb 				if (!V_rsvp_on)
388f0068c4aSGarrett Wollman 					imo = NULL;
389bbb4330bSLuigi Rizzo 				if (ip_mforward &&
390bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
391df8bae1dSRodney W. Grimes 					m_freem(m);
392df8bae1dSRodney W. Grimes 					goto done;
393df8bae1dSRodney W. Grimes 				}
394df8bae1dSRodney W. Grimes 			}
395df8bae1dSRodney W. Grimes 		}
3965e9ae478SGarrett Wollman 
397df8bae1dSRodney W. Grimes 		/*
398df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
399df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
400df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
401df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
4028b889dbbSBruce M Simpson 		 * loop back a copy. ip_input() will drop the copy if
4038b889dbbSBruce M Simpson 		 * this host does not belong to the destination group on
4048b889dbbSBruce M Simpson 		 * the loopback interface.
405df8bae1dSRodney W. Grimes 		 */
406f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
407df8bae1dSRodney W. Grimes 			m_freem(m);
408df8bae1dSRodney W. Grimes 			goto done;
409df8bae1dSRodney W. Grimes 		}
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes 		goto sendit;
412df8bae1dSRodney W. Grimes 	}
4136a7c943cSAndre Oppermann 
414df8bae1dSRodney W. Grimes 	/*
4152b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
416cb459254SJohn-Mark Gurney 	 * of the outoing interface.
417df8bae1dSRodney W. Grimes 	 */
418f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
41938c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
42038c1bc35SRuslan Ermilov 		if (ia != NULL) {
421df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
422f9e354dfSJulian Elischer 		}
42338c1bc35SRuslan Ermilov 	}
4246a7c943cSAndre Oppermann 
425ca7a789aSMax Laier 	/*
426ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
427ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
428ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
429ca7a789aSMax Laier 	 */
43002b199f1SMax Laier #ifdef ALTQ
431ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
4323ae2ad08SJohn-Mark Gurney 	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
433ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
434ca7a789aSMax Laier #else
4353ae2ad08SJohn-Mark Gurney 	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
436ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
437ca7a789aSMax Laier #endif /* ALTQ */
438ca7a789aSMax Laier 	{
439b5390296SDavid Greenman 		error = ENOBUFS;
44086425c62SRobert Watson 		IPSTAT_INC(ips_odropped);
441bbce982bSGleb Smirnoff 		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
442b5390296SDavid Greenman 		goto bad;
443b5390296SDavid Greenman 	}
444b5390296SDavid Greenman 
445b5390296SDavid Greenman 	/*
446df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4478c3f5566SRuslan Ermilov 	 * verify user is allowed to send
448df8bae1dSRodney W. Grimes 	 * such a packet.
449df8bae1dSRodney W. Grimes 	 */
4509f9b3dc4SGarrett Wollman 	if (isbroadcast) {
451df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
452df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
453df8bae1dSRodney W. Grimes 			goto bad;
454df8bae1dSRodney W. Grimes 		}
455df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
456df8bae1dSRodney W. Grimes 			error = EACCES;
457df8bae1dSRodney W. Grimes 			goto bad;
458df8bae1dSRodney W. Grimes 		}
459df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4603ae2ad08SJohn-Mark Gurney 		if (ip->ip_len > mtu) {
461df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
462df8bae1dSRodney W. Grimes 			goto bad;
463df8bae1dSRodney W. Grimes 		}
464df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4659f9b3dc4SGarrett Wollman 	} else {
466df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4679f9b3dc4SGarrett Wollman 	}
468df8bae1dSRodney W. Grimes 
469f1743588SDarren Reed sendit:
470b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
4711dfcf0d2SAndre Oppermann 	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
4721dfcf0d2SAndre Oppermann 	case 1:
47333841545SHajimu UMEMOTO 		goto bad;
4741dfcf0d2SAndre Oppermann 	case -1:
4751dfcf0d2SAndre Oppermann 		goto done;
4761dfcf0d2SAndre Oppermann 	case 0:
47733841545SHajimu UMEMOTO 	default:
4781dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
47933841545SHajimu UMEMOTO 	}
4801a499816SEdward Tomasz Napierala 	/*
4811a499816SEdward Tomasz Napierala 	 * Check if there was a route for this packet; return error if not.
4821a499816SEdward Tomasz Napierala 	 */
4831a499816SEdward Tomasz Napierala 	if (no_route_but_check_spd) {
4841a499816SEdward Tomasz Napierala 		IPSTAT_INC(ips_noroute);
4851a499816SEdward Tomasz Napierala 		error = EHOSTUNREACH;
4861a499816SEdward Tomasz Napierala 		goto bad;
4871a499816SEdward Tomasz Napierala 	}
4881dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
48933841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
49033841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
491b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
49233841545SHajimu UMEMOTO 
493c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
494604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
495c21fd232SAndre Oppermann 		goto passout;
496c21fd232SAndre Oppermann 
497c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
4989b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
499d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
500134ea224SSam Leffler 	if (error != 0 || m == NULL)
501c4ac87eaSDarren Reed 		goto done;
5029b932e9eSAndre Oppermann 
503c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
504fed1c7e9SSøren Schmidt 
5059b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
5069b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
5079b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
508f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
5099b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
5109b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
511f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
512603724d3SBjoern A. Zeeb 				m->m_pkthdr.rcvif = V_loif;
51320c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
51420c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
51520c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
516ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
51720c822f3SJonathan Lemon 			}
51820c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
51920c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
5202f4afd21SRandall Stewart #ifdef SCTP
5212f4afd21SRandall Stewart 			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
5222f4afd21SRandall Stewart 				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
5232f4afd21SRandall Stewart #endif
5249b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
5259b932e9eSAndre Oppermann 			goto done;
5269b932e9eSAndre Oppermann 		} else
527f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
5289b932e9eSAndre Oppermann 	}
5299b932e9eSAndre Oppermann 
5309b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
5319b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
5329b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
5339b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
534603724d3SBjoern A. Zeeb 			m->m_pkthdr.rcvif = V_loif;
5359b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
5369b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
5379b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
5389b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
5399b932e9eSAndre Oppermann 		}
5402f4afd21SRandall Stewart #ifdef SCTP
5412f4afd21SRandall Stewart 		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
5422f4afd21SRandall Stewart 			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
5432f4afd21SRandall Stewart #endif
5449b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
5459b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
5469b932e9eSAndre Oppermann 
5479b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
548f9e354dfSJulian Elischer 		goto done;
549f9e354dfSJulian Elischer 	}
5509b932e9eSAndre Oppermann 	/* Or forward to some other address? */
5519b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
5529b932e9eSAndre Oppermann 	if (fwd_tag) {
5539b932e9eSAndre Oppermann 		dst = (struct sockaddr_in *)&ro->ro_dst;
5549b932e9eSAndre Oppermann 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
5559b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
5569b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
5579b932e9eSAndre Oppermann 		goto again;
558099dd043SAndre Oppermann 	}
559099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5602b25acc1SLuigi Rizzo 
561c21fd232SAndre Oppermann passout:
56251c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
56351c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
56451c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
56551c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
56686425c62SRobert Watson 			IPSTAT_INC(ips_badaddr);
56751c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
56851c8ec4aSRuslan Ermilov 			goto bad;
56951c8ec4aSRuslan Ermilov 		}
57051c8ec4aSRuslan Ermilov 	}
57151c8ec4aSRuslan Ermilov 
572206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
573206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
574db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
575db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
576db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
577db4f9cc7SJonathan Lemon 	}
5782f4afd21SRandall Stewart #ifdef SCTP
5792f4afd21SRandall Stewart 	if (sw_csum & CSUM_SCTP) {
5802f4afd21SRandall Stewart 		sctp_delayed_cksum(m);
5812f4afd21SRandall Stewart 		sw_csum &= ~CSUM_SCTP;
5822f4afd21SRandall Stewart 	}
5832f4afd21SRandall Stewart #endif
584206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
585db4f9cc7SJonathan Lemon 
586e7319babSPoul-Henning Kamp 	/*
587db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
588233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
589df8bae1dSRodney W. Grimes 	 */
5903ae2ad08SJohn-Mark Gurney 	if (ip->ip_len <= mtu ||
591233dcce1SAndre Oppermann 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
592233dcce1SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
593fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
594fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
595df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
59653be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
597df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
5985da9f8faSJosef Karthauser 
599233dcce1SAndre Oppermann 		/*
600233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
601233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
602233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
603233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
604233dcce1SAndre Oppermann 		 */
60538c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
606233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
6073dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets +=
608233dcce1SAndre Oppermann 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
609233dcce1SAndre Oppermann 			else
6103dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
6113dbee59bSBruce M Simpson 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
6125da9f8faSJosef Karthauser 		}
61353dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
6143390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
6153390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
6169d9edc56SMike Silbersack #endif
617147f74d1SAndre Oppermann 		/*
618147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
619147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
620147f74d1SAndre Oppermann 		 */
621147f74d1SAndre Oppermann 		m->m_flags &= ~(M_PROTOFLAGS);
622df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
623279aa3d4SKip Macy 		    		(struct sockaddr *)dst, ro);
624df8bae1dSRodney W. Grimes 		goto done;
625df8bae1dSRodney W. Grimes 	}
6261e78ac21SJeffrey Hsu 
627233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
628233dcce1SAndre Oppermann 	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
629df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
63086425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
631df8bae1dSRodney W. Grimes 		goto bad;
632df8bae1dSRodney W. Grimes 	}
6331e78ac21SJeffrey Hsu 
6341e78ac21SJeffrey Hsu 	/*
6351e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
6361e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
6371e78ac21SJeffrey Hsu 	 */
6383ae2ad08SJohn-Mark Gurney 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
6391e78ac21SJeffrey Hsu 	if (error)
640df8bae1dSRodney W. Grimes 		goto bad;
6411e78ac21SJeffrey Hsu 	for (; m; m = m0) {
642df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
643b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
64407203494SDaniel C. Sobral 		if (error == 0) {
645fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
64607203494SDaniel C. Sobral 			if (ia != NULL) {
6473dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
6483dbee59bSBruce M Simpson 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
64907203494SDaniel C. Sobral 			}
650147f74d1SAndre Oppermann 			/*
651147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
652147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
653147f74d1SAndre Oppermann 			 */
654147f74d1SAndre Oppermann 			m->m_flags &= ~(M_PROTOFLAGS);
655fe937674SJosef Karthauser 
656df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
657279aa3d4SKip Macy 			    (struct sockaddr *)dst, ro);
658fe937674SJosef Karthauser 		} else
659df8bae1dSRodney W. Grimes 			m_freem(m);
660df8bae1dSRodney W. Grimes 	}
661df8bae1dSRodney W. Grimes 
662df8bae1dSRodney W. Grimes 	if (error == 0)
66386425c62SRobert Watson 		IPSTAT_INC(ips_fragmented);
6641e78ac21SJeffrey Hsu 
665df8bae1dSRodney W. Grimes done:
66665111ec7SKip Macy 	if (ro == &iproute && ro->ro_rt && !nortfree) {
6676a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
668ac9d7e26SMax Laier 	}
669df8bae1dSRodney W. Grimes 	return (error);
670df8bae1dSRodney W. Grimes bad:
6713528d68fSBill Paul 	m_freem(m);
672df8bae1dSRodney W. Grimes 	goto done;
673df8bae1dSRodney W. Grimes }
674df8bae1dSRodney W. Grimes 
6751e78ac21SJeffrey Hsu /*
6761e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
6771e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
6781e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
6791e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
6801e78ac21SJeffrey Hsu  *
6811e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
6821e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
6831e78ac21SJeffrey Hsu  */
6841e78ac21SJeffrey Hsu int
6851e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
6861e78ac21SJeffrey Hsu     u_long if_hwassist_flags, int sw_csum)
6871e78ac21SJeffrey Hsu {
6888b615593SMarko Zec 	INIT_VNET_INET(curvnet);
6891e78ac21SJeffrey Hsu 	int error = 0;
6901e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
6911e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
6921e78ac21SJeffrey Hsu 	int off;
6931e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
6941e78ac21SJeffrey Hsu 	int firstlen;
6951e78ac21SJeffrey Hsu 	struct mbuf **mnext;
6961e78ac21SJeffrey Hsu 	int nfrags;
6971e78ac21SJeffrey Hsu 
6981e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
69986425c62SRobert Watson 		IPSTAT_INC(ips_cantfrag);
7001e78ac21SJeffrey Hsu 		return EMSGSIZE;
7011e78ac21SJeffrey Hsu 	}
7021e78ac21SJeffrey Hsu 
7031e78ac21SJeffrey Hsu 	/*
7041e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
7051e78ac21SJeffrey Hsu 	 */
7061e78ac21SJeffrey Hsu 	if (len < 8)
7071e78ac21SJeffrey Hsu 		return EMSGSIZE;
7081e78ac21SJeffrey Hsu 
7091e78ac21SJeffrey Hsu 	/*
7101e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
7111e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
7121e78ac21SJeffrey Hsu 	 */
7131e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
7141e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
7151e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
7161e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
7171e78ac21SJeffrey Hsu 	}
7182f4afd21SRandall Stewart #ifdef SCTP
7192f4afd21SRandall Stewart 	if (m0->m_pkthdr.csum_flags & CSUM_SCTP &&
7202f4afd21SRandall Stewart 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
7212f4afd21SRandall Stewart 		sctp_delayed_cksum(m0);
7222f4afd21SRandall Stewart 		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
7232f4afd21SRandall Stewart 	}
7242f4afd21SRandall Stewart #endif
7251e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
7261e78ac21SJeffrey Hsu 		/*
7271e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
7281e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
7291e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
7301e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
7311e78ac21SJeffrey Hsu 		 *
7321e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
7331e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
7341e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
7351e78ac21SJeffrey Hsu 		 */
7361e78ac21SJeffrey Hsu 		int newlen;
7371e78ac21SJeffrey Hsu 		struct mbuf *m;
7381e78ac21SJeffrey Hsu 
7391e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
7401e78ac21SJeffrey Hsu 			off += m->m_len;
7411e78ac21SJeffrey Hsu 
7421e78ac21SJeffrey Hsu 		/*
7431e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
7441e78ac21SJeffrey Hsu 		 * 8-byte boundary
7451e78ac21SJeffrey Hsu 		 */
7461e78ac21SJeffrey Hsu 		if (off < hlen)
7471e78ac21SJeffrey Hsu 			goto smart_frag_failure;
7481e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
7491e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
7501e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
7511e78ac21SJeffrey Hsu 			/* we failed, go back the default */
7521e78ac21SJeffrey Hsu smart_frag_failure:
7531e78ac21SJeffrey Hsu 			newlen = len;
7541e78ac21SJeffrey Hsu 			off = hlen + len;
7551e78ac21SJeffrey Hsu 		}
7561e78ac21SJeffrey Hsu 		len = newlen;
7571e78ac21SJeffrey Hsu 
7581e78ac21SJeffrey Hsu 	} else {
7591e78ac21SJeffrey Hsu 		off = hlen + len;
7601e78ac21SJeffrey Hsu 	}
7611e78ac21SJeffrey Hsu 
7621e78ac21SJeffrey Hsu 	firstlen = off - hlen;
7631e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
7641e78ac21SJeffrey Hsu 
7651e78ac21SJeffrey Hsu 	/*
7661e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
7671e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
7681e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
7691e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
7701e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
7711e78ac21SJeffrey Hsu 	 */
7721e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
7731e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
7741e78ac21SJeffrey Hsu 		struct mbuf *m;
7751e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
7761e78ac21SJeffrey Hsu 
77734333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
7780b17fba7SAndre Oppermann 		if (m == NULL) {
7791e78ac21SJeffrey Hsu 			error = ENOBUFS;
78086425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
7811e78ac21SJeffrey Hsu 			goto done;
7821e78ac21SJeffrey Hsu 		}
7831e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
7841e78ac21SJeffrey Hsu 		/*
7851e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
7861e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
7878b889dbbSBruce M Simpson 		 * goes into an additional mbuf chain returned by m_copym().
7881e78ac21SJeffrey Hsu 		 */
7891e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
7901e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
7911e78ac21SJeffrey Hsu 		*mhip = *ip;
7921e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
7931e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
7941e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
7951e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
7961e78ac21SJeffrey Hsu 		}
7971e78ac21SJeffrey Hsu 		m->m_len = mhlen;
7981e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
7991e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
8001e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
8011e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
8021e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
8031e78ac21SJeffrey Hsu 		} else
8041e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
8051e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
8068b889dbbSBruce M Simpson 		m->m_next = m_copym(m0, off, len, M_DONTWAIT);
8070b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
8081e78ac21SJeffrey Hsu 			m_free(m);
8091e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
81086425c62SRobert Watson 			IPSTAT_INC(ips_odropped);
8111e78ac21SJeffrey Hsu 			goto done;
8121e78ac21SJeffrey Hsu 		}
8131e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
814fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
8151e78ac21SJeffrey Hsu #ifdef MAC
81630d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
8171e78ac21SJeffrey Hsu #endif
8181e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
8191e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
8201e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
8211e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
8221e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
8231e78ac21SJeffrey Hsu 		*mnext = m;
8241e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
8251e78ac21SJeffrey Hsu 	}
82686425c62SRobert Watson 	IPSTAT_ADD(ips_ofragments, nfrags);
8271e78ac21SJeffrey Hsu 
8281e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
8291e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
8301e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
8311e78ac21SJeffrey Hsu 
8321e78ac21SJeffrey Hsu 	/*
8331e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
8341e78ac21SJeffrey Hsu 	 * and updating header.
8351e78ac21SJeffrey Hsu 	 */
8361e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
8371e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
8381e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
8391e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
8401e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
8411e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
8421e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
8431e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
8441e78ac21SJeffrey Hsu 
8451e78ac21SJeffrey Hsu done:
8461e78ac21SJeffrey Hsu 	*m_frag = m0;
8471e78ac21SJeffrey Hsu 	return error;
8481e78ac21SJeffrey Hsu }
8491e78ac21SJeffrey Hsu 
8501c238475SJonathan Lemon void
851db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
852db4f9cc7SJonathan Lemon {
853db4f9cc7SJonathan Lemon 	struct ip *ip;
854db4f9cc7SJonathan Lemon 	u_short csum, offset;
855db4f9cc7SJonathan Lemon 
856db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
85753be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
858db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
859206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
860206a3274SRuslan Ermilov 		csum = 0xffff;
861db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
862db4f9cc7SJonathan Lemon 
863db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
864db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
865db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
866db4f9cc7SJonathan Lemon 		/*
867db4f9cc7SJonathan Lemon 		 * XXX
868db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
869db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
8708f8d29f6SAndre Oppermann 		 * in the appropriate next mbuf in the chain.
871db4f9cc7SJonathan Lemon 		 */
8728f8d29f6SAndre Oppermann 		return;
873db4f9cc7SJonathan Lemon 	}
874db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
875db4f9cc7SJonathan Lemon }
876db4f9cc7SJonathan Lemon 
877df8bae1dSRodney W. Grimes /*
878df8bae1dSRodney W. Grimes  * IP socket option processing.
879df8bae1dSRodney W. Grimes  */
880df8bae1dSRodney W. Grimes int
881f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
882df8bae1dSRodney W. Grimes {
883cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
884cfe8b629SGarrett Wollman 	int	error, optval;
885df8bae1dSRodney W. Grimes 
886cfe8b629SGarrett Wollman 	error = optval = 0;
887cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
888bc97ba51SJulian Elischer 		if ((sopt->sopt_level == SOL_SOCKET) &&
889bc97ba51SJulian Elischer 		    (sopt->sopt_name == SO_SETFIB)) {
890bc97ba51SJulian Elischer 			inp->inp_inc.inc_fibnum = so->so_fibnum;
891bc97ba51SJulian Elischer 			return (0);
892bc97ba51SJulian Elischer 		}
893cfe8b629SGarrett Wollman 		return (EINVAL);
894cfe8b629SGarrett Wollman 	}
895df8bae1dSRodney W. Grimes 
896cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
897cfe8b629SGarrett Wollman 	case SOPT_SET:
898cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
899df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
900df8bae1dSRodney W. Grimes #ifdef notyet
901df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
902df8bae1dSRodney W. Grimes #endif
903cfe8b629SGarrett Wollman 		{
904cfe8b629SGarrett Wollman 			struct mbuf *m;
905cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
906cfe8b629SGarrett Wollman 				error = EMSGSIZE;
907cfe8b629SGarrett Wollman 				break;
908cfe8b629SGarrett Wollman 			}
909ea26d587SRuslan Ermilov 			MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
9100b17fba7SAndre Oppermann 			if (m == NULL) {
911cfe8b629SGarrett Wollman 				error = ENOBUFS;
912cfe8b629SGarrett Wollman 				break;
913cfe8b629SGarrett Wollman 			}
914cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
915cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
916cfe8b629SGarrett Wollman 					    m->m_len);
917635354c4SMaxim Konovalov 			if (error) {
918635354c4SMaxim Konovalov 				m_free(m);
919635354c4SMaxim Konovalov 				break;
920635354c4SMaxim Konovalov 			}
9218501a69cSRobert Watson 			INP_WLOCK(inp);
922993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
9238501a69cSRobert Watson 			INP_WUNLOCK(inp);
924993d9505SRobert Watson 			return (error);
925cfe8b629SGarrett Wollman 		}
926df8bae1dSRodney W. Grimes 
927f44270e7SPawel Jakub Dawidek 		case IP_BINDANY:
928f44270e7SPawel Jakub Dawidek 			if (sopt->sopt_td != NULL) {
929f44270e7SPawel Jakub Dawidek 				error = priv_check(sopt->sopt_td,
930f44270e7SPawel Jakub Dawidek 				    PRIV_NETINET_BINDANY);
931f44270e7SPawel Jakub Dawidek 				if (error)
932be9347e3SAdrian Chadd 					break;
933be9347e3SAdrian Chadd 			}
934cef27294SAdrian Chadd 			/* FALLTHROUGH */
935df8bae1dSRodney W. Grimes 		case IP_TOS:
936df8bae1dSRodney W. Grimes 		case IP_TTL:
937936cd18dSAndre Oppermann 		case IP_MINTTL:
938df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
939df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
940df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
9414957466bSMatthew N. Dodd 		case IP_RECVTTL:
94282c23ebaSBill Fenner 		case IP_RECVIF:
9436a800098SYoshinobu Inoue 		case IP_FAITH:
9448afa2304SBruce M Simpson 		case IP_ONESBCAST:
945b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
946cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
947cfe8b629SGarrett Wollman 					    sizeof optval);
948cfe8b629SGarrett Wollman 			if (error)
949cfe8b629SGarrett Wollman 				break;
950df8bae1dSRodney W. Grimes 
951cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
952df8bae1dSRodney W. Grimes 			case IP_TOS:
953ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
954df8bae1dSRodney W. Grimes 				break;
955df8bae1dSRodney W. Grimes 
956df8bae1dSRodney W. Grimes 			case IP_TTL:
957ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
958df8bae1dSRodney W. Grimes 				break;
959936cd18dSAndre Oppermann 
960936cd18dSAndre Oppermann 			case IP_MINTTL:
961a603c811SRobert Watson 				if (optval >= 0 && optval <= MAXTTL)
962936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
963936cd18dSAndre Oppermann 				else
964936cd18dSAndre Oppermann 					error = EINVAL;
965936cd18dSAndre Oppermann 				break;
966936cd18dSAndre Oppermann 
967a138d217SRobert Watson #define	OPTSET(bit) do {						\
9688501a69cSRobert Watson 	INP_WLOCK(inp);							\
969df8bae1dSRodney W. Grimes 	if (optval)							\
970df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
971df8bae1dSRodney W. Grimes 	else								\
972a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
9738501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
974a138d217SRobert Watson } while (0)
975df8bae1dSRodney W. Grimes 
976df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
977df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
978df8bae1dSRodney W. Grimes 				break;
979df8bae1dSRodney W. Grimes 
980df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
981df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
982df8bae1dSRodney W. Grimes 				break;
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
985df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
986df8bae1dSRodney W. Grimes 				break;
98782c23ebaSBill Fenner 
9884957466bSMatthew N. Dodd 			case IP_RECVTTL:
9894957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
9904957466bSMatthew N. Dodd 				break;
9914957466bSMatthew N. Dodd 
99282c23ebaSBill Fenner 			case IP_RECVIF:
99382c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
99482c23ebaSBill Fenner 				break;
9956a800098SYoshinobu Inoue 
9966a800098SYoshinobu Inoue 			case IP_FAITH:
9976a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
9986a800098SYoshinobu Inoue 				break;
9998afa2304SBruce M Simpson 
10008afa2304SBruce M Simpson 			case IP_ONESBCAST:
10018afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
10028afa2304SBruce M Simpson 				break;
1003b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1004b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
1005b2828ad2SAndre Oppermann 				break;
1006f44270e7SPawel Jakub Dawidek 			case IP_BINDANY:
1007f44270e7SPawel Jakub Dawidek 				OPTSET(INP_BINDANY);
1008be9347e3SAdrian Chadd 				break;
1009df8bae1dSRodney W. Grimes 			}
1010df8bae1dSRodney W. Grimes 			break;
1011df8bae1dSRodney W. Grimes #undef OPTSET
1012df8bae1dSRodney W. Grimes 
101371498f30SBruce M Simpson 		/*
101471498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
101571498f30SBruce M Simpson 		 * module.
101671498f30SBruce M Simpson 		 */
1017df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1018f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1019df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1020df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
1021df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
1022df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
102371498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
102471498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
102571498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
102671498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
102771498f30SBruce M Simpson 		case IP_MSFILTER:
102871498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
102971498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
103071498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
103171498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
103271498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
103371498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
103471498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
1035df8bae1dSRodney W. Grimes 			break;
1036df8bae1dSRodney W. Grimes 
103733b3ac06SPeter Wemm 		case IP_PORTRANGE:
1038cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
1039cfe8b629SGarrett Wollman 					    sizeof optval);
1040cfe8b629SGarrett Wollman 			if (error)
1041cfe8b629SGarrett Wollman 				break;
104233b3ac06SPeter Wemm 
10438501a69cSRobert Watson 			INP_WLOCK(inp);
104433b3ac06SPeter Wemm 			switch (optval) {
104533b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
104633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
104733b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
104833b3ac06SPeter Wemm 				break;
104933b3ac06SPeter Wemm 
105033b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
105133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
105233b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
105333b3ac06SPeter Wemm 				break;
105433b3ac06SPeter Wemm 
105533b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
105633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
105733b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
105833b3ac06SPeter Wemm 				break;
105933b3ac06SPeter Wemm 
106033b3ac06SPeter Wemm 			default:
106133b3ac06SPeter Wemm 				error = EINVAL;
106233b3ac06SPeter Wemm 				break;
106333b3ac06SPeter Wemm 			}
10648501a69cSRobert Watson 			INP_WUNLOCK(inp);
1065ce8c72b1SPeter Wemm 			break;
106633b3ac06SPeter Wemm 
1067b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
10686a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
10696a800098SYoshinobu Inoue 		{
10706a800098SYoshinobu Inoue 			caddr_t req;
10716a800098SYoshinobu Inoue 			struct mbuf *m;
10726a800098SYoshinobu Inoue 
10736a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
10746a800098SYoshinobu Inoue 				break;
10756a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
10766a800098SYoshinobu Inoue 				break;
10776a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
107897aa4a51SBjoern A. Zeeb 			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1079c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
1080c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
10816a800098SYoshinobu Inoue 			m_freem(m);
10826a800098SYoshinobu Inoue 			break;
10836a800098SYoshinobu Inoue 		}
1084b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
10856a800098SYoshinobu Inoue 
1086df8bae1dSRodney W. Grimes 		default:
1087df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1088df8bae1dSRodney W. Grimes 			break;
1089df8bae1dSRodney W. Grimes 		}
1090df8bae1dSRodney W. Grimes 		break;
1091df8bae1dSRodney W. Grimes 
1092cfe8b629SGarrett Wollman 	case SOPT_GET:
1093cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1094df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1095df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1096cfe8b629SGarrett Wollman 			if (inp->inp_options)
1097cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1098cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1099cfe8b629SGarrett Wollman 							  char *),
1100cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1101cfe8b629SGarrett Wollman 			else
1102cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1103df8bae1dSRodney W. Grimes 			break;
1104df8bae1dSRodney W. Grimes 
1105df8bae1dSRodney W. Grimes 		case IP_TOS:
1106df8bae1dSRodney W. Grimes 		case IP_TTL:
1107936cd18dSAndre Oppermann 		case IP_MINTTL:
1108df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1109df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1110df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
11114957466bSMatthew N. Dodd 		case IP_RECVTTL:
111282c23ebaSBill Fenner 		case IP_RECVIF:
1113cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
11146a800098SYoshinobu Inoue 		case IP_FAITH:
11158afa2304SBruce M Simpson 		case IP_ONESBCAST:
1116b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1117cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1118df8bae1dSRodney W. Grimes 
1119df8bae1dSRodney W. Grimes 			case IP_TOS:
1120ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1121df8bae1dSRodney W. Grimes 				break;
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 			case IP_TTL:
1124ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1125df8bae1dSRodney W. Grimes 				break;
1126df8bae1dSRodney W. Grimes 
1127936cd18dSAndre Oppermann 			case IP_MINTTL:
1128936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1129936cd18dSAndre Oppermann 				break;
1130936cd18dSAndre Oppermann 
1131df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1132df8bae1dSRodney W. Grimes 
1133df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1134df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1135df8bae1dSRodney W. Grimes 				break;
1136df8bae1dSRodney W. Grimes 
1137df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1138df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1139df8bae1dSRodney W. Grimes 				break;
1140df8bae1dSRodney W. Grimes 
1141df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1142df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1143df8bae1dSRodney W. Grimes 				break;
114482c23ebaSBill Fenner 
11454957466bSMatthew N. Dodd 			case IP_RECVTTL:
11464957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
11474957466bSMatthew N. Dodd 				break;
11484957466bSMatthew N. Dodd 
114982c23ebaSBill Fenner 			case IP_RECVIF:
115082c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
115182c23ebaSBill Fenner 				break;
1152cfe8b629SGarrett Wollman 
1153cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1154cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1155cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1156cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1157cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1158cfe8b629SGarrett Wollman 				else
1159cfe8b629SGarrett Wollman 					optval = 0;
1160cfe8b629SGarrett Wollman 				break;
11616a800098SYoshinobu Inoue 
11626a800098SYoshinobu Inoue 			case IP_FAITH:
11636a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
11646a800098SYoshinobu Inoue 				break;
11658afa2304SBruce M Simpson 
11668afa2304SBruce M Simpson 			case IP_ONESBCAST:
11678afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
11688afa2304SBruce M Simpson 				break;
1169b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1170b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1171b2828ad2SAndre Oppermann 				break;
1172df8bae1dSRodney W. Grimes 			}
1173cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1174df8bae1dSRodney W. Grimes 			break;
1175df8bae1dSRodney W. Grimes 
117671498f30SBruce M Simpson 		/*
117771498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
117871498f30SBruce M Simpson 		 * module.
117971498f30SBruce M Simpson 		 */
1180df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1181f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1182df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1183df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
118471498f30SBruce M Simpson 		case IP_MSFILTER:
118571498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
118633b3ac06SPeter Wemm 			break;
118733b3ac06SPeter Wemm 
1188b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11896a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11906a800098SYoshinobu Inoue 		{
1191f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
11926a800098SYoshinobu Inoue 			caddr_t req = NULL;
1193686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
11946a800098SYoshinobu Inoue 
1195686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
11966a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1197686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1198686cdd19SJun-ichiro itojun Hagino 			}
119997aa4a51SBjoern A. Zeeb 			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
12006a800098SYoshinobu Inoue 			if (error == 0)
12016a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1202f63e7634SYoshinobu Inoue 			if (error == 0)
12036a800098SYoshinobu Inoue 				m_freem(m);
12046a800098SYoshinobu Inoue 			break;
12056a800098SYoshinobu Inoue 		}
1206b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
12076a800098SYoshinobu Inoue 
1208df8bae1dSRodney W. Grimes 		default:
1209df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1210df8bae1dSRodney W. Grimes 			break;
1211df8bae1dSRodney W. Grimes 		}
1212df8bae1dSRodney W. Grimes 		break;
1213df8bae1dSRodney W. Grimes 	}
1214df8bae1dSRodney W. Grimes 	return (error);
1215df8bae1dSRodney W. Grimes }
1216df8bae1dSRodney W. Grimes 
1217df8bae1dSRodney W. Grimes /*
1218df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1219df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1220df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1221f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1222f5fea3ddSPaul Traina  * replicating that code here.
1223df8bae1dSRodney W. Grimes  */
1224df8bae1dSRodney W. Grimes static void
1225f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1226f2565d68SRobert Watson     int hlen)
1227df8bae1dSRodney W. Grimes {
1228b375c9ecSLuigi Rizzo 	register struct ip *ip;
1229df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1230df8bae1dSRodney W. Grimes 
1231e4762f75SGeorge V. Neville-Neil 	/*
1232e4762f75SGeorge V. Neville-Neil 	 * Make a deep copy of the packet because we're going to
1233e4762f75SGeorge V. Neville-Neil 	 * modify the pack in order to generate checksums.
1234e4762f75SGeorge V. Neville-Neil 	 */
1235e4762f75SGeorge V. Neville-Neil 	copym = m_dup(m, M_DONTWAIT);
123686b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
123786b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1238df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1239390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1240390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1241390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1242390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1243390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1244390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1245390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1246390cdc6aSRuslan Ermilov 		}
1247df8bae1dSRodney W. Grimes 		/*
1248df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1249df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1250df8bae1dSRodney W. Grimes 		 */
1251df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1252fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1253fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1254df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
125586b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
1256201c2527SJulian Elischer #if 1 /* XXX */
1257201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
12582b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1259201c2527SJulian Elischer 						dst->sin_family);
1260201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1261201c2527SJulian Elischer 		}
1262201c2527SJulian Elischer #endif
126306a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
1264df8bae1dSRodney W. Grimes 	}
1265df8bae1dSRodney W. Grimes }
1266