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