xref: /netbsd-src/sys/netinet/ip_output.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ip_output.c,v 1.320 2020/09/08 14:12:57 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * 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 project 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 PROJECT 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 PROJECT 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 /*
33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Public Access Networks Corporation ("Panix").  It was developed under
38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59  * POSSIBILITY OF SUCH DAMAGE.
60  */
61 
62 /*
63  * Copyright (c) 1982, 1986, 1988, 1990, 1993
64  *	The Regents of the University of California.  All rights reserved.
65  *
66  * Redistribution and use in source and binary forms, with or without
67  * modification, are permitted provided that the following conditions
68  * are met:
69  * 1. Redistributions of source code must retain the above copyright
70  *    notice, this list of conditions and the following disclaimer.
71  * 2. Redistributions in binary form must reproduce the above copyright
72  *    notice, this list of conditions and the following disclaimer in the
73  *    documentation and/or other materials provided with the distribution.
74  * 3. Neither the name of the University nor the names of its contributors
75  *    may be used to endorse or promote products derived from this software
76  *    without specific prior written permission.
77  *
78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88  * SUCH DAMAGE.
89  *
90  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
91  */
92 
93 #include <sys/cdefs.h>
94 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.320 2020/09/08 14:12:57 christos Exp $");
95 
96 #ifdef _KERNEL_OPT
97 #include "opt_inet.h"
98 #include "opt_ipsec.h"
99 #include "opt_mrouting.h"
100 #include "opt_net_mpsafe.h"
101 #include "opt_mpls.h"
102 #endif
103 
104 #include "arp.h"
105 
106 #include <sys/param.h>
107 #include <sys/kmem.h>
108 #include <sys/mbuf.h>
109 #include <sys/socket.h>
110 #include <sys/socketvar.h>
111 #include <sys/kauth.h>
112 #include <sys/systm.h>
113 #include <sys/syslog.h>
114 
115 #include <net/if.h>
116 #include <net/if_types.h>
117 #include <net/route.h>
118 #include <net/pfil.h>
119 
120 #include <netinet/in.h>
121 #include <netinet/in_systm.h>
122 #include <netinet/ip.h>
123 #include <netinet/in_pcb.h>
124 #include <netinet/in_var.h>
125 #include <netinet/ip_var.h>
126 #include <netinet/ip_private.h>
127 #include <netinet/in_offload.h>
128 #include <netinet/portalgo.h>
129 #include <netinet/udp.h>
130 #include <netinet/udp_var.h>
131 
132 #ifdef INET6
133 #include <netinet6/ip6_var.h>
134 #endif
135 
136 #ifdef MROUTING
137 #include <netinet/ip_mroute.h>
138 #endif
139 
140 #ifdef IPSEC
141 #include <netipsec/ipsec.h>
142 #include <netipsec/key.h>
143 #endif
144 
145 #ifdef MPLS
146 #include <netmpls/mpls.h>
147 #include <netmpls/mpls_var.h>
148 #endif
149 
150 static int ip_pcbopts(struct inpcb *, const struct sockopt *);
151 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
152 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
153 static void ip_mloopback(struct ifnet *, struct mbuf *,
154     const struct sockaddr_in *);
155 static int ip_ifaddrvalid(const struct in_ifaddr *);
156 
157 extern pfil_head_t *inet_pfil_hook;			/* XXX */
158 
159 int ip_do_loopback_cksum = 0;
160 
161 static int
162 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m,
163     const struct rtentry *rt)
164 {
165 	int error = 0;
166 #ifdef MPLS
167 	union mpls_shim msh;
168 
169 	if (rt == NULL || rt_gettag(rt) == NULL ||
170 	    rt_gettag(rt)->sa_family != AF_MPLS ||
171 	    (m->m_flags & (M_MCAST | M_BCAST)) != 0 ||
172 	    ifp->if_type != IFT_ETHER)
173 		return 0;
174 
175 	msh.s_addr = MPLS_GETSADDR(rt);
176 	if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
177 		struct m_tag *mtag;
178 		/*
179 		 * XXX tentative solution to tell ether_output
180 		 * it's MPLS. Need some more efficient solution.
181 		 */
182 		mtag = m_tag_get(PACKET_TAG_MPLS,
183 		    sizeof(int) /* dummy */,
184 		    M_NOWAIT);
185 		if (mtag == NULL)
186 			return ENOMEM;
187 		m_tag_prepend(m, mtag);
188 	}
189 #endif
190 	return error;
191 }
192 
193 /*
194  * Send an IP packet to a host.
195  */
196 int
197 ip_if_output(struct ifnet * const ifp, struct mbuf * const m,
198     const struct sockaddr * const dst, const struct rtentry *rt)
199 {
200 	int error = 0;
201 
202 	if (rt != NULL) {
203 		error = rt_check_reject_route(rt, ifp);
204 		if (error != 0) {
205 			IP_STATINC(IP_STAT_RTREJECT);
206 			m_freem(m);
207 			return error;
208 		}
209 	}
210 
211 	error = ip_mark_mpls(ifp, m, rt);
212 	if (error != 0) {
213 		m_freem(m);
214 		return error;
215 	}
216 
217 	error = if_output_lock(ifp, ifp, m, dst, rt);
218 
219 	return error;
220 }
221 
222 /*
223  * IP output.  The packet in mbuf chain m contains a skeletal IP
224  * header (with len, off, ttl, proto, tos, src, dst).
225  * The mbuf chain containing the packet will be freed.
226  * The mbuf opt, if present, will not be freed.
227  */
228 int
229 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
230     struct ip_moptions *imo, struct inpcb *inp)
231 {
232 	struct rtentry *rt;
233 	struct ip *ip;
234 	struct ifnet *ifp, *mifp = NULL;
235 	struct mbuf *m = m0;
236 	int len, hlen, error = 0;
237 	struct route iproute;
238 	const struct sockaddr_in *dst;
239 	struct in_ifaddr *ia = NULL;
240 	struct ifaddr *ifa;
241 	int isbroadcast;
242 	int sw_csum;
243 	u_long mtu;
244 	bool natt_frag = false;
245 	bool rtmtu_nolock;
246 	union {
247 		struct sockaddr		sa;
248 		struct sockaddr_in	sin;
249 	} udst, usrc;
250 	struct sockaddr *rdst = &udst.sa;	/* real IP destination, as
251 						 * opposed to the nexthop
252 						 */
253 	struct psref psref, psref_ia;
254 	int bound;
255 	bool bind_need_restore = false;
256 	const struct sockaddr *sa;
257 
258 	len = 0;
259 
260 	MCLAIM(m, &ip_tx_mowner);
261 
262 	KASSERT((m->m_flags & M_PKTHDR) != 0);
263 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
264 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
265 	    (M_CSUM_TCPv4|M_CSUM_UDPv4));
266 	KASSERT(m->m_len >= sizeof(struct ip));
267 
268 	hlen = sizeof(struct ip);
269 	if (opt) {
270 		m = ip_insertoptions(m, opt, &len);
271 		hlen = len;
272 	}
273 	ip = mtod(m, struct ip *);
274 
275 	/*
276 	 * Fill in IP header.
277 	 */
278 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
279 		ip->ip_v = IPVERSION;
280 		ip->ip_off = htons(0);
281 		/* ip->ip_id filled in after we find out source ia */
282 		ip->ip_hl = hlen >> 2;
283 		IP_STATINC(IP_STAT_LOCALOUT);
284 	} else {
285 		hlen = ip->ip_hl << 2;
286 	}
287 
288 	/*
289 	 * Route packet.
290 	 */
291 	if (ro == NULL) {
292 		memset(&iproute, 0, sizeof(iproute));
293 		ro = &iproute;
294 	}
295 	sockaddr_in_init(&udst.sin, &ip->ip_dst, 0);
296 	dst = satocsin(rtcache_getdst(ro));
297 
298 	/*
299 	 * If there is a cached route, check that it is to the same
300 	 * destination and is still up.  If not, free it and try again.
301 	 * The address family should also be checked in case of sharing
302 	 * the cache with IPv6.
303 	 */
304 	if (dst && (dst->sin_family != AF_INET ||
305 	    !in_hosteq(dst->sin_addr, ip->ip_dst)))
306 		rtcache_free(ro);
307 
308 	/* XXX must be before rtcache operations */
309 	bound = curlwp_bind();
310 	bind_need_restore = true;
311 
312 	if ((rt = rtcache_validate(ro)) == NULL &&
313 	    (rt = rtcache_update(ro, 1)) == NULL) {
314 		dst = &udst.sin;
315 		error = rtcache_setdst(ro, &udst.sa);
316 		if (error != 0) {
317 			IP_STATINC(IP_STAT_ODROPPED);
318 			goto bad;
319 		}
320 	}
321 
322 	/*
323 	 * If routing to interface only, short circuit routing lookup.
324 	 */
325 	if (flags & IP_ROUTETOIF) {
326 		ifa = ifa_ifwithladdr_psref(sintocsa(dst), &psref_ia);
327 		if (ifa == NULL) {
328 			IP_STATINC(IP_STAT_NOROUTE);
329 			error = ENETUNREACH;
330 			goto bad;
331 		}
332 		/* ia is already referenced by psref_ia */
333 		ia = ifatoia(ifa);
334 
335 		ifp = ia->ia_ifp;
336 		mtu = ifp->if_mtu;
337 		ip->ip_ttl = 1;
338 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
339 	} else if (((IN_MULTICAST(ip->ip_dst.s_addr) ||
340 	    ip->ip_dst.s_addr == INADDR_BROADCAST) ||
341 	    (flags & IP_ROUTETOIFINDEX)) &&
342 	    imo != NULL && imo->imo_multicast_if_index != 0) {
343 		ifp = mifp = if_get_byindex(imo->imo_multicast_if_index, &psref);
344 		if (ifp == NULL) {
345 			IP_STATINC(IP_STAT_NOROUTE);
346 			error = ENETUNREACH;
347 			goto bad;
348 		}
349 		mtu = ifp->if_mtu;
350 		ia = in_get_ia_from_ifp_psref(ifp, &psref_ia);
351 		if (ia == NULL) {
352 			IP_STATINC(IP_STAT_IFNOADDR);
353 			error = EADDRNOTAVAIL;
354 			goto bad;
355 		}
356 		if (IN_MULTICAST(ip->ip_dst.s_addr) ||
357 		    ip->ip_dst.s_addr == INADDR_BROADCAST) {
358 			isbroadcast = 0;
359 		} else {
360 			/* IP_ROUTETOIFINDEX */
361 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
362 			if ((isbroadcast == 0) && ((ifp->if_flags &
363 			    (IFF_LOOPBACK | IFF_POINTOPOINT)) == 0) &&
364 			    (in_direct(dst->sin_addr, ifp) == 0)) {
365 				/* gateway address required */
366 				if (rt == NULL)
367 					rt = rtcache_init(ro);
368 				if (rt == NULL || rt->rt_ifp != ifp) {
369 					IP_STATINC(IP_STAT_NOROUTE);
370 					error = EHOSTUNREACH;
371 					goto bad;
372 				}
373 				rt->rt_use++;
374 				if (rt->rt_flags & RTF_GATEWAY)
375 					dst = satosin(rt->rt_gateway);
376 				if (rt->rt_flags & RTF_HOST)
377 					isbroadcast =
378 					    rt->rt_flags & RTF_BROADCAST;
379 			}
380 		}
381 	} else {
382 		if (rt == NULL)
383 			rt = rtcache_init(ro);
384 		if (rt == NULL) {
385 			IP_STATINC(IP_STAT_NOROUTE);
386 			error = EHOSTUNREACH;
387 			goto bad;
388 		}
389 		if (ifa_is_destroying(rt->rt_ifa)) {
390 			rtcache_unref(rt, ro);
391 			rt = NULL;
392 			IP_STATINC(IP_STAT_NOROUTE);
393 			error = EHOSTUNREACH;
394 			goto bad;
395 		}
396 		ifa_acquire(rt->rt_ifa, &psref_ia);
397 		ia = ifatoia(rt->rt_ifa);
398 		ifp = rt->rt_ifp;
399 		if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
400 			mtu = ifp->if_mtu;
401 		rt->rt_use++;
402 		if (rt->rt_flags & RTF_GATEWAY)
403 			dst = satosin(rt->rt_gateway);
404 		if (rt->rt_flags & RTF_HOST)
405 			isbroadcast = rt->rt_flags & RTF_BROADCAST;
406 		else
407 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
408 	}
409 	rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
410 
411 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
412 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
413 		bool inmgroup;
414 
415 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
416 		    M_BCAST : M_MCAST;
417 		/*
418 		 * See if the caller provided any multicast options
419 		 */
420 		if (imo != NULL)
421 			ip->ip_ttl = imo->imo_multicast_ttl;
422 		else
423 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
424 
425 		/*
426 		 * if we don't know the outgoing ifp yet, we can't generate
427 		 * output
428 		 */
429 		if (!ifp) {
430 			IP_STATINC(IP_STAT_NOROUTE);
431 			error = ENETUNREACH;
432 			goto bad;
433 		}
434 
435 		/*
436 		 * If the packet is multicast or broadcast, confirm that
437 		 * the outgoing interface can transmit it.
438 		 */
439 		if (((m->m_flags & M_MCAST) &&
440 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
441 		    ((m->m_flags & M_BCAST) &&
442 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
443 			IP_STATINC(IP_STAT_NOROUTE);
444 			error = ENETUNREACH;
445 			goto bad;
446 		}
447 		/*
448 		 * If source address not specified yet, use an address
449 		 * of outgoing interface.
450 		 */
451 		if (in_nullhost(ip->ip_src)) {
452 			struct in_ifaddr *xia;
453 			struct ifaddr *xifa;
454 			struct psref _psref;
455 
456 			xia = in_get_ia_from_ifp_psref(ifp, &_psref);
457 			if (!xia) {
458 				IP_STATINC(IP_STAT_IFNOADDR);
459 				error = EADDRNOTAVAIL;
460 				goto bad;
461 			}
462 			xifa = &xia->ia_ifa;
463 			if (xifa->ifa_getifa != NULL) {
464 				ia4_release(xia, &_psref);
465 				/* FIXME ifa_getifa is NOMPSAFE */
466 				xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
467 				if (xia == NULL) {
468 					IP_STATINC(IP_STAT_IFNOADDR);
469 					error = EADDRNOTAVAIL;
470 					goto bad;
471 				}
472 				ia4_acquire(xia, &_psref);
473 			}
474 			ip->ip_src = xia->ia_addr.sin_addr;
475 			ia4_release(xia, &_psref);
476 		}
477 
478 		inmgroup = in_multi_group(ip->ip_dst, ifp, flags);
479 		if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) {
480 			/*
481 			 * If we belong to the destination multicast group
482 			 * on the outgoing interface, and the caller did not
483 			 * forbid loopback, loop back a copy.
484 			 */
485 			ip_mloopback(ifp, m, &udst.sin);
486 		}
487 #ifdef MROUTING
488 		else {
489 			/*
490 			 * If we are acting as a multicast router, perform
491 			 * multicast forwarding as if the packet had just
492 			 * arrived on the interface to which we are about
493 			 * to send.  The multicast forwarding function
494 			 * recursively calls this function, using the
495 			 * IP_FORWARDING flag to prevent infinite recursion.
496 			 *
497 			 * Multicasts that are looped back by ip_mloopback(),
498 			 * above, will be forwarded by the ip_input() routine,
499 			 * if necessary.
500 			 */
501 			extern struct socket *ip_mrouter;
502 
503 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
504 				if (ip_mforward(m, ifp) != 0) {
505 					m_freem(m);
506 					goto done;
507 				}
508 			}
509 		}
510 #endif
511 		/*
512 		 * Multicasts with a time-to-live of zero may be looped-
513 		 * back, above, but must not be transmitted on a network.
514 		 * Also, multicasts addressed to the loopback interface
515 		 * are not sent -- the above call to ip_mloopback() will
516 		 * loop back a copy if this host actually belongs to the
517 		 * destination group on the loopback interface.
518 		 */
519 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
520 			IP_STATINC(IP_STAT_ODROPPED);
521 			m_freem(m);
522 			goto done;
523 		}
524 		goto sendit;
525 	}
526 
527 	/*
528 	 * If source address not specified yet, use address
529 	 * of outgoing interface.
530 	 */
531 	if (in_nullhost(ip->ip_src)) {
532 		struct ifaddr *xifa;
533 
534 		xifa = &ia->ia_ifa;
535 		if (xifa->ifa_getifa != NULL) {
536 			ia4_release(ia, &psref_ia);
537 			/* FIXME ifa_getifa is NOMPSAFE */
538 			ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
539 			if (ia == NULL) {
540 				error = EADDRNOTAVAIL;
541 				goto bad;
542 			}
543 			ia4_acquire(ia, &psref_ia);
544 		}
545 		ip->ip_src = ia->ia_addr.sin_addr;
546 	}
547 
548 	/*
549 	 * Packets with Class-D address as source are not valid per
550 	 * RFC1112.
551 	 */
552 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
553 		IP_STATINC(IP_STAT_ODROPPED);
554 		error = EADDRNOTAVAIL;
555 		goto bad;
556 	}
557 
558 	/*
559 	 * Look for broadcast address and verify user is allowed to
560 	 * send such a packet.
561 	 */
562 	if (isbroadcast) {
563 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
564 			IP_STATINC(IP_STAT_BCASTDENIED);
565 			error = EADDRNOTAVAIL;
566 			goto bad;
567 		}
568 		if ((flags & IP_ALLOWBROADCAST) == 0) {
569 			IP_STATINC(IP_STAT_BCASTDENIED);
570 			error = EACCES;
571 			goto bad;
572 		}
573 		/* don't allow broadcast messages to be fragmented */
574 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
575 			IP_STATINC(IP_STAT_BCASTDENIED);
576 			error = EMSGSIZE;
577 			goto bad;
578 		}
579 		m->m_flags |= M_BCAST;
580 	} else
581 		m->m_flags &= ~M_BCAST;
582 
583 sendit:
584 	if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
585 		if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
586 			ip->ip_id = 0;
587 		} else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
588 			ip->ip_id = ip_newid(ia);
589 		} else {
590 			/*
591 			 * TSO capable interfaces (typically?) increment
592 			 * ip_id for each segment.
593 			 * "allocate" enough ids here to increase the chance
594 			 * for them to be unique.
595 			 *
596 			 * note that the following calculation is not
597 			 * needed to be precise.  wasting some ip_id is fine.
598 			 */
599 
600 			unsigned int segsz = m->m_pkthdr.segsz;
601 			unsigned int datasz = ntohs(ip->ip_len) - hlen;
602 			unsigned int num = howmany(datasz, segsz);
603 
604 			ip->ip_id = ip_newid_range(ia, num);
605 		}
606 	}
607 	if (ia != NULL) {
608 		ia4_release(ia, &psref_ia);
609 		ia = NULL;
610 	}
611 
612 	/*
613 	 * If we're doing Path MTU Discovery, we need to set DF unless
614 	 * the route's MTU is locked.
615 	 */
616 	if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) {
617 		ip->ip_off |= htons(IP_DF);
618 	}
619 
620 #ifdef IPSEC
621 	if (ipsec_used) {
622 		bool ipsec_done = false;
623 		bool count_drop = false;
624 
625 		/* Perform IPsec processing, if any. */
626 		error = ipsec4_output(m, inp, flags, &mtu, &natt_frag,
627 		    &ipsec_done, &count_drop);
628 		if (count_drop)
629 			IP_STATINC(IP_STAT_IPSECDROP_OUT);
630 		if (error || ipsec_done)
631 			goto done;
632 	}
633 
634 	if (!ipsec_used || !natt_frag)
635 #endif
636 	{
637 		/*
638 		 * Run through list of hooks for output packets.
639 		 */
640 		error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT);
641 		if (error || m == NULL) {
642 			IP_STATINC(IP_STAT_PFILDROP_OUT);
643 			goto done;
644 		}
645 	}
646 
647 	ip = mtod(m, struct ip *);
648 	hlen = ip->ip_hl << 2;
649 
650 	m->m_pkthdr.csum_data |= hlen << 16;
651 
652 	/*
653 	 * search for the source address structure to
654 	 * maintain output statistics, and verify address
655 	 * validity
656 	 */
657 	KASSERT(ia == NULL);
658 	sockaddr_in_init(&usrc.sin, &ip->ip_src, 0);
659 	ifa = ifaof_ifpforaddr_psref(&usrc.sa, ifp, &psref_ia);
660 	if (ifa != NULL)
661 		ia = ifatoia(ifa);
662 
663 	/*
664 	 * Ensure we only send from a valid address.
665 	 * A NULL address is valid because the packet could be
666 	 * generated from a packet filter.
667 	 */
668 	if (ia != NULL && (flags & IP_FORWARDING) == 0 &&
669 	    (error = ip_ifaddrvalid(ia)) != 0)
670 	{
671 		ARPLOG(LOG_ERR,
672 		    "refusing to send from invalid address %s (pid %d)\n",
673 		    ARPLOGADDR(&ip->ip_src), curproc->p_pid);
674 		IP_STATINC(IP_STAT_ODROPPED);
675 		if (error == 1)
676 			/*
677 			 * Address exists, but is tentative or detached.
678 			 * We can't send from it because it's invalid,
679 			 * so we drop the packet.
680 			 */
681 			error = 0;
682 		else
683 			error = EADDRNOTAVAIL;
684 		goto bad;
685 	}
686 
687 	/* Maybe skip checksums on loopback interfaces. */
688 	if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
689 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
690 	}
691 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
692 
693 	/* Need to fragment the packet */
694 	if (ntohs(ip->ip_len) > mtu &&
695 	    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
696 		goto fragment;
697 	}
698 
699 #if IFA_STATS
700 	if (ia)
701 		ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
702 #endif
703 	/*
704 	 * Always initialize the sum to 0!  Some HW assisted
705 	 * checksumming requires this.
706 	 */
707 	ip->ip_sum = 0;
708 
709 	if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
710 		/*
711 		 * Perform any checksums that the hardware can't do
712 		 * for us.
713 		 *
714 		 * XXX Does any hardware require the {th,uh}_sum
715 		 * XXX fields to be 0?
716 		 */
717 		if (sw_csum & M_CSUM_IPv4) {
718 			KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
719 			ip->ip_sum = in_cksum(m, hlen);
720 			m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
721 		}
722 		if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
723 			if (IN_NEED_CHECKSUM(ifp,
724 			    sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
725 				in_undefer_cksum_tcpudp(m);
726 			}
727 			m->m_pkthdr.csum_flags &=
728 			    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
729 		}
730 	}
731 
732 	sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst);
733 
734 	/* Send it */
735 	if (__predict_false(sw_csum & M_CSUM_TSOv4)) {
736 		/*
737 		 * TSO4 is required by a packet, but disabled for
738 		 * the interface.
739 		 */
740 		error = ip_tso_output(ifp, m, sa, rt);
741 	} else
742 		error = ip_if_output(ifp, m, sa, rt);
743 	goto done;
744 
745 fragment:
746 	/*
747 	 * We can't use HW checksumming if we're about to fragment the packet.
748 	 *
749 	 * XXX Some hardware can do this.
750 	 */
751 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
752 		if (IN_NEED_CHECKSUM(ifp,
753 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
754 			in_undefer_cksum_tcpudp(m);
755 		}
756 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
757 	}
758 
759 	/*
760 	 * Too large for interface; fragment if possible.
761 	 * Must be able to put at least 8 bytes per fragment.
762 	 */
763 	if (ntohs(ip->ip_off) & IP_DF) {
764 		if (flags & IP_RETURNMTU) {
765 			KASSERT(inp != NULL);
766 			inp->inp_errormtu = mtu;
767 		}
768 		error = EMSGSIZE;
769 		IP_STATINC(IP_STAT_CANTFRAG);
770 		goto bad;
771 	}
772 
773 	error = ip_fragment(m, ifp, mtu);
774 	if (error) {
775 		m = NULL;
776 		goto bad;
777 	}
778 
779 	for (; m; m = m0) {
780 		m0 = m->m_nextpkt;
781 		m->m_nextpkt = NULL;
782 		if (error) {
783 			m_freem(m);
784 			continue;
785 		}
786 #if IFA_STATS
787 		if (ia)
788 			ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
789 #endif
790 		/*
791 		 * If we get there, the packet has not been handled by
792 		 * IPsec whereas it should have. Now that it has been
793 		 * fragmented, re-inject it in ip_output so that IPsec
794 		 * processing can occur.
795 		 */
796 		if (natt_frag) {
797 			error = ip_output(m, opt, NULL,
798 			    flags | IP_RAWOUTPUT | IP_NOIPNEWID,
799 			    imo, inp);
800 		} else {
801 			KASSERT((m->m_pkthdr.csum_flags &
802 			    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
803 			error = ip_if_output(ifp, m, (m->m_flags & M_MCAST) ?
804 			    sintocsa(rdst) : sintocsa(dst), rt);
805 		}
806 	}
807 	if (error == 0) {
808 		IP_STATINC(IP_STAT_FRAGMENTED);
809 	}
810 
811 done:
812 	ia4_release(ia, &psref_ia);
813 	rtcache_unref(rt, ro);
814 	if (ro == &iproute) {
815 		rtcache_free(&iproute);
816 	}
817 	if (mifp != NULL) {
818 		if_put(mifp, &psref);
819 	}
820 	if (bind_need_restore)
821 		curlwp_bindx(bound);
822 	return error;
823 
824 bad:
825 	m_freem(m);
826 	goto done;
827 }
828 
829 int
830 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
831 {
832 	struct ip *ip, *mhip;
833 	struct mbuf *m0;
834 	int len, hlen, off;
835 	int mhlen, firstlen;
836 	struct mbuf **mnext;
837 	int sw_csum = m->m_pkthdr.csum_flags;
838 	int fragments = 0;
839 	int error = 0;
840 	int ipoff, ipflg;
841 
842 	ip = mtod(m, struct ip *);
843 	hlen = ip->ip_hl << 2;
844 
845 	/* Preserve the offset and flags. */
846 	ipoff = ntohs(ip->ip_off) & IP_OFFMASK;
847 	ipflg = ntohs(ip->ip_off) & (IP_RF|IP_DF|IP_MF);
848 
849 	if (ifp != NULL)
850 		sw_csum &= ~ifp->if_csum_flags_tx;
851 
852 	len = (mtu - hlen) &~ 7;
853 	if (len < 8) {
854 		IP_STATINC(IP_STAT_CANTFRAG);
855 		m_freem(m);
856 		return EMSGSIZE;
857 	}
858 
859 	firstlen = len;
860 	mnext = &m->m_nextpkt;
861 
862 	/*
863 	 * Loop through length of segment after first fragment,
864 	 * make new header and copy data of each part and link onto chain.
865 	 */
866 	m0 = m;
867 	mhlen = sizeof(struct ip);
868 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
869 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
870 		if (m == NULL) {
871 			error = ENOBUFS;
872 			IP_STATINC(IP_STAT_ODROPPED);
873 			goto sendorfree;
874 		}
875 		MCLAIM(m, m0->m_owner);
876 
877 		*mnext = m;
878 		mnext = &m->m_nextpkt;
879 
880 		m->m_data += max_linkhdr;
881 		mhip = mtod(m, struct ip *);
882 		*mhip = *ip;
883 
884 		/* we must inherit the flags */
885 		m->m_flags |= m0->m_flags & M_COPYFLAGS;
886 
887 		if (hlen > sizeof(struct ip)) {
888 			mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
889 			mhip->ip_hl = mhlen >> 2;
890 		}
891 		m->m_len = mhlen;
892 
893 		mhip->ip_off = ((off - hlen) >> 3) + ipoff;
894 		mhip->ip_off |= ipflg;
895 		if (off + len >= ntohs(ip->ip_len))
896 			len = ntohs(ip->ip_len) - off;
897 		else
898 			mhip->ip_off |= IP_MF;
899 		HTONS(mhip->ip_off);
900 
901 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
902 		m->m_next = m_copym(m0, off, len, M_DONTWAIT);
903 		if (m->m_next == NULL) {
904 			error = ENOBUFS;
905 			IP_STATINC(IP_STAT_ODROPPED);
906 			goto sendorfree;
907 		}
908 
909 		m->m_pkthdr.len = mhlen + len;
910 		m_reset_rcvif(m);
911 
912 		mhip->ip_sum = 0;
913 		KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
914 		if (sw_csum & M_CSUM_IPv4) {
915 			mhip->ip_sum = in_cksum(m, mhlen);
916 		} else {
917 			/*
918 			 * checksum is hw-offloaded or not necessary.
919 			 */
920 			m->m_pkthdr.csum_flags |=
921 			    m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
922 			m->m_pkthdr.csum_data |= mhlen << 16;
923 			KASSERT(!(ifp != NULL &&
924 			    IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
925 			    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
926 		}
927 		IP_STATINC(IP_STAT_OFRAGMENTS);
928 		fragments++;
929 	}
930 
931 	/*
932 	 * Update first fragment by trimming what's been copied out
933 	 * and updating header, then send each fragment (in order).
934 	 */
935 	m = m0;
936 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
937 	m->m_pkthdr.len = hlen + firstlen;
938 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
939 	ip->ip_off |= htons(IP_MF);
940 	ip->ip_sum = 0;
941 	if (sw_csum & M_CSUM_IPv4) {
942 		ip->ip_sum = in_cksum(m, hlen);
943 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
944 	} else {
945 		/*
946 		 * checksum is hw-offloaded or not necessary.
947 		 */
948 		KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
949 		    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
950 		KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
951 		    sizeof(struct ip));
952 	}
953 
954 sendorfree:
955 	/*
956 	 * If there is no room for all the fragments, don't queue
957 	 * any of them.
958 	 */
959 	if (ifp != NULL) {
960 		IFQ_LOCK(&ifp->if_snd);
961 		if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
962 		    error == 0) {
963 			error = ENOBUFS;
964 			IP_STATINC(IP_STAT_ODROPPED);
965 			IFQ_INC_DROPS(&ifp->if_snd);
966 		}
967 		IFQ_UNLOCK(&ifp->if_snd);
968 	}
969 	if (error) {
970 		for (m = m0; m; m = m0) {
971 			m0 = m->m_nextpkt;
972 			m->m_nextpkt = NULL;
973 			m_freem(m);
974 		}
975 	}
976 
977 	return error;
978 }
979 
980 /*
981  * Determine the maximum length of the options to be inserted;
982  * we would far rather allocate too much space rather than too little.
983  */
984 u_int
985 ip_optlen(struct inpcb *inp)
986 {
987 	struct mbuf *m = inp->inp_options;
988 
989 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
990 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
991 	}
992 	return 0;
993 }
994 
995 /*
996  * Insert IP options into preformed packet.
997  * Adjust IP destination as required for IP source routing,
998  * as indicated by a non-zero in_addr at the start of the options.
999  */
1000 static struct mbuf *
1001 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1002 {
1003 	struct ipoption *p = mtod(opt, struct ipoption *);
1004 	struct mbuf *n;
1005 	struct ip *ip = mtod(m, struct ip *);
1006 	unsigned optlen;
1007 
1008 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1009 	KASSERT(optlen % 4 == 0);
1010 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
1011 		return m;		/* XXX should fail */
1012 	if (!in_nullhost(p->ipopt_dst))
1013 		ip->ip_dst = p->ipopt_dst;
1014 	if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
1015 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1016 		if (n == NULL)
1017 			return m;
1018 		MCLAIM(n, m->m_owner);
1019 		m_move_pkthdr(n, m);
1020 		m->m_len -= sizeof(struct ip);
1021 		m->m_data += sizeof(struct ip);
1022 		n->m_next = m;
1023 		n->m_len = optlen + sizeof(struct ip);
1024 		n->m_data += max_linkhdr;
1025 		memcpy(mtod(n, void *), ip, sizeof(struct ip));
1026 		m = n;
1027 	} else {
1028 		m->m_data -= optlen;
1029 		m->m_len += optlen;
1030 		memmove(mtod(m, void *), ip, sizeof(struct ip));
1031 	}
1032 	m->m_pkthdr.len += optlen;
1033 	ip = mtod(m, struct ip *);
1034 	memcpy(ip + 1, p->ipopt_list, optlen);
1035 	*phlen = sizeof(struct ip) + optlen;
1036 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
1037 	return m;
1038 }
1039 
1040 /*
1041  * Copy options from ipsrc to ipdst, omitting those not copied during
1042  * fragmentation.
1043  */
1044 int
1045 ip_optcopy(struct ip *ipsrc, struct ip *ipdst)
1046 {
1047 	u_char *cp, *dp;
1048 	int opt, optlen, cnt;
1049 
1050 	cp = (u_char *)(ipsrc + 1);
1051 	dp = (u_char *)(ipdst + 1);
1052 	cnt = (ipsrc->ip_hl << 2) - sizeof(struct ip);
1053 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1054 		opt = cp[0];
1055 		if (opt == IPOPT_EOL)
1056 			break;
1057 		if (opt == IPOPT_NOP) {
1058 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1059 			*dp++ = IPOPT_NOP;
1060 			optlen = 1;
1061 			continue;
1062 		}
1063 
1064 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
1065 		optlen = cp[IPOPT_OLEN];
1066 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
1067 
1068 		/* Invalid lengths should have been caught by ip_dooptions. */
1069 		if (optlen > cnt)
1070 			optlen = cnt;
1071 		if (IPOPT_COPIED(opt)) {
1072 			bcopy((void *)cp, (void *)dp, (unsigned)optlen);
1073 			dp += optlen;
1074 		}
1075 	}
1076 
1077 	for (optlen = dp - (u_char *)(ipdst+1); optlen & 0x3; optlen++) {
1078 		*dp++ = IPOPT_EOL;
1079 	}
1080 
1081 	return optlen;
1082 }
1083 
1084 /*
1085  * IP socket option processing.
1086  */
1087 int
1088 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1089 {
1090 	struct inpcb *inp = sotoinpcb(so);
1091 	struct ip *ip = &inp->inp_ip;
1092 	int inpflags = inp->inp_flags;
1093 	int optval = 0, error = 0;
1094 	struct in_pktinfo pktinfo;
1095 
1096 	KASSERT(solocked(so));
1097 
1098 	if (sopt->sopt_level != IPPROTO_IP) {
1099 		if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
1100 			return 0;
1101 		return ENOPROTOOPT;
1102 	}
1103 
1104 	switch (op) {
1105 	case PRCO_SETOPT:
1106 		switch (sopt->sopt_name) {
1107 		case IP_OPTIONS:
1108 #ifdef notyet
1109 		case IP_RETOPTS:
1110 #endif
1111 			error = ip_pcbopts(inp, sopt);
1112 			break;
1113 
1114 		case IP_TOS:
1115 		case IP_TTL:
1116 		case IP_MINTTL:
1117 		case IP_RECVOPTS:
1118 		case IP_RECVRETOPTS:
1119 		case IP_RECVDSTADDR:
1120 		case IP_RECVIF:
1121 		case IP_RECVPKTINFO:
1122 		case IP_RECVTTL:
1123 		case IP_BINDANY:
1124 			error = sockopt_getint(sopt, &optval);
1125 			if (error)
1126 				break;
1127 
1128 			switch (sopt->sopt_name) {
1129 			case IP_TOS:
1130 				ip->ip_tos = optval;
1131 				break;
1132 
1133 			case IP_TTL:
1134 				ip->ip_ttl = optval;
1135 				break;
1136 
1137 			case IP_MINTTL:
1138 				if (optval > 0 && optval <= MAXTTL)
1139 					inp->inp_ip_minttl = optval;
1140 				else
1141 					error = EINVAL;
1142 				break;
1143 #define	OPTSET(bit) \
1144 	if (optval) \
1145 		inpflags |= bit; \
1146 	else \
1147 		inpflags &= ~bit;
1148 
1149 			case IP_RECVOPTS:
1150 				OPTSET(INP_RECVOPTS);
1151 				break;
1152 
1153 			case IP_RECVPKTINFO:
1154 				OPTSET(INP_RECVPKTINFO);
1155 				break;
1156 
1157 			case IP_RECVRETOPTS:
1158 				OPTSET(INP_RECVRETOPTS);
1159 				break;
1160 
1161 			case IP_RECVDSTADDR:
1162 				OPTSET(INP_RECVDSTADDR);
1163 				break;
1164 
1165 			case IP_RECVIF:
1166 				OPTSET(INP_RECVIF);
1167 				break;
1168 
1169 			case IP_RECVTTL:
1170 				OPTSET(INP_RECVTTL);
1171 				break;
1172 
1173 			case IP_BINDANY:
1174 				error = kauth_authorize_network(
1175 				    kauth_cred_get(), KAUTH_NETWORK_BIND,
1176 				    KAUTH_REQ_NETWORK_BIND_ANYADDR, so,
1177 				    NULL, NULL);
1178 				if (error == 0) {
1179 					OPTSET(INP_BINDANY);
1180 				}
1181 				break;
1182 			}
1183 			break;
1184 		case IP_PKTINFO:
1185 			error = sockopt_getint(sopt, &optval);
1186 			if (!error) {
1187 				/* Linux compatibility */
1188 				OPTSET(INP_RECVPKTINFO);
1189 				break;
1190 			}
1191 			error = sockopt_get(sopt, &pktinfo, sizeof(pktinfo));
1192 			if (error)
1193 				break;
1194 
1195 			if (pktinfo.ipi_ifindex == 0) {
1196 				inp->inp_prefsrcip = pktinfo.ipi_addr;
1197 				break;
1198 			}
1199 
1200 			/* Solaris compatibility */
1201 			struct ifnet *ifp;
1202 			struct in_ifaddr *ia;
1203 			int s;
1204 
1205 			/* pick up primary address */
1206 			s = pserialize_read_enter();
1207 			ifp = if_byindex(pktinfo.ipi_ifindex);
1208 			if (ifp == NULL) {
1209 				pserialize_read_exit(s);
1210 				error = EADDRNOTAVAIL;
1211 				break;
1212 			}
1213 			ia = in_get_ia_from_ifp(ifp);
1214 			if (ia == NULL) {
1215 				pserialize_read_exit(s);
1216 				error = EADDRNOTAVAIL;
1217 				break;
1218 			}
1219 			inp->inp_prefsrcip = IA_SIN(ia)->sin_addr;
1220 			pserialize_read_exit(s);
1221 			break;
1222 		break;
1223 #undef OPTSET
1224 
1225 		case IP_MULTICAST_IF:
1226 		case IP_MULTICAST_TTL:
1227 		case IP_MULTICAST_LOOP:
1228 		case IP_ADD_MEMBERSHIP:
1229 		case IP_DROP_MEMBERSHIP:
1230 			error = ip_setmoptions(&inp->inp_moptions, sopt);
1231 			break;
1232 
1233 		case IP_PORTRANGE:
1234 			error = sockopt_getint(sopt, &optval);
1235 			if (error)
1236 				break;
1237 
1238 			switch (optval) {
1239 			case IP_PORTRANGE_DEFAULT:
1240 			case IP_PORTRANGE_HIGH:
1241 				inpflags &= ~(INP_LOWPORT);
1242 				break;
1243 
1244 			case IP_PORTRANGE_LOW:
1245 				inpflags |= INP_LOWPORT;
1246 				break;
1247 
1248 			default:
1249 				error = EINVAL;
1250 				break;
1251 			}
1252 			break;
1253 
1254 		case IP_PORTALGO:
1255 			error = sockopt_getint(sopt, &optval);
1256 			if (error)
1257 				break;
1258 
1259 			error = portalgo_algo_index_select(
1260 			    (struct inpcb_hdr *)inp, optval);
1261 			break;
1262 
1263 #if defined(IPSEC)
1264 		case IP_IPSEC_POLICY:
1265 			if (ipsec_enabled) {
1266 				error = ipsec_set_policy(inp,
1267 				    sopt->sopt_data, sopt->sopt_size,
1268 				    curlwp->l_cred);
1269 			} else
1270 				error = ENOPROTOOPT;
1271 			break;
1272 #endif /* IPSEC */
1273 
1274 		default:
1275 			error = ENOPROTOOPT;
1276 			break;
1277 		}
1278 		break;
1279 
1280 	case PRCO_GETOPT:
1281 		switch (sopt->sopt_name) {
1282 		case IP_OPTIONS:
1283 		case IP_RETOPTS: {
1284 			struct mbuf *mopts = inp->inp_options;
1285 
1286 			if (mopts) {
1287 				struct mbuf *m;
1288 
1289 				m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
1290 				if (m == NULL) {
1291 					error = ENOBUFS;
1292 					break;
1293 				}
1294 				error = sockopt_setmbuf(sopt, m);
1295 			}
1296 			break;
1297 		}
1298 		case IP_TOS:
1299 		case IP_TTL:
1300 		case IP_MINTTL:
1301 		case IP_RECVOPTS:
1302 		case IP_RECVRETOPTS:
1303 		case IP_RECVDSTADDR:
1304 		case IP_RECVIF:
1305 		case IP_RECVPKTINFO:
1306 		case IP_RECVTTL:
1307 		case IP_ERRORMTU:
1308 		case IP_BINDANY:
1309 			switch (sopt->sopt_name) {
1310 			case IP_TOS:
1311 				optval = ip->ip_tos;
1312 				break;
1313 
1314 			case IP_TTL:
1315 				optval = ip->ip_ttl;
1316 				break;
1317 
1318 			case IP_MINTTL:
1319 				optval = inp->inp_ip_minttl;
1320 				break;
1321 
1322 			case IP_ERRORMTU:
1323 				optval = inp->inp_errormtu;
1324 				break;
1325 
1326 #define	OPTBIT(bit)	(inpflags & bit ? 1 : 0)
1327 
1328 			case IP_RECVOPTS:
1329 				optval = OPTBIT(INP_RECVOPTS);
1330 				break;
1331 
1332 			case IP_RECVPKTINFO:
1333 				optval = OPTBIT(INP_RECVPKTINFO);
1334 				break;
1335 
1336 			case IP_RECVRETOPTS:
1337 				optval = OPTBIT(INP_RECVRETOPTS);
1338 				break;
1339 
1340 			case IP_RECVDSTADDR:
1341 				optval = OPTBIT(INP_RECVDSTADDR);
1342 				break;
1343 
1344 			case IP_RECVIF:
1345 				optval = OPTBIT(INP_RECVIF);
1346 				break;
1347 
1348 			case IP_RECVTTL:
1349 				optval = OPTBIT(INP_RECVTTL);
1350 				break;
1351 
1352 			case IP_BINDANY:
1353 				optval = OPTBIT(INP_BINDANY);
1354 				break;
1355 			}
1356 			error = sockopt_setint(sopt, optval);
1357 			break;
1358 
1359 		case IP_PKTINFO:
1360 			switch (sopt->sopt_size) {
1361 			case sizeof(int):
1362 				/* Linux compatibility */
1363 				optval = OPTBIT(INP_RECVPKTINFO);
1364 				error = sockopt_setint(sopt, optval);
1365 				break;
1366 			case sizeof(struct in_pktinfo):
1367 				/* Solaris compatibility */
1368 				pktinfo.ipi_ifindex = 0;
1369 				pktinfo.ipi_addr = inp->inp_prefsrcip;
1370 				error = sockopt_set(sopt, &pktinfo,
1371 				    sizeof(pktinfo));
1372 				break;
1373 			default:
1374 				/*
1375 				 * While size is stuck at 0, and, later, if
1376 				 * the caller doesn't use an exactly sized
1377 				 * recipient for the data, default to Linux
1378 				 * compatibility
1379 				 */
1380 				optval = OPTBIT(INP_RECVPKTINFO);
1381 				error = sockopt_setint(sopt, optval);
1382 				break;
1383 			}
1384 			break;
1385 
1386 #if 0	/* defined(IPSEC) */
1387 		case IP_IPSEC_POLICY:
1388 		{
1389 			struct mbuf *m = NULL;
1390 
1391 			/* XXX this will return EINVAL as sopt is empty */
1392 			error = ipsec_get_policy(inp, sopt->sopt_data,
1393 			    sopt->sopt_size, &m);
1394 			if (error == 0)
1395 				error = sockopt_setmbuf(sopt, m);
1396 			break;
1397 		}
1398 #endif /*IPSEC*/
1399 
1400 		case IP_MULTICAST_IF:
1401 		case IP_MULTICAST_TTL:
1402 		case IP_MULTICAST_LOOP:
1403 		case IP_ADD_MEMBERSHIP:
1404 		case IP_DROP_MEMBERSHIP:
1405 			error = ip_getmoptions(inp->inp_moptions, sopt);
1406 			break;
1407 
1408 		case IP_PORTRANGE:
1409 			if (inpflags & INP_LOWPORT)
1410 				optval = IP_PORTRANGE_LOW;
1411 			else
1412 				optval = IP_PORTRANGE_DEFAULT;
1413 			error = sockopt_setint(sopt, optval);
1414 			break;
1415 
1416 		case IP_PORTALGO:
1417 			optval = inp->inp_portalgo;
1418 			error = sockopt_setint(sopt, optval);
1419 			break;
1420 
1421 		default:
1422 			error = ENOPROTOOPT;
1423 			break;
1424 		}
1425 		break;
1426 	}
1427 
1428 	if (!error) {
1429 		inp->inp_flags = inpflags;
1430 	}
1431 	return error;
1432 }
1433 
1434 static int
1435 ip_pktinfo_prepare(const struct inpcb *inp, const struct in_pktinfo *pktinfo,
1436     struct ip_pktopts *pktopts, int *flags, kauth_cred_t cred)
1437 {
1438 	struct ip_moptions *imo;
1439 	int error = 0;
1440 	bool addrset = false;
1441 
1442 	if (!in_nullhost(pktinfo->ipi_addr)) {
1443 		pktopts->ippo_laddr.sin_addr = pktinfo->ipi_addr;
1444 		/* EADDRNOTAVAIL? */
1445 		error = in_pcbbindableaddr(inp, &pktopts->ippo_laddr, cred);
1446 		if (error != 0)
1447 			return error;
1448 		addrset = true;
1449 	}
1450 
1451 	if (pktinfo->ipi_ifindex != 0) {
1452 		if (!addrset) {
1453 			struct ifnet *ifp;
1454 			struct in_ifaddr *ia;
1455 			int s;
1456 
1457 			/* pick up primary address */
1458 			s = pserialize_read_enter();
1459 			ifp = if_byindex(pktinfo->ipi_ifindex);
1460 			if (ifp == NULL) {
1461 				pserialize_read_exit(s);
1462 				return EADDRNOTAVAIL;
1463 			}
1464 			ia = in_get_ia_from_ifp(ifp);
1465 			if (ia == NULL) {
1466 				pserialize_read_exit(s);
1467 				return EADDRNOTAVAIL;
1468 			}
1469 			pktopts->ippo_laddr.sin_addr = IA_SIN(ia)->sin_addr;
1470 			pserialize_read_exit(s);
1471 		}
1472 
1473 		/*
1474 		 * If specified ipi_ifindex,
1475 		 * use copied or locally initialized ip_moptions.
1476 		 * Original ip_moptions must not be modified.
1477 		 */
1478 		imo = &pktopts->ippo_imobuf;	/* local buf in pktopts */
1479 		if (pktopts->ippo_imo != NULL) {
1480 			memcpy(imo, pktopts->ippo_imo, sizeof(*imo));
1481 		} else {
1482 			memset(imo, 0, sizeof(*imo));
1483 			imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1484 			imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1485 		}
1486 		imo->imo_multicast_if_index = pktinfo->ipi_ifindex;
1487 		pktopts->ippo_imo = imo;
1488 		*flags |= IP_ROUTETOIFINDEX;
1489 	}
1490 	return error;
1491 }
1492 
1493 /*
1494  * Set up IP outgoing packet options. Even if control is NULL,
1495  * pktopts->ippo_laddr and pktopts->ippo_imo are set and used.
1496  */
1497 int
1498 ip_setpktopts(struct mbuf *control, struct ip_pktopts *pktopts, int *flags,
1499     struct inpcb *inp, kauth_cred_t cred)
1500 {
1501 	struct cmsghdr *cm;
1502 	struct in_pktinfo pktinfo;
1503 	int error;
1504 
1505 	pktopts->ippo_imo = inp->inp_moptions;
1506 
1507 	struct in_addr *ia = in_nullhost(inp->inp_prefsrcip) ? &inp->inp_laddr :
1508 	    &inp->inp_prefsrcip;
1509 	sockaddr_in_init(&pktopts->ippo_laddr, ia, 0);
1510 
1511 	if (control == NULL)
1512 		return 0;
1513 
1514 	/*
1515 	 * XXX: Currently, we assume all the optional information is
1516 	 * stored in a single mbuf.
1517 	 */
1518 	if (control->m_next)
1519 		return EINVAL;
1520 
1521 	for (; control->m_len > 0;
1522 	    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1523 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1524 		cm = mtod(control, struct cmsghdr *);
1525 		if ((control->m_len < sizeof(*cm)) ||
1526 		    (cm->cmsg_len == 0) ||
1527 		    (cm->cmsg_len > control->m_len)) {
1528 			return EINVAL;
1529 		}
1530 		if (cm->cmsg_level != IPPROTO_IP)
1531 			continue;
1532 
1533 		switch (cm->cmsg_type) {
1534 		case IP_PKTINFO:
1535 			if (cm->cmsg_len != CMSG_LEN(sizeof(pktinfo)))
1536 				return EINVAL;
1537 			memcpy(&pktinfo, CMSG_DATA(cm), sizeof(pktinfo));
1538 			error = ip_pktinfo_prepare(inp, &pktinfo, pktopts,
1539 			    flags, cred);
1540 			if (error)
1541 				return error;
1542 			break;
1543 		case IP_SENDSRCADDR: /* FreeBSD compatibility */
1544 			if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_addr)))
1545 				return EINVAL;
1546 			pktinfo.ipi_ifindex = 0;
1547 			pktinfo.ipi_addr =
1548 			    ((struct in_pktinfo *)CMSG_DATA(cm))->ipi_addr;
1549 			error = ip_pktinfo_prepare(inp, &pktinfo, pktopts,
1550 			    flags, cred);
1551 			if (error)
1552 				return error;
1553 			break;
1554 		default:
1555 			return ENOPROTOOPT;
1556 		}
1557 	}
1558 	return 0;
1559 }
1560 
1561 /*
1562  * Set up IP options in pcb for insertion in output packets.
1563  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1564  * with destination address if source routed.
1565  */
1566 static int
1567 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
1568 {
1569 	struct mbuf *m;
1570 	const u_char *cp;
1571 	u_char *dp;
1572 	int cnt;
1573 
1574 	KASSERT(inp_locked(inp));
1575 
1576 	/* Turn off any old options. */
1577 	if (inp->inp_options) {
1578 		m_free(inp->inp_options);
1579 	}
1580 	inp->inp_options = NULL;
1581 	if ((cnt = sopt->sopt_size) == 0) {
1582 		/* Only turning off any previous options. */
1583 		return 0;
1584 	}
1585 	cp = sopt->sopt_data;
1586 
1587 	if (cnt % 4) {
1588 		/* Must be 4-byte aligned, because there's no padding. */
1589 		return EINVAL;
1590 	}
1591 
1592 	m = m_get(M_DONTWAIT, MT_SOOPTS);
1593 	if (m == NULL)
1594 		return ENOBUFS;
1595 
1596 	dp = mtod(m, u_char *);
1597 	memset(dp, 0, sizeof(struct in_addr));
1598 	dp += sizeof(struct in_addr);
1599 	m->m_len = sizeof(struct in_addr);
1600 
1601 	/*
1602 	 * IP option list according to RFC791. Each option is of the form
1603 	 *
1604 	 *	[optval] [olen] [(olen - 2) data bytes]
1605 	 *
1606 	 * We validate the list and copy options to an mbuf for prepending
1607 	 * to data packets. The IP first-hop destination address will be
1608 	 * stored before actual options and is zero if unset.
1609 	 */
1610 	while (cnt > 0) {
1611 		uint8_t optval, olen, offset;
1612 
1613 		optval = cp[IPOPT_OPTVAL];
1614 
1615 		if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
1616 			olen = 1;
1617 		} else {
1618 			if (cnt < IPOPT_OLEN + 1)
1619 				goto bad;
1620 
1621 			olen = cp[IPOPT_OLEN];
1622 			if (olen < IPOPT_OLEN + 1 || olen > cnt)
1623 				goto bad;
1624 		}
1625 
1626 		if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
1627 			/*
1628 			 * user process specifies route as:
1629 			 *	->A->B->C->D
1630 			 * D must be our final destination (but we can't
1631 			 * check that since we may not have connected yet).
1632 			 * A is first hop destination, which doesn't appear in
1633 			 * actual IP option, but is stored before the options.
1634 			 */
1635 			if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
1636 				goto bad;
1637 
1638 			offset = cp[IPOPT_OFFSET];
1639 			memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
1640 			    sizeof(struct in_addr));
1641 
1642 			cp += sizeof(struct in_addr);
1643 			cnt -= sizeof(struct in_addr);
1644 			olen -= sizeof(struct in_addr);
1645 
1646 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1647 				goto bad;
1648 
1649 			memcpy(dp, cp, olen);
1650 			dp[IPOPT_OPTVAL] = optval;
1651 			dp[IPOPT_OLEN] = olen;
1652 			dp[IPOPT_OFFSET] = offset;
1653 			break;
1654 		} else {
1655 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1656 				goto bad;
1657 
1658 			memcpy(dp, cp, olen);
1659 			break;
1660 		}
1661 
1662 		dp += olen;
1663 		m->m_len += olen;
1664 
1665 		if (optval == IPOPT_EOL)
1666 			break;
1667 
1668 		cp += olen;
1669 		cnt -= olen;
1670 	}
1671 
1672 	inp->inp_options = m;
1673 	return 0;
1674 
1675 bad:
1676 	(void)m_free(m);
1677 	return EINVAL;
1678 }
1679 
1680 /*
1681  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1682  * Must be called in a pserialize critical section.
1683  */
1684 static struct ifnet *
1685 ip_multicast_if(struct in_addr *a, int *ifindexp)
1686 {
1687 	int ifindex;
1688 	struct ifnet *ifp = NULL;
1689 	struct in_ifaddr *ia;
1690 
1691 	if (ifindexp)
1692 		*ifindexp = 0;
1693 	if (ntohl(a->s_addr) >> 24 == 0) {
1694 		ifindex = ntohl(a->s_addr) & 0xffffff;
1695 		ifp = if_byindex(ifindex);
1696 		if (!ifp)
1697 			return NULL;
1698 		if (ifindexp)
1699 			*ifindexp = ifindex;
1700 	} else {
1701 		IN_ADDRHASH_READER_FOREACH(ia, a->s_addr) {
1702 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1703 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1704 				ifp = ia->ia_ifp;
1705 				if (if_is_deactivated(ifp))
1706 					ifp = NULL;
1707 				break;
1708 			}
1709 		}
1710 	}
1711 	return ifp;
1712 }
1713 
1714 static int
1715 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
1716 {
1717 	u_int tval;
1718 	u_char cval;
1719 	int error;
1720 
1721 	if (sopt == NULL)
1722 		return EINVAL;
1723 
1724 	switch (sopt->sopt_size) {
1725 	case sizeof(u_char):
1726 		error = sockopt_get(sopt, &cval, sizeof(u_char));
1727 		tval = cval;
1728 		break;
1729 
1730 	case sizeof(u_int):
1731 		error = sockopt_get(sopt, &tval, sizeof(u_int));
1732 		break;
1733 
1734 	default:
1735 		error = EINVAL;
1736 	}
1737 
1738 	if (error)
1739 		return error;
1740 
1741 	if (tval > maxval)
1742 		return EINVAL;
1743 
1744 	*val = tval;
1745 	return 0;
1746 }
1747 
1748 static int
1749 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
1750     struct psref *psref, struct in_addr *ia, bool add)
1751 {
1752 	int error;
1753 	struct ip_mreq mreq;
1754 
1755 	error = sockopt_get(sopt, &mreq, sizeof(mreq));
1756 	if (error)
1757 		return error;
1758 
1759 	if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
1760 		return EINVAL;
1761 
1762 	memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
1763 
1764 	if (in_nullhost(mreq.imr_interface)) {
1765 		union {
1766 			struct sockaddr		dst;
1767 			struct sockaddr_in	dst4;
1768 		} u;
1769 		struct route ro;
1770 
1771 		if (!add) {
1772 			*ifp = NULL;
1773 			return 0;
1774 		}
1775 		/*
1776 		 * If no interface address was provided, use the interface of
1777 		 * the route to the given multicast address.
1778 		 */
1779 		struct rtentry *rt;
1780 		memset(&ro, 0, sizeof(ro));
1781 
1782 		sockaddr_in_init(&u.dst4, ia, 0);
1783 		error = rtcache_setdst(&ro, &u.dst);
1784 		if (error != 0)
1785 			return error;
1786 		*ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
1787 		if (*ifp != NULL) {
1788 			if (if_is_deactivated(*ifp))
1789 				*ifp = NULL;
1790 			else
1791 				if_acquire(*ifp, psref);
1792 		}
1793 		rtcache_unref(rt, &ro);
1794 		rtcache_free(&ro);
1795 	} else {
1796 		int s = pserialize_read_enter();
1797 		*ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1798 		if (!add && *ifp == NULL) {
1799 			pserialize_read_exit(s);
1800 			return EADDRNOTAVAIL;
1801 		}
1802 		if (*ifp != NULL) {
1803 			if (if_is_deactivated(*ifp))
1804 				*ifp = NULL;
1805 			else
1806 				if_acquire(*ifp, psref);
1807 		}
1808 		pserialize_read_exit(s);
1809 	}
1810 	return 0;
1811 }
1812 
1813 /*
1814  * Add a multicast group membership.
1815  * Group must be a valid IP multicast address.
1816  */
1817 static int
1818 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1819 {
1820 	struct ifnet *ifp = NULL;	// XXX: gcc [ppc]
1821 	struct in_addr ia;
1822 	int i, error, bound;
1823 	struct psref psref;
1824 
1825 	/* imo is protected by solock or referenced only by the caller */
1826 
1827 	bound = curlwp_bind();
1828 	if (sopt->sopt_size == sizeof(struct ip_mreq))
1829 		error = ip_get_membership(sopt, &ifp, &psref, &ia, true);
1830 	else {
1831 #ifdef INET6
1832 		error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
1833 #else
1834 		error = EINVAL;
1835 #endif
1836 	}
1837 
1838 	if (error)
1839 		goto out;
1840 
1841 	/*
1842 	 * See if we found an interface, and confirm that it
1843 	 * supports multicast.
1844 	 */
1845 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1846 		error = EADDRNOTAVAIL;
1847 		goto out;
1848 	}
1849 
1850 	/*
1851 	 * See if the membership already exists or if all the
1852 	 * membership slots are full.
1853 	 */
1854 	for (i = 0; i < imo->imo_num_memberships; ++i) {
1855 		if (imo->imo_membership[i]->inm_ifp == ifp &&
1856 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1857 			break;
1858 	}
1859 	if (i < imo->imo_num_memberships) {
1860 		error = EADDRINUSE;
1861 		goto out;
1862 	}
1863 
1864 	if (i == IP_MAX_MEMBERSHIPS) {
1865 		error = ETOOMANYREFS;
1866 		goto out;
1867 	}
1868 
1869 	/*
1870 	 * Everything looks good; add a new record to the multicast
1871 	 * address list for the given interface.
1872 	 */
1873 	imo->imo_membership[i] = in_addmulti(&ia, ifp);
1874 	if (imo->imo_membership[i] == NULL) {
1875 		error = ENOBUFS;
1876 		goto out;
1877 	}
1878 
1879 	++imo->imo_num_memberships;
1880 	error = 0;
1881 out:
1882 	if_put(ifp, &psref);
1883 	curlwp_bindx(bound);
1884 	return error;
1885 }
1886 
1887 /*
1888  * Drop a multicast group membership.
1889  * Group must be a valid IP multicast address.
1890  */
1891 static int
1892 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1893 {
1894 	struct in_addr ia = { .s_addr = 0 };	// XXX: gcc [ppc]
1895 	struct ifnet *ifp = NULL;		// XXX: gcc [ppc]
1896 	int i, error, bound;
1897 	struct psref psref;
1898 
1899 	/* imo is protected by solock or referenced only by the caller */
1900 
1901 	bound = curlwp_bind();
1902 	if (sopt->sopt_size == sizeof(struct ip_mreq))
1903 		error = ip_get_membership(sopt, &ifp, &psref, &ia, false);
1904 	else {
1905 #ifdef INET6
1906 		error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
1907 #else
1908 		error = EINVAL;
1909 #endif
1910 	}
1911 
1912 	if (error)
1913 		goto out;
1914 
1915 	/*
1916 	 * Find the membership in the membership array.
1917 	 */
1918 	for (i = 0; i < imo->imo_num_memberships; ++i) {
1919 		if ((ifp == NULL ||
1920 		     imo->imo_membership[i]->inm_ifp == ifp) &&
1921 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1922 			break;
1923 	}
1924 	if (i == imo->imo_num_memberships) {
1925 		error = EADDRNOTAVAIL;
1926 		goto out;
1927 	}
1928 
1929 	/*
1930 	 * Give up the multicast address record to which the
1931 	 * membership points.
1932 	 */
1933 	in_delmulti(imo->imo_membership[i]);
1934 
1935 	/*
1936 	 * Remove the gap in the membership array.
1937 	 */
1938 	for (++i; i < imo->imo_num_memberships; ++i)
1939 		imo->imo_membership[i-1] = imo->imo_membership[i];
1940 	--imo->imo_num_memberships;
1941 	error = 0;
1942 out:
1943 	if_put(ifp, &psref);
1944 	curlwp_bindx(bound);
1945 	return error;
1946 }
1947 
1948 /*
1949  * Set the IP multicast options in response to user setsockopt().
1950  */
1951 int
1952 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
1953 {
1954 	struct ip_moptions *imo = *pimo;
1955 	struct in_addr addr;
1956 	struct ifnet *ifp;
1957 	int ifindex, error = 0;
1958 
1959 	/* The passed imo isn't NULL, it should be protected by solock */
1960 
1961 	if (!imo) {
1962 		/*
1963 		 * No multicast option buffer attached to the pcb;
1964 		 * allocate one and initialize to default values.
1965 		 */
1966 		imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
1967 		if (imo == NULL)
1968 			return ENOBUFS;
1969 
1970 		imo->imo_multicast_if_index = 0;
1971 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1972 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1973 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1974 		imo->imo_num_memberships = 0;
1975 		*pimo = imo;
1976 	}
1977 
1978 	switch (sopt->sopt_name) {
1979 	case IP_MULTICAST_IF: {
1980 		int s;
1981 		/*
1982 		 * Select the interface for outgoing multicast packets.
1983 		 */
1984 		error = sockopt_get(sopt, &addr, sizeof(addr));
1985 		if (error)
1986 			break;
1987 
1988 		/*
1989 		 * INADDR_ANY is used to remove a previous selection.
1990 		 * When no interface is selected, a default one is
1991 		 * chosen every time a multicast packet is sent.
1992 		 */
1993 		if (in_nullhost(addr)) {
1994 			imo->imo_multicast_if_index = 0;
1995 			break;
1996 		}
1997 		/*
1998 		 * The selected interface is identified by its local
1999 		 * IP address.  Find the interface and confirm that
2000 		 * it supports multicasting.
2001 		 */
2002 		s = pserialize_read_enter();
2003 		ifp = ip_multicast_if(&addr, &ifindex);
2004 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2005 			pserialize_read_exit(s);
2006 			error = EADDRNOTAVAIL;
2007 			break;
2008 		}
2009 		imo->imo_multicast_if_index = ifp->if_index;
2010 		pserialize_read_exit(s);
2011 		if (ifindex)
2012 			imo->imo_multicast_addr = addr;
2013 		else
2014 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
2015 		break;
2016 	    }
2017 
2018 	case IP_MULTICAST_TTL:
2019 		/*
2020 		 * Set the IP time-to-live for outgoing multicast packets.
2021 		 */
2022 		error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
2023 		break;
2024 
2025 	case IP_MULTICAST_LOOP:
2026 		/*
2027 		 * Set the loopback flag for outgoing multicast packets.
2028 		 * Must be zero or one.
2029 		 */
2030 		error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
2031 		break;
2032 
2033 	case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
2034 		error = ip_add_membership(imo, sopt);
2035 		break;
2036 
2037 	case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
2038 		error = ip_drop_membership(imo, sopt);
2039 		break;
2040 
2041 	default:
2042 		error = EOPNOTSUPP;
2043 		break;
2044 	}
2045 
2046 	/*
2047 	 * If all options have default values, no need to keep the mbuf.
2048 	 */
2049 	if (imo->imo_multicast_if_index == 0 &&
2050 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2051 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2052 	    imo->imo_num_memberships == 0) {
2053 		kmem_intr_free(imo, sizeof(*imo));
2054 		*pimo = NULL;
2055 	}
2056 
2057 	return error;
2058 }
2059 
2060 /*
2061  * Return the IP multicast options in response to user getsockopt().
2062  */
2063 int
2064 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
2065 {
2066 	struct in_addr addr;
2067 	uint8_t optval;
2068 	int error = 0;
2069 
2070 	/* imo is protected by solock or referenced only by the caller */
2071 
2072 	switch (sopt->sopt_name) {
2073 	case IP_MULTICAST_IF:
2074 		if (imo == NULL || imo->imo_multicast_if_index == 0)
2075 			addr = zeroin_addr;
2076 		else if (imo->imo_multicast_addr.s_addr) {
2077 			/* return the value user has set */
2078 			addr = imo->imo_multicast_addr;
2079 		} else {
2080 			struct ifnet *ifp;
2081 			struct in_ifaddr *ia = NULL;
2082 			int s = pserialize_read_enter();
2083 
2084 			ifp = if_byindex(imo->imo_multicast_if_index);
2085 			if (ifp != NULL) {
2086 				ia = in_get_ia_from_ifp(ifp);
2087 			}
2088 			addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
2089 			pserialize_read_exit(s);
2090 		}
2091 		error = sockopt_set(sopt, &addr, sizeof(addr));
2092 		break;
2093 
2094 	case IP_MULTICAST_TTL:
2095 		optval = imo ? imo->imo_multicast_ttl
2096 		    : IP_DEFAULT_MULTICAST_TTL;
2097 
2098 		error = sockopt_set(sopt, &optval, sizeof(optval));
2099 		break;
2100 
2101 	case IP_MULTICAST_LOOP:
2102 		optval = imo ? imo->imo_multicast_loop
2103 		    : IP_DEFAULT_MULTICAST_LOOP;
2104 
2105 		error = sockopt_set(sopt, &optval, sizeof(optval));
2106 		break;
2107 
2108 	default:
2109 		error = EOPNOTSUPP;
2110 	}
2111 
2112 	return error;
2113 }
2114 
2115 /*
2116  * Discard the IP multicast options.
2117  */
2118 void
2119 ip_freemoptions(struct ip_moptions *imo)
2120 {
2121 	int i;
2122 
2123 	/* The owner of imo (inp) should be protected by solock */
2124 
2125 	if (imo != NULL) {
2126 		for (i = 0; i < imo->imo_num_memberships; ++i) {
2127 			struct in_multi *inm = imo->imo_membership[i];
2128 			in_delmulti(inm);
2129 			/* ifp should not leave thanks to solock */
2130 		}
2131 
2132 		kmem_intr_free(imo, sizeof(*imo));
2133 	}
2134 }
2135 
2136 /*
2137  * Routine called from ip_output() to loop back a copy of an IP multicast
2138  * packet to the input queue of a specified interface.  Note that this
2139  * calls the output routine of the loopback "driver", but with an interface
2140  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
2141  */
2142 static void
2143 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
2144 {
2145 	struct ip *ip;
2146 	struct mbuf *copym;
2147 
2148 	copym = m_copypacket(m, M_DONTWAIT);
2149 	if (copym != NULL &&
2150 	    (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
2151 		copym = m_pullup(copym, sizeof(struct ip));
2152 	if (copym == NULL)
2153 		return;
2154 	/*
2155 	 * We don't bother to fragment if the IP length is greater
2156 	 * than the interface's MTU.  Can this possibly matter?
2157 	 */
2158 	ip = mtod(copym, struct ip *);
2159 
2160 	if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
2161 		in_undefer_cksum_tcpudp(copym);
2162 		copym->m_pkthdr.csum_flags &=
2163 		    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
2164 	}
2165 
2166 	ip->ip_sum = 0;
2167 	ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
2168 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
2169 	(void)looutput(ifp, copym, sintocsa(dst), NULL);
2170 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2171 }
2172 
2173 /*
2174  * Ensure sending address is valid.
2175  * Returns 0 on success, -1 if an error should be sent back or 1
2176  * if the packet could be dropped without error (protocol dependent).
2177  */
2178 static int
2179 ip_ifaddrvalid(const struct in_ifaddr *ia)
2180 {
2181 
2182 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
2183 		return 0;
2184 
2185 	if (ia->ia4_flags & IN_IFF_DUPLICATED)
2186 		return -1;
2187 	else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED))
2188 		return 1;
2189 
2190 	return 0;
2191 }
2192