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