xref: /freebsd/sys/netinet/ip_output.c (revision 3fe92528afe8313fecf48822dde74bad5e380f48)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30  * $FreeBSD$
31  */
32 
33 #include "opt_ipfw.h"
34 #include "opt_ipsec.h"
35 #include "opt_mac.h"
36 #include "opt_mbuf_stress_test.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/mac.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
48 
49 #include <net/if.h>
50 #include <net/netisr.h>
51 #include <net/pfil.h>
52 #include <net/route.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_options.h>
61 
62 #if defined(IPSEC) || defined(FAST_IPSEC)
63 #include <netinet/ip_ipsec.h>
64 #ifdef IPSEC
65 #include <netinet6/ipsec.h>
66 #endif
67 #ifdef FAST_IPSEC
68 #include <netipsec/ipsec.h>
69 #endif
70 #endif /*IPSEC*/
71 
72 #include <machine/in_cksum.h>
73 
74 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
75 
76 #define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
77 				x, (ntohl(a.s_addr)>>24)&0xFF,\
78 				  (ntohl(a.s_addr)>>16)&0xFF,\
79 				  (ntohl(a.s_addr)>>8)&0xFF,\
80 				  (ntohl(a.s_addr))&0xFF, y);
81 
82 u_short ip_id;
83 
84 #ifdef MBUF_STRESS_TEST
85 int mbuf_frag_size = 0;
86 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
87 	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
88 #endif
89 
90 static struct ifnet *ip_multicast_if(struct in_addr *, int *);
91 static void	ip_mloopback
92 	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
93 static int	ip_getmoptions(struct inpcb *, struct sockopt *);
94 static int	ip_setmoptions(struct inpcb *, struct sockopt *);
95 
96 
97 extern	struct protosw inetsw[];
98 
99 /*
100  * IP output.  The packet in mbuf chain m contains a skeletal IP
101  * header (with len, off, ttl, proto, tos, src, dst).
102  * The mbuf chain containing the packet will be freed.
103  * The mbuf opt, if present, will not be freed.
104  * In the IP forwarding case, the packet will arrive with options already
105  * inserted, so must have a NULL opt pointer.
106  */
107 int
108 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro,
109 	int flags, struct ip_moptions *imo, struct inpcb *inp)
110 {
111 	struct ip *ip;
112 	struct ifnet *ifp = NULL;	/* keep compiler happy */
113 	struct mbuf *m0;
114 	int hlen = sizeof (struct ip);
115 	int mtu;
116 	int len, error = 0;
117 	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
118 	struct in_ifaddr *ia = NULL;
119 	int isbroadcast, sw_csum;
120 	struct route iproute;
121 	struct in_addr odst;
122 #ifdef IPFIREWALL_FORWARD
123 	struct m_tag *fwd_tag = NULL;
124 #endif
125 	M_ASSERTPKTHDR(m);
126 
127 	if (ro == NULL) {
128 		ro = &iproute;
129 		bzero(ro, sizeof (*ro));
130 	}
131 
132 	if (inp != NULL)
133 		INP_LOCK_ASSERT(inp);
134 
135 	if (opt) {
136 		len = 0;
137 		m = ip_insertoptions(m, opt, &len);
138 		if (len != 0)
139 			hlen = len;
140 	}
141 	ip = mtod(m, struct ip *);
142 
143 	/*
144 	 * Fill in IP header.  If we are not allowing fragmentation,
145 	 * then the ip_id field is meaningless, but we don't set it
146 	 * to zero.  Doing so causes various problems when devices along
147 	 * the path (routers, load balancers, firewalls, etc.) illegally
148 	 * disable DF on our packet.  Note that a 16-bit counter
149 	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
150 	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
151 	 * for Counting NATted Hosts", Proc. IMW'02, available at
152 	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
153 	 */
154 	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
155 		ip->ip_v = IPVERSION;
156 		ip->ip_hl = hlen >> 2;
157 		ip->ip_id = ip_newid();
158 		ipstat.ips_localout++;
159 	} else {
160 		hlen = ip->ip_hl << 2;
161 	}
162 
163 	dst = (struct sockaddr_in *)&ro->ro_dst;
164 again:
165 	/*
166 	 * If there is a cached route,
167 	 * check that it is to the same destination
168 	 * and is still up.  If not, free it and try again.
169 	 * The address family should also be checked in case of sharing the
170 	 * cache with IPv6.
171 	 */
172 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
173 			  dst->sin_family != AF_INET ||
174 			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
175 		RTFREE(ro->ro_rt);
176 		ro->ro_rt = (struct rtentry *)NULL;
177 	}
178 #ifdef IPFIREWALL_FORWARD
179 	if (ro->ro_rt == NULL && fwd_tag == NULL) {
180 #else
181 	if (ro->ro_rt == NULL) {
182 #endif
183 		bzero(dst, sizeof(*dst));
184 		dst->sin_family = AF_INET;
185 		dst->sin_len = sizeof(*dst);
186 		dst->sin_addr = ip->ip_dst;
187 	}
188 	/*
189 	 * If routing to interface only,
190 	 * short circuit routing lookup.
191 	 */
192 	if (flags & IP_ROUTETOIF) {
193 		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
194 		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
195 			ipstat.ips_noroute++;
196 			error = ENETUNREACH;
197 			goto bad;
198 		}
199 		ifp = ia->ia_ifp;
200 		ip->ip_ttl = 1;
201 		isbroadcast = in_broadcast(dst->sin_addr, ifp);
202 	} else if (flags & IP_SENDONES) {
203 		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL) {
204 			ipstat.ips_noroute++;
205 			error = ENETUNREACH;
206 			goto bad;
207 		}
208 		ifp = ia->ia_ifp;
209 		ip->ip_dst.s_addr = INADDR_BROADCAST;
210 		dst->sin_addr = ip->ip_dst;
211 		ip->ip_ttl = 1;
212 		isbroadcast = 1;
213 	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
214 	    imo != NULL && imo->imo_multicast_ifp != NULL) {
215 		/*
216 		 * Bypass the normal routing lookup for multicast
217 		 * packets if the interface is specified.
218 		 */
219 		ifp = imo->imo_multicast_ifp;
220 		IFP_TO_IA(ifp, ia);
221 		isbroadcast = 0;	/* fool gcc */
222 	} else {
223 		/*
224 		 * We want to do any cloning requested by the link layer,
225 		 * as this is probably required in all cases for correct
226 		 * operation (as it is for ARP).
227 		 */
228 		if (ro->ro_rt == NULL)
229 			rtalloc_ign(ro, 0);
230 		if (ro->ro_rt == NULL) {
231 			ipstat.ips_noroute++;
232 			error = EHOSTUNREACH;
233 			goto bad;
234 		}
235 		ia = ifatoia(ro->ro_rt->rt_ifa);
236 		ifp = ro->ro_rt->rt_ifp;
237 		ro->ro_rt->rt_rmx.rmx_pksent++;
238 		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
239 			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
240 		if (ro->ro_rt->rt_flags & RTF_HOST)
241 			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
242 		else
243 			isbroadcast = in_broadcast(dst->sin_addr, ifp);
244 	}
245 	/*
246 	 * Calculate MTU.  If we have a route that is up, use that,
247 	 * otherwise use the interface's MTU.
248 	 */
249 	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
250 		/*
251 		 * This case can happen if the user changed the MTU
252 		 * of an interface after enabling IP on it.  Because
253 		 * most netifs don't keep track of routes pointing to
254 		 * them, there is no way for one to update all its
255 		 * routes when the MTU is changed.
256 		 */
257 		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
258 			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
259 		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
260 	} else {
261 		mtu = ifp->if_mtu;
262 	}
263 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
264 		struct in_multi *inm;
265 
266 		m->m_flags |= M_MCAST;
267 		/*
268 		 * IP destination address is multicast.  Make sure "dst"
269 		 * still points to the address in "ro".  (It may have been
270 		 * changed to point to a gateway address, above.)
271 		 */
272 		dst = (struct sockaddr_in *)&ro->ro_dst;
273 		/*
274 		 * See if the caller provided any multicast options
275 		 */
276 		if (imo != NULL) {
277 			ip->ip_ttl = imo->imo_multicast_ttl;
278 			if (imo->imo_multicast_vif != -1)
279 				ip->ip_src.s_addr =
280 				    ip_mcast_src ?
281 				    ip_mcast_src(imo->imo_multicast_vif) :
282 				    INADDR_ANY;
283 		} else
284 			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
285 		/*
286 		 * Confirm that the outgoing interface supports multicast.
287 		 */
288 		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
289 			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
290 				ipstat.ips_noroute++;
291 				error = ENETUNREACH;
292 				goto bad;
293 			}
294 		}
295 		/*
296 		 * If source address not specified yet, use address
297 		 * of outgoing interface.
298 		 */
299 		if (ip->ip_src.s_addr == INADDR_ANY) {
300 			/* Interface may have no addresses. */
301 			if (ia != NULL)
302 				ip->ip_src = IA_SIN(ia)->sin_addr;
303 		}
304 
305 		IN_MULTI_LOCK();
306 		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
307 		if (inm != NULL &&
308 		   (imo == NULL || imo->imo_multicast_loop)) {
309 			IN_MULTI_UNLOCK();
310 			/*
311 			 * If we belong to the destination multicast group
312 			 * on the outgoing interface, and the caller did not
313 			 * forbid loopback, loop back a copy.
314 			 */
315 			ip_mloopback(ifp, m, dst, hlen);
316 		}
317 		else {
318 			IN_MULTI_UNLOCK();
319 			/*
320 			 * If we are acting as a multicast router, perform
321 			 * multicast forwarding as if the packet had just
322 			 * arrived on the interface to which we are about
323 			 * to send.  The multicast forwarding function
324 			 * recursively calls this function, using the
325 			 * IP_FORWARDING flag to prevent infinite recursion.
326 			 *
327 			 * Multicasts that are looped back by ip_mloopback(),
328 			 * above, will be forwarded by the ip_input() routine,
329 			 * if necessary.
330 			 */
331 			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
332 				/*
333 				 * If rsvp daemon is not running, do not
334 				 * set ip_moptions. This ensures that the packet
335 				 * is multicast and not just sent down one link
336 				 * as prescribed by rsvpd.
337 				 */
338 				if (!rsvp_on)
339 					imo = NULL;
340 				if (ip_mforward &&
341 				    ip_mforward(ip, ifp, m, imo) != 0) {
342 					m_freem(m);
343 					goto done;
344 				}
345 			}
346 		}
347 
348 		/*
349 		 * Multicasts with a time-to-live of zero may be looped-
350 		 * back, above, but must not be transmitted on a network.
351 		 * Also, multicasts addressed to the loopback interface
352 		 * are not sent -- the above call to ip_mloopback() will
353 		 * loop back a copy if this host actually belongs to the
354 		 * destination group on the loopback interface.
355 		 */
356 		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
357 			m_freem(m);
358 			goto done;
359 		}
360 
361 		goto sendit;
362 	}
363 #ifndef notdef
364 	/*
365 	 * If the source address is not specified yet, use the address
366 	 * of the outoing interface.
367 	 */
368 	if (ip->ip_src.s_addr == INADDR_ANY) {
369 		/* Interface may have no addresses. */
370 		if (ia != NULL) {
371 			ip->ip_src = IA_SIN(ia)->sin_addr;
372 		}
373 	}
374 #endif /* notdef */
375 	/*
376 	 * Verify that we have any chance at all of being able to queue the
377 	 * packet or packet fragments, unless ALTQ is enabled on the given
378 	 * interface in which case packetdrop should be done by queueing.
379 	 */
380 #ifdef ALTQ
381 	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
382 	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
383 	    ifp->if_snd.ifq_maxlen))
384 #else
385 	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
386 	    ifp->if_snd.ifq_maxlen)
387 #endif /* ALTQ */
388 	{
389 		error = ENOBUFS;
390 		ipstat.ips_odropped++;
391 		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
392 		goto bad;
393 	}
394 
395 	/*
396 	 * Look for broadcast address and
397 	 * verify user is allowed to send
398 	 * such a packet.
399 	 */
400 	if (isbroadcast) {
401 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
402 			error = EADDRNOTAVAIL;
403 			goto bad;
404 		}
405 		if ((flags & IP_ALLOWBROADCAST) == 0) {
406 			error = EACCES;
407 			goto bad;
408 		}
409 		/* don't allow broadcast messages to be fragmented */
410 		if (ip->ip_len > mtu) {
411 			error = EMSGSIZE;
412 			goto bad;
413 		}
414 		m->m_flags |= M_BCAST;
415 	} else {
416 		m->m_flags &= ~M_BCAST;
417 	}
418 
419 sendit:
420 #if defined(IPSEC) || defined(FAST_IPSEC)
421 	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
422 	case 1:
423 		goto bad;
424 	case -1:
425 		goto done;
426 	case 0:
427 	default:
428 		break;	/* Continue with packet processing. */
429 	}
430 	/* Update variables that are affected by ipsec4_output(). */
431 	ip = mtod(m, struct ip *);
432 	hlen = ip->ip_hl << 2;
433 #endif /* IPSEC */
434 
435 	/* Jump over all PFIL processing if hooks are not active. */
436 	if (!PFIL_HOOKED(&inet_pfil_hook))
437 		goto passout;
438 
439 	/* Run through list of hooks for output packets. */
440 	odst.s_addr = ip->ip_dst.s_addr;
441 	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
442 	if (error != 0 || m == NULL)
443 		goto done;
444 
445 	ip = mtod(m, struct ip *);
446 
447 	/* See if destination IP address was changed by packet filter. */
448 	if (odst.s_addr != ip->ip_dst.s_addr) {
449 		m->m_flags |= M_SKIP_FIREWALL;
450 		/* If destination is now ourself drop to ip_input(). */
451 		if (in_localip(ip->ip_dst)) {
452 			m->m_flags |= M_FASTFWD_OURS;
453 			if (m->m_pkthdr.rcvif == NULL)
454 				m->m_pkthdr.rcvif = loif;
455 			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
456 				m->m_pkthdr.csum_flags |=
457 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
458 				m->m_pkthdr.csum_data = 0xffff;
459 			}
460 			m->m_pkthdr.csum_flags |=
461 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
462 
463 			error = netisr_queue(NETISR_IP, m);
464 			goto done;
465 		} else
466 			goto again;	/* Redo the routing table lookup. */
467 	}
468 
469 #ifdef IPFIREWALL_FORWARD
470 	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
471 	if (m->m_flags & M_FASTFWD_OURS) {
472 		if (m->m_pkthdr.rcvif == NULL)
473 			m->m_pkthdr.rcvif = loif;
474 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
475 			m->m_pkthdr.csum_flags |=
476 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
477 			m->m_pkthdr.csum_data = 0xffff;
478 		}
479 		m->m_pkthdr.csum_flags |=
480 			    CSUM_IP_CHECKED | CSUM_IP_VALID;
481 
482 		error = netisr_queue(NETISR_IP, m);
483 		goto done;
484 	}
485 	/* Or forward to some other address? */
486 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
487 	if (fwd_tag) {
488 		dst = (struct sockaddr_in *)&ro->ro_dst;
489 		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
490 		m->m_flags |= M_SKIP_FIREWALL;
491 		m_tag_delete(m, fwd_tag);
492 		goto again;
493 	}
494 #endif /* IPFIREWALL_FORWARD */
495 
496 passout:
497 	/* 127/8 must not appear on wire - RFC1122. */
498 	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
499 	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
500 		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
501 			ipstat.ips_badaddr++;
502 			error = EADDRNOTAVAIL;
503 			goto bad;
504 		}
505 	}
506 
507 	m->m_pkthdr.csum_flags |= CSUM_IP;
508 	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
509 	if (sw_csum & CSUM_DELAY_DATA) {
510 		in_delayed_cksum(m);
511 		sw_csum &= ~CSUM_DELAY_DATA;
512 	}
513 	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
514 
515 	/*
516 	 * If small enough for interface, or the interface will take
517 	 * care of the fragmentation for us, we can just send directly.
518 	 */
519 	if (ip->ip_len <= mtu ||
520 	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
521 	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
522 		ip->ip_len = htons(ip->ip_len);
523 		ip->ip_off = htons(ip->ip_off);
524 		ip->ip_sum = 0;
525 		if (sw_csum & CSUM_DELAY_IP)
526 			ip->ip_sum = in_cksum(m, hlen);
527 
528 		/*
529 		 * Record statistics for this interface address.
530 		 * With CSUM_TSO the byte/packet count will be slightly
531 		 * incorrect because we count the IP+TCP headers only
532 		 * once instead of for every generated packet.
533 		 */
534 		if (!(flags & IP_FORWARDING) && ia) {
535 			if (m->m_pkthdr.csum_flags & CSUM_TSO)
536 				ia->ia_ifa.if_opackets +=
537 				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
538 			else
539 				ia->ia_ifa.if_opackets++;
540 			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
541 		}
542 #ifdef IPSEC
543 		/* clean ipsec history once it goes out of the node */
544 		ipsec_delaux(m);
545 #endif
546 #ifdef MBUF_STRESS_TEST
547 		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
548 			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
549 #endif
550 		/*
551 		 * Reset layer specific mbuf flags
552 		 * to avoid confusing lower layers.
553 		 */
554 		m->m_flags &= ~(M_PROTOFLAGS);
555 
556 		error = (*ifp->if_output)(ifp, m,
557 				(struct sockaddr *)dst, ro->ro_rt);
558 		goto done;
559 	}
560 
561 	/* Balk when DF bit is set or the interface didn't support TSO. */
562 	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
563 		error = EMSGSIZE;
564 		ipstat.ips_cantfrag++;
565 		goto bad;
566 	}
567 
568 	/*
569 	 * Too large for interface; fragment if possible. If successful,
570 	 * on return, m will point to a list of packets to be sent.
571 	 */
572 	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
573 	if (error)
574 		goto bad;
575 	for (; m; m = m0) {
576 		m0 = m->m_nextpkt;
577 		m->m_nextpkt = 0;
578 #ifdef IPSEC
579 		/* clean ipsec history once it goes out of the node */
580 		ipsec_delaux(m);
581 #endif
582 		if (error == 0) {
583 			/* Record statistics for this interface address. */
584 			if (ia != NULL) {
585 				ia->ia_ifa.if_opackets++;
586 				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
587 			}
588 			/*
589 			 * Reset layer specific mbuf flags
590 			 * to avoid confusing upper layers.
591 			 */
592 			m->m_flags &= ~(M_PROTOFLAGS);
593 
594 			error = (*ifp->if_output)(ifp, m,
595 			    (struct sockaddr *)dst, ro->ro_rt);
596 		} else
597 			m_freem(m);
598 	}
599 
600 	if (error == 0)
601 		ipstat.ips_fragmented++;
602 
603 done:
604 	if (ro == &iproute && ro->ro_rt) {
605 		RTFREE(ro->ro_rt);
606 	}
607 	return (error);
608 bad:
609 	m_freem(m);
610 	goto done;
611 }
612 
613 /*
614  * Create a chain of fragments which fit the given mtu. m_frag points to the
615  * mbuf to be fragmented; on return it points to the chain with the fragments.
616  * Return 0 if no error. If error, m_frag may contain a partially built
617  * chain of fragments that should be freed by the caller.
618  *
619  * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
620  * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
621  */
622 int
623 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
624 	    u_long if_hwassist_flags, int sw_csum)
625 {
626 	int error = 0;
627 	int hlen = ip->ip_hl << 2;
628 	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
629 	int off;
630 	struct mbuf *m0 = *m_frag;	/* the original packet		*/
631 	int firstlen;
632 	struct mbuf **mnext;
633 	int nfrags;
634 
635 	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
636 		ipstat.ips_cantfrag++;
637 		return EMSGSIZE;
638 	}
639 
640 	/*
641 	 * Must be able to put at least 8 bytes per fragment.
642 	 */
643 	if (len < 8)
644 		return EMSGSIZE;
645 
646 	/*
647 	 * If the interface will not calculate checksums on
648 	 * fragmented packets, then do it here.
649 	 */
650 	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
651 	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
652 		in_delayed_cksum(m0);
653 		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
654 	}
655 
656 	if (len > PAGE_SIZE) {
657 		/*
658 		 * Fragment large datagrams such that each segment
659 		 * contains a multiple of PAGE_SIZE amount of data,
660 		 * plus headers. This enables a receiver to perform
661 		 * page-flipping zero-copy optimizations.
662 		 *
663 		 * XXX When does this help given that sender and receiver
664 		 * could have different page sizes, and also mtu could
665 		 * be less than the receiver's page size ?
666 		 */
667 		int newlen;
668 		struct mbuf *m;
669 
670 		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
671 			off += m->m_len;
672 
673 		/*
674 		 * firstlen (off - hlen) must be aligned on an
675 		 * 8-byte boundary
676 		 */
677 		if (off < hlen)
678 			goto smart_frag_failure;
679 		off = ((off - hlen) & ~7) + hlen;
680 		newlen = (~PAGE_MASK) & mtu;
681 		if ((newlen + sizeof (struct ip)) > mtu) {
682 			/* we failed, go back the default */
683 smart_frag_failure:
684 			newlen = len;
685 			off = hlen + len;
686 		}
687 		len = newlen;
688 
689 	} else {
690 		off = hlen + len;
691 	}
692 
693 	firstlen = off - hlen;
694 	mnext = &m0->m_nextpkt;		/* pointer to next packet */
695 
696 	/*
697 	 * Loop through length of segment after first fragment,
698 	 * make new header and copy data of each part and link onto chain.
699 	 * Here, m0 is the original packet, m is the fragment being created.
700 	 * The fragments are linked off the m_nextpkt of the original
701 	 * packet, which after processing serves as the first fragment.
702 	 */
703 	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
704 		struct ip *mhip;	/* ip header on the fragment */
705 		struct mbuf *m;
706 		int mhlen = sizeof (struct ip);
707 
708 		MGETHDR(m, M_DONTWAIT, MT_DATA);
709 		if (m == NULL) {
710 			error = ENOBUFS;
711 			ipstat.ips_odropped++;
712 			goto done;
713 		}
714 		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
715 		/*
716 		 * In the first mbuf, leave room for the link header, then
717 		 * copy the original IP header including options. The payload
718 		 * goes into an additional mbuf chain returned by m_copy().
719 		 */
720 		m->m_data += max_linkhdr;
721 		mhip = mtod(m, struct ip *);
722 		*mhip = *ip;
723 		if (hlen > sizeof (struct ip)) {
724 			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
725 			mhip->ip_v = IPVERSION;
726 			mhip->ip_hl = mhlen >> 2;
727 		}
728 		m->m_len = mhlen;
729 		/* XXX do we need to add ip->ip_off below ? */
730 		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
731 		if (off + len >= ip->ip_len) {	/* last fragment */
732 			len = ip->ip_len - off;
733 			m->m_flags |= M_LASTFRAG;
734 		} else
735 			mhip->ip_off |= IP_MF;
736 		mhip->ip_len = htons((u_short)(len + mhlen));
737 		m->m_next = m_copy(m0, off, len);
738 		if (m->m_next == NULL) {	/* copy failed */
739 			m_free(m);
740 			error = ENOBUFS;	/* ??? */
741 			ipstat.ips_odropped++;
742 			goto done;
743 		}
744 		m->m_pkthdr.len = mhlen + len;
745 		m->m_pkthdr.rcvif = NULL;
746 #ifdef MAC
747 		mac_create_fragment(m0, m);
748 #endif
749 		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
750 		mhip->ip_off = htons(mhip->ip_off);
751 		mhip->ip_sum = 0;
752 		if (sw_csum & CSUM_DELAY_IP)
753 			mhip->ip_sum = in_cksum(m, mhlen);
754 		*mnext = m;
755 		mnext = &m->m_nextpkt;
756 	}
757 	ipstat.ips_ofragments += nfrags;
758 
759 	/* set first marker for fragment chain */
760 	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
761 	m0->m_pkthdr.csum_data = nfrags;
762 
763 	/*
764 	 * Update first fragment by trimming what's been copied out
765 	 * and updating header.
766 	 */
767 	m_adj(m0, hlen + firstlen - ip->ip_len);
768 	m0->m_pkthdr.len = hlen + firstlen;
769 	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
770 	ip->ip_off |= IP_MF;
771 	ip->ip_off = htons(ip->ip_off);
772 	ip->ip_sum = 0;
773 	if (sw_csum & CSUM_DELAY_IP)
774 		ip->ip_sum = in_cksum(m0, hlen);
775 
776 done:
777 	*m_frag = m0;
778 	return error;
779 }
780 
781 void
782 in_delayed_cksum(struct mbuf *m)
783 {
784 	struct ip *ip;
785 	u_short csum, offset;
786 
787 	ip = mtod(m, struct ip *);
788 	offset = ip->ip_hl << 2 ;
789 	csum = in_cksum_skip(m, ip->ip_len, offset);
790 	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
791 		csum = 0xffff;
792 	offset += m->m_pkthdr.csum_data;	/* checksum offset */
793 
794 	if (offset + sizeof(u_short) > m->m_len) {
795 		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
796 		    m->m_len, offset, ip->ip_p);
797 		/*
798 		 * XXX
799 		 * this shouldn't happen, but if it does, the
800 		 * correct behavior may be to insert the checksum
801 		 * in the appropriate next mbuf in the chain.
802 		 */
803 		return;
804 	}
805 	*(u_short *)(m->m_data + offset) = csum;
806 }
807 
808 /*
809  * IP socket option processing.
810  */
811 int
812 ip_ctloutput(so, sopt)
813 	struct socket *so;
814 	struct sockopt *sopt;
815 {
816 	struct	inpcb *inp = sotoinpcb(so);
817 	int	error, optval;
818 
819 	error = optval = 0;
820 	if (sopt->sopt_level != IPPROTO_IP) {
821 		return (EINVAL);
822 	}
823 
824 	switch (sopt->sopt_dir) {
825 	case SOPT_SET:
826 		switch (sopt->sopt_name) {
827 		case IP_OPTIONS:
828 #ifdef notyet
829 		case IP_RETOPTS:
830 #endif
831 		{
832 			struct mbuf *m;
833 			if (sopt->sopt_valsize > MLEN) {
834 				error = EMSGSIZE;
835 				break;
836 			}
837 			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
838 			if (m == NULL) {
839 				error = ENOBUFS;
840 				break;
841 			}
842 			m->m_len = sopt->sopt_valsize;
843 			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
844 					    m->m_len);
845 			if (error) {
846 				m_free(m);
847 				break;
848 			}
849 			INP_LOCK(inp);
850 			error = ip_pcbopts(inp, sopt->sopt_name, m);
851 			INP_UNLOCK(inp);
852 			return (error);
853 		}
854 
855 		case IP_TOS:
856 		case IP_TTL:
857 		case IP_MINTTL:
858 		case IP_RECVOPTS:
859 		case IP_RECVRETOPTS:
860 		case IP_RECVDSTADDR:
861 		case IP_RECVTTL:
862 		case IP_RECVIF:
863 		case IP_FAITH:
864 		case IP_ONESBCAST:
865 		case IP_DONTFRAG:
866 			error = sooptcopyin(sopt, &optval, sizeof optval,
867 					    sizeof optval);
868 			if (error)
869 				break;
870 
871 			switch (sopt->sopt_name) {
872 			case IP_TOS:
873 				inp->inp_ip_tos = optval;
874 				break;
875 
876 			case IP_TTL:
877 				inp->inp_ip_ttl = optval;
878 				break;
879 
880 			case IP_MINTTL:
881 				if (optval > 0 && optval <= MAXTTL)
882 					inp->inp_ip_minttl = optval;
883 				else
884 					error = EINVAL;
885 				break;
886 
887 #define	OPTSET(bit) do {						\
888 	INP_LOCK(inp);							\
889 	if (optval)							\
890 		inp->inp_flags |= bit;					\
891 	else								\
892 		inp->inp_flags &= ~bit;					\
893 	INP_UNLOCK(inp);						\
894 } while (0)
895 
896 			case IP_RECVOPTS:
897 				OPTSET(INP_RECVOPTS);
898 				break;
899 
900 			case IP_RECVRETOPTS:
901 				OPTSET(INP_RECVRETOPTS);
902 				break;
903 
904 			case IP_RECVDSTADDR:
905 				OPTSET(INP_RECVDSTADDR);
906 				break;
907 
908 			case IP_RECVTTL:
909 				OPTSET(INP_RECVTTL);
910 				break;
911 
912 			case IP_RECVIF:
913 				OPTSET(INP_RECVIF);
914 				break;
915 
916 			case IP_FAITH:
917 				OPTSET(INP_FAITH);
918 				break;
919 
920 			case IP_ONESBCAST:
921 				OPTSET(INP_ONESBCAST);
922 				break;
923 			case IP_DONTFRAG:
924 				OPTSET(INP_DONTFRAG);
925 				break;
926 			}
927 			break;
928 #undef OPTSET
929 
930 		case IP_MULTICAST_IF:
931 		case IP_MULTICAST_VIF:
932 		case IP_MULTICAST_TTL:
933 		case IP_MULTICAST_LOOP:
934 		case IP_ADD_MEMBERSHIP:
935 		case IP_DROP_MEMBERSHIP:
936 			error = ip_setmoptions(inp, sopt);
937 			break;
938 
939 		case IP_PORTRANGE:
940 			error = sooptcopyin(sopt, &optval, sizeof optval,
941 					    sizeof optval);
942 			if (error)
943 				break;
944 
945 			INP_LOCK(inp);
946 			switch (optval) {
947 			case IP_PORTRANGE_DEFAULT:
948 				inp->inp_flags &= ~(INP_LOWPORT);
949 				inp->inp_flags &= ~(INP_HIGHPORT);
950 				break;
951 
952 			case IP_PORTRANGE_HIGH:
953 				inp->inp_flags &= ~(INP_LOWPORT);
954 				inp->inp_flags |= INP_HIGHPORT;
955 				break;
956 
957 			case IP_PORTRANGE_LOW:
958 				inp->inp_flags &= ~(INP_HIGHPORT);
959 				inp->inp_flags |= INP_LOWPORT;
960 				break;
961 
962 			default:
963 				error = EINVAL;
964 				break;
965 			}
966 			INP_UNLOCK(inp);
967 			break;
968 
969 #if defined(IPSEC) || defined(FAST_IPSEC)
970 		case IP_IPSEC_POLICY:
971 		{
972 			caddr_t req;
973 			size_t len = 0;
974 			int priv;
975 			struct mbuf *m;
976 			int optname;
977 
978 			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
979 				break;
980 			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
981 				break;
982 			priv = (sopt->sopt_td != NULL &&
983 				suser(sopt->sopt_td) != 0) ? 0 : 1;
984 			req = mtod(m, caddr_t);
985 			len = m->m_len;
986 			optname = sopt->sopt_name;
987 			error = ipsec4_set_policy(inp, optname, req, len, priv);
988 			m_freem(m);
989 			break;
990 		}
991 #endif /*IPSEC*/
992 
993 		default:
994 			error = ENOPROTOOPT;
995 			break;
996 		}
997 		break;
998 
999 	case SOPT_GET:
1000 		switch (sopt->sopt_name) {
1001 		case IP_OPTIONS:
1002 		case IP_RETOPTS:
1003 			if (inp->inp_options)
1004 				error = sooptcopyout(sopt,
1005 						     mtod(inp->inp_options,
1006 							  char *),
1007 						     inp->inp_options->m_len);
1008 			else
1009 				sopt->sopt_valsize = 0;
1010 			break;
1011 
1012 		case IP_TOS:
1013 		case IP_TTL:
1014 		case IP_MINTTL:
1015 		case IP_RECVOPTS:
1016 		case IP_RECVRETOPTS:
1017 		case IP_RECVDSTADDR:
1018 		case IP_RECVTTL:
1019 		case IP_RECVIF:
1020 		case IP_PORTRANGE:
1021 		case IP_FAITH:
1022 		case IP_ONESBCAST:
1023 		case IP_DONTFRAG:
1024 			switch (sopt->sopt_name) {
1025 
1026 			case IP_TOS:
1027 				optval = inp->inp_ip_tos;
1028 				break;
1029 
1030 			case IP_TTL:
1031 				optval = inp->inp_ip_ttl;
1032 				break;
1033 
1034 			case IP_MINTTL:
1035 				optval = inp->inp_ip_minttl;
1036 				break;
1037 
1038 #define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1039 
1040 			case IP_RECVOPTS:
1041 				optval = OPTBIT(INP_RECVOPTS);
1042 				break;
1043 
1044 			case IP_RECVRETOPTS:
1045 				optval = OPTBIT(INP_RECVRETOPTS);
1046 				break;
1047 
1048 			case IP_RECVDSTADDR:
1049 				optval = OPTBIT(INP_RECVDSTADDR);
1050 				break;
1051 
1052 			case IP_RECVTTL:
1053 				optval = OPTBIT(INP_RECVTTL);
1054 				break;
1055 
1056 			case IP_RECVIF:
1057 				optval = OPTBIT(INP_RECVIF);
1058 				break;
1059 
1060 			case IP_PORTRANGE:
1061 				if (inp->inp_flags & INP_HIGHPORT)
1062 					optval = IP_PORTRANGE_HIGH;
1063 				else if (inp->inp_flags & INP_LOWPORT)
1064 					optval = IP_PORTRANGE_LOW;
1065 				else
1066 					optval = 0;
1067 				break;
1068 
1069 			case IP_FAITH:
1070 				optval = OPTBIT(INP_FAITH);
1071 				break;
1072 
1073 			case IP_ONESBCAST:
1074 				optval = OPTBIT(INP_ONESBCAST);
1075 				break;
1076 			case IP_DONTFRAG:
1077 				optval = OPTBIT(INP_DONTFRAG);
1078 				break;
1079 			}
1080 			error = sooptcopyout(sopt, &optval, sizeof optval);
1081 			break;
1082 
1083 		case IP_MULTICAST_IF:
1084 		case IP_MULTICAST_VIF:
1085 		case IP_MULTICAST_TTL:
1086 		case IP_MULTICAST_LOOP:
1087 		case IP_ADD_MEMBERSHIP:
1088 		case IP_DROP_MEMBERSHIP:
1089 			error = ip_getmoptions(inp, sopt);
1090 			break;
1091 
1092 #if defined(IPSEC) || defined(FAST_IPSEC)
1093 		case IP_IPSEC_POLICY:
1094 		{
1095 			struct mbuf *m = NULL;
1096 			caddr_t req = NULL;
1097 			size_t len = 0;
1098 
1099 			if (m != 0) {
1100 				req = mtod(m, caddr_t);
1101 				len = m->m_len;
1102 			}
1103 			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1104 			if (error == 0)
1105 				error = soopt_mcopyout(sopt, m); /* XXX */
1106 			if (error == 0)
1107 				m_freem(m);
1108 			break;
1109 		}
1110 #endif /*IPSEC*/
1111 
1112 		default:
1113 			error = ENOPROTOOPT;
1114 			break;
1115 		}
1116 		break;
1117 	}
1118 	return (error);
1119 }
1120 
1121 /*
1122  * XXX
1123  * The whole multicast option thing needs to be re-thought.
1124  * Several of these options are equally applicable to non-multicast
1125  * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1126  * standard option (IP_TTL).
1127  */
1128 
1129 /*
1130  * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1131  */
1132 static struct ifnet *
1133 ip_multicast_if(a, ifindexp)
1134 	struct in_addr *a;
1135 	int *ifindexp;
1136 {
1137 	int ifindex;
1138 	struct ifnet *ifp;
1139 
1140 	if (ifindexp)
1141 		*ifindexp = 0;
1142 	if (ntohl(a->s_addr) >> 24 == 0) {
1143 		ifindex = ntohl(a->s_addr) & 0xffffff;
1144 		if (ifindex < 0 || if_index < ifindex)
1145 			return NULL;
1146 		ifp = ifnet_byindex(ifindex);
1147 		if (ifindexp)
1148 			*ifindexp = ifindex;
1149 	} else {
1150 		INADDR_TO_IFP(*a, ifp);
1151 	}
1152 	return ifp;
1153 }
1154 
1155 /*
1156  * Given an inpcb, return its multicast options structure pointer.  Accepts
1157  * an unlocked inpcb pointer, but will return it locked.  May sleep.
1158  */
1159 static struct ip_moptions *
1160 ip_findmoptions(struct inpcb *inp)
1161 {
1162 	struct ip_moptions *imo;
1163 	struct in_multi **immp;
1164 
1165 	INP_LOCK(inp);
1166 	if (inp->inp_moptions != NULL)
1167 		return (inp->inp_moptions);
1168 
1169 	INP_UNLOCK(inp);
1170 
1171 	imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1172 	immp = (struct in_multi **)malloc((sizeof(*immp) * IP_MIN_MEMBERSHIPS),
1173 					  M_IPMOPTS, M_WAITOK);
1174 
1175 	imo->imo_multicast_ifp = NULL;
1176 	imo->imo_multicast_addr.s_addr = INADDR_ANY;
1177 	imo->imo_multicast_vif = -1;
1178 	imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1179 	imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1180 	imo->imo_num_memberships = 0;
1181 	imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1182 	imo->imo_membership = immp;
1183 
1184 	INP_LOCK(inp);
1185 	if (inp->inp_moptions != NULL) {
1186 		free(immp, M_IPMOPTS);
1187 		free(imo, M_IPMOPTS);
1188 		return (inp->inp_moptions);
1189 	}
1190 	inp->inp_moptions = imo;
1191 	return (imo);
1192 }
1193 
1194 /*
1195  * Set the IP multicast options in response to user setsockopt().
1196  */
1197 static int
1198 ip_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1199 {
1200 	int error = 0;
1201 	int i;
1202 	struct in_addr addr;
1203 	struct ip_mreq mreq;
1204 	struct ifnet *ifp;
1205 	struct ip_moptions *imo;
1206 	struct route ro;
1207 	struct sockaddr_in *dst;
1208 	int ifindex;
1209 	int s;
1210 
1211 	switch (sopt->sopt_name) {
1212 	/* store an index number for the vif you wanna use in the send */
1213 	case IP_MULTICAST_VIF:
1214 		if (legal_vif_num == 0) {
1215 			error = EOPNOTSUPP;
1216 			break;
1217 		}
1218 		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1219 		if (error)
1220 			break;
1221 		if (!legal_vif_num(i) && (i != -1)) {
1222 			error = EINVAL;
1223 			break;
1224 		}
1225 		imo = ip_findmoptions(inp);
1226 		imo->imo_multicast_vif = i;
1227 		INP_UNLOCK(inp);
1228 		break;
1229 
1230 	case IP_MULTICAST_IF:
1231 		/*
1232 		 * Select the interface for outgoing multicast packets.
1233 		 */
1234 		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1235 		if (error)
1236 			break;
1237 		/*
1238 		 * INADDR_ANY is used to remove a previous selection.
1239 		 * When no interface is selected, a default one is
1240 		 * chosen every time a multicast packet is sent.
1241 		 */
1242 		imo = ip_findmoptions(inp);
1243 		if (addr.s_addr == INADDR_ANY) {
1244 			imo->imo_multicast_ifp = NULL;
1245 			INP_UNLOCK(inp);
1246 			break;
1247 		}
1248 		/*
1249 		 * The selected interface is identified by its local
1250 		 * IP address.  Find the interface and confirm that
1251 		 * it supports multicasting.
1252 		 */
1253 		s = splimp();
1254 		ifp = ip_multicast_if(&addr, &ifindex);
1255 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1256 			INP_UNLOCK(inp);
1257 			splx(s);
1258 			error = EADDRNOTAVAIL;
1259 			break;
1260 		}
1261 		imo->imo_multicast_ifp = ifp;
1262 		if (ifindex)
1263 			imo->imo_multicast_addr = addr;
1264 		else
1265 			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1266 		INP_UNLOCK(inp);
1267 		splx(s);
1268 		break;
1269 
1270 	case IP_MULTICAST_TTL:
1271 		/*
1272 		 * Set the IP time-to-live for outgoing multicast packets.
1273 		 * The original multicast API required a char argument,
1274 		 * which is inconsistent with the rest of the socket API.
1275 		 * We allow either a char or an int.
1276 		 */
1277 		if (sopt->sopt_valsize == 1) {
1278 			u_char ttl;
1279 			error = sooptcopyin(sopt, &ttl, 1, 1);
1280 			if (error)
1281 				break;
1282 			imo = ip_findmoptions(inp);
1283 			imo->imo_multicast_ttl = ttl;
1284 			INP_UNLOCK(inp);
1285 		} else {
1286 			u_int ttl;
1287 			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1288 					    sizeof ttl);
1289 			if (error)
1290 				break;
1291 			if (ttl > 255)
1292 				error = EINVAL;
1293 			else {
1294 				imo = ip_findmoptions(inp);
1295 				imo->imo_multicast_ttl = ttl;
1296 				INP_UNLOCK(inp);
1297 			}
1298 		}
1299 		break;
1300 
1301 	case IP_MULTICAST_LOOP:
1302 		/*
1303 		 * Set the loopback flag for outgoing multicast packets.
1304 		 * Must be zero or one.  The original multicast API required a
1305 		 * char argument, which is inconsistent with the rest
1306 		 * of the socket API.  We allow either a char or an int.
1307 		 */
1308 		if (sopt->sopt_valsize == 1) {
1309 			u_char loop;
1310 			error = sooptcopyin(sopt, &loop, 1, 1);
1311 			if (error)
1312 				break;
1313 			imo = ip_findmoptions(inp);
1314 			imo->imo_multicast_loop = !!loop;
1315 			INP_UNLOCK(inp);
1316 		} else {
1317 			u_int loop;
1318 			error = sooptcopyin(sopt, &loop, sizeof loop,
1319 					    sizeof loop);
1320 			if (error)
1321 				break;
1322 			imo = ip_findmoptions(inp);
1323 			imo->imo_multicast_loop = !!loop;
1324 			INP_UNLOCK(inp);
1325 		}
1326 		break;
1327 
1328 	case IP_ADD_MEMBERSHIP:
1329 		/*
1330 		 * Add a multicast group membership.
1331 		 * Group must be a valid IP multicast address.
1332 		 */
1333 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1334 		if (error)
1335 			break;
1336 
1337 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1338 			error = EINVAL;
1339 			break;
1340 		}
1341 		s = splimp();
1342 		/*
1343 		 * If no interface address was provided, use the interface of
1344 		 * the route to the given multicast address.
1345 		 */
1346 		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1347 			bzero((caddr_t)&ro, sizeof(ro));
1348 			dst = (struct sockaddr_in *)&ro.ro_dst;
1349 			dst->sin_len = sizeof(*dst);
1350 			dst->sin_family = AF_INET;
1351 			dst->sin_addr = mreq.imr_multiaddr;
1352 			rtalloc_ign(&ro, RTF_CLONING);
1353 			if (ro.ro_rt == NULL) {
1354 				error = EADDRNOTAVAIL;
1355 				splx(s);
1356 				break;
1357 			}
1358 			ifp = ro.ro_rt->rt_ifp;
1359 			RTFREE(ro.ro_rt);
1360 		}
1361 		else {
1362 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1363 		}
1364 
1365 		/*
1366 		 * See if we found an interface, and confirm that it
1367 		 * supports multicast.
1368 		 */
1369 		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1370 			error = EADDRNOTAVAIL;
1371 			splx(s);
1372 			break;
1373 		}
1374 		/*
1375 		 * See if the membership already exists or if all the
1376 		 * membership slots are full.
1377 		 */
1378 		imo = ip_findmoptions(inp);
1379 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1380 			if (imo->imo_membership[i]->inm_ifp == ifp &&
1381 			    imo->imo_membership[i]->inm_addr.s_addr
1382 						== mreq.imr_multiaddr.s_addr)
1383 				break;
1384 		}
1385 		if (i < imo->imo_num_memberships) {
1386 			INP_UNLOCK(inp);
1387 			error = EADDRINUSE;
1388 			splx(s);
1389 			break;
1390 		}
1391 		if (imo->imo_num_memberships == imo->imo_max_memberships) {
1392 		    struct in_multi **nmships, **omships;
1393 		    size_t newmax;
1394 		    /*
1395 		     * Resize the vector to next power-of-two minus 1. If the
1396 		     * size would exceed the maximum then we know we've really
1397 		     * run out of entries. Otherwise, we realloc() the vector
1398 		     * with the INP lock held to avoid introducing a race.
1399 		     */
1400 		    nmships = NULL;
1401 		    omships = imo->imo_membership;
1402 		    newmax = ((imo->imo_max_memberships + 1) * 2) - 1;
1403 		    if (newmax <= IP_MAX_MEMBERSHIPS) {
1404 			nmships = (struct in_multi **)realloc(omships,
1405 sizeof(*nmships) * newmax, M_IPMOPTS, M_NOWAIT);
1406 			if (nmships != NULL) {
1407 			    imo->imo_membership = nmships;
1408 			    imo->imo_max_memberships = newmax;
1409 			}
1410 		    }
1411 		    if (nmships == NULL) {
1412 			INP_UNLOCK(inp);
1413 			error = ETOOMANYREFS;
1414 			splx(s);
1415 			break;
1416 		    }
1417 		}
1418 		/*
1419 		 * Everything looks good; add a new record to the multicast
1420 		 * address list for the given interface.
1421 		 */
1422 		if ((imo->imo_membership[i] =
1423 		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1424 			INP_UNLOCK(inp);
1425 			error = ENOBUFS;
1426 			splx(s);
1427 			break;
1428 		}
1429 		++imo->imo_num_memberships;
1430 		INP_UNLOCK(inp);
1431 		splx(s);
1432 		break;
1433 
1434 	case IP_DROP_MEMBERSHIP:
1435 		/*
1436 		 * Drop a multicast group membership.
1437 		 * Group must be a valid IP multicast address.
1438 		 */
1439 		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1440 		if (error)
1441 			break;
1442 
1443 		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1444 			error = EINVAL;
1445 			break;
1446 		}
1447 
1448 		s = splimp();
1449 		/*
1450 		 * If an interface address was specified, get a pointer
1451 		 * to its ifnet structure.
1452 		 */
1453 		if (mreq.imr_interface.s_addr == INADDR_ANY)
1454 			ifp = NULL;
1455 		else {
1456 			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1457 			if (ifp == NULL) {
1458 				error = EADDRNOTAVAIL;
1459 				splx(s);
1460 				break;
1461 			}
1462 		}
1463 		/*
1464 		 * Find the membership in the membership array.
1465 		 */
1466 		imo = ip_findmoptions(inp);
1467 		for (i = 0; i < imo->imo_num_memberships; ++i) {
1468 			if ((ifp == NULL ||
1469 			     imo->imo_membership[i]->inm_ifp == ifp) &&
1470 			     imo->imo_membership[i]->inm_addr.s_addr ==
1471 			     mreq.imr_multiaddr.s_addr)
1472 				break;
1473 		}
1474 		if (i == imo->imo_num_memberships) {
1475 			INP_UNLOCK(inp);
1476 			error = EADDRNOTAVAIL;
1477 			splx(s);
1478 			break;
1479 		}
1480 		/*
1481 		 * Give up the multicast address record to which the
1482 		 * membership points.
1483 		 */
1484 		in_delmulti(imo->imo_membership[i]);
1485 		/*
1486 		 * Remove the gap in the membership array.
1487 		 */
1488 		for (++i; i < imo->imo_num_memberships; ++i)
1489 			imo->imo_membership[i-1] = imo->imo_membership[i];
1490 		--imo->imo_num_memberships;
1491 		INP_UNLOCK(inp);
1492 		splx(s);
1493 		break;
1494 
1495 	default:
1496 		error = EOPNOTSUPP;
1497 		break;
1498 	}
1499 
1500 	return (error);
1501 }
1502 
1503 /*
1504  * Return the IP multicast options in response to user getsockopt().
1505  */
1506 static int
1507 ip_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1508 {
1509 	struct ip_moptions *imo;
1510 	struct in_addr addr;
1511 	struct in_ifaddr *ia;
1512 	int error, optval;
1513 	u_char coptval;
1514 
1515 	INP_LOCK(inp);
1516 	imo = inp->inp_moptions;
1517 
1518 	error = 0;
1519 	switch (sopt->sopt_name) {
1520 	case IP_MULTICAST_VIF:
1521 		if (imo != NULL)
1522 			optval = imo->imo_multicast_vif;
1523 		else
1524 			optval = -1;
1525 		INP_UNLOCK(inp);
1526 		error = sooptcopyout(sopt, &optval, sizeof optval);
1527 		break;
1528 
1529 	case IP_MULTICAST_IF:
1530 		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1531 			addr.s_addr = INADDR_ANY;
1532 		else if (imo->imo_multicast_addr.s_addr) {
1533 			/* return the value user has set */
1534 			addr = imo->imo_multicast_addr;
1535 		} else {
1536 			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1537 			addr.s_addr = (ia == NULL) ? INADDR_ANY
1538 				: IA_SIN(ia)->sin_addr.s_addr;
1539 		}
1540 		INP_UNLOCK(inp);
1541 		error = sooptcopyout(sopt, &addr, sizeof addr);
1542 		break;
1543 
1544 	case IP_MULTICAST_TTL:
1545 		if (imo == 0)
1546 			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1547 		else
1548 			optval = coptval = imo->imo_multicast_ttl;
1549 		INP_UNLOCK(inp);
1550 		if (sopt->sopt_valsize == 1)
1551 			error = sooptcopyout(sopt, &coptval, 1);
1552 		else
1553 			error = sooptcopyout(sopt, &optval, sizeof optval);
1554 		break;
1555 
1556 	case IP_MULTICAST_LOOP:
1557 		if (imo == 0)
1558 			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1559 		else
1560 			optval = coptval = imo->imo_multicast_loop;
1561 		INP_UNLOCK(inp);
1562 		if (sopt->sopt_valsize == 1)
1563 			error = sooptcopyout(sopt, &coptval, 1);
1564 		else
1565 			error = sooptcopyout(sopt, &optval, sizeof optval);
1566 		break;
1567 
1568 	default:
1569 		INP_UNLOCK(inp);
1570 		error = ENOPROTOOPT;
1571 		break;
1572 	}
1573 	INP_UNLOCK_ASSERT(inp);
1574 
1575 	return (error);
1576 }
1577 
1578 /*
1579  * Discard the IP multicast options.
1580  */
1581 void
1582 ip_freemoptions(imo)
1583 	register struct ip_moptions *imo;
1584 {
1585 	register int i;
1586 
1587 	if (imo != NULL) {
1588 		for (i = 0; i < imo->imo_num_memberships; ++i)
1589 			in_delmulti(imo->imo_membership[i]);
1590 		free(imo->imo_membership, M_IPMOPTS);
1591 		free(imo, M_IPMOPTS);
1592 	}
1593 }
1594 
1595 /*
1596  * Routine called from ip_output() to loop back a copy of an IP multicast
1597  * packet to the input queue of a specified interface.  Note that this
1598  * calls the output routine of the loopback "driver", but with an interface
1599  * pointer that might NOT be a loopback interface -- evil, but easier than
1600  * replicating that code here.
1601  */
1602 static void
1603 ip_mloopback(ifp, m, dst, hlen)
1604 	struct ifnet *ifp;
1605 	register struct mbuf *m;
1606 	register struct sockaddr_in *dst;
1607 	int hlen;
1608 {
1609 	register struct ip *ip;
1610 	struct mbuf *copym;
1611 
1612 	copym = m_copy(m, 0, M_COPYALL);
1613 	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1614 		copym = m_pullup(copym, hlen);
1615 	if (copym != NULL) {
1616 		/* If needed, compute the checksum and mark it as valid. */
1617 		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1618 			in_delayed_cksum(copym);
1619 			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1620 			copym->m_pkthdr.csum_flags |=
1621 			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1622 			copym->m_pkthdr.csum_data = 0xffff;
1623 		}
1624 		/*
1625 		 * We don't bother to fragment if the IP length is greater
1626 		 * than the interface's MTU.  Can this possibly matter?
1627 		 */
1628 		ip = mtod(copym, struct ip *);
1629 		ip->ip_len = htons(ip->ip_len);
1630 		ip->ip_off = htons(ip->ip_off);
1631 		ip->ip_sum = 0;
1632 		ip->ip_sum = in_cksum(copym, hlen);
1633 		/*
1634 		 * NB:
1635 		 * It's not clear whether there are any lingering
1636 		 * reentrancy problems in other areas which might
1637 		 * be exposed by using ip_input directly (in
1638 		 * particular, everything which modifies the packet
1639 		 * in-place).  Yet another option is using the
1640 		 * protosw directly to deliver the looped back
1641 		 * packet.  For the moment, we'll err on the side
1642 		 * of safety by using if_simloop().
1643 		 */
1644 #if 1 /* XXX */
1645 		if (dst->sin_family != AF_INET) {
1646 			printf("ip_mloopback: bad address family %d\n",
1647 						dst->sin_family);
1648 			dst->sin_family = AF_INET;
1649 		}
1650 #endif
1651 
1652 #ifdef notdef
1653 		copym->m_pkthdr.rcvif = ifp;
1654 		ip_input(copym);
1655 #else
1656 		if_simloop(ifp, copym, dst->sin_family, 0);
1657 #endif
1658 	}
1659 }
1660