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.33 2008/01/06 16:55:52 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 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1303 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1305 m_freem(m
); /* XXX */
1310 * Use of some Hop-by-Hop options or some
1311 * Destination options, might require special
1312 * privilege. That is, normal applications
1313 * (without special privilege) might be forbidden
1314 * from setting certain options in outgoing packets,
1315 * and might never see certain options in received
1316 * packets. [RFC 2292 Section 6]
1317 * KAME specific note:
1318 * KAME prevents non-privileged users from sending or
1319 * receiving ANY hbh/dst options in order to avoid
1320 * overhead of parsing options in the kernel.
1322 case IPV6_UNICAST_HOPS
:
1327 if (optlen
!= sizeof(int)) {
1331 error
= sooptcopyin(sopt
, &optval
,
1332 sizeof optval
, sizeof optval
);
1337 case IPV6_UNICAST_HOPS
:
1338 if (optval
< -1 || optval
>= 256)
1341 /* -1 = kernel default */
1342 in6p
->in6p_hops
= optval
;
1344 if ((in6p
->in6p_vflag
&
1346 in6p
->inp_ip_ttl
= optval
;
1349 #define OPTSET(bit) \
1352 in6p->in6p_flags |= (bit); \
1354 in6p->in6p_flags &= ~(bit); \
1356 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1359 in6p
->in6p_cksum
= optval
;
1368 * make setsockopt(IPV6_V6ONLY)
1369 * available only prior to bind(2).
1370 * see ipng mailing list, Jun 22 2001.
1372 if (in6p
->in6p_lport
||
1373 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
))
1378 OPTSET(IN6P_IPV6_V6ONLY
);
1380 in6p
->in6p_vflag
&= ~INP_IPV4
;
1382 in6p
->in6p_vflag
|= INP_IPV4
;
1393 if (optlen
!= sizeof(int)) {
1397 error
= sooptcopyin(sopt
, &optval
,
1398 sizeof optval
, sizeof optval
);
1403 OPTSET(IN6P_PKTINFO
);
1406 OPTSET(IN6P_HOPLIMIT
);
1410 * Check super-user privilege.
1411 * See comments for IPV6_RECVHOPOPTS.
1415 OPTSET(IN6P_HOPOPTS
);
1420 OPTSET(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1429 case IPV6_MULTICAST_IF
:
1430 case IPV6_MULTICAST_HOPS
:
1431 case IPV6_MULTICAST_LOOP
:
1432 case IPV6_JOIN_GROUP
:
1433 case IPV6_LEAVE_GROUP
:
1436 if (sopt
->sopt_valsize
> MLEN
) {
1441 MGET(m
, sopt
->sopt_td
? MB_WAIT
: MB_DONTWAIT
, MT_HEADER
);
1446 m
->m_len
= sopt
->sopt_valsize
;
1447 error
= sooptcopyin(sopt
, mtod(m
, char *),
1448 m
->m_len
, m
->m_len
);
1449 error
= ip6_setmoptions(sopt
->sopt_name
,
1450 &in6p
->in6p_moptions
,
1456 case IPV6_PORTRANGE
:
1457 error
= sooptcopyin(sopt
, &optval
,
1458 sizeof optval
, sizeof optval
);
1463 case IPV6_PORTRANGE_DEFAULT
:
1464 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1465 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1468 case IPV6_PORTRANGE_HIGH
:
1469 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1470 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1473 case IPV6_PORTRANGE_LOW
:
1474 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1475 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1484 #if defined(IPSEC) || defined(FAST_IPSEC)
1485 case IPV6_IPSEC_POLICY
:
1491 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1493 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1496 req
= mtod(m
, caddr_t
);
1499 error
= ipsec6_set_policy(in6p
, optname
, req
,
1504 #endif /* KAME IPSEC */
1512 struct mbuf
**mp
= &m
;
1514 if (ip6_fw_ctl_ptr
== NULL
)
1517 if ((error
= soopt_getm(sopt
, &m
)) != 0)
1520 if ((error
= soopt_mcopyin(sopt
, m
)) != 0)
1522 error
= (*ip6_fw_ctl_ptr
)(optname
, mp
);
1528 error
= ENOPROTOOPT
;
1536 case IPV6_PKTOPTIONS
:
1537 if (in6p
->in6p_options
) {
1539 m
= m_copym(in6p
->in6p_options
,
1540 0, M_COPYALL
, MB_WAIT
);
1541 error
= soopt_mcopyout(sopt
, m
);
1545 sopt
->sopt_valsize
= 0;
1548 case IPV6_UNICAST_HOPS
:
1553 case IPV6_PORTRANGE
:
1556 case IPV6_UNICAST_HOPS
:
1557 optval
= in6p
->in6p_hops
;
1561 optval
= in6p
->in6p_cksum
;
1565 optval
= OPTBIT(IN6P_FAITH
);
1569 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1572 case IPV6_PORTRANGE
:
1575 flags
= in6p
->in6p_flags
;
1576 if (flags
& IN6P_HIGHPORT
)
1577 optval
= IPV6_PORTRANGE_HIGH
;
1578 else if (flags
& IN6P_LOWPORT
)
1579 optval
= IPV6_PORTRANGE_LOW
;
1585 error
= sooptcopyout(sopt
, &optval
,
1594 if (optname
== IPV6_HOPOPTS
||
1595 optname
== IPV6_DSTOPTS
||
1600 optval
= OPTBIT(IN6P_PKTINFO
);
1603 optval
= OPTBIT(IN6P_HOPLIMIT
);
1608 optval
= OPTBIT(IN6P_HOPOPTS
);
1611 optval
= OPTBIT(IN6P_RTHDR
);
1616 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1619 error
= sooptcopyout(sopt
, &optval
,
1623 case IPV6_MULTICAST_IF
:
1624 case IPV6_MULTICAST_HOPS
:
1625 case IPV6_MULTICAST_LOOP
:
1626 case IPV6_JOIN_GROUP
:
1627 case IPV6_LEAVE_GROUP
:
1630 error
= ip6_getmoptions(sopt
->sopt_name
,
1631 in6p
->in6p_moptions
, &m
);
1633 error
= sooptcopyout(sopt
,
1634 mtod(m
, char *), m
->m_len
);
1639 #if defined(IPSEC) || defined(FAST_IPSEC)
1640 case IPV6_IPSEC_POLICY
:
1644 struct mbuf
*m
= NULL
;
1645 struct mbuf
**mp
= &m
;
1647 error
= soopt_getm(sopt
, &m
); /* XXX */
1650 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1654 req
= mtod(m
, caddr_t
);
1657 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
1659 error
= soopt_mcopyout(sopt
, m
); /*XXX*/
1660 if (error
== 0 && m
!= NULL
)
1664 #endif /* KAME IPSEC */
1669 struct mbuf
**mp
= &m
;
1671 if (ip6_fw_ctl_ptr
== NULL
)
1675 error
= (*ip6_fw_ctl_ptr
)(optname
, mp
);
1677 error
= soopt_mcopyout(sopt
, m
); /* XXX */
1678 if (error
== 0 && m
!= NULL
)
1684 error
= ENOPROTOOPT
;
1696 * Set up IP6 options in pcb for insertion in output packets or
1697 * specifying behavior of outgoing packets.
1700 ip6_pcbopts(struct ip6_pktopts
**pktopt
, struct mbuf
*m
, struct socket
*so
,
1701 struct sockopt
*sopt
)
1703 struct ip6_pktopts
*opt
= *pktopt
;
1705 struct thread
*td
= sopt
->sopt_td
;
1708 /* turn off any old options. */
1711 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
1712 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
1713 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1714 kprintf("ip6_pcbopts: all specified options are cleared.\n");
1716 ip6_clearpktopts(opt
, 1, -1);
1718 opt
= kmalloc(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
1721 if (m
== NULL
|| m
->m_len
== 0) {
1723 * Only turning off any previous options, regardless of
1724 * whether the opt is just created or given.
1726 kfree(opt
, M_IP6OPT
);
1730 /* set options specified by user. */
1733 if ((error
= ip6_setpktoptions(m
, opt
, priv
, 1)) != 0) {
1734 ip6_clearpktopts(opt
, 1, -1); /* XXX: discard all options */
1735 kfree(opt
, M_IP6OPT
);
1743 * initialize ip6_pktopts. beware that there are non-zero default values in
1747 init_ip6pktopts(struct ip6_pktopts
*opt
)
1750 bzero(opt
, sizeof(*opt
));
1751 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
1755 ip6_clearpktopts(struct ip6_pktopts
*pktopt
, int needfree
, int optname
)
1760 if (optname
== -1) {
1761 if (needfree
&& pktopt
->ip6po_pktinfo
)
1762 kfree(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
1763 pktopt
->ip6po_pktinfo
= NULL
;
1766 pktopt
->ip6po_hlim
= -1;
1767 if (optname
== -1) {
1768 if (needfree
&& pktopt
->ip6po_nexthop
)
1769 kfree(pktopt
->ip6po_nexthop
, M_IP6OPT
);
1770 pktopt
->ip6po_nexthop
= NULL
;
1772 if (optname
== -1) {
1773 if (needfree
&& pktopt
->ip6po_hbh
)
1774 kfree(pktopt
->ip6po_hbh
, M_IP6OPT
);
1775 pktopt
->ip6po_hbh
= NULL
;
1777 if (optname
== -1) {
1778 if (needfree
&& pktopt
->ip6po_dest1
)
1779 kfree(pktopt
->ip6po_dest1
, M_IP6OPT
);
1780 pktopt
->ip6po_dest1
= NULL
;
1782 if (optname
== -1) {
1783 if (needfree
&& pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1784 kfree(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
1785 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
1786 if (pktopt
->ip6po_route
.ro_rt
) {
1787 RTFREE(pktopt
->ip6po_route
.ro_rt
);
1788 pktopt
->ip6po_route
.ro_rt
= NULL
;
1791 if (optname
== -1) {
1792 if (needfree
&& pktopt
->ip6po_dest2
)
1793 kfree(pktopt
->ip6po_dest2
, M_IP6OPT
);
1794 pktopt
->ip6po_dest2
= NULL
;
1798 #define PKTOPT_EXTHDRCPY(type) \
1802 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1803 dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
1804 if (dst->type == NULL)\
1806 bcopy(src->type, dst->type, hlen);\
1810 struct ip6_pktopts
*
1811 ip6_copypktopts(struct ip6_pktopts
*src
, int canwait
)
1813 struct ip6_pktopts
*dst
;
1816 kprintf("ip6_clearpktopts: invalid argument\n");
1820 dst
= kmalloc(sizeof(*dst
), M_IP6OPT
, canwait
| M_ZERO
);
1824 dst
->ip6po_hlim
= src
->ip6po_hlim
;
1825 if (src
->ip6po_pktinfo
) {
1826 dst
->ip6po_pktinfo
= kmalloc(sizeof(*dst
->ip6po_pktinfo
),
1828 if (dst
->ip6po_pktinfo
== NULL
)
1830 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
1832 if (src
->ip6po_nexthop
) {
1833 dst
->ip6po_nexthop
= kmalloc(src
->ip6po_nexthop
->sa_len
,
1835 if (dst
->ip6po_nexthop
== NULL
)
1837 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
1838 src
->ip6po_nexthop
->sa_len
);
1840 PKTOPT_EXTHDRCPY(ip6po_hbh
);
1841 PKTOPT_EXTHDRCPY(ip6po_dest1
);
1842 PKTOPT_EXTHDRCPY(ip6po_dest2
);
1843 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
1847 if (dst
->ip6po_pktinfo
) kfree(dst
->ip6po_pktinfo
, M_IP6OPT
);
1848 if (dst
->ip6po_nexthop
) kfree(dst
->ip6po_nexthop
, M_IP6OPT
);
1849 if (dst
->ip6po_hbh
) kfree(dst
->ip6po_hbh
, M_IP6OPT
);
1850 if (dst
->ip6po_dest1
) kfree(dst
->ip6po_dest1
, M_IP6OPT
);
1851 if (dst
->ip6po_dest2
) kfree(dst
->ip6po_dest2
, M_IP6OPT
);
1852 if (dst
->ip6po_rthdr
) kfree(dst
->ip6po_rthdr
, M_IP6OPT
);
1853 kfree(dst
, M_IP6OPT
);
1856 #undef PKTOPT_EXTHDRCPY
1859 ip6_freepcbopts(struct ip6_pktopts
*pktopt
)
1864 ip6_clearpktopts(pktopt
, 1, -1);
1866 kfree(pktopt
, M_IP6OPT
);
1870 * Set the IP6 multicast options in response to user setsockopt().
1873 ip6_setmoptions(int optname
, struct ip6_moptions
**im6op
, struct mbuf
*m
)
1876 u_int loop
, ifindex
;
1877 struct ipv6_mreq
*mreq
;
1879 struct ip6_moptions
*im6o
= *im6op
;
1880 struct route_in6 ro
;
1881 struct sockaddr_in6
*dst
;
1882 struct in6_multi_mship
*imm
;
1883 struct thread
*td
= curthread
; /* XXX */
1887 * No multicast option buffer attached to the pcb;
1888 * allocate one and initialize to default values.
1890 im6o
= (struct ip6_moptions
*)
1891 kmalloc(sizeof(*im6o
), M_IPMOPTS
, M_WAITOK
);
1894 im6o
->im6o_multicast_ifp
= NULL
;
1895 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1896 im6o
->im6o_multicast_loop
= IPV6_DEFAULT_MULTICAST_LOOP
;
1897 LIST_INIT(&im6o
->im6o_memberships
);
1902 case IPV6_MULTICAST_IF
:
1904 * Select the interface for outgoing multicast packets.
1906 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
1910 bcopy(mtod(m
, u_int
*), &ifindex
, sizeof(ifindex
));
1911 if (ifindex
< 0 || if_index
< ifindex
) {
1912 error
= ENXIO
; /* XXX EINVAL? */
1915 ifp
= ifindex2ifnet
[ifindex
];
1916 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
1917 error
= EADDRNOTAVAIL
;
1920 im6o
->im6o_multicast_ifp
= ifp
;
1923 case IPV6_MULTICAST_HOPS
:
1926 * Set the IP6 hoplimit for outgoing multicast packets.
1929 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
1933 bcopy(mtod(m
, u_int
*), &optval
, sizeof(optval
));
1934 if (optval
< -1 || optval
>= 256)
1936 else if (optval
== -1)
1937 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1939 im6o
->im6o_multicast_hlim
= optval
;
1943 case IPV6_MULTICAST_LOOP
:
1945 * Set the loopback flag for outgoing multicast packets.
1946 * Must be zero or one.
1948 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
1952 bcopy(mtod(m
, u_int
*), &loop
, sizeof(loop
));
1957 im6o
->im6o_multicast_loop
= loop
;
1960 case IPV6_JOIN_GROUP
:
1962 * Add a multicast group membership.
1963 * Group must be a valid IP6 multicast address.
1965 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
1969 mreq
= mtod(m
, struct ipv6_mreq
*);
1970 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
1972 * We use the unspecified address to specify to accept
1973 * all multicast addresses. Only super user is allowed
1981 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
1987 * If the interface is specified, validate it.
1989 if (mreq
->ipv6mr_interface
< 0
1990 || if_index
< mreq
->ipv6mr_interface
) {
1991 error
= ENXIO
; /* XXX EINVAL? */
1995 * If no interface was explicitly specified, choose an
1996 * appropriate one according to the given multicast address.
1998 if (mreq
->ipv6mr_interface
== 0) {
2000 * If the multicast address is in node-local scope,
2001 * the interface should be a loopback interface.
2002 * Otherwise, look up the routing table for the
2003 * address, and choose the outgoing interface.
2004 * XXX: is it a good approach?
2006 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq
->ipv6mr_multiaddr
)) {
2010 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
2011 bzero(dst
, sizeof(*dst
));
2012 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2013 dst
->sin6_family
= AF_INET6
;
2014 dst
->sin6_addr
= mreq
->ipv6mr_multiaddr
;
2015 rtalloc((struct route
*)&ro
);
2016 if (ro
.ro_rt
== NULL
) {
2017 error
= EADDRNOTAVAIL
;
2020 ifp
= ro
.ro_rt
->rt_ifp
;
2024 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2027 * See if we found an interface, and confirm that it
2028 * supports multicast
2030 if (ifp
== NULL
|| !(ifp
->if_flags
& IFF_MULTICAST
)) {
2031 error
= EADDRNOTAVAIL
;
2035 * Put interface index into the multicast address,
2036 * if the address has link-local scope.
2038 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2039 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2040 = htons(mreq
->ipv6mr_interface
);
2043 * See if the membership already exists.
2045 for (imm
= im6o
->im6o_memberships
.lh_first
;
2046 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2047 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2048 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2049 &mreq
->ipv6mr_multiaddr
))
2056 * Everything looks good; add a new record to the multicast
2057 * address list for the given interface.
2059 imm
= kmalloc(sizeof(*imm
), M_IPMADDR
, M_WAITOK
);
2060 if ((imm
->i6mm_maddr
=
2061 in6_addmulti(&mreq
->ipv6mr_multiaddr
, ifp
, &error
)) == NULL
) {
2062 kfree(imm
, M_IPMADDR
);
2065 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2068 case IPV6_LEAVE_GROUP
:
2070 * Drop a multicast group membership.
2071 * Group must be a valid IP6 multicast address.
2073 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2077 mreq
= mtod(m
, struct ipv6_mreq
*);
2078 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2083 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2088 * If an interface address was specified, get a pointer
2089 * to its ifnet structure.
2091 if (mreq
->ipv6mr_interface
< 0
2092 || if_index
< mreq
->ipv6mr_interface
) {
2093 error
= ENXIO
; /* XXX EINVAL? */
2096 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2098 * Put interface index into the multicast address,
2099 * if the address has link-local scope.
2101 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2102 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2103 = htons(mreq
->ipv6mr_interface
);
2106 * Find the membership in the membership list.
2108 for (imm
= im6o
->im6o_memberships
.lh_first
;
2109 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2111 imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2112 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2113 &mreq
->ipv6mr_multiaddr
))
2117 /* Unable to resolve interface */
2118 error
= EADDRNOTAVAIL
;
2122 * Give up the multicast address record to which the
2123 * membership points.
2125 LIST_REMOVE(imm
, i6mm_chain
);
2126 in6_delmulti(imm
->i6mm_maddr
);
2127 kfree(imm
, M_IPMADDR
);
2136 * If all options have default values, no need to keep the mbuf.
2138 if (im6o
->im6o_multicast_ifp
== NULL
&&
2139 im6o
->im6o_multicast_hlim
== ip6_defmcasthlim
&&
2140 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2141 im6o
->im6o_memberships
.lh_first
== NULL
) {
2142 kfree(*im6op
, M_IPMOPTS
);
2150 * Return the IP6 multicast options in response to user getsockopt().
2153 ip6_getmoptions(int optname
, struct ip6_moptions
*im6o
, struct mbuf
**mp
)
2155 u_int
*hlim
, *loop
, *ifindex
;
2157 *mp
= m_get(MB_WAIT
, MT_HEADER
); /* XXX */
2161 case IPV6_MULTICAST_IF
:
2162 ifindex
= mtod(*mp
, u_int
*);
2163 (*mp
)->m_len
= sizeof(u_int
);
2164 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2167 *ifindex
= im6o
->im6o_multicast_ifp
->if_index
;
2170 case IPV6_MULTICAST_HOPS
:
2171 hlim
= mtod(*mp
, u_int
*);
2172 (*mp
)->m_len
= sizeof(u_int
);
2174 *hlim
= ip6_defmcasthlim
;
2176 *hlim
= im6o
->im6o_multicast_hlim
;
2179 case IPV6_MULTICAST_LOOP
:
2180 loop
= mtod(*mp
, u_int
*);
2181 (*mp
)->m_len
= sizeof(u_int
);
2183 *loop
= ip6_defmcasthlim
;
2185 *loop
= im6o
->im6o_multicast_loop
;
2189 return (EOPNOTSUPP
);
2194 * Discard the IP6 multicast options.
2197 ip6_freemoptions(struct ip6_moptions
*im6o
)
2199 struct in6_multi_mship
*imm
;
2204 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2205 LIST_REMOVE(imm
, i6mm_chain
);
2206 if (imm
->i6mm_maddr
)
2207 in6_delmulti(imm
->i6mm_maddr
);
2208 kfree(imm
, M_IPMADDR
);
2210 kfree(im6o
, M_IPMOPTS
);
2214 * Set IPv6 outgoing packet options based on advanced API.
2217 ip6_setpktoptions(struct mbuf
*control
, struct ip6_pktopts
*opt
, int priv
,
2220 struct cmsghdr
*cm
= NULL
;
2222 if (control
== NULL
|| opt
== NULL
)
2225 init_ip6pktopts(opt
);
2228 * XXX: Currently, we assume all the optional information is stored
2231 if (control
->m_next
)
2234 for (; control
->m_len
; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2235 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2236 cm
= mtod(control
, struct cmsghdr
*);
2237 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2239 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2243 * XXX should check if RFC2292 API is mixed with 2292bis API
2245 switch (cm
->cmsg_type
) {
2247 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(struct in6_pktinfo
)))
2250 /* XXX: Is it really WAITOK? */
2251 opt
->ip6po_pktinfo
=
2252 kmalloc(sizeof(struct in6_pktinfo
),
2253 M_IP6OPT
, M_WAITOK
);
2254 bcopy(CMSG_DATA(cm
), opt
->ip6po_pktinfo
,
2255 sizeof(struct in6_pktinfo
));
2257 opt
->ip6po_pktinfo
=
2258 (struct in6_pktinfo
*)CMSG_DATA(cm
);
2259 if (opt
->ip6po_pktinfo
->ipi6_ifindex
&&
2260 IN6_IS_ADDR_LINKLOCAL(&opt
->ip6po_pktinfo
->ipi6_addr
))
2261 opt
->ip6po_pktinfo
->ipi6_addr
.s6_addr16
[1] =
2262 htons(opt
->ip6po_pktinfo
->ipi6_ifindex
);
2264 if (opt
->ip6po_pktinfo
->ipi6_ifindex
> if_index
2265 || opt
->ip6po_pktinfo
->ipi6_ifindex
< 0) {
2270 * Check if the requested source address is indeed a
2271 * unicast address assigned to the node, and can be
2272 * used as the packet's source address.
2274 if (!IN6_IS_ADDR_UNSPECIFIED(&opt
->ip6po_pktinfo
->ipi6_addr
)) {
2275 struct in6_ifaddr
*ia6
;
2276 struct sockaddr_in6 sin6
;
2278 bzero(&sin6
, sizeof(sin6
));
2279 sin6
.sin6_len
= sizeof(sin6
);
2280 sin6
.sin6_family
= AF_INET6
;
2282 opt
->ip6po_pktinfo
->ipi6_addr
;
2283 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr(sin6tosa(&sin6
));
2285 (ia6
->ia6_flags
& (IN6_IFF_ANYCAST
|
2286 IN6_IFF_NOTREADY
)) != 0)
2287 return (EADDRNOTAVAIL
);
2292 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
2295 opt
->ip6po_hlim
= *(int *)CMSG_DATA(cm
);
2296 if (opt
->ip6po_hlim
< -1 || opt
->ip6po_hlim
> 255)
2304 if (cm
->cmsg_len
< sizeof(u_char
) ||
2305 /* check if cmsg_len is large enough for sa_len */
2306 cm
->cmsg_len
< CMSG_LEN(*CMSG_DATA(cm
)))
2310 opt
->ip6po_nexthop
=
2311 kmalloc(*CMSG_DATA(cm
),
2312 M_IP6OPT
, M_WAITOK
);
2313 bcopy(CMSG_DATA(cm
),
2317 opt
->ip6po_nexthop
=
2318 (struct sockaddr
*)CMSG_DATA(cm
);
2323 struct ip6_hbh
*hbh
;
2326 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_hbh
)))
2328 hbh
= (struct ip6_hbh
*)CMSG_DATA(cm
);
2329 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
2330 if (cm
->cmsg_len
!= CMSG_LEN(hbhlen
))
2335 kmalloc(hbhlen
, M_IP6OPT
, M_WAITOK
);
2336 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
2338 opt
->ip6po_hbh
= hbh
;
2344 struct ip6_dest
*dest
, **newdest
;
2347 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_dest
)))
2349 dest
= (struct ip6_dest
*)CMSG_DATA(cm
);
2350 destlen
= (dest
->ip6d_len
+ 1) << 3;
2351 if (cm
->cmsg_len
!= CMSG_LEN(destlen
))
2355 * The old advacned API is ambiguous on this
2356 * point. Our approach is to determine the
2357 * position based according to the existence
2358 * of a routing header. Note, however, that
2359 * this depends on the order of the extension
2360 * headers in the ancillary data; the 1st part
2361 * of the destination options header must
2362 * appear before the routing header in the
2363 * ancillary data, too.
2364 * RFC2292bis solved the ambiguity by
2365 * introducing separate cmsg types.
2367 if (opt
->ip6po_rthdr
== NULL
)
2368 newdest
= &opt
->ip6po_dest1
;
2370 newdest
= &opt
->ip6po_dest2
;
2373 *newdest
= kmalloc(destlen
, M_IP6OPT
, M_WAITOK
);
2374 bcopy(dest
, *newdest
, destlen
);
2383 struct ip6_rthdr
*rth
;
2386 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_rthdr
)))
2388 rth
= (struct ip6_rthdr
*)CMSG_DATA(cm
);
2389 rthlen
= (rth
->ip6r_len
+ 1) << 3;
2390 if (cm
->cmsg_len
!= CMSG_LEN(rthlen
))
2393 switch (rth
->ip6r_type
) {
2394 case IPV6_RTHDR_TYPE_0
:
2395 /* must contain one addr */
2396 if (rth
->ip6r_len
== 0)
2398 /* length must be even */
2399 if (rth
->ip6r_len
% 2)
2401 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
2405 return (EINVAL
); /* not supported */
2409 opt
->ip6po_rthdr
= kmalloc(rthlen
, M_IP6OPT
,
2411 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
2413 opt
->ip6po_rthdr
= rth
;
2419 return (ENOPROTOOPT
);
2427 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2428 * packet to the input queue of a specified interface. Note that this
2429 * calls the output routine of the loopback "driver", but with an interface
2430 * pointer that might NOT be &loif -- easier than replicating that code here.
2433 ip6_mloopback(struct ifnet
*ifp
, struct mbuf
*m
, struct sockaddr_in6
*dst
)
2436 struct ip6_hdr
*ip6
;
2438 copym
= m_copy(m
, 0, M_COPYALL
);
2443 * Make sure to deep-copy IPv6 header portion in case the data
2444 * is in an mbuf cluster, so that we can safely override the IPv6
2445 * header portion later.
2447 if ((copym
->m_flags
& M_EXT
) != 0 ||
2448 copym
->m_len
< sizeof(struct ip6_hdr
)) {
2449 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
2455 if (copym
->m_len
< sizeof(*ip6
)) {
2461 ip6
= mtod(copym
, struct ip6_hdr
*);
2463 * clear embedded scope identifiers if necessary.
2464 * in6_clearscope will touch the addresses only when necessary.
2466 in6_clearscope(&ip6
->ip6_src
);
2467 in6_clearscope(&ip6
->ip6_dst
);
2469 if_simloop(ifp
, copym
, dst
->sin6_family
, NULL
);
2473 * Separate the IPv6 header from the payload into its own mbuf.
2475 * Returns the new mbuf chain or the original mbuf if no payload.
2476 * Returns NULL if can't allocate new mbuf for header.
2478 static struct mbuf
*
2479 ip6_splithdr(struct mbuf
*m
)
2483 if (m
->m_len
<= sizeof(struct ip6_hdr
)) /* no payload */
2486 MGETHDR(mh
, MB_DONTWAIT
, MT_HEADER
);
2489 mh
->m_len
= sizeof(struct ip6_hdr
);
2490 M_MOVE_PKTHDR(mh
, m
);
2491 MH_ALIGN(mh
, sizeof(struct ip6_hdr
));
2492 bcopy(mtod(m
, caddr_t
), mtod(mh
, caddr_t
), sizeof(struct ip6_hdr
));
2493 m
->m_data
+= sizeof(struct ip6_hdr
);
2494 m
->m_len
-= sizeof(struct ip6_hdr
);
2500 * Compute IPv6 extension header length.
2503 ip6_optlen(struct in6pcb
*in6p
)
2507 if (!in6p
->in6p_outputopts
)
2512 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2514 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
2515 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
2516 /* dest1 is valid with rthdr only */
2517 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
2518 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
2519 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);