xref: /netbsd-src/sys/netinet/ip_output.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: ip_output.c,v 1.133 2004/06/01 05:06:56 itojun 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.133 2004/06/01 05:06:56 itojun 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 	m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
741 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
742 	/*
743 	 * If small enough for mtu of path, can just send directly.
744 	 */
745 	if (ip_len <= mtu) {
746 #if IFA_STATS
747 		/*
748 		 * search for the source address structure to
749 		 * maintain output statistics.
750 		 */
751 		INADDR_TO_IA(ip->ip_src, ia);
752 		if (ia)
753 			ia->ia_ifa.ifa_data.ifad_outbytes += ip_len;
754 #endif
755 		/*
756 		 * Always initialize the sum to 0!  Some HW assisted
757 		 * checksumming requires this.
758 		 */
759 		ip->ip_sum = 0;
760 
761 		/*
762 		 * Perform any checksums that the hardware can't do
763 		 * for us.
764 		 *
765 		 * XXX Does any hardware require the {th,uh}_sum
766 		 * XXX fields to be 0?
767 		 */
768 		if (sw_csum & M_CSUM_IPv4) {
769 			ip->ip_sum = in_cksum(m, hlen);
770 			m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
771 		}
772 		if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
773 			in_delayed_cksum(m);
774 			m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
775 		}
776 
777 #ifdef IPSEC
778 		/* clean ipsec history once it goes out of the node */
779 		ipsec_delaux(m);
780 #endif
781 		error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt);
782 		goto done;
783 	}
784 
785 	/*
786 	 * We can't use HW checksumming if we're about to
787 	 * to fragment the packet.
788 	 *
789 	 * XXX Some hardware can do this.
790 	 */
791 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
792 		in_delayed_cksum(m);
793 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
794 	}
795 
796 	/*
797 	 * Too large for interface; fragment if possible.
798 	 * Must be able to put at least 8 bytes per fragment.
799 	 */
800 	if (ntohs(ip->ip_off) & IP_DF) {
801 		if (flags & IP_RETURNMTU)
802 			*mtu_p = mtu;
803 		error = EMSGSIZE;
804 		ipstat.ips_cantfrag++;
805 		goto bad;
806 	}
807 
808 	error = ip_fragment(m, ifp, mtu);
809 	if (error) {
810 		m = NULL;
811 		goto bad;
812 	}
813 
814 	for (; m; m = m0) {
815 		m0 = m->m_nextpkt;
816 		m->m_nextpkt = 0;
817 		if (error == 0) {
818 #if IFA_STATS
819 			/*
820 			 * search for the source address structure to
821 			 * maintain output statistics.
822 			 */
823 			INADDR_TO_IA(ip->ip_src, ia);
824 			if (ia) {
825 				ia->ia_ifa.ifa_data.ifad_outbytes +=
826 				    ntohs(ip->ip_len);
827 			}
828 #endif
829 #ifdef IPSEC
830 			/* clean ipsec history once it goes out of the node */
831 			ipsec_delaux(m);
832 #endif
833 			KASSERT((m->m_pkthdr.csum_flags &
834 			    (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0);
835 			error = (*ifp->if_output)(ifp, m, sintosa(dst),
836 			    ro->ro_rt);
837 		} else
838 			m_freem(m);
839 	}
840 
841 	if (error == 0)
842 		ipstat.ips_fragmented++;
843 done:
844 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) {
845 		RTFREE(ro->ro_rt);
846 		ro->ro_rt = 0;
847 	}
848 
849 #ifdef IPSEC
850 	if (sp != NULL) {
851 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
852 			printf("DP ip_output call free SP:%p\n", sp));
853 		key_freesp(sp);
854 	}
855 #endif /* IPSEC */
856 #ifdef FAST_IPSEC
857 	if (sp != NULL)
858 		KEY_FREESP(&sp);
859 #endif /* FAST_IPSEC */
860 
861 	return (error);
862 bad:
863 	m_freem(m);
864 	goto done;
865 }
866 
867 int
868 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu)
869 {
870 	struct ip *ip, *mhip;
871 	struct mbuf *m0;
872 	int len, hlen, off;
873 	int mhlen, firstlen;
874 	struct mbuf **mnext;
875 	int sw_csum;
876 	int fragments = 0;
877 	int s;
878 	int error = 0;
879 
880 	ip = mtod(m, struct ip *);
881 	hlen = ip->ip_hl << 2;
882 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
883 
884 	len = (mtu - hlen) &~ 7;
885 	if (len < 8) {
886 		m_freem(m);
887 		return (EMSGSIZE);
888 	}
889 
890 	firstlen = len;
891 	mnext = &m->m_nextpkt;
892 
893 	/*
894 	 * Loop through length of segment after first fragment,
895 	 * make new header and copy data of each part and link onto chain.
896 	 */
897 	m0 = m;
898 	mhlen = sizeof (struct ip);
899 	for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
900 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
901 		if (m == 0) {
902 			error = ENOBUFS;
903 			ipstat.ips_odropped++;
904 			goto sendorfree;
905 		}
906 		MCLAIM(m, m0->m_owner);
907 		*mnext = m;
908 		mnext = &m->m_nextpkt;
909 		m->m_data += max_linkhdr;
910 		mhip = mtod(m, struct ip *);
911 		*mhip = *ip;
912 		/* we must inherit MCAST and BCAST flags */
913 		m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST);
914 		if (hlen > sizeof (struct ip)) {
915 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
916 			mhip->ip_hl = mhlen >> 2;
917 		}
918 		m->m_len = mhlen;
919 		mhip->ip_off = ((off - hlen) >> 3) +
920 		    (ntohs(ip->ip_off) & ~IP_MF);
921 		if (ip->ip_off & htons(IP_MF))
922 			mhip->ip_off |= IP_MF;
923 		if (off + len >= ntohs(ip->ip_len))
924 			len = ntohs(ip->ip_len) - off;
925 		else
926 			mhip->ip_off |= IP_MF;
927 		HTONS(mhip->ip_off);
928 		mhip->ip_len = htons((u_int16_t)(len + mhlen));
929 		m->m_next = m_copy(m0, off, len);
930 		if (m->m_next == 0) {
931 			error = ENOBUFS;	/* ??? */
932 			ipstat.ips_odropped++;
933 			goto sendorfree;
934 		}
935 		m->m_pkthdr.len = mhlen + len;
936 		m->m_pkthdr.rcvif = (struct ifnet *)0;
937 		mhip->ip_sum = 0;
938 		if (sw_csum & M_CSUM_IPv4) {
939 			mhip->ip_sum = in_cksum(m, mhlen);
940 			KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0);
941 		} else {
942 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
943 		}
944 		ipstat.ips_ofragments++;
945 		fragments++;
946 	}
947 	/*
948 	 * Update first fragment by trimming what's been copied out
949 	 * and updating header, then send each fragment (in order).
950 	 */
951 	m = m0;
952 	m_adj(m, hlen + firstlen - ntohs(ip->ip_len));
953 	m->m_pkthdr.len = hlen + firstlen;
954 	ip->ip_len = htons((u_int16_t)m->m_pkthdr.len);
955 	ip->ip_off |= htons(IP_MF);
956 	ip->ip_sum = 0;
957 	if (sw_csum & M_CSUM_IPv4) {
958 		ip->ip_sum = in_cksum(m, hlen);
959 		m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4;
960 	} else {
961 		KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4);
962 	}
963 sendorfree:
964 	/*
965 	 * If there is no room for all the fragments, don't queue
966 	 * any of them.
967 	 */
968 	s = splnet();
969 	if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments &&
970 	    error == 0) {
971 		error = ENOBUFS;
972 		ipstat.ips_odropped++;
973 		IFQ_INC_DROPS(&ifp->if_snd);
974 	}
975 	splx(s);
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 defined(IPSEC) || defined(FAST_IPSEC)
1329 		case IP_IPSEC_POLICY:
1330 		{
1331 			caddr_t req = NULL;
1332 			size_t len = 0;
1333 
1334 			if (m) {
1335 				req = mtod(m, caddr_t);
1336 				len = m->m_len;
1337 			}
1338 			error = ipsec4_get_policy(inp, req, len, mp);
1339 			break;
1340 		}
1341 #endif /*IPSEC*/
1342 
1343 		case IP_MULTICAST_IF:
1344 		case IP_MULTICAST_TTL:
1345 		case IP_MULTICAST_LOOP:
1346 		case IP_ADD_MEMBERSHIP:
1347 		case IP_DROP_MEMBERSHIP:
1348 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
1349 			if (*mp)
1350 				MCLAIM(*mp, so->so_mowner);
1351 			break;
1352 
1353 		case IP_PORTRANGE:
1354 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
1355 			MCLAIM(m, so->so_mowner);
1356 			m->m_len = sizeof(int);
1357 
1358 			if (inp->inp_flags & INP_LOWPORT)
1359 				optval = IP_PORTRANGE_LOW;
1360 			else
1361 				optval = IP_PORTRANGE_DEFAULT;
1362 
1363 			*mtod(m, int *) = optval;
1364 			break;
1365 
1366 		default:
1367 			error = ENOPROTOOPT;
1368 			break;
1369 		}
1370 		break;
1371 	}
1372 	return (error);
1373 }
1374 
1375 /*
1376  * Set up IP options in pcb for insertion in output packets.
1377  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1378  * with destination address if source routed.
1379  */
1380 int
1381 #ifdef notyet
1382 ip_pcbopts(optname, pcbopt, m)
1383 	int optname;
1384 #else
1385 ip_pcbopts(pcbopt, m)
1386 #endif
1387 	struct mbuf **pcbopt;
1388 	struct mbuf *m;
1389 {
1390 	int cnt, optlen;
1391 	u_char *cp;
1392 	u_char opt;
1393 
1394 	/* turn off any old options */
1395 	if (*pcbopt)
1396 		(void)m_free(*pcbopt);
1397 	*pcbopt = 0;
1398 	if (m == (struct mbuf *)0 || m->m_len == 0) {
1399 		/*
1400 		 * Only turning off any previous options.
1401 		 */
1402 		if (m)
1403 			(void)m_free(m);
1404 		return (0);
1405 	}
1406 
1407 #ifndef	__vax__
1408 	if (m->m_len % sizeof(int32_t))
1409 		goto bad;
1410 #endif
1411 	/*
1412 	 * IP first-hop destination address will be stored before
1413 	 * actual options; move other options back
1414 	 * and clear it when none present.
1415 	 */
1416 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1417 		goto bad;
1418 	cnt = m->m_len;
1419 	m->m_len += sizeof(struct in_addr);
1420 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1421 	memmove(cp, mtod(m, caddr_t), (unsigned)cnt);
1422 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1423 
1424 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1425 		opt = cp[IPOPT_OPTVAL];
1426 		if (opt == IPOPT_EOL)
1427 			break;
1428 		if (opt == IPOPT_NOP)
1429 			optlen = 1;
1430 		else {
1431 			if (cnt < IPOPT_OLEN + sizeof(*cp))
1432 				goto bad;
1433 			optlen = cp[IPOPT_OLEN];
1434 			if (optlen < IPOPT_OLEN  + sizeof(*cp) || optlen > cnt)
1435 				goto bad;
1436 		}
1437 		switch (opt) {
1438 
1439 		default:
1440 			break;
1441 
1442 		case IPOPT_LSRR:
1443 		case IPOPT_SSRR:
1444 			/*
1445 			 * user process specifies route as:
1446 			 *	->A->B->C->D
1447 			 * D must be our final destination (but we can't
1448 			 * check that since we may not have connected yet).
1449 			 * A is first hop destination, which doesn't appear in
1450 			 * actual IP option, but is stored before the options.
1451 			 */
1452 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1453 				goto bad;
1454 			m->m_len -= sizeof(struct in_addr);
1455 			cnt -= sizeof(struct in_addr);
1456 			optlen -= sizeof(struct in_addr);
1457 			cp[IPOPT_OLEN] = optlen;
1458 			/*
1459 			 * Move first hop before start of options.
1460 			 */
1461 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1462 			    sizeof(struct in_addr));
1463 			/*
1464 			 * Then copy rest of options back
1465 			 * to close up the deleted entry.
1466 			 */
1467 			(void)memmove(&cp[IPOPT_OFFSET+1],
1468 			    &cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
1469 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
1470 			break;
1471 		}
1472 	}
1473 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1474 		goto bad;
1475 	*pcbopt = m;
1476 	return (0);
1477 
1478 bad:
1479 	(void)m_free(m);
1480 	return (EINVAL);
1481 }
1482 
1483 /*
1484  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1485  */
1486 static struct ifnet *
1487 ip_multicast_if(a, ifindexp)
1488 	struct in_addr *a;
1489 	int *ifindexp;
1490 {
1491 	int ifindex;
1492 	struct ifnet *ifp = NULL;
1493 	struct in_ifaddr *ia;
1494 
1495 	if (ifindexp)
1496 		*ifindexp = 0;
1497 	if (ntohl(a->s_addr) >> 24 == 0) {
1498 		ifindex = ntohl(a->s_addr) & 0xffffff;
1499 		if (ifindex < 0 || if_indexlim <= ifindex)
1500 			return NULL;
1501 		ifp = ifindex2ifnet[ifindex];
1502 		if (!ifp)
1503 			return NULL;
1504 		if (ifindexp)
1505 			*ifindexp = ifindex;
1506 	} else {
1507 		LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) {
1508 			if (in_hosteq(ia->ia_addr.sin_addr, *a) &&
1509 			    (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) {
1510 				ifp = ia->ia_ifp;
1511 				break;
1512 			}
1513 		}
1514 	}
1515 	return ifp;
1516 }
1517 
1518 /*
1519  * Set the IP multicast options in response to user setsockopt().
1520  */
1521 int
1522 ip_setmoptions(optname, imop, m)
1523 	int optname;
1524 	struct ip_moptions **imop;
1525 	struct mbuf *m;
1526 {
1527 	int error = 0;
1528 	u_char loop;
1529 	int i;
1530 	struct in_addr addr;
1531 	struct ip_mreq *mreq;
1532 	struct ifnet *ifp;
1533 	struct ip_moptions *imo = *imop;
1534 	struct route ro;
1535 	struct sockaddr_in *dst;
1536 	int ifindex;
1537 
1538 	if (imo == NULL) {
1539 		/*
1540 		 * No multicast option buffer attached to the pcb;
1541 		 * allocate one and initialize to default values.
1542 		 */
1543 		imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
1544 		    M_WAITOK);
1545 
1546 		if (imo == NULL)
1547 			return (ENOBUFS);
1548 		*imop = imo;
1549 		imo->imo_multicast_ifp = NULL;
1550 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1551 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1552 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1553 		imo->imo_num_memberships = 0;
1554 	}
1555 
1556 	switch (optname) {
1557 
1558 	case IP_MULTICAST_IF:
1559 		/*
1560 		 * Select the interface for outgoing multicast packets.
1561 		 */
1562 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1563 			error = EINVAL;
1564 			break;
1565 		}
1566 		addr = *(mtod(m, struct in_addr *));
1567 		/*
1568 		 * INADDR_ANY is used to remove a previous selection.
1569 		 * When no interface is selected, a default one is
1570 		 * chosen every time a multicast packet is sent.
1571 		 */
1572 		if (in_nullhost(addr)) {
1573 			imo->imo_multicast_ifp = NULL;
1574 			break;
1575 		}
1576 		/*
1577 		 * The selected interface is identified by its local
1578 		 * IP address.  Find the interface and confirm that
1579 		 * it supports multicasting.
1580 		 */
1581 		ifp = ip_multicast_if(&addr, &ifindex);
1582 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1583 			error = EADDRNOTAVAIL;
1584 			break;
1585 		}
1586 		imo->imo_multicast_ifp = ifp;
1587 		if (ifindex)
1588 			imo->imo_multicast_addr = addr;
1589 		else
1590 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1591 		break;
1592 
1593 	case IP_MULTICAST_TTL:
1594 		/*
1595 		 * Set the IP time-to-live for outgoing multicast packets.
1596 		 */
1597 		if (m == NULL || m->m_len != 1) {
1598 			error = EINVAL;
1599 			break;
1600 		}
1601 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
1602 		break;
1603 
1604 	case IP_MULTICAST_LOOP:
1605 		/*
1606 		 * Set the loopback flag for outgoing multicast packets.
1607 		 * Must be zero or one.
1608 		 */
1609 		if (m == NULL || m->m_len != 1 ||
1610 		   (loop = *(mtod(m, u_char *))) > 1) {
1611 			error = EINVAL;
1612 			break;
1613 		}
1614 		imo->imo_multicast_loop = loop;
1615 		break;
1616 
1617 	case IP_ADD_MEMBERSHIP:
1618 		/*
1619 		 * Add a multicast group membership.
1620 		 * Group must be a valid IP multicast address.
1621 		 */
1622 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1623 			error = EINVAL;
1624 			break;
1625 		}
1626 		mreq = mtod(m, struct ip_mreq *);
1627 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1628 			error = EINVAL;
1629 			break;
1630 		}
1631 		/*
1632 		 * If no interface address was provided, use the interface of
1633 		 * the route to the given multicast address.
1634 		 */
1635 		if (in_nullhost(mreq->imr_interface)) {
1636 			bzero((caddr_t)&ro, sizeof(ro));
1637 			ro.ro_rt = NULL;
1638 			dst = satosin(&ro.ro_dst);
1639 			dst->sin_len = sizeof(*dst);
1640 			dst->sin_family = AF_INET;
1641 			dst->sin_addr = mreq->imr_multiaddr;
1642 			rtalloc(&ro);
1643 			if (ro.ro_rt == NULL) {
1644 				error = EADDRNOTAVAIL;
1645 				break;
1646 			}
1647 			ifp = ro.ro_rt->rt_ifp;
1648 			rtfree(ro.ro_rt);
1649 		} else {
1650 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1651 		}
1652 		/*
1653 		 * See if we found an interface, and confirm that it
1654 		 * supports multicast.
1655 		 */
1656 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1657 			error = EADDRNOTAVAIL;
1658 			break;
1659 		}
1660 		/*
1661 		 * See if the membership already exists or if all the
1662 		 * membership slots are full.
1663 		 */
1664 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1665 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1666 			    in_hosteq(imo->imo_membership[i]->inm_addr,
1667 				      mreq->imr_multiaddr))
1668 				break;
1669 		}
1670 		if (i < imo->imo_num_memberships) {
1671 			error = EADDRINUSE;
1672 			break;
1673 		}
1674 		if (i == IP_MAX_MEMBERSHIPS) {
1675 			error = ETOOMANYREFS;
1676 			break;
1677 		}
1678 		/*
1679 		 * Everything looks good; add a new record to the multicast
1680 		 * address list for the given interface.
1681 		 */
1682 		if ((imo->imo_membership[i] =
1683 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1684 			error = ENOBUFS;
1685 			break;
1686 		}
1687 		++imo->imo_num_memberships;
1688 		break;
1689 
1690 	case IP_DROP_MEMBERSHIP:
1691 		/*
1692 		 * Drop a multicast group membership.
1693 		 * Group must be a valid IP multicast address.
1694 		 */
1695 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1696 			error = EINVAL;
1697 			break;
1698 		}
1699 		mreq = mtod(m, struct ip_mreq *);
1700 		if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) {
1701 			error = EINVAL;
1702 			break;
1703 		}
1704 		/*
1705 		 * If an interface address was specified, get a pointer
1706 		 * to its ifnet structure.
1707 		 */
1708 		if (in_nullhost(mreq->imr_interface))
1709 			ifp = NULL;
1710 		else {
1711 			ifp = ip_multicast_if(&mreq->imr_interface, NULL);
1712 			if (ifp == NULL) {
1713 				error = EADDRNOTAVAIL;
1714 				break;
1715 			}
1716 		}
1717 		/*
1718 		 * Find the membership in the membership array.
1719 		 */
1720 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1721 			if ((ifp == NULL ||
1722 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1723 			     in_hosteq(imo->imo_membership[i]->inm_addr,
1724 				       mreq->imr_multiaddr))
1725 				break;
1726 		}
1727 		if (i == imo->imo_num_memberships) {
1728 			error = EADDRNOTAVAIL;
1729 			break;
1730 		}
1731 		/*
1732 		 * Give up the multicast address record to which the
1733 		 * membership points.
1734 		 */
1735 		in_delmulti(imo->imo_membership[i]);
1736 		/*
1737 		 * Remove the gap in the membership array.
1738 		 */
1739 		for (++i; i < imo->imo_num_memberships; ++i)
1740 			imo->imo_membership[i-1] = imo->imo_membership[i];
1741 		--imo->imo_num_memberships;
1742 		break;
1743 
1744 	default:
1745 		error = EOPNOTSUPP;
1746 		break;
1747 	}
1748 
1749 	/*
1750 	 * If all options have default values, no need to keep the mbuf.
1751 	 */
1752 	if (imo->imo_multicast_ifp == NULL &&
1753 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1754 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1755 	    imo->imo_num_memberships == 0) {
1756 		free(*imop, M_IPMOPTS);
1757 		*imop = NULL;
1758 	}
1759 
1760 	return (error);
1761 }
1762 
1763 /*
1764  * Return the IP multicast options in response to user getsockopt().
1765  */
1766 int
1767 ip_getmoptions(optname, imo, mp)
1768 	int optname;
1769 	struct ip_moptions *imo;
1770 	struct mbuf **mp;
1771 {
1772 	u_char *ttl;
1773 	u_char *loop;
1774 	struct in_addr *addr;
1775 	struct in_ifaddr *ia;
1776 
1777 	*mp = m_get(M_WAIT, MT_SOOPTS);
1778 
1779 	switch (optname) {
1780 
1781 	case IP_MULTICAST_IF:
1782 		addr = mtod(*mp, struct in_addr *);
1783 		(*mp)->m_len = sizeof(struct in_addr);
1784 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1785 			*addr = zeroin_addr;
1786 		else if (imo->imo_multicast_addr.s_addr) {
1787 			/* return the value user has set */
1788 			*addr = imo->imo_multicast_addr;
1789 		} else {
1790 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1791 			*addr = ia ? ia->ia_addr.sin_addr : zeroin_addr;
1792 		}
1793 		return (0);
1794 
1795 	case IP_MULTICAST_TTL:
1796 		ttl = mtod(*mp, u_char *);
1797 		(*mp)->m_len = 1;
1798 		*ttl = imo ? imo->imo_multicast_ttl
1799 			   : IP_DEFAULT_MULTICAST_TTL;
1800 		return (0);
1801 
1802 	case IP_MULTICAST_LOOP:
1803 		loop = mtod(*mp, u_char *);
1804 		(*mp)->m_len = 1;
1805 		*loop = imo ? imo->imo_multicast_loop
1806 			    : IP_DEFAULT_MULTICAST_LOOP;
1807 		return (0);
1808 
1809 	default:
1810 		return (EOPNOTSUPP);
1811 	}
1812 }
1813 
1814 /*
1815  * Discard the IP multicast options.
1816  */
1817 void
1818 ip_freemoptions(imo)
1819 	struct ip_moptions *imo;
1820 {
1821 	int i;
1822 
1823 	if (imo != NULL) {
1824 		for (i = 0; i < imo->imo_num_memberships; ++i)
1825 			in_delmulti(imo->imo_membership[i]);
1826 		free(imo, M_IPMOPTS);
1827 	}
1828 }
1829 
1830 /*
1831  * Routine called from ip_output() to loop back a copy of an IP multicast
1832  * packet to the input queue of a specified interface.  Note that this
1833  * calls the output routine of the loopback "driver", but with an interface
1834  * pointer that might NOT be &loif -- easier than replicating that code here.
1835  */
1836 static void
1837 ip_mloopback(ifp, m, dst)
1838 	struct ifnet *ifp;
1839 	struct mbuf *m;
1840 	struct sockaddr_in *dst;
1841 {
1842 	struct ip *ip;
1843 	struct mbuf *copym;
1844 
1845 	copym = m_copy(m, 0, M_COPYALL);
1846 	if (copym != NULL
1847 	 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip)))
1848 		copym = m_pullup(copym, sizeof(struct ip));
1849 	if (copym != NULL) {
1850 		/*
1851 		 * We don't bother to fragment if the IP length is greater
1852 		 * than the interface's MTU.  Can this possibly matter?
1853 		 */
1854 		ip = mtod(copym, struct ip *);
1855 
1856 		if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
1857 			in_delayed_cksum(copym);
1858 			copym->m_pkthdr.csum_flags &=
1859 			    ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
1860 		}
1861 
1862 		ip->ip_sum = 0;
1863 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1864 		(void) looutput(ifp, copym, sintosa(dst), NULL);
1865 	}
1866 }
1867