2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
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>
70 #include <net/route.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
82 #include <sys/kdebug.h>
84 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
85 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
86 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
90 #include <machine/mtpr.h>
94 #include <machine/in_cksum.h>
96 static MALLOC_DEFINE(M_IPMOPTS
, "ip_moptions", "internet multicast options");
100 #include <netinet6/ipsec.h>
101 #include <netkey/key.h>
103 #include <netkey/key_debug.h>
105 #define KEYDEBUG(lev,arg)
109 #include <netinet/ip_fw.h>
112 #include <netinet/ip_dummynet.h>
115 #if IPFIREWALL_FORWARD_DEBUG
116 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
117 (ntohl(a.s_addr)>>16)&0xFF,\
118 (ntohl(a.s_addr)>>8)&0xFF,\
119 (ntohl(a.s_addr))&0xFF);
124 static struct mbuf
*ip_insertoptions
__P((struct mbuf
*, struct mbuf
*, int *));
125 static struct ifnet
*ip_multicast_if
__P((struct in_addr
*, int *));
126 static void ip_mloopback
127 __P((struct ifnet
*, struct mbuf
*, struct sockaddr_in
*, int));
128 static int ip_getmoptions
129 __P((struct sockopt
*, struct ip_moptions
*));
130 static int ip_pcbopts
__P((int, struct mbuf
**, struct mbuf
*));
131 static int ip_setmoptions
132 __P((struct sockopt
*, struct ip_moptions
**));
134 int ip_optcopy
__P((struct ip
*, struct ip
*));
135 extern int (*fr_checkp
) __P((struct ip
*, int, struct ifnet
*, int, struct mbuf
**));
137 extern struct mbuf
* m_dup(register struct mbuf
*m
, int how
);
140 static u_long lo_dl_tag
= 0;
142 void in_delayed_cksum(struct mbuf
*m
);
143 extern int apple_hwcksum_tx
;
145 extern struct protosw inetsw
[];
147 extern struct ip_linklocal_stat ip_linklocal_stat
;
149 /* temporary: for testing */
151 extern int ipsec_bypass
;
155 * IP output. The packet in mbuf chain m contains a skeletal IP
156 * header (with len, off, ttl, proto, tos, src, dst).
157 * The mbuf chain containing the packet will be freed.
158 * The mbuf opt, if present, will not be freed.
161 ip_output(m0
, opt
, ro
, flags
, imo
)
166 struct ip_moptions
*imo
;
168 struct ip
*ip
, *mhip
;
172 int hlen
= sizeof (struct ip
);
173 int len
, off
, error
= 0;
174 struct sockaddr_in
*dst
;
175 struct in_ifaddr
*ia
= NULL
;
176 int isbroadcast
, sw_csum
;
178 struct route iproute
;
179 struct socket
*so
= NULL
;
180 struct secpolicy
*sp
= NULL
;
182 u_int16_t divert_cookie
; /* firewall cookie */
183 #if IPFIREWALL_FORWARD
184 int fwd_rewrite_src
= 0;
186 struct ip_fw_chain
*rule
= NULL
;
189 /* Get and reset firewall cookie */
190 divert_cookie
= ip_divert_cookie
;
191 ip_divert_cookie
= 0;
196 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
198 #if IPFIREWALL && DUMMYNET
200 * dummynet packet are prepended a vestigial mbuf with
201 * m_type = MT_DUMMYNET and m_data pointing to the matching
204 if (m
->m_type
== MT_DUMMYNET
) {
206 * the packet was already tagged, so part of the
207 * processing was already done, and we need to go down.
208 * Get parameters from the header.
210 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
212 ro
= & ( ((struct dn_pkt
*)m
)->ro
) ;
214 dst
= ((struct dn_pkt
*)m
)->dn_dst
;
215 ifp
= ((struct dn_pkt
*)m
)->ifp
;
216 flags
= ((struct dn_pkt
*)m
)->flags
;
219 if (ipsec_bypass
== 0) {
220 so
= ipsec_getsocket(m
);
221 (void)ipsec_setsocket(m
, NULL
);
224 ip
= mtod(m
, struct ip
*);
225 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
226 if (ro
->ro_rt
!= NULL
)
227 ia
= (struct in_ifaddr
*)ro
->ro_rt
->rt_ifa
;
233 if (ipsec_bypass
== 0) {
234 so
= ipsec_getsocket(m
);
235 (void)ipsec_setsocket(m
, NULL
);
240 if ((m
->m_flags
& M_PKTHDR
) == 0)
241 panic("ip_output no HDR");
243 panic("ip_output no route, proto = %d",
244 mtod(m
, struct ip
*)->ip_p
);
247 m
= ip_insertoptions(m
, opt
, &len
);
250 ip
= mtod(m
, struct ip
*);
254 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
255 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
258 ip
->ip_id
= ip_randomid();
260 ip
->ip_id
= htons(ip_id
++);
262 ipstat
.ips_localout
++;
264 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
267 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
268 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
270 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
272 * If there is a cached route,
273 * check that it is to the same destination
274 * and is still up. If not, free it and try again.
276 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
277 dst
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
279 ro
->ro_rt
= (struct rtentry
*)0;
281 if (ro
->ro_rt
== 0) {
282 dst
->sin_family
= AF_INET
;
283 dst
->sin_len
= sizeof(*dst
);
284 dst
->sin_addr
= ip
->ip_dst
;
287 * If routing to interface only,
288 * short circuit routing lookup.
290 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
291 #define sintosa(sin) ((struct sockaddr *)(sin))
292 if (flags
& IP_ROUTETOIF
) {
293 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0 &&
294 (ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
295 ipstat
.ips_noroute
++;
300 dl_tag
= ia
->ia_ifa
.ifa_dlt
;
302 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
305 * If this is the case, we probably don't want to allocate
306 * a protocol-cloned route since we didn't get one from the
307 * ULP. This lets TCP do its thing, while not burdening
308 * forwarding or ICMP with the overhead of cloning a route.
309 * Of course, we still want to do any cloning requested by
310 * the link layer, as this is probably required in all cases
311 * for correct operation (as it is for ARP).
314 rtalloc_ign(ro
, RTF_PRCLONING
);
315 if (ro
->ro_rt
== 0) {
316 ipstat
.ips_noroute
++;
317 error
= EHOSTUNREACH
;
320 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
321 ifp
= ro
->ro_rt
->rt_ifp
;
322 dl_tag
= ro
->ro_rt
->rt_dlt
;
324 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
325 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
326 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
327 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
329 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
331 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
332 struct in_multi
*inm
;
334 m
->m_flags
|= M_MCAST
;
336 * IP destination address is multicast. Make sure "dst"
337 * still points to the address in "ro". (It may have been
338 * changed to point to a gateway address, above.)
340 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
342 * See if the caller provided any multicast options
345 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
346 if (imo
->imo_multicast_ifp
!= NULL
) {
347 ifp
= imo
->imo_multicast_ifp
;
348 dl_tag
= ifp
->if_data
.default_proto
;
350 if (imo
->imo_multicast_vif
!= -1)
352 ip_mcast_src(imo
->imo_multicast_vif
);
354 ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
356 * Confirm that the outgoing interface supports multicast.
358 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
359 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
360 ipstat
.ips_noroute
++;
366 * If source address not specified yet, use address
367 * of outgoing interface.
369 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
370 register struct in_ifaddr
*ia1
;
372 TAILQ_FOREACH(ia1
, &in_ifaddrhead
, ia_link
)
373 if (ia1
->ia_ifp
== ifp
) {
374 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
379 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
381 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
383 * If we belong to the destination multicast group
384 * on the outgoing interface, and the caller did not
385 * forbid loopback, loop back a copy.
387 ip_mloopback(ifp
, m
, dst
, hlen
);
391 * If we are acting as a multicast router, perform
392 * multicast forwarding as if the packet had just
393 * arrived on the interface to which we are about
394 * to send. The multicast forwarding function
395 * recursively calls this function, using the
396 * IP_FORWARDING flag to prevent infinite recursion.
398 * Multicasts that are looped back by ip_mloopback(),
399 * above, will be forwarded by the ip_input() routine,
402 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
404 * Check if rsvp daemon is running. If not, don't
405 * set ip_moptions. This ensures that the packet
406 * is multicast and not just sent down one link
407 * as prescribed by rsvpd.
411 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
419 * Multicasts with a time-to-live of zero may be looped-
420 * back, above, but must not be transmitted on a network.
421 * Also, multicasts addressed to the loopback interface
422 * are not sent -- the above call to ip_mloopback() will
423 * loop back a copy if this host actually belongs to the
424 * destination group on the loopback interface.
426 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
435 * If source address not specified yet, use address
436 * of outgoing interface.
438 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
439 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
440 #if IPFIREWALL_FORWARD
441 /* Keep note that we did this - if the firewall changes
442 * the next-hop, our interface may change, changing the
443 * default source IP. It's a shame so much effort happens
447 #endif /* IPFIREWALL_FORWARD */
451 * Verify that we have any chance at all of being able to queue
452 * the packet or packet fragments
454 if ((ifp
->if_snd
.ifq_len
+ ip
->ip_len
/ ifp
->if_mtu
+ 1) >=
455 ifp
->if_snd
.ifq_maxlen
) {
461 * Look for broadcast address and
462 * and verify user is allowed to send
466 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
467 error
= EADDRNOTAVAIL
;
470 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
474 /* don't allow broadcast messages to be fragmented */
475 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
479 m
->m_flags
|= M_BCAST
;
481 m
->m_flags
&= ~M_BCAST
;
486 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
488 if (IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)) || IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
))) {
489 ip_linklocal_stat
.iplls_out_total
++;
490 if (ip
->ip_ttl
!= MAXTTL
) {
491 ip_linklocal_stat
.iplls_out_badttl
++;
497 /* temporary for testing only: bypass ipsec alltogether */
499 if (ipsec_bypass
!= 0)
502 /* get SP for this packet */
504 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
506 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
509 ipsecstat
.out_inval
++;
516 switch (sp
->policy
) {
517 case IPSEC_POLICY_DISCARD
:
519 * This packet is just discarded.
521 ipsecstat
.out_polvio
++;
524 case IPSEC_POLICY_BYPASS
:
525 case IPSEC_POLICY_NONE
:
526 /* no need to do IPsec. */
529 case IPSEC_POLICY_IPSEC
:
530 if (sp
->req
== NULL
) {
531 /* acquire a policy */
532 error
= key_spdacquire(sp
);
537 case IPSEC_POLICY_ENTRUST
:
539 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
542 struct ipsec_output_state state
;
543 bzero(&state
, sizeof(state
));
545 if (flags
& IP_ROUTETOIF
) {
547 bzero(&iproute
, sizeof(iproute
));
550 state
.dst
= (struct sockaddr
*)dst
;
556 * delayed checksums are not currently compatible with IPsec
558 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
560 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
566 error
= ipsec4_output(&state
, sp
, flags
);
569 if (flags
& IP_ROUTETOIF
) {
571 * if we have tunnel mode SA, we may need to ignore
574 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
575 flags
&= ~IP_ROUTETOIF
;
580 dst
= (struct sockaddr_in
*)state
.dst
;
582 /* mbuf is already reclaimed in ipsec4_output. */
592 printf("ip4_output (ipsec): error code %d\n", error
);
595 /* don't show these error codes to the user */
603 /* be sure to update variables that are affected by ipsec4_output() */
604 ip
= mtod(m
, struct ip
*);
606 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
608 hlen
= ip
->ip_hl
<< 2;
610 if (ro
->ro_rt
== NULL
) {
611 if ((flags
& IP_ROUTETOIF
) == 0) {
613 "can't update route after IPsec processing\n");
614 error
= EHOSTUNREACH
; /*XXX*/
618 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
619 ifp
= ro
->ro_rt
->rt_ifp
;
620 dl_tag
= ia
->ia_ifa
.ifa_dlt
;
623 /* make it flipped, again. */
631 * - Xlate: translate packet's addr/port (NAT).
632 * - Firewall: deny/allow/etc.
633 * - Wrap: fake packet's addr/port <unimpl.>
634 * - Encapsulate: put it in another IP and send out. <unimp.>
639 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
)
641 ip
= mtod(m
= m1
, struct ip
*);
645 * Check with the firewall...
647 if (fw_enable
&& ip_fw_chk_ptr
) {
648 struct sockaddr_in
*old
= dst
;
650 off
= (*ip_fw_chk_ptr
)(&ip
,
651 hlen
, ifp
, &divert_cookie
, &m
, &rule
, &dst
);
653 * On return we must do the following:
654 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
655 * 1<=off<= 0xffff -> DIVERT
656 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
657 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
658 * dst != old -> IPFIREWALL_FORWARD
659 * off==0, dst==old -> accept
660 * If some of the above modules is not compiled in, then
661 * we should't have to check the corresponding condition
662 * (because the ipfw control socket should not accept
663 * unsupported rules), but better play safe and drop
664 * packets in case of doubt.
666 if ( (off
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) {
672 ip
= mtod(m
, struct ip
*);
673 if (off
== 0 && dst
== old
) /* common case */
676 if ((off
& IP_FW_PORT_DYNT_FLAG
) != 0) {
678 * pass the pkt to dummynet. Need to include
679 * pipe number, m, ifp, ro, dst because these are
680 * not recomputed in the next pass.
681 * All other parameters have been already used and
682 * so they are not needed anymore.
683 * XXX note: if the ifp or ro entry are deleted
684 * while a pkt is in dummynet, we are in trouble!
686 error
= dummynet_io(off
& 0xffff, DN_TO_IP_OUT
, m
,
687 ifp
,ro
,dst
,rule
, flags
);
692 if (off
!= 0 && (off
& IP_FW_PORT_DYNT_FLAG
) == 0) {
693 struct mbuf
*clone
= NULL
;
695 /* Clone packet if we're doing a 'tee' */
696 if ((off
& IP_FW_PORT_TEE_FLAG
) != 0)
697 clone
= m_dup(m
, M_DONTWAIT
);
700 * delayed checksums are not currently compatible
701 * with divert sockets.
703 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
705 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
708 /* Restore packet header fields to original values */
712 /* Deliver packet to divert input routine */
713 ip_divert_cookie
= divert_cookie
;
714 divert_packet(m
, 0, off
& 0xffff);
716 /* If 'tee', continue with original packet */
719 ip
= mtod(m
, struct ip
*);
726 #if IPFIREWALL_FORWARD
727 /* Here we check dst to make sure it's directly reachable on the
728 * interface we previously thought it was.
729 * If it isn't (which may be likely in some situations) we have
730 * to re-route it (ie, find a route for the next-hop and the
731 * associated interface) and set them here. This is nested
732 * forwarding which in most cases is undesirable, except where
733 * such control is nigh impossible. So we do it here.
736 if (off
== 0 && old
!= dst
) {
737 struct in_ifaddr
*ia
;
739 /* It's changed... */
740 /* There must be a better way to do this next line... */
741 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
742 #if IPFIREWALL_FORWARD_DEBUG
743 printf("IPFIREWALL_FORWARD: New dst ip: ");
744 print_ip(dst
->sin_addr
);
748 * We need to figure out if we have been forwarded
749 * to a local socket. If so then we should somehow
750 * "loop back" to ip_input, and get directed to the
751 * PCB as if we had received this packet. This is
752 * because it may be dificult to identify the packets
753 * you want to forward until they are being output
754 * and have selected an interface. (e.g. locally
755 * initiated packets) If we used the loopback inteface,
756 * we would not be able to control what happens
757 * as the packet runs through ip_input() as
758 * it is done through a ISR.
760 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
762 * If the addr to forward to is one
763 * of ours, we pretend to
764 * be the destination for this packet.
766 if (IA_SIN(ia
)->sin_addr
.s_addr
==
767 dst
->sin_addr
.s_addr
)
771 /* tell ip_input "dont filter" */
772 ip_fw_fwd_addr
= dst
;
773 if (m
->m_pkthdr
.rcvif
== NULL
)
774 m
->m_pkthdr
.rcvif
= ifunit("lo0");
775 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
776 m
->m_pkthdr
.csum_flags
|=
777 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
778 m0
->m_pkthdr
.csum_data
= 0xffff;
780 m
->m_pkthdr
.csum_flags
|=
781 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
787 /* Some of the logic for this was
790 * This rewrites the cached route in a local PCB.
791 * Is this what we want to do?
793 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
796 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
798 if (ro_fwd
->ro_rt
== 0) {
799 ipstat
.ips_noroute
++;
800 error
= EHOSTUNREACH
;
804 ia
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
805 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
806 dl_tag
= ro_fwd
->ro_rt
->rt_dlt
;
807 ro_fwd
->ro_rt
->rt_use
++;
808 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
809 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
810 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
812 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
814 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
816 ro
->ro_rt
= ro_fwd
->ro_rt
;
817 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
820 * If we added a default src ip earlier,
821 * which would have been gotten from the-then
822 * interface, do it again, from the new one.
825 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
828 #endif /* IPFIREWALL_FORWARD */
830 * if we get here, none of the above matches, and
831 * we have to drop the pkt
834 error
= EACCES
; /* not sure this is the right error msg */
839 m
->m_pkthdr
.csum_flags
|= CSUM_IP
;
840 sw_csum
= m
->m_pkthdr
.csum_flags
& ~ifp
->if_hwassist
;
842 if ((ifp
->if_hwassist
& CSUM_TCP_SUM16
) != 0) {
844 * Special case code for GMACE
845 * frames that can be checksumed by GMACE SUM16 HW:
846 * frame >64, no fragments, no UDP
848 if (apple_hwcksum_tx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP
)
849 && (ip
->ip_len
> 50) && (ip
->ip_len
<= ifp
->if_mtu
)) {
850 /* Apple GMAC HW, expects STUFF_OFFSET << 16 | START_OFFSET */
851 u_short offset
= (IP_VHL_HL(ip
->ip_vhl
) << 2) +14 ; /* IP+Enet header length */
852 u_short csumprev
= m
->m_pkthdr
.csum_data
& 0xFFFF;
853 m
->m_pkthdr
.csum_flags
= CSUM_DATA_VALID
| CSUM_TCP_SUM16
; /* for GMAC */
854 m
->m_pkthdr
.csum_data
= (csumprev
+ offset
) << 16 ;
855 m
->m_pkthdr
.csum_data
+= offset
;
856 sw_csum
= CSUM_DELAY_IP
; /* do IP hdr chksum in software */
859 /* let the software handle any UDP or TCP checksums */
860 sw_csum
|= (CSUM_DELAY_DATA
& m
->m_pkthdr
.csum_flags
);
864 if (sw_csum
& CSUM_DELAY_DATA
) {
866 sw_csum
&= ~CSUM_DELAY_DATA
;
867 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
870 m
->m_pkthdr
.csum_flags
&= ifp
->if_hwassist
;
873 * If small enough for interface, or the interface will take
874 * care of the fragmentation for us, can just send directly.
876 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
||
877 ifp
->if_hwassist
& CSUM_FRAGMENT
) {
881 if (sw_csum
& CSUM_DELAY_IP
) {
882 ip
->ip_sum
= in_cksum(m
, hlen
);
886 /* Record statistics for this interface address. */
887 if (!(flags
& IP_FORWARDING
) && ia
!= NULL
) {
888 ia
->ia_ifa
.if_opackets
++;
889 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
894 /* clean ipsec history once it goes out of the node */
895 if (ipsec_bypass
== 0)
899 error
= dlil_output(dl_tag
, m
, (void *) ro
->ro_rt
,
900 (struct sockaddr
*)dst
, 0);
902 error
= (*ifp
->if_output
)(ifp
, m
,
903 (struct sockaddr
*)dst
, ro
->ro_rt
);
908 * Too large for interface; fragment if possible.
909 * Must be able to put at least 8 bytes per fragment.
911 if (ip
->ip_off
& IP_DF
) {
914 * This case can happen if the user changed the MTU
915 * of an interface after enabling IP on it. Because
916 * most netifs don't keep track of routes pointing to
917 * them, there is no way for one to update all its
918 * routes when the MTU is changed.
920 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
921 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
922 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
923 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
925 ipstat
.ips_cantfrag
++;
928 len
= (ifp
->if_mtu
- hlen
) &~ 7;
935 * if the interface will not calculate checksums on
936 * fragmented packets, then do it here.
938 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
&&
939 (ifp
->if_hwassist
& CSUM_IP_FRAGS
) == 0) {
943 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
948 int mhlen
, firstlen
= len
;
949 struct mbuf
**mnext
= &m
->m_nextpkt
;
953 * Loop through length of segment after first fragment,
954 * make new header and copy data of each part and link onto chain.
957 mhlen
= sizeof (struct ip
);
958 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
959 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
962 ipstat
.ips_odropped
++;
965 m
->m_flags
|= (m0
->m_flags
& M_MCAST
) | M_FRAG
;
966 m
->m_data
+= max_linkhdr
;
967 mhip
= mtod(m
, struct ip
*);
969 if (hlen
> sizeof (struct ip
)) {
970 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
971 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
974 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
975 if (ip
->ip_off
& IP_MF
)
976 mhip
->ip_off
|= IP_MF
;
977 if (off
+ len
>= (u_short
)ip
->ip_len
)
978 len
= (u_short
)ip
->ip_len
- off
;
980 mhip
->ip_off
|= IP_MF
;
981 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
982 m
->m_next
= m_copy(m0
, off
, len
);
983 if (m
->m_next
== 0) {
985 error
= ENOBUFS
; /* ??? */
986 ipstat
.ips_odropped
++;
989 m
->m_pkthdr
.len
= mhlen
+ len
;
990 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
991 m
->m_pkthdr
.csum_flags
= m0
->m_pkthdr
.csum_flags
;
994 if (sw_csum
& CSUM_DELAY_IP
) {
995 mhip
->ip_sum
= in_cksum(m
, mhlen
);
998 mnext
= &m
->m_nextpkt
;
1001 ipstat
.ips_ofragments
+= nfrags
;
1003 /* set first/last markers for fragment chain */
1004 m
->m_flags
|= M_LASTFRAG
;
1005 m0
->m_flags
|= M_FIRSTFRAG
| M_FRAG
;
1006 m0
->m_pkthdr
.csum_data
= nfrags
;
1009 * Update first fragment by trimming what's been copied out
1010 * and updating header, then send each fragment (in order).
1013 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
1014 m
->m_pkthdr
.len
= hlen
+ firstlen
;
1015 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
1016 ip
->ip_off
|= IP_MF
;
1019 if (sw_csum
& CSUM_DELAY_IP
) {
1020 ip
->ip_sum
= in_cksum(m
, hlen
);
1024 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1025 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1027 for (m
= m0
; m
; m
= m0
) {
1031 /* clean ipsec history once it goes out of the node */
1032 if (ipsec_bypass
== 0)
1037 /* Record statistics for this interface address. */
1039 ia
->ia_ifa
.if_opackets
++;
1040 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1045 error
= dlil_output(dl_tag
, m
, (void *) ro
->ro_rt
,
1046 (struct sockaddr
*)dst
, 0);
1048 error
= (*ifp
->if_output
)(ifp
, m
,
1049 (struct sockaddr
*)dst
, ro
->ro_rt
);
1056 ipstat
.ips_fragmented
++;
1060 if (ipsec_bypass
== 0) {
1061 if (ro
== &iproute
&& ro
->ro_rt
) {
1066 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
1067 printf("DP ip_output call free SP:%x\n", sp
));
1073 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
1081 in_delayed_cksum(struct mbuf
*m
)
1084 u_short csum
, offset
;
1085 ip
= mtod(m
, struct ip
*);
1086 offset
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
1087 csum
= in_cksum_skip(m
, ip
->ip_len
, offset
);
1088 if (m
->m_pkthdr
.csum_flags
& CSUM_UDP
&& csum
== 0)
1090 offset
+= m
->m_pkthdr
.csum_data
& 0xFFFF; /* checksum offset */
1092 if (offset
> ip
->ip_len
) /* bogus offset */
1095 if (offset
+ sizeof(u_short
) > m
->m_len
) {
1096 printf("delayed m_pullup, m->len: %d off: %d p: %d\n",
1097 m
->m_len
, offset
, ip
->ip_p
);
1100 * this shouldn't happen, but if it does, the
1101 * correct behavior may be to insert the checksum
1102 * in the existing chain instead of rearranging it.
1104 m
= m_pullup(m
, offset
+ sizeof(u_short
));
1106 *(u_short
*)(m
->m_data
+ offset
) = csum
;
1110 * Insert IP options into preformed packet.
1111 * Adjust IP destination as required for IP source routing,
1112 * as indicated by a non-zero in_addr at the start of the options.
1114 * XXX This routine assumes that the packet has no options in place.
1116 static struct mbuf
*
1117 ip_insertoptions(m
, opt
, phlen
)
1118 register struct mbuf
*m
;
1122 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
1124 register struct ip
*ip
= mtod(m
, struct ip
*);
1127 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
1128 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
1129 return (m
); /* XXX should fail */
1130 if (p
->ipopt_dst
.s_addr
)
1131 ip
->ip_dst
= p
->ipopt_dst
;
1132 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
1133 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
1136 n
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1137 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
1138 m
->m_len
-= sizeof(struct ip
);
1139 m
->m_data
+= sizeof(struct ip
);
1142 m
->m_len
= optlen
+ sizeof(struct ip
);
1143 m
->m_data
+= max_linkhdr
;
1144 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
1146 m
->m_data
-= optlen
;
1148 m
->m_pkthdr
.len
+= optlen
;
1149 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
1151 ip
= mtod(m
, struct ip
*);
1152 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
1153 *phlen
= sizeof(struct ip
) + optlen
;
1154 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
1155 ip
->ip_len
+= optlen
;
1160 * Copy options from ip to jp,
1161 * omitting those not copied during fragmentation.
1167 register u_char
*cp
, *dp
;
1168 int opt
, optlen
, cnt
;
1170 cp
= (u_char
*)(ip
+ 1);
1171 dp
= (u_char
*)(jp
+ 1);
1172 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1173 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1175 if (opt
== IPOPT_EOL
)
1177 if (opt
== IPOPT_NOP
) {
1178 /* Preserve for IP mcast tunnel's LSRR alignment. */
1184 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1185 panic("malformed IPv4 option passed to ip_optcopy");
1187 optlen
= cp
[IPOPT_OLEN
];
1189 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1190 panic("malformed IPv4 option passed to ip_optcopy");
1192 /* bogus lengths should have been caught by ip_dooptions */
1195 if (IPOPT_COPIED(opt
)) {
1196 bcopy(cp
, dp
, optlen
);
1200 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1206 * IP socket option processing.
1209 ip_ctloutput(so
, sopt
)
1211 struct sockopt
*sopt
;
1213 struct inpcb
*inp
= sotoinpcb(so
);
1217 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1221 switch (sopt
->sopt_dir
) {
1223 switch (sopt
->sopt_name
) {
1230 if (sopt
->sopt_valsize
> MLEN
) {
1234 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1239 m
->m_len
= sopt
->sopt_valsize
;
1240 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1245 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1252 case IP_RECVRETOPTS
:
1253 case IP_RECVDSTADDR
:
1255 #if defined(NFAITH) && NFAITH > 0
1258 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1263 switch (sopt
->sopt_name
) {
1265 inp
->inp_ip_tos
= optval
;
1269 inp
->inp_ip_ttl
= optval
;
1271 #define OPTSET(bit) \
1273 inp->inp_flags |= bit; \
1275 inp->inp_flags &= ~bit;
1278 OPTSET(INP_RECVOPTS
);
1281 case IP_RECVRETOPTS
:
1282 OPTSET(INP_RECVRETOPTS
);
1285 case IP_RECVDSTADDR
:
1286 OPTSET(INP_RECVDSTADDR
);
1293 #if defined(NFAITH) && NFAITH > 0
1302 case IP_MULTICAST_IF
:
1303 case IP_MULTICAST_VIF
:
1304 case IP_MULTICAST_TTL
:
1305 case IP_MULTICAST_LOOP
:
1306 case IP_ADD_MEMBERSHIP
:
1307 case IP_DROP_MEMBERSHIP
:
1308 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1312 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1318 case IP_PORTRANGE_DEFAULT
:
1319 inp
->inp_flags
&= ~(INP_LOWPORT
);
1320 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1323 case IP_PORTRANGE_HIGH
:
1324 inp
->inp_flags
&= ~(INP_LOWPORT
);
1325 inp
->inp_flags
|= INP_HIGHPORT
;
1328 case IP_PORTRANGE_LOW
:
1329 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1330 inp
->inp_flags
|= INP_LOWPORT
;
1340 case IP_IPSEC_POLICY
:
1348 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1350 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1352 priv
= (sopt
->sopt_p
!= NULL
&&
1353 suser(sopt
->sopt_p
->p_ucred
,
1354 &sopt
->sopt_p
->p_acflag
) != 0) ? 0 : 1;
1356 req
= mtod(m
, caddr_t
);
1359 optname
= sopt
->sopt_name
;
1360 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1367 error
= ENOPROTOOPT
;
1373 switch (sopt
->sopt_name
) {
1376 if (inp
->inp_options
)
1377 error
= sooptcopyout(sopt
,
1378 mtod(inp
->inp_options
,
1380 inp
->inp_options
->m_len
);
1382 sopt
->sopt_valsize
= 0;
1388 case IP_RECVRETOPTS
:
1389 case IP_RECVDSTADDR
:
1392 #if defined(NFAITH) && NFAITH > 0
1395 switch (sopt
->sopt_name
) {
1398 optval
= inp
->inp_ip_tos
;
1402 optval
= inp
->inp_ip_ttl
;
1405 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1408 optval
= OPTBIT(INP_RECVOPTS
);
1411 case IP_RECVRETOPTS
:
1412 optval
= OPTBIT(INP_RECVRETOPTS
);
1415 case IP_RECVDSTADDR
:
1416 optval
= OPTBIT(INP_RECVDSTADDR
);
1420 optval
= OPTBIT(INP_RECVIF
);
1424 if (inp
->inp_flags
& INP_HIGHPORT
)
1425 optval
= IP_PORTRANGE_HIGH
;
1426 else if (inp
->inp_flags
& INP_LOWPORT
)
1427 optval
= IP_PORTRANGE_LOW
;
1432 #if defined(NFAITH) && NFAITH > 0
1434 optval
= OPTBIT(INP_FAITH
);
1438 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1441 case IP_MULTICAST_IF
:
1442 case IP_MULTICAST_VIF
:
1443 case IP_MULTICAST_TTL
:
1444 case IP_MULTICAST_LOOP
:
1445 case IP_ADD_MEMBERSHIP
:
1446 case IP_DROP_MEMBERSHIP
:
1447 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1451 case IP_IPSEC_POLICY
:
1453 struct mbuf
*m
= NULL
;
1458 req
= mtod(m
, caddr_t
);
1461 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
1463 error
= soopt_mcopyout(sopt
, m
); /* XXX */
1471 error
= ENOPROTOOPT
;
1480 * Set up IP options in pcb for insertion in output packets.
1481 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1482 * with destination address if source routed.
1485 ip_pcbopts(optname
, pcbopt
, m
)
1487 struct mbuf
**pcbopt
;
1488 register struct mbuf
*m
;
1490 register int cnt
, optlen
;
1491 register u_char
*cp
;
1494 /* turn off any old options */
1496 (void)m_free(*pcbopt
);
1498 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
1500 * Only turning off any previous options.
1508 if (m
->m_len
% sizeof(int32_t))
1512 * IP first-hop destination address will be stored before
1513 * actual options; move other options back
1514 * and clear it when none present.
1516 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1519 m
->m_len
+= sizeof(struct in_addr
);
1520 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1521 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
1522 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1524 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1525 opt
= cp
[IPOPT_OPTVAL
];
1526 if (opt
== IPOPT_EOL
)
1528 if (opt
== IPOPT_NOP
)
1531 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1533 optlen
= cp
[IPOPT_OLEN
];
1534 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1545 * user process specifies route as:
1547 * D must be our final destination (but we can't
1548 * check that since we may not have connected yet).
1549 * A is first hop destination, which doesn't appear in
1550 * actual IP option, but is stored before the options.
1552 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1554 m
->m_len
-= sizeof(struct in_addr
);
1555 cnt
-= sizeof(struct in_addr
);
1556 optlen
-= sizeof(struct in_addr
);
1557 cp
[IPOPT_OLEN
] = optlen
;
1559 * Move first hop before start of options.
1561 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1562 sizeof(struct in_addr
));
1564 * Then copy rest of options back
1565 * to close up the deleted entry.
1567 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
1568 sizeof(struct in_addr
)),
1569 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
1570 (unsigned)cnt
+ sizeof(struct in_addr
));
1574 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1586 * The whole multicast option thing needs to be re-thought.
1587 * Several of these options are equally applicable to non-multicast
1588 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1589 * standard option (IP_TTL).
1593 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1595 static struct ifnet
*
1596 ip_multicast_if(a
, ifindexp
)
1605 if (ntohl(a
->s_addr
) >> 24 == 0) {
1606 ifindex
= ntohl(a
->s_addr
) & 0xffffff;
1607 if (ifindex
< 0 || if_index
< ifindex
)
1609 ifp
= ifindex2ifnet
[ifindex
];
1611 *ifindexp
= ifindex
;
1613 INADDR_TO_IFP(*a
, ifp
);
1619 * Set the IP multicast options in response to user setsockopt().
1622 ip_setmoptions(sopt
, imop
)
1623 struct sockopt
*sopt
;
1624 struct ip_moptions
**imop
;
1628 struct in_addr addr
;
1629 struct ip_mreq mreq
;
1630 struct ifnet
*ifp
= NULL
;
1631 struct ip_moptions
*imo
= *imop
;
1633 struct sockaddr_in
*dst
;
1639 * No multicast option buffer attached to the pcb;
1640 * allocate one and initialize to default values.
1642 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
1648 imo
->imo_multicast_ifp
= NULL
;
1649 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1650 imo
->imo_multicast_vif
= -1;
1651 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1652 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
1653 imo
->imo_num_memberships
= 0;
1656 switch (sopt
->sopt_name
) {
1657 /* store an index number for the vif you wanna use in the send */
1658 case IP_MULTICAST_VIF
:
1659 if (legal_vif_num
== 0) {
1663 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
1666 if (!legal_vif_num(i
) && (i
!= -1)) {
1670 imo
->imo_multicast_vif
= i
;
1673 case IP_MULTICAST_IF
:
1675 * Select the interface for outgoing multicast packets.
1677 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
1681 * INADDR_ANY is used to remove a previous selection.
1682 * When no interface is selected, a default one is
1683 * chosen every time a multicast packet is sent.
1685 if (addr
.s_addr
== INADDR_ANY
) {
1686 imo
->imo_multicast_ifp
= NULL
;
1690 * The selected interface is identified by its local
1691 * IP address. Find the interface and confirm that
1692 * it supports multicasting.
1695 ifp
= ip_multicast_if(&addr
, &ifindex
);
1696 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1698 error
= EADDRNOTAVAIL
;
1701 imo
->imo_multicast_ifp
= ifp
;
1703 imo
->imo_multicast_addr
= addr
;
1705 imo
->imo_multicast_addr
.s_addr
= INADDR_ANY
;
1709 case IP_MULTICAST_TTL
:
1711 * Set the IP time-to-live for outgoing multicast packets.
1712 * The original multicast API required a char argument,
1713 * which is inconsistent with the rest of the socket API.
1714 * We allow either a char or an int.
1716 if (sopt
->sopt_valsize
== 1) {
1718 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
1721 imo
->imo_multicast_ttl
= ttl
;
1724 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
1731 imo
->imo_multicast_ttl
= ttl
;
1735 case IP_MULTICAST_LOOP
:
1737 * Set the loopback flag for outgoing multicast packets.
1738 * Must be zero or one. The original multicast API required a
1739 * char argument, which is inconsistent with the rest
1740 * of the socket API. We allow either a char or an int.
1742 if (sopt
->sopt_valsize
== 1) {
1744 error
= sooptcopyin(sopt
, &loop
, 1, 1);
1747 imo
->imo_multicast_loop
= !!loop
;
1750 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
1754 imo
->imo_multicast_loop
= !!loop
;
1758 case IP_ADD_MEMBERSHIP
:
1760 * Add a multicast group membership.
1761 * Group must be a valid IP multicast address.
1763 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1767 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1773 * If no interface address was provided, use the interface of
1774 * the route to the given multicast address.
1776 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
) {
1777 bzero((caddr_t
)&ro
, sizeof(ro
));
1778 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
1779 dst
->sin_len
= sizeof(*dst
);
1780 dst
->sin_family
= AF_INET
;
1781 dst
->sin_addr
= mreq
.imr_multiaddr
;
1783 if (ro
.ro_rt
!= NULL
) {
1784 ifp
= ro
.ro_rt
->rt_ifp
;
1788 /* If there's no default route, try using loopback */
1789 mreq
.imr_interface
.s_addr
= INADDR_LOOPBACK
;
1794 ifp
= ip_multicast_if(&mreq
.imr_interface
, NULL
);
1798 * See if we found an interface, and confirm that it
1799 * supports multicast.
1801 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1802 error
= EADDRNOTAVAIL
;
1807 * See if the membership already exists or if all the
1808 * membership slots are full.
1810 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1811 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1812 imo
->imo_membership
[i
]->inm_addr
.s_addr
1813 == mreq
.imr_multiaddr
.s_addr
)
1816 if (i
< imo
->imo_num_memberships
) {
1821 if (i
== IP_MAX_MEMBERSHIPS
) {
1822 error
= ETOOMANYREFS
;
1827 * Everything looks good; add a new record to the multicast
1828 * address list for the given interface.
1830 if ((imo
->imo_membership
[i
] =
1831 in_addmulti(&mreq
.imr_multiaddr
, ifp
)) == NULL
) {
1836 ++imo
->imo_num_memberships
;
1840 case IP_DROP_MEMBERSHIP
:
1842 * Drop a multicast group membership.
1843 * Group must be a valid IP multicast address.
1845 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1849 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1856 * If an interface address was specified, get a pointer
1857 * to its ifnet structure.
1859 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
)
1862 ifp
= ip_multicast_if(&mreq
.imr_interface
, NULL
);
1864 error
= EADDRNOTAVAIL
;
1870 * Find the membership in the membership array.
1872 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1874 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
1875 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
1876 mreq
.imr_multiaddr
.s_addr
)
1879 if (i
== imo
->imo_num_memberships
) {
1880 error
= EADDRNOTAVAIL
;
1885 * Give up the multicast address record to which the
1886 * membership points.
1888 in_delmulti(imo
->imo_membership
[i
]);
1890 * Remove the gap in the membership array.
1892 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
1893 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
1894 --imo
->imo_num_memberships
;
1904 * If all options have default values, no need to keep the mbuf.
1906 if (imo
->imo_multicast_ifp
== NULL
&&
1907 imo
->imo_multicast_vif
== -1 &&
1908 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
1909 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
1910 imo
->imo_num_memberships
== 0) {
1911 FREE(*imop
, M_IPMOPTS
);
1919 * Return the IP multicast options in response to user getsockopt().
1922 ip_getmoptions(sopt
, imo
)
1923 struct sockopt
*sopt
;
1924 register struct ip_moptions
*imo
;
1926 struct in_addr addr
;
1927 struct in_ifaddr
*ia
;
1932 switch (sopt
->sopt_name
) {
1933 case IP_MULTICAST_VIF
:
1935 optval
= imo
->imo_multicast_vif
;
1938 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1941 case IP_MULTICAST_IF
:
1942 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
1943 addr
.s_addr
= INADDR_ANY
;
1944 else if (imo
->imo_multicast_addr
.s_addr
) {
1945 /* return the value user has set */
1946 addr
= imo
->imo_multicast_addr
;
1948 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
1949 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
1950 : IA_SIN(ia
)->sin_addr
.s_addr
;
1952 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
1955 case IP_MULTICAST_TTL
:
1957 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
1959 optval
= coptval
= imo
->imo_multicast_ttl
;
1960 if (sopt
->sopt_valsize
== 1)
1961 error
= sooptcopyout(sopt
, &coptval
, 1);
1963 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1966 case IP_MULTICAST_LOOP
:
1968 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
1970 optval
= coptval
= imo
->imo_multicast_loop
;
1971 if (sopt
->sopt_valsize
== 1)
1972 error
= sooptcopyout(sopt
, &coptval
, 1);
1974 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1978 error
= ENOPROTOOPT
;
1985 * Discard the IP multicast options.
1988 ip_freemoptions(imo
)
1989 register struct ip_moptions
*imo
;
1994 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
1995 if (imo
->imo_membership
[i
] != NULL
)
1996 in_delmulti(imo
->imo_membership
[i
]);
1997 FREE(imo
, M_IPMOPTS
);
2002 * Routine called from ip_output() to loop back a copy of an IP multicast
2003 * packet to the input queue of a specified interface. Note that this
2004 * calls the output routine of the loopback "driver", but with an interface
2005 * pointer that might NOT be a loopback interface -- evil, but easier than
2006 * replicating that code here.
2009 ip_mloopback(ifp
, m
, dst
, hlen
)
2011 register struct mbuf
*m
;
2012 register struct sockaddr_in
*dst
;
2015 register struct ip
*ip
;
2018 copym
= m_copy(m
, 0, M_COPYALL
);
2019 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
2020 copym
= m_pullup(copym
, hlen
);
2021 if (copym
!= NULL
) {
2023 * We don't bother to fragment if the IP length is greater
2024 * than the interface's MTU. Can this possibly matter?
2026 ip
= mtod(copym
, struct ip
*);
2030 ip
->ip_sum
= in_cksum(copym
, hlen
);
2033 * It's not clear whether there are any lingering
2034 * reentrancy problems in other areas which might
2035 * be exposed by using ip_input directly (in
2036 * particular, everything which modifies the packet
2037 * in-place). Yet another option is using the
2038 * protosw directly to deliver the looped back
2039 * packet. For the moment, we'll err on the side
2040 * of safety by using if_simloop().
2043 if (dst
->sin_family
!= AF_INET
) {
2044 printf("ip_mloopback: bad address family %d\n",
2046 dst
->sin_family
= AF_INET
;
2052 * Mark checksum as valid or calculate checksum for loopback.
2054 * This is done this way because we have to embed the ifp of
2055 * the interface we will send the original copy of the packet
2056 * out on in the mbuf. ip_input will check if_hwassist of the
2057 * embedded ifp and ignore all csum_flags if if_hwassist is 0.
2058 * The UDP checksum has not been calculated yet.
2060 if (copym
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2061 if (ifp
->if_hwassist
) {
2062 copym
->m_pkthdr
.csum_flags
|=
2063 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
|
2064 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
2065 copym
->m_pkthdr
.csum_data
= 0xffff;
2068 in_delayed_cksum(copym
);
2076 * We need to send all loopback traffic down to dlil in case
2077 * a filter has tapped-in.
2081 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK
, 0, PF_INET
, &lo_dl_tag
);
2084 * Stuff the 'real' ifp into the pkthdr, to be used in matching
2085 * in ip_input(); we need the loopback ifp/dl_tag passed as args
2086 * to make the loopback driver compliant with the data link
2090 copym
->m_pkthdr
.rcvif
= ifp
;
2091 dlil_output(lo_dl_tag
, copym
, 0, (struct sockaddr
*) dst
, 0);
2093 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
2097 /* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/