2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
71 #include "opt_route.h"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/syslog.h>
84 #include <sys/ucred.h>
86 #include <machine/in_cksum.h>
89 #include <net/netisr.h>
90 #include <net/route.h>
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97 #include <netinet6/in6_var.h>
98 #include <netinet/ip6.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/tcp_var.h>
103 #include <netinet6/nd6.h>
106 #include <netipsec/ipsec.h>
107 #include <netipsec/ipsec6.h>
108 #include <netipsec/key.h>
109 #include <netinet6/ip6_ipsec.h>
112 #include <netinet/sctp.h>
113 #include <netinet/sctp_crc32.h>
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/scope6_var.h>
120 #include <net/flowtable.h>
123 extern int in6_mcast_loop;
126 struct mbuf *ip6e_ip6;
127 struct mbuf *ip6e_hbh;
128 struct mbuf *ip6e_dest1;
129 struct mbuf *ip6e_rthdr;
130 struct mbuf *ip6e_dest2;
133 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
134 struct ucred *, int);
135 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
136 struct socket *, struct sockopt *);
137 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
138 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
139 struct ucred *, int, int, int);
141 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
142 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
144 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
145 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
146 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
147 struct ifnet *, struct in6_addr *, u_long *, int *, u_int);
148 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
152 * Make an extension header from option data. hp is the source, and
153 * mp is the destination.
155 #define MAKE_EXTHDR(hp, mp) \
158 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
159 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
160 ((eh)->ip6e_len + 1) << 3); \
164 } while (/*CONSTCOND*/ 0)
167 * Form a chain of extension headers.
168 * m is the extension header mbuf
169 * mp is the previous mbuf in the chain
170 * p is the next header
171 * i is the type of option.
173 #define MAKE_CHAIN(m, mp, p, i)\
177 panic("assumption failed: hdr not split"); \
178 *mtod((m), u_char *) = *(p);\
180 p = mtod((m), u_char *);\
181 (m)->m_next = (mp)->m_next;\
185 } while (/*CONSTCOND*/ 0)
188 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
192 csum = in_cksum_skip(m, offset + plen, offset);
193 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
195 offset += m->m_pkthdr.csum_data; /* checksum offset */
197 if (offset + sizeof(u_short) > m->m_len) {
198 printf("%s: delayed m_pullup, m->len: %d plen %u off %u "
199 "csum_flags=%b\n", __func__, m->m_len, plen, offset,
200 (int)m->m_pkthdr.csum_flags, CSUM_BITS);
202 * XXX this should not happen, but if it does, the correct
203 * behavior may be to insert the checksum in the appropriate
204 * next mbuf in the chain.
208 *(u_short *)(m->m_data + offset) = csum;
212 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
213 int mtu, uint32_t id)
215 struct mbuf *m, **mnext, *m_frgpart;
216 struct ip6_hdr *ip6, *mhip6;
217 struct ip6_frag *ip6f;
220 int tlen = m0->m_pkthdr.len;
222 KASSERT(( mtu % 8 == 0), ("Fragment length must be a multiple of 8"));
225 ip6 = mtod(m, struct ip6_hdr *);
226 mnext = &m->m_nextpkt;
228 for (off = hlen; off < tlen; off += mtu) {
229 m = m_gethdr(M_NOWAIT, MT_DATA);
231 IP6STAT_INC(ip6s_odropped);
234 m->m_flags = m0->m_flags & M_COPYFLAGS;
236 mnext = &m->m_nextpkt;
237 m->m_data += max_linkhdr;
238 mhip6 = mtod(m, struct ip6_hdr *);
240 m->m_len = sizeof(*mhip6);
241 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
243 IP6STAT_INC(ip6s_odropped);
246 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
247 if (off + mtu >= tlen)
250 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
251 mhip6->ip6_plen = htons((u_short)(mtu + hlen +
252 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
253 if ((m_frgpart = m_copy(m0, off, mtu)) == 0) {
254 IP6STAT_INC(ip6s_odropped);
258 m->m_pkthdr.len = mtu + hlen + sizeof(*ip6f);
259 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
260 m->m_pkthdr.rcvif = NULL;
261 ip6f->ip6f_reserved = 0;
262 ip6f->ip6f_ident = id;
263 ip6f->ip6f_nxt = nextproto;
264 IP6STAT_INC(ip6s_ofragments);
265 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
272 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
273 * header (with pri, len, nxt, hlim, src, dst).
274 * This function may modify ver and hlim only.
275 * The mbuf chain containing the packet will be freed.
276 * The mbuf opt, if present, will not be freed.
277 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
278 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
279 * then result of route lookup is stored in ro->ro_rt.
281 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
282 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
285 * ifpp - XXX: just for statistics
288 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
289 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
290 struct ifnet **ifpp, struct inpcb *inp)
293 struct ifnet *ifp, *origifp;
295 struct mbuf *mprev = NULL;
297 struct route_in6 ip6route;
298 struct rtentry *rt = NULL;
299 struct sockaddr_in6 *dst, src_sa, dst_sa;
300 struct in6_addr odst;
302 struct in6_ifaddr *ia = NULL;
304 int alwaysfrag, dontfrag;
305 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
306 struct ip6_exthdrs exthdrs;
307 struct in6_addr finaldst, src0, dst0;
309 struct route_in6 *ro_pmtu = NULL;
312 struct m_tag *fwd_tag = NULL;
315 ip6 = mtod(m, struct ip6_hdr *);
317 printf ("ip6 is NULL");
322 M_SETFIB(m, inp->inp_inc.inc_fibnum);
324 finaldst = ip6->ip6_dst;
325 bzero(&exthdrs, sizeof(exthdrs));
327 /* Hop-by-Hop options header */
328 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
329 /* Destination options header(1st part) */
330 if (opt->ip6po_rthdr) {
332 * Destination options header(1st part)
333 * This only makes sense with a routing header.
334 * See Section 9.2 of RFC 3542.
335 * Disabling this part just for MIP6 convenience is
336 * a bad idea. We need to think carefully about a
337 * way to make the advanced API coexist with MIP6
338 * options, which might automatically be inserted in
341 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
344 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
345 /* Destination options header(2nd part) */
346 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
351 * IPSec checking which handles several cases.
352 * FAST IPSEC: We re-injected the packet.
354 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp))
356 case 1: /* Bad packet */
358 case -1: /* IPSec done */
360 case 0: /* No IPSec */
367 * Calculate the total length of the extension header chain.
368 * Keep the length of the unfragmentable part for fragmentation.
371 if (exthdrs.ip6e_hbh)
372 optlen += exthdrs.ip6e_hbh->m_len;
373 if (exthdrs.ip6e_dest1)
374 optlen += exthdrs.ip6e_dest1->m_len;
375 if (exthdrs.ip6e_rthdr)
376 optlen += exthdrs.ip6e_rthdr->m_len;
377 unfragpartlen = optlen + sizeof(struct ip6_hdr);
379 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
380 if (exthdrs.ip6e_dest2)
381 optlen += exthdrs.ip6e_dest2->m_len;
384 * If there is at least one extension header,
385 * separate IP6 header from the payload.
387 if (optlen && !hdrsplit) {
388 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
392 m = exthdrs.ip6e_ip6;
397 ip6 = mtod(m, struct ip6_hdr *);
399 /* adjust mbuf packet header length */
400 m->m_pkthdr.len += optlen;
401 plen = m->m_pkthdr.len - sizeof(*ip6);
403 /* If this is a jumbo payload, insert a jumbo payload option. */
404 if (plen > IPV6_MAXPACKET) {
406 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
410 m = exthdrs.ip6e_ip6;
414 ip6 = mtod(m, struct ip6_hdr *);
415 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
419 ip6->ip6_plen = htons(plen);
422 * Concatenate headers and fill in next header fields.
423 * Here we have, on "m"
425 * and we insert headers accordingly. Finally, we should be getting:
426 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
428 * during the header composing process, "m" points to IPv6 header.
429 * "mprev" points to an extension header prior to esp.
431 u_char *nexthdrp = &ip6->ip6_nxt;
435 * we treat dest2 specially. this makes IPsec processing
436 * much easier. the goal here is to make mprev point the
437 * mbuf prior to dest2.
439 * result: IPv6 dest2 payload
440 * m and mprev will point to IPv6 header.
442 if (exthdrs.ip6e_dest2) {
444 panic("assumption failed: hdr not split");
445 exthdrs.ip6e_dest2->m_next = m->m_next;
446 m->m_next = exthdrs.ip6e_dest2;
447 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
448 ip6->ip6_nxt = IPPROTO_DSTOPTS;
452 * result: IPv6 hbh dest1 rthdr dest2 payload
453 * m will point to IPv6 header. mprev will point to the
454 * extension header prior to dest2 (rthdr in the above case).
456 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
457 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
459 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
463 * If there is a routing header, discard the packet.
465 if (exthdrs.ip6e_rthdr) {
470 /* Source address validation */
471 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
472 (flags & IPV6_UNSPECSRC) == 0) {
474 IP6STAT_INC(ip6s_badscope);
477 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
479 IP6STAT_INC(ip6s_badscope);
483 IP6STAT_INC(ip6s_localout);
490 bzero((caddr_t)ro, sizeof(*ro));
493 if (opt && opt->ip6po_rthdr)
494 ro = &opt->ip6po_route;
495 dst = (struct sockaddr_in6 *)&ro->ro_dst;
497 if (ro->ro_rt == NULL)
498 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
502 * if specified, try to fill in the traffic class field.
503 * do not override if a non-zero value is already set.
504 * we check the diffserv field and the ecn field separately.
506 if (opt && opt->ip6po_tclass >= 0) {
509 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
511 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
514 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
517 /* fill in or override the hop limit field, if necessary. */
518 if (opt && opt->ip6po_hlim != -1)
519 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
520 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
522 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
524 ip6->ip6_hlim = V_ip6_defmcasthlim;
528 ip6 = mtod(m, struct ip6_hdr *);
530 if (ro->ro_rt && fwd_tag == NULL) {
532 ifp = ro->ro_rt->rt_ifp;
534 if (fwd_tag == NULL) {
535 bzero(&dst_sa, sizeof(dst_sa));
536 dst_sa.sin6_family = AF_INET6;
537 dst_sa.sin6_len = sizeof(dst_sa);
538 dst_sa.sin6_addr = ip6->ip6_dst;
540 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
541 &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
544 in6_ifstat_inc(ifp, ifs6_out_discard);
550 * If in6_selectroute() does not return a route entry,
551 * dst may not have been updated.
553 *dst = dst_sa; /* XXX */
557 * then rt (for unicast) and ifp must be non-NULL valid values.
559 if ((flags & IPV6_FORWARDING) == 0) {
560 /* XXX: the FORWARDING flag can be set for mrouting. */
561 in6_ifstat_inc(ifp, ifs6_out_request);
564 ia = (struct in6_ifaddr *)(rt->rt_ifa);
565 counter_u64_add(rt->rt_pksent, 1);
568 /* Setup data structures for scope ID checks. */
570 bzero(&src_sa, sizeof(src_sa));
571 src_sa.sin6_family = AF_INET6;
572 src_sa.sin6_len = sizeof(src_sa);
573 src_sa.sin6_addr = ip6->ip6_src;
576 /* re-initialize to be sure */
577 bzero(&dst_sa, sizeof(dst_sa));
578 dst_sa.sin6_family = AF_INET6;
579 dst_sa.sin6_len = sizeof(dst_sa);
580 dst_sa.sin6_addr = ip6->ip6_dst;
582 /* Check for valid scope ID. */
583 if (in6_setscope(&src0, ifp, &zone) == 0 &&
584 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
585 in6_setscope(&dst0, ifp, &zone) == 0 &&
586 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
588 * The outgoing interface is in the zone of the source
589 * and destination addresses.
591 * Because the loopback interface cannot receive
592 * packets with a different scope ID than its own,
593 * there is a trick is to pretend the outgoing packet
594 * was received by the real network interface, by
595 * setting "origifp" different from "ifp". This is
596 * only allowed when "ifp" is a loopback network
597 * interface. Refer to code in nd6_output_ifp() for
603 * We should use ia_ifp to support the case of sending
604 * packets to an address of our own.
606 if (ia != NULL && ia->ia_ifp)
609 } else if ((ifp->if_flags & IFF_LOOPBACK) == 0 ||
610 sa6_recoverscope(&src_sa) != 0 ||
611 sa6_recoverscope(&dst_sa) != 0 ||
612 dst_sa.sin6_scope_id == 0 ||
613 (src_sa.sin6_scope_id != 0 &&
614 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
615 (origifp = ifnet_byindex(dst_sa.sin6_scope_id)) == NULL) {
617 * If the destination network interface is not a
618 * loopback interface, or the destination network
619 * address has no scope ID, or the source address has
620 * a scope ID set which is different from the
621 * destination address one, or there is no network
622 * interface representing this scope ID, the address
623 * pair is considered invalid.
625 IP6STAT_INC(ip6s_badscope);
626 in6_ifstat_inc(ifp, ifs6_out_discard);
628 error = EHOSTUNREACH; /* XXX */
632 /* All scope ID checks are successful. */
634 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
635 if (opt && opt->ip6po_nextroute.ro_rt) {
637 * The nexthop is explicitly specified by the
638 * application. We assume the next hop is an IPv6
641 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
643 else if ((rt->rt_flags & RTF_GATEWAY))
644 dst = (struct sockaddr_in6 *)rt->rt_gateway;
647 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
648 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
650 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
651 in6_ifstat_inc(ifp, ifs6_out_mcast);
653 * Confirm that the outgoing interface supports multicast.
655 if (!(ifp->if_flags & IFF_MULTICAST)) {
656 IP6STAT_INC(ip6s_noroute);
657 in6_ifstat_inc(ifp, ifs6_out_discard);
661 if ((im6o == NULL && in6_mcast_loop) ||
662 (im6o && im6o->im6o_multicast_loop)) {
664 * Loop back multicast datagram if not expressly
665 * forbidden to do so, even if we have not joined
666 * the address; protocols will filter it later,
667 * thus deferring a hash lookup and lock acquisition
668 * at the expense of an m_copym().
670 ip6_mloopback(ifp, m, dst);
673 * If we are acting as a multicast router, perform
674 * multicast forwarding as if the packet had just
675 * arrived on the interface to which we are about
676 * to send. The multicast forwarding function
677 * recursively calls this function, using the
678 * IPV6_FORWARDING flag to prevent infinite recursion.
680 * Multicasts that are looped back by ip6_mloopback(),
681 * above, will be forwarded by the ip6_input() routine,
684 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
686 * XXX: ip6_mforward expects that rcvif is NULL
687 * when it is called from the originating path.
688 * However, it may not always be the case.
690 m->m_pkthdr.rcvif = NULL;
691 if (ip6_mforward(ip6, ifp, m) != 0) {
698 * Multicasts with a hoplimit of zero may be looped back,
699 * above, but must not be transmitted on a network.
700 * Also, multicasts addressed to the loopback interface
701 * are not sent -- the above call to ip6_mloopback() will
702 * loop back a copy if this host actually belongs to the
703 * destination group on the loopback interface.
705 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
706 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
713 * Fill the outgoing inteface to tell the upper layer
714 * to increment per-interface statistics.
719 /* Determine path MTU. */
720 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
721 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
725 * The caller of this function may specify to use the minimum MTU
727 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
728 * setting. The logic is a bit complicated; by default, unicast
729 * packets will follow path MTU while multicast packets will be sent at
730 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
731 * including unicast ones will be sent at the minimum MTU. Multicast
732 * packets will always be sent at the minimum MTU unless
733 * IP6PO_MINMTU_DISABLE is explicitly specified.
734 * See RFC 3542 for more details.
736 if (mtu > IPV6_MMTU) {
737 if ((flags & IPV6_MINMTU))
739 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
741 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
743 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
749 * clear embedded scope identifiers if necessary.
750 * in6_clearscope will touch the addresses only when necessary.
752 in6_clearscope(&ip6->ip6_src);
753 in6_clearscope(&ip6->ip6_dst);
756 * If the outgoing packet contains a hop-by-hop options header,
757 * it must be examined and processed even by the source node.
758 * (RFC 2460, section 4.)
760 if (exthdrs.ip6e_hbh) {
761 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
762 u_int32_t dummy; /* XXX unused */
763 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
766 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
767 panic("ip6e_hbh is not contiguous");
770 * XXX: if we have to send an ICMPv6 error to the sender,
771 * we need the M_LOOP flag since icmp6_error() expects
772 * the IPv6 and the hop-by-hop options header are
773 * contiguous unless the flag is set.
775 m->m_flags |= M_LOOP;
776 m->m_pkthdr.rcvif = ifp;
777 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
778 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
779 &dummy, &plen) < 0) {
780 /* m was already freed at this point */
781 error = EINVAL;/* better error? */
784 m->m_flags &= ~M_LOOP; /* XXX */
785 m->m_pkthdr.rcvif = NULL;
788 /* Jump over all PFIL processing if hooks are not active. */
789 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
793 /* Run through list of hooks for output packets. */
794 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
795 if (error != 0 || m == NULL)
797 ip6 = mtod(m, struct ip6_hdr *);
799 /* See if destination IP address was changed by packet filter. */
800 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
801 m->m_flags |= M_SKIP_FIREWALL;
802 /* If destination is now ourself drop to ip6_input(). */
803 if (in6_localip(&ip6->ip6_dst)) {
804 m->m_flags |= M_FASTFWD_OURS;
805 if (m->m_pkthdr.rcvif == NULL)
806 m->m_pkthdr.rcvif = V_loif;
807 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
808 m->m_pkthdr.csum_flags |=
809 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
810 m->m_pkthdr.csum_data = 0xffff;
813 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
814 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
816 error = netisr_queue(NETISR_IPV6, m);
819 goto again; /* Redo the routing table lookup. */
822 /* See if local, if yes, send it to netisr. */
823 if (m->m_flags & M_FASTFWD_OURS) {
824 if (m->m_pkthdr.rcvif == NULL)
825 m->m_pkthdr.rcvif = V_loif;
826 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
827 m->m_pkthdr.csum_flags |=
828 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
829 m->m_pkthdr.csum_data = 0xffff;
832 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
833 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
835 error = netisr_queue(NETISR_IPV6, m);
838 /* Or forward to some other address? */
839 if ((m->m_flags & M_IP6_NEXTHOP) &&
840 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
841 dst = (struct sockaddr_in6 *)&ro->ro_dst;
842 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
843 m->m_flags |= M_SKIP_FIREWALL;
844 m->m_flags &= ~M_IP6_NEXTHOP;
845 m_tag_delete(m, fwd_tag);
851 * Send the packet to the outgoing interface.
852 * If necessary, do IPv6 fragmentation before sending.
854 * the logic here is rather complex:
855 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
856 * 1-a: send as is if tlen <= path mtu
857 * 1-b: fragment if tlen > path mtu
859 * 2: if user asks us not to fragment (dontfrag == 1)
860 * 2-a: send as is if tlen <= interface mtu
861 * 2-b: error if tlen > interface mtu
863 * 3: if we always need to attach fragment header (alwaysfrag == 1)
866 * 4: if dontfrag == 1 && alwaysfrag == 1
867 * error, as we cannot handle this conflicting request
869 sw_csum = m->m_pkthdr.csum_flags;
871 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
872 sw_csum &= ~ifp->if_hwassist;
876 * If we added extension headers, we will not do TSO and calculate the
877 * checksums ourselves for now.
878 * XXX-BZ Need a framework to know when the NIC can handle it, even
881 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
882 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
883 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
886 if (sw_csum & CSUM_SCTP_IPV6) {
887 sw_csum &= ~CSUM_SCTP_IPV6;
888 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
891 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
892 tlen = m->m_pkthdr.len;
894 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
898 if (dontfrag && alwaysfrag) { /* case 4 */
899 /* conflicting request - can't transmit */
903 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
905 * Even if the DONTFRAG option is specified, we cannot send the
906 * packet when the data length is larger than the MTU of the
907 * outgoing interface.
908 * Notify the error by sending IPV6_PATHMTU ancillary data if
909 * application wanted to know the MTU value. Also return an
910 * error code (this is not described in the API spec).
913 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
919 * transmit packet without fragmentation
921 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
922 struct in6_ifaddr *ia6;
924 ip6 = mtod(m, struct ip6_hdr *);
925 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
927 /* Record statistics for this interface address. */
928 ia6->ia_ifa.if_opackets++;
929 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
930 ifa_free(&ia6->ia_ifa);
932 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
937 * try to fragment the packet. case 1-b and 3
939 if (mtu < IPV6_MMTU) {
940 /* path MTU cannot be less than IPV6_MMTU */
942 in6_ifstat_inc(ifp, ifs6_out_fragfail);
944 } else if (ip6->ip6_plen == 0) {
945 /* jumbo payload cannot be fragmented */
947 in6_ifstat_inc(ifp, ifs6_out_fragfail);
952 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
955 * Too large for the destination or interface;
956 * fragment if possible.
957 * Must be able to put at least 8 bytes per fragment.
959 hlen = unfragpartlen;
960 if (mtu > IPV6_MAXPACKET)
961 mtu = IPV6_MAXPACKET;
963 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
966 in6_ifstat_inc(ifp, ifs6_out_fragfail);
971 * Verify that we have any chance at all of being able to queue
972 * the packet or packet fragments
974 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
975 < tlen /* - hlen */)) {
977 IP6STAT_INC(ip6s_odropped);
983 * If the interface will not calculate checksums on
984 * fragmented packets, then do it here.
985 * XXX-BZ handle the hw offloading case. Need flags.
987 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
988 in6_delayed_cksum(m, plen, hlen);
989 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
992 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
993 sctp_delayed_cksum(m, hlen);
994 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
998 * Change the next header field of the last header in the
999 * unfragmentable part.
1001 if (exthdrs.ip6e_rthdr) {
1002 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1003 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1004 } else if (exthdrs.ip6e_dest1) {
1005 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1006 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1007 } else if (exthdrs.ip6e_hbh) {
1008 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1009 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1011 nextproto = ip6->ip6_nxt;
1012 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1016 * Loop through length of segment after first fragment,
1017 * make new header and copy data of each part and link onto
1021 id = htonl(ip6_randomid());
1022 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1025 in6_ifstat_inc(ifp, ifs6_out_fragok);
1029 * Remove leading garbages.
1035 for (m0 = m; m; m = m0) {
1039 /* Record statistics for this interface address. */
1041 ia->ia_ifa.if_opackets++;
1042 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1044 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1050 IP6STAT_INC(ip6s_fragmented);
1053 if (ro == &ip6route)
1055 if (ro_pmtu == &ip6route)
1060 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1061 m_freem(exthdrs.ip6e_dest1);
1062 m_freem(exthdrs.ip6e_rthdr);
1063 m_freem(exthdrs.ip6e_dest2);
1072 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1076 if (hlen > MCLBYTES)
1077 return (ENOBUFS); /* XXX */
1080 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1082 m = m_get(M_NOWAIT, MT_DATA);
1087 bcopy(hdr, mtod(m, caddr_t), hlen);
1094 * Insert jumbo payload option.
1097 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1103 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1106 * If there is no hop-by-hop options header, allocate new one.
1107 * If there is one but it doesn't have enough space to store the
1108 * jumbo payload option, allocate a cluster to store the whole options.
1109 * Otherwise, use it to store the options.
1111 if (exthdrs->ip6e_hbh == 0) {
1112 mopt = m_get(M_NOWAIT, MT_DATA);
1115 mopt->m_len = JUMBOOPTLEN;
1116 optbuf = mtod(mopt, u_char *);
1117 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1118 exthdrs->ip6e_hbh = mopt;
1120 struct ip6_hbh *hbh;
1122 mopt = exthdrs->ip6e_hbh;
1123 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1126 * - exthdrs->ip6e_hbh is not referenced from places
1127 * other than exthdrs.
1128 * - exthdrs->ip6e_hbh is not an mbuf chain.
1130 int oldoptlen = mopt->m_len;
1134 * XXX: give up if the whole (new) hbh header does
1135 * not fit even in an mbuf cluster.
1137 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1141 * As a consequence, we must always prepare a cluster
1144 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1147 n->m_len = oldoptlen + JUMBOOPTLEN;
1148 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1150 optbuf = mtod(n, caddr_t) + oldoptlen;
1152 mopt = exthdrs->ip6e_hbh = n;
1154 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1155 mopt->m_len += JUMBOOPTLEN;
1157 optbuf[0] = IP6OPT_PADN;
1161 * Adjust the header length according to the pad and
1162 * the jumbo payload option.
1164 hbh = mtod(mopt, struct ip6_hbh *);
1165 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1168 /* fill in the option. */
1169 optbuf[2] = IP6OPT_JUMBO;
1171 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1172 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1174 /* finally, adjust the packet header length */
1175 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1182 * Insert fragment header and copy unfragmentable header portions.
1185 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1186 struct ip6_frag **frghdrp)
1188 struct mbuf *n, *mlast;
1190 if (hlen > sizeof(struct ip6_hdr)) {
1191 n = m_copym(m0, sizeof(struct ip6_hdr),
1192 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1199 /* Search for the last mbuf of unfragmentable part. */
1200 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1203 if ((mlast->m_flags & M_EXT) == 0 &&
1204 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1205 /* use the trailing space of the last mbuf for the fragment hdr */
1206 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1208 mlast->m_len += sizeof(struct ip6_frag);
1209 m->m_pkthdr.len += sizeof(struct ip6_frag);
1211 /* allocate a new mbuf for the fragment header */
1214 mfrg = m_get(M_NOWAIT, MT_DATA);
1217 mfrg->m_len = sizeof(struct ip6_frag);
1218 *frghdrp = mtod(mfrg, struct ip6_frag *);
1219 mlast->m_next = mfrg;
1226 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1227 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1228 int *alwaysfragp, u_int fibnum)
1234 if (ro_pmtu != ro) {
1235 /* The first hop and the final destination may differ. */
1236 struct sockaddr_in6 *sa6_dst =
1237 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1238 if (ro_pmtu->ro_rt &&
1239 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1240 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1241 RTFREE(ro_pmtu->ro_rt);
1242 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1244 if (ro_pmtu->ro_rt == NULL) {
1245 bzero(sa6_dst, sizeof(*sa6_dst));
1246 sa6_dst->sin6_family = AF_INET6;
1247 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1248 sa6_dst->sin6_addr = *dst;
1250 in6_rtalloc(ro_pmtu, fibnum);
1253 if (ro_pmtu->ro_rt) {
1255 struct in_conninfo inc;
1257 bzero(&inc, sizeof(inc));
1258 inc.inc_flags |= INC_ISIPV6;
1259 inc.inc6_faddr = *dst;
1262 ifp = ro_pmtu->ro_rt->rt_ifp;
1263 ifmtu = IN6_LINKMTU(ifp);
1264 mtu = tcp_hc_getmtu(&inc);
1266 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu);
1268 mtu = ro_pmtu->ro_rt->rt_mtu;
1271 else if (mtu < IPV6_MMTU) {
1273 * RFC2460 section 5, last paragraph:
1274 * if we record ICMPv6 too big message with
1275 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1276 * or smaller, with framgent header attached.
1277 * (fragment header is needed regardless from the
1278 * packet size, for translators to identify packets)
1282 } else if (mtu > ifmtu) {
1284 * The MTU on the route is larger than the MTU on
1285 * the interface! This shouldn't happen, unless the
1286 * MTU of the interface has been changed after the
1287 * interface was brought up. Change the MTU in the
1288 * route to match the interface MTU (as long as the
1289 * field isn't locked).
1292 ro_pmtu->ro_rt->rt_mtu = mtu;
1295 mtu = IN6_LINKMTU(ifp);
1297 error = EHOSTUNREACH; /* XXX */
1301 *alwaysfragp = alwaysfrag;
1306 * IP6 socket option processing.
1309 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1311 int optdatalen, uproto;
1313 struct inpcb *in6p = sotoinpcb(so);
1315 int level, op, optname;
1319 level = sopt->sopt_level;
1320 op = sopt->sopt_dir;
1321 optname = sopt->sopt_name;
1322 optlen = sopt->sopt_valsize;
1326 uproto = (int)so->so_proto->pr_protocol;
1328 if (level != IPPROTO_IPV6) {
1331 if (sopt->sopt_level == SOL_SOCKET &&
1332 sopt->sopt_dir == SOPT_SET) {
1333 switch (sopt->sopt_name) {
1336 if ((so->so_options & SO_REUSEADDR) != 0)
1337 in6p->inp_flags2 |= INP_REUSEADDR;
1339 in6p->inp_flags2 &= ~INP_REUSEADDR;
1345 if ((so->so_options & SO_REUSEPORT) != 0)
1346 in6p->inp_flags2 |= INP_REUSEPORT;
1348 in6p->inp_flags2 &= ~INP_REUSEPORT;
1354 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1362 } else { /* level == IPPROTO_IPV6 */
1367 case IPV6_2292PKTOPTIONS:
1368 #ifdef IPV6_PKTOPTIONS
1369 case IPV6_PKTOPTIONS:
1374 error = soopt_getm(sopt, &m); /* XXX */
1377 error = soopt_mcopyin(sopt, m); /* XXX */
1380 error = ip6_pcbopts(&in6p->in6p_outputopts,
1382 m_freem(m); /* XXX */
1387 * Use of some Hop-by-Hop options or some
1388 * Destination options, might require special
1389 * privilege. That is, normal applications
1390 * (without special privilege) might be forbidden
1391 * from setting certain options in outgoing packets,
1392 * and might never see certain options in received
1393 * packets. [RFC 2292 Section 6]
1394 * KAME specific note:
1395 * KAME prevents non-privileged users from sending or
1396 * receiving ANY hbh/dst options in order to avoid
1397 * overhead of parsing options in the kernel.
1399 case IPV6_RECVHOPOPTS:
1400 case IPV6_RECVDSTOPTS:
1401 case IPV6_RECVRTHDRDSTOPTS:
1403 error = priv_check(td,
1404 PRIV_NETINET_SETHDROPTS);
1409 case IPV6_UNICAST_HOPS:
1413 case IPV6_RECVPKTINFO:
1414 case IPV6_RECVHOPLIMIT:
1415 case IPV6_RECVRTHDR:
1416 case IPV6_RECVPATHMTU:
1417 case IPV6_RECVTCLASS:
1419 case IPV6_AUTOFLOWLABEL:
1421 if (optname == IPV6_BINDANY && td != NULL) {
1422 error = priv_check(td,
1423 PRIV_NETINET_BINDANY);
1428 if (optlen != sizeof(int)) {
1432 error = sooptcopyin(sopt, &optval,
1433 sizeof optval, sizeof optval);
1438 case IPV6_UNICAST_HOPS:
1439 if (optval < -1 || optval >= 256)
1442 /* -1 = kernel default */
1443 in6p->in6p_hops = optval;
1444 if ((in6p->inp_vflag &
1446 in6p->inp_ip_ttl = optval;
1449 #define OPTSET(bit) \
1453 in6p->inp_flags |= (bit); \
1455 in6p->inp_flags &= ~(bit); \
1456 INP_WUNLOCK(in6p); \
1457 } while (/*CONSTCOND*/ 0)
1458 #define OPTSET2292(bit) \
1461 in6p->inp_flags |= IN6P_RFC2292; \
1463 in6p->inp_flags |= (bit); \
1465 in6p->inp_flags &= ~(bit); \
1466 INP_WUNLOCK(in6p); \
1467 } while (/*CONSTCOND*/ 0)
1468 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1470 case IPV6_RECVPKTINFO:
1471 /* cannot mix with RFC2292 */
1472 if (OPTBIT(IN6P_RFC2292)) {
1476 OPTSET(IN6P_PKTINFO);
1481 struct ip6_pktopts **optp;
1483 /* cannot mix with RFC2292 */
1484 if (OPTBIT(IN6P_RFC2292)) {
1488 optp = &in6p->in6p_outputopts;
1489 error = ip6_pcbopt(IPV6_HOPLIMIT,
1490 (u_char *)&optval, sizeof(optval),
1491 optp, (td != NULL) ? td->td_ucred :
1496 case IPV6_RECVHOPLIMIT:
1497 /* cannot mix with RFC2292 */
1498 if (OPTBIT(IN6P_RFC2292)) {
1502 OPTSET(IN6P_HOPLIMIT);
1505 case IPV6_RECVHOPOPTS:
1506 /* cannot mix with RFC2292 */
1507 if (OPTBIT(IN6P_RFC2292)) {
1511 OPTSET(IN6P_HOPOPTS);
1514 case IPV6_RECVDSTOPTS:
1515 /* cannot mix with RFC2292 */
1516 if (OPTBIT(IN6P_RFC2292)) {
1520 OPTSET(IN6P_DSTOPTS);
1523 case IPV6_RECVRTHDRDSTOPTS:
1524 /* cannot mix with RFC2292 */
1525 if (OPTBIT(IN6P_RFC2292)) {
1529 OPTSET(IN6P_RTHDRDSTOPTS);
1532 case IPV6_RECVRTHDR:
1533 /* cannot mix with RFC2292 */
1534 if (OPTBIT(IN6P_RFC2292)) {
1545 case IPV6_RECVPATHMTU:
1547 * We ignore this option for TCP
1549 * (RFC3542 leaves this case
1552 if (uproto != IPPROTO_TCP)
1558 * make setsockopt(IPV6_V6ONLY)
1559 * available only prior to bind(2).
1560 * see ipng mailing list, Jun 22 2001.
1562 if (in6p->inp_lport ||
1563 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1567 OPTSET(IN6P_IPV6_V6ONLY);
1569 in6p->inp_vflag &= ~INP_IPV4;
1571 in6p->inp_vflag |= INP_IPV4;
1573 case IPV6_RECVTCLASS:
1574 /* cannot mix with RFC2292 XXX */
1575 if (OPTBIT(IN6P_RFC2292)) {
1579 OPTSET(IN6P_TCLASS);
1581 case IPV6_AUTOFLOWLABEL:
1582 OPTSET(IN6P_AUTOFLOWLABEL);
1586 OPTSET(INP_BINDANY);
1593 case IPV6_USE_MIN_MTU:
1594 case IPV6_PREFER_TEMPADDR:
1595 if (optlen != sizeof(optval)) {
1599 error = sooptcopyin(sopt, &optval,
1600 sizeof optval, sizeof optval);
1604 struct ip6_pktopts **optp;
1605 optp = &in6p->in6p_outputopts;
1606 error = ip6_pcbopt(optname,
1607 (u_char *)&optval, sizeof(optval),
1608 optp, (td != NULL) ? td->td_ucred :
1613 case IPV6_2292PKTINFO:
1614 case IPV6_2292HOPLIMIT:
1615 case IPV6_2292HOPOPTS:
1616 case IPV6_2292DSTOPTS:
1617 case IPV6_2292RTHDR:
1619 if (optlen != sizeof(int)) {
1623 error = sooptcopyin(sopt, &optval,
1624 sizeof optval, sizeof optval);
1628 case IPV6_2292PKTINFO:
1629 OPTSET2292(IN6P_PKTINFO);
1631 case IPV6_2292HOPLIMIT:
1632 OPTSET2292(IN6P_HOPLIMIT);
1634 case IPV6_2292HOPOPTS:
1636 * Check super-user privilege.
1637 * See comments for IPV6_RECVHOPOPTS.
1640 error = priv_check(td,
1641 PRIV_NETINET_SETHDROPTS);
1645 OPTSET2292(IN6P_HOPOPTS);
1647 case IPV6_2292DSTOPTS:
1649 error = priv_check(td,
1650 PRIV_NETINET_SETHDROPTS);
1654 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1656 case IPV6_2292RTHDR:
1657 OPTSET2292(IN6P_RTHDR);
1665 case IPV6_RTHDRDSTOPTS:
1668 /* new advanced API (RFC3542) */
1670 u_char optbuf_storage[MCLBYTES];
1672 struct ip6_pktopts **optp;
1674 /* cannot mix with RFC2292 */
1675 if (OPTBIT(IN6P_RFC2292)) {
1681 * We only ensure valsize is not too large
1682 * here. Further validation will be done
1685 error = sooptcopyin(sopt, optbuf_storage,
1686 sizeof(optbuf_storage), 0);
1689 optlen = sopt->sopt_valsize;
1690 optbuf = optbuf_storage;
1691 optp = &in6p->in6p_outputopts;
1692 error = ip6_pcbopt(optname, optbuf, optlen,
1693 optp, (td != NULL) ? td->td_ucred : NULL,
1699 case IPV6_MULTICAST_IF:
1700 case IPV6_MULTICAST_HOPS:
1701 case IPV6_MULTICAST_LOOP:
1702 case IPV6_JOIN_GROUP:
1703 case IPV6_LEAVE_GROUP:
1705 case MCAST_BLOCK_SOURCE:
1706 case MCAST_UNBLOCK_SOURCE:
1707 case MCAST_JOIN_GROUP:
1708 case MCAST_LEAVE_GROUP:
1709 case MCAST_JOIN_SOURCE_GROUP:
1710 case MCAST_LEAVE_SOURCE_GROUP:
1711 error = ip6_setmoptions(in6p, sopt);
1714 case IPV6_PORTRANGE:
1715 error = sooptcopyin(sopt, &optval,
1716 sizeof optval, sizeof optval);
1722 case IPV6_PORTRANGE_DEFAULT:
1723 in6p->inp_flags &= ~(INP_LOWPORT);
1724 in6p->inp_flags &= ~(INP_HIGHPORT);
1727 case IPV6_PORTRANGE_HIGH:
1728 in6p->inp_flags &= ~(INP_LOWPORT);
1729 in6p->inp_flags |= INP_HIGHPORT;
1732 case IPV6_PORTRANGE_LOW:
1733 in6p->inp_flags &= ~(INP_HIGHPORT);
1734 in6p->inp_flags |= INP_LOWPORT;
1745 case IPV6_IPSEC_POLICY:
1750 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1752 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1754 req = mtod(m, caddr_t);
1755 error = ipsec_set_policy(in6p, optname, req,
1756 m->m_len, (sopt->sopt_td != NULL) ?
1757 sopt->sopt_td->td_ucred : NULL);
1764 error = ENOPROTOOPT;
1772 case IPV6_2292PKTOPTIONS:
1773 #ifdef IPV6_PKTOPTIONS
1774 case IPV6_PKTOPTIONS:
1777 * RFC3542 (effectively) deprecated the
1778 * semantics of the 2292-style pktoptions.
1779 * Since it was not reliable in nature (i.e.,
1780 * applications had to expect the lack of some
1781 * information after all), it would make sense
1782 * to simplify this part by always returning
1785 sopt->sopt_valsize = 0;
1788 case IPV6_RECVHOPOPTS:
1789 case IPV6_RECVDSTOPTS:
1790 case IPV6_RECVRTHDRDSTOPTS:
1791 case IPV6_UNICAST_HOPS:
1792 case IPV6_RECVPKTINFO:
1793 case IPV6_RECVHOPLIMIT:
1794 case IPV6_RECVRTHDR:
1795 case IPV6_RECVPATHMTU:
1799 case IPV6_PORTRANGE:
1800 case IPV6_RECVTCLASS:
1801 case IPV6_AUTOFLOWLABEL:
1805 case IPV6_RECVHOPOPTS:
1806 optval = OPTBIT(IN6P_HOPOPTS);
1809 case IPV6_RECVDSTOPTS:
1810 optval = OPTBIT(IN6P_DSTOPTS);
1813 case IPV6_RECVRTHDRDSTOPTS:
1814 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1817 case IPV6_UNICAST_HOPS:
1818 optval = in6p->in6p_hops;
1821 case IPV6_RECVPKTINFO:
1822 optval = OPTBIT(IN6P_PKTINFO);
1825 case IPV6_RECVHOPLIMIT:
1826 optval = OPTBIT(IN6P_HOPLIMIT);
1829 case IPV6_RECVRTHDR:
1830 optval = OPTBIT(IN6P_RTHDR);
1833 case IPV6_RECVPATHMTU:
1834 optval = OPTBIT(IN6P_MTU);
1838 optval = OPTBIT(INP_FAITH);
1842 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1845 case IPV6_PORTRANGE:
1848 flags = in6p->inp_flags;
1849 if (flags & INP_HIGHPORT)
1850 optval = IPV6_PORTRANGE_HIGH;
1851 else if (flags & INP_LOWPORT)
1852 optval = IPV6_PORTRANGE_LOW;
1857 case IPV6_RECVTCLASS:
1858 optval = OPTBIT(IN6P_TCLASS);
1861 case IPV6_AUTOFLOWLABEL:
1862 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1866 optval = OPTBIT(INP_BINDANY);
1871 error = sooptcopyout(sopt, &optval,
1878 struct ip6_mtuinfo mtuinfo;
1879 struct route_in6 sro;
1881 bzero(&sro, sizeof(sro));
1883 if (!(so->so_state & SS_ISCONNECTED))
1886 * XXX: we dot not consider the case of source
1887 * routing, or optional information to specify
1888 * the outgoing interface.
1890 error = ip6_getpmtu(&sro, NULL, NULL,
1891 &in6p->in6p_faddr, &pmtu, NULL,
1897 if (pmtu > IPV6_MAXPACKET)
1898 pmtu = IPV6_MAXPACKET;
1900 bzero(&mtuinfo, sizeof(mtuinfo));
1901 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1902 optdata = (void *)&mtuinfo;
1903 optdatalen = sizeof(mtuinfo);
1904 error = sooptcopyout(sopt, optdata,
1909 case IPV6_2292PKTINFO:
1910 case IPV6_2292HOPLIMIT:
1911 case IPV6_2292HOPOPTS:
1912 case IPV6_2292RTHDR:
1913 case IPV6_2292DSTOPTS:
1915 case IPV6_2292PKTINFO:
1916 optval = OPTBIT(IN6P_PKTINFO);
1918 case IPV6_2292HOPLIMIT:
1919 optval = OPTBIT(IN6P_HOPLIMIT);
1921 case IPV6_2292HOPOPTS:
1922 optval = OPTBIT(IN6P_HOPOPTS);
1924 case IPV6_2292RTHDR:
1925 optval = OPTBIT(IN6P_RTHDR);
1927 case IPV6_2292DSTOPTS:
1928 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1931 error = sooptcopyout(sopt, &optval,
1938 case IPV6_RTHDRDSTOPTS:
1942 case IPV6_USE_MIN_MTU:
1943 case IPV6_PREFER_TEMPADDR:
1944 error = ip6_getpcbopt(in6p->in6p_outputopts,
1948 case IPV6_MULTICAST_IF:
1949 case IPV6_MULTICAST_HOPS:
1950 case IPV6_MULTICAST_LOOP:
1952 error = ip6_getmoptions(in6p, sopt);
1956 case IPV6_IPSEC_POLICY:
1960 struct mbuf *m = NULL;
1961 struct mbuf **mp = &m;
1962 size_t ovalsize = sopt->sopt_valsize;
1963 caddr_t oval = (caddr_t)sopt->sopt_val;
1965 error = soopt_getm(sopt, &m); /* XXX */
1968 error = soopt_mcopyin(sopt, m); /* XXX */
1971 sopt->sopt_valsize = ovalsize;
1972 sopt->sopt_val = oval;
1974 req = mtod(m, caddr_t);
1977 error = ipsec_get_policy(in6p, req, len, mp);
1979 error = soopt_mcopyout(sopt, m); /* XXX */
1980 if (error == 0 && m)
1987 error = ENOPROTOOPT;
1997 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1999 int error = 0, optval, optlen;
2000 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2001 struct inpcb *in6p = sotoinpcb(so);
2002 int level, op, optname;
2004 level = sopt->sopt_level;
2005 op = sopt->sopt_dir;
2006 optname = sopt->sopt_name;
2007 optlen = sopt->sopt_valsize;
2009 if (level != IPPROTO_IPV6) {
2016 * For ICMPv6 sockets, no modification allowed for checksum
2017 * offset, permit "no change" values to help existing apps.
2019 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2020 * for an ICMPv6 socket will fail."
2021 * The current behavior does not meet RFC3542.
2025 if (optlen != sizeof(int)) {
2029 error = sooptcopyin(sopt, &optval, sizeof(optval),
2033 if ((optval % 2) != 0) {
2034 /* the API assumes even offset values */
2036 } else if (so->so_proto->pr_protocol ==
2038 if (optval != icmp6off)
2041 in6p->in6p_cksum = optval;
2045 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2048 optval = in6p->in6p_cksum;
2050 error = sooptcopyout(sopt, &optval, sizeof(optval));
2060 error = ENOPROTOOPT;
2068 * Set up IP6 options in pcb for insertion in output packets or
2069 * specifying behavior of outgoing packets.
2072 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2073 struct socket *so, struct sockopt *sopt)
2075 struct ip6_pktopts *opt = *pktopt;
2077 struct thread *td = sopt->sopt_td;
2079 /* turn off any old options. */
2082 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2083 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2084 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2085 printf("ip6_pcbopts: all specified options are cleared.\n");
2087 ip6_clearpktopts(opt, -1);
2089 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2092 if (!m || m->m_len == 0) {
2094 * Only turning off any previous options, regardless of
2095 * whether the opt is just created or given.
2097 free(opt, M_IP6OPT);
2101 /* set options specified by user. */
2102 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2103 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2104 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2105 free(opt, M_IP6OPT);
2113 * initialize ip6_pktopts. beware that there are non-zero default values in
2117 ip6_initpktopts(struct ip6_pktopts *opt)
2120 bzero(opt, sizeof(*opt));
2121 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2122 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2123 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2124 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2128 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2129 struct ucred *cred, int uproto)
2131 struct ip6_pktopts *opt;
2133 if (*pktopt == NULL) {
2134 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2136 ip6_initpktopts(*pktopt);
2140 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2144 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2146 void *optdata = NULL;
2148 struct ip6_ext *ip6e;
2150 struct in6_pktinfo null_pktinfo;
2151 int deftclass = 0, on;
2152 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2153 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2157 if (pktopt && pktopt->ip6po_pktinfo)
2158 optdata = (void *)pktopt->ip6po_pktinfo;
2160 /* XXX: we don't have to do this every time... */
2161 bzero(&null_pktinfo, sizeof(null_pktinfo));
2162 optdata = (void *)&null_pktinfo;
2164 optdatalen = sizeof(struct in6_pktinfo);
2167 if (pktopt && pktopt->ip6po_tclass >= 0)
2168 optdata = (void *)&pktopt->ip6po_tclass;
2170 optdata = (void *)&deftclass;
2171 optdatalen = sizeof(int);
2174 if (pktopt && pktopt->ip6po_hbh) {
2175 optdata = (void *)pktopt->ip6po_hbh;
2176 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2177 optdatalen = (ip6e->ip6e_len + 1) << 3;
2181 if (pktopt && pktopt->ip6po_rthdr) {
2182 optdata = (void *)pktopt->ip6po_rthdr;
2183 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2184 optdatalen = (ip6e->ip6e_len + 1) << 3;
2187 case IPV6_RTHDRDSTOPTS:
2188 if (pktopt && pktopt->ip6po_dest1) {
2189 optdata = (void *)pktopt->ip6po_dest1;
2190 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2191 optdatalen = (ip6e->ip6e_len + 1) << 3;
2195 if (pktopt && pktopt->ip6po_dest2) {
2196 optdata = (void *)pktopt->ip6po_dest2;
2197 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2198 optdatalen = (ip6e->ip6e_len + 1) << 3;
2202 if (pktopt && pktopt->ip6po_nexthop) {
2203 optdata = (void *)pktopt->ip6po_nexthop;
2204 optdatalen = pktopt->ip6po_nexthop->sa_len;
2207 case IPV6_USE_MIN_MTU:
2209 optdata = (void *)&pktopt->ip6po_minmtu;
2211 optdata = (void *)&defminmtu;
2212 optdatalen = sizeof(int);
2215 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2219 optdata = (void *)&on;
2220 optdatalen = sizeof(on);
2222 case IPV6_PREFER_TEMPADDR:
2224 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2226 optdata = (void *)&defpreftemp;
2227 optdatalen = sizeof(int);
2229 default: /* should not happen */
2231 panic("ip6_getpcbopt: unexpected option\n");
2233 return (ENOPROTOOPT);
2236 error = sooptcopyout(sopt, optdata, optdatalen);
2242 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2247 if (optname == -1 || optname == IPV6_PKTINFO) {
2248 if (pktopt->ip6po_pktinfo)
2249 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2250 pktopt->ip6po_pktinfo = NULL;
2252 if (optname == -1 || optname == IPV6_HOPLIMIT)
2253 pktopt->ip6po_hlim = -1;
2254 if (optname == -1 || optname == IPV6_TCLASS)
2255 pktopt->ip6po_tclass = -1;
2256 if (optname == -1 || optname == IPV6_NEXTHOP) {
2257 if (pktopt->ip6po_nextroute.ro_rt) {
2258 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2259 pktopt->ip6po_nextroute.ro_rt = NULL;
2261 if (pktopt->ip6po_nexthop)
2262 free(pktopt->ip6po_nexthop, M_IP6OPT);
2263 pktopt->ip6po_nexthop = NULL;
2265 if (optname == -1 || optname == IPV6_HOPOPTS) {
2266 if (pktopt->ip6po_hbh)
2267 free(pktopt->ip6po_hbh, M_IP6OPT);
2268 pktopt->ip6po_hbh = NULL;
2270 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2271 if (pktopt->ip6po_dest1)
2272 free(pktopt->ip6po_dest1, M_IP6OPT);
2273 pktopt->ip6po_dest1 = NULL;
2275 if (optname == -1 || optname == IPV6_RTHDR) {
2276 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2277 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2278 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2279 if (pktopt->ip6po_route.ro_rt) {
2280 RTFREE(pktopt->ip6po_route.ro_rt);
2281 pktopt->ip6po_route.ro_rt = NULL;
2284 if (optname == -1 || optname == IPV6_DSTOPTS) {
2285 if (pktopt->ip6po_dest2)
2286 free(pktopt->ip6po_dest2, M_IP6OPT);
2287 pktopt->ip6po_dest2 = NULL;
2291 #define PKTOPT_EXTHDRCPY(type) \
2294 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2295 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2296 if (dst->type == NULL && canwait == M_NOWAIT)\
2298 bcopy(src->type, dst->type, hlen);\
2300 } while (/*CONSTCOND*/ 0)
2303 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2305 if (dst == NULL || src == NULL) {
2306 printf("ip6_clearpktopts: invalid argument\n");
2310 dst->ip6po_hlim = src->ip6po_hlim;
2311 dst->ip6po_tclass = src->ip6po_tclass;
2312 dst->ip6po_flags = src->ip6po_flags;
2313 dst->ip6po_minmtu = src->ip6po_minmtu;
2314 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2315 if (src->ip6po_pktinfo) {
2316 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2318 if (dst->ip6po_pktinfo == NULL)
2320 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2322 if (src->ip6po_nexthop) {
2323 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2325 if (dst->ip6po_nexthop == NULL)
2327 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2328 src->ip6po_nexthop->sa_len);
2330 PKTOPT_EXTHDRCPY(ip6po_hbh);
2331 PKTOPT_EXTHDRCPY(ip6po_dest1);
2332 PKTOPT_EXTHDRCPY(ip6po_dest2);
2333 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2337 ip6_clearpktopts(dst, -1);
2340 #undef PKTOPT_EXTHDRCPY
2342 struct ip6_pktopts *
2343 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2346 struct ip6_pktopts *dst;
2348 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2351 ip6_initpktopts(dst);
2353 if ((error = copypktopts(dst, src, canwait)) != 0) {
2354 free(dst, M_IP6OPT);
2362 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2367 ip6_clearpktopts(pktopt, -1);
2369 free(pktopt, M_IP6OPT);
2373 * Set IPv6 outgoing packet options based on advanced API.
2376 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2377 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2379 struct cmsghdr *cm = 0;
2381 if (control == NULL || opt == NULL)
2384 ip6_initpktopts(opt);
2389 * If stickyopt is provided, make a local copy of the options
2390 * for this particular packet, then override them by ancillary
2392 * XXX: copypktopts() does not copy the cached route to a next
2393 * hop (if any). This is not very good in terms of efficiency,
2394 * but we can allow this since this option should be rarely
2397 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2402 * XXX: Currently, we assume all the optional information is stored
2405 if (control->m_next)
2408 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2409 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2412 if (control->m_len < CMSG_LEN(0))
2415 cm = mtod(control, struct cmsghdr *);
2416 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2418 if (cm->cmsg_level != IPPROTO_IPV6)
2421 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2422 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2431 * Set a particular packet option, as a sticky option or an ancillary data
2432 * item. "len" can be 0 only when it's a sticky option.
2433 * We have 4 cases of combination of "sticky" and "cmsg":
2434 * "sticky=0, cmsg=0": impossible
2435 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2436 * "sticky=1, cmsg=0": RFC3542 socket option
2437 * "sticky=1, cmsg=1": RFC2292 socket option
2440 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2441 struct ucred *cred, int sticky, int cmsg, int uproto)
2443 int minmtupolicy, preftemp;
2446 if (!sticky && !cmsg) {
2448 printf("ip6_setpktopt: impossible case\n");
2454 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2455 * not be specified in the context of RFC3542. Conversely,
2456 * RFC3542 types should not be specified in the context of RFC2292.
2460 case IPV6_2292PKTINFO:
2461 case IPV6_2292HOPLIMIT:
2462 case IPV6_2292NEXTHOP:
2463 case IPV6_2292HOPOPTS:
2464 case IPV6_2292DSTOPTS:
2465 case IPV6_2292RTHDR:
2466 case IPV6_2292PKTOPTIONS:
2467 return (ENOPROTOOPT);
2470 if (sticky && cmsg) {
2477 case IPV6_RTHDRDSTOPTS:
2479 case IPV6_USE_MIN_MTU:
2482 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2483 return (ENOPROTOOPT);
2488 case IPV6_2292PKTINFO:
2491 struct ifnet *ifp = NULL;
2492 struct in6_pktinfo *pktinfo;
2494 if (len != sizeof(struct in6_pktinfo))
2497 pktinfo = (struct in6_pktinfo *)buf;
2500 * An application can clear any sticky IPV6_PKTINFO option by
2501 * doing a "regular" setsockopt with ipi6_addr being
2502 * in6addr_any and ipi6_ifindex being zero.
2503 * [RFC 3542, Section 6]
2505 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2506 pktinfo->ipi6_ifindex == 0 &&
2507 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2508 ip6_clearpktopts(opt, optname);
2512 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2513 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2517 /* validate the interface index if specified. */
2518 if (pktinfo->ipi6_ifindex > V_if_index ||
2519 pktinfo->ipi6_ifindex < 0) {
2522 if (pktinfo->ipi6_ifindex) {
2523 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2529 * We store the address anyway, and let in6_selectsrc()
2530 * validate the specified address. This is because ipi6_addr
2531 * may not have enough information about its scope zone, and
2532 * we may need additional information (such as outgoing
2533 * interface or the scope zone of a destination address) to
2534 * disambiguate the scope.
2535 * XXX: the delay of the validation may confuse the
2536 * application when it is used as a sticky option.
2538 if (opt->ip6po_pktinfo == NULL) {
2539 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2540 M_IP6OPT, M_NOWAIT);
2541 if (opt->ip6po_pktinfo == NULL)
2544 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2548 case IPV6_2292HOPLIMIT:
2554 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2555 * to simplify the ordering among hoplimit options.
2557 if (optname == IPV6_HOPLIMIT && sticky)
2558 return (ENOPROTOOPT);
2560 if (len != sizeof(int))
2563 if (*hlimp < -1 || *hlimp > 255)
2566 opt->ip6po_hlim = *hlimp;
2574 if (len != sizeof(int))
2576 tclass = *(int *)buf;
2577 if (tclass < -1 || tclass > 255)
2580 opt->ip6po_tclass = tclass;
2584 case IPV6_2292NEXTHOP:
2587 error = priv_check_cred(cred,
2588 PRIV_NETINET_SETHDROPTS, 0);
2593 if (len == 0) { /* just remove the option */
2594 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2598 /* check if cmsg_len is large enough for sa_len */
2599 if (len < sizeof(struct sockaddr) || len < *buf)
2602 switch (((struct sockaddr *)buf)->sa_family) {
2605 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2608 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2611 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2612 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2615 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2621 case AF_LINK: /* should eventually be supported */
2623 return (EAFNOSUPPORT);
2626 /* turn off the previous option, then set the new option. */
2627 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2628 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2629 if (opt->ip6po_nexthop == NULL)
2631 bcopy(buf, opt->ip6po_nexthop, *buf);
2634 case IPV6_2292HOPOPTS:
2637 struct ip6_hbh *hbh;
2641 * XXX: We don't allow a non-privileged user to set ANY HbH
2642 * options, since per-option restriction has too much
2646 error = priv_check_cred(cred,
2647 PRIV_NETINET_SETHDROPTS, 0);
2653 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2654 break; /* just remove the option */
2657 /* message length validation */
2658 if (len < sizeof(struct ip6_hbh))
2660 hbh = (struct ip6_hbh *)buf;
2661 hbhlen = (hbh->ip6h_len + 1) << 3;
2665 /* turn off the previous option, then set the new option. */
2666 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2667 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2668 if (opt->ip6po_hbh == NULL)
2670 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2675 case IPV6_2292DSTOPTS:
2677 case IPV6_RTHDRDSTOPTS:
2679 struct ip6_dest *dest, **newdest = NULL;
2682 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2683 error = priv_check_cred(cred,
2684 PRIV_NETINET_SETHDROPTS, 0);
2690 ip6_clearpktopts(opt, optname);
2691 break; /* just remove the option */
2694 /* message length validation */
2695 if (len < sizeof(struct ip6_dest))
2697 dest = (struct ip6_dest *)buf;
2698 destlen = (dest->ip6d_len + 1) << 3;
2703 * Determine the position that the destination options header
2704 * should be inserted; before or after the routing header.
2707 case IPV6_2292DSTOPTS:
2709 * The old advacned API is ambiguous on this point.
2710 * Our approach is to determine the position based
2711 * according to the existence of a routing header.
2712 * Note, however, that this depends on the order of the
2713 * extension headers in the ancillary data; the 1st
2714 * part of the destination options header must appear
2715 * before the routing header in the ancillary data,
2717 * RFC3542 solved the ambiguity by introducing
2718 * separate ancillary data or option types.
2720 if (opt->ip6po_rthdr == NULL)
2721 newdest = &opt->ip6po_dest1;
2723 newdest = &opt->ip6po_dest2;
2725 case IPV6_RTHDRDSTOPTS:
2726 newdest = &opt->ip6po_dest1;
2729 newdest = &opt->ip6po_dest2;
2733 /* turn off the previous option, then set the new option. */
2734 ip6_clearpktopts(opt, optname);
2735 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2736 if (*newdest == NULL)
2738 bcopy(dest, *newdest, destlen);
2743 case IPV6_2292RTHDR:
2746 struct ip6_rthdr *rth;
2750 ip6_clearpktopts(opt, IPV6_RTHDR);
2751 break; /* just remove the option */
2754 /* message length validation */
2755 if (len < sizeof(struct ip6_rthdr))
2757 rth = (struct ip6_rthdr *)buf;
2758 rthlen = (rth->ip6r_len + 1) << 3;
2762 switch (rth->ip6r_type) {
2763 case IPV6_RTHDR_TYPE_0:
2764 if (rth->ip6r_len == 0) /* must contain one addr */
2766 if (rth->ip6r_len % 2) /* length must be even */
2768 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2772 return (EINVAL); /* not supported */
2775 /* turn off the previous option */
2776 ip6_clearpktopts(opt, IPV6_RTHDR);
2777 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2778 if (opt->ip6po_rthdr == NULL)
2780 bcopy(rth, opt->ip6po_rthdr, rthlen);
2785 case IPV6_USE_MIN_MTU:
2786 if (len != sizeof(int))
2788 minmtupolicy = *(int *)buf;
2789 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2790 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2791 minmtupolicy != IP6PO_MINMTU_ALL) {
2794 opt->ip6po_minmtu = minmtupolicy;
2798 if (len != sizeof(int))
2801 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2803 * we ignore this option for TCP sockets.
2804 * (RFC3542 leaves this case unspecified.)
2806 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2808 opt->ip6po_flags |= IP6PO_DONTFRAG;
2811 case IPV6_PREFER_TEMPADDR:
2812 if (len != sizeof(int))
2814 preftemp = *(int *)buf;
2815 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2816 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2817 preftemp != IP6PO_TEMPADDR_PREFER) {
2820 opt->ip6po_prefer_tempaddr = preftemp;
2824 return (ENOPROTOOPT);
2825 } /* end of switch */
2831 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2832 * packet to the input queue of a specified interface. Note that this
2833 * calls the output routine of the loopback "driver", but with an interface
2834 * pointer that might NOT be &loif -- easier than replicating that code here.
2837 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2840 struct ip6_hdr *ip6;
2842 copym = m_copy(m, 0, M_COPYALL);
2847 * Make sure to deep-copy IPv6 header portion in case the data
2848 * is in an mbuf cluster, so that we can safely override the IPv6
2849 * header portion later.
2851 if ((copym->m_flags & M_EXT) != 0 ||
2852 copym->m_len < sizeof(struct ip6_hdr)) {
2853 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2857 ip6 = mtod(copym, struct ip6_hdr *);
2859 * clear embedded scope identifiers if necessary.
2860 * in6_clearscope will touch the addresses only when necessary.
2862 in6_clearscope(&ip6->ip6_src);
2863 in6_clearscope(&ip6->ip6_dst);
2864 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
2865 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
2867 copym->m_pkthdr.csum_data = 0xffff;
2869 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2873 * Chop IPv6 header off from the payload.
2876 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2879 struct ip6_hdr *ip6;
2881 ip6 = mtod(m, struct ip6_hdr *);
2882 if (m->m_len > sizeof(*ip6)) {
2883 mh = m_gethdr(M_NOWAIT, MT_DATA);
2888 m_move_pkthdr(mh, m);
2889 MH_ALIGN(mh, sizeof(*ip6));
2890 m->m_len -= sizeof(*ip6);
2891 m->m_data += sizeof(*ip6);
2894 m->m_len = sizeof(*ip6);
2895 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2897 exthdrs->ip6e_ip6 = m;
2902 * Compute IPv6 extension header length.
2905 ip6_optlen(struct inpcb *in6p)
2909 if (!in6p->in6p_outputopts)
2914 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2916 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2917 if (in6p->in6p_outputopts->ip6po_rthdr)
2918 /* dest1 is valid with rthdr only */
2919 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2920 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2921 len += elen(in6p->in6p_outputopts->ip6po_dest2);