xref: /netbsd-src/sys/netinet/ip_output.c (revision e7ac2a8b5bd66fa2e050809de09a075c36a7014d)
1 /*	$NetBSD: ip_output.c,v 1.319 2020/08/28 17:01:48 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.319 2020/08/28 17:01:48 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 			error = sockopt_getint(sopt, &optval);
1124 			if (error)
1125 				break;
1126 
1127 			switch (sopt->sopt_name) {
1128 			case IP_TOS:
1129 				ip->ip_tos = optval;
1130 				break;
1131 
1132 			case IP_TTL:
1133 				ip->ip_ttl = optval;
1134 				break;
1135 
1136 			case IP_MINTTL:
1137 				if (optval > 0 && optval <= MAXTTL)
1138 					inp->inp_ip_minttl = optval;
1139 				else
1140 					error = EINVAL;
1141 				break;
1142 #define	OPTSET(bit) \
1143 	if (optval) \
1144 		inpflags |= bit; \
1145 	else \
1146 		inpflags &= ~bit;
1147 
1148 			case IP_RECVOPTS:
1149 				OPTSET(INP_RECVOPTS);
1150 				break;
1151 
1152 			case IP_RECVPKTINFO:
1153 				OPTSET(INP_RECVPKTINFO);
1154 				break;
1155 
1156 			case IP_RECVRETOPTS:
1157 				OPTSET(INP_RECVRETOPTS);
1158 				break;
1159 
1160 			case IP_RECVDSTADDR:
1161 				OPTSET(INP_RECVDSTADDR);
1162 				break;
1163 
1164 			case IP_RECVIF:
1165 				OPTSET(INP_RECVIF);
1166 				break;
1167 
1168 			case IP_RECVTTL:
1169 				OPTSET(INP_RECVTTL);
1170 				break;
1171 			}
1172 			break;
1173 		case IP_PKTINFO:
1174 			error = sockopt_getint(sopt, &optval);
1175 			if (!error) {
1176 				/* Linux compatibility */
1177 				OPTSET(INP_RECVPKTINFO);
1178 				break;
1179 			}
1180 			error = sockopt_get(sopt, &pktinfo, sizeof(pktinfo));
1181 			if (error)
1182 				break;
1183 
1184 			if (pktinfo.ipi_ifindex == 0) {
1185 				inp->inp_prefsrcip = pktinfo.ipi_addr;
1186 				break;
1187 			}
1188 
1189 			/* Solaris compatibility */
1190 			struct ifnet *ifp;
1191 			struct in_ifaddr *ia;
1192 			int s;
1193 
1194 			/* pick up primary address */
1195 			s = pserialize_read_enter();
1196 			ifp = if_byindex(pktinfo.ipi_ifindex);
1197 			if (ifp == NULL) {
1198 				pserialize_read_exit(s);
1199 				error = EADDRNOTAVAIL;
1200 				break;
1201 			}
1202 			ia = in_get_ia_from_ifp(ifp);
1203 			if (ia == NULL) {
1204 				pserialize_read_exit(s);
1205 				error = EADDRNOTAVAIL;
1206 				break;
1207 			}
1208 			inp->inp_prefsrcip = IA_SIN(ia)->sin_addr;
1209 			pserialize_read_exit(s);
1210 			break;
1211 		break;
1212 #undef OPTSET
1213 
1214 		case IP_MULTICAST_IF:
1215 		case IP_MULTICAST_TTL:
1216 		case IP_MULTICAST_LOOP:
1217 		case IP_ADD_MEMBERSHIP:
1218 		case IP_DROP_MEMBERSHIP:
1219 			error = ip_setmoptions(&inp->inp_moptions, sopt);
1220 			break;
1221 
1222 		case IP_PORTRANGE:
1223 			error = sockopt_getint(sopt, &optval);
1224 			if (error)
1225 				break;
1226 
1227 			switch (optval) {
1228 			case IP_PORTRANGE_DEFAULT:
1229 			case IP_PORTRANGE_HIGH:
1230 				inpflags &= ~(INP_LOWPORT);
1231 				break;
1232 
1233 			case IP_PORTRANGE_LOW:
1234 				inpflags |= INP_LOWPORT;
1235 				break;
1236 
1237 			default:
1238 				error = EINVAL;
1239 				break;
1240 			}
1241 			break;
1242 
1243 		case IP_PORTALGO:
1244 			error = sockopt_getint(sopt, &optval);
1245 			if (error)
1246 				break;
1247 
1248 			error = portalgo_algo_index_select(
1249 			    (struct inpcb_hdr *)inp, optval);
1250 			break;
1251 
1252 #if defined(IPSEC)
1253 		case IP_IPSEC_POLICY:
1254 			if (ipsec_enabled) {
1255 				error = ipsec_set_policy(inp,
1256 				    sopt->sopt_data, sopt->sopt_size,
1257 				    curlwp->l_cred);
1258 			} else
1259 				error = ENOPROTOOPT;
1260 			break;
1261 #endif /* IPSEC */
1262 
1263 		default:
1264 			error = ENOPROTOOPT;
1265 			break;
1266 		}
1267 		break;
1268 
1269 	case PRCO_GETOPT:
1270 		switch (sopt->sopt_name) {
1271 		case IP_OPTIONS:
1272 		case IP_RETOPTS: {
1273 			struct mbuf *mopts = inp->inp_options;
1274 
1275 			if (mopts) {
1276 				struct mbuf *m;
1277 
1278 				m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
1279 				if (m == NULL) {
1280 					error = ENOBUFS;
1281 					break;
1282 				}
1283 				error = sockopt_setmbuf(sopt, m);
1284 			}
1285 			break;
1286 		}
1287 		case IP_TOS:
1288 		case IP_TTL:
1289 		case IP_MINTTL:
1290 		case IP_RECVOPTS:
1291 		case IP_RECVRETOPTS:
1292 		case IP_RECVDSTADDR:
1293 		case IP_RECVIF:
1294 		case IP_RECVPKTINFO:
1295 		case IP_RECVTTL:
1296 		case IP_ERRORMTU:
1297 			switch (sopt->sopt_name) {
1298 			case IP_TOS:
1299 				optval = ip->ip_tos;
1300 				break;
1301 
1302 			case IP_TTL:
1303 				optval = ip->ip_ttl;
1304 				break;
1305 
1306 			case IP_MINTTL:
1307 				optval = inp->inp_ip_minttl;
1308 				break;
1309 
1310 			case IP_ERRORMTU:
1311 				optval = inp->inp_errormtu;
1312 				break;
1313 
1314 #define	OPTBIT(bit)	(inpflags & bit ? 1 : 0)
1315 
1316 			case IP_RECVOPTS:
1317 				optval = OPTBIT(INP_RECVOPTS);
1318 				break;
1319 
1320 			case IP_RECVPKTINFO:
1321 				optval = OPTBIT(INP_RECVPKTINFO);
1322 				break;
1323 
1324 			case IP_RECVRETOPTS:
1325 				optval = OPTBIT(INP_RECVRETOPTS);
1326 				break;
1327 
1328 			case IP_RECVDSTADDR:
1329 				optval = OPTBIT(INP_RECVDSTADDR);
1330 				break;
1331 
1332 			case IP_RECVIF:
1333 				optval = OPTBIT(INP_RECVIF);
1334 				break;
1335 
1336 			case IP_RECVTTL:
1337 				optval = OPTBIT(INP_RECVTTL);
1338 				break;
1339 			}
1340 			error = sockopt_setint(sopt, optval);
1341 			break;
1342 
1343 		case IP_PKTINFO:
1344 			switch (sopt->sopt_size) {
1345 			case sizeof(int):
1346 				/* Linux compatibility */
1347 				optval = OPTBIT(INP_RECVPKTINFO);
1348 				error = sockopt_setint(sopt, optval);
1349 				break;
1350 			case sizeof(struct in_pktinfo):
1351 				/* Solaris compatibility */
1352 				pktinfo.ipi_ifindex = 0;
1353 				pktinfo.ipi_addr = inp->inp_prefsrcip;
1354 				error = sockopt_set(sopt, &pktinfo,
1355 				    sizeof(pktinfo));
1356 				break;
1357 			default:
1358 				/*
1359 				 * While size is stuck at 0, and, later, if
1360 				 * the caller doesn't use an exactly sized
1361 				 * recipient for the data, default to Linux
1362 				 * compatibility
1363 				 */
1364 				optval = OPTBIT(INP_RECVPKTINFO);
1365 				error = sockopt_setint(sopt, optval);
1366 				break;
1367 			}
1368 			break;
1369 
1370 #if 0	/* defined(IPSEC) */
1371 		case IP_IPSEC_POLICY:
1372 		{
1373 			struct mbuf *m = NULL;
1374 
1375 			/* XXX this will return EINVAL as sopt is empty */
1376 			error = ipsec_get_policy(inp, sopt->sopt_data,
1377 			    sopt->sopt_size, &m);
1378 			if (error == 0)
1379 				error = sockopt_setmbuf(sopt, m);
1380 			break;
1381 		}
1382 #endif /*IPSEC*/
1383 
1384 		case IP_MULTICAST_IF:
1385 		case IP_MULTICAST_TTL:
1386 		case IP_MULTICAST_LOOP:
1387 		case IP_ADD_MEMBERSHIP:
1388 		case IP_DROP_MEMBERSHIP:
1389 			error = ip_getmoptions(inp->inp_moptions, sopt);
1390 			break;
1391 
1392 		case IP_PORTRANGE:
1393 			if (inpflags & INP_LOWPORT)
1394 				optval = IP_PORTRANGE_LOW;
1395 			else
1396 				optval = IP_PORTRANGE_DEFAULT;
1397 			error = sockopt_setint(sopt, optval);
1398 			break;
1399 
1400 		case IP_PORTALGO:
1401 			optval = inp->inp_portalgo;
1402 			error = sockopt_setint(sopt, optval);
1403 			break;
1404 
1405 		default:
1406 			error = ENOPROTOOPT;
1407 			break;
1408 		}
1409 		break;
1410 	}
1411 
1412 	if (!error) {
1413 		inp->inp_flags = inpflags;
1414 	}
1415 	return error;
1416 }
1417 
1418 static int
1419 ip_pktinfo_prepare(const struct in_pktinfo *pktinfo, struct ip_pktopts *pktopts,
1420     int *flags, kauth_cred_t cred)
1421 {
1422 	struct ip_moptions *imo;
1423 	int error = 0;
1424 	bool addrset = false;
1425 
1426 	if (!in_nullhost(pktinfo->ipi_addr)) {
1427 		pktopts->ippo_laddr.sin_addr = pktinfo->ipi_addr;
1428 		/* EADDRNOTAVAIL? */
1429 		error = in_pcbbindableaddr(&pktopts->ippo_laddr, cred);
1430 		if (error != 0)
1431 			return error;
1432 		addrset = true;
1433 	}
1434 
1435 	if (pktinfo->ipi_ifindex != 0) {
1436 		if (!addrset) {
1437 			struct ifnet *ifp;
1438 			struct in_ifaddr *ia;
1439 			int s;
1440 
1441 			/* pick up primary address */
1442 			s = pserialize_read_enter();
1443 			ifp = if_byindex(pktinfo->ipi_ifindex);
1444 			if (ifp == NULL) {
1445 				pserialize_read_exit(s);
1446 				return EADDRNOTAVAIL;
1447 			}
1448 			ia = in_get_ia_from_ifp(ifp);
1449 			if (ia == NULL) {
1450 				pserialize_read_exit(s);
1451 				return EADDRNOTAVAIL;
1452 			}
1453 			pktopts->ippo_laddr.sin_addr = IA_SIN(ia)->sin_addr;
1454 			pserialize_read_exit(s);
1455 		}
1456 
1457 		/*
1458 		 * If specified ipi_ifindex,
1459 		 * use copied or locally initialized ip_moptions.
1460 		 * Original ip_moptions must not be modified.
1461 		 */
1462 		imo = &pktopts->ippo_imobuf;	/* local buf in pktopts */
1463 		if (pktopts->ippo_imo != NULL) {
1464 			memcpy(imo, pktopts->ippo_imo, sizeof(*imo));
1465 		} else {
1466 			memset(imo, 0, sizeof(*imo));
1467 			imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1468 			imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1469 		}
1470 		imo->imo_multicast_if_index = pktinfo->ipi_ifindex;
1471 		pktopts->ippo_imo = imo;
1472 		*flags |= IP_ROUTETOIFINDEX;
1473 	}
1474 	return error;
1475 }
1476 
1477 /*
1478  * Set up IP outgoing packet options. Even if control is NULL,
1479  * pktopts->ippo_laddr and pktopts->ippo_imo are set and used.
1480  */
1481 int
1482 ip_setpktopts(struct mbuf *control, struct ip_pktopts *pktopts, int *flags,
1483     struct inpcb *inp, kauth_cred_t cred)
1484 {
1485 	struct cmsghdr *cm;
1486 	struct in_pktinfo pktinfo;
1487 	int error;
1488 
1489 	pktopts->ippo_imo = inp->inp_moptions;
1490 
1491 	struct in_addr *ia = in_nullhost(inp->inp_prefsrcip) ? &inp->inp_laddr :
1492 	    &inp->inp_prefsrcip;
1493 	sockaddr_in_init(&pktopts->ippo_laddr, ia, 0);
1494 
1495 	if (control == NULL)
1496 		return 0;
1497 
1498 	/*
1499 	 * XXX: Currently, we assume all the optional information is
1500 	 * stored in a single mbuf.
1501 	 */
1502 	if (control->m_next)
1503 		return EINVAL;
1504 
1505 	for (; control->m_len > 0;
1506 	    control->m_data += CMSG_ALIGN(cm->cmsg_len),
1507 	    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1508 		cm = mtod(control, struct cmsghdr *);
1509 		if ((control->m_len < sizeof(*cm)) ||
1510 		    (cm->cmsg_len == 0) ||
1511 		    (cm->cmsg_len > control->m_len)) {
1512 			return EINVAL;
1513 		}
1514 		if (cm->cmsg_level != IPPROTO_IP)
1515 			continue;
1516 
1517 		switch (cm->cmsg_type) {
1518 		case IP_PKTINFO:
1519 			if (cm->cmsg_len != CMSG_LEN(sizeof(pktinfo)))
1520 				return EINVAL;
1521 			memcpy(&pktinfo, CMSG_DATA(cm), sizeof(pktinfo));
1522 			error = ip_pktinfo_prepare(&pktinfo, pktopts, flags,
1523 			    cred);
1524 			if (error)
1525 				return error;
1526 			break;
1527 		case IP_SENDSRCADDR: /* FreeBSD compatibility */
1528 			if (cm->cmsg_len != CMSG_LEN(sizeof(struct in_addr)))
1529 				return EINVAL;
1530 			pktinfo.ipi_ifindex = 0;
1531 			pktinfo.ipi_addr =
1532 			    ((struct in_pktinfo *)CMSG_DATA(cm))->ipi_addr;
1533 			error = ip_pktinfo_prepare(&pktinfo, pktopts, flags,
1534 			    cred);
1535 			if (error)
1536 				return error;
1537 			break;
1538 		default:
1539 			return ENOPROTOOPT;
1540 		}
1541 	}
1542 	return 0;
1543 }
1544 
1545 /*
1546  * Set up IP options in pcb for insertion in output packets.
1547  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1548  * with destination address if source routed.
1549  */
1550 static int
1551 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
1552 {
1553 	struct mbuf *m;
1554 	const u_char *cp;
1555 	u_char *dp;
1556 	int cnt;
1557 
1558 	KASSERT(inp_locked(inp));
1559 
1560 	/* Turn off any old options. */
1561 	if (inp->inp_options) {
1562 		m_free(inp->inp_options);
1563 	}
1564 	inp->inp_options = NULL;
1565 	if ((cnt = sopt->sopt_size) == 0) {
1566 		/* Only turning off any previous options. */
1567 		return 0;
1568 	}
1569 	cp = sopt->sopt_data;
1570 
1571 	if (cnt % 4) {
1572 		/* Must be 4-byte aligned, because there's no padding. */
1573 		return EINVAL;
1574 	}
1575 
1576 	m = m_get(M_DONTWAIT, MT_SOOPTS);
1577 	if (m == NULL)
1578 		return ENOBUFS;
1579 
1580 	dp = mtod(m, u_char *);
1581 	memset(dp, 0, sizeof(struct in_addr));
1582 	dp += sizeof(struct in_addr);
1583 	m->m_len = sizeof(struct in_addr);
1584 
1585 	/*
1586 	 * IP option list according to RFC791. Each option is of the form
1587 	 *
1588 	 *	[optval] [olen] [(olen - 2) data bytes]
1589 	 *
1590 	 * We validate the list and copy options to an mbuf for prepending
1591 	 * to data packets. The IP first-hop destination address will be
1592 	 * stored before actual options and is zero if unset.
1593 	 */
1594 	while (cnt > 0) {
1595 		uint8_t optval, olen, offset;
1596 
1597 		optval = cp[IPOPT_OPTVAL];
1598 
1599 		if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
1600 			olen = 1;
1601 		} else {
1602 			if (cnt < IPOPT_OLEN + 1)
1603 				goto bad;
1604 
1605 			olen = cp[IPOPT_OLEN];
1606 			if (olen < IPOPT_OLEN + 1 || olen > cnt)
1607 				goto bad;
1608 		}
1609 
1610 		if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
1611 			/*
1612 			 * user process specifies route as:
1613 			 *	->A->B->C->D
1614 			 * D must be our final destination (but we can't
1615 			 * check that since we may not have connected yet).
1616 			 * A is first hop destination, which doesn't appear in
1617 			 * actual IP option, but is stored before the options.
1618 			 */
1619 			if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
1620 				goto bad;
1621 
1622 			offset = cp[IPOPT_OFFSET];
1623 			memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
1624 			    sizeof(struct in_addr));
1625 
1626 			cp += sizeof(struct in_addr);
1627 			cnt -= sizeof(struct in_addr);
1628 			olen -= sizeof(struct in_addr);
1629 
1630 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1631 				goto bad;
1632 
1633 			memcpy(dp, cp, olen);
1634 			dp[IPOPT_OPTVAL] = optval;
1635 			dp[IPOPT_OLEN] = olen;
1636 			dp[IPOPT_OFFSET] = offset;
1637 			break;
1638 		} else {
1639 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1640 				goto bad;
1641 
1642 			memcpy(dp, cp, olen);
1643 			break;
1644 		}
1645 
1646 		dp += olen;
1647 		m->m_len += olen;
1648 
1649 		if (optval == IPOPT_EOL)
1650 			break;
1651 
1652 		cp += olen;
1653 		cnt -= olen;
1654 	}
1655 
1656 	inp->inp_options = m;
1657 	return 0;
1658 
1659 bad:
1660 	(void)m_free(m);
1661 	return EINVAL;
1662 }
1663 
1664 /*
1665  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1666  * Must be called in a pserialize critical section.
1667  */
1668 static struct ifnet *
1669 ip_multicast_if(struct in_addr *a, int *ifindexp)
1670 {
1671 	int ifindex;
1672 	struct ifnet *ifp = NULL;
1673 	struct in_ifaddr *ia;
1674 
1675 	if (ifindexp)
1676 		*ifindexp = 0;
1677 	if (ntohl(a->s_addr) >> 24 == 0) {
1678 		ifindex = ntohl(a->s_addr) & 0xffffff;
1679 		ifp = if_byindex(ifindex);
1680 		if (!ifp)
1681 			return NULL;
1682 		if (ifindexp)
1683 			*ifindexp = ifindex;
1684 	} else {
1685 		IN_ADDRHASH_READER_FOREACH(ia, a->s_addr) {
1686 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1687 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1688 				ifp = ia->ia_ifp;
1689 				if (if_is_deactivated(ifp))
1690 					ifp = NULL;
1691 				break;
1692 			}
1693 		}
1694 	}
1695 	return ifp;
1696 }
1697 
1698 static int
1699 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
1700 {
1701 	u_int tval;
1702 	u_char cval;
1703 	int error;
1704 
1705 	if (sopt == NULL)
1706 		return EINVAL;
1707 
1708 	switch (sopt->sopt_size) {
1709 	case sizeof(u_char):
1710 		error = sockopt_get(sopt, &cval, sizeof(u_char));
1711 		tval = cval;
1712 		break;
1713 
1714 	case sizeof(u_int):
1715 		error = sockopt_get(sopt, &tval, sizeof(u_int));
1716 		break;
1717 
1718 	default:
1719 		error = EINVAL;
1720 	}
1721 
1722 	if (error)
1723 		return error;
1724 
1725 	if (tval > maxval)
1726 		return EINVAL;
1727 
1728 	*val = tval;
1729 	return 0;
1730 }
1731 
1732 static int
1733 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
1734     struct psref *psref, struct in_addr *ia, bool add)
1735 {
1736 	int error;
1737 	struct ip_mreq mreq;
1738 
1739 	error = sockopt_get(sopt, &mreq, sizeof(mreq));
1740 	if (error)
1741 		return error;
1742 
1743 	if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
1744 		return EINVAL;
1745 
1746 	memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
1747 
1748 	if (in_nullhost(mreq.imr_interface)) {
1749 		union {
1750 			struct sockaddr		dst;
1751 			struct sockaddr_in	dst4;
1752 		} u;
1753 		struct route ro;
1754 
1755 		if (!add) {
1756 			*ifp = NULL;
1757 			return 0;
1758 		}
1759 		/*
1760 		 * If no interface address was provided, use the interface of
1761 		 * the route to the given multicast address.
1762 		 */
1763 		struct rtentry *rt;
1764 		memset(&ro, 0, sizeof(ro));
1765 
1766 		sockaddr_in_init(&u.dst4, ia, 0);
1767 		error = rtcache_setdst(&ro, &u.dst);
1768 		if (error != 0)
1769 			return error;
1770 		*ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
1771 		if (*ifp != NULL) {
1772 			if (if_is_deactivated(*ifp))
1773 				*ifp = NULL;
1774 			else
1775 				if_acquire(*ifp, psref);
1776 		}
1777 		rtcache_unref(rt, &ro);
1778 		rtcache_free(&ro);
1779 	} else {
1780 		int s = pserialize_read_enter();
1781 		*ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1782 		if (!add && *ifp == NULL) {
1783 			pserialize_read_exit(s);
1784 			return EADDRNOTAVAIL;
1785 		}
1786 		if (*ifp != NULL) {
1787 			if (if_is_deactivated(*ifp))
1788 				*ifp = NULL;
1789 			else
1790 				if_acquire(*ifp, psref);
1791 		}
1792 		pserialize_read_exit(s);
1793 	}
1794 	return 0;
1795 }
1796 
1797 /*
1798  * Add a multicast group membership.
1799  * Group must be a valid IP multicast address.
1800  */
1801 static int
1802 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1803 {
1804 	struct ifnet *ifp = NULL;	// XXX: gcc [ppc]
1805 	struct in_addr ia;
1806 	int i, error, bound;
1807 	struct psref psref;
1808 
1809 	/* imo is protected by solock or referenced only by the caller */
1810 
1811 	bound = curlwp_bind();
1812 	if (sopt->sopt_size == sizeof(struct ip_mreq))
1813 		error = ip_get_membership(sopt, &ifp, &psref, &ia, true);
1814 	else {
1815 #ifdef INET6
1816 		error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
1817 #else
1818 		error = EINVAL;
1819 #endif
1820 	}
1821 
1822 	if (error)
1823 		goto out;
1824 
1825 	/*
1826 	 * See if we found an interface, and confirm that it
1827 	 * supports multicast.
1828 	 */
1829 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1830 		error = EADDRNOTAVAIL;
1831 		goto out;
1832 	}
1833 
1834 	/*
1835 	 * See if the membership already exists or if all the
1836 	 * membership slots are full.
1837 	 */
1838 	for (i = 0; i < imo->imo_num_memberships; ++i) {
1839 		if (imo->imo_membership[i]->inm_ifp == ifp &&
1840 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1841 			break;
1842 	}
1843 	if (i < imo->imo_num_memberships) {
1844 		error = EADDRINUSE;
1845 		goto out;
1846 	}
1847 
1848 	if (i == IP_MAX_MEMBERSHIPS) {
1849 		error = ETOOMANYREFS;
1850 		goto out;
1851 	}
1852 
1853 	/*
1854 	 * Everything looks good; add a new record to the multicast
1855 	 * address list for the given interface.
1856 	 */
1857 	imo->imo_membership[i] = in_addmulti(&ia, ifp);
1858 	if (imo->imo_membership[i] == NULL) {
1859 		error = ENOBUFS;
1860 		goto out;
1861 	}
1862 
1863 	++imo->imo_num_memberships;
1864 	error = 0;
1865 out:
1866 	if_put(ifp, &psref);
1867 	curlwp_bindx(bound);
1868 	return error;
1869 }
1870 
1871 /*
1872  * Drop a multicast group membership.
1873  * Group must be a valid IP multicast address.
1874  */
1875 static int
1876 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1877 {
1878 	struct in_addr ia = { .s_addr = 0 };	// XXX: gcc [ppc]
1879 	struct ifnet *ifp = NULL;		// XXX: gcc [ppc]
1880 	int i, error, bound;
1881 	struct psref psref;
1882 
1883 	/* imo is protected by solock or referenced only by the caller */
1884 
1885 	bound = curlwp_bind();
1886 	if (sopt->sopt_size == sizeof(struct ip_mreq))
1887 		error = ip_get_membership(sopt, &ifp, &psref, &ia, false);
1888 	else {
1889 #ifdef INET6
1890 		error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
1891 #else
1892 		error = EINVAL;
1893 #endif
1894 	}
1895 
1896 	if (error)
1897 		goto out;
1898 
1899 	/*
1900 	 * Find the membership in the membership array.
1901 	 */
1902 	for (i = 0; i < imo->imo_num_memberships; ++i) {
1903 		if ((ifp == NULL ||
1904 		     imo->imo_membership[i]->inm_ifp == ifp) &&
1905 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1906 			break;
1907 	}
1908 	if (i == imo->imo_num_memberships) {
1909 		error = EADDRNOTAVAIL;
1910 		goto out;
1911 	}
1912 
1913 	/*
1914 	 * Give up the multicast address record to which the
1915 	 * membership points.
1916 	 */
1917 	in_delmulti(imo->imo_membership[i]);
1918 
1919 	/*
1920 	 * Remove the gap in the membership array.
1921 	 */
1922 	for (++i; i < imo->imo_num_memberships; ++i)
1923 		imo->imo_membership[i-1] = imo->imo_membership[i];
1924 	--imo->imo_num_memberships;
1925 	error = 0;
1926 out:
1927 	if_put(ifp, &psref);
1928 	curlwp_bindx(bound);
1929 	return error;
1930 }
1931 
1932 /*
1933  * Set the IP multicast options in response to user setsockopt().
1934  */
1935 int
1936 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
1937 {
1938 	struct ip_moptions *imo = *pimo;
1939 	struct in_addr addr;
1940 	struct ifnet *ifp;
1941 	int ifindex, error = 0;
1942 
1943 	/* The passed imo isn't NULL, it should be protected by solock */
1944 
1945 	if (!imo) {
1946 		/*
1947 		 * No multicast option buffer attached to the pcb;
1948 		 * allocate one and initialize to default values.
1949 		 */
1950 		imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
1951 		if (imo == NULL)
1952 			return ENOBUFS;
1953 
1954 		imo->imo_multicast_if_index = 0;
1955 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1956 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1957 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1958 		imo->imo_num_memberships = 0;
1959 		*pimo = imo;
1960 	}
1961 
1962 	switch (sopt->sopt_name) {
1963 	case IP_MULTICAST_IF: {
1964 		int s;
1965 		/*
1966 		 * Select the interface for outgoing multicast packets.
1967 		 */
1968 		error = sockopt_get(sopt, &addr, sizeof(addr));
1969 		if (error)
1970 			break;
1971 
1972 		/*
1973 		 * INADDR_ANY is used to remove a previous selection.
1974 		 * When no interface is selected, a default one is
1975 		 * chosen every time a multicast packet is sent.
1976 		 */
1977 		if (in_nullhost(addr)) {
1978 			imo->imo_multicast_if_index = 0;
1979 			break;
1980 		}
1981 		/*
1982 		 * The selected interface is identified by its local
1983 		 * IP address.  Find the interface and confirm that
1984 		 * it supports multicasting.
1985 		 */
1986 		s = pserialize_read_enter();
1987 		ifp = ip_multicast_if(&addr, &ifindex);
1988 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1989 			pserialize_read_exit(s);
1990 			error = EADDRNOTAVAIL;
1991 			break;
1992 		}
1993 		imo->imo_multicast_if_index = ifp->if_index;
1994 		pserialize_read_exit(s);
1995 		if (ifindex)
1996 			imo->imo_multicast_addr = addr;
1997 		else
1998 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1999 		break;
2000 	    }
2001 
2002 	case IP_MULTICAST_TTL:
2003 		/*
2004 		 * Set the IP time-to-live for outgoing multicast packets.
2005 		 */
2006 		error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
2007 		break;
2008 
2009 	case IP_MULTICAST_LOOP:
2010 		/*
2011 		 * Set the loopback flag for outgoing multicast packets.
2012 		 * Must be zero or one.
2013 		 */
2014 		error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
2015 		break;
2016 
2017 	case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
2018 		error = ip_add_membership(imo, sopt);
2019 		break;
2020 
2021 	case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
2022 		error = ip_drop_membership(imo, sopt);
2023 		break;
2024 
2025 	default:
2026 		error = EOPNOTSUPP;
2027 		break;
2028 	}
2029 
2030 	/*
2031 	 * If all options have default values, no need to keep the mbuf.
2032 	 */
2033 	if (imo->imo_multicast_if_index == 0 &&
2034 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2035 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2036 	    imo->imo_num_memberships == 0) {
2037 		kmem_intr_free(imo, sizeof(*imo));
2038 		*pimo = NULL;
2039 	}
2040 
2041 	return error;
2042 }
2043 
2044 /*
2045  * Return the IP multicast options in response to user getsockopt().
2046  */
2047 int
2048 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
2049 {
2050 	struct in_addr addr;
2051 	uint8_t optval;
2052 	int error = 0;
2053 
2054 	/* imo is protected by solock or referenced only by the caller */
2055 
2056 	switch (sopt->sopt_name) {
2057 	case IP_MULTICAST_IF:
2058 		if (imo == NULL || imo->imo_multicast_if_index == 0)
2059 			addr = zeroin_addr;
2060 		else if (imo->imo_multicast_addr.s_addr) {
2061 			/* return the value user has set */
2062 			addr = imo->imo_multicast_addr;
2063 		} else {
2064 			struct ifnet *ifp;
2065 			struct in_ifaddr *ia = NULL;
2066 			int s = pserialize_read_enter();
2067 
2068 			ifp = if_byindex(imo->imo_multicast_if_index);
2069 			if (ifp != NULL) {
2070 				ia = in_get_ia_from_ifp(ifp);
2071 			}
2072 			addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
2073 			pserialize_read_exit(s);
2074 		}
2075 		error = sockopt_set(sopt, &addr, sizeof(addr));
2076 		break;
2077 
2078 	case IP_MULTICAST_TTL:
2079 		optval = imo ? imo->imo_multicast_ttl
2080 		    : IP_DEFAULT_MULTICAST_TTL;
2081 
2082 		error = sockopt_set(sopt, &optval, sizeof(optval));
2083 		break;
2084 
2085 	case IP_MULTICAST_LOOP:
2086 		optval = imo ? imo->imo_multicast_loop
2087 		    : IP_DEFAULT_MULTICAST_LOOP;
2088 
2089 		error = sockopt_set(sopt, &optval, sizeof(optval));
2090 		break;
2091 
2092 	default:
2093 		error = EOPNOTSUPP;
2094 	}
2095 
2096 	return error;
2097 }
2098 
2099 /*
2100  * Discard the IP multicast options.
2101  */
2102 void
2103 ip_freemoptions(struct ip_moptions *imo)
2104 {
2105 	int i;
2106 
2107 	/* The owner of imo (inp) should be protected by solock */
2108 
2109 	if (imo != NULL) {
2110 		for (i = 0; i < imo->imo_num_memberships; ++i) {
2111 			struct in_multi *inm = imo->imo_membership[i];
2112 			in_delmulti(inm);
2113 			/* ifp should not leave thanks to solock */
2114 		}
2115 
2116 		kmem_intr_free(imo, sizeof(*imo));
2117 	}
2118 }
2119 
2120 /*
2121  * Routine called from ip_output() to loop back a copy of an IP multicast
2122  * packet to the input queue of a specified interface.  Note that this
2123  * calls the output routine of the loopback "driver", but with an interface
2124  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
2125  */
2126 static void
2127 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
2128 {
2129 	struct ip *ip;
2130 	struct mbuf *copym;
2131 
2132 	copym = m_copypacket(m, M_DONTWAIT);
2133 	if (copym != NULL &&
2134 	    (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
2135 		copym = m_pullup(copym, sizeof(struct ip));
2136 	if (copym == NULL)
2137 		return;
2138 	/*
2139 	 * We don't bother to fragment if the IP length is greater
2140 	 * than the interface's MTU.  Can this possibly matter?
2141 	 */
2142 	ip = mtod(copym, struct ip *);
2143 
2144 	if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
2145 		in_undefer_cksum_tcpudp(copym);
2146 		copym->m_pkthdr.csum_flags &=
2147 		    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
2148 	}
2149 
2150 	ip->ip_sum = 0;
2151 	ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
2152 	KERNEL_LOCK_UNLESS_NET_MPSAFE();
2153 	(void)looutput(ifp, copym, sintocsa(dst), NULL);
2154 	KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
2155 }
2156 
2157 /*
2158  * Ensure sending address is valid.
2159  * Returns 0 on success, -1 if an error should be sent back or 1
2160  * if the packet could be dropped without error (protocol dependent).
2161  */
2162 static int
2163 ip_ifaddrvalid(const struct in_ifaddr *ia)
2164 {
2165 
2166 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
2167 		return 0;
2168 
2169 	if (ia->ia4_flags & IN_IFF_DUPLICATED)
2170 		return -1;
2171 	else if (ia->ia4_flags & (IN_IFF_TENTATIVE | IN_IFF_DETACHED))
2172 		return 1;
2173 
2174 	return 0;
2175 }
2176