2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1988, 1990, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.16 2001/07/19 06:37:26 kris Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/malloc.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <kern/locks.h>
77 #include <sys/sysctl.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip_var.h>
89 #include <netinet/kpi_ipfilter_var.h>
94 #include <sys/kdebug.h>
96 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
97 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
98 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
99 #define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
106 #include <netkey/key_debug.h>
108 #define KEYDEBUG(lev,arg)
112 #include <netinet/ip_fw.h>
113 #include <netinet/ip_divert.h>
116 #include <netinet/ip_dummynet.h>
119 #if IPFIREWALL_FORWARD_DEBUG
120 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
121 (ntohl(a.s_addr)>>16)&0xFF,\
122 (ntohl(a.s_addr)>>8)&0xFF,\
123 (ntohl(a.s_addr))&0xFF);
127 extern lck_mtx_t
*sadb_mutex
;
132 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
133 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
134 static void ip_mloopback(struct ifnet
*, struct mbuf
*,
135 struct sockaddr_in
*, int);
136 static int ip_getmoptions(struct sockopt
*, struct ip_moptions
*);
137 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
138 static int ip_setmoptions(struct sockopt
*, struct ip_moptions
**);
140 int ip_createmoptions(struct ip_moptions
**imop
);
141 int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
142 int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
143 int ip_optcopy(struct ip
*, struct ip
*);
144 extern int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**);
146 extern struct mbuf
* m_dup(register struct mbuf
*m
, int how
);
149 extern int apple_hwcksum_tx
;
150 extern u_long route_generation
;
152 extern struct protosw inetsw
[];
154 extern struct ip_linklocal_stat ip_linklocal_stat
;
155 extern lck_mtx_t
*ip_mutex
;
157 /* temporary: for testing */
159 extern int ipsec_bypass
;
162 static int ip_maxchainsent
= 0;
163 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxchainsent
, CTLFLAG_RW
,
164 &ip_maxchainsent
, 0, "use dlil_output_list");
166 * IP output. The packet in mbuf chain m contains a skeletal IP
167 * header (with len, off, ttl, proto, tos, src, dst).
168 * The mbuf chain containing the packet will be freed.
169 * The mbuf opt, if present, will not be freed.
177 struct ip_moptions
*imo
)
180 error
= ip_output_list(m0
, 0, opt
, ro
, flags
, imo
);
191 struct ip_moptions
*imo
)
193 struct ip
*ip
, *mhip
;
194 struct ifnet
*ifp
= NULL
;
196 int hlen
= sizeof (struct ip
);
197 int len
, off
, error
= 0;
198 struct sockaddr_in
*dst
= NULL
;
199 struct in_ifaddr
*ia
= NULL
;
200 int isbroadcast
, sw_csum
;
201 struct in_addr pkt_dst
;
203 struct route iproute
;
204 struct socket
*so
= NULL
;
205 struct secpolicy
*sp
= NULL
;
207 #if IPFIREWALL_FORWARD
208 int fwd_rewrite_src
= 0;
210 struct ip_fw_args args
;
212 ipfilter_t inject_filter_ref
= 0;
214 struct route dn_route
;
215 struct mbuf
* packetlist
;
218 lck_mtx_lock(ip_mutex
);
220 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
225 args
.next_hop
= NULL
;
226 args
.divert_rule
= 0; /* divert cookie */
228 /* Grab info from mtags prepended to the chain */
230 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
231 struct dn_pkt_tag
*dn_tag
;
233 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
234 args
.rule
= dn_tag
->rule
;
236 dn_route
= dn_tag
->ro
;
240 dst
= dn_tag
->dn_dst
;
242 flags
= dn_tag
->flags
;
244 m_tag_delete(m0
, tag
);
246 #endif /* DUMMYNET */
248 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
249 struct divert_tag
*div_tag
;
251 div_tag
= (struct divert_tag
*)(tag
+1);
252 args
.divert_rule
= div_tag
->cookie
;
254 m_tag_delete(m0
, tag
);
256 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
257 struct ip_fwd_tag
*ipfwd_tag
;
259 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
260 args
.next_hop
= ipfwd_tag
->next_hop
;
262 m_tag_delete(m0
, tag
);
268 if ( !m
|| (m
->m_flags
& M_PKTHDR
) != 0)
269 panic("ip_output no HDR");
271 panic("ip_output no route, proto = %d",
272 mtod(m
, struct ip
*)->ip_p
);
275 if (args
.rule
!= NULL
) { /* dummynet already saw us */
276 ip
= mtod(m
, struct ip
*);
277 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
278 if (ro
->ro_rt
!= NULL
)
279 ia
= (struct in_ifaddr
*)ro
->ro_rt
->rt_ifa
;
283 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
284 so
= ipsec_getsocket(m
);
285 (void)ipsec_setsocket(m
, NULL
);
292 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
293 so
= ipsec_getsocket(m
);
294 (void)ipsec_setsocket(m
, NULL
);
299 * No need to proccess packet twice if we've
302 inject_filter_ref
= ipf_get_inject_filter(m
);
305 m
= ip_insertoptions(m
, opt
, &len
);
308 ip
= mtod(m
, struct ip
*);
309 pkt_dst
= args
.next_hop
? args
.next_hop
->sin_addr
: ip
->ip_dst
;
314 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
315 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
318 ip
->ip_id
= ip_randomid();
320 ip
->ip_id
= htons(ip_id
++);
322 ipstat
.ips_localout
++;
324 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
327 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
328 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
330 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
333 * If there is a cached route,
334 * check that it is to the same destination
335 * and is still up. If not, free it and try again.
336 * The address family should also be checked in case of sharing the
341 if (ro
->ro_rt
&& (ro
->ro_rt
->generation_id
!= route_generation
) &&
342 ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0) && (ip
->ip_src
.s_addr
!= INADDR_ANY
) &&
343 (ifa_foraddr(ip
->ip_src
.s_addr
) == 0)) {
344 error
= EADDRNOTAVAIL
;
348 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
349 dst
->sin_family
!= AF_INET
||
350 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
)) {
352 ro
->ro_rt
= (struct rtentry
*)0;
354 if (ro
->ro_rt
== 0) {
355 bzero(dst
, sizeof(*dst
));
356 dst
->sin_family
= AF_INET
;
357 dst
->sin_len
= sizeof(*dst
);
358 dst
->sin_addr
= pkt_dst
;
361 * If routing to interface only,
362 * short circuit routing lookup.
364 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
365 #define sintosa(sin) ((struct sockaddr *)(sin))
366 if (flags
& IP_ROUTETOIF
) {
368 ifafree(&ia
->ia_ifa
);
369 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0) {
370 if ((ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
371 ipstat
.ips_noroute
++;
378 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
381 * If this is the case, we probably don't want to allocate
382 * a protocol-cloned route since we didn't get one from the
383 * ULP. This lets TCP do its thing, while not burdening
384 * forwarding or ICMP with the overhead of cloning a route.
385 * Of course, we still want to do any cloning requested by
386 * the link layer, as this is probably required in all cases
387 * for correct operation (as it is for ARP).
390 rtalloc_ign(ro
, RTF_PRCLONING
);
391 if (ro
->ro_rt
== 0) {
392 ipstat
.ips_noroute
++;
393 error
= EHOSTUNREACH
;
397 ifafree(&ia
->ia_ifa
);
398 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
401 ifp
= ro
->ro_rt
->rt_ifp
;
403 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
404 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
405 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
406 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
408 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
410 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
411 struct in_multi
*inm
;
413 m
->m_flags
|= M_MCAST
;
415 * IP destination address is multicast. Make sure "dst"
416 * still points to the address in "ro". (It may have been
417 * changed to point to a gateway address, above.)
419 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
421 * See if the caller provided any multicast options
424 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= imo
->imo_multicast_ttl
;
425 if (imo
->imo_multicast_ifp
!= NULL
) {
426 ifp
= imo
->imo_multicast_ifp
;
428 if (imo
->imo_multicast_vif
!= -1 &&
429 ((flags
& IP_RAWOUTPUT
) == 0 || ip
->ip_src
.s_addr
== INADDR_ANY
))
431 ip_mcast_src(imo
->imo_multicast_vif
);
433 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
435 * Confirm that the outgoing interface supports multicast.
437 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
438 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
439 ipstat
.ips_noroute
++;
445 * If source address not specified yet, use address
446 * of outgoing interface.
448 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
449 register struct in_ifaddr
*ia1
;
451 TAILQ_FOREACH(ia1
, &in_ifaddrhead
, ia_link
)
452 if (ia1
->ia_ifp
== ifp
) {
453 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
457 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
463 ifnet_lock_shared(ifp
);
464 IN_LOOKUP_MULTI(pkt_dst
, ifp
, inm
);
465 ifnet_lock_done(ifp
);
467 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
469 * If we belong to the destination multicast group
470 * on the outgoing interface, and the caller did not
471 * forbid loopback, loop back a copy.
473 if (!TAILQ_EMPTY(&ipv4_filters
)) {
474 struct ipfilter
*filter
;
475 int seen
= (inject_filter_ref
== 0);
476 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
480 ipf_pktopts
.ippo_mcast_ifnet
= imo
->imo_multicast_ifp
;
481 ipf_pktopts
.ippo_mcast_ttl
= imo
->imo_multicast_ttl
;
482 ipf_pktopts
.ippo_mcast_loop
= imo
->imo_multicast_loop
;
485 lck_mtx_unlock(ip_mutex
);
487 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
489 if ((struct ipfilter
*)inject_filter_ref
== filter
)
491 } else if (filter
->ipf_filter
.ipf_output
) {
493 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
494 if (result
== EJUSTRETURN
) {
500 lck_mtx_lock(ip_mutex
);
505 lck_mtx_lock(ip_mutex
);
509 ip_mloopback(ifp
, m
, dst
, hlen
);
513 * If we are acting as a multicast router, perform
514 * multicast forwarding as if the packet had just
515 * arrived on the interface to which we are about
516 * to send. The multicast forwarding function
517 * recursively calls this function, using the
518 * IP_FORWARDING flag to prevent infinite recursion.
520 * Multicasts that are looped back by ip_mloopback(),
521 * above, will be forwarded by the ip_input() routine,
524 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
526 * Check if rsvp daemon is running. If not, don't
527 * set ip_moptions. This ensures that the packet
528 * is multicast and not just sent down one link
529 * as prescribed by rsvpd.
533 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
535 lck_mtx_unlock(ip_mutex
);
542 * Multicasts with a time-to-live of zero may be looped-
543 * back, above, but must not be transmitted on a network.
544 * Also, multicasts addressed to the loopback interface
545 * are not sent -- the above call to ip_mloopback() will
546 * loop back a copy if this host actually belongs to the
547 * destination group on the loopback interface.
549 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
551 lck_mtx_unlock(ip_mutex
);
559 * If source address not specified yet, use address
560 * of outgoing interface.
562 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
563 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
564 #if IPFIREWALL_FORWARD
565 /* Keep note that we did this - if the firewall changes
566 * the next-hop, our interface may change, changing the
567 * default source IP. It's a shame so much effort happens
571 #endif /* IPFIREWALL_FORWARD */
576 * Look for broadcast address and
577 * and verify user is allowed to send
581 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
582 error
= EADDRNOTAVAIL
;
585 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
589 /* don't allow broadcast messages to be fragmented */
590 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
594 m
->m_flags
|= M_BCAST
;
596 m
->m_flags
&= ~M_BCAST
;
601 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
603 if (IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) || IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))) {
604 ip_linklocal_stat
.iplls_out_total
++;
605 if (ip
->ip_ttl
!= MAXTTL
) {
606 ip_linklocal_stat
.iplls_out_badttl
++;
612 if (!didfilter
&& !TAILQ_EMPTY(&ipv4_filters
)) {
613 struct ipfilter
*filter
;
614 int seen
= (inject_filter_ref
== 0);
616 lck_mtx_unlock(ip_mutex
);
618 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
620 if ((struct ipfilter
*)inject_filter_ref
== filter
)
622 } else if (filter
->ipf_filter
.ipf_output
) {
624 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
625 if (result
== EJUSTRETURN
) {
631 lck_mtx_lock(ip_mutex
);
637 lck_mtx_lock(ip_mutex
);
641 /* temporary for testing only: bypass ipsec alltogether */
643 if (ipsec_bypass
!= 0 || (flags
& IP_NOIPSEC
) != 0)
646 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
648 lck_mtx_lock(sadb_mutex
);
650 /* get SP for this packet */
652 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
654 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
657 ipsecstat
.out_inval
++;
658 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
659 lck_mtx_unlock(sadb_mutex
);
666 switch (sp
->policy
) {
667 case IPSEC_POLICY_DISCARD
:
669 * This packet is just discarded.
671 ipsecstat
.out_polvio
++;
672 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 1,0,0,0,0);
673 lck_mtx_unlock(sadb_mutex
);
676 case IPSEC_POLICY_BYPASS
:
677 case IPSEC_POLICY_NONE
:
678 /* no need to do IPsec. */
679 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 2,0,0,0,0);
680 lck_mtx_unlock(sadb_mutex
);
683 case IPSEC_POLICY_IPSEC
:
684 if (sp
->req
== NULL
) {
685 /* acquire a policy */
686 error
= key_spdacquire(sp
);
687 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 3,0,0,0,0);
688 lck_mtx_unlock(sadb_mutex
);
693 case IPSEC_POLICY_ENTRUST
:
695 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
698 struct ipsec_output_state state
;
699 bzero(&state
, sizeof(state
));
701 if (flags
& IP_ROUTETOIF
) {
703 bzero(&iproute
, sizeof(iproute
));
706 state
.dst
= (struct sockaddr
*)dst
;
712 * delayed checksums are not currently compatible with IPsec
714 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
716 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
722 lck_mtx_unlock(ip_mutex
);
723 error
= ipsec4_output(&state
, sp
, flags
);
724 lck_mtx_unlock(sadb_mutex
);
725 lck_mtx_lock(ip_mutex
);
729 if (flags
& IP_ROUTETOIF
) {
731 * if we have tunnel mode SA, we may need to ignore
734 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
735 flags
&= ~IP_ROUTETOIF
;
741 dst
= (struct sockaddr_in
*)state
.dst
;
743 /* mbuf is already reclaimed in ipsec4_output. */
753 printf("ip4_output (ipsec): error code %d\n", error
);
756 /* don't show these error codes to the user */
760 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 4,0,0,0,0);
765 /* be sure to update variables that are affected by ipsec4_output() */
766 ip
= mtod(m
, struct ip
*);
769 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
771 hlen
= ip
->ip_hl
<< 2;
773 /* Check that there wasn't a route change and src is still valid */
775 if (ro
->ro_rt
->generation_id
!= route_generation
) {
776 if (ifa_foraddr(ip
->ip_src
.s_addr
) == 0 && ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0)) {
777 error
= EADDRNOTAVAIL
;
778 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 5,0,0,0,0);
785 if (ro
->ro_rt
== NULL
) {
786 if ((flags
& IP_ROUTETOIF
) == 0) {
788 "can't update route after IPsec processing\n");
789 error
= EHOSTUNREACH
; /*XXX*/
790 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 6,0,0,0,0);
795 ifafree(&ia
->ia_ifa
);
796 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
799 ifp
= ro
->ro_rt
->rt_ifp
;
802 /* make it flipped, again. */
805 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 7,0xff,0xff,0xff,0xff);
807 /* Pass to filters again */
808 if (!TAILQ_EMPTY(&ipv4_filters
)) {
809 struct ipfilter
*filter
;
811 lck_mtx_unlock(ip_mutex
);
813 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
814 if (filter
->ipf_filter
.ipf_output
) {
816 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
817 if (result
== EJUSTRETURN
) {
823 lck_mtx_lock(ip_mutex
);
829 lck_mtx_lock(ip_mutex
);
836 * - Xlate: translate packet's addr/port (NAT).
837 * - Firewall: deny/allow/etc.
838 * - Wrap: fake packet's addr/port <unimpl.>
839 * - Encapsulate: put it in another IP and send out. <unimp.>
844 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
) {
845 lck_mtx_unlock(ip_mutex
);
848 ip
= mtod(m0
= m
= m1
, struct ip
*);
852 * Check with the firewall...
853 * but not if we are already being fwd'd from a firewall.
855 if (fw_enable
&& IPFW_LOADED
&& !args
.next_hop
) {
856 struct sockaddr_in
*old
= dst
;
861 lck_mtx_unlock(ip_mutex
);
862 off
= ip_fw_chk_ptr(&args
);
867 * On return we must do the following:
868 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
869 * 1<=off<= 0xffff -> DIVERT
870 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
871 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
872 * dst != old -> IPFIREWALL_FORWARD
873 * off==0, dst==old -> accept
874 * If some of the above modules is not compiled in, then
875 * we should't have to check the corresponding condition
876 * (because the ipfw control socket should not accept
877 * unsupported rules), but better play safe and drop
878 * packets in case of doubt.
881 if ( (off
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) {
887 ip
= mtod(m
, struct ip
*);
888 if (off
== 0 && dst
== old
) {/* common case */
889 lck_mtx_lock(ip_mutex
);
893 if (DUMMYNET_LOADED
&& (off
& IP_FW_PORT_DYNT_FLAG
) != 0) {
895 * pass the pkt to dummynet. Need to include
896 * pipe number, m, ifp, ro, dst because these are
897 * not recomputed in the next pass.
898 * All other parameters have been already used and
899 * so they are not needed anymore.
900 * XXX note: if the ifp or ro entry are deleted
901 * while a pkt is in dummynet, we are in trouble!
907 error
= ip_dn_io_ptr(m
, off
& 0xffff, DN_TO_IP_OUT
,
911 #endif /* DUMMYNET */
912 lck_mtx_lock(ip_mutex
);
914 if (off
!= 0 && (off
& IP_FW_PORT_DYNT_FLAG
) == 0) {
915 struct mbuf
*clone
= NULL
;
917 /* Clone packet if we're doing a 'tee' */
918 if ((off
& IP_FW_PORT_TEE_FLAG
) != 0)
919 clone
= m_dup(m
, M_DONTWAIT
);
922 * delayed checksums are not currently compatible
923 * with divert sockets.
925 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
927 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
930 /* Restore packet header fields to original values */
934 /* Deliver packet to divert input routine */
935 divert_packet(m
, 0, off
& 0xffff, args
.divert_rule
);
937 /* If 'tee', continue with original packet */
940 ip
= mtod(m
, struct ip
*);
943 lck_mtx_unlock(ip_mutex
);
948 #if IPFIREWALL_FORWARD
949 /* Here we check dst to make sure it's directly reachable on the
950 * interface we previously thought it was.
951 * If it isn't (which may be likely in some situations) we have
952 * to re-route it (ie, find a route for the next-hop and the
953 * associated interface) and set them here. This is nested
954 * forwarding which in most cases is undesirable, except where
955 * such control is nigh impossible. So we do it here.
958 if (off
== 0 && old
!= dst
) {
959 struct in_ifaddr
*ia_fw
;
961 /* It's changed... */
962 /* There must be a better way to do this next line... */
963 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
964 #if IPFIREWALL_FORWARD_DEBUG
965 printf("IPFIREWALL_FORWARD: New dst ip: ");
966 print_ip(dst
->sin_addr
);
970 * We need to figure out if we have been forwarded
971 * to a local socket. If so then we should somehow
972 * "loop back" to ip_input, and get directed to the
973 * PCB as if we had received this packet. This is
974 * because it may be dificult to identify the packets
975 * you want to forward until they are being output
976 * and have selected an interface. (e.g. locally
977 * initiated packets) If we used the loopback inteface,
978 * we would not be able to control what happens
979 * as the packet runs through ip_input() as
980 * it is done through a ISR.
982 TAILQ_FOREACH(ia_fw
, &in_ifaddrhead
, ia_link
) {
984 * If the addr to forward to is one
985 * of ours, we pretend to
986 * be the destination for this packet.
988 if (IA_SIN(ia_fw
)->sin_addr
.s_addr
==
989 dst
->sin_addr
.s_addr
)
993 /* tell ip_input "dont filter" */
994 struct m_tag
*fwd_tag
;
995 struct ip_fwd_tag
*ipfwd_tag
;
997 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
998 sizeof(struct sockaddr_in
), M_NOWAIT
);
999 if (fwd_tag
== NULL
) {
1004 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1005 ipfwd_tag
->next_hop
= args
.next_hop
;
1007 m_tag_prepend(m
, fwd_tag
);
1009 if (m
->m_pkthdr
.rcvif
== NULL
)
1010 m
->m_pkthdr
.rcvif
= ifunit("lo0");
1011 if ((~IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) &
1012 m
->m_pkthdr
.csum_flags
) == 0) {
1013 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1014 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1015 m
->m_pkthdr
.csum_flags
|=
1016 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1017 m
->m_pkthdr
.csum_data
= 0xffff;
1019 m
->m_pkthdr
.csum_flags
|=
1020 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1022 else if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1023 in_delayed_cksum(m
);
1024 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1025 ip
->ip_sum
= in_cksum(m
, hlen
);
1030 lck_mtx_unlock(ip_mutex
);
1032 /* we need to call dlil_output to run filters
1033 * and resync to avoid recursion loops.
1036 dlil_output(lo_ifp
, PF_INET
, m
, 0, (struct sockaddr
*)dst
, 0);
1039 printf("ip_output: no loopback ifp for forwarding!!!\n");
1043 /* Some of the logic for this was
1044 * nicked from above.
1046 * This rewrites the cached route in a local PCB.
1047 * Is this what we want to do?
1049 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
1052 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
1054 if (ro_fwd
->ro_rt
== 0) {
1055 ipstat
.ips_noroute
++;
1056 error
= EHOSTUNREACH
;
1060 ia_fw
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
1061 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
1062 ro_fwd
->ro_rt
->rt_use
++;
1063 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
1064 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
1065 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
1067 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
1069 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
1071 ro
->ro_rt
= ro_fwd
->ro_rt
;
1072 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
1075 * If we added a default src ip earlier,
1076 * which would have been gotten from the-then
1077 * interface, do it again, from the new one.
1079 if (fwd_rewrite_src
)
1080 ip
->ip_src
= IA_SIN(ia_fw
)->sin_addr
;
1083 #endif /* IPFIREWALL_FORWARD */
1085 * if we get here, none of the above matches, and
1086 * we have to drop the pkt
1089 error
= EACCES
; /* not sure this is the right error msg */
1090 lck_mtx_unlock(ip_mutex
);
1096 /* Do not allow loopback address to wind up on a wire */
1097 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
1098 ((ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1099 (ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)) {
1100 ipstat
.ips_badaddr
++;
1103 * Do not simply drop the packet just like a firewall -- we want the
1104 * the application to feel the pain.
1105 * Return ENETUNREACH like ip6_output does in some similar cases.
1106 * This can startle the otherwise clueless process that specifies
1107 * loopback as the source address.
1109 error
= ENETUNREACH
;
1110 lck_mtx_unlock(ip_mutex
);
1114 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
1115 sw_csum
= m
->m_pkthdr
.csum_flags
1116 & ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1118 if ((ifp
->if_hwassist
& CSUM_TCP_SUM16
) != 0) {
1120 * Special case code for GMACE
1121 * frames that can be checksumed by GMACE SUM16 HW:
1122 * frame >64, no fragments, no UDP
1124 if (apple_hwcksum_tx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP
)
1125 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)) {
1126 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
1127 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
1128 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
1129 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
1130 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
1131 m
->m_pkthdr
.csum_data
+= offset
;
1132 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
1135 /* let the software handle any UDP or TCP checksums */
1136 sw_csum
|= (CSUM_DELAY_DATA
& m
->m_pkthdr
.csum_flags
);
1140 if (sw_csum
& CSUM_DELAY_DATA
) {
1141 in_delayed_cksum(m
);
1142 sw_csum
&= ~CSUM_DELAY_DATA
;
1143 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1146 m
->m_pkthdr
.csum_flags
&= IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1149 * If small enough for interface, or the interface will take
1150 * care of the fragmentation for us, can just send directly.
1152 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
||
1153 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
1157 if (sw_csum
& CSUM_DELAY_IP
) {
1158 ip
->ip_sum
= in_cksum(m
, hlen
);
1162 /* Record statistics for this interface address. */
1163 if (!(flags
& IP_FORWARDING
) && ia
!= NULL
) {
1164 ia
->ia_ifa
.if_opackets
++;
1165 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1170 /* clean ipsec history once it goes out of the node */
1171 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1174 if (packetchain
== 0) {
1175 lck_mtx_unlock(ip_mutex
);
1176 error
= dlil_output(ifp
, PF_INET
, m
, (void *) ro
->ro_rt
,
1177 (struct sockaddr
*)dst
, 0);
1180 else { /* packet chaining allows us to reuse the route for all packets */
1183 if (pktcnt
> ip_maxchainsent
)
1184 ip_maxchainsent
= pktcnt
;
1186 lck_mtx_unlock(ip_mutex
);
1187 error
= dlil_output_list(ifp
, PF_INET
, packetlist
, (void *) ro
->ro_rt
,
1188 (struct sockaddr
*)dst
, 0);
1199 * Too large for interface; fragment if possible.
1200 * Must be able to put at least 8 bytes per fragment.
1202 if (ip
->ip_off
& IP_DF
) {
1205 * This case can happen if the user changed the MTU
1206 * of an interface after enabling IP on it. Because
1207 * most netifs don't keep track of routes pointing to
1208 * them, there is no way for one to update all its
1209 * routes when the MTU is changed.
1211 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
1212 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
1213 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
1214 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
1216 ipstat
.ips_cantfrag
++;
1219 len
= (ifp
->if_mtu
- hlen
) &~ 7;
1226 * if the interface will not calculate checksums on
1227 * fragmented packets, then do it here.
1229 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
1230 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
1231 in_delayed_cksum(m
);
1233 lck_mtx_unlock(ip_mutex
);
1236 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1241 int mhlen
, firstlen
= len
;
1242 struct mbuf
**mnext
= &m
->m_nextpkt
;
1246 * Loop through length of segment after first fragment,
1247 * make new header and copy data of each part and link onto chain.
1250 mhlen
= sizeof (struct ip
);
1251 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
1252 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1255 ipstat
.ips_odropped
++;
1258 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
1259 m
->m_data
+= max_linkhdr
;
1260 mhip
= mtod(m
, struct ip
*);
1262 if (hlen
> sizeof (struct ip
)) {
1263 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
1264 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
1267 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
1268 if (ip
->ip_off
& IP_MF
)
1269 mhip
->ip_off
|= IP_MF
;
1270 if (off
+ len
>= (u_short
)ip
->ip_len
)
1271 len
= (u_short
)ip
->ip_len
- off
;
1273 mhip
->ip_off
|= IP_MF
;
1274 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
1275 m
->m_next
= m_copy(m0
, off
, len
);
1276 if (m
->m_next
== 0) {
1278 error
= ENOBUFS
; /* ??? */
1279 ipstat
.ips_odropped
++;
1282 m
->m_pkthdr
.len
= mhlen
+ len
;
1283 m
->m_pkthdr
.rcvif
= 0;
1284 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
1285 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1286 HTONS(mhip
->ip_off
);
1288 if (sw_csum
& CSUM_DELAY_IP
) {
1289 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1292 mnext
= &m
->m_nextpkt
;
1295 ipstat
.ips_ofragments
+= nfrags
;
1297 /* set first/last markers for fragment chain */
1298 m
->m_flags
|= M_LASTFRAG
;
1299 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1300 m0
->m_pkthdr
.csum_data
= nfrags
;
1303 * Update first fragment by trimming what's been copied out
1304 * and updating header, then send each fragment (in order).
1307 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
1308 m
->m_pkthdr
.len
= hlen
+ firstlen
;
1309 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
1310 ip
->ip_off
|= IP_MF
;
1313 if (sw_csum
& CSUM_DELAY_IP
) {
1314 ip
->ip_sum
= in_cksum(m
, hlen
);
1318 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1319 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1321 lck_mtx_unlock(ip_mutex
);
1322 for (m
= m0
; m
; m
= m0
) {
1326 /* clean ipsec history once it goes out of the node */
1327 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1332 /* Record statistics for this interface address. */
1334 ia
->ia_ifa
.if_opackets
++;
1335 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1338 if ((packetchain
!= 0) && (pktcnt
> 0))
1339 panic("ip_output: mix of packet in packetlist is wrong=%x", packetlist
);
1340 error
= dlil_output(ifp
, PF_INET
, m
, (void *) ro
->ro_rt
,
1341 (struct sockaddr
*)dst
, 0);
1347 ipstat
.ips_fragmented
++;
1351 ifafree(&ia
->ia_ifa
);
1355 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
1356 if (ro
== &iproute
&& ro
->ro_rt
) {
1361 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1362 printf("DP ip_output call free SP:%x\n", sp
));
1363 lck_mtx_lock(sadb_mutex
);
1365 lck_mtx_unlock(sadb_mutex
);
1370 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1374 lck_mtx_unlock(ip_mutex
);
1379 in_delayed_cksum_offset(struct mbuf
*m
, int ip_offset
)
1382 u_short csum
, offset
;
1384 while (ip_offset
> m
->m_len
) {
1385 ip_offset
-= m
->m_len
;
1388 printf("in_delayed_cksum_withoffset failed - ip_offset wasn't in the packet\n");
1393 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1394 printf("delayed m_pullup, m->len: %d off: %d\n",
1395 m
->m_len
, ip_offset
);
1398 * this shouldn't happen
1400 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
1405 m
->m_len
-= ip_offset
;
1406 m
->m_data
+= ip_offset
;
1409 ip
= mtod(m
, struct ip
*);
1410 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1411 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1412 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1414 offset
+= m
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1418 if (M_LEADINGSPACE(m
) < ip_offset
)
1419 panic("in_delayed_cksum_withoffset - chain modified!\n");
1420 m
->m_len
+= ip_offset
;
1421 m
->m_data
-= ip_offset
;
1424 if (offset
> ip
->ip_len
) /* bogus offset */
1427 if (offset
+ ip_offset
+ sizeof(u_short
) > m
->m_len
) {
1428 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1429 m
->m_len
, offset
+ ip_offset
, ip
->ip_p
);
1432 * this shouldn't happen, but if it does, the
1433 * correct behavior may be to insert the checksum
1434 * in the existing chain instead of rearranging it.
1436 m
= m_pullup(m
, offset
+ ip_offset
+ sizeof(u_short
));
1438 *(u_short
*)(m
->m_data
+ offset
+ ip_offset
) = csum
;
1442 in_delayed_cksum(struct mbuf
*m
)
1444 in_delayed_cksum_offset(m
, 0);
1448 in_cksum_offset(struct mbuf
* m
, size_t ip_offset
)
1450 struct ip
* ip
= NULL
;
1453 while (ip_offset
> m
->m_len
) {
1454 ip_offset
-= m
->m_len
;
1457 printf("in_cksum_offset failed - ip_offset wasn't in the packet\n");
1462 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1463 printf("in_cksum_offset - delayed m_pullup, m->len: %d off: %d\n",
1464 m
->m_len
, ip_offset
);
1467 * this shouldn't happen
1469 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
1474 m
->m_len
-= ip_offset
;
1475 m
->m_data
+= ip_offset
;
1478 ip
= mtod(m
, struct ip
*);
1481 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1483 hlen
= ip
->ip_hl
<< 2;
1487 ip
->ip_sum
= in_cksum(m
, hlen
);
1491 if (M_LEADINGSPACE(m
) < ip_offset
)
1492 panic("in_cksum_offset - chain modified!\n");
1493 m
->m_len
+= ip_offset
;
1494 m
->m_data
-= ip_offset
;
1499 * Insert IP options into preformed packet.
1500 * Adjust IP destination as required for IP source routing,
1501 * as indicated by a non-zero in_addr at the start of the options.
1503 * XXX This routine assumes that the packet has no options in place.
1505 static struct mbuf
*
1506 ip_insertoptions(m
, opt
, phlen
)
1507 register struct mbuf
*m
;
1511 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1513 register struct ip
*ip
= mtod(m
, struct ip
*);
1516 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1517 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1518 return (m
); /* XXX should fail */
1519 if (p
->ipopt_dst
.s_addr
)
1520 ip
->ip_dst
= p
->ipopt_dst
;
1521 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1522 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
1525 n
->m_pkthdr
.rcvif
= 0;
1526 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1527 m
->m_len
-= sizeof(struct ip
);
1528 m
->m_data
+= sizeof(struct ip
);
1531 m
->m_len
= optlen
+ sizeof(struct ip
);
1532 m
->m_data
+= max_linkhdr
;
1533 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1535 m
->m_data
-= optlen
;
1537 m
->m_pkthdr
.len
+= optlen
;
1538 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1540 ip
= mtod(m
, struct ip
*);
1541 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1542 *phlen
= sizeof(struct ip
) + optlen
;
1543 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1544 ip
->ip_len
+= optlen
;
1549 * Copy options from ip to jp,
1550 * omitting those not copied during fragmentation.
1556 register u_char
*cp
, *dp
;
1557 int opt
, optlen
, cnt
;
1559 cp
= (u_char
*)(ip
+ 1);
1560 dp
= (u_char
*)(jp
+ 1);
1561 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1562 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1564 if (opt
== IPOPT_EOL
)
1566 if (opt
== IPOPT_NOP
) {
1567 /* Preserve for IP mcast tunnel's LSRR alignment. */
1573 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1574 panic("malformed IPv4 option passed to ip_optcopy");
1576 optlen
= cp
[IPOPT_OLEN
];
1578 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1579 panic("malformed IPv4 option passed to ip_optcopy");
1581 /* bogus lengths should have been caught by ip_dooptions */
1584 if (IPOPT_COPIED(opt
)) {
1585 bcopy(cp
, dp
, optlen
);
1589 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1595 * IP socket option processing.
1598 ip_ctloutput(so
, sopt
)
1600 struct sockopt
*sopt
;
1602 struct inpcb
*inp
= sotoinpcb(so
);
1606 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1610 switch (sopt
->sopt_dir
) {
1612 switch (sopt
->sopt_name
) {
1619 if (sopt
->sopt_valsize
> MLEN
) {
1623 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1628 m
->m_len
= sopt
->sopt_valsize
;
1629 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1634 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1641 case IP_RECVRETOPTS
:
1642 case IP_RECVDSTADDR
:
1645 #if defined(NFAITH) && NFAITH > 0
1648 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1653 switch (sopt
->sopt_name
) {
1655 inp
->inp_ip_tos
= optval
;
1659 inp
->inp_ip_ttl
= optval
;
1661 #define OPTSET(bit) \
1663 inp->inp_flags |= bit; \
1665 inp->inp_flags &= ~bit;
1668 OPTSET(INP_RECVOPTS
);
1671 case IP_RECVRETOPTS
:
1672 OPTSET(INP_RECVRETOPTS
);
1675 case IP_RECVDSTADDR
:
1676 OPTSET(INP_RECVDSTADDR
);
1684 OPTSET(INP_RECVTTL
);
1687 #if defined(NFAITH) && NFAITH > 0
1696 case IP_MULTICAST_IF
:
1697 case IP_MULTICAST_VIF
:
1698 case IP_MULTICAST_TTL
:
1699 case IP_MULTICAST_LOOP
:
1700 case IP_ADD_MEMBERSHIP
:
1701 case IP_DROP_MEMBERSHIP
:
1702 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1706 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1712 case IP_PORTRANGE_DEFAULT
:
1713 inp
->inp_flags
&= ~(INP_LOWPORT
);
1714 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1717 case IP_PORTRANGE_HIGH
:
1718 inp
->inp_flags
&= ~(INP_LOWPORT
);
1719 inp
->inp_flags
|= INP_HIGHPORT
;
1722 case IP_PORTRANGE_LOW
:
1723 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1724 inp
->inp_flags
|= INP_LOWPORT
;
1734 case IP_IPSEC_POLICY
:
1742 if (sopt
->sopt_valsize
> MCLBYTES
) {
1746 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1748 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1750 priv
= (sopt
->sopt_p
!= NULL
&&
1751 proc_suser(sopt
->sopt_p
) != 0) ? 0 : 1;
1753 req
= mtod(m
, caddr_t
);
1756 optname
= sopt
->sopt_name
;
1757 lck_mtx_lock(sadb_mutex
);
1758 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1759 lck_mtx_unlock(sadb_mutex
);
1766 error
= ENOPROTOOPT
;
1772 switch (sopt
->sopt_name
) {
1775 if (inp
->inp_options
)
1776 error
= sooptcopyout(sopt
,
1777 mtod(inp
->inp_options
,
1779 inp
->inp_options
->m_len
);
1781 sopt
->sopt_valsize
= 0;
1787 case IP_RECVRETOPTS
:
1788 case IP_RECVDSTADDR
:
1792 #if defined(NFAITH) && NFAITH > 0
1795 switch (sopt
->sopt_name
) {
1798 optval
= inp
->inp_ip_tos
;
1802 optval
= inp
->inp_ip_ttl
;
1805 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1808 optval
= OPTBIT(INP_RECVOPTS
);
1811 case IP_RECVRETOPTS
:
1812 optval
= OPTBIT(INP_RECVRETOPTS
);
1815 case IP_RECVDSTADDR
:
1816 optval
= OPTBIT(INP_RECVDSTADDR
);
1820 optval
= OPTBIT(INP_RECVIF
);
1824 optval
= OPTBIT(INP_RECVTTL
);
1828 if (inp
->inp_flags
& INP_HIGHPORT
)
1829 optval
= IP_PORTRANGE_HIGH
;
1830 else if (inp
->inp_flags
& INP_LOWPORT
)
1831 optval
= IP_PORTRANGE_LOW
;
1836 #if defined(NFAITH) && NFAITH > 0
1838 optval
= OPTBIT(INP_FAITH
);
1842 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1845 case IP_MULTICAST_IF
:
1846 case IP_MULTICAST_VIF
:
1847 case IP_MULTICAST_TTL
:
1848 case IP_MULTICAST_LOOP
:
1849 case IP_ADD_MEMBERSHIP
:
1850 case IP_DROP_MEMBERSHIP
:
1851 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1855 case IP_IPSEC_POLICY
:
1857 struct mbuf
*m
= NULL
;
1862 req
= mtod(m
, caddr_t
);
1865 lck_mtx_lock(sadb_mutex
);
1866 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
1867 lck_mtx_unlock(sadb_mutex
);
1869 error
= soopt_mcopyout(sopt
, m
); /* XXX */
1877 error
= ENOPROTOOPT
;
1886 * Set up IP options in pcb for insertion in output packets.
1887 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1888 * with destination address if source routed.
1891 ip_pcbopts(optname
, pcbopt
, m
)
1893 struct mbuf
**pcbopt
;
1894 register struct mbuf
*m
;
1896 register int cnt
, optlen
;
1897 register u_char
*cp
;
1900 /* turn off any old options */
1902 (void)m_free(*pcbopt
);
1904 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
1906 * Only turning off any previous options.
1914 if (m
->m_len
% sizeof(int32_t))
1918 * IP first-hop destination address will be stored before
1919 * actual options; move other options back
1920 * and clear it when none present.
1922 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1925 m
->m_len
+= sizeof(struct in_addr
);
1926 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1927 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
1928 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1930 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1931 opt
= cp
[IPOPT_OPTVAL
];
1932 if (opt
== IPOPT_EOL
)
1934 if (opt
== IPOPT_NOP
)
1937 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1939 optlen
= cp
[IPOPT_OLEN
];
1940 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1951 * user process specifies route as:
1953 * D must be our final destination (but we can't
1954 * check that since we may not have connected yet).
1955 * A is first hop destination, which doesn't appear in
1956 * actual IP option, but is stored before the options.
1958 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1960 m
->m_len
-= sizeof(struct in_addr
);
1961 cnt
-= sizeof(struct in_addr
);
1962 optlen
-= sizeof(struct in_addr
);
1963 cp
[IPOPT_OLEN
] = optlen
;
1965 * Move first hop before start of options.
1967 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1968 sizeof(struct in_addr
));
1970 * Then copy rest of options back
1971 * to close up the deleted entry.
1973 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
1974 sizeof(struct in_addr
)),
1975 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
1976 (unsigned)cnt
+ sizeof(struct in_addr
));
1980 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1992 * The whole multicast option thing needs to be re-thought.
1993 * Several of these options are equally applicable to non-multicast
1994 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1995 * standard option (IP_TTL).
1999 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
2001 static struct ifnet
*
2002 ip_multicast_if(a
, ifindexp
)
2011 if (ntohl(a
->s_addr
) >> 24 == 0) {
2012 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
2013 ifnet_head_lock_shared();
2014 if (ifindex
< 0 || if_index
< ifindex
) {
2018 ifp
= ifindex2ifnet
[ifindex
];
2021 *ifindexp
= ifindex
;
2023 INADDR_TO_IFP(*a
, ifp
);
2029 * Set the IP multicast options in response to user setsockopt().
2032 ip_setmoptions(sopt
, imop
)
2033 struct sockopt
*sopt
;
2034 struct ip_moptions
**imop
;
2038 struct in_addr addr
;
2039 struct ip_mreq mreq
;
2040 struct ifnet
*ifp
= NULL
;
2041 struct ip_moptions
*imo
= *imop
;
2046 * No multicast option buffer attached to the pcb;
2047 * allocate one and initialize to default values.
2049 error
= ip_createmoptions(imop
);
2055 switch (sopt
->sopt_name
) {
2056 /* store an index number for the vif you wanna use in the send */
2057 case IP_MULTICAST_VIF
:
2058 if (legal_vif_num
== 0) {
2062 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2065 if (!legal_vif_num(i
) && (i
!= -1)) {
2069 imo
->imo_multicast_vif
= i
;
2072 case IP_MULTICAST_IF
:
2074 * Select the interface for outgoing multicast packets.
2076 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
2080 * INADDR_ANY is used to remove a previous selection.
2081 * When no interface is selected, a default one is
2082 * chosen every time a multicast packet is sent.
2084 if (addr
.s_addr
== INADDR_ANY
) {
2085 imo
->imo_multicast_ifp
= NULL
;
2089 * The selected interface is identified by its local
2090 * IP address. Find the interface and confirm that
2091 * it supports multicasting.
2093 ifp
= ip_multicast_if(&addr
, &ifindex
);
2094 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2095 error
= EADDRNOTAVAIL
;
2098 imo
->imo_multicast_ifp
= ifp
;
2100 imo
->imo_multicast_addr
= addr
;
2102 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2105 case IP_MULTICAST_TTL
:
2107 * Set the IP time-to-live for outgoing multicast packets.
2108 * The original multicast API required a char argument,
2109 * which is inconsistent with the rest of the socket API.
2110 * We allow either a char or an int.
2112 if (sopt
->sopt_valsize
== 1) {
2114 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
2117 imo
->imo_multicast_ttl
= ttl
;
2120 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
2127 imo
->imo_multicast_ttl
= ttl
;
2131 case IP_MULTICAST_LOOP
:
2133 * Set the loopback flag for outgoing multicast packets.
2134 * Must be zero or one. The original multicast API required a
2135 * char argument, which is inconsistent with the rest
2136 * of the socket API. We allow either a char or an int.
2138 if (sopt
->sopt_valsize
== 1) {
2140 error
= sooptcopyin(sopt
, &loop
, 1, 1);
2143 imo
->imo_multicast_loop
= !!loop
;
2146 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
2150 imo
->imo_multicast_loop
= !!loop
;
2154 case IP_ADD_MEMBERSHIP
:
2156 * Add a multicast group membership.
2157 * Group must be a valid IP multicast address.
2159 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2163 error
= ip_addmembership(imo
, &mreq
);
2166 case IP_DROP_MEMBERSHIP
:
2168 * Drop a multicast group membership.
2169 * Group must be a valid IP multicast address.
2171 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2175 error
= ip_dropmembership(imo
, &mreq
);
2184 * If all options have default values, no need to keep the mbuf.
2186 if (imo
->imo_multicast_ifp
== NULL
&&
2187 imo
->imo_multicast_vif
== -1 &&
2188 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2189 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2190 imo
->imo_num_memberships
== 0) {
2191 FREE(*imop
, M_IPMOPTS
);
2199 * Set the IP multicast options in response to user setsockopt().
2201 __private_extern__
int
2203 struct ip_moptions
**imop
)
2205 struct ip_moptions
*imo
;
2206 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
2212 imo
->imo_multicast_ifp
= NULL
;
2213 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2214 imo
->imo_multicast_vif
= -1;
2215 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2216 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
2217 imo
->imo_num_memberships
= 0;
2223 * Add membership to an IPv4 multicast.
2225 __private_extern__
int
2227 struct ip_moptions
*imo
,
2228 struct ip_mreq
*mreq
)
2231 struct sockaddr_in
*dst
;
2232 struct ifnet
*ifp
= NULL
;
2236 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2241 * If no interface address was provided, use the interface of
2242 * the route to the given multicast address.
2244 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
2245 bzero((caddr_t
)&ro
, sizeof(ro
));
2246 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
2247 dst
->sin_len
= sizeof(*dst
);
2248 dst
->sin_family
= AF_INET
;
2249 dst
->sin_addr
= mreq
->imr_multiaddr
;
2251 if (ro
.ro_rt
!= NULL
) {
2252 ifp
= ro
.ro_rt
->rt_ifp
;
2256 /* If there's no default route, try using loopback */
2257 mreq
->imr_interface
.s_addr
= INADDR_LOOPBACK
;
2262 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2266 * See if we found an interface, and confirm that it
2267 * supports multicast.
2269 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2270 error
= EADDRNOTAVAIL
;
2274 * See if the membership already exists or if all the
2275 * membership slots are full.
2277 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2278 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
2279 imo
->imo_membership
[i
]->inm_addr
.s_addr
2280 == mreq
->imr_multiaddr
.s_addr
)
2283 if (i
< imo
->imo_num_memberships
) {
2287 if (i
== IP_MAX_MEMBERSHIPS
) {
2288 error
= ETOOMANYREFS
;
2292 * Everything looks good; add a new record to the multicast
2293 * address list for the given interface.
2295 if ((imo
->imo_membership
[i
] =
2296 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
2300 ++imo
->imo_num_memberships
;
2306 * Drop membership of an IPv4 multicast.
2308 __private_extern__
int
2310 struct ip_moptions
*imo
,
2311 struct ip_mreq
*mreq
)
2314 struct ifnet
* ifp
= NULL
;
2317 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2323 * If an interface address was specified, get a pointer
2324 * to its ifnet structure.
2326 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
2329 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2331 error
= EADDRNOTAVAIL
;
2336 * Find the membership in the membership array.
2338 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2340 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2341 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2342 mreq
->imr_multiaddr
.s_addr
)
2345 if (i
== imo
->imo_num_memberships
) {
2346 error
= EADDRNOTAVAIL
;
2350 * Give up the multicast address record to which the
2351 * membership points.
2353 in_delmulti(&imo
->imo_membership
[i
]);
2355 * Remove the gap in the membership array.
2357 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2358 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2359 --imo
->imo_num_memberships
;
2365 * Return the IP multicast options in response to user getsockopt().
2368 ip_getmoptions(sopt
, imo
)
2369 struct sockopt
*sopt
;
2370 register struct ip_moptions
*imo
;
2372 struct in_addr addr
;
2373 struct in_ifaddr
*ia
;
2378 switch (sopt
->sopt_name
) {
2379 case IP_MULTICAST_VIF
:
2381 optval
= imo
->imo_multicast_vif
;
2384 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2387 case IP_MULTICAST_IF
:
2388 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2389 addr
.s_addr
= INADDR_ANY
;
2390 else if (imo
->imo_multicast_addr
.s_addr
) {
2391 /* return the value user has set */
2392 addr
= imo
->imo_multicast_addr
;
2394 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
2395 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2396 : IA_SIN(ia
)->sin_addr
.s_addr
;
2398 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
2401 case IP_MULTICAST_TTL
:
2403 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
2405 optval
= coptval
= imo
->imo_multicast_ttl
;
2406 if (sopt
->sopt_valsize
== 1)
2407 error
= sooptcopyout(sopt
, &coptval
, 1);
2409 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2412 case IP_MULTICAST_LOOP
:
2414 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
2416 optval
= coptval
= imo
->imo_multicast_loop
;
2417 if (sopt
->sopt_valsize
== 1)
2418 error
= sooptcopyout(sopt
, &coptval
, 1);
2420 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2424 error
= ENOPROTOOPT
;
2431 * Discard the IP multicast options.
2434 ip_freemoptions(imo
)
2435 register struct ip_moptions
*imo
;
2440 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
2441 in_delmulti(&imo
->imo_membership
[i
]);
2442 FREE(imo
, M_IPMOPTS
);
2447 * Routine called from ip_output() to loop back a copy of an IP multicast
2448 * packet to the input queue of a specified interface. Note that this
2449 * calls the output routine of the loopback "driver", but with an interface
2450 * pointer that might NOT be a loopback interface -- evil, but easier than
2451 * replicating that code here.
2454 ip_mloopback(ifp
, m
, dst
, hlen
)
2456 register struct mbuf
*m
;
2457 register struct sockaddr_in
*dst
;
2460 register struct ip
*ip
;
2463 copym
= m_copy(m
, 0, M_COPYALL
);
2464 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2465 copym
= m_pullup(copym
, hlen
);
2466 if (copym
!= NULL
) {
2468 * We don't bother to fragment if the IP length is greater
2469 * than the interface's MTU. Can this possibly matter?
2471 ip
= mtod(copym
, struct ip
*);
2475 ip
->ip_sum
= in_cksum(copym
, hlen
);
2478 * It's not clear whether there are any lingering
2479 * reentrancy problems in other areas which might
2480 * be exposed by using ip_input directly (in
2481 * particular, everything which modifies the packet
2482 * in-place). Yet another option is using the
2483 * protosw directly to deliver the looped back
2484 * packet. For the moment, we'll err on the side
2485 * of safety by using if_simloop().
2488 if (dst
->sin_family
!= AF_INET
) {
2489 printf("ip_mloopback: bad address family %d\n",
2491 dst
->sin_family
= AF_INET
;
2497 * Mark checksum as valid or calculate checksum for loopback.
2499 * This is done this way because we have to embed the ifp of
2500 * the interface we will send the original copy of the packet
2501 * out on in the mbuf. ip_input will check if_hwassist of the
2502 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
2503 * The UDP checksum has not been calculated yet.
2505 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2506 if (IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
)) {
2507 copym
->m_pkthdr
.csum_flags
|=
2508 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2509 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2510 copym
->m_pkthdr
.csum_data
= 0xffff;
2513 in_delayed_cksum(copym
);
2521 * We need to send all loopback traffic down to dlil in case
2522 * a filter has tapped-in.
2526 * Stuff the 'real' ifp into the pkthdr, to be used in matching
2527 * in ip_input(); we need the loopback ifp/dl_tag passed as args
2528 * to make the loopback driver compliant with the data link
2532 copym
->m_pkthdr
.rcvif
= ifp
;
2533 dlil_output(lo_ifp
, PF_INET
, copym
, 0, (struct sockaddr
*) dst
, 0);
2535 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
2539 /* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/