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