2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include "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/if_llatbl.h>
91 #include <net/netisr.h>
92 #include <net/route.h>
94 #include <net/rss_config.h>
97 #include <netinet/in.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
100 #include <netinet6/in6_fib.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet6/nd6.h>
108 #include <netinet6/in6_rss.h>
111 #include <netipsec/ipsec.h>
112 #include <netipsec/ipsec6.h>
113 #include <netipsec/key.h>
114 #include <netinet6/ip6_ipsec.h>
117 #include <netinet/sctp.h>
118 #include <netinet/sctp_crc32.h>
121 #include <netinet6/ip6protosw.h>
122 #include <netinet6/scope6_var.h>
125 #include <net/flowtable.h>
128 extern int in6_mcast_loop;
131 struct mbuf *ip6e_ip6;
132 struct mbuf *ip6e_hbh;
133 struct mbuf *ip6e_dest1;
134 struct mbuf *ip6e_rthdr;
135 struct mbuf *ip6e_dest2;
138 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
140 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
141 struct ucred *, int);
142 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
143 struct socket *, struct sockopt *);
144 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
145 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
146 struct ucred *, int, int, int);
148 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
149 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
151 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
152 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
153 static int ip6_getpmtu(struct route_in6 *, int,
154 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
156 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
157 u_long *, int *, u_int);
158 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
159 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
163 * Make an extension header from option data. hp is the source, and
164 * mp is the destination.
166 #define MAKE_EXTHDR(hp, mp) \
169 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
170 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
171 ((eh)->ip6e_len + 1) << 3); \
175 } while (/*CONSTCOND*/ 0)
178 * Form a chain of extension headers.
179 * m is the extension header mbuf
180 * mp is the previous mbuf in the chain
181 * p is the next header
182 * i is the type of option.
184 #define MAKE_CHAIN(m, mp, p, i)\
188 panic("assumption failed: hdr not split"); \
189 *mtod((m), u_char *) = *(p);\
191 p = mtod((m), u_char *);\
192 (m)->m_next = (mp)->m_next;\
196 } while (/*CONSTCOND*/ 0)
199 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
203 csum = in_cksum_skip(m, offset + plen, offset);
204 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
206 offset += m->m_pkthdr.csum_data; /* checksum offset */
208 if (offset + sizeof(u_short) > m->m_len) {
209 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
210 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
211 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
213 * XXX this should not happen, but if it does, the correct
214 * behavior may be to insert the checksum in the appropriate
215 * next mbuf in the chain.
219 *(u_short *)(m->m_data + offset) = csum;
223 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
224 int mtu, uint32_t id)
226 struct mbuf *m, **mnext, *m_frgpart;
227 struct ip6_hdr *ip6, *mhip6;
228 struct ip6_frag *ip6f;
231 int tlen = m0->m_pkthdr.len;
234 ip6 = mtod(m, struct ip6_hdr *);
235 mnext = &m->m_nextpkt;
237 for (off = hlen; off < tlen; off += mtu) {
238 m = m_gethdr(M_NOWAIT, MT_DATA);
240 IP6STAT_INC(ip6s_odropped);
243 m->m_flags = m0->m_flags & M_COPYFLAGS;
245 mnext = &m->m_nextpkt;
246 m->m_data += max_linkhdr;
247 mhip6 = mtod(m, struct ip6_hdr *);
249 m->m_len = sizeof(*mhip6);
250 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
252 IP6STAT_INC(ip6s_odropped);
255 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
256 if (off + mtu >= tlen)
259 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
260 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
261 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
262 if ((m_frgpart = m_copym(m0, off, mtu, M_NOWAIT)) == NULL) {
263 IP6STAT_INC(ip6s_odropped);
267 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
268 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
269 m->m_pkthdr.rcvif = NULL;
270 ip6f->ip6f_reserved = 0;
271 ip6f->ip6f_ident = id;
272 ip6f->ip6f_nxt = nextproto;
273 IP6STAT_INC(ip6s_ofragments);
274 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
281 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
282 * header (with pri, len, nxt, hlim, src, dst).
283 * This function may modify ver and hlim only.
284 * The mbuf chain containing the packet will be freed.
285 * The mbuf opt, if present, will not be freed.
286 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
287 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
288 * then result of route lookup is stored in ro->ro_rt.
290 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
291 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
294 * ifpp - XXX: just for statistics
297 * XXX TODO: no flowid is assigned for outbound flows?
300 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
301 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
302 struct ifnet **ifpp, struct inpcb *inp)
305 struct ifnet *ifp, *origifp;
307 struct mbuf *mprev = NULL;
309 struct route_in6 ip6route;
310 struct rtentry *rt = NULL;
311 struct sockaddr_in6 *dst, src_sa, dst_sa;
312 struct in6_addr odst;
314 struct in6_ifaddr *ia = NULL;
316 int alwaysfrag, dontfrag;
317 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
318 struct ip6_exthdrs exthdrs;
319 struct in6_addr src0, dst0;
321 struct route_in6 *ro_pmtu = NULL;
326 struct m_tag *fwd_tag = NULL;
330 M_SETFIB(m, inp->inp_inc.inc_fibnum);
331 if ((flags & IP_NODEFAULTFLOWID) == 0) {
332 /* unconditionally set flowid */
333 m->m_pkthdr.flowid = inp->inp_flowid;
334 M_HASHTYPE_SET(m, inp->inp_flowtype);
338 bzero(&exthdrs, sizeof(exthdrs));
340 /* Hop-by-Hop options header */
341 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
342 /* Destination options header(1st part) */
343 if (opt->ip6po_rthdr) {
345 * Destination options header(1st part)
346 * This only makes sense with a routing header.
347 * See Section 9.2 of RFC 3542.
348 * Disabling this part just for MIP6 convenience is
349 * a bad idea. We need to think carefully about a
350 * way to make the advanced API coexist with MIP6
351 * options, which might automatically be inserted in
354 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
357 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
358 /* Destination options header(2nd part) */
359 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
364 * IPSec checking which handles several cases.
365 * FAST IPSEC: We re-injected the packet.
366 * XXX: need scope argument.
368 switch(ip6_ipsec_output(&m, inp, &error))
370 case 1: /* Bad packet */
372 case -1: /* IPSec done */
374 case 0: /* No IPSec */
381 * Calculate the total length of the extension header chain.
382 * Keep the length of the unfragmentable part for fragmentation.
385 if (exthdrs.ip6e_hbh)
386 optlen += exthdrs.ip6e_hbh->m_len;
387 if (exthdrs.ip6e_dest1)
388 optlen += exthdrs.ip6e_dest1->m_len;
389 if (exthdrs.ip6e_rthdr)
390 optlen += exthdrs.ip6e_rthdr->m_len;
391 unfragpartlen = optlen + sizeof(struct ip6_hdr);
393 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
394 if (exthdrs.ip6e_dest2)
395 optlen += exthdrs.ip6e_dest2->m_len;
398 * If there is at least one extension header,
399 * separate IP6 header from the payload.
401 if (optlen && !hdrsplit) {
402 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
406 m = exthdrs.ip6e_ip6;
410 ip6 = mtod(m, struct ip6_hdr *);
412 /* adjust mbuf packet header length */
413 m->m_pkthdr.len += optlen;
414 plen = m->m_pkthdr.len - sizeof(*ip6);
416 /* If this is a jumbo payload, insert a jumbo payload option. */
417 if (plen > IPV6_MAXPACKET) {
419 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
423 m = exthdrs.ip6e_ip6;
427 ip6 = mtod(m, struct ip6_hdr *);
428 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
432 ip6->ip6_plen = htons(plen);
435 * Concatenate headers and fill in next header fields.
436 * Here we have, on "m"
438 * and we insert headers accordingly. Finally, we should be getting:
439 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
441 * during the header composing process, "m" points to IPv6 header.
442 * "mprev" points to an extension header prior to esp.
444 u_char *nexthdrp = &ip6->ip6_nxt;
448 * we treat dest2 specially. this makes IPsec processing
449 * much easier. the goal here is to make mprev point the
450 * mbuf prior to dest2.
452 * result: IPv6 dest2 payload
453 * m and mprev will point to IPv6 header.
455 if (exthdrs.ip6e_dest2) {
457 panic("assumption failed: hdr not split");
458 exthdrs.ip6e_dest2->m_next = m->m_next;
459 m->m_next = exthdrs.ip6e_dest2;
460 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
461 ip6->ip6_nxt = IPPROTO_DSTOPTS;
465 * result: IPv6 hbh dest1 rthdr dest2 payload
466 * m will point to IPv6 header. mprev will point to the
467 * extension header prior to dest2 (rthdr in the above case).
469 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
470 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
472 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
476 * If there is a routing header, discard the packet.
478 if (exthdrs.ip6e_rthdr) {
483 /* Source address validation */
484 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
485 (flags & IPV6_UNSPECSRC) == 0) {
487 IP6STAT_INC(ip6s_badscope);
490 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
492 IP6STAT_INC(ip6s_badscope);
496 IP6STAT_INC(ip6s_localout);
503 bzero((caddr_t)ro, sizeof(*ro));
505 ro->ro_flags |= RT_LLE_CACHE;
507 if (opt && opt->ip6po_rthdr)
508 ro = &opt->ip6po_route;
509 dst = (struct sockaddr_in6 *)&ro->ro_dst;
511 if (ro->ro_rt == NULL)
512 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
514 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
517 * if specified, try to fill in the traffic class field.
518 * do not override if a non-zero value is already set.
519 * we check the diffserv field and the ecn field separately.
521 if (opt && opt->ip6po_tclass >= 0) {
524 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
526 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
529 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
532 /* fill in or override the hop limit field, if necessary. */
533 if (opt && opt->ip6po_hlim != -1)
534 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
535 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
537 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
539 ip6->ip6_hlim = V_ip6_defmcasthlim;
542 * Validate route against routing table additions;
543 * a better/more specific route might have been added.
544 * Make sure address family is set in route.
547 ro->ro_dst.sin6_family = AF_INET6;
548 RT_VALIDATE((struct route *)ro, &inp->inp_rt_cookie, fibnum);
550 if (ro->ro_rt && fwd_tag == NULL && (ro->ro_rt->rt_flags & RTF_UP) &&
551 ro->ro_dst.sin6_family == AF_INET6 &&
552 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
554 ifp = ro->ro_rt->rt_ifp;
557 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
559 if (fwd_tag == NULL) {
560 bzero(&dst_sa, sizeof(dst_sa));
561 dst_sa.sin6_family = AF_INET6;
562 dst_sa.sin6_len = sizeof(dst_sa);
563 dst_sa.sin6_addr = ip6->ip6_dst;
565 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
569 in6_ifstat_inc(ifp, ifs6_out_discard);
575 * If in6_selectroute() does not return a route entry,
576 * dst may not have been updated.
578 *dst = dst_sa; /* XXX */
582 * then rt (for unicast) and ifp must be non-NULL valid values.
584 if ((flags & IPV6_FORWARDING) == 0) {
585 /* XXX: the FORWARDING flag can be set for mrouting. */
586 in6_ifstat_inc(ifp, ifs6_out_request);
589 ia = (struct in6_ifaddr *)(rt->rt_ifa);
590 counter_u64_add(rt->rt_pksent, 1);
595 * The outgoing interface must be in the zone of source and
596 * destination addresses.
601 if (in6_setscope(&src0, origifp, &zone))
603 bzero(&src_sa, sizeof(src_sa));
604 src_sa.sin6_family = AF_INET6;
605 src_sa.sin6_len = sizeof(src_sa);
606 src_sa.sin6_addr = ip6->ip6_src;
607 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
611 if (in6_setscope(&dst0, origifp, &zone))
613 /* re-initialize to be sure */
614 bzero(&dst_sa, sizeof(dst_sa));
615 dst_sa.sin6_family = AF_INET6;
616 dst_sa.sin6_len = sizeof(dst_sa);
617 dst_sa.sin6_addr = ip6->ip6_dst;
618 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
622 /* We should use ia_ifp to support the case of
623 * sending packets to an address of our own.
625 if (ia != NULL && ia->ia_ifp)
628 /* scope check is done. */
632 IP6STAT_INC(ip6s_badscope);
633 in6_ifstat_inc(origifp, ifs6_out_discard);
635 error = EHOSTUNREACH; /* XXX */
639 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
640 if (opt && opt->ip6po_nextroute.ro_rt) {
642 * The nexthop is explicitly specified by the
643 * application. We assume the next hop is an IPv6
646 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
648 else if ((rt->rt_flags & RTF_GATEWAY))
649 dst = (struct sockaddr_in6 *)rt->rt_gateway;
652 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
653 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
655 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
656 in6_ifstat_inc(ifp, ifs6_out_mcast);
658 * Confirm that the outgoing interface supports multicast.
660 if (!(ifp->if_flags & IFF_MULTICAST)) {
661 IP6STAT_INC(ip6s_noroute);
662 in6_ifstat_inc(ifp, ifs6_out_discard);
666 if ((im6o == NULL && in6_mcast_loop) ||
667 (im6o && im6o->im6o_multicast_loop)) {
669 * Loop back multicast datagram if not expressly
670 * forbidden to do so, even if we have not joined
671 * the address; protocols will filter it later,
672 * thus deferring a hash lookup and lock acquisition
673 * at the expense of an m_copym().
675 ip6_mloopback(ifp, m);
678 * If we are acting as a multicast router, perform
679 * multicast forwarding as if the packet had just
680 * arrived on the interface to which we are about
681 * to send. The multicast forwarding function
682 * recursively calls this function, using the
683 * IPV6_FORWARDING flag to prevent infinite recursion.
685 * Multicasts that are looped back by ip6_mloopback(),
686 * above, will be forwarded by the ip6_input() routine,
689 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
691 * XXX: ip6_mforward expects that rcvif is NULL
692 * when it is called from the originating path.
693 * However, it may not always be the case.
695 m->m_pkthdr.rcvif = NULL;
696 if (ip6_mforward(ip6, ifp, m) != 0) {
703 * Multicasts with a hoplimit of zero may be looped back,
704 * above, but must not be transmitted on a network.
705 * Also, multicasts addressed to the loopback interface
706 * are not sent -- the above call to ip6_mloopback() will
707 * loop back a copy if this host actually belongs to the
708 * destination group on the loopback interface.
710 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
711 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
718 * Fill the outgoing inteface to tell the upper layer
719 * to increment per-interface statistics.
724 /* Determine path MTU. */
725 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
726 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
730 * The caller of this function may specify to use the minimum MTU
732 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
733 * setting. The logic is a bit complicated; by default, unicast
734 * packets will follow path MTU while multicast packets will be sent at
735 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
736 * including unicast ones will be sent at the minimum MTU. Multicast
737 * packets will always be sent at the minimum MTU unless
738 * IP6PO_MINMTU_DISABLE is explicitly specified.
739 * See RFC 3542 for more details.
741 if (mtu > IPV6_MMTU) {
742 if ((flags & IPV6_MINMTU))
744 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
746 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
748 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
754 * clear embedded scope identifiers if necessary.
755 * in6_clearscope will touch the addresses only when necessary.
757 in6_clearscope(&ip6->ip6_src);
758 in6_clearscope(&ip6->ip6_dst);
761 * If the outgoing packet contains a hop-by-hop options header,
762 * it must be examined and processed even by the source node.
763 * (RFC 2460, section 4.)
765 if (exthdrs.ip6e_hbh) {
766 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
767 u_int32_t dummy; /* XXX unused */
768 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
771 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
772 panic("ip6e_hbh is not contiguous");
775 * XXX: if we have to send an ICMPv6 error to the sender,
776 * we need the M_LOOP flag since icmp6_error() expects
777 * the IPv6 and the hop-by-hop options header are
778 * contiguous unless the flag is set.
780 m->m_flags |= M_LOOP;
781 m->m_pkthdr.rcvif = ifp;
782 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
783 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
784 &dummy, &plen) < 0) {
785 /* m was already freed at this point */
786 error = EINVAL;/* better error? */
789 m->m_flags &= ~M_LOOP; /* XXX */
790 m->m_pkthdr.rcvif = NULL;
793 /* Jump over all PFIL processing if hooks are not active. */
794 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
798 /* Run through list of hooks for output packets. */
799 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
800 if (error != 0 || m == NULL)
803 ip6 = mtod(m, struct ip6_hdr *);
806 /* See if destination IP address was changed by packet filter. */
807 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
808 m->m_flags |= M_SKIP_FIREWALL;
809 /* If destination is now ourself drop to ip6_input(). */
810 if (in6_localip(&ip6->ip6_dst)) {
811 m->m_flags |= M_FASTFWD_OURS;
812 if (m->m_pkthdr.rcvif == NULL)
813 m->m_pkthdr.rcvif = V_loif;
814 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
815 m->m_pkthdr.csum_flags |=
816 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
817 m->m_pkthdr.csum_data = 0xffff;
820 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
821 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
823 error = netisr_queue(NETISR_IPV6, m);
827 needfiblookup = 1; /* Redo the routing table lookup. */
829 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
833 /* See if fib was changed by packet filter. */
834 if (fibnum != M_GETFIB(m)) {
835 m->m_flags |= M_SKIP_FIREWALL;
836 fibnum = M_GETFIB(m);
840 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
846 /* See if local, if yes, send it to netisr. */
847 if (m->m_flags & M_FASTFWD_OURS) {
848 if (m->m_pkthdr.rcvif == NULL)
849 m->m_pkthdr.rcvif = V_loif;
850 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
851 m->m_pkthdr.csum_flags |=
852 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
853 m->m_pkthdr.csum_data = 0xffff;
856 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
857 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
859 error = netisr_queue(NETISR_IPV6, m);
862 /* Or forward to some other address? */
863 if ((m->m_flags & M_IP6_NEXTHOP) &&
864 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
865 dst = (struct sockaddr_in6 *)&ro->ro_dst;
866 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
867 m->m_flags |= M_SKIP_FIREWALL;
868 m->m_flags &= ~M_IP6_NEXTHOP;
869 m_tag_delete(m, fwd_tag);
875 * Send the packet to the outgoing interface.
876 * If necessary, do IPv6 fragmentation before sending.
878 * the logic here is rather complex:
879 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
880 * 1-a: send as is if tlen <= path mtu
881 * 1-b: fragment if tlen > path mtu
883 * 2: if user asks us not to fragment (dontfrag == 1)
884 * 2-a: send as is if tlen <= interface mtu
885 * 2-b: error if tlen > interface mtu
887 * 3: if we always need to attach fragment header (alwaysfrag == 1)
890 * 4: if dontfrag == 1 && alwaysfrag == 1
891 * error, as we cannot handle this conflicting request
893 sw_csum = m->m_pkthdr.csum_flags;
895 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
896 sw_csum &= ~ifp->if_hwassist;
900 * If we added extension headers, we will not do TSO and calculate the
901 * checksums ourselves for now.
902 * XXX-BZ Need a framework to know when the NIC can handle it, even
905 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
906 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
907 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
910 if (sw_csum & CSUM_SCTP_IPV6) {
911 sw_csum &= ~CSUM_SCTP_IPV6;
912 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
915 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
916 tlen = m->m_pkthdr.len;
918 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
922 if (dontfrag && alwaysfrag) { /* case 4 */
923 /* conflicting request - can't transmit */
927 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
929 * Even if the DONTFRAG option is specified, we cannot send the
930 * packet when the data length is larger than the MTU of the
931 * outgoing interface.
932 * Notify the error by sending IPV6_PATHMTU ancillary data if
933 * application wanted to know the MTU value. Also return an
934 * error code (this is not described in the API spec).
937 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
943 * transmit packet without fragmentation
945 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
946 struct in6_ifaddr *ia6;
948 ip6 = mtod(m, struct ip6_hdr *);
949 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
951 /* Record statistics for this interface address. */
952 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
953 counter_u64_add(ia6->ia_ifa.ifa_obytes,
955 ifa_free(&ia6->ia_ifa);
957 error = nd6_output_ifp(ifp, origifp, m, dst,
963 * try to fragment the packet. case 1-b and 3
965 if (mtu < IPV6_MMTU) {
966 /* path MTU cannot be less than IPV6_MMTU */
968 in6_ifstat_inc(ifp, ifs6_out_fragfail);
970 } else if (ip6->ip6_plen == 0) {
971 /* jumbo payload cannot be fragmented */
973 in6_ifstat_inc(ifp, ifs6_out_fragfail);
979 * Too large for the destination or interface;
980 * fragment if possible.
981 * Must be able to put at least 8 bytes per fragment.
983 hlen = unfragpartlen;
984 if (mtu > IPV6_MAXPACKET)
985 mtu = IPV6_MAXPACKET;
987 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
990 in6_ifstat_inc(ifp, ifs6_out_fragfail);
995 * If the interface will not calculate checksums on
996 * fragmented packets, then do it here.
997 * XXX-BZ handle the hw offloading case. Need flags.
999 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1000 in6_delayed_cksum(m, plen, hlen);
1001 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
1004 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
1005 sctp_delayed_cksum(m, hlen);
1006 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
1010 * Change the next header field of the last header in the
1011 * unfragmentable part.
1013 if (exthdrs.ip6e_rthdr) {
1014 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1015 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1016 } else if (exthdrs.ip6e_dest1) {
1017 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1018 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1019 } else if (exthdrs.ip6e_hbh) {
1020 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1021 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1023 nextproto = ip6->ip6_nxt;
1024 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1028 * Loop through length of segment after first fragment,
1029 * make new header and copy data of each part and link onto
1033 id = htonl(ip6_randomid());
1034 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1037 in6_ifstat_inc(ifp, ifs6_out_fragok);
1041 * Remove leading garbages.
1047 for (m0 = m; m; m = m0) {
1051 /* Record statistics for this interface address. */
1053 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1054 counter_u64_add(ia->ia_ifa.ifa_obytes,
1057 error = nd6_output_ifp(ifp, origifp, m, dst,
1058 (struct route *)ro);
1064 IP6STAT_INC(ip6s_fragmented);
1067 if (ro == &ip6route)
1072 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1073 m_freem(exthdrs.ip6e_dest1);
1074 m_freem(exthdrs.ip6e_rthdr);
1075 m_freem(exthdrs.ip6e_dest2);
1084 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1088 if (hlen > MCLBYTES)
1089 return (ENOBUFS); /* XXX */
1092 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1094 m = m_get(M_NOWAIT, MT_DATA);
1099 bcopy(hdr, mtod(m, caddr_t), hlen);
1106 * Insert jumbo payload option.
1109 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1115 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1118 * If there is no hop-by-hop options header, allocate new one.
1119 * If there is one but it doesn't have enough space to store the
1120 * jumbo payload option, allocate a cluster to store the whole options.
1121 * Otherwise, use it to store the options.
1123 if (exthdrs->ip6e_hbh == NULL) {
1124 mopt = m_get(M_NOWAIT, MT_DATA);
1127 mopt->m_len = JUMBOOPTLEN;
1128 optbuf = mtod(mopt, u_char *);
1129 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1130 exthdrs->ip6e_hbh = mopt;
1132 struct ip6_hbh *hbh;
1134 mopt = exthdrs->ip6e_hbh;
1135 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1138 * - exthdrs->ip6e_hbh is not referenced from places
1139 * other than exthdrs.
1140 * - exthdrs->ip6e_hbh is not an mbuf chain.
1142 int oldoptlen = mopt->m_len;
1146 * XXX: give up if the whole (new) hbh header does
1147 * not fit even in an mbuf cluster.
1149 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1153 * As a consequence, we must always prepare a cluster
1156 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1159 n->m_len = oldoptlen + JUMBOOPTLEN;
1160 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1162 optbuf = mtod(n, caddr_t) + oldoptlen;
1164 mopt = exthdrs->ip6e_hbh = n;
1166 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1167 mopt->m_len += JUMBOOPTLEN;
1169 optbuf[0] = IP6OPT_PADN;
1173 * Adjust the header length according to the pad and
1174 * the jumbo payload option.
1176 hbh = mtod(mopt, struct ip6_hbh *);
1177 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1180 /* fill in the option. */
1181 optbuf[2] = IP6OPT_JUMBO;
1183 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1184 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1186 /* finally, adjust the packet header length */
1187 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1194 * Insert fragment header and copy unfragmentable header portions.
1197 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1198 struct ip6_frag **frghdrp)
1200 struct mbuf *n, *mlast;
1202 if (hlen > sizeof(struct ip6_hdr)) {
1203 n = m_copym(m0, sizeof(struct ip6_hdr),
1204 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1211 /* Search for the last mbuf of unfragmentable part. */
1212 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1215 if (M_WRITABLE(mlast) &&
1216 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1217 /* use the trailing space of the last mbuf for the fragment hdr */
1218 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1220 mlast->m_len += sizeof(struct ip6_frag);
1221 m->m_pkthdr.len += sizeof(struct ip6_frag);
1223 /* allocate a new mbuf for the fragment header */
1226 mfrg = m_get(M_NOWAIT, MT_DATA);
1229 mfrg->m_len = sizeof(struct ip6_frag);
1230 *frghdrp = mtod(mfrg, struct ip6_frag *);
1231 mlast->m_next = mfrg;
1238 * Calculates IPv6 path mtu for destination @dst.
1239 * Resulting MTU is stored in @mtup.
1241 * Returns 0 on success.
1244 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1246 struct nhop6_extended nh6;
1247 struct in6_addr kdst;
1253 in6_splitscope(dst, &kdst, &scopeid);
1254 if (fib6_lookup_nh_ext(fibnum, &kdst, scopeid, NHR_REF, 0, &nh6) != 0)
1255 return (EHOSTUNREACH);
1260 error = ip6_calcmtu(ifp, dst, mtu, mtup, NULL, 0);
1261 fib6_free_nh_ext(fibnum, &nh6);
1267 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1268 * and cached data in @ro_pmtu.
1269 * MTU from (successful) route lookup is saved (along with dst)
1270 * inside @ro_pmtu to avoid subsequent route lookups after packet
1271 * filter processing.
1273 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1274 * Returns 0 on success.
1277 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1278 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1279 int *alwaysfragp, u_int fibnum, u_int proto)
1281 struct nhop6_basic nh6;
1282 struct in6_addr kdst;
1284 struct sockaddr_in6 *sa6_dst;
1291 * Here ro_pmtu has final destination address, while
1292 * ro might represent immediate destination.
1293 * Use ro_pmtu destination since mtu might differ.
1295 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1296 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1297 ro_pmtu->ro_mtu = 0;
1299 if (ro_pmtu->ro_mtu == 0) {
1300 bzero(sa6_dst, sizeof(*sa6_dst));
1301 sa6_dst->sin6_family = AF_INET6;
1302 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1303 sa6_dst->sin6_addr = *dst;
1305 in6_splitscope(dst, &kdst, &scopeid);
1306 if (fib6_lookup_nh_basic(fibnum, &kdst, scopeid, 0, 0,
1308 ro_pmtu->ro_mtu = nh6.nh_mtu;
1311 mtu = ro_pmtu->ro_mtu;
1315 mtu = ro_pmtu->ro_rt->rt_mtu;
1317 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1321 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1322 * hostcache data for @dst.
1323 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1325 * Returns 0 on success.
1328 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1329 u_long *mtup, int *alwaysfragp, u_int proto)
1337 struct in_conninfo inc;
1339 bzero(&inc, sizeof(inc));
1340 inc.inc_flags |= INC_ISIPV6;
1341 inc.inc6_faddr = *dst;
1343 ifmtu = IN6_LINKMTU(ifp);
1345 /* TCP is known to react to pmtu changes so skip hc */
1346 if (proto != IPPROTO_TCP)
1347 mtu = tcp_hc_getmtu(&inc);
1350 mtu = min(mtu, rt_mtu);
1355 else if (mtu < IPV6_MMTU) {
1357 * RFC2460 section 5, last paragraph:
1358 * if we record ICMPv6 too big message with
1359 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1360 * or smaller, with framgent header attached.
1361 * (fragment header is needed regardless from the
1362 * packet size, for translators to identify packets)
1368 mtu = IN6_LINKMTU(ifp);
1370 error = EHOSTUNREACH; /* XXX */
1374 *alwaysfragp = alwaysfrag;
1379 * IP6 socket option processing.
1382 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1384 int optdatalen, uproto;
1386 struct inpcb *in6p = sotoinpcb(so);
1388 int level, op, optname;
1392 uint32_t rss_bucket;
1397 * Don't use more than a quarter of mbuf clusters. N.B.:
1398 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1399 * on LP64 architectures, so cast to u_long to avoid undefined
1400 * behavior. ILP32 architectures cannot have nmbclusters
1401 * large enough to overflow for other reasons.
1403 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1405 level = sopt->sopt_level;
1406 op = sopt->sopt_dir;
1407 optname = sopt->sopt_name;
1408 optlen = sopt->sopt_valsize;
1412 uproto = (int)so->so_proto->pr_protocol;
1414 if (level != IPPROTO_IPV6) {
1417 if (sopt->sopt_level == SOL_SOCKET &&
1418 sopt->sopt_dir == SOPT_SET) {
1419 switch (sopt->sopt_name) {
1422 if ((so->so_options & SO_REUSEADDR) != 0)
1423 in6p->inp_flags2 |= INP_REUSEADDR;
1425 in6p->inp_flags2 &= ~INP_REUSEADDR;
1431 if ((so->so_options & SO_REUSEPORT) != 0)
1432 in6p->inp_flags2 |= INP_REUSEPORT;
1434 in6p->inp_flags2 &= ~INP_REUSEPORT;
1440 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1448 } else { /* level == IPPROTO_IPV6 */
1453 case IPV6_2292PKTOPTIONS:
1454 #ifdef IPV6_PKTOPTIONS
1455 case IPV6_PKTOPTIONS:
1460 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1461 printf("ip6_ctloutput: mbuf limit hit\n");
1466 error = soopt_getm(sopt, &m); /* XXX */
1469 error = soopt_mcopyin(sopt, m); /* XXX */
1472 error = ip6_pcbopts(&in6p->in6p_outputopts,
1474 m_freem(m); /* XXX */
1479 * Use of some Hop-by-Hop options or some
1480 * Destination options, might require special
1481 * privilege. That is, normal applications
1482 * (without special privilege) might be forbidden
1483 * from setting certain options in outgoing packets,
1484 * and might never see certain options in received
1485 * packets. [RFC 2292 Section 6]
1486 * KAME specific note:
1487 * KAME prevents non-privileged users from sending or
1488 * receiving ANY hbh/dst options in order to avoid
1489 * overhead of parsing options in the kernel.
1491 case IPV6_RECVHOPOPTS:
1492 case IPV6_RECVDSTOPTS:
1493 case IPV6_RECVRTHDRDSTOPTS:
1495 error = priv_check(td,
1496 PRIV_NETINET_SETHDROPTS);
1501 case IPV6_UNICAST_HOPS:
1504 case IPV6_RECVPKTINFO:
1505 case IPV6_RECVHOPLIMIT:
1506 case IPV6_RECVRTHDR:
1507 case IPV6_RECVPATHMTU:
1508 case IPV6_RECVTCLASS:
1509 case IPV6_RECVFLOWID:
1511 case IPV6_RECVRSSBUCKETID:
1514 case IPV6_AUTOFLOWLABEL:
1516 case IPV6_BINDMULTI:
1518 case IPV6_RSS_LISTEN_BUCKET:
1520 if (optname == IPV6_BINDANY && td != NULL) {
1521 error = priv_check(td,
1522 PRIV_NETINET_BINDANY);
1527 if (optlen != sizeof(int)) {
1531 error = sooptcopyin(sopt, &optval,
1532 sizeof optval, sizeof optval);
1537 case IPV6_UNICAST_HOPS:
1538 if (optval < -1 || optval >= 256)
1541 /* -1 = kernel default */
1542 in6p->in6p_hops = optval;
1543 if ((in6p->inp_vflag &
1545 in6p->inp_ip_ttl = optval;
1548 #define OPTSET(bit) \
1552 in6p->inp_flags |= (bit); \
1554 in6p->inp_flags &= ~(bit); \
1555 INP_WUNLOCK(in6p); \
1556 } while (/*CONSTCOND*/ 0)
1557 #define OPTSET2292(bit) \
1560 in6p->inp_flags |= IN6P_RFC2292; \
1562 in6p->inp_flags |= (bit); \
1564 in6p->inp_flags &= ~(bit); \
1565 INP_WUNLOCK(in6p); \
1566 } while (/*CONSTCOND*/ 0)
1567 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1569 #define OPTSET2(bit, val) do { \
1572 in6p->inp_flags2 |= bit; \
1574 in6p->inp_flags2 &= ~bit; \
1575 INP_WUNLOCK(in6p); \
1577 #define OPTBIT2(bit) (in6p->inp_flags2 & (bit) ? 1 : 0)
1579 case IPV6_RECVPKTINFO:
1580 /* cannot mix with RFC2292 */
1581 if (OPTBIT(IN6P_RFC2292)) {
1585 OPTSET(IN6P_PKTINFO);
1590 struct ip6_pktopts **optp;
1592 /* cannot mix with RFC2292 */
1593 if (OPTBIT(IN6P_RFC2292)) {
1597 optp = &in6p->in6p_outputopts;
1598 error = ip6_pcbopt(IPV6_HOPLIMIT,
1599 (u_char *)&optval, sizeof(optval),
1600 optp, (td != NULL) ? td->td_ucred :
1605 case IPV6_RECVHOPLIMIT:
1606 /* cannot mix with RFC2292 */
1607 if (OPTBIT(IN6P_RFC2292)) {
1611 OPTSET(IN6P_HOPLIMIT);
1614 case IPV6_RECVHOPOPTS:
1615 /* cannot mix with RFC2292 */
1616 if (OPTBIT(IN6P_RFC2292)) {
1620 OPTSET(IN6P_HOPOPTS);
1623 case IPV6_RECVDSTOPTS:
1624 /* cannot mix with RFC2292 */
1625 if (OPTBIT(IN6P_RFC2292)) {
1629 OPTSET(IN6P_DSTOPTS);
1632 case IPV6_RECVRTHDRDSTOPTS:
1633 /* cannot mix with RFC2292 */
1634 if (OPTBIT(IN6P_RFC2292)) {
1638 OPTSET(IN6P_RTHDRDSTOPTS);
1641 case IPV6_RECVRTHDR:
1642 /* cannot mix with RFC2292 */
1643 if (OPTBIT(IN6P_RFC2292)) {
1650 case IPV6_RECVPATHMTU:
1652 * We ignore this option for TCP
1654 * (RFC3542 leaves this case
1657 if (uproto != IPPROTO_TCP)
1661 case IPV6_RECVFLOWID:
1662 OPTSET2(INP_RECVFLOWID, optval);
1666 case IPV6_RECVRSSBUCKETID:
1667 OPTSET2(INP_RECVRSSBUCKETID, optval);
1673 * make setsockopt(IPV6_V6ONLY)
1674 * available only prior to bind(2).
1675 * see ipng mailing list, Jun 22 2001.
1677 if (in6p->inp_lport ||
1678 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1682 OPTSET(IN6P_IPV6_V6ONLY);
1684 in6p->inp_vflag &= ~INP_IPV4;
1686 in6p->inp_vflag |= INP_IPV4;
1688 case IPV6_RECVTCLASS:
1689 /* cannot mix with RFC2292 XXX */
1690 if (OPTBIT(IN6P_RFC2292)) {
1694 OPTSET(IN6P_TCLASS);
1696 case IPV6_AUTOFLOWLABEL:
1697 OPTSET(IN6P_AUTOFLOWLABEL);
1701 OPTSET(INP_BINDANY);
1704 case IPV6_BINDMULTI:
1705 OPTSET2(INP_BINDMULTI, optval);
1708 case IPV6_RSS_LISTEN_BUCKET:
1709 if ((optval >= 0) &&
1710 (optval < rss_getnumbuckets())) {
1711 in6p->inp_rss_listen_bucket = optval;
1712 OPTSET2(INP_RSS_BUCKET_SET, 1);
1723 case IPV6_USE_MIN_MTU:
1724 case IPV6_PREFER_TEMPADDR:
1725 if (optlen != sizeof(optval)) {
1729 error = sooptcopyin(sopt, &optval,
1730 sizeof optval, sizeof optval);
1734 struct ip6_pktopts **optp;
1735 optp = &in6p->in6p_outputopts;
1736 error = ip6_pcbopt(optname,
1737 (u_char *)&optval, sizeof(optval),
1738 optp, (td != NULL) ? td->td_ucred :
1743 case IPV6_2292PKTINFO:
1744 case IPV6_2292HOPLIMIT:
1745 case IPV6_2292HOPOPTS:
1746 case IPV6_2292DSTOPTS:
1747 case IPV6_2292RTHDR:
1749 if (optlen != sizeof(int)) {
1753 error = sooptcopyin(sopt, &optval,
1754 sizeof optval, sizeof optval);
1758 case IPV6_2292PKTINFO:
1759 OPTSET2292(IN6P_PKTINFO);
1761 case IPV6_2292HOPLIMIT:
1762 OPTSET2292(IN6P_HOPLIMIT);
1764 case IPV6_2292HOPOPTS:
1766 * Check super-user privilege.
1767 * See comments for IPV6_RECVHOPOPTS.
1770 error = priv_check(td,
1771 PRIV_NETINET_SETHDROPTS);
1775 OPTSET2292(IN6P_HOPOPTS);
1777 case IPV6_2292DSTOPTS:
1779 error = priv_check(td,
1780 PRIV_NETINET_SETHDROPTS);
1784 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1786 case IPV6_2292RTHDR:
1787 OPTSET2292(IN6P_RTHDR);
1795 case IPV6_RTHDRDSTOPTS:
1798 /* new advanced API (RFC3542) */
1800 u_char optbuf_storage[MCLBYTES];
1802 struct ip6_pktopts **optp;
1804 /* cannot mix with RFC2292 */
1805 if (OPTBIT(IN6P_RFC2292)) {
1811 * We only ensure valsize is not too large
1812 * here. Further validation will be done
1815 error = sooptcopyin(sopt, optbuf_storage,
1816 sizeof(optbuf_storage), 0);
1819 optlen = sopt->sopt_valsize;
1820 optbuf = optbuf_storage;
1821 optp = &in6p->in6p_outputopts;
1822 error = ip6_pcbopt(optname, optbuf, optlen,
1823 optp, (td != NULL) ? td->td_ucred : NULL,
1829 case IPV6_MULTICAST_IF:
1830 case IPV6_MULTICAST_HOPS:
1831 case IPV6_MULTICAST_LOOP:
1832 case IPV6_JOIN_GROUP:
1833 case IPV6_LEAVE_GROUP:
1835 case MCAST_BLOCK_SOURCE:
1836 case MCAST_UNBLOCK_SOURCE:
1837 case MCAST_JOIN_GROUP:
1838 case MCAST_LEAVE_GROUP:
1839 case MCAST_JOIN_SOURCE_GROUP:
1840 case MCAST_LEAVE_SOURCE_GROUP:
1841 error = ip6_setmoptions(in6p, sopt);
1844 case IPV6_PORTRANGE:
1845 error = sooptcopyin(sopt, &optval,
1846 sizeof optval, sizeof optval);
1852 case IPV6_PORTRANGE_DEFAULT:
1853 in6p->inp_flags &= ~(INP_LOWPORT);
1854 in6p->inp_flags &= ~(INP_HIGHPORT);
1857 case IPV6_PORTRANGE_HIGH:
1858 in6p->inp_flags &= ~(INP_LOWPORT);
1859 in6p->inp_flags |= INP_HIGHPORT;
1862 case IPV6_PORTRANGE_LOW:
1863 in6p->inp_flags &= ~(INP_HIGHPORT);
1864 in6p->inp_flags |= INP_LOWPORT;
1875 case IPV6_IPSEC_POLICY:
1880 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1882 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1884 req = mtod(m, caddr_t);
1885 error = ipsec_set_policy(in6p, optname, req,
1886 m->m_len, (sopt->sopt_td != NULL) ?
1887 sopt->sopt_td->td_ucred : NULL);
1894 error = ENOPROTOOPT;
1902 case IPV6_2292PKTOPTIONS:
1903 #ifdef IPV6_PKTOPTIONS
1904 case IPV6_PKTOPTIONS:
1907 * RFC3542 (effectively) deprecated the
1908 * semantics of the 2292-style pktoptions.
1909 * Since it was not reliable in nature (i.e.,
1910 * applications had to expect the lack of some
1911 * information after all), it would make sense
1912 * to simplify this part by always returning
1915 sopt->sopt_valsize = 0;
1918 case IPV6_RECVHOPOPTS:
1919 case IPV6_RECVDSTOPTS:
1920 case IPV6_RECVRTHDRDSTOPTS:
1921 case IPV6_UNICAST_HOPS:
1922 case IPV6_RECVPKTINFO:
1923 case IPV6_RECVHOPLIMIT:
1924 case IPV6_RECVRTHDR:
1925 case IPV6_RECVPATHMTU:
1928 case IPV6_PORTRANGE:
1929 case IPV6_RECVTCLASS:
1930 case IPV6_AUTOFLOWLABEL:
1934 case IPV6_RECVFLOWID:
1936 case IPV6_RSSBUCKETID:
1937 case IPV6_RECVRSSBUCKETID:
1939 case IPV6_BINDMULTI:
1942 case IPV6_RECVHOPOPTS:
1943 optval = OPTBIT(IN6P_HOPOPTS);
1946 case IPV6_RECVDSTOPTS:
1947 optval = OPTBIT(IN6P_DSTOPTS);
1950 case IPV6_RECVRTHDRDSTOPTS:
1951 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1954 case IPV6_UNICAST_HOPS:
1955 optval = in6p->in6p_hops;
1958 case IPV6_RECVPKTINFO:
1959 optval = OPTBIT(IN6P_PKTINFO);
1962 case IPV6_RECVHOPLIMIT:
1963 optval = OPTBIT(IN6P_HOPLIMIT);
1966 case IPV6_RECVRTHDR:
1967 optval = OPTBIT(IN6P_RTHDR);
1970 case IPV6_RECVPATHMTU:
1971 optval = OPTBIT(IN6P_MTU);
1975 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1978 case IPV6_PORTRANGE:
1981 flags = in6p->inp_flags;
1982 if (flags & INP_HIGHPORT)
1983 optval = IPV6_PORTRANGE_HIGH;
1984 else if (flags & INP_LOWPORT)
1985 optval = IPV6_PORTRANGE_LOW;
1990 case IPV6_RECVTCLASS:
1991 optval = OPTBIT(IN6P_TCLASS);
1994 case IPV6_AUTOFLOWLABEL:
1995 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1999 optval = OPTBIT(INP_BINDANY);
2003 optval = in6p->inp_flowid;
2007 optval = in6p->inp_flowtype;
2010 case IPV6_RECVFLOWID:
2011 optval = OPTBIT2(INP_RECVFLOWID);
2014 case IPV6_RSSBUCKETID:
2016 rss_hash2bucket(in6p->inp_flowid,
2020 optval = rss_bucket;
2025 case IPV6_RECVRSSBUCKETID:
2026 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2030 case IPV6_BINDMULTI:
2031 optval = OPTBIT2(INP_BINDMULTI);
2037 error = sooptcopyout(sopt, &optval,
2044 struct ip6_mtuinfo mtuinfo;
2046 if (!(so->so_state & SS_ISCONNECTED))
2049 * XXX: we dot not consider the case of source
2050 * routing, or optional information to specify
2051 * the outgoing interface.
2053 error = ip6_getpmtu_ctl(so->so_fibnum,
2054 &in6p->in6p_faddr, &pmtu);
2057 if (pmtu > IPV6_MAXPACKET)
2058 pmtu = IPV6_MAXPACKET;
2060 bzero(&mtuinfo, sizeof(mtuinfo));
2061 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2062 optdata = (void *)&mtuinfo;
2063 optdatalen = sizeof(mtuinfo);
2064 error = sooptcopyout(sopt, optdata,
2069 case IPV6_2292PKTINFO:
2070 case IPV6_2292HOPLIMIT:
2071 case IPV6_2292HOPOPTS:
2072 case IPV6_2292RTHDR:
2073 case IPV6_2292DSTOPTS:
2075 case IPV6_2292PKTINFO:
2076 optval = OPTBIT(IN6P_PKTINFO);
2078 case IPV6_2292HOPLIMIT:
2079 optval = OPTBIT(IN6P_HOPLIMIT);
2081 case IPV6_2292HOPOPTS:
2082 optval = OPTBIT(IN6P_HOPOPTS);
2084 case IPV6_2292RTHDR:
2085 optval = OPTBIT(IN6P_RTHDR);
2087 case IPV6_2292DSTOPTS:
2088 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2091 error = sooptcopyout(sopt, &optval,
2098 case IPV6_RTHDRDSTOPTS:
2102 case IPV6_USE_MIN_MTU:
2103 case IPV6_PREFER_TEMPADDR:
2104 error = ip6_getpcbopt(in6p->in6p_outputopts,
2108 case IPV6_MULTICAST_IF:
2109 case IPV6_MULTICAST_HOPS:
2110 case IPV6_MULTICAST_LOOP:
2112 error = ip6_getmoptions(in6p, sopt);
2116 case IPV6_IPSEC_POLICY:
2120 struct mbuf *m = NULL;
2121 struct mbuf **mp = &m;
2122 size_t ovalsize = sopt->sopt_valsize;
2123 caddr_t oval = (caddr_t)sopt->sopt_val;
2125 error = soopt_getm(sopt, &m); /* XXX */
2128 error = soopt_mcopyin(sopt, m); /* XXX */
2131 sopt->sopt_valsize = ovalsize;
2132 sopt->sopt_val = oval;
2134 req = mtod(m, caddr_t);
2137 error = ipsec_get_policy(in6p, req, len, mp);
2139 error = soopt_mcopyout(sopt, m); /* XXX */
2140 if (error == 0 && m)
2147 error = ENOPROTOOPT;
2157 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2159 int error = 0, optval, optlen;
2160 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2161 struct inpcb *in6p = sotoinpcb(so);
2162 int level, op, optname;
2164 level = sopt->sopt_level;
2165 op = sopt->sopt_dir;
2166 optname = sopt->sopt_name;
2167 optlen = sopt->sopt_valsize;
2169 if (level != IPPROTO_IPV6) {
2176 * For ICMPv6 sockets, no modification allowed for checksum
2177 * offset, permit "no change" values to help existing apps.
2179 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2180 * for an ICMPv6 socket will fail."
2181 * The current behavior does not meet RFC3542.
2185 if (optlen != sizeof(int)) {
2189 error = sooptcopyin(sopt, &optval, sizeof(optval),
2193 if ((optval % 2) != 0) {
2194 /* the API assumes even offset values */
2196 } else if (so->so_proto->pr_protocol ==
2198 if (optval != icmp6off)
2201 in6p->in6p_cksum = optval;
2205 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2208 optval = in6p->in6p_cksum;
2210 error = sooptcopyout(sopt, &optval, sizeof(optval));
2220 error = ENOPROTOOPT;
2228 * Set up IP6 options in pcb for insertion in output packets or
2229 * specifying behavior of outgoing packets.
2232 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2233 struct socket *so, struct sockopt *sopt)
2235 struct ip6_pktopts *opt = *pktopt;
2237 struct thread *td = sopt->sopt_td;
2239 /* turn off any old options. */
2242 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2243 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2244 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2245 printf("ip6_pcbopts: all specified options are cleared.\n");
2247 ip6_clearpktopts(opt, -1);
2249 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2252 if (!m || m->m_len == 0) {
2254 * Only turning off any previous options, regardless of
2255 * whether the opt is just created or given.
2257 free(opt, M_IP6OPT);
2261 /* set options specified by user. */
2262 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2263 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2264 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2265 free(opt, M_IP6OPT);
2273 * initialize ip6_pktopts. beware that there are non-zero default values in
2277 ip6_initpktopts(struct ip6_pktopts *opt)
2280 bzero(opt, sizeof(*opt));
2281 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2282 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2283 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2284 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2288 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2289 struct ucred *cred, int uproto)
2291 struct ip6_pktopts *opt;
2293 if (*pktopt == NULL) {
2294 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2296 ip6_initpktopts(*pktopt);
2300 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2304 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2306 void *optdata = NULL;
2308 struct ip6_ext *ip6e;
2310 struct in6_pktinfo null_pktinfo;
2311 int deftclass = 0, on;
2312 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2313 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2317 optdata = (void *)&null_pktinfo;
2318 if (pktopt && pktopt->ip6po_pktinfo) {
2319 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2320 sizeof(null_pktinfo));
2321 in6_clearscope(&null_pktinfo.ipi6_addr);
2323 /* XXX: we don't have to do this every time... */
2324 bzero(&null_pktinfo, sizeof(null_pktinfo));
2326 optdatalen = sizeof(struct in6_pktinfo);
2329 if (pktopt && pktopt->ip6po_tclass >= 0)
2330 optdata = (void *)&pktopt->ip6po_tclass;
2332 optdata = (void *)&deftclass;
2333 optdatalen = sizeof(int);
2336 if (pktopt && pktopt->ip6po_hbh) {
2337 optdata = (void *)pktopt->ip6po_hbh;
2338 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2339 optdatalen = (ip6e->ip6e_len + 1) << 3;
2343 if (pktopt && pktopt->ip6po_rthdr) {
2344 optdata = (void *)pktopt->ip6po_rthdr;
2345 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2346 optdatalen = (ip6e->ip6e_len + 1) << 3;
2349 case IPV6_RTHDRDSTOPTS:
2350 if (pktopt && pktopt->ip6po_dest1) {
2351 optdata = (void *)pktopt->ip6po_dest1;
2352 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2353 optdatalen = (ip6e->ip6e_len + 1) << 3;
2357 if (pktopt && pktopt->ip6po_dest2) {
2358 optdata = (void *)pktopt->ip6po_dest2;
2359 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2360 optdatalen = (ip6e->ip6e_len + 1) << 3;
2364 if (pktopt && pktopt->ip6po_nexthop) {
2365 optdata = (void *)pktopt->ip6po_nexthop;
2366 optdatalen = pktopt->ip6po_nexthop->sa_len;
2369 case IPV6_USE_MIN_MTU:
2371 optdata = (void *)&pktopt->ip6po_minmtu;
2373 optdata = (void *)&defminmtu;
2374 optdatalen = sizeof(int);
2377 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2381 optdata = (void *)&on;
2382 optdatalen = sizeof(on);
2384 case IPV6_PREFER_TEMPADDR:
2386 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2388 optdata = (void *)&defpreftemp;
2389 optdatalen = sizeof(int);
2391 default: /* should not happen */
2393 panic("ip6_getpcbopt: unexpected option\n");
2395 return (ENOPROTOOPT);
2398 error = sooptcopyout(sopt, optdata, optdatalen);
2404 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2409 if (optname == -1 || optname == IPV6_PKTINFO) {
2410 if (pktopt->ip6po_pktinfo)
2411 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2412 pktopt->ip6po_pktinfo = NULL;
2414 if (optname == -1 || optname == IPV6_HOPLIMIT)
2415 pktopt->ip6po_hlim = -1;
2416 if (optname == -1 || optname == IPV6_TCLASS)
2417 pktopt->ip6po_tclass = -1;
2418 if (optname == -1 || optname == IPV6_NEXTHOP) {
2419 if (pktopt->ip6po_nextroute.ro_rt) {
2420 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2421 pktopt->ip6po_nextroute.ro_rt = NULL;
2423 if (pktopt->ip6po_nexthop)
2424 free(pktopt->ip6po_nexthop, M_IP6OPT);
2425 pktopt->ip6po_nexthop = NULL;
2427 if (optname == -1 || optname == IPV6_HOPOPTS) {
2428 if (pktopt->ip6po_hbh)
2429 free(pktopt->ip6po_hbh, M_IP6OPT);
2430 pktopt->ip6po_hbh = NULL;
2432 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2433 if (pktopt->ip6po_dest1)
2434 free(pktopt->ip6po_dest1, M_IP6OPT);
2435 pktopt->ip6po_dest1 = NULL;
2437 if (optname == -1 || optname == IPV6_RTHDR) {
2438 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2439 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2440 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2441 if (pktopt->ip6po_route.ro_rt) {
2442 RTFREE(pktopt->ip6po_route.ro_rt);
2443 pktopt->ip6po_route.ro_rt = NULL;
2446 if (optname == -1 || optname == IPV6_DSTOPTS) {
2447 if (pktopt->ip6po_dest2)
2448 free(pktopt->ip6po_dest2, M_IP6OPT);
2449 pktopt->ip6po_dest2 = NULL;
2453 #define PKTOPT_EXTHDRCPY(type) \
2456 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2457 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2458 if (dst->type == NULL && canwait == M_NOWAIT)\
2460 bcopy(src->type, dst->type, hlen);\
2462 } while (/*CONSTCOND*/ 0)
2465 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2467 if (dst == NULL || src == NULL) {
2468 printf("ip6_clearpktopts: invalid argument\n");
2472 dst->ip6po_hlim = src->ip6po_hlim;
2473 dst->ip6po_tclass = src->ip6po_tclass;
2474 dst->ip6po_flags = src->ip6po_flags;
2475 dst->ip6po_minmtu = src->ip6po_minmtu;
2476 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2477 if (src->ip6po_pktinfo) {
2478 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2480 if (dst->ip6po_pktinfo == NULL)
2482 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2484 if (src->ip6po_nexthop) {
2485 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2487 if (dst->ip6po_nexthop == NULL)
2489 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2490 src->ip6po_nexthop->sa_len);
2492 PKTOPT_EXTHDRCPY(ip6po_hbh);
2493 PKTOPT_EXTHDRCPY(ip6po_dest1);
2494 PKTOPT_EXTHDRCPY(ip6po_dest2);
2495 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2499 ip6_clearpktopts(dst, -1);
2502 #undef PKTOPT_EXTHDRCPY
2504 struct ip6_pktopts *
2505 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2508 struct ip6_pktopts *dst;
2510 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2513 ip6_initpktopts(dst);
2515 if ((error = copypktopts(dst, src, canwait)) != 0) {
2516 free(dst, M_IP6OPT);
2524 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2529 ip6_clearpktopts(pktopt, -1);
2531 free(pktopt, M_IP6OPT);
2535 * Set IPv6 outgoing packet options based on advanced API.
2538 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2539 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2541 struct cmsghdr *cm = NULL;
2543 if (control == NULL || opt == NULL)
2546 ip6_initpktopts(opt);
2551 * If stickyopt is provided, make a local copy of the options
2552 * for this particular packet, then override them by ancillary
2554 * XXX: copypktopts() does not copy the cached route to a next
2555 * hop (if any). This is not very good in terms of efficiency,
2556 * but we can allow this since this option should be rarely
2559 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2564 * XXX: Currently, we assume all the optional information is stored
2567 if (control->m_next)
2570 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2571 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2574 if (control->m_len < CMSG_LEN(0))
2577 cm = mtod(control, struct cmsghdr *);
2578 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2580 if (cm->cmsg_level != IPPROTO_IPV6)
2583 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2584 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2593 * Set a particular packet option, as a sticky option or an ancillary data
2594 * item. "len" can be 0 only when it's a sticky option.
2595 * We have 4 cases of combination of "sticky" and "cmsg":
2596 * "sticky=0, cmsg=0": impossible
2597 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2598 * "sticky=1, cmsg=0": RFC3542 socket option
2599 * "sticky=1, cmsg=1": RFC2292 socket option
2602 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2603 struct ucred *cred, int sticky, int cmsg, int uproto)
2605 int minmtupolicy, preftemp;
2608 if (!sticky && !cmsg) {
2610 printf("ip6_setpktopt: impossible case\n");
2616 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2617 * not be specified in the context of RFC3542. Conversely,
2618 * RFC3542 types should not be specified in the context of RFC2292.
2622 case IPV6_2292PKTINFO:
2623 case IPV6_2292HOPLIMIT:
2624 case IPV6_2292NEXTHOP:
2625 case IPV6_2292HOPOPTS:
2626 case IPV6_2292DSTOPTS:
2627 case IPV6_2292RTHDR:
2628 case IPV6_2292PKTOPTIONS:
2629 return (ENOPROTOOPT);
2632 if (sticky && cmsg) {
2639 case IPV6_RTHDRDSTOPTS:
2641 case IPV6_USE_MIN_MTU:
2644 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2645 return (ENOPROTOOPT);
2650 case IPV6_2292PKTINFO:
2653 struct ifnet *ifp = NULL;
2654 struct in6_pktinfo *pktinfo;
2656 if (len != sizeof(struct in6_pktinfo))
2659 pktinfo = (struct in6_pktinfo *)buf;
2662 * An application can clear any sticky IPV6_PKTINFO option by
2663 * doing a "regular" setsockopt with ipi6_addr being
2664 * in6addr_any and ipi6_ifindex being zero.
2665 * [RFC 3542, Section 6]
2667 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2668 pktinfo->ipi6_ifindex == 0 &&
2669 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2670 ip6_clearpktopts(opt, optname);
2674 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2675 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2678 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2680 /* validate the interface index if specified. */
2681 if (pktinfo->ipi6_ifindex > V_if_index)
2683 if (pktinfo->ipi6_ifindex) {
2684 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2688 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2689 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2693 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2694 struct in6_ifaddr *ia;
2696 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2697 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2699 return (EADDRNOTAVAIL);
2700 ifa_free(&ia->ia_ifa);
2703 * We store the address anyway, and let in6_selectsrc()
2704 * validate the specified address. This is because ipi6_addr
2705 * may not have enough information about its scope zone, and
2706 * we may need additional information (such as outgoing
2707 * interface or the scope zone of a destination address) to
2708 * disambiguate the scope.
2709 * XXX: the delay of the validation may confuse the
2710 * application when it is used as a sticky option.
2712 if (opt->ip6po_pktinfo == NULL) {
2713 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2714 M_IP6OPT, M_NOWAIT);
2715 if (opt->ip6po_pktinfo == NULL)
2718 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2722 case IPV6_2292HOPLIMIT:
2728 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2729 * to simplify the ordering among hoplimit options.
2731 if (optname == IPV6_HOPLIMIT && sticky)
2732 return (ENOPROTOOPT);
2734 if (len != sizeof(int))
2737 if (*hlimp < -1 || *hlimp > 255)
2740 opt->ip6po_hlim = *hlimp;
2748 if (len != sizeof(int))
2750 tclass = *(int *)buf;
2751 if (tclass < -1 || tclass > 255)
2754 opt->ip6po_tclass = tclass;
2758 case IPV6_2292NEXTHOP:
2761 error = priv_check_cred(cred,
2762 PRIV_NETINET_SETHDROPTS, 0);
2767 if (len == 0) { /* just remove the option */
2768 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2772 /* check if cmsg_len is large enough for sa_len */
2773 if (len < sizeof(struct sockaddr) || len < *buf)
2776 switch (((struct sockaddr *)buf)->sa_family) {
2779 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2782 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2785 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2786 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2789 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2795 case AF_LINK: /* should eventually be supported */
2797 return (EAFNOSUPPORT);
2800 /* turn off the previous option, then set the new option. */
2801 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2802 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2803 if (opt->ip6po_nexthop == NULL)
2805 bcopy(buf, opt->ip6po_nexthop, *buf);
2808 case IPV6_2292HOPOPTS:
2811 struct ip6_hbh *hbh;
2815 * XXX: We don't allow a non-privileged user to set ANY HbH
2816 * options, since per-option restriction has too much
2820 error = priv_check_cred(cred,
2821 PRIV_NETINET_SETHDROPTS, 0);
2827 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2828 break; /* just remove the option */
2831 /* message length validation */
2832 if (len < sizeof(struct ip6_hbh))
2834 hbh = (struct ip6_hbh *)buf;
2835 hbhlen = (hbh->ip6h_len + 1) << 3;
2839 /* turn off the previous option, then set the new option. */
2840 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2841 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2842 if (opt->ip6po_hbh == NULL)
2844 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2849 case IPV6_2292DSTOPTS:
2851 case IPV6_RTHDRDSTOPTS:
2853 struct ip6_dest *dest, **newdest = NULL;
2856 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2857 error = priv_check_cred(cred,
2858 PRIV_NETINET_SETHDROPTS, 0);
2864 ip6_clearpktopts(opt, optname);
2865 break; /* just remove the option */
2868 /* message length validation */
2869 if (len < sizeof(struct ip6_dest))
2871 dest = (struct ip6_dest *)buf;
2872 destlen = (dest->ip6d_len + 1) << 3;
2877 * Determine the position that the destination options header
2878 * should be inserted; before or after the routing header.
2881 case IPV6_2292DSTOPTS:
2883 * The old advacned API is ambiguous on this point.
2884 * Our approach is to determine the position based
2885 * according to the existence of a routing header.
2886 * Note, however, that this depends on the order of the
2887 * extension headers in the ancillary data; the 1st
2888 * part of the destination options header must appear
2889 * before the routing header in the ancillary data,
2891 * RFC3542 solved the ambiguity by introducing
2892 * separate ancillary data or option types.
2894 if (opt->ip6po_rthdr == NULL)
2895 newdest = &opt->ip6po_dest1;
2897 newdest = &opt->ip6po_dest2;
2899 case IPV6_RTHDRDSTOPTS:
2900 newdest = &opt->ip6po_dest1;
2903 newdest = &opt->ip6po_dest2;
2907 /* turn off the previous option, then set the new option. */
2908 ip6_clearpktopts(opt, optname);
2909 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2910 if (*newdest == NULL)
2912 bcopy(dest, *newdest, destlen);
2917 case IPV6_2292RTHDR:
2920 struct ip6_rthdr *rth;
2924 ip6_clearpktopts(opt, IPV6_RTHDR);
2925 break; /* just remove the option */
2928 /* message length validation */
2929 if (len < sizeof(struct ip6_rthdr))
2931 rth = (struct ip6_rthdr *)buf;
2932 rthlen = (rth->ip6r_len + 1) << 3;
2936 switch (rth->ip6r_type) {
2937 case IPV6_RTHDR_TYPE_0:
2938 if (rth->ip6r_len == 0) /* must contain one addr */
2940 if (rth->ip6r_len % 2) /* length must be even */
2942 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2946 return (EINVAL); /* not supported */
2949 /* turn off the previous option */
2950 ip6_clearpktopts(opt, IPV6_RTHDR);
2951 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2952 if (opt->ip6po_rthdr == NULL)
2954 bcopy(rth, opt->ip6po_rthdr, rthlen);
2959 case IPV6_USE_MIN_MTU:
2960 if (len != sizeof(int))
2962 minmtupolicy = *(int *)buf;
2963 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2964 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2965 minmtupolicy != IP6PO_MINMTU_ALL) {
2968 opt->ip6po_minmtu = minmtupolicy;
2972 if (len != sizeof(int))
2975 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2977 * we ignore this option for TCP sockets.
2978 * (RFC3542 leaves this case unspecified.)
2980 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2982 opt->ip6po_flags |= IP6PO_DONTFRAG;
2985 case IPV6_PREFER_TEMPADDR:
2986 if (len != sizeof(int))
2988 preftemp = *(int *)buf;
2989 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2990 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2991 preftemp != IP6PO_TEMPADDR_PREFER) {
2994 opt->ip6po_prefer_tempaddr = preftemp;
2998 return (ENOPROTOOPT);
2999 } /* end of switch */
3005 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3006 * packet to the input queue of a specified interface. Note that this
3007 * calls the output routine of the loopback "driver", but with an interface
3008 * pointer that might NOT be &loif -- easier than replicating that code here.
3011 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3014 struct ip6_hdr *ip6;
3016 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3021 * Make sure to deep-copy IPv6 header portion in case the data
3022 * is in an mbuf cluster, so that we can safely override the IPv6
3023 * header portion later.
3025 if (!M_WRITABLE(copym) ||
3026 copym->m_len < sizeof(struct ip6_hdr)) {
3027 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3031 ip6 = mtod(copym, struct ip6_hdr *);
3033 * clear embedded scope identifiers if necessary.
3034 * in6_clearscope will touch the addresses only when necessary.
3036 in6_clearscope(&ip6->ip6_src);
3037 in6_clearscope(&ip6->ip6_dst);
3038 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3039 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3041 copym->m_pkthdr.csum_data = 0xffff;
3043 if_simloop(ifp, copym, AF_INET6, 0);
3047 * Chop IPv6 header off from the payload.
3050 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3053 struct ip6_hdr *ip6;
3055 ip6 = mtod(m, struct ip6_hdr *);
3056 if (m->m_len > sizeof(*ip6)) {
3057 mh = m_gethdr(M_NOWAIT, MT_DATA);
3062 m_move_pkthdr(mh, m);
3063 M_ALIGN(mh, sizeof(*ip6));
3064 m->m_len -= sizeof(*ip6);
3065 m->m_data += sizeof(*ip6);
3068 m->m_len = sizeof(*ip6);
3069 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3071 exthdrs->ip6e_ip6 = m;
3076 * Compute IPv6 extension header length.
3079 ip6_optlen(struct inpcb *in6p)
3083 if (!in6p->in6p_outputopts)
3088 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3090 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3091 if (in6p->in6p_outputopts->ip6po_rthdr)
3092 /* dest1 is valid with rthdr only */
3093 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3094 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3095 len += elen(in6p->in6p_outputopts->ip6po_dest2);