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