1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.43 2002/10/31 19:45:48 ume Exp $ */
2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
69 #include <sys/param.h>
70 #include <sys/malloc.h>
72 #include <sys/errno.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
81 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/ip_var.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet/ip6.h>
88 #include <netinet/icmp6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet6/nd6.h>
94 #include <netinet6/ipsec.h>
96 #include <netinet6/ipsec6.h>
98 #include <netkey/key.h>
99 extern int ipsec_bypass
;
102 #include <netinet6/ip6_fw.h>
104 #include <net/net_osdep.h>
107 static MALLOC_DEFINE(M_IPMOPTS
, "ip6_moptions", "internet multicast options");
111 static u_long lo_dl_tag
= 0;
112 extern u_long route_generation
;
116 struct mbuf
*ip6e_ip6
;
117 struct mbuf
*ip6e_hbh
;
118 struct mbuf
*ip6e_dest1
;
119 struct mbuf
*ip6e_rthdr
;
120 struct mbuf
*ip6e_dest2
;
123 static int ip6_pcbopts
__P((struct ip6_pktopts
**, struct mbuf
*,
124 struct socket
*, struct sockopt
*sopt
));
125 static int ip6_setmoptions
__P((int, struct inpcb
*, struct mbuf
*));
126 static int ip6_getmoptions
__P((int, struct ip6_moptions
*, struct mbuf
**));
127 static int ip6_copyexthdr
__P((struct mbuf
**, caddr_t
, int));
128 static int ip6_insertfraghdr
__P((struct mbuf
*, struct mbuf
*, int,
129 struct ip6_frag
**));
130 static int ip6_insert_jumboopt
__P((struct ip6_exthdrs
*, u_int32_t
));
131 static int ip6_splithdr
__P((struct mbuf
*, struct ip6_exthdrs
*));
133 extern int ip_createmoptions(struct ip_moptions
**imop
);
134 extern int ip_addmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
135 extern int ip_dropmembership(struct ip_moptions
*imo
, struct ip_mreq
*mreq
);
138 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
139 * header (with pri, len, nxt, hlim, src, dst).
140 * This function may modify ver and hlim only.
141 * The mbuf chain containing the packet will be freed.
142 * The mbuf opt, if present, will not be freed.
144 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
145 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
146 * which is rt_rmx.rmx_mtu.
149 ip6_output(m0
, opt
, ro
, flags
, im6o
, ifpp
)
151 struct ip6_pktopts
*opt
;
152 struct route_in6
*ro
;
154 struct ip6_moptions
*im6o
;
155 struct ifnet
**ifpp
; /* XXX: just for statistics */
157 struct ip6_hdr
*ip6
, *mhip6
;
158 struct ifnet
*ifp
, *origifp
;
160 int hlen
, tlen
, len
, off
;
161 struct route_in6 ip6route
;
162 struct sockaddr_in6
*dst
;
164 struct in6_ifaddr
*ia
= NULL
;
166 u_int32_t optlen
= 0, plen
= 0, unfragpartlen
= 0;
167 struct ip6_exthdrs exthdrs
;
168 struct in6_addr finaldst
;
169 struct route_in6
*ro_pmtu
= NULL
;
173 int needipsectun
= 0;
174 struct socket
*so
= NULL
;
175 struct secpolicy
*sp
= NULL
;
177 /* for AH processing. stupid to have "socket" variable in IP layer... */
178 if (ipsec_bypass
== 0)
180 so
= ipsec_getsocket(m
);
181 (void)ipsec_setsocket(m
, NULL
);
183 ip6
= mtod(m
, struct ip6_hdr
*);
186 #define MAKE_EXTHDR(hp, mp) \
189 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
190 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
191 ((eh)->ip6e_len + 1) << 3); \
197 bzero(&exthdrs
, sizeof(exthdrs
));
200 /* Hop-by-Hop options header */
201 MAKE_EXTHDR(opt
->ip6po_hbh
, &exthdrs
.ip6e_hbh
);
202 /* Destination options header(1st part) */
203 MAKE_EXTHDR(opt
->ip6po_dest1
, &exthdrs
.ip6e_dest1
);
205 MAKE_EXTHDR(opt
->ip6po_rthdr
, &exthdrs
.ip6e_rthdr
);
206 /* Destination options header(2nd part) */
207 MAKE_EXTHDR(opt
->ip6po_dest2
, &exthdrs
.ip6e_dest2
);
211 if (ipsec_bypass
!= 0)
214 /* get a security policy for this packet */
216 sp
= ipsec6_getpolicybyaddr(m
, IPSEC_DIR_OUTBOUND
, 0, &error
);
218 sp
= ipsec6_getpolicybysock(m
, IPSEC_DIR_OUTBOUND
, so
, &error
);
221 ipsec6stat
.out_inval
++;
228 switch (sp
->policy
) {
229 case IPSEC_POLICY_DISCARD
:
231 * This packet is just discarded.
233 ipsec6stat
.out_polvio
++;
236 case IPSEC_POLICY_BYPASS
:
237 case IPSEC_POLICY_NONE
:
238 /* no need to do IPsec. */
242 case IPSEC_POLICY_IPSEC
:
243 if (sp
->req
== NULL
) {
244 /* acquire a policy */
245 error
= key_spdacquire(sp
);
251 case IPSEC_POLICY_ENTRUST
:
253 printf("ip6_output: Invalid policy found. %d\n", sp
->policy
);
259 * Calculate the total length of the extension header chain.
260 * Keep the length of the unfragmentable part for fragmentation.
263 if (exthdrs
.ip6e_hbh
) optlen
+= exthdrs
.ip6e_hbh
->m_len
;
264 if (exthdrs
.ip6e_dest1
) optlen
+= exthdrs
.ip6e_dest1
->m_len
;
265 if (exthdrs
.ip6e_rthdr
) optlen
+= exthdrs
.ip6e_rthdr
->m_len
;
266 unfragpartlen
= optlen
+ sizeof(struct ip6_hdr
);
267 /* NOTE: we don't add AH/ESP length here. do that later. */
268 if (exthdrs
.ip6e_dest2
) optlen
+= exthdrs
.ip6e_dest2
->m_len
;
271 * If we need IPsec, or there is at least one extension header,
272 * separate IP6 header from the payload.
274 if ((needipsec
|| optlen
) && !hdrsplit
) {
275 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
279 m
= exthdrs
.ip6e_ip6
;
284 ip6
= mtod(m
, struct ip6_hdr
*);
286 /* adjust mbuf packet header length */
287 m
->m_pkthdr
.len
+= optlen
;
288 plen
= m
->m_pkthdr
.len
- sizeof(*ip6
);
290 /* If this is a jumbo payload, insert a jumbo payload option. */
291 if (plen
> IPV6_MAXPACKET
) {
293 if ((error
= ip6_splithdr(m
, &exthdrs
)) != 0) {
297 m
= exthdrs
.ip6e_ip6
;
301 ip6
= mtod(m
, struct ip6_hdr
*);
302 if ((error
= ip6_insert_jumboopt(&exthdrs
, plen
)) != 0)
306 ip6
->ip6_plen
= htons(plen
);
309 * Concatenate headers and fill in next header fields.
310 * Here we have, on "m"
312 * and we insert headers accordingly. Finally, we should be getting:
313 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
315 * during the header composing process, "m" points to IPv6 header.
316 * "mprev" points to an extension header prior to esp.
319 u_char
*nexthdrp
= &ip6
->ip6_nxt
;
320 struct mbuf
*mprev
= m
;
323 * we treat dest2 specially. this makes IPsec processing
324 * much easier. the goal here is to make mprev point the
325 * mbuf prior to dest2.
327 * result: IPv6 dest2 payload
328 * m and mprev will point to IPv6 header.
330 if (exthdrs
.ip6e_dest2
) {
332 panic("assumption failed: hdr not split");
333 exthdrs
.ip6e_dest2
->m_next
= m
->m_next
;
334 m
->m_next
= exthdrs
.ip6e_dest2
;
335 *mtod(exthdrs
.ip6e_dest2
, u_char
*) = ip6
->ip6_nxt
;
336 ip6
->ip6_nxt
= IPPROTO_DSTOPTS
;
339 #define MAKE_CHAIN(m, mp, p, i)\
343 panic("assumption failed: hdr not split"); \
344 *mtod((m), u_char *) = *(p);\
346 p = mtod((m), u_char *);\
347 (m)->m_next = (mp)->m_next;\
353 * result: IPv6 hbh dest1 rthdr dest2 payload
354 * m will point to IPv6 header. mprev will point to the
355 * extension header prior to dest2 (rthdr in the above case).
357 MAKE_CHAIN(exthdrs
.ip6e_hbh
, mprev
,
358 nexthdrp
, IPPROTO_HOPOPTS
);
359 MAKE_CHAIN(exthdrs
.ip6e_dest1
, mprev
,
360 nexthdrp
, IPPROTO_DSTOPTS
);
361 MAKE_CHAIN(exthdrs
.ip6e_rthdr
, mprev
,
362 nexthdrp
, IPPROTO_ROUTING
);
369 * pointers after IPsec headers are not valid any more.
370 * other pointers need a great care too.
371 * (IPsec routines should not mangle mbufs prior to AH/ESP)
373 exthdrs
.ip6e_dest2
= NULL
;
376 struct ip6_rthdr
*rh
= NULL
;
378 struct ipsec_output_state state
;
380 if (exthdrs
.ip6e_rthdr
) {
381 rh
= mtod(exthdrs
.ip6e_rthdr
, struct ip6_rthdr
*);
382 segleft_org
= rh
->ip6r_segleft
;
383 rh
->ip6r_segleft
= 0;
386 bzero(&state
, sizeof(state
));
388 error
= ipsec6_output_trans(&state
, nexthdrp
, mprev
, sp
, flags
,
392 /* mbuf is already reclaimed in ipsec6_output_trans. */
402 printf("ip6_output (ipsec): error code %d\n", error
);
405 /* don't show these error codes to the user */
411 if (exthdrs
.ip6e_rthdr
) {
412 /* ah6_output doesn't modify mbuf chain */
413 rh
->ip6r_segleft
= segleft_org
;
421 * If there is a routing header, replace destination address field
422 * with the first hop of the routing header.
424 if (exthdrs
.ip6e_rthdr
) {
425 struct ip6_rthdr
*rh
=
426 (struct ip6_rthdr
*)(mtod(exthdrs
.ip6e_rthdr
,
427 struct ip6_rthdr
*));
428 struct ip6_rthdr0
*rh0
;
430 finaldst
= ip6
->ip6_dst
;
431 switch (rh
->ip6r_type
) {
432 case IPV6_RTHDR_TYPE_0
:
433 rh0
= (struct ip6_rthdr0
*)rh
;
434 ip6
->ip6_dst
= rh0
->ip6r0_addr
[0];
435 bcopy((caddr_t
)&rh0
->ip6r0_addr
[1],
436 (caddr_t
)&rh0
->ip6r0_addr
[0],
437 sizeof(struct in6_addr
)*(rh0
->ip6r0_segleft
- 1)
439 rh0
->ip6r0_addr
[rh0
->ip6r0_segleft
- 1] = finaldst
;
441 default: /* is it possible? */
447 /* Source address validation */
448 if (IN6_IS_ADDR_UNSPECIFIED(&ip6
->ip6_src
) &&
449 (flags
& IPV6_DADOUTPUT
) == 0) {
451 ip6stat
.ip6s_badscope
++;
454 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_src
)) {
456 ip6stat
.ip6s_badscope
++;
460 ip6stat
.ip6s_localout
++;
467 bzero((caddr_t
)ro
, sizeof(*ro
));
470 if (opt
&& opt
->ip6po_rthdr
)
471 ro
= &opt
->ip6po_route
;
472 dst
= (struct sockaddr_in6
*)&ro
->ro_dst
;
474 * If there is a cached route,
475 * check that it is to the same destination
476 * and is still up. If not, free it and try again.
478 if (ro
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
479 dst
->sin6_family
!= AF_INET6
||
480 !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
) ||
481 ro
->ro_rt
->generation_id
!= route_generation
)) {
483 ro
->ro_rt
= (struct rtentry
*)0;
485 if (ro
->ro_rt
== 0) {
486 bzero(dst
, sizeof(*dst
));
487 dst
->sin6_family
= AF_INET6
;
488 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
489 dst
->sin6_addr
= ip6
->ip6_dst
;
491 /* XXX: sin6_scope_id should already be fixed at this point */
492 if (IN6_IS_SCOPE_LINKLOCAL(&dst
->sin6_addr
))
493 dst
->sin6_scope_id
= ntohs(dst
->sin6_addr
.s6_addr16
[1]);
497 if (needipsec
&& needipsectun
) {
498 struct ipsec_output_state state
;
501 * All the extension headers will become inaccessible
502 * (since they can be encrypted).
503 * Don't panic, we need no more updates to extension headers
504 * on inner IPv6 packet (since they are now encapsulated).
506 * IPv6 [ESP|AH] IPv6 [extension headers] payload
508 bzero(&exthdrs
, sizeof(exthdrs
));
509 exthdrs
.ip6e_ip6
= m
;
511 bzero(&state
, sizeof(state
));
513 state
.ro
= (struct route
*)ro
;
514 state
.dst
= (struct sockaddr
*)dst
;
516 error
= ipsec6_output_tunnel(&state
, sp
, flags
);
519 ro
= (struct route_in6
*)state
.ro
;
520 dst
= (struct sockaddr_in6
*)state
.dst
;
522 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
533 printf("ip6_output (ipsec): error code %d\n", error
);
536 /* don't show these error codes to the user */
543 exthdrs
.ip6e_ip6
= m
;
547 if (!IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
550 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
551 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
553 * interface selection comes here
554 * if an interface is specified from an upper layer,
557 if (ro
->ro_rt
== 0) {
559 * non-bsdi always clone routes, if parent is
562 rtalloc((struct route
*)ro
);
564 if (ro
->ro_rt
== 0) {
565 ip6stat
.ip6s_noroute
++;
566 error
= EHOSTUNREACH
;
567 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
570 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
571 ifp
= ro
->ro_rt
->rt_ifp
;
573 if (ro
->ro_rt
->rt_flags
& RTF_GATEWAY
)
574 dst
= (struct sockaddr_in6
*)ro
->ro_rt
->rt_gateway
;
575 m
->m_flags
&= ~(M_BCAST
| M_MCAST
); /* just in case */
577 in6_ifstat_inc(ifp
, ifs6_out_request
);
580 * Check if the outgoing interface conflicts with
581 * the interface specified by ifi6_ifindex (if specified).
582 * Note that loopback interface is always okay.
583 * (this may happen when we are sending a packet to one of
584 * our own addresses.)
586 if (opt
&& opt
->ip6po_pktinfo
587 && opt
->ip6po_pktinfo
->ipi6_ifindex
) {
588 if (!(ifp
->if_flags
& IFF_LOOPBACK
)
589 && ifp
->if_index
!= opt
->ip6po_pktinfo
->ipi6_ifindex
) {
590 ip6stat
.ip6s_noroute
++;
591 in6_ifstat_inc(ifp
, ifs6_out_discard
);
592 error
= EHOSTUNREACH
;
597 if (opt
&& opt
->ip6po_hlim
!= -1)
598 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
601 struct in6_multi
*in6m
;
603 m
->m_flags
= (m
->m_flags
& ~M_BCAST
) | M_MCAST
;
606 * See if the caller provided any multicast options
610 ip6
->ip6_hlim
= im6o
->im6o_multicast_hlim
;
611 if (im6o
->im6o_multicast_ifp
!= NULL
)
612 ifp
= im6o
->im6o_multicast_ifp
;
614 ip6
->ip6_hlim
= ip6_defmcasthlim
;
617 * See if the caller provided the outgoing interface
618 * as an ancillary data.
619 * Boundary check for ifindex is assumed to be already done.
621 if (opt
&& opt
->ip6po_pktinfo
&& opt
->ip6po_pktinfo
->ipi6_ifindex
)
622 ifp
= ifindex2ifnet
[opt
->ip6po_pktinfo
->ipi6_ifindex
];
625 * If the destination is a node-local scope multicast,
626 * the packet should be loop-backed only.
628 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6
->ip6_dst
)) {
630 * If the outgoing interface is already specified,
631 * it should be a loopback interface.
633 if (ifp
&& (ifp
->if_flags
& IFF_LOOPBACK
) == 0) {
634 ip6stat
.ip6s_badscope
++;
635 error
= ENETUNREACH
; /* XXX: better error? */
636 /* XXX correct ifp? */
637 in6_ifstat_inc(ifp
, ifs6_out_discard
);
644 if (opt
&& opt
->ip6po_hlim
!= -1)
645 ip6
->ip6_hlim
= opt
->ip6po_hlim
& 0xff;
648 * If caller did not provide an interface lookup a
649 * default in the routing table. This is either a
650 * default for the speicfied group (i.e. a host
651 * route), or a multicast default (a route for the
655 if (ro
->ro_rt
== 0) {
656 ro
->ro_rt
= rtalloc1((struct sockaddr
*)
657 &ro
->ro_dst
, 0, 0UL);
659 if (ro
->ro_rt
== 0) {
660 ip6stat
.ip6s_noroute
++;
661 error
= EHOSTUNREACH
;
662 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
665 ia
= ifatoia6(ro
->ro_rt
->rt_ifa
);
666 ifp
= ro
->ro_rt
->rt_ifp
;
670 if ((flags
& IPV6_FORWARDING
) == 0)
671 in6_ifstat_inc(ifp
, ifs6_out_request
);
672 in6_ifstat_inc(ifp
, ifs6_out_mcast
);
675 * Confirm that the outgoing interface supports multicast.
677 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0) {
678 ip6stat
.ip6s_noroute
++;
679 in6_ifstat_inc(ifp
, ifs6_out_discard
);
683 IN6_LOOKUP_MULTI(ip6
->ip6_dst
, ifp
, in6m
);
685 (im6o
== NULL
|| im6o
->im6o_multicast_loop
)) {
687 * If we belong to the destination multicast group
688 * on the outgoing interface, and the caller did not
689 * forbid loopback, loop back a copy.
691 ip6_mloopback(ifp
, m
, dst
);
694 * If we are acting as a multicast router, perform
695 * multicast forwarding as if the packet had just
696 * arrived on the interface to which we are about
697 * to send. The multicast forwarding function
698 * recursively calls this function, using the
699 * IPV6_FORWARDING flag to prevent infinite recursion.
701 * Multicasts that are looped back by ip6_mloopback(),
702 * above, will be forwarded by the ip6_input() routine,
705 if (ip6_mrouter
&& (flags
& IPV6_FORWARDING
) == 0) {
706 if (ip6_mforward(ip6
, ifp
, m
) != NULL
) {
713 * Multicasts with a hoplimit of zero may be looped back,
714 * above, but must not be transmitted on a network.
715 * Also, multicasts addressed to the loopback interface
716 * are not sent -- the above call to ip6_mloopback() will
717 * loop back a copy if this host actually belongs to the
718 * destination group on the loopback interface.
720 if (ip6
->ip6_hlim
== 0 || (ifp
->if_flags
& IFF_LOOPBACK
)) {
727 * Fill the outgoing inteface to tell the upper layer
728 * to increment per-interface statistics.
734 * Determine path MTU.
737 /* The first hop and the final destination may differ. */
738 struct sockaddr_in6
*sin6_fin
=
739 (struct sockaddr_in6
*)&ro_pmtu
->ro_dst
;
740 if (ro_pmtu
->ro_rt
&& ((ro
->ro_rt
->rt_flags
& RTF_UP
) == 0 ||
741 !IN6_ARE_ADDR_EQUAL(&sin6_fin
->sin6_addr
,
743 rtfree(ro_pmtu
->ro_rt
);
744 ro_pmtu
->ro_rt
= (struct rtentry
*)0;
746 if (ro_pmtu
->ro_rt
== 0) {
747 bzero(sin6_fin
, sizeof(*sin6_fin
));
748 sin6_fin
->sin6_family
= AF_INET6
;
749 sin6_fin
->sin6_len
= sizeof(struct sockaddr_in6
);
750 sin6_fin
->sin6_addr
= finaldst
;
752 rtalloc((struct route
*)ro_pmtu
);
755 if (ro_pmtu
->ro_rt
!= NULL
) {
756 u_int32_t ifmtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
758 mtu
= ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
;
759 if (mtu
> ifmtu
|| mtu
== 0) {
761 * The MTU on the route is larger than the MTU on
762 * the interface! This shouldn't happen, unless the
763 * MTU of the interface has been changed after the
764 * interface was brought up. Change the MTU in the
765 * route to match the interface MTU (as long as the
766 * field isn't locked).
768 * if MTU on the route is 0, we need to fix the MTU.
769 * this case happens with path MTU discovery timeouts.
772 if ((ro_pmtu
->ro_rt
->rt_rmx
.rmx_locks
& RTV_MTU
) == 0)
773 ro_pmtu
->ro_rt
->rt_rmx
.rmx_mtu
= mtu
; /* XXX */
776 mtu
= nd_ifinfo
[ifp
->if_index
].linkmtu
;
780 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
782 if ((flags
& IPV6_MINMTU
) != 0 && mtu
> IPV6_MMTU
)
785 /* Fake scoped addresses */
786 if ((ifp
->if_flags
& IFF_LOOPBACK
) != 0) {
788 * If source or destination address is a scoped address, and
789 * the packet is going to be sent to a loopback interface,
790 * we should keep the original interface.
794 * XXX: this is a very experimental and temporary solution.
795 * We eventually have sockaddr_in6 and use the sin6_scope_id
796 * field of the structure here.
797 * We rely on the consistency between two scope zone ids
798 * of source and destination, which should already be assured.
799 * Larger scopes than link will be supported in the future.
802 if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_src
))
803 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_src
.s6_addr16
[1])];
804 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6
->ip6_dst
))
805 origifp
= ifindex2ifnet
[ntohs(ip6
->ip6_dst
.s6_addr16
[1])];
807 * XXX: origifp can be NULL even in those two cases above.
808 * For example, if we remove the (only) link-local address
809 * from the loopback interface, and try to send a link-local
810 * address without link-id information. Then the source
811 * address is ::1, and the destination address is the
812 * link-local address with its s6_addr16[1] being zero.
813 * What is worse, if the packet goes to the loopback interface
814 * by a default rejected route, the null pointer would be
815 * passed to looutput, and the kernel would hang.
816 * The following last resort would prevent such disaster.
823 #ifndef SCOPEDROUTING
825 * clear embedded scope identifiers if necessary.
826 * in6_clearscope will touch the addresses only when necessary.
828 in6_clearscope(&ip6
->ip6_src
);
829 in6_clearscope(&ip6
->ip6_dst
);
833 * Check with the firewall...
835 if (ip6_fw_enable
&& ip6_fw_chk_ptr
) {
837 m
->m_pkthdr
.rcvif
= NULL
; /* XXX */
838 /* If ipfw says divert, we have to just drop packet */
839 if ((*ip6_fw_chk_ptr
)(&ip6
, ifp
, &port
, &m
)) {
850 * If the outgoing packet contains a hop-by-hop options header,
851 * it must be examined and processed even by the source node.
852 * (RFC 2460, section 4.)
854 if (exthdrs
.ip6e_hbh
) {
855 struct ip6_hbh
*hbh
= mtod(exthdrs
.ip6e_hbh
, struct ip6_hbh
*);
856 u_int32_t dummy1
; /* XXX unused */
857 u_int32_t dummy2
; /* XXX unused */
860 if ((hbh
->ip6h_len
+ 1) << 3 > exthdrs
.ip6e_hbh
->m_len
)
861 panic("ip6e_hbh is not continuous");
864 * XXX: if we have to send an ICMPv6 error to the sender,
865 * we need the M_LOOP flag since icmp6_error() expects
866 * the IPv6 and the hop-by-hop options header are
867 * continuous unless the flag is set.
869 m
->m_flags
|= M_LOOP
;
870 m
->m_pkthdr
.rcvif
= ifp
;
871 if (ip6_process_hopopts(m
,
872 (u_int8_t
*)(hbh
+ 1),
873 ((hbh
->ip6h_len
+ 1) << 3) -
874 sizeof(struct ip6_hbh
),
875 &dummy1
, &dummy2
) < 0) {
876 /* m was already freed at this point */
877 error
= EINVAL
;/* better error? */
880 m
->m_flags
&= ~M_LOOP
; /* XXX */
881 m
->m_pkthdr
.rcvif
= NULL
;
885 * Send the packet to the outgoing interface.
886 * If necessary, do IPv6 fragmentation before sending.
888 tlen
= m
->m_pkthdr
.len
;
892 * On any link that cannot convey a 1280-octet packet in one piece,
893 * link-specific fragmentation and reassembly must be provided at
894 * a layer below IPv6. [RFC 2460, sec.5]
895 * Thus if the interface has ability of link-level fragmentation,
896 * we can just send the packet even if the packet size is
897 * larger than the link's MTU.
898 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
901 || ifp
->if_flags
& IFF_FRAGMENTABLE
905 /* Record statistics for this interface address. */
906 if (ia
&& !(flags
& IPV6_FORWARDING
)) {
908 ia
->ia_ifa
.if_opackets
++;
909 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
913 /* clean ipsec history once it goes out of the node */
917 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
919 } else if (mtu
< IPV6_MMTU
) {
921 * note that path MTU is never less than IPV6_MMTU
925 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
927 } else if (ip6
->ip6_plen
== 0) { /* jumbo payload cannot be fragmented */
929 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
932 struct mbuf
**mnext
, *m_frgpart
;
933 struct ip6_frag
*ip6f
;
934 u_int32_t id
= htonl(ip6_id
++);
938 * Too large for the destination or interface;
939 * fragment if possible.
940 * Must be able to put at least 8 bytes per fragment.
942 hlen
= unfragpartlen
;
943 if (mtu
> IPV6_MAXPACKET
)
944 mtu
= IPV6_MAXPACKET
;
946 len
= (mtu
- hlen
- sizeof(struct ip6_frag
)) & ~7;
949 in6_ifstat_inc(ifp
, ifs6_out_fragfail
);
953 mnext
= &m
->m_nextpkt
;
956 * Change the next header field of the last header in the
957 * unfragmentable part.
959 if (exthdrs
.ip6e_rthdr
) {
960 nextproto
= *mtod(exthdrs
.ip6e_rthdr
, u_char
*);
961 *mtod(exthdrs
.ip6e_rthdr
, u_char
*) = IPPROTO_FRAGMENT
;
962 } else if (exthdrs
.ip6e_dest1
) {
963 nextproto
= *mtod(exthdrs
.ip6e_dest1
, u_char
*);
964 *mtod(exthdrs
.ip6e_dest1
, u_char
*) = IPPROTO_FRAGMENT
;
965 } else if (exthdrs
.ip6e_hbh
) {
966 nextproto
= *mtod(exthdrs
.ip6e_hbh
, u_char
*);
967 *mtod(exthdrs
.ip6e_hbh
, u_char
*) = IPPROTO_FRAGMENT
;
969 nextproto
= ip6
->ip6_nxt
;
970 ip6
->ip6_nxt
= IPPROTO_FRAGMENT
;
974 * Loop through length of segment after first fragment,
975 * make new header and copy data of each part and link onto
979 for (off
= hlen
; off
< tlen
; off
+= len
) {
980 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
983 ip6stat
.ip6s_odropped
++;
986 m
->m_pkthdr
.rcvif
= NULL
;
987 m
->m_flags
= m0
->m_flags
& M_COPYFLAGS
;
989 mnext
= &m
->m_nextpkt
;
990 m
->m_data
+= max_linkhdr
;
991 mhip6
= mtod(m
, struct ip6_hdr
*);
993 m
->m_len
= sizeof(*mhip6
);
994 error
= ip6_insertfraghdr(m0
, m
, hlen
, &ip6f
);
996 ip6stat
.ip6s_odropped
++;
999 ip6f
->ip6f_offlg
= htons((u_short
)((off
- hlen
) & ~7));
1000 if (off
+ len
>= tlen
)
1003 ip6f
->ip6f_offlg
|= IP6F_MORE_FRAG
;
1004 mhip6
->ip6_plen
= htons((u_short
)(len
+ hlen
+
1006 sizeof(struct ip6_hdr
)));
1007 if ((m_frgpart
= m_copy(m0
, off
, len
)) == 0) {
1009 ip6stat
.ip6s_odropped
++;
1012 m_cat(m
, m_frgpart
);
1013 m
->m_pkthdr
.len
= len
+ hlen
+ sizeof(*ip6f
);
1014 m
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
1015 ip6f
->ip6f_reserved
= 0;
1016 ip6f
->ip6f_ident
= id
;
1017 ip6f
->ip6f_nxt
= nextproto
;
1018 ip6stat
.ip6s_ofragments
++;
1019 in6_ifstat_inc(ifp
, ifs6_out_fragcreat
);
1022 in6_ifstat_inc(ifp
, ifs6_out_fragok
);
1026 * Remove leading garbages.
1032 for (m0
= m
; m
; m
= m0
) {
1036 /* Record statistics for this interface address. */
1039 ia
->ia_ifa
.if_opackets
++;
1040 ia
->ia_ifa
.if_obytes
+= m
->m_pkthdr
.len
;
1044 /* clean ipsec history once it goes out of the node */
1047 error
= nd6_output(ifp
, origifp
, m
, dst
, ro
->ro_rt
);
1053 ip6stat
.ip6s_fragmented
++;
1056 if (ro
== &ip6route
&& ro
->ro_rt
) { /* brace necessary for rtfree */
1058 } else if (ro_pmtu
== &ip6route
&& ro_pmtu
->ro_rt
) {
1059 rtfree(ro_pmtu
->ro_rt
);
1070 m_freem(exthdrs
.ip6e_hbh
); /* m_freem will check if mbuf is 0 */
1071 m_freem(exthdrs
.ip6e_dest1
);
1072 m_freem(exthdrs
.ip6e_rthdr
);
1073 m_freem(exthdrs
.ip6e_dest2
);
1081 ip6_copyexthdr(mp
, hdr
, hlen
)
1088 if (hlen
> MCLBYTES
)
1089 return(ENOBUFS
); /* XXX */
1091 MGET(m
, M_DONTWAIT
, MT_DATA
);
1096 MCLGET(m
, M_DONTWAIT
);
1097 if ((m
->m_flags
& M_EXT
) == 0) {
1104 bcopy(hdr
, mtod(m
, caddr_t
), hlen
);
1111 * Insert jumbo payload option.
1114 ip6_insert_jumboopt(exthdrs
, plen
)
1115 struct ip6_exthdrs
*exthdrs
;
1122 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1125 * If there is no hop-by-hop options header, allocate new one.
1126 * If there is one but it doesn't have enough space to store the
1127 * jumbo payload option, allocate a cluster to store the whole options.
1128 * Otherwise, use it to store the options.
1130 if (exthdrs
->ip6e_hbh
== 0) {
1131 MGET(mopt
, M_DONTWAIT
, MT_DATA
);
1134 mopt
->m_len
= JUMBOOPTLEN
;
1135 optbuf
= mtod(mopt
, u_char
*);
1136 optbuf
[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1137 exthdrs
->ip6e_hbh
= mopt
;
1139 struct ip6_hbh
*hbh
;
1141 mopt
= exthdrs
->ip6e_hbh
;
1142 if (M_TRAILINGSPACE(mopt
) < JUMBOOPTLEN
) {
1145 * - exthdrs->ip6e_hbh is not referenced from places
1146 * other than exthdrs.
1147 * - exthdrs->ip6e_hbh is not an mbuf chain.
1149 int oldoptlen
= mopt
->m_len
;
1153 * XXX: give up if the whole (new) hbh header does
1154 * not fit even in an mbuf cluster.
1156 if (oldoptlen
+ JUMBOOPTLEN
> MCLBYTES
)
1160 * As a consequence, we must always prepare a cluster
1163 MGET(n
, M_DONTWAIT
, MT_DATA
);
1165 MCLGET(n
, M_DONTWAIT
);
1166 if ((n
->m_flags
& M_EXT
) == 0) {
1173 n
->m_len
= oldoptlen
+ JUMBOOPTLEN
;
1174 bcopy(mtod(mopt
, caddr_t
), mtod(n
, caddr_t
),
1176 optbuf
= mtod(n
, caddr_t
) + oldoptlen
;
1178 mopt
= exthdrs
->ip6e_hbh
= n
;
1180 optbuf
= mtod(mopt
, u_char
*) + mopt
->m_len
;
1181 mopt
->m_len
+= JUMBOOPTLEN
;
1183 optbuf
[0] = IP6OPT_PADN
;
1187 * Adjust the header length according to the pad and
1188 * the jumbo payload option.
1190 hbh
= mtod(mopt
, struct ip6_hbh
*);
1191 hbh
->ip6h_len
+= (JUMBOOPTLEN
>> 3);
1194 /* fill in the option. */
1195 optbuf
[2] = IP6OPT_JUMBO
;
1197 v
= (u_int32_t
)htonl(plen
+ JUMBOOPTLEN
);
1198 bcopy(&v
, &optbuf
[4], sizeof(u_int32_t
));
1200 /* finally, adjust the packet header length */
1201 exthdrs
->ip6e_ip6
->m_pkthdr
.len
+= JUMBOOPTLEN
;
1208 * Insert fragment header and copy unfragmentable header portions.
1211 ip6_insertfraghdr(m0
, m
, hlen
, frghdrp
)
1212 struct mbuf
*m0
, *m
;
1214 struct ip6_frag
**frghdrp
;
1216 struct mbuf
*n
, *mlast
;
1218 if (hlen
> sizeof(struct ip6_hdr
)) {
1219 n
= m_copym(m0
, sizeof(struct ip6_hdr
),
1220 hlen
- sizeof(struct ip6_hdr
), M_DONTWAIT
);
1227 /* Search for the last mbuf of unfragmentable part. */
1228 for (mlast
= n
; mlast
->m_next
; mlast
= mlast
->m_next
)
1231 if ((mlast
->m_flags
& M_EXT
) == 0 &&
1232 M_TRAILINGSPACE(mlast
) >= sizeof(struct ip6_frag
)) {
1233 /* use the trailing space of the last mbuf for the fragment hdr */
1235 (struct ip6_frag
*)(mtod(mlast
, caddr_t
) + mlast
->m_len
);
1236 mlast
->m_len
+= sizeof(struct ip6_frag
);
1237 m
->m_pkthdr
.len
+= sizeof(struct ip6_frag
);
1239 /* allocate a new mbuf for the fragment header */
1242 MGET(mfrg
, M_DONTWAIT
, MT_DATA
);
1245 mfrg
->m_len
= sizeof(struct ip6_frag
);
1246 *frghdrp
= mtod(mfrg
, struct ip6_frag
*);
1247 mlast
->m_next
= mfrg
;
1253 extern int load_ipfw();
1256 * IP6 socket option processing.
1259 ip6_ctloutput(so
, sopt
)
1261 struct sockopt
*sopt
;
1264 struct inpcb
*in6p
= sotoinpcb(so
);
1266 int level
, op
, optname
;
1271 panic("ip6_ctloutput: arg soopt is NULL");
1273 level
= sopt
->sopt_level
;
1274 op
= sopt
->sopt_dir
;
1275 optname
= sopt
->sopt_name
;
1276 optlen
= sopt
->sopt_valsize
;
1281 privileged
= (p
== 0 || suser(p
->p_ucred
, &p
->p_acflag
)) ? 0 : 1;
1283 if (level
== IPPROTO_IPV6
) {
1288 case IPV6_PKTOPTIONS
:
1292 error
= soopt_getm(sopt
, &m
); /* XXX */
1295 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1298 error
= ip6_pcbopts(&in6p
->in6p_outputopts
,
1300 m_freem(m
); /* XXX */
1305 * Use of some Hop-by-Hop options or some
1306 * Destination options, might require special
1307 * privilege. That is, normal applications
1308 * (without special privilege) might be forbidden
1309 * from setting certain options in outgoing packets,
1310 * and might never see certain options in received
1311 * packets. [RFC 2292 Section 6]
1312 * KAME specific note:
1313 * KAME prevents non-privileged users from sending or
1314 * receiving ANY hbh/dst options in order to avoid
1315 * overhead of parsing options in the kernel.
1317 case IPV6_UNICAST_HOPS
:
1322 if (optlen
!= sizeof(int)) {
1326 error
= sooptcopyin(sopt
, &optval
,
1327 sizeof optval
, sizeof optval
);
1332 case IPV6_UNICAST_HOPS
:
1333 if (optval
< -1 || optval
>= 256)
1336 /* -1 = kernel default */
1337 in6p
->in6p_hops
= optval
;
1339 if ((in6p
->in6p_vflag
&
1341 in6p
->inp_ip_ttl
= optval
;
1344 #define OPTSET(bit) \
1347 in6p->in6p_flags |= (bit); \
1349 in6p->in6p_flags &= ~(bit); \
1351 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1354 in6p
->in6p_cksum
= optval
;
1363 * make setsockopt(IPV6_V6ONLY)
1364 * available only prior to bind(2).
1365 * see ipng mailing list, Jun 22 2001.
1367 if (in6p
->in6p_lport
||
1368 !IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_laddr
))
1373 OPTSET(IN6P_IPV6_V6ONLY
);
1375 in6p
->in6p_vflag
&= ~INP_IPV4
;
1377 in6p
->in6p_vflag
|= INP_IPV4
;
1388 if (optlen
!= sizeof(int)) {
1392 error
= sooptcopyin(sopt
, &optval
,
1393 sizeof optval
, sizeof optval
);
1398 OPTSET(IN6P_PKTINFO
);
1401 OPTSET(IN6P_HOPLIMIT
);
1405 * Check super-user privilege.
1406 * See comments for IPV6_RECVHOPOPTS.
1410 OPTSET(IN6P_HOPOPTS
);
1415 OPTSET(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
); /* XXX */
1424 case IPV6_MULTICAST_IF
:
1425 case IPV6_MULTICAST_HOPS
:
1426 case IPV6_MULTICAST_LOOP
:
1427 case IPV6_JOIN_GROUP
:
1428 case IPV6_LEAVE_GROUP
:
1431 if (sopt
->sopt_valsize
> MLEN
) {
1436 MGET(m
, sopt
->sopt_p
? M_WAIT
: M_DONTWAIT
, MT_HEADER
);
1441 m
->m_len
= sopt
->sopt_valsize
;
1442 error
= sooptcopyin(sopt
, mtod(m
, char *),
1443 m
->m_len
, m
->m_len
);
1444 error
= ip6_setmoptions(sopt
->sopt_name
, in6p
, m
);
1449 case IPV6_PORTRANGE
:
1450 error
= sooptcopyin(sopt
, &optval
,
1451 sizeof optval
, sizeof optval
);
1456 case IPV6_PORTRANGE_DEFAULT
:
1457 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1458 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1461 case IPV6_PORTRANGE_HIGH
:
1462 in6p
->in6p_flags
&= ~(IN6P_LOWPORT
);
1463 in6p
->in6p_flags
|= IN6P_HIGHPORT
;
1466 case IPV6_PORTRANGE_LOW
:
1467 in6p
->in6p_flags
&= ~(IN6P_HIGHPORT
);
1468 in6p
->in6p_flags
|= IN6P_LOWPORT
;
1478 case IPV6_IPSEC_POLICY
:
1484 if ((error
= soopt_getm(sopt
, &m
)) != 0) /* XXX */
1486 if ((error
= soopt_mcopyin(sopt
, m
)) != 0) /* XXX */
1489 req
= mtod(m
, caddr_t
);
1492 error
= ipsec6_set_policy(in6p
, optname
, req
,
1497 #endif /* KAME IPSEC */
1504 if (ip6_fw_ctl_ptr
== NULL
&& load_ipfw() != 0)
1507 error
= (*ip6_fw_ctl_ptr
)(sopt
);
1512 error
= ENOPROTOOPT
;
1520 case IPV6_PKTOPTIONS
:
1521 if (in6p
->in6p_options
) {
1523 m
= m_copym(in6p
->in6p_options
,
1524 0, M_COPYALL
, M_WAIT
);
1525 error
= soopt_mcopyout(sopt
, m
);
1529 sopt
->sopt_valsize
= 0;
1532 case IPV6_UNICAST_HOPS
:
1537 case IPV6_PORTRANGE
:
1540 case IPV6_UNICAST_HOPS
:
1541 optval
= in6p
->in6p_hops
;
1545 optval
= in6p
->in6p_cksum
;
1549 optval
= OPTBIT(IN6P_FAITH
);
1553 optval
= OPTBIT(IN6P_IPV6_V6ONLY
);
1556 case IPV6_PORTRANGE
:
1559 flags
= in6p
->in6p_flags
;
1560 if (flags
& IN6P_HIGHPORT
)
1561 optval
= IPV6_PORTRANGE_HIGH
;
1562 else if (flags
& IN6P_LOWPORT
)
1563 optval
= IPV6_PORTRANGE_LOW
;
1569 error
= sooptcopyout(sopt
, &optval
,
1578 if (optname
== IPV6_HOPOPTS
||
1579 optname
== IPV6_DSTOPTS
||
1584 optval
= OPTBIT(IN6P_PKTINFO
);
1587 optval
= OPTBIT(IN6P_HOPLIMIT
);
1592 optval
= OPTBIT(IN6P_HOPOPTS
);
1595 optval
= OPTBIT(IN6P_RTHDR
);
1600 optval
= OPTBIT(IN6P_DSTOPTS
|IN6P_RTHDRDSTOPTS
);
1603 error
= sooptcopyout(sopt
, &optval
,
1607 case IPV6_MULTICAST_IF
:
1608 case IPV6_MULTICAST_HOPS
:
1609 case IPV6_MULTICAST_LOOP
:
1610 case IPV6_JOIN_GROUP
:
1611 case IPV6_LEAVE_GROUP
:
1614 error
= ip6_getmoptions(sopt
->sopt_name
,
1615 in6p
->in6p_moptions
, &m
);
1617 error
= sooptcopyout(sopt
,
1618 mtod(m
, char *), m
->m_len
);
1624 case IPV6_IPSEC_POLICY
:
1628 struct mbuf
*m
= NULL
;
1629 struct mbuf
**mp
= &m
;
1631 error
= soopt_getm(sopt
, &m
); /* XXX */
1634 error
= soopt_mcopyin(sopt
, m
); /* XXX */
1638 req
= mtod(m
, caddr_t
);
1641 error
= ipsec6_get_policy(in6p
, req
, len
, mp
);
1643 error
= soopt_mcopyout(sopt
, m
); /*XXX*/
1644 if (error
== 0 && m
)
1648 #endif /* KAME IPSEC */
1652 if (ip6_fw_ctl_ptr
== NULL
&& load_ipfw() != 0)
1655 error
= (*ip6_fw_ctl_ptr
)(sopt
);
1660 error
= ENOPROTOOPT
;
1672 * Set up IP6 options in pcb for insertion in output packets or
1673 * specifying behavior of outgoing packets.
1676 ip6_pcbopts(pktopt
, m
, so
, sopt
)
1677 struct ip6_pktopts
**pktopt
;
1680 struct sockopt
*sopt
;
1682 struct ip6_pktopts
*opt
= *pktopt
;
1684 struct proc
*p
= sopt
->sopt_p
;
1687 /* turn off any old options. */
1690 if (opt
->ip6po_pktinfo
|| opt
->ip6po_nexthop
||
1691 opt
->ip6po_hbh
|| opt
->ip6po_dest1
|| opt
->ip6po_dest2
||
1692 opt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1693 printf("ip6_pcbopts: all specified options are cleared.\n");
1695 ip6_clearpktopts(opt
, 1, -1);
1697 opt
= _MALLOC(sizeof(*opt
), M_IP6OPT
, M_WAITOK
);
1700 if (!m
|| m
->m_len
== 0) {
1702 * Only turning off any previous options, regardless of
1703 * whether the opt is just created or given.
1706 FREE(opt
, M_IP6OPT
);
1710 /* set options specified by user. */
1711 if (p
&& !suser(p
->p_ucred
, &p
->p_acflag
))
1713 if ((error
= ip6_setpktoptions(m
, opt
, priv
, 1)) != 0) {
1714 ip6_clearpktopts(opt
, 1, -1); /* XXX: discard all options */
1715 FREE(opt
, M_IP6OPT
);
1723 * initialize ip6_pktopts. beware that there are non-zero default values in
1727 init_ip6pktopts(opt
)
1728 struct ip6_pktopts
*opt
;
1731 bzero(opt
, sizeof(*opt
));
1732 opt
->ip6po_hlim
= -1; /* -1 means default hop limit */
1736 ip6_clearpktopts(pktopt
, needfree
, optname
)
1737 struct ip6_pktopts
*pktopt
;
1738 int needfree
, optname
;
1743 if (optname
== -1) {
1744 if (needfree
&& pktopt
->ip6po_pktinfo
)
1745 FREE(pktopt
->ip6po_pktinfo
, M_IP6OPT
);
1746 pktopt
->ip6po_pktinfo
= NULL
;
1749 pktopt
->ip6po_hlim
= -1;
1750 if (optname
== -1) {
1751 if (needfree
&& pktopt
->ip6po_nexthop
)
1752 FREE(pktopt
->ip6po_nexthop
, M_IP6OPT
);
1753 pktopt
->ip6po_nexthop
= NULL
;
1755 if (optname
== -1) {
1756 if (needfree
&& pktopt
->ip6po_hbh
)
1757 FREE(pktopt
->ip6po_hbh
, M_IP6OPT
);
1758 pktopt
->ip6po_hbh
= NULL
;
1760 if (optname
== -1) {
1761 if (needfree
&& pktopt
->ip6po_dest1
)
1762 FREE(pktopt
->ip6po_dest1
, M_IP6OPT
);
1763 pktopt
->ip6po_dest1
= NULL
;
1765 if (optname
== -1) {
1766 if (needfree
&& pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
)
1767 FREE(pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
, M_IP6OPT
);
1768 pktopt
->ip6po_rhinfo
.ip6po_rhi_rthdr
= NULL
;
1769 if (pktopt
->ip6po_route
.ro_rt
) {
1770 rtfree(pktopt
->ip6po_route
.ro_rt
);
1771 pktopt
->ip6po_route
.ro_rt
= NULL
;
1774 if (optname
== -1) {
1775 if (needfree
&& pktopt
->ip6po_dest2
)
1776 FREE(pktopt
->ip6po_dest2
, M_IP6OPT
);
1777 pktopt
->ip6po_dest2
= NULL
;
1781 #define PKTOPT_EXTHDRCPY(type) \
1785 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1786 dst->type = _MALLOC(hlen, M_IP6OPT, canwait);\
1787 if (dst->type == NULL && canwait == M_NOWAIT)\
1789 bcopy(src->type, dst->type, hlen);\
1793 struct ip6_pktopts
*
1794 ip6_copypktopts(src
, canwait
)
1795 struct ip6_pktopts
*src
;
1798 struct ip6_pktopts
*dst
;
1801 printf("ip6_clearpktopts: invalid argument\n");
1805 dst
= _MALLOC(sizeof(*dst
), M_IP6OPT
, canwait
);
1806 if (dst
== NULL
&& canwait
== M_NOWAIT
)
1808 bzero(dst
, sizeof(*dst
));
1810 dst
->ip6po_hlim
= src
->ip6po_hlim
;
1811 if (src
->ip6po_pktinfo
) {
1812 dst
->ip6po_pktinfo
= _MALLOC(sizeof(*dst
->ip6po_pktinfo
),
1814 if (dst
->ip6po_pktinfo
== NULL
&& canwait
== M_NOWAIT
)
1816 *dst
->ip6po_pktinfo
= *src
->ip6po_pktinfo
;
1818 if (src
->ip6po_nexthop
) {
1819 dst
->ip6po_nexthop
= _MALLOC(src
->ip6po_nexthop
->sa_len
,
1821 if (dst
->ip6po_nexthop
== NULL
&& canwait
== M_NOWAIT
)
1823 bcopy(src
->ip6po_nexthop
, dst
->ip6po_nexthop
,
1824 src
->ip6po_nexthop
->sa_len
);
1826 PKTOPT_EXTHDRCPY(ip6po_hbh
);
1827 PKTOPT_EXTHDRCPY(ip6po_dest1
);
1828 PKTOPT_EXTHDRCPY(ip6po_dest2
);
1829 PKTOPT_EXTHDRCPY(ip6po_rthdr
); /* not copy the cached route */
1833 if (dst
->ip6po_pktinfo
) FREE(dst
->ip6po_pktinfo
, M_IP6OPT
);
1834 if (dst
->ip6po_nexthop
) FREE(dst
->ip6po_nexthop
, M_IP6OPT
);
1835 if (dst
->ip6po_hbh
) FREE(dst
->ip6po_hbh
, M_IP6OPT
);
1836 if (dst
->ip6po_dest1
) FREE(dst
->ip6po_dest1
, M_IP6OPT
);
1837 if (dst
->ip6po_dest2
) FREE(dst
->ip6po_dest2
, M_IP6OPT
);
1838 if (dst
->ip6po_rthdr
) FREE(dst
->ip6po_rthdr
, M_IP6OPT
);
1839 FREE(dst
, M_IP6OPT
);
1842 #undef PKTOPT_EXTHDRCPY
1845 ip6_freepcbopts(pktopt
)
1846 struct ip6_pktopts
*pktopt
;
1851 ip6_clearpktopts(pktopt
, 1, -1);
1853 FREE(pktopt
, M_IP6OPT
);
1857 * Set the IP6 multicast options in response to user setsockopt().
1860 ip6_setmoptions(optname
, in6p
, m
)
1866 u_int loop
, ifindex
;
1867 struct ipv6_mreq
*mreq
;
1869 struct ip6_moptions
**im6op
= &in6p
->in6p_moptions
;
1870 struct ip6_moptions
*im6o
= *im6op
;
1871 struct ip_moptions
*imo
;
1872 struct route_in6 ro
;
1873 struct sockaddr_in6
*dst
;
1874 struct in6_multi_mship
*imm
;
1875 struct proc
*p
= current_proc(); /* XXX */
1879 * No multicast option buffer attached to the pcb;
1880 * allocate one and initialize to default values.
1882 im6o
= (struct ip6_moptions
*)
1883 _MALLOC(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
);
1894 if (in6p
->inp_moptions
== NULL
) {
1896 * No IPv4 multicast option buffer attached to the pcb;
1897 * call ip_createmoptions to allocate one and initialize
1898 * to default values.
1900 error
= ip_createmoptions(&in6p
->inp_moptions
);
1904 imo
= in6p
->inp_moptions
;
1908 case IPV6_MULTICAST_IF
:
1910 * Select the interface for outgoing multicast packets.
1912 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
1916 bcopy(mtod(m
, u_int
*), &ifindex
, sizeof(ifindex
));
1917 if (ifindex
< 0 || if_index
< ifindex
) {
1918 error
= ENXIO
; /* XXX EINVAL? */
1921 ifp
= ifindex2ifnet
[ifindex
];
1922 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
1923 error
= EADDRNOTAVAIL
;
1926 im6o
->im6o_multicast_ifp
= ifp
;
1927 imo
->imo_multicast_ifp
= ifp
;
1930 case IPV6_MULTICAST_HOPS
:
1933 * Set the IP6 hoplimit for outgoing multicast packets.
1936 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
1940 bcopy(mtod(m
, u_int
*), &optval
, sizeof(optval
));
1941 if (optval
< -1 || optval
>= 256)
1943 else if (optval
== -1) {
1944 im6o
->im6o_multicast_hlim
= ip6_defmcasthlim
;
1945 imo
->imo_multicast_ttl
= IP_DEFAULT_MULTICAST_TTL
;
1947 im6o
->im6o_multicast_hlim
= optval
;
1948 imo
->imo_multicast_ttl
= optval
;
1953 case IPV6_MULTICAST_LOOP
:
1955 * Set the loopback flag for outgoing multicast packets.
1956 * Must be zero or one.
1958 if (m
== NULL
|| m
->m_len
!= sizeof(u_int
)) {
1962 bcopy(mtod(m
, u_int
*), &loop
, sizeof(loop
));
1967 im6o
->im6o_multicast_loop
= loop
;
1968 imo
->imo_multicast_loop
= loop
;
1971 case IPV6_JOIN_GROUP
:
1973 * Add a multicast group membership.
1974 * Group must be a valid IP6 multicast address.
1976 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
1980 mreq
= mtod(m
, struct ipv6_mreq
*);
1982 * If the interface is specified, validate it.
1984 if (mreq
->ipv6mr_interface
< 0
1985 || if_index
< mreq
->ipv6mr_interface
) {
1986 error
= ENXIO
; /* XXX EINVAL? */
1990 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
1992 * We use the unspecified address to specify to accept
1993 * all multicast addresses. Only super user is allowed
1996 if (suser(p
->p_ucred
, &p
->p_acflag
))
2001 } else if (IN6_IS_ADDR_V4MAPPED(&mreq
->ipv6mr_multiaddr
)) {
2002 struct ip_mreq v4req
;
2004 v4req
.imr_multiaddr
.s_addr
= mreq
->ipv6mr_multiaddr
.s6_addr32
[3];
2005 v4req
.imr_interface
.s_addr
= INADDR_ANY
;
2007 /* Find an IPv4 address on the specified interface. */
2008 if (mreq
->ipv6mr_interface
!= 0) {
2009 struct in_ifaddr
*ifa
;
2011 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2013 TAILQ_FOREACH(ifa
, &in_ifaddrhead
, ia_link
) {
2014 if (ifa
->ia_ifp
== ifp
) {
2015 v4req
.imr_interface
= IA_SIN(ifa
)->sin_addr
;
2020 if (v4req
.imr_multiaddr
.s_addr
== 0) {
2021 /* Interface has no IPv4 address. */
2027 error
= ip_addmembership(imo
, &v4req
);
2029 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2034 * If no interface was explicitly specified, choose an
2035 * appropriate one according to the given multicast address.
2037 if (mreq
->ipv6mr_interface
== 0) {
2039 * If the multicast address is in node-local scope,
2040 * the interface should be a loopback interface.
2041 * Otherwise, look up the routing table for the
2042 * address, and choose the outgoing interface.
2043 * XXX: is it a good approach?
2045 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq
->ipv6mr_multiaddr
)) {
2049 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
2050 bzero(dst
, sizeof(*dst
));
2051 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
2052 dst
->sin6_family
= AF_INET6
;
2053 dst
->sin6_addr
= mreq
->ipv6mr_multiaddr
;
2054 rtalloc((struct route
*)&ro
);
2055 if (ro
.ro_rt
== NULL
) {
2056 error
= EADDRNOTAVAIL
;
2059 ifp
= ro
.ro_rt
->rt_ifp
;
2063 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2066 * See if we found an interface, and confirm that it
2067 * supports multicast
2069 if (ifp
== NULL
|| (ifp
->if_flags
& IFF_MULTICAST
) == 0) {
2070 error
= EADDRNOTAVAIL
;
2074 * Put interface index into the multicast address,
2075 * if the address has link-local scope.
2077 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2078 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2079 = htons(mreq
->ipv6mr_interface
);
2082 * See if the membership already exists.
2084 for (imm
= im6o
->im6o_memberships
.lh_first
;
2085 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
)
2086 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
&&
2087 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2088 &mreq
->ipv6mr_multiaddr
))
2095 * Everything looks good; add a new record to the multicast
2096 * address list for the given interface.
2098 imm
= _MALLOC(sizeof(*imm
), M_IPMADDR
, M_WAITOK
);
2103 if ((imm
->i6mm_maddr
=
2104 in6_addmulti(&mreq
->ipv6mr_multiaddr
, ifp
, &error
)) == NULL
) {
2105 FREE(imm
, M_IPMADDR
);
2108 LIST_INSERT_HEAD(&im6o
->im6o_memberships
, imm
, i6mm_chain
);
2111 case IPV6_LEAVE_GROUP
:
2113 * Drop a multicast group membership.
2114 * Group must be a valid IP6 multicast address.
2116 if (m
== NULL
|| m
->m_len
!= sizeof(struct ipv6_mreq
)) {
2120 mreq
= mtod(m
, struct ipv6_mreq
*);
2122 * If an interface address was specified, get a pointer
2123 * to its ifnet structure.
2125 if (mreq
->ipv6mr_interface
< 0
2126 || if_index
< mreq
->ipv6mr_interface
) {
2127 error
= ENXIO
; /* XXX EINVAL? */
2130 ifp
= ifindex2ifnet
[mreq
->ipv6mr_interface
];
2132 if (IN6_IS_ADDR_UNSPECIFIED(&mreq
->ipv6mr_multiaddr
)) {
2133 if (suser(p
->p_ucred
, &p
->p_acflag
)) {
2137 } else if (IN6_IS_ADDR_V4MAPPED(&mreq
->ipv6mr_multiaddr
)) {
2138 struct ip_mreq v4req
;
2140 v4req
.imr_multiaddr
.s_addr
= mreq
->ipv6mr_multiaddr
.s6_addr32
[3];
2141 v4req
.imr_interface
.s_addr
= INADDR_ANY
;
2144 struct in_ifaddr
*ifa
;
2146 TAILQ_FOREACH(ifa
, &in_ifaddrhead
, ia_link
) {
2147 if (ifa
->ia_ifp
== ifp
) {
2148 v4req
.imr_interface
= IA_SIN(ifa
)->sin_addr
;
2154 error
= ip_dropmembership(imo
, &v4req
);
2156 } else if (!IN6_IS_ADDR_MULTICAST(&mreq
->ipv6mr_multiaddr
)) {
2161 * Put interface index into the multicast address,
2162 * if the address has link-local scope.
2164 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq
->ipv6mr_multiaddr
)) {
2165 mreq
->ipv6mr_multiaddr
.s6_addr16
[1]
2166 = htons(mreq
->ipv6mr_interface
);
2169 * Find the membership in the membership list.
2171 for (imm
= im6o
->im6o_memberships
.lh_first
;
2172 imm
!= NULL
; imm
= imm
->i6mm_chain
.le_next
) {
2174 imm
->i6mm_maddr
->in6m_ifp
== ifp
) &&
2175 IN6_ARE_ADDR_EQUAL(&imm
->i6mm_maddr
->in6m_addr
,
2176 &mreq
->ipv6mr_multiaddr
))
2180 /* Unable to resolve interface */
2181 error
= EADDRNOTAVAIL
;
2185 * Give up the multicast address record to which the
2186 * membership points.
2188 LIST_REMOVE(imm
, i6mm_chain
);
2189 in6_delmulti(imm
->i6mm_maddr
);
2190 FREE(imm
, M_IPMADDR
);
2199 * If all options have default values, no need to keep the mbuf.
2201 if (im6o
->im6o_multicast_ifp
== NULL
&&
2202 im6o
->im6o_multicast_hlim
== ip6_defmcasthlim
&&
2203 im6o
->im6o_multicast_loop
== IPV6_DEFAULT_MULTICAST_LOOP
&&
2204 im6o
->im6o_memberships
.lh_first
== NULL
) {
2205 FREE(*im6op
, M_IPMOPTS
);
2208 if (imo
->imo_multicast_ifp
== NULL
&&
2209 imo
->imo_multicast_vif
== -1 &&
2210 imo
->imo_multicast_ttl
== IP_DEFAULT_MULTICAST_TTL
&&
2211 imo
->imo_multicast_loop
== IP_DEFAULT_MULTICAST_LOOP
&&
2212 imo
->imo_num_memberships
== 0) {
2213 ip_freemoptions(imo
);
2214 in6p
->inp_moptions
= 0;
2221 * Return the IP6 multicast options in response to user getsockopt().
2224 ip6_getmoptions(optname
, im6o
, mp
)
2226 struct ip6_moptions
*im6o
;
2229 u_int
*hlim
, *loop
, *ifindex
;
2231 *mp
= m_get(M_WAIT
, MT_HEADER
); /*XXX*/
2235 case IPV6_MULTICAST_IF
:
2236 ifindex
= mtod(*mp
, u_int
*);
2237 (*mp
)->m_len
= sizeof(u_int
);
2238 if (im6o
== NULL
|| im6o
->im6o_multicast_ifp
== NULL
)
2241 *ifindex
= im6o
->im6o_multicast_ifp
->if_index
;
2244 case IPV6_MULTICAST_HOPS
:
2245 hlim
= mtod(*mp
, u_int
*);
2246 (*mp
)->m_len
= sizeof(u_int
);
2248 *hlim
= ip6_defmcasthlim
;
2250 *hlim
= im6o
->im6o_multicast_hlim
;
2253 case IPV6_MULTICAST_LOOP
:
2254 loop
= mtod(*mp
, u_int
*);
2255 (*mp
)->m_len
= sizeof(u_int
);
2257 *loop
= ip6_defmcasthlim
;
2259 *loop
= im6o
->im6o_multicast_loop
;
2268 * Discard the IP6 multicast options.
2271 ip6_freemoptions(im6o
)
2272 struct ip6_moptions
*im6o
;
2274 struct in6_multi_mship
*imm
;
2279 while ((imm
= im6o
->im6o_memberships
.lh_first
) != NULL
) {
2280 LIST_REMOVE(imm
, i6mm_chain
);
2281 if (imm
->i6mm_maddr
)
2282 in6_delmulti(imm
->i6mm_maddr
);
2283 FREE(imm
, M_IPMADDR
);
2285 FREE(im6o
, M_IPMOPTS
);
2289 * Set IPv6 outgoing packet options based on advanced API.
2292 ip6_setpktoptions(control
, opt
, priv
, needcopy
)
2293 struct mbuf
*control
;
2294 struct ip6_pktopts
*opt
;
2297 struct cmsghdr
*cm
= 0;
2299 if (control
== 0 || opt
== 0)
2302 init_ip6pktopts(opt
);
2305 * XXX: Currently, we assume all the optional information is stored
2308 if (control
->m_next
)
2311 for (; control
->m_len
; control
->m_data
+= CMSG_ALIGN(cm
->cmsg_len
),
2312 control
->m_len
-= CMSG_ALIGN(cm
->cmsg_len
)) {
2313 cm
= mtod(control
, struct cmsghdr
*);
2314 if (cm
->cmsg_len
== 0 || cm
->cmsg_len
> control
->m_len
)
2316 if (cm
->cmsg_level
!= IPPROTO_IPV6
)
2320 * XXX should check if RFC2292 API is mixed with 2292bis API
2322 switch (cm
->cmsg_type
) {
2324 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(struct in6_pktinfo
)))
2327 /* XXX: Is it really WAITOK? */
2328 opt
->ip6po_pktinfo
=
2329 _MALLOC(sizeof(struct in6_pktinfo
),
2330 M_IP6OPT
, M_WAITOK
);
2331 bcopy(CMSG_DATA(cm
), opt
->ip6po_pktinfo
,
2332 sizeof(struct in6_pktinfo
));
2334 opt
->ip6po_pktinfo
=
2335 (struct in6_pktinfo
*)CMSG_DATA(cm
);
2336 if (opt
->ip6po_pktinfo
->ipi6_ifindex
&&
2337 IN6_IS_ADDR_LINKLOCAL(&opt
->ip6po_pktinfo
->ipi6_addr
))
2338 opt
->ip6po_pktinfo
->ipi6_addr
.s6_addr16
[1] =
2339 htons(opt
->ip6po_pktinfo
->ipi6_ifindex
);
2341 if (opt
->ip6po_pktinfo
->ipi6_ifindex
> if_index
2342 || opt
->ip6po_pktinfo
->ipi6_ifindex
< 0) {
2347 * Check if the requested source address is indeed a
2348 * unicast address assigned to the node, and can be
2349 * used as the packet's source address.
2351 if (!IN6_IS_ADDR_UNSPECIFIED(&opt
->ip6po_pktinfo
->ipi6_addr
)) {
2352 struct in6_ifaddr
*ia6
;
2353 struct sockaddr_in6 sin6
;
2355 bzero(&sin6
, sizeof(sin6
));
2356 sin6
.sin6_len
= sizeof(sin6
);
2357 sin6
.sin6_family
= AF_INET6
;
2359 opt
->ip6po_pktinfo
->ipi6_addr
;
2360 ia6
= (struct in6_ifaddr
*)ifa_ifwithaddr(sin6tosa(&sin6
));
2362 (ia6
->ia6_flags
& (IN6_IFF_ANYCAST
|
2363 IN6_IFF_NOTREADY
)) != 0)
2364 return(EADDRNOTAVAIL
);
2369 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(int)))
2372 opt
->ip6po_hlim
= *(int *)CMSG_DATA(cm
);
2373 if (opt
->ip6po_hlim
< -1 || opt
->ip6po_hlim
> 255)
2381 if (cm
->cmsg_len
< sizeof(u_char
) ||
2382 /* check if cmsg_len is large enough for sa_len */
2383 cm
->cmsg_len
< CMSG_LEN(*CMSG_DATA(cm
)))
2387 opt
->ip6po_nexthop
=
2388 _MALLOC(*CMSG_DATA(cm
),
2389 M_IP6OPT
, M_WAITOK
);
2390 bcopy(CMSG_DATA(cm
),
2394 opt
->ip6po_nexthop
=
2395 (struct sockaddr
*)CMSG_DATA(cm
);
2400 struct ip6_hbh
*hbh
;
2403 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_hbh
)))
2405 hbh
= (struct ip6_hbh
*)CMSG_DATA(cm
);
2406 hbhlen
= (hbh
->ip6h_len
+ 1) << 3;
2407 if (cm
->cmsg_len
!= CMSG_LEN(hbhlen
))
2412 _MALLOC(hbhlen
, M_IP6OPT
, M_WAITOK
);
2413 bcopy(hbh
, opt
->ip6po_hbh
, hbhlen
);
2415 opt
->ip6po_hbh
= hbh
;
2421 struct ip6_dest
*dest
, **newdest
;
2424 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_dest
)))
2426 dest
= (struct ip6_dest
*)CMSG_DATA(cm
);
2427 destlen
= (dest
->ip6d_len
+ 1) << 3;
2428 if (cm
->cmsg_len
!= CMSG_LEN(destlen
))
2432 * The old advacned API is ambiguous on this
2433 * point. Our approach is to determine the
2434 * position based according to the existence
2435 * of a routing header. Note, however, that
2436 * this depends on the order of the extension
2437 * headers in the ancillary data; the 1st part
2438 * of the destination options header must
2439 * appear before the routing header in the
2440 * ancillary data, too.
2441 * RFC2292bis solved the ambiguity by
2442 * introducing separate cmsg types.
2444 if (opt
->ip6po_rthdr
== NULL
)
2445 newdest
= &opt
->ip6po_dest1
;
2447 newdest
= &opt
->ip6po_dest2
;
2450 *newdest
= _MALLOC(destlen
, M_IP6OPT
, M_WAITOK
);
2451 bcopy(dest
, *newdest
, destlen
);
2460 struct ip6_rthdr
*rth
;
2463 if (cm
->cmsg_len
< CMSG_LEN(sizeof(struct ip6_rthdr
)))
2465 rth
= (struct ip6_rthdr
*)CMSG_DATA(cm
);
2466 rthlen
= (rth
->ip6r_len
+ 1) << 3;
2467 if (cm
->cmsg_len
!= CMSG_LEN(rthlen
))
2470 switch (rth
->ip6r_type
) {
2471 case IPV6_RTHDR_TYPE_0
:
2472 /* must contain one addr */
2473 if (rth
->ip6r_len
== 0)
2475 /* length must be even */
2476 if (rth
->ip6r_len
% 2)
2478 if (rth
->ip6r_len
/ 2 != rth
->ip6r_segleft
)
2482 return(EINVAL
); /* not supported */
2486 opt
->ip6po_rthdr
= _MALLOC(rthlen
, M_IP6OPT
,
2488 bcopy(rth
, opt
->ip6po_rthdr
, rthlen
);
2490 opt
->ip6po_rthdr
= rth
;
2496 return(ENOPROTOOPT
);
2504 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2505 * packet to the input queue of a specified interface. Note that this
2506 * calls the output routine of the loopback "driver", but with an interface
2507 * pointer that might NOT be &loif -- easier than replicating that code here.
2510 ip6_mloopback(ifp
, m
, dst
)
2513 struct sockaddr_in6
*dst
;
2516 struct ip6_hdr
*ip6
;
2518 copym
= m_copy(m
, 0, M_COPYALL
);
2523 * Make sure to deep-copy IPv6 header portion in case the data
2524 * is in an mbuf cluster, so that we can safely override the IPv6
2525 * header portion later.
2527 if ((copym
->m_flags
& M_EXT
) != 0 ||
2528 copym
->m_len
< sizeof(struct ip6_hdr
)) {
2529 copym
= m_pullup(copym
, sizeof(struct ip6_hdr
));
2535 if (copym
->m_len
< sizeof(*ip6
)) {
2541 ip6
= mtod(copym
, struct ip6_hdr
*);
2542 #ifndef SCOPEDROUTING
2544 * clear embedded scope identifiers if necessary.
2545 * in6_clearscope will touch the addresses only when necessary.
2547 in6_clearscope(&ip6
->ip6_src
);
2548 in6_clearscope(&ip6
->ip6_dst
);
2553 /* Makes sure the HW checksum flags are cleaned before sending the packet */
2555 copym
->m_pkthdr
.rcvif
= (struct ifnet
*)0;
2556 copym
->m_pkthdr
.csum_data
= 0;
2557 copym
->m_pkthdr
.csum_flags
= 0;
2560 dlil_find_dltag(APPLE_IF_FAM_LOOPBACK
, 0, PF_INET
, &lo_dl_tag
);
2563 copym
->m_pkthdr
.rcvif
= ifp
;
2564 dlil_output(lo_dl_tag
, copym
, 0, (struct sockaddr
*)dst
, 0);
2568 (void)if_simloop(ifp
, copym
, dst
->sin6_family
, NULL
);
2573 * Chop IPv6 header off from the payload.
2576 ip6_splithdr(m
, exthdrs
)
2578 struct ip6_exthdrs
*exthdrs
;
2581 struct ip6_hdr
*ip6
;
2583 ip6
= mtod(m
, struct ip6_hdr
*);
2584 if (m
->m_len
> sizeof(*ip6
)) {
2585 MGETHDR(mh
, M_DONTWAIT
, MT_HEADER
);
2590 M_COPY_PKTHDR(mh
, m
);
2591 MH_ALIGN(mh
, sizeof(*ip6
));
2592 m
->m_flags
&= ~M_PKTHDR
;
2593 m
->m_len
-= sizeof(*ip6
);
2594 m
->m_data
+= sizeof(*ip6
);
2597 m
->m_len
= sizeof(*ip6
);
2598 bcopy((caddr_t
)ip6
, mtod(m
, caddr_t
), sizeof(*ip6
));
2600 exthdrs
->ip6e_ip6
= m
;
2605 * Compute IPv6 extension header length.
2609 struct in6pcb
*in6p
;
2613 if (!in6p
->in6p_outputopts
)
2618 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2620 len
+= elen(in6p
->in6p_outputopts
->ip6po_hbh
);
2621 if (in6p
->in6p_outputopts
->ip6po_rthdr
)
2622 /* dest1 is valid with rthdr only */
2623 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest1
);
2624 len
+= elen(in6p
->in6p_outputopts
->ip6po_rthdr
);
2625 len
+= elen(in6p
->in6p_outputopts
->ip6po_dest2
);