xref: /dragonfly/sys/netinet/ip_output.c (revision cc93b0eb)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30  * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.37 2003/04/15 06:44:45 silby Exp $
31  * $DragonFly: src/sys/netinet/ip_output.c,v 1.46 2008/07/07 22:02:10 nant Exp $
32  */
33 
34 #define _IP_VHL
35 
36 #include "opt_ipfw.h"
37 #include "opt_ipdn.h"
38 #include "opt_ipdivert.h"
39 #include "opt_ipfilter.h"
40 #include "opt_ipsec.h"
41 #include "opt_mbuf_stress_test.h"
42 #include "opt_mpls.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/proc.h>
53 #include <sys/sysctl.h>
54 #include <sys/thread2.h>
55 #include <sys/in_cksum.h>
56 
57 #include <net/if.h>
58 #include <net/netisr.h>
59 #include <net/pfil.h>
60 #include <net/route.h>
61 
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
68 
69 #include <netproto/mpls/mpls_var.h>
70 
71 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
72 
73 #ifdef IPSEC
74 #include <netinet6/ipsec.h>
75 #include <netproto/key/key.h>
76 #ifdef IPSEC_DEBUG
77 #include <netproto/key/key_debug.h>
78 #else
79 #define	KEYDEBUG(lev,arg)
80 #endif
81 #endif /*IPSEC*/
82 
83 #ifdef FAST_IPSEC
84 #include <netproto/ipsec/ipsec.h>
85 #include <netproto/ipsec/xform.h>
86 #include <netproto/ipsec/key.h>
87 #endif /*FAST_IPSEC*/
88 
89 #include <net/ipfw/ip_fw.h>
90 #include <net/dummynet/ip_dummynet.h>
91 
92 #define print_ip(x, a, y)	 kprintf("%s %d.%d.%d.%d%s",\
93 				x, (ntohl(a.s_addr)>>24)&0xFF,\
94 				  (ntohl(a.s_addr)>>16)&0xFF,\
95 				  (ntohl(a.s_addr)>>8)&0xFF,\
96 				  (ntohl(a.s_addr))&0xFF, y);
97 
98 u_short ip_id;
99 
100 #ifdef MBUF_STRESS_TEST
101 int mbuf_frag_size = 0;
102 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
103 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
104 #endif
105 
106 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
107 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
108 static void	ip_mloopback
109 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
110 static int	ip_getmoptions
111 	(struct sockopt *, struct ip_moptions *);
112 static int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
113 static int	ip_setmoptions
114 	(struct sockopt *, struct ip_moptions **);
115 
116 int	ip_optcopy(struct ip *, struct ip *);
117 
118 
119 extern	struct protosw inetsw[];
120 
121 /*
122  * IP output.  The packet in mbuf chain m contains a skeletal IP
123  * header (with len, off, ttl, proto, tos, src, dst).
124  * The mbuf chain containing the packet will be freed.
125  * The mbuf opt, if present, will not be freed.
126  */
127 int
128 ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro,
129 	  int flags, struct ip_moptions *imo, struct inpcb *inp)
130 {
131 	struct ip *ip;
132 	struct ifnet *ifp = NULL;	/* keep compiler happy */
133 	struct mbuf *m;
134 	int hlen = sizeof(struct ip);
135 	int len, off, error = 0;
136 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
137 	struct in_ifaddr *ia = NULL;
138 	int isbroadcast, sw_csum;
139 	struct in_addr pkt_dst;
140 	struct route iproute;
141 	struct m_tag *dn_mtag = NULL;
142 #ifdef IPSEC
143 	struct secpolicy *sp = NULL;
144 	struct socket *so = inp ? inp->inp_socket : NULL;
145 #endif
146 #ifdef FAST_IPSEC
147 	struct m_tag *mtag;
148 	struct secpolicy *sp = NULL;
149 	struct tdb_ident *tdbi;
150 #endif /* FAST_IPSEC */
151 	struct ip_fw_args args;
152 	int src_was_INADDR_ANY = 0;	/* as the name says... */
153 
154 	args.eh = NULL;
155 	args.rule = NULL;
156 	args.next_hop = NULL;
157 
158 	/* Grab info from MT_TAG mbufs prepended to the chain. */
159 	while (m0 != NULL && m0->m_type == MT_TAG) {
160 		switch(m0->_m_tag_id) {
161 		case PACKET_TAG_IPFORWARD:
162 			args.next_hop = (struct sockaddr_in *)m0->m_data;
163 			break;
164 		default:
165 			kprintf("ip_output: unrecognised MT_TAG tag %d\n",
166 			    m0->_m_tag_id);
167 			break;
168 		}
169 		m0 = m0->m_next;
170 	}
171 	m = m0;
172 	M_ASSERTPKTHDR(m);
173 
174 	/* Extract info from dummynet tag */
175 	dn_mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
176 	if (dn_mtag != NULL) {
177 		struct dn_pkt *dn_pkt = m_tag_data(dn_mtag);
178 
179 		/*
180 		 * The packet was already tagged, so part of the
181 		 * processing was already done, and we need to go down.
182 		 * Get parameters from the tag.
183 		 */
184 		args.rule = dn_pkt->dn_priv;
185 		opt = NULL;
186 		ro = &dn_pkt->ro;
187 		imo = NULL;
188 		dst = dn_pkt->dn_dst;
189 		ifp = dn_pkt->ifp;
190 		flags = dn_pkt->flags;
191 
192 		/*
193 		 * Don't delete the dummynet tag here, just unlink it,
194 		 * since some local variables (like 'ro' and 'dst') are
195 		 * still referencing certain parts of it.
196 		 * The dummynet tag will be freed at the end of the
197 		 * output process.
198 		 */
199 		m_tag_unlink(m, dn_mtag);
200 	}
201 
202 	if (ro == NULL) {
203 		ro = &iproute;
204 		bzero(ro, sizeof *ro);
205 	}
206 
207 	if (args.rule != NULL) {	/* dummynet already saw us */
208 		ip = mtod(m, struct ip *);
209 		hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
210 		if (ro->ro_rt)
211 			ia = ifatoia(ro->ro_rt->rt_ifa);
212 		goto sendit;
213 	}
214 
215 	if (opt) {
216 		len = 0;
217 		m = ip_insertoptions(m, opt, &len);
218 		if (len != 0)
219 			hlen = len;
220 	}
221 	ip = mtod(m, struct ip *);
222 	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
223 
224 	/*
225 	 * Fill in IP header.
226 	 */
227 	if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) {
228 		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
229 		ip->ip_off &= IP_DF;
230 		ip->ip_id = ip_newid();
231 		ipstat.ips_localout++;
232 	} else {
233 		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
234 	}
235 
236 	dst = (struct sockaddr_in *)&ro->ro_dst;
237 	/*
238 	 * If there is a cached route,
239 	 * check that it is to the same destination
240 	 * and is still up.  If not, free it and try again.
241 	 * The address family should also be checked in case of sharing the
242 	 * cache with IPv6.
243 	 */
244 	if (ro->ro_rt &&
245 	    (!(ro->ro_rt->rt_flags & RTF_UP) ||
246 	     dst->sin_family != AF_INET ||
247 	     dst->sin_addr.s_addr != pkt_dst.s_addr)) {
248 		rtfree(ro->ro_rt);
249 		ro->ro_rt = (struct rtentry *)NULL;
250 	}
251 	if (ro->ro_rt == NULL) {
252 		bzero(dst, sizeof *dst);
253 		dst->sin_family = AF_INET;
254 		dst->sin_len = sizeof *dst;
255 		dst->sin_addr = pkt_dst;
256 	}
257 	/*
258 	 * If routing to interface only,
259 	 * short circuit routing lookup.
260 	 */
261 	if (flags & IP_ROUTETOIF) {
262 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
263 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
264 			ipstat.ips_noroute++;
265 			error = ENETUNREACH;
266 			goto bad;
267 		}
268 		ifp = ia->ia_ifp;
269 		ip->ip_ttl = 1;
270 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
271 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
272 		   imo != NULL && imo->imo_multicast_ifp != NULL) {
273 		/*
274 		 * Bypass the normal routing lookup for multicast
275 		 * packets if the interface is specified.
276 		 */
277 		ifp = imo->imo_multicast_ifp;
278 		ia = IFP_TO_IA(ifp);
279 		isbroadcast = 0;	/* fool gcc */
280 	} else {
281 		/*
282 		 * If this is the case, we probably don't want to allocate
283 		 * a protocol-cloned route since we didn't get one from the
284 		 * ULP.  This lets TCP do its thing, while not burdening
285 		 * forwarding or ICMP with the overhead of cloning a route.
286 		 * Of course, we still want to do any cloning requested by
287 		 * the link layer, as this is probably required in all cases
288 		 * for correct operation (as it is for ARP).
289 		 */
290 		if (ro->ro_rt == NULL)
291 			rtalloc_ign(ro, RTF_PRCLONING);
292 		if (ro->ro_rt == NULL) {
293 			ipstat.ips_noroute++;
294 			error = EHOSTUNREACH;
295 			goto bad;
296 		}
297 		ia = ifatoia(ro->ro_rt->rt_ifa);
298 		ifp = ro->ro_rt->rt_ifp;
299 		ro->ro_rt->rt_use++;
300 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
301 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
302 		if (ro->ro_rt->rt_flags & RTF_HOST)
303 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
304 		else
305 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
306 	}
307 	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
308 		struct in_multi *inm;
309 
310 		m->m_flags |= M_MCAST;
311 		/*
312 		 * IP destination address is multicast.  Make sure "dst"
313 		 * still points to the address in "ro".  (It may have been
314 		 * changed to point to a gateway address, above.)
315 		 */
316 		dst = (struct sockaddr_in *)&ro->ro_dst;
317 		/*
318 		 * See if the caller provided any multicast options
319 		 */
320 		if (imo != NULL) {
321 			ip->ip_ttl = imo->imo_multicast_ttl;
322 			if (imo->imo_multicast_vif != -1)
323 				ip->ip_src.s_addr =
324 				    ip_mcast_src ?
325 				    ip_mcast_src(imo->imo_multicast_vif) :
326 				    INADDR_ANY;
327 		} else
328 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
329 		/*
330 		 * Confirm that the outgoing interface supports multicast.
331 		 */
332 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
333 			if (!(ifp->if_flags & IFF_MULTICAST)) {
334 				ipstat.ips_noroute++;
335 				error = ENETUNREACH;
336 				goto bad;
337 			}
338 		}
339 		/*
340 		 * If source address not specified yet, use address
341 		 * of outgoing interface.
342 		 */
343 		if (ip->ip_src.s_addr == INADDR_ANY) {
344 			/* Interface may have no addresses. */
345 			if (ia != NULL)
346 				ip->ip_src = IA_SIN(ia)->sin_addr;
347 		}
348 
349 		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
350 		if (inm != NULL &&
351 		   (imo == NULL || imo->imo_multicast_loop)) {
352 			/*
353 			 * If we belong to the destination multicast group
354 			 * on the outgoing interface, and the caller did not
355 			 * forbid loopback, loop back a copy.
356 			 */
357 			ip_mloopback(ifp, m, dst, hlen);
358 		}
359 		else {
360 			/*
361 			 * If we are acting as a multicast router, perform
362 			 * multicast forwarding as if the packet had just
363 			 * arrived on the interface to which we are about
364 			 * to send.  The multicast forwarding function
365 			 * recursively calls this function, using the
366 			 * IP_FORWARDING flag to prevent infinite recursion.
367 			 *
368 			 * Multicasts that are looped back by ip_mloopback(),
369 			 * above, will be forwarded by the ip_input() routine,
370 			 * if necessary.
371 			 */
372 			if (ip_mrouter && !(flags & IP_FORWARDING)) {
373 				/*
374 				 * If rsvp daemon is not running, do not
375 				 * set ip_moptions. This ensures that the packet
376 				 * is multicast and not just sent down one link
377 				 * as prescribed by rsvpd.
378 				 */
379 				if (!rsvp_on)
380 					imo = NULL;
381 				if (ip_mforward &&
382 				    ip_mforward(ip, ifp, m, imo) != 0) {
383 					m_freem(m);
384 					goto done;
385 				}
386 			}
387 		}
388 
389 		/*
390 		 * Multicasts with a time-to-live of zero may be looped-
391 		 * back, above, but must not be transmitted on a network.
392 		 * Also, multicasts addressed to the loopback interface
393 		 * are not sent -- the above call to ip_mloopback() will
394 		 * loop back a copy if this host actually belongs to the
395 		 * destination group on the loopback interface.
396 		 */
397 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
398 			m_freem(m);
399 			goto done;
400 		}
401 
402 		goto sendit;
403 	}
404 #ifndef notdef
405 	/*
406 	 * If the source address is not specified yet, use the address
407 	 * of the outoing interface. In case, keep note we did that, so
408 	 * if the the firewall changes the next-hop causing the output
409 	 * interface to change, we can fix that.
410 	 */
411 	if (ip->ip_src.s_addr == INADDR_ANY) {
412 		/* Interface may have no addresses. */
413 		if (ia != NULL) {
414 			ip->ip_src = IA_SIN(ia)->sin_addr;
415 			src_was_INADDR_ANY = 1;
416 		}
417 	}
418 #endif /* notdef */
419 #ifdef ALTQ
420 	/*
421 	 * Disable packet drop hack.
422 	 * Packetdrop should be done by queueing.
423 	 */
424 #else /* !ALTQ */
425 	/*
426 	 * Verify that we have any chance at all of being able to queue
427 	 *      the packet or packet fragments
428 	 */
429 	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
430 		ifp->if_snd.ifq_maxlen) {
431 			error = ENOBUFS;
432 			ipstat.ips_odropped++;
433 			goto bad;
434 	}
435 #endif /* !ALTQ */
436 
437 	/*
438 	 * Look for broadcast address and
439 	 * verify user is allowed to send
440 	 * such a packet.
441 	 */
442 	if (isbroadcast) {
443 		if (!(ifp->if_flags & IFF_BROADCAST)) {
444 			error = EADDRNOTAVAIL;
445 			goto bad;
446 		}
447 		if (!(flags & IP_ALLOWBROADCAST)) {
448 			error = EACCES;
449 			goto bad;
450 		}
451 		/* don't allow broadcast messages to be fragmented */
452 		if (ip->ip_len > ifp->if_mtu) {
453 			error = EMSGSIZE;
454 			goto bad;
455 		}
456 		m->m_flags |= M_BCAST;
457 	} else {
458 		m->m_flags &= ~M_BCAST;
459 	}
460 
461 sendit:
462 #ifdef IPSEC
463 	/* get SP for this packet */
464 	if (so == NULL)
465 		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
466 	else
467 		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
468 
469 	if (sp == NULL) {
470 		ipsecstat.out_inval++;
471 		goto bad;
472 	}
473 
474 	error = 0;
475 
476 	/* check policy */
477 	switch (sp->policy) {
478 	case IPSEC_POLICY_DISCARD:
479 		/*
480 		 * This packet is just discarded.
481 		 */
482 		ipsecstat.out_polvio++;
483 		goto bad;
484 
485 	case IPSEC_POLICY_BYPASS:
486 	case IPSEC_POLICY_NONE:
487 		/* no need to do IPsec. */
488 		goto skip_ipsec;
489 
490 	case IPSEC_POLICY_IPSEC:
491 		if (sp->req == NULL) {
492 			/* acquire a policy */
493 			error = key_spdacquire(sp);
494 			goto bad;
495 		}
496 		break;
497 
498 	case IPSEC_POLICY_ENTRUST:
499 	default:
500 		kprintf("ip_output: Invalid policy found. %d\n", sp->policy);
501 	}
502     {
503 	struct ipsec_output_state state;
504 	bzero(&state, sizeof state);
505 	state.m = m;
506 	if (flags & IP_ROUTETOIF) {
507 		state.ro = &iproute;
508 		bzero(&iproute, sizeof iproute);
509 	} else
510 		state.ro = ro;
511 	state.dst = (struct sockaddr *)dst;
512 
513 	ip->ip_sum = 0;
514 
515 	/*
516 	 * XXX
517 	 * delayed checksums are not currently compatible with IPsec
518 	 */
519 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
520 		in_delayed_cksum(m);
521 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
522 	}
523 
524 	ip->ip_len = htons(ip->ip_len);
525 	ip->ip_off = htons(ip->ip_off);
526 
527 	error = ipsec4_output(&state, sp, flags);
528 
529 	m = state.m;
530 	if (flags & IP_ROUTETOIF) {
531 		/*
532 		 * if we have tunnel mode SA, we may need to ignore
533 		 * IP_ROUTETOIF.
534 		 */
535 		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
536 			flags &= ~IP_ROUTETOIF;
537 			ro = state.ro;
538 		}
539 	} else
540 		ro = state.ro;
541 	dst = (struct sockaddr_in *)state.dst;
542 	if (error) {
543 		/* mbuf is already reclaimed in ipsec4_output. */
544 		m0 = NULL;
545 		switch (error) {
546 		case EHOSTUNREACH:
547 		case ENETUNREACH:
548 		case EMSGSIZE:
549 		case ENOBUFS:
550 		case ENOMEM:
551 			break;
552 		default:
553 			kprintf("ip4_output (ipsec): error code %d\n", error);
554 			/*fall through*/
555 		case ENOENT:
556 			/* don't show these error codes to the user */
557 			error = 0;
558 			break;
559 		}
560 		goto bad;
561 	}
562     }
563 
564 	/* be sure to update variables that are affected by ipsec4_output() */
565 	ip = mtod(m, struct ip *);
566 #ifdef _IP_VHL
567 	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
568 #else
569 	hlen = ip->ip_hl << 2;
570 #endif
571 	if (ro->ro_rt == NULL) {
572 		if (!(flags & IP_ROUTETOIF)) {
573 			kprintf("ip_output: "
574 				"can't update route after IPsec processing\n");
575 			error = EHOSTUNREACH;	/*XXX*/
576 			goto bad;
577 		}
578 	} else {
579 		ia = ifatoia(ro->ro_rt->rt_ifa);
580 		ifp = ro->ro_rt->rt_ifp;
581 	}
582 
583 	/* make it flipped, again. */
584 	ip->ip_len = ntohs(ip->ip_len);
585 	ip->ip_off = ntohs(ip->ip_off);
586 skip_ipsec:
587 #endif /*IPSEC*/
588 #ifdef FAST_IPSEC
589 	/*
590 	 * Check the security policy (SP) for the packet and, if
591 	 * required, do IPsec-related processing.  There are two
592 	 * cases here; the first time a packet is sent through
593 	 * it will be untagged and handled by ipsec4_checkpolicy.
594 	 * If the packet is resubmitted to ip_output (e.g. after
595 	 * AH, ESP, etc. processing), there will be a tag to bypass
596 	 * the lookup and related policy checking.
597 	 */
598 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL);
599 	crit_enter();
600 	if (mtag != NULL) {
601 		tdbi = (struct tdb_ident *)m_tag_data(mtag);
602 		sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND);
603 		if (sp == NULL)
604 			error = -EINVAL;	/* force silent drop */
605 		m_tag_delete(m, mtag);
606 	} else {
607 		sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags,
608 					&error, inp);
609 	}
610 	/*
611 	 * There are four return cases:
612 	 *    sp != NULL		    apply IPsec policy
613 	 *    sp == NULL, error == 0	    no IPsec handling needed
614 	 *    sp == NULL, error == -EINVAL  discard packet w/o error
615 	 *    sp == NULL, error != 0	    discard packet, report error
616 	 */
617 	if (sp != NULL) {
618 		/* Loop detection, check if ipsec processing already done */
619 		KASSERT(sp->req != NULL, ("ip_output: no ipsec request"));
620 		for (mtag = m_tag_first(m); mtag != NULL;
621 		     mtag = m_tag_next(m, mtag)) {
622 			if (mtag->m_tag_cookie != MTAG_ABI_COMPAT)
623 				continue;
624 			if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE &&
625 			    mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED)
626 				continue;
627 			/*
628 			 * Check if policy has an SA associated with it.
629 			 * This can happen when an SP has yet to acquire
630 			 * an SA; e.g. on first reference.  If it occurs,
631 			 * then we let ipsec4_process_packet do its thing.
632 			 */
633 			if (sp->req->sav == NULL)
634 				break;
635 			tdbi = (struct tdb_ident *)m_tag_data(mtag);
636 			if (tdbi->spi == sp->req->sav->spi &&
637 			    tdbi->proto == sp->req->sav->sah->saidx.proto &&
638 			    bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst,
639 				 sizeof(union sockaddr_union)) == 0) {
640 				/*
641 				 * No IPsec processing is needed, free
642 				 * reference to SP.
643 				 *
644 				 * NB: null pointer to avoid free at
645 				 *     done: below.
646 				 */
647 				KEY_FREESP(&sp), sp = NULL;
648 				crit_exit();
649 				goto spd_done;
650 			}
651 		}
652 
653 		/*
654 		 * Do delayed checksums now because we send before
655 		 * this is done in the normal processing path.
656 		 */
657 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
658 			in_delayed_cksum(m);
659 			m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
660 		}
661 
662 		ip->ip_len = htons(ip->ip_len);
663 		ip->ip_off = htons(ip->ip_off);
664 
665 		/* NB: callee frees mbuf */
666 		error = ipsec4_process_packet(m, sp->req, flags, 0);
667 		/*
668 		 * Preserve KAME behaviour: ENOENT can be returned
669 		 * when an SA acquire is in progress.  Don't propagate
670 		 * this to user-level; it confuses applications.
671 		 *
672 		 * XXX this will go away when the SADB is redone.
673 		 */
674 		if (error == ENOENT)
675 			error = 0;
676 		crit_exit();
677 		goto done;
678 	} else {
679 		crit_exit();
680 
681 		if (error != 0) {
682 			/*
683 			 * Hack: -EINVAL is used to signal that a packet
684 			 * should be silently discarded.  This is typically
685 			 * because we asked key management for an SA and
686 			 * it was delayed (e.g. kicked up to IKE).
687 			 */
688 			if (error == -EINVAL)
689 				error = 0;
690 			goto bad;
691 		} else {
692 			/* No IPsec processing for this packet. */
693 		}
694 #ifdef notyet
695 		/*
696 		 * If deferred crypto processing is needed, check that
697 		 * the interface supports it.
698 		 */
699 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
700 		if (mtag != NULL && !(ifp->if_capenable & IFCAP_IPSEC)) {
701 			/* notify IPsec to do its own crypto */
702 			ipsp_skipcrypto_unmark((struct tdb_ident *)m_tag_data(mtag));
703 			error = EHOSTUNREACH;
704 			goto bad;
705 		}
706 #endif
707 	}
708 spd_done:
709 #endif /* FAST_IPSEC */
710 	/*
711 	 * IpHack's section.
712 	 * - Xlate: translate packet's addr/port (NAT).
713 	 * - Firewall: deny/allow/etc.
714 	 * - Wrap: fake packet's addr/port <unimpl.>
715 	 * - Encapsulate: put it in another IP and send out. <unimp.>
716 	 */
717 
718 	/*
719 	 * Run through list of hooks for output packets.
720 	 */
721 	if (pfil_has_hooks(&inet_pfil_hook)) {
722 		error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT);
723 		if (error != 0 || m == NULL)
724 			goto done;
725 		ip = mtod(m, struct ip *);
726 	}
727 
728 	/*
729 	 * Check with the firewall...
730 	 * but not if we are already being fwd'd from a firewall.
731 	 */
732 	if (fw_enable && IPFW_LOADED && !args.next_hop) {
733 		struct sockaddr_in *old = dst;
734 
735 		args.m = m;
736 		args.next_hop = dst;
737 		args.oif = ifp;
738 		off = ip_fw_chk_ptr(&args);
739 		m = args.m;
740 		dst = args.next_hop;
741 
742 		/*
743 		 * On return we must do the following:
744 		 * m == NULL	-> drop the pkt (old interface, deprecated)
745 		 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
746 		 * 1<=off<= 0xffff		-> DIVERT
747 		 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
748 		 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
749 		 * dst != old			-> IPFIREWALL_FORWARD
750 		 * off==0, dst==old		-> accept
751 		 * If some of the above modules are not compiled in, then
752 		 * we should't have to check the corresponding condition
753 		 * (because the ipfw control socket should not accept
754 		 * unsupported rules), but better play safe and drop
755 		 * packets in case of doubt.
756 		 */
757 		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
758 			if (m)
759 				m_freem(m);
760 			error = EACCES;
761 			goto done;
762 		}
763 		ip = mtod(m, struct ip *);
764 		if (off == 0 && dst == old)		/* common case */
765 			goto pass;
766 		if (off & IP_FW_PORT_DYNT_FLAG) {
767 			/*
768 			 * pass the pkt to dummynet. Need to include
769 			 * pipe number, m, ifp, ro, dst because these are
770 			 * not recomputed in the next pass.
771 			 * All other parameters have been already used and
772 			 * so they are not needed anymore.
773 			 * XXX note: if the ifp or ro entry are deleted
774 			 * while a pkt is in dummynet, we are in trouble!
775 			 */
776 			args.ro = ro;
777 			args.dst = dst;
778 			args.flags = flags;
779 
780 			error = 0;
781 			ip_fw_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT, &args);
782 			goto done;
783 		}
784 #ifdef IPDIVERT
785 		if (off != 0 && !(off & IP_FW_PORT_DYNT_FLAG)) {
786 			struct mbuf *clone = NULL;
787 
788 			/* Clone packet if we're doing a 'tee' */
789 			if ((off & IP_FW_PORT_TEE_FLAG))
790 				clone = m_dup(m, MB_DONTWAIT);
791 
792 			/*
793 			 * XXX
794 			 * delayed checksums are not currently compatible
795 			 * with divert sockets.
796 			 */
797 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
798 				in_delayed_cksum(m);
799 				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
800 			}
801 
802 			/* Restore packet header fields to original values */
803 			ip->ip_len = htons(ip->ip_len);
804 			ip->ip_off = htons(ip->ip_off);
805 
806 			/* Deliver packet to divert input routine */
807 			divert_packet(m, 0, off & 0xffff);
808 
809 			/* If 'tee', continue with original packet */
810 			if (clone != NULL) {
811 				m = clone;
812 				ip = mtod(m, struct ip *);
813 				goto pass;
814 			}
815 			goto done;
816 		}
817 #endif
818 
819 		/* IPFIREWALL_FORWARD */
820 		/*
821 		 * Check dst to make sure it is directly reachable on the
822 		 * interface we previously thought it was.
823 		 * If it isn't (which may be likely in some situations) we have
824 		 * to re-route it (ie, find a route for the next-hop and the
825 		 * associated interface) and set them here. This is nested
826 		 * forwarding which in most cases is undesirable, except where
827 		 * such control is nigh impossible. So we do it here.
828 		 * And I'm babbling.
829 		 */
830 		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
831 #if 0
832 			/*
833 			 * XXX To improve readability, this block should be
834 			 * changed into a function call as below:
835 			 */
836 			error = ip_ipforward(&m, &dst, &ifp);
837 			if (error)
838 				goto bad;
839 			if (m == NULL) /* ip_input consumed the mbuf */
840 				goto done;
841 #else
842 			struct in_ifaddr *ia;
843 			struct in_ifaddr_container *iac;
844 
845 			/*
846 			 * XXX sro_fwd below is static, and a pointer
847 			 * to it gets passed to routines downstream.
848 			 * This could have surprisingly bad results in
849 			 * practice, because its content is overwritten
850 			 * by subsequent packets.
851 			 */
852 			/* There must be a better way to do this next line... */
853 			static struct route sro_fwd;
854 			struct route *ro_fwd = &sro_fwd;
855 
856 #if 0
857 			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
858 			    dst->sin_addr, "\n");
859 #endif
860 
861 			/*
862 			 * We need to figure out if we have been forwarded
863 			 * to a local socket. If so, then we should somehow
864 			 * "loop back" to ip_input, and get directed to the
865 			 * PCB as if we had received this packet. This is
866 			 * because it may be dificult to identify the packets
867 			 * you want to forward until they are being output
868 			 * and have selected an interface. (e.g. locally
869 			 * initiated packets) If we used the loopback inteface,
870 			 * we would not be able to control what happens
871 			 * as the packet runs through ip_input() as
872 			 * it is done through a ISR.
873 			 */
874 			ia = NULL;
875 			LIST_FOREACH(iac, INADDR_HASH(dst->sin_addr.s_addr),
876 				     ia_hash) {
877 				/*
878 				 * If the addr to forward to is one
879 				 * of ours, we pretend to
880 				 * be the destination for this packet.
881 				 */
882 				if (IA_SIN(iac->ia)->sin_addr.s_addr ==
883 				    dst->sin_addr.s_addr) {
884 					ia = iac->ia;
885 					break;
886 				}
887 			}
888 			if (ia != NULL) {    /* tell ip_input "dont filter" */
889 				struct m_hdr tag;
890 
891 				tag.mh_type = MT_TAG;
892 				tag.mh_flags = PACKET_TAG_IPFORWARD;
893 				tag.mh_data = (caddr_t)args.next_hop;
894 				tag.mh_next = m;
895 
896 				if (m->m_pkthdr.rcvif == NULL)
897 					m->m_pkthdr.rcvif = ifunit("lo0");
898 				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
899 					m->m_pkthdr.csum_flags |=
900 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
901 					m->m_pkthdr.csum_data = 0xffff;
902 				}
903 				m->m_pkthdr.csum_flags |=
904 				    CSUM_IP_CHECKED | CSUM_IP_VALID;
905 				ip->ip_len = htons(ip->ip_len);
906 				ip->ip_off = htons(ip->ip_off);
907 				ip_input((struct mbuf *)&tag);
908 				goto done;
909 			}
910 			/* Some of the logic for this was nicked from above.
911 			 *
912 			 * This rewrites the cached route in a local PCB.
913 			 * Is this what we want to do?
914 			 */
915 			bcopy(dst, &ro_fwd->ro_dst, sizeof *dst);
916 			ro_fwd->ro_rt = NULL;
917 
918 			rtalloc_ign(ro_fwd, RTF_PRCLONING);
919 			if (ro_fwd->ro_rt == NULL) {
920 				ipstat.ips_noroute++;
921 				error = EHOSTUNREACH;
922 				goto bad;
923 			}
924 
925 			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
926 			ifp = ro_fwd->ro_rt->rt_ifp;
927 			ro_fwd->ro_rt->rt_use++;
928 			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
929 				dst = (struct sockaddr_in *)
930 				    ro_fwd->ro_rt->rt_gateway;
931 			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
932 				isbroadcast =
933 				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
934 			else
935 				isbroadcast = in_broadcast(dst->sin_addr, ifp);
936 			if (ro->ro_rt != NULL)
937 				rtfree(ro->ro_rt);
938 			ro->ro_rt = ro_fwd->ro_rt;
939 			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
940 
941 #endif	/* ... block to be put into a function */
942 			/*
943 			 * If we added a default src ip earlier,
944 			 * which would have been gotten from the-then
945 			 * interface, do it again, from the new one.
946 			 */
947 			if (src_was_INADDR_ANY)
948 				ip->ip_src = IA_SIN(ia)->sin_addr;
949 			goto pass ;
950 		}
951 
952 		/*
953 		 * if we get here, none of the above matches, and
954 		 * we have to drop the pkt
955 		 */
956 		m_freem(m);
957 		error = EACCES; /* not sure this is the right error msg */
958 		goto done;
959 	}
960 
961 pass:
962 	/* 127/8 must not appear on wire - RFC1122. */
963 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
964 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
965 		if (!(ifp->if_flags & IFF_LOOPBACK)) {
966 			ipstat.ips_badaddr++;
967 			error = EADDRNOTAVAIL;
968 			goto bad;
969 		}
970 	}
971 
972 	m->m_pkthdr.csum_flags |= CSUM_IP;
973 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
974 	if (sw_csum & CSUM_DELAY_DATA) {
975 		in_delayed_cksum(m);
976 		sw_csum &= ~CSUM_DELAY_DATA;
977 	}
978 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
979 
980 	/*
981 	 * If small enough for interface, or the interface will take
982 	 * care of the fragmentation for us, can just send directly.
983 	 */
984 	if (ip->ip_len <= ifp->if_mtu || ((ifp->if_hwassist & CSUM_FRAGMENT) &&
985 	    !(ip->ip_off & IP_DF))) {
986 		ip->ip_len = htons(ip->ip_len);
987 		ip->ip_off = htons(ip->ip_off);
988 		ip->ip_sum = 0;
989 		if (sw_csum & CSUM_DELAY_IP) {
990 			if (ip->ip_vhl == IP_VHL_BORING) {
991 				ip->ip_sum = in_cksum_hdr(ip);
992 			} else {
993 				ip->ip_sum = in_cksum(m, hlen);
994 			}
995 		}
996 
997 		/* Record statistics for this interface address. */
998 		if (!(flags & IP_FORWARDING) && ia) {
999 			ia->ia_ifa.if_opackets++;
1000 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1001 		}
1002 
1003 #ifdef IPSEC
1004 		/* clean ipsec history once it goes out of the node */
1005 		ipsec_delaux(m);
1006 #endif
1007 
1008 #ifdef MBUF_STRESS_TEST
1009 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) {
1010 			struct mbuf *m1, *m2;
1011 			int length, tmp;
1012 
1013 			tmp = length = m->m_pkthdr.len;
1014 
1015 			while ((length -= mbuf_frag_size) >= 1) {
1016 				m1 = m_split(m, length, MB_DONTWAIT);
1017 				if (m1 == NULL)
1018 					break;
1019 				m2 = m;
1020 				while (m2->m_next != NULL)
1021 					m2 = m2->m_next;
1022 				m2->m_next = m1;
1023 			}
1024 			m->m_pkthdr.len = tmp;
1025 		}
1026 #endif
1027 
1028 #ifdef MPLS
1029 		struct rtentry *send_route = ro->ro_rt;	/* copy-in/copy-out parameter */
1030 
1031 		if (!mpls_output_process(ifp, m, &dst, send_route))
1032 			goto done;
1033 #endif
1034 		error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
1035 				       ro->ro_rt);
1036 		goto done;
1037 	}
1038 
1039 	if (ip->ip_off & IP_DF) {
1040 		error = EMSGSIZE;
1041 		/*
1042 		 * This case can happen if the user changed the MTU
1043 		 * of an interface after enabling IP on it.  Because
1044 		 * most netifs don't keep track of routes pointing to
1045 		 * them, there is no way for one to update all its
1046 		 * routes when the MTU is changed.
1047 		 */
1048 		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
1049 		    !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) &&
1050 		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
1051 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
1052 		}
1053 		ipstat.ips_cantfrag++;
1054 		goto bad;
1055 	}
1056 
1057 	/*
1058 	 * Too large for interface; fragment if possible. If successful,
1059 	 * on return, m will point to a list of packets to be sent.
1060 	 */
1061 	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
1062 	if (error)
1063 		goto bad;
1064 	for (; m; m = m0) {
1065 		m0 = m->m_nextpkt;
1066 		m->m_nextpkt = NULL;
1067 #ifdef IPSEC
1068 		/* clean ipsec history once it goes out of the node */
1069 		ipsec_delaux(m);
1070 #endif
1071 		if (error == 0) {
1072 			/* Record statistics for this interface address. */
1073 			if (ia != NULL) {
1074 				ia->ia_ifa.if_opackets++;
1075 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1076 			}
1077 #ifdef MPLS
1078 		struct rtentry *send_route = ro->ro_rt;	/* copy-in/copy-out parameter */
1079 
1080 		if (!mpls_output_process(ifp, m, &dst, send_route))
1081 			goto done;
1082 #endif
1083 			error = ifp->if_output(ifp, m, (struct sockaddr *)dst,
1084 					       ro->ro_rt);
1085 		} else {
1086 			m_freem(m);
1087 		}
1088 	}
1089 
1090 	if (error == 0)
1091 		ipstat.ips_fragmented++;
1092 
1093 done:
1094 	if (ro == &iproute && ro->ro_rt != NULL) {
1095 		RTFREE(ro->ro_rt);
1096 		ro->ro_rt = NULL;
1097 	}
1098 #ifdef IPSEC
1099 	if (sp != NULL) {
1100 		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1101 			kprintf("DP ip_output call free SP:%p\n", sp));
1102 		key_freesp(sp);
1103 	}
1104 #endif
1105 #ifdef FAST_IPSEC
1106 	if (sp != NULL)
1107 		KEY_FREESP(&sp);
1108 #endif
1109 	if (dn_mtag != NULL)
1110 		m_tag_free(dn_mtag);
1111 
1112 	return (error);
1113 bad:
1114 	m_freem(m);
1115 	goto done;
1116 }
1117 
1118 /*
1119  * Create a chain of fragments which fit the given mtu. m_frag points to the
1120  * mbuf to be fragmented; on return it points to the chain with the fragments.
1121  * Return 0 if no error. If error, m_frag may contain a partially built
1122  * chain of fragments that should be freed by the caller.
1123  *
1124  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
1125  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
1126  */
1127 int
1128 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
1129 	    u_long if_hwassist_flags, int sw_csum)
1130 {
1131 	int error = 0;
1132 	int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1133 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
1134 	int off;
1135 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
1136 	int firstlen;
1137 	struct mbuf **mnext;
1138 	int nfrags;
1139 
1140 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
1141 		ipstat.ips_cantfrag++;
1142 		return EMSGSIZE;
1143 	}
1144 
1145 	/*
1146 	 * Must be able to put at least 8 bytes per fragment.
1147 	 */
1148 	if (len < 8)
1149 		return EMSGSIZE;
1150 
1151 	/*
1152 	 * If the interface will not calculate checksums on
1153 	 * fragmented packets, then do it here.
1154 	 */
1155 	if ((m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) &&
1156 	    !(if_hwassist_flags & CSUM_IP_FRAGS)) {
1157 		in_delayed_cksum(m0);
1158 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1159 	}
1160 
1161 	if (len > PAGE_SIZE) {
1162 		/*
1163 		 * Fragment large datagrams such that each segment
1164 		 * contains a multiple of PAGE_SIZE amount of data,
1165 		 * plus headers. This enables a receiver to perform
1166 		 * page-flipping zero-copy optimizations.
1167 		 *
1168 		 * XXX When does this help given that sender and receiver
1169 		 * could have different page sizes, and also mtu could
1170 		 * be less than the receiver's page size ?
1171 		 */
1172 		int newlen;
1173 		struct mbuf *m;
1174 
1175 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
1176 			off += m->m_len;
1177 
1178 		/*
1179 		 * firstlen (off - hlen) must be aligned on an
1180 		 * 8-byte boundary
1181 		 */
1182 		if (off < hlen)
1183 			goto smart_frag_failure;
1184 		off = ((off - hlen) & ~7) + hlen;
1185 		newlen = (~PAGE_MASK) & mtu;
1186 		if ((newlen + sizeof(struct ip)) > mtu) {
1187 			/* we failed, go back the default */
1188 smart_frag_failure:
1189 			newlen = len;
1190 			off = hlen + len;
1191 		}
1192 		len = newlen;
1193 
1194 	} else {
1195 		off = hlen + len;
1196 	}
1197 
1198 	firstlen = off - hlen;
1199 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
1200 
1201 	/*
1202 	 * Loop through length of segment after first fragment,
1203 	 * make new header and copy data of each part and link onto chain.
1204 	 * Here, m0 is the original packet, m is the fragment being created.
1205 	 * The fragments are linked off the m_nextpkt of the original
1206 	 * packet, which after processing serves as the first fragment.
1207 	 */
1208 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
1209 		struct ip *mhip;	/* ip header on the fragment */
1210 		struct mbuf *m;
1211 		int mhlen = sizeof(struct ip);
1212 
1213 		MGETHDR(m, MB_DONTWAIT, MT_HEADER);
1214 		if (m == NULL) {
1215 			error = ENOBUFS;
1216 			ipstat.ips_odropped++;
1217 			goto done;
1218 		}
1219 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1220 		/*
1221 		 * In the first mbuf, leave room for the link header, then
1222 		 * copy the original IP header including options. The payload
1223 		 * goes into an additional mbuf chain returned by m_copy().
1224 		 */
1225 		m->m_data += max_linkhdr;
1226 		mhip = mtod(m, struct ip *);
1227 		*mhip = *ip;
1228 		if (hlen > sizeof(struct ip)) {
1229 			mhlen = ip_optcopy(ip, mhip) + sizeof(struct ip);
1230 			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
1231 		}
1232 		m->m_len = mhlen;
1233 		/* XXX do we need to add ip->ip_off below ? */
1234 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
1235 		if (off + len >= ip->ip_len) {	/* last fragment */
1236 			len = ip->ip_len - off;
1237 			m->m_flags |= M_LASTFRAG;
1238 		} else
1239 			mhip->ip_off |= IP_MF;
1240 		mhip->ip_len = htons((u_short)(len + mhlen));
1241 		m->m_next = m_copy(m0, off, len);
1242 		if (m->m_next == NULL) {		/* copy failed */
1243 			m_free(m);
1244 			error = ENOBUFS;	/* ??? */
1245 			ipstat.ips_odropped++;
1246 			goto done;
1247 		}
1248 		m->m_pkthdr.len = mhlen + len;
1249 		m->m_pkthdr.rcvif = (struct ifnet *)NULL;
1250 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1251 		mhip->ip_off = htons(mhip->ip_off);
1252 		mhip->ip_sum = 0;
1253 		if (sw_csum & CSUM_DELAY_IP)
1254 			mhip->ip_sum = in_cksum(m, mhlen);
1255 		*mnext = m;
1256 		mnext = &m->m_nextpkt;
1257 	}
1258 	ipstat.ips_ofragments += nfrags;
1259 
1260 	/* set first marker for fragment chain */
1261 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1262 	m0->m_pkthdr.csum_data = nfrags;
1263 
1264 	/*
1265 	 * Update first fragment by trimming what's been copied out
1266 	 * and updating header.
1267 	 */
1268 	m_adj(m0, hlen + firstlen - ip->ip_len);
1269 	m0->m_pkthdr.len = hlen + firstlen;
1270 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
1271 	ip->ip_off |= IP_MF;
1272 	ip->ip_off = htons(ip->ip_off);
1273 	ip->ip_sum = 0;
1274 	if (sw_csum & CSUM_DELAY_IP)
1275 		ip->ip_sum = in_cksum(m0, hlen);
1276 
1277 done:
1278 	*m_frag = m0;
1279 	return error;
1280 }
1281 
1282 void
1283 in_delayed_cksum(struct mbuf *m)
1284 {
1285 	struct ip *ip;
1286 	u_short csum, offset;
1287 
1288 	ip = mtod(m, struct ip *);
1289 	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1290 	csum = in_cksum_skip(m, ip->ip_len, offset);
1291 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1292 		csum = 0xffff;
1293 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1294 
1295 	if (offset + sizeof(u_short) > m->m_len) {
1296 		kprintf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1297 		    m->m_len, offset, ip->ip_p);
1298 		/*
1299 		 * XXX
1300 		 * this shouldn't happen, but if it does, the
1301 		 * correct behavior may be to insert the checksum
1302 		 * in the existing chain instead of rearranging it.
1303 		 */
1304 		m = m_pullup(m, offset + sizeof(u_short));
1305 	}
1306 	*(u_short *)(m->m_data + offset) = csum;
1307 }
1308 
1309 /*
1310  * Insert IP options into preformed packet.
1311  * Adjust IP destination as required for IP source routing,
1312  * as indicated by a non-zero in_addr at the start of the options.
1313  *
1314  * XXX This routine assumes that the packet has no options in place.
1315  */
1316 static struct mbuf *
1317 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1318 {
1319 	struct ipoption *p = mtod(opt, struct ipoption *);
1320 	struct mbuf *n;
1321 	struct ip *ip = mtod(m, struct ip *);
1322 	unsigned optlen;
1323 
1324 	optlen = opt->m_len - sizeof p->ipopt_dst;
1325 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
1326 		*phlen = 0;
1327 		return (m);		/* XXX should fail */
1328 	}
1329 	if (p->ipopt_dst.s_addr)
1330 		ip->ip_dst = p->ipopt_dst;
1331 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1332 		MGETHDR(n, MB_DONTWAIT, MT_HEADER);
1333 		if (n == NULL) {
1334 			*phlen = 0;
1335 			return (m);
1336 		}
1337 		n->m_pkthdr.rcvif = (struct ifnet *)NULL;
1338 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1339 		m->m_len -= sizeof(struct ip);
1340 		m->m_data += sizeof(struct ip);
1341 		n->m_next = m;
1342 		m = n;
1343 		m->m_len = optlen + sizeof(struct ip);
1344 		m->m_data += max_linkhdr;
1345 		memcpy(mtod(m, void *), ip, sizeof(struct ip));
1346 	} else {
1347 		m->m_data -= optlen;
1348 		m->m_len += optlen;
1349 		m->m_pkthdr.len += optlen;
1350 		ovbcopy(ip, mtod(m, caddr_t), sizeof(struct ip));
1351 	}
1352 	ip = mtod(m, struct ip *);
1353 	bcopy(p->ipopt_list, ip + 1, optlen);
1354 	*phlen = sizeof(struct ip) + optlen;
1355 	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1356 	ip->ip_len += optlen;
1357 	return (m);
1358 }
1359 
1360 /*
1361  * Copy options from ip to jp,
1362  * omitting those not copied during fragmentation.
1363  */
1364 int
1365 ip_optcopy(struct ip *ip, struct ip *jp)
1366 {
1367 	u_char *cp, *dp;
1368 	int opt, optlen, cnt;
1369 
1370 	cp = (u_char *)(ip + 1);
1371 	dp = (u_char *)(jp + 1);
1372 	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof(struct ip);
1373 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1374 		opt = cp[0];
1375 		if (opt == IPOPT_EOL)
1376 			break;
1377 		if (opt == IPOPT_NOP) {
1378 			/* Preserve for IP mcast tunnel's LSRR alignment. */
1379 			*dp++ = IPOPT_NOP;
1380 			optlen = 1;
1381 			continue;
1382 		}
1383 
1384 		KASSERT(cnt >= IPOPT_OLEN + sizeof *cp,
1385 		    ("ip_optcopy: malformed ipv4 option"));
1386 		optlen = cp[IPOPT_OLEN];
1387 		KASSERT(optlen >= IPOPT_OLEN + sizeof *cp && optlen <= cnt,
1388 		    ("ip_optcopy: malformed ipv4 option"));
1389 
1390 		/* bogus lengths should have been caught by ip_dooptions */
1391 		if (optlen > cnt)
1392 			optlen = cnt;
1393 		if (IPOPT_COPIED(opt)) {
1394 			bcopy(cp, dp, optlen);
1395 			dp += optlen;
1396 		}
1397 	}
1398 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1399 		*dp++ = IPOPT_EOL;
1400 	return (optlen);
1401 }
1402 
1403 /*
1404  * IP socket option processing.
1405  */
1406 int
1407 ip_ctloutput(struct socket *so, struct sockopt *sopt)
1408 {
1409 	struct	inpcb *inp = so->so_pcb;
1410 	int	error, optval;
1411 
1412 	error = optval = 0;
1413 	if (sopt->sopt_level != IPPROTO_IP) {
1414 		return (EINVAL);
1415 	}
1416 
1417 	switch (sopt->sopt_dir) {
1418 	case SOPT_SET:
1419 		switch (sopt->sopt_name) {
1420 		case IP_OPTIONS:
1421 #ifdef notyet
1422 		case IP_RETOPTS:
1423 #endif
1424 		{
1425 			struct mbuf *m;
1426 			if (sopt->sopt_valsize > MLEN) {
1427 				error = EMSGSIZE;
1428 				break;
1429 			}
1430 			MGET(m, sopt->sopt_td ? MB_WAIT : MB_DONTWAIT, MT_HEADER);
1431 			if (m == NULL) {
1432 				error = ENOBUFS;
1433 				break;
1434 			}
1435 			m->m_len = sopt->sopt_valsize;
1436 			error = soopt_to_kbuf(sopt, mtod(m, void *), m->m_len,
1437 					      m->m_len);
1438 			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1439 					   m));
1440 		}
1441 
1442 		case IP_TOS:
1443 		case IP_TTL:
1444 		case IP_MINTTL:
1445 		case IP_RECVOPTS:
1446 		case IP_RECVRETOPTS:
1447 		case IP_RECVDSTADDR:
1448 		case IP_RECVIF:
1449 		case IP_RECVTTL:
1450 		case IP_FAITH:
1451 			error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1452 					     sizeof optval);
1453 			if (error)
1454 				break;
1455 			switch (sopt->sopt_name) {
1456 			case IP_TOS:
1457 				inp->inp_ip_tos = optval;
1458 				break;
1459 
1460 			case IP_TTL:
1461 				inp->inp_ip_ttl = optval;
1462 				break;
1463 			case IP_MINTTL:
1464 				if (optval > 0 && optval <= MAXTTL)
1465 					inp->inp_ip_minttl = optval;
1466 				else
1467 					error = EINVAL;
1468 				break;
1469 #define	OPTSET(bit) \
1470 	if (optval) \
1471 		inp->inp_flags |= bit; \
1472 	else \
1473 		inp->inp_flags &= ~bit;
1474 
1475 			case IP_RECVOPTS:
1476 				OPTSET(INP_RECVOPTS);
1477 				break;
1478 
1479 			case IP_RECVRETOPTS:
1480 				OPTSET(INP_RECVRETOPTS);
1481 				break;
1482 
1483 			case IP_RECVDSTADDR:
1484 				OPTSET(INP_RECVDSTADDR);
1485 				break;
1486 
1487 			case IP_RECVIF:
1488 				OPTSET(INP_RECVIF);
1489 				break;
1490 
1491 			case IP_RECVTTL:
1492 				OPTSET(INP_RECVTTL);
1493 				break;
1494 
1495 			case IP_FAITH:
1496 				OPTSET(INP_FAITH);
1497 				break;
1498 			}
1499 			break;
1500 #undef OPTSET
1501 
1502 		case IP_MULTICAST_IF:
1503 		case IP_MULTICAST_VIF:
1504 		case IP_MULTICAST_TTL:
1505 		case IP_MULTICAST_LOOP:
1506 		case IP_ADD_MEMBERSHIP:
1507 		case IP_DROP_MEMBERSHIP:
1508 			error = ip_setmoptions(sopt, &inp->inp_moptions);
1509 			break;
1510 
1511 		case IP_PORTRANGE:
1512 			error = soopt_to_kbuf(sopt, &optval, sizeof optval,
1513 					    sizeof optval);
1514 			if (error)
1515 				break;
1516 
1517 			switch (optval) {
1518 			case IP_PORTRANGE_DEFAULT:
1519 				inp->inp_flags &= ~(INP_LOWPORT);
1520 				inp->inp_flags &= ~(INP_HIGHPORT);
1521 				break;
1522 
1523 			case IP_PORTRANGE_HIGH:
1524 				inp->inp_flags &= ~(INP_LOWPORT);
1525 				inp->inp_flags |= INP_HIGHPORT;
1526 				break;
1527 
1528 			case IP_PORTRANGE_LOW:
1529 				inp->inp_flags &= ~(INP_HIGHPORT);
1530 				inp->inp_flags |= INP_LOWPORT;
1531 				break;
1532 
1533 			default:
1534 				error = EINVAL;
1535 				break;
1536 			}
1537 			break;
1538 
1539 #if defined(IPSEC) || defined(FAST_IPSEC)
1540 		case IP_IPSEC_POLICY:
1541 		{
1542 			caddr_t req;
1543 			size_t len = 0;
1544 			int priv;
1545 			struct mbuf *m;
1546 			int optname;
1547 
1548 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1549 				break;
1550 			soopt_to_mbuf(sopt, m);
1551 			priv = (sopt->sopt_td != NULL &&
1552 				suser(sopt->sopt_td) != 0) ? 0 : 1;
1553 			req = mtod(m, caddr_t);
1554 			len = m->m_len;
1555 			optname = sopt->sopt_name;
1556 			error = ipsec4_set_policy(inp, optname, req, len, priv);
1557 			m_freem(m);
1558 			break;
1559 		}
1560 #endif /*IPSEC*/
1561 
1562 		default:
1563 			error = ENOPROTOOPT;
1564 			break;
1565 		}
1566 		break;
1567 
1568 	case SOPT_GET:
1569 		switch (sopt->sopt_name) {
1570 		case IP_OPTIONS:
1571 		case IP_RETOPTS:
1572 			if (inp->inp_options)
1573 				soopt_from_kbuf(sopt, mtod(inp->inp_options,
1574 							   char *),
1575 						inp->inp_options->m_len);
1576 			else
1577 				sopt->sopt_valsize = 0;
1578 			break;
1579 
1580 		case IP_TOS:
1581 		case IP_TTL:
1582 		case IP_MINTTL:
1583 		case IP_RECVOPTS:
1584 		case IP_RECVRETOPTS:
1585 		case IP_RECVDSTADDR:
1586 		case IP_RECVTTL:
1587 		case IP_RECVIF:
1588 		case IP_PORTRANGE:
1589 		case IP_FAITH:
1590 			switch (sopt->sopt_name) {
1591 
1592 			case IP_TOS:
1593 				optval = inp->inp_ip_tos;
1594 				break;
1595 
1596 			case IP_TTL:
1597 				optval = inp->inp_ip_ttl;
1598 				break;
1599 			case IP_MINTTL:
1600 				optval = inp->inp_ip_minttl;
1601 				break;
1602 
1603 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1604 
1605 			case IP_RECVOPTS:
1606 				optval = OPTBIT(INP_RECVOPTS);
1607 				break;
1608 
1609 			case IP_RECVRETOPTS:
1610 				optval = OPTBIT(INP_RECVRETOPTS);
1611 				break;
1612 
1613 			case IP_RECVDSTADDR:
1614 				optval = OPTBIT(INP_RECVDSTADDR);
1615 				break;
1616 
1617 			case IP_RECVTTL:
1618 				optval = OPTBIT(INP_RECVTTL);
1619 				break;
1620 
1621 			case IP_RECVIF:
1622 				optval = OPTBIT(INP_RECVIF);
1623 				break;
1624 
1625 			case IP_PORTRANGE:
1626 				if (inp->inp_flags & INP_HIGHPORT)
1627 					optval = IP_PORTRANGE_HIGH;
1628 				else if (inp->inp_flags & INP_LOWPORT)
1629 					optval = IP_PORTRANGE_LOW;
1630 				else
1631 					optval = 0;
1632 				break;
1633 
1634 			case IP_FAITH:
1635 				optval = OPTBIT(INP_FAITH);
1636 				break;
1637 			}
1638 			soopt_from_kbuf(sopt, &optval, sizeof optval);
1639 			break;
1640 
1641 		case IP_MULTICAST_IF:
1642 		case IP_MULTICAST_VIF:
1643 		case IP_MULTICAST_TTL:
1644 		case IP_MULTICAST_LOOP:
1645 		case IP_ADD_MEMBERSHIP:
1646 		case IP_DROP_MEMBERSHIP:
1647 			error = ip_getmoptions(sopt, inp->inp_moptions);
1648 			break;
1649 
1650 #if defined(IPSEC) || defined(FAST_IPSEC)
1651 		case IP_IPSEC_POLICY:
1652 		{
1653 			struct mbuf *m = NULL;
1654 			caddr_t req = NULL;
1655 			size_t len = 0;
1656 
1657 			if (m != NULL) {
1658 				req = mtod(m, caddr_t);
1659 				len = m->m_len;
1660 			}
1661 			error = ipsec4_get_policy(so->so_pcb, req, len, &m);
1662 			if (error == 0)
1663 				error = soopt_from_mbuf(sopt, m); /* XXX */
1664 			if (error == 0)
1665 				m_freem(m);
1666 			break;
1667 		}
1668 #endif /*IPSEC*/
1669 
1670 		default:
1671 			error = ENOPROTOOPT;
1672 			break;
1673 		}
1674 		break;
1675 	}
1676 	return (error);
1677 }
1678 
1679 /*
1680  * Set up IP options in pcb for insertion in output packets.
1681  * Store in mbuf with pointer in pcbopt, adding pseudo-option
1682  * with destination address if source routed.
1683  */
1684 static int
1685 ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
1686 {
1687 	int cnt, optlen;
1688 	u_char *cp;
1689 	u_char opt;
1690 
1691 	/* turn off any old options */
1692 	if (*pcbopt)
1693 		m_free(*pcbopt);
1694 	*pcbopt = 0;
1695 	if (m == NULL || m->m_len == 0) {
1696 		/*
1697 		 * Only turning off any previous options.
1698 		 */
1699 		if (m != NULL)
1700 			m_free(m);
1701 		return (0);
1702 	}
1703 
1704 	if (m->m_len % sizeof(int32_t))
1705 		goto bad;
1706 	/*
1707 	 * IP first-hop destination address will be stored before
1708 	 * actual options; move other options back
1709 	 * and clear it when none present.
1710 	 */
1711 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1712 		goto bad;
1713 	cnt = m->m_len;
1714 	m->m_len += sizeof(struct in_addr);
1715 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1716 	ovbcopy(mtod(m, caddr_t), cp, cnt);
1717 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1718 
1719 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1720 		opt = cp[IPOPT_OPTVAL];
1721 		if (opt == IPOPT_EOL)
1722 			break;
1723 		if (opt == IPOPT_NOP)
1724 			optlen = 1;
1725 		else {
1726 			if (cnt < IPOPT_OLEN + sizeof *cp)
1727 				goto bad;
1728 			optlen = cp[IPOPT_OLEN];
1729 			if (optlen < IPOPT_OLEN + sizeof *cp || optlen > cnt)
1730 				goto bad;
1731 		}
1732 		switch (opt) {
1733 
1734 		default:
1735 			break;
1736 
1737 		case IPOPT_LSRR:
1738 		case IPOPT_SSRR:
1739 			/*
1740 			 * user process specifies route as:
1741 			 *	->A->B->C->D
1742 			 * D must be our final destination (but we can't
1743 			 * check that since we may not have connected yet).
1744 			 * A is first hop destination, which doesn't appear in
1745 			 * actual IP option, but is stored before the options.
1746 			 */
1747 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1748 				goto bad;
1749 			m->m_len -= sizeof(struct in_addr);
1750 			cnt -= sizeof(struct in_addr);
1751 			optlen -= sizeof(struct in_addr);
1752 			cp[IPOPT_OLEN] = optlen;
1753 			/*
1754 			 * Move first hop before start of options.
1755 			 */
1756 			bcopy(&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1757 			      sizeof(struct in_addr));
1758 			/*
1759 			 * Then copy rest of options back
1760 			 * to close up the deleted entry.
1761 			 */
1762 			ovbcopy(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr),
1763 				&cp[IPOPT_OFFSET+1],
1764 				cnt - (IPOPT_MINOFF - 1));
1765 			break;
1766 		}
1767 	}
1768 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1769 		goto bad;
1770 	*pcbopt = m;
1771 	return (0);
1772 
1773 bad:
1774 	m_free(m);
1775 	return (EINVAL);
1776 }
1777 
1778 /*
1779  * XXX
1780  * The whole multicast option thing needs to be re-thought.
1781  * Several of these options are equally applicable to non-multicast
1782  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1783  * standard option (IP_TTL).
1784  */
1785 
1786 /*
1787  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1788  */
1789 static struct ifnet *
1790 ip_multicast_if(struct in_addr *a, int *ifindexp)
1791 {
1792 	int ifindex;
1793 	struct ifnet *ifp;
1794 
1795 	if (ifindexp)
1796 		*ifindexp = 0;
1797 	if (ntohl(a->s_addr) >> 24 == 0) {
1798 		ifindex = ntohl(a->s_addr) & 0xffffff;
1799 		if (ifindex < 0 || if_index < ifindex)
1800 			return NULL;
1801 		ifp = ifindex2ifnet[ifindex];
1802 		if (ifindexp)
1803 			*ifindexp = ifindex;
1804 	} else {
1805 		ifp = INADDR_TO_IFP(a);
1806 	}
1807 	return ifp;
1808 }
1809 
1810 /*
1811  * Set the IP multicast options in response to user setsockopt().
1812  */
1813 static int
1814 ip_setmoptions(struct sockopt *sopt, struct ip_moptions **imop)
1815 {
1816 	int error = 0;
1817 	int i;
1818 	struct in_addr addr;
1819 	struct ip_mreq mreq;
1820 	struct ifnet *ifp;
1821 	struct ip_moptions *imo = *imop;
1822 	int ifindex;
1823 
1824 	if (imo == NULL) {
1825 		/*
1826 		 * No multicast option buffer attached to the pcb;
1827 		 * allocate one and initialize to default values.
1828 		 */
1829 		imo = kmalloc(sizeof *imo, M_IPMOPTS, M_WAITOK);
1830 
1831 		*imop = imo;
1832 		imo->imo_multicast_ifp = NULL;
1833 		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1834 		imo->imo_multicast_vif = -1;
1835 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1836 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1837 		imo->imo_num_memberships = 0;
1838 	}
1839 	switch (sopt->sopt_name) {
1840 	/* store an index number for the vif you wanna use in the send */
1841 	case IP_MULTICAST_VIF:
1842 		if (legal_vif_num == 0) {
1843 			error = EOPNOTSUPP;
1844 			break;
1845 		}
1846 		error = soopt_to_kbuf(sopt, &i, sizeof i, sizeof i);
1847 		if (error)
1848 			break;
1849 		if (!legal_vif_num(i) && (i != -1)) {
1850 			error = EINVAL;
1851 			break;
1852 		}
1853 		imo->imo_multicast_vif = i;
1854 		break;
1855 
1856 	case IP_MULTICAST_IF:
1857 		/*
1858 		 * Select the interface for outgoing multicast packets.
1859 		 */
1860 		error = soopt_to_kbuf(sopt, &addr, sizeof addr, sizeof addr);
1861 		if (error)
1862 			break;
1863 
1864 		/*
1865 		 * INADDR_ANY is used to remove a previous selection.
1866 		 * When no interface is selected, a default one is
1867 		 * chosen every time a multicast packet is sent.
1868 		 */
1869 		if (addr.s_addr == INADDR_ANY) {
1870 			imo->imo_multicast_ifp = NULL;
1871 			break;
1872 		}
1873 		/*
1874 		 * The selected interface is identified by its local
1875 		 * IP address.  Find the interface and confirm that
1876 		 * it supports multicasting.
1877 		 */
1878 		crit_enter();
1879 		ifp = ip_multicast_if(&addr, &ifindex);
1880 		if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1881 			crit_exit();
1882 			error = EADDRNOTAVAIL;
1883 			break;
1884 		}
1885 		imo->imo_multicast_ifp = ifp;
1886 		if (ifindex)
1887 			imo->imo_multicast_addr = addr;
1888 		else
1889 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1890 		crit_exit();
1891 		break;
1892 
1893 	case IP_MULTICAST_TTL:
1894 		/*
1895 		 * Set the IP time-to-live for outgoing multicast packets.
1896 		 * The original multicast API required a char argument,
1897 		 * which is inconsistent with the rest of the socket API.
1898 		 * We allow either a char or an int.
1899 		 */
1900 		if (sopt->sopt_valsize == 1) {
1901 			u_char ttl;
1902 			error = soopt_to_kbuf(sopt, &ttl, 1, 1);
1903 			if (error)
1904 				break;
1905 			imo->imo_multicast_ttl = ttl;
1906 		} else {
1907 			u_int ttl;
1908 			error = soopt_to_kbuf(sopt, &ttl, sizeof ttl, sizeof ttl);
1909 			if (error)
1910 				break;
1911 			if (ttl > 255)
1912 				error = EINVAL;
1913 			else
1914 				imo->imo_multicast_ttl = ttl;
1915 		}
1916 		break;
1917 
1918 	case IP_MULTICAST_LOOP:
1919 		/*
1920 		 * Set the loopback flag for outgoing multicast packets.
1921 		 * Must be zero or one.  The original multicast API required a
1922 		 * char argument, which is inconsistent with the rest
1923 		 * of the socket API.  We allow either a char or an int.
1924 		 */
1925 		if (sopt->sopt_valsize == 1) {
1926 			u_char loop;
1927 
1928 			error = soopt_to_kbuf(sopt, &loop, 1, 1);
1929 			if (error)
1930 				break;
1931 			imo->imo_multicast_loop = !!loop;
1932 		} else {
1933 			u_int loop;
1934 
1935 			error = soopt_to_kbuf(sopt, &loop, sizeof loop,
1936 					    sizeof loop);
1937 			if (error)
1938 				break;
1939 			imo->imo_multicast_loop = !!loop;
1940 		}
1941 		break;
1942 
1943 	case IP_ADD_MEMBERSHIP:
1944 		/*
1945 		 * Add a multicast group membership.
1946 		 * Group must be a valid IP multicast address.
1947 		 */
1948 		error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
1949 		if (error)
1950 			break;
1951 
1952 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1953 			error = EINVAL;
1954 			break;
1955 		}
1956 		crit_enter();
1957 		/*
1958 		 * If no interface address was provided, use the interface of
1959 		 * the route to the given multicast address.
1960 		 */
1961 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1962 			struct sockaddr_in dst;
1963 			struct rtentry *rt;
1964 
1965 			bzero(&dst, sizeof(struct sockaddr_in));
1966 			dst.sin_len = sizeof(struct sockaddr_in);
1967 			dst.sin_family = AF_INET;
1968 			dst.sin_addr = mreq.imr_multiaddr;
1969 			rt = rtlookup((struct sockaddr *)&dst);
1970 			if (rt == NULL) {
1971 				error = EADDRNOTAVAIL;
1972 				crit_exit();
1973 				break;
1974 			}
1975 			--rt->rt_refcnt;
1976 			ifp = rt->rt_ifp;
1977 		} else {
1978 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1979 		}
1980 
1981 		/*
1982 		 * See if we found an interface, and confirm that it
1983 		 * supports multicast.
1984 		 */
1985 		if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
1986 			error = EADDRNOTAVAIL;
1987 			crit_exit();
1988 			break;
1989 		}
1990 		/*
1991 		 * See if the membership already exists or if all the
1992 		 * membership slots are full.
1993 		 */
1994 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1995 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1996 			    imo->imo_membership[i]->inm_addr.s_addr
1997 						== mreq.imr_multiaddr.s_addr)
1998 				break;
1999 		}
2000 		if (i < imo->imo_num_memberships) {
2001 			error = EADDRINUSE;
2002 			crit_exit();
2003 			break;
2004 		}
2005 		if (i == IP_MAX_MEMBERSHIPS) {
2006 			error = ETOOMANYREFS;
2007 			crit_exit();
2008 			break;
2009 		}
2010 		/*
2011 		 * Everything looks good; add a new record to the multicast
2012 		 * address list for the given interface.
2013 		 */
2014 		if ((imo->imo_membership[i] =
2015 		     in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
2016 			error = ENOBUFS;
2017 			crit_exit();
2018 			break;
2019 		}
2020 		++imo->imo_num_memberships;
2021 		crit_exit();
2022 		break;
2023 
2024 	case IP_DROP_MEMBERSHIP:
2025 		/*
2026 		 * Drop a multicast group membership.
2027 		 * Group must be a valid IP multicast address.
2028 		 */
2029 		error = soopt_to_kbuf(sopt, &mreq, sizeof mreq, sizeof mreq);
2030 		if (error)
2031 			break;
2032 
2033 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
2034 			error = EINVAL;
2035 			break;
2036 		}
2037 
2038 		crit_enter();
2039 		/*
2040 		 * If an interface address was specified, get a pointer
2041 		 * to its ifnet structure.
2042 		 */
2043 		if (mreq.imr_interface.s_addr == INADDR_ANY)
2044 			ifp = NULL;
2045 		else {
2046 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
2047 			if (ifp == NULL) {
2048 				error = EADDRNOTAVAIL;
2049 				crit_exit();
2050 				break;
2051 			}
2052 		}
2053 		/*
2054 		 * Find the membership in the membership array.
2055 		 */
2056 		for (i = 0; i < imo->imo_num_memberships; ++i) {
2057 			if ((ifp == NULL ||
2058 			     imo->imo_membership[i]->inm_ifp == ifp) &&
2059 			    imo->imo_membership[i]->inm_addr.s_addr ==
2060 			    mreq.imr_multiaddr.s_addr)
2061 				break;
2062 		}
2063 		if (i == imo->imo_num_memberships) {
2064 			error = EADDRNOTAVAIL;
2065 			crit_exit();
2066 			break;
2067 		}
2068 		/*
2069 		 * Give up the multicast address record to which the
2070 		 * membership points.
2071 		 */
2072 		in_delmulti(imo->imo_membership[i]);
2073 		/*
2074 		 * Remove the gap in the membership array.
2075 		 */
2076 		for (++i; i < imo->imo_num_memberships; ++i)
2077 			imo->imo_membership[i-1] = imo->imo_membership[i];
2078 		--imo->imo_num_memberships;
2079 		crit_exit();
2080 		break;
2081 
2082 	default:
2083 		error = EOPNOTSUPP;
2084 		break;
2085 	}
2086 
2087 	/*
2088 	 * If all options have default values, no need to keep the mbuf.
2089 	 */
2090 	if (imo->imo_multicast_ifp == NULL &&
2091 	    imo->imo_multicast_vif == -1 &&
2092 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
2093 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
2094 	    imo->imo_num_memberships == 0) {
2095 		kfree(*imop, M_IPMOPTS);
2096 		*imop = NULL;
2097 	}
2098 
2099 	return (error);
2100 }
2101 
2102 /*
2103  * Return the IP multicast options in response to user getsockopt().
2104  */
2105 static int
2106 ip_getmoptions(struct sockopt *sopt, struct ip_moptions *imo)
2107 {
2108 	struct in_addr addr;
2109 	struct in_ifaddr *ia;
2110 	int error, optval;
2111 	u_char coptval;
2112 
2113 	error = 0;
2114 	switch (sopt->sopt_name) {
2115 	case IP_MULTICAST_VIF:
2116 		if (imo != NULL)
2117 			optval = imo->imo_multicast_vif;
2118 		else
2119 			optval = -1;
2120 		soopt_from_kbuf(sopt, &optval, sizeof optval);
2121 		break;
2122 
2123 	case IP_MULTICAST_IF:
2124 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
2125 			addr.s_addr = INADDR_ANY;
2126 		else if (imo->imo_multicast_addr.s_addr) {
2127 			/* return the value user has set */
2128 			addr = imo->imo_multicast_addr;
2129 		} else {
2130 			ia = IFP_TO_IA(imo->imo_multicast_ifp);
2131 			addr.s_addr = (ia == NULL) ? INADDR_ANY
2132 				: IA_SIN(ia)->sin_addr.s_addr;
2133 		}
2134 		soopt_from_kbuf(sopt, &addr, sizeof addr);
2135 		break;
2136 
2137 	case IP_MULTICAST_TTL:
2138 		if (imo == NULL)
2139 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
2140 		else
2141 			optval = coptval = imo->imo_multicast_ttl;
2142 		if (sopt->sopt_valsize == 1)
2143 			soopt_from_kbuf(sopt, &coptval, 1);
2144 		else
2145 			soopt_from_kbuf(sopt, &optval, sizeof optval);
2146 		break;
2147 
2148 	case IP_MULTICAST_LOOP:
2149 		if (imo == NULL)
2150 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
2151 		else
2152 			optval = coptval = imo->imo_multicast_loop;
2153 		if (sopt->sopt_valsize == 1)
2154 			soopt_from_kbuf(sopt, &coptval, 1);
2155 		else
2156 			soopt_from_kbuf(sopt, &optval, sizeof optval);
2157 		break;
2158 
2159 	default:
2160 		error = ENOPROTOOPT;
2161 		break;
2162 	}
2163 	return (error);
2164 }
2165 
2166 /*
2167  * Discard the IP multicast options.
2168  */
2169 void
2170 ip_freemoptions(struct ip_moptions *imo)
2171 {
2172 	int i;
2173 
2174 	if (imo != NULL) {
2175 		for (i = 0; i < imo->imo_num_memberships; ++i)
2176 			in_delmulti(imo->imo_membership[i]);
2177 		kfree(imo, M_IPMOPTS);
2178 	}
2179 }
2180 
2181 /*
2182  * Routine called from ip_output() to loop back a copy of an IP multicast
2183  * packet to the input queue of a specified interface.  Note that this
2184  * calls the output routine of the loopback "driver", but with an interface
2185  * pointer that might NOT be a loopback interface -- evil, but easier than
2186  * replicating that code here.
2187  */
2188 static void
2189 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
2190 	     int hlen)
2191 {
2192 	struct ip *ip;
2193 	struct mbuf *copym;
2194 
2195 	copym = m_copypacket(m, MB_DONTWAIT);
2196 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2197 		copym = m_pullup(copym, hlen);
2198 	if (copym != NULL) {
2199 		/*
2200 		 * if the checksum hasn't been computed, mark it as valid
2201 		 */
2202 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2203 			in_delayed_cksum(copym);
2204 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2205 			copym->m_pkthdr.csum_flags |=
2206 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2207 			copym->m_pkthdr.csum_data = 0xffff;
2208 		}
2209 		/*
2210 		 * We don't bother to fragment if the IP length is greater
2211 		 * than the interface's MTU.  Can this possibly matter?
2212 		 */
2213 		ip = mtod(copym, struct ip *);
2214 		ip->ip_len = htons(ip->ip_len);
2215 		ip->ip_off = htons(ip->ip_off);
2216 		ip->ip_sum = 0;
2217 		if (ip->ip_vhl == IP_VHL_BORING) {
2218 			ip->ip_sum = in_cksum_hdr(ip);
2219 		} else {
2220 			ip->ip_sum = in_cksum(copym, hlen);
2221 		}
2222 		/*
2223 		 * NB:
2224 		 * It's not clear whether there are any lingering
2225 		 * reentrancy problems in other areas which might
2226 		 * be exposed by using ip_input directly (in
2227 		 * particular, everything which modifies the packet
2228 		 * in-place).  Yet another option is using the
2229 		 * protosw directly to deliver the looped back
2230 		 * packet.  For the moment, we'll err on the side
2231 		 * of safety by using if_simloop().
2232 		 */
2233 #if 1 /* XXX */
2234 		if (dst->sin_family != AF_INET) {
2235 			kprintf("ip_mloopback: bad address family %d\n",
2236 						dst->sin_family);
2237 			dst->sin_family = AF_INET;
2238 		}
2239 #endif
2240 
2241 #ifdef notdef
2242 		copym->m_pkthdr.rcvif = ifp;
2243 		ip_input(copym);
2244 #else
2245 		if_simloop(ifp, copym, dst->sin_family, 0);
2246 #endif
2247 	}
2248 }
2249