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/if_var.h>
90 #include <net/netisr.h>
91 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip_var.h>
98 #include <netinet6/in6_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/tcp_var.h>
104 #include <netinet6/nd6.h>
107 #include <netipsec/ipsec.h>
108 #include <netipsec/ipsec6.h>
109 #include <netipsec/key.h>
110 #include <netinet6/ip6_ipsec.h>
113 #include <netinet/sctp.h>
114 #include <netinet/sctp_crc32.h>
117 #include <netinet6/ip6protosw.h>
118 #include <netinet6/scope6_var.h>
121 #include <net/flowtable.h>
124 extern int in6_mcast_loop;
127 struct mbuf *ip6e_ip6;
128 struct mbuf *ip6e_hbh;
129 struct mbuf *ip6e_dest1;
130 struct mbuf *ip6e_rthdr;
131 struct mbuf *ip6e_dest2;
134 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
135 struct ucred *, int);
136 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
137 struct socket *, struct sockopt *);
138 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
139 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
140 struct ucred *, int, int, int);
142 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
143 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
145 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
146 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
147 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
148 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
149 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
153 * Make an extension header from option data. hp is the source, and
154 * mp is the destination.
156 #define MAKE_EXTHDR(hp, mp) \
159 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
160 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
161 ((eh)->ip6e_len + 1) << 3); \
165 } while (/*CONSTCOND*/ 0)
168 * Form a chain of extension headers.
169 * m is the extension header mbuf
170 * mp is the previous mbuf in the chain
171 * p is the next header
172 * i is the type of option.
174 #define MAKE_CHAIN(m, mp, p, i)\
178 panic("assumption failed: hdr not split"); \
179 *mtod((m), u_char *) = *(p);\
181 p = mtod((m), u_char *);\
182 (m)->m_next = (mp)->m_next;\
186 } while (/*CONSTCOND*/ 0)
189 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
193 csum = in_cksum_skip(m, offset + plen, offset);
194 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
196 offset += m->m_pkthdr.csum_data; /* checksum offset */
198 if (offset + sizeof(u_short) > m->m_len) {
199 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
200 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
201 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
203 * XXX this should not happen, but if it does, the correct
204 * behavior may be to insert the checksum in the appropriate
205 * next mbuf in the chain.
209 *(u_short *)(m->m_data + offset) = csum;
213 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
214 * header (with pri, len, nxt, hlim, src, dst).
215 * This function may modify ver and hlim only.
216 * The mbuf chain containing the packet will be freed.
217 * The mbuf opt, if present, will not be freed.
218 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
219 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
220 * then result of route lookup is stored in ro->ro_rt.
222 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
223 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
224 * which is rt_rmx.rmx_mtu.
226 * ifpp - XXX: just for statistics
229 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
230 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
231 struct ifnet **ifpp, struct inpcb *inp)
233 struct ip6_hdr *ip6, *mhip6;
234 struct ifnet *ifp, *origifp;
236 struct mbuf *mprev = NULL;
237 int hlen, tlen, len, off;
238 struct route_in6 ip6route;
239 struct rtentry *rt = NULL;
240 struct sockaddr_in6 *dst, src_sa, dst_sa;
241 struct in6_addr odst;
243 struct in6_ifaddr *ia = NULL;
245 int alwaysfrag, dontfrag;
246 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
247 struct ip6_exthdrs exthdrs;
248 struct in6_addr finaldst, src0, dst0;
250 struct route_in6 *ro_pmtu = NULL;
255 struct ipsec_output_state state;
256 struct ip6_rthdr *rh = NULL;
257 int needipsectun = 0;
259 struct secpolicy *sp = NULL;
261 struct m_tag *fwd_tag = NULL;
263 ip6 = mtod(m, struct ip6_hdr *);
265 printf ("ip6 is NULL");
270 M_SETFIB(m, inp->inp_inc.inc_fibnum);
272 finaldst = ip6->ip6_dst;
273 bzero(&exthdrs, sizeof(exthdrs));
275 /* Hop-by-Hop options header */
276 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
277 /* Destination options header(1st part) */
278 if (opt->ip6po_rthdr) {
280 * Destination options header(1st part)
281 * This only makes sense with a routing header.
282 * See Section 9.2 of RFC 3542.
283 * Disabling this part just for MIP6 convenience is
284 * a bad idea. We need to think carefully about a
285 * way to make the advanced API coexist with MIP6
286 * options, which might automatically be inserted in
289 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
292 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
293 /* Destination options header(2nd part) */
294 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
299 * IPSec checking which handles several cases.
300 * FAST IPSEC: We re-injected the packet.
302 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
304 case 1: /* Bad packet */
306 case -1: /* Do IPSec */
309 * Do delayed checksums now, as we may send before returning.
311 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
312 plen = m->m_pkthdr.len - sizeof(*ip6);
313 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
314 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
317 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
318 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
319 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
322 case 0: /* No IPSec */
329 * Calculate the total length of the extension header chain.
330 * Keep the length of the unfragmentable part for fragmentation.
333 if (exthdrs.ip6e_hbh)
334 optlen += exthdrs.ip6e_hbh->m_len;
335 if (exthdrs.ip6e_dest1)
336 optlen += exthdrs.ip6e_dest1->m_len;
337 if (exthdrs.ip6e_rthdr)
338 optlen += exthdrs.ip6e_rthdr->m_len;
339 unfragpartlen = optlen + sizeof(struct ip6_hdr);
341 /* NOTE: we don't add AH/ESP length here. do that later. */
342 if (exthdrs.ip6e_dest2)
343 optlen += exthdrs.ip6e_dest2->m_len;
346 * If we need IPsec, or there is at least one extension header,
347 * separate IP6 header from the payload.
349 if ((needipsec || optlen) && !hdrsplit) {
350 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
354 m = exthdrs.ip6e_ip6;
359 ip6 = mtod(m, struct ip6_hdr *);
361 /* adjust mbuf packet header length */
362 m->m_pkthdr.len += optlen;
363 plen = m->m_pkthdr.len - sizeof(*ip6);
365 /* If this is a jumbo payload, insert a jumbo payload option. */
366 if (plen > IPV6_MAXPACKET) {
368 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
372 m = exthdrs.ip6e_ip6;
376 ip6 = mtod(m, struct ip6_hdr *);
377 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
381 ip6->ip6_plen = htons(plen);
384 * Concatenate headers and fill in next header fields.
385 * Here we have, on "m"
387 * and we insert headers accordingly. Finally, we should be getting:
388 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
390 * during the header composing process, "m" points to IPv6 header.
391 * "mprev" points to an extension header prior to esp.
393 u_char *nexthdrp = &ip6->ip6_nxt;
397 * we treat dest2 specially. this makes IPsec processing
398 * much easier. the goal here is to make mprev point the
399 * mbuf prior to dest2.
401 * result: IPv6 dest2 payload
402 * m and mprev will point to IPv6 header.
404 if (exthdrs.ip6e_dest2) {
406 panic("assumption failed: hdr not split");
407 exthdrs.ip6e_dest2->m_next = m->m_next;
408 m->m_next = exthdrs.ip6e_dest2;
409 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
410 ip6->ip6_nxt = IPPROTO_DSTOPTS;
414 * result: IPv6 hbh dest1 rthdr dest2 payload
415 * m will point to IPv6 header. mprev will point to the
416 * extension header prior to dest2 (rthdr in the above case).
418 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
419 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
421 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
429 * pointers after IPsec headers are not valid any more.
430 * other pointers need a great care too.
431 * (IPsec routines should not mangle mbufs prior to AH/ESP)
433 exthdrs.ip6e_dest2 = NULL;
435 if (exthdrs.ip6e_rthdr) {
436 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
437 segleft_org = rh->ip6r_segleft;
438 rh->ip6r_segleft = 0;
441 bzero(&state, sizeof(state));
443 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
446 if (error == EJUSTRETURN) {
448 * We had a SP with a level of 'use' and no SA. We
449 * will just continue to process the packet without
454 /* mbuf is already reclaimed in ipsec6_output_trans. */
464 printf("[%s:%d] (ipsec): error code %d\n",
465 __func__, __LINE__, error);
468 /* don't show these error codes to the user */
473 } else if (!needipsectun) {
475 * In the FAST IPSec case we have already
476 * re-injected the packet and it has been freed
477 * by the ipsec_done() function. So, just clean
478 * up after ourselves.
483 if (exthdrs.ip6e_rthdr) {
484 /* ah6_output doesn't modify mbuf chain */
485 rh->ip6r_segleft = segleft_org;
491 * If there is a routing header, discard the packet.
493 if (exthdrs.ip6e_rthdr) {
498 /* Source address validation */
499 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
500 (flags & IPV6_UNSPECSRC) == 0) {
502 IP6STAT_INC(ip6s_badscope);
505 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
507 IP6STAT_INC(ip6s_badscope);
511 IP6STAT_INC(ip6s_localout);
518 bzero((caddr_t)ro, sizeof(*ro));
521 if (opt && opt->ip6po_rthdr)
522 ro = &opt->ip6po_route;
523 dst = (struct sockaddr_in6 *)&ro->ro_dst;
525 if (ro->ro_rt == NULL) {
529 * The flow table returns route entries valid for up to 30
530 * seconds; we rely on the remainder of ip_output() taking no
531 * longer than that long for the stability of ro_rt. The
532 * flow ID assignment must have happened before this point.
534 fle = flowtable_lookup(AF_INET6, m);
536 flow_to_route_in6(fle, ro);
541 * if specified, try to fill in the traffic class field.
542 * do not override if a non-zero value is already set.
543 * we check the diffserv field and the ecn field separately.
545 if (opt && opt->ip6po_tclass >= 0) {
548 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
550 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
553 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
556 /* fill in or override the hop limit field, if necessary. */
557 if (opt && opt->ip6po_hlim != -1)
558 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
559 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
561 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
563 ip6->ip6_hlim = V_ip6_defmcasthlim;
568 * We may re-inject packets into the stack here.
570 if (needipsec && needipsectun) {
571 struct ipsec_output_state state;
574 * All the extension headers will become inaccessible
575 * (since they can be encrypted).
576 * Don't panic, we need no more updates to extension headers
577 * on inner IPv6 packet (since they are now encapsulated).
579 * IPv6 [ESP|AH] IPv6 [extension headers] payload
581 bzero(&exthdrs, sizeof(exthdrs));
582 exthdrs.ip6e_ip6 = m;
584 bzero(&state, sizeof(state));
586 state.ro = (struct route *)ro;
587 state.dst = (struct sockaddr *)dst;
589 error = ipsec6_output_tunnel(&state, sp, flags);
592 ro = (struct route_in6 *)state.ro;
593 dst = (struct sockaddr_in6 *)state.dst;
594 if (error == EJUSTRETURN) {
596 * We had a SP with a level of 'use' and no SA. We
597 * will just continue to process the packet without
602 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
613 printf("[%s:%d] (ipsec): error code %d\n",
614 __func__, __LINE__, error);
617 /* don't show these error codes to the user */
624 * In the FAST IPSec case we have already
625 * re-injected the packet and it has been freed
626 * by the ipsec_done() function. So, just clean
627 * up after ourselves.
633 exthdrs.ip6e_ip6 = m;
638 ip6 = mtod(m, struct ip6_hdr *);
640 if (ro->ro_rt && fwd_tag == NULL) {
642 ifp = ro->ro_rt->rt_ifp;
644 if (fwd_tag == NULL) {
645 bzero(&dst_sa, sizeof(dst_sa));
646 dst_sa.sin6_family = AF_INET6;
647 dst_sa.sin6_len = sizeof(dst_sa);
648 dst_sa.sin6_addr = ip6->ip6_dst;
650 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
651 &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
654 in6_ifstat_inc(ifp, ifs6_out_discard);
660 * If in6_selectroute() does not return a route entry,
661 * dst may not have been updated.
663 *dst = dst_sa; /* XXX */
667 * then rt (for unicast) and ifp must be non-NULL valid values.
669 if ((flags & IPV6_FORWARDING) == 0) {
670 /* XXX: the FORWARDING flag can be set for mrouting. */
671 in6_ifstat_inc(ifp, ifs6_out_request);
674 ia = (struct in6_ifaddr *)(rt->rt_ifa);
680 * The outgoing interface must be in the zone of source and
681 * destination addresses.
686 if (in6_setscope(&src0, origifp, &zone))
688 bzero(&src_sa, sizeof(src_sa));
689 src_sa.sin6_family = AF_INET6;
690 src_sa.sin6_len = sizeof(src_sa);
691 src_sa.sin6_addr = ip6->ip6_src;
692 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
696 if (in6_setscope(&dst0, origifp, &zone))
698 /* re-initialize to be sure */
699 bzero(&dst_sa, sizeof(dst_sa));
700 dst_sa.sin6_family = AF_INET6;
701 dst_sa.sin6_len = sizeof(dst_sa);
702 dst_sa.sin6_addr = ip6->ip6_dst;
703 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
707 /* We should use ia_ifp to support the case of
708 * sending packets to an address of our own.
710 if (ia != NULL && ia->ia_ifp)
713 /* scope check is done. */
717 IP6STAT_INC(ip6s_badscope);
718 in6_ifstat_inc(origifp, ifs6_out_discard);
720 error = EHOSTUNREACH; /* XXX */
724 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
725 if (opt && opt->ip6po_nextroute.ro_rt) {
727 * The nexthop is explicitly specified by the
728 * application. We assume the next hop is an IPv6
731 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
733 else if ((rt->rt_flags & RTF_GATEWAY))
734 dst = (struct sockaddr_in6 *)rt->rt_gateway;
737 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
738 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
740 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
741 in6_ifstat_inc(ifp, ifs6_out_mcast);
743 * Confirm that the outgoing interface supports multicast.
745 if (!(ifp->if_flags & IFF_MULTICAST)) {
746 IP6STAT_INC(ip6s_noroute);
747 in6_ifstat_inc(ifp, ifs6_out_discard);
751 if ((im6o == NULL && in6_mcast_loop) ||
752 (im6o && im6o->im6o_multicast_loop)) {
754 * Loop back multicast datagram if not expressly
755 * forbidden to do so, even if we have not joined
756 * the address; protocols will filter it later,
757 * thus deferring a hash lookup and lock acquisition
758 * at the expense of an m_copym().
760 ip6_mloopback(ifp, m, dst);
763 * If we are acting as a multicast router, perform
764 * multicast forwarding as if the packet had just
765 * arrived on the interface to which we are about
766 * to send. The multicast forwarding function
767 * recursively calls this function, using the
768 * IPV6_FORWARDING flag to prevent infinite recursion.
770 * Multicasts that are looped back by ip6_mloopback(),
771 * above, will be forwarded by the ip6_input() routine,
774 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
776 * XXX: ip6_mforward expects that rcvif is NULL
777 * when it is called from the originating path.
778 * However, it may not always be the case.
780 m->m_pkthdr.rcvif = NULL;
781 if (ip6_mforward(ip6, ifp, m) != 0) {
788 * Multicasts with a hoplimit of zero may be looped back,
789 * above, but must not be transmitted on a network.
790 * Also, multicasts addressed to the loopback interface
791 * are not sent -- the above call to ip6_mloopback() will
792 * loop back a copy if this host actually belongs to the
793 * destination group on the loopback interface.
795 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
796 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
803 * Fill the outgoing inteface to tell the upper layer
804 * to increment per-interface statistics.
809 /* Determine path MTU. */
810 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
811 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
815 * The caller of this function may specify to use the minimum MTU
817 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
818 * setting. The logic is a bit complicated; by default, unicast
819 * packets will follow path MTU while multicast packets will be sent at
820 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
821 * including unicast ones will be sent at the minimum MTU. Multicast
822 * packets will always be sent at the minimum MTU unless
823 * IP6PO_MINMTU_DISABLE is explicitly specified.
824 * See RFC 3542 for more details.
826 if (mtu > IPV6_MMTU) {
827 if ((flags & IPV6_MINMTU))
829 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
831 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
833 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
839 * clear embedded scope identifiers if necessary.
840 * in6_clearscope will touch the addresses only when necessary.
842 in6_clearscope(&ip6->ip6_src);
843 in6_clearscope(&ip6->ip6_dst);
846 * If the outgoing packet contains a hop-by-hop options header,
847 * it must be examined and processed even by the source node.
848 * (RFC 2460, section 4.)
850 if (exthdrs.ip6e_hbh) {
851 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
852 u_int32_t dummy; /* XXX unused */
853 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
856 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
857 panic("ip6e_hbh is not contiguous");
860 * XXX: if we have to send an ICMPv6 error to the sender,
861 * we need the M_LOOP flag since icmp6_error() expects
862 * the IPv6 and the hop-by-hop options header are
863 * contiguous unless the flag is set.
865 m->m_flags |= M_LOOP;
866 m->m_pkthdr.rcvif = ifp;
867 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
868 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
869 &dummy, &plen) < 0) {
870 /* m was already freed at this point */
871 error = EINVAL;/* better error? */
874 m->m_flags &= ~M_LOOP; /* XXX */
875 m->m_pkthdr.rcvif = NULL;
878 /* Jump over all PFIL processing if hooks are not active. */
879 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
883 /* Run through list of hooks for output packets. */
884 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
885 if (error != 0 || m == NULL)
887 ip6 = mtod(m, struct ip6_hdr *);
889 /* See if destination IP address was changed by packet filter. */
890 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
891 m->m_flags |= M_SKIP_FIREWALL;
892 /* If destination is now ourself drop to ip6_input(). */
893 if (in6_localip(&ip6->ip6_dst)) {
894 m->m_flags |= M_FASTFWD_OURS;
895 if (m->m_pkthdr.rcvif == NULL)
896 m->m_pkthdr.rcvif = V_loif;
897 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
898 m->m_pkthdr.csum_flags |=
899 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
900 m->m_pkthdr.csum_data = 0xffff;
903 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
904 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
906 error = netisr_queue(NETISR_IPV6, m);
909 goto again; /* Redo the routing table lookup. */
912 /* See if local, if yes, send it to netisr. */
913 if (m->m_flags & M_FASTFWD_OURS) {
914 if (m->m_pkthdr.rcvif == NULL)
915 m->m_pkthdr.rcvif = V_loif;
916 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
917 m->m_pkthdr.csum_flags |=
918 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
919 m->m_pkthdr.csum_data = 0xffff;
922 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
923 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
925 error = netisr_queue(NETISR_IPV6, m);
928 /* Or forward to some other address? */
929 if ((m->m_flags & M_IP6_NEXTHOP) &&
930 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
931 dst = (struct sockaddr_in6 *)&ro->ro_dst;
932 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
933 m->m_flags |= M_SKIP_FIREWALL;
934 m->m_flags &= ~M_IP6_NEXTHOP;
935 m_tag_delete(m, fwd_tag);
941 * Send the packet to the outgoing interface.
942 * If necessary, do IPv6 fragmentation before sending.
944 * the logic here is rather complex:
945 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
946 * 1-a: send as is if tlen <= path mtu
947 * 1-b: fragment if tlen > path mtu
949 * 2: if user asks us not to fragment (dontfrag == 1)
950 * 2-a: send as is if tlen <= interface mtu
951 * 2-b: error if tlen > interface mtu
953 * 3: if we always need to attach fragment header (alwaysfrag == 1)
956 * 4: if dontfrag == 1 && alwaysfrag == 1
957 * error, as we cannot handle this conflicting request
959 sw_csum = m->m_pkthdr.csum_flags;
961 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
962 sw_csum &= ~ifp->if_hwassist;
966 * If we added extension headers, we will not do TSO and calculate the
967 * checksums ourselves for now.
968 * XXX-BZ Need a framework to know when the NIC can handle it, even
971 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
972 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
973 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
976 if (sw_csum & CSUM_SCTP_IPV6) {
977 sw_csum &= ~CSUM_SCTP_IPV6;
978 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
981 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
982 tlen = m->m_pkthdr.len;
984 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
988 if (dontfrag && alwaysfrag) { /* case 4 */
989 /* conflicting request - can't transmit */
993 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
995 * Even if the DONTFRAG option is specified, we cannot send the
996 * packet when the data length is larger than the MTU of the
997 * outgoing interface.
998 * Notify the error by sending IPV6_PATHMTU ancillary data as
999 * well as returning an error code (the latter is not described
1003 struct ip6ctlparam ip6cp;
1005 mtu32 = (u_int32_t)mtu;
1006 bzero(&ip6cp, sizeof(ip6cp));
1007 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1008 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1016 * transmit packet without fragmentation
1018 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
1019 struct in6_ifaddr *ia6;
1021 ip6 = mtod(m, struct ip6_hdr *);
1022 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1024 /* Record statistics for this interface address. */
1025 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1026 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1028 ifa_free(&ia6->ia_ifa);
1030 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1035 * try to fragment the packet. case 1-b and 3
1037 if (mtu < IPV6_MMTU) {
1038 /* path MTU cannot be less than IPV6_MMTU */
1040 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1042 } else if (ip6->ip6_plen == 0) {
1043 /* jumbo payload cannot be fragmented */
1045 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1048 struct mbuf **mnext, *m_frgpart;
1049 struct ip6_frag *ip6f;
1050 u_int32_t id = htonl(ip6_randomid());
1053 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1056 * Too large for the destination or interface;
1057 * fragment if possible.
1058 * Must be able to put at least 8 bytes per fragment.
1060 hlen = unfragpartlen;
1061 if (mtu > IPV6_MAXPACKET)
1062 mtu = IPV6_MAXPACKET;
1064 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1067 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1072 * Verify that we have any chance at all of being able to queue
1073 * the packet or packet fragments
1075 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1076 < tlen /* - hlen */)) {
1078 IP6STAT_INC(ip6s_odropped);
1084 * If the interface will not calculate checksums on
1085 * fragmented packets, then do it here.
1086 * XXX-BZ handle the hw offloading case. Need flags.
1088 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1089 in6_delayed_cksum(m, plen, hlen);
1090 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1093 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1094 sctp_delayed_cksum(m, hlen);
1095 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1098 mnext = &m->m_nextpkt;
1101 * Change the next header field of the last header in the
1102 * unfragmentable part.
1104 if (exthdrs.ip6e_rthdr) {
1105 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1106 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1107 } else if (exthdrs.ip6e_dest1) {
1108 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1109 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1110 } else if (exthdrs.ip6e_hbh) {
1111 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1112 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1114 nextproto = ip6->ip6_nxt;
1115 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1119 * Loop through length of segment after first fragment,
1120 * make new header and copy data of each part and link onto
1124 for (off = hlen; off < tlen; off += len) {
1125 m = m_gethdr(M_NOWAIT, MT_DATA);
1128 IP6STAT_INC(ip6s_odropped);
1131 m->m_flags = m0->m_flags & M_COPYFLAGS;
1133 mnext = &m->m_nextpkt;
1134 m->m_data += max_linkhdr;
1135 mhip6 = mtod(m, struct ip6_hdr *);
1137 m->m_len = sizeof(*mhip6);
1138 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1140 IP6STAT_INC(ip6s_odropped);
1143 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1144 if (off + len >= tlen)
1147 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1148 mhip6->ip6_plen = htons((u_short)(len + hlen +
1149 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1150 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1152 IP6STAT_INC(ip6s_odropped);
1155 m_cat(m, m_frgpart);
1156 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1157 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
1158 m->m_pkthdr.rcvif = NULL;
1159 ip6f->ip6f_reserved = 0;
1160 ip6f->ip6f_ident = id;
1161 ip6f->ip6f_nxt = nextproto;
1162 IP6STAT_INC(ip6s_ofragments);
1163 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1166 in6_ifstat_inc(ifp, ifs6_out_fragok);
1170 * Remove leading garbages.
1176 for (m0 = m; m; m = m0) {
1180 /* Record statistics for this interface address. */
1182 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1183 counter_u64_add(ia->ia_ifa.ifa_obytes,
1186 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1192 IP6STAT_INC(ip6s_fragmented);
1195 if (ro == &ip6route)
1197 if (ro_pmtu == &ip6route)
1207 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1208 m_freem(exthdrs.ip6e_dest1);
1209 m_freem(exthdrs.ip6e_rthdr);
1210 m_freem(exthdrs.ip6e_dest2);
1219 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1223 if (hlen > MCLBYTES)
1224 return (ENOBUFS); /* XXX */
1227 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1229 m = m_get(M_NOWAIT, MT_DATA);
1234 bcopy(hdr, mtod(m, caddr_t), hlen);
1241 * Insert jumbo payload option.
1244 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1250 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1253 * If there is no hop-by-hop options header, allocate new one.
1254 * If there is one but it doesn't have enough space to store the
1255 * jumbo payload option, allocate a cluster to store the whole options.
1256 * Otherwise, use it to store the options.
1258 if (exthdrs->ip6e_hbh == 0) {
1259 mopt = m_get(M_NOWAIT, MT_DATA);
1262 mopt->m_len = JUMBOOPTLEN;
1263 optbuf = mtod(mopt, u_char *);
1264 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1265 exthdrs->ip6e_hbh = mopt;
1267 struct ip6_hbh *hbh;
1269 mopt = exthdrs->ip6e_hbh;
1270 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1273 * - exthdrs->ip6e_hbh is not referenced from places
1274 * other than exthdrs.
1275 * - exthdrs->ip6e_hbh is not an mbuf chain.
1277 int oldoptlen = mopt->m_len;
1281 * XXX: give up if the whole (new) hbh header does
1282 * not fit even in an mbuf cluster.
1284 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1288 * As a consequence, we must always prepare a cluster
1291 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1294 n->m_len = oldoptlen + JUMBOOPTLEN;
1295 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1297 optbuf = mtod(n, caddr_t) + oldoptlen;
1299 mopt = exthdrs->ip6e_hbh = n;
1301 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1302 mopt->m_len += JUMBOOPTLEN;
1304 optbuf[0] = IP6OPT_PADN;
1308 * Adjust the header length according to the pad and
1309 * the jumbo payload option.
1311 hbh = mtod(mopt, struct ip6_hbh *);
1312 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1315 /* fill in the option. */
1316 optbuf[2] = IP6OPT_JUMBO;
1318 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1319 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1321 /* finally, adjust the packet header length */
1322 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1329 * Insert fragment header and copy unfragmentable header portions.
1332 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1333 struct ip6_frag **frghdrp)
1335 struct mbuf *n, *mlast;
1337 if (hlen > sizeof(struct ip6_hdr)) {
1338 n = m_copym(m0, sizeof(struct ip6_hdr),
1339 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1346 /* Search for the last mbuf of unfragmentable part. */
1347 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1350 if ((mlast->m_flags & M_EXT) == 0 &&
1351 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1352 /* use the trailing space of the last mbuf for the fragment hdr */
1353 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1355 mlast->m_len += sizeof(struct ip6_frag);
1356 m->m_pkthdr.len += sizeof(struct ip6_frag);
1358 /* allocate a new mbuf for the fragment header */
1361 mfrg = m_get(M_NOWAIT, MT_DATA);
1364 mfrg->m_len = sizeof(struct ip6_frag);
1365 *frghdrp = mtod(mfrg, struct ip6_frag *);
1366 mlast->m_next = mfrg;
1373 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1374 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1375 int *alwaysfragp, u_int fibnum)
1381 if (ro_pmtu != ro) {
1382 /* The first hop and the final destination may differ. */
1383 struct sockaddr_in6 *sa6_dst =
1384 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1385 if (ro_pmtu->ro_rt &&
1386 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1387 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1388 RTFREE(ro_pmtu->ro_rt);
1389 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1391 if (ro_pmtu->ro_rt == NULL) {
1392 bzero(sa6_dst, sizeof(*sa6_dst));
1393 sa6_dst->sin6_family = AF_INET6;
1394 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1395 sa6_dst->sin6_addr = *dst;
1397 in6_rtalloc(ro_pmtu, fibnum);
1400 if (ro_pmtu->ro_rt) {
1402 struct in_conninfo inc;
1404 bzero(&inc, sizeof(inc));
1405 inc.inc_flags |= INC_ISIPV6;
1406 inc.inc6_faddr = *dst;
1409 ifp = ro_pmtu->ro_rt->rt_ifp;
1410 ifmtu = IN6_LINKMTU(ifp);
1411 mtu = tcp_hc_getmtu(&inc);
1413 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1415 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1418 else if (mtu < IPV6_MMTU) {
1420 * RFC2460 section 5, last paragraph:
1421 * if we record ICMPv6 too big message with
1422 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1423 * or smaller, with framgent header attached.
1424 * (fragment header is needed regardless from the
1425 * packet size, for translators to identify packets)
1429 } else if (mtu > ifmtu) {
1431 * The MTU on the route is larger than the MTU on
1432 * the interface! This shouldn't happen, unless the
1433 * MTU of the interface has been changed after the
1434 * interface was brought up. Change the MTU in the
1435 * route to match the interface MTU (as long as the
1436 * field isn't locked).
1439 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1442 mtu = IN6_LINKMTU(ifp);
1444 error = EHOSTUNREACH; /* XXX */
1448 *alwaysfragp = alwaysfrag;
1453 * IP6 socket option processing.
1456 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1458 int optdatalen, uproto;
1460 struct inpcb *in6p = sotoinpcb(so);
1462 int level, op, optname;
1466 level = sopt->sopt_level;
1467 op = sopt->sopt_dir;
1468 optname = sopt->sopt_name;
1469 optlen = sopt->sopt_valsize;
1473 uproto = (int)so->so_proto->pr_protocol;
1475 if (level != IPPROTO_IPV6) {
1478 if (sopt->sopt_level == SOL_SOCKET &&
1479 sopt->sopt_dir == SOPT_SET) {
1480 switch (sopt->sopt_name) {
1483 if ((so->so_options & SO_REUSEADDR) != 0)
1484 in6p->inp_flags2 |= INP_REUSEADDR;
1486 in6p->inp_flags2 &= ~INP_REUSEADDR;
1492 if ((so->so_options & SO_REUSEPORT) != 0)
1493 in6p->inp_flags2 |= INP_REUSEPORT;
1495 in6p->inp_flags2 &= ~INP_REUSEPORT;
1501 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1509 } else { /* level == IPPROTO_IPV6 */
1514 case IPV6_2292PKTOPTIONS:
1515 #ifdef IPV6_PKTOPTIONS
1516 case IPV6_PKTOPTIONS:
1521 error = soopt_getm(sopt, &m); /* XXX */
1524 error = soopt_mcopyin(sopt, m); /* XXX */
1527 error = ip6_pcbopts(&in6p->in6p_outputopts,
1529 m_freem(m); /* XXX */
1534 * Use of some Hop-by-Hop options or some
1535 * Destination options, might require special
1536 * privilege. That is, normal applications
1537 * (without special privilege) might be forbidden
1538 * from setting certain options in outgoing packets,
1539 * and might never see certain options in received
1540 * packets. [RFC 2292 Section 6]
1541 * KAME specific note:
1542 * KAME prevents non-privileged users from sending or
1543 * receiving ANY hbh/dst options in order to avoid
1544 * overhead of parsing options in the kernel.
1546 case IPV6_RECVHOPOPTS:
1547 case IPV6_RECVDSTOPTS:
1548 case IPV6_RECVRTHDRDSTOPTS:
1550 error = priv_check(td,
1551 PRIV_NETINET_SETHDROPTS);
1556 case IPV6_UNICAST_HOPS:
1560 case IPV6_RECVPKTINFO:
1561 case IPV6_RECVHOPLIMIT:
1562 case IPV6_RECVRTHDR:
1563 case IPV6_RECVPATHMTU:
1564 case IPV6_RECVTCLASS:
1566 case IPV6_AUTOFLOWLABEL:
1568 if (optname == IPV6_BINDANY && td != NULL) {
1569 error = priv_check(td,
1570 PRIV_NETINET_BINDANY);
1575 if (optlen != sizeof(int)) {
1579 error = sooptcopyin(sopt, &optval,
1580 sizeof optval, sizeof optval);
1585 case IPV6_UNICAST_HOPS:
1586 if (optval < -1 || optval >= 256)
1589 /* -1 = kernel default */
1590 in6p->in6p_hops = optval;
1591 if ((in6p->inp_vflag &
1593 in6p->inp_ip_ttl = optval;
1596 #define OPTSET(bit) \
1600 in6p->inp_flags |= (bit); \
1602 in6p->inp_flags &= ~(bit); \
1603 INP_WUNLOCK(in6p); \
1604 } while (/*CONSTCOND*/ 0)
1605 #define OPTSET2292(bit) \
1608 in6p->inp_flags |= IN6P_RFC2292; \
1610 in6p->inp_flags |= (bit); \
1612 in6p->inp_flags &= ~(bit); \
1613 INP_WUNLOCK(in6p); \
1614 } while (/*CONSTCOND*/ 0)
1615 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1617 case IPV6_RECVPKTINFO:
1618 /* cannot mix with RFC2292 */
1619 if (OPTBIT(IN6P_RFC2292)) {
1623 OPTSET(IN6P_PKTINFO);
1628 struct ip6_pktopts **optp;
1630 /* cannot mix with RFC2292 */
1631 if (OPTBIT(IN6P_RFC2292)) {
1635 optp = &in6p->in6p_outputopts;
1636 error = ip6_pcbopt(IPV6_HOPLIMIT,
1637 (u_char *)&optval, sizeof(optval),
1638 optp, (td != NULL) ? td->td_ucred :
1643 case IPV6_RECVHOPLIMIT:
1644 /* cannot mix with RFC2292 */
1645 if (OPTBIT(IN6P_RFC2292)) {
1649 OPTSET(IN6P_HOPLIMIT);
1652 case IPV6_RECVHOPOPTS:
1653 /* cannot mix with RFC2292 */
1654 if (OPTBIT(IN6P_RFC2292)) {
1658 OPTSET(IN6P_HOPOPTS);
1661 case IPV6_RECVDSTOPTS:
1662 /* cannot mix with RFC2292 */
1663 if (OPTBIT(IN6P_RFC2292)) {
1667 OPTSET(IN6P_DSTOPTS);
1670 case IPV6_RECVRTHDRDSTOPTS:
1671 /* cannot mix with RFC2292 */
1672 if (OPTBIT(IN6P_RFC2292)) {
1676 OPTSET(IN6P_RTHDRDSTOPTS);
1679 case IPV6_RECVRTHDR:
1680 /* cannot mix with RFC2292 */
1681 if (OPTBIT(IN6P_RFC2292)) {
1692 case IPV6_RECVPATHMTU:
1694 * We ignore this option for TCP
1696 * (RFC3542 leaves this case
1699 if (uproto != IPPROTO_TCP)
1705 * make setsockopt(IPV6_V6ONLY)
1706 * available only prior to bind(2).
1707 * see ipng mailing list, Jun 22 2001.
1709 if (in6p->inp_lport ||
1710 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1714 OPTSET(IN6P_IPV6_V6ONLY);
1716 in6p->inp_vflag &= ~INP_IPV4;
1718 in6p->inp_vflag |= INP_IPV4;
1720 case IPV6_RECVTCLASS:
1721 /* cannot mix with RFC2292 XXX */
1722 if (OPTBIT(IN6P_RFC2292)) {
1726 OPTSET(IN6P_TCLASS);
1728 case IPV6_AUTOFLOWLABEL:
1729 OPTSET(IN6P_AUTOFLOWLABEL);
1733 OPTSET(INP_BINDANY);
1740 case IPV6_USE_MIN_MTU:
1741 case IPV6_PREFER_TEMPADDR:
1742 if (optlen != sizeof(optval)) {
1746 error = sooptcopyin(sopt, &optval,
1747 sizeof optval, sizeof optval);
1751 struct ip6_pktopts **optp;
1752 optp = &in6p->in6p_outputopts;
1753 error = ip6_pcbopt(optname,
1754 (u_char *)&optval, sizeof(optval),
1755 optp, (td != NULL) ? td->td_ucred :
1760 case IPV6_2292PKTINFO:
1761 case IPV6_2292HOPLIMIT:
1762 case IPV6_2292HOPOPTS:
1763 case IPV6_2292DSTOPTS:
1764 case IPV6_2292RTHDR:
1766 if (optlen != sizeof(int)) {
1770 error = sooptcopyin(sopt, &optval,
1771 sizeof optval, sizeof optval);
1775 case IPV6_2292PKTINFO:
1776 OPTSET2292(IN6P_PKTINFO);
1778 case IPV6_2292HOPLIMIT:
1779 OPTSET2292(IN6P_HOPLIMIT);
1781 case IPV6_2292HOPOPTS:
1783 * Check super-user privilege.
1784 * See comments for IPV6_RECVHOPOPTS.
1787 error = priv_check(td,
1788 PRIV_NETINET_SETHDROPTS);
1792 OPTSET2292(IN6P_HOPOPTS);
1794 case IPV6_2292DSTOPTS:
1796 error = priv_check(td,
1797 PRIV_NETINET_SETHDROPTS);
1801 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1803 case IPV6_2292RTHDR:
1804 OPTSET2292(IN6P_RTHDR);
1812 case IPV6_RTHDRDSTOPTS:
1815 /* new advanced API (RFC3542) */
1817 u_char optbuf_storage[MCLBYTES];
1819 struct ip6_pktopts **optp;
1821 /* cannot mix with RFC2292 */
1822 if (OPTBIT(IN6P_RFC2292)) {
1828 * We only ensure valsize is not too large
1829 * here. Further validation will be done
1832 error = sooptcopyin(sopt, optbuf_storage,
1833 sizeof(optbuf_storage), 0);
1836 optlen = sopt->sopt_valsize;
1837 optbuf = optbuf_storage;
1838 optp = &in6p->in6p_outputopts;
1839 error = ip6_pcbopt(optname, optbuf, optlen,
1840 optp, (td != NULL) ? td->td_ucred : NULL,
1846 case IPV6_MULTICAST_IF:
1847 case IPV6_MULTICAST_HOPS:
1848 case IPV6_MULTICAST_LOOP:
1849 case IPV6_JOIN_GROUP:
1850 case IPV6_LEAVE_GROUP:
1852 case MCAST_BLOCK_SOURCE:
1853 case MCAST_UNBLOCK_SOURCE:
1854 case MCAST_JOIN_GROUP:
1855 case MCAST_LEAVE_GROUP:
1856 case MCAST_JOIN_SOURCE_GROUP:
1857 case MCAST_LEAVE_SOURCE_GROUP:
1858 error = ip6_setmoptions(in6p, sopt);
1861 case IPV6_PORTRANGE:
1862 error = sooptcopyin(sopt, &optval,
1863 sizeof optval, sizeof optval);
1869 case IPV6_PORTRANGE_DEFAULT:
1870 in6p->inp_flags &= ~(INP_LOWPORT);
1871 in6p->inp_flags &= ~(INP_HIGHPORT);
1874 case IPV6_PORTRANGE_HIGH:
1875 in6p->inp_flags &= ~(INP_LOWPORT);
1876 in6p->inp_flags |= INP_HIGHPORT;
1879 case IPV6_PORTRANGE_LOW:
1880 in6p->inp_flags &= ~(INP_HIGHPORT);
1881 in6p->inp_flags |= INP_LOWPORT;
1892 case IPV6_IPSEC_POLICY:
1897 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1899 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1901 req = mtod(m, caddr_t);
1902 error = ipsec_set_policy(in6p, optname, req,
1903 m->m_len, (sopt->sopt_td != NULL) ?
1904 sopt->sopt_td->td_ucred : NULL);
1911 error = ENOPROTOOPT;
1919 case IPV6_2292PKTOPTIONS:
1920 #ifdef IPV6_PKTOPTIONS
1921 case IPV6_PKTOPTIONS:
1924 * RFC3542 (effectively) deprecated the
1925 * semantics of the 2292-style pktoptions.
1926 * Since it was not reliable in nature (i.e.,
1927 * applications had to expect the lack of some
1928 * information after all), it would make sense
1929 * to simplify this part by always returning
1932 sopt->sopt_valsize = 0;
1935 case IPV6_RECVHOPOPTS:
1936 case IPV6_RECVDSTOPTS:
1937 case IPV6_RECVRTHDRDSTOPTS:
1938 case IPV6_UNICAST_HOPS:
1939 case IPV6_RECVPKTINFO:
1940 case IPV6_RECVHOPLIMIT:
1941 case IPV6_RECVRTHDR:
1942 case IPV6_RECVPATHMTU:
1946 case IPV6_PORTRANGE:
1947 case IPV6_RECVTCLASS:
1948 case IPV6_AUTOFLOWLABEL:
1952 case IPV6_RECVHOPOPTS:
1953 optval = OPTBIT(IN6P_HOPOPTS);
1956 case IPV6_RECVDSTOPTS:
1957 optval = OPTBIT(IN6P_DSTOPTS);
1960 case IPV6_RECVRTHDRDSTOPTS:
1961 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1964 case IPV6_UNICAST_HOPS:
1965 optval = in6p->in6p_hops;
1968 case IPV6_RECVPKTINFO:
1969 optval = OPTBIT(IN6P_PKTINFO);
1972 case IPV6_RECVHOPLIMIT:
1973 optval = OPTBIT(IN6P_HOPLIMIT);
1976 case IPV6_RECVRTHDR:
1977 optval = OPTBIT(IN6P_RTHDR);
1980 case IPV6_RECVPATHMTU:
1981 optval = OPTBIT(IN6P_MTU);
1985 optval = OPTBIT(INP_FAITH);
1989 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1992 case IPV6_PORTRANGE:
1995 flags = in6p->inp_flags;
1996 if (flags & INP_HIGHPORT)
1997 optval = IPV6_PORTRANGE_HIGH;
1998 else if (flags & INP_LOWPORT)
1999 optval = IPV6_PORTRANGE_LOW;
2004 case IPV6_RECVTCLASS:
2005 optval = OPTBIT(IN6P_TCLASS);
2008 case IPV6_AUTOFLOWLABEL:
2009 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2013 optval = OPTBIT(INP_BINDANY);
2018 error = sooptcopyout(sopt, &optval,
2025 struct ip6_mtuinfo mtuinfo;
2026 struct route_in6 sro;
2028 bzero(&sro, sizeof(sro));
2030 if (!(so->so_state & SS_ISCONNECTED))
2033 * XXX: we dot not consider the case of source
2034 * routing, or optional information to specify
2035 * the outgoing interface.
2037 error = ip6_getpmtu(&sro, NULL, NULL,
2038 &in6p->in6p_faddr, &pmtu, NULL,
2044 if (pmtu > IPV6_MAXPACKET)
2045 pmtu = IPV6_MAXPACKET;
2047 bzero(&mtuinfo, sizeof(mtuinfo));
2048 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2049 optdata = (void *)&mtuinfo;
2050 optdatalen = sizeof(mtuinfo);
2051 error = sooptcopyout(sopt, optdata,
2056 case IPV6_2292PKTINFO:
2057 case IPV6_2292HOPLIMIT:
2058 case IPV6_2292HOPOPTS:
2059 case IPV6_2292RTHDR:
2060 case IPV6_2292DSTOPTS:
2062 case IPV6_2292PKTINFO:
2063 optval = OPTBIT(IN6P_PKTINFO);
2065 case IPV6_2292HOPLIMIT:
2066 optval = OPTBIT(IN6P_HOPLIMIT);
2068 case IPV6_2292HOPOPTS:
2069 optval = OPTBIT(IN6P_HOPOPTS);
2071 case IPV6_2292RTHDR:
2072 optval = OPTBIT(IN6P_RTHDR);
2074 case IPV6_2292DSTOPTS:
2075 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2078 error = sooptcopyout(sopt, &optval,
2085 case IPV6_RTHDRDSTOPTS:
2089 case IPV6_USE_MIN_MTU:
2090 case IPV6_PREFER_TEMPADDR:
2091 error = ip6_getpcbopt(in6p->in6p_outputopts,
2095 case IPV6_MULTICAST_IF:
2096 case IPV6_MULTICAST_HOPS:
2097 case IPV6_MULTICAST_LOOP:
2099 error = ip6_getmoptions(in6p, sopt);
2103 case IPV6_IPSEC_POLICY:
2107 struct mbuf *m = NULL;
2108 struct mbuf **mp = &m;
2109 size_t ovalsize = sopt->sopt_valsize;
2110 caddr_t oval = (caddr_t)sopt->sopt_val;
2112 error = soopt_getm(sopt, &m); /* XXX */
2115 error = soopt_mcopyin(sopt, m); /* XXX */
2118 sopt->sopt_valsize = ovalsize;
2119 sopt->sopt_val = oval;
2121 req = mtod(m, caddr_t);
2124 error = ipsec_get_policy(in6p, req, len, mp);
2126 error = soopt_mcopyout(sopt, m); /* XXX */
2127 if (error == 0 && m)
2134 error = ENOPROTOOPT;
2144 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2146 int error = 0, optval, optlen;
2147 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2148 struct inpcb *in6p = sotoinpcb(so);
2149 int level, op, optname;
2151 level = sopt->sopt_level;
2152 op = sopt->sopt_dir;
2153 optname = sopt->sopt_name;
2154 optlen = sopt->sopt_valsize;
2156 if (level != IPPROTO_IPV6) {
2163 * For ICMPv6 sockets, no modification allowed for checksum
2164 * offset, permit "no change" values to help existing apps.
2166 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2167 * for an ICMPv6 socket will fail."
2168 * The current behavior does not meet RFC3542.
2172 if (optlen != sizeof(int)) {
2176 error = sooptcopyin(sopt, &optval, sizeof(optval),
2180 if ((optval % 2) != 0) {
2181 /* the API assumes even offset values */
2183 } else if (so->so_proto->pr_protocol ==
2185 if (optval != icmp6off)
2188 in6p->in6p_cksum = optval;
2192 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2195 optval = in6p->in6p_cksum;
2197 error = sooptcopyout(sopt, &optval, sizeof(optval));
2207 error = ENOPROTOOPT;
2215 * Set up IP6 options in pcb for insertion in output packets or
2216 * specifying behavior of outgoing packets.
2219 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2220 struct socket *so, struct sockopt *sopt)
2222 struct ip6_pktopts *opt = *pktopt;
2224 struct thread *td = sopt->sopt_td;
2226 /* turn off any old options. */
2229 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2230 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2231 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2232 printf("ip6_pcbopts: all specified options are cleared.\n");
2234 ip6_clearpktopts(opt, -1);
2236 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2239 if (!m || m->m_len == 0) {
2241 * Only turning off any previous options, regardless of
2242 * whether the opt is just created or given.
2244 free(opt, M_IP6OPT);
2248 /* set options specified by user. */
2249 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2250 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2251 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2252 free(opt, M_IP6OPT);
2260 * initialize ip6_pktopts. beware that there are non-zero default values in
2264 ip6_initpktopts(struct ip6_pktopts *opt)
2267 bzero(opt, sizeof(*opt));
2268 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2269 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2270 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2271 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2275 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2276 struct ucred *cred, int uproto)
2278 struct ip6_pktopts *opt;
2280 if (*pktopt == NULL) {
2281 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2283 ip6_initpktopts(*pktopt);
2287 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2291 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2293 void *optdata = NULL;
2295 struct ip6_ext *ip6e;
2297 struct in6_pktinfo null_pktinfo;
2298 int deftclass = 0, on;
2299 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2300 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2304 if (pktopt && pktopt->ip6po_pktinfo)
2305 optdata = (void *)pktopt->ip6po_pktinfo;
2307 /* XXX: we don't have to do this every time... */
2308 bzero(&null_pktinfo, sizeof(null_pktinfo));
2309 optdata = (void *)&null_pktinfo;
2311 optdatalen = sizeof(struct in6_pktinfo);
2314 if (pktopt && pktopt->ip6po_tclass >= 0)
2315 optdata = (void *)&pktopt->ip6po_tclass;
2317 optdata = (void *)&deftclass;
2318 optdatalen = sizeof(int);
2321 if (pktopt && pktopt->ip6po_hbh) {
2322 optdata = (void *)pktopt->ip6po_hbh;
2323 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2324 optdatalen = (ip6e->ip6e_len + 1) << 3;
2328 if (pktopt && pktopt->ip6po_rthdr) {
2329 optdata = (void *)pktopt->ip6po_rthdr;
2330 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2331 optdatalen = (ip6e->ip6e_len + 1) << 3;
2334 case IPV6_RTHDRDSTOPTS:
2335 if (pktopt && pktopt->ip6po_dest1) {
2336 optdata = (void *)pktopt->ip6po_dest1;
2337 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2338 optdatalen = (ip6e->ip6e_len + 1) << 3;
2342 if (pktopt && pktopt->ip6po_dest2) {
2343 optdata = (void *)pktopt->ip6po_dest2;
2344 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2345 optdatalen = (ip6e->ip6e_len + 1) << 3;
2349 if (pktopt && pktopt->ip6po_nexthop) {
2350 optdata = (void *)pktopt->ip6po_nexthop;
2351 optdatalen = pktopt->ip6po_nexthop->sa_len;
2354 case IPV6_USE_MIN_MTU:
2356 optdata = (void *)&pktopt->ip6po_minmtu;
2358 optdata = (void *)&defminmtu;
2359 optdatalen = sizeof(int);
2362 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2366 optdata = (void *)&on;
2367 optdatalen = sizeof(on);
2369 case IPV6_PREFER_TEMPADDR:
2371 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2373 optdata = (void *)&defpreftemp;
2374 optdatalen = sizeof(int);
2376 default: /* should not happen */
2378 panic("ip6_getpcbopt: unexpected option\n");
2380 return (ENOPROTOOPT);
2383 error = sooptcopyout(sopt, optdata, optdatalen);
2389 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2394 if (optname == -1 || optname == IPV6_PKTINFO) {
2395 if (pktopt->ip6po_pktinfo)
2396 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2397 pktopt->ip6po_pktinfo = NULL;
2399 if (optname == -1 || optname == IPV6_HOPLIMIT)
2400 pktopt->ip6po_hlim = -1;
2401 if (optname == -1 || optname == IPV6_TCLASS)
2402 pktopt->ip6po_tclass = -1;
2403 if (optname == -1 || optname == IPV6_NEXTHOP) {
2404 if (pktopt->ip6po_nextroute.ro_rt) {
2405 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2406 pktopt->ip6po_nextroute.ro_rt = NULL;
2408 if (pktopt->ip6po_nexthop)
2409 free(pktopt->ip6po_nexthop, M_IP6OPT);
2410 pktopt->ip6po_nexthop = NULL;
2412 if (optname == -1 || optname == IPV6_HOPOPTS) {
2413 if (pktopt->ip6po_hbh)
2414 free(pktopt->ip6po_hbh, M_IP6OPT);
2415 pktopt->ip6po_hbh = NULL;
2417 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2418 if (pktopt->ip6po_dest1)
2419 free(pktopt->ip6po_dest1, M_IP6OPT);
2420 pktopt->ip6po_dest1 = NULL;
2422 if (optname == -1 || optname == IPV6_RTHDR) {
2423 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2424 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2425 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2426 if (pktopt->ip6po_route.ro_rt) {
2427 RTFREE(pktopt->ip6po_route.ro_rt);
2428 pktopt->ip6po_route.ro_rt = NULL;
2431 if (optname == -1 || optname == IPV6_DSTOPTS) {
2432 if (pktopt->ip6po_dest2)
2433 free(pktopt->ip6po_dest2, M_IP6OPT);
2434 pktopt->ip6po_dest2 = NULL;
2438 #define PKTOPT_EXTHDRCPY(type) \
2441 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2442 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2443 if (dst->type == NULL && canwait == M_NOWAIT)\
2445 bcopy(src->type, dst->type, hlen);\
2447 } while (/*CONSTCOND*/ 0)
2450 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2452 if (dst == NULL || src == NULL) {
2453 printf("ip6_clearpktopts: invalid argument\n");
2457 dst->ip6po_hlim = src->ip6po_hlim;
2458 dst->ip6po_tclass = src->ip6po_tclass;
2459 dst->ip6po_flags = src->ip6po_flags;
2460 dst->ip6po_minmtu = src->ip6po_minmtu;
2461 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2462 if (src->ip6po_pktinfo) {
2463 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2465 if (dst->ip6po_pktinfo == NULL)
2467 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2469 if (src->ip6po_nexthop) {
2470 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2472 if (dst->ip6po_nexthop == NULL)
2474 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2475 src->ip6po_nexthop->sa_len);
2477 PKTOPT_EXTHDRCPY(ip6po_hbh);
2478 PKTOPT_EXTHDRCPY(ip6po_dest1);
2479 PKTOPT_EXTHDRCPY(ip6po_dest2);
2480 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2484 ip6_clearpktopts(dst, -1);
2487 #undef PKTOPT_EXTHDRCPY
2489 struct ip6_pktopts *
2490 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2493 struct ip6_pktopts *dst;
2495 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2498 ip6_initpktopts(dst);
2500 if ((error = copypktopts(dst, src, canwait)) != 0) {
2501 free(dst, M_IP6OPT);
2509 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2514 ip6_clearpktopts(pktopt, -1);
2516 free(pktopt, M_IP6OPT);
2520 * Set IPv6 outgoing packet options based on advanced API.
2523 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2524 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2526 struct cmsghdr *cm = 0;
2528 if (control == NULL || opt == NULL)
2531 ip6_initpktopts(opt);
2536 * If stickyopt is provided, make a local copy of the options
2537 * for this particular packet, then override them by ancillary
2539 * XXX: copypktopts() does not copy the cached route to a next
2540 * hop (if any). This is not very good in terms of efficiency,
2541 * but we can allow this since this option should be rarely
2544 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2549 * XXX: Currently, we assume all the optional information is stored
2552 if (control->m_next)
2555 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2556 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2559 if (control->m_len < CMSG_LEN(0))
2562 cm = mtod(control, struct cmsghdr *);
2563 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2565 if (cm->cmsg_level != IPPROTO_IPV6)
2568 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2569 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2578 * Set a particular packet option, as a sticky option or an ancillary data
2579 * item. "len" can be 0 only when it's a sticky option.
2580 * We have 4 cases of combination of "sticky" and "cmsg":
2581 * "sticky=0, cmsg=0": impossible
2582 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2583 * "sticky=1, cmsg=0": RFC3542 socket option
2584 * "sticky=1, cmsg=1": RFC2292 socket option
2587 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2588 struct ucred *cred, int sticky, int cmsg, int uproto)
2590 int minmtupolicy, preftemp;
2593 if (!sticky && !cmsg) {
2595 printf("ip6_setpktopt: impossible case\n");
2601 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2602 * not be specified in the context of RFC3542. Conversely,
2603 * RFC3542 types should not be specified in the context of RFC2292.
2607 case IPV6_2292PKTINFO:
2608 case IPV6_2292HOPLIMIT:
2609 case IPV6_2292NEXTHOP:
2610 case IPV6_2292HOPOPTS:
2611 case IPV6_2292DSTOPTS:
2612 case IPV6_2292RTHDR:
2613 case IPV6_2292PKTOPTIONS:
2614 return (ENOPROTOOPT);
2617 if (sticky && cmsg) {
2624 case IPV6_RTHDRDSTOPTS:
2626 case IPV6_USE_MIN_MTU:
2629 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2630 return (ENOPROTOOPT);
2635 case IPV6_2292PKTINFO:
2638 struct ifnet *ifp = NULL;
2639 struct in6_pktinfo *pktinfo;
2641 if (len != sizeof(struct in6_pktinfo))
2644 pktinfo = (struct in6_pktinfo *)buf;
2647 * An application can clear any sticky IPV6_PKTINFO option by
2648 * doing a "regular" setsockopt with ipi6_addr being
2649 * in6addr_any and ipi6_ifindex being zero.
2650 * [RFC 3542, Section 6]
2652 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2653 pktinfo->ipi6_ifindex == 0 &&
2654 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2655 ip6_clearpktopts(opt, optname);
2659 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2660 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2664 /* validate the interface index if specified. */
2665 if (pktinfo->ipi6_ifindex > V_if_index)
2667 if (pktinfo->ipi6_ifindex) {
2668 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2674 * We store the address anyway, and let in6_selectsrc()
2675 * validate the specified address. This is because ipi6_addr
2676 * may not have enough information about its scope zone, and
2677 * we may need additional information (such as outgoing
2678 * interface or the scope zone of a destination address) to
2679 * disambiguate the scope.
2680 * XXX: the delay of the validation may confuse the
2681 * application when it is used as a sticky option.
2683 if (opt->ip6po_pktinfo == NULL) {
2684 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2685 M_IP6OPT, M_NOWAIT);
2686 if (opt->ip6po_pktinfo == NULL)
2689 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2693 case IPV6_2292HOPLIMIT:
2699 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2700 * to simplify the ordering among hoplimit options.
2702 if (optname == IPV6_HOPLIMIT && sticky)
2703 return (ENOPROTOOPT);
2705 if (len != sizeof(int))
2708 if (*hlimp < -1 || *hlimp > 255)
2711 opt->ip6po_hlim = *hlimp;
2719 if (len != sizeof(int))
2721 tclass = *(int *)buf;
2722 if (tclass < -1 || tclass > 255)
2725 opt->ip6po_tclass = tclass;
2729 case IPV6_2292NEXTHOP:
2732 error = priv_check_cred(cred,
2733 PRIV_NETINET_SETHDROPTS, 0);
2738 if (len == 0) { /* just remove the option */
2739 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2743 /* check if cmsg_len is large enough for sa_len */
2744 if (len < sizeof(struct sockaddr) || len < *buf)
2747 switch (((struct sockaddr *)buf)->sa_family) {
2750 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2753 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2756 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2757 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2760 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2766 case AF_LINK: /* should eventually be supported */
2768 return (EAFNOSUPPORT);
2771 /* turn off the previous option, then set the new option. */
2772 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2773 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2774 if (opt->ip6po_nexthop == NULL)
2776 bcopy(buf, opt->ip6po_nexthop, *buf);
2779 case IPV6_2292HOPOPTS:
2782 struct ip6_hbh *hbh;
2786 * XXX: We don't allow a non-privileged user to set ANY HbH
2787 * options, since per-option restriction has too much
2791 error = priv_check_cred(cred,
2792 PRIV_NETINET_SETHDROPTS, 0);
2798 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2799 break; /* just remove the option */
2802 /* message length validation */
2803 if (len < sizeof(struct ip6_hbh))
2805 hbh = (struct ip6_hbh *)buf;
2806 hbhlen = (hbh->ip6h_len + 1) << 3;
2810 /* turn off the previous option, then set the new option. */
2811 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2812 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2813 if (opt->ip6po_hbh == NULL)
2815 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2820 case IPV6_2292DSTOPTS:
2822 case IPV6_RTHDRDSTOPTS:
2824 struct ip6_dest *dest, **newdest = NULL;
2827 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2828 error = priv_check_cred(cred,
2829 PRIV_NETINET_SETHDROPTS, 0);
2835 ip6_clearpktopts(opt, optname);
2836 break; /* just remove the option */
2839 /* message length validation */
2840 if (len < sizeof(struct ip6_dest))
2842 dest = (struct ip6_dest *)buf;
2843 destlen = (dest->ip6d_len + 1) << 3;
2848 * Determine the position that the destination options header
2849 * should be inserted; before or after the routing header.
2852 case IPV6_2292DSTOPTS:
2854 * The old advacned API is ambiguous on this point.
2855 * Our approach is to determine the position based
2856 * according to the existence of a routing header.
2857 * Note, however, that this depends on the order of the
2858 * extension headers in the ancillary data; the 1st
2859 * part of the destination options header must appear
2860 * before the routing header in the ancillary data,
2862 * RFC3542 solved the ambiguity by introducing
2863 * separate ancillary data or option types.
2865 if (opt->ip6po_rthdr == NULL)
2866 newdest = &opt->ip6po_dest1;
2868 newdest = &opt->ip6po_dest2;
2870 case IPV6_RTHDRDSTOPTS:
2871 newdest = &opt->ip6po_dest1;
2874 newdest = &opt->ip6po_dest2;
2878 /* turn off the previous option, then set the new option. */
2879 ip6_clearpktopts(opt, optname);
2880 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2881 if (*newdest == NULL)
2883 bcopy(dest, *newdest, destlen);
2888 case IPV6_2292RTHDR:
2891 struct ip6_rthdr *rth;
2895 ip6_clearpktopts(opt, IPV6_RTHDR);
2896 break; /* just remove the option */
2899 /* message length validation */
2900 if (len < sizeof(struct ip6_rthdr))
2902 rth = (struct ip6_rthdr *)buf;
2903 rthlen = (rth->ip6r_len + 1) << 3;
2907 switch (rth->ip6r_type) {
2908 case IPV6_RTHDR_TYPE_0:
2909 if (rth->ip6r_len == 0) /* must contain one addr */
2911 if (rth->ip6r_len % 2) /* length must be even */
2913 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2917 return (EINVAL); /* not supported */
2920 /* turn off the previous option */
2921 ip6_clearpktopts(opt, IPV6_RTHDR);
2922 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2923 if (opt->ip6po_rthdr == NULL)
2925 bcopy(rth, opt->ip6po_rthdr, rthlen);
2930 case IPV6_USE_MIN_MTU:
2931 if (len != sizeof(int))
2933 minmtupolicy = *(int *)buf;
2934 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2935 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2936 minmtupolicy != IP6PO_MINMTU_ALL) {
2939 opt->ip6po_minmtu = minmtupolicy;
2943 if (len != sizeof(int))
2946 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2948 * we ignore this option for TCP sockets.
2949 * (RFC3542 leaves this case unspecified.)
2951 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2953 opt->ip6po_flags |= IP6PO_DONTFRAG;
2956 case IPV6_PREFER_TEMPADDR:
2957 if (len != sizeof(int))
2959 preftemp = *(int *)buf;
2960 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2961 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2962 preftemp != IP6PO_TEMPADDR_PREFER) {
2965 opt->ip6po_prefer_tempaddr = preftemp;
2969 return (ENOPROTOOPT);
2970 } /* end of switch */
2976 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2977 * packet to the input queue of a specified interface. Note that this
2978 * calls the output routine of the loopback "driver", but with an interface
2979 * pointer that might NOT be &loif -- easier than replicating that code here.
2982 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2985 struct ip6_hdr *ip6;
2987 copym = m_copy(m, 0, M_COPYALL);
2992 * Make sure to deep-copy IPv6 header portion in case the data
2993 * is in an mbuf cluster, so that we can safely override the IPv6
2994 * header portion later.
2996 if ((copym->m_flags & M_EXT) != 0 ||
2997 copym->m_len < sizeof(struct ip6_hdr)) {
2998 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3004 if (copym->m_len < sizeof(*ip6)) {
3010 ip6 = mtod(copym, struct ip6_hdr *);
3012 * clear embedded scope identifiers if necessary.
3013 * in6_clearscope will touch the addresses only when necessary.
3015 in6_clearscope(&ip6->ip6_src);
3016 in6_clearscope(&ip6->ip6_dst);
3018 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3022 * Chop IPv6 header off from the payload.
3025 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3028 struct ip6_hdr *ip6;
3030 ip6 = mtod(m, struct ip6_hdr *);
3031 if (m->m_len > sizeof(*ip6)) {
3032 mh = m_gethdr(M_NOWAIT, MT_DATA);
3037 m_move_pkthdr(mh, m);
3038 MH_ALIGN(mh, sizeof(*ip6));
3039 m->m_len -= sizeof(*ip6);
3040 m->m_data += sizeof(*ip6);
3043 m->m_len = sizeof(*ip6);
3044 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3046 exthdrs->ip6e_ip6 = m;
3051 * Compute IPv6 extension header length.
3054 ip6_optlen(struct inpcb *in6p)
3058 if (!in6p->in6p_outputopts)
3063 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3065 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3066 if (in6p->in6p_outputopts->ip6po_rthdr)
3067 /* dest1 is valid with rthdr only */
3068 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3069 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3070 len += elen(in6p->in6p_outputopts->ip6po_dest2);