1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/ip6_output.c,v 1.36 2008/06/18 11:38:37 swildner Exp $ */
3 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
69 #include "opt_ip6fw.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
74 #include <sys/param.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
86 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet/icmp6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet6/nd6.h>
99 #include <netinet6/ipsec.h>
101 #include <netinet6/ipsec6.h>
103 #include <netproto/key/key.h>
107 #include <netproto/ipsec/ipsec.h>
108 #include <netproto/ipsec/ipsec6.h>
109 #include <netproto/ipsec/key.h>
112 #include <net/ip6fw/ip6_fw.h>
114 #include <net/net_osdep.h>
116 static MALLOC_DEFINE(M_IPMOPTS
, "ip6_moptions", "internet multicast options");
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 ip6_moptions
**, struct mbuf
*);
129 static int ip6_getmoptions (int, struct ip6_moptions
*, struct mbuf
**);
130 static int copyexthdr (void *, struct mbuf
**);
131 static int ip6_insertfraghdr (struct mbuf
*, struct mbuf
*, int,
133 static int ip6_insert_jumboopt (struct ip6_exthdrs
*, u_int32_t
);
134 static struct mbuf
*ip6_splithdr (struct mbuf
*);
137 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
138 * header (with pri, len, nxt, hlim, src, dst).
139 * This function may modify ver and hlim only.
140 * The mbuf chain containing the packet will be freed.
141 * The mbuf opt, if present, will not be freed.
143 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
144 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
145 * which is rt_rmx.rmx_mtu.
148 ip6_output(struct mbuf
*m0
, struct ip6_pktopts
*opt
, struct route_in6
*ro
,
149 int flags
, struct ip6_moptions
*im6o
,
150 struct ifnet
**ifpp
, /* XXX: just for statistics */
153 struct ip6_hdr
*ip6
, *mhip6
;
154 struct ifnet
*ifp
, *origifp
;
158 int hlen
, tlen
, len
, off
;
159 struct route_in6 ip6route
;
160 struct sockaddr_in6
*dst
;
162 struct in6_ifaddr
*ia
= NULL
;
164 u_int32_t optlen
, plen
= 0, unfragpartlen
;
165 struct ip6_exthdrs exthdrs
;
166 struct in6_addr finaldst
;
167 struct route_in6
*ro_pmtu
= NULL
;
168 boolean_t hdrsplit
= FALSE
;
169 boolean_t needipsec
= FALSE
;
171 boolean_t needipsectun
= FALSE
;
172 struct secpolicy
*sp
= NULL
;
173 struct socket
*so
= inp
? inp
->inp_socket
: NULL
;
175 ip6
= mtod(m
, struct ip6_hdr
*);
178 boolean_t needipsectun
= FALSE
;
179 struct secpolicy
*sp
= NULL
;
181 ip6
= mtod(m
, struct ip6_hdr
*);
184 bzero(&exthdrs
, sizeof exthdrs
);
187 if ((error
= copyexthdr(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
)))
189 if ((error
= copyexthdr(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
)))
191 if ((error
= copyexthdr(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
)))
193 if ((error
= copyexthdr(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
)))
198 /* get a security policy for this packet */
200 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
202 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
205 ipsec6stat
.out_inval
++;
212 switch (sp
->policy
) {
213 case IPSEC_POLICY_DISCARD
:
215 * This packet is just discarded.
217 ipsec6stat
.out_polvio
++;
220 case IPSEC_POLICY_BYPASS
:
221 case IPSEC_POLICY_NONE
:
222 /* no need to do IPsec. */
226 case IPSEC_POLICY_IPSEC
:
227 if (sp
->req
== NULL
) {
228 error
= key_spdacquire(sp
); /* acquire a policy */
234 case IPSEC_POLICY_ENTRUST
:
236 kprintf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
240 /* get a security policy for this packet */
242 sp
= ipsec_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
244 sp
= ipsec_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, inp
, &error
);
247 newipsecstat
.ips_out_inval
++;
254 switch (sp
->policy
) {
255 case IPSEC_POLICY_DISCARD
:
257 * This packet is just discarded.
259 newipsecstat
.ips_out_polvio
++;
262 case IPSEC_POLICY_BYPASS
:
263 case IPSEC_POLICY_NONE
:
264 /* no need to do IPsec. */
268 case IPSEC_POLICY_IPSEC
:
269 if (sp
->req
== NULL
) {
270 error
= key_spdacquire(sp
); /* acquire a policy */
276 case IPSEC_POLICY_ENTRUST
:
278 kprintf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
280 #endif /* FAST_IPSEC */
283 * Calculate the total length of the extension header chain.
284 * Keep the length of the unfragmentable part for fragmentation.
286 optlen
= m_lengthm(exthdrs
.ip6e_hbh
, NULL
) +
287 m_lengthm(exthdrs
.ip6e_dest1
, NULL
) +
288 m_lengthm(exthdrs
.ip6e_rthdr
, NULL
);
290 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
292 /* NOTE: we don't add AH/ESP length here. do that later. */
293 optlen
+= m_lengthm(exthdrs
.ip6e_dest2
, NULL
);
296 * If we need IPsec, or there is at least one extension header,
297 * separate IP6 header from the payload.
299 if ((needipsec
|| optlen
) && !hdrsplit
) {
300 exthdrs
.ip6e_ip6
= ip6_splithdr(m
);
301 if (exthdrs
.ip6e_ip6
== NULL
) {
305 m
= exthdrs
.ip6e_ip6
;
310 ip6
= mtod(m
, struct ip6_hdr
*);
312 /* adjust mbuf packet header length */
313 m
->m_pkthdr
.len
+= optlen
;
314 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
316 /* If this is a jumbo payload, insert a jumbo payload option. */
317 if (plen
> IPV6_MAXPACKET
) {
319 exthdrs
.ip6e_ip6
= ip6_splithdr(m
);
320 if (exthdrs
.ip6e_ip6
== NULL
) {
324 m
= exthdrs
.ip6e_ip6
;
328 ip6
= mtod(m
, struct ip6_hdr
*);
329 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
333 ip6
->ip6_plen
= htons(plen
);
336 * Concatenate headers and fill in next header fields.
337 * Here we have, on "m"
339 * and we insert headers accordingly. Finally, we should be getting:
340 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
342 * during the header composing process, "m" points to IPv6 header.
343 * "mprev" points to an extension header prior to esp.
346 nexthdrp
= &ip6
->ip6_nxt
;
350 * we treat dest2 specially. this makes IPsec processing
351 * much easier. the goal here is to make mprev point the
352 * mbuf prior to dest2.
354 * result: IPv6 dest2 payload
355 * m and mprev will point to IPv6 header.
357 if (exthdrs
.ip6e_dest2
) {
359 panic("assumption failed: hdr not split");
360 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
361 m
->m_next
= exthdrs
.ip6e_dest2
;
362 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
363 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
367 * Place m1 after mprev.
369 #define MAKE_CHAIN(m1, mprev, nexthdrp, i)\
373 panic("assumption failed: hdr not split");\
374 *mtod(m1, u_char *) = *nexthdrp;\
376 nexthdrp = mtod(m1, u_char *);\
377 m1->m_next = mprev->m_next;\
384 * result: IPv6 hbh dest1 rthdr dest2 payload
385 * m will point to IPv6 header. mprev will point to the
386 * extension header prior to dest2 (rthdr in the above case).
388 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
, nexthdrp
, IPPROTO_HOPOPTS
);
389 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
, nexthdrp
, IPPROTO_DSTOPTS
);
390 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
, nexthdrp
, IPPROTO_ROUTING
);
392 #if defined(IPSEC) || defined(FAST_IPSEC)
394 struct ipsec_output_state state
;
396 struct ip6_rthdr
*rh
= NULL
;
399 * pointers after IPsec headers are not valid any more.
400 * other pointers need a great care too.
401 * (IPsec routines should not mangle mbufs prior to AH/ESP)
403 exthdrs
.ip6e_dest2
= NULL
;
405 if (exthdrs
.ip6e_rthdr
) {
406 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
407 segleft_org
= rh
->ip6r_segleft
;
408 rh
->ip6r_segleft
= 0;
411 bzero(&state
, sizeof state
);
413 error
= ipsec6_output_trans(&state
, nexthdrp
, mprev
, sp
, flags
,
417 /* mbuf is already reclaimed in ipsec6_output_trans. */
427 kprintf("ip6_output (ipsec): error code %d\n",
431 /* don't show these error codes to the user */
437 if (exthdrs
.ip6e_rthdr
) {
438 /* ah6_output doesn't modify mbuf chain */
439 rh
->ip6r_segleft
= segleft_org
;
445 * If there is a routing header, replace destination address field
446 * with the first hop of the routing header.
448 if (exthdrs
.ip6e_rthdr
) {
449 struct ip6_rthdr
*rh
;
450 struct ip6_rthdr0
*rh0
;
452 finaldst
= ip6
->ip6_dst
;
453 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
454 switch (rh
->ip6r_type
) {
455 case IPV6_RTHDR_TYPE_0
:
456 rh0
= (struct ip6_rthdr0
*)rh
;
457 ip6
->ip6_dst
= rh0
->ip6r0_addr
[0];
458 bcopy(&rh0
->ip6r0_addr
[1], &rh0
->ip6r0_addr
[0],
459 sizeof(struct in6_addr
)*(rh0
->ip6r0_segleft
- 1));
460 rh0
->ip6r0_addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
462 default: /* is it possible? */
468 /* Source address validation */
469 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
470 !(flags
& IPV6_DADOUTPUT
)) {
472 ip6stat
.ip6s_badscope
++;
475 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
477 ip6stat
.ip6s_badscope
++;
481 ip6stat
.ip6s_localout
++;
488 bzero(ro
, sizeof(*ro
));
491 if (opt
&& opt
->ip6po_rthdr
)
492 ro
= &opt
->ip6po_route
;
493 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
495 * If there is a cached route,
496 * check that it is to the same destination
497 * and is still up. If not, free it and try again.
499 if (ro
->ro_rt
!= NULL
&&
500 (!(ro
->ro_rt
->rt_flags
& RTF_UP
) || dst
->sin6_family
!= AF_INET6
||
501 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
))) {
505 if (ro
->ro_rt
== NULL
) {
506 bzero(dst
, sizeof(*dst
));
507 dst
->sin6_family
= AF_INET6
;
508 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
509 dst
->sin6_addr
= ip6
->ip6_dst
;
511 #if defined(IPSEC) || defined(FAST_IPSEC)
512 if (needipsec
&& needipsectun
) {
513 struct ipsec_output_state state
;
516 * All the extension headers will become inaccessible
517 * (since they can be encrypted).
518 * Don't panic, we need no more updates to extension headers
519 * on inner IPv6 packet (since they are now encapsulated).
521 * IPv6 [ESP|AH] IPv6 [extension headers] payload
523 bzero(&exthdrs
, sizeof(exthdrs
));
524 exthdrs
.ip6e_ip6
= m
;
526 bzero(&state
, sizeof(state
));
528 state
.ro
= (struct route
*)ro
;
529 state
.dst
= (struct sockaddr
*)dst
;
531 error
= ipsec6_output_tunnel(&state
, sp
, flags
);
534 ro
= (struct route_in6
*)state
.ro
;
535 dst
= (struct sockaddr_in6
*)state
.dst
;
537 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
548 kprintf("ip6_output (ipsec): error code %d\n", error
);
551 /* don't show these error codes to the user */
558 exthdrs
.ip6e_ip6
= m
;
562 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
565 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
566 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
568 * interface selection comes here
569 * if an interface is specified from an upper layer,
572 if (ro
->ro_rt
== NULL
) {
574 * non-bsdi always clone routes, if parent is
577 rtalloc((struct route
*)ro
);
579 if (ro
->ro_rt
== NULL
) {
580 ip6stat
.ip6s_noroute
++;
581 error
= EHOSTUNREACH
;
582 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
585 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
586 ifp
= ro
->ro_rt
->rt_ifp
;
588 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
589 dst
= (struct sockaddr_in6
*)ro
->ro_rt
->rt_gateway
;
590 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
592 in6_ifstat_inc(ifp
, ifs6_out_request
);
595 * Check if the outgoing interface conflicts with
596 * the interface specified by ifi6_ifindex (if specified).
597 * Note that loopback interface is always okay.
598 * (this may happen when we are sending a packet to one of
599 * our own addresses.)
601 if (opt
&& opt
->ip6po_pktinfo
602 && opt
->ip6po_pktinfo
->ipi6_ifindex
) {
603 if (!(ifp
->if_flags
& IFF_LOOPBACK
)
604 && ifp
->if_index
!= opt
->ip6po_pktinfo
->ipi6_ifindex
) {
605 ip6stat
.ip6s_noroute
++;
606 in6_ifstat_inc(ifp
, ifs6_out_discard
);
607 error
= EHOSTUNREACH
;
612 if (opt
&& opt
->ip6po_hlim
!= -1)
613 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
616 struct in6_multi
*in6m
;
618 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
621 * See if the caller provided any multicast options
625 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
626 if (im6o
->im6o_multicast_ifp
!= NULL
)
627 ifp
= im6o
->im6o_multicast_ifp
;
629 ip6
->ip6_hlim
= ip6_defmcasthlim
;
632 * See if the caller provided the outgoing interface
633 * as an ancillary data.
634 * Boundary check for ifindex is assumed to be already done.
636 if (opt
&& opt
->ip6po_pktinfo
&& opt
->ip6po_pktinfo
->ipi6_ifindex
)
637 ifp
= ifindex2ifnet
[opt
->ip6po_pktinfo
->ipi6_ifindex
];
640 * If the destination is a node-local scope multicast,
641 * the packet should be loop-backed only.
643 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
)) {
645 * If the outgoing interface is already specified,
646 * it should be a loopback interface.
648 if (ifp
&& !(ifp
->if_flags
& IFF_LOOPBACK
)) {
649 ip6stat
.ip6s_badscope
++;
650 error
= ENETUNREACH
; /* XXX: better error? */
651 /* XXX correct ifp? */
652 in6_ifstat_inc(ifp
, ifs6_out_discard
);
659 if (opt
&& opt
->ip6po_hlim
!= -1)
660 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
663 * If caller did not provide an interface lookup a
664 * default in the routing table. This is either a
665 * default for the speicfied group (i.e. a host
666 * route), or a multicast default (a route for the
670 if (ro
->ro_rt
== NULL
) {
672 rtpurelookup((struct sockaddr
*)&ro
->ro_dst
);
674 if (ro
->ro_rt
== NULL
) {
675 ip6stat
.ip6s_noroute
++;
676 error
= EHOSTUNREACH
;
677 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
680 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
681 ifp
= ro
->ro_rt
->rt_ifp
;
685 if (!(flags
& IPV6_FORWARDING
))
686 in6_ifstat_inc(ifp
, ifs6_out_request
);
687 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
690 * Confirm that the outgoing interface supports multicast.
692 if (!(ifp
->if_flags
& IFF_MULTICAST
)) {
693 ip6stat
.ip6s_noroute
++;
694 in6_ifstat_inc(ifp
, ifs6_out_discard
);
698 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
700 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
702 * If we belong to the destination multicast group
703 * on the outgoing interface, and the caller did not
704 * forbid loopback, loop back a copy.
706 ip6_mloopback(ifp
, m
, dst
);
709 * If we are acting as a multicast router, perform
710 * multicast forwarding as if the packet had just
711 * arrived on the interface to which we are about
712 * to send. The multicast forwarding function
713 * recursively calls this function, using the
714 * IPV6_FORWARDING flag to prevent infinite recursion.
716 * Multicasts that are looped back by ip6_mloopback(),
717 * above, will be forwarded by the ip6_input() routine,
720 if (ip6_mrouter
&& !(flags
& IPV6_FORWARDING
)) {
721 if (ip6_mforward(ip6
, ifp
, m
) != 0) {
728 * Multicasts with a hoplimit of zero may be looped back,
729 * above, but must not be transmitted on a network.
730 * Also, multicasts addressed to the loopback interface
731 * are not sent -- the above call to ip6_mloopback() will
732 * loop back a copy if this host actually belongs to the
733 * destination group on the loopback interface.
735 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
)) {
742 * Fill the outgoing inteface to tell the upper layer
743 * to increment per-interface statistics.
749 * Determine path MTU.
752 /* The first hop and the final destination may differ. */
753 struct sockaddr_in6
*sin6_fin
=
754 (struct sockaddr_in6
*)&ro_pmtu
->ro_dst
;
756 if (ro_pmtu
->ro_rt
!= NULL
&&
757 (!(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
758 !IN6_ARE_ADDR_EQUAL(&sin6_fin
->sin6_addr
, &finaldst
))) {
759 RTFREE(ro_pmtu
->ro_rt
);
760 ro_pmtu
->ro_rt
= NULL
;
762 if (ro_pmtu
->ro_rt
== NULL
) {
763 bzero(sin6_fin
, sizeof(*sin6_fin
));
764 sin6_fin
->sin6_family
= AF_INET6
;
765 sin6_fin
->sin6_len
= sizeof(struct sockaddr_in6
);
766 sin6_fin
->sin6_addr
= finaldst
;
768 rtalloc((struct route
*)ro_pmtu
);
771 if (ro_pmtu
->ro_rt
!= NULL
) {
772 u_int32_t ifmtu
= ND_IFINFO(ifp
)->linkmtu
;
774 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
775 if (mtu
> ifmtu
|| mtu
== 0) {
777 * The MTU on the route is larger than the MTU on
778 * the interface! This shouldn't happen, unless the
779 * MTU of the interface has been changed after the
780 * interface was brought up. Change the MTU in the
781 * route to match the interface MTU (as long as the
782 * field isn't locked).
784 * if MTU on the route is 0, we need to fix the MTU.
785 * this case happens with path MTU discovery timeouts.
788 if (!(ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
))
789 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
792 mtu
= ND_IFINFO(ifp
)->linkmtu
;
796 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
798 if ((flags
& IPV6_MINMTU
) != 0 && mtu
> IPV6_MMTU
)
801 /* Fake scoped addresses */
802 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
804 * If source or destination address is a scoped address, and
805 * the packet is going to be sent to a loopback interface,
806 * we should keep the original interface.
810 * XXX: this is a very experimental and temporary solution.
811 * We eventually have sockaddr_in6 and use the sin6_scope_id
812 * field of the structure here.
813 * We rely on the consistency between two scope zone ids
814 * of source and destination, which should already be assured.
815 * Larger scopes than link will be supported in the future.
818 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
819 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_src
.s6_addr16
[1])];
820 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
821 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_dst
.s6_addr16
[1])];
823 * XXX: origifp can be NULL even in those two cases above.
824 * For example, if we remove the (only) link-local address
825 * from the loopback interface, and try to send a link-local
826 * address without link-id information. Then the source
827 * address is ::1, and the destination address is the
828 * link-local address with its s6_addr16[1] being zero.
829 * What is worse, if the packet goes to the loopback interface
830 * by a default rejected route, the null pointer would be
831 * passed to looutput, and the kernel would hang.
832 * The following last resort would prevent such disaster.
840 * clear embedded scope identifiers if necessary.
841 * in6_clearscope will touch the addresses only when necessary.
843 in6_clearscope(&ip6
->ip6_src
);
844 in6_clearscope(&ip6
->ip6_dst
);
847 * Check with the firewall...
849 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
852 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
853 /* If ipfw says divert, we have to just drop packet */
854 if ((*ip6_fw_chk_ptr
)(&ip6
, ifp
, &port
, &m
)) {
865 * If the outgoing packet contains a hop-by-hop options header,
866 * it must be examined and processed even by the source node.
867 * (RFC 2460, section 4.)
869 if (exthdrs
.ip6e_hbh
) {
870 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
871 u_int32_t dummy1
; /* XXX unused */
872 u_int32_t dummy2
; /* XXX unused */
875 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
876 panic("ip6e_hbh is not continuous");
879 * XXX: if we have to send an ICMPv6 error to the sender,
880 * we need the M_LOOP flag since icmp6_error() expects
881 * the IPv6 and the hop-by-hop options header are
882 * continuous unless the flag is set.
884 m
->m_flags
|= M_LOOP
;
885 m
->m_pkthdr
.rcvif
= ifp
;
886 if (ip6_process_hopopts(m
,
887 (u_int8_t
*)(hbh
+ 1),
888 ((hbh
->ip6h_len
+ 1) << 3) -
889 sizeof(struct ip6_hbh
),
890 &dummy1
, &dummy2
) < 0) {
891 /* m was already freed at this point */
892 error
= EINVAL
;/* better error? */
895 m
->m_flags
&= ~M_LOOP
; /* XXX */
896 m
->m_pkthdr
.rcvif
= NULL
;
900 * Run through list of hooks for output packets.
902 if (pfil_has_hooks(&inet6_pfil_hook
)) {
903 error
= pfil_run_hooks(&inet6_pfil_hook
, &m
, ifp
, PFIL_OUT
);
904 if (error
!= 0 || m
== NULL
)
906 ip6
= mtod(m
, struct ip6_hdr
*);
910 * Send the packet to the outgoing interface.
911 * If necessary, do IPv6 fragmentation before sending.
913 tlen
= m
->m_pkthdr
.len
;
917 * On any link that cannot convey a 1280-octet packet in one piece,
918 * link-specific fragmentation and reassembly must be provided at
919 * a layer below IPv6. [RFC 2460, sec.5]
920 * Thus if the interface has ability of link-level fragmentation,
921 * we can just send the packet even if the packet size is
922 * larger than the link's MTU.
923 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
926 || ifp
->if_flags
& IFF_FRAGMENTABLE
930 /* Record statistics for this interface address. */
931 if (ia
&& !(flags
& IPV6_FORWARDING
)) {
932 ia
->ia_ifa
.if_opackets
++;
933 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
936 /* clean ipsec history once it goes out of the node */
939 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
941 } else if (mtu
< IPV6_MMTU
) {
943 * note that path MTU is never less than IPV6_MMTU
947 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
949 } else if (ip6
->ip6_plen
== 0) { /* jumbo payload cannot be fragmented */
951 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
954 struct mbuf
**mnext
, *m_frgpart
;
955 struct ip6_frag
*ip6f
;
956 u_int32_t id
= htonl(ip6_id
++);
960 * Too large for the destination or interface;
961 * fragment if possible.
962 * Must be able to put at least 8 bytes per fragment.
964 hlen
= unfragpartlen
;
965 if (mtu
> IPV6_MAXPACKET
)
966 mtu
= IPV6_MAXPACKET
;
968 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
971 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
975 mnext
= &m
->m_nextpkt
;
978 * Change the next header field of the last header in the
979 * unfragmentable part.
981 if (exthdrs
.ip6e_rthdr
) {
982 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
983 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
984 } else if (exthdrs
.ip6e_dest1
) {
985 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
986 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
987 } else if (exthdrs
.ip6e_hbh
) {
988 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
989 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
991 nextproto
= ip6
->ip6_nxt
;
992 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
996 * Loop through length of segment after first fragment,
997 * make new header and copy data of each part and link onto
1001 for (off
= hlen
; off
< tlen
; off
+= len
) {
1002 MGETHDR(m
, MB_DONTWAIT
, MT_HEADER
);
1005 ip6stat
.ip6s_odropped
++;
1008 m
->m_pkthdr
.rcvif
= NULL
;
1009 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
1011 mnext
= &m
->m_nextpkt
;
1012 m
->m_data
+= max_linkhdr
;
1013 mhip6
= mtod(m
, struct ip6_hdr
*);
1015 m
->m_len
= sizeof(*mhip6
);
1016 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
1018 ip6stat
.ip6s_odropped
++;
1021 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1022 if (off
+ len
>= tlen
)
1025 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1026 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1028 sizeof(struct ip6_hdr
)));
1029 if ((m_frgpart
= m_copy(m0
, off
, len
)) == NULL
) {
1031 ip6stat
.ip6s_odropped
++;
1034 m_cat(m
, m_frgpart
);
1035 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1036 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1037 ip6f
->ip6f_reserved
= 0;
1038 ip6f
->ip6f_ident
= id
;
1039 ip6f
->ip6f_nxt
= nextproto
;
1040 ip6stat
.ip6s_ofragments
++;
1041 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1044 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1048 * Remove leading garbages.
1052 m0
->m_nextpkt
= NULL
;
1054 for (m0
= m
; m
; m
= m0
) {
1056 m
->m_nextpkt
= NULL
;
1058 /* Record statistics for this interface address. */
1060 ia
->ia_ifa
.if_opackets
++;
1061 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1064 /* clean ipsec history once it goes out of the node */
1067 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
1073 ip6stat
.ip6s_fragmented
++;
1076 if (ro
== &ip6route
&& ro
->ro_rt
) { /* brace necessary for RTFREE */
1078 } else if (ro_pmtu
== &ip6route
&& ro_pmtu
->ro_rt
) {
1079 RTFREE(ro_pmtu
->ro_rt
);
1094 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1095 m_freem(exthdrs
.ip6e_dest1
);
1096 m_freem(exthdrs
.ip6e_rthdr
);
1097 m_freem(exthdrs
.ip6e_dest2
);
1105 copyexthdr(void *h
, struct mbuf
**mp
)
1107 struct ip6_ext
*hdr
= h
;
1114 hlen
= (hdr
->ip6e_len
+ 1) * 8;
1115 if (hlen
> MCLBYTES
)
1116 return ENOBUFS
; /* XXX */
1118 m
= m_getb(hlen
, MB_DONTWAIT
, MT_DATA
, 0);
1123 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1130 * Insert jumbo payload option.
1133 ip6_insert_jumboopt(struct ip6_exthdrs
*exthdrs
, u_int32_t plen
)
1139 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1142 * If there is no hop-by-hop options header, allocate new one.
1143 * If there is one but it doesn't have enough space to store the
1144 * jumbo payload option, allocate a cluster to store the whole options.
1145 * Otherwise, use it to store the options.
1147 if (exthdrs
->ip6e_hbh
== NULL
) {
1148 MGET(mopt
, MB_DONTWAIT
, MT_DATA
);
1151 mopt
->m_len
= JUMBOOPTLEN
;
1152 optbuf
= mtod(mopt
, u_char
*);
1153 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1154 exthdrs
->ip6e_hbh
= mopt
;
1156 struct ip6_hbh
*hbh
;
1158 mopt
= exthdrs
->ip6e_hbh
;
1159 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1162 * - exthdrs->ip6e_hbh is not referenced from places
1163 * other than exthdrs.
1164 * - exthdrs->ip6e_hbh is not an mbuf chain.
1166 int oldoptlen
= mopt
->m_len
;
1170 * XXX: give up if the whole (new) hbh header does
1171 * not fit even in an mbuf cluster.
1173 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1177 * As a consequence, we must always prepare a cluster
1180 n
= m_getcl(MB_DONTWAIT
, MT_DATA
, 0);
1183 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1184 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
), oldoptlen
);
1185 optbuf
= mtod(n
, caddr_t
) + oldoptlen
;
1187 mopt
= exthdrs
->ip6e_hbh
= n
;
1189 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1190 mopt
->m_len
+= JUMBOOPTLEN
;
1192 optbuf
[0] = IP6OPT_PADN
;
1196 * Adjust the header length according to the pad and
1197 * the jumbo payload option.
1199 hbh
= mtod(mopt
, struct ip6_hbh
*);
1200 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1203 /* fill in the option. */
1204 optbuf
[2] = IP6OPT_JUMBO
;
1206 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1207 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1209 /* finally, adjust the packet header length */
1210 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1217 * Insert fragment header and copy unfragmentable header portions.
1220 ip6_insertfraghdr(struct mbuf
*m0
, struct mbuf
*m
, int hlen
,
1221 struct ip6_frag
**frghdrp
)
1223 struct mbuf
*n
, *mlast
;
1225 if (hlen
> sizeof(struct ip6_hdr
)) {
1226 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1227 hlen
- sizeof(struct ip6_hdr
), MB_DONTWAIT
);
1234 /* Search for the last mbuf of unfragmentable part. */
1235 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1238 if (!(mlast
->m_flags
& M_EXT
) &&
1239 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1240 /* use the trailing space of the last mbuf for the fragment hdr */
1242 (struct ip6_frag
*)(mtod(mlast
, caddr_t
) + mlast
->m_len
);
1243 mlast
->m_len
+= sizeof(struct ip6_frag
);
1244 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1246 /* allocate a new mbuf for the fragment header */
1249 MGET(mfrg
, MB_DONTWAIT
, MT_DATA
);
1252 mfrg
->m_len
= sizeof(struct ip6_frag
);
1253 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1254 mlast
->m_next
= mfrg
;
1261 * IP6 socket option processing.
1264 ip6_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
1267 struct inpcb
*in6p
= so
->so_pcb
;
1269 int level
, op
, optname
;
1274 level
= sopt
->sopt_level
;
1275 op
= sopt
->sopt_dir
;
1276 optname
= sopt
->sopt_name
;
1277 optlen
= sopt
->sopt_valsize
;
1280 panic("ip6_ctloutput: arg soopt is NULL");
1286 privileged
= (td
== NULL
|| suser(td
)) ? 0 : 1;
1288 if (level
== IPPROTO_IPV6
) {
1293 case IPV6_PKTOPTIONS
:
1297 error
= soopt_getm(sopt
, &m
); /* XXX */
1300 soopt_to_mbuf(sopt
, m
); /* XXX */
1301 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1303 m_freem(m
); /* XXX */
1308 * Use of some Hop-by-Hop options or some
1309 * Destination options, might require special
1310 * privilege. That is, normal applications
1311 * (without special privilege) might be forbidden
1312 * from setting certain options in outgoing packets,
1313 * and might never see certain options in received
1314 * packets. [RFC 2292 Section 6]
1315 * KAME specific note:
1316 * KAME prevents non-privileged users from sending or
1317 * receiving ANY hbh/dst options in order to avoid
1318 * overhead of parsing options in the kernel.
1320 case IPV6_UNICAST_HOPS
:
1325 if (optlen
!= sizeof(int)) {
1329 error
= soopt_to_kbuf(sopt
, &optval
,
1330 sizeof optval
, sizeof optval
);
1335 case IPV6_UNICAST_HOPS
:
1336 if (optval
< -1 || optval
>= 256)
1339 /* -1 = kernel default */
1340 in6p
->in6p_hops
= optval
;
1342 if ((in6p
->in6p_vflag
&
1344 in6p
->inp_ip_ttl
= optval
;
1347 #define OPTSET(bit) \
1350 in6p->in6p_flags |= (bit); \
1352 in6p->in6p_flags &= ~(bit); \
1354 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1357 in6p
->in6p_cksum
= optval
;
1366 * make setsockopt(IPV6_V6ONLY)
1367 * available only prior to bind(2).
1368 * see ipng mailing list, Jun 22 2001.
1370 if (in6p
->in6p_lport
||
1371 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
))
1376 OPTSET(IN6P_IPV6_V6ONLY
);
1378 in6p
->in6p_vflag
&= ~INP_IPV4
;
1380 in6p
->in6p_vflag
|= INP_IPV4
;
1391 if (optlen
!= sizeof(int)) {
1395 error
= soopt_to_kbuf(sopt
, &optval
,
1396 sizeof optval
, sizeof optval
);
1401 OPTSET(IN6P_PKTINFO
);
1404 OPTSET(IN6P_HOPLIMIT
);
1408 * Check super-user privilege.
1409 * See comments for IPV6_RECVHOPOPTS.
1413 OPTSET(IN6P_HOPOPTS
);
1418 OPTSET(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1427 case IPV6_MULTICAST_IF
:
1428 case IPV6_MULTICAST_HOPS
:
1429 case IPV6_MULTICAST_LOOP
:
1430 case IPV6_JOIN_GROUP
:
1431 case IPV6_LEAVE_GROUP
:
1434 if (sopt
->sopt_valsize
> MLEN
) {
1439 MGET(m
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
, MT_HEADER
);
1444 m
->m_len
= sopt
->sopt_valsize
;
1445 error
= soopt_to_kbuf(sopt
, mtod(m
, char *),
1446 m
->m_len
, m
->m_len
);
1447 error
= ip6_setmoptions(sopt
->sopt_name
,
1448 &in6p
->in6p_moptions
,
1454 case IPV6_PORTRANGE
:
1455 error
= soopt_to_kbuf(sopt
, &optval
,
1456 sizeof optval
, sizeof optval
);
1461 case IPV6_PORTRANGE_DEFAULT
:
1462 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1463 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1466 case IPV6_PORTRANGE_HIGH
:
1467 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1468 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1471 case IPV6_PORTRANGE_LOW
:
1472 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1473 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1482 #if defined(IPSEC) || defined(FAST_IPSEC)
1483 case IPV6_IPSEC_POLICY
:
1489 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1491 soopt_to_mbuf(sopt
, m
); /* XXX */
1493 req
= mtod(m
, caddr_t
);
1496 error
= ipsec6_set_policy(in6p
, optname
, req
,
1501 #endif /* KAME IPSEC */
1509 struct mbuf
**mp
= &m
;
1511 if (ip6_fw_ctl_ptr
== NULL
)
1514 if ((error
= soopt_getm(sopt
, &m
)) != 0)
1517 soopt_to_mbuf(sopt
, m
);
1518 error
= (*ip6_fw_ctl_ptr
)(optname
, mp
);
1524 error
= ENOPROTOOPT
;
1532 case IPV6_PKTOPTIONS
:
1533 if (in6p
->in6p_options
) {
1535 m
= m_copym(in6p
->in6p_options
,
1536 0, M_COPYALL
, MB_WAIT
);
1537 error
= soopt_from_mbuf(sopt
, m
);
1541 sopt
->sopt_valsize
= 0;
1544 case IPV6_UNICAST_HOPS
:
1549 case IPV6_PORTRANGE
:
1552 case IPV6_UNICAST_HOPS
:
1553 optval
= in6p
->in6p_hops
;
1557 optval
= in6p
->in6p_cksum
;
1561 optval
= OPTBIT(IN6P_FAITH
);
1565 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1568 case IPV6_PORTRANGE
:
1571 flags
= in6p
->in6p_flags
;
1572 if (flags
& IN6P_HIGHPORT
)
1573 optval
= IPV6_PORTRANGE_HIGH
;
1574 else if (flags
& IN6P_LOWPORT
)
1575 optval
= IPV6_PORTRANGE_LOW
;
1581 soopt_from_kbuf(sopt
, &optval
,
1590 if (optname
== IPV6_HOPOPTS
||
1591 optname
== IPV6_DSTOPTS
||
1596 optval
= OPTBIT(IN6P_PKTINFO
);
1599 optval
= OPTBIT(IN6P_HOPLIMIT
);
1604 optval
= OPTBIT(IN6P_HOPOPTS
);
1607 optval
= OPTBIT(IN6P_RTHDR
);
1612 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1615 soopt_from_kbuf(sopt
, &optval
,
1619 case IPV6_MULTICAST_IF
:
1620 case IPV6_MULTICAST_HOPS
:
1621 case IPV6_MULTICAST_LOOP
:
1622 case IPV6_JOIN_GROUP
:
1623 case IPV6_LEAVE_GROUP
:
1626 error
= ip6_getmoptions(sopt
->sopt_name
,
1627 in6p
->in6p_moptions
, &m
);
1629 soopt_from_kbuf(sopt
,
1630 mtod(m
, char *), m
->m_len
);
1635 #if defined(IPSEC) || defined(FAST_IPSEC)
1636 case IPV6_IPSEC_POLICY
:
1640 struct mbuf
*m
= NULL
;
1641 struct mbuf
**mp
= &m
;
1643 error
= soopt_getm(sopt
, &m
); /* XXX */
1646 soopt_to_mbuf(sopt
, m
); /* XXX */
1648 req
= mtod(m
, caddr_t
);
1651 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
1653 error
= soopt_from_mbuf(sopt
, m
); /*XXX*/
1654 if (error
== 0 && m
!= NULL
)
1658 #endif /* KAME IPSEC */
1663 struct mbuf
**mp
= &m
;
1665 if (ip6_fw_ctl_ptr
== NULL
)
1669 error
= (*ip6_fw_ctl_ptr
)(optname
, mp
);
1671 error
= soopt_from_mbuf(sopt
, m
); /* XXX */
1672 if (error
== 0 && m
!= NULL
)
1678 error
= ENOPROTOOPT
;
1690 * Set up IP6 options in pcb for insertion in output packets or
1691 * specifying behavior of outgoing packets.
1694 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
1695 struct sockopt
*sopt
)
1697 struct ip6_pktopts
*opt
= *pktopt
;
1699 struct thread
*td
= sopt
->sopt_td
;
1702 /* turn off any old options. */
1705 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
1706 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
1707 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1708 kprintf("ip6_pcbopts: all specified options are cleared.\n");
1710 ip6_clearpktopts(opt
, 1, -1);
1712 opt
= kmalloc(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
1715 if (m
== NULL
|| m
->m_len
== 0) {
1717 * Only turning off any previous options, regardless of
1718 * whether the opt is just created or given.
1720 kfree(opt
, M_IP6OPT
);
1724 /* set options specified by user. */
1727 if ((error
= ip6_setpktoptions(m
, opt
, priv
, 1)) != 0) {
1728 ip6_clearpktopts(opt
, 1, -1); /* XXX: discard all options */
1729 kfree(opt
, M_IP6OPT
);
1737 * initialize ip6_pktopts. beware that there are non-zero default values in
1741 init_ip6pktopts(struct ip6_pktopts
*opt
)
1744 bzero(opt
, sizeof(*opt
));
1745 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
1749 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int needfree
, int optname
)
1754 if (optname
== -1) {
1755 if (needfree
&& pktopt
->ip6po_pktinfo
)
1756 kfree(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
1757 pktopt
->ip6po_pktinfo
= NULL
;
1760 pktopt
->ip6po_hlim
= -1;
1761 if (optname
== -1) {
1762 if (needfree
&& pktopt
->ip6po_nexthop
)
1763 kfree(pktopt
->ip6po_nexthop
, M_IP6OPT
);
1764 pktopt
->ip6po_nexthop
= NULL
;
1766 if (optname
== -1) {
1767 if (needfree
&& pktopt
->ip6po_hbh
)
1768 kfree(pktopt
->ip6po_hbh
, M_IP6OPT
);
1769 pktopt
->ip6po_hbh
= NULL
;
1771 if (optname
== -1) {
1772 if (needfree
&& pktopt
->ip6po_dest1
)
1773 kfree(pktopt
->ip6po_dest1
, M_IP6OPT
);
1774 pktopt
->ip6po_dest1
= NULL
;
1776 if (optname
== -1) {
1777 if (needfree
&& pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1778 kfree(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
1779 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
1780 if (pktopt
->ip6po_route
.ro_rt
) {
1781 RTFREE(pktopt
->ip6po_route
.ro_rt
);
1782 pktopt
->ip6po_route
.ro_rt
= NULL
;
1785 if (optname
== -1) {
1786 if (needfree
&& pktopt
->ip6po_dest2
)
1787 kfree(pktopt
->ip6po_dest2
, M_IP6OPT
);
1788 pktopt
->ip6po_dest2
= NULL
;
1792 #define PKTOPT_EXTHDRCPY(type) \
1796 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1797 dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
1798 if (dst->type == NULL)\
1800 bcopy(src->type, dst->type, hlen);\
1804 struct ip6_pktopts
*
1805 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
1807 struct ip6_pktopts
*dst
;
1810 kprintf("ip6_clearpktopts: invalid argument\n");
1814 dst
= kmalloc(sizeof(*dst
), M_IP6OPT
, canwait
| M_ZERO
);
1818 dst
->ip6po_hlim
= src
->ip6po_hlim
;
1819 if (src
->ip6po_pktinfo
) {
1820 dst
->ip6po_pktinfo
= kmalloc(sizeof(*dst
->ip6po_pktinfo
),
1822 if (dst
->ip6po_pktinfo
== NULL
)
1824 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
1826 if (src
->ip6po_nexthop
) {
1827 dst
->ip6po_nexthop
= kmalloc(src
->ip6po_nexthop
->sa_len
,
1829 if (dst
->ip6po_nexthop
== NULL
)
1831 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
1832 src
->ip6po_nexthop
->sa_len
);
1834 PKTOPT_EXTHDRCPY(ip6po_hbh
);
1835 PKTOPT_EXTHDRCPY(ip6po_dest1
);
1836 PKTOPT_EXTHDRCPY(ip6po_dest2
);
1837 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
1841 if (dst
->ip6po_pktinfo
) kfree(dst
->ip6po_pktinfo
, M_IP6OPT
);
1842 if (dst
->ip6po_nexthop
) kfree(dst
->ip6po_nexthop
, M_IP6OPT
);
1843 if (dst
->ip6po_hbh
) kfree(dst
->ip6po_hbh
, M_IP6OPT
);
1844 if (dst
->ip6po_dest1
) kfree(dst
->ip6po_dest1
, M_IP6OPT
);
1845 if (dst
->ip6po_dest2
) kfree(dst
->ip6po_dest2
, M_IP6OPT
);
1846 if (dst
->ip6po_rthdr
) kfree(dst
->ip6po_rthdr
, M_IP6OPT
);
1847 kfree(dst
, M_IP6OPT
);
1850 #undef PKTOPT_EXTHDRCPY
1853 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
1858 ip6_clearpktopts(pktopt
, 1, -1);
1860 kfree(pktopt
, M_IP6OPT
);
1864 * Set the IP6 multicast options in response to user setsockopt().
1867 ip6_setmoptions(int optname
, struct ip6_moptions
**im6op
, struct mbuf
*m
)
1870 u_int loop
, ifindex
;
1871 struct ipv6_mreq
*mreq
;
1873 struct ip6_moptions
*im6o
= *im6op
;
1874 struct route_in6 ro
;
1875 struct sockaddr_in6
*dst
;
1876 struct in6_multi_mship
*imm
;
1877 struct thread
*td
= curthread
; /* XXX */
1881 * No multicast option buffer attached to the pcb;
1882 * allocate one and initialize to default values.
1884 im6o
= (struct ip6_moptions
*)
1885 kmalloc(sizeof(*im6o
), M_IPMOPTS
, M_WAITOK
);
1888 im6o
->im6o_multicast_ifp
= NULL
;
1889 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1890 im6o
->im6o_multicast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
1891 LIST_INIT(&im6o
->im6o_memberships
);
1896 case IPV6_MULTICAST_IF
:
1898 * Select the interface for outgoing multicast packets.
1900 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
1904 bcopy(mtod(m
, u_int
*), &ifindex
, sizeof(ifindex
));
1905 if (ifindex
< 0 || if_index
< ifindex
) {
1906 error
= ENXIO
; /* XXX EINVAL? */
1909 ifp
= ifindex2ifnet
[ifindex
];
1910 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
1911 error
= EADDRNOTAVAIL
;
1914 im6o
->im6o_multicast_ifp
= ifp
;
1917 case IPV6_MULTICAST_HOPS
:
1920 * Set the IP6 hoplimit for outgoing multicast packets.
1923 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
1927 bcopy(mtod(m
, u_int
*), &optval
, sizeof(optval
));
1928 if (optval
< -1 || optval
>= 256)
1930 else if (optval
== -1)
1931 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1933 im6o
->im6o_multicast_hlim
= optval
;
1937 case IPV6_MULTICAST_LOOP
:
1939 * Set the loopback flag for outgoing multicast packets.
1940 * Must be zero or one.
1942 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
1946 bcopy(mtod(m
, u_int
*), &loop
, sizeof(loop
));
1951 im6o
->im6o_multicast_loop
= loop
;
1954 case IPV6_JOIN_GROUP
:
1956 * Add a multicast group membership.
1957 * Group must be a valid IP6 multicast address.
1959 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
1963 mreq
= mtod(m
, struct ipv6_mreq
*);
1964 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
1966 * We use the unspecified address to specify to accept
1967 * all multicast addresses. Only super user is allowed
1975 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
1981 * If the interface is specified, validate it.
1983 if (mreq
->ipv6mr_interface
< 0
1984 || if_index
< mreq
->ipv6mr_interface
) {
1985 error
= ENXIO
; /* XXX EINVAL? */
1989 * If no interface was explicitly specified, choose an
1990 * appropriate one according to the given multicast address.
1992 if (mreq
->ipv6mr_interface
== 0) {
1994 * If the multicast address is in node-local scope,
1995 * the interface should be a loopback interface.
1996 * Otherwise, look up the routing table for the
1997 * address, and choose the outgoing interface.
1998 * XXX: is it a good approach?
2000 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq
->ipv6mr_multiaddr
)) {
2004 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
2005 bzero(dst
, sizeof(*dst
));
2006 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2007 dst
->sin6_family
= AF_INET6
;
2008 dst
->sin6_addr
= mreq
->ipv6mr_multiaddr
;
2009 rtalloc((struct route
*)&ro
);
2010 if (ro
.ro_rt
== NULL
) {
2011 error
= EADDRNOTAVAIL
;
2014 ifp
= ro
.ro_rt
->rt_ifp
;
2018 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2021 * See if we found an interface, and confirm that it
2022 * supports multicast
2024 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
2025 error
= EADDRNOTAVAIL
;
2029 * Put interface index into the multicast address,
2030 * if the address has link-local scope.
2032 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2033 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2034 = htons(mreq
->ipv6mr_interface
);
2037 * See if the membership already exists.
2039 for (imm
= im6o
->im6o_memberships
.lh_first
;
2040 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2041 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2042 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2043 &mreq
->ipv6mr_multiaddr
))
2050 * Everything looks good; add a new record to the multicast
2051 * address list for the given interface.
2053 imm
= kmalloc(sizeof(*imm
), M_IPMADDR
, M_WAITOK
);
2054 if ((imm
->i6mm_maddr
=
2055 in6_addmulti(&mreq
->ipv6mr_multiaddr
, ifp
, &error
)) == NULL
) {
2056 kfree(imm
, M_IPMADDR
);
2059 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2062 case IPV6_LEAVE_GROUP
:
2064 * Drop a multicast group membership.
2065 * Group must be a valid IP6 multicast address.
2067 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2071 mreq
= mtod(m
, struct ipv6_mreq
*);
2072 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2077 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2082 * If an interface address was specified, get a pointer
2083 * to its ifnet structure.
2085 if (mreq
->ipv6mr_interface
< 0
2086 || if_index
< mreq
->ipv6mr_interface
) {
2087 error
= ENXIO
; /* XXX EINVAL? */
2090 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2092 * Put interface index into the multicast address,
2093 * if the address has link-local scope.
2095 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2096 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2097 = htons(mreq
->ipv6mr_interface
);
2100 * Find the membership in the membership list.
2102 for (imm
= im6o
->im6o_memberships
.lh_first
;
2103 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2105 imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2106 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2107 &mreq
->ipv6mr_multiaddr
))
2111 /* Unable to resolve interface */
2112 error
= EADDRNOTAVAIL
;
2116 * Give up the multicast address record to which the
2117 * membership points.
2119 LIST_REMOVE(imm
, i6mm_chain
);
2120 in6_delmulti(imm
->i6mm_maddr
);
2121 kfree(imm
, M_IPMADDR
);
2130 * If all options have default values, no need to keep the mbuf.
2132 if (im6o
->im6o_multicast_ifp
== NULL
&&
2133 im6o
->im6o_multicast_hlim
== ip6_defmcasthlim
&&
2134 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2135 im6o
->im6o_memberships
.lh_first
== NULL
) {
2136 kfree(*im6op
, M_IPMOPTS
);
2144 * Return the IP6 multicast options in response to user getsockopt().
2147 ip6_getmoptions(int optname
, struct ip6_moptions
*im6o
, struct mbuf
**mp
)
2149 u_int
*hlim
, *loop
, *ifindex
;
2151 *mp
= m_get(MB_WAIT
, MT_HEADER
); /* XXX */
2155 case IPV6_MULTICAST_IF
:
2156 ifindex
= mtod(*mp
, u_int
*);
2157 (*mp
)->m_len
= sizeof(u_int
);
2158 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2161 *ifindex
= im6o
->im6o_multicast_ifp
->if_index
;
2164 case IPV6_MULTICAST_HOPS
:
2165 hlim
= mtod(*mp
, u_int
*);
2166 (*mp
)->m_len
= sizeof(u_int
);
2168 *hlim
= ip6_defmcasthlim
;
2170 *hlim
= im6o
->im6o_multicast_hlim
;
2173 case IPV6_MULTICAST_LOOP
:
2174 loop
= mtod(*mp
, u_int
*);
2175 (*mp
)->m_len
= sizeof(u_int
);
2177 *loop
= ip6_defmcasthlim
;
2179 *loop
= im6o
->im6o_multicast_loop
;
2183 return (EOPNOTSUPP
);
2188 * Discard the IP6 multicast options.
2191 ip6_freemoptions(struct ip6_moptions
*im6o
)
2193 struct in6_multi_mship
*imm
;
2198 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2199 LIST_REMOVE(imm
, i6mm_chain
);
2200 if (imm
->i6mm_maddr
)
2201 in6_delmulti(imm
->i6mm_maddr
);
2202 kfree(imm
, M_IPMADDR
);
2204 kfree(im6o
, M_IPMOPTS
);
2208 * Set IPv6 outgoing packet options based on advanced API.
2211 ip6_setpktoptions(struct mbuf
*control
, struct ip6_pktopts
*opt
, int priv
,
2214 struct cmsghdr
*cm
= NULL
;
2216 if (control
== NULL
|| opt
== NULL
)
2219 init_ip6pktopts(opt
);
2222 * XXX: Currently, we assume all the optional information is stored
2225 if (control
->m_next
)
2228 for (; control
->m_len
; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2229 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2230 cm
= mtod(control
, struct cmsghdr
*);
2231 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2233 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2237 * XXX should check if RFC2292 API is mixed with 2292bis API
2239 switch (cm
->cmsg_type
) {
2241 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(struct in6_pktinfo
)))
2244 /* XXX: Is it really WAITOK? */
2245 opt
->ip6po_pktinfo
=
2246 kmalloc(sizeof(struct in6_pktinfo
),
2247 M_IP6OPT
, M_WAITOK
);
2248 bcopy(CMSG_DATA(cm
), opt
->ip6po_pktinfo
,
2249 sizeof(struct in6_pktinfo
));
2251 opt
->ip6po_pktinfo
=
2252 (struct in6_pktinfo
*)CMSG_DATA(cm
);
2253 if (opt
->ip6po_pktinfo
->ipi6_ifindex
&&
2254 IN6_IS_ADDR_LINKLOCAL(&opt
->ip6po_pktinfo
->ipi6_addr
))
2255 opt
->ip6po_pktinfo
->ipi6_addr
.s6_addr16
[1] =
2256 htons(opt
->ip6po_pktinfo
->ipi6_ifindex
);
2258 if (opt
->ip6po_pktinfo
->ipi6_ifindex
> if_index
2259 || opt
->ip6po_pktinfo
->ipi6_ifindex
< 0) {
2264 * Check if the requested source address is indeed a
2265 * unicast address assigned to the node, and can be
2266 * used as the packet's source address.
2268 if (!IN6_IS_ADDR_UNSPECIFIED(&opt
->ip6po_pktinfo
->ipi6_addr
)) {
2269 struct in6_ifaddr
*ia6
;
2270 struct sockaddr_in6 sin6
;
2272 bzero(&sin6
, sizeof(sin6
));
2273 sin6
.sin6_len
= sizeof(sin6
);
2274 sin6
.sin6_family
= AF_INET6
;
2276 opt
->ip6po_pktinfo
->ipi6_addr
;
2277 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr(sin6tosa(&sin6
));
2279 (ia6
->ia6_flags
& (IN6_IFF_ANYCAST
|
2280 IN6_IFF_NOTREADY
)) != 0)
2281 return (EADDRNOTAVAIL
);
2286 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
2289 opt
->ip6po_hlim
= *(int *)CMSG_DATA(cm
);
2290 if (opt
->ip6po_hlim
< -1 || opt
->ip6po_hlim
> 255)
2298 if (cm
->cmsg_len
< sizeof(u_char
) ||
2299 /* check if cmsg_len is large enough for sa_len */
2300 cm
->cmsg_len
< CMSG_LEN(*CMSG_DATA(cm
)))
2304 opt
->ip6po_nexthop
=
2305 kmalloc(*CMSG_DATA(cm
),
2306 M_IP6OPT
, M_WAITOK
);
2307 bcopy(CMSG_DATA(cm
),
2311 opt
->ip6po_nexthop
=
2312 (struct sockaddr
*)CMSG_DATA(cm
);
2317 struct ip6_hbh
*hbh
;
2320 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_hbh
)))
2322 hbh
= (struct ip6_hbh
*)CMSG_DATA(cm
);
2323 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
2324 if (cm
->cmsg_len
!= CMSG_LEN(hbhlen
))
2329 kmalloc(hbhlen
, M_IP6OPT
, M_WAITOK
);
2330 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
2332 opt
->ip6po_hbh
= hbh
;
2338 struct ip6_dest
*dest
, **newdest
;
2341 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_dest
)))
2343 dest
= (struct ip6_dest
*)CMSG_DATA(cm
);
2344 destlen
= (dest
->ip6d_len
+ 1) << 3;
2345 if (cm
->cmsg_len
!= CMSG_LEN(destlen
))
2349 * The old advacned API is ambiguous on this
2350 * point. Our approach is to determine the
2351 * position based according to the existence
2352 * of a routing header. Note, however, that
2353 * this depends on the order of the extension
2354 * headers in the ancillary data; the 1st part
2355 * of the destination options header must
2356 * appear before the routing header in the
2357 * ancillary data, too.
2358 * RFC2292bis solved the ambiguity by
2359 * introducing separate cmsg types.
2361 if (opt
->ip6po_rthdr
== NULL
)
2362 newdest
= &opt
->ip6po_dest1
;
2364 newdest
= &opt
->ip6po_dest2
;
2367 *newdest
= kmalloc(destlen
, M_IP6OPT
, M_WAITOK
);
2368 bcopy(dest
, *newdest
, destlen
);
2377 struct ip6_rthdr
*rth
;
2380 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_rthdr
)))
2382 rth
= (struct ip6_rthdr
*)CMSG_DATA(cm
);
2383 rthlen
= (rth
->ip6r_len
+ 1) << 3;
2384 if (cm
->cmsg_len
!= CMSG_LEN(rthlen
))
2387 switch (rth
->ip6r_type
) {
2388 case IPV6_RTHDR_TYPE_0
:
2389 /* must contain one addr */
2390 if (rth
->ip6r_len
== 0)
2392 /* length must be even */
2393 if (rth
->ip6r_len
% 2)
2395 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
2399 return (EINVAL
); /* not supported */
2403 opt
->ip6po_rthdr
= kmalloc(rthlen
, M_IP6OPT
,
2405 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
2407 opt
->ip6po_rthdr
= rth
;
2413 return (ENOPROTOOPT
);
2421 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2422 * packet to the input queue of a specified interface. Note that this
2423 * calls the output routine of the loopback "driver", but with an interface
2424 * pointer that might NOT be &loif -- easier than replicating that code here.
2427 ip6_mloopback(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr_in6
*dst
)
2430 struct ip6_hdr
*ip6
;
2432 copym
= m_copy(m
, 0, M_COPYALL
);
2437 * Make sure to deep-copy IPv6 header portion in case the data
2438 * is in an mbuf cluster, so that we can safely override the IPv6
2439 * header portion later.
2441 if ((copym
->m_flags
& M_EXT
) != 0 ||
2442 copym
->m_len
< sizeof(struct ip6_hdr
)) {
2443 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
2449 if (copym
->m_len
< sizeof(*ip6
)) {
2455 ip6
= mtod(copym
, struct ip6_hdr
*);
2457 * clear embedded scope identifiers if necessary.
2458 * in6_clearscope will touch the addresses only when necessary.
2460 in6_clearscope(&ip6
->ip6_src
);
2461 in6_clearscope(&ip6
->ip6_dst
);
2463 if_simloop(ifp
, copym
, dst
->sin6_family
, 0);
2467 * Separate the IPv6 header from the payload into its own mbuf.
2469 * Returns the new mbuf chain or the original mbuf if no payload.
2470 * Returns NULL if can't allocate new mbuf for header.
2472 static struct mbuf
*
2473 ip6_splithdr(struct mbuf
*m
)
2477 if (m
->m_len
<= sizeof(struct ip6_hdr
)) /* no payload */
2480 MGETHDR(mh
, MB_DONTWAIT
, MT_HEADER
);
2483 mh
->m_len
= sizeof(struct ip6_hdr
);
2484 M_MOVE_PKTHDR(mh
, m
);
2485 MH_ALIGN(mh
, sizeof(struct ip6_hdr
));
2486 bcopy(mtod(m
, caddr_t
), mtod(mh
, caddr_t
), sizeof(struct ip6_hdr
));
2487 m
->m_data
+= sizeof(struct ip6_hdr
);
2488 m
->m_len
-= sizeof(struct ip6_hdr
);
2494 * Compute IPv6 extension header length.
2497 ip6_optlen(struct in6pcb
*in6p
)
2501 if (!in6p
->in6p_outputopts
)
2506 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2508 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
2509 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
2510 /* dest1 is valid with rthdr only */
2511 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
2512 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
2513 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);