ip_output.c revision 1.5 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.5 1993/12/06 04:59:40 hpeyerl Exp $
35 */
36
37 #include "param.h"
38 #include "malloc.h"
39 #include "mbuf.h"
40 #include "errno.h"
41 #include "protosw.h"
42 #include "socket.h"
43 #include "socketvar.h"
44
45 #include "../net/if.h"
46 #include "../net/route.h"
47
48 #include "in.h"
49 #include "in_systm.h"
50 #include "ip.h"
51 #include "in_pcb.h"
52 #include "in_var.h"
53 #include "ip_var.h"
54
55 #ifdef vax
56 #include "machine/mtpr.h"
57 #endif
58
59 struct mbuf *ip_insertoptions();
60 void ip_mloopback __P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
61
62 /*
63 * IP output. The packet in mbuf chain m contains a skeletal IP
64 * header (with len, off, ttl, proto, tos, src, dst).
65 * The mbuf chain containing the packet will be freed.
66 * The mbuf opt, if present, will not be freed.
67 */
68 ip_output(m0, opt, ro, flags
69 #ifdef MULTICAST
70 ,imo
71 #endif
72 )
73 struct mbuf *m0;
74 struct mbuf *opt;
75 struct route *ro;
76 int flags;
77 #ifdef MULTICAST
78 struct ip_moptions *imo;
79 #endif
80 {
81 register struct ip *ip, *mhip;
82 register struct ifnet *ifp;
83 register struct mbuf *m = m0;
84 register int hlen = sizeof (struct ip);
85 int len, off, error = 0;
86 struct route iproute;
87 struct sockaddr_in *dst;
88 struct in_ifaddr *ia;
89
90 #ifdef DIAGNOSTIC
91 if ((m->m_flags & M_PKTHDR) == 0)
92 panic("ip_output no HDR");
93 #endif
94 if (opt) {
95 m = ip_insertoptions(m, opt, &len);
96 hlen = len;
97 }
98 ip = mtod(m, struct ip *);
99 /*
100 * Fill in IP header.
101 */
102 if ((flags & IP_FORWARDING) == 0) {
103 ip->ip_v = IPVERSION;
104 ip->ip_off &= IP_DF;
105 ip->ip_id = htons(ip_id++);
106 ip->ip_hl = hlen >> 2;
107 } else {
108 hlen = ip->ip_hl << 2;
109 ipstat.ips_localout++;
110 }
111 /*
112 * Route packet.
113 */
114 if (ro == 0) {
115 ro = &iproute;
116 bzero((caddr_t)ro, sizeof (*ro));
117 }
118 dst = (struct sockaddr_in *)&ro->ro_dst;
119 /*
120 * If there is a cached route,
121 * check that it is to the same destination
122 * and is still up. If not, free it and try again.
123 */
124 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
125 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
126 RTFREE(ro->ro_rt);
127 ro->ro_rt = (struct rtentry *)0;
128 }
129 if (ro->ro_rt == 0) {
130 dst->sin_family = AF_INET;
131 dst->sin_len = sizeof(*dst);
132 dst->sin_addr = ip->ip_dst;
133 }
134 /*
135 * If routing to interface only,
136 * short circuit routing lookup.
137 */
138 if (flags & IP_ROUTETOIF) {
139
140 ia = (struct in_ifaddr *)ifa_ifwithdstaddr((struct sockaddr *)dst);
141 if (ia == 0)
142 ia = in_iaonnetof(in_netof(ip->ip_dst));
143 if (ia == 0) {
144 error = ENETUNREACH;
145 goto bad;
146 }
147 ifp = ia->ia_ifp;
148 } else {
149 if (ro->ro_rt == 0)
150 rtalloc(ro);
151 if (ro->ro_rt == 0) {
152 error = EHOSTUNREACH;
153 goto bad;
154 }
155 ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa;
156 ifp = ro->ro_rt->rt_ifp;
157 ro->ro_rt->rt_use++;
158 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
159 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
160 }
161 #ifdef MULTICAST
162 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
163 struct in_multi *inm;
164 extern struct ifnet loif;
165 extern struct socket *ip_mrouter;
166
167 m->m_flags |= M_MCAST;
168 /*
169 * IP destination address is multicast. Make sure "dst"
170 * still points to the address in "ro". (It may have been
171 * changed to point to a gateway address, above.)
172 */
173 dst = (struct sockaddr_in *)&ro->ro_dst;
174 /*
175 * See if the caller provided any multicast options
176 */
177 if ((flags & IP_MULTICASTOPTS) && imo != NULL) {
178 ip->ip_ttl = imo->imo_multicast_ttl;
179 if (imo->imo_multicast_ifp != NULL)
180 ifp = imo->imo_multicast_ifp;
181 } else {
182 imo = NULL;
183 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
184 }
185 /*
186 * Confirm that the outgoing interface supports multicast.
187 */
188 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
189 error = ENETUNREACH;
190 goto bad;
191 }
192 /*
193 * If source address not specified yet, use address
194 * of outgoing interface.
195 */
196 if (ip->ip_src.s_addr == INADDR_ANY) {
197 register struct in_ifaddr *ia;
198
199 for (ia = in_ifaddr; ia; ia = ia->ia_next)
200 if (ia->ia_ifp == ifp) {
201 ip->ip_src = IA_SIN(ia)->sin_addr;
202 break;
203 }
204 }
205
206 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
207 if (inm != NULL &&
208 (imo == NULL || imo->imo_multicast_loop)) {
209 /*
210 * If we belong to the destination multicast group
211 * on the outgoing interface, and the caller did not
212 * forbid loopback, loop back a copy.
213 */
214 ip_mloopback(ifp, m, dst);
215 }
216 #ifdef MROUTING
217 else if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
218 /*
219 * If we are acting as a multicast router, perform
220 * multicast forwarding as if the packet had just
221 * arrived on the interface to which we are about
222 * to send. The multicast forwarding function
223 * recursively calls this function, using the
224 * IP_FORWARDING flag to prevent infinite recursion.
225 *
226 * Multicasts that are looped back by ip_mloopback(),
227 * above, will be forwarded by the ip_input() routine,
228 * if necessary.
229 */
230 if (ip_mforward(ip, ifp, m) != 0) {
231 m_freem(m);
232 goto done;
233 }
234 }
235 #endif
236 /*
237 * Multicasts with a time-to-live of zero may be looped-
238 * back, above, but must not be transmitted on a network.
239 * Also, multicasts addressed to the loopback interface
240 * are not sent -- the above call to ip_mloopback() will
241 * loop back a copy if this host actually belongs to the
242 * destination group on the loopback interface.
243 */
244 if (ip->ip_ttl == 0 || ifp == &loif) {
245 m_freem(m);
246 goto done;
247 }
248
249 goto sendit;
250 }
251 #endif
252 #ifndef notdef
253 /*
254 * If source address not specified yet, use address
255 * of outgoing interface.
256 */
257 if (ip->ip_src.s_addr == INADDR_ANY)
258 ip->ip_src = IA_SIN(ia)->sin_addr;
259 #endif
260
261 /*
262 * Verify that we have any chance at all of being able to queue
263 * the packet or packet fragments
264 */
265 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
266 ifp->if_snd.ifq_maxlen) {
267 error = ENOBUFS;
268 goto bad;
269 }
270
271 /*
272 * Look for broadcast address and
273 * and verify user is allowed to send
274 * such a packet.
275 */
276 if (in_broadcast(dst->sin_addr)) {
277 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
278 error = EADDRNOTAVAIL;
279 goto bad;
280 }
281 if ((flags & IP_ALLOWBROADCAST) == 0) {
282 error = EACCES;
283 goto bad;
284 }
285 /* don't allow broadcast messages to be fragmented */
286 if ((u_short)ip->ip_len > ifp->if_mtu) {
287 error = EMSGSIZE;
288 goto bad;
289 }
290 m->m_flags |= M_BCAST;
291 }
292 #ifdef MULTICAST
293 sendit:
294 #endif
295
296 /*
297 * If small enough for interface, can just send directly.
298 */
299 if ((u_short)ip->ip_len <= ifp->if_mtu) {
300 ip->ip_len = htons((u_short)ip->ip_len);
301 ip->ip_off = htons((u_short)ip->ip_off);
302 ip->ip_sum = 0;
303 ip->ip_sum = in_cksum(m, hlen);
304 error = (*ifp->if_output)(ifp, m,
305 (struct sockaddr *)dst, ro->ro_rt);
306 goto done;
307 }
308 ipstat.ips_fragmented++;
309 /*
310 * Too large for interface; fragment if possible.
311 * Must be able to put at least 8 bytes per fragment.
312 */
313 if (ip->ip_off & IP_DF) {
314 error = EMSGSIZE;
315 goto bad;
316 }
317 len = (ifp->if_mtu - hlen) &~ 7;
318 if (len < 8) {
319 error = EMSGSIZE;
320 goto bad;
321 }
322
323 {
324 int mhlen, firstlen = len;
325 struct mbuf **mnext = &m->m_nextpkt;
326
327 /*
328 * Loop through length of segment after first fragment,
329 * make new header and copy data of each part and link onto chain.
330 */
331 m0 = m;
332 mhlen = sizeof (struct ip);
333 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
334 MGETHDR(m, M_DONTWAIT, MT_HEADER);
335 if (m == 0) {
336 error = ENOBUFS;
337 goto sendorfree;
338 }
339 m->m_data += max_linkhdr;
340 mhip = mtod(m, struct ip *);
341 *mhip = *ip;
342 if (hlen > sizeof (struct ip)) {
343 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
344 mhip->ip_hl = mhlen >> 2;
345 }
346 m->m_len = mhlen;
347 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
348 if (ip->ip_off & IP_MF)
349 mhip->ip_off |= IP_MF;
350 if (off + len >= (u_short)ip->ip_len)
351 len = (u_short)ip->ip_len - off;
352 else
353 mhip->ip_off |= IP_MF;
354 mhip->ip_len = htons((u_short)(len + mhlen));
355 m->m_next = m_copy(m0, off, len);
356 if (m->m_next == 0) {
357 error = ENOBUFS; /* ??? */
358 goto sendorfree;
359 }
360 m->m_pkthdr.len = mhlen + len;
361 m->m_pkthdr.rcvif = (struct ifnet *)0;
362 mhip->ip_off = htons((u_short)mhip->ip_off);
363 mhip->ip_sum = 0;
364 mhip->ip_sum = in_cksum(m, mhlen);
365 *mnext = m;
366 mnext = &m->m_nextpkt;
367 ipstat.ips_ofragments++;
368 }
369 /*
370 * Update first fragment by trimming what's been copied out
371 * and updating header, then send each fragment (in order).
372 */
373 m = m0;
374 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
375 m->m_pkthdr.len = hlen + firstlen;
376 ip->ip_len = htons((u_short)m->m_pkthdr.len);
377 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
378 ip->ip_sum = 0;
379 ip->ip_sum = in_cksum(m, hlen);
380 sendorfree:
381 for (m = m0; m; m = m0) {
382 m0 = m->m_nextpkt;
383 m->m_nextpkt = 0;
384 if (error == 0)
385 error = (*ifp->if_output)(ifp, m,
386 (struct sockaddr *)dst, ro->ro_rt);
387 else
388 m_freem(m);
389 }
390 }
391 done:
392 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
393 RTFREE(ro->ro_rt);
394 return (error);
395 bad:
396 m_freem(m0);
397 goto done;
398 }
399
400 /*
401 * Insert IP options into preformed packet.
402 * Adjust IP destination as required for IP source routing,
403 * as indicated by a non-zero in_addr at the start of the options.
404 */
405 struct mbuf *
406 ip_insertoptions(m, opt, phlen)
407 register struct mbuf *m;
408 struct mbuf *opt;
409 int *phlen;
410 {
411 register struct ipoption *p = mtod(opt, struct ipoption *);
412 struct mbuf *n;
413 register struct ip *ip = mtod(m, struct ip *);
414 unsigned optlen;
415
416 optlen = opt->m_len - sizeof(p->ipopt_dst);
417 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
418 return (m); /* XXX should fail */
419 if (p->ipopt_dst.s_addr)
420 ip->ip_dst = p->ipopt_dst;
421 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
422 MGETHDR(n, M_DONTWAIT, MT_HEADER);
423 if (n == 0)
424 return (m);
425 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
426 m->m_len -= sizeof(struct ip);
427 m->m_data += sizeof(struct ip);
428 n->m_next = m;
429 m = n;
430 m->m_len = optlen + sizeof(struct ip);
431 m->m_data += max_linkhdr;
432 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
433 } else {
434 m->m_data -= optlen;
435 m->m_len += optlen;
436 m->m_pkthdr.len += optlen;
437 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
438 }
439 ip = mtod(m, struct ip *);
440 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
441 *phlen = sizeof(struct ip) + optlen;
442 ip->ip_len += optlen;
443 return (m);
444 }
445
446 /*
447 * Copy options from ip to jp,
448 * omitting those not copied during fragmentation.
449 */
450 ip_optcopy(ip, jp)
451 struct ip *ip, *jp;
452 {
453 register u_char *cp, *dp;
454 int opt, optlen, cnt;
455
456 cp = (u_char *)(ip + 1);
457 dp = (u_char *)(jp + 1);
458 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
459 for (; cnt > 0; cnt -= optlen, cp += optlen) {
460 opt = cp[0];
461 if (opt == IPOPT_EOL)
462 break;
463 if (opt == IPOPT_NOP)
464 optlen = 1;
465 else
466 optlen = cp[IPOPT_OLEN];
467 /* bogus lengths should have been caught by ip_dooptions */
468 if (optlen > cnt)
469 optlen = cnt;
470 if (IPOPT_COPIED(opt)) {
471 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
472 dp += optlen;
473 }
474 }
475 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
476 *dp++ = IPOPT_EOL;
477 return (optlen);
478 }
479
480 /*
481 * IP socket option processing.
482 */
483 ip_ctloutput(op, so, level, optname, mp)
484 int op;
485 struct socket *so;
486 int level, optname;
487 struct mbuf **mp;
488 {
489 register struct inpcb *inp = sotoinpcb(so);
490 register struct mbuf *m = *mp;
491 register int optval;
492 int error = 0;
493
494 if (level != IPPROTO_IP)
495 error = EINVAL;
496 else switch (op) {
497
498 case PRCO_SETOPT:
499 switch (optname) {
500 case IP_OPTIONS:
501 #ifdef notyet
502 case IP_RETOPTS:
503 return (ip_pcbopts(optname, &inp->inp_options, m));
504 #else
505 return (ip_pcbopts(&inp->inp_options, m));
506 #endif
507
508 case IP_TOS:
509 case IP_TTL:
510 case IP_RECVOPTS:
511 case IP_RECVRETOPTS:
512 case IP_RECVDSTADDR:
513 if (m->m_len != sizeof(int))
514 error = EINVAL;
515 else {
516 optval = *mtod(m, int *);
517 switch (optname) {
518
519 case IP_TOS:
520 inp->inp_ip.ip_tos = optval;
521 break;
522
523 case IP_TTL:
524 inp->inp_ip.ip_ttl = optval;
525 break;
526 #define OPTSET(bit) \
527 if (optval) \
528 inp->inp_flags |= bit; \
529 else \
530 inp->inp_flags &= ~bit;
531
532 case IP_RECVOPTS:
533 OPTSET(INP_RECVOPTS);
534 break;
535
536 case IP_RECVRETOPTS:
537 OPTSET(INP_RECVRETOPTS);
538 break;
539
540 case IP_RECVDSTADDR:
541 OPTSET(INP_RECVDSTADDR);
542 break;
543 }
544 }
545 break;
546 #undef OPTSET
547 #ifdef MULTICAST
548 case IP_MULTICAST_IF:
549 case IP_MULTICAST_TTL:
550 case IP_MULTICAST_LOOP:
551 case IP_ADD_MEMBERSHIP:
552 case IP_DROP_MEMBERSHIP:
553 error = ip_setmoptions(optname, &inp->inp_moptions, m);
554 break;
555 #endif
556
557 default:
558 error = EINVAL;
559 break;
560 }
561 if (m)
562 (void)m_free(m);
563 break;
564
565 case PRCO_GETOPT:
566 switch (optname) {
567 case IP_OPTIONS:
568 case IP_RETOPTS:
569 *mp = m = m_get(M_WAIT, MT_SOOPTS);
570 if (inp->inp_options) {
571 m->m_len = inp->inp_options->m_len;
572 bcopy(mtod(inp->inp_options, caddr_t),
573 mtod(m, caddr_t), (unsigned)m->m_len);
574 } else
575 m->m_len = 0;
576 break;
577
578 case IP_TOS:
579 case IP_TTL:
580 case IP_RECVOPTS:
581 case IP_RECVRETOPTS:
582 case IP_RECVDSTADDR:
583 *mp = m = m_get(M_WAIT, MT_SOOPTS);
584 m->m_len = sizeof(int);
585 switch (optname) {
586
587 case IP_TOS:
588 optval = inp->inp_ip.ip_tos;
589 break;
590
591 case IP_TTL:
592 optval = inp->inp_ip.ip_ttl;
593 break;
594
595 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
596
597 case IP_RECVOPTS:
598 optval = OPTBIT(INP_RECVOPTS);
599 break;
600
601 case IP_RECVRETOPTS:
602 optval = OPTBIT(INP_RECVRETOPTS);
603 break;
604
605 case IP_RECVDSTADDR:
606 optval = OPTBIT(INP_RECVDSTADDR);
607 break;
608 }
609 *mtod(m, int *) = optval;
610 break;
611 #ifdef MULTICAST
612 case IP_MULTICAST_IF:
613 case IP_MULTICAST_TTL:
614 case IP_MULTICAST_LOOP:
615 case IP_ADD_MEMBERSHIP:
616 case IP_DROP_MEMBERSHIP:
617 error = ip_getmoptions(optname, &inp->inp_moptions, m);
618 break;
619 #endif
620
621 default:
622 error = EINVAL;
623 break;
624 }
625 break;
626 }
627 return (error);
628 }
629
630 /*
631 * Set up IP options in pcb for insertion in output packets.
632 * Store in mbuf with pointer in pcbopt, adding pseudo-option
633 * with destination address if source routed.
634 */
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 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);
1068 }
1069 }
1070 #endif
1071