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