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
69 #include <sys/param.h>
70 #include <sys/malloc.h>
72 #include <sys/errno.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
79 #include <sys/kauth.h>
82 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip_var.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet6/nd6.h>
95 #include <netinet6/ipsec.h>
97 #include <netinet6/ipsec6.h>
99 #include <netkey/key.h>
100 extern int ipsec_bypass
;
101 extern lck_mtx_t
*sadb_mutex
;
102 extern lck_mtx_t
*nd6_mutex
;
105 #include <netinet6/ip6_fw.h>
107 #include <net/net_osdep.h>
109 #include <netinet/kpi_ipfilter_var.h>
112 static MALLOC_DEFINE(M_IPMOPTS
, "ip6_moptions", "internet multicast options");
116 extern u_long route_generation
;
119 struct mbuf
*ip6e_ip6
;
120 struct mbuf
*ip6e_hbh
;
121 struct mbuf
*ip6e_dest1
;
122 struct mbuf
*ip6e_rthdr
;
123 struct mbuf
*ip6e_dest2
;
126 static int ip6_pcbopts(struct ip6_pktopts
**, struct mbuf
*,
127 struct socket
*, struct sockopt
*sopt
);
128 static int ip6_setmoptions(int, struct inpcb
*, struct mbuf
*);
129 static int ip6_getmoptions(int, struct ip6_moptions
*, struct mbuf
**);
130 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
131 static int ip6_insertfraghdr(struct mbuf
*, struct mbuf
*, int,
133 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
134 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
136 extern int ip_createmoptions(struct ip_moptions
**imop
);
137 extern int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
138 extern int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
139 extern lck_mtx_t
*ip6_mutex
;
142 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
143 * header (with pri, len, nxt, hlim, src, dst).
144 * This function may modify ver and hlim only.
145 * The mbuf chain containing the packet will be freed.
146 * The mbuf opt, if present, will not be freed.
148 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
149 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
150 * which is rt_rmx.rmx_mtu.
155 struct ip6_pktopts
*opt
,
156 struct route_in6
*ro
,
158 struct ip6_moptions
*im6o
,
159 struct ifnet
**ifpp
, /* XXX: just for statistics */
162 struct ip6_hdr
*ip6
, *mhip6
;
163 struct ifnet
*ifp
, *origifp
;
165 int hlen
, tlen
, len
, off
;
166 struct route_in6 ip6route
;
167 struct sockaddr_in6
*dst
;
169 struct in6_ifaddr
*ia
= NULL
;
171 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
172 struct ip6_exthdrs exthdrs
;
173 struct in6_addr finaldst
;
174 struct route_in6
*ro_pmtu
= NULL
;
177 ipfilter_t inject_filter_ref
;
180 int needipsectun
= 0;
181 struct socket
*so
= NULL
;
182 struct secpolicy
*sp
= NULL
;
185 lck_mtx_lock(ip6_mutex
);
186 /* for AH processing. stupid to have "socket" variable in IP layer... */
187 if (ipsec_bypass
== 0)
189 so
= ipsec_getsocket(m
);
190 (void)ipsec_setsocket(m
, NULL
);
194 ip6
= mtod(m
, struct ip6_hdr
*);
195 inject_filter_ref
= ipf_get_inject_filter(m
);
197 #define MAKE_EXTHDR(hp, mp) \
200 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
201 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
202 ((eh)->ip6e_len + 1) << 3); \
208 bzero(&exthdrs
, sizeof(exthdrs
));
211 /* Hop-by-Hop options header */
212 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
213 /* Destination options header(1st part) */
214 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
216 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
217 /* Destination options header(2nd part) */
218 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
222 if (ipsec_bypass
!= 0)
225 lck_mtx_lock(sadb_mutex
);
226 /* get a security policy for this packet */
228 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
230 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
233 ipsec6stat
.out_inval
++;
234 lck_mtx_unlock(sadb_mutex
);
241 switch (sp
->policy
) {
242 case IPSEC_POLICY_DISCARD
:
244 * This packet is just discarded.
246 ipsec6stat
.out_polvio
++;
247 lck_mtx_unlock(sadb_mutex
);
250 case IPSEC_POLICY_BYPASS
:
251 case IPSEC_POLICY_NONE
:
252 /* no need to do IPsec. */
256 case IPSEC_POLICY_IPSEC
:
257 if (sp
->req
== NULL
) {
258 /* acquire a policy */
259 error
= key_spdacquire(sp
);
260 lck_mtx_unlock(sadb_mutex
);
266 case IPSEC_POLICY_ENTRUST
:
268 printf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
270 lck_mtx_unlock(sadb_mutex
);
275 * Calculate the total length of the extension header chain.
276 * Keep the length of the unfragmentable part for fragmentation.
279 if (exthdrs
.ip6e_hbh
) optlen
+= exthdrs
.ip6e_hbh
->m_len
;
280 if (exthdrs
.ip6e_dest1
) optlen
+= exthdrs
.ip6e_dest1
->m_len
;
281 if (exthdrs
.ip6e_rthdr
) optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
282 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
283 /* NOTE: we don't add AH/ESP length here. do that later. */
284 if (exthdrs
.ip6e_dest2
) optlen
+= exthdrs
.ip6e_dest2
->m_len
;
287 * If we need IPsec, or there is at least one extension header,
288 * separate IP6 header from the payload.
290 if ((needipsec
|| optlen
) && !hdrsplit
) {
291 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
295 m
= exthdrs
.ip6e_ip6
;
300 ip6
= mtod(m
, struct ip6_hdr
*);
302 /* adjust mbuf packet header length */
303 m
->m_pkthdr
.len
+= optlen
;
304 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
306 /* If this is a jumbo payload, insert a jumbo payload option. */
307 if (plen
> IPV6_MAXPACKET
) {
309 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
313 m
= exthdrs
.ip6e_ip6
;
317 ip6
= mtod(m
, struct ip6_hdr
*);
318 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
322 ip6
->ip6_plen
= htons(plen
);
325 * Concatenate headers and fill in next header fields.
326 * Here we have, on "m"
328 * and we insert headers accordingly. Finally, we should be getting:
329 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
331 * during the header composing process, "m" points to IPv6 header.
332 * "mprev" points to an extension header prior to esp.
335 u_char
*nexthdrp
= &ip6
->ip6_nxt
;
336 struct mbuf
*mprev
= m
;
339 * we treat dest2 specially. this makes IPsec processing
340 * much easier. the goal here is to make mprev point the
341 * mbuf prior to dest2.
343 * result: IPv6 dest2 payload
344 * m and mprev will point to IPv6 header.
346 if (exthdrs
.ip6e_dest2
) {
348 panic("assumption failed: hdr not split");
349 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
350 m
->m_next
= exthdrs
.ip6e_dest2
;
351 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
352 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
355 #define MAKE_CHAIN(m, mp, p, i)\
359 panic("assumption failed: hdr not split"); \
360 *mtod((m), u_char *) = *(p);\
362 p = mtod((m), u_char *);\
363 (m)->m_next = (mp)->m_next;\
369 * result: IPv6 hbh dest1 rthdr dest2 payload
370 * m will point to IPv6 header. mprev will point to the
371 * extension header prior to dest2 (rthdr in the above case).
373 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
,
374 nexthdrp
, IPPROTO_HOPOPTS
);
375 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
,
376 nexthdrp
, IPPROTO_DSTOPTS
);
377 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
,
378 nexthdrp
, IPPROTO_ROUTING
);
380 if (!TAILQ_EMPTY(&ipv6_filters
)) {
381 struct ipfilter
*filter
;
382 int seen
= (inject_filter_ref
== 0);
384 struct ipf_pktopts
*ippo
= 0, ipf_pktopts
;
386 if (im6o
!= NULL
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
388 ippo
->ippo_flags
= IPPOF_MCAST_OPTS
;
389 ippo
->ippo_mcast_ifnet
= im6o
->im6o_multicast_ifp
;
390 ippo
->ippo_mcast_ttl
= im6o
->im6o_multicast_hlim
;
391 ippo
->ippo_mcast_loop
= im6o
->im6o_multicast_loop
;
394 /* Hack: embed the scope_id in the destination */
395 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
) &&
396 (ip6
->ip6_dst
.s6_addr16
[1] == 0) && (ro
!= NULL
)) {
398 ip6
->ip6_dst
.s6_addr16
[1] = htons(ro
->ro_dst
.sin6_scope_id
);
401 lck_mtx_unlock(ip6_mutex
);
403 TAILQ_FOREACH(filter
, &ipv6_filters
, ipf_link
) {
405 * No need to proccess packet twice if we've
409 if ((struct ipfilter
*)inject_filter_ref
== filter
)
411 } else if (filter
->ipf_filter
.ipf_output
) {
414 result
= filter
->ipf_filter
.ipf_output(filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, ippo
);
415 if (result
== EJUSTRETURN
) {
417 locked
= 1; /* Don't want to take lock to unlock it right away */
422 locked
= 1; /* Don't want to take lock to unlock it right away */
428 lck_mtx_lock(ip6_mutex
);
430 /* Hack: cleanup embedded scope_id if we put it there */
432 ip6
->ip6_dst
.s6_addr16
[1] = 0;
440 * pointers after IPsec headers are not valid any more.
441 * other pointers need a great care too.
442 * (IPsec routines should not mangle mbufs prior to AH/ESP)
444 exthdrs
.ip6e_dest2
= NULL
;
447 struct ip6_rthdr
*rh
= NULL
;
449 struct ipsec_output_state state
;
451 if (exthdrs
.ip6e_rthdr
) {
452 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
453 segleft_org
= rh
->ip6r_segleft
;
454 rh
->ip6r_segleft
= 0;
457 bzero(&state
, sizeof(state
));
459 error
= ipsec6_output_trans(&state
, nexthdrp
, mprev
, sp
, flags
,
463 /* mbuf is already reclaimed in ipsec6_output_trans. */
473 printf("ip6_output (ipsec): error code %d\n", error
);
476 /* don't show these error codes to the user */
482 if (exthdrs
.ip6e_rthdr
) {
483 /* ah6_output doesn't modify mbuf chain */
484 rh
->ip6r_segleft
= segleft_org
;
492 * If there is a routing header, replace destination address field
493 * with the first hop of the routing header.
495 if (exthdrs
.ip6e_rthdr
) {
496 struct ip6_rthdr
*rh
=
497 (struct ip6_rthdr
*)(mtod(exthdrs
.ip6e_rthdr
,
498 struct ip6_rthdr
*));
499 struct ip6_rthdr0
*rh0
;
501 finaldst
= ip6
->ip6_dst
;
502 switch (rh
->ip6r_type
) {
503 case IPV6_RTHDR_TYPE_0
:
504 rh0
= (struct ip6_rthdr0
*)rh
;
505 ip6
->ip6_dst
= rh0
->ip6r0_addr
[0];
506 bcopy((caddr_t
)&rh0
->ip6r0_addr
[1],
507 (caddr_t
)&rh0
->ip6r0_addr
[0],
508 sizeof(struct in6_addr
)*(rh0
->ip6r0_segleft
- 1)
510 rh0
->ip6r0_addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
512 default: /* is it possible? */
518 /* Source address validation */
519 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
520 (flags
& IPV6_DADOUTPUT
) == 0) {
522 ip6stat
.ip6s_badscope
++;
525 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
527 ip6stat
.ip6s_badscope
++;
531 ip6stat
.ip6s_localout
++;
538 bzero((caddr_t
)ro
, sizeof(*ro
));
541 if (opt
&& opt
->ip6po_rthdr
)
542 ro
= &opt
->ip6po_route
;
543 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
545 * If there is a cached route,
546 * check that it is to the same destination
547 * and is still up. If not, free it and try again.
549 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
550 dst
->sin6_family
!= AF_INET6
||
551 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
) ||
552 ro
->ro_rt
->generation_id
!= route_generation
)) {
554 ro
->ro_rt
= (struct rtentry
*)0;
556 if (ro
->ro_rt
== 0) {
557 bzero(dst
, sizeof(*dst
));
558 dst
->sin6_family
= AF_INET6
;
559 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
560 dst
->sin6_addr
= ip6
->ip6_dst
;
562 /* XXX: sin6_scope_id should already be fixed at this point */
563 if (IN6_IS_SCOPE_LINKLOCAL(&dst
->sin6_addr
))
564 dst
->sin6_scope_id
= ntohs(dst
->sin6_addr
.s6_addr16
[1]);
568 if (needipsec
&& needipsectun
) {
569 struct ipsec_output_state state
;
572 * All the extension headers will become inaccessible
573 * (since they can be encrypted).
574 * Don't panic, we need no more updates to extension headers
575 * on inner IPv6 packet (since they are now encapsulated).
577 * IPv6 [ESP|AH] IPv6 [extension headers] payload
579 bzero(&exthdrs
, sizeof(exthdrs
));
580 exthdrs
.ip6e_ip6
= m
;
582 bzero(&state
, sizeof(state
));
584 state
.ro
= (struct route
*)ro
;
585 state
.dst
= (struct sockaddr
*)dst
;
587 lck_mtx_lock(sadb_mutex
);
588 error
= ipsec6_output_tunnel(&state
, sp
, flags
);
589 lck_mtx_unlock(sadb_mutex
);
591 ro
= (struct route_in6
*)state
.ro
;
592 dst
= (struct sockaddr_in6
*)state
.dst
;
594 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
605 printf("ip6_output (ipsec): error code %d\n", error
);
608 /* don't show these error codes to the user */
612 lck_mtx_unlock(sadb_mutex
);
616 exthdrs
.ip6e_ip6
= m
;
620 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
623 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
624 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
626 * interface selection comes here
627 * if an interface is specified from an upper layer,
630 lck_mtx_lock(rt_mtx
);
631 if (ro
->ro_rt
== 0) {
633 * non-bsdi always clone routes, if parent is
636 rtalloc_ign_locked((struct route
*)ro
, 0UL);
638 if (ro
->ro_rt
== 0) {
639 ip6stat
.ip6s_noroute
++;
640 error
= EHOSTUNREACH
;
641 lck_mtx_unlock(rt_mtx
);
642 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
645 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
646 ifp
= ro
->ro_rt
->rt_ifp
;
648 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
649 dst
= (struct sockaddr_in6
*)ro
->ro_rt
->rt_gateway
;
650 lck_mtx_unlock(rt_mtx
);
651 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
653 in6_ifstat_inc(ifp
, ifs6_out_request
);
656 * Check if the outgoing interface conflicts with
657 * the interface specified by ifi6_ifindex (if specified).
658 * Note that loopback interface is always okay.
659 * (this may happen when we are sending a packet to one of
660 * our own addresses.)
662 if (opt
&& opt
->ip6po_pktinfo
663 && opt
->ip6po_pktinfo
->ipi6_ifindex
) {
664 if (!(ifp
->if_flags
& IFF_LOOPBACK
)
665 && ifp
->if_index
!= opt
->ip6po_pktinfo
->ipi6_ifindex
) {
666 ip6stat
.ip6s_noroute
++;
667 in6_ifstat_inc(ifp
, ifs6_out_discard
);
668 error
= EHOSTUNREACH
;
673 if (opt
&& opt
->ip6po_hlim
!= -1)
674 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
677 struct in6_multi
*in6m
;
679 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
682 * See if the caller provided any multicast options
686 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
687 if (im6o
->im6o_multicast_ifp
!= NULL
)
688 ifp
= im6o
->im6o_multicast_ifp
;
690 ip6
->ip6_hlim
= ip6_defmcasthlim
;
693 * See if the caller provided the outgoing interface
694 * as an ancillary data.
695 * Boundary check for ifindex is assumed to be already done.
697 if (opt
&& opt
->ip6po_pktinfo
&& opt
->ip6po_pktinfo
->ipi6_ifindex
)
698 ifp
= ifindex2ifnet
[opt
->ip6po_pktinfo
->ipi6_ifindex
];
701 * If the destination is a node-local scope multicast,
702 * the packet should be loop-backed only.
704 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
)) {
706 * If the outgoing interface is already specified,
707 * it should be a loopback interface.
709 if (ifp
&& (ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
710 ip6stat
.ip6s_badscope
++;
711 error
= ENETUNREACH
; /* XXX: better error? */
712 /* XXX correct ifp? */
713 in6_ifstat_inc(ifp
, ifs6_out_discard
);
720 if (opt
&& opt
->ip6po_hlim
!= -1)
721 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
724 * If caller did not provide an interface lookup a
725 * default in the routing table. This is either a
726 * default for the speicfied group (i.e. a host
727 * route), or a multicast default (a route for the
731 lck_mtx_lock(rt_mtx
);
732 if (ro
->ro_rt
== 0) {
733 ro
->ro_rt
= rtalloc1_locked((struct sockaddr
*)
734 &ro
->ro_dst
, 0, 0UL);
736 if (ro
->ro_rt
== 0) {
737 ip6stat
.ip6s_noroute
++;
738 lck_mtx_unlock(rt_mtx
);
739 error
= EHOSTUNREACH
;
740 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
743 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
744 ifp
= ro
->ro_rt
->rt_ifp
;
746 lck_mtx_unlock(rt_mtx
);
749 if ((flags
& IPV6_FORWARDING
) == 0)
750 in6_ifstat_inc(ifp
, ifs6_out_request
);
751 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
754 * Confirm that the outgoing interface supports multicast.
756 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
757 ip6stat
.ip6s_noroute
++;
758 in6_ifstat_inc(ifp
, ifs6_out_discard
);
762 ifnet_lock_shared(ifp
);
763 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
764 ifnet_lock_done(ifp
);
766 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
768 * If we belong to the destination multicast group
769 * on the outgoing interface, and the caller did not
770 * forbid loopback, loop back a copy.
772 ip6_mloopback(ifp
, m
, dst
);
775 * If we are acting as a multicast router, perform
776 * multicast forwarding as if the packet had just
777 * arrived on the interface to which we are about
778 * to send. The multicast forwarding function
779 * recursively calls this function, using the
780 * IPV6_FORWARDING flag to prevent infinite recursion.
782 * Multicasts that are looped back by ip6_mloopback(),
783 * above, will be forwarded by the ip6_input() routine,
786 if (ip6_mrouter
&& (flags
& IPV6_FORWARDING
) == 0) {
787 if (ip6_mforward(ip6
, ifp
, m
) != NULL
) {
794 * Multicasts with a hoplimit of zero may be looped back,
795 * above, but must not be transmitted on a network.
796 * Also, multicasts addressed to the loopback interface
797 * are not sent -- the above call to ip6_mloopback() will
798 * loop back a copy if this host actually belongs to the
799 * destination group on the loopback interface.
801 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
)) {
808 * Fill the outgoing inteface to tell the upper layer
809 * to increment per-interface statistics.
815 * Determine path MTU.
818 /* The first hop and the final destination may differ. */
819 struct sockaddr_in6
*sin6_fin
=
820 (struct sockaddr_in6
*)&ro_pmtu
->ro_dst
;
821 if (ro_pmtu
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
822 !IN6_ARE_ADDR_EQUAL(&sin6_fin
->sin6_addr
,
824 rtfree(ro_pmtu
->ro_rt
);
825 ro_pmtu
->ro_rt
= (struct rtentry
*)0;
827 if (ro_pmtu
->ro_rt
== 0) {
828 bzero(sin6_fin
, sizeof(*sin6_fin
));
829 sin6_fin
->sin6_family
= AF_INET6
;
830 sin6_fin
->sin6_len
= sizeof(struct sockaddr_in6
);
831 sin6_fin
->sin6_addr
= finaldst
;
833 rtalloc((struct route
*)ro_pmtu
);
836 if (ro_pmtu
->ro_rt
!= NULL
) {
837 u_int32_t ifmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
839 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
840 if (mtu
> ifmtu
|| mtu
== 0) {
842 * The MTU on the route is larger than the MTU on
843 * the interface! This shouldn't happen, unless the
844 * MTU of the interface has been changed after the
845 * interface was brought up. Change the MTU in the
846 * route to match the interface MTU (as long as the
847 * field isn't locked).
849 * if MTU on the route is 0, we need to fix the MTU.
850 * this case happens with path MTU discovery timeouts.
853 if ((ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0)
854 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
857 mtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
861 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
863 if ((flags
& IPV6_MINMTU
) != 0 && mtu
> IPV6_MMTU
)
866 /* Fake scoped addresses */
867 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
869 * If source or destination address is a scoped address, and
870 * the packet is going to be sent to a loopback interface,
871 * we should keep the original interface.
875 * XXX: this is a very experimental and temporary solution.
876 * We eventually have sockaddr_in6 and use the sin6_scope_id
877 * field of the structure here.
878 * We rely on the consistency between two scope zone ids
879 * of source and destination, which should already be assured.
880 * Larger scopes than link will be supported in the future.
883 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
884 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_src
.s6_addr16
[1])];
885 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
886 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_dst
.s6_addr16
[1])];
888 * XXX: origifp can be NULL even in those two cases above.
889 * For example, if we remove the (only) link-local address
890 * from the loopback interface, and try to send a link-local
891 * address without link-id information. Then the source
892 * address is ::1, and the destination address is the
893 * link-local address with its s6_addr16[1] being zero.
894 * What is worse, if the packet goes to the loopback interface
895 * by a default rejected route, the null pointer would be
896 * passed to looutput, and the kernel would hang.
897 * The following last resort would prevent such disaster.
904 #ifndef SCOPEDROUTING
906 * clear embedded scope identifiers if necessary.
907 * in6_clearscope will touch the addresses only when necessary.
909 in6_clearscope(&ip6
->ip6_src
);
910 in6_clearscope(&ip6
->ip6_dst
);
914 * Check with the firewall...
916 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
918 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
919 /* If ipfw says divert, we have to just drop packet */
920 if (ip6_fw_chk_ptr(&ip6
, ifp
, &port
, &m
)) {
931 * If the outgoing packet contains a hop-by-hop options header,
932 * it must be examined and processed even by the source node.
933 * (RFC 2460, section 4.)
935 if (exthdrs
.ip6e_hbh
) {
936 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
937 u_int32_t dummy1
; /* XXX unused */
938 u_int32_t dummy2
; /* XXX unused */
941 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
942 panic("ip6e_hbh is not continuous");
945 * XXX: if we have to send an ICMPv6 error to the sender,
946 * we need the M_LOOP flag since icmp6_error() expects
947 * the IPv6 and the hop-by-hop options header are
948 * continuous unless the flag is set.
950 m
->m_flags
|= M_LOOP
;
951 m
->m_pkthdr
.rcvif
= ifp
;
952 if (ip6_process_hopopts(m
,
953 (u_int8_t
*)(hbh
+ 1),
954 ((hbh
->ip6h_len
+ 1) << 3) -
955 sizeof(struct ip6_hbh
),
956 &dummy1
, &dummy2
) < 0) {
957 /* m was already freed at this point */
958 error
= EINVAL
;/* better error? */
961 m
->m_flags
&= ~M_LOOP
; /* XXX */
962 m
->m_pkthdr
.rcvif
= NULL
;
966 * Send the packet to the outgoing interface.
967 * If necessary, do IPv6 fragmentation before sending.
969 tlen
= m
->m_pkthdr
.len
;
973 * On any link that cannot convey a 1280-octet packet in one piece,
974 * link-specific fragmentation and reassembly must be provided at
975 * a layer below IPv6. [RFC 2460, sec.5]
976 * Thus if the interface has ability of link-level fragmentation,
977 * we can just send the packet even if the packet size is
978 * larger than the link's MTU.
979 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
982 || ifp
->if_flags
& IFF_FRAGMENTABLE
986 /* Record statistics for this interface address. */
987 if (ia
&& !(flags
& IPV6_FORWARDING
)) {
989 ia
->ia_ifa
.if_opackets
++;
990 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
994 /* clean ipsec history once it goes out of the node */
998 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
, 1);
1000 } else if (mtu
< IPV6_MMTU
) {
1002 * note that path MTU is never less than IPV6_MMTU
1003 * (see icmp6_input).
1006 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1008 } else if (ip6
->ip6_plen
== 0) { /* jumbo payload cannot be fragmented */
1010 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1013 struct mbuf
**mnext
, *m_frgpart
;
1014 struct ip6_frag
*ip6f
;
1015 u_int32_t id
= htonl(ip6_id
++);
1019 * Too large for the destination or interface;
1020 * fragment if possible.
1021 * Must be able to put at least 8 bytes per fragment.
1023 hlen
= unfragpartlen
;
1024 if (mtu
> IPV6_MAXPACKET
)
1025 mtu
= IPV6_MAXPACKET
;
1027 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
1030 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
1034 mnext
= &m
->m_nextpkt
;
1037 * Change the next header field of the last header in the
1038 * unfragmentable part.
1040 if (exthdrs
.ip6e_rthdr
) {
1041 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1042 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1043 } else if (exthdrs
.ip6e_dest1
) {
1044 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1045 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1046 } else if (exthdrs
.ip6e_hbh
) {
1047 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1048 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1050 nextproto
= ip6
->ip6_nxt
;
1051 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1055 * Loop through length of segment after first fragment,
1056 * make new header and copy data of each part and link onto
1060 for (off
= hlen
; off
< tlen
; off
+= len
) {
1061 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1064 ip6stat
.ip6s_odropped
++;
1067 m
->m_pkthdr
.rcvif
= NULL
;
1068 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1070 mnext
= &m
->m_nextpkt
;
1071 m
->m_data
+= max_linkhdr
;
1072 mhip6
= mtod(m
, struct ip6_hdr
*);
1074 m
->m_len
= sizeof(*mhip6
);
1075 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1077 ip6stat
.ip6s_odropped
++;
1080 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1081 if (off
+ len
>= tlen
)
1084 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1085 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1087 sizeof(struct ip6_hdr
)));
1088 if ((m_frgpart
= m_copy(m0
, off
, len
)) == 0) {
1090 ip6stat
.ip6s_odropped
++;
1093 m_cat(m
, m_frgpart
);
1094 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1095 m
->m_pkthdr
.rcvif
= 0;
1096 m
->m_pkthdr
.socket_id
= m0
->m_pkthdr
.socket_id
;
1097 ip6f
->ip6f_reserved
= 0;
1098 ip6f
->ip6f_ident
= id
;
1099 ip6f
->ip6f_nxt
= nextproto
;
1100 ip6stat
.ip6s_ofragments
++;
1101 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1104 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1108 * Remove leading garbages.
1114 for (m0
= m
; m
; m
= m0
) {
1118 /* Record statistics for this interface address. */
1121 ia
->ia_ifa
.if_opackets
++;
1122 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1126 /* clean ipsec history once it goes out of the node */
1129 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
, 1);
1136 ip6stat
.ip6s_fragmented
++;
1140 lck_mtx_unlock(ip6_mutex
);
1141 if (ro
== &ip6route
&& ro
->ro_rt
) { /* brace necessary for rtfree */
1143 } else if (ro_pmtu
== &ip6route
&& ro_pmtu
->ro_rt
) {
1144 rtfree(ro_pmtu
->ro_rt
);
1149 lck_mtx_lock(sadb_mutex
);
1151 lck_mtx_unlock(sadb_mutex
);
1158 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1159 m_freem(exthdrs
.ip6e_dest1
);
1160 m_freem(exthdrs
.ip6e_rthdr
);
1161 m_freem(exthdrs
.ip6e_dest2
);
1169 ip6_copyexthdr(mp
, hdr
, hlen
)
1176 if (hlen
> MCLBYTES
)
1177 return(ENOBUFS
); /* XXX */
1179 MGET(m
, M_DONTWAIT
, MT_DATA
);
1184 MCLGET(m
, M_DONTWAIT
);
1185 if ((m
->m_flags
& M_EXT
) == 0) {
1192 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1199 * Insert jumbo payload option.
1202 ip6_insert_jumboopt(exthdrs
, plen
)
1203 struct ip6_exthdrs
*exthdrs
;
1210 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1213 * If there is no hop-by-hop options header, allocate new one.
1214 * If there is one but it doesn't have enough space to store the
1215 * jumbo payload option, allocate a cluster to store the whole options.
1216 * Otherwise, use it to store the options.
1218 if (exthdrs
->ip6e_hbh
== 0) {
1219 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1222 mopt
->m_len
= JUMBOOPTLEN
;
1223 optbuf
= mtod(mopt
, u_char
*);
1224 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1225 exthdrs
->ip6e_hbh
= mopt
;
1227 struct ip6_hbh
*hbh
;
1229 mopt
= exthdrs
->ip6e_hbh
;
1230 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1233 * - exthdrs->ip6e_hbh is not referenced from places
1234 * other than exthdrs.
1235 * - exthdrs->ip6e_hbh is not an mbuf chain.
1237 int oldoptlen
= mopt
->m_len
;
1241 * XXX: give up if the whole (new) hbh header does
1242 * not fit even in an mbuf cluster.
1244 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1248 * As a consequence, we must always prepare a cluster
1251 MGET(n
, M_DONTWAIT
, MT_DATA
);
1253 MCLGET(n
, M_DONTWAIT
);
1254 if ((n
->m_flags
& M_EXT
) == 0) {
1261 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1262 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1264 optbuf
= mtod(n
, caddr_t
) + oldoptlen
;
1266 mopt
= exthdrs
->ip6e_hbh
= n
;
1268 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1269 mopt
->m_len
+= JUMBOOPTLEN
;
1271 optbuf
[0] = IP6OPT_PADN
;
1275 * Adjust the header length according to the pad and
1276 * the jumbo payload option.
1278 hbh
= mtod(mopt
, struct ip6_hbh
*);
1279 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1282 /* fill in the option. */
1283 optbuf
[2] = IP6OPT_JUMBO
;
1285 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1286 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1288 /* finally, adjust the packet header length */
1289 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1296 * Insert fragment header and copy unfragmentable header portions.
1299 ip6_insertfraghdr(m0
, m
, hlen
, frghdrp
)
1300 struct mbuf
*m0
, *m
;
1302 struct ip6_frag
**frghdrp
;
1304 struct mbuf
*n
, *mlast
;
1306 if (hlen
> sizeof(struct ip6_hdr
)) {
1307 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1308 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
1315 /* Search for the last mbuf of unfragmentable part. */
1316 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1319 if ((mlast
->m_flags
& M_EXT
) == 0 &&
1320 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1321 /* use the trailing space of the last mbuf for the fragment hdr */
1323 (struct ip6_frag
*)(mtod(mlast
, caddr_t
) + mlast
->m_len
);
1324 mlast
->m_len
+= sizeof(struct ip6_frag
);
1325 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1327 /* allocate a new mbuf for the fragment header */
1330 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1333 mfrg
->m_len
= sizeof(struct ip6_frag
);
1334 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1335 mlast
->m_next
= mfrg
;
1341 extern int load_ipfw();
1344 * IP6 socket option processing.
1347 ip6_ctloutput(so
, sopt
)
1349 struct sockopt
*sopt
;
1352 struct inpcb
*in6p
= sotoinpcb(so
);
1354 int level
, op
, optname
;
1358 level
= error
= optval
= 0;
1360 panic("ip6_ctloutput: arg soopt is NULL");
1362 level
= sopt
->sopt_level
;
1363 op
= sopt
->sopt_dir
;
1364 optname
= sopt
->sopt_name
;
1365 optlen
= sopt
->sopt_valsize
;
1369 privileged
= (p
== 0 || proc_suser(p
)) ? 0 : 1;
1371 if (level
== IPPROTO_IPV6
) {
1376 case IPV6_PKTOPTIONS
:
1380 if (sopt
->sopt_valsize
> MCLBYTES
) {
1384 error
= soopt_getm(sopt
, &m
); /* XXX */
1387 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1390 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1392 m_freem(m
); /* XXX */
1397 * Use of some Hop-by-Hop options or some
1398 * Destination options, might require special
1399 * privilege. That is, normal applications
1400 * (without special privilege) might be forbidden
1401 * from setting certain options in outgoing packets,
1402 * and might never see certain options in received
1403 * packets. [RFC 2292 Section 6]
1404 * KAME specific note:
1405 * KAME prevents non-privileged users from sending or
1406 * receiving ANY hbh/dst options in order to avoid
1407 * overhead of parsing options in the kernel.
1409 case IPV6_UNICAST_HOPS
:
1414 if (optlen
!= sizeof(int)) {
1418 error
= sooptcopyin(sopt
, &optval
,
1419 sizeof optval
, sizeof optval
);
1424 case IPV6_UNICAST_HOPS
:
1425 if (optval
< -1 || optval
>= 256)
1428 /* -1 = kernel default */
1429 in6p
->in6p_hops
= optval
;
1431 if ((in6p
->in6p_vflag
&
1433 in6p
->inp_ip_ttl
= optval
;
1436 #define OPTSET(bit) \
1439 in6p->in6p_flags |= (bit); \
1441 in6p->in6p_flags &= ~(bit); \
1443 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1446 in6p
->in6p_cksum
= optval
;
1455 * make setsockopt(IPV6_V6ONLY)
1456 * available only prior to bind(2).
1457 * see ipng mailing list, Jun 22 2001.
1459 if (in6p
->in6p_lport
||
1460 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
))
1465 OPTSET(IN6P_IPV6_V6ONLY
);
1467 in6p
->in6p_vflag
&= ~INP_IPV4
;
1469 in6p
->in6p_vflag
|= INP_IPV4
;
1480 if (optlen
!= sizeof(int)) {
1484 error
= sooptcopyin(sopt
, &optval
,
1485 sizeof optval
, sizeof optval
);
1490 OPTSET(IN6P_PKTINFO
);
1493 OPTSET(IN6P_HOPLIMIT
);
1497 * Check super-user privilege.
1498 * See comments for IPV6_RECVHOPOPTS.
1502 OPTSET(IN6P_HOPOPTS
);
1507 OPTSET(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1516 case IPV6_MULTICAST_IF
:
1517 case IPV6_MULTICAST_HOPS
:
1518 case IPV6_MULTICAST_LOOP
:
1519 case IPV6_JOIN_GROUP
:
1520 case IPV6_LEAVE_GROUP
:
1523 if (sopt
->sopt_valsize
> MLEN
) {
1528 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1533 m
->m_len
= sopt
->sopt_valsize
;
1534 error
= sooptcopyin(sopt
, mtod(m
, char *),
1535 m
->m_len
, m
->m_len
);
1536 error
= ip6_setmoptions(sopt
->sopt_name
, in6p
, m
);
1541 case IPV6_PORTRANGE
:
1542 error
= sooptcopyin(sopt
, &optval
,
1543 sizeof optval
, sizeof optval
);
1548 case IPV6_PORTRANGE_DEFAULT
:
1549 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1550 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1553 case IPV6_PORTRANGE_HIGH
:
1554 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1555 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1558 case IPV6_PORTRANGE_LOW
:
1559 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1560 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1570 case IPV6_IPSEC_POLICY
:
1576 if (sopt
->sopt_valsize
> MCLBYTES
) {
1580 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1582 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1585 req
= mtod(m
, caddr_t
);
1588 lck_mtx_lock(sadb_mutex
);
1589 error
= ipsec6_set_policy(in6p
, optname
, req
,
1591 lck_mtx_unlock(sadb_mutex
);
1595 #endif /* KAME IPSEC */
1602 if (ip6_fw_ctl_ptr
== NULL
&& load_ipfw() != 0)
1605 error
= (*ip6_fw_ctl_ptr
)(sopt
);
1610 error
= ENOPROTOOPT
;
1618 case IPV6_PKTOPTIONS
:
1619 if (in6p
->in6p_options
) {
1621 m
= m_copym(in6p
->in6p_options
,
1622 0, M_COPYALL
, M_WAIT
);
1623 error
= soopt_mcopyout(sopt
, m
);
1627 sopt
->sopt_valsize
= 0;
1630 case IPV6_UNICAST_HOPS
:
1635 case IPV6_PORTRANGE
:
1638 case IPV6_UNICAST_HOPS
:
1639 optval
= in6p
->in6p_hops
;
1643 optval
= in6p
->in6p_cksum
;
1647 optval
= OPTBIT(IN6P_FAITH
);
1651 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1654 case IPV6_PORTRANGE
:
1657 flags
= in6p
->in6p_flags
;
1658 if (flags
& IN6P_HIGHPORT
)
1659 optval
= IPV6_PORTRANGE_HIGH
;
1660 else if (flags
& IN6P_LOWPORT
)
1661 optval
= IPV6_PORTRANGE_LOW
;
1667 error
= sooptcopyout(sopt
, &optval
,
1676 if ((optname
== IPV6_HOPOPTS
||
1677 optname
== IPV6_DSTOPTS
) &&
1682 optval
= OPTBIT(IN6P_PKTINFO
);
1685 optval
= OPTBIT(IN6P_HOPLIMIT
);
1690 optval
= OPTBIT(IN6P_HOPOPTS
);
1693 optval
= OPTBIT(IN6P_RTHDR
);
1698 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1701 error
= sooptcopyout(sopt
, &optval
,
1705 case IPV6_MULTICAST_IF
:
1706 case IPV6_MULTICAST_HOPS
:
1707 case IPV6_MULTICAST_LOOP
:
1708 case IPV6_JOIN_GROUP
:
1709 case IPV6_LEAVE_GROUP
:
1712 error
= ip6_getmoptions(sopt
->sopt_name
,
1713 in6p
->in6p_moptions
, &m
);
1715 error
= sooptcopyout(sopt
,
1716 mtod(m
, char *), m
->m_len
);
1722 case IPV6_IPSEC_POLICY
:
1726 struct mbuf
*m
= NULL
;
1727 struct mbuf
**mp
= &m
;
1729 if (sopt
->sopt_valsize
> MCLBYTES
) {
1733 error
= soopt_getm(sopt
, &m
); /* XXX */
1736 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1740 req
= mtod(m
, caddr_t
);
1743 lck_mtx_lock(sadb_mutex
);
1744 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
1745 lck_mtx_unlock(sadb_mutex
);
1747 error
= soopt_mcopyout(sopt
, m
); /*XXX*/
1748 if (error
== 0 && m
)
1752 #endif /* KAME IPSEC */
1756 if (ip6_fw_ctl_ptr
== NULL
&& load_ipfw() != 0)
1759 error
= (*ip6_fw_ctl_ptr
)(sopt
);
1764 error
= ENOPROTOOPT
;
1776 * Set up IP6 options in pcb for insertion in output packets or
1777 * specifying behavior of outgoing packets.
1780 ip6_pcbopts(pktopt
, m
, so
, sopt
)
1781 struct ip6_pktopts
**pktopt
;
1784 struct sockopt
*sopt
;
1786 struct ip6_pktopts
*opt
= *pktopt
;
1788 struct proc
*p
= sopt
->sopt_p
;
1791 /* turn off any old options. */
1794 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
1795 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
1796 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1797 printf("ip6_pcbopts: all specified options are cleared.\n");
1799 ip6_clearpktopts(opt
, 1, -1);
1801 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
1804 if (!m
|| m
->m_len
== 0) {
1806 * Only turning off any previous options, regardless of
1807 * whether the opt is just created or given.
1810 FREE(opt
, M_IP6OPT
);
1814 /* set options specified by user. */
1815 if (p
&& !proc_suser(p
))
1817 if ((error
= ip6_setpktoptions(m
, opt
, priv
, 1)) != 0) {
1818 ip6_clearpktopts(opt
, 1, -1); /* XXX: discard all options */
1819 FREE(opt
, M_IP6OPT
);
1827 * initialize ip6_pktopts. beware that there are non-zero default values in
1831 init_ip6pktopts(opt
)
1832 struct ip6_pktopts
*opt
;
1835 bzero(opt
, sizeof(*opt
));
1836 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
1840 ip6_clearpktopts(pktopt
, needfree
, optname
)
1841 struct ip6_pktopts
*pktopt
;
1842 int needfree
, optname
;
1847 if (optname
== -1) {
1848 if (needfree
&& pktopt
->ip6po_pktinfo
)
1849 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
1850 pktopt
->ip6po_pktinfo
= NULL
;
1853 pktopt
->ip6po_hlim
= -1;
1854 if (optname
== -1) {
1855 if (needfree
&& pktopt
->ip6po_nexthop
)
1856 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
1857 pktopt
->ip6po_nexthop
= NULL
;
1859 if (optname
== -1) {
1860 if (needfree
&& pktopt
->ip6po_hbh
)
1861 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
1862 pktopt
->ip6po_hbh
= NULL
;
1864 if (optname
== -1) {
1865 if (needfree
&& pktopt
->ip6po_dest1
)
1866 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
1867 pktopt
->ip6po_dest1
= NULL
;
1869 if (optname
== -1) {
1870 if (needfree
&& pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1871 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
1872 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
1873 if (pktopt
->ip6po_route
.ro_rt
) {
1874 rtfree(pktopt
->ip6po_route
.ro_rt
);
1875 pktopt
->ip6po_route
.ro_rt
= NULL
;
1878 if (optname
== -1) {
1879 if (needfree
&& pktopt
->ip6po_dest2
)
1880 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
1881 pktopt
->ip6po_dest2
= NULL
;
1885 #define PKTOPT_EXTHDRCPY(type) \
1889 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1890 dst->type = _MALLOC(hlen, M_IP6OPT, canwait);\
1891 if (dst->type == NULL && canwait == M_NOWAIT)\
1893 bcopy(src->type, dst->type, hlen);\
1897 struct ip6_pktopts
*
1898 ip6_copypktopts(src
, canwait
)
1899 struct ip6_pktopts
*src
;
1902 struct ip6_pktopts
*dst
;
1905 printf("ip6_clearpktopts: invalid argument\n");
1909 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
1910 if (dst
== NULL
&& canwait
== M_NOWAIT
)
1912 bzero(dst
, sizeof(*dst
));
1914 dst
->ip6po_hlim
= src
->ip6po_hlim
;
1915 if (src
->ip6po_pktinfo
) {
1916 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
1918 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
1920 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
1922 if (src
->ip6po_nexthop
) {
1923 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
1925 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
1927 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
1928 src
->ip6po_nexthop
->sa_len
);
1930 PKTOPT_EXTHDRCPY(ip6po_hbh
);
1931 PKTOPT_EXTHDRCPY(ip6po_dest1
);
1932 PKTOPT_EXTHDRCPY(ip6po_dest2
);
1933 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
1937 if (dst
->ip6po_pktinfo
) FREE(dst
->ip6po_pktinfo
, M_IP6OPT
);
1938 if (dst
->ip6po_nexthop
) FREE(dst
->ip6po_nexthop
, M_IP6OPT
);
1939 if (dst
->ip6po_hbh
) FREE(dst
->ip6po_hbh
, M_IP6OPT
);
1940 if (dst
->ip6po_dest1
) FREE(dst
->ip6po_dest1
, M_IP6OPT
);
1941 if (dst
->ip6po_dest2
) FREE(dst
->ip6po_dest2
, M_IP6OPT
);
1942 if (dst
->ip6po_rthdr
) FREE(dst
->ip6po_rthdr
, M_IP6OPT
);
1943 FREE(dst
, M_IP6OPT
);
1946 #undef PKTOPT_EXTHDRCPY
1949 ip6_freepcbopts(pktopt
)
1950 struct ip6_pktopts
*pktopt
;
1955 ip6_clearpktopts(pktopt
, 1, -1);
1957 FREE(pktopt
, M_IP6OPT
);
1961 * Set the IP6 multicast options in response to user setsockopt().
1970 u_int loop
, ifindex
;
1971 struct ipv6_mreq
*mreq
;
1973 struct ip6_moptions
**im6op
= &in6p
->in6p_moptions
;
1974 struct ip6_moptions
*im6o
= *im6op
;
1975 struct ip_moptions
*imo
;
1976 struct route_in6 ro
;
1977 struct sockaddr_in6
*dst
;
1978 struct in6_multi_mship
*imm
;
1979 struct proc
*p
= current_proc(); /* XXX */
1983 * No multicast option buffer attached to the pcb;
1984 * allocate one and initialize to default values.
1986 im6o
= (struct ip6_moptions
*)
1987 _MALLOC(sizeof(*im6o
), M_IPMOPTS
, M_WAITOK
);
1992 im6o
->im6o_multicast_ifp
= NULL
;
1993 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1994 im6o
->im6o_multicast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
1995 LIST_INIT(&im6o
->im6o_memberships
);
1998 if (in6p
->inp_moptions
== NULL
) {
2000 * No IPv4 multicast option buffer attached to the pcb;
2001 * call ip_createmoptions to allocate one and initialize
2002 * to default values.
2004 error
= ip_createmoptions(&in6p
->inp_moptions
);
2008 imo
= in6p
->inp_moptions
;
2012 case IPV6_MULTICAST_IF
:
2014 * Select the interface for outgoing multicast packets.
2016 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
2020 bcopy(mtod(m
, u_int
*), &ifindex
, sizeof(ifindex
));
2021 if (ifindex
< 0 || if_index
< ifindex
) {
2022 error
= ENXIO
; /* XXX EINVAL? */
2025 ifp
= ifindex2ifnet
[ifindex
];
2026 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2027 error
= EADDRNOTAVAIL
;
2030 im6o
->im6o_multicast_ifp
= ifp
;
2031 imo
->imo_multicast_ifp
= ifp
;
2034 case IPV6_MULTICAST_HOPS
:
2037 * Set the IP6 hoplimit for outgoing multicast packets.
2040 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
2044 bcopy(mtod(m
, u_int
*), &optval
, sizeof(optval
));
2045 if (optval
< -1 || optval
>= 256)
2047 else if (optval
== -1) {
2048 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
2049 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
2051 im6o
->im6o_multicast_hlim
= optval
;
2052 imo
->imo_multicast_ttl
= optval
;
2057 case IPV6_MULTICAST_LOOP
:
2059 * Set the loopback flag for outgoing multicast packets.
2060 * Must be zero or one.
2062 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
2066 bcopy(mtod(m
, u_int
*), &loop
, sizeof(loop
));
2071 im6o
->im6o_multicast_loop
= loop
;
2072 imo
->imo_multicast_loop
= loop
;
2075 case IPV6_JOIN_GROUP
:
2077 * Add a multicast group membership.
2078 * Group must be a valid IP6 multicast address.
2080 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2084 mreq
= mtod(m
, struct ipv6_mreq
*);
2086 * If the interface is specified, validate it.
2088 if (mreq
->ipv6mr_interface
< 0
2089 || if_index
< mreq
->ipv6mr_interface
) {
2090 error
= ENXIO
; /* XXX EINVAL? */
2094 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2096 * We use the unspecified address to specify to accept
2097 * all multicast addresses. Only super user is allowed
2100 if (suser(kauth_cred_get(), 0))
2105 } else if (IN6_IS_ADDR_V4MAPPED(&mreq
->ipv6mr_multiaddr
)) {
2106 struct ip_mreq v4req
;
2108 v4req
.imr_multiaddr
.s_addr
= mreq
->ipv6mr_multiaddr
.s6_addr32
[3];
2109 v4req
.imr_interface
.s_addr
= INADDR_ANY
;
2111 /* Find an IPv4 address on the specified interface. */
2112 if (mreq
->ipv6mr_interface
!= 0) {
2113 struct in_ifaddr
*ifa
;
2115 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2117 lck_mtx_lock(rt_mtx
);
2118 TAILQ_FOREACH(ifa
, &in_ifaddrhead
, ia_link
) {
2119 if (ifa
->ia_ifp
== ifp
) {
2120 v4req
.imr_interface
= IA_SIN(ifa
)->sin_addr
;
2124 lck_mtx_unlock(rt_mtx
);
2126 if (v4req
.imr_multiaddr
.s_addr
== 0) {
2127 /* Interface has no IPv4 address. */
2133 error
= ip_addmembership(imo
, &v4req
);
2135 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2140 * If no interface was explicitly specified, choose an
2141 * appropriate one according to the given multicast address.
2143 if (mreq
->ipv6mr_interface
== 0) {
2145 * If the multicast address is in node-local scope,
2146 * the interface should be a loopback interface.
2147 * Otherwise, look up the routing table for the
2148 * address, and choose the outgoing interface.
2149 * XXX: is it a good approach?
2151 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq
->ipv6mr_multiaddr
)) {
2155 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
2156 bzero(dst
, sizeof(*dst
));
2157 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2158 dst
->sin6_family
= AF_INET6
;
2159 dst
->sin6_addr
= mreq
->ipv6mr_multiaddr
;
2160 rtalloc((struct route
*)&ro
);
2161 if (ro
.ro_rt
== NULL
) {
2162 error
= EADDRNOTAVAIL
;
2165 ifp
= ro
.ro_rt
->rt_ifp
;
2169 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2172 * See if we found an interface, and confirm that it
2173 * supports multicast
2175 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2176 error
= EADDRNOTAVAIL
;
2180 * Put interface index into the multicast address,
2181 * if the address has link-local scope.
2183 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2184 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2185 = htons(mreq
->ipv6mr_interface
);
2188 * See if the membership already exists.
2190 lck_mtx_lock(nd6_mutex
);
2191 for (imm
= im6o
->im6o_memberships
.lh_first
;
2192 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2193 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2194 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2195 &mreq
->ipv6mr_multiaddr
))
2199 lck_mtx_unlock(nd6_mutex
);
2203 * Everything looks good; add a new record to the multicast
2204 * address list for the given interface.
2206 imm
= _MALLOC(sizeof(*imm
), M_IPMADDR
, M_WAITOK
);
2209 lck_mtx_unlock(nd6_mutex
);
2212 if ((imm
->i6mm_maddr
=
2213 in6_addmulti(&mreq
->ipv6mr_multiaddr
, ifp
, &error
, 1)) == NULL
) {
2214 FREE(imm
, M_IPMADDR
);
2215 lck_mtx_unlock(nd6_mutex
);
2218 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2219 lck_mtx_unlock(nd6_mutex
);
2222 case IPV6_LEAVE_GROUP
:
2224 * Drop a multicast group membership.
2225 * Group must be a valid IP6 multicast address.
2227 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2231 mreq
= mtod(m
, struct ipv6_mreq
*);
2233 * If an interface address was specified, get a pointer
2234 * to its ifnet structure.
2236 if (mreq
->ipv6mr_interface
< 0
2237 || if_index
< mreq
->ipv6mr_interface
) {
2238 error
= ENXIO
; /* XXX EINVAL? */
2241 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2243 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2244 if (suser(kauth_cred_get(), 0)) {
2248 } else if (IN6_IS_ADDR_V4MAPPED(&mreq
->ipv6mr_multiaddr
)) {
2249 struct ip_mreq v4req
;
2251 v4req
.imr_multiaddr
.s_addr
= mreq
->ipv6mr_multiaddr
.s6_addr32
[3];
2252 v4req
.imr_interface
.s_addr
= INADDR_ANY
;
2255 struct in_ifaddr
*ifa
;
2257 lck_mtx_lock(rt_mtx
);
2258 TAILQ_FOREACH(ifa
, &in_ifaddrhead
, ia_link
) {
2259 if (ifa
->ia_ifp
== ifp
) {
2260 v4req
.imr_interface
= IA_SIN(ifa
)->sin_addr
;
2264 lck_mtx_unlock(rt_mtx
);
2267 error
= ip_dropmembership(imo
, &v4req
);
2269 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2274 * Put interface index into the multicast address,
2275 * if the address has link-local scope.
2277 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2278 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2279 = htons(mreq
->ipv6mr_interface
);
2282 * Find the membership in the membership list.
2284 lck_mtx_lock(nd6_mutex
);
2285 for (imm
= im6o
->im6o_memberships
.lh_first
;
2286 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2288 imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2289 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2290 &mreq
->ipv6mr_multiaddr
))
2294 /* Unable to resolve interface */
2295 error
= EADDRNOTAVAIL
;
2296 lck_mtx_unlock(nd6_mutex
);
2300 * Give up the multicast address record to which the
2301 * membership points.
2303 LIST_REMOVE(imm
, i6mm_chain
);
2304 in6_delmulti(imm
->i6mm_maddr
, 1);
2305 lck_mtx_unlock(nd6_mutex
);
2306 FREE(imm
, M_IPMADDR
);
2315 * If all options have default values, no need to keep the mbuf.
2317 lck_mtx_lock(nd6_mutex
);
2318 if (im6o
->im6o_multicast_ifp
== NULL
&&
2319 im6o
->im6o_multicast_hlim
== ip6_defmcasthlim
&&
2320 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2321 im6o
->im6o_memberships
.lh_first
== NULL
) {
2322 FREE(*im6op
, M_IPMOPTS
);
2325 if (imo
->imo_multicast_ifp
== NULL
&&
2326 imo
->imo_multicast_vif
== -1 &&
2327 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2328 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2329 imo
->imo_num_memberships
== 0) {
2330 ip_freemoptions(imo
);
2331 in6p
->inp_moptions
= 0;
2333 lck_mtx_unlock(nd6_mutex
);
2339 * Return the IP6 multicast options in response to user getsockopt().
2342 ip6_getmoptions(optname
, im6o
, mp
)
2344 struct ip6_moptions
*im6o
;
2347 u_int
*hlim
, *loop
, *ifindex
;
2349 *mp
= m_get(M_WAIT
, MT_HEADER
); /*XXX*/
2353 case IPV6_MULTICAST_IF
:
2354 ifindex
= mtod(*mp
, u_int
*);
2355 (*mp
)->m_len
= sizeof(u_int
);
2356 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2359 *ifindex
= im6o
->im6o_multicast_ifp
->if_index
;
2362 case IPV6_MULTICAST_HOPS
:
2363 hlim
= mtod(*mp
, u_int
*);
2364 (*mp
)->m_len
= sizeof(u_int
);
2366 *hlim
= ip6_defmcasthlim
;
2368 *hlim
= im6o
->im6o_multicast_hlim
;
2371 case IPV6_MULTICAST_LOOP
:
2372 loop
= mtod(*mp
, u_int
*);
2373 (*mp
)->m_len
= sizeof(u_int
);
2375 *loop
= ip6_defmcasthlim
;
2377 *loop
= im6o
->im6o_multicast_loop
;
2386 * Discard the IP6 multicast options.
2389 ip6_freemoptions(im6o
)
2390 struct ip6_moptions
*im6o
;
2392 struct in6_multi_mship
*imm
;
2397 lck_mtx_lock(nd6_mutex
);
2398 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2399 LIST_REMOVE(imm
, i6mm_chain
);
2400 if (imm
->i6mm_maddr
)
2401 in6_delmulti(imm
->i6mm_maddr
, 1);
2402 FREE(imm
, M_IPMADDR
);
2404 lck_mtx_unlock(nd6_mutex
);
2405 FREE(im6o
, M_IPMOPTS
);
2409 * Set IPv6 outgoing packet options based on advanced API.
2412 ip6_setpktoptions(control
, opt
, priv
, needcopy
)
2413 struct mbuf
*control
;
2414 struct ip6_pktopts
*opt
;
2417 struct cmsghdr
*cm
= 0;
2419 if (control
== 0 || opt
== 0)
2422 init_ip6pktopts(opt
);
2425 * XXX: Currently, we assume all the optional information is stored
2428 if (control
->m_next
)
2431 for (; control
->m_len
; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2432 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2433 cm
= mtod(control
, struct cmsghdr
*);
2434 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2436 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2440 * XXX should check if RFC2292 API is mixed with 2292bis API
2442 switch (cm
->cmsg_type
) {
2444 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(struct in6_pktinfo
)))
2447 /* XXX: Is it really WAITOK? */
2448 opt
->ip6po_pktinfo
=
2449 _MALLOC(sizeof(struct in6_pktinfo
),
2450 M_IP6OPT
, M_WAITOK
);
2451 bcopy(CMSG_DATA(cm
), opt
->ip6po_pktinfo
,
2452 sizeof(struct in6_pktinfo
));
2454 opt
->ip6po_pktinfo
=
2455 (struct in6_pktinfo
*)CMSG_DATA(cm
);
2456 if (opt
->ip6po_pktinfo
->ipi6_ifindex
&&
2457 IN6_IS_ADDR_LINKLOCAL(&opt
->ip6po_pktinfo
->ipi6_addr
))
2458 opt
->ip6po_pktinfo
->ipi6_addr
.s6_addr16
[1] =
2459 htons(opt
->ip6po_pktinfo
->ipi6_ifindex
);
2461 if (opt
->ip6po_pktinfo
->ipi6_ifindex
> if_index
2462 || opt
->ip6po_pktinfo
->ipi6_ifindex
< 0) {
2467 * Check if the requested source address is indeed a
2468 * unicast address assigned to the node, and can be
2469 * used as the packet's source address.
2471 if (!IN6_IS_ADDR_UNSPECIFIED(&opt
->ip6po_pktinfo
->ipi6_addr
)) {
2472 struct in6_ifaddr
*ia6
;
2473 struct sockaddr_in6 sin6
;
2475 bzero(&sin6
, sizeof(sin6
));
2476 sin6
.sin6_len
= sizeof(sin6
);
2477 sin6
.sin6_family
= AF_INET6
;
2479 opt
->ip6po_pktinfo
->ipi6_addr
;
2480 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr(sin6tosa(&sin6
));
2482 (ia6
->ia6_flags
& (IN6_IFF_ANYCAST
|
2483 IN6_IFF_NOTREADY
)) != 0) {
2484 if (ia6
) ifafree(&ia6
->ia_ifa
);
2485 return(EADDRNOTAVAIL
);
2487 ifafree(&ia6
->ia_ifa
);
2493 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
2496 opt
->ip6po_hlim
= *(int *)CMSG_DATA(cm
);
2497 if (opt
->ip6po_hlim
< -1 || opt
->ip6po_hlim
> 255)
2505 if (cm
->cmsg_len
< sizeof(u_char
) ||
2506 /* check if cmsg_len is large enough for sa_len */
2507 cm
->cmsg_len
< CMSG_LEN(*CMSG_DATA(cm
)))
2511 opt
->ip6po_nexthop
=
2512 _MALLOC(*CMSG_DATA(cm
),
2513 M_IP6OPT
, M_WAITOK
);
2514 bcopy(CMSG_DATA(cm
),
2518 opt
->ip6po_nexthop
=
2519 (struct sockaddr
*)CMSG_DATA(cm
);
2524 struct ip6_hbh
*hbh
;
2527 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_hbh
)))
2529 hbh
= (struct ip6_hbh
*)CMSG_DATA(cm
);
2530 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
2531 if (cm
->cmsg_len
!= CMSG_LEN(hbhlen
))
2536 _MALLOC(hbhlen
, M_IP6OPT
, M_WAITOK
);
2537 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
2539 opt
->ip6po_hbh
= hbh
;
2545 struct ip6_dest
*dest
, **newdest
;
2548 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_dest
)))
2550 dest
= (struct ip6_dest
*)CMSG_DATA(cm
);
2551 destlen
= (dest
->ip6d_len
+ 1) << 3;
2552 if (cm
->cmsg_len
!= CMSG_LEN(destlen
))
2556 * The old advacned API is ambiguous on this
2557 * point. Our approach is to determine the
2558 * position based according to the existence
2559 * of a routing header. Note, however, that
2560 * this depends on the order of the extension
2561 * headers in the ancillary data; the 1st part
2562 * of the destination options header must
2563 * appear before the routing header in the
2564 * ancillary data, too.
2565 * RFC2292bis solved the ambiguity by
2566 * introducing separate cmsg types.
2568 if (opt
->ip6po_rthdr
== NULL
)
2569 newdest
= &opt
->ip6po_dest1
;
2571 newdest
= &opt
->ip6po_dest2
;
2574 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_WAITOK
);
2575 bcopy(dest
, *newdest
, destlen
);
2584 struct ip6_rthdr
*rth
;
2587 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_rthdr
)))
2589 rth
= (struct ip6_rthdr
*)CMSG_DATA(cm
);
2590 rthlen
= (rth
->ip6r_len
+ 1) << 3;
2591 if (cm
->cmsg_len
!= CMSG_LEN(rthlen
))
2594 switch (rth
->ip6r_type
) {
2595 case IPV6_RTHDR_TYPE_0
:
2596 /* must contain one addr */
2597 if (rth
->ip6r_len
== 0)
2599 /* length must be even */
2600 if (rth
->ip6r_len
% 2)
2602 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
2606 return(EINVAL
); /* not supported */
2610 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
,
2612 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
2614 opt
->ip6po_rthdr
= rth
;
2620 return(ENOPROTOOPT
);
2628 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2629 * packet to the input queue of a specified interface. Note that this
2630 * calls the output routine of the loopback "driver", but with an interface
2631 * pointer that might NOT be &loif -- easier than replicating that code here.
2637 struct sockaddr_in6
*dst
)
2640 struct ip6_hdr
*ip6
;
2642 copym
= m_copy(m
, 0, M_COPYALL
);
2647 * Make sure to deep-copy IPv6 header portion in case the data
2648 * is in an mbuf cluster, so that we can safely override the IPv6
2649 * header portion later.
2651 if ((copym
->m_flags
& M_EXT
) != 0 ||
2652 copym
->m_len
< sizeof(struct ip6_hdr
)) {
2653 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
2659 if (copym
->m_len
< sizeof(*ip6
)) {
2665 ip6
= mtod(copym
, struct ip6_hdr
*);
2666 #ifndef SCOPEDROUTING
2668 * clear embedded scope identifiers if necessary.
2669 * in6_clearscope will touch the addresses only when necessary.
2671 in6_clearscope(&ip6
->ip6_src
);
2672 in6_clearscope(&ip6
->ip6_dst
);
2677 /* Makes sure the HW checksum flags are cleaned before sending the packet */
2679 copym
->m_pkthdr
.rcvif
= 0;
2680 copym
->m_pkthdr
.csum_data
= 0;
2681 copym
->m_pkthdr
.csum_flags
= 0;
2684 copym
->m_pkthdr
.rcvif
= ifp
;
2685 lck_mtx_unlock(ip6_mutex
);
2686 dlil_output(lo_ifp
, PF_INET6
, copym
, 0, (struct sockaddr
*)dst
, 0);
2687 lck_mtx_lock(ip6_mutex
);
2691 (void)if_simloop(ifp
, copym
, dst
->sin6_family
, NULL
);
2696 * Chop IPv6 header off from the payload.
2699 ip6_splithdr(m
, exthdrs
)
2701 struct ip6_exthdrs
*exthdrs
;
2704 struct ip6_hdr
*ip6
;
2706 ip6
= mtod(m
, struct ip6_hdr
*);
2707 if (m
->m_len
> sizeof(*ip6
)) {
2708 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
2713 M_COPY_PKTHDR(mh
, m
);
2714 MH_ALIGN(mh
, sizeof(*ip6
));
2715 m
->m_flags
&= ~M_PKTHDR
;
2716 m
->m_len
-= sizeof(*ip6
);
2717 m
->m_data
+= sizeof(*ip6
);
2720 m
->m_len
= sizeof(*ip6
);
2721 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
2723 exthdrs
->ip6e_ip6
= m
;
2728 * Compute IPv6 extension header length.
2732 struct in6pcb
*in6p
;
2736 if (!in6p
->in6p_outputopts
)
2741 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2743 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
2744 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
2745 /* dest1 is valid with rthdr only */
2746 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
2747 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
2748 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);