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