1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.43 2002/10/31 19:45:48 ume Exp $ */
2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
74 #include <sys/param.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
84 #include <sys/kauth.h>
87 #include <net/route.h>
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip_var.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet/ip6.h>
95 #include <netinet/icmp6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet6/nd6.h>
101 #include <netinet6/ipsec.h>
103 #include <netinet6/ipsec6.h>
105 #include <netkey/key.h>
106 extern int ipsec_bypass
;
108 extern lck_mtx_t
*nd6_mutex
;
111 #include <security/mac.h>
114 #include <netinet6/ip6_fw.h>
116 #include <net/net_osdep.h>
118 #include <netinet/kpi_ipfilter_var.h>
121 static MALLOC_DEFINE(M_IPMOPTS
, "ip6_moptions", "internet multicast options");
125 extern u_long route_generation
;
128 struct mbuf
*ip6e_ip6
;
129 struct mbuf
*ip6e_hbh
;
130 struct mbuf
*ip6e_dest1
;
131 struct mbuf
*ip6e_rthdr
;
132 struct mbuf
*ip6e_dest2
;
135 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*,
136 struct socket
*, struct sockopt
*sopt
);
137 static int ip6_setmoptions(int, struct inpcb
*, struct mbuf
*);
138 static int ip6_getmoptions(int, struct ip6_moptions
*, struct mbuf
**);
139 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
140 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
142 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
143 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
145 extern int ip_createmoptions(struct ip_moptions
**imop
);
146 extern int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
147 extern int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
148 extern lck_mtx_t
*ip6_mutex
;
151 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
152 * header (with pri, len, nxt, hlim, src, dst).
153 * This function may modify ver and hlim only.
154 * The mbuf chain containing the packet will be freed.
155 * The mbuf opt, if present, will not be freed.
157 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
158 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
159 * which is rt_rmx.rmx_mtu.
164 struct ip6_pktopts
*opt
,
165 struct route_in6
*ro
,
167 struct ip6_moptions
*im6o
,
168 struct ifnet
**ifpp
, /* XXX: just for statistics */
171 struct ip6_hdr
*ip6
, *mhip6
;
172 struct ifnet
*ifp
, *origifp
;
174 int hlen
, tlen
, len
, off
;
175 struct route_in6 ip6route
;
176 struct sockaddr_in6
*dst
;
178 struct in6_ifaddr
*ia
= NULL
;
180 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
181 struct ip6_exthdrs exthdrs
;
182 struct in6_addr finaldst
;
183 struct route_in6
*ro_pmtu
= NULL
;
186 ipfilter_t inject_filter_ref
;
189 int needipsectun
= 0;
190 struct socket
*so
= NULL
;
191 struct secpolicy
*sp
= NULL
;
194 lck_mtx_lock(ip6_mutex
);
195 /* for AH processing. stupid to have "socket" variable in IP layer... */
196 if (ipsec_bypass
== 0)
198 so
= ipsec_getsocket(m
);
199 (void)ipsec_setsocket(m
, NULL
);
203 ip6
= mtod(m
, struct ip6_hdr
*);
204 inject_filter_ref
= ipf_get_inject_filter(m
);
206 #define MAKE_EXTHDR(hp, mp) \
209 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
210 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
211 ((eh)->ip6e_len + 1) << 3); \
217 bzero(&exthdrs
, sizeof(exthdrs
));
220 /* Hop-by-Hop options header */
221 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
222 /* Destination options header(1st part) */
223 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
225 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
226 /* Destination options header(2nd part) */
227 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
231 if (ipsec_bypass
!= 0)
234 /* get a security policy for this packet */
236 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
238 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
241 IPSEC_STAT_INCREMENT(ipsec6stat
.out_inval
);
248 switch (sp
->policy
) {
249 case IPSEC_POLICY_DISCARD
:
250 case IPSEC_POLICY_GENERATE
:
252 * This packet is just discarded.
254 IPSEC_STAT_INCREMENT(ipsec6stat
.out_polvio
);
257 case IPSEC_POLICY_BYPASS
:
258 case IPSEC_POLICY_NONE
:
259 /* no need to do IPsec. */
263 case IPSEC_POLICY_IPSEC
:
264 if (sp
->req
== NULL
) {
265 /* acquire a policy */
266 error
= key_spdacquire(sp
);
272 case IPSEC_POLICY_ENTRUST
:
274 printf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
280 * Calculate the total length of the extension header chain.
281 * Keep the length of the unfragmentable part for fragmentation.
284 if (exthdrs
.ip6e_hbh
) optlen
+= exthdrs
.ip6e_hbh
->m_len
;
285 if (exthdrs
.ip6e_dest1
) optlen
+= exthdrs
.ip6e_dest1
->m_len
;
286 if (exthdrs
.ip6e_rthdr
) optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
287 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
288 /* NOTE: we don't add AH/ESP length here. do that later. */
289 if (exthdrs
.ip6e_dest2
) optlen
+= exthdrs
.ip6e_dest2
->m_len
;
292 * If we need IPsec, or there is at least one extension header,
293 * separate IP6 header from the payload.
295 if ((needipsec
|| optlen
) && !hdrsplit
) {
296 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
300 m
= exthdrs
.ip6e_ip6
;
305 ip6
= mtod(m
, struct ip6_hdr
*);
307 /* adjust mbuf packet header length */
308 m
->m_pkthdr
.len
+= optlen
;
309 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
311 /* If this is a jumbo payload, insert a jumbo payload option. */
312 if (plen
> IPV6_MAXPACKET
) {
314 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
318 m
= exthdrs
.ip6e_ip6
;
322 ip6
= mtod(m
, struct ip6_hdr
*);
323 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
327 ip6
->ip6_plen
= htons(plen
);
330 * Concatenate headers and fill in next header fields.
331 * Here we have, on "m"
333 * and we insert headers accordingly. Finally, we should be getting:
334 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
336 * during the header composing process, "m" points to IPv6 header.
337 * "mprev" points to an extension header prior to esp.
340 u_char
*nexthdrp
= &ip6
->ip6_nxt
;
341 struct mbuf
*mprev
= m
;
344 * we treat dest2 specially. this makes IPsec processing
345 * much easier. the goal here is to make mprev point the
346 * mbuf prior to dest2.
348 * result: IPv6 dest2 payload
349 * m and mprev will point to IPv6 header.
351 if (exthdrs
.ip6e_dest2
) {
353 panic("assumption failed: hdr not split");
354 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
355 m
->m_next
= exthdrs
.ip6e_dest2
;
356 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
357 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
360 #define MAKE_CHAIN(m, mp, p, i)\
364 panic("assumption failed: hdr not split"); \
365 *mtod((m), u_char *) = *(p);\
367 p = mtod((m), u_char *);\
368 (m)->m_next = (mp)->m_next;\
374 * result: IPv6 hbh dest1 rthdr dest2 payload
375 * m will point to IPv6 header. mprev will point to the
376 * extension header prior to dest2 (rthdr in the above case).
378 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
,
379 nexthdrp
, IPPROTO_HOPOPTS
);
380 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
,
381 nexthdrp
, IPPROTO_DSTOPTS
);
382 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
,
383 nexthdrp
, IPPROTO_ROUTING
);
385 if (!TAILQ_EMPTY(&ipv6_filters
)) {
386 struct ipfilter
*filter
;
387 int seen
= (inject_filter_ref
== 0);
389 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
391 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
393 ippo
->ippo_flags
= IPPOF_MCAST_OPTS
;
394 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
395 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
396 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
399 /* Hack: embed the scope_id in the destination */
400 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
401 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
403 ip6
->ip6_dst
.s6_addr16
[1] = htons(ro
->ro_dst
.sin6_scope_id
);
406 lck_mtx_unlock(ip6_mutex
);
408 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
410 * No need to proccess packet twice if we've
414 if ((struct ipfilter
*)inject_filter_ref
== filter
)
416 } else if (filter
->ipf_filter
.ipf_output
) {
419 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
420 if (result
== EJUSTRETURN
) {
422 locked
= 1; /* Don't want to take lock to unlock it right away */
427 locked
= 1; /* Don't want to take lock to unlock it right away */
433 lck_mtx_lock(ip6_mutex
);
435 ip6
= mtod(m
, struct ip6_hdr
*);
436 /* Hack: cleanup embedded scope_id if we put it there */
438 ip6
->ip6_dst
.s6_addr16
[1] = 0;
446 * pointers after IPsec headers are not valid any more.
447 * other pointers need a great care too.
448 * (IPsec routines should not mangle mbufs prior to AH/ESP)
450 exthdrs
.ip6e_dest2
= NULL
;
453 struct ip6_rthdr
*rh
= NULL
;
455 struct ipsec_output_state state
;
457 if (exthdrs
.ip6e_rthdr
) {
458 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
459 segleft_org
= rh
->ip6r_segleft
;
460 rh
->ip6r_segleft
= 0;
463 bzero(&state
, sizeof(state
));
465 lck_mtx_unlock(ip6_mutex
);
466 error
= ipsec6_output_trans(&state
, nexthdrp
, mprev
, sp
, flags
,
468 lck_mtx_lock(ip6_mutex
);
471 /* mbuf is already reclaimed in ipsec6_output_trans. */
481 printf("ip6_output (ipsec): error code %d\n", error
);
484 /* don't show these error codes to the user */
490 if (exthdrs
.ip6e_rthdr
) {
491 /* ah6_output doesn't modify mbuf chain */
492 rh
->ip6r_segleft
= segleft_org
;
500 * If there is a routing header, replace destination address field
501 * with the first hop of the routing header.
503 if (exthdrs
.ip6e_rthdr
) {
504 struct ip6_rthdr
*rh
=
505 (struct ip6_rthdr
*)(mtod(exthdrs
.ip6e_rthdr
,
506 struct ip6_rthdr
*));
507 struct ip6_rthdr0
*rh0
;
509 finaldst
= ip6
->ip6_dst
;
510 switch (rh
->ip6r_type
) {
511 case IPV6_RTHDR_TYPE_0
:
512 rh0
= (struct ip6_rthdr0
*)rh
;
513 ip6
->ip6_dst
= rh0
->ip6r0_addr
[0];
514 bcopy((caddr_t
)&rh0
->ip6r0_addr
[1],
515 (caddr_t
)&rh0
->ip6r0_addr
[0],
516 sizeof(struct in6_addr
)*(rh0
->ip6r0_segleft
- 1)
518 rh0
->ip6r0_addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
520 default: /* is it possible? */
526 /* Source address validation */
527 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
528 (flags
& IPV6_DADOUTPUT
) == 0) {
530 ip6stat
.ip6s_badscope
++;
533 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
535 ip6stat
.ip6s_badscope
++;
539 ip6stat
.ip6s_localout
++;
546 bzero((caddr_t
)ro
, sizeof(*ro
));
549 if (opt
&& opt
->ip6po_rthdr
)
550 ro
= &opt
->ip6po_route
;
551 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
553 * If there is a cached route,
554 * check that it is to the same destination
555 * and is still up. If not, free it and try again.
557 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
558 dst
->sin6_family
!= AF_INET6
||
559 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
) ||
560 ro
->ro_rt
->generation_id
!= route_generation
)) {
562 ro
->ro_rt
= (struct rtentry
*)0;
564 if (ro
->ro_rt
== 0) {
565 bzero(dst
, sizeof(*dst
));
566 dst
->sin6_family
= AF_INET6
;
567 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
568 dst
->sin6_addr
= ip6
->ip6_dst
;
570 /* XXX: sin6_scope_id should already be fixed at this point */
571 if (IN6_IS_SCOPE_LINKLOCAL(&dst
->sin6_addr
))
572 dst
->sin6_scope_id
= ntohs(dst
->sin6_addr
.s6_addr16
[1]);
576 if (needipsec
&& needipsectun
) {
577 struct ipsec_output_state state
;
581 * All the extension headers will become inaccessible
582 * (since they can be encrypted).
583 * Don't panic, we need no more updates to extension headers
584 * on inner IPv6 packet (since they are now encapsulated).
586 * IPv6 [ESP|AH] IPv6 [extension headers] payload
588 bzero(&exthdrs
, sizeof(exthdrs
));
589 exthdrs
.ip6e_ip6
= m
;
591 bzero(&state
, sizeof(state
));
593 state
.ro
= (struct route
*)ro
;
594 state
.dst
= (struct sockaddr
*)dst
;
595 lck_mtx_unlock(ip6_mutex
);
596 error
= ipsec6_output_tunnel(&state
, sp
, flags
, &tunneledv4
);
597 lck_mtx_lock(ip6_mutex
);
598 if (tunneledv4
) /* tunneled in IPv4 - packet is gone */
601 ro
= (struct route_in6
*)state
.ro
;
602 dst
= (struct sockaddr_in6
*)state
.dst
;
604 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
615 printf("ip6_output (ipsec): error code %d\n", error
);
618 /* don't show these error codes to the user */
625 exthdrs
.ip6e_ip6
= m
;
629 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
632 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
633 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
635 * interface selection comes here
636 * if an interface is specified from an upper layer,
639 lck_mtx_lock(rt_mtx
);
640 if (ro
->ro_rt
== 0) {
642 * non-bsdi always clone routes, if parent is
645 rtalloc_ign_locked((struct route
*)ro
, 0UL);
647 if (ro
->ro_rt
== 0) {
648 ip6stat
.ip6s_noroute
++;
649 error
= EHOSTUNREACH
;
650 lck_mtx_unlock(rt_mtx
);
651 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
654 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
655 ifp
= ro
->ro_rt
->rt_ifp
;
657 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
658 dst
= (struct sockaddr_in6
*)ro
->ro_rt
->rt_gateway
;
659 lck_mtx_unlock(rt_mtx
);
660 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
662 in6_ifstat_inc(ifp
, ifs6_out_request
);
665 * Check if the outgoing interface conflicts with
666 * the interface specified by ifi6_ifindex (if specified).
667 * Note that loopback interface is always okay.
668 * (this may happen when we are sending a packet to one of
669 * our own addresses.)
671 if (opt
&& opt
->ip6po_pktinfo
672 && opt
->ip6po_pktinfo
->ipi6_ifindex
) {
673 if (!(ifp
->if_flags
& IFF_LOOPBACK
)
674 && ifp
->if_index
!= opt
->ip6po_pktinfo
->ipi6_ifindex
) {
675 ip6stat
.ip6s_noroute
++;
676 in6_ifstat_inc(ifp
, ifs6_out_discard
);
677 error
= EHOSTUNREACH
;
682 if (opt
&& opt
->ip6po_hlim
!= -1)
683 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
686 struct in6_multi
*in6m
;
688 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
691 * See if the caller provided any multicast options
695 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
696 if (im6o
->im6o_multicast_ifp
!= NULL
)
697 ifp
= im6o
->im6o_multicast_ifp
;
699 ip6
->ip6_hlim
= ip6_defmcasthlim
;
702 * See if the caller provided the outgoing interface
703 * as an ancillary data.
704 * Boundary check for ifindex is assumed to be already done.
706 if (opt
&& opt
->ip6po_pktinfo
&& opt
->ip6po_pktinfo
->ipi6_ifindex
)
707 ifp
= ifindex2ifnet
[opt
->ip6po_pktinfo
->ipi6_ifindex
];
710 * If the destination is a node-local scope multicast,
711 * the packet should be loop-backed only.
713 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
)) {
715 * If the outgoing interface is already specified,
716 * it should be a loopback interface.
718 if (ifp
&& (ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
719 ip6stat
.ip6s_badscope
++;
720 error
= ENETUNREACH
; /* XXX: better error? */
721 /* XXX correct ifp? */
722 in6_ifstat_inc(ifp
, ifs6_out_discard
);
729 if (opt
&& opt
->ip6po_hlim
!= -1)
730 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
733 * If caller did not provide an interface lookup a
734 * default in the routing table. This is either a
735 * default for the speicfied group (i.e. a host
736 * route), or a multicast default (a route for the
740 lck_mtx_lock(rt_mtx
);
741 if (ro
->ro_rt
== 0) {
742 ro
->ro_rt
= rtalloc1_locked(
743 (struct sockaddr
*)&ro
->ro_dst
, 0, 0UL);
745 if (ro
->ro_rt
== 0) {
746 ip6stat
.ip6s_noroute
++;
747 lck_mtx_unlock(rt_mtx
);
748 error
= EHOSTUNREACH
;
749 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
752 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
753 ifp
= ro
->ro_rt
->rt_ifp
;
755 lck_mtx_unlock(rt_mtx
);
758 if ((flags
& IPV6_FORWARDING
) == 0)
759 in6_ifstat_inc(ifp
, ifs6_out_request
);
760 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
763 * Confirm that the outgoing interface supports multicast.
765 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
766 ip6stat
.ip6s_noroute
++;
767 in6_ifstat_inc(ifp
, ifs6_out_discard
);
771 ifnet_lock_shared(ifp
);
772 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
773 ifnet_lock_done(ifp
);
775 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
777 * If we belong to the destination multicast group
778 * on the outgoing interface, and the caller did not
779 * forbid loopback, loop back a copy.
781 ip6_mloopback(ifp
, m
, dst
);
784 * If we are acting as a multicast router, perform
785 * multicast forwarding as if the packet had just
786 * arrived on the interface to which we are about
787 * to send. The multicast forwarding function
788 * recursively calls this function, using the
789 * IPV6_FORWARDING flag to prevent infinite recursion.
791 * Multicasts that are looped back by ip6_mloopback(),
792 * above, will be forwarded by the ip6_input() routine,
795 if (ip6_mrouter
&& (flags
& IPV6_FORWARDING
) == 0) {
796 if (ip6_mforward(ip6
, ifp
, m
) != 0) {
803 * Multicasts with a hoplimit of zero may be looped back,
804 * above, but must not be transmitted on a network.
805 * Also, multicasts addressed to the loopback interface
806 * are not sent -- the above call to ip6_mloopback() will
807 * loop back a copy if this host actually belongs to the
808 * destination group on the loopback interface.
810 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
)) {
817 * Fill the outgoing inteface to tell the upper layer
818 * to increment per-interface statistics.
824 * Determine path MTU.
827 /* The first hop and the final destination may differ. */
828 struct sockaddr_in6
*sin6_fin
=
829 (struct sockaddr_in6
*)&ro_pmtu
->ro_dst
;
830 if (ro_pmtu
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
831 !IN6_ARE_ADDR_EQUAL(&sin6_fin
->sin6_addr
,
833 rtfree(ro_pmtu
->ro_rt
);
834 ro_pmtu
->ro_rt
= (struct rtentry
*)0;
836 if (ro_pmtu
->ro_rt
== 0) {
837 bzero(sin6_fin
, sizeof(*sin6_fin
));
838 sin6_fin
->sin6_family
= AF_INET6
;
839 sin6_fin
->sin6_len
= sizeof(struct sockaddr_in6
);
840 sin6_fin
->sin6_addr
= finaldst
;
842 rtalloc((struct route
*)ro_pmtu
);
845 if (ro_pmtu
->ro_rt
!= NULL
) {
846 u_int32_t ifmtu
= IN6_LINKMTU(ifp
);
848 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
849 if (mtu
> ifmtu
|| mtu
== 0) {
851 * The MTU on the route is larger than the MTU on
852 * the interface! This shouldn't happen, unless the
853 * MTU of the interface has been changed after the
854 * interface was brought up. Change the MTU in the
855 * route to match the interface MTU (as long as the
856 * field isn't locked).
858 * if MTU on the route is 0, we need to fix the MTU.
859 * this case happens with path MTU discovery timeouts.
862 if ((ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0)
863 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
866 mtu
= IN6_LINKMTU(ifp
);
870 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
872 if ((flags
& IPV6_MINMTU
) != 0 && mtu
> IPV6_MMTU
)
875 /* Fake scoped addresses */
876 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
878 * If source or destination address is a scoped address, and
879 * the packet is going to be sent to a loopback interface,
880 * we should keep the original interface.
884 * XXX: this is a very experimental and temporary solution.
885 * We eventually have sockaddr_in6 and use the sin6_scope_id
886 * field of the structure here.
887 * We rely on the consistency between two scope zone ids
888 * of source and destination, which should already be assured.
889 * Larger scopes than link will be supported in the future.
892 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
893 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_src
.s6_addr16
[1])];
894 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
895 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_dst
.s6_addr16
[1])];
897 * XXX: origifp can be NULL even in those two cases above.
898 * For example, if we remove the (only) link-local address
899 * from the loopback interface, and try to send a link-local
900 * address without link-id information. Then the source
901 * address is ::1, and the destination address is the
902 * link-local address with its s6_addr16[1] being zero.
903 * What is worse, if the packet goes to the loopback interface
904 * by a default rejected route, the null pointer would be
905 * passed to looutput, and the kernel would hang.
906 * The following last resort would prevent such disaster.
913 #ifndef SCOPEDROUTING
915 * clear embedded scope identifiers if necessary.
916 * in6_clearscope will touch the addresses only when necessary.
918 in6_clearscope(&ip6
->ip6_src
);
919 in6_clearscope(&ip6
->ip6_dst
);
923 * Check with the firewall...
925 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
927 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
928 /* If ipfw says divert, we have to just drop packet */
929 if (ip6_fw_chk_ptr(&ip6
, ifp
, &port
, &m
)) {
940 * If the outgoing packet contains a hop-by-hop options header,
941 * it must be examined and processed even by the source node.
942 * (RFC 2460, section 4.)
944 if (exthdrs
.ip6e_hbh
) {
945 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
946 u_int32_t dummy1
; /* XXX unused */
947 u_int32_t dummy2
; /* XXX unused */
950 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
951 panic("ip6e_hbh is not continuous");
954 * XXX: if we have to send an ICMPv6 error to the sender,
955 * we need the M_LOOP flag since icmp6_error() expects
956 * the IPv6 and the hop-by-hop options header are
957 * continuous unless the flag is set.
959 m
->m_flags
|= M_LOOP
;
960 m
->m_pkthdr
.rcvif
= ifp
;
961 if (ip6_process_hopopts(m
,
962 (u_int8_t
*)(hbh
+ 1),
963 ((hbh
->ip6h_len
+ 1) << 3) -
964 sizeof(struct ip6_hbh
),
965 &dummy1
, &dummy2
) < 0) {
966 /* m was already freed at this point */
967 error
= EINVAL
;/* better error? */
970 m
->m_flags
&= ~M_LOOP
; /* XXX */
971 m
->m_pkthdr
.rcvif
= NULL
;
975 * Send the packet to the outgoing interface.
976 * If necessary, do IPv6 fragmentation before sending.
978 tlen
= m
->m_pkthdr
.len
;
982 * On any link that cannot convey a 1280-octet packet in one piece,
983 * link-specific fragmentation and reassembly must be provided at
984 * a layer below IPv6. [RFC 2460, sec.5]
985 * Thus if the interface has ability of link-level fragmentation,
986 * we can just send the packet even if the packet size is
987 * larger than the link's MTU.
988 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
991 || ifp
->if_flags
& IFF_FRAGMENTABLE
995 /* Record statistics for this interface address. */
996 if (ia
&& !(flags
& IPV6_FORWARDING
)) {
998 ia
->ia_ifa
.if_opackets
++;
999 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1003 /* clean ipsec history once it goes out of the node */
1007 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
, 1);
1009 } else if (mtu
< IPV6_MMTU
) {
1011 * note that path MTU is never less than IPV6_MMTU
1012 * (see icmp6_input).
1015 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1017 } else if (ip6
->ip6_plen
== 0) { /* jumbo payload cannot be fragmented */
1019 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1022 struct mbuf
**mnext
, *m_frgpart
;
1023 struct ip6_frag
*ip6f
= NULL
;
1024 u_int32_t id
= htonl(ip6_id
++);
1028 * Too large for the destination or interface;
1029 * fragment if possible.
1030 * Must be able to put at least 8 bytes per fragment.
1032 hlen
= unfragpartlen
;
1033 if (mtu
> IPV6_MAXPACKET
)
1034 mtu
= IPV6_MAXPACKET
;
1036 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
1039 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1043 mnext
= &m
->m_nextpkt
;
1046 * Change the next header field of the last header in the
1047 * unfragmentable part.
1049 if (exthdrs
.ip6e_rthdr
) {
1050 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1051 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1052 } else if (exthdrs
.ip6e_dest1
) {
1053 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1054 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1055 } else if (exthdrs
.ip6e_hbh
) {
1056 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1057 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1059 nextproto
= ip6
->ip6_nxt
;
1060 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1064 * Loop through length of segment after first fragment,
1065 * make new header and copy data of each part and link onto
1069 for (off
= hlen
; off
< tlen
; off
+= len
) {
1070 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
1073 ip6stat
.ip6s_odropped
++;
1076 m
->m_pkthdr
.rcvif
= NULL
;
1077 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1079 mnext
= &m
->m_nextpkt
;
1080 m
->m_data
+= max_linkhdr
;
1081 mhip6
= mtod(m
, struct ip6_hdr
*);
1083 m
->m_len
= sizeof(*mhip6
);
1084 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1086 ip6stat
.ip6s_odropped
++;
1089 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1090 if (off
+ len
>= tlen
)
1093 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1094 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1096 sizeof(struct ip6_hdr
)));
1097 if ((m_frgpart
= m_copy(m0
, off
, len
)) == 0) {
1099 ip6stat
.ip6s_odropped
++;
1102 m_cat(m
, m_frgpart
);
1103 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1104 m
->m_pkthdr
.rcvif
= 0;
1105 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1106 #ifdef __darwin8_notyet
1108 mac_create_fragment(m0
, m
);
1111 ip6f
->ip6f_reserved
= 0;
1112 ip6f
->ip6f_ident
= id
;
1113 ip6f
->ip6f_nxt
= nextproto
;
1114 ip6stat
.ip6s_ofragments
++;
1115 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1118 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1122 * Remove leading garbages.
1128 for (m0
= m
; m
; m
= m0
) {
1132 /* Record statistics for this interface address. */
1135 ia
->ia_ifa
.if_opackets
++;
1136 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1140 /* clean ipsec history once it goes out of the node */
1143 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
, 1);
1150 ip6stat
.ip6s_fragmented
++;
1154 lck_mtx_unlock(ip6_mutex
);
1155 if (ro
== &ip6route
&& ro
->ro_rt
) { /* brace necessary for rtfree */
1158 } else if (ro_pmtu
== &ip6route
&& ro_pmtu
->ro_rt
) {
1159 rtfree(ro_pmtu
->ro_rt
);
1160 ro_pmtu
->ro_rt
= NULL
;
1165 key_freesp(sp
, KEY_SADB_UNLOCKED
);
1171 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1172 m_freem(exthdrs
.ip6e_dest1
);
1173 m_freem(exthdrs
.ip6e_rthdr
);
1174 m_freem(exthdrs
.ip6e_dest2
);
1182 ip6_copyexthdr(mp
, hdr
, hlen
)
1189 if (hlen
> MCLBYTES
)
1190 return(ENOBUFS
); /* XXX */
1192 MGET(m
, M_DONTWAIT
, MT_DATA
);
1197 MCLGET(m
, M_DONTWAIT
);
1198 if ((m
->m_flags
& M_EXT
) == 0) {
1205 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1212 * Insert jumbo payload option.
1215 ip6_insert_jumboopt(exthdrs
, plen
)
1216 struct ip6_exthdrs
*exthdrs
;
1223 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1226 * If there is no hop-by-hop options header, allocate new one.
1227 * If there is one but it doesn't have enough space to store the
1228 * jumbo payload option, allocate a cluster to store the whole options.
1229 * Otherwise, use it to store the options.
1231 if (exthdrs
->ip6e_hbh
== 0) {
1232 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1235 mopt
->m_len
= JUMBOOPTLEN
;
1236 optbuf
= mtod(mopt
, u_char
*);
1237 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1238 exthdrs
->ip6e_hbh
= mopt
;
1240 struct ip6_hbh
*hbh
;
1242 mopt
= exthdrs
->ip6e_hbh
;
1243 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1246 * - exthdrs->ip6e_hbh is not referenced from places
1247 * other than exthdrs.
1248 * - exthdrs->ip6e_hbh is not an mbuf chain.
1250 int oldoptlen
= mopt
->m_len
;
1254 * XXX: give up if the whole (new) hbh header does
1255 * not fit even in an mbuf cluster.
1257 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1261 * As a consequence, we must always prepare a cluster
1264 MGET(n
, M_DONTWAIT
, MT_DATA
);
1266 MCLGET(n
, M_DONTWAIT
);
1267 if ((n
->m_flags
& M_EXT
) == 0) {
1274 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1275 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1277 optbuf
= (u_char
*) (mtod(n
, caddr_t
) + oldoptlen
);
1279 mopt
= exthdrs
->ip6e_hbh
= n
;
1281 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1282 mopt
->m_len
+= JUMBOOPTLEN
;
1284 optbuf
[0] = IP6OPT_PADN
;
1288 * Adjust the header length according to the pad and
1289 * the jumbo payload option.
1291 hbh
= mtod(mopt
, struct ip6_hbh
*);
1292 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1295 /* fill in the option. */
1296 optbuf
[2] = IP6OPT_JUMBO
;
1298 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1299 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1301 /* finally, adjust the packet header length */
1302 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1309 * Insert fragment header and copy unfragmentable header portions.
1312 ip6_insertfraghdr(m0
, m
, hlen
, frghdrp
)
1313 struct mbuf
*m0
, *m
;
1315 struct ip6_frag
**frghdrp
;
1317 struct mbuf
*n
, *mlast
;
1319 if (hlen
> sizeof(struct ip6_hdr
)) {
1320 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1321 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
1328 /* Search for the last mbuf of unfragmentable part. */
1329 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1332 if ((mlast
->m_flags
& M_EXT
) == 0 &&
1333 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1334 /* use the trailing space of the last mbuf for the fragment hdr */
1336 (struct ip6_frag
*)(mtod(mlast
, caddr_t
) + mlast
->m_len
);
1337 mlast
->m_len
+= sizeof(struct ip6_frag
);
1338 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1340 /* allocate a new mbuf for the fragment header */
1343 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1346 mfrg
->m_len
= sizeof(struct ip6_frag
);
1347 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1348 mlast
->m_next
= mfrg
;
1354 extern int load_ipfw(void);
1357 * IP6 socket option processing.
1360 ip6_ctloutput(so
, sopt
)
1362 struct sockopt
*sopt
;
1365 struct inpcb
*in6p
= sotoinpcb(so
);
1367 int level
, op
= -1, optname
= 0;
1369 struct proc
*p
= NULL
;
1371 level
= error
= optval
= 0;
1373 panic("ip6_ctloutput: arg soopt is NULL");
1375 level
= sopt
->sopt_level
;
1376 op
= sopt
->sopt_dir
;
1377 optname
= sopt
->sopt_name
;
1378 optlen
= sopt
->sopt_valsize
;
1382 privileged
= (p
== 0 || proc_suser(p
)) ? 0 : 1;
1384 if (level
== IPPROTO_IPV6
) {
1389 case IPV6_PKTOPTIONS
:
1393 if (sopt
->sopt_valsize
> MCLBYTES
) {
1397 error
= soopt_getm(sopt
, &m
); /* XXX */
1400 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1403 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1405 m_freem(m
); /* XXX */
1410 * Use of some Hop-by-Hop options or some
1411 * Destination options, might require special
1412 * privilege. That is, normal applications
1413 * (without special privilege) might be forbidden
1414 * from setting certain options in outgoing packets,
1415 * and might never see certain options in received
1416 * packets. [RFC 2292 Section 6]
1417 * KAME specific note:
1418 * KAME prevents non-privileged users from sending or
1419 * receiving ANY hbh/dst options in order to avoid
1420 * overhead of parsing options in the kernel.
1422 case IPV6_UNICAST_HOPS
:
1427 if (optlen
!= sizeof(int)) {
1431 error
= sooptcopyin(sopt
, &optval
,
1432 sizeof optval
, sizeof optval
);
1437 case IPV6_UNICAST_HOPS
:
1438 if (optval
< -1 || optval
>= 256)
1441 /* -1 = kernel default */
1442 in6p
->in6p_hops
= optval
;
1444 if ((in6p
->in6p_vflag
&
1446 in6p
->inp_ip_ttl
= optval
;
1449 #define OPTSET(bit) \
1452 in6p->in6p_flags |= (bit); \
1454 in6p->in6p_flags &= ~(bit); \
1456 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1459 in6p
->in6p_cksum
= optval
;
1468 * make setsockopt(IPV6_V6ONLY)
1469 * available only prior to bind(2).
1470 * see ipng mailing list, Jun 22 2001.
1472 if (in6p
->in6p_lport
||
1473 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
))
1478 OPTSET(IN6P_IPV6_V6ONLY
);
1480 in6p
->in6p_vflag
&= ~INP_IPV4
;
1482 in6p
->in6p_vflag
|= INP_IPV4
;
1493 if (optlen
!= sizeof(int)) {
1497 error
= sooptcopyin(sopt
, &optval
,
1498 sizeof optval
, sizeof optval
);
1503 OPTSET(IN6P_PKTINFO
);
1506 OPTSET(IN6P_HOPLIMIT
);
1510 * Check super-user privilege.
1511 * See comments for IPV6_RECVHOPOPTS.
1515 OPTSET(IN6P_HOPOPTS
);
1520 OPTSET(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1529 case IPV6_MULTICAST_IF
:
1530 case IPV6_MULTICAST_HOPS
:
1531 case IPV6_MULTICAST_LOOP
:
1532 case IPV6_JOIN_GROUP
:
1533 case IPV6_LEAVE_GROUP
:
1536 if (sopt
->sopt_valsize
> MLEN
) {
1541 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1546 m
->m_len
= sopt
->sopt_valsize
;
1547 error
= sooptcopyin(sopt
, mtod(m
, char *),
1548 m
->m_len
, m
->m_len
);
1549 error
= ip6_setmoptions(sopt
->sopt_name
, in6p
, m
);
1554 case IPV6_PORTRANGE
:
1555 error
= sooptcopyin(sopt
, &optval
,
1556 sizeof optval
, sizeof optval
);
1561 case IPV6_PORTRANGE_DEFAULT
:
1562 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1563 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1566 case IPV6_PORTRANGE_HIGH
:
1567 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1568 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1571 case IPV6_PORTRANGE_LOW
:
1572 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1573 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1583 case IPV6_IPSEC_POLICY
:
1589 if (sopt
->sopt_valsize
> MCLBYTES
) {
1593 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1595 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1598 req
= mtod(m
, caddr_t
);
1601 error
= ipsec6_set_policy(in6p
, optname
, req
,
1606 #endif /* KAME IPSEC */
1614 if (ip6_fw_ctl_ptr
== NULL
&& load_ipfw() != 0)
1617 error
= (*ip6_fw_ctl_ptr
)(sopt
);
1620 #endif /* IPFIREWALL */
1623 error
= ENOPROTOOPT
;
1631 case IPV6_PKTOPTIONS
:
1632 if (in6p
->in6p_options
) {
1634 m
= m_copym(in6p
->in6p_options
,
1635 0, M_COPYALL
, M_WAIT
);
1636 error
= soopt_mcopyout(sopt
, m
);
1640 sopt
->sopt_valsize
= 0;
1643 case IPV6_UNICAST_HOPS
:
1648 case IPV6_PORTRANGE
:
1651 case IPV6_UNICAST_HOPS
:
1652 optval
= in6p
->in6p_hops
;
1656 optval
= in6p
->in6p_cksum
;
1660 optval
= OPTBIT(IN6P_FAITH
);
1664 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1667 case IPV6_PORTRANGE
:
1670 flags
= in6p
->in6p_flags
;
1671 if (flags
& IN6P_HIGHPORT
)
1672 optval
= IPV6_PORTRANGE_HIGH
;
1673 else if (flags
& IN6P_LOWPORT
)
1674 optval
= IPV6_PORTRANGE_LOW
;
1680 error
= sooptcopyout(sopt
, &optval
,
1689 if ((optname
== IPV6_HOPOPTS
||
1690 optname
== IPV6_DSTOPTS
) &&
1695 optval
= OPTBIT(IN6P_PKTINFO
);
1698 optval
= OPTBIT(IN6P_HOPLIMIT
);
1703 optval
= OPTBIT(IN6P_HOPOPTS
);
1706 optval
= OPTBIT(IN6P_RTHDR
);
1711 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1714 error
= sooptcopyout(sopt
, &optval
,
1718 case IPV6_MULTICAST_IF
:
1719 case IPV6_MULTICAST_HOPS
:
1720 case IPV6_MULTICAST_LOOP
:
1721 case IPV6_JOIN_GROUP
:
1722 case IPV6_LEAVE_GROUP
:
1725 error
= ip6_getmoptions(sopt
->sopt_name
,
1726 in6p
->in6p_moptions
, &m
);
1728 error
= sooptcopyout(sopt
,
1729 mtod(m
, char *), m
->m_len
);
1735 case IPV6_IPSEC_POLICY
:
1739 struct mbuf
*m
= NULL
;
1740 struct mbuf
**mp
= &m
;
1742 if (sopt
->sopt_valsize
> MCLBYTES
) {
1746 error
= soopt_getm(sopt
, &m
); /* XXX */
1749 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1753 req
= mtod(m
, caddr_t
);
1756 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
1758 error
= soopt_mcopyout(sopt
, m
); /*XXX*/
1759 if (error
== 0 && m
)
1763 #endif /* KAME IPSEC */
1768 if (ip6_fw_ctl_ptr
== NULL
&& load_ipfw() != 0)
1771 error
= (*ip6_fw_ctl_ptr
)(sopt
);
1774 #endif /* IPFIREWALL */
1777 error
= ENOPROTOOPT
;
1789 * Set up IP6 options in pcb for insertion in output packets or
1790 * specifying behavior of outgoing packets.
1794 struct ip6_pktopts
**pktopt
,
1796 __unused
struct socket
*so
,
1797 struct sockopt
*sopt
)
1799 struct ip6_pktopts
*opt
= *pktopt
;
1801 struct proc
*p
= sopt
->sopt_p
;
1804 /* turn off any old options. */
1807 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
1808 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
1809 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1810 printf("ip6_pcbopts: all specified options are cleared.\n");
1812 ip6_clearpktopts(opt
, 1, -1);
1814 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
1817 if (!m
|| m
->m_len
== 0) {
1819 * Only turning off any previous options, regardless of
1820 * whether the opt is just created or given.
1823 FREE(opt
, M_IP6OPT
);
1827 /* set options specified by user. */
1828 if (p
&& !proc_suser(p
))
1830 if ((error
= ip6_setpktoptions(m
, opt
, priv
, 1)) != 0) {
1831 ip6_clearpktopts(opt
, 1, -1); /* XXX: discard all options */
1832 FREE(opt
, M_IP6OPT
);
1840 * initialize ip6_pktopts. beware that there are non-zero default values in
1844 init_ip6pktopts(opt
)
1845 struct ip6_pktopts
*opt
;
1848 bzero(opt
, sizeof(*opt
));
1849 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
1853 ip6_clearpktopts(pktopt
, needfree
, optname
)
1854 struct ip6_pktopts
*pktopt
;
1855 int needfree
, optname
;
1860 if (optname
== -1) {
1861 if (needfree
&& pktopt
->ip6po_pktinfo
)
1862 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
1863 pktopt
->ip6po_pktinfo
= NULL
;
1866 pktopt
->ip6po_hlim
= -1;
1867 if (optname
== -1) {
1868 if (needfree
&& pktopt
->ip6po_nexthop
)
1869 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
1870 pktopt
->ip6po_nexthop
= NULL
;
1872 if (optname
== -1) {
1873 if (needfree
&& pktopt
->ip6po_hbh
)
1874 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
1875 pktopt
->ip6po_hbh
= NULL
;
1877 if (optname
== -1) {
1878 if (needfree
&& pktopt
->ip6po_dest1
)
1879 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
1880 pktopt
->ip6po_dest1
= NULL
;
1882 if (optname
== -1) {
1883 if (needfree
&& pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1884 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
1885 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
1886 if (pktopt
->ip6po_route
.ro_rt
) {
1887 rtfree(pktopt
->ip6po_route
.ro_rt
);
1888 pktopt
->ip6po_route
.ro_rt
= NULL
;
1891 if (optname
== -1) {
1892 if (needfree
&& pktopt
->ip6po_dest2
)
1893 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
1894 pktopt
->ip6po_dest2
= NULL
;
1898 #define PKTOPT_EXTHDRCPY(type) \
1902 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1903 dst->type = _MALLOC(hlen, M_IP6OPT, canwait);\
1904 if (dst->type == NULL && canwait == M_NOWAIT)\
1906 bcopy(src->type, dst->type, hlen);\
1910 struct ip6_pktopts
*
1911 ip6_copypktopts(src
, canwait
)
1912 struct ip6_pktopts
*src
;
1915 struct ip6_pktopts
*dst
;
1918 printf("ip6_clearpktopts: invalid argument\n");
1922 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
1923 if (dst
== NULL
&& canwait
== M_NOWAIT
)
1925 bzero(dst
, sizeof(*dst
));
1927 dst
->ip6po_hlim
= src
->ip6po_hlim
;
1928 if (src
->ip6po_pktinfo
) {
1929 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
1931 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
1933 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
1935 if (src
->ip6po_nexthop
) {
1936 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
1938 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
1940 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
1941 src
->ip6po_nexthop
->sa_len
);
1943 PKTOPT_EXTHDRCPY(ip6po_hbh
);
1944 PKTOPT_EXTHDRCPY(ip6po_dest1
);
1945 PKTOPT_EXTHDRCPY(ip6po_dest2
);
1946 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
1950 if (dst
->ip6po_pktinfo
) FREE(dst
->ip6po_pktinfo
, M_IP6OPT
);
1951 if (dst
->ip6po_nexthop
) FREE(dst
->ip6po_nexthop
, M_IP6OPT
);
1952 if (dst
->ip6po_hbh
) FREE(dst
->ip6po_hbh
, M_IP6OPT
);
1953 if (dst
->ip6po_dest1
) FREE(dst
->ip6po_dest1
, M_IP6OPT
);
1954 if (dst
->ip6po_dest2
) FREE(dst
->ip6po_dest2
, M_IP6OPT
);
1955 if (dst
->ip6po_rthdr
) FREE(dst
->ip6po_rthdr
, M_IP6OPT
);
1956 FREE(dst
, M_IP6OPT
);
1959 #undef PKTOPT_EXTHDRCPY
1962 ip6_freepcbopts(pktopt
)
1963 struct ip6_pktopts
*pktopt
;
1968 ip6_clearpktopts(pktopt
, 1, -1);
1970 FREE(pktopt
, M_IP6OPT
);
1974 * Set the IP6 multicast options in response to user setsockopt().
1983 u_int loop
, ifindex
;
1984 struct ipv6_mreq
*mreq
;
1986 struct ip6_moptions
**im6op
= &in6p
->in6p_moptions
;
1987 struct ip6_moptions
*im6o
= *im6op
;
1988 struct ip_moptions
*imo
;
1989 struct route_in6 ro
;
1990 struct sockaddr_in6
*dst
;
1991 struct in6_multi_mship
*imm
;
1995 * No multicast option buffer attached to the pcb;
1996 * allocate one and initialize to default values.
1998 im6o
= (struct ip6_moptions
*)
1999 _MALLOC(sizeof(*im6o
), M_IPMOPTS
, M_WAITOK
);
2004 im6o
->im6o_multicast_ifp
= NULL
;
2005 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
2006 im6o
->im6o_multicast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
2007 LIST_INIT(&im6o
->im6o_memberships
);
2010 if (in6p
->inp_moptions
== NULL
) {
2012 * No IPv4 multicast option buffer attached to the pcb;
2013 * call ip_createmoptions to allocate one and initialize
2014 * to default values.
2016 error
= ip_createmoptions(&in6p
->inp_moptions
);
2020 imo
= in6p
->inp_moptions
;
2024 case IPV6_MULTICAST_IF
:
2026 * Select the interface for outgoing multicast packets.
2028 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
2032 bcopy(mtod(m
, u_int
*), &ifindex
, sizeof(ifindex
));
2033 if (ifindex
< 0 || if_index
< ifindex
) {
2034 error
= ENXIO
; /* XXX EINVAL? */
2037 ifp
= ifindex2ifnet
[ifindex
];
2038 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2039 error
= EADDRNOTAVAIL
;
2042 im6o
->im6o_multicast_ifp
= ifp
;
2043 imo
->imo_multicast_ifp
= ifp
;
2046 case IPV6_MULTICAST_HOPS
:
2049 * Set the IP6 hoplimit for outgoing multicast packets.
2052 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
2056 bcopy(mtod(m
, u_int
*), &optval
, sizeof(optval
));
2057 if (optval
< -1 || optval
>= 256)
2059 else if (optval
== -1) {
2060 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
2061 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2063 im6o
->im6o_multicast_hlim
= optval
;
2064 imo
->imo_multicast_ttl
= optval
;
2069 case IPV6_MULTICAST_LOOP
:
2071 * Set the loopback flag for outgoing multicast packets.
2072 * Must be zero or one.
2074 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
2078 bcopy(mtod(m
, u_int
*), &loop
, sizeof(loop
));
2083 im6o
->im6o_multicast_loop
= loop
;
2084 imo
->imo_multicast_loop
= loop
;
2087 case IPV6_JOIN_GROUP
:
2089 * Add a multicast group membership.
2090 * Group must be a valid IP6 multicast address.
2092 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2096 mreq
= mtod(m
, struct ipv6_mreq
*);
2098 * If the interface is specified, validate it.
2100 if (mreq
->ipv6mr_interface
< 0
2101 || if_index
< mreq
->ipv6mr_interface
) {
2102 error
= ENXIO
; /* XXX EINVAL? */
2106 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2108 * We use the unspecified address to specify to accept
2109 * all multicast addresses. Only super user is allowed
2112 if (suser(kauth_cred_get(), 0))
2117 } else if (IN6_IS_ADDR_V4MAPPED(&mreq
->ipv6mr_multiaddr
)) {
2118 struct ip_mreq v4req
;
2120 v4req
.imr_multiaddr
.s_addr
= mreq
->ipv6mr_multiaddr
.s6_addr32
[3];
2121 v4req
.imr_interface
.s_addr
= INADDR_ANY
;
2123 /* Find an IPv4 address on the specified interface. */
2124 if (mreq
->ipv6mr_interface
!= 0) {
2125 struct in_ifaddr
*ifa
;
2127 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2129 lck_mtx_lock(rt_mtx
);
2130 TAILQ_FOREACH(ifa
, &in_ifaddrhead
, ia_link
) {
2131 if (ifa
->ia_ifp
== ifp
) {
2132 v4req
.imr_interface
= IA_SIN(ifa
)->sin_addr
;
2136 lck_mtx_unlock(rt_mtx
);
2138 if (v4req
.imr_multiaddr
.s_addr
== 0) {
2139 /* Interface has no IPv4 address. */
2145 error
= ip_addmembership(imo
, &v4req
);
2147 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2152 * If no interface was explicitly specified, choose an
2153 * appropriate one according to the given multicast address.
2155 if (mreq
->ipv6mr_interface
== 0) {
2157 * If the multicast address is in node-local scope,
2158 * the interface should be a loopback interface.
2159 * Otherwise, look up the routing table for the
2160 * address, and choose the outgoing interface.
2161 * XXX: is it a good approach?
2163 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq
->ipv6mr_multiaddr
)) {
2167 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
2168 bzero(dst
, sizeof(*dst
));
2169 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2170 dst
->sin6_family
= AF_INET6
;
2171 dst
->sin6_addr
= mreq
->ipv6mr_multiaddr
;
2172 rtalloc((struct route
*)&ro
);
2173 if (ro
.ro_rt
== NULL
) {
2174 error
= EADDRNOTAVAIL
;
2177 ifp
= ro
.ro_rt
->rt_ifp
;
2182 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2185 * See if we found an interface, and confirm that it
2186 * supports multicast
2188 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2189 error
= EADDRNOTAVAIL
;
2193 * Put interface index into the multicast address,
2194 * if the address has link-local scope.
2196 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2197 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2198 = htons(mreq
->ipv6mr_interface
);
2201 * See if the membership already exists.
2203 lck_mtx_lock(nd6_mutex
);
2204 for (imm
= im6o
->im6o_memberships
.lh_first
;
2205 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2206 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2207 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2208 &mreq
->ipv6mr_multiaddr
))
2212 lck_mtx_unlock(nd6_mutex
);
2216 * Everything looks good; add a new record to the multicast
2217 * address list for the given interface.
2219 imm
= _MALLOC(sizeof(*imm
), M_IPMADDR
, M_WAITOK
);
2222 lck_mtx_unlock(nd6_mutex
);
2225 if ((imm
->i6mm_maddr
=
2226 in6_addmulti(&mreq
->ipv6mr_multiaddr
, ifp
, &error
, 1)) == NULL
) {
2227 FREE(imm
, M_IPMADDR
);
2228 lck_mtx_unlock(nd6_mutex
);
2231 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2232 lck_mtx_unlock(nd6_mutex
);
2235 case IPV6_LEAVE_GROUP
:
2237 * Drop a multicast group membership.
2238 * Group must be a valid IP6 multicast address.
2240 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2244 mreq
= mtod(m
, struct ipv6_mreq
*);
2246 * If an interface address was specified, get a pointer
2247 * to its ifnet structure.
2249 if (mreq
->ipv6mr_interface
< 0
2250 || if_index
< mreq
->ipv6mr_interface
) {
2251 error
= ENXIO
; /* XXX EINVAL? */
2254 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2256 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2257 if (suser(kauth_cred_get(), 0)) {
2261 } else if (IN6_IS_ADDR_V4MAPPED(&mreq
->ipv6mr_multiaddr
)) {
2262 struct ip_mreq v4req
;
2264 v4req
.imr_multiaddr
.s_addr
= mreq
->ipv6mr_multiaddr
.s6_addr32
[3];
2265 v4req
.imr_interface
.s_addr
= INADDR_ANY
;
2268 struct in_ifaddr
*ifa
;
2270 lck_mtx_lock(rt_mtx
);
2271 TAILQ_FOREACH(ifa
, &in_ifaddrhead
, ia_link
) {
2272 if (ifa
->ia_ifp
== ifp
) {
2273 v4req
.imr_interface
= IA_SIN(ifa
)->sin_addr
;
2277 lck_mtx_unlock(rt_mtx
);
2280 error
= ip_dropmembership(imo
, &v4req
);
2282 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2287 * Put interface index into the multicast address,
2288 * if the address has link-local scope.
2290 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2291 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2292 = htons(mreq
->ipv6mr_interface
);
2295 * Find the membership in the membership list.
2297 lck_mtx_lock(nd6_mutex
);
2298 for (imm
= im6o
->im6o_memberships
.lh_first
;
2299 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2301 imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2302 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2303 &mreq
->ipv6mr_multiaddr
))
2307 /* Unable to resolve interface */
2308 error
= EADDRNOTAVAIL
;
2309 lck_mtx_unlock(nd6_mutex
);
2313 * Give up the multicast address record to which the
2314 * membership points.
2316 LIST_REMOVE(imm
, i6mm_chain
);
2317 in6_delmulti(imm
->i6mm_maddr
, 1);
2318 lck_mtx_unlock(nd6_mutex
);
2319 FREE(imm
, M_IPMADDR
);
2328 * If all options have default values, no need to keep the mbuf.
2330 lck_mtx_lock(nd6_mutex
);
2331 if (im6o
->im6o_multicast_ifp
== NULL
&&
2332 im6o
->im6o_multicast_hlim
== ip6_defmcasthlim
&&
2333 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2334 im6o
->im6o_memberships
.lh_first
== NULL
) {
2335 FREE(*im6op
, M_IPMOPTS
);
2338 if (imo
->imo_multicast_ifp
== NULL
&&
2339 imo
->imo_multicast_vif
== -1 &&
2340 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2341 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2342 imo
->imo_num_memberships
== 0) {
2343 ip_freemoptions(imo
);
2344 in6p
->inp_moptions
= 0;
2346 lck_mtx_unlock(nd6_mutex
);
2352 * Return the IP6 multicast options in response to user getsockopt().
2355 ip6_getmoptions(optname
, im6o
, mp
)
2357 struct ip6_moptions
*im6o
;
2360 u_int
*hlim
, *loop
, *ifindex
;
2362 *mp
= m_get(M_WAIT
, MT_HEADER
); /*XXX*/
2366 case IPV6_MULTICAST_IF
:
2367 ifindex
= mtod(*mp
, u_int
*);
2368 (*mp
)->m_len
= sizeof(u_int
);
2369 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2372 *ifindex
= im6o
->im6o_multicast_ifp
->if_index
;
2375 case IPV6_MULTICAST_HOPS
:
2376 hlim
= mtod(*mp
, u_int
*);
2377 (*mp
)->m_len
= sizeof(u_int
);
2379 *hlim
= ip6_defmcasthlim
;
2381 *hlim
= im6o
->im6o_multicast_hlim
;
2384 case IPV6_MULTICAST_LOOP
:
2385 loop
= mtod(*mp
, u_int
*);
2386 (*mp
)->m_len
= sizeof(u_int
);
2388 *loop
= ip6_defmcasthlim
;
2390 *loop
= im6o
->im6o_multicast_loop
;
2399 * Discard the IP6 multicast options.
2402 ip6_freemoptions(im6o
)
2403 struct ip6_moptions
*im6o
;
2405 struct in6_multi_mship
*imm
;
2410 lck_mtx_lock(nd6_mutex
);
2411 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2412 LIST_REMOVE(imm
, i6mm_chain
);
2413 if (imm
->i6mm_maddr
)
2414 in6_delmulti(imm
->i6mm_maddr
, 1);
2415 FREE(imm
, M_IPMADDR
);
2417 lck_mtx_unlock(nd6_mutex
);
2418 FREE(im6o
, M_IPMOPTS
);
2422 * Set IPv6 outgoing packet options based on advanced API.
2425 ip6_setpktoptions(control
, opt
, priv
, needcopy
)
2426 struct mbuf
*control
;
2427 struct ip6_pktopts
*opt
;
2430 struct cmsghdr
*cm
= 0;
2432 if (control
== 0 || opt
== 0)
2435 init_ip6pktopts(opt
);
2438 * XXX: Currently, we assume all the optional information is stored
2441 if (control
->m_next
)
2444 for (; control
->m_len
; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2445 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2446 cm
= mtod(control
, struct cmsghdr
*);
2447 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2449 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2453 * XXX should check if RFC2292 API is mixed with 2292bis API
2455 switch (cm
->cmsg_type
) {
2457 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(struct in6_pktinfo
)))
2460 /* XXX: Is it really WAITOK? */
2461 opt
->ip6po_pktinfo
=
2462 _MALLOC(sizeof(struct in6_pktinfo
),
2463 M_IP6OPT
, M_WAITOK
);
2464 bcopy(CMSG_DATA(cm
), opt
->ip6po_pktinfo
,
2465 sizeof(struct in6_pktinfo
));
2467 opt
->ip6po_pktinfo
=
2468 (struct in6_pktinfo
*)CMSG_DATA(cm
);
2469 if (opt
->ip6po_pktinfo
->ipi6_ifindex
&&
2470 IN6_IS_ADDR_LINKLOCAL(&opt
->ip6po_pktinfo
->ipi6_addr
))
2471 opt
->ip6po_pktinfo
->ipi6_addr
.s6_addr16
[1] =
2472 htons(opt
->ip6po_pktinfo
->ipi6_ifindex
);
2474 if (opt
->ip6po_pktinfo
->ipi6_ifindex
> if_index
2475 || opt
->ip6po_pktinfo
->ipi6_ifindex
< 0) {
2480 * Check if the requested source address is indeed a
2481 * unicast address assigned to the node, and can be
2482 * used as the packet's source address.
2484 if (!IN6_IS_ADDR_UNSPECIFIED(&opt
->ip6po_pktinfo
->ipi6_addr
)) {
2485 struct in6_ifaddr
*ia6
;
2486 struct sockaddr_in6 sin6
;
2488 bzero(&sin6
, sizeof(sin6
));
2489 sin6
.sin6_len
= sizeof(sin6
);
2490 sin6
.sin6_family
= AF_INET6
;
2492 opt
->ip6po_pktinfo
->ipi6_addr
;
2493 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr(sin6tosa(&sin6
));
2495 (ia6
->ia6_flags
& (IN6_IFF_ANYCAST
|
2496 IN6_IFF_NOTREADY
)) != 0) {
2497 if (ia6
) ifafree(&ia6
->ia_ifa
);
2498 return(EADDRNOTAVAIL
);
2500 ifafree(&ia6
->ia_ifa
);
2506 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
2509 opt
->ip6po_hlim
= *(int *)CMSG_DATA(cm
);
2510 if (opt
->ip6po_hlim
< -1 || opt
->ip6po_hlim
> 255)
2518 if (cm
->cmsg_len
< sizeof(u_char
) ||
2519 /* check if cmsg_len is large enough for sa_len */
2520 cm
->cmsg_len
< CMSG_LEN(*CMSG_DATA(cm
)))
2524 opt
->ip6po_nexthop
=
2525 _MALLOC(*CMSG_DATA(cm
),
2526 M_IP6OPT
, M_WAITOK
);
2527 bcopy(CMSG_DATA(cm
),
2531 opt
->ip6po_nexthop
=
2532 (struct sockaddr
*)CMSG_DATA(cm
);
2537 struct ip6_hbh
*hbh
;
2540 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_hbh
)))
2542 hbh
= (struct ip6_hbh
*)CMSG_DATA(cm
);
2543 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
2544 if (cm
->cmsg_len
!= CMSG_LEN(hbhlen
))
2549 _MALLOC(hbhlen
, M_IP6OPT
, M_WAITOK
);
2550 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
2552 opt
->ip6po_hbh
= hbh
;
2558 struct ip6_dest
*dest
, **newdest
;
2561 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_dest
)))
2563 dest
= (struct ip6_dest
*)CMSG_DATA(cm
);
2564 destlen
= (dest
->ip6d_len
+ 1) << 3;
2565 if (cm
->cmsg_len
!= CMSG_LEN(destlen
))
2569 * The old advacned API is ambiguous on this
2570 * point. Our approach is to determine the
2571 * position based according to the existence
2572 * of a routing header. Note, however, that
2573 * this depends on the order of the extension
2574 * headers in the ancillary data; the 1st part
2575 * of the destination options header must
2576 * appear before the routing header in the
2577 * ancillary data, too.
2578 * RFC2292bis solved the ambiguity by
2579 * introducing separate cmsg types.
2581 if (opt
->ip6po_rthdr
== NULL
)
2582 newdest
= &opt
->ip6po_dest1
;
2584 newdest
= &opt
->ip6po_dest2
;
2587 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_WAITOK
);
2588 bcopy(dest
, *newdest
, destlen
);
2597 struct ip6_rthdr
*rth
;
2600 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_rthdr
)))
2602 rth
= (struct ip6_rthdr
*)CMSG_DATA(cm
);
2603 rthlen
= (rth
->ip6r_len
+ 1) << 3;
2604 if (cm
->cmsg_len
!= CMSG_LEN(rthlen
))
2607 switch (rth
->ip6r_type
) {
2608 case IPV6_RTHDR_TYPE_0
:
2609 /* must contain one addr */
2610 if (rth
->ip6r_len
== 0)
2612 /* length must be even */
2613 if (rth
->ip6r_len
% 2)
2615 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
2619 return(EINVAL
); /* not supported */
2623 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
,
2625 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
2627 opt
->ip6po_rthdr
= rth
;
2633 return(ENOPROTOOPT
);
2641 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2642 * packet to the input queue of a specified interface. Note that this
2643 * calls the output routine of the loopback "driver", but with an interface
2644 * pointer that might NOT be &loif -- easier than replicating that code here.
2650 struct sockaddr_in6
*dst
)
2653 struct ip6_hdr
*ip6
;
2655 copym
= m_copy(m
, 0, M_COPYALL
);
2660 * Make sure to deep-copy IPv6 header portion in case the data
2661 * is in an mbuf cluster, so that we can safely override the IPv6
2662 * header portion later.
2664 if ((copym
->m_flags
& M_EXT
) != 0 ||
2665 copym
->m_len
< sizeof(struct ip6_hdr
)) {
2666 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
2672 if (copym
->m_len
< sizeof(*ip6
)) {
2678 ip6
= mtod(copym
, struct ip6_hdr
*);
2679 #ifndef SCOPEDROUTING
2681 * clear embedded scope identifiers if necessary.
2682 * in6_clearscope will touch the addresses only when necessary.
2684 in6_clearscope(&ip6
->ip6_src
);
2685 in6_clearscope(&ip6
->ip6_dst
);
2690 /* Makes sure the HW checksum flags are cleaned before sending the packet */
2692 copym
->m_pkthdr
.rcvif
= 0;
2693 copym
->m_pkthdr
.csum_data
= 0;
2694 copym
->m_pkthdr
.csum_flags
= 0;
2697 copym
->m_pkthdr
.rcvif
= ifp
;
2698 lck_mtx_unlock(ip6_mutex
);
2699 dlil_output(lo_ifp
, PF_INET6
, copym
, 0, (struct sockaddr
*)dst
, 0);
2700 lck_mtx_lock(ip6_mutex
);
2704 (void)if_simloop(ifp
, copym
, dst
->sin6_family
, NULL
);
2709 * Chop IPv6 header off from the payload.
2712 ip6_splithdr(m
, exthdrs
)
2714 struct ip6_exthdrs
*exthdrs
;
2717 struct ip6_hdr
*ip6
;
2719 ip6
= mtod(m
, struct ip6_hdr
*);
2720 if (m
->m_len
> sizeof(*ip6
)) {
2721 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
2726 M_COPY_PKTHDR(mh
, m
);
2727 MH_ALIGN(mh
, sizeof(*ip6
));
2728 m
->m_flags
&= ~M_PKTHDR
;
2729 m
->m_len
-= sizeof(*ip6
);
2730 m
->m_data
+= sizeof(*ip6
);
2733 m
->m_len
= sizeof(*ip6
);
2734 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
2736 exthdrs
->ip6e_ip6
= m
;
2741 * Compute IPv6 extension header length.
2745 struct in6pcb
*in6p
;
2749 if (!in6p
->in6p_outputopts
)
2754 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2756 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
2757 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
2758 /* dest1 is valid with rthdr only */
2759 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
2760 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
2761 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);