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