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) {
1427 if (sopt->sopt_level == SOL_SOCKET &&
1428 sopt->sopt_dir == SOPT_SET) {
1429 switch (sopt->sopt_name) {
1432 if (IN_MULTICAST(ntohl(in6p->inp_laddr.s_addr))) {
1433 if ((so->so_options &
1434 (SO_REUSEADDR | SO_REUSEPORT)) != 0)
1435 in6p->inp_flags2 |= INP_REUSEPORT;
1437 in6p->inp_flags2 &= ~INP_REUSEPORT;
1444 if ((so->so_options & SO_REUSEPORT) != 0)
1445 in6p->inp_flags2 |= INP_REUSEPORT;
1447 in6p->inp_flags2 &= ~INP_REUSEPORT;
1455 } else { /* level == IPPROTO_IPV6 */
1460 case IPV6_2292PKTOPTIONS:
1461 #ifdef IPV6_PKTOPTIONS
1462 case IPV6_PKTOPTIONS:
1467 error = soopt_getm(sopt, &m); /* XXX */
1470 error = soopt_mcopyin(sopt, m); /* XXX */
1473 error = ip6_pcbopts(&in6p->in6p_outputopts,
1475 m_freem(m); /* XXX */
1480 * Use of some Hop-by-Hop options or some
1481 * Destination options, might require special
1482 * privilege. That is, normal applications
1483 * (without special privilege) might be forbidden
1484 * from setting certain options in outgoing packets,
1485 * and might never see certain options in received
1486 * packets. [RFC 2292 Section 6]
1487 * KAME specific note:
1488 * KAME prevents non-privileged users from sending or
1489 * receiving ANY hbh/dst options in order to avoid
1490 * overhead of parsing options in the kernel.
1492 case IPV6_RECVHOPOPTS:
1493 case IPV6_RECVDSTOPTS:
1494 case IPV6_RECVRTHDRDSTOPTS:
1496 error = priv_check(td,
1497 PRIV_NETINET_SETHDROPTS);
1502 case IPV6_UNICAST_HOPS:
1506 case IPV6_RECVPKTINFO:
1507 case IPV6_RECVHOPLIMIT:
1508 case IPV6_RECVRTHDR:
1509 case IPV6_RECVPATHMTU:
1510 case IPV6_RECVTCLASS:
1512 case IPV6_AUTOFLOWLABEL:
1514 if (optname == IPV6_BINDANY && td != NULL) {
1515 error = priv_check(td,
1516 PRIV_NETINET_BINDANY);
1521 if (optlen != sizeof(int)) {
1525 error = sooptcopyin(sopt, &optval,
1526 sizeof optval, sizeof optval);
1531 case IPV6_UNICAST_HOPS:
1532 if (optval < -1 || optval >= 256)
1535 /* -1 = kernel default */
1536 in6p->in6p_hops = optval;
1537 if ((in6p->inp_vflag &
1539 in6p->inp_ip_ttl = optval;
1542 #define OPTSET(bit) \
1545 in6p->inp_flags |= (bit); \
1547 in6p->inp_flags &= ~(bit); \
1548 } while (/*CONSTCOND*/ 0)
1549 #define OPTSET2292(bit) \
1551 in6p->inp_flags |= IN6P_RFC2292; \
1553 in6p->inp_flags |= (bit); \
1555 in6p->inp_flags &= ~(bit); \
1556 } while (/*CONSTCOND*/ 0)
1557 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1559 case IPV6_RECVPKTINFO:
1560 /* cannot mix with RFC2292 */
1561 if (OPTBIT(IN6P_RFC2292)) {
1565 OPTSET(IN6P_PKTINFO);
1570 struct ip6_pktopts **optp;
1572 /* cannot mix with RFC2292 */
1573 if (OPTBIT(IN6P_RFC2292)) {
1577 optp = &in6p->in6p_outputopts;
1578 error = ip6_pcbopt(IPV6_HOPLIMIT,
1579 (u_char *)&optval, sizeof(optval),
1580 optp, (td != NULL) ? td->td_ucred :
1585 case IPV6_RECVHOPLIMIT:
1586 /* cannot mix with RFC2292 */
1587 if (OPTBIT(IN6P_RFC2292)) {
1591 OPTSET(IN6P_HOPLIMIT);
1594 case IPV6_RECVHOPOPTS:
1595 /* cannot mix with RFC2292 */
1596 if (OPTBIT(IN6P_RFC2292)) {
1600 OPTSET(IN6P_HOPOPTS);
1603 case IPV6_RECVDSTOPTS:
1604 /* cannot mix with RFC2292 */
1605 if (OPTBIT(IN6P_RFC2292)) {
1609 OPTSET(IN6P_DSTOPTS);
1612 case IPV6_RECVRTHDRDSTOPTS:
1613 /* cannot mix with RFC2292 */
1614 if (OPTBIT(IN6P_RFC2292)) {
1618 OPTSET(IN6P_RTHDRDSTOPTS);
1621 case IPV6_RECVRTHDR:
1622 /* cannot mix with RFC2292 */
1623 if (OPTBIT(IN6P_RFC2292)) {
1634 case IPV6_RECVPATHMTU:
1636 * We ignore this option for TCP
1638 * (RFC3542 leaves this case
1641 if (uproto != IPPROTO_TCP)
1647 * make setsockopt(IPV6_V6ONLY)
1648 * available only prior to bind(2).
1649 * see ipng mailing list, Jun 22 2001.
1651 if (in6p->inp_lport ||
1652 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1656 OPTSET(IN6P_IPV6_V6ONLY);
1658 in6p->inp_vflag &= ~INP_IPV4;
1660 in6p->inp_vflag |= INP_IPV4;
1662 case IPV6_RECVTCLASS:
1663 /* cannot mix with RFC2292 XXX */
1664 if (OPTBIT(IN6P_RFC2292)) {
1668 OPTSET(IN6P_TCLASS);
1670 case IPV6_AUTOFLOWLABEL:
1671 OPTSET(IN6P_AUTOFLOWLABEL);
1675 OPTSET(INP_BINDANY);
1682 case IPV6_USE_MIN_MTU:
1683 case IPV6_PREFER_TEMPADDR:
1684 if (optlen != sizeof(optval)) {
1688 error = sooptcopyin(sopt, &optval,
1689 sizeof optval, sizeof optval);
1693 struct ip6_pktopts **optp;
1694 optp = &in6p->in6p_outputopts;
1695 error = ip6_pcbopt(optname,
1696 (u_char *)&optval, sizeof(optval),
1697 optp, (td != NULL) ? td->td_ucred :
1702 case IPV6_2292PKTINFO:
1703 case IPV6_2292HOPLIMIT:
1704 case IPV6_2292HOPOPTS:
1705 case IPV6_2292DSTOPTS:
1706 case IPV6_2292RTHDR:
1708 if (optlen != sizeof(int)) {
1712 error = sooptcopyin(sopt, &optval,
1713 sizeof optval, sizeof optval);
1717 case IPV6_2292PKTINFO:
1718 OPTSET2292(IN6P_PKTINFO);
1720 case IPV6_2292HOPLIMIT:
1721 OPTSET2292(IN6P_HOPLIMIT);
1723 case IPV6_2292HOPOPTS:
1725 * Check super-user privilege.
1726 * See comments for IPV6_RECVHOPOPTS.
1729 error = priv_check(td,
1730 PRIV_NETINET_SETHDROPTS);
1734 OPTSET2292(IN6P_HOPOPTS);
1736 case IPV6_2292DSTOPTS:
1738 error = priv_check(td,
1739 PRIV_NETINET_SETHDROPTS);
1743 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1745 case IPV6_2292RTHDR:
1746 OPTSET2292(IN6P_RTHDR);
1754 case IPV6_RTHDRDSTOPTS:
1757 /* new advanced API (RFC3542) */
1759 u_char optbuf_storage[MCLBYTES];
1761 struct ip6_pktopts **optp;
1763 /* cannot mix with RFC2292 */
1764 if (OPTBIT(IN6P_RFC2292)) {
1770 * We only ensure valsize is not too large
1771 * here. Further validation will be done
1774 error = sooptcopyin(sopt, optbuf_storage,
1775 sizeof(optbuf_storage), 0);
1778 optlen = sopt->sopt_valsize;
1779 optbuf = optbuf_storage;
1780 optp = &in6p->in6p_outputopts;
1781 error = ip6_pcbopt(optname, optbuf, optlen,
1782 optp, (td != NULL) ? td->td_ucred : NULL,
1788 case IPV6_MULTICAST_IF:
1789 case IPV6_MULTICAST_HOPS:
1790 case IPV6_MULTICAST_LOOP:
1791 case IPV6_JOIN_GROUP:
1792 case IPV6_LEAVE_GROUP:
1794 case MCAST_BLOCK_SOURCE:
1795 case MCAST_UNBLOCK_SOURCE:
1796 case MCAST_JOIN_GROUP:
1797 case MCAST_LEAVE_GROUP:
1798 case MCAST_JOIN_SOURCE_GROUP:
1799 case MCAST_LEAVE_SOURCE_GROUP:
1800 error = ip6_setmoptions(in6p, sopt);
1803 case IPV6_PORTRANGE:
1804 error = sooptcopyin(sopt, &optval,
1805 sizeof optval, sizeof optval);
1810 case IPV6_PORTRANGE_DEFAULT:
1811 in6p->inp_flags &= ~(INP_LOWPORT);
1812 in6p->inp_flags &= ~(INP_HIGHPORT);
1815 case IPV6_PORTRANGE_HIGH:
1816 in6p->inp_flags &= ~(INP_LOWPORT);
1817 in6p->inp_flags |= INP_HIGHPORT;
1820 case IPV6_PORTRANGE_LOW:
1821 in6p->inp_flags &= ~(INP_HIGHPORT);
1822 in6p->inp_flags |= INP_LOWPORT;
1832 case IPV6_IPSEC_POLICY:
1837 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1839 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1841 req = mtod(m, caddr_t);
1842 error = ipsec_set_policy(in6p, optname, req,
1843 m->m_len, (sopt->sopt_td != NULL) ?
1844 sopt->sopt_td->td_ucred : NULL);
1851 error = ENOPROTOOPT;
1859 case IPV6_2292PKTOPTIONS:
1860 #ifdef IPV6_PKTOPTIONS
1861 case IPV6_PKTOPTIONS:
1864 * RFC3542 (effectively) deprecated the
1865 * semantics of the 2292-style pktoptions.
1866 * Since it was not reliable in nature (i.e.,
1867 * applications had to expect the lack of some
1868 * information after all), it would make sense
1869 * to simplify this part by always returning
1872 sopt->sopt_valsize = 0;
1875 case IPV6_RECVHOPOPTS:
1876 case IPV6_RECVDSTOPTS:
1877 case IPV6_RECVRTHDRDSTOPTS:
1878 case IPV6_UNICAST_HOPS:
1879 case IPV6_RECVPKTINFO:
1880 case IPV6_RECVHOPLIMIT:
1881 case IPV6_RECVRTHDR:
1882 case IPV6_RECVPATHMTU:
1886 case IPV6_PORTRANGE:
1887 case IPV6_RECVTCLASS:
1888 case IPV6_AUTOFLOWLABEL:
1892 case IPV6_RECVHOPOPTS:
1893 optval = OPTBIT(IN6P_HOPOPTS);
1896 case IPV6_RECVDSTOPTS:
1897 optval = OPTBIT(IN6P_DSTOPTS);
1900 case IPV6_RECVRTHDRDSTOPTS:
1901 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1904 case IPV6_UNICAST_HOPS:
1905 optval = in6p->in6p_hops;
1908 case IPV6_RECVPKTINFO:
1909 optval = OPTBIT(IN6P_PKTINFO);
1912 case IPV6_RECVHOPLIMIT:
1913 optval = OPTBIT(IN6P_HOPLIMIT);
1916 case IPV6_RECVRTHDR:
1917 optval = OPTBIT(IN6P_RTHDR);
1920 case IPV6_RECVPATHMTU:
1921 optval = OPTBIT(IN6P_MTU);
1925 optval = OPTBIT(INP_FAITH);
1929 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1932 case IPV6_PORTRANGE:
1935 flags = in6p->inp_flags;
1936 if (flags & INP_HIGHPORT)
1937 optval = IPV6_PORTRANGE_HIGH;
1938 else if (flags & INP_LOWPORT)
1939 optval = IPV6_PORTRANGE_LOW;
1944 case IPV6_RECVTCLASS:
1945 optval = OPTBIT(IN6P_TCLASS);
1948 case IPV6_AUTOFLOWLABEL:
1949 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1953 optval = OPTBIT(INP_BINDANY);
1958 error = sooptcopyout(sopt, &optval,
1965 struct ip6_mtuinfo mtuinfo;
1966 struct route_in6 sro;
1968 bzero(&sro, sizeof(sro));
1970 if (!(so->so_state & SS_ISCONNECTED))
1973 * XXX: we dot not consider the case of source
1974 * routing, or optional information to specify
1975 * the outgoing interface.
1977 error = ip6_getpmtu(&sro, NULL, NULL,
1978 &in6p->in6p_faddr, &pmtu, NULL);
1983 if (pmtu > IPV6_MAXPACKET)
1984 pmtu = IPV6_MAXPACKET;
1986 bzero(&mtuinfo, sizeof(mtuinfo));
1987 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1988 optdata = (void *)&mtuinfo;
1989 optdatalen = sizeof(mtuinfo);
1990 error = sooptcopyout(sopt, optdata,
1995 case IPV6_2292PKTINFO:
1996 case IPV6_2292HOPLIMIT:
1997 case IPV6_2292HOPOPTS:
1998 case IPV6_2292RTHDR:
1999 case IPV6_2292DSTOPTS:
2001 case IPV6_2292PKTINFO:
2002 optval = OPTBIT(IN6P_PKTINFO);
2004 case IPV6_2292HOPLIMIT:
2005 optval = OPTBIT(IN6P_HOPLIMIT);
2007 case IPV6_2292HOPOPTS:
2008 optval = OPTBIT(IN6P_HOPOPTS);
2010 case IPV6_2292RTHDR:
2011 optval = OPTBIT(IN6P_RTHDR);
2013 case IPV6_2292DSTOPTS:
2014 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2017 error = sooptcopyout(sopt, &optval,
2024 case IPV6_RTHDRDSTOPTS:
2028 case IPV6_USE_MIN_MTU:
2029 case IPV6_PREFER_TEMPADDR:
2030 error = ip6_getpcbopt(in6p->in6p_outputopts,
2034 case IPV6_MULTICAST_IF:
2035 case IPV6_MULTICAST_HOPS:
2036 case IPV6_MULTICAST_LOOP:
2038 error = ip6_getmoptions(in6p, sopt);
2042 case IPV6_IPSEC_POLICY:
2046 struct mbuf *m = NULL;
2047 struct mbuf **mp = &m;
2048 size_t ovalsize = sopt->sopt_valsize;
2049 caddr_t oval = (caddr_t)sopt->sopt_val;
2051 error = soopt_getm(sopt, &m); /* XXX */
2054 error = soopt_mcopyin(sopt, m); /* XXX */
2057 sopt->sopt_valsize = ovalsize;
2058 sopt->sopt_val = oval;
2060 req = mtod(m, caddr_t);
2063 error = ipsec_get_policy(in6p, req, len, mp);
2065 error = soopt_mcopyout(sopt, m); /* XXX */
2066 if (error == 0 && m)
2073 error = ENOPROTOOPT;
2083 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2085 int error = 0, optval, optlen;
2086 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2087 struct inpcb *in6p = sotoinpcb(so);
2088 int level, op, optname;
2090 level = sopt->sopt_level;
2091 op = sopt->sopt_dir;
2092 optname = sopt->sopt_name;
2093 optlen = sopt->sopt_valsize;
2095 if (level != IPPROTO_IPV6) {
2102 * For ICMPv6 sockets, no modification allowed for checksum
2103 * offset, permit "no change" values to help existing apps.
2105 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2106 * for an ICMPv6 socket will fail."
2107 * The current behavior does not meet RFC3542.
2111 if (optlen != sizeof(int)) {
2115 error = sooptcopyin(sopt, &optval, sizeof(optval),
2119 if ((optval % 2) != 0) {
2120 /* the API assumes even offset values */
2122 } else if (so->so_proto->pr_protocol ==
2124 if (optval != icmp6off)
2127 in6p->in6p_cksum = optval;
2131 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2134 optval = in6p->in6p_cksum;
2136 error = sooptcopyout(sopt, &optval, sizeof(optval));
2146 error = ENOPROTOOPT;
2154 * Set up IP6 options in pcb for insertion in output packets or
2155 * specifying behavior of outgoing packets.
2158 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2159 struct socket *so, struct sockopt *sopt)
2161 struct ip6_pktopts *opt = *pktopt;
2163 struct thread *td = sopt->sopt_td;
2165 /* turn off any old options. */
2168 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2169 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2170 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2171 printf("ip6_pcbopts: all specified options are cleared.\n");
2173 ip6_clearpktopts(opt, -1);
2175 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2178 if (!m || m->m_len == 0) {
2180 * Only turning off any previous options, regardless of
2181 * whether the opt is just created or given.
2183 free(opt, M_IP6OPT);
2187 /* set options specified by user. */
2188 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2189 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2190 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2191 free(opt, M_IP6OPT);
2199 * initialize ip6_pktopts. beware that there are non-zero default values in
2203 ip6_initpktopts(struct ip6_pktopts *opt)
2206 bzero(opt, sizeof(*opt));
2207 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2208 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2209 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2210 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2214 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2215 struct ucred *cred, int uproto)
2217 struct ip6_pktopts *opt;
2219 if (*pktopt == NULL) {
2220 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2222 ip6_initpktopts(*pktopt);
2226 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2230 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2232 void *optdata = NULL;
2234 struct ip6_ext *ip6e;
2236 struct in6_pktinfo null_pktinfo;
2237 int deftclass = 0, on;
2238 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2239 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2243 if (pktopt && pktopt->ip6po_pktinfo)
2244 optdata = (void *)pktopt->ip6po_pktinfo;
2246 /* XXX: we don't have to do this every time... */
2247 bzero(&null_pktinfo, sizeof(null_pktinfo));
2248 optdata = (void *)&null_pktinfo;
2250 optdatalen = sizeof(struct in6_pktinfo);
2253 if (pktopt && pktopt->ip6po_tclass >= 0)
2254 optdata = (void *)&pktopt->ip6po_tclass;
2256 optdata = (void *)&deftclass;
2257 optdatalen = sizeof(int);
2260 if (pktopt && pktopt->ip6po_hbh) {
2261 optdata = (void *)pktopt->ip6po_hbh;
2262 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2263 optdatalen = (ip6e->ip6e_len + 1) << 3;
2267 if (pktopt && pktopt->ip6po_rthdr) {
2268 optdata = (void *)pktopt->ip6po_rthdr;
2269 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2270 optdatalen = (ip6e->ip6e_len + 1) << 3;
2273 case IPV6_RTHDRDSTOPTS:
2274 if (pktopt && pktopt->ip6po_dest1) {
2275 optdata = (void *)pktopt->ip6po_dest1;
2276 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2277 optdatalen = (ip6e->ip6e_len + 1) << 3;
2281 if (pktopt && pktopt->ip6po_dest2) {
2282 optdata = (void *)pktopt->ip6po_dest2;
2283 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2284 optdatalen = (ip6e->ip6e_len + 1) << 3;
2288 if (pktopt && pktopt->ip6po_nexthop) {
2289 optdata = (void *)pktopt->ip6po_nexthop;
2290 optdatalen = pktopt->ip6po_nexthop->sa_len;
2293 case IPV6_USE_MIN_MTU:
2295 optdata = (void *)&pktopt->ip6po_minmtu;
2297 optdata = (void *)&defminmtu;
2298 optdatalen = sizeof(int);
2301 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2305 optdata = (void *)&on;
2306 optdatalen = sizeof(on);
2308 case IPV6_PREFER_TEMPADDR:
2310 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2312 optdata = (void *)&defpreftemp;
2313 optdatalen = sizeof(int);
2315 default: /* should not happen */
2317 panic("ip6_getpcbopt: unexpected option\n");
2319 return (ENOPROTOOPT);
2322 error = sooptcopyout(sopt, optdata, optdatalen);
2328 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2333 if (optname == -1 || optname == IPV6_PKTINFO) {
2334 if (pktopt->ip6po_pktinfo)
2335 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2336 pktopt->ip6po_pktinfo = NULL;
2338 if (optname == -1 || optname == IPV6_HOPLIMIT)
2339 pktopt->ip6po_hlim = -1;
2340 if (optname == -1 || optname == IPV6_TCLASS)
2341 pktopt->ip6po_tclass = -1;
2342 if (optname == -1 || optname == IPV6_NEXTHOP) {
2343 if (pktopt->ip6po_nextroute.ro_rt) {
2344 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2345 pktopt->ip6po_nextroute.ro_rt = NULL;
2347 if (pktopt->ip6po_nexthop)
2348 free(pktopt->ip6po_nexthop, M_IP6OPT);
2349 pktopt->ip6po_nexthop = NULL;
2351 if (optname == -1 || optname == IPV6_HOPOPTS) {
2352 if (pktopt->ip6po_hbh)
2353 free(pktopt->ip6po_hbh, M_IP6OPT);
2354 pktopt->ip6po_hbh = NULL;
2356 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2357 if (pktopt->ip6po_dest1)
2358 free(pktopt->ip6po_dest1, M_IP6OPT);
2359 pktopt->ip6po_dest1 = NULL;
2361 if (optname == -1 || optname == IPV6_RTHDR) {
2362 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2363 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2364 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2365 if (pktopt->ip6po_route.ro_rt) {
2366 RTFREE(pktopt->ip6po_route.ro_rt);
2367 pktopt->ip6po_route.ro_rt = NULL;
2370 if (optname == -1 || optname == IPV6_DSTOPTS) {
2371 if (pktopt->ip6po_dest2)
2372 free(pktopt->ip6po_dest2, M_IP6OPT);
2373 pktopt->ip6po_dest2 = NULL;
2377 #define PKTOPT_EXTHDRCPY(type) \
2380 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2381 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2382 if (dst->type == NULL && canwait == M_NOWAIT)\
2384 bcopy(src->type, dst->type, hlen);\
2386 } while (/*CONSTCOND*/ 0)
2389 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2391 if (dst == NULL || src == NULL) {
2392 printf("ip6_clearpktopts: invalid argument\n");
2396 dst->ip6po_hlim = src->ip6po_hlim;
2397 dst->ip6po_tclass = src->ip6po_tclass;
2398 dst->ip6po_flags = src->ip6po_flags;
2399 dst->ip6po_minmtu = src->ip6po_minmtu;
2400 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2401 if (src->ip6po_pktinfo) {
2402 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2404 if (dst->ip6po_pktinfo == NULL)
2406 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2408 if (src->ip6po_nexthop) {
2409 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2411 if (dst->ip6po_nexthop == NULL)
2413 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2414 src->ip6po_nexthop->sa_len);
2416 PKTOPT_EXTHDRCPY(ip6po_hbh);
2417 PKTOPT_EXTHDRCPY(ip6po_dest1);
2418 PKTOPT_EXTHDRCPY(ip6po_dest2);
2419 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2423 ip6_clearpktopts(dst, -1);
2426 #undef PKTOPT_EXTHDRCPY
2428 struct ip6_pktopts *
2429 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2432 struct ip6_pktopts *dst;
2434 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2437 ip6_initpktopts(dst);
2439 if ((error = copypktopts(dst, src, canwait)) != 0) {
2440 free(dst, M_IP6OPT);
2448 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2453 ip6_clearpktopts(pktopt, -1);
2455 free(pktopt, M_IP6OPT);
2459 * Set IPv6 outgoing packet options based on advanced API.
2462 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2463 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2465 struct cmsghdr *cm = 0;
2467 if (control == NULL || opt == NULL)
2470 ip6_initpktopts(opt);
2475 * If stickyopt is provided, make a local copy of the options
2476 * for this particular packet, then override them by ancillary
2478 * XXX: copypktopts() does not copy the cached route to a next
2479 * hop (if any). This is not very good in terms of efficiency,
2480 * but we can allow this since this option should be rarely
2483 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2488 * XXX: Currently, we assume all the optional information is stored
2491 if (control->m_next)
2494 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2495 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2498 if (control->m_len < CMSG_LEN(0))
2501 cm = mtod(control, struct cmsghdr *);
2502 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2504 if (cm->cmsg_level != IPPROTO_IPV6)
2507 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2508 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2517 * Set a particular packet option, as a sticky option or an ancillary data
2518 * item. "len" can be 0 only when it's a sticky option.
2519 * We have 4 cases of combination of "sticky" and "cmsg":
2520 * "sticky=0, cmsg=0": impossible
2521 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2522 * "sticky=1, cmsg=0": RFC3542 socket option
2523 * "sticky=1, cmsg=1": RFC2292 socket option
2526 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2527 struct ucred *cred, int sticky, int cmsg, int uproto)
2529 int minmtupolicy, preftemp;
2532 if (!sticky && !cmsg) {
2534 printf("ip6_setpktopt: impossible case\n");
2540 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2541 * not be specified in the context of RFC3542. Conversely,
2542 * RFC3542 types should not be specified in the context of RFC2292.
2546 case IPV6_2292PKTINFO:
2547 case IPV6_2292HOPLIMIT:
2548 case IPV6_2292NEXTHOP:
2549 case IPV6_2292HOPOPTS:
2550 case IPV6_2292DSTOPTS:
2551 case IPV6_2292RTHDR:
2552 case IPV6_2292PKTOPTIONS:
2553 return (ENOPROTOOPT);
2556 if (sticky && cmsg) {
2563 case IPV6_RTHDRDSTOPTS:
2565 case IPV6_USE_MIN_MTU:
2568 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2569 return (ENOPROTOOPT);
2574 case IPV6_2292PKTINFO:
2577 struct ifnet *ifp = NULL;
2578 struct in6_pktinfo *pktinfo;
2580 if (len != sizeof(struct in6_pktinfo))
2583 pktinfo = (struct in6_pktinfo *)buf;
2586 * An application can clear any sticky IPV6_PKTINFO option by
2587 * doing a "regular" setsockopt with ipi6_addr being
2588 * in6addr_any and ipi6_ifindex being zero.
2589 * [RFC 3542, Section 6]
2591 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2592 pktinfo->ipi6_ifindex == 0 &&
2593 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2594 ip6_clearpktopts(opt, optname);
2598 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2599 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2603 /* validate the interface index if specified. */
2604 if (pktinfo->ipi6_ifindex > V_if_index ||
2605 pktinfo->ipi6_ifindex < 0) {
2608 if (pktinfo->ipi6_ifindex) {
2609 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2615 * We store the address anyway, and let in6_selectsrc()
2616 * validate the specified address. This is because ipi6_addr
2617 * may not have enough information about its scope zone, and
2618 * we may need additional information (such as outgoing
2619 * interface or the scope zone of a destination address) to
2620 * disambiguate the scope.
2621 * XXX: the delay of the validation may confuse the
2622 * application when it is used as a sticky option.
2624 if (opt->ip6po_pktinfo == NULL) {
2625 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2626 M_IP6OPT, M_NOWAIT);
2627 if (opt->ip6po_pktinfo == NULL)
2630 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2634 case IPV6_2292HOPLIMIT:
2640 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2641 * to simplify the ordering among hoplimit options.
2643 if (optname == IPV6_HOPLIMIT && sticky)
2644 return (ENOPROTOOPT);
2646 if (len != sizeof(int))
2649 if (*hlimp < -1 || *hlimp > 255)
2652 opt->ip6po_hlim = *hlimp;
2660 if (len != sizeof(int))
2662 tclass = *(int *)buf;
2663 if (tclass < -1 || tclass > 255)
2666 opt->ip6po_tclass = tclass;
2670 case IPV6_2292NEXTHOP:
2673 error = priv_check_cred(cred,
2674 PRIV_NETINET_SETHDROPTS, 0);
2679 if (len == 0) { /* just remove the option */
2680 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2684 /* check if cmsg_len is large enough for sa_len */
2685 if (len < sizeof(struct sockaddr) || len < *buf)
2688 switch (((struct sockaddr *)buf)->sa_family) {
2691 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2694 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2697 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2698 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2701 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2707 case AF_LINK: /* should eventually be supported */
2709 return (EAFNOSUPPORT);
2712 /* turn off the previous option, then set the new option. */
2713 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2714 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2715 if (opt->ip6po_nexthop == NULL)
2717 bcopy(buf, opt->ip6po_nexthop, *buf);
2720 case IPV6_2292HOPOPTS:
2723 struct ip6_hbh *hbh;
2727 * XXX: We don't allow a non-privileged user to set ANY HbH
2728 * options, since per-option restriction has too much
2732 error = priv_check_cred(cred,
2733 PRIV_NETINET_SETHDROPTS, 0);
2739 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2740 break; /* just remove the option */
2743 /* message length validation */
2744 if (len < sizeof(struct ip6_hbh))
2746 hbh = (struct ip6_hbh *)buf;
2747 hbhlen = (hbh->ip6h_len + 1) << 3;
2751 /* turn off the previous option, then set the new option. */
2752 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2753 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2754 if (opt->ip6po_hbh == NULL)
2756 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2761 case IPV6_2292DSTOPTS:
2763 case IPV6_RTHDRDSTOPTS:
2765 struct ip6_dest *dest, **newdest = NULL;
2768 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2769 error = priv_check_cred(cred,
2770 PRIV_NETINET_SETHDROPTS, 0);
2776 ip6_clearpktopts(opt, optname);
2777 break; /* just remove the option */
2780 /* message length validation */
2781 if (len < sizeof(struct ip6_dest))
2783 dest = (struct ip6_dest *)buf;
2784 destlen = (dest->ip6d_len + 1) << 3;
2789 * Determine the position that the destination options header
2790 * should be inserted; before or after the routing header.
2793 case IPV6_2292DSTOPTS:
2795 * The old advacned API is ambiguous on this point.
2796 * Our approach is to determine the position based
2797 * according to the existence of a routing header.
2798 * Note, however, that this depends on the order of the
2799 * extension headers in the ancillary data; the 1st
2800 * part of the destination options header must appear
2801 * before the routing header in the ancillary data,
2803 * RFC3542 solved the ambiguity by introducing
2804 * separate ancillary data or option types.
2806 if (opt->ip6po_rthdr == NULL)
2807 newdest = &opt->ip6po_dest1;
2809 newdest = &opt->ip6po_dest2;
2811 case IPV6_RTHDRDSTOPTS:
2812 newdest = &opt->ip6po_dest1;
2815 newdest = &opt->ip6po_dest2;
2819 /* turn off the previous option, then set the new option. */
2820 ip6_clearpktopts(opt, optname);
2821 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2822 if (*newdest == NULL)
2824 bcopy(dest, *newdest, destlen);
2829 case IPV6_2292RTHDR:
2832 struct ip6_rthdr *rth;
2836 ip6_clearpktopts(opt, IPV6_RTHDR);
2837 break; /* just remove the option */
2840 /* message length validation */
2841 if (len < sizeof(struct ip6_rthdr))
2843 rth = (struct ip6_rthdr *)buf;
2844 rthlen = (rth->ip6r_len + 1) << 3;
2848 switch (rth->ip6r_type) {
2849 case IPV6_RTHDR_TYPE_0:
2850 if (rth->ip6r_len == 0) /* must contain one addr */
2852 if (rth->ip6r_len % 2) /* length must be even */
2854 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2858 return (EINVAL); /* not supported */
2861 /* turn off the previous option */
2862 ip6_clearpktopts(opt, IPV6_RTHDR);
2863 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2864 if (opt->ip6po_rthdr == NULL)
2866 bcopy(rth, opt->ip6po_rthdr, rthlen);
2871 case IPV6_USE_MIN_MTU:
2872 if (len != sizeof(int))
2874 minmtupolicy = *(int *)buf;
2875 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2876 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2877 minmtupolicy != IP6PO_MINMTU_ALL) {
2880 opt->ip6po_minmtu = minmtupolicy;
2884 if (len != sizeof(int))
2887 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2889 * we ignore this option for TCP sockets.
2890 * (RFC3542 leaves this case unspecified.)
2892 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2894 opt->ip6po_flags |= IP6PO_DONTFRAG;
2897 case IPV6_PREFER_TEMPADDR:
2898 if (len != sizeof(int))
2900 preftemp = *(int *)buf;
2901 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2902 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2903 preftemp != IP6PO_TEMPADDR_PREFER) {
2906 opt->ip6po_prefer_tempaddr = preftemp;
2910 return (ENOPROTOOPT);
2911 } /* end of switch */
2917 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2918 * packet to the input queue of a specified interface. Note that this
2919 * calls the output routine of the loopback "driver", but with an interface
2920 * pointer that might NOT be &loif -- easier than replicating that code here.
2923 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2926 struct ip6_hdr *ip6;
2928 copym = m_copy(m, 0, M_COPYALL);
2933 * Make sure to deep-copy IPv6 header portion in case the data
2934 * is in an mbuf cluster, so that we can safely override the IPv6
2935 * header portion later.
2937 if ((copym->m_flags & M_EXT) != 0 ||
2938 copym->m_len < sizeof(struct ip6_hdr)) {
2939 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2945 if (copym->m_len < sizeof(*ip6)) {
2951 ip6 = mtod(copym, struct ip6_hdr *);
2953 * clear embedded scope identifiers if necessary.
2954 * in6_clearscope will touch the addresses only when necessary.
2956 in6_clearscope(&ip6->ip6_src);
2957 in6_clearscope(&ip6->ip6_dst);
2959 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2963 * Chop IPv6 header off from the payload.
2966 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2969 struct ip6_hdr *ip6;
2971 ip6 = mtod(m, struct ip6_hdr *);
2972 if (m->m_len > sizeof(*ip6)) {
2973 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2978 M_MOVE_PKTHDR(mh, m);
2979 MH_ALIGN(mh, sizeof(*ip6));
2980 m->m_len -= sizeof(*ip6);
2981 m->m_data += sizeof(*ip6);
2984 m->m_len = sizeof(*ip6);
2985 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2987 exthdrs->ip6e_ip6 = m;
2992 * Compute IPv6 extension header length.
2995 ip6_optlen(struct inpcb *in6p)
2999 if (!in6p->in6p_outputopts)
3004 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3006 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3007 if (in6p->in6p_outputopts->ip6po_rthdr)
3008 /* dest1 is valid with rthdr only */
3009 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3010 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3011 len += elen(in6p->in6p_outputopts->ip6po_dest2);