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 output. The packet in mbuf chain m contains a skeletal IP6
213 * header (with pri, len, nxt, hlim, src, dst).
214 * This function may modify ver and hlim only.
215 * The mbuf chain containing the packet will be freed.
216 * The mbuf opt, if present, will not be freed.
217 * If route_in6 ro is present and has ro_rt initialized, route lookup would be
218 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL,
219 * then result of route lookup is stored in ro->ro_rt.
221 * type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and
222 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
225 * ifpp - XXX: just for statistics
228 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
229 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
230 struct ifnet **ifpp, struct inpcb *inp)
232 struct ip6_hdr *ip6, *mhip6;
233 struct ifnet *ifp, *origifp;
235 struct mbuf *mprev = NULL;
236 int hlen, tlen, len, off;
237 struct route_in6 ip6route;
238 struct rtentry *rt = NULL;
239 struct sockaddr_in6 *dst, src_sa, dst_sa;
240 struct in6_addr odst;
242 struct in6_ifaddr *ia = NULL;
244 int alwaysfrag, dontfrag;
245 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
246 struct ip6_exthdrs exthdrs;
247 struct in6_addr finaldst, src0, dst0;
249 struct route_in6 *ro_pmtu = NULL;
252 struct m_tag *fwd_tag = NULL;
254 ip6 = mtod(m, struct ip6_hdr *);
256 printf ("ip6 is NULL");
261 M_SETFIB(m, inp->inp_inc.inc_fibnum);
263 finaldst = ip6->ip6_dst;
264 bzero(&exthdrs, sizeof(exthdrs));
266 /* Hop-by-Hop options header */
267 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
268 /* Destination options header(1st part) */
269 if (opt->ip6po_rthdr) {
271 * Destination options header(1st part)
272 * This only makes sense with a routing header.
273 * See Section 9.2 of RFC 3542.
274 * Disabling this part just for MIP6 convenience is
275 * a bad idea. We need to think carefully about a
276 * way to make the advanced API coexist with MIP6
277 * options, which might automatically be inserted in
280 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
283 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
284 /* Destination options header(2nd part) */
285 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
290 * IPSec checking which handles several cases.
291 * FAST IPSEC: We re-injected the packet.
293 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp))
295 case 1: /* Bad packet */
297 case -1: /* IPSec done */
299 case 0: /* No IPSec */
306 * Calculate the total length of the extension header chain.
307 * Keep the length of the unfragmentable part for fragmentation.
310 if (exthdrs.ip6e_hbh)
311 optlen += exthdrs.ip6e_hbh->m_len;
312 if (exthdrs.ip6e_dest1)
313 optlen += exthdrs.ip6e_dest1->m_len;
314 if (exthdrs.ip6e_rthdr)
315 optlen += exthdrs.ip6e_rthdr->m_len;
316 unfragpartlen = optlen + sizeof(struct ip6_hdr);
318 /* NOTE: we don't add AH/ESP length here (done in ip6_ipsec_output) */
319 if (exthdrs.ip6e_dest2)
320 optlen += exthdrs.ip6e_dest2->m_len;
323 * If there is at least one extension header,
324 * separate IP6 header from the payload.
326 if (optlen && !hdrsplit) {
327 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
331 m = exthdrs.ip6e_ip6;
336 ip6 = mtod(m, struct ip6_hdr *);
338 /* adjust mbuf packet header length */
339 m->m_pkthdr.len += optlen;
340 plen = m->m_pkthdr.len - sizeof(*ip6);
342 /* If this is a jumbo payload, insert a jumbo payload option. */
343 if (plen > IPV6_MAXPACKET) {
345 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
349 m = exthdrs.ip6e_ip6;
353 ip6 = mtod(m, struct ip6_hdr *);
354 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
358 ip6->ip6_plen = htons(plen);
361 * Concatenate headers and fill in next header fields.
362 * Here we have, on "m"
364 * and we insert headers accordingly. Finally, we should be getting:
365 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
367 * during the header composing process, "m" points to IPv6 header.
368 * "mprev" points to an extension header prior to esp.
370 u_char *nexthdrp = &ip6->ip6_nxt;
374 * we treat dest2 specially. this makes IPsec processing
375 * much easier. the goal here is to make mprev point the
376 * mbuf prior to dest2.
378 * result: IPv6 dest2 payload
379 * m and mprev will point to IPv6 header.
381 if (exthdrs.ip6e_dest2) {
383 panic("assumption failed: hdr not split");
384 exthdrs.ip6e_dest2->m_next = m->m_next;
385 m->m_next = exthdrs.ip6e_dest2;
386 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
387 ip6->ip6_nxt = IPPROTO_DSTOPTS;
391 * result: IPv6 hbh dest1 rthdr dest2 payload
392 * m will point to IPv6 header. mprev will point to the
393 * extension header prior to dest2 (rthdr in the above case).
395 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
396 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
398 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
402 * If there is a routing header, discard the packet.
404 if (exthdrs.ip6e_rthdr) {
409 /* Source address validation */
410 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
411 (flags & IPV6_UNSPECSRC) == 0) {
413 IP6STAT_INC(ip6s_badscope);
416 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
418 IP6STAT_INC(ip6s_badscope);
422 IP6STAT_INC(ip6s_localout);
429 bzero((caddr_t)ro, sizeof(*ro));
432 if (opt && opt->ip6po_rthdr)
433 ro = &opt->ip6po_route;
434 dst = (struct sockaddr_in6 *)&ro->ro_dst;
436 if (ro->ro_rt == NULL)
437 (void )flowtable_lookup(AF_INET6, m, (struct route *)ro);
441 * if specified, try to fill in the traffic class field.
442 * do not override if a non-zero value is already set.
443 * we check the diffserv field and the ecn field separately.
445 if (opt && opt->ip6po_tclass >= 0) {
448 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
450 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
453 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
456 /* fill in or override the hop limit field, if necessary. */
457 if (opt && opt->ip6po_hlim != -1)
458 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
459 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
461 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
463 ip6->ip6_hlim = V_ip6_defmcasthlim;
467 ip6 = mtod(m, struct ip6_hdr *);
469 if (ro->ro_rt && fwd_tag == NULL) {
471 ifp = ro->ro_rt->rt_ifp;
473 if (fwd_tag == NULL) {
474 bzero(&dst_sa, sizeof(dst_sa));
475 dst_sa.sin6_family = AF_INET6;
476 dst_sa.sin6_len = sizeof(dst_sa);
477 dst_sa.sin6_addr = ip6->ip6_dst;
479 error = in6_selectroute_fib(&dst_sa, opt, im6o, ro, &ifp,
480 &rt, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m));
483 in6_ifstat_inc(ifp, ifs6_out_discard);
489 * If in6_selectroute() does not return a route entry,
490 * dst may not have been updated.
492 *dst = dst_sa; /* XXX */
496 * then rt (for unicast) and ifp must be non-NULL valid values.
498 if ((flags & IPV6_FORWARDING) == 0) {
499 /* XXX: the FORWARDING flag can be set for mrouting. */
500 in6_ifstat_inc(ifp, ifs6_out_request);
503 ia = (struct in6_ifaddr *)(rt->rt_ifa);
504 counter_u64_add(rt->rt_pksent, 1);
509 * The outgoing interface must be in the zone of source and
510 * destination addresses.
515 if (in6_setscope(&src0, origifp, &zone))
517 bzero(&src_sa, sizeof(src_sa));
518 src_sa.sin6_family = AF_INET6;
519 src_sa.sin6_len = sizeof(src_sa);
520 src_sa.sin6_addr = ip6->ip6_src;
521 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
525 if (in6_setscope(&dst0, origifp, &zone))
527 /* re-initialize to be sure */
528 bzero(&dst_sa, sizeof(dst_sa));
529 dst_sa.sin6_family = AF_INET6;
530 dst_sa.sin6_len = sizeof(dst_sa);
531 dst_sa.sin6_addr = ip6->ip6_dst;
532 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
536 /* We should use ia_ifp to support the case of
537 * sending packets to an address of our own.
539 if (ia != NULL && ia->ia_ifp)
542 /* scope check is done. */
546 IP6STAT_INC(ip6s_badscope);
547 in6_ifstat_inc(origifp, ifs6_out_discard);
549 error = EHOSTUNREACH; /* XXX */
553 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
554 if (opt && opt->ip6po_nextroute.ro_rt) {
556 * The nexthop is explicitly specified by the
557 * application. We assume the next hop is an IPv6
560 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
562 else if ((rt->rt_flags & RTF_GATEWAY))
563 dst = (struct sockaddr_in6 *)rt->rt_gateway;
566 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
567 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
569 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
570 in6_ifstat_inc(ifp, ifs6_out_mcast);
572 * Confirm that the outgoing interface supports multicast.
574 if (!(ifp->if_flags & IFF_MULTICAST)) {
575 IP6STAT_INC(ip6s_noroute);
576 in6_ifstat_inc(ifp, ifs6_out_discard);
580 if ((im6o == NULL && in6_mcast_loop) ||
581 (im6o && im6o->im6o_multicast_loop)) {
583 * Loop back multicast datagram if not expressly
584 * forbidden to do so, even if we have not joined
585 * the address; protocols will filter it later,
586 * thus deferring a hash lookup and lock acquisition
587 * at the expense of an m_copym().
589 ip6_mloopback(ifp, m, dst);
592 * If we are acting as a multicast router, perform
593 * multicast forwarding as if the packet had just
594 * arrived on the interface to which we are about
595 * to send. The multicast forwarding function
596 * recursively calls this function, using the
597 * IPV6_FORWARDING flag to prevent infinite recursion.
599 * Multicasts that are looped back by ip6_mloopback(),
600 * above, will be forwarded by the ip6_input() routine,
603 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
605 * XXX: ip6_mforward expects that rcvif is NULL
606 * when it is called from the originating path.
607 * However, it may not always be the case.
609 m->m_pkthdr.rcvif = NULL;
610 if (ip6_mforward(ip6, ifp, m) != 0) {
617 * Multicasts with a hoplimit of zero may be looped back,
618 * above, but must not be transmitted on a network.
619 * Also, multicasts addressed to the loopback interface
620 * are not sent -- the above call to ip6_mloopback() will
621 * loop back a copy if this host actually belongs to the
622 * destination group on the loopback interface.
624 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
625 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
632 * Fill the outgoing inteface to tell the upper layer
633 * to increment per-interface statistics.
638 /* Determine path MTU. */
639 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
640 &alwaysfrag, inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m))) != 0)
644 * The caller of this function may specify to use the minimum MTU
646 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
647 * setting. The logic is a bit complicated; by default, unicast
648 * packets will follow path MTU while multicast packets will be sent at
649 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
650 * including unicast ones will be sent at the minimum MTU. Multicast
651 * packets will always be sent at the minimum MTU unless
652 * IP6PO_MINMTU_DISABLE is explicitly specified.
653 * See RFC 3542 for more details.
655 if (mtu > IPV6_MMTU) {
656 if ((flags & IPV6_MINMTU))
658 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
660 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
662 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
668 * clear embedded scope identifiers if necessary.
669 * in6_clearscope will touch the addresses only when necessary.
671 in6_clearscope(&ip6->ip6_src);
672 in6_clearscope(&ip6->ip6_dst);
675 * If the outgoing packet contains a hop-by-hop options header,
676 * it must be examined and processed even by the source node.
677 * (RFC 2460, section 4.)
679 if (exthdrs.ip6e_hbh) {
680 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
681 u_int32_t dummy; /* XXX unused */
682 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
685 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
686 panic("ip6e_hbh is not contiguous");
689 * XXX: if we have to send an ICMPv6 error to the sender,
690 * we need the M_LOOP flag since icmp6_error() expects
691 * the IPv6 and the hop-by-hop options header are
692 * contiguous unless the flag is set.
694 m->m_flags |= M_LOOP;
695 m->m_pkthdr.rcvif = ifp;
696 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
697 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
698 &dummy, &plen) < 0) {
699 /* m was already freed at this point */
700 error = EINVAL;/* better error? */
703 m->m_flags &= ~M_LOOP; /* XXX */
704 m->m_pkthdr.rcvif = NULL;
707 /* Jump over all PFIL processing if hooks are not active. */
708 if (!PFIL_HOOKED(&V_inet6_pfil_hook))
712 /* Run through list of hooks for output packets. */
713 error = pfil_run_hooks(&V_inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
714 if (error != 0 || m == NULL)
716 ip6 = mtod(m, struct ip6_hdr *);
718 /* See if destination IP address was changed by packet filter. */
719 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
720 m->m_flags |= M_SKIP_FIREWALL;
721 /* If destination is now ourself drop to ip6_input(). */
722 if (in6_localip(&ip6->ip6_dst)) {
723 m->m_flags |= M_FASTFWD_OURS;
724 if (m->m_pkthdr.rcvif == NULL)
725 m->m_pkthdr.rcvif = V_loif;
726 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
727 m->m_pkthdr.csum_flags |=
728 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
729 m->m_pkthdr.csum_data = 0xffff;
732 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
733 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
735 error = netisr_queue(NETISR_IPV6, m);
738 goto again; /* Redo the routing table lookup. */
741 /* See if local, if yes, send it to netisr. */
742 if (m->m_flags & M_FASTFWD_OURS) {
743 if (m->m_pkthdr.rcvif == NULL)
744 m->m_pkthdr.rcvif = V_loif;
745 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
746 m->m_pkthdr.csum_flags |=
747 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
748 m->m_pkthdr.csum_data = 0xffff;
751 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
752 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
754 error = netisr_queue(NETISR_IPV6, m);
757 /* Or forward to some other address? */
758 if ((m->m_flags & M_IP6_NEXTHOP) &&
759 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
760 dst = (struct sockaddr_in6 *)&ro->ro_dst;
761 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
762 m->m_flags |= M_SKIP_FIREWALL;
763 m->m_flags &= ~M_IP6_NEXTHOP;
764 m_tag_delete(m, fwd_tag);
770 * Send the packet to the outgoing interface.
771 * If necessary, do IPv6 fragmentation before sending.
773 * the logic here is rather complex:
774 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
775 * 1-a: send as is if tlen <= path mtu
776 * 1-b: fragment if tlen > path mtu
778 * 2: if user asks us not to fragment (dontfrag == 1)
779 * 2-a: send as is if tlen <= interface mtu
780 * 2-b: error if tlen > interface mtu
782 * 3: if we always need to attach fragment header (alwaysfrag == 1)
785 * 4: if dontfrag == 1 && alwaysfrag == 1
786 * error, as we cannot handle this conflicting request
788 sw_csum = m->m_pkthdr.csum_flags;
790 tso = ((sw_csum & ifp->if_hwassist & CSUM_TSO) != 0) ? 1 : 0;
791 sw_csum &= ~ifp->if_hwassist;
795 * If we added extension headers, we will not do TSO and calculate the
796 * checksums ourselves for now.
797 * XXX-BZ Need a framework to know when the NIC can handle it, even
800 if (sw_csum & CSUM_DELAY_DATA_IPV6) {
801 sw_csum &= ~CSUM_DELAY_DATA_IPV6;
802 in6_delayed_cksum(m, plen, sizeof(struct ip6_hdr));
805 if (sw_csum & CSUM_SCTP_IPV6) {
806 sw_csum &= ~CSUM_SCTP_IPV6;
807 sctp_delayed_cksum(m, sizeof(struct ip6_hdr));
810 m->m_pkthdr.csum_flags &= ifp->if_hwassist;
811 tlen = m->m_pkthdr.len;
813 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
817 if (dontfrag && alwaysfrag) { /* case 4 */
818 /* conflicting request - can't transmit */
822 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* case 2-b */
824 * Even if the DONTFRAG option is specified, we cannot send the
825 * packet when the data length is larger than the MTU of the
826 * outgoing interface.
827 * Notify the error by sending IPV6_PATHMTU ancillary data as
828 * well as returning an error code (the latter is not described
832 struct ip6ctlparam ip6cp;
834 mtu32 = (u_int32_t)mtu;
835 bzero(&ip6cp, sizeof(ip6cp));
836 ip6cp.ip6c_cmdarg = (void *)&mtu32;
837 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
845 * transmit packet without fragmentation
847 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
848 struct in6_ifaddr *ia6;
850 ip6 = mtod(m, struct ip6_hdr *);
851 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
853 /* Record statistics for this interface address. */
854 ia6->ia_ifa.if_opackets++;
855 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
856 ifa_free(&ia6->ia_ifa);
858 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
863 * try to fragment the packet. case 1-b and 3
865 if (mtu < IPV6_MMTU) {
866 /* path MTU cannot be less than IPV6_MMTU */
868 in6_ifstat_inc(ifp, ifs6_out_fragfail);
870 } else if (ip6->ip6_plen == 0) {
871 /* jumbo payload cannot be fragmented */
873 in6_ifstat_inc(ifp, ifs6_out_fragfail);
876 struct mbuf **mnext, *m_frgpart;
877 struct ip6_frag *ip6f;
878 u_int32_t id = htonl(ip6_randomid());
881 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
884 * Too large for the destination or interface;
885 * fragment if possible.
886 * Must be able to put at least 8 bytes per fragment.
888 hlen = unfragpartlen;
889 if (mtu > IPV6_MAXPACKET)
890 mtu = IPV6_MAXPACKET;
892 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
895 in6_ifstat_inc(ifp, ifs6_out_fragfail);
900 * Verify that we have any chance at all of being able to queue
901 * the packet or packet fragments
903 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
904 < tlen /* - hlen */)) {
906 IP6STAT_INC(ip6s_odropped);
912 * If the interface will not calculate checksums on
913 * fragmented packets, then do it here.
914 * XXX-BZ handle the hw offloading case. Need flags.
916 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
917 in6_delayed_cksum(m, plen, hlen);
918 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
921 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6) {
922 sctp_delayed_cksum(m, hlen);
923 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
926 mnext = &m->m_nextpkt;
929 * Change the next header field of the last header in the
930 * unfragmentable part.
932 if (exthdrs.ip6e_rthdr) {
933 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
934 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
935 } else if (exthdrs.ip6e_dest1) {
936 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
937 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
938 } else if (exthdrs.ip6e_hbh) {
939 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
940 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
942 nextproto = ip6->ip6_nxt;
943 ip6->ip6_nxt = IPPROTO_FRAGMENT;
947 * Loop through length of segment after first fragment,
948 * make new header and copy data of each part and link onto
952 for (off = hlen; off < tlen; off += len) {
953 m = m_gethdr(M_NOWAIT, MT_DATA);
956 IP6STAT_INC(ip6s_odropped);
959 m->m_flags = m0->m_flags & M_COPYFLAGS;
961 mnext = &m->m_nextpkt;
962 m->m_data += max_linkhdr;
963 mhip6 = mtod(m, struct ip6_hdr *);
965 m->m_len = sizeof(*mhip6);
966 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
968 IP6STAT_INC(ip6s_odropped);
971 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
972 if (off + len >= tlen)
975 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
976 mhip6->ip6_plen = htons((u_short)(len + hlen +
977 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
978 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
980 IP6STAT_INC(ip6s_odropped);
984 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
985 m->m_pkthdr.fibnum = m0->m_pkthdr.fibnum;
986 m->m_pkthdr.rcvif = NULL;
987 ip6f->ip6f_reserved = 0;
988 ip6f->ip6f_ident = id;
989 ip6f->ip6f_nxt = nextproto;
990 IP6STAT_INC(ip6s_ofragments);
991 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
994 in6_ifstat_inc(ifp, ifs6_out_fragok);
998 * Remove leading garbages.
1004 for (m0 = m; m; m = m0) {
1008 /* Record statistics for this interface address. */
1010 ia->ia_ifa.if_opackets++;
1011 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1013 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1019 IP6STAT_INC(ip6s_fragmented);
1022 if (ro == &ip6route)
1024 if (ro_pmtu == &ip6route)
1029 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1030 m_freem(exthdrs.ip6e_dest1);
1031 m_freem(exthdrs.ip6e_rthdr);
1032 m_freem(exthdrs.ip6e_dest2);
1041 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1045 if (hlen > MCLBYTES)
1046 return (ENOBUFS); /* XXX */
1049 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1051 m = m_get(M_NOWAIT, MT_DATA);
1056 bcopy(hdr, mtod(m, caddr_t), hlen);
1063 * Insert jumbo payload option.
1066 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1072 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1075 * If there is no hop-by-hop options header, allocate new one.
1076 * If there is one but it doesn't have enough space to store the
1077 * jumbo payload option, allocate a cluster to store the whole options.
1078 * Otherwise, use it to store the options.
1080 if (exthdrs->ip6e_hbh == 0) {
1081 mopt = m_get(M_NOWAIT, MT_DATA);
1084 mopt->m_len = JUMBOOPTLEN;
1085 optbuf = mtod(mopt, u_char *);
1086 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1087 exthdrs->ip6e_hbh = mopt;
1089 struct ip6_hbh *hbh;
1091 mopt = exthdrs->ip6e_hbh;
1092 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1095 * - exthdrs->ip6e_hbh is not referenced from places
1096 * other than exthdrs.
1097 * - exthdrs->ip6e_hbh is not an mbuf chain.
1099 int oldoptlen = mopt->m_len;
1103 * XXX: give up if the whole (new) hbh header does
1104 * not fit even in an mbuf cluster.
1106 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1110 * As a consequence, we must always prepare a cluster
1113 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1116 n->m_len = oldoptlen + JUMBOOPTLEN;
1117 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1119 optbuf = mtod(n, caddr_t) + oldoptlen;
1121 mopt = exthdrs->ip6e_hbh = n;
1123 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1124 mopt->m_len += JUMBOOPTLEN;
1126 optbuf[0] = IP6OPT_PADN;
1130 * Adjust the header length according to the pad and
1131 * the jumbo payload option.
1133 hbh = mtod(mopt, struct ip6_hbh *);
1134 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1137 /* fill in the option. */
1138 optbuf[2] = IP6OPT_JUMBO;
1140 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1141 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1143 /* finally, adjust the packet header length */
1144 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1151 * Insert fragment header and copy unfragmentable header portions.
1154 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1155 struct ip6_frag **frghdrp)
1157 struct mbuf *n, *mlast;
1159 if (hlen > sizeof(struct ip6_hdr)) {
1160 n = m_copym(m0, sizeof(struct ip6_hdr),
1161 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1168 /* Search for the last mbuf of unfragmentable part. */
1169 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1172 if ((mlast->m_flags & M_EXT) == 0 &&
1173 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1174 /* use the trailing space of the last mbuf for the fragment hdr */
1175 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1177 mlast->m_len += sizeof(struct ip6_frag);
1178 m->m_pkthdr.len += sizeof(struct ip6_frag);
1180 /* allocate a new mbuf for the fragment header */
1183 mfrg = m_get(M_NOWAIT, MT_DATA);
1186 mfrg->m_len = sizeof(struct ip6_frag);
1187 *frghdrp = mtod(mfrg, struct ip6_frag *);
1188 mlast->m_next = mfrg;
1195 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1196 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1197 int *alwaysfragp, u_int fibnum)
1203 if (ro_pmtu != ro) {
1204 /* The first hop and the final destination may differ. */
1205 struct sockaddr_in6 *sa6_dst =
1206 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1207 if (ro_pmtu->ro_rt &&
1208 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1209 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1210 RTFREE(ro_pmtu->ro_rt);
1211 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1213 if (ro_pmtu->ro_rt == NULL) {
1214 bzero(sa6_dst, sizeof(*sa6_dst));
1215 sa6_dst->sin6_family = AF_INET6;
1216 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1217 sa6_dst->sin6_addr = *dst;
1219 in6_rtalloc(ro_pmtu, fibnum);
1222 if (ro_pmtu->ro_rt) {
1224 struct in_conninfo inc;
1226 bzero(&inc, sizeof(inc));
1227 inc.inc_flags |= INC_ISIPV6;
1228 inc.inc6_faddr = *dst;
1231 ifp = ro_pmtu->ro_rt->rt_ifp;
1232 ifmtu = IN6_LINKMTU(ifp);
1233 mtu = tcp_hc_getmtu(&inc);
1235 mtu = min(mtu, ro_pmtu->ro_rt->rt_mtu);
1237 mtu = ro_pmtu->ro_rt->rt_mtu;
1240 else if (mtu < IPV6_MMTU) {
1242 * RFC2460 section 5, last paragraph:
1243 * if we record ICMPv6 too big message with
1244 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1245 * or smaller, with framgent header attached.
1246 * (fragment header is needed regardless from the
1247 * packet size, for translators to identify packets)
1251 } else if (mtu > ifmtu) {
1253 * The MTU on the route is larger than the MTU on
1254 * the interface! This shouldn't happen, unless the
1255 * MTU of the interface has been changed after the
1256 * interface was brought up. Change the MTU in the
1257 * route to match the interface MTU (as long as the
1258 * field isn't locked).
1261 ro_pmtu->ro_rt->rt_mtu = mtu;
1264 mtu = IN6_LINKMTU(ifp);
1266 error = EHOSTUNREACH; /* XXX */
1270 *alwaysfragp = alwaysfrag;
1275 * IP6 socket option processing.
1278 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1280 int optdatalen, uproto;
1282 struct inpcb *in6p = sotoinpcb(so);
1284 int level, op, optname;
1288 level = sopt->sopt_level;
1289 op = sopt->sopt_dir;
1290 optname = sopt->sopt_name;
1291 optlen = sopt->sopt_valsize;
1295 uproto = (int)so->so_proto->pr_protocol;
1297 if (level != IPPROTO_IPV6) {
1300 if (sopt->sopt_level == SOL_SOCKET &&
1301 sopt->sopt_dir == SOPT_SET) {
1302 switch (sopt->sopt_name) {
1305 if ((so->so_options & SO_REUSEADDR) != 0)
1306 in6p->inp_flags2 |= INP_REUSEADDR;
1308 in6p->inp_flags2 &= ~INP_REUSEADDR;
1314 if ((so->so_options & SO_REUSEPORT) != 0)
1315 in6p->inp_flags2 |= INP_REUSEPORT;
1317 in6p->inp_flags2 &= ~INP_REUSEPORT;
1323 in6p->inp_inc.inc_fibnum = so->so_fibnum;
1331 } else { /* level == IPPROTO_IPV6 */
1336 case IPV6_2292PKTOPTIONS:
1337 #ifdef IPV6_PKTOPTIONS
1338 case IPV6_PKTOPTIONS:
1343 error = soopt_getm(sopt, &m); /* XXX */
1346 error = soopt_mcopyin(sopt, m); /* XXX */
1349 error = ip6_pcbopts(&in6p->in6p_outputopts,
1351 m_freem(m); /* XXX */
1356 * Use of some Hop-by-Hop options or some
1357 * Destination options, might require special
1358 * privilege. That is, normal applications
1359 * (without special privilege) might be forbidden
1360 * from setting certain options in outgoing packets,
1361 * and might never see certain options in received
1362 * packets. [RFC 2292 Section 6]
1363 * KAME specific note:
1364 * KAME prevents non-privileged users from sending or
1365 * receiving ANY hbh/dst options in order to avoid
1366 * overhead of parsing options in the kernel.
1368 case IPV6_RECVHOPOPTS:
1369 case IPV6_RECVDSTOPTS:
1370 case IPV6_RECVRTHDRDSTOPTS:
1372 error = priv_check(td,
1373 PRIV_NETINET_SETHDROPTS);
1378 case IPV6_UNICAST_HOPS:
1382 case IPV6_RECVPKTINFO:
1383 case IPV6_RECVHOPLIMIT:
1384 case IPV6_RECVRTHDR:
1385 case IPV6_RECVPATHMTU:
1386 case IPV6_RECVTCLASS:
1388 case IPV6_AUTOFLOWLABEL:
1390 if (optname == IPV6_BINDANY && td != NULL) {
1391 error = priv_check(td,
1392 PRIV_NETINET_BINDANY);
1397 if (optlen != sizeof(int)) {
1401 error = sooptcopyin(sopt, &optval,
1402 sizeof optval, sizeof optval);
1407 case IPV6_UNICAST_HOPS:
1408 if (optval < -1 || optval >= 256)
1411 /* -1 = kernel default */
1412 in6p->in6p_hops = optval;
1413 if ((in6p->inp_vflag &
1415 in6p->inp_ip_ttl = optval;
1418 #define OPTSET(bit) \
1422 in6p->inp_flags |= (bit); \
1424 in6p->inp_flags &= ~(bit); \
1425 INP_WUNLOCK(in6p); \
1426 } while (/*CONSTCOND*/ 0)
1427 #define OPTSET2292(bit) \
1430 in6p->inp_flags |= IN6P_RFC2292; \
1432 in6p->inp_flags |= (bit); \
1434 in6p->inp_flags &= ~(bit); \
1435 INP_WUNLOCK(in6p); \
1436 } while (/*CONSTCOND*/ 0)
1437 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1439 case IPV6_RECVPKTINFO:
1440 /* cannot mix with RFC2292 */
1441 if (OPTBIT(IN6P_RFC2292)) {
1445 OPTSET(IN6P_PKTINFO);
1450 struct ip6_pktopts **optp;
1452 /* cannot mix with RFC2292 */
1453 if (OPTBIT(IN6P_RFC2292)) {
1457 optp = &in6p->in6p_outputopts;
1458 error = ip6_pcbopt(IPV6_HOPLIMIT,
1459 (u_char *)&optval, sizeof(optval),
1460 optp, (td != NULL) ? td->td_ucred :
1465 case IPV6_RECVHOPLIMIT:
1466 /* cannot mix with RFC2292 */
1467 if (OPTBIT(IN6P_RFC2292)) {
1471 OPTSET(IN6P_HOPLIMIT);
1474 case IPV6_RECVHOPOPTS:
1475 /* cannot mix with RFC2292 */
1476 if (OPTBIT(IN6P_RFC2292)) {
1480 OPTSET(IN6P_HOPOPTS);
1483 case IPV6_RECVDSTOPTS:
1484 /* cannot mix with RFC2292 */
1485 if (OPTBIT(IN6P_RFC2292)) {
1489 OPTSET(IN6P_DSTOPTS);
1492 case IPV6_RECVRTHDRDSTOPTS:
1493 /* cannot mix with RFC2292 */
1494 if (OPTBIT(IN6P_RFC2292)) {
1498 OPTSET(IN6P_RTHDRDSTOPTS);
1501 case IPV6_RECVRTHDR:
1502 /* cannot mix with RFC2292 */
1503 if (OPTBIT(IN6P_RFC2292)) {
1514 case IPV6_RECVPATHMTU:
1516 * We ignore this option for TCP
1518 * (RFC3542 leaves this case
1521 if (uproto != IPPROTO_TCP)
1527 * make setsockopt(IPV6_V6ONLY)
1528 * available only prior to bind(2).
1529 * see ipng mailing list, Jun 22 2001.
1531 if (in6p->inp_lport ||
1532 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1536 OPTSET(IN6P_IPV6_V6ONLY);
1538 in6p->inp_vflag &= ~INP_IPV4;
1540 in6p->inp_vflag |= INP_IPV4;
1542 case IPV6_RECVTCLASS:
1543 /* cannot mix with RFC2292 XXX */
1544 if (OPTBIT(IN6P_RFC2292)) {
1548 OPTSET(IN6P_TCLASS);
1550 case IPV6_AUTOFLOWLABEL:
1551 OPTSET(IN6P_AUTOFLOWLABEL);
1555 OPTSET(INP_BINDANY);
1562 case IPV6_USE_MIN_MTU:
1563 case IPV6_PREFER_TEMPADDR:
1564 if (optlen != sizeof(optval)) {
1568 error = sooptcopyin(sopt, &optval,
1569 sizeof optval, sizeof optval);
1573 struct ip6_pktopts **optp;
1574 optp = &in6p->in6p_outputopts;
1575 error = ip6_pcbopt(optname,
1576 (u_char *)&optval, sizeof(optval),
1577 optp, (td != NULL) ? td->td_ucred :
1582 case IPV6_2292PKTINFO:
1583 case IPV6_2292HOPLIMIT:
1584 case IPV6_2292HOPOPTS:
1585 case IPV6_2292DSTOPTS:
1586 case IPV6_2292RTHDR:
1588 if (optlen != sizeof(int)) {
1592 error = sooptcopyin(sopt, &optval,
1593 sizeof optval, sizeof optval);
1597 case IPV6_2292PKTINFO:
1598 OPTSET2292(IN6P_PKTINFO);
1600 case IPV6_2292HOPLIMIT:
1601 OPTSET2292(IN6P_HOPLIMIT);
1603 case IPV6_2292HOPOPTS:
1605 * Check super-user privilege.
1606 * See comments for IPV6_RECVHOPOPTS.
1609 error = priv_check(td,
1610 PRIV_NETINET_SETHDROPTS);
1614 OPTSET2292(IN6P_HOPOPTS);
1616 case IPV6_2292DSTOPTS:
1618 error = priv_check(td,
1619 PRIV_NETINET_SETHDROPTS);
1623 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1625 case IPV6_2292RTHDR:
1626 OPTSET2292(IN6P_RTHDR);
1634 case IPV6_RTHDRDSTOPTS:
1637 /* new advanced API (RFC3542) */
1639 u_char optbuf_storage[MCLBYTES];
1641 struct ip6_pktopts **optp;
1643 /* cannot mix with RFC2292 */
1644 if (OPTBIT(IN6P_RFC2292)) {
1650 * We only ensure valsize is not too large
1651 * here. Further validation will be done
1654 error = sooptcopyin(sopt, optbuf_storage,
1655 sizeof(optbuf_storage), 0);
1658 optlen = sopt->sopt_valsize;
1659 optbuf = optbuf_storage;
1660 optp = &in6p->in6p_outputopts;
1661 error = ip6_pcbopt(optname, optbuf, optlen,
1662 optp, (td != NULL) ? td->td_ucred : NULL,
1668 case IPV6_MULTICAST_IF:
1669 case IPV6_MULTICAST_HOPS:
1670 case IPV6_MULTICAST_LOOP:
1671 case IPV6_JOIN_GROUP:
1672 case IPV6_LEAVE_GROUP:
1674 case MCAST_BLOCK_SOURCE:
1675 case MCAST_UNBLOCK_SOURCE:
1676 case MCAST_JOIN_GROUP:
1677 case MCAST_LEAVE_GROUP:
1678 case MCAST_JOIN_SOURCE_GROUP:
1679 case MCAST_LEAVE_SOURCE_GROUP:
1680 error = ip6_setmoptions(in6p, sopt);
1683 case IPV6_PORTRANGE:
1684 error = sooptcopyin(sopt, &optval,
1685 sizeof optval, sizeof optval);
1691 case IPV6_PORTRANGE_DEFAULT:
1692 in6p->inp_flags &= ~(INP_LOWPORT);
1693 in6p->inp_flags &= ~(INP_HIGHPORT);
1696 case IPV6_PORTRANGE_HIGH:
1697 in6p->inp_flags &= ~(INP_LOWPORT);
1698 in6p->inp_flags |= INP_HIGHPORT;
1701 case IPV6_PORTRANGE_LOW:
1702 in6p->inp_flags &= ~(INP_HIGHPORT);
1703 in6p->inp_flags |= INP_LOWPORT;
1714 case IPV6_IPSEC_POLICY:
1719 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1721 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1723 req = mtod(m, caddr_t);
1724 error = ipsec_set_policy(in6p, optname, req,
1725 m->m_len, (sopt->sopt_td != NULL) ?
1726 sopt->sopt_td->td_ucred : NULL);
1733 error = ENOPROTOOPT;
1741 case IPV6_2292PKTOPTIONS:
1742 #ifdef IPV6_PKTOPTIONS
1743 case IPV6_PKTOPTIONS:
1746 * RFC3542 (effectively) deprecated the
1747 * semantics of the 2292-style pktoptions.
1748 * Since it was not reliable in nature (i.e.,
1749 * applications had to expect the lack of some
1750 * information after all), it would make sense
1751 * to simplify this part by always returning
1754 sopt->sopt_valsize = 0;
1757 case IPV6_RECVHOPOPTS:
1758 case IPV6_RECVDSTOPTS:
1759 case IPV6_RECVRTHDRDSTOPTS:
1760 case IPV6_UNICAST_HOPS:
1761 case IPV6_RECVPKTINFO:
1762 case IPV6_RECVHOPLIMIT:
1763 case IPV6_RECVRTHDR:
1764 case IPV6_RECVPATHMTU:
1768 case IPV6_PORTRANGE:
1769 case IPV6_RECVTCLASS:
1770 case IPV6_AUTOFLOWLABEL:
1774 case IPV6_RECVHOPOPTS:
1775 optval = OPTBIT(IN6P_HOPOPTS);
1778 case IPV6_RECVDSTOPTS:
1779 optval = OPTBIT(IN6P_DSTOPTS);
1782 case IPV6_RECVRTHDRDSTOPTS:
1783 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1786 case IPV6_UNICAST_HOPS:
1787 optval = in6p->in6p_hops;
1790 case IPV6_RECVPKTINFO:
1791 optval = OPTBIT(IN6P_PKTINFO);
1794 case IPV6_RECVHOPLIMIT:
1795 optval = OPTBIT(IN6P_HOPLIMIT);
1798 case IPV6_RECVRTHDR:
1799 optval = OPTBIT(IN6P_RTHDR);
1802 case IPV6_RECVPATHMTU:
1803 optval = OPTBIT(IN6P_MTU);
1807 optval = OPTBIT(INP_FAITH);
1811 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1814 case IPV6_PORTRANGE:
1817 flags = in6p->inp_flags;
1818 if (flags & INP_HIGHPORT)
1819 optval = IPV6_PORTRANGE_HIGH;
1820 else if (flags & INP_LOWPORT)
1821 optval = IPV6_PORTRANGE_LOW;
1826 case IPV6_RECVTCLASS:
1827 optval = OPTBIT(IN6P_TCLASS);
1830 case IPV6_AUTOFLOWLABEL:
1831 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1835 optval = OPTBIT(INP_BINDANY);
1840 error = sooptcopyout(sopt, &optval,
1847 struct ip6_mtuinfo mtuinfo;
1848 struct route_in6 sro;
1850 bzero(&sro, sizeof(sro));
1852 if (!(so->so_state & SS_ISCONNECTED))
1855 * XXX: we dot not consider the case of source
1856 * routing, or optional information to specify
1857 * the outgoing interface.
1859 error = ip6_getpmtu(&sro, NULL, NULL,
1860 &in6p->in6p_faddr, &pmtu, NULL,
1866 if (pmtu > IPV6_MAXPACKET)
1867 pmtu = IPV6_MAXPACKET;
1869 bzero(&mtuinfo, sizeof(mtuinfo));
1870 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1871 optdata = (void *)&mtuinfo;
1872 optdatalen = sizeof(mtuinfo);
1873 error = sooptcopyout(sopt, optdata,
1878 case IPV6_2292PKTINFO:
1879 case IPV6_2292HOPLIMIT:
1880 case IPV6_2292HOPOPTS:
1881 case IPV6_2292RTHDR:
1882 case IPV6_2292DSTOPTS:
1884 case IPV6_2292PKTINFO:
1885 optval = OPTBIT(IN6P_PKTINFO);
1887 case IPV6_2292HOPLIMIT:
1888 optval = OPTBIT(IN6P_HOPLIMIT);
1890 case IPV6_2292HOPOPTS:
1891 optval = OPTBIT(IN6P_HOPOPTS);
1893 case IPV6_2292RTHDR:
1894 optval = OPTBIT(IN6P_RTHDR);
1896 case IPV6_2292DSTOPTS:
1897 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1900 error = sooptcopyout(sopt, &optval,
1907 case IPV6_RTHDRDSTOPTS:
1911 case IPV6_USE_MIN_MTU:
1912 case IPV6_PREFER_TEMPADDR:
1913 error = ip6_getpcbopt(in6p->in6p_outputopts,
1917 case IPV6_MULTICAST_IF:
1918 case IPV6_MULTICAST_HOPS:
1919 case IPV6_MULTICAST_LOOP:
1921 error = ip6_getmoptions(in6p, sopt);
1925 case IPV6_IPSEC_POLICY:
1929 struct mbuf *m = NULL;
1930 struct mbuf **mp = &m;
1931 size_t ovalsize = sopt->sopt_valsize;
1932 caddr_t oval = (caddr_t)sopt->sopt_val;
1934 error = soopt_getm(sopt, &m); /* XXX */
1937 error = soopt_mcopyin(sopt, m); /* XXX */
1940 sopt->sopt_valsize = ovalsize;
1941 sopt->sopt_val = oval;
1943 req = mtod(m, caddr_t);
1946 error = ipsec_get_policy(in6p, req, len, mp);
1948 error = soopt_mcopyout(sopt, m); /* XXX */
1949 if (error == 0 && m)
1956 error = ENOPROTOOPT;
1966 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1968 int error = 0, optval, optlen;
1969 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1970 struct inpcb *in6p = sotoinpcb(so);
1971 int level, op, optname;
1973 level = sopt->sopt_level;
1974 op = sopt->sopt_dir;
1975 optname = sopt->sopt_name;
1976 optlen = sopt->sopt_valsize;
1978 if (level != IPPROTO_IPV6) {
1985 * For ICMPv6 sockets, no modification allowed for checksum
1986 * offset, permit "no change" values to help existing apps.
1988 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
1989 * for an ICMPv6 socket will fail."
1990 * The current behavior does not meet RFC3542.
1994 if (optlen != sizeof(int)) {
1998 error = sooptcopyin(sopt, &optval, sizeof(optval),
2002 if ((optval % 2) != 0) {
2003 /* the API assumes even offset values */
2005 } else if (so->so_proto->pr_protocol ==
2007 if (optval != icmp6off)
2010 in6p->in6p_cksum = optval;
2014 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2017 optval = in6p->in6p_cksum;
2019 error = sooptcopyout(sopt, &optval, sizeof(optval));
2029 error = ENOPROTOOPT;
2037 * Set up IP6 options in pcb for insertion in output packets or
2038 * specifying behavior of outgoing packets.
2041 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2042 struct socket *so, struct sockopt *sopt)
2044 struct ip6_pktopts *opt = *pktopt;
2046 struct thread *td = sopt->sopt_td;
2048 /* turn off any old options. */
2051 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2052 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2053 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2054 printf("ip6_pcbopts: all specified options are cleared.\n");
2056 ip6_clearpktopts(opt, -1);
2058 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2061 if (!m || m->m_len == 0) {
2063 * Only turning off any previous options, regardless of
2064 * whether the opt is just created or given.
2066 free(opt, M_IP6OPT);
2070 /* set options specified by user. */
2071 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2072 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2073 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2074 free(opt, M_IP6OPT);
2082 * initialize ip6_pktopts. beware that there are non-zero default values in
2086 ip6_initpktopts(struct ip6_pktopts *opt)
2089 bzero(opt, sizeof(*opt));
2090 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2091 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2092 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2093 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2097 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2098 struct ucred *cred, int uproto)
2100 struct ip6_pktopts *opt;
2102 if (*pktopt == NULL) {
2103 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2105 ip6_initpktopts(*pktopt);
2109 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2113 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2115 void *optdata = NULL;
2117 struct ip6_ext *ip6e;
2119 struct in6_pktinfo null_pktinfo;
2120 int deftclass = 0, on;
2121 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2122 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2126 if (pktopt && pktopt->ip6po_pktinfo)
2127 optdata = (void *)pktopt->ip6po_pktinfo;
2129 /* XXX: we don't have to do this every time... */
2130 bzero(&null_pktinfo, sizeof(null_pktinfo));
2131 optdata = (void *)&null_pktinfo;
2133 optdatalen = sizeof(struct in6_pktinfo);
2136 if (pktopt && pktopt->ip6po_tclass >= 0)
2137 optdata = (void *)&pktopt->ip6po_tclass;
2139 optdata = (void *)&deftclass;
2140 optdatalen = sizeof(int);
2143 if (pktopt && pktopt->ip6po_hbh) {
2144 optdata = (void *)pktopt->ip6po_hbh;
2145 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2146 optdatalen = (ip6e->ip6e_len + 1) << 3;
2150 if (pktopt && pktopt->ip6po_rthdr) {
2151 optdata = (void *)pktopt->ip6po_rthdr;
2152 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2153 optdatalen = (ip6e->ip6e_len + 1) << 3;
2156 case IPV6_RTHDRDSTOPTS:
2157 if (pktopt && pktopt->ip6po_dest1) {
2158 optdata = (void *)pktopt->ip6po_dest1;
2159 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2160 optdatalen = (ip6e->ip6e_len + 1) << 3;
2164 if (pktopt && pktopt->ip6po_dest2) {
2165 optdata = (void *)pktopt->ip6po_dest2;
2166 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2167 optdatalen = (ip6e->ip6e_len + 1) << 3;
2171 if (pktopt && pktopt->ip6po_nexthop) {
2172 optdata = (void *)pktopt->ip6po_nexthop;
2173 optdatalen = pktopt->ip6po_nexthop->sa_len;
2176 case IPV6_USE_MIN_MTU:
2178 optdata = (void *)&pktopt->ip6po_minmtu;
2180 optdata = (void *)&defminmtu;
2181 optdatalen = sizeof(int);
2184 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2188 optdata = (void *)&on;
2189 optdatalen = sizeof(on);
2191 case IPV6_PREFER_TEMPADDR:
2193 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2195 optdata = (void *)&defpreftemp;
2196 optdatalen = sizeof(int);
2198 default: /* should not happen */
2200 panic("ip6_getpcbopt: unexpected option\n");
2202 return (ENOPROTOOPT);
2205 error = sooptcopyout(sopt, optdata, optdatalen);
2211 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2216 if (optname == -1 || optname == IPV6_PKTINFO) {
2217 if (pktopt->ip6po_pktinfo)
2218 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2219 pktopt->ip6po_pktinfo = NULL;
2221 if (optname == -1 || optname == IPV6_HOPLIMIT)
2222 pktopt->ip6po_hlim = -1;
2223 if (optname == -1 || optname == IPV6_TCLASS)
2224 pktopt->ip6po_tclass = -1;
2225 if (optname == -1 || optname == IPV6_NEXTHOP) {
2226 if (pktopt->ip6po_nextroute.ro_rt) {
2227 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2228 pktopt->ip6po_nextroute.ro_rt = NULL;
2230 if (pktopt->ip6po_nexthop)
2231 free(pktopt->ip6po_nexthop, M_IP6OPT);
2232 pktopt->ip6po_nexthop = NULL;
2234 if (optname == -1 || optname == IPV6_HOPOPTS) {
2235 if (pktopt->ip6po_hbh)
2236 free(pktopt->ip6po_hbh, M_IP6OPT);
2237 pktopt->ip6po_hbh = NULL;
2239 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2240 if (pktopt->ip6po_dest1)
2241 free(pktopt->ip6po_dest1, M_IP6OPT);
2242 pktopt->ip6po_dest1 = NULL;
2244 if (optname == -1 || optname == IPV6_RTHDR) {
2245 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2246 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2247 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2248 if (pktopt->ip6po_route.ro_rt) {
2249 RTFREE(pktopt->ip6po_route.ro_rt);
2250 pktopt->ip6po_route.ro_rt = NULL;
2253 if (optname == -1 || optname == IPV6_DSTOPTS) {
2254 if (pktopt->ip6po_dest2)
2255 free(pktopt->ip6po_dest2, M_IP6OPT);
2256 pktopt->ip6po_dest2 = NULL;
2260 #define PKTOPT_EXTHDRCPY(type) \
2263 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2264 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2265 if (dst->type == NULL && canwait == M_NOWAIT)\
2267 bcopy(src->type, dst->type, hlen);\
2269 } while (/*CONSTCOND*/ 0)
2272 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2274 if (dst == NULL || src == NULL) {
2275 printf("ip6_clearpktopts: invalid argument\n");
2279 dst->ip6po_hlim = src->ip6po_hlim;
2280 dst->ip6po_tclass = src->ip6po_tclass;
2281 dst->ip6po_flags = src->ip6po_flags;
2282 dst->ip6po_minmtu = src->ip6po_minmtu;
2283 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2284 if (src->ip6po_pktinfo) {
2285 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2287 if (dst->ip6po_pktinfo == NULL)
2289 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2291 if (src->ip6po_nexthop) {
2292 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2294 if (dst->ip6po_nexthop == NULL)
2296 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2297 src->ip6po_nexthop->sa_len);
2299 PKTOPT_EXTHDRCPY(ip6po_hbh);
2300 PKTOPT_EXTHDRCPY(ip6po_dest1);
2301 PKTOPT_EXTHDRCPY(ip6po_dest2);
2302 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2306 ip6_clearpktopts(dst, -1);
2309 #undef PKTOPT_EXTHDRCPY
2311 struct ip6_pktopts *
2312 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2315 struct ip6_pktopts *dst;
2317 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2320 ip6_initpktopts(dst);
2322 if ((error = copypktopts(dst, src, canwait)) != 0) {
2323 free(dst, M_IP6OPT);
2331 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2336 ip6_clearpktopts(pktopt, -1);
2338 free(pktopt, M_IP6OPT);
2342 * Set IPv6 outgoing packet options based on advanced API.
2345 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2346 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2348 struct cmsghdr *cm = 0;
2350 if (control == NULL || opt == NULL)
2353 ip6_initpktopts(opt);
2358 * If stickyopt is provided, make a local copy of the options
2359 * for this particular packet, then override them by ancillary
2361 * XXX: copypktopts() does not copy the cached route to a next
2362 * hop (if any). This is not very good in terms of efficiency,
2363 * but we can allow this since this option should be rarely
2366 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2371 * XXX: Currently, we assume all the optional information is stored
2374 if (control->m_next)
2377 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2378 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2381 if (control->m_len < CMSG_LEN(0))
2384 cm = mtod(control, struct cmsghdr *);
2385 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2387 if (cm->cmsg_level != IPPROTO_IPV6)
2390 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2391 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2400 * Set a particular packet option, as a sticky option or an ancillary data
2401 * item. "len" can be 0 only when it's a sticky option.
2402 * We have 4 cases of combination of "sticky" and "cmsg":
2403 * "sticky=0, cmsg=0": impossible
2404 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2405 * "sticky=1, cmsg=0": RFC3542 socket option
2406 * "sticky=1, cmsg=1": RFC2292 socket option
2409 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2410 struct ucred *cred, int sticky, int cmsg, int uproto)
2412 int minmtupolicy, preftemp;
2415 if (!sticky && !cmsg) {
2417 printf("ip6_setpktopt: impossible case\n");
2423 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2424 * not be specified in the context of RFC3542. Conversely,
2425 * RFC3542 types should not be specified in the context of RFC2292.
2429 case IPV6_2292PKTINFO:
2430 case IPV6_2292HOPLIMIT:
2431 case IPV6_2292NEXTHOP:
2432 case IPV6_2292HOPOPTS:
2433 case IPV6_2292DSTOPTS:
2434 case IPV6_2292RTHDR:
2435 case IPV6_2292PKTOPTIONS:
2436 return (ENOPROTOOPT);
2439 if (sticky && cmsg) {
2446 case IPV6_RTHDRDSTOPTS:
2448 case IPV6_USE_MIN_MTU:
2451 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2452 return (ENOPROTOOPT);
2457 case IPV6_2292PKTINFO:
2460 struct ifnet *ifp = NULL;
2461 struct in6_pktinfo *pktinfo;
2463 if (len != sizeof(struct in6_pktinfo))
2466 pktinfo = (struct in6_pktinfo *)buf;
2469 * An application can clear any sticky IPV6_PKTINFO option by
2470 * doing a "regular" setsockopt with ipi6_addr being
2471 * in6addr_any and ipi6_ifindex being zero.
2472 * [RFC 3542, Section 6]
2474 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2475 pktinfo->ipi6_ifindex == 0 &&
2476 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2477 ip6_clearpktopts(opt, optname);
2481 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2482 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2486 /* validate the interface index if specified. */
2487 if (pktinfo->ipi6_ifindex > V_if_index ||
2488 pktinfo->ipi6_ifindex < 0) {
2491 if (pktinfo->ipi6_ifindex) {
2492 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2498 * We store the address anyway, and let in6_selectsrc()
2499 * validate the specified address. This is because ipi6_addr
2500 * may not have enough information about its scope zone, and
2501 * we may need additional information (such as outgoing
2502 * interface or the scope zone of a destination address) to
2503 * disambiguate the scope.
2504 * XXX: the delay of the validation may confuse the
2505 * application when it is used as a sticky option.
2507 if (opt->ip6po_pktinfo == NULL) {
2508 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2509 M_IP6OPT, M_NOWAIT);
2510 if (opt->ip6po_pktinfo == NULL)
2513 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2517 case IPV6_2292HOPLIMIT:
2523 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2524 * to simplify the ordering among hoplimit options.
2526 if (optname == IPV6_HOPLIMIT && sticky)
2527 return (ENOPROTOOPT);
2529 if (len != sizeof(int))
2532 if (*hlimp < -1 || *hlimp > 255)
2535 opt->ip6po_hlim = *hlimp;
2543 if (len != sizeof(int))
2545 tclass = *(int *)buf;
2546 if (tclass < -1 || tclass > 255)
2549 opt->ip6po_tclass = tclass;
2553 case IPV6_2292NEXTHOP:
2556 error = priv_check_cred(cred,
2557 PRIV_NETINET_SETHDROPTS, 0);
2562 if (len == 0) { /* just remove the option */
2563 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2567 /* check if cmsg_len is large enough for sa_len */
2568 if (len < sizeof(struct sockaddr) || len < *buf)
2571 switch (((struct sockaddr *)buf)->sa_family) {
2574 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2577 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2580 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2581 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2584 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
2590 case AF_LINK: /* should eventually be supported */
2592 return (EAFNOSUPPORT);
2595 /* turn off the previous option, then set the new option. */
2596 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2597 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2598 if (opt->ip6po_nexthop == NULL)
2600 bcopy(buf, opt->ip6po_nexthop, *buf);
2603 case IPV6_2292HOPOPTS:
2606 struct ip6_hbh *hbh;
2610 * XXX: We don't allow a non-privileged user to set ANY HbH
2611 * options, since per-option restriction has too much
2615 error = priv_check_cred(cred,
2616 PRIV_NETINET_SETHDROPTS, 0);
2622 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2623 break; /* just remove the option */
2626 /* message length validation */
2627 if (len < sizeof(struct ip6_hbh))
2629 hbh = (struct ip6_hbh *)buf;
2630 hbhlen = (hbh->ip6h_len + 1) << 3;
2634 /* turn off the previous option, then set the new option. */
2635 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2636 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2637 if (opt->ip6po_hbh == NULL)
2639 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2644 case IPV6_2292DSTOPTS:
2646 case IPV6_RTHDRDSTOPTS:
2648 struct ip6_dest *dest, **newdest = NULL;
2651 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
2652 error = priv_check_cred(cred,
2653 PRIV_NETINET_SETHDROPTS, 0);
2659 ip6_clearpktopts(opt, optname);
2660 break; /* just remove the option */
2663 /* message length validation */
2664 if (len < sizeof(struct ip6_dest))
2666 dest = (struct ip6_dest *)buf;
2667 destlen = (dest->ip6d_len + 1) << 3;
2672 * Determine the position that the destination options header
2673 * should be inserted; before or after the routing header.
2676 case IPV6_2292DSTOPTS:
2678 * The old advacned API is ambiguous on this point.
2679 * Our approach is to determine the position based
2680 * according to the existence of a routing header.
2681 * Note, however, that this depends on the order of the
2682 * extension headers in the ancillary data; the 1st
2683 * part of the destination options header must appear
2684 * before the routing header in the ancillary data,
2686 * RFC3542 solved the ambiguity by introducing
2687 * separate ancillary data or option types.
2689 if (opt->ip6po_rthdr == NULL)
2690 newdest = &opt->ip6po_dest1;
2692 newdest = &opt->ip6po_dest2;
2694 case IPV6_RTHDRDSTOPTS:
2695 newdest = &opt->ip6po_dest1;
2698 newdest = &opt->ip6po_dest2;
2702 /* turn off the previous option, then set the new option. */
2703 ip6_clearpktopts(opt, optname);
2704 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2705 if (*newdest == NULL)
2707 bcopy(dest, *newdest, destlen);
2712 case IPV6_2292RTHDR:
2715 struct ip6_rthdr *rth;
2719 ip6_clearpktopts(opt, IPV6_RTHDR);
2720 break; /* just remove the option */
2723 /* message length validation */
2724 if (len < sizeof(struct ip6_rthdr))
2726 rth = (struct ip6_rthdr *)buf;
2727 rthlen = (rth->ip6r_len + 1) << 3;
2731 switch (rth->ip6r_type) {
2732 case IPV6_RTHDR_TYPE_0:
2733 if (rth->ip6r_len == 0) /* must contain one addr */
2735 if (rth->ip6r_len % 2) /* length must be even */
2737 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2741 return (EINVAL); /* not supported */
2744 /* turn off the previous option */
2745 ip6_clearpktopts(opt, IPV6_RTHDR);
2746 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
2747 if (opt->ip6po_rthdr == NULL)
2749 bcopy(rth, opt->ip6po_rthdr, rthlen);
2754 case IPV6_USE_MIN_MTU:
2755 if (len != sizeof(int))
2757 minmtupolicy = *(int *)buf;
2758 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2759 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2760 minmtupolicy != IP6PO_MINMTU_ALL) {
2763 opt->ip6po_minmtu = minmtupolicy;
2767 if (len != sizeof(int))
2770 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2772 * we ignore this option for TCP sockets.
2773 * (RFC3542 leaves this case unspecified.)
2775 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2777 opt->ip6po_flags |= IP6PO_DONTFRAG;
2780 case IPV6_PREFER_TEMPADDR:
2781 if (len != sizeof(int))
2783 preftemp = *(int *)buf;
2784 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2785 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2786 preftemp != IP6PO_TEMPADDR_PREFER) {
2789 opt->ip6po_prefer_tempaddr = preftemp;
2793 return (ENOPROTOOPT);
2794 } /* end of switch */
2800 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2801 * packet to the input queue of a specified interface. Note that this
2802 * calls the output routine of the loopback "driver", but with an interface
2803 * pointer that might NOT be &loif -- easier than replicating that code here.
2806 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2809 struct ip6_hdr *ip6;
2811 copym = m_copy(m, 0, M_COPYALL);
2816 * Make sure to deep-copy IPv6 header portion in case the data
2817 * is in an mbuf cluster, so that we can safely override the IPv6
2818 * header portion later.
2820 if ((copym->m_flags & M_EXT) != 0 ||
2821 copym->m_len < sizeof(struct ip6_hdr)) {
2822 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2828 if (copym->m_len < sizeof(*ip6)) {
2834 ip6 = mtod(copym, struct ip6_hdr *);
2836 * clear embedded scope identifiers if necessary.
2837 * in6_clearscope will touch the addresses only when necessary.
2839 in6_clearscope(&ip6->ip6_src);
2840 in6_clearscope(&ip6->ip6_dst);
2842 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
2846 * Chop IPv6 header off from the payload.
2849 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
2852 struct ip6_hdr *ip6;
2854 ip6 = mtod(m, struct ip6_hdr *);
2855 if (m->m_len > sizeof(*ip6)) {
2856 mh = m_gethdr(M_NOWAIT, MT_DATA);
2861 m_move_pkthdr(mh, m);
2862 MH_ALIGN(mh, sizeof(*ip6));
2863 m->m_len -= sizeof(*ip6);
2864 m->m_data += sizeof(*ip6);
2867 m->m_len = sizeof(*ip6);
2868 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2870 exthdrs->ip6e_ip6 = m;
2875 * Compute IPv6 extension header length.
2878 ip6_optlen(struct inpcb *in6p)
2882 if (!in6p->in6p_outputopts)
2887 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2889 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2890 if (in6p->in6p_outputopts->ip6po_rthdr)
2891 /* dest1 is valid with rthdr only */
2892 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2893 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2894 len += elen(in6p->in6p_outputopts->ip6po_dest2);