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);
570 * The outgoing interface must be in the zone of source and
571 * destination addresses.
576 if (in6_setscope(&src0, origifp, &zone))
578 bzero(&src_sa, sizeof(src_sa));
579 src_sa.sin6_family = AF_INET6;
580 src_sa.sin6_len = sizeof(src_sa);
581 src_sa.sin6_addr = ip6->ip6_src;
582 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
586 if (in6_setscope(&dst0, origifp, &zone))
588 /* re-initialize to be sure */
589 bzero(&dst_sa, sizeof(dst_sa));
590 dst_sa.sin6_family = AF_INET6;
591 dst_sa.sin6_len = sizeof(dst_sa);
592 dst_sa.sin6_addr = ip6->ip6_dst;
593 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
597 /* We should use ia_ifp to support the case of
598 * sending packets to an address of our own.
600 if (ia != NULL && ia->ia_ifp)
603 /* scope check is done. */
607 IP6STAT_INC(ip6s_badscope);
608 in6_ifstat_inc(origifp, ifs6_out_discard);
610 error = EHOSTUNREACH; /* XXX */
614 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
615 if (opt && opt->ip6po_nextroute.ro_rt) {
617 * The nexthop is explicitly specified by the
618 * application. We assume the next hop is an IPv6
621 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
623 else if ((rt->rt_flags & RTF_GATEWAY))
624 dst = (struct sockaddr_in6 *)rt->rt_gateway;
627 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
628 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
630 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
631 in6_ifstat_inc(ifp, ifs6_out_mcast);
633 * Confirm that the outgoing interface supports multicast.
635 if (!(ifp->if_flags & IFF_MULTICAST)) {
636 IP6STAT_INC(ip6s_noroute);
637 in6_ifstat_inc(ifp, ifs6_out_discard);
641 if ((im6o == NULL && in6_mcast_loop) ||
642 (im6o && im6o->im6o_multicast_loop)) {
644 * Loop back multicast datagram if not expressly
645 * forbidden to do so, even if we have not joined
646 * the address; protocols will filter it later,
647 * thus deferring a hash lookup and lock acquisition
648 * at the expense of an m_copym().
650 ip6_mloopback(ifp, m, dst);
653 * If we are acting as a multicast router, perform
654 * multicast forwarding as if the packet had just
655 * arrived on the interface to which we are about
656 * to send. The multicast forwarding function
657 * recursively calls this function, using the
658 * IPV6_FORWARDING flag to prevent infinite recursion.
660 * Multicasts that are looped back by ip6_mloopback(),
661 * above, will be forwarded by the ip6_input() routine,
664 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
666 * XXX: ip6_mforward expects that rcvif is NULL
667 * when it is called from the originating path.
668 * However, it may not always be the case.
670 m->m_pkthdr.rcvif = NULL;
671 if (ip6_mforward(ip6, ifp, m) != 0) {
678 * Multicasts with a hoplimit of zero may be looped back,
679 * above, but must not be transmitted on a network.
680 * Also, multicasts addressed to the loopback interface
681 * are not sent -- the above call to ip6_mloopback() will
682 * loop back a copy if this host actually belongs to the
683 * destination group on the loopback interface.
685 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
686 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
693 * Fill the outgoing inteface to tell the upper layer
694 * to increment per-interface statistics.
699 /* Determine path MTU. */
700 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
701 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
705 * The caller of this function may specify to use the minimum MTU
707 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
708 * setting. The logic is a bit complicated; by default, unicast
709 * packets will follow path MTU while multicast packets will be sent at
710 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
711 * including unicast ones will be sent at the minimum MTU. Multicast
712 * packets will always be sent at the minimum MTU unless
713 * IP6PO_MINMTU_DISABLE is explicitly specified.
714 * See RFC 3542 for more details.
716 if (mtu > IPV6_MMTU) {
717 if ((flags & IPV6_MINMTU))
719 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
721 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
723 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
729 * clear embedded scope identifiers if necessary.
730 * in6_clearscope will touch the addresses only when necessary.
732 in6_clearscope(&ip6->ip6_src);
733 in6_clearscope(&ip6->ip6_dst);
736 * If the outgoing packet contains a hop-by-hop options header,
737 * it must be examined and processed even by the source node.
738 * (RFC 2460, section 4.)
740 if (exthdrs.ip6e_hbh) {
741 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
742 u_int32_t dummy; /* XXX unused */
743 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
746 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
747 panic("ip6e_hbh is not contiguous");
750 * XXX: if we have to send an ICMPv6 error to the sender,
751 * we need the M_LOOP flag since icmp6_error() expects
752 * the IPv6 and the hop-by-hop options header are
753 * contiguous unless the flag is set.
755 m->m_flags |= M_LOOP;
756 m->m_pkthdr.rcvif = ifp;
757 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
758 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
759 &dummy, &plen) < 0) {
760 /* m was already freed at this point */
761 error = EINVAL;/* better error? */
764 m->m_flags &= ~M_LOOP; /* XXX */
765 m->m_pkthdr.rcvif = NULL;
768 /* Jump over all PFIL processing if hooks are not active. */
769 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
773 /* Run through list of hooks for output packets. */
774 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
775 if (error != 0 || m == NULL)
777 ip6 = mtod(m, struct ip6_hdr *);
779 /* See if destination IP address was changed by packet filter. */
780 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
781 m->m_flags |= M_SKIP_FIREWALL;
782 /* If destination is now ourself drop to ip6_input(). */
783 if (in6_localip(&ip6->ip6_dst)) {
784 m->m_flags |= M_FASTFWD_OURS;
785 if (m->m_pkthdr.rcvif == NULL)
786 m->m_pkthdr.rcvif = V_loif;
787 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
788 m->m_pkthdr.csum_flags |=
789 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
790 m->m_pkthdr.csum_data = 0xffff;
793 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
794 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
796 error = netisr_queue(NETISR_IPV6, m);
799 goto again; /* Redo the routing table lookup. */
802 /* See if local, if yes, send it to netisr. */
803 if (m->m_flags & M_FASTFWD_OURS) {
804 if (m->m_pkthdr.rcvif == NULL)
805 m->m_pkthdr.rcvif = V_loif;
806 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
807 m->m_pkthdr.csum_flags |=
808 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
809 m->m_pkthdr.csum_data = 0xffff;
812 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
813 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
815 error = netisr_queue(NETISR_IPV6, m);
818 /* Or forward to some other address? */
819 if ((m->m_flags & M_IP6_NEXTHOP) &&
820 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
821 dst = (struct sockaddr_in6 *)&ro->ro_dst;
822 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
823 m->m_flags |= M_SKIP_FIREWALL;
824 m->m_flags &= ~M_IP6_NEXTHOP;
825 m_tag_delete(m, fwd_tag);
831 * Send the packet to the outgoing interface.
832 * If necessary, do IPv6 fragmentation before sending.
834 * the logic here is rather complex:
835 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
836 * 1-a: send as is if tlen <= path mtu
837 * 1-b: fragment if tlen > path mtu
839 * 2: if user asks us not to fragment (dontfrag == 1)
840 * 2-a: send as is if tlen <= interface mtu
841 * 2-b: error if tlen > interface mtu
843 * 3: if we always need to attach fragment header (alwaysfrag == 1)
846 * 4: if dontfrag == 1 && alwaysfrag == 1
847 * error, as we cannot handle this conflicting request
849 sw_csum = m->m_pkthdr.csum_flags;
851 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
852 sw_csum &= ~ifp->if_hwassist;
856 * If we added extension headers, we will not do TSO and calculate the
857 * checksums ourselves for now.
858 * XXX-BZ Need a framework to know when the NIC can handle it, even
861 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
862 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
863 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
866 if (sw_csum & CSUM_SCTP_IPV6) {
867 sw_csum &= ~CSUM_SCTP_IPV6;
868 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
871 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
872 tlen = m->m_pkthdr.len;
874 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
878 if (dontfrag && alwaysfrag) { /* case 4 */
879 /* conflicting request - can't transmit */
883 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
885 * Even if the DONTFRAG option is specified, we cannot send the
886 * packet when the data length is larger than the MTU of the
887 * outgoing interface.
888 * Notify the error by sending IPV6_PATHMTU ancillary data if
889 * application wanted to know the MTU value. Also return an
890 * error code (this is not described in the API spec).
893 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
899 * transmit packet without fragmentation
901 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
902 struct in6_ifaddr *ia6;
904 ip6 = mtod(m, struct ip6_hdr *);
905 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
907 /* Record statistics for this interface address. */
908 ia6->ia_ifa.if_opackets++;
909 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
910 ifa_free(&ia6->ia_ifa);
912 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
917 * try to fragment the packet. case 1-b and 3
919 if (mtu < IPV6_MMTU) {
920 /* path MTU cannot be less than IPV6_MMTU */
922 in6_ifstat_inc(ifp, ifs6_out_fragfail);
924 } else if (ip6->ip6_plen == 0) {
925 /* jumbo payload cannot be fragmented */
927 in6_ifstat_inc(ifp, ifs6_out_fragfail);
932 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
935 * Too large for the destination or interface;
936 * fragment if possible.
937 * Must be able to put at least 8 bytes per fragment.
939 hlen = unfragpartlen;
940 if (mtu > IPV6_MAXPACKET)
941 mtu = IPV6_MAXPACKET;
943 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
946 in6_ifstat_inc(ifp, ifs6_out_fragfail);
951 * Verify that we have any chance at all of being able to queue
952 * the packet or packet fragments
954 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
955 < tlen /* - hlen */)) {
957 IP6STAT_INC(ip6s_odropped);
963 * If the interface will not calculate checksums on
964 * fragmented packets, then do it here.
965 * XXX-BZ handle the hw offloading case. Need flags.
967 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
968 in6_delayed_cksum(m, plen, hlen);
969 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
972 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
973 sctp_delayed_cksum(m, hlen);
974 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
978 * Change the next header field of the last header in the
979 * unfragmentable part.
981 if (exthdrs.ip6e_rthdr) {
982 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
983 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
984 } else if (exthdrs.ip6e_dest1) {
985 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
986 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
987 } else if (exthdrs.ip6e_hbh) {
988 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
989 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
991 nextproto = ip6->ip6_nxt;
992 ip6->ip6_nxt = IPPROTO_FRAGMENT;
996 * Loop through length of segment after first fragment,
997 * make new header and copy data of each part and link onto
1001 id = htonl(ip6_randomid());
1002 if ((error = ip6_fragment(ifp, m, hlen, nextproto, len, id)))
1005 in6_ifstat_inc(ifp, ifs6_out_fragok);
1009 * Remove leading garbages.
1015 for (m0 = m; m; m = m0) {
1019 /* Record statistics for this interface address. */
1021 ia->ia_ifa.if_opackets++;
1022 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1024 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1030 IP6STAT_INC(ip6s_fragmented);
1033 if (ro == &ip6route)
1035 if (ro_pmtu == &ip6route)
1040 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1041 m_freem(exthdrs.ip6e_dest1);
1042 m_freem(exthdrs.ip6e_rthdr);
1043 m_freem(exthdrs.ip6e_dest2);
1052 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1056 if (hlen > MCLBYTES)
1057 return (ENOBUFS); /* XXX */
1060 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1062 m = m_get(M_NOWAIT, MT_DATA);
1067 bcopy(hdr, mtod(m, caddr_t), hlen);
1074 * Insert jumbo payload option.
1077 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1083 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1086 * If there is no hop-by-hop options header, allocate new one.
1087 * If there is one but it doesn't have enough space to store the
1088 * jumbo payload option, allocate a cluster to store the whole options.
1089 * Otherwise, use it to store the options.
1091 if (exthdrs->ip6e_hbh == 0) {
1092 mopt = m_get(M_NOWAIT, MT_DATA);
1095 mopt->m_len = JUMBOOPTLEN;
1096 optbuf = mtod(mopt, u_char *);
1097 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1098 exthdrs->ip6e_hbh = mopt;
1100 struct ip6_hbh *hbh;
1102 mopt = exthdrs->ip6e_hbh;
1103 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1106 * - exthdrs->ip6e_hbh is not referenced from places
1107 * other than exthdrs.
1108 * - exthdrs->ip6e_hbh is not an mbuf chain.
1110 int oldoptlen = mopt->m_len;
1114 * XXX: give up if the whole (new) hbh header does
1115 * not fit even in an mbuf cluster.
1117 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1121 * As a consequence, we must always prepare a cluster
1124 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1127 n->m_len = oldoptlen + JUMBOOPTLEN;
1128 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1130 optbuf = mtod(n, caddr_t) + oldoptlen;
1132 mopt = exthdrs->ip6e_hbh = n;
1134 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1135 mopt->m_len += JUMBOOPTLEN;
1137 optbuf[0] = IP6OPT_PADN;
1141 * Adjust the header length according to the pad and
1142 * the jumbo payload option.
1144 hbh = mtod(mopt, struct ip6_hbh *);
1145 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1148 /* fill in the option. */
1149 optbuf[2] = IP6OPT_JUMBO;
1151 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1152 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1154 /* finally, adjust the packet header length */
1155 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1162 * Insert fragment header and copy unfragmentable header portions.
1165 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1166 struct ip6_frag **frghdrp)
1168 struct mbuf *n, *mlast;
1170 if (hlen > sizeof(struct ip6_hdr)) {
1171 n = m_copym(m0, sizeof(struct ip6_hdr),
1172 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1179 /* Search for the last mbuf of unfragmentable part. */
1180 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1183 if ((mlast->m_flags & M_EXT) == 0 &&
1184 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1185 /* use the trailing space of the last mbuf for the fragment hdr */
1186 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1188 mlast->m_len += sizeof(struct ip6_frag);
1189 m->m_pkthdr.len += sizeof(struct ip6_frag);
1191 /* allocate a new mbuf for the fragment header */
1194 mfrg = m_get(M_NOWAIT, MT_DATA);
1197 mfrg->m_len = sizeof(struct ip6_frag);
1198 *frghdrp = mtod(mfrg, struct ip6_frag *);
1199 mlast->m_next = mfrg;
1206 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1207 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1208 int *alwaysfragp, u_int fibnum)
1214 if (ro_pmtu != ro) {
1215 /* The first hop and the final destination may differ. */
1216 struct sockaddr_in6 *sa6_dst =
1217 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1218 if (ro_pmtu->ro_rt &&
1219 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1220 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1221 RTFREE(ro_pmtu->ro_rt);
1222 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1224 if (ro_pmtu->ro_rt == NULL) {
1225 bzero(sa6_dst, sizeof(*sa6_dst));
1226 sa6_dst->sin6_family = AF_INET6;
1227 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1228 sa6_dst->sin6_addr = *dst;
1230 in6_rtalloc(ro_pmtu, fibnum);
1233 if (ro_pmtu->ro_rt) {
1235 struct in_conninfo inc;
1237 bzero(&inc, sizeof(inc));
1238 inc.inc_flags |= INC_ISIPV6;
1239 inc.inc6_faddr = *dst;
1242 ifp = ro_pmtu->ro_rt->rt_ifp;
1243 ifmtu = IN6_LINKMTU(ifp);
1244 mtu = tcp_hc_getmtu(&inc);
1246 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu);
1248 mtu = ro_pmtu->ro_rt->rt_mtu;
1251 else if (mtu < IPV6_MMTU) {
1253 * RFC2460 section 5, last paragraph:
1254 * if we record ICMPv6 too big message with
1255 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1256 * or smaller, with framgent header attached.
1257 * (fragment header is needed regardless from the
1258 * packet size, for translators to identify packets)
1262 } else if (mtu > ifmtu) {
1264 * The MTU on the route is larger than the MTU on
1265 * the interface! This shouldn't happen, unless the
1266 * MTU of the interface has been changed after the
1267 * interface was brought up. Change the MTU in the
1268 * route to match the interface MTU (as long as the
1269 * field isn't locked).
1272 ro_pmtu->ro_rt->rt_mtu = mtu;
1275 mtu = IN6_LINKMTU(ifp);
1277 error = EHOSTUNREACH; /* XXX */
1281 *alwaysfragp = alwaysfrag;
1286 * IP6 socket option processing.
1289 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1291 int optdatalen, uproto;
1293 struct inpcb *in6p = sotoinpcb(so);
1295 int level, op, optname;
1299 level = sopt->sopt_level;
1300 op = sopt->sopt_dir;
1301 optname = sopt->sopt_name;
1302 optlen = sopt->sopt_valsize;
1306 uproto = (int)so->so_proto->pr_protocol;
1308 if (level != IPPROTO_IPV6) {
1311 if (sopt->sopt_level == SOL_SOCKET &&
1312 sopt->sopt_dir == SOPT_SET) {
1313 switch (sopt->sopt_name) {
1316 if ((so->so_options & SO_REUSEADDR) != 0)
1317 in6p->inp_flags2 |= INP_REUSEADDR;
1319 in6p->inp_flags2 &= ~INP_REUSEADDR;
1325 if ((so->so_options & SO_REUSEPORT) != 0)
1326 in6p->inp_flags2 |= INP_REUSEPORT;
1328 in6p->inp_flags2 &= ~INP_REUSEPORT;
1334 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1342 } else { /* level == IPPROTO_IPV6 */
1347 case IPV6_2292PKTOPTIONS:
1348 #ifdef IPV6_PKTOPTIONS
1349 case IPV6_PKTOPTIONS:
1354 error = soopt_getm(sopt, &m); /* XXX */
1357 error = soopt_mcopyin(sopt, m); /* XXX */
1360 error = ip6_pcbopts(&in6p->in6p_outputopts,
1362 m_freem(m); /* XXX */
1367 * Use of some Hop-by-Hop options or some
1368 * Destination options, might require special
1369 * privilege. That is, normal applications
1370 * (without special privilege) might be forbidden
1371 * from setting certain options in outgoing packets,
1372 * and might never see certain options in received
1373 * packets. [RFC 2292 Section 6]
1374 * KAME specific note:
1375 * KAME prevents non-privileged users from sending or
1376 * receiving ANY hbh/dst options in order to avoid
1377 * overhead of parsing options in the kernel.
1379 case IPV6_RECVHOPOPTS:
1380 case IPV6_RECVDSTOPTS:
1381 case IPV6_RECVRTHDRDSTOPTS:
1383 error = priv_check(td,
1384 PRIV_NETINET_SETHDROPTS);
1389 case IPV6_UNICAST_HOPS:
1393 case IPV6_RECVPKTINFO:
1394 case IPV6_RECVHOPLIMIT:
1395 case IPV6_RECVRTHDR:
1396 case IPV6_RECVPATHMTU:
1397 case IPV6_RECVTCLASS:
1399 case IPV6_AUTOFLOWLABEL:
1401 if (optname == IPV6_BINDANY && td != NULL) {
1402 error = priv_check(td,
1403 PRIV_NETINET_BINDANY);
1408 if (optlen != sizeof(int)) {
1412 error = sooptcopyin(sopt, &optval,
1413 sizeof optval, sizeof optval);
1418 case IPV6_UNICAST_HOPS:
1419 if (optval < -1 || optval >= 256)
1422 /* -1 = kernel default */
1423 in6p->in6p_hops = optval;
1424 if ((in6p->inp_vflag &
1426 in6p->inp_ip_ttl = optval;
1429 #define OPTSET(bit) \
1433 in6p->inp_flags |= (bit); \
1435 in6p->inp_flags &= ~(bit); \
1436 INP_WUNLOCK(in6p); \
1437 } while (/*CONSTCOND*/ 0)
1438 #define OPTSET2292(bit) \
1441 in6p->inp_flags |= IN6P_RFC2292; \
1443 in6p->inp_flags |= (bit); \
1445 in6p->inp_flags &= ~(bit); \
1446 INP_WUNLOCK(in6p); \
1447 } while (/*CONSTCOND*/ 0)
1448 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1450 case IPV6_RECVPKTINFO:
1451 /* cannot mix with RFC2292 */
1452 if (OPTBIT(IN6P_RFC2292)) {
1456 OPTSET(IN6P_PKTINFO);
1461 struct ip6_pktopts **optp;
1463 /* cannot mix with RFC2292 */
1464 if (OPTBIT(IN6P_RFC2292)) {
1468 optp = &in6p->in6p_outputopts;
1469 error = ip6_pcbopt(IPV6_HOPLIMIT,
1470 (u_char *)&optval, sizeof(optval),
1471 optp, (td != NULL) ? td->td_ucred :
1476 case IPV6_RECVHOPLIMIT:
1477 /* cannot mix with RFC2292 */
1478 if (OPTBIT(IN6P_RFC2292)) {
1482 OPTSET(IN6P_HOPLIMIT);
1485 case IPV6_RECVHOPOPTS:
1486 /* cannot mix with RFC2292 */
1487 if (OPTBIT(IN6P_RFC2292)) {
1491 OPTSET(IN6P_HOPOPTS);
1494 case IPV6_RECVDSTOPTS:
1495 /* cannot mix with RFC2292 */
1496 if (OPTBIT(IN6P_RFC2292)) {
1500 OPTSET(IN6P_DSTOPTS);
1503 case IPV6_RECVRTHDRDSTOPTS:
1504 /* cannot mix with RFC2292 */
1505 if (OPTBIT(IN6P_RFC2292)) {
1509 OPTSET(IN6P_RTHDRDSTOPTS);
1512 case IPV6_RECVRTHDR:
1513 /* cannot mix with RFC2292 */
1514 if (OPTBIT(IN6P_RFC2292)) {
1525 case IPV6_RECVPATHMTU:
1527 * We ignore this option for TCP
1529 * (RFC3542 leaves this case
1532 if (uproto != IPPROTO_TCP)
1538 * make setsockopt(IPV6_V6ONLY)
1539 * available only prior to bind(2).
1540 * see ipng mailing list, Jun 22 2001.
1542 if (in6p->inp_lport ||
1543 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1547 OPTSET(IN6P_IPV6_V6ONLY);
1549 in6p->inp_vflag &= ~INP_IPV4;
1551 in6p->inp_vflag |= INP_IPV4;
1553 case IPV6_RECVTCLASS:
1554 /* cannot mix with RFC2292 XXX */
1555 if (OPTBIT(IN6P_RFC2292)) {
1559 OPTSET(IN6P_TCLASS);
1561 case IPV6_AUTOFLOWLABEL:
1562 OPTSET(IN6P_AUTOFLOWLABEL);
1566 OPTSET(INP_BINDANY);
1573 case IPV6_USE_MIN_MTU:
1574 case IPV6_PREFER_TEMPADDR:
1575 if (optlen != sizeof(optval)) {
1579 error = sooptcopyin(sopt, &optval,
1580 sizeof optval, sizeof optval);
1584 struct ip6_pktopts **optp;
1585 optp = &in6p->in6p_outputopts;
1586 error = ip6_pcbopt(optname,
1587 (u_char *)&optval, sizeof(optval),
1588 optp, (td != NULL) ? td->td_ucred :
1593 case IPV6_2292PKTINFO:
1594 case IPV6_2292HOPLIMIT:
1595 case IPV6_2292HOPOPTS:
1596 case IPV6_2292DSTOPTS:
1597 case IPV6_2292RTHDR:
1599 if (optlen != sizeof(int)) {
1603 error = sooptcopyin(sopt, &optval,
1604 sizeof optval, sizeof optval);
1608 case IPV6_2292PKTINFO:
1609 OPTSET2292(IN6P_PKTINFO);
1611 case IPV6_2292HOPLIMIT:
1612 OPTSET2292(IN6P_HOPLIMIT);
1614 case IPV6_2292HOPOPTS:
1616 * Check super-user privilege.
1617 * See comments for IPV6_RECVHOPOPTS.
1620 error = priv_check(td,
1621 PRIV_NETINET_SETHDROPTS);
1625 OPTSET2292(IN6P_HOPOPTS);
1627 case IPV6_2292DSTOPTS:
1629 error = priv_check(td,
1630 PRIV_NETINET_SETHDROPTS);
1634 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1636 case IPV6_2292RTHDR:
1637 OPTSET2292(IN6P_RTHDR);
1645 case IPV6_RTHDRDSTOPTS:
1648 /* new advanced API (RFC3542) */
1650 u_char optbuf_storage[MCLBYTES];
1652 struct ip6_pktopts **optp;
1654 /* cannot mix with RFC2292 */
1655 if (OPTBIT(IN6P_RFC2292)) {
1661 * We only ensure valsize is not too large
1662 * here. Further validation will be done
1665 error = sooptcopyin(sopt, optbuf_storage,
1666 sizeof(optbuf_storage), 0);
1669 optlen = sopt->sopt_valsize;
1670 optbuf = optbuf_storage;
1671 optp = &in6p->in6p_outputopts;
1672 error = ip6_pcbopt(optname, optbuf, optlen,
1673 optp, (td != NULL) ? td->td_ucred : NULL,
1679 case IPV6_MULTICAST_IF:
1680 case IPV6_MULTICAST_HOPS:
1681 case IPV6_MULTICAST_LOOP:
1682 case IPV6_JOIN_GROUP:
1683 case IPV6_LEAVE_GROUP:
1685 case MCAST_BLOCK_SOURCE:
1686 case MCAST_UNBLOCK_SOURCE:
1687 case MCAST_JOIN_GROUP:
1688 case MCAST_LEAVE_GROUP:
1689 case MCAST_JOIN_SOURCE_GROUP:
1690 case MCAST_LEAVE_SOURCE_GROUP:
1691 error = ip6_setmoptions(in6p, sopt);
1694 case IPV6_PORTRANGE:
1695 error = sooptcopyin(sopt, &optval,
1696 sizeof optval, sizeof optval);
1702 case IPV6_PORTRANGE_DEFAULT:
1703 in6p->inp_flags &= ~(INP_LOWPORT);
1704 in6p->inp_flags &= ~(INP_HIGHPORT);
1707 case IPV6_PORTRANGE_HIGH:
1708 in6p->inp_flags &= ~(INP_LOWPORT);
1709 in6p->inp_flags |= INP_HIGHPORT;
1712 case IPV6_PORTRANGE_LOW:
1713 in6p->inp_flags &= ~(INP_HIGHPORT);
1714 in6p->inp_flags |= INP_LOWPORT;
1725 case IPV6_IPSEC_POLICY:
1730 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1732 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1734 req = mtod(m, caddr_t);
1735 error = ipsec_set_policy(in6p, optname, req,
1736 m->m_len, (sopt->sopt_td != NULL) ?
1737 sopt->sopt_td->td_ucred : NULL);
1744 error = ENOPROTOOPT;
1752 case IPV6_2292PKTOPTIONS:
1753 #ifdef IPV6_PKTOPTIONS
1754 case IPV6_PKTOPTIONS:
1757 * RFC3542 (effectively) deprecated the
1758 * semantics of the 2292-style pktoptions.
1759 * Since it was not reliable in nature (i.e.,
1760 * applications had to expect the lack of some
1761 * information after all), it would make sense
1762 * to simplify this part by always returning
1765 sopt->sopt_valsize = 0;
1768 case IPV6_RECVHOPOPTS:
1769 case IPV6_RECVDSTOPTS:
1770 case IPV6_RECVRTHDRDSTOPTS:
1771 case IPV6_UNICAST_HOPS:
1772 case IPV6_RECVPKTINFO:
1773 case IPV6_RECVHOPLIMIT:
1774 case IPV6_RECVRTHDR:
1775 case IPV6_RECVPATHMTU:
1779 case IPV6_PORTRANGE:
1780 case IPV6_RECVTCLASS:
1781 case IPV6_AUTOFLOWLABEL:
1785 case IPV6_RECVHOPOPTS:
1786 optval = OPTBIT(IN6P_HOPOPTS);
1789 case IPV6_RECVDSTOPTS:
1790 optval = OPTBIT(IN6P_DSTOPTS);
1793 case IPV6_RECVRTHDRDSTOPTS:
1794 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1797 case IPV6_UNICAST_HOPS:
1798 optval = in6p->in6p_hops;
1801 case IPV6_RECVPKTINFO:
1802 optval = OPTBIT(IN6P_PKTINFO);
1805 case IPV6_RECVHOPLIMIT:
1806 optval = OPTBIT(IN6P_HOPLIMIT);
1809 case IPV6_RECVRTHDR:
1810 optval = OPTBIT(IN6P_RTHDR);
1813 case IPV6_RECVPATHMTU:
1814 optval = OPTBIT(IN6P_MTU);
1818 optval = OPTBIT(INP_FAITH);
1822 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1825 case IPV6_PORTRANGE:
1828 flags = in6p->inp_flags;
1829 if (flags & INP_HIGHPORT)
1830 optval = IPV6_PORTRANGE_HIGH;
1831 else if (flags & INP_LOWPORT)
1832 optval = IPV6_PORTRANGE_LOW;
1837 case IPV6_RECVTCLASS:
1838 optval = OPTBIT(IN6P_TCLASS);
1841 case IPV6_AUTOFLOWLABEL:
1842 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1846 optval = OPTBIT(INP_BINDANY);
1851 error = sooptcopyout(sopt, &optval,
1858 struct ip6_mtuinfo mtuinfo;
1859 struct route_in6 sro;
1861 bzero(&sro, sizeof(sro));
1863 if (!(so->so_state & SS_ISCONNECTED))
1866 * XXX: we dot not consider the case of source
1867 * routing, or optional information to specify
1868 * the outgoing interface.
1870 error = ip6_getpmtu(&sro, NULL, NULL,
1871 &in6p->in6p_faddr, &pmtu, NULL,
1877 if (pmtu > IPV6_MAXPACKET)
1878 pmtu = IPV6_MAXPACKET;
1880 bzero(&mtuinfo, sizeof(mtuinfo));
1881 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1882 optdata = (void *)&mtuinfo;
1883 optdatalen = sizeof(mtuinfo);
1884 error = sooptcopyout(sopt, optdata,
1889 case IPV6_2292PKTINFO:
1890 case IPV6_2292HOPLIMIT:
1891 case IPV6_2292HOPOPTS:
1892 case IPV6_2292RTHDR:
1893 case IPV6_2292DSTOPTS:
1895 case IPV6_2292PKTINFO:
1896 optval = OPTBIT(IN6P_PKTINFO);
1898 case IPV6_2292HOPLIMIT:
1899 optval = OPTBIT(IN6P_HOPLIMIT);
1901 case IPV6_2292HOPOPTS:
1902 optval = OPTBIT(IN6P_HOPOPTS);
1904 case IPV6_2292RTHDR:
1905 optval = OPTBIT(IN6P_RTHDR);
1907 case IPV6_2292DSTOPTS:
1908 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1911 error = sooptcopyout(sopt, &optval,
1918 case IPV6_RTHDRDSTOPTS:
1922 case IPV6_USE_MIN_MTU:
1923 case IPV6_PREFER_TEMPADDR:
1924 error = ip6_getpcbopt(in6p->in6p_outputopts,
1928 case IPV6_MULTICAST_IF:
1929 case IPV6_MULTICAST_HOPS:
1930 case IPV6_MULTICAST_LOOP:
1932 error = ip6_getmoptions(in6p, sopt);
1936 case IPV6_IPSEC_POLICY:
1940 struct mbuf *m = NULL;
1941 struct mbuf **mp = &m;
1942 size_t ovalsize = sopt->sopt_valsize;
1943 caddr_t oval = (caddr_t)sopt->sopt_val;
1945 error = soopt_getm(sopt, &m); /* XXX */
1948 error = soopt_mcopyin(sopt, m); /* XXX */
1951 sopt->sopt_valsize = ovalsize;
1952 sopt->sopt_val = oval;
1954 req = mtod(m, caddr_t);
1957 error = ipsec_get_policy(in6p, req, len, mp);
1959 error = soopt_mcopyout(sopt, m); /* XXX */
1960 if (error == 0 && m)
1967 error = ENOPROTOOPT;
1977 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1979 int error = 0, optval, optlen;
1980 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1981 struct inpcb *in6p = sotoinpcb(so);
1982 int level, op, optname;
1984 level = sopt->sopt_level;
1985 op = sopt->sopt_dir;
1986 optname = sopt->sopt_name;
1987 optlen = sopt->sopt_valsize;
1989 if (level != IPPROTO_IPV6) {
1996 * For ICMPv6 sockets, no modification allowed for checksum
1997 * offset, permit "no change" values to help existing apps.
1999 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2000 * for an ICMPv6 socket will fail."
2001 * The current behavior does not meet RFC3542.
2005 if (optlen != sizeof(int)) {
2009 error = sooptcopyin(sopt, &optval, sizeof(optval),
2013 if ((optval % 2) != 0) {
2014 /* the API assumes even offset values */
2016 } else if (so->so_proto->pr_protocol ==
2018 if (optval != icmp6off)
2021 in6p->in6p_cksum = optval;
2025 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2028 optval = in6p->in6p_cksum;
2030 error = sooptcopyout(sopt, &optval, sizeof(optval));
2040 error = ENOPROTOOPT;
2048 * Set up IP6 options in pcb for insertion in output packets or
2049 * specifying behavior of outgoing packets.
2052 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2053 struct socket *so, struct sockopt *sopt)
2055 struct ip6_pktopts *opt = *pktopt;
2057 struct thread *td = sopt->sopt_td;
2059 /* turn off any old options. */
2062 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2063 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2064 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2065 printf("ip6_pcbopts: all specified options are cleared.\n");
2067 ip6_clearpktopts(opt, -1);
2069 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2072 if (!m || m->m_len == 0) {
2074 * Only turning off any previous options, regardless of
2075 * whether the opt is just created or given.
2077 free(opt, M_IP6OPT);
2081 /* set options specified by user. */
2082 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2083 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2084 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2085 free(opt, M_IP6OPT);
2093 * initialize ip6_pktopts. beware that there are non-zero default values in
2097 ip6_initpktopts(struct ip6_pktopts *opt)
2100 bzero(opt, sizeof(*opt));
2101 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2102 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2103 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2104 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2108 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2109 struct ucred *cred, int uproto)
2111 struct ip6_pktopts *opt;
2113 if (*pktopt == NULL) {
2114 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2116 ip6_initpktopts(*pktopt);
2120 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2124 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2126 void *optdata = NULL;
2128 struct ip6_ext *ip6e;
2130 struct in6_pktinfo null_pktinfo;
2131 int deftclass = 0, on;
2132 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2133 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2137 if (pktopt && pktopt->ip6po_pktinfo)
2138 optdata = (void *)pktopt->ip6po_pktinfo;
2140 /* XXX: we don't have to do this every time... */
2141 bzero(&null_pktinfo, sizeof(null_pktinfo));
2142 optdata = (void *)&null_pktinfo;
2144 optdatalen = sizeof(struct in6_pktinfo);
2147 if (pktopt && pktopt->ip6po_tclass >= 0)
2148 optdata = (void *)&pktopt->ip6po_tclass;
2150 optdata = (void *)&deftclass;
2151 optdatalen = sizeof(int);
2154 if (pktopt && pktopt->ip6po_hbh) {
2155 optdata = (void *)pktopt->ip6po_hbh;
2156 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2157 optdatalen = (ip6e->ip6e_len + 1) << 3;
2161 if (pktopt && pktopt->ip6po_rthdr) {
2162 optdata = (void *)pktopt->ip6po_rthdr;
2163 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2164 optdatalen = (ip6e->ip6e_len + 1) << 3;
2167 case IPV6_RTHDRDSTOPTS:
2168 if (pktopt && pktopt->ip6po_dest1) {
2169 optdata = (void *)pktopt->ip6po_dest1;
2170 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2171 optdatalen = (ip6e->ip6e_len + 1) << 3;
2175 if (pktopt && pktopt->ip6po_dest2) {
2176 optdata = (void *)pktopt->ip6po_dest2;
2177 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2178 optdatalen = (ip6e->ip6e_len + 1) << 3;
2182 if (pktopt && pktopt->ip6po_nexthop) {
2183 optdata = (void *)pktopt->ip6po_nexthop;
2184 optdatalen = pktopt->ip6po_nexthop->sa_len;
2187 case IPV6_USE_MIN_MTU:
2189 optdata = (void *)&pktopt->ip6po_minmtu;
2191 optdata = (void *)&defminmtu;
2192 optdatalen = sizeof(int);
2195 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2199 optdata = (void *)&on;
2200 optdatalen = sizeof(on);
2202 case IPV6_PREFER_TEMPADDR:
2204 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2206 optdata = (void *)&defpreftemp;
2207 optdatalen = sizeof(int);
2209 default: /* should not happen */
2211 panic("ip6_getpcbopt: unexpected option\n");
2213 return (ENOPROTOOPT);
2216 error = sooptcopyout(sopt, optdata, optdatalen);
2222 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2227 if (optname == -1 || optname == IPV6_PKTINFO) {
2228 if (pktopt->ip6po_pktinfo)
2229 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2230 pktopt->ip6po_pktinfo = NULL;
2232 if (optname == -1 || optname == IPV6_HOPLIMIT)
2233 pktopt->ip6po_hlim = -1;
2234 if (optname == -1 || optname == IPV6_TCLASS)
2235 pktopt->ip6po_tclass = -1;
2236 if (optname == -1 || optname == IPV6_NEXTHOP) {
2237 if (pktopt->ip6po_nextroute.ro_rt) {
2238 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2239 pktopt->ip6po_nextroute.ro_rt = NULL;
2241 if (pktopt->ip6po_nexthop)
2242 free(pktopt->ip6po_nexthop, M_IP6OPT);
2243 pktopt->ip6po_nexthop = NULL;
2245 if (optname == -1 || optname == IPV6_HOPOPTS) {
2246 if (pktopt->ip6po_hbh)
2247 free(pktopt->ip6po_hbh, M_IP6OPT);
2248 pktopt->ip6po_hbh = NULL;
2250 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2251 if (pktopt->ip6po_dest1)
2252 free(pktopt->ip6po_dest1, M_IP6OPT);
2253 pktopt->ip6po_dest1 = NULL;
2255 if (optname == -1 || optname == IPV6_RTHDR) {
2256 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2257 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2258 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2259 if (pktopt->ip6po_route.ro_rt) {
2260 RTFREE(pktopt->ip6po_route.ro_rt);
2261 pktopt->ip6po_route.ro_rt = NULL;
2264 if (optname == -1 || optname == IPV6_DSTOPTS) {
2265 if (pktopt->ip6po_dest2)
2266 free(pktopt->ip6po_dest2, M_IP6OPT);
2267 pktopt->ip6po_dest2 = NULL;
2271 #define PKTOPT_EXTHDRCPY(type) \
2274 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2275 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2276 if (dst->type == NULL && canwait == M_NOWAIT)\
2278 bcopy(src->type, dst->type, hlen);\
2280 } while (/*CONSTCOND*/ 0)
2283 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2285 if (dst == NULL || src == NULL) {
2286 printf("ip6_clearpktopts: invalid argument\n");
2290 dst->ip6po_hlim = src->ip6po_hlim;
2291 dst->ip6po_tclass = src->ip6po_tclass;
2292 dst->ip6po_flags = src->ip6po_flags;
2293 dst->ip6po_minmtu = src->ip6po_minmtu;
2294 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2295 if (src->ip6po_pktinfo) {
2296 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2298 if (dst->ip6po_pktinfo == NULL)
2300 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2302 if (src->ip6po_nexthop) {
2303 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2305 if (dst->ip6po_nexthop == NULL)
2307 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2308 src->ip6po_nexthop->sa_len);
2310 PKTOPT_EXTHDRCPY(ip6po_hbh);
2311 PKTOPT_EXTHDRCPY(ip6po_dest1);
2312 PKTOPT_EXTHDRCPY(ip6po_dest2);
2313 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2317 ip6_clearpktopts(dst, -1);
2320 #undef PKTOPT_EXTHDRCPY
2322 struct ip6_pktopts *
2323 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2326 struct ip6_pktopts *dst;
2328 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2331 ip6_initpktopts(dst);
2333 if ((error = copypktopts(dst, src, canwait)) != 0) {
2334 free(dst, M_IP6OPT);
2342 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2347 ip6_clearpktopts(pktopt, -1);
2349 free(pktopt, M_IP6OPT);
2353 * Set IPv6 outgoing packet options based on advanced API.
2356 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2357 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2359 struct cmsghdr *cm = 0;
2361 if (control == NULL || opt == NULL)
2364 ip6_initpktopts(opt);
2369 * If stickyopt is provided, make a local copy of the options
2370 * for this particular packet, then override them by ancillary
2372 * XXX: copypktopts() does not copy the cached route to a next
2373 * hop (if any). This is not very good in terms of efficiency,
2374 * but we can allow this since this option should be rarely
2377 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2382 * XXX: Currently, we assume all the optional information is stored
2385 if (control->m_next)
2388 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2389 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2392 if (control->m_len < CMSG_LEN(0))
2395 cm = mtod(control, struct cmsghdr *);
2396 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2398 if (cm->cmsg_level != IPPROTO_IPV6)
2401 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2402 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2411 * Set a particular packet option, as a sticky option or an ancillary data
2412 * item. "len" can be 0 only when it's a sticky option.
2413 * We have 4 cases of combination of "sticky" and "cmsg":
2414 * "sticky=0, cmsg=0": impossible
2415 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2416 * "sticky=1, cmsg=0": RFC3542 socket option
2417 * "sticky=1, cmsg=1": RFC2292 socket option
2420 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2421 struct ucred *cred, int sticky, int cmsg, int uproto)
2423 int minmtupolicy, preftemp;
2426 if (!sticky && !cmsg) {
2428 printf("ip6_setpktopt: impossible case\n");
2434 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2435 * not be specified in the context of RFC3542. Conversely,
2436 * RFC3542 types should not be specified in the context of RFC2292.
2440 case IPV6_2292PKTINFO:
2441 case IPV6_2292HOPLIMIT:
2442 case IPV6_2292NEXTHOP:
2443 case IPV6_2292HOPOPTS:
2444 case IPV6_2292DSTOPTS:
2445 case IPV6_2292RTHDR:
2446 case IPV6_2292PKTOPTIONS:
2447 return (ENOPROTOOPT);
2450 if (sticky && cmsg) {
2457 case IPV6_RTHDRDSTOPTS:
2459 case IPV6_USE_MIN_MTU:
2462 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2463 return (ENOPROTOOPT);
2468 case IPV6_2292PKTINFO:
2471 struct ifnet *ifp = NULL;
2472 struct in6_pktinfo *pktinfo;
2474 if (len != sizeof(struct in6_pktinfo))
2477 pktinfo = (struct in6_pktinfo *)buf;
2480 * An application can clear any sticky IPV6_PKTINFO option by
2481 * doing a "regular" setsockopt with ipi6_addr being
2482 * in6addr_any and ipi6_ifindex being zero.
2483 * [RFC 3542, Section 6]
2485 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2486 pktinfo->ipi6_ifindex == 0 &&
2487 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2488 ip6_clearpktopts(opt, optname);
2492 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2493 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2497 /* validate the interface index if specified. */
2498 if (pktinfo->ipi6_ifindex > V_if_index ||
2499 pktinfo->ipi6_ifindex < 0) {
2502 if (pktinfo->ipi6_ifindex) {
2503 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2509 * We store the address anyway, and let in6_selectsrc()
2510 * validate the specified address. This is because ipi6_addr
2511 * may not have enough information about its scope zone, and
2512 * we may need additional information (such as outgoing
2513 * interface or the scope zone of a destination address) to
2514 * disambiguate the scope.
2515 * XXX: the delay of the validation may confuse the
2516 * application when it is used as a sticky option.
2518 if (opt->ip6po_pktinfo == NULL) {
2519 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2520 M_IP6OPT, M_NOWAIT);
2521 if (opt->ip6po_pktinfo == NULL)
2524 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2528 case IPV6_2292HOPLIMIT:
2534 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2535 * to simplify the ordering among hoplimit options.
2537 if (optname == IPV6_HOPLIMIT && sticky)
2538 return (ENOPROTOOPT);
2540 if (len != sizeof(int))
2543 if (*hlimp < -1 || *hlimp > 255)
2546 opt->ip6po_hlim = *hlimp;
2554 if (len != sizeof(int))
2556 tclass = *(int *)buf;
2557 if (tclass < -1 || tclass > 255)
2560 opt->ip6po_tclass = tclass;
2564 case IPV6_2292NEXTHOP:
2567 error = priv_check_cred(cred,
2568 PRIV_NETINET_SETHDROPTS, 0);
2573 if (len == 0) { /* just remove the option */
2574 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2578 /* check if cmsg_len is large enough for sa_len */
2579 if (len < sizeof(struct sockaddr) || len < *buf)
2582 switch (((struct sockaddr *)buf)->sa_family) {
2585 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2588 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2591 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2592 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2595 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2601 case AF_LINK: /* should eventually be supported */
2603 return (EAFNOSUPPORT);
2606 /* turn off the previous option, then set the new option. */
2607 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2608 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2609 if (opt->ip6po_nexthop == NULL)
2611 bcopy(buf, opt->ip6po_nexthop, *buf);
2614 case IPV6_2292HOPOPTS:
2617 struct ip6_hbh *hbh;
2621 * XXX: We don't allow a non-privileged user to set ANY HbH
2622 * options, since per-option restriction has too much
2626 error = priv_check_cred(cred,
2627 PRIV_NETINET_SETHDROPTS, 0);
2633 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2634 break; /* just remove the option */
2637 /* message length validation */
2638 if (len < sizeof(struct ip6_hbh))
2640 hbh = (struct ip6_hbh *)buf;
2641 hbhlen = (hbh->ip6h_len + 1) << 3;
2645 /* turn off the previous option, then set the new option. */
2646 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2647 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2648 if (opt->ip6po_hbh == NULL)
2650 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2655 case IPV6_2292DSTOPTS:
2657 case IPV6_RTHDRDSTOPTS:
2659 struct ip6_dest *dest, **newdest = NULL;
2662 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2663 error = priv_check_cred(cred,
2664 PRIV_NETINET_SETHDROPTS, 0);
2670 ip6_clearpktopts(opt, optname);
2671 break; /* just remove the option */
2674 /* message length validation */
2675 if (len < sizeof(struct ip6_dest))
2677 dest = (struct ip6_dest *)buf;
2678 destlen = (dest->ip6d_len + 1) << 3;
2683 * Determine the position that the destination options header
2684 * should be inserted; before or after the routing header.
2687 case IPV6_2292DSTOPTS:
2689 * The old advacned API is ambiguous on this point.
2690 * Our approach is to determine the position based
2691 * according to the existence of a routing header.
2692 * Note, however, that this depends on the order of the
2693 * extension headers in the ancillary data; the 1st
2694 * part of the destination options header must appear
2695 * before the routing header in the ancillary data,
2697 * RFC3542 solved the ambiguity by introducing
2698 * separate ancillary data or option types.
2700 if (opt->ip6po_rthdr == NULL)
2701 newdest = &opt->ip6po_dest1;
2703 newdest = &opt->ip6po_dest2;
2705 case IPV6_RTHDRDSTOPTS:
2706 newdest = &opt->ip6po_dest1;
2709 newdest = &opt->ip6po_dest2;
2713 /* turn off the previous option, then set the new option. */
2714 ip6_clearpktopts(opt, optname);
2715 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2716 if (*newdest == NULL)
2718 bcopy(dest, *newdest, destlen);
2723 case IPV6_2292RTHDR:
2726 struct ip6_rthdr *rth;
2730 ip6_clearpktopts(opt, IPV6_RTHDR);
2731 break; /* just remove the option */
2734 /* message length validation */
2735 if (len < sizeof(struct ip6_rthdr))
2737 rth = (struct ip6_rthdr *)buf;
2738 rthlen = (rth->ip6r_len + 1) << 3;
2742 switch (rth->ip6r_type) {
2743 case IPV6_RTHDR_TYPE_0:
2744 if (rth->ip6r_len == 0) /* must contain one addr */
2746 if (rth->ip6r_len % 2) /* length must be even */
2748 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2752 return (EINVAL); /* not supported */
2755 /* turn off the previous option */
2756 ip6_clearpktopts(opt, IPV6_RTHDR);
2757 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2758 if (opt->ip6po_rthdr == NULL)
2760 bcopy(rth, opt->ip6po_rthdr, rthlen);
2765 case IPV6_USE_MIN_MTU:
2766 if (len != sizeof(int))
2768 minmtupolicy = *(int *)buf;
2769 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2770 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2771 minmtupolicy != IP6PO_MINMTU_ALL) {
2774 opt->ip6po_minmtu = minmtupolicy;
2778 if (len != sizeof(int))
2781 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2783 * we ignore this option for TCP sockets.
2784 * (RFC3542 leaves this case unspecified.)
2786 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2788 opt->ip6po_flags |= IP6PO_DONTFRAG;
2791 case IPV6_PREFER_TEMPADDR:
2792 if (len != sizeof(int))
2794 preftemp = *(int *)buf;
2795 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2796 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2797 preftemp != IP6PO_TEMPADDR_PREFER) {
2800 opt->ip6po_prefer_tempaddr = preftemp;
2804 return (ENOPROTOOPT);
2805 } /* end of switch */
2811 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2812 * packet to the input queue of a specified interface. Note that this
2813 * calls the output routine of the loopback "driver", but with an interface
2814 * pointer that might NOT be &loif -- easier than replicating that code here.
2817 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2820 struct ip6_hdr *ip6;
2822 copym = m_copy(m, 0, M_COPYALL);
2827 * Make sure to deep-copy IPv6 header portion in case the data
2828 * is in an mbuf cluster, so that we can safely override the IPv6
2829 * header portion later.
2831 if ((copym->m_flags & M_EXT) != 0 ||
2832 copym->m_len < sizeof(struct ip6_hdr)) {
2833 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2837 ip6 = mtod(copym, struct ip6_hdr *);
2839 * clear embedded scope identifiers if necessary.
2840 * in6_clearscope will touch the addresses only when necessary.
2842 in6_clearscope(&ip6->ip6_src);
2843 in6_clearscope(&ip6->ip6_dst);
2844 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
2845 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
2847 copym->m_pkthdr.csum_data = 0xffff;
2849 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2853 * Chop IPv6 header off from the payload.
2856 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2859 struct ip6_hdr *ip6;
2861 ip6 = mtod(m, struct ip6_hdr *);
2862 if (m->m_len > sizeof(*ip6)) {
2863 mh = m_gethdr(M_NOWAIT, MT_DATA);
2868 m_move_pkthdr(mh, m);
2869 MH_ALIGN(mh, sizeof(*ip6));
2870 m->m_len -= sizeof(*ip6);
2871 m->m_data += sizeof(*ip6);
2874 m->m_len = sizeof(*ip6);
2875 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2877 exthdrs->ip6e_ip6 = m;
2882 * Compute IPv6 extension header length.
2885 ip6_optlen(struct inpcb *in6p)
2889 if (!in6p->in6p_outputopts)
2894 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2896 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2897 if (in6p->in6p_outputopts->ip6po_rthdr)
2898 /* dest1 is valid with rthdr only */
2899 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2900 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2901 len += elen(in6p->in6p_outputopts->ip6po_dest2);