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