2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
71 #include "opt_route.h"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
86 #include <machine/in_cksum.h>
89 #include <net/netisr.h>
90 #include <net/route.h>
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet/ip6.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/tcp_var.h>
103 #include <netinet6/nd6.h>
106 #include <netipsec/ipsec.h>
107 #include <netipsec/ipsec6.h>
108 #include <netipsec/key.h>
109 #include <netinet6/ip6_ipsec.h>
112 #include <netinet/sctp.h>
113 #include <netinet/sctp_crc32.h>
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/scope6_var.h>
120 #include <net/flowtable.h>
123 extern int in6_mcast_loop;
126 struct mbuf *ip6e_ip6;
127 struct mbuf *ip6e_hbh;
128 struct mbuf *ip6e_dest1;
129 struct mbuf *ip6e_rthdr;
130 struct mbuf *ip6e_dest2;
133 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
134 struct ucred *, int));
135 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
136 struct socket *, struct sockopt *));
137 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
138 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *,
139 struct ucred *, int, int, int));
141 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
142 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
143 struct ip6_frag **));
144 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
145 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
146 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
147 struct ifnet *, struct in6_addr *, u_long *, int *, u_int));
148 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
152 * Make an extension header from option data. hp is the source, and
153 * mp is the destination.
155 #define MAKE_EXTHDR(hp, mp) \
158 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
159 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
160 ((eh)->ip6e_len + 1) << 3); \
164 } while (/*CONSTCOND*/ 0)
167 * Form a chain of extension headers.
168 * m is the extension header mbuf
169 * mp is the previous mbuf in the chain
170 * p is the next header
171 * i is the type of option.
173 #define MAKE_CHAIN(m, mp, p, i)\
177 panic("assumption failed: hdr not split"); \
178 *mtod((m), u_char *) = *(p);\
180 p = mtod((m), u_char *);\
181 (m)->m_next = (mp)->m_next;\
185 } while (/*CONSTCOND*/ 0)
188 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
192 csum = in_cksum_skip(m, offset + plen, offset);
193 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
195 offset += m->m_pkthdr.csum_data; /* checksum offset */
197 if (offset + sizeof(u_short) > m->m_len) {
198 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
199 "csum_flags=0x%04x\n", __func__, m->m_len, plen, offset,
200 m->m_pkthdr.csum_flags);
202 * XXX this should not happen, but if it does, the correct
203 * behavior may be to insert the checksum in the appropriate
204 * next mbuf in the chain.
208 *(u_short *)(m->m_data + offset) = csum;
212 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
213 * header (with pri, len, nxt, hlim, src, dst).
214 * This function may modify ver and hlim only.
215 * The mbuf chain containing the packet will be freed.
216 * The mbuf opt, if present, will not be freed.
218 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
219 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
220 * which is rt_rmx.rmx_mtu.
222 * ifpp - XXX: just for statistics
225 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
226 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
227 struct ifnet **ifpp, struct inpcb *inp)
229 struct ip6_hdr *ip6, *mhip6;
230 struct ifnet *ifp, *origifp;
232 struct mbuf *mprev = NULL;
233 int hlen, tlen, len, off;
234 struct route_in6 ip6route;
235 struct rtentry *rt = NULL;
236 struct sockaddr_in6 *dst, src_sa, dst_sa;
237 struct in6_addr odst;
239 struct in6_ifaddr *ia = NULL;
241 int alwaysfrag, dontfrag;
242 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
243 struct ip6_exthdrs exthdrs;
244 struct in6_addr finaldst, src0, dst0;
246 struct route_in6 *ro_pmtu = NULL;
252 struct ipsec_output_state state;
253 struct ip6_rthdr *rh = NULL;
254 int needipsectun = 0;
256 struct secpolicy *sp = NULL;
258 #ifdef IPFIREWALL_FORWARD
259 struct m_tag *fwd_tag;
262 ip6 = mtod(m, struct ip6_hdr *);
264 printf ("ip6 is NULL");
269 M_SETFIB(m, inp->inp_inc.inc_fibnum);
271 finaldst = ip6->ip6_dst;
272 bzero(&exthdrs, sizeof(exthdrs));
274 /* Hop-by-Hop options header */
275 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
276 /* Destination options header(1st part) */
277 if (opt->ip6po_rthdr) {
279 * Destination options header(1st part)
280 * This only makes sense with a routing header.
281 * See Section 9.2 of RFC 3542.
282 * Disabling this part just for MIP6 convenience is
283 * a bad idea. We need to think carefully about a
284 * way to make the advanced API coexist with MIP6
285 * options, which might automatically be inserted in
288 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
291 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
292 /* Destination options header(2nd part) */
293 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
298 * IPSec checking which handles several cases.
299 * FAST IPSEC: We re-injected the packet.
301 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
303 case 1: /* Bad packet */
305 case -1: /* Do IPSec */
307 case 0: /* No IPSec */
314 * Calculate the total length of the extension header chain.
315 * Keep the length of the unfragmentable part for fragmentation.
318 if (exthdrs.ip6e_hbh)
319 optlen += exthdrs.ip6e_hbh->m_len;
320 if (exthdrs.ip6e_dest1)
321 optlen += exthdrs.ip6e_dest1->m_len;
322 if (exthdrs.ip6e_rthdr)
323 optlen += exthdrs.ip6e_rthdr->m_len;
324 unfragpartlen = optlen + sizeof(struct ip6_hdr);
326 /* NOTE: we don't add AH/ESP length here. do that later. */
327 if (exthdrs.ip6e_dest2)
328 optlen += exthdrs.ip6e_dest2->m_len;
331 * If we need IPsec, or there is at least one extension header,
332 * separate IP6 header from the payload.
334 if ((needipsec || optlen) && !hdrsplit) {
335 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
339 m = exthdrs.ip6e_ip6;
344 ip6 = mtod(m, struct ip6_hdr *);
346 /* adjust mbuf packet header length */
347 m->m_pkthdr.len += optlen;
348 plen = m->m_pkthdr.len - sizeof(*ip6);
350 /* If this is a jumbo payload, insert a jumbo payload option. */
351 if (plen > IPV6_MAXPACKET) {
353 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
357 m = exthdrs.ip6e_ip6;
361 ip6 = mtod(m, struct ip6_hdr *);
362 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
366 ip6->ip6_plen = htons(plen);
369 * Concatenate headers and fill in next header fields.
370 * Here we have, on "m"
372 * and we insert headers accordingly. Finally, we should be getting:
373 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
375 * during the header composing process, "m" points to IPv6 header.
376 * "mprev" points to an extension header prior to esp.
378 u_char *nexthdrp = &ip6->ip6_nxt;
382 * we treat dest2 specially. this makes IPsec processing
383 * much easier. the goal here is to make mprev point the
384 * mbuf prior to dest2.
386 * result: IPv6 dest2 payload
387 * m and mprev will point to IPv6 header.
389 if (exthdrs.ip6e_dest2) {
391 panic("assumption failed: hdr not split");
392 exthdrs.ip6e_dest2->m_next = m->m_next;
393 m->m_next = exthdrs.ip6e_dest2;
394 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
395 ip6->ip6_nxt = IPPROTO_DSTOPTS;
399 * result: IPv6 hbh dest1 rthdr dest2 payload
400 * m will point to IPv6 header. mprev will point to the
401 * extension header prior to dest2 (rthdr in the above case).
403 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
404 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
406 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
414 * pointers after IPsec headers are not valid any more.
415 * other pointers need a great care too.
416 * (IPsec routines should not mangle mbufs prior to AH/ESP)
418 exthdrs.ip6e_dest2 = NULL;
420 if (exthdrs.ip6e_rthdr) {
421 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
422 segleft_org = rh->ip6r_segleft;
423 rh->ip6r_segleft = 0;
426 bzero(&state, sizeof(state));
428 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
431 if (error == EJUSTRETURN) {
433 * We had a SP with a level of 'use' and no SA. We
434 * will just continue to process the packet without
439 /* mbuf is already reclaimed in ipsec6_output_trans. */
449 printf("[%s:%d] (ipsec): error code %d\n",
450 __func__, __LINE__, error);
453 /* don't show these error codes to the user */
458 } else if (!needipsectun) {
460 * In the FAST IPSec case we have already
461 * re-injected the packet and it has been freed
462 * by the ipsec_done() function. So, just clean
463 * up after ourselves.
468 if (exthdrs.ip6e_rthdr) {
469 /* ah6_output doesn't modify mbuf chain */
470 rh->ip6r_segleft = segleft_org;
476 * If there is a routing header, discard the packet.
478 if (exthdrs.ip6e_rthdr) {
483 /* Source address validation */
484 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
485 (flags & IPV6_UNSPECSRC) == 0) {
487 V_ip6stat.ip6s_badscope++;
490 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
492 V_ip6stat.ip6s_badscope++;
496 V_ip6stat.ip6s_localout++;
503 bzero((caddr_t)ro, sizeof(*ro));
506 if (opt && opt->ip6po_rthdr)
507 ro = &opt->ip6po_route;
508 dst = (struct sockaddr_in6 *)&ro->ro_dst;
510 if (ro == &ip6route) {
514 * The flow table returns route entries valid for up to 30
515 * seconds; we rely on the remainder of ip_output() taking no
516 * longer than that long for the stability of ro_rt. The
517 * flow ID assignment must have happened before this point.
519 if ((fle = flowtable_lookup_mbuf(V_ip6_ft, m, AF_INET6)) != NULL) {
520 flow_to_route_in6(fle, ro);
521 if (ro->ro_rt != NULL && ro->ro_lle != NULL)
528 * if specified, try to fill in the traffic class field.
529 * do not override if a non-zero value is already set.
530 * we check the diffserv field and the ecn field separately.
532 if (opt && opt->ip6po_tclass >= 0) {
535 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
537 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
540 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
543 /* fill in or override the hop limit field, if necessary. */
544 if (opt && opt->ip6po_hlim != -1)
545 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
546 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
548 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
550 ip6->ip6_hlim = V_ip6_defmcasthlim;
555 * We may re-inject packets into the stack here.
557 if (needipsec && needipsectun) {
558 struct ipsec_output_state state;
561 * All the extension headers will become inaccessible
562 * (since they can be encrypted).
563 * Don't panic, we need no more updates to extension headers
564 * on inner IPv6 packet (since they are now encapsulated).
566 * IPv6 [ESP|AH] IPv6 [extension headers] payload
568 bzero(&exthdrs, sizeof(exthdrs));
569 exthdrs.ip6e_ip6 = m;
571 bzero(&state, sizeof(state));
573 state.ro = (struct route *)ro;
574 state.dst = (struct sockaddr *)dst;
576 error = ipsec6_output_tunnel(&state, sp, flags);
579 ro = (struct route_in6 *)state.ro;
580 dst = (struct sockaddr_in6 *)state.dst;
581 if (error == EJUSTRETURN) {
583 * We had a SP with a level of 'use' and no SA. We
584 * will just continue to process the packet without
589 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
600 printf("[%s:%d] (ipsec): error code %d\n",
601 __func__, __LINE__, error);
604 /* don't show these error codes to the user */
611 * In the FAST IPSec case we have already
612 * re-injected the packet and it has been freed
613 * by the ipsec_done() function. So, just clean
614 * up after ourselves.
620 exthdrs.ip6e_ip6 = m;
625 ip6 = mtod(m, struct ip6_hdr *);
627 bzero(&dst_sa, sizeof(dst_sa));
628 dst_sa.sin6_family = AF_INET6;
629 dst_sa.sin6_len = sizeof(dst_sa);
630 dst_sa.sin6_addr = ip6->ip6_dst;
633 ifp = ro->ro_rt->rt_ifp;
634 } else if ((error = in6_selectroute_fib(&dst_sa, opt, im6o, ro,
635 &ifp, &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0) {
638 V_ip6stat.ip6s_noroute++;
642 break; /* XXX statistics? */
645 in6_ifstat_inc(ifp, ifs6_out_discard);
650 * If in6_selectroute() does not return a route entry,
651 * dst may not have been updated.
653 *dst = dst_sa; /* XXX */
657 * then rt (for unicast) and ifp must be non-NULL valid values.
659 if ((flags & IPV6_FORWARDING) == 0) {
660 /* XXX: the FORWARDING flag can be set for mrouting. */
661 in6_ifstat_inc(ifp, ifs6_out_request);
664 ia = (struct in6_ifaddr *)(rt->rt_ifa);
670 * The outgoing interface must be in the zone of source and
671 * destination addresses.
676 if (in6_setscope(&src0, origifp, &zone))
678 bzero(&src_sa, sizeof(src_sa));
679 src_sa.sin6_family = AF_INET6;
680 src_sa.sin6_len = sizeof(src_sa);
681 src_sa.sin6_addr = ip6->ip6_src;
682 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
686 if (in6_setscope(&dst0, origifp, &zone))
688 /* re-initialize to be sure */
689 bzero(&dst_sa, sizeof(dst_sa));
690 dst_sa.sin6_family = AF_INET6;
691 dst_sa.sin6_len = sizeof(dst_sa);
692 dst_sa.sin6_addr = ip6->ip6_dst;
693 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
697 /* We should use ia_ifp to support the case of
698 * sending packets to an address of our own.
700 if (ia != NULL && ia->ia_ifp)
703 /* scope check is done. */
707 V_ip6stat.ip6s_badscope++;
708 in6_ifstat_inc(origifp, ifs6_out_discard);
710 error = EHOSTUNREACH; /* XXX */
714 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
715 if (opt && opt->ip6po_nextroute.ro_rt) {
717 * The nexthop is explicitly specified by the
718 * application. We assume the next hop is an IPv6
721 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
723 else if ((rt->rt_flags & RTF_GATEWAY))
724 dst = (struct sockaddr_in6 *)rt->rt_gateway;
727 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
728 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
730 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
731 in6_ifstat_inc(ifp, ifs6_out_mcast);
733 * Confirm that the outgoing interface supports multicast.
735 if (!(ifp->if_flags & IFF_MULTICAST)) {
736 V_ip6stat.ip6s_noroute++;
737 in6_ifstat_inc(ifp, ifs6_out_discard);
741 if ((im6o == NULL && in6_mcast_loop) ||
742 (im6o && im6o->im6o_multicast_loop)) {
744 * Loop back multicast datagram if not expressly
745 * forbidden to do so, even if we have not joined
746 * the address; protocols will filter it later,
747 * thus deferring a hash lookup and lock acquisition
748 * at the expense of an m_copym().
750 ip6_mloopback(ifp, m, dst);
753 * If we are acting as a multicast router, perform
754 * multicast forwarding as if the packet had just
755 * arrived on the interface to which we are about
756 * to send. The multicast forwarding function
757 * recursively calls this function, using the
758 * IPV6_FORWARDING flag to prevent infinite recursion.
760 * Multicasts that are looped back by ip6_mloopback(),
761 * above, will be forwarded by the ip6_input() routine,
764 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
766 * XXX: ip6_mforward expects that rcvif is NULL
767 * when it is called from the originating path.
768 * However, it is not always the case, since
769 * some versions of MGETHDR() does not
770 * initialize the field.
772 m->m_pkthdr.rcvif = NULL;
773 if (ip6_mforward(ip6, ifp, m) != 0) {
780 * Multicasts with a hoplimit of zero may be looped back,
781 * above, but must not be transmitted on a network.
782 * Also, multicasts addressed to the loopback interface
783 * are not sent -- the above call to ip6_mloopback() will
784 * loop back a copy if this host actually belongs to the
785 * destination group on the loopback interface.
787 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
788 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
795 * Fill the outgoing inteface to tell the upper layer
796 * to increment per-interface statistics.
801 /* Determine path MTU. */
802 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
803 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
807 * The caller of this function may specify to use the minimum MTU
809 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
810 * setting. The logic is a bit complicated; by default, unicast
811 * packets will follow path MTU while multicast packets will be sent at
812 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
813 * including unicast ones will be sent at the minimum MTU. Multicast
814 * packets will always be sent at the minimum MTU unless
815 * IP6PO_MINMTU_DISABLE is explicitly specified.
816 * See RFC 3542 for more details.
818 if (mtu > IPV6_MMTU) {
819 if ((flags & IPV6_MINMTU))
821 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
823 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
825 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
831 * clear embedded scope identifiers if necessary.
832 * in6_clearscope will touch the addresses only when necessary.
834 in6_clearscope(&ip6->ip6_src);
835 in6_clearscope(&ip6->ip6_dst);
838 * If the outgoing packet contains a hop-by-hop options header,
839 * it must be examined and processed even by the source node.
840 * (RFC 2460, section 4.)
842 if (exthdrs.ip6e_hbh) {
843 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
844 u_int32_t dummy; /* XXX unused */
845 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
848 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
849 panic("ip6e_hbh is not contiguous");
852 * XXX: if we have to send an ICMPv6 error to the sender,
853 * we need the M_LOOP flag since icmp6_error() expects
854 * the IPv6 and the hop-by-hop options header are
855 * contiguous unless the flag is set.
857 m->m_flags |= M_LOOP;
858 m->m_pkthdr.rcvif = ifp;
859 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
860 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
861 &dummy, &plen) < 0) {
862 /* m was already freed at this point */
863 error = EINVAL;/* better error? */
866 m->m_flags &= ~M_LOOP; /* XXX */
867 m->m_pkthdr.rcvif = NULL;
870 /* Jump over all PFIL processing if hooks are not active. */
871 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
875 /* Run through list of hooks for output packets. */
876 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
877 if (error != 0 || m == NULL)
879 ip6 = mtod(m, struct ip6_hdr *);
881 /* See if destination IP address was changed by packet filter. */
882 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
883 m->m_flags |= M_SKIP_FIREWALL;
884 /* If destination is now ourself drop to ip6_input(). */
885 if (in6_localip(&ip6->ip6_dst)) {
886 m->m_flags |= M_FASTFWD_OURS;
887 if (m->m_pkthdr.rcvif == NULL)
888 m->m_pkthdr.rcvif = V_loif;
889 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
890 m->m_pkthdr.csum_flags |=
891 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
892 m->m_pkthdr.csum_data = 0xffff;
895 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
896 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
898 error = netisr_queue(NETISR_IPV6, m);
901 goto again; /* Redo the routing table lookup. */
904 #ifdef IPFIREWALL_FORWARD
905 /* See if local, if yes, send it to netisr. */
906 if (m->m_flags & M_FASTFWD_OURS) {
907 if (m->m_pkthdr.rcvif == NULL)
908 m->m_pkthdr.rcvif = V_loif;
909 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
910 m->m_pkthdr.csum_flags |=
911 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
912 m->m_pkthdr.csum_data = 0xffff;
915 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
916 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
918 error = netisr_queue(NETISR_IPV6, m);
921 /* Or forward to some other address? */
922 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
924 dst = (struct sockaddr_in6 *)&ro->ro_dst;
925 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in6));
926 m->m_flags |= M_SKIP_FIREWALL;
927 m_tag_delete(m, fwd_tag);
930 #endif /* IPFIREWALL_FORWARD */
934 * Send the packet to the outgoing interface.
935 * If necessary, do IPv6 fragmentation before sending.
937 * the logic here is rather complex:
938 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
939 * 1-a: send as is if tlen <= path mtu
940 * 1-b: fragment if tlen > path mtu
942 * 2: if user asks us not to fragment (dontfrag == 1)
943 * 2-a: send as is if tlen <= interface mtu
944 * 2-b: error if tlen > interface mtu
946 * 3: if we always need to attach fragment header (alwaysfrag == 1)
949 * 4: if dontfrag == 1 && alwaysfrag == 1
950 * error, as we cannot handle this conflicting request
952 sw_csum = m->m_pkthdr.csum_flags;
954 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
955 sw_csum &= ~ifp->if_hwassist;
959 * If we added extension headers, we will not do TSO and calculate the
960 * checksums ourselves for now.
961 * XXX-BZ Need a framework to know when the NIC can handle it, even
964 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
965 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
966 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
969 if (sw_csum & CSUM_SCTP_IPV6) {
970 sw_csum &= ~CSUM_SCTP_IPV6;
971 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
974 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
975 tlen = m->m_pkthdr.len;
977 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
981 if (dontfrag && alwaysfrag) { /* case 4 */
982 /* conflicting request - can't transmit */
986 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
988 * Even if the DONTFRAG option is specified, we cannot send the
989 * packet when the data length is larger than the MTU of the
990 * outgoing interface.
991 * Notify the error by sending IPV6_PATHMTU ancillary data as
992 * well as returning an error code (the latter is not described
996 struct ip6ctlparam ip6cp;
998 mtu32 = (u_int32_t)mtu;
999 bzero(&ip6cp, sizeof(ip6cp));
1000 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1001 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1009 * transmit packet without fragmentation
1011 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1012 struct in6_ifaddr *ia6;
1014 ip6 = mtod(m, struct ip6_hdr *);
1015 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1017 /* Record statistics for this interface address. */
1018 ia6->ia_ifa.if_opackets++;
1019 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
1020 ifa_free(&ia6->ia_ifa);
1022 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1027 * try to fragment the packet. case 1-b and 3
1029 if (mtu < IPV6_MMTU) {
1030 /* path MTU cannot be less than IPV6_MMTU */
1032 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1034 } else if (ip6->ip6_plen == 0) {
1035 /* jumbo payload cannot be fragmented */
1037 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1040 struct mbuf **mnext, *m_frgpart;
1041 struct ip6_frag *ip6f;
1042 u_int32_t id = htonl(ip6_randomid());
1045 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1048 * Too large for the destination or interface;
1049 * fragment if possible.
1050 * Must be able to put at least 8 bytes per fragment.
1052 hlen = unfragpartlen;
1053 if (mtu > IPV6_MAXPACKET)
1054 mtu = IPV6_MAXPACKET;
1056 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1059 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1064 * Verify that we have any chance at all of being able to queue
1065 * the packet or packet fragments
1067 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1068 < tlen /* - hlen */)) {
1070 V_ip6stat.ip6s_odropped++;
1076 * If the interface will not calculate checksums on
1077 * fragmented packets, then do it here.
1078 * XXX-BZ handle the hw offloading case. Need flags.
1080 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1081 in6_delayed_cksum(m, plen, hlen);
1082 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1085 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1086 sctp_delayed_cksum(m, hlen);
1087 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1090 mnext = &m->m_nextpkt;
1093 * Change the next header field of the last header in the
1094 * unfragmentable part.
1096 if (exthdrs.ip6e_rthdr) {
1097 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1098 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1099 } else if (exthdrs.ip6e_dest1) {
1100 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1101 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1102 } else if (exthdrs.ip6e_hbh) {
1103 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1104 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1106 nextproto = ip6->ip6_nxt;
1107 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1111 * Loop through length of segment after first fragment,
1112 * make new header and copy data of each part and link onto
1116 for (off = hlen; off < tlen; off += len) {
1117 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1120 V_ip6stat.ip6s_odropped++;
1123 m->m_pkthdr.rcvif = NULL;
1124 m->m_flags = m0->m_flags & M_COPYFLAGS; /* incl. FIB */
1126 mnext = &m->m_nextpkt;
1127 m->m_data += max_linkhdr;
1128 mhip6 = mtod(m, struct ip6_hdr *);
1130 m->m_len = sizeof(*mhip6);
1131 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1133 V_ip6stat.ip6s_odropped++;
1136 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1137 if (off + len >= tlen)
1140 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1141 mhip6->ip6_plen = htons((u_short)(len + hlen +
1142 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1143 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1145 V_ip6stat.ip6s_odropped++;
1148 m_cat(m, m_frgpart);
1149 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1150 m->m_pkthdr.rcvif = NULL;
1151 ip6f->ip6f_reserved = 0;
1152 ip6f->ip6f_ident = id;
1153 ip6f->ip6f_nxt = nextproto;
1154 V_ip6stat.ip6s_ofragments++;
1155 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1158 in6_ifstat_inc(ifp, ifs6_out_fragok);
1162 * Remove leading garbages.
1168 for (m0 = m; m; m = m0) {
1172 /* Record statistics for this interface address. */
1174 ia->ia_ifa.if_opackets++;
1175 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1177 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1183 V_ip6stat.ip6s_fragmented++;
1186 if (ro == &ip6route && ro->ro_rt && flevalid == 0) {
1187 /* brace necessary for RTFREE */
1189 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt &&
1190 ((flevalid == 0) || (ro_pmtu != ro))) {
1191 RTFREE(ro_pmtu->ro_rt);
1201 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1202 m_freem(exthdrs.ip6e_dest1);
1203 m_freem(exthdrs.ip6e_rthdr);
1204 m_freem(exthdrs.ip6e_dest2);
1213 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1217 if (hlen > MCLBYTES)
1218 return (ENOBUFS); /* XXX */
1220 MGET(m, M_DONTWAIT, MT_DATA);
1225 MCLGET(m, M_DONTWAIT);
1226 if ((m->m_flags & M_EXT) == 0) {
1233 bcopy(hdr, mtod(m, caddr_t), hlen);
1240 * Insert jumbo payload option.
1243 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1249 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1252 * If there is no hop-by-hop options header, allocate new one.
1253 * If there is one but it doesn't have enough space to store the
1254 * jumbo payload option, allocate a cluster to store the whole options.
1255 * Otherwise, use it to store the options.
1257 if (exthdrs->ip6e_hbh == 0) {
1258 MGET(mopt, M_DONTWAIT, MT_DATA);
1261 mopt->m_len = JUMBOOPTLEN;
1262 optbuf = mtod(mopt, u_char *);
1263 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1264 exthdrs->ip6e_hbh = mopt;
1266 struct ip6_hbh *hbh;
1268 mopt = exthdrs->ip6e_hbh;
1269 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1272 * - exthdrs->ip6e_hbh is not referenced from places
1273 * other than exthdrs.
1274 * - exthdrs->ip6e_hbh is not an mbuf chain.
1276 int oldoptlen = mopt->m_len;
1280 * XXX: give up if the whole (new) hbh header does
1281 * not fit even in an mbuf cluster.
1283 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1287 * As a consequence, we must always prepare a cluster
1290 MGET(n, M_DONTWAIT, MT_DATA);
1292 MCLGET(n, M_DONTWAIT);
1293 if ((n->m_flags & M_EXT) == 0) {
1300 n->m_len = oldoptlen + JUMBOOPTLEN;
1301 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1303 optbuf = mtod(n, caddr_t) + oldoptlen;
1305 mopt = exthdrs->ip6e_hbh = n;
1307 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1308 mopt->m_len += JUMBOOPTLEN;
1310 optbuf[0] = IP6OPT_PADN;
1314 * Adjust the header length according to the pad and
1315 * the jumbo payload option.
1317 hbh = mtod(mopt, struct ip6_hbh *);
1318 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1321 /* fill in the option. */
1322 optbuf[2] = IP6OPT_JUMBO;
1324 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1325 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1327 /* finally, adjust the packet header length */
1328 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1335 * Insert fragment header and copy unfragmentable header portions.
1338 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1339 struct ip6_frag **frghdrp)
1341 struct mbuf *n, *mlast;
1343 if (hlen > sizeof(struct ip6_hdr)) {
1344 n = m_copym(m0, sizeof(struct ip6_hdr),
1345 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1352 /* Search for the last mbuf of unfragmentable part. */
1353 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1356 if ((mlast->m_flags & M_EXT) == 0 &&
1357 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1358 /* use the trailing space of the last mbuf for the fragment hdr */
1359 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1361 mlast->m_len += sizeof(struct ip6_frag);
1362 m->m_pkthdr.len += sizeof(struct ip6_frag);
1364 /* allocate a new mbuf for the fragment header */
1367 MGET(mfrg, M_DONTWAIT, MT_DATA);
1370 mfrg->m_len = sizeof(struct ip6_frag);
1371 *frghdrp = mtod(mfrg, struct ip6_frag *);
1372 mlast->m_next = mfrg;
1379 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1380 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1381 int *alwaysfragp, u_int fibnum)
1387 if (ro_pmtu != ro) {
1388 /* The first hop and the final destination may differ. */
1389 struct sockaddr_in6 *sa6_dst =
1390 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1391 if (ro_pmtu->ro_rt &&
1392 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1393 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1394 RTFREE(ro_pmtu->ro_rt);
1395 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1397 if (ro_pmtu->ro_rt == NULL) {
1398 bzero(sa6_dst, sizeof(*sa6_dst));
1399 sa6_dst->sin6_family = AF_INET6;
1400 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1401 sa6_dst->sin6_addr = *dst;
1403 in6_rtalloc(ro_pmtu, fibnum);
1406 if (ro_pmtu->ro_rt) {
1408 struct in_conninfo inc;
1410 bzero(&inc, sizeof(inc));
1411 inc.inc_flags |= INC_ISIPV6;
1412 inc.inc6_faddr = *dst;
1415 ifp = ro_pmtu->ro_rt->rt_ifp;
1416 ifmtu = IN6_LINKMTU(ifp);
1417 mtu = tcp_hc_getmtu(&inc);
1419 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1421 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1424 else if (mtu < IPV6_MMTU) {
1426 * RFC2460 section 5, last paragraph:
1427 * if we record ICMPv6 too big message with
1428 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1429 * or smaller, with framgent header attached.
1430 * (fragment header is needed regardless from the
1431 * packet size, for translators to identify packets)
1435 } else if (mtu > ifmtu) {
1437 * The MTU on the route is larger than the MTU on
1438 * the interface! This shouldn't happen, unless the
1439 * MTU of the interface has been changed after the
1440 * interface was brought up. Change the MTU in the
1441 * route to match the interface MTU (as long as the
1442 * field isn't locked).
1445 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1448 mtu = IN6_LINKMTU(ifp);
1450 error = EHOSTUNREACH; /* XXX */
1454 *alwaysfragp = alwaysfrag;
1459 * IP6 socket option processing.
1462 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1464 int optdatalen, uproto;
1466 struct inpcb *in6p = sotoinpcb(so);
1468 int level, op, optname;
1472 level = sopt->sopt_level;
1473 op = sopt->sopt_dir;
1474 optname = sopt->sopt_name;
1475 optlen = sopt->sopt_valsize;
1479 uproto = (int)so->so_proto->pr_protocol;
1481 if (level != IPPROTO_IPV6) {
1484 if (sopt->sopt_level == SOL_SOCKET &&
1485 sopt->sopt_dir == SOPT_SET) {
1486 switch (sopt->sopt_name) {
1489 if (IN_MULTICAST(ntohl(in6p->inp_laddr.s_addr))) {
1490 if ((so->so_options &
1491 (SO_REUSEADDR | SO_REUSEPORT)) != 0)
1492 in6p->inp_flags2 |= INP_REUSEPORT;
1494 in6p->inp_flags2 &= ~INP_REUSEPORT;
1501 if ((so->so_options & SO_REUSEPORT) != 0)
1502 in6p->inp_flags2 |= INP_REUSEPORT;
1504 in6p->inp_flags2 &= ~INP_REUSEPORT;
1510 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1518 } else { /* level == IPPROTO_IPV6 */
1523 case IPV6_2292PKTOPTIONS:
1524 #ifdef IPV6_PKTOPTIONS
1525 case IPV6_PKTOPTIONS:
1530 error = soopt_getm(sopt, &m); /* XXX */
1533 error = soopt_mcopyin(sopt, m); /* XXX */
1536 error = ip6_pcbopts(&in6p->in6p_outputopts,
1538 m_freem(m); /* XXX */
1543 * Use of some Hop-by-Hop options or some
1544 * Destination options, might require special
1545 * privilege. That is, normal applications
1546 * (without special privilege) might be forbidden
1547 * from setting certain options in outgoing packets,
1548 * and might never see certain options in received
1549 * packets. [RFC 2292 Section 6]
1550 * KAME specific note:
1551 * KAME prevents non-privileged users from sending or
1552 * receiving ANY hbh/dst options in order to avoid
1553 * overhead of parsing options in the kernel.
1555 case IPV6_RECVHOPOPTS:
1556 case IPV6_RECVDSTOPTS:
1557 case IPV6_RECVRTHDRDSTOPTS:
1559 error = priv_check(td,
1560 PRIV_NETINET_SETHDROPTS);
1565 case IPV6_UNICAST_HOPS:
1569 case IPV6_RECVPKTINFO:
1570 case IPV6_RECVHOPLIMIT:
1571 case IPV6_RECVRTHDR:
1572 case IPV6_RECVPATHMTU:
1573 case IPV6_RECVTCLASS:
1575 case IPV6_AUTOFLOWLABEL:
1577 if (optname == IPV6_BINDANY && td != NULL) {
1578 error = priv_check(td,
1579 PRIV_NETINET_BINDANY);
1584 if (optlen != sizeof(int)) {
1588 error = sooptcopyin(sopt, &optval,
1589 sizeof optval, sizeof optval);
1594 case IPV6_UNICAST_HOPS:
1595 if (optval < -1 || optval >= 256)
1598 /* -1 = kernel default */
1599 in6p->in6p_hops = optval;
1600 if ((in6p->inp_vflag &
1602 in6p->inp_ip_ttl = optval;
1605 #define OPTSET(bit) \
1608 in6p->inp_flags |= (bit); \
1610 in6p->inp_flags &= ~(bit); \
1611 } while (/*CONSTCOND*/ 0)
1612 #define OPTSET2292(bit) \
1614 in6p->inp_flags |= IN6P_RFC2292; \
1616 in6p->inp_flags |= (bit); \
1618 in6p->inp_flags &= ~(bit); \
1619 } while (/*CONSTCOND*/ 0)
1620 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1622 case IPV6_RECVPKTINFO:
1623 /* cannot mix with RFC2292 */
1624 if (OPTBIT(IN6P_RFC2292)) {
1628 OPTSET(IN6P_PKTINFO);
1633 struct ip6_pktopts **optp;
1635 /* cannot mix with RFC2292 */
1636 if (OPTBIT(IN6P_RFC2292)) {
1640 optp = &in6p->in6p_outputopts;
1641 error = ip6_pcbopt(IPV6_HOPLIMIT,
1642 (u_char *)&optval, sizeof(optval),
1643 optp, (td != NULL) ? td->td_ucred :
1648 case IPV6_RECVHOPLIMIT:
1649 /* cannot mix with RFC2292 */
1650 if (OPTBIT(IN6P_RFC2292)) {
1654 OPTSET(IN6P_HOPLIMIT);
1657 case IPV6_RECVHOPOPTS:
1658 /* cannot mix with RFC2292 */
1659 if (OPTBIT(IN6P_RFC2292)) {
1663 OPTSET(IN6P_HOPOPTS);
1666 case IPV6_RECVDSTOPTS:
1667 /* cannot mix with RFC2292 */
1668 if (OPTBIT(IN6P_RFC2292)) {
1672 OPTSET(IN6P_DSTOPTS);
1675 case IPV6_RECVRTHDRDSTOPTS:
1676 /* cannot mix with RFC2292 */
1677 if (OPTBIT(IN6P_RFC2292)) {
1681 OPTSET(IN6P_RTHDRDSTOPTS);
1684 case IPV6_RECVRTHDR:
1685 /* cannot mix with RFC2292 */
1686 if (OPTBIT(IN6P_RFC2292)) {
1697 case IPV6_RECVPATHMTU:
1699 * We ignore this option for TCP
1701 * (RFC3542 leaves this case
1704 if (uproto != IPPROTO_TCP)
1710 * make setsockopt(IPV6_V6ONLY)
1711 * available only prior to bind(2).
1712 * see ipng mailing list, Jun 22 2001.
1714 if (in6p->inp_lport ||
1715 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1719 OPTSET(IN6P_IPV6_V6ONLY);
1721 in6p->inp_vflag &= ~INP_IPV4;
1723 in6p->inp_vflag |= INP_IPV4;
1725 case IPV6_RECVTCLASS:
1726 /* cannot mix with RFC2292 XXX */
1727 if (OPTBIT(IN6P_RFC2292)) {
1731 OPTSET(IN6P_TCLASS);
1733 case IPV6_AUTOFLOWLABEL:
1734 OPTSET(IN6P_AUTOFLOWLABEL);
1738 OPTSET(INP_BINDANY);
1745 case IPV6_USE_MIN_MTU:
1746 case IPV6_PREFER_TEMPADDR:
1747 if (optlen != sizeof(optval)) {
1751 error = sooptcopyin(sopt, &optval,
1752 sizeof optval, sizeof optval);
1756 struct ip6_pktopts **optp;
1757 optp = &in6p->in6p_outputopts;
1758 error = ip6_pcbopt(optname,
1759 (u_char *)&optval, sizeof(optval),
1760 optp, (td != NULL) ? td->td_ucred :
1765 case IPV6_2292PKTINFO:
1766 case IPV6_2292HOPLIMIT:
1767 case IPV6_2292HOPOPTS:
1768 case IPV6_2292DSTOPTS:
1769 case IPV6_2292RTHDR:
1771 if (optlen != sizeof(int)) {
1775 error = sooptcopyin(sopt, &optval,
1776 sizeof optval, sizeof optval);
1780 case IPV6_2292PKTINFO:
1781 OPTSET2292(IN6P_PKTINFO);
1783 case IPV6_2292HOPLIMIT:
1784 OPTSET2292(IN6P_HOPLIMIT);
1786 case IPV6_2292HOPOPTS:
1788 * Check super-user privilege.
1789 * See comments for IPV6_RECVHOPOPTS.
1792 error = priv_check(td,
1793 PRIV_NETINET_SETHDROPTS);
1797 OPTSET2292(IN6P_HOPOPTS);
1799 case IPV6_2292DSTOPTS:
1801 error = priv_check(td,
1802 PRIV_NETINET_SETHDROPTS);
1806 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1808 case IPV6_2292RTHDR:
1809 OPTSET2292(IN6P_RTHDR);
1817 case IPV6_RTHDRDSTOPTS:
1820 /* new advanced API (RFC3542) */
1822 u_char optbuf_storage[MCLBYTES];
1824 struct ip6_pktopts **optp;
1826 /* cannot mix with RFC2292 */
1827 if (OPTBIT(IN6P_RFC2292)) {
1833 * We only ensure valsize is not too large
1834 * here. Further validation will be done
1837 error = sooptcopyin(sopt, optbuf_storage,
1838 sizeof(optbuf_storage), 0);
1841 optlen = sopt->sopt_valsize;
1842 optbuf = optbuf_storage;
1843 optp = &in6p->in6p_outputopts;
1844 error = ip6_pcbopt(optname, optbuf, optlen,
1845 optp, (td != NULL) ? td->td_ucred : NULL,
1851 case IPV6_MULTICAST_IF:
1852 case IPV6_MULTICAST_HOPS:
1853 case IPV6_MULTICAST_LOOP:
1854 case IPV6_JOIN_GROUP:
1855 case IPV6_LEAVE_GROUP:
1857 case MCAST_BLOCK_SOURCE:
1858 case MCAST_UNBLOCK_SOURCE:
1859 case MCAST_JOIN_GROUP:
1860 case MCAST_LEAVE_GROUP:
1861 case MCAST_JOIN_SOURCE_GROUP:
1862 case MCAST_LEAVE_SOURCE_GROUP:
1863 error = ip6_setmoptions(in6p, sopt);
1866 case IPV6_PORTRANGE:
1867 error = sooptcopyin(sopt, &optval,
1868 sizeof optval, sizeof optval);
1873 case IPV6_PORTRANGE_DEFAULT:
1874 in6p->inp_flags &= ~(INP_LOWPORT);
1875 in6p->inp_flags &= ~(INP_HIGHPORT);
1878 case IPV6_PORTRANGE_HIGH:
1879 in6p->inp_flags &= ~(INP_LOWPORT);
1880 in6p->inp_flags |= INP_HIGHPORT;
1883 case IPV6_PORTRANGE_LOW:
1884 in6p->inp_flags &= ~(INP_HIGHPORT);
1885 in6p->inp_flags |= INP_LOWPORT;
1895 case IPV6_IPSEC_POLICY:
1900 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1902 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1904 req = mtod(m, caddr_t);
1905 error = ipsec_set_policy(in6p, optname, req,
1906 m->m_len, (sopt->sopt_td != NULL) ?
1907 sopt->sopt_td->td_ucred : NULL);
1914 error = ENOPROTOOPT;
1922 case IPV6_2292PKTOPTIONS:
1923 #ifdef IPV6_PKTOPTIONS
1924 case IPV6_PKTOPTIONS:
1927 * RFC3542 (effectively) deprecated the
1928 * semantics of the 2292-style pktoptions.
1929 * Since it was not reliable in nature (i.e.,
1930 * applications had to expect the lack of some
1931 * information after all), it would make sense
1932 * to simplify this part by always returning
1935 sopt->sopt_valsize = 0;
1938 case IPV6_RECVHOPOPTS:
1939 case IPV6_RECVDSTOPTS:
1940 case IPV6_RECVRTHDRDSTOPTS:
1941 case IPV6_UNICAST_HOPS:
1942 case IPV6_RECVPKTINFO:
1943 case IPV6_RECVHOPLIMIT:
1944 case IPV6_RECVRTHDR:
1945 case IPV6_RECVPATHMTU:
1949 case IPV6_PORTRANGE:
1950 case IPV6_RECVTCLASS:
1951 case IPV6_AUTOFLOWLABEL:
1955 case IPV6_RECVHOPOPTS:
1956 optval = OPTBIT(IN6P_HOPOPTS);
1959 case IPV6_RECVDSTOPTS:
1960 optval = OPTBIT(IN6P_DSTOPTS);
1963 case IPV6_RECVRTHDRDSTOPTS:
1964 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1967 case IPV6_UNICAST_HOPS:
1968 optval = in6p->in6p_hops;
1971 case IPV6_RECVPKTINFO:
1972 optval = OPTBIT(IN6P_PKTINFO);
1975 case IPV6_RECVHOPLIMIT:
1976 optval = OPTBIT(IN6P_HOPLIMIT);
1979 case IPV6_RECVRTHDR:
1980 optval = OPTBIT(IN6P_RTHDR);
1983 case IPV6_RECVPATHMTU:
1984 optval = OPTBIT(IN6P_MTU);
1988 optval = OPTBIT(INP_FAITH);
1992 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1995 case IPV6_PORTRANGE:
1998 flags = in6p->inp_flags;
1999 if (flags & INP_HIGHPORT)
2000 optval = IPV6_PORTRANGE_HIGH;
2001 else if (flags & INP_LOWPORT)
2002 optval = IPV6_PORTRANGE_LOW;
2007 case IPV6_RECVTCLASS:
2008 optval = OPTBIT(IN6P_TCLASS);
2011 case IPV6_AUTOFLOWLABEL:
2012 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2016 optval = OPTBIT(INP_BINDANY);
2021 error = sooptcopyout(sopt, &optval,
2028 struct ip6_mtuinfo mtuinfo;
2029 struct route_in6 sro;
2031 bzero(&sro, sizeof(sro));
2033 if (!(so->so_state & SS_ISCONNECTED))
2036 * XXX: we dot not consider the case of source
2037 * routing, or optional information to specify
2038 * the outgoing interface.
2040 error = ip6_getpmtu(&sro, NULL, NULL,
2041 &in6p->in6p_faddr, &pmtu, NULL,
2047 if (pmtu > IPV6_MAXPACKET)
2048 pmtu = IPV6_MAXPACKET;
2050 bzero(&mtuinfo, sizeof(mtuinfo));
2051 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2052 optdata = (void *)&mtuinfo;
2053 optdatalen = sizeof(mtuinfo);
2054 error = sooptcopyout(sopt, optdata,
2059 case IPV6_2292PKTINFO:
2060 case IPV6_2292HOPLIMIT:
2061 case IPV6_2292HOPOPTS:
2062 case IPV6_2292RTHDR:
2063 case IPV6_2292DSTOPTS:
2065 case IPV6_2292PKTINFO:
2066 optval = OPTBIT(IN6P_PKTINFO);
2068 case IPV6_2292HOPLIMIT:
2069 optval = OPTBIT(IN6P_HOPLIMIT);
2071 case IPV6_2292HOPOPTS:
2072 optval = OPTBIT(IN6P_HOPOPTS);
2074 case IPV6_2292RTHDR:
2075 optval = OPTBIT(IN6P_RTHDR);
2077 case IPV6_2292DSTOPTS:
2078 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2081 error = sooptcopyout(sopt, &optval,
2088 case IPV6_RTHDRDSTOPTS:
2092 case IPV6_USE_MIN_MTU:
2093 case IPV6_PREFER_TEMPADDR:
2094 error = ip6_getpcbopt(in6p->in6p_outputopts,
2098 case IPV6_MULTICAST_IF:
2099 case IPV6_MULTICAST_HOPS:
2100 case IPV6_MULTICAST_LOOP:
2102 error = ip6_getmoptions(in6p, sopt);
2106 case IPV6_IPSEC_POLICY:
2110 struct mbuf *m = NULL;
2111 struct mbuf **mp = &m;
2112 size_t ovalsize = sopt->sopt_valsize;
2113 caddr_t oval = (caddr_t)sopt->sopt_val;
2115 error = soopt_getm(sopt, &m); /* XXX */
2118 error = soopt_mcopyin(sopt, m); /* XXX */
2121 sopt->sopt_valsize = ovalsize;
2122 sopt->sopt_val = oval;
2124 req = mtod(m, caddr_t);
2127 error = ipsec_get_policy(in6p, req, len, mp);
2129 error = soopt_mcopyout(sopt, m); /* XXX */
2130 if (error == 0 && m)
2137 error = ENOPROTOOPT;
2147 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2149 int error = 0, optval, optlen;
2150 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2151 struct inpcb *in6p = sotoinpcb(so);
2152 int level, op, optname;
2154 level = sopt->sopt_level;
2155 op = sopt->sopt_dir;
2156 optname = sopt->sopt_name;
2157 optlen = sopt->sopt_valsize;
2159 if (level != IPPROTO_IPV6) {
2166 * For ICMPv6 sockets, no modification allowed for checksum
2167 * offset, permit "no change" values to help existing apps.
2169 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2170 * for an ICMPv6 socket will fail."
2171 * The current behavior does not meet RFC3542.
2175 if (optlen != sizeof(int)) {
2179 error = sooptcopyin(sopt, &optval, sizeof(optval),
2183 if ((optval % 2) != 0) {
2184 /* the API assumes even offset values */
2186 } else if (so->so_proto->pr_protocol ==
2188 if (optval != icmp6off)
2191 in6p->in6p_cksum = optval;
2195 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2198 optval = in6p->in6p_cksum;
2200 error = sooptcopyout(sopt, &optval, sizeof(optval));
2210 error = ENOPROTOOPT;
2218 * Set up IP6 options in pcb for insertion in output packets or
2219 * specifying behavior of outgoing packets.
2222 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2223 struct socket *so, struct sockopt *sopt)
2225 struct ip6_pktopts *opt = *pktopt;
2227 struct thread *td = sopt->sopt_td;
2229 /* turn off any old options. */
2232 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2233 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2234 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2235 printf("ip6_pcbopts: all specified options are cleared.\n");
2237 ip6_clearpktopts(opt, -1);
2239 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2242 if (!m || m->m_len == 0) {
2244 * Only turning off any previous options, regardless of
2245 * whether the opt is just created or given.
2247 free(opt, M_IP6OPT);
2251 /* set options specified by user. */
2252 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2253 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2254 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2255 free(opt, M_IP6OPT);
2263 * initialize ip6_pktopts. beware that there are non-zero default values in
2267 ip6_initpktopts(struct ip6_pktopts *opt)
2270 bzero(opt, sizeof(*opt));
2271 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2272 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2273 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2274 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2278 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2279 struct ucred *cred, int uproto)
2281 struct ip6_pktopts *opt;
2283 if (*pktopt == NULL) {
2284 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2286 ip6_initpktopts(*pktopt);
2290 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2294 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2296 void *optdata = NULL;
2298 struct ip6_ext *ip6e;
2300 struct in6_pktinfo null_pktinfo;
2301 int deftclass = 0, on;
2302 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2303 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2307 if (pktopt && pktopt->ip6po_pktinfo)
2308 optdata = (void *)pktopt->ip6po_pktinfo;
2310 /* XXX: we don't have to do this every time... */
2311 bzero(&null_pktinfo, sizeof(null_pktinfo));
2312 optdata = (void *)&null_pktinfo;
2314 optdatalen = sizeof(struct in6_pktinfo);
2317 if (pktopt && pktopt->ip6po_tclass >= 0)
2318 optdata = (void *)&pktopt->ip6po_tclass;
2320 optdata = (void *)&deftclass;
2321 optdatalen = sizeof(int);
2324 if (pktopt && pktopt->ip6po_hbh) {
2325 optdata = (void *)pktopt->ip6po_hbh;
2326 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2327 optdatalen = (ip6e->ip6e_len + 1) << 3;
2331 if (pktopt && pktopt->ip6po_rthdr) {
2332 optdata = (void *)pktopt->ip6po_rthdr;
2333 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2334 optdatalen = (ip6e->ip6e_len + 1) << 3;
2337 case IPV6_RTHDRDSTOPTS:
2338 if (pktopt && pktopt->ip6po_dest1) {
2339 optdata = (void *)pktopt->ip6po_dest1;
2340 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2341 optdatalen = (ip6e->ip6e_len + 1) << 3;
2345 if (pktopt && pktopt->ip6po_dest2) {
2346 optdata = (void *)pktopt->ip6po_dest2;
2347 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2348 optdatalen = (ip6e->ip6e_len + 1) << 3;
2352 if (pktopt && pktopt->ip6po_nexthop) {
2353 optdata = (void *)pktopt->ip6po_nexthop;
2354 optdatalen = pktopt->ip6po_nexthop->sa_len;
2357 case IPV6_USE_MIN_MTU:
2359 optdata = (void *)&pktopt->ip6po_minmtu;
2361 optdata = (void *)&defminmtu;
2362 optdatalen = sizeof(int);
2365 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2369 optdata = (void *)&on;
2370 optdatalen = sizeof(on);
2372 case IPV6_PREFER_TEMPADDR:
2374 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2376 optdata = (void *)&defpreftemp;
2377 optdatalen = sizeof(int);
2379 default: /* should not happen */
2381 panic("ip6_getpcbopt: unexpected option\n");
2383 return (ENOPROTOOPT);
2386 error = sooptcopyout(sopt, optdata, optdatalen);
2392 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2397 if (optname == -1 || optname == IPV6_PKTINFO) {
2398 if (pktopt->ip6po_pktinfo)
2399 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2400 pktopt->ip6po_pktinfo = NULL;
2402 if (optname == -1 || optname == IPV6_HOPLIMIT)
2403 pktopt->ip6po_hlim = -1;
2404 if (optname == -1 || optname == IPV6_TCLASS)
2405 pktopt->ip6po_tclass = -1;
2406 if (optname == -1 || optname == IPV6_NEXTHOP) {
2407 if (pktopt->ip6po_nextroute.ro_rt) {
2408 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2409 pktopt->ip6po_nextroute.ro_rt = NULL;
2411 if (pktopt->ip6po_nexthop)
2412 free(pktopt->ip6po_nexthop, M_IP6OPT);
2413 pktopt->ip6po_nexthop = NULL;
2415 if (optname == -1 || optname == IPV6_HOPOPTS) {
2416 if (pktopt->ip6po_hbh)
2417 free(pktopt->ip6po_hbh, M_IP6OPT);
2418 pktopt->ip6po_hbh = NULL;
2420 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2421 if (pktopt->ip6po_dest1)
2422 free(pktopt->ip6po_dest1, M_IP6OPT);
2423 pktopt->ip6po_dest1 = NULL;
2425 if (optname == -1 || optname == IPV6_RTHDR) {
2426 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2427 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2428 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2429 if (pktopt->ip6po_route.ro_rt) {
2430 RTFREE(pktopt->ip6po_route.ro_rt);
2431 pktopt->ip6po_route.ro_rt = NULL;
2434 if (optname == -1 || optname == IPV6_DSTOPTS) {
2435 if (pktopt->ip6po_dest2)
2436 free(pktopt->ip6po_dest2, M_IP6OPT);
2437 pktopt->ip6po_dest2 = NULL;
2441 #define PKTOPT_EXTHDRCPY(type) \
2444 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2445 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2446 if (dst->type == NULL && canwait == M_NOWAIT)\
2448 bcopy(src->type, dst->type, hlen);\
2450 } while (/*CONSTCOND*/ 0)
2453 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2455 if (dst == NULL || src == NULL) {
2456 printf("ip6_clearpktopts: invalid argument\n");
2460 dst->ip6po_hlim = src->ip6po_hlim;
2461 dst->ip6po_tclass = src->ip6po_tclass;
2462 dst->ip6po_flags = src->ip6po_flags;
2463 dst->ip6po_minmtu = src->ip6po_minmtu;
2464 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2465 if (src->ip6po_pktinfo) {
2466 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2468 if (dst->ip6po_pktinfo == NULL)
2470 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2472 if (src->ip6po_nexthop) {
2473 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2475 if (dst->ip6po_nexthop == NULL)
2477 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2478 src->ip6po_nexthop->sa_len);
2480 PKTOPT_EXTHDRCPY(ip6po_hbh);
2481 PKTOPT_EXTHDRCPY(ip6po_dest1);
2482 PKTOPT_EXTHDRCPY(ip6po_dest2);
2483 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2487 ip6_clearpktopts(dst, -1);
2490 #undef PKTOPT_EXTHDRCPY
2492 struct ip6_pktopts *
2493 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2496 struct ip6_pktopts *dst;
2498 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2501 ip6_initpktopts(dst);
2503 if ((error = copypktopts(dst, src, canwait)) != 0) {
2504 free(dst, M_IP6OPT);
2512 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2517 ip6_clearpktopts(pktopt, -1);
2519 free(pktopt, M_IP6OPT);
2523 * Set IPv6 outgoing packet options based on advanced API.
2526 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2527 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2529 struct cmsghdr *cm = 0;
2531 if (control == NULL || opt == NULL)
2534 ip6_initpktopts(opt);
2539 * If stickyopt is provided, make a local copy of the options
2540 * for this particular packet, then override them by ancillary
2542 * XXX: copypktopts() does not copy the cached route to a next
2543 * hop (if any). This is not very good in terms of efficiency,
2544 * but we can allow this since this option should be rarely
2547 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2552 * XXX: Currently, we assume all the optional information is stored
2555 if (control->m_next)
2558 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2559 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2562 if (control->m_len < CMSG_LEN(0))
2565 cm = mtod(control, struct cmsghdr *);
2566 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2568 if (cm->cmsg_level != IPPROTO_IPV6)
2571 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2572 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2581 * Set a particular packet option, as a sticky option or an ancillary data
2582 * item. "len" can be 0 only when it's a sticky option.
2583 * We have 4 cases of combination of "sticky" and "cmsg":
2584 * "sticky=0, cmsg=0": impossible
2585 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2586 * "sticky=1, cmsg=0": RFC3542 socket option
2587 * "sticky=1, cmsg=1": RFC2292 socket option
2590 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2591 struct ucred *cred, int sticky, int cmsg, int uproto)
2593 int minmtupolicy, preftemp;
2596 if (!sticky && !cmsg) {
2598 printf("ip6_setpktopt: impossible case\n");
2604 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2605 * not be specified in the context of RFC3542. Conversely,
2606 * RFC3542 types should not be specified in the context of RFC2292.
2610 case IPV6_2292PKTINFO:
2611 case IPV6_2292HOPLIMIT:
2612 case IPV6_2292NEXTHOP:
2613 case IPV6_2292HOPOPTS:
2614 case IPV6_2292DSTOPTS:
2615 case IPV6_2292RTHDR:
2616 case IPV6_2292PKTOPTIONS:
2617 return (ENOPROTOOPT);
2620 if (sticky && cmsg) {
2627 case IPV6_RTHDRDSTOPTS:
2629 case IPV6_USE_MIN_MTU:
2632 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2633 return (ENOPROTOOPT);
2638 case IPV6_2292PKTINFO:
2641 struct ifnet *ifp = NULL;
2642 struct in6_pktinfo *pktinfo;
2644 if (len != sizeof(struct in6_pktinfo))
2647 pktinfo = (struct in6_pktinfo *)buf;
2650 * An application can clear any sticky IPV6_PKTINFO option by
2651 * doing a "regular" setsockopt with ipi6_addr being
2652 * in6addr_any and ipi6_ifindex being zero.
2653 * [RFC 3542, Section 6]
2655 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2656 pktinfo->ipi6_ifindex == 0 &&
2657 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2658 ip6_clearpktopts(opt, optname);
2662 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2663 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2667 /* validate the interface index if specified. */
2668 if (pktinfo->ipi6_ifindex > V_if_index ||
2669 pktinfo->ipi6_ifindex < 0) {
2672 if (pktinfo->ipi6_ifindex) {
2673 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2679 * We store the address anyway, and let in6_selectsrc()
2680 * validate the specified address. This is because ipi6_addr
2681 * may not have enough information about its scope zone, and
2682 * we may need additional information (such as outgoing
2683 * interface or the scope zone of a destination address) to
2684 * disambiguate the scope.
2685 * XXX: the delay of the validation may confuse the
2686 * application when it is used as a sticky option.
2688 if (opt->ip6po_pktinfo == NULL) {
2689 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2690 M_IP6OPT, M_NOWAIT);
2691 if (opt->ip6po_pktinfo == NULL)
2694 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2698 case IPV6_2292HOPLIMIT:
2704 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2705 * to simplify the ordering among hoplimit options.
2707 if (optname == IPV6_HOPLIMIT && sticky)
2708 return (ENOPROTOOPT);
2710 if (len != sizeof(int))
2713 if (*hlimp < -1 || *hlimp > 255)
2716 opt->ip6po_hlim = *hlimp;
2724 if (len != sizeof(int))
2726 tclass = *(int *)buf;
2727 if (tclass < -1 || tclass > 255)
2730 opt->ip6po_tclass = tclass;
2734 case IPV6_2292NEXTHOP:
2737 error = priv_check_cred(cred,
2738 PRIV_NETINET_SETHDROPTS, 0);
2743 if (len == 0) { /* just remove the option */
2744 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2748 /* check if cmsg_len is large enough for sa_len */
2749 if (len < sizeof(struct sockaddr) || len < *buf)
2752 switch (((struct sockaddr *)buf)->sa_family) {
2755 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2758 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2761 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2762 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2765 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2771 case AF_LINK: /* should eventually be supported */
2773 return (EAFNOSUPPORT);
2776 /* turn off the previous option, then set the new option. */
2777 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2778 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2779 if (opt->ip6po_nexthop == NULL)
2781 bcopy(buf, opt->ip6po_nexthop, *buf);
2784 case IPV6_2292HOPOPTS:
2787 struct ip6_hbh *hbh;
2791 * XXX: We don't allow a non-privileged user to set ANY HbH
2792 * options, since per-option restriction has too much
2796 error = priv_check_cred(cred,
2797 PRIV_NETINET_SETHDROPTS, 0);
2803 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2804 break; /* just remove the option */
2807 /* message length validation */
2808 if (len < sizeof(struct ip6_hbh))
2810 hbh = (struct ip6_hbh *)buf;
2811 hbhlen = (hbh->ip6h_len + 1) << 3;
2815 /* turn off the previous option, then set the new option. */
2816 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2817 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2818 if (opt->ip6po_hbh == NULL)
2820 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2825 case IPV6_2292DSTOPTS:
2827 case IPV6_RTHDRDSTOPTS:
2829 struct ip6_dest *dest, **newdest = NULL;
2832 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2833 error = priv_check_cred(cred,
2834 PRIV_NETINET_SETHDROPTS, 0);
2840 ip6_clearpktopts(opt, optname);
2841 break; /* just remove the option */
2844 /* message length validation */
2845 if (len < sizeof(struct ip6_dest))
2847 dest = (struct ip6_dest *)buf;
2848 destlen = (dest->ip6d_len + 1) << 3;
2853 * Determine the position that the destination options header
2854 * should be inserted; before or after the routing header.
2857 case IPV6_2292DSTOPTS:
2859 * The old advacned API is ambiguous on this point.
2860 * Our approach is to determine the position based
2861 * according to the existence of a routing header.
2862 * Note, however, that this depends on the order of the
2863 * extension headers in the ancillary data; the 1st
2864 * part of the destination options header must appear
2865 * before the routing header in the ancillary data,
2867 * RFC3542 solved the ambiguity by introducing
2868 * separate ancillary data or option types.
2870 if (opt->ip6po_rthdr == NULL)
2871 newdest = &opt->ip6po_dest1;
2873 newdest = &opt->ip6po_dest2;
2875 case IPV6_RTHDRDSTOPTS:
2876 newdest = &opt->ip6po_dest1;
2879 newdest = &opt->ip6po_dest2;
2883 /* turn off the previous option, then set the new option. */
2884 ip6_clearpktopts(opt, optname);
2885 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2886 if (*newdest == NULL)
2888 bcopy(dest, *newdest, destlen);
2893 case IPV6_2292RTHDR:
2896 struct ip6_rthdr *rth;
2900 ip6_clearpktopts(opt, IPV6_RTHDR);
2901 break; /* just remove the option */
2904 /* message length validation */
2905 if (len < sizeof(struct ip6_rthdr))
2907 rth = (struct ip6_rthdr *)buf;
2908 rthlen = (rth->ip6r_len + 1) << 3;
2912 switch (rth->ip6r_type) {
2913 case IPV6_RTHDR_TYPE_0:
2914 if (rth->ip6r_len == 0) /* must contain one addr */
2916 if (rth->ip6r_len % 2) /* length must be even */
2918 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2922 return (EINVAL); /* not supported */
2925 /* turn off the previous option */
2926 ip6_clearpktopts(opt, IPV6_RTHDR);
2927 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2928 if (opt->ip6po_rthdr == NULL)
2930 bcopy(rth, opt->ip6po_rthdr, rthlen);
2935 case IPV6_USE_MIN_MTU:
2936 if (len != sizeof(int))
2938 minmtupolicy = *(int *)buf;
2939 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2940 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2941 minmtupolicy != IP6PO_MINMTU_ALL) {
2944 opt->ip6po_minmtu = minmtupolicy;
2948 if (len != sizeof(int))
2951 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2953 * we ignore this option for TCP sockets.
2954 * (RFC3542 leaves this case unspecified.)
2956 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2958 opt->ip6po_flags |= IP6PO_DONTFRAG;
2961 case IPV6_PREFER_TEMPADDR:
2962 if (len != sizeof(int))
2964 preftemp = *(int *)buf;
2965 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2966 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2967 preftemp != IP6PO_TEMPADDR_PREFER) {
2970 opt->ip6po_prefer_tempaddr = preftemp;
2974 return (ENOPROTOOPT);
2975 } /* end of switch */
2981 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2982 * packet to the input queue of a specified interface. Note that this
2983 * calls the output routine of the loopback "driver", but with an interface
2984 * pointer that might NOT be &loif -- easier than replicating that code here.
2987 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2990 struct ip6_hdr *ip6;
2992 copym = m_copy(m, 0, M_COPYALL);
2997 * Make sure to deep-copy IPv6 header portion in case the data
2998 * is in an mbuf cluster, so that we can safely override the IPv6
2999 * header portion later.
3001 if ((copym->m_flags & M_EXT) != 0 ||
3002 copym->m_len < sizeof(struct ip6_hdr)) {
3003 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3009 if (copym->m_len < sizeof(*ip6)) {
3015 ip6 = mtod(copym, struct ip6_hdr *);
3017 * clear embedded scope identifiers if necessary.
3018 * in6_clearscope will touch the addresses only when necessary.
3020 in6_clearscope(&ip6->ip6_src);
3021 in6_clearscope(&ip6->ip6_dst);
3023 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3027 * Chop IPv6 header off from the payload.
3030 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3033 struct ip6_hdr *ip6;
3035 ip6 = mtod(m, struct ip6_hdr *);
3036 if (m->m_len > sizeof(*ip6)) {
3037 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3042 M_MOVE_PKTHDR(mh, m);
3043 MH_ALIGN(mh, sizeof(*ip6));
3044 m->m_len -= sizeof(*ip6);
3045 m->m_data += sizeof(*ip6);
3048 m->m_len = sizeof(*ip6);
3049 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3051 exthdrs->ip6e_ip6 = m;
3056 * Compute IPv6 extension header length.
3059 ip6_optlen(struct inpcb *in6p)
3063 if (!in6p->in6p_outputopts)
3068 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3070 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3071 if (in6p->in6p_outputopts->ip6po_rthdr)
3072 /* dest1 is valid with rthdr only */
3073 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3074 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3075 len += elen(in6p->in6p_outputopts->ip6po_dest2);