tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / netinet / ip_output.c
blob7ebb074e6fd423d83de508f2299ce1d16fdd6803
1 /*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2005 - 2006 Pavel Fedin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
36 #include <conf.h>
38 #include <libraries/miami.h>
39 #include <sys/param.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/errno.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/synch.h>
47 #include <sys/syslog.h>
49 #include <net/route.h>
50 #include <net/if.h>
51 #include <net/if_protos.h>
52 #include <net/pfil.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
61 #include <kern/amiga_subr.h>
63 #ifdef vax
64 #include <machine/mtpr.h>
65 #endif
67 #ifndef IP_RAWOUTPUT
68 #define IP_RAWOUTPUT 0
69 #endif
71 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *));
72 #ifdef ENABLE_MULTICAST
73 static void ip_mloopback
74 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *));
75 #endif
78 * IP output. The packet in mbuf chain m contains a skeletal IP
79 * header (with len, off, ttl, proto, tos, src, dst).
80 * The mbuf chain containing the packet will be freed.
81 * The mbuf opt, if present, will not be freed.
83 int STKARGFUN ip_output(void *args, ...)
85 struct mbuf *m0 = args;
86 struct mbuf *opt;
87 struct route *ro;
88 int flags;
89 #ifdef ENABLE_MULTICAST
90 struct ip_moptions *imo;
91 #endif
92 register struct ip *ip, *mhip;
93 register struct ifnet *ifp;
94 register struct mbuf *m = m0;
95 register int hlen = sizeof (struct ip);
96 int len, off, error = 0;
97 struct route iproute;
98 struct sockaddr_in *dst;
99 struct in_ifaddr *ia;
100 va_list va;
102 va_start(va, args);
103 opt = va_arg(va, struct mbuf *);
104 ro = va_arg(va, struct route *);
105 flags = va_arg(va, int);
106 #ifdef ENABLE_MULTICAST
107 imo = v_arg(va, struct ip_moptions *);
108 #endif
109 va_end(va);
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;
119 ip = mtod(m, struct ip *);
121 * Fill in IP header.
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;
133 * Route packet.
135 if (ro == 0) {
136 ro = &iproute;
137 bzero((caddr_t)ro, sizeof (*ro));
139 dst = (struct sockaddr_in *)&ro->ro_dst;
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.
145 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
146 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
147 RTFREE(ro->ro_rt);
148 ro->ro_rt = (struct rtentry *)0;
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;
156 * If routing to interface only,
157 * short circuit routing lookup.
159 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
160 #define sintosa(sin) ((struct sockaddr *)(sin))
161 if (flags & IP_ROUTETOIF) {
162 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
163 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
164 ipstat.ips_noroute++;
165 error = ENETUNREACH;
166 goto bad;
168 ifp = ia->ia_ifp;
169 ip->ip_ttl = 1;
170 } else {
171 if (ro->ro_rt == 0)
172 rtalloc(ro);
173 if (ro->ro_rt == 0) {
174 ipstat.ips_noroute++;
175 error = EHOSTUNREACH;
176 DROUTE(log(LOG_DEBUG,"ip_output(): packet dropped\n");)
177 goto bad;
179 ia = ifatoia(ro->ro_rt->rt_ifa);
180 ifp = ro->ro_rt->rt_ifp;
181 ro->ro_rt->rt_use++;
182 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
183 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
185 #ifdef ENABLE_MULTICAST
186 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
187 struct in_multi *inm;
188 extern struct ifnet loif;
190 m->m_flags |= M_MCAST;
192 * IP destination address is multicast. Make sure "dst"
193 * still points to the address in "ro". (It may have been
194 * changed to point to a gateway address, above.)
196 dst = (struct sockaddr_in *)&ro->ro_dst;
198 * See if the caller provided any multicast options
200 if (imo != NULL) {
201 ip->ip_ttl = imo->imo_multicast_ttl;
202 if (imo->imo_multicast_ifp != NULL)
203 ifp = imo->imo_multicast_ifp;
204 } else
205 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
207 * Confirm that the outgoing interface supports multicast.
209 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
210 ipstat.ips_noroute++;
211 error = ENETUNREACH;
212 goto bad;
215 * If source address not specified yet, use address
216 * of outgoing interface.
218 if (ip->ip_src.s_addr == INADDR_ANY) {
219 register struct in_ifaddr *ia;
221 for (ia = in_ifaddr; ia; ia = ia->ia_next)
222 if (ia->ia_ifp == ifp) {
223 ip->ip_src = IA_SIN(ia)->sin_addr;
224 break;
228 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
229 if (inm != NULL &&
230 (imo == NULL || imo->imo_multicast_loop)) {
232 * If we belong to the destination multicast group
233 * on the outgoing interface, and the caller did not
234 * forbid loopback, loop back a copy.
236 ip_mloopback(ifp, m, dst);
238 #ifdef MROUTING
239 else {
241 * If we are acting as a multicast router, perform
242 * multicast forwarding as if the packet had just
243 * arrived on the interface to which we are about
244 * to send. The multicast forwarding function
245 * recursively calls this function, using the
246 * IP_FORWARDING flag to prevent infinite recursion.
248 * Multicasts that are looped back by ip_mloopback(),
249 * above, will be forwarded by the ip_input() routine,
250 * if necessary.
252 extern struct socket *ip_mrouter;
253 if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
254 if (ip_mforward(m, ifp) != 0) {
255 m_freem(m);
256 goto done;
260 #endif
262 * Multicasts with a time-to-live of zero may be looped-
263 * back, above, but must not be transmitted on a network.
264 * Also, multicasts addressed to the loopback interface
265 * are not sent -- the above call to ip_mloopback() will
266 * loop back a copy if this host actually belongs to the
267 * destination group on the loopback interface.
269 if (ip->ip_ttl == 0 || ifp == &loif) {
270 m_freem(m);
271 goto done;
274 goto sendit;
276 #endif
277 #ifndef notdef
279 * If source address not specified yet, use address
280 * of outgoing interface.
282 if (ip->ip_src.s_addr == INADDR_ANY)
283 ip->ip_src = IA_SIN(ia)->sin_addr;
284 #endif
286 * Look for broadcast address and
287 * and verify user is allowed to send
288 * such a packet.
290 /* TODO: NicJA - Function proto changed?? needs reflected in protos/netinet/in_protos.h */
291 #if !defined(__AROS__)
292 if (in_broadcast(dst->sin_addr, ifp)) {
293 #else
294 if (in_broadcast(dst->sin_addr)) {
295 #endif
296 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
297 error = EADDRNOTAVAIL;
298 goto bad;
300 if ((flags & IP_ALLOWBROADCAST) == 0) {
301 error = EACCES;
302 goto bad;
304 /* don't allow broadcast messages to be fragmented */
305 if ((u_short)ip->ip_len > ifp->if_mtu) {
306 error = EMSGSIZE;
307 goto bad;
309 m->m_flags |= M_BCAST;
310 } else
311 m->m_flags &= ~M_BCAST;
313 #ifdef ENABLE_MULTICAST
314 sendit:
315 #endif
316 /* Run through list of hooks */
317 pfil_run_hooks(m, ifp, MIAMIPFBPT_IP);
320 * If small enough for interface, can just send directly.
322 if ((u_short)ip->ip_len <= ifp->if_mtu) {
323 ip->ip_len = htons((u_short)ip->ip_len);
324 ip->ip_off = htons((u_short)ip->ip_off);
325 ip->ip_sum = 0;
326 ip->ip_sum = in_cksum(m, hlen);
327 error = (*ifp->if_output)(ifp, m,
328 (struct sockaddr *)dst, ro->ro_rt);
329 goto done;
332 * Too large for interface; fragment if possible.
333 * Must be able to put at least 8 bytes per fragment.
335 if (ip->ip_off & IP_DF) {
336 error = EMSGSIZE;
337 ipstat.ips_cantfrag++;
338 goto bad;
340 len = (ifp->if_mtu - hlen) &~ 7;
341 if (len < 8) {
342 error = EMSGSIZE;
343 goto bad;
347 int mhlen, firstlen = len;
348 struct mbuf **mnext = &m->m_nextpkt;
351 * Loop through length of segment after first fragment,
352 * make new header and copy data of each part and link onto chain.
354 m0 = m;
355 mhlen = sizeof (struct ip);
356 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
357 MGETHDR(m, M_DONTWAIT, MT_HEADER);
358 if (m == 0) {
359 error = ENOBUFS;
360 ipstat.ips_odropped++;
361 goto sendorfree;
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;
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_short)ip->ip_len)
375 len = (u_short)ip->ip_len - off;
376 else
377 mhip->ip_off |= IP_MF;
378 mhip->ip_len = htons((u_short)(len + mhlen));
379 m->m_next = m_copy(m0, off, len);
380 if (m->m_next == 0) {
381 (void) m_free(m);
382 error = ENOBUFS; /* ??? */
383 ipstat.ips_odropped++;
384 goto sendorfree;
386 m->m_pkthdr.len = mhlen + len;
387 m->m_pkthdr.rcvif = (struct ifnet *)0;
388 mhip->ip_off = htons((u_short)mhip->ip_off);
389 mhip->ip_sum = 0;
390 mhip->ip_sum = in_cksum(m, mhlen);
391 *mnext = m;
392 mnext = &m->m_nextpkt;
393 ipstat.ips_ofragments++;
396 * Update first fragment by trimming what's been copied out
397 * and updating header, then send each fragment (in order).
399 m = m0;
400 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
401 m->m_pkthdr.len = hlen + firstlen;
402 ip->ip_len = htons((u_short)m->m_pkthdr.len);
403 ip->ip_off = htons((u_short)(ip->ip_off | IP_MF));
404 ip->ip_sum = 0;
405 ip->ip_sum = in_cksum(m, hlen);
406 sendorfree:
407 for (m = m0; m; m = m0) {
408 m0 = m->m_nextpkt;
409 m->m_nextpkt = 0;
410 if (error == 0)
411 error = (*ifp->if_output)(ifp, m,
412 (struct sockaddr *)dst, ro->ro_rt);
413 else
414 m_freem(m);
417 if (error == 0)
418 ipstat.ips_fragmented++;
420 done:
421 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt)
422 RTFREE(ro->ro_rt);
423 return (error);
424 bad:
425 m_freem(m0);
426 goto done;
430 * Insert IP options into preformed packet.
431 * Adjust IP destination as required for IP source routing,
432 * as indicated by a non-zero in_addr at the start of the options.
434 static struct mbuf *
435 ip_insertoptions(m, opt, phlen)
436 register struct mbuf *m;
437 struct mbuf *opt;
438 int *phlen;
440 register struct ipoption *p = mtod(opt, struct ipoption *);
441 struct mbuf *n;
442 register struct ip *ip = mtod(m, struct ip *);
443 unsigned optlen;
445 optlen = opt->m_len - sizeof(p->ipopt_dst);
446 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
447 return (m); /* XXX should fail */
448 if (p->ipopt_dst.s_addr)
449 ip->ip_dst = p->ipopt_dst;
450 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
451 MGETHDR(n, M_DONTWAIT, MT_HEADER);
452 if (n == 0)
453 return (m);
454 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
455 m->m_len -= sizeof(struct ip);
456 m->m_data += sizeof(struct ip);
457 n->m_next = m;
458 m = n;
459 m->m_len = optlen + sizeof(struct ip);
460 m->m_data += max_linkhdr;
461 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
462 } else {
463 m->m_data -= optlen;
464 m->m_len += optlen;
465 m->m_pkthdr.len += optlen;
466 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
468 ip = mtod(m, struct ip *);
469 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen);
470 *phlen = sizeof(struct ip) + optlen;
471 ip->ip_len += optlen;
472 return (m);
476 * Copy options from ip to jp,
477 * omitting those not copied during fragmentation.
480 ip_optcopy(ip, jp)
481 struct ip *ip, *jp;
483 register u_char *cp, *dp;
484 int opt, optlen, cnt;
486 cp = (u_char *)(ip + 1);
487 dp = (u_char *)(jp + 1);
488 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
489 for (; cnt > 0; cnt -= optlen, cp += optlen) {
490 opt = cp[0];
491 if (opt == IPOPT_EOL)
492 break;
493 if (opt == IPOPT_NOP) {
494 /* Preserve for IP mcast tunnel's LSRR alignment. */
495 *dp++ = IPOPT_NOP;
496 optlen = 1;
497 continue;
498 } else
499 optlen = cp[IPOPT_OLEN];
500 /* bogus lengths should have been caught by ip_dooptions */
501 if (optlen > cnt)
502 optlen = cnt;
503 if (IPOPT_COPIED(opt)) {
504 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen);
505 dp += optlen;
508 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
509 *dp++ = IPOPT_EOL;
510 return (optlen);
514 * IP socket option processing.
517 ip_ctloutput(op, so, level, optname, mp)
518 int op;
519 struct socket *so;
520 int level, optname;
521 struct mbuf **mp;
523 register struct inpcb *inp = sotoinpcb(so);
524 register struct mbuf *m = *mp;
525 register int optval;
526 int error = 0;
528 if (level != IPPROTO_IP) {
529 error = EINVAL;
530 if (op == PRCO_SETOPT && *mp)
531 (void) m_free(*mp);
532 } else switch (op) {
534 case PRCO_SETOPT:
535 switch (optname) {
536 case IP_OPTIONS:
537 #ifdef notyet
538 case IP_RETOPTS:
539 return (ip_pcbopts(optname, &inp->inp_options, m));
540 #else
541 return (ip_pcbopts(&inp->inp_options, m));
542 #endif
544 case IP_TOS:
545 case IP_TTL:
546 case IP_RECVOPTS:
547 case IP_RECVRETOPTS:
548 case IP_RECVDSTADDR:
549 if (m->m_len != sizeof(int))
550 error = EINVAL;
551 else {
552 optval = *mtod(m, int *);
553 switch (optname) {
555 case IP_TOS:
556 inp->inp_ip.ip_tos = optval;
557 break;
559 case IP_TTL:
560 inp->inp_ip.ip_ttl = optval;
561 break;
562 #define OPTSET(bit) \
563 if (optval) \
564 inp->inp_flags |= bit; \
565 else \
566 inp->inp_flags &= ~bit;
568 case IP_RECVOPTS:
569 OPTSET(INP_RECVOPTS);
570 break;
572 case IP_RECVRETOPTS:
573 OPTSET(INP_RECVRETOPTS);
574 break;
576 case IP_RECVDSTADDR:
577 OPTSET(INP_RECVDSTADDR);
578 break;
581 break;
582 #undef OPTSET
583 #ifdef ENABLE_MULTICAST
584 case IP_MULTICAST_IF:
585 case IP_MULTICAST_TTL:
586 case IP_MULTICAST_LOOP:
587 case IP_ADD_MEMBERSHIP:
588 case IP_DROP_MEMBERSHIP:
589 error = ip_setmoptions(optname, &inp->inp_moptions, m);
590 break;
591 #endif
592 default:
593 error = ENOPROTOOPT;
594 break;
596 if (m)
597 (void)m_free(m);
598 break;
600 case PRCO_GETOPT:
601 switch (optname) {
602 case IP_OPTIONS:
603 case IP_RETOPTS:
604 *mp = m = m_get(M_WAIT, MT_SOOPTS);
605 if (inp->inp_options) {
606 m->m_len = inp->inp_options->m_len;
607 bcopy(mtod(inp->inp_options, caddr_t),
608 mtod(m, caddr_t), (unsigned)m->m_len);
609 } else
610 m->m_len = 0;
611 break;
613 case IP_TOS:
614 case IP_TTL:
615 case IP_RECVOPTS:
616 case IP_RECVRETOPTS:
617 case IP_RECVDSTADDR:
618 *mp = m = m_get(M_WAIT, MT_SOOPTS);
619 m->m_len = sizeof(int);
620 switch (optname) {
622 case IP_TOS:
623 optval = inp->inp_ip.ip_tos;
624 break;
626 case IP_TTL:
627 optval = inp->inp_ip.ip_ttl;
628 break;
630 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
632 case IP_RECVOPTS:
633 optval = OPTBIT(INP_RECVOPTS);
634 break;
636 case IP_RECVRETOPTS:
637 optval = OPTBIT(INP_RECVRETOPTS);
638 break;
640 case IP_RECVDSTADDR:
641 optval = OPTBIT(INP_RECVDSTADDR);
642 break;
644 *mtod(m, int *) = optval;
645 break;
646 #ifdef ENABLE_MULTICAST
647 case IP_MULTICAST_IF:
648 case IP_MULTICAST_TTL:
649 case IP_MULTICAST_LOOP:
650 case IP_ADD_MEMBERSHIP:
651 case IP_DROP_MEMBERSHIP:
652 error = ip_getmoptions(optname, inp->inp_moptions, mp);
653 break;
654 #endif
655 default:
656 error = ENOPROTOOPT;
657 break;
659 break;
661 return (error);
665 * Set up IP options in pcb for insertion in output packets.
666 * Store in mbuf with pointer in pcbopt, adding pseudo-option
667 * with destination address if source routed.
670 #ifdef notyet
671 ip_pcbopts(optname, pcbopt, m)
672 int optname;
673 #else
674 ip_pcbopts(pcbopt, m)
675 #endif
676 struct mbuf **pcbopt;
677 register struct mbuf *m;
679 register int cnt, optlen;
680 register u_char *cp;
681 u_char opt;
683 /* turn off any old options */
684 if (*pcbopt)
685 (void)m_free(*pcbopt);
686 *pcbopt = 0;
687 if (m == (struct mbuf *)0 || m->m_len == 0) {
689 * Only turning off any previous options.
691 if (m)
692 (void)m_free(m);
693 return (0);
696 #ifndef vax
697 if (m->m_len % sizeof(long))
698 goto bad;
699 #endif
701 * IP first-hop destination address will be stored before
702 * actual options; move other options back
703 * and clear it when none present.
705 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
706 goto bad;
707 cnt = m->m_len;
708 m->m_len += sizeof(struct in_addr);
709 cp = mtod(m, u_char *) + sizeof(struct in_addr);
710 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
711 bzero(mtod(m, caddr_t), sizeof(struct in_addr));
713 for (; cnt > 0; cnt -= optlen, cp += optlen) {
714 opt = cp[IPOPT_OPTVAL];
715 if (opt == IPOPT_EOL)
716 break;
717 if (opt == IPOPT_NOP)
718 optlen = 1;
719 else {
720 optlen = cp[IPOPT_OLEN];
721 if (optlen <= IPOPT_OLEN || optlen > cnt)
722 goto bad;
724 switch (opt) {
726 default:
727 break;
729 case IPOPT_LSRR:
730 case IPOPT_SSRR:
732 * user process specifies route as:
733 * ->A->B->C->D
734 * D must be our final destination (but we can't
735 * check that since we may not have connected yet).
736 * A is first hop destination, which doesn't appear in
737 * actual IP option, but is stored before the options.
739 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
740 goto bad;
741 m->m_len -= sizeof(struct in_addr);
742 cnt -= sizeof(struct in_addr);
743 optlen -= sizeof(struct in_addr);
744 cp[IPOPT_OLEN] = optlen;
746 * Move first hop before start of options.
748 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
749 sizeof(struct in_addr));
751 * Then copy rest of options back
752 * to close up the deleted entry.
754 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
755 sizeof(struct in_addr)),
756 (caddr_t)&cp[IPOPT_OFFSET+1],
757 (unsigned)cnt + sizeof(struct in_addr));
758 break;
761 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
762 goto bad;
763 *pcbopt = m;
764 return (0);
766 bad:
767 (void)m_free(m);
768 return (EINVAL);
771 #ifdef ENABLE_MULTICAST
773 * Set the IP multicast options in response to user setsockopt().
776 ip_setmoptions(optname, imop, m)
777 int optname;
778 struct ip_moptions **imop;
779 struct mbuf *m;
781 register int error = 0;
782 u_char loop;
783 register int i;
784 struct in_addr addr;
785 register struct ip_mreq *mreq;
786 register struct ifnet *ifp;
787 register struct ip_moptions *imo = *imop;
788 struct route ro;
789 register struct sockaddr_in *dst;
791 if (imo == NULL) {
793 * No multicast option buffer attached to the pcb;
794 * allocate one and initialize to default values.
796 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
797 M_WAITOK);
799 if (imo == NULL)
800 return (ENOBUFS);
801 *imop = imo;
802 imo->imo_multicast_ifp = NULL;
803 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
804 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
805 imo->imo_num_memberships = 0;
808 switch (optname) {
810 case IP_MULTICAST_IF:
812 * Select the interface for outgoing multicast packets.
814 if (m == NULL || m->m_len != sizeof(struct in_addr)) {
815 error = EINVAL;
816 break;
818 addr = *(mtod(m, struct in_addr *));
820 * INADDR_ANY is used to remove a previous selection.
821 * When no interface is selected, a default one is
822 * chosen every time a multicast packet is sent.
824 if (addr.s_addr == INADDR_ANY) {
825 imo->imo_multicast_ifp = NULL;
826 break;
829 * The selected interface is identified by its local
830 * IP address. Find the interface and confirm that
831 * it supports multicasting.
833 INADDR_TO_IFP(addr, ifp);
834 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
835 error = EADDRNOTAVAIL;
836 break;
838 imo->imo_multicast_ifp = ifp;
839 break;
841 case IP_MULTICAST_TTL:
843 * Set the IP time-to-live for outgoing multicast packets.
845 if (m == NULL || m->m_len != 1) {
846 error = EINVAL;
847 break;
849 imo->imo_multicast_ttl = *(mtod(m, u_char *));
850 break;
852 case IP_MULTICAST_LOOP:
854 * Set the loopback flag for outgoing multicast packets.
855 * Must be zero or one.
857 if (m == NULL || m->m_len != 1 ||
858 (loop = *(mtod(m, u_char *))) > 1) {
859 error = EINVAL;
860 break;
862 imo->imo_multicast_loop = loop;
863 break;
865 case IP_ADD_MEMBERSHIP:
867 * Add a multicast group membership.
868 * Group must be a valid IP multicast address.
870 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
871 error = EINVAL;
872 break;
874 mreq = mtod(m, struct ip_mreq *);
875 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
876 error = EINVAL;
877 break;
880 * If no interface address was provided, use the interface of
881 * the route to the given multicast address.
883 if (mreq->imr_interface.s_addr == INADDR_ANY) {
884 ro.ro_rt = NULL;
885 dst = (struct sockaddr_in *)&ro.ro_dst;
886 dst->sin_len = sizeof(*dst);
887 dst->sin_family = AF_INET;
888 dst->sin_addr = mreq->imr_multiaddr;
889 rtalloc(&ro);
890 if (ro.ro_rt == NULL) {
891 error = EADDRNOTAVAIL;
892 break;
894 ifp = ro.ro_rt->rt_ifp;
895 rtfree(ro.ro_rt);
897 else {
898 INADDR_TO_IFP(mreq->imr_interface, ifp);
901 * See if we found an interface, and confirm that it
902 * supports multicast.
904 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
905 error = EADDRNOTAVAIL;
906 break;
909 * See if the membership already exists or if all the
910 * membership slots are full.
912 for (i = 0; i < imo->imo_num_memberships; ++i) {
913 if (imo->imo_membership[i]->inm_ifp == ifp &&
914 imo->imo_membership[i]->inm_addr.s_addr
915 == mreq->imr_multiaddr.s_addr)
916 break;
918 if (i < imo->imo_num_memberships) {
919 error = EADDRINUSE;
920 break;
922 if (i == IP_MAX_MEMBERSHIPS) {
923 error = ETOOMANYREFS;
924 break;
927 * Everything looks good; add a new record to the multicast
928 * address list for the given interface.
930 if ((imo->imo_membership[i] =
931 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
932 error = ENOBUFS;
933 break;
935 ++imo->imo_num_memberships;
936 break;
938 case IP_DROP_MEMBERSHIP:
940 * Drop a multicast group membership.
941 * Group must be a valid IP multicast address.
943 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) {
944 error = EINVAL;
945 break;
947 mreq = mtod(m, struct ip_mreq *);
948 if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
949 error = EINVAL;
950 break;
953 * If an interface address was specified, get a pointer
954 * to its ifnet structure.
956 if (mreq->imr_interface.s_addr == INADDR_ANY)
957 ifp = NULL;
958 else {
959 INADDR_TO_IFP(mreq->imr_interface, ifp);
960 if (ifp == NULL) {
961 error = EADDRNOTAVAIL;
962 break;
966 * Find the membership in the membership array.
968 for (i = 0; i < imo->imo_num_memberships; ++i) {
969 if ((ifp == NULL ||
970 imo->imo_membership[i]->inm_ifp == ifp) &&
971 imo->imo_membership[i]->inm_addr.s_addr ==
972 mreq->imr_multiaddr.s_addr)
973 break;
975 if (i == imo->imo_num_memberships) {
976 error = EADDRNOTAVAIL;
977 break;
980 * Give up the multicast address record to which the
981 * membership points.
983 in_delmulti(imo->imo_membership[i]);
985 * Remove the gap in the membership array.
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;
992 default:
993 error = EOPNOTSUPP;
994 break;
998 * If all options have default values, no need to keep the mbuf.
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;
1008 return (error);
1012 * Return the IP multicast options in response to user getsockopt().
1015 ip_getmoptions(optname, imo, mp)
1016 int optname;
1017 register struct ip_moptions *imo;
1018 register struct mbuf **mp;
1020 u_char *ttl;
1021 u_char *loop;
1022 struct in_addr *addr;
1023 struct in_ifaddr *ia;
1025 *mp = m_get(M_WAIT, MT_SOOPTS);
1027 switch (optname) {
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->s_addr = INADDR_ANY;
1034 else {
1035 IFP_TO_IA(imo->imo_multicast_ifp, ia);
1036 addr->s_addr = (ia == NULL) ? INADDR_ANY
1037 : IA_SIN(ia)->sin_addr.s_addr;
1039 return (0);
1041 case IP_MULTICAST_TTL:
1042 ttl = mtod(*mp, u_char *);
1043 (*mp)->m_len = 1;
1044 *ttl = (imo == NULL) ? IP_DEFAULT_MULTICAST_TTL
1045 : imo->imo_multicast_ttl;
1046 return (0);
1048 case IP_MULTICAST_LOOP:
1049 loop = mtod(*mp, u_char *);
1050 (*mp)->m_len = 1;
1051 *loop = (imo == NULL) ? IP_DEFAULT_MULTICAST_LOOP
1052 : imo->imo_multicast_loop;
1053 return (0);
1055 default:
1056 return (EOPNOTSUPP);
1061 * Discard the IP multicast options.
1063 void
1064 ip_freemoptions(imo)
1065 register struct ip_moptions *imo;
1067 register int i;
1069 if (imo != NULL) {
1070 for (i = 0; i < imo->imo_num_memberships; ++i)
1071 in_delmulti(imo->imo_membership[i]);
1072 free(imo, M_IPMOPTS);
1077 * Routine called from ip_output() to loop back a copy of an IP multicast
1078 * packet to the input queue of a specified interface. Note that this
1079 * calls the output routine of the loopback "driver", but with an interface
1080 * pointer that might NOT be &loif -- easier than replicating that code here.
1082 static void
1083 ip_mloopback(ifp, m, dst)
1084 struct ifnet *ifp;
1085 register struct mbuf *m;
1086 register struct sockaddr_in *dst;
1088 register struct ip *ip;
1089 struct mbuf *copym;
1091 copym = m_copy(m, 0, M_COPYALL);
1092 if (copym != NULL) {
1094 * We don't bother to fragment if the IP length is greater
1095 * than the interface's MTU. Can this possibly matter?
1097 ip = mtod(copym, struct ip *);
1098 ip->ip_len = htons((u_short)ip->ip_len);
1099 ip->ip_off = htons((u_short)ip->ip_off);
1100 ip->ip_sum = 0;
1101 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2);
1102 (void) looutput(ifp, copym, (struct sockaddr *)dst, NULL);
1105 #endif