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