xref: /netbsd-src/sys/netinet/ip_output.c (revision c0179c282a5968435315a82f4128c61372c68fc3)
1 /*	$NetBSD: ip_output.c,v 1.166 2006/11/13 05:13:42 dyoung 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  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by the NetBSD
51  *	Foundation, Inc. and its contributors.
52  * 4. Neither the name of The NetBSD Foundation nor the names of its
53  *    contributors may be used to endorse or promote products derived
54  *    from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66  * POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 /*
70  * Copyright (c) 1982, 1986, 1988, 1990, 1993
71  *	The Regents of the University of California.  All rights reserved.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  * 1. Redistributions of source code must retain the above copyright
77  *    notice, this list of conditions and the following disclaimer.
78  * 2. Redistributions in binary form must reproduce the above copyright
79  *    notice, this list of conditions and the following disclaimer in the
80  *    documentation and/or other materials provided with the distribution.
81  * 3. Neither the name of the University nor the names of its contributors
82  *    may be used to endorse or promote products derived from this software
83  *    without specific prior written permission.
84  *
85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95  * SUCH DAMAGE.
96  *
97  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
98  */
99 
100 #include <sys/cdefs.h>
101 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.166 2006/11/13 05:13:42 dyoung Exp $");
102 
103 #include "opt_pfil_hooks.h"
104 #include "opt_inet.h"
105 #include "opt_ipsec.h"
106 #include "opt_mrouting.h"
107 
108 #include <sys/param.h>
109 #include <sys/malloc.h>
110 #include <sys/mbuf.h>
111 #include <sys/errno.h>
112 #include <sys/protosw.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/kauth.h>
116 #ifdef FAST_IPSEC
117 #include <sys/domain.h>
118 #endif
119 #include <sys/systm.h>
120 #include <sys/proc.h>
121 
122 #include <net/if.h>
123 #include <net/route.h>
124 #include <net/pfil.h>
125 
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/in_pcb.h>
130 #include <netinet/in_var.h>
131 #include <netinet/ip_var.h>
132 #include <netinet/in_offload.h>
133 
134 #ifdef MROUTING
135 #include <netinet/ip_mroute.h>
136 #endif
137 
138 #include <machine/stdarg.h>
139 
140 #ifdef IPSEC
141 #include <netinet6/ipsec.h>
142 #include <netkey/key.h>
143 #include <netkey/key_debug.h>
144 #endif /*IPSEC*/
145 
146 #ifdef FAST_IPSEC
147 #include <netipsec/ipsec.h>
148 #include <netipsec/key.h>
149 #include <netipsec/xform.h>
150 #endif	/* FAST_IPSEC*/
151 
152 #ifdef IPSEC_NAT_T
153 #include <netinet/udp.h>
154 #endif
155 
156 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
157 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
158 static void ip_mloopback(struct ifnet *, struct mbuf *, struct sockaddr_in *);
159 static int ip_getoptval(struct mbuf *, u_int8_t *, u_int);
160 
161 #ifdef PFIL_HOOKS
162 extern struct pfil_head inet_pfil_hook;			/* XXX */
163 #endif
164 
165 int	ip_do_loopback_cksum = 0;
166 
167 #define	IN_NEED_CHECKSUM(ifp, csum_flags) \
168 	(__predict_true(((ifp)->if_flags & IFF_LOOPBACK) == 0 || \
169 	(((csum_flags) & M_CSUM_UDPv4) != 0 && udp_do_loopback_cksum) || \
170 	(((csum_flags) & M_CSUM_TCPv4) != 0 && tcp_do_loopback_cksum) || \
171 	(((csum_flags) & M_CSUM_IPv4) != 0 && ip_do_loopback_cksum)))
172 
173 struct ip_tso_output_args {
174 	struct ifnet *ifp;
175 	struct sockaddr *sa;
176 	struct rtentry *rt;
177 };
178 
179 static int ip_tso_output_callback(void *, struct mbuf *);
180 static int ip_tso_output(struct ifnet *, struct mbuf *, struct sockaddr *,
181     struct rtentry *);
182 
183 static int
184 ip_tso_output_callback(void *vp, struct mbuf *m)
185 {
186 	struct ip_tso_output_args *args = vp;
187 	struct ifnet *ifp = args->ifp;
188 
189 	return (*ifp->if_output)(ifp, m, args->sa, args->rt);
190 }
191 
192 static int
193 ip_tso_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
194     struct rtentry *rt)
195 {
196 	struct ip_tso_output_args args;
197 
198 	args.ifp = ifp;
199 	args.sa = sa;
200 	args.rt = rt;
201 
202 	return tcp4_segment(m, ip_tso_output_callback, &args);
203 }
204 
205 /*
206  * IP output.  The packet in mbuf chain m contains a skeletal IP
207  * header (with len, off, ttl, proto, tos, src, dst).
208  * The mbuf chain containing the packet will be freed.
209  * The mbuf opt, if present, will not be freed.
210  */
211 int
212 ip_output(struct mbuf *m0, ...)
213 {
214 	struct ip *ip;
215 	struct ifnet *ifp;
216 	struct mbuf *m = m0;
217 	int hlen = sizeof (struct ip);
218 	int len, error = 0;
219 	struct route iproute;
220 	struct sockaddr_in *dst;
221 	struct in_ifaddr *ia;
222 	struct ifaddr *xifa;
223 	struct mbuf *opt;
224 	struct route *ro;
225 	int flags, sw_csum;
226 	int *mtu_p;
227 	u_long mtu;
228 	struct ip_moptions *imo;
229 	struct socket *so;
230 	va_list ap;
231 #ifdef IPSEC_NAT_T
232 	int natt_frag = 0;
233 #endif
234 #ifdef IPSEC
235 	struct secpolicy *sp = NULL;
236 #endif /*IPSEC*/
237 #ifdef FAST_IPSEC
238 	struct inpcb *inp;
239 	struct m_tag *mtag;
240 	struct secpolicy *sp = NULL;
241 	struct tdb_ident *tdbi;
242 	int s;
243 #endif
244 	u_int16_t ip_len;
245 
246 	len = 0;
247 	va_start(ap, m0);
248 	opt = va_arg(ap, struct mbuf *);
249 	ro = va_arg(ap, struct route *);
250 	flags = va_arg(ap, int);
251 	imo = va_arg(ap, struct ip_moptions *);
252 	so = va_arg(ap, struct socket *);
253 	if (flags & IP_RETURNMTU)
254 		mtu_p = va_arg(ap, int *);
255 	else
256 		mtu_p = NULL;
257 	va_end(ap);
258 
259 	MCLAIM(m, &ip_tx_mowner);
260 #ifdef FAST_IPSEC
261 	if (so != NULL && so->so_proto->pr_domain->dom_family == AF_INET)
262 		inp = (struct inpcb *)so->so_pcb;
263 	else
264 		inp = NULL;
265 #endif /* FAST_IPSEC */
266 
267 #ifdef	DIAGNOSTIC
268 	if ((m->m_flags & M_PKTHDR) == 0)
269 		panic("ip_output: no HDR");
270 
271 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) != 0) {
272 		panic("ip_output: IPv6 checksum offload flags: %d",
273 		    m->m_pkthdr.csum_flags);
274 	}
275 
276 	if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) ==
277 	    (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
278 		panic("ip_output: conflicting checksum offload flags: %d",
279 		    m->m_pkthdr.csum_flags);
280 	}
281 #endif
282 	if (opt) {
283 		m = ip_insertoptions(m, opt, &len);
284 		if (len >= sizeof(struct ip))
285 			hlen = len;
286 	}
287 	ip = mtod(m, struct ip *);
288 	/*
289 	 * Fill in IP header.
290 	 */
291 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
292 		ip->ip_v = IPVERSION;
293 		ip->ip_off = htons(0);
294 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
295 			ip->ip_id = ip_newid();
296 		} else {
297 
298 			/*
299 			 * TSO capable interfaces (typically?) increment
300 			 * ip_id for each segment.
301 			 * "allocate" enough ids here to increase the chance
302 			 * for them to be unique.
303 			 *
304 			 * note that the following calculation is not
305 			 * needed to be precise.  wasting some ip_id is fine.
306 			 */
307 
308 			unsigned int segsz = m->m_pkthdr.segsz;
309 			unsigned int datasz = ntohs(ip->ip_len) - hlen;
310 			unsigned int num = howmany(datasz, segsz);
311 
312 			ip->ip_id = ip_newid_range(num);
313 		}
314 		ip->ip_hl = hlen >> 2;
315 		ipstat.ips_localout++;
316 	} else {
317 		hlen = ip->ip_hl << 2;
318 	}
319 	/*
320 	 * Route packet.
321 	 */
322 	if (ro == 0) {
323 		ro = &iproute;
324 		bzero((caddr_t)ro, sizeof (*ro));
325 	}
326 	dst = satosin(&ro->ro_dst);
327 	/*
328 	 * If there is a cached route,
329 	 * check that it is to the same destination
330 	 * and is still up.  If not, free it and try again.
331 	 * The address family should also be checked in case of sharing the
332 	 * cache with IPv6.
333 	 */
334 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
335 	    dst->sin_family != AF_INET ||
336 	    !in_hosteq(dst->sin_addr, ip->ip_dst))) {
337 		RTFREE(ro->ro_rt);
338 		ro->ro_rt = (struct rtentry *)0;
339 	}
340 	if (ro->ro_rt == 0) {
341 		bzero(dst, sizeof(*dst));
342 		dst->sin_family = AF_INET;
343 		dst->sin_len = sizeof(*dst);
344 		dst->sin_addr = ip->ip_dst;
345 	}
346 	/*
347 	 * If routing to interface only,
348 	 * short circuit routing lookup.
349 	 */
350 	if (flags & IP_ROUTETOIF) {
351 		if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) {
352 			ipstat.ips_noroute++;
353 			error = ENETUNREACH;
354 			goto bad;
355 		}
356 		ifp = ia->ia_ifp;
357 		mtu = ifp->if_mtu;
358 		ip->ip_ttl = 1;
359 	} else if ((IN_MULTICAST(ip->ip_dst.s_addr) ||
360 	    ip->ip_dst.s_addr == INADDR_BROADCAST) &&
361 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
362 		ifp = imo->imo_multicast_ifp;
363 		mtu = ifp->if_mtu;
364 		IFP_TO_IA(ifp, ia);
365 	} else {
366 		if (ro->ro_rt == 0)
367 			rtalloc(ro);
368 		if (ro->ro_rt == 0) {
369 			ipstat.ips_noroute++;
370 			error = EHOSTUNREACH;
371 			goto bad;
372 		}
373 		ia = ifatoia(ro->ro_rt->rt_ifa);
374 		ifp = ro->ro_rt->rt_ifp;
375 		if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
376 			mtu = ifp->if_mtu;
377 		ro->ro_rt->rt_use++;
378 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
379 			dst = satosin(ro->ro_rt->rt_gateway);
380 	}
381 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
382 	    (ip->ip_dst.s_addr == INADDR_BROADCAST)) {
383 		struct in_multi *inm;
384 
385 		m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ?
386 			M_BCAST : M_MCAST;
387 		/*
388 		 * IP destination address is multicast.  Make sure "dst"
389 		 * still points to the address in "ro".  (It may have been
390 		 * changed to point to a gateway address, above.)
391 		 */
392 		dst = satosin(&ro->ro_dst);
393 		/*
394 		 * See if the caller provided any multicast options
395 		 */
396 		if (imo != NULL)
397 			ip->ip_ttl = imo->imo_multicast_ttl;
398 		else
399 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
400 
401 		/*
402 		 * if we don't know the outgoing ifp yet, we can't generate
403 		 * output
404 		 */
405 		if (!ifp) {
406 			ipstat.ips_noroute++;
407 			error = ENETUNREACH;
408 			goto bad;
409 		}
410 
411 		/*
412 		 * If the packet is multicast or broadcast, confirm that
413 		 * the outgoing interface can transmit it.
414 		 */
415 		if (((m->m_flags & M_MCAST) &&
416 		     (ifp->if_flags & IFF_MULTICAST) == 0) ||
417 		    ((m->m_flags & M_BCAST) &&
418 		     (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0))  {
419 			ipstat.ips_noroute++;
420 			error = ENETUNREACH;
421 			goto bad;
422 		}
423 		/*
424 		 * If source address not specified yet, use an address
425 		 * of outgoing interface.
426 		 */
427 		if (in_nullhost(ip->ip_src)) {
428 			struct in_ifaddr *xia;
429 
430 			IFP_TO_IA(ifp, xia);
431 			if (!xia) {
432 				error = EADDRNOTAVAIL;
433 				goto bad;
434 			}
435 			xifa = &xia->ia_ifa;
436 			if (xifa->ifa_getifa != NULL) {
437 				xia = ifatoia((*xifa->ifa_getifa)(xifa,
438 				    &ro->ro_dst));
439 			}
440 			ip->ip_src = xia->ia_addr.sin_addr;
441 		}
442 
443 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
444 		if (inm != NULL &&
445 		   (imo == NULL || imo->imo_multicast_loop)) {
446 			/*
447 			 * If we belong to the destination multicast group
448 			 * on the outgoing interface, and the caller did not
449 			 * forbid loopback, loop back a copy.
450 			 */
451 			ip_mloopback(ifp, m, dst);
452 		}
453 #ifdef MROUTING
454 		else {
455 			/*
456 			 * If we are acting as a multicast router, perform
457 			 * multicast forwarding as if the packet had just
458 			 * arrived on the interface to which we are about
459 			 * to send.  The multicast forwarding function
460 			 * recursively calls this function, using the
461 			 * IP_FORWARDING flag to prevent infinite recursion.
462 			 *
463 			 * Multicasts that are looped back by ip_mloopback(),
464 			 * above, will be forwarded by the ip_input() routine,
465 			 * if necessary.
466 			 */
467 			extern struct socket *ip_mrouter;
468 
469 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
470 				if (ip_mforward(m, ifp) != 0) {
471 					m_freem(m);
472 					goto done;
473 				}
474 			}
475 		}
476 #endif
477 		/*
478 		 * Multicasts with a time-to-live of zero may be looped-
479 		 * back, above, but must not be transmitted on a network.
480 		 * Also, multicasts addressed to the loopback interface
481 		 * are not sent -- the above call to ip_mloopback() will
482 		 * loop back a copy if this host actually belongs to the
483 		 * destination group on the loopback interface.
484 		 */
485 		if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) {
486 			m_freem(m);
487 			goto done;
488 		}
489 
490 		goto sendit;
491 	}
492 	/*
493 	 * If source address not specified yet, use address
494 	 * of outgoing interface.
495 	 */
496 	if (in_nullhost(ip->ip_src)) {
497 		xifa = &ia->ia_ifa;
498 		if (xifa->ifa_getifa != NULL)
499 			ia = ifatoia((*xifa->ifa_getifa)(xifa, &ro->ro_dst));
500 		ip->ip_src = ia->ia_addr.sin_addr;
501 	}
502 
503 	/*
504 	 * packets with Class-D address as source are not valid per
505 	 * RFC 1112
506 	 */
507 	if (IN_MULTICAST(ip->ip_src.s_addr)) {
508 		ipstat.ips_odropped++;
509 		error = EADDRNOTAVAIL;
510 		goto bad;
511 	}
512 
513 	/*
514 	 * Look for broadcast address and
515 	 * and verify user is allowed to send
516 	 * such a packet.
517 	 */
518 	if (in_broadcast(dst->sin_addr, ifp)) {
519 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
520 			error = EADDRNOTAVAIL;
521 			goto bad;
522 		}
523 		if ((flags & IP_ALLOWBROADCAST) == 0) {
524 			error = EACCES;
525 			goto bad;
526 		}
527 		/* don't allow broadcast messages to be fragmented */
528 		if (ntohs(ip->ip_len) > ifp->if_mtu) {
529 			error = EMSGSIZE;
530 			goto bad;
531 		}
532 		m->m_flags |= M_BCAST;
533 	} else
534 		m->m_flags &= ~M_BCAST;
535 
536 sendit:
537 	/*
538 	 * If we're doing Path MTU Discovery, we need to set DF unless
539 	 * the route's MTU is locked.
540 	 */
541 	if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL &&
542 	    (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
543 		ip->ip_off |= htons(IP_DF);
544 
545 	/* Remember the current ip_len */
546 	ip_len = ntohs(ip->ip_len);
547 
548 #ifdef IPSEC
549 	/* get SP for this packet */
550 	if (so == NULL)
551 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
552 		    flags, &error);
553 	else {
554 		if (IPSEC_PCB_SKIP_IPSEC(sotoinpcb_hdr(so)->inph_sp,
555 					 IPSEC_DIR_OUTBOUND))
556 			goto skip_ipsec;
557 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
558 	}
559 
560 	if (sp == NULL) {
561 		ipsecstat.out_inval++;
562 		goto bad;
563 	}
564 
565 	error = 0;
566 
567 	/* check policy */
568 	switch (sp->policy) {
569 	case IPSEC_POLICY_DISCARD:
570 		/*
571 		 * This packet is just discarded.
572 		 */
573 		ipsecstat.out_polvio++;
574 		goto bad;
575 
576 	case IPSEC_POLICY_BYPASS:
577 	case IPSEC_POLICY_NONE:
578 		/* no need to do IPsec. */
579 		goto skip_ipsec;
580 
581 	case IPSEC_POLICY_IPSEC:
582 		if (sp->req == NULL) {
583 			/* XXX should be panic ? */
584 			printf("ip_output: No IPsec request specified.\n");
585 			error = EINVAL;
586 			goto bad;
587 		}
588 		break;
589 
590 	case IPSEC_POLICY_ENTRUST:
591 	default:
592 		printf("ip_output: Invalid policy found. %d\n", sp->policy);
593 	}
594 
595 #ifdef IPSEC_NAT_T
596 	/*
597 	 * NAT-T ESP fragmentation: don't do IPSec processing now,
598 	 * we'll do it on each fragmented packet.
599 	 */
600 	if (sp->req->sav &&
601 	    ((sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP) ||
602 	     (sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP_NON_IKE))) {
603 		if (ntohs(ip->ip_len) > sp->req->sav->esp_frag) {
604 			natt_frag = 1;
605 			mtu = sp->req->sav->esp_frag;
606 			goto skip_ipsec;
607 		}
608 	}
609 #endif /* IPSEC_NAT_T */
610 
611 	/*
612 	 * ipsec4_output() expects ip_len and ip_off in network
613 	 * order.  They have been set to network order above.
614 	 */
615 
616     {
617 	struct ipsec_output_state state;
618 	bzero(&state, sizeof(state));
619 	state.m = m;
620 	if (flags & IP_ROUTETOIF) {
621 		state.ro = &iproute;
622 		bzero(&iproute, sizeof(iproute));
623 	} else
624 		state.ro = ro;
625 	state.dst = (struct sockaddr *)dst;
626 
627 	/*
628 	 * We can't defer the checksum of payload data if
629 	 * we're about to encrypt/authenticate it.
630 	 *
631 	 * XXX When we support crypto offloading functions of
632 	 * XXX network interfaces, we need to reconsider this,
633 	 * XXX since it's likely that they'll support checksumming,
634 	 * XXX as well.
635 	 */
636 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
637 		in_delayed_cksum(m);
638 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
639 	}
640 
641 	error = ipsec4_output(&state, sp, flags);
642 
643 	m = state.m;
644 	if (flags & IP_ROUTETOIF) {
645 		/*
646 		 * if we have tunnel mode SA, we may need to ignore
647 		 * IP_ROUTETOIF.
648 		 */
649 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
650 			flags &= ~IP_ROUTETOIF;
651 			ro = state.ro;
652 		}
653 	} else
654 		ro = state.ro;
655 	dst = (struct sockaddr_in *)state.dst;
656 	if (error) {
657 		/* mbuf is already reclaimed in ipsec4_output. */
658 		m0 = NULL;
659 		switch (error) {
660 		case EHOSTUNREACH:
661 		case ENETUNREACH:
662 		case EMSGSIZE:
663 		case ENOBUFS:
664 		case ENOMEM:
665 			break;
666 		default:
667 			printf("ip4_output (ipsec): error code %d\n", error);
668 			/*fall through*/
669 		case ENOENT:
670 			/* don't show these error codes to the user */
671 			error = 0;
672 			break;
673 		}
674 		goto bad;
675 	}
676 
677 	/* be sure to update variables that are affected by ipsec4_output() */
678 	ip = mtod(m, struct ip *);
679 	hlen = ip->ip_hl << 2;
680 	ip_len = ntohs(ip->ip_len);
681 
682 	if (ro->ro_rt == NULL) {
683 		if ((flags & IP_ROUTETOIF) == 0) {
684 			printf("ip_output: "
685 				"can't update route after IPsec processing\n");
686 			error = EHOSTUNREACH;	/*XXX*/
687 			goto bad;
688 		}
689 	} else {
690 		/* nobody uses ia beyond here */
691 		if (state.encap) {
692 			ifp = ro->ro_rt->rt_ifp;
693 			if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0)
694 				mtu = ifp->if_mtu;
695 		}
696 	}
697     }
698 skip_ipsec:
699 #endif /*IPSEC*/
700 #ifdef FAST_IPSEC
701 	/*
702 	 * Check the security policy (SP) for the packet and, if
703 	 * required, do IPsec-related processing.  There are two
704 	 * cases here; the first time a packet is sent through
705 	 * it will be untagged and handled by ipsec4_checkpolicy.
706 	 * If the packet is resubmitted to ip_output (e.g. after
707 	 * AH, ESP, etc. processing), there will be a tag to bypass
708 	 * the lookup and related policy checking.
709 	 */
710 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
711 	s = splsoftnet();
712 	if (mtag != NULL) {
713 		tdbi = (struct tdb_ident *)(mtag + 1);
714 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
715 		if (sp == NULL)
716 			error = -EINVAL;	/* force silent drop */
717 		m_tag_delete(m, mtag);
718 	} else {
719 		if (inp != NULL &&
720 		    IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
721 			goto spd_done;
722 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
723 					&error, inp);
724 	}
725 	/*
726 	 * There are four return cases:
727 	 *    sp != NULL	 	    apply IPsec policy
728 	 *    sp == NULL, error == 0	    no IPsec handling needed
729 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
730 	 *    sp == NULL, error != 0	    discard packet, report error
731 	 */
732 	if (sp != NULL) {
733 #ifdef IPSEC_NAT_T
734 		/*
735 		 * NAT-T ESP fragmentation: don't do IPSec processing now,
736 		 * we'll do it on each fragmented packet.
737 		 */
738 		if (sp->req->sav &&
739 		    ((sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP) ||
740 		     (sp->req->sav->natt_type & UDP_ENCAP_ESPINUDP_NON_IKE))) {
741 			if (ntohs(ip->ip_len) > sp->req->sav->esp_frag) {
742 				natt_frag = 1;
743 				mtu = sp->req->sav->esp_frag;
744 				goto spd_done;
745 			}
746 		}
747 #endif /* IPSEC_NAT_T */
748 		/* Loop detection, check if ipsec processing already done */
749 		IPSEC_ASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
750 		for (mtag = m_tag_first(m); mtag != NULL;
751 		     mtag = m_tag_next(m, mtag)) {
752 #ifdef MTAG_ABI_COMPAT
753 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
754 				continue;
755 #endif
756 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
757 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
758 				continue;
759 			/*
760 			 * Check if policy has an SA associated with it.
761 			 * This can happen when an SP has yet to acquire
762 			 * an SA; e.g. on first reference.  If it occurs,
763 			 * then we let ipsec4_process_packet do its thing.
764 			 */
765 			if (sp->req->sav == NULL)
766 				break;
767 			tdbi = (struct tdb_ident *)(mtag + 1);
768 			if (tdbi->spi == sp->req->sav->spi &&
769 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
770 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
771 				 sizeof (union sockaddr_union)) == 0) {
772 				/*
773 				 * No IPsec processing is needed, free
774 				 * reference to SP.
775 				 *
776 				 * NB: null pointer to avoid free at
777 				 *     done: below.
778 				 */
779 				KEY_FREESP(&sp), sp = NULL;
780 				splx(s);
781 				goto spd_done;
782 			}
783 		}
784 
785 		/*
786 		 * Do delayed checksums now because we send before
787 		 * this is done in the normal processing path.
788 		 */
789 		if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
790 			in_delayed_cksum(m);
791 			m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
792 		}
793 
794 #ifdef __FreeBSD__
795 		ip->ip_len = htons(ip->ip_len);
796 		ip->ip_off = htons(ip->ip_off);
797 #endif
798 
799 		/* NB: callee frees mbuf */
800 		error = ipsec4_process_packet(m, sp->req, flags, 0);
801 		/*
802 		 * Preserve KAME behaviour: ENOENT can be returned
803 		 * when an SA acquire is in progress.  Don't propagate
804 		 * this to user-level; it confuses applications.
805 		 *
806 		 * XXX this will go away when the SADB is redone.
807 		 */
808 		if (error == ENOENT)
809 			error = 0;
810 		splx(s);
811 		goto done;
812 	} else {
813 		splx(s);
814 
815 		if (error != 0) {
816 			/*
817 			 * Hack: -EINVAL is used to signal that a packet
818 			 * should be silently discarded.  This is typically
819 			 * because we asked key management for an SA and
820 			 * it was delayed (e.g. kicked up to IKE).
821 			 */
822 			if (error == -EINVAL)
823 				error = 0;
824 			goto bad;
825 		} else {
826 			/* No IPsec processing for this packet. */
827 		}
828 #ifdef notyet
829 		/*
830 		 * If deferred crypto processing is needed, check that
831 		 * the interface supports it.
832 		 */
833 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
834 		if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) {
835 			/* notify IPsec to do its own crypto */
836 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
837 			error = EHOSTUNREACH;
838 			goto bad;
839 		}
840 #endif
841 	}
842 spd_done:
843 #endif /* FAST_IPSEC */
844 
845 #ifdef PFIL_HOOKS
846 	/*
847 	 * Run through list of hooks for output packets.
848 	 */
849 	if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
850 		goto done;
851 	if (m == NULL)
852 		goto done;
853 
854 	ip = mtod(m, struct ip *);
855 	hlen = ip->ip_hl << 2;
856 #endif /* PFIL_HOOKS */
857 
858 	m->m_pkthdr.csum_data |= hlen << 16;
859 
860 #if IFA_STATS
861 	/*
862 	 * search for the source address structure to
863 	 * maintain output statistics.
864 	 */
865 	INADDR_TO_IA(ip->ip_src, ia);
866 #endif
867 
868 	/* Maybe skip checksums on loopback interfaces. */
869 	if (IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4)) {
870 		m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
871 	}
872 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
873 	/*
874 	 * If small enough for mtu of path, or if using TCP segmentation
875 	 * offload, can just send directly.
876 	 */
877 	if (ip_len <= mtu ||
878 	    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0) {
879 #if IFA_STATS
880 		if (ia)
881 			ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
882 #endif
883 		/*
884 		 * Always initialize the sum to 0!  Some HW assisted
885 		 * checksumming requires this.
886 		 */
887 		ip->ip_sum = 0;
888 
889 		if ((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0) {
890 			/*
891 			 * Perform any checksums that the hardware can't do
892 			 * for us.
893 			 *
894 			 * XXX Does any hardware require the {th,uh}_sum
895 			 * XXX fields to be 0?
896 			 */
897 			if (sw_csum & M_CSUM_IPv4) {
898 				KASSERT(IN_NEED_CHECKSUM(ifp, M_CSUM_IPv4));
899 				ip->ip_sum = in_cksum(m, hlen);
900 				m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
901 			}
902 			if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
903 				if (IN_NEED_CHECKSUM(ifp,
904 				    sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
905 					in_delayed_cksum(m);
906 				}
907 				m->m_pkthdr.csum_flags &=
908 				    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
909 			}
910 		}
911 
912 #ifdef IPSEC
913 		/* clean ipsec history once it goes out of the node */
914 		ipsec_delaux(m);
915 #endif
916 
917 		if (__predict_true(
918 		    (m->m_pkthdr.csum_flags & M_CSUM_TSOv4) == 0 ||
919 		    (ifp->if_capenable & IFCAP_TSOv4) != 0)) {
920 			error =
921 			    (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
922 		} else {
923 			error =
924 			    ip_tso_output(ifp, m, sintosa(dst), ro->ro_rt);
925 		}
926 		goto done;
927 	}
928 
929 	/*
930 	 * We can't use HW checksumming if we're about to
931 	 * to fragment the packet.
932 	 *
933 	 * XXX Some hardware can do this.
934 	 */
935 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
936 		if (IN_NEED_CHECKSUM(ifp,
937 		    m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))) {
938 			in_delayed_cksum(m);
939 		}
940 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
941 	}
942 
943 	/*
944 	 * Too large for interface; fragment if possible.
945 	 * Must be able to put at least 8 bytes per fragment.
946 	 */
947 	if (ntohs(ip->ip_off) & IP_DF) {
948 		if (flags & IP_RETURNMTU)
949 			*mtu_p = mtu;
950 		error = EMSGSIZE;
951 		ipstat.ips_cantfrag++;
952 		goto bad;
953 	}
954 
955 	error = ip_fragment(m, ifp, mtu);
956 	if (error) {
957 		m = NULL;
958 		goto bad;
959 	}
960 
961 	for (; m; m = m0) {
962 		m0 = m->m_nextpkt;
963 		m->m_nextpkt = 0;
964 		if (error == 0) {
965 #if IFA_STATS
966 			if (ia)
967 				ia->ia_ifa.ifa_data.ifad_outbytes +=
968 				    ntohs(ip->ip_len);
969 #endif
970 #ifdef IPSEC
971 			/* clean ipsec history once it goes out of the node */
972 			ipsec_delaux(m);
973 #endif /* IPSEC */
974 
975 #ifdef IPSEC_NAT_T
976 			/*
977 			 * If we get there, the packet has not been handeld by
978 			 * IPSec whereas it should have. Now that it has been
979 			 * fragmented, re-inject it in ip_output so that IPsec
980 			 * processing can occur.
981 			 */
982 			if (natt_frag) {
983 				error = ip_output(m, opt,
984 				    ro, flags, imo, so, mtu_p);
985 			} else
986 #endif /* IPSEC_NAT_T */
987 			{
988 				KASSERT((m->m_pkthdr.csum_flags &
989 				    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
990 				error = (*ifp->if_output)(ifp, m, sintosa(dst),
991 				    ro->ro_rt);
992 			}
993 		} else
994 			m_freem(m);
995 	}
996 
997 	if (error == 0)
998 		ipstat.ips_fragmented++;
999 done:
1000 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
1001 		RTFREE(ro->ro_rt);
1002 		ro->ro_rt = 0;
1003 	}
1004 
1005 #ifdef IPSEC
1006 	if (sp != NULL) {
1007 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1008 			printf("DP ip_output call free SP:%p\n", sp));
1009 		key_freesp(sp);
1010 	}
1011 #endif /* IPSEC */
1012 #ifdef FAST_IPSEC
1013 	if (sp != NULL)
1014 		KEY_FREESP(&sp);
1015 #endif /* FAST_IPSEC */
1016 
1017 	return (error);
1018 bad:
1019 	m_freem(m);
1020 	goto done;
1021 }
1022 
1023 int
1024 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
1025 {
1026 	struct ip *ip, *mhip;
1027 	struct mbuf *m0;
1028 	int len, hlen, off;
1029 	int mhlen, firstlen;
1030 	struct mbuf **mnext;
1031 	int sw_csum = m->m_pkthdr.csum_flags;
1032 	int fragments = 0;
1033 	int s;
1034 	int error = 0;
1035 
1036 	ip = mtod(m, struct ip *);
1037 	hlen = ip->ip_hl << 2;
1038 	if (ifp != NULL)
1039 		sw_csum &= ~ifp->if_csum_flags_tx;
1040 
1041 	len = (mtu - hlen) &~ 7;
1042 	if (len < 8) {
1043 		m_freem(m);
1044 		return (EMSGSIZE);
1045 	}
1046 
1047 	firstlen = len;
1048 	mnext = &m->m_nextpkt;
1049 
1050 	/*
1051 	 * Loop through length of segment after first fragment,
1052 	 * make new header and copy data of each part and link onto chain.
1053 	 */
1054 	m0 = m;
1055 	mhlen = sizeof (struct ip);
1056 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
1057 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
1058 		if (m == 0) {
1059 			error = ENOBUFS;
1060 			ipstat.ips_odropped++;
1061 			goto sendorfree;
1062 		}
1063 		MCLAIM(m, m0->m_owner);
1064 		*mnext = m;
1065 		mnext = &m->m_nextpkt;
1066 		m->m_data += max_linkhdr;
1067 		mhip = mtod(m, struct ip *);
1068 		*mhip = *ip;
1069 		/* we must inherit MCAST and BCAST flags */
1070 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
1071 		if (hlen > sizeof (struct ip)) {
1072 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
1073 			mhip->ip_hl = mhlen >> 2;
1074 		}
1075 		m->m_len = mhlen;
1076 		mhip->ip_off = ((off - hlen) >> 3) +
1077 		    (ntohs(ip->ip_off) & ~IP_MF);
1078 		if (ip->ip_off & htons(IP_MF))
1079 			mhip->ip_off |= IP_MF;
1080 		if (off + len >= ntohs(ip->ip_len))
1081 			len = ntohs(ip->ip_len) - off;
1082 		else
1083 			mhip->ip_off |= IP_MF;
1084 		HTONS(mhip->ip_off);
1085 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
1086 		m->m_next = m_copy(m0, off, len);
1087 		if (m->m_next == 0) {
1088 			error = ENOBUFS;	/* ??? */
1089 			ipstat.ips_odropped++;
1090 			goto sendorfree;
1091 		}
1092 		m->m_pkthdr.len = mhlen + len;
1093 		m->m_pkthdr.rcvif = (struct ifnet *)0;
1094 		mhip->ip_sum = 0;
1095 		if (sw_csum & M_CSUM_IPv4) {
1096 			mhip->ip_sum = in_cksum(m, mhlen);
1097 			KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
1098 		} else {
1099 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
1100 			m->m_pkthdr.csum_data |= mhlen << 16;
1101 		}
1102 		ipstat.ips_ofragments++;
1103 		fragments++;
1104 	}
1105 	/*
1106 	 * Update first fragment by trimming what's been copied out
1107 	 * and updating header, then send each fragment (in order).
1108 	 */
1109 	m = m0;
1110 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
1111 	m->m_pkthdr.len = hlen + firstlen;
1112 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
1113 	ip->ip_off |= htons(IP_MF);
1114 	ip->ip_sum = 0;
1115 	if (sw_csum & M_CSUM_IPv4) {
1116 		ip->ip_sum = in_cksum(m, hlen);
1117 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
1118 	} else {
1119 		KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4);
1120 		KASSERT(M_CSUM_DATA_IPv4_IPHL(m->m_pkthdr.csum_data) >=
1121 			sizeof(struct ip));
1122 	}
1123 sendorfree:
1124 	/*
1125 	 * If there is no room for all the fragments, don't queue
1126 	 * any of them.
1127 	 */
1128 	if (ifp != NULL) {
1129 		s = splnet();
1130 		if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
1131 		    error == 0) {
1132 			error = ENOBUFS;
1133 			ipstat.ips_odropped++;
1134 			IFQ_INC_DROPS(&ifp->if_snd);
1135 		}
1136 		splx(s);
1137 	}
1138 	if (error) {
1139 		for (m = m0; m; m = m0) {
1140 			m0 = m->m_nextpkt;
1141 			m->m_nextpkt = NULL;
1142 			m_freem(m);
1143 		}
1144 	}
1145 	return (error);
1146 }
1147 
1148 /*
1149  * Process a delayed payload checksum calculation.
1150  */
1151 void
1152 in_delayed_cksum(struct mbuf *m)
1153 {
1154 	struct ip *ip;
1155 	u_int16_t csum, offset;
1156 
1157 	ip = mtod(m, struct ip *);
1158 	offset = ip->ip_hl << 2;
1159 	csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset);
1160 	if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0)
1161 		csum = 0xffff;
1162 
1163 	offset += M_CSUM_DATA_IPv4_OFFSET(m->m_pkthdr.csum_data);
1164 
1165 	if ((offset + sizeof(u_int16_t)) > m->m_len) {
1166 		/* This happen when ip options were inserted
1167 		printf("in_delayed_cksum: pullup len %d off %d proto %d\n",
1168 		    m->m_len, offset, ip->ip_p);
1169 		 */
1170 		m_copyback(m, offset, sizeof(csum), (caddr_t) &csum);
1171 	} else
1172 		*(u_int16_t *)(mtod(m, caddr_t) + offset) = csum;
1173 }
1174 
1175 /*
1176  * Determine the maximum length of the options to be inserted;
1177  * we would far rather allocate too much space rather than too little.
1178  */
1179 
1180 u_int
1181 ip_optlen(struct inpcb *inp)
1182 {
1183 	struct mbuf *m = inp->inp_options;
1184 
1185 	if (m && m->m_len > offsetof(struct ipoption, ipopt_dst))
1186 		return (m->m_len - offsetof(struct ipoption, ipopt_dst));
1187 	else
1188 		return 0;
1189 }
1190 
1191 
1192 /*
1193  * Insert IP options into preformed packet.
1194  * Adjust IP destination as required for IP source routing,
1195  * as indicated by a non-zero in_addr at the start of the options.
1196  */
1197 static struct mbuf *
1198 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1199 {
1200 	struct ipoption *p = mtod(opt, struct ipoption *);
1201 	struct mbuf *n;
1202 	struct ip *ip = mtod(m, struct ip *);
1203 	unsigned optlen;
1204 
1205 	optlen = opt->m_len - sizeof(p->ipopt_dst);
1206 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET)
1207 		return (m);		/* XXX should fail */
1208 	if (!in_nullhost(p->ipopt_dst))
1209 		ip->ip_dst = p->ipopt_dst;
1210 	if (M_READONLY(m) || M_LEADINGSPACE(m) < optlen) {
1211 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1212 		if (n == 0)
1213 			return (m);
1214 		MCLAIM(n, m->m_owner);
1215 		M_MOVE_PKTHDR(n, m);
1216 		m->m_len -= sizeof(struct ip);
1217 		m->m_data += sizeof(struct ip);
1218 		n->m_next = m;
1219 		m = n;
1220 		m->m_len = optlen + sizeof(struct ip);
1221 		m->m_data += max_linkhdr;
1222 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1223 	} else {
1224 		m->m_data -= optlen;
1225 		m->m_len += optlen;
1226 		memmove(mtod(m, caddr_t), ip, sizeof(struct ip));
1227 	}
1228 	m->m_pkthdr.len += optlen;
1229 	ip = mtod(m, struct ip *);
1230 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
1231 	*phlen = sizeof(struct ip) + optlen;
1232 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
1233 	return (m);
1234 }
1235 
1236 /*
1237  * Copy options from ip to jp,
1238  * omitting those not copied during fragmentation.
1239  */
1240 int
1241 ip_optcopy(struct ip *ip, struct ip *jp)
1242 {
1243 	u_char *cp, *dp;
1244 	int opt, optlen, cnt;
1245 
1246 	cp = (u_char *)(ip + 1);
1247 	dp = (u_char *)(jp + 1);
1248 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1249 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1250 		opt = cp[0];
1251 		if (opt == IPOPT_EOL)
1252 			break;
1253 		if (opt == IPOPT_NOP) {
1254 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1255 			*dp++ = IPOPT_NOP;
1256 			optlen = 1;
1257 			continue;
1258 		}
1259 #ifdef DIAGNOSTIC
1260 		if (cnt < IPOPT_OLEN + sizeof(*cp))
1261 			panic("malformed IPv4 option passed to ip_optcopy");
1262 #endif
1263 		optlen = cp[IPOPT_OLEN];
1264 #ifdef DIAGNOSTIC
1265 		if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1266 			panic("malformed IPv4 option passed to ip_optcopy");
1267 #endif
1268 		/* bogus lengths should have been caught by ip_dooptions */
1269 		if (optlen > cnt)
1270 			optlen = cnt;
1271 		if (IPOPT_COPIED(opt)) {
1272 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
1273 			dp += optlen;
1274 		}
1275 	}
1276 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1277 		*dp++ = IPOPT_EOL;
1278 	return (optlen);
1279 }
1280 
1281 /*
1282  * IP socket option processing.
1283  */
1284 int
1285 ip_ctloutput(int op, struct socket *so, int level, int optname,
1286     struct mbuf **mp)
1287 {
1288 	struct inpcb *inp = sotoinpcb(so);
1289 	struct mbuf *m = *mp;
1290 	int optval = 0;
1291 	int error = 0;
1292 #if defined(IPSEC) || defined(FAST_IPSEC)
1293 	struct lwp *l = curlwp;	/*XXX*/
1294 #endif
1295 
1296 	if (level != IPPROTO_IP) {
1297 		error = EINVAL;
1298 		if (op == PRCO_SETOPT && *mp)
1299 			(void) m_free(*mp);
1300 	} else switch (op) {
1301 
1302 	case PRCO_SETOPT:
1303 		switch (optname) {
1304 		case IP_OPTIONS:
1305 #ifdef notyet
1306 		case IP_RETOPTS:
1307 			return (ip_pcbopts(optname, &inp->inp_options, m));
1308 #else
1309 			return (ip_pcbopts(&inp->inp_options, m));
1310 #endif
1311 
1312 		case IP_TOS:
1313 		case IP_TTL:
1314 		case IP_RECVOPTS:
1315 		case IP_RECVRETOPTS:
1316 		case IP_RECVDSTADDR:
1317 		case IP_RECVIF:
1318 			if (m == NULL || m->m_len != sizeof(int))
1319 				error = EINVAL;
1320 			else {
1321 				optval = *mtod(m, int *);
1322 				switch (optname) {
1323 
1324 				case IP_TOS:
1325 					inp->inp_ip.ip_tos = optval;
1326 					break;
1327 
1328 				case IP_TTL:
1329 					inp->inp_ip.ip_ttl = optval;
1330 					break;
1331 #define	OPTSET(bit) \
1332 	if (optval) \
1333 		inp->inp_flags |= bit; \
1334 	else \
1335 		inp->inp_flags &= ~bit;
1336 
1337 				case IP_RECVOPTS:
1338 					OPTSET(INP_RECVOPTS);
1339 					break;
1340 
1341 				case IP_RECVRETOPTS:
1342 					OPTSET(INP_RECVRETOPTS);
1343 					break;
1344 
1345 				case IP_RECVDSTADDR:
1346 					OPTSET(INP_RECVDSTADDR);
1347 					break;
1348 
1349 				case IP_RECVIF:
1350 					OPTSET(INP_RECVIF);
1351 					break;
1352 				}
1353 			}
1354 			break;
1355 #undef OPTSET
1356 
1357 		case IP_MULTICAST_IF:
1358 		case IP_MULTICAST_TTL:
1359 		case IP_MULTICAST_LOOP:
1360 		case IP_ADD_MEMBERSHIP:
1361 		case IP_DROP_MEMBERSHIP:
1362 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
1363 			break;
1364 
1365 		case IP_PORTRANGE:
1366 			if (m == 0 || m->m_len != sizeof(int))
1367 				error = EINVAL;
1368 			else {
1369 				optval = *mtod(m, int *);
1370 
1371 				switch (optval) {
1372 
1373 				case IP_PORTRANGE_DEFAULT:
1374 				case IP_PORTRANGE_HIGH:
1375 					inp->inp_flags &= ~(INP_LOWPORT);
1376 					break;
1377 
1378 				case IP_PORTRANGE_LOW:
1379 					inp->inp_flags |= INP_LOWPORT;
1380 					break;
1381 
1382 				default:
1383 					error = EINVAL;
1384 					break;
1385 				}
1386 			}
1387 			break;
1388 
1389 #if defined(IPSEC) || defined(FAST_IPSEC)
1390 		case IP_IPSEC_POLICY:
1391 		{
1392 			caddr_t req = NULL;
1393 			size_t len = 0;
1394 			int priv = 0;
1395 
1396 #ifdef __NetBSD__
1397 			if (l == 0 || kauth_authorize_generic(l->l_cred,
1398 			    KAUTH_GENERIC_ISSUSER, &l->l_acflag))
1399 				priv = 0;
1400 			else
1401 				priv = 1;
1402 #else
1403 			priv = (in6p->in6p_socket->so_state & SS_PRIV);
1404 #endif
1405 			if (m) {
1406 				req = mtod(m, caddr_t);
1407 				len = m->m_len;
1408 			}
1409 			error = ipsec4_set_policy(inp, optname, req, len, priv);
1410 			break;
1411 		    }
1412 #endif /*IPSEC*/
1413 
1414 		default:
1415 			error = ENOPROTOOPT;
1416 			break;
1417 		}
1418 		if (m)
1419 			(void)m_free(m);
1420 		break;
1421 
1422 	case PRCO_GETOPT:
1423 		switch (optname) {
1424 		case IP_OPTIONS:
1425 		case IP_RETOPTS:
1426 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
1427 			MCLAIM(m, so->so_mowner);
1428 			if (inp->inp_options) {
1429 				m->m_len = inp->inp_options->m_len;
1430 				bcopy(mtod(inp->inp_options, caddr_t),
1431 				    mtod(m, caddr_t), (unsigned)m->m_len);
1432 			} else
1433 				m->m_len = 0;
1434 			break;
1435 
1436 		case IP_TOS:
1437 		case IP_TTL:
1438 		case IP_RECVOPTS:
1439 		case IP_RECVRETOPTS:
1440 		case IP_RECVDSTADDR:
1441 		case IP_RECVIF:
1442 		case IP_ERRORMTU:
1443 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
1444 			MCLAIM(m, so->so_mowner);
1445 			m->m_len = sizeof(int);
1446 			switch (optname) {
1447 
1448 			case IP_TOS:
1449 				optval = inp->inp_ip.ip_tos;
1450 				break;
1451 
1452 			case IP_TTL:
1453 				optval = inp->inp_ip.ip_ttl;
1454 				break;
1455 
1456 			case IP_ERRORMTU:
1457 				optval = inp->inp_errormtu;
1458 				break;
1459 
1460 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1461 
1462 			case IP_RECVOPTS:
1463 				optval = OPTBIT(INP_RECVOPTS);
1464 				break;
1465 
1466 			case IP_RECVRETOPTS:
1467 				optval = OPTBIT(INP_RECVRETOPTS);
1468 				break;
1469 
1470 			case IP_RECVDSTADDR:
1471 				optval = OPTBIT(INP_RECVDSTADDR);
1472 				break;
1473 
1474 			case IP_RECVIF:
1475 				optval = OPTBIT(INP_RECVIF);
1476 				break;
1477 			}
1478 			*mtod(m, int *) = optval;
1479 			break;
1480 
1481 #if 0	/* defined(IPSEC) || defined(FAST_IPSEC) */
1482 		/* XXX: code broken */
1483 		case IP_IPSEC_POLICY:
1484 		{
1485 			caddr_t req = NULL;
1486 			size_t len = 0;
1487 
1488 			if (m) {
1489 				req = mtod(m, caddr_t);
1490 				len = m->m_len;
1491 			}
1492 			error = ipsec4_get_policy(inp, req, len, mp);
1493 			break;
1494 		}
1495 #endif /*IPSEC*/
1496 
1497 		case IP_MULTICAST_IF:
1498 		case IP_MULTICAST_TTL:
1499 		case IP_MULTICAST_LOOP:
1500 		case IP_ADD_MEMBERSHIP:
1501 		case IP_DROP_MEMBERSHIP:
1502 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
1503 			if (*mp)
1504 				MCLAIM(*mp, so->so_mowner);
1505 			break;
1506 
1507 		case IP_PORTRANGE:
1508 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
1509 			MCLAIM(m, so->so_mowner);
1510 			m->m_len = sizeof(int);
1511 
1512 			if (inp->inp_flags & INP_LOWPORT)
1513 				optval = IP_PORTRANGE_LOW;
1514 			else
1515 				optval = IP_PORTRANGE_DEFAULT;
1516 
1517 			*mtod(m, int *) = optval;
1518 			break;
1519 
1520 		default:
1521 			error = ENOPROTOOPT;
1522 			break;
1523 		}
1524 		break;
1525 	}
1526 	return (error);
1527 }
1528 
1529 /*
1530  * Set up IP options in pcb for insertion in output packets.
1531  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1532  * with destination address if source routed.
1533  */
1534 int
1535 #ifdef notyet
1536 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
1537 #else
1538 ip_pcbopts(struct mbuf **pcbopt, struct mbuf *m)
1539 #endif
1540 {
1541 	int cnt, optlen;
1542 	u_char *cp;
1543 	u_char opt;
1544 
1545 	/* turn off any old options */
1546 	if (*pcbopt)
1547 		(void)m_free(*pcbopt);
1548 	*pcbopt = 0;
1549 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1550 		/*
1551 		 * Only turning off any previous options.
1552 		 */
1553 		if (m)
1554 			(void)m_free(m);
1555 		return (0);
1556 	}
1557 
1558 #ifndef	__vax__
1559 	if (m->m_len % sizeof(int32_t))
1560 		goto bad;
1561 #endif
1562 	/*
1563 	 * IP first-hop destination address will be stored before
1564 	 * actual options; move other options back
1565 	 * and clear it when none present.
1566 	 */
1567 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1568 		goto bad;
1569 	cnt = m->m_len;
1570 	m->m_len += sizeof(struct in_addr);
1571 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1572 	memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1573 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1574 
1575 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1576 		opt = cp[IPOPT_OPTVAL];
1577 		if (opt == IPOPT_EOL)
1578 			break;
1579 		if (opt == IPOPT_NOP)
1580 			optlen = 1;
1581 		else {
1582 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1583 				goto bad;
1584 			optlen = cp[IPOPT_OLEN];
1585 			if (optlen < IPOPT_OLEN  + sizeof(*cp) || optlen > cnt)
1586 				goto bad;
1587 		}
1588 		switch (opt) {
1589 
1590 		default:
1591 			break;
1592 
1593 		case IPOPT_LSRR:
1594 		case IPOPT_SSRR:
1595 			/*
1596 			 * user process specifies route as:
1597 			 *	->A->B->C->D
1598 			 * D must be our final destination (but we can't
1599 			 * check that since we may not have connected yet).
1600 			 * A is first hop destination, which doesn't appear in
1601 			 * actual IP option, but is stored before the options.
1602 			 */
1603 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1604 				goto bad;
1605 			m->m_len -= sizeof(struct in_addr);
1606 			cnt -= sizeof(struct in_addr);
1607 			optlen -= sizeof(struct in_addr);
1608 			cp[IPOPT_OLEN] = optlen;
1609 			/*
1610 			 * Move first hop before start of options.
1611 			 */
1612 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1613 			    sizeof(struct in_addr));
1614 			/*
1615 			 * Then copy rest of options back
1616 			 * to close up the deleted entry.
1617 			 */
1618 			(void)memmove(&cp[IPOPT_OFFSET+1],
1619 			    &cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
1620 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1621 			break;
1622 		}
1623 	}
1624 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1625 		goto bad;
1626 	*pcbopt = m;
1627 	return (0);
1628 
1629 bad:
1630 	(void)m_free(m);
1631 	return (EINVAL);
1632 }
1633 
1634 /*
1635  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1636  */
1637 static struct ifnet *
1638 ip_multicast_if(struct in_addr *a, int *ifindexp)
1639 {
1640 	int ifindex;
1641 	struct ifnet *ifp = NULL;
1642 	struct in_ifaddr *ia;
1643 
1644 	if (ifindexp)
1645 		*ifindexp = 0;
1646 	if (ntohl(a->s_addr) >> 24 == 0) {
1647 		ifindex = ntohl(a->s_addr) & 0xffffff;
1648 		if (ifindex < 0 || if_indexlim <= ifindex)
1649 			return NULL;
1650 		ifp = ifindex2ifnet[ifindex];
1651 		if (!ifp)
1652 			return NULL;
1653 		if (ifindexp)
1654 			*ifindexp = ifindex;
1655 	} else {
1656 		LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1657 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1658 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1659 				ifp = ia->ia_ifp;
1660 				break;
1661 			}
1662 		}
1663 	}
1664 	return ifp;
1665 }
1666 
1667 static int
1668 ip_getoptval(struct mbuf *m, u_int8_t *val, u_int maxval)
1669 {
1670 	u_int tval;
1671 
1672 	if (m == NULL)
1673 		return EINVAL;
1674 
1675 	switch (m->m_len) {
1676 	case sizeof(u_char):
1677 		tval = *(mtod(m, u_char *));
1678 		break;
1679 	case sizeof(u_int):
1680 		tval = *(mtod(m, u_int *));
1681 		break;
1682 	default:
1683 		return EINVAL;
1684 	}
1685 
1686 	if (tval > maxval)
1687 		return EINVAL;
1688 
1689 	*val = tval;
1690 	return 0;
1691 }
1692 
1693 /*
1694  * Set the IP multicast options in response to user setsockopt().
1695  */
1696 int
1697 ip_setmoptions(int optname, struct ip_moptions **imop, struct mbuf *m)
1698 {
1699 	int error = 0;
1700 	int i;
1701 	struct in_addr addr;
1702 	struct ip_mreq *mreq;
1703 	struct ifnet *ifp;
1704 	struct ip_moptions *imo = *imop;
1705 	struct route ro;
1706 	struct sockaddr_in *dst;
1707 	int ifindex;
1708 
1709 	if (imo == NULL) {
1710 		/*
1711 		 * No multicast option buffer attached to the pcb;
1712 		 * allocate one and initialize to default values.
1713 		 */
1714 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1715 		    M_WAITOK);
1716 
1717 		if (imo == NULL)
1718 			return (ENOBUFS);
1719 		*imop = imo;
1720 		imo->imo_multicast_ifp = NULL;
1721 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1722 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1723 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1724 		imo->imo_num_memberships = 0;
1725 	}
1726 
1727 	switch (optname) {
1728 
1729 	case IP_MULTICAST_IF:
1730 		/*
1731 		 * Select the interface for outgoing multicast packets.
1732 		 */
1733 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1734 			error = EINVAL;
1735 			break;
1736 		}
1737 		addr = *(mtod(m, struct in_addr *));
1738 		/*
1739 		 * INADDR_ANY is used to remove a previous selection.
1740 		 * When no interface is selected, a default one is
1741 		 * chosen every time a multicast packet is sent.
1742 		 */
1743 		if (in_nullhost(addr)) {
1744 			imo->imo_multicast_ifp = NULL;
1745 			break;
1746 		}
1747 		/*
1748 		 * The selected interface is identified by its local
1749 		 * IP address.  Find the interface and confirm that
1750 		 * it supports multicasting.
1751 		 */
1752 		ifp = ip_multicast_if(&addr, &ifindex);
1753 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1754 			error = EADDRNOTAVAIL;
1755 			break;
1756 		}
1757 		imo->imo_multicast_ifp = ifp;
1758 		if (ifindex)
1759 			imo->imo_multicast_addr = addr;
1760 		else
1761 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1762 		break;
1763 
1764 	case IP_MULTICAST_TTL:
1765 		/*
1766 		 * Set the IP time-to-live for outgoing multicast packets.
1767 		 */
1768 		error = ip_getoptval(m, &imo->imo_multicast_ttl, MAXTTL);
1769 		break;
1770 
1771 	case IP_MULTICAST_LOOP:
1772 		/*
1773 		 * Set the loopback flag for outgoing multicast packets.
1774 		 * Must be zero or one.
1775 		 */
1776 		error = ip_getoptval(m, &imo->imo_multicast_loop, 1);
1777 		break;
1778 
1779 	case IP_ADD_MEMBERSHIP:
1780 		/*
1781 		 * Add a multicast group membership.
1782 		 * Group must be a valid IP multicast address.
1783 		 */
1784 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1785 			error = EINVAL;
1786 			break;
1787 		}
1788 		mreq = mtod(m, struct ip_mreq *);
1789 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1790 			error = EINVAL;
1791 			break;
1792 		}
1793 		/*
1794 		 * If no interface address was provided, use the interface of
1795 		 * the route to the given multicast address.
1796 		 */
1797 		if (in_nullhost(mreq->imr_interface)) {
1798 			bzero((caddr_t)&ro, sizeof(ro));
1799 			ro.ro_rt = NULL;
1800 			dst = satosin(&ro.ro_dst);
1801 			dst->sin_len = sizeof(*dst);
1802 			dst->sin_family = AF_INET;
1803 			dst->sin_addr = mreq->imr_multiaddr;
1804 			rtalloc(&ro);
1805 			if (ro.ro_rt == NULL) {
1806 				error = EADDRNOTAVAIL;
1807 				break;
1808 			}
1809 			ifp = ro.ro_rt->rt_ifp;
1810 			rtfree(ro.ro_rt);
1811 		} else {
1812 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1813 		}
1814 		/*
1815 		 * See if we found an interface, and confirm that it
1816 		 * supports multicast.
1817 		 */
1818 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1819 			error = EADDRNOTAVAIL;
1820 			break;
1821 		}
1822 		/*
1823 		 * See if the membership already exists or if all the
1824 		 * membership slots are full.
1825 		 */
1826 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1827 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1828 			    in_hosteq(imo->imo_membership[i]->inm_addr,
1829 				      mreq->imr_multiaddr))
1830 				break;
1831 		}
1832 		if (i < imo->imo_num_memberships) {
1833 			error = EADDRINUSE;
1834 			break;
1835 		}
1836 		if (i == IP_MAX_MEMBERSHIPS) {
1837 			error = ETOOMANYREFS;
1838 			break;
1839 		}
1840 		/*
1841 		 * Everything looks good; add a new record to the multicast
1842 		 * address list for the given interface.
1843 		 */
1844 		if ((imo->imo_membership[i] =
1845 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1846 			error = ENOBUFS;
1847 			break;
1848 		}
1849 		++imo->imo_num_memberships;
1850 		break;
1851 
1852 	case IP_DROP_MEMBERSHIP:
1853 		/*
1854 		 * Drop a multicast group membership.
1855 		 * Group must be a valid IP multicast address.
1856 		 */
1857 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1858 			error = EINVAL;
1859 			break;
1860 		}
1861 		mreq = mtod(m, struct ip_mreq *);
1862 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1863 			error = EINVAL;
1864 			break;
1865 		}
1866 		/*
1867 		 * If an interface address was specified, get a pointer
1868 		 * to its ifnet structure.
1869 		 */
1870 		if (in_nullhost(mreq->imr_interface))
1871 			ifp = NULL;
1872 		else {
1873 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1874 			if (ifp == NULL) {
1875 				error = EADDRNOTAVAIL;
1876 				break;
1877 			}
1878 		}
1879 		/*
1880 		 * Find the membership in the membership array.
1881 		 */
1882 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1883 			if ((ifp == NULL ||
1884 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1885 			     in_hosteq(imo->imo_membership[i]->inm_addr,
1886 				       mreq->imr_multiaddr))
1887 				break;
1888 		}
1889 		if (i == imo->imo_num_memberships) {
1890 			error = EADDRNOTAVAIL;
1891 			break;
1892 		}
1893 		/*
1894 		 * Give up the multicast address record to which the
1895 		 * membership points.
1896 		 */
1897 		in_delmulti(imo->imo_membership[i]);
1898 		/*
1899 		 * Remove the gap in the membership array.
1900 		 */
1901 		for (++i; i < imo->imo_num_memberships; ++i)
1902 			imo->imo_membership[i-1] = imo->imo_membership[i];
1903 		--imo->imo_num_memberships;
1904 		break;
1905 
1906 	default:
1907 		error = EOPNOTSUPP;
1908 		break;
1909 	}
1910 
1911 	/*
1912 	 * If all options have default values, no need to keep the mbuf.
1913 	 */
1914 	if (imo->imo_multicast_ifp == NULL &&
1915 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1916 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1917 	    imo->imo_num_memberships == 0) {
1918 		free(*imop, M_IPMOPTS);
1919 		*imop = NULL;
1920 	}
1921 
1922 	return (error);
1923 }
1924 
1925 /*
1926  * Return the IP multicast options in response to user getsockopt().
1927  */
1928 int
1929 ip_getmoptions(int optname, struct ip_moptions *imo, struct mbuf **mp)
1930 {
1931 	u_char *ttl;
1932 	u_char *loop;
1933 	struct in_addr *addr;
1934 	struct in_ifaddr *ia;
1935 
1936 	*mp = m_get(M_WAIT, MT_SOOPTS);
1937 
1938 	switch (optname) {
1939 
1940 	case IP_MULTICAST_IF:
1941 		addr = mtod(*mp, struct in_addr *);
1942 		(*mp)->m_len = sizeof(struct in_addr);
1943 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1944 			*addr = zeroin_addr;
1945 		else if (imo->imo_multicast_addr.s_addr) {
1946 			/* return the value user has set */
1947 			*addr = imo->imo_multicast_addr;
1948 		} else {
1949 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1950 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1951 		}
1952 		return (0);
1953 
1954 	case IP_MULTICAST_TTL:
1955 		ttl = mtod(*mp, u_char *);
1956 		(*mp)->m_len = 1;
1957 		*ttl = imo ? imo->imo_multicast_ttl
1958 			   : IP_DEFAULT_MULTICAST_TTL;
1959 		return (0);
1960 
1961 	case IP_MULTICAST_LOOP:
1962 		loop = mtod(*mp, u_char *);
1963 		(*mp)->m_len = 1;
1964 		*loop = imo ? imo->imo_multicast_loop
1965 			    : IP_DEFAULT_MULTICAST_LOOP;
1966 		return (0);
1967 
1968 	default:
1969 		return (EOPNOTSUPP);
1970 	}
1971 }
1972 
1973 /*
1974  * Discard the IP multicast options.
1975  */
1976 void
1977 ip_freemoptions(struct ip_moptions *imo)
1978 {
1979 	int i;
1980 
1981 	if (imo != NULL) {
1982 		for (i = 0; i < imo->imo_num_memberships; ++i)
1983 			in_delmulti(imo->imo_membership[i]);
1984 		free(imo, M_IPMOPTS);
1985 	}
1986 }
1987 
1988 /*
1989  * Routine called from ip_output() to loop back a copy of an IP multicast
1990  * packet to the input queue of a specified interface.  Note that this
1991  * calls the output routine of the loopback "driver", but with an interface
1992  * pointer that might NOT be lo0ifp -- easier than replicating that code here.
1993  */
1994 static void
1995 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst)
1996 {
1997 	struct ip *ip;
1998 	struct mbuf *copym;
1999 
2000 	copym = m_copy(m, 0, M_COPYALL);
2001 	if (copym != NULL
2002 	 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
2003 		copym = m_pullup(copym, sizeof(struct ip));
2004 	if (copym != NULL) {
2005 		/*
2006 		 * We don't bother to fragment if the IP length is greater
2007 		 * than the interface's MTU.  Can this possibly matter?
2008 		 */
2009 		ip = mtod(copym, struct ip *);
2010 
2011 		if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
2012 			in_delayed_cksum(copym);
2013 			copym->m_pkthdr.csum_flags &=
2014 			    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
2015 		}
2016 
2017 		ip->ip_sum = 0;
2018 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
2019 		(void) looutput(ifp, copym, sintosa(dst), NULL);
2020 	}
2021 }
2022