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