ip_output.c revision 2754
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.7 1994/09/09 22:05:02 wollman Exp $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/malloc.h>
40#include <sys/mbuf.h>
41#include <sys/errno.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45
46#include <net/if.h>
47#include <net/route.h>
48
49#include <netinet/in.h>
50#include <netinet/in_systm.h>
51#include <netinet/ip.h>
52#include <netinet/in_pcb.h>
53#include <netinet/in_var.h>
54#include <netinet/ip_var.h>
55
56#ifdef vax
57#include <machine/mtpr.h>
58#endif
59
60u_short ip_id;
61
62static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
63static void ip_mloopback
64	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
65
66/*
67 * IP output.  The packet in mbuf chain m contains a skeletal IP
68 * header (with len, off, ttl, proto, tos, src, dst).
69 * The mbuf chain containing the packet will be freed.
70 * The mbuf opt, if present, will not be freed.
71 */
72int
73ip_output(m0, opt, ro, flags, imo)
74	struct mbuf *m0;
75	struct mbuf *opt;
76	struct route *ro;
77	int flags;
78	struct ip_moptions *imo;
79{
80	register struct ip *ip, *mhip;
81	register struct ifnet *ifp;
82	register struct mbuf *m = m0;
83	register int hlen = sizeof (struct ip);
84	int len, off, error = 0;
85	struct route iproute;
86	struct sockaddr_in *dst;
87	struct in_ifaddr *ia;
88
89#ifdef	DIAGNOSTIC
90	if ((m->m_flags & M_PKTHDR) == 0)
91		panic("ip_output no HDR");
92#endif
93	if (opt) {
94		m = ip_insertoptions(m, opt, &len);
95		hlen = len;
96	}
97	ip = mtod(m, struct ip *);
98	/*
99	 * Fill in IP header.
100	 */
101	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
102		ip->ip_v = IPVERSION;
103		ip->ip_off &= IP_DF;
104		ip->ip_id = htons(ip_id++);
105		ip->ip_hl = hlen >> 2;
106		ipstat.ips_localout++;
107	} else {
108		hlen = ip->ip_hl << 2;
109	}
110	/*
111	 * Route packet.
112	 */
113	if (ro == 0) {
114		ro = &iproute;
115		bzero((caddr_t)ro, sizeof (*ro));
116	}
117	dst = (struct sockaddr_in *)&ro->ro_dst;
118	/*
119	 * If there is a cached route,
120	 * check that it is to the same destination
121	 * and is still up.  If not, free it and try again.
122	 */
123	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
124	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
125		RTFREE(ro->ro_rt);
126		ro->ro_rt = (struct rtentry *)0;
127	}
128	if (ro->ro_rt == 0) {
129		dst->sin_family = AF_INET;
130		dst->sin_len = sizeof(*dst);
131		dst->sin_addr = ip->ip_dst;
132	}
133	/*
134	 * If routing to interface only,
135	 * short circuit routing lookup.
136	 */
137#define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
138#define sintosa(sin)	((struct sockaddr *)(sin))
139	if (flags & IP_ROUTETOIF) {
140		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
141		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
142			ipstat.ips_noroute++;
143			error = ENETUNREACH;
144			goto bad;
145		}
146		ifp = ia->ia_ifp;
147		ip->ip_ttl = 1;
148	} else {
149		if (ro->ro_rt == 0)
150			rtalloc(ro);
151		if (ro->ro_rt == 0) {
152			ipstat.ips_noroute++;
153			error = EHOSTUNREACH;
154			goto bad;
155		}
156		ia = ifatoia(ro->ro_rt->rt_ifa);
157		ifp = ro->ro_rt->rt_ifp;
158		ro->ro_rt->rt_use++;
159		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
160			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
161	}
162	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
163		struct in_multi *inm;
164		extern struct ifnet loif;
165
166		m->m_flags |= M_MCAST;
167		/*
168		 * IP destination address is multicast.  Make sure "dst"
169		 * still points to the address in "ro".  (It may have been
170		 * changed to point to a gateway address, above.)
171		 */
172		dst = (struct sockaddr_in *)&ro->ro_dst;
173		/*
174		 * See if the caller provided any multicast options
175		 */
176		if (imo != NULL) {
177			ip->ip_ttl = imo->imo_multicast_ttl;
178			if (imo->imo_multicast_ifp != NULL)
179				ifp = imo->imo_multicast_ifp;
180		} else
181			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
182		/*
183		 * Confirm that the outgoing interface supports multicast.
184		 */
185		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
186			ipstat.ips_noroute++;
187			error = ENETUNREACH;
188			goto bad;
189		}
190		/*
191		 * If source address not specified yet, use address
192		 * of outgoing interface.
193		 */
194		if (ip->ip_src.s_addr == INADDR_ANY) {
195			register struct in_ifaddr *ia;
196
197			for (ia = in_ifaddr; ia; ia = ia->ia_next)
198				if (ia->ia_ifp == ifp) {
199					ip->ip_src = IA_SIN(ia)->sin_addr;
200					break;
201				}
202		}
203
204		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
205		if (inm != NULL &&
206		   (imo == NULL || imo->imo_multicast_loop)) {
207			/*
208			 * If we belong to the destination multicast group
209			 * on the outgoing interface, and the caller did not
210			 * forbid loopback, loop back a copy.
211			 */
212			ip_mloopback(ifp, m, dst);
213		}
214		else {
215			/*
216			 * If we are acting as a multicast router, perform
217			 * multicast forwarding as if the packet had just
218			 * arrived on the interface to which we are about
219			 * to send.  The multicast forwarding function
220			 * recursively calls this function, using the
221			 * IP_FORWARDING flag to prevent infinite recursion.
222			 *
223			 * Multicasts that are looped back by ip_mloopback(),
224			 * above, will be forwarded by the ip_input() routine,
225			 * if necessary.
226			 */
227			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
228				/*
229				 * Check if rsvp daemon is running. If not, don't
230				 * set ip_moptions. This ensures that the packet
231				 * is multicast and not just sent down one link
232				 * as prescribed by rsvpd.
233				 */
234				if (ip_rsvpd == NULL)
235				  imo = NULL;
236				if (ip_mforward(ip, ifp, m, imo) != 0) {
237					m_freem(m);
238					goto done;
239				}
240			}
241		}
242
243		/*
244		 * Multicasts with a time-to-live of zero may be looped-
245		 * back, above, but must not be transmitted on a network.
246		 * Also, multicasts addressed to the loopback interface
247		 * are not sent -- the above call to ip_mloopback() will
248		 * loop back a copy if this host actually belongs to the
249		 * destination group on the loopback interface.
250		 */
251		if (ip->ip_ttl == 0 || ifp == &loif) {
252			m_freem(m);
253			goto done;
254		}
255
256		goto sendit;
257	}
258#ifndef notdef
259	/*
260	 * If source address not specified yet, use address
261	 * of outgoing interface.
262	 */
263	if (ip->ip_src.s_addr == INADDR_ANY)
264		ip->ip_src = IA_SIN(ia)->sin_addr;
265#endif
266	/*
267	 * Verify that we have any chance at all of being able to queue
268	 *      the packet or packet fragments
269	 */
270	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
271		ifp->if_snd.ifq_maxlen) {
272			error = ENOBUFS;
273			goto bad;
274	}
275
276	/*
277	 * Look for broadcast address and
278	 * and verify user is allowed to send
279	 * such a packet.
280	 */
281	if (in_broadcast(dst->sin_addr, ifp)) {
282		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
283			error = EADDRNOTAVAIL;
284			goto bad;
285		}
286		if ((flags & IP_ALLOWBROADCAST) == 0) {
287			error = EACCES;
288			goto bad;
289		}
290		/* don't allow broadcast messages to be fragmented */
291		if ((u_short)ip->ip_len > ifp->if_mtu) {
292			error = EMSGSIZE;
293			goto bad;
294		}
295		m->m_flags |= M_BCAST;
296	} else
297		m->m_flags &= ~M_BCAST;
298
299sendit:
300	/*
301	 * If small enough for interface, can just send directly.
302	 */
303	if ((u_short)ip->ip_len <= ifp->if_mtu) {
304		ip->ip_len = htons((u_short)ip->ip_len);
305		ip->ip_off = htons((u_short)ip->ip_off);
306		ip->ip_sum = 0;
307		ip->ip_sum = in_cksum(m, hlen);
308		error = (*ifp->if_output)(ifp, m,
309				(struct sockaddr *)dst, ro->ro_rt);
310		goto done;
311	}
312	/*
313	 * Too large for interface; fragment if possible.
314	 * Must be able to put at least 8 bytes per fragment.
315	 */
316	if (ip->ip_off & IP_DF) {
317		error = EMSGSIZE;
318		ipstat.ips_cantfrag++;
319		goto bad;
320	}
321	len = (ifp->if_mtu - hlen) &~ 7;
322	if (len < 8) {
323		error = EMSGSIZE;
324		goto bad;
325	}
326
327    {
328	int mhlen, firstlen = len;
329	struct mbuf **mnext = &m->m_nextpkt;
330
331	/*
332	 * Loop through length of segment after first fragment,
333	 * make new header and copy data of each part and link onto chain.
334	 */
335	m0 = m;
336	mhlen = sizeof (struct ip);
337	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
338		MGETHDR(m, M_DONTWAIT, MT_HEADER);
339		if (m == 0) {
340			error = ENOBUFS;
341			ipstat.ips_odropped++;
342			goto sendorfree;
343		}
344		m->m_data += max_linkhdr;
345		mhip = mtod(m, struct ip *);
346		*mhip = *ip;
347		if (hlen > sizeof (struct ip)) {
348			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
349			mhip->ip_hl = mhlen >> 2;
350		}
351		m->m_len = mhlen;
352		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
353		if (ip->ip_off & IP_MF)
354			mhip->ip_off |= IP_MF;
355		if (off + len >= (u_short)ip->ip_len)
356			len = (u_short)ip->ip_len - off;
357		else
358			mhip->ip_off |= IP_MF;
359		mhip->ip_len = htons((u_short)(len + mhlen));
360		m->m_next = m_copy(m0, off, len);
361		if (m->m_next == 0) {
362			(void) m_free(m);
363			error = ENOBUFS;	/* ??? */
364			ipstat.ips_odropped++;
365			goto sendorfree;
366		}
367		m->m_pkthdr.len = mhlen + len;
368		m->m_pkthdr.rcvif = (struct ifnet *)0;
369		mhip->ip_off = htons((u_short)mhip->ip_off);
370		mhip->ip_sum = 0;
371		mhip->ip_sum = in_cksum(m, mhlen);
372		*mnext = m;
373		mnext = &m->m_nextpkt;
374		ipstat.ips_ofragments++;
375	}
376	/*
377	 * Update first fragment by trimming what's been copied out
378	 * and updating header, then send each fragment (in order).
379	 */
380	m = m0;
381	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
382	m->m_pkthdr.len = hlen + firstlen;
383	ip->ip_len = htons((u_short)m->m_pkthdr.len);
384	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
385	ip->ip_sum = 0;
386	ip->ip_sum = in_cksum(m, hlen);
387sendorfree:
388	for (m = m0; m; m = m0) {
389		m0 = m->m_nextpkt;
390		m->m_nextpkt = 0;
391		if (error == 0)
392			error = (*ifp->if_output)(ifp, m,
393			    (struct sockaddr *)dst, ro->ro_rt);
394		else
395			m_freem(m);
396	}
397
398	if (error == 0)
399		ipstat.ips_fragmented++;
400    }
401done:
402	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
403		RTFREE(ro->ro_rt);
404	return (error);
405bad:
406	m_freem(m0);
407	goto done;
408}
409
410/*
411 * Insert IP options into preformed packet.
412 * Adjust IP destination as required for IP source routing,
413 * as indicated by a non-zero in_addr at the start of the options.
414 */
415static struct mbuf *
416ip_insertoptions(m, opt, phlen)
417	register struct mbuf *m;
418	struct mbuf *opt;
419	int *phlen;
420{
421	register struct ipoption *p = mtod(opt, struct ipoption *);
422	struct mbuf *n;
423	register struct ip *ip = mtod(m, struct ip *);
424	unsigned optlen;
425
426	optlen = opt->m_len - sizeof(p->ipopt_dst);
427	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
428		return (m);		/* XXX should fail */
429	if (p->ipopt_dst.s_addr)
430		ip->ip_dst = p->ipopt_dst;
431	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
432		MGETHDR(n, M_DONTWAIT, MT_HEADER);
433		if (n == 0)
434			return (m);
435		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
436		m->m_len -= sizeof(struct ip);
437		m->m_data += sizeof(struct ip);
438		n->m_next = m;
439		m = n;
440		m->m_len = optlen + sizeof(struct ip);
441		m->m_data += max_linkhdr;
442		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
443	} else {
444		m->m_data -= optlen;
445		m->m_len += optlen;
446		m->m_pkthdr.len += optlen;
447		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
448	}
449	ip = mtod(m, struct ip *);
450	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
451	*phlen = sizeof(struct ip) + optlen;
452	ip->ip_len += optlen;
453	return (m);
454}
455
456/*
457 * Copy options from ip to jp,
458 * omitting those not copied during fragmentation.
459 */
460int
461ip_optcopy(ip, jp)
462	struct ip *ip, *jp;
463{
464	register u_char *cp, *dp;
465	int opt, optlen, cnt;
466
467	cp = (u_char *)(ip + 1);
468	dp = (u_char *)(jp + 1);
469	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
470	for (; cnt > 0; cnt -= optlen, cp += optlen) {
471		opt = cp[0];
472		if (opt == IPOPT_EOL)
473			break;
474		if (opt == IPOPT_NOP) {
475			/* Preserve for IP mcast tunnel's LSRR alignment. */
476			*dp++ = IPOPT_NOP;
477			optlen = 1;
478			continue;
479		} else
480			optlen = cp[IPOPT_OLEN];
481		/* bogus lengths should have been caught by ip_dooptions */
482		if (optlen > cnt)
483			optlen = cnt;
484		if (IPOPT_COPIED(opt)) {
485			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
486			dp += optlen;
487		}
488	}
489	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
490		*dp++ = IPOPT_EOL;
491	return (optlen);
492}
493
494/*
495 * IP socket option processing.
496 */
497int
498ip_ctloutput(op, so, level, optname, mp)
499	int op;
500	struct socket *so;
501	int level, optname;
502	struct mbuf **mp;
503{
504	register struct inpcb *inp = sotoinpcb(so);
505	register struct mbuf *m = *mp;
506	register int optval = 0;
507	int error = 0;
508
509	if (level != IPPROTO_IP) {
510		error = EINVAL;
511		if (op == PRCO_SETOPT && *mp)
512			(void) m_free(*mp);
513	} else switch (op) {
514
515	case PRCO_SETOPT:
516		switch (optname) {
517		case IP_OPTIONS:
518#ifdef notyet
519		case IP_RETOPTS:
520			return (ip_pcbopts(optname, &inp->inp_options, m));
521#else
522			return (ip_pcbopts(&inp->inp_options, m));
523#endif
524
525		case IP_TOS:
526		case IP_TTL:
527		case IP_RECVOPTS:
528		case IP_RECVRETOPTS:
529		case IP_RECVDSTADDR:
530			if (m->m_len != sizeof(int))
531				error = EINVAL;
532			else {
533				optval = *mtod(m, int *);
534				switch (optname) {
535
536				case IP_TOS:
537					inp->inp_ip.ip_tos = optval;
538					break;
539
540				case IP_TTL:
541					inp->inp_ip.ip_ttl = optval;
542					break;
543#define	OPTSET(bit) \
544	if (optval) \
545		inp->inp_flags |= bit; \
546	else \
547		inp->inp_flags &= ~bit;
548
549				case IP_RECVOPTS:
550					OPTSET(INP_RECVOPTS);
551					break;
552
553				case IP_RECVRETOPTS:
554					OPTSET(INP_RECVRETOPTS);
555					break;
556
557				case IP_RECVDSTADDR:
558					OPTSET(INP_RECVDSTADDR);
559					break;
560				}
561			}
562			break;
563#undef OPTSET
564
565		case IP_MULTICAST_IF:
566		case IP_MULTICAST_VIF:
567		case IP_MULTICAST_TTL:
568		case IP_MULTICAST_LOOP:
569		case IP_ADD_MEMBERSHIP:
570		case IP_DROP_MEMBERSHIP:
571			error = ip_setmoptions(optname, &inp->inp_moptions, m);
572			break;
573
574		default:
575			error = ENOPROTOOPT;
576			break;
577		}
578		if (m)
579			(void)m_free(m);
580		break;
581
582	case PRCO_GETOPT:
583		switch (optname) {
584		case IP_OPTIONS:
585		case IP_RETOPTS:
586			*mp = m = m_get(M_WAIT, MT_SOOPTS);
587			if (inp->inp_options) {
588				m->m_len = inp->inp_options->m_len;
589				bcopy(mtod(inp->inp_options, caddr_t),
590				    mtod(m, caddr_t), (unsigned)m->m_len);
591			} else
592				m->m_len = 0;
593			break;
594
595		case IP_TOS:
596		case IP_TTL:
597		case IP_RECVOPTS:
598		case IP_RECVRETOPTS:
599		case IP_RECVDSTADDR:
600			*mp = m = m_get(M_WAIT, MT_SOOPTS);
601			m->m_len = sizeof(int);
602			switch (optname) {
603
604			case IP_TOS:
605				optval = inp->inp_ip.ip_tos;
606				break;
607
608			case IP_TTL:
609				optval = inp->inp_ip.ip_ttl;
610				break;
611
612#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
613
614			case IP_RECVOPTS:
615				optval = OPTBIT(INP_RECVOPTS);
616				break;
617
618			case IP_RECVRETOPTS:
619				optval = OPTBIT(INP_RECVRETOPTS);
620				break;
621
622			case IP_RECVDSTADDR:
623				optval = OPTBIT(INP_RECVDSTADDR);
624				break;
625			}
626			*mtod(m, int *) = optval;
627			break;
628
629		case IP_MULTICAST_IF:
630		case IP_MULTICAST_VIF:
631		case IP_MULTICAST_TTL:
632		case IP_MULTICAST_LOOP:
633		case IP_ADD_MEMBERSHIP:
634		case IP_DROP_MEMBERSHIP:
635			error = ip_getmoptions(optname, inp->inp_moptions, mp);
636			break;
637
638		default:
639			error = ENOPROTOOPT;
640			break;
641		}
642		break;
643	}
644	return (error);
645}
646
647/*
648 * Set up IP options in pcb for insertion in output packets.
649 * Store in mbuf with pointer in pcbopt, adding pseudo-option
650 * with destination address if source routed.
651 */
652int
653#ifdef notyet
654ip_pcbopts(optname, pcbopt, m)
655	int optname;
656#else
657ip_pcbopts(pcbopt, m)
658#endif
659	struct mbuf **pcbopt;
660	register struct mbuf *m;
661{
662	register cnt, optlen;
663	register u_char *cp;
664	u_char opt;
665
666	/* turn off any old options */
667	if (*pcbopt)
668		(void)m_free(*pcbopt);
669	*pcbopt = 0;
670	if (m == (struct mbuf *)0 || m->m_len == 0) {
671		/*
672		 * Only turning off any previous options.
673		 */
674		if (m)
675			(void)m_free(m);
676		return (0);
677	}
678
679#ifndef	vax
680	if (m->m_len % sizeof(long))
681		goto bad;
682#endif
683	/*
684	 * IP first-hop destination address will be stored before
685	 * actual options; move other options back
686	 * and clear it when none present.
687	 */
688	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
689		goto bad;
690	cnt = m->m_len;
691	m->m_len += sizeof(struct in_addr);
692	cp = mtod(m, u_char *) + sizeof(struct in_addr);
693	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
694	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
695
696	for (; cnt > 0; cnt -= optlen, cp += optlen) {
697		opt = cp[IPOPT_OPTVAL];
698		if (opt == IPOPT_EOL)
699			break;
700		if (opt == IPOPT_NOP)
701			optlen = 1;
702		else {
703			optlen = cp[IPOPT_OLEN];
704			if (optlen <= IPOPT_OLEN || optlen > cnt)
705				goto bad;
706		}
707		switch (opt) {
708
709		default:
710			break;
711
712		case IPOPT_LSRR:
713		case IPOPT_SSRR:
714			/*
715			 * user process specifies route as:
716			 *	->A->B->C->D
717			 * D must be our final destination (but we can't
718			 * check that since we may not have connected yet).
719			 * A is first hop destination, which doesn't appear in
720			 * actual IP option, but is stored before the options.
721			 */
722			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
723				goto bad;
724			m->m_len -= sizeof(struct in_addr);
725			cnt -= sizeof(struct in_addr);
726			optlen -= sizeof(struct in_addr);
727			cp[IPOPT_OLEN] = optlen;
728			/*
729			 * Move first hop before start of options.
730			 */
731			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
732			    sizeof(struct in_addr));
733			/*
734			 * Then copy rest of options back
735			 * to close up the deleted entry.
736			 */
737			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
738			    sizeof(struct in_addr)),
739			    (caddr_t)&cp[IPOPT_OFFSET+1],
740			    (unsigned)cnt + sizeof(struct in_addr));
741			break;
742		}
743	}
744	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
745		goto bad;
746	*pcbopt = m;
747	return (0);
748
749bad:
750	(void)m_free(m);
751	return (EINVAL);
752}
753
754/*
755 * Set the IP multicast options in response to user setsockopt().
756 */
757int
758ip_setmoptions(optname, imop, m)
759	int optname;
760	struct ip_moptions **imop;
761	struct mbuf *m;
762{
763	register int error = 0;
764	u_char loop;
765	register int i;
766	struct in_addr addr;
767	register struct ip_mreq *mreq;
768	register struct ifnet *ifp;
769	register struct ip_moptions *imo = *imop;
770	struct route ro;
771	register struct sockaddr_in *dst;
772
773	if (imo == NULL) {
774		/*
775		 * No multicast option buffer attached to the pcb;
776		 * allocate one and initialize to default values.
777		 */
778		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
779		    M_WAITOK);
780
781		if (imo == NULL)
782			return (ENOBUFS);
783		*imop = imo;
784		imo->imo_multicast_ifp = NULL;
785		imo->imo_multicast_vif = 0;
786		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
787		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
788		imo->imo_num_memberships = 0;
789	}
790
791	switch (optname) {
792		extern int (*legal_vif_num)(int);
793	/* store an index number for the vif you wanna use in the send */
794	case IP_MULTICAST_VIF:
795		if (!legal_vif_num) {
796			error = EOPNOTSUPP;
797			break;
798		}
799		if (m == NULL || m->m_len != sizeof(int)) {
800			error = EINVAL;
801			break;
802		}
803		i = *(mtod(m, int *));
804		if (!legal_vif_num(i)) {
805			error = EINVAL;
806			break;
807		}
808		imo->imo_multicast_vif = i;
809		break;
810
811	case IP_MULTICAST_IF:
812		/*
813		 * Select the interface for outgoing multicast packets.
814		 */
815		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
816			error = EINVAL;
817			break;
818		}
819		addr = *(mtod(m, struct in_addr *));
820		/*
821		 * INADDR_ANY is used to remove a previous selection.
822		 * When no interface is selected, a default one is
823		 * chosen every time a multicast packet is sent.
824		 */
825		if (addr.s_addr == INADDR_ANY) {
826			imo->imo_multicast_ifp = NULL;
827			break;
828		}
829		/*
830		 * The selected interface is identified by its local
831		 * IP address.  Find the interface and confirm that
832		 * it supports multicasting.
833		 */
834		INADDR_TO_IFP(addr, ifp);
835		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
836			error = EADDRNOTAVAIL;
837			break;
838		}
839		imo->imo_multicast_ifp = ifp;
840		break;
841
842	case IP_MULTICAST_TTL:
843		/*
844		 * Set the IP time-to-live for outgoing multicast packets.
845		 */
846		if (m == NULL || m->m_len != 1) {
847			error = EINVAL;
848			break;
849		}
850		imo->imo_multicast_ttl = *(mtod(m, u_char *));
851		break;
852
853	case IP_MULTICAST_LOOP:
854		/*
855		 * Set the loopback flag for outgoing multicast packets.
856		 * Must be zero or one.
857		 */
858		if (m == NULL || m->m_len != 1 ||
859		   (loop = *(mtod(m, u_char *))) > 1) {
860			error = EINVAL;
861			break;
862		}
863		imo->imo_multicast_loop = loop;
864		break;
865
866	case IP_ADD_MEMBERSHIP:
867		/*
868		 * Add a multicast group membership.
869		 * Group must be a valid IP multicast address.
870		 */
871		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
872			error = EINVAL;
873			break;
874		}
875		mreq = mtod(m, struct ip_mreq *);
876		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
877			error = EINVAL;
878			break;
879		}
880		/*
881		 * If no interface address was provided, use the interface of
882		 * the route to the given multicast address.
883		 */
884		if (mreq->imr_interface.s_addr == INADDR_ANY) {
885			ro.ro_rt = NULL;
886			dst = (struct sockaddr_in *)&ro.ro_dst;
887			dst->sin_len = sizeof(*dst);
888			dst->sin_family = AF_INET;
889			dst->sin_addr = mreq->imr_multiaddr;
890			rtalloc(&ro);
891			if (ro.ro_rt == NULL) {
892				error = EADDRNOTAVAIL;
893				break;
894			}
895			ifp = ro.ro_rt->rt_ifp;
896			rtfree(ro.ro_rt);
897		}
898		else {
899			INADDR_TO_IFP(mreq->imr_interface, ifp);
900		}
901		/*
902		 * See if we found an interface, and confirm that it
903		 * supports multicast.
904		 */
905		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
906			error = EADDRNOTAVAIL;
907			break;
908		}
909		/*
910		 * See if the membership already exists or if all the
911		 * membership slots are full.
912		 */
913		for (i = 0; i < imo->imo_num_memberships; ++i) {
914			if (imo->imo_membership[i]->inm_ifp == ifp &&
915			    imo->imo_membership[i]->inm_addr.s_addr
916						== mreq->imr_multiaddr.s_addr)
917				break;
918		}
919		if (i < imo->imo_num_memberships) {
920			error = EADDRINUSE;
921			break;
922		}
923		if (i == IP_MAX_MEMBERSHIPS) {
924			error = ETOOMANYREFS;
925			break;
926		}
927		/*
928		 * Everything looks good; add a new record to the multicast
929		 * address list for the given interface.
930		 */
931		if ((imo->imo_membership[i] =
932		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
933			error = ENOBUFS;
934			break;
935		}
936		++imo->imo_num_memberships;
937		break;
938
939	case IP_DROP_MEMBERSHIP:
940		/*
941		 * Drop a multicast group membership.
942		 * Group must be a valid IP multicast address.
943		 */
944		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
945			error = EINVAL;
946			break;
947		}
948		mreq = mtod(m, struct ip_mreq *);
949		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
950			error = EINVAL;
951			break;
952		}
953		/*
954		 * If an interface address was specified, get a pointer
955		 * to its ifnet structure.
956		 */
957		if (mreq->imr_interface.s_addr == INADDR_ANY)
958			ifp = NULL;
959		else {
960			INADDR_TO_IFP(mreq->imr_interface, ifp);
961			if (ifp == NULL) {
962				error = EADDRNOTAVAIL;
963				break;
964			}
965		}
966		/*
967		 * Find the membership in the membership array.
968		 */
969		for (i = 0; i < imo->imo_num_memberships; ++i) {
970			if ((ifp == NULL ||
971			     imo->imo_membership[i]->inm_ifp == ifp) &&
972			     imo->imo_membership[i]->inm_addr.s_addr ==
973			     mreq->imr_multiaddr.s_addr)
974				break;
975		}
976		if (i == imo->imo_num_memberships) {
977			error = EADDRNOTAVAIL;
978			break;
979		}
980		/*
981		 * Give up the multicast address record to which the
982		 * membership points.
983		 */
984		in_delmulti(imo->imo_membership[i]);
985		/*
986		 * Remove the gap in the membership array.
987		 */
988		for (++i; i < imo->imo_num_memberships; ++i)
989			imo->imo_membership[i-1] = imo->imo_membership[i];
990		--imo->imo_num_memberships;
991		break;
992
993	default:
994		error = EOPNOTSUPP;
995		break;
996	}
997
998	/*
999	 * If all options have default values, no need to keep the mbuf.
1000	 */
1001	if (imo->imo_multicast_ifp == NULL &&
1002	    imo->imo_multicast_vif == 0 &&
1003	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1004	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1005	    imo->imo_num_memberships == 0) {
1006		free(*imop, M_IPMOPTS);
1007		*imop = NULL;
1008	}
1009
1010	return (error);
1011}
1012
1013/*
1014 * Return the IP multicast options in response to user getsockopt().
1015 */
1016int
1017ip_getmoptions(optname, imo, mp)
1018	int optname;
1019	register struct ip_moptions *imo;
1020	register struct mbuf **mp;
1021{
1022	u_char *ttl;
1023	u_char *loop;
1024	struct in_addr *addr;
1025	struct in_ifaddr *ia;
1026
1027	*mp = m_get(M_WAIT, MT_SOOPTS);
1028
1029	switch (optname) {
1030
1031	case IP_MULTICAST_VIF:
1032		if (imo != NULL)
1033			*(mtod(*mp, int *)) = imo->imo_multicast_vif;
1034		else
1035			*(mtod(*mp, int *)) = 7890;
1036		(*mp)->m_len = sizeof(int);
1037		return(0);
1038
1039	case IP_MULTICAST_IF:
1040		addr = mtod(*mp, struct in_addr *);
1041		(*mp)->m_len = sizeof(struct in_addr);
1042		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1043			addr->s_addr = INADDR_ANY;
1044		else {
1045			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1046			addr->s_addr = (ia == NULL) ? INADDR_ANY
1047					: IA_SIN(ia)->sin_addr.s_addr;
1048		}
1049		return (0);
1050
1051	case IP_MULTICAST_TTL:
1052		ttl = mtod(*mp, u_char *);
1053		(*mp)->m_len = 1;
1054		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1055				     : imo->imo_multicast_ttl;
1056		return (0);
1057
1058	case IP_MULTICAST_LOOP:
1059		loop = mtod(*mp, u_char *);
1060		(*mp)->m_len = 1;
1061		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1062				      : imo->imo_multicast_loop;
1063		return (0);
1064
1065	default:
1066		return (EOPNOTSUPP);
1067	}
1068}
1069
1070/*
1071 * Discard the IP multicast options.
1072 */
1073void
1074ip_freemoptions(imo)
1075	register struct ip_moptions *imo;
1076{
1077	register int i;
1078
1079	if (imo != NULL) {
1080		for (i = 0; i < imo->imo_num_memberships; ++i)
1081			in_delmulti(imo->imo_membership[i]);
1082		free(imo, M_IPMOPTS);
1083	}
1084}
1085
1086/*
1087 * Routine called from ip_output() to loop back a copy of an IP multicast
1088 * packet to the input queue of a specified interface.  Note that this
1089 * calls the output routine of the loopback "driver", but with an interface
1090 * pointer that might NOT be &loif -- easier than replicating that code here.
1091 */
1092static void
1093ip_mloopback(ifp, m, dst)
1094	struct ifnet *ifp;
1095	register struct mbuf *m;
1096	register struct sockaddr_in *dst;
1097{
1098	register struct ip *ip;
1099	struct mbuf *copym;
1100
1101	copym = m_copy(m, 0, M_COPYALL);
1102	if (copym != NULL) {
1103		/*
1104		 * We don't bother to fragment if the IP length is greater
1105		 * than the interface's MTU.  Can this possibly matter?
1106		 */
1107		ip = mtod(copym, struct ip *);
1108		ip->ip_len = htons((u_short)ip->ip_len);
1109		ip->ip_off = htons((u_short)ip->ip_off);
1110		ip->ip_sum = 0;
1111		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1112		(void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1113	}
1114}
1115