ip_output.c revision 17758
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.41 1996/07/10 19:44:26 julian 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        if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, IP_NAT_OUT)) {
343		error = EACCES;
344		goto done;
345	}
346
347#ifdef COMPAT_IPFW
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			if (m == 0 || m->m_len != sizeof(int))
632				error = EINVAL;
633			else {
634				optval = *mtod(m, int *);
635				switch (optname) {
636
637				case IP_TOS:
638					inp->inp_ip.ip_tos = optval;
639					break;
640
641				case IP_TTL:
642					inp->inp_ip.ip_ttl = optval;
643					break;
644#define	OPTSET(bit) \
645	if (optval) \
646		inp->inp_flags |= bit; \
647	else \
648		inp->inp_flags &= ~bit;
649
650				case IP_RECVOPTS:
651					OPTSET(INP_RECVOPTS);
652					break;
653
654				case IP_RECVRETOPTS:
655					OPTSET(INP_RECVRETOPTS);
656					break;
657
658				case IP_RECVDSTADDR:
659					OPTSET(INP_RECVDSTADDR);
660					break;
661				}
662			}
663			break;
664#undef OPTSET
665
666		case IP_MULTICAST_IF:
667		case IP_MULTICAST_VIF:
668		case IP_MULTICAST_TTL:
669		case IP_MULTICAST_LOOP:
670		case IP_ADD_MEMBERSHIP:
671		case IP_DROP_MEMBERSHIP:
672			error = ip_setmoptions(optname, &inp->inp_moptions, m);
673			break;
674
675		case IP_PORTRANGE:
676			if (m == 0 || m->m_len != sizeof(int))
677				error = EINVAL;
678			else {
679				optval = *mtod(m, int *);
680
681				switch (optval) {
682
683				case IP_PORTRANGE_DEFAULT:
684					inp->inp_flags &= ~(INP_LOWPORT);
685					inp->inp_flags &= ~(INP_HIGHPORT);
686					break;
687
688				case IP_PORTRANGE_HIGH:
689					inp->inp_flags &= ~(INP_LOWPORT);
690					inp->inp_flags |= INP_HIGHPORT;
691					break;
692
693				case IP_PORTRANGE_LOW:
694					inp->inp_flags &= ~(INP_HIGHPORT);
695					inp->inp_flags |= INP_LOWPORT;
696					break;
697
698				default:
699					error = EINVAL;
700					break;
701				}
702			}
703			break;
704
705		default:
706			error = ENOPROTOOPT;
707			break;
708		}
709		if (m)
710			(void)m_free(m);
711		break;
712
713	case PRCO_GETOPT:
714		switch (optname) {
715		case IP_OPTIONS:
716		case IP_RETOPTS:
717			*mp = m = m_get(M_WAIT, MT_SOOPTS);
718			if (inp->inp_options) {
719				m->m_len = inp->inp_options->m_len;
720				bcopy(mtod(inp->inp_options, void *),
721				    mtod(m, void *), m->m_len);
722			} else
723				m->m_len = 0;
724			break;
725
726		case IP_TOS:
727		case IP_TTL:
728		case IP_RECVOPTS:
729		case IP_RECVRETOPTS:
730		case IP_RECVDSTADDR:
731			*mp = m = m_get(M_WAIT, MT_SOOPTS);
732			m->m_len = sizeof(int);
733			switch (optname) {
734
735			case IP_TOS:
736				optval = inp->inp_ip.ip_tos;
737				break;
738
739			case IP_TTL:
740				optval = inp->inp_ip.ip_ttl;
741				break;
742
743#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
744
745			case IP_RECVOPTS:
746				optval = OPTBIT(INP_RECVOPTS);
747				break;
748
749			case IP_RECVRETOPTS:
750				optval = OPTBIT(INP_RECVRETOPTS);
751				break;
752
753			case IP_RECVDSTADDR:
754				optval = OPTBIT(INP_RECVDSTADDR);
755				break;
756			}
757			*mtod(m, int *) = optval;
758			break;
759
760		case IP_MULTICAST_IF:
761		case IP_MULTICAST_VIF:
762		case IP_MULTICAST_TTL:
763		case IP_MULTICAST_LOOP:
764		case IP_ADD_MEMBERSHIP:
765		case IP_DROP_MEMBERSHIP:
766			error = ip_getmoptions(optname, inp->inp_moptions, mp);
767			break;
768
769		case IP_PORTRANGE:
770			*mp = m = m_get(M_WAIT, MT_SOOPTS);
771			m->m_len = sizeof(int);
772
773			if (inp->inp_flags & INP_HIGHPORT)
774				optval = IP_PORTRANGE_HIGH;
775			else if (inp->inp_flags & INP_LOWPORT)
776				optval = IP_PORTRANGE_LOW;
777			else
778				optval = 0;
779
780			*mtod(m, int *) = optval;
781			break;
782
783		default:
784			error = ENOPROTOOPT;
785			break;
786		}
787		break;
788	}
789	return (error);
790}
791
792/*
793 * Set up IP options in pcb for insertion in output packets.
794 * Store in mbuf with pointer in pcbopt, adding pseudo-option
795 * with destination address if source routed.
796 */
797static int
798#ifdef notyet
799ip_pcbopts(optname, pcbopt, m)
800	int optname;
801#else
802ip_pcbopts(pcbopt, m)
803#endif
804	struct mbuf **pcbopt;
805	register struct mbuf *m;
806{
807	register cnt, optlen;
808	register u_char *cp;
809	u_char opt;
810
811	/* turn off any old options */
812	if (*pcbopt)
813		(void)m_free(*pcbopt);
814	*pcbopt = 0;
815	if (m == (struct mbuf *)0 || m->m_len == 0) {
816		/*
817		 * Only turning off any previous options.
818		 */
819		if (m)
820			(void)m_free(m);
821		return (0);
822	}
823
824#ifndef	vax
825	if (m->m_len % sizeof(long))
826		goto bad;
827#endif
828	/*
829	 * IP first-hop destination address will be stored before
830	 * actual options; move other options back
831	 * and clear it when none present.
832	 */
833	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
834		goto bad;
835	cnt = m->m_len;
836	m->m_len += sizeof(struct in_addr);
837	cp = mtod(m, u_char *) + sizeof(struct in_addr);
838	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
839	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
840
841	for (; cnt > 0; cnt -= optlen, cp += optlen) {
842		opt = cp[IPOPT_OPTVAL];
843		if (opt == IPOPT_EOL)
844			break;
845		if (opt == IPOPT_NOP)
846			optlen = 1;
847		else {
848			optlen = cp[IPOPT_OLEN];
849			if (optlen <= IPOPT_OLEN || optlen > cnt)
850				goto bad;
851		}
852		switch (opt) {
853
854		default:
855			break;
856
857		case IPOPT_LSRR:
858		case IPOPT_SSRR:
859			/*
860			 * user process specifies route as:
861			 *	->A->B->C->D
862			 * D must be our final destination (but we can't
863			 * check that since we may not have connected yet).
864			 * A is first hop destination, which doesn't appear in
865			 * actual IP option, but is stored before the options.
866			 */
867			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
868				goto bad;
869			m->m_len -= sizeof(struct in_addr);
870			cnt -= sizeof(struct in_addr);
871			optlen -= sizeof(struct in_addr);
872			cp[IPOPT_OLEN] = optlen;
873			/*
874			 * Move first hop before start of options.
875			 */
876			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
877			    sizeof(struct in_addr));
878			/*
879			 * Then copy rest of options back
880			 * to close up the deleted entry.
881			 */
882			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
883			    sizeof(struct in_addr)),
884			    (caddr_t)&cp[IPOPT_OFFSET+1],
885			    (unsigned)cnt + sizeof(struct in_addr));
886			break;
887		}
888	}
889	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
890		goto bad;
891	*pcbopt = m;
892	return (0);
893
894bad:
895	(void)m_free(m);
896	return (EINVAL);
897}
898
899/*
900 * Set the IP multicast options in response to user setsockopt().
901 */
902static int
903ip_setmoptions(optname, imop, m)
904	int optname;
905	struct ip_moptions **imop;
906	struct mbuf *m;
907{
908	register int error = 0;
909	u_char loop;
910	register int i;
911	struct in_addr addr;
912	register struct ip_mreq *mreq;
913	register struct ifnet *ifp;
914	register struct ip_moptions *imo = *imop;
915	struct route ro;
916	register struct sockaddr_in *dst;
917	int s;
918
919	if (imo == NULL) {
920		/*
921		 * No multicast option buffer attached to the pcb;
922		 * allocate one and initialize to default values.
923		 */
924		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
925		    M_WAITOK);
926
927		if (imo == NULL)
928			return (ENOBUFS);
929		*imop = imo;
930		imo->imo_multicast_ifp = NULL;
931		imo->imo_multicast_vif = -1;
932		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
933		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
934		imo->imo_num_memberships = 0;
935	}
936
937	switch (optname) {
938	/* store an index number for the vif you wanna use in the send */
939	case IP_MULTICAST_VIF:
940		if (!legal_vif_num) {
941			error = EOPNOTSUPP;
942			break;
943		}
944		if (m == NULL || m->m_len != sizeof(int)) {
945			error = EINVAL;
946			break;
947		}
948		i = *(mtod(m, int *));
949		if (!legal_vif_num(i) && (i != -1)) {
950			error = EINVAL;
951			break;
952		}
953		imo->imo_multicast_vif = i;
954		break;
955
956	case IP_MULTICAST_IF:
957		/*
958		 * Select the interface for outgoing multicast packets.
959		 */
960		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
961			error = EINVAL;
962			break;
963		}
964		addr = *(mtod(m, struct in_addr *));
965		/*
966		 * INADDR_ANY is used to remove a previous selection.
967		 * When no interface is selected, a default one is
968		 * chosen every time a multicast packet is sent.
969		 */
970		if (addr.s_addr == INADDR_ANY) {
971			imo->imo_multicast_ifp = NULL;
972			break;
973		}
974		/*
975		 * The selected interface is identified by its local
976		 * IP address.  Find the interface and confirm that
977		 * it supports multicasting.
978		 */
979		s = splimp();
980		INADDR_TO_IFP(addr, ifp);
981		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
982			splx(s);
983			error = EADDRNOTAVAIL;
984			break;
985		}
986		imo->imo_multicast_ifp = ifp;
987		splx(s);
988		break;
989
990	case IP_MULTICAST_TTL:
991		/*
992		 * Set the IP time-to-live for outgoing multicast packets.
993		 */
994		if (m == NULL || m->m_len != 1) {
995			error = EINVAL;
996			break;
997		}
998		imo->imo_multicast_ttl = *(mtod(m, u_char *));
999		break;
1000
1001	case IP_MULTICAST_LOOP:
1002		/*
1003		 * Set the loopback flag for outgoing multicast packets.
1004		 * Must be zero or one.
1005		 */
1006		if (m == NULL || m->m_len != 1 ||
1007		   (loop = *(mtod(m, u_char *))) > 1) {
1008			error = EINVAL;
1009			break;
1010		}
1011		imo->imo_multicast_loop = loop;
1012		break;
1013
1014	case IP_ADD_MEMBERSHIP:
1015		/*
1016		 * Add a multicast group membership.
1017		 * Group must be a valid IP multicast address.
1018		 */
1019		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1020			error = EINVAL;
1021			break;
1022		}
1023		mreq = mtod(m, struct ip_mreq *);
1024		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1025			error = EINVAL;
1026			break;
1027		}
1028		s = splimp();
1029		/*
1030		 * If no interface address was provided, use the interface of
1031		 * the route to the given multicast address.
1032		 */
1033		if (mreq->imr_interface.s_addr == INADDR_ANY) {
1034			bzero((caddr_t)&ro, sizeof(ro));
1035			dst = (struct sockaddr_in *)&ro.ro_dst;
1036			dst->sin_len = sizeof(*dst);
1037			dst->sin_family = AF_INET;
1038			dst->sin_addr = mreq->imr_multiaddr;
1039			rtalloc(&ro);
1040			if (ro.ro_rt == NULL) {
1041				error = EADDRNOTAVAIL;
1042				splx(s);
1043				break;
1044			}
1045			ifp = ro.ro_rt->rt_ifp;
1046			rtfree(ro.ro_rt);
1047		}
1048		else {
1049			INADDR_TO_IFP(mreq->imr_interface, ifp);
1050		}
1051
1052		/*
1053		 * See if we found an interface, and confirm that it
1054		 * supports multicast.
1055		 */
1056		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1057			error = EADDRNOTAVAIL;
1058			splx(s);
1059			break;
1060		}
1061		/*
1062		 * See if the membership already exists or if all the
1063		 * membership slots are full.
1064		 */
1065		for (i = 0; i < imo->imo_num_memberships; ++i) {
1066			if (imo->imo_membership[i]->inm_ifp == ifp &&
1067			    imo->imo_membership[i]->inm_addr.s_addr
1068						== mreq->imr_multiaddr.s_addr)
1069				break;
1070		}
1071		if (i < imo->imo_num_memberships) {
1072			error = EADDRINUSE;
1073			splx(s);
1074			break;
1075		}
1076		if (i == IP_MAX_MEMBERSHIPS) {
1077			error = ETOOMANYREFS;
1078			splx(s);
1079			break;
1080		}
1081		/*
1082		 * Everything looks good; add a new record to the multicast
1083		 * address list for the given interface.
1084		 */
1085		if ((imo->imo_membership[i] =
1086		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
1087			error = ENOBUFS;
1088			splx(s);
1089			break;
1090		}
1091		++imo->imo_num_memberships;
1092		splx(s);
1093		break;
1094
1095	case IP_DROP_MEMBERSHIP:
1096		/*
1097		 * Drop a multicast group membership.
1098		 * Group must be a valid IP multicast address.
1099		 */
1100		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
1101			error = EINVAL;
1102			break;
1103		}
1104		mreq = mtod(m, struct ip_mreq *);
1105		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
1106			error = EINVAL;
1107			break;
1108		}
1109
1110		s = splimp();
1111		/*
1112		 * If an interface address was specified, get a pointer
1113		 * to its ifnet structure.
1114		 */
1115		if (mreq->imr_interface.s_addr == INADDR_ANY)
1116			ifp = NULL;
1117		else {
1118			INADDR_TO_IFP(mreq->imr_interface, ifp);
1119			if (ifp == NULL) {
1120				error = EADDRNOTAVAIL;
1121				splx(s);
1122				break;
1123			}
1124		}
1125		/*
1126		 * Find the membership in the membership array.
1127		 */
1128		for (i = 0; i < imo->imo_num_memberships; ++i) {
1129			if ((ifp == NULL ||
1130			     imo->imo_membership[i]->inm_ifp == ifp) &&
1131			     imo->imo_membership[i]->inm_addr.s_addr ==
1132			     mreq->imr_multiaddr.s_addr)
1133				break;
1134		}
1135		if (i == imo->imo_num_memberships) {
1136			error = EADDRNOTAVAIL;
1137			splx(s);
1138			break;
1139		}
1140		/*
1141		 * Give up the multicast address record to which the
1142		 * membership points.
1143		 */
1144		in_delmulti(imo->imo_membership[i]);
1145		/*
1146		 * Remove the gap in the membership array.
1147		 */
1148		for (++i; i < imo->imo_num_memberships; ++i)
1149			imo->imo_membership[i-1] = imo->imo_membership[i];
1150		--imo->imo_num_memberships;
1151		splx(s);
1152		break;
1153
1154	default:
1155		error = EOPNOTSUPP;
1156		break;
1157	}
1158
1159	/*
1160	 * If all options have default values, no need to keep the mbuf.
1161	 */
1162	if (imo->imo_multicast_ifp == NULL &&
1163	    imo->imo_multicast_vif == -1 &&
1164	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1165	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1166	    imo->imo_num_memberships == 0) {
1167		free(*imop, M_IPMOPTS);
1168		*imop = NULL;
1169	}
1170
1171	return (error);
1172}
1173
1174/*
1175 * Return the IP multicast options in response to user getsockopt().
1176 */
1177static int
1178ip_getmoptions(optname, imo, mp)
1179	int optname;
1180	register struct ip_moptions *imo;
1181	register struct mbuf **mp;
1182{
1183	u_char *ttl;
1184	u_char *loop;
1185	struct in_addr *addr;
1186	struct in_ifaddr *ia;
1187
1188	*mp = m_get(M_WAIT, MT_SOOPTS);
1189
1190	switch (optname) {
1191
1192	case IP_MULTICAST_VIF:
1193		if (imo != NULL)
1194			*(mtod(*mp, int *)) = imo->imo_multicast_vif;
1195		else
1196			*(mtod(*mp, int *)) = -1;
1197		(*mp)->m_len = sizeof(int);
1198		return(0);
1199
1200	case IP_MULTICAST_IF:
1201		addr = mtod(*mp, struct in_addr *);
1202		(*mp)->m_len = sizeof(struct in_addr);
1203		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1204			addr->s_addr = INADDR_ANY;
1205		else {
1206			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1207			addr->s_addr = (ia == NULL) ? INADDR_ANY
1208					: IA_SIN(ia)->sin_addr.s_addr;
1209		}
1210		return (0);
1211
1212	case IP_MULTICAST_TTL:
1213		ttl = mtod(*mp, u_char *);
1214		(*mp)->m_len = 1;
1215		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1216				     : imo->imo_multicast_ttl;
1217		return (0);
1218
1219	case IP_MULTICAST_LOOP:
1220		loop = mtod(*mp, u_char *);
1221		(*mp)->m_len = 1;
1222		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1223				      : imo->imo_multicast_loop;
1224		return (0);
1225
1226	default:
1227		return (EOPNOTSUPP);
1228	}
1229}
1230
1231/*
1232 * Discard the IP multicast options.
1233 */
1234void
1235ip_freemoptions(imo)
1236	register struct ip_moptions *imo;
1237{
1238	register int i;
1239
1240	if (imo != NULL) {
1241		for (i = 0; i < imo->imo_num_memberships; ++i)
1242			in_delmulti(imo->imo_membership[i]);
1243		free(imo, M_IPMOPTS);
1244	}
1245}
1246
1247/*
1248 * Routine called from ip_output() to loop back a copy of an IP multicast
1249 * packet to the input queue of a specified interface.  Note that this
1250 * calls the output routine of the loopback "driver", but with an interface
1251 * pointer that might NOT be a loopback interface -- evil, but easier than
1252 * replicating that code here.
1253 */
1254static void
1255ip_mloopback(ifp, m, dst)
1256	struct ifnet *ifp;
1257	register struct mbuf *m;
1258	register struct sockaddr_in *dst;
1259{
1260	register struct ip *ip;
1261	struct mbuf *copym;
1262
1263	copym = m_copy(m, 0, M_COPYALL);
1264	if (copym != NULL) {
1265		/*
1266		 * We don't bother to fragment if the IP length is greater
1267		 * than the interface's MTU.  Can this possibly matter?
1268		 */
1269		ip = mtod(copym, struct ip *);
1270		ip->ip_len = htons((u_short)ip->ip_len);
1271		ip->ip_off = htons((u_short)ip->ip_off);
1272		ip->ip_sum = 0;
1273		if (ip->ip_vhl == IP_VHL_BORING) {
1274			ip->ip_sum = in_cksum_hdr(ip);
1275		} else {
1276			ip->ip_sum = in_cksum(copym,
1277					      IP_VHL_HL(ip->ip_vhl) << 2);
1278		}
1279		/*
1280		 * NB:
1281		 * We can't simply call ip_input() directly because
1282		 * the ip_mforward() depends on the `input interface'
1283		 * being set to something unreasonable so that we don't
1284		 * attempt to forward the looped-back copy.
1285		 * It's also not clear whether there are any lingering
1286		 * reentrancy problems in other areas which might be
1287		 * exposed by this code.  For the moment, we'll err
1288		 * on the side of safety by continuing to abuse
1289		 * loinput().
1290		 */
1291#ifdef notdef
1292		copym->m_pkthdr.rcvif = &loif[0];
1293		ip_input(copym)
1294#else
1295		(void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1296#endif
1297	}
1298}
1299