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