xref: /freebsd/sys/netinet/ip_output.c (revision baa45840d7c8424215ff4ed3468f30067dee37ce)
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"
374ed84624SRobert Watson #include "opt_mac.h"
3853dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
39e440aed9SQing Li #include "opt_mpath.h"
40fbd1372aSJoerg Wunsch 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
4226f9a767SRodney W. Grimes #include <sys/systm.h>
43f0f6d643SLuigi Rizzo #include <sys/kernel.h>
44df8bae1dSRodney W. Grimes #include <sys/malloc.h>
45df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
46acd3428bSRobert Watson #include <sys/priv.h>
47c26fe973SBjoern A. Zeeb #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/protosw.h>
49df8bae1dSRodney W. Grimes #include <sys/socket.h>
50df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
519d9edc56SMike Silbersack #include <sys/sysctl.h>
52c26fe973SBjoern A. Zeeb #include <sys/ucred.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
559b932e9eSAndre Oppermann #include <net/netisr.h>
56c21fd232SAndre Oppermann #include <net/pfil.h>
57df8bae1dSRodney W. Grimes #include <net/route.h>
58e440aed9SQing Li #ifdef RADIX_MPATH
59e440aed9SQing Li #include <net/radix_mpath.h>
60e440aed9SQing Li #endif
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <netinet/in.h>
63df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
64df8bae1dSRodney W. Grimes #include <netinet/ip.h>
65df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
66df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
67df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
68ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
69df8bae1dSRodney W. Grimes 
70b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
712cb64cb2SGeorge V. Neville-Neil #include <netinet/ip_ipsec.h>
721dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
73b2630c29SGeorge V. Neville-Neil #endif /* IPSEC*/
746a800098SYoshinobu Inoue 
751dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
761dfcf0d2SAndre Oppermann 
77aed55708SRobert Watson #include <security/mac/mac_framework.h>
78aed55708SRobert Watson 
792b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
802b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
81f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
82f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
832b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
84f9e354dfSJulian Elischer 
85f23b4c91SGarrett Wollman u_short ip_id;
86f23b4c91SGarrett Wollman 
8753dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
889d9edc56SMike Silbersack int mbuf_frag_size = 0;
899d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
909d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
919d9edc56SMike Silbersack #endif
929d9edc56SMike Silbersack 
93df8bae1dSRodney W. Grimes static void	ip_mloopback
944d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
95afed1b49SDarren Reed 
96afed1b49SDarren Reed 
9793e0e116SJulian Elischer extern	struct protosw inetsw[];
9893e0e116SJulian Elischer 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
101df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
102df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
103df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1043de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1053de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
106df8bae1dSRodney W. Grimes  */
107df8bae1dSRodney W. Grimes int
108f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
109f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
110df8bae1dSRodney W. Grimes {
1111e78ac21SJeffrey Hsu 	struct ip *ip;
1122b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
113ac9d7e26SMax Laier 	struct mbuf *m0;
11423bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1153ae2ad08SJohn-Mark Gurney 	int mtu;
1169b932e9eSAndre Oppermann 	int len, error = 0;
1172b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1183956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
119db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
120e3f406b3SRuslan Ermilov 	struct route iproute;
1219b932e9eSAndre Oppermann 	struct in_addr odst;
1229b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1239b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1249b932e9eSAndre Oppermann #endif
125ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
12636e8826fSMax Laier 
12702c1c707SAndre Oppermann 	if (ro == NULL) {
12802c1c707SAndre Oppermann 		ro = &iproute;
12902c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
13002c1c707SAndre Oppermann 	}
13102c1c707SAndre Oppermann 
13284843845SSam Leffler 	if (inp != NULL)
133baa45840SRobert Watson 		INP_LOCK_ASSERT(inp);
1342b25acc1SLuigi Rizzo 
135df8bae1dSRodney W. Grimes 	if (opt) {
136cb7641e8SMaxim Konovalov 		len = 0;
137df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
138cb7641e8SMaxim Konovalov 		if (len != 0)
139df8bae1dSRodney W. Grimes 			hlen = len;
140df8bae1dSRodney W. Grimes 	}
141df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1423efc3014SJulian Elischer 
143df8bae1dSRodney W. Grimes 	/*
1446e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
145e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
146e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
147e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
148e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1496e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1506e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1516e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1524d09f5a0SGleb Smirnoff 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
153df8bae1dSRodney W. Grimes 	 */
154df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
15553be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
15653be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1571f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
158df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
159df8bae1dSRodney W. Grimes 	} else {
16053be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
161df8bae1dSRodney W. Grimes 	}
1629c9137eaSGarrett Wollman 
163df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1649b932e9eSAndre Oppermann again:
165df8bae1dSRodney W. Grimes 	/*
166df8bae1dSRodney W. Grimes 	 * If there is a cached route,
167df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
168df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
169a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
170a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
171df8bae1dSRodney W. Grimes 	 */
172df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
173a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1749b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
175df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
1763ae2ad08SJohn-Mark Gurney 		ro->ro_rt = (struct rtentry *)NULL;
177df8bae1dSRodney W. Grimes 	}
1789b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1799b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
1809b932e9eSAndre Oppermann #else
1810b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
1829b932e9eSAndre Oppermann #endif
183a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
184df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
185df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
1869b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
187df8bae1dSRodney W. Grimes 	}
188df8bae1dSRodney W. Grimes 	/*
189a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
190a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
191a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
192a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
193df8bae1dSRodney W. Grimes 	 */
194a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
195a3fd02d8SBruce M Simpson 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
196a3fd02d8SBruce M Simpson 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
197a3fd02d8SBruce M Simpson 			ipstat.ips_noroute++;
198a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
199a3fd02d8SBruce M Simpson 			goto bad;
200a3fd02d8SBruce M Simpson 		}
201a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
202a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
203a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
204a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
205a3fd02d8SBruce M Simpson 		isbroadcast = 1;
206a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
2070b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2080b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
209df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
210df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
211df8bae1dSRodney W. Grimes 			goto bad;
212df8bae1dSRodney W. Grimes 		}
213df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
214df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2159f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2163afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
21738c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2183afefa39SDaniel C. Sobral 		/*
21938c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
22038c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2213afefa39SDaniel C. Sobral 		 */
22238c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
22338c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
22438c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
225df8bae1dSRodney W. Grimes 	} else {
2262c17fe93SGarrett Wollman 		/*
22797d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
22897d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
22997d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2302c17fe93SGarrett Wollman 		 */
2310b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
232e440aed9SQing Li #ifdef RADIX_MPATH
233e440aed9SQing Li 			rtalloc_mpath(ro,
234e440aed9SQing Li 			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr));
235e440aed9SQing Li #else
236e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
237e440aed9SQing Li #endif
2380b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
239df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
240df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
241df8bae1dSRodney W. Grimes 			goto bad;
242df8bae1dSRodney W. Grimes 		}
243df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
244df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
24597d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
246df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
247f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2489f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
249f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2509f9b3dc4SGarrett Wollman 		else
2519f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
252df8bae1dSRodney W. Grimes 	}
2533ae2ad08SJohn-Mark Gurney 	/*
2543ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
2553ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
2563ae2ad08SJohn-Mark Gurney 	 */
257384a05bfSAndre Oppermann 	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
2583ae2ad08SJohn-Mark Gurney 		/*
2593ae2ad08SJohn-Mark Gurney 		 * This case can happen if the user changed the MTU
2603ae2ad08SJohn-Mark Gurney 		 * of an interface after enabling IP on it.  Because
2613ae2ad08SJohn-Mark Gurney 		 * most netifs don't keep track of routes pointing to
2623ae2ad08SJohn-Mark Gurney 		 * them, there is no way for one to update all its
2633ae2ad08SJohn-Mark Gurney 		 * routes when the MTU is changed.
2643ae2ad08SJohn-Mark Gurney 		 */
2653ae2ad08SJohn-Mark Gurney 		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
2663ae2ad08SJohn-Mark Gurney 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
2673ae2ad08SJohn-Mark Gurney 		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
2683ae2ad08SJohn-Mark Gurney 	} else {
2693ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
2703ae2ad08SJohn-Mark Gurney 	}
2719b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
272df8bae1dSRodney W. Grimes 		struct in_multi *inm;
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
275df8bae1dSRodney W. Grimes 		/*
276df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
277df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
278df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
279df8bae1dSRodney W. Grimes 		 */
280df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
281df8bae1dSRodney W. Grimes 		/*
282df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
283df8bae1dSRodney W. Grimes 		 */
284df8bae1dSRodney W. Grimes 		if (imo != NULL) {
285df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
2861c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2871c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
288bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
289bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
290bbb4330bSLuigi Rizzo 				    INADDR_ANY;
291df8bae1dSRodney W. Grimes 		} else
292df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
293df8bae1dSRodney W. Grimes 		/*
294df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
295df8bae1dSRodney W. Grimes 		 */
2961c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
297df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
298df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
299df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
300df8bae1dSRodney W. Grimes 				goto bad;
301df8bae1dSRodney W. Grimes 			}
3021c5de19aSGarrett Wollman 		}
303df8bae1dSRodney W. Grimes 		/*
304df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
305df8bae1dSRodney W. Grimes 		 * of outgoing interface.
306df8bae1dSRodney W. Grimes 		 */
307df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
30838c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
30938c1bc35SRuslan Ermilov 			if (ia != NULL)
31038c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
311df8bae1dSRodney W. Grimes 		}
312df8bae1dSRodney W. Grimes 
313dd5a318bSRobert Watson 		IN_MULTI_LOCK();
3149b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
315df8bae1dSRodney W. Grimes 		if (inm != NULL &&
316df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
317dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
318df8bae1dSRodney W. Grimes 			/*
319df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
320df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
321df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
322df8bae1dSRodney W. Grimes 			 */
32386b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
324df8bae1dSRodney W. Grimes 		}
325df8bae1dSRodney W. Grimes 		else {
326dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
327df8bae1dSRodney W. Grimes 			/*
328df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
329df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
330df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
331df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
332df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
333df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
334df8bae1dSRodney W. Grimes 			 *
335df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
336df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
337df8bae1dSRodney W. Grimes 			 * if necessary.
338df8bae1dSRodney W. Grimes 			 */
339df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
340f0068c4aSGarrett Wollman 				/*
341bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
342f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
343f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
344f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
345f0068c4aSGarrett Wollman 				 */
346b124e4f2SGarrett Wollman 				if (!rsvp_on)
347f0068c4aSGarrett Wollman 					imo = NULL;
348bbb4330bSLuigi Rizzo 				if (ip_mforward &&
349bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
350df8bae1dSRodney W. Grimes 					m_freem(m);
351df8bae1dSRodney W. Grimes 					goto done;
352df8bae1dSRodney W. Grimes 				}
353df8bae1dSRodney W. Grimes 			}
354df8bae1dSRodney W. Grimes 		}
3555e9ae478SGarrett Wollman 
356df8bae1dSRodney W. Grimes 		/*
357df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
358df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
359df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
360df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
361df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
362df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
363df8bae1dSRodney W. Grimes 		 */
364f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
365df8bae1dSRodney W. Grimes 			m_freem(m);
366df8bae1dSRodney W. Grimes 			goto done;
367df8bae1dSRodney W. Grimes 		}
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes 		goto sendit;
370df8bae1dSRodney W. Grimes 	}
3716a7c943cSAndre Oppermann 
372df8bae1dSRodney W. Grimes 	/*
3732b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
374cb459254SJohn-Mark Gurney 	 * of the outoing interface.
375df8bae1dSRodney W. Grimes 	 */
376f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
37738c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
37838c1bc35SRuslan Ermilov 		if (ia != NULL) {
379df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
380f9e354dfSJulian Elischer 		}
38138c1bc35SRuslan Ermilov 	}
3826a7c943cSAndre Oppermann 
383ca7a789aSMax Laier 	/*
384ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
385ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
386ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
387ca7a789aSMax Laier 	 */
38802b199f1SMax Laier #ifdef ALTQ
389ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
3903ae2ad08SJohn-Mark Gurney 	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
391ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
392ca7a789aSMax Laier #else
3933ae2ad08SJohn-Mark Gurney 	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
394ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
395ca7a789aSMax Laier #endif /* ALTQ */
396ca7a789aSMax Laier 	{
397b5390296SDavid Greenman 		error = ENOBUFS;
39835609d45SJonathan Lemon 		ipstat.ips_odropped++;
399bbce982bSGleb Smirnoff 		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
400b5390296SDavid Greenman 		goto bad;
401b5390296SDavid Greenman 	}
402b5390296SDavid Greenman 
403b5390296SDavid Greenman 	/*
404df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4058c3f5566SRuslan Ermilov 	 * verify user is allowed to send
406df8bae1dSRodney W. Grimes 	 * such a packet.
407df8bae1dSRodney W. Grimes 	 */
4089f9b3dc4SGarrett Wollman 	if (isbroadcast) {
409df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
410df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
411df8bae1dSRodney W. Grimes 			goto bad;
412df8bae1dSRodney W. Grimes 		}
413df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
414df8bae1dSRodney W. Grimes 			error = EACCES;
415df8bae1dSRodney W. Grimes 			goto bad;
416df8bae1dSRodney W. Grimes 		}
417df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4183ae2ad08SJohn-Mark Gurney 		if (ip->ip_len > mtu) {
419df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
420df8bae1dSRodney W. Grimes 			goto bad;
421df8bae1dSRodney W. Grimes 		}
422df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4239f9b3dc4SGarrett Wollman 	} else {
424df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4259f9b3dc4SGarrett Wollman 	}
426df8bae1dSRodney W. Grimes 
427f1743588SDarren Reed sendit:
428b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
4291dfcf0d2SAndre Oppermann 	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
4301dfcf0d2SAndre Oppermann 	case 1:
43133841545SHajimu UMEMOTO 		goto bad;
4321dfcf0d2SAndre Oppermann 	case -1:
4331dfcf0d2SAndre Oppermann 		goto done;
4341dfcf0d2SAndre Oppermann 	case 0:
43533841545SHajimu UMEMOTO 	default:
4361dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
43733841545SHajimu UMEMOTO 	}
4381dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
43933841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
44033841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
441b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
44233841545SHajimu UMEMOTO 
443c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
444604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
445c21fd232SAndre Oppermann 		goto passout;
446c21fd232SAndre Oppermann 
447c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
4489b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
449d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
450134ea224SSam Leffler 	if (error != 0 || m == NULL)
451c4ac87eaSDarren Reed 		goto done;
4529b932e9eSAndre Oppermann 
453c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
454fed1c7e9SSøren Schmidt 
4559b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
4569b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
4579b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
458f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
4599b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
4609b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
461f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
462a9c92b54SAndre Oppermann 				m->m_pkthdr.rcvif = loif;
46320c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
46420c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
46520c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
466ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
46720c822f3SJonathan Lemon 			}
46820c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
46920c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4709b932e9eSAndre Oppermann 
4719b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
4729b932e9eSAndre Oppermann 			goto done;
4739b932e9eSAndre Oppermann 		} else
474f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
4759b932e9eSAndre Oppermann 	}
4769b932e9eSAndre Oppermann 
4779b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4789b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
4799b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4809b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
481a9c92b54SAndre Oppermann 			m->m_pkthdr.rcvif = loif;
4829b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
4839b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
4849b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
4859b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
4869b932e9eSAndre Oppermann 		}
4879b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
4889b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4899b932e9eSAndre Oppermann 
4909b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
491f9e354dfSJulian Elischer 		goto done;
492f9e354dfSJulian Elischer 	}
4939b932e9eSAndre Oppermann 	/* Or forward to some other address? */
4949b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
4959b932e9eSAndre Oppermann 	if (fwd_tag) {
4969b932e9eSAndre Oppermann 		dst = (struct sockaddr_in *)&ro->ro_dst;
4979b932e9eSAndre Oppermann 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
4989b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
4999b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
5009b932e9eSAndre Oppermann 		goto again;
501099dd043SAndre Oppermann 	}
502099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5032b25acc1SLuigi Rizzo 
504c21fd232SAndre Oppermann passout:
50551c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
50651c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
50751c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
50851c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
50951c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
51051c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
51151c8ec4aSRuslan Ermilov 			goto bad;
51251c8ec4aSRuslan Ermilov 		}
51351c8ec4aSRuslan Ermilov 	}
51451c8ec4aSRuslan Ermilov 
515206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
516206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
517db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
518db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
519db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
520db4f9cc7SJonathan Lemon 	}
521206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
522db4f9cc7SJonathan Lemon 
523e7319babSPoul-Henning Kamp 	/*
524db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
525233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
526df8bae1dSRodney W. Grimes 	 */
5273ae2ad08SJohn-Mark Gurney 	if (ip->ip_len <= mtu ||
528233dcce1SAndre Oppermann 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
529233dcce1SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
530fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
531fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
532df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
53353be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
534df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
5355da9f8faSJosef Karthauser 
536233dcce1SAndre Oppermann 		/*
537233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
538233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
539233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
540233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
541233dcce1SAndre Oppermann 		 */
54238c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
543233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
5443dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets +=
545233dcce1SAndre Oppermann 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
546233dcce1SAndre Oppermann 			else
5473dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5483dbee59bSBruce M Simpson 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
5495da9f8faSJosef Karthauser 		}
55053dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
5513390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
5523390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
5539d9edc56SMike Silbersack #endif
554147f74d1SAndre Oppermann 		/*
555147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
556147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
557147f74d1SAndre Oppermann 		 */
558147f74d1SAndre Oppermann 		m->m_flags &= ~(M_PROTOFLAGS);
559147f74d1SAndre Oppermann 
560df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
561df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
562df8bae1dSRodney W. Grimes 		goto done;
563df8bae1dSRodney W. Grimes 	}
5641e78ac21SJeffrey Hsu 
565233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
566233dcce1SAndre Oppermann 	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
567df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
568df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
569df8bae1dSRodney W. Grimes 		goto bad;
570df8bae1dSRodney W. Grimes 	}
5711e78ac21SJeffrey Hsu 
5721e78ac21SJeffrey Hsu 	/*
5731e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
5741e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
5751e78ac21SJeffrey Hsu 	 */
5763ae2ad08SJohn-Mark Gurney 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
5771e78ac21SJeffrey Hsu 	if (error)
578df8bae1dSRodney W. Grimes 		goto bad;
5791e78ac21SJeffrey Hsu 	for (; m; m = m0) {
580df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
581b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
58207203494SDaniel C. Sobral 		if (error == 0) {
583fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
58407203494SDaniel C. Sobral 			if (ia != NULL) {
5853dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5863dbee59bSBruce M Simpson 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
58707203494SDaniel C. Sobral 			}
588147f74d1SAndre Oppermann 			/*
589147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
590147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
591147f74d1SAndre Oppermann 			 */
592147f74d1SAndre Oppermann 			m->m_flags &= ~(M_PROTOFLAGS);
593fe937674SJosef Karthauser 
594df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
595df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
596fe937674SJosef Karthauser 		} else
597df8bae1dSRodney W. Grimes 			m_freem(m);
598df8bae1dSRodney W. Grimes 	}
599df8bae1dSRodney W. Grimes 
600df8bae1dSRodney W. Grimes 	if (error == 0)
601df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
6021e78ac21SJeffrey Hsu 
603df8bae1dSRodney W. Grimes done:
6046a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
6056a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
606ac9d7e26SMax Laier 	}
607df8bae1dSRodney W. Grimes 	return (error);
608df8bae1dSRodney W. Grimes bad:
6093528d68fSBill Paul 	m_freem(m);
610df8bae1dSRodney W. Grimes 	goto done;
611df8bae1dSRodney W. Grimes }
612df8bae1dSRodney W. Grimes 
6131e78ac21SJeffrey Hsu /*
6141e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
6151e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
6161e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
6171e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
6181e78ac21SJeffrey Hsu  *
6191e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
6201e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
6211e78ac21SJeffrey Hsu  */
6221e78ac21SJeffrey Hsu int
6231e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
6241e78ac21SJeffrey Hsu     u_long if_hwassist_flags, int sw_csum)
6251e78ac21SJeffrey Hsu {
6261e78ac21SJeffrey Hsu 	int error = 0;
6271e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
6281e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
6291e78ac21SJeffrey Hsu 	int off;
6301e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
6311e78ac21SJeffrey Hsu 	int firstlen;
6321e78ac21SJeffrey Hsu 	struct mbuf **mnext;
6331e78ac21SJeffrey Hsu 	int nfrags;
6341e78ac21SJeffrey Hsu 
6351e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
6361e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
6371e78ac21SJeffrey Hsu 		return EMSGSIZE;
6381e78ac21SJeffrey Hsu 	}
6391e78ac21SJeffrey Hsu 
6401e78ac21SJeffrey Hsu 	/*
6411e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
6421e78ac21SJeffrey Hsu 	 */
6431e78ac21SJeffrey Hsu 	if (len < 8)
6441e78ac21SJeffrey Hsu 		return EMSGSIZE;
6451e78ac21SJeffrey Hsu 
6461e78ac21SJeffrey Hsu 	/*
6471e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
6481e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
6491e78ac21SJeffrey Hsu 	 */
6501e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
6511e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
6521e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
6531e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
6541e78ac21SJeffrey Hsu 	}
6551e78ac21SJeffrey Hsu 
6561e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
6571e78ac21SJeffrey Hsu 		/*
6581e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
6591e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
6601e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
6611e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
6621e78ac21SJeffrey Hsu 		 *
6631e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
6641e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
6651e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
6661e78ac21SJeffrey Hsu 		 */
6671e78ac21SJeffrey Hsu 		int newlen;
6681e78ac21SJeffrey Hsu 		struct mbuf *m;
6691e78ac21SJeffrey Hsu 
6701e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
6711e78ac21SJeffrey Hsu 			off += m->m_len;
6721e78ac21SJeffrey Hsu 
6731e78ac21SJeffrey Hsu 		/*
6741e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
6751e78ac21SJeffrey Hsu 		 * 8-byte boundary
6761e78ac21SJeffrey Hsu 		 */
6771e78ac21SJeffrey Hsu 		if (off < hlen)
6781e78ac21SJeffrey Hsu 			goto smart_frag_failure;
6791e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
6801e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
6811e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
6821e78ac21SJeffrey Hsu 			/* we failed, go back the default */
6831e78ac21SJeffrey Hsu smart_frag_failure:
6841e78ac21SJeffrey Hsu 			newlen = len;
6851e78ac21SJeffrey Hsu 			off = hlen + len;
6861e78ac21SJeffrey Hsu 		}
6871e78ac21SJeffrey Hsu 		len = newlen;
6881e78ac21SJeffrey Hsu 
6891e78ac21SJeffrey Hsu 	} else {
6901e78ac21SJeffrey Hsu 		off = hlen + len;
6911e78ac21SJeffrey Hsu 	}
6921e78ac21SJeffrey Hsu 
6931e78ac21SJeffrey Hsu 	firstlen = off - hlen;
6941e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
6951e78ac21SJeffrey Hsu 
6961e78ac21SJeffrey Hsu 	/*
6971e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
6981e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
6991e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
7001e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
7011e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
7021e78ac21SJeffrey Hsu 	 */
7031e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
7041e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
7051e78ac21SJeffrey Hsu 		struct mbuf *m;
7061e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
7071e78ac21SJeffrey Hsu 
70834333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
7090b17fba7SAndre Oppermann 		if (m == NULL) {
7101e78ac21SJeffrey Hsu 			error = ENOBUFS;
7111e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
7121e78ac21SJeffrey Hsu 			goto done;
7131e78ac21SJeffrey Hsu 		}
7141e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
7151e78ac21SJeffrey Hsu 		/*
7161e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
7171e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
7181e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
7191e78ac21SJeffrey Hsu 		 */
7201e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
7211e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
7221e78ac21SJeffrey Hsu 		*mhip = *ip;
7231e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
7241e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
7251e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
7261e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
7271e78ac21SJeffrey Hsu 		}
7281e78ac21SJeffrey Hsu 		m->m_len = mhlen;
7291e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
7301e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
7311e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
7321e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
7331e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
7341e78ac21SJeffrey Hsu 		} else
7351e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
7361e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
7371e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
7380b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
7391e78ac21SJeffrey Hsu 			m_free(m);
7401e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
7411e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
7421e78ac21SJeffrey Hsu 			goto done;
7431e78ac21SJeffrey Hsu 		}
7441e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
745fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
7461e78ac21SJeffrey Hsu #ifdef MAC
74730d239bcSRobert Watson 		mac_netinet_fragment(m0, m);
7481e78ac21SJeffrey Hsu #endif
7491e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
7501e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
7511e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
7521e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
7531e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
7541e78ac21SJeffrey Hsu 		*mnext = m;
7551e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
7561e78ac21SJeffrey Hsu 	}
7571e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
7581e78ac21SJeffrey Hsu 
7591e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
7601e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
7611e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
7621e78ac21SJeffrey Hsu 
7631e78ac21SJeffrey Hsu 	/*
7641e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
7651e78ac21SJeffrey Hsu 	 * and updating header.
7661e78ac21SJeffrey Hsu 	 */
7671e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
7681e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
7691e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
7701e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
7711e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
7721e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
7731e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
7741e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
7751e78ac21SJeffrey Hsu 
7761e78ac21SJeffrey Hsu done:
7771e78ac21SJeffrey Hsu 	*m_frag = m0;
7781e78ac21SJeffrey Hsu 	return error;
7791e78ac21SJeffrey Hsu }
7801e78ac21SJeffrey Hsu 
7811c238475SJonathan Lemon void
782db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
783db4f9cc7SJonathan Lemon {
784db4f9cc7SJonathan Lemon 	struct ip *ip;
785db4f9cc7SJonathan Lemon 	u_short csum, offset;
786db4f9cc7SJonathan Lemon 
787db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
78853be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
789db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
790206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
791206a3274SRuslan Ermilov 		csum = 0xffff;
792db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
793db4f9cc7SJonathan Lemon 
794db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
795db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
796db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
797db4f9cc7SJonathan Lemon 		/*
798db4f9cc7SJonathan Lemon 		 * XXX
799db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
800db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
8018f8d29f6SAndre Oppermann 		 * in the appropriate next mbuf in the chain.
802db4f9cc7SJonathan Lemon 		 */
8038f8d29f6SAndre Oppermann 		return;
804db4f9cc7SJonathan Lemon 	}
805db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
806db4f9cc7SJonathan Lemon }
807db4f9cc7SJonathan Lemon 
808df8bae1dSRodney W. Grimes /*
809df8bae1dSRodney W. Grimes  * IP socket option processing.
810df8bae1dSRodney W. Grimes  */
811df8bae1dSRodney W. Grimes int
812f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
813df8bae1dSRodney W. Grimes {
814cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
815cfe8b629SGarrett Wollman 	int	error, optval;
816df8bae1dSRodney W. Grimes 
817cfe8b629SGarrett Wollman 	error = optval = 0;
818cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
819cfe8b629SGarrett Wollman 		return (EINVAL);
820cfe8b629SGarrett Wollman 	}
821df8bae1dSRodney W. Grimes 
822cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
823cfe8b629SGarrett Wollman 	case SOPT_SET:
824cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
825df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
826df8bae1dSRodney W. Grimes #ifdef notyet
827df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
828df8bae1dSRodney W. Grimes #endif
829cfe8b629SGarrett Wollman 		{
830cfe8b629SGarrett Wollman 			struct mbuf *m;
831cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
832cfe8b629SGarrett Wollman 				error = EMSGSIZE;
833cfe8b629SGarrett Wollman 				break;
834cfe8b629SGarrett Wollman 			}
835ea26d587SRuslan Ermilov 			MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
8360b17fba7SAndre Oppermann 			if (m == NULL) {
837cfe8b629SGarrett Wollman 				error = ENOBUFS;
838cfe8b629SGarrett Wollman 				break;
839cfe8b629SGarrett Wollman 			}
840cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
841cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
842cfe8b629SGarrett Wollman 					    m->m_len);
843635354c4SMaxim Konovalov 			if (error) {
844635354c4SMaxim Konovalov 				m_free(m);
845635354c4SMaxim Konovalov 				break;
846635354c4SMaxim Konovalov 			}
8478501a69cSRobert Watson 			INP_WLOCK(inp);
848993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
8498501a69cSRobert Watson 			INP_WUNLOCK(inp);
850993d9505SRobert Watson 			return (error);
851cfe8b629SGarrett Wollman 		}
852df8bae1dSRodney W. Grimes 
853df8bae1dSRodney W. Grimes 		case IP_TOS:
854df8bae1dSRodney W. Grimes 		case IP_TTL:
855936cd18dSAndre Oppermann 		case IP_MINTTL:
856df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
857df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
858df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
8594957466bSMatthew N. Dodd 		case IP_RECVTTL:
86082c23ebaSBill Fenner 		case IP_RECVIF:
8616a800098SYoshinobu Inoue 		case IP_FAITH:
8628afa2304SBruce M Simpson 		case IP_ONESBCAST:
863b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
864cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
865cfe8b629SGarrett Wollman 					    sizeof optval);
866cfe8b629SGarrett Wollman 			if (error)
867cfe8b629SGarrett Wollman 				break;
868df8bae1dSRodney W. Grimes 
869cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
870df8bae1dSRodney W. Grimes 			case IP_TOS:
871ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
872df8bae1dSRodney W. Grimes 				break;
873df8bae1dSRodney W. Grimes 
874df8bae1dSRodney W. Grimes 			case IP_TTL:
875ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
876df8bae1dSRodney W. Grimes 				break;
877936cd18dSAndre Oppermann 
878936cd18dSAndre Oppermann 			case IP_MINTTL:
879936cd18dSAndre Oppermann 				if (optval > 0 && optval <= MAXTTL)
880936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
881936cd18dSAndre Oppermann 				else
882936cd18dSAndre Oppermann 					error = EINVAL;
883936cd18dSAndre Oppermann 				break;
884936cd18dSAndre Oppermann 
885a138d217SRobert Watson #define	OPTSET(bit) do {						\
8868501a69cSRobert Watson 	INP_WLOCK(inp);							\
887df8bae1dSRodney W. Grimes 	if (optval)							\
888df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
889df8bae1dSRodney W. Grimes 	else								\
890a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
8918501a69cSRobert Watson 	INP_WUNLOCK(inp);						\
892a138d217SRobert Watson } while (0)
893df8bae1dSRodney W. Grimes 
894df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
895df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
896df8bae1dSRodney W. Grimes 				break;
897df8bae1dSRodney W. Grimes 
898df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
899df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
900df8bae1dSRodney W. Grimes 				break;
901df8bae1dSRodney W. Grimes 
902df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
903df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
904df8bae1dSRodney W. Grimes 				break;
90582c23ebaSBill Fenner 
9064957466bSMatthew N. Dodd 			case IP_RECVTTL:
9074957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
9084957466bSMatthew N. Dodd 				break;
9094957466bSMatthew N. Dodd 
91082c23ebaSBill Fenner 			case IP_RECVIF:
91182c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
91282c23ebaSBill Fenner 				break;
9136a800098SYoshinobu Inoue 
9146a800098SYoshinobu Inoue 			case IP_FAITH:
9156a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
9166a800098SYoshinobu Inoue 				break;
9178afa2304SBruce M Simpson 
9188afa2304SBruce M Simpson 			case IP_ONESBCAST:
9198afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
9208afa2304SBruce M Simpson 				break;
921b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
922b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
923b2828ad2SAndre Oppermann 				break;
924df8bae1dSRodney W. Grimes 			}
925df8bae1dSRodney W. Grimes 			break;
926df8bae1dSRodney W. Grimes #undef OPTSET
927df8bae1dSRodney W. Grimes 
92871498f30SBruce M Simpson 		/*
92971498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
93071498f30SBruce M Simpson 		 * module.
93171498f30SBruce M Simpson 		 */
932df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
933f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
934df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
935df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
936df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
937df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
93871498f30SBruce M Simpson 		case IP_ADD_SOURCE_MEMBERSHIP:
93971498f30SBruce M Simpson 		case IP_DROP_SOURCE_MEMBERSHIP:
94071498f30SBruce M Simpson 		case IP_BLOCK_SOURCE:
94171498f30SBruce M Simpson 		case IP_UNBLOCK_SOURCE:
94271498f30SBruce M Simpson 		case IP_MSFILTER:
94371498f30SBruce M Simpson 		case MCAST_JOIN_GROUP:
94471498f30SBruce M Simpson 		case MCAST_LEAVE_GROUP:
94571498f30SBruce M Simpson 		case MCAST_JOIN_SOURCE_GROUP:
94671498f30SBruce M Simpson 		case MCAST_LEAVE_SOURCE_GROUP:
94771498f30SBruce M Simpson 		case MCAST_BLOCK_SOURCE:
94871498f30SBruce M Simpson 		case MCAST_UNBLOCK_SOURCE:
94971498f30SBruce M Simpson 			error = inp_setmoptions(inp, sopt);
950df8bae1dSRodney W. Grimes 			break;
951df8bae1dSRodney W. Grimes 
95233b3ac06SPeter Wemm 		case IP_PORTRANGE:
953cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
954cfe8b629SGarrett Wollman 					    sizeof optval);
955cfe8b629SGarrett Wollman 			if (error)
956cfe8b629SGarrett Wollman 				break;
95733b3ac06SPeter Wemm 
9588501a69cSRobert Watson 			INP_WLOCK(inp);
95933b3ac06SPeter Wemm 			switch (optval) {
96033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
96133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
96233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
96333b3ac06SPeter Wemm 				break;
96433b3ac06SPeter Wemm 
96533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
96633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
96733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
96833b3ac06SPeter Wemm 				break;
96933b3ac06SPeter Wemm 
97033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
97133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
97233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
97333b3ac06SPeter Wemm 				break;
97433b3ac06SPeter Wemm 
97533b3ac06SPeter Wemm 			default:
97633b3ac06SPeter Wemm 				error = EINVAL;
97733b3ac06SPeter Wemm 				break;
97833b3ac06SPeter Wemm 			}
9798501a69cSRobert Watson 			INP_WUNLOCK(inp);
980ce8c72b1SPeter Wemm 			break;
98133b3ac06SPeter Wemm 
982b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
9836a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
9846a800098SYoshinobu Inoue 		{
9856a800098SYoshinobu Inoue 			caddr_t req;
9866a800098SYoshinobu Inoue 			struct mbuf *m;
9876a800098SYoshinobu Inoue 
9886a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
9896a800098SYoshinobu Inoue 				break;
9906a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
9916a800098SYoshinobu Inoue 				break;
9926a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
993c26fe973SBjoern A. Zeeb 			error = ipsec4_set_policy(inp, sopt->sopt_name, req,
994c26fe973SBjoern A. Zeeb 			    m->m_len, (sopt->sopt_td != NULL) ?
995c26fe973SBjoern A. Zeeb 			    sopt->sopt_td->td_ucred : NULL);
9966a800098SYoshinobu Inoue 			m_freem(m);
9976a800098SYoshinobu Inoue 			break;
9986a800098SYoshinobu Inoue 		}
999b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
10006a800098SYoshinobu Inoue 
1001df8bae1dSRodney W. Grimes 		default:
1002df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1003df8bae1dSRodney W. Grimes 			break;
1004df8bae1dSRodney W. Grimes 		}
1005df8bae1dSRodney W. Grimes 		break;
1006df8bae1dSRodney W. Grimes 
1007cfe8b629SGarrett Wollman 	case SOPT_GET:
1008cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1009df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1010df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1011cfe8b629SGarrett Wollman 			if (inp->inp_options)
1012cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1013cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1014cfe8b629SGarrett Wollman 							  char *),
1015cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1016cfe8b629SGarrett Wollman 			else
1017cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1018df8bae1dSRodney W. Grimes 			break;
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes 		case IP_TOS:
1021df8bae1dSRodney W. Grimes 		case IP_TTL:
1022936cd18dSAndre Oppermann 		case IP_MINTTL:
1023df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1024df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1025df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10264957466bSMatthew N. Dodd 		case IP_RECVTTL:
102782c23ebaSBill Fenner 		case IP_RECVIF:
1028cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
10296a800098SYoshinobu Inoue 		case IP_FAITH:
10308afa2304SBruce M Simpson 		case IP_ONESBCAST:
1031b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1032cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1033df8bae1dSRodney W. Grimes 
1034df8bae1dSRodney W. Grimes 			case IP_TOS:
1035ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1036df8bae1dSRodney W. Grimes 				break;
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes 			case IP_TTL:
1039ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1040df8bae1dSRodney W. Grimes 				break;
1041df8bae1dSRodney W. Grimes 
1042936cd18dSAndre Oppermann 			case IP_MINTTL:
1043936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1044936cd18dSAndre Oppermann 				break;
1045936cd18dSAndre Oppermann 
1046df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1049df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1050df8bae1dSRodney W. Grimes 				break;
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1053df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1054df8bae1dSRodney W. Grimes 				break;
1055df8bae1dSRodney W. Grimes 
1056df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1057df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1058df8bae1dSRodney W. Grimes 				break;
105982c23ebaSBill Fenner 
10604957466bSMatthew N. Dodd 			case IP_RECVTTL:
10614957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
10624957466bSMatthew N. Dodd 				break;
10634957466bSMatthew N. Dodd 
106482c23ebaSBill Fenner 			case IP_RECVIF:
106582c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
106682c23ebaSBill Fenner 				break;
1067cfe8b629SGarrett Wollman 
1068cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1069cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1070cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1071cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1072cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1073cfe8b629SGarrett Wollman 				else
1074cfe8b629SGarrett Wollman 					optval = 0;
1075cfe8b629SGarrett Wollman 				break;
10766a800098SYoshinobu Inoue 
10776a800098SYoshinobu Inoue 			case IP_FAITH:
10786a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
10796a800098SYoshinobu Inoue 				break;
10808afa2304SBruce M Simpson 
10818afa2304SBruce M Simpson 			case IP_ONESBCAST:
10828afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
10838afa2304SBruce M Simpson 				break;
1084b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1085b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1086b2828ad2SAndre Oppermann 				break;
1087df8bae1dSRodney W. Grimes 			}
1088cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1089df8bae1dSRodney W. Grimes 			break;
1090df8bae1dSRodney W. Grimes 
109171498f30SBruce M Simpson 		/*
109271498f30SBruce M Simpson 		 * Multicast socket options are processed by the in_mcast
109371498f30SBruce M Simpson 		 * module.
109471498f30SBruce M Simpson 		 */
1095df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1096f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1097df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1098df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
109971498f30SBruce M Simpson 		case IP_MSFILTER:
110071498f30SBruce M Simpson 			error = inp_getmoptions(inp, sopt);
110133b3ac06SPeter Wemm 			break;
110233b3ac06SPeter Wemm 
1103b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
11046a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11056a800098SYoshinobu Inoue 		{
1106f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
11076a800098SYoshinobu Inoue 			caddr_t req = NULL;
1108686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
11096a800098SYoshinobu Inoue 
1110686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
11116a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1112686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1113686cdd19SJun-ichiro itojun Hagino 			}
1114686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
11156a800098SYoshinobu Inoue 			if (error == 0)
11166a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1117f63e7634SYoshinobu Inoue 			if (error == 0)
11186a800098SYoshinobu Inoue 				m_freem(m);
11196a800098SYoshinobu Inoue 			break;
11206a800098SYoshinobu Inoue 		}
1121b2630c29SGeorge V. Neville-Neil #endif /* IPSEC */
11226a800098SYoshinobu Inoue 
1123df8bae1dSRodney W. Grimes 		default:
1124df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1125df8bae1dSRodney W. Grimes 			break;
1126df8bae1dSRodney W. Grimes 		}
1127df8bae1dSRodney W. Grimes 		break;
1128df8bae1dSRodney W. Grimes 	}
1129df8bae1dSRodney W. Grimes 	return (error);
1130df8bae1dSRodney W. Grimes }
1131df8bae1dSRodney W. Grimes 
1132df8bae1dSRodney W. Grimes /*
1133df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1134df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1135df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1136f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1137f5fea3ddSPaul Traina  * replicating that code here.
1138df8bae1dSRodney W. Grimes  */
1139df8bae1dSRodney W. Grimes static void
1140f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1141f2565d68SRobert Watson     int hlen)
1142df8bae1dSRodney W. Grimes {
1143b375c9ecSLuigi Rizzo 	register struct ip *ip;
1144df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1145df8bae1dSRodney W. Grimes 
1146b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
114786b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
114886b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1149df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1150390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1151390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1152390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1153390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1154390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1155390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1156390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1157390cdc6aSRuslan Ermilov 		}
1158df8bae1dSRodney W. Grimes 		/*
1159df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1160df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1161df8bae1dSRodney W. Grimes 		 */
1162df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1163fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1164fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1165df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
116686b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
11679c9137eaSGarrett Wollman 		/*
11689c9137eaSGarrett Wollman 		 * NB:
1169e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1170e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1171e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1172e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1173e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1174e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1175e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1176ed7509acSJulian Elischer 		 * of safety by using if_simloop().
11779c9137eaSGarrett Wollman 		 */
1178201c2527SJulian Elischer #if 1 /* XXX */
1179201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
11802b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1181201c2527SJulian Elischer 						dst->sin_family);
1182201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1183201c2527SJulian Elischer 		}
1184201c2527SJulian Elischer #endif
1185201c2527SJulian Elischer 
11869c9137eaSGarrett Wollman #ifdef notdef
1187e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1188ed7509acSJulian Elischer 		ip_input(copym);
11899c9137eaSGarrett Wollman #else
119006a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
11919c9137eaSGarrett Wollman #endif
1192df8bae1dSRodney W. Grimes 	}
1193df8bae1dSRodney W. Grimes }
1194