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/netisr.h>
82 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet/icmp6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet6/nd6.h>
96 #include <netinet6/ipsec.h>
98 #include <netinet6/ipsec6.h>
100 #include <netkey/key.h>
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #include <netipsec/key.h>
107 #endif /* FAST_IPSEC */
109 #include <netinet6/ip6_fw.h>
111 #include <net/net_osdep.h>
113 #include <netinet6/ip6protosw.h>
115 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
118 struct mbuf *ip6e_ip6;
119 struct mbuf *ip6e_hbh;
120 struct mbuf *ip6e_dest1;
121 struct mbuf *ip6e_rthdr;
122 struct mbuf *ip6e_dest2;
125 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
127 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
128 struct socket *, struct sockopt *));
129 static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *));
130 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
133 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
134 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
135 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
136 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
137 struct ip6_frag **));
138 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
139 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
140 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
141 struct ifnet *, struct in6_addr *, u_long *, int *));
142 static int copypktopts __P((struct ip6_pktopts *, struct ip6_pktopts *, int));
146 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
147 * header (with pri, len, nxt, hlim, src, dst).
148 * This function may modify ver and hlim only.
149 * The mbuf chain containing the packet will be freed.
150 * The mbuf opt, if present, will not be freed.
152 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
153 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
154 * which is rt_rmx.rmx_mtu.
157 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
159 struct ip6_pktopts *opt;
160 struct route_in6 *ro;
162 struct ip6_moptions *im6o;
163 struct ifnet **ifpp; /* XXX: just for statistics */
166 struct ip6_hdr *ip6, *mhip6;
167 struct ifnet *ifp, *origifp;
169 int hlen, tlen, len, off;
170 struct route_in6 ip6route;
171 struct sockaddr_in6 *dst;
172 struct in6_addr odst;
174 struct in6_ifaddr *ia = NULL;
176 int alwaysfrag, dontfrag;
177 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
178 struct ip6_exthdrs exthdrs;
179 struct in6_addr finaldst;
180 struct route_in6 *ro_pmtu = NULL;
183 #if defined(IPSEC) || defined(FAST_IPSEC)
184 int needipsectun = 0;
185 struct secpolicy *sp = NULL;
186 #endif /*IPSEC || FAST_IPSEC*/
188 ip6 = mtod(m, struct ip6_hdr *);
189 finaldst = ip6->ip6_dst;
191 #define MAKE_EXTHDR(hp, mp) \
194 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
195 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
196 ((eh)->ip6e_len + 1) << 3); \
200 } while (/*CONSTCOND*/ 0)
202 bzero(&exthdrs, sizeof(exthdrs));
205 /* Hop-by-Hop options header */
206 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
207 /* Destination options header(1st part) */
208 if (opt->ip6po_rthdr) {
210 * Destination options header(1st part)
211 * This only makes sence with a routing header.
212 * See Section 9.2 of RFC 3542.
213 * Disabling this part just for MIP6 convenience is
214 * a bad idea. We need to think carefully about a
215 * way to make the advanced API coexist with MIP6
216 * options, which might automatically be inserted in
219 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
222 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
223 /* Destination options header(2nd part) */
224 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
228 /* get a security policy for this packet */
230 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
232 sp = ipsec6_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
235 ipsec6stat.out_inval++;
242 switch (sp->policy) {
243 case IPSEC_POLICY_DISCARD:
245 * This packet is just discarded.
247 ipsec6stat.out_polvio++;
250 case IPSEC_POLICY_BYPASS:
251 case IPSEC_POLICY_NONE:
252 /* no need to do IPsec. */
256 case IPSEC_POLICY_IPSEC:
257 if (sp->req == NULL) {
258 /* acquire a policy */
259 error = key_spdacquire(sp);
265 case IPSEC_POLICY_ENTRUST:
267 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
271 /* get a security policy for this packet */
273 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
275 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
278 newipsecstat.ips_out_inval++;
285 switch (sp->policy) {
286 case IPSEC_POLICY_DISCARD:
288 * This packet is just discarded.
290 newipsecstat.ips_out_polvio++;
293 case IPSEC_POLICY_BYPASS:
294 case IPSEC_POLICY_NONE:
295 /* no need to do IPsec. */
299 case IPSEC_POLICY_IPSEC:
300 if (sp->req == NULL) {
301 /* acquire a policy */
302 error = key_spdacquire(sp);
308 case IPSEC_POLICY_ENTRUST:
310 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
312 #endif /* FAST_IPSEC */
315 * Calculate the total length of the extension header chain.
316 * Keep the length of the unfragmentable part for fragmentation.
319 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
320 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
321 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
322 unfragpartlen = optlen + sizeof(struct ip6_hdr);
323 /* NOTE: we don't add AH/ESP length here. do that later. */
324 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
327 * If we need IPsec, or there is at least one extension header,
328 * separate IP6 header from the payload.
330 if ((needipsec || optlen) && !hdrsplit) {
331 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
335 m = exthdrs.ip6e_ip6;
340 ip6 = mtod(m, struct ip6_hdr *);
342 /* adjust mbuf packet header length */
343 m->m_pkthdr.len += optlen;
344 plen = m->m_pkthdr.len - sizeof(*ip6);
346 /* If this is a jumbo payload, insert a jumbo payload option. */
347 if (plen > IPV6_MAXPACKET) {
349 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
353 m = exthdrs.ip6e_ip6;
357 ip6 = mtod(m, struct ip6_hdr *);
358 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
362 ip6->ip6_plen = htons(plen);
365 * Concatenate headers and fill in next header fields.
366 * Here we have, on "m"
368 * and we insert headers accordingly. Finally, we should be getting:
369 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
371 * during the header composing process, "m" points to IPv6 header.
372 * "mprev" points to an extension header prior to esp.
375 u_char *nexthdrp = &ip6->ip6_nxt;
376 struct mbuf *mprev = m;
379 * we treat dest2 specially. this makes IPsec processing
380 * much easier. the goal here is to make mprev point the
381 * mbuf prior to dest2.
383 * result: IPv6 dest2 payload
384 * m and mprev will point to IPv6 header.
386 if (exthdrs.ip6e_dest2) {
388 panic("assumption failed: hdr not split");
389 exthdrs.ip6e_dest2->m_next = m->m_next;
390 m->m_next = exthdrs.ip6e_dest2;
391 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
392 ip6->ip6_nxt = IPPROTO_DSTOPTS;
395 #define MAKE_CHAIN(m, mp, p, i)\
399 panic("assumption failed: hdr not split"); \
400 *mtod((m), u_char *) = *(p);\
402 p = mtod((m), u_char *);\
403 (m)->m_next = (mp)->m_next;\
407 } while (/*CONSTCOND*/ 0)
409 * result: IPv6 hbh dest1 rthdr dest2 payload
410 * m will point to IPv6 header. mprev will point to the
411 * extension header prior to dest2 (rthdr in the above case).
413 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
414 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
416 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
419 #if defined(IPSEC) || defined(FAST_IPSEC)
424 * pointers after IPsec headers are not valid any more.
425 * other pointers need a great care too.
426 * (IPsec routines should not mangle mbufs prior to AH/ESP)
428 exthdrs.ip6e_dest2 = NULL;
431 struct ip6_rthdr *rh = NULL;
433 struct ipsec_output_state state;
435 if (exthdrs.ip6e_rthdr) {
436 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
437 segleft_org = rh->ip6r_segleft;
438 rh->ip6r_segleft = 0;
441 bzero(&state, sizeof(state));
443 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
447 /* mbuf is already reclaimed in ipsec6_output_trans. */
457 printf("ip6_output (ipsec): error code %d\n", error);
460 /* don't show these error codes to the user */
466 if (exthdrs.ip6e_rthdr) {
467 /* ah6_output doesn't modify mbuf chain */
468 rh->ip6r_segleft = segleft_org;
476 * If there is a routing header, replace the destination address field
477 * with the first hop of the routing header.
479 if (exthdrs.ip6e_rthdr) {
480 struct ip6_rthdr *rh =
481 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
482 struct ip6_rthdr *));
483 struct ip6_rthdr0 *rh0;
484 struct in6_addr *addrs;
486 switch (rh->ip6r_type) {
487 case IPV6_RTHDR_TYPE_0:
488 rh0 = (struct ip6_rthdr0 *)rh;
489 addrs = (struct in6_addr *)(rh0 + 1);
491 ip6->ip6_dst = *addrs;
492 bcopy((caddr_t)(addrs + 1), (caddr_t)addrs,
493 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
495 *(addrs + rh0->ip6r0_segleft - 1) = finaldst;
497 default: /* is it possible? */
503 /* Source address validation */
504 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
505 (flags & IPV6_DADOUTPUT) == 0) {
507 ip6stat.ip6s_badscope++;
510 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
512 ip6stat.ip6s_badscope++;
516 ip6stat.ip6s_localout++;
523 bzero((caddr_t)ro, sizeof(*ro));
526 if (opt && opt->ip6po_rthdr)
527 ro = &opt->ip6po_route;
528 dst = (struct sockaddr_in6 *)&ro->ro_dst;
532 * If there is a cached route,
533 * check that it is to the same destination
534 * and is still up. If not, free it and try again.
536 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
537 dst->sin6_family != AF_INET6 ||
538 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
540 ro->ro_rt = (struct rtentry *)0;
542 if (ro->ro_rt == 0) {
543 bzero(dst, sizeof(*dst));
544 dst->sin6_family = AF_INET6;
545 dst->sin6_len = sizeof(struct sockaddr_in6);
546 dst->sin6_addr = ip6->ip6_dst;
550 * if specified, try to fill in the traffic class field.
551 * do not override if a non-zero value is already set.
552 * we check the diffserv field and the ecn field separately.
554 if (opt && opt->ip6po_tclass >= 0) {
557 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
559 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
562 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
565 /* fill in or override the hop limit field, if necessary. */
566 if (opt && opt->ip6po_hlim != -1)
567 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
568 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
570 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
572 ip6->ip6_hlim = ip6_defmcasthlim;
575 #if defined(IPSEC) || defined(FAST_IPSEC)
576 if (needipsec && needipsectun) {
577 struct ipsec_output_state state;
580 * All the extension headers will become inaccessible
581 * (since they can be encrypted).
582 * Don't panic, we need no more updates to extension headers
583 * on inner IPv6 packet (since they are now encapsulated).
585 * IPv6 [ESP|AH] IPv6 [extension headers] payload
587 bzero(&exthdrs, sizeof(exthdrs));
588 exthdrs.ip6e_ip6 = m;
590 bzero(&state, sizeof(state));
592 state.ro = (struct route *)ro;
593 state.dst = (struct sockaddr *)dst;
595 error = ipsec6_output_tunnel(&state, sp, flags);
598 ro = (struct route_in6 *)state.ro;
599 dst = (struct sockaddr_in6 *)state.dst;
601 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
612 printf("ip6_output (ipsec): error code %d\n", error);
615 /* don't show these error codes to the user */
622 exthdrs.ip6e_ip6 = m;
626 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
629 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
630 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
632 * interface selection comes here
633 * if an interface is specified from an upper layer,
636 if (ro->ro_rt == 0) {
638 * non-bsdi always clone routes, if parent is
641 rtalloc((struct route *)ro);
643 if (ro->ro_rt == 0) {
644 ip6stat.ip6s_noroute++;
645 error = EHOSTUNREACH;
646 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
649 /* XXX rt not locked */
650 ia = ifatoia6(ro->ro_rt->rt_ifa);
651 ifp = ro->ro_rt->rt_ifp;
652 ro->ro_rt->rt_rmx.rmx_pksent++;
653 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
654 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
655 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
657 in6_ifstat_inc(ifp, ifs6_out_request);
660 * Check if the outgoing interface conflicts with
661 * the interface specified by ifi6_ifindex (if specified).
662 * Note that loopback interface is always okay.
663 * (this may happen when we are sending a packet to one of
664 * our own addresses.)
666 if (opt && opt->ip6po_pktinfo
667 && opt->ip6po_pktinfo->ipi6_ifindex) {
668 if (!(ifp->if_flags & IFF_LOOPBACK)
669 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
670 ip6stat.ip6s_noroute++;
671 in6_ifstat_inc(ifp, ifs6_out_discard);
672 error = EHOSTUNREACH;
677 if (opt && opt->ip6po_hlim != -1)
678 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
681 struct in6_multi *in6m;
683 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
686 * See if the caller provided any multicast options
690 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
691 if (im6o->im6o_multicast_ifp != NULL)
692 ifp = im6o->im6o_multicast_ifp;
694 ip6->ip6_hlim = ip6_defmcasthlim;
697 * See if the caller provided the outgoing interface
698 * as an ancillary data.
699 * Boundary check for ifindex is assumed to be already done.
701 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
702 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex);
705 * If the destination is a node-local scope multicast,
706 * the packet should be loop-backed only.
708 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
710 * If the outgoing interface is already specified,
711 * it should be a loopback interface.
713 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
714 ip6stat.ip6s_badscope++;
715 error = ENETUNREACH; /* XXX: better error? */
716 /* XXX correct ifp? */
717 in6_ifstat_inc(ifp, ifs6_out_discard);
724 if (opt && opt->ip6po_hlim != -1)
725 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
728 * If caller did not provide an interface lookup a
729 * default in the routing table. This is either a
730 * default for the speicfied group (i.e. a host
731 * route), or a multicast default (a route for the
736 ro->ro_rt = rtalloc1((struct sockaddr *)
737 &ro->ro_dst, 0, 0UL);
740 if (ro->ro_rt == 0) {
741 ip6stat.ip6s_noroute++;
742 error = EHOSTUNREACH;
743 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
746 ia = ifatoia6(ro->ro_rt->rt_ifa);
747 ifp = ro->ro_rt->rt_ifp;
748 ro->ro_rt->rt_rmx.rmx_pksent++;
749 RT_UNLOCK(ro->ro_rt);
752 if ((flags & IPV6_FORWARDING) == 0)
753 in6_ifstat_inc(ifp, ifs6_out_request);
754 in6_ifstat_inc(ifp, ifs6_out_mcast);
757 * Confirm that the outgoing interface supports multicast.
759 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
760 ip6stat.ip6s_noroute++;
761 in6_ifstat_inc(ifp, ifs6_out_discard);
765 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
767 (im6o == NULL || im6o->im6o_multicast_loop)) {
769 * If we belong to the destination multicast group
770 * on the outgoing interface, and the caller did not
771 * forbid loopback, loop back a copy.
773 ip6_mloopback(ifp, m, dst);
776 * If we are acting as a multicast router, perform
777 * multicast forwarding as if the packet had just
778 * arrived on the interface to which we are about
779 * to send. The multicast forwarding function
780 * recursively calls this function, using the
781 * IPV6_FORWARDING flag to prevent infinite recursion.
783 * Multicasts that are looped back by ip6_mloopback(),
784 * above, will be forwarded by the ip6_input() routine,
787 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
788 if (ip6_mforward(ip6, ifp, m) != 0) {
795 * Multicasts with a hoplimit of zero may be looped back,
796 * above, but must not be transmitted on a network.
797 * Also, multicasts addressed to the loopback interface
798 * are not sent -- the above call to ip6_mloopback() will
799 * loop back a copy if this host actually belongs to the
800 * destination group on the loopback interface.
802 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
803 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
810 * Fill the outgoing inteface to tell the upper layer
811 * to increment per-interface statistics.
816 /* Determine path MTU. */
817 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
822 * The caller of this function may specify to use the minimum MTU
824 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
825 * setting. The logic is a bit complicated; by default, unicast
826 * packets will follow path MTU while multicast packets will be sent at
827 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
828 * including unicast ones will be sent at the minimum MTU. Multicast
829 * packets will always be sent at the minimum MTU unless
830 * IP6PO_MINMTU_DISABLE is explicitly specified.
831 * See RFC 3542 for more details.
833 if (mtu > IPV6_MMTU) {
834 if ((flags & IPV6_MINMTU))
836 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
838 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
840 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
845 /* Fake scoped addresses */
846 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
848 * If source or destination address is a scoped address, and
849 * the packet is going to be sent to a loopback interface,
850 * we should keep the original interface.
854 * XXX: this is a very experimental and temporary solution.
855 * We eventually have sockaddr_in6 and use the sin6_scope_id
856 * field of the structure here.
857 * We rely on the consistency between two scope zone ids
858 * of source and destination, which should already be assured.
859 * Larger scopes than link will be supported in the future.
862 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
863 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1]));
864 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
865 origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1]));
867 * XXX: origifp can be NULL even in those two cases above.
868 * For example, if we remove the (only) link-local address
869 * from the loopback interface, and try to send a link-local
870 * address without link-id information. Then the source
871 * address is ::1, and the destination address is the
872 * link-local address with its s6_addr16[1] being zero.
873 * What is worse, if the packet goes to the loopback interface
874 * by a default rejected route, the null pointer would be
875 * passed to looutput, and the kernel would hang.
876 * The following last resort would prevent such disaster.
884 * clear embedded scope identifiers if necessary.
885 * in6_clearscope will touch the addresses only when necessary.
887 in6_clearscope(&ip6->ip6_src);
888 in6_clearscope(&ip6->ip6_dst);
891 * Check with the firewall...
893 if (ip6_fw_enable && ip6_fw_chk_ptr) {
895 m->m_pkthdr.rcvif = NULL; /* XXX */
896 /* If ipfw says divert, we have to just drop packet */
897 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
908 * If the outgoing packet contains a hop-by-hop options header,
909 * it must be examined and processed even by the source node.
910 * (RFC 2460, section 4.)
912 if (exthdrs.ip6e_hbh) {
913 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
914 u_int32_t dummy; /* XXX unused */
915 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
918 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
919 panic("ip6e_hbh is not continuous");
922 * XXX: if we have to send an ICMPv6 error to the sender,
923 * we need the M_LOOP flag since icmp6_error() expects
924 * the IPv6 and the hop-by-hop options header are
925 * continuous unless the flag is set.
927 m->m_flags |= M_LOOP;
928 m->m_pkthdr.rcvif = ifp;
929 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
930 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
931 &dummy, &plen) < 0) {
932 /* m was already freed at this point */
933 error = EINVAL;/* better error? */
936 m->m_flags &= ~M_LOOP; /* XXX */
937 m->m_pkthdr.rcvif = NULL;
940 /* Jump over all PFIL processing if hooks are not active. */
941 if (inet6_pfil_hook.ph_busy_count == -1)
945 /* Run through list of hooks for output packets. */
946 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
947 if (error != 0 || m == NULL)
949 ip6 = mtod(m, struct ip6_hdr *);
951 /* See if destination IP address was changed by packet filter. */
952 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
953 m->m_flags |= M_SKIP_FIREWALL;
954 /* If destination is now ourself drop to ip6_input(). */
955 if (in6_localaddr(&ip6->ip6_dst)) {
956 if (m->m_pkthdr.rcvif == NULL)
957 m->m_pkthdr.rcvif = loif;
958 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
959 m->m_pkthdr.csum_flags |=
960 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
961 m->m_pkthdr.csum_data = 0xffff;
963 m->m_pkthdr.csum_flags |=
964 CSUM_IP_CHECKED | CSUM_IP_VALID;
965 error = netisr_queue(NETISR_IPV6, m);
968 goto again; /* Redo the routing table lookup. */
971 /* XXX: IPFIREWALL_FORWARD */
975 * Send the packet to the outgoing interface.
976 * If necessary, do IPv6 fragmentation before sending.
978 * the logic here is rather complex:
979 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
980 * 1-a: send as is if tlen <= path mtu
981 * 1-b: fragment if tlen > path mtu
983 * 2: if user asks us not to fragment (dontfrag == 1)
984 * 2-a: send as is if tlen <= interface mtu
985 * 2-b: error if tlen > interface mtu
987 * 3: if we always need to attach fragment header (alwaysfrag == 1)
990 * 4: if dontfrag == 1 && alwaysfrag == 1
991 * error, as we cannot handle this conflicting request
993 tlen = m->m_pkthdr.len;
995 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
999 if (dontfrag && alwaysfrag) { /* case 4 */
1000 /* conflicting request - can't transmit */
1004 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
1006 * Even if the DONTFRAG option is specified, we cannot send the
1007 * packet when the data length is larger than the MTU of the
1008 * outgoing interface.
1009 * Notify the error by sending IPV6_PATHMTU ancillary data as
1010 * well as returning an error code (the latter is not described
1014 struct ip6ctlparam ip6cp;
1016 mtu32 = (u_int32_t)mtu;
1017 bzero(&ip6cp, sizeof(ip6cp));
1018 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1019 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1027 * transmit packet without fragmentation
1029 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1030 struct in6_ifaddr *ia6;
1032 ip6 = mtod(m, struct ip6_hdr *);
1033 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1035 /* Record statistics for this interface address. */
1036 ia6->ia_ifa.if_opackets++;
1037 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
1040 /* clean ipsec history once it goes out of the node */
1043 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1048 * try to fragment the packet. case 1-b and 3
1050 if (mtu < IPV6_MMTU) {
1051 /* path MTU cannot be less than IPV6_MMTU */
1053 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1055 } else if (ip6->ip6_plen == 0) {
1056 /* jumbo payload cannot be fragmented */
1058 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1061 struct mbuf **mnext, *m_frgpart;
1062 struct ip6_frag *ip6f;
1063 u_int32_t id = htonl(ip6_randomid());
1066 struct ip6ctlparam ip6cp;
1069 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1072 * Too large for the destination or interface;
1073 * fragment if possible.
1074 * Must be able to put at least 8 bytes per fragment.
1076 hlen = unfragpartlen;
1077 if (mtu > IPV6_MAXPACKET)
1078 mtu = IPV6_MAXPACKET;
1082 * It is believed this code is a leftover from the
1083 * development of the IPV6_RECVPATHMTU sockopt and
1084 * associated work to implement RFC3542.
1085 * It's not entirely clear what the intent of the API
1086 * is at this point, so disable this code for now.
1087 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1088 * will send notifications if the application requests.
1091 /* Notify a proper path MTU to applications. */
1092 mtu32 = (u_int32_t)mtu;
1093 bzero(&ip6cp, sizeof(ip6cp));
1094 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1095 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1099 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1102 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1107 * Verify that we have any chance at all of being able to queue
1108 * the packet or packet fragments
1110 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1111 < tlen /* - hlen */)) {
1113 ip6stat.ip6s_odropped++;
1117 mnext = &m->m_nextpkt;
1120 * Change the next header field of the last header in the
1121 * unfragmentable part.
1123 if (exthdrs.ip6e_rthdr) {
1124 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1125 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1126 } else if (exthdrs.ip6e_dest1) {
1127 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1128 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1129 } else if (exthdrs.ip6e_hbh) {
1130 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1131 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1133 nextproto = ip6->ip6_nxt;
1134 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1138 * Loop through length of segment after first fragment,
1139 * make new header and copy data of each part and link onto
1143 for (off = hlen; off < tlen; off += len) {
1144 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1147 ip6stat.ip6s_odropped++;
1150 m->m_pkthdr.rcvif = NULL;
1151 m->m_flags = m0->m_flags & M_COPYFLAGS;
1153 mnext = &m->m_nextpkt;
1154 m->m_data += max_linkhdr;
1155 mhip6 = mtod(m, struct ip6_hdr *);
1157 m->m_len = sizeof(*mhip6);
1158 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1160 ip6stat.ip6s_odropped++;
1163 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1164 if (off + len >= tlen)
1167 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1168 mhip6->ip6_plen = htons((u_short)(len + hlen +
1169 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1170 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1172 ip6stat.ip6s_odropped++;
1175 m_cat(m, m_frgpart);
1176 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1177 m->m_pkthdr.rcvif = NULL;
1178 ip6f->ip6f_reserved = 0;
1179 ip6f->ip6f_ident = id;
1180 ip6f->ip6f_nxt = nextproto;
1181 ip6stat.ip6s_ofragments++;
1182 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1185 in6_ifstat_inc(ifp, ifs6_out_fragok);
1189 * Remove leading garbages.
1195 for (m0 = m; m; m = m0) {
1199 /* Record statistics for this interface address. */
1201 ia->ia_ifa.if_opackets++;
1202 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1205 /* clean ipsec history once it goes out of the node */
1208 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1214 ip6stat.ip6s_fragmented++;
1217 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1219 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1220 RTFREE(ro_pmtu->ro_rt);
1230 #endif /* FAST_IPSEC */
1235 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1236 m_freem(exthdrs.ip6e_dest1);
1237 m_freem(exthdrs.ip6e_rthdr);
1238 m_freem(exthdrs.ip6e_dest2);
1246 ip6_copyexthdr(mp, hdr, hlen)
1253 if (hlen > MCLBYTES)
1254 return (ENOBUFS); /* XXX */
1256 MGET(m, M_DONTWAIT, MT_DATA);
1261 MCLGET(m, M_DONTWAIT);
1262 if ((m->m_flags & M_EXT) == 0) {
1269 bcopy(hdr, mtod(m, caddr_t), hlen);
1276 * Insert jumbo payload option.
1279 ip6_insert_jumboopt(exthdrs, plen)
1280 struct ip6_exthdrs *exthdrs;
1287 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1290 * If there is no hop-by-hop options header, allocate new one.
1291 * If there is one but it doesn't have enough space to store the
1292 * jumbo payload option, allocate a cluster to store the whole options.
1293 * Otherwise, use it to store the options.
1295 if (exthdrs->ip6e_hbh == 0) {
1296 MGET(mopt, M_DONTWAIT, MT_DATA);
1299 mopt->m_len = JUMBOOPTLEN;
1300 optbuf = mtod(mopt, u_char *);
1301 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1302 exthdrs->ip6e_hbh = mopt;
1304 struct ip6_hbh *hbh;
1306 mopt = exthdrs->ip6e_hbh;
1307 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1310 * - exthdrs->ip6e_hbh is not referenced from places
1311 * other than exthdrs.
1312 * - exthdrs->ip6e_hbh is not an mbuf chain.
1314 int oldoptlen = mopt->m_len;
1318 * XXX: give up if the whole (new) hbh header does
1319 * not fit even in an mbuf cluster.
1321 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1325 * As a consequence, we must always prepare a cluster
1328 MGET(n, M_DONTWAIT, MT_DATA);
1330 MCLGET(n, M_DONTWAIT);
1331 if ((n->m_flags & M_EXT) == 0) {
1338 n->m_len = oldoptlen + JUMBOOPTLEN;
1339 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1341 optbuf = mtod(n, caddr_t) + oldoptlen;
1343 mopt = exthdrs->ip6e_hbh = n;
1345 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1346 mopt->m_len += JUMBOOPTLEN;
1348 optbuf[0] = IP6OPT_PADN;
1352 * Adjust the header length according to the pad and
1353 * the jumbo payload option.
1355 hbh = mtod(mopt, struct ip6_hbh *);
1356 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1359 /* fill in the option. */
1360 optbuf[2] = IP6OPT_JUMBO;
1362 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1363 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1365 /* finally, adjust the packet header length */
1366 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1373 * Insert fragment header and copy unfragmentable header portions.
1376 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1377 struct mbuf *m0, *m;
1379 struct ip6_frag **frghdrp;
1381 struct mbuf *n, *mlast;
1383 if (hlen > sizeof(struct ip6_hdr)) {
1384 n = m_copym(m0, sizeof(struct ip6_hdr),
1385 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1392 /* Search for the last mbuf of unfragmentable part. */
1393 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1396 if ((mlast->m_flags & M_EXT) == 0 &&
1397 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1398 /* use the trailing space of the last mbuf for the fragment hdr */
1399 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1401 mlast->m_len += sizeof(struct ip6_frag);
1402 m->m_pkthdr.len += sizeof(struct ip6_frag);
1404 /* allocate a new mbuf for the fragment header */
1407 MGET(mfrg, M_DONTWAIT, MT_DATA);
1410 mfrg->m_len = sizeof(struct ip6_frag);
1411 *frghdrp = mtod(mfrg, struct ip6_frag *);
1412 mlast->m_next = mfrg;
1419 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1420 struct route_in6 *ro_pmtu, *ro;
1422 struct in6_addr *dst;
1430 if (ro_pmtu != ro) {
1431 /* The first hop and the final destination may differ. */
1432 struct sockaddr_in6 *sa6_dst =
1433 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1434 if (ro_pmtu->ro_rt &&
1435 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1436 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1437 RTFREE(ro_pmtu->ro_rt);
1438 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1440 if (ro_pmtu->ro_rt == NULL) {
1441 bzero(sa6_dst, sizeof(*sa6_dst));
1442 sa6_dst->sin6_family = AF_INET6;
1443 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1444 sa6_dst->sin6_addr = *dst;
1446 rtalloc((struct route *)ro_pmtu);
1449 if (ro_pmtu->ro_rt) {
1451 struct in_conninfo inc;
1453 bzero(&inc, sizeof(inc));
1454 inc.inc_flags = 1; /* IPv6 */
1455 inc.inc6_faddr = *dst;
1458 ifp = ro_pmtu->ro_rt->rt_ifp;
1459 ifmtu = IN6_LINKMTU(ifp);
1460 mtu = tcp_hc_getmtu(&inc);
1462 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1464 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1467 else if (mtu < IPV6_MMTU) {
1469 * RFC2460 section 5, last paragraph:
1470 * if we record ICMPv6 too big message with
1471 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1472 * or smaller, with framgent header attached.
1473 * (fragment header is needed regardless from the
1474 * packet size, for translators to identify packets)
1478 } else if (mtu > ifmtu) {
1480 * The MTU on the route is larger than the MTU on
1481 * the interface! This shouldn't happen, unless the
1482 * MTU of the interface has been changed after the
1483 * interface was brought up. Change the MTU in the
1484 * route to match the interface MTU (as long as the
1485 * field isn't locked).
1488 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1491 mtu = IN6_LINKMTU(ifp);
1493 error = EHOSTUNREACH; /* XXX */
1497 *alwaysfragp = alwaysfrag;
1502 * IP6 socket option processing.
1505 ip6_ctloutput(so, sopt)
1507 struct sockopt *sopt;
1509 int privileged, optdatalen, uproto;
1511 struct inpcb *in6p = sotoinpcb(so);
1513 int level, op, optname;
1518 level = sopt->sopt_level;
1519 op = sopt->sopt_dir;
1520 optname = sopt->sopt_name;
1521 optlen = sopt->sopt_valsize;
1524 panic("ip6_ctloutput: arg soopt is NULL");
1528 privileged = (td == 0 || suser(td)) ? 0 : 1;
1529 uproto = (int)so->so_proto->pr_protocol;
1531 if (level == IPPROTO_IPV6) {
1536 case IPV6_2292PKTOPTIONS:
1537 #ifdef IPV6_PKTOPTIONS
1538 case IPV6_PKTOPTIONS:
1543 error = soopt_getm(sopt, &m); /* XXX */
1546 error = soopt_mcopyin(sopt, m); /* XXX */
1549 error = ip6_pcbopts(&in6p->in6p_outputopts,
1551 m_freem(m); /* XXX */
1556 * Use of some Hop-by-Hop options or some
1557 * Destination options, might require special
1558 * privilege. That is, normal applications
1559 * (without special privilege) might be forbidden
1560 * from setting certain options in outgoing packets,
1561 * and might never see certain options in received
1562 * packets. [RFC 2292 Section 6]
1563 * KAME specific note:
1564 * KAME prevents non-privileged users from sending or
1565 * receiving ANY hbh/dst options in order to avoid
1566 * overhead of parsing options in the kernel.
1568 case IPV6_RECVHOPOPTS:
1569 case IPV6_RECVDSTOPTS:
1570 case IPV6_RECVRTHDRDSTOPTS:
1576 case IPV6_UNICAST_HOPS:
1580 case IPV6_RECVPKTINFO:
1581 case IPV6_RECVHOPLIMIT:
1582 case IPV6_RECVRTHDR:
1583 case IPV6_RECVPATHMTU:
1584 case IPV6_RECVTCLASS:
1586 case IPV6_AUTOFLOWLABEL:
1587 if (optlen != sizeof(int)) {
1591 error = sooptcopyin(sopt, &optval,
1592 sizeof optval, sizeof optval);
1597 case IPV6_UNICAST_HOPS:
1598 if (optval < -1 || optval >= 256)
1601 /* -1 = kernel default */
1602 in6p->in6p_hops = optval;
1603 if ((in6p->in6p_vflag &
1605 in6p->inp_ip_ttl = optval;
1608 #define OPTSET(bit) \
1611 in6p->in6p_flags |= (bit); \
1613 in6p->in6p_flags &= ~(bit); \
1614 } while (/*CONSTCOND*/ 0)
1615 #define OPTSET2292(bit) \
1617 in6p->in6p_flags |= IN6P_RFC2292; \
1619 in6p->in6p_flags |= (bit); \
1621 in6p->in6p_flags &= ~(bit); \
1622 } while (/*CONSTCOND*/ 0)
1623 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1625 case IPV6_RECVPKTINFO:
1626 /* cannot mix with RFC2292 */
1627 if (OPTBIT(IN6P_RFC2292)) {
1631 OPTSET(IN6P_PKTINFO);
1636 struct ip6_pktopts **optp;
1638 /* cannot mix with RFC2292 */
1639 if (OPTBIT(IN6P_RFC2292)) {
1643 optp = &in6p->in6p_outputopts;
1644 error = ip6_pcbopt(IPV6_HOPLIMIT,
1648 privileged, uproto);
1652 case IPV6_RECVHOPLIMIT:
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1658 OPTSET(IN6P_HOPLIMIT);
1661 case IPV6_RECVHOPOPTS:
1662 /* cannot mix with RFC2292 */
1663 if (OPTBIT(IN6P_RFC2292)) {
1667 OPTSET(IN6P_HOPOPTS);
1670 case IPV6_RECVDSTOPTS:
1671 /* cannot mix with RFC2292 */
1672 if (OPTBIT(IN6P_RFC2292)) {
1676 OPTSET(IN6P_DSTOPTS);
1679 case IPV6_RECVRTHDRDSTOPTS:
1680 /* cannot mix with RFC2292 */
1681 if (OPTBIT(IN6P_RFC2292)) {
1685 OPTSET(IN6P_RTHDRDSTOPTS);
1688 case IPV6_RECVRTHDR:
1689 /* cannot mix with RFC2292 */
1690 if (OPTBIT(IN6P_RFC2292)) {
1701 case IPV6_RECVPATHMTU:
1703 * We ignore this option for TCP
1705 * (RFC3542 leaves this case
1708 if (uproto != IPPROTO_TCP)
1714 * make setsockopt(IPV6_V6ONLY)
1715 * available only prior to bind(2).
1716 * see ipng mailing list, Jun 22 2001.
1718 if (in6p->in6p_lport ||
1719 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1723 OPTSET(IN6P_IPV6_V6ONLY);
1725 in6p->in6p_vflag &= ~INP_IPV4;
1727 in6p->in6p_vflag |= INP_IPV4;
1729 case IPV6_RECVTCLASS:
1730 /* cannot mix with RFC2292 XXX */
1731 if (OPTBIT(IN6P_RFC2292)) {
1735 OPTSET(IN6P_TCLASS);
1737 case IPV6_AUTOFLOWLABEL:
1738 OPTSET(IN6P_AUTOFLOWLABEL);
1746 case IPV6_USE_MIN_MTU:
1747 case IPV6_PREFER_TEMPADDR:
1748 if (optlen != sizeof(optval)) {
1752 error = sooptcopyin(sopt, &optval,
1753 sizeof optval, sizeof optval);
1757 struct ip6_pktopts **optp;
1758 optp = &in6p->in6p_outputopts;
1759 error = ip6_pcbopt(optname,
1763 privileged, uproto);
1767 case IPV6_2292PKTINFO:
1768 case IPV6_2292HOPLIMIT:
1769 case IPV6_2292HOPOPTS:
1770 case IPV6_2292DSTOPTS:
1771 case IPV6_2292RTHDR:
1773 if (optlen != sizeof(int)) {
1777 error = sooptcopyin(sopt, &optval,
1778 sizeof optval, sizeof optval);
1782 case IPV6_2292PKTINFO:
1783 OPTSET2292(IN6P_PKTINFO);
1785 case IPV6_2292HOPLIMIT:
1786 OPTSET2292(IN6P_HOPLIMIT);
1788 case IPV6_2292HOPOPTS:
1790 * Check super-user privilege.
1791 * See comments for IPV6_RECVHOPOPTS.
1795 OPTSET2292(IN6P_HOPOPTS);
1797 case IPV6_2292DSTOPTS:
1800 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1802 case IPV6_2292RTHDR:
1803 OPTSET2292(IN6P_RTHDR);
1811 case IPV6_RTHDRDSTOPTS:
1814 /* new advanced API (RFC3542) */
1817 struct ip6_pktopts **optp;
1819 /* cannot mix with RFC2292 */
1820 if (OPTBIT(IN6P_RFC2292)) {
1828 case IPV6_RTHDRDSTOPTS:
1839 optlen = sizeof(struct in6_pktinfo);
1842 optlen = SOCK_MAXADDRLEN;
1845 optlen = IPV6_MAXOPTHDR;
1848 if (sopt->sopt_valsize > optlen) {
1853 optlen = sopt->sopt_valsize;
1854 optbuf = malloc(optlen, M_TEMP, M_WAITOK);
1855 error = sooptcopyin(sopt, optbuf, optlen,
1858 free(optbuf, M_TEMP);
1862 optp = &in6p->in6p_outputopts;
1863 error = ip6_pcbopt(optname,
1865 optp, privileged, uproto);
1866 free(optbuf, M_TEMP);
1871 case IPV6_MULTICAST_IF:
1872 case IPV6_MULTICAST_HOPS:
1873 case IPV6_MULTICAST_LOOP:
1874 case IPV6_JOIN_GROUP:
1875 case IPV6_LEAVE_GROUP:
1877 if (sopt->sopt_valsize > MLEN) {
1887 if (sopt->sopt_valsize > MCLBYTES) {
1892 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER);
1897 if (sopt->sopt_valsize > MLEN) {
1898 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1899 if ((m->m_flags & M_EXT) == 0) {
1905 m->m_len = sopt->sopt_valsize;
1906 error = sooptcopyin(sopt, mtod(m, char *),
1907 m->m_len, m->m_len);
1912 error = ip6_setmoptions(sopt->sopt_name,
1913 &in6p->in6p_moptions,
1919 case IPV6_PORTRANGE:
1920 error = sooptcopyin(sopt, &optval,
1921 sizeof optval, sizeof optval);
1926 case IPV6_PORTRANGE_DEFAULT:
1927 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1928 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1931 case IPV6_PORTRANGE_HIGH:
1932 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1933 in6p->in6p_flags |= IN6P_HIGHPORT;
1936 case IPV6_PORTRANGE_LOW:
1937 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1938 in6p->in6p_flags |= IN6P_LOWPORT;
1947 #if defined(IPSEC) || defined(FAST_IPSEC)
1948 case IPV6_IPSEC_POLICY:
1954 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1956 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1959 req = mtod(m, caddr_t);
1962 error = ipsec6_set_policy(in6p, optname, req,
1967 #endif /* KAME IPSEC */
1975 struct mbuf **mp = &m;
1977 if (ip6_fw_ctl_ptr == NULL)
1980 if ((error = soopt_getm(sopt, &m)) != 0)
1983 if ((error = soopt_mcopyin(sopt, m)) != 0)
1985 error = (*ip6_fw_ctl_ptr)(optname, mp);
1991 error = ENOPROTOOPT;
1999 case IPV6_2292PKTOPTIONS:
2000 #ifdef IPV6_PKTOPTIONS
2001 case IPV6_PKTOPTIONS:
2004 * RFC3542 (effectively) deprecated the
2005 * semantics of the 2292-style pktoptions.
2006 * Since it was not reliable in nature (i.e.,
2007 * applications had to expect the lack of some
2008 * information after all), it would make sense
2009 * to simplify this part by always returning
2012 sopt->sopt_valsize = 0;
2015 case IPV6_RECVHOPOPTS:
2016 case IPV6_RECVDSTOPTS:
2017 case IPV6_RECVRTHDRDSTOPTS:
2018 case IPV6_UNICAST_HOPS:
2019 case IPV6_RECVPKTINFO:
2020 case IPV6_RECVHOPLIMIT:
2021 case IPV6_RECVRTHDR:
2022 case IPV6_RECVPATHMTU:
2026 case IPV6_PORTRANGE:
2027 case IPV6_RECVTCLASS:
2028 case IPV6_AUTOFLOWLABEL:
2031 case IPV6_RECVHOPOPTS:
2032 optval = OPTBIT(IN6P_HOPOPTS);
2035 case IPV6_RECVDSTOPTS:
2036 optval = OPTBIT(IN6P_DSTOPTS);
2039 case IPV6_RECVRTHDRDSTOPTS:
2040 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2043 case IPV6_UNICAST_HOPS:
2044 optval = in6p->in6p_hops;
2047 case IPV6_RECVPKTINFO:
2048 optval = OPTBIT(IN6P_PKTINFO);
2051 case IPV6_RECVHOPLIMIT:
2052 optval = OPTBIT(IN6P_HOPLIMIT);
2055 case IPV6_RECVRTHDR:
2056 optval = OPTBIT(IN6P_RTHDR);
2059 case IPV6_RECVPATHMTU:
2060 optval = OPTBIT(IN6P_MTU);
2064 optval = OPTBIT(IN6P_FAITH);
2068 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2071 case IPV6_PORTRANGE:
2074 flags = in6p->in6p_flags;
2075 if (flags & IN6P_HIGHPORT)
2076 optval = IPV6_PORTRANGE_HIGH;
2077 else if (flags & IN6P_LOWPORT)
2078 optval = IPV6_PORTRANGE_LOW;
2083 case IPV6_RECVTCLASS:
2084 optval = OPTBIT(IN6P_TCLASS);
2087 case IPV6_AUTOFLOWLABEL:
2088 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2093 error = sooptcopyout(sopt, &optval,
2100 struct ip6_mtuinfo mtuinfo;
2101 struct route_in6 sro;
2103 bzero(&sro, sizeof(sro));
2105 if (!(so->so_state & SS_ISCONNECTED))
2108 * XXX: we dot not consider the case of source
2109 * routing, or optional information to specify
2110 * the outgoing interface.
2112 error = ip6_getpmtu(&sro, NULL, NULL,
2113 &in6p->in6p_faddr, &pmtu, NULL);
2118 if (pmtu > IPV6_MAXPACKET)
2119 pmtu = IPV6_MAXPACKET;
2121 bzero(&mtuinfo, sizeof(mtuinfo));
2122 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2123 optdata = (void *)&mtuinfo;
2124 optdatalen = sizeof(mtuinfo);
2125 error = sooptcopyout(sopt, optdata,
2130 case IPV6_2292PKTINFO:
2131 case IPV6_2292HOPLIMIT:
2132 case IPV6_2292HOPOPTS:
2133 case IPV6_2292RTHDR:
2134 case IPV6_2292DSTOPTS:
2136 case IPV6_2292PKTINFO:
2137 optval = OPTBIT(IN6P_PKTINFO);
2139 case IPV6_2292HOPLIMIT:
2140 optval = OPTBIT(IN6P_HOPLIMIT);
2142 case IPV6_2292HOPOPTS:
2143 optval = OPTBIT(IN6P_HOPOPTS);
2145 case IPV6_2292RTHDR:
2146 optval = OPTBIT(IN6P_RTHDR);
2148 case IPV6_2292DSTOPTS:
2149 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2152 error = sooptcopyout(sopt, &optval,
2159 case IPV6_RTHDRDSTOPTS:
2163 case IPV6_USE_MIN_MTU:
2164 case IPV6_PREFER_TEMPADDR:
2165 error = ip6_getpcbopt(in6p->in6p_outputopts,
2169 case IPV6_MULTICAST_IF:
2170 case IPV6_MULTICAST_HOPS:
2171 case IPV6_MULTICAST_LOOP:
2172 case IPV6_JOIN_GROUP:
2173 case IPV6_LEAVE_GROUP:
2176 error = ip6_getmoptions(sopt->sopt_name,
2177 in6p->in6p_moptions, &m);
2179 error = sooptcopyout(sopt,
2180 mtod(m, char *), m->m_len);
2185 #if defined(IPSEC) || defined(FAST_IPSEC)
2186 case IPV6_IPSEC_POLICY:
2190 struct mbuf *m = NULL;
2191 struct mbuf **mp = &m;
2192 size_t ovalsize = sopt->sopt_valsize;
2193 caddr_t oval = (caddr_t)sopt->sopt_val;
2195 error = soopt_getm(sopt, &m); /* XXX */
2198 error = soopt_mcopyin(sopt, m); /* XXX */
2201 sopt->sopt_valsize = ovalsize;
2202 sopt->sopt_val = oval;
2204 req = mtod(m, caddr_t);
2207 error = ipsec6_get_policy(in6p, req, len, mp);
2209 error = soopt_mcopyout(sopt, m); /* XXX */
2210 if (error == 0 && m)
2214 #endif /* KAME IPSEC */
2219 struct mbuf **mp = &m;
2221 if (ip6_fw_ctl_ptr == NULL)
2225 error = (*ip6_fw_ctl_ptr)(optname, mp);
2227 error = soopt_mcopyout(sopt, m); /* XXX */
2228 if (error == 0 && m)
2234 error = ENOPROTOOPT;
2239 } else { /* level != IPPROTO_IPV6 */
2246 ip6_raw_ctloutput(so, sopt)
2248 struct sockopt *sopt;
2250 int error = 0, optval, optlen;
2251 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2252 struct in6pcb *in6p = sotoin6pcb(so);
2253 int level, op, optname;
2256 level = sopt->sopt_level;
2257 op = sopt->sopt_dir;
2258 optname = sopt->sopt_name;
2259 optlen = sopt->sopt_valsize;
2261 panic("ip6_raw_ctloutput: arg soopt is NULL");
2263 if (level != IPPROTO_IPV6) {
2270 * For ICMPv6 sockets, no modification allowed for checksum
2271 * offset, permit "no change" values to help existing apps.
2273 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2274 * for an ICMPv6 socket will fail."
2275 * The current behavior does not meet RFC3542.
2279 if (optlen != sizeof(int)) {
2283 error = sooptcopyin(sopt, &optval, sizeof(optval),
2287 if ((optval % 2) != 0) {
2288 /* the API assumes even offset values */
2290 } else if (so->so_proto->pr_protocol ==
2292 if (optval != icmp6off)
2295 in6p->in6p_cksum = optval;
2299 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2302 optval = in6p->in6p_cksum;
2304 error = sooptcopyout(sopt, &optval, sizeof(optval));
2314 error = ENOPROTOOPT;
2322 * Set up IP6 options in pcb for insertion in output packets or
2323 * specifying behavior of outgoing packets.
2326 ip6_pcbopts(pktopt, m, so, sopt)
2327 struct ip6_pktopts **pktopt;
2330 struct sockopt *sopt;
2332 struct ip6_pktopts *opt = *pktopt;
2334 struct thread *td = sopt->sopt_td;
2337 /* turn off any old options. */
2340 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2341 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2342 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2343 printf("ip6_pcbopts: all specified options are cleared.\n");
2345 ip6_clearpktopts(opt, -1);
2347 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2350 if (!m || m->m_len == 0) {
2352 * Only turning off any previous options, regardless of
2353 * whether the opt is just created or given.
2355 free(opt, M_IP6OPT);
2359 /* set options specified by user. */
2360 if (td && !suser(td))
2362 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2363 so->so_proto->pr_protocol)) != 0) {
2364 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2365 free(opt, M_IP6OPT);
2373 * initialize ip6_pktopts. beware that there are non-zero default values in
2377 ip6_initpktopts(opt)
2378 struct ip6_pktopts *opt;
2381 bzero(opt, sizeof(*opt));
2382 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2383 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2384 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2385 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2389 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2390 int optname, len, priv;
2392 struct ip6_pktopts **pktopt;
2395 struct ip6_pktopts *opt;
2397 if (*pktopt == NULL) {
2398 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2400 ip6_initpktopts(*pktopt);
2404 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2408 ip6_getpcbopt(pktopt, optname, sopt)
2409 struct ip6_pktopts *pktopt;
2410 struct sockopt *sopt;
2413 void *optdata = NULL;
2415 struct ip6_ext *ip6e;
2417 struct in6_pktinfo null_pktinfo;
2418 int deftclass = 0, on;
2419 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2420 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2424 if (pktopt && pktopt->ip6po_pktinfo)
2425 optdata = (void *)pktopt->ip6po_pktinfo;
2427 /* XXX: we don't have to do this every time... */
2428 bzero(&null_pktinfo, sizeof(null_pktinfo));
2429 optdata = (void *)&null_pktinfo;
2431 optdatalen = sizeof(struct in6_pktinfo);
2434 if (pktopt && pktopt->ip6po_tclass >= 0)
2435 optdata = (void *)&pktopt->ip6po_tclass;
2437 optdata = (void *)&deftclass;
2438 optdatalen = sizeof(int);
2441 if (pktopt && pktopt->ip6po_hbh) {
2442 optdata = (void *)pktopt->ip6po_hbh;
2443 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2444 optdatalen = (ip6e->ip6e_len + 1) << 3;
2448 if (pktopt && pktopt->ip6po_rthdr) {
2449 optdata = (void *)pktopt->ip6po_rthdr;
2450 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2451 optdatalen = (ip6e->ip6e_len + 1) << 3;
2454 case IPV6_RTHDRDSTOPTS:
2455 if (pktopt && pktopt->ip6po_dest1) {
2456 optdata = (void *)pktopt->ip6po_dest1;
2457 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2458 optdatalen = (ip6e->ip6e_len + 1) << 3;
2462 if (pktopt && pktopt->ip6po_dest2) {
2463 optdata = (void *)pktopt->ip6po_dest2;
2464 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2465 optdatalen = (ip6e->ip6e_len + 1) << 3;
2469 if (pktopt && pktopt->ip6po_nexthop) {
2470 optdata = (void *)pktopt->ip6po_nexthop;
2471 optdatalen = pktopt->ip6po_nexthop->sa_len;
2474 case IPV6_USE_MIN_MTU:
2476 optdata = (void *)&pktopt->ip6po_minmtu;
2478 optdata = (void *)&defminmtu;
2479 optdatalen = sizeof(int);
2482 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2486 optdata = (void *)&on;
2487 optdatalen = sizeof(on);
2489 case IPV6_PREFER_TEMPADDR:
2491 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2493 optdata = (void *)&defpreftemp;
2494 optdatalen = sizeof(int);
2496 default: /* should not happen */
2498 panic("ip6_getpcbopt: unexpected option\n");
2500 return (ENOPROTOOPT);
2503 error = sooptcopyout(sopt, optdata, optdatalen);
2509 ip6_clearpktopts(pktopt, optname)
2510 struct ip6_pktopts *pktopt;
2516 if (optname == -1 || optname == IPV6_PKTINFO) {
2517 if (pktopt->ip6po_pktinfo)
2518 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2519 pktopt->ip6po_pktinfo = NULL;
2521 if (optname == -1 || optname == IPV6_HOPLIMIT)
2522 pktopt->ip6po_hlim = -1;
2523 if (optname == -1 || optname == IPV6_TCLASS)
2524 pktopt->ip6po_tclass = -1;
2525 if (optname == -1 || optname == IPV6_NEXTHOP) {
2526 if (pktopt->ip6po_nextroute.ro_rt) {
2527 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2528 pktopt->ip6po_nextroute.ro_rt = NULL;
2530 if (pktopt->ip6po_nexthop)
2531 free(pktopt->ip6po_nexthop, M_IP6OPT);
2532 pktopt->ip6po_nexthop = NULL;
2534 if (optname == -1 || optname == IPV6_HOPOPTS) {
2535 if (pktopt->ip6po_hbh)
2536 free(pktopt->ip6po_hbh, M_IP6OPT);
2537 pktopt->ip6po_hbh = NULL;
2539 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2540 if (pktopt->ip6po_dest1)
2541 free(pktopt->ip6po_dest1, M_IP6OPT);
2542 pktopt->ip6po_dest1 = NULL;
2544 if (optname == -1 || optname == IPV6_RTHDR) {
2545 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2546 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2547 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2548 if (pktopt->ip6po_route.ro_rt) {
2549 RTFREE(pktopt->ip6po_route.ro_rt);
2550 pktopt->ip6po_route.ro_rt = NULL;
2553 if (optname == -1 || optname == IPV6_DSTOPTS) {
2554 if (pktopt->ip6po_dest2)
2555 free(pktopt->ip6po_dest2, M_IP6OPT);
2556 pktopt->ip6po_dest2 = NULL;
2560 #define PKTOPT_EXTHDRCPY(type) \
2563 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2564 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2565 if (dst->type == NULL && canwait == M_NOWAIT)\
2567 bcopy(src->type, dst->type, hlen);\
2569 } while (/*CONSTCOND*/ 0)
2572 copypktopts(dst, src, canwait)
2573 struct ip6_pktopts *dst, *src;
2576 if (dst == NULL || src == NULL) {
2577 printf("ip6_clearpktopts: invalid argument\n");
2581 dst->ip6po_hlim = src->ip6po_hlim;
2582 dst->ip6po_tclass = src->ip6po_tclass;
2583 dst->ip6po_flags = src->ip6po_flags;
2584 if (src->ip6po_pktinfo) {
2585 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2587 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2589 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2591 if (src->ip6po_nexthop) {
2592 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2594 if (dst->ip6po_nexthop == NULL)
2596 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2597 src->ip6po_nexthop->sa_len);
2599 PKTOPT_EXTHDRCPY(ip6po_hbh);
2600 PKTOPT_EXTHDRCPY(ip6po_dest1);
2601 PKTOPT_EXTHDRCPY(ip6po_dest2);
2602 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2606 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2607 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2608 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2609 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2610 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2611 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2614 #undef PKTOPT_EXTHDRCPY
2616 struct ip6_pktopts *
2617 ip6_copypktopts(src, canwait)
2618 struct ip6_pktopts *src;
2622 struct ip6_pktopts *dst;
2624 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2625 if (dst == NULL && canwait == M_NOWAIT)
2627 ip6_initpktopts(dst);
2629 if ((error = copypktopts(dst, src, canwait)) != 0) {
2630 free(dst, M_IP6OPT);
2638 ip6_freepcbopts(pktopt)
2639 struct ip6_pktopts *pktopt;
2644 ip6_clearpktopts(pktopt, -1);
2646 free(pktopt, M_IP6OPT);
2650 * Set the IP6 multicast options in response to user setsockopt().
2653 ip6_setmoptions(optname, im6op, m)
2655 struct ip6_moptions **im6op;
2659 u_int loop, ifindex;
2660 struct ipv6_mreq *mreq;
2662 struct ip6_moptions *im6o = *im6op;
2663 struct route_in6 ro;
2664 struct sockaddr_in6 *dst;
2665 struct in6_multi_mship *imm;
2666 struct thread *td = curthread;
2670 * No multicast option buffer attached to the pcb;
2671 * allocate one and initialize to default values.
2673 im6o = (struct ip6_moptions *)
2674 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2679 im6o->im6o_multicast_ifp = NULL;
2680 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2681 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2682 LIST_INIT(&im6o->im6o_memberships);
2687 case IPV6_MULTICAST_IF:
2689 * Select the interface for outgoing multicast packets.
2691 if (m == NULL || m->m_len != sizeof(u_int)) {
2695 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2696 if (ifindex < 0 || if_index < ifindex) {
2697 error = ENXIO; /* XXX EINVAL? */
2700 ifp = ifnet_byindex(ifindex);
2701 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2702 error = EADDRNOTAVAIL;
2705 im6o->im6o_multicast_ifp = ifp;
2708 case IPV6_MULTICAST_HOPS:
2711 * Set the IP6 hoplimit for outgoing multicast packets.
2714 if (m == NULL || m->m_len != sizeof(int)) {
2718 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2719 if (optval < -1 || optval >= 256)
2721 else if (optval == -1)
2722 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2724 im6o->im6o_multicast_hlim = optval;
2728 case IPV6_MULTICAST_LOOP:
2730 * Set the loopback flag for outgoing multicast packets.
2731 * Must be zero or one.
2733 if (m == NULL || m->m_len != sizeof(u_int)) {
2737 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2742 im6o->im6o_multicast_loop = loop;
2745 case IPV6_JOIN_GROUP:
2747 * Add a multicast group membership.
2748 * Group must be a valid IP6 multicast address.
2750 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2754 mreq = mtod(m, struct ipv6_mreq *);
2755 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2757 * We use the unspecified address to specify to accept
2758 * all multicast addresses. Only super user is allowed
2765 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2771 * If the interface is specified, validate it.
2773 if (mreq->ipv6mr_interface < 0 ||
2774 if_index < mreq->ipv6mr_interface) {
2775 error = ENXIO; /* XXX EINVAL? */
2779 * If no interface was explicitly specified, choose an
2780 * appropriate one according to the given multicast address.
2782 if (mreq->ipv6mr_interface == 0) {
2784 * If the multicast address is in node-local scope,
2785 * the interface should be a loopback interface.
2786 * Otherwise, look up the routing table for the
2787 * address, and choose the outgoing interface.
2788 * XXX: is it a good approach?
2790 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) {
2794 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2795 bzero(dst, sizeof(*dst));
2796 dst->sin6_len = sizeof(struct sockaddr_in6);
2797 dst->sin6_family = AF_INET6;
2798 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2799 rtalloc((struct route *)&ro);
2800 if (ro.ro_rt == NULL) {
2801 error = EADDRNOTAVAIL;
2804 ifp = ro.ro_rt->rt_ifp;
2808 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2811 * See if we found an interface, and confirm that it
2812 * supports multicast
2814 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2815 error = EADDRNOTAVAIL;
2819 * Put interface index into the multicast address,
2820 * if the address has link-local scope.
2822 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2823 mreq->ipv6mr_multiaddr.s6_addr16[1] =
2824 htons(ifp->if_index);
2827 * See if the membership already exists.
2829 for (imm = im6o->im6o_memberships.lh_first;
2830 imm != NULL; imm = imm->i6mm_chain.le_next)
2831 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2832 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2833 &mreq->ipv6mr_multiaddr))
2840 * Everything looks good; add a new record to the multicast
2841 * address list for the given interface.
2843 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2848 if ((imm->i6mm_maddr =
2849 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2850 free(imm, M_IPMADDR);
2853 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2856 case IPV6_LEAVE_GROUP:
2858 * Drop a multicast group membership.
2859 * Group must be a valid IP6 multicast address.
2861 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2865 mreq = mtod(m, struct ipv6_mreq *);
2866 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2871 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2876 * If an interface address was specified, get a pointer
2877 * to its ifnet structure.
2879 if (mreq->ipv6mr_interface < 0
2880 || if_index < mreq->ipv6mr_interface) {
2881 error = ENXIO; /* XXX EINVAL? */
2884 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2886 * Put interface index into the multicast address,
2887 * if the address has link-local scope.
2889 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2890 mreq->ipv6mr_multiaddr.s6_addr16[1]
2891 = htons(mreq->ipv6mr_interface);
2895 * Find the membership in the membership list.
2897 for (imm = im6o->im6o_memberships.lh_first;
2898 imm != NULL; imm = imm->i6mm_chain.le_next) {
2899 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2900 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2901 &mreq->ipv6mr_multiaddr))
2905 /* Unable to resolve interface */
2906 error = EADDRNOTAVAIL;
2910 * Give up the multicast address record to which the
2911 * membership points.
2913 LIST_REMOVE(imm, i6mm_chain);
2914 in6_delmulti(imm->i6mm_maddr);
2915 free(imm, M_IPMADDR);
2924 * If all options have default values, no need to keep the mbuf.
2926 if (im6o->im6o_multicast_ifp == NULL &&
2927 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2928 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2929 im6o->im6o_memberships.lh_first == NULL) {
2930 free(*im6op, M_IPMOPTS);
2938 * Return the IP6 multicast options in response to user getsockopt().
2941 ip6_getmoptions(optname, im6o, mp)
2943 struct ip6_moptions *im6o;
2946 u_int *hlim, *loop, *ifindex;
2948 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2952 case IPV6_MULTICAST_IF:
2953 ifindex = mtod(*mp, u_int *);
2954 (*mp)->m_len = sizeof(u_int);
2955 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2958 *ifindex = im6o->im6o_multicast_ifp->if_index;
2961 case IPV6_MULTICAST_HOPS:
2962 hlim = mtod(*mp, u_int *);
2963 (*mp)->m_len = sizeof(u_int);
2965 *hlim = ip6_defmcasthlim;
2967 *hlim = im6o->im6o_multicast_hlim;
2970 case IPV6_MULTICAST_LOOP:
2971 loop = mtod(*mp, u_int *);
2972 (*mp)->m_len = sizeof(u_int);
2974 *loop = ip6_defmcasthlim;
2976 *loop = im6o->im6o_multicast_loop;
2980 return (EOPNOTSUPP);
2985 * Discard the IP6 multicast options.
2988 ip6_freemoptions(im6o)
2989 struct ip6_moptions *im6o;
2991 struct in6_multi_mship *imm;
2996 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2997 LIST_REMOVE(imm, i6mm_chain);
2998 if (imm->i6mm_maddr)
2999 in6_delmulti(imm->i6mm_maddr);
3000 free(imm, M_IPMADDR);
3002 free(im6o, M_IPMOPTS);
3006 * Set IPv6 outgoing packet options based on advanced API.
3009 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
3010 struct mbuf *control;
3011 struct ip6_pktopts *opt, *stickyopt;
3014 struct cmsghdr *cm = 0;
3016 if (control == NULL || opt == NULL)
3019 ip6_initpktopts(opt);
3024 * If stickyopt is provided, make a local copy of the options
3025 * for this particular packet, then override them by ancillary
3027 * XXX: copypktopts() does not copy the cached route to a next
3028 * hop (if any). This is not very good in terms of efficiency,
3029 * but we can allow this since this option should be rarely
3032 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
3037 * XXX: Currently, we assume all the optional information is stored
3040 if (control->m_next)
3043 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
3044 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
3047 if (control->m_len < CMSG_LEN(0))
3050 cm = mtod(control, struct cmsghdr *);
3051 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
3053 if (cm->cmsg_level != IPPROTO_IPV6)
3056 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
3057 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
3066 * Set a particular packet option, as a sticky option or an ancillary data
3067 * item. "len" can be 0 only when it's a sticky option.
3068 * We have 4 cases of combination of "sticky" and "cmsg":
3069 * "sticky=0, cmsg=0": impossible
3070 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3071 * "sticky=1, cmsg=0": RFC3542 socket option
3072 * "sticky=1, cmsg=1": RFC2292 socket option
3075 ip6_setpktopt(optname, buf, len, opt, priv, sticky, cmsg, uproto)
3076 int optname, len, priv, sticky, cmsg, uproto;
3078 struct ip6_pktopts *opt;
3080 int minmtupolicy, preftemp;
3082 if (!sticky && !cmsg) {
3084 printf("ip6_setpktopt: impossible case\n");
3090 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3091 * not be specified in the context of RFC3542. Conversely,
3092 * RFC3542 types should not be specified in the context of RFC2292.
3096 case IPV6_2292PKTINFO:
3097 case IPV6_2292HOPLIMIT:
3098 case IPV6_2292NEXTHOP:
3099 case IPV6_2292HOPOPTS:
3100 case IPV6_2292DSTOPTS:
3101 case IPV6_2292RTHDR:
3102 case IPV6_2292PKTOPTIONS:
3103 return (ENOPROTOOPT);
3106 if (sticky && cmsg) {
3113 case IPV6_RTHDRDSTOPTS:
3115 case IPV6_USE_MIN_MTU:
3118 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3119 return (ENOPROTOOPT);
3124 case IPV6_2292PKTINFO:
3127 struct ifnet *ifp = NULL;
3128 struct in6_pktinfo *pktinfo;
3130 if (len != sizeof(struct in6_pktinfo))
3133 pktinfo = (struct in6_pktinfo *)buf;
3136 * An application can clear any sticky IPV6_PKTINFO option by
3137 * doing a "regular" setsockopt with ipi6_addr being
3138 * in6addr_any and ipi6_ifindex being zero.
3139 * [RFC 3542, Section 6]
3141 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3142 pktinfo->ipi6_ifindex == 0 &&
3143 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3144 ip6_clearpktopts(opt, optname);
3148 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3149 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3153 /* validate the interface index if specified. */
3154 if (pktinfo->ipi6_ifindex > if_index ||
3155 pktinfo->ipi6_ifindex < 0) {
3158 if (pktinfo->ipi6_ifindex) {
3159 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3165 * We store the address anyway, and let in6_selectsrc()
3166 * validate the specified address. This is because ipi6_addr
3167 * may not have enough information about its scope zone, and
3168 * we may need additional information (such as outgoing
3169 * interface or the scope zone of a destination address) to
3170 * disambiguate the scope.
3171 * XXX: the delay of the validation may confuse the
3172 * application when it is used as a sticky option.
3174 if (opt->ip6po_pktinfo == NULL) {
3175 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3176 M_IP6OPT, M_NOWAIT);
3177 if (opt->ip6po_pktinfo == NULL)
3180 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3184 case IPV6_2292HOPLIMIT:
3190 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3191 * to simplify the ordering among hoplimit options.
3193 if (optname == IPV6_HOPLIMIT && sticky)
3194 return (ENOPROTOOPT);
3196 if (len != sizeof(int))
3199 if (*hlimp < -1 || *hlimp > 255)
3202 opt->ip6po_hlim = *hlimp;
3210 if (len != sizeof(int))
3212 tclass = *(int *)buf;
3213 if (tclass < -1 || tclass > 255)
3216 opt->ip6po_tclass = tclass;
3220 case IPV6_2292NEXTHOP:
3225 if (len == 0) { /* just remove the option */
3226 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3230 /* check if cmsg_len is large enough for sa_len */
3231 if (len < sizeof(struct sockaddr) || len < *buf)
3234 switch (((struct sockaddr *)buf)->sa_family) {
3237 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3242 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3245 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3246 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3250 if ((error = scope6_check_id(sa6, ip6_use_defzone))
3255 sa6->sin6_scope_id = 0; /* XXX */
3258 case AF_LINK: /* should eventually be supported */
3260 return (EAFNOSUPPORT);
3263 /* turn off the previous option, then set the new option. */
3264 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3265 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_WAITOK);
3266 bcopy(buf, opt->ip6po_nexthop, *buf);
3269 case IPV6_2292HOPOPTS:
3272 struct ip6_hbh *hbh;
3276 * XXX: We don't allow a non-privileged user to set ANY HbH
3277 * options, since per-option restriction has too much
3284 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3285 break; /* just remove the option */
3288 /* message length validation */
3289 if (len < sizeof(struct ip6_hbh))
3291 hbh = (struct ip6_hbh *)buf;
3292 hbhlen = (hbh->ip6h_len + 1) << 3;
3296 /* turn off the previous option, then set the new option. */
3297 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3298 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_WAITOK);
3299 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3304 case IPV6_2292DSTOPTS:
3306 case IPV6_RTHDRDSTOPTS:
3308 struct ip6_dest *dest, **newdest = NULL;
3311 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3315 ip6_clearpktopts(opt, optname);
3316 break; /* just remove the option */
3319 /* message length validation */
3320 if (len < sizeof(struct ip6_dest))
3322 dest = (struct ip6_dest *)buf;
3323 destlen = (dest->ip6d_len + 1) << 3;
3328 * Determine the position that the destination options header
3329 * should be inserted; before or after the routing header.
3332 case IPV6_2292DSTOPTS:
3334 * The old advacned API is ambiguous on this point.
3335 * Our approach is to determine the position based
3336 * according to the existence of a routing header.
3337 * Note, however, that this depends on the order of the
3338 * extension headers in the ancillary data; the 1st
3339 * part of the destination options header must appear
3340 * before the routing header in the ancillary data,
3342 * RFC3542 solved the ambiguity by introducing
3343 * separate ancillary data or option types.
3345 if (opt->ip6po_rthdr == NULL)
3346 newdest = &opt->ip6po_dest1;
3348 newdest = &opt->ip6po_dest2;
3350 case IPV6_RTHDRDSTOPTS:
3351 newdest = &opt->ip6po_dest1;
3354 newdest = &opt->ip6po_dest2;
3358 /* turn off the previous option, then set the new option. */
3359 ip6_clearpktopts(opt, optname);
3360 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
3361 bcopy(dest, *newdest, destlen);
3366 case IPV6_2292RTHDR:
3369 struct ip6_rthdr *rth;
3373 ip6_clearpktopts(opt, IPV6_RTHDR);
3374 break; /* just remove the option */
3377 /* message length validation */
3378 if (len < sizeof(struct ip6_rthdr))
3380 rth = (struct ip6_rthdr *)buf;
3381 rthlen = (rth->ip6r_len + 1) << 3;
3385 switch (rth->ip6r_type) {
3386 case IPV6_RTHDR_TYPE_0:
3387 if (rth->ip6r_len == 0) /* must contain one addr */
3389 if (rth->ip6r_len % 2) /* length must be even */
3391 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3395 return (EINVAL); /* not supported */
3398 /* turn off the previous option */
3399 ip6_clearpktopts(opt, IPV6_RTHDR);
3400 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_WAITOK);
3401 bcopy(rth, opt->ip6po_rthdr, rthlen);
3406 case IPV6_USE_MIN_MTU:
3407 if (len != sizeof(int))
3409 minmtupolicy = *(int *)buf;
3410 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3411 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3412 minmtupolicy != IP6PO_MINMTU_ALL) {
3415 opt->ip6po_minmtu = minmtupolicy;
3419 if (len != sizeof(int))
3422 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3424 * we ignore this option for TCP sockets.
3425 * (RFC3542 leaves this case unspecified.)
3427 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3429 opt->ip6po_flags |= IP6PO_DONTFRAG;
3432 case IPV6_PREFER_TEMPADDR:
3433 if (len != sizeof(int))
3435 preftemp = *(int *)buf;
3436 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3437 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3438 preftemp != IP6PO_TEMPADDR_PREFER) {
3441 opt->ip6po_prefer_tempaddr = preftemp;
3445 return (ENOPROTOOPT);
3446 } /* end of switch */
3452 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3453 * packet to the input queue of a specified interface. Note that this
3454 * calls the output routine of the loopback "driver", but with an interface
3455 * pointer that might NOT be &loif -- easier than replicating that code here.
3458 ip6_mloopback(ifp, m, dst)
3461 struct sockaddr_in6 *dst;
3464 struct ip6_hdr *ip6;
3466 copym = m_copy(m, 0, M_COPYALL);
3471 * Make sure to deep-copy IPv6 header portion in case the data
3472 * is in an mbuf cluster, so that we can safely override the IPv6
3473 * header portion later.
3475 if ((copym->m_flags & M_EXT) != 0 ||
3476 copym->m_len < sizeof(struct ip6_hdr)) {
3477 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3483 if (copym->m_len < sizeof(*ip6)) {
3489 ip6 = mtod(copym, struct ip6_hdr *);
3491 * clear embedded scope identifiers if necessary.
3492 * in6_clearscope will touch the addresses only when necessary.
3494 in6_clearscope(&ip6->ip6_src);
3495 in6_clearscope(&ip6->ip6_dst);
3497 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3501 * Chop IPv6 header off from the payload.
3504 ip6_splithdr(m, exthdrs)
3506 struct ip6_exthdrs *exthdrs;
3509 struct ip6_hdr *ip6;
3511 ip6 = mtod(m, struct ip6_hdr *);
3512 if (m->m_len > sizeof(*ip6)) {
3513 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3518 M_MOVE_PKTHDR(mh, m);
3519 MH_ALIGN(mh, sizeof(*ip6));
3520 m->m_len -= sizeof(*ip6);
3521 m->m_data += sizeof(*ip6);
3524 m->m_len = sizeof(*ip6);
3525 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3527 exthdrs->ip6e_ip6 = m;
3532 * Compute IPv6 extension header length.
3536 struct in6pcb *in6p;
3540 if (!in6p->in6p_outputopts)
3545 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3547 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3548 if (in6p->in6p_outputopts->ip6po_rthdr)
3549 /* dest1 is valid with rthdr only */
3550 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3551 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3552 len += elen(in6p->in6p_outputopts->ip6po_dest2);