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"
68 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/errno.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/syslog.h>
81 #include <sys/ucred.h>
84 #include <net/netisr.h>
85 #include <net/route.h>
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet6/in6_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet/icmp6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet6/nd6.h>
100 #include <netipsec/ipsec.h>
101 #include <netipsec/ipsec6.h>
102 #include <netipsec/key.h>
103 #include <netinet6/ip6_ipsec.h>
106 #include <netinet6/ip6protosw.h>
107 #include <netinet6/scope6_var.h>
109 extern int in6_mcast_loop;
112 struct mbuf *ip6e_ip6;
113 struct mbuf *ip6e_hbh;
114 struct mbuf *ip6e_dest1;
115 struct mbuf *ip6e_rthdr;
116 struct mbuf *ip6e_dest2;
119 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
120 struct ucred *, int));
121 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
122 struct socket *, struct sockopt *));
123 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
124 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *,
125 struct ucred *, int, int, int));
127 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
128 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
129 struct ip6_frag **));
130 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
131 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
132 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
133 struct ifnet *, struct in6_addr *, u_long *, int *));
134 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
138 * Make an extension header from option data. hp is the source, and
139 * mp is the destination.
141 #define MAKE_EXTHDR(hp, mp) \
144 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
145 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
146 ((eh)->ip6e_len + 1) << 3); \
150 } while (/*CONSTCOND*/ 0)
153 * Form a chain of extension headers.
154 * m is the extension header mbuf
155 * mp is the previous mbuf in the chain
156 * p is the next header
157 * i is the type of option.
159 #define MAKE_CHAIN(m, mp, p, i)\
163 panic("assumption failed: hdr not split"); \
164 *mtod((m), u_char *) = *(p);\
166 p = mtod((m), u_char *);\
167 (m)->m_next = (mp)->m_next;\
171 } while (/*CONSTCOND*/ 0)
174 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
175 * header (with pri, len, nxt, hlim, src, dst).
176 * This function may modify ver and hlim only.
177 * The mbuf chain containing the packet will be freed.
178 * The mbuf opt, if present, will not be freed.
180 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
181 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
182 * which is rt_rmx.rmx_mtu.
184 * ifpp - XXX: just for statistics
187 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
188 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
189 struct ifnet **ifpp, struct inpcb *inp)
191 struct ip6_hdr *ip6, *mhip6;
192 struct ifnet *ifp, *origifp;
194 struct mbuf *mprev = NULL;
195 int hlen, tlen, len, off;
196 struct route_in6 ip6route;
197 struct rtentry *rt = NULL;
198 struct sockaddr_in6 *dst, src_sa, dst_sa;
199 struct in6_addr odst;
201 struct in6_ifaddr *ia = NULL;
203 int alwaysfrag, dontfrag;
204 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
205 struct ip6_exthdrs exthdrs;
206 struct in6_addr finaldst, src0, dst0;
208 struct route_in6 *ro_pmtu = NULL;
212 struct ipsec_output_state state;
213 struct ip6_rthdr *rh = NULL;
214 int needipsectun = 0;
216 struct secpolicy *sp = NULL;
219 ip6 = mtod(m, struct ip6_hdr *);
221 printf ("ip6 is NULL");
225 finaldst = ip6->ip6_dst;
227 bzero(&exthdrs, sizeof(exthdrs));
230 /* Hop-by-Hop options header */
231 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
232 /* Destination options header(1st part) */
233 if (opt->ip6po_rthdr) {
235 * Destination options header(1st part)
236 * This only makes sense with a routing header.
237 * See Section 9.2 of RFC 3542.
238 * Disabling this part just for MIP6 convenience is
239 * a bad idea. We need to think carefully about a
240 * way to make the advanced API coexist with MIP6
241 * options, which might automatically be inserted in
244 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
247 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
248 /* Destination options header(2nd part) */
249 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
253 * IPSec checking which handles several cases.
254 * FAST IPSEC: We re-injected the packet.
257 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
259 case 1: /* Bad packet */
261 case -1: /* Do IPSec */
263 case 0: /* No IPSec */
270 * Calculate the total length of the extension header chain.
271 * Keep the length of the unfragmentable part for fragmentation.
274 if (exthdrs.ip6e_hbh)
275 optlen += exthdrs.ip6e_hbh->m_len;
276 if (exthdrs.ip6e_dest1)
277 optlen += exthdrs.ip6e_dest1->m_len;
278 if (exthdrs.ip6e_rthdr)
279 optlen += exthdrs.ip6e_rthdr->m_len;
280 unfragpartlen = optlen + sizeof(struct ip6_hdr);
282 /* NOTE: we don't add AH/ESP length here. do that later. */
283 if (exthdrs.ip6e_dest2)
284 optlen += exthdrs.ip6e_dest2->m_len;
287 * If we need IPsec, or there is at least one extension header,
288 * separate IP6 header from the payload.
290 if ((needipsec || optlen) && !hdrsplit) {
291 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
295 m = exthdrs.ip6e_ip6;
300 ip6 = mtod(m, struct ip6_hdr *);
302 /* adjust mbuf packet header length */
303 m->m_pkthdr.len += optlen;
304 plen = m->m_pkthdr.len - sizeof(*ip6);
306 /* If this is a jumbo payload, insert a jumbo payload option. */
307 if (plen > IPV6_MAXPACKET) {
309 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
313 m = exthdrs.ip6e_ip6;
317 ip6 = mtod(m, struct ip6_hdr *);
318 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
322 ip6->ip6_plen = htons(plen);
325 * Concatenate headers and fill in next header fields.
326 * Here we have, on "m"
328 * and we insert headers accordingly. Finally, we should be getting:
329 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
331 * during the header composing process, "m" points to IPv6 header.
332 * "mprev" points to an extension header prior to esp.
334 u_char *nexthdrp = &ip6->ip6_nxt;
338 * we treat dest2 specially. this makes IPsec processing
339 * much easier. the goal here is to make mprev point the
340 * mbuf prior to dest2.
342 * result: IPv6 dest2 payload
343 * m and mprev will point to IPv6 header.
345 if (exthdrs.ip6e_dest2) {
347 panic("assumption failed: hdr not split");
348 exthdrs.ip6e_dest2->m_next = m->m_next;
349 m->m_next = exthdrs.ip6e_dest2;
350 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
351 ip6->ip6_nxt = IPPROTO_DSTOPTS;
355 * result: IPv6 hbh dest1 rthdr dest2 payload
356 * m will point to IPv6 header. mprev will point to the
357 * extension header prior to dest2 (rthdr in the above case).
359 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
360 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
362 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
370 * pointers after IPsec headers are not valid any more.
371 * other pointers need a great care too.
372 * (IPsec routines should not mangle mbufs prior to AH/ESP)
374 exthdrs.ip6e_dest2 = NULL;
376 if (exthdrs.ip6e_rthdr) {
377 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
378 segleft_org = rh->ip6r_segleft;
379 rh->ip6r_segleft = 0;
382 bzero(&state, sizeof(state));
384 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
387 if (error == EJUSTRETURN) {
389 * We had a SP with a level of 'use' and no SA. We
390 * will just continue to process the packet without
395 /* mbuf is already reclaimed in ipsec6_output_trans. */
405 printf("[%s:%d] (ipsec): error code %d\n",
406 __func__, __LINE__, error);
409 /* don't show these error codes to the user */
414 } else if (!needipsectun) {
416 * In the FAST IPSec case we have already
417 * re-injected the packet and it has been freed
418 * by the ipsec_done() function. So, just clean
419 * up after ourselves.
424 if (exthdrs.ip6e_rthdr) {
425 /* ah6_output doesn't modify mbuf chain */
426 rh->ip6r_segleft = segleft_org;
432 * If there is a routing header, discard the packet.
434 if (exthdrs.ip6e_rthdr) {
439 /* Source address validation */
440 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
441 (flags & IPV6_UNSPECSRC) == 0) {
443 V_ip6stat.ip6s_badscope++;
446 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
448 V_ip6stat.ip6s_badscope++;
452 V_ip6stat.ip6s_localout++;
459 bzero((caddr_t)ro, sizeof(*ro));
462 if (opt && opt->ip6po_rthdr)
463 ro = &opt->ip6po_route;
464 dst = (struct sockaddr_in6 *)&ro->ro_dst;
468 * if specified, try to fill in the traffic class field.
469 * do not override if a non-zero value is already set.
470 * we check the diffserv field and the ecn field separately.
472 if (opt && opt->ip6po_tclass >= 0) {
475 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
477 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
480 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
483 /* fill in or override the hop limit field, if necessary. */
484 if (opt && opt->ip6po_hlim != -1)
485 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
486 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
488 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
490 ip6->ip6_hlim = V_ip6_defmcasthlim;
495 * We may re-inject packets into the stack here.
497 if (needipsec && needipsectun) {
498 struct ipsec_output_state state;
501 * All the extension headers will become inaccessible
502 * (since they can be encrypted).
503 * Don't panic, we need no more updates to extension headers
504 * on inner IPv6 packet (since they are now encapsulated).
506 * IPv6 [ESP|AH] IPv6 [extension headers] payload
508 bzero(&exthdrs, sizeof(exthdrs));
509 exthdrs.ip6e_ip6 = m;
511 bzero(&state, sizeof(state));
513 state.ro = (struct route *)ro;
514 state.dst = (struct sockaddr *)dst;
516 error = ipsec6_output_tunnel(&state, sp, flags);
519 ro = (struct route_in6 *)state.ro;
520 dst = (struct sockaddr_in6 *)state.dst;
521 if (error == EJUSTRETURN) {
523 * We had a SP with a level of 'use' and no SA. We
524 * will just continue to process the packet without
529 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
540 printf("[%s:%d] (ipsec): error code %d\n",
541 __func__, __LINE__, error);
544 /* don't show these error codes to the user */
551 * In the FAST IPSec case we have already
552 * re-injected the packet and it has been freed
553 * by the ipsec_done() function. So, just clean
554 * up after ourselves.
560 exthdrs.ip6e_ip6 = m;
565 ip6 = mtod(m, struct ip6_hdr *);
567 bzero(&dst_sa, sizeof(dst_sa));
568 dst_sa.sin6_family = AF_INET6;
569 dst_sa.sin6_len = sizeof(dst_sa);
570 dst_sa.sin6_addr = ip6->ip6_dst;
571 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
575 V_ip6stat.ip6s_noroute++;
579 break; /* XXX statistics? */
582 in6_ifstat_inc(ifp, ifs6_out_discard);
587 * If in6_selectroute() does not return a route entry,
588 * dst may not have been updated.
590 *dst = dst_sa; /* XXX */
594 * then rt (for unicast) and ifp must be non-NULL valid values.
596 if ((flags & IPV6_FORWARDING) == 0) {
597 /* XXX: the FORWARDING flag can be set for mrouting. */
598 in6_ifstat_inc(ifp, ifs6_out_request);
601 ia = (struct in6_ifaddr *)(rt->rt_ifa);
607 * The outgoing interface must be in the zone of source and
608 * destination addresses.
613 if (in6_setscope(&src0, origifp, &zone))
615 bzero(&src_sa, sizeof(src_sa));
616 src_sa.sin6_family = AF_INET6;
617 src_sa.sin6_len = sizeof(src_sa);
618 src_sa.sin6_addr = ip6->ip6_src;
619 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
623 if (in6_setscope(&dst0, origifp, &zone))
625 /* re-initialize to be sure */
626 bzero(&dst_sa, sizeof(dst_sa));
627 dst_sa.sin6_family = AF_INET6;
628 dst_sa.sin6_len = sizeof(dst_sa);
629 dst_sa.sin6_addr = ip6->ip6_dst;
630 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
634 /* We should use ia_ifp to support the case of
635 * sending packets to an address of our own.
637 if (ia != NULL && ia->ia_ifp)
640 /* scope check is done. */
644 V_ip6stat.ip6s_badscope++;
645 in6_ifstat_inc(origifp, ifs6_out_discard);
647 error = EHOSTUNREACH; /* XXX */
651 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
652 if (opt && opt->ip6po_nextroute.ro_rt) {
654 * The nexthop is explicitly specified by the
655 * application. We assume the next hop is an IPv6
658 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
660 else if ((rt->rt_flags & RTF_GATEWAY))
661 dst = (struct sockaddr_in6 *)rt->rt_gateway;
664 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
665 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
667 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
668 in6_ifstat_inc(ifp, ifs6_out_mcast);
670 * Confirm that the outgoing interface supports multicast.
672 if (!(ifp->if_flags & IFF_MULTICAST)) {
673 V_ip6stat.ip6s_noroute++;
674 in6_ifstat_inc(ifp, ifs6_out_discard);
678 if ((im6o == NULL && in6_mcast_loop) ||
679 (im6o && im6o->im6o_multicast_loop)) {
681 * Loop back multicast datagram if not expressly
682 * forbidden to do so, even if we have not joined
683 * the address; protocols will filter it later,
684 * thus deferring a hash lookup and lock acquisition
685 * at the expense of an m_copym().
687 ip6_mloopback(ifp, m, dst);
690 * If we are acting as a multicast router, perform
691 * multicast forwarding as if the packet had just
692 * arrived on the interface to which we are about
693 * to send. The multicast forwarding function
694 * recursively calls this function, using the
695 * IPV6_FORWARDING flag to prevent infinite recursion.
697 * Multicasts that are looped back by ip6_mloopback(),
698 * above, will be forwarded by the ip6_input() routine,
701 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
703 * XXX: ip6_mforward expects that rcvif is NULL
704 * when it is called from the originating path.
705 * However, it is not always the case, since
706 * some versions of MGETHDR() does not
707 * initialize the field.
709 m->m_pkthdr.rcvif = NULL;
710 if (ip6_mforward(ip6, ifp, m) != 0) {
717 * Multicasts with a hoplimit of zero may be looped back,
718 * above, but must not be transmitted on a network.
719 * Also, multicasts addressed to the loopback interface
720 * are not sent -- the above call to ip6_mloopback() will
721 * loop back a copy if this host actually belongs to the
722 * destination group on the loopback interface.
724 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
725 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
732 * Fill the outgoing inteface to tell the upper layer
733 * to increment per-interface statistics.
738 /* Determine path MTU. */
739 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
744 * The caller of this function may specify to use the minimum MTU
746 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
747 * setting. The logic is a bit complicated; by default, unicast
748 * packets will follow path MTU while multicast packets will be sent at
749 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
750 * including unicast ones will be sent at the minimum MTU. Multicast
751 * packets will always be sent at the minimum MTU unless
752 * IP6PO_MINMTU_DISABLE is explicitly specified.
753 * See RFC 3542 for more details.
755 if (mtu > IPV6_MMTU) {
756 if ((flags & IPV6_MINMTU))
758 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
760 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
762 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
768 * clear embedded scope identifiers if necessary.
769 * in6_clearscope will touch the addresses only when necessary.
771 in6_clearscope(&ip6->ip6_src);
772 in6_clearscope(&ip6->ip6_dst);
775 * If the outgoing packet contains a hop-by-hop options header,
776 * it must be examined and processed even by the source node.
777 * (RFC 2460, section 4.)
779 if (exthdrs.ip6e_hbh) {
780 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
781 u_int32_t dummy; /* XXX unused */
782 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
785 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
786 panic("ip6e_hbh is not continuous");
789 * XXX: if we have to send an ICMPv6 error to the sender,
790 * we need the M_LOOP flag since icmp6_error() expects
791 * the IPv6 and the hop-by-hop options header are
792 * continuous unless the flag is set.
794 m->m_flags |= M_LOOP;
795 m->m_pkthdr.rcvif = ifp;
796 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
797 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
798 &dummy, &plen) < 0) {
799 /* m was already freed at this point */
800 error = EINVAL;/* better error? */
803 m->m_flags &= ~M_LOOP; /* XXX */
804 m->m_pkthdr.rcvif = NULL;
807 /* Jump over all PFIL processing if hooks are not active. */
808 if (!PFIL_HOOKED(&inet6_pfil_hook))
812 /* Run through list of hooks for output packets. */
813 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
814 if (error != 0 || m == NULL)
816 ip6 = mtod(m, struct ip6_hdr *);
818 /* See if destination IP address was changed by packet filter. */
819 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
820 m->m_flags |= M_SKIP_FIREWALL;
821 /* If destination is now ourself drop to ip6_input(). */
822 if (in6_localaddr(&ip6->ip6_dst)) {
823 if (m->m_pkthdr.rcvif == NULL)
824 m->m_pkthdr.rcvif = V_loif;
825 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
826 m->m_pkthdr.csum_flags |=
827 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
828 m->m_pkthdr.csum_data = 0xffff;
830 m->m_pkthdr.csum_flags |=
831 CSUM_IP_CHECKED | CSUM_IP_VALID;
832 error = netisr_queue(NETISR_IPV6, m);
835 goto again; /* Redo the routing table lookup. */
838 /* XXX: IPFIREWALL_FORWARD */
842 * Send the packet to the outgoing interface.
843 * If necessary, do IPv6 fragmentation before sending.
845 * the logic here is rather complex:
846 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
847 * 1-a: send as is if tlen <= path mtu
848 * 1-b: fragment if tlen > path mtu
850 * 2: if user asks us not to fragment (dontfrag == 1)
851 * 2-a: send as is if tlen <= interface mtu
852 * 2-b: error if tlen > interface mtu
854 * 3: if we always need to attach fragment header (alwaysfrag == 1)
857 * 4: if dontfrag == 1 && alwaysfrag == 1
858 * error, as we cannot handle this conflicting request
860 tlen = m->m_pkthdr.len;
862 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
866 if (dontfrag && alwaysfrag) { /* case 4 */
867 /* conflicting request - can't transmit */
871 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
873 * Even if the DONTFRAG option is specified, we cannot send the
874 * packet when the data length is larger than the MTU of the
875 * outgoing interface.
876 * Notify the error by sending IPV6_PATHMTU ancillary data as
877 * well as returning an error code (the latter is not described
881 struct ip6ctlparam ip6cp;
883 mtu32 = (u_int32_t)mtu;
884 bzero(&ip6cp, sizeof(ip6cp));
885 ip6cp.ip6c_cmdarg = (void *)&mtu32;
886 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
894 * transmit packet without fragmentation
896 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
897 struct in6_ifaddr *ia6;
899 ip6 = mtod(m, struct ip6_hdr *);
900 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
902 /* Record statistics for this interface address. */
903 ia6->ia_ifa.if_opackets++;
904 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
905 ifa_free(&ia6->ia_ifa);
907 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
912 * try to fragment the packet. case 1-b and 3
914 if (mtu < IPV6_MMTU) {
915 /* path MTU cannot be less than IPV6_MMTU */
917 in6_ifstat_inc(ifp, ifs6_out_fragfail);
919 } else if (ip6->ip6_plen == 0) {
920 /* jumbo payload cannot be fragmented */
922 in6_ifstat_inc(ifp, ifs6_out_fragfail);
925 struct mbuf **mnext, *m_frgpart;
926 struct ip6_frag *ip6f;
927 u_int32_t id = htonl(ip6_randomid());
930 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
933 * Too large for the destination or interface;
934 * fragment if possible.
935 * Must be able to put at least 8 bytes per fragment.
937 hlen = unfragpartlen;
938 if (mtu > IPV6_MAXPACKET)
939 mtu = IPV6_MAXPACKET;
941 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
944 in6_ifstat_inc(ifp, ifs6_out_fragfail);
949 * Verify that we have any chance at all of being able to queue
950 * the packet or packet fragments
952 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
953 < tlen /* - hlen */)) {
955 V_ip6stat.ip6s_odropped++;
959 mnext = &m->m_nextpkt;
962 * Change the next header field of the last header in the
963 * unfragmentable part.
965 if (exthdrs.ip6e_rthdr) {
966 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
967 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
968 } else if (exthdrs.ip6e_dest1) {
969 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
970 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
971 } else if (exthdrs.ip6e_hbh) {
972 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
973 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
975 nextproto = ip6->ip6_nxt;
976 ip6->ip6_nxt = IPPROTO_FRAGMENT;
980 * Loop through length of segment after first fragment,
981 * make new header and copy data of each part and link onto
985 for (off = hlen; off < tlen; off += len) {
986 MGETHDR(m, M_DONTWAIT, MT_HEADER);
989 V_ip6stat.ip6s_odropped++;
992 m->m_pkthdr.rcvif = NULL;
993 m->m_flags = m0->m_flags & M_COPYFLAGS;
995 mnext = &m->m_nextpkt;
996 m->m_data += max_linkhdr;
997 mhip6 = mtod(m, struct ip6_hdr *);
999 m->m_len = sizeof(*mhip6);
1000 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1002 V_ip6stat.ip6s_odropped++;
1005 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1006 if (off + len >= tlen)
1009 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1010 mhip6->ip6_plen = htons((u_short)(len + hlen +
1011 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1012 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1014 V_ip6stat.ip6s_odropped++;
1017 m_cat(m, m_frgpart);
1018 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1019 m->m_pkthdr.rcvif = NULL;
1020 ip6f->ip6f_reserved = 0;
1021 ip6f->ip6f_ident = id;
1022 ip6f->ip6f_nxt = nextproto;
1023 V_ip6stat.ip6s_ofragments++;
1024 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1027 in6_ifstat_inc(ifp, ifs6_out_fragok);
1031 * Remove leading garbages.
1037 for (m0 = m; m; m = m0) {
1041 /* Record statistics for this interface address. */
1043 ia->ia_ifa.if_opackets++;
1044 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1046 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1052 V_ip6stat.ip6s_fragmented++;
1055 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1057 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1058 RTFREE(ro_pmtu->ro_rt);
1068 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1069 m_freem(exthdrs.ip6e_dest1);
1070 m_freem(exthdrs.ip6e_rthdr);
1071 m_freem(exthdrs.ip6e_dest2);
1080 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1084 if (hlen > MCLBYTES)
1085 return (ENOBUFS); /* XXX */
1087 MGET(m, M_DONTWAIT, MT_DATA);
1092 MCLGET(m, M_DONTWAIT);
1093 if ((m->m_flags & M_EXT) == 0) {
1100 bcopy(hdr, mtod(m, caddr_t), hlen);
1107 * Insert jumbo payload option.
1110 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1116 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1119 * If there is no hop-by-hop options header, allocate new one.
1120 * If there is one but it doesn't have enough space to store the
1121 * jumbo payload option, allocate a cluster to store the whole options.
1122 * Otherwise, use it to store the options.
1124 if (exthdrs->ip6e_hbh == 0) {
1125 MGET(mopt, M_DONTWAIT, MT_DATA);
1128 mopt->m_len = JUMBOOPTLEN;
1129 optbuf = mtod(mopt, u_char *);
1130 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1131 exthdrs->ip6e_hbh = mopt;
1133 struct ip6_hbh *hbh;
1135 mopt = exthdrs->ip6e_hbh;
1136 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1139 * - exthdrs->ip6e_hbh is not referenced from places
1140 * other than exthdrs.
1141 * - exthdrs->ip6e_hbh is not an mbuf chain.
1143 int oldoptlen = mopt->m_len;
1147 * XXX: give up if the whole (new) hbh header does
1148 * not fit even in an mbuf cluster.
1150 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1154 * As a consequence, we must always prepare a cluster
1157 MGET(n, M_DONTWAIT, MT_DATA);
1159 MCLGET(n, M_DONTWAIT);
1160 if ((n->m_flags & M_EXT) == 0) {
1167 n->m_len = oldoptlen + JUMBOOPTLEN;
1168 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1170 optbuf = mtod(n, caddr_t) + oldoptlen;
1172 mopt = exthdrs->ip6e_hbh = n;
1174 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1175 mopt->m_len += JUMBOOPTLEN;
1177 optbuf[0] = IP6OPT_PADN;
1181 * Adjust the header length according to the pad and
1182 * the jumbo payload option.
1184 hbh = mtod(mopt, struct ip6_hbh *);
1185 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1188 /* fill in the option. */
1189 optbuf[2] = IP6OPT_JUMBO;
1191 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1192 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1194 /* finally, adjust the packet header length */
1195 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1202 * Insert fragment header and copy unfragmentable header portions.
1205 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1206 struct ip6_frag **frghdrp)
1208 struct mbuf *n, *mlast;
1210 if (hlen > sizeof(struct ip6_hdr)) {
1211 n = m_copym(m0, sizeof(struct ip6_hdr),
1212 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1219 /* Search for the last mbuf of unfragmentable part. */
1220 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1223 if ((mlast->m_flags & M_EXT) == 0 &&
1224 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1225 /* use the trailing space of the last mbuf for the fragment hdr */
1226 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1228 mlast->m_len += sizeof(struct ip6_frag);
1229 m->m_pkthdr.len += sizeof(struct ip6_frag);
1231 /* allocate a new mbuf for the fragment header */
1234 MGET(mfrg, M_DONTWAIT, MT_DATA);
1237 mfrg->m_len = sizeof(struct ip6_frag);
1238 *frghdrp = mtod(mfrg, struct ip6_frag *);
1239 mlast->m_next = mfrg;
1246 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1247 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1254 if (ro_pmtu != ro) {
1255 /* The first hop and the final destination may differ. */
1256 struct sockaddr_in6 *sa6_dst =
1257 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1258 if (ro_pmtu->ro_rt &&
1259 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1260 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1261 RTFREE(ro_pmtu->ro_rt);
1262 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1264 if (ro_pmtu->ro_rt == NULL) {
1265 bzero(sa6_dst, sizeof(*sa6_dst));
1266 sa6_dst->sin6_family = AF_INET6;
1267 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1268 sa6_dst->sin6_addr = *dst;
1270 rtalloc((struct route *)ro_pmtu);
1273 if (ro_pmtu->ro_rt) {
1275 struct in_conninfo inc;
1277 bzero(&inc, sizeof(inc));
1278 inc.inc_flags |= INC_ISIPV6;
1279 inc.inc6_faddr = *dst;
1282 ifp = ro_pmtu->ro_rt->rt_ifp;
1283 ifmtu = IN6_LINKMTU(ifp);
1284 mtu = tcp_hc_getmtu(&inc);
1286 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1288 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1291 else if (mtu < IPV6_MMTU) {
1293 * RFC2460 section 5, last paragraph:
1294 * if we record ICMPv6 too big message with
1295 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1296 * or smaller, with framgent header attached.
1297 * (fragment header is needed regardless from the
1298 * packet size, for translators to identify packets)
1302 } else if (mtu > ifmtu) {
1304 * The MTU on the route is larger than the MTU on
1305 * the interface! This shouldn't happen, unless the
1306 * MTU of the interface has been changed after the
1307 * interface was brought up. Change the MTU in the
1308 * route to match the interface MTU (as long as the
1309 * field isn't locked).
1312 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1315 mtu = IN6_LINKMTU(ifp);
1317 error = EHOSTUNREACH; /* XXX */
1321 *alwaysfragp = alwaysfrag;
1326 * IP6 socket option processing.
1329 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1331 int optdatalen, uproto;
1333 struct inpcb *in6p = sotoinpcb(so);
1335 int level, op, optname;
1339 level = sopt->sopt_level;
1340 op = sopt->sopt_dir;
1341 optname = sopt->sopt_name;
1342 optlen = sopt->sopt_valsize;
1346 uproto = (int)so->so_proto->pr_protocol;
1348 if (level == IPPROTO_IPV6) {
1353 case IPV6_2292PKTOPTIONS:
1354 #ifdef IPV6_PKTOPTIONS
1355 case IPV6_PKTOPTIONS:
1360 error = soopt_getm(sopt, &m); /* XXX */
1363 error = soopt_mcopyin(sopt, m); /* XXX */
1366 error = ip6_pcbopts(&in6p->in6p_outputopts,
1368 m_freem(m); /* XXX */
1373 * Use of some Hop-by-Hop options or some
1374 * Destination options, might require special
1375 * privilege. That is, normal applications
1376 * (without special privilege) might be forbidden
1377 * from setting certain options in outgoing packets,
1378 * and might never see certain options in received
1379 * packets. [RFC 2292 Section 6]
1380 * KAME specific note:
1381 * KAME prevents non-privileged users from sending or
1382 * receiving ANY hbh/dst options in order to avoid
1383 * overhead of parsing options in the kernel.
1385 case IPV6_RECVHOPOPTS:
1386 case IPV6_RECVDSTOPTS:
1387 case IPV6_RECVRTHDRDSTOPTS:
1389 error = priv_check(td,
1390 PRIV_NETINET_SETHDROPTS);
1395 case IPV6_UNICAST_HOPS:
1399 case IPV6_RECVPKTINFO:
1400 case IPV6_RECVHOPLIMIT:
1401 case IPV6_RECVRTHDR:
1402 case IPV6_RECVPATHMTU:
1403 case IPV6_RECVTCLASS:
1405 case IPV6_AUTOFLOWLABEL:
1407 if (optname == IPV6_BINDANY && td != NULL) {
1408 error = priv_check(td,
1409 PRIV_NETINET_BINDANY);
1414 if (optlen != sizeof(int)) {
1418 error = sooptcopyin(sopt, &optval,
1419 sizeof optval, sizeof optval);
1424 case IPV6_UNICAST_HOPS:
1425 if (optval < -1 || optval >= 256)
1428 /* -1 = kernel default */
1429 in6p->in6p_hops = optval;
1430 if ((in6p->inp_vflag &
1432 in6p->inp_ip_ttl = optval;
1435 #define OPTSET(bit) \
1438 in6p->inp_flags |= (bit); \
1440 in6p->inp_flags &= ~(bit); \
1441 } while (/*CONSTCOND*/ 0)
1442 #define OPTSET2292(bit) \
1444 in6p->inp_flags |= IN6P_RFC2292; \
1446 in6p->inp_flags |= (bit); \
1448 in6p->inp_flags &= ~(bit); \
1449 } while (/*CONSTCOND*/ 0)
1450 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1452 case IPV6_RECVPKTINFO:
1453 /* cannot mix with RFC2292 */
1454 if (OPTBIT(IN6P_RFC2292)) {
1458 OPTSET(IN6P_PKTINFO);
1463 struct ip6_pktopts **optp;
1465 /* cannot mix with RFC2292 */
1466 if (OPTBIT(IN6P_RFC2292)) {
1470 optp = &in6p->in6p_outputopts;
1471 error = ip6_pcbopt(IPV6_HOPLIMIT,
1472 (u_char *)&optval, sizeof(optval),
1473 optp, (td != NULL) ? td->td_ucred :
1478 case IPV6_RECVHOPLIMIT:
1479 /* cannot mix with RFC2292 */
1480 if (OPTBIT(IN6P_RFC2292)) {
1484 OPTSET(IN6P_HOPLIMIT);
1487 case IPV6_RECVHOPOPTS:
1488 /* cannot mix with RFC2292 */
1489 if (OPTBIT(IN6P_RFC2292)) {
1493 OPTSET(IN6P_HOPOPTS);
1496 case IPV6_RECVDSTOPTS:
1497 /* cannot mix with RFC2292 */
1498 if (OPTBIT(IN6P_RFC2292)) {
1502 OPTSET(IN6P_DSTOPTS);
1505 case IPV6_RECVRTHDRDSTOPTS:
1506 /* cannot mix with RFC2292 */
1507 if (OPTBIT(IN6P_RFC2292)) {
1511 OPTSET(IN6P_RTHDRDSTOPTS);
1514 case IPV6_RECVRTHDR:
1515 /* cannot mix with RFC2292 */
1516 if (OPTBIT(IN6P_RFC2292)) {
1527 case IPV6_RECVPATHMTU:
1529 * We ignore this option for TCP
1531 * (RFC3542 leaves this case
1534 if (uproto != IPPROTO_TCP)
1540 * make setsockopt(IPV6_V6ONLY)
1541 * available only prior to bind(2).
1542 * see ipng mailing list, Jun 22 2001.
1544 if (in6p->inp_lport ||
1545 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1549 OPTSET(IN6P_IPV6_V6ONLY);
1551 in6p->inp_vflag &= ~INP_IPV4;
1553 in6p->inp_vflag |= INP_IPV4;
1555 case IPV6_RECVTCLASS:
1556 /* cannot mix with RFC2292 XXX */
1557 if (OPTBIT(IN6P_RFC2292)) {
1561 OPTSET(IN6P_TCLASS);
1563 case IPV6_AUTOFLOWLABEL:
1564 OPTSET(IN6P_AUTOFLOWLABEL);
1568 OPTSET(INP_BINDANY);
1575 case IPV6_USE_MIN_MTU:
1576 case IPV6_PREFER_TEMPADDR:
1577 if (optlen != sizeof(optval)) {
1581 error = sooptcopyin(sopt, &optval,
1582 sizeof optval, sizeof optval);
1586 struct ip6_pktopts **optp;
1587 optp = &in6p->in6p_outputopts;
1588 error = ip6_pcbopt(optname,
1589 (u_char *)&optval, sizeof(optval),
1590 optp, (td != NULL) ? td->td_ucred :
1595 case IPV6_2292PKTINFO:
1596 case IPV6_2292HOPLIMIT:
1597 case IPV6_2292HOPOPTS:
1598 case IPV6_2292DSTOPTS:
1599 case IPV6_2292RTHDR:
1601 if (optlen != sizeof(int)) {
1605 error = sooptcopyin(sopt, &optval,
1606 sizeof optval, sizeof optval);
1610 case IPV6_2292PKTINFO:
1611 OPTSET2292(IN6P_PKTINFO);
1613 case IPV6_2292HOPLIMIT:
1614 OPTSET2292(IN6P_HOPLIMIT);
1616 case IPV6_2292HOPOPTS:
1618 * Check super-user privilege.
1619 * See comments for IPV6_RECVHOPOPTS.
1622 error = priv_check(td,
1623 PRIV_NETINET_SETHDROPTS);
1627 OPTSET2292(IN6P_HOPOPTS);
1629 case IPV6_2292DSTOPTS:
1631 error = priv_check(td,
1632 PRIV_NETINET_SETHDROPTS);
1636 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1638 case IPV6_2292RTHDR:
1639 OPTSET2292(IN6P_RTHDR);
1647 case IPV6_RTHDRDSTOPTS:
1650 /* new advanced API (RFC3542) */
1652 u_char optbuf_storage[MCLBYTES];
1654 struct ip6_pktopts **optp;
1656 /* cannot mix with RFC2292 */
1657 if (OPTBIT(IN6P_RFC2292)) {
1663 * We only ensure valsize is not too large
1664 * here. Further validation will be done
1667 error = sooptcopyin(sopt, optbuf_storage,
1668 sizeof(optbuf_storage), 0);
1671 optlen = sopt->sopt_valsize;
1672 optbuf = optbuf_storage;
1673 optp = &in6p->in6p_outputopts;
1674 error = ip6_pcbopt(optname, optbuf, optlen,
1675 optp, (td != NULL) ? td->td_ucred : NULL,
1681 case IPV6_MULTICAST_IF:
1682 case IPV6_MULTICAST_HOPS:
1683 case IPV6_MULTICAST_LOOP:
1684 case IPV6_JOIN_GROUP:
1685 case IPV6_LEAVE_GROUP:
1687 case MCAST_BLOCK_SOURCE:
1688 case MCAST_UNBLOCK_SOURCE:
1689 case MCAST_JOIN_GROUP:
1690 case MCAST_LEAVE_GROUP:
1691 case MCAST_JOIN_SOURCE_GROUP:
1692 case MCAST_LEAVE_SOURCE_GROUP:
1693 error = ip6_setmoptions(in6p, sopt);
1696 case IPV6_PORTRANGE:
1697 error = sooptcopyin(sopt, &optval,
1698 sizeof optval, sizeof optval);
1703 case IPV6_PORTRANGE_DEFAULT:
1704 in6p->inp_flags &= ~(INP_LOWPORT);
1705 in6p->inp_flags &= ~(INP_HIGHPORT);
1708 case IPV6_PORTRANGE_HIGH:
1709 in6p->inp_flags &= ~(INP_LOWPORT);
1710 in6p->inp_flags |= INP_HIGHPORT;
1713 case IPV6_PORTRANGE_LOW:
1714 in6p->inp_flags &= ~(INP_HIGHPORT);
1715 in6p->inp_flags |= INP_LOWPORT;
1725 case IPV6_IPSEC_POLICY:
1730 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1732 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1734 req = mtod(m, caddr_t);
1735 error = ipsec_set_policy(in6p, optname, req,
1736 m->m_len, (sopt->sopt_td != NULL) ?
1737 sopt->sopt_td->td_ucred : NULL);
1744 error = ENOPROTOOPT;
1752 case IPV6_2292PKTOPTIONS:
1753 #ifdef IPV6_PKTOPTIONS
1754 case IPV6_PKTOPTIONS:
1757 * RFC3542 (effectively) deprecated the
1758 * semantics of the 2292-style pktoptions.
1759 * Since it was not reliable in nature (i.e.,
1760 * applications had to expect the lack of some
1761 * information after all), it would make sense
1762 * to simplify this part by always returning
1765 sopt->sopt_valsize = 0;
1768 case IPV6_RECVHOPOPTS:
1769 case IPV6_RECVDSTOPTS:
1770 case IPV6_RECVRTHDRDSTOPTS:
1771 case IPV6_UNICAST_HOPS:
1772 case IPV6_RECVPKTINFO:
1773 case IPV6_RECVHOPLIMIT:
1774 case IPV6_RECVRTHDR:
1775 case IPV6_RECVPATHMTU:
1779 case IPV6_PORTRANGE:
1780 case IPV6_RECVTCLASS:
1781 case IPV6_AUTOFLOWLABEL:
1784 case IPV6_RECVHOPOPTS:
1785 optval = OPTBIT(IN6P_HOPOPTS);
1788 case IPV6_RECVDSTOPTS:
1789 optval = OPTBIT(IN6P_DSTOPTS);
1792 case IPV6_RECVRTHDRDSTOPTS:
1793 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1796 case IPV6_UNICAST_HOPS:
1797 optval = in6p->in6p_hops;
1800 case IPV6_RECVPKTINFO:
1801 optval = OPTBIT(IN6P_PKTINFO);
1804 case IPV6_RECVHOPLIMIT:
1805 optval = OPTBIT(IN6P_HOPLIMIT);
1808 case IPV6_RECVRTHDR:
1809 optval = OPTBIT(IN6P_RTHDR);
1812 case IPV6_RECVPATHMTU:
1813 optval = OPTBIT(IN6P_MTU);
1817 optval = OPTBIT(INP_FAITH);
1821 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1824 case IPV6_PORTRANGE:
1827 flags = in6p->inp_flags;
1828 if (flags & INP_HIGHPORT)
1829 optval = IPV6_PORTRANGE_HIGH;
1830 else if (flags & INP_LOWPORT)
1831 optval = IPV6_PORTRANGE_LOW;
1836 case IPV6_RECVTCLASS:
1837 optval = OPTBIT(IN6P_TCLASS);
1840 case IPV6_AUTOFLOWLABEL:
1841 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1845 optval = OPTBIT(INP_BINDANY);
1850 error = sooptcopyout(sopt, &optval,
1857 struct ip6_mtuinfo mtuinfo;
1858 struct route_in6 sro;
1860 bzero(&sro, sizeof(sro));
1862 if (!(so->so_state & SS_ISCONNECTED))
1865 * XXX: we dot not consider the case of source
1866 * routing, or optional information to specify
1867 * the outgoing interface.
1869 error = ip6_getpmtu(&sro, NULL, NULL,
1870 &in6p->in6p_faddr, &pmtu, NULL);
1875 if (pmtu > IPV6_MAXPACKET)
1876 pmtu = IPV6_MAXPACKET;
1878 bzero(&mtuinfo, sizeof(mtuinfo));
1879 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1880 optdata = (void *)&mtuinfo;
1881 optdatalen = sizeof(mtuinfo);
1882 error = sooptcopyout(sopt, optdata,
1887 case IPV6_2292PKTINFO:
1888 case IPV6_2292HOPLIMIT:
1889 case IPV6_2292HOPOPTS:
1890 case IPV6_2292RTHDR:
1891 case IPV6_2292DSTOPTS:
1893 case IPV6_2292PKTINFO:
1894 optval = OPTBIT(IN6P_PKTINFO);
1896 case IPV6_2292HOPLIMIT:
1897 optval = OPTBIT(IN6P_HOPLIMIT);
1899 case IPV6_2292HOPOPTS:
1900 optval = OPTBIT(IN6P_HOPOPTS);
1902 case IPV6_2292RTHDR:
1903 optval = OPTBIT(IN6P_RTHDR);
1905 case IPV6_2292DSTOPTS:
1906 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1909 error = sooptcopyout(sopt, &optval,
1916 case IPV6_RTHDRDSTOPTS:
1920 case IPV6_USE_MIN_MTU:
1921 case IPV6_PREFER_TEMPADDR:
1922 error = ip6_getpcbopt(in6p->in6p_outputopts,
1926 case IPV6_MULTICAST_IF:
1927 case IPV6_MULTICAST_HOPS:
1928 case IPV6_MULTICAST_LOOP:
1930 error = ip6_getmoptions(in6p, sopt);
1934 case IPV6_IPSEC_POLICY:
1938 struct mbuf *m = NULL;
1939 struct mbuf **mp = &m;
1940 size_t ovalsize = sopt->sopt_valsize;
1941 caddr_t oval = (caddr_t)sopt->sopt_val;
1943 error = soopt_getm(sopt, &m); /* XXX */
1946 error = soopt_mcopyin(sopt, m); /* XXX */
1949 sopt->sopt_valsize = ovalsize;
1950 sopt->sopt_val = oval;
1952 req = mtod(m, caddr_t);
1955 error = ipsec_get_policy(in6p, req, len, mp);
1957 error = soopt_mcopyout(sopt, m); /* XXX */
1958 if (error == 0 && m)
1965 error = ENOPROTOOPT;
1970 } else { /* level != IPPROTO_IPV6 */
1977 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1979 int error = 0, optval, optlen;
1980 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1981 struct inpcb *in6p = sotoinpcb(so);
1982 int level, op, optname;
1984 level = sopt->sopt_level;
1985 op = sopt->sopt_dir;
1986 optname = sopt->sopt_name;
1987 optlen = sopt->sopt_valsize;
1989 if (level != IPPROTO_IPV6) {
1996 * For ICMPv6 sockets, no modification allowed for checksum
1997 * offset, permit "no change" values to help existing apps.
1999 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2000 * for an ICMPv6 socket will fail."
2001 * The current behavior does not meet RFC3542.
2005 if (optlen != sizeof(int)) {
2009 error = sooptcopyin(sopt, &optval, sizeof(optval),
2013 if ((optval % 2) != 0) {
2014 /* the API assumes even offset values */
2016 } else if (so->so_proto->pr_protocol ==
2018 if (optval != icmp6off)
2021 in6p->in6p_cksum = optval;
2025 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2028 optval = in6p->in6p_cksum;
2030 error = sooptcopyout(sopt, &optval, sizeof(optval));
2040 error = ENOPROTOOPT;
2048 * Set up IP6 options in pcb for insertion in output packets or
2049 * specifying behavior of outgoing packets.
2052 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2053 struct socket *so, struct sockopt *sopt)
2055 struct ip6_pktopts *opt = *pktopt;
2057 struct thread *td = sopt->sopt_td;
2059 /* turn off any old options. */
2062 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2063 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2064 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2065 printf("ip6_pcbopts: all specified options are cleared.\n");
2067 ip6_clearpktopts(opt, -1);
2069 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2072 if (!m || m->m_len == 0) {
2074 * Only turning off any previous options, regardless of
2075 * whether the opt is just created or given.
2077 free(opt, M_IP6OPT);
2081 /* set options specified by user. */
2082 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2083 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2084 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2085 free(opt, M_IP6OPT);
2093 * initialize ip6_pktopts. beware that there are non-zero default values in
2097 ip6_initpktopts(struct ip6_pktopts *opt)
2100 bzero(opt, sizeof(*opt));
2101 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2102 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2103 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2104 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2108 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2109 struct ucred *cred, int uproto)
2111 struct ip6_pktopts *opt;
2113 if (*pktopt == NULL) {
2114 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2116 ip6_initpktopts(*pktopt);
2120 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2124 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2126 void *optdata = NULL;
2128 struct ip6_ext *ip6e;
2130 struct in6_pktinfo null_pktinfo;
2131 int deftclass = 0, on;
2132 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2133 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2137 if (pktopt && pktopt->ip6po_pktinfo)
2138 optdata = (void *)pktopt->ip6po_pktinfo;
2140 /* XXX: we don't have to do this every time... */
2141 bzero(&null_pktinfo, sizeof(null_pktinfo));
2142 optdata = (void *)&null_pktinfo;
2144 optdatalen = sizeof(struct in6_pktinfo);
2147 if (pktopt && pktopt->ip6po_tclass >= 0)
2148 optdata = (void *)&pktopt->ip6po_tclass;
2150 optdata = (void *)&deftclass;
2151 optdatalen = sizeof(int);
2154 if (pktopt && pktopt->ip6po_hbh) {
2155 optdata = (void *)pktopt->ip6po_hbh;
2156 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2157 optdatalen = (ip6e->ip6e_len + 1) << 3;
2161 if (pktopt && pktopt->ip6po_rthdr) {
2162 optdata = (void *)pktopt->ip6po_rthdr;
2163 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2164 optdatalen = (ip6e->ip6e_len + 1) << 3;
2167 case IPV6_RTHDRDSTOPTS:
2168 if (pktopt && pktopt->ip6po_dest1) {
2169 optdata = (void *)pktopt->ip6po_dest1;
2170 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2171 optdatalen = (ip6e->ip6e_len + 1) << 3;
2175 if (pktopt && pktopt->ip6po_dest2) {
2176 optdata = (void *)pktopt->ip6po_dest2;
2177 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2178 optdatalen = (ip6e->ip6e_len + 1) << 3;
2182 if (pktopt && pktopt->ip6po_nexthop) {
2183 optdata = (void *)pktopt->ip6po_nexthop;
2184 optdatalen = pktopt->ip6po_nexthop->sa_len;
2187 case IPV6_USE_MIN_MTU:
2189 optdata = (void *)&pktopt->ip6po_minmtu;
2191 optdata = (void *)&defminmtu;
2192 optdatalen = sizeof(int);
2195 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2199 optdata = (void *)&on;
2200 optdatalen = sizeof(on);
2202 case IPV6_PREFER_TEMPADDR:
2204 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2206 optdata = (void *)&defpreftemp;
2207 optdatalen = sizeof(int);
2209 default: /* should not happen */
2211 panic("ip6_getpcbopt: unexpected option\n");
2213 return (ENOPROTOOPT);
2216 error = sooptcopyout(sopt, optdata, optdatalen);
2222 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2227 if (optname == -1 || optname == IPV6_PKTINFO) {
2228 if (pktopt->ip6po_pktinfo)
2229 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2230 pktopt->ip6po_pktinfo = NULL;
2232 if (optname == -1 || optname == IPV6_HOPLIMIT)
2233 pktopt->ip6po_hlim = -1;
2234 if (optname == -1 || optname == IPV6_TCLASS)
2235 pktopt->ip6po_tclass = -1;
2236 if (optname == -1 || optname == IPV6_NEXTHOP) {
2237 if (pktopt->ip6po_nextroute.ro_rt) {
2238 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2239 pktopt->ip6po_nextroute.ro_rt = NULL;
2241 if (pktopt->ip6po_nexthop)
2242 free(pktopt->ip6po_nexthop, M_IP6OPT);
2243 pktopt->ip6po_nexthop = NULL;
2245 if (optname == -1 || optname == IPV6_HOPOPTS) {
2246 if (pktopt->ip6po_hbh)
2247 free(pktopt->ip6po_hbh, M_IP6OPT);
2248 pktopt->ip6po_hbh = NULL;
2250 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2251 if (pktopt->ip6po_dest1)
2252 free(pktopt->ip6po_dest1, M_IP6OPT);
2253 pktopt->ip6po_dest1 = NULL;
2255 if (optname == -1 || optname == IPV6_RTHDR) {
2256 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2257 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2258 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2259 if (pktopt->ip6po_route.ro_rt) {
2260 RTFREE(pktopt->ip6po_route.ro_rt);
2261 pktopt->ip6po_route.ro_rt = NULL;
2264 if (optname == -1 || optname == IPV6_DSTOPTS) {
2265 if (pktopt->ip6po_dest2)
2266 free(pktopt->ip6po_dest2, M_IP6OPT);
2267 pktopt->ip6po_dest2 = NULL;
2271 #define PKTOPT_EXTHDRCPY(type) \
2274 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2275 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2276 if (dst->type == NULL && canwait == M_NOWAIT)\
2278 bcopy(src->type, dst->type, hlen);\
2280 } while (/*CONSTCOND*/ 0)
2283 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2285 if (dst == NULL || src == NULL) {
2286 printf("ip6_clearpktopts: invalid argument\n");
2290 dst->ip6po_hlim = src->ip6po_hlim;
2291 dst->ip6po_tclass = src->ip6po_tclass;
2292 dst->ip6po_flags = src->ip6po_flags;
2293 if (src->ip6po_pktinfo) {
2294 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2296 if (dst->ip6po_pktinfo == NULL)
2298 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2300 if (src->ip6po_nexthop) {
2301 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2303 if (dst->ip6po_nexthop == NULL)
2305 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2306 src->ip6po_nexthop->sa_len);
2308 PKTOPT_EXTHDRCPY(ip6po_hbh);
2309 PKTOPT_EXTHDRCPY(ip6po_dest1);
2310 PKTOPT_EXTHDRCPY(ip6po_dest2);
2311 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2315 ip6_clearpktopts(dst, -1);
2318 #undef PKTOPT_EXTHDRCPY
2320 struct ip6_pktopts *
2321 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2324 struct ip6_pktopts *dst;
2326 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2329 ip6_initpktopts(dst);
2331 if ((error = copypktopts(dst, src, canwait)) != 0) {
2332 free(dst, M_IP6OPT);
2340 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2345 ip6_clearpktopts(pktopt, -1);
2347 free(pktopt, M_IP6OPT);
2351 * Set IPv6 outgoing packet options based on advanced API.
2354 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2355 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2357 struct cmsghdr *cm = 0;
2359 if (control == NULL || opt == NULL)
2362 ip6_initpktopts(opt);
2367 * If stickyopt is provided, make a local copy of the options
2368 * for this particular packet, then override them by ancillary
2370 * XXX: copypktopts() does not copy the cached route to a next
2371 * hop (if any). This is not very good in terms of efficiency,
2372 * but we can allow this since this option should be rarely
2375 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2380 * XXX: Currently, we assume all the optional information is stored
2383 if (control->m_next)
2386 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2387 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2390 if (control->m_len < CMSG_LEN(0))
2393 cm = mtod(control, struct cmsghdr *);
2394 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2396 if (cm->cmsg_level != IPPROTO_IPV6)
2399 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2400 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2409 * Set a particular packet option, as a sticky option or an ancillary data
2410 * item. "len" can be 0 only when it's a sticky option.
2411 * We have 4 cases of combination of "sticky" and "cmsg":
2412 * "sticky=0, cmsg=0": impossible
2413 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2414 * "sticky=1, cmsg=0": RFC3542 socket option
2415 * "sticky=1, cmsg=1": RFC2292 socket option
2418 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2419 struct ucred *cred, int sticky, int cmsg, int uproto)
2421 int minmtupolicy, preftemp;
2424 if (!sticky && !cmsg) {
2426 printf("ip6_setpktopt: impossible case\n");
2432 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2433 * not be specified in the context of RFC3542. Conversely,
2434 * RFC3542 types should not be specified in the context of RFC2292.
2438 case IPV6_2292PKTINFO:
2439 case IPV6_2292HOPLIMIT:
2440 case IPV6_2292NEXTHOP:
2441 case IPV6_2292HOPOPTS:
2442 case IPV6_2292DSTOPTS:
2443 case IPV6_2292RTHDR:
2444 case IPV6_2292PKTOPTIONS:
2445 return (ENOPROTOOPT);
2448 if (sticky && cmsg) {
2455 case IPV6_RTHDRDSTOPTS:
2457 case IPV6_USE_MIN_MTU:
2460 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2461 return (ENOPROTOOPT);
2466 case IPV6_2292PKTINFO:
2469 struct ifnet *ifp = NULL;
2470 struct in6_pktinfo *pktinfo;
2472 if (len != sizeof(struct in6_pktinfo))
2475 pktinfo = (struct in6_pktinfo *)buf;
2478 * An application can clear any sticky IPV6_PKTINFO option by
2479 * doing a "regular" setsockopt with ipi6_addr being
2480 * in6addr_any and ipi6_ifindex being zero.
2481 * [RFC 3542, Section 6]
2483 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2484 pktinfo->ipi6_ifindex == 0 &&
2485 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2486 ip6_clearpktopts(opt, optname);
2490 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2491 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2495 /* validate the interface index if specified. */
2496 if (pktinfo->ipi6_ifindex > V_if_index ||
2497 pktinfo->ipi6_ifindex < 0) {
2500 if (pktinfo->ipi6_ifindex) {
2501 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2507 * We store the address anyway, and let in6_selectsrc()
2508 * validate the specified address. This is because ipi6_addr
2509 * may not have enough information about its scope zone, and
2510 * we may need additional information (such as outgoing
2511 * interface or the scope zone of a destination address) to
2512 * disambiguate the scope.
2513 * XXX: the delay of the validation may confuse the
2514 * application when it is used as a sticky option.
2516 if (opt->ip6po_pktinfo == NULL) {
2517 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2518 M_IP6OPT, M_NOWAIT);
2519 if (opt->ip6po_pktinfo == NULL)
2522 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2526 case IPV6_2292HOPLIMIT:
2532 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2533 * to simplify the ordering among hoplimit options.
2535 if (optname == IPV6_HOPLIMIT && sticky)
2536 return (ENOPROTOOPT);
2538 if (len != sizeof(int))
2541 if (*hlimp < -1 || *hlimp > 255)
2544 opt->ip6po_hlim = *hlimp;
2552 if (len != sizeof(int))
2554 tclass = *(int *)buf;
2555 if (tclass < -1 || tclass > 255)
2558 opt->ip6po_tclass = tclass;
2562 case IPV6_2292NEXTHOP:
2565 error = priv_check_cred(cred,
2566 PRIV_NETINET_SETHDROPTS, 0);
2571 if (len == 0) { /* just remove the option */
2572 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2576 /* check if cmsg_len is large enough for sa_len */
2577 if (len < sizeof(struct sockaddr) || len < *buf)
2580 switch (((struct sockaddr *)buf)->sa_family) {
2583 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2586 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2589 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2590 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2593 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2599 case AF_LINK: /* should eventually be supported */
2601 return (EAFNOSUPPORT);
2604 /* turn off the previous option, then set the new option. */
2605 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2606 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2607 if (opt->ip6po_nexthop == NULL)
2609 bcopy(buf, opt->ip6po_nexthop, *buf);
2612 case IPV6_2292HOPOPTS:
2615 struct ip6_hbh *hbh;
2619 * XXX: We don't allow a non-privileged user to set ANY HbH
2620 * options, since per-option restriction has too much
2624 error = priv_check_cred(cred,
2625 PRIV_NETINET_SETHDROPTS, 0);
2631 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2632 break; /* just remove the option */
2635 /* message length validation */
2636 if (len < sizeof(struct ip6_hbh))
2638 hbh = (struct ip6_hbh *)buf;
2639 hbhlen = (hbh->ip6h_len + 1) << 3;
2643 /* turn off the previous option, then set the new option. */
2644 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2645 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2646 if (opt->ip6po_hbh == NULL)
2648 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2653 case IPV6_2292DSTOPTS:
2655 case IPV6_RTHDRDSTOPTS:
2657 struct ip6_dest *dest, **newdest = NULL;
2660 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2661 error = priv_check_cred(cred,
2662 PRIV_NETINET_SETHDROPTS, 0);
2668 ip6_clearpktopts(opt, optname);
2669 break; /* just remove the option */
2672 /* message length validation */
2673 if (len < sizeof(struct ip6_dest))
2675 dest = (struct ip6_dest *)buf;
2676 destlen = (dest->ip6d_len + 1) << 3;
2681 * Determine the position that the destination options header
2682 * should be inserted; before or after the routing header.
2685 case IPV6_2292DSTOPTS:
2687 * The old advacned API is ambiguous on this point.
2688 * Our approach is to determine the position based
2689 * according to the existence of a routing header.
2690 * Note, however, that this depends on the order of the
2691 * extension headers in the ancillary data; the 1st
2692 * part of the destination options header must appear
2693 * before the routing header in the ancillary data,
2695 * RFC3542 solved the ambiguity by introducing
2696 * separate ancillary data or option types.
2698 if (opt->ip6po_rthdr == NULL)
2699 newdest = &opt->ip6po_dest1;
2701 newdest = &opt->ip6po_dest2;
2703 case IPV6_RTHDRDSTOPTS:
2704 newdest = &opt->ip6po_dest1;
2707 newdest = &opt->ip6po_dest2;
2711 /* turn off the previous option, then set the new option. */
2712 ip6_clearpktopts(opt, optname);
2713 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2714 if (*newdest == NULL)
2716 bcopy(dest, *newdest, destlen);
2721 case IPV6_2292RTHDR:
2724 struct ip6_rthdr *rth;
2728 ip6_clearpktopts(opt, IPV6_RTHDR);
2729 break; /* just remove the option */
2732 /* message length validation */
2733 if (len < sizeof(struct ip6_rthdr))
2735 rth = (struct ip6_rthdr *)buf;
2736 rthlen = (rth->ip6r_len + 1) << 3;
2740 switch (rth->ip6r_type) {
2741 case IPV6_RTHDR_TYPE_0:
2742 if (rth->ip6r_len == 0) /* must contain one addr */
2744 if (rth->ip6r_len % 2) /* length must be even */
2746 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2750 return (EINVAL); /* not supported */
2753 /* turn off the previous option */
2754 ip6_clearpktopts(opt, IPV6_RTHDR);
2755 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2756 if (opt->ip6po_rthdr == NULL)
2758 bcopy(rth, opt->ip6po_rthdr, rthlen);
2763 case IPV6_USE_MIN_MTU:
2764 if (len != sizeof(int))
2766 minmtupolicy = *(int *)buf;
2767 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2768 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2769 minmtupolicy != IP6PO_MINMTU_ALL) {
2772 opt->ip6po_minmtu = minmtupolicy;
2776 if (len != sizeof(int))
2779 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2781 * we ignore this option for TCP sockets.
2782 * (RFC3542 leaves this case unspecified.)
2784 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2786 opt->ip6po_flags |= IP6PO_DONTFRAG;
2789 case IPV6_PREFER_TEMPADDR:
2790 if (len != sizeof(int))
2792 preftemp = *(int *)buf;
2793 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2794 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2795 preftemp != IP6PO_TEMPADDR_PREFER) {
2798 opt->ip6po_prefer_tempaddr = preftemp;
2802 return (ENOPROTOOPT);
2803 } /* end of switch */
2809 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2810 * packet to the input queue of a specified interface. Note that this
2811 * calls the output routine of the loopback "driver", but with an interface
2812 * pointer that might NOT be &loif -- easier than replicating that code here.
2815 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2818 struct ip6_hdr *ip6;
2820 copym = m_copy(m, 0, M_COPYALL);
2825 * Make sure to deep-copy IPv6 header portion in case the data
2826 * is in an mbuf cluster, so that we can safely override the IPv6
2827 * header portion later.
2829 if ((copym->m_flags & M_EXT) != 0 ||
2830 copym->m_len < sizeof(struct ip6_hdr)) {
2831 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2837 if (copym->m_len < sizeof(*ip6)) {
2843 ip6 = mtod(copym, struct ip6_hdr *);
2845 * clear embedded scope identifiers if necessary.
2846 * in6_clearscope will touch the addresses only when necessary.
2848 in6_clearscope(&ip6->ip6_src);
2849 in6_clearscope(&ip6->ip6_dst);
2851 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2855 * Chop IPv6 header off from the payload.
2858 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2861 struct ip6_hdr *ip6;
2863 ip6 = mtod(m, struct ip6_hdr *);
2864 if (m->m_len > sizeof(*ip6)) {
2865 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2870 M_MOVE_PKTHDR(mh, m);
2871 MH_ALIGN(mh, sizeof(*ip6));
2872 m->m_len -= sizeof(*ip6);
2873 m->m_data += sizeof(*ip6);
2876 m->m_len = sizeof(*ip6);
2877 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2879 exthdrs->ip6e_ip6 = m;
2884 * Compute IPv6 extension header length.
2887 ip6_optlen(struct inpcb *in6p)
2891 if (!in6p->in6p_outputopts)
2896 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2898 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2899 if (in6p->in6p_outputopts->ip6po_rthdr)
2900 /* dest1 is valid with rthdr only */
2901 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2902 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2903 len += elen(in6p->in6p_outputopts->ip6po_dest2);