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 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
64 #include "opt_ip6fw.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
69 #include <sys/param.h>
70 #include <sys/malloc.h>
73 #include <sys/errno.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
81 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_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 <netinet/tcp_var.h>
92 #include <netinet6/nd6.h>
95 #include <netinet6/ipsec.h>
97 #include <netinet6/ipsec6.h>
99 #include <netkey/key.h>
103 #include <netipsec/ipsec.h>
104 #include <netipsec/ipsec6.h>
105 #include <netipsec/key.h>
106 #endif /* FAST_IPSEC */
108 #include <netinet6/ip6_fw.h>
110 #include <net/net_osdep.h>
112 #include <netinet6/ip6protosw.h>
114 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
117 struct mbuf *ip6e_ip6;
118 struct mbuf *ip6e_hbh;
119 struct mbuf *ip6e_dest1;
120 struct mbuf *ip6e_rthdr;
121 struct mbuf *ip6e_dest2;
124 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
126 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
127 struct socket *, struct sockopt *));
128 static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *));
129 static int ip6_setpktoption __P((int, u_char *, int, struct ip6_pktopts *, int,
132 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
133 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
134 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
135 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
136 struct ip6_frag **));
137 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
138 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
139 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
140 struct ifnet *, struct in6_addr *, u_long *, int *));
144 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
145 * header (with pri, len, nxt, hlim, src, dst).
146 * This function may modify ver and hlim only.
147 * The mbuf chain containing the packet will be freed.
148 * The mbuf opt, if present, will not be freed.
150 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
151 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
152 * which is rt_rmx.rmx_mtu.
155 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
157 struct ip6_pktopts *opt;
158 struct route_in6 *ro;
160 struct ip6_moptions *im6o;
161 struct ifnet **ifpp; /* XXX: just for statistics */
164 struct ip6_hdr *ip6, *mhip6;
165 struct ifnet *ifp, *origifp;
167 int hlen, tlen, len, off;
168 struct route_in6 ip6route;
169 struct sockaddr_in6 *dst;
171 struct in6_ifaddr *ia = NULL;
173 int alwaysfrag, dontfrag;
174 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
175 struct ip6_exthdrs exthdrs;
176 struct in6_addr finaldst;
177 struct route_in6 *ro_pmtu = NULL;
180 #if defined(IPSEC) || defined(FAST_IPSEC)
181 int needipsectun = 0;
182 struct secpolicy *sp = NULL;
183 #endif /*IPSEC || FAST_IPSEC*/
185 ip6 = mtod(m, struct ip6_hdr *);
186 finaldst = ip6->ip6_dst;
188 #define MAKE_EXTHDR(hp, mp) \
191 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
192 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
193 ((eh)->ip6e_len + 1) << 3); \
197 } while (/*CONSTCOND*/ 0)
199 bzero(&exthdrs, sizeof(exthdrs));
202 /* Hop-by-Hop options header */
203 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
204 /* Destination options header(1st part) */
205 if (opt->ip6po_rthdr) {
207 * Destination options header(1st part)
208 * This only makes sence with a routing header.
209 * See Section 9.2 of RFC 3542.
210 * Disabling this part just for MIP6 convenience is
211 * a bad idea. We need to think carefully about a
212 * way to make the advanced API coexist with MIP6
213 * options, which might automatically be inserted in
216 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
219 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
220 /* Destination options header(2nd part) */
221 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
225 /* get a security policy for this packet */
227 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
229 sp = ipsec6_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
232 ipsec6stat.out_inval++;
239 switch (sp->policy) {
240 case IPSEC_POLICY_DISCARD:
242 * This packet is just discarded.
244 ipsec6stat.out_polvio++;
247 case IPSEC_POLICY_BYPASS:
248 case IPSEC_POLICY_NONE:
249 /* no need to do IPsec. */
253 case IPSEC_POLICY_IPSEC:
254 if (sp->req == NULL) {
255 /* acquire a policy */
256 error = key_spdacquire(sp);
262 case IPSEC_POLICY_ENTRUST:
264 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
268 /* get a security policy for this packet */
270 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
272 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
275 newipsecstat.ips_out_inval++;
282 switch (sp->policy) {
283 case IPSEC_POLICY_DISCARD:
285 * This packet is just discarded.
287 newipsecstat.ips_out_polvio++;
290 case IPSEC_POLICY_BYPASS:
291 case IPSEC_POLICY_NONE:
292 /* no need to do IPsec. */
296 case IPSEC_POLICY_IPSEC:
297 if (sp->req == NULL) {
298 /* acquire a policy */
299 error = key_spdacquire(sp);
305 case IPSEC_POLICY_ENTRUST:
307 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
309 #endif /* FAST_IPSEC */
312 * Calculate the total length of the extension header chain.
313 * Keep the length of the unfragmentable part for fragmentation.
316 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
317 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
318 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
319 unfragpartlen = optlen + sizeof(struct ip6_hdr);
320 /* NOTE: we don't add AH/ESP length here. do that later. */
321 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
324 * If we need IPsec, or there is at least one extension header,
325 * separate IP6 header from the payload.
327 if ((needipsec || optlen) && !hdrsplit) {
328 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
332 m = exthdrs.ip6e_ip6;
337 ip6 = mtod(m, struct ip6_hdr *);
339 /* adjust mbuf packet header length */
340 m->m_pkthdr.len += optlen;
341 plen = m->m_pkthdr.len - sizeof(*ip6);
343 /* If this is a jumbo payload, insert a jumbo payload option. */
344 if (plen > IPV6_MAXPACKET) {
346 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
350 m = exthdrs.ip6e_ip6;
354 ip6 = mtod(m, struct ip6_hdr *);
355 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
359 ip6->ip6_plen = htons(plen);
362 * Concatenate headers and fill in next header fields.
363 * Here we have, on "m"
365 * and we insert headers accordingly. Finally, we should be getting:
366 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
368 * during the header composing process, "m" points to IPv6 header.
369 * "mprev" points to an extension header prior to esp.
372 u_char *nexthdrp = &ip6->ip6_nxt;
373 struct mbuf *mprev = m;
376 * we treat dest2 specially. this makes IPsec processing
377 * much easier. the goal here is to make mprev point the
378 * mbuf prior to dest2.
380 * result: IPv6 dest2 payload
381 * m and mprev will point to IPv6 header.
383 if (exthdrs.ip6e_dest2) {
385 panic("assumption failed: hdr not split");
386 exthdrs.ip6e_dest2->m_next = m->m_next;
387 m->m_next = exthdrs.ip6e_dest2;
388 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
389 ip6->ip6_nxt = IPPROTO_DSTOPTS;
392 #define MAKE_CHAIN(m, mp, p, i)\
396 panic("assumption failed: hdr not split"); \
397 *mtod((m), u_char *) = *(p);\
399 p = mtod((m), u_char *);\
400 (m)->m_next = (mp)->m_next;\
404 } while (/*CONSTCOND*/ 0)
406 * result: IPv6 hbh dest1 rthdr dest2 payload
407 * m will point to IPv6 header. mprev will point to the
408 * extension header prior to dest2 (rthdr in the above case).
410 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
411 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
413 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
416 #if defined(IPSEC) || defined(FAST_IPSEC)
421 * pointers after IPsec headers are not valid any more.
422 * other pointers need a great care too.
423 * (IPsec routines should not mangle mbufs prior to AH/ESP)
425 exthdrs.ip6e_dest2 = NULL;
428 struct ip6_rthdr *rh = NULL;
430 struct ipsec_output_state state;
432 if (exthdrs.ip6e_rthdr) {
433 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
434 segleft_org = rh->ip6r_segleft;
435 rh->ip6r_segleft = 0;
438 bzero(&state, sizeof(state));
440 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
444 /* mbuf is already reclaimed in ipsec6_output_trans. */
454 printf("ip6_output (ipsec): error code %d\n", error);
457 /* don't show these error codes to the user */
463 if (exthdrs.ip6e_rthdr) {
464 /* ah6_output doesn't modify mbuf chain */
465 rh->ip6r_segleft = segleft_org;
473 * If there is a routing header, replace the destination address field
474 * with the first hop of the routing header.
476 if (exthdrs.ip6e_rthdr) {
477 struct ip6_rthdr *rh =
478 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
479 struct ip6_rthdr *));
480 struct ip6_rthdr0 *rh0;
481 struct in6_addr *addrs;
483 switch (rh->ip6r_type) {
484 case IPV6_RTHDR_TYPE_0:
485 rh0 = (struct ip6_rthdr0 *)rh;
486 addrs = (struct in6_addr *)(rh0 + 1);
488 ip6->ip6_dst = *addrs;
489 bcopy((caddr_t)(addrs + 1), (caddr_t)addrs,
490 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
492 *(addrs + rh0->ip6r0_segleft - 1) = finaldst;
494 default: /* is it possible? */
500 /* Source address validation */
501 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
502 (flags & IPV6_DADOUTPUT) == 0) {
504 ip6stat.ip6s_badscope++;
507 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
509 ip6stat.ip6s_badscope++;
513 ip6stat.ip6s_localout++;
520 bzero((caddr_t)ro, sizeof(*ro));
523 if (opt && opt->ip6po_rthdr)
524 ro = &opt->ip6po_route;
525 dst = (struct sockaddr_in6 *)&ro->ro_dst;
528 * If there is a cached route,
529 * check that it is to the same destination
530 * and is still up. If not, free it and try again.
532 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
533 dst->sin6_family != AF_INET6 ||
534 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
536 ro->ro_rt = (struct rtentry *)0;
538 if (ro->ro_rt == 0) {
539 bzero(dst, sizeof(*dst));
540 dst->sin6_family = AF_INET6;
541 dst->sin6_len = sizeof(struct sockaddr_in6);
542 dst->sin6_addr = ip6->ip6_dst;
546 * if specified, try to fill in the traffic class field.
547 * do not override if a non-zero value is already set.
548 * we check the diffserv field and the ecn field separately.
550 if (opt && opt->ip6po_tclass >= 0) {
553 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
555 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
558 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
561 /* fill in or override the hop limit field, if necessary. */
562 if (opt && opt->ip6po_hlim != -1)
563 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
564 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
566 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
568 ip6->ip6_hlim = ip6_defmcasthlim;
571 #if defined(IPSEC) || defined(FAST_IPSEC)
572 if (needipsec && needipsectun) {
573 struct ipsec_output_state state;
576 * All the extension headers will become inaccessible
577 * (since they can be encrypted).
578 * Don't panic, we need no more updates to extension headers
579 * on inner IPv6 packet (since they are now encapsulated).
581 * IPv6 [ESP|AH] IPv6 [extension headers] payload
583 bzero(&exthdrs, sizeof(exthdrs));
584 exthdrs.ip6e_ip6 = m;
586 bzero(&state, sizeof(state));
588 state.ro = (struct route *)ro;
589 state.dst = (struct sockaddr *)dst;
591 error = ipsec6_output_tunnel(&state, sp, flags);
594 ro = (struct route_in6 *)state.ro;
595 dst = (struct sockaddr_in6 *)state.dst;
597 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
608 printf("ip6_output (ipsec): error code %d\n", error);
611 /* don't show these error codes to the user */
618 exthdrs.ip6e_ip6 = m;
622 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
625 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
626 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
628 * interface selection comes here
629 * if an interface is specified from an upper layer,
632 if (ro->ro_rt == 0) {
634 * non-bsdi always clone routes, if parent is
637 rtalloc((struct route *)ro);
639 if (ro->ro_rt == 0) {
640 ip6stat.ip6s_noroute++;
641 error = EHOSTUNREACH;
642 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
645 /* XXX rt not locked */
646 ia = ifatoia6(ro->ro_rt->rt_ifa);
647 ifp = ro->ro_rt->rt_ifp;
648 ro->ro_rt->rt_rmx.rmx_pksent++;
649 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
650 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
651 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
653 in6_ifstat_inc(ifp, ifs6_out_request);
656 * Check if the outgoing interface conflicts with
657 * the interface specified by ifi6_ifindex (if specified).
658 * Note that loopback interface is always okay.
659 * (this may happen when we are sending a packet to one of
660 * our own addresses.)
662 if (opt && opt->ip6po_pktinfo
663 && opt->ip6po_pktinfo->ipi6_ifindex) {
664 if (!(ifp->if_flags & IFF_LOOPBACK)
665 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
666 ip6stat.ip6s_noroute++;
667 in6_ifstat_inc(ifp, ifs6_out_discard);
668 error = EHOSTUNREACH;
673 if (opt && opt->ip6po_hlim != -1)
674 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
677 struct in6_multi *in6m;
679 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
682 * See if the caller provided any multicast options
686 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
687 if (im6o->im6o_multicast_ifp != NULL)
688 ifp = im6o->im6o_multicast_ifp;
690 ip6->ip6_hlim = ip6_defmcasthlim;
693 * See if the caller provided the outgoing interface
694 * as an ancillary data.
695 * Boundary check for ifindex is assumed to be already done.
697 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
698 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex);
701 * If the destination is a node-local scope multicast,
702 * the packet should be loop-backed only.
704 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
706 * If the outgoing interface is already specified,
707 * it should be a loopback interface.
709 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
710 ip6stat.ip6s_badscope++;
711 error = ENETUNREACH; /* XXX: better error? */
712 /* XXX correct ifp? */
713 in6_ifstat_inc(ifp, ifs6_out_discard);
720 if (opt && opt->ip6po_hlim != -1)
721 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
724 * If caller did not provide an interface lookup a
725 * default in the routing table. This is either a
726 * default for the speicfied group (i.e. a host
727 * route), or a multicast default (a route for the
732 ro->ro_rt = rtalloc1((struct sockaddr *)
733 &ro->ro_dst, 0, 0UL);
736 if (ro->ro_rt == 0) {
737 ip6stat.ip6s_noroute++;
738 error = EHOSTUNREACH;
739 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
742 ia = ifatoia6(ro->ro_rt->rt_ifa);
743 ifp = ro->ro_rt->rt_ifp;
744 ro->ro_rt->rt_rmx.rmx_pksent++;
745 RT_UNLOCK(ro->ro_rt);
748 if ((flags & IPV6_FORWARDING) == 0)
749 in6_ifstat_inc(ifp, ifs6_out_request);
750 in6_ifstat_inc(ifp, ifs6_out_mcast);
753 * Confirm that the outgoing interface supports multicast.
755 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
756 ip6stat.ip6s_noroute++;
757 in6_ifstat_inc(ifp, ifs6_out_discard);
761 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
763 (im6o == NULL || im6o->im6o_multicast_loop)) {
765 * If we belong to the destination multicast group
766 * on the outgoing interface, and the caller did not
767 * forbid loopback, loop back a copy.
769 ip6_mloopback(ifp, m, dst);
772 * If we are acting as a multicast router, perform
773 * multicast forwarding as if the packet had just
774 * arrived on the interface to which we are about
775 * to send. The multicast forwarding function
776 * recursively calls this function, using the
777 * IPV6_FORWARDING flag to prevent infinite recursion.
779 * Multicasts that are looped back by ip6_mloopback(),
780 * above, will be forwarded by the ip6_input() routine,
783 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
784 if (ip6_mforward(ip6, ifp, m) != 0) {
791 * Multicasts with a hoplimit of zero may be looped back,
792 * above, but must not be transmitted on a network.
793 * Also, multicasts addressed to the loopback interface
794 * are not sent -- the above call to ip6_mloopback() will
795 * loop back a copy if this host actually belongs to the
796 * destination group on the loopback interface.
798 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
799 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
806 * Fill the outgoing inteface to tell the upper layer
807 * to increment per-interface statistics.
812 /* Determine path MTU. */
813 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
818 * The caller of this function may specify to use the minimum MTU
820 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
821 * setting. The logic is a bit complicated; by default, unicast
822 * packets will follow path MTU while multicast packets will be sent at
823 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
824 * including unicast ones will be sent at the minimum MTU. Multicast
825 * packets will always be sent at the minimum MTU unless
826 * IP6PO_MINMTU_DISABLE is explicitly specified.
827 * See RFC 3542 for more details.
829 if (mtu > IPV6_MMTU) {
830 if ((flags & IPV6_MINMTU))
832 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
834 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
836 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
841 /* Fake scoped addresses */
842 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
844 * If source or destination address is a scoped address, and
845 * the packet is going to be sent to a loopback interface,
846 * we should keep the original interface.
850 * XXX: this is a very experimental and temporary solution.
851 * We eventually have sockaddr_in6 and use the sin6_scope_id
852 * field of the structure here.
853 * We rely on the consistency between two scope zone ids
854 * of source and destination, which should already be assured.
855 * Larger scopes than link will be supported in the future.
858 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
859 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1]));
860 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
861 origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1]));
863 * XXX: origifp can be NULL even in those two cases above.
864 * For example, if we remove the (only) link-local address
865 * from the loopback interface, and try to send a link-local
866 * address without link-id information. Then the source
867 * address is ::1, and the destination address is the
868 * link-local address with its s6_addr16[1] being zero.
869 * What is worse, if the packet goes to the loopback interface
870 * by a default rejected route, the null pointer would be
871 * passed to looutput, and the kernel would hang.
872 * The following last resort would prevent such disaster.
880 * clear embedded scope identifiers if necessary.
881 * in6_clearscope will touch the addresses only when necessary.
883 in6_clearscope(&ip6->ip6_src);
884 in6_clearscope(&ip6->ip6_dst);
887 * Check with the firewall...
889 if (ip6_fw_enable && ip6_fw_chk_ptr) {
891 m->m_pkthdr.rcvif = NULL; /* XXX */
892 /* If ipfw says divert, we have to just drop packet */
893 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
904 * If the outgoing packet contains a hop-by-hop options header,
905 * it must be examined and processed even by the source node.
906 * (RFC 2460, section 4.)
908 if (exthdrs.ip6e_hbh) {
909 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
910 u_int32_t dummy1; /* XXX unused */
911 u_int32_t dummy2; /* XXX unused */
914 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
915 panic("ip6e_hbh is not continuous");
918 * XXX: if we have to send an ICMPv6 error to the sender,
919 * we need the M_LOOP flag since icmp6_error() expects
920 * the IPv6 and the hop-by-hop options header are
921 * continuous unless the flag is set.
923 m->m_flags |= M_LOOP;
924 m->m_pkthdr.rcvif = ifp;
925 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
926 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
927 &dummy1, &dummy2) < 0) {
928 /* m was already freed at this point */
929 error = EINVAL;/* better error? */
932 m->m_flags &= ~M_LOOP; /* XXX */
933 m->m_pkthdr.rcvif = NULL;
936 /* Jump over all PFIL processing if hooks are not active. */
937 if (inet6_pfil_hook.ph_busy_count == -1)
940 /* Run through list of hooks for output packets. */
941 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
942 if (error != 0 || m == NULL)
944 ip6 = mtod(m, struct ip6_hdr *);
948 * Send the packet to the outgoing interface.
949 * If necessary, do IPv6 fragmentation before sending.
951 * the logic here is rather complex:
952 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
953 * 1-a: send as is if tlen <= path mtu
954 * 1-b: fragment if tlen > path mtu
956 * 2: if user asks us not to fragment (dontfrag == 1)
957 * 2-a: send as is if tlen <= interface mtu
958 * 2-b: error if tlen > interface mtu
960 * 3: if we always need to attach fragment header (alwaysfrag == 1)
963 * 4: if dontfrag == 1 && alwaysfrag == 1
964 * error, as we cannot handle this conflicting request
966 tlen = m->m_pkthdr.len;
968 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
972 if (dontfrag && alwaysfrag) { /* case 4 */
973 /* conflicting request - can't transmit */
977 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
979 * Even if the DONTFRAG option is specified, we cannot send the
980 * packet when the data length is larger than the MTU of the
981 * outgoing interface.
982 * Notify the error by sending IPV6_PATHMTU ancillary data as
983 * well as returning an error code (the latter is not described
987 struct ip6ctlparam ip6cp;
989 mtu32 = (u_int32_t)mtu;
990 bzero(&ip6cp, sizeof(ip6cp));
991 ip6cp.ip6c_cmdarg = (void *)&mtu32;
992 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1000 * transmit packet without fragmentation
1002 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1003 struct in6_ifaddr *ia6;
1005 ip6 = mtod(m, struct ip6_hdr *);
1006 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1008 /* Record statistics for this interface address. */
1009 ia6->ia_ifa.if_opackets++;
1010 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
1013 /* clean ipsec history once it goes out of the node */
1016 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1021 * try to fragment the packet. case 1-b and 3
1023 if (mtu < IPV6_MMTU) {
1024 /* path MTU cannot be less than IPV6_MMTU */
1026 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1028 } else if (ip6->ip6_plen == 0) {
1029 /* jumbo payload cannot be fragmented */
1031 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1034 struct mbuf **mnext, *m_frgpart;
1035 struct ip6_frag *ip6f;
1036 u_int32_t id = htonl(ip6_randomid());
1039 struct ip6ctlparam ip6cp;
1042 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1045 * Too large for the destination or interface;
1046 * fragment if possible.
1047 * Must be able to put at least 8 bytes per fragment.
1049 hlen = unfragpartlen;
1050 if (mtu > IPV6_MAXPACKET)
1051 mtu = IPV6_MAXPACKET;
1055 * It is believed this code is a leftover from the
1056 * development of the IPV6_RECVPATHMTU sockopt and
1057 * associated work to implement RFC3542.
1058 * It's not entirely clear what the intent of the API
1059 * is at this point, so disable this code for now.
1060 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1061 * will send notifications if the application requests.
1064 /* Notify a proper path MTU to applications. */
1065 mtu32 = (u_int32_t)mtu;
1066 bzero(&ip6cp, sizeof(ip6cp));
1067 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1068 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1072 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1075 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1080 * Verify that we have any chance at all of being able to queue
1081 * the packet or packet fragments
1083 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1084 < tlen /* - hlen */)) {
1086 ip6stat.ip6s_odropped++;
1090 mnext = &m->m_nextpkt;
1093 * Change the next header field of the last header in the
1094 * unfragmentable part.
1096 if (exthdrs.ip6e_rthdr) {
1097 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1098 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1099 } else if (exthdrs.ip6e_dest1) {
1100 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1101 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1102 } else if (exthdrs.ip6e_hbh) {
1103 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1104 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1106 nextproto = ip6->ip6_nxt;
1107 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1111 * Loop through length of segment after first fragment,
1112 * make new header and copy data of each part and link onto
1116 for (off = hlen; off < tlen; off += len) {
1117 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1120 ip6stat.ip6s_odropped++;
1123 m->m_pkthdr.rcvif = NULL;
1124 m->m_flags = m0->m_flags & M_COPYFLAGS;
1126 mnext = &m->m_nextpkt;
1127 m->m_data += max_linkhdr;
1128 mhip6 = mtod(m, struct ip6_hdr *);
1130 m->m_len = sizeof(*mhip6);
1131 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1133 ip6stat.ip6s_odropped++;
1136 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1137 if (off + len >= tlen)
1140 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1141 mhip6->ip6_plen = htons((u_short)(len + hlen +
1142 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1143 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1145 ip6stat.ip6s_odropped++;
1148 m_cat(m, m_frgpart);
1149 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1150 m->m_pkthdr.rcvif = (struct ifnet *)0;
1151 ip6f->ip6f_reserved = 0;
1152 ip6f->ip6f_ident = id;
1153 ip6f->ip6f_nxt = nextproto;
1154 ip6stat.ip6s_ofragments++;
1155 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1158 in6_ifstat_inc(ifp, ifs6_out_fragok);
1162 * Remove leading garbages.
1168 for (m0 = m; m; m = m0) {
1172 /* Record statistics for this interface address. */
1174 ia->ia_ifa.if_opackets++;
1175 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1178 /* clean ipsec history once it goes out of the node */
1181 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1187 ip6stat.ip6s_fragmented++;
1190 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1192 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1193 RTFREE(ro_pmtu->ro_rt);
1203 #endif /* FAST_IPSEC */
1208 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1209 m_freem(exthdrs.ip6e_dest1);
1210 m_freem(exthdrs.ip6e_rthdr);
1211 m_freem(exthdrs.ip6e_dest2);
1219 ip6_copyexthdr(mp, hdr, hlen)
1226 if (hlen > MCLBYTES)
1227 return (ENOBUFS); /* XXX */
1229 MGET(m, M_DONTWAIT, MT_DATA);
1234 MCLGET(m, M_DONTWAIT);
1235 if ((m->m_flags & M_EXT) == 0) {
1242 bcopy(hdr, mtod(m, caddr_t), hlen);
1249 * Insert jumbo payload option.
1252 ip6_insert_jumboopt(exthdrs, plen)
1253 struct ip6_exthdrs *exthdrs;
1260 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1263 * If there is no hop-by-hop options header, allocate new one.
1264 * If there is one but it doesn't have enough space to store the
1265 * jumbo payload option, allocate a cluster to store the whole options.
1266 * Otherwise, use it to store the options.
1268 if (exthdrs->ip6e_hbh == 0) {
1269 MGET(mopt, M_DONTWAIT, MT_DATA);
1272 mopt->m_len = JUMBOOPTLEN;
1273 optbuf = mtod(mopt, u_char *);
1274 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1275 exthdrs->ip6e_hbh = mopt;
1277 struct ip6_hbh *hbh;
1279 mopt = exthdrs->ip6e_hbh;
1280 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1283 * - exthdrs->ip6e_hbh is not referenced from places
1284 * other than exthdrs.
1285 * - exthdrs->ip6e_hbh is not an mbuf chain.
1287 int oldoptlen = mopt->m_len;
1291 * XXX: give up if the whole (new) hbh header does
1292 * not fit even in an mbuf cluster.
1294 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1298 * As a consequence, we must always prepare a cluster
1301 MGET(n, M_DONTWAIT, MT_DATA);
1303 MCLGET(n, M_DONTWAIT);
1304 if ((n->m_flags & M_EXT) == 0) {
1311 n->m_len = oldoptlen + JUMBOOPTLEN;
1312 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1314 optbuf = mtod(n, caddr_t) + oldoptlen;
1316 mopt = exthdrs->ip6e_hbh = n;
1318 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1319 mopt->m_len += JUMBOOPTLEN;
1321 optbuf[0] = IP6OPT_PADN;
1325 * Adjust the header length according to the pad and
1326 * the jumbo payload option.
1328 hbh = mtod(mopt, struct ip6_hbh *);
1329 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1332 /* fill in the option. */
1333 optbuf[2] = IP6OPT_JUMBO;
1335 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1336 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1338 /* finally, adjust the packet header length */
1339 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1346 * Insert fragment header and copy unfragmentable header portions.
1349 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1350 struct mbuf *m0, *m;
1352 struct ip6_frag **frghdrp;
1354 struct mbuf *n, *mlast;
1356 if (hlen > sizeof(struct ip6_hdr)) {
1357 n = m_copym(m0, sizeof(struct ip6_hdr),
1358 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1365 /* Search for the last mbuf of unfragmentable part. */
1366 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1369 if ((mlast->m_flags & M_EXT) == 0 &&
1370 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1371 /* use the trailing space of the last mbuf for the fragment hdr */
1372 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1374 mlast->m_len += sizeof(struct ip6_frag);
1375 m->m_pkthdr.len += sizeof(struct ip6_frag);
1377 /* allocate a new mbuf for the fragment header */
1380 MGET(mfrg, M_DONTWAIT, MT_DATA);
1383 mfrg->m_len = sizeof(struct ip6_frag);
1384 *frghdrp = mtod(mfrg, struct ip6_frag *);
1385 mlast->m_next = mfrg;
1392 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1393 struct route_in6 *ro_pmtu, *ro;
1395 struct in6_addr *dst;
1403 if (ro_pmtu != ro) {
1404 /* The first hop and the final destination may differ. */
1405 struct sockaddr_in6 *sa6_dst =
1406 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1407 if (ro_pmtu->ro_rt &&
1408 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1409 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1410 RTFREE(ro_pmtu->ro_rt);
1411 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1413 if (ro_pmtu->ro_rt == NULL) {
1414 bzero(sa6_dst, sizeof(*sa6_dst));
1415 sa6_dst->sin6_family = AF_INET6;
1416 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1417 sa6_dst->sin6_addr = *dst;
1419 rtalloc((struct route *)ro_pmtu);
1422 if (ro_pmtu->ro_rt) {
1424 struct in_conninfo inc;
1426 bzero(&inc, sizeof(inc));
1427 inc.inc_flags = 1; /* IPv6 */
1428 inc.inc6_faddr = *dst;
1431 ifp = ro_pmtu->ro_rt->rt_ifp;
1432 ifmtu = IN6_LINKMTU(ifp);
1433 mtu = tcp_hc_getmtu(&inc);
1435 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1437 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1440 else if (mtu < IPV6_MMTU) {
1442 * RFC2460 section 5, last paragraph:
1443 * if we record ICMPv6 too big message with
1444 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1445 * or smaller, with framgent header attached.
1446 * (fragment header is needed regardless from the
1447 * packet size, for translators to identify packets)
1451 } else if (mtu > ifmtu) {
1453 * The MTU on the route is larger than the MTU on
1454 * the interface! This shouldn't happen, unless the
1455 * MTU of the interface has been changed after the
1456 * interface was brought up. Change the MTU in the
1457 * route to match the interface MTU (as long as the
1458 * field isn't locked).
1461 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1464 mtu = IN6_LINKMTU(ifp);
1466 error = EHOSTUNREACH; /* XXX */
1470 *alwaysfragp = alwaysfrag;
1475 * IP6 socket option processing.
1478 ip6_ctloutput(so, sopt)
1480 struct sockopt *sopt;
1482 int privileged, optdatalen, uproto;
1484 struct inpcb *in6p = sotoinpcb(so);
1486 int level, op, optname;
1491 level = sopt->sopt_level;
1492 op = sopt->sopt_dir;
1493 optname = sopt->sopt_name;
1494 optlen = sopt->sopt_valsize;
1497 panic("ip6_ctloutput: arg soopt is NULL");
1501 privileged = (td == 0 || suser(td)) ? 0 : 1;
1502 uproto = (int)so->so_proto->pr_protocol;
1504 if (level == IPPROTO_IPV6) {
1509 case IPV6_2292PKTOPTIONS:
1510 #ifdef IPV6_PKTOPTIONS
1511 case IPV6_PKTOPTIONS:
1516 error = soopt_getm(sopt, &m); /* XXX */
1519 error = soopt_mcopyin(sopt, m); /* XXX */
1522 error = ip6_pcbopts(&in6p->in6p_outputopts,
1524 m_freem(m); /* XXX */
1529 * Use of some Hop-by-Hop options or some
1530 * Destination options, might require special
1531 * privilege. That is, normal applications
1532 * (without special privilege) might be forbidden
1533 * from setting certain options in outgoing packets,
1534 * and might never see certain options in received
1535 * packets. [RFC 2292 Section 6]
1536 * KAME specific note:
1537 * KAME prevents non-privileged users from sending or
1538 * receiving ANY hbh/dst options in order to avoid
1539 * overhead of parsing options in the kernel.
1541 case IPV6_RECVHOPOPTS:
1542 case IPV6_RECVDSTOPTS:
1543 case IPV6_RECVRTHDRDSTOPTS:
1549 case IPV6_UNICAST_HOPS:
1553 case IPV6_RECVPKTINFO:
1554 case IPV6_RECVHOPLIMIT:
1555 case IPV6_RECVRTHDR:
1556 case IPV6_RECVPATHMTU:
1557 case IPV6_RECVTCLASS:
1559 case IPV6_AUTOFLOWLABEL:
1560 if (optlen != sizeof(int)) {
1564 error = sooptcopyin(sopt, &optval,
1565 sizeof optval, sizeof optval);
1570 case IPV6_UNICAST_HOPS:
1571 if (optval < -1 || optval >= 256)
1574 /* -1 = kernel default */
1575 in6p->in6p_hops = optval;
1576 if ((in6p->in6p_vflag &
1578 in6p->inp_ip_ttl = optval;
1581 #define OPTSET(bit) \
1584 in6p->in6p_flags |= (bit); \
1586 in6p->in6p_flags &= ~(bit); \
1587 } while (/*CONSTCOND*/ 0)
1588 #define OPTSET2292(bit) \
1590 in6p->in6p_flags |= IN6P_RFC2292; \
1592 in6p->in6p_flags |= (bit); \
1594 in6p->in6p_flags &= ~(bit); \
1595 } while (/*CONSTCOND*/ 0)
1596 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1598 case IPV6_RECVPKTINFO:
1599 /* cannot mix with RFC2292 */
1600 if (OPTBIT(IN6P_RFC2292)) {
1604 OPTSET(IN6P_PKTINFO);
1609 struct ip6_pktopts **optp;
1611 /* cannot mix with RFC2292 */
1612 if (OPTBIT(IN6P_RFC2292)) {
1616 optp = &in6p->in6p_outputopts;
1617 error = ip6_pcbopt(IPV6_HOPLIMIT,
1621 privileged, uproto);
1625 case IPV6_RECVHOPLIMIT:
1626 /* cannot mix with RFC2292 */
1627 if (OPTBIT(IN6P_RFC2292)) {
1631 OPTSET(IN6P_HOPLIMIT);
1634 case IPV6_RECVHOPOPTS:
1635 /* cannot mix with RFC2292 */
1636 if (OPTBIT(IN6P_RFC2292)) {
1640 OPTSET(IN6P_HOPOPTS);
1643 case IPV6_RECVDSTOPTS:
1644 /* cannot mix with RFC2292 */
1645 if (OPTBIT(IN6P_RFC2292)) {
1649 OPTSET(IN6P_DSTOPTS);
1652 case IPV6_RECVRTHDRDSTOPTS:
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1658 OPTSET(IN6P_RTHDRDSTOPTS);
1661 case IPV6_RECVRTHDR:
1662 /* cannot mix with RFC2292 */
1663 if (OPTBIT(IN6P_RFC2292)) {
1674 case IPV6_RECVPATHMTU:
1676 * We ignore this option for TCP
1678 * (rfc2292bis leaves this case
1681 if (uproto != IPPROTO_TCP)
1687 * make setsockopt(IPV6_V6ONLY)
1688 * available only prior to bind(2).
1689 * see ipng mailing list, Jun 22 2001.
1691 if (in6p->in6p_lport ||
1692 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1696 OPTSET(IN6P_IPV6_V6ONLY);
1698 in6p->in6p_vflag &= ~INP_IPV4;
1700 in6p->in6p_vflag |= INP_IPV4;
1702 case IPV6_RECVTCLASS:
1703 /* cannot mix with RFC2292 XXX */
1704 if (OPTBIT(IN6P_RFC2292)) {
1708 OPTSET(IN6P_TCLASS);
1710 case IPV6_AUTOFLOWLABEL:
1711 OPTSET(IN6P_AUTOFLOWLABEL);
1719 case IPV6_USE_MIN_MTU:
1720 case IPV6_PREFER_TEMPADDR:
1721 if (optlen != sizeof(optval)) {
1725 error = sooptcopyin(sopt, &optval,
1726 sizeof optval, sizeof optval);
1730 struct ip6_pktopts **optp;
1731 optp = &in6p->in6p_outputopts;
1732 error = ip6_pcbopt(optname,
1736 privileged, uproto);
1740 case IPV6_2292PKTINFO:
1741 case IPV6_2292HOPLIMIT:
1742 case IPV6_2292HOPOPTS:
1743 case IPV6_2292DSTOPTS:
1744 case IPV6_2292RTHDR:
1746 if (optlen != sizeof(int)) {
1750 error = sooptcopyin(sopt, &optval,
1751 sizeof optval, sizeof optval);
1755 case IPV6_2292PKTINFO:
1756 OPTSET2292(IN6P_PKTINFO);
1758 case IPV6_2292HOPLIMIT:
1759 OPTSET2292(IN6P_HOPLIMIT);
1761 case IPV6_2292HOPOPTS:
1763 * Check super-user privilege.
1764 * See comments for IPV6_RECVHOPOPTS.
1768 OPTSET2292(IN6P_HOPOPTS);
1770 case IPV6_2292DSTOPTS:
1773 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1775 case IPV6_2292RTHDR:
1776 OPTSET2292(IN6P_RTHDR);
1784 case IPV6_RTHDRDSTOPTS:
1787 /* new advanced API (2292bis) */
1790 struct ip6_pktopts **optp;
1792 /* cannot mix with RFC2292 */
1793 if (OPTBIT(IN6P_RFC2292)) {
1801 case IPV6_RTHDRDSTOPTS:
1812 optlen = sizeof(struct in6_pktinfo);
1815 optlen = SOCK_MAXADDRLEN;
1818 optlen = IPV6_MAXOPTHDR;
1821 if (sopt->sopt_valsize > optlen) {
1826 optlen = sopt->sopt_valsize;
1827 optbuf = malloc(optlen, M_TEMP, M_WAITOK);
1828 error = sooptcopyin(sopt, optbuf, optlen,
1831 free(optbuf, M_TEMP);
1835 optp = &in6p->in6p_outputopts;
1836 error = ip6_pcbopt(optname,
1838 optp, privileged, uproto);
1839 free(optbuf, M_TEMP);
1844 case IPV6_MULTICAST_IF:
1845 case IPV6_MULTICAST_HOPS:
1846 case IPV6_MULTICAST_LOOP:
1847 case IPV6_JOIN_GROUP:
1848 case IPV6_LEAVE_GROUP:
1850 if (sopt->sopt_valsize > MLEN) {
1860 if (sopt->sopt_valsize > MCLBYTES) {
1865 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER);
1870 if (sopt->sopt_valsize > MLEN) {
1871 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1872 if ((m->m_flags & M_EXT) == 0) {
1878 m->m_len = sopt->sopt_valsize;
1879 error = sooptcopyin(sopt, mtod(m, char *),
1880 m->m_len, m->m_len);
1885 error = ip6_setmoptions(sopt->sopt_name,
1886 &in6p->in6p_moptions,
1892 case IPV6_PORTRANGE:
1893 error = sooptcopyin(sopt, &optval,
1894 sizeof optval, sizeof optval);
1899 case IPV6_PORTRANGE_DEFAULT:
1900 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1901 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1904 case IPV6_PORTRANGE_HIGH:
1905 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1906 in6p->in6p_flags |= IN6P_HIGHPORT;
1909 case IPV6_PORTRANGE_LOW:
1910 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1911 in6p->in6p_flags |= IN6P_LOWPORT;
1920 #if defined(IPSEC) || defined(FAST_IPSEC)
1921 case IPV6_IPSEC_POLICY:
1927 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1929 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1932 req = mtod(m, caddr_t);
1935 error = ipsec6_set_policy(in6p, optname, req,
1940 #endif /* KAME IPSEC */
1948 struct mbuf **mp = &m;
1950 if (ip6_fw_ctl_ptr == NULL)
1953 if ((error = soopt_getm(sopt, &m)) != 0)
1956 if ((error = soopt_mcopyin(sopt, m)) != 0)
1958 error = (*ip6_fw_ctl_ptr)(optname, mp);
1964 error = ENOPROTOOPT;
1972 case IPV6_2292PKTOPTIONS:
1973 #ifdef IPV6_PKTOPTIONS
1974 case IPV6_PKTOPTIONS:
1977 * RFC3542 (effectively) deprecated the
1978 * semantics of the 2292-style pktoptions.
1979 * Since it was not reliable in nature (i.e.,
1980 * applications had to expect the lack of some
1981 * information after all), it would make sense
1982 * to simplify this part by always returning
1985 sopt->sopt_valsize = 0;
1988 case IPV6_RECVHOPOPTS:
1989 case IPV6_RECVDSTOPTS:
1990 case IPV6_RECVRTHDRDSTOPTS:
1991 case IPV6_UNICAST_HOPS:
1992 case IPV6_RECVPKTINFO:
1993 case IPV6_RECVHOPLIMIT:
1994 case IPV6_RECVRTHDR:
1995 case IPV6_RECVPATHMTU:
1999 case IPV6_PORTRANGE:
2000 case IPV6_RECVTCLASS:
2001 case IPV6_AUTOFLOWLABEL:
2004 case IPV6_RECVHOPOPTS:
2005 optval = OPTBIT(IN6P_HOPOPTS);
2008 case IPV6_RECVDSTOPTS:
2009 optval = OPTBIT(IN6P_DSTOPTS);
2012 case IPV6_RECVRTHDRDSTOPTS:
2013 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2016 case IPV6_UNICAST_HOPS:
2017 optval = in6p->in6p_hops;
2020 case IPV6_RECVPKTINFO:
2021 optval = OPTBIT(IN6P_PKTINFO);
2024 case IPV6_RECVHOPLIMIT:
2025 optval = OPTBIT(IN6P_HOPLIMIT);
2028 case IPV6_RECVRTHDR:
2029 optval = OPTBIT(IN6P_RTHDR);
2032 case IPV6_RECVPATHMTU:
2033 optval = OPTBIT(IN6P_MTU);
2037 optval = OPTBIT(IN6P_FAITH);
2041 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2044 case IPV6_PORTRANGE:
2047 flags = in6p->in6p_flags;
2048 if (flags & IN6P_HIGHPORT)
2049 optval = IPV6_PORTRANGE_HIGH;
2050 else if (flags & IN6P_LOWPORT)
2051 optval = IPV6_PORTRANGE_LOW;
2056 case IPV6_RECVTCLASS:
2057 optval = OPTBIT(IN6P_TCLASS);
2060 case IPV6_AUTOFLOWLABEL:
2061 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2066 error = sooptcopyout(sopt, &optval,
2073 struct ip6_mtuinfo mtuinfo;
2074 struct route_in6 sro;
2076 bzero(&sro, sizeof(sro));
2078 if (!(so->so_state & SS_ISCONNECTED))
2081 * XXX: we dot not consider the case of source
2082 * routing, or optional information to specify
2083 * the outgoing interface.
2085 error = ip6_getpmtu(&sro, NULL, NULL,
2086 &in6p->in6p_faddr, &pmtu, NULL);
2091 if (pmtu > IPV6_MAXPACKET)
2092 pmtu = IPV6_MAXPACKET;
2094 bzero(&mtuinfo, sizeof(mtuinfo));
2095 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2096 optdata = (void *)&mtuinfo;
2097 optdatalen = sizeof(mtuinfo);
2098 error = sooptcopyout(sopt, optdata,
2103 case IPV6_2292PKTINFO:
2104 case IPV6_2292HOPLIMIT:
2105 case IPV6_2292HOPOPTS:
2106 case IPV6_2292RTHDR:
2107 case IPV6_2292DSTOPTS:
2109 case IPV6_2292PKTINFO:
2110 optval = OPTBIT(IN6P_PKTINFO);
2112 case IPV6_2292HOPLIMIT:
2113 optval = OPTBIT(IN6P_HOPLIMIT);
2115 case IPV6_2292HOPOPTS:
2116 optval = OPTBIT(IN6P_HOPOPTS);
2118 case IPV6_2292RTHDR:
2119 optval = OPTBIT(IN6P_RTHDR);
2121 case IPV6_2292DSTOPTS:
2122 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2125 error = sooptcopyout(sopt, &optval,
2132 case IPV6_RTHDRDSTOPTS:
2136 case IPV6_USE_MIN_MTU:
2137 case IPV6_PREFER_TEMPADDR:
2138 error = ip6_getpcbopt(in6p->in6p_outputopts,
2142 case IPV6_MULTICAST_IF:
2143 case IPV6_MULTICAST_HOPS:
2144 case IPV6_MULTICAST_LOOP:
2145 case IPV6_JOIN_GROUP:
2146 case IPV6_LEAVE_GROUP:
2149 error = ip6_getmoptions(sopt->sopt_name,
2150 in6p->in6p_moptions, &m);
2152 error = sooptcopyout(sopt,
2153 mtod(m, char *), m->m_len);
2158 #if defined(IPSEC) || defined(FAST_IPSEC)
2159 case IPV6_IPSEC_POLICY:
2163 struct mbuf *m = NULL;
2164 struct mbuf **mp = &m;
2165 size_t ovalsize = sopt->sopt_valsize;
2166 caddr_t oval = (caddr_t)sopt->sopt_val;
2168 error = soopt_getm(sopt, &m); /* XXX */
2171 error = soopt_mcopyin(sopt, m); /* XXX */
2174 sopt->sopt_valsize = ovalsize;
2175 sopt->sopt_val = oval;
2177 req = mtod(m, caddr_t);
2180 error = ipsec6_get_policy(in6p, req, len, mp);
2182 error = soopt_mcopyout(sopt, m); /* XXX */
2183 if (error == 0 && m)
2187 #endif /* KAME IPSEC */
2192 struct mbuf **mp = &m;
2194 if (ip6_fw_ctl_ptr == NULL)
2198 error = (*ip6_fw_ctl_ptr)(optname, mp);
2200 error = soopt_mcopyout(sopt, m); /* XXX */
2201 if (error == 0 && m)
2207 error = ENOPROTOOPT;
2212 } else { /* level != IPPROTO_IPV6 */
2219 ip6_raw_ctloutput(so, sopt)
2221 struct sockopt *sopt;
2223 int error = 0, optval, optlen;
2224 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2225 struct in6pcb *in6p = sotoin6pcb(so);
2226 int level, op, optname;
2229 level = sopt->sopt_level;
2230 op = sopt->sopt_dir;
2231 optname = sopt->sopt_name;
2232 optlen = sopt->sopt_valsize;
2234 panic("ip6_raw_ctloutput: arg soopt is NULL");
2236 if (level != IPPROTO_IPV6) {
2243 * For ICMPv6 sockets, no modification allowed for checksum
2244 * offset, permit "no change" values to help existing apps.
2246 * XXX 2292bis says: "An attempt to set IPV6_CHECKSUM
2247 * for an ICMPv6 socket will fail."
2248 * The current behavior does not meet 2292bis.
2252 if (optlen != sizeof(int)) {
2256 error = sooptcopyin(sopt, &optval, sizeof(optval),
2260 if ((optval % 2) != 0) {
2261 /* the API assumes even offset values */
2263 } else if (so->so_proto->pr_protocol ==
2265 if (optval != icmp6off)
2268 in6p->in6p_cksum = optval;
2272 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2275 optval = in6p->in6p_cksum;
2277 error = sooptcopyout(sopt, &optval, sizeof(optval));
2287 error = ENOPROTOOPT;
2295 * Set up IP6 options in pcb for insertion in output packets or
2296 * specifying behavior of outgoing packets.
2299 ip6_pcbopts(pktopt, m, so, sopt)
2300 struct ip6_pktopts **pktopt;
2303 struct sockopt *sopt;
2305 struct ip6_pktopts *opt = *pktopt;
2307 struct thread *td = sopt->sopt_td;
2310 /* turn off any old options. */
2313 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2314 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2315 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2316 printf("ip6_pcbopts: all specified options are cleared.\n");
2318 ip6_clearpktopts(opt, -1);
2320 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2323 if (!m || m->m_len == 0) {
2325 * Only turning off any previous options, regardless of
2326 * whether the opt is just created or given.
2328 free(opt, M_IP6OPT);
2332 /* set options specified by user. */
2333 if (td && !suser(td))
2335 if ((error = ip6_setpktoptions(m, opt, NULL, priv, 1,
2336 so->so_proto->pr_protocol)) != 0) {
2337 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2338 free(opt, M_IP6OPT);
2346 * initialize ip6_pktopts. beware that there are non-zero default values in
2350 init_ip6pktopts(opt)
2351 struct ip6_pktopts *opt;
2354 bzero(opt, sizeof(*opt));
2355 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2356 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2357 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2358 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2362 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2363 int optname, len, priv;
2365 struct ip6_pktopts **pktopt;
2368 struct ip6_pktopts *opt;
2370 if (*pktopt == NULL) {
2371 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2373 init_ip6pktopts(*pktopt);
2374 (*pktopt)->needfree = 1;
2378 return (ip6_setpktoption(optname, buf, len, opt, priv, 1, 0, uproto));
2382 ip6_getpcbopt(pktopt, optname, sopt)
2383 struct ip6_pktopts *pktopt;
2384 struct sockopt *sopt;
2387 void *optdata = NULL;
2389 struct ip6_ext *ip6e;
2391 struct in6_pktinfo null_pktinfo;
2392 int deftclass = 0, on;
2393 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2394 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2398 if (pktopt && pktopt->ip6po_pktinfo)
2399 optdata = (void *)pktopt->ip6po_pktinfo;
2401 /* XXX: we don't have to do this every time... */
2402 bzero(&null_pktinfo, sizeof(null_pktinfo));
2403 optdata = (void *)&null_pktinfo;
2405 optdatalen = sizeof(struct in6_pktinfo);
2408 if (pktopt && pktopt->ip6po_tclass >= 0)
2409 optdata = (void *)&pktopt->ip6po_tclass;
2411 optdata = (void *)&deftclass;
2412 optdatalen = sizeof(int);
2415 if (pktopt && pktopt->ip6po_hbh) {
2416 optdata = (void *)pktopt->ip6po_hbh;
2417 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2418 optdatalen = (ip6e->ip6e_len + 1) << 3;
2422 if (pktopt && pktopt->ip6po_rthdr) {
2423 optdata = (void *)pktopt->ip6po_rthdr;
2424 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2425 optdatalen = (ip6e->ip6e_len + 1) << 3;
2428 case IPV6_RTHDRDSTOPTS:
2429 if (pktopt && pktopt->ip6po_dest1) {
2430 optdata = (void *)pktopt->ip6po_dest1;
2431 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2432 optdatalen = (ip6e->ip6e_len + 1) << 3;
2436 if (pktopt && pktopt->ip6po_dest2) {
2437 optdata = (void *)pktopt->ip6po_dest2;
2438 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2439 optdatalen = (ip6e->ip6e_len + 1) << 3;
2443 if (pktopt && pktopt->ip6po_nexthop) {
2444 optdata = (void *)pktopt->ip6po_nexthop;
2445 optdatalen = pktopt->ip6po_nexthop->sa_len;
2448 case IPV6_USE_MIN_MTU:
2450 optdata = (void *)&pktopt->ip6po_minmtu;
2452 optdata = (void *)&defminmtu;
2453 optdatalen = sizeof(int);
2456 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2460 optdata = (void *)&on;
2461 optdatalen = sizeof(on);
2463 case IPV6_PREFER_TEMPADDR:
2465 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2467 optdata = (void *)&defpreftemp;
2468 optdatalen = sizeof(int);
2470 default: /* should not happen */
2472 panic("ip6_getpcbopt: unexpected option\n");
2474 return (ENOPROTOOPT);
2477 error = sooptcopyout(sopt, optdata, optdatalen);
2483 ip6_clearpktopts(pktopt, optname)
2484 struct ip6_pktopts *pktopt;
2492 needfree = pktopt->needfree;
2494 if (optname == -1 || optname == IPV6_PKTINFO) {
2495 if (needfree && pktopt->ip6po_pktinfo)
2496 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2497 pktopt->ip6po_pktinfo = NULL;
2499 if (optname == -1 || optname == IPV6_HOPLIMIT)
2500 pktopt->ip6po_hlim = -1;
2501 if (optname == -1 || optname == IPV6_TCLASS)
2502 pktopt->ip6po_tclass = -1;
2503 if (optname == -1 || optname == IPV6_NEXTHOP) {
2504 if (pktopt->ip6po_nextroute.ro_rt) {
2505 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2506 pktopt->ip6po_nextroute.ro_rt = NULL;
2508 if (needfree && pktopt->ip6po_nexthop)
2509 free(pktopt->ip6po_nexthop, M_IP6OPT);
2510 pktopt->ip6po_nexthop = NULL;
2512 if (optname == -1 || optname == IPV6_HOPOPTS) {
2513 if (needfree && pktopt->ip6po_hbh)
2514 free(pktopt->ip6po_hbh, M_IP6OPT);
2515 pktopt->ip6po_hbh = NULL;
2517 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2518 if (needfree && pktopt->ip6po_dest1)
2519 free(pktopt->ip6po_dest1, M_IP6OPT);
2520 pktopt->ip6po_dest1 = NULL;
2522 if (optname == -1 || optname == IPV6_RTHDR) {
2523 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2524 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2525 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2526 if (pktopt->ip6po_route.ro_rt) {
2527 RTFREE(pktopt->ip6po_route.ro_rt);
2528 pktopt->ip6po_route.ro_rt = NULL;
2531 if (optname == -1 || optname == IPV6_DSTOPTS) {
2532 if (needfree && pktopt->ip6po_dest2)
2533 free(pktopt->ip6po_dest2, M_IP6OPT);
2534 pktopt->ip6po_dest2 = NULL;
2538 #define PKTOPT_EXTHDRCPY(type) \
2541 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2542 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2543 if (dst->type == NULL && canwait == M_NOWAIT)\
2545 bcopy(src->type, dst->type, hlen);\
2547 } while (/*CONSTCOND*/ 0)
2549 struct ip6_pktopts *
2550 ip6_copypktopts(src, canwait)
2551 struct ip6_pktopts *src;
2554 struct ip6_pktopts *dst;
2557 printf("ip6_clearpktopts: invalid argument\n");
2561 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2562 if (dst == NULL && canwait == M_NOWAIT)
2564 bzero(dst, sizeof(*dst));
2567 dst->ip6po_hlim = src->ip6po_hlim;
2568 dst->ip6po_tclass = src->ip6po_tclass;
2569 dst->ip6po_flags = src->ip6po_flags;
2570 if (src->ip6po_pktinfo) {
2571 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2573 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2575 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2577 if (src->ip6po_nexthop) {
2578 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2580 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
2582 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2583 src->ip6po_nexthop->sa_len);
2585 PKTOPT_EXTHDRCPY(ip6po_hbh);
2586 PKTOPT_EXTHDRCPY(ip6po_dest1);
2587 PKTOPT_EXTHDRCPY(ip6po_dest2);
2588 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2592 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2593 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2594 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2595 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2596 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2597 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2598 free(dst, M_IP6OPT);
2601 #undef PKTOPT_EXTHDRCPY
2604 ip6_freepcbopts(pktopt)
2605 struct ip6_pktopts *pktopt;
2610 ip6_clearpktopts(pktopt, -1);
2612 free(pktopt, M_IP6OPT);
2616 * Set the IP6 multicast options in response to user setsockopt().
2619 ip6_setmoptions(optname, im6op, m)
2621 struct ip6_moptions **im6op;
2625 u_int loop, ifindex;
2626 struct ipv6_mreq *mreq;
2628 struct ip6_moptions *im6o = *im6op;
2629 struct route_in6 ro;
2630 struct sockaddr_in6 *dst;
2631 struct in6_multi_mship *imm;
2632 struct thread *td = curthread;
2636 * No multicast option buffer attached to the pcb;
2637 * allocate one and initialize to default values.
2639 im6o = (struct ip6_moptions *)
2640 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2645 im6o->im6o_multicast_ifp = NULL;
2646 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2647 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2648 LIST_INIT(&im6o->im6o_memberships);
2653 case IPV6_MULTICAST_IF:
2655 * Select the interface for outgoing multicast packets.
2657 if (m == NULL || m->m_len != sizeof(u_int)) {
2661 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2662 if (ifindex < 0 || if_index < ifindex) {
2663 error = ENXIO; /* XXX EINVAL? */
2666 ifp = ifnet_byindex(ifindex);
2667 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2668 error = EADDRNOTAVAIL;
2671 im6o->im6o_multicast_ifp = ifp;
2674 case IPV6_MULTICAST_HOPS:
2677 * Set the IP6 hoplimit for outgoing multicast packets.
2680 if (m == NULL || m->m_len != sizeof(int)) {
2684 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2685 if (optval < -1 || optval >= 256)
2687 else if (optval == -1)
2688 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2690 im6o->im6o_multicast_hlim = optval;
2694 case IPV6_MULTICAST_LOOP:
2696 * Set the loopback flag for outgoing multicast packets.
2697 * Must be zero or one.
2699 if (m == NULL || m->m_len != sizeof(u_int)) {
2703 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2708 im6o->im6o_multicast_loop = loop;
2711 case IPV6_JOIN_GROUP:
2713 * Add a multicast group membership.
2714 * Group must be a valid IP6 multicast address.
2716 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2720 mreq = mtod(m, struct ipv6_mreq *);
2721 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2723 * We use the unspecified address to specify to accept
2724 * all multicast addresses. Only super user is allowed
2731 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2737 * If the interface is specified, validate it.
2739 if (mreq->ipv6mr_interface < 0 ||
2740 if_index < mreq->ipv6mr_interface) {
2741 error = ENXIO; /* XXX EINVAL? */
2745 * If no interface was explicitly specified, choose an
2746 * appropriate one according to the given multicast address.
2748 if (mreq->ipv6mr_interface == 0) {
2750 * If the multicast address is in node-local scope,
2751 * the interface should be a loopback interface.
2752 * Otherwise, look up the routing table for the
2753 * address, and choose the outgoing interface.
2754 * XXX: is it a good approach?
2756 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) {
2760 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2761 bzero(dst, sizeof(*dst));
2762 dst->sin6_len = sizeof(struct sockaddr_in6);
2763 dst->sin6_family = AF_INET6;
2764 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2765 rtalloc((struct route *)&ro);
2766 if (ro.ro_rt == NULL) {
2767 error = EADDRNOTAVAIL;
2770 ifp = ro.ro_rt->rt_ifp;
2774 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2777 * See if we found an interface, and confirm that it
2778 * supports multicast
2780 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2781 error = EADDRNOTAVAIL;
2785 * Put interface index into the multicast address,
2786 * if the address has link-local scope.
2788 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2789 mreq->ipv6mr_multiaddr.s6_addr16[1] =
2790 htons(ifp->if_index);
2793 * See if the membership already exists.
2795 for (imm = im6o->im6o_memberships.lh_first;
2796 imm != NULL; imm = imm->i6mm_chain.le_next)
2797 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2798 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2799 &mreq->ipv6mr_multiaddr))
2806 * Everything looks good; add a new record to the multicast
2807 * address list for the given interface.
2809 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2814 if ((imm->i6mm_maddr =
2815 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2816 free(imm, M_IPMADDR);
2819 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2822 case IPV6_LEAVE_GROUP:
2824 * Drop a multicast group membership.
2825 * Group must be a valid IP6 multicast address.
2827 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2831 mreq = mtod(m, struct ipv6_mreq *);
2832 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2837 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2842 * If an interface address was specified, get a pointer
2843 * to its ifnet structure.
2845 if (mreq->ipv6mr_interface < 0
2846 || if_index < mreq->ipv6mr_interface) {
2847 error = ENXIO; /* XXX EINVAL? */
2850 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2852 * Put interface index into the multicast address,
2853 * if the address has link-local scope.
2855 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2856 mreq->ipv6mr_multiaddr.s6_addr16[1]
2857 = htons(mreq->ipv6mr_interface);
2861 * Find the membership in the membership list.
2863 for (imm = im6o->im6o_memberships.lh_first;
2864 imm != NULL; imm = imm->i6mm_chain.le_next) {
2865 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2866 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2867 &mreq->ipv6mr_multiaddr))
2871 /* Unable to resolve interface */
2872 error = EADDRNOTAVAIL;
2876 * Give up the multicast address record to which the
2877 * membership points.
2879 LIST_REMOVE(imm, i6mm_chain);
2880 in6_delmulti(imm->i6mm_maddr);
2881 free(imm, M_IPMADDR);
2890 * If all options have default values, no need to keep the mbuf.
2892 if (im6o->im6o_multicast_ifp == NULL &&
2893 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2894 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2895 im6o->im6o_memberships.lh_first == NULL) {
2896 free(*im6op, M_IPMOPTS);
2904 * Return the IP6 multicast options in response to user getsockopt().
2907 ip6_getmoptions(optname, im6o, mp)
2909 struct ip6_moptions *im6o;
2912 u_int *hlim, *loop, *ifindex;
2914 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2918 case IPV6_MULTICAST_IF:
2919 ifindex = mtod(*mp, u_int *);
2920 (*mp)->m_len = sizeof(u_int);
2921 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2924 *ifindex = im6o->im6o_multicast_ifp->if_index;
2927 case IPV6_MULTICAST_HOPS:
2928 hlim = mtod(*mp, u_int *);
2929 (*mp)->m_len = sizeof(u_int);
2931 *hlim = ip6_defmcasthlim;
2933 *hlim = im6o->im6o_multicast_hlim;
2936 case IPV6_MULTICAST_LOOP:
2937 loop = mtod(*mp, u_int *);
2938 (*mp)->m_len = sizeof(u_int);
2940 *loop = ip6_defmcasthlim;
2942 *loop = im6o->im6o_multicast_loop;
2946 return (EOPNOTSUPP);
2951 * Discard the IP6 multicast options.
2954 ip6_freemoptions(im6o)
2955 struct ip6_moptions *im6o;
2957 struct in6_multi_mship *imm;
2962 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2963 LIST_REMOVE(imm, i6mm_chain);
2964 if (imm->i6mm_maddr)
2965 in6_delmulti(imm->i6mm_maddr);
2966 free(imm, M_IPMADDR);
2968 free(im6o, M_IPMOPTS);
2972 * Set IPv6 outgoing packet options based on advanced API.
2975 ip6_setpktoptions(control, opt, stickyopt, priv, needcopy, uproto)
2976 struct mbuf *control;
2977 struct ip6_pktopts *opt, *stickyopt;
2978 int priv, needcopy, uproto;
2980 struct cmsghdr *cm = 0;
2982 if (control == 0 || opt == 0)
2987 * If stickyopt is provided, make a local copy of the options
2988 * for this particular packet, then override them by ancillary
2990 * XXX: need to gain a reference for the cached route of the
2991 * next hop in case of the overriding.
2994 if (opt->ip6po_nextroute.ro_rt) {
2995 RT_LOCK(opt->ip6po_nextroute.ro_rt);
2996 RT_ADDREF(opt->ip6po_nextroute.ro_rt);
2997 RT_UNLOCK(opt->ip6po_nextroute.ro_rt);
3000 init_ip6pktopts(opt);
3001 opt->needfree = needcopy;
3004 * XXX: Currently, we assume all the optional information is stored
3007 if (control->m_next)
3010 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
3011 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
3014 if (control->m_len < CMSG_LEN(0))
3017 cm = mtod(control, struct cmsghdr *);
3018 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
3020 if (cm->cmsg_level != IPPROTO_IPV6)
3023 error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm),
3024 cm->cmsg_len - CMSG_LEN(0), opt, priv, needcopy, 1, uproto);
3033 * Set a particular packet option, as a sticky option or an ancillary data
3034 * item. "len" can be 0 only when it's a sticky option.
3035 * We have 4 cases of combination of "sticky" and "cmsg":
3036 * "sticky=0, cmsg=0": impossible
3037 * "sticky=0, cmsg=1": RFC2292 or rfc2292bis ancillary data
3038 * "sticky=1, cmsg=0": rfc2292bis socket option
3039 * "sticky=1, cmsg=1": RFC2292 socket option
3042 ip6_setpktoption(optname, buf, len, opt, priv, sticky, cmsg, uproto)
3043 int optname, len, priv, sticky, cmsg, uproto;
3045 struct ip6_pktopts *opt;
3047 int minmtupolicy, preftemp;
3049 if (!sticky && !cmsg) {
3051 printf("ip6_setpktoption: impossible case\n");
3057 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3058 * not be specified in the context of rfc2292bis. Conversely,
3059 * rfc2292bis types should not be specified in the context of RFC2292.
3063 case IPV6_2292PKTINFO:
3064 case IPV6_2292HOPLIMIT:
3065 case IPV6_2292NEXTHOP:
3066 case IPV6_2292HOPOPTS:
3067 case IPV6_2292DSTOPTS:
3068 case IPV6_2292RTHDR:
3069 case IPV6_2292PKTOPTIONS:
3070 return (ENOPROTOOPT);
3073 if (sticky && cmsg) {
3080 case IPV6_RTHDRDSTOPTS:
3082 case IPV6_USE_MIN_MTU:
3085 case IPV6_PREFER_TEMPADDR: /* XXX: not an rfc2292bis option */
3086 return (ENOPROTOOPT);
3091 case IPV6_2292PKTINFO:
3094 struct ifnet *ifp = NULL;
3095 struct in6_pktinfo *pktinfo;
3097 if (len != sizeof(struct in6_pktinfo))
3100 pktinfo = (struct in6_pktinfo *)buf;
3103 * An application can clear any sticky IPV6_PKTINFO option by
3104 * doing a "regular" setsockopt with ipi6_addr being
3105 * in6addr_any and ipi6_ifindex being zero.
3106 * [RFC 3542, Section 6]
3108 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3109 pktinfo->ipi6_ifindex == 0 &&
3110 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3111 ip6_clearpktopts(opt, optname);
3115 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3116 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3120 /* validate the interface index if specified. */
3121 if (pktinfo->ipi6_ifindex > if_index ||
3122 pktinfo->ipi6_ifindex < 0) {
3125 if (pktinfo->ipi6_ifindex) {
3126 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3132 * We store the address anyway, and let in6_selectsrc()
3133 * validate the specified address. This is because ipi6_addr
3134 * may not have enough information about its scope zone, and
3135 * we may need additional information (such as outgoing
3136 * interface or the scope zone of a destination address) to
3137 * disambiguate the scope.
3138 * XXX: the delay of the validation may confuse the
3139 * application when it is used as a sticky option.
3142 if (opt->ip6po_pktinfo == NULL) {
3143 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3144 M_IP6OPT, M_WAITOK);
3146 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3148 opt->ip6po_pktinfo = pktinfo;
3152 case IPV6_2292HOPLIMIT:
3158 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3159 * to simplify the ordering among hoplimit options.
3161 if (optname == IPV6_HOPLIMIT && sticky)
3162 return (ENOPROTOOPT);
3164 if (len != sizeof(int))
3167 if (*hlimp < -1 || *hlimp > 255)
3170 opt->ip6po_hlim = *hlimp;
3178 if (len != sizeof(int))
3180 tclass = *(int *)buf;
3181 if (tclass < -1 || tclass > 255)
3184 opt->ip6po_tclass = tclass;
3188 case IPV6_2292NEXTHOP:
3193 if (len == 0) { /* just remove the option */
3194 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3198 /* check if cmsg_len is large enough for sa_len */
3199 if (len < sizeof(struct sockaddr) || len < *buf)
3202 switch (((struct sockaddr *)buf)->sa_family) {
3205 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3210 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3213 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3214 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3218 if ((error = scope6_check_id(sa6, ip6_use_defzone))
3223 sa6->sin6_scope_id = 0; /* XXX */
3226 case AF_LINK: /* should eventually be supported */
3228 return (EAFNOSUPPORT);
3231 /* turn off the previous option, then set the new option. */
3232 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3234 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_WAITOK);
3235 bcopy(buf, opt->ip6po_nexthop, *buf);
3237 opt->ip6po_nexthop = (struct sockaddr *)buf;
3240 case IPV6_2292HOPOPTS:
3243 struct ip6_hbh *hbh;
3247 * XXX: We don't allow a non-privileged user to set ANY HbH
3248 * options, since per-option restriction has too much
3255 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3256 break; /* just remove the option */
3259 /* message length validation */
3260 if (len < sizeof(struct ip6_hbh))
3262 hbh = (struct ip6_hbh *)buf;
3263 hbhlen = (hbh->ip6h_len + 1) << 3;
3267 /* turn off the previous option, then set the new option. */
3268 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3270 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_WAITOK);
3271 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3273 opt->ip6po_hbh = hbh;
3278 case IPV6_2292DSTOPTS:
3280 case IPV6_RTHDRDSTOPTS:
3282 struct ip6_dest *dest, **newdest = NULL;
3285 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3289 ip6_clearpktopts(opt, optname);
3290 break; /* just remove the option */
3293 /* message length validation */
3294 if (len < sizeof(struct ip6_dest))
3296 dest = (struct ip6_dest *)buf;
3297 destlen = (dest->ip6d_len + 1) << 3;
3302 * Determine the position that the destination options header
3303 * should be inserted; before or after the routing header.
3306 case IPV6_2292DSTOPTS:
3308 * The old advacned API is ambiguous on this point.
3309 * Our approach is to determine the position based
3310 * according to the existence of a routing header.
3311 * Note, however, that this depends on the order of the
3312 * extension headers in the ancillary data; the 1st
3313 * part of the destination options header must appear
3314 * before the routing header in the ancillary data,
3316 * RFC2292bis solved the ambiguity by introducing
3317 * separate ancillary data or option types.
3319 if (opt->ip6po_rthdr == NULL)
3320 newdest = &opt->ip6po_dest1;
3322 newdest = &opt->ip6po_dest2;
3324 case IPV6_RTHDRDSTOPTS:
3325 newdest = &opt->ip6po_dest1;
3328 newdest = &opt->ip6po_dest2;
3332 /* turn off the previous option, then set the new option. */
3333 ip6_clearpktopts(opt, optname);
3335 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
3336 bcopy(dest, *newdest, destlen);
3343 case IPV6_2292RTHDR:
3346 struct ip6_rthdr *rth;
3350 ip6_clearpktopts(opt, IPV6_RTHDR);
3351 break; /* just remove the option */
3354 /* message length validation */
3355 if (len < sizeof(struct ip6_rthdr))
3357 rth = (struct ip6_rthdr *)buf;
3358 rthlen = (rth->ip6r_len + 1) << 3;
3362 switch (rth->ip6r_type) {
3363 case IPV6_RTHDR_TYPE_0:
3364 if (rth->ip6r_len == 0) /* must contain one addr */
3366 if (rth->ip6r_len % 2) /* length must be even */
3368 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3372 return (EINVAL); /* not supported */
3375 /* turn off the previous option */
3376 ip6_clearpktopts(opt, IPV6_RTHDR);
3378 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_WAITOK);
3379 bcopy(rth, opt->ip6po_rthdr, rthlen);
3381 opt->ip6po_rthdr = rth;
3386 case IPV6_USE_MIN_MTU:
3387 if (len != sizeof(int))
3389 minmtupolicy = *(int *)buf;
3390 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3391 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3392 minmtupolicy != IP6PO_MINMTU_ALL) {
3395 opt->ip6po_minmtu = minmtupolicy;
3399 if (len != sizeof(int))
3402 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3404 * we ignore this option for TCP sockets.
3405 * (rfc2292bis leaves this case unspecified.)
3407 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3409 opt->ip6po_flags |= IP6PO_DONTFRAG;
3412 case IPV6_PREFER_TEMPADDR:
3413 if (len != sizeof(int))
3415 preftemp = *(int *)buf;
3416 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3417 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3418 preftemp != IP6PO_TEMPADDR_PREFER) {
3421 opt->ip6po_prefer_tempaddr = preftemp;
3425 return (ENOPROTOOPT);
3426 } /* end of switch */
3432 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3433 * packet to the input queue of a specified interface. Note that this
3434 * calls the output routine of the loopback "driver", but with an interface
3435 * pointer that might NOT be &loif -- easier than replicating that code here.
3438 ip6_mloopback(ifp, m, dst)
3441 struct sockaddr_in6 *dst;
3444 struct ip6_hdr *ip6;
3446 copym = m_copy(m, 0, M_COPYALL);
3451 * Make sure to deep-copy IPv6 header portion in case the data
3452 * is in an mbuf cluster, so that we can safely override the IPv6
3453 * header portion later.
3455 if ((copym->m_flags & M_EXT) != 0 ||
3456 copym->m_len < sizeof(struct ip6_hdr)) {
3457 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3463 if (copym->m_len < sizeof(*ip6)) {
3469 ip6 = mtod(copym, struct ip6_hdr *);
3471 * clear embedded scope identifiers if necessary.
3472 * in6_clearscope will touch the addresses only when necessary.
3474 in6_clearscope(&ip6->ip6_src);
3475 in6_clearscope(&ip6->ip6_dst);
3477 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3481 * Chop IPv6 header off from the payload.
3484 ip6_splithdr(m, exthdrs)
3486 struct ip6_exthdrs *exthdrs;
3489 struct ip6_hdr *ip6;
3491 ip6 = mtod(m, struct ip6_hdr *);
3492 if (m->m_len > sizeof(*ip6)) {
3493 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3498 M_MOVE_PKTHDR(mh, m);
3499 MH_ALIGN(mh, sizeof(*ip6));
3500 m->m_len -= sizeof(*ip6);
3501 m->m_data += sizeof(*ip6);
3504 m->m_len = sizeof(*ip6);
3505 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3507 exthdrs->ip6e_ip6 = m;
3512 * Compute IPv6 extension header length.
3516 struct in6pcb *in6p;
3520 if (!in6p->in6p_outputopts)
3525 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3527 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3528 if (in6p->in6p_outputopts->ip6po_rthdr)
3529 /* dest1 is valid with rthdr only */
3530 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3531 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3532 len += elen(in6p->in6p_outputopts->ip6po_dest2);