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