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