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
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
34 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
38 #include <libraries/miami.h>
39 #include <sys/param.h>
40 #include <sys/malloc.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>
51 #include <net/if_protos.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>
64 #include <machine/mtpr.h>
68 #define IP_RAWOUTPUT 0
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
*));
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
;
89 #ifdef ENABLE_MULTICAST
90 struct ip_moptions
*imo
;
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;
98 struct sockaddr_in
*dst
;
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
*);
112 if ((m
->m_flags
& M_PKTHDR
) == 0)
113 panic("ip_output no HDR");
116 m
= ip_insertoptions(m
, opt
, &len
);
119 ip
= mtod(m
, struct ip
*);
123 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
124 ip
->ip_v
= IPVERSION
;
126 ip
->ip_id
= htons(ip_id
++);
127 ip
->ip_hl
= hlen
>> 2;
128 ipstat
.ips_localout
++;
130 hlen
= ip
->ip_hl
<< 2;
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
)) {
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
++;
173 if (ro
->ro_rt
== 0) {
174 ipstat
.ips_noroute
++;
175 error
= EHOSTUNREACH
;
176 DROUTE(log(LOG_DEBUG
,"ip_output(): packet dropped\n");)
179 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
180 ifp
= ro
->ro_rt
->rt_ifp
;
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
201 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
202 if (imo
->imo_multicast_ifp
!= NULL
)
203 ifp
= imo
->imo_multicast_ifp
;
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
++;
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
;
228 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
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
);
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,
252 extern struct socket
*ip_mrouter
;
253 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
254 if (ip_mforward(m
, ifp
) != 0) {
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
) {
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
;
286 * Look for broadcast address and
287 * and verify user is allowed to send
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
)) {
294 if (in_broadcast(dst
->sin_addr
)) {
296 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
297 error
= EADDRNOTAVAIL
;
300 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
304 /* don't allow broadcast messages to be fragmented */
305 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
309 m
->m_flags
|= M_BCAST
;
311 m
->m_flags
&= ~M_BCAST
;
313 #ifdef ENABLE_MULTICAST
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
);
326 ip
->ip_sum
= in_cksum(m
, hlen
);
327 error
= (*ifp
->if_output
)(ifp
, m
,
328 (struct sockaddr
*)dst
, ro
->ro_rt
);
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
) {
337 ipstat
.ips_cantfrag
++;
340 len
= (ifp
->if_mtu
- hlen
) &~ 7;
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.
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
);
360 ipstat
.ips_odropped
++;
363 m
->m_data
+= max_linkhdr
;
364 mhip
= mtod(m
, struct ip
*);
366 if (hlen
> sizeof (struct ip
)) {
367 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
368 mhip
->ip_hl
= mhlen
>> 2;
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
;
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) {
382 error
= ENOBUFS
; /* ??? */
383 ipstat
.ips_odropped
++;
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
);
390 mhip
->ip_sum
= in_cksum(m
, mhlen
);
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).
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
));
405 ip
->ip_sum
= in_cksum(m
, hlen
);
407 for (m
= m0
; m
; m
= m0
) {
411 error
= (*ifp
->if_output
)(ifp
, m
,
412 (struct sockaddr
*)dst
, ro
->ro_rt
);
418 ipstat
.ips_fragmented
++;
421 if (ro
== &iproute
&& (flags
& IP_ROUTETOIF
) == 0 && ro
->ro_rt
)
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.
435 ip_insertoptions(m
, opt
, phlen
)
436 register struct mbuf
*m
;
440 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
442 register struct ip
*ip
= mtod(m
, struct ip
*);
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
);
454 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
455 m
->m_len
-= sizeof(struct ip
);
456 m
->m_data
+= sizeof(struct ip
);
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
));
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
;
476 * Copy options from ip to jp,
477 * omitting those not copied during fragmentation.
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
) {
491 if (opt
== IPOPT_EOL
)
493 if (opt
== IPOPT_NOP
) {
494 /* Preserve for IP mcast tunnel's LSRR alignment. */
499 optlen
= cp
[IPOPT_OLEN
];
500 /* bogus lengths should have been caught by ip_dooptions */
503 if (IPOPT_COPIED(opt
)) {
504 bcopy((caddr_t
)cp
, (caddr_t
)dp
, (unsigned)optlen
);
508 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
514 * IP socket option processing.
517 ip_ctloutput(op
, so
, level
, optname
, mp
)
523 register struct inpcb
*inp
= sotoinpcb(so
);
524 register struct mbuf
*m
= *mp
;
528 if (level
!= IPPROTO_IP
) {
530 if (op
== PRCO_SETOPT
&& *mp
)
539 return (ip_pcbopts(optname
, &inp
->inp_options
, m
));
541 return (ip_pcbopts(&inp
->inp_options
, m
));
549 if (m
->m_len
!= sizeof(int))
552 optval
= *mtod(m
, int *);
556 inp
->inp_ip
.ip_tos
= optval
;
560 inp
->inp_ip
.ip_ttl
= optval
;
562 #define OPTSET(bit) \
564 inp->inp_flags |= bit; \
566 inp->inp_flags &= ~bit;
569 OPTSET(INP_RECVOPTS
);
573 OPTSET(INP_RECVRETOPTS
);
577 OPTSET(INP_RECVDSTADDR
);
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
);
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
);
618 *mp
= m
= m_get(M_WAIT
, MT_SOOPTS
);
619 m
->m_len
= sizeof(int);
623 optval
= inp
->inp_ip
.ip_tos
;
627 optval
= inp
->inp_ip
.ip_ttl
;
630 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
633 optval
= OPTBIT(INP_RECVOPTS
);
637 optval
= OPTBIT(INP_RECVRETOPTS
);
641 optval
= OPTBIT(INP_RECVDSTADDR
);
644 *mtod(m
, int *) = optval
;
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
);
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.
671 ip_pcbopts(optname
, pcbopt
, m
)
674 ip_pcbopts(pcbopt
, m
)
676 struct mbuf
**pcbopt
;
677 register struct mbuf
*m
;
679 register int cnt
, optlen
;
683 /* turn off any old options */
685 (void)m_free(*pcbopt
);
687 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
689 * Only turning off any previous options.
697 if (m
->m_len
% sizeof(long))
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
])
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
)
717 if (opt
== IPOPT_NOP
)
720 optlen
= cp
[IPOPT_OLEN
];
721 if (optlen
<= IPOPT_OLEN
|| optlen
> cnt
)
732 * user process specifies route as:
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
))
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
));
761 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
771 #ifdef ENABLE_MULTICAST
773 * Set the IP multicast options in response to user setsockopt().
776 ip_setmoptions(optname
, imop
, m
)
778 struct ip_moptions
**imop
;
781 register int error
= 0;
785 register struct ip_mreq
*mreq
;
786 register struct ifnet
*ifp
;
787 register struct ip_moptions
*imo
= *imop
;
789 register struct sockaddr_in
*dst
;
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
,
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;
810 case IP_MULTICAST_IF
:
812 * Select the interface for outgoing multicast packets.
814 if (m
== NULL
|| m
->m_len
!= sizeof(struct in_addr
)) {
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
;
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
;
838 imo
->imo_multicast_ifp
= ifp
;
841 case IP_MULTICAST_TTL
:
843 * Set the IP time-to-live for outgoing multicast packets.
845 if (m
== NULL
|| m
->m_len
!= 1) {
849 imo
->imo_multicast_ttl
= *(mtod(m
, u_char
*));
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) {
862 imo
->imo_multicast_loop
= loop
;
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
)) {
874 mreq
= mtod(m
, struct ip_mreq
*);
875 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
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
) {
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
;
890 if (ro
.ro_rt
== NULL
) {
891 error
= EADDRNOTAVAIL
;
894 ifp
= ro
.ro_rt
->rt_ifp
;
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
;
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
)
918 if (i
< imo
->imo_num_memberships
) {
922 if (i
== IP_MAX_MEMBERSHIPS
) {
923 error
= ETOOMANYREFS
;
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
) {
935 ++imo
->imo_num_memberships
;
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
)) {
947 mreq
= mtod(m
, struct ip_mreq
*);
948 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
953 * If an interface address was specified, get a pointer
954 * to its ifnet structure.
956 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
959 INADDR_TO_IFP(mreq
->imr_interface
, ifp
);
961 error
= EADDRNOTAVAIL
;
966 * Find the membership in the membership array.
968 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
970 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
971 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
972 mreq
->imr_multiaddr
.s_addr
)
975 if (i
== imo
->imo_num_memberships
) {
976 error
= EADDRNOTAVAIL
;
980 * Give up the multicast address record to which the
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
;
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
);
1012 * Return the IP multicast options in response to user getsockopt().
1015 ip_getmoptions(optname
, imo
, mp
)
1017 register struct ip_moptions
*imo
;
1018 register struct mbuf
**mp
;
1022 struct in_addr
*addr
;
1023 struct in_ifaddr
*ia
;
1025 *mp
= m_get(M_WAIT
, MT_SOOPTS
);
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
;
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
;
1041 case IP_MULTICAST_TTL
:
1042 ttl
= mtod(*mp
, u_char
*);
1044 *ttl
= (imo
== NULL
) ? IP_DEFAULT_MULTICAST_TTL
1045 : imo
->imo_multicast_ttl
;
1048 case IP_MULTICAST_LOOP
:
1049 loop
= mtod(*mp
, u_char
*);
1051 *loop
= (imo
== NULL
) ? IP_DEFAULT_MULTICAST_LOOP
1052 : imo
->imo_multicast_loop
;
1056 return (EOPNOTSUPP
);
1061 * Discard the IP multicast options.
1064 ip_freemoptions(imo
)
1065 register struct ip_moptions
*imo
;
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.
1083 ip_mloopback(ifp
, m
, dst
)
1085 register struct mbuf
*m
;
1086 register struct sockaddr_in
*dst
;
1088 register struct ip
*ip
;
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
);
1101 ip
->ip_sum
= in_cksum(copym
, ip
->ip_hl
<< 2);
1102 (void) looutput(ifp
, copym
, (struct sockaddr
*)dst
, NULL
);