1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $ */
2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
64 #include "opt_ip6fw.h"
66 #include "opt_inet6.h"
68 #include <sys/param.h>
69 #include <sys/malloc.h>
71 #include <sys/errno.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
80 #include <sys/thread2.h>
81 #include <sys/msgport2.h>
84 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/ip6protosw.h>
97 #include <net/ip6fw/ip6_fw.h>
99 #include <net/net_osdep.h>
101 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
104 struct mbuf *ip6e_ip6;
105 struct mbuf *ip6e_hbh;
106 struct mbuf *ip6e_dest1;
107 struct mbuf *ip6e_rthdr;
108 struct mbuf *ip6e_dest2;
111 static int ip6_pcbopt (int, u_char *, int, struct ip6_pktopts **, int);
112 static int ip6_setpktoption (int, u_char *, int, struct ip6_pktopts *,
114 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *,
116 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
117 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
118 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
119 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
120 struct ifnet *, struct in6_addr *, u_long *, int *);
121 static int copyexthdr (void *, struct mbuf **);
122 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
124 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
125 static struct mbuf *ip6_splithdr (struct mbuf *);
126 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
129 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
130 * header (with pri, len, nxt, hlim, src, dst).
131 * This function may modify ver and hlim only.
132 * The mbuf chain containing the packet will be freed.
133 * The mbuf opt, if present, will not be freed.
135 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
136 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
137 * which is rt_rmx.rmx_mtu.
140 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
141 int flags, struct ip6_moptions *im6o,
142 struct ifnet **ifpp, /* XXX: just for statistics */
145 struct ip6_hdr *ip6, *mhip6;
146 struct ifnet *ifp, *origifp;
150 int hlen, tlen, len, off;
151 struct route_in6 ip6route;
152 struct sockaddr_in6 *dst;
154 struct in6_ifaddr *ia = NULL;
156 int alwaysfrag, dontfrag;
157 u_int32_t optlen, plen = 0, unfragpartlen;
158 struct ip6_exthdrs exthdrs;
159 struct in6_addr finaldst;
160 struct route_in6 *ro_pmtu = NULL;
161 boolean_t hdrsplit = FALSE;
163 bzero(&exthdrs, sizeof exthdrs);
166 if ((error = copyexthdr(opt->ip6po_hbh, &exthdrs.ip6e_hbh)))
168 if ((error = copyexthdr(opt->ip6po_dest1, &exthdrs.ip6e_dest1)))
170 if ((error = copyexthdr(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr)))
172 if ((error = copyexthdr(opt->ip6po_dest2, &exthdrs.ip6e_dest2)))
177 * Calculate the total length of the extension header chain.
178 * Keep the length of the unfragmentable part for fragmentation.
180 optlen = m_lengthm(exthdrs.ip6e_hbh, NULL) +
181 m_lengthm(exthdrs.ip6e_dest1, NULL) +
182 m_lengthm(exthdrs.ip6e_rthdr, NULL);
184 unfragpartlen = optlen + sizeof(struct ip6_hdr);
186 /* NOTE: we don't add AH/ESP length here. do that later. */
187 optlen += m_lengthm(exthdrs.ip6e_dest2, NULL);
190 * If there is at least one extension header,
191 * separate IP6 header from the payload.
193 if (optlen && !hdrsplit) {
194 exthdrs.ip6e_ip6 = ip6_splithdr(m);
195 if (exthdrs.ip6e_ip6 == NULL) {
199 m = exthdrs.ip6e_ip6;
204 ip6 = mtod(m, struct ip6_hdr *);
206 /* adjust mbuf packet header length */
207 m->m_pkthdr.len += optlen;
208 plen = m->m_pkthdr.len - sizeof(*ip6);
210 /* If this is a jumbo payload, insert a jumbo payload option. */
211 if (plen > IPV6_MAXPACKET) {
213 exthdrs.ip6e_ip6 = ip6_splithdr(m);
214 if (exthdrs.ip6e_ip6 == NULL) {
218 m = exthdrs.ip6e_ip6;
222 ip6 = mtod(m, struct ip6_hdr *);
223 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
227 ip6->ip6_plen = htons(plen);
230 * Concatenate headers and fill in next header fields.
231 * Here we have, on "m"
233 * and we insert headers accordingly. Finally, we should be getting:
234 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
236 * during the header composing process, "m" points to IPv6 header.
237 * "mprev" points to an extension header prior to esp.
240 nexthdrp = &ip6->ip6_nxt;
244 * we treat dest2 specially. the goal here is to make mprev point the
245 * mbuf prior to dest2.
247 * result: IPv6 dest2 payload
248 * m and mprev will point to IPv6 header.
250 if (exthdrs.ip6e_dest2) {
252 panic("assumption failed: hdr not split");
253 exthdrs.ip6e_dest2->m_next = m->m_next;
254 m->m_next = exthdrs.ip6e_dest2;
255 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
256 ip6->ip6_nxt = IPPROTO_DSTOPTS;
260 * Place m1 after mprev.
262 #define MAKE_CHAIN(m1, mprev, nexthdrp, i)\
266 panic("assumption failed: hdr not split");\
267 *mtod(m1, u_char *) = *nexthdrp;\
269 nexthdrp = mtod(m1, u_char *);\
270 m1->m_next = mprev->m_next;\
277 * result: IPv6 hbh dest1 rthdr dest2 payload
278 * m will point to IPv6 header. mprev will point to the
279 * extension header prior to dest2 (rthdr in the above case).
281 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
282 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
283 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
286 * If there is a routing header, replace the destination address field
287 * with the first hop of the routing header.
289 if (exthdrs.ip6e_rthdr) {
290 struct ip6_rthdr *rh;
292 finaldst = ip6->ip6_dst;
293 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
294 switch (rh->ip6r_type) {
295 default: /* is it possible? */
301 /* Source address validation */
302 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
303 !(flags & IPV6_DADOUTPUT)) {
305 ip6stat.ip6s_badscope++;
308 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
310 ip6stat.ip6s_badscope++;
314 ip6stat.ip6s_localout++;
321 bzero(ro, sizeof(*ro));
324 if (opt && opt->ip6po_rthdr)
325 ro = &opt->ip6po_route;
326 dst = (struct sockaddr_in6 *)&ro->ro_dst;
329 * If there is a cached route,
330 * check that it is to the same destination
331 * and is still up. If not, free it and try again.
333 if (ro->ro_rt != NULL &&
334 (!(ro->ro_rt->rt_flags & RTF_UP) || dst->sin6_family != AF_INET6 ||
335 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
339 if (ro->ro_rt == NULL) {
340 bzero(dst, sizeof(*dst));
341 dst->sin6_family = AF_INET6;
342 dst->sin6_len = sizeof(struct sockaddr_in6);
343 dst->sin6_addr = ip6->ip6_dst;
345 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
348 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
349 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
351 * interface selection comes here
352 * if an interface is specified from an upper layer,
355 if (ro->ro_rt == NULL) {
357 * non-bsdi always clone routes, if parent is
360 rtalloc((struct route *)ro);
362 if (ro->ro_rt == NULL) {
363 ip6stat.ip6s_noroute++;
364 error = EHOSTUNREACH;
365 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
368 ia = ifatoia6(ro->ro_rt->rt_ifa);
369 ifp = ro->ro_rt->rt_ifp;
371 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
372 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
373 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
375 in6_ifstat_inc(ifp, ifs6_out_request);
378 * Check if the outgoing interface conflicts with
379 * the interface specified by ifi6_ifindex (if specified).
380 * Note that loopback interface is always okay.
381 * (this may happen when we are sending a packet to one of
382 * our own addresses.)
384 if (opt && opt->ip6po_pktinfo
385 && opt->ip6po_pktinfo->ipi6_ifindex) {
386 if (!(ifp->if_flags & IFF_LOOPBACK)
387 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
388 ip6stat.ip6s_noroute++;
389 in6_ifstat_inc(ifp, ifs6_out_discard);
390 error = EHOSTUNREACH;
395 if (opt && opt->ip6po_hlim != -1)
396 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
399 struct in6_multi *in6m;
401 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
404 * See if the caller provided any multicast options
408 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
409 if (im6o->im6o_multicast_ifp != NULL)
410 ifp = im6o->im6o_multicast_ifp;
412 ip6->ip6_hlim = ip6_defmcasthlim;
415 * See if the caller provided the outgoing interface
416 * as an ancillary data.
417 * Boundary check for ifindex is assumed to be already done.
419 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
420 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
423 * If the destination is a node-local scope multicast,
424 * the packet should be loop-backed only.
426 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
428 * If the outgoing interface is already specified,
429 * it should be a loopback interface.
431 if (ifp && !(ifp->if_flags & IFF_LOOPBACK)) {
432 ip6stat.ip6s_badscope++;
433 error = ENETUNREACH; /* XXX: better error? */
434 /* XXX correct ifp? */
435 in6_ifstat_inc(ifp, ifs6_out_discard);
442 if (opt && opt->ip6po_hlim != -1)
443 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
446 * If caller did not provide an interface lookup a
447 * default in the routing table. This is either a
448 * default for the speicfied group (i.e. a host
449 * route), or a multicast default (a route for the
453 if (ro->ro_rt == NULL) {
455 rtpurelookup((struct sockaddr *)&ro->ro_dst);
457 if (ro->ro_rt == NULL) {
458 ip6stat.ip6s_noroute++;
459 error = EHOSTUNREACH;
460 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
463 ia = ifatoia6(ro->ro_rt->rt_ifa);
464 ifp = ro->ro_rt->rt_ifp;
468 if (!(flags & IPV6_FORWARDING))
469 in6_ifstat_inc(ifp, ifs6_out_request);
470 in6_ifstat_inc(ifp, ifs6_out_mcast);
473 * Confirm that the outgoing interface supports multicast.
475 if (!(ifp->if_flags & IFF_MULTICAST)) {
476 ip6stat.ip6s_noroute++;
477 in6_ifstat_inc(ifp, ifs6_out_discard);
481 in6m = IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp);
483 (im6o == NULL || im6o->im6o_multicast_loop)) {
485 * If we belong to the destination multicast group
486 * on the outgoing interface, and the caller did not
487 * forbid loopback, loop back a copy.
489 ip6_mloopback(ifp, m, dst);
492 * If we are acting as a multicast router, perform
493 * multicast forwarding as if the packet had just
494 * arrived on the interface to which we are about
495 * to send. The multicast forwarding function
496 * recursively calls this function, using the
497 * IPV6_FORWARDING flag to prevent infinite recursion.
499 * Multicasts that are looped back by ip6_mloopback(),
500 * above, will be forwarded by the ip6_input() routine,
503 if (ip6_mrouter && !(flags & IPV6_FORWARDING)) {
504 if (ip6_mforward(ip6, ifp, m) != 0) {
511 * Multicasts with a hoplimit of zero may be looped back,
512 * above, but must not be transmitted on a network.
513 * Also, multicasts addressed to the loopback interface
514 * are not sent -- the above call to ip6_mloopback() will
515 * loop back a copy if this host actually belongs to the
516 * destination group on the loopback interface.
518 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
525 * Fill the outgoing inteface to tell the upper layer
526 * to increment per-interface statistics.
531 /* Determine path MTU. */
532 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
537 * The caller of this function may specify to use the minimum MTU
539 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
540 * setting. The logic is a bit complicated; by default, unicast
541 * packets will follow path MTU while multicast packets will be sent at
542 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
543 * including unicast ones will be sent at the minimum MTU. Multicast
544 * packets will always be sent at the minimum MTU unless
545 * IP6PO_MINMTU_DISABLE is explicitly specified.
546 * See RFC 3542 for more details.
548 if (mtu > IPV6_MMTU) {
549 if ((flags & IPV6_MINMTU))
551 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
553 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
555 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
560 /* Fake scoped addresses */
561 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
563 * If source or destination address is a scoped address, and
564 * the packet is going to be sent to a loopback interface,
565 * we should keep the original interface.
569 * XXX: this is a very experimental and temporary solution.
570 * We eventually have sockaddr_in6 and use the sin6_scope_id
571 * field of the structure here.
572 * We rely on the consistency between two scope zone ids
573 * of source and destination, which should already be assured.
574 * Larger scopes than link will be supported in the future.
577 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
578 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
579 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
580 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
582 * XXX: origifp can be NULL even in those two cases above.
583 * For example, if we remove the (only) link-local address
584 * from the loopback interface, and try to send a link-local
585 * address without link-id information. Then the source
586 * address is ::1, and the destination address is the
587 * link-local address with its s6_addr16[1] being zero.
588 * What is worse, if the packet goes to the loopback interface
589 * by a default rejected route, the null pointer would be
590 * passed to looutput, and the kernel would hang.
591 * The following last resort would prevent such disaster.
599 * clear embedded scope identifiers if necessary.
600 * in6_clearscope will touch the addresses only when necessary.
602 in6_clearscope(&ip6->ip6_src);
603 in6_clearscope(&ip6->ip6_dst);
606 * Check with the firewall...
608 if (ip6_fw_enable && ip6_fw_chk_ptr) {
611 m->m_pkthdr.rcvif = NULL; /* XXX */
612 /* If ipfw says divert, we have to just drop packet */
613 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
624 * If the outgoing packet contains a hop-by-hop options header,
625 * it must be examined and processed even by the source node.
626 * (RFC 2460, section 4.)
628 if (exthdrs.ip6e_hbh) {
629 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
630 u_int32_t dummy1; /* XXX unused */
631 u_int32_t dummy2; /* XXX unused */
634 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
635 panic("ip6e_hbh is not continuous");
638 * XXX: if we have to send an ICMPv6 error to the sender,
639 * we need the M_LOOP flag since icmp6_error() expects
640 * the IPv6 and the hop-by-hop options header are
641 * continuous unless the flag is set.
643 m->m_flags |= M_LOOP;
644 m->m_pkthdr.rcvif = ifp;
645 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
646 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
647 &dummy1, &dummy2) < 0) {
648 /* m was already freed at this point */
649 error = EINVAL;/* better error? */
652 m->m_flags &= ~M_LOOP; /* XXX */
653 m->m_pkthdr.rcvif = NULL;
657 * Run through list of hooks for output packets.
659 if (pfil_has_hooks(&inet6_pfil_hook)) {
660 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
661 if (error != 0 || m == NULL)
663 ip6 = mtod(m, struct ip6_hdr *);
667 * Send the packet to the outgoing interface.
668 * If necessary, do IPv6 fragmentation before sending.
670 * the logic here is rather complex:
671 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
672 * 1-a: send as is if tlen <= path mtu
673 * 1-b: fragment if tlen > path mtu
675 * 2: if user asks us not to fragment (dontfrag == 1)
676 * 2-a: send as is if tlen <= interface mtu
677 * 2-b: error if tlen > interface mtu
679 * 3: if we always need to attach fragment header (alwaysfrag == 1)
682 * 4: if dontfrag == 1 && alwaysfrag == 1
683 * error, as we cannot handle this conflicting request
685 tlen = m->m_pkthdr.len;
687 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
691 if (dontfrag && alwaysfrag) { /* case 4 */
692 /* conflicting request - can't transmit */
696 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
698 * Even if the DONTFRAG option is specified, we cannot send the
699 * packet when the data length is larger than the MTU of the
700 * outgoing interface.
701 * Notify the error by sending IPV6_PATHMTU ancillary data as
702 * well as returning an error code (the latter is not described
706 struct ip6ctlparam ip6cp;
708 mtu32 = (u_int32_t)mtu;
709 bzero(&ip6cp, sizeof(ip6cp));
710 ip6cp.ip6c_cmdarg = (void *)&mtu32;
711 kpfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
719 * transmit packet without fragmentation
721 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
722 struct in6_ifaddr *ia6;
724 ip6 = mtod(m, struct ip6_hdr *);
725 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
727 /* Record statistics for this interface address. */
728 IFA_STAT_INC(&ia6->ia_ifa, opackets, 1);
729 IFA_STAT_INC(&ia6->ia_ifa, obytes, m->m_pkthdr.len);
731 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
736 * try to fragment the packet. case 1-b and 3
738 if (mtu < IPV6_MMTU) {
740 * note that path MTU is never less than IPV6_MMTU
744 in6_ifstat_inc(ifp, ifs6_out_fragfail);
746 } else if (ip6->ip6_plen == 0) {
747 /* jumbo payload cannot be fragmented */
749 in6_ifstat_inc(ifp, ifs6_out_fragfail);
752 struct mbuf **mnext, *m_frgpart;
753 struct ip6_frag *ip6f;
754 u_int32_t id = htonl(ip6_id++);
758 * Too large for the destination or interface;
759 * fragment if possible.
760 * Must be able to put at least 8 bytes per fragment.
762 hlen = unfragpartlen;
763 if (mtu > IPV6_MAXPACKET)
764 mtu = IPV6_MAXPACKET;
766 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
769 in6_ifstat_inc(ifp, ifs6_out_fragfail);
773 mnext = &m->m_nextpkt;
776 * Change the next header field of the last header in the
777 * unfragmentable part.
779 if (exthdrs.ip6e_rthdr) {
780 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
781 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
782 } else if (exthdrs.ip6e_dest1) {
783 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
784 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
785 } else if (exthdrs.ip6e_hbh) {
786 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
787 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
789 nextproto = ip6->ip6_nxt;
790 ip6->ip6_nxt = IPPROTO_FRAGMENT;
794 * Loop through length of segment after first fragment,
795 * make new header and copy data of each part and link onto
799 for (off = hlen; off < tlen; off += len) {
800 MGETHDR(m, M_NOWAIT, MT_HEADER);
803 ip6stat.ip6s_odropped++;
806 m->m_pkthdr.rcvif = NULL;
807 m->m_flags = m0->m_flags & M_COPYFLAGS;
809 mnext = &m->m_nextpkt;
810 m->m_data += max_linkhdr;
811 mhip6 = mtod(m, struct ip6_hdr *);
813 m->m_len = sizeof(*mhip6);
814 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
816 ip6stat.ip6s_odropped++;
819 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
820 if (off + len >= tlen)
823 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
824 mhip6->ip6_plen = htons((u_short)(len + hlen +
825 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
826 if ((m_frgpart = m_copy(m0, off, len)) == NULL) {
828 ip6stat.ip6s_odropped++;
832 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
833 m->m_pkthdr.rcvif = NULL;
834 ip6f->ip6f_reserved = 0;
835 ip6f->ip6f_ident = id;
836 ip6f->ip6f_nxt = nextproto;
837 ip6stat.ip6s_ofragments++;
838 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
841 in6_ifstat_inc(ifp, ifs6_out_fragok);
845 * Remove leading garbages.
849 m0->m_nextpkt = NULL;
851 for (m0 = m; m; m = m0) {
855 /* Record statistics for this interface address. */
857 IFA_STAT_INC(&ia->ia_ifa, opackets, 1);
858 IFA_STAT_INC(&ia->ia_ifa, obytes,
861 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
867 ip6stat.ip6s_fragmented++;
870 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
872 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
873 RTFREE(ro_pmtu->ro_rt);
879 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
880 m_freem(exthdrs.ip6e_dest1);
881 m_freem(exthdrs.ip6e_rthdr);
882 m_freem(exthdrs.ip6e_dest2);
890 copyexthdr(void *h, struct mbuf **mp)
892 struct ip6_ext *hdr = h;
899 hlen = (hdr->ip6e_len + 1) * 8;
901 return ENOBUFS; /* XXX */
903 m = m_getb(hlen, M_NOWAIT, MT_DATA, 0);
908 bcopy(hdr, mtod(m, caddr_t), hlen);
915 * Insert jumbo payload option.
918 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
924 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
927 * If there is no hop-by-hop options header, allocate new one.
928 * If there is one but it doesn't have enough space to store the
929 * jumbo payload option, allocate a cluster to store the whole options.
930 * Otherwise, use it to store the options.
932 if (exthdrs->ip6e_hbh == NULL) {
933 MGET(mopt, M_NOWAIT, MT_DATA);
936 mopt->m_len = JUMBOOPTLEN;
937 optbuf = mtod(mopt, u_char *);
938 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
939 exthdrs->ip6e_hbh = mopt;
943 mopt = exthdrs->ip6e_hbh;
944 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
947 * - exthdrs->ip6e_hbh is not referenced from places
948 * other than exthdrs.
949 * - exthdrs->ip6e_hbh is not an mbuf chain.
951 int oldoptlen = mopt->m_len;
955 * XXX: give up if the whole (new) hbh header does
956 * not fit even in an mbuf cluster.
958 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
962 * As a consequence, we must always prepare a cluster
965 n = m_getcl(M_NOWAIT, MT_DATA, 0);
968 n->m_len = oldoptlen + JUMBOOPTLEN;
969 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), oldoptlen);
970 optbuf = mtod(n, caddr_t) + oldoptlen;
972 mopt = exthdrs->ip6e_hbh = n;
974 optbuf = mtod(mopt, u_char *) + mopt->m_len;
975 mopt->m_len += JUMBOOPTLEN;
977 optbuf[0] = IP6OPT_PADN;
981 * Adjust the header length according to the pad and
982 * the jumbo payload option.
984 hbh = mtod(mopt, struct ip6_hbh *);
985 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
988 /* fill in the option. */
989 optbuf[2] = IP6OPT_JUMBO;
991 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
992 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
994 /* finally, adjust the packet header length */
995 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1002 * Insert fragment header and copy unfragmentable header portions.
1005 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1006 struct ip6_frag **frghdrp)
1008 struct mbuf *n, *mlast;
1010 if (hlen > sizeof(struct ip6_hdr)) {
1011 n = m_copym(m0, sizeof(struct ip6_hdr),
1012 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1019 /* Search for the last mbuf of unfragmentable part. */
1020 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1023 if (!(mlast->m_flags & M_EXT) &&
1024 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1025 /* use the trailing space of the last mbuf for the fragment hdr */
1026 *frghdrp = (struct ip6_frag *)
1027 (mtod(mlast, caddr_t) + mlast->m_len);
1028 mlast->m_len += sizeof(struct ip6_frag);
1029 m->m_pkthdr.len += sizeof(struct ip6_frag);
1031 /* allocate a new mbuf for the fragment header */
1034 MGET(mfrg, M_NOWAIT, MT_DATA);
1037 mfrg->m_len = sizeof(struct ip6_frag);
1038 *frghdrp = mtod(mfrg, struct ip6_frag *);
1039 mlast->m_next = mfrg;
1046 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1047 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1054 if (ro_pmtu != ro) {
1055 /* The first hop and the final destination may differ. */
1056 struct sockaddr_in6 *sa6_dst =
1057 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1058 if (ro_pmtu->ro_rt &&
1059 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1060 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1061 RTFREE(ro_pmtu->ro_rt);
1062 ro_pmtu->ro_rt = NULL;
1064 if (ro_pmtu->ro_rt == NULL) {
1065 bzero(sa6_dst, sizeof(*sa6_dst));
1066 sa6_dst->sin6_family = AF_INET6;
1067 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1068 sa6_dst->sin6_addr = *dst;
1070 rtalloc((struct route *)ro_pmtu);
1073 if (ro_pmtu->ro_rt) {
1077 ifp = ro_pmtu->ro_rt->rt_ifp;
1078 ifmtu = IN6_LINKMTU(ifp);
1079 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1082 } else if (mtu < IPV6_MMTU) {
1084 * RFC2460 section 5, last paragraph:
1085 * if we record ICMPv6 too big message with
1086 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1087 * or smaller, with framgent header attached.
1088 * (fragment header is needed regardless from the
1089 * packet size, for translators to identify packets)
1093 } else if (mtu > ifmtu) {
1095 * The MTU on the route is larger than the MTU on
1096 * the interface! This shouldn't happen, unless the
1097 * MTU of the interface has been changed after the
1098 * interface was brought up. Change the MTU in the
1099 * route to match the interface MTU (as long as the
1100 * field isn't locked).
1103 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1106 mtu = IN6_LINKMTU(ifp);
1108 error = EHOSTUNREACH; /* XXX */
1113 *alwaysfragp = alwaysfrag;
1118 * IP6 socket option processing.
1121 ip6_ctloutput_dispatch(netmsg_t msg)
1125 error = ip6_ctloutput(msg->ctloutput.base.nm_so,
1126 msg->ctloutput.nm_sopt);
1127 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
1131 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1133 int optdatalen,uproto;
1135 struct inpcb *in6p = so->so_pcb;
1138 int level, op, optname;
1143 level = sopt->sopt_level;
1144 op = sopt->sopt_dir;
1145 optname = sopt->sopt_name;
1146 optlen = sopt->sopt_valsize;
1149 panic("ip6_ctloutput: arg soopt is NULL");
1155 uproto = (int)so->so_proto->pr_protocol;
1156 privileged = (td == NULL || priv_check(td, PRIV_ROOT)) ? 0 : 1;
1158 if (level == IPPROTO_IPV6) {
1163 case IPV6_2292PKTOPTIONS:
1164 #ifdef IPV6_PKTOPTIONS
1165 case IPV6_PKTOPTIONS:
1170 error = soopt_getm(sopt, &m); /* XXX */
1173 soopt_to_mbuf(sopt, m); /* XXX */
1174 error = ip6_pcbopts(&in6p->in6p_outputopts,
1176 m_freem(m); /* XXX */
1181 * Use of some Hop-by-Hop options or some
1182 * Destination options, might require special
1183 * privilege. That is, normal applications
1184 * (without special privilege) might be forbidden
1185 * from setting certain options in outgoing packets,
1186 * and might never see certain options in received
1187 * packets. [RFC 2292 Section 6]
1188 * KAME specific note:
1189 * KAME prevents non-privileged users from sending or
1190 * receiving ANY hbh/dst options in order to avoid
1191 * overhead of parsing options in the kernel.
1193 case IPV6_RECVHOPOPTS:
1194 case IPV6_RECVDSTOPTS:
1195 case IPV6_RECVRTHDRDSTOPTS:
1198 case IPV6_RECVPKTINFO:
1199 case IPV6_RECVHOPLIMIT:
1200 case IPV6_RECVRTHDR:
1201 case IPV6_RECVPATHMTU:
1202 case IPV6_RECVTCLASS:
1203 case IPV6_AUTOFLOWLABEL:
1206 case IPV6_UNICAST_HOPS:
1209 if (optlen != sizeof(int)) {
1213 error = soopt_to_kbuf(sopt, &optval,
1214 sizeof optval, sizeof optval);
1219 case IPV6_UNICAST_HOPS:
1220 if (optval < -1 || optval >= 256)
1223 /* -1 = kernel default */
1224 in6p->in6p_hops = optval;
1227 #define OPTSET(bit) \
1230 in6p->in6p_flags |= (bit); \
1232 in6p->in6p_flags &= ~(bit); \
1234 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1236 * Although changed to RFC3542, It's better to also support RFC2292 API
1238 #define OPTSET2292(bit) \
1240 in6p->in6p_flags |= IN6P_RFC2292; \
1242 in6p->in6p_flags |= (bit); \
1244 in6p->in6p_flags &= ~(bit); \
1245 } while (/*CONSTCOND*/ 0)
1247 case IPV6_RECVPKTINFO:
1248 /* cannot mix with RFC2292 */
1249 if (OPTBIT(IN6P_RFC2292)) {
1253 OPTSET(IN6P_PKTINFO);
1258 struct ip6_pktopts **optp;
1260 /* cannot mix with RFC2292 */
1261 if (OPTBIT(IN6P_RFC2292)) {
1265 optp = &in6p->in6p_outputopts;
1266 error = ip6_pcbopt(IPV6_HOPLIMIT,
1267 (u_char *)&optval, sizeof(optval),
1272 case IPV6_RECVHOPLIMIT:
1273 /* cannot mix with RFC2292 */
1274 if (OPTBIT(IN6P_RFC2292)) {
1278 OPTSET(IN6P_HOPLIMIT);
1281 case IPV6_RECVHOPOPTS:
1282 /* cannot mix with RFC2292 */
1283 if (OPTBIT(IN6P_RFC2292)) {
1287 OPTSET(IN6P_HOPOPTS);
1290 case IPV6_RECVDSTOPTS:
1291 /* cannot mix with RFC2292 */
1292 if (OPTBIT(IN6P_RFC2292)) {
1296 OPTSET(IN6P_DSTOPTS);
1299 case IPV6_RECVRTHDRDSTOPTS:
1300 /* cannot mix with RFC2292 */
1301 if (OPTBIT(IN6P_RFC2292)) {
1305 OPTSET(IN6P_RTHDRDSTOPTS);
1308 case IPV6_RECVRTHDR:
1309 /* cannot mix with RFC2292 */
1310 if (OPTBIT(IN6P_RFC2292)) {
1317 case IPV6_RECVPATHMTU:
1319 * We ignore this option for TCP
1321 * (RFC3542 leaves this case
1324 if (uproto != IPPROTO_TCP)
1328 case IPV6_RECVTCLASS:
1329 /* cannot mix with RFC2292 XXX */
1330 if (OPTBIT(IN6P_RFC2292)) {
1334 OPTSET(IN6P_TCLASS);
1337 case IPV6_AUTOFLOWLABEL:
1338 OPTSET(IN6P_AUTOFLOWLABEL);
1343 * make setsockopt(IPV6_V6ONLY)
1344 * available only prior to bind(2).
1346 if (in6p->in6p_lport ||
1347 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1353 /* Don't allow v4-mapped */
1362 case IPV6_USE_MIN_MTU:
1363 case IPV6_PREFER_TEMPADDR:
1364 if (optlen != sizeof(optval)) {
1368 error = soopt_to_kbuf(sopt, &optval,
1369 sizeof optval, sizeof optval);
1373 struct ip6_pktopts **optp;
1374 optp = &in6p->in6p_outputopts;
1375 error = ip6_pcbopt(optname,
1376 (u_char *)&optval, sizeof(optval),
1381 case IPV6_2292PKTINFO:
1382 case IPV6_2292HOPLIMIT:
1383 case IPV6_2292HOPOPTS:
1384 case IPV6_2292DSTOPTS:
1385 case IPV6_2292RTHDR:
1387 if (optlen != sizeof(int)) {
1391 error = soopt_to_kbuf(sopt, &optval,
1392 sizeof optval, sizeof optval);
1396 case IPV6_2292PKTINFO:
1397 OPTSET2292(IN6P_PKTINFO);
1399 case IPV6_2292HOPLIMIT:
1400 OPTSET2292(IN6P_HOPLIMIT);
1402 case IPV6_2292HOPOPTS:
1404 * Check super-user privilege.
1405 * See comments for IPV6_RECVHOPOPTS.
1409 OPTSET2292(IN6P_HOPOPTS);
1411 case IPV6_2292DSTOPTS:
1414 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1416 case IPV6_2292RTHDR:
1417 OPTSET2292(IN6P_RTHDR);
1426 case IPV6_RTHDRDSTOPTS:
1430 * New advanced API (RFC3542)
1433 u_char optbuf_storage[MCLBYTES];
1435 struct ip6_pktopts **optp;
1437 /* cannot mix with RFC2292 */
1438 if (OPTBIT(IN6P_RFC2292)) {
1444 * We only ensure valsize is not too large
1445 * here. Further validation will be done
1448 error = soopt_to_kbuf(sopt, optbuf_storage,
1449 sizeof(optbuf_storage), 0);
1452 optlen = sopt->sopt_valsize;
1453 optbuf = optbuf_storage;
1454 optp = &in6p->in6p_outputopts;
1455 error = ip6_pcbopt(optname, optbuf, optlen,
1461 case IPV6_MULTICAST_IF:
1462 case IPV6_MULTICAST_HOPS:
1463 case IPV6_MULTICAST_LOOP:
1464 case IPV6_JOIN_GROUP:
1465 case IPV6_LEAVE_GROUP:
1469 if (sopt->sopt_valsize > MLEN) {
1474 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_HEADER);
1479 m->m_len = sopt->sopt_valsize;
1480 error = soopt_to_kbuf(sopt, mtod(m, char *),
1481 m->m_len, m->m_len);
1482 error = ip6_setmoptions(sopt->sopt_name,
1483 &in6p->in6p_moptions,
1489 case IPV6_PORTRANGE:
1490 error = soopt_to_kbuf(sopt, &optval,
1491 sizeof optval, sizeof optval);
1496 case IPV6_PORTRANGE_DEFAULT:
1497 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1498 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1501 case IPV6_PORTRANGE_HIGH:
1502 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1503 in6p->in6p_flags |= IN6P_HIGHPORT;
1506 case IPV6_PORTRANGE_LOW:
1507 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1508 in6p->in6p_flags |= IN6P_LOWPORT;
1523 struct mbuf **mp = &m;
1525 if (ip6_fw_ctl_ptr == NULL)
1528 if ((error = soopt_getm(sopt, &m)) != 0)
1531 soopt_to_mbuf(sopt, m);
1532 error = (*ip6_fw_ctl_ptr)(optname, mp);
1538 error = ENOPROTOOPT;
1545 case IPV6_2292PKTOPTIONS:
1546 #ifdef IPV6_PKTOPTIONS
1547 case IPV6_PKTOPTIONS:
1550 * RFC3542 (effectively) deprecated the
1551 * semantics of the 2292-style pktoptions.
1552 * Since it was not reliable in nature (i.e.,
1553 * applications had to expect the lack of some
1554 * information after all), it would make sense
1555 * to simplify this part by always returning
1558 if (in6p->in6p_options) {
1560 m = m_copym(in6p->in6p_options,
1561 0, M_COPYALL, M_WAITOK);
1562 error = soopt_from_mbuf(sopt, m);
1566 sopt->sopt_valsize = 0;
1569 case IPV6_RECVHOPOPTS:
1570 case IPV6_RECVDSTOPTS:
1571 case IPV6_RECVRTHDRDSTOPTS:
1572 case IPV6_UNICAST_HOPS:
1573 case IPV6_RECVPKTINFO:
1574 case IPV6_RECVHOPLIMIT:
1575 case IPV6_RECVRTHDR:
1576 case IPV6_RECVPATHMTU:
1577 case IPV6_RECVTCLASS:
1578 case IPV6_AUTOFLOWLABEL:
1580 case IPV6_PORTRANGE:
1583 case IPV6_RECVHOPOPTS:
1584 optval = OPTBIT(IN6P_HOPOPTS);
1587 case IPV6_RECVDSTOPTS:
1588 optval = OPTBIT(IN6P_DSTOPTS);
1591 case IPV6_RECVRTHDRDSTOPTS:
1592 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1595 case IPV6_RECVPKTINFO:
1596 optval = OPTBIT(IN6P_PKTINFO);
1599 case IPV6_RECVHOPLIMIT:
1600 optval = OPTBIT(IN6P_HOPLIMIT);
1603 case IPV6_RECVRTHDR:
1604 optval = OPTBIT(IN6P_RTHDR);
1607 case IPV6_RECVPATHMTU:
1608 optval = OPTBIT(IN6P_MTU);
1611 case IPV6_RECVTCLASS:
1612 optval = OPTBIT(IN6P_TCLASS);
1615 case IPV6_AUTOFLOWLABEL:
1616 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1620 case IPV6_UNICAST_HOPS:
1621 optval = in6p->in6p_hops;
1628 case IPV6_PORTRANGE:
1631 flags = in6p->in6p_flags;
1632 if (flags & IN6P_HIGHPORT)
1633 optval = IPV6_PORTRANGE_HIGH;
1634 else if (flags & IN6P_LOWPORT)
1635 optval = IPV6_PORTRANGE_LOW;
1641 soopt_from_kbuf(sopt, &optval,
1648 struct ip6_mtuinfo mtuinfo;
1649 struct route_in6 sro;
1651 bzero(&sro, sizeof(sro));
1653 if (!(so->so_state & SS_ISCONNECTED))
1656 * XXX: we dot not consider the case of source
1657 * routing, or optional information to specify
1658 * the outgoing interface.
1660 error = ip6_getpmtu(&sro, NULL, NULL,
1661 &in6p->in6p_faddr, &pmtu, NULL);
1666 if (pmtu > IPV6_MAXPACKET)
1667 pmtu = IPV6_MAXPACKET;
1669 bzero(&mtuinfo, sizeof(mtuinfo));
1670 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1671 optdata = (void *)&mtuinfo;
1672 optdatalen = sizeof(mtuinfo);
1673 soopt_from_kbuf(sopt, optdata,
1678 case IPV6_2292PKTINFO:
1679 case IPV6_2292HOPLIMIT:
1680 case IPV6_2292HOPOPTS:
1681 case IPV6_2292RTHDR:
1682 case IPV6_2292DSTOPTS:
1683 if (optname == IPV6_2292HOPOPTS ||
1684 optname == IPV6_2292DSTOPTS ||
1688 case IPV6_2292PKTINFO:
1689 optval = OPTBIT(IN6P_PKTINFO);
1691 case IPV6_2292HOPLIMIT:
1692 optval = OPTBIT(IN6P_HOPLIMIT);
1694 case IPV6_2292HOPOPTS:
1697 optval = OPTBIT(IN6P_HOPOPTS);
1699 case IPV6_2292RTHDR:
1700 optval = OPTBIT(IN6P_RTHDR);
1702 case IPV6_2292DSTOPTS:
1705 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1708 soopt_from_kbuf(sopt, &optval,
1716 case IPV6_RTHDRDSTOPTS:
1720 case IPV6_USE_MIN_MTU:
1721 case IPV6_PREFER_TEMPADDR:
1722 error = ip6_getpcbopt(in6p->in6p_outputopts,
1726 case IPV6_MULTICAST_IF:
1727 case IPV6_MULTICAST_HOPS:
1728 case IPV6_MULTICAST_LOOP:
1729 case IPV6_JOIN_GROUP:
1730 case IPV6_LEAVE_GROUP:
1733 error = ip6_getmoptions(sopt->sopt_name,
1734 in6p->in6p_moptions, &m);
1736 soopt_from_kbuf(sopt,
1737 mtod(m, char *), m->m_len);
1746 struct mbuf **mp = &m;
1748 if (ip6_fw_ctl_ptr == NULL)
1752 error = (*ip6_fw_ctl_ptr)(optname, mp);
1754 error = soopt_from_mbuf(sopt, m); /* XXX */
1755 if (error == 0 && m != NULL)
1761 error = ENOPROTOOPT;
1773 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1775 int error = 0, optval, optlen;
1776 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1777 struct in6pcb *in6p = sotoin6pcb(so);
1778 int level, op, optname;
1781 level = sopt->sopt_level;
1782 op = sopt->sopt_dir;
1783 optname = sopt->sopt_name;
1784 optlen = sopt->sopt_valsize;
1786 panic("ip6_raw_ctloutput: arg soopt is NULL");
1788 if (level != IPPROTO_IPV6) {
1795 * For ICMPv6 sockets, no modification allowed for checksum
1796 * offset, permit "no change" values to help existing apps.
1798 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
1799 * for an ICMPv6 socket will fail."
1800 * The current behavior does not meet RFC3542.
1804 if (optlen != sizeof(int)) {
1808 error = soopt_to_kbuf(sopt, &optval,
1809 sizeof optval, sizeof optval);
1812 if ((optval % 2) != 0) {
1813 /* the API assumes even offset values */
1815 } else if (so->so_proto->pr_protocol ==
1817 if (optval != icmp6off)
1820 in6p->in6p_cksum = optval;
1824 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
1827 optval = in6p->in6p_cksum;
1829 soopt_from_kbuf(sopt, &optval, sizeof(optval));
1839 error = ENOPROTOOPT;
1847 * Set up IP6 options in pcb for insertion in output packets or
1848 * specifying behavior of outgoing packets.
1851 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
1852 struct socket *so, struct sockopt *sopt)
1855 struct ip6_pktopts *opt = *pktopt;
1858 /* turn off any old options. */
1861 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1862 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1863 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1864 kprintf("ip6_pcbopts: all specified options are cleared.\n");
1866 ip6_clearpktopts(opt, -1);
1868 opt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1871 if (!m || m->m_len == 0) {
1873 * Only turning off any previous options, regardless of
1874 * whether the opt is just created or given.
1876 kfree(opt, M_IP6OPT);
1880 /* set options specified by user. */
1881 if ((error = ip6_setpktoptions(m, opt, NULL, so->so_proto->pr_protocol, priv)) != 0) {
1882 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
1883 kfree(opt, M_IP6OPT);
1892 * Below three functions are introduced by merge to RFC3542
1896 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
1898 void *optdata = NULL;
1900 struct ip6_ext *ip6e;
1902 struct in6_pktinfo null_pktinfo;
1903 int deftclass = 0, on;
1904 int defminmtu = IP6PO_MINMTU_MCASTONLY;
1905 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
1909 if (pktopt && pktopt->ip6po_pktinfo)
1910 optdata = (void *)pktopt->ip6po_pktinfo;
1912 /* XXX: we don't have to do this every time... */
1913 bzero(&null_pktinfo, sizeof(null_pktinfo));
1914 optdata = (void *)&null_pktinfo;
1916 optdatalen = sizeof(struct in6_pktinfo);
1919 if (pktopt && pktopt->ip6po_tclass >= 0)
1920 optdata = (void *)&pktopt->ip6po_tclass;
1922 optdata = (void *)&deftclass;
1923 optdatalen = sizeof(int);
1926 if (pktopt && pktopt->ip6po_hbh) {
1927 optdata = (void *)pktopt->ip6po_hbh;
1928 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
1929 optdatalen = (ip6e->ip6e_len + 1) << 3;
1933 if (pktopt && pktopt->ip6po_rthdr) {
1934 optdata = (void *)pktopt->ip6po_rthdr;
1935 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
1936 optdatalen = (ip6e->ip6e_len + 1) << 3;
1939 case IPV6_RTHDRDSTOPTS:
1940 if (pktopt && pktopt->ip6po_dest1) {
1941 optdata = (void *)pktopt->ip6po_dest1;
1942 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
1943 optdatalen = (ip6e->ip6e_len + 1) << 3;
1947 if (pktopt && pktopt->ip6po_dest2) {
1948 optdata = (void *)pktopt->ip6po_dest2;
1949 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
1950 optdatalen = (ip6e->ip6e_len + 1) << 3;
1954 if (pktopt && pktopt->ip6po_nexthop) {
1955 optdata = (void *)pktopt->ip6po_nexthop;
1956 optdatalen = pktopt->ip6po_nexthop->sa_len;
1959 case IPV6_USE_MIN_MTU:
1961 optdata = (void *)&pktopt->ip6po_minmtu;
1963 optdata = (void *)&defminmtu;
1964 optdatalen = sizeof(int);
1967 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
1971 optdata = (void *)&on;
1972 optdatalen = sizeof(on);
1974 case IPV6_PREFER_TEMPADDR:
1976 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
1978 optdata = (void *)&defpreftemp;
1979 optdatalen = sizeof(int);
1981 default: /* should not happen */
1983 panic("ip6_getpcbopt: unexpected option");
1985 return (ENOPROTOOPT);
1988 soopt_from_kbuf(sopt, optdata, optdatalen);
1994 * initialize ip6_pktopts. beware that there are non-zero default values in
1999 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, int uproto)
2001 struct ip6_pktopts *opt;
2003 if (*pktopt == NULL) {
2004 *pktopt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2005 init_ip6pktopts(*pktopt);
2009 return (ip6_setpktoption(optname, buf, len, opt, 1, 0, uproto, priv));
2013 * initialize ip6_pktopts. beware that there are non-zero default values in
2017 init_ip6pktopts(struct ip6_pktopts *opt)
2020 bzero(opt, sizeof(*opt));
2021 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2022 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2023 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2024 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2028 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2033 if (optname == -1 || optname == IPV6_PKTINFO) {
2034 if (pktopt->ip6po_pktinfo)
2035 kfree(pktopt->ip6po_pktinfo, M_IP6OPT);
2036 pktopt->ip6po_pktinfo = NULL;
2038 if (optname == -1 || optname == IPV6_HOPLIMIT)
2039 pktopt->ip6po_hlim = -1;
2040 if (optname == -1 || optname == IPV6_TCLASS)
2041 pktopt->ip6po_tclass = -1;
2042 if (optname == -1 || optname == IPV6_NEXTHOP) {
2043 if (pktopt->ip6po_nextroute.ro_rt) {
2044 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2045 pktopt->ip6po_nextroute.ro_rt = NULL;
2047 if (pktopt->ip6po_nexthop)
2048 kfree(pktopt->ip6po_nexthop, M_IP6OPT);
2049 pktopt->ip6po_nexthop = NULL;
2051 if (optname == -1 || optname == IPV6_HOPOPTS) {
2052 if (pktopt->ip6po_hbh)
2053 kfree(pktopt->ip6po_hbh, M_IP6OPT);
2054 pktopt->ip6po_hbh = NULL;
2056 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2057 if (pktopt->ip6po_dest1)
2058 kfree(pktopt->ip6po_dest1, M_IP6OPT);
2059 pktopt->ip6po_dest1 = NULL;
2061 if (optname == -1 || optname == IPV6_RTHDR) {
2062 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2063 kfree(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2064 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2065 if (pktopt->ip6po_route.ro_rt) {
2066 RTFREE(pktopt->ip6po_route.ro_rt);
2067 pktopt->ip6po_route.ro_rt = NULL;
2070 if (optname == -1 || optname == IPV6_DSTOPTS) {
2071 if (pktopt->ip6po_dest2)
2072 kfree(pktopt->ip6po_dest2, M_IP6OPT);
2073 pktopt->ip6po_dest2 = NULL;
2077 #define PKTOPT_EXTHDRCPY(type) \
2080 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2081 dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
2082 if (dst->type == NULL)\
2084 bcopy(src->type, dst->type, hlen);\
2088 struct ip6_pktopts *
2089 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2091 struct ip6_pktopts *dst;
2094 kprintf("ip6_clearpktopts: invalid argument\n");
2098 dst = kmalloc(sizeof(*dst), M_IP6OPT, canwait | M_ZERO);
2102 dst->ip6po_hlim = src->ip6po_hlim;
2103 if (src->ip6po_pktinfo) {
2104 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2106 if (dst->ip6po_pktinfo == NULL)
2108 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2110 if (src->ip6po_nexthop) {
2111 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2113 if (dst->ip6po_nexthop == NULL)
2115 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2116 src->ip6po_nexthop->sa_len);
2118 PKTOPT_EXTHDRCPY(ip6po_hbh);
2119 PKTOPT_EXTHDRCPY(ip6po_dest1);
2120 PKTOPT_EXTHDRCPY(ip6po_dest2);
2121 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2125 if (dst->ip6po_pktinfo) kfree(dst->ip6po_pktinfo, M_IP6OPT);
2126 if (dst->ip6po_nexthop) kfree(dst->ip6po_nexthop, M_IP6OPT);
2127 if (dst->ip6po_hbh) kfree(dst->ip6po_hbh, M_IP6OPT);
2128 if (dst->ip6po_dest1) kfree(dst->ip6po_dest1, M_IP6OPT);
2129 if (dst->ip6po_dest2) kfree(dst->ip6po_dest2, M_IP6OPT);
2130 if (dst->ip6po_rthdr) kfree(dst->ip6po_rthdr, M_IP6OPT);
2131 kfree(dst, M_IP6OPT);
2136 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2138 if (dst == NULL || src == NULL) {
2140 kprintf("ip6_clearpktopts: invalid argument\n");
2145 dst->ip6po_hlim = src->ip6po_hlim;
2146 dst->ip6po_tclass = src->ip6po_tclass;
2147 dst->ip6po_flags = src->ip6po_flags;
2148 if (src->ip6po_pktinfo) {
2149 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2151 if (dst->ip6po_pktinfo == NULL)
2153 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2155 if (src->ip6po_nexthop) {
2156 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2158 if (dst->ip6po_nexthop == NULL)
2160 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2161 src->ip6po_nexthop->sa_len);
2163 PKTOPT_EXTHDRCPY(ip6po_hbh);
2164 PKTOPT_EXTHDRCPY(ip6po_dest1);
2165 PKTOPT_EXTHDRCPY(ip6po_dest2);
2166 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2170 ip6_clearpktopts(dst, -1);
2173 #undef PKTOPT_EXTHDRCPY
2176 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2181 ip6_clearpktopts(pktopt, -1);
2183 kfree(pktopt, M_IP6OPT);
2187 * Set the IP6 multicast options in response to user setsockopt().
2190 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2193 u_int loop, ifindex;
2194 struct ipv6_mreq *mreq;
2196 struct ip6_moptions *im6o = *im6op;
2197 struct route_in6 ro;
2198 struct sockaddr_in6 *dst;
2199 struct in6_multi_mship *imm;
2200 struct thread *td = curthread;
2204 * No multicast option buffer attached to the pcb;
2205 * allocate one and initialize to default values.
2207 im6o = (struct ip6_moptions *)
2208 kmalloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2211 im6o->im6o_multicast_ifp = NULL;
2212 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2213 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2214 LIST_INIT(&im6o->im6o_memberships);
2219 case IPV6_MULTICAST_IF:
2221 * Select the interface for outgoing multicast packets.
2223 if (m == NULL || m->m_len != sizeof(u_int)) {
2227 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2228 if (ifindex < 0 || if_index < ifindex) {
2229 error = ENXIO; /* XXX EINVAL? */
2232 ifp = ifindex2ifnet[ifindex];
2233 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2234 error = EADDRNOTAVAIL;
2237 im6o->im6o_multicast_ifp = ifp;
2240 case IPV6_MULTICAST_HOPS:
2243 * Set the IP6 hoplimit for outgoing multicast packets.
2246 if (m == NULL || m->m_len != sizeof(int)) {
2250 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2251 if (optval < -1 || optval >= 256)
2253 else if (optval == -1)
2254 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2256 im6o->im6o_multicast_hlim = optval;
2260 case IPV6_MULTICAST_LOOP:
2262 * Set the loopback flag for outgoing multicast packets.
2263 * Must be zero or one.
2265 if (m == NULL || m->m_len != sizeof(u_int)) {
2269 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2274 im6o->im6o_multicast_loop = loop;
2277 case IPV6_JOIN_GROUP:
2279 * Add a multicast group membership.
2280 * Group must be a valid IP6 multicast address.
2282 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2286 mreq = mtod(m, struct ipv6_mreq *);
2287 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2289 * We use the unspecified address to specify to accept
2290 * all multicast addresses. Only super user is allowed
2293 if (priv_check(td, PRIV_ROOT)) {
2297 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2303 * If the interface is specified, validate it.
2305 if (mreq->ipv6mr_interface < 0
2306 || if_index < mreq->ipv6mr_interface) {
2307 error = ENXIO; /* XXX EINVAL? */
2311 * If no interface was explicitly specified, choose an
2312 * appropriate one according to the given multicast address.
2314 if (mreq->ipv6mr_interface == 0) {
2316 * If the multicast address is in node-local scope,
2317 * the interface should be a loopback interface.
2318 * Otherwise, look up the routing table for the
2319 * address, and choose the outgoing interface.
2320 * XXX: is it a good approach?
2322 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) {
2326 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2327 bzero(dst, sizeof(*dst));
2328 dst->sin6_len = sizeof(struct sockaddr_in6);
2329 dst->sin6_family = AF_INET6;
2330 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2331 rtalloc((struct route *)&ro);
2332 if (ro.ro_rt == NULL) {
2333 error = EADDRNOTAVAIL;
2336 ifp = ro.ro_rt->rt_ifp;
2340 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2343 * See if we found an interface, and confirm that it
2344 * supports multicast
2346 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2347 error = EADDRNOTAVAIL;
2351 * Put interface index into the multicast address,
2352 * if the address has link-local scope.
2354 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2355 mreq->ipv6mr_multiaddr.s6_addr16[1]
2356 = htons(mreq->ipv6mr_interface);
2359 * See if the membership already exists.
2361 for (imm = im6o->im6o_memberships.lh_first;
2362 imm != NULL; imm = imm->i6mm_chain.le_next)
2363 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2364 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2365 &mreq->ipv6mr_multiaddr))
2372 * Everything looks good; add a new record to the multicast
2373 * address list for the given interface.
2375 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2376 if ((imm->i6mm_maddr =
2377 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2378 kfree(imm, M_IPMADDR);
2381 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2384 case IPV6_LEAVE_GROUP:
2386 * Drop a multicast group membership.
2387 * Group must be a valid IP6 multicast address.
2389 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2393 mreq = mtod(m, struct ipv6_mreq *);
2394 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2395 if (priv_check(td, PRIV_ROOT)) {
2399 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2404 * If an interface address was specified, get a pointer
2405 * to its ifnet structure.
2407 if (mreq->ipv6mr_interface < 0
2408 || if_index < mreq->ipv6mr_interface) {
2409 error = ENXIO; /* XXX EINVAL? */
2412 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2414 * Put interface index into the multicast address,
2415 * if the address has link-local scope.
2417 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2418 mreq->ipv6mr_multiaddr.s6_addr16[1]
2419 = htons(mreq->ipv6mr_interface);
2423 * Find the membership in the membership list.
2425 for (imm = im6o->im6o_memberships.lh_first;
2426 imm != NULL; imm = imm->i6mm_chain.le_next) {
2427 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2428 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2429 &mreq->ipv6mr_multiaddr))
2433 /* Unable to resolve interface */
2434 error = EADDRNOTAVAIL;
2438 * Give up the multicast address record to which the
2439 * membership points.
2441 LIST_REMOVE(imm, i6mm_chain);
2442 in6_delmulti(imm->i6mm_maddr);
2443 kfree(imm, M_IPMADDR);
2452 * If all options have default values, no need to keep the mbuf.
2454 if (im6o->im6o_multicast_ifp == NULL &&
2455 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2456 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2457 im6o->im6o_memberships.lh_first == NULL) {
2458 kfree(*im6op, M_IPMOPTS);
2466 * Return the IP6 multicast options in response to user getsockopt().
2469 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2471 u_int *hlim, *loop, *ifindex;
2473 *mp = m_get(M_WAITOK, MT_HEADER); /* XXX */
2477 case IPV6_MULTICAST_IF:
2478 ifindex = mtod(*mp, u_int *);
2479 (*mp)->m_len = sizeof(u_int);
2480 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2483 *ifindex = im6o->im6o_multicast_ifp->if_index;
2486 case IPV6_MULTICAST_HOPS:
2487 hlim = mtod(*mp, u_int *);
2488 (*mp)->m_len = sizeof(u_int);
2490 *hlim = ip6_defmcasthlim;
2492 *hlim = im6o->im6o_multicast_hlim;
2495 case IPV6_MULTICAST_LOOP:
2496 loop = mtod(*mp, u_int *);
2497 (*mp)->m_len = sizeof(u_int);
2499 *loop = ip6_defmcasthlim;
2501 *loop = im6o->im6o_multicast_loop;
2505 return (EOPNOTSUPP);
2510 * Discard the IP6 multicast options.
2513 ip6_freemoptions(struct ip6_moptions *im6o)
2515 struct in6_multi_mship *imm;
2520 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2521 LIST_REMOVE(imm, i6mm_chain);
2522 if (imm->i6mm_maddr)
2523 in6_delmulti(imm->i6mm_maddr);
2524 kfree(imm, M_IPMADDR);
2526 kfree(im6o, M_IPMOPTS);
2530 * Set a particular packet option, as a sticky option or an ancillary data
2531 * item. "len" can be 0 only when it's a sticky option.
2532 * We have 4 cases of combination of "sticky" and "cmsg":
2533 * "sticky=0, cmsg=0": impossible
2534 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2535 * "sticky=1, cmsg=0": RFC3542 socket option
2536 * "sticky=1, cmsg=1": RFC2292 socket option
2539 ip6_setpktoption(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2540 int sticky, int cmsg, int uproto, int priv)
2542 int minmtupolicy, preftemp;
2545 if (!sticky && !cmsg) {
2546 kprintf("ip6_setpktoption: impossible case\n");
2551 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2552 * not be specified in the context of RFC3542. Conversely,
2553 * RFC3542 types should not be specified in the context of RFC2292.
2557 case IPV6_2292PKTINFO:
2558 case IPV6_2292HOPLIMIT:
2559 case IPV6_2292NEXTHOP:
2560 case IPV6_2292HOPOPTS:
2561 case IPV6_2292DSTOPTS:
2562 case IPV6_2292RTHDR:
2563 case IPV6_2292PKTOPTIONS:
2564 return (ENOPROTOOPT);
2567 if (sticky && cmsg) {
2574 case IPV6_RTHDRDSTOPTS:
2576 case IPV6_USE_MIN_MTU:
2579 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2580 return (ENOPROTOOPT);
2585 case IPV6_2292PKTINFO:
2588 struct in6_pktinfo *pktinfo;
2589 if (len != sizeof(struct in6_pktinfo))
2591 pktinfo = (struct in6_pktinfo *)buf;
2594 * An application can clear any sticky IPV6_PKTINFO option by
2595 * doing a "regular" setsockopt with ipi6_addr being
2596 * in6addr_any and ipi6_ifindex being zero.
2597 * [RFC 3542, Section 6]
2599 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2600 pktinfo->ipi6_ifindex == 0 &&
2601 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2602 ip6_clearpktopts(opt, optname);
2606 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2607 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2611 /* validate the interface index if specified. */
2612 if (pktinfo->ipi6_ifindex > if_index ||
2613 pktinfo->ipi6_ifindex < 0) {
2617 * Check if the requested source address is indeed a
2618 * unicast address assigned to the node, and can be
2619 * used as the packet's source address.
2621 if (opt->ip6po_pktinfo != NULL &&
2622 !IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2623 struct in6_ifaddr *ia6;
2624 struct sockaddr_in6 sin6;
2626 bzero(&sin6, sizeof(sin6));
2627 sin6.sin6_len = sizeof(sin6);
2628 sin6.sin6_family = AF_INET6;
2630 opt->ip6po_pktinfo->ipi6_addr;
2631 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2633 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2634 IN6_IFF_NOTREADY)) != 0)
2635 return (EADDRNOTAVAIL);
2639 * We store the address anyway, and let in6_selectsrc()
2640 * validate the specified address. This is because ipi6_addr
2641 * may not have enough information about its scope zone, and
2642 * we may need additional information (such as outgoing
2643 * interface or the scope zone of a destination address) to
2644 * disambiguate the scope.
2645 * XXX: the delay of the validation may confuse the
2646 * application when it is used as a sticky option.
2648 if (opt->ip6po_pktinfo == NULL) {
2649 opt->ip6po_pktinfo = kmalloc(sizeof(*pktinfo),
2650 M_IP6OPT, M_NOWAIT);
2651 if (opt->ip6po_pktinfo == NULL)
2654 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2658 case IPV6_2292HOPLIMIT:
2664 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2665 * to simplify the ordering among hoplimit options.
2667 if (optname == IPV6_HOPLIMIT && sticky)
2668 return (ENOPROTOOPT);
2670 if (len != sizeof(int))
2673 if (*hlimp < -1 || *hlimp > 255)
2676 opt->ip6po_hlim = *hlimp;
2684 if (len != sizeof(int))
2686 tclass = *(int *)buf;
2687 if (tclass < -1 || tclass > 255)
2690 opt->ip6po_tclass = tclass;
2694 case IPV6_2292NEXTHOP:
2699 if (len == 0) { /* just remove the option */
2700 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2704 /* check if cmsg_len is large enough for sa_len */
2705 if (len < sizeof(struct sockaddr) || len < *buf)
2708 switch (((struct sockaddr *)buf)->sa_family) {
2711 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2714 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2717 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2718 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2723 case AF_LINK: /* should eventually be supported */
2725 return (EAFNOSUPPORT);
2728 /* turn off the previous option, then set the new option. */
2729 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2730 opt->ip6po_nexthop = kmalloc(*buf, M_IP6OPT, M_NOWAIT);
2731 if (opt->ip6po_nexthop == NULL)
2733 bcopy(buf, opt->ip6po_nexthop, *buf);
2736 case IPV6_2292HOPOPTS:
2739 struct ip6_hbh *hbh;
2743 * XXX: We don't allow a non-privileged user to set ANY HbH
2744 * options, since per-option restriction has too much
2750 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2751 break; /* just remove the option */
2754 /* message length validation */
2755 if (len < sizeof(struct ip6_hbh))
2757 hbh = (struct ip6_hbh *)buf;
2758 hbhlen = (hbh->ip6h_len + 1) << 3;
2762 /* turn off the previous option, then set the new option. */
2763 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2764 opt->ip6po_hbh = kmalloc(hbhlen, M_IP6OPT, M_NOWAIT);
2765 if (opt->ip6po_hbh == NULL)
2767 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2772 case IPV6_2292DSTOPTS:
2774 case IPV6_RTHDRDSTOPTS:
2776 struct ip6_dest *dest, **newdest = NULL;
2782 ip6_clearpktopts(opt, optname);
2783 break; /* just remove the option */
2786 /* message length validation */
2787 if (len < sizeof(struct ip6_dest))
2789 dest = (struct ip6_dest *)buf;
2790 destlen = (dest->ip6d_len + 1) << 3;
2795 * Determine the position that the destination options header
2796 * should be inserted; before or after the routing header.
2799 case IPV6_2292DSTOPTS:
2801 * The old advacned API is ambiguous on this point.
2802 * Our approach is to determine the position based
2803 * according to the existence of a routing header.
2804 * Note, however, that this depends on the order of the
2805 * extension headers in the ancillary data; the 1st
2806 * part of the destination options header must appear
2807 * before the routing header in the ancillary data,
2809 * RFC3542 solved the ambiguity by introducing
2810 * separate ancillary data or option types.
2812 if (opt->ip6po_rthdr == NULL)
2813 newdest = &opt->ip6po_dest1;
2815 newdest = &opt->ip6po_dest2;
2817 case IPV6_RTHDRDSTOPTS:
2818 newdest = &opt->ip6po_dest1;
2821 newdest = &opt->ip6po_dest2;
2825 /* turn off the previous option, then set the new option. */
2826 ip6_clearpktopts(opt, optname);
2827 *newdest = kmalloc(destlen, M_IP6OPT, M_NOWAIT);
2828 if (*newdest == NULL)
2830 bcopy(dest, *newdest, destlen);
2835 case IPV6_2292RTHDR:
2838 struct ip6_rthdr *rth;
2842 ip6_clearpktopts(opt, IPV6_RTHDR);
2843 break; /* just remove the option */
2846 /* message length validation */
2847 if (len < sizeof(struct ip6_rthdr))
2849 rth = (struct ip6_rthdr *)buf;
2850 rthlen = (rth->ip6r_len + 1) << 3;
2854 switch (rth->ip6r_type) {
2856 return (EINVAL); /* not supported */
2859 /* turn off the previous option */
2860 ip6_clearpktopts(opt, IPV6_RTHDR);
2861 opt->ip6po_rthdr = kmalloc(rthlen, M_IP6OPT, M_NOWAIT);
2862 if (opt->ip6po_rthdr == NULL)
2864 bcopy(rth, opt->ip6po_rthdr, rthlen);
2869 case IPV6_USE_MIN_MTU:
2870 if (len != sizeof(int))
2872 minmtupolicy = *(int *)buf;
2873 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2874 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2875 minmtupolicy != IP6PO_MINMTU_ALL) {
2878 opt->ip6po_minmtu = minmtupolicy;
2882 if (len != sizeof(int))
2885 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2887 * we ignore this option for TCP sockets.
2888 * (RFC3542 leaves this case unspecified.)
2890 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2892 opt->ip6po_flags |= IP6PO_DONTFRAG;
2895 case IPV6_PREFER_TEMPADDR:
2896 if (len != sizeof(int))
2898 preftemp = *(int *)buf;
2899 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2900 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2901 preftemp != IP6PO_TEMPADDR_PREFER) {
2904 opt->ip6po_prefer_tempaddr = preftemp;
2908 return (ENOPROTOOPT);
2909 } /* end of switch */
2916 * Set IPv6 outgoing packet options based on advanced API.
2919 ip6_setpktoptions(struct mbuf *control, struct ip6_pktopts *opt,
2920 struct ip6_pktopts *stickyopt, int uproto, int priv)
2922 struct cmsghdr *cm = NULL;
2924 if (control == NULL || opt == NULL)
2927 init_ip6pktopts(opt);
2930 * XXX: Currently, we assume all the optional information is stored
2937 * If stickyopt is provided, make a local copy of the options
2938 * for this particular packet, then override them by ancillary
2940 * XXX: copypktopts() does not copy the cached route to a next
2941 * hop (if any). This is not very good in terms of efficiency,
2942 * but we can allow this since this option should be rarely
2945 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2950 * XXX: Currently, we assume all the optional information is stored
2953 if (control->m_next)
2956 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2957 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2960 if (control->m_len < CMSG_LEN(0))
2963 cm = mtod(control, struct cmsghdr *);
2964 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2966 if (cm->cmsg_level != IPPROTO_IPV6)
2969 error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm),
2970 cm->cmsg_len - CMSG_LEN(0), opt, 0, 1, uproto, priv);
2979 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2980 * packet to the input queue of a specified interface. Note that this
2981 * calls the output routine of the loopback "driver", but with an interface
2982 * pointer that might NOT be loif -- easier than replicating that code here.
2985 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
2988 struct ip6_hdr *ip6;
2990 copym = m_copy(m, 0, M_COPYALL);
2995 * Make sure to deep-copy IPv6 header portion in case the data
2996 * is in an mbuf cluster, so that we can safely override the IPv6
2997 * header portion later.
2999 if ((copym->m_flags & M_EXT) != 0 ||
3000 copym->m_len < sizeof(struct ip6_hdr)) {
3001 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3007 if (copym->m_len < sizeof(*ip6)) {
3013 ip6 = mtod(copym, struct ip6_hdr *);
3015 * clear embedded scope identifiers if necessary.
3016 * in6_clearscope will touch the addresses only when necessary.
3018 in6_clearscope(&ip6->ip6_src);
3019 in6_clearscope(&ip6->ip6_dst);
3021 if_simloop(ifp, copym, dst->sin6_family, 0);
3025 * Separate the IPv6 header from the payload into its own mbuf.
3027 * Returns the new mbuf chain or the original mbuf if no payload.
3028 * Returns NULL if can't allocate new mbuf for header.
3030 static struct mbuf *
3031 ip6_splithdr(struct mbuf *m)
3035 if (m->m_len <= sizeof(struct ip6_hdr)) /* no payload */
3038 MGETHDR(mh, M_NOWAIT, MT_HEADER);
3041 mh->m_len = sizeof(struct ip6_hdr);
3042 M_MOVE_PKTHDR(mh, m);
3043 MH_ALIGN(mh, sizeof(struct ip6_hdr));
3044 bcopy(mtod(m, caddr_t), mtod(mh, caddr_t), sizeof(struct ip6_hdr));
3045 m->m_data += sizeof(struct ip6_hdr);
3046 m->m_len -= sizeof(struct ip6_hdr);
3052 * Compute IPv6 extension header length.
3055 ip6_optlen(struct in6pcb *in6p)
3059 if (!in6p->in6p_outputopts)
3064 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3066 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3067 if (in6p->in6p_outputopts->ip6po_rthdr)
3068 /* dest1 is valid with rthdr only */
3069 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3070 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3071 len += elen(in6p->in6p_outputopts->ip6po_dest2);