2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
40 #include <sys/errno.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/queue.h>
47 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/in_pcb.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip_var.h>
56 #include <netinet/ip_fw.h>
59 #include <machine/mtpr.h>
65 static struct mbuf
*ip_insertoptions
__P((struct mbuf
*, struct mbuf
*, int *));
67 static void ip_mloopback
68 __P((struct ifnet
*, struct mbuf
*, struct sockaddr_in
*));
72 * IP output. The packet in mbuf chain m contains a skeletal IP
73 * header (with len, off, ttl, proto, tos, src, dst).
74 * The mbuf chain containing the packet will be freed.
75 * The mbuf opt, if present, will not be freed.
78 ip_output(m0
, opt
, ro
, flags
, imo
)
83 struct ip_moptions
*imo
;
85 register struct ip
*ip
, *mhip
;
87 register struct ifnet
*ifp
;
89 register struct mbuf
*m
= m0
;
90 register int hlen
= sizeof (struct ip
);
91 int len
, off
, error
= 0;
93 * It might seem obvious at first glance that one could easily
94 * make a one-behind cache out of this by simply making `iproute'
95 * static and eliminating the bzero() below. However, this turns
96 * out not to work, for two reasons:
98 * 1) This routine needs to be reentrant. It can be called
99 * recursively from encapsulating network interfaces, and it
100 * is always called recursively from ip_mforward().
102 * 2) You turn out not to gain much. There is already a one-
103 * behind cache implemented for the specific case of forwarding,
104 * and sends on a connected socket will use a route associated
105 * with the PCB. The only cases left are sends on unconnected
106 * and raw sockets, and if these cases are really significant,
107 * something is seriously wrong.
109 struct route iproute
;
110 struct sockaddr_in
*dst
;
111 struct in_ifaddr
*ia
;
114 if ((m
->m_flags
& M_PKTHDR
) == 0)
115 panic("ip_output no HDR");
118 m
= ip_insertoptions(m
, opt
, &len
);
121 ip
= mtod(m
, struct ip
*);
125 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
126 ip
->ip_v
= IPVERSION
;
128 ip
->ip_id
= htons(ip_id
++);
129 ip
->ip_hl
= hlen
>> 2;
130 ipstat
.ips_localout
++;
132 hlen
= ip
->ip_hl
<< 2;
139 bzero((caddr_t
)ro
, sizeof (*ro
));
141 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
143 * If there is a cached route,
144 * check that it is to the same destination
145 * and is still up. If not, free it and try again.
147 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
148 dst
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
150 ro
->ro_rt
= (struct rtentry
*)0;
152 if (ro
->ro_rt
== 0) {
153 dst
->sin_family
= AF_INET
;
154 dst
->sin_len
= sizeof(*dst
);
155 dst
->sin_addr
= ip
->ip_dst
;
158 * If routing to interface only,
159 * short circuit routing lookup.
161 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
162 #define sintosa(sin) ((struct sockaddr *)(sin))
163 if (flags
& IP_ROUTETOIF
) {
164 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0 &&
165 (ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
166 ipstat
.ips_noroute
++;
176 * If this is the case, we probably don't want to allocate
177 * a protocol-cloned route since we didn't get one from the
178 * ULP. This lets TCP do its thing, while not burdening
179 * forwarding or ICMP with the overhead of cloning a route.
180 * Of course, we still want to do any cloning requested by
181 * the link layer, as this is probably required in all cases
182 * for correct operation (as it is for ARP).
186 rtalloc_ign(ro
, RTF_PRCLONING
);
187 if (ro
->ro_rt
== 0) {
188 ipstat
.ips_noroute
++;
189 OS_DbgPrint(OSK_MID_TRACE
,("EHOSTUNREACH\n"));
190 error
= EHOSTUNREACH
;
193 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
194 ifp
= ro
->ro_rt
->rt_ifp
;
196 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
197 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
201 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
202 struct in_multi
*inm
;
204 m
->m_flags
|= M_MCAST
;
206 * IP destination address is multicast. Make sure "dst"
207 * still points to the address in "ro". (It may have been
208 * changed to point to a gateway address, above.)
210 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
212 * See if the caller provided any multicast options
215 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
216 if (imo
->imo_multicast_ifp
!= NULL
)
217 ifp
= imo
->imo_multicast_ifp
;
218 if (imo
->imo_multicast_vif
!= -1)
220 ip_mcast_src(imo
->imo_multicast_vif
);
222 ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
224 * Confirm that the outgoing interface supports multicast.
226 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
227 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
228 ipstat
.ips_noroute
++;
234 * If source address not specified yet, use address
235 * of outgoing interface.
237 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
238 register struct in_ifaddr
*ia
;
240 panic("We don't handle this yet\n");
241 for (ia
= in_ifaddr
; ia
; ia
= ia
->ia_next
)
242 if (ia
->ia_ifp
== ifp
) {
243 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
248 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
250 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
252 * If we belong to the destination multicast group
253 * on the outgoing interface, and the caller did not
254 * forbid loopback, loop back a copy.
256 ip_mloopback(ifp
, m
, dst
);
260 * If we are acting as a multicast router, perform
261 * multicast forwarding as if the packet had just
262 * arrived on the interface to which we are about
263 * to send. The multicast forwarding function
264 * recursively calls this function, using the
265 * IP_FORWARDING flag to prevent infinite recursion.
267 * Multicasts that are looped back by ip_mloopback(),
268 * above, will be forwarded by the ip_input() routine,
271 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
273 * Check if rsvp daemon is running. If not, don't
274 * set ip_moptions. This ensures that the packet
275 * is multicast and not just sent down one link
276 * as prescribed by rsvpd.
280 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
288 * Multicasts with a time-to-live of zero may be looped-
289 * back, above, but must not be transmitted on a network.
290 * Also, multicasts addressed to the loopback interface
291 * are not sent -- the above call to ip_mloopback() will
292 * loop back a copy if this host actually belongs to the
293 * destination group on the loopback interface.
295 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
306 * If source address not specified yet, use address
307 * of outgoing interface.
309 if (ip
->ip_src
.s_addr
== INADDR_ANY
)
310 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
314 * Verify that we have any chance at all of being able to queue
315 * the packet or packet fragments
317 if ((ifp
->if_snd
.ifq_len
+ ip
->ip_len
/ ifp
->if_mtu
+ 1) >=
318 ifp
->if_snd
.ifq_maxlen
) {
324 * Look for broadcast address and
325 * and verify user is allowed to send
328 if (in_broadcast(dst
->sin_addr
, ifp
)) {
329 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
330 error
= EADDRNOTAVAIL
;
333 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
337 /* don't allow broadcast messages to be fragmented */
338 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
342 m
->m_flags
|= M_BCAST
;
344 m
->m_flags
&= ~M_BCAST
;
350 * Check with the firewall...
352 if (!(*ip_fw_chk_ptr
)(m
,ip
,ifp
,1)) {
359 * If small enough for interface, can just send directly.
361 if ((u_short
)ip
->ip_len
<= 1400 /* XXX Get MTU from Interface */) {
362 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
363 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
365 ip
->ip_sum
= in_cksum(m
, hlen
);
367 if( OtcpEvent
.PacketSend
) {
369 new_m
= m_get( M_DONTWAIT
, 0 );
370 if ( NULL
== new_m
) {
374 MCLGET( new_m
, M_DONTWAIT
);
375 if (0 == (new_m
->m_flags
& M_EXT
)) {
380 m_copydata( m
, 0, htons(ip
->ip_len
), new_m
->m_data
);
381 new_m
->m_len
= htons(ip
->ip_len
);
382 error
= OtcpEvent
.PacketSend( OtcpEvent
.ClientData
,
383 new_m
->m_data
, new_m
->m_len
);
388 error
= (*ifp
->if_output
)(ifp
, m
,
389 (struct sockaddr
*)dst
, ro
->ro_rt
);
393 * Too large for interface; fragment if possible.
394 * Must be able to put at least 8 bytes per fragment.
396 if (ip
->ip_off
& IP_DF
) {
401 * This case can happen if the user changed the MTU
402 * of an interface after enabling IP on it. Because
403 * most netifs don't keep track of routes pointing to
404 * them, there is no way for one to update all its
405 * routes when the MTU is changed.
407 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
408 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
409 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
410 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
414 ipstat
.ips_cantfrag
++;
418 len
= (ifp
->if_mtu
- hlen
) &~ 7;
424 OS_DbgPrint(OSK_MID_TRACE
,("Using default mtu of 1500\n"));
425 len
= (1500 - hlen
) & ~7;
429 int mhlen
, firstlen
= len
;
430 struct mbuf
**mnext
= &m
->m_nextpkt
;
433 * Loop through length of segment after first fragment,
434 * make new header and copy data of each part and link onto chain.
437 mhlen
= sizeof (struct ip
);
438 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
439 OS_DbgPrint(OSK_MID_TRACE
,("off = %d, len = %d\n", off
, len
));
440 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
443 ipstat
.ips_odropped
++;
446 m
->m_data
+= max_linkhdr
;
447 mhip
= mtod(m
, struct ip
*);
449 if (hlen
> sizeof (struct ip
)) {
450 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
451 mhip
->ip_hl
= mhlen
>> 2;
454 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
455 if (ip
->ip_off
& IP_MF
)
456 mhip
->ip_off
|= IP_MF
;
457 if (off
+ len
>= (u_short
)ip
->ip_len
)
458 len
= (u_short
)ip
->ip_len
- off
;
460 mhip
->ip_off
|= IP_MF
;
461 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
462 m
->m_next
= m_copy(m0
, off
, len
);
463 if (m
->m_next
== 0) {
465 error
= ENOBUFS
; /* ??? */
466 ipstat
.ips_odropped
++;
469 m
->m_pkthdr
.len
= mhlen
+ len
;
470 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
471 mhip
->ip_off
= htons((u_short
)mhip
->ip_off
);
473 mhip
->ip_sum
= in_cksum(m
, mhlen
);
475 mnext
= &m
->m_nextpkt
;
476 ipstat
.ips_ofragments
++;
479 * Update first fragment by trimming what's been copied out
480 * and updating header, then send each fragment (in order).
483 OS_DbgPrint(OSK_MID_TRACE
,("hlen %d firstlen %d ip->ip_len %x\n",
484 hlen
, firstlen
, ip
->ip_len
));
485 OS_DbgPrint(OSK_MID_TRACE
,("hlen + firstlen - ip->ip_len %d\n",
486 hlen
+ firstlen
- (u_short
)ip
->ip_len
));
487 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
488 m
->m_pkthdr
.len
= hlen
+ firstlen
;
489 ip
->ip_len
= htons((u_short
)(m
->m_pkthdr
.len
));
490 ip
->ip_off
= htons((u_short
)(ip
->ip_off
| IP_MF
));
492 ip
->ip_sum
= in_cksum(m
, hlen
- sizeof( struct ip
) );
494 OS_DbgPrint(OSK_MID_TRACE
,("ip->ip_len = %x\n", ip
->ip_len
));
497 for (m
= m0
; m
; m
= m0
) {
502 error
= (*ifp
->if_output
)(ifp
, m
,
503 (struct sockaddr
*)dst
, ro
->ro_rt
);
507 if( error
== 0 && OtcpEvent
.PacketSend
) {
509 MGET( new_m
, M_DONTWAIT
, 0 );
510 if ( NULL
== new_m
) {
514 MCLGET( new_m
, M_DONTWAIT
);
515 if (0 == (new_m
->m_flags
& M_EXT
)) {
520 m_copydata( m
, 0, htons(ip
->ip_len
), new_m
->m_data
);
521 new_m
->m_len
= htons(ip
->ip_len
);
522 error
= OtcpEvent
.PacketSend( OtcpEvent
.ClientData
,
523 new_m
->m_data
, new_m
->m_len
);
528 OS_DbgPrint(OSK_MID_TRACE
,("Error from upper layer: %d\n", error
));
533 ipstat
.ips_fragmented
++;
536 if (ro
== &iproute
&& (flags
& IP_ROUTETOIF
) == 0 && ro
->ro_rt
) {
547 * Insert IP options into preformed packet.
548 * Adjust IP destination as required for IP source routing,
549 * as indicated by a non-zero in_addr at the start of the options.
551 * XXX This routine assumes that the packet has no options in place.
554 ip_insertoptions(m
, opt
, phlen
)
555 register struct mbuf
*m
;
559 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
561 register struct ip
*ip
= mtod(m
, struct ip
*);
564 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
565 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
566 return (m
); /* XXX should fail */
567 if (p
->ipopt_dst
.s_addr
)
568 ip
->ip_dst
= p
->ipopt_dst
;
569 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
570 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
573 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
574 m
->m_len
-= sizeof(struct ip
);
575 m
->m_data
+= sizeof(struct ip
);
578 m
->m_len
= optlen
+ sizeof(struct ip
);
579 m
->m_data
+= max_linkhdr
;
580 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
584 m
->m_pkthdr
.len
+= optlen
;
585 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
587 ip
= mtod(m
, struct ip
*);
588 (void)memcpy(ip
+ 1, p
->ipopt_list
, (unsigned)optlen
);
589 *phlen
= sizeof(struct ip
) + optlen
;
590 ip
->ip_hl
= *phlen
>> 2;
591 ip
->ip_len
+= optlen
;
596 * Copy options from ip to jp,
597 * omitting those not copied during fragmentation.
603 register u_char
*cp
, *dp
;
604 int opt
, optlen
, cnt
;
606 cp
= (u_char
*)(ip
+ 1);
607 dp
= (u_char
*)(jp
+ 1);
608 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
609 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
611 if (opt
== IPOPT_EOL
)
613 if (opt
== IPOPT_NOP
) {
614 /* Preserve for IP mcast tunnel's LSRR alignment. */
619 optlen
= cp
[IPOPT_OLEN
];
620 /* bogus lengths should have been caught by ip_dooptions */
623 if (IPOPT_COPIED(opt
)) {
624 (void)memcpy(dp
, cp
, (unsigned)optlen
);
628 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
634 * IP socket option processing.
637 ip_ctloutput(op
, so
, level
, optname
, mp
)
643 register struct inpcb
*inp
= sotoinpcb(so
);
644 register struct mbuf
*m
= *mp
;
645 register int optval
= 0;
648 if (level
!= IPPROTO_IP
) {
650 if (op
== PRCO_SETOPT
&& *mp
)
659 return (ip_pcbopts(optname
, &inp
->inp_options
, m
));
661 return (ip_pcbopts(&inp
->inp_options
, m
));
669 if (m
== 0 || m
->m_len
!= sizeof(int))
672 optval
= *mtod(m
, int *);
676 inp
->inp_ip
.ip_tos
= optval
;
680 inp
->inp_ip
.ip_ttl
= optval
;
682 #define OPTSET(bit) \
684 inp->inp_flags |= bit; \
686 inp->inp_flags &= ~bit;
689 OPTSET(INP_RECVOPTS
);
693 OPTSET(INP_RECVRETOPTS
);
697 OPTSET(INP_RECVDSTADDR
);
704 case IP_MULTICAST_IF
:
705 case IP_MULTICAST_VIF
:
706 case IP_MULTICAST_TTL
:
707 case IP_MULTICAST_LOOP
:
708 case IP_ADD_MEMBERSHIP
:
709 case IP_DROP_MEMBERSHIP
:
710 error
= ip_setmoptions(optname
, &inp
->inp_moptions
, m
);
725 *mp
= m
= m_get(M_WAIT
, MT_SOOPTS
);
726 if (inp
->inp_options
) {
727 m
->m_len
= inp
->inp_options
->m_len
;
728 (void)memcpy(mtod(m
, void *),
729 mtod(inp
->inp_options
, void *), (unsigned)m
->m_len
);
739 *mp
= m
= m_get(M_WAIT
, MT_SOOPTS
);
740 m
->m_len
= sizeof(int);
744 optval
= inp
->inp_ip
.ip_tos
;
748 optval
= inp
->inp_ip
.ip_ttl
;
751 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
754 optval
= OPTBIT(INP_RECVOPTS
);
758 optval
= OPTBIT(INP_RECVRETOPTS
);
762 optval
= OPTBIT(INP_RECVDSTADDR
);
765 *mtod(m
, int *) = optval
;
768 case IP_MULTICAST_IF
:
769 case IP_MULTICAST_VIF
:
770 case IP_MULTICAST_TTL
:
771 case IP_MULTICAST_LOOP
:
772 case IP_ADD_MEMBERSHIP
:
773 case IP_DROP_MEMBERSHIP
:
774 error
= ip_getmoptions(optname
, inp
->inp_moptions
, mp
);
787 * Set up IP options in pcb for insertion in output packets.
788 * Store in mbuf with pointer in pcbopt, adding pseudo-option
789 * with destination address if source routed.
793 ip_pcbopts(optname
, pcbopt
, m
)
796 ip_pcbopts(pcbopt
, m
)
798 struct mbuf
**pcbopt
;
799 register struct mbuf
*m
;
801 register int cnt
, optlen
;
805 /* turn off any old options */
807 (void)m_free(*pcbopt
);
809 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
811 * Only turning off any previous options.
819 if (m
->m_len
% sizeof(long))
823 * IP first-hop destination address will be stored before
824 * actual options; move other options back
825 * and clear it when none present.
827 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
830 m
->m_len
+= sizeof(struct in_addr
);
831 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
832 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
833 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
835 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
836 opt
= cp
[IPOPT_OPTVAL
];
837 if (opt
== IPOPT_EOL
)
839 if (opt
== IPOPT_NOP
)
842 optlen
= cp
[IPOPT_OLEN
];
843 if (optlen
<= IPOPT_OLEN
|| optlen
> cnt
)
854 * user process specifies route as:
856 * D must be our final destination (but we can't
857 * check that since we may not have connected yet).
858 * A is first hop destination, which doesn't appear in
859 * actual IP option, but is stored before the options.
861 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
863 m
->m_len
-= sizeof(struct in_addr
);
864 cnt
-= sizeof(struct in_addr
);
865 optlen
-= sizeof(struct in_addr
);
866 cp
[IPOPT_OLEN
] = optlen
;
868 * Move first hop before start of options.
870 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
871 sizeof(struct in_addr
));
873 * Then copy rest of options back
874 * to close up the deleted entry.
876 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
877 sizeof(struct in_addr
)),
878 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
879 (unsigned)cnt
+ sizeof(struct in_addr
));
883 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
894 * Set the IP multicast options in response to user setsockopt().
897 ip_setmoptions(optname
, imop
, m
)
899 struct ip_moptions
**imop
;
902 register int error
= 0;
907 register struct ip_mreq
*mreq
;
908 register struct ifnet
*ifp
;
910 register struct ip_moptions
*imo
= *imop
;
913 register struct sockaddr_in
*dst
;
919 * No multicast option buffer attached to the pcb;
920 * allocate one and initialize to default values.
922 imo
= (struct ip_moptions
*)malloc(sizeof(*imo
), M_IPMOPTS
,
928 imo
->imo_multicast_ifp
= NULL
;
929 imo
->imo_multicast_vif
= -1;
930 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
931 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
932 imo
->imo_num_memberships
= 0;
937 /* store an index number for the vif you wanna use in the send */
938 case IP_MULTICAST_VIF
:
939 if (!legal_vif_num
) {
943 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
947 i
= *(mtod(m
, int *));
948 if (!legal_vif_num(i
) && (i
!= -1)) {
952 imo
->imo_multicast_vif
= i
;
955 case IP_MULTICAST_IF
:
957 * Select the interface for outgoing multicast packets.
959 if (m
== NULL
|| m
->m_len
!= sizeof(struct in_addr
)) {
963 addr
= *(mtod(m
, struct in_addr
*));
965 * INADDR_ANY is used to remove a previous selection.
966 * When no interface is selected, a default one is
967 * chosen every time a multicast packet is sent.
969 if (addr
.s_addr
== INADDR_ANY
) {
970 imo
->imo_multicast_ifp
= NULL
;
974 * The selected interface is identified by its local
975 * IP address. Find the interface and confirm that
976 * it supports multicasting.
979 INADDR_TO_IFP(addr
, ifp
);
980 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
981 error
= EADDRNOTAVAIL
;
984 imo
->imo_multicast_ifp
= ifp
;
988 case IP_MULTICAST_TTL
:
990 * Set the IP time-to-live for outgoing multicast packets.
992 if (m
== NULL
|| m
->m_len
!= 1) {
996 imo
->imo_multicast_ttl
= *(mtod(m
, u_char
*));
999 case IP_MULTICAST_LOOP
:
1001 * Set the loopback flag for outgoing multicast packets.
1002 * Must be zero or one.
1004 if (m
== NULL
|| m
->m_len
!= 1 ||
1005 (loop
= *(mtod(m
, u_char
*))) > 1) {
1009 imo
->imo_multicast_loop
= loop
;
1012 case IP_ADD_MEMBERSHIP
:
1014 * Add a multicast group membership.
1015 * Group must be a valid IP multicast address.
1017 if (m
== NULL
|| m
->m_len
!= sizeof(struct ip_mreq
)) {
1021 mreq
= mtod(m
, struct ip_mreq
*);
1022 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
1028 * If no interface address was provided, use the interface of
1029 * the route to the given multicast address.
1031 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
1032 bzero((caddr_t
)&ro
, sizeof(ro
));
1033 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
1034 dst
->sin_len
= sizeof(*dst
);
1035 dst
->sin_family
= AF_INET
;
1036 dst
->sin_addr
= mreq
->imr_multiaddr
;
1038 if (ro
.ro_rt
== NULL
) {
1039 error
= EADDRNOTAVAIL
;
1043 ifp
= ro
.ro_rt
->rt_ifp
;
1047 INADDR_TO_IFP(mreq
->imr_interface
, ifp
);
1051 * See if we found an interface, and confirm that it
1052 * supports multicast.
1054 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1055 error
= EADDRNOTAVAIL
;
1060 * See if the membership already exists or if all the
1061 * membership slots are full.
1063 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1064 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1065 imo
->imo_membership
[i
]->inm_addr
.s_addr
1066 == mreq
->imr_multiaddr
.s_addr
)
1069 if (i
< imo
->imo_num_memberships
) {
1074 if (i
== IP_MAX_MEMBERSHIPS
) {
1075 error
= ETOOMANYREFS
;
1080 * Everything looks good; add a new record to the multicast
1081 * address list for the given interface.
1083 if ((imo
->imo_membership
[i
] =
1084 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
1089 ++imo
->imo_num_memberships
;
1093 case IP_DROP_MEMBERSHIP
:
1095 * Drop a multicast group membership.
1096 * Group must be a valid IP multicast address.
1098 if (m
== NULL
|| m
->m_len
!= sizeof(struct ip_mreq
)) {
1102 mreq
= mtod(m
, struct ip_mreq
*);
1103 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
1110 * If an interface address was specified, get a pointer
1111 * to its ifnet structure.
1113 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
1116 INADDR_TO_IFP(mreq
->imr_interface
, ifp
);
1118 error
= EADDRNOTAVAIL
;
1124 * Find the membership in the membership array.
1126 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1128 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
1129 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
1130 mreq
->imr_multiaddr
.s_addr
)
1133 if (i
== imo
->imo_num_memberships
) {
1134 error
= EADDRNOTAVAIL
;
1139 * Give up the multicast address record to which the
1140 * membership points.
1142 in_delmulti(imo
->imo_membership
[i
]);
1144 * Remove the gap in the membership array.
1146 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
1147 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
1148 --imo
->imo_num_memberships
;
1158 * If all options have default values, no need to keep the mbuf.
1160 if (imo
->imo_multicast_ifp
== NULL
&&
1161 imo
->imo_multicast_vif
== -1 &&
1162 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
1163 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
1164 imo
->imo_num_memberships
== 0) {
1165 free(*imop
, M_IPMOPTS
);
1173 * Return the IP multicast options in response to user getsockopt().
1176 ip_getmoptions(optname
, imo
, mp
)
1178 register struct ip_moptions
*imo
;
1179 register struct mbuf
**mp
;
1184 struct in_addr
*addr
;
1185 struct in_ifaddr
*ia
;
1188 *mp
= m_get(M_WAIT
, MT_SOOPTS
);
1192 case IP_MULTICAST_VIF
:
1194 *(mtod(*mp
, int *)) = imo
->imo_multicast_vif
;
1196 *(mtod(*mp
, int *)) = -1;
1197 (*mp
)->m_len
= sizeof(int);
1200 case IP_MULTICAST_IF
:
1201 addr
= mtod(*mp
, struct in_addr
*);
1202 (*mp
)->m_len
= sizeof(struct in_addr
);
1203 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
1204 addr
->s_addr
= INADDR_ANY
;
1206 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
1207 addr
->s_addr
= (ia
== NULL
) ? INADDR_ANY
1208 : IA_SIN(ia
)->sin_addr
.s_addr
;
1212 case IP_MULTICAST_TTL
:
1213 ttl
= mtod(*mp
, u_char
*);
1215 *ttl
= (imo
== NULL
) ? IP_DEFAULT_MULTICAST_TTL
1216 : imo
->imo_multicast_ttl
;
1219 case IP_MULTICAST_LOOP
:
1220 loop
= mtod(*mp
, u_char
*);
1222 *loop
= (imo
== NULL
) ? IP_DEFAULT_MULTICAST_LOOP
1223 : imo
->imo_multicast_loop
;
1227 return (EOPNOTSUPP
);
1232 * Discard the IP multicast options.
1235 ip_freemoptions(imo
)
1236 register struct ip_moptions
*imo
;
1241 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
1242 in_delmulti(imo
->imo_membership
[i
]);
1243 free(imo
, M_IPMOPTS
);
1249 * Routine called from ip_output() to loop back a copy of an IP multicast
1250 * packet to the input queue of a specified interface. Note that this
1251 * calls the output routine of the loopback "driver", but with an interface
1252 * pointer that might NOT be a loopback interface -- evil, but easier than
1253 * replicating that code here.
1256 ip_mloopback(ifp
, m
, dst
)
1258 register struct mbuf
*m
;
1259 register struct sockaddr_in
*dst
;
1261 register struct ip
*ip
;
1264 copym
= m_copy(m
, 0, M_COPYALL
);
1265 if (copym
!= NULL
) {
1267 * We don't bother to fragment if the IP length is greater
1268 * than the interface's MTU. Can this possibly matter?
1270 ip
= mtod(copym
, struct ip
*);
1271 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
1272 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
1274 ip
->ip_sum
= in_cksum(copym
, ip
->ip_hl
<< 2);
1275 (void) looutput(ifp
, copym
, (struct sockaddr
*)dst
, NULL
);