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