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