2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
71 #include "opt_route.h"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
87 #include <net/netisr.h>
88 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet/tcp_var.h>
101 #include <netinet6/nd6.h>
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #include <netipsec/key.h>
107 #include <netinet6/ip6_ipsec.h>
110 #include <netinet/sctp.h>
111 #include <netinet/sctp_crc32.h>
114 #include <netinet6/ip6protosw.h>
115 #include <netinet6/scope6_var.h>
118 #include <net/flowtable.h>
121 extern int in6_mcast_loop;
124 struct mbuf *ip6e_ip6;
125 struct mbuf *ip6e_hbh;
126 struct mbuf *ip6e_dest1;
127 struct mbuf *ip6e_rthdr;
128 struct mbuf *ip6e_dest2;
131 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
132 struct ucred *, int));
133 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
134 struct socket *, struct sockopt *));
135 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
136 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *,
137 struct ucred *, int, int, int));
139 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
140 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
141 struct ip6_frag **));
142 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
143 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
144 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
145 struct ifnet *, struct in6_addr *, u_long *, int *));
146 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
150 * Make an extension header from option data. hp is the source, and
151 * mp is the destination.
153 #define MAKE_EXTHDR(hp, mp) \
156 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
157 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
158 ((eh)->ip6e_len + 1) << 3); \
162 } while (/*CONSTCOND*/ 0)
165 * Form a chain of extension headers.
166 * m is the extension header mbuf
167 * mp is the previous mbuf in the chain
168 * p is the next header
169 * i is the type of option.
171 #define MAKE_CHAIN(m, mp, p, i)\
175 panic("assumption failed: hdr not split"); \
176 *mtod((m), u_char *) = *(p);\
178 p = mtod((m), u_char *);\
179 (m)->m_next = (mp)->m_next;\
183 } while (/*CONSTCOND*/ 0)
186 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
187 * header (with pri, len, nxt, hlim, src, dst).
188 * This function may modify ver and hlim only.
189 * The mbuf chain containing the packet will be freed.
190 * The mbuf opt, if present, will not be freed.
192 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
193 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
194 * which is rt_rmx.rmx_mtu.
196 * ifpp - XXX: just for statistics
199 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
200 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
201 struct ifnet **ifpp, struct inpcb *inp)
203 struct ip6_hdr *ip6, *mhip6;
204 struct ifnet *ifp, *origifp;
206 struct mbuf *mprev = NULL;
207 int hlen, tlen, len, off;
208 struct route_in6 ip6route;
209 struct rtentry *rt = NULL;
210 struct sockaddr_in6 *dst, src_sa, dst_sa;
211 struct in6_addr odst;
213 struct in6_ifaddr *ia = NULL;
215 int alwaysfrag, dontfrag;
216 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
217 struct ip6_exthdrs exthdrs;
218 struct in6_addr finaldst, src0, dst0;
220 struct route_in6 *ro_pmtu = NULL;
228 struct ipsec_output_state state;
229 struct ip6_rthdr *rh = NULL;
230 int needipsectun = 0;
232 struct secpolicy *sp = NULL;
234 #ifdef IPFIREWALL_FORWARD
235 struct m_tag *fwd_tag;
238 ip6 = mtod(m, struct ip6_hdr *);
240 printf ("ip6 is NULL");
244 finaldst = ip6->ip6_dst;
245 bzero(&exthdrs, sizeof(exthdrs));
247 /* Hop-by-Hop options header */
248 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
249 /* Destination options header(1st part) */
250 if (opt->ip6po_rthdr) {
252 * Destination options header(1st part)
253 * This only makes sense with a routing header.
254 * See Section 9.2 of RFC 3542.
255 * Disabling this part just for MIP6 convenience is
256 * a bad idea. We need to think carefully about a
257 * way to make the advanced API coexist with MIP6
258 * options, which might automatically be inserted in
261 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
264 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
265 /* Destination options header(2nd part) */
266 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
270 * IPSec checking which handles several cases.
271 * FAST IPSEC: We re-injected the packet.
274 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
276 case 1: /* Bad packet */
278 case -1: /* Do IPSec */
280 case 0: /* No IPSec */
287 * Calculate the total length of the extension header chain.
288 * Keep the length of the unfragmentable part for fragmentation.
291 if (exthdrs.ip6e_hbh)
292 optlen += exthdrs.ip6e_hbh->m_len;
293 if (exthdrs.ip6e_dest1)
294 optlen += exthdrs.ip6e_dest1->m_len;
295 if (exthdrs.ip6e_rthdr)
296 optlen += exthdrs.ip6e_rthdr->m_len;
297 unfragpartlen = optlen + sizeof(struct ip6_hdr);
299 /* NOTE: we don't add AH/ESP length here. do that later. */
300 if (exthdrs.ip6e_dest2)
301 optlen += exthdrs.ip6e_dest2->m_len;
304 * If we need IPsec, or there is at least one extension header,
305 * separate IP6 header from the payload.
307 if ((needipsec || optlen) && !hdrsplit) {
308 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
312 m = exthdrs.ip6e_ip6;
317 ip6 = mtod(m, struct ip6_hdr *);
319 /* adjust mbuf packet header length */
320 m->m_pkthdr.len += optlen;
321 plen = m->m_pkthdr.len - sizeof(*ip6);
323 /* If this is a jumbo payload, insert a jumbo payload option. */
324 if (plen > IPV6_MAXPACKET) {
326 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
330 m = exthdrs.ip6e_ip6;
334 ip6 = mtod(m, struct ip6_hdr *);
335 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
339 ip6->ip6_plen = htons(plen);
342 * Concatenate headers and fill in next header fields.
343 * Here we have, on "m"
345 * and we insert headers accordingly. Finally, we should be getting:
346 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
348 * during the header composing process, "m" points to IPv6 header.
349 * "mprev" points to an extension header prior to esp.
351 u_char *nexthdrp = &ip6->ip6_nxt;
355 * we treat dest2 specially. this makes IPsec processing
356 * much easier. the goal here is to make mprev point the
357 * mbuf prior to dest2.
359 * result: IPv6 dest2 payload
360 * m and mprev will point to IPv6 header.
362 if (exthdrs.ip6e_dest2) {
364 panic("assumption failed: hdr not split");
365 exthdrs.ip6e_dest2->m_next = m->m_next;
366 m->m_next = exthdrs.ip6e_dest2;
367 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
368 ip6->ip6_nxt = IPPROTO_DSTOPTS;
372 * result: IPv6 hbh dest1 rthdr dest2 payload
373 * m will point to IPv6 header. mprev will point to the
374 * extension header prior to dest2 (rthdr in the above case).
376 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
377 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
379 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
387 * pointers after IPsec headers are not valid any more.
388 * other pointers need a great care too.
389 * (IPsec routines should not mangle mbufs prior to AH/ESP)
391 exthdrs.ip6e_dest2 = NULL;
393 if (exthdrs.ip6e_rthdr) {
394 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
395 segleft_org = rh->ip6r_segleft;
396 rh->ip6r_segleft = 0;
399 bzero(&state, sizeof(state));
401 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
404 if (error == EJUSTRETURN) {
406 * We had a SP with a level of 'use' and no SA. We
407 * will just continue to process the packet without
412 /* mbuf is already reclaimed in ipsec6_output_trans. */
422 printf("[%s:%d] (ipsec): error code %d\n",
423 __func__, __LINE__, error);
426 /* don't show these error codes to the user */
431 } else if (!needipsectun) {
433 * In the FAST IPSec case we have already
434 * re-injected the packet and it has been freed
435 * by the ipsec_done() function. So, just clean
436 * up after ourselves.
441 if (exthdrs.ip6e_rthdr) {
442 /* ah6_output doesn't modify mbuf chain */
443 rh->ip6r_segleft = segleft_org;
449 * If there is a routing header, discard the packet.
451 if (exthdrs.ip6e_rthdr) {
456 /* Source address validation */
457 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
458 (flags & IPV6_UNSPECSRC) == 0) {
460 V_ip6stat.ip6s_badscope++;
463 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
465 V_ip6stat.ip6s_badscope++;
469 V_ip6stat.ip6s_localout++;
476 bzero((caddr_t)ro, sizeof(*ro));
479 if (opt && opt->ip6po_rthdr)
480 ro = &opt->ip6po_route;
481 dst = (struct sockaddr_in6 *)&ro->ro_dst;
483 if (ro == &ip6route) {
487 * The flow table returns route entries valid for up to 30
488 * seconds; we rely on the remainder of ip_output() taking no
489 * longer than that long for the stability of ro_rt. The
490 * flow ID assignment must have happened before this point.
492 if ((fle = flowtable_lookup_mbuf(V_ip6_ft, m, AF_INET6)) != NULL) {
493 flow_to_route_in6(fle, ro);
494 if (ro->ro_rt != NULL && ro->ro_lle != NULL)
501 * if specified, try to fill in the traffic class field.
502 * do not override if a non-zero value is already set.
503 * we check the diffserv field and the ecn field separately.
505 if (opt && opt->ip6po_tclass >= 0) {
508 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
510 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
513 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
516 /* fill in or override the hop limit field, if necessary. */
517 if (opt && opt->ip6po_hlim != -1)
518 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
519 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
521 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
523 ip6->ip6_hlim = V_ip6_defmcasthlim;
528 * We may re-inject packets into the stack here.
530 if (needipsec && needipsectun) {
531 struct ipsec_output_state state;
534 * All the extension headers will become inaccessible
535 * (since they can be encrypted).
536 * Don't panic, we need no more updates to extension headers
537 * on inner IPv6 packet (since they are now encapsulated).
539 * IPv6 [ESP|AH] IPv6 [extension headers] payload
541 bzero(&exthdrs, sizeof(exthdrs));
542 exthdrs.ip6e_ip6 = m;
544 bzero(&state, sizeof(state));
546 state.ro = (struct route *)ro;
547 state.dst = (struct sockaddr *)dst;
549 error = ipsec6_output_tunnel(&state, sp, flags);
552 ro = (struct route_in6 *)state.ro;
553 dst = (struct sockaddr_in6 *)state.dst;
554 if (error == EJUSTRETURN) {
556 * We had a SP with a level of 'use' and no SA. We
557 * will just continue to process the packet without
562 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
573 printf("[%s:%d] (ipsec): error code %d\n",
574 __func__, __LINE__, error);
577 /* don't show these error codes to the user */
584 * In the FAST IPSec case we have already
585 * re-injected the packet and it has been freed
586 * by the ipsec_done() function. So, just clean
587 * up after ourselves.
593 exthdrs.ip6e_ip6 = m;
598 ip6 = mtod(m, struct ip6_hdr *);
600 bzero(&dst_sa, sizeof(dst_sa));
601 dst_sa.sin6_family = AF_INET6;
602 dst_sa.sin6_len = sizeof(dst_sa);
603 dst_sa.sin6_addr = ip6->ip6_dst;
606 ifp = ro->ro_rt->rt_ifp;
607 } else if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
611 V_ip6stat.ip6s_noroute++;
615 break; /* XXX statistics? */
618 in6_ifstat_inc(ifp, ifs6_out_discard);
623 * If in6_selectroute() does not return a route entry,
624 * dst may not have been updated.
626 *dst = dst_sa; /* XXX */
630 * then rt (for unicast) and ifp must be non-NULL valid values.
632 if ((flags & IPV6_FORWARDING) == 0) {
633 /* XXX: the FORWARDING flag can be set for mrouting. */
634 in6_ifstat_inc(ifp, ifs6_out_request);
637 ia = (struct in6_ifaddr *)(rt->rt_ifa);
643 * The outgoing interface must be in the zone of source and
644 * destination addresses.
649 if (in6_setscope(&src0, origifp, &zone))
651 bzero(&src_sa, sizeof(src_sa));
652 src_sa.sin6_family = AF_INET6;
653 src_sa.sin6_len = sizeof(src_sa);
654 src_sa.sin6_addr = ip6->ip6_src;
655 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
659 if (in6_setscope(&dst0, origifp, &zone))
661 /* re-initialize to be sure */
662 bzero(&dst_sa, sizeof(dst_sa));
663 dst_sa.sin6_family = AF_INET6;
664 dst_sa.sin6_len = sizeof(dst_sa);
665 dst_sa.sin6_addr = ip6->ip6_dst;
666 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
670 /* We should use ia_ifp to support the case of
671 * sending packets to an address of our own.
673 if (ia != NULL && ia->ia_ifp)
676 /* scope check is done. */
680 V_ip6stat.ip6s_badscope++;
681 in6_ifstat_inc(origifp, ifs6_out_discard);
683 error = EHOSTUNREACH; /* XXX */
687 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
688 if (opt && opt->ip6po_nextroute.ro_rt) {
690 * The nexthop is explicitly specified by the
691 * application. We assume the next hop is an IPv6
694 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
696 else if ((rt->rt_flags & RTF_GATEWAY))
697 dst = (struct sockaddr_in6 *)rt->rt_gateway;
700 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
701 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
703 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
704 in6_ifstat_inc(ifp, ifs6_out_mcast);
706 * Confirm that the outgoing interface supports multicast.
708 if (!(ifp->if_flags & IFF_MULTICAST)) {
709 V_ip6stat.ip6s_noroute++;
710 in6_ifstat_inc(ifp, ifs6_out_discard);
714 if ((im6o == NULL && in6_mcast_loop) ||
715 (im6o && im6o->im6o_multicast_loop)) {
717 * Loop back multicast datagram if not expressly
718 * forbidden to do so, even if we have not joined
719 * the address; protocols will filter it later,
720 * thus deferring a hash lookup and lock acquisition
721 * at the expense of an m_copym().
723 ip6_mloopback(ifp, m, dst);
726 * If we are acting as a multicast router, perform
727 * multicast forwarding as if the packet had just
728 * arrived on the interface to which we are about
729 * to send. The multicast forwarding function
730 * recursively calls this function, using the
731 * IPV6_FORWARDING flag to prevent infinite recursion.
733 * Multicasts that are looped back by ip6_mloopback(),
734 * above, will be forwarded by the ip6_input() routine,
737 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
739 * XXX: ip6_mforward expects that rcvif is NULL
740 * when it is called from the originating path.
741 * However, it is not always the case, since
742 * some versions of MGETHDR() does not
743 * initialize the field.
745 m->m_pkthdr.rcvif = NULL;
746 if (ip6_mforward(ip6, ifp, m) != 0) {
753 * Multicasts with a hoplimit of zero may be looped back,
754 * above, but must not be transmitted on a network.
755 * Also, multicasts addressed to the loopback interface
756 * are not sent -- the above call to ip6_mloopback() will
757 * loop back a copy if this host actually belongs to the
758 * destination group on the loopback interface.
760 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
761 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
768 * Fill the outgoing inteface to tell the upper layer
769 * to increment per-interface statistics.
774 /* Determine path MTU. */
775 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
780 * The caller of this function may specify to use the minimum MTU
782 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
783 * setting. The logic is a bit complicated; by default, unicast
784 * packets will follow path MTU while multicast packets will be sent at
785 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
786 * including unicast ones will be sent at the minimum MTU. Multicast
787 * packets will always be sent at the minimum MTU unless
788 * IP6PO_MINMTU_DISABLE is explicitly specified.
789 * See RFC 3542 for more details.
791 if (mtu > IPV6_MMTU) {
792 if ((flags & IPV6_MINMTU))
794 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
796 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
798 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
804 * clear embedded scope identifiers if necessary.
805 * in6_clearscope will touch the addresses only when necessary.
807 in6_clearscope(&ip6->ip6_src);
808 in6_clearscope(&ip6->ip6_dst);
811 * If the outgoing packet contains a hop-by-hop options header,
812 * it must be examined and processed even by the source node.
813 * (RFC 2460, section 4.)
815 if (exthdrs.ip6e_hbh) {
816 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
817 u_int32_t dummy; /* XXX unused */
818 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
821 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
822 panic("ip6e_hbh is not contiguous");
825 * XXX: if we have to send an ICMPv6 error to the sender,
826 * we need the M_LOOP flag since icmp6_error() expects
827 * the IPv6 and the hop-by-hop options header are
828 * contiguous unless the flag is set.
830 m->m_flags |= M_LOOP;
831 m->m_pkthdr.rcvif = ifp;
832 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
833 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
834 &dummy, &plen) < 0) {
835 /* m was already freed at this point */
836 error = EINVAL;/* better error? */
839 m->m_flags &= ~M_LOOP; /* XXX */
840 m->m_pkthdr.rcvif = NULL;
843 /* Jump over all PFIL processing if hooks are not active. */
844 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
848 /* Run through list of hooks for output packets. */
849 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
850 if (error != 0 || m == NULL)
852 ip6 = mtod(m, struct ip6_hdr *);
854 /* See if destination IP address was changed by packet filter. */
855 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
856 m->m_flags |= M_SKIP_FIREWALL;
857 /* If destination is now ourself drop to ip6_input(). */
858 if (in6_localip(&ip6->ip6_dst)) {
859 m->m_flags |= M_FASTFWD_OURS;
860 if (m->m_pkthdr.rcvif == NULL)
861 m->m_pkthdr.rcvif = V_loif;
862 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
863 m->m_pkthdr.csum_flags |=
864 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
865 m->m_pkthdr.csum_data = 0xffff;
867 m->m_pkthdr.csum_flags |=
868 CSUM_IP_CHECKED | CSUM_IP_VALID;
870 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
871 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
873 error = netisr_queue(NETISR_IPV6, m);
876 goto again; /* Redo the routing table lookup. */
879 #ifdef IPFIREWALL_FORWARD
880 /* See if local, if yes, send it to netisr. */
881 if (m->m_flags & M_FASTFWD_OURS) {
882 if (m->m_pkthdr.rcvif == NULL)
883 m->m_pkthdr.rcvif = V_loif;
884 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
885 m->m_pkthdr.csum_flags |=
886 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
887 m->m_pkthdr.csum_data = 0xffff;
890 if (m->m_pkthdr.csum_flags & CSUM_SCTP)
891 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
893 error = netisr_queue(NETISR_IPV6, m);
896 /* Or forward to some other address? */
897 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
899 dst = (struct sockaddr_in6 *)&ro->ro_dst;
900 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
901 m->m_flags |= M_SKIP_FIREWALL;
902 m_tag_delete(m, fwd_tag);
905 #endif /* IPFIREWALL_FORWARD */
909 * Send the packet to the outgoing interface.
910 * If necessary, do IPv6 fragmentation before sending.
912 * the logic here is rather complex:
913 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
914 * 1-a: send as is if tlen <= path mtu
915 * 1-b: fragment if tlen > path mtu
917 * 2: if user asks us not to fragment (dontfrag == 1)
918 * 2-a: send as is if tlen <= interface mtu
919 * 2-b: error if tlen > interface mtu
921 * 3: if we always need to attach fragment header (alwaysfrag == 1)
924 * 4: if dontfrag == 1 && alwaysfrag == 1
925 * error, as we cannot handle this conflicting request
928 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
929 if (sw_csum & CSUM_SCTP) {
930 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
931 sw_csum &= ~CSUM_SCTP;
934 tlen = m->m_pkthdr.len;
936 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
940 if (dontfrag && alwaysfrag) { /* case 4 */
941 /* conflicting request - can't transmit */
945 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
947 * Even if the DONTFRAG option is specified, we cannot send the
948 * packet when the data length is larger than the MTU of the
949 * outgoing interface.
950 * Notify the error by sending IPV6_PATHMTU ancillary data as
951 * well as returning an error code (the latter is not described
955 struct ip6ctlparam ip6cp;
957 mtu32 = (u_int32_t)mtu;
958 bzero(&ip6cp, sizeof(ip6cp));
959 ip6cp.ip6c_cmdarg = (void *)&mtu32;
960 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
968 * transmit packet without fragmentation
970 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
971 struct in6_ifaddr *ia6;
973 ip6 = mtod(m, struct ip6_hdr *);
974 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
976 /* Record statistics for this interface address. */
977 ia6->ia_ifa.if_opackets++;
978 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
979 ifa_free(&ia6->ia_ifa);
981 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
986 * try to fragment the packet. case 1-b and 3
988 if (mtu < IPV6_MMTU) {
989 /* path MTU cannot be less than IPV6_MMTU */
991 in6_ifstat_inc(ifp, ifs6_out_fragfail);
993 } else if (ip6->ip6_plen == 0) {
994 /* jumbo payload cannot be fragmented */
996 in6_ifstat_inc(ifp, ifs6_out_fragfail);
999 struct mbuf **mnext, *m_frgpart;
1000 struct ip6_frag *ip6f;
1001 u_int32_t id = htonl(ip6_randomid());
1004 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1007 * Too large for the destination or interface;
1008 * fragment if possible.
1009 * Must be able to put at least 8 bytes per fragment.
1011 hlen = unfragpartlen;
1012 if (mtu > IPV6_MAXPACKET)
1013 mtu = IPV6_MAXPACKET;
1015 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1018 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1023 * Verify that we have any chance at all of being able to queue
1024 * the packet or packet fragments
1026 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1027 < tlen /* - hlen */)) {
1029 V_ip6stat.ip6s_odropped++;
1033 mnext = &m->m_nextpkt;
1036 * Change the next header field of the last header in the
1037 * unfragmentable part.
1039 if (exthdrs.ip6e_rthdr) {
1040 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1041 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1042 } else if (exthdrs.ip6e_dest1) {
1043 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1044 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1045 } else if (exthdrs.ip6e_hbh) {
1046 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1047 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1049 nextproto = ip6->ip6_nxt;
1050 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1054 * Loop through length of segment after first fragment,
1055 * make new header and copy data of each part and link onto
1059 for (off = hlen; off < tlen; off += len) {
1060 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1063 V_ip6stat.ip6s_odropped++;
1066 m->m_pkthdr.rcvif = NULL;
1067 m->m_flags = m0->m_flags & M_COPYFLAGS;
1069 mnext = &m->m_nextpkt;
1070 m->m_data += max_linkhdr;
1071 mhip6 = mtod(m, struct ip6_hdr *);
1073 m->m_len = sizeof(*mhip6);
1074 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1076 V_ip6stat.ip6s_odropped++;
1079 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1080 if (off + len >= tlen)
1083 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1084 mhip6->ip6_plen = htons((u_short)(len + hlen +
1085 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1086 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1088 V_ip6stat.ip6s_odropped++;
1091 m_cat(m, m_frgpart);
1092 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1093 m->m_pkthdr.rcvif = NULL;
1094 ip6f->ip6f_reserved = 0;
1095 ip6f->ip6f_ident = id;
1096 ip6f->ip6f_nxt = nextproto;
1097 V_ip6stat.ip6s_ofragments++;
1098 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1101 in6_ifstat_inc(ifp, ifs6_out_fragok);
1105 * Remove leading garbages.
1111 for (m0 = m; m; m = m0) {
1115 /* Record statistics for this interface address. */
1117 ia->ia_ifa.if_opackets++;
1118 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1120 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1126 V_ip6stat.ip6s_fragmented++;
1129 if (ro == &ip6route && ro->ro_rt && flevalid == 0) {
1130 /* brace necessary for RTFREE */
1132 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt &&
1133 ((flevalid == 0) || (ro_pmtu != ro))) {
1134 RTFREE(ro_pmtu->ro_rt);
1144 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1145 m_freem(exthdrs.ip6e_dest1);
1146 m_freem(exthdrs.ip6e_rthdr);
1147 m_freem(exthdrs.ip6e_dest2);
1156 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1160 if (hlen > MCLBYTES)
1161 return (ENOBUFS); /* XXX */
1163 MGET(m, M_DONTWAIT, MT_DATA);
1168 MCLGET(m, M_DONTWAIT);
1169 if ((m->m_flags & M_EXT) == 0) {
1176 bcopy(hdr, mtod(m, caddr_t), hlen);
1183 * Insert jumbo payload option.
1186 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1192 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1195 * If there is no hop-by-hop options header, allocate new one.
1196 * If there is one but it doesn't have enough space to store the
1197 * jumbo payload option, allocate a cluster to store the whole options.
1198 * Otherwise, use it to store the options.
1200 if (exthdrs->ip6e_hbh == 0) {
1201 MGET(mopt, M_DONTWAIT, MT_DATA);
1204 mopt->m_len = JUMBOOPTLEN;
1205 optbuf = mtod(mopt, u_char *);
1206 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1207 exthdrs->ip6e_hbh = mopt;
1209 struct ip6_hbh *hbh;
1211 mopt = exthdrs->ip6e_hbh;
1212 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1215 * - exthdrs->ip6e_hbh is not referenced from places
1216 * other than exthdrs.
1217 * - exthdrs->ip6e_hbh is not an mbuf chain.
1219 int oldoptlen = mopt->m_len;
1223 * XXX: give up if the whole (new) hbh header does
1224 * not fit even in an mbuf cluster.
1226 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1230 * As a consequence, we must always prepare a cluster
1233 MGET(n, M_DONTWAIT, MT_DATA);
1235 MCLGET(n, M_DONTWAIT);
1236 if ((n->m_flags & M_EXT) == 0) {
1243 n->m_len = oldoptlen + JUMBOOPTLEN;
1244 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1246 optbuf = mtod(n, caddr_t) + oldoptlen;
1248 mopt = exthdrs->ip6e_hbh = n;
1250 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1251 mopt->m_len += JUMBOOPTLEN;
1253 optbuf[0] = IP6OPT_PADN;
1257 * Adjust the header length according to the pad and
1258 * the jumbo payload option.
1260 hbh = mtod(mopt, struct ip6_hbh *);
1261 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1264 /* fill in the option. */
1265 optbuf[2] = IP6OPT_JUMBO;
1267 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1268 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1270 /* finally, adjust the packet header length */
1271 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1278 * Insert fragment header and copy unfragmentable header portions.
1281 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1282 struct ip6_frag **frghdrp)
1284 struct mbuf *n, *mlast;
1286 if (hlen > sizeof(struct ip6_hdr)) {
1287 n = m_copym(m0, sizeof(struct ip6_hdr),
1288 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1295 /* Search for the last mbuf of unfragmentable part. */
1296 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1299 if ((mlast->m_flags & M_EXT) == 0 &&
1300 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1301 /* use the trailing space of the last mbuf for the fragment hdr */
1302 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1304 mlast->m_len += sizeof(struct ip6_frag);
1305 m->m_pkthdr.len += sizeof(struct ip6_frag);
1307 /* allocate a new mbuf for the fragment header */
1310 MGET(mfrg, M_DONTWAIT, MT_DATA);
1313 mfrg->m_len = sizeof(struct ip6_frag);
1314 *frghdrp = mtod(mfrg, struct ip6_frag *);
1315 mlast->m_next = mfrg;
1322 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1323 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1330 if (ro_pmtu != ro) {
1331 /* The first hop and the final destination may differ. */
1332 struct sockaddr_in6 *sa6_dst =
1333 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1334 if (ro_pmtu->ro_rt &&
1335 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1336 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1337 RTFREE(ro_pmtu->ro_rt);
1338 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1340 if (ro_pmtu->ro_rt == NULL) {
1341 bzero(sa6_dst, sizeof(*sa6_dst));
1342 sa6_dst->sin6_family = AF_INET6;
1343 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1344 sa6_dst->sin6_addr = *dst;
1346 rtalloc((struct route *)ro_pmtu);
1349 if (ro_pmtu->ro_rt) {
1351 struct in_conninfo inc;
1353 bzero(&inc, sizeof(inc));
1354 inc.inc_flags |= INC_ISIPV6;
1355 inc.inc6_faddr = *dst;
1358 ifp = ro_pmtu->ro_rt->rt_ifp;
1359 ifmtu = IN6_LINKMTU(ifp);
1360 mtu = tcp_hc_getmtu(&inc);
1362 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1364 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1367 else if (mtu < IPV6_MMTU) {
1369 * RFC2460 section 5, last paragraph:
1370 * if we record ICMPv6 too big message with
1371 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1372 * or smaller, with framgent header attached.
1373 * (fragment header is needed regardless from the
1374 * packet size, for translators to identify packets)
1378 } else if (mtu > ifmtu) {
1380 * The MTU on the route is larger than the MTU on
1381 * the interface! This shouldn't happen, unless the
1382 * MTU of the interface has been changed after the
1383 * interface was brought up. Change the MTU in the
1384 * route to match the interface MTU (as long as the
1385 * field isn't locked).
1388 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1391 mtu = IN6_LINKMTU(ifp);
1393 error = EHOSTUNREACH; /* XXX */
1397 *alwaysfragp = alwaysfrag;
1402 * IP6 socket option processing.
1405 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1407 int optdatalen, uproto;
1409 struct inpcb *in6p = sotoinpcb(so);
1411 int level, op, optname;
1415 level = sopt->sopt_level;
1416 op = sopt->sopt_dir;
1417 optname = sopt->sopt_name;
1418 optlen = sopt->sopt_valsize;
1422 uproto = (int)so->so_proto->pr_protocol;
1424 if (level == IPPROTO_IPV6) {
1429 case IPV6_2292PKTOPTIONS:
1430 #ifdef IPV6_PKTOPTIONS
1431 case IPV6_PKTOPTIONS:
1436 error = soopt_getm(sopt, &m); /* XXX */
1439 error = soopt_mcopyin(sopt, m); /* XXX */
1442 error = ip6_pcbopts(&in6p->in6p_outputopts,
1444 m_freem(m); /* XXX */
1449 * Use of some Hop-by-Hop options or some
1450 * Destination options, might require special
1451 * privilege. That is, normal applications
1452 * (without special privilege) might be forbidden
1453 * from setting certain options in outgoing packets,
1454 * and might never see certain options in received
1455 * packets. [RFC 2292 Section 6]
1456 * KAME specific note:
1457 * KAME prevents non-privileged users from sending or
1458 * receiving ANY hbh/dst options in order to avoid
1459 * overhead of parsing options in the kernel.
1461 case IPV6_RECVHOPOPTS:
1462 case IPV6_RECVDSTOPTS:
1463 case IPV6_RECVRTHDRDSTOPTS:
1465 error = priv_check(td,
1466 PRIV_NETINET_SETHDROPTS);
1471 case IPV6_UNICAST_HOPS:
1475 case IPV6_RECVPKTINFO:
1476 case IPV6_RECVHOPLIMIT:
1477 case IPV6_RECVRTHDR:
1478 case IPV6_RECVPATHMTU:
1479 case IPV6_RECVTCLASS:
1481 case IPV6_AUTOFLOWLABEL:
1483 if (optname == IPV6_BINDANY && td != NULL) {
1484 error = priv_check(td,
1485 PRIV_NETINET_BINDANY);
1490 if (optlen != sizeof(int)) {
1494 error = sooptcopyin(sopt, &optval,
1495 sizeof optval, sizeof optval);
1500 case IPV6_UNICAST_HOPS:
1501 if (optval < -1 || optval >= 256)
1504 /* -1 = kernel default */
1505 in6p->in6p_hops = optval;
1506 if ((in6p->inp_vflag &
1508 in6p->inp_ip_ttl = optval;
1511 #define OPTSET(bit) \
1514 in6p->inp_flags |= (bit); \
1516 in6p->inp_flags &= ~(bit); \
1517 } while (/*CONSTCOND*/ 0)
1518 #define OPTSET2292(bit) \
1520 in6p->inp_flags |= IN6P_RFC2292; \
1522 in6p->inp_flags |= (bit); \
1524 in6p->inp_flags &= ~(bit); \
1525 } while (/*CONSTCOND*/ 0)
1526 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1528 case IPV6_RECVPKTINFO:
1529 /* cannot mix with RFC2292 */
1530 if (OPTBIT(IN6P_RFC2292)) {
1534 OPTSET(IN6P_PKTINFO);
1539 struct ip6_pktopts **optp;
1541 /* cannot mix with RFC2292 */
1542 if (OPTBIT(IN6P_RFC2292)) {
1546 optp = &in6p->in6p_outputopts;
1547 error = ip6_pcbopt(IPV6_HOPLIMIT,
1548 (u_char *)&optval, sizeof(optval),
1549 optp, (td != NULL) ? td->td_ucred :
1554 case IPV6_RECVHOPLIMIT:
1555 /* cannot mix with RFC2292 */
1556 if (OPTBIT(IN6P_RFC2292)) {
1560 OPTSET(IN6P_HOPLIMIT);
1563 case IPV6_RECVHOPOPTS:
1564 /* cannot mix with RFC2292 */
1565 if (OPTBIT(IN6P_RFC2292)) {
1569 OPTSET(IN6P_HOPOPTS);
1572 case IPV6_RECVDSTOPTS:
1573 /* cannot mix with RFC2292 */
1574 if (OPTBIT(IN6P_RFC2292)) {
1578 OPTSET(IN6P_DSTOPTS);
1581 case IPV6_RECVRTHDRDSTOPTS:
1582 /* cannot mix with RFC2292 */
1583 if (OPTBIT(IN6P_RFC2292)) {
1587 OPTSET(IN6P_RTHDRDSTOPTS);
1590 case IPV6_RECVRTHDR:
1591 /* cannot mix with RFC2292 */
1592 if (OPTBIT(IN6P_RFC2292)) {
1603 case IPV6_RECVPATHMTU:
1605 * We ignore this option for TCP
1607 * (RFC3542 leaves this case
1610 if (uproto != IPPROTO_TCP)
1616 * make setsockopt(IPV6_V6ONLY)
1617 * available only prior to bind(2).
1618 * see ipng mailing list, Jun 22 2001.
1620 if (in6p->inp_lport ||
1621 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1625 OPTSET(IN6P_IPV6_V6ONLY);
1627 in6p->inp_vflag &= ~INP_IPV4;
1629 in6p->inp_vflag |= INP_IPV4;
1631 case IPV6_RECVTCLASS:
1632 /* cannot mix with RFC2292 XXX */
1633 if (OPTBIT(IN6P_RFC2292)) {
1637 OPTSET(IN6P_TCLASS);
1639 case IPV6_AUTOFLOWLABEL:
1640 OPTSET(IN6P_AUTOFLOWLABEL);
1644 OPTSET(INP_BINDANY);
1651 case IPV6_USE_MIN_MTU:
1652 case IPV6_PREFER_TEMPADDR:
1653 if (optlen != sizeof(optval)) {
1657 error = sooptcopyin(sopt, &optval,
1658 sizeof optval, sizeof optval);
1662 struct ip6_pktopts **optp;
1663 optp = &in6p->in6p_outputopts;
1664 error = ip6_pcbopt(optname,
1665 (u_char *)&optval, sizeof(optval),
1666 optp, (td != NULL) ? td->td_ucred :
1671 case IPV6_2292PKTINFO:
1672 case IPV6_2292HOPLIMIT:
1673 case IPV6_2292HOPOPTS:
1674 case IPV6_2292DSTOPTS:
1675 case IPV6_2292RTHDR:
1677 if (optlen != sizeof(int)) {
1681 error = sooptcopyin(sopt, &optval,
1682 sizeof optval, sizeof optval);
1686 case IPV6_2292PKTINFO:
1687 OPTSET2292(IN6P_PKTINFO);
1689 case IPV6_2292HOPLIMIT:
1690 OPTSET2292(IN6P_HOPLIMIT);
1692 case IPV6_2292HOPOPTS:
1694 * Check super-user privilege.
1695 * See comments for IPV6_RECVHOPOPTS.
1698 error = priv_check(td,
1699 PRIV_NETINET_SETHDROPTS);
1703 OPTSET2292(IN6P_HOPOPTS);
1705 case IPV6_2292DSTOPTS:
1707 error = priv_check(td,
1708 PRIV_NETINET_SETHDROPTS);
1712 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1714 case IPV6_2292RTHDR:
1715 OPTSET2292(IN6P_RTHDR);
1723 case IPV6_RTHDRDSTOPTS:
1726 /* new advanced API (RFC3542) */
1728 u_char optbuf_storage[MCLBYTES];
1730 struct ip6_pktopts **optp;
1732 /* cannot mix with RFC2292 */
1733 if (OPTBIT(IN6P_RFC2292)) {
1739 * We only ensure valsize is not too large
1740 * here. Further validation will be done
1743 error = sooptcopyin(sopt, optbuf_storage,
1744 sizeof(optbuf_storage), 0);
1747 optlen = sopt->sopt_valsize;
1748 optbuf = optbuf_storage;
1749 optp = &in6p->in6p_outputopts;
1750 error = ip6_pcbopt(optname, optbuf, optlen,
1751 optp, (td != NULL) ? td->td_ucred : NULL,
1757 case IPV6_MULTICAST_IF:
1758 case IPV6_MULTICAST_HOPS:
1759 case IPV6_MULTICAST_LOOP:
1760 case IPV6_JOIN_GROUP:
1761 case IPV6_LEAVE_GROUP:
1763 case MCAST_BLOCK_SOURCE:
1764 case MCAST_UNBLOCK_SOURCE:
1765 case MCAST_JOIN_GROUP:
1766 case MCAST_LEAVE_GROUP:
1767 case MCAST_JOIN_SOURCE_GROUP:
1768 case MCAST_LEAVE_SOURCE_GROUP:
1769 error = ip6_setmoptions(in6p, sopt);
1772 case IPV6_PORTRANGE:
1773 error = sooptcopyin(sopt, &optval,
1774 sizeof optval, sizeof optval);
1779 case IPV6_PORTRANGE_DEFAULT:
1780 in6p->inp_flags &= ~(INP_LOWPORT);
1781 in6p->inp_flags &= ~(INP_HIGHPORT);
1784 case IPV6_PORTRANGE_HIGH:
1785 in6p->inp_flags &= ~(INP_LOWPORT);
1786 in6p->inp_flags |= INP_HIGHPORT;
1789 case IPV6_PORTRANGE_LOW:
1790 in6p->inp_flags &= ~(INP_HIGHPORT);
1791 in6p->inp_flags |= INP_LOWPORT;
1801 case IPV6_IPSEC_POLICY:
1806 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1808 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1810 req = mtod(m, caddr_t);
1811 error = ipsec_set_policy(in6p, optname, req,
1812 m->m_len, (sopt->sopt_td != NULL) ?
1813 sopt->sopt_td->td_ucred : NULL);
1820 error = ENOPROTOOPT;
1828 case IPV6_2292PKTOPTIONS:
1829 #ifdef IPV6_PKTOPTIONS
1830 case IPV6_PKTOPTIONS:
1833 * RFC3542 (effectively) deprecated the
1834 * semantics of the 2292-style pktoptions.
1835 * Since it was not reliable in nature (i.e.,
1836 * applications had to expect the lack of some
1837 * information after all), it would make sense
1838 * to simplify this part by always returning
1841 sopt->sopt_valsize = 0;
1844 case IPV6_RECVHOPOPTS:
1845 case IPV6_RECVDSTOPTS:
1846 case IPV6_RECVRTHDRDSTOPTS:
1847 case IPV6_UNICAST_HOPS:
1848 case IPV6_RECVPKTINFO:
1849 case IPV6_RECVHOPLIMIT:
1850 case IPV6_RECVRTHDR:
1851 case IPV6_RECVPATHMTU:
1855 case IPV6_PORTRANGE:
1856 case IPV6_RECVTCLASS:
1857 case IPV6_AUTOFLOWLABEL:
1861 case IPV6_RECVHOPOPTS:
1862 optval = OPTBIT(IN6P_HOPOPTS);
1865 case IPV6_RECVDSTOPTS:
1866 optval = OPTBIT(IN6P_DSTOPTS);
1869 case IPV6_RECVRTHDRDSTOPTS:
1870 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1873 case IPV6_UNICAST_HOPS:
1874 optval = in6p->in6p_hops;
1877 case IPV6_RECVPKTINFO:
1878 optval = OPTBIT(IN6P_PKTINFO);
1881 case IPV6_RECVHOPLIMIT:
1882 optval = OPTBIT(IN6P_HOPLIMIT);
1885 case IPV6_RECVRTHDR:
1886 optval = OPTBIT(IN6P_RTHDR);
1889 case IPV6_RECVPATHMTU:
1890 optval = OPTBIT(IN6P_MTU);
1894 optval = OPTBIT(INP_FAITH);
1898 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1901 case IPV6_PORTRANGE:
1904 flags = in6p->inp_flags;
1905 if (flags & INP_HIGHPORT)
1906 optval = IPV6_PORTRANGE_HIGH;
1907 else if (flags & INP_LOWPORT)
1908 optval = IPV6_PORTRANGE_LOW;
1913 case IPV6_RECVTCLASS:
1914 optval = OPTBIT(IN6P_TCLASS);
1917 case IPV6_AUTOFLOWLABEL:
1918 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1922 optval = OPTBIT(INP_BINDANY);
1927 error = sooptcopyout(sopt, &optval,
1934 struct ip6_mtuinfo mtuinfo;
1935 struct route_in6 sro;
1937 bzero(&sro, sizeof(sro));
1939 if (!(so->so_state & SS_ISCONNECTED))
1942 * XXX: we dot not consider the case of source
1943 * routing, or optional information to specify
1944 * the outgoing interface.
1946 error = ip6_getpmtu(&sro, NULL, NULL,
1947 &in6p->in6p_faddr, &pmtu, NULL);
1952 if (pmtu > IPV6_MAXPACKET)
1953 pmtu = IPV6_MAXPACKET;
1955 bzero(&mtuinfo, sizeof(mtuinfo));
1956 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1957 optdata = (void *)&mtuinfo;
1958 optdatalen = sizeof(mtuinfo);
1959 error = sooptcopyout(sopt, optdata,
1964 case IPV6_2292PKTINFO:
1965 case IPV6_2292HOPLIMIT:
1966 case IPV6_2292HOPOPTS:
1967 case IPV6_2292RTHDR:
1968 case IPV6_2292DSTOPTS:
1970 case IPV6_2292PKTINFO:
1971 optval = OPTBIT(IN6P_PKTINFO);
1973 case IPV6_2292HOPLIMIT:
1974 optval = OPTBIT(IN6P_HOPLIMIT);
1976 case IPV6_2292HOPOPTS:
1977 optval = OPTBIT(IN6P_HOPOPTS);
1979 case IPV6_2292RTHDR:
1980 optval = OPTBIT(IN6P_RTHDR);
1982 case IPV6_2292DSTOPTS:
1983 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1986 error = sooptcopyout(sopt, &optval,
1993 case IPV6_RTHDRDSTOPTS:
1997 case IPV6_USE_MIN_MTU:
1998 case IPV6_PREFER_TEMPADDR:
1999 error = ip6_getpcbopt(in6p->in6p_outputopts,
2003 case IPV6_MULTICAST_IF:
2004 case IPV6_MULTICAST_HOPS:
2005 case IPV6_MULTICAST_LOOP:
2007 error = ip6_getmoptions(in6p, sopt);
2011 case IPV6_IPSEC_POLICY:
2015 struct mbuf *m = NULL;
2016 struct mbuf **mp = &m;
2017 size_t ovalsize = sopt->sopt_valsize;
2018 caddr_t oval = (caddr_t)sopt->sopt_val;
2020 error = soopt_getm(sopt, &m); /* XXX */
2023 error = soopt_mcopyin(sopt, m); /* XXX */
2026 sopt->sopt_valsize = ovalsize;
2027 sopt->sopt_val = oval;
2029 req = mtod(m, caddr_t);
2032 error = ipsec_get_policy(in6p, req, len, mp);
2034 error = soopt_mcopyout(sopt, m); /* XXX */
2035 if (error == 0 && m)
2042 error = ENOPROTOOPT;
2047 } else { /* level != IPPROTO_IPV6 */
2054 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2056 int error = 0, optval, optlen;
2057 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2058 struct inpcb *in6p = sotoinpcb(so);
2059 int level, op, optname;
2061 level = sopt->sopt_level;
2062 op = sopt->sopt_dir;
2063 optname = sopt->sopt_name;
2064 optlen = sopt->sopt_valsize;
2066 if (level != IPPROTO_IPV6) {
2073 * For ICMPv6 sockets, no modification allowed for checksum
2074 * offset, permit "no change" values to help existing apps.
2076 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2077 * for an ICMPv6 socket will fail."
2078 * The current behavior does not meet RFC3542.
2082 if (optlen != sizeof(int)) {
2086 error = sooptcopyin(sopt, &optval, sizeof(optval),
2090 if ((optval % 2) != 0) {
2091 /* the API assumes even offset values */
2093 } else if (so->so_proto->pr_protocol ==
2095 if (optval != icmp6off)
2098 in6p->in6p_cksum = optval;
2102 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2105 optval = in6p->in6p_cksum;
2107 error = sooptcopyout(sopt, &optval, sizeof(optval));
2117 error = ENOPROTOOPT;
2125 * Set up IP6 options in pcb for insertion in output packets or
2126 * specifying behavior of outgoing packets.
2129 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2130 struct socket *so, struct sockopt *sopt)
2132 struct ip6_pktopts *opt = *pktopt;
2134 struct thread *td = sopt->sopt_td;
2136 /* turn off any old options. */
2139 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2140 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2141 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2142 printf("ip6_pcbopts: all specified options are cleared.\n");
2144 ip6_clearpktopts(opt, -1);
2146 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2149 if (!m || m->m_len == 0) {
2151 * Only turning off any previous options, regardless of
2152 * whether the opt is just created or given.
2154 free(opt, M_IP6OPT);
2158 /* set options specified by user. */
2159 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2160 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2161 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2162 free(opt, M_IP6OPT);
2170 * initialize ip6_pktopts. beware that there are non-zero default values in
2174 ip6_initpktopts(struct ip6_pktopts *opt)
2177 bzero(opt, sizeof(*opt));
2178 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2179 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2180 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2181 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2185 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2186 struct ucred *cred, int uproto)
2188 struct ip6_pktopts *opt;
2190 if (*pktopt == NULL) {
2191 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2193 ip6_initpktopts(*pktopt);
2197 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2201 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2203 void *optdata = NULL;
2205 struct ip6_ext *ip6e;
2207 struct in6_pktinfo null_pktinfo;
2208 int deftclass = 0, on;
2209 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2210 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2214 if (pktopt && pktopt->ip6po_pktinfo)
2215 optdata = (void *)pktopt->ip6po_pktinfo;
2217 /* XXX: we don't have to do this every time... */
2218 bzero(&null_pktinfo, sizeof(null_pktinfo));
2219 optdata = (void *)&null_pktinfo;
2221 optdatalen = sizeof(struct in6_pktinfo);
2224 if (pktopt && pktopt->ip6po_tclass >= 0)
2225 optdata = (void *)&pktopt->ip6po_tclass;
2227 optdata = (void *)&deftclass;
2228 optdatalen = sizeof(int);
2231 if (pktopt && pktopt->ip6po_hbh) {
2232 optdata = (void *)pktopt->ip6po_hbh;
2233 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2234 optdatalen = (ip6e->ip6e_len + 1) << 3;
2238 if (pktopt && pktopt->ip6po_rthdr) {
2239 optdata = (void *)pktopt->ip6po_rthdr;
2240 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2241 optdatalen = (ip6e->ip6e_len + 1) << 3;
2244 case IPV6_RTHDRDSTOPTS:
2245 if (pktopt && pktopt->ip6po_dest1) {
2246 optdata = (void *)pktopt->ip6po_dest1;
2247 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2248 optdatalen = (ip6e->ip6e_len + 1) << 3;
2252 if (pktopt && pktopt->ip6po_dest2) {
2253 optdata = (void *)pktopt->ip6po_dest2;
2254 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2255 optdatalen = (ip6e->ip6e_len + 1) << 3;
2259 if (pktopt && pktopt->ip6po_nexthop) {
2260 optdata = (void *)pktopt->ip6po_nexthop;
2261 optdatalen = pktopt->ip6po_nexthop->sa_len;
2264 case IPV6_USE_MIN_MTU:
2266 optdata = (void *)&pktopt->ip6po_minmtu;
2268 optdata = (void *)&defminmtu;
2269 optdatalen = sizeof(int);
2272 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2276 optdata = (void *)&on;
2277 optdatalen = sizeof(on);
2279 case IPV6_PREFER_TEMPADDR:
2281 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2283 optdata = (void *)&defpreftemp;
2284 optdatalen = sizeof(int);
2286 default: /* should not happen */
2288 panic("ip6_getpcbopt: unexpected option\n");
2290 return (ENOPROTOOPT);
2293 error = sooptcopyout(sopt, optdata, optdatalen);
2299 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2304 if (optname == -1 || optname == IPV6_PKTINFO) {
2305 if (pktopt->ip6po_pktinfo)
2306 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2307 pktopt->ip6po_pktinfo = NULL;
2309 if (optname == -1 || optname == IPV6_HOPLIMIT)
2310 pktopt->ip6po_hlim = -1;
2311 if (optname == -1 || optname == IPV6_TCLASS)
2312 pktopt->ip6po_tclass = -1;
2313 if (optname == -1 || optname == IPV6_NEXTHOP) {
2314 if (pktopt->ip6po_nextroute.ro_rt) {
2315 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2316 pktopt->ip6po_nextroute.ro_rt = NULL;
2318 if (pktopt->ip6po_nexthop)
2319 free(pktopt->ip6po_nexthop, M_IP6OPT);
2320 pktopt->ip6po_nexthop = NULL;
2322 if (optname == -1 || optname == IPV6_HOPOPTS) {
2323 if (pktopt->ip6po_hbh)
2324 free(pktopt->ip6po_hbh, M_IP6OPT);
2325 pktopt->ip6po_hbh = NULL;
2327 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2328 if (pktopt->ip6po_dest1)
2329 free(pktopt->ip6po_dest1, M_IP6OPT);
2330 pktopt->ip6po_dest1 = NULL;
2332 if (optname == -1 || optname == IPV6_RTHDR) {
2333 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2334 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2335 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2336 if (pktopt->ip6po_route.ro_rt) {
2337 RTFREE(pktopt->ip6po_route.ro_rt);
2338 pktopt->ip6po_route.ro_rt = NULL;
2341 if (optname == -1 || optname == IPV6_DSTOPTS) {
2342 if (pktopt->ip6po_dest2)
2343 free(pktopt->ip6po_dest2, M_IP6OPT);
2344 pktopt->ip6po_dest2 = NULL;
2348 #define PKTOPT_EXTHDRCPY(type) \
2351 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2352 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2353 if (dst->type == NULL && canwait == M_NOWAIT)\
2355 bcopy(src->type, dst->type, hlen);\
2357 } while (/*CONSTCOND*/ 0)
2360 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2362 if (dst == NULL || src == NULL) {
2363 printf("ip6_clearpktopts: invalid argument\n");
2367 dst->ip6po_hlim = src->ip6po_hlim;
2368 dst->ip6po_tclass = src->ip6po_tclass;
2369 dst->ip6po_flags = src->ip6po_flags;
2370 dst->ip6po_minmtu = src->ip6po_minmtu;
2371 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2372 if (src->ip6po_pktinfo) {
2373 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2375 if (dst->ip6po_pktinfo == NULL)
2377 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2379 if (src->ip6po_nexthop) {
2380 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2382 if (dst->ip6po_nexthop == NULL)
2384 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2385 src->ip6po_nexthop->sa_len);
2387 PKTOPT_EXTHDRCPY(ip6po_hbh);
2388 PKTOPT_EXTHDRCPY(ip6po_dest1);
2389 PKTOPT_EXTHDRCPY(ip6po_dest2);
2390 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2394 ip6_clearpktopts(dst, -1);
2397 #undef PKTOPT_EXTHDRCPY
2399 struct ip6_pktopts *
2400 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2403 struct ip6_pktopts *dst;
2405 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2408 ip6_initpktopts(dst);
2410 if ((error = copypktopts(dst, src, canwait)) != 0) {
2411 free(dst, M_IP6OPT);
2419 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2424 ip6_clearpktopts(pktopt, -1);
2426 free(pktopt, M_IP6OPT);
2430 * Set IPv6 outgoing packet options based on advanced API.
2433 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2434 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2436 struct cmsghdr *cm = 0;
2438 if (control == NULL || opt == NULL)
2441 ip6_initpktopts(opt);
2446 * If stickyopt is provided, make a local copy of the options
2447 * for this particular packet, then override them by ancillary
2449 * XXX: copypktopts() does not copy the cached route to a next
2450 * hop (if any). This is not very good in terms of efficiency,
2451 * but we can allow this since this option should be rarely
2454 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2459 * XXX: Currently, we assume all the optional information is stored
2462 if (control->m_next)
2465 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2466 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2469 if (control->m_len < CMSG_LEN(0))
2472 cm = mtod(control, struct cmsghdr *);
2473 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2475 if (cm->cmsg_level != IPPROTO_IPV6)
2478 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2479 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2488 * Set a particular packet option, as a sticky option or an ancillary data
2489 * item. "len" can be 0 only when it's a sticky option.
2490 * We have 4 cases of combination of "sticky" and "cmsg":
2491 * "sticky=0, cmsg=0": impossible
2492 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2493 * "sticky=1, cmsg=0": RFC3542 socket option
2494 * "sticky=1, cmsg=1": RFC2292 socket option
2497 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2498 struct ucred *cred, int sticky, int cmsg, int uproto)
2500 int minmtupolicy, preftemp;
2503 if (!sticky && !cmsg) {
2505 printf("ip6_setpktopt: impossible case\n");
2511 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2512 * not be specified in the context of RFC3542. Conversely,
2513 * RFC3542 types should not be specified in the context of RFC2292.
2517 case IPV6_2292PKTINFO:
2518 case IPV6_2292HOPLIMIT:
2519 case IPV6_2292NEXTHOP:
2520 case IPV6_2292HOPOPTS:
2521 case IPV6_2292DSTOPTS:
2522 case IPV6_2292RTHDR:
2523 case IPV6_2292PKTOPTIONS:
2524 return (ENOPROTOOPT);
2527 if (sticky && cmsg) {
2534 case IPV6_RTHDRDSTOPTS:
2536 case IPV6_USE_MIN_MTU:
2539 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2540 return (ENOPROTOOPT);
2545 case IPV6_2292PKTINFO:
2548 struct ifnet *ifp = NULL;
2549 struct in6_pktinfo *pktinfo;
2551 if (len != sizeof(struct in6_pktinfo))
2554 pktinfo = (struct in6_pktinfo *)buf;
2557 * An application can clear any sticky IPV6_PKTINFO option by
2558 * doing a "regular" setsockopt with ipi6_addr being
2559 * in6addr_any and ipi6_ifindex being zero.
2560 * [RFC 3542, Section 6]
2562 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2563 pktinfo->ipi6_ifindex == 0 &&
2564 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2565 ip6_clearpktopts(opt, optname);
2569 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2570 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2574 /* validate the interface index if specified. */
2575 if (pktinfo->ipi6_ifindex > V_if_index ||
2576 pktinfo->ipi6_ifindex < 0) {
2579 if (pktinfo->ipi6_ifindex) {
2580 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2586 * We store the address anyway, and let in6_selectsrc()
2587 * validate the specified address. This is because ipi6_addr
2588 * may not have enough information about its scope zone, and
2589 * we may need additional information (such as outgoing
2590 * interface or the scope zone of a destination address) to
2591 * disambiguate the scope.
2592 * XXX: the delay of the validation may confuse the
2593 * application when it is used as a sticky option.
2595 if (opt->ip6po_pktinfo == NULL) {
2596 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2597 M_IP6OPT, M_NOWAIT);
2598 if (opt->ip6po_pktinfo == NULL)
2601 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2605 case IPV6_2292HOPLIMIT:
2611 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2612 * to simplify the ordering among hoplimit options.
2614 if (optname == IPV6_HOPLIMIT && sticky)
2615 return (ENOPROTOOPT);
2617 if (len != sizeof(int))
2620 if (*hlimp < -1 || *hlimp > 255)
2623 opt->ip6po_hlim = *hlimp;
2631 if (len != sizeof(int))
2633 tclass = *(int *)buf;
2634 if (tclass < -1 || tclass > 255)
2637 opt->ip6po_tclass = tclass;
2641 case IPV6_2292NEXTHOP:
2644 error = priv_check_cred(cred,
2645 PRIV_NETINET_SETHDROPTS, 0);
2650 if (len == 0) { /* just remove the option */
2651 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2655 /* check if cmsg_len is large enough for sa_len */
2656 if (len < sizeof(struct sockaddr) || len < *buf)
2659 switch (((struct sockaddr *)buf)->sa_family) {
2662 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2665 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2668 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2669 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2672 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2678 case AF_LINK: /* should eventually be supported */
2680 return (EAFNOSUPPORT);
2683 /* turn off the previous option, then set the new option. */
2684 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2685 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2686 if (opt->ip6po_nexthop == NULL)
2688 bcopy(buf, opt->ip6po_nexthop, *buf);
2691 case IPV6_2292HOPOPTS:
2694 struct ip6_hbh *hbh;
2698 * XXX: We don't allow a non-privileged user to set ANY HbH
2699 * options, since per-option restriction has too much
2703 error = priv_check_cred(cred,
2704 PRIV_NETINET_SETHDROPTS, 0);
2710 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2711 break; /* just remove the option */
2714 /* message length validation */
2715 if (len < sizeof(struct ip6_hbh))
2717 hbh = (struct ip6_hbh *)buf;
2718 hbhlen = (hbh->ip6h_len + 1) << 3;
2722 /* turn off the previous option, then set the new option. */
2723 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2724 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2725 if (opt->ip6po_hbh == NULL)
2727 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2732 case IPV6_2292DSTOPTS:
2734 case IPV6_RTHDRDSTOPTS:
2736 struct ip6_dest *dest, **newdest = NULL;
2739 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2740 error = priv_check_cred(cred,
2741 PRIV_NETINET_SETHDROPTS, 0);
2747 ip6_clearpktopts(opt, optname);
2748 break; /* just remove the option */
2751 /* message length validation */
2752 if (len < sizeof(struct ip6_dest))
2754 dest = (struct ip6_dest *)buf;
2755 destlen = (dest->ip6d_len + 1) << 3;
2760 * Determine the position that the destination options header
2761 * should be inserted; before or after the routing header.
2764 case IPV6_2292DSTOPTS:
2766 * The old advacned API is ambiguous on this point.
2767 * Our approach is to determine the position based
2768 * according to the existence of a routing header.
2769 * Note, however, that this depends on the order of the
2770 * extension headers in the ancillary data; the 1st
2771 * part of the destination options header must appear
2772 * before the routing header in the ancillary data,
2774 * RFC3542 solved the ambiguity by introducing
2775 * separate ancillary data or option types.
2777 if (opt->ip6po_rthdr == NULL)
2778 newdest = &opt->ip6po_dest1;
2780 newdest = &opt->ip6po_dest2;
2782 case IPV6_RTHDRDSTOPTS:
2783 newdest = &opt->ip6po_dest1;
2786 newdest = &opt->ip6po_dest2;
2790 /* turn off the previous option, then set the new option. */
2791 ip6_clearpktopts(opt, optname);
2792 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2793 if (*newdest == NULL)
2795 bcopy(dest, *newdest, destlen);
2800 case IPV6_2292RTHDR:
2803 struct ip6_rthdr *rth;
2807 ip6_clearpktopts(opt, IPV6_RTHDR);
2808 break; /* just remove the option */
2811 /* message length validation */
2812 if (len < sizeof(struct ip6_rthdr))
2814 rth = (struct ip6_rthdr *)buf;
2815 rthlen = (rth->ip6r_len + 1) << 3;
2819 switch (rth->ip6r_type) {
2820 case IPV6_RTHDR_TYPE_0:
2821 if (rth->ip6r_len == 0) /* must contain one addr */
2823 if (rth->ip6r_len % 2) /* length must be even */
2825 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2829 return (EINVAL); /* not supported */
2832 /* turn off the previous option */
2833 ip6_clearpktopts(opt, IPV6_RTHDR);
2834 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2835 if (opt->ip6po_rthdr == NULL)
2837 bcopy(rth, opt->ip6po_rthdr, rthlen);
2842 case IPV6_USE_MIN_MTU:
2843 if (len != sizeof(int))
2845 minmtupolicy = *(int *)buf;
2846 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2847 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2848 minmtupolicy != IP6PO_MINMTU_ALL) {
2851 opt->ip6po_minmtu = minmtupolicy;
2855 if (len != sizeof(int))
2858 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2860 * we ignore this option for TCP sockets.
2861 * (RFC3542 leaves this case unspecified.)
2863 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2865 opt->ip6po_flags |= IP6PO_DONTFRAG;
2868 case IPV6_PREFER_TEMPADDR:
2869 if (len != sizeof(int))
2871 preftemp = *(int *)buf;
2872 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2873 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2874 preftemp != IP6PO_TEMPADDR_PREFER) {
2877 opt->ip6po_prefer_tempaddr = preftemp;
2881 return (ENOPROTOOPT);
2882 } /* end of switch */
2888 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2889 * packet to the input queue of a specified interface. Note that this
2890 * calls the output routine of the loopback "driver", but with an interface
2891 * pointer that might NOT be &loif -- easier than replicating that code here.
2894 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2897 struct ip6_hdr *ip6;
2899 copym = m_copy(m, 0, M_COPYALL);
2904 * Make sure to deep-copy IPv6 header portion in case the data
2905 * is in an mbuf cluster, so that we can safely override the IPv6
2906 * header portion later.
2908 if ((copym->m_flags & M_EXT) != 0 ||
2909 copym->m_len < sizeof(struct ip6_hdr)) {
2910 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2916 if (copym->m_len < sizeof(*ip6)) {
2922 ip6 = mtod(copym, struct ip6_hdr *);
2924 * clear embedded scope identifiers if necessary.
2925 * in6_clearscope will touch the addresses only when necessary.
2927 in6_clearscope(&ip6->ip6_src);
2928 in6_clearscope(&ip6->ip6_dst);
2930 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2934 * Chop IPv6 header off from the payload.
2937 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2940 struct ip6_hdr *ip6;
2942 ip6 = mtod(m, struct ip6_hdr *);
2943 if (m->m_len > sizeof(*ip6)) {
2944 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2949 M_MOVE_PKTHDR(mh, m);
2950 MH_ALIGN(mh, sizeof(*ip6));
2951 m->m_len -= sizeof(*ip6);
2952 m->m_data += sizeof(*ip6);
2955 m->m_len = sizeof(*ip6);
2956 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2958 exthdrs->ip6e_ip6 = m;
2963 * Compute IPv6 extension header length.
2966 ip6_optlen(struct inpcb *in6p)
2970 if (!in6p->in6p_outputopts)
2975 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2977 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2978 if (in6p->in6p_outputopts->ip6po_rthdr)
2979 /* dest1 is valid with rthdr only */
2980 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2981 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2982 len += elen(in6p->in6p_outputopts->ip6po_dest2);