2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
62 #include "opt_ipdivert.h"
63 #include "opt_ipfilter.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
76 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip_var.h>
90 #include <sys/kdebug.h>
92 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
93 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
94 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
98 #include <machine/mtpr.h>
102 #include <machine/in_cksum.h>
104 static MALLOC_DEFINE(M_IPMOPTS
, "ip_moptions", "internet multicast options");
107 //static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
110 #include <netinet6/ipsec.h>
111 #include <netkey/key.h>
112 #include <netkey/key_debug.h>
116 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
118 #define COMPAT_IPFW 1
124 #include <netinet/ip_fw.h>
128 #include <netinet/ip_dummynet.h>
131 #if IPFIREWALL_FORWARD_DEBUG
132 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
133 (ntohl(a.s_addr)>>16)&0xFF,\
134 (ntohl(a.s_addr)>>8)&0xFF,\
135 (ntohl(a.s_addr))&0xFF);
140 static struct mbuf
*ip_insertoptions
__P((struct mbuf
*, struct mbuf
*, int *));
141 static void ip_mloopback
142 __P((struct ifnet
*, struct mbuf
*, struct sockaddr_in
*, int));
143 static int ip_getmoptions
144 __P((struct sockopt
*, struct ip_moptions
*));
145 static int ip_pcbopts
__P((int, struct mbuf
**, struct mbuf
*));
146 static int ip_setmoptions
147 __P((struct sockopt
*, struct ip_moptions
**));
148 static u_long lo_dl_tag
= 0;
149 static int ip_optcopy
__P((struct ip
*, struct ip
*));
151 void in_delayed_cksum(struct mbuf
*m
);
152 extern int apple_hwcksum_tx
;
154 extern struct protosw inetsw
[];
157 * IP output. The packet in mbuf chain m contains a skeletal IP
158 * header (with len, off, ttl, proto, tos, src, dst).
159 * The mbuf chain containing the packet will be freed.
160 * The mbuf opt, if present, will not be freed.
163 ip_output(m0
, opt
, ro
, flags
, imo
)
168 struct ip_moptions
*imo
;
170 struct ip
*ip
, *mhip
;
174 int hlen
= sizeof (struct ip
);
175 int len
, off
, error
= 0;
176 struct sockaddr_in
*dst
;
177 struct in_ifaddr
*ia
;
178 int isbroadcast
, sw_csum
;
180 struct route iproute
;
182 struct secpolicy
*sp
= NULL
;
184 #if IPFIREWALL_FORWARD
185 int fwd_rewrite_src
= 0;
189 #if !IPDIVERT /* dummy variable for the firewall code to play with */
190 u_short ip_divert_cookie
= 0 ;
193 struct ip_fw_chain
*rule
= NULL
;
196 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
200 * NOTE: m->m_pkthdr is NULL cleared below just to prevent ipfw code
202 * ipfw code uses rcvif to determine incoming interface, and
203 * KAME uses rcvif for ipsec processing.
204 * ipfw may not be working right with KAME at this moment.
205 * We need more tests.
208 if (m
->m_type
== MT_DUMMYNET
) {
209 if (m
->m_next
!= NULL
) {
210 so
= (struct socket
*)m
->m_next
->m_pkthdr
.rcvif
;
211 m
->m_next
->m_pkthdr
.rcvif
= NULL
;
217 so
= ipsec_getsocket(m
);
218 ipsec_setsocket(m
, NULL
);
223 #if IPFIREWALL && DUMMYNET
225 * dummynet packet are prepended a vestigial mbuf with
226 * m_type = MT_DUMMYNET and m_data pointing to the matching
229 if (m
->m_type
== MT_DUMMYNET
) {
230 struct mbuf
*tmp_m
= m
;
232 * the packet was already tagged, so part of the
233 * processing was already done, and we need to go down.
234 * opt, flags and imo have already been used, and now
235 * they are used to hold ifp and hlen and NULL, respectively.
237 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
240 ip
= mtod(m
, struct ip
*);
241 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
242 ifp
= (struct ifnet
*)opt
;
243 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
245 flags
= 0 ; /* XXX is this correct ? */
252 if ((m
->m_flags
& M_PKTHDR
) == 0)
253 panic("ip_output no HDR");
255 panic("ip_output no route, proto = %d",
256 mtod(m
, struct ip
*)->ip_p
);
259 m
= ip_insertoptions(m
, opt
, &len
);
262 ip
= mtod(m
, struct ip
*);
266 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
267 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
269 ip
->ip_id
= htons(ip_id
++);
270 ipstat
.ips_localout
++;
272 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
275 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
276 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
278 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
280 * If there is a cached route,
281 * check that it is to the same destination
282 * and is still up. If not, free it and try again.
284 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
285 dst
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
287 ro
->ro_rt
= (struct rtentry
*)0;
289 if (ro
->ro_rt
== 0) {
290 dst
->sin_family
= AF_INET
;
291 dst
->sin_len
= sizeof(*dst
);
292 dst
->sin_addr
= ip
->ip_dst
;
295 * If routing to interface only,
296 * short circuit routing lookup.
298 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
299 #define sintosa(sin) ((struct sockaddr *)(sin))
300 if (flags
& IP_ROUTETOIF
) {
301 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0 &&
302 (ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
303 ipstat
.ips_noroute
++;
308 dl_tag
= ia
->ia_ifa
.ifa_dlt
;
310 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
313 * If this is the case, we probably don't want to allocate
314 * a protocol-cloned route since we didn't get one from the
315 * ULP. This lets TCP do its thing, while not burdening
316 * forwarding or ICMP with the overhead of cloning a route.
317 * Of course, we still want to do any cloning requested by
318 * the link layer, as this is probably required in all cases
319 * for correct operation (as it is for ARP).
322 rtalloc_ign(ro
, RTF_PRCLONING
);
323 if (ro
->ro_rt
== 0) {
324 ipstat
.ips_noroute
++;
325 error
= EHOSTUNREACH
;
328 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
329 ifp
= ro
->ro_rt
->rt_ifp
;
330 dl_tag
= ro
->ro_rt
->rt_dlt
;
332 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
333 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
334 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
335 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
337 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
339 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
340 struct in_multi
*inm
;
342 m
->m_flags
|= M_MCAST
;
344 * IP destination address is multicast. Make sure "dst"
345 * still points to the address in "ro". (It may have been
346 * changed to point to a gateway address, above.)
348 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
350 * See if the caller provided any multicast options
353 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
354 if (imo
->imo_multicast_ifp
!= NULL
) {
355 ifp
= imo
->imo_multicast_ifp
;
356 dl_tag
= ifp
->if_data
.default_proto
;
358 if (imo
->imo_multicast_vif
!= -1)
360 ip_mcast_src(imo
->imo_multicast_vif
);
362 ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
364 * Confirm that the outgoing interface supports multicast.
366 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
367 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
368 ipstat
.ips_noroute
++;
374 * If source address not specified yet, use address
375 * of outgoing interface.
377 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
378 register struct in_ifaddr
*ia1
;
380 for (ia1
= in_ifaddrhead
.tqh_first
; ia1
;
381 ia1
= ia1
->ia_link
.tqe_next
)
382 if (ia1
->ia_ifp
== ifp
) {
383 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
388 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
390 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
392 * If we belong to the destination multicast group
393 * on the outgoing interface, and the caller did not
394 * forbid loopback, loop back a copy.
396 ip_mloopback(ifp
, m
, dst
, hlen
);
400 * If we are acting as a multicast router, perform
401 * multicast forwarding as if the packet had just
402 * arrived on the interface to which we are about
403 * to send. The multicast forwarding function
404 * recursively calls this function, using the
405 * IP_FORWARDING flag to prevent infinite recursion.
407 * Multicasts that are looped back by ip_mloopback(),
408 * above, will be forwarded by the ip_input() routine,
411 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
413 * Check if rsvp daemon is running. If not, don't
414 * set ip_moptions. This ensures that the packet
415 * is multicast and not just sent down one link
416 * as prescribed by rsvpd.
420 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
428 * Multicasts with a time-to-live of zero may be looped-
429 * back, above, but must not be transmitted on a network.
430 * Also, multicasts addressed to the loopback interface
431 * are not sent -- the above call to ip_mloopback() will
432 * loop back a copy if this host actually belongs to the
433 * destination group on the loopback interface.
435 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
444 * If source address not specified yet, use address
445 * of outgoing interface.
447 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
448 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
449 #if IPFIREWALL_FORWARD
450 /* Keep note that we did this - if the firewall changes
451 * the next-hop, our interface may change, changing the
452 * default source IP. It's a shame so much effort happens
456 #endif /* IPFIREWALL_FORWARD */
460 * Verify that we have any chance at all of being able to queue
461 * the packet or packet fragments
463 if ((ifp
->if_snd
.ifq_len
+ ip
->ip_len
/ ifp
->if_mtu
+ 1) >=
464 ifp
->if_snd
.ifq_maxlen
) {
470 * Look for broadcast address and
471 * and verify user is allowed to send
475 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
476 error
= EADDRNOTAVAIL
;
479 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
483 /* don't allow broadcast messages to be fragmented */
484 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
488 m
->m_flags
|= M_BCAST
;
490 m
->m_flags
&= ~M_BCAST
;
496 * - Xlate: translate packet's addr/port (NAT).
497 * - Firewall: deny/allow/etc.
498 * - Wrap: fake packet's addr/port <unimpl.>
499 * - Encapsulate: put it in another IP and send out. <unimp.>
502 if (ip_nat_ptr
&& !(*ip_nat_ptr
)(&ip
, &m
, ifp
, IP_NAT_OUT
)) {
508 * Check with the firewall...
511 struct sockaddr_in
*old
= dst
;
513 off
= (*ip_fw_chk_ptr
)(&ip
,
514 hlen
, ifp
, &ip_divert_cookie
, &m
, &rule
, &dst
);
516 * On return we must do the following:
517 * m == NULL -> drop the pkt
518 * 1<=off<= 0xffff -> DIVERT
519 * (off & 0x10000) -> send to a DUMMYNET pipe
520 * dst != old -> IPFIREWALL_FORWARD
521 * off==0, dst==old -> accept
522 * If some of the above modules is not compiled in, then
523 * we should't have to check the corresponding condition
524 * (because the ipfw control socket should not accept
525 * unsupported rules), but better play safe and drop
526 * packets in case of doubt.
528 if (!m
) { /* firewall said to reject */
532 if (off
== 0 && dst
== old
) /* common case */
537 * pass the pkt to dummynet. Need to include
538 * pipe number, m, ifp, ro, hlen because these are
539 * not recomputed in the next pass.
540 * All other parameters have been already used and
541 * so they are not needed anymore.
542 * XXX note: if the ifp or ro entry are deleted
543 * while a pkt is in dummynet, we are in trouble!
545 dummynet_io(off
& 0xffff, DN_TO_IP_OUT
, m
,ifp
,ro
,hlen
,rule
);
550 if (off
> 0 && off
< 0x10000) { /* Divert packet */
553 * delayed checksums are not currently compatible
554 * with divert sockets.
556 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
560 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
563 /* Restore packet header fields to original values */
564 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
565 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
567 ip_divert_port
= off
& 0xffff ;
568 (*ip_protox
[IPPROTO_DIVERT
]->pr_input
)(m
, 0);
573 #if IPFIREWALL_FORWARD
574 /* Here we check dst to make sure it's directly reachable on the
575 * interface we previously thought it was.
576 * If it isn't (which may be likely in some situations) we have
577 * to re-route it (ie, find a route for the next-hop and the
578 * associated interface) and set them here. This is nested
579 * forwarding which in most cases is undesirable, except where
580 * such control is nigh impossible. So we do it here.
583 if (off
== 0 && old
!= dst
) {
584 struct in_ifaddr
*ia
;
586 /* It's changed... */
587 /* There must be a better way to do this next line... */
588 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
589 #if IPFIREWALL_FORWARD_DEBUG
590 printf("IPFIREWALL_FORWARD: New dst ip: ");
591 print_ip(dst
->sin_addr
);
595 * We need to figure out if we have been forwarded
596 * to a local socket. If so then we should somehow
597 * "loop back" to ip_input, and get directed to the
598 * PCB as if we had received this packet. This is
599 * because it may be dificult to identify the packets
600 * you want to forward until they are being output
601 * and have selected an interface. (e.g. locally
602 * initiated packets) If we used the loopback inteface,
603 * we would not be able to control what happens
604 * as the packet runs through ip_input() as
605 * it is done through a ISR.
607 for (ia
= TAILQ_FIRST(&in_ifaddrhead
); ia
;
608 ia
= TAILQ_NEXT(ia
, ia_link
)) {
610 * If the addr to forward to is one
611 * of ours, we pretend to
612 * be the destination for this packet.
614 if (IA_SIN(ia
)->sin_addr
.s_addr
==
615 dst
->sin_addr
.s_addr
)
619 /* tell ip_input "dont filter" */
620 ip_fw_fwd_addr
= dst
;
621 if (m
->m_pkthdr
.rcvif
== NULL
)
622 m
->m_pkthdr
.rcvif
= ifunit("lo0");
624 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
625 m
->m_pkthdr
.csum_flags
|=
626 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
627 m0
->m_pkthdr
.csum_data
= 0xffff;
629 m
->m_pkthdr
.csum_flags
|=
630 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
631 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
632 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
638 /* Some of the logic for this was
641 * This rewrites the cached route in a local PCB.
642 * Is this what we want to do?
644 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
647 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
649 if (ro_fwd
->ro_rt
== 0) {
650 ipstat
.ips_noroute
++;
651 error
= EHOSTUNREACH
;
655 ia
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
656 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
657 dl_tag
= ro
->ro_rt
->rt_dlt
;
658 ro_fwd
->ro_rt
->rt_use
++;
659 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
660 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
661 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
663 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
665 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
667 ro
->ro_rt
= ro_fwd
->ro_rt
;
668 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
671 * If we added a default src ip earlier,
672 * which would have been gotten from the-then
673 * interface, do it again, from the new one.
676 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
679 #endif /* IPFIREWALL_FORWARD */
681 * if we get here, none of the above matches, and
682 * we have to drop the pkt
685 error
= EACCES
; /* not sure this is the right error msg */
688 #endif /* COMPAT_IPFW */
694 * Processing IP filter/NAT.
695 * Return TRUE iff this packet is discarded.
696 * Return FALSE iff this packet is accepted.
699 if (doNatFil
&& pm_out(ro
->ro_rt
->rt_ifp
, ip
, m
))
704 /* get SP for this packet */
706 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
708 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
711 ipsecstat
.out_inval
++;
718 switch (sp
->policy
) {
719 case IPSEC_POLICY_DISCARD
:
721 * This packet is just discarded.
723 ipsecstat
.out_polvio
++;
726 case IPSEC_POLICY_BYPASS
:
727 case IPSEC_POLICY_NONE
:
728 /* no need to do IPsec. */
731 case IPSEC_POLICY_IPSEC
:
732 if (sp
->req
== NULL
) {
733 /* XXX should be panic ? */
734 printf("ip_output: No IPsec request specified.\n");
740 case IPSEC_POLICY_ENTRUST
:
742 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
747 struct ipsec_output_state state
;
748 bzero(&state
, sizeof(state
));
750 if (flags
& IP_ROUTETOIF
) {
752 bzero(&iproute
, sizeof(iproute
));
755 state
.dst
= (struct sockaddr
*)dst
;
760 * delayed checksums are not currently compatible with IPsec
762 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
766 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
769 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
770 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
772 error
= ipsec4_output(&state
, sp
, flags
);
775 if (flags
& IP_ROUTETOIF
) {
777 * if we have tunnel mode SA, we may need to ignore
780 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
781 flags
&= ~IP_ROUTETOIF
;
786 dst
= (struct sockaddr_in
*)state
.dst
;
788 /* mbuf is already reclaimed in ipsec4_output. */
798 printf("ip4_output (ipsec): error code %d\n", error
);
801 /* don't show these error codes to the user */
809 /* be sure to update variables that are affected by ipsec4_output() */
810 ip
= mtod(m
, struct ip
*);
812 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
814 hlen
= ip
->ip_hl
<< 2;
816 if (ro
->ro_rt
== NULL
) {
817 if ((flags
& IP_ROUTETOIF
) == 0) {
819 "can't update route after IPsec processing\n");
820 error
= EHOSTUNREACH
; /*XXX*/
824 /* nobody uses ia beyond here */
825 ifp
= ro
->ro_rt
->rt_ifp
;
828 /* make it flipped, again. */
829 ip
->ip_len
= ntohs((u_short
)ip
->ip_len
);
830 ip
->ip_off
= ntohs((u_short
)ip
->ip_off
);
835 sw_csum
= m
->m_pkthdr
.csum_flags
| CSUM_IP
;
838 /* frames that can be checksumed by GMACE SUM16 HW: frame >64, no fragments, no UDP odd length */
840 if (apple_hwcksum_tx
&& (sw_csum
& CSUM_DELAY_DATA
) && (ifp
->if_hwassist
& CSUM_TCP_SUM16
)
841 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)
842 && !((ip
->ip_len
& 0x1) && (sw_csum
& CSUM_UDP
)) ) {
844 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
845 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
846 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
847 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
848 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
849 m
->m_pkthdr
.csum_data
+= offset
;
850 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
853 if (ifp
->if_hwassist
& CSUM_TCP_SUM16
) /* force SW checksuming */
854 m
->m_pkthdr
.csum_flags
= 0;
855 else { /* not Apple enet */
856 m
->m_pkthdr
.csum_flags
= sw_csum
& ifp
->if_hwassist
;
857 sw_csum
&= ~ifp
->if_hwassist
;
860 if (sw_csum
& CSUM_DELAY_DATA
) { /* perform TCP/UDP checksuming now */
864 sw_csum
&= ~CSUM_DELAY_DATA
;
869 * If small enough for interface, or the interface will take
870 * care of the fragmentation for us, can just send directly.
872 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
||
873 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
875 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
876 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
878 if (sw_csum
& CSUM_DELAY_IP
)
879 ip
->ip_sum
= in_cksum(m
, hlen
);
880 error
= dlil_output(dl_tag
, m
, (void *) ro
->ro_rt
,
881 (struct sockaddr
*)dst
, 0);
885 * Too large for interface; fragment if possible.
886 * Must be able to put at least 8 bytes per fragment.
888 if (ip
->ip_off
& IP_DF
) {
891 * This case can happen if the user changed the MTU
892 * of an interface after enabling IP on it. Because
893 * most netifs don't keep track of routes pointing to
894 * them, there is no way for one to update all its
895 * routes when the MTU is changed.
897 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
898 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
899 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
900 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
902 ipstat
.ips_cantfrag
++;
905 len
= (ifp
->if_mtu
- hlen
) &~ 7;
912 * if the interface will not calculate checksums on
913 * fragmented packets, then do it here.
915 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
916 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
920 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
925 int mhlen
, firstlen
= len
;
926 struct mbuf
**mnext
= &m
->m_nextpkt
;
931 * Loop through length of segment after first fragment,
932 * make new header and copy data of each part and link onto chain.
935 mhlen
= sizeof (struct ip
);
936 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
937 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
940 ipstat
.ips_odropped
++;
943 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
944 m
->m_data
+= max_linkhdr
;
945 mhip
= mtod(m
, struct ip
*);
947 if (hlen
> sizeof (struct ip
)) {
948 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
949 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
952 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
953 if (ip
->ip_off
& IP_MF
)
954 mhip
->ip_off
|= IP_MF
;
955 if (off
+ len
>= (u_short
)ip
->ip_len
)
956 len
= (u_short
)ip
->ip_len
- off
;
958 mhip
->ip_off
|= IP_MF
;
959 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
960 m
->m_next
= m_copy(m0
, off
, len
);
961 if (m
->m_next
== 0) {
963 error
= ENOBUFS
; /* ??? */
964 ipstat
.ips_odropped
++;
967 m
->m_pkthdr
.len
= mhlen
+ len
;
968 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
969 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
970 mhip
->ip_off
= htons((u_short
)mhip
->ip_off
);
972 if (sw_csum
& CSUM_DELAY_IP
)
973 mhip
->ip_sum
= in_cksum(m
, mhlen
);
975 mnext
= &m
->m_nextpkt
;
978 ipstat
.ips_ofragments
+= nfrags
;
980 /* set first/last markers for fragment chain */
981 m0
->m_flags
|= M_FRAG
;
982 m0
->m_pkthdr
.csum_data
= nfrags
;
985 * Update first fragment by trimming what's been copied out
986 * and updating header, then send each fragment (in order).
989 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
990 m
->m_pkthdr
.len
= hlen
+ firstlen
;
991 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
992 ip
->ip_off
= htons((u_short
)(ip
->ip_off
| IP_MF
));
994 if (sw_csum
& CSUM_DELAY_IP
)
995 ip
->ip_sum
= in_cksum(m
, hlen
);
999 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1000 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1002 for (m
= m0
; m
; m
= m0
) {
1006 error
= dlil_output(dl_tag
, m
, (void *) ro
->ro_rt
,
1007 (struct sockaddr
*)dst
, 0);
1013 ipstat
.ips_fragmented
++;
1017 if (ro
== &iproute
&& ro
->ro_rt
) {
1022 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1023 printf("DP ip_output call free SP:%x\n", sp
));
1028 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1035 extern u_short
in_chksum_skip(struct mbuf
*, int, int);
1038 in_delayed_cksum(struct mbuf
*m
)
1041 u_short csum
, csum2
, offset
;
1043 ip
= mtod(m
, struct ip
*);
1044 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1046 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1048 if ((m
->m_pkthdr
.csum_flags
& CSUM_UDP
) && csum
== 0)
1051 offset
+= m
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1053 if (offset
> ip
->ip_len
) /* bogus offset */
1056 if (offset
+ sizeof(u_short
) > m
->m_len
) {
1057 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1058 m
->m_len
, offset
, ip
->ip_p
);
1061 * this shouldn't happen, but if it does, the
1062 * correct behavior may be to insert the checksum
1063 * in the existing chain instead of rearranging it.
1065 if (m
= m_pullup(m
, offset
+ sizeof(u_short
)) == 0)
1069 *(u_short
*)(m
->m_data
+ offset
) = csum
;
1073 * Insert IP options into preformed packet.
1074 * Adjust IP destination as required for IP source routing,
1075 * as indicated by a non-zero in_addr at the start of the options.
1077 * XXX This routine assumes that the packet has no options in place.
1079 static struct mbuf
*
1080 ip_insertoptions(m
, opt
, phlen
)
1081 register struct mbuf
*m
;
1085 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1087 register struct ip
*ip
= mtod(m
, struct ip
*);
1090 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1091 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1092 return (m
); /* XXX should fail */
1093 if (p
->ipopt_dst
.s_addr
)
1094 ip
->ip_dst
= p
->ipopt_dst
;
1095 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1096 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
1099 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1100 m
->m_len
-= sizeof(struct ip
);
1101 m
->m_data
+= sizeof(struct ip
);
1104 m
->m_len
= optlen
+ sizeof(struct ip
);
1105 m
->m_data
+= max_linkhdr
;
1106 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1108 m
->m_data
-= optlen
;
1110 m
->m_pkthdr
.len
+= optlen
;
1111 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1113 ip
= mtod(m
, struct ip
*);
1114 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1115 *phlen
= sizeof(struct ip
) + optlen
;
1116 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1117 ip
->ip_len
+= optlen
;
1122 * Copy options from ip to jp,
1123 * omitting those not copied during fragmentation.
1129 register u_char
*cp
, *dp
;
1130 int opt
, optlen
, cnt
;
1132 cp
= (u_char
*)(ip
+ 1);
1133 dp
= (u_char
*)(jp
+ 1);
1134 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1135 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1137 if (opt
== IPOPT_EOL
)
1139 if (opt
== IPOPT_NOP
) {
1140 /* Preserve for IP mcast tunnel's LSRR alignment. */
1145 optlen
= cp
[IPOPT_OLEN
];
1146 /* bogus lengths should have been caught by ip_dooptions */
1149 if (IPOPT_COPIED(opt
)) {
1150 bcopy(cp
, dp
, optlen
);
1154 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1160 * IP socket option processing.
1163 ip_ctloutput(so
, sopt
)
1165 struct sockopt
*sopt
;
1167 struct inpcb
*inp
= sotoinpcb(so
);
1171 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1175 switch (sopt
->sopt_dir
) {
1177 switch (sopt
->sopt_name
) {
1184 if (sopt
->sopt_valsize
> MLEN
) {
1188 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1193 m
->m_len
= sopt
->sopt_valsize
;
1194 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1199 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1206 case IP_RECVRETOPTS
:
1207 case IP_RECVDSTADDR
:
1210 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1215 switch (sopt
->sopt_name
) {
1217 inp
->inp_ip_tos
= optval
;
1221 inp
->inp_ip_ttl
= optval
;
1223 #define OPTSET(bit) \
1225 inp->inp_flags |= bit; \
1227 inp->inp_flags &= ~bit;
1230 OPTSET(INP_RECVOPTS
);
1233 case IP_RECVRETOPTS
:
1234 OPTSET(INP_RECVRETOPTS
);
1237 case IP_RECVDSTADDR
:
1238 OPTSET(INP_RECVDSTADDR
);
1252 case IP_MULTICAST_IF
:
1253 case IP_MULTICAST_VIF
:
1254 case IP_MULTICAST_TTL
:
1255 case IP_MULTICAST_LOOP
:
1256 case IP_ADD_MEMBERSHIP
:
1257 case IP_DROP_MEMBERSHIP
:
1258 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1262 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1268 case IP_PORTRANGE_DEFAULT
:
1269 inp
->inp_flags
&= ~(INP_LOWPORT
);
1270 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1273 case IP_PORTRANGE_HIGH
:
1274 inp
->inp_flags
&= ~(INP_LOWPORT
);
1275 inp
->inp_flags
|= INP_HIGHPORT
;
1278 case IP_PORTRANGE_LOW
:
1279 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1280 inp
->inp_flags
|= INP_LOWPORT
;
1290 case IP_IPSEC_POLICY
:
1298 if (error
= sooptgetm(sopt
, &m
)) /* XXX */
1300 if (error
= sooptmcopyin(sopt
, m
)) /* XXX */
1302 priv
= (sopt
->sopt_p
!= NULL
&&
1303 suser(sopt
->sopt_p
->p_ucred
,
1304 &sopt
->sopt_p
->p_acflag
) != 0) ? 0 : 1;
1306 req
= mtod(m
, caddr_t
);
1309 optname
= sopt
->sopt_name
;
1310 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1317 error
= ENOPROTOOPT
;
1323 switch (sopt
->sopt_name
) {
1326 if (inp
->inp_options
)
1327 error
= sooptcopyout(sopt
,
1328 mtod(inp
->inp_options
,
1330 inp
->inp_options
->m_len
);
1332 sopt
->sopt_valsize
= 0;
1338 case IP_RECVRETOPTS
:
1339 case IP_RECVDSTADDR
:
1343 switch (sopt
->sopt_name
) {
1346 optval
= inp
->inp_ip_tos
;
1350 optval
= inp
->inp_ip_ttl
;
1353 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1356 optval
= OPTBIT(INP_RECVOPTS
);
1359 case IP_RECVRETOPTS
:
1360 optval
= OPTBIT(INP_RECVRETOPTS
);
1363 case IP_RECVDSTADDR
:
1364 optval
= OPTBIT(INP_RECVDSTADDR
);
1368 optval
= OPTBIT(INP_RECVIF
);
1372 if (inp
->inp_flags
& INP_HIGHPORT
)
1373 optval
= IP_PORTRANGE_HIGH
;
1374 else if (inp
->inp_flags
& INP_LOWPORT
)
1375 optval
= IP_PORTRANGE_LOW
;
1381 optval
= OPTBIT(INP_FAITH
);
1384 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1387 case IP_MULTICAST_IF
:
1388 case IP_MULTICAST_VIF
:
1389 case IP_MULTICAST_TTL
:
1390 case IP_MULTICAST_LOOP
:
1391 case IP_ADD_MEMBERSHIP
:
1392 case IP_DROP_MEMBERSHIP
:
1393 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1397 case IP_IPSEC_POLICY
:
1399 struct mbuf
*m
= NULL
;
1403 if (error
= sooptgetm(sopt
, &m
)) /* XXX */
1405 if (error
= sooptmcopyin(sopt
, m
)) /* XXX */
1408 req
= mtod(m
, caddr_t
);
1412 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
1414 error
= sooptmcopyout(sopt
, m
); /* XXX */
1416 /* if error, m_freem called at soopt_mcopyout(). */
1424 error
= ENOPROTOOPT
;
1433 * Set up IP options in pcb for insertion in output packets.
1434 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1435 * with destination address if source routed.
1438 ip_pcbopts(optname
, pcbopt
, m
)
1440 struct mbuf
**pcbopt
;
1441 register struct mbuf
*m
;
1443 register int cnt
, optlen
;
1444 register u_char
*cp
;
1447 /* turn off any old options */
1449 (void)m_free(*pcbopt
);
1451 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
1453 * Only turning off any previous options.
1461 if (m
->m_len
% sizeof(int32_t))
1465 * IP first-hop destination address will be stored before
1466 * actual options; move other options back
1467 * and clear it when none present.
1469 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1472 m
->m_len
+= sizeof(struct in_addr
);
1473 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1474 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
1475 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1477 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1478 opt
= cp
[IPOPT_OPTVAL
];
1479 if (opt
== IPOPT_EOL
)
1481 if (opt
== IPOPT_NOP
)
1484 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1486 optlen
= cp
[IPOPT_OLEN
];
1487 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1498 * user process specifies route as:
1500 * D must be our final destination (but we can't
1501 * check that since we may not have connected yet).
1502 * A is first hop destination, which doesn't appear in
1503 * actual IP option, but is stored before the options.
1505 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1507 m
->m_len
-= sizeof(struct in_addr
);
1508 cnt
-= sizeof(struct in_addr
);
1509 optlen
-= sizeof(struct in_addr
);
1510 cp
[IPOPT_OLEN
] = optlen
;
1512 * Move first hop before start of options.
1514 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1515 sizeof(struct in_addr
));
1517 * Then copy rest of options back
1518 * to close up the deleted entry.
1520 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
1521 sizeof(struct in_addr
)),
1522 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
1523 (unsigned)cnt
+ sizeof(struct in_addr
));
1527 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1539 * The whole multicast option thing needs to be re-thought.
1540 * Several of these options are equally applicable to non-multicast
1541 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1542 * standard option (IP_TTL).
1545 * Set the IP multicast options in response to user setsockopt().
1548 ip_setmoptions(sopt
, imop
)
1549 struct sockopt
*sopt
;
1550 struct ip_moptions
**imop
;
1554 struct in_addr addr
;
1555 struct ip_mreq mreq
;
1557 struct ip_moptions
*imo
= *imop
;
1559 struct sockaddr_in
*dst
;
1564 * No multicast option buffer attached to the pcb;
1565 * allocate one and initialize to default values.
1567 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
1573 imo
->imo_multicast_ifp
= NULL
;
1574 imo
->imo_multicast_vif
= -1;
1575 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1576 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
1577 imo
->imo_num_memberships
= 0;
1580 switch (sopt
->sopt_name
) {
1581 /* store an index number for the vif you wanna use in the send */
1582 case IP_MULTICAST_VIF
:
1583 if (legal_vif_num
== 0) {
1587 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
1590 if (!legal_vif_num(i
) && (i
!= -1)) {
1594 imo
->imo_multicast_vif
= i
;
1597 case IP_MULTICAST_IF
:
1599 * Select the interface for outgoing multicast packets.
1601 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
1605 * INADDR_ANY is used to remove a previous selection.
1606 * When no interface is selected, a default one is
1607 * chosen every time a multicast packet is sent.
1609 if (addr
.s_addr
== INADDR_ANY
) {
1610 imo
->imo_multicast_ifp
= NULL
;
1614 * The selected interface is identified by its local
1615 * IP address. Find the interface and confirm that
1616 * it supports multicasting.
1619 INADDR_TO_IFP(addr
, ifp
);
1620 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1622 error
= EADDRNOTAVAIL
;
1625 imo
->imo_multicast_ifp
= ifp
;
1629 case IP_MULTICAST_TTL
:
1631 * Set the IP time-to-live for outgoing multicast packets.
1632 * The original multicast API required a char argument,
1633 * which is inconsistent with the rest of the socket API.
1634 * We allow either a char or an int.
1636 if (sopt
->sopt_valsize
== 1) {
1638 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
1641 imo
->imo_multicast_ttl
= ttl
;
1644 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
1651 imo
->imo_multicast_ttl
= ttl
;
1655 case IP_MULTICAST_LOOP
:
1657 * Set the loopback flag for outgoing multicast packets.
1658 * Must be zero or one. The original multicast API required a
1659 * char argument, which is inconsistent with the rest
1660 * of the socket API. We allow either a char or an int.
1662 if (sopt
->sopt_valsize
== 1) {
1664 error
= sooptcopyin(sopt
, &loop
, 1, 1);
1667 imo
->imo_multicast_loop
= !!loop
;
1670 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
1674 imo
->imo_multicast_loop
= !!loop
;
1678 case IP_ADD_MEMBERSHIP
:
1680 * Add a multicast group membership.
1681 * Group must be a valid IP multicast address.
1683 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1687 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1693 * If no interface address was provided, use the interface of
1694 * the route to the given multicast address.
1696 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
) {
1697 bzero((caddr_t
)&ro
, sizeof(ro
));
1698 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
1699 dst
->sin_len
= sizeof(*dst
);
1700 dst
->sin_family
= AF_INET
;
1701 dst
->sin_addr
= mreq
.imr_multiaddr
;
1703 if (ro
.ro_rt
== NULL
) {
1704 error
= EADDRNOTAVAIL
;
1708 ifp
= ro
.ro_rt
->rt_ifp
;
1712 INADDR_TO_IFP(mreq
.imr_interface
, ifp
);
1716 * See if we found an interface, and confirm that it
1717 * supports multicast.
1719 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1720 error
= EADDRNOTAVAIL
;
1725 * See if the membership already exists or if all the
1726 * membership slots are full.
1728 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1729 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1730 imo
->imo_membership
[i
]->inm_addr
.s_addr
1731 == mreq
.imr_multiaddr
.s_addr
)
1734 if (i
< imo
->imo_num_memberships
) {
1739 if (i
== IP_MAX_MEMBERSHIPS
) {
1740 error
= ETOOMANYREFS
;
1745 * Everything looks good; add a new record to the multicast
1746 * address list for the given interface.
1748 if ((imo
->imo_membership
[i
] =
1749 in_addmulti(&mreq
.imr_multiaddr
, ifp
)) == NULL
) {
1754 ++imo
->imo_num_memberships
;
1758 case IP_DROP_MEMBERSHIP
:
1760 * Drop a multicast group membership.
1761 * Group must be a valid IP multicast address.
1763 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1767 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1774 * If an interface address was specified, get a pointer
1775 * to its ifnet structure.
1777 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
)
1780 INADDR_TO_IFP(mreq
.imr_interface
, ifp
);
1782 error
= EADDRNOTAVAIL
;
1788 * Find the membership in the membership array.
1790 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1792 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
1793 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
1794 mreq
.imr_multiaddr
.s_addr
)
1797 if (i
== imo
->imo_num_memberships
) {
1798 error
= EADDRNOTAVAIL
;
1803 * Give up the multicast address record to which the
1804 * membership points.
1806 in_delmulti(imo
->imo_membership
[i
]);
1808 * Remove the gap in the membership array.
1810 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
1811 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
1812 --imo
->imo_num_memberships
;
1822 * If all options have default values, no need to keep the mbuf.
1824 if (imo
->imo_multicast_ifp
== NULL
&&
1825 imo
->imo_multicast_vif
== -1 &&
1826 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
1827 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
1828 imo
->imo_num_memberships
== 0) {
1829 FREE(*imop
, M_IPMOPTS
);
1837 * Return the IP multicast options in response to user getsockopt().
1840 ip_getmoptions(sopt
, imo
)
1841 struct sockopt
*sopt
;
1842 register struct ip_moptions
*imo
;
1844 struct in_addr addr
;
1845 struct in_ifaddr
*ia
;
1850 switch (sopt
->sopt_name
) {
1851 case IP_MULTICAST_VIF
:
1853 optval
= imo
->imo_multicast_vif
;
1856 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1859 case IP_MULTICAST_IF
:
1860 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
1861 addr
.s_addr
= INADDR_ANY
;
1863 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
1864 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
1865 : IA_SIN(ia
)->sin_addr
.s_addr
;
1867 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
1870 case IP_MULTICAST_TTL
:
1872 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
1874 optval
= coptval
= imo
->imo_multicast_ttl
;
1875 if (sopt
->sopt_valsize
== 1)
1876 error
= sooptcopyout(sopt
, &coptval
, 1);
1878 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1881 case IP_MULTICAST_LOOP
:
1883 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
1885 optval
= coptval
= imo
->imo_multicast_loop
;
1886 if (sopt
->sopt_valsize
== 1)
1887 error
= sooptcopyout(sopt
, &coptval
, 1);
1889 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1893 error
= ENOPROTOOPT
;
1900 * Discard the IP multicast options.
1903 ip_freemoptions(imo
)
1904 register struct ip_moptions
*imo
;
1909 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
1910 in_delmulti(imo
->imo_membership
[i
]);
1911 FREE(imo
, M_IPMOPTS
);
1916 * Routine called from ip_output() to loop back a copy of an IP multicast
1917 * packet to the input queue of a specified interface. Note that this
1918 * calls the output routine of the loopback "driver", but with an interface
1919 * pointer that might NOT be a loopback interface -- evil, but easier than
1920 * replicating that code here.
1923 ip_mloopback(ifp
, m
, dst
, hlen
)
1925 register struct mbuf
*m
;
1926 register struct sockaddr_in
*dst
;
1929 register struct ip
*ip
;
1932 copym
= m_copy(m
, 0, M_COPYALL
);
1933 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
1934 copym
= m_pullup(copym
, hlen
);
1935 if (copym
!= NULL
) {
1937 * We don't bother to fragment if the IP length is greater
1938 * than the interface's MTU. Can this possibly matter?
1940 ip
= mtod(copym
, struct ip
*);
1941 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
1942 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
1944 ip
->ip_sum
= in_cksum(copym
, hlen
);
1948 * It's not clear whether there are any lingering
1949 * reentrancy problems in other areas which might
1950 * be exposed by using ip_input directly (in
1951 * particular, everything which modifies the packet
1952 * in-place). Yet another option is using the
1953 * protosw directly to deliver the looped back
1954 * packet. For the moment, we'll err on the side
1955 * of safety by using if_simloop().
1958 if (dst
->sin_family
!= AF_INET
) {
1959 printf("ip_mloopback: bad address family %d\n",
1961 dst
->sin_family
= AF_INET
;
1967 * Mark checksum as valid or calculate checksum for loopback.
1969 * This is done this way because we have to embed the ifp of
1970 * the interface we will send the original copy of the packet
1971 * out on in the mbuf. ip_input will check if_hwassist of the
1972 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
1973 * The UDP checksum has not been calculated yet.
1975 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1976 if (ifp
->if_hwassist
) {
1977 copym
->m_pkthdr
.csum_flags
|=
1978 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
1979 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1980 copym
->m_pkthdr
.csum_data
= 0xffff;
1982 in_delayed_cksum(copym
);
1988 * We need to send all loopback traffic down to dlil in case
1989 * a filter has tapped-in.
1993 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK
, 0, PF_INET
, &lo_dl_tag
);
1996 * Stuff the 'real' ifp into the pkthdr, to be used in matching
1997 * in ip_input(); we need the loopback ifp/dl_tag passed as args
1998 * to make the loopback driver compliant with the data link
2002 { copym
->m_pkthdr
.rcvif
= ifp
;
2003 dlil_output(lo_dl_tag
, copym
, 0, (struct sockaddr
*) dst
, 0);
2005 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
2009 /* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/