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