2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/errno.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/ucred.h>
81 #include <sys/vimage.h>
84 #include <net/netisr.h>
85 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet6/nd6.h>
99 #include <netipsec/ipsec.h>
100 #include <netipsec/ipsec6.h>
101 #include <netipsec/key.h>
102 #include <netinet6/ip6_ipsec.h>
105 #include <netinet6/ip6protosw.h>
106 #include <netinet6/scope6_var.h>
108 static MALLOC_DEFINE(M_IP6MOPTS
, "ip6_moptions", "internet multicast options");
111 struct mbuf
*ip6e_ip6
;
112 struct mbuf
*ip6e_hbh
;
113 struct mbuf
*ip6e_dest1
;
114 struct mbuf
*ip6e_rthdr
;
115 struct mbuf
*ip6e_dest2
;
118 static int ip6_pcbopt
__P((int, u_char
*, int, struct ip6_pktopts
**,
119 struct ucred
*, int));
120 static int ip6_pcbopts
__P((struct ip6_pktopts
**, struct mbuf
*,
121 struct socket
*, struct sockopt
*));
122 static int ip6_getpcbopt(struct ip6_pktopts
*, int, struct sockopt
*);
123 static int ip6_setpktopt
__P((int, u_char
*, int, struct ip6_pktopts
*,
124 struct ucred
*, int, int, int));
126 static int ip6_setmoptions(int, struct ip6_moptions
**, struct mbuf
*);
127 static int ip6_getmoptions(int, struct ip6_moptions
*, struct mbuf
**);
128 static int ip6_copyexthdr(struct mbuf
**, caddr_t
, int);
129 static int ip6_insertfraghdr
__P((struct mbuf
*, struct mbuf
*, int,
130 struct ip6_frag
**));
131 static int ip6_insert_jumboopt(struct ip6_exthdrs
*, u_int32_t
);
132 static int ip6_splithdr(struct mbuf
*, struct ip6_exthdrs
*);
133 static int ip6_getpmtu
__P((struct route_in6
*, struct route_in6
*,
134 struct ifnet
*, struct in6_addr
*, u_long
*, int *));
135 static int copypktopts(struct ip6_pktopts
*, struct ip6_pktopts
*, int);
139 * Make an extension header from option data. hp is the source, and
140 * mp is the destination.
142 #define MAKE_EXTHDR(hp, mp) \
145 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
146 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
147 ((eh)->ip6e_len + 1) << 3); \
151 } while (/*CONSTCOND*/ 0)
154 * Form a chain of extension headers.
155 * m is the extension header mbuf
156 * mp is the previous mbuf in the chain
157 * p is the next header
158 * i is the type of option.
160 #define MAKE_CHAIN(m, mp, p, i)\
164 panic("assumption failed: hdr not split"); \
165 *mtod((m), u_char *) = *(p);\
167 p = mtod((m), u_char *);\
168 (m)->m_next = (mp)->m_next;\
172 } while (/*CONSTCOND*/ 0)
175 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
176 * header (with pri, len, nxt, hlim, src, dst).
177 * This function may modify ver and hlim only.
178 * The mbuf chain containing the packet will be freed.
179 * The mbuf opt, if present, will not be freed.
181 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
182 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
183 * which is rt_rmx.rmx_mtu.
185 * ifpp - XXX: just for statistics
188 ip6_output(struct mbuf
*m0
, struct ip6_pktopts
*opt
,
189 struct route_in6
*ro
, int flags
, struct ip6_moptions
*im6o
,
190 struct ifnet
**ifpp
, struct inpcb
*inp
)
192 struct ip6_hdr
*ip6
, *mhip6
;
193 struct ifnet
*ifp
, *origifp
;
195 struct mbuf
*mprev
= NULL
;
196 int hlen
, tlen
, len
, off
;
197 struct route_in6 ip6route
;
198 struct rtentry
*rt
= NULL
;
199 struct sockaddr_in6
*dst
, src_sa
, dst_sa
;
200 struct in6_addr odst
;
202 struct in6_ifaddr
*ia
= NULL
;
204 int alwaysfrag
, dontfrag
;
205 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
206 struct ip6_exthdrs exthdrs
;
207 struct in6_addr finaldst
, src0
, dst0
;
209 struct route_in6
*ro_pmtu
= NULL
;
213 struct ipsec_output_state state
;
214 struct ip6_rthdr
*rh
= NULL
;
215 int needipsectun
= 0;
217 struct secpolicy
*sp
= NULL
;
220 ip6
= mtod(m
, struct ip6_hdr
*);
222 printf ("ip6 is NULL");
226 finaldst
= ip6
->ip6_dst
;
228 bzero(&exthdrs
, sizeof(exthdrs
));
231 /* Hop-by-Hop options header */
232 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
233 /* Destination options header(1st part) */
234 if (opt
->ip6po_rthdr
) {
236 * Destination options header(1st part)
237 * This only makes sense with a routing header.
238 * See Section 9.2 of RFC 3542.
239 * Disabling this part just for MIP6 convenience is
240 * a bad idea. We need to think carefully about a
241 * way to make the advanced API coexist with MIP6
242 * options, which might automatically be inserted in
245 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
248 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
249 /* Destination options header(2nd part) */
250 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
254 * IPSec checking which handles several cases.
255 * FAST IPSEC: We re-injected the packet.
258 switch(ip6_ipsec_output(&m
, inp
, &flags
, &error
, &ifp
, &sp
))
260 case 1: /* Bad packet */
262 case -1: /* Do IPSec */
264 case 0: /* No IPSec */
271 * Calculate the total length of the extension header chain.
272 * Keep the length of the unfragmentable part for fragmentation.
275 if (exthdrs
.ip6e_hbh
)
276 optlen
+= exthdrs
.ip6e_hbh
->m_len
;
277 if (exthdrs
.ip6e_dest1
)
278 optlen
+= exthdrs
.ip6e_dest1
->m_len
;
279 if (exthdrs
.ip6e_rthdr
)
280 optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
281 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
283 /* NOTE: we don't add AH/ESP length here. do that later. */
284 if (exthdrs
.ip6e_dest2
)
285 optlen
+= exthdrs
.ip6e_dest2
->m_len
;
288 * If we need IPsec, or there is at least one extension header,
289 * separate IP6 header from the payload.
291 if ((needipsec
|| optlen
) && !hdrsplit
) {
292 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
296 m
= exthdrs
.ip6e_ip6
;
301 ip6
= mtod(m
, struct ip6_hdr
*);
303 /* adjust mbuf packet header length */
304 m
->m_pkthdr
.len
+= optlen
;
305 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
307 /* If this is a jumbo payload, insert a jumbo payload option. */
308 if (plen
> IPV6_MAXPACKET
) {
310 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
314 m
= exthdrs
.ip6e_ip6
;
318 ip6
= mtod(m
, struct ip6_hdr
*);
319 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
323 ip6
->ip6_plen
= htons(plen
);
326 * Concatenate headers and fill in next header fields.
327 * Here we have, on "m"
329 * and we insert headers accordingly. Finally, we should be getting:
330 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
332 * during the header composing process, "m" points to IPv6 header.
333 * "mprev" points to an extension header prior to esp.
335 u_char
*nexthdrp
= &ip6
->ip6_nxt
;
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
;
356 * result: IPv6 hbh dest1 rthdr dest2 payload
357 * m will point to IPv6 header. mprev will point to the
358 * extension header prior to dest2 (rthdr in the above case).
360 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
361 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
,
363 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
,
371 * pointers after IPsec headers are not valid any more.
372 * other pointers need a great care too.
373 * (IPsec routines should not mangle mbufs prior to AH/ESP)
375 exthdrs
.ip6e_dest2
= NULL
;
377 if (exthdrs
.ip6e_rthdr
) {
378 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
379 segleft_org
= rh
->ip6r_segleft
;
380 rh
->ip6r_segleft
= 0;
383 bzero(&state
, sizeof(state
));
385 error
= ipsec6_output_trans(&state
, nexthdrp
, mprev
, sp
, flags
,
388 if (error
== EJUSTRETURN
) {
390 * We had a SP with a level of 'use' and no SA. We
391 * will just continue to process the packet without
396 /* mbuf is already reclaimed in ipsec6_output_trans. */
406 printf("[%s:%d] (ipsec): error code %d\n",
407 __func__
, __LINE__
, error
);
410 /* don't show these error codes to the user */
415 } else if (!needipsectun
) {
417 * In the FAST IPSec case we have already
418 * re-injected the packet and it has been freed
419 * by the ipsec_done() function. So, just clean
420 * up after ourselves.
425 if (exthdrs
.ip6e_rthdr
) {
426 /* ah6_output doesn't modify mbuf chain */
427 rh
->ip6r_segleft
= segleft_org
;
433 * If there is a routing header, replace the destination address field
434 * with the first hop of the routing header.
436 if (exthdrs
.ip6e_rthdr
) {
437 struct ip6_rthdr
*rh
=
438 (struct ip6_rthdr
*)(mtod(exthdrs
.ip6e_rthdr
,
439 struct ip6_rthdr
*));
440 struct ip6_rthdr0
*rh0
;
441 struct in6_addr
*addr
;
442 struct sockaddr_in6 sa
;
444 switch (rh
->ip6r_type
) {
445 case IPV6_RTHDR_TYPE_0
:
446 rh0
= (struct ip6_rthdr0
*)rh
;
447 addr
= (struct in6_addr
*)(rh0
+ 1);
450 * construct a sockaddr_in6 form of
453 * XXX: we may not have enough
454 * information about its scope zone;
455 * there is no standard API to pass
456 * the information from the
459 bzero(&sa
, sizeof(sa
));
460 sa
.sin6_family
= AF_INET6
;
461 sa
.sin6_len
= sizeof(sa
);
462 sa
.sin6_addr
= addr
[0];
463 if ((error
= sa6_embedscope(&sa
,
464 V_ip6_use_defzone
)) != 0) {
467 ip6
->ip6_dst
= sa
.sin6_addr
;
468 bcopy(&addr
[1], &addr
[0], sizeof(struct in6_addr
)
469 * (rh0
->ip6r0_segleft
- 1));
470 addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
472 in6_clearscope(addr
+ rh0
->ip6r0_segleft
- 1);
474 default: /* is it possible? */
480 /* Source address validation */
481 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
482 (flags
& IPV6_UNSPECSRC
) == 0) {
484 V_ip6stat
.ip6s_badscope
++;
487 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
489 V_ip6stat
.ip6s_badscope
++;
493 V_ip6stat
.ip6s_localout
++;
500 bzero((caddr_t
)ro
, sizeof(*ro
));
503 if (opt
&& opt
->ip6po_rthdr
)
504 ro
= &opt
->ip6po_route
;
505 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
509 * if specified, try to fill in the traffic class field.
510 * do not override if a non-zero value is already set.
511 * we check the diffserv field and the ecn field separately.
513 if (opt
&& opt
->ip6po_tclass
>= 0) {
516 if ((ip6
->ip6_flow
& htonl(0xfc << 20)) == 0)
518 if ((ip6
->ip6_flow
& htonl(0x03 << 20)) == 0)
521 ip6
->ip6_flow
|= htonl((opt
->ip6po_tclass
& mask
) << 20);
524 /* fill in or override the hop limit field, if necessary. */
525 if (opt
&& opt
->ip6po_hlim
!= -1)
526 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
527 else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
529 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
531 ip6
->ip6_hlim
= V_ip6_defmcasthlim
;
536 * We may re-inject packets into the stack here.
538 if (needipsec
&& needipsectun
) {
539 struct ipsec_output_state state
;
542 * All the extension headers will become inaccessible
543 * (since they can be encrypted).
544 * Don't panic, we need no more updates to extension headers
545 * on inner IPv6 packet (since they are now encapsulated).
547 * IPv6 [ESP|AH] IPv6 [extension headers] payload
549 bzero(&exthdrs
, sizeof(exthdrs
));
550 exthdrs
.ip6e_ip6
= m
;
552 bzero(&state
, sizeof(state
));
554 state
.ro
= (struct route
*)ro
;
555 state
.dst
= (struct sockaddr
*)dst
;
557 error
= ipsec6_output_tunnel(&state
, sp
, flags
);
560 ro
= (struct route_in6
*)state
.ro
;
561 dst
= (struct sockaddr_in6
*)state
.dst
;
562 if (error
== EJUSTRETURN
) {
564 * We had a SP with a level of 'use' and no SA. We
565 * will just continue to process the packet without
570 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
581 printf("[%s:%d] (ipsec): error code %d\n",
582 __func__
, __LINE__
, error
);
585 /* don't show these error codes to the user */
592 * In the FAST IPSec case we have already
593 * re-injected the packet and it has been freed
594 * by the ipsec_done() function. So, just clean
595 * up after ourselves.
601 exthdrs
.ip6e_ip6
= m
;
606 ip6
= mtod(m
, struct ip6_hdr
*);
608 bzero(&dst_sa
, sizeof(dst_sa
));
609 dst_sa
.sin6_family
= AF_INET6
;
610 dst_sa
.sin6_len
= sizeof(dst_sa
);
611 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
612 if ((error
= in6_selectroute(&dst_sa
, opt
, im6o
, ro
,
613 &ifp
, &rt
, 0)) != 0) {
616 V_ip6stat
.ip6s_noroute
++;
620 break; /* XXX statistics? */
623 in6_ifstat_inc(ifp
, ifs6_out_discard
);
628 * If in6_selectroute() does not return a route entry,
629 * dst may not have been updated.
631 *dst
= dst_sa
; /* XXX */
635 * then rt (for unicast) and ifp must be non-NULL valid values.
637 if ((flags
& IPV6_FORWARDING
) == 0) {
638 /* XXX: the FORWARDING flag can be set for mrouting. */
639 in6_ifstat_inc(ifp
, ifs6_out_request
);
642 ia
= (struct in6_ifaddr
*)(rt
->rt_ifa
);
647 * The outgoing interface must be in the zone of source and
648 * destination addresses. We should use ia_ifp to support the
649 * case of sending packets to an address of our own.
651 if (ia
!= NULL
&& ia
->ia_ifp
)
652 origifp
= ia
->ia_ifp
;
657 if (in6_setscope(&src0
, origifp
, &zone
))
659 bzero(&src_sa
, sizeof(src_sa
));
660 src_sa
.sin6_family
= AF_INET6
;
661 src_sa
.sin6_len
= sizeof(src_sa
);
662 src_sa
.sin6_addr
= ip6
->ip6_src
;
663 if (sa6_recoverscope(&src_sa
) || zone
!= src_sa
.sin6_scope_id
)
667 if (in6_setscope(&dst0
, origifp
, &zone
))
669 /* re-initialize to be sure */
670 bzero(&dst_sa
, sizeof(dst_sa
));
671 dst_sa
.sin6_family
= AF_INET6
;
672 dst_sa
.sin6_len
= sizeof(dst_sa
);
673 dst_sa
.sin6_addr
= ip6
->ip6_dst
;
674 if (sa6_recoverscope(&dst_sa
) || zone
!= dst_sa
.sin6_scope_id
) {
678 /* scope check is done. */
682 V_ip6stat
.ip6s_badscope
++;
683 in6_ifstat_inc(origifp
, ifs6_out_discard
);
685 error
= EHOSTUNREACH
; /* XXX */
689 if (rt
&& !IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
690 if (opt
&& opt
->ip6po_nextroute
.ro_rt
) {
692 * The nexthop is explicitly specified by the
693 * application. We assume the next hop is an IPv6
696 dst
= (struct sockaddr_in6
*)opt
->ip6po_nexthop
;
698 else if ((rt
->rt_flags
& RTF_GATEWAY
))
699 dst
= (struct sockaddr_in6
*)rt
->rt_gateway
;
702 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
703 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
705 struct in6_multi
*in6m
;
707 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
709 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
712 * Confirm that the outgoing interface supports multicast.
714 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
715 V_ip6stat
.ip6s_noroute
++;
716 in6_ifstat_inc(ifp
, ifs6_out_discard
);
720 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
722 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
724 * If we belong to the destination multicast group
725 * on the outgoing interface, and the caller did not
726 * forbid loopback, loop back a copy.
728 ip6_mloopback(ifp
, m
, dst
);
731 * If we are acting as a multicast router, perform
732 * multicast forwarding as if the packet had just
733 * arrived on the interface to which we are about
734 * to send. The multicast forwarding function
735 * recursively calls this function, using the
736 * IPV6_FORWARDING flag to prevent infinite recursion.
738 * Multicasts that are looped back by ip6_mloopback(),
739 * above, will be forwarded by the ip6_input() routine,
742 if (ip6_mrouter
&& (flags
& IPV6_FORWARDING
) == 0) {
744 * XXX: ip6_mforward expects that rcvif is NULL
745 * when it is called from the originating path.
746 * However, it is not always the case, since
747 * some versions of MGETHDR() does not
748 * initialize the field.
750 m
->m_pkthdr
.rcvif
= NULL
;
751 if (ip6_mforward(ip6
, ifp
, m
) != 0) {
758 * Multicasts with a hoplimit of zero may be looped back,
759 * above, but must not be transmitted on a network.
760 * Also, multicasts addressed to the loopback interface
761 * are not sent -- the above call to ip6_mloopback() will
762 * loop back a copy if this host actually belongs to the
763 * destination group on the loopback interface.
765 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
) ||
766 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6
->ip6_dst
)) {
773 * Fill the outgoing inteface to tell the upper layer
774 * to increment per-interface statistics.
779 /* Determine path MTU. */
780 if ((error
= ip6_getpmtu(ro_pmtu
, ro
, ifp
, &finaldst
, &mtu
,
785 * The caller of this function may specify to use the minimum MTU
787 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
788 * setting. The logic is a bit complicated; by default, unicast
789 * packets will follow path MTU while multicast packets will be sent at
790 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
791 * including unicast ones will be sent at the minimum MTU. Multicast
792 * packets will always be sent at the minimum MTU unless
793 * IP6PO_MINMTU_DISABLE is explicitly specified.
794 * See RFC 3542 for more details.
796 if (mtu
> IPV6_MMTU
) {
797 if ((flags
& IPV6_MINMTU
))
799 else if (opt
&& opt
->ip6po_minmtu
== IP6PO_MINMTU_ALL
)
801 else if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
803 opt
->ip6po_minmtu
!= IP6PO_MINMTU_DISABLE
)) {
809 * clear embedded scope identifiers if necessary.
810 * in6_clearscope will touch the addresses only when necessary.
812 in6_clearscope(&ip6
->ip6_src
);
813 in6_clearscope(&ip6
->ip6_dst
);
816 * If the outgoing packet contains a hop-by-hop options header,
817 * it must be examined and processed even by the source node.
818 * (RFC 2460, section 4.)
820 if (exthdrs
.ip6e_hbh
) {
821 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
822 u_int32_t dummy
; /* XXX unused */
823 u_int32_t plen
= 0; /* XXX: ip6_process will check the value */
826 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
827 panic("ip6e_hbh is not continuous");
830 * XXX: if we have to send an ICMPv6 error to the sender,
831 * we need the M_LOOP flag since icmp6_error() expects
832 * the IPv6 and the hop-by-hop options header are
833 * continuous unless the flag is set.
835 m
->m_flags
|= M_LOOP
;
836 m
->m_pkthdr
.rcvif
= ifp
;
837 if (ip6_process_hopopts(m
, (u_int8_t
*)(hbh
+ 1),
838 ((hbh
->ip6h_len
+ 1) << 3) - sizeof(struct ip6_hbh
),
839 &dummy
, &plen
) < 0) {
840 /* m was already freed at this point */
841 error
= EINVAL
;/* better error? */
844 m
->m_flags
&= ~M_LOOP
; /* XXX */
845 m
->m_pkthdr
.rcvif
= NULL
;
848 /* Jump over all PFIL processing if hooks are not active. */
849 if (!PFIL_HOOKED(&inet6_pfil_hook
))
853 /* Run through list of hooks for output packets. */
854 error
= pfil_run_hooks(&inet6_pfil_hook
, &m
, ifp
, PFIL_OUT
, inp
);
855 if (error
!= 0 || m
== NULL
)
857 ip6
= mtod(m
, struct ip6_hdr
*);
859 /* See if destination IP address was changed by packet filter. */
860 if (!IN6_ARE_ADDR_EQUAL(&odst
, &ip6
->ip6_dst
)) {
861 m
->m_flags
|= M_SKIP_FIREWALL
;
862 /* If destination is now ourself drop to ip6_input(). */
863 if (in6_localaddr(&ip6
->ip6_dst
)) {
864 if (m
->m_pkthdr
.rcvif
== NULL
)
865 m
->m_pkthdr
.rcvif
= V_loif
;
866 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
867 m
->m_pkthdr
.csum_flags
|=
868 CSUM_DATA_VALID
| CSUM_PSEUDO_HDR
;
869 m
->m_pkthdr
.csum_data
= 0xffff;
871 m
->m_pkthdr
.csum_flags
|=
872 CSUM_IP_CHECKED
| CSUM_IP_VALID
;
873 error
= netisr_queue(NETISR_IPV6
, m
);
876 goto again
; /* Redo the routing table lookup. */
879 /* XXX: IPFIREWALL_FORWARD */
883 * Send the packet to the outgoing interface.
884 * If necessary, do IPv6 fragmentation before sending.
886 * the logic here is rather complex:
887 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
888 * 1-a: send as is if tlen <= path mtu
889 * 1-b: fragment if tlen > path mtu
891 * 2: if user asks us not to fragment (dontfrag == 1)
892 * 2-a: send as is if tlen <= interface mtu
893 * 2-b: error if tlen > interface mtu
895 * 3: if we always need to attach fragment header (alwaysfrag == 1)
898 * 4: if dontfrag == 1 && alwaysfrag == 1
899 * error, as we cannot handle this conflicting request
901 tlen
= m
->m_pkthdr
.len
;
903 if (opt
&& (opt
->ip6po_flags
& IP6PO_DONTFRAG
))
907 if (dontfrag
&& alwaysfrag
) { /* case 4 */
908 /* conflicting request - can't transmit */
912 if (dontfrag
&& tlen
> IN6_LINKMTU(ifp
)) { /* case 2-b */
914 * Even if the DONTFRAG option is specified, we cannot send the
915 * packet when the data length is larger than the MTU of the
916 * outgoing interface.
917 * Notify the error by sending IPV6_PATHMTU ancillary data as
918 * well as returning an error code (the latter is not described
922 struct ip6ctlparam ip6cp
;
924 mtu32
= (u_int32_t
)mtu
;
925 bzero(&ip6cp
, sizeof(ip6cp
));
926 ip6cp
.ip6c_cmdarg
= (void *)&mtu32
;
927 pfctlinput2(PRC_MSGSIZE
, (struct sockaddr
*)&ro_pmtu
->ro_dst
,
935 * transmit packet without fragmentation
937 if (dontfrag
|| (!alwaysfrag
&& tlen
<= mtu
)) { /* case 1-a and 2-a */
938 struct in6_ifaddr
*ia6
;
940 ip6
= mtod(m
, struct ip6_hdr
*);
941 ia6
= in6_ifawithifp(ifp
, &ip6
->ip6_src
);
943 /* Record statistics for this interface address. */
944 ia6
->ia_ifa
.if_opackets
++;
945 ia6
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
947 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
952 * try to fragment the packet. case 1-b and 3
954 if (mtu
< IPV6_MMTU
) {
955 /* path MTU cannot be less than IPV6_MMTU */
957 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
959 } else if (ip6
->ip6_plen
== 0) {
960 /* jumbo payload cannot be fragmented */
962 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
965 struct mbuf
**mnext
, *m_frgpart
;
966 struct ip6_frag
*ip6f
;
967 u_int32_t id
= htonl(ip6_randomid());
970 int qslots
= ifp
->if_snd
.ifq_maxlen
- ifp
->if_snd
.ifq_len
;
973 * Too large for the destination or interface;
974 * fragment if possible.
975 * Must be able to put at least 8 bytes per fragment.
977 hlen
= unfragpartlen
;
978 if (mtu
> IPV6_MAXPACKET
)
979 mtu
= IPV6_MAXPACKET
;
981 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
984 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
989 * Verify that we have any chance at all of being able to queue
990 * the packet or packet fragments
992 if (qslots
<= 0 || ((u_int
)qslots
* (mtu
- hlen
)
993 < tlen
/* - hlen */)) {
995 V_ip6stat
.ip6s_odropped
++;
999 mnext
= &m
->m_nextpkt
;
1002 * Change the next header field of the last header in the
1003 * unfragmentable part.
1005 if (exthdrs
.ip6e_rthdr
) {
1006 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
1007 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
1008 } else if (exthdrs
.ip6e_dest1
) {
1009 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
1010 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
1011 } else if (exthdrs
.ip6e_hbh
) {
1012 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
1013 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
1015 nextproto
= ip6
->ip6_nxt
;
1016 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
1020 * Loop through length of segment after first fragment,
1021 * make new header and copy data of each part and link onto
1025 for (off
= hlen
; off
< tlen
; off
+= len
) {
1026 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
1029 V_ip6stat
.ip6s_odropped
++;
1032 m
->m_pkthdr
.rcvif
= NULL
;
1033 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1035 mnext
= &m
->m_nextpkt
;
1036 m
->m_data
+= max_linkhdr
;
1037 mhip6
= mtod(m
, struct ip6_hdr
*);
1039 m
->m_len
= sizeof(*mhip6
);
1040 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1042 V_ip6stat
.ip6s_odropped
++;
1045 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1046 if (off
+ len
>= tlen
)
1049 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1050 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1051 sizeof(*ip6f
) - sizeof(struct ip6_hdr
)));
1052 if ((m_frgpart
= m_copy(m0
, off
, len
)) == 0) {
1054 V_ip6stat
.ip6s_odropped
++;
1057 m_cat(m
, m_frgpart
);
1058 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1059 m
->m_pkthdr
.rcvif
= NULL
;
1060 ip6f
->ip6f_reserved
= 0;
1061 ip6f
->ip6f_ident
= id
;
1062 ip6f
->ip6f_nxt
= nextproto
;
1063 V_ip6stat
.ip6s_ofragments
++;
1064 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1067 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1071 * Remove leading garbages.
1077 for (m0
= m
; m
; m
= m0
) {
1081 /* Record statistics for this interface address. */
1083 ia
->ia_ifa
.if_opackets
++;
1084 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1086 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
1092 V_ip6stat
.ip6s_fragmented
++;
1095 if (ro
== &ip6route
&& ro
->ro_rt
) { /* brace necessary for RTFREE */
1097 } else if (ro_pmtu
== &ip6route
&& ro_pmtu
->ro_rt
) {
1098 RTFREE(ro_pmtu
->ro_rt
);
1108 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1109 m_freem(exthdrs
.ip6e_dest1
);
1110 m_freem(exthdrs
.ip6e_rthdr
);
1111 m_freem(exthdrs
.ip6e_dest2
);
1120 ip6_copyexthdr(struct mbuf
**mp
, caddr_t hdr
, int hlen
)
1124 if (hlen
> MCLBYTES
)
1125 return (ENOBUFS
); /* XXX */
1127 MGET(m
, M_DONTWAIT
, MT_DATA
);
1132 MCLGET(m
, M_DONTWAIT
);
1133 if ((m
->m_flags
& M_EXT
) == 0) {
1140 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1147 * Insert jumbo payload option.
1150 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
1156 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1159 * If there is no hop-by-hop options header, allocate new one.
1160 * If there is one but it doesn't have enough space to store the
1161 * jumbo payload option, allocate a cluster to store the whole options.
1162 * Otherwise, use it to store the options.
1164 if (exthdrs
->ip6e_hbh
== 0) {
1165 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1168 mopt
->m_len
= JUMBOOPTLEN
;
1169 optbuf
= mtod(mopt
, u_char
*);
1170 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1171 exthdrs
->ip6e_hbh
= mopt
;
1173 struct ip6_hbh
*hbh
;
1175 mopt
= exthdrs
->ip6e_hbh
;
1176 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1179 * - exthdrs->ip6e_hbh is not referenced from places
1180 * other than exthdrs.
1181 * - exthdrs->ip6e_hbh is not an mbuf chain.
1183 int oldoptlen
= mopt
->m_len
;
1187 * XXX: give up if the whole (new) hbh header does
1188 * not fit even in an mbuf cluster.
1190 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1194 * As a consequence, we must always prepare a cluster
1197 MGET(n
, M_DONTWAIT
, MT_DATA
);
1199 MCLGET(n
, M_DONTWAIT
);
1200 if ((n
->m_flags
& M_EXT
) == 0) {
1207 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1208 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1210 optbuf
= mtod(n
, caddr_t
) + oldoptlen
;
1212 mopt
= exthdrs
->ip6e_hbh
= n
;
1214 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1215 mopt
->m_len
+= JUMBOOPTLEN
;
1217 optbuf
[0] = IP6OPT_PADN
;
1221 * Adjust the header length according to the pad and
1222 * the jumbo payload option.
1224 hbh
= mtod(mopt
, struct ip6_hbh
*);
1225 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1228 /* fill in the option. */
1229 optbuf
[2] = IP6OPT_JUMBO
;
1231 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1232 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1234 /* finally, adjust the packet header length */
1235 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1242 * Insert fragment header and copy unfragmentable header portions.
1245 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
1246 struct ip6_frag
**frghdrp
)
1248 struct mbuf
*n
, *mlast
;
1250 if (hlen
> sizeof(struct ip6_hdr
)) {
1251 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1252 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
1259 /* Search for the last mbuf of unfragmentable part. */
1260 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1263 if ((mlast
->m_flags
& M_EXT
) == 0 &&
1264 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1265 /* use the trailing space of the last mbuf for the fragment hdr */
1266 *frghdrp
= (struct ip6_frag
*)(mtod(mlast
, caddr_t
) +
1268 mlast
->m_len
+= sizeof(struct ip6_frag
);
1269 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1271 /* allocate a new mbuf for the fragment header */
1274 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1277 mfrg
->m_len
= sizeof(struct ip6_frag
);
1278 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1279 mlast
->m_next
= mfrg
;
1286 ip6_getpmtu(struct route_in6
*ro_pmtu
, struct route_in6
*ro
,
1287 struct ifnet
*ifp
, struct in6_addr
*dst
, u_long
*mtup
,
1294 if (ro_pmtu
!= ro
) {
1295 /* The first hop and the final destination may differ. */
1296 struct sockaddr_in6
*sa6_dst
=
1297 (struct sockaddr_in6
*)&ro_pmtu
->ro_dst
;
1298 if (ro_pmtu
->ro_rt
&&
1299 ((ro_pmtu
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
1300 !IN6_ARE_ADDR_EQUAL(&sa6_dst
->sin6_addr
, dst
))) {
1301 RTFREE(ro_pmtu
->ro_rt
);
1302 ro_pmtu
->ro_rt
= (struct rtentry
*)NULL
;
1304 if (ro_pmtu
->ro_rt
== NULL
) {
1305 bzero(sa6_dst
, sizeof(*sa6_dst
));
1306 sa6_dst
->sin6_family
= AF_INET6
;
1307 sa6_dst
->sin6_len
= sizeof(struct sockaddr_in6
);
1308 sa6_dst
->sin6_addr
= *dst
;
1310 rtalloc((struct route
*)ro_pmtu
);
1313 if (ro_pmtu
->ro_rt
) {
1315 struct in_conninfo inc
;
1317 bzero(&inc
, sizeof(inc
));
1318 inc
.inc_flags
= 1; /* IPv6 */
1319 inc
.inc6_faddr
= *dst
;
1322 ifp
= ro_pmtu
->ro_rt
->rt_ifp
;
1323 ifmtu
= IN6_LINKMTU(ifp
);
1324 mtu
= tcp_hc_getmtu(&inc
);
1326 mtu
= min(mtu
, ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
);
1328 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
1331 else if (mtu
< IPV6_MMTU
) {
1333 * RFC2460 section 5, last paragraph:
1334 * if we record ICMPv6 too big message with
1335 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1336 * or smaller, with framgent header attached.
1337 * (fragment header is needed regardless from the
1338 * packet size, for translators to identify packets)
1342 } else if (mtu
> ifmtu
) {
1344 * The MTU on the route is larger than the MTU on
1345 * the interface! This shouldn't happen, unless the
1346 * MTU of the interface has been changed after the
1347 * interface was brought up. Change the MTU in the
1348 * route to match the interface MTU (as long as the
1349 * field isn't locked).
1352 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
;
1355 mtu
= IN6_LINKMTU(ifp
);
1357 error
= EHOSTUNREACH
; /* XXX */
1361 *alwaysfragp
= alwaysfrag
;
1366 * IP6 socket option processing.
1369 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1371 int optdatalen
, uproto
;
1373 struct inpcb
*in6p
= sotoinpcb(so
);
1375 int level
, op
, optname
;
1379 level
= sopt
->sopt_level
;
1380 op
= sopt
->sopt_dir
;
1381 optname
= sopt
->sopt_name
;
1382 optlen
= sopt
->sopt_valsize
;
1386 uproto
= (int)so
->so_proto
->pr_protocol
;
1388 if (level
== IPPROTO_IPV6
) {
1393 case IPV6_2292PKTOPTIONS
:
1394 #ifdef IPV6_PKTOPTIONS
1395 case IPV6_PKTOPTIONS
:
1400 error
= soopt_getm(sopt
, &m
); /* XXX */
1403 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1406 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1408 m_freem(m
); /* XXX */
1413 * Use of some Hop-by-Hop options or some
1414 * Destination options, might require special
1415 * privilege. That is, normal applications
1416 * (without special privilege) might be forbidden
1417 * from setting certain options in outgoing packets,
1418 * and might never see certain options in received
1419 * packets. [RFC 2292 Section 6]
1420 * KAME specific note:
1421 * KAME prevents non-privileged users from sending or
1422 * receiving ANY hbh/dst options in order to avoid
1423 * overhead of parsing options in the kernel.
1425 case IPV6_RECVHOPOPTS
:
1426 case IPV6_RECVDSTOPTS
:
1427 case IPV6_RECVRTHDRDSTOPTS
:
1429 error
= priv_check(td
,
1430 PRIV_NETINET_SETHDROPTS
);
1435 case IPV6_UNICAST_HOPS
:
1439 case IPV6_RECVPKTINFO
:
1440 case IPV6_RECVHOPLIMIT
:
1441 case IPV6_RECVRTHDR
:
1442 case IPV6_RECVPATHMTU
:
1443 case IPV6_RECVTCLASS
:
1445 case IPV6_AUTOFLOWLABEL
:
1446 if (optlen
!= sizeof(int)) {
1450 error
= sooptcopyin(sopt
, &optval
,
1451 sizeof optval
, sizeof optval
);
1456 case IPV6_UNICAST_HOPS
:
1457 if (optval
< -1 || optval
>= 256)
1460 /* -1 = kernel default */
1461 in6p
->in6p_hops
= optval
;
1462 if ((in6p
->in6p_vflag
&
1464 in6p
->inp_ip_ttl
= optval
;
1467 #define OPTSET(bit) \
1470 in6p->in6p_flags |= (bit); \
1472 in6p->in6p_flags &= ~(bit); \
1473 } while (/*CONSTCOND*/ 0)
1474 #define OPTSET2292(bit) \
1476 in6p->in6p_flags |= IN6P_RFC2292; \
1478 in6p->in6p_flags |= (bit); \
1480 in6p->in6p_flags &= ~(bit); \
1481 } while (/*CONSTCOND*/ 0)
1482 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1484 case IPV6_RECVPKTINFO
:
1485 /* cannot mix with RFC2292 */
1486 if (OPTBIT(IN6P_RFC2292
)) {
1490 OPTSET(IN6P_PKTINFO
);
1495 struct ip6_pktopts
**optp
;
1497 /* cannot mix with RFC2292 */
1498 if (OPTBIT(IN6P_RFC2292
)) {
1502 optp
= &in6p
->in6p_outputopts
;
1503 error
= ip6_pcbopt(IPV6_HOPLIMIT
,
1504 (u_char
*)&optval
, sizeof(optval
),
1505 optp
, (td
!= NULL
) ? td
->td_ucred
:
1510 case IPV6_RECVHOPLIMIT
:
1511 /* cannot mix with RFC2292 */
1512 if (OPTBIT(IN6P_RFC2292
)) {
1516 OPTSET(IN6P_HOPLIMIT
);
1519 case IPV6_RECVHOPOPTS
:
1520 /* cannot mix with RFC2292 */
1521 if (OPTBIT(IN6P_RFC2292
)) {
1525 OPTSET(IN6P_HOPOPTS
);
1528 case IPV6_RECVDSTOPTS
:
1529 /* cannot mix with RFC2292 */
1530 if (OPTBIT(IN6P_RFC2292
)) {
1534 OPTSET(IN6P_DSTOPTS
);
1537 case IPV6_RECVRTHDRDSTOPTS
:
1538 /* cannot mix with RFC2292 */
1539 if (OPTBIT(IN6P_RFC2292
)) {
1543 OPTSET(IN6P_RTHDRDSTOPTS
);
1546 case IPV6_RECVRTHDR
:
1547 /* cannot mix with RFC2292 */
1548 if (OPTBIT(IN6P_RFC2292
)) {
1559 case IPV6_RECVPATHMTU
:
1561 * We ignore this option for TCP
1563 * (RFC3542 leaves this case
1566 if (uproto
!= IPPROTO_TCP
)
1572 * make setsockopt(IPV6_V6ONLY)
1573 * available only prior to bind(2).
1574 * see ipng mailing list, Jun 22 2001.
1576 if (in6p
->in6p_lport
||
1577 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
)) {
1581 OPTSET(IN6P_IPV6_V6ONLY
);
1583 in6p
->in6p_vflag
&= ~INP_IPV4
;
1585 in6p
->in6p_vflag
|= INP_IPV4
;
1587 case IPV6_RECVTCLASS
:
1588 /* cannot mix with RFC2292 XXX */
1589 if (OPTBIT(IN6P_RFC2292
)) {
1593 OPTSET(IN6P_TCLASS
);
1595 case IPV6_AUTOFLOWLABEL
:
1596 OPTSET(IN6P_AUTOFLOWLABEL
);
1604 case IPV6_USE_MIN_MTU
:
1605 case IPV6_PREFER_TEMPADDR
:
1606 if (optlen
!= sizeof(optval
)) {
1610 error
= sooptcopyin(sopt
, &optval
,
1611 sizeof optval
, sizeof optval
);
1615 struct ip6_pktopts
**optp
;
1616 optp
= &in6p
->in6p_outputopts
;
1617 error
= ip6_pcbopt(optname
,
1618 (u_char
*)&optval
, sizeof(optval
),
1619 optp
, (td
!= NULL
) ? td
->td_ucred
:
1624 case IPV6_2292PKTINFO
:
1625 case IPV6_2292HOPLIMIT
:
1626 case IPV6_2292HOPOPTS
:
1627 case IPV6_2292DSTOPTS
:
1628 case IPV6_2292RTHDR
:
1630 if (optlen
!= sizeof(int)) {
1634 error
= sooptcopyin(sopt
, &optval
,
1635 sizeof optval
, sizeof optval
);
1639 case IPV6_2292PKTINFO
:
1640 OPTSET2292(IN6P_PKTINFO
);
1642 case IPV6_2292HOPLIMIT
:
1643 OPTSET2292(IN6P_HOPLIMIT
);
1645 case IPV6_2292HOPOPTS
:
1647 * Check super-user privilege.
1648 * See comments for IPV6_RECVHOPOPTS.
1651 error
= priv_check(td
,
1652 PRIV_NETINET_SETHDROPTS
);
1656 OPTSET2292(IN6P_HOPOPTS
);
1658 case IPV6_2292DSTOPTS
:
1660 error
= priv_check(td
,
1661 PRIV_NETINET_SETHDROPTS
);
1665 OPTSET2292(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1667 case IPV6_2292RTHDR
:
1668 OPTSET2292(IN6P_RTHDR
);
1676 case IPV6_RTHDRDSTOPTS
:
1679 /* new advanced API (RFC3542) */
1681 u_char optbuf_storage
[MCLBYTES
];
1683 struct ip6_pktopts
**optp
;
1685 /* cannot mix with RFC2292 */
1686 if (OPTBIT(IN6P_RFC2292
)) {
1692 * We only ensure valsize is not too large
1693 * here. Further validation will be done
1696 error
= sooptcopyin(sopt
, optbuf_storage
,
1697 sizeof(optbuf_storage
), 0);
1700 optlen
= sopt
->sopt_valsize
;
1701 optbuf
= optbuf_storage
;
1702 optp
= &in6p
->in6p_outputopts
;
1703 error
= ip6_pcbopt(optname
, optbuf
, optlen
,
1704 optp
, (td
!= NULL
) ? td
->td_ucred
: NULL
,
1710 case IPV6_MULTICAST_IF
:
1711 case IPV6_MULTICAST_HOPS
:
1712 case IPV6_MULTICAST_LOOP
:
1713 case IPV6_JOIN_GROUP
:
1714 case IPV6_LEAVE_GROUP
:
1716 if (sopt
->sopt_valsize
> MLEN
) {
1726 if (sopt
->sopt_valsize
> MCLBYTES
) {
1731 MGET(m
, sopt
->sopt_td
? M_WAIT
: M_DONTWAIT
, MT_DATA
);
1736 if (sopt
->sopt_valsize
> MLEN
) {
1737 MCLGET(m
, sopt
->sopt_td
? M_WAIT
: M_DONTWAIT
);
1738 if ((m
->m_flags
& M_EXT
) == 0) {
1744 m
->m_len
= sopt
->sopt_valsize
;
1745 error
= sooptcopyin(sopt
, mtod(m
, char *),
1746 m
->m_len
, m
->m_len
);
1751 error
= ip6_setmoptions(sopt
->sopt_name
,
1752 &in6p
->in6p_moptions
,
1758 case IPV6_PORTRANGE
:
1759 error
= sooptcopyin(sopt
, &optval
,
1760 sizeof optval
, sizeof optval
);
1765 case IPV6_PORTRANGE_DEFAULT
:
1766 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1767 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1770 case IPV6_PORTRANGE_HIGH
:
1771 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1772 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1775 case IPV6_PORTRANGE_LOW
:
1776 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1777 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1787 case IPV6_IPSEC_POLICY
:
1792 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1794 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1796 req
= mtod(m
, caddr_t
);
1797 error
= ipsec6_set_policy(in6p
, optname
, req
,
1798 m
->m_len
, (sopt
->sopt_td
!= NULL
) ?
1799 sopt
->sopt_td
->td_ucred
: NULL
);
1806 error
= ENOPROTOOPT
;
1814 case IPV6_2292PKTOPTIONS
:
1815 #ifdef IPV6_PKTOPTIONS
1816 case IPV6_PKTOPTIONS
:
1819 * RFC3542 (effectively) deprecated the
1820 * semantics of the 2292-style pktoptions.
1821 * Since it was not reliable in nature (i.e.,
1822 * applications had to expect the lack of some
1823 * information after all), it would make sense
1824 * to simplify this part by always returning
1827 sopt
->sopt_valsize
= 0;
1830 case IPV6_RECVHOPOPTS
:
1831 case IPV6_RECVDSTOPTS
:
1832 case IPV6_RECVRTHDRDSTOPTS
:
1833 case IPV6_UNICAST_HOPS
:
1834 case IPV6_RECVPKTINFO
:
1835 case IPV6_RECVHOPLIMIT
:
1836 case IPV6_RECVRTHDR
:
1837 case IPV6_RECVPATHMTU
:
1841 case IPV6_PORTRANGE
:
1842 case IPV6_RECVTCLASS
:
1843 case IPV6_AUTOFLOWLABEL
:
1846 case IPV6_RECVHOPOPTS
:
1847 optval
= OPTBIT(IN6P_HOPOPTS
);
1850 case IPV6_RECVDSTOPTS
:
1851 optval
= OPTBIT(IN6P_DSTOPTS
);
1854 case IPV6_RECVRTHDRDSTOPTS
:
1855 optval
= OPTBIT(IN6P_RTHDRDSTOPTS
);
1858 case IPV6_UNICAST_HOPS
:
1859 optval
= in6p
->in6p_hops
;
1862 case IPV6_RECVPKTINFO
:
1863 optval
= OPTBIT(IN6P_PKTINFO
);
1866 case IPV6_RECVHOPLIMIT
:
1867 optval
= OPTBIT(IN6P_HOPLIMIT
);
1870 case IPV6_RECVRTHDR
:
1871 optval
= OPTBIT(IN6P_RTHDR
);
1874 case IPV6_RECVPATHMTU
:
1875 optval
= OPTBIT(IN6P_MTU
);
1879 optval
= OPTBIT(IN6P_FAITH
);
1883 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1886 case IPV6_PORTRANGE
:
1889 flags
= in6p
->in6p_flags
;
1890 if (flags
& IN6P_HIGHPORT
)
1891 optval
= IPV6_PORTRANGE_HIGH
;
1892 else if (flags
& IN6P_LOWPORT
)
1893 optval
= IPV6_PORTRANGE_LOW
;
1898 case IPV6_RECVTCLASS
:
1899 optval
= OPTBIT(IN6P_TCLASS
);
1902 case IPV6_AUTOFLOWLABEL
:
1903 optval
= OPTBIT(IN6P_AUTOFLOWLABEL
);
1908 error
= sooptcopyout(sopt
, &optval
,
1915 struct ip6_mtuinfo mtuinfo
;
1916 struct route_in6 sro
;
1918 bzero(&sro
, sizeof(sro
));
1920 if (!(so
->so_state
& SS_ISCONNECTED
))
1923 * XXX: we dot not consider the case of source
1924 * routing, or optional information to specify
1925 * the outgoing interface.
1927 error
= ip6_getpmtu(&sro
, NULL
, NULL
,
1928 &in6p
->in6p_faddr
, &pmtu
, NULL
);
1933 if (pmtu
> IPV6_MAXPACKET
)
1934 pmtu
= IPV6_MAXPACKET
;
1936 bzero(&mtuinfo
, sizeof(mtuinfo
));
1937 mtuinfo
.ip6m_mtu
= (u_int32_t
)pmtu
;
1938 optdata
= (void *)&mtuinfo
;
1939 optdatalen
= sizeof(mtuinfo
);
1940 error
= sooptcopyout(sopt
, optdata
,
1945 case IPV6_2292PKTINFO
:
1946 case IPV6_2292HOPLIMIT
:
1947 case IPV6_2292HOPOPTS
:
1948 case IPV6_2292RTHDR
:
1949 case IPV6_2292DSTOPTS
:
1951 case IPV6_2292PKTINFO
:
1952 optval
= OPTBIT(IN6P_PKTINFO
);
1954 case IPV6_2292HOPLIMIT
:
1955 optval
= OPTBIT(IN6P_HOPLIMIT
);
1957 case IPV6_2292HOPOPTS
:
1958 optval
= OPTBIT(IN6P_HOPOPTS
);
1960 case IPV6_2292RTHDR
:
1961 optval
= OPTBIT(IN6P_RTHDR
);
1963 case IPV6_2292DSTOPTS
:
1964 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1967 error
= sooptcopyout(sopt
, &optval
,
1974 case IPV6_RTHDRDSTOPTS
:
1978 case IPV6_USE_MIN_MTU
:
1979 case IPV6_PREFER_TEMPADDR
:
1980 error
= ip6_getpcbopt(in6p
->in6p_outputopts
,
1984 case IPV6_MULTICAST_IF
:
1985 case IPV6_MULTICAST_HOPS
:
1986 case IPV6_MULTICAST_LOOP
:
1987 case IPV6_JOIN_GROUP
:
1988 case IPV6_LEAVE_GROUP
:
1991 error
= ip6_getmoptions(sopt
->sopt_name
,
1992 in6p
->in6p_moptions
, &m
);
1994 error
= sooptcopyout(sopt
,
1995 mtod(m
, char *), m
->m_len
);
2001 case IPV6_IPSEC_POLICY
:
2005 struct mbuf
*m
= NULL
;
2006 struct mbuf
**mp
= &m
;
2007 size_t ovalsize
= sopt
->sopt_valsize
;
2008 caddr_t oval
= (caddr_t
)sopt
->sopt_val
;
2010 error
= soopt_getm(sopt
, &m
); /* XXX */
2013 error
= soopt_mcopyin(sopt
, m
); /* XXX */
2016 sopt
->sopt_valsize
= ovalsize
;
2017 sopt
->sopt_val
= oval
;
2019 req
= mtod(m
, caddr_t
);
2022 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
2024 error
= soopt_mcopyout(sopt
, m
); /* XXX */
2025 if (error
== 0 && m
)
2032 error
= ENOPROTOOPT
;
2037 } else { /* level != IPPROTO_IPV6 */
2044 ip6_raw_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
2046 int error
= 0, optval
, optlen
;
2047 const int icmp6off
= offsetof(struct icmp6_hdr
, icmp6_cksum
);
2048 struct in6pcb
*in6p
= sotoin6pcb(so
);
2049 int level
, op
, optname
;
2051 level
= sopt
->sopt_level
;
2052 op
= sopt
->sopt_dir
;
2053 optname
= sopt
->sopt_name
;
2054 optlen
= sopt
->sopt_valsize
;
2056 if (level
!= IPPROTO_IPV6
) {
2063 * For ICMPv6 sockets, no modification allowed for checksum
2064 * offset, permit "no change" values to help existing apps.
2066 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2067 * for an ICMPv6 socket will fail."
2068 * The current behavior does not meet RFC3542.
2072 if (optlen
!= sizeof(int)) {
2076 error
= sooptcopyin(sopt
, &optval
, sizeof(optval
),
2080 if ((optval
% 2) != 0) {
2081 /* the API assumes even offset values */
2083 } else if (so
->so_proto
->pr_protocol
==
2085 if (optval
!= icmp6off
)
2088 in6p
->in6p_cksum
= optval
;
2092 if (so
->so_proto
->pr_protocol
== IPPROTO_ICMPV6
)
2095 optval
= in6p
->in6p_cksum
;
2097 error
= sooptcopyout(sopt
, &optval
, sizeof(optval
));
2107 error
= ENOPROTOOPT
;
2115 * Set up IP6 options in pcb for insertion in output packets or
2116 * specifying behavior of outgoing packets.
2119 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
,
2120 struct socket
*so
, struct sockopt
*sopt
)
2122 struct ip6_pktopts
*opt
= *pktopt
;
2124 struct thread
*td
= sopt
->sopt_td
;
2126 /* turn off any old options. */
2129 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
2130 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
2131 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2132 printf("ip6_pcbopts: all specified options are cleared.\n");
2134 ip6_clearpktopts(opt
, -1);
2136 opt
= malloc(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
2139 if (!m
|| m
->m_len
== 0) {
2141 * Only turning off any previous options, regardless of
2142 * whether the opt is just created or given.
2144 free(opt
, M_IP6OPT
);
2148 /* set options specified by user. */
2149 if ((error
= ip6_setpktopts(m
, opt
, NULL
, (td
!= NULL
) ?
2150 td
->td_ucred
: NULL
, so
->so_proto
->pr_protocol
)) != 0) {
2151 ip6_clearpktopts(opt
, -1); /* XXX: discard all options */
2152 free(opt
, M_IP6OPT
);
2160 * initialize ip6_pktopts. beware that there are non-zero default values in
2164 ip6_initpktopts(struct ip6_pktopts
*opt
)
2167 bzero(opt
, sizeof(*opt
));
2168 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
2169 opt
->ip6po_tclass
= -1; /* -1 means default traffic class */
2170 opt
->ip6po_minmtu
= IP6PO_MINMTU_MCASTONLY
;
2171 opt
->ip6po_prefer_tempaddr
= IP6PO_TEMPADDR_SYSTEM
;
2175 ip6_pcbopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
**pktopt
,
2176 struct ucred
*cred
, int uproto
)
2178 struct ip6_pktopts
*opt
;
2180 if (*pktopt
== NULL
) {
2181 *pktopt
= malloc(sizeof(struct ip6_pktopts
), M_IP6OPT
,
2183 ip6_initpktopts(*pktopt
);
2187 return (ip6_setpktopt(optname
, buf
, len
, opt
, cred
, 1, 0, uproto
));
2191 ip6_getpcbopt(struct ip6_pktopts
*pktopt
, int optname
, struct sockopt
*sopt
)
2193 void *optdata
= NULL
;
2195 struct ip6_ext
*ip6e
;
2197 struct in6_pktinfo null_pktinfo
;
2198 int deftclass
= 0, on
;
2199 int defminmtu
= IP6PO_MINMTU_MCASTONLY
;
2200 int defpreftemp
= IP6PO_TEMPADDR_SYSTEM
;
2204 if (pktopt
&& pktopt
->ip6po_pktinfo
)
2205 optdata
= (void *)pktopt
->ip6po_pktinfo
;
2207 /* XXX: we don't have to do this every time... */
2208 bzero(&null_pktinfo
, sizeof(null_pktinfo
));
2209 optdata
= (void *)&null_pktinfo
;
2211 optdatalen
= sizeof(struct in6_pktinfo
);
2214 if (pktopt
&& pktopt
->ip6po_tclass
>= 0)
2215 optdata
= (void *)&pktopt
->ip6po_tclass
;
2217 optdata
= (void *)&deftclass
;
2218 optdatalen
= sizeof(int);
2221 if (pktopt
&& pktopt
->ip6po_hbh
) {
2222 optdata
= (void *)pktopt
->ip6po_hbh
;
2223 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_hbh
;
2224 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2228 if (pktopt
&& pktopt
->ip6po_rthdr
) {
2229 optdata
= (void *)pktopt
->ip6po_rthdr
;
2230 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_rthdr
;
2231 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2234 case IPV6_RTHDRDSTOPTS
:
2235 if (pktopt
&& pktopt
->ip6po_dest1
) {
2236 optdata
= (void *)pktopt
->ip6po_dest1
;
2237 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest1
;
2238 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2242 if (pktopt
&& pktopt
->ip6po_dest2
) {
2243 optdata
= (void *)pktopt
->ip6po_dest2
;
2244 ip6e
= (struct ip6_ext
*)pktopt
->ip6po_dest2
;
2245 optdatalen
= (ip6e
->ip6e_len
+ 1) << 3;
2249 if (pktopt
&& pktopt
->ip6po_nexthop
) {
2250 optdata
= (void *)pktopt
->ip6po_nexthop
;
2251 optdatalen
= pktopt
->ip6po_nexthop
->sa_len
;
2254 case IPV6_USE_MIN_MTU
:
2256 optdata
= (void *)&pktopt
->ip6po_minmtu
;
2258 optdata
= (void *)&defminmtu
;
2259 optdatalen
= sizeof(int);
2262 if (pktopt
&& ((pktopt
->ip6po_flags
) & IP6PO_DONTFRAG
))
2266 optdata
= (void *)&on
;
2267 optdatalen
= sizeof(on
);
2269 case IPV6_PREFER_TEMPADDR
:
2271 optdata
= (void *)&pktopt
->ip6po_prefer_tempaddr
;
2273 optdata
= (void *)&defpreftemp
;
2274 optdatalen
= sizeof(int);
2276 default: /* should not happen */
2278 panic("ip6_getpcbopt: unexpected option\n");
2280 return (ENOPROTOOPT
);
2283 error
= sooptcopyout(sopt
, optdata
, optdatalen
);
2289 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int optname
)
2294 if (optname
== -1 || optname
== IPV6_PKTINFO
) {
2295 if (pktopt
->ip6po_pktinfo
)
2296 free(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
2297 pktopt
->ip6po_pktinfo
= NULL
;
2299 if (optname
== -1 || optname
== IPV6_HOPLIMIT
)
2300 pktopt
->ip6po_hlim
= -1;
2301 if (optname
== -1 || optname
== IPV6_TCLASS
)
2302 pktopt
->ip6po_tclass
= -1;
2303 if (optname
== -1 || optname
== IPV6_NEXTHOP
) {
2304 if (pktopt
->ip6po_nextroute
.ro_rt
) {
2305 RTFREE(pktopt
->ip6po_nextroute
.ro_rt
);
2306 pktopt
->ip6po_nextroute
.ro_rt
= NULL
;
2308 if (pktopt
->ip6po_nexthop
)
2309 free(pktopt
->ip6po_nexthop
, M_IP6OPT
);
2310 pktopt
->ip6po_nexthop
= NULL
;
2312 if (optname
== -1 || optname
== IPV6_HOPOPTS
) {
2313 if (pktopt
->ip6po_hbh
)
2314 free(pktopt
->ip6po_hbh
, M_IP6OPT
);
2315 pktopt
->ip6po_hbh
= NULL
;
2317 if (optname
== -1 || optname
== IPV6_RTHDRDSTOPTS
) {
2318 if (pktopt
->ip6po_dest1
)
2319 free(pktopt
->ip6po_dest1
, M_IP6OPT
);
2320 pktopt
->ip6po_dest1
= NULL
;
2322 if (optname
== -1 || optname
== IPV6_RTHDR
) {
2323 if (pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
2324 free(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
2325 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
2326 if (pktopt
->ip6po_route
.ro_rt
) {
2327 RTFREE(pktopt
->ip6po_route
.ro_rt
);
2328 pktopt
->ip6po_route
.ro_rt
= NULL
;
2331 if (optname
== -1 || optname
== IPV6_DSTOPTS
) {
2332 if (pktopt
->ip6po_dest2
)
2333 free(pktopt
->ip6po_dest2
, M_IP6OPT
);
2334 pktopt
->ip6po_dest2
= NULL
;
2338 #define PKTOPT_EXTHDRCPY(type) \
2341 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2342 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2343 if (dst->type == NULL && canwait == M_NOWAIT)\
2345 bcopy(src->type, dst->type, hlen);\
2347 } while (/*CONSTCOND*/ 0)
2350 copypktopts(struct ip6_pktopts
*dst
, struct ip6_pktopts
*src
, int canwait
)
2352 if (dst
== NULL
|| src
== NULL
) {
2353 printf("ip6_clearpktopts: invalid argument\n");
2357 dst
->ip6po_hlim
= src
->ip6po_hlim
;
2358 dst
->ip6po_tclass
= src
->ip6po_tclass
;
2359 dst
->ip6po_flags
= src
->ip6po_flags
;
2360 if (src
->ip6po_pktinfo
) {
2361 dst
->ip6po_pktinfo
= malloc(sizeof(*dst
->ip6po_pktinfo
),
2363 if (dst
->ip6po_pktinfo
== NULL
)
2365 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
2367 if (src
->ip6po_nexthop
) {
2368 dst
->ip6po_nexthop
= malloc(src
->ip6po_nexthop
->sa_len
,
2370 if (dst
->ip6po_nexthop
== NULL
)
2372 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
2373 src
->ip6po_nexthop
->sa_len
);
2375 PKTOPT_EXTHDRCPY(ip6po_hbh
);
2376 PKTOPT_EXTHDRCPY(ip6po_dest1
);
2377 PKTOPT_EXTHDRCPY(ip6po_dest2
);
2378 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
2382 ip6_clearpktopts(dst
, -1);
2385 #undef PKTOPT_EXTHDRCPY
2387 struct ip6_pktopts
*
2388 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
2391 struct ip6_pktopts
*dst
;
2393 dst
= malloc(sizeof(*dst
), M_IP6OPT
, canwait
);
2396 ip6_initpktopts(dst
);
2398 if ((error
= copypktopts(dst
, src
, canwait
)) != 0) {
2399 free(dst
, M_IP6OPT
);
2407 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
2412 ip6_clearpktopts(pktopt
, -1);
2414 free(pktopt
, M_IP6OPT
);
2418 * Set the IP6 multicast options in response to user setsockopt().
2421 ip6_setmoptions(int optname
, struct ip6_moptions
**im6op
, struct mbuf
*m
)
2424 u_int loop
, ifindex
;
2425 struct ipv6_mreq
*mreq
;
2427 struct ip6_moptions
*im6o
= *im6op
;
2428 struct route_in6 ro
;
2429 struct in6_multi_mship
*imm
;
2433 * No multicast option buffer attached to the pcb;
2434 * allocate one and initialize to default values.
2436 im6o
= (struct ip6_moptions
*)
2437 malloc(sizeof(*im6o
), M_IP6MOPTS
, M_WAITOK
);
2442 im6o
->im6o_multicast_ifp
= NULL
;
2443 im6o
->im6o_multicast_hlim
= V_ip6_defmcasthlim
;
2444 im6o
->im6o_multicast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
2445 LIST_INIT(&im6o
->im6o_memberships
);
2450 case IPV6_MULTICAST_IF
:
2452 * Select the interface for outgoing multicast packets.
2454 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
2458 bcopy(mtod(m
, u_int
*), &ifindex
, sizeof(ifindex
));
2459 if (ifindex
< 0 || V_if_index
< ifindex
) {
2460 error
= ENXIO
; /* XXX EINVAL? */
2463 ifp
= ifnet_byindex(ifindex
);
2464 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2465 error
= EADDRNOTAVAIL
;
2468 im6o
->im6o_multicast_ifp
= ifp
;
2471 case IPV6_MULTICAST_HOPS
:
2474 * Set the IP6 hoplimit for outgoing multicast packets.
2477 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
2481 bcopy(mtod(m
, u_int
*), &optval
, sizeof(optval
));
2482 if (optval
< -1 || optval
>= 256)
2484 else if (optval
== -1)
2485 im6o
->im6o_multicast_hlim
= V_ip6_defmcasthlim
;
2487 im6o
->im6o_multicast_hlim
= optval
;
2491 case IPV6_MULTICAST_LOOP
:
2493 * Set the loopback flag for outgoing multicast packets.
2494 * Must be zero or one.
2496 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
2500 bcopy(mtod(m
, u_int
*), &loop
, sizeof(loop
));
2505 im6o
->im6o_multicast_loop
= loop
;
2508 case IPV6_JOIN_GROUP
:
2510 * Add a multicast group membership.
2511 * Group must be a valid IP6 multicast address.
2513 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2517 mreq
= mtod(m
, struct ipv6_mreq
*);
2519 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2521 * We use the unspecified address to specify to accept
2522 * all multicast addresses. Only super user is allowed
2525 /* XXX-BZ might need a better PRIV_NETINET_x for this */
2526 error
= priv_check(curthread
, PRIV_NETINET_MROUTE
);
2529 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2535 * If no interface was explicitly specified, choose an
2536 * appropriate one according to the given multicast address.
2538 if (mreq
->ipv6mr_interface
== 0) {
2539 struct sockaddr_in6
*dst
;
2542 * Look up the routing table for the
2543 * address, and choose the outgoing interface.
2544 * XXX: is it a good approach?
2547 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
2548 bzero(dst
, sizeof(*dst
));
2549 dst
->sin6_family
= AF_INET6
;
2550 dst
->sin6_len
= sizeof(*dst
);
2551 dst
->sin6_addr
= mreq
->ipv6mr_multiaddr
;
2552 rtalloc((struct route
*)&ro
);
2553 if (ro
.ro_rt
== NULL
) {
2554 error
= EADDRNOTAVAIL
;
2557 ifp
= ro
.ro_rt
->rt_ifp
;
2561 * If the interface is specified, validate it.
2563 if (mreq
->ipv6mr_interface
< 0 ||
2564 V_if_index
< mreq
->ipv6mr_interface
) {
2565 error
= ENXIO
; /* XXX EINVAL? */
2568 ifp
= ifnet_byindex(mreq
->ipv6mr_interface
);
2570 error
= ENXIO
; /* XXX EINVAL? */
2576 * See if we found an interface, and confirm that it
2577 * supports multicast
2579 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2580 error
= EADDRNOTAVAIL
;
2584 if (in6_setscope(&mreq
->ipv6mr_multiaddr
, ifp
, NULL
)) {
2585 error
= EADDRNOTAVAIL
; /* XXX: should not happen */
2590 * See if the membership already exists.
2592 for (imm
= im6o
->im6o_memberships
.lh_first
;
2593 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2594 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2595 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2596 &mreq
->ipv6mr_multiaddr
))
2603 * Everything looks good; add a new record to the multicast
2604 * address list for the given interface.
2606 imm
= in6_joingroup(ifp
, &mreq
->ipv6mr_multiaddr
, &error
, 0);
2609 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2612 case IPV6_LEAVE_GROUP
:
2614 * Drop a multicast group membership.
2615 * Group must be a valid IP6 multicast address.
2617 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2621 mreq
= mtod(m
, struct ipv6_mreq
*);
2624 * If an interface address was specified, get a pointer
2625 * to its ifnet structure.
2627 if (mreq
->ipv6mr_interface
< 0 ||
2628 V_if_index
< mreq
->ipv6mr_interface
) {
2629 error
= ENXIO
; /* XXX EINVAL? */
2632 if (mreq
->ipv6mr_interface
== 0)
2635 ifp
= ifnet_byindex(mreq
->ipv6mr_interface
);
2637 /* Fill in the scope zone ID */
2639 if (in6_setscope(&mreq
->ipv6mr_multiaddr
, ifp
, NULL
)) {
2640 /* XXX: should not happen */
2641 error
= EADDRNOTAVAIL
;
2644 } else if (mreq
->ipv6mr_interface
!= 0) {
2646 * This case happens when the (positive) index is in
2647 * the valid range, but the corresponding interface has
2648 * been detached dynamically (XXX).
2650 error
= EADDRNOTAVAIL
;
2652 } else { /* ipv6mr_interface == 0 */
2653 struct sockaddr_in6 sa6_mc
;
2656 * The API spec says as follows:
2657 * If the interface index is specified as 0, the
2658 * system may choose a multicast group membership to
2659 * drop by matching the multicast address only.
2660 * On the other hand, we cannot disambiguate the scope
2661 * zone unless an interface is provided. Thus, we
2662 * check if there's ambiguity with the default scope
2663 * zone as the last resort.
2665 bzero(&sa6_mc
, sizeof(sa6_mc
));
2666 sa6_mc
.sin6_family
= AF_INET6
;
2667 sa6_mc
.sin6_len
= sizeof(sa6_mc
);
2668 sa6_mc
.sin6_addr
= mreq
->ipv6mr_multiaddr
;
2669 error
= sa6_embedscope(&sa6_mc
, V_ip6_use_defzone
);
2672 mreq
->ipv6mr_multiaddr
= sa6_mc
.sin6_addr
;
2676 * Find the membership in the membership list.
2678 for (imm
= im6o
->im6o_memberships
.lh_first
;
2679 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2680 if ((ifp
== NULL
|| imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2681 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2682 &mreq
->ipv6mr_multiaddr
))
2686 /* Unable to resolve interface */
2687 error
= EADDRNOTAVAIL
;
2691 * Give up the multicast address record to which the
2692 * membership points.
2694 LIST_REMOVE(imm
, i6mm_chain
);
2695 in6_delmulti(imm
->i6mm_maddr
);
2696 free(imm
, M_IP6MADDR
);
2705 * If all options have default values, no need to keep the mbuf.
2707 if (im6o
->im6o_multicast_ifp
== NULL
&&
2708 im6o
->im6o_multicast_hlim
== V_ip6_defmcasthlim
&&
2709 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2710 im6o
->im6o_memberships
.lh_first
== NULL
) {
2711 free(*im6op
, M_IP6MOPTS
);
2719 * Return the IP6 multicast options in response to user getsockopt().
2722 ip6_getmoptions(int optname
, struct ip6_moptions
*im6o
, struct mbuf
**mp
)
2724 u_int
*hlim
, *loop
, *ifindex
;
2726 *mp
= m_get(M_WAIT
, MT_HEADER
); /* XXX */
2730 case IPV6_MULTICAST_IF
:
2731 ifindex
= mtod(*mp
, u_int
*);
2732 (*mp
)->m_len
= sizeof(u_int
);
2733 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2736 *ifindex
= im6o
->im6o_multicast_ifp
->if_index
;
2739 case IPV6_MULTICAST_HOPS
:
2740 hlim
= mtod(*mp
, u_int
*);
2741 (*mp
)->m_len
= sizeof(u_int
);
2743 *hlim
= V_ip6_defmcasthlim
;
2745 *hlim
= im6o
->im6o_multicast_hlim
;
2748 case IPV6_MULTICAST_LOOP
:
2749 loop
= mtod(*mp
, u_int
*);
2750 (*mp
)->m_len
= sizeof(u_int
);
2752 *loop
= V_ip6_defmcasthlim
;
2754 *loop
= im6o
->im6o_multicast_loop
;
2758 return (EOPNOTSUPP
);
2763 * Discard the IP6 multicast options.
2766 ip6_freemoptions(struct ip6_moptions
*im6o
)
2768 struct in6_multi_mship
*imm
;
2773 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2774 LIST_REMOVE(imm
, i6mm_chain
);
2775 if (imm
->i6mm_maddr
)
2776 in6_delmulti(imm
->i6mm_maddr
);
2777 free(imm
, M_IP6MADDR
);
2779 free(im6o
, M_IP6MOPTS
);
2783 * Set IPv6 outgoing packet options based on advanced API.
2786 ip6_setpktopts(struct mbuf
*control
, struct ip6_pktopts
*opt
,
2787 struct ip6_pktopts
*stickyopt
, struct ucred
*cred
, int uproto
)
2789 struct cmsghdr
*cm
= 0;
2791 if (control
== NULL
|| opt
== NULL
)
2794 ip6_initpktopts(opt
);
2799 * If stickyopt is provided, make a local copy of the options
2800 * for this particular packet, then override them by ancillary
2802 * XXX: copypktopts() does not copy the cached route to a next
2803 * hop (if any). This is not very good in terms of efficiency,
2804 * but we can allow this since this option should be rarely
2807 if ((error
= copypktopts(opt
, stickyopt
, M_NOWAIT
)) != 0)
2812 * XXX: Currently, we assume all the optional information is stored
2815 if (control
->m_next
)
2818 for (; control
->m_len
; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2819 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2822 if (control
->m_len
< CMSG_LEN(0))
2825 cm
= mtod(control
, struct cmsghdr
*);
2826 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2828 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2831 error
= ip6_setpktopt(cm
->cmsg_type
, CMSG_DATA(cm
),
2832 cm
->cmsg_len
- CMSG_LEN(0), opt
, cred
, 0, 1, uproto
);
2841 * Set a particular packet option, as a sticky option or an ancillary data
2842 * item. "len" can be 0 only when it's a sticky option.
2843 * We have 4 cases of combination of "sticky" and "cmsg":
2844 * "sticky=0, cmsg=0": impossible
2845 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2846 * "sticky=1, cmsg=0": RFC3542 socket option
2847 * "sticky=1, cmsg=1": RFC2292 socket option
2850 ip6_setpktopt(int optname
, u_char
*buf
, int len
, struct ip6_pktopts
*opt
,
2851 struct ucred
*cred
, int sticky
, int cmsg
, int uproto
)
2853 int minmtupolicy
, preftemp
;
2856 if (!sticky
&& !cmsg
) {
2858 printf("ip6_setpktopt: impossible case\n");
2864 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2865 * not be specified in the context of RFC3542. Conversely,
2866 * RFC3542 types should not be specified in the context of RFC2292.
2870 case IPV6_2292PKTINFO
:
2871 case IPV6_2292HOPLIMIT
:
2872 case IPV6_2292NEXTHOP
:
2873 case IPV6_2292HOPOPTS
:
2874 case IPV6_2292DSTOPTS
:
2875 case IPV6_2292RTHDR
:
2876 case IPV6_2292PKTOPTIONS
:
2877 return (ENOPROTOOPT
);
2880 if (sticky
&& cmsg
) {
2887 case IPV6_RTHDRDSTOPTS
:
2889 case IPV6_USE_MIN_MTU
:
2892 case IPV6_PREFER_TEMPADDR
: /* XXX: not an RFC3542 option */
2893 return (ENOPROTOOPT
);
2898 case IPV6_2292PKTINFO
:
2901 struct ifnet
*ifp
= NULL
;
2902 struct in6_pktinfo
*pktinfo
;
2904 if (len
!= sizeof(struct in6_pktinfo
))
2907 pktinfo
= (struct in6_pktinfo
*)buf
;
2910 * An application can clear any sticky IPV6_PKTINFO option by
2911 * doing a "regular" setsockopt with ipi6_addr being
2912 * in6addr_any and ipi6_ifindex being zero.
2913 * [RFC 3542, Section 6]
2915 if (optname
== IPV6_PKTINFO
&& opt
->ip6po_pktinfo
&&
2916 pktinfo
->ipi6_ifindex
== 0 &&
2917 IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
2918 ip6_clearpktopts(opt
, optname
);
2922 if (uproto
== IPPROTO_TCP
&& optname
== IPV6_PKTINFO
&&
2923 sticky
&& !IN6_IS_ADDR_UNSPECIFIED(&pktinfo
->ipi6_addr
)) {
2927 /* validate the interface index if specified. */
2928 if (pktinfo
->ipi6_ifindex
> V_if_index
||
2929 pktinfo
->ipi6_ifindex
< 0) {
2932 if (pktinfo
->ipi6_ifindex
) {
2933 ifp
= ifnet_byindex(pktinfo
->ipi6_ifindex
);
2939 * We store the address anyway, and let in6_selectsrc()
2940 * validate the specified address. This is because ipi6_addr
2941 * may not have enough information about its scope zone, and
2942 * we may need additional information (such as outgoing
2943 * interface or the scope zone of a destination address) to
2944 * disambiguate the scope.
2945 * XXX: the delay of the validation may confuse the
2946 * application when it is used as a sticky option.
2948 if (opt
->ip6po_pktinfo
== NULL
) {
2949 opt
->ip6po_pktinfo
= malloc(sizeof(*pktinfo
),
2950 M_IP6OPT
, M_NOWAIT
);
2951 if (opt
->ip6po_pktinfo
== NULL
)
2954 bcopy(pktinfo
, opt
->ip6po_pktinfo
, sizeof(*pktinfo
));
2958 case IPV6_2292HOPLIMIT
:
2964 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2965 * to simplify the ordering among hoplimit options.
2967 if (optname
== IPV6_HOPLIMIT
&& sticky
)
2968 return (ENOPROTOOPT
);
2970 if (len
!= sizeof(int))
2973 if (*hlimp
< -1 || *hlimp
> 255)
2976 opt
->ip6po_hlim
= *hlimp
;
2984 if (len
!= sizeof(int))
2986 tclass
= *(int *)buf
;
2987 if (tclass
< -1 || tclass
> 255)
2990 opt
->ip6po_tclass
= tclass
;
2994 case IPV6_2292NEXTHOP
:
2997 error
= priv_check_cred(cred
,
2998 PRIV_NETINET_SETHDROPTS
, 0);
3003 if (len
== 0) { /* just remove the option */
3004 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3008 /* check if cmsg_len is large enough for sa_len */
3009 if (len
< sizeof(struct sockaddr
) || len
< *buf
)
3012 switch (((struct sockaddr
*)buf
)->sa_family
) {
3015 struct sockaddr_in6
*sa6
= (struct sockaddr_in6
*)buf
;
3018 if (sa6
->sin6_len
!= sizeof(struct sockaddr_in6
))
3021 if (IN6_IS_ADDR_UNSPECIFIED(&sa6
->sin6_addr
) ||
3022 IN6_IS_ADDR_MULTICAST(&sa6
->sin6_addr
)) {
3025 if ((error
= sa6_embedscope(sa6
, V_ip6_use_defzone
))
3031 case AF_LINK
: /* should eventually be supported */
3033 return (EAFNOSUPPORT
);
3036 /* turn off the previous option, then set the new option. */
3037 ip6_clearpktopts(opt
, IPV6_NEXTHOP
);
3038 opt
->ip6po_nexthop
= malloc(*buf
, M_IP6OPT
, M_NOWAIT
);
3039 if (opt
->ip6po_nexthop
== NULL
)
3041 bcopy(buf
, opt
->ip6po_nexthop
, *buf
);
3044 case IPV6_2292HOPOPTS
:
3047 struct ip6_hbh
*hbh
;
3051 * XXX: We don't allow a non-privileged user to set ANY HbH
3052 * options, since per-option restriction has too much
3056 error
= priv_check_cred(cred
,
3057 PRIV_NETINET_SETHDROPTS
, 0);
3063 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3064 break; /* just remove the option */
3067 /* message length validation */
3068 if (len
< sizeof(struct ip6_hbh
))
3070 hbh
= (struct ip6_hbh
*)buf
;
3071 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
3075 /* turn off the previous option, then set the new option. */
3076 ip6_clearpktopts(opt
, IPV6_HOPOPTS
);
3077 opt
->ip6po_hbh
= malloc(hbhlen
, M_IP6OPT
, M_NOWAIT
);
3078 if (opt
->ip6po_hbh
== NULL
)
3080 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
3085 case IPV6_2292DSTOPTS
:
3087 case IPV6_RTHDRDSTOPTS
:
3089 struct ip6_dest
*dest
, **newdest
= NULL
;
3092 if (cred
!= NULL
) { /* XXX: see the comment for IPV6_HOPOPTS */
3093 error
= priv_check_cred(cred
,
3094 PRIV_NETINET_SETHDROPTS
, 0);
3100 ip6_clearpktopts(opt
, optname
);
3101 break; /* just remove the option */
3104 /* message length validation */
3105 if (len
< sizeof(struct ip6_dest
))
3107 dest
= (struct ip6_dest
*)buf
;
3108 destlen
= (dest
->ip6d_len
+ 1) << 3;
3113 * Determine the position that the destination options header
3114 * should be inserted; before or after the routing header.
3117 case IPV6_2292DSTOPTS
:
3119 * The old advacned API is ambiguous on this point.
3120 * Our approach is to determine the position based
3121 * according to the existence of a routing header.
3122 * Note, however, that this depends on the order of the
3123 * extension headers in the ancillary data; the 1st
3124 * part of the destination options header must appear
3125 * before the routing header in the ancillary data,
3127 * RFC3542 solved the ambiguity by introducing
3128 * separate ancillary data or option types.
3130 if (opt
->ip6po_rthdr
== NULL
)
3131 newdest
= &opt
->ip6po_dest1
;
3133 newdest
= &opt
->ip6po_dest2
;
3135 case IPV6_RTHDRDSTOPTS
:
3136 newdest
= &opt
->ip6po_dest1
;
3139 newdest
= &opt
->ip6po_dest2
;
3143 /* turn off the previous option, then set the new option. */
3144 ip6_clearpktopts(opt
, optname
);
3145 *newdest
= malloc(destlen
, M_IP6OPT
, M_NOWAIT
);
3146 if (*newdest
== NULL
)
3148 bcopy(dest
, *newdest
, destlen
);
3153 case IPV6_2292RTHDR
:
3156 struct ip6_rthdr
*rth
;
3160 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3161 break; /* just remove the option */
3164 /* message length validation */
3165 if (len
< sizeof(struct ip6_rthdr
))
3167 rth
= (struct ip6_rthdr
*)buf
;
3168 rthlen
= (rth
->ip6r_len
+ 1) << 3;
3172 switch (rth
->ip6r_type
) {
3173 case IPV6_RTHDR_TYPE_0
:
3174 if (rth
->ip6r_len
== 0) /* must contain one addr */
3176 if (rth
->ip6r_len
% 2) /* length must be even */
3178 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
3182 return (EINVAL
); /* not supported */
3185 /* turn off the previous option */
3186 ip6_clearpktopts(opt
, IPV6_RTHDR
);
3187 opt
->ip6po_rthdr
= malloc(rthlen
, M_IP6OPT
, M_NOWAIT
);
3188 if (opt
->ip6po_rthdr
== NULL
)
3190 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
3195 case IPV6_USE_MIN_MTU
:
3196 if (len
!= sizeof(int))
3198 minmtupolicy
= *(int *)buf
;
3199 if (minmtupolicy
!= IP6PO_MINMTU_MCASTONLY
&&
3200 minmtupolicy
!= IP6PO_MINMTU_DISABLE
&&
3201 minmtupolicy
!= IP6PO_MINMTU_ALL
) {
3204 opt
->ip6po_minmtu
= minmtupolicy
;
3208 if (len
!= sizeof(int))
3211 if (uproto
== IPPROTO_TCP
|| *(int *)buf
== 0) {
3213 * we ignore this option for TCP sockets.
3214 * (RFC3542 leaves this case unspecified.)
3216 opt
->ip6po_flags
&= ~IP6PO_DONTFRAG
;
3218 opt
->ip6po_flags
|= IP6PO_DONTFRAG
;
3221 case IPV6_PREFER_TEMPADDR
:
3222 if (len
!= sizeof(int))
3224 preftemp
= *(int *)buf
;
3225 if (preftemp
!= IP6PO_TEMPADDR_SYSTEM
&&
3226 preftemp
!= IP6PO_TEMPADDR_NOTPREFER
&&
3227 preftemp
!= IP6PO_TEMPADDR_PREFER
) {
3230 opt
->ip6po_prefer_tempaddr
= preftemp
;
3234 return (ENOPROTOOPT
);
3235 } /* end of switch */
3241 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3242 * packet to the input queue of a specified interface. Note that this
3243 * calls the output routine of the loopback "driver", but with an interface
3244 * pointer that might NOT be &loif -- easier than replicating that code here.
3247 ip6_mloopback(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr_in6
*dst
)
3250 struct ip6_hdr
*ip6
;
3252 copym
= m_copy(m
, 0, M_COPYALL
);
3257 * Make sure to deep-copy IPv6 header portion in case the data
3258 * is in an mbuf cluster, so that we can safely override the IPv6
3259 * header portion later.
3261 if ((copym
->m_flags
& M_EXT
) != 0 ||
3262 copym
->m_len
< sizeof(struct ip6_hdr
)) {
3263 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
3269 if (copym
->m_len
< sizeof(*ip6
)) {
3275 ip6
= mtod(copym
, struct ip6_hdr
*);
3277 * clear embedded scope identifiers if necessary.
3278 * in6_clearscope will touch the addresses only when necessary.
3280 in6_clearscope(&ip6
->ip6_src
);
3281 in6_clearscope(&ip6
->ip6_dst
);
3283 (void)if_simloop(ifp
, copym
, dst
->sin6_family
, 0);
3287 * Chop IPv6 header off from the payload.
3290 ip6_splithdr(struct mbuf
*m
, struct ip6_exthdrs
*exthdrs
)
3293 struct ip6_hdr
*ip6
;
3295 ip6
= mtod(m
, struct ip6_hdr
*);
3296 if (m
->m_len
> sizeof(*ip6
)) {
3297 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
3302 M_MOVE_PKTHDR(mh
, m
);
3303 MH_ALIGN(mh
, sizeof(*ip6
));
3304 m
->m_len
-= sizeof(*ip6
);
3305 m
->m_data
+= sizeof(*ip6
);
3308 m
->m_len
= sizeof(*ip6
);
3309 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
3311 exthdrs
->ip6e_ip6
= m
;
3316 * Compute IPv6 extension header length.
3319 ip6_optlen(struct in6pcb
*in6p
)
3323 if (!in6p
->in6p_outputopts
)
3328 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3330 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
3331 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
3332 /* dest1 is valid with rthdr only */
3333 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
3334 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
3335 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);