xref: /freebsd/sys/netinet/ip_output.c (revision 26f9a76710a312a951848542b9ca1f44100450e2)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1990, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
34df8bae1dSRodney W. Grimes  */
35df8bae1dSRodney W. Grimes 
36df8bae1dSRodney W. Grimes #include <sys/param.h>
3726f9a767SRodney W. Grimes #include <sys/systm.h>
38df8bae1dSRodney W. Grimes #include <sys/malloc.h>
39df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
40df8bae1dSRodney W. Grimes #include <sys/errno.h>
41df8bae1dSRodney W. Grimes #include <sys/protosw.h>
42df8bae1dSRodney W. Grimes #include <sys/socket.h>
43df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes #include <net/if.h>
46df8bae1dSRodney W. Grimes #include <net/route.h>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <netinet/in.h>
49df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
50df8bae1dSRodney W. Grimes #include <netinet/ip.h>
51df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
52df8bae1dSRodney W. Grimes #include <netinet/in_var.h>
53df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
54df8bae1dSRodney W. Grimes 
55df8bae1dSRodney W. Grimes #ifdef vax
56df8bae1dSRodney W. Grimes #include <machine/mtpr.h>
57df8bae1dSRodney W. Grimes #endif
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
60df8bae1dSRodney W. Grimes static void ip_mloopback
61df8bae1dSRodney W. Grimes 	__P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  * IP output.  The packet in mbuf chain m contains a skeletal IP
65df8bae1dSRodney W. Grimes  * header (with len, off, ttl, proto, tos, src, dst).
66df8bae1dSRodney W. Grimes  * The mbuf chain containing the packet will be freed.
67df8bae1dSRodney W. Grimes  * The mbuf opt, if present, will not be freed.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes int
70df8bae1dSRodney W. Grimes ip_output(m0, opt, ro, flags, imo)
71df8bae1dSRodney W. Grimes 	struct mbuf *m0;
72df8bae1dSRodney W. Grimes 	struct mbuf *opt;
73df8bae1dSRodney W. Grimes 	struct route *ro;
74df8bae1dSRodney W. Grimes 	int flags;
75df8bae1dSRodney W. Grimes 	struct ip_moptions *imo;
76df8bae1dSRodney W. Grimes {
77df8bae1dSRodney W. Grimes 	register struct ip *ip, *mhip;
78df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
79df8bae1dSRodney W. Grimes 	register struct mbuf *m = m0;
80df8bae1dSRodney W. Grimes 	register int hlen = sizeof (struct ip);
81df8bae1dSRodney W. Grimes 	int len, off, error = 0;
82df8bae1dSRodney W. Grimes 	struct route iproute;
83df8bae1dSRodney W. Grimes 	struct sockaddr_in *dst;
84df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #ifdef	DIAGNOSTIC
87df8bae1dSRodney W. Grimes 	if ((m->m_flags & M_PKTHDR) == 0)
88df8bae1dSRodney W. Grimes 		panic("ip_output no HDR");
89df8bae1dSRodney W. Grimes #endif
90df8bae1dSRodney W. Grimes 	if (opt) {
91df8bae1dSRodney W. Grimes 		m = ip_insertoptions(m, opt, &len);
92df8bae1dSRodney W. Grimes 		hlen = len;
93df8bae1dSRodney W. Grimes 	}
94df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
95df8bae1dSRodney W. Grimes 	/*
96df8bae1dSRodney W. Grimes 	 * Fill in IP header.
97df8bae1dSRodney W. Grimes 	 */
98df8bae1dSRodney W. Grimes 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
99df8bae1dSRodney W. Grimes 		ip->ip_v = IPVERSION;
100df8bae1dSRodney W. Grimes 		ip->ip_off &= IP_DF;
101df8bae1dSRodney W. Grimes 		ip->ip_id = htons(ip_id++);
102df8bae1dSRodney W. Grimes 		ip->ip_hl = hlen >> 2;
103df8bae1dSRodney W. Grimes 		ipstat.ips_localout++;
104df8bae1dSRodney W. Grimes 	} else {
105df8bae1dSRodney W. Grimes 		hlen = ip->ip_hl << 2;
106df8bae1dSRodney W. Grimes 	}
107df8bae1dSRodney W. Grimes 	/*
108df8bae1dSRodney W. Grimes 	 * Route packet.
109df8bae1dSRodney W. Grimes 	 */
110df8bae1dSRodney W. Grimes 	if (ro == 0) {
111df8bae1dSRodney W. Grimes 		ro = &iproute;
112df8bae1dSRodney W. Grimes 		bzero((caddr_t)ro, sizeof (*ro));
113df8bae1dSRodney W. Grimes 	}
114df8bae1dSRodney W. Grimes 	dst = (struct sockaddr_in *)&ro->ro_dst;
115df8bae1dSRodney W. Grimes 	/*
116df8bae1dSRodney W. Grimes 	 * If there is a cached route,
117df8bae1dSRodney W. Grimes 	 * check that it is to the same destination
118df8bae1dSRodney W. Grimes 	 * and is still up.  If not, free it and try again.
119df8bae1dSRodney W. Grimes 	 */
120df8bae1dSRodney W. Grimes 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
121df8bae1dSRodney W. Grimes 	   dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
122df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
123df8bae1dSRodney W. Grimes 		ro->ro_rt = (struct rtentry *)0;
124df8bae1dSRodney W. Grimes 	}
125df8bae1dSRodney W. Grimes 	if (ro->ro_rt == 0) {
126df8bae1dSRodney W. Grimes 		dst->sin_family = AF_INET;
127df8bae1dSRodney W. Grimes 		dst->sin_len = sizeof(*dst);
128df8bae1dSRodney W. Grimes 		dst->sin_addr = ip->ip_dst;
129df8bae1dSRodney W. Grimes 	}
130df8bae1dSRodney W. Grimes 	/*
131df8bae1dSRodney W. Grimes 	 * If routing to interface only,
132df8bae1dSRodney W. Grimes 	 * short circuit routing lookup.
133df8bae1dSRodney W. Grimes 	 */
134df8bae1dSRodney W. Grimes #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
135df8bae1dSRodney W. Grimes #define sintosa(sin)	((struct sockaddr *)(sin))
136df8bae1dSRodney W. Grimes 	if (flags & IP_ROUTETOIF) {
137df8bae1dSRodney W. Grimes 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
138df8bae1dSRodney W. Grimes 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
139df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
140df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
141df8bae1dSRodney W. Grimes 			goto bad;
142df8bae1dSRodney W. Grimes 		}
143df8bae1dSRodney W. Grimes 		ifp = ia->ia_ifp;
144df8bae1dSRodney W. Grimes 		ip->ip_ttl = 1;
145df8bae1dSRodney W. Grimes 	} else {
146df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0)
147df8bae1dSRodney W. Grimes 			rtalloc(ro);
148df8bae1dSRodney W. Grimes 		if (ro->ro_rt == 0) {
149df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
150df8bae1dSRodney W. Grimes 			error = EHOSTUNREACH;
151df8bae1dSRodney W. Grimes 			goto bad;
152df8bae1dSRodney W. Grimes 		}
153df8bae1dSRodney W. Grimes 		ia = ifatoia(ro->ro_rt->rt_ifa);
154df8bae1dSRodney W. Grimes 		ifp = ro->ro_rt->rt_ifp;
155df8bae1dSRodney W. Grimes 		ro->ro_rt->rt_use++;
156df8bae1dSRodney W. Grimes 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
157df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
158df8bae1dSRodney W. Grimes 	}
159df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
160df8bae1dSRodney W. Grimes 		struct in_multi *inm;
161df8bae1dSRodney W. Grimes 		extern struct ifnet loif;
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 		m->m_flags |= M_MCAST;
164df8bae1dSRodney W. Grimes 		/*
165df8bae1dSRodney W. Grimes 		 * IP destination address is multicast.  Make sure "dst"
166df8bae1dSRodney W. Grimes 		 * still points to the address in "ro".  (It may have been
167df8bae1dSRodney W. Grimes 		 * changed to point to a gateway address, above.)
168df8bae1dSRodney W. Grimes 		 */
169df8bae1dSRodney W. Grimes 		dst = (struct sockaddr_in *)&ro->ro_dst;
170df8bae1dSRodney W. Grimes 		/*
171df8bae1dSRodney W. Grimes 		 * See if the caller provided any multicast options
172df8bae1dSRodney W. Grimes 		 */
173df8bae1dSRodney W. Grimes 		if (imo != NULL) {
174df8bae1dSRodney W. Grimes 			ip->ip_ttl = imo->imo_multicast_ttl;
175df8bae1dSRodney W. Grimes 			if (imo->imo_multicast_ifp != NULL)
176df8bae1dSRodney W. Grimes 				ifp = imo->imo_multicast_ifp;
177df8bae1dSRodney W. Grimes 		} else
178df8bae1dSRodney W. Grimes 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
179df8bae1dSRodney W. Grimes 		/*
180df8bae1dSRodney W. Grimes 		 * Confirm that the outgoing interface supports multicast.
181df8bae1dSRodney W. Grimes 		 */
182df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
183df8bae1dSRodney W. Grimes 			ipstat.ips_noroute++;
184df8bae1dSRodney W. Grimes 			error = ENETUNREACH;
185df8bae1dSRodney W. Grimes 			goto bad;
186df8bae1dSRodney W. Grimes 		}
187df8bae1dSRodney W. Grimes 		/*
188df8bae1dSRodney W. Grimes 		 * If source address not specified yet, use address
189df8bae1dSRodney W. Grimes 		 * of outgoing interface.
190df8bae1dSRodney W. Grimes 		 */
191df8bae1dSRodney W. Grimes 		if (ip->ip_src.s_addr == INADDR_ANY) {
192df8bae1dSRodney W. Grimes 			register struct in_ifaddr *ia;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 			for (ia = in_ifaddr; ia; ia = ia->ia_next)
195df8bae1dSRodney W. Grimes 				if (ia->ia_ifp == ifp) {
196df8bae1dSRodney W. Grimes 					ip->ip_src = IA_SIN(ia)->sin_addr;
197df8bae1dSRodney W. Grimes 					break;
198df8bae1dSRodney W. Grimes 				}
199df8bae1dSRodney W. Grimes 		}
200df8bae1dSRodney W. Grimes 
201df8bae1dSRodney W. Grimes 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
202df8bae1dSRodney W. Grimes 		if (inm != NULL &&
203df8bae1dSRodney W. Grimes 		   (imo == NULL || imo->imo_multicast_loop)) {
204df8bae1dSRodney W. Grimes 			/*
205df8bae1dSRodney W. Grimes 			 * If we belong to the destination multicast group
206df8bae1dSRodney W. Grimes 			 * on the outgoing interface, and the caller did not
207df8bae1dSRodney W. Grimes 			 * forbid loopback, loop back a copy.
208df8bae1dSRodney W. Grimes 			 */
209df8bae1dSRodney W. Grimes 			ip_mloopback(ifp, m, dst);
210df8bae1dSRodney W. Grimes 		}
211df8bae1dSRodney W. Grimes #ifdef MROUTING
212df8bae1dSRodney W. Grimes 		else {
213df8bae1dSRodney W. Grimes 			/*
214df8bae1dSRodney W. Grimes 			 * If we are acting as a multicast router, perform
215df8bae1dSRodney W. Grimes 			 * multicast forwarding as if the packet had just
216df8bae1dSRodney W. Grimes 			 * arrived on the interface to which we are about
217df8bae1dSRodney W. Grimes 			 * to send.  The multicast forwarding function
218df8bae1dSRodney W. Grimes 			 * recursively calls this function, using the
219df8bae1dSRodney W. Grimes 			 * IP_FORWARDING flag to prevent infinite recursion.
220df8bae1dSRodney W. Grimes 			 *
221df8bae1dSRodney W. Grimes 			 * Multicasts that are looped back by ip_mloopback(),
222df8bae1dSRodney W. Grimes 			 * above, will be forwarded by the ip_input() routine,
223df8bae1dSRodney W. Grimes 			 * if necessary.
224df8bae1dSRodney W. Grimes 			 */
225df8bae1dSRodney W. Grimes 			extern struct socket *ip_mrouter;
226df8bae1dSRodney W. Grimes 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
227df8bae1dSRodney W. Grimes 				if (ip_mforward(m, ifp) != 0) {
228df8bae1dSRodney W. Grimes 					m_freem(m);
229df8bae1dSRodney W. Grimes 					goto done;
230df8bae1dSRodney W. Grimes 				}
231df8bae1dSRodney W. Grimes 			}
232df8bae1dSRodney W. Grimes 		}
233df8bae1dSRodney W. Grimes #endif
234df8bae1dSRodney W. Grimes 		/*
235df8bae1dSRodney W. Grimes 		 * Multicasts with a time-to-live of zero may be looped-
236df8bae1dSRodney W. Grimes 		 * back, above, but must not be transmitted on a network.
237df8bae1dSRodney W. Grimes 		 * Also, multicasts addressed to the loopback interface
238df8bae1dSRodney W. Grimes 		 * are not sent -- the above call to ip_mloopback() will
239df8bae1dSRodney W. Grimes 		 * loop back a copy if this host actually belongs to the
240df8bae1dSRodney W. Grimes 		 * destination group on the loopback interface.
241df8bae1dSRodney W. Grimes 		 */
242df8bae1dSRodney W. Grimes 		if (ip->ip_ttl == 0 || ifp == &loif) {
243df8bae1dSRodney W. Grimes 			m_freem(m);
244df8bae1dSRodney W. Grimes 			goto done;
245df8bae1dSRodney W. Grimes 		}
246df8bae1dSRodney W. Grimes 
247df8bae1dSRodney W. Grimes 		goto sendit;
248df8bae1dSRodney W. Grimes 	}
249df8bae1dSRodney W. Grimes #ifndef notdef
250df8bae1dSRodney W. Grimes 	/*
251df8bae1dSRodney W. Grimes 	 * If source address not specified yet, use address
252df8bae1dSRodney W. Grimes 	 * of outgoing interface.
253df8bae1dSRodney W. Grimes 	 */
254df8bae1dSRodney W. Grimes 	if (ip->ip_src.s_addr == INADDR_ANY)
255df8bae1dSRodney W. Grimes 		ip->ip_src = IA_SIN(ia)->sin_addr;
256df8bae1dSRodney W. Grimes #endif
257df8bae1dSRodney W. Grimes 	/*
258df8bae1dSRodney W. Grimes 	 * Look for broadcast address and
259df8bae1dSRodney W. Grimes 	 * and verify user is allowed to send
260df8bae1dSRodney W. Grimes 	 * such a packet.
261df8bae1dSRodney W. Grimes 	 */
262df8bae1dSRodney W. Grimes 	if (in_broadcast(dst->sin_addr, ifp)) {
263df8bae1dSRodney W. Grimes 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
264df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
265df8bae1dSRodney W. Grimes 			goto bad;
266df8bae1dSRodney W. Grimes 		}
267df8bae1dSRodney W. Grimes 		if ((flags & IP_ALLOWBROADCAST) == 0) {
268df8bae1dSRodney W. Grimes 			error = EACCES;
269df8bae1dSRodney W. Grimes 			goto bad;
270df8bae1dSRodney W. Grimes 		}
271df8bae1dSRodney W. Grimes 		/* don't allow broadcast messages to be fragmented */
272df8bae1dSRodney W. Grimes 		if ((u_short)ip->ip_len > ifp->if_mtu) {
273df8bae1dSRodney W. Grimes 			error = EMSGSIZE;
274df8bae1dSRodney W. Grimes 			goto bad;
275df8bae1dSRodney W. Grimes 		}
276df8bae1dSRodney W. Grimes 		m->m_flags |= M_BCAST;
277df8bae1dSRodney W. Grimes 	} else
278df8bae1dSRodney W. Grimes 		m->m_flags &= ~M_BCAST;
279df8bae1dSRodney W. Grimes 
280df8bae1dSRodney W. Grimes sendit:
281df8bae1dSRodney W. Grimes 	/*
282df8bae1dSRodney W. Grimes 	 * If small enough for interface, can just send directly.
283df8bae1dSRodney W. Grimes 	 */
284df8bae1dSRodney W. Grimes 	if ((u_short)ip->ip_len <= ifp->if_mtu) {
285df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
286df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
287df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
288df8bae1dSRodney W. Grimes 		ip->ip_sum = in_cksum(m, hlen);
289df8bae1dSRodney W. Grimes 		error = (*ifp->if_output)(ifp, m,
290df8bae1dSRodney W. Grimes 				(struct sockaddr *)dst, ro->ro_rt);
291df8bae1dSRodney W. Grimes 		goto done;
292df8bae1dSRodney W. Grimes 	}
293df8bae1dSRodney W. Grimes 	/*
294df8bae1dSRodney W. Grimes 	 * Too large for interface; fragment if possible.
295df8bae1dSRodney W. Grimes 	 * Must be able to put at least 8 bytes per fragment.
296df8bae1dSRodney W. Grimes 	 */
297df8bae1dSRodney W. Grimes 	if (ip->ip_off & IP_DF) {
298df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
299df8bae1dSRodney W. Grimes 		ipstat.ips_cantfrag++;
300df8bae1dSRodney W. Grimes 		goto bad;
301df8bae1dSRodney W. Grimes 	}
302df8bae1dSRodney W. Grimes 	len = (ifp->if_mtu - hlen) &~ 7;
303df8bae1dSRodney W. Grimes 	if (len < 8) {
304df8bae1dSRodney W. Grimes 		error = EMSGSIZE;
305df8bae1dSRodney W. Grimes 		goto bad;
306df8bae1dSRodney W. Grimes 	}
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes     {
309df8bae1dSRodney W. Grimes 	int mhlen, firstlen = len;
310df8bae1dSRodney W. Grimes 	struct mbuf **mnext = &m->m_nextpkt;
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 	/*
313df8bae1dSRodney W. Grimes 	 * Loop through length of segment after first fragment,
314df8bae1dSRodney W. Grimes 	 * make new header and copy data of each part and link onto chain.
315df8bae1dSRodney W. Grimes 	 */
316df8bae1dSRodney W. Grimes 	m0 = m;
317df8bae1dSRodney W. Grimes 	mhlen = sizeof (struct ip);
318df8bae1dSRodney W. Grimes 	for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
319df8bae1dSRodney W. Grimes 		MGETHDR(m, M_DONTWAIT, MT_HEADER);
320df8bae1dSRodney W. Grimes 		if (m == 0) {
321df8bae1dSRodney W. Grimes 			error = ENOBUFS;
322df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
323df8bae1dSRodney W. Grimes 			goto sendorfree;
324df8bae1dSRodney W. Grimes 		}
325df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
326df8bae1dSRodney W. Grimes 		mhip = mtod(m, struct ip *);
327df8bae1dSRodney W. Grimes 		*mhip = *ip;
328df8bae1dSRodney W. Grimes 		if (hlen > sizeof (struct ip)) {
329df8bae1dSRodney W. Grimes 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
330df8bae1dSRodney W. Grimes 			mhip->ip_hl = mhlen >> 2;
331df8bae1dSRodney W. Grimes 		}
332df8bae1dSRodney W. Grimes 		m->m_len = mhlen;
333df8bae1dSRodney W. Grimes 		mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
334df8bae1dSRodney W. Grimes 		if (ip->ip_off & IP_MF)
335df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
336df8bae1dSRodney W. Grimes 		if (off + len >= (u_short)ip->ip_len)
337df8bae1dSRodney W. Grimes 			len = (u_short)ip->ip_len - off;
338df8bae1dSRodney W. Grimes 		else
339df8bae1dSRodney W. Grimes 			mhip->ip_off |= IP_MF;
340df8bae1dSRodney W. Grimes 		mhip->ip_len = htons((u_short)(len + mhlen));
341df8bae1dSRodney W. Grimes 		m->m_next = m_copy(m0, off, len);
342df8bae1dSRodney W. Grimes 		if (m->m_next == 0) {
343df8bae1dSRodney W. Grimes 			(void) m_free(m);
344df8bae1dSRodney W. Grimes 			error = ENOBUFS;	/* ??? */
345df8bae1dSRodney W. Grimes 			ipstat.ips_odropped++;
346df8bae1dSRodney W. Grimes 			goto sendorfree;
347df8bae1dSRodney W. Grimes 		}
348df8bae1dSRodney W. Grimes 		m->m_pkthdr.len = mhlen + len;
349df8bae1dSRodney W. Grimes 		m->m_pkthdr.rcvif = (struct ifnet *)0;
350df8bae1dSRodney W. Grimes 		mhip->ip_off = htons((u_short)mhip->ip_off);
351df8bae1dSRodney W. Grimes 		mhip->ip_sum = 0;
352df8bae1dSRodney W. Grimes 		mhip->ip_sum = in_cksum(m, mhlen);
353df8bae1dSRodney W. Grimes 		*mnext = m;
354df8bae1dSRodney W. Grimes 		mnext = &m->m_nextpkt;
355df8bae1dSRodney W. Grimes 		ipstat.ips_ofragments++;
356df8bae1dSRodney W. Grimes 	}
357df8bae1dSRodney W. Grimes 	/*
358df8bae1dSRodney W. Grimes 	 * Update first fragment by trimming what's been copied out
359df8bae1dSRodney W. Grimes 	 * and updating header, then send each fragment (in order).
360df8bae1dSRodney W. Grimes 	 */
361df8bae1dSRodney W. Grimes 	m = m0;
362df8bae1dSRodney W. Grimes 	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
363df8bae1dSRodney W. Grimes 	m->m_pkthdr.len = hlen + firstlen;
364df8bae1dSRodney W. Grimes 	ip->ip_len = htons((u_short)m->m_pkthdr.len);
365df8bae1dSRodney W. Grimes 	ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
366df8bae1dSRodney W. Grimes 	ip->ip_sum = 0;
367df8bae1dSRodney W. Grimes 	ip->ip_sum = in_cksum(m, hlen);
368df8bae1dSRodney W. Grimes sendorfree:
369df8bae1dSRodney W. Grimes 	for (m = m0; m; m = m0) {
370df8bae1dSRodney W. Grimes 		m0 = m->m_nextpkt;
371df8bae1dSRodney W. Grimes 		m->m_nextpkt = 0;
372df8bae1dSRodney W. Grimes 		if (error == 0)
373df8bae1dSRodney W. Grimes 			error = (*ifp->if_output)(ifp, m,
374df8bae1dSRodney W. Grimes 			    (struct sockaddr *)dst, ro->ro_rt);
375df8bae1dSRodney W. Grimes 		else
376df8bae1dSRodney W. Grimes 			m_freem(m);
377df8bae1dSRodney W. Grimes 	}
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes 	if (error == 0)
380df8bae1dSRodney W. Grimes 		ipstat.ips_fragmented++;
381df8bae1dSRodney W. Grimes     }
382df8bae1dSRodney W. Grimes done:
383df8bae1dSRodney W. Grimes 	if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
384df8bae1dSRodney W. Grimes 		RTFREE(ro->ro_rt);
385df8bae1dSRodney W. Grimes 	return (error);
386df8bae1dSRodney W. Grimes bad:
387df8bae1dSRodney W. Grimes 	m_freem(m0);
388df8bae1dSRodney W. Grimes 	goto done;
389df8bae1dSRodney W. Grimes }
390df8bae1dSRodney W. Grimes 
391df8bae1dSRodney W. Grimes /*
392df8bae1dSRodney W. Grimes  * Insert IP options into preformed packet.
393df8bae1dSRodney W. Grimes  * Adjust IP destination as required for IP source routing,
394df8bae1dSRodney W. Grimes  * as indicated by a non-zero in_addr at the start of the options.
395df8bae1dSRodney W. Grimes  */
396df8bae1dSRodney W. Grimes static struct mbuf *
397df8bae1dSRodney W. Grimes ip_insertoptions(m, opt, phlen)
398df8bae1dSRodney W. Grimes 	register struct mbuf *m;
399df8bae1dSRodney W. Grimes 	struct mbuf *opt;
400df8bae1dSRodney W. Grimes 	int *phlen;
401df8bae1dSRodney W. Grimes {
402df8bae1dSRodney W. Grimes 	register struct ipoption *p = mtod(opt, struct ipoption *);
403df8bae1dSRodney W. Grimes 	struct mbuf *n;
404df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
405df8bae1dSRodney W. Grimes 	unsigned optlen;
406df8bae1dSRodney W. Grimes 
407df8bae1dSRodney W. Grimes 	optlen = opt->m_len - sizeof(p->ipopt_dst);
408df8bae1dSRodney W. Grimes 	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
409df8bae1dSRodney W. Grimes 		return (m);		/* XXX should fail */
410df8bae1dSRodney W. Grimes 	if (p->ipopt_dst.s_addr)
411df8bae1dSRodney W. Grimes 		ip->ip_dst = p->ipopt_dst;
412df8bae1dSRodney W. Grimes 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
413df8bae1dSRodney W. Grimes 		MGETHDR(n, M_DONTWAIT, MT_HEADER);
414df8bae1dSRodney W. Grimes 		if (n == 0)
415df8bae1dSRodney W. Grimes 			return (m);
416df8bae1dSRodney W. Grimes 		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
417df8bae1dSRodney W. Grimes 		m->m_len -= sizeof(struct ip);
418df8bae1dSRodney W. Grimes 		m->m_data += sizeof(struct ip);
419df8bae1dSRodney W. Grimes 		n->m_next = m;
420df8bae1dSRodney W. Grimes 		m = n;
421df8bae1dSRodney W. Grimes 		m->m_len = optlen + sizeof(struct ip);
422df8bae1dSRodney W. Grimes 		m->m_data += max_linkhdr;
423df8bae1dSRodney W. Grimes 		bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
424df8bae1dSRodney W. Grimes 	} else {
425df8bae1dSRodney W. Grimes 		m->m_data -= optlen;
426df8bae1dSRodney W. Grimes 		m->m_len += optlen;
427df8bae1dSRodney W. Grimes 		m->m_pkthdr.len += optlen;
428df8bae1dSRodney W. Grimes 		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
429df8bae1dSRodney W. Grimes 	}
430df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
431df8bae1dSRodney W. Grimes 	bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
432df8bae1dSRodney W. Grimes 	*phlen = sizeof(struct ip) + optlen;
433df8bae1dSRodney W. Grimes 	ip->ip_len += optlen;
434df8bae1dSRodney W. Grimes 	return (m);
435df8bae1dSRodney W. Grimes }
436df8bae1dSRodney W. Grimes 
437df8bae1dSRodney W. Grimes /*
438df8bae1dSRodney W. Grimes  * Copy options from ip to jp,
439df8bae1dSRodney W. Grimes  * omitting those not copied during fragmentation.
440df8bae1dSRodney W. Grimes  */
441df8bae1dSRodney W. Grimes int
442df8bae1dSRodney W. Grimes ip_optcopy(ip, jp)
443df8bae1dSRodney W. Grimes 	struct ip *ip, *jp;
444df8bae1dSRodney W. Grimes {
445df8bae1dSRodney W. Grimes 	register u_char *cp, *dp;
446df8bae1dSRodney W. Grimes 	int opt, optlen, cnt;
447df8bae1dSRodney W. Grimes 
448df8bae1dSRodney W. Grimes 	cp = (u_char *)(ip + 1);
449df8bae1dSRodney W. Grimes 	dp = (u_char *)(jp + 1);
450df8bae1dSRodney W. Grimes 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
451df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
452df8bae1dSRodney W. Grimes 		opt = cp[0];
453df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
454df8bae1dSRodney W. Grimes 			break;
455df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP) {
456df8bae1dSRodney W. Grimes 			/* Preserve for IP mcast tunnel's LSRR alignment. */
457df8bae1dSRodney W. Grimes 			*dp++ = IPOPT_NOP;
458df8bae1dSRodney W. Grimes 			optlen = 1;
459df8bae1dSRodney W. Grimes 			continue;
460df8bae1dSRodney W. Grimes 		} else
461df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
462df8bae1dSRodney W. Grimes 		/* bogus lengths should have been caught by ip_dooptions */
463df8bae1dSRodney W. Grimes 		if (optlen > cnt)
464df8bae1dSRodney W. Grimes 			optlen = cnt;
465df8bae1dSRodney W. Grimes 		if (IPOPT_COPIED(opt)) {
466df8bae1dSRodney W. Grimes 			bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
467df8bae1dSRodney W. Grimes 			dp += optlen;
468df8bae1dSRodney W. Grimes 		}
469df8bae1dSRodney W. Grimes 	}
470df8bae1dSRodney W. Grimes 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
471df8bae1dSRodney W. Grimes 		*dp++ = IPOPT_EOL;
472df8bae1dSRodney W. Grimes 	return (optlen);
473df8bae1dSRodney W. Grimes }
474df8bae1dSRodney W. Grimes 
475df8bae1dSRodney W. Grimes /*
476df8bae1dSRodney W. Grimes  * IP socket option processing.
477df8bae1dSRodney W. Grimes  */
478df8bae1dSRodney W. Grimes int
479df8bae1dSRodney W. Grimes ip_ctloutput(op, so, level, optname, mp)
480df8bae1dSRodney W. Grimes 	int op;
481df8bae1dSRodney W. Grimes 	struct socket *so;
482df8bae1dSRodney W. Grimes 	int level, optname;
483df8bae1dSRodney W. Grimes 	struct mbuf **mp;
484df8bae1dSRodney W. Grimes {
485df8bae1dSRodney W. Grimes 	register struct inpcb *inp = sotoinpcb(so);
486df8bae1dSRodney W. Grimes 	register struct mbuf *m = *mp;
48726f9a767SRodney W. Grimes 	register int optval = 0;
488df8bae1dSRodney W. Grimes 	int error = 0;
489df8bae1dSRodney W. Grimes 
490df8bae1dSRodney W. Grimes 	if (level != IPPROTO_IP) {
491df8bae1dSRodney W. Grimes 		error = EINVAL;
492df8bae1dSRodney W. Grimes 		if (op == PRCO_SETOPT && *mp)
493df8bae1dSRodney W. Grimes 			(void) m_free(*mp);
494df8bae1dSRodney W. Grimes 	} else switch (op) {
495df8bae1dSRodney W. Grimes 
496df8bae1dSRodney W. Grimes 	case PRCO_SETOPT:
497df8bae1dSRodney W. Grimes 		switch (optname) {
498df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
499df8bae1dSRodney W. Grimes #ifdef notyet
500df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
501df8bae1dSRodney W. Grimes 			return (ip_pcbopts(optname, &inp->inp_options, m));
502df8bae1dSRodney W. Grimes #else
503df8bae1dSRodney W. Grimes 			return (ip_pcbopts(&inp->inp_options, m));
504df8bae1dSRodney W. Grimes #endif
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 		case IP_TOS:
507df8bae1dSRodney W. Grimes 		case IP_TTL:
508df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
509df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
510df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
511df8bae1dSRodney W. Grimes 			if (m->m_len != sizeof(int))
512df8bae1dSRodney W. Grimes 				error = EINVAL;
513df8bae1dSRodney W. Grimes 			else {
514df8bae1dSRodney W. Grimes 				optval = *mtod(m, int *);
515df8bae1dSRodney W. Grimes 				switch (optname) {
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes 				case IP_TOS:
518df8bae1dSRodney W. Grimes 					inp->inp_ip.ip_tos = optval;
519df8bae1dSRodney W. Grimes 					break;
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 				case IP_TTL:
522df8bae1dSRodney W. Grimes 					inp->inp_ip.ip_ttl = optval;
523df8bae1dSRodney W. Grimes 					break;
524df8bae1dSRodney W. Grimes #define	OPTSET(bit) \
525df8bae1dSRodney W. Grimes 	if (optval) \
526df8bae1dSRodney W. Grimes 		inp->inp_flags |= bit; \
527df8bae1dSRodney W. Grimes 	else \
528df8bae1dSRodney W. Grimes 		inp->inp_flags &= ~bit;
529df8bae1dSRodney W. Grimes 
530df8bae1dSRodney W. Grimes 				case IP_RECVOPTS:
531df8bae1dSRodney W. Grimes 					OPTSET(INP_RECVOPTS);
532df8bae1dSRodney W. Grimes 					break;
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 				case IP_RECVRETOPTS:
535df8bae1dSRodney W. Grimes 					OPTSET(INP_RECVRETOPTS);
536df8bae1dSRodney W. Grimes 					break;
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes 				case IP_RECVDSTADDR:
539df8bae1dSRodney W. Grimes 					OPTSET(INP_RECVDSTADDR);
540df8bae1dSRodney W. Grimes 					break;
541df8bae1dSRodney W. Grimes 				}
542df8bae1dSRodney W. Grimes 			}
543df8bae1dSRodney W. Grimes 			break;
544df8bae1dSRodney W. Grimes #undef OPTSET
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
547df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
548df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
549df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
550df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
551df8bae1dSRodney W. Grimes 			error = ip_setmoptions(optname, &inp->inp_moptions, m);
552df8bae1dSRodney W. Grimes 			break;
553df8bae1dSRodney W. Grimes 
554df8bae1dSRodney W. Grimes 		default:
555df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
556df8bae1dSRodney W. Grimes 			break;
557df8bae1dSRodney W. Grimes 		}
558df8bae1dSRodney W. Grimes 		if (m)
559df8bae1dSRodney W. Grimes 			(void)m_free(m);
560df8bae1dSRodney W. Grimes 		break;
561df8bae1dSRodney W. Grimes 
562df8bae1dSRodney W. Grimes 	case PRCO_GETOPT:
563df8bae1dSRodney W. Grimes 		switch (optname) {
564df8bae1dSRodney W. Grimes 		case IP_OPTIONS:
565df8bae1dSRodney W. Grimes 		case IP_RETOPTS:
566df8bae1dSRodney W. Grimes 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
567df8bae1dSRodney W. Grimes 			if (inp->inp_options) {
568df8bae1dSRodney W. Grimes 				m->m_len = inp->inp_options->m_len;
569df8bae1dSRodney W. Grimes 				bcopy(mtod(inp->inp_options, caddr_t),
570df8bae1dSRodney W. Grimes 				    mtod(m, caddr_t), (unsigned)m->m_len);
571df8bae1dSRodney W. Grimes 			} else
572df8bae1dSRodney W. Grimes 				m->m_len = 0;
573df8bae1dSRodney W. Grimes 			break;
574df8bae1dSRodney W. Grimes 
575df8bae1dSRodney W. Grimes 		case IP_TOS:
576df8bae1dSRodney W. Grimes 		case IP_TTL:
577df8bae1dSRodney W. Grimes 		case IP_RECVOPTS:
578df8bae1dSRodney W. Grimes 		case IP_RECVRETOPTS:
579df8bae1dSRodney W. Grimes 		case IP_RECVDSTADDR:
580df8bae1dSRodney W. Grimes 			*mp = m = m_get(M_WAIT, MT_SOOPTS);
581df8bae1dSRodney W. Grimes 			m->m_len = sizeof(int);
582df8bae1dSRodney W. Grimes 			switch (optname) {
583df8bae1dSRodney W. Grimes 
584df8bae1dSRodney W. Grimes 			case IP_TOS:
585df8bae1dSRodney W. Grimes 				optval = inp->inp_ip.ip_tos;
586df8bae1dSRodney W. Grimes 				break;
587df8bae1dSRodney W. Grimes 
588df8bae1dSRodney W. Grimes 			case IP_TTL:
589df8bae1dSRodney W. Grimes 				optval = inp->inp_ip.ip_ttl;
590df8bae1dSRodney W. Grimes 				break;
591df8bae1dSRodney W. Grimes 
592df8bae1dSRodney W. Grimes #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
593df8bae1dSRodney W. Grimes 
594df8bae1dSRodney W. Grimes 			case IP_RECVOPTS:
595df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVOPTS);
596df8bae1dSRodney W. Grimes 				break;
597df8bae1dSRodney W. Grimes 
598df8bae1dSRodney W. Grimes 			case IP_RECVRETOPTS:
599df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVRETOPTS);
600df8bae1dSRodney W. Grimes 				break;
601df8bae1dSRodney W. Grimes 
602df8bae1dSRodney W. Grimes 			case IP_RECVDSTADDR:
603df8bae1dSRodney W. Grimes 				optval = OPTBIT(INP_RECVDSTADDR);
604df8bae1dSRodney W. Grimes 				break;
605df8bae1dSRodney W. Grimes 			}
606df8bae1dSRodney W. Grimes 			*mtod(m, int *) = optval;
607df8bae1dSRodney W. Grimes 			break;
608df8bae1dSRodney W. Grimes 
609df8bae1dSRodney W. Grimes 		case IP_MULTICAST_IF:
610df8bae1dSRodney W. Grimes 		case IP_MULTICAST_TTL:
611df8bae1dSRodney W. Grimes 		case IP_MULTICAST_LOOP:
612df8bae1dSRodney W. Grimes 		case IP_ADD_MEMBERSHIP:
613df8bae1dSRodney W. Grimes 		case IP_DROP_MEMBERSHIP:
614df8bae1dSRodney W. Grimes 			error = ip_getmoptions(optname, inp->inp_moptions, mp);
615df8bae1dSRodney W. Grimes 			break;
616df8bae1dSRodney W. Grimes 
617df8bae1dSRodney W. Grimes 		default:
618df8bae1dSRodney W. Grimes 			error = ENOPROTOOPT;
619df8bae1dSRodney W. Grimes 			break;
620df8bae1dSRodney W. Grimes 		}
621df8bae1dSRodney W. Grimes 		break;
622df8bae1dSRodney W. Grimes 	}
623df8bae1dSRodney W. Grimes 	return (error);
624df8bae1dSRodney W. Grimes }
625df8bae1dSRodney W. Grimes 
626df8bae1dSRodney W. Grimes /*
627df8bae1dSRodney W. Grimes  * Set up IP options in pcb for insertion in output packets.
628df8bae1dSRodney W. Grimes  * Store in mbuf with pointer in pcbopt, adding pseudo-option
629df8bae1dSRodney W. Grimes  * with destination address if source routed.
630df8bae1dSRodney W. Grimes  */
631df8bae1dSRodney W. Grimes int
632df8bae1dSRodney W. Grimes #ifdef notyet
633df8bae1dSRodney W. Grimes ip_pcbopts(optname, pcbopt, m)
634df8bae1dSRodney W. Grimes 	int optname;
635df8bae1dSRodney W. Grimes #else
636df8bae1dSRodney W. Grimes ip_pcbopts(pcbopt, m)
637df8bae1dSRodney W. Grimes #endif
638df8bae1dSRodney W. Grimes 	struct mbuf **pcbopt;
639df8bae1dSRodney W. Grimes 	register struct mbuf *m;
640df8bae1dSRodney W. Grimes {
641df8bae1dSRodney W. Grimes 	register cnt, optlen;
642df8bae1dSRodney W. Grimes 	register u_char *cp;
643df8bae1dSRodney W. Grimes 	u_char opt;
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes 	/* turn off any old options */
646df8bae1dSRodney W. Grimes 	if (*pcbopt)
647df8bae1dSRodney W. Grimes 		(void)m_free(*pcbopt);
648df8bae1dSRodney W. Grimes 	*pcbopt = 0;
649df8bae1dSRodney W. Grimes 	if (m == (struct mbuf *)0 || m->m_len == 0) {
650df8bae1dSRodney W. Grimes 		/*
651df8bae1dSRodney W. Grimes 		 * Only turning off any previous options.
652df8bae1dSRodney W. Grimes 		 */
653df8bae1dSRodney W. Grimes 		if (m)
654df8bae1dSRodney W. Grimes 			(void)m_free(m);
655df8bae1dSRodney W. Grimes 		return (0);
656df8bae1dSRodney W. Grimes 	}
657df8bae1dSRodney W. Grimes 
658df8bae1dSRodney W. Grimes #ifndef	vax
659df8bae1dSRodney W. Grimes 	if (m->m_len % sizeof(long))
660df8bae1dSRodney W. Grimes 		goto bad;
661df8bae1dSRodney W. Grimes #endif
662df8bae1dSRodney W. Grimes 	/*
663df8bae1dSRodney W. Grimes 	 * IP first-hop destination address will be stored before
664df8bae1dSRodney W. Grimes 	 * actual options; move other options back
665df8bae1dSRodney W. Grimes 	 * and clear it when none present.
666df8bae1dSRodney W. Grimes 	 */
667df8bae1dSRodney W. Grimes 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
668df8bae1dSRodney W. Grimes 		goto bad;
669df8bae1dSRodney W. Grimes 	cnt = m->m_len;
670df8bae1dSRodney W. Grimes 	m->m_len += sizeof(struct in_addr);
671df8bae1dSRodney W. Grimes 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
672df8bae1dSRodney W. Grimes 	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
673df8bae1dSRodney W. Grimes 	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
674df8bae1dSRodney W. Grimes 
675df8bae1dSRodney W. Grimes 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
676df8bae1dSRodney W. Grimes 		opt = cp[IPOPT_OPTVAL];
677df8bae1dSRodney W. Grimes 		if (opt == IPOPT_EOL)
678df8bae1dSRodney W. Grimes 			break;
679df8bae1dSRodney W. Grimes 		if (opt == IPOPT_NOP)
680df8bae1dSRodney W. Grimes 			optlen = 1;
681df8bae1dSRodney W. Grimes 		else {
682df8bae1dSRodney W. Grimes 			optlen = cp[IPOPT_OLEN];
683df8bae1dSRodney W. Grimes 			if (optlen <= IPOPT_OLEN || optlen > cnt)
684df8bae1dSRodney W. Grimes 				goto bad;
685df8bae1dSRodney W. Grimes 		}
686df8bae1dSRodney W. Grimes 		switch (opt) {
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 		default:
689df8bae1dSRodney W. Grimes 			break;
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes 		case IPOPT_LSRR:
692df8bae1dSRodney W. Grimes 		case IPOPT_SSRR:
693df8bae1dSRodney W. Grimes 			/*
694df8bae1dSRodney W. Grimes 			 * user process specifies route as:
695df8bae1dSRodney W. Grimes 			 *	->A->B->C->D
696df8bae1dSRodney W. Grimes 			 * D must be our final destination (but we can't
697df8bae1dSRodney W. Grimes 			 * check that since we may not have connected yet).
698df8bae1dSRodney W. Grimes 			 * A is first hop destination, which doesn't appear in
699df8bae1dSRodney W. Grimes 			 * actual IP option, but is stored before the options.
700df8bae1dSRodney W. Grimes 			 */
701df8bae1dSRodney W. Grimes 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
702df8bae1dSRodney W. Grimes 				goto bad;
703df8bae1dSRodney W. Grimes 			m->m_len -= sizeof(struct in_addr);
704df8bae1dSRodney W. Grimes 			cnt -= sizeof(struct in_addr);
705df8bae1dSRodney W. Grimes 			optlen -= sizeof(struct in_addr);
706df8bae1dSRodney W. Grimes 			cp[IPOPT_OLEN] = optlen;
707df8bae1dSRodney W. Grimes 			/*
708df8bae1dSRodney W. Grimes 			 * Move first hop before start of options.
709df8bae1dSRodney W. Grimes 			 */
710df8bae1dSRodney W. Grimes 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
711df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr));
712df8bae1dSRodney W. Grimes 			/*
713df8bae1dSRodney W. Grimes 			 * Then copy rest of options back
714df8bae1dSRodney W. Grimes 			 * to close up the deleted entry.
715df8bae1dSRodney W. Grimes 			 */
716df8bae1dSRodney W. Grimes 			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
717df8bae1dSRodney W. Grimes 			    sizeof(struct in_addr)),
718df8bae1dSRodney W. Grimes 			    (caddr_t)&cp[IPOPT_OFFSET+1],
719df8bae1dSRodney W. Grimes 			    (unsigned)cnt + sizeof(struct in_addr));
720df8bae1dSRodney W. Grimes 			break;
721df8bae1dSRodney W. Grimes 		}
722df8bae1dSRodney W. Grimes 	}
723df8bae1dSRodney W. Grimes 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
724df8bae1dSRodney W. Grimes 		goto bad;
725df8bae1dSRodney W. Grimes 	*pcbopt = m;
726df8bae1dSRodney W. Grimes 	return (0);
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes bad:
729df8bae1dSRodney W. Grimes 	(void)m_free(m);
730df8bae1dSRodney W. Grimes 	return (EINVAL);
731df8bae1dSRodney W. Grimes }
732df8bae1dSRodney W. Grimes 
733df8bae1dSRodney W. Grimes /*
734df8bae1dSRodney W. Grimes  * Set the IP multicast options in response to user setsockopt().
735df8bae1dSRodney W. Grimes  */
736df8bae1dSRodney W. Grimes int
737df8bae1dSRodney W. Grimes ip_setmoptions(optname, imop, m)
738df8bae1dSRodney W. Grimes 	int optname;
739df8bae1dSRodney W. Grimes 	struct ip_moptions **imop;
740df8bae1dSRodney W. Grimes 	struct mbuf *m;
741df8bae1dSRodney W. Grimes {
742df8bae1dSRodney W. Grimes 	register int error = 0;
743df8bae1dSRodney W. Grimes 	u_char loop;
744df8bae1dSRodney W. Grimes 	register int i;
745df8bae1dSRodney W. Grimes 	struct in_addr addr;
746df8bae1dSRodney W. Grimes 	register struct ip_mreq *mreq;
747df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
748df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo = *imop;
749df8bae1dSRodney W. Grimes 	struct route ro;
750df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
751df8bae1dSRodney W. Grimes 
752df8bae1dSRodney W. Grimes 	if (imo == NULL) {
753df8bae1dSRodney W. Grimes 		/*
754df8bae1dSRodney W. Grimes 		 * No multicast option buffer attached to the pcb;
755df8bae1dSRodney W. Grimes 		 * allocate one and initialize to default values.
756df8bae1dSRodney W. Grimes 		 */
757df8bae1dSRodney W. Grimes 		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
758df8bae1dSRodney W. Grimes 		    M_WAITOK);
759df8bae1dSRodney W. Grimes 
760df8bae1dSRodney W. Grimes 		if (imo == NULL)
761df8bae1dSRodney W. Grimes 			return (ENOBUFS);
762df8bae1dSRodney W. Grimes 		*imop = imo;
763df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = NULL;
764df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
765df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
766df8bae1dSRodney W. Grimes 		imo->imo_num_memberships = 0;
767df8bae1dSRodney W. Grimes 	}
768df8bae1dSRodney W. Grimes 
769df8bae1dSRodney W. Grimes 	switch (optname) {
770df8bae1dSRodney W. Grimes 
771df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
772df8bae1dSRodney W. Grimes 		/*
773df8bae1dSRodney W. Grimes 		 * Select the interface for outgoing multicast packets.
774df8bae1dSRodney W. Grimes 		 */
775df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != sizeof(struct in_addr)) {
776df8bae1dSRodney W. Grimes 			error = EINVAL;
777df8bae1dSRodney W. Grimes 			break;
778df8bae1dSRodney W. Grimes 		}
779df8bae1dSRodney W. Grimes 		addr = *(mtod(m, struct in_addr *));
780df8bae1dSRodney W. Grimes 		/*
781df8bae1dSRodney W. Grimes 		 * INADDR_ANY is used to remove a previous selection.
782df8bae1dSRodney W. Grimes 		 * When no interface is selected, a default one is
783df8bae1dSRodney W. Grimes 		 * chosen every time a multicast packet is sent.
784df8bae1dSRodney W. Grimes 		 */
785df8bae1dSRodney W. Grimes 		if (addr.s_addr == INADDR_ANY) {
786df8bae1dSRodney W. Grimes 			imo->imo_multicast_ifp = NULL;
787df8bae1dSRodney W. Grimes 			break;
788df8bae1dSRodney W. Grimes 		}
789df8bae1dSRodney W. Grimes 		/*
790df8bae1dSRodney W. Grimes 		 * The selected interface is identified by its local
791df8bae1dSRodney W. Grimes 		 * IP address.  Find the interface and confirm that
792df8bae1dSRodney W. Grimes 		 * it supports multicasting.
793df8bae1dSRodney W. Grimes 		 */
794df8bae1dSRodney W. Grimes 		INADDR_TO_IFP(addr, ifp);
795df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
796df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
797df8bae1dSRodney W. Grimes 			break;
798df8bae1dSRodney W. Grimes 		}
799df8bae1dSRodney W. Grimes 		imo->imo_multicast_ifp = ifp;
800df8bae1dSRodney W. Grimes 		break;
801df8bae1dSRodney W. Grimes 
802df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
803df8bae1dSRodney W. Grimes 		/*
804df8bae1dSRodney W. Grimes 		 * Set the IP time-to-live for outgoing multicast packets.
805df8bae1dSRodney W. Grimes 		 */
806df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != 1) {
807df8bae1dSRodney W. Grimes 			error = EINVAL;
808df8bae1dSRodney W. Grimes 			break;
809df8bae1dSRodney W. Grimes 		}
810df8bae1dSRodney W. Grimes 		imo->imo_multicast_ttl = *(mtod(m, u_char *));
811df8bae1dSRodney W. Grimes 		break;
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
814df8bae1dSRodney W. Grimes 		/*
815df8bae1dSRodney W. Grimes 		 * Set the loopback flag for outgoing multicast packets.
816df8bae1dSRodney W. Grimes 		 * Must be zero or one.
817df8bae1dSRodney W. Grimes 		 */
818df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != 1 ||
819df8bae1dSRodney W. Grimes 		   (loop = *(mtod(m, u_char *))) > 1) {
820df8bae1dSRodney W. Grimes 			error = EINVAL;
821df8bae1dSRodney W. Grimes 			break;
822df8bae1dSRodney W. Grimes 		}
823df8bae1dSRodney W. Grimes 		imo->imo_multicast_loop = loop;
824df8bae1dSRodney W. Grimes 		break;
825df8bae1dSRodney W. Grimes 
826df8bae1dSRodney W. Grimes 	case IP_ADD_MEMBERSHIP:
827df8bae1dSRodney W. Grimes 		/*
828df8bae1dSRodney W. Grimes 		 * Add a multicast group membership.
829df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
830df8bae1dSRodney W. Grimes 		 */
831df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
832df8bae1dSRodney W. Grimes 			error = EINVAL;
833df8bae1dSRodney W. Grimes 			break;
834df8bae1dSRodney W. Grimes 		}
835df8bae1dSRodney W. Grimes 		mreq = mtod(m, struct ip_mreq *);
836df8bae1dSRodney W. Grimes 		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
837df8bae1dSRodney W. Grimes 			error = EINVAL;
838df8bae1dSRodney W. Grimes 			break;
839df8bae1dSRodney W. Grimes 		}
840df8bae1dSRodney W. Grimes 		/*
841df8bae1dSRodney W. Grimes 		 * If no interface address was provided, use the interface of
842df8bae1dSRodney W. Grimes 		 * the route to the given multicast address.
843df8bae1dSRodney W. Grimes 		 */
844df8bae1dSRodney W. Grimes 		if (mreq->imr_interface.s_addr == INADDR_ANY) {
845df8bae1dSRodney W. Grimes 			ro.ro_rt = NULL;
846df8bae1dSRodney W. Grimes 			dst = (struct sockaddr_in *)&ro.ro_dst;
847df8bae1dSRodney W. Grimes 			dst->sin_len = sizeof(*dst);
848df8bae1dSRodney W. Grimes 			dst->sin_family = AF_INET;
849df8bae1dSRodney W. Grimes 			dst->sin_addr = mreq->imr_multiaddr;
850df8bae1dSRodney W. Grimes 			rtalloc(&ro);
851df8bae1dSRodney W. Grimes 			if (ro.ro_rt == NULL) {
852df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
853df8bae1dSRodney W. Grimes 				break;
854df8bae1dSRodney W. Grimes 			}
855df8bae1dSRodney W. Grimes 			ifp = ro.ro_rt->rt_ifp;
856df8bae1dSRodney W. Grimes 			rtfree(ro.ro_rt);
857df8bae1dSRodney W. Grimes 		}
858df8bae1dSRodney W. Grimes 		else {
859df8bae1dSRodney W. Grimes 			INADDR_TO_IFP(mreq->imr_interface, ifp);
860df8bae1dSRodney W. Grimes 		}
861df8bae1dSRodney W. Grimes 		/*
862df8bae1dSRodney W. Grimes 		 * See if we found an interface, and confirm that it
863df8bae1dSRodney W. Grimes 		 * supports multicast.
864df8bae1dSRodney W. Grimes 		 */
865df8bae1dSRodney W. Grimes 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
866df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
867df8bae1dSRodney W. Grimes 			break;
868df8bae1dSRodney W. Grimes 		}
869df8bae1dSRodney W. Grimes 		/*
870df8bae1dSRodney W. Grimes 		 * See if the membership already exists or if all the
871df8bae1dSRodney W. Grimes 		 * membership slots are full.
872df8bae1dSRodney W. Grimes 		 */
873df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
874df8bae1dSRodney W. Grimes 			if (imo->imo_membership[i]->inm_ifp == ifp &&
875df8bae1dSRodney W. Grimes 			    imo->imo_membership[i]->inm_addr.s_addr
876df8bae1dSRodney W. Grimes 						== mreq->imr_multiaddr.s_addr)
877df8bae1dSRodney W. Grimes 				break;
878df8bae1dSRodney W. Grimes 		}
879df8bae1dSRodney W. Grimes 		if (i < imo->imo_num_memberships) {
880df8bae1dSRodney W. Grimes 			error = EADDRINUSE;
881df8bae1dSRodney W. Grimes 			break;
882df8bae1dSRodney W. Grimes 		}
883df8bae1dSRodney W. Grimes 		if (i == IP_MAX_MEMBERSHIPS) {
884df8bae1dSRodney W. Grimes 			error = ETOOMANYREFS;
885df8bae1dSRodney W. Grimes 			break;
886df8bae1dSRodney W. Grimes 		}
887df8bae1dSRodney W. Grimes 		/*
888df8bae1dSRodney W. Grimes 		 * Everything looks good; add a new record to the multicast
889df8bae1dSRodney W. Grimes 		 * address list for the given interface.
890df8bae1dSRodney W. Grimes 		 */
891df8bae1dSRodney W. Grimes 		if ((imo->imo_membership[i] =
892df8bae1dSRodney W. Grimes 		    in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
893df8bae1dSRodney W. Grimes 			error = ENOBUFS;
894df8bae1dSRodney W. Grimes 			break;
895df8bae1dSRodney W. Grimes 		}
896df8bae1dSRodney W. Grimes 		++imo->imo_num_memberships;
897df8bae1dSRodney W. Grimes 		break;
898df8bae1dSRodney W. Grimes 
899df8bae1dSRodney W. Grimes 	case IP_DROP_MEMBERSHIP:
900df8bae1dSRodney W. Grimes 		/*
901df8bae1dSRodney W. Grimes 		 * Drop a multicast group membership.
902df8bae1dSRodney W. Grimes 		 * Group must be a valid IP multicast address.
903df8bae1dSRodney W. Grimes 		 */
904df8bae1dSRodney W. Grimes 		if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
905df8bae1dSRodney W. Grimes 			error = EINVAL;
906df8bae1dSRodney W. Grimes 			break;
907df8bae1dSRodney W. Grimes 		}
908df8bae1dSRodney W. Grimes 		mreq = mtod(m, struct ip_mreq *);
909df8bae1dSRodney W. Grimes 		if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
910df8bae1dSRodney W. Grimes 			error = EINVAL;
911df8bae1dSRodney W. Grimes 			break;
912df8bae1dSRodney W. Grimes 		}
913df8bae1dSRodney W. Grimes 		/*
914df8bae1dSRodney W. Grimes 		 * If an interface address was specified, get a pointer
915df8bae1dSRodney W. Grimes 		 * to its ifnet structure.
916df8bae1dSRodney W. Grimes 		 */
917df8bae1dSRodney W. Grimes 		if (mreq->imr_interface.s_addr == INADDR_ANY)
918df8bae1dSRodney W. Grimes 			ifp = NULL;
919df8bae1dSRodney W. Grimes 		else {
920df8bae1dSRodney W. Grimes 			INADDR_TO_IFP(mreq->imr_interface, ifp);
921df8bae1dSRodney W. Grimes 			if (ifp == NULL) {
922df8bae1dSRodney W. Grimes 				error = EADDRNOTAVAIL;
923df8bae1dSRodney W. Grimes 				break;
924df8bae1dSRodney W. Grimes 			}
925df8bae1dSRodney W. Grimes 		}
926df8bae1dSRodney W. Grimes 		/*
927df8bae1dSRodney W. Grimes 		 * Find the membership in the membership array.
928df8bae1dSRodney W. Grimes 		 */
929df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i) {
930df8bae1dSRodney W. Grimes 			if ((ifp == NULL ||
931df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_ifp == ifp) &&
932df8bae1dSRodney W. Grimes 			     imo->imo_membership[i]->inm_addr.s_addr ==
933df8bae1dSRodney W. Grimes 			     mreq->imr_multiaddr.s_addr)
934df8bae1dSRodney W. Grimes 				break;
935df8bae1dSRodney W. Grimes 		}
936df8bae1dSRodney W. Grimes 		if (i == imo->imo_num_memberships) {
937df8bae1dSRodney W. Grimes 			error = EADDRNOTAVAIL;
938df8bae1dSRodney W. Grimes 			break;
939df8bae1dSRodney W. Grimes 		}
940df8bae1dSRodney W. Grimes 		/*
941df8bae1dSRodney W. Grimes 		 * Give up the multicast address record to which the
942df8bae1dSRodney W. Grimes 		 * membership points.
943df8bae1dSRodney W. Grimes 		 */
944df8bae1dSRodney W. Grimes 		in_delmulti(imo->imo_membership[i]);
945df8bae1dSRodney W. Grimes 		/*
946df8bae1dSRodney W. Grimes 		 * Remove the gap in the membership array.
947df8bae1dSRodney W. Grimes 		 */
948df8bae1dSRodney W. Grimes 		for (++i; i < imo->imo_num_memberships; ++i)
949df8bae1dSRodney W. Grimes 			imo->imo_membership[i-1] = imo->imo_membership[i];
950df8bae1dSRodney W. Grimes 		--imo->imo_num_memberships;
951df8bae1dSRodney W. Grimes 		break;
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 	default:
954df8bae1dSRodney W. Grimes 		error = EOPNOTSUPP;
955df8bae1dSRodney W. Grimes 		break;
956df8bae1dSRodney W. Grimes 	}
957df8bae1dSRodney W. Grimes 
958df8bae1dSRodney W. Grimes 	/*
959df8bae1dSRodney W. Grimes 	 * If all options have default values, no need to keep the mbuf.
960df8bae1dSRodney W. Grimes 	 */
961df8bae1dSRodney W. Grimes 	if (imo->imo_multicast_ifp == NULL &&
962df8bae1dSRodney W. Grimes 	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
963df8bae1dSRodney W. Grimes 	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
964df8bae1dSRodney W. Grimes 	    imo->imo_num_memberships == 0) {
965df8bae1dSRodney W. Grimes 		free(*imop, M_IPMOPTS);
966df8bae1dSRodney W. Grimes 		*imop = NULL;
967df8bae1dSRodney W. Grimes 	}
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes 	return (error);
970df8bae1dSRodney W. Grimes }
971df8bae1dSRodney W. Grimes 
972df8bae1dSRodney W. Grimes /*
973df8bae1dSRodney W. Grimes  * Return the IP multicast options in response to user getsockopt().
974df8bae1dSRodney W. Grimes  */
975df8bae1dSRodney W. Grimes int
976df8bae1dSRodney W. Grimes ip_getmoptions(optname, imo, mp)
977df8bae1dSRodney W. Grimes 	int optname;
978df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
979df8bae1dSRodney W. Grimes 	register struct mbuf **mp;
980df8bae1dSRodney W. Grimes {
981df8bae1dSRodney W. Grimes 	u_char *ttl;
982df8bae1dSRodney W. Grimes 	u_char *loop;
983df8bae1dSRodney W. Grimes 	struct in_addr *addr;
984df8bae1dSRodney W. Grimes 	struct in_ifaddr *ia;
985df8bae1dSRodney W. Grimes 
986df8bae1dSRodney W. Grimes 	*mp = m_get(M_WAIT, MT_SOOPTS);
987df8bae1dSRodney W. Grimes 
988df8bae1dSRodney W. Grimes 	switch (optname) {
989df8bae1dSRodney W. Grimes 
990df8bae1dSRodney W. Grimes 	case IP_MULTICAST_IF:
991df8bae1dSRodney W. Grimes 		addr = mtod(*mp, struct in_addr *);
992df8bae1dSRodney W. Grimes 		(*mp)->m_len = sizeof(struct in_addr);
993df8bae1dSRodney W. Grimes 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
994df8bae1dSRodney W. Grimes 			addr->s_addr = INADDR_ANY;
995df8bae1dSRodney W. Grimes 		else {
996df8bae1dSRodney W. Grimes 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
997df8bae1dSRodney W. Grimes 			addr->s_addr = (ia == NULL) ? INADDR_ANY
998df8bae1dSRodney W. Grimes 					: IA_SIN(ia)->sin_addr.s_addr;
999df8bae1dSRodney W. Grimes 		}
1000df8bae1dSRodney W. Grimes 		return (0);
1001df8bae1dSRodney W. Grimes 
1002df8bae1dSRodney W. Grimes 	case IP_MULTICAST_TTL:
1003df8bae1dSRodney W. Grimes 		ttl = mtod(*mp, u_char *);
1004df8bae1dSRodney W. Grimes 		(*mp)->m_len = 1;
1005df8bae1dSRodney W. Grimes 		*ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1006df8bae1dSRodney W. Grimes 				     : imo->imo_multicast_ttl;
1007df8bae1dSRodney W. Grimes 		return (0);
1008df8bae1dSRodney W. Grimes 
1009df8bae1dSRodney W. Grimes 	case IP_MULTICAST_LOOP:
1010df8bae1dSRodney W. Grimes 		loop = mtod(*mp, u_char *);
1011df8bae1dSRodney W. Grimes 		(*mp)->m_len = 1;
1012df8bae1dSRodney W. Grimes 		*loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1013df8bae1dSRodney W. Grimes 				      : imo->imo_multicast_loop;
1014df8bae1dSRodney W. Grimes 		return (0);
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes 	default:
1017df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
1018df8bae1dSRodney W. Grimes 	}
1019df8bae1dSRodney W. Grimes }
1020df8bae1dSRodney W. Grimes 
1021df8bae1dSRodney W. Grimes /*
1022df8bae1dSRodney W. Grimes  * Discard the IP multicast options.
1023df8bae1dSRodney W. Grimes  */
1024df8bae1dSRodney W. Grimes void
1025df8bae1dSRodney W. Grimes ip_freemoptions(imo)
1026df8bae1dSRodney W. Grimes 	register struct ip_moptions *imo;
1027df8bae1dSRodney W. Grimes {
1028df8bae1dSRodney W. Grimes 	register int i;
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes 	if (imo != NULL) {
1031df8bae1dSRodney W. Grimes 		for (i = 0; i < imo->imo_num_memberships; ++i)
1032df8bae1dSRodney W. Grimes 			in_delmulti(imo->imo_membership[i]);
1033df8bae1dSRodney W. Grimes 		free(imo, M_IPMOPTS);
1034df8bae1dSRodney W. Grimes 	}
1035df8bae1dSRodney W. Grimes }
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes /*
1038df8bae1dSRodney W. Grimes  * Routine called from ip_output() to loop back a copy of an IP multicast
1039df8bae1dSRodney W. Grimes  * packet to the input queue of a specified interface.  Note that this
1040df8bae1dSRodney W. Grimes  * calls the output routine of the loopback "driver", but with an interface
1041df8bae1dSRodney W. Grimes  * pointer that might NOT be &loif -- easier than replicating that code here.
1042df8bae1dSRodney W. Grimes  */
1043df8bae1dSRodney W. Grimes static void
1044df8bae1dSRodney W. Grimes ip_mloopback(ifp, m, dst)
1045df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1046df8bae1dSRodney W. Grimes 	register struct mbuf *m;
1047df8bae1dSRodney W. Grimes 	register struct sockaddr_in *dst;
1048df8bae1dSRodney W. Grimes {
1049df8bae1dSRodney W. Grimes 	register struct ip *ip;
1050df8bae1dSRodney W. Grimes 	struct mbuf *copym;
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes 	copym = m_copy(m, 0, M_COPYALL);
1053df8bae1dSRodney W. Grimes 	if (copym != NULL) {
1054df8bae1dSRodney W. Grimes 		/*
1055df8bae1dSRodney W. Grimes 		 * We don't bother to fragment if the IP length is greater
1056df8bae1dSRodney W. Grimes 		 * than the interface's MTU.  Can this possibly matter?
1057df8bae1dSRodney W. Grimes 		 */
1058df8bae1dSRodney W. Grimes 		ip = mtod(copym, struct ip *);
1059df8bae1dSRodney W. Grimes 		ip->ip_len = htons((u_short)ip->ip_len);
1060df8bae1dSRodney W. Grimes 		ip->ip_off = htons((u_short)ip->ip_off);
1061df8bae1dSRodney W. Grimes 		ip->ip_sum = 0;
1062df8bae1dSRodney W. Grimes 		ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1063df8bae1dSRodney W. Grimes 		(void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1064df8bae1dSRodney W. Grimes 	}
1065df8bae1dSRodney W. Grimes }
1066