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);
606 * The outgoing interface must be in the zone of source and
607 * destination addresses. We should use ia_ifp to support the
608 * case of sending packets to an address of our own.
610 if (ia != NULL && ia->ia_ifp)
611 origifp = ia->ia_ifp;
616 if (in6_setscope(&src0, origifp, &zone))
618 bzero(&src_sa, sizeof(src_sa));
619 src_sa.sin6_family = AF_INET6;
620 src_sa.sin6_len = sizeof(src_sa);
621 src_sa.sin6_addr = ip6->ip6_src;
622 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
626 if (in6_setscope(&dst0, origifp, &zone))
628 /* re-initialize to be sure */
629 bzero(&dst_sa, sizeof(dst_sa));
630 dst_sa.sin6_family = AF_INET6;
631 dst_sa.sin6_len = sizeof(dst_sa);
632 dst_sa.sin6_addr = ip6->ip6_dst;
633 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
637 /* scope check is done. */
641 V_ip6stat.ip6s_badscope++;
642 in6_ifstat_inc(origifp, ifs6_out_discard);
644 error = EHOSTUNREACH; /* XXX */
648 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
649 if (opt && opt->ip6po_nextroute.ro_rt) {
651 * The nexthop is explicitly specified by the
652 * application. We assume the next hop is an IPv6
655 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
657 else if ((rt->rt_flags & RTF_GATEWAY))
658 dst = (struct sockaddr_in6 *)rt->rt_gateway;
661 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
662 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
664 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
665 in6_ifstat_inc(ifp, ifs6_out_mcast);
667 * Confirm that the outgoing interface supports multicast.
669 if (!(ifp->if_flags & IFF_MULTICAST)) {
670 V_ip6stat.ip6s_noroute++;
671 in6_ifstat_inc(ifp, ifs6_out_discard);
675 if ((im6o == NULL && in6_mcast_loop) ||
676 (im6o && im6o->im6o_multicast_loop)) {
678 * Loop back multicast datagram if not expressly
679 * forbidden to do so, even if we have not joined
680 * the address; protocols will filter it later,
681 * thus deferring a hash lookup and lock acquisition
682 * at the expense of an m_copym().
684 ip6_mloopback(ifp, m, dst);
687 * If we are acting as a multicast router, perform
688 * multicast forwarding as if the packet had just
689 * arrived on the interface to which we are about
690 * to send. The multicast forwarding function
691 * recursively calls this function, using the
692 * IPV6_FORWARDING flag to prevent infinite recursion.
694 * Multicasts that are looped back by ip6_mloopback(),
695 * above, will be forwarded by the ip6_input() routine,
698 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
700 * XXX: ip6_mforward expects that rcvif is NULL
701 * when it is called from the originating path.
702 * However, it is not always the case, since
703 * some versions of MGETHDR() does not
704 * initialize the field.
706 m->m_pkthdr.rcvif = NULL;
707 if (ip6_mforward(ip6, ifp, m) != 0) {
714 * Multicasts with a hoplimit of zero may be looped back,
715 * above, but must not be transmitted on a network.
716 * Also, multicasts addressed to the loopback interface
717 * are not sent -- the above call to ip6_mloopback() will
718 * loop back a copy if this host actually belongs to the
719 * destination group on the loopback interface.
721 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
722 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
729 * Fill the outgoing inteface to tell the upper layer
730 * to increment per-interface statistics.
735 /* Determine path MTU. */
736 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
741 * The caller of this function may specify to use the minimum MTU
743 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
744 * setting. The logic is a bit complicated; by default, unicast
745 * packets will follow path MTU while multicast packets will be sent at
746 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
747 * including unicast ones will be sent at the minimum MTU. Multicast
748 * packets will always be sent at the minimum MTU unless
749 * IP6PO_MINMTU_DISABLE is explicitly specified.
750 * See RFC 3542 for more details.
752 if (mtu > IPV6_MMTU) {
753 if ((flags & IPV6_MINMTU))
755 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
757 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
759 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
765 * clear embedded scope identifiers if necessary.
766 * in6_clearscope will touch the addresses only when necessary.
768 in6_clearscope(&ip6->ip6_src);
769 in6_clearscope(&ip6->ip6_dst);
772 * If the outgoing packet contains a hop-by-hop options header,
773 * it must be examined and processed even by the source node.
774 * (RFC 2460, section 4.)
776 if (exthdrs.ip6e_hbh) {
777 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
778 u_int32_t dummy; /* XXX unused */
779 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
782 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
783 panic("ip6e_hbh is not continuous");
786 * XXX: if we have to send an ICMPv6 error to the sender,
787 * we need the M_LOOP flag since icmp6_error() expects
788 * the IPv6 and the hop-by-hop options header are
789 * continuous unless the flag is set.
791 m->m_flags |= M_LOOP;
792 m->m_pkthdr.rcvif = ifp;
793 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
794 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
795 &dummy, &plen) < 0) {
796 /* m was already freed at this point */
797 error = EINVAL;/* better error? */
800 m->m_flags &= ~M_LOOP; /* XXX */
801 m->m_pkthdr.rcvif = NULL;
804 /* Jump over all PFIL processing if hooks are not active. */
805 if (!PFIL_HOOKED(&inet6_pfil_hook))
809 /* Run through list of hooks for output packets. */
810 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
811 if (error != 0 || m == NULL)
813 ip6 = mtod(m, struct ip6_hdr *);
815 /* See if destination IP address was changed by packet filter. */
816 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
817 m->m_flags |= M_SKIP_FIREWALL;
818 /* If destination is now ourself drop to ip6_input(). */
819 if (in6_localaddr(&ip6->ip6_dst)) {
820 if (m->m_pkthdr.rcvif == NULL)
821 m->m_pkthdr.rcvif = V_loif;
822 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
823 m->m_pkthdr.csum_flags |=
824 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
825 m->m_pkthdr.csum_data = 0xffff;
827 m->m_pkthdr.csum_flags |=
828 CSUM_IP_CHECKED | CSUM_IP_VALID;
829 error = netisr_queue(NETISR_IPV6, m);
832 goto again; /* Redo the routing table lookup. */
835 /* XXX: IPFIREWALL_FORWARD */
839 * Send the packet to the outgoing interface.
840 * If necessary, do IPv6 fragmentation before sending.
842 * the logic here is rather complex:
843 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
844 * 1-a: send as is if tlen <= path mtu
845 * 1-b: fragment if tlen > path mtu
847 * 2: if user asks us not to fragment (dontfrag == 1)
848 * 2-a: send as is if tlen <= interface mtu
849 * 2-b: error if tlen > interface mtu
851 * 3: if we always need to attach fragment header (alwaysfrag == 1)
854 * 4: if dontfrag == 1 && alwaysfrag == 1
855 * error, as we cannot handle this conflicting request
857 tlen = m->m_pkthdr.len;
859 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
863 if (dontfrag && alwaysfrag) { /* case 4 */
864 /* conflicting request - can't transmit */
868 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
870 * Even if the DONTFRAG option is specified, we cannot send the
871 * packet when the data length is larger than the MTU of the
872 * outgoing interface.
873 * Notify the error by sending IPV6_PATHMTU ancillary data as
874 * well as returning an error code (the latter is not described
878 struct ip6ctlparam ip6cp;
880 mtu32 = (u_int32_t)mtu;
881 bzero(&ip6cp, sizeof(ip6cp));
882 ip6cp.ip6c_cmdarg = (void *)&mtu32;
883 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
891 * transmit packet without fragmentation
893 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
894 struct in6_ifaddr *ia6;
896 ip6 = mtod(m, struct ip6_hdr *);
897 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
899 /* Record statistics for this interface address. */
900 ia6->ia_ifa.if_opackets++;
901 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
902 ifa_free(&ia6->ia_ifa);
904 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
909 * try to fragment the packet. case 1-b and 3
911 if (mtu < IPV6_MMTU) {
912 /* path MTU cannot be less than IPV6_MMTU */
914 in6_ifstat_inc(ifp, ifs6_out_fragfail);
916 } else if (ip6->ip6_plen == 0) {
917 /* jumbo payload cannot be fragmented */
919 in6_ifstat_inc(ifp, ifs6_out_fragfail);
922 struct mbuf **mnext, *m_frgpart;
923 struct ip6_frag *ip6f;
924 u_int32_t id = htonl(ip6_randomid());
927 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
930 * Too large for the destination or interface;
931 * fragment if possible.
932 * Must be able to put at least 8 bytes per fragment.
934 hlen = unfragpartlen;
935 if (mtu > IPV6_MAXPACKET)
936 mtu = IPV6_MAXPACKET;
938 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
941 in6_ifstat_inc(ifp, ifs6_out_fragfail);
946 * Verify that we have any chance at all of being able to queue
947 * the packet or packet fragments
949 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
950 < tlen /* - hlen */)) {
952 V_ip6stat.ip6s_odropped++;
956 mnext = &m->m_nextpkt;
959 * Change the next header field of the last header in the
960 * unfragmentable part.
962 if (exthdrs.ip6e_rthdr) {
963 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
964 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
965 } else if (exthdrs.ip6e_dest1) {
966 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
967 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
968 } else if (exthdrs.ip6e_hbh) {
969 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
970 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
972 nextproto = ip6->ip6_nxt;
973 ip6->ip6_nxt = IPPROTO_FRAGMENT;
977 * Loop through length of segment after first fragment,
978 * make new header and copy data of each part and link onto
982 for (off = hlen; off < tlen; off += len) {
983 MGETHDR(m, M_DONTWAIT, MT_HEADER);
986 V_ip6stat.ip6s_odropped++;
989 m->m_pkthdr.rcvif = NULL;
990 m->m_flags = m0->m_flags & M_COPYFLAGS;
992 mnext = &m->m_nextpkt;
993 m->m_data += max_linkhdr;
994 mhip6 = mtod(m, struct ip6_hdr *);
996 m->m_len = sizeof(*mhip6);
997 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
999 V_ip6stat.ip6s_odropped++;
1002 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1003 if (off + len >= tlen)
1006 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1007 mhip6->ip6_plen = htons((u_short)(len + hlen +
1008 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1009 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1011 V_ip6stat.ip6s_odropped++;
1014 m_cat(m, m_frgpart);
1015 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1016 m->m_pkthdr.rcvif = NULL;
1017 ip6f->ip6f_reserved = 0;
1018 ip6f->ip6f_ident = id;
1019 ip6f->ip6f_nxt = nextproto;
1020 V_ip6stat.ip6s_ofragments++;
1021 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1024 in6_ifstat_inc(ifp, ifs6_out_fragok);
1028 * Remove leading garbages.
1034 for (m0 = m; m; m = m0) {
1038 /* Record statistics for this interface address. */
1040 ia->ia_ifa.if_opackets++;
1041 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1043 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1049 V_ip6stat.ip6s_fragmented++;
1052 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1054 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1055 RTFREE(ro_pmtu->ro_rt);
1065 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1066 m_freem(exthdrs.ip6e_dest1);
1067 m_freem(exthdrs.ip6e_rthdr);
1068 m_freem(exthdrs.ip6e_dest2);
1077 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1081 if (hlen > MCLBYTES)
1082 return (ENOBUFS); /* XXX */
1084 MGET(m, M_DONTWAIT, MT_DATA);
1089 MCLGET(m, M_DONTWAIT);
1090 if ((m->m_flags & M_EXT) == 0) {
1097 bcopy(hdr, mtod(m, caddr_t), hlen);
1104 * Insert jumbo payload option.
1107 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1113 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1116 * If there is no hop-by-hop options header, allocate new one.
1117 * If there is one but it doesn't have enough space to store the
1118 * jumbo payload option, allocate a cluster to store the whole options.
1119 * Otherwise, use it to store the options.
1121 if (exthdrs->ip6e_hbh == 0) {
1122 MGET(mopt, M_DONTWAIT, MT_DATA);
1125 mopt->m_len = JUMBOOPTLEN;
1126 optbuf = mtod(mopt, u_char *);
1127 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1128 exthdrs->ip6e_hbh = mopt;
1130 struct ip6_hbh *hbh;
1132 mopt = exthdrs->ip6e_hbh;
1133 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1136 * - exthdrs->ip6e_hbh is not referenced from places
1137 * other than exthdrs.
1138 * - exthdrs->ip6e_hbh is not an mbuf chain.
1140 int oldoptlen = mopt->m_len;
1144 * XXX: give up if the whole (new) hbh header does
1145 * not fit even in an mbuf cluster.
1147 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1151 * As a consequence, we must always prepare a cluster
1154 MGET(n, M_DONTWAIT, MT_DATA);
1156 MCLGET(n, M_DONTWAIT);
1157 if ((n->m_flags & M_EXT) == 0) {
1164 n->m_len = oldoptlen + JUMBOOPTLEN;
1165 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1167 optbuf = mtod(n, caddr_t) + oldoptlen;
1169 mopt = exthdrs->ip6e_hbh = n;
1171 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1172 mopt->m_len += JUMBOOPTLEN;
1174 optbuf[0] = IP6OPT_PADN;
1178 * Adjust the header length according to the pad and
1179 * the jumbo payload option.
1181 hbh = mtod(mopt, struct ip6_hbh *);
1182 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1185 /* fill in the option. */
1186 optbuf[2] = IP6OPT_JUMBO;
1188 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1189 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1191 /* finally, adjust the packet header length */
1192 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1199 * Insert fragment header and copy unfragmentable header portions.
1202 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1203 struct ip6_frag **frghdrp)
1205 struct mbuf *n, *mlast;
1207 if (hlen > sizeof(struct ip6_hdr)) {
1208 n = m_copym(m0, sizeof(struct ip6_hdr),
1209 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1216 /* Search for the last mbuf of unfragmentable part. */
1217 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1220 if ((mlast->m_flags & M_EXT) == 0 &&
1221 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1222 /* use the trailing space of the last mbuf for the fragment hdr */
1223 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1225 mlast->m_len += sizeof(struct ip6_frag);
1226 m->m_pkthdr.len += sizeof(struct ip6_frag);
1228 /* allocate a new mbuf for the fragment header */
1231 MGET(mfrg, M_DONTWAIT, MT_DATA);
1234 mfrg->m_len = sizeof(struct ip6_frag);
1235 *frghdrp = mtod(mfrg, struct ip6_frag *);
1236 mlast->m_next = mfrg;
1243 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1244 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1251 if (ro_pmtu != ro) {
1252 /* The first hop and the final destination may differ. */
1253 struct sockaddr_in6 *sa6_dst =
1254 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1255 if (ro_pmtu->ro_rt &&
1256 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1257 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1258 RTFREE(ro_pmtu->ro_rt);
1259 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1261 if (ro_pmtu->ro_rt == NULL) {
1262 bzero(sa6_dst, sizeof(*sa6_dst));
1263 sa6_dst->sin6_family = AF_INET6;
1264 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1265 sa6_dst->sin6_addr = *dst;
1267 rtalloc((struct route *)ro_pmtu);
1270 if (ro_pmtu->ro_rt) {
1272 struct in_conninfo inc;
1274 bzero(&inc, sizeof(inc));
1275 inc.inc_flags |= INC_ISIPV6;
1276 inc.inc6_faddr = *dst;
1279 ifp = ro_pmtu->ro_rt->rt_ifp;
1280 ifmtu = IN6_LINKMTU(ifp);
1281 mtu = tcp_hc_getmtu(&inc);
1283 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1285 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1288 else if (mtu < IPV6_MMTU) {
1290 * RFC2460 section 5, last paragraph:
1291 * if we record ICMPv6 too big message with
1292 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1293 * or smaller, with framgent header attached.
1294 * (fragment header is needed regardless from the
1295 * packet size, for translators to identify packets)
1299 } else if (mtu > ifmtu) {
1301 * The MTU on the route is larger than the MTU on
1302 * the interface! This shouldn't happen, unless the
1303 * MTU of the interface has been changed after the
1304 * interface was brought up. Change the MTU in the
1305 * route to match the interface MTU (as long as the
1306 * field isn't locked).
1309 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1312 mtu = IN6_LINKMTU(ifp);
1314 error = EHOSTUNREACH; /* XXX */
1318 *alwaysfragp = alwaysfrag;
1323 * IP6 socket option processing.
1326 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1328 int optdatalen, uproto;
1330 struct inpcb *in6p = sotoinpcb(so);
1332 int level, op, optname;
1336 level = sopt->sopt_level;
1337 op = sopt->sopt_dir;
1338 optname = sopt->sopt_name;
1339 optlen = sopt->sopt_valsize;
1343 uproto = (int)so->so_proto->pr_protocol;
1345 if (level == IPPROTO_IPV6) {
1350 case IPV6_2292PKTOPTIONS:
1351 #ifdef IPV6_PKTOPTIONS
1352 case IPV6_PKTOPTIONS:
1357 error = soopt_getm(sopt, &m); /* XXX */
1360 error = soopt_mcopyin(sopt, m); /* XXX */
1363 error = ip6_pcbopts(&in6p->in6p_outputopts,
1365 m_freem(m); /* XXX */
1370 * Use of some Hop-by-Hop options or some
1371 * Destination options, might require special
1372 * privilege. That is, normal applications
1373 * (without special privilege) might be forbidden
1374 * from setting certain options in outgoing packets,
1375 * and might never see certain options in received
1376 * packets. [RFC 2292 Section 6]
1377 * KAME specific note:
1378 * KAME prevents non-privileged users from sending or
1379 * receiving ANY hbh/dst options in order to avoid
1380 * overhead of parsing options in the kernel.
1382 case IPV6_RECVHOPOPTS:
1383 case IPV6_RECVDSTOPTS:
1384 case IPV6_RECVRTHDRDSTOPTS:
1386 error = priv_check(td,
1387 PRIV_NETINET_SETHDROPTS);
1392 case IPV6_UNICAST_HOPS:
1396 case IPV6_RECVPKTINFO:
1397 case IPV6_RECVHOPLIMIT:
1398 case IPV6_RECVRTHDR:
1399 case IPV6_RECVPATHMTU:
1400 case IPV6_RECVTCLASS:
1402 case IPV6_AUTOFLOWLABEL:
1404 if (optname == IPV6_BINDANY && td != NULL) {
1405 error = priv_check(td,
1406 PRIV_NETINET_BINDANY);
1411 if (optlen != sizeof(int)) {
1415 error = sooptcopyin(sopt, &optval,
1416 sizeof optval, sizeof optval);
1421 case IPV6_UNICAST_HOPS:
1422 if (optval < -1 || optval >= 256)
1425 /* -1 = kernel default */
1426 in6p->in6p_hops = optval;
1427 if ((in6p->inp_vflag &
1429 in6p->inp_ip_ttl = optval;
1432 #define OPTSET(bit) \
1435 in6p->inp_flags |= (bit); \
1437 in6p->inp_flags &= ~(bit); \
1438 } while (/*CONSTCOND*/ 0)
1439 #define OPTSET2292(bit) \
1441 in6p->inp_flags |= IN6P_RFC2292; \
1443 in6p->inp_flags |= (bit); \
1445 in6p->inp_flags &= ~(bit); \
1446 } while (/*CONSTCOND*/ 0)
1447 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1449 case IPV6_RECVPKTINFO:
1450 /* cannot mix with RFC2292 */
1451 if (OPTBIT(IN6P_RFC2292)) {
1455 OPTSET(IN6P_PKTINFO);
1460 struct ip6_pktopts **optp;
1462 /* cannot mix with RFC2292 */
1463 if (OPTBIT(IN6P_RFC2292)) {
1467 optp = &in6p->in6p_outputopts;
1468 error = ip6_pcbopt(IPV6_HOPLIMIT,
1469 (u_char *)&optval, sizeof(optval),
1470 optp, (td != NULL) ? td->td_ucred :
1475 case IPV6_RECVHOPLIMIT:
1476 /* cannot mix with RFC2292 */
1477 if (OPTBIT(IN6P_RFC2292)) {
1481 OPTSET(IN6P_HOPLIMIT);
1484 case IPV6_RECVHOPOPTS:
1485 /* cannot mix with RFC2292 */
1486 if (OPTBIT(IN6P_RFC2292)) {
1490 OPTSET(IN6P_HOPOPTS);
1493 case IPV6_RECVDSTOPTS:
1494 /* cannot mix with RFC2292 */
1495 if (OPTBIT(IN6P_RFC2292)) {
1499 OPTSET(IN6P_DSTOPTS);
1502 case IPV6_RECVRTHDRDSTOPTS:
1503 /* cannot mix with RFC2292 */
1504 if (OPTBIT(IN6P_RFC2292)) {
1508 OPTSET(IN6P_RTHDRDSTOPTS);
1511 case IPV6_RECVRTHDR:
1512 /* cannot mix with RFC2292 */
1513 if (OPTBIT(IN6P_RFC2292)) {
1524 case IPV6_RECVPATHMTU:
1526 * We ignore this option for TCP
1528 * (RFC3542 leaves this case
1531 if (uproto != IPPROTO_TCP)
1537 * make setsockopt(IPV6_V6ONLY)
1538 * available only prior to bind(2).
1539 * see ipng mailing list, Jun 22 2001.
1541 if (in6p->inp_lport ||
1542 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1546 OPTSET(IN6P_IPV6_V6ONLY);
1548 in6p->inp_vflag &= ~INP_IPV4;
1550 in6p->inp_vflag |= INP_IPV4;
1552 case IPV6_RECVTCLASS:
1553 /* cannot mix with RFC2292 XXX */
1554 if (OPTBIT(IN6P_RFC2292)) {
1558 OPTSET(IN6P_TCLASS);
1560 case IPV6_AUTOFLOWLABEL:
1561 OPTSET(IN6P_AUTOFLOWLABEL);
1565 OPTSET(INP_BINDANY);
1572 case IPV6_USE_MIN_MTU:
1573 case IPV6_PREFER_TEMPADDR:
1574 if (optlen != sizeof(optval)) {
1578 error = sooptcopyin(sopt, &optval,
1579 sizeof optval, sizeof optval);
1583 struct ip6_pktopts **optp;
1584 optp = &in6p->in6p_outputopts;
1585 error = ip6_pcbopt(optname,
1586 (u_char *)&optval, sizeof(optval),
1587 optp, (td != NULL) ? td->td_ucred :
1592 case IPV6_2292PKTINFO:
1593 case IPV6_2292HOPLIMIT:
1594 case IPV6_2292HOPOPTS:
1595 case IPV6_2292DSTOPTS:
1596 case IPV6_2292RTHDR:
1598 if (optlen != sizeof(int)) {
1602 error = sooptcopyin(sopt, &optval,
1603 sizeof optval, sizeof optval);
1607 case IPV6_2292PKTINFO:
1608 OPTSET2292(IN6P_PKTINFO);
1610 case IPV6_2292HOPLIMIT:
1611 OPTSET2292(IN6P_HOPLIMIT);
1613 case IPV6_2292HOPOPTS:
1615 * Check super-user privilege.
1616 * See comments for IPV6_RECVHOPOPTS.
1619 error = priv_check(td,
1620 PRIV_NETINET_SETHDROPTS);
1624 OPTSET2292(IN6P_HOPOPTS);
1626 case IPV6_2292DSTOPTS:
1628 error = priv_check(td,
1629 PRIV_NETINET_SETHDROPTS);
1633 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1635 case IPV6_2292RTHDR:
1636 OPTSET2292(IN6P_RTHDR);
1644 case IPV6_RTHDRDSTOPTS:
1647 /* new advanced API (RFC3542) */
1649 u_char optbuf_storage[MCLBYTES];
1651 struct ip6_pktopts **optp;
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1660 * We only ensure valsize is not too large
1661 * here. Further validation will be done
1664 error = sooptcopyin(sopt, optbuf_storage,
1665 sizeof(optbuf_storage), 0);
1668 optlen = sopt->sopt_valsize;
1669 optbuf = optbuf_storage;
1670 optp = &in6p->in6p_outputopts;
1671 error = ip6_pcbopt(optname, optbuf, optlen,
1672 optp, (td != NULL) ? td->td_ucred : NULL,
1678 case IPV6_MULTICAST_IF:
1679 case IPV6_MULTICAST_HOPS:
1680 case IPV6_MULTICAST_LOOP:
1681 case IPV6_JOIN_GROUP:
1682 case IPV6_LEAVE_GROUP:
1684 case MCAST_BLOCK_SOURCE:
1685 case MCAST_UNBLOCK_SOURCE:
1686 case MCAST_JOIN_GROUP:
1687 case MCAST_LEAVE_GROUP:
1688 case MCAST_JOIN_SOURCE_GROUP:
1689 case MCAST_LEAVE_SOURCE_GROUP:
1690 error = ip6_setmoptions(in6p, sopt);
1693 case IPV6_PORTRANGE:
1694 error = sooptcopyin(sopt, &optval,
1695 sizeof optval, sizeof optval);
1700 case IPV6_PORTRANGE_DEFAULT:
1701 in6p->inp_flags &= ~(INP_LOWPORT);
1702 in6p->inp_flags &= ~(INP_HIGHPORT);
1705 case IPV6_PORTRANGE_HIGH:
1706 in6p->inp_flags &= ~(INP_LOWPORT);
1707 in6p->inp_flags |= INP_HIGHPORT;
1710 case IPV6_PORTRANGE_LOW:
1711 in6p->inp_flags &= ~(INP_HIGHPORT);
1712 in6p->inp_flags |= INP_LOWPORT;
1722 case IPV6_IPSEC_POLICY:
1727 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1729 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1731 req = mtod(m, caddr_t);
1732 error = ipsec_set_policy(in6p, optname, req,
1733 m->m_len, (sopt->sopt_td != NULL) ?
1734 sopt->sopt_td->td_ucred : NULL);
1741 error = ENOPROTOOPT;
1749 case IPV6_2292PKTOPTIONS:
1750 #ifdef IPV6_PKTOPTIONS
1751 case IPV6_PKTOPTIONS:
1754 * RFC3542 (effectively) deprecated the
1755 * semantics of the 2292-style pktoptions.
1756 * Since it was not reliable in nature (i.e.,
1757 * applications had to expect the lack of some
1758 * information after all), it would make sense
1759 * to simplify this part by always returning
1762 sopt->sopt_valsize = 0;
1765 case IPV6_RECVHOPOPTS:
1766 case IPV6_RECVDSTOPTS:
1767 case IPV6_RECVRTHDRDSTOPTS:
1768 case IPV6_UNICAST_HOPS:
1769 case IPV6_RECVPKTINFO:
1770 case IPV6_RECVHOPLIMIT:
1771 case IPV6_RECVRTHDR:
1772 case IPV6_RECVPATHMTU:
1776 case IPV6_PORTRANGE:
1777 case IPV6_RECVTCLASS:
1778 case IPV6_AUTOFLOWLABEL:
1781 case IPV6_RECVHOPOPTS:
1782 optval = OPTBIT(IN6P_HOPOPTS);
1785 case IPV6_RECVDSTOPTS:
1786 optval = OPTBIT(IN6P_DSTOPTS);
1789 case IPV6_RECVRTHDRDSTOPTS:
1790 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1793 case IPV6_UNICAST_HOPS:
1794 optval = in6p->in6p_hops;
1797 case IPV6_RECVPKTINFO:
1798 optval = OPTBIT(IN6P_PKTINFO);
1801 case IPV6_RECVHOPLIMIT:
1802 optval = OPTBIT(IN6P_HOPLIMIT);
1805 case IPV6_RECVRTHDR:
1806 optval = OPTBIT(IN6P_RTHDR);
1809 case IPV6_RECVPATHMTU:
1810 optval = OPTBIT(IN6P_MTU);
1814 optval = OPTBIT(INP_FAITH);
1818 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1821 case IPV6_PORTRANGE:
1824 flags = in6p->inp_flags;
1825 if (flags & INP_HIGHPORT)
1826 optval = IPV6_PORTRANGE_HIGH;
1827 else if (flags & INP_LOWPORT)
1828 optval = IPV6_PORTRANGE_LOW;
1833 case IPV6_RECVTCLASS:
1834 optval = OPTBIT(IN6P_TCLASS);
1837 case IPV6_AUTOFLOWLABEL:
1838 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1842 optval = OPTBIT(INP_BINDANY);
1847 error = sooptcopyout(sopt, &optval,
1854 struct ip6_mtuinfo mtuinfo;
1855 struct route_in6 sro;
1857 bzero(&sro, sizeof(sro));
1859 if (!(so->so_state & SS_ISCONNECTED))
1862 * XXX: we dot not consider the case of source
1863 * routing, or optional information to specify
1864 * the outgoing interface.
1866 error = ip6_getpmtu(&sro, NULL, NULL,
1867 &in6p->in6p_faddr, &pmtu, NULL);
1872 if (pmtu > IPV6_MAXPACKET)
1873 pmtu = IPV6_MAXPACKET;
1875 bzero(&mtuinfo, sizeof(mtuinfo));
1876 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1877 optdata = (void *)&mtuinfo;
1878 optdatalen = sizeof(mtuinfo);
1879 error = sooptcopyout(sopt, optdata,
1884 case IPV6_2292PKTINFO:
1885 case IPV6_2292HOPLIMIT:
1886 case IPV6_2292HOPOPTS:
1887 case IPV6_2292RTHDR:
1888 case IPV6_2292DSTOPTS:
1890 case IPV6_2292PKTINFO:
1891 optval = OPTBIT(IN6P_PKTINFO);
1893 case IPV6_2292HOPLIMIT:
1894 optval = OPTBIT(IN6P_HOPLIMIT);
1896 case IPV6_2292HOPOPTS:
1897 optval = OPTBIT(IN6P_HOPOPTS);
1899 case IPV6_2292RTHDR:
1900 optval = OPTBIT(IN6P_RTHDR);
1902 case IPV6_2292DSTOPTS:
1903 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1906 error = sooptcopyout(sopt, &optval,
1913 case IPV6_RTHDRDSTOPTS:
1917 case IPV6_USE_MIN_MTU:
1918 case IPV6_PREFER_TEMPADDR:
1919 error = ip6_getpcbopt(in6p->in6p_outputopts,
1923 case IPV6_MULTICAST_IF:
1924 case IPV6_MULTICAST_HOPS:
1925 case IPV6_MULTICAST_LOOP:
1927 error = ip6_getmoptions(in6p, sopt);
1931 case IPV6_IPSEC_POLICY:
1935 struct mbuf *m = NULL;
1936 struct mbuf **mp = &m;
1937 size_t ovalsize = sopt->sopt_valsize;
1938 caddr_t oval = (caddr_t)sopt->sopt_val;
1940 error = soopt_getm(sopt, &m); /* XXX */
1943 error = soopt_mcopyin(sopt, m); /* XXX */
1946 sopt->sopt_valsize = ovalsize;
1947 sopt->sopt_val = oval;
1949 req = mtod(m, caddr_t);
1952 error = ipsec_get_policy(in6p, req, len, mp);
1954 error = soopt_mcopyout(sopt, m); /* XXX */
1955 if (error == 0 && m)
1962 error = ENOPROTOOPT;
1967 } else { /* level != IPPROTO_IPV6 */
1974 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1976 int error = 0, optval, optlen;
1977 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1978 struct inpcb *in6p = sotoinpcb(so);
1979 int level, op, optname;
1981 level = sopt->sopt_level;
1982 op = sopt->sopt_dir;
1983 optname = sopt->sopt_name;
1984 optlen = sopt->sopt_valsize;
1986 if (level != IPPROTO_IPV6) {
1993 * For ICMPv6 sockets, no modification allowed for checksum
1994 * offset, permit "no change" values to help existing apps.
1996 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
1997 * for an ICMPv6 socket will fail."
1998 * The current behavior does not meet RFC3542.
2002 if (optlen != sizeof(int)) {
2006 error = sooptcopyin(sopt, &optval, sizeof(optval),
2010 if ((optval % 2) != 0) {
2011 /* the API assumes even offset values */
2013 } else if (so->so_proto->pr_protocol ==
2015 if (optval != icmp6off)
2018 in6p->in6p_cksum = optval;
2022 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2025 optval = in6p->in6p_cksum;
2027 error = sooptcopyout(sopt, &optval, sizeof(optval));
2037 error = ENOPROTOOPT;
2045 * Set up IP6 options in pcb for insertion in output packets or
2046 * specifying behavior of outgoing packets.
2049 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2050 struct socket *so, struct sockopt *sopt)
2052 struct ip6_pktopts *opt = *pktopt;
2054 struct thread *td = sopt->sopt_td;
2056 /* turn off any old options. */
2059 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2060 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2061 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2062 printf("ip6_pcbopts: all specified options are cleared.\n");
2064 ip6_clearpktopts(opt, -1);
2066 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2069 if (!m || m->m_len == 0) {
2071 * Only turning off any previous options, regardless of
2072 * whether the opt is just created or given.
2074 free(opt, M_IP6OPT);
2078 /* set options specified by user. */
2079 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2080 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2081 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2082 free(opt, M_IP6OPT);
2090 * initialize ip6_pktopts. beware that there are non-zero default values in
2094 ip6_initpktopts(struct ip6_pktopts *opt)
2097 bzero(opt, sizeof(*opt));
2098 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2099 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2100 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2101 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2105 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2106 struct ucred *cred, int uproto)
2108 struct ip6_pktopts *opt;
2110 if (*pktopt == NULL) {
2111 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2113 ip6_initpktopts(*pktopt);
2117 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2121 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2123 void *optdata = NULL;
2125 struct ip6_ext *ip6e;
2127 struct in6_pktinfo null_pktinfo;
2128 int deftclass = 0, on;
2129 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2130 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2134 if (pktopt && pktopt->ip6po_pktinfo)
2135 optdata = (void *)pktopt->ip6po_pktinfo;
2137 /* XXX: we don't have to do this every time... */
2138 bzero(&null_pktinfo, sizeof(null_pktinfo));
2139 optdata = (void *)&null_pktinfo;
2141 optdatalen = sizeof(struct in6_pktinfo);
2144 if (pktopt && pktopt->ip6po_tclass >= 0)
2145 optdata = (void *)&pktopt->ip6po_tclass;
2147 optdata = (void *)&deftclass;
2148 optdatalen = sizeof(int);
2151 if (pktopt && pktopt->ip6po_hbh) {
2152 optdata = (void *)pktopt->ip6po_hbh;
2153 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2154 optdatalen = (ip6e->ip6e_len + 1) << 3;
2158 if (pktopt && pktopt->ip6po_rthdr) {
2159 optdata = (void *)pktopt->ip6po_rthdr;
2160 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2161 optdatalen = (ip6e->ip6e_len + 1) << 3;
2164 case IPV6_RTHDRDSTOPTS:
2165 if (pktopt && pktopt->ip6po_dest1) {
2166 optdata = (void *)pktopt->ip6po_dest1;
2167 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2168 optdatalen = (ip6e->ip6e_len + 1) << 3;
2172 if (pktopt && pktopt->ip6po_dest2) {
2173 optdata = (void *)pktopt->ip6po_dest2;
2174 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2175 optdatalen = (ip6e->ip6e_len + 1) << 3;
2179 if (pktopt && pktopt->ip6po_nexthop) {
2180 optdata = (void *)pktopt->ip6po_nexthop;
2181 optdatalen = pktopt->ip6po_nexthop->sa_len;
2184 case IPV6_USE_MIN_MTU:
2186 optdata = (void *)&pktopt->ip6po_minmtu;
2188 optdata = (void *)&defminmtu;
2189 optdatalen = sizeof(int);
2192 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2196 optdata = (void *)&on;
2197 optdatalen = sizeof(on);
2199 case IPV6_PREFER_TEMPADDR:
2201 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2203 optdata = (void *)&defpreftemp;
2204 optdatalen = sizeof(int);
2206 default: /* should not happen */
2208 panic("ip6_getpcbopt: unexpected option\n");
2210 return (ENOPROTOOPT);
2213 error = sooptcopyout(sopt, optdata, optdatalen);
2219 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2224 if (optname == -1 || optname == IPV6_PKTINFO) {
2225 if (pktopt->ip6po_pktinfo)
2226 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2227 pktopt->ip6po_pktinfo = NULL;
2229 if (optname == -1 || optname == IPV6_HOPLIMIT)
2230 pktopt->ip6po_hlim = -1;
2231 if (optname == -1 || optname == IPV6_TCLASS)
2232 pktopt->ip6po_tclass = -1;
2233 if (optname == -1 || optname == IPV6_NEXTHOP) {
2234 if (pktopt->ip6po_nextroute.ro_rt) {
2235 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2236 pktopt->ip6po_nextroute.ro_rt = NULL;
2238 if (pktopt->ip6po_nexthop)
2239 free(pktopt->ip6po_nexthop, M_IP6OPT);
2240 pktopt->ip6po_nexthop = NULL;
2242 if (optname == -1 || optname == IPV6_HOPOPTS) {
2243 if (pktopt->ip6po_hbh)
2244 free(pktopt->ip6po_hbh, M_IP6OPT);
2245 pktopt->ip6po_hbh = NULL;
2247 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2248 if (pktopt->ip6po_dest1)
2249 free(pktopt->ip6po_dest1, M_IP6OPT);
2250 pktopt->ip6po_dest1 = NULL;
2252 if (optname == -1 || optname == IPV6_RTHDR) {
2253 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2254 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2255 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2256 if (pktopt->ip6po_route.ro_rt) {
2257 RTFREE(pktopt->ip6po_route.ro_rt);
2258 pktopt->ip6po_route.ro_rt = NULL;
2261 if (optname == -1 || optname == IPV6_DSTOPTS) {
2262 if (pktopt->ip6po_dest2)
2263 free(pktopt->ip6po_dest2, M_IP6OPT);
2264 pktopt->ip6po_dest2 = NULL;
2268 #define PKTOPT_EXTHDRCPY(type) \
2271 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2272 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2273 if (dst->type == NULL && canwait == M_NOWAIT)\
2275 bcopy(src->type, dst->type, hlen);\
2277 } while (/*CONSTCOND*/ 0)
2280 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2282 if (dst == NULL || src == NULL) {
2283 printf("ip6_clearpktopts: invalid argument\n");
2287 dst->ip6po_hlim = src->ip6po_hlim;
2288 dst->ip6po_tclass = src->ip6po_tclass;
2289 dst->ip6po_flags = src->ip6po_flags;
2290 if (src->ip6po_pktinfo) {
2291 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2293 if (dst->ip6po_pktinfo == NULL)
2295 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2297 if (src->ip6po_nexthop) {
2298 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2300 if (dst->ip6po_nexthop == NULL)
2302 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2303 src->ip6po_nexthop->sa_len);
2305 PKTOPT_EXTHDRCPY(ip6po_hbh);
2306 PKTOPT_EXTHDRCPY(ip6po_dest1);
2307 PKTOPT_EXTHDRCPY(ip6po_dest2);
2308 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2312 ip6_clearpktopts(dst, -1);
2315 #undef PKTOPT_EXTHDRCPY
2317 struct ip6_pktopts *
2318 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2321 struct ip6_pktopts *dst;
2323 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2326 ip6_initpktopts(dst);
2328 if ((error = copypktopts(dst, src, canwait)) != 0) {
2329 free(dst, M_IP6OPT);
2337 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2342 ip6_clearpktopts(pktopt, -1);
2344 free(pktopt, M_IP6OPT);
2348 * Set IPv6 outgoing packet options based on advanced API.
2351 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2352 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2354 struct cmsghdr *cm = 0;
2356 if (control == NULL || opt == NULL)
2359 ip6_initpktopts(opt);
2364 * If stickyopt is provided, make a local copy of the options
2365 * for this particular packet, then override them by ancillary
2367 * XXX: copypktopts() does not copy the cached route to a next
2368 * hop (if any). This is not very good in terms of efficiency,
2369 * but we can allow this since this option should be rarely
2372 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2377 * XXX: Currently, we assume all the optional information is stored
2380 if (control->m_next)
2383 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2384 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2387 if (control->m_len < CMSG_LEN(0))
2390 cm = mtod(control, struct cmsghdr *);
2391 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2393 if (cm->cmsg_level != IPPROTO_IPV6)
2396 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2397 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2406 * Set a particular packet option, as a sticky option or an ancillary data
2407 * item. "len" can be 0 only when it's a sticky option.
2408 * We have 4 cases of combination of "sticky" and "cmsg":
2409 * "sticky=0, cmsg=0": impossible
2410 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2411 * "sticky=1, cmsg=0": RFC3542 socket option
2412 * "sticky=1, cmsg=1": RFC2292 socket option
2415 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2416 struct ucred *cred, int sticky, int cmsg, int uproto)
2418 int minmtupolicy, preftemp;
2421 if (!sticky && !cmsg) {
2423 printf("ip6_setpktopt: impossible case\n");
2429 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2430 * not be specified in the context of RFC3542. Conversely,
2431 * RFC3542 types should not be specified in the context of RFC2292.
2435 case IPV6_2292PKTINFO:
2436 case IPV6_2292HOPLIMIT:
2437 case IPV6_2292NEXTHOP:
2438 case IPV6_2292HOPOPTS:
2439 case IPV6_2292DSTOPTS:
2440 case IPV6_2292RTHDR:
2441 case IPV6_2292PKTOPTIONS:
2442 return (ENOPROTOOPT);
2445 if (sticky && cmsg) {
2452 case IPV6_RTHDRDSTOPTS:
2454 case IPV6_USE_MIN_MTU:
2457 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2458 return (ENOPROTOOPT);
2463 case IPV6_2292PKTINFO:
2466 struct ifnet *ifp = NULL;
2467 struct in6_pktinfo *pktinfo;
2469 if (len != sizeof(struct in6_pktinfo))
2472 pktinfo = (struct in6_pktinfo *)buf;
2475 * An application can clear any sticky IPV6_PKTINFO option by
2476 * doing a "regular" setsockopt with ipi6_addr being
2477 * in6addr_any and ipi6_ifindex being zero.
2478 * [RFC 3542, Section 6]
2480 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2481 pktinfo->ipi6_ifindex == 0 &&
2482 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2483 ip6_clearpktopts(opt, optname);
2487 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2488 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2492 /* validate the interface index if specified. */
2493 if (pktinfo->ipi6_ifindex > V_if_index ||
2494 pktinfo->ipi6_ifindex < 0) {
2497 if (pktinfo->ipi6_ifindex) {
2498 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2504 * We store the address anyway, and let in6_selectsrc()
2505 * validate the specified address. This is because ipi6_addr
2506 * may not have enough information about its scope zone, and
2507 * we may need additional information (such as outgoing
2508 * interface or the scope zone of a destination address) to
2509 * disambiguate the scope.
2510 * XXX: the delay of the validation may confuse the
2511 * application when it is used as a sticky option.
2513 if (opt->ip6po_pktinfo == NULL) {
2514 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2515 M_IP6OPT, M_NOWAIT);
2516 if (opt->ip6po_pktinfo == NULL)
2519 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2523 case IPV6_2292HOPLIMIT:
2529 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2530 * to simplify the ordering among hoplimit options.
2532 if (optname == IPV6_HOPLIMIT && sticky)
2533 return (ENOPROTOOPT);
2535 if (len != sizeof(int))
2538 if (*hlimp < -1 || *hlimp > 255)
2541 opt->ip6po_hlim = *hlimp;
2549 if (len != sizeof(int))
2551 tclass = *(int *)buf;
2552 if (tclass < -1 || tclass > 255)
2555 opt->ip6po_tclass = tclass;
2559 case IPV6_2292NEXTHOP:
2562 error = priv_check_cred(cred,
2563 PRIV_NETINET_SETHDROPTS, 0);
2568 if (len == 0) { /* just remove the option */
2569 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2573 /* check if cmsg_len is large enough for sa_len */
2574 if (len < sizeof(struct sockaddr) || len < *buf)
2577 switch (((struct sockaddr *)buf)->sa_family) {
2580 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2583 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2586 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2587 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2590 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2596 case AF_LINK: /* should eventually be supported */
2598 return (EAFNOSUPPORT);
2601 /* turn off the previous option, then set the new option. */
2602 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2603 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2604 if (opt->ip6po_nexthop == NULL)
2606 bcopy(buf, opt->ip6po_nexthop, *buf);
2609 case IPV6_2292HOPOPTS:
2612 struct ip6_hbh *hbh;
2616 * XXX: We don't allow a non-privileged user to set ANY HbH
2617 * options, since per-option restriction has too much
2621 error = priv_check_cred(cred,
2622 PRIV_NETINET_SETHDROPTS, 0);
2628 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2629 break; /* just remove the option */
2632 /* message length validation */
2633 if (len < sizeof(struct ip6_hbh))
2635 hbh = (struct ip6_hbh *)buf;
2636 hbhlen = (hbh->ip6h_len + 1) << 3;
2640 /* turn off the previous option, then set the new option. */
2641 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2642 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2643 if (opt->ip6po_hbh == NULL)
2645 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2650 case IPV6_2292DSTOPTS:
2652 case IPV6_RTHDRDSTOPTS:
2654 struct ip6_dest *dest, **newdest = NULL;
2657 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2658 error = priv_check_cred(cred,
2659 PRIV_NETINET_SETHDROPTS, 0);
2665 ip6_clearpktopts(opt, optname);
2666 break; /* just remove the option */
2669 /* message length validation */
2670 if (len < sizeof(struct ip6_dest))
2672 dest = (struct ip6_dest *)buf;
2673 destlen = (dest->ip6d_len + 1) << 3;
2678 * Determine the position that the destination options header
2679 * should be inserted; before or after the routing header.
2682 case IPV6_2292DSTOPTS:
2684 * The old advacned API is ambiguous on this point.
2685 * Our approach is to determine the position based
2686 * according to the existence of a routing header.
2687 * Note, however, that this depends on the order of the
2688 * extension headers in the ancillary data; the 1st
2689 * part of the destination options header must appear
2690 * before the routing header in the ancillary data,
2692 * RFC3542 solved the ambiguity by introducing
2693 * separate ancillary data or option types.
2695 if (opt->ip6po_rthdr == NULL)
2696 newdest = &opt->ip6po_dest1;
2698 newdest = &opt->ip6po_dest2;
2700 case IPV6_RTHDRDSTOPTS:
2701 newdest = &opt->ip6po_dest1;
2704 newdest = &opt->ip6po_dest2;
2708 /* turn off the previous option, then set the new option. */
2709 ip6_clearpktopts(opt, optname);
2710 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2711 if (*newdest == NULL)
2713 bcopy(dest, *newdest, destlen);
2718 case IPV6_2292RTHDR:
2721 struct ip6_rthdr *rth;
2725 ip6_clearpktopts(opt, IPV6_RTHDR);
2726 break; /* just remove the option */
2729 /* message length validation */
2730 if (len < sizeof(struct ip6_rthdr))
2732 rth = (struct ip6_rthdr *)buf;
2733 rthlen = (rth->ip6r_len + 1) << 3;
2737 switch (rth->ip6r_type) {
2738 case IPV6_RTHDR_TYPE_0:
2739 if (rth->ip6r_len == 0) /* must contain one addr */
2741 if (rth->ip6r_len % 2) /* length must be even */
2743 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2747 return (EINVAL); /* not supported */
2750 /* turn off the previous option */
2751 ip6_clearpktopts(opt, IPV6_RTHDR);
2752 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2753 if (opt->ip6po_rthdr == NULL)
2755 bcopy(rth, opt->ip6po_rthdr, rthlen);
2760 case IPV6_USE_MIN_MTU:
2761 if (len != sizeof(int))
2763 minmtupolicy = *(int *)buf;
2764 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2765 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2766 minmtupolicy != IP6PO_MINMTU_ALL) {
2769 opt->ip6po_minmtu = minmtupolicy;
2773 if (len != sizeof(int))
2776 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2778 * we ignore this option for TCP sockets.
2779 * (RFC3542 leaves this case unspecified.)
2781 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2783 opt->ip6po_flags |= IP6PO_DONTFRAG;
2786 case IPV6_PREFER_TEMPADDR:
2787 if (len != sizeof(int))
2789 preftemp = *(int *)buf;
2790 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2791 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2792 preftemp != IP6PO_TEMPADDR_PREFER) {
2795 opt->ip6po_prefer_tempaddr = preftemp;
2799 return (ENOPROTOOPT);
2800 } /* end of switch */
2806 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2807 * packet to the input queue of a specified interface. Note that this
2808 * calls the output routine of the loopback "driver", but with an interface
2809 * pointer that might NOT be &loif -- easier than replicating that code here.
2812 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2815 struct ip6_hdr *ip6;
2817 copym = m_copy(m, 0, M_COPYALL);
2822 * Make sure to deep-copy IPv6 header portion in case the data
2823 * is in an mbuf cluster, so that we can safely override the IPv6
2824 * header portion later.
2826 if ((copym->m_flags & M_EXT) != 0 ||
2827 copym->m_len < sizeof(struct ip6_hdr)) {
2828 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2834 if (copym->m_len < sizeof(*ip6)) {
2840 ip6 = mtod(copym, struct ip6_hdr *);
2842 * clear embedded scope identifiers if necessary.
2843 * in6_clearscope will touch the addresses only when necessary.
2845 in6_clearscope(&ip6->ip6_src);
2846 in6_clearscope(&ip6->ip6_dst);
2848 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2852 * Chop IPv6 header off from the payload.
2855 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2858 struct ip6_hdr *ip6;
2860 ip6 = mtod(m, struct ip6_hdr *);
2861 if (m->m_len > sizeof(*ip6)) {
2862 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2867 M_MOVE_PKTHDR(mh, m);
2868 MH_ALIGN(mh, sizeof(*ip6));
2869 m->m_len -= sizeof(*ip6);
2870 m->m_data += sizeof(*ip6);
2873 m->m_len = sizeof(*ip6);
2874 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2876 exthdrs->ip6e_ip6 = m;
2881 * Compute IPv6 extension header length.
2884 ip6_optlen(struct inpcb *in6p)
2888 if (!in6p->in6p_outputopts)
2893 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2895 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2896 if (in6p->in6p_outputopts->ip6po_rthdr)
2897 /* dest1 is valid with rthdr only */
2898 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2899 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2900 len += elen(in6p->in6p_outputopts->ip6po_dest2);