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