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 MGET( new_m
, M_DONTWAIT
, 0 );
370 MCLGET( new_m
, M_DONTWAIT
);
371 m_copydata( m
, 0, htons(ip
->ip_len
), new_m
->m_data
);
372 new_m
->m_len
= htons(ip
->ip_len
);
373 error
= OtcpEvent
.PacketSend( OtcpEvent
.ClientData
,
374 new_m
->m_data
, new_m
->m_len
);
379 error
= (*ifp
->if_output
)(ifp
, m
,
380 (struct sockaddr
*)dst
, ro
->ro_rt
);
384 * Too large for interface; fragment if possible.
385 * Must be able to put at least 8 bytes per fragment.
387 if (ip
->ip_off
& IP_DF
) {
392 * This case can happen if the user changed the MTU
393 * of an interface after enabling IP on it. Because
394 * most netifs don't keep track of routes pointing to
395 * them, there is no way for one to update all its
396 * routes when the MTU is changed.
398 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
399 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
400 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
401 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
405 ipstat
.ips_cantfrag
++;
409 len
= (ifp
->if_mtu
- hlen
) &~ 7;
415 OS_DbgPrint(OSK_MID_TRACE
,("Using default mtu of 1500\n"));
416 len
= (1500 - hlen
) & ~7;
420 int mhlen
, firstlen
= len
;
421 struct mbuf
**mnext
= &m
->m_nextpkt
;
424 * Loop through length of segment after first fragment,
425 * make new header and copy data of each part and link onto chain.
428 mhlen
= sizeof (struct ip
);
429 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
430 OS_DbgPrint(OSK_MID_TRACE
,("off = %d, len = %d\n", off
, len
));
431 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
434 ipstat
.ips_odropped
++;
437 m
->m_data
+= max_linkhdr
;
438 mhip
= mtod(m
, struct ip
*);
440 if (hlen
> sizeof (struct ip
)) {
441 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
442 mhip
->ip_hl
= mhlen
>> 2;
445 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
446 if (ip
->ip_off
& IP_MF
)
447 mhip
->ip_off
|= IP_MF
;
448 if (off
+ len
>= (u_short
)ip
->ip_len
)
449 len
= (u_short
)ip
->ip_len
- off
;
451 mhip
->ip_off
|= IP_MF
;
452 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
453 m
->m_next
= m_copy(m0
, off
, len
);
454 if (m
->m_next
== 0) {
456 error
= ENOBUFS
; /* ??? */
457 ipstat
.ips_odropped
++;
460 m
->m_pkthdr
.len
= mhlen
+ len
;
461 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
462 mhip
->ip_off
= htons((u_short
)mhip
->ip_off
);
464 mhip
->ip_sum
= in_cksum(m
, mhlen
);
466 mnext
= &m
->m_nextpkt
;
467 ipstat
.ips_ofragments
++;
470 * Update first fragment by trimming what's been copied out
471 * and updating header, then send each fragment (in order).
474 OS_DbgPrint(OSK_MID_TRACE
,("hlen %d firstlen %d ip->ip_len %x\n",
475 hlen
, firstlen
, ip
->ip_len
));
476 OS_DbgPrint(OSK_MID_TRACE
,("hlen + firstlen - ip->ip_len %d\n",
477 hlen
+ firstlen
- (u_short
)ip
->ip_len
));
478 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
479 m
->m_pkthdr
.len
= hlen
+ firstlen
;
480 ip
->ip_len
= htons((u_short
)(m
->m_pkthdr
.len
));
481 ip
->ip_off
= htons((u_short
)(ip
->ip_off
| IP_MF
));
483 ip
->ip_sum
= in_cksum(m
, hlen
- sizeof( struct ip
) );
485 OS_DbgPrint(OSK_MID_TRACE
,("ip->ip_len = %x\n", ip
->ip_len
));
488 for (m
= m0
; m
; m
= m0
) {
493 error
= (*ifp
->if_output
)(ifp
, m
,
494 (struct sockaddr
*)dst
, ro
->ro_rt
);
498 if( error
== 0 && OtcpEvent
.PacketSend
) {
500 MGET( new_m
, M_DONTWAIT
, 0 );
501 MCLGET( new_m
, M_DONTWAIT
);
502 m_copydata( m
, 0, htons(ip
->ip_len
), new_m
->m_data
);
503 new_m
->m_len
= htons(ip
->ip_len
);
504 error
= OtcpEvent
.PacketSend( OtcpEvent
.ClientData
,
505 new_m
->m_data
, new_m
->m_len
);
510 OS_DbgPrint(OSK_MID_TRACE
,("Error from upper layer: %d\n", error
));
515 ipstat
.ips_fragmented
++;
518 if (ro
== &iproute
&& (flags
& IP_ROUTETOIF
) == 0 && ro
->ro_rt
) {
529 * Insert IP options into preformed packet.
530 * Adjust IP destination as required for IP source routing,
531 * as indicated by a non-zero in_addr at the start of the options.
533 * XXX This routine assumes that the packet has no options in place.
536 ip_insertoptions(m
, opt
, phlen
)
537 register struct mbuf
*m
;
541 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
543 register struct ip
*ip
= mtod(m
, struct ip
*);
546 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
547 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
548 return (m
); /* XXX should fail */
549 if (p
->ipopt_dst
.s_addr
)
550 ip
->ip_dst
= p
->ipopt_dst
;
551 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
552 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
555 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
556 m
->m_len
-= sizeof(struct ip
);
557 m
->m_data
+= sizeof(struct ip
);
560 m
->m_len
= optlen
+ sizeof(struct ip
);
561 m
->m_data
+= max_linkhdr
;
562 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
566 m
->m_pkthdr
.len
+= optlen
;
567 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
569 ip
= mtod(m
, struct ip
*);
570 (void)memcpy(ip
+ 1, p
->ipopt_list
, (unsigned)optlen
);
571 *phlen
= sizeof(struct ip
) + optlen
;
572 ip
->ip_hl
= *phlen
>> 2;
573 ip
->ip_len
+= optlen
;
578 * Copy options from ip to jp,
579 * omitting those not copied during fragmentation.
585 register u_char
*cp
, *dp
;
586 int opt
, optlen
, cnt
;
588 cp
= (u_char
*)(ip
+ 1);
589 dp
= (u_char
*)(jp
+ 1);
590 cnt
= (ip
->ip_hl
<< 2) - sizeof (struct ip
);
591 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
593 if (opt
== IPOPT_EOL
)
595 if (opt
== IPOPT_NOP
) {
596 /* Preserve for IP mcast tunnel's LSRR alignment. */
601 optlen
= cp
[IPOPT_OLEN
];
602 /* bogus lengths should have been caught by ip_dooptions */
605 if (IPOPT_COPIED(opt
)) {
606 (void)memcpy(dp
, cp
, (unsigned)optlen
);
610 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
616 * IP socket option processing.
619 ip_ctloutput(op
, so
, level
, optname
, mp
)
625 register struct inpcb
*inp
= sotoinpcb(so
);
626 register struct mbuf
*m
= *mp
;
627 register int optval
= 0;
630 if (level
!= IPPROTO_IP
) {
632 if (op
== PRCO_SETOPT
&& *mp
)
641 return (ip_pcbopts(optname
, &inp
->inp_options
, m
));
643 return (ip_pcbopts(&inp
->inp_options
, m
));
651 if (m
== 0 || m
->m_len
!= sizeof(int))
654 optval
= *mtod(m
, int *);
658 inp
->inp_ip
.ip_tos
= optval
;
662 inp
->inp_ip
.ip_ttl
= optval
;
664 #define OPTSET(bit) \
666 inp->inp_flags |= bit; \
668 inp->inp_flags &= ~bit;
671 OPTSET(INP_RECVOPTS
);
675 OPTSET(INP_RECVRETOPTS
);
679 OPTSET(INP_RECVDSTADDR
);
686 case IP_MULTICAST_IF
:
687 case IP_MULTICAST_VIF
:
688 case IP_MULTICAST_TTL
:
689 case IP_MULTICAST_LOOP
:
690 case IP_ADD_MEMBERSHIP
:
691 case IP_DROP_MEMBERSHIP
:
692 error
= ip_setmoptions(optname
, &inp
->inp_moptions
, m
);
707 *mp
= m
= m_get(M_WAIT
, MT_SOOPTS
);
708 if (inp
->inp_options
) {
709 m
->m_len
= inp
->inp_options
->m_len
;
710 (void)memcpy(mtod(m
, void *),
711 mtod(inp
->inp_options
, void *), (unsigned)m
->m_len
);
721 *mp
= m
= m_get(M_WAIT
, MT_SOOPTS
);
722 m
->m_len
= sizeof(int);
726 optval
= inp
->inp_ip
.ip_tos
;
730 optval
= inp
->inp_ip
.ip_ttl
;
733 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
736 optval
= OPTBIT(INP_RECVOPTS
);
740 optval
= OPTBIT(INP_RECVRETOPTS
);
744 optval
= OPTBIT(INP_RECVDSTADDR
);
747 *mtod(m
, int *) = optval
;
750 case IP_MULTICAST_IF
:
751 case IP_MULTICAST_VIF
:
752 case IP_MULTICAST_TTL
:
753 case IP_MULTICAST_LOOP
:
754 case IP_ADD_MEMBERSHIP
:
755 case IP_DROP_MEMBERSHIP
:
756 error
= ip_getmoptions(optname
, inp
->inp_moptions
, mp
);
769 * Set up IP options in pcb for insertion in output packets.
770 * Store in mbuf with pointer in pcbopt, adding pseudo-option
771 * with destination address if source routed.
775 ip_pcbopts(optname
, pcbopt
, m
)
778 ip_pcbopts(pcbopt
, m
)
780 struct mbuf
**pcbopt
;
781 register struct mbuf
*m
;
783 register int cnt
, optlen
;
787 /* turn off any old options */
789 (void)m_free(*pcbopt
);
791 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
793 * Only turning off any previous options.
801 if (m
->m_len
% sizeof(long))
805 * IP first-hop destination address will be stored before
806 * actual options; move other options back
807 * and clear it when none present.
809 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
812 m
->m_len
+= sizeof(struct in_addr
);
813 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
814 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
815 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
817 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
818 opt
= cp
[IPOPT_OPTVAL
];
819 if (opt
== IPOPT_EOL
)
821 if (opt
== IPOPT_NOP
)
824 optlen
= cp
[IPOPT_OLEN
];
825 if (optlen
<= IPOPT_OLEN
|| optlen
> cnt
)
836 * user process specifies route as:
838 * D must be our final destination (but we can't
839 * check that since we may not have connected yet).
840 * A is first hop destination, which doesn't appear in
841 * actual IP option, but is stored before the options.
843 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
845 m
->m_len
-= sizeof(struct in_addr
);
846 cnt
-= sizeof(struct in_addr
);
847 optlen
-= sizeof(struct in_addr
);
848 cp
[IPOPT_OLEN
] = optlen
;
850 * Move first hop before start of options.
852 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
853 sizeof(struct in_addr
));
855 * Then copy rest of options back
856 * to close up the deleted entry.
858 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
859 sizeof(struct in_addr
)),
860 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
861 (unsigned)cnt
+ sizeof(struct in_addr
));
865 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
876 * Set the IP multicast options in response to user setsockopt().
879 ip_setmoptions(optname
, imop
, m
)
881 struct ip_moptions
**imop
;
884 register int error
= 0;
889 register struct ip_mreq
*mreq
;
890 register struct ifnet
*ifp
;
892 register struct ip_moptions
*imo
= *imop
;
895 register struct sockaddr_in
*dst
;
901 * No multicast option buffer attached to the pcb;
902 * allocate one and initialize to default values.
904 imo
= (struct ip_moptions
*)malloc(sizeof(*imo
), M_IPMOPTS
,
910 imo
->imo_multicast_ifp
= NULL
;
911 imo
->imo_multicast_vif
= -1;
912 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
913 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
914 imo
->imo_num_memberships
= 0;
919 /* store an index number for the vif you wanna use in the send */
920 case IP_MULTICAST_VIF
:
921 if (!legal_vif_num
) {
925 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
929 i
= *(mtod(m
, int *));
930 if (!legal_vif_num(i
) && (i
!= -1)) {
934 imo
->imo_multicast_vif
= i
;
937 case IP_MULTICAST_IF
:
939 * Select the interface for outgoing multicast packets.
941 if (m
== NULL
|| m
->m_len
!= sizeof(struct in_addr
)) {
945 addr
= *(mtod(m
, struct in_addr
*));
947 * INADDR_ANY is used to remove a previous selection.
948 * When no interface is selected, a default one is
949 * chosen every time a multicast packet is sent.
951 if (addr
.s_addr
== INADDR_ANY
) {
952 imo
->imo_multicast_ifp
= NULL
;
956 * The selected interface is identified by its local
957 * IP address. Find the interface and confirm that
958 * it supports multicasting.
961 INADDR_TO_IFP(addr
, ifp
);
962 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
963 error
= EADDRNOTAVAIL
;
966 imo
->imo_multicast_ifp
= ifp
;
970 case IP_MULTICAST_TTL
:
972 * Set the IP time-to-live for outgoing multicast packets.
974 if (m
== NULL
|| m
->m_len
!= 1) {
978 imo
->imo_multicast_ttl
= *(mtod(m
, u_char
*));
981 case IP_MULTICAST_LOOP
:
983 * Set the loopback flag for outgoing multicast packets.
984 * Must be zero or one.
986 if (m
== NULL
|| m
->m_len
!= 1 ||
987 (loop
= *(mtod(m
, u_char
*))) > 1) {
991 imo
->imo_multicast_loop
= loop
;
994 case IP_ADD_MEMBERSHIP
:
996 * Add a multicast group membership.
997 * Group must be a valid IP multicast address.
999 if (m
== NULL
|| m
->m_len
!= sizeof(struct ip_mreq
)) {
1003 mreq
= mtod(m
, struct ip_mreq
*);
1004 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
1010 * If no interface address was provided, use the interface of
1011 * the route to the given multicast address.
1013 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
1014 bzero((caddr_t
)&ro
, sizeof(ro
));
1015 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
1016 dst
->sin_len
= sizeof(*dst
);
1017 dst
->sin_family
= AF_INET
;
1018 dst
->sin_addr
= mreq
->imr_multiaddr
;
1020 if (ro
.ro_rt
== NULL
) {
1021 error
= EADDRNOTAVAIL
;
1025 ifp
= ro
.ro_rt
->rt_ifp
;
1029 INADDR_TO_IFP(mreq
->imr_interface
, ifp
);
1033 * See if we found an interface, and confirm that it
1034 * supports multicast.
1036 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1037 error
= EADDRNOTAVAIL
;
1042 * See if the membership already exists or if all the
1043 * membership slots are full.
1045 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1046 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1047 imo
->imo_membership
[i
]->inm_addr
.s_addr
1048 == mreq
->imr_multiaddr
.s_addr
)
1051 if (i
< imo
->imo_num_memberships
) {
1056 if (i
== IP_MAX_MEMBERSHIPS
) {
1057 error
= ETOOMANYREFS
;
1062 * Everything looks good; add a new record to the multicast
1063 * address list for the given interface.
1065 if ((imo
->imo_membership
[i
] =
1066 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
1071 ++imo
->imo_num_memberships
;
1075 case IP_DROP_MEMBERSHIP
:
1077 * Drop a multicast group membership.
1078 * Group must be a valid IP multicast address.
1080 if (m
== NULL
|| m
->m_len
!= sizeof(struct ip_mreq
)) {
1084 mreq
= mtod(m
, struct ip_mreq
*);
1085 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
1092 * If an interface address was specified, get a pointer
1093 * to its ifnet structure.
1095 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
1098 INADDR_TO_IFP(mreq
->imr_interface
, ifp
);
1100 error
= EADDRNOTAVAIL
;
1106 * Find the membership in the membership array.
1108 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1110 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
1111 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
1112 mreq
->imr_multiaddr
.s_addr
)
1115 if (i
== imo
->imo_num_memberships
) {
1116 error
= EADDRNOTAVAIL
;
1121 * Give up the multicast address record to which the
1122 * membership points.
1124 in_delmulti(imo
->imo_membership
[i
]);
1126 * Remove the gap in the membership array.
1128 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
1129 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
1130 --imo
->imo_num_memberships
;
1140 * If all options have default values, no need to keep the mbuf.
1142 if (imo
->imo_multicast_ifp
== NULL
&&
1143 imo
->imo_multicast_vif
== -1 &&
1144 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
1145 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
1146 imo
->imo_num_memberships
== 0) {
1147 free(*imop
, M_IPMOPTS
);
1155 * Return the IP multicast options in response to user getsockopt().
1158 ip_getmoptions(optname
, imo
, mp
)
1160 register struct ip_moptions
*imo
;
1161 register struct mbuf
**mp
;
1166 struct in_addr
*addr
;
1167 struct in_ifaddr
*ia
;
1170 *mp
= m_get(M_WAIT
, MT_SOOPTS
);
1174 case IP_MULTICAST_VIF
:
1176 *(mtod(*mp
, int *)) = imo
->imo_multicast_vif
;
1178 *(mtod(*mp
, int *)) = -1;
1179 (*mp
)->m_len
= sizeof(int);
1182 case IP_MULTICAST_IF
:
1183 addr
= mtod(*mp
, struct in_addr
*);
1184 (*mp
)->m_len
= sizeof(struct in_addr
);
1185 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
1186 addr
->s_addr
= INADDR_ANY
;
1188 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
1189 addr
->s_addr
= (ia
== NULL
) ? INADDR_ANY
1190 : IA_SIN(ia
)->sin_addr
.s_addr
;
1194 case IP_MULTICAST_TTL
:
1195 ttl
= mtod(*mp
, u_char
*);
1197 *ttl
= (imo
== NULL
) ? IP_DEFAULT_MULTICAST_TTL
1198 : imo
->imo_multicast_ttl
;
1201 case IP_MULTICAST_LOOP
:
1202 loop
= mtod(*mp
, u_char
*);
1204 *loop
= (imo
== NULL
) ? IP_DEFAULT_MULTICAST_LOOP
1205 : imo
->imo_multicast_loop
;
1209 return (EOPNOTSUPP
);
1214 * Discard the IP multicast options.
1217 ip_freemoptions(imo
)
1218 register struct ip_moptions
*imo
;
1223 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
1224 in_delmulti(imo
->imo_membership
[i
]);
1225 free(imo
, M_IPMOPTS
);
1231 * Routine called from ip_output() to loop back a copy of an IP multicast
1232 * packet to the input queue of a specified interface. Note that this
1233 * calls the output routine of the loopback "driver", but with an interface
1234 * pointer that might NOT be a loopback interface -- evil, but easier than
1235 * replicating that code here.
1238 ip_mloopback(ifp
, m
, dst
)
1240 register struct mbuf
*m
;
1241 register struct sockaddr_in
*dst
;
1243 register struct ip
*ip
;
1246 copym
= m_copy(m
, 0, M_COPYALL
);
1247 if (copym
!= NULL
) {
1249 * We don't bother to fragment if the IP length is greater
1250 * than the interface's MTU. Can this possibly matter?
1252 ip
= mtod(copym
, struct ip
*);
1253 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
1254 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
1256 ip
->ip_sum
= in_cksum(copym
, ip
->ip_hl
<< 2);
1257 (void) looutput(ifp
, copym
, (struct sockaddr
*)dst
, NULL
);