ip_output.c revision 37996
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 *	$Id: ip_output.c,v 1.79 1998/07/13 12:12:25 bde Exp $
35 */
36
37#define _IP_VHL
38
39#include "opt_ipfw.h"
40#include "opt_ipdivert.h"
41#include "opt_ipfilter.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/protosw.h>
48#include <sys/socket.h>
49#include <sys/socketvar.h>
50
51#include <net/if.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
61#ifdef vax
62#include <machine/mtpr.h>
63#endif
64#include <machine/in_cksum.h>
65
66static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
67
68#if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
69#undef COMPAT_IPFW
70#define COMPAT_IPFW 1
71#else
72#undef COMPAT_IPFW
73#endif
74
75#ifdef IPFIREWALL_FORWARD_DEBUG
76#define print_ip(a)	 printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
77				 		  (ntohl(a.s_addr)>>16)&0xFF,\
78						  (ntohl(a.s_addr)>>8)&0xFF,\
79						  (ntohl(a.s_addr))&0xFF);
80#endif
81
82u_short ip_id;
83
84static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
85static void	ip_mloopback
86	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int));
87static int	ip_getmoptions
88	__P((int, struct ip_moptions *, struct mbuf **));
89static int	ip_pcbopts __P((struct mbuf **, struct mbuf *));
90static int	ip_setmoptions
91	__P((int, struct ip_moptions **, struct mbuf *));
92
93#if defined(IPFILTER_LKM) || defined(IPFILTER)
94int	ip_optcopy __P((struct ip *, struct ip *));
95extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
96#else
97static int	ip_optcopy __P((struct ip *, struct ip *));
98#endif
99
100
101extern	struct protosw inetsw[];
102
103/*
104 * IP output.  The packet in mbuf chain m contains a skeletal IP
105 * header (with len, off, ttl, proto, tos, src, dst).
106 * The mbuf chain containing the packet will be freed.
107 * The mbuf opt, if present, will not be freed.
108 */
109int
110ip_output(m0, opt, ro, flags, imo)
111	struct mbuf *m0;
112	struct mbuf *opt;
113	struct route *ro;
114	int flags;
115	struct ip_moptions *imo;
116{
117	struct ip *ip, *mhip;
118	struct ifnet *ifp;
119	struct mbuf *m = m0;
120	int hlen = sizeof (struct ip);
121	int len, off, error = 0;
122	struct sockaddr_in *dst;
123	struct in_ifaddr *ia;
124	int isbroadcast;
125#ifdef IPFIREWALL_FORWARD
126	int fwd_rewrite_src = 0;
127#endif
128
129#ifdef	DIAGNOSTIC
130	if ((m->m_flags & M_PKTHDR) == 0)
131		panic("ip_output no HDR");
132	if (!ro)
133		panic("ip_output no route, proto = %d",
134		      mtod(m, struct ip *)->ip_p);
135#endif
136	if (opt) {
137		m = ip_insertoptions(m, opt, &len);
138		hlen = len;
139	}
140	ip = mtod(m, struct ip *);
141	/*
142	 * Fill in IP header.
143	 */
144	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
145		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
146		ip->ip_off &= IP_DF;
147		ip->ip_id = htons(ip_id++);
148		ipstat.ips_localout++;
149	} else {
150		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
151	}
152
153	dst = (struct sockaddr_in *)&ro->ro_dst;
154	/*
155	 * If there is a cached route,
156	 * check that it is to the same destination
157	 * and is still up.  If not, free it and try again.
158	 */
159	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
160	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
161		RTFREE(ro->ro_rt);
162		ro->ro_rt = (struct rtentry *)0;
163	}
164	if (ro->ro_rt == 0) {
165		dst->sin_family = AF_INET;
166		dst->sin_len = sizeof(*dst);
167		dst->sin_addr = ip->ip_dst;
168	}
169	/*
170	 * If routing to interface only,
171	 * short circuit routing lookup.
172	 */
173#define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
174#define sintosa(sin)	((struct sockaddr *)(sin))
175	if (flags & IP_ROUTETOIF) {
176		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
177		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
178			ipstat.ips_noroute++;
179			error = ENETUNREACH;
180			goto bad;
181		}
182		ifp = ia->ia_ifp;
183		ip->ip_ttl = 1;
184		isbroadcast = in_broadcast(dst->sin_addr, ifp);
185	} else {
186		/*
187		 * If this is the case, we probably don't want to allocate
188		 * a protocol-cloned route since we didn't get one from the
189		 * ULP.  This lets TCP do its thing, while not burdening
190		 * forwarding or ICMP with the overhead of cloning a route.
191		 * Of course, we still want to do any cloning requested by
192		 * the link layer, as this is probably required in all cases
193		 * for correct operation (as it is for ARP).
194		 */
195		if (ro->ro_rt == 0)
196			rtalloc_ign(ro, RTF_PRCLONING);
197		if (ro->ro_rt == 0) {
198			ipstat.ips_noroute++;
199			error = EHOSTUNREACH;
200			goto bad;
201		}
202		ia = ifatoia(ro->ro_rt->rt_ifa);
203		ifp = ro->ro_rt->rt_ifp;
204		ro->ro_rt->rt_use++;
205		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
206			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
207		if (ro->ro_rt->rt_flags & RTF_HOST)
208			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
209		else
210			isbroadcast = in_broadcast(dst->sin_addr, ifp);
211	}
212	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
213		struct in_multi *inm;
214
215		m->m_flags |= M_MCAST;
216		/*
217		 * IP destination address is multicast.  Make sure "dst"
218		 * still points to the address in "ro".  (It may have been
219		 * changed to point to a gateway address, above.)
220		 */
221		dst = (struct sockaddr_in *)&ro->ro_dst;
222		/*
223		 * See if the caller provided any multicast options
224		 */
225		if (imo != NULL) {
226			ip->ip_ttl = imo->imo_multicast_ttl;
227			if (imo->imo_multicast_ifp != NULL)
228				ifp = imo->imo_multicast_ifp;
229			if (imo->imo_multicast_vif != -1)
230				ip->ip_src.s_addr =
231				    ip_mcast_src(imo->imo_multicast_vif);
232		} else
233			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
234		/*
235		 * Confirm that the outgoing interface supports multicast.
236		 */
237		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
238			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
239				ipstat.ips_noroute++;
240				error = ENETUNREACH;
241				goto bad;
242			}
243		}
244		/*
245		 * If source address not specified yet, use address
246		 * of outgoing interface.
247		 */
248		if (ip->ip_src.s_addr == INADDR_ANY) {
249			register struct in_ifaddr *ia1;
250
251			for (ia1 = in_ifaddrhead.tqh_first; ia1;
252			     ia1 = ia1->ia_link.tqe_next)
253				if (ia1->ia_ifp == ifp) {
254					ip->ip_src = IA_SIN(ia1)->sin_addr;
255					break;
256				}
257		}
258
259		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
260		if (inm != NULL &&
261		   (imo == NULL || imo->imo_multicast_loop)) {
262			/*
263			 * If we belong to the destination multicast group
264			 * on the outgoing interface, and the caller did not
265			 * forbid loopback, loop back a copy.
266			 */
267			ip_mloopback(ifp, m, dst, hlen);
268		}
269		else {
270			/*
271			 * If we are acting as a multicast router, perform
272			 * multicast forwarding as if the packet had just
273			 * arrived on the interface to which we are about
274			 * to send.  The multicast forwarding function
275			 * recursively calls this function, using the
276			 * IP_FORWARDING flag to prevent infinite recursion.
277			 *
278			 * Multicasts that are looped back by ip_mloopback(),
279			 * above, will be forwarded by the ip_input() routine,
280			 * if necessary.
281			 */
282			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
283				/*
284				 * Check if rsvp daemon is running. If not, don't
285				 * set ip_moptions. This ensures that the packet
286				 * is multicast and not just sent down one link
287				 * as prescribed by rsvpd.
288				 */
289				if (!rsvp_on)
290				  imo = NULL;
291				if (ip_mforward(ip, ifp, m, imo) != 0) {
292					m_freem(m);
293					goto done;
294				}
295			}
296		}
297
298		/*
299		 * Multicasts with a time-to-live of zero may be looped-
300		 * back, above, but must not be transmitted on a network.
301		 * Also, multicasts addressed to the loopback interface
302		 * are not sent -- the above call to ip_mloopback() will
303		 * loop back a copy if this host actually belongs to the
304		 * destination group on the loopback interface.
305		 */
306		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
307			m_freem(m);
308			goto done;
309		}
310
311		goto sendit;
312	}
313#ifndef notdef
314	/*
315	 * If source address not specified yet, use address
316	 * of outgoing interface.
317	 */
318	if (ip->ip_src.s_addr == INADDR_ANY) {
319		ip->ip_src = IA_SIN(ia)->sin_addr;
320#ifdef IPFIREWALL_FORWARD
321		/* Keep note that we did this - if the firewall changes
322		 * the next-hop, our interface may change, changing the
323		 * default source IP. It's a shame so much effort happens
324		 * twice. Oh well.
325		 */
326		fwd_rewrite_src++;
327#endif /* IPFIREWALL_FORWARD */
328	}
329#endif /* notdef */
330	/*
331	 * Verify that we have any chance at all of being able to queue
332	 *      the packet or packet fragments
333	 */
334	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
335		ifp->if_snd.ifq_maxlen) {
336			error = ENOBUFS;
337			goto bad;
338	}
339
340	/*
341	 * Look for broadcast address and
342	 * and verify user is allowed to send
343	 * such a packet.
344	 */
345	if (isbroadcast) {
346		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
347			error = EADDRNOTAVAIL;
348			goto bad;
349		}
350		if ((flags & IP_ALLOWBROADCAST) == 0) {
351			error = EACCES;
352			goto bad;
353		}
354		/* don't allow broadcast messages to be fragmented */
355		if ((u_short)ip->ip_len > ifp->if_mtu) {
356			error = EMSGSIZE;
357			goto bad;
358		}
359		m->m_flags |= M_BCAST;
360	} else {
361		m->m_flags &= ~M_BCAST;
362	}
363
364sendit:
365	/*
366	 * IpHack's section.
367	 * - Xlate: translate packet's addr/port (NAT).
368	 * - Firewall: deny/allow/etc.
369	 * - Wrap: fake packet's addr/port <unimpl.>
370	 * - Encapsulate: put it in another IP and send out. <unimp.>
371	 */
372#if defined(IPFILTER) || defined(IPFILTER_LKM)
373	if (fr_checkp) {
374		struct  mbuf    *m1 = m;
375
376		if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1)
377			goto done;
378		ip = mtod(m = m1, struct ip *);
379	}
380#endif
381
382#ifdef COMPAT_IPFW
383        if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, ifp, IP_NAT_OUT)) {
384		error = EACCES;
385		goto done;
386	}
387
388	/*
389	 * Check with the firewall...
390	 */
391	if (ip_fw_chk_ptr) {
392#ifdef IPFIREWALL_FORWARD
393		struct sockaddr_in *old = dst;
394#endif
395#ifdef IPDIVERT
396		ip_divert_port = (*ip_fw_chk_ptr)(&ip,
397		    hlen, ifp, &ip_divert_cookie, &m, &dst);
398		if (ip_divert_port) {		/* Divert packet */
399			(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
400			goto done;
401		}
402#else	/* !IPDIVERT */
403		u_int16_t 	dummy = 0;
404		/* If ipfw says divert, we have to just drop packet */
405		if ((*ip_fw_chk_ptr)(&ip, hlen, ifp, &dummy, &m, &dst)) {
406			m_freem(m);
407			goto done;
408		}
409#endif	/* !IPDIVERT */
410		if (!m) {
411			error = EACCES;
412			goto done;
413		}
414#ifdef IPFIREWALL_FORWARD
415		/* Here we check dst to make sure it's directly reachable on the
416		 * interface we previously thought it was.
417		 * If it isn't (which may be likely in some situations) we have
418		 * to re-route it (ie, find a route for the next-hop and the
419		 * associated interface) and set them here. This is nested
420		 * forwarding which in most cases is undesirable, except where
421		 * such control is nigh impossible. So we do it here.
422		 * And I'm babbling.
423		 */
424		if (old != dst) {
425			struct in_ifaddr *ia;
426
427			/* It's changed... */
428			/* There must be a better way to do this next line... */
429			static struct route sro_fwd, *ro_fwd = &sro_fwd;
430#ifdef IPFIREWALL_FORWARD_DEBUG
431			printf("IPFIREWALL_FORWARD: New dst ip: ");
432			print_ip(dst->sin_addr);
433			printf("\n");
434#endif
435			/*
436			 * We need to figure out if we have been forwarded
437			 * to a local socket. If so then we should somehow
438			 * "loop back" to ip_input, and get directed to the
439			 * PCB as if we had received this packet. This is
440			 * because it may be dificult to identify the packets
441			 * you want to forward until they are being output
442			 * and have selected an interface. (e.g. locally
443			 * initiated packets) If we used the loopback inteface,
444			 * we would not be able to control what happens
445			 * as the packet runs through ip_input() as
446			 * it is done through a ISR.
447			 */
448			for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
449					ia = TAILQ_NEXT(ia, ia_link)) {
450				/*
451				 * If the addr to forward to is one
452				 * of ours, we pretend to
453				 * be the destination for this packet.
454				 */
455				if (IA_SIN(ia)->sin_addr.s_addr ==
456						 dst->sin_addr.s_addr)
457					break;
458			}
459			if (ia) {
460				/* tell ip_input "dont filter" */
461				ip_fw_fwd_addr = dst;
462				if (m->m_pkthdr.rcvif == NULL)
463					m->m_pkthdr.rcvif = ifunit("lo0");
464				ip->ip_len = htons((u_short)ip->ip_len);
465				ip->ip_off = htons((u_short)ip->ip_off);
466				ip->ip_sum = 0;
467				if (ip->ip_vhl == IP_VHL_BORING) {
468					ip->ip_sum = in_cksum_hdr(ip);
469				} else {
470					ip->ip_sum = in_cksum(m, hlen);
471				}
472				ip_input(m);
473				goto done;
474			}
475			/* Some of the logic for this was
476			 * nicked from above.
477			 *
478			 * This rewrites the cached route in a local PCB.
479			 * Is this what we want to do?
480			 */
481			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
482
483			ro_fwd->ro_rt = 0;
484			rtalloc_ign(ro_fwd, RTF_PRCLONING);
485
486			if (ro_fwd->ro_rt == 0) {
487				ipstat.ips_noroute++;
488				error = EHOSTUNREACH;
489				goto bad;
490			}
491
492			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
493			ifp = ro_fwd->ro_rt->rt_ifp;
494			ro_fwd->ro_rt->rt_use++;
495			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
496				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
497			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
498				isbroadcast =
499				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
500			else
501				isbroadcast = in_broadcast(dst->sin_addr, ifp);
502			RTFREE(ro->ro_rt);
503			ro->ro_rt = ro_fwd->ro_rt;
504			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
505
506			/*
507			 * If we added a default src ip earlier,
508			 * which would have been gotten from the-then
509			 * interface, do it again, from the new one.
510			 */
511			if (fwd_rewrite_src)
512				ip->ip_src = IA_SIN(ia)->sin_addr;
513		}
514#endif /* IPFIREWALL_FORWARD */
515	}
516#endif /* COMPAT_IPFW */
517
518
519	/*
520	 * If small enough for interface, can just send directly.
521	 */
522	if ((u_short)ip->ip_len <= ifp->if_mtu) {
523		ip->ip_len = htons((u_short)ip->ip_len);
524		ip->ip_off = htons((u_short)ip->ip_off);
525		ip->ip_sum = 0;
526		if (ip->ip_vhl == IP_VHL_BORING) {
527			ip->ip_sum = in_cksum_hdr(ip);
528		} else {
529			ip->ip_sum = in_cksum(m, hlen);
530		}
531		error = (*ifp->if_output)(ifp, m,
532				(struct sockaddr *)dst, ro->ro_rt);
533		goto done;
534	}
535	/*
536	 * Too large for interface; fragment if possible.
537	 * Must be able to put at least 8 bytes per fragment.
538	 */
539	if (ip->ip_off & IP_DF) {
540		error = EMSGSIZE;
541		/*
542		 * This case can happen if the user changed the MTU
543		 * of an interface after enabling IP on it.  Because
544		 * most netifs don't keep track of routes pointing to
545		 * them, there is no way for one to update all its
546		 * routes when the MTU is changed.
547		 */
548		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
549		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
550		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
551			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
552		}
553		ipstat.ips_cantfrag++;
554		goto bad;
555	}
556	len = (ifp->if_mtu - hlen) &~ 7;
557	if (len < 8) {
558		error = EMSGSIZE;
559		goto bad;
560	}
561
562    {
563	int mhlen, firstlen = len;
564	struct mbuf **mnext = &m->m_nextpkt;
565
566	/*
567	 * Loop through length of segment after first fragment,
568	 * make new header and copy data of each part and link onto chain.
569	 */
570	m0 = m;
571	mhlen = sizeof (struct ip);
572	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
573		MGETHDR(m, M_DONTWAIT, MT_HEADER);
574		if (m == 0) {
575			error = ENOBUFS;
576			ipstat.ips_odropped++;
577			goto sendorfree;
578		}
579		m->m_data += max_linkhdr;
580		mhip = mtod(m, struct ip *);
581		*mhip = *ip;
582		if (hlen > sizeof (struct ip)) {
583			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
584			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
585		}
586		m->m_len = mhlen;
587		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
588		if (ip->ip_off & IP_MF)
589			mhip->ip_off |= IP_MF;
590		if (off + len >= (u_short)ip->ip_len)
591			len = (u_short)ip->ip_len - off;
592		else
593			mhip->ip_off |= IP_MF;
594		mhip->ip_len = htons((u_short)(len + mhlen));
595		m->m_next = m_copy(m0, off, len);
596		if (m->m_next == 0) {
597			(void) m_free(m);
598			error = ENOBUFS;	/* ??? */
599			ipstat.ips_odropped++;
600			goto sendorfree;
601		}
602		m->m_pkthdr.len = mhlen + len;
603		m->m_pkthdr.rcvif = (struct ifnet *)0;
604		mhip->ip_off = htons((u_short)mhip->ip_off);
605		mhip->ip_sum = 0;
606		if (mhip->ip_vhl == IP_VHL_BORING) {
607			mhip->ip_sum = in_cksum_hdr(mhip);
608		} else {
609			mhip->ip_sum = in_cksum(m, mhlen);
610		}
611		*mnext = m;
612		mnext = &m->m_nextpkt;
613		ipstat.ips_ofragments++;
614	}
615	/*
616	 * Update first fragment by trimming what's been copied out
617	 * and updating header, then send each fragment (in order).
618	 */
619	m = m0;
620	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
621	m->m_pkthdr.len = hlen + firstlen;
622	ip->ip_len = htons((u_short)m->m_pkthdr.len);
623	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
624	ip->ip_sum = 0;
625	if (ip->ip_vhl == IP_VHL_BORING) {
626		ip->ip_sum = in_cksum_hdr(ip);
627	} else {
628		ip->ip_sum = in_cksum(m, hlen);
629	}
630sendorfree:
631	for (m = m0; m; m = m0) {
632		m0 = m->m_nextpkt;
633		m->m_nextpkt = 0;
634		if (error == 0)
635			error = (*ifp->if_output)(ifp, m,
636			    (struct sockaddr *)dst, ro->ro_rt);
637		else
638			m_freem(m);
639	}
640
641	if (error == 0)
642		ipstat.ips_fragmented++;
643    }
644done:
645	return (error);
646bad:
647	m_freem(m0);
648	goto done;
649}
650
651/*
652 * Insert IP options into preformed packet.
653 * Adjust IP destination as required for IP source routing,
654 * as indicated by a non-zero in_addr at the start of the options.
655 *
656 * XXX This routine assumes that the packet has no options in place.
657 */
658static struct mbuf *
659ip_insertoptions(m, opt, phlen)
660	register struct mbuf *m;
661	struct mbuf *opt;
662	int *phlen;
663{
664	register struct ipoption *p = mtod(opt, struct ipoption *);
665	struct mbuf *n;
666	register struct ip *ip = mtod(m, struct ip *);
667	unsigned optlen;
668
669	optlen = opt->m_len - sizeof(p->ipopt_dst);
670	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
671		return (m);		/* XXX should fail */
672	if (p->ipopt_dst.s_addr)
673		ip->ip_dst = p->ipopt_dst;
674	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
675		MGETHDR(n, M_DONTWAIT, MT_HEADER);
676		if (n == 0)
677			return (m);
678		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
679		m->m_len -= sizeof(struct ip);
680		m->m_data += sizeof(struct ip);
681		n->m_next = m;
682		m = n;
683		m->m_len = optlen + sizeof(struct ip);
684		m->m_data += max_linkhdr;
685		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
686	} else {
687		m->m_data -= optlen;
688		m->m_len += optlen;
689		m->m_pkthdr.len += optlen;
690		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
691	}
692	ip = mtod(m, struct ip *);
693	bcopy(p->ipopt_list, ip + 1, optlen);
694	*phlen = sizeof(struct ip) + optlen;
695	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
696	ip->ip_len += optlen;
697	return (m);
698}
699
700/*
701 * Copy options from ip to jp,
702 * omitting those not copied during fragmentation.
703 */
704#if !defined(IPFILTER) && !defined(IPFILTER_LKM)
705static
706#endif
707int
708ip_optcopy(ip, jp)
709	struct ip *ip, *jp;
710{
711	register u_char *cp, *dp;
712	int opt, optlen, cnt;
713
714	cp = (u_char *)(ip + 1);
715	dp = (u_char *)(jp + 1);
716	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
717	for (; cnt > 0; cnt -= optlen, cp += optlen) {
718		opt = cp[0];
719		if (opt == IPOPT_EOL)
720			break;
721		if (opt == IPOPT_NOP) {
722			/* Preserve for IP mcast tunnel's LSRR alignment. */
723			*dp++ = IPOPT_NOP;
724			optlen = 1;
725			continue;
726		} else
727			optlen = cp[IPOPT_OLEN];
728		/* bogus lengths should have been caught by ip_dooptions */
729		if (optlen > cnt)
730			optlen = cnt;
731		if (IPOPT_COPIED(opt)) {
732			bcopy(cp, dp, optlen);
733			dp += optlen;
734		}
735	}
736	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
737		*dp++ = IPOPT_EOL;
738	return (optlen);
739}
740
741/*
742 * IP socket option processing.
743 */
744int
745ip_ctloutput(op, so, level, optname, mp, p)
746	int op;
747	struct socket *so;
748	int level, optname;
749	struct mbuf **mp;
750	struct proc *p;
751{
752	register struct inpcb *inp = sotoinpcb(so);
753	register struct mbuf *m = *mp;
754	register int optval = 0;
755	int error = 0;
756
757	if (level != IPPROTO_IP) {
758		error = EINVAL;
759		if (op == PRCO_SETOPT && *mp)
760			(void) m_free(*mp);
761	} else switch (op) {
762
763	case PRCO_SETOPT:
764		switch (optname) {
765		case IP_OPTIONS:
766#ifdef notyet
767		case IP_RETOPTS:
768			return (ip_pcbopts(optname, &inp->inp_options, m));
769#else
770			return (ip_pcbopts(&inp->inp_options, m));
771#endif
772
773		case IP_TOS:
774		case IP_TTL:
775		case IP_RECVOPTS:
776		case IP_RECVRETOPTS:
777		case IP_RECVDSTADDR:
778		case IP_RECVIF:
779			if (m == 0 || m->m_len != sizeof(int))
780				error = EINVAL;
781			else {
782				optval = *mtod(m, int *);
783				switch (optname) {
784
785				case IP_TOS:
786					inp->inp_ip_tos = optval;
787					break;
788
789				case IP_TTL:
790					inp->inp_ip_ttl = optval;
791					break;
792#define	OPTSET(bit) \
793	if (optval) \
794		inp->inp_flags |= bit; \
795	else \
796		inp->inp_flags &= ~bit;
797
798				case IP_RECVOPTS:
799					OPTSET(INP_RECVOPTS);
800					break;
801
802				case IP_RECVRETOPTS:
803					OPTSET(INP_RECVRETOPTS);
804					break;
805
806				case IP_RECVDSTADDR:
807					OPTSET(INP_RECVDSTADDR);
808					break;
809
810				case IP_RECVIF:
811					OPTSET(INP_RECVIF);
812					break;
813				}
814			}
815			break;
816#undef OPTSET
817
818		case IP_MULTICAST_IF:
819		case IP_MULTICAST_VIF:
820		case IP_MULTICAST_TTL:
821		case IP_MULTICAST_LOOP:
822		case IP_ADD_MEMBERSHIP:
823		case IP_DROP_MEMBERSHIP:
824			error = ip_setmoptions(optname, &inp->inp_moptions, m);
825			break;
826
827		case IP_PORTRANGE:
828			if (m == 0 || m->m_len != sizeof(int))
829				error = EINVAL;
830			else {
831				optval = *mtod(m, int *);
832
833				switch (optval) {
834
835				case IP_PORTRANGE_DEFAULT:
836					inp->inp_flags &= ~(INP_LOWPORT);
837					inp->inp_flags &= ~(INP_HIGHPORT);
838					break;
839
840				case IP_PORTRANGE_HIGH:
841					inp->inp_flags &= ~(INP_LOWPORT);
842					inp->inp_flags |= INP_HIGHPORT;
843					break;
844
845				case IP_PORTRANGE_LOW:
846					inp->inp_flags &= ~(INP_HIGHPORT);
847					inp->inp_flags |= INP_LOWPORT;
848					break;
849
850				default:
851					error = EINVAL;
852					break;
853				}
854			}
855			break;
856
857		default:
858			error = ENOPROTOOPT;
859			break;
860		}
861		if (m)
862			(void)m_free(m);
863		break;
864
865	case PRCO_GETOPT:
866		switch (optname) {
867		case IP_OPTIONS:
868		case IP_RETOPTS:
869			*mp = m = m_get(M_WAIT, MT_SOOPTS);
870			if (inp->inp_options) {
871				m->m_len = inp->inp_options->m_len;
872				bcopy(mtod(inp->inp_options, void *),
873				    mtod(m, void *), m->m_len);
874			} else
875				m->m_len = 0;
876			break;
877
878		case IP_TOS:
879		case IP_TTL:
880		case IP_RECVOPTS:
881		case IP_RECVRETOPTS:
882		case IP_RECVDSTADDR:
883		case IP_RECVIF:
884			*mp = m = m_get(M_WAIT, MT_SOOPTS);
885			m->m_len = sizeof(int);
886			switch (optname) {
887
888			case IP_TOS:
889				optval = inp->inp_ip_tos;
890				break;
891
892			case IP_TTL:
893				optval = inp->inp_ip_ttl;
894				break;
895
896#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
897
898			case IP_RECVOPTS:
899				optval = OPTBIT(INP_RECVOPTS);
900				break;
901
902			case IP_RECVRETOPTS:
903				optval = OPTBIT(INP_RECVRETOPTS);
904				break;
905
906			case IP_RECVDSTADDR:
907				optval = OPTBIT(INP_RECVDSTADDR);
908				break;
909
910			case IP_RECVIF:
911				optval = OPTBIT(INP_RECVIF);
912				break;
913			}
914			*mtod(m, int *) = optval;
915			break;
916
917		case IP_MULTICAST_IF:
918		case IP_MULTICAST_VIF:
919		case IP_MULTICAST_TTL:
920		case IP_MULTICAST_LOOP:
921		case IP_ADD_MEMBERSHIP:
922		case IP_DROP_MEMBERSHIP:
923			error = ip_getmoptions(optname, inp->inp_moptions, mp);
924			break;
925
926		case IP_PORTRANGE:
927			*mp = m = m_get(M_WAIT, MT_SOOPTS);
928			m->m_len = sizeof(int);
929
930			if (inp->inp_flags & INP_HIGHPORT)
931				optval = IP_PORTRANGE_HIGH;
932			else if (inp->inp_flags & INP_LOWPORT)
933				optval = IP_PORTRANGE_LOW;
934			else
935				optval = 0;
936
937			*mtod(m, int *) = optval;
938			break;
939
940		default:
941			error = ENOPROTOOPT;
942			break;
943		}
944		break;
945	}
946	return (error);
947}
948
949/*
950 * Set up IP options in pcb for insertion in output packets.
951 * Store in mbuf with pointer in pcbopt, adding pseudo-option
952 * with destination address if source routed.
953 */
954static int
955#ifdef notyet
956ip_pcbopts(optname, pcbopt, m)
957	int optname;
958#else
959ip_pcbopts(pcbopt, m)
960#endif
961	struct mbuf **pcbopt;
962	register struct mbuf *m;
963{
964	register int cnt, optlen;
965	register u_char *cp;
966	u_char opt;
967
968	/* turn off any old options */
969	if (*pcbopt)
970		(void)m_free(*pcbopt);
971	*pcbopt = 0;
972	if (m == (struct mbuf *)0 || m->m_len == 0) {
973		/*
974		 * Only turning off any previous options.
975		 */
976		if (m)
977			(void)m_free(m);
978		return (0);
979	}
980
981#ifndef	vax
982	if (m->m_len % sizeof(int32_t))
983		goto bad;
984#endif
985	/*
986	 * IP first-hop destination address will be stored before
987	 * actual options; move other options back
988	 * and clear it when none present.
989	 */
990	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
991		goto bad;
992	cnt = m->m_len;
993	m->m_len += sizeof(struct in_addr);
994	cp = mtod(m, u_char *) + sizeof(struct in_addr);
995	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
996	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
997
998	for (; cnt > 0; cnt -= optlen, cp += optlen) {
999		opt = cp[IPOPT_OPTVAL];
1000		if (opt == IPOPT_EOL)
1001			break;
1002		if (opt == IPOPT_NOP)
1003			optlen = 1;
1004		else {
1005			optlen = cp[IPOPT_OLEN];
1006			if (optlen <= IPOPT_OLEN || optlen > cnt)
1007				goto bad;
1008		}
1009		switch (opt) {
1010
1011		default:
1012			break;
1013
1014		case IPOPT_LSRR:
1015		case IPOPT_SSRR:
1016			/*
1017			 * user process specifies route as:
1018			 *	->A->B->C->D
1019			 * D must be our final destination (but we can't
1020			 * check that since we may not have connected yet).
1021			 * A is first hop destination, which doesn't appear in
1022			 * actual IP option, but is stored before the options.
1023			 */
1024			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1025				goto bad;
1026			m->m_len -= sizeof(struct in_addr);
1027			cnt -= sizeof(struct in_addr);
1028			optlen -= sizeof(struct in_addr);
1029			cp[IPOPT_OLEN] = optlen;
1030			/*
1031			 * Move first hop before start of options.
1032			 */
1033			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1034			    sizeof(struct in_addr));
1035			/*
1036			 * Then copy rest of options back
1037			 * to close up the deleted entry.
1038			 */
1039			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1040			    sizeof(struct in_addr)),
1041			    (caddr_t)&cp[IPOPT_OFFSET+1],
1042			    (unsigned)cnt + sizeof(struct in_addr));
1043			break;
1044		}
1045	}
1046	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1047		goto bad;
1048	*pcbopt = m;
1049	return (0);
1050
1051bad:
1052	(void)m_free(m);
1053	return (EINVAL);
1054}
1055
1056/*
1057 * Set the IP multicast options in response to user setsockopt().
1058 */
1059static int
1060ip_setmoptions(optname, imop, m)
1061	int optname;
1062	struct ip_moptions **imop;
1063	struct mbuf *m;
1064{
1065	register int error = 0;
1066	u_char loop;
1067	register int i;
1068	struct in_addr addr;
1069	register struct ip_mreq *mreq;
1070	register struct ifnet *ifp;
1071	register struct ip_moptions *imo = *imop;
1072	struct route ro;
1073	register struct sockaddr_in *dst;
1074	int s;
1075
1076	if (imo == NULL) {
1077		/*
1078		 * No multicast option buffer attached to the pcb;
1079		 * allocate one and initialize to default values.
1080		 */
1081		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1082		    M_WAITOK);
1083
1084		if (imo == NULL)
1085			return (ENOBUFS);
1086		*imop = imo;
1087		imo->imo_multicast_ifp = NULL;
1088		imo->imo_multicast_vif = -1;
1089		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1090		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1091		imo->imo_num_memberships = 0;
1092	}
1093
1094	switch (optname) {
1095	/* store an index number for the vif you wanna use in the send */
1096	case IP_MULTICAST_VIF:
1097		if (!legal_vif_num) {
1098			error = EOPNOTSUPP;
1099			break;
1100		}
1101		if (m == NULL || m->m_len != sizeof(int)) {
1102			error = EINVAL;
1103			break;
1104		}
1105		i = *(mtod(m, int *));
1106		if (!legal_vif_num(i) && (i != -1)) {
1107			error = EINVAL;
1108			break;
1109		}
1110		imo->imo_multicast_vif = i;
1111		break;
1112
1113	case IP_MULTICAST_IF:
1114		/*
1115		 * Select the interface for outgoing multicast packets.
1116		 */
1117		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1118			error = EINVAL;
1119			break;
1120		}
1121		addr = *(mtod(m, struct in_addr *));
1122		/*
1123		 * INADDR_ANY is used to remove a previous selection.
1124		 * When no interface is selected, a default one is
1125		 * chosen every time a multicast packet is sent.
1126		 */
1127		if (addr.s_addr == INADDR_ANY) {
1128			imo->imo_multicast_ifp = NULL;
1129			break;
1130		}
1131		/*
1132		 * The selected interface is identified by its local
1133		 * IP address.  Find the interface and confirm that
1134		 * it supports multicasting.
1135		 */
1136		s = splimp();
1137		INADDR_TO_IFP(addr, ifp);
1138		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1139			splx(s);
1140			error = EADDRNOTAVAIL;
1141			break;
1142		}
1143		imo->imo_multicast_ifp = ifp;
1144		splx(s);
1145		break;
1146
1147	case IP_MULTICAST_TTL:
1148		/*
1149		 * Set the IP time-to-live for outgoing multicast packets.
1150		 */
1151		if (m == NULL || m->m_len != 1) {
1152			error = EINVAL;
1153			break;
1154		}
1155		imo->imo_multicast_ttl = *(mtod(m, u_char *));
1156		break;
1157
1158	case IP_MULTICAST_LOOP:
1159		/*
1160		 * Set the loopback flag for outgoing multicast packets.
1161		 * Must be zero or one.
1162		 */
1163		if (m == NULL || m->m_len != 1 ||
1164		   (loop = *(mtod(m, u_char *))) > 1) {
1165			error = EINVAL;
1166			break;
1167		}
1168		imo->imo_multicast_loop = loop;
1169		break;
1170
1171	case IP_ADD_MEMBERSHIP:
1172		/*
1173		 * Add a multicast group membership.
1174		 * Group must be a valid IP multicast address.
1175		 */
1176		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1177			error = EINVAL;
1178			break;
1179		}
1180		mreq = mtod(m, struct ip_mreq *);
1181		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1182			error = EINVAL;
1183			break;
1184		}
1185		s = splimp();
1186		/*
1187		 * If no interface address was provided, use the interface of
1188		 * the route to the given multicast address.
1189		 */
1190		if (mreq->imr_interface.s_addr == INADDR_ANY) {
1191			bzero((caddr_t)&ro, sizeof(ro));
1192			dst = (struct sockaddr_in *)&ro.ro_dst;
1193			dst->sin_len = sizeof(*dst);
1194			dst->sin_family = AF_INET;
1195			dst->sin_addr = mreq->imr_multiaddr;
1196			rtalloc(&ro);
1197			if (ro.ro_rt == NULL) {
1198				error = EADDRNOTAVAIL;
1199				splx(s);
1200				break;
1201			}
1202			ifp = ro.ro_rt->rt_ifp;
1203			rtfree(ro.ro_rt);
1204		}
1205		else {
1206			INADDR_TO_IFP(mreq->imr_interface, ifp);
1207		}
1208
1209		/*
1210		 * See if we found an interface, and confirm that it
1211		 * supports multicast.
1212		 */
1213		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1214			error = EADDRNOTAVAIL;
1215			splx(s);
1216			break;
1217		}
1218		/*
1219		 * See if the membership already exists or if all the
1220		 * membership slots are full.
1221		 */
1222		for (i = 0; i < imo->imo_num_memberships; ++i) {
1223			if (imo->imo_membership[i]->inm_ifp == ifp &&
1224			    imo->imo_membership[i]->inm_addr.s_addr
1225						== mreq->imr_multiaddr.s_addr)
1226				break;
1227		}
1228		if (i < imo->imo_num_memberships) {
1229			error = EADDRINUSE;
1230			splx(s);
1231			break;
1232		}
1233		if (i == IP_MAX_MEMBERSHIPS) {
1234			error = ETOOMANYREFS;
1235			splx(s);
1236			break;
1237		}
1238		/*
1239		 * Everything looks good; add a new record to the multicast
1240		 * address list for the given interface.
1241		 */
1242		if ((imo->imo_membership[i] =
1243		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1244			error = ENOBUFS;
1245			splx(s);
1246			break;
1247		}
1248		++imo->imo_num_memberships;
1249		splx(s);
1250		break;
1251
1252	case IP_DROP_MEMBERSHIP:
1253		/*
1254		 * Drop a multicast group membership.
1255		 * Group must be a valid IP multicast address.
1256		 */
1257		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1258			error = EINVAL;
1259			break;
1260		}
1261		mreq = mtod(m, struct ip_mreq *);
1262		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1263			error = EINVAL;
1264			break;
1265		}
1266
1267		s = splimp();
1268		/*
1269		 * If an interface address was specified, get a pointer
1270		 * to its ifnet structure.
1271		 */
1272		if (mreq->imr_interface.s_addr == INADDR_ANY)
1273			ifp = NULL;
1274		else {
1275			INADDR_TO_IFP(mreq->imr_interface, ifp);
1276			if (ifp == NULL) {
1277				error = EADDRNOTAVAIL;
1278				splx(s);
1279				break;
1280			}
1281		}
1282		/*
1283		 * Find the membership in the membership array.
1284		 */
1285		for (i = 0; i < imo->imo_num_memberships; ++i) {
1286			if ((ifp == NULL ||
1287			     imo->imo_membership[i]->inm_ifp == ifp) &&
1288			     imo->imo_membership[i]->inm_addr.s_addr ==
1289			     mreq->imr_multiaddr.s_addr)
1290				break;
1291		}
1292		if (i == imo->imo_num_memberships) {
1293			error = EADDRNOTAVAIL;
1294			splx(s);
1295			break;
1296		}
1297		/*
1298		 * Give up the multicast address record to which the
1299		 * membership points.
1300		 */
1301		in_delmulti(imo->imo_membership[i]);
1302		/*
1303		 * Remove the gap in the membership array.
1304		 */
1305		for (++i; i < imo->imo_num_memberships; ++i)
1306			imo->imo_membership[i-1] = imo->imo_membership[i];
1307		--imo->imo_num_memberships;
1308		splx(s);
1309		break;
1310
1311	default:
1312		error = EOPNOTSUPP;
1313		break;
1314	}
1315
1316	/*
1317	 * If all options have default values, no need to keep the mbuf.
1318	 */
1319	if (imo->imo_multicast_ifp == NULL &&
1320	    imo->imo_multicast_vif == -1 &&
1321	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1322	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1323	    imo->imo_num_memberships == 0) {
1324		free(*imop, M_IPMOPTS);
1325		*imop = NULL;
1326	}
1327
1328	return (error);
1329}
1330
1331/*
1332 * Return the IP multicast options in response to user getsockopt().
1333 */
1334static int
1335ip_getmoptions(optname, imo, mp)
1336	int optname;
1337	register struct ip_moptions *imo;
1338	register struct mbuf **mp;
1339{
1340	u_char *ttl;
1341	u_char *loop;
1342	struct in_addr *addr;
1343	struct in_ifaddr *ia;
1344
1345	*mp = m_get(M_WAIT, MT_SOOPTS);
1346
1347	switch (optname) {
1348
1349	case IP_MULTICAST_VIF:
1350		if (imo != NULL)
1351			*(mtod(*mp, int *)) = imo->imo_multicast_vif;
1352		else
1353			*(mtod(*mp, int *)) = -1;
1354		(*mp)->m_len = sizeof(int);
1355		return(0);
1356
1357	case IP_MULTICAST_IF:
1358		addr = mtod(*mp, struct in_addr *);
1359		(*mp)->m_len = sizeof(struct in_addr);
1360		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1361			addr->s_addr = INADDR_ANY;
1362		else {
1363			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1364			addr->s_addr = (ia == NULL) ? INADDR_ANY
1365					: IA_SIN(ia)->sin_addr.s_addr;
1366		}
1367		return (0);
1368
1369	case IP_MULTICAST_TTL:
1370		ttl = mtod(*mp, u_char *);
1371		(*mp)->m_len = 1;
1372		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1373				     : imo->imo_multicast_ttl;
1374		return (0);
1375
1376	case IP_MULTICAST_LOOP:
1377		loop = mtod(*mp, u_char *);
1378		(*mp)->m_len = 1;
1379		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1380				      : imo->imo_multicast_loop;
1381		return (0);
1382
1383	default:
1384		return (EOPNOTSUPP);
1385	}
1386}
1387
1388/*
1389 * Discard the IP multicast options.
1390 */
1391void
1392ip_freemoptions(imo)
1393	register struct ip_moptions *imo;
1394{
1395	register int i;
1396
1397	if (imo != NULL) {
1398		for (i = 0; i < imo->imo_num_memberships; ++i)
1399			in_delmulti(imo->imo_membership[i]);
1400		free(imo, M_IPMOPTS);
1401	}
1402}
1403
1404/*
1405 * Routine called from ip_output() to loop back a copy of an IP multicast
1406 * packet to the input queue of a specified interface.  Note that this
1407 * calls the output routine of the loopback "driver", but with an interface
1408 * pointer that might NOT be a loopback interface -- evil, but easier than
1409 * replicating that code here.
1410 */
1411static void
1412ip_mloopback(ifp, m, dst, hlen)
1413	struct ifnet *ifp;
1414	register struct mbuf *m;
1415	register struct sockaddr_in *dst;
1416	int hlen;
1417{
1418	register struct ip *ip;
1419	struct mbuf *copym;
1420
1421	copym = m_copy(m, 0, M_COPYALL);
1422	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1423		copym = m_pullup(copym, hlen);
1424	if (copym != NULL) {
1425		/*
1426		 * We don't bother to fragment if the IP length is greater
1427		 * than the interface's MTU.  Can this possibly matter?
1428		 */
1429		ip = mtod(copym, struct ip *);
1430		ip->ip_len = htons((u_short)ip->ip_len);
1431		ip->ip_off = htons((u_short)ip->ip_off);
1432		ip->ip_sum = 0;
1433		if (ip->ip_vhl == IP_VHL_BORING) {
1434			ip->ip_sum = in_cksum_hdr(ip);
1435		} else {
1436			ip->ip_sum = in_cksum(copym, hlen);
1437		}
1438		/*
1439		 * NB:
1440		 * It's not clear whether there are any lingering
1441		 * reentrancy problems in other areas which might
1442		 * be exposed by using ip_input directly (in
1443		 * particular, everything which modifies the packet
1444		 * in-place).  Yet another option is using the
1445		 * protosw directly to deliver the looped back
1446		 * packet.  For the moment, we'll err on the side
1447		 * of safety by using if_simloop().
1448		 */
1449#if 1 /* XXX */
1450		if (dst->sin_family != AF_INET) {
1451			printf("ip_mloopback: bad address family %d\n",
1452						dst->sin_family);
1453			dst->sin_family = AF_INET;
1454		}
1455#endif
1456
1457#ifdef notdef
1458		copym->m_pkthdr.rcvif = ifp;
1459		ip_input(copym);
1460#else
1461		if_simloop(ifp, copym, (struct sockaddr *)dst, 0);
1462#endif
1463	}
1464}
1465