2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
62 #include "opt_ipdivert.h"
63 #include "opt_ipfilter.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
76 #include <net/route.h>
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip_var.h>
90 #include <sys/kdebug.h>
92 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
93 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
94 #define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
98 #include <machine/mtpr.h>
102 #include <machine/in_cksum.h>
104 static MALLOC_DEFINE(M_IPMOPTS
, "ip_moptions", "internet multicast options");
107 //static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
110 #include <netinet6/ipsec.h>
111 #include <netkey/key.h>
112 #include <netkey/key_debug.h>
116 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
118 #define COMPAT_IPFW 1
124 #include <netinet/ip_fw.h>
128 #include <netinet/ip_dummynet.h>
131 #if IPFIREWALL_FORWARD_DEBUG
132 #define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\
133 (ntohl(a.s_addr)>>16)&0xFF,\
134 (ntohl(a.s_addr)>>8)&0xFF,\
135 (ntohl(a.s_addr))&0xFF);
140 static struct mbuf
*ip_insertoptions
__P((struct mbuf
*, struct mbuf
*, int *));
141 static void ip_mloopback
142 __P((struct ifnet
*, struct mbuf
*, struct sockaddr_in
*, int));
143 static int ip_getmoptions
144 __P((struct sockopt
*, struct ip_moptions
*));
145 static int ip_pcbopts
__P((int, struct mbuf
**, struct mbuf
*));
146 static int ip_setmoptions
147 __P((struct sockopt
*, struct ip_moptions
**));
148 static u_long lo_dl_tag
= 0;
150 #if IPFILTER_LKM || IPFILTER
151 int ip_optcopy
__P((struct ip
*, struct ip
*));
152 extern int (*fr_checkp
) __P((struct ip
*, int, struct ifnet
*, int, struct mbuf
**));
154 static int ip_optcopy
__P((struct ip
*, struct ip
*));
158 extern struct protosw inetsw
[];
161 * IP output. The packet in mbuf chain m contains a skeletal IP
162 * header (with len, off, ttl, proto, tos, src, dst).
163 * The mbuf chain containing the packet will be freed.
164 * The mbuf opt, if present, will not be freed.
167 ip_output(m0
, opt
, ro
, flags
, imo
)
172 struct ip_moptions
*imo
;
174 struct ip
*ip
, *mhip
;
178 int hlen
= sizeof (struct ip
);
179 int len
, off
, error
= 0;
180 struct sockaddr_in
*dst
;
181 struct in_ifaddr
*ia
;
184 struct route iproute
;
186 struct secpolicy
*sp
= NULL
;
188 #if IPFIREWALL_FORWARD
189 int fwd_rewrite_src
= 0;
193 #if !IPDIVERT /* dummy variable for the firewall code to play with */
194 u_short ip_divert_cookie
= 0 ;
197 struct ip_fw_chain
*rule
= NULL
;
200 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
204 * NOTE: m->m_pkthdr is NULL cleared below just to prevent ipfw code
206 * ipfw code uses rcvif to determine incoming interface, and
207 * KAME uses rcvif for ipsec processing.
208 * ipfw may not be working right with KAME at this moment.
209 * We need more tests.
212 if (m
->m_type
== MT_DUMMYNET
) {
213 if (m
->m_next
!= NULL
) {
214 so
= (struct socket
*)m
->m_next
->m_pkthdr
.rcvif
;
215 m
->m_next
->m_pkthdr
.rcvif
= NULL
;
221 so
= ipsec_getsocket(m
);
222 ipsec_setsocket(m
, NULL
);
227 #if IPFIREWALL && DUMMYNET
229 * dummynet packet are prepended a vestigial mbuf with
230 * m_type = MT_DUMMYNET and m_data pointing to the matching
233 if (m
->m_type
== MT_DUMMYNET
) {
234 struct mbuf
*tmp_m
= m
;
236 * the packet was already tagged, so part of the
237 * processing was already done, and we need to go down.
238 * opt, flags and imo have already been used, and now
239 * they are used to hold ifp and hlen and NULL, respectively.
241 rule
= (struct ip_fw_chain
*)(m
->m_data
) ;
244 ip
= mtod(m
, struct ip
*);
245 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
246 ifp
= (struct ifnet
*)opt
;
247 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2 ;
249 flags
= 0 ; /* XXX is this correct ? */
256 if ((m
->m_flags
& M_PKTHDR
) == 0)
257 panic("ip_output no HDR");
259 panic("ip_output no route, proto = %d",
260 mtod(m
, struct ip
*)->ip_p
);
263 m
= ip_insertoptions(m
, opt
, &len
);
266 ip
= mtod(m
, struct ip
*);
270 if ((flags
& (IP_FORWARDING
|IP_RAWOUTPUT
)) == 0) {
271 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, hlen
>> 2);
273 ip
->ip_id
= htons(ip_id
++);
274 ipstat
.ips_localout
++;
276 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
279 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
280 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
282 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
284 * If there is a cached route,
285 * check that it is to the same destination
286 * and is still up. If not, free it and try again.
288 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
289 dst
->sin_addr
.s_addr
!= ip
->ip_dst
.s_addr
)) {
291 ro
->ro_rt
= (struct rtentry
*)0;
293 if (ro
->ro_rt
== 0) {
294 dst
->sin_family
= AF_INET
;
295 dst
->sin_len
= sizeof(*dst
);
296 dst
->sin_addr
= ip
->ip_dst
;
299 * If routing to interface only,
300 * short circuit routing lookup.
302 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
303 #define sintosa(sin) ((struct sockaddr *)(sin))
304 if (flags
& IP_ROUTETOIF
) {
305 if ((ia
= ifatoia(ifa_ifwithdstaddr(sintosa(dst
)))) == 0 &&
306 (ia
= ifatoia(ifa_ifwithnet(sintosa(dst
)))) == 0) {
307 ipstat
.ips_noroute
++;
312 dl_tag
= ia
->ia_ifa
.ifa_dlt
;
314 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
317 * If this is the case, we probably don't want to allocate
318 * a protocol-cloned route since we didn't get one from the
319 * ULP. This lets TCP do its thing, while not burdening
320 * forwarding or ICMP with the overhead of cloning a route.
321 * Of course, we still want to do any cloning requested by
322 * the link layer, as this is probably required in all cases
323 * for correct operation (as it is for ARP).
326 rtalloc_ign(ro
, RTF_PRCLONING
);
327 if (ro
->ro_rt
== 0) {
328 ipstat
.ips_noroute
++;
329 error
= EHOSTUNREACH
;
332 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
333 ifp
= ro
->ro_rt
->rt_ifp
;
334 dl_tag
= ro
->ro_rt
->rt_dlt
;
336 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
337 dst
= (struct sockaddr_in
*)ro
->ro_rt
->rt_gateway
;
338 if (ro
->ro_rt
->rt_flags
& RTF_HOST
)
339 isbroadcast
= (ro
->ro_rt
->rt_flags
& RTF_BROADCAST
);
341 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
343 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
344 struct in_multi
*inm
;
346 m
->m_flags
|= M_MCAST
;
348 * IP destination address is multicast. Make sure "dst"
349 * still points to the address in "ro". (It may have been
350 * changed to point to a gateway address, above.)
352 dst
= (struct sockaddr_in
*)&ro
->ro_dst
;
354 * See if the caller provided any multicast options
357 ip
->ip_ttl
= imo
->imo_multicast_ttl
;
358 if (imo
->imo_multicast_ifp
!= NULL
)
359 ifp
= imo
->imo_multicast_ifp
;
360 if (imo
->imo_multicast_vif
!= -1)
362 ip_mcast_src(imo
->imo_multicast_vif
);
364 ip
->ip_ttl
= IP_DEFAULT_MULTICAST_TTL
;
366 * Confirm that the outgoing interface supports multicast.
368 if ((imo
== NULL
) || (imo
->imo_multicast_vif
== -1)) {
369 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
370 ipstat
.ips_noroute
++;
376 * If source address not specified yet, use address
377 * of outgoing interface.
379 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
380 register struct in_ifaddr
*ia1
;
382 for (ia1
= in_ifaddrhead
.tqh_first
; ia1
;
383 ia1
= ia1
->ia_link
.tqe_next
)
384 if (ia1
->ia_ifp
== ifp
) {
385 ip
->ip_src
= IA_SIN(ia1
)->sin_addr
;
390 IN_LOOKUP_MULTI(ip
->ip_dst
, ifp
, inm
);
392 (imo
== NULL
|| imo
->imo_multicast_loop
)) {
394 * If we belong to the destination multicast group
395 * on the outgoing interface, and the caller did not
396 * forbid loopback, loop back a copy.
398 ip_mloopback(ifp
, m
, dst
, hlen
);
402 * If we are acting as a multicast router, perform
403 * multicast forwarding as if the packet had just
404 * arrived on the interface to which we are about
405 * to send. The multicast forwarding function
406 * recursively calls this function, using the
407 * IP_FORWARDING flag to prevent infinite recursion.
409 * Multicasts that are looped back by ip_mloopback(),
410 * above, will be forwarded by the ip_input() routine,
413 if (ip_mrouter
&& (flags
& IP_FORWARDING
) == 0) {
415 * Check if rsvp daemon is running. If not, don't
416 * set ip_moptions. This ensures that the packet
417 * is multicast and not just sent down one link
418 * as prescribed by rsvpd.
422 if (ip_mforward(ip
, ifp
, m
, imo
) != 0) {
430 * Multicasts with a time-to-live of zero may be looped-
431 * back, above, but must not be transmitted on a network.
432 * Also, multicasts addressed to the loopback interface
433 * are not sent -- the above call to ip_mloopback() will
434 * loop back a copy if this host actually belongs to the
435 * destination group on the loopback interface.
437 if (ip
->ip_ttl
== 0 || ifp
->if_flags
& IFF_LOOPBACK
) {
446 * If source address not specified yet, use address
447 * of outgoing interface.
449 if (ip
->ip_src
.s_addr
== INADDR_ANY
) {
450 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
451 #if IPFIREWALL_FORWARD
452 /* Keep note that we did this - if the firewall changes
453 * the next-hop, our interface may change, changing the
454 * default source IP. It's a shame so much effort happens
458 #endif /* IPFIREWALL_FORWARD */
462 * Verify that we have any chance at all of being able to queue
463 * the packet or packet fragments
465 if ((ifp
->if_snd
.ifq_len
+ ip
->ip_len
/ ifp
->if_mtu
+ 1) >=
466 ifp
->if_snd
.ifq_maxlen
) {
472 * Look for broadcast address and
473 * and verify user is allowed to send
477 if ((ifp
->if_flags
& IFF_BROADCAST
) == 0) {
478 error
= EADDRNOTAVAIL
;
481 if ((flags
& IP_ALLOWBROADCAST
) == 0) {
485 /* don't allow broadcast messages to be fragmented */
486 if ((u_short
)ip
->ip_len
> ifp
->if_mtu
) {
490 m
->m_flags
|= M_BCAST
;
492 m
->m_flags
&= ~M_BCAST
;
498 * - Xlate: translate packet's addr/port (NAT).
499 * - Firewall: deny/allow/etc.
500 * - Wrap: fake packet's addr/port <unimpl.>
501 * - Encapsulate: put it in another IP and send out. <unimp.>
503 #if IPFILTER || IPFILTER_LKM
507 if ((error
= (*fr_checkp
)(ip
, hlen
, ifp
, 1, &m1
)) || !m1
)
509 ip
= mtod(m
= m1
, struct ip
*);
514 if (ip_nat_ptr
&& !(*ip_nat_ptr
)(&ip
, &m
, ifp
, IP_NAT_OUT
)) {
520 * Check with the firewall...
523 struct sockaddr_in
*old
= dst
;
525 off
= (*ip_fw_chk_ptr
)(&ip
,
526 hlen
, ifp
, &ip_divert_cookie
, &m
, &rule
, &dst
);
528 * On return we must do the following:
529 * m == NULL -> drop the pkt
530 * 1<=off<= 0xffff -> DIVERT
531 * (off & 0x10000) -> send to a DUMMYNET pipe
532 * dst != old -> IPFIREWALL_FORWARD
533 * off==0, dst==old -> accept
534 * If some of the above modules is not compiled in, then
535 * we should't have to check the corresponding condition
536 * (because the ipfw control socket should not accept
537 * unsupported rules), but better play safe and drop
538 * packets in case of doubt.
540 if (!m
) { /* firewall said to reject */
544 if (off
== 0 && dst
== old
) /* common case */
549 * pass the pkt to dummynet. Need to include
550 * pipe number, m, ifp, ro, hlen because these are
551 * not recomputed in the next pass.
552 * All other parameters have been already used and
553 * so they are not needed anymore.
554 * XXX note: if the ifp or ro entry are deleted
555 * while a pkt is in dummynet, we are in trouble!
557 dummynet_io(off
& 0xffff, DN_TO_IP_OUT
, m
,ifp
,ro
,hlen
,rule
);
562 if (off
> 0 && off
< 0x10000) { /* Divert packet */
563 ip_divert_port
= off
& 0xffff ;
564 (*ip_protox
[IPPROTO_DIVERT
]->pr_input
)(m
, 0);
569 #if IPFIREWALL_FORWARD
570 /* Here we check dst to make sure it's directly reachable on the
571 * interface we previously thought it was.
572 * If it isn't (which may be likely in some situations) we have
573 * to re-route it (ie, find a route for the next-hop and the
574 * associated interface) and set them here. This is nested
575 * forwarding which in most cases is undesirable, except where
576 * such control is nigh impossible. So we do it here.
579 if (off
== 0 && old
!= dst
) {
580 struct in_ifaddr
*ia
;
582 /* It's changed... */
583 /* There must be a better way to do this next line... */
584 static struct route sro_fwd
, *ro_fwd
= &sro_fwd
;
585 #if IPFIREWALL_FORWARD_DEBUG
586 printf("IPFIREWALL_FORWARD: New dst ip: ");
587 print_ip(dst
->sin_addr
);
591 * We need to figure out if we have been forwarded
592 * to a local socket. If so then we should somehow
593 * "loop back" to ip_input, and get directed to the
594 * PCB as if we had received this packet. This is
595 * because it may be dificult to identify the packets
596 * you want to forward until they are being output
597 * and have selected an interface. (e.g. locally
598 * initiated packets) If we used the loopback inteface,
599 * we would not be able to control what happens
600 * as the packet runs through ip_input() as
601 * it is done through a ISR.
603 for (ia
= TAILQ_FIRST(&in_ifaddrhead
); ia
;
604 ia
= TAILQ_NEXT(ia
, ia_link
)) {
606 * If the addr to forward to is one
607 * of ours, we pretend to
608 * be the destination for this packet.
610 if (IA_SIN(ia
)->sin_addr
.s_addr
==
611 dst
->sin_addr
.s_addr
)
615 /* tell ip_input "dont filter" */
616 ip_fw_fwd_addr
= dst
;
617 if (m
->m_pkthdr
.rcvif
== NULL
)
618 m
->m_pkthdr
.rcvif
= ifunit("lo0");
619 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
620 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
623 ip
->ip_sum
= in_cksum(m
, hlen
);
628 /* Some of the logic for this was
631 * This rewrites the cached route in a local PCB.
632 * Is this what we want to do?
634 bcopy(dst
, &ro_fwd
->ro_dst
, sizeof(*dst
));
637 rtalloc_ign(ro_fwd
, RTF_PRCLONING
);
639 if (ro_fwd
->ro_rt
== 0) {
640 ipstat
.ips_noroute
++;
641 error
= EHOSTUNREACH
;
645 ia
= ifatoia(ro_fwd
->ro_rt
->rt_ifa
);
646 ifp
= ro_fwd
->ro_rt
->rt_ifp
;
647 dl_tag
= ro
->ro_rt
->rt_dlt
;
648 ro_fwd
->ro_rt
->rt_use
++;
649 if (ro_fwd
->ro_rt
->rt_flags
& RTF_GATEWAY
)
650 dst
= (struct sockaddr_in
*)ro_fwd
->ro_rt
->rt_gateway
;
651 if (ro_fwd
->ro_rt
->rt_flags
& RTF_HOST
)
653 (ro_fwd
->ro_rt
->rt_flags
& RTF_BROADCAST
);
655 isbroadcast
= in_broadcast(dst
->sin_addr
, ifp
);
657 ro
->ro_rt
= ro_fwd
->ro_rt
;
658 dst
= (struct sockaddr_in
*)&ro_fwd
->ro_dst
;
661 * If we added a default src ip earlier,
662 * which would have been gotten from the-then
663 * interface, do it again, from the new one.
666 ip
->ip_src
= IA_SIN(ia
)->sin_addr
;
669 #endif /* IPFIREWALL_FORWARD */
671 * if we get here, none of the above matches, and
672 * we have to drop the pkt
675 error
= EACCES
; /* not sure this is the right error msg */
678 #endif /* COMPAT_IPFW */
684 * Processing IP filter/NAT.
685 * Return TRUE iff this packet is discarded.
686 * Return FALSE iff this packet is accepted.
689 if (doNatFil
&& pm_out(ro
->ro_rt
->rt_ifp
, ip
, m
))
694 /* get SP for this packet */
696 sp
= ipsec4_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, flags
, &error
);
698 sp
= ipsec4_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
701 ipsecstat
.out_inval
++;
708 switch (sp
->policy
) {
709 case IPSEC_POLICY_DISCARD
:
711 * This packet is just discarded.
713 ipsecstat
.out_polvio
++;
716 case IPSEC_POLICY_BYPASS
:
717 case IPSEC_POLICY_NONE
:
718 /* no need to do IPsec. */
721 case IPSEC_POLICY_IPSEC
:
722 if (sp
->req
== NULL
) {
723 /* XXX should be panic ? */
724 printf("ip_output: No IPsec request specified.\n");
730 case IPSEC_POLICY_ENTRUST
:
732 printf("ip_output: Invalid policy found. %d\n", sp
->policy
);
735 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
736 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
740 struct ipsec_output_state state
;
741 bzero(&state
, sizeof(state
));
743 if (flags
& IP_ROUTETOIF
) {
745 bzero(&iproute
, sizeof(iproute
));
748 state
.dst
= (struct sockaddr
*)dst
;
750 error
= ipsec4_output(&state
, sp
, flags
);
753 if (flags
& IP_ROUTETOIF
) {
755 * if we have tunnel mode SA, we may need to ignore
758 if (state
.ro
!= &iproute
|| state
.ro
->ro_rt
!= NULL
) {
759 flags
&= ~IP_ROUTETOIF
;
764 dst
= (struct sockaddr_in
*)state
.dst
;
766 /* mbuf is already reclaimed in ipsec4_output. */
776 printf("ip4_output (ipsec): error code %d\n", error
);
779 /* don't show these error codes to the user */
787 /* be sure to update variables that are affected by ipsec4_output() */
788 ip
= mtod(m
, struct ip
*);
790 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
792 hlen
= ip
->ip_hl
<< 2;
794 if (ro
->ro_rt
== NULL
) {
795 if ((flags
& IP_ROUTETOIF
) == 0) {
797 "can't update route after IPsec processing\n");
798 error
= EHOSTUNREACH
; /*XXX*/
802 /* nobody uses ia beyond here */
803 ifp
= ro
->ro_rt
->rt_ifp
;
806 /* make it flipped, again. */
807 ip
->ip_len
= ntohs((u_short
)ip
->ip_len
);
808 ip
->ip_off
= ntohs((u_short
)ip
->ip_off
);
813 * If small enough for interface, can just send directly.
815 if ((u_short
)ip
->ip_len
<= ifp
->if_mtu
) {
816 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
817 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
819 ip
->ip_sum
= in_cksum(m
, hlen
);
820 error
= dlil_output(dl_tag
, m
, (void *) ro
->ro_rt
,
821 (struct sockaddr
*)dst
, 0);
825 * Too large for interface; fragment if possible.
826 * Must be able to put at least 8 bytes per fragment.
828 if (ip
->ip_off
& IP_DF
) {
831 * This case can happen if the user changed the MTU
832 * of an interface after enabling IP on it. Because
833 * most netifs don't keep track of routes pointing to
834 * them, there is no way for one to update all its
835 * routes when the MTU is changed.
837 if ((ro
->ro_rt
->rt_flags
& (RTF_UP
| RTF_HOST
))
838 && !(ro
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
)
839 && (ro
->ro_rt
->rt_rmx
.rmx_mtu
> ifp
->if_mtu
)) {
840 ro
->ro_rt
->rt_rmx
.rmx_mtu
= ifp
->if_mtu
;
842 ipstat
.ips_cantfrag
++;
845 len
= (ifp
->if_mtu
- hlen
) &~ 7;
852 int mhlen
, firstlen
= len
;
853 struct mbuf
**mnext
= &m
->m_nextpkt
;
856 * Loop through length of segment after first fragment,
857 * make new header and copy data of each part and link onto chain.
860 mhlen
= sizeof (struct ip
);
861 for (off
= hlen
+ len
; off
< (u_short
)ip
->ip_len
; off
+= len
) {
862 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
865 ipstat
.ips_odropped
++;
868 m
->m_flags
|= (m0
->m_flags
& M_MCAST
);
869 m
->m_data
+= max_linkhdr
;
870 mhip
= mtod(m
, struct ip
*);
872 if (hlen
> sizeof (struct ip
)) {
873 mhlen
= ip_optcopy(ip
, mhip
) + sizeof (struct ip
);
874 mhip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, mhlen
>> 2);
877 mhip
->ip_off
= ((off
- hlen
) >> 3) + (ip
->ip_off
& ~IP_MF
);
878 if (ip
->ip_off
& IP_MF
)
879 mhip
->ip_off
|= IP_MF
;
880 if (off
+ len
>= (u_short
)ip
->ip_len
)
881 len
= (u_short
)ip
->ip_len
- off
;
883 mhip
->ip_off
|= IP_MF
;
884 mhip
->ip_len
= htons((u_short
)(len
+ mhlen
));
885 m
->m_next
= m_copy(m0
, off
, len
);
886 if (m
->m_next
== 0) {
888 error
= ENOBUFS
; /* ??? */
889 ipstat
.ips_odropped
++;
892 m
->m_pkthdr
.len
= mhlen
+ len
;
893 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
894 mhip
->ip_off
= htons((u_short
)mhip
->ip_off
);
896 mhip
->ip_sum
= in_cksum(m
, mhlen
);
898 mnext
= &m
->m_nextpkt
;
899 ipstat
.ips_ofragments
++;
902 * Update first fragment by trimming what's been copied out
903 * and updating header, then send each fragment (in order).
906 m_adj(m
, hlen
+ firstlen
- (u_short
)ip
->ip_len
);
907 m
->m_pkthdr
.len
= hlen
+ firstlen
;
908 ip
->ip_len
= htons((u_short
)m
->m_pkthdr
.len
);
909 ip
->ip_off
= htons((u_short
)(ip
->ip_off
| IP_MF
));
911 ip
->ip_sum
= in_cksum(m
, hlen
);
915 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
916 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
918 for (m
= m0
; m
; m
= m0
) {
922 error
= dlil_output(dl_tag
, m
, (void *) ro
->ro_rt
,
923 (struct sockaddr
*)dst
, 0);
929 ipstat
.ips_fragmented
++;
933 if (ro
== &iproute
&& ro
->ro_rt
) {
938 KEYDEBUG(KEYDEBUG_IPSEC_STAMP
,
939 printf("DP ip_output call free SP:%x\n", sp
));
944 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT
| DBG_FUNC_END
, error
,0,0,0,0);
952 * Insert IP options into preformed packet.
953 * Adjust IP destination as required for IP source routing,
954 * as indicated by a non-zero in_addr at the start of the options.
956 * XXX This routine assumes that the packet has no options in place.
959 ip_insertoptions(m
, opt
, phlen
)
960 register struct mbuf
*m
;
964 register struct ipoption
*p
= mtod(opt
, struct ipoption
*);
966 register struct ip
*ip
= mtod(m
, struct ip
*);
969 optlen
= opt
->m_len
- sizeof(p
->ipopt_dst
);
970 if (optlen
+ (u_short
)ip
->ip_len
> IP_MAXPACKET
)
971 return (m
); /* XXX should fail */
972 if (p
->ipopt_dst
.s_addr
)
973 ip
->ip_dst
= p
->ipopt_dst
;
974 if (m
->m_flags
& M_EXT
|| m
->m_data
- optlen
< m
->m_pktdat
) {
975 MGETHDR(n
, M_DONTWAIT
, MT_HEADER
);
978 n
->m_pkthdr
.len
= m
->m_pkthdr
.len
+ optlen
;
979 m
->m_len
-= sizeof(struct ip
);
980 m
->m_data
+= sizeof(struct ip
);
983 m
->m_len
= optlen
+ sizeof(struct ip
);
984 m
->m_data
+= max_linkhdr
;
985 (void)memcpy(mtod(m
, void *), ip
, sizeof(struct ip
));
989 m
->m_pkthdr
.len
+= optlen
;
990 ovbcopy((caddr_t
)ip
, mtod(m
, caddr_t
), sizeof(struct ip
));
992 ip
= mtod(m
, struct ip
*);
993 bcopy(p
->ipopt_list
, ip
+ 1, optlen
);
994 *phlen
= sizeof(struct ip
) + optlen
;
995 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, *phlen
>> 2);
996 ip
->ip_len
+= optlen
;
1001 * Copy options from ip to jp,
1002 * omitting those not copied during fragmentation.
1004 #if !IPFILTER && !IPFILTER_LKM
1011 register u_char
*cp
, *dp
;
1012 int opt
, optlen
, cnt
;
1014 cp
= (u_char
*)(ip
+ 1);
1015 dp
= (u_char
*)(jp
+ 1);
1016 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1017 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1019 if (opt
== IPOPT_EOL
)
1021 if (opt
== IPOPT_NOP
) {
1022 /* Preserve for IP mcast tunnel's LSRR alignment. */
1027 optlen
= cp
[IPOPT_OLEN
];
1028 /* bogus lengths should have been caught by ip_dooptions */
1031 if (IPOPT_COPIED(opt
)) {
1032 bcopy(cp
, dp
, optlen
);
1036 for (optlen
= dp
- (u_char
*)(jp
+1); optlen
& 0x3; optlen
++)
1042 * IP socket option processing.
1045 ip_ctloutput(so
, sopt
)
1047 struct sockopt
*sopt
;
1049 struct inpcb
*inp
= sotoinpcb(so
);
1053 if (sopt
->sopt_level
!= IPPROTO_IP
) {
1057 switch (sopt
->sopt_dir
) {
1059 switch (sopt
->sopt_name
) {
1066 if (sopt
->sopt_valsize
> MLEN
) {
1070 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1075 m
->m_len
= sopt
->sopt_valsize
;
1076 error
= sooptcopyin(sopt
, mtod(m
, char *), m
->m_len
,
1081 return (ip_pcbopts(sopt
->sopt_name
, &inp
->inp_options
,
1088 case IP_RECVRETOPTS
:
1089 case IP_RECVDSTADDR
:
1092 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1097 switch (sopt
->sopt_name
) {
1099 inp
->inp_ip_tos
= optval
;
1103 inp
->inp_ip_ttl
= optval
;
1105 #define OPTSET(bit) \
1107 inp->inp_flags |= bit; \
1109 inp->inp_flags &= ~bit;
1112 OPTSET(INP_RECVOPTS
);
1115 case IP_RECVRETOPTS
:
1116 OPTSET(INP_RECVRETOPTS
);
1119 case IP_RECVDSTADDR
:
1120 OPTSET(INP_RECVDSTADDR
);
1134 case IP_MULTICAST_IF
:
1135 case IP_MULTICAST_VIF
:
1136 case IP_MULTICAST_TTL
:
1137 case IP_MULTICAST_LOOP
:
1138 case IP_ADD_MEMBERSHIP
:
1139 case IP_DROP_MEMBERSHIP
:
1140 error
= ip_setmoptions(sopt
, &inp
->inp_moptions
);
1144 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
1150 case IP_PORTRANGE_DEFAULT
:
1151 inp
->inp_flags
&= ~(INP_LOWPORT
);
1152 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1155 case IP_PORTRANGE_HIGH
:
1156 inp
->inp_flags
&= ~(INP_LOWPORT
);
1157 inp
->inp_flags
|= INP_HIGHPORT
;
1160 case IP_PORTRANGE_LOW
:
1161 inp
->inp_flags
&= ~(INP_HIGHPORT
);
1162 inp
->inp_flags
|= INP_LOWPORT
;
1172 case IP_IPSEC_POLICY
:
1180 if (error
= sooptgetm(sopt
, &m
)) /* XXX */
1182 if (error
= sooptmcopyin(sopt
, m
)) /* XXX */
1184 priv
= (sopt
->sopt_p
!= NULL
&&
1185 suser(sopt
->sopt_p
->p_ucred
,
1186 &sopt
->sopt_p
->p_acflag
) != 0) ? 0 : 1;
1188 req
= mtod(m
, caddr_t
);
1191 optname
= sopt
->sopt_name
;
1192 error
= ipsec4_set_policy(inp
, optname
, req
, len
, priv
);
1199 error
= ENOPROTOOPT
;
1205 switch (sopt
->sopt_name
) {
1208 if (inp
->inp_options
)
1209 error
= sooptcopyout(sopt
,
1210 mtod(inp
->inp_options
,
1212 inp
->inp_options
->m_len
);
1214 sopt
->sopt_valsize
= 0;
1220 case IP_RECVRETOPTS
:
1221 case IP_RECVDSTADDR
:
1225 switch (sopt
->sopt_name
) {
1228 optval
= inp
->inp_ip_tos
;
1232 optval
= inp
->inp_ip_ttl
;
1235 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
1238 optval
= OPTBIT(INP_RECVOPTS
);
1241 case IP_RECVRETOPTS
:
1242 optval
= OPTBIT(INP_RECVRETOPTS
);
1245 case IP_RECVDSTADDR
:
1246 optval
= OPTBIT(INP_RECVDSTADDR
);
1250 optval
= OPTBIT(INP_RECVIF
);
1254 if (inp
->inp_flags
& INP_HIGHPORT
)
1255 optval
= IP_PORTRANGE_HIGH
;
1256 else if (inp
->inp_flags
& INP_LOWPORT
)
1257 optval
= IP_PORTRANGE_LOW
;
1263 optval
= OPTBIT(INP_FAITH
);
1266 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1269 case IP_MULTICAST_IF
:
1270 case IP_MULTICAST_VIF
:
1271 case IP_MULTICAST_TTL
:
1272 case IP_MULTICAST_LOOP
:
1273 case IP_ADD_MEMBERSHIP
:
1274 case IP_DROP_MEMBERSHIP
:
1275 error
= ip_getmoptions(sopt
, inp
->inp_moptions
);
1279 case IP_IPSEC_POLICY
:
1281 struct mbuf
*m
= NULL
;
1285 if (error
= sooptgetm(sopt
, &m
)) /* XXX */
1287 if (error
= sooptmcopyin(sopt
, m
)) /* XXX */
1290 req
= mtod(m
, caddr_t
);
1294 error
= ipsec4_get_policy(sotoinpcb(so
), req
, len
, &m
);
1296 error
= sooptmcopyout(sopt
, m
); /* XXX */
1298 /* if error, m_freem called at soopt_mcopyout(). */
1306 error
= ENOPROTOOPT
;
1315 * Set up IP options in pcb for insertion in output packets.
1316 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1317 * with destination address if source routed.
1320 ip_pcbopts(optname
, pcbopt
, m
)
1322 struct mbuf
**pcbopt
;
1323 register struct mbuf
*m
;
1325 register int cnt
, optlen
;
1326 register u_char
*cp
;
1329 /* turn off any old options */
1331 (void)m_free(*pcbopt
);
1333 if (m
== (struct mbuf
*)0 || m
->m_len
== 0) {
1335 * Only turning off any previous options.
1343 if (m
->m_len
% sizeof(int32_t))
1347 * IP first-hop destination address will be stored before
1348 * actual options; move other options back
1349 * and clear it when none present.
1351 if (m
->m_data
+ m
->m_len
+ sizeof(struct in_addr
) >= &m
->m_dat
[MLEN
])
1354 m
->m_len
+= sizeof(struct in_addr
);
1355 cp
= mtod(m
, u_char
*) + sizeof(struct in_addr
);
1356 ovbcopy(mtod(m
, caddr_t
), (caddr_t
)cp
, (unsigned)cnt
);
1357 bzero(mtod(m
, caddr_t
), sizeof(struct in_addr
));
1359 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1360 opt
= cp
[IPOPT_OPTVAL
];
1361 if (opt
== IPOPT_EOL
)
1363 if (opt
== IPOPT_NOP
)
1366 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
))
1368 optlen
= cp
[IPOPT_OLEN
];
1369 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
)
1380 * user process specifies route as:
1382 * D must be our final destination (but we can't
1383 * check that since we may not have connected yet).
1384 * A is first hop destination, which doesn't appear in
1385 * actual IP option, but is stored before the options.
1387 if (optlen
< IPOPT_MINOFF
- 1 + sizeof(struct in_addr
))
1389 m
->m_len
-= sizeof(struct in_addr
);
1390 cnt
-= sizeof(struct in_addr
);
1391 optlen
-= sizeof(struct in_addr
);
1392 cp
[IPOPT_OLEN
] = optlen
;
1394 * Move first hop before start of options.
1396 bcopy((caddr_t
)&cp
[IPOPT_OFFSET
+1], mtod(m
, caddr_t
),
1397 sizeof(struct in_addr
));
1399 * Then copy rest of options back
1400 * to close up the deleted entry.
1402 ovbcopy((caddr_t
)(&cp
[IPOPT_OFFSET
+1] +
1403 sizeof(struct in_addr
)),
1404 (caddr_t
)&cp
[IPOPT_OFFSET
+1],
1405 (unsigned)cnt
+ sizeof(struct in_addr
));
1409 if (m
->m_len
> MAX_IPOPTLEN
+ sizeof(struct in_addr
))
1421 * The whole multicast option thing needs to be re-thought.
1422 * Several of these options are equally applicable to non-multicast
1423 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1424 * standard option (IP_TTL).
1427 * Set the IP multicast options in response to user setsockopt().
1430 ip_setmoptions(sopt
, imop
)
1431 struct sockopt
*sopt
;
1432 struct ip_moptions
**imop
;
1436 struct in_addr addr
;
1437 struct ip_mreq mreq
;
1439 struct ip_moptions
*imo
= *imop
;
1441 struct sockaddr_in
*dst
;
1446 * No multicast option buffer attached to the pcb;
1447 * allocate one and initialize to default values.
1449 imo
= (struct ip_moptions
*) _MALLOC(sizeof(*imo
), M_IPMOPTS
,
1455 imo
->imo_multicast_ifp
= NULL
;
1456 imo
->imo_multicast_vif
= -1;
1457 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1458 imo
->imo_multicast_loop
= IP_DEFAULT_MULTICAST_LOOP
;
1459 imo
->imo_num_memberships
= 0;
1462 switch (sopt
->sopt_name
) {
1463 /* store an index number for the vif you wanna use in the send */
1464 case IP_MULTICAST_VIF
:
1465 if (legal_vif_num
== 0) {
1469 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
1472 if (!legal_vif_num(i
) && (i
!= -1)) {
1476 imo
->imo_multicast_vif
= i
;
1479 case IP_MULTICAST_IF
:
1481 * Select the interface for outgoing multicast packets.
1483 error
= sooptcopyin(sopt
, &addr
, sizeof addr
, sizeof addr
);
1487 * INADDR_ANY is used to remove a previous selection.
1488 * When no interface is selected, a default one is
1489 * chosen every time a multicast packet is sent.
1491 if (addr
.s_addr
== INADDR_ANY
) {
1492 imo
->imo_multicast_ifp
= NULL
;
1496 * The selected interface is identified by its local
1497 * IP address. Find the interface and confirm that
1498 * it supports multicasting.
1501 INADDR_TO_IFP(addr
, ifp
);
1502 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1504 error
= EADDRNOTAVAIL
;
1507 imo
->imo_multicast_ifp
= ifp
;
1511 case IP_MULTICAST_TTL
:
1513 * Set the IP time-to-live for outgoing multicast packets.
1514 * The original multicast API required a char argument,
1515 * which is inconsistent with the rest of the socket API.
1516 * We allow either a char or an int.
1518 if (sopt
->sopt_valsize
== 1) {
1520 error
= sooptcopyin(sopt
, &ttl
, 1, 1);
1523 imo
->imo_multicast_ttl
= ttl
;
1526 error
= sooptcopyin(sopt
, &ttl
, sizeof ttl
,
1533 imo
->imo_multicast_ttl
= ttl
;
1537 case IP_MULTICAST_LOOP
:
1539 * Set the loopback flag for outgoing multicast packets.
1540 * Must be zero or one. The original multicast API required a
1541 * char argument, which is inconsistent with the rest
1542 * of the socket API. We allow either a char or an int.
1544 if (sopt
->sopt_valsize
== 1) {
1546 error
= sooptcopyin(sopt
, &loop
, 1, 1);
1549 imo
->imo_multicast_loop
= !!loop
;
1552 error
= sooptcopyin(sopt
, &loop
, sizeof loop
,
1556 imo
->imo_multicast_loop
= !!loop
;
1560 case IP_ADD_MEMBERSHIP
:
1562 * Add a multicast group membership.
1563 * Group must be a valid IP multicast address.
1565 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1569 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1575 * If no interface address was provided, use the interface of
1576 * the route to the given multicast address.
1578 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
) {
1579 bzero((caddr_t
)&ro
, sizeof(ro
));
1580 dst
= (struct sockaddr_in
*)&ro
.ro_dst
;
1581 dst
->sin_len
= sizeof(*dst
);
1582 dst
->sin_family
= AF_INET
;
1583 dst
->sin_addr
= mreq
.imr_multiaddr
;
1585 if (ro
.ro_rt
== NULL
) {
1586 error
= EADDRNOTAVAIL
;
1590 ifp
= ro
.ro_rt
->rt_ifp
;
1594 INADDR_TO_IFP(mreq
.imr_interface
, ifp
);
1598 * See if we found an interface, and confirm that it
1599 * supports multicast.
1601 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1602 error
= EADDRNOTAVAIL
;
1607 * See if the membership already exists or if all the
1608 * membership slots are full.
1610 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1611 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
&&
1612 imo
->imo_membership
[i
]->inm_addr
.s_addr
1613 == mreq
.imr_multiaddr
.s_addr
)
1616 if (i
< imo
->imo_num_memberships
) {
1621 if (i
== IP_MAX_MEMBERSHIPS
) {
1622 error
= ETOOMANYREFS
;
1627 * Everything looks good; add a new record to the multicast
1628 * address list for the given interface.
1630 if ((imo
->imo_membership
[i
] =
1631 in_addmulti(&mreq
.imr_multiaddr
, ifp
)) == NULL
) {
1636 ++imo
->imo_num_memberships
;
1640 case IP_DROP_MEMBERSHIP
:
1642 * Drop a multicast group membership.
1643 * Group must be a valid IP multicast address.
1645 error
= sooptcopyin(sopt
, &mreq
, sizeof mreq
, sizeof mreq
);
1649 if (!IN_MULTICAST(ntohl(mreq
.imr_multiaddr
.s_addr
))) {
1656 * If an interface address was specified, get a pointer
1657 * to its ifnet structure.
1659 if (mreq
.imr_interface
.s_addr
== INADDR_ANY
)
1662 INADDR_TO_IFP(mreq
.imr_interface
, ifp
);
1664 error
= EADDRNOTAVAIL
;
1670 * Find the membership in the membership array.
1672 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
) {
1674 imo
->imo_membership
[i
]->inm_ifp
== ifp
) &&
1675 imo
->imo_membership
[i
]->inm_addr
.s_addr
==
1676 mreq
.imr_multiaddr
.s_addr
)
1679 if (i
== imo
->imo_num_memberships
) {
1680 error
= EADDRNOTAVAIL
;
1685 * Give up the multicast address record to which the
1686 * membership points.
1688 in_delmulti(imo
->imo_membership
[i
]);
1690 * Remove the gap in the membership array.
1692 for (++i
; i
< imo
->imo_num_memberships
; ++i
)
1693 imo
->imo_membership
[i
-1] = imo
->imo_membership
[i
];
1694 --imo
->imo_num_memberships
;
1704 * If all options have default values, no need to keep the mbuf.
1706 if (imo
->imo_multicast_ifp
== NULL
&&
1707 imo
->imo_multicast_vif
== -1 &&
1708 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
1709 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
1710 imo
->imo_num_memberships
== 0) {
1711 FREE(*imop
, M_IPMOPTS
);
1719 * Return the IP multicast options in response to user getsockopt().
1722 ip_getmoptions(sopt
, imo
)
1723 struct sockopt
*sopt
;
1724 register struct ip_moptions
*imo
;
1726 struct in_addr addr
;
1727 struct in_ifaddr
*ia
;
1732 switch (sopt
->sopt_name
) {
1733 case IP_MULTICAST_VIF
:
1735 optval
= imo
->imo_multicast_vif
;
1738 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1741 case IP_MULTICAST_IF
:
1742 if (imo
== NULL
|| imo
->imo_multicast_ifp
== NULL
)
1743 addr
.s_addr
= INADDR_ANY
;
1745 IFP_TO_IA(imo
->imo_multicast_ifp
, ia
);
1746 addr
.s_addr
= (ia
== NULL
) ? INADDR_ANY
1747 : IA_SIN(ia
)->sin_addr
.s_addr
;
1749 error
= sooptcopyout(sopt
, &addr
, sizeof addr
);
1752 case IP_MULTICAST_TTL
:
1754 optval
= coptval
= IP_DEFAULT_MULTICAST_TTL
;
1756 optval
= coptval
= imo
->imo_multicast_ttl
;
1757 if (sopt
->sopt_valsize
== 1)
1758 error
= sooptcopyout(sopt
, &coptval
, 1);
1760 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1763 case IP_MULTICAST_LOOP
:
1765 optval
= coptval
= IP_DEFAULT_MULTICAST_LOOP
;
1767 optval
= coptval
= imo
->imo_multicast_loop
;
1768 if (sopt
->sopt_valsize
== 1)
1769 error
= sooptcopyout(sopt
, &coptval
, 1);
1771 error
= sooptcopyout(sopt
, &optval
, sizeof optval
);
1775 error
= ENOPROTOOPT
;
1782 * Discard the IP multicast options.
1785 ip_freemoptions(imo
)
1786 register struct ip_moptions
*imo
;
1791 for (i
= 0; i
< imo
->imo_num_memberships
; ++i
)
1792 in_delmulti(imo
->imo_membership
[i
]);
1793 FREE(imo
, M_IPMOPTS
);
1798 * Routine called from ip_output() to loop back a copy of an IP multicast
1799 * packet to the input queue of a specified interface. Note that this
1800 * calls the output routine of the loopback "driver", but with an interface
1801 * pointer that might NOT be a loopback interface -- evil, but easier than
1802 * replicating that code here.
1805 ip_mloopback(ifp
, m
, dst
, hlen
)
1807 register struct mbuf
*m
;
1808 register struct sockaddr_in
*dst
;
1811 register struct ip
*ip
;
1814 copym
= m_copy(m
, 0, M_COPYALL
);
1815 if (copym
!= NULL
&& (copym
->m_flags
& M_EXT
|| copym
->m_len
< hlen
))
1816 copym
= m_pullup(copym
, hlen
);
1817 if (copym
!= NULL
) {
1819 * We don't bother to fragment if the IP length is greater
1820 * than the interface's MTU. Can this possibly matter?
1822 ip
= mtod(copym
, struct ip
*);
1823 ip
->ip_len
= htons((u_short
)ip
->ip_len
);
1824 ip
->ip_off
= htons((u_short
)ip
->ip_off
);
1826 ip
->ip_sum
= in_cksum(copym
, hlen
);
1830 * It's not clear whether there are any lingering
1831 * reentrancy problems in other areas which might
1832 * be exposed by using ip_input directly (in
1833 * particular, everything which modifies the packet
1834 * in-place). Yet another option is using the
1835 * protosw directly to deliver the looped back
1836 * packet. For the moment, we'll err on the side
1837 * of safety by using if_simloop().
1840 if (dst
->sin_family
!= AF_INET
) {
1841 printf("ip_mloopback: bad address family %d\n",
1843 dst
->sin_family
= AF_INET
;
1849 * We need to send all loopback traffic down to dlil in case
1850 * a filter has tapped-in.
1854 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK
, 0, PF_INET
, &lo_dl_tag
);
1857 * Stuff the 'real' ifp into the pkthdr, to be used in matching
1858 * in ip_input(); we need the loopback ifp/dl_tag passed as args
1859 * to make the loopback driver compliant with the data link
1863 { copym
->m_pkthdr
.rcvif
= ifp
;
1864 dlil_output(lo_dl_tag
, copym
, 0, (struct sockaddr
*) dst
, 0);
1866 printf("Warning: ip_output call to dlil_find_dltag failed!\n");
1870 /* if_simloop(ifp, copym, (struct sockaddr *)dst, 0);*/