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