2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1982, 1986, 1988, 1990, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
56 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.16 2001/07/19 06:37:26 kris Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
69 #include <kern/locks.h>
70 #include <sys/sysctl.h>
73 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip_var.h>
82 #include <netinet/kpi_ipfilter_var.h>
87 #include <sys/kdebug.h>
89 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
90 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
91 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
92 #define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
96 #include <netinet6/ipsec.h>
97 #include <netkey/key.h>
99 #include <netkey/key_debug.h>
101 #define KEYDEBUG(lev,arg)
105 #include <netinet/ip_fw.h>
106 #include <netinet/ip_divert.h>
109 #include <netinet/ip_dummynet.h>
112 #if IPFIREWALL_FORWARD_DEBUG
113 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
114 (ntohl(a.s_addr)>>16)&0xFF,\
115 (ntohl(a.s_addr)>>8)&0xFF,\
116 (ntohl(a.s_addr))&0xFF);
120 extern lck_mtx_t
*sadb_mutex
;
125 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
126 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
127 static void ip_mloopback(struct ifnet
*, struct mbuf
*,
128 struct sockaddr_in
*, int);
129 static int ip_getmoptions(struct sockopt
*, struct ip_moptions
*);
130 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
131 static int ip_setmoptions(struct sockopt
*, struct ip_moptions
**);
133 int ip_createmoptions(struct ip_moptions
**imop
);
134 int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
135 int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
136 int ip_optcopy(struct ip
*, struct ip
*);
137 extern int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**);
139 extern struct mbuf
* m_dup(register struct mbuf
*m
, int how
);
142 extern int apple_hwcksum_tx
;
143 extern u_long route_generation
;
145 extern struct protosw inetsw
[];
147 extern struct ip_linklocal_stat ip_linklocal_stat
;
148 extern lck_mtx_t
*ip_mutex
;
150 /* temporary: for testing */
152 extern int ipsec_bypass
;
155 static int ip_maxchainsent
= 0;
156 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxchainsent
, CTLFLAG_RW
,
157 &ip_maxchainsent
, 0, "use dlil_output_list");
159 * IP output. The packet in mbuf chain m contains a skeletal IP
160 * header (with len, off, ttl, proto, tos, src, dst).
161 * The mbuf chain containing the packet will be freed.
162 * The mbuf opt, if present, will not be freed.
170 struct ip_moptions
*imo
)
173 error
= ip_output_list(m0
, 0, opt
, ro
, flags
, imo
);
184 struct ip_moptions
*imo
)
186 struct ip
*ip
, *mhip
;
187 struct ifnet
*ifp
= NULL
;
189 int hlen
= sizeof (struct ip
);
190 int len
, off
, error
= 0;
191 struct sockaddr_in
*dst
= NULL
;
192 struct in_ifaddr
*ia
= NULL
;
193 int isbroadcast
, sw_csum
;
194 struct in_addr pkt_dst
;
196 struct route iproute
;
197 struct socket
*so
= NULL
;
198 struct secpolicy
*sp
= NULL
;
200 #if IPFIREWALL_FORWARD
201 int fwd_rewrite_src
= 0;
203 struct ip_fw_args args
;
205 ipfilter_t inject_filter_ref
= 0;
207 struct route dn_route
;
208 struct mbuf
* packetlist
;
211 lck_mtx_lock(ip_mutex
);
213 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
218 args
.next_hop
= NULL
;
219 args
.divert_rule
= 0; /* divert cookie */
221 /* Grab info from mtags prepended to the chain */
223 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
224 struct dn_pkt_tag
*dn_tag
;
226 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
227 args
.rule
= dn_tag
->rule
;
229 dn_route
= dn_tag
->ro
;
233 dst
= dn_tag
->dn_dst
;
235 flags
= dn_tag
->flags
;
237 m_tag_delete(m0
, tag
);
239 #endif /* DUMMYNET */
241 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
242 struct divert_tag
*div_tag
;
244 div_tag
= (struct divert_tag
*)(tag
+1);
245 args
.divert_rule
= div_tag
->cookie
;
247 m_tag_delete(m0
, tag
);
249 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
250 struct ip_fwd_tag
*ipfwd_tag
;
252 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
253 args
.next_hop
= ipfwd_tag
->next_hop
;
255 m_tag_delete(m0
, tag
);
261 if ( !m
|| (m
->m_flags
& M_PKTHDR
) != 0)
262 panic("ip_output no HDR");
264 panic("ip_output no route, proto = %d",
265 mtod(m
, struct ip
*)->ip_p
);
268 if (args
.rule
!= NULL
) { /* dummynet already saw us */
269 ip
= mtod(m
, struct ip
*);
270 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
271 if (ro
->ro_rt
!= NULL
)
272 ia
= (struct in_ifaddr
*)ro
->ro_rt
->rt_ifa
;
276 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
277 so
= ipsec_getsocket(m
);
278 (void)ipsec_setsocket(m
, NULL
);
285 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
286 so
= ipsec_getsocket(m
);
287 (void)ipsec_setsocket(m
, NULL
);
292 * No need to proccess packet twice if we've
295 inject_filter_ref
= ipf_get_inject_filter(m
);
298 m
= ip_insertoptions(m
, opt
, &len
);
301 ip
= mtod(m
, struct ip
*);
302 pkt_dst
= args
.next_hop
? args
.next_hop
->sin_addr
: ip
->ip_dst
;
307 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
308 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
311 ip
->ip_id
= ip_randomid();
313 ip
->ip_id
= htons(ip_id
++);
315 ipstat
.ips_localout
++;
317 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
320 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
321 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
323 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
326 * If there is a cached route,
327 * check that it is to the same destination
328 * and is still up. If not, free it and try again.
329 * The address family should also be checked in case of sharing the
334 if (ro
->ro_rt
&& (ro
->ro_rt
->generation_id
!= route_generation
) &&
335 ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0) && (ip
->ip_src
.s_addr
!= INADDR_ANY
) &&
336 (ifa_foraddr(ip
->ip_src
.s_addr
) == 0)) {
337 error
= EADDRNOTAVAIL
;
341 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
342 dst
->sin_family
!= AF_INET
||
343 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
)) {
345 ro
->ro_rt
= (struct rtentry
*)0;
347 if (ro
->ro_rt
== 0) {
348 bzero(dst
, sizeof(*dst
));
349 dst
->sin_family
= AF_INET
;
350 dst
->sin_len
= sizeof(*dst
);
351 dst
->sin_addr
= pkt_dst
;
354 * If routing to interface only,
355 * short circuit routing lookup.
357 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
358 #define sintosa(sin) ((struct sockaddr *)(sin))
359 if (flags
& IP_ROUTETOIF
) {
361 ifafree(&ia
->ia_ifa
);
362 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0) {
363 if ((ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
364 ipstat
.ips_noroute
++;
371 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
374 * If this is the case, we probably don't want to allocate
375 * a protocol-cloned route since we didn't get one from the
376 * ULP. This lets TCP do its thing, while not burdening
377 * forwarding or ICMP with the overhead of cloning a route.
378 * Of course, we still want to do any cloning requested by
379 * the link layer, as this is probably required in all cases
380 * for correct operation (as it is for ARP).
383 rtalloc_ign(ro
, RTF_PRCLONING
);
384 if (ro
->ro_rt
== 0) {
385 ipstat
.ips_noroute
++;
386 error
= EHOSTUNREACH
;
390 ifafree(&ia
->ia_ifa
);
391 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
394 ifp
= ro
->ro_rt
->rt_ifp
;
396 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
397 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
398 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
399 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
401 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
403 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
404 struct in_multi
*inm
;
406 m
->m_flags
|= M_MCAST
;
408 * IP destination address is multicast. Make sure "dst"
409 * still points to the address in "ro". (It may have been
410 * changed to point to a gateway address, above.)
412 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
414 * See if the caller provided any multicast options
417 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= imo
->imo_multicast_ttl
;
418 if (imo
->imo_multicast_ifp
!= NULL
) {
419 ifp
= imo
->imo_multicast_ifp
;
421 if (imo
->imo_multicast_vif
!= -1 &&
422 ((flags
& IP_RAWOUTPUT
) == 0 || ip
->ip_src
.s_addr
== INADDR_ANY
))
424 ip_mcast_src(imo
->imo_multicast_vif
);
426 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
428 * Confirm that the outgoing interface supports multicast.
430 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
431 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
432 ipstat
.ips_noroute
++;
438 * If source address not specified yet, use address
439 * of outgoing interface.
441 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
442 register struct in_ifaddr
*ia1
;
444 TAILQ_FOREACH(ia1
, &in_ifaddrhead
, ia_link
)
445 if (ia1
->ia_ifp
== ifp
) {
446 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
450 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
456 ifnet_lock_shared(ifp
);
457 IN_LOOKUP_MULTI(pkt_dst
, ifp
, inm
);
458 ifnet_lock_done(ifp
);
460 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
462 * If we belong to the destination multicast group
463 * on the outgoing interface, and the caller did not
464 * forbid loopback, loop back a copy.
466 if (!TAILQ_EMPTY(&ipv4_filters
)) {
467 struct ipfilter
*filter
;
468 int seen
= (inject_filter_ref
== 0);
469 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
473 ipf_pktopts
.ippo_mcast_ifnet
= imo
->imo_multicast_ifp
;
474 ipf_pktopts
.ippo_mcast_ttl
= imo
->imo_multicast_ttl
;
475 ipf_pktopts
.ippo_mcast_loop
= imo
->imo_multicast_loop
;
478 lck_mtx_unlock(ip_mutex
);
480 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
482 if ((struct ipfilter
*)inject_filter_ref
== filter
)
484 } else if (filter
->ipf_filter
.ipf_output
) {
486 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
487 if (result
== EJUSTRETURN
) {
493 lck_mtx_lock(ip_mutex
);
498 lck_mtx_lock(ip_mutex
);
502 ip_mloopback(ifp
, m
, dst
, hlen
);
506 * If we are acting as a multicast router, perform
507 * multicast forwarding as if the packet had just
508 * arrived on the interface to which we are about
509 * to send. The multicast forwarding function
510 * recursively calls this function, using the
511 * IP_FORWARDING flag to prevent infinite recursion.
513 * Multicasts that are looped back by ip_mloopback(),
514 * above, will be forwarded by the ip_input() routine,
517 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
519 * Check if rsvp daemon is running. If not, don't
520 * set ip_moptions. This ensures that the packet
521 * is multicast and not just sent down one link
522 * as prescribed by rsvpd.
526 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
528 lck_mtx_unlock(ip_mutex
);
535 * Multicasts with a time-to-live of zero may be looped-
536 * back, above, but must not be transmitted on a network.
537 * Also, multicasts addressed to the loopback interface
538 * are not sent -- the above call to ip_mloopback() will
539 * loop back a copy if this host actually belongs to the
540 * destination group on the loopback interface.
542 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
544 lck_mtx_unlock(ip_mutex
);
552 * If source address not specified yet, use address
553 * of outgoing interface.
555 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
556 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
557 #if IPFIREWALL_FORWARD
558 /* Keep note that we did this - if the firewall changes
559 * the next-hop, our interface may change, changing the
560 * default source IP. It's a shame so much effort happens
564 #endif /* IPFIREWALL_FORWARD */
569 * Look for broadcast address and
570 * and verify user is allowed to send
574 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
575 error
= EADDRNOTAVAIL
;
578 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
582 /* don't allow broadcast messages to be fragmented */
583 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
587 m
->m_flags
|= M_BCAST
;
589 m
->m_flags
&= ~M_BCAST
;
594 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
596 if (IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) || IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))) {
597 ip_linklocal_stat
.iplls_out_total
++;
598 if (ip
->ip_ttl
!= MAXTTL
) {
599 ip_linklocal_stat
.iplls_out_badttl
++;
605 if (!didfilter
&& !TAILQ_EMPTY(&ipv4_filters
)) {
606 struct ipfilter
*filter
;
607 int seen
= (inject_filter_ref
== 0);
609 lck_mtx_unlock(ip_mutex
);
611 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
613 if ((struct ipfilter
*)inject_filter_ref
== filter
)
615 } else if (filter
->ipf_filter
.ipf_output
) {
617 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
618 if (result
== EJUSTRETURN
) {
624 lck_mtx_lock(ip_mutex
);
630 lck_mtx_lock(ip_mutex
);
634 /* temporary for testing only: bypass ipsec alltogether */
636 if (ipsec_bypass
!= 0 || (flags
& IP_NOIPSEC
) != 0)
639 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
641 lck_mtx_lock(sadb_mutex
);
643 /* get SP for this packet */
645 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
647 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
650 ipsecstat
.out_inval
++;
651 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
652 lck_mtx_unlock(sadb_mutex
);
659 switch (sp
->policy
) {
660 case IPSEC_POLICY_DISCARD
:
662 * This packet is just discarded.
664 ipsecstat
.out_polvio
++;
665 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 1,0,0,0,0);
666 lck_mtx_unlock(sadb_mutex
);
669 case IPSEC_POLICY_BYPASS
:
670 case IPSEC_POLICY_NONE
:
671 /* no need to do IPsec. */
672 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 2,0,0,0,0);
673 lck_mtx_unlock(sadb_mutex
);
676 case IPSEC_POLICY_IPSEC
:
677 if (sp
->req
== NULL
) {
678 /* acquire a policy */
679 error
= key_spdacquire(sp
);
680 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 3,0,0,0,0);
681 lck_mtx_unlock(sadb_mutex
);
686 case IPSEC_POLICY_ENTRUST
:
688 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
691 struct ipsec_output_state state
;
692 bzero(&state
, sizeof(state
));
694 if (flags
& IP_ROUTETOIF
) {
696 bzero(&iproute
, sizeof(iproute
));
699 state
.dst
= (struct sockaddr
*)dst
;
705 * delayed checksums are not currently compatible with IPsec
707 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
709 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
715 lck_mtx_unlock(ip_mutex
);
716 error
= ipsec4_output(&state
, sp
, flags
);
717 lck_mtx_unlock(sadb_mutex
);
718 lck_mtx_lock(ip_mutex
);
722 if (flags
& IP_ROUTETOIF
) {
724 * if we have tunnel mode SA, we may need to ignore
727 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
728 flags
&= ~IP_ROUTETOIF
;
734 dst
= (struct sockaddr_in
*)state
.dst
;
736 /* mbuf is already reclaimed in ipsec4_output. */
746 printf("ip4_output (ipsec): error code %d\n", error
);
749 /* don't show these error codes to the user */
753 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 4,0,0,0,0);
758 /* be sure to update variables that are affected by ipsec4_output() */
759 ip
= mtod(m
, struct ip
*);
762 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
764 hlen
= ip
->ip_hl
<< 2;
766 /* Check that there wasn't a route change and src is still valid */
768 if (ro
->ro_rt
->generation_id
!= route_generation
) {
769 if (ifa_foraddr(ip
->ip_src
.s_addr
) == 0 && ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0)) {
770 error
= EADDRNOTAVAIL
;
771 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 5,0,0,0,0);
778 if (ro
->ro_rt
== NULL
) {
779 if ((flags
& IP_ROUTETOIF
) == 0) {
781 "can't update route after IPsec processing\n");
782 error
= EHOSTUNREACH
; /*XXX*/
783 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 6,0,0,0,0);
788 ifafree(&ia
->ia_ifa
);
789 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
792 ifp
= ro
->ro_rt
->rt_ifp
;
795 /* make it flipped, again. */
798 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 7,0xff,0xff,0xff,0xff);
800 /* Pass to filters again */
801 if (!TAILQ_EMPTY(&ipv4_filters
)) {
802 struct ipfilter
*filter
;
804 lck_mtx_unlock(ip_mutex
);
806 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
807 if (filter
->ipf_filter
.ipf_output
) {
809 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
810 if (result
== EJUSTRETURN
) {
816 lck_mtx_lock(ip_mutex
);
822 lck_mtx_lock(ip_mutex
);
829 * - Xlate: translate packet's addr/port (NAT).
830 * - Firewall: deny/allow/etc.
831 * - Wrap: fake packet's addr/port <unimpl.>
832 * - Encapsulate: put it in another IP and send out. <unimp.>
837 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
) {
838 lck_mtx_unlock(ip_mutex
);
841 ip
= mtod(m0
= m
= m1
, struct ip
*);
845 * Check with the firewall...
846 * but not if we are already being fwd'd from a firewall.
848 if (fw_enable
&& IPFW_LOADED
&& !args
.next_hop
) {
849 struct sockaddr_in
*old
= dst
;
854 lck_mtx_unlock(ip_mutex
);
855 off
= ip_fw_chk_ptr(&args
);
860 * On return we must do the following:
861 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
862 * 1<=off<= 0xffff -> DIVERT
863 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
864 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
865 * dst != old -> IPFIREWALL_FORWARD
866 * off==0, dst==old -> accept
867 * If some of the above modules is not compiled in, then
868 * we should't have to check the corresponding condition
869 * (because the ipfw control socket should not accept
870 * unsupported rules), but better play safe and drop
871 * packets in case of doubt.
874 if ( (off
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) {
880 ip
= mtod(m
, struct ip
*);
881 if (off
== 0 && dst
== old
) {/* common case */
882 lck_mtx_lock(ip_mutex
);
886 if (DUMMYNET_LOADED
&& (off
& IP_FW_PORT_DYNT_FLAG
) != 0) {
888 * pass the pkt to dummynet. Need to include
889 * pipe number, m, ifp, ro, dst because these are
890 * not recomputed in the next pass.
891 * All other parameters have been already used and
892 * so they are not needed anymore.
893 * XXX note: if the ifp or ro entry are deleted
894 * while a pkt is in dummynet, we are in trouble!
900 error
= ip_dn_io_ptr(m
, off
& 0xffff, DN_TO_IP_OUT
,
904 #endif /* DUMMYNET */
905 lck_mtx_lock(ip_mutex
);
907 if (off
!= 0 && (off
& IP_FW_PORT_DYNT_FLAG
) == 0) {
908 struct mbuf
*clone
= NULL
;
910 /* Clone packet if we're doing a 'tee' */
911 if ((off
& IP_FW_PORT_TEE_FLAG
) != 0)
912 clone
= m_dup(m
, M_DONTWAIT
);
915 * delayed checksums are not currently compatible
916 * with divert sockets.
918 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
920 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
923 /* Restore packet header fields to original values */
927 /* Deliver packet to divert input routine */
928 divert_packet(m
, 0, off
& 0xffff, args
.divert_rule
);
930 /* If 'tee', continue with original packet */
933 ip
= mtod(m
, struct ip
*);
936 lck_mtx_unlock(ip_mutex
);
941 #if IPFIREWALL_FORWARD
942 /* Here we check dst to make sure it's directly reachable on the
943 * interface we previously thought it was.
944 * If it isn't (which may be likely in some situations) we have
945 * to re-route it (ie, find a route for the next-hop and the
946 * associated interface) and set them here. This is nested
947 * forwarding which in most cases is undesirable, except where
948 * such control is nigh impossible. So we do it here.
951 if (off
== 0 && old
!= dst
) {
952 struct in_ifaddr
*ia_fw
;
954 /* It's changed... */
955 /* There must be a better way to do this next line... */
956 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
957 #if IPFIREWALL_FORWARD_DEBUG
958 printf("IPFIREWALL_FORWARD: New dst ip: ");
959 print_ip(dst
->sin_addr
);
963 * We need to figure out if we have been forwarded
964 * to a local socket. If so then we should somehow
965 * "loop back" to ip_input, and get directed to the
966 * PCB as if we had received this packet. This is
967 * because it may be dificult to identify the packets
968 * you want to forward until they are being output
969 * and have selected an interface. (e.g. locally
970 * initiated packets) If we used the loopback inteface,
971 * we would not be able to control what happens
972 * as the packet runs through ip_input() as
973 * it is done through a ISR.
975 TAILQ_FOREACH(ia_fw
, &in_ifaddrhead
, ia_link
) {
977 * If the addr to forward to is one
978 * of ours, we pretend to
979 * be the destination for this packet.
981 if (IA_SIN(ia_fw
)->sin_addr
.s_addr
==
982 dst
->sin_addr
.s_addr
)
986 /* tell ip_input "dont filter" */
987 struct m_tag
*fwd_tag
;
988 struct ip_fwd_tag
*ipfwd_tag
;
990 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
991 sizeof(struct sockaddr_in
), M_NOWAIT
);
992 if (fwd_tag
== NULL
) {
997 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
998 ipfwd_tag
->next_hop
= args
.next_hop
;
1000 m_tag_prepend(m
, fwd_tag
);
1002 if (m
->m_pkthdr
.rcvif
== NULL
)
1003 m
->m_pkthdr
.rcvif
= ifunit("lo0");
1004 if ((~IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) &
1005 m
->m_pkthdr
.csum_flags
) == 0) {
1006 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1007 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1008 m
->m_pkthdr
.csum_flags
|=
1009 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1010 m
->m_pkthdr
.csum_data
= 0xffff;
1012 m
->m_pkthdr
.csum_flags
|=
1013 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1015 else if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1016 in_delayed_cksum(m
);
1017 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1018 ip
->ip_sum
= in_cksum(m
, hlen
);
1023 lck_mtx_unlock(ip_mutex
);
1025 /* we need to call dlil_output to run filters
1026 * and resync to avoid recursion loops.
1029 dlil_output(lo_ifp
, PF_INET
, m
, 0, (struct sockaddr
*)dst
, 0);
1032 printf("ip_output: no loopback ifp for forwarding!!!\n");
1036 /* Some of the logic for this was
1037 * nicked from above.
1039 * This rewrites the cached route in a local PCB.
1040 * Is this what we want to do?
1042 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
1045 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
1047 if (ro_fwd
->ro_rt
== 0) {
1048 ipstat
.ips_noroute
++;
1049 error
= EHOSTUNREACH
;
1053 ia_fw
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
1054 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
1055 ro_fwd
->ro_rt
->rt_use
++;
1056 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
1057 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
1058 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
1060 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
1062 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
1064 ro
->ro_rt
= ro_fwd
->ro_rt
;
1065 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
1068 * If we added a default src ip earlier,
1069 * which would have been gotten from the-then
1070 * interface, do it again, from the new one.
1072 if (fwd_rewrite_src
)
1073 ip
->ip_src
= IA_SIN(ia_fw
)->sin_addr
;
1076 #endif /* IPFIREWALL_FORWARD */
1078 * if we get here, none of the above matches, and
1079 * we have to drop the pkt
1082 error
= EACCES
; /* not sure this is the right error msg */
1083 lck_mtx_unlock(ip_mutex
);
1089 /* Do not allow loopback address to wind up on a wire */
1090 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
1091 ((ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1092 (ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)) {
1093 ipstat
.ips_badaddr
++;
1096 * Do not simply drop the packet just like a firewall -- we want the
1097 * the application to feel the pain.
1098 * Return ENETUNREACH like ip6_output does in some similar cases.
1099 * This can startle the otherwise clueless process that specifies
1100 * loopback as the source address.
1102 error
= ENETUNREACH
;
1103 lck_mtx_unlock(ip_mutex
);
1107 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
1108 sw_csum
= m
->m_pkthdr
.csum_flags
1109 & ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1111 if ((ifp
->if_hwassist
& CSUM_TCP_SUM16
) != 0) {
1113 * Special case code for GMACE
1114 * frames that can be checksumed by GMACE SUM16 HW:
1115 * frame >64, no fragments, no UDP
1117 if (apple_hwcksum_tx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP
)
1118 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)) {
1119 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
1120 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
1121 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
1122 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
1123 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
1124 m
->m_pkthdr
.csum_data
+= offset
;
1125 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
1128 /* let the software handle any UDP or TCP checksums */
1129 sw_csum
|= (CSUM_DELAY_DATA
& m
->m_pkthdr
.csum_flags
);
1133 if (sw_csum
& CSUM_DELAY_DATA
) {
1134 in_delayed_cksum(m
);
1135 sw_csum
&= ~CSUM_DELAY_DATA
;
1136 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1139 m
->m_pkthdr
.csum_flags
&= IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1142 * If small enough for interface, or the interface will take
1143 * care of the fragmentation for us, can just send directly.
1145 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
||
1146 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
1150 if (sw_csum
& CSUM_DELAY_IP
) {
1151 ip
->ip_sum
= in_cksum(m
, hlen
);
1155 /* Record statistics for this interface address. */
1156 if (!(flags
& IP_FORWARDING
) && ia
!= NULL
) {
1157 ia
->ia_ifa
.if_opackets
++;
1158 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1163 /* clean ipsec history once it goes out of the node */
1164 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1167 if (packetchain
== 0) {
1168 lck_mtx_unlock(ip_mutex
);
1169 error
= dlil_output(ifp
, PF_INET
, m
, (void *) ro
->ro_rt
,
1170 (struct sockaddr
*)dst
, 0);
1173 else { /* packet chaining allows us to reuse the route for all packets */
1176 if (pktcnt
> ip_maxchainsent
)
1177 ip_maxchainsent
= pktcnt
;
1179 lck_mtx_unlock(ip_mutex
);
1180 error
= dlil_output_list(ifp
, PF_INET
, packetlist
, (void *) ro
->ro_rt
,
1181 (struct sockaddr
*)dst
, 0);
1192 * Too large for interface; fragment if possible.
1193 * Must be able to put at least 8 bytes per fragment.
1195 if (ip
->ip_off
& IP_DF
) {
1198 * This case can happen if the user changed the MTU
1199 * of an interface after enabling IP on it. Because
1200 * most netifs don't keep track of routes pointing to
1201 * them, there is no way for one to update all its
1202 * routes when the MTU is changed.
1204 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
1205 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
1206 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
1207 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
1209 ipstat
.ips_cantfrag
++;
1212 len
= (ifp
->if_mtu
- hlen
) &~ 7;
1219 * if the interface will not calculate checksums on
1220 * fragmented packets, then do it here.
1222 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
1223 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
1224 in_delayed_cksum(m
);
1226 lck_mtx_unlock(ip_mutex
);
1229 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1234 int mhlen
, firstlen
= len
;
1235 struct mbuf
**mnext
= &m
->m_nextpkt
;
1239 * Loop through length of segment after first fragment,
1240 * make new header and copy data of each part and link onto chain.
1243 mhlen
= sizeof (struct ip
);
1244 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
1245 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1248 ipstat
.ips_odropped
++;
1251 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
1252 m
->m_data
+= max_linkhdr
;
1253 mhip
= mtod(m
, struct ip
*);
1255 if (hlen
> sizeof (struct ip
)) {
1256 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
1257 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
1260 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
1261 if (ip
->ip_off
& IP_MF
)
1262 mhip
->ip_off
|= IP_MF
;
1263 if (off
+ len
>= (u_short
)ip
->ip_len
)
1264 len
= (u_short
)ip
->ip_len
- off
;
1266 mhip
->ip_off
|= IP_MF
;
1267 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
1268 m
->m_next
= m_copy(m0
, off
, len
);
1269 if (m
->m_next
== 0) {
1271 error
= ENOBUFS
; /* ??? */
1272 ipstat
.ips_odropped
++;
1275 m
->m_pkthdr
.len
= mhlen
+ len
;
1276 m
->m_pkthdr
.rcvif
= 0;
1277 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
1278 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1279 HTONS(mhip
->ip_off
);
1281 if (sw_csum
& CSUM_DELAY_IP
) {
1282 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1285 mnext
= &m
->m_nextpkt
;
1288 ipstat
.ips_ofragments
+= nfrags
;
1290 /* set first/last markers for fragment chain */
1291 m
->m_flags
|= M_LASTFRAG
;
1292 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1293 m0
->m_pkthdr
.csum_data
= nfrags
;
1296 * Update first fragment by trimming what's been copied out
1297 * and updating header, then send each fragment (in order).
1300 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
1301 m
->m_pkthdr
.len
= hlen
+ firstlen
;
1302 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
1303 ip
->ip_off
|= IP_MF
;
1306 if (sw_csum
& CSUM_DELAY_IP
) {
1307 ip
->ip_sum
= in_cksum(m
, hlen
);
1311 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1312 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1314 lck_mtx_unlock(ip_mutex
);
1315 for (m
= m0
; m
; m
= m0
) {
1319 /* clean ipsec history once it goes out of the node */
1320 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1325 /* Record statistics for this interface address. */
1327 ia
->ia_ifa
.if_opackets
++;
1328 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1331 if ((packetchain
!= 0) && (pktcnt
> 0))
1332 panic("ip_output: mix of packet in packetlist is wrong=%x", packetlist
);
1333 error
= dlil_output(ifp
, PF_INET
, m
, (void *) ro
->ro_rt
,
1334 (struct sockaddr
*)dst
, 0);
1340 ipstat
.ips_fragmented
++;
1344 ifafree(&ia
->ia_ifa
);
1348 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
1349 if (ro
== &iproute
&& ro
->ro_rt
) {
1354 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1355 printf("DP ip_output call free SP:%x\n", sp
));
1356 lck_mtx_lock(sadb_mutex
);
1358 lck_mtx_unlock(sadb_mutex
);
1363 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1367 lck_mtx_unlock(ip_mutex
);
1372 in_delayed_cksum_offset(struct mbuf
*m
, int ip_offset
)
1375 u_short csum
, offset
;
1377 while (ip_offset
> m
->m_len
) {
1378 ip_offset
-= m
->m_len
;
1381 printf("in_delayed_cksum_withoffset failed - ip_offset wasn't in the packet\n");
1386 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1387 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1388 m
->m_len
, ip_offset
, ip
->ip_p
);
1391 * this shouldn't happen
1393 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
1398 m
->m_len
-= ip_offset
;
1399 m
->m_data
+= ip_offset
;
1402 ip
= mtod(m
, struct ip
*);
1403 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1404 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1405 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1407 offset
+= m
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1411 if (M_LEADINGSPACE(m
) < ip_offset
)
1412 panic("in_delayed_cksum_withoffset - chain modified!\n");
1413 m
->m_len
+= ip_offset
;
1414 m
->m_data
-= ip_offset
;
1417 if (offset
> ip
->ip_len
) /* bogus offset */
1420 if (offset
+ ip_offset
+ sizeof(u_short
) > m
->m_len
) {
1421 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1422 m
->m_len
, offset
+ ip_offset
, ip
->ip_p
);
1425 * this shouldn't happen, but if it does, the
1426 * correct behavior may be to insert the checksum
1427 * in the existing chain instead of rearranging it.
1429 m
= m_pullup(m
, offset
+ ip_offset
+ sizeof(u_short
));
1431 *(u_short
*)(m
->m_data
+ offset
+ ip_offset
) = csum
;
1435 in_delayed_cksum(struct mbuf
*m
)
1437 in_delayed_cksum_offset(m
, 0);
1441 in_cksum_offset(struct mbuf
* m
, size_t ip_offset
)
1443 struct ip
* ip
= NULL
;
1446 while (ip_offset
> m
->m_len
) {
1447 ip_offset
-= m
->m_len
;
1450 printf("in_cksum_offset failed - ip_offset wasn't in the packet\n");
1455 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1456 printf("in_cksum_offset - delayed m_pullup, m->len: %d off: %d\n",
1457 m
->m_len
, ip_offset
);
1460 * this shouldn't happen
1462 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
1467 m
->m_len
-= ip_offset
;
1468 m
->m_data
+= ip_offset
;
1471 ip
= mtod(m
, struct ip
*);
1474 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1476 hlen
= ip
->ip_hl
<< 2;
1480 ip
->ip_sum
= in_cksum(m
, hlen
);
1484 if (M_LEADINGSPACE(m
) < ip_offset
)
1485 panic("in_cksum_offset - chain modified!\n");
1486 m
->m_len
+= ip_offset
;
1487 m
->m_data
-= ip_offset
;
1492 * Insert IP options into preformed packet.
1493 * Adjust IP destination as required for IP source routing,
1494 * as indicated by a non-zero in_addr at the start of the options.
1496 * XXX This routine assumes that the packet has no options in place.
1498 static struct mbuf
*
1499 ip_insertoptions(m
, opt
, phlen
)
1500 register struct mbuf
*m
;
1504 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1506 register struct ip
*ip
= mtod(m
, struct ip
*);
1509 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1510 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1511 return (m
); /* XXX should fail */
1512 if (p
->ipopt_dst
.s_addr
)
1513 ip
->ip_dst
= p
->ipopt_dst
;
1514 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1515 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
1518 n
->m_pkthdr
.rcvif
= 0;
1519 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1520 m
->m_len
-= sizeof(struct ip
);
1521 m
->m_data
+= sizeof(struct ip
);
1524 m
->m_len
= optlen
+ sizeof(struct ip
);
1525 m
->m_data
+= max_linkhdr
;
1526 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1528 m
->m_data
-= optlen
;
1530 m
->m_pkthdr
.len
+= optlen
;
1531 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1533 ip
= mtod(m
, struct ip
*);
1534 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1535 *phlen
= sizeof(struct ip
) + optlen
;
1536 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1537 ip
->ip_len
+= optlen
;
1542 * Copy options from ip to jp,
1543 * omitting those not copied during fragmentation.
1549 register u_char
*cp
, *dp
;
1550 int opt
, optlen
, cnt
;
1552 cp
= (u_char
*)(ip
+ 1);
1553 dp
= (u_char
*)(jp
+ 1);
1554 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1555 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1557 if (opt
== IPOPT_EOL
)
1559 if (opt
== IPOPT_NOP
) {
1560 /* Preserve for IP mcast tunnel's LSRR alignment. */
1566 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1567 panic("malformed IPv4 option passed to ip_optcopy");
1569 optlen
= cp
[IPOPT_OLEN
];
1571 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1572 panic("malformed IPv4 option passed to ip_optcopy");
1574 /* bogus lengths should have been caught by ip_dooptions */
1577 if (IPOPT_COPIED(opt
)) {
1578 bcopy(cp
, dp
, optlen
);
1582 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1588 * IP socket option processing.
1591 ip_ctloutput(so
, sopt
)
1593 struct sockopt
*sopt
;
1595 struct inpcb
*inp
= sotoinpcb(so
);
1599 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1603 switch (sopt
->sopt_dir
) {
1605 switch (sopt
->sopt_name
) {
1612 if (sopt
->sopt_valsize
> MLEN
) {
1616 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1621 m
->m_len
= sopt
->sopt_valsize
;
1622 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1627 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1634 case IP_RECVRETOPTS
:
1635 case IP_RECVDSTADDR
:
1638 #if defined(NFAITH) && NFAITH > 0
1641 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1646 switch (sopt
->sopt_name
) {
1648 inp
->inp_ip_tos
= optval
;
1652 inp
->inp_ip_ttl
= optval
;
1654 #define OPTSET(bit) \
1656 inp->inp_flags |= bit; \
1658 inp->inp_flags &= ~bit;
1661 OPTSET(INP_RECVOPTS
);
1664 case IP_RECVRETOPTS
:
1665 OPTSET(INP_RECVRETOPTS
);
1668 case IP_RECVDSTADDR
:
1669 OPTSET(INP_RECVDSTADDR
);
1677 OPTSET(INP_RECVTTL
);
1680 #if defined(NFAITH) && NFAITH > 0
1689 case IP_MULTICAST_IF
:
1690 case IP_MULTICAST_VIF
:
1691 case IP_MULTICAST_TTL
:
1692 case IP_MULTICAST_LOOP
:
1693 case IP_ADD_MEMBERSHIP
:
1694 case IP_DROP_MEMBERSHIP
:
1695 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1699 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1705 case IP_PORTRANGE_DEFAULT
:
1706 inp
->inp_flags
&= ~(INP_LOWPORT
);
1707 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1710 case IP_PORTRANGE_HIGH
:
1711 inp
->inp_flags
&= ~(INP_LOWPORT
);
1712 inp
->inp_flags
|= INP_HIGHPORT
;
1715 case IP_PORTRANGE_LOW
:
1716 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1717 inp
->inp_flags
|= INP_LOWPORT
;
1727 case IP_IPSEC_POLICY
:
1735 if (sopt
->sopt_valsize
> MCLBYTES
) {
1739 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1741 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1743 priv
= (sopt
->sopt_p
!= NULL
&&
1744 proc_suser(sopt
->sopt_p
) != 0) ? 0 : 1;
1746 req
= mtod(m
, caddr_t
);
1749 optname
= sopt
->sopt_name
;
1750 lck_mtx_lock(sadb_mutex
);
1751 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1752 lck_mtx_unlock(sadb_mutex
);
1759 error
= ENOPROTOOPT
;
1765 switch (sopt
->sopt_name
) {
1768 if (inp
->inp_options
)
1769 error
= sooptcopyout(sopt
,
1770 mtod(inp
->inp_options
,
1772 inp
->inp_options
->m_len
);
1774 sopt
->sopt_valsize
= 0;
1780 case IP_RECVRETOPTS
:
1781 case IP_RECVDSTADDR
:
1785 #if defined(NFAITH) && NFAITH > 0
1788 switch (sopt
->sopt_name
) {
1791 optval
= inp
->inp_ip_tos
;
1795 optval
= inp
->inp_ip_ttl
;
1798 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1801 optval
= OPTBIT(INP_RECVOPTS
);
1804 case IP_RECVRETOPTS
:
1805 optval
= OPTBIT(INP_RECVRETOPTS
);
1808 case IP_RECVDSTADDR
:
1809 optval
= OPTBIT(INP_RECVDSTADDR
);
1813 optval
= OPTBIT(INP_RECVIF
);
1817 optval
= OPTBIT(INP_RECVTTL
);
1821 if (inp
->inp_flags
& INP_HIGHPORT
)
1822 optval
= IP_PORTRANGE_HIGH
;
1823 else if (inp
->inp_flags
& INP_LOWPORT
)
1824 optval
= IP_PORTRANGE_LOW
;
1829 #if defined(NFAITH) && NFAITH > 0
1831 optval
= OPTBIT(INP_FAITH
);
1835 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1838 case IP_MULTICAST_IF
:
1839 case IP_MULTICAST_VIF
:
1840 case IP_MULTICAST_TTL
:
1841 case IP_MULTICAST_LOOP
:
1842 case IP_ADD_MEMBERSHIP
:
1843 case IP_DROP_MEMBERSHIP
:
1844 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1848 case IP_IPSEC_POLICY
:
1850 struct mbuf
*m
= NULL
;
1855 req
= mtod(m
, caddr_t
);
1858 lck_mtx_lock(sadb_mutex
);
1859 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
1860 lck_mtx_unlock(sadb_mutex
);
1862 error
= soopt_mcopyout(sopt
, m
); /* XXX */
1870 error
= ENOPROTOOPT
;
1879 * Set up IP options in pcb for insertion in output packets.
1880 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1881 * with destination address if source routed.
1884 ip_pcbopts(optname
, pcbopt
, m
)
1886 struct mbuf
**pcbopt
;
1887 register struct mbuf
*m
;
1889 register int cnt
, optlen
;
1890 register u_char
*cp
;
1893 /* turn off any old options */
1895 (void)m_free(*pcbopt
);
1897 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
1899 * Only turning off any previous options.
1907 if (m
->m_len
% sizeof(int32_t))
1911 * IP first-hop destination address will be stored before
1912 * actual options; move other options back
1913 * and clear it when none present.
1915 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1918 m
->m_len
+= sizeof(struct in_addr
);
1919 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1920 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
1921 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1923 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1924 opt
= cp
[IPOPT_OPTVAL
];
1925 if (opt
== IPOPT_EOL
)
1927 if (opt
== IPOPT_NOP
)
1930 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1932 optlen
= cp
[IPOPT_OLEN
];
1933 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1944 * user process specifies route as:
1946 * D must be our final destination (but we can't
1947 * check that since we may not have connected yet).
1948 * A is first hop destination, which doesn't appear in
1949 * actual IP option, but is stored before the options.
1951 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1953 m
->m_len
-= sizeof(struct in_addr
);
1954 cnt
-= sizeof(struct in_addr
);
1955 optlen
-= sizeof(struct in_addr
);
1956 cp
[IPOPT_OLEN
] = optlen
;
1958 * Move first hop before start of options.
1960 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1961 sizeof(struct in_addr
));
1963 * Then copy rest of options back
1964 * to close up the deleted entry.
1966 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
1967 sizeof(struct in_addr
)),
1968 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
1969 (unsigned)cnt
+ sizeof(struct in_addr
));
1973 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1985 * The whole multicast option thing needs to be re-thought.
1986 * Several of these options are equally applicable to non-multicast
1987 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1988 * standard option (IP_TTL).
1992 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1994 static struct ifnet
*
1995 ip_multicast_if(a
, ifindexp
)
2004 if (ntohl(a
->s_addr
) >> 24 == 0) {
2005 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
2006 ifnet_head_lock_shared();
2007 if (ifindex
< 0 || if_index
< ifindex
) {
2011 ifp
= ifindex2ifnet
[ifindex
];
2014 *ifindexp
= ifindex
;
2016 INADDR_TO_IFP(*a
, ifp
);
2022 * Set the IP multicast options in response to user setsockopt().
2025 ip_setmoptions(sopt
, imop
)
2026 struct sockopt
*sopt
;
2027 struct ip_moptions
**imop
;
2031 struct in_addr addr
;
2032 struct ip_mreq mreq
;
2033 struct ifnet
*ifp
= NULL
;
2034 struct ip_moptions
*imo
= *imop
;
2039 * No multicast option buffer attached to the pcb;
2040 * allocate one and initialize to default values.
2042 error
= ip_createmoptions(imop
);
2048 switch (sopt
->sopt_name
) {
2049 /* store an index number for the vif you wanna use in the send */
2050 case IP_MULTICAST_VIF
:
2051 if (legal_vif_num
== 0) {
2055 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2058 if (!legal_vif_num(i
) && (i
!= -1)) {
2062 imo
->imo_multicast_vif
= i
;
2065 case IP_MULTICAST_IF
:
2067 * Select the interface for outgoing multicast packets.
2069 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
2073 * INADDR_ANY is used to remove a previous selection.
2074 * When no interface is selected, a default one is
2075 * chosen every time a multicast packet is sent.
2077 if (addr
.s_addr
== INADDR_ANY
) {
2078 imo
->imo_multicast_ifp
= NULL
;
2082 * The selected interface is identified by its local
2083 * IP address. Find the interface and confirm that
2084 * it supports multicasting.
2086 ifp
= ip_multicast_if(&addr
, &ifindex
);
2087 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2088 error
= EADDRNOTAVAIL
;
2091 imo
->imo_multicast_ifp
= ifp
;
2093 imo
->imo_multicast_addr
= addr
;
2095 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2098 case IP_MULTICAST_TTL
:
2100 * Set the IP time-to-live for outgoing multicast packets.
2101 * The original multicast API required a char argument,
2102 * which is inconsistent with the rest of the socket API.
2103 * We allow either a char or an int.
2105 if (sopt
->sopt_valsize
== 1) {
2107 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
2110 imo
->imo_multicast_ttl
= ttl
;
2113 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
2120 imo
->imo_multicast_ttl
= ttl
;
2124 case IP_MULTICAST_LOOP
:
2126 * Set the loopback flag for outgoing multicast packets.
2127 * Must be zero or one. The original multicast API required a
2128 * char argument, which is inconsistent with the rest
2129 * of the socket API. We allow either a char or an int.
2131 if (sopt
->sopt_valsize
== 1) {
2133 error
= sooptcopyin(sopt
, &loop
, 1, 1);
2136 imo
->imo_multicast_loop
= !!loop
;
2139 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
2143 imo
->imo_multicast_loop
= !!loop
;
2147 case IP_ADD_MEMBERSHIP
:
2149 * Add a multicast group membership.
2150 * Group must be a valid IP multicast address.
2152 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2156 error
= ip_addmembership(imo
, &mreq
);
2159 case IP_DROP_MEMBERSHIP
:
2161 * Drop a multicast group membership.
2162 * Group must be a valid IP multicast address.
2164 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2168 error
= ip_dropmembership(imo
, &mreq
);
2177 * If all options have default values, no need to keep the mbuf.
2179 if (imo
->imo_multicast_ifp
== NULL
&&
2180 imo
->imo_multicast_vif
== -1 &&
2181 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2182 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2183 imo
->imo_num_memberships
== 0) {
2184 FREE(*imop
, M_IPMOPTS
);
2192 * Set the IP multicast options in response to user setsockopt().
2194 __private_extern__
int
2196 struct ip_moptions
**imop
)
2198 struct ip_moptions
*imo
;
2199 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
2205 imo
->imo_multicast_ifp
= NULL
;
2206 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2207 imo
->imo_multicast_vif
= -1;
2208 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2209 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
2210 imo
->imo_num_memberships
= 0;
2216 * Add membership to an IPv4 multicast.
2218 __private_extern__
int
2220 struct ip_moptions
*imo
,
2221 struct ip_mreq
*mreq
)
2224 struct sockaddr_in
*dst
;
2225 struct ifnet
*ifp
= NULL
;
2229 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2234 * If no interface address was provided, use the interface of
2235 * the route to the given multicast address.
2237 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
2238 bzero((caddr_t
)&ro
, sizeof(ro
));
2239 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
2240 dst
->sin_len
= sizeof(*dst
);
2241 dst
->sin_family
= AF_INET
;
2242 dst
->sin_addr
= mreq
->imr_multiaddr
;
2244 if (ro
.ro_rt
!= NULL
) {
2245 ifp
= ro
.ro_rt
->rt_ifp
;
2249 /* If there's no default route, try using loopback */
2250 mreq
->imr_interface
.s_addr
= INADDR_LOOPBACK
;
2255 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2259 * See if we found an interface, and confirm that it
2260 * supports multicast.
2262 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2263 error
= EADDRNOTAVAIL
;
2267 * See if the membership already exists or if all the
2268 * membership slots are full.
2270 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2271 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
2272 imo
->imo_membership
[i
]->inm_addr
.s_addr
2273 == mreq
->imr_multiaddr
.s_addr
)
2276 if (i
< imo
->imo_num_memberships
) {
2280 if (i
== IP_MAX_MEMBERSHIPS
) {
2281 error
= ETOOMANYREFS
;
2285 * Everything looks good; add a new record to the multicast
2286 * address list for the given interface.
2288 if ((imo
->imo_membership
[i
] =
2289 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
2293 ++imo
->imo_num_memberships
;
2299 * Drop membership of an IPv4 multicast.
2301 __private_extern__
int
2303 struct ip_moptions
*imo
,
2304 struct ip_mreq
*mreq
)
2307 struct ifnet
* ifp
= NULL
;
2310 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2316 * If an interface address was specified, get a pointer
2317 * to its ifnet structure.
2319 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
2322 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2324 error
= EADDRNOTAVAIL
;
2329 * Find the membership in the membership array.
2331 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2333 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2334 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2335 mreq
->imr_multiaddr
.s_addr
)
2338 if (i
== imo
->imo_num_memberships
) {
2339 error
= EADDRNOTAVAIL
;
2343 * Give up the multicast address record to which the
2344 * membership points.
2346 in_delmulti(&imo
->imo_membership
[i
]);
2348 * Remove the gap in the membership array.
2350 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2351 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2352 --imo
->imo_num_memberships
;
2358 * Return the IP multicast options in response to user getsockopt().
2361 ip_getmoptions(sopt
, imo
)
2362 struct sockopt
*sopt
;
2363 register struct ip_moptions
*imo
;
2365 struct in_addr addr
;
2366 struct in_ifaddr
*ia
;
2371 switch (sopt
->sopt_name
) {
2372 case IP_MULTICAST_VIF
:
2374 optval
= imo
->imo_multicast_vif
;
2377 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2380 case IP_MULTICAST_IF
:
2381 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2382 addr
.s_addr
= INADDR_ANY
;
2383 else if (imo
->imo_multicast_addr
.s_addr
) {
2384 /* return the value user has set */
2385 addr
= imo
->imo_multicast_addr
;
2387 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
2388 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2389 : IA_SIN(ia
)->sin_addr
.s_addr
;
2391 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
2394 case IP_MULTICAST_TTL
:
2396 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
2398 optval
= coptval
= imo
->imo_multicast_ttl
;
2399 if (sopt
->sopt_valsize
== 1)
2400 error
= sooptcopyout(sopt
, &coptval
, 1);
2402 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2405 case IP_MULTICAST_LOOP
:
2407 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
2409 optval
= coptval
= imo
->imo_multicast_loop
;
2410 if (sopt
->sopt_valsize
== 1)
2411 error
= sooptcopyout(sopt
, &coptval
, 1);
2413 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2417 error
= ENOPROTOOPT
;
2424 * Discard the IP multicast options.
2427 ip_freemoptions(imo
)
2428 register struct ip_moptions
*imo
;
2433 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
2434 in_delmulti(&imo
->imo_membership
[i
]);
2435 FREE(imo
, M_IPMOPTS
);
2440 * Routine called from ip_output() to loop back a copy of an IP multicast
2441 * packet to the input queue of a specified interface. Note that this
2442 * calls the output routine of the loopback "driver", but with an interface
2443 * pointer that might NOT be a loopback interface -- evil, but easier than
2444 * replicating that code here.
2447 ip_mloopback(ifp
, m
, dst
, hlen
)
2449 register struct mbuf
*m
;
2450 register struct sockaddr_in
*dst
;
2453 register struct ip
*ip
;
2456 copym
= m_copy(m
, 0, M_COPYALL
);
2457 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2458 copym
= m_pullup(copym
, hlen
);
2459 if (copym
!= NULL
) {
2461 * We don't bother to fragment if the IP length is greater
2462 * than the interface's MTU. Can this possibly matter?
2464 ip
= mtod(copym
, struct ip
*);
2468 ip
->ip_sum
= in_cksum(copym
, hlen
);
2471 * It's not clear whether there are any lingering
2472 * reentrancy problems in other areas which might
2473 * be exposed by using ip_input directly (in
2474 * particular, everything which modifies the packet
2475 * in-place). Yet another option is using the
2476 * protosw directly to deliver the looped back
2477 * packet. For the moment, we'll err on the side
2478 * of safety by using if_simloop().
2481 if (dst
->sin_family
!= AF_INET
) {
2482 printf("ip_mloopback: bad address family %d\n",
2484 dst
->sin_family
= AF_INET
;
2490 * Mark checksum as valid or calculate checksum for loopback.
2492 * This is done this way because we have to embed the ifp of
2493 * the interface we will send the original copy of the packet
2494 * out on in the mbuf. ip_input will check if_hwassist of the
2495 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
2496 * The UDP checksum has not been calculated yet.
2498 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2499 if (IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
)) {
2500 copym
->m_pkthdr
.csum_flags
|=
2501 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2502 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2503 copym
->m_pkthdr
.csum_data
= 0xffff;
2506 in_delayed_cksum(copym
);
2514 * We need to send all loopback traffic down to dlil in case
2515 * a filter has tapped-in.
2519 * Stuff the 'real' ifp into the pkthdr, to be used in matching
2520 * in ip_input(); we need the loopback ifp/dl_tag passed as args
2521 * to make the loopback driver compliant with the data link
2525 copym
->m_pkthdr
.rcvif
= ifp
;
2526 dlil_output(lo_ifp
, PF_INET
, copym
, 0, (struct sockaddr
*) dst
, 0);
2528 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
2532 /* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/