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