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_setpktoption __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 *));
145 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
146 * header (with pri, len, nxt, hlim, src, dst).
147 * This function may modify ver and hlim only.
148 * The mbuf chain containing the packet will be freed.
149 * The mbuf opt, if present, will not be freed.
151 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
152 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
153 * which is rt_rmx.rmx_mtu.
156 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
158 struct ip6_pktopts *opt;
159 struct route_in6 *ro;
161 struct ip6_moptions *im6o;
162 struct ifnet **ifpp; /* XXX: just for statistics */
165 struct ip6_hdr *ip6, *mhip6;
166 struct ifnet *ifp, *origifp;
168 int hlen, tlen, len, off;
169 struct route_in6 ip6route;
170 struct sockaddr_in6 *dst;
171 struct in6_addr odst;
173 struct in6_ifaddr *ia = NULL;
175 int alwaysfrag, dontfrag;
176 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
177 struct ip6_exthdrs exthdrs;
178 struct in6_addr finaldst;
179 struct route_in6 *ro_pmtu = NULL;
182 #if defined(IPSEC) || defined(FAST_IPSEC)
183 int needipsectun = 0;
184 struct secpolicy *sp = NULL;
185 #endif /*IPSEC || FAST_IPSEC*/
187 ip6 = mtod(m, struct ip6_hdr *);
188 finaldst = ip6->ip6_dst;
190 #define MAKE_EXTHDR(hp, mp) \
193 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
194 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
195 ((eh)->ip6e_len + 1) << 3); \
199 } while (/*CONSTCOND*/ 0)
201 bzero(&exthdrs, sizeof(exthdrs));
204 /* Hop-by-Hop options header */
205 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
206 /* Destination options header(1st part) */
207 if (opt->ip6po_rthdr) {
209 * Destination options header(1st part)
210 * This only makes sence with a routing header.
211 * See Section 9.2 of RFC 3542.
212 * Disabling this part just for MIP6 convenience is
213 * a bad idea. We need to think carefully about a
214 * way to make the advanced API coexist with MIP6
215 * options, which might automatically be inserted in
218 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
221 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
222 /* Destination options header(2nd part) */
223 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
227 /* get a security policy for this packet */
229 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
231 sp = ipsec6_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
234 ipsec6stat.out_inval++;
241 switch (sp->policy) {
242 case IPSEC_POLICY_DISCARD:
244 * This packet is just discarded.
246 ipsec6stat.out_polvio++;
249 case IPSEC_POLICY_BYPASS:
250 case IPSEC_POLICY_NONE:
251 /* no need to do IPsec. */
255 case IPSEC_POLICY_IPSEC:
256 if (sp->req == NULL) {
257 /* acquire a policy */
258 error = key_spdacquire(sp);
264 case IPSEC_POLICY_ENTRUST:
266 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
270 /* get a security policy for this packet */
272 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
274 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
277 newipsecstat.ips_out_inval++;
284 switch (sp->policy) {
285 case IPSEC_POLICY_DISCARD:
287 * This packet is just discarded.
289 newipsecstat.ips_out_polvio++;
292 case IPSEC_POLICY_BYPASS:
293 case IPSEC_POLICY_NONE:
294 /* no need to do IPsec. */
298 case IPSEC_POLICY_IPSEC:
299 if (sp->req == NULL) {
300 /* acquire a policy */
301 error = key_spdacquire(sp);
307 case IPSEC_POLICY_ENTRUST:
309 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
311 #endif /* FAST_IPSEC */
314 * Calculate the total length of the extension header chain.
315 * Keep the length of the unfragmentable part for fragmentation.
318 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
319 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
320 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
321 unfragpartlen = optlen + sizeof(struct ip6_hdr);
322 /* NOTE: we don't add AH/ESP length here. do that later. */
323 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
326 * If we need IPsec, or there is at least one extension header,
327 * separate IP6 header from the payload.
329 if ((needipsec || optlen) && !hdrsplit) {
330 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
334 m = exthdrs.ip6e_ip6;
339 ip6 = mtod(m, struct ip6_hdr *);
341 /* adjust mbuf packet header length */
342 m->m_pkthdr.len += optlen;
343 plen = m->m_pkthdr.len - sizeof(*ip6);
345 /* If this is a jumbo payload, insert a jumbo payload option. */
346 if (plen > IPV6_MAXPACKET) {
348 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
352 m = exthdrs.ip6e_ip6;
356 ip6 = mtod(m, struct ip6_hdr *);
357 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
361 ip6->ip6_plen = htons(plen);
364 * Concatenate headers and fill in next header fields.
365 * Here we have, on "m"
367 * and we insert headers accordingly. Finally, we should be getting:
368 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
370 * during the header composing process, "m" points to IPv6 header.
371 * "mprev" points to an extension header prior to esp.
374 u_char *nexthdrp = &ip6->ip6_nxt;
375 struct mbuf *mprev = m;
378 * we treat dest2 specially. this makes IPsec processing
379 * much easier. the goal here is to make mprev point the
380 * mbuf prior to dest2.
382 * result: IPv6 dest2 payload
383 * m and mprev will point to IPv6 header.
385 if (exthdrs.ip6e_dest2) {
387 panic("assumption failed: hdr not split");
388 exthdrs.ip6e_dest2->m_next = m->m_next;
389 m->m_next = exthdrs.ip6e_dest2;
390 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
391 ip6->ip6_nxt = IPPROTO_DSTOPTS;
394 #define MAKE_CHAIN(m, mp, p, i)\
398 panic("assumption failed: hdr not split"); \
399 *mtod((m), u_char *) = *(p);\
401 p = mtod((m), u_char *);\
402 (m)->m_next = (mp)->m_next;\
406 } while (/*CONSTCOND*/ 0)
408 * result: IPv6 hbh dest1 rthdr dest2 payload
409 * m will point to IPv6 header. mprev will point to the
410 * extension header prior to dest2 (rthdr in the above case).
412 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
413 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
415 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
418 #if defined(IPSEC) || defined(FAST_IPSEC)
423 * pointers after IPsec headers are not valid any more.
424 * other pointers need a great care too.
425 * (IPsec routines should not mangle mbufs prior to AH/ESP)
427 exthdrs.ip6e_dest2 = NULL;
430 struct ip6_rthdr *rh = NULL;
432 struct ipsec_output_state state;
434 if (exthdrs.ip6e_rthdr) {
435 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
436 segleft_org = rh->ip6r_segleft;
437 rh->ip6r_segleft = 0;
440 bzero(&state, sizeof(state));
442 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
446 /* mbuf is already reclaimed in ipsec6_output_trans. */
456 printf("ip6_output (ipsec): error code %d\n", error);
459 /* don't show these error codes to the user */
465 if (exthdrs.ip6e_rthdr) {
466 /* ah6_output doesn't modify mbuf chain */
467 rh->ip6r_segleft = segleft_org;
475 * If there is a routing header, replace the destination address field
476 * with the first hop of the routing header.
478 if (exthdrs.ip6e_rthdr) {
479 struct ip6_rthdr *rh =
480 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
481 struct ip6_rthdr *));
482 struct ip6_rthdr0 *rh0;
483 struct in6_addr *addrs;
485 switch (rh->ip6r_type) {
486 case IPV6_RTHDR_TYPE_0:
487 rh0 = (struct ip6_rthdr0 *)rh;
488 addrs = (struct in6_addr *)(rh0 + 1);
490 ip6->ip6_dst = *addrs;
491 bcopy((caddr_t)(addrs + 1), (caddr_t)addrs,
492 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
494 *(addrs + rh0->ip6r0_segleft - 1) = finaldst;
496 default: /* is it possible? */
502 /* Source address validation */
503 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
504 (flags & IPV6_DADOUTPUT) == 0) {
506 ip6stat.ip6s_badscope++;
509 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
511 ip6stat.ip6s_badscope++;
515 ip6stat.ip6s_localout++;
522 bzero((caddr_t)ro, sizeof(*ro));
525 if (opt && opt->ip6po_rthdr)
526 ro = &opt->ip6po_route;
527 dst = (struct sockaddr_in6 *)&ro->ro_dst;
531 * If there is a cached route,
532 * check that it is to the same destination
533 * and is still up. If not, free it and try again.
535 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
536 dst->sin6_family != AF_INET6 ||
537 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
539 ro->ro_rt = (struct rtentry *)0;
541 if (ro->ro_rt == 0) {
542 bzero(dst, sizeof(*dst));
543 dst->sin6_family = AF_INET6;
544 dst->sin6_len = sizeof(struct sockaddr_in6);
545 dst->sin6_addr = ip6->ip6_dst;
549 * if specified, try to fill in the traffic class field.
550 * do not override if a non-zero value is already set.
551 * we check the diffserv field and the ecn field separately.
553 if (opt && opt->ip6po_tclass >= 0) {
556 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
558 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
561 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
564 /* fill in or override the hop limit field, if necessary. */
565 if (opt && opt->ip6po_hlim != -1)
566 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
567 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
569 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
571 ip6->ip6_hlim = ip6_defmcasthlim;
574 #if defined(IPSEC) || defined(FAST_IPSEC)
575 if (needipsec && needipsectun) {
576 struct ipsec_output_state state;
579 * All the extension headers will become inaccessible
580 * (since they can be encrypted).
581 * Don't panic, we need no more updates to extension headers
582 * on inner IPv6 packet (since they are now encapsulated).
584 * IPv6 [ESP|AH] IPv6 [extension headers] payload
586 bzero(&exthdrs, sizeof(exthdrs));
587 exthdrs.ip6e_ip6 = m;
589 bzero(&state, sizeof(state));
591 state.ro = (struct route *)ro;
592 state.dst = (struct sockaddr *)dst;
594 error = ipsec6_output_tunnel(&state, sp, flags);
597 ro = (struct route_in6 *)state.ro;
598 dst = (struct sockaddr_in6 *)state.dst;
600 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
611 printf("ip6_output (ipsec): error code %d\n", error);
614 /* don't show these error codes to the user */
621 exthdrs.ip6e_ip6 = m;
625 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
628 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
629 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
631 * interface selection comes here
632 * if an interface is specified from an upper layer,
635 if (ro->ro_rt == 0) {
637 * non-bsdi always clone routes, if parent is
640 rtalloc((struct route *)ro);
642 if (ro->ro_rt == 0) {
643 ip6stat.ip6s_noroute++;
644 error = EHOSTUNREACH;
645 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
648 /* XXX rt not locked */
649 ia = ifatoia6(ro->ro_rt->rt_ifa);
650 ifp = ro->ro_rt->rt_ifp;
651 ro->ro_rt->rt_rmx.rmx_pksent++;
652 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
653 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
654 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
656 in6_ifstat_inc(ifp, ifs6_out_request);
659 * Check if the outgoing interface conflicts with
660 * the interface specified by ifi6_ifindex (if specified).
661 * Note that loopback interface is always okay.
662 * (this may happen when we are sending a packet to one of
663 * our own addresses.)
665 if (opt && opt->ip6po_pktinfo
666 && opt->ip6po_pktinfo->ipi6_ifindex) {
667 if (!(ifp->if_flags & IFF_LOOPBACK)
668 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
669 ip6stat.ip6s_noroute++;
670 in6_ifstat_inc(ifp, ifs6_out_discard);
671 error = EHOSTUNREACH;
676 if (opt && opt->ip6po_hlim != -1)
677 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
680 struct in6_multi *in6m;
682 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
685 * See if the caller provided any multicast options
689 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
690 if (im6o->im6o_multicast_ifp != NULL)
691 ifp = im6o->im6o_multicast_ifp;
693 ip6->ip6_hlim = ip6_defmcasthlim;
696 * See if the caller provided the outgoing interface
697 * as an ancillary data.
698 * Boundary check for ifindex is assumed to be already done.
700 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
701 ifp = ifnet_byindex(opt->ip6po_pktinfo->ipi6_ifindex);
704 * If the destination is a node-local scope multicast,
705 * the packet should be loop-backed only.
707 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
709 * If the outgoing interface is already specified,
710 * it should be a loopback interface.
712 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
713 ip6stat.ip6s_badscope++;
714 error = ENETUNREACH; /* XXX: better error? */
715 /* XXX correct ifp? */
716 in6_ifstat_inc(ifp, ifs6_out_discard);
723 if (opt && opt->ip6po_hlim != -1)
724 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
727 * If caller did not provide an interface lookup a
728 * default in the routing table. This is either a
729 * default for the speicfied group (i.e. a host
730 * route), or a multicast default (a route for the
735 ro->ro_rt = rtalloc1((struct sockaddr *)
736 &ro->ro_dst, 0, 0UL);
739 if (ro->ro_rt == 0) {
740 ip6stat.ip6s_noroute++;
741 error = EHOSTUNREACH;
742 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
745 ia = ifatoia6(ro->ro_rt->rt_ifa);
746 ifp = ro->ro_rt->rt_ifp;
747 ro->ro_rt->rt_rmx.rmx_pksent++;
748 RT_UNLOCK(ro->ro_rt);
751 if ((flags & IPV6_FORWARDING) == 0)
752 in6_ifstat_inc(ifp, ifs6_out_request);
753 in6_ifstat_inc(ifp, ifs6_out_mcast);
756 * Confirm that the outgoing interface supports multicast.
758 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
759 ip6stat.ip6s_noroute++;
760 in6_ifstat_inc(ifp, ifs6_out_discard);
764 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
766 (im6o == NULL || im6o->im6o_multicast_loop)) {
768 * If we belong to the destination multicast group
769 * on the outgoing interface, and the caller did not
770 * forbid loopback, loop back a copy.
772 ip6_mloopback(ifp, m, dst);
775 * If we are acting as a multicast router, perform
776 * multicast forwarding as if the packet had just
777 * arrived on the interface to which we are about
778 * to send. The multicast forwarding function
779 * recursively calls this function, using the
780 * IPV6_FORWARDING flag to prevent infinite recursion.
782 * Multicasts that are looped back by ip6_mloopback(),
783 * above, will be forwarded by the ip6_input() routine,
786 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
787 if (ip6_mforward(ip6, ifp, m) != 0) {
794 * Multicasts with a hoplimit of zero may be looped back,
795 * above, but must not be transmitted on a network.
796 * Also, multicasts addressed to the loopback interface
797 * are not sent -- the above call to ip6_mloopback() will
798 * loop back a copy if this host actually belongs to the
799 * destination group on the loopback interface.
801 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
802 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
809 * Fill the outgoing inteface to tell the upper layer
810 * to increment per-interface statistics.
815 /* Determine path MTU. */
816 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
821 * The caller of this function may specify to use the minimum MTU
823 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
824 * setting. The logic is a bit complicated; by default, unicast
825 * packets will follow path MTU while multicast packets will be sent at
826 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
827 * including unicast ones will be sent at the minimum MTU. Multicast
828 * packets will always be sent at the minimum MTU unless
829 * IP6PO_MINMTU_DISABLE is explicitly specified.
830 * See RFC 3542 for more details.
832 if (mtu > IPV6_MMTU) {
833 if ((flags & IPV6_MINMTU))
835 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
837 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
839 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
844 /* Fake scoped addresses */
845 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
847 * If source or destination address is a scoped address, and
848 * the packet is going to be sent to a loopback interface,
849 * we should keep the original interface.
853 * XXX: this is a very experimental and temporary solution.
854 * We eventually have sockaddr_in6 and use the sin6_scope_id
855 * field of the structure here.
856 * We rely on the consistency between two scope zone ids
857 * of source and destination, which should already be assured.
858 * Larger scopes than link will be supported in the future.
861 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
862 origifp = ifnet_byindex(ntohs(ip6->ip6_src.s6_addr16[1]));
863 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
864 origifp = ifnet_byindex(ntohs(ip6->ip6_dst.s6_addr16[1]));
866 * XXX: origifp can be NULL even in those two cases above.
867 * For example, if we remove the (only) link-local address
868 * from the loopback interface, and try to send a link-local
869 * address without link-id information. Then the source
870 * address is ::1, and the destination address is the
871 * link-local address with its s6_addr16[1] being zero.
872 * What is worse, if the packet goes to the loopback interface
873 * by a default rejected route, the null pointer would be
874 * passed to looutput, and the kernel would hang.
875 * The following last resort would prevent such disaster.
883 * clear embedded scope identifiers if necessary.
884 * in6_clearscope will touch the addresses only when necessary.
886 in6_clearscope(&ip6->ip6_src);
887 in6_clearscope(&ip6->ip6_dst);
890 * Check with the firewall...
892 if (ip6_fw_enable && ip6_fw_chk_ptr) {
894 m->m_pkthdr.rcvif = NULL; /* XXX */
895 /* If ipfw says divert, we have to just drop packet */
896 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
907 * If the outgoing packet contains a hop-by-hop options header,
908 * it must be examined and processed even by the source node.
909 * (RFC 2460, section 4.)
911 if (exthdrs.ip6e_hbh) {
912 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
913 u_int32_t dummy; /* XXX unused */
914 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
917 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
918 panic("ip6e_hbh is not continuous");
921 * XXX: if we have to send an ICMPv6 error to the sender,
922 * we need the M_LOOP flag since icmp6_error() expects
923 * the IPv6 and the hop-by-hop options header are
924 * continuous unless the flag is set.
926 m->m_flags |= M_LOOP;
927 m->m_pkthdr.rcvif = ifp;
928 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
929 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
930 &dummy, &plen) < 0) {
931 /* m was already freed at this point */
932 error = EINVAL;/* better error? */
935 m->m_flags &= ~M_LOOP; /* XXX */
936 m->m_pkthdr.rcvif = NULL;
939 /* Jump over all PFIL processing if hooks are not active. */
940 if (inet6_pfil_hook.ph_busy_count == -1)
944 /* Run through list of hooks for output packets. */
945 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
946 if (error != 0 || m == NULL)
948 ip6 = mtod(m, struct ip6_hdr *);
950 /* See if destination IP address was changed by packet filter. */
951 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
952 m->m_flags |= M_SKIP_FIREWALL;
953 /* If destination is now ourself drop to ip6_input(). */
954 if (in6_localaddr(&ip6->ip6_dst)) {
955 if (m->m_pkthdr.rcvif == NULL)
956 m->m_pkthdr.rcvif = loif;
957 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
958 m->m_pkthdr.csum_flags |=
959 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
960 m->m_pkthdr.csum_data = 0xffff;
962 m->m_pkthdr.csum_flags |=
963 CSUM_IP_CHECKED | CSUM_IP_VALID;
964 error = netisr_queue(NETISR_IPV6, m);
967 goto again; /* Redo the routing table lookup. */
970 /* XXX: IPFIREWALL_FORWARD */
974 * Send the packet to the outgoing interface.
975 * If necessary, do IPv6 fragmentation before sending.
977 * the logic here is rather complex:
978 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
979 * 1-a: send as is if tlen <= path mtu
980 * 1-b: fragment if tlen > path mtu
982 * 2: if user asks us not to fragment (dontfrag == 1)
983 * 2-a: send as is if tlen <= interface mtu
984 * 2-b: error if tlen > interface mtu
986 * 3: if we always need to attach fragment header (alwaysfrag == 1)
989 * 4: if dontfrag == 1 && alwaysfrag == 1
990 * error, as we cannot handle this conflicting request
992 tlen = m->m_pkthdr.len;
994 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
998 if (dontfrag && alwaysfrag) { /* case 4 */
999 /* conflicting request - can't transmit */
1003 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
1005 * Even if the DONTFRAG option is specified, we cannot send the
1006 * packet when the data length is larger than the MTU of the
1007 * outgoing interface.
1008 * Notify the error by sending IPV6_PATHMTU ancillary data as
1009 * well as returning an error code (the latter is not described
1013 struct ip6ctlparam ip6cp;
1015 mtu32 = (u_int32_t)mtu;
1016 bzero(&ip6cp, sizeof(ip6cp));
1017 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1018 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1026 * transmit packet without fragmentation
1028 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1029 struct in6_ifaddr *ia6;
1031 ip6 = mtod(m, struct ip6_hdr *);
1032 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1034 /* Record statistics for this interface address. */
1035 ia6->ia_ifa.if_opackets++;
1036 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
1039 /* clean ipsec history once it goes out of the node */
1042 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1047 * try to fragment the packet. case 1-b and 3
1049 if (mtu < IPV6_MMTU) {
1050 /* path MTU cannot be less than IPV6_MMTU */
1052 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1054 } else if (ip6->ip6_plen == 0) {
1055 /* jumbo payload cannot be fragmented */
1057 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1060 struct mbuf **mnext, *m_frgpart;
1061 struct ip6_frag *ip6f;
1062 u_int32_t id = htonl(ip6_randomid());
1065 struct ip6ctlparam ip6cp;
1068 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1071 * Too large for the destination or interface;
1072 * fragment if possible.
1073 * Must be able to put at least 8 bytes per fragment.
1075 hlen = unfragpartlen;
1076 if (mtu > IPV6_MAXPACKET)
1077 mtu = IPV6_MAXPACKET;
1081 * It is believed this code is a leftover from the
1082 * development of the IPV6_RECVPATHMTU sockopt and
1083 * associated work to implement RFC3542.
1084 * It's not entirely clear what the intent of the API
1085 * is at this point, so disable this code for now.
1086 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1087 * will send notifications if the application requests.
1090 /* Notify a proper path MTU to applications. */
1091 mtu32 = (u_int32_t)mtu;
1092 bzero(&ip6cp, sizeof(ip6cp));
1093 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1094 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1098 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1101 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1106 * Verify that we have any chance at all of being able to queue
1107 * the packet or packet fragments
1109 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1110 < tlen /* - hlen */)) {
1112 ip6stat.ip6s_odropped++;
1116 mnext = &m->m_nextpkt;
1119 * Change the next header field of the last header in the
1120 * unfragmentable part.
1122 if (exthdrs.ip6e_rthdr) {
1123 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1124 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1125 } else if (exthdrs.ip6e_dest1) {
1126 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1127 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1128 } else if (exthdrs.ip6e_hbh) {
1129 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1130 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1132 nextproto = ip6->ip6_nxt;
1133 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1137 * Loop through length of segment after first fragment,
1138 * make new header and copy data of each part and link onto
1142 for (off = hlen; off < tlen; off += len) {
1143 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1146 ip6stat.ip6s_odropped++;
1149 m->m_pkthdr.rcvif = NULL;
1150 m->m_flags = m0->m_flags & M_COPYFLAGS;
1152 mnext = &m->m_nextpkt;
1153 m->m_data += max_linkhdr;
1154 mhip6 = mtod(m, struct ip6_hdr *);
1156 m->m_len = sizeof(*mhip6);
1157 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1159 ip6stat.ip6s_odropped++;
1162 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1163 if (off + len >= tlen)
1166 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1167 mhip6->ip6_plen = htons((u_short)(len + hlen +
1168 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1169 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1171 ip6stat.ip6s_odropped++;
1174 m_cat(m, m_frgpart);
1175 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1176 m->m_pkthdr.rcvif = (struct ifnet *)0;
1177 ip6f->ip6f_reserved = 0;
1178 ip6f->ip6f_ident = id;
1179 ip6f->ip6f_nxt = nextproto;
1180 ip6stat.ip6s_ofragments++;
1181 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1184 in6_ifstat_inc(ifp, ifs6_out_fragok);
1188 * Remove leading garbages.
1194 for (m0 = m; m; m = m0) {
1198 /* Record statistics for this interface address. */
1200 ia->ia_ifa.if_opackets++;
1201 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1204 /* clean ipsec history once it goes out of the node */
1207 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1213 ip6stat.ip6s_fragmented++;
1216 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1218 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1219 RTFREE(ro_pmtu->ro_rt);
1229 #endif /* FAST_IPSEC */
1234 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1235 m_freem(exthdrs.ip6e_dest1);
1236 m_freem(exthdrs.ip6e_rthdr);
1237 m_freem(exthdrs.ip6e_dest2);
1245 ip6_copyexthdr(mp, hdr, hlen)
1252 if (hlen > MCLBYTES)
1253 return (ENOBUFS); /* XXX */
1255 MGET(m, M_DONTWAIT, MT_DATA);
1260 MCLGET(m, M_DONTWAIT);
1261 if ((m->m_flags & M_EXT) == 0) {
1268 bcopy(hdr, mtod(m, caddr_t), hlen);
1275 * Insert jumbo payload option.
1278 ip6_insert_jumboopt(exthdrs, plen)
1279 struct ip6_exthdrs *exthdrs;
1286 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1289 * If there is no hop-by-hop options header, allocate new one.
1290 * If there is one but it doesn't have enough space to store the
1291 * jumbo payload option, allocate a cluster to store the whole options.
1292 * Otherwise, use it to store the options.
1294 if (exthdrs->ip6e_hbh == 0) {
1295 MGET(mopt, M_DONTWAIT, MT_DATA);
1298 mopt->m_len = JUMBOOPTLEN;
1299 optbuf = mtod(mopt, u_char *);
1300 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1301 exthdrs->ip6e_hbh = mopt;
1303 struct ip6_hbh *hbh;
1305 mopt = exthdrs->ip6e_hbh;
1306 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1309 * - exthdrs->ip6e_hbh is not referenced from places
1310 * other than exthdrs.
1311 * - exthdrs->ip6e_hbh is not an mbuf chain.
1313 int oldoptlen = mopt->m_len;
1317 * XXX: give up if the whole (new) hbh header does
1318 * not fit even in an mbuf cluster.
1320 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1324 * As a consequence, we must always prepare a cluster
1327 MGET(n, M_DONTWAIT, MT_DATA);
1329 MCLGET(n, M_DONTWAIT);
1330 if ((n->m_flags & M_EXT) == 0) {
1337 n->m_len = oldoptlen + JUMBOOPTLEN;
1338 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1340 optbuf = mtod(n, caddr_t) + oldoptlen;
1342 mopt = exthdrs->ip6e_hbh = n;
1344 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1345 mopt->m_len += JUMBOOPTLEN;
1347 optbuf[0] = IP6OPT_PADN;
1351 * Adjust the header length according to the pad and
1352 * the jumbo payload option.
1354 hbh = mtod(mopt, struct ip6_hbh *);
1355 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1358 /* fill in the option. */
1359 optbuf[2] = IP6OPT_JUMBO;
1361 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1362 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1364 /* finally, adjust the packet header length */
1365 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1372 * Insert fragment header and copy unfragmentable header portions.
1375 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1376 struct mbuf *m0, *m;
1378 struct ip6_frag **frghdrp;
1380 struct mbuf *n, *mlast;
1382 if (hlen > sizeof(struct ip6_hdr)) {
1383 n = m_copym(m0, sizeof(struct ip6_hdr),
1384 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1391 /* Search for the last mbuf of unfragmentable part. */
1392 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1395 if ((mlast->m_flags & M_EXT) == 0 &&
1396 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1397 /* use the trailing space of the last mbuf for the fragment hdr */
1398 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1400 mlast->m_len += sizeof(struct ip6_frag);
1401 m->m_pkthdr.len += sizeof(struct ip6_frag);
1403 /* allocate a new mbuf for the fragment header */
1406 MGET(mfrg, M_DONTWAIT, MT_DATA);
1409 mfrg->m_len = sizeof(struct ip6_frag);
1410 *frghdrp = mtod(mfrg, struct ip6_frag *);
1411 mlast->m_next = mfrg;
1418 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1419 struct route_in6 *ro_pmtu, *ro;
1421 struct in6_addr *dst;
1429 if (ro_pmtu != ro) {
1430 /* The first hop and the final destination may differ. */
1431 struct sockaddr_in6 *sa6_dst =
1432 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1433 if (ro_pmtu->ro_rt &&
1434 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1435 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1436 RTFREE(ro_pmtu->ro_rt);
1437 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1439 if (ro_pmtu->ro_rt == NULL) {
1440 bzero(sa6_dst, sizeof(*sa6_dst));
1441 sa6_dst->sin6_family = AF_INET6;
1442 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1443 sa6_dst->sin6_addr = *dst;
1445 rtalloc((struct route *)ro_pmtu);
1448 if (ro_pmtu->ro_rt) {
1450 struct in_conninfo inc;
1452 bzero(&inc, sizeof(inc));
1453 inc.inc_flags = 1; /* IPv6 */
1454 inc.inc6_faddr = *dst;
1457 ifp = ro_pmtu->ro_rt->rt_ifp;
1458 ifmtu = IN6_LINKMTU(ifp);
1459 mtu = tcp_hc_getmtu(&inc);
1461 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1463 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1466 else if (mtu < IPV6_MMTU) {
1468 * RFC2460 section 5, last paragraph:
1469 * if we record ICMPv6 too big message with
1470 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1471 * or smaller, with framgent header attached.
1472 * (fragment header is needed regardless from the
1473 * packet size, for translators to identify packets)
1477 } else if (mtu > ifmtu) {
1479 * The MTU on the route is larger than the MTU on
1480 * the interface! This shouldn't happen, unless the
1481 * MTU of the interface has been changed after the
1482 * interface was brought up. Change the MTU in the
1483 * route to match the interface MTU (as long as the
1484 * field isn't locked).
1487 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1490 mtu = IN6_LINKMTU(ifp);
1492 error = EHOSTUNREACH; /* XXX */
1496 *alwaysfragp = alwaysfrag;
1501 * IP6 socket option processing.
1504 ip6_ctloutput(so, sopt)
1506 struct sockopt *sopt;
1508 int privileged, optdatalen, uproto;
1510 struct inpcb *in6p = sotoinpcb(so);
1512 int level, op, optname;
1517 level = sopt->sopt_level;
1518 op = sopt->sopt_dir;
1519 optname = sopt->sopt_name;
1520 optlen = sopt->sopt_valsize;
1523 panic("ip6_ctloutput: arg soopt is NULL");
1527 privileged = (td == 0 || suser(td)) ? 0 : 1;
1528 uproto = (int)so->so_proto->pr_protocol;
1530 if (level == IPPROTO_IPV6) {
1535 case IPV6_2292PKTOPTIONS:
1536 #ifdef IPV6_PKTOPTIONS
1537 case IPV6_PKTOPTIONS:
1542 error = soopt_getm(sopt, &m); /* XXX */
1545 error = soopt_mcopyin(sopt, m); /* XXX */
1548 error = ip6_pcbopts(&in6p->in6p_outputopts,
1550 m_freem(m); /* XXX */
1555 * Use of some Hop-by-Hop options or some
1556 * Destination options, might require special
1557 * privilege. That is, normal applications
1558 * (without special privilege) might be forbidden
1559 * from setting certain options in outgoing packets,
1560 * and might never see certain options in received
1561 * packets. [RFC 2292 Section 6]
1562 * KAME specific note:
1563 * KAME prevents non-privileged users from sending or
1564 * receiving ANY hbh/dst options in order to avoid
1565 * overhead of parsing options in the kernel.
1567 case IPV6_RECVHOPOPTS:
1568 case IPV6_RECVDSTOPTS:
1569 case IPV6_RECVRTHDRDSTOPTS:
1575 case IPV6_UNICAST_HOPS:
1579 case IPV6_RECVPKTINFO:
1580 case IPV6_RECVHOPLIMIT:
1581 case IPV6_RECVRTHDR:
1582 case IPV6_RECVPATHMTU:
1583 case IPV6_RECVTCLASS:
1585 case IPV6_AUTOFLOWLABEL:
1586 if (optlen != sizeof(int)) {
1590 error = sooptcopyin(sopt, &optval,
1591 sizeof optval, sizeof optval);
1596 case IPV6_UNICAST_HOPS:
1597 if (optval < -1 || optval >= 256)
1600 /* -1 = kernel default */
1601 in6p->in6p_hops = optval;
1602 if ((in6p->in6p_vflag &
1604 in6p->inp_ip_ttl = optval;
1607 #define OPTSET(bit) \
1610 in6p->in6p_flags |= (bit); \
1612 in6p->in6p_flags &= ~(bit); \
1613 } while (/*CONSTCOND*/ 0)
1614 #define OPTSET2292(bit) \
1616 in6p->in6p_flags |= IN6P_RFC2292; \
1618 in6p->in6p_flags |= (bit); \
1620 in6p->in6p_flags &= ~(bit); \
1621 } while (/*CONSTCOND*/ 0)
1622 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1624 case IPV6_RECVPKTINFO:
1625 /* cannot mix with RFC2292 */
1626 if (OPTBIT(IN6P_RFC2292)) {
1630 OPTSET(IN6P_PKTINFO);
1635 struct ip6_pktopts **optp;
1637 /* cannot mix with RFC2292 */
1638 if (OPTBIT(IN6P_RFC2292)) {
1642 optp = &in6p->in6p_outputopts;
1643 error = ip6_pcbopt(IPV6_HOPLIMIT,
1647 privileged, uproto);
1651 case IPV6_RECVHOPLIMIT:
1652 /* cannot mix with RFC2292 */
1653 if (OPTBIT(IN6P_RFC2292)) {
1657 OPTSET(IN6P_HOPLIMIT);
1660 case IPV6_RECVHOPOPTS:
1661 /* cannot mix with RFC2292 */
1662 if (OPTBIT(IN6P_RFC2292)) {
1666 OPTSET(IN6P_HOPOPTS);
1669 case IPV6_RECVDSTOPTS:
1670 /* cannot mix with RFC2292 */
1671 if (OPTBIT(IN6P_RFC2292)) {
1675 OPTSET(IN6P_DSTOPTS);
1678 case IPV6_RECVRTHDRDSTOPTS:
1679 /* cannot mix with RFC2292 */
1680 if (OPTBIT(IN6P_RFC2292)) {
1684 OPTSET(IN6P_RTHDRDSTOPTS);
1687 case IPV6_RECVRTHDR:
1688 /* cannot mix with RFC2292 */
1689 if (OPTBIT(IN6P_RFC2292)) {
1700 case IPV6_RECVPATHMTU:
1702 * We ignore this option for TCP
1704 * (rfc2292bis leaves this case
1707 if (uproto != IPPROTO_TCP)
1713 * make setsockopt(IPV6_V6ONLY)
1714 * available only prior to bind(2).
1715 * see ipng mailing list, Jun 22 2001.
1717 if (in6p->in6p_lport ||
1718 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1722 OPTSET(IN6P_IPV6_V6ONLY);
1724 in6p->in6p_vflag &= ~INP_IPV4;
1726 in6p->in6p_vflag |= INP_IPV4;
1728 case IPV6_RECVTCLASS:
1729 /* cannot mix with RFC2292 XXX */
1730 if (OPTBIT(IN6P_RFC2292)) {
1734 OPTSET(IN6P_TCLASS);
1736 case IPV6_AUTOFLOWLABEL:
1737 OPTSET(IN6P_AUTOFLOWLABEL);
1745 case IPV6_USE_MIN_MTU:
1746 case IPV6_PREFER_TEMPADDR:
1747 if (optlen != sizeof(optval)) {
1751 error = sooptcopyin(sopt, &optval,
1752 sizeof optval, sizeof optval);
1756 struct ip6_pktopts **optp;
1757 optp = &in6p->in6p_outputopts;
1758 error = ip6_pcbopt(optname,
1762 privileged, uproto);
1766 case IPV6_2292PKTINFO:
1767 case IPV6_2292HOPLIMIT:
1768 case IPV6_2292HOPOPTS:
1769 case IPV6_2292DSTOPTS:
1770 case IPV6_2292RTHDR:
1772 if (optlen != sizeof(int)) {
1776 error = sooptcopyin(sopt, &optval,
1777 sizeof optval, sizeof optval);
1781 case IPV6_2292PKTINFO:
1782 OPTSET2292(IN6P_PKTINFO);
1784 case IPV6_2292HOPLIMIT:
1785 OPTSET2292(IN6P_HOPLIMIT);
1787 case IPV6_2292HOPOPTS:
1789 * Check super-user privilege.
1790 * See comments for IPV6_RECVHOPOPTS.
1794 OPTSET2292(IN6P_HOPOPTS);
1796 case IPV6_2292DSTOPTS:
1799 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1801 case IPV6_2292RTHDR:
1802 OPTSET2292(IN6P_RTHDR);
1810 case IPV6_RTHDRDSTOPTS:
1813 /* new advanced API (2292bis) */
1816 struct ip6_pktopts **optp;
1818 /* cannot mix with RFC2292 */
1819 if (OPTBIT(IN6P_RFC2292)) {
1827 case IPV6_RTHDRDSTOPTS:
1838 optlen = sizeof(struct in6_pktinfo);
1841 optlen = SOCK_MAXADDRLEN;
1844 optlen = IPV6_MAXOPTHDR;
1847 if (sopt->sopt_valsize > optlen) {
1852 optlen = sopt->sopt_valsize;
1853 optbuf = malloc(optlen, M_TEMP, M_WAITOK);
1854 error = sooptcopyin(sopt, optbuf, optlen,
1857 free(optbuf, M_TEMP);
1861 optp = &in6p->in6p_outputopts;
1862 error = ip6_pcbopt(optname,
1864 optp, privileged, uproto);
1865 free(optbuf, M_TEMP);
1870 case IPV6_MULTICAST_IF:
1871 case IPV6_MULTICAST_HOPS:
1872 case IPV6_MULTICAST_LOOP:
1873 case IPV6_JOIN_GROUP:
1874 case IPV6_LEAVE_GROUP:
1876 if (sopt->sopt_valsize > MLEN) {
1886 if (sopt->sopt_valsize > MCLBYTES) {
1891 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER);
1896 if (sopt->sopt_valsize > MLEN) {
1897 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1898 if ((m->m_flags & M_EXT) == 0) {
1904 m->m_len = sopt->sopt_valsize;
1905 error = sooptcopyin(sopt, mtod(m, char *),
1906 m->m_len, m->m_len);
1911 error = ip6_setmoptions(sopt->sopt_name,
1912 &in6p->in6p_moptions,
1918 case IPV6_PORTRANGE:
1919 error = sooptcopyin(sopt, &optval,
1920 sizeof optval, sizeof optval);
1925 case IPV6_PORTRANGE_DEFAULT:
1926 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1927 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1930 case IPV6_PORTRANGE_HIGH:
1931 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1932 in6p->in6p_flags |= IN6P_HIGHPORT;
1935 case IPV6_PORTRANGE_LOW:
1936 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1937 in6p->in6p_flags |= IN6P_LOWPORT;
1946 #if defined(IPSEC) || defined(FAST_IPSEC)
1947 case IPV6_IPSEC_POLICY:
1953 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1955 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1958 req = mtod(m, caddr_t);
1961 error = ipsec6_set_policy(in6p, optname, req,
1966 #endif /* KAME IPSEC */
1974 struct mbuf **mp = &m;
1976 if (ip6_fw_ctl_ptr == NULL)
1979 if ((error = soopt_getm(sopt, &m)) != 0)
1982 if ((error = soopt_mcopyin(sopt, m)) != 0)
1984 error = (*ip6_fw_ctl_ptr)(optname, mp);
1990 error = ENOPROTOOPT;
1998 case IPV6_2292PKTOPTIONS:
1999 #ifdef IPV6_PKTOPTIONS
2000 case IPV6_PKTOPTIONS:
2003 * RFC3542 (effectively) deprecated the
2004 * semantics of the 2292-style pktoptions.
2005 * Since it was not reliable in nature (i.e.,
2006 * applications had to expect the lack of some
2007 * information after all), it would make sense
2008 * to simplify this part by always returning
2011 sopt->sopt_valsize = 0;
2014 case IPV6_RECVHOPOPTS:
2015 case IPV6_RECVDSTOPTS:
2016 case IPV6_RECVRTHDRDSTOPTS:
2017 case IPV6_UNICAST_HOPS:
2018 case IPV6_RECVPKTINFO:
2019 case IPV6_RECVHOPLIMIT:
2020 case IPV6_RECVRTHDR:
2021 case IPV6_RECVPATHMTU:
2025 case IPV6_PORTRANGE:
2026 case IPV6_RECVTCLASS:
2027 case IPV6_AUTOFLOWLABEL:
2030 case IPV6_RECVHOPOPTS:
2031 optval = OPTBIT(IN6P_HOPOPTS);
2034 case IPV6_RECVDSTOPTS:
2035 optval = OPTBIT(IN6P_DSTOPTS);
2038 case IPV6_RECVRTHDRDSTOPTS:
2039 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2042 case IPV6_UNICAST_HOPS:
2043 optval = in6p->in6p_hops;
2046 case IPV6_RECVPKTINFO:
2047 optval = OPTBIT(IN6P_PKTINFO);
2050 case IPV6_RECVHOPLIMIT:
2051 optval = OPTBIT(IN6P_HOPLIMIT);
2054 case IPV6_RECVRTHDR:
2055 optval = OPTBIT(IN6P_RTHDR);
2058 case IPV6_RECVPATHMTU:
2059 optval = OPTBIT(IN6P_MTU);
2063 optval = OPTBIT(IN6P_FAITH);
2067 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2070 case IPV6_PORTRANGE:
2073 flags = in6p->in6p_flags;
2074 if (flags & IN6P_HIGHPORT)
2075 optval = IPV6_PORTRANGE_HIGH;
2076 else if (flags & IN6P_LOWPORT)
2077 optval = IPV6_PORTRANGE_LOW;
2082 case IPV6_RECVTCLASS:
2083 optval = OPTBIT(IN6P_TCLASS);
2086 case IPV6_AUTOFLOWLABEL:
2087 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2092 error = sooptcopyout(sopt, &optval,
2099 struct ip6_mtuinfo mtuinfo;
2100 struct route_in6 sro;
2102 bzero(&sro, sizeof(sro));
2104 if (!(so->so_state & SS_ISCONNECTED))
2107 * XXX: we dot not consider the case of source
2108 * routing, or optional information to specify
2109 * the outgoing interface.
2111 error = ip6_getpmtu(&sro, NULL, NULL,
2112 &in6p->in6p_faddr, &pmtu, NULL);
2117 if (pmtu > IPV6_MAXPACKET)
2118 pmtu = IPV6_MAXPACKET;
2120 bzero(&mtuinfo, sizeof(mtuinfo));
2121 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2122 optdata = (void *)&mtuinfo;
2123 optdatalen = sizeof(mtuinfo);
2124 error = sooptcopyout(sopt, optdata,
2129 case IPV6_2292PKTINFO:
2130 case IPV6_2292HOPLIMIT:
2131 case IPV6_2292HOPOPTS:
2132 case IPV6_2292RTHDR:
2133 case IPV6_2292DSTOPTS:
2135 case IPV6_2292PKTINFO:
2136 optval = OPTBIT(IN6P_PKTINFO);
2138 case IPV6_2292HOPLIMIT:
2139 optval = OPTBIT(IN6P_HOPLIMIT);
2141 case IPV6_2292HOPOPTS:
2142 optval = OPTBIT(IN6P_HOPOPTS);
2144 case IPV6_2292RTHDR:
2145 optval = OPTBIT(IN6P_RTHDR);
2147 case IPV6_2292DSTOPTS:
2148 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2151 error = sooptcopyout(sopt, &optval,
2158 case IPV6_RTHDRDSTOPTS:
2162 case IPV6_USE_MIN_MTU:
2163 case IPV6_PREFER_TEMPADDR:
2164 error = ip6_getpcbopt(in6p->in6p_outputopts,
2168 case IPV6_MULTICAST_IF:
2169 case IPV6_MULTICAST_HOPS:
2170 case IPV6_MULTICAST_LOOP:
2171 case IPV6_JOIN_GROUP:
2172 case IPV6_LEAVE_GROUP:
2175 error = ip6_getmoptions(sopt->sopt_name,
2176 in6p->in6p_moptions, &m);
2178 error = sooptcopyout(sopt,
2179 mtod(m, char *), m->m_len);
2184 #if defined(IPSEC) || defined(FAST_IPSEC)
2185 case IPV6_IPSEC_POLICY:
2189 struct mbuf *m = NULL;
2190 struct mbuf **mp = &m;
2191 size_t ovalsize = sopt->sopt_valsize;
2192 caddr_t oval = (caddr_t)sopt->sopt_val;
2194 error = soopt_getm(sopt, &m); /* XXX */
2197 error = soopt_mcopyin(sopt, m); /* XXX */
2200 sopt->sopt_valsize = ovalsize;
2201 sopt->sopt_val = oval;
2203 req = mtod(m, caddr_t);
2206 error = ipsec6_get_policy(in6p, req, len, mp);
2208 error = soopt_mcopyout(sopt, m); /* XXX */
2209 if (error == 0 && m)
2213 #endif /* KAME IPSEC */
2218 struct mbuf **mp = &m;
2220 if (ip6_fw_ctl_ptr == NULL)
2224 error = (*ip6_fw_ctl_ptr)(optname, mp);
2226 error = soopt_mcopyout(sopt, m); /* XXX */
2227 if (error == 0 && m)
2233 error = ENOPROTOOPT;
2238 } else { /* level != IPPROTO_IPV6 */
2245 ip6_raw_ctloutput(so, sopt)
2247 struct sockopt *sopt;
2249 int error = 0, optval, optlen;
2250 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2251 struct in6pcb *in6p = sotoin6pcb(so);
2252 int level, op, optname;
2255 level = sopt->sopt_level;
2256 op = sopt->sopt_dir;
2257 optname = sopt->sopt_name;
2258 optlen = sopt->sopt_valsize;
2260 panic("ip6_raw_ctloutput: arg soopt is NULL");
2262 if (level != IPPROTO_IPV6) {
2269 * For ICMPv6 sockets, no modification allowed for checksum
2270 * offset, permit "no change" values to help existing apps.
2272 * XXX 2292bis says: "An attempt to set IPV6_CHECKSUM
2273 * for an ICMPv6 socket will fail."
2274 * The current behavior does not meet 2292bis.
2278 if (optlen != sizeof(int)) {
2282 error = sooptcopyin(sopt, &optval, sizeof(optval),
2286 if ((optval % 2) != 0) {
2287 /* the API assumes even offset values */
2289 } else if (so->so_proto->pr_protocol ==
2291 if (optval != icmp6off)
2294 in6p->in6p_cksum = optval;
2298 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2301 optval = in6p->in6p_cksum;
2303 error = sooptcopyout(sopt, &optval, sizeof(optval));
2313 error = ENOPROTOOPT;
2321 * Set up IP6 options in pcb for insertion in output packets or
2322 * specifying behavior of outgoing packets.
2325 ip6_pcbopts(pktopt, m, so, sopt)
2326 struct ip6_pktopts **pktopt;
2329 struct sockopt *sopt;
2331 struct ip6_pktopts *opt = *pktopt;
2333 struct thread *td = sopt->sopt_td;
2336 /* turn off any old options. */
2339 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2340 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2341 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2342 printf("ip6_pcbopts: all specified options are cleared.\n");
2344 ip6_clearpktopts(opt, -1);
2346 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2349 if (!m || m->m_len == 0) {
2351 * Only turning off any previous options, regardless of
2352 * whether the opt is just created or given.
2354 free(opt, M_IP6OPT);
2358 /* set options specified by user. */
2359 if (td && !suser(td))
2361 if ((error = ip6_setpktoptions(m, opt, NULL, priv, 1,
2362 so->so_proto->pr_protocol)) != 0) {
2363 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2364 free(opt, M_IP6OPT);
2372 * initialize ip6_pktopts. beware that there are non-zero default values in
2376 init_ip6pktopts(opt)
2377 struct ip6_pktopts *opt;
2380 bzero(opt, sizeof(*opt));
2381 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2382 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2383 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2384 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2388 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2389 int optname, len, priv;
2391 struct ip6_pktopts **pktopt;
2394 struct ip6_pktopts *opt;
2396 if (*pktopt == NULL) {
2397 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2399 init_ip6pktopts(*pktopt);
2400 (*pktopt)->needfree = 1;
2404 return (ip6_setpktoption(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;
2518 needfree = pktopt->needfree;
2520 if (optname == -1 || optname == IPV6_PKTINFO) {
2521 if (needfree && pktopt->ip6po_pktinfo)
2522 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2523 pktopt->ip6po_pktinfo = NULL;
2525 if (optname == -1 || optname == IPV6_HOPLIMIT)
2526 pktopt->ip6po_hlim = -1;
2527 if (optname == -1 || optname == IPV6_TCLASS)
2528 pktopt->ip6po_tclass = -1;
2529 if (optname == -1 || optname == IPV6_NEXTHOP) {
2530 if (pktopt->ip6po_nextroute.ro_rt) {
2531 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2532 pktopt->ip6po_nextroute.ro_rt = NULL;
2534 if (needfree && pktopt->ip6po_nexthop)
2535 free(pktopt->ip6po_nexthop, M_IP6OPT);
2536 pktopt->ip6po_nexthop = NULL;
2538 if (optname == -1 || optname == IPV6_HOPOPTS) {
2539 if (needfree && pktopt->ip6po_hbh)
2540 free(pktopt->ip6po_hbh, M_IP6OPT);
2541 pktopt->ip6po_hbh = NULL;
2543 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2544 if (needfree && pktopt->ip6po_dest1)
2545 free(pktopt->ip6po_dest1, M_IP6OPT);
2546 pktopt->ip6po_dest1 = NULL;
2548 if (optname == -1 || optname == IPV6_RTHDR) {
2549 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2550 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2551 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2552 if (pktopt->ip6po_route.ro_rt) {
2553 RTFREE(pktopt->ip6po_route.ro_rt);
2554 pktopt->ip6po_route.ro_rt = NULL;
2557 if (optname == -1 || optname == IPV6_DSTOPTS) {
2558 if (needfree && pktopt->ip6po_dest2)
2559 free(pktopt->ip6po_dest2, M_IP6OPT);
2560 pktopt->ip6po_dest2 = NULL;
2564 #define PKTOPT_EXTHDRCPY(type) \
2567 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2568 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2569 if (dst->type == NULL && canwait == M_NOWAIT)\
2571 bcopy(src->type, dst->type, hlen);\
2573 } while (/*CONSTCOND*/ 0)
2575 struct ip6_pktopts *
2576 ip6_copypktopts(src, canwait)
2577 struct ip6_pktopts *src;
2580 struct ip6_pktopts *dst;
2583 printf("ip6_clearpktopts: invalid argument\n");
2587 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2588 if (dst == NULL && canwait == M_NOWAIT)
2590 bzero(dst, sizeof(*dst));
2593 dst->ip6po_hlim = src->ip6po_hlim;
2594 dst->ip6po_tclass = src->ip6po_tclass;
2595 dst->ip6po_flags = src->ip6po_flags;
2596 if (src->ip6po_pktinfo) {
2597 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2599 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2601 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2603 if (src->ip6po_nexthop) {
2604 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2606 if (dst->ip6po_nexthop == NULL)
2608 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2609 src->ip6po_nexthop->sa_len);
2611 PKTOPT_EXTHDRCPY(ip6po_hbh);
2612 PKTOPT_EXTHDRCPY(ip6po_dest1);
2613 PKTOPT_EXTHDRCPY(ip6po_dest2);
2614 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2618 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2619 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2620 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2621 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2622 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2623 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2624 free(dst, M_IP6OPT);
2627 #undef PKTOPT_EXTHDRCPY
2630 ip6_freepcbopts(pktopt)
2631 struct ip6_pktopts *pktopt;
2636 ip6_clearpktopts(pktopt, -1);
2638 free(pktopt, M_IP6OPT);
2642 * Set the IP6 multicast options in response to user setsockopt().
2645 ip6_setmoptions(optname, im6op, m)
2647 struct ip6_moptions **im6op;
2651 u_int loop, ifindex;
2652 struct ipv6_mreq *mreq;
2654 struct ip6_moptions *im6o = *im6op;
2655 struct route_in6 ro;
2656 struct sockaddr_in6 *dst;
2657 struct in6_multi_mship *imm;
2658 struct thread *td = curthread;
2662 * No multicast option buffer attached to the pcb;
2663 * allocate one and initialize to default values.
2665 im6o = (struct ip6_moptions *)
2666 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2671 im6o->im6o_multicast_ifp = NULL;
2672 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2673 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2674 LIST_INIT(&im6o->im6o_memberships);
2679 case IPV6_MULTICAST_IF:
2681 * Select the interface for outgoing multicast packets.
2683 if (m == NULL || m->m_len != sizeof(u_int)) {
2687 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2688 if (ifindex < 0 || if_index < ifindex) {
2689 error = ENXIO; /* XXX EINVAL? */
2692 ifp = ifnet_byindex(ifindex);
2693 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2694 error = EADDRNOTAVAIL;
2697 im6o->im6o_multicast_ifp = ifp;
2700 case IPV6_MULTICAST_HOPS:
2703 * Set the IP6 hoplimit for outgoing multicast packets.
2706 if (m == NULL || m->m_len != sizeof(int)) {
2710 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2711 if (optval < -1 || optval >= 256)
2713 else if (optval == -1)
2714 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2716 im6o->im6o_multicast_hlim = optval;
2720 case IPV6_MULTICAST_LOOP:
2722 * Set the loopback flag for outgoing multicast packets.
2723 * Must be zero or one.
2725 if (m == NULL || m->m_len != sizeof(u_int)) {
2729 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2734 im6o->im6o_multicast_loop = loop;
2737 case IPV6_JOIN_GROUP:
2739 * Add a multicast group membership.
2740 * Group must be a valid IP6 multicast address.
2742 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2746 mreq = mtod(m, struct ipv6_mreq *);
2747 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2749 * We use the unspecified address to specify to accept
2750 * all multicast addresses. Only super user is allowed
2757 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2763 * If the interface is specified, validate it.
2765 if (mreq->ipv6mr_interface < 0 ||
2766 if_index < mreq->ipv6mr_interface) {
2767 error = ENXIO; /* XXX EINVAL? */
2771 * If no interface was explicitly specified, choose an
2772 * appropriate one according to the given multicast address.
2774 if (mreq->ipv6mr_interface == 0) {
2776 * If the multicast address is in node-local scope,
2777 * the interface should be a loopback interface.
2778 * Otherwise, look up the routing table for the
2779 * address, and choose the outgoing interface.
2780 * XXX: is it a good approach?
2782 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) {
2786 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2787 bzero(dst, sizeof(*dst));
2788 dst->sin6_len = sizeof(struct sockaddr_in6);
2789 dst->sin6_family = AF_INET6;
2790 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2791 rtalloc((struct route *)&ro);
2792 if (ro.ro_rt == NULL) {
2793 error = EADDRNOTAVAIL;
2796 ifp = ro.ro_rt->rt_ifp;
2800 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2803 * See if we found an interface, and confirm that it
2804 * supports multicast
2806 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2807 error = EADDRNOTAVAIL;
2811 * Put interface index into the multicast address,
2812 * if the address has link-local scope.
2814 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2815 mreq->ipv6mr_multiaddr.s6_addr16[1] =
2816 htons(ifp->if_index);
2819 * See if the membership already exists.
2821 for (imm = im6o->im6o_memberships.lh_first;
2822 imm != NULL; imm = imm->i6mm_chain.le_next)
2823 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2824 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2825 &mreq->ipv6mr_multiaddr))
2832 * Everything looks good; add a new record to the multicast
2833 * address list for the given interface.
2835 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2840 if ((imm->i6mm_maddr =
2841 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2842 free(imm, M_IPMADDR);
2845 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2848 case IPV6_LEAVE_GROUP:
2850 * Drop a multicast group membership.
2851 * Group must be a valid IP6 multicast address.
2853 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2857 mreq = mtod(m, struct ipv6_mreq *);
2858 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2863 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2868 * If an interface address was specified, get a pointer
2869 * to its ifnet structure.
2871 if (mreq->ipv6mr_interface < 0
2872 || if_index < mreq->ipv6mr_interface) {
2873 error = ENXIO; /* XXX EINVAL? */
2876 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2878 * Put interface index into the multicast address,
2879 * if the address has link-local scope.
2881 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2882 mreq->ipv6mr_multiaddr.s6_addr16[1]
2883 = htons(mreq->ipv6mr_interface);
2887 * Find the membership in the membership list.
2889 for (imm = im6o->im6o_memberships.lh_first;
2890 imm != NULL; imm = imm->i6mm_chain.le_next) {
2891 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2892 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2893 &mreq->ipv6mr_multiaddr))
2897 /* Unable to resolve interface */
2898 error = EADDRNOTAVAIL;
2902 * Give up the multicast address record to which the
2903 * membership points.
2905 LIST_REMOVE(imm, i6mm_chain);
2906 in6_delmulti(imm->i6mm_maddr);
2907 free(imm, M_IPMADDR);
2916 * If all options have default values, no need to keep the mbuf.
2918 if (im6o->im6o_multicast_ifp == NULL &&
2919 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2920 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2921 im6o->im6o_memberships.lh_first == NULL) {
2922 free(*im6op, M_IPMOPTS);
2930 * Return the IP6 multicast options in response to user getsockopt().
2933 ip6_getmoptions(optname, im6o, mp)
2935 struct ip6_moptions *im6o;
2938 u_int *hlim, *loop, *ifindex;
2940 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2944 case IPV6_MULTICAST_IF:
2945 ifindex = mtod(*mp, u_int *);
2946 (*mp)->m_len = sizeof(u_int);
2947 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2950 *ifindex = im6o->im6o_multicast_ifp->if_index;
2953 case IPV6_MULTICAST_HOPS:
2954 hlim = mtod(*mp, u_int *);
2955 (*mp)->m_len = sizeof(u_int);
2957 *hlim = ip6_defmcasthlim;
2959 *hlim = im6o->im6o_multicast_hlim;
2962 case IPV6_MULTICAST_LOOP:
2963 loop = mtod(*mp, u_int *);
2964 (*mp)->m_len = sizeof(u_int);
2966 *loop = ip6_defmcasthlim;
2968 *loop = im6o->im6o_multicast_loop;
2972 return (EOPNOTSUPP);
2977 * Discard the IP6 multicast options.
2980 ip6_freemoptions(im6o)
2981 struct ip6_moptions *im6o;
2983 struct in6_multi_mship *imm;
2988 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2989 LIST_REMOVE(imm, i6mm_chain);
2990 if (imm->i6mm_maddr)
2991 in6_delmulti(imm->i6mm_maddr);
2992 free(imm, M_IPMADDR);
2994 free(im6o, M_IPMOPTS);
2998 * Set IPv6 outgoing packet options based on advanced API.
3001 ip6_setpktoptions(control, opt, stickyopt, priv, needcopy, uproto)
3002 struct mbuf *control;
3003 struct ip6_pktopts *opt, *stickyopt;
3004 int priv, needcopy, uproto;
3006 struct cmsghdr *cm = 0;
3008 if (control == 0 || opt == 0)
3013 * If stickyopt is provided, make a local copy of the options
3014 * for this particular packet, then override them by ancillary
3016 * XXX: need to gain a reference for the cached route of the
3017 * next hop in case of the overriding.
3020 if (opt->ip6po_nextroute.ro_rt) {
3021 RT_LOCK(opt->ip6po_nextroute.ro_rt);
3022 RT_ADDREF(opt->ip6po_nextroute.ro_rt);
3023 RT_UNLOCK(opt->ip6po_nextroute.ro_rt);
3026 init_ip6pktopts(opt);
3027 opt->needfree = needcopy;
3030 * XXX: Currently, we assume all the optional information is stored
3033 if (control->m_next)
3036 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
3037 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
3040 if (control->m_len < CMSG_LEN(0))
3043 cm = mtod(control, struct cmsghdr *);
3044 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
3046 if (cm->cmsg_level != IPPROTO_IPV6)
3049 error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm),
3050 cm->cmsg_len - CMSG_LEN(0), opt, priv, needcopy, 1, uproto);
3059 * Set a particular packet option, as a sticky option or an ancillary data
3060 * item. "len" can be 0 only when it's a sticky option.
3061 * We have 4 cases of combination of "sticky" and "cmsg":
3062 * "sticky=0, cmsg=0": impossible
3063 * "sticky=0, cmsg=1": RFC2292 or rfc2292bis ancillary data
3064 * "sticky=1, cmsg=0": rfc2292bis socket option
3065 * "sticky=1, cmsg=1": RFC2292 socket option
3068 ip6_setpktoption(optname, buf, len, opt, priv, sticky, cmsg, uproto)
3069 int optname, len, priv, sticky, cmsg, uproto;
3071 struct ip6_pktopts *opt;
3073 int minmtupolicy, preftemp;
3075 if (!sticky && !cmsg) {
3077 printf("ip6_setpktoption: impossible case\n");
3083 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3084 * not be specified in the context of rfc2292bis. Conversely,
3085 * rfc2292bis types should not be specified in the context of RFC2292.
3089 case IPV6_2292PKTINFO:
3090 case IPV6_2292HOPLIMIT:
3091 case IPV6_2292NEXTHOP:
3092 case IPV6_2292HOPOPTS:
3093 case IPV6_2292DSTOPTS:
3094 case IPV6_2292RTHDR:
3095 case IPV6_2292PKTOPTIONS:
3096 return (ENOPROTOOPT);
3099 if (sticky && cmsg) {
3106 case IPV6_RTHDRDSTOPTS:
3108 case IPV6_USE_MIN_MTU:
3111 case IPV6_PREFER_TEMPADDR: /* XXX: not an rfc2292bis option */
3112 return (ENOPROTOOPT);
3117 case IPV6_2292PKTINFO:
3120 struct ifnet *ifp = NULL;
3121 struct in6_pktinfo *pktinfo;
3123 if (len != sizeof(struct in6_pktinfo))
3126 pktinfo = (struct in6_pktinfo *)buf;
3129 * An application can clear any sticky IPV6_PKTINFO option by
3130 * doing a "regular" setsockopt with ipi6_addr being
3131 * in6addr_any and ipi6_ifindex being zero.
3132 * [RFC 3542, Section 6]
3134 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3135 pktinfo->ipi6_ifindex == 0 &&
3136 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3137 ip6_clearpktopts(opt, optname);
3141 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3142 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3146 /* validate the interface index if specified. */
3147 if (pktinfo->ipi6_ifindex > if_index ||
3148 pktinfo->ipi6_ifindex < 0) {
3151 if (pktinfo->ipi6_ifindex) {
3152 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3158 * We store the address anyway, and let in6_selectsrc()
3159 * validate the specified address. This is because ipi6_addr
3160 * may not have enough information about its scope zone, and
3161 * we may need additional information (such as outgoing
3162 * interface or the scope zone of a destination address) to
3163 * disambiguate the scope.
3164 * XXX: the delay of the validation may confuse the
3165 * application when it is used as a sticky option.
3168 if (opt->ip6po_pktinfo == NULL) {
3169 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3170 M_IP6OPT, M_WAITOK);
3172 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3174 opt->ip6po_pktinfo = pktinfo;
3178 case IPV6_2292HOPLIMIT:
3184 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3185 * to simplify the ordering among hoplimit options.
3187 if (optname == IPV6_HOPLIMIT && sticky)
3188 return (ENOPROTOOPT);
3190 if (len != sizeof(int))
3193 if (*hlimp < -1 || *hlimp > 255)
3196 opt->ip6po_hlim = *hlimp;
3204 if (len != sizeof(int))
3206 tclass = *(int *)buf;
3207 if (tclass < -1 || tclass > 255)
3210 opt->ip6po_tclass = tclass;
3214 case IPV6_2292NEXTHOP:
3219 if (len == 0) { /* just remove the option */
3220 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3224 /* check if cmsg_len is large enough for sa_len */
3225 if (len < sizeof(struct sockaddr) || len < *buf)
3228 switch (((struct sockaddr *)buf)->sa_family) {
3231 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3236 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3239 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3240 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3244 if ((error = scope6_check_id(sa6, ip6_use_defzone))
3249 sa6->sin6_scope_id = 0; /* XXX */
3252 case AF_LINK: /* should eventually be supported */
3254 return (EAFNOSUPPORT);
3257 /* turn off the previous option, then set the new option. */
3258 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3260 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_WAITOK);
3261 bcopy(buf, opt->ip6po_nexthop, *buf);
3263 opt->ip6po_nexthop = (struct sockaddr *)buf;
3266 case IPV6_2292HOPOPTS:
3269 struct ip6_hbh *hbh;
3273 * XXX: We don't allow a non-privileged user to set ANY HbH
3274 * options, since per-option restriction has too much
3281 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3282 break; /* just remove the option */
3285 /* message length validation */
3286 if (len < sizeof(struct ip6_hbh))
3288 hbh = (struct ip6_hbh *)buf;
3289 hbhlen = (hbh->ip6h_len + 1) << 3;
3293 /* turn off the previous option, then set the new option. */
3294 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3296 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_WAITOK);
3297 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3299 opt->ip6po_hbh = hbh;
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 * RFC2292bis 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);
3361 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
3362 bcopy(dest, *newdest, destlen);
3369 case IPV6_2292RTHDR:
3372 struct ip6_rthdr *rth;
3376 ip6_clearpktopts(opt, IPV6_RTHDR);
3377 break; /* just remove the option */
3380 /* message length validation */
3381 if (len < sizeof(struct ip6_rthdr))
3383 rth = (struct ip6_rthdr *)buf;
3384 rthlen = (rth->ip6r_len + 1) << 3;
3388 switch (rth->ip6r_type) {
3389 case IPV6_RTHDR_TYPE_0:
3390 if (rth->ip6r_len == 0) /* must contain one addr */
3392 if (rth->ip6r_len % 2) /* length must be even */
3394 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3398 return (EINVAL); /* not supported */
3401 /* turn off the previous option */
3402 ip6_clearpktopts(opt, IPV6_RTHDR);
3404 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_WAITOK);
3405 bcopy(rth, opt->ip6po_rthdr, rthlen);
3407 opt->ip6po_rthdr = rth;
3412 case IPV6_USE_MIN_MTU:
3413 if (len != sizeof(int))
3415 minmtupolicy = *(int *)buf;
3416 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3417 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3418 minmtupolicy != IP6PO_MINMTU_ALL) {
3421 opt->ip6po_minmtu = minmtupolicy;
3425 if (len != sizeof(int))
3428 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3430 * we ignore this option for TCP sockets.
3431 * (rfc2292bis leaves this case unspecified.)
3433 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3435 opt->ip6po_flags |= IP6PO_DONTFRAG;
3438 case IPV6_PREFER_TEMPADDR:
3439 if (len != sizeof(int))
3441 preftemp = *(int *)buf;
3442 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3443 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3444 preftemp != IP6PO_TEMPADDR_PREFER) {
3447 opt->ip6po_prefer_tempaddr = preftemp;
3451 return (ENOPROTOOPT);
3452 } /* end of switch */
3458 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3459 * packet to the input queue of a specified interface. Note that this
3460 * calls the output routine of the loopback "driver", but with an interface
3461 * pointer that might NOT be &loif -- easier than replicating that code here.
3464 ip6_mloopback(ifp, m, dst)
3467 struct sockaddr_in6 *dst;
3470 struct ip6_hdr *ip6;
3472 copym = m_copy(m, 0, M_COPYALL);
3477 * Make sure to deep-copy IPv6 header portion in case the data
3478 * is in an mbuf cluster, so that we can safely override the IPv6
3479 * header portion later.
3481 if ((copym->m_flags & M_EXT) != 0 ||
3482 copym->m_len < sizeof(struct ip6_hdr)) {
3483 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3489 if (copym->m_len < sizeof(*ip6)) {
3495 ip6 = mtod(copym, struct ip6_hdr *);
3497 * clear embedded scope identifiers if necessary.
3498 * in6_clearscope will touch the addresses only when necessary.
3500 in6_clearscope(&ip6->ip6_src);
3501 in6_clearscope(&ip6->ip6_dst);
3503 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3507 * Chop IPv6 header off from the payload.
3510 ip6_splithdr(m, exthdrs)
3512 struct ip6_exthdrs *exthdrs;
3515 struct ip6_hdr *ip6;
3517 ip6 = mtod(m, struct ip6_hdr *);
3518 if (m->m_len > sizeof(*ip6)) {
3519 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3524 M_MOVE_PKTHDR(mh, m);
3525 MH_ALIGN(mh, sizeof(*ip6));
3526 m->m_len -= sizeof(*ip6);
3527 m->m_data += sizeof(*ip6);
3530 m->m_len = sizeof(*ip6);
3531 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3533 exthdrs->ip6e_ip6 = m;
3538 * Compute IPv6 extension header length.
3542 struct in6pcb *in6p;
3546 if (!in6p->in6p_outputopts)
3551 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3553 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3554 if (in6p->in6p_outputopts->ip6po_rthdr)
3555 /* dest1 is valid with rthdr only */
3556 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3557 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3558 len += elen(in6p->in6p_outputopts->ip6po_dest2);