xref: /netbsd-src/sys/netinet/ip_output.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*	$NetBSD: ip_output.c,v 1.255 2016/05/09 07:02:10 ozaki-r 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.255 2016/05/09 07:02:10 ozaki-r 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 <sys/param.h>
105 #include <sys/kmem.h>
106 #include <sys/mbuf.h>
107 #include <sys/protosw.h>
108 #include <sys/socket.h>
109 #include <sys/socketvar.h>
110 #include <sys/kauth.h>
111 #ifdef IPSEC
112 #include <sys/domain.h>
113 #endif
114 #include <sys/systm.h>
115 
116 #include <net/if.h>
117 #include <net/if_types.h>
118 #include <net/route.h>
119 #include <net/pfil.h>
120 
121 #include <netinet/in.h>
122 #include <netinet/in_systm.h>
123 #include <netinet/ip.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/in_var.h>
126 #include <netinet/ip_var.h>
127 #include <netinet/ip_private.h>
128 #include <netinet/in_offload.h>
129 #include <netinet/portalgo.h>
130 #include <netinet/udp.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 
156 extern pfil_head_t *inet_pfil_hook;			/* XXX */
157 
158 int	ip_do_loopback_cksum = 0;
159 
160 static int
161 ip_mark_mpls(struct ifnet * const ifp, struct mbuf * const m,
162     const struct rtentry *rt)
163 {
164 	int error = 0;
165 #ifdef MPLS
166 	union mpls_shim msh;
167 
168 	if (rt == NULL || rt_gettag(rt) == NULL ||
169 	    rt_gettag(rt)->sa_family != AF_MPLS ||
170 	    (m->m_flags & (M_MCAST | M_BCAST)) != 0 ||
171 	    ifp->if_type != IFT_ETHER)
172 		return 0;
173 
174 	msh.s_addr = MPLS_GETSADDR(rt);
175 	if (msh.shim.label != MPLS_LABEL_IMPLNULL) {
176 		struct m_tag *mtag;
177 		/*
178 		 * XXX tentative solution to tell ether_output
179 		 * it's MPLS. Need some more efficient solution.
180 		 */
181 		mtag = m_tag_get(PACKET_TAG_MPLS,
182 		    sizeof(int) /* dummy */,
183 		    M_NOWAIT);
184 		if (mtag == NULL)
185 			return ENOMEM;
186 		m_tag_prepend(m, mtag);
187 	}
188 #endif
189 	return error;
190 }
191 
192 /*
193  * Send an IP packet to a host.
194  */
195 int
196 ip_if_output(struct ifnet * const ifp, struct mbuf * const m,
197     const struct sockaddr * const dst, const struct rtentry *rt)
198 {
199 	int error = 0;
200 
201 	if (rt != NULL) {
202 		error = rt_check_reject_route(rt, ifp);
203 		if (error != 0) {
204 			m_freem(m);
205 			return error;
206 		}
207 	}
208 
209 	error = ip_mark_mpls(ifp, m, rt);
210 	if (error != 0) {
211 		m_freem(m);
212 		return error;
213 	}
214 
215 #ifndef NET_MPSAFE
216 	KERNEL_LOCK(1, NULL);
217 #endif
218 
219 	error = (*ifp->if_output)(ifp, m, dst, rt);
220 
221 #ifndef NET_MPSAFE
222 	KERNEL_UNLOCK_ONE(NULL);
223 #endif
224 	return error;
225 }
226 
227 /*
228  * IP output.  The packet in mbuf chain m contains a skeletal IP
229  * header (with len, off, ttl, proto, tos, src, dst).
230  * The mbuf chain containing the packet will be freed.
231  * The mbuf opt, if present, will not be freed.
232  */
233 int
234 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
235     struct ip_moptions *imo, struct socket *so)
236 {
237 	struct rtentry *rt;
238 	struct ip *ip;
239 	struct ifnet *ifp;
240 	struct mbuf *m = m0;
241 	int hlen = sizeof (struct ip);
242 	int len, error = 0;
243 	struct route iproute;
244 	const struct sockaddr_in *dst;
245 	struct in_ifaddr *ia;
246 	int isbroadcast;
247 	int sw_csum;
248 	u_long mtu;
249 #ifdef IPSEC
250 	struct secpolicy *sp = NULL;
251 #endif
252 	bool natt_frag = false;
253 	bool rtmtu_nolock;
254 	union {
255 		struct sockaddr		dst;
256 		struct sockaddr_in	dst4;
257 	} u;
258 	struct sockaddr *rdst = &u.dst;	/* real IP destination, as opposed
259 					 * to the nexthop
260 					 */
261 
262 	len = 0;
263 
264 	MCLAIM(m, &ip_tx_mowner);
265 
266 	KASSERT((m->m_flags & M_PKTHDR) != 0);
267 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) == 0);
268 	KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) !=
269 	    (M_CSUM_TCPv4|M_CSUM_UDPv4));
270 
271 	if (opt) {
272 		m = ip_insertoptions(m, opt, &len);
273 		if (len >= sizeof(struct ip))
274 			hlen = len;
275 	}
276 	ip = mtod(m, struct ip *);
277 
278 	/*
279 	 * Fill in IP header.
280 	 */
281 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
282 		ip->ip_v = IPVERSION;
283 		ip->ip_off = htons(0);
284 		/* ip->ip_id filled in after we find out source ia */
285 		ip->ip_hl = hlen >> 2;
286 		IP_STATINC(IP_STAT_LOCALOUT);
287 	} else {
288 		hlen = ip->ip_hl << 2;
289 	}
290 
291 	/*
292 	 * Route packet.
293 	 */
294 	if (ro == NULL) {
295 		memset(&iproute, 0, sizeof(iproute));
296 		ro = &iproute;
297 	}
298 	sockaddr_in_init(&u.dst4, &ip->ip_dst, 0);
299 	dst = satocsin(rtcache_getdst(ro));
300 
301 	/*
302 	 * If there is a cached route, check that it is to the same
303 	 * destination and is still up.  If not, free it and try again.
304 	 * The address family should also be checked in case of sharing
305 	 * the cache with IPv6.
306 	 */
307 	if (dst && (dst->sin_family != AF_INET ||
308 	    !in_hosteq(dst->sin_addr, ip->ip_dst)))
309 		rtcache_free(ro);
310 
311 	if ((rt = rtcache_validate(ro)) == NULL &&
312 	    (rt = rtcache_update(ro, 1)) == NULL) {
313 		dst = &u.dst4;
314 		error = rtcache_setdst(ro, &u.dst);
315 		if (error != 0)
316 			goto bad;
317 	}
318 
319 	/*
320 	 * If routing to interface only, short circuit routing lookup.
321 	 */
322 	if (flags & IP_ROUTETOIF) {
323 		if ((ia = ifatoia(ifa_ifwithladdr(sintocsa(dst)))) == NULL) {
324 			IP_STATINC(IP_STAT_NOROUTE);
325 			error = ENETUNREACH;
326 			goto bad;
327 		}
328 		ifp = ia->ia_ifp;
329 		mtu = ifp->if_mtu;
330 		ip->ip_ttl = 1;
331 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
332 	} else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
333 	    ip->ip_dst.s_addr == INADDR_BROADCAST) &&
334 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
335 		ifp = imo->imo_multicast_ifp;
336 		mtu = ifp->if_mtu;
337 		IFP_TO_IA(ifp, ia);
338 		isbroadcast = 0;
339 	} else {
340 		if (rt == NULL)
341 			rt = rtcache_init(ro);
342 		if (rt == NULL) {
343 			IP_STATINC(IP_STAT_NOROUTE);
344 			error = EHOSTUNREACH;
345 			goto bad;
346 		}
347 		ia = ifatoia(rt->rt_ifa);
348 		ifp = rt->rt_ifp;
349 		if ((mtu = rt->rt_rmx.rmx_mtu) == 0)
350 			mtu = ifp->if_mtu;
351 		rt->rt_use++;
352 		if (rt->rt_flags & RTF_GATEWAY)
353 			dst = satosin(rt->rt_gateway);
354 		if (rt->rt_flags & RTF_HOST)
355 			isbroadcast = rt->rt_flags & RTF_BROADCAST;
356 		else
357 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
358 	}
359 	rtmtu_nolock = rt && (rt->rt_rmx.rmx_locks & RTV_MTU) == 0;
360 
361 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
362 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
363 		bool inmgroup;
364 
365 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
366 		    M_BCAST : M_MCAST;
367 		/*
368 		 * See if the caller provided any multicast options
369 		 */
370 		if (imo != NULL)
371 			ip->ip_ttl = imo->imo_multicast_ttl;
372 		else
373 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
374 
375 		/*
376 		 * if we don't know the outgoing ifp yet, we can't generate
377 		 * output
378 		 */
379 		if (!ifp) {
380 			IP_STATINC(IP_STAT_NOROUTE);
381 			error = ENETUNREACH;
382 			goto bad;
383 		}
384 
385 		/*
386 		 * If the packet is multicast or broadcast, confirm that
387 		 * the outgoing interface can transmit it.
388 		 */
389 		if (((m->m_flags & M_MCAST) &&
390 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
391 		    ((m->m_flags & M_BCAST) &&
392 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
393 			IP_STATINC(IP_STAT_NOROUTE);
394 			error = ENETUNREACH;
395 			goto bad;
396 		}
397 		/*
398 		 * If source address not specified yet, use an address
399 		 * of outgoing interface.
400 		 */
401 		if (in_nullhost(ip->ip_src)) {
402 			struct in_ifaddr *xia;
403 			struct ifaddr *xifa;
404 
405 			IFP_TO_IA(ifp, xia);
406 			if (!xia) {
407 				error = EADDRNOTAVAIL;
408 				goto bad;
409 			}
410 			xifa = &xia->ia_ifa;
411 			if (xifa->ifa_getifa != NULL) {
412 				xia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
413 				if (xia == NULL) {
414 					error = EADDRNOTAVAIL;
415 					goto bad;
416 				}
417 			}
418 			ip->ip_src = xia->ia_addr.sin_addr;
419 		}
420 
421 		inmgroup = in_multi_group(ip->ip_dst, ifp, flags);
422 		if (inmgroup && (imo == NULL || imo->imo_multicast_loop)) {
423 			/*
424 			 * If we belong to the destination multicast group
425 			 * on the outgoing interface, and the caller did not
426 			 * forbid loopback, loop back a copy.
427 			 */
428 			ip_mloopback(ifp, m, &u.dst4);
429 		}
430 #ifdef MROUTING
431 		else {
432 			/*
433 			 * If we are acting as a multicast router, perform
434 			 * multicast forwarding as if the packet had just
435 			 * arrived on the interface to which we are about
436 			 * to send.  The multicast forwarding function
437 			 * recursively calls this function, using the
438 			 * IP_FORWARDING flag to prevent infinite recursion.
439 			 *
440 			 * Multicasts that are looped back by ip_mloopback(),
441 			 * above, will be forwarded by the ip_input() routine,
442 			 * if necessary.
443 			 */
444 			extern struct socket *ip_mrouter;
445 
446 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
447 				if (ip_mforward(m, ifp) != 0) {
448 					m_freem(m);
449 					goto done;
450 				}
451 			}
452 		}
453 #endif
454 		/*
455 		 * Multicasts with a time-to-live of zero may be looped-
456 		 * back, above, but must not be transmitted on a network.
457 		 * Also, multicasts addressed to the loopback interface
458 		 * are not sent -- the above call to ip_mloopback() will
459 		 * loop back a copy if this host actually belongs to the
460 		 * destination group on the loopback interface.
461 		 */
462 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
463 			m_freem(m);
464 			goto done;
465 		}
466 		goto sendit;
467 	}
468 
469 	/*
470 	 * If source address not specified yet, use address
471 	 * of outgoing interface.
472 	 */
473 	if (in_nullhost(ip->ip_src)) {
474 		struct ifaddr *xifa;
475 
476 		xifa = &ia->ia_ifa;
477 		if (xifa->ifa_getifa != NULL) {
478 			ia = ifatoia((*xifa->ifa_getifa)(xifa, rdst));
479 			if (ia == NULL) {
480 				error = EADDRNOTAVAIL;
481 				goto bad;
482 			}
483 		}
484 		ip->ip_src = ia->ia_addr.sin_addr;
485 	}
486 
487 	/*
488 	 * packets with Class-D address as source are not valid per
489 	 * RFC 1112
490 	 */
491 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
492 		IP_STATINC(IP_STAT_ODROPPED);
493 		error = EADDRNOTAVAIL;
494 		goto bad;
495 	}
496 
497 	/*
498 	 * Look for broadcast address and and verify user is allowed to
499 	 * send such a packet.
500 	 */
501 	if (isbroadcast) {
502 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
503 			error = EADDRNOTAVAIL;
504 			goto bad;
505 		}
506 		if ((flags & IP_ALLOWBROADCAST) == 0) {
507 			error = EACCES;
508 			goto bad;
509 		}
510 		/* don't allow broadcast messages to be fragmented */
511 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
512 			error = EMSGSIZE;
513 			goto bad;
514 		}
515 		m->m_flags |= M_BCAST;
516 	} else
517 		m->m_flags &= ~M_BCAST;
518 
519 sendit:
520 	if ((flags & (IP_FORWARDING|IP_NOIPNEWID)) == 0) {
521 		if (m->m_pkthdr.len < IP_MINFRAGSIZE) {
522 			ip->ip_id = 0;
523 		} else if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
524 			ip->ip_id = ip_newid(ia);
525 		} else {
526 
527 			/*
528 			 * TSO capable interfaces (typically?) increment
529 			 * ip_id for each segment.
530 			 * "allocate" enough ids here to increase the chance
531 			 * for them to be unique.
532 			 *
533 			 * note that the following calculation is not
534 			 * needed to be precise.  wasting some ip_id is fine.
535 			 */
536 
537 			unsigned int segsz = m->m_pkthdr.segsz;
538 			unsigned int datasz = ntohs(ip->ip_len) - hlen;
539 			unsigned int num = howmany(datasz, segsz);
540 
541 			ip->ip_id = ip_newid_range(ia, num);
542 		}
543 	}
544 
545 	/*
546 	 * If we're doing Path MTU Discovery, we need to set DF unless
547 	 * the route's MTU is locked.
548 	 */
549 	if ((flags & IP_MTUDISC) != 0 && rtmtu_nolock) {
550 		ip->ip_off |= htons(IP_DF);
551 	}
552 
553 #ifdef IPSEC
554 	if (ipsec_used) {
555 		bool ipsec_done = false;
556 
557 		/* Perform IPsec processing, if any. */
558 		error = ipsec4_output(m, so, flags, &sp, &mtu, &natt_frag,
559 		    &ipsec_done);
560 		if (error || ipsec_done)
561 			goto done;
562 	}
563 #endif
564 
565 	/*
566 	 * Run through list of hooks for output packets.
567 	 */
568 	error = pfil_run_hooks(inet_pfil_hook, &m, ifp, PFIL_OUT);
569 	if (error)
570 		goto done;
571 	if (m == NULL)
572 		goto done;
573 
574 	ip = mtod(m, struct ip *);
575 	hlen = ip->ip_hl << 2;
576 
577 	m->m_pkthdr.csum_data |= hlen << 16;
578 
579 #if IFA_STATS
580 	/*
581 	 * search for the source address structure to
582 	 * maintain output statistics.
583 	 */
584 	INADDR_TO_IA(ip->ip_src, ia);
585 #endif
586 
587 	/* Maybe skip checksums on loopback interfaces. */
588 	if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
589 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
590 	}
591 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
592 	/*
593 	 * If small enough for mtu of path, or if using TCP segmentation
594 	 * offload, can just send directly.
595 	 */
596 	if (ntohs(ip->ip_len) <= mtu ||
597 	    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
598 		const struct sockaddr *sa;
599 
600 #if IFA_STATS
601 		if (ia)
602 			ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
603 #endif
604 		/*
605 		 * Always initialize the sum to 0!  Some HW assisted
606 		 * checksumming requires this.
607 		 */
608 		ip->ip_sum = 0;
609 
610 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
611 			/*
612 			 * Perform any checksums that the hardware can't do
613 			 * for us.
614 			 *
615 			 * XXX Does any hardware require the {th,uh}_sum
616 			 * XXX fields to be 0?
617 			 */
618 			if (sw_csum & M_CSUM_IPv4) {
619 				KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
620 				ip->ip_sum = in_cksum(m, hlen);
621 				m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
622 			}
623 			if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
624 				if (IN_NEED_CHECKSUM(ifp,
625 				    sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
626 					in_delayed_cksum(m);
627 				}
628 				m->m_pkthdr.csum_flags &=
629 				    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
630 			}
631 		}
632 
633 		sa = (m->m_flags & M_MCAST) ? sintocsa(rdst) : sintocsa(dst);
634 		if (__predict_true(
635 		    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 ||
636 		    (ifp->if_capenable & IFCAP_TSOv4) != 0)) {
637 			error = ip_if_output(ifp, m, sa, rt);
638 		} else {
639 			error = ip_tso_output(ifp, m, sa, rt);
640 		}
641 		goto done;
642 	}
643 
644 	/*
645 	 * We can't use HW checksumming if we're about to
646 	 * to fragment the packet.
647 	 *
648 	 * XXX Some hardware can do this.
649 	 */
650 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
651 		if (IN_NEED_CHECKSUM(ifp,
652 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
653 			in_delayed_cksum(m);
654 		}
655 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
656 	}
657 
658 	/*
659 	 * Too large for interface; fragment if possible.
660 	 * Must be able to put at least 8 bytes per fragment.
661 	 */
662 	if (ntohs(ip->ip_off) & IP_DF) {
663 		if (flags & IP_RETURNMTU) {
664 			struct inpcb *inp;
665 
666 			KASSERT(so && solocked(so));
667 			inp = sotoinpcb(so);
668 			inp->inp_errormtu = mtu;
669 		}
670 		error = EMSGSIZE;
671 		IP_STATINC(IP_STAT_CANTFRAG);
672 		goto bad;
673 	}
674 
675 	error = ip_fragment(m, ifp, mtu);
676 	if (error) {
677 		m = NULL;
678 		goto bad;
679 	}
680 
681 	for (; m; m = m0) {
682 		m0 = m->m_nextpkt;
683 		m->m_nextpkt = 0;
684 		if (error) {
685 			m_freem(m);
686 			continue;
687 		}
688 #if IFA_STATS
689 		if (ia)
690 			ia->ia_ifa.ifa_data.ifad_outbytes += ntohs(ip->ip_len);
691 #endif
692 		/*
693 		 * If we get there, the packet has not been handled by
694 		 * IPsec whereas it should have. Now that it has been
695 		 * fragmented, re-inject it in ip_output so that IPsec
696 		 * processing can occur.
697 		 */
698 		if (natt_frag) {
699 			error = ip_output(m, opt, ro,
700 			    flags | IP_RAWOUTPUT | IP_NOIPNEWID,
701 			    imo, so);
702 		} else {
703 			KASSERT((m->m_pkthdr.csum_flags &
704 			    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
705 			error = ip_if_output(ifp, m,
706 			    (m->m_flags & M_MCAST) ?
707 			    sintocsa(rdst) : sintocsa(dst), rt);
708 		}
709 	}
710 	if (error == 0) {
711 		IP_STATINC(IP_STAT_FRAGMENTED);
712 	}
713 done:
714 	if (ro == &iproute) {
715 		rtcache_free(&iproute);
716 	}
717 #ifdef IPSEC
718 	if (sp) {
719 		KEY_FREESP(&sp);
720 	}
721 #endif
722 	return error;
723 bad:
724 	m_freem(m);
725 	goto done;
726 }
727 
728 int
729 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
730 {
731 	struct ip *ip, *mhip;
732 	struct mbuf *m0;
733 	int len, hlen, off;
734 	int mhlen, firstlen;
735 	struct mbuf **mnext;
736 	int sw_csum = m->m_pkthdr.csum_flags;
737 	int fragments = 0;
738 	int s;
739 	int error = 0;
740 
741 	ip = mtod(m, struct ip *);
742 	hlen = ip->ip_hl << 2;
743 	if (ifp != NULL)
744 		sw_csum &= ~ifp->if_csum_flags_tx;
745 
746 	len = (mtu - hlen) &~ 7;
747 	if (len < 8) {
748 		m_freem(m);
749 		return (EMSGSIZE);
750 	}
751 
752 	firstlen = len;
753 	mnext = &m->m_nextpkt;
754 
755 	/*
756 	 * Loop through length of segment after first fragment,
757 	 * make new header and copy data of each part and link onto chain.
758 	 */
759 	m0 = m;
760 	mhlen = sizeof (struct ip);
761 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
762 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
763 		if (m == 0) {
764 			error = ENOBUFS;
765 			IP_STATINC(IP_STAT_ODROPPED);
766 			goto sendorfree;
767 		}
768 		MCLAIM(m, m0->m_owner);
769 		*mnext = m;
770 		mnext = &m->m_nextpkt;
771 		m->m_data += max_linkhdr;
772 		mhip = mtod(m, struct ip *);
773 		*mhip = *ip;
774 		/* we must inherit MCAST and BCAST flags */
775 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
776 		if (hlen > sizeof (struct ip)) {
777 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
778 			mhip->ip_hl = mhlen >> 2;
779 		}
780 		m->m_len = mhlen;
781 		mhip->ip_off = ((off - hlen) >> 3) +
782 		    (ntohs(ip->ip_off) & ~IP_MF);
783 		if (ip->ip_off & htons(IP_MF))
784 			mhip->ip_off |= IP_MF;
785 		if (off + len >= ntohs(ip->ip_len))
786 			len = ntohs(ip->ip_len) - off;
787 		else
788 			mhip->ip_off |= IP_MF;
789 		HTONS(mhip->ip_off);
790 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
791 		m->m_next = m_copym(m0, off, len, M_DONTWAIT);
792 		if (m->m_next == 0) {
793 			error = ENOBUFS;	/* ??? */
794 			IP_STATINC(IP_STAT_ODROPPED);
795 			goto sendorfree;
796 		}
797 		m->m_pkthdr.len = mhlen + len;
798 		m->m_pkthdr.rcvif = NULL;
799 		mhip->ip_sum = 0;
800 		KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
801 		if (sw_csum & M_CSUM_IPv4) {
802 			mhip->ip_sum = in_cksum(m, mhlen);
803 		} else {
804 			/*
805 			 * checksum is hw-offloaded or not necessary.
806 			 */
807 			m->m_pkthdr.csum_flags |=
808 			    m0->m_pkthdr.csum_flags & M_CSUM_IPv4;
809 			m->m_pkthdr.csum_data |= mhlen << 16;
810 			KASSERT(!(ifp != NULL &&
811 			    IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
812 			    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
813 		}
814 		IP_STATINC(IP_STAT_OFRAGMENTS);
815 		fragments++;
816 	}
817 	/*
818 	 * Update first fragment by trimming what's been copied out
819 	 * and updating header, then send each fragment (in order).
820 	 */
821 	m = m0;
822 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
823 	m->m_pkthdr.len = hlen + firstlen;
824 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
825 	ip->ip_off |= htons(IP_MF);
826 	ip->ip_sum = 0;
827 	if (sw_csum & M_CSUM_IPv4) {
828 		ip->ip_sum = in_cksum(m, hlen);
829 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
830 	} else {
831 		/*
832 		 * checksum is hw-offloaded or not necessary.
833 		 */
834 		KASSERT(!(ifp != NULL && IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) ||
835 		    (m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0);
836 		KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
837 		    sizeof(struct ip));
838 	}
839 sendorfree:
840 	/*
841 	 * If there is no room for all the fragments, don't queue
842 	 * any of them.
843 	 */
844 	if (ifp != NULL) {
845 		s = splnet();
846 		if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
847 		    error == 0) {
848 			error = ENOBUFS;
849 			IP_STATINC(IP_STAT_ODROPPED);
850 			IFQ_INC_DROPS(&ifp->if_snd);
851 		}
852 		splx(s);
853 	}
854 	if (error) {
855 		for (m = m0; m; m = m0) {
856 			m0 = m->m_nextpkt;
857 			m->m_nextpkt = NULL;
858 			m_freem(m);
859 		}
860 	}
861 	return (error);
862 }
863 
864 /*
865  * Process a delayed payload checksum calculation.
866  */
867 void
868 in_delayed_cksum(struct mbuf *m)
869 {
870 	struct ip *ip;
871 	u_int16_t csum, offset;
872 
873 	ip = mtod(m, struct ip *);
874 	offset = ip->ip_hl << 2;
875 	csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
876 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
877 		csum = 0xffff;
878 
879 	offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
880 
881 	if ((offset + sizeof(u_int16_t)) > m->m_len) {
882 		/* This happen when ip options were inserted
883 		printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
884 		    m->m_len, offset, ip->ip_p);
885 		 */
886 		m_copyback(m, offset, sizeof(csum), (void *) &csum);
887 	} else
888 		*(u_int16_t *)(mtod(m, char *) + offset) = csum;
889 }
890 
891 /*
892  * Determine the maximum length of the options to be inserted;
893  * we would far rather allocate too much space rather than too little.
894  */
895 
896 u_int
897 ip_optlen(struct inpcb *inp)
898 {
899 	struct mbuf *m = inp->inp_options;
900 
901 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) {
902 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
903 	}
904 	return 0;
905 }
906 
907 /*
908  * Insert IP options into preformed packet.
909  * Adjust IP destination as required for IP source routing,
910  * as indicated by a non-zero in_addr at the start of the options.
911  */
912 static struct mbuf *
913 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
914 {
915 	struct ipoption *p = mtod(opt, struct ipoption *);
916 	struct mbuf *n;
917 	struct ip *ip = mtod(m, struct ip *);
918 	unsigned optlen;
919 
920 	optlen = opt->m_len - sizeof(p->ipopt_dst);
921 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
922 		return (m);		/* XXX should fail */
923 	if (!in_nullhost(p->ipopt_dst))
924 		ip->ip_dst = p->ipopt_dst;
925 	if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
926 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
927 		if (n == 0)
928 			return (m);
929 		MCLAIM(n, m->m_owner);
930 		M_MOVE_PKTHDR(n, m);
931 		m->m_len -= sizeof(struct ip);
932 		m->m_data += sizeof(struct ip);
933 		n->m_next = m;
934 		m = n;
935 		m->m_len = optlen + sizeof(struct ip);
936 		m->m_data += max_linkhdr;
937 		bcopy((void *)ip, mtod(m, void *), sizeof(struct ip));
938 	} else {
939 		m->m_data -= optlen;
940 		m->m_len += optlen;
941 		memmove(mtod(m, void *), ip, sizeof(struct ip));
942 	}
943 	m->m_pkthdr.len += optlen;
944 	ip = mtod(m, struct ip *);
945 	bcopy((void *)p->ipopt_list, (void *)(ip + 1), (unsigned)optlen);
946 	*phlen = sizeof(struct ip) + optlen;
947 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
948 	return (m);
949 }
950 
951 /*
952  * Copy options from ip to jp,
953  * omitting those not copied during fragmentation.
954  */
955 int
956 ip_optcopy(struct ip *ip, struct ip *jp)
957 {
958 	u_char *cp, *dp;
959 	int opt, optlen, cnt;
960 
961 	cp = (u_char *)(ip + 1);
962 	dp = (u_char *)(jp + 1);
963 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
964 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
965 		opt = cp[0];
966 		if (opt == IPOPT_EOL)
967 			break;
968 		if (opt == IPOPT_NOP) {
969 			/* Preserve for IP mcast tunnel's LSRR alignment. */
970 			*dp++ = IPOPT_NOP;
971 			optlen = 1;
972 			continue;
973 		}
974 
975 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp));
976 		optlen = cp[IPOPT_OLEN];
977 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen < cnt);
978 
979 		/* Invalid lengths should have been caught by ip_dooptions. */
980 		if (optlen > cnt)
981 			optlen = cnt;
982 		if (IPOPT_COPIED(opt)) {
983 			bcopy((void *)cp, (void *)dp, (unsigned)optlen);
984 			dp += optlen;
985 		}
986 	}
987 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
988 		*dp++ = IPOPT_EOL;
989 	return (optlen);
990 }
991 
992 /*
993  * IP socket option processing.
994  */
995 int
996 ip_ctloutput(int op, struct socket *so, struct sockopt *sopt)
997 {
998 	struct inpcb *inp = sotoinpcb(so);
999 	struct ip *ip = &inp->inp_ip;
1000 	int inpflags = inp->inp_flags;
1001 	int optval = 0, error = 0;
1002 
1003 	if (sopt->sopt_level != IPPROTO_IP) {
1004 		if (sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_NOHEADER)
1005 			return 0;
1006 		return ENOPROTOOPT;
1007 	}
1008 
1009 	switch (op) {
1010 	case PRCO_SETOPT:
1011 		switch (sopt->sopt_name) {
1012 		case IP_OPTIONS:
1013 #ifdef notyet
1014 		case IP_RETOPTS:
1015 #endif
1016 			error = ip_pcbopts(inp, sopt);
1017 			break;
1018 
1019 		case IP_TOS:
1020 		case IP_TTL:
1021 		case IP_MINTTL:
1022 		case IP_PKTINFO:
1023 		case IP_RECVOPTS:
1024 		case IP_RECVRETOPTS:
1025 		case IP_RECVDSTADDR:
1026 		case IP_RECVIF:
1027 		case IP_RECVPKTINFO:
1028 		case IP_RECVTTL:
1029 			error = sockopt_getint(sopt, &optval);
1030 			if (error)
1031 				break;
1032 
1033 			switch (sopt->sopt_name) {
1034 			case IP_TOS:
1035 				ip->ip_tos = optval;
1036 				break;
1037 
1038 			case IP_TTL:
1039 				ip->ip_ttl = optval;
1040 				break;
1041 
1042 			case IP_MINTTL:
1043 				if (optval > 0 && optval <= MAXTTL)
1044 					inp->inp_ip_minttl = optval;
1045 				else
1046 					error = EINVAL;
1047 				break;
1048 #define	OPTSET(bit) \
1049 	if (optval) \
1050 		inpflags |= bit; \
1051 	else \
1052 		inpflags &= ~bit;
1053 
1054 			case IP_PKTINFO:
1055 				OPTSET(INP_PKTINFO);
1056 				break;
1057 
1058 			case IP_RECVOPTS:
1059 				OPTSET(INP_RECVOPTS);
1060 				break;
1061 
1062 			case IP_RECVPKTINFO:
1063 				OPTSET(INP_RECVPKTINFO);
1064 				break;
1065 
1066 			case IP_RECVRETOPTS:
1067 				OPTSET(INP_RECVRETOPTS);
1068 				break;
1069 
1070 			case IP_RECVDSTADDR:
1071 				OPTSET(INP_RECVDSTADDR);
1072 				break;
1073 
1074 			case IP_RECVIF:
1075 				OPTSET(INP_RECVIF);
1076 				break;
1077 
1078 			case IP_RECVTTL:
1079 				OPTSET(INP_RECVTTL);
1080 				break;
1081 			}
1082 		break;
1083 #undef OPTSET
1084 
1085 		case IP_MULTICAST_IF:
1086 		case IP_MULTICAST_TTL:
1087 		case IP_MULTICAST_LOOP:
1088 		case IP_ADD_MEMBERSHIP:
1089 		case IP_DROP_MEMBERSHIP:
1090 			error = ip_setmoptions(&inp->inp_moptions, sopt);
1091 			break;
1092 
1093 		case IP_PORTRANGE:
1094 			error = sockopt_getint(sopt, &optval);
1095 			if (error)
1096 				break;
1097 
1098 			switch (optval) {
1099 			case IP_PORTRANGE_DEFAULT:
1100 			case IP_PORTRANGE_HIGH:
1101 				inpflags &= ~(INP_LOWPORT);
1102 				break;
1103 
1104 			case IP_PORTRANGE_LOW:
1105 				inpflags |= INP_LOWPORT;
1106 				break;
1107 
1108 			default:
1109 				error = EINVAL;
1110 				break;
1111 			}
1112 			break;
1113 
1114 		case IP_PORTALGO:
1115 			error = sockopt_getint(sopt, &optval);
1116 			if (error)
1117 				break;
1118 
1119 			error = portalgo_algo_index_select(
1120 			    (struct inpcb_hdr *)inp, optval);
1121 			break;
1122 
1123 #if defined(IPSEC)
1124 		case IP_IPSEC_POLICY:
1125 			if (ipsec_enabled) {
1126 				error = ipsec4_set_policy(inp, sopt->sopt_name,
1127 				    sopt->sopt_data, sopt->sopt_size,
1128 				    curlwp->l_cred);
1129 				break;
1130 			}
1131 			/*FALLTHROUGH*/
1132 #endif /* IPSEC */
1133 
1134 		default:
1135 			error = ENOPROTOOPT;
1136 			break;
1137 		}
1138 		break;
1139 
1140 	case PRCO_GETOPT:
1141 		switch (sopt->sopt_name) {
1142 		case IP_OPTIONS:
1143 		case IP_RETOPTS: {
1144 			struct mbuf *mopts = inp->inp_options;
1145 
1146 			if (mopts) {
1147 				struct mbuf *m;
1148 
1149 				m = m_copym(mopts, 0, M_COPYALL, M_DONTWAIT);
1150 				if (m == NULL) {
1151 					error = ENOBUFS;
1152 					break;
1153 				}
1154 				error = sockopt_setmbuf(sopt, m);
1155 			}
1156 			break;
1157 		}
1158 		case IP_PKTINFO:
1159 		case IP_TOS:
1160 		case IP_TTL:
1161 		case IP_MINTTL:
1162 		case IP_RECVOPTS:
1163 		case IP_RECVRETOPTS:
1164 		case IP_RECVDSTADDR:
1165 		case IP_RECVIF:
1166 		case IP_RECVPKTINFO:
1167 		case IP_RECVTTL:
1168 		case IP_ERRORMTU:
1169 			switch (sopt->sopt_name) {
1170 			case IP_TOS:
1171 				optval = ip->ip_tos;
1172 				break;
1173 
1174 			case IP_TTL:
1175 				optval = ip->ip_ttl;
1176 				break;
1177 
1178 			case IP_MINTTL:
1179 				optval = inp->inp_ip_minttl;
1180 				break;
1181 
1182 			case IP_ERRORMTU:
1183 				optval = inp->inp_errormtu;
1184 				break;
1185 
1186 #define	OPTBIT(bit)	(inpflags & bit ? 1 : 0)
1187 
1188 			case IP_PKTINFO:
1189 				optval = OPTBIT(INP_PKTINFO);
1190 				break;
1191 
1192 			case IP_RECVOPTS:
1193 				optval = OPTBIT(INP_RECVOPTS);
1194 				break;
1195 
1196 			case IP_RECVPKTINFO:
1197 				optval = OPTBIT(INP_RECVPKTINFO);
1198 				break;
1199 
1200 			case IP_RECVRETOPTS:
1201 				optval = OPTBIT(INP_RECVRETOPTS);
1202 				break;
1203 
1204 			case IP_RECVDSTADDR:
1205 				optval = OPTBIT(INP_RECVDSTADDR);
1206 				break;
1207 
1208 			case IP_RECVIF:
1209 				optval = OPTBIT(INP_RECVIF);
1210 				break;
1211 
1212 			case IP_RECVTTL:
1213 				optval = OPTBIT(INP_RECVTTL);
1214 				break;
1215 			}
1216 			error = sockopt_setint(sopt, optval);
1217 			break;
1218 
1219 #if 0	/* defined(IPSEC) */
1220 		case IP_IPSEC_POLICY:
1221 		{
1222 			struct mbuf *m = NULL;
1223 
1224 			/* XXX this will return EINVAL as sopt is empty */
1225 			error = ipsec4_get_policy(inp, sopt->sopt_data,
1226 			    sopt->sopt_size, &m);
1227 			if (error == 0)
1228 				error = sockopt_setmbuf(sopt, m);
1229 			break;
1230 		}
1231 #endif /*IPSEC*/
1232 
1233 		case IP_MULTICAST_IF:
1234 		case IP_MULTICAST_TTL:
1235 		case IP_MULTICAST_LOOP:
1236 		case IP_ADD_MEMBERSHIP:
1237 		case IP_DROP_MEMBERSHIP:
1238 			error = ip_getmoptions(inp->inp_moptions, sopt);
1239 			break;
1240 
1241 		case IP_PORTRANGE:
1242 			if (inpflags & INP_LOWPORT)
1243 				optval = IP_PORTRANGE_LOW;
1244 			else
1245 				optval = IP_PORTRANGE_DEFAULT;
1246 			error = sockopt_setint(sopt, optval);
1247 			break;
1248 
1249 		case IP_PORTALGO:
1250 			optval = inp->inp_portalgo;
1251 			error = sockopt_setint(sopt, optval);
1252 			break;
1253 
1254 		default:
1255 			error = ENOPROTOOPT;
1256 			break;
1257 		}
1258 		break;
1259 	}
1260 
1261 	if (!error) {
1262 		inp->inp_flags = inpflags;
1263 	}
1264 	return error;
1265 }
1266 
1267 /*
1268  * Set up IP options in pcb for insertion in output packets.
1269  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1270  * with destination address if source routed.
1271  */
1272 static int
1273 ip_pcbopts(struct inpcb *inp, const struct sockopt *sopt)
1274 {
1275 	struct mbuf *m;
1276 	const u_char *cp;
1277 	u_char *dp;
1278 	int cnt;
1279 
1280 	/* Turn off any old options. */
1281 	if (inp->inp_options) {
1282 		m_free(inp->inp_options);
1283 	}
1284 	inp->inp_options = NULL;
1285 	if ((cnt = sopt->sopt_size) == 0) {
1286 		/* Only turning off any previous options. */
1287 		return 0;
1288 	}
1289 	cp = sopt->sopt_data;
1290 
1291 #ifndef	__vax__
1292 	if (cnt % sizeof(int32_t))
1293 		return (EINVAL);
1294 #endif
1295 
1296 	m = m_get(M_DONTWAIT, MT_SOOPTS);
1297 	if (m == NULL)
1298 		return (ENOBUFS);
1299 
1300 	dp = mtod(m, u_char *);
1301 	memset(dp, 0, sizeof(struct in_addr));
1302 	dp += sizeof(struct in_addr);
1303 	m->m_len = sizeof(struct in_addr);
1304 
1305 	/*
1306 	 * IP option list according to RFC791. Each option is of the form
1307 	 *
1308 	 *	[optval] [olen] [(olen - 2) data bytes]
1309 	 *
1310 	 * We validate the list and copy options to an mbuf for prepending
1311 	 * to data packets. The IP first-hop destination address will be
1312 	 * stored before actual options and is zero if unset.
1313 	 */
1314 	while (cnt > 0) {
1315 		uint8_t optval, olen, offset;
1316 
1317 		optval = cp[IPOPT_OPTVAL];
1318 
1319 		if (optval == IPOPT_EOL || optval == IPOPT_NOP) {
1320 			olen = 1;
1321 		} else {
1322 			if (cnt < IPOPT_OLEN + 1)
1323 				goto bad;
1324 
1325 			olen = cp[IPOPT_OLEN];
1326 			if (olen < IPOPT_OLEN + 1 || olen > cnt)
1327 				goto bad;
1328 		}
1329 
1330 		if (optval == IPOPT_LSRR || optval == IPOPT_SSRR) {
1331 			/*
1332 			 * user process specifies route as:
1333 			 *	->A->B->C->D
1334 			 * D must be our final destination (but we can't
1335 			 * check that since we may not have connected yet).
1336 			 * A is first hop destination, which doesn't appear in
1337 			 * actual IP option, but is stored before the options.
1338 			 */
1339 			if (olen < IPOPT_OFFSET + 1 + sizeof(struct in_addr))
1340 				goto bad;
1341 
1342 			offset = cp[IPOPT_OFFSET];
1343 			memcpy(mtod(m, u_char *), cp + IPOPT_OFFSET + 1,
1344 			    sizeof(struct in_addr));
1345 
1346 			cp += sizeof(struct in_addr);
1347 			cnt -= sizeof(struct in_addr);
1348 			olen -= sizeof(struct in_addr);
1349 
1350 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1351 				goto bad;
1352 
1353 			memcpy(dp, cp, olen);
1354 			dp[IPOPT_OPTVAL] = optval;
1355 			dp[IPOPT_OLEN] = olen;
1356 			dp[IPOPT_OFFSET] = offset;
1357 			break;
1358 		} else {
1359 			if (m->m_len + olen > MAX_IPOPTLEN + sizeof(struct in_addr))
1360 				goto bad;
1361 
1362 			memcpy(dp, cp, olen);
1363 			break;
1364 		}
1365 
1366 		dp += olen;
1367 		m->m_len += olen;
1368 
1369 		if (optval == IPOPT_EOL)
1370 			break;
1371 
1372 		cp += olen;
1373 		cnt -= olen;
1374 	}
1375 
1376 	inp->inp_options = m;
1377 	return 0;
1378 bad:
1379 	(void)m_free(m);
1380 	return EINVAL;
1381 }
1382 
1383 /*
1384  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1385  */
1386 static struct ifnet *
1387 ip_multicast_if(struct in_addr *a, int *ifindexp)
1388 {
1389 	int ifindex;
1390 	struct ifnet *ifp = NULL;
1391 	struct in_ifaddr *ia;
1392 
1393 	if (ifindexp)
1394 		*ifindexp = 0;
1395 	if (ntohl(a->s_addr) >> 24 == 0) {
1396 		ifindex = ntohl(a->s_addr) & 0xffffff;
1397 		ifp = if_byindex(ifindex);
1398 		if (!ifp)
1399 			return NULL;
1400 		if (ifindexp)
1401 			*ifindexp = ifindex;
1402 	} else {
1403 		LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1404 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1405 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1406 				ifp = ia->ia_ifp;
1407 				break;
1408 			}
1409 		}
1410 	}
1411 	return ifp;
1412 }
1413 
1414 static int
1415 ip_getoptval(const struct sockopt *sopt, u_int8_t *val, u_int maxval)
1416 {
1417 	u_int tval;
1418 	u_char cval;
1419 	int error;
1420 
1421 	if (sopt == NULL)
1422 		return EINVAL;
1423 
1424 	switch (sopt->sopt_size) {
1425 	case sizeof(u_char):
1426 		error = sockopt_get(sopt, &cval, sizeof(u_char));
1427 		tval = cval;
1428 		break;
1429 
1430 	case sizeof(u_int):
1431 		error = sockopt_get(sopt, &tval, sizeof(u_int));
1432 		break;
1433 
1434 	default:
1435 		error = EINVAL;
1436 	}
1437 
1438 	if (error)
1439 		return error;
1440 
1441 	if (tval > maxval)
1442 		return EINVAL;
1443 
1444 	*val = tval;
1445 	return 0;
1446 }
1447 
1448 static int
1449 ip_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
1450     struct in_addr *ia, bool add)
1451 {
1452 	int error;
1453 	struct ip_mreq mreq;
1454 
1455 	error = sockopt_get(sopt, &mreq, sizeof(mreq));
1456 	if (error)
1457 		return error;
1458 
1459 	if (!IN_MULTICAST(mreq.imr_multiaddr.s_addr))
1460 		return EINVAL;
1461 
1462 	memcpy(ia, &mreq.imr_multiaddr, sizeof(*ia));
1463 
1464 	if (in_nullhost(mreq.imr_interface)) {
1465 		union {
1466 			struct sockaddr		dst;
1467 			struct sockaddr_in	dst4;
1468 		} u;
1469 		struct route ro;
1470 
1471 		if (!add) {
1472 			*ifp = NULL;
1473 			return 0;
1474 		}
1475 		/*
1476 		 * If no interface address was provided, use the interface of
1477 		 * the route to the given multicast address.
1478 		 */
1479 		struct rtentry *rt;
1480 		memset(&ro, 0, sizeof(ro));
1481 
1482 		sockaddr_in_init(&u.dst4, ia, 0);
1483 		error = rtcache_setdst(&ro, &u.dst);
1484 		if (error != 0)
1485 			return error;
1486 		*ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp : NULL;
1487 		rtcache_free(&ro);
1488 	} else {
1489 		*ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1490 		if (!add && *ifp == NULL)
1491 			return EADDRNOTAVAIL;
1492 	}
1493 	return 0;
1494 }
1495 
1496 /*
1497  * Add a multicast group membership.
1498  * Group must be a valid IP multicast address.
1499  */
1500 static int
1501 ip_add_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1502 {
1503 	struct ifnet *ifp = NULL;	// XXX: gcc [ppc]
1504 	struct in_addr ia;
1505 	int i, error;
1506 
1507 	if (sopt->sopt_size == sizeof(struct ip_mreq))
1508 		error = ip_get_membership(sopt, &ifp, &ia, true);
1509 	else
1510 #ifdef INET6
1511 		error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia));
1512 #else
1513 		return EINVAL;
1514 #endif
1515 
1516 	if (error)
1517 		return error;
1518 
1519 	/*
1520 	 * See if we found an interface, and confirm that it
1521 	 * supports multicast.
1522 	 */
1523 	if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1524 		return EADDRNOTAVAIL;
1525 
1526 	/*
1527 	 * See if the membership already exists or if all the
1528 	 * membership slots are full.
1529 	 */
1530 	for (i = 0; i < imo->imo_num_memberships; ++i) {
1531 		if (imo->imo_membership[i]->inm_ifp == ifp &&
1532 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1533 			break;
1534 	}
1535 	if (i < imo->imo_num_memberships)
1536 		return EADDRINUSE;
1537 
1538 	if (i == IP_MAX_MEMBERSHIPS)
1539 		return ETOOMANYREFS;
1540 
1541 	/*
1542 	 * Everything looks good; add a new record to the multicast
1543 	 * address list for the given interface.
1544 	 */
1545 	if ((imo->imo_membership[i] = in_addmulti(&ia, ifp)) == NULL)
1546 		return ENOBUFS;
1547 
1548 	++imo->imo_num_memberships;
1549 	return 0;
1550 }
1551 
1552 /*
1553  * Drop a multicast group membership.
1554  * Group must be a valid IP multicast address.
1555  */
1556 static int
1557 ip_drop_membership(struct ip_moptions *imo, const struct sockopt *sopt)
1558 {
1559 	struct in_addr ia = { .s_addr = 0 };	// XXX: gcc [ppc]
1560 	struct ifnet *ifp = NULL;		// XXX: gcc [ppc]
1561 	int i, error;
1562 
1563 	if (sopt->sopt_size == sizeof(struct ip_mreq))
1564 		error = ip_get_membership(sopt, &ifp, &ia, false);
1565 	else
1566 #ifdef INET6
1567 		error = ip6_get_membership(sopt, &ifp, &ia, sizeof(ia));
1568 #else
1569 		return EINVAL;
1570 #endif
1571 
1572 	if (error)
1573 		return error;
1574 
1575 	/*
1576 	 * Find the membership in the membership array.
1577 	 */
1578 	for (i = 0; i < imo->imo_num_memberships; ++i) {
1579 		if ((ifp == NULL ||
1580 		     imo->imo_membership[i]->inm_ifp == ifp) &&
1581 		    in_hosteq(imo->imo_membership[i]->inm_addr, ia))
1582 			break;
1583 	}
1584 	if (i == imo->imo_num_memberships)
1585 		return EADDRNOTAVAIL;
1586 
1587 	/*
1588 	 * Give up the multicast address record to which the
1589 	 * membership points.
1590 	 */
1591 	in_delmulti(imo->imo_membership[i]);
1592 
1593 	/*
1594 	 * Remove the gap in the membership array.
1595 	 */
1596 	for (++i; i < imo->imo_num_memberships; ++i)
1597 		imo->imo_membership[i-1] = imo->imo_membership[i];
1598 	--imo->imo_num_memberships;
1599 	return 0;
1600 }
1601 
1602 /*
1603  * Set the IP multicast options in response to user setsockopt().
1604  */
1605 int
1606 ip_setmoptions(struct ip_moptions **pimo, const struct sockopt *sopt)
1607 {
1608 	struct ip_moptions *imo = *pimo;
1609 	struct in_addr addr;
1610 	struct ifnet *ifp;
1611 	int ifindex, error = 0;
1612 
1613 	if (!imo) {
1614 		/*
1615 		 * No multicast option buffer attached to the pcb;
1616 		 * allocate one and initialize to default values.
1617 		 */
1618 		imo = kmem_intr_alloc(sizeof(*imo), KM_NOSLEEP);
1619 		if (imo == NULL)
1620 			return ENOBUFS;
1621 
1622 		imo->imo_multicast_ifp = NULL;
1623 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1624 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1625 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1626 		imo->imo_num_memberships = 0;
1627 		*pimo = imo;
1628 	}
1629 
1630 	switch (sopt->sopt_name) {
1631 	case IP_MULTICAST_IF:
1632 		/*
1633 		 * Select the interface for outgoing multicast packets.
1634 		 */
1635 		error = sockopt_get(sopt, &addr, sizeof(addr));
1636 		if (error)
1637 			break;
1638 
1639 		/*
1640 		 * INADDR_ANY is used to remove a previous selection.
1641 		 * When no interface is selected, a default one is
1642 		 * chosen every time a multicast packet is sent.
1643 		 */
1644 		if (in_nullhost(addr)) {
1645 			imo->imo_multicast_ifp = NULL;
1646 			break;
1647 		}
1648 		/*
1649 		 * The selected interface is identified by its local
1650 		 * IP address.  Find the interface and confirm that
1651 		 * it supports multicasting.
1652 		 */
1653 		ifp = ip_multicast_if(&addr, &ifindex);
1654 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1655 			error = EADDRNOTAVAIL;
1656 			break;
1657 		}
1658 		imo->imo_multicast_ifp = ifp;
1659 		if (ifindex)
1660 			imo->imo_multicast_addr = addr;
1661 		else
1662 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1663 		break;
1664 
1665 	case IP_MULTICAST_TTL:
1666 		/*
1667 		 * Set the IP time-to-live for outgoing multicast packets.
1668 		 */
1669 		error = ip_getoptval(sopt, &imo->imo_multicast_ttl, MAXTTL);
1670 		break;
1671 
1672 	case IP_MULTICAST_LOOP:
1673 		/*
1674 		 * Set the loopback flag for outgoing multicast packets.
1675 		 * Must be zero or one.
1676 		 */
1677 		error = ip_getoptval(sopt, &imo->imo_multicast_loop, 1);
1678 		break;
1679 
1680 	case IP_ADD_MEMBERSHIP: /* IPV6_JOIN_GROUP */
1681 		error = ip_add_membership(imo, sopt);
1682 		break;
1683 
1684 	case IP_DROP_MEMBERSHIP: /* IPV6_LEAVE_GROUP */
1685 		error = ip_drop_membership(imo, sopt);
1686 		break;
1687 
1688 	default:
1689 		error = EOPNOTSUPP;
1690 		break;
1691 	}
1692 
1693 	/*
1694 	 * If all options have default values, no need to keep the mbuf.
1695 	 */
1696 	if (imo->imo_multicast_ifp == NULL &&
1697 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1698 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1699 	    imo->imo_num_memberships == 0) {
1700 		kmem_free(imo, sizeof(*imo));
1701 		*pimo = NULL;
1702 	}
1703 
1704 	return error;
1705 }
1706 
1707 /*
1708  * Return the IP multicast options in response to user getsockopt().
1709  */
1710 int
1711 ip_getmoptions(struct ip_moptions *imo, struct sockopt *sopt)
1712 {
1713 	struct in_addr addr;
1714 	struct in_ifaddr *ia;
1715 	uint8_t optval;
1716 	int error = 0;
1717 
1718 	switch (sopt->sopt_name) {
1719 	case IP_MULTICAST_IF:
1720 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1721 			addr = zeroin_addr;
1722 		else if (imo->imo_multicast_addr.s_addr) {
1723 			/* return the value user has set */
1724 			addr = imo->imo_multicast_addr;
1725 		} else {
1726 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1727 			addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1728 		}
1729 		error = sockopt_set(sopt, &addr, sizeof(addr));
1730 		break;
1731 
1732 	case IP_MULTICAST_TTL:
1733 		optval = imo ? imo->imo_multicast_ttl
1734 		    : IP_DEFAULT_MULTICAST_TTL;
1735 
1736 		error = sockopt_set(sopt, &optval, sizeof(optval));
1737 		break;
1738 
1739 	case IP_MULTICAST_LOOP:
1740 		optval = imo ? imo->imo_multicast_loop
1741 		    : IP_DEFAULT_MULTICAST_LOOP;
1742 
1743 		error = sockopt_set(sopt, &optval, sizeof(optval));
1744 		break;
1745 
1746 	default:
1747 		error = EOPNOTSUPP;
1748 	}
1749 
1750 	return error;
1751 }
1752 
1753 /*
1754  * Discard the IP multicast options.
1755  */
1756 void
1757 ip_freemoptions(struct ip_moptions *imo)
1758 {
1759 	int i;
1760 
1761 	if (imo != NULL) {
1762 		for (i = 0; i < imo->imo_num_memberships; ++i)
1763 			in_delmulti(imo->imo_membership[i]);
1764 		kmem_free(imo, sizeof(*imo));
1765 	}
1766 }
1767 
1768 /*
1769  * Routine called from ip_output() to loop back a copy of an IP multicast
1770  * packet to the input queue of a specified interface.  Note that this
1771  * calls the output routine of the loopback "driver", but with an interface
1772  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
1773  */
1774 static void
1775 ip_mloopback(struct ifnet *ifp, struct mbuf *m, const struct sockaddr_in *dst)
1776 {
1777 	struct ip *ip;
1778 	struct mbuf *copym;
1779 
1780 	copym = m_copypacket(m, M_DONTWAIT);
1781 	if (copym != NULL &&
1782 	    (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1783 		copym = m_pullup(copym, sizeof(struct ip));
1784 	if (copym == NULL)
1785 		return;
1786 	/*
1787 	 * We don't bother to fragment if the IP length is greater
1788 	 * than the interface's MTU.  Can this possibly matter?
1789 	 */
1790 	ip = mtod(copym, struct ip *);
1791 
1792 	if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1793 		in_delayed_cksum(copym);
1794 		copym->m_pkthdr.csum_flags &=
1795 		    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1796 	}
1797 
1798 	ip->ip_sum = 0;
1799 	ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1800 #ifndef NET_MPSAFE
1801 	KERNEL_LOCK(1, NULL);
1802 #endif
1803 	(void)looutput(ifp, copym, sintocsa(dst), NULL);
1804 #ifndef NET_MPSAFE
1805 	KERNEL_UNLOCK_ONE(NULL);
1806 #endif
1807 }
1808