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 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ratelimit.h"
69 #include "opt_ipsec.h"
71 #include "opt_route.h"
74 #include <sys/param.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/errno.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/syslog.h>
85 #include <sys/ucred.h>
87 #include <machine/in_cksum.h>
90 #include <net/if_var.h>
91 #include <net/if_llatbl.h>
92 #include <net/netisr.h>
93 #include <net/route.h>
95 #include <net/rss_config.h>
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip_var.h>
101 #include <netinet6/in6_fib.h>
102 #include <netinet6/in6_var.h>
103 #include <netinet/ip6.h>
104 #include <netinet/icmp6.h>
105 #include <netinet6/ip6_var.h>
106 #include <netinet/in_pcb.h>
107 #include <netinet/tcp_var.h>
108 #include <netinet6/nd6.h>
109 #include <netinet6/in6_rss.h>
111 #include <netipsec/ipsec_support.h>
113 #include <netinet/sctp.h>
114 #include <netinet/sctp_crc32.h>
117 #include <netinet6/ip6protosw.h>
118 #include <netinet6/scope6_var.h>
120 extern int in6_mcast_loop;
123 struct mbuf *ip6e_ip6;
124 struct mbuf *ip6e_hbh;
125 struct mbuf *ip6e_dest1;
126 struct mbuf *ip6e_rthdr;
127 struct mbuf *ip6e_dest2;
130 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
132 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
133 struct ucred *, int);
134 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
135 struct socket *, struct sockopt *);
136 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
137 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
138 struct ucred *, int, int, int);
140 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
141 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
143 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
144 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
145 static int ip6_getpmtu(struct route_in6 *, int,
146 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
148 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
149 u_long *, int *, u_int);
150 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
151 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
155 * Make an extension header from option data. hp is the source, and
156 * mp is the destination.
158 #define MAKE_EXTHDR(hp, mp) \
161 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
162 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
163 ((eh)->ip6e_len + 1) << 3); \
167 } while (/*CONSTCOND*/ 0)
170 * Form a chain of extension headers.
171 * m is the extension header mbuf
172 * mp is the previous mbuf in the chain
173 * p is the next header
174 * i is the type of option.
176 #define MAKE_CHAIN(m, mp, p, i)\
180 panic("assumption failed: hdr not split"); \
181 *mtod((m), u_char *) = *(p);\
183 p = mtod((m), u_char *);\
184 (m)->m_next = (mp)->m_next;\
188 } while (/*CONSTCOND*/ 0)
191 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
195 csum = in_cksum_skip(m, offset + plen, offset);
196 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
198 offset += m->m_pkthdr.csum_data; /* checksum offset */
200 if (offset + sizeof(u_short) > m->m_len) {
201 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
202 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
203 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
205 * XXX this should not happen, but if it does, the correct
206 * behavior may be to insert the checksum in the appropriate
207 * next mbuf in the chain.
211 *(u_short *)(m->m_data + offset) = csum;
215 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
216 int fraglen , uint32_t id)
218 struct mbuf *m, **mnext, *m_frgpart;
219 struct ip6_hdr *ip6, *mhip6;
220 struct ip6_frag *ip6f;
223 int tlen = m0->m_pkthdr.len;
225 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
228 ip6 = mtod(m, struct ip6_hdr *);
229 mnext = &m->m_nextpkt;
231 for (off = hlen; off < tlen; off += fraglen) {
232 m = m_gethdr(M_NOWAIT, MT_DATA);
234 IP6STAT_INC(ip6s_odropped);
237 m->m_flags = m0->m_flags & M_COPYFLAGS;
239 mnext = &m->m_nextpkt;
240 m->m_data += max_linkhdr;
241 mhip6 = mtod(m, struct ip6_hdr *);
243 m->m_len = sizeof(*mhip6);
244 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
246 IP6STAT_INC(ip6s_odropped);
249 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
250 if (off + fraglen >= tlen)
251 fraglen = tlen - off;
253 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
254 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
255 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
256 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
257 IP6STAT_INC(ip6s_odropped);
261 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
262 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
263 m->m_pkthdr.rcvif = NULL;
264 ip6f->ip6f_reserved = 0;
265 ip6f->ip6f_ident = id;
266 ip6f->ip6f_nxt = nextproto;
267 IP6STAT_INC(ip6s_ofragments);
268 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
275 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
276 * header (with pri, len, nxt, hlim, src, dst).
277 * This function may modify ver and hlim only.
278 * The mbuf chain containing the packet will be freed.
279 * The mbuf opt, if present, will not be freed.
280 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
281 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
282 * then result of route lookup is stored in ro->ro_rt.
284 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
285 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
288 * ifpp - XXX: just for statistics
291 * XXX TODO: no flowid is assigned for outbound flows?
294 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
295 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
296 struct ifnet **ifpp, struct inpcb *inp)
299 struct ifnet *ifp, *origifp;
301 struct mbuf *mprev = NULL;
303 struct route_in6 ip6route;
304 struct rtentry *rt = NULL;
305 struct sockaddr_in6 *dst, src_sa, dst_sa;
306 struct in6_addr odst;
308 struct in6_ifaddr *ia = NULL;
310 int alwaysfrag, dontfrag;
311 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
312 struct ip6_exthdrs exthdrs;
313 struct in6_addr src0, dst0;
315 struct route_in6 *ro_pmtu = NULL;
320 struct m_tag *fwd_tag = NULL;
324 INP_LOCK_ASSERT(inp);
325 M_SETFIB(m, inp->inp_inc.inc_fibnum);
326 if ((flags & IP_NODEFAULTFLOWID) == 0) {
327 /* unconditionally set flowid */
328 m->m_pkthdr.flowid = inp->inp_flowid;
329 M_HASHTYPE_SET(m, inp->inp_flowtype);
333 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
335 * IPSec checking which handles several cases.
336 * FAST IPSEC: We re-injected the packet.
337 * XXX: need scope argument.
339 if (IPSEC_ENABLED(ipv6)) {
340 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
341 if (error == EINPROGRESS)
348 bzero(&exthdrs, sizeof(exthdrs));
350 /* Hop-by-Hop options header */
351 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
352 /* Destination options header(1st part) */
353 if (opt->ip6po_rthdr) {
355 * Destination options header(1st part)
356 * This only makes sense with a routing header.
357 * See Section 9.2 of RFC 3542.
358 * Disabling this part just for MIP6 convenience is
359 * a bad idea. We need to think carefully about a
360 * way to make the advanced API coexist with MIP6
361 * options, which might automatically be inserted in
364 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
367 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
368 /* Destination options header(2nd part) */
369 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
373 * Calculate the total length of the extension header chain.
374 * Keep the length of the unfragmentable part for fragmentation.
377 if (exthdrs.ip6e_hbh)
378 optlen += exthdrs.ip6e_hbh->m_len;
379 if (exthdrs.ip6e_dest1)
380 optlen += exthdrs.ip6e_dest1->m_len;
381 if (exthdrs.ip6e_rthdr)
382 optlen += exthdrs.ip6e_rthdr->m_len;
383 unfragpartlen = optlen + sizeof(struct ip6_hdr);
385 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
386 if (exthdrs.ip6e_dest2)
387 optlen += exthdrs.ip6e_dest2->m_len;
390 * If there is at least one extension header,
391 * separate IP6 header from the payload.
393 if (optlen && !hdrsplit) {
394 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
398 m = exthdrs.ip6e_ip6;
402 ip6 = mtod(m, struct ip6_hdr *);
404 /* adjust mbuf packet header length */
405 m->m_pkthdr.len += optlen;
406 plen = m->m_pkthdr.len - sizeof(*ip6);
408 /* If this is a jumbo payload, insert a jumbo payload option. */
409 if (plen > IPV6_MAXPACKET) {
411 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
415 m = exthdrs.ip6e_ip6;
419 ip6 = mtod(m, struct ip6_hdr *);
420 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
424 ip6->ip6_plen = htons(plen);
427 * Concatenate headers and fill in next header fields.
428 * Here we have, on "m"
430 * and we insert headers accordingly. Finally, we should be getting:
431 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
433 * during the header composing process, "m" points to IPv6 header.
434 * "mprev" points to an extension header prior to esp.
436 u_char *nexthdrp = &ip6->ip6_nxt;
440 * we treat dest2 specially. this makes IPsec processing
441 * much easier. the goal here is to make mprev point the
442 * mbuf prior to dest2.
444 * result: IPv6 dest2 payload
445 * m and mprev will point to IPv6 header.
447 if (exthdrs.ip6e_dest2) {
449 panic("assumption failed: hdr not split");
450 exthdrs.ip6e_dest2->m_next = m->m_next;
451 m->m_next = exthdrs.ip6e_dest2;
452 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
453 ip6->ip6_nxt = IPPROTO_DSTOPTS;
457 * result: IPv6 hbh dest1 rthdr dest2 payload
458 * m will point to IPv6 header. mprev will point to the
459 * extension header prior to dest2 (rthdr in the above case).
461 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
462 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
464 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
468 * If there is a routing header, discard the packet.
470 if (exthdrs.ip6e_rthdr) {
475 /* Source address validation */
476 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
477 (flags & IPV6_UNSPECSRC) == 0) {
479 IP6STAT_INC(ip6s_badscope);
482 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
484 IP6STAT_INC(ip6s_badscope);
488 IP6STAT_INC(ip6s_localout);
495 bzero((caddr_t)ro, sizeof(*ro));
498 if (opt && opt->ip6po_rthdr)
499 ro = &opt->ip6po_route;
500 dst = (struct sockaddr_in6 *)&ro->ro_dst;
501 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
504 * if specified, try to fill in the traffic class field.
505 * do not override if a non-zero value is already set.
506 * we check the diffserv field and the ecn field separately.
508 if (opt && opt->ip6po_tclass >= 0) {
511 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
513 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
516 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
519 /* fill in or override the hop limit field, if necessary. */
520 if (opt && opt->ip6po_hlim != -1)
521 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
522 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
524 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
526 ip6->ip6_hlim = V_ip6_defmcasthlim;
529 * Validate route against routing table additions;
530 * a better/more specific route might have been added.
531 * Make sure address family is set in route.
534 ro->ro_dst.sin6_family = AF_INET6;
535 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
537 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
538 ro->ro_dst.sin6_family == AF_INET6 &&
539 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
541 ifp = ro->ro_rt->rt_ifp;
544 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
546 if (fwd_tag == NULL) {
547 bzero(&dst_sa, sizeof(dst_sa));
548 dst_sa.sin6_family = AF_INET6;
549 dst_sa.sin6_len = sizeof(dst_sa);
550 dst_sa.sin6_addr = ip6->ip6_dst;
552 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
556 in6_ifstat_inc(ifp, ifs6_out_discard);
562 * If in6_selectroute() does not return a route entry,
563 * dst may not have been updated.
565 *dst = dst_sa; /* XXX */
569 * then rt (for unicast) and ifp must be non-NULL valid values.
571 if ((flags & IPV6_FORWARDING) == 0) {
572 /* XXX: the FORWARDING flag can be set for mrouting. */
573 in6_ifstat_inc(ifp, ifs6_out_request);
576 ia = (struct in6_ifaddr *)(rt->rt_ifa);
577 counter_u64_add(rt->rt_pksent, 1);
582 * The outgoing interface must be in the zone of source and
583 * destination addresses.
588 if (in6_setscope(&src0, origifp, &zone))
590 bzero(&src_sa, sizeof(src_sa));
591 src_sa.sin6_family = AF_INET6;
592 src_sa.sin6_len = sizeof(src_sa);
593 src_sa.sin6_addr = ip6->ip6_src;
594 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
598 if (in6_setscope(&dst0, origifp, &zone))
600 /* re-initialize to be sure */
601 bzero(&dst_sa, sizeof(dst_sa));
602 dst_sa.sin6_family = AF_INET6;
603 dst_sa.sin6_len = sizeof(dst_sa);
604 dst_sa.sin6_addr = ip6->ip6_dst;
605 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
609 /* We should use ia_ifp to support the case of
610 * sending packets to an address of our own.
612 if (ia != NULL && ia->ia_ifp)
615 /* scope check is done. */
619 IP6STAT_INC(ip6s_badscope);
620 in6_ifstat_inc(origifp, ifs6_out_discard);
622 error = EHOSTUNREACH; /* XXX */
626 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
627 if (opt && opt->ip6po_nextroute.ro_rt) {
629 * The nexthop is explicitly specified by the
630 * application. We assume the next hop is an IPv6
633 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
635 else if ((rt->rt_flags & RTF_GATEWAY))
636 dst = (struct sockaddr_in6 *)rt->rt_gateway;
639 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
640 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
642 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
643 in6_ifstat_inc(ifp, ifs6_out_mcast);
645 * Confirm that the outgoing interface supports multicast.
647 if (!(ifp->if_flags & IFF_MULTICAST)) {
648 IP6STAT_INC(ip6s_noroute);
649 in6_ifstat_inc(ifp, ifs6_out_discard);
653 if ((im6o == NULL && in6_mcast_loop) ||
654 (im6o && im6o->im6o_multicast_loop)) {
656 * Loop back multicast datagram if not expressly
657 * forbidden to do so, even if we have not joined
658 * the address; protocols will filter it later,
659 * thus deferring a hash lookup and lock acquisition
660 * at the expense of an m_copym().
662 ip6_mloopback(ifp, m);
665 * If we are acting as a multicast router, perform
666 * multicast forwarding as if the packet had just
667 * arrived on the interface to which we are about
668 * to send. The multicast forwarding function
669 * recursively calls this function, using the
670 * IPV6_FORWARDING flag to prevent infinite recursion.
672 * Multicasts that are looped back by ip6_mloopback(),
673 * above, will be forwarded by the ip6_input() routine,
676 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
678 * XXX: ip6_mforward expects that rcvif is NULL
679 * when it is called from the originating path.
680 * However, it may not always be the case.
682 m->m_pkthdr.rcvif = NULL;
683 if (ip6_mforward(ip6, ifp, m) != 0) {
690 * Multicasts with a hoplimit of zero may be looped back,
691 * above, but must not be transmitted on a network.
692 * Also, multicasts addressed to the loopback interface
693 * are not sent -- the above call to ip6_mloopback() will
694 * loop back a copy if this host actually belongs to the
695 * destination group on the loopback interface.
697 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
698 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
705 * Fill the outgoing inteface to tell the upper layer
706 * to increment per-interface statistics.
711 /* Determine path MTU. */
712 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
713 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
717 * The caller of this function may specify to use the minimum MTU
719 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
720 * setting. The logic is a bit complicated; by default, unicast
721 * packets will follow path MTU while multicast packets will be sent at
722 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
723 * including unicast ones will be sent at the minimum MTU. Multicast
724 * packets will always be sent at the minimum MTU unless
725 * IP6PO_MINMTU_DISABLE is explicitly specified.
726 * See RFC 3542 for more details.
728 if (mtu > IPV6_MMTU) {
729 if ((flags & IPV6_MINMTU))
731 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
733 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
735 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
741 * clear embedded scope identifiers if necessary.
742 * in6_clearscope will touch the addresses only when necessary.
744 in6_clearscope(&ip6->ip6_src);
745 in6_clearscope(&ip6->ip6_dst);
748 * If the outgoing packet contains a hop-by-hop options header,
749 * it must be examined and processed even by the source node.
750 * (RFC 2460, section 4.)
752 if (exthdrs.ip6e_hbh) {
753 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
754 u_int32_t dummy; /* XXX unused */
755 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
758 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
759 panic("ip6e_hbh is not contiguous");
762 * XXX: if we have to send an ICMPv6 error to the sender,
763 * we need the M_LOOP flag since icmp6_error() expects
764 * the IPv6 and the hop-by-hop options header are
765 * contiguous unless the flag is set.
767 m->m_flags |= M_LOOP;
768 m->m_pkthdr.rcvif = ifp;
769 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
770 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
771 &dummy, &plen) < 0) {
772 /* m was already freed at this point */
773 error = EINVAL;/* better error? */
776 m->m_flags &= ~M_LOOP; /* XXX */
777 m->m_pkthdr.rcvif = NULL;
780 /* Jump over all PFIL processing if hooks are not active. */
781 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
785 /* Run through list of hooks for output packets. */
786 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
787 if (error != 0 || m == NULL)
790 ip6 = mtod(m, struct ip6_hdr *);
793 /* See if destination IP address was changed by packet filter. */
794 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
795 m->m_flags |= M_SKIP_FIREWALL;
796 /* If destination is now ourself drop to ip6_input(). */
797 if (in6_localip(&ip6->ip6_dst)) {
798 m->m_flags |= M_FASTFWD_OURS;
799 if (m->m_pkthdr.rcvif == NULL)
800 m->m_pkthdr.rcvif = V_loif;
801 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
802 m->m_pkthdr.csum_flags |=
803 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
804 m->m_pkthdr.csum_data = 0xffff;
807 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
808 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
810 error = netisr_queue(NETISR_IPV6, m);
814 needfiblookup = 1; /* Redo the routing table lookup. */
816 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
820 /* See if fib was changed by packet filter. */
821 if (fibnum != M_GETFIB(m)) {
822 m->m_flags |= M_SKIP_FIREWALL;
823 fibnum = M_GETFIB(m);
827 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
833 /* See if local, if yes, send it to netisr. */
834 if (m->m_flags & M_FASTFWD_OURS) {
835 if (m->m_pkthdr.rcvif == NULL)
836 m->m_pkthdr.rcvif = V_loif;
837 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
838 m->m_pkthdr.csum_flags |=
839 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
840 m->m_pkthdr.csum_data = 0xffff;
843 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
844 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
846 error = netisr_queue(NETISR_IPV6, m);
849 /* Or forward to some other address? */
850 if ((m->m_flags & M_IP6_NEXTHOP) &&
851 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
852 dst = (struct sockaddr_in6 *)&ro->ro_dst;
853 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
854 m->m_flags |= M_SKIP_FIREWALL;
855 m->m_flags &= ~M_IP6_NEXTHOP;
856 m_tag_delete(m, fwd_tag);
862 * Send the packet to the outgoing interface.
863 * If necessary, do IPv6 fragmentation before sending.
865 * the logic here is rather complex:
866 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
867 * 1-a: send as is if tlen <= path mtu
868 * 1-b: fragment if tlen > path mtu
870 * 2: if user asks us not to fragment (dontfrag == 1)
871 * 2-a: send as is if tlen <= interface mtu
872 * 2-b: error if tlen > interface mtu
874 * 3: if we always need to attach fragment header (alwaysfrag == 1)
877 * 4: if dontfrag == 1 && alwaysfrag == 1
878 * error, as we cannot handle this conflicting request
880 sw_csum = m->m_pkthdr.csum_flags;
882 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
883 sw_csum &= ~ifp->if_hwassist;
887 * If we added extension headers, we will not do TSO and calculate the
888 * checksums ourselves for now.
889 * XXX-BZ Need a framework to know when the NIC can handle it, even
892 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
893 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
894 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
897 if (sw_csum & CSUM_SCTP_IPV6) {
898 sw_csum &= ~CSUM_SCTP_IPV6;
899 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
902 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
903 tlen = m->m_pkthdr.len;
905 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
909 if (dontfrag && alwaysfrag) { /* case 4 */
910 /* conflicting request - can't transmit */
914 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
916 * Even if the DONTFRAG option is specified, we cannot send the
917 * packet when the data length is larger than the MTU of the
918 * outgoing interface.
919 * Notify the error by sending IPV6_PATHMTU ancillary data if
920 * application wanted to know the MTU value. Also return an
921 * error code (this is not described in the API spec).
924 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
930 * transmit packet without fragmentation
932 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
933 struct in6_ifaddr *ia6;
935 ip6 = mtod(m, struct ip6_hdr *);
936 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
938 /* Record statistics for this interface address. */
939 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
940 counter_u64_add(ia6->ia_ifa.ifa_obytes,
942 ifa_free(&ia6->ia_ifa);
946 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
947 in_pcboutput_txrtlmt(inp, ifp, m);
948 /* stamp send tag on mbuf */
949 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
951 m->m_pkthdr.snd_tag = NULL;
954 error = nd6_output_ifp(ifp, origifp, m, dst,
957 /* check for route change */
959 in_pcboutput_eagain(inp);
965 * try to fragment the packet. case 1-b and 3
967 if (mtu < IPV6_MMTU) {
968 /* path MTU cannot be less than IPV6_MMTU */
970 in6_ifstat_inc(ifp, ifs6_out_fragfail);
972 } else if (ip6->ip6_plen == 0) {
973 /* jumbo payload cannot be fragmented */
975 in6_ifstat_inc(ifp, ifs6_out_fragfail);
981 * Too large for the destination or interface;
982 * fragment if possible.
983 * Must be able to put at least 8 bytes per fragment.
985 hlen = unfragpartlen;
986 if (mtu > IPV6_MAXPACKET)
987 mtu = IPV6_MAXPACKET;
989 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
992 in6_ifstat_inc(ifp, ifs6_out_fragfail);
997 * If the interface will not calculate checksums on
998 * fragmented packets, then do it here.
999 * XXX-BZ handle the hw offloading case. Need flags.
1001 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1002 in6_delayed_cksum(m, plen, hlen);
1003 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1006 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1007 sctp_delayed_cksum(m, hlen);
1008 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1012 * Change the next header field of the last header in the
1013 * unfragmentable part.
1015 if (exthdrs.ip6e_rthdr) {
1016 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1017 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1018 } else if (exthdrs.ip6e_dest1) {
1019 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1020 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1021 } else if (exthdrs.ip6e_hbh) {
1022 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1023 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1025 nextproto = ip6->ip6_nxt;
1026 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1030 * Loop through length of segment after first fragment,
1031 * make new header and copy data of each part and link onto
1035 id = htonl(ip6_randomid());
1036 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1039 in6_ifstat_inc(ifp, ifs6_out_fragok);
1043 * Remove leading garbages.
1049 for (m0 = m; m; m = m0) {
1053 /* Record statistics for this interface address. */
1055 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1056 counter_u64_add(ia->ia_ifa.ifa_obytes,
1061 if (inp->inp_flags2 & INP_RATE_LIMIT_CHANGED)
1062 in_pcboutput_txrtlmt(inp, ifp, m);
1063 /* stamp send tag on mbuf */
1064 m->m_pkthdr.snd_tag = inp->inp_snd_tag;
1066 m->m_pkthdr.snd_tag = NULL;
1069 error = nd6_output_ifp(ifp, origifp, m, dst,
1070 (struct route *)ro);
1072 /* check for route change */
1073 if (error == EAGAIN)
1074 in_pcboutput_eagain(inp);
1081 IP6STAT_INC(ip6s_fragmented);
1084 if (ro == &ip6route)
1089 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1090 m_freem(exthdrs.ip6e_dest1);
1091 m_freem(exthdrs.ip6e_rthdr);
1092 m_freem(exthdrs.ip6e_dest2);
1101 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1105 if (hlen > MCLBYTES)
1106 return (ENOBUFS); /* XXX */
1109 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1111 m = m_get(M_NOWAIT, MT_DATA);
1116 bcopy(hdr, mtod(m, caddr_t), hlen);
1123 * Insert jumbo payload option.
1126 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1132 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1135 * If there is no hop-by-hop options header, allocate new one.
1136 * If there is one but it doesn't have enough space to store the
1137 * jumbo payload option, allocate a cluster to store the whole options.
1138 * Otherwise, use it to store the options.
1140 if (exthdrs->ip6e_hbh == NULL) {
1141 mopt = m_get(M_NOWAIT, MT_DATA);
1144 mopt->m_len = JUMBOOPTLEN;
1145 optbuf = mtod(mopt, u_char *);
1146 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1147 exthdrs->ip6e_hbh = mopt;
1149 struct ip6_hbh *hbh;
1151 mopt = exthdrs->ip6e_hbh;
1152 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1155 * - exthdrs->ip6e_hbh is not referenced from places
1156 * other than exthdrs.
1157 * - exthdrs->ip6e_hbh is not an mbuf chain.
1159 int oldoptlen = mopt->m_len;
1163 * XXX: give up if the whole (new) hbh header does
1164 * not fit even in an mbuf cluster.
1166 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1170 * As a consequence, we must always prepare a cluster
1173 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1176 n->m_len = oldoptlen + JUMBOOPTLEN;
1177 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1179 optbuf = mtod(n, caddr_t) + oldoptlen;
1181 mopt = exthdrs->ip6e_hbh = n;
1183 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1184 mopt->m_len += JUMBOOPTLEN;
1186 optbuf[0] = IP6OPT_PADN;
1190 * Adjust the header length according to the pad and
1191 * the jumbo payload option.
1193 hbh = mtod(mopt, struct ip6_hbh *);
1194 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1197 /* fill in the option. */
1198 optbuf[2] = IP6OPT_JUMBO;
1200 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1201 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1203 /* finally, adjust the packet header length */
1204 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1211 * Insert fragment header and copy unfragmentable header portions.
1214 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1215 struct ip6_frag **frghdrp)
1217 struct mbuf *n, *mlast;
1219 if (hlen > sizeof(struct ip6_hdr)) {
1220 n = m_copym(m0, sizeof(struct ip6_hdr),
1221 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1228 /* Search for the last mbuf of unfragmentable part. */
1229 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1232 if (M_WRITABLE(mlast) &&
1233 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1234 /* use the trailing space of the last mbuf for the fragment hdr */
1235 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1237 mlast->m_len += sizeof(struct ip6_frag);
1238 m->m_pkthdr.len += sizeof(struct ip6_frag);
1240 /* allocate a new mbuf for the fragment header */
1243 mfrg = m_get(M_NOWAIT, MT_DATA);
1246 mfrg->m_len = sizeof(struct ip6_frag);
1247 *frghdrp = mtod(mfrg, struct ip6_frag *);
1248 mlast->m_next = mfrg;
1255 * Calculates IPv6 path mtu for destination @dst.
1256 * Resulting MTU is stored in @mtup.
1258 * Returns 0 on success.
1261 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1263 struct nhop6_extended nh6;
1264 struct in6_addr kdst;
1270 in6_splitscope(dst, &kdst, &scopeid);
1271 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1272 return (EHOSTUNREACH);
1277 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1278 fib6_free_nh_ext(fibnum, &nh6);
1284 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1285 * and cached data in @ro_pmtu.
1286 * MTU from (successful) route lookup is saved (along with dst)
1287 * inside @ro_pmtu to avoid subsequent route lookups after packet
1288 * filter processing.
1290 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1291 * Returns 0 on success.
1294 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1295 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1296 int *alwaysfragp, u_int fibnum, u_int proto)
1298 struct nhop6_basic nh6;
1299 struct in6_addr kdst;
1301 struct sockaddr_in6 *sa6_dst;
1308 * Here ro_pmtu has final destination address, while
1309 * ro might represent immediate destination.
1310 * Use ro_pmtu destination since mtu might differ.
1312 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1313 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1314 ro_pmtu->ro_mtu = 0;
1316 if (ro_pmtu->ro_mtu == 0) {
1317 bzero(sa6_dst, sizeof(*sa6_dst));
1318 sa6_dst->sin6_family = AF_INET6;
1319 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1320 sa6_dst->sin6_addr = *dst;
1322 in6_splitscope(dst, &kdst, &scopeid);
1323 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1325 ro_pmtu->ro_mtu = nh6.nh_mtu;
1328 mtu = ro_pmtu->ro_mtu;
1332 mtu = ro_pmtu->ro_rt->rt_mtu;
1334 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1338 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1339 * hostcache data for @dst.
1340 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1342 * Returns 0 on success.
1345 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1346 u_long *mtup, int *alwaysfragp, u_int proto)
1354 struct in_conninfo inc;
1356 bzero(&inc, sizeof(inc));
1357 inc.inc_flags |= INC_ISIPV6;
1358 inc.inc6_faddr = *dst;
1360 ifmtu = IN6_LINKMTU(ifp);
1362 /* TCP is known to react to pmtu changes so skip hc */
1363 if (proto != IPPROTO_TCP)
1364 mtu = tcp_hc_getmtu(&inc);
1367 mtu = min(mtu, rt_mtu);
1372 else if (mtu < IPV6_MMTU) {
1374 * RFC2460 section 5, last paragraph:
1375 * if we record ICMPv6 too big message with
1376 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1377 * or smaller, with framgent header attached.
1378 * (fragment header is needed regardless from the
1379 * packet size, for translators to identify packets)
1385 mtu = IN6_LINKMTU(ifp);
1387 error = EHOSTUNREACH; /* XXX */
1391 *alwaysfragp = alwaysfrag;
1396 * IP6 socket option processing.
1399 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1401 int optdatalen, uproto;
1403 struct inpcb *in6p = sotoinpcb(so);
1405 int level, op, optname;
1409 uint32_t rss_bucket;
1414 * Don't use more than a quarter of mbuf clusters. N.B.:
1415 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1416 * on LP64 architectures, so cast to u_long to avoid undefined
1417 * behavior. ILP32 architectures cannot have nmbclusters
1418 * large enough to overflow for other reasons.
1420 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1422 level = sopt->sopt_level;
1423 op = sopt->sopt_dir;
1424 optname = sopt->sopt_name;
1425 optlen = sopt->sopt_valsize;
1429 uproto = (int)so->so_proto->pr_protocol;
1431 if (level != IPPROTO_IPV6) {
1434 if (sopt->sopt_level == SOL_SOCKET &&
1435 sopt->sopt_dir == SOPT_SET) {
1436 switch (sopt->sopt_name) {
1439 if ((so->so_options & SO_REUSEADDR) != 0)
1440 in6p->inp_flags2 |= INP_REUSEADDR;
1442 in6p->inp_flags2 &= ~INP_REUSEADDR;
1448 if ((so->so_options & SO_REUSEPORT) != 0)
1449 in6p->inp_flags2 |= INP_REUSEPORT;
1451 in6p->inp_flags2 &= ~INP_REUSEPORT;
1457 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1461 case SO_MAX_PACING_RATE:
1464 in6p->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1475 } else { /* level == IPPROTO_IPV6 */
1480 case IPV6_2292PKTOPTIONS:
1481 #ifdef IPV6_PKTOPTIONS
1482 case IPV6_PKTOPTIONS:
1487 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1488 printf("ip6_ctloutput: mbuf limit hit\n");
1493 error = soopt_getm(sopt, &m); /* XXX */
1496 error = soopt_mcopyin(sopt, m); /* XXX */
1499 error = ip6_pcbopts(&in6p->in6p_outputopts,
1501 m_freem(m); /* XXX */
1506 * Use of some Hop-by-Hop options or some
1507 * Destination options, might require special
1508 * privilege. That is, normal applications
1509 * (without special privilege) might be forbidden
1510 * from setting certain options in outgoing packets,
1511 * and might never see certain options in received
1512 * packets. [RFC 2292 Section 6]
1513 * KAME specific note:
1514 * KAME prevents non-privileged users from sending or
1515 * receiving ANY hbh/dst options in order to avoid
1516 * overhead of parsing options in the kernel.
1518 case IPV6_RECVHOPOPTS:
1519 case IPV6_RECVDSTOPTS:
1520 case IPV6_RECVRTHDRDSTOPTS:
1522 error = priv_check(td,
1523 PRIV_NETINET_SETHDROPTS);
1528 case IPV6_UNICAST_HOPS:
1531 case IPV6_RECVPKTINFO:
1532 case IPV6_RECVHOPLIMIT:
1533 case IPV6_RECVRTHDR:
1534 case IPV6_RECVPATHMTU:
1535 case IPV6_RECVTCLASS:
1536 case IPV6_RECVFLOWID:
1538 case IPV6_RECVRSSBUCKETID:
1541 case IPV6_AUTOFLOWLABEL:
1542 case IPV6_ORIGDSTADDR:
1544 case IPV6_BINDMULTI:
1546 case IPV6_RSS_LISTEN_BUCKET:
1548 if (optname == IPV6_BINDANY && td != NULL) {
1549 error = priv_check(td,
1550 PRIV_NETINET_BINDANY);
1555 if (optlen != sizeof(int)) {
1559 error = sooptcopyin(sopt, &optval,
1560 sizeof optval, sizeof optval);
1565 case IPV6_UNICAST_HOPS:
1566 if (optval < -1 || optval >= 256)
1569 /* -1 = kernel default */
1570 in6p->in6p_hops = optval;
1571 if ((in6p->inp_vflag &
1573 in6p->inp_ip_ttl = optval;
1576 #define OPTSET(bit) \
1580 in6p->inp_flags |= (bit); \
1582 in6p->inp_flags &= ~(bit); \
1583 INP_WUNLOCK(in6p); \
1584 } while (/*CONSTCOND*/ 0)
1585 #define OPTSET2292(bit) \
1588 in6p->inp_flags |= IN6P_RFC2292; \
1590 in6p->inp_flags |= (bit); \
1592 in6p->inp_flags &= ~(bit); \
1593 INP_WUNLOCK(in6p); \
1594 } while (/*CONSTCOND*/ 0)
1595 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1597 #define OPTSET2(bit, val) do { \
1600 in6p->inp_flags2 |= bit; \
1602 in6p->inp_flags2 &= ~bit; \
1603 INP_WUNLOCK(in6p); \
1605 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1607 case IPV6_RECVPKTINFO:
1608 /* cannot mix with RFC2292 */
1609 if (OPTBIT(IN6P_RFC2292)) {
1613 OPTSET(IN6P_PKTINFO);
1618 struct ip6_pktopts **optp;
1620 /* cannot mix with RFC2292 */
1621 if (OPTBIT(IN6P_RFC2292)) {
1625 optp = &in6p->in6p_outputopts;
1626 error = ip6_pcbopt(IPV6_HOPLIMIT,
1627 (u_char *)&optval, sizeof(optval),
1628 optp, (td != NULL) ? td->td_ucred :
1633 case IPV6_RECVHOPLIMIT:
1634 /* cannot mix with RFC2292 */
1635 if (OPTBIT(IN6P_RFC2292)) {
1639 OPTSET(IN6P_HOPLIMIT);
1642 case IPV6_RECVHOPOPTS:
1643 /* cannot mix with RFC2292 */
1644 if (OPTBIT(IN6P_RFC2292)) {
1648 OPTSET(IN6P_HOPOPTS);
1651 case IPV6_RECVDSTOPTS:
1652 /* cannot mix with RFC2292 */
1653 if (OPTBIT(IN6P_RFC2292)) {
1657 OPTSET(IN6P_DSTOPTS);
1660 case IPV6_RECVRTHDRDSTOPTS:
1661 /* cannot mix with RFC2292 */
1662 if (OPTBIT(IN6P_RFC2292)) {
1666 OPTSET(IN6P_RTHDRDSTOPTS);
1669 case IPV6_RECVRTHDR:
1670 /* cannot mix with RFC2292 */
1671 if (OPTBIT(IN6P_RFC2292)) {
1678 case IPV6_RECVPATHMTU:
1680 * We ignore this option for TCP
1682 * (RFC3542 leaves this case
1685 if (uproto != IPPROTO_TCP)
1689 case IPV6_RECVFLOWID:
1690 OPTSET2(INP_RECVFLOWID, optval);
1694 case IPV6_RECVRSSBUCKETID:
1695 OPTSET2(INP_RECVRSSBUCKETID, optval);
1701 * make setsockopt(IPV6_V6ONLY)
1702 * available only prior to bind(2).
1703 * see ipng mailing list, Jun 22 2001.
1705 if (in6p->inp_lport ||
1706 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1710 OPTSET(IN6P_IPV6_V6ONLY);
1712 in6p->inp_vflag &= ~INP_IPV4;
1714 in6p->inp_vflag |= INP_IPV4;
1716 case IPV6_RECVTCLASS:
1717 /* cannot mix with RFC2292 XXX */
1718 if (OPTBIT(IN6P_RFC2292)) {
1722 OPTSET(IN6P_TCLASS);
1724 case IPV6_AUTOFLOWLABEL:
1725 OPTSET(IN6P_AUTOFLOWLABEL);
1728 case IPV6_ORIGDSTADDR:
1729 OPTSET2(INP_ORIGDSTADDR, optval);
1732 OPTSET(INP_BINDANY);
1735 case IPV6_BINDMULTI:
1736 OPTSET2(INP_BINDMULTI, optval);
1739 case IPV6_RSS_LISTEN_BUCKET:
1740 if ((optval >= 0) &&
1741 (optval < rss_getnumbuckets())) {
1742 in6p->inp_rss_listen_bucket = optval;
1743 OPTSET2(INP_RSS_BUCKET_SET, 1);
1754 case IPV6_USE_MIN_MTU:
1755 case IPV6_PREFER_TEMPADDR:
1756 if (optlen != sizeof(optval)) {
1760 error = sooptcopyin(sopt, &optval,
1761 sizeof optval, sizeof optval);
1765 struct ip6_pktopts **optp;
1766 optp = &in6p->in6p_outputopts;
1767 error = ip6_pcbopt(optname,
1768 (u_char *)&optval, sizeof(optval),
1769 optp, (td != NULL) ? td->td_ucred :
1774 case IPV6_2292PKTINFO:
1775 case IPV6_2292HOPLIMIT:
1776 case IPV6_2292HOPOPTS:
1777 case IPV6_2292DSTOPTS:
1778 case IPV6_2292RTHDR:
1780 if (optlen != sizeof(int)) {
1784 error = sooptcopyin(sopt, &optval,
1785 sizeof optval, sizeof optval);
1789 case IPV6_2292PKTINFO:
1790 OPTSET2292(IN6P_PKTINFO);
1792 case IPV6_2292HOPLIMIT:
1793 OPTSET2292(IN6P_HOPLIMIT);
1795 case IPV6_2292HOPOPTS:
1797 * Check super-user privilege.
1798 * See comments for IPV6_RECVHOPOPTS.
1801 error = priv_check(td,
1802 PRIV_NETINET_SETHDROPTS);
1806 OPTSET2292(IN6P_HOPOPTS);
1808 case IPV6_2292DSTOPTS:
1810 error = priv_check(td,
1811 PRIV_NETINET_SETHDROPTS);
1815 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1817 case IPV6_2292RTHDR:
1818 OPTSET2292(IN6P_RTHDR);
1826 case IPV6_RTHDRDSTOPTS:
1829 /* new advanced API (RFC3542) */
1831 u_char optbuf_storage[MCLBYTES];
1833 struct ip6_pktopts **optp;
1835 /* cannot mix with RFC2292 */
1836 if (OPTBIT(IN6P_RFC2292)) {
1842 * We only ensure valsize is not too large
1843 * here. Further validation will be done
1846 error = sooptcopyin(sopt, optbuf_storage,
1847 sizeof(optbuf_storage), 0);
1850 optlen = sopt->sopt_valsize;
1851 optbuf = optbuf_storage;
1852 optp = &in6p->in6p_outputopts;
1853 error = ip6_pcbopt(optname, optbuf, optlen,
1854 optp, (td != NULL) ? td->td_ucred : NULL,
1860 case IPV6_MULTICAST_IF:
1861 case IPV6_MULTICAST_HOPS:
1862 case IPV6_MULTICAST_LOOP:
1863 case IPV6_JOIN_GROUP:
1864 case IPV6_LEAVE_GROUP:
1866 case MCAST_BLOCK_SOURCE:
1867 case MCAST_UNBLOCK_SOURCE:
1868 case MCAST_JOIN_GROUP:
1869 case MCAST_LEAVE_GROUP:
1870 case MCAST_JOIN_SOURCE_GROUP:
1871 case MCAST_LEAVE_SOURCE_GROUP:
1872 error = ip6_setmoptions(in6p, sopt);
1875 case IPV6_PORTRANGE:
1876 error = sooptcopyin(sopt, &optval,
1877 sizeof optval, sizeof optval);
1883 case IPV6_PORTRANGE_DEFAULT:
1884 in6p->inp_flags &= ~(INP_LOWPORT);
1885 in6p->inp_flags &= ~(INP_HIGHPORT);
1888 case IPV6_PORTRANGE_HIGH:
1889 in6p->inp_flags &= ~(INP_LOWPORT);
1890 in6p->inp_flags |= INP_HIGHPORT;
1893 case IPV6_PORTRANGE_LOW:
1894 in6p->inp_flags &= ~(INP_HIGHPORT);
1895 in6p->inp_flags |= INP_LOWPORT;
1905 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1906 case IPV6_IPSEC_POLICY:
1907 if (IPSEC_ENABLED(ipv6)) {
1908 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
1915 error = ENOPROTOOPT;
1923 case IPV6_2292PKTOPTIONS:
1924 #ifdef IPV6_PKTOPTIONS
1925 case IPV6_PKTOPTIONS:
1928 * RFC3542 (effectively) deprecated the
1929 * semantics of the 2292-style pktoptions.
1930 * Since it was not reliable in nature (i.e.,
1931 * applications had to expect the lack of some
1932 * information after all), it would make sense
1933 * to simplify this part by always returning
1936 sopt->sopt_valsize = 0;
1939 case IPV6_RECVHOPOPTS:
1940 case IPV6_RECVDSTOPTS:
1941 case IPV6_RECVRTHDRDSTOPTS:
1942 case IPV6_UNICAST_HOPS:
1943 case IPV6_RECVPKTINFO:
1944 case IPV6_RECVHOPLIMIT:
1945 case IPV6_RECVRTHDR:
1946 case IPV6_RECVPATHMTU:
1949 case IPV6_PORTRANGE:
1950 case IPV6_RECVTCLASS:
1951 case IPV6_AUTOFLOWLABEL:
1955 case IPV6_RECVFLOWID:
1957 case IPV6_RSSBUCKETID:
1958 case IPV6_RECVRSSBUCKETID:
1960 case IPV6_BINDMULTI:
1963 case IPV6_RECVHOPOPTS:
1964 optval = OPTBIT(IN6P_HOPOPTS);
1967 case IPV6_RECVDSTOPTS:
1968 optval = OPTBIT(IN6P_DSTOPTS);
1971 case IPV6_RECVRTHDRDSTOPTS:
1972 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1975 case IPV6_UNICAST_HOPS:
1976 optval = in6p->in6p_hops;
1979 case IPV6_RECVPKTINFO:
1980 optval = OPTBIT(IN6P_PKTINFO);
1983 case IPV6_RECVHOPLIMIT:
1984 optval = OPTBIT(IN6P_HOPLIMIT);
1987 case IPV6_RECVRTHDR:
1988 optval = OPTBIT(IN6P_RTHDR);
1991 case IPV6_RECVPATHMTU:
1992 optval = OPTBIT(IN6P_MTU);
1996 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1999 case IPV6_PORTRANGE:
2002 flags = in6p->inp_flags;
2003 if (flags & INP_HIGHPORT)
2004 optval = IPV6_PORTRANGE_HIGH;
2005 else if (flags & INP_LOWPORT)
2006 optval = IPV6_PORTRANGE_LOW;
2011 case IPV6_RECVTCLASS:
2012 optval = OPTBIT(IN6P_TCLASS);
2015 case IPV6_AUTOFLOWLABEL:
2016 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2019 case IPV6_ORIGDSTADDR:
2020 optval = OPTBIT2(INP_ORIGDSTADDR);
2024 optval = OPTBIT(INP_BINDANY);
2028 optval = in6p->inp_flowid;
2032 optval = in6p->inp_flowtype;
2035 case IPV6_RECVFLOWID:
2036 optval = OPTBIT2(INP_RECVFLOWID);
2039 case IPV6_RSSBUCKETID:
2041 rss_hash2bucket(in6p->inp_flowid,
2045 optval = rss_bucket;
2050 case IPV6_RECVRSSBUCKETID:
2051 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2055 case IPV6_BINDMULTI:
2056 optval = OPTBIT2(INP_BINDMULTI);
2062 error = sooptcopyout(sopt, &optval,
2069 struct ip6_mtuinfo mtuinfo;
2071 if (!(so->so_state & SS_ISCONNECTED))
2074 * XXX: we dot not consider the case of source
2075 * routing, or optional information to specify
2076 * the outgoing interface.
2078 error = ip6_getpmtu_ctl(so->so_fibnum,
2079 &in6p->in6p_faddr, &pmtu);
2082 if (pmtu > IPV6_MAXPACKET)
2083 pmtu = IPV6_MAXPACKET;
2085 bzero(&mtuinfo, sizeof(mtuinfo));
2086 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2087 optdata = (void *)&mtuinfo;
2088 optdatalen = sizeof(mtuinfo);
2089 error = sooptcopyout(sopt, optdata,
2094 case IPV6_2292PKTINFO:
2095 case IPV6_2292HOPLIMIT:
2096 case IPV6_2292HOPOPTS:
2097 case IPV6_2292RTHDR:
2098 case IPV6_2292DSTOPTS:
2100 case IPV6_2292PKTINFO:
2101 optval = OPTBIT(IN6P_PKTINFO);
2103 case IPV6_2292HOPLIMIT:
2104 optval = OPTBIT(IN6P_HOPLIMIT);
2106 case IPV6_2292HOPOPTS:
2107 optval = OPTBIT(IN6P_HOPOPTS);
2109 case IPV6_2292RTHDR:
2110 optval = OPTBIT(IN6P_RTHDR);
2112 case IPV6_2292DSTOPTS:
2113 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2116 error = sooptcopyout(sopt, &optval,
2123 case IPV6_RTHDRDSTOPTS:
2127 case IPV6_USE_MIN_MTU:
2128 case IPV6_PREFER_TEMPADDR:
2129 error = ip6_getpcbopt(in6p->in6p_outputopts,
2133 case IPV6_MULTICAST_IF:
2134 case IPV6_MULTICAST_HOPS:
2135 case IPV6_MULTICAST_LOOP:
2137 error = ip6_getmoptions(in6p, sopt);
2140 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2141 case IPV6_IPSEC_POLICY:
2142 if (IPSEC_ENABLED(ipv6)) {
2143 error = IPSEC_PCBCTL(ipv6, in6p, sopt);
2149 error = ENOPROTOOPT;
2159 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2161 int error = 0, optval, optlen;
2162 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2163 struct inpcb *in6p = sotoinpcb(so);
2164 int level, op, optname;
2166 level = sopt->sopt_level;
2167 op = sopt->sopt_dir;
2168 optname = sopt->sopt_name;
2169 optlen = sopt->sopt_valsize;
2171 if (level != IPPROTO_IPV6) {
2178 * For ICMPv6 sockets, no modification allowed for checksum
2179 * offset, permit "no change" values to help existing apps.
2181 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2182 * for an ICMPv6 socket will fail."
2183 * The current behavior does not meet RFC3542.
2187 if (optlen != sizeof(int)) {
2191 error = sooptcopyin(sopt, &optval, sizeof(optval),
2195 if ((optval % 2) != 0) {
2196 /* the API assumes even offset values */
2198 } else if (so->so_proto->pr_protocol ==
2200 if (optval != icmp6off)
2203 in6p->in6p_cksum = optval;
2207 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2210 optval = in6p->in6p_cksum;
2212 error = sooptcopyout(sopt, &optval, sizeof(optval));
2222 error = ENOPROTOOPT;
2230 * Set up IP6 options in pcb for insertion in output packets or
2231 * specifying behavior of outgoing packets.
2234 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2235 struct socket *so, struct sockopt *sopt)
2237 struct ip6_pktopts *opt = *pktopt;
2239 struct thread *td = sopt->sopt_td;
2241 /* turn off any old options. */
2244 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2245 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2246 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2247 printf("ip6_pcbopts: all specified options are cleared.\n");
2249 ip6_clearpktopts(opt, -1);
2251 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2254 if (!m || m->m_len == 0) {
2256 * Only turning off any previous options, regardless of
2257 * whether the opt is just created or given.
2259 free(opt, M_IP6OPT);
2263 /* set options specified by user. */
2264 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2265 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2266 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2267 free(opt, M_IP6OPT);
2275 * initialize ip6_pktopts. beware that there are non-zero default values in
2279 ip6_initpktopts(struct ip6_pktopts *opt)
2282 bzero(opt, sizeof(*opt));
2283 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2284 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2285 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2286 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2290 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2291 struct ucred *cred, int uproto)
2293 struct ip6_pktopts *opt;
2295 if (*pktopt == NULL) {
2296 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2298 ip6_initpktopts(*pktopt);
2302 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2306 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2308 void *optdata = NULL;
2310 struct ip6_ext *ip6e;
2312 struct in6_pktinfo null_pktinfo;
2313 int deftclass = 0, on;
2314 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2315 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2319 optdata = (void *)&null_pktinfo;
2320 if (pktopt && pktopt->ip6po_pktinfo) {
2321 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2322 sizeof(null_pktinfo));
2323 in6_clearscope(&null_pktinfo.ipi6_addr);
2325 /* XXX: we don't have to do this every time... */
2326 bzero(&null_pktinfo, sizeof(null_pktinfo));
2328 optdatalen = sizeof(struct in6_pktinfo);
2331 if (pktopt && pktopt->ip6po_tclass >= 0)
2332 optdata = (void *)&pktopt->ip6po_tclass;
2334 optdata = (void *)&deftclass;
2335 optdatalen = sizeof(int);
2338 if (pktopt && pktopt->ip6po_hbh) {
2339 optdata = (void *)pktopt->ip6po_hbh;
2340 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2341 optdatalen = (ip6e->ip6e_len + 1) << 3;
2345 if (pktopt && pktopt->ip6po_rthdr) {
2346 optdata = (void *)pktopt->ip6po_rthdr;
2347 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2348 optdatalen = (ip6e->ip6e_len + 1) << 3;
2351 case IPV6_RTHDRDSTOPTS:
2352 if (pktopt && pktopt->ip6po_dest1) {
2353 optdata = (void *)pktopt->ip6po_dest1;
2354 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2355 optdatalen = (ip6e->ip6e_len + 1) << 3;
2359 if (pktopt && pktopt->ip6po_dest2) {
2360 optdata = (void *)pktopt->ip6po_dest2;
2361 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2362 optdatalen = (ip6e->ip6e_len + 1) << 3;
2366 if (pktopt && pktopt->ip6po_nexthop) {
2367 optdata = (void *)pktopt->ip6po_nexthop;
2368 optdatalen = pktopt->ip6po_nexthop->sa_len;
2371 case IPV6_USE_MIN_MTU:
2373 optdata = (void *)&pktopt->ip6po_minmtu;
2375 optdata = (void *)&defminmtu;
2376 optdatalen = sizeof(int);
2379 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2383 optdata = (void *)&on;
2384 optdatalen = sizeof(on);
2386 case IPV6_PREFER_TEMPADDR:
2388 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2390 optdata = (void *)&defpreftemp;
2391 optdatalen = sizeof(int);
2393 default: /* should not happen */
2395 panic("ip6_getpcbopt: unexpected option\n");
2397 return (ENOPROTOOPT);
2400 error = sooptcopyout(sopt, optdata, optdatalen);
2406 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2411 if (optname == -1 || optname == IPV6_PKTINFO) {
2412 if (pktopt->ip6po_pktinfo)
2413 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2414 pktopt->ip6po_pktinfo = NULL;
2416 if (optname == -1 || optname == IPV6_HOPLIMIT)
2417 pktopt->ip6po_hlim = -1;
2418 if (optname == -1 || optname == IPV6_TCLASS)
2419 pktopt->ip6po_tclass = -1;
2420 if (optname == -1 || optname == IPV6_NEXTHOP) {
2421 if (pktopt->ip6po_nextroute.ro_rt) {
2422 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2423 pktopt->ip6po_nextroute.ro_rt = NULL;
2425 if (pktopt->ip6po_nexthop)
2426 free(pktopt->ip6po_nexthop, M_IP6OPT);
2427 pktopt->ip6po_nexthop = NULL;
2429 if (optname == -1 || optname == IPV6_HOPOPTS) {
2430 if (pktopt->ip6po_hbh)
2431 free(pktopt->ip6po_hbh, M_IP6OPT);
2432 pktopt->ip6po_hbh = NULL;
2434 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2435 if (pktopt->ip6po_dest1)
2436 free(pktopt->ip6po_dest1, M_IP6OPT);
2437 pktopt->ip6po_dest1 = NULL;
2439 if (optname == -1 || optname == IPV6_RTHDR) {
2440 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2441 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2442 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2443 if (pktopt->ip6po_route.ro_rt) {
2444 RTFREE(pktopt->ip6po_route.ro_rt);
2445 pktopt->ip6po_route.ro_rt = NULL;
2448 if (optname == -1 || optname == IPV6_DSTOPTS) {
2449 if (pktopt->ip6po_dest2)
2450 free(pktopt->ip6po_dest2, M_IP6OPT);
2451 pktopt->ip6po_dest2 = NULL;
2455 #define PKTOPT_EXTHDRCPY(type) \
2458 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2459 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2460 if (dst->type == NULL)\
2462 bcopy(src->type, dst->type, hlen);\
2464 } while (/*CONSTCOND*/ 0)
2467 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2469 if (dst == NULL || src == NULL) {
2470 printf("ip6_clearpktopts: invalid argument\n");
2474 dst->ip6po_hlim = src->ip6po_hlim;
2475 dst->ip6po_tclass = src->ip6po_tclass;
2476 dst->ip6po_flags = src->ip6po_flags;
2477 dst->ip6po_minmtu = src->ip6po_minmtu;
2478 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2479 if (src->ip6po_pktinfo) {
2480 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2482 if (dst->ip6po_pktinfo == NULL)
2484 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2486 if (src->ip6po_nexthop) {
2487 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2489 if (dst->ip6po_nexthop == NULL)
2491 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2492 src->ip6po_nexthop->sa_len);
2494 PKTOPT_EXTHDRCPY(ip6po_hbh);
2495 PKTOPT_EXTHDRCPY(ip6po_dest1);
2496 PKTOPT_EXTHDRCPY(ip6po_dest2);
2497 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2501 ip6_clearpktopts(dst, -1);
2504 #undef PKTOPT_EXTHDRCPY
2506 struct ip6_pktopts *
2507 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2510 struct ip6_pktopts *dst;
2512 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2515 ip6_initpktopts(dst);
2517 if ((error = copypktopts(dst, src, canwait)) != 0) {
2518 free(dst, M_IP6OPT);
2526 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2531 ip6_clearpktopts(pktopt, -1);
2533 free(pktopt, M_IP6OPT);
2537 * Set IPv6 outgoing packet options based on advanced API.
2540 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2541 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2543 struct cmsghdr *cm = NULL;
2545 if (control == NULL || opt == NULL)
2548 ip6_initpktopts(opt);
2553 * If stickyopt is provided, make a local copy of the options
2554 * for this particular packet, then override them by ancillary
2556 * XXX: copypktopts() does not copy the cached route to a next
2557 * hop (if any). This is not very good in terms of efficiency,
2558 * but we can allow this since this option should be rarely
2561 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2566 * XXX: Currently, we assume all the optional information is stored
2569 if (control->m_next)
2572 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2573 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2576 if (control->m_len < CMSG_LEN(0))
2579 cm = mtod(control, struct cmsghdr *);
2580 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2582 if (cm->cmsg_level != IPPROTO_IPV6)
2585 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2586 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2595 * Set a particular packet option, as a sticky option or an ancillary data
2596 * item. "len" can be 0 only when it's a sticky option.
2597 * We have 4 cases of combination of "sticky" and "cmsg":
2598 * "sticky=0, cmsg=0": impossible
2599 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2600 * "sticky=1, cmsg=0": RFC3542 socket option
2601 * "sticky=1, cmsg=1": RFC2292 socket option
2604 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2605 struct ucred *cred, int sticky, int cmsg, int uproto)
2607 int minmtupolicy, preftemp;
2610 if (!sticky && !cmsg) {
2612 printf("ip6_setpktopt: impossible case\n");
2618 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2619 * not be specified in the context of RFC3542. Conversely,
2620 * RFC3542 types should not be specified in the context of RFC2292.
2624 case IPV6_2292PKTINFO:
2625 case IPV6_2292HOPLIMIT:
2626 case IPV6_2292NEXTHOP:
2627 case IPV6_2292HOPOPTS:
2628 case IPV6_2292DSTOPTS:
2629 case IPV6_2292RTHDR:
2630 case IPV6_2292PKTOPTIONS:
2631 return (ENOPROTOOPT);
2634 if (sticky && cmsg) {
2641 case IPV6_RTHDRDSTOPTS:
2643 case IPV6_USE_MIN_MTU:
2646 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2647 return (ENOPROTOOPT);
2652 case IPV6_2292PKTINFO:
2655 struct ifnet *ifp = NULL;
2656 struct in6_pktinfo *pktinfo;
2658 if (len != sizeof(struct in6_pktinfo))
2661 pktinfo = (struct in6_pktinfo *)buf;
2664 * An application can clear any sticky IPV6_PKTINFO option by
2665 * doing a "regular" setsockopt with ipi6_addr being
2666 * in6addr_any and ipi6_ifindex being zero.
2667 * [RFC 3542, Section 6]
2669 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2670 pktinfo->ipi6_ifindex == 0 &&
2671 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2672 ip6_clearpktopts(opt, optname);
2676 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2677 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2680 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2682 /* validate the interface index if specified. */
2683 if (pktinfo->ipi6_ifindex > V_if_index)
2685 if (pktinfo->ipi6_ifindex) {
2686 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2690 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2691 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2695 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2696 struct in6_ifaddr *ia;
2698 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2699 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2701 return (EADDRNOTAVAIL);
2702 ifa_free(&ia->ia_ifa);
2705 * We store the address anyway, and let in6_selectsrc()
2706 * validate the specified address. This is because ipi6_addr
2707 * may not have enough information about its scope zone, and
2708 * we may need additional information (such as outgoing
2709 * interface or the scope zone of a destination address) to
2710 * disambiguate the scope.
2711 * XXX: the delay of the validation may confuse the
2712 * application when it is used as a sticky option.
2714 if (opt->ip6po_pktinfo == NULL) {
2715 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2716 M_IP6OPT, M_NOWAIT);
2717 if (opt->ip6po_pktinfo == NULL)
2720 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2724 case IPV6_2292HOPLIMIT:
2730 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2731 * to simplify the ordering among hoplimit options.
2733 if (optname == IPV6_HOPLIMIT && sticky)
2734 return (ENOPROTOOPT);
2736 if (len != sizeof(int))
2739 if (*hlimp < -1 || *hlimp > 255)
2742 opt->ip6po_hlim = *hlimp;
2750 if (len != sizeof(int))
2752 tclass = *(int *)buf;
2753 if (tclass < -1 || tclass > 255)
2756 opt->ip6po_tclass = tclass;
2760 case IPV6_2292NEXTHOP:
2763 error = priv_check_cred(cred,
2764 PRIV_NETINET_SETHDROPTS, 0);
2769 if (len == 0) { /* just remove the option */
2770 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2774 /* check if cmsg_len is large enough for sa_len */
2775 if (len < sizeof(struct sockaddr) || len < *buf)
2778 switch (((struct sockaddr *)buf)->sa_family) {
2781 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2784 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2787 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2788 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2791 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2797 case AF_LINK: /* should eventually be supported */
2799 return (EAFNOSUPPORT);
2802 /* turn off the previous option, then set the new option. */
2803 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2804 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2805 if (opt->ip6po_nexthop == NULL)
2807 bcopy(buf, opt->ip6po_nexthop, *buf);
2810 case IPV6_2292HOPOPTS:
2813 struct ip6_hbh *hbh;
2817 * XXX: We don't allow a non-privileged user to set ANY HbH
2818 * options, since per-option restriction has too much
2822 error = priv_check_cred(cred,
2823 PRIV_NETINET_SETHDROPTS, 0);
2829 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2830 break; /* just remove the option */
2833 /* message length validation */
2834 if (len < sizeof(struct ip6_hbh))
2836 hbh = (struct ip6_hbh *)buf;
2837 hbhlen = (hbh->ip6h_len + 1) << 3;
2841 /* turn off the previous option, then set the new option. */
2842 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2843 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2844 if (opt->ip6po_hbh == NULL)
2846 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2851 case IPV6_2292DSTOPTS:
2853 case IPV6_RTHDRDSTOPTS:
2855 struct ip6_dest *dest, **newdest = NULL;
2858 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2859 error = priv_check_cred(cred,
2860 PRIV_NETINET_SETHDROPTS, 0);
2866 ip6_clearpktopts(opt, optname);
2867 break; /* just remove the option */
2870 /* message length validation */
2871 if (len < sizeof(struct ip6_dest))
2873 dest = (struct ip6_dest *)buf;
2874 destlen = (dest->ip6d_len + 1) << 3;
2879 * Determine the position that the destination options header
2880 * should be inserted; before or after the routing header.
2883 case IPV6_2292DSTOPTS:
2885 * The old advacned API is ambiguous on this point.
2886 * Our approach is to determine the position based
2887 * according to the existence of a routing header.
2888 * Note, however, that this depends on the order of the
2889 * extension headers in the ancillary data; the 1st
2890 * part of the destination options header must appear
2891 * before the routing header in the ancillary data,
2893 * RFC3542 solved the ambiguity by introducing
2894 * separate ancillary data or option types.
2896 if (opt->ip6po_rthdr == NULL)
2897 newdest = &opt->ip6po_dest1;
2899 newdest = &opt->ip6po_dest2;
2901 case IPV6_RTHDRDSTOPTS:
2902 newdest = &opt->ip6po_dest1;
2905 newdest = &opt->ip6po_dest2;
2909 /* turn off the previous option, then set the new option. */
2910 ip6_clearpktopts(opt, optname);
2911 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2912 if (*newdest == NULL)
2914 bcopy(dest, *newdest, destlen);
2919 case IPV6_2292RTHDR:
2922 struct ip6_rthdr *rth;
2926 ip6_clearpktopts(opt, IPV6_RTHDR);
2927 break; /* just remove the option */
2930 /* message length validation */
2931 if (len < sizeof(struct ip6_rthdr))
2933 rth = (struct ip6_rthdr *)buf;
2934 rthlen = (rth->ip6r_len + 1) << 3;
2938 switch (rth->ip6r_type) {
2939 case IPV6_RTHDR_TYPE_0:
2940 if (rth->ip6r_len == 0) /* must contain one addr */
2942 if (rth->ip6r_len % 2) /* length must be even */
2944 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2948 return (EINVAL); /* not supported */
2951 /* turn off the previous option */
2952 ip6_clearpktopts(opt, IPV6_RTHDR);
2953 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2954 if (opt->ip6po_rthdr == NULL)
2956 bcopy(rth, opt->ip6po_rthdr, rthlen);
2961 case IPV6_USE_MIN_MTU:
2962 if (len != sizeof(int))
2964 minmtupolicy = *(int *)buf;
2965 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2966 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2967 minmtupolicy != IP6PO_MINMTU_ALL) {
2970 opt->ip6po_minmtu = minmtupolicy;
2974 if (len != sizeof(int))
2977 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2979 * we ignore this option for TCP sockets.
2980 * (RFC3542 leaves this case unspecified.)
2982 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2984 opt->ip6po_flags |= IP6PO_DONTFRAG;
2987 case IPV6_PREFER_TEMPADDR:
2988 if (len != sizeof(int))
2990 preftemp = *(int *)buf;
2991 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2992 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2993 preftemp != IP6PO_TEMPADDR_PREFER) {
2996 opt->ip6po_prefer_tempaddr = preftemp;
3000 return (ENOPROTOOPT);
3001 } /* end of switch */
3007 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3008 * packet to the input queue of a specified interface. Note that this
3009 * calls the output routine of the loopback "driver", but with an interface
3010 * pointer that might NOT be &loif -- easier than replicating that code here.
3013 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3016 struct ip6_hdr *ip6;
3018 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3023 * Make sure to deep-copy IPv6 header portion in case the data
3024 * is in an mbuf cluster, so that we can safely override the IPv6
3025 * header portion later.
3027 if (!M_WRITABLE(copym) ||
3028 copym->m_len < sizeof(struct ip6_hdr)) {
3029 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3033 ip6 = mtod(copym, struct ip6_hdr *);
3035 * clear embedded scope identifiers if necessary.
3036 * in6_clearscope will touch the addresses only when necessary.
3038 in6_clearscope(&ip6->ip6_src);
3039 in6_clearscope(&ip6->ip6_dst);
3040 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3041 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3043 copym->m_pkthdr.csum_data = 0xffff;
3045 if_simloop(ifp, copym, AF_INET6, 0);
3049 * Chop IPv6 header off from the payload.
3052 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3055 struct ip6_hdr *ip6;
3057 ip6 = mtod(m, struct ip6_hdr *);
3058 if (m->m_len > sizeof(*ip6)) {
3059 mh = m_gethdr(M_NOWAIT, MT_DATA);
3064 m_move_pkthdr(mh, m);
3065 M_ALIGN(mh, sizeof(*ip6));
3066 m->m_len -= sizeof(*ip6);
3067 m->m_data += sizeof(*ip6);
3070 m->m_len = sizeof(*ip6);
3071 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3073 exthdrs->ip6e_ip6 = m;
3078 * Compute IPv6 extension header length.
3081 ip6_optlen(struct inpcb *in6p)
3085 if (!in6p->in6p_outputopts)
3090 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3092 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3093 if (in6p->in6p_outputopts->ip6po_rthdr)
3094 /* dest1 is valid with rthdr only */
3095 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3096 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3097 len += elen(in6p->in6p_outputopts->ip6po_dest2);