ip_output.c revision 162084
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30 * $FreeBSD: head/sys/netinet/ip_output.c 162084 2006-09-06 21:51:59Z andre $
31 */
32
33#include "opt_ipfw.h"
34#include "opt_ipsec.h"
35#include "opt_mac.h"
36#include "opt_mbuf_stress_test.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/mac.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/protosw.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47#include <sys/sysctl.h>
48
49#include <net/if.h>
50#include <net/netisr.h>
51#include <net/pfil.h>
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/ip.h>
57#include <netinet/in_pcb.h>
58#include <netinet/in_var.h>
59#include <netinet/ip_var.h>
60#include <netinet/ip_options.h>
61
62#if defined(IPSEC) || defined(FAST_IPSEC)
63#include <netinet/ip_ipsec.h>
64#ifdef IPSEC
65#include <netinet6/ipsec.h>
66#endif
67#ifdef FAST_IPSEC
68#include <netipsec/ipsec.h>
69#endif
70#endif /*IPSEC*/
71
72#include <machine/in_cksum.h>
73
74static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
75
76#define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
77				x, (ntohl(a.s_addr)>>24)&0xFF,\
78				  (ntohl(a.s_addr)>>16)&0xFF,\
79				  (ntohl(a.s_addr)>>8)&0xFF,\
80				  (ntohl(a.s_addr))&0xFF, y);
81
82u_short ip_id;
83
84#ifdef MBUF_STRESS_TEST
85int mbuf_frag_size = 0;
86SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
87	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
88#endif
89
90static struct ifnet *ip_multicast_if(struct in_addr *, int *);
91static void	ip_mloopback
92	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
93static int	ip_getmoptions(struct inpcb *, struct sockopt *);
94static int	ip_setmoptions(struct inpcb *, struct sockopt *);
95
96
97extern	struct protosw inetsw[];
98
99/*
100 * IP output.  The packet in mbuf chain m contains a skeletal IP
101 * header (with len, off, ttl, proto, tos, src, dst).
102 * The mbuf chain containing the packet will be freed.
103 * The mbuf opt, if present, will not be freed.
104 * In the IP forwarding case, the packet will arrive with options already
105 * inserted, so must have a NULL opt pointer.
106 */
107int
108ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
109	int flags, struct ip_moptions *imo, struct inpcb *inp)
110{
111	struct ip *ip;
112	struct ifnet *ifp = NULL;	/* keep compiler happy */
113	struct mbuf *m0;
114	int hlen = sizeof (struct ip);
115	int len, error = 0;
116	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
117	struct in_ifaddr *ia = NULL;
118	int isbroadcast, sw_csum;
119	struct route iproute;
120	struct in_addr odst;
121#ifdef IPFIREWALL_FORWARD
122	struct m_tag *fwd_tag = NULL;
123#endif
124	M_ASSERTPKTHDR(m);
125
126	if (ro == NULL) {
127		ro = &iproute;
128		bzero(ro, sizeof (*ro));
129	}
130
131	if (inp != NULL)
132		INP_LOCK_ASSERT(inp);
133
134	if (opt) {
135		len = 0;
136		m = ip_insertoptions(m, opt, &len);
137		if (len != 0)
138			hlen = len;
139	}
140	ip = mtod(m, struct ip *);
141
142	/*
143	 * Fill in IP header.  If we are not allowing fragmentation,
144	 * then the ip_id field is meaningless, but we don't set it
145	 * to zero.  Doing so causes various problems when devices along
146	 * the path (routers, load balancers, firewalls, etc.) illegally
147	 * disable DF on our packet.  Note that a 16-bit counter
148	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
149	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
150	 * for Counting NATted Hosts", Proc. IMW'02, available at
151	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
152	 */
153	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
154		ip->ip_v = IPVERSION;
155		ip->ip_hl = hlen >> 2;
156		ip->ip_id = ip_newid();
157		ipstat.ips_localout++;
158	} else {
159		hlen = ip->ip_hl << 2;
160	}
161
162	dst = (struct sockaddr_in *)&ro->ro_dst;
163again:
164	/*
165	 * If there is a cached route,
166	 * check that it is to the same destination
167	 * and is still up.  If not, free it and try again.
168	 * The address family should also be checked in case of sharing the
169	 * cache with IPv6.
170	 */
171	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
172			  dst->sin_family != AF_INET ||
173			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
174		RTFREE(ro->ro_rt);
175		ro->ro_rt = (struct rtentry *)0;
176	}
177#ifdef IPFIREWALL_FORWARD
178	if (ro->ro_rt == NULL && fwd_tag == NULL) {
179#else
180	if (ro->ro_rt == NULL) {
181#endif
182		bzero(dst, sizeof(*dst));
183		dst->sin_family = AF_INET;
184		dst->sin_len = sizeof(*dst);
185		dst->sin_addr = ip->ip_dst;
186	}
187	/*
188	 * If routing to interface only,
189	 * short circuit routing lookup.
190	 */
191	if (flags & IP_ROUTETOIF) {
192		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
193		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
194			ipstat.ips_noroute++;
195			error = ENETUNREACH;
196			goto bad;
197		}
198		ifp = ia->ia_ifp;
199		ip->ip_ttl = 1;
200		isbroadcast = in_broadcast(dst->sin_addr, ifp);
201	} else if (flags & IP_SENDONES) {
202		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL) {
203			ipstat.ips_noroute++;
204			error = ENETUNREACH;
205			goto bad;
206		}
207		ifp = ia->ia_ifp;
208		ip->ip_dst.s_addr = INADDR_BROADCAST;
209		dst->sin_addr = ip->ip_dst;
210		ip->ip_ttl = 1;
211		isbroadcast = 1;
212	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
213	    imo != NULL && imo->imo_multicast_ifp != NULL) {
214		/*
215		 * Bypass the normal routing lookup for multicast
216		 * packets if the interface is specified.
217		 */
218		ifp = imo->imo_multicast_ifp;
219		IFP_TO_IA(ifp, ia);
220		isbroadcast = 0;	/* fool gcc */
221	} else {
222		/*
223		 * We want to do any cloning requested by the link layer,
224		 * as this is probably required in all cases for correct
225		 * operation (as it is for ARP).
226		 */
227		if (ro->ro_rt == NULL)
228			rtalloc_ign(ro, 0);
229		if (ro->ro_rt == NULL) {
230			ipstat.ips_noroute++;
231			error = EHOSTUNREACH;
232			goto bad;
233		}
234		ia = ifatoia(ro->ro_rt->rt_ifa);
235		ifp = ro->ro_rt->rt_ifp;
236		ro->ro_rt->rt_rmx.rmx_pksent++;
237		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
238			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
239		if (ro->ro_rt->rt_flags & RTF_HOST)
240			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
241		else
242			isbroadcast = in_broadcast(dst->sin_addr, ifp);
243	}
244	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
245		struct in_multi *inm;
246
247		m->m_flags |= M_MCAST;
248		/*
249		 * IP destination address is multicast.  Make sure "dst"
250		 * still points to the address in "ro".  (It may have been
251		 * changed to point to a gateway address, above.)
252		 */
253		dst = (struct sockaddr_in *)&ro->ro_dst;
254		/*
255		 * See if the caller provided any multicast options
256		 */
257		if (imo != NULL) {
258			ip->ip_ttl = imo->imo_multicast_ttl;
259			if (imo->imo_multicast_vif != -1)
260				ip->ip_src.s_addr =
261				    ip_mcast_src ?
262				    ip_mcast_src(imo->imo_multicast_vif) :
263				    INADDR_ANY;
264		} else
265			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
266		/*
267		 * Confirm that the outgoing interface supports multicast.
268		 */
269		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
270			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
271				ipstat.ips_noroute++;
272				error = ENETUNREACH;
273				goto bad;
274			}
275		}
276		/*
277		 * If source address not specified yet, use address
278		 * of outgoing interface.
279		 */
280		if (ip->ip_src.s_addr == INADDR_ANY) {
281			/* Interface may have no addresses. */
282			if (ia != NULL)
283				ip->ip_src = IA_SIN(ia)->sin_addr;
284		}
285
286		IN_MULTI_LOCK();
287		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
288		if (inm != NULL &&
289		   (imo == NULL || imo->imo_multicast_loop)) {
290			IN_MULTI_UNLOCK();
291			/*
292			 * If we belong to the destination multicast group
293			 * on the outgoing interface, and the caller did not
294			 * forbid loopback, loop back a copy.
295			 */
296			ip_mloopback(ifp, m, dst, hlen);
297		}
298		else {
299			IN_MULTI_UNLOCK();
300			/*
301			 * If we are acting as a multicast router, perform
302			 * multicast forwarding as if the packet had just
303			 * arrived on the interface to which we are about
304			 * to send.  The multicast forwarding function
305			 * recursively calls this function, using the
306			 * IP_FORWARDING flag to prevent infinite recursion.
307			 *
308			 * Multicasts that are looped back by ip_mloopback(),
309			 * above, will be forwarded by the ip_input() routine,
310			 * if necessary.
311			 */
312			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
313				/*
314				 * If rsvp daemon is not running, do not
315				 * set ip_moptions. This ensures that the packet
316				 * is multicast and not just sent down one link
317				 * as prescribed by rsvpd.
318				 */
319				if (!rsvp_on)
320					imo = NULL;
321				if (ip_mforward &&
322				    ip_mforward(ip, ifp, m, imo) != 0) {
323					m_freem(m);
324					goto done;
325				}
326			}
327		}
328
329		/*
330		 * Multicasts with a time-to-live of zero may be looped-
331		 * back, above, but must not be transmitted on a network.
332		 * Also, multicasts addressed to the loopback interface
333		 * are not sent -- the above call to ip_mloopback() will
334		 * loop back a copy if this host actually belongs to the
335		 * destination group on the loopback interface.
336		 */
337		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
338			m_freem(m);
339			goto done;
340		}
341
342		goto sendit;
343	}
344#ifndef notdef
345	/*
346	 * If the source address is not specified yet, use the address
347	 * of the outoing interface.
348	 */
349	if (ip->ip_src.s_addr == INADDR_ANY) {
350		/* Interface may have no addresses. */
351		if (ia != NULL) {
352			ip->ip_src = IA_SIN(ia)->sin_addr;
353		}
354	}
355#endif /* notdef */
356	/*
357	 * Verify that we have any chance at all of being able to queue the
358	 * packet or packet fragments, unless ALTQ is enabled on the given
359	 * interface in which case packetdrop should be done by queueing.
360	 */
361#ifdef ALTQ
362	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
363	    ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
364	    ifp->if_snd.ifq_maxlen))
365#else
366	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
367	    ifp->if_snd.ifq_maxlen)
368#endif /* ALTQ */
369	{
370		error = ENOBUFS;
371		ipstat.ips_odropped++;
372		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
373		goto bad;
374	}
375
376	/*
377	 * Look for broadcast address and
378	 * verify user is allowed to send
379	 * such a packet.
380	 */
381	if (isbroadcast) {
382		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
383			error = EADDRNOTAVAIL;
384			goto bad;
385		}
386		if ((flags & IP_ALLOWBROADCAST) == 0) {
387			error = EACCES;
388			goto bad;
389		}
390		/* don't allow broadcast messages to be fragmented */
391		if (ip->ip_len > ifp->if_mtu) {
392			error = EMSGSIZE;
393			goto bad;
394		}
395		m->m_flags |= M_BCAST;
396	} else {
397		m->m_flags &= ~M_BCAST;
398	}
399
400sendit:
401#if defined(IPSEC) || defined(FAST_IPSEC)
402	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
403	case 1:
404		goto bad;
405	case -1:
406		goto done;
407	case 0:
408	default:
409		break;	/* Continue with packet processing. */
410	}
411	/* Update variables that are affected by ipsec4_output(). */
412	ip = mtod(m, struct ip *);
413	hlen = ip->ip_hl << 2;
414#endif /* IPSEC */
415
416	/* Jump over all PFIL processing if hooks are not active. */
417	if (!PFIL_HOOKED(&inet_pfil_hook))
418		goto passout;
419
420	/* Run through list of hooks for output packets. */
421	odst.s_addr = ip->ip_dst.s_addr;
422	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
423	if (error != 0 || m == NULL)
424		goto done;
425
426	ip = mtod(m, struct ip *);
427
428	/* See if destination IP address was changed by packet filter. */
429	if (odst.s_addr != ip->ip_dst.s_addr) {
430		m->m_flags |= M_SKIP_FIREWALL;
431		/* If destination is now ourself drop to ip_input(). */
432		if (in_localip(ip->ip_dst)) {
433			m->m_flags |= M_FASTFWD_OURS;
434			if (m->m_pkthdr.rcvif == NULL)
435				m->m_pkthdr.rcvif = loif;
436			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
437				m->m_pkthdr.csum_flags |=
438				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
439				m->m_pkthdr.csum_data = 0xffff;
440			}
441			m->m_pkthdr.csum_flags |=
442			    CSUM_IP_CHECKED | CSUM_IP_VALID;
443
444			error = netisr_queue(NETISR_IP, m);
445			goto done;
446		} else
447			goto again;	/* Redo the routing table lookup. */
448	}
449
450#ifdef IPFIREWALL_FORWARD
451	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
452	if (m->m_flags & M_FASTFWD_OURS) {
453		if (m->m_pkthdr.rcvif == NULL)
454			m->m_pkthdr.rcvif = loif;
455		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
456			m->m_pkthdr.csum_flags |=
457			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
458			m->m_pkthdr.csum_data = 0xffff;
459		}
460		m->m_pkthdr.csum_flags |=
461			    CSUM_IP_CHECKED | CSUM_IP_VALID;
462
463		error = netisr_queue(NETISR_IP, m);
464		goto done;
465	}
466	/* Or forward to some other address? */
467	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
468	if (fwd_tag) {
469		dst = (struct sockaddr_in *)&ro->ro_dst;
470		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
471		m->m_flags |= M_SKIP_FIREWALL;
472		m_tag_delete(m, fwd_tag);
473		goto again;
474	}
475#endif /* IPFIREWALL_FORWARD */
476
477passout:
478	/* 127/8 must not appear on wire - RFC1122. */
479	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
480	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
481		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
482			ipstat.ips_badaddr++;
483			error = EADDRNOTAVAIL;
484			goto bad;
485		}
486	}
487
488	m->m_pkthdr.csum_flags |= CSUM_IP;
489	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
490	if (sw_csum & CSUM_DELAY_DATA) {
491		in_delayed_cksum(m);
492		sw_csum &= ~CSUM_DELAY_DATA;
493	}
494	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
495
496	/*
497	 * If small enough for interface, or the interface will take
498	 * care of the fragmentation for us, we can just send directly.
499	 */
500	if (ip->ip_len <= ifp->if_mtu ||
501	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
502	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
503		ip->ip_len = htons(ip->ip_len);
504		ip->ip_off = htons(ip->ip_off);
505		ip->ip_sum = 0;
506		if (sw_csum & CSUM_DELAY_IP)
507			ip->ip_sum = in_cksum(m, hlen);
508
509		/*
510		 * Record statistics for this interface address.
511		 * With CSUM_TSO the byte/packet count will be slightly
512		 * incorrect because we count the IP+TCP headers only
513		 * once instead of for every generated packet.
514		 */
515		if (!(flags & IP_FORWARDING) && ia) {
516			if (m->m_pkthdr.csum_flags & CSUM_TSO)
517				ia->ia_ifa.if_opackets +=
518				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
519			else
520				ia->ia_ifa.if_opackets++;
521			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
522		}
523#ifdef IPSEC
524		/* clean ipsec history once it goes out of the node */
525		ipsec_delaux(m);
526#endif
527#ifdef MBUF_STRESS_TEST
528		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
529			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
530#endif
531		/*
532		 * Reset layer specific mbuf flags
533		 * to avoid confusing lower layers.
534		 */
535		m->m_flags &= ~(M_PROTOFLAGS);
536
537		error = (*ifp->if_output)(ifp, m,
538				(struct sockaddr *)dst, ro->ro_rt);
539		goto done;
540	}
541
542	/* Balk when DF bit is set or the interface didn't support TSO. */
543	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
544		error = EMSGSIZE;
545		/*
546		 * This case can happen if the user changed the MTU
547		 * of an interface after enabling IP on it.  Because
548		 * most netifs don't keep track of routes pointing to
549		 * them, there is no way for one to update all its
550		 * routes when the MTU is changed.
551		 */
552		if (ro != NULL &&
553		    (ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
554		    (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
555			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
556		}
557		ipstat.ips_cantfrag++;
558		goto bad;
559	}
560
561	/*
562	 * Too large for interface; fragment if possible. If successful,
563	 * on return, m will point to a list of packets to be sent.
564	 */
565	error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum);
566	if (error)
567		goto bad;
568	for (; m; m = m0) {
569		m0 = m->m_nextpkt;
570		m->m_nextpkt = 0;
571#ifdef IPSEC
572		/* clean ipsec history once it goes out of the node */
573		ipsec_delaux(m);
574#endif
575		if (error == 0) {
576			/* Record statistics for this interface address. */
577			if (ia != NULL) {
578				ia->ia_ifa.if_opackets++;
579				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
580			}
581			/*
582			 * Reset layer specific mbuf flags
583			 * to avoid confusing upper layers.
584			 */
585			m->m_flags &= ~(M_PROTOFLAGS);
586
587			error = (*ifp->if_output)(ifp, m,
588			    (struct sockaddr *)dst, ro->ro_rt);
589		} else
590			m_freem(m);
591	}
592
593	if (error == 0)
594		ipstat.ips_fragmented++;
595
596done:
597	if (ro == &iproute && ro->ro_rt) {
598		RTFREE(ro->ro_rt);
599	}
600	return (error);
601bad:
602	m_freem(m);
603	goto done;
604}
605
606/*
607 * Create a chain of fragments which fit the given mtu. m_frag points to the
608 * mbuf to be fragmented; on return it points to the chain with the fragments.
609 * Return 0 if no error. If error, m_frag may contain a partially built
610 * chain of fragments that should be freed by the caller.
611 *
612 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
613 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
614 */
615int
616ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
617	    u_long if_hwassist_flags, int sw_csum)
618{
619	int error = 0;
620	int hlen = ip->ip_hl << 2;
621	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
622	int off;
623	struct mbuf *m0 = *m_frag;	/* the original packet		*/
624	int firstlen;
625	struct mbuf **mnext;
626	int nfrags;
627
628	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
629		ipstat.ips_cantfrag++;
630		return EMSGSIZE;
631	}
632
633	/*
634	 * Must be able to put at least 8 bytes per fragment.
635	 */
636	if (len < 8)
637		return EMSGSIZE;
638
639	/*
640	 * If the interface will not calculate checksums on
641	 * fragmented packets, then do it here.
642	 */
643	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
644	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
645		in_delayed_cksum(m0);
646		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
647	}
648
649	if (len > PAGE_SIZE) {
650		/*
651		 * Fragment large datagrams such that each segment
652		 * contains a multiple of PAGE_SIZE amount of data,
653		 * plus headers. This enables a receiver to perform
654		 * page-flipping zero-copy optimizations.
655		 *
656		 * XXX When does this help given that sender and receiver
657		 * could have different page sizes, and also mtu could
658		 * be less than the receiver's page size ?
659		 */
660		int newlen;
661		struct mbuf *m;
662
663		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
664			off += m->m_len;
665
666		/*
667		 * firstlen (off - hlen) must be aligned on an
668		 * 8-byte boundary
669		 */
670		if (off < hlen)
671			goto smart_frag_failure;
672		off = ((off - hlen) & ~7) + hlen;
673		newlen = (~PAGE_MASK) & mtu;
674		if ((newlen + sizeof (struct ip)) > mtu) {
675			/* we failed, go back the default */
676smart_frag_failure:
677			newlen = len;
678			off = hlen + len;
679		}
680		len = newlen;
681
682	} else {
683		off = hlen + len;
684	}
685
686	firstlen = off - hlen;
687	mnext = &m0->m_nextpkt;		/* pointer to next packet */
688
689	/*
690	 * Loop through length of segment after first fragment,
691	 * make new header and copy data of each part and link onto chain.
692	 * Here, m0 is the original packet, m is the fragment being created.
693	 * The fragments are linked off the m_nextpkt of the original
694	 * packet, which after processing serves as the first fragment.
695	 */
696	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
697		struct ip *mhip;	/* ip header on the fragment */
698		struct mbuf *m;
699		int mhlen = sizeof (struct ip);
700
701		MGETHDR(m, M_DONTWAIT, MT_DATA);
702		if (m == NULL) {
703			error = ENOBUFS;
704			ipstat.ips_odropped++;
705			goto done;
706		}
707		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
708		/*
709		 * In the first mbuf, leave room for the link header, then
710		 * copy the original IP header including options. The payload
711		 * goes into an additional mbuf chain returned by m_copy().
712		 */
713		m->m_data += max_linkhdr;
714		mhip = mtod(m, struct ip *);
715		*mhip = *ip;
716		if (hlen > sizeof (struct ip)) {
717			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
718			mhip->ip_v = IPVERSION;
719			mhip->ip_hl = mhlen >> 2;
720		}
721		m->m_len = mhlen;
722		/* XXX do we need to add ip->ip_off below ? */
723		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
724		if (off + len >= ip->ip_len) {	/* last fragment */
725			len = ip->ip_len - off;
726			m->m_flags |= M_LASTFRAG;
727		} else
728			mhip->ip_off |= IP_MF;
729		mhip->ip_len = htons((u_short)(len + mhlen));
730		m->m_next = m_copy(m0, off, len);
731		if (m->m_next == NULL) {	/* copy failed */
732			m_free(m);
733			error = ENOBUFS;	/* ??? */
734			ipstat.ips_odropped++;
735			goto done;
736		}
737		m->m_pkthdr.len = mhlen + len;
738		m->m_pkthdr.rcvif = NULL;
739#ifdef MAC
740		mac_create_fragment(m0, m);
741#endif
742		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
743		mhip->ip_off = htons(mhip->ip_off);
744		mhip->ip_sum = 0;
745		if (sw_csum & CSUM_DELAY_IP)
746			mhip->ip_sum = in_cksum(m, mhlen);
747		*mnext = m;
748		mnext = &m->m_nextpkt;
749	}
750	ipstat.ips_ofragments += nfrags;
751
752	/* set first marker for fragment chain */
753	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
754	m0->m_pkthdr.csum_data = nfrags;
755
756	/*
757	 * Update first fragment by trimming what's been copied out
758	 * and updating header.
759	 */
760	m_adj(m0, hlen + firstlen - ip->ip_len);
761	m0->m_pkthdr.len = hlen + firstlen;
762	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
763	ip->ip_off |= IP_MF;
764	ip->ip_off = htons(ip->ip_off);
765	ip->ip_sum = 0;
766	if (sw_csum & CSUM_DELAY_IP)
767		ip->ip_sum = in_cksum(m0, hlen);
768
769done:
770	*m_frag = m0;
771	return error;
772}
773
774void
775in_delayed_cksum(struct mbuf *m)
776{
777	struct ip *ip;
778	u_short csum, offset;
779
780	ip = mtod(m, struct ip *);
781	offset = ip->ip_hl << 2 ;
782	csum = in_cksum_skip(m, ip->ip_len, offset);
783	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
784		csum = 0xffff;
785	offset += m->m_pkthdr.csum_data;	/* checksum offset */
786
787	if (offset + sizeof(u_short) > m->m_len) {
788		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
789		    m->m_len, offset, ip->ip_p);
790		/*
791		 * XXX
792		 * this shouldn't happen, but if it does, the
793		 * correct behavior may be to insert the checksum
794		 * in the appropriate next mbuf in the chain.
795		 */
796		return;
797	}
798	*(u_short *)(m->m_data + offset) = csum;
799}
800
801/*
802 * IP socket option processing.
803 */
804int
805ip_ctloutput(so, sopt)
806	struct socket *so;
807	struct sockopt *sopt;
808{
809	struct	inpcb *inp = sotoinpcb(so);
810	int	error, optval;
811
812	error = optval = 0;
813	if (sopt->sopt_level != IPPROTO_IP) {
814		return (EINVAL);
815	}
816
817	switch (sopt->sopt_dir) {
818	case SOPT_SET:
819		switch (sopt->sopt_name) {
820		case IP_OPTIONS:
821#ifdef notyet
822		case IP_RETOPTS:
823#endif
824		{
825			struct mbuf *m;
826			if (sopt->sopt_valsize > MLEN) {
827				error = EMSGSIZE;
828				break;
829			}
830			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
831			if (m == NULL) {
832				error = ENOBUFS;
833				break;
834			}
835			m->m_len = sopt->sopt_valsize;
836			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
837					    m->m_len);
838			if (error) {
839				m_free(m);
840				break;
841			}
842			INP_LOCK(inp);
843			error = ip_pcbopts(inp, sopt->sopt_name, m);
844			INP_UNLOCK(inp);
845			return (error);
846		}
847
848		case IP_TOS:
849		case IP_TTL:
850		case IP_MINTTL:
851		case IP_RECVOPTS:
852		case IP_RECVRETOPTS:
853		case IP_RECVDSTADDR:
854		case IP_RECVTTL:
855		case IP_RECVIF:
856		case IP_FAITH:
857		case IP_ONESBCAST:
858		case IP_DONTFRAG:
859			error = sooptcopyin(sopt, &optval, sizeof optval,
860					    sizeof optval);
861			if (error)
862				break;
863
864			switch (sopt->sopt_name) {
865			case IP_TOS:
866				inp->inp_ip_tos = optval;
867				break;
868
869			case IP_TTL:
870				inp->inp_ip_ttl = optval;
871				break;
872
873			case IP_MINTTL:
874				if (optval > 0 && optval <= MAXTTL)
875					inp->inp_ip_minttl = optval;
876				else
877					error = EINVAL;
878				break;
879
880#define	OPTSET(bit) do {						\
881	INP_LOCK(inp);							\
882	if (optval)							\
883		inp->inp_flags |= bit;					\
884	else								\
885		inp->inp_flags &= ~bit;					\
886	INP_UNLOCK(inp);						\
887} while (0)
888
889			case IP_RECVOPTS:
890				OPTSET(INP_RECVOPTS);
891				break;
892
893			case IP_RECVRETOPTS:
894				OPTSET(INP_RECVRETOPTS);
895				break;
896
897			case IP_RECVDSTADDR:
898				OPTSET(INP_RECVDSTADDR);
899				break;
900
901			case IP_RECVTTL:
902				OPTSET(INP_RECVTTL);
903				break;
904
905			case IP_RECVIF:
906				OPTSET(INP_RECVIF);
907				break;
908
909			case IP_FAITH:
910				OPTSET(INP_FAITH);
911				break;
912
913			case IP_ONESBCAST:
914				OPTSET(INP_ONESBCAST);
915				break;
916			case IP_DONTFRAG:
917				OPTSET(INP_DONTFRAG);
918				break;
919			}
920			break;
921#undef OPTSET
922
923		case IP_MULTICAST_IF:
924		case IP_MULTICAST_VIF:
925		case IP_MULTICAST_TTL:
926		case IP_MULTICAST_LOOP:
927		case IP_ADD_MEMBERSHIP:
928		case IP_DROP_MEMBERSHIP:
929			error = ip_setmoptions(inp, sopt);
930			break;
931
932		case IP_PORTRANGE:
933			error = sooptcopyin(sopt, &optval, sizeof optval,
934					    sizeof optval);
935			if (error)
936				break;
937
938			INP_LOCK(inp);
939			switch (optval) {
940			case IP_PORTRANGE_DEFAULT:
941				inp->inp_flags &= ~(INP_LOWPORT);
942				inp->inp_flags &= ~(INP_HIGHPORT);
943				break;
944
945			case IP_PORTRANGE_HIGH:
946				inp->inp_flags &= ~(INP_LOWPORT);
947				inp->inp_flags |= INP_HIGHPORT;
948				break;
949
950			case IP_PORTRANGE_LOW:
951				inp->inp_flags &= ~(INP_HIGHPORT);
952				inp->inp_flags |= INP_LOWPORT;
953				break;
954
955			default:
956				error = EINVAL;
957				break;
958			}
959			INP_UNLOCK(inp);
960			break;
961
962#if defined(IPSEC) || defined(FAST_IPSEC)
963		case IP_IPSEC_POLICY:
964		{
965			caddr_t req;
966			size_t len = 0;
967			int priv;
968			struct mbuf *m;
969			int optname;
970
971			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
972				break;
973			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
974				break;
975			priv = (sopt->sopt_td != NULL &&
976				suser(sopt->sopt_td) != 0) ? 0 : 1;
977			req = mtod(m, caddr_t);
978			len = m->m_len;
979			optname = sopt->sopt_name;
980			error = ipsec4_set_policy(inp, optname, req, len, priv);
981			m_freem(m);
982			break;
983		}
984#endif /*IPSEC*/
985
986		default:
987			error = ENOPROTOOPT;
988			break;
989		}
990		break;
991
992	case SOPT_GET:
993		switch (sopt->sopt_name) {
994		case IP_OPTIONS:
995		case IP_RETOPTS:
996			if (inp->inp_options)
997				error = sooptcopyout(sopt,
998						     mtod(inp->inp_options,
999							  char *),
1000						     inp->inp_options->m_len);
1001			else
1002				sopt->sopt_valsize = 0;
1003			break;
1004
1005		case IP_TOS:
1006		case IP_TTL:
1007		case IP_MINTTL:
1008		case IP_RECVOPTS:
1009		case IP_RECVRETOPTS:
1010		case IP_RECVDSTADDR:
1011		case IP_RECVTTL:
1012		case IP_RECVIF:
1013		case IP_PORTRANGE:
1014		case IP_FAITH:
1015		case IP_ONESBCAST:
1016		case IP_DONTFRAG:
1017			switch (sopt->sopt_name) {
1018
1019			case IP_TOS:
1020				optval = inp->inp_ip_tos;
1021				break;
1022
1023			case IP_TTL:
1024				optval = inp->inp_ip_ttl;
1025				break;
1026
1027			case IP_MINTTL:
1028				optval = inp->inp_ip_minttl;
1029				break;
1030
1031#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1032
1033			case IP_RECVOPTS:
1034				optval = OPTBIT(INP_RECVOPTS);
1035				break;
1036
1037			case IP_RECVRETOPTS:
1038				optval = OPTBIT(INP_RECVRETOPTS);
1039				break;
1040
1041			case IP_RECVDSTADDR:
1042				optval = OPTBIT(INP_RECVDSTADDR);
1043				break;
1044
1045			case IP_RECVTTL:
1046				optval = OPTBIT(INP_RECVTTL);
1047				break;
1048
1049			case IP_RECVIF:
1050				optval = OPTBIT(INP_RECVIF);
1051				break;
1052
1053			case IP_PORTRANGE:
1054				if (inp->inp_flags & INP_HIGHPORT)
1055					optval = IP_PORTRANGE_HIGH;
1056				else if (inp->inp_flags & INP_LOWPORT)
1057					optval = IP_PORTRANGE_LOW;
1058				else
1059					optval = 0;
1060				break;
1061
1062			case IP_FAITH:
1063				optval = OPTBIT(INP_FAITH);
1064				break;
1065
1066			case IP_ONESBCAST:
1067				optval = OPTBIT(INP_ONESBCAST);
1068				break;
1069			case IP_DONTFRAG:
1070				optval = OPTBIT(INP_DONTFRAG);
1071				break;
1072			}
1073			error = sooptcopyout(sopt, &optval, sizeof optval);
1074			break;
1075
1076		case IP_MULTICAST_IF:
1077		case IP_MULTICAST_VIF:
1078		case IP_MULTICAST_TTL:
1079		case IP_MULTICAST_LOOP:
1080		case IP_ADD_MEMBERSHIP:
1081		case IP_DROP_MEMBERSHIP:
1082			error = ip_getmoptions(inp, sopt);
1083			break;
1084
1085#if defined(IPSEC) || defined(FAST_IPSEC)
1086		case IP_IPSEC_POLICY:
1087		{
1088			struct mbuf *m = NULL;
1089			caddr_t req = NULL;
1090			size_t len = 0;
1091
1092			if (m != 0) {
1093				req = mtod(m, caddr_t);
1094				len = m->m_len;
1095			}
1096			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1097			if (error == 0)
1098				error = soopt_mcopyout(sopt, m); /* XXX */
1099			if (error == 0)
1100				m_freem(m);
1101			break;
1102		}
1103#endif /*IPSEC*/
1104
1105		default:
1106			error = ENOPROTOOPT;
1107			break;
1108		}
1109		break;
1110	}
1111	return (error);
1112}
1113
1114/*
1115 * XXX
1116 * The whole multicast option thing needs to be re-thought.
1117 * Several of these options are equally applicable to non-multicast
1118 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1119 * standard option (IP_TTL).
1120 */
1121
1122/*
1123 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1124 */
1125static struct ifnet *
1126ip_multicast_if(a, ifindexp)
1127	struct in_addr *a;
1128	int *ifindexp;
1129{
1130	int ifindex;
1131	struct ifnet *ifp;
1132
1133	if (ifindexp)
1134		*ifindexp = 0;
1135	if (ntohl(a->s_addr) >> 24 == 0) {
1136		ifindex = ntohl(a->s_addr) & 0xffffff;
1137		if (ifindex < 0 || if_index < ifindex)
1138			return NULL;
1139		ifp = ifnet_byindex(ifindex);
1140		if (ifindexp)
1141			*ifindexp = ifindex;
1142	} else {
1143		INADDR_TO_IFP(*a, ifp);
1144	}
1145	return ifp;
1146}
1147
1148/*
1149 * Given an inpcb, return its multicast options structure pointer.  Accepts
1150 * an unlocked inpcb pointer, but will return it locked.  May sleep.
1151 */
1152static struct ip_moptions *
1153ip_findmoptions(struct inpcb *inp)
1154{
1155	struct ip_moptions *imo;
1156	struct in_multi **immp;
1157
1158	INP_LOCK(inp);
1159	if (inp->inp_moptions != NULL)
1160		return (inp->inp_moptions);
1161
1162	INP_UNLOCK(inp);
1163
1164	imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1165	immp = (struct in_multi **)malloc((sizeof(*immp) * IP_MIN_MEMBERSHIPS),
1166					  M_IPMOPTS, M_WAITOK);
1167
1168	imo->imo_multicast_ifp = NULL;
1169	imo->imo_multicast_addr.s_addr = INADDR_ANY;
1170	imo->imo_multicast_vif = -1;
1171	imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1172	imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1173	imo->imo_num_memberships = 0;
1174	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1175	imo->imo_membership = immp;
1176
1177	INP_LOCK(inp);
1178	if (inp->inp_moptions != NULL) {
1179		free(immp, M_IPMOPTS);
1180		free(imo, M_IPMOPTS);
1181		return (inp->inp_moptions);
1182	}
1183	inp->inp_moptions = imo;
1184	return (imo);
1185}
1186
1187/*
1188 * Set the IP multicast options in response to user setsockopt().
1189 */
1190static int
1191ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1192{
1193	int error = 0;
1194	int i;
1195	struct in_addr addr;
1196	struct ip_mreq mreq;
1197	struct ifnet *ifp;
1198	struct ip_moptions *imo;
1199	struct route ro;
1200	struct sockaddr_in *dst;
1201	int ifindex;
1202	int s;
1203
1204	switch (sopt->sopt_name) {
1205	/* store an index number for the vif you wanna use in the send */
1206	case IP_MULTICAST_VIF:
1207		if (legal_vif_num == 0) {
1208			error = EOPNOTSUPP;
1209			break;
1210		}
1211		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1212		if (error)
1213			break;
1214		if (!legal_vif_num(i) && (i != -1)) {
1215			error = EINVAL;
1216			break;
1217		}
1218		imo = ip_findmoptions(inp);
1219		imo->imo_multicast_vif = i;
1220		INP_UNLOCK(inp);
1221		break;
1222
1223	case IP_MULTICAST_IF:
1224		/*
1225		 * Select the interface for outgoing multicast packets.
1226		 */
1227		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1228		if (error)
1229			break;
1230		/*
1231		 * INADDR_ANY is used to remove a previous selection.
1232		 * When no interface is selected, a default one is
1233		 * chosen every time a multicast packet is sent.
1234		 */
1235		imo = ip_findmoptions(inp);
1236		if (addr.s_addr == INADDR_ANY) {
1237			imo->imo_multicast_ifp = NULL;
1238			INP_UNLOCK(inp);
1239			break;
1240		}
1241		/*
1242		 * The selected interface is identified by its local
1243		 * IP address.  Find the interface and confirm that
1244		 * it supports multicasting.
1245		 */
1246		s = splimp();
1247		ifp = ip_multicast_if(&addr, &ifindex);
1248		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1249			INP_UNLOCK(inp);
1250			splx(s);
1251			error = EADDRNOTAVAIL;
1252			break;
1253		}
1254		imo->imo_multicast_ifp = ifp;
1255		if (ifindex)
1256			imo->imo_multicast_addr = addr;
1257		else
1258			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1259		INP_UNLOCK(inp);
1260		splx(s);
1261		break;
1262
1263	case IP_MULTICAST_TTL:
1264		/*
1265		 * Set the IP time-to-live for outgoing multicast packets.
1266		 * The original multicast API required a char argument,
1267		 * which is inconsistent with the rest of the socket API.
1268		 * We allow either a char or an int.
1269		 */
1270		if (sopt->sopt_valsize == 1) {
1271			u_char ttl;
1272			error = sooptcopyin(sopt, &ttl, 1, 1);
1273			if (error)
1274				break;
1275			imo = ip_findmoptions(inp);
1276			imo->imo_multicast_ttl = ttl;
1277			INP_UNLOCK(inp);
1278		} else {
1279			u_int ttl;
1280			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1281					    sizeof ttl);
1282			if (error)
1283				break;
1284			if (ttl > 255)
1285				error = EINVAL;
1286			else {
1287				imo = ip_findmoptions(inp);
1288				imo->imo_multicast_ttl = ttl;
1289				INP_UNLOCK(inp);
1290			}
1291		}
1292		break;
1293
1294	case IP_MULTICAST_LOOP:
1295		/*
1296		 * Set the loopback flag for outgoing multicast packets.
1297		 * Must be zero or one.  The original multicast API required a
1298		 * char argument, which is inconsistent with the rest
1299		 * of the socket API.  We allow either a char or an int.
1300		 */
1301		if (sopt->sopt_valsize == 1) {
1302			u_char loop;
1303			error = sooptcopyin(sopt, &loop, 1, 1);
1304			if (error)
1305				break;
1306			imo = ip_findmoptions(inp);
1307			imo->imo_multicast_loop = !!loop;
1308			INP_UNLOCK(inp);
1309		} else {
1310			u_int loop;
1311			error = sooptcopyin(sopt, &loop, sizeof loop,
1312					    sizeof loop);
1313			if (error)
1314				break;
1315			imo = ip_findmoptions(inp);
1316			imo->imo_multicast_loop = !!loop;
1317			INP_UNLOCK(inp);
1318		}
1319		break;
1320
1321	case IP_ADD_MEMBERSHIP:
1322		/*
1323		 * Add a multicast group membership.
1324		 * Group must be a valid IP multicast address.
1325		 */
1326		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1327		if (error)
1328			break;
1329
1330		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1331			error = EINVAL;
1332			break;
1333		}
1334		s = splimp();
1335		/*
1336		 * If no interface address was provided, use the interface of
1337		 * the route to the given multicast address.
1338		 */
1339		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1340			bzero((caddr_t)&ro, sizeof(ro));
1341			dst = (struct sockaddr_in *)&ro.ro_dst;
1342			dst->sin_len = sizeof(*dst);
1343			dst->sin_family = AF_INET;
1344			dst->sin_addr = mreq.imr_multiaddr;
1345			rtalloc_ign(&ro, RTF_CLONING);
1346			if (ro.ro_rt == NULL) {
1347				error = EADDRNOTAVAIL;
1348				splx(s);
1349				break;
1350			}
1351			ifp = ro.ro_rt->rt_ifp;
1352			RTFREE(ro.ro_rt);
1353		}
1354		else {
1355			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1356		}
1357
1358		/*
1359		 * See if we found an interface, and confirm that it
1360		 * supports multicast.
1361		 */
1362		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1363			error = EADDRNOTAVAIL;
1364			splx(s);
1365			break;
1366		}
1367		/*
1368		 * See if the membership already exists or if all the
1369		 * membership slots are full.
1370		 */
1371		imo = ip_findmoptions(inp);
1372		for (i = 0; i < imo->imo_num_memberships; ++i) {
1373			if (imo->imo_membership[i]->inm_ifp == ifp &&
1374			    imo->imo_membership[i]->inm_addr.s_addr
1375						== mreq.imr_multiaddr.s_addr)
1376				break;
1377		}
1378		if (i < imo->imo_num_memberships) {
1379			INP_UNLOCK(inp);
1380			error = EADDRINUSE;
1381			splx(s);
1382			break;
1383		}
1384		if (imo->imo_num_memberships == imo->imo_max_memberships) {
1385		    struct in_multi **nmships, **omships;
1386		    size_t newmax;
1387		    /*
1388		     * Resize the vector to next power-of-two minus 1. If the
1389		     * size would exceed the maximum then we know we've really
1390		     * run out of entries. Otherwise, we realloc() the vector
1391		     * with the INP lock held to avoid introducing a race.
1392		     */
1393		    nmships = NULL;
1394		    omships = imo->imo_membership;
1395		    newmax = ((imo->imo_max_memberships + 1) * 2) - 1;
1396		    if (newmax <= IP_MAX_MEMBERSHIPS) {
1397			nmships = (struct in_multi **)realloc(omships,
1398sizeof(*nmships) * newmax, M_IPMOPTS, M_NOWAIT);
1399			if (nmships != NULL) {
1400			    imo->imo_membership = nmships;
1401			    imo->imo_max_memberships = newmax;
1402			}
1403		    }
1404		    if (nmships == NULL) {
1405			INP_UNLOCK(inp);
1406			error = ETOOMANYREFS;
1407			splx(s);
1408			break;
1409		    }
1410		}
1411		/*
1412		 * Everything looks good; add a new record to the multicast
1413		 * address list for the given interface.
1414		 */
1415		if ((imo->imo_membership[i] =
1416		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1417			INP_UNLOCK(inp);
1418			error = ENOBUFS;
1419			splx(s);
1420			break;
1421		}
1422		++imo->imo_num_memberships;
1423		INP_UNLOCK(inp);
1424		splx(s);
1425		break;
1426
1427	case IP_DROP_MEMBERSHIP:
1428		/*
1429		 * Drop a multicast group membership.
1430		 * Group must be a valid IP multicast address.
1431		 */
1432		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1433		if (error)
1434			break;
1435
1436		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1437			error = EINVAL;
1438			break;
1439		}
1440
1441		s = splimp();
1442		/*
1443		 * If an interface address was specified, get a pointer
1444		 * to its ifnet structure.
1445		 */
1446		if (mreq.imr_interface.s_addr == INADDR_ANY)
1447			ifp = NULL;
1448		else {
1449			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1450			if (ifp == NULL) {
1451				error = EADDRNOTAVAIL;
1452				splx(s);
1453				break;
1454			}
1455		}
1456		/*
1457		 * Find the membership in the membership array.
1458		 */
1459		imo = ip_findmoptions(inp);
1460		for (i = 0; i < imo->imo_num_memberships; ++i) {
1461			if ((ifp == NULL ||
1462			     imo->imo_membership[i]->inm_ifp == ifp) &&
1463			     imo->imo_membership[i]->inm_addr.s_addr ==
1464			     mreq.imr_multiaddr.s_addr)
1465				break;
1466		}
1467		if (i == imo->imo_num_memberships) {
1468			INP_UNLOCK(inp);
1469			error = EADDRNOTAVAIL;
1470			splx(s);
1471			break;
1472		}
1473		/*
1474		 * Give up the multicast address record to which the
1475		 * membership points.
1476		 */
1477		in_delmulti(imo->imo_membership[i]);
1478		/*
1479		 * Remove the gap in the membership array.
1480		 */
1481		for (++i; i < imo->imo_num_memberships; ++i)
1482			imo->imo_membership[i-1] = imo->imo_membership[i];
1483		--imo->imo_num_memberships;
1484		INP_UNLOCK(inp);
1485		splx(s);
1486		break;
1487
1488	default:
1489		error = EOPNOTSUPP;
1490		break;
1491	}
1492
1493	return (error);
1494}
1495
1496/*
1497 * Return the IP multicast options in response to user getsockopt().
1498 */
1499static int
1500ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1501{
1502	struct ip_moptions *imo;
1503	struct in_addr addr;
1504	struct in_ifaddr *ia;
1505	int error, optval;
1506	u_char coptval;
1507
1508	INP_LOCK(inp);
1509	imo = inp->inp_moptions;
1510
1511	error = 0;
1512	switch (sopt->sopt_name) {
1513	case IP_MULTICAST_VIF:
1514		if (imo != NULL)
1515			optval = imo->imo_multicast_vif;
1516		else
1517			optval = -1;
1518		INP_UNLOCK(inp);
1519		error = sooptcopyout(sopt, &optval, sizeof optval);
1520		break;
1521
1522	case IP_MULTICAST_IF:
1523		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1524			addr.s_addr = INADDR_ANY;
1525		else if (imo->imo_multicast_addr.s_addr) {
1526			/* return the value user has set */
1527			addr = imo->imo_multicast_addr;
1528		} else {
1529			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1530			addr.s_addr = (ia == NULL) ? INADDR_ANY
1531				: IA_SIN(ia)->sin_addr.s_addr;
1532		}
1533		INP_UNLOCK(inp);
1534		error = sooptcopyout(sopt, &addr, sizeof addr);
1535		break;
1536
1537	case IP_MULTICAST_TTL:
1538		if (imo == 0)
1539			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1540		else
1541			optval = coptval = imo->imo_multicast_ttl;
1542		INP_UNLOCK(inp);
1543		if (sopt->sopt_valsize == 1)
1544			error = sooptcopyout(sopt, &coptval, 1);
1545		else
1546			error = sooptcopyout(sopt, &optval, sizeof optval);
1547		break;
1548
1549	case IP_MULTICAST_LOOP:
1550		if (imo == 0)
1551			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1552		else
1553			optval = coptval = imo->imo_multicast_loop;
1554		INP_UNLOCK(inp);
1555		if (sopt->sopt_valsize == 1)
1556			error = sooptcopyout(sopt, &coptval, 1);
1557		else
1558			error = sooptcopyout(sopt, &optval, sizeof optval);
1559		break;
1560
1561	default:
1562		INP_UNLOCK(inp);
1563		error = ENOPROTOOPT;
1564		break;
1565	}
1566	INP_UNLOCK_ASSERT(inp);
1567
1568	return (error);
1569}
1570
1571/*
1572 * Discard the IP multicast options.
1573 */
1574void
1575ip_freemoptions(imo)
1576	register struct ip_moptions *imo;
1577{
1578	register int i;
1579
1580	if (imo != NULL) {
1581		for (i = 0; i < imo->imo_num_memberships; ++i)
1582			in_delmulti(imo->imo_membership[i]);
1583		free(imo->imo_membership, M_IPMOPTS);
1584		free(imo, M_IPMOPTS);
1585	}
1586}
1587
1588/*
1589 * Routine called from ip_output() to loop back a copy of an IP multicast
1590 * packet to the input queue of a specified interface.  Note that this
1591 * calls the output routine of the loopback "driver", but with an interface
1592 * pointer that might NOT be a loopback interface -- evil, but easier than
1593 * replicating that code here.
1594 */
1595static void
1596ip_mloopback(ifp, m, dst, hlen)
1597	struct ifnet *ifp;
1598	register struct mbuf *m;
1599	register struct sockaddr_in *dst;
1600	int hlen;
1601{
1602	register struct ip *ip;
1603	struct mbuf *copym;
1604
1605	copym = m_copy(m, 0, M_COPYALL);
1606	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1607		copym = m_pullup(copym, hlen);
1608	if (copym != NULL) {
1609		/* If needed, compute the checksum and mark it as valid. */
1610		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1611			in_delayed_cksum(copym);
1612			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1613			copym->m_pkthdr.csum_flags |=
1614			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1615			copym->m_pkthdr.csum_data = 0xffff;
1616		}
1617		/*
1618		 * We don't bother to fragment if the IP length is greater
1619		 * than the interface's MTU.  Can this possibly matter?
1620		 */
1621		ip = mtod(copym, struct ip *);
1622		ip->ip_len = htons(ip->ip_len);
1623		ip->ip_off = htons(ip->ip_off);
1624		ip->ip_sum = 0;
1625		ip->ip_sum = in_cksum(copym, hlen);
1626		/*
1627		 * NB:
1628		 * It's not clear whether there are any lingering
1629		 * reentrancy problems in other areas which might
1630		 * be exposed by using ip_input directly (in
1631		 * particular, everything which modifies the packet
1632		 * in-place).  Yet another option is using the
1633		 * protosw directly to deliver the looped back
1634		 * packet.  For the moment, we'll err on the side
1635		 * of safety by using if_simloop().
1636		 */
1637#if 1 /* XXX */
1638		if (dst->sin_family != AF_INET) {
1639			printf("ip_mloopback: bad address family %d\n",
1640						dst->sin_family);
1641			dst->sin_family = AF_INET;
1642		}
1643#endif
1644
1645#ifdef notdef
1646		copym->m_pkthdr.rcvif = ifp;
1647		ip_input(copym);
1648#else
1649		if_simloop(ifp, copym, dst->sin_family, 0);
1650#endif
1651	}
1652}
1653