ip_output.c revision 37624
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.78 1998/07/06 05:04:33 julian 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;
394		old = dst;
395#endif
396#ifdef IPDIVERT
397		ip_divert_port = (*ip_fw_chk_ptr)(&ip,
398		    hlen, ifp, &ip_divert_cookie, &m, &dst);
399		if (ip_divert_port) {		/* Divert packet */
400			(*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, 0);
401			goto done;
402		}
403#else	/* !IPDIVERT */
404		u_int16_t 	dummy = 0;
405		/* If ipfw says divert, we have to just drop packet */
406		if ((*ip_fw_chk_ptr)(&ip, hlen, ifp, &dummy, &m, &dst)) {
407			m_freem(m);
408			goto done;
409		}
410#endif	/* !IPDIVERT */
411		if (!m) {
412			error = EACCES;
413			goto done;
414		}
415#ifdef IPFIREWALL_FORWARD
416		/* Here we check dst to make sure it's directly reachable on the
417		 * interface we previously thought it was.
418		 * If it isn't (which may be likely in some situations) we have
419		 * to re-route it (ie, find a route for the next-hop and the
420		 * associated interface) and set them here. This is nested
421		 * forwarding which in most cases is undesirable, except where
422		 * such control is nigh impossible. So we do it here.
423		 * And I'm babbling.
424		 */
425		if (old != dst) {
426			struct in_ifaddr *ia;
427
428			/* It's changed... */
429			/* There must be a better way to do this next line... */
430			static struct route sro_fwd, *ro_fwd = &sro_fwd;
431#ifdef IPFIREWALL_FORWARD_DEBUG
432			printf("IPFIREWALL_FORWARD: New dst ip: ");
433			print_ip(dst->sin_addr);
434			printf("\n");
435#endif
436			/*
437			 * We need to figure out if we have been forwarded
438			 * to a local socket. If so then we should somehow
439			 * "loop back" to ip_input, and get directed to the
440			 * PCB as if we had received this packet. This is
441			 * because it may be dificult to identify the packets
442			 * you want to forward until they are being output
443			 * and have selected an interface. (e.g. locally
444			 * initiated packets) If we used the loopback inteface,
445			 * we would not be able to control what happens
446			 * as the packet runs through ip_input() as
447			 * it is done through a ISR.
448			 */
449			for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
450					ia = TAILQ_NEXT(ia, ia_link)) {
451				/*
452				 * If the addr to forward to is one
453				 * of ours, we pretend to
454				 * be the destination for this packet.
455				 */
456				if (IA_SIN(ia)->sin_addr.s_addr ==
457						 dst->sin_addr.s_addr)
458					break;
459			}
460			if (ia) {
461				/* tell ip_input "dont filter" */
462				ip_fw_fwd_addr = dst;
463				if (m->m_pkthdr.rcvif == NULL)
464					m->m_pkthdr.rcvif = ifunit("lo0");
465				ip->ip_len = htons((u_short)ip->ip_len);
466				ip->ip_off = htons((u_short)ip->ip_off);
467				ip->ip_sum = 0;
468				if (ip->ip_vhl == IP_VHL_BORING) {
469					ip->ip_sum = in_cksum_hdr(ip);
470				} else {
471					ip->ip_sum = in_cksum(m, hlen);
472				}
473				ip_input(m);
474				goto done;
475			}
476			/* Some of the logic for this was
477			 * nicked from above.
478			 *
479			 * This rewrites the cached route in a local PCB.
480			 * Is this what we want to do?
481			 */
482			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
483
484			ro_fwd->ro_rt = 0;
485			rtalloc_ign(ro_fwd, RTF_PRCLONING);
486
487			if (ro_fwd->ro_rt == 0) {
488				ipstat.ips_noroute++;
489				error = EHOSTUNREACH;
490				goto bad;
491			}
492
493			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
494			ifp = ro_fwd->ro_rt->rt_ifp;
495			ro_fwd->ro_rt->rt_use++;
496			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
497				dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
498			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
499				isbroadcast =
500				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
501			else
502				isbroadcast = in_broadcast(dst->sin_addr, ifp);
503			RTFREE(ro->ro_rt);
504			ro->ro_rt = ro_fwd->ro_rt;
505			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
506
507			/*
508			 * If we added a default src ip earlier,
509			 * which would have been gotten from the-then
510			 * interface, do it again, from the new one.
511			 */
512			if (fwd_rewrite_src)
513				ip->ip_src = IA_SIN(ia)->sin_addr;
514		}
515#endif /* IPFIREWALL_FORWARD */
516	}
517#endif /* COMPAT_IPFW */
518
519
520	/*
521	 * If small enough for interface, can just send directly.
522	 */
523	if ((u_short)ip->ip_len <= ifp->if_mtu) {
524		ip->ip_len = htons((u_short)ip->ip_len);
525		ip->ip_off = htons((u_short)ip->ip_off);
526		ip->ip_sum = 0;
527		if (ip->ip_vhl == IP_VHL_BORING) {
528			ip->ip_sum = in_cksum_hdr(ip);
529		} else {
530			ip->ip_sum = in_cksum(m, hlen);
531		}
532		error = (*ifp->if_output)(ifp, m,
533				(struct sockaddr *)dst, ro->ro_rt);
534		goto done;
535	}
536	/*
537	 * Too large for interface; fragment if possible.
538	 * Must be able to put at least 8 bytes per fragment.
539	 */
540	if (ip->ip_off & IP_DF) {
541		error = EMSGSIZE;
542		/*
543		 * This case can happen if the user changed the MTU
544		 * of an interface after enabling IP on it.  Because
545		 * most netifs don't keep track of routes pointing to
546		 * them, there is no way for one to update all its
547		 * routes when the MTU is changed.
548		 */
549		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
550		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
551		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
552			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
553		}
554		ipstat.ips_cantfrag++;
555		goto bad;
556	}
557	len = (ifp->if_mtu - hlen) &~ 7;
558	if (len < 8) {
559		error = EMSGSIZE;
560		goto bad;
561	}
562
563    {
564	int mhlen, firstlen = len;
565	struct mbuf **mnext = &m->m_nextpkt;
566
567	/*
568	 * Loop through length of segment after first fragment,
569	 * make new header and copy data of each part and link onto chain.
570	 */
571	m0 = m;
572	mhlen = sizeof (struct ip);
573	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
574		MGETHDR(m, M_DONTWAIT, MT_HEADER);
575		if (m == 0) {
576			error = ENOBUFS;
577			ipstat.ips_odropped++;
578			goto sendorfree;
579		}
580		m->m_data += max_linkhdr;
581		mhip = mtod(m, struct ip *);
582		*mhip = *ip;
583		if (hlen > sizeof (struct ip)) {
584			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
585			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
586		}
587		m->m_len = mhlen;
588		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
589		if (ip->ip_off & IP_MF)
590			mhip->ip_off |= IP_MF;
591		if (off + len >= (u_short)ip->ip_len)
592			len = (u_short)ip->ip_len - off;
593		else
594			mhip->ip_off |= IP_MF;
595		mhip->ip_len = htons((u_short)(len + mhlen));
596		m->m_next = m_copy(m0, off, len);
597		if (m->m_next == 0) {
598			(void) m_free(m);
599			error = ENOBUFS;	/* ??? */
600			ipstat.ips_odropped++;
601			goto sendorfree;
602		}
603		m->m_pkthdr.len = mhlen + len;
604		m->m_pkthdr.rcvif = (struct ifnet *)0;
605		mhip->ip_off = htons((u_short)mhip->ip_off);
606		mhip->ip_sum = 0;
607		if (mhip->ip_vhl == IP_VHL_BORING) {
608			mhip->ip_sum = in_cksum_hdr(mhip);
609		} else {
610			mhip->ip_sum = in_cksum(m, mhlen);
611		}
612		*mnext = m;
613		mnext = &m->m_nextpkt;
614		ipstat.ips_ofragments++;
615	}
616	/*
617	 * Update first fragment by trimming what's been copied out
618	 * and updating header, then send each fragment (in order).
619	 */
620	m = m0;
621	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
622	m->m_pkthdr.len = hlen + firstlen;
623	ip->ip_len = htons((u_short)m->m_pkthdr.len);
624	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
625	ip->ip_sum = 0;
626	if (ip->ip_vhl == IP_VHL_BORING) {
627		ip->ip_sum = in_cksum_hdr(ip);
628	} else {
629		ip->ip_sum = in_cksum(m, hlen);
630	}
631sendorfree:
632	for (m = m0; m; m = m0) {
633		m0 = m->m_nextpkt;
634		m->m_nextpkt = 0;
635		if (error == 0)
636			error = (*ifp->if_output)(ifp, m,
637			    (struct sockaddr *)dst, ro->ro_rt);
638		else
639			m_freem(m);
640	}
641
642	if (error == 0)
643		ipstat.ips_fragmented++;
644    }
645done:
646	return (error);
647bad:
648	m_freem(m0);
649	goto done;
650}
651
652/*
653 * Insert IP options into preformed packet.
654 * Adjust IP destination as required for IP source routing,
655 * as indicated by a non-zero in_addr at the start of the options.
656 *
657 * XXX This routine assumes that the packet has no options in place.
658 */
659static struct mbuf *
660ip_insertoptions(m, opt, phlen)
661	register struct mbuf *m;
662	struct mbuf *opt;
663	int *phlen;
664{
665	register struct ipoption *p = mtod(opt, struct ipoption *);
666	struct mbuf *n;
667	register struct ip *ip = mtod(m, struct ip *);
668	unsigned optlen;
669
670	optlen = opt->m_len - sizeof(p->ipopt_dst);
671	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
672		return (m);		/* XXX should fail */
673	if (p->ipopt_dst.s_addr)
674		ip->ip_dst = p->ipopt_dst;
675	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
676		MGETHDR(n, M_DONTWAIT, MT_HEADER);
677		if (n == 0)
678			return (m);
679		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
680		m->m_len -= sizeof(struct ip);
681		m->m_data += sizeof(struct ip);
682		n->m_next = m;
683		m = n;
684		m->m_len = optlen + sizeof(struct ip);
685		m->m_data += max_linkhdr;
686		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
687	} else {
688		m->m_data -= optlen;
689		m->m_len += optlen;
690		m->m_pkthdr.len += optlen;
691		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
692	}
693	ip = mtod(m, struct ip *);
694	bcopy(p->ipopt_list, ip + 1, optlen);
695	*phlen = sizeof(struct ip) + optlen;
696	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
697	ip->ip_len += optlen;
698	return (m);
699}
700
701/*
702 * Copy options from ip to jp,
703 * omitting those not copied during fragmentation.
704 */
705#if !defined(IPFILTER) && !defined(IPFILTER_LKM)
706static
707#endif
708int
709ip_optcopy(ip, jp)
710	struct ip *ip, *jp;
711{
712	register u_char *cp, *dp;
713	int opt, optlen, cnt;
714
715	cp = (u_char *)(ip + 1);
716	dp = (u_char *)(jp + 1);
717	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
718	for (; cnt > 0; cnt -= optlen, cp += optlen) {
719		opt = cp[0];
720		if (opt == IPOPT_EOL)
721			break;
722		if (opt == IPOPT_NOP) {
723			/* Preserve for IP mcast tunnel's LSRR alignment. */
724			*dp++ = IPOPT_NOP;
725			optlen = 1;
726			continue;
727		} else
728			optlen = cp[IPOPT_OLEN];
729		/* bogus lengths should have been caught by ip_dooptions */
730		if (optlen > cnt)
731			optlen = cnt;
732		if (IPOPT_COPIED(opt)) {
733			bcopy(cp, dp, optlen);
734			dp += optlen;
735		}
736	}
737	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
738		*dp++ = IPOPT_EOL;
739	return (optlen);
740}
741
742/*
743 * IP socket option processing.
744 */
745int
746ip_ctloutput(op, so, level, optname, mp, p)
747	int op;
748	struct socket *so;
749	int level, optname;
750	struct mbuf **mp;
751	struct proc *p;
752{
753	register struct inpcb *inp = sotoinpcb(so);
754	register struct mbuf *m = *mp;
755	register int optval = 0;
756	int error = 0;
757
758	if (level != IPPROTO_IP) {
759		error = EINVAL;
760		if (op == PRCO_SETOPT && *mp)
761			(void) m_free(*mp);
762	} else switch (op) {
763
764	case PRCO_SETOPT:
765		switch (optname) {
766		case IP_OPTIONS:
767#ifdef notyet
768		case IP_RETOPTS:
769			return (ip_pcbopts(optname, &inp->inp_options, m));
770#else
771			return (ip_pcbopts(&inp->inp_options, m));
772#endif
773
774		case IP_TOS:
775		case IP_TTL:
776		case IP_RECVOPTS:
777		case IP_RECVRETOPTS:
778		case IP_RECVDSTADDR:
779		case IP_RECVIF:
780			if (m == 0 || m->m_len != sizeof(int))
781				error = EINVAL;
782			else {
783				optval = *mtod(m, int *);
784				switch (optname) {
785
786				case IP_TOS:
787					inp->inp_ip_tos = optval;
788					break;
789
790				case IP_TTL:
791					inp->inp_ip_ttl = optval;
792					break;
793#define	OPTSET(bit) \
794	if (optval) \
795		inp->inp_flags |= bit; \
796	else \
797		inp->inp_flags &= ~bit;
798
799				case IP_RECVOPTS:
800					OPTSET(INP_RECVOPTS);
801					break;
802
803				case IP_RECVRETOPTS:
804					OPTSET(INP_RECVRETOPTS);
805					break;
806
807				case IP_RECVDSTADDR:
808					OPTSET(INP_RECVDSTADDR);
809					break;
810
811				case IP_RECVIF:
812					OPTSET(INP_RECVIF);
813					break;
814				}
815			}
816			break;
817#undef OPTSET
818
819		case IP_MULTICAST_IF:
820		case IP_MULTICAST_VIF:
821		case IP_MULTICAST_TTL:
822		case IP_MULTICAST_LOOP:
823		case IP_ADD_MEMBERSHIP:
824		case IP_DROP_MEMBERSHIP:
825			error = ip_setmoptions(optname, &inp->inp_moptions, m);
826			break;
827
828		case IP_PORTRANGE:
829			if (m == 0 || m->m_len != sizeof(int))
830				error = EINVAL;
831			else {
832				optval = *mtod(m, int *);
833
834				switch (optval) {
835
836				case IP_PORTRANGE_DEFAULT:
837					inp->inp_flags &= ~(INP_LOWPORT);
838					inp->inp_flags &= ~(INP_HIGHPORT);
839					break;
840
841				case IP_PORTRANGE_HIGH:
842					inp->inp_flags &= ~(INP_LOWPORT);
843					inp->inp_flags |= INP_HIGHPORT;
844					break;
845
846				case IP_PORTRANGE_LOW:
847					inp->inp_flags &= ~(INP_HIGHPORT);
848					inp->inp_flags |= INP_LOWPORT;
849					break;
850
851				default:
852					error = EINVAL;
853					break;
854				}
855			}
856			break;
857
858		default:
859			error = ENOPROTOOPT;
860			break;
861		}
862		if (m)
863			(void)m_free(m);
864		break;
865
866	case PRCO_GETOPT:
867		switch (optname) {
868		case IP_OPTIONS:
869		case IP_RETOPTS:
870			*mp = m = m_get(M_WAIT, MT_SOOPTS);
871			if (inp->inp_options) {
872				m->m_len = inp->inp_options->m_len;
873				bcopy(mtod(inp->inp_options, void *),
874				    mtod(m, void *), m->m_len);
875			} else
876				m->m_len = 0;
877			break;
878
879		case IP_TOS:
880		case IP_TTL:
881		case IP_RECVOPTS:
882		case IP_RECVRETOPTS:
883		case IP_RECVDSTADDR:
884		case IP_RECVIF:
885			*mp = m = m_get(M_WAIT, MT_SOOPTS);
886			m->m_len = sizeof(int);
887			switch (optname) {
888
889			case IP_TOS:
890				optval = inp->inp_ip_tos;
891				break;
892
893			case IP_TTL:
894				optval = inp->inp_ip_ttl;
895				break;
896
897#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
898
899			case IP_RECVOPTS:
900				optval = OPTBIT(INP_RECVOPTS);
901				break;
902
903			case IP_RECVRETOPTS:
904				optval = OPTBIT(INP_RECVRETOPTS);
905				break;
906
907			case IP_RECVDSTADDR:
908				optval = OPTBIT(INP_RECVDSTADDR);
909				break;
910
911			case IP_RECVIF:
912				optval = OPTBIT(INP_RECVIF);
913				break;
914			}
915			*mtod(m, int *) = optval;
916			break;
917
918		case IP_MULTICAST_IF:
919		case IP_MULTICAST_VIF:
920		case IP_MULTICAST_TTL:
921		case IP_MULTICAST_LOOP:
922		case IP_ADD_MEMBERSHIP:
923		case IP_DROP_MEMBERSHIP:
924			error = ip_getmoptions(optname, inp->inp_moptions, mp);
925			break;
926
927		case IP_PORTRANGE:
928			*mp = m = m_get(M_WAIT, MT_SOOPTS);
929			m->m_len = sizeof(int);
930
931			if (inp->inp_flags & INP_HIGHPORT)
932				optval = IP_PORTRANGE_HIGH;
933			else if (inp->inp_flags & INP_LOWPORT)
934				optval = IP_PORTRANGE_LOW;
935			else
936				optval = 0;
937
938			*mtod(m, int *) = optval;
939			break;
940
941		default:
942			error = ENOPROTOOPT;
943			break;
944		}
945		break;
946	}
947	return (error);
948}
949
950/*
951 * Set up IP options in pcb for insertion in output packets.
952 * Store in mbuf with pointer in pcbopt, adding pseudo-option
953 * with destination address if source routed.
954 */
955static int
956#ifdef notyet
957ip_pcbopts(optname, pcbopt, m)
958	int optname;
959#else
960ip_pcbopts(pcbopt, m)
961#endif
962	struct mbuf **pcbopt;
963	register struct mbuf *m;
964{
965	register int cnt, optlen;
966	register u_char *cp;
967	u_char opt;
968
969	/* turn off any old options */
970	if (*pcbopt)
971		(void)m_free(*pcbopt);
972	*pcbopt = 0;
973	if (m == (struct mbuf *)0 || m->m_len == 0) {
974		/*
975		 * Only turning off any previous options.
976		 */
977		if (m)
978			(void)m_free(m);
979		return (0);
980	}
981
982#ifndef	vax
983	if (m->m_len % sizeof(int32_t))
984		goto bad;
985#endif
986	/*
987	 * IP first-hop destination address will be stored before
988	 * actual options; move other options back
989	 * and clear it when none present.
990	 */
991	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
992		goto bad;
993	cnt = m->m_len;
994	m->m_len += sizeof(struct in_addr);
995	cp = mtod(m, u_char *) + sizeof(struct in_addr);
996	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
997	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
998
999	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1000		opt = cp[IPOPT_OPTVAL];
1001		if (opt == IPOPT_EOL)
1002			break;
1003		if (opt == IPOPT_NOP)
1004			optlen = 1;
1005		else {
1006			optlen = cp[IPOPT_OLEN];
1007			if (optlen <= IPOPT_OLEN || optlen > cnt)
1008				goto bad;
1009		}
1010		switch (opt) {
1011
1012		default:
1013			break;
1014
1015		case IPOPT_LSRR:
1016		case IPOPT_SSRR:
1017			/*
1018			 * user process specifies route as:
1019			 *	->A->B->C->D
1020			 * D must be our final destination (but we can't
1021			 * check that since we may not have connected yet).
1022			 * A is first hop destination, which doesn't appear in
1023			 * actual IP option, but is stored before the options.
1024			 */
1025			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1026				goto bad;
1027			m->m_len -= sizeof(struct in_addr);
1028			cnt -= sizeof(struct in_addr);
1029			optlen -= sizeof(struct in_addr);
1030			cp[IPOPT_OLEN] = optlen;
1031			/*
1032			 * Move first hop before start of options.
1033			 */
1034			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1035			    sizeof(struct in_addr));
1036			/*
1037			 * Then copy rest of options back
1038			 * to close up the deleted entry.
1039			 */
1040			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1041			    sizeof(struct in_addr)),
1042			    (caddr_t)&cp[IPOPT_OFFSET+1],
1043			    (unsigned)cnt + sizeof(struct in_addr));
1044			break;
1045		}
1046	}
1047	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1048		goto bad;
1049	*pcbopt = m;
1050	return (0);
1051
1052bad:
1053	(void)m_free(m);
1054	return (EINVAL);
1055}
1056
1057/*
1058 * Set the IP multicast options in response to user setsockopt().
1059 */
1060static int
1061ip_setmoptions(optname, imop, m)
1062	int optname;
1063	struct ip_moptions **imop;
1064	struct mbuf *m;
1065{
1066	register int error = 0;
1067	u_char loop;
1068	register int i;
1069	struct in_addr addr;
1070	register struct ip_mreq *mreq;
1071	register struct ifnet *ifp;
1072	register struct ip_moptions *imo = *imop;
1073	struct route ro;
1074	register struct sockaddr_in *dst;
1075	int s;
1076
1077	if (imo == NULL) {
1078		/*
1079		 * No multicast option buffer attached to the pcb;
1080		 * allocate one and initialize to default values.
1081		 */
1082		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1083		    M_WAITOK);
1084
1085		if (imo == NULL)
1086			return (ENOBUFS);
1087		*imop = imo;
1088		imo->imo_multicast_ifp = NULL;
1089		imo->imo_multicast_vif = -1;
1090		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1091		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1092		imo->imo_num_memberships = 0;
1093	}
1094
1095	switch (optname) {
1096	/* store an index number for the vif you wanna use in the send */
1097	case IP_MULTICAST_VIF:
1098		if (!legal_vif_num) {
1099			error = EOPNOTSUPP;
1100			break;
1101		}
1102		if (m == NULL || m->m_len != sizeof(int)) {
1103			error = EINVAL;
1104			break;
1105		}
1106		i = *(mtod(m, int *));
1107		if (!legal_vif_num(i) && (i != -1)) {
1108			error = EINVAL;
1109			break;
1110		}
1111		imo->imo_multicast_vif = i;
1112		break;
1113
1114	case IP_MULTICAST_IF:
1115		/*
1116		 * Select the interface for outgoing multicast packets.
1117		 */
1118		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
1119			error = EINVAL;
1120			break;
1121		}
1122		addr = *(mtod(m, struct in_addr *));
1123		/*
1124		 * INADDR_ANY is used to remove a previous selection.
1125		 * When no interface is selected, a default one is
1126		 * chosen every time a multicast packet is sent.
1127		 */
1128		if (addr.s_addr == INADDR_ANY) {
1129			imo->imo_multicast_ifp = NULL;
1130			break;
1131		}
1132		/*
1133		 * The selected interface is identified by its local
1134		 * IP address.  Find the interface and confirm that
1135		 * it supports multicasting.
1136		 */
1137		s = splimp();
1138		INADDR_TO_IFP(addr, ifp);
1139		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1140			splx(s);
1141			error = EADDRNOTAVAIL;
1142			break;
1143		}
1144		imo->imo_multicast_ifp = ifp;
1145		splx(s);
1146		break;
1147
1148	case IP_MULTICAST_TTL:
1149		/*
1150		 * Set the IP time-to-live for outgoing multicast packets.
1151		 */
1152		if (m == NULL || m->m_len != 1) {
1153			error = EINVAL;
1154			break;
1155		}
1156		imo->imo_multicast_ttl = *(mtod(m, u_char *));
1157		break;
1158
1159	case IP_MULTICAST_LOOP:
1160		/*
1161		 * Set the loopback flag for outgoing multicast packets.
1162		 * Must be zero or one.
1163		 */
1164		if (m == NULL || m->m_len != 1 ||
1165		   (loop = *(mtod(m, u_char *))) > 1) {
1166			error = EINVAL;
1167			break;
1168		}
1169		imo->imo_multicast_loop = loop;
1170		break;
1171
1172	case IP_ADD_MEMBERSHIP:
1173		/*
1174		 * Add a multicast group membership.
1175		 * Group must be a valid IP multicast address.
1176		 */
1177		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1178			error = EINVAL;
1179			break;
1180		}
1181		mreq = mtod(m, struct ip_mreq *);
1182		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1183			error = EINVAL;
1184			break;
1185		}
1186		s = splimp();
1187		/*
1188		 * If no interface address was provided, use the interface of
1189		 * the route to the given multicast address.
1190		 */
1191		if (mreq->imr_interface.s_addr == INADDR_ANY) {
1192			bzero((caddr_t)&ro, sizeof(ro));
1193			dst = (struct sockaddr_in *)&ro.ro_dst;
1194			dst->sin_len = sizeof(*dst);
1195			dst->sin_family = AF_INET;
1196			dst->sin_addr = mreq->imr_multiaddr;
1197			rtalloc(&ro);
1198			if (ro.ro_rt == NULL) {
1199				error = EADDRNOTAVAIL;
1200				splx(s);
1201				break;
1202			}
1203			ifp = ro.ro_rt->rt_ifp;
1204			rtfree(ro.ro_rt);
1205		}
1206		else {
1207			INADDR_TO_IFP(mreq->imr_interface, ifp);
1208		}
1209
1210		/*
1211		 * See if we found an interface, and confirm that it
1212		 * supports multicast.
1213		 */
1214		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1215			error = EADDRNOTAVAIL;
1216			splx(s);
1217			break;
1218		}
1219		/*
1220		 * See if the membership already exists or if all the
1221		 * membership slots are full.
1222		 */
1223		for (i = 0; i < imo->imo_num_memberships; ++i) {
1224			if (imo->imo_membership[i]->inm_ifp == ifp &&
1225			    imo->imo_membership[i]->inm_addr.s_addr
1226						== mreq->imr_multiaddr.s_addr)
1227				break;
1228		}
1229		if (i < imo->imo_num_memberships) {
1230			error = EADDRINUSE;
1231			splx(s);
1232			break;
1233		}
1234		if (i == IP_MAX_MEMBERSHIPS) {
1235			error = ETOOMANYREFS;
1236			splx(s);
1237			break;
1238		}
1239		/*
1240		 * Everything looks good; add a new record to the multicast
1241		 * address list for the given interface.
1242		 */
1243		if ((imo->imo_membership[i] =
1244		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1245			error = ENOBUFS;
1246			splx(s);
1247			break;
1248		}
1249		++imo->imo_num_memberships;
1250		splx(s);
1251		break;
1252
1253	case IP_DROP_MEMBERSHIP:
1254		/*
1255		 * Drop a multicast group membership.
1256		 * Group must be a valid IP multicast address.
1257		 */
1258		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1259			error = EINVAL;
1260			break;
1261		}
1262		mreq = mtod(m, struct ip_mreq *);
1263		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1264			error = EINVAL;
1265			break;
1266		}
1267
1268		s = splimp();
1269		/*
1270		 * If an interface address was specified, get a pointer
1271		 * to its ifnet structure.
1272		 */
1273		if (mreq->imr_interface.s_addr == INADDR_ANY)
1274			ifp = NULL;
1275		else {
1276			INADDR_TO_IFP(mreq->imr_interface, ifp);
1277			if (ifp == NULL) {
1278				error = EADDRNOTAVAIL;
1279				splx(s);
1280				break;
1281			}
1282		}
1283		/*
1284		 * Find the membership in the membership array.
1285		 */
1286		for (i = 0; i < imo->imo_num_memberships; ++i) {
1287			if ((ifp == NULL ||
1288			     imo->imo_membership[i]->inm_ifp == ifp) &&
1289			     imo->imo_membership[i]->inm_addr.s_addr ==
1290			     mreq->imr_multiaddr.s_addr)
1291				break;
1292		}
1293		if (i == imo->imo_num_memberships) {
1294			error = EADDRNOTAVAIL;
1295			splx(s);
1296			break;
1297		}
1298		/*
1299		 * Give up the multicast address record to which the
1300		 * membership points.
1301		 */
1302		in_delmulti(imo->imo_membership[i]);
1303		/*
1304		 * Remove the gap in the membership array.
1305		 */
1306		for (++i; i < imo->imo_num_memberships; ++i)
1307			imo->imo_membership[i-1] = imo->imo_membership[i];
1308		--imo->imo_num_memberships;
1309		splx(s);
1310		break;
1311
1312	default:
1313		error = EOPNOTSUPP;
1314		break;
1315	}
1316
1317	/*
1318	 * If all options have default values, no need to keep the mbuf.
1319	 */
1320	if (imo->imo_multicast_ifp == NULL &&
1321	    imo->imo_multicast_vif == -1 &&
1322	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1323	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1324	    imo->imo_num_memberships == 0) {
1325		free(*imop, M_IPMOPTS);
1326		*imop = NULL;
1327	}
1328
1329	return (error);
1330}
1331
1332/*
1333 * Return the IP multicast options in response to user getsockopt().
1334 */
1335static int
1336ip_getmoptions(optname, imo, mp)
1337	int optname;
1338	register struct ip_moptions *imo;
1339	register struct mbuf **mp;
1340{
1341	u_char *ttl;
1342	u_char *loop;
1343	struct in_addr *addr;
1344	struct in_ifaddr *ia;
1345
1346	*mp = m_get(M_WAIT, MT_SOOPTS);
1347
1348	switch (optname) {
1349
1350	case IP_MULTICAST_VIF:
1351		if (imo != NULL)
1352			*(mtod(*mp, int *)) = imo->imo_multicast_vif;
1353		else
1354			*(mtod(*mp, int *)) = -1;
1355		(*mp)->m_len = sizeof(int);
1356		return(0);
1357
1358	case IP_MULTICAST_IF:
1359		addr = mtod(*mp, struct in_addr *);
1360		(*mp)->m_len = sizeof(struct in_addr);
1361		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1362			addr->s_addr = INADDR_ANY;
1363		else {
1364			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1365			addr->s_addr = (ia == NULL) ? INADDR_ANY
1366					: IA_SIN(ia)->sin_addr.s_addr;
1367		}
1368		return (0);
1369
1370	case IP_MULTICAST_TTL:
1371		ttl = mtod(*mp, u_char *);
1372		(*mp)->m_len = 1;
1373		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1374				     : imo->imo_multicast_ttl;
1375		return (0);
1376
1377	case IP_MULTICAST_LOOP:
1378		loop = mtod(*mp, u_char *);
1379		(*mp)->m_len = 1;
1380		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1381				      : imo->imo_multicast_loop;
1382		return (0);
1383
1384	default:
1385		return (EOPNOTSUPP);
1386	}
1387}
1388
1389/*
1390 * Discard the IP multicast options.
1391 */
1392void
1393ip_freemoptions(imo)
1394	register struct ip_moptions *imo;
1395{
1396	register int i;
1397
1398	if (imo != NULL) {
1399		for (i = 0; i < imo->imo_num_memberships; ++i)
1400			in_delmulti(imo->imo_membership[i]);
1401		free(imo, M_IPMOPTS);
1402	}
1403}
1404
1405/*
1406 * Routine called from ip_output() to loop back a copy of an IP multicast
1407 * packet to the input queue of a specified interface.  Note that this
1408 * calls the output routine of the loopback "driver", but with an interface
1409 * pointer that might NOT be a loopback interface -- evil, but easier than
1410 * replicating that code here.
1411 */
1412static void
1413ip_mloopback(ifp, m, dst, hlen)
1414	struct ifnet *ifp;
1415	register struct mbuf *m;
1416	register struct sockaddr_in *dst;
1417	int hlen;
1418{
1419	register struct ip *ip;
1420	struct mbuf *copym;
1421
1422	copym = m_copy(m, 0, M_COPYALL);
1423	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1424		copym = m_pullup(copym, hlen);
1425	if (copym != NULL) {
1426		/*
1427		 * We don't bother to fragment if the IP length is greater
1428		 * than the interface's MTU.  Can this possibly matter?
1429		 */
1430		ip = mtod(copym, struct ip *);
1431		ip->ip_len = htons((u_short)ip->ip_len);
1432		ip->ip_off = htons((u_short)ip->ip_off);
1433		ip->ip_sum = 0;
1434		if (ip->ip_vhl == IP_VHL_BORING) {
1435			ip->ip_sum = in_cksum_hdr(ip);
1436		} else {
1437			ip->ip_sum = in_cksum(copym, hlen);
1438		}
1439		/*
1440		 * NB:
1441		 * It's not clear whether there are any lingering
1442		 * reentrancy problems in other areas which might
1443		 * be exposed by using ip_input directly (in
1444		 * particular, everything which modifies the packet
1445		 * in-place).  Yet another option is using the
1446		 * protosw directly to deliver the looped back
1447		 * packet.  For the moment, we'll err on the side
1448		 * of safety by using if_simloop().
1449		 */
1450#if 1 /* XXX */
1451		if (dst->sin_family != AF_INET) {
1452			printf("ip_mloopback: bad address family %d\n",
1453						dst->sin_family);
1454			dst->sin_family = AF_INET;
1455		}
1456#endif
1457
1458#ifdef notdef
1459		copym->m_pkthdr.rcvif = ifp;
1460		ip_input(copym);
1461#else
1462		if_simloop(ifp, copym, (struct sockaddr *)dst, 0);
1463#endif
1464	}
1465}
1466