ip_output.c revision 277331
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/netinet/ip_output.c 277331 2015-01-18 18:06:40Z adrian $");
34
35#include "opt_inet.h"
36#include "opt_ipfw.h"
37#include "opt_ipsec.h"
38#include "opt_mbuf_stress_test.h"
39#include "opt_mpath.h"
40#include "opt_route.h"
41#include "opt_sctp.h"
42#include "opt_rss.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/mbuf.h>
49#include <sys/priv.h>
50#include <sys/proc.h>
51#include <sys/protosw.h>
52#include <sys/sdt.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/sysctl.h>
56#include <sys/ucred.h>
57
58#include <net/if.h>
59#include <net/if_var.h>
60#include <net/if_llatbl.h>
61#include <net/netisr.h>
62#include <net/pfil.h>
63#include <net/route.h>
64#include <net/flowtable.h>
65#ifdef RADIX_MPATH
66#include <net/radix_mpath.h>
67#endif
68#include <net/rss_config.h>
69#include <net/vnet.h>
70
71#include <netinet/in.h>
72#include <netinet/in_kdtrace.h>
73#include <netinet/in_systm.h>
74#include <netinet/ip.h>
75#include <netinet/in_pcb.h>
76#include <netinet/in_rss.h>
77#include <netinet/in_var.h>
78#include <netinet/ip_var.h>
79#include <netinet/ip_options.h>
80#ifdef SCTP
81#include <netinet/sctp.h>
82#include <netinet/sctp_crc32.h>
83#endif
84
85#ifdef IPSEC
86#include <netinet/ip_ipsec.h>
87#include <netipsec/ipsec.h>
88#endif /* IPSEC*/
89
90#include <machine/in_cksum.h>
91
92#include <security/mac/mac_framework.h>
93
94VNET_DEFINE(u_short, ip_id);
95
96#ifdef MBUF_STRESS_TEST
97static int mbuf_frag_size = 0;
98SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
99	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
100#endif
101
102static void	ip_mloopback
103	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
104
105
106extern int in_mcast_loop;
107extern	struct protosw inetsw[];
108
109/*
110 * IP output.  The packet in mbuf chain m contains a skeletal IP
111 * header (with len, off, ttl, proto, tos, src, dst).
112 * The mbuf chain containing the packet will be freed.
113 * The mbuf opt, if present, will not be freed.
114 * If route ro is present and has ro_rt initialized, route lookup would be
115 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
116 * then result of route lookup is stored in ro->ro_rt.
117 *
118 * In the IP forwarding case, the packet will arrive with options already
119 * inserted, so must have a NULL opt pointer.
120 */
121int
122ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
123    struct ip_moptions *imo, struct inpcb *inp)
124{
125	struct ip *ip;
126	struct ifnet *ifp = NULL;	/* keep compiler happy */
127	struct mbuf *m0;
128	int hlen = sizeof (struct ip);
129	int mtu;
130	int error = 0;
131	struct sockaddr_in *dst;
132	const struct sockaddr_in *gw;
133	struct in_ifaddr *ia;
134	int isbroadcast;
135	uint16_t ip_len, ip_off;
136	struct route iproute;
137	struct rtentry *rte;	/* cache for ro->ro_rt */
138	struct in_addr odst;
139	struct m_tag *fwd_tag = NULL;
140	uint32_t fibnum;
141	int have_ia_ref;
142	int needfiblookup;
143#ifdef IPSEC
144	int no_route_but_check_spd = 0;
145#endif
146	M_ASSERTPKTHDR(m);
147
148	if (inp != NULL) {
149		INP_LOCK_ASSERT(inp);
150		M_SETFIB(m, inp->inp_inc.inc_fibnum);
151		if ((flags & IP_NODEFAULTFLOWID) == 0) {
152			m->m_pkthdr.flowid = inp->inp_flowid;
153			M_HASHTYPE_SET(m, inp->inp_flowtype);
154		}
155	}
156
157	if (ro == NULL) {
158		ro = &iproute;
159		bzero(ro, sizeof (*ro));
160	}
161
162#ifdef FLOWTABLE
163	if (ro->ro_rt == NULL)
164		(void )flowtable_lookup(AF_INET, m, ro);
165#endif
166
167	if (opt) {
168		int len = 0;
169		m = ip_insertoptions(m, opt, &len);
170		if (len != 0)
171			hlen = len; /* ip->ip_hl is updated above */
172	}
173	ip = mtod(m, struct ip *);
174	ip_len = ntohs(ip->ip_len);
175	ip_off = ntohs(ip->ip_off);
176
177	/*
178	 * Fill in IP header.  If we are not allowing fragmentation,
179	 * then the ip_id field is meaningless, but we don't set it
180	 * to zero.  Doing so causes various problems when devices along
181	 * the path (routers, load balancers, firewalls, etc.) illegally
182	 * disable DF on our packet.  Note that a 16-bit counter
183	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
184	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
185	 * for Counting NATted Hosts", Proc. IMW'02, available at
186	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
187	 */
188	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
189		ip->ip_v = IPVERSION;
190		ip->ip_hl = hlen >> 2;
191		ip->ip_id = ip_newid();
192		IPSTAT_INC(ips_localout);
193	} else {
194		/* Header already set, fetch hlen from there */
195		hlen = ip->ip_hl << 2;
196	}
197
198	/*
199	 * dst/gw handling:
200	 *
201	 * dst can be rewritten but always points to &ro->ro_dst.
202	 * gw is readonly but can point either to dst OR rt_gateway,
203	 * therefore we need restore gw if we're redoing lookup.
204	 */
205	gw = dst = (struct sockaddr_in *)&ro->ro_dst;
206	fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
207again:
208	ia = NULL;
209	have_ia_ref = 0;
210	/*
211	 * If there is a cached route, check that it is to the same
212	 * destination and is still up.  If not, free it and try again.
213	 * The address family should also be checked in case of sharing
214	 * the cache with IPv6.
215	 */
216	rte = ro->ro_rt;
217	if (rte && ((rte->rt_flags & RTF_UP) == 0 ||
218		    rte->rt_ifp == NULL ||
219		    !RT_LINK_IS_UP(rte->rt_ifp) ||
220			  dst->sin_family != AF_INET ||
221			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
222		RO_RTFREE(ro);
223		ro->ro_lle = NULL;
224		rte = NULL;
225		gw = dst;
226	}
227	if (rte == NULL && fwd_tag == NULL) {
228		bzero(dst, sizeof(*dst));
229		dst->sin_family = AF_INET;
230		dst->sin_len = sizeof(*dst);
231		dst->sin_addr = ip->ip_dst;
232	}
233	/*
234	 * If routing to interface only, short circuit routing lookup.
235	 * The use of an all-ones broadcast address implies this; an
236	 * interface is specified by the broadcast address of an interface,
237	 * or the destination address of a ptp interface.
238	 */
239	if (flags & IP_SENDONES) {
240		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst),
241						      M_GETFIB(m)))) == NULL &&
242		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
243						    M_GETFIB(m)))) == NULL) {
244			IPSTAT_INC(ips_noroute);
245			error = ENETUNREACH;
246			goto bad;
247		}
248		have_ia_ref = 1;
249		ip->ip_dst.s_addr = INADDR_BROADCAST;
250		dst->sin_addr = ip->ip_dst;
251		ifp = ia->ia_ifp;
252		ip->ip_ttl = 1;
253		isbroadcast = 1;
254	} else if (flags & IP_ROUTETOIF) {
255		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst),
256						    M_GETFIB(m)))) == NULL &&
257		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0,
258						M_GETFIB(m)))) == NULL) {
259			IPSTAT_INC(ips_noroute);
260			error = ENETUNREACH;
261			goto bad;
262		}
263		have_ia_ref = 1;
264		ifp = ia->ia_ifp;
265		ip->ip_ttl = 1;
266		isbroadcast = in_broadcast(dst->sin_addr, ifp);
267	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
268	    imo != NULL && imo->imo_multicast_ifp != NULL) {
269		/*
270		 * Bypass the normal routing lookup for multicast
271		 * packets if the interface is specified.
272		 */
273		ifp = imo->imo_multicast_ifp;
274		IFP_TO_IA(ifp, ia);
275		if (ia)
276			have_ia_ref = 1;
277		isbroadcast = 0;	/* fool gcc */
278	} else {
279		/*
280		 * We want to do any cloning requested by the link layer,
281		 * as this is probably required in all cases for correct
282		 * operation (as it is for ARP).
283		 */
284		if (rte == NULL) {
285#ifdef RADIX_MPATH
286			rtalloc_mpath_fib(ro,
287			    ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr),
288			    fibnum);
289#else
290			in_rtalloc_ign(ro, 0, fibnum);
291#endif
292			rte = ro->ro_rt;
293		}
294		if (rte == NULL ||
295		    rte->rt_ifp == NULL ||
296		    !RT_LINK_IS_UP(rte->rt_ifp)) {
297#ifdef IPSEC
298			/*
299			 * There is no route for this packet, but it is
300			 * possible that a matching SPD entry exists.
301			 */
302			no_route_but_check_spd = 1;
303			mtu = 0; /* Silence GCC warning. */
304			goto sendit;
305#endif
306			IPSTAT_INC(ips_noroute);
307			error = EHOSTUNREACH;
308			goto bad;
309		}
310		ia = ifatoia(rte->rt_ifa);
311		ifp = rte->rt_ifp;
312		counter_u64_add(rte->rt_pksent, 1);
313		if (rte->rt_flags & RTF_GATEWAY)
314			gw = (struct sockaddr_in *)rte->rt_gateway;
315		if (rte->rt_flags & RTF_HOST)
316			isbroadcast = (rte->rt_flags & RTF_BROADCAST);
317		else
318			isbroadcast = in_broadcast(gw->sin_addr, ifp);
319	}
320	/*
321	 * Calculate MTU.  If we have a route that is up, use that,
322	 * otherwise use the interface's MTU.
323	 */
324	if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST)))
325		mtu = rte->rt_mtu;
326	else
327		mtu = ifp->if_mtu;
328	/* Catch a possible divide by zero later. */
329	KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p",
330	    __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp));
331	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
332		m->m_flags |= M_MCAST;
333		/*
334		 * IP destination address is multicast.  Make sure "gw"
335		 * still points to the address in "ro".  (It may have been
336		 * changed to point to a gateway address, above.)
337		 */
338		gw = dst;
339		/*
340		 * See if the caller provided any multicast options
341		 */
342		if (imo != NULL) {
343			ip->ip_ttl = imo->imo_multicast_ttl;
344			if (imo->imo_multicast_vif != -1)
345				ip->ip_src.s_addr =
346				    ip_mcast_src ?
347				    ip_mcast_src(imo->imo_multicast_vif) :
348				    INADDR_ANY;
349		} else
350			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
351		/*
352		 * Confirm that the outgoing interface supports multicast.
353		 */
354		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
355			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
356				IPSTAT_INC(ips_noroute);
357				error = ENETUNREACH;
358				goto bad;
359			}
360		}
361		/*
362		 * If source address not specified yet, use address
363		 * of outgoing interface.
364		 */
365		if (ip->ip_src.s_addr == INADDR_ANY) {
366			/* Interface may have no addresses. */
367			if (ia != NULL)
368				ip->ip_src = IA_SIN(ia)->sin_addr;
369		}
370
371		if ((imo == NULL && in_mcast_loop) ||
372		    (imo && imo->imo_multicast_loop)) {
373			/*
374			 * Loop back multicast datagram if not expressly
375			 * forbidden to do so, even if we are not a member
376			 * of the group; ip_input() will filter it later,
377			 * thus deferring a hash lookup and mutex acquisition
378			 * at the expense of a cheap copy using m_copym().
379			 */
380			ip_mloopback(ifp, m, dst, hlen);
381		} else {
382			/*
383			 * If we are acting as a multicast router, perform
384			 * multicast forwarding as if the packet had just
385			 * arrived on the interface to which we are about
386			 * to send.  The multicast forwarding function
387			 * recursively calls this function, using the
388			 * IP_FORWARDING flag to prevent infinite recursion.
389			 *
390			 * Multicasts that are looped back by ip_mloopback(),
391			 * above, will be forwarded by the ip_input() routine,
392			 * if necessary.
393			 */
394			if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) {
395				/*
396				 * If rsvp daemon is not running, do not
397				 * set ip_moptions. This ensures that the packet
398				 * is multicast and not just sent down one link
399				 * as prescribed by rsvpd.
400				 */
401				if (!V_rsvp_on)
402					imo = NULL;
403				if (ip_mforward &&
404				    ip_mforward(ip, ifp, m, imo) != 0) {
405					m_freem(m);
406					goto done;
407				}
408			}
409		}
410
411		/*
412		 * Multicasts with a time-to-live of zero may be looped-
413		 * back, above, but must not be transmitted on a network.
414		 * Also, multicasts addressed to the loopback interface
415		 * are not sent -- the above call to ip_mloopback() will
416		 * loop back a copy. ip_input() will drop the copy if
417		 * this host does not belong to the destination group on
418		 * the loopback interface.
419		 */
420		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
421			m_freem(m);
422			goto done;
423		}
424
425		goto sendit;
426	}
427
428	/*
429	 * If the source address is not specified yet, use the address
430	 * of the outoing interface.
431	 */
432	if (ip->ip_src.s_addr == INADDR_ANY) {
433		/* Interface may have no addresses. */
434		if (ia != NULL) {
435			ip->ip_src = IA_SIN(ia)->sin_addr;
436		}
437	}
438
439	/*
440	 * Look for broadcast address and
441	 * verify user is allowed to send
442	 * such a packet.
443	 */
444	if (isbroadcast) {
445		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
446			error = EADDRNOTAVAIL;
447			goto bad;
448		}
449		if ((flags & IP_ALLOWBROADCAST) == 0) {
450			error = EACCES;
451			goto bad;
452		}
453		/* don't allow broadcast messages to be fragmented */
454		if (ip_len > mtu) {
455			error = EMSGSIZE;
456			goto bad;
457		}
458		m->m_flags |= M_BCAST;
459	} else {
460		m->m_flags &= ~M_BCAST;
461	}
462
463sendit:
464#ifdef IPSEC
465	switch(ip_ipsec_output(&m, inp, &error)) {
466	case 1:
467		goto bad;
468	case -1:
469		goto done;
470	case 0:
471	default:
472		break;	/* Continue with packet processing. */
473	}
474	/*
475	 * Check if there was a route for this packet; return error if not.
476	 */
477	if (no_route_but_check_spd) {
478		IPSTAT_INC(ips_noroute);
479		error = EHOSTUNREACH;
480		goto bad;
481	}
482	/* Update variables that are affected by ipsec4_output(). */
483	ip = mtod(m, struct ip *);
484	hlen = ip->ip_hl << 2;
485#endif /* IPSEC */
486
487	/* Jump over all PFIL processing if hooks are not active. */
488	if (!PFIL_HOOKED(&V_inet_pfil_hook))
489		goto passout;
490
491	/* Run through list of hooks for output packets. */
492	odst.s_addr = ip->ip_dst.s_addr;
493	error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
494	if (error != 0 || m == NULL)
495		goto done;
496
497	ip = mtod(m, struct ip *);
498	needfiblookup = 0;
499
500	/* See if destination IP address was changed by packet filter. */
501	if (odst.s_addr != ip->ip_dst.s_addr) {
502		m->m_flags |= M_SKIP_FIREWALL;
503		/* If destination is now ourself drop to ip_input(). */
504		if (in_localip(ip->ip_dst)) {
505			m->m_flags |= M_FASTFWD_OURS;
506			if (m->m_pkthdr.rcvif == NULL)
507				m->m_pkthdr.rcvif = V_loif;
508			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
509				m->m_pkthdr.csum_flags |=
510				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
511				m->m_pkthdr.csum_data = 0xffff;
512			}
513			m->m_pkthdr.csum_flags |=
514			    CSUM_IP_CHECKED | CSUM_IP_VALID;
515#ifdef SCTP
516			if (m->m_pkthdr.csum_flags & CSUM_SCTP)
517				m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
518#endif
519			error = netisr_queue(NETISR_IP, m);
520			goto done;
521		} else {
522			if (have_ia_ref)
523				ifa_free(&ia->ia_ifa);
524			needfiblookup = 1; /* Redo the routing table lookup. */
525		}
526	}
527	/* See if fib was changed by packet filter. */
528	if (fibnum != M_GETFIB(m)) {
529		m->m_flags |= M_SKIP_FIREWALL;
530		fibnum = M_GETFIB(m);
531		RO_RTFREE(ro);
532		needfiblookup = 1;
533	}
534	if (needfiblookup)
535		goto again;
536
537	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
538	if (m->m_flags & M_FASTFWD_OURS) {
539		if (m->m_pkthdr.rcvif == NULL)
540			m->m_pkthdr.rcvif = V_loif;
541		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
542			m->m_pkthdr.csum_flags |=
543			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
544			m->m_pkthdr.csum_data = 0xffff;
545		}
546#ifdef SCTP
547		if (m->m_pkthdr.csum_flags & CSUM_SCTP)
548			m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
549#endif
550		m->m_pkthdr.csum_flags |=
551			    CSUM_IP_CHECKED | CSUM_IP_VALID;
552
553		error = netisr_queue(NETISR_IP, m);
554		goto done;
555	}
556	/* Or forward to some other address? */
557	if ((m->m_flags & M_IP_NEXTHOP) &&
558	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
559		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
560		m->m_flags |= M_SKIP_FIREWALL;
561		m->m_flags &= ~M_IP_NEXTHOP;
562		m_tag_delete(m, fwd_tag);
563		if (have_ia_ref)
564			ifa_free(&ia->ia_ifa);
565		goto again;
566	}
567
568passout:
569	/* 127/8 must not appear on wire - RFC1122. */
570	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
571	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
572		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
573			IPSTAT_INC(ips_badaddr);
574			error = EADDRNOTAVAIL;
575			goto bad;
576		}
577	}
578
579	m->m_pkthdr.csum_flags |= CSUM_IP;
580	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) {
581		in_delayed_cksum(m);
582		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
583	}
584#ifdef SCTP
585	if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) {
586		sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
587		m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
588	}
589#endif
590
591	/*
592	 * If small enough for interface, or the interface will take
593	 * care of the fragmentation for us, we can just send directly.
594	 */
595	if (ip_len <= mtu ||
596	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) {
597		ip->ip_sum = 0;
598		if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) {
599			ip->ip_sum = in_cksum(m, hlen);
600			m->m_pkthdr.csum_flags &= ~CSUM_IP;
601		}
602
603		/*
604		 * Record statistics for this interface address.
605		 * With CSUM_TSO the byte/packet count will be slightly
606		 * incorrect because we count the IP+TCP headers only
607		 * once instead of for every generated packet.
608		 */
609		if (!(flags & IP_FORWARDING) && ia) {
610			if (m->m_pkthdr.csum_flags & CSUM_TSO)
611				counter_u64_add(ia->ia_ifa.ifa_opackets,
612				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz);
613			else
614				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
615
616			counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len);
617		}
618#ifdef MBUF_STRESS_TEST
619		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
620			m = m_fragment(m, M_NOWAIT, mbuf_frag_size);
621#endif
622		/*
623		 * Reset layer specific mbuf flags
624		 * to avoid confusing lower layers.
625		 */
626		m_clrprotoflags(m);
627		IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
628		error = (*ifp->if_output)(ifp, m,
629		    (const struct sockaddr *)gw, ro);
630		goto done;
631	}
632
633	/* Balk when DF bit is set or the interface didn't support TSO. */
634	if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
635		error = EMSGSIZE;
636		IPSTAT_INC(ips_cantfrag);
637		goto bad;
638	}
639
640	/*
641	 * Too large for interface; fragment if possible. If successful,
642	 * on return, m will point to a list of packets to be sent.
643	 */
644	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist);
645	if (error)
646		goto bad;
647	for (; m; m = m0) {
648		m0 = m->m_nextpkt;
649		m->m_nextpkt = 0;
650		if (error == 0) {
651			/* Record statistics for this interface address. */
652			if (ia != NULL) {
653				counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
654				counter_u64_add(ia->ia_ifa.ifa_obytes,
655				    m->m_pkthdr.len);
656			}
657			/*
658			 * Reset layer specific mbuf flags
659			 * to avoid confusing upper layers.
660			 */
661			m_clrprotoflags(m);
662
663			IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL);
664			error = (*ifp->if_output)(ifp, m,
665			    (const struct sockaddr *)gw, ro);
666		} else
667			m_freem(m);
668	}
669
670	if (error == 0)
671		IPSTAT_INC(ips_fragmented);
672
673done:
674	if (ro == &iproute)
675		RO_RTFREE(ro);
676	if (have_ia_ref)
677		ifa_free(&ia->ia_ifa);
678	return (error);
679bad:
680	m_freem(m);
681	goto done;
682}
683
684/*
685 * Create a chain of fragments which fit the given mtu. m_frag points to the
686 * mbuf to be fragmented; on return it points to the chain with the fragments.
687 * Return 0 if no error. If error, m_frag may contain a partially built
688 * chain of fragments that should be freed by the caller.
689 *
690 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
691 */
692int
693ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
694    u_long if_hwassist_flags)
695{
696	int error = 0;
697	int hlen = ip->ip_hl << 2;
698	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
699	int off;
700	struct mbuf *m0 = *m_frag;	/* the original packet		*/
701	int firstlen;
702	struct mbuf **mnext;
703	int nfrags;
704	uint16_t ip_len, ip_off;
705
706	ip_len = ntohs(ip->ip_len);
707	ip_off = ntohs(ip->ip_off);
708
709	if (ip_off & IP_DF) {	/* Fragmentation not allowed */
710		IPSTAT_INC(ips_cantfrag);
711		return EMSGSIZE;
712	}
713
714	/*
715	 * Must be able to put at least 8 bytes per fragment.
716	 */
717	if (len < 8)
718		return EMSGSIZE;
719
720	/*
721	 * If the interface will not calculate checksums on
722	 * fragmented packets, then do it here.
723	 */
724	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
725		in_delayed_cksum(m0);
726		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
727	}
728#ifdef SCTP
729	if (m0->m_pkthdr.csum_flags & CSUM_SCTP) {
730		sctp_delayed_cksum(m0, hlen);
731		m0->m_pkthdr.csum_flags &= ~CSUM_SCTP;
732	}
733#endif
734	if (len > PAGE_SIZE) {
735		/*
736		 * Fragment large datagrams such that each segment
737		 * contains a multiple of PAGE_SIZE amount of data,
738		 * plus headers. This enables a receiver to perform
739		 * page-flipping zero-copy optimizations.
740		 *
741		 * XXX When does this help given that sender and receiver
742		 * could have different page sizes, and also mtu could
743		 * be less than the receiver's page size ?
744		 */
745		int newlen;
746		struct mbuf *m;
747
748		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
749			off += m->m_len;
750
751		/*
752		 * firstlen (off - hlen) must be aligned on an
753		 * 8-byte boundary
754		 */
755		if (off < hlen)
756			goto smart_frag_failure;
757		off = ((off - hlen) & ~7) + hlen;
758		newlen = (~PAGE_MASK) & mtu;
759		if ((newlen + sizeof (struct ip)) > mtu) {
760			/* we failed, go back the default */
761smart_frag_failure:
762			newlen = len;
763			off = hlen + len;
764		}
765		len = newlen;
766
767	} else {
768		off = hlen + len;
769	}
770
771	firstlen = off - hlen;
772	mnext = &m0->m_nextpkt;		/* pointer to next packet */
773
774	/*
775	 * Loop through length of segment after first fragment,
776	 * make new header and copy data of each part and link onto chain.
777	 * Here, m0 is the original packet, m is the fragment being created.
778	 * The fragments are linked off the m_nextpkt of the original
779	 * packet, which after processing serves as the first fragment.
780	 */
781	for (nfrags = 1; off < ip_len; off += len, nfrags++) {
782		struct ip *mhip;	/* ip header on the fragment */
783		struct mbuf *m;
784		int mhlen = sizeof (struct ip);
785
786		m = m_gethdr(M_NOWAIT, MT_DATA);
787		if (m == NULL) {
788			error = ENOBUFS;
789			IPSTAT_INC(ips_odropped);
790			goto done;
791		}
792		m->m_flags |= (m0->m_flags & M_MCAST);
793		/*
794		 * In the first mbuf, leave room for the link header, then
795		 * copy the original IP header including options. The payload
796		 * goes into an additional mbuf chain returned by m_copym().
797		 */
798		m->m_data += max_linkhdr;
799		mhip = mtod(m, struct ip *);
800		*mhip = *ip;
801		if (hlen > sizeof (struct ip)) {
802			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
803			mhip->ip_v = IPVERSION;
804			mhip->ip_hl = mhlen >> 2;
805		}
806		m->m_len = mhlen;
807		/* XXX do we need to add ip_off below ? */
808		mhip->ip_off = ((off - hlen) >> 3) + ip_off;
809		if (off + len >= ip_len)
810			len = ip_len - off;
811		else
812			mhip->ip_off |= IP_MF;
813		mhip->ip_len = htons((u_short)(len + mhlen));
814		m->m_next = m_copym(m0, off, len, M_NOWAIT);
815		if (m->m_next == NULL) {	/* copy failed */
816			m_free(m);
817			error = ENOBUFS;	/* ??? */
818			IPSTAT_INC(ips_odropped);
819			goto done;
820		}
821		m->m_pkthdr.len = mhlen + len;
822		m->m_pkthdr.rcvif = NULL;
823#ifdef MAC
824		mac_netinet_fragment(m0, m);
825#endif
826		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
827		mhip->ip_off = htons(mhip->ip_off);
828		mhip->ip_sum = 0;
829		if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
830			mhip->ip_sum = in_cksum(m, mhlen);
831			m->m_pkthdr.csum_flags &= ~CSUM_IP;
832		}
833		*mnext = m;
834		mnext = &m->m_nextpkt;
835	}
836	IPSTAT_ADD(ips_ofragments, nfrags);
837
838	/*
839	 * Update first fragment by trimming what's been copied out
840	 * and updating header.
841	 */
842	m_adj(m0, hlen + firstlen - ip_len);
843	m0->m_pkthdr.len = hlen + firstlen;
844	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
845	ip->ip_off = htons(ip_off | IP_MF);
846	ip->ip_sum = 0;
847	if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) {
848		ip->ip_sum = in_cksum(m0, hlen);
849		m0->m_pkthdr.csum_flags &= ~CSUM_IP;
850	}
851
852done:
853	*m_frag = m0;
854	return error;
855}
856
857void
858in_delayed_cksum(struct mbuf *m)
859{
860	struct ip *ip;
861	uint16_t csum, offset, ip_len;
862
863	ip = mtod(m, struct ip *);
864	offset = ip->ip_hl << 2 ;
865	ip_len = ntohs(ip->ip_len);
866	csum = in_cksum_skip(m, ip_len, offset);
867	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
868		csum = 0xffff;
869	offset += m->m_pkthdr.csum_data;	/* checksum offset */
870
871	/* find the mbuf in the chain where the checksum starts*/
872	while ((m != NULL) && (offset >= m->m_len)) {
873		offset -= m->m_len;
874		m = m->m_next;
875	}
876	KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain."));
877	KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs."));
878	*(u_short *)(m->m_data + offset) = csum;
879}
880
881/*
882 * IP socket option processing.
883 */
884int
885ip_ctloutput(struct socket *so, struct sockopt *sopt)
886{
887	struct	inpcb *inp = sotoinpcb(so);
888	int	error, optval;
889#ifdef	RSS
890	uint32_t rss_bucket;
891	int retval;
892#endif
893
894	error = optval = 0;
895	if (sopt->sopt_level != IPPROTO_IP) {
896		error = EINVAL;
897
898		if (sopt->sopt_level == SOL_SOCKET &&
899		    sopt->sopt_dir == SOPT_SET) {
900			switch (sopt->sopt_name) {
901			case SO_REUSEADDR:
902				INP_WLOCK(inp);
903				if ((so->so_options & SO_REUSEADDR) != 0)
904					inp->inp_flags2 |= INP_REUSEADDR;
905				else
906					inp->inp_flags2 &= ~INP_REUSEADDR;
907				INP_WUNLOCK(inp);
908				error = 0;
909				break;
910			case SO_REUSEPORT:
911				INP_WLOCK(inp);
912				if ((so->so_options & SO_REUSEPORT) != 0)
913					inp->inp_flags2 |= INP_REUSEPORT;
914				else
915					inp->inp_flags2 &= ~INP_REUSEPORT;
916				INP_WUNLOCK(inp);
917				error = 0;
918				break;
919			case SO_SETFIB:
920				INP_WLOCK(inp);
921				inp->inp_inc.inc_fibnum = so->so_fibnum;
922				INP_WUNLOCK(inp);
923				error = 0;
924				break;
925			default:
926				break;
927			}
928		}
929		return (error);
930	}
931
932	switch (sopt->sopt_dir) {
933	case SOPT_SET:
934		switch (sopt->sopt_name) {
935		case IP_OPTIONS:
936#ifdef notyet
937		case IP_RETOPTS:
938#endif
939		{
940			struct mbuf *m;
941			if (sopt->sopt_valsize > MLEN) {
942				error = EMSGSIZE;
943				break;
944			}
945			m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA);
946			if (m == NULL) {
947				error = ENOBUFS;
948				break;
949			}
950			m->m_len = sopt->sopt_valsize;
951			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
952					    m->m_len);
953			if (error) {
954				m_free(m);
955				break;
956			}
957			INP_WLOCK(inp);
958			error = ip_pcbopts(inp, sopt->sopt_name, m);
959			INP_WUNLOCK(inp);
960			return (error);
961		}
962
963		case IP_BINDANY:
964			if (sopt->sopt_td != NULL) {
965				error = priv_check(sopt->sopt_td,
966				    PRIV_NETINET_BINDANY);
967				if (error)
968					break;
969			}
970			/* FALLTHROUGH */
971		case IP_BINDMULTI:
972#ifdef	RSS
973		case IP_RSS_LISTEN_BUCKET:
974#endif
975		case IP_TOS:
976		case IP_TTL:
977		case IP_MINTTL:
978		case IP_RECVOPTS:
979		case IP_RECVRETOPTS:
980		case IP_RECVDSTADDR:
981		case IP_RECVTTL:
982		case IP_RECVIF:
983		case IP_ONESBCAST:
984		case IP_DONTFRAG:
985		case IP_RECVTOS:
986		case IP_RECVFLOWID:
987#ifdef	RSS
988		case IP_RECVRSSBUCKETID:
989#endif
990			error = sooptcopyin(sopt, &optval, sizeof optval,
991					    sizeof optval);
992			if (error)
993				break;
994
995			switch (sopt->sopt_name) {
996			case IP_TOS:
997				inp->inp_ip_tos = optval;
998				break;
999
1000			case IP_TTL:
1001				inp->inp_ip_ttl = optval;
1002				break;
1003
1004			case IP_MINTTL:
1005				if (optval >= 0 && optval <= MAXTTL)
1006					inp->inp_ip_minttl = optval;
1007				else
1008					error = EINVAL;
1009				break;
1010
1011#define	OPTSET(bit) do {						\
1012	INP_WLOCK(inp);							\
1013	if (optval)							\
1014		inp->inp_flags |= bit;					\
1015	else								\
1016		inp->inp_flags &= ~bit;					\
1017	INP_WUNLOCK(inp);						\
1018} while (0)
1019
1020#define	OPTSET2(bit, val) do {						\
1021	INP_WLOCK(inp);							\
1022	if (val)							\
1023		inp->inp_flags2 |= bit;					\
1024	else								\
1025		inp->inp_flags2 &= ~bit;				\
1026	INP_WUNLOCK(inp);						\
1027} while (0)
1028
1029			case IP_RECVOPTS:
1030				OPTSET(INP_RECVOPTS);
1031				break;
1032
1033			case IP_RECVRETOPTS:
1034				OPTSET(INP_RECVRETOPTS);
1035				break;
1036
1037			case IP_RECVDSTADDR:
1038				OPTSET(INP_RECVDSTADDR);
1039				break;
1040
1041			case IP_RECVTTL:
1042				OPTSET(INP_RECVTTL);
1043				break;
1044
1045			case IP_RECVIF:
1046				OPTSET(INP_RECVIF);
1047				break;
1048
1049			case IP_ONESBCAST:
1050				OPTSET(INP_ONESBCAST);
1051				break;
1052			case IP_DONTFRAG:
1053				OPTSET(INP_DONTFRAG);
1054				break;
1055			case IP_BINDANY:
1056				OPTSET(INP_BINDANY);
1057				break;
1058			case IP_RECVTOS:
1059				OPTSET(INP_RECVTOS);
1060				break;
1061			case IP_BINDMULTI:
1062				OPTSET2(INP_BINDMULTI, optval);
1063				break;
1064			case IP_RECVFLOWID:
1065				OPTSET2(INP_RECVFLOWID, optval);
1066				break;
1067#ifdef	RSS
1068			case IP_RSS_LISTEN_BUCKET:
1069				if ((optval >= 0) &&
1070				    (optval < rss_getnumbuckets())) {
1071					inp->inp_rss_listen_bucket = optval;
1072					OPTSET2(INP_RSS_BUCKET_SET, 1);
1073				} else {
1074					error = EINVAL;
1075				}
1076				break;
1077			case IP_RECVRSSBUCKETID:
1078				OPTSET2(INP_RECVRSSBUCKETID, optval);
1079				break;
1080#endif
1081			}
1082			break;
1083#undef OPTSET
1084#undef OPTSET2
1085
1086		/*
1087		 * Multicast socket options are processed by the in_mcast
1088		 * module.
1089		 */
1090		case IP_MULTICAST_IF:
1091		case IP_MULTICAST_VIF:
1092		case IP_MULTICAST_TTL:
1093		case IP_MULTICAST_LOOP:
1094		case IP_ADD_MEMBERSHIP:
1095		case IP_DROP_MEMBERSHIP:
1096		case IP_ADD_SOURCE_MEMBERSHIP:
1097		case IP_DROP_SOURCE_MEMBERSHIP:
1098		case IP_BLOCK_SOURCE:
1099		case IP_UNBLOCK_SOURCE:
1100		case IP_MSFILTER:
1101		case MCAST_JOIN_GROUP:
1102		case MCAST_LEAVE_GROUP:
1103		case MCAST_JOIN_SOURCE_GROUP:
1104		case MCAST_LEAVE_SOURCE_GROUP:
1105		case MCAST_BLOCK_SOURCE:
1106		case MCAST_UNBLOCK_SOURCE:
1107			error = inp_setmoptions(inp, sopt);
1108			break;
1109
1110		case IP_PORTRANGE:
1111			error = sooptcopyin(sopt, &optval, sizeof optval,
1112					    sizeof optval);
1113			if (error)
1114				break;
1115
1116			INP_WLOCK(inp);
1117			switch (optval) {
1118			case IP_PORTRANGE_DEFAULT:
1119				inp->inp_flags &= ~(INP_LOWPORT);
1120				inp->inp_flags &= ~(INP_HIGHPORT);
1121				break;
1122
1123			case IP_PORTRANGE_HIGH:
1124				inp->inp_flags &= ~(INP_LOWPORT);
1125				inp->inp_flags |= INP_HIGHPORT;
1126				break;
1127
1128			case IP_PORTRANGE_LOW:
1129				inp->inp_flags &= ~(INP_HIGHPORT);
1130				inp->inp_flags |= INP_LOWPORT;
1131				break;
1132
1133			default:
1134				error = EINVAL;
1135				break;
1136			}
1137			INP_WUNLOCK(inp);
1138			break;
1139
1140#ifdef IPSEC
1141		case IP_IPSEC_POLICY:
1142		{
1143			caddr_t req;
1144			struct mbuf *m;
1145
1146			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1147				break;
1148			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1149				break;
1150			req = mtod(m, caddr_t);
1151			error = ipsec_set_policy(inp, sopt->sopt_name, req,
1152			    m->m_len, (sopt->sopt_td != NULL) ?
1153			    sopt->sopt_td->td_ucred : NULL);
1154			m_freem(m);
1155			break;
1156		}
1157#endif /* IPSEC */
1158
1159		default:
1160			error = ENOPROTOOPT;
1161			break;
1162		}
1163		break;
1164
1165	case SOPT_GET:
1166		switch (sopt->sopt_name) {
1167		case IP_OPTIONS:
1168		case IP_RETOPTS:
1169			if (inp->inp_options)
1170				error = sooptcopyout(sopt,
1171						     mtod(inp->inp_options,
1172							  char *),
1173						     inp->inp_options->m_len);
1174			else
1175				sopt->sopt_valsize = 0;
1176			break;
1177
1178		case IP_TOS:
1179		case IP_TTL:
1180		case IP_MINTTL:
1181		case IP_RECVOPTS:
1182		case IP_RECVRETOPTS:
1183		case IP_RECVDSTADDR:
1184		case IP_RECVTTL:
1185		case IP_RECVIF:
1186		case IP_PORTRANGE:
1187		case IP_ONESBCAST:
1188		case IP_DONTFRAG:
1189		case IP_BINDANY:
1190		case IP_RECVTOS:
1191		case IP_BINDMULTI:
1192		case IP_FLOWID:
1193		case IP_FLOWTYPE:
1194		case IP_RECVFLOWID:
1195#ifdef	RSS
1196		case IP_RSSBUCKETID:
1197		case IP_RECVRSSBUCKETID:
1198#endif
1199			switch (sopt->sopt_name) {
1200
1201			case IP_TOS:
1202				optval = inp->inp_ip_tos;
1203				break;
1204
1205			case IP_TTL:
1206				optval = inp->inp_ip_ttl;
1207				break;
1208
1209			case IP_MINTTL:
1210				optval = inp->inp_ip_minttl;
1211				break;
1212
1213#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1214#define	OPTBIT2(bit)	(inp->inp_flags2 & bit ? 1 : 0)
1215
1216			case IP_RECVOPTS:
1217				optval = OPTBIT(INP_RECVOPTS);
1218				break;
1219
1220			case IP_RECVRETOPTS:
1221				optval = OPTBIT(INP_RECVRETOPTS);
1222				break;
1223
1224			case IP_RECVDSTADDR:
1225				optval = OPTBIT(INP_RECVDSTADDR);
1226				break;
1227
1228			case IP_RECVTTL:
1229				optval = OPTBIT(INP_RECVTTL);
1230				break;
1231
1232			case IP_RECVIF:
1233				optval = OPTBIT(INP_RECVIF);
1234				break;
1235
1236			case IP_PORTRANGE:
1237				if (inp->inp_flags & INP_HIGHPORT)
1238					optval = IP_PORTRANGE_HIGH;
1239				else if (inp->inp_flags & INP_LOWPORT)
1240					optval = IP_PORTRANGE_LOW;
1241				else
1242					optval = 0;
1243				break;
1244
1245			case IP_ONESBCAST:
1246				optval = OPTBIT(INP_ONESBCAST);
1247				break;
1248			case IP_DONTFRAG:
1249				optval = OPTBIT(INP_DONTFRAG);
1250				break;
1251			case IP_BINDANY:
1252				optval = OPTBIT(INP_BINDANY);
1253				break;
1254			case IP_RECVTOS:
1255				optval = OPTBIT(INP_RECVTOS);
1256				break;
1257			case IP_FLOWID:
1258				optval = inp->inp_flowid;
1259				break;
1260			case IP_FLOWTYPE:
1261				optval = inp->inp_flowtype;
1262				break;
1263			case IP_RECVFLOWID:
1264				optval = OPTBIT2(INP_RECVFLOWID);
1265				break;
1266#ifdef	RSS
1267			case IP_RSSBUCKETID:
1268				retval = rss_hash2bucket(inp->inp_flowid,
1269				    inp->inp_flowtype,
1270				    &rss_bucket);
1271				if (retval == 0)
1272					optval = rss_bucket;
1273				else
1274					error = EINVAL;
1275				break;
1276			case IP_RECVRSSBUCKETID:
1277				optval = OPTBIT2(INP_RECVRSSBUCKETID);
1278				break;
1279#endif
1280			case IP_BINDMULTI:
1281				optval = OPTBIT2(INP_BINDMULTI);
1282				break;
1283			}
1284			error = sooptcopyout(sopt, &optval, sizeof optval);
1285			break;
1286
1287		/*
1288		 * Multicast socket options are processed by the in_mcast
1289		 * module.
1290		 */
1291		case IP_MULTICAST_IF:
1292		case IP_MULTICAST_VIF:
1293		case IP_MULTICAST_TTL:
1294		case IP_MULTICAST_LOOP:
1295		case IP_MSFILTER:
1296			error = inp_getmoptions(inp, sopt);
1297			break;
1298
1299#ifdef IPSEC
1300		case IP_IPSEC_POLICY:
1301		{
1302			struct mbuf *m = NULL;
1303			caddr_t req = NULL;
1304			size_t len = 0;
1305
1306			if (m != 0) {
1307				req = mtod(m, caddr_t);
1308				len = m->m_len;
1309			}
1310			error = ipsec_get_policy(sotoinpcb(so), req, len, &m);
1311			if (error == 0)
1312				error = soopt_mcopyout(sopt, m); /* XXX */
1313			if (error == 0)
1314				m_freem(m);
1315			break;
1316		}
1317#endif /* IPSEC */
1318
1319		default:
1320			error = ENOPROTOOPT;
1321			break;
1322		}
1323		break;
1324	}
1325	return (error);
1326}
1327
1328/*
1329 * Routine called from ip_output() to loop back a copy of an IP multicast
1330 * packet to the input queue of a specified interface.  Note that this
1331 * calls the output routine of the loopback "driver", but with an interface
1332 * pointer that might NOT be a loopback interface -- evil, but easier than
1333 * replicating that code here.
1334 */
1335static void
1336ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1337    int hlen)
1338{
1339	register struct ip *ip;
1340	struct mbuf *copym;
1341
1342	/*
1343	 * Make a deep copy of the packet because we're going to
1344	 * modify the pack in order to generate checksums.
1345	 */
1346	copym = m_dup(m, M_NOWAIT);
1347	if (copym != NULL && (!M_WRITABLE(copym) || copym->m_len < hlen))
1348		copym = m_pullup(copym, hlen);
1349	if (copym != NULL) {
1350		/* If needed, compute the checksum and mark it as valid. */
1351		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1352			in_delayed_cksum(copym);
1353			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1354			copym->m_pkthdr.csum_flags |=
1355			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1356			copym->m_pkthdr.csum_data = 0xffff;
1357		}
1358		/*
1359		 * We don't bother to fragment if the IP length is greater
1360		 * than the interface's MTU.  Can this possibly matter?
1361		 */
1362		ip = mtod(copym, struct ip *);
1363		ip->ip_sum = 0;
1364		ip->ip_sum = in_cksum(copym, hlen);
1365#if 1 /* XXX */
1366		if (dst->sin_family != AF_INET) {
1367			printf("ip_mloopback: bad address family %d\n",
1368						dst->sin_family);
1369			dst->sin_family = AF_INET;
1370		}
1371#endif
1372		if_simloop(ifp, copym, dst->sin_family, 0);
1373	}
1374}
1375