2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
55 * $FreeBSD: src/sys/netinet/ip_output.c,v 1.99.2.16 2001/07/19 06:37:26 kris Exp $
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <kern/locks.h>
69 #include <sys/sysctl.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip_var.h>
81 #include <netinet/kpi_ipfilter_var.h>
86 #include <sys/kdebug.h>
88 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
89 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
90 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
91 #define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
95 #include <netinet6/ipsec.h>
96 #include <netkey/key.h>
98 #include <netkey/key_debug.h>
100 #define KEYDEBUG(lev,arg)
104 #include <netinet/ip_fw.h>
105 #include <netinet/ip_divert.h>
108 #include <netinet/ip_dummynet.h>
111 #if IPFIREWALL_FORWARD_DEBUG
112 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
113 (ntohl(a.s_addr)>>16)&0xFF,\
114 (ntohl(a.s_addr)>>8)&0xFF,\
115 (ntohl(a.s_addr))&0xFF);
119 extern lck_mtx_t
*sadb_mutex
;
124 static struct mbuf
*ip_insertoptions(struct mbuf
*, struct mbuf
*, int *);
125 static struct ifnet
*ip_multicast_if(struct in_addr
*, int *);
126 static void ip_mloopback(struct ifnet
*, struct mbuf
*,
127 struct sockaddr_in
*, int);
128 static int ip_getmoptions(struct sockopt
*, struct ip_moptions
*);
129 static int ip_pcbopts(int, struct mbuf
**, struct mbuf
*);
130 static int ip_setmoptions(struct sockopt
*, struct ip_moptions
**);
132 int ip_createmoptions(struct ip_moptions
**imop
);
133 int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
134 int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
135 int ip_optcopy(struct ip
*, struct ip
*);
136 extern int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**);
138 extern struct mbuf
* m_dup(register struct mbuf
*m
, int how
);
141 extern int apple_hwcksum_tx
;
142 extern u_long route_generation
;
144 extern struct protosw inetsw
[];
146 extern struct ip_linklocal_stat ip_linklocal_stat
;
147 extern lck_mtx_t
*ip_mutex
;
149 /* temporary: for testing */
151 extern int ipsec_bypass
;
154 static int ip_maxchainsent
= 0;
155 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxchainsent
, CTLFLAG_RW
,
156 &ip_maxchainsent
, 0, "use dlil_output_list");
158 * IP output. The packet in mbuf chain m contains a skeletal IP
159 * header (with len, off, ttl, proto, tos, src, dst).
160 * The mbuf chain containing the packet will be freed.
161 * The mbuf opt, if present, will not be freed.
169 struct ip_moptions
*imo
)
172 error
= ip_output_list(m0
, 0, opt
, ro
, flags
, imo
);
183 struct ip_moptions
*imo
)
185 struct ip
*ip
, *mhip
;
186 struct ifnet
*ifp
= NULL
;
188 int hlen
= sizeof (struct ip
);
189 int len
, off
, error
= 0;
190 struct sockaddr_in
*dst
= NULL
;
191 struct in_ifaddr
*ia
= NULL
;
192 int isbroadcast
, sw_csum
;
193 struct in_addr pkt_dst
;
195 struct route iproute
;
196 struct socket
*so
= NULL
;
197 struct secpolicy
*sp
= NULL
;
199 #if IPFIREWALL_FORWARD
200 int fwd_rewrite_src
= 0;
202 struct ip_fw_args args
;
204 ipfilter_t inject_filter_ref
= 0;
206 struct route dn_route
;
207 struct mbuf
* packetlist
;
210 lck_mtx_lock(ip_mutex
);
212 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
217 args
.next_hop
= NULL
;
218 args
.divert_rule
= 0; /* divert cookie */
220 /* Grab info from mtags prepended to the chain */
222 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
223 struct dn_pkt_tag
*dn_tag
;
225 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
226 args
.rule
= dn_tag
->rule
;
228 dn_route
= dn_tag
->ro
;
232 dst
= dn_tag
->dn_dst
;
234 flags
= dn_tag
->flags
;
236 m_tag_delete(m0
, tag
);
238 #endif /* DUMMYNET */
240 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
241 struct divert_tag
*div_tag
;
243 div_tag
= (struct divert_tag
*)(tag
+1);
244 args
.divert_rule
= div_tag
->cookie
;
246 m_tag_delete(m0
, tag
);
248 if ((tag
= m_tag_locate(m0
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
249 struct ip_fwd_tag
*ipfwd_tag
;
251 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
252 args
.next_hop
= ipfwd_tag
->next_hop
;
254 m_tag_delete(m0
, tag
);
260 if ( !m
|| (m
->m_flags
& M_PKTHDR
) != 0)
261 panic("ip_output no HDR");
263 panic("ip_output no route, proto = %d",
264 mtod(m
, struct ip
*)->ip_p
);
267 if (args
.rule
!= NULL
) { /* dummynet already saw us */
268 ip
= mtod(m
, struct ip
*);
269 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
270 if (ro
->ro_rt
!= NULL
)
271 ia
= (struct in_ifaddr
*)ro
->ro_rt
->rt_ifa
;
275 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
276 so
= ipsec_getsocket(m
);
277 (void)ipsec_setsocket(m
, NULL
);
284 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
285 so
= ipsec_getsocket(m
);
286 (void)ipsec_setsocket(m
, NULL
);
291 * No need to proccess packet twice if we've
294 inject_filter_ref
= ipf_get_inject_filter(m
);
297 m
= ip_insertoptions(m
, opt
, &len
);
300 ip
= mtod(m
, struct ip
*);
301 pkt_dst
= args
.next_hop
? args
.next_hop
->sin_addr
: ip
->ip_dst
;
306 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
307 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
310 ip
->ip_id
= ip_randomid();
312 ip
->ip_id
= htons(ip_id
++);
314 ipstat
.ips_localout
++;
316 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
319 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
320 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
322 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
325 * If there is a cached route,
326 * check that it is to the same destination
327 * and is still up. If not, free it and try again.
328 * The address family should also be checked in case of sharing the
333 if (ro
->ro_rt
&& (ro
->ro_rt
->generation_id
!= route_generation
) &&
334 ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0) && (ip
->ip_src
.s_addr
!= INADDR_ANY
) &&
335 (ifa_foraddr(ip
->ip_src
.s_addr
) == 0)) {
336 error
= EADDRNOTAVAIL
;
340 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
341 dst
->sin_family
!= AF_INET
||
342 dst
->sin_addr
.s_addr
!= pkt_dst
.s_addr
)) {
344 ro
->ro_rt
= (struct rtentry
*)0;
346 if (ro
->ro_rt
== 0) {
347 bzero(dst
, sizeof(*dst
));
348 dst
->sin_family
= AF_INET
;
349 dst
->sin_len
= sizeof(*dst
);
350 dst
->sin_addr
= pkt_dst
;
353 * If routing to interface only,
354 * short circuit routing lookup.
356 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
357 #define sintosa(sin) ((struct sockaddr *)(sin))
358 if (flags
& IP_ROUTETOIF
) {
360 ifafree(&ia
->ia_ifa
);
361 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0) {
362 if ((ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
363 ipstat
.ips_noroute
++;
370 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
373 * If this is the case, we probably don't want to allocate
374 * a protocol-cloned route since we didn't get one from the
375 * ULP. This lets TCP do its thing, while not burdening
376 * forwarding or ICMP with the overhead of cloning a route.
377 * Of course, we still want to do any cloning requested by
378 * the link layer, as this is probably required in all cases
379 * for correct operation (as it is for ARP).
382 rtalloc_ign(ro
, RTF_PRCLONING
);
383 if (ro
->ro_rt
== 0) {
384 ipstat
.ips_noroute
++;
385 error
= EHOSTUNREACH
;
389 ifafree(&ia
->ia_ifa
);
390 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
393 ifp
= ro
->ro_rt
->rt_ifp
;
395 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
396 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
397 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
398 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
400 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
402 if (IN_MULTICAST(ntohl(pkt_dst
.s_addr
))) {
403 struct in_multi
*inm
;
405 m
->m_flags
|= M_MCAST
;
407 * IP destination address is multicast. Make sure "dst"
408 * still points to the address in "ro". (It may have been
409 * changed to point to a gateway address, above.)
411 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
413 * See if the caller provided any multicast options
416 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= imo
->imo_multicast_ttl
;
417 if (imo
->imo_multicast_ifp
!= NULL
) {
418 ifp
= imo
->imo_multicast_ifp
;
420 if (imo
->imo_multicast_vif
!= -1 &&
421 ((flags
& IP_RAWOUTPUT
) == 0 || ip
->ip_src
.s_addr
== INADDR_ANY
))
423 ip_mcast_src(imo
->imo_multicast_vif
);
425 if ((flags
& IP_RAWOUTPUT
) == 0) ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
427 * Confirm that the outgoing interface supports multicast.
429 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
430 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
431 ipstat
.ips_noroute
++;
437 * If source address not specified yet, use address
438 * of outgoing interface.
440 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
441 register struct in_ifaddr
*ia1
;
443 TAILQ_FOREACH(ia1
, &in_ifaddrhead
, ia_link
)
444 if (ia1
->ia_ifp
== ifp
) {
445 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
449 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
455 ifnet_lock_shared(ifp
);
456 IN_LOOKUP_MULTI(pkt_dst
, ifp
, inm
);
457 ifnet_lock_done(ifp
);
459 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
461 * If we belong to the destination multicast group
462 * on the outgoing interface, and the caller did not
463 * forbid loopback, loop back a copy.
465 if (!TAILQ_EMPTY(&ipv4_filters
)) {
466 struct ipfilter
*filter
;
467 int seen
= (inject_filter_ref
== 0);
468 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
472 ipf_pktopts
.ippo_mcast_ifnet
= imo
->imo_multicast_ifp
;
473 ipf_pktopts
.ippo_mcast_ttl
= imo
->imo_multicast_ttl
;
474 ipf_pktopts
.ippo_mcast_loop
= imo
->imo_multicast_loop
;
477 lck_mtx_unlock(ip_mutex
);
479 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
481 if ((struct ipfilter
*)inject_filter_ref
== filter
)
483 } else if (filter
->ipf_filter
.ipf_output
) {
485 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
486 if (result
== EJUSTRETURN
) {
492 lck_mtx_lock(ip_mutex
);
497 lck_mtx_lock(ip_mutex
);
501 ip_mloopback(ifp
, m
, dst
, hlen
);
505 * If we are acting as a multicast router, perform
506 * multicast forwarding as if the packet had just
507 * arrived on the interface to which we are about
508 * to send. The multicast forwarding function
509 * recursively calls this function, using the
510 * IP_FORWARDING flag to prevent infinite recursion.
512 * Multicasts that are looped back by ip_mloopback(),
513 * above, will be forwarded by the ip_input() routine,
516 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
518 * Check if rsvp daemon is running. If not, don't
519 * set ip_moptions. This ensures that the packet
520 * is multicast and not just sent down one link
521 * as prescribed by rsvpd.
525 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
527 lck_mtx_unlock(ip_mutex
);
534 * Multicasts with a time-to-live of zero may be looped-
535 * back, above, but must not be transmitted on a network.
536 * Also, multicasts addressed to the loopback interface
537 * are not sent -- the above call to ip_mloopback() will
538 * loop back a copy if this host actually belongs to the
539 * destination group on the loopback interface.
541 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
543 lck_mtx_unlock(ip_mutex
);
551 * If source address not specified yet, use address
552 * of outgoing interface.
554 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
555 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
556 #if IPFIREWALL_FORWARD
557 /* Keep note that we did this - if the firewall changes
558 * the next-hop, our interface may change, changing the
559 * default source IP. It's a shame so much effort happens
563 #endif /* IPFIREWALL_FORWARD */
568 * Look for broadcast address and
569 * and verify user is allowed to send
573 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
574 error
= EADDRNOTAVAIL
;
577 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
581 /* don't allow broadcast messages to be fragmented */
582 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
586 m
->m_flags
|= M_BCAST
;
588 m
->m_flags
&= ~M_BCAST
;
593 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
595 if (IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) || IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))) {
596 ip_linklocal_stat
.iplls_out_total
++;
597 if (ip
->ip_ttl
!= MAXTTL
) {
598 ip_linklocal_stat
.iplls_out_badttl
++;
604 if (!didfilter
&& !TAILQ_EMPTY(&ipv4_filters
)) {
605 struct ipfilter
*filter
;
606 int seen
= (inject_filter_ref
== 0);
608 lck_mtx_unlock(ip_mutex
);
610 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
612 if ((struct ipfilter
*)inject_filter_ref
== filter
)
614 } else if (filter
->ipf_filter
.ipf_output
) {
616 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
617 if (result
== EJUSTRETURN
) {
623 lck_mtx_lock(ip_mutex
);
629 lck_mtx_lock(ip_mutex
);
633 /* temporary for testing only: bypass ipsec alltogether */
635 if (ipsec_bypass
!= 0 || (flags
& IP_NOIPSEC
) != 0)
638 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
640 lck_mtx_lock(sadb_mutex
);
642 /* get SP for this packet */
644 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
646 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
649 ipsecstat
.out_inval
++;
650 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 0,0,0,0,0);
651 lck_mtx_unlock(sadb_mutex
);
658 switch (sp
->policy
) {
659 case IPSEC_POLICY_DISCARD
:
661 * This packet is just discarded.
663 ipsecstat
.out_polvio
++;
664 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 1,0,0,0,0);
665 lck_mtx_unlock(sadb_mutex
);
668 case IPSEC_POLICY_BYPASS
:
669 case IPSEC_POLICY_NONE
:
670 /* no need to do IPsec. */
671 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 2,0,0,0,0);
672 lck_mtx_unlock(sadb_mutex
);
675 case IPSEC_POLICY_IPSEC
:
676 if (sp
->req
== NULL
) {
677 /* acquire a policy */
678 error
= key_spdacquire(sp
);
679 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 3,0,0,0,0);
680 lck_mtx_unlock(sadb_mutex
);
685 case IPSEC_POLICY_ENTRUST
:
687 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
690 struct ipsec_output_state state
;
691 bzero(&state
, sizeof(state
));
693 if (flags
& IP_ROUTETOIF
) {
695 bzero(&iproute
, sizeof(iproute
));
698 state
.dst
= (struct sockaddr
*)dst
;
704 * delayed checksums are not currently compatible with IPsec
706 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
708 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
714 lck_mtx_unlock(ip_mutex
);
715 error
= ipsec4_output(&state
, sp
, flags
);
716 lck_mtx_unlock(sadb_mutex
);
717 lck_mtx_lock(ip_mutex
);
721 if (flags
& IP_ROUTETOIF
) {
723 * if we have tunnel mode SA, we may need to ignore
726 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
727 flags
&= ~IP_ROUTETOIF
;
733 dst
= (struct sockaddr_in
*)state
.dst
;
735 /* mbuf is already reclaimed in ipsec4_output. */
745 printf("ip4_output (ipsec): error code %d\n", error
);
748 /* don't show these error codes to the user */
752 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 4,0,0,0,0);
757 /* be sure to update variables that are affected by ipsec4_output() */
758 ip
= mtod(m
, struct ip
*);
761 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
763 hlen
= ip
->ip_hl
<< 2;
765 /* Check that there wasn't a route change and src is still valid */
767 if (ro
->ro_rt
->generation_id
!= route_generation
) {
768 if (ifa_foraddr(ip
->ip_src
.s_addr
) == 0 && ((flags
& (IP_ROUTETOIF
| IP_FORWARDING
)) == 0)) {
769 error
= EADDRNOTAVAIL
;
770 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 5,0,0,0,0);
777 if (ro
->ro_rt
== NULL
) {
778 if ((flags
& IP_ROUTETOIF
) == 0) {
780 "can't update route after IPsec processing\n");
781 error
= EHOSTUNREACH
; /*XXX*/
782 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 6,0,0,0,0);
787 ifafree(&ia
->ia_ifa
);
788 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
791 ifp
= ro
->ro_rt
->rt_ifp
;
794 /* make it flipped, again. */
797 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT
| DBG_FUNC_END
, 7,0xff,0xff,0xff,0xff);
799 /* Pass to filters again */
800 if (!TAILQ_EMPTY(&ipv4_filters
)) {
801 struct ipfilter
*filter
;
803 lck_mtx_unlock(ip_mutex
);
805 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
806 if (filter
->ipf_filter
.ipf_output
) {
808 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, 0);
809 if (result
== EJUSTRETURN
) {
815 lck_mtx_lock(ip_mutex
);
821 lck_mtx_lock(ip_mutex
);
828 * - Xlate: translate packet's addr/port (NAT).
829 * - Firewall: deny/allow/etc.
830 * - Wrap: fake packet's addr/port <unimpl.>
831 * - Encapsulate: put it in another IP and send out. <unimp.>
836 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
) {
837 lck_mtx_unlock(ip_mutex
);
840 ip
= mtod(m0
= m
= m1
, struct ip
*);
844 * Check with the firewall...
845 * but not if we are already being fwd'd from a firewall.
847 if (fw_enable
&& IPFW_LOADED
&& !args
.next_hop
) {
848 struct sockaddr_in
*old
= dst
;
853 off
= ip_fw_chk_ptr(&args
);
858 * On return we must do the following:
859 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
860 * 1<=off<= 0xffff -> DIVERT
861 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
862 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
863 * dst != old -> IPFIREWALL_FORWARD
864 * off==0, dst==old -> accept
865 * If some of the above modules is not compiled in, then
866 * we should't have to check the corresponding condition
867 * (because the ipfw control socket should not accept
868 * unsupported rules), but better play safe and drop
869 * packets in case of doubt.
872 if ( (off
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) {
876 lck_mtx_unlock(ip_mutex
);
879 ip
= mtod(m
, struct ip
*);
880 if (off
== 0 && dst
== old
) /* common case */
883 if (DUMMYNET_LOADED
&& (off
& IP_FW_PORT_DYNT_FLAG
) != 0) {
885 * pass the pkt to dummynet. Need to include
886 * pipe number, m, ifp, ro, dst because these are
887 * not recomputed in the next pass.
888 * All other parameters have been already used and
889 * so they are not needed anymore.
890 * XXX note: if the ifp or ro entry are deleted
891 * while a pkt is in dummynet, we are in trouble!
897 lck_mtx_unlock(ip_mutex
);
898 error
= ip_dn_io_ptr(m
, off
& 0xffff, DN_TO_IP_OUT
,
902 #endif /* DUMMYNET */
904 if (off
!= 0 && (off
& IP_FW_PORT_DYNT_FLAG
) == 0) {
905 struct mbuf
*clone
= NULL
;
907 /* Clone packet if we're doing a 'tee' */
908 if ((off
& IP_FW_PORT_TEE_FLAG
) != 0)
909 clone
= m_dup(m
, M_DONTWAIT
);
912 * delayed checksums are not currently compatible
913 * with divert sockets.
915 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
917 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
920 /* Restore packet header fields to original values */
924 /* Deliver packet to divert input routine */
925 divert_packet(m
, 0, off
& 0xffff, args
.divert_rule
);
927 /* If 'tee', continue with original packet */
930 ip
= mtod(m
, struct ip
*);
933 lck_mtx_unlock(ip_mutex
);
938 #if IPFIREWALL_FORWARD
939 /* Here we check dst to make sure it's directly reachable on the
940 * interface we previously thought it was.
941 * If it isn't (which may be likely in some situations) we have
942 * to re-route it (ie, find a route for the next-hop and the
943 * associated interface) and set them here. This is nested
944 * forwarding which in most cases is undesirable, except where
945 * such control is nigh impossible. So we do it here.
948 if (off
== 0 && old
!= dst
) {
949 struct in_ifaddr
*ia_fw
;
951 /* It's changed... */
952 /* There must be a better way to do this next line... */
953 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
954 #if IPFIREWALL_FORWARD_DEBUG
955 printf("IPFIREWALL_FORWARD: New dst ip: ");
956 print_ip(dst
->sin_addr
);
960 * We need to figure out if we have been forwarded
961 * to a local socket. If so then we should somehow
962 * "loop back" to ip_input, and get directed to the
963 * PCB as if we had received this packet. This is
964 * because it may be dificult to identify the packets
965 * you want to forward until they are being output
966 * and have selected an interface. (e.g. locally
967 * initiated packets) If we used the loopback inteface,
968 * we would not be able to control what happens
969 * as the packet runs through ip_input() as
970 * it is done through a ISR.
972 TAILQ_FOREACH(ia_fw
, &in_ifaddrhead
, ia_link
) {
974 * If the addr to forward to is one
975 * of ours, we pretend to
976 * be the destination for this packet.
978 if (IA_SIN(ia_fw
)->sin_addr
.s_addr
==
979 dst
->sin_addr
.s_addr
)
983 /* tell ip_input "dont filter" */
984 struct m_tag
*fwd_tag
;
985 struct ip_fwd_tag
*ipfwd_tag
;
987 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
988 sizeof(struct sockaddr_in
), M_NOWAIT
);
989 if (fwd_tag
== NULL
) {
994 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
995 ipfwd_tag
->next_hop
= args
.next_hop
;
997 m_tag_prepend(m
, fwd_tag
);
999 if (m
->m_pkthdr
.rcvif
== NULL
)
1000 m
->m_pkthdr
.rcvif
= ifunit("lo0");
1001 if ((~IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) &
1002 m
->m_pkthdr
.csum_flags
) == 0) {
1003 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1004 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1005 m
->m_pkthdr
.csum_flags
|=
1006 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
1007 m
->m_pkthdr
.csum_data
= 0xffff;
1009 m
->m_pkthdr
.csum_flags
|=
1010 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
1012 else if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1013 in_delayed_cksum(m
);
1014 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1015 ip
->ip_sum
= in_cksum(m
, hlen
);
1020 lck_mtx_unlock(ip_mutex
);
1022 /* we need to call dlil_output to run filters
1023 * and resync to avoid recursion loops.
1026 dlil_output(lo_ifp
, PF_INET
, m
, 0, (struct sockaddr
*)dst
, 0);
1029 printf("ip_output: no loopback ifp for forwarding!!!\n");
1033 /* Some of the logic for this was
1034 * nicked from above.
1036 * This rewrites the cached route in a local PCB.
1037 * Is this what we want to do?
1039 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
1042 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
1044 if (ro_fwd
->ro_rt
== 0) {
1045 ipstat
.ips_noroute
++;
1046 error
= EHOSTUNREACH
;
1050 ia_fw
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
1051 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
1052 ro_fwd
->ro_rt
->rt_use
++;
1053 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
1054 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
1055 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
1057 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
1059 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
1061 ro
->ro_rt
= ro_fwd
->ro_rt
;
1062 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
1065 * If we added a default src ip earlier,
1066 * which would have been gotten from the-then
1067 * interface, do it again, from the new one.
1069 if (fwd_rewrite_src
)
1070 ip
->ip_src
= IA_SIN(ia_fw
)->sin_addr
;
1073 #endif /* IPFIREWALL_FORWARD */
1075 * if we get here, none of the above matches, and
1076 * we have to drop the pkt
1079 error
= EACCES
; /* not sure this is the right error msg */
1080 lck_mtx_unlock(ip_mutex
);
1086 /* Do not allow loopback address to wind up on a wire */
1087 if ((ifp
->if_flags
& IFF_LOOPBACK
) == 0 &&
1088 ((ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
1089 (ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
)) {
1090 ipstat
.ips_badaddr
++;
1093 * Do not simply drop the packet just like a firewall -- we want the
1094 * the application to feel the pain.
1095 * Return ENETUNREACH like ip6_output does in some similar cases.
1096 * This can startle the otherwise clueless process that specifies
1097 * loopback as the source address.
1099 error
= ENETUNREACH
;
1100 lck_mtx_unlock(ip_mutex
);
1104 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
1105 sw_csum
= m
->m_pkthdr
.csum_flags
1106 & ~IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1108 if ((ifp
->if_hwassist
& CSUM_TCP_SUM16
) != 0) {
1110 * Special case code for GMACE
1111 * frames that can be checksumed by GMACE SUM16 HW:
1112 * frame >64, no fragments, no UDP
1114 if (apple_hwcksum_tx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP
)
1115 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)) {
1116 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
1117 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
1118 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
1119 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
1120 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
1121 m
->m_pkthdr
.csum_data
+= offset
;
1122 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
1125 /* let the software handle any UDP or TCP checksums */
1126 sw_csum
|= (CSUM_DELAY_DATA
& m
->m_pkthdr
.csum_flags
);
1130 if (sw_csum
& CSUM_DELAY_DATA
) {
1131 in_delayed_cksum(m
);
1132 sw_csum
&= ~CSUM_DELAY_DATA
;
1133 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1136 m
->m_pkthdr
.csum_flags
&= IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
);
1139 * If small enough for interface, or the interface will take
1140 * care of the fragmentation for us, can just send directly.
1142 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
||
1143 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
1147 if (sw_csum
& CSUM_DELAY_IP
) {
1148 ip
->ip_sum
= in_cksum(m
, hlen
);
1152 /* Record statistics for this interface address. */
1153 if (!(flags
& IP_FORWARDING
) && ia
!= NULL
) {
1154 ia
->ia_ifa
.if_opackets
++;
1155 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1160 /* clean ipsec history once it goes out of the node */
1161 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1164 if (packetchain
== 0) {
1165 lck_mtx_unlock(ip_mutex
);
1166 error
= dlil_output(ifp
, PF_INET
, m
, (void *) ro
->ro_rt
,
1167 (struct sockaddr
*)dst
, 0);
1170 else { /* packet chaining allows us to reuse the route for all packets */
1173 if (pktcnt
> ip_maxchainsent
)
1174 ip_maxchainsent
= pktcnt
;
1176 lck_mtx_unlock(ip_mutex
);
1177 error
= dlil_output_list(ifp
, PF_INET
, packetlist
, (void *) ro
->ro_rt
,
1178 (struct sockaddr
*)dst
, 0);
1189 * Too large for interface; fragment if possible.
1190 * Must be able to put at least 8 bytes per fragment.
1192 if (ip
->ip_off
& IP_DF
) {
1195 * This case can happen if the user changed the MTU
1196 * of an interface after enabling IP on it. Because
1197 * most netifs don't keep track of routes pointing to
1198 * them, there is no way for one to update all its
1199 * routes when the MTU is changed.
1201 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
1202 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
1203 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
1204 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
1206 ipstat
.ips_cantfrag
++;
1209 len
= (ifp
->if_mtu
- hlen
) &~ 7;
1216 * if the interface will not calculate checksums on
1217 * fragmented packets, then do it here.
1219 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
1220 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
1221 in_delayed_cksum(m
);
1223 lck_mtx_unlock(ip_mutex
);
1226 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1231 int mhlen
, firstlen
= len
;
1232 struct mbuf
**mnext
= &m
->m_nextpkt
;
1236 * Loop through length of segment after first fragment,
1237 * make new header and copy data of each part and link onto chain.
1240 mhlen
= sizeof (struct ip
);
1241 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
1242 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1245 ipstat
.ips_odropped
++;
1248 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
1249 m
->m_data
+= max_linkhdr
;
1250 mhip
= mtod(m
, struct ip
*);
1252 if (hlen
> sizeof (struct ip
)) {
1253 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
1254 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
1257 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
1258 if (ip
->ip_off
& IP_MF
)
1259 mhip
->ip_off
|= IP_MF
;
1260 if (off
+ len
>= (u_short
)ip
->ip_len
)
1261 len
= (u_short
)ip
->ip_len
- off
;
1263 mhip
->ip_off
|= IP_MF
;
1264 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
1265 m
->m_next
= m_copy(m0
, off
, len
);
1266 if (m
->m_next
== 0) {
1268 error
= ENOBUFS
; /* ??? */
1269 ipstat
.ips_odropped
++;
1272 m
->m_pkthdr
.len
= mhlen
+ len
;
1273 m
->m_pkthdr
.rcvif
= 0;
1274 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
1275 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1276 HTONS(mhip
->ip_off
);
1278 if (sw_csum
& CSUM_DELAY_IP
) {
1279 mhip
->ip_sum
= in_cksum(m
, mhlen
);
1282 mnext
= &m
->m_nextpkt
;
1285 ipstat
.ips_ofragments
+= nfrags
;
1287 /* set first/last markers for fragment chain */
1288 m
->m_flags
|= M_LASTFRAG
;
1289 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1290 m0
->m_pkthdr
.csum_data
= nfrags
;
1293 * Update first fragment by trimming what's been copied out
1294 * and updating header, then send each fragment (in order).
1297 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
1298 m
->m_pkthdr
.len
= hlen
+ firstlen
;
1299 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
1300 ip
->ip_off
|= IP_MF
;
1303 if (sw_csum
& CSUM_DELAY_IP
) {
1304 ip
->ip_sum
= in_cksum(m
, hlen
);
1308 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1309 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1311 lck_mtx_unlock(ip_mutex
);
1312 for (m
= m0
; m
; m
= m0
) {
1316 /* clean ipsec history once it goes out of the node */
1317 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0)
1322 /* Record statistics for this interface address. */
1324 ia
->ia_ifa
.if_opackets
++;
1325 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1328 if ((packetchain
!= 0) && (pktcnt
> 0))
1329 panic("ip_output: mix of packet in packetlist is wrong=%x", packetlist
);
1330 error
= dlil_output(ifp
, PF_INET
, m
, (void *) ro
->ro_rt
,
1331 (struct sockaddr
*)dst
, 0);
1337 ipstat
.ips_fragmented
++;
1341 ifafree(&ia
->ia_ifa
);
1345 if (ipsec_bypass
== 0 && (flags
& IP_NOIPSEC
) == 0) {
1346 if (ro
== &iproute
&& ro
->ro_rt
) {
1351 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1352 printf("DP ip_output call free SP:%x\n", sp
));
1353 lck_mtx_lock(sadb_mutex
);
1355 lck_mtx_unlock(sadb_mutex
);
1360 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1364 lck_mtx_unlock(ip_mutex
);
1369 in_delayed_cksum_offset(struct mbuf
*m
, int ip_offset
)
1372 u_short csum
, offset
;
1374 while (ip_offset
> m
->m_len
) {
1375 ip_offset
-= m
->m_len
;
1378 printf("in_delayed_cksum_withoffset failed - ip_offset wasn't in the packet\n");
1383 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1384 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1385 m
->m_len
, ip_offset
, ip
->ip_p
);
1388 * this shouldn't happen
1390 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
1395 m
->m_len
-= ip_offset
;
1396 m
->m_data
+= ip_offset
;
1399 ip
= mtod(m
, struct ip
*);
1400 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1401 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1402 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1404 offset
+= m
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1408 if (M_LEADINGSPACE(m
) < ip_offset
)
1409 panic("in_delayed_cksum_withoffset - chain modified!\n");
1410 m
->m_len
+= ip_offset
;
1411 m
->m_data
-= ip_offset
;
1414 if (offset
> ip
->ip_len
) /* bogus offset */
1417 if (offset
+ ip_offset
+ sizeof(u_short
) > m
->m_len
) {
1418 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1419 m
->m_len
, offset
+ ip_offset
, ip
->ip_p
);
1422 * this shouldn't happen, but if it does, the
1423 * correct behavior may be to insert the checksum
1424 * in the existing chain instead of rearranging it.
1426 m
= m_pullup(m
, offset
+ ip_offset
+ sizeof(u_short
));
1428 *(u_short
*)(m
->m_data
+ offset
+ ip_offset
) = csum
;
1432 in_delayed_cksum(struct mbuf
*m
)
1434 in_delayed_cksum_offset(m
, 0);
1438 in_cksum_offset(struct mbuf
* m
, size_t ip_offset
)
1440 struct ip
* ip
= NULL
;
1443 while (ip_offset
> m
->m_len
) {
1444 ip_offset
-= m
->m_len
;
1447 printf("in_cksum_offset failed - ip_offset wasn't in the packet\n");
1452 if (ip_offset
+ sizeof(struct ip
) > m
->m_len
) {
1453 printf("in_cksum_offset - delayed m_pullup, m->len: %d off: %d\n",
1454 m
->m_len
, ip_offset
);
1457 * this shouldn't happen
1459 m
= m_pullup(m
, ip_offset
+ sizeof(struct ip
));
1464 m
->m_len
-= ip_offset
;
1465 m
->m_data
+= ip_offset
;
1468 ip
= mtod(m
, struct ip
*);
1471 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1473 hlen
= ip
->ip_hl
<< 2;
1477 ip
->ip_sum
= in_cksum(m
, hlen
);
1481 if (M_LEADINGSPACE(m
) < ip_offset
)
1482 panic("in_cksum_offset - chain modified!\n");
1483 m
->m_len
+= ip_offset
;
1484 m
->m_data
-= ip_offset
;
1489 * Insert IP options into preformed packet.
1490 * Adjust IP destination as required for IP source routing,
1491 * as indicated by a non-zero in_addr at the start of the options.
1493 * XXX This routine assumes that the packet has no options in place.
1495 static struct mbuf
*
1496 ip_insertoptions(m
, opt
, phlen
)
1497 register struct mbuf
*m
;
1501 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1503 register struct ip
*ip
= mtod(m
, struct ip
*);
1506 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1507 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1508 return (m
); /* XXX should fail */
1509 if (p
->ipopt_dst
.s_addr
)
1510 ip
->ip_dst
= p
->ipopt_dst
;
1511 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1512 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
1515 n
->m_pkthdr
.rcvif
= 0;
1516 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1517 m
->m_len
-= sizeof(struct ip
);
1518 m
->m_data
+= sizeof(struct ip
);
1521 m
->m_len
= optlen
+ sizeof(struct ip
);
1522 m
->m_data
+= max_linkhdr
;
1523 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1525 m
->m_data
-= optlen
;
1527 m
->m_pkthdr
.len
+= optlen
;
1528 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1530 ip
= mtod(m
, struct ip
*);
1531 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1532 *phlen
= sizeof(struct ip
) + optlen
;
1533 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1534 ip
->ip_len
+= optlen
;
1539 * Copy options from ip to jp,
1540 * omitting those not copied during fragmentation.
1546 register u_char
*cp
, *dp
;
1547 int opt
, optlen
, cnt
;
1549 cp
= (u_char
*)(ip
+ 1);
1550 dp
= (u_char
*)(jp
+ 1);
1551 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1552 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1554 if (opt
== IPOPT_EOL
)
1556 if (opt
== IPOPT_NOP
) {
1557 /* Preserve for IP mcast tunnel's LSRR alignment. */
1563 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1564 panic("malformed IPv4 option passed to ip_optcopy");
1566 optlen
= cp
[IPOPT_OLEN
];
1568 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1569 panic("malformed IPv4 option passed to ip_optcopy");
1571 /* bogus lengths should have been caught by ip_dooptions */
1574 if (IPOPT_COPIED(opt
)) {
1575 bcopy(cp
, dp
, optlen
);
1579 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1585 * IP socket option processing.
1588 ip_ctloutput(so
, sopt
)
1590 struct sockopt
*sopt
;
1592 struct inpcb
*inp
= sotoinpcb(so
);
1596 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1600 switch (sopt
->sopt_dir
) {
1602 switch (sopt
->sopt_name
) {
1609 if (sopt
->sopt_valsize
> MLEN
) {
1613 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1618 m
->m_len
= sopt
->sopt_valsize
;
1619 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1624 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1631 case IP_RECVRETOPTS
:
1632 case IP_RECVDSTADDR
:
1635 #if defined(NFAITH) && NFAITH > 0
1638 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1643 switch (sopt
->sopt_name
) {
1645 inp
->inp_ip_tos
= optval
;
1649 inp
->inp_ip_ttl
= optval
;
1651 #define OPTSET(bit) \
1653 inp->inp_flags |= bit; \
1655 inp->inp_flags &= ~bit;
1658 OPTSET(INP_RECVOPTS
);
1661 case IP_RECVRETOPTS
:
1662 OPTSET(INP_RECVRETOPTS
);
1665 case IP_RECVDSTADDR
:
1666 OPTSET(INP_RECVDSTADDR
);
1674 OPTSET(INP_RECVTTL
);
1677 #if defined(NFAITH) && NFAITH > 0
1686 case IP_MULTICAST_IF
:
1687 case IP_MULTICAST_VIF
:
1688 case IP_MULTICAST_TTL
:
1689 case IP_MULTICAST_LOOP
:
1690 case IP_ADD_MEMBERSHIP
:
1691 case IP_DROP_MEMBERSHIP
:
1692 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1696 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1702 case IP_PORTRANGE_DEFAULT
:
1703 inp
->inp_flags
&= ~(INP_LOWPORT
);
1704 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1707 case IP_PORTRANGE_HIGH
:
1708 inp
->inp_flags
&= ~(INP_LOWPORT
);
1709 inp
->inp_flags
|= INP_HIGHPORT
;
1712 case IP_PORTRANGE_LOW
:
1713 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1714 inp
->inp_flags
|= INP_LOWPORT
;
1724 case IP_IPSEC_POLICY
:
1732 if (sopt
->sopt_valsize
> MCLBYTES
) {
1736 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1738 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1740 priv
= (sopt
->sopt_p
!= NULL
&&
1741 proc_suser(sopt
->sopt_p
) != 0) ? 0 : 1;
1743 req
= mtod(m
, caddr_t
);
1746 optname
= sopt
->sopt_name
;
1747 lck_mtx_lock(sadb_mutex
);
1748 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1749 lck_mtx_unlock(sadb_mutex
);
1756 error
= ENOPROTOOPT
;
1762 switch (sopt
->sopt_name
) {
1765 if (inp
->inp_options
)
1766 error
= sooptcopyout(sopt
,
1767 mtod(inp
->inp_options
,
1769 inp
->inp_options
->m_len
);
1771 sopt
->sopt_valsize
= 0;
1777 case IP_RECVRETOPTS
:
1778 case IP_RECVDSTADDR
:
1782 #if defined(NFAITH) && NFAITH > 0
1785 switch (sopt
->sopt_name
) {
1788 optval
= inp
->inp_ip_tos
;
1792 optval
= inp
->inp_ip_ttl
;
1795 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1798 optval
= OPTBIT(INP_RECVOPTS
);
1801 case IP_RECVRETOPTS
:
1802 optval
= OPTBIT(INP_RECVRETOPTS
);
1805 case IP_RECVDSTADDR
:
1806 optval
= OPTBIT(INP_RECVDSTADDR
);
1810 optval
= OPTBIT(INP_RECVIF
);
1814 optval
= OPTBIT(INP_RECVTTL
);
1818 if (inp
->inp_flags
& INP_HIGHPORT
)
1819 optval
= IP_PORTRANGE_HIGH
;
1820 else if (inp
->inp_flags
& INP_LOWPORT
)
1821 optval
= IP_PORTRANGE_LOW
;
1826 #if defined(NFAITH) && NFAITH > 0
1828 optval
= OPTBIT(INP_FAITH
);
1832 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1835 case IP_MULTICAST_IF
:
1836 case IP_MULTICAST_VIF
:
1837 case IP_MULTICAST_TTL
:
1838 case IP_MULTICAST_LOOP
:
1839 case IP_ADD_MEMBERSHIP
:
1840 case IP_DROP_MEMBERSHIP
:
1841 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1845 case IP_IPSEC_POLICY
:
1847 struct mbuf
*m
= NULL
;
1852 req
= mtod(m
, caddr_t
);
1855 lck_mtx_lock(sadb_mutex
);
1856 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
1857 lck_mtx_unlock(sadb_mutex
);
1859 error
= soopt_mcopyout(sopt
, m
); /* XXX */
1867 error
= ENOPROTOOPT
;
1876 * Set up IP options in pcb for insertion in output packets.
1877 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1878 * with destination address if source routed.
1881 ip_pcbopts(optname
, pcbopt
, m
)
1883 struct mbuf
**pcbopt
;
1884 register struct mbuf
*m
;
1886 register int cnt
, optlen
;
1887 register u_char
*cp
;
1890 /* turn off any old options */
1892 (void)m_free(*pcbopt
);
1894 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
1896 * Only turning off any previous options.
1904 if (m
->m_len
% sizeof(int32_t))
1908 * IP first-hop destination address will be stored before
1909 * actual options; move other options back
1910 * and clear it when none present.
1912 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1915 m
->m_len
+= sizeof(struct in_addr
);
1916 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1917 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
1918 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1920 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1921 opt
= cp
[IPOPT_OPTVAL
];
1922 if (opt
== IPOPT_EOL
)
1924 if (opt
== IPOPT_NOP
)
1927 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1929 optlen
= cp
[IPOPT_OLEN
];
1930 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1941 * user process specifies route as:
1943 * D must be our final destination (but we can't
1944 * check that since we may not have connected yet).
1945 * A is first hop destination, which doesn't appear in
1946 * actual IP option, but is stored before the options.
1948 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1950 m
->m_len
-= sizeof(struct in_addr
);
1951 cnt
-= sizeof(struct in_addr
);
1952 optlen
-= sizeof(struct in_addr
);
1953 cp
[IPOPT_OLEN
] = optlen
;
1955 * Move first hop before start of options.
1957 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1958 sizeof(struct in_addr
));
1960 * Then copy rest of options back
1961 * to close up the deleted entry.
1963 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
1964 sizeof(struct in_addr
)),
1965 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
1966 (unsigned)cnt
+ sizeof(struct in_addr
));
1970 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1982 * The whole multicast option thing needs to be re-thought.
1983 * Several of these options are equally applicable to non-multicast
1984 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1985 * standard option (IP_TTL).
1989 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1991 static struct ifnet
*
1992 ip_multicast_if(a
, ifindexp
)
2001 if (ntohl(a
->s_addr
) >> 24 == 0) {
2002 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
2003 ifnet_head_lock_shared();
2004 if (ifindex
< 0 || if_index
< ifindex
) {
2008 ifp
= ifindex2ifnet
[ifindex
];
2011 *ifindexp
= ifindex
;
2013 INADDR_TO_IFP(*a
, ifp
);
2019 * Set the IP multicast options in response to user setsockopt().
2022 ip_setmoptions(sopt
, imop
)
2023 struct sockopt
*sopt
;
2024 struct ip_moptions
**imop
;
2028 struct in_addr addr
;
2029 struct ip_mreq mreq
;
2030 struct ifnet
*ifp
= NULL
;
2031 struct ip_moptions
*imo
= *imop
;
2036 * No multicast option buffer attached to the pcb;
2037 * allocate one and initialize to default values.
2039 error
= ip_createmoptions(imop
);
2045 switch (sopt
->sopt_name
) {
2046 /* store an index number for the vif you wanna use in the send */
2047 case IP_MULTICAST_VIF
:
2048 if (legal_vif_num
== 0) {
2052 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2055 if (!legal_vif_num(i
) && (i
!= -1)) {
2059 imo
->imo_multicast_vif
= i
;
2062 case IP_MULTICAST_IF
:
2064 * Select the interface for outgoing multicast packets.
2066 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
2070 * INADDR_ANY is used to remove a previous selection.
2071 * When no interface is selected, a default one is
2072 * chosen every time a multicast packet is sent.
2074 if (addr
.s_addr
== INADDR_ANY
) {
2075 imo
->imo_multicast_ifp
= NULL
;
2079 * The selected interface is identified by its local
2080 * IP address. Find the interface and confirm that
2081 * it supports multicasting.
2083 ifp
= ip_multicast_if(&addr
, &ifindex
);
2084 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2085 error
= EADDRNOTAVAIL
;
2088 imo
->imo_multicast_ifp
= ifp
;
2090 imo
->imo_multicast_addr
= addr
;
2092 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2095 case IP_MULTICAST_TTL
:
2097 * Set the IP time-to-live for outgoing multicast packets.
2098 * The original multicast API required a char argument,
2099 * which is inconsistent with the rest of the socket API.
2100 * We allow either a char or an int.
2102 if (sopt
->sopt_valsize
== 1) {
2104 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
2107 imo
->imo_multicast_ttl
= ttl
;
2110 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
2117 imo
->imo_multicast_ttl
= ttl
;
2121 case IP_MULTICAST_LOOP
:
2123 * Set the loopback flag for outgoing multicast packets.
2124 * Must be zero or one. The original multicast API required a
2125 * char argument, which is inconsistent with the rest
2126 * of the socket API. We allow either a char or an int.
2128 if (sopt
->sopt_valsize
== 1) {
2130 error
= sooptcopyin(sopt
, &loop
, 1, 1);
2133 imo
->imo_multicast_loop
= !!loop
;
2136 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
2140 imo
->imo_multicast_loop
= !!loop
;
2144 case IP_ADD_MEMBERSHIP
:
2146 * Add a multicast group membership.
2147 * Group must be a valid IP multicast address.
2149 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2153 error
= ip_addmembership(imo
, &mreq
);
2156 case IP_DROP_MEMBERSHIP
:
2158 * Drop a multicast group membership.
2159 * Group must be a valid IP multicast address.
2161 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
2165 error
= ip_dropmembership(imo
, &mreq
);
2174 * If all options have default values, no need to keep the mbuf.
2176 if (imo
->imo_multicast_ifp
== NULL
&&
2177 imo
->imo_multicast_vif
== -1 &&
2178 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2179 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2180 imo
->imo_num_memberships
== 0) {
2181 FREE(*imop
, M_IPMOPTS
);
2189 * Set the IP multicast options in response to user setsockopt().
2191 __private_extern__
int
2193 struct ip_moptions
**imop
)
2195 struct ip_moptions
*imo
;
2196 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
2202 imo
->imo_multicast_ifp
= NULL
;
2203 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
2204 imo
->imo_multicast_vif
= -1;
2205 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2206 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
2207 imo
->imo_num_memberships
= 0;
2213 * Add membership to an IPv4 multicast.
2215 __private_extern__
int
2217 struct ip_moptions
*imo
,
2218 struct ip_mreq
*mreq
)
2221 struct sockaddr_in
*dst
;
2222 struct ifnet
*ifp
= NULL
;
2226 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2231 * If no interface address was provided, use the interface of
2232 * the route to the given multicast address.
2234 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
) {
2235 bzero((caddr_t
)&ro
, sizeof(ro
));
2236 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
2237 dst
->sin_len
= sizeof(*dst
);
2238 dst
->sin_family
= AF_INET
;
2239 dst
->sin_addr
= mreq
->imr_multiaddr
;
2241 if (ro
.ro_rt
!= NULL
) {
2242 ifp
= ro
.ro_rt
->rt_ifp
;
2246 /* If there's no default route, try using loopback */
2247 mreq
->imr_interface
.s_addr
= INADDR_LOOPBACK
;
2252 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2256 * See if we found an interface, and confirm that it
2257 * supports multicast.
2259 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2260 error
= EADDRNOTAVAIL
;
2264 * See if the membership already exists or if all the
2265 * membership slots are full.
2267 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2268 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
2269 imo
->imo_membership
[i
]->inm_addr
.s_addr
2270 == mreq
->imr_multiaddr
.s_addr
)
2273 if (i
< imo
->imo_num_memberships
) {
2277 if (i
== IP_MAX_MEMBERSHIPS
) {
2278 error
= ETOOMANYREFS
;
2282 * Everything looks good; add a new record to the multicast
2283 * address list for the given interface.
2285 if ((imo
->imo_membership
[i
] =
2286 in_addmulti(&mreq
->imr_multiaddr
, ifp
)) == NULL
) {
2290 ++imo
->imo_num_memberships
;
2296 * Drop membership of an IPv4 multicast.
2298 __private_extern__
int
2300 struct ip_moptions
*imo
,
2301 struct ip_mreq
*mreq
)
2304 struct ifnet
* ifp
= NULL
;
2307 if (!IN_MULTICAST(ntohl(mreq
->imr_multiaddr
.s_addr
))) {
2313 * If an interface address was specified, get a pointer
2314 * to its ifnet structure.
2316 if (mreq
->imr_interface
.s_addr
== INADDR_ANY
)
2319 ifp
= ip_multicast_if(&mreq
->imr_interface
, NULL
);
2321 error
= EADDRNOTAVAIL
;
2326 * Find the membership in the membership array.
2328 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
2330 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
2331 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
2332 mreq
->imr_multiaddr
.s_addr
)
2335 if (i
== imo
->imo_num_memberships
) {
2336 error
= EADDRNOTAVAIL
;
2340 * Give up the multicast address record to which the
2341 * membership points.
2343 in_delmulti(&imo
->imo_membership
[i
]);
2345 * Remove the gap in the membership array.
2347 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
2348 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
2349 --imo
->imo_num_memberships
;
2355 * Return the IP multicast options in response to user getsockopt().
2358 ip_getmoptions(sopt
, imo
)
2359 struct sockopt
*sopt
;
2360 register struct ip_moptions
*imo
;
2362 struct in_addr addr
;
2363 struct in_ifaddr
*ia
;
2368 switch (sopt
->sopt_name
) {
2369 case IP_MULTICAST_VIF
:
2371 optval
= imo
->imo_multicast_vif
;
2374 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2377 case IP_MULTICAST_IF
:
2378 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
2379 addr
.s_addr
= INADDR_ANY
;
2380 else if (imo
->imo_multicast_addr
.s_addr
) {
2381 /* return the value user has set */
2382 addr
= imo
->imo_multicast_addr
;
2384 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
2385 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
2386 : IA_SIN(ia
)->sin_addr
.s_addr
;
2388 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
2391 case IP_MULTICAST_TTL
:
2393 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
2395 optval
= coptval
= imo
->imo_multicast_ttl
;
2396 if (sopt
->sopt_valsize
== 1)
2397 error
= sooptcopyout(sopt
, &coptval
, 1);
2399 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2402 case IP_MULTICAST_LOOP
:
2404 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
2406 optval
= coptval
= imo
->imo_multicast_loop
;
2407 if (sopt
->sopt_valsize
== 1)
2408 error
= sooptcopyout(sopt
, &coptval
, 1);
2410 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
2414 error
= ENOPROTOOPT
;
2421 * Discard the IP multicast options.
2424 ip_freemoptions(imo
)
2425 register struct ip_moptions
*imo
;
2430 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
2431 in_delmulti(&imo
->imo_membership
[i
]);
2432 FREE(imo
, M_IPMOPTS
);
2437 * Routine called from ip_output() to loop back a copy of an IP multicast
2438 * packet to the input queue of a specified interface. Note that this
2439 * calls the output routine of the loopback "driver", but with an interface
2440 * pointer that might NOT be a loopback interface -- evil, but easier than
2441 * replicating that code here.
2444 ip_mloopback(ifp
, m
, dst
, hlen
)
2446 register struct mbuf
*m
;
2447 register struct sockaddr_in
*dst
;
2450 register struct ip
*ip
;
2453 copym
= m_copy(m
, 0, M_COPYALL
);
2454 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2455 copym
= m_pullup(copym
, hlen
);
2456 if (copym
!= NULL
) {
2458 * We don't bother to fragment if the IP length is greater
2459 * than the interface's MTU. Can this possibly matter?
2461 ip
= mtod(copym
, struct ip
*);
2465 ip
->ip_sum
= in_cksum(copym
, hlen
);
2468 * It's not clear whether there are any lingering
2469 * reentrancy problems in other areas which might
2470 * be exposed by using ip_input directly (in
2471 * particular, everything which modifies the packet
2472 * in-place). Yet another option is using the
2473 * protosw directly to deliver the looped back
2474 * packet. For the moment, we'll err on the side
2475 * of safety by using if_simloop().
2478 if (dst
->sin_family
!= AF_INET
) {
2479 printf("ip_mloopback: bad address family %d\n",
2481 dst
->sin_family
= AF_INET
;
2487 * Mark checksum as valid or calculate checksum for loopback.
2489 * This is done this way because we have to embed the ifp of
2490 * the interface we will send the original copy of the packet
2491 * out on in the mbuf. ip_input will check if_hwassist of the
2492 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
2493 * The UDP checksum has not been calculated yet.
2495 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2496 if (IF_HWASSIST_CSUM_FLAGS(ifp
->if_hwassist
)) {
2497 copym
->m_pkthdr
.csum_flags
|=
2498 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2499 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2500 copym
->m_pkthdr
.csum_data
= 0xffff;
2503 in_delayed_cksum(copym
);
2511 * We need to send all loopback traffic down to dlil in case
2512 * a filter has tapped-in.
2516 * Stuff the 'real' ifp into the pkthdr, to be used in matching
2517 * in ip_input(); we need the loopback ifp/dl_tag passed as args
2518 * to make the loopback driver compliant with the data link
2522 copym
->m_pkthdr
.rcvif
= ifp
;
2523 dlil_output(lo_ifp
, PF_INET
, copym
, 0, (struct sockaddr
*) dst
, 0);
2525 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
2529 /* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/