xref: /freebsd/sys/netinet/ip_output.c (revision f2565d68a4e5922928b92ad6043be96ada8cb7b3)
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
30c3aac50fSPeter Wemm  * $FreeBSD$
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
331f7e052cSJulian Elischer #include "opt_ipfw.h"
346a800098SYoshinobu Inoue #include "opt_ipsec.h"
354ed84624SRobert Watson #include "opt_mac.h"
3653dcc544SMike Silbersack #include "opt_mbuf_stress_test.h"
37fbd1372aSJoerg Wunsch 
38df8bae1dSRodney W. Grimes #include <sys/param.h>
3926f9a767SRodney W. Grimes #include <sys/systm.h>
40f0f6d643SLuigi Rizzo #include <sys/kernel.h>
41df8bae1dSRodney W. Grimes #include <sys/malloc.h>
42df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
43acd3428bSRobert Watson #include <sys/priv.h>
44df8bae1dSRodney W. Grimes #include <sys/protosw.h>
45df8bae1dSRodney W. Grimes #include <sys/socket.h>
46df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
479d9edc56SMike Silbersack #include <sys/sysctl.h>
48df8bae1dSRodney W. Grimes 
49df8bae1dSRodney W. Grimes #include <net/if.h>
509b932e9eSAndre Oppermann #include <net/netisr.h>
51c21fd232SAndre Oppermann #include <net/pfil.h>
52df8bae1dSRodney W. Grimes #include <net/route.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <netinet/in.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip.h>
57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
60ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
61df8bae1dSRodney W. Grimes 
621dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
631dfcf0d2SAndre Oppermann #include <netinet/ip_ipsec.h>
646a800098SYoshinobu Inoue #ifdef IPSEC
656a800098SYoshinobu Inoue #include <netinet6/ipsec.h>
661dfcf0d2SAndre Oppermann #endif
671dfcf0d2SAndre Oppermann #ifdef FAST_IPSEC
681dfcf0d2SAndre Oppermann #include <netipsec/ipsec.h>
696a800098SYoshinobu Inoue #endif
706a800098SYoshinobu Inoue #endif /*IPSEC*/
716a800098SYoshinobu Inoue 
721dfcf0d2SAndre Oppermann #include <machine/in_cksum.h>
731dfcf0d2SAndre Oppermann 
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson 
761dfcf0d2SAndre Oppermann static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
77b9234fafSSam Leffler 
782b25acc1SLuigi Rizzo #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
792b25acc1SLuigi Rizzo 				x, (ntohl(a.s_addr)>>24)&0xFF,\
80f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>16)&0xFF,\
81f9e354dfSJulian Elischer 				  (ntohl(a.s_addr)>>8)&0xFF,\
822b25acc1SLuigi Rizzo 				  (ntohl(a.s_addr))&0xFF, y);
83f9e354dfSJulian Elischer 
84f23b4c91SGarrett Wollman u_short ip_id;
85f23b4c91SGarrett Wollman 
8653dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
879d9edc56SMike Silbersack int mbuf_frag_size = 0;
889d9edc56SMike Silbersack SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
899d9edc56SMike Silbersack 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
909d9edc56SMike Silbersack #endif
919d9edc56SMike Silbersack 
924d77a549SAlfred Perlstein static struct ifnet *ip_multicast_if(struct in_addr *, int *);
93df8bae1dSRodney W. Grimes static void	ip_mloopback
944d77a549SAlfred Perlstein 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
9589924e58SRobert Watson static int	ip_getmoptions(struct inpcb *, struct sockopt *);
965c918b56SRobert Watson static int	ip_setmoptions(struct inpcb *, struct sockopt *);
97afed1b49SDarren Reed 
98afed1b49SDarren Reed 
9993e0e116SJulian Elischer extern	struct protosw inetsw[];
10093e0e116SJulian Elischer 
101df8bae1dSRodney W. Grimes /*
102df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
103df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
104df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
105df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
1063de758d3SRobert Watson  * In the IP forwarding case, the packet will arrive with options already
1073de758d3SRobert Watson  * inserted, so must have a NULL opt pointer.
108df8bae1dSRodney W. Grimes  */
109df8bae1dSRodney W. Grimes int
110f2565d68SRobert Watson ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
111f2565d68SRobert Watson     struct ip_moptions *imo, struct inpcb *inp)
112df8bae1dSRodney W. Grimes {
1131e78ac21SJeffrey Hsu 	struct ip *ip;
1142b25acc1SLuigi Rizzo 	struct ifnet *ifp = NULL;	/* keep compiler happy */
115ac9d7e26SMax Laier 	struct mbuf *m0;
11623bf9953SPoul-Henning Kamp 	int hlen = sizeof (struct ip);
1173ae2ad08SJohn-Mark Gurney 	int mtu;
1189b932e9eSAndre Oppermann 	int len, error = 0;
1192b25acc1SLuigi Rizzo 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
1203956b023SLuigi Rizzo 	struct in_ifaddr *ia = NULL;
121db4f9cc7SJonathan Lemon 	int isbroadcast, sw_csum;
122e3f406b3SRuslan Ermilov 	struct route iproute;
1239b932e9eSAndre Oppermann 	struct in_addr odst;
1249b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1259b932e9eSAndre Oppermann 	struct m_tag *fwd_tag = NULL;
1269b932e9eSAndre Oppermann #endif
127ac9d7e26SMax Laier 	M_ASSERTPKTHDR(m);
12836e8826fSMax Laier 
12902c1c707SAndre Oppermann 	if (ro == NULL) {
13002c1c707SAndre Oppermann 		ro = &iproute;
13102c1c707SAndre Oppermann 		bzero(ro, sizeof (*ro));
13202c1c707SAndre Oppermann 	}
13302c1c707SAndre Oppermann 
13484843845SSam Leffler 	if (inp != NULL)
13584843845SSam Leffler 		INP_LOCK_ASSERT(inp);
1362b25acc1SLuigi Rizzo 
137df8bae1dSRodney W. Grimes 	if (opt) {
138cb7641e8SMaxim Konovalov 		len = 0;
139df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
140cb7641e8SMaxim Konovalov 		if (len != 0)
141df8bae1dSRodney W. Grimes 			hlen = len;
142df8bae1dSRodney W. Grimes 	}
143df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
1443efc3014SJulian Elischer 
145df8bae1dSRodney W. Grimes 	/*
1466e49b1feSGarrett Wollman 	 * Fill in IP header.  If we are not allowing fragmentation,
147e0f630eaSAndre Oppermann 	 * then the ip_id field is meaningless, but we don't set it
148e0f630eaSAndre Oppermann 	 * to zero.  Doing so causes various problems when devices along
149e0f630eaSAndre Oppermann 	 * the path (routers, load balancers, firewalls, etc.) illegally
150e0f630eaSAndre Oppermann 	 * disable DF on our packet.  Note that a 16-bit counter
1516e49b1feSGarrett Wollman 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
1526e49b1feSGarrett Wollman 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
1536e49b1feSGarrett Wollman 	 * for Counting NATted Hosts", Proc. IMW'02, available at
1544d09f5a0SGleb Smirnoff 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
155df8bae1dSRodney W. Grimes 	 */
156df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
15753be11f6SPoul-Henning Kamp 		ip->ip_v = IPVERSION;
15853be11f6SPoul-Henning Kamp 		ip->ip_hl = hlen >> 2;
1591f44b0a1SDavid Malone 		ip->ip_id = ip_newid();
160df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
161df8bae1dSRodney W. Grimes 	} else {
16253be11f6SPoul-Henning Kamp 		hlen = ip->ip_hl << 2;
163df8bae1dSRodney W. Grimes 	}
1649c9137eaSGarrett Wollman 
165df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
1669b932e9eSAndre Oppermann again:
167df8bae1dSRodney W. Grimes 	/*
168df8bae1dSRodney W. Grimes 	 * If there is a cached route,
169df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
170df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
171a4a6e773SHajimu UMEMOTO 	 * The address family should also be checked in case of sharing the
172a4a6e773SHajimu UMEMOTO 	 * cache with IPv6.
173df8bae1dSRodney W. Grimes 	 */
174df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
175a4a6e773SHajimu UMEMOTO 			  dst->sin_family != AF_INET ||
1769b932e9eSAndre Oppermann 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
177df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
1783ae2ad08SJohn-Mark Gurney 		ro->ro_rt = (struct rtentry *)NULL;
179df8bae1dSRodney W. Grimes 	}
1809b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
1819b932e9eSAndre Oppermann 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
1829b932e9eSAndre Oppermann #else
1830b17fba7SAndre Oppermann 	if (ro->ro_rt == NULL) {
1849b932e9eSAndre Oppermann #endif
185a4a6e773SHajimu UMEMOTO 		bzero(dst, sizeof(*dst));
186df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
187df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
1889b932e9eSAndre Oppermann 		dst->sin_addr = ip->ip_dst;
189df8bae1dSRodney W. Grimes 	}
190df8bae1dSRodney W. Grimes 	/*
191a3fd02d8SBruce M Simpson 	 * If routing to interface only, short circuit routing lookup.
192a3fd02d8SBruce M Simpson 	 * The use of an all-ones broadcast address implies this; an
193a3fd02d8SBruce M Simpson 	 * interface is specified by the broadcast address of an interface,
194a3fd02d8SBruce M Simpson 	 * or the destination address of a ptp interface.
195df8bae1dSRodney W. Grimes 	 */
196a3fd02d8SBruce M Simpson 	if (flags & IP_SENDONES) {
197a3fd02d8SBruce M Simpson 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
198a3fd02d8SBruce M Simpson 		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
199a3fd02d8SBruce M Simpson 			ipstat.ips_noroute++;
200a3fd02d8SBruce M Simpson 			error = ENETUNREACH;
201a3fd02d8SBruce M Simpson 			goto bad;
202a3fd02d8SBruce M Simpson 		}
203a3fd02d8SBruce M Simpson 		ip->ip_dst.s_addr = INADDR_BROADCAST;
204a3fd02d8SBruce M Simpson 		dst->sin_addr = ip->ip_dst;
205a3fd02d8SBruce M Simpson 		ifp = ia->ia_ifp;
206a3fd02d8SBruce M Simpson 		ip->ip_ttl = 1;
207a3fd02d8SBruce M Simpson 		isbroadcast = 1;
208a3fd02d8SBruce M Simpson 	} else if (flags & IP_ROUTETOIF) {
2090b17fba7SAndre Oppermann 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
2100b17fba7SAndre Oppermann 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
211df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
212df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
213df8bae1dSRodney W. Grimes 			goto bad;
214df8bae1dSRodney W. Grimes 		}
215df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
216df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
2179f9b3dc4SGarrett Wollman 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
2183afefa39SDaniel C. Sobral 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
21938c1bc35SRuslan Ermilov 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
2203afefa39SDaniel C. Sobral 		/*
22138c1bc35SRuslan Ermilov 		 * Bypass the normal routing lookup for multicast
22238c1bc35SRuslan Ermilov 		 * packets if the interface is specified.
2233afefa39SDaniel C. Sobral 		 */
22438c1bc35SRuslan Ermilov 		ifp = imo->imo_multicast_ifp;
22538c1bc35SRuslan Ermilov 		IFP_TO_IA(ifp, ia);
22638c1bc35SRuslan Ermilov 		isbroadcast = 0;	/* fool gcc */
227df8bae1dSRodney W. Grimes 	} else {
2282c17fe93SGarrett Wollman 		/*
22997d8d152SAndre Oppermann 		 * We want to do any cloning requested by the link layer,
23097d8d152SAndre Oppermann 		 * as this is probably required in all cases for correct
23197d8d152SAndre Oppermann 		 * operation (as it is for ARP).
2322c17fe93SGarrett Wollman 		 */
2330b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL)
234e6e51f05SLuigi Rizzo 			rtalloc_ign(ro, 0);
2350b17fba7SAndre Oppermann 		if (ro->ro_rt == NULL) {
236df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
237df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
238df8bae1dSRodney W. Grimes 			goto bad;
239df8bae1dSRodney W. Grimes 		}
240df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
241df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
24297d8d152SAndre Oppermann 		ro->ro_rt->rt_rmx.rmx_pksent++;
243df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
244f2c2962eSRuslan Ermilov 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
2459f9b3dc4SGarrett Wollman 		if (ro->ro_rt->rt_flags & RTF_HOST)
246f2c2962eSRuslan Ermilov 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
2479f9b3dc4SGarrett Wollman 		else
2489f9b3dc4SGarrett Wollman 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
249df8bae1dSRodney W. Grimes 	}
2503ae2ad08SJohn-Mark Gurney 	/*
2513ae2ad08SJohn-Mark Gurney 	 * Calculate MTU.  If we have a route that is up, use that,
2523ae2ad08SJohn-Mark Gurney 	 * otherwise use the interface's MTU.
2533ae2ad08SJohn-Mark Gurney 	 */
254384a05bfSAndre Oppermann 	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
2553ae2ad08SJohn-Mark Gurney 		/*
2563ae2ad08SJohn-Mark Gurney 		 * This case can happen if the user changed the MTU
2573ae2ad08SJohn-Mark Gurney 		 * of an interface after enabling IP on it.  Because
2583ae2ad08SJohn-Mark Gurney 		 * most netifs don't keep track of routes pointing to
2593ae2ad08SJohn-Mark Gurney 		 * them, there is no way for one to update all its
2603ae2ad08SJohn-Mark Gurney 		 * routes when the MTU is changed.
2613ae2ad08SJohn-Mark Gurney 		 */
2623ae2ad08SJohn-Mark Gurney 		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
2633ae2ad08SJohn-Mark Gurney 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
2643ae2ad08SJohn-Mark Gurney 		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
2653ae2ad08SJohn-Mark Gurney 	} else {
2663ae2ad08SJohn-Mark Gurney 		mtu = ifp->if_mtu;
2673ae2ad08SJohn-Mark Gurney 	}
2689b932e9eSAndre Oppermann 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
269df8bae1dSRodney W. Grimes 		struct in_multi *inm;
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
272df8bae1dSRodney W. Grimes 		/*
273df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
274df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
275df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
276df8bae1dSRodney W. Grimes 		 */
277df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
278df8bae1dSRodney W. Grimes 		/*
279df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
280df8bae1dSRodney W. Grimes 		 */
281df8bae1dSRodney W. Grimes 		if (imo != NULL) {
282df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
2831c5de19aSGarrett Wollman 			if (imo->imo_multicast_vif != -1)
2841c5de19aSGarrett Wollman 				ip->ip_src.s_addr =
285bbb4330bSLuigi Rizzo 				    ip_mcast_src ?
286bbb4330bSLuigi Rizzo 				    ip_mcast_src(imo->imo_multicast_vif) :
287bbb4330bSLuigi Rizzo 				    INADDR_ANY;
288df8bae1dSRodney W. Grimes 		} else
289df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
290df8bae1dSRodney W. Grimes 		/*
291df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
292df8bae1dSRodney W. Grimes 		 */
2931c5de19aSGarrett Wollman 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
294df8bae1dSRodney W. Grimes 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
295df8bae1dSRodney W. Grimes 				ipstat.ips_noroute++;
296df8bae1dSRodney W. Grimes 				error = ENETUNREACH;
297df8bae1dSRodney W. Grimes 				goto bad;
298df8bae1dSRodney W. Grimes 			}
2991c5de19aSGarrett Wollman 		}
300df8bae1dSRodney W. Grimes 		/*
301df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
302df8bae1dSRodney W. Grimes 		 * of outgoing interface.
303df8bae1dSRodney W. Grimes 		 */
304df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
30538c1bc35SRuslan Ermilov 			/* Interface may have no addresses. */
30638c1bc35SRuslan Ermilov 			if (ia != NULL)
30738c1bc35SRuslan Ermilov 				ip->ip_src = IA_SIN(ia)->sin_addr;
308df8bae1dSRodney W. Grimes 		}
309df8bae1dSRodney W. Grimes 
310dd5a318bSRobert Watson 		IN_MULTI_LOCK();
3119b932e9eSAndre Oppermann 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
312df8bae1dSRodney W. Grimes 		if (inm != NULL &&
313df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
314dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
315df8bae1dSRodney W. Grimes 			/*
316df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
317df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
318df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
319df8bae1dSRodney W. Grimes 			 */
32086b1d6d2SBill Fenner 			ip_mloopback(ifp, m, dst, hlen);
321df8bae1dSRodney W. Grimes 		}
322df8bae1dSRodney W. Grimes 		else {
323dd5a318bSRobert Watson 			IN_MULTI_UNLOCK();
324df8bae1dSRodney W. Grimes 			/*
325df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
326df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
327df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
328df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
329df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
330df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
331df8bae1dSRodney W. Grimes 			 *
332df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
333df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
334df8bae1dSRodney W. Grimes 			 * if necessary.
335df8bae1dSRodney W. Grimes 			 */
336df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
337f0068c4aSGarrett Wollman 				/*
338bbb4330bSLuigi Rizzo 				 * If rsvp daemon is not running, do not
339f0068c4aSGarrett Wollman 				 * set ip_moptions. This ensures that the packet
340f0068c4aSGarrett Wollman 				 * is multicast and not just sent down one link
341f0068c4aSGarrett Wollman 				 * as prescribed by rsvpd.
342f0068c4aSGarrett Wollman 				 */
343b124e4f2SGarrett Wollman 				if (!rsvp_on)
344f0068c4aSGarrett Wollman 					imo = NULL;
345bbb4330bSLuigi Rizzo 				if (ip_mforward &&
346bbb4330bSLuigi Rizzo 				    ip_mforward(ip, ifp, m, imo) != 0) {
347df8bae1dSRodney W. Grimes 					m_freem(m);
348df8bae1dSRodney W. Grimes 					goto done;
349df8bae1dSRodney W. Grimes 				}
350df8bae1dSRodney W. Grimes 			}
351df8bae1dSRodney W. Grimes 		}
3525e9ae478SGarrett Wollman 
353df8bae1dSRodney W. Grimes 		/*
354df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
355df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
356df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
357df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
358df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
359df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
360df8bae1dSRodney W. Grimes 		 */
361f5fea3ddSPaul Traina 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
362df8bae1dSRodney W. Grimes 			m_freem(m);
363df8bae1dSRodney W. Grimes 			goto done;
364df8bae1dSRodney W. Grimes 		}
365df8bae1dSRodney W. Grimes 
366df8bae1dSRodney W. Grimes 		goto sendit;
367df8bae1dSRodney W. Grimes 	}
3686a7c943cSAndre Oppermann 
369df8bae1dSRodney W. Grimes 	/*
3702b25acc1SLuigi Rizzo 	 * If the source address is not specified yet, use the address
371cb459254SJohn-Mark Gurney 	 * of the outoing interface.
372df8bae1dSRodney W. Grimes 	 */
373f9e354dfSJulian Elischer 	if (ip->ip_src.s_addr == INADDR_ANY) {
37438c1bc35SRuslan Ermilov 		/* Interface may have no addresses. */
37538c1bc35SRuslan Ermilov 		if (ia != NULL) {
376df8bae1dSRodney W. Grimes 			ip->ip_src = IA_SIN(ia)->sin_addr;
377f9e354dfSJulian Elischer 		}
37838c1bc35SRuslan Ermilov 	}
3796a7c943cSAndre Oppermann 
380ca7a789aSMax Laier 	/*
381ca7a789aSMax Laier 	 * Verify that we have any chance at all of being able to queue the
382ca7a789aSMax Laier 	 * packet or packet fragments, unless ALTQ is enabled on the given
383ca7a789aSMax Laier 	 * interface in which case packetdrop should be done by queueing.
384ca7a789aSMax Laier 	 */
38502b199f1SMax Laier #ifdef ALTQ
386ca7a789aSMax Laier 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
3873ae2ad08SJohn-Mark Gurney 	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
388ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen))
389ca7a789aSMax Laier #else
3903ae2ad08SJohn-Mark Gurney 	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
391ca7a789aSMax Laier 	    ifp->if_snd.ifq_maxlen)
392ca7a789aSMax Laier #endif /* ALTQ */
393ca7a789aSMax Laier 	{
394b5390296SDavid Greenman 		error = ENOBUFS;
39535609d45SJonathan Lemon 		ipstat.ips_odropped++;
396bbce982bSGleb Smirnoff 		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
397b5390296SDavid Greenman 		goto bad;
398b5390296SDavid Greenman 	}
399b5390296SDavid Greenman 
400b5390296SDavid Greenman 	/*
401df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
4028c3f5566SRuslan Ermilov 	 * verify user is allowed to send
403df8bae1dSRodney W. Grimes 	 * such a packet.
404df8bae1dSRodney W. Grimes 	 */
4059f9b3dc4SGarrett Wollman 	if (isbroadcast) {
406df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
407df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
408df8bae1dSRodney W. Grimes 			goto bad;
409df8bae1dSRodney W. Grimes 		}
410df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
411df8bae1dSRodney W. Grimes 			error = EACCES;
412df8bae1dSRodney W. Grimes 			goto bad;
413df8bae1dSRodney W. Grimes 		}
414df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
4153ae2ad08SJohn-Mark Gurney 		if (ip->ip_len > mtu) {
416df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
417df8bae1dSRodney W. Grimes 			goto bad;
418df8bae1dSRodney W. Grimes 		}
419df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
4209f9b3dc4SGarrett Wollman 	} else {
421df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
4229f9b3dc4SGarrett Wollman 	}
423df8bae1dSRodney W. Grimes 
424f1743588SDarren Reed sendit:
4251dfcf0d2SAndre Oppermann #if defined(IPSEC) || defined(FAST_IPSEC)
4261dfcf0d2SAndre Oppermann 	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
4271dfcf0d2SAndre Oppermann 	case 1:
42833841545SHajimu UMEMOTO 		goto bad;
4291dfcf0d2SAndre Oppermann 	case -1:
4301dfcf0d2SAndre Oppermann 		goto done;
4311dfcf0d2SAndre Oppermann 	case 0:
43233841545SHajimu UMEMOTO 	default:
4331dfcf0d2SAndre Oppermann 		break;	/* Continue with packet processing. */
43433841545SHajimu UMEMOTO 	}
4351dfcf0d2SAndre Oppermann 	/* Update variables that are affected by ipsec4_output(). */
43633841545SHajimu UMEMOTO 	ip = mtod(m, struct ip *);
43733841545SHajimu UMEMOTO 	hlen = ip->ip_hl << 2;
43833841545SHajimu UMEMOTO #endif /* IPSEC */
43933841545SHajimu UMEMOTO 
440c21fd232SAndre Oppermann 	/* Jump over all PFIL processing if hooks are not active. */
441604afec4SChristian S.J. Peron 	if (!PFIL_HOOKED(&inet_pfil_hook))
442c21fd232SAndre Oppermann 		goto passout;
443c21fd232SAndre Oppermann 
444c21fd232SAndre Oppermann 	/* Run through list of hooks for output packets. */
4459b932e9eSAndre Oppermann 	odst.s_addr = ip->ip_dst.s_addr;
446d6a8d588SMax Laier 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
447134ea224SSam Leffler 	if (error != 0 || m == NULL)
448c4ac87eaSDarren Reed 		goto done;
4499b932e9eSAndre Oppermann 
450c4ac87eaSDarren Reed 	ip = mtod(m, struct ip *);
451fed1c7e9SSøren Schmidt 
4529b932e9eSAndre Oppermann 	/* See if destination IP address was changed by packet filter. */
4539b932e9eSAndre Oppermann 	if (odst.s_addr != ip->ip_dst.s_addr) {
4549b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
455f4fca2d8SAndre Oppermann 		/* If destination is now ourself drop to ip_input(). */
4569b932e9eSAndre Oppermann 		if (in_localip(ip->ip_dst)) {
4579b932e9eSAndre Oppermann 			m->m_flags |= M_FASTFWD_OURS;
458f9e354dfSJulian Elischer 			if (m->m_pkthdr.rcvif == NULL)
459a9c92b54SAndre Oppermann 				m->m_pkthdr.rcvif = loif;
46020c822f3SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
46120c822f3SJonathan Lemon 				m->m_pkthdr.csum_flags |=
46220c822f3SJonathan Lemon 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
463ac9d7e26SMax Laier 				m->m_pkthdr.csum_data = 0xffff;
46420c822f3SJonathan Lemon 			}
46520c822f3SJonathan Lemon 			m->m_pkthdr.csum_flags |=
46620c822f3SJonathan Lemon 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4679b932e9eSAndre Oppermann 
4689b932e9eSAndre Oppermann 			error = netisr_queue(NETISR_IP, m);
4699b932e9eSAndre Oppermann 			goto done;
4709b932e9eSAndre Oppermann 		} else
471f4fca2d8SAndre Oppermann 			goto again;	/* Redo the routing table lookup. */
4729b932e9eSAndre Oppermann 	}
4739b932e9eSAndre Oppermann 
4749b932e9eSAndre Oppermann #ifdef IPFIREWALL_FORWARD
4759b932e9eSAndre Oppermann 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
4769b932e9eSAndre Oppermann 	if (m->m_flags & M_FASTFWD_OURS) {
4779b932e9eSAndre Oppermann 		if (m->m_pkthdr.rcvif == NULL)
478a9c92b54SAndre Oppermann 			m->m_pkthdr.rcvif = loif;
4799b932e9eSAndre Oppermann 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
4809b932e9eSAndre Oppermann 			m->m_pkthdr.csum_flags |=
4819b932e9eSAndre Oppermann 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
4829b932e9eSAndre Oppermann 			m->m_pkthdr.csum_data = 0xffff;
4839b932e9eSAndre Oppermann 		}
4849b932e9eSAndre Oppermann 		m->m_pkthdr.csum_flags |=
4859b932e9eSAndre Oppermann 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
4869b932e9eSAndre Oppermann 
4879b932e9eSAndre Oppermann 		error = netisr_queue(NETISR_IP, m);
488f9e354dfSJulian Elischer 		goto done;
489f9e354dfSJulian Elischer 	}
4909b932e9eSAndre Oppermann 	/* Or forward to some other address? */
4919b932e9eSAndre Oppermann 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
4929b932e9eSAndre Oppermann 	if (fwd_tag) {
4939b932e9eSAndre Oppermann 		dst = (struct sockaddr_in *)&ro->ro_dst;
4949b932e9eSAndre Oppermann 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
4959b932e9eSAndre Oppermann 		m->m_flags |= M_SKIP_FIREWALL;
4969b932e9eSAndre Oppermann 		m_tag_delete(m, fwd_tag);
4979b932e9eSAndre Oppermann 		goto again;
498099dd043SAndre Oppermann 	}
499099dd043SAndre Oppermann #endif /* IPFIREWALL_FORWARD */
5002b25acc1SLuigi Rizzo 
501c21fd232SAndre Oppermann passout:
50251c8ec4aSRuslan Ermilov 	/* 127/8 must not appear on wire - RFC1122. */
50351c8ec4aSRuslan Ermilov 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
50451c8ec4aSRuslan Ermilov 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
50551c8ec4aSRuslan Ermilov 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
50651c8ec4aSRuslan Ermilov 			ipstat.ips_badaddr++;
50751c8ec4aSRuslan Ermilov 			error = EADDRNOTAVAIL;
50851c8ec4aSRuslan Ermilov 			goto bad;
50951c8ec4aSRuslan Ermilov 		}
51051c8ec4aSRuslan Ermilov 	}
51151c8ec4aSRuslan Ermilov 
512206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags |= CSUM_IP;
513206a3274SRuslan Ermilov 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
514db4f9cc7SJonathan Lemon 	if (sw_csum & CSUM_DELAY_DATA) {
515db4f9cc7SJonathan Lemon 		in_delayed_cksum(m);
516db4f9cc7SJonathan Lemon 		sw_csum &= ~CSUM_DELAY_DATA;
517db4f9cc7SJonathan Lemon 	}
518206a3274SRuslan Ermilov 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
519db4f9cc7SJonathan Lemon 
520e7319babSPoul-Henning Kamp 	/*
521db4f9cc7SJonathan Lemon 	 * If small enough for interface, or the interface will take
522233dcce1SAndre Oppermann 	 * care of the fragmentation for us, we can just send directly.
523df8bae1dSRodney W. Grimes 	 */
5243ae2ad08SJohn-Mark Gurney 	if (ip->ip_len <= mtu ||
525233dcce1SAndre Oppermann 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
526233dcce1SAndre Oppermann 	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
527fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
528fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
529df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
53053be11f6SPoul-Henning Kamp 		if (sw_csum & CSUM_DELAY_IP)
531df8bae1dSRodney W. Grimes 			ip->ip_sum = in_cksum(m, hlen);
5325da9f8faSJosef Karthauser 
533233dcce1SAndre Oppermann 		/*
534233dcce1SAndre Oppermann 		 * Record statistics for this interface address.
535233dcce1SAndre Oppermann 		 * With CSUM_TSO the byte/packet count will be slightly
536233dcce1SAndre Oppermann 		 * incorrect because we count the IP+TCP headers only
537233dcce1SAndre Oppermann 		 * once instead of for every generated packet.
538233dcce1SAndre Oppermann 		 */
53938c1bc35SRuslan Ermilov 		if (!(flags & IP_FORWARDING) && ia) {
540233dcce1SAndre Oppermann 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
5413dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets +=
542233dcce1SAndre Oppermann 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
543233dcce1SAndre Oppermann 			else
5443dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5453dbee59bSBruce M Simpson 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
5465da9f8faSJosef Karthauser 		}
54733841545SHajimu UMEMOTO #ifdef IPSEC
54833841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
54933841545SHajimu UMEMOTO 		ipsec_delaux(m);
55033841545SHajimu UMEMOTO #endif
55153dcc544SMike Silbersack #ifdef MBUF_STRESS_TEST
5523390d476SMike Silbersack 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
5533390d476SMike Silbersack 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
5549d9edc56SMike Silbersack #endif
555147f74d1SAndre Oppermann 		/*
556147f74d1SAndre Oppermann 		 * Reset layer specific mbuf flags
557147f74d1SAndre Oppermann 		 * to avoid confusing lower layers.
558147f74d1SAndre Oppermann 		 */
559147f74d1SAndre Oppermann 		m->m_flags &= ~(M_PROTOFLAGS);
560147f74d1SAndre Oppermann 
561df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
562df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
563df8bae1dSRodney W. Grimes 		goto done;
564df8bae1dSRodney W. Grimes 	}
5651e78ac21SJeffrey Hsu 
566233dcce1SAndre Oppermann 	/* Balk when DF bit is set or the interface didn't support TSO. */
567233dcce1SAndre Oppermann 	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
568df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
569df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
570df8bae1dSRodney W. Grimes 		goto bad;
571df8bae1dSRodney W. Grimes 	}
5721e78ac21SJeffrey Hsu 
5731e78ac21SJeffrey Hsu 	/*
5741e78ac21SJeffrey Hsu 	 * Too large for interface; fragment if possible. If successful,
5751e78ac21SJeffrey Hsu 	 * on return, m will point to a list of packets to be sent.
5761e78ac21SJeffrey Hsu 	 */
5773ae2ad08SJohn-Mark Gurney 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
5781e78ac21SJeffrey Hsu 	if (error)
579df8bae1dSRodney W. Grimes 		goto bad;
5801e78ac21SJeffrey Hsu 	for (; m; m = m0) {
581df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
582b375c9ecSLuigi Rizzo 		m->m_nextpkt = 0;
58333841545SHajimu UMEMOTO #ifdef IPSEC
58433841545SHajimu UMEMOTO 		/* clean ipsec history once it goes out of the node */
58533841545SHajimu UMEMOTO 		ipsec_delaux(m);
58633841545SHajimu UMEMOTO #endif
58707203494SDaniel C. Sobral 		if (error == 0) {
588fe937674SJosef Karthauser 			/* Record statistics for this interface address. */
58907203494SDaniel C. Sobral 			if (ia != NULL) {
5903dbee59bSBruce M Simpson 				ia->ia_ifa.if_opackets++;
5913dbee59bSBruce M Simpson 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
59207203494SDaniel C. Sobral 			}
593147f74d1SAndre Oppermann 			/*
594147f74d1SAndre Oppermann 			 * Reset layer specific mbuf flags
595147f74d1SAndre Oppermann 			 * to avoid confusing upper layers.
596147f74d1SAndre Oppermann 			 */
597147f74d1SAndre Oppermann 			m->m_flags &= ~(M_PROTOFLAGS);
598fe937674SJosef Karthauser 
599df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
600df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
601fe937674SJosef Karthauser 		} else
602df8bae1dSRodney W. Grimes 			m_freem(m);
603df8bae1dSRodney W. Grimes 	}
604df8bae1dSRodney W. Grimes 
605df8bae1dSRodney W. Grimes 	if (error == 0)
606df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
6071e78ac21SJeffrey Hsu 
608df8bae1dSRodney W. Grimes done:
6096a800098SYoshinobu Inoue 	if (ro == &iproute && ro->ro_rt) {
6106a800098SYoshinobu Inoue 		RTFREE(ro->ro_rt);
611ac9d7e26SMax Laier 	}
612df8bae1dSRodney W. Grimes 	return (error);
613df8bae1dSRodney W. Grimes bad:
6143528d68fSBill Paul 	m_freem(m);
615df8bae1dSRodney W. Grimes 	goto done;
616df8bae1dSRodney W. Grimes }
617df8bae1dSRodney W. Grimes 
6181e78ac21SJeffrey Hsu /*
6191e78ac21SJeffrey Hsu  * Create a chain of fragments which fit the given mtu. m_frag points to the
6201e78ac21SJeffrey Hsu  * mbuf to be fragmented; on return it points to the chain with the fragments.
6211e78ac21SJeffrey Hsu  * Return 0 if no error. If error, m_frag may contain a partially built
6221e78ac21SJeffrey Hsu  * chain of fragments that should be freed by the caller.
6231e78ac21SJeffrey Hsu  *
6241e78ac21SJeffrey Hsu  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
6251e78ac21SJeffrey Hsu  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
6261e78ac21SJeffrey Hsu  */
6271e78ac21SJeffrey Hsu int
6281e78ac21SJeffrey Hsu ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
6291e78ac21SJeffrey Hsu     u_long if_hwassist_flags, int sw_csum)
6301e78ac21SJeffrey Hsu {
6311e78ac21SJeffrey Hsu 	int error = 0;
6321e78ac21SJeffrey Hsu 	int hlen = ip->ip_hl << 2;
6331e78ac21SJeffrey Hsu 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
6341e78ac21SJeffrey Hsu 	int off;
6351e78ac21SJeffrey Hsu 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
6361e78ac21SJeffrey Hsu 	int firstlen;
6371e78ac21SJeffrey Hsu 	struct mbuf **mnext;
6381e78ac21SJeffrey Hsu 	int nfrags;
6391e78ac21SJeffrey Hsu 
6401e78ac21SJeffrey Hsu 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
6411e78ac21SJeffrey Hsu 		ipstat.ips_cantfrag++;
6421e78ac21SJeffrey Hsu 		return EMSGSIZE;
6431e78ac21SJeffrey Hsu 	}
6441e78ac21SJeffrey Hsu 
6451e78ac21SJeffrey Hsu 	/*
6461e78ac21SJeffrey Hsu 	 * Must be able to put at least 8 bytes per fragment.
6471e78ac21SJeffrey Hsu 	 */
6481e78ac21SJeffrey Hsu 	if (len < 8)
6491e78ac21SJeffrey Hsu 		return EMSGSIZE;
6501e78ac21SJeffrey Hsu 
6511e78ac21SJeffrey Hsu 	/*
6521e78ac21SJeffrey Hsu 	 * If the interface will not calculate checksums on
6531e78ac21SJeffrey Hsu 	 * fragmented packets, then do it here.
6541e78ac21SJeffrey Hsu 	 */
6551e78ac21SJeffrey Hsu 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
6561e78ac21SJeffrey Hsu 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
6571e78ac21SJeffrey Hsu 		in_delayed_cksum(m0);
6581e78ac21SJeffrey Hsu 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
6591e78ac21SJeffrey Hsu 	}
6601e78ac21SJeffrey Hsu 
6611e78ac21SJeffrey Hsu 	if (len > PAGE_SIZE) {
6621e78ac21SJeffrey Hsu 		/*
6631e78ac21SJeffrey Hsu 		 * Fragment large datagrams such that each segment
6641e78ac21SJeffrey Hsu 		 * contains a multiple of PAGE_SIZE amount of data,
6651e78ac21SJeffrey Hsu 		 * plus headers. This enables a receiver to perform
6661e78ac21SJeffrey Hsu 		 * page-flipping zero-copy optimizations.
6671e78ac21SJeffrey Hsu 		 *
6681e78ac21SJeffrey Hsu 		 * XXX When does this help given that sender and receiver
6691e78ac21SJeffrey Hsu 		 * could have different page sizes, and also mtu could
6701e78ac21SJeffrey Hsu 		 * be less than the receiver's page size ?
6711e78ac21SJeffrey Hsu 		 */
6721e78ac21SJeffrey Hsu 		int newlen;
6731e78ac21SJeffrey Hsu 		struct mbuf *m;
6741e78ac21SJeffrey Hsu 
6751e78ac21SJeffrey Hsu 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
6761e78ac21SJeffrey Hsu 			off += m->m_len;
6771e78ac21SJeffrey Hsu 
6781e78ac21SJeffrey Hsu 		/*
6791e78ac21SJeffrey Hsu 		 * firstlen (off - hlen) must be aligned on an
6801e78ac21SJeffrey Hsu 		 * 8-byte boundary
6811e78ac21SJeffrey Hsu 		 */
6821e78ac21SJeffrey Hsu 		if (off < hlen)
6831e78ac21SJeffrey Hsu 			goto smart_frag_failure;
6841e78ac21SJeffrey Hsu 		off = ((off - hlen) & ~7) + hlen;
6851e78ac21SJeffrey Hsu 		newlen = (~PAGE_MASK) & mtu;
6861e78ac21SJeffrey Hsu 		if ((newlen + sizeof (struct ip)) > mtu) {
6871e78ac21SJeffrey Hsu 			/* we failed, go back the default */
6881e78ac21SJeffrey Hsu smart_frag_failure:
6891e78ac21SJeffrey Hsu 			newlen = len;
6901e78ac21SJeffrey Hsu 			off = hlen + len;
6911e78ac21SJeffrey Hsu 		}
6921e78ac21SJeffrey Hsu 		len = newlen;
6931e78ac21SJeffrey Hsu 
6941e78ac21SJeffrey Hsu 	} else {
6951e78ac21SJeffrey Hsu 		off = hlen + len;
6961e78ac21SJeffrey Hsu 	}
6971e78ac21SJeffrey Hsu 
6981e78ac21SJeffrey Hsu 	firstlen = off - hlen;
6991e78ac21SJeffrey Hsu 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
7001e78ac21SJeffrey Hsu 
7011e78ac21SJeffrey Hsu 	/*
7021e78ac21SJeffrey Hsu 	 * Loop through length of segment after first fragment,
7031e78ac21SJeffrey Hsu 	 * make new header and copy data of each part and link onto chain.
7041e78ac21SJeffrey Hsu 	 * Here, m0 is the original packet, m is the fragment being created.
7051e78ac21SJeffrey Hsu 	 * The fragments are linked off the m_nextpkt of the original
7061e78ac21SJeffrey Hsu 	 * packet, which after processing serves as the first fragment.
7071e78ac21SJeffrey Hsu 	 */
7081e78ac21SJeffrey Hsu 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
7091e78ac21SJeffrey Hsu 		struct ip *mhip;	/* ip header on the fragment */
7101e78ac21SJeffrey Hsu 		struct mbuf *m;
7111e78ac21SJeffrey Hsu 		int mhlen = sizeof (struct ip);
7121e78ac21SJeffrey Hsu 
71334333b16SAndre Oppermann 		MGETHDR(m, M_DONTWAIT, MT_DATA);
7140b17fba7SAndre Oppermann 		if (m == NULL) {
7151e78ac21SJeffrey Hsu 			error = ENOBUFS;
7161e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
7171e78ac21SJeffrey Hsu 			goto done;
7181e78ac21SJeffrey Hsu 		}
7191e78ac21SJeffrey Hsu 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
7201e78ac21SJeffrey Hsu 		/*
7211e78ac21SJeffrey Hsu 		 * In the first mbuf, leave room for the link header, then
7221e78ac21SJeffrey Hsu 		 * copy the original IP header including options. The payload
7231e78ac21SJeffrey Hsu 		 * goes into an additional mbuf chain returned by m_copy().
7241e78ac21SJeffrey Hsu 		 */
7251e78ac21SJeffrey Hsu 		m->m_data += max_linkhdr;
7261e78ac21SJeffrey Hsu 		mhip = mtod(m, struct ip *);
7271e78ac21SJeffrey Hsu 		*mhip = *ip;
7281e78ac21SJeffrey Hsu 		if (hlen > sizeof (struct ip)) {
7291e78ac21SJeffrey Hsu 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
7301e78ac21SJeffrey Hsu 			mhip->ip_v = IPVERSION;
7311e78ac21SJeffrey Hsu 			mhip->ip_hl = mhlen >> 2;
7321e78ac21SJeffrey Hsu 		}
7331e78ac21SJeffrey Hsu 		m->m_len = mhlen;
7341e78ac21SJeffrey Hsu 		/* XXX do we need to add ip->ip_off below ? */
7351e78ac21SJeffrey Hsu 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
7361e78ac21SJeffrey Hsu 		if (off + len >= ip->ip_len) {	/* last fragment */
7371e78ac21SJeffrey Hsu 			len = ip->ip_len - off;
7381e78ac21SJeffrey Hsu 			m->m_flags |= M_LASTFRAG;
7391e78ac21SJeffrey Hsu 		} else
7401e78ac21SJeffrey Hsu 			mhip->ip_off |= IP_MF;
7411e78ac21SJeffrey Hsu 		mhip->ip_len = htons((u_short)(len + mhlen));
7421e78ac21SJeffrey Hsu 		m->m_next = m_copy(m0, off, len);
7430b17fba7SAndre Oppermann 		if (m->m_next == NULL) {	/* copy failed */
7441e78ac21SJeffrey Hsu 			m_free(m);
7451e78ac21SJeffrey Hsu 			error = ENOBUFS;	/* ??? */
7461e78ac21SJeffrey Hsu 			ipstat.ips_odropped++;
7471e78ac21SJeffrey Hsu 			goto done;
7481e78ac21SJeffrey Hsu 		}
7491e78ac21SJeffrey Hsu 		m->m_pkthdr.len = mhlen + len;
750fc74a9f9SBrooks Davis 		m->m_pkthdr.rcvif = NULL;
7511e78ac21SJeffrey Hsu #ifdef MAC
7521e78ac21SJeffrey Hsu 		mac_create_fragment(m0, m);
7531e78ac21SJeffrey Hsu #endif
7541e78ac21SJeffrey Hsu 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
7551e78ac21SJeffrey Hsu 		mhip->ip_off = htons(mhip->ip_off);
7561e78ac21SJeffrey Hsu 		mhip->ip_sum = 0;
7571e78ac21SJeffrey Hsu 		if (sw_csum & CSUM_DELAY_IP)
7581e78ac21SJeffrey Hsu 			mhip->ip_sum = in_cksum(m, mhlen);
7591e78ac21SJeffrey Hsu 		*mnext = m;
7601e78ac21SJeffrey Hsu 		mnext = &m->m_nextpkt;
7611e78ac21SJeffrey Hsu 	}
7621e78ac21SJeffrey Hsu 	ipstat.ips_ofragments += nfrags;
7631e78ac21SJeffrey Hsu 
7641e78ac21SJeffrey Hsu 	/* set first marker for fragment chain */
7651e78ac21SJeffrey Hsu 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
7661e78ac21SJeffrey Hsu 	m0->m_pkthdr.csum_data = nfrags;
7671e78ac21SJeffrey Hsu 
7681e78ac21SJeffrey Hsu 	/*
7691e78ac21SJeffrey Hsu 	 * Update first fragment by trimming what's been copied out
7701e78ac21SJeffrey Hsu 	 * and updating header.
7711e78ac21SJeffrey Hsu 	 */
7721e78ac21SJeffrey Hsu 	m_adj(m0, hlen + firstlen - ip->ip_len);
7731e78ac21SJeffrey Hsu 	m0->m_pkthdr.len = hlen + firstlen;
7741e78ac21SJeffrey Hsu 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
7751e78ac21SJeffrey Hsu 	ip->ip_off |= IP_MF;
7761e78ac21SJeffrey Hsu 	ip->ip_off = htons(ip->ip_off);
7771e78ac21SJeffrey Hsu 	ip->ip_sum = 0;
7781e78ac21SJeffrey Hsu 	if (sw_csum & CSUM_DELAY_IP)
7791e78ac21SJeffrey Hsu 		ip->ip_sum = in_cksum(m0, hlen);
7801e78ac21SJeffrey Hsu 
7811e78ac21SJeffrey Hsu done:
7821e78ac21SJeffrey Hsu 	*m_frag = m0;
7831e78ac21SJeffrey Hsu 	return error;
7841e78ac21SJeffrey Hsu }
7851e78ac21SJeffrey Hsu 
7861c238475SJonathan Lemon void
787db4f9cc7SJonathan Lemon in_delayed_cksum(struct mbuf *m)
788db4f9cc7SJonathan Lemon {
789db4f9cc7SJonathan Lemon 	struct ip *ip;
790db4f9cc7SJonathan Lemon 	u_short csum, offset;
791db4f9cc7SJonathan Lemon 
792db4f9cc7SJonathan Lemon 	ip = mtod(m, struct ip *);
79353be11f6SPoul-Henning Kamp 	offset = ip->ip_hl << 2 ;
794db4f9cc7SJonathan Lemon 	csum = in_cksum_skip(m, ip->ip_len, offset);
795206a3274SRuslan Ermilov 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
796206a3274SRuslan Ermilov 		csum = 0xffff;
797db4f9cc7SJonathan Lemon 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
798db4f9cc7SJonathan Lemon 
799db4f9cc7SJonathan Lemon 	if (offset + sizeof(u_short) > m->m_len) {
800db4f9cc7SJonathan Lemon 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
801db4f9cc7SJonathan Lemon 		    m->m_len, offset, ip->ip_p);
802db4f9cc7SJonathan Lemon 		/*
803db4f9cc7SJonathan Lemon 		 * XXX
804db4f9cc7SJonathan Lemon 		 * this shouldn't happen, but if it does, the
805db4f9cc7SJonathan Lemon 		 * correct behavior may be to insert the checksum
8068f8d29f6SAndre Oppermann 		 * in the appropriate next mbuf in the chain.
807db4f9cc7SJonathan Lemon 		 */
8088f8d29f6SAndre Oppermann 		return;
809db4f9cc7SJonathan Lemon 	}
810db4f9cc7SJonathan Lemon 	*(u_short *)(m->m_data + offset) = csum;
811db4f9cc7SJonathan Lemon }
812db4f9cc7SJonathan Lemon 
813df8bae1dSRodney W. Grimes /*
814df8bae1dSRodney W. Grimes  * IP socket option processing.
815df8bae1dSRodney W. Grimes  */
816df8bae1dSRodney W. Grimes int
817f2565d68SRobert Watson ip_ctloutput(struct socket *so, struct sockopt *sopt)
818df8bae1dSRodney W. Grimes {
819cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
820cfe8b629SGarrett Wollman 	int	error, optval;
821df8bae1dSRodney W. Grimes 
822cfe8b629SGarrett Wollman 	error = optval = 0;
823cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP) {
824cfe8b629SGarrett Wollman 		return (EINVAL);
825cfe8b629SGarrett Wollman 	}
826df8bae1dSRodney W. Grimes 
827cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
828cfe8b629SGarrett Wollman 	case SOPT_SET:
829cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
830df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
831df8bae1dSRodney W. Grimes #ifdef notyet
832df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
833df8bae1dSRodney W. Grimes #endif
834cfe8b629SGarrett Wollman 		{
835cfe8b629SGarrett Wollman 			struct mbuf *m;
836cfe8b629SGarrett Wollman 			if (sopt->sopt_valsize > MLEN) {
837cfe8b629SGarrett Wollman 				error = EMSGSIZE;
838cfe8b629SGarrett Wollman 				break;
839cfe8b629SGarrett Wollman 			}
840e0aec682SAndre Oppermann 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
8410b17fba7SAndre Oppermann 			if (m == NULL) {
842cfe8b629SGarrett Wollman 				error = ENOBUFS;
843cfe8b629SGarrett Wollman 				break;
844cfe8b629SGarrett Wollman 			}
845cfe8b629SGarrett Wollman 			m->m_len = sopt->sopt_valsize;
846cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
847cfe8b629SGarrett Wollman 					    m->m_len);
848635354c4SMaxim Konovalov 			if (error) {
849635354c4SMaxim Konovalov 				m_free(m);
850635354c4SMaxim Konovalov 				break;
851635354c4SMaxim Konovalov 			}
852993d9505SRobert Watson 			INP_LOCK(inp);
853993d9505SRobert Watson 			error = ip_pcbopts(inp, sopt->sopt_name, m);
854993d9505SRobert Watson 			INP_UNLOCK(inp);
855993d9505SRobert Watson 			return (error);
856cfe8b629SGarrett Wollman 		}
857df8bae1dSRodney W. Grimes 
858df8bae1dSRodney W. Grimes 		case IP_TOS:
859df8bae1dSRodney W. Grimes 		case IP_TTL:
860936cd18dSAndre Oppermann 		case IP_MINTTL:
861df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
862df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
863df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
8644957466bSMatthew N. Dodd 		case IP_RECVTTL:
86582c23ebaSBill Fenner 		case IP_RECVIF:
8666a800098SYoshinobu Inoue 		case IP_FAITH:
8678afa2304SBruce M Simpson 		case IP_ONESBCAST:
868b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
869cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
870cfe8b629SGarrett Wollman 					    sizeof optval);
871cfe8b629SGarrett Wollman 			if (error)
872cfe8b629SGarrett Wollman 				break;
873df8bae1dSRodney W. Grimes 
874cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
875df8bae1dSRodney W. Grimes 			case IP_TOS:
876ca98b82cSDavid Greenman 				inp->inp_ip_tos = optval;
877df8bae1dSRodney W. Grimes 				break;
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes 			case IP_TTL:
880ca98b82cSDavid Greenman 				inp->inp_ip_ttl = optval;
881df8bae1dSRodney W. Grimes 				break;
882936cd18dSAndre Oppermann 
883936cd18dSAndre Oppermann 			case IP_MINTTL:
884936cd18dSAndre Oppermann 				if (optval > 0 && optval <= MAXTTL)
885936cd18dSAndre Oppermann 					inp->inp_ip_minttl = optval;
886936cd18dSAndre Oppermann 				else
887936cd18dSAndre Oppermann 					error = EINVAL;
888936cd18dSAndre Oppermann 				break;
889936cd18dSAndre Oppermann 
890a138d217SRobert Watson #define	OPTSET(bit) do {						\
891a138d217SRobert Watson 	INP_LOCK(inp);							\
892df8bae1dSRodney W. Grimes 	if (optval)							\
893df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit;					\
894df8bae1dSRodney W. Grimes 	else								\
895a138d217SRobert Watson 		inp->inp_flags &= ~bit;					\
896a138d217SRobert Watson 	INP_UNLOCK(inp);						\
897a138d217SRobert Watson } while (0)
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
900df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVOPTS);
901df8bae1dSRodney W. Grimes 				break;
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
904df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVRETOPTS);
905df8bae1dSRodney W. Grimes 				break;
906df8bae1dSRodney W. Grimes 
907df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
908df8bae1dSRodney W. Grimes 				OPTSET(INP_RECVDSTADDR);
909df8bae1dSRodney W. Grimes 				break;
91082c23ebaSBill Fenner 
9114957466bSMatthew N. Dodd 			case IP_RECVTTL:
9124957466bSMatthew N. Dodd 				OPTSET(INP_RECVTTL);
9134957466bSMatthew N. Dodd 				break;
9144957466bSMatthew N. Dodd 
91582c23ebaSBill Fenner 			case IP_RECVIF:
91682c23ebaSBill Fenner 				OPTSET(INP_RECVIF);
91782c23ebaSBill Fenner 				break;
9186a800098SYoshinobu Inoue 
9196a800098SYoshinobu Inoue 			case IP_FAITH:
9206a800098SYoshinobu Inoue 				OPTSET(INP_FAITH);
9216a800098SYoshinobu Inoue 				break;
9228afa2304SBruce M Simpson 
9238afa2304SBruce M Simpson 			case IP_ONESBCAST:
9248afa2304SBruce M Simpson 				OPTSET(INP_ONESBCAST);
9258afa2304SBruce M Simpson 				break;
926b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
927b2828ad2SAndre Oppermann 				OPTSET(INP_DONTFRAG);
928b2828ad2SAndre Oppermann 				break;
929df8bae1dSRodney W. Grimes 			}
930df8bae1dSRodney W. Grimes 			break;
931df8bae1dSRodney W. Grimes #undef OPTSET
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
934f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
935df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
936df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
937df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
938df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
9395c918b56SRobert Watson 			error = ip_setmoptions(inp, sopt);
940df8bae1dSRodney W. Grimes 			break;
941df8bae1dSRodney W. Grimes 
94233b3ac06SPeter Wemm 		case IP_PORTRANGE:
943cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
944cfe8b629SGarrett Wollman 					    sizeof optval);
945cfe8b629SGarrett Wollman 			if (error)
946cfe8b629SGarrett Wollman 				break;
94733b3ac06SPeter Wemm 
948a138d217SRobert Watson 			INP_LOCK(inp);
94933b3ac06SPeter Wemm 			switch (optval) {
95033b3ac06SPeter Wemm 			case IP_PORTRANGE_DEFAULT:
95133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
95233b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
95333b3ac06SPeter Wemm 				break;
95433b3ac06SPeter Wemm 
95533b3ac06SPeter Wemm 			case IP_PORTRANGE_HIGH:
95633b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_LOWPORT);
95733b3ac06SPeter Wemm 				inp->inp_flags |= INP_HIGHPORT;
95833b3ac06SPeter Wemm 				break;
95933b3ac06SPeter Wemm 
96033b3ac06SPeter Wemm 			case IP_PORTRANGE_LOW:
96133b3ac06SPeter Wemm 				inp->inp_flags &= ~(INP_HIGHPORT);
96233b3ac06SPeter Wemm 				inp->inp_flags |= INP_LOWPORT;
96333b3ac06SPeter Wemm 				break;
96433b3ac06SPeter Wemm 
96533b3ac06SPeter Wemm 			default:
96633b3ac06SPeter Wemm 				error = EINVAL;
96733b3ac06SPeter Wemm 				break;
96833b3ac06SPeter Wemm 			}
969a138d217SRobert Watson 			INP_UNLOCK(inp);
970ce8c72b1SPeter Wemm 			break;
97133b3ac06SPeter Wemm 
972b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
9736a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
9746a800098SYoshinobu Inoue 		{
9756a800098SYoshinobu Inoue 			caddr_t req;
976686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
9776a800098SYoshinobu Inoue 			int priv;
9786a800098SYoshinobu Inoue 			struct mbuf *m;
9796a800098SYoshinobu Inoue 			int optname;
9806a800098SYoshinobu Inoue 
9816a800098SYoshinobu Inoue 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
9826a800098SYoshinobu Inoue 				break;
9836a800098SYoshinobu Inoue 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
9846a800098SYoshinobu Inoue 				break;
985acd3428bSRobert Watson 			if (sopt->sopt_td != NULL) {
986acd3428bSRobert Watson 				/*
987acd3428bSRobert Watson 				 * XXXRW: Would be more desirable to do this
988acd3428bSRobert Watson 				 * one layer down so that we only exercise
989acd3428bSRobert Watson 				 * privilege if it is needed.
990acd3428bSRobert Watson 				 */
991acd3428bSRobert Watson 				error = priv_check(sopt->sopt_td,
992acd3428bSRobert Watson 				    PRIV_NETINET_IPSEC);
993acd3428bSRobert Watson 				if (error)
994acd3428bSRobert Watson 					priv = 0;
995acd3428bSRobert Watson 				else
996acd3428bSRobert Watson 					priv = 1;
997acd3428bSRobert Watson 			} else
998acd3428bSRobert Watson 				priv = 1;
9996a800098SYoshinobu Inoue 			req = mtod(m, caddr_t);
1000686cdd19SJun-ichiro itojun Hagino 			len = m->m_len;
10016a800098SYoshinobu Inoue 			optname = sopt->sopt_name;
1002686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_set_policy(inp, optname, req, len, priv);
10036a800098SYoshinobu Inoue 			m_freem(m);
10046a800098SYoshinobu Inoue 			break;
10056a800098SYoshinobu Inoue 		}
10066a800098SYoshinobu Inoue #endif /*IPSEC*/
10076a800098SYoshinobu Inoue 
1008df8bae1dSRodney W. Grimes 		default:
1009df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1010df8bae1dSRodney W. Grimes 			break;
1011df8bae1dSRodney W. Grimes 		}
1012df8bae1dSRodney W. Grimes 		break;
1013df8bae1dSRodney W. Grimes 
1014cfe8b629SGarrett Wollman 	case SOPT_GET:
1015cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
1016df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
1017df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
1018cfe8b629SGarrett Wollman 			if (inp->inp_options)
1019cfe8b629SGarrett Wollman 				error = sooptcopyout(sopt,
1020cfe8b629SGarrett Wollman 						     mtod(inp->inp_options,
1021cfe8b629SGarrett Wollman 							  char *),
1022cfe8b629SGarrett Wollman 						     inp->inp_options->m_len);
1023cfe8b629SGarrett Wollman 			else
1024cfe8b629SGarrett Wollman 				sopt->sopt_valsize = 0;
1025df8bae1dSRodney W. Grimes 			break;
1026df8bae1dSRodney W. Grimes 
1027df8bae1dSRodney W. Grimes 		case IP_TOS:
1028df8bae1dSRodney W. Grimes 		case IP_TTL:
1029936cd18dSAndre Oppermann 		case IP_MINTTL:
1030df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
1031df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
1032df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
10334957466bSMatthew N. Dodd 		case IP_RECVTTL:
103482c23ebaSBill Fenner 		case IP_RECVIF:
1035cfe8b629SGarrett Wollman 		case IP_PORTRANGE:
10366a800098SYoshinobu Inoue 		case IP_FAITH:
10378afa2304SBruce M Simpson 		case IP_ONESBCAST:
1038b2828ad2SAndre Oppermann 		case IP_DONTFRAG:
1039cfe8b629SGarrett Wollman 			switch (sopt->sopt_name) {
1040df8bae1dSRodney W. Grimes 
1041df8bae1dSRodney W. Grimes 			case IP_TOS:
1042ca98b82cSDavid Greenman 				optval = inp->inp_ip_tos;
1043df8bae1dSRodney W. Grimes 				break;
1044df8bae1dSRodney W. Grimes 
1045df8bae1dSRodney W. Grimes 			case IP_TTL:
1046ca98b82cSDavid Greenman 				optval = inp->inp_ip_ttl;
1047df8bae1dSRodney W. Grimes 				break;
1048df8bae1dSRodney W. Grimes 
1049936cd18dSAndre Oppermann 			case IP_MINTTL:
1050936cd18dSAndre Oppermann 				optval = inp->inp_ip_minttl;
1051936cd18dSAndre Oppermann 				break;
1052936cd18dSAndre Oppermann 
1053df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
1056df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
1057df8bae1dSRodney W. Grimes 				break;
1058df8bae1dSRodney W. Grimes 
1059df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
1060df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
1061df8bae1dSRodney W. Grimes 				break;
1062df8bae1dSRodney W. Grimes 
1063df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
1064df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
1065df8bae1dSRodney W. Grimes 				break;
106682c23ebaSBill Fenner 
10674957466bSMatthew N. Dodd 			case IP_RECVTTL:
10684957466bSMatthew N. Dodd 				optval = OPTBIT(INP_RECVTTL);
10694957466bSMatthew N. Dodd 				break;
10704957466bSMatthew N. Dodd 
107182c23ebaSBill Fenner 			case IP_RECVIF:
107282c23ebaSBill Fenner 				optval = OPTBIT(INP_RECVIF);
107382c23ebaSBill Fenner 				break;
1074cfe8b629SGarrett Wollman 
1075cfe8b629SGarrett Wollman 			case IP_PORTRANGE:
1076cfe8b629SGarrett Wollman 				if (inp->inp_flags & INP_HIGHPORT)
1077cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_HIGH;
1078cfe8b629SGarrett Wollman 				else if (inp->inp_flags & INP_LOWPORT)
1079cfe8b629SGarrett Wollman 					optval = IP_PORTRANGE_LOW;
1080cfe8b629SGarrett Wollman 				else
1081cfe8b629SGarrett Wollman 					optval = 0;
1082cfe8b629SGarrett Wollman 				break;
10836a800098SYoshinobu Inoue 
10846a800098SYoshinobu Inoue 			case IP_FAITH:
10856a800098SYoshinobu Inoue 				optval = OPTBIT(INP_FAITH);
10866a800098SYoshinobu Inoue 				break;
10878afa2304SBruce M Simpson 
10888afa2304SBruce M Simpson 			case IP_ONESBCAST:
10898afa2304SBruce M Simpson 				optval = OPTBIT(INP_ONESBCAST);
10908afa2304SBruce M Simpson 				break;
1091b2828ad2SAndre Oppermann 			case IP_DONTFRAG:
1092b2828ad2SAndre Oppermann 				optval = OPTBIT(INP_DONTFRAG);
1093b2828ad2SAndre Oppermann 				break;
1094df8bae1dSRodney W. Grimes 			}
1095cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1096df8bae1dSRodney W. Grimes 			break;
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
1099f0068c4aSGarrett Wollman 		case IP_MULTICAST_VIF:
1100df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
1101df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
110289924e58SRobert Watson 			error = ip_getmoptions(inp, sopt);
110333b3ac06SPeter Wemm 			break;
110433b3ac06SPeter Wemm 
1105b9234fafSSam Leffler #if defined(IPSEC) || defined(FAST_IPSEC)
11066a800098SYoshinobu Inoue 		case IP_IPSEC_POLICY:
11076a800098SYoshinobu Inoue 		{
1108f63e7634SYoshinobu Inoue 			struct mbuf *m = NULL;
11096a800098SYoshinobu Inoue 			caddr_t req = NULL;
1110686cdd19SJun-ichiro itojun Hagino 			size_t len = 0;
11116a800098SYoshinobu Inoue 
1112686cdd19SJun-ichiro itojun Hagino 			if (m != 0) {
11136a800098SYoshinobu Inoue 				req = mtod(m, caddr_t);
1114686cdd19SJun-ichiro itojun Hagino 				len = m->m_len;
1115686cdd19SJun-ichiro itojun Hagino 			}
1116686cdd19SJun-ichiro itojun Hagino 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
11176a800098SYoshinobu Inoue 			if (error == 0)
11186a800098SYoshinobu Inoue 				error = soopt_mcopyout(sopt, m); /* XXX */
1119f63e7634SYoshinobu Inoue 			if (error == 0)
11206a800098SYoshinobu Inoue 				m_freem(m);
11216a800098SYoshinobu Inoue 			break;
11226a800098SYoshinobu Inoue 		}
11236a800098SYoshinobu Inoue #endif /*IPSEC*/
11246a800098SYoshinobu Inoue 
1125df8bae1dSRodney W. Grimes 		default:
1126df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
1127df8bae1dSRodney W. Grimes 			break;
1128df8bae1dSRodney W. Grimes 		}
1129df8bae1dSRodney W. Grimes 		break;
1130df8bae1dSRodney W. Grimes 	}
1131df8bae1dSRodney W. Grimes 	return (error);
1132df8bae1dSRodney W. Grimes }
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes /*
1135cfe8b629SGarrett Wollman  * XXX
1136cfe8b629SGarrett Wollman  * The whole multicast option thing needs to be re-thought.
1137cfe8b629SGarrett Wollman  * Several of these options are equally applicable to non-multicast
1138cfe8b629SGarrett Wollman  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1139cfe8b629SGarrett Wollman  * standard option (IP_TTL).
1140cfe8b629SGarrett Wollman  */
114133841545SHajimu UMEMOTO 
114233841545SHajimu UMEMOTO /*
114333841545SHajimu UMEMOTO  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
114433841545SHajimu UMEMOTO  */
114533841545SHajimu UMEMOTO static struct ifnet *
1146f2565d68SRobert Watson ip_multicast_if(struct in_addr *a, int *ifindexp)
114733841545SHajimu UMEMOTO {
114833841545SHajimu UMEMOTO 	int ifindex;
114933841545SHajimu UMEMOTO 	struct ifnet *ifp;
115033841545SHajimu UMEMOTO 
115133841545SHajimu UMEMOTO 	if (ifindexp)
115233841545SHajimu UMEMOTO 		*ifindexp = 0;
115333841545SHajimu UMEMOTO 	if (ntohl(a->s_addr) >> 24 == 0) {
115433841545SHajimu UMEMOTO 		ifindex = ntohl(a->s_addr) & 0xffffff;
115533841545SHajimu UMEMOTO 		if (ifindex < 0 || if_index < ifindex)
115633841545SHajimu UMEMOTO 			return NULL;
1157f9132cebSJonathan Lemon 		ifp = ifnet_byindex(ifindex);
115833841545SHajimu UMEMOTO 		if (ifindexp)
115933841545SHajimu UMEMOTO 			*ifindexp = ifindex;
116033841545SHajimu UMEMOTO 	} else {
116133841545SHajimu UMEMOTO 		INADDR_TO_IFP(*a, ifp);
116233841545SHajimu UMEMOTO 	}
116333841545SHajimu UMEMOTO 	return ifp;
116433841545SHajimu UMEMOTO }
116533841545SHajimu UMEMOTO 
1166cfe8b629SGarrett Wollman /*
1167a2dc1f50SRobert Watson  * Given an inpcb, return its multicast options structure pointer.  Accepts
1168a2dc1f50SRobert Watson  * an unlocked inpcb pointer, but will return it locked.  May sleep.
1169a2dc1f50SRobert Watson  */
1170a2dc1f50SRobert Watson static struct ip_moptions *
1171a2dc1f50SRobert Watson ip_findmoptions(struct inpcb *inp)
1172a2dc1f50SRobert Watson {
1173a2dc1f50SRobert Watson 	struct ip_moptions *imo;
11743548bfc9SBruce M Simpson 	struct in_multi **immp;
1175a2dc1f50SRobert Watson 
1176a2dc1f50SRobert Watson 	INP_LOCK(inp);
1177a2dc1f50SRobert Watson 	if (inp->inp_moptions != NULL)
1178a2dc1f50SRobert Watson 		return (inp->inp_moptions);
1179a2dc1f50SRobert Watson 
1180a2dc1f50SRobert Watson 	INP_UNLOCK(inp);
1181a2dc1f50SRobert Watson 
1182a2dc1f50SRobert Watson 	imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
11833548bfc9SBruce M Simpson 	immp = (struct in_multi **)malloc((sizeof(*immp) * IP_MIN_MEMBERSHIPS),
11843548bfc9SBruce M Simpson 					  M_IPMOPTS, M_WAITOK);
1185a2dc1f50SRobert Watson 
1186a2dc1f50SRobert Watson 	imo->imo_multicast_ifp = NULL;
1187a2dc1f50SRobert Watson 	imo->imo_multicast_addr.s_addr = INADDR_ANY;
1188a2dc1f50SRobert Watson 	imo->imo_multicast_vif = -1;
1189a2dc1f50SRobert Watson 	imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1190a2dc1f50SRobert Watson 	imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1191a2dc1f50SRobert Watson 	imo->imo_num_memberships = 0;
11923548bfc9SBruce M Simpson 	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
11933548bfc9SBruce M Simpson 	imo->imo_membership = immp;
1194a2dc1f50SRobert Watson 
1195a2dc1f50SRobert Watson 	INP_LOCK(inp);
1196a2dc1f50SRobert Watson 	if (inp->inp_moptions != NULL) {
11973548bfc9SBruce M Simpson 		free(immp, M_IPMOPTS);
1198a2dc1f50SRobert Watson 		free(imo, M_IPMOPTS);
1199a2dc1f50SRobert Watson 		return (inp->inp_moptions);
1200a2dc1f50SRobert Watson 	}
1201a2dc1f50SRobert Watson 	inp->inp_moptions = imo;
1202a2dc1f50SRobert Watson 	return (imo);
1203a2dc1f50SRobert Watson }
1204a2dc1f50SRobert Watson 
1205a2dc1f50SRobert Watson /*
1206df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
1207df8bae1dSRodney W. Grimes  */
12080312fbe9SPoul-Henning Kamp static int
12095c918b56SRobert Watson ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1210df8bae1dSRodney W. Grimes {
1211cfe8b629SGarrett Wollman 	int error = 0;
1212cfe8b629SGarrett Wollman 	int i;
1213df8bae1dSRodney W. Grimes 	struct in_addr addr;
1214cfe8b629SGarrett Wollman 	struct ip_mreq mreq;
1215cfe8b629SGarrett Wollman 	struct ifnet *ifp;
12165c918b56SRobert Watson 	struct ip_moptions *imo;
1217df8bae1dSRodney W. Grimes 	struct route ro;
1218cfe8b629SGarrett Wollman 	struct sockaddr_in *dst;
121933841545SHajimu UMEMOTO 	int ifindex;
12209b626c29SGarrett Wollman 	int s;
1221df8bae1dSRodney W. Grimes 
1222cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1223f0068c4aSGarrett Wollman 	/* store an index number for the vif you wanna use in the send */
1224f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1225cfe8b629SGarrett Wollman 		if (legal_vif_num == 0) {
12265e9ae478SGarrett Wollman 			error = EOPNOTSUPP;
12275e9ae478SGarrett Wollman 			break;
12285e9ae478SGarrett Wollman 		}
1229cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1230cfe8b629SGarrett Wollman 		if (error)
1231f0068c4aSGarrett Wollman 			break;
12321c5de19aSGarrett Wollman 		if (!legal_vif_num(i) && (i != -1)) {
1233f0068c4aSGarrett Wollman 			error = EINVAL;
1234f0068c4aSGarrett Wollman 			break;
1235f0068c4aSGarrett Wollman 		}
1236a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1237f0068c4aSGarrett Wollman 		imo->imo_multicast_vif = i;
1238a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
1239f0068c4aSGarrett Wollman 		break;
1240f0068c4aSGarrett Wollman 
1241df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1242df8bae1dSRodney W. Grimes 		/*
1243df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
1244df8bae1dSRodney W. Grimes 		 */
1245cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1246cfe8b629SGarrett Wollman 		if (error)
1247df8bae1dSRodney W. Grimes 			break;
1248df8bae1dSRodney W. Grimes 		/*
1249df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
1250df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
1251df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
1252df8bae1dSRodney W. Grimes 		 */
1253a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1254df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
1255df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
1256a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1257df8bae1dSRodney W. Grimes 			break;
1258df8bae1dSRodney W. Grimes 		}
1259df8bae1dSRodney W. Grimes 		/*
1260df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
1261df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
1262df8bae1dSRodney W. Grimes 		 * it supports multicasting.
1263df8bae1dSRodney W. Grimes 		 */
126420e8807cSGarrett Wollman 		s = splimp();
126533841545SHajimu UMEMOTO 		ifp = ip_multicast_if(&addr, &ifindex);
1266df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1267a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1268fbc6ab00SBill Fenner 			splx(s);
1269df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
1270df8bae1dSRodney W. Grimes 			break;
1271df8bae1dSRodney W. Grimes 		}
1272df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
127333841545SHajimu UMEMOTO 		if (ifindex)
127433841545SHajimu UMEMOTO 			imo->imo_multicast_addr = addr;
127533841545SHajimu UMEMOTO 		else
127633841545SHajimu UMEMOTO 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1277a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
12789b626c29SGarrett Wollman 		splx(s);
1279df8bae1dSRodney W. Grimes 		break;
1280df8bae1dSRodney W. Grimes 
1281df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1282df8bae1dSRodney W. Grimes 		/*
1283df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
1284cfe8b629SGarrett Wollman 		 * The original multicast API required a char argument,
1285cfe8b629SGarrett Wollman 		 * which is inconsistent with the rest of the socket API.
1286cfe8b629SGarrett Wollman 		 * We allow either a char or an int.
1287df8bae1dSRodney W. Grimes 		 */
1288cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1289cfe8b629SGarrett Wollman 			u_char ttl;
1290cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, 1, 1);
1291cfe8b629SGarrett Wollman 			if (error)
1292df8bae1dSRodney W. Grimes 				break;
1293a2dc1f50SRobert Watson 			imo = ip_findmoptions(inp);
1294cfe8b629SGarrett Wollman 			imo->imo_multicast_ttl = ttl;
1295a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1296cfe8b629SGarrett Wollman 		} else {
1297cfe8b629SGarrett Wollman 			u_int ttl;
1298cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1299cfe8b629SGarrett Wollman 					    sizeof ttl);
1300cfe8b629SGarrett Wollman 			if (error)
1301cfe8b629SGarrett Wollman 				break;
1302cfe8b629SGarrett Wollman 			if (ttl > 255)
1303cfe8b629SGarrett Wollman 				error = EINVAL;
1304a2dc1f50SRobert Watson 			else {
1305a2dc1f50SRobert Watson 				imo = ip_findmoptions(inp);
1306cfe8b629SGarrett Wollman 				imo->imo_multicast_ttl = ttl;
1307a2dc1f50SRobert Watson 				INP_UNLOCK(inp);
1308a2dc1f50SRobert Watson 			}
1309df8bae1dSRodney W. Grimes 		}
1310df8bae1dSRodney W. Grimes 		break;
1311df8bae1dSRodney W. Grimes 
1312df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1313df8bae1dSRodney W. Grimes 		/*
1314df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
1315cfe8b629SGarrett Wollman 		 * Must be zero or one.  The original multicast API required a
1316cfe8b629SGarrett Wollman 		 * char argument, which is inconsistent with the rest
1317cfe8b629SGarrett Wollman 		 * of the socket API.  We allow either a char or an int.
1318df8bae1dSRodney W. Grimes 		 */
1319cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1) {
1320cfe8b629SGarrett Wollman 			u_char loop;
1321cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, 1, 1);
1322cfe8b629SGarrett Wollman 			if (error)
1323df8bae1dSRodney W. Grimes 				break;
1324a2dc1f50SRobert Watson 			imo = ip_findmoptions(inp);
1325cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1326a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1327cfe8b629SGarrett Wollman 		} else {
1328cfe8b629SGarrett Wollman 			u_int loop;
1329cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &loop, sizeof loop,
1330cfe8b629SGarrett Wollman 					    sizeof loop);
1331cfe8b629SGarrett Wollman 			if (error)
1332cfe8b629SGarrett Wollman 				break;
1333a2dc1f50SRobert Watson 			imo = ip_findmoptions(inp);
1334cfe8b629SGarrett Wollman 			imo->imo_multicast_loop = !!loop;
1335a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1336df8bae1dSRodney W. Grimes 		}
1337df8bae1dSRodney W. Grimes 		break;
1338df8bae1dSRodney W. Grimes 
1339df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
1340df8bae1dSRodney W. Grimes 		/*
1341df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
1342df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1343df8bae1dSRodney W. Grimes 		 */
1344cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1345cfe8b629SGarrett Wollman 		if (error)
1346df8bae1dSRodney W. Grimes 			break;
1347cfe8b629SGarrett Wollman 
1348cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1349df8bae1dSRodney W. Grimes 			error = EINVAL;
1350df8bae1dSRodney W. Grimes 			break;
1351df8bae1dSRodney W. Grimes 		}
135220e8807cSGarrett Wollman 		s = splimp();
1353df8bae1dSRodney W. Grimes 		/*
1354df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
1355df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
1356df8bae1dSRodney W. Grimes 		 */
1357cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
13581c5de19aSGarrett Wollman 			bzero((caddr_t)&ro, sizeof(ro));
1359df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
1360df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
1361df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
1362cfe8b629SGarrett Wollman 			dst->sin_addr = mreq.imr_multiaddr;
136397d8d152SAndre Oppermann 			rtalloc_ign(&ro, RTF_CLONING);
1364df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
1365df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
13669b626c29SGarrett Wollman 				splx(s);
1367df8bae1dSRodney W. Grimes 				break;
1368df8bae1dSRodney W. Grimes 			}
1369df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
1370d1dd20beSSam Leffler 			RTFREE(ro.ro_rt);
1371df8bae1dSRodney W. Grimes 		}
1372df8bae1dSRodney W. Grimes 		else {
137333841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1374df8bae1dSRodney W. Grimes 		}
13759b626c29SGarrett Wollman 
1376df8bae1dSRodney W. Grimes 		/*
1377df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
1378df8bae1dSRodney W. Grimes 		 * supports multicast.
1379df8bae1dSRodney W. Grimes 		 */
1380df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1381df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
13829b626c29SGarrett Wollman 			splx(s);
1383df8bae1dSRodney W. Grimes 			break;
1384df8bae1dSRodney W. Grimes 		}
1385df8bae1dSRodney W. Grimes 		/*
1386df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
1387df8bae1dSRodney W. Grimes 		 * membership slots are full.
1388df8bae1dSRodney W. Grimes 		 */
1389a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1390df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1391df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1392df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
1393cfe8b629SGarrett Wollman 						== mreq.imr_multiaddr.s_addr)
1394df8bae1dSRodney W. Grimes 				break;
1395df8bae1dSRodney W. Grimes 		}
1396df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
1397a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1398df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
13999b626c29SGarrett Wollman 			splx(s);
1400df8bae1dSRodney W. Grimes 			break;
1401df8bae1dSRodney W. Grimes 		}
14023548bfc9SBruce M Simpson 		if (imo->imo_num_memberships == imo->imo_max_memberships) {
14033548bfc9SBruce M Simpson 		    struct in_multi **nmships, **omships;
14043548bfc9SBruce M Simpson 		    size_t newmax;
14053548bfc9SBruce M Simpson 		    /*
14063548bfc9SBruce M Simpson 		     * Resize the vector to next power-of-two minus 1. If the
14073548bfc9SBruce M Simpson 		     * size would exceed the maximum then we know we've really
14083548bfc9SBruce M Simpson 		     * run out of entries. Otherwise, we realloc() the vector
14093548bfc9SBruce M Simpson 		     * with the INP lock held to avoid introducing a race.
14103548bfc9SBruce M Simpson 		     */
14113548bfc9SBruce M Simpson 		    nmships = NULL;
14123548bfc9SBruce M Simpson 		    omships = imo->imo_membership;
14133548bfc9SBruce M Simpson 		    newmax = ((imo->imo_max_memberships + 1) * 2) - 1;
14143548bfc9SBruce M Simpson 		    if (newmax <= IP_MAX_MEMBERSHIPS) {
14153548bfc9SBruce M Simpson 			nmships = (struct in_multi **)realloc(omships,
14163548bfc9SBruce M Simpson sizeof(*nmships) * newmax, M_IPMOPTS, M_NOWAIT);
14173548bfc9SBruce M Simpson 			if (nmships != NULL) {
14183548bfc9SBruce M Simpson 			    imo->imo_membership = nmships;
14193548bfc9SBruce M Simpson 			    imo->imo_max_memberships = newmax;
14203548bfc9SBruce M Simpson 			}
14213548bfc9SBruce M Simpson 		    }
14223548bfc9SBruce M Simpson 		    if (nmships == NULL) {
1423a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1424df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
14259b626c29SGarrett Wollman 			splx(s);
1426df8bae1dSRodney W. Grimes 			break;
1427df8bae1dSRodney W. Grimes 		    }
14283548bfc9SBruce M Simpson 		}
1429df8bae1dSRodney W. Grimes 		/*
1430df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
1431df8bae1dSRodney W. Grimes 		 * address list for the given interface.
1432df8bae1dSRodney W. Grimes 		 */
1433df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
1434cfe8b629SGarrett Wollman 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1435a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1436df8bae1dSRodney W. Grimes 			error = ENOBUFS;
14379b626c29SGarrett Wollman 			splx(s);
1438df8bae1dSRodney W. Grimes 			break;
1439df8bae1dSRodney W. Grimes 		}
1440df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
1441a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
14429b626c29SGarrett Wollman 		splx(s);
1443df8bae1dSRodney W. Grimes 		break;
1444df8bae1dSRodney W. Grimes 
1445df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
1446df8bae1dSRodney W. Grimes 		/*
1447df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
1448df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
1449df8bae1dSRodney W. Grimes 		 */
1450cfe8b629SGarrett Wollman 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1451cfe8b629SGarrett Wollman 		if (error)
1452df8bae1dSRodney W. Grimes 			break;
1453cfe8b629SGarrett Wollman 
1454cfe8b629SGarrett Wollman 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1455df8bae1dSRodney W. Grimes 			error = EINVAL;
1456df8bae1dSRodney W. Grimes 			break;
1457df8bae1dSRodney W. Grimes 		}
14589b626c29SGarrett Wollman 
145920e8807cSGarrett Wollman 		s = splimp();
1460df8bae1dSRodney W. Grimes 		/*
1461df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
1462df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
1463df8bae1dSRodney W. Grimes 		 */
1464cfe8b629SGarrett Wollman 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1465df8bae1dSRodney W. Grimes 			ifp = NULL;
1466df8bae1dSRodney W. Grimes 		else {
146733841545SHajimu UMEMOTO 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1468df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
1469df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
14709b626c29SGarrett Wollman 				splx(s);
1471df8bae1dSRodney W. Grimes 				break;
1472df8bae1dSRodney W. Grimes 			}
1473df8bae1dSRodney W. Grimes 		}
1474df8bae1dSRodney W. Grimes 		/*
1475df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
1476df8bae1dSRodney W. Grimes 		 */
1477a2dc1f50SRobert Watson 		imo = ip_findmoptions(inp);
1478df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1479df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
1480df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1481df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
1482cfe8b629SGarrett Wollman 			     mreq.imr_multiaddr.s_addr)
1483df8bae1dSRodney W. Grimes 				break;
1484df8bae1dSRodney W. Grimes 		}
1485df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
1486a2dc1f50SRobert Watson 			INP_UNLOCK(inp);
1487df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
14889b626c29SGarrett Wollman 			splx(s);
1489df8bae1dSRodney W. Grimes 			break;
1490df8bae1dSRodney W. Grimes 		}
1491df8bae1dSRodney W. Grimes 		/*
1492df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
1493df8bae1dSRodney W. Grimes 		 * membership points.
1494df8bae1dSRodney W. Grimes 		 */
1495df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
1496df8bae1dSRodney W. Grimes 		/*
1497df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
1498df8bae1dSRodney W. Grimes 		 */
1499df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
1500df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
1501df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
1502a2dc1f50SRobert Watson 		INP_UNLOCK(inp);
15039b626c29SGarrett Wollman 		splx(s);
1504df8bae1dSRodney W. Grimes 		break;
1505df8bae1dSRodney W. Grimes 
1506df8bae1dSRodney W. Grimes 	default:
1507df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
1508df8bae1dSRodney W. Grimes 		break;
1509df8bae1dSRodney W. Grimes 	}
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes 	return (error);
1512df8bae1dSRodney W. Grimes }
1513df8bae1dSRodney W. Grimes 
1514df8bae1dSRodney W. Grimes /*
1515df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
1516df8bae1dSRodney W. Grimes  */
15170312fbe9SPoul-Henning Kamp static int
151889924e58SRobert Watson ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1519df8bae1dSRodney W. Grimes {
152089924e58SRobert Watson 	struct ip_moptions *imo;
1521cfe8b629SGarrett Wollman 	struct in_addr addr;
1522df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
1523cfe8b629SGarrett Wollman 	int error, optval;
1524cfe8b629SGarrett Wollman 	u_char coptval;
1525df8bae1dSRodney W. Grimes 
152689924e58SRobert Watson 	INP_LOCK(inp);
152789924e58SRobert Watson 	imo = inp->inp_moptions;
152889924e58SRobert Watson 
1529cfe8b629SGarrett Wollman 	error = 0;
1530cfe8b629SGarrett Wollman 	switch (sopt->sopt_name) {
1531f0068c4aSGarrett Wollman 	case IP_MULTICAST_VIF:
1532f0068c4aSGarrett Wollman 		if (imo != NULL)
1533cfe8b629SGarrett Wollman 			optval = imo->imo_multicast_vif;
1534f0068c4aSGarrett Wollman 		else
1535cfe8b629SGarrett Wollman 			optval = -1;
153689924e58SRobert Watson 		INP_UNLOCK(inp);
1537cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &optval, sizeof optval);
1538cfe8b629SGarrett Wollman 		break;
1539f0068c4aSGarrett Wollman 
1540df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
1541df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1542cfe8b629SGarrett Wollman 			addr.s_addr = INADDR_ANY;
154333841545SHajimu UMEMOTO 		else if (imo->imo_multicast_addr.s_addr) {
154433841545SHajimu UMEMOTO 			/* return the value user has set */
154533841545SHajimu UMEMOTO 			addr = imo->imo_multicast_addr;
154633841545SHajimu UMEMOTO 		} else {
1547df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1548cfe8b629SGarrett Wollman 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1549df8bae1dSRodney W. Grimes 				: IA_SIN(ia)->sin_addr.s_addr;
1550df8bae1dSRodney W. Grimes 		}
155189924e58SRobert Watson 		INP_UNLOCK(inp);
1552cfe8b629SGarrett Wollman 		error = sooptcopyout(sopt, &addr, sizeof addr);
1553cfe8b629SGarrett Wollman 		break;
1554df8bae1dSRodney W. Grimes 
1555df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1556cfe8b629SGarrett Wollman 		if (imo == 0)
1557cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1558cfe8b629SGarrett Wollman 		else
1559cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_ttl;
156089924e58SRobert Watson 		INP_UNLOCK(inp);
1561cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1562cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1563cfe8b629SGarrett Wollman 		else
1564cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1565cfe8b629SGarrett Wollman 		break;
1566df8bae1dSRodney W. Grimes 
1567df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1568cfe8b629SGarrett Wollman 		if (imo == 0)
1569cfe8b629SGarrett Wollman 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1570cfe8b629SGarrett Wollman 		else
1571cfe8b629SGarrett Wollman 			optval = coptval = imo->imo_multicast_loop;
157289924e58SRobert Watson 		INP_UNLOCK(inp);
1573cfe8b629SGarrett Wollman 		if (sopt->sopt_valsize == 1)
1574cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &coptval, 1);
1575cfe8b629SGarrett Wollman 		else
1576cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
1577cfe8b629SGarrett Wollman 		break;
1578df8bae1dSRodney W. Grimes 
1579df8bae1dSRodney W. Grimes 	default:
158089924e58SRobert Watson 		INP_UNLOCK(inp);
1581cfe8b629SGarrett Wollman 		error = ENOPROTOOPT;
1582cfe8b629SGarrett Wollman 		break;
1583df8bae1dSRodney W. Grimes 	}
158489924e58SRobert Watson 	INP_UNLOCK_ASSERT(inp);
158589924e58SRobert Watson 
1586cfe8b629SGarrett Wollman 	return (error);
1587df8bae1dSRodney W. Grimes }
1588df8bae1dSRodney W. Grimes 
1589df8bae1dSRodney W. Grimes /*
1590df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1591df8bae1dSRodney W. Grimes  */
1592df8bae1dSRodney W. Grimes void
1593f2565d68SRobert Watson ip_freemoptions(struct ip_moptions *imo)
1594df8bae1dSRodney W. Grimes {
1595b375c9ecSLuigi Rizzo 	register int i;
1596df8bae1dSRodney W. Grimes 
1597df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1598df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1599df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
16003548bfc9SBruce M Simpson 		free(imo->imo_membership, M_IPMOPTS);
1601df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1602df8bae1dSRodney W. Grimes 	}
1603df8bae1dSRodney W. Grimes }
1604df8bae1dSRodney W. Grimes 
1605df8bae1dSRodney W. Grimes /*
1606df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1607df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1608df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1609f5fea3ddSPaul Traina  * pointer that might NOT be a loopback interface -- evil, but easier than
1610f5fea3ddSPaul Traina  * replicating that code here.
1611df8bae1dSRodney W. Grimes  */
1612df8bae1dSRodney W. Grimes static void
1613f2565d68SRobert Watson ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1614f2565d68SRobert Watson     int hlen)
1615df8bae1dSRodney W. Grimes {
1616b375c9ecSLuigi Rizzo 	register struct ip *ip;
1617df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1618df8bae1dSRodney W. Grimes 
1619b375c9ecSLuigi Rizzo 	copym = m_copy(m, 0, M_COPYALL);
162086b1d6d2SBill Fenner 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
162186b1d6d2SBill Fenner 		copym = m_pullup(copym, hlen);
1622df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1623390cdc6aSRuslan Ermilov 		/* If needed, compute the checksum and mark it as valid. */
1624390cdc6aSRuslan Ermilov 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1625390cdc6aSRuslan Ermilov 			in_delayed_cksum(copym);
1626390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1627390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_flags |=
1628390cdc6aSRuslan Ermilov 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1629390cdc6aSRuslan Ermilov 			copym->m_pkthdr.csum_data = 0xffff;
1630390cdc6aSRuslan Ermilov 		}
1631df8bae1dSRodney W. Grimes 		/*
1632df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1633df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1634df8bae1dSRodney W. Grimes 		 */
1635df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1636fd8e4ebcSMike Barcroft 		ip->ip_len = htons(ip->ip_len);
1637fd8e4ebcSMike Barcroft 		ip->ip_off = htons(ip->ip_off);
1638df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
163986b1d6d2SBill Fenner 		ip->ip_sum = in_cksum(copym, hlen);
16409c9137eaSGarrett Wollman 		/*
16419c9137eaSGarrett Wollman 		 * NB:
1642e1596dffSBill Fenner 		 * It's not clear whether there are any lingering
1643e1596dffSBill Fenner 		 * reentrancy problems in other areas which might
1644e1596dffSBill Fenner 		 * be exposed by using ip_input directly (in
1645e1596dffSBill Fenner 		 * particular, everything which modifies the packet
1646e1596dffSBill Fenner 		 * in-place).  Yet another option is using the
1647e1596dffSBill Fenner 		 * protosw directly to deliver the looped back
1648e1596dffSBill Fenner 		 * packet.  For the moment, we'll err on the side
1649ed7509acSJulian Elischer 		 * of safety by using if_simloop().
16509c9137eaSGarrett Wollman 		 */
1651201c2527SJulian Elischer #if 1 /* XXX */
1652201c2527SJulian Elischer 		if (dst->sin_family != AF_INET) {
16532b8a366cSJulian Elischer 			printf("ip_mloopback: bad address family %d\n",
1654201c2527SJulian Elischer 						dst->sin_family);
1655201c2527SJulian Elischer 			dst->sin_family = AF_INET;
1656201c2527SJulian Elischer 		}
1657201c2527SJulian Elischer #endif
1658201c2527SJulian Elischer 
16599c9137eaSGarrett Wollman #ifdef notdef
1660e1596dffSBill Fenner 		copym->m_pkthdr.rcvif = ifp;
1661ed7509acSJulian Elischer 		ip_input(copym);
16629c9137eaSGarrett Wollman #else
166306a429a3SArchie Cobbs 		if_simloop(ifp, copym, dst->sin_family, 0);
16649c9137eaSGarrett Wollman #endif
1665df8bae1dSRodney W. Grimes 	}
1666df8bae1dSRodney W. Grimes }
1667