2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/malloc.h>
74 #include <sys/errno.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/kernel.h>
82 #include <net/netisr.h>
83 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet6/nd6.h>
97 #include <netipsec/ipsec.h>
98 #include <netipsec/ipsec6.h>
99 #include <netipsec/key.h>
100 #include <netinet6/ip6_ipsec.h>
103 #include <netinet6/ip6protosw.h>
104 #include <netinet6/scope6_var.h>
106 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "internet multicast options");
109 struct mbuf *ip6e_ip6;
110 struct mbuf *ip6e_hbh;
111 struct mbuf *ip6e_dest1;
112 struct mbuf *ip6e_rthdr;
113 struct mbuf *ip6e_dest2;
116 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
118 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
119 struct socket *, struct sockopt *));
120 static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *));
121 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
124 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
125 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
126 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
127 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
128 struct ip6_frag **));
129 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
130 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
131 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
132 struct ifnet *, struct in6_addr *, u_long *, int *));
133 static int copypktopts __P((struct ip6_pktopts *, struct ip6_pktopts *, int));
137 * Make an extension header from option data. hp is the source, and
138 * mp is the destination.
140 #define MAKE_EXTHDR(hp, mp) \
143 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
144 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
145 ((eh)->ip6e_len + 1) << 3); \
149 } while (/*CONSTCOND*/ 0)
152 * Form a chain of extension headers.
153 * m is the extension header mbuf
154 * mp is the previous mbuf in the chain
155 * p is the next header
156 * i is the type of option.
158 #define MAKE_CHAIN(m, mp, p, i)\
162 panic("assumption failed: hdr not split"); \
163 *mtod((m), u_char *) = *(p);\
165 p = mtod((m), u_char *);\
166 (m)->m_next = (mp)->m_next;\
170 } while (/*CONSTCOND*/ 0)
173 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
174 * header (with pri, len, nxt, hlim, src, dst).
175 * This function may modify ver and hlim only.
176 * The mbuf chain containing the packet will be freed.
177 * The mbuf opt, if present, will not be freed.
179 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
180 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
181 * which is rt_rmx.rmx_mtu.
183 * ifpp - XXX: just for statistics
186 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
187 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
188 struct ifnet **ifpp, struct inpcb *inp)
190 struct ip6_hdr *ip6, *mhip6;
191 struct ifnet *ifp, *origifp;
193 struct mbuf *mprev = NULL;
194 int hlen, tlen, len, off;
195 struct route_in6 ip6route;
196 struct rtentry *rt = NULL;
197 struct sockaddr_in6 *dst, src_sa, dst_sa;
198 struct in6_addr odst;
200 struct in6_ifaddr *ia = NULL;
202 int alwaysfrag, dontfrag;
203 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
204 struct ip6_exthdrs exthdrs;
205 struct in6_addr finaldst, src0, dst0;
207 struct route_in6 *ro_pmtu = NULL;
211 struct ipsec_output_state state;
212 struct ip6_rthdr *rh = NULL;
213 int needipsectun = 0;
215 struct secpolicy *sp = NULL;
218 ip6 = mtod(m, struct ip6_hdr *);
220 printf ("ip6 is NULL");
224 finaldst = ip6->ip6_dst;
226 bzero(&exthdrs, sizeof(exthdrs));
229 /* Hop-by-Hop options header */
230 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
231 /* Destination options header(1st part) */
232 if (opt->ip6po_rthdr) {
234 * Destination options header(1st part)
235 * This only makes sense with a routing header.
236 * See Section 9.2 of RFC 3542.
237 * Disabling this part just for MIP6 convenience is
238 * a bad idea. We need to think carefully about a
239 * way to make the advanced API coexist with MIP6
240 * options, which might automatically be inserted in
243 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
246 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
247 /* Destination options header(2nd part) */
248 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
252 * IPSec checking which handles several cases.
253 * FAST IPSEC: We re-injected the packet.
256 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
258 case 1: /* Bad packet */
260 case -1: /* Do IPSec */
262 case 0: /* No IPSec */
269 * Calculate the total length of the extension header chain.
270 * Keep the length of the unfragmentable part for fragmentation.
273 if (exthdrs.ip6e_hbh)
274 optlen += exthdrs.ip6e_hbh->m_len;
275 if (exthdrs.ip6e_dest1)
276 optlen += exthdrs.ip6e_dest1->m_len;
277 if (exthdrs.ip6e_rthdr)
278 optlen += exthdrs.ip6e_rthdr->m_len;
279 unfragpartlen = optlen + sizeof(struct ip6_hdr);
281 /* NOTE: we don't add AH/ESP length here. do that later. */
282 if (exthdrs.ip6e_dest2)
283 optlen += exthdrs.ip6e_dest2->m_len;
286 * If we need IPsec, or there is at least one extension header,
287 * separate IP6 header from the payload.
289 if ((needipsec || optlen) && !hdrsplit) {
290 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
294 m = exthdrs.ip6e_ip6;
299 ip6 = mtod(m, struct ip6_hdr *);
301 /* adjust mbuf packet header length */
302 m->m_pkthdr.len += optlen;
303 plen = m->m_pkthdr.len - sizeof(*ip6);
305 /* If this is a jumbo payload, insert a jumbo payload option. */
306 if (plen > IPV6_MAXPACKET) {
308 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
312 m = exthdrs.ip6e_ip6;
316 ip6 = mtod(m, struct ip6_hdr *);
317 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
321 ip6->ip6_plen = htons(plen);
324 * Concatenate headers and fill in next header fields.
325 * Here we have, on "m"
327 * and we insert headers accordingly. Finally, we should be getting:
328 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
330 * during the header composing process, "m" points to IPv6 header.
331 * "mprev" points to an extension header prior to esp.
333 u_char *nexthdrp = &ip6->ip6_nxt;
337 * we treat dest2 specially. this makes IPsec processing
338 * much easier. the goal here is to make mprev point the
339 * mbuf prior to dest2.
341 * result: IPv6 dest2 payload
342 * m and mprev will point to IPv6 header.
344 if (exthdrs.ip6e_dest2) {
346 panic("assumption failed: hdr not split");
347 exthdrs.ip6e_dest2->m_next = m->m_next;
348 m->m_next = exthdrs.ip6e_dest2;
349 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
350 ip6->ip6_nxt = IPPROTO_DSTOPTS;
354 * result: IPv6 hbh dest1 rthdr dest2 payload
355 * m will point to IPv6 header. mprev will point to the
356 * extension header prior to dest2 (rthdr in the above case).
358 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
359 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
361 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
369 * pointers after IPsec headers are not valid any more.
370 * other pointers need a great care too.
371 * (IPsec routines should not mangle mbufs prior to AH/ESP)
373 exthdrs.ip6e_dest2 = NULL;
375 if (exthdrs.ip6e_rthdr) {
376 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
377 segleft_org = rh->ip6r_segleft;
378 rh->ip6r_segleft = 0;
381 bzero(&state, sizeof(state));
383 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
387 /* mbuf is already reclaimed in ipsec6_output_trans. */
397 printf("ip6_output (ipsec): error code %d\n", error);
400 /* don't show these error codes to the user */
405 } else if (!needipsectun) {
407 * In the FAST IPSec case we have already
408 * re-injected the packet and it has been freed
409 * by the ipsec_done() function. So, just clean
410 * up after ourselves.
415 if (exthdrs.ip6e_rthdr) {
416 /* ah6_output doesn't modify mbuf chain */
417 rh->ip6r_segleft = segleft_org;
423 * If there is a routing header, replace the destination address field
424 * with the first hop of the routing header.
426 if (exthdrs.ip6e_rthdr) {
427 struct ip6_rthdr *rh =
428 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
429 struct ip6_rthdr *));
430 struct ip6_rthdr0 *rh0;
431 struct in6_addr *addr;
432 struct sockaddr_in6 sa;
434 switch (rh->ip6r_type) {
435 case IPV6_RTHDR_TYPE_0:
436 rh0 = (struct ip6_rthdr0 *)rh;
437 addr = (struct in6_addr *)(rh0 + 1);
440 * construct a sockaddr_in6 form of
443 * XXX: we may not have enough
444 * information about its scope zone;
445 * there is no standard API to pass
446 * the information from the
449 bzero(&sa, sizeof(sa));
450 sa.sin6_family = AF_INET6;
451 sa.sin6_len = sizeof(sa);
452 sa.sin6_addr = addr[0];
453 if ((error = sa6_embedscope(&sa,
454 ip6_use_defzone)) != 0) {
457 ip6->ip6_dst = sa.sin6_addr;
458 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr)
459 * (rh0->ip6r0_segleft - 1));
460 addr[rh0->ip6r0_segleft - 1] = finaldst;
462 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
464 default: /* is it possible? */
470 /* Source address validation */
471 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
472 (flags & IPV6_UNSPECSRC) == 0) {
474 ip6stat.ip6s_badscope++;
477 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
479 ip6stat.ip6s_badscope++;
483 ip6stat.ip6s_localout++;
490 bzero((caddr_t)ro, sizeof(*ro));
493 if (opt && opt->ip6po_rthdr)
494 ro = &opt->ip6po_route;
495 dst = (struct sockaddr_in6 *)&ro->ro_dst;
499 * if specified, try to fill in the traffic class field.
500 * do not override if a non-zero value is already set.
501 * we check the diffserv field and the ecn field separately.
503 if (opt && opt->ip6po_tclass >= 0) {
506 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
508 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
511 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
514 /* fill in or override the hop limit field, if necessary. */
515 if (opt && opt->ip6po_hlim != -1)
516 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
517 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
519 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
521 ip6->ip6_hlim = ip6_defmcasthlim;
526 * We may re-inject packets into the stack here.
528 if (needipsec && needipsectun) {
529 struct ipsec_output_state state;
532 * All the extension headers will become inaccessible
533 * (since they can be encrypted).
534 * Don't panic, we need no more updates to extension headers
535 * on inner IPv6 packet (since they are now encapsulated).
537 * IPv6 [ESP|AH] IPv6 [extension headers] payload
539 bzero(&exthdrs, sizeof(exthdrs));
540 exthdrs.ip6e_ip6 = m;
542 bzero(&state, sizeof(state));
544 state.ro = (struct route *)ro;
545 state.dst = (struct sockaddr *)dst;
547 error = ipsec6_output_tunnel(&state, sp, flags);
550 ro = (struct route_in6 *)state.ro;
551 dst = (struct sockaddr_in6 *)state.dst;
553 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
564 printf("ip6_output (ipsec): error code %d\n", error);
567 /* don't show these error codes to the user */
574 * In the FAST IPSec case we have already
575 * re-injected the packet and it has been freed
576 * by the ipsec_done() function. So, just clean
577 * up after ourselves.
583 exthdrs.ip6e_ip6 = m;
588 ip6 = mtod(m, struct ip6_hdr *);
590 bzero(&dst_sa, sizeof(dst_sa));
591 dst_sa.sin6_family = AF_INET6;
592 dst_sa.sin6_len = sizeof(dst_sa);
593 dst_sa.sin6_addr = ip6->ip6_dst;
594 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
595 &ifp, &rt, 0)) != 0) {
598 ip6stat.ip6s_noroute++;
602 break; /* XXX statistics? */
605 in6_ifstat_inc(ifp, ifs6_out_discard);
610 * If in6_selectroute() does not return a route entry,
611 * dst may not have been updated.
613 *dst = dst_sa; /* XXX */
617 * then rt (for unicast) and ifp must be non-NULL valid values.
619 if ((flags & IPV6_FORWARDING) == 0) {
620 /* XXX: the FORWARDING flag can be set for mrouting. */
621 in6_ifstat_inc(ifp, ifs6_out_request);
624 ia = (struct in6_ifaddr *)(rt->rt_ifa);
629 * The outgoing interface must be in the zone of source and
630 * destination addresses. We should use ia_ifp to support the
631 * case of sending packets to an address of our own.
633 if (ia != NULL && ia->ia_ifp)
634 origifp = ia->ia_ifp;
639 if (in6_setscope(&src0, origifp, &zone))
641 bzero(&src_sa, sizeof(src_sa));
642 src_sa.sin6_family = AF_INET6;
643 src_sa.sin6_len = sizeof(src_sa);
644 src_sa.sin6_addr = ip6->ip6_src;
645 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
649 if (in6_setscope(&dst0, origifp, &zone))
651 /* re-initialize to be sure */
652 bzero(&dst_sa, sizeof(dst_sa));
653 dst_sa.sin6_family = AF_INET6;
654 dst_sa.sin6_len = sizeof(dst_sa);
655 dst_sa.sin6_addr = ip6->ip6_dst;
656 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
660 /* scope check is done. */
664 ip6stat.ip6s_badscope++;
665 in6_ifstat_inc(origifp, ifs6_out_discard);
667 error = EHOSTUNREACH; /* XXX */
671 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
672 if (opt && opt->ip6po_nextroute.ro_rt) {
674 * The nexthop is explicitly specified by the
675 * application. We assume the next hop is an IPv6
678 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
680 else if ((rt->rt_flags & RTF_GATEWAY))
681 dst = (struct sockaddr_in6 *)rt->rt_gateway;
684 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
685 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
687 struct in6_multi *in6m;
689 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
691 in6_ifstat_inc(ifp, ifs6_out_mcast);
694 * Confirm that the outgoing interface supports multicast.
696 if (!(ifp->if_flags & IFF_MULTICAST)) {
697 ip6stat.ip6s_noroute++;
698 in6_ifstat_inc(ifp, ifs6_out_discard);
702 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
704 (im6o == NULL || im6o->im6o_multicast_loop)) {
706 * If we belong to the destination multicast group
707 * on the outgoing interface, and the caller did not
708 * forbid loopback, loop back a copy.
710 ip6_mloopback(ifp, m, dst);
713 * If we are acting as a multicast router, perform
714 * multicast forwarding as if the packet had just
715 * arrived on the interface to which we are about
716 * to send. The multicast forwarding function
717 * recursively calls this function, using the
718 * IPV6_FORWARDING flag to prevent infinite recursion.
720 * Multicasts that are looped back by ip6_mloopback(),
721 * above, will be forwarded by the ip6_input() routine,
724 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
726 * XXX: ip6_mforward expects that rcvif is NULL
727 * when it is called from the originating path.
728 * However, it is not always the case, since
729 * some versions of MGETHDR() does not
730 * initialize the field.
732 m->m_pkthdr.rcvif = NULL;
733 if (ip6_mforward(ip6, ifp, m) != 0) {
740 * Multicasts with a hoplimit of zero may be looped back,
741 * above, but must not be transmitted on a network.
742 * Also, multicasts addressed to the loopback interface
743 * are not sent -- the above call to ip6_mloopback() will
744 * loop back a copy if this host actually belongs to the
745 * destination group on the loopback interface.
747 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
748 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
755 * Fill the outgoing inteface to tell the upper layer
756 * to increment per-interface statistics.
761 /* Determine path MTU. */
762 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
767 * The caller of this function may specify to use the minimum MTU
769 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
770 * setting. The logic is a bit complicated; by default, unicast
771 * packets will follow path MTU while multicast packets will be sent at
772 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
773 * including unicast ones will be sent at the minimum MTU. Multicast
774 * packets will always be sent at the minimum MTU unless
775 * IP6PO_MINMTU_DISABLE is explicitly specified.
776 * See RFC 3542 for more details.
778 if (mtu > IPV6_MMTU) {
779 if ((flags & IPV6_MINMTU))
781 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
783 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
785 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
791 * clear embedded scope identifiers if necessary.
792 * in6_clearscope will touch the addresses only when necessary.
794 in6_clearscope(&ip6->ip6_src);
795 in6_clearscope(&ip6->ip6_dst);
798 * If the outgoing packet contains a hop-by-hop options header,
799 * it must be examined and processed even by the source node.
800 * (RFC 2460, section 4.)
802 if (exthdrs.ip6e_hbh) {
803 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
804 u_int32_t dummy; /* XXX unused */
805 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
808 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
809 panic("ip6e_hbh is not continuous");
812 * XXX: if we have to send an ICMPv6 error to the sender,
813 * we need the M_LOOP flag since icmp6_error() expects
814 * the IPv6 and the hop-by-hop options header are
815 * continuous unless the flag is set.
817 m->m_flags |= M_LOOP;
818 m->m_pkthdr.rcvif = ifp;
819 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
820 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
821 &dummy, &plen) < 0) {
822 /* m was already freed at this point */
823 error = EINVAL;/* better error? */
826 m->m_flags &= ~M_LOOP; /* XXX */
827 m->m_pkthdr.rcvif = NULL;
830 /* Jump over all PFIL processing if hooks are not active. */
831 if (!PFIL_HOOKED(&inet6_pfil_hook))
835 /* Run through list of hooks for output packets. */
836 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
837 if (error != 0 || m == NULL)
839 ip6 = mtod(m, struct ip6_hdr *);
841 /* See if destination IP address was changed by packet filter. */
842 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
843 m->m_flags |= M_SKIP_FIREWALL;
844 /* If destination is now ourself drop to ip6_input(). */
845 if (in6_localaddr(&ip6->ip6_dst)) {
846 if (m->m_pkthdr.rcvif == NULL)
847 m->m_pkthdr.rcvif = loif;
848 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
849 m->m_pkthdr.csum_flags |=
850 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
851 m->m_pkthdr.csum_data = 0xffff;
853 m->m_pkthdr.csum_flags |=
854 CSUM_IP_CHECKED | CSUM_IP_VALID;
855 error = netisr_queue(NETISR_IPV6, m);
858 goto again; /* Redo the routing table lookup. */
861 /* XXX: IPFIREWALL_FORWARD */
865 * Send the packet to the outgoing interface.
866 * If necessary, do IPv6 fragmentation before sending.
868 * the logic here is rather complex:
869 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
870 * 1-a: send as is if tlen <= path mtu
871 * 1-b: fragment if tlen > path mtu
873 * 2: if user asks us not to fragment (dontfrag == 1)
874 * 2-a: send as is if tlen <= interface mtu
875 * 2-b: error if tlen > interface mtu
877 * 3: if we always need to attach fragment header (alwaysfrag == 1)
880 * 4: if dontfrag == 1 && alwaysfrag == 1
881 * error, as we cannot handle this conflicting request
883 tlen = m->m_pkthdr.len;
885 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
889 if (dontfrag && alwaysfrag) { /* case 4 */
890 /* conflicting request - can't transmit */
894 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
896 * Even if the DONTFRAG option is specified, we cannot send the
897 * packet when the data length is larger than the MTU of the
898 * outgoing interface.
899 * Notify the error by sending IPV6_PATHMTU ancillary data as
900 * well as returning an error code (the latter is not described
904 struct ip6ctlparam ip6cp;
906 mtu32 = (u_int32_t)mtu;
907 bzero(&ip6cp, sizeof(ip6cp));
908 ip6cp.ip6c_cmdarg = (void *)&mtu32;
909 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
917 * transmit packet without fragmentation
919 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
920 struct in6_ifaddr *ia6;
922 ip6 = mtod(m, struct ip6_hdr *);
923 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
925 /* Record statistics for this interface address. */
926 ia6->ia_ifa.if_opackets++;
927 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
929 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
934 * try to fragment the packet. case 1-b and 3
936 if (mtu < IPV6_MMTU) {
937 /* path MTU cannot be less than IPV6_MMTU */
939 in6_ifstat_inc(ifp, ifs6_out_fragfail);
941 } else if (ip6->ip6_plen == 0) {
942 /* jumbo payload cannot be fragmented */
944 in6_ifstat_inc(ifp, ifs6_out_fragfail);
947 struct mbuf **mnext, *m_frgpart;
948 struct ip6_frag *ip6f;
949 u_int32_t id = htonl(ip6_randomid());
952 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
955 * Too large for the destination or interface;
956 * fragment if possible.
957 * Must be able to put at least 8 bytes per fragment.
959 hlen = unfragpartlen;
960 if (mtu > IPV6_MAXPACKET)
961 mtu = IPV6_MAXPACKET;
963 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
966 in6_ifstat_inc(ifp, ifs6_out_fragfail);
971 * Verify that we have any chance at all of being able to queue
972 * the packet or packet fragments
974 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
975 < tlen /* - hlen */)) {
977 ip6stat.ip6s_odropped++;
981 mnext = &m->m_nextpkt;
984 * Change the next header field of the last header in the
985 * unfragmentable part.
987 if (exthdrs.ip6e_rthdr) {
988 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
989 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
990 } else if (exthdrs.ip6e_dest1) {
991 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
992 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
993 } else if (exthdrs.ip6e_hbh) {
994 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
995 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
997 nextproto = ip6->ip6_nxt;
998 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1002 * Loop through length of segment after first fragment,
1003 * make new header and copy data of each part and link onto
1007 for (off = hlen; off < tlen; off += len) {
1008 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1011 ip6stat.ip6s_odropped++;
1014 m->m_pkthdr.rcvif = NULL;
1015 m->m_flags = m0->m_flags & M_COPYFLAGS;
1017 mnext = &m->m_nextpkt;
1018 m->m_data += max_linkhdr;
1019 mhip6 = mtod(m, struct ip6_hdr *);
1021 m->m_len = sizeof(*mhip6);
1022 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1024 ip6stat.ip6s_odropped++;
1027 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1028 if (off + len >= tlen)
1031 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1032 mhip6->ip6_plen = htons((u_short)(len + hlen +
1033 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1034 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1036 ip6stat.ip6s_odropped++;
1039 m_cat(m, m_frgpart);
1040 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1041 m->m_pkthdr.rcvif = NULL;
1042 ip6f->ip6f_reserved = 0;
1043 ip6f->ip6f_ident = id;
1044 ip6f->ip6f_nxt = nextproto;
1045 ip6stat.ip6s_ofragments++;
1046 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1049 in6_ifstat_inc(ifp, ifs6_out_fragok);
1053 * Remove leading garbages.
1059 for (m0 = m; m; m = m0) {
1063 /* Record statistics for this interface address. */
1065 ia->ia_ifa.if_opackets++;
1066 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1068 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1074 ip6stat.ip6s_fragmented++;
1077 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1079 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1080 RTFREE(ro_pmtu->ro_rt);
1086 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1087 m_freem(exthdrs.ip6e_dest1);
1088 m_freem(exthdrs.ip6e_rthdr);
1089 m_freem(exthdrs.ip6e_dest2);
1098 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1102 if (hlen > MCLBYTES)
1103 return (ENOBUFS); /* XXX */
1105 MGET(m, M_DONTWAIT, MT_DATA);
1110 MCLGET(m, M_DONTWAIT);
1111 if ((m->m_flags & M_EXT) == 0) {
1118 bcopy(hdr, mtod(m, caddr_t), hlen);
1125 * Insert jumbo payload option.
1128 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1134 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1137 * If there is no hop-by-hop options header, allocate new one.
1138 * If there is one but it doesn't have enough space to store the
1139 * jumbo payload option, allocate a cluster to store the whole options.
1140 * Otherwise, use it to store the options.
1142 if (exthdrs->ip6e_hbh == 0) {
1143 MGET(mopt, M_DONTWAIT, MT_DATA);
1146 mopt->m_len = JUMBOOPTLEN;
1147 optbuf = mtod(mopt, u_char *);
1148 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1149 exthdrs->ip6e_hbh = mopt;
1151 struct ip6_hbh *hbh;
1153 mopt = exthdrs->ip6e_hbh;
1154 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1157 * - exthdrs->ip6e_hbh is not referenced from places
1158 * other than exthdrs.
1159 * - exthdrs->ip6e_hbh is not an mbuf chain.
1161 int oldoptlen = mopt->m_len;
1165 * XXX: give up if the whole (new) hbh header does
1166 * not fit even in an mbuf cluster.
1168 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1172 * As a consequence, we must always prepare a cluster
1175 MGET(n, M_DONTWAIT, MT_DATA);
1177 MCLGET(n, M_DONTWAIT);
1178 if ((n->m_flags & M_EXT) == 0) {
1185 n->m_len = oldoptlen + JUMBOOPTLEN;
1186 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1188 optbuf = mtod(n, caddr_t) + oldoptlen;
1190 mopt = exthdrs->ip6e_hbh = n;
1192 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1193 mopt->m_len += JUMBOOPTLEN;
1195 optbuf[0] = IP6OPT_PADN;
1199 * Adjust the header length according to the pad and
1200 * the jumbo payload option.
1202 hbh = mtod(mopt, struct ip6_hbh *);
1203 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1206 /* fill in the option. */
1207 optbuf[2] = IP6OPT_JUMBO;
1209 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1210 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1212 /* finally, adjust the packet header length */
1213 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1220 * Insert fragment header and copy unfragmentable header portions.
1223 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1224 struct ip6_frag **frghdrp)
1226 struct mbuf *n, *mlast;
1228 if (hlen > sizeof(struct ip6_hdr)) {
1229 n = m_copym(m0, sizeof(struct ip6_hdr),
1230 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1237 /* Search for the last mbuf of unfragmentable part. */
1238 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1241 if ((mlast->m_flags & M_EXT) == 0 &&
1242 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1243 /* use the trailing space of the last mbuf for the fragment hdr */
1244 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1246 mlast->m_len += sizeof(struct ip6_frag);
1247 m->m_pkthdr.len += sizeof(struct ip6_frag);
1249 /* allocate a new mbuf for the fragment header */
1252 MGET(mfrg, M_DONTWAIT, MT_DATA);
1255 mfrg->m_len = sizeof(struct ip6_frag);
1256 *frghdrp = mtod(mfrg, struct ip6_frag *);
1257 mlast->m_next = mfrg;
1264 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1265 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1272 if (ro_pmtu != ro) {
1273 /* The first hop and the final destination may differ. */
1274 struct sockaddr_in6 *sa6_dst =
1275 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1276 if (ro_pmtu->ro_rt &&
1277 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1278 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1279 RTFREE(ro_pmtu->ro_rt);
1280 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1282 if (ro_pmtu->ro_rt == NULL) {
1283 bzero(sa6_dst, sizeof(*sa6_dst));
1284 sa6_dst->sin6_family = AF_INET6;
1285 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1286 sa6_dst->sin6_addr = *dst;
1288 rtalloc((struct route *)ro_pmtu);
1291 if (ro_pmtu->ro_rt) {
1293 struct in_conninfo inc;
1295 bzero(&inc, sizeof(inc));
1296 inc.inc_flags = 1; /* IPv6 */
1297 inc.inc6_faddr = *dst;
1300 ifp = ro_pmtu->ro_rt->rt_ifp;
1301 ifmtu = IN6_LINKMTU(ifp);
1302 mtu = tcp_hc_getmtu(&inc);
1304 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1306 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1309 else if (mtu < IPV6_MMTU) {
1311 * RFC2460 section 5, last paragraph:
1312 * if we record ICMPv6 too big message with
1313 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1314 * or smaller, with framgent header attached.
1315 * (fragment header is needed regardless from the
1316 * packet size, for translators to identify packets)
1320 } else if (mtu > ifmtu) {
1322 * The MTU on the route is larger than the MTU on
1323 * the interface! This shouldn't happen, unless the
1324 * MTU of the interface has been changed after the
1325 * interface was brought up. Change the MTU in the
1326 * route to match the interface MTU (as long as the
1327 * field isn't locked).
1330 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1333 mtu = IN6_LINKMTU(ifp);
1335 error = EHOSTUNREACH; /* XXX */
1339 *alwaysfragp = alwaysfrag;
1344 * IP6 socket option processing.
1347 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1349 int privileged, optdatalen, uproto;
1351 struct inpcb *in6p = sotoinpcb(so);
1353 int level, op, optname;
1358 level = sopt->sopt_level;
1359 op = sopt->sopt_dir;
1360 optname = sopt->sopt_name;
1361 optlen = sopt->sopt_valsize;
1364 panic("ip6_ctloutput: arg soopt is NULL");
1368 privileged = (td == 0 || suser(td)) ? 0 : 1;
1369 uproto = (int)so->so_proto->pr_protocol;
1371 if (level == IPPROTO_IPV6) {
1376 case IPV6_2292PKTOPTIONS:
1377 #ifdef IPV6_PKTOPTIONS
1378 case IPV6_PKTOPTIONS:
1383 error = soopt_getm(sopt, &m); /* XXX */
1386 error = soopt_mcopyin(sopt, m); /* XXX */
1389 error = ip6_pcbopts(&in6p->in6p_outputopts,
1391 m_freem(m); /* XXX */
1396 * Use of some Hop-by-Hop options or some
1397 * Destination options, might require special
1398 * privilege. That is, normal applications
1399 * (without special privilege) might be forbidden
1400 * from setting certain options in outgoing packets,
1401 * and might never see certain options in received
1402 * packets. [RFC 2292 Section 6]
1403 * KAME specific note:
1404 * KAME prevents non-privileged users from sending or
1405 * receiving ANY hbh/dst options in order to avoid
1406 * overhead of parsing options in the kernel.
1408 case IPV6_RECVHOPOPTS:
1409 case IPV6_RECVDSTOPTS:
1410 case IPV6_RECVRTHDRDSTOPTS:
1416 case IPV6_UNICAST_HOPS:
1420 case IPV6_RECVPKTINFO:
1421 case IPV6_RECVHOPLIMIT:
1422 case IPV6_RECVRTHDR:
1423 case IPV6_RECVPATHMTU:
1424 case IPV6_RECVTCLASS:
1426 case IPV6_AUTOFLOWLABEL:
1427 if (optlen != sizeof(int)) {
1431 error = sooptcopyin(sopt, &optval,
1432 sizeof optval, sizeof optval);
1437 case IPV6_UNICAST_HOPS:
1438 if (optval < -1 || optval >= 256)
1441 /* -1 = kernel default */
1442 in6p->in6p_hops = optval;
1443 if ((in6p->in6p_vflag &
1445 in6p->inp_ip_ttl = optval;
1448 #define OPTSET(bit) \
1451 in6p->in6p_flags |= (bit); \
1453 in6p->in6p_flags &= ~(bit); \
1454 } while (/*CONSTCOND*/ 0)
1455 #define OPTSET2292(bit) \
1457 in6p->in6p_flags |= IN6P_RFC2292; \
1459 in6p->in6p_flags |= (bit); \
1461 in6p->in6p_flags &= ~(bit); \
1462 } while (/*CONSTCOND*/ 0)
1463 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1465 case IPV6_RECVPKTINFO:
1466 /* cannot mix with RFC2292 */
1467 if (OPTBIT(IN6P_RFC2292)) {
1471 OPTSET(IN6P_PKTINFO);
1476 struct ip6_pktopts **optp;
1478 /* cannot mix with RFC2292 */
1479 if (OPTBIT(IN6P_RFC2292)) {
1483 optp = &in6p->in6p_outputopts;
1484 error = ip6_pcbopt(IPV6_HOPLIMIT,
1488 privileged, uproto);
1492 case IPV6_RECVHOPLIMIT:
1493 /* cannot mix with RFC2292 */
1494 if (OPTBIT(IN6P_RFC2292)) {
1498 OPTSET(IN6P_HOPLIMIT);
1501 case IPV6_RECVHOPOPTS:
1502 /* cannot mix with RFC2292 */
1503 if (OPTBIT(IN6P_RFC2292)) {
1507 OPTSET(IN6P_HOPOPTS);
1510 case IPV6_RECVDSTOPTS:
1511 /* cannot mix with RFC2292 */
1512 if (OPTBIT(IN6P_RFC2292)) {
1516 OPTSET(IN6P_DSTOPTS);
1519 case IPV6_RECVRTHDRDSTOPTS:
1520 /* cannot mix with RFC2292 */
1521 if (OPTBIT(IN6P_RFC2292)) {
1525 OPTSET(IN6P_RTHDRDSTOPTS);
1528 case IPV6_RECVRTHDR:
1529 /* cannot mix with RFC2292 */
1530 if (OPTBIT(IN6P_RFC2292)) {
1541 case IPV6_RECVPATHMTU:
1543 * We ignore this option for TCP
1545 * (RFC3542 leaves this case
1548 if (uproto != IPPROTO_TCP)
1554 * make setsockopt(IPV6_V6ONLY)
1555 * available only prior to bind(2).
1556 * see ipng mailing list, Jun 22 2001.
1558 if (in6p->in6p_lport ||
1559 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1563 OPTSET(IN6P_IPV6_V6ONLY);
1565 in6p->in6p_vflag &= ~INP_IPV4;
1567 in6p->in6p_vflag |= INP_IPV4;
1569 case IPV6_RECVTCLASS:
1570 /* cannot mix with RFC2292 XXX */
1571 if (OPTBIT(IN6P_RFC2292)) {
1575 OPTSET(IN6P_TCLASS);
1577 case IPV6_AUTOFLOWLABEL:
1578 OPTSET(IN6P_AUTOFLOWLABEL);
1586 case IPV6_USE_MIN_MTU:
1587 case IPV6_PREFER_TEMPADDR:
1588 if (optlen != sizeof(optval)) {
1592 error = sooptcopyin(sopt, &optval,
1593 sizeof optval, sizeof optval);
1597 struct ip6_pktopts **optp;
1598 optp = &in6p->in6p_outputopts;
1599 error = ip6_pcbopt(optname,
1603 privileged, uproto);
1607 case IPV6_2292PKTINFO:
1608 case IPV6_2292HOPLIMIT:
1609 case IPV6_2292HOPOPTS:
1610 case IPV6_2292DSTOPTS:
1611 case IPV6_2292RTHDR:
1613 if (optlen != sizeof(int)) {
1617 error = sooptcopyin(sopt, &optval,
1618 sizeof optval, sizeof optval);
1622 case IPV6_2292PKTINFO:
1623 OPTSET2292(IN6P_PKTINFO);
1625 case IPV6_2292HOPLIMIT:
1626 OPTSET2292(IN6P_HOPLIMIT);
1628 case IPV6_2292HOPOPTS:
1630 * Check super-user privilege.
1631 * See comments for IPV6_RECVHOPOPTS.
1635 OPTSET2292(IN6P_HOPOPTS);
1637 case IPV6_2292DSTOPTS:
1640 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1642 case IPV6_2292RTHDR:
1643 OPTSET2292(IN6P_RTHDR);
1651 case IPV6_RTHDRDSTOPTS:
1654 /* new advanced API (RFC3542) */
1656 u_char optbuf_storage[MCLBYTES];
1658 struct ip6_pktopts **optp;
1660 /* cannot mix with RFC2292 */
1661 if (OPTBIT(IN6P_RFC2292)) {
1667 * We only ensure valsize is not too large
1668 * here. Further validation will be done
1671 error = sooptcopyin(sopt, optbuf_storage,
1672 sizeof(optbuf_storage), 0);
1675 optlen = sopt->sopt_valsize;
1676 optbuf = optbuf_storage;
1677 optp = &in6p->in6p_outputopts;
1678 error = ip6_pcbopt(optname,
1680 optp, privileged, uproto);
1685 case IPV6_MULTICAST_IF:
1686 case IPV6_MULTICAST_HOPS:
1687 case IPV6_MULTICAST_LOOP:
1688 case IPV6_JOIN_GROUP:
1689 case IPV6_LEAVE_GROUP:
1691 if (sopt->sopt_valsize > MLEN) {
1701 if (sopt->sopt_valsize > MCLBYTES) {
1706 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1711 if (sopt->sopt_valsize > MLEN) {
1712 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1713 if ((m->m_flags & M_EXT) == 0) {
1719 m->m_len = sopt->sopt_valsize;
1720 error = sooptcopyin(sopt, mtod(m, char *),
1721 m->m_len, m->m_len);
1726 error = ip6_setmoptions(sopt->sopt_name,
1727 &in6p->in6p_moptions,
1733 case IPV6_PORTRANGE:
1734 error = sooptcopyin(sopt, &optval,
1735 sizeof optval, sizeof optval);
1740 case IPV6_PORTRANGE_DEFAULT:
1741 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1742 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1745 case IPV6_PORTRANGE_HIGH:
1746 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1747 in6p->in6p_flags |= IN6P_HIGHPORT;
1750 case IPV6_PORTRANGE_LOW:
1751 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1752 in6p->in6p_flags |= IN6P_LOWPORT;
1762 case IPV6_IPSEC_POLICY:
1768 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1770 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1773 req = mtod(m, caddr_t);
1776 error = ipsec6_set_policy(in6p, optname, req,
1784 error = ENOPROTOOPT;
1792 case IPV6_2292PKTOPTIONS:
1793 #ifdef IPV6_PKTOPTIONS
1794 case IPV6_PKTOPTIONS:
1797 * RFC3542 (effectively) deprecated the
1798 * semantics of the 2292-style pktoptions.
1799 * Since it was not reliable in nature (i.e.,
1800 * applications had to expect the lack of some
1801 * information after all), it would make sense
1802 * to simplify this part by always returning
1805 sopt->sopt_valsize = 0;
1808 case IPV6_RECVHOPOPTS:
1809 case IPV6_RECVDSTOPTS:
1810 case IPV6_RECVRTHDRDSTOPTS:
1811 case IPV6_UNICAST_HOPS:
1812 case IPV6_RECVPKTINFO:
1813 case IPV6_RECVHOPLIMIT:
1814 case IPV6_RECVRTHDR:
1815 case IPV6_RECVPATHMTU:
1819 case IPV6_PORTRANGE:
1820 case IPV6_RECVTCLASS:
1821 case IPV6_AUTOFLOWLABEL:
1824 case IPV6_RECVHOPOPTS:
1825 optval = OPTBIT(IN6P_HOPOPTS);
1828 case IPV6_RECVDSTOPTS:
1829 optval = OPTBIT(IN6P_DSTOPTS);
1832 case IPV6_RECVRTHDRDSTOPTS:
1833 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1836 case IPV6_UNICAST_HOPS:
1837 optval = in6p->in6p_hops;
1840 case IPV6_RECVPKTINFO:
1841 optval = OPTBIT(IN6P_PKTINFO);
1844 case IPV6_RECVHOPLIMIT:
1845 optval = OPTBIT(IN6P_HOPLIMIT);
1848 case IPV6_RECVRTHDR:
1849 optval = OPTBIT(IN6P_RTHDR);
1852 case IPV6_RECVPATHMTU:
1853 optval = OPTBIT(IN6P_MTU);
1857 optval = OPTBIT(IN6P_FAITH);
1861 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1864 case IPV6_PORTRANGE:
1867 flags = in6p->in6p_flags;
1868 if (flags & IN6P_HIGHPORT)
1869 optval = IPV6_PORTRANGE_HIGH;
1870 else if (flags & IN6P_LOWPORT)
1871 optval = IPV6_PORTRANGE_LOW;
1876 case IPV6_RECVTCLASS:
1877 optval = OPTBIT(IN6P_TCLASS);
1880 case IPV6_AUTOFLOWLABEL:
1881 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1886 error = sooptcopyout(sopt, &optval,
1893 struct ip6_mtuinfo mtuinfo;
1894 struct route_in6 sro;
1896 bzero(&sro, sizeof(sro));
1898 if (!(so->so_state & SS_ISCONNECTED))
1901 * XXX: we dot not consider the case of source
1902 * routing, or optional information to specify
1903 * the outgoing interface.
1905 error = ip6_getpmtu(&sro, NULL, NULL,
1906 &in6p->in6p_faddr, &pmtu, NULL);
1911 if (pmtu > IPV6_MAXPACKET)
1912 pmtu = IPV6_MAXPACKET;
1914 bzero(&mtuinfo, sizeof(mtuinfo));
1915 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1916 optdata = (void *)&mtuinfo;
1917 optdatalen = sizeof(mtuinfo);
1918 error = sooptcopyout(sopt, optdata,
1923 case IPV6_2292PKTINFO:
1924 case IPV6_2292HOPLIMIT:
1925 case IPV6_2292HOPOPTS:
1926 case IPV6_2292RTHDR:
1927 case IPV6_2292DSTOPTS:
1929 case IPV6_2292PKTINFO:
1930 optval = OPTBIT(IN6P_PKTINFO);
1932 case IPV6_2292HOPLIMIT:
1933 optval = OPTBIT(IN6P_HOPLIMIT);
1935 case IPV6_2292HOPOPTS:
1936 optval = OPTBIT(IN6P_HOPOPTS);
1938 case IPV6_2292RTHDR:
1939 optval = OPTBIT(IN6P_RTHDR);
1941 case IPV6_2292DSTOPTS:
1942 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1945 error = sooptcopyout(sopt, &optval,
1952 case IPV6_RTHDRDSTOPTS:
1956 case IPV6_USE_MIN_MTU:
1957 case IPV6_PREFER_TEMPADDR:
1958 error = ip6_getpcbopt(in6p->in6p_outputopts,
1962 case IPV6_MULTICAST_IF:
1963 case IPV6_MULTICAST_HOPS:
1964 case IPV6_MULTICAST_LOOP:
1965 case IPV6_JOIN_GROUP:
1966 case IPV6_LEAVE_GROUP:
1969 error = ip6_getmoptions(sopt->sopt_name,
1970 in6p->in6p_moptions, &m);
1972 error = sooptcopyout(sopt,
1973 mtod(m, char *), m->m_len);
1979 case IPV6_IPSEC_POLICY:
1983 struct mbuf *m = NULL;
1984 struct mbuf **mp = &m;
1985 size_t ovalsize = sopt->sopt_valsize;
1986 caddr_t oval = (caddr_t)sopt->sopt_val;
1988 error = soopt_getm(sopt, &m); /* XXX */
1991 error = soopt_mcopyin(sopt, m); /* XXX */
1994 sopt->sopt_valsize = ovalsize;
1995 sopt->sopt_val = oval;
1997 req = mtod(m, caddr_t);
2000 error = ipsec6_get_policy(in6p, req, len, mp);
2002 error = soopt_mcopyout(sopt, m); /* XXX */
2003 if (error == 0 && m)
2010 error = ENOPROTOOPT;
2015 } else { /* level != IPPROTO_IPV6 */
2022 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2024 int error = 0, optval, optlen;
2025 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2026 struct in6pcb *in6p = sotoin6pcb(so);
2027 int level, op, optname;
2030 level = sopt->sopt_level;
2031 op = sopt->sopt_dir;
2032 optname = sopt->sopt_name;
2033 optlen = sopt->sopt_valsize;
2035 panic("ip6_raw_ctloutput: arg soopt is NULL");
2037 if (level != IPPROTO_IPV6) {
2044 * For ICMPv6 sockets, no modification allowed for checksum
2045 * offset, permit "no change" values to help existing apps.
2047 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2048 * for an ICMPv6 socket will fail."
2049 * The current behavior does not meet RFC3542.
2053 if (optlen != sizeof(int)) {
2057 error = sooptcopyin(sopt, &optval, sizeof(optval),
2061 if ((optval % 2) != 0) {
2062 /* the API assumes even offset values */
2064 } else if (so->so_proto->pr_protocol ==
2066 if (optval != icmp6off)
2069 in6p->in6p_cksum = optval;
2073 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2076 optval = in6p->in6p_cksum;
2078 error = sooptcopyout(sopt, &optval, sizeof(optval));
2088 error = ENOPROTOOPT;
2096 * Set up IP6 options in pcb for insertion in output packets or
2097 * specifying behavior of outgoing packets.
2100 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2101 struct socket *so, struct sockopt *sopt)
2103 struct ip6_pktopts *opt = *pktopt;
2105 struct thread *td = sopt->sopt_td;
2108 /* turn off any old options. */
2111 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2112 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2113 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2114 printf("ip6_pcbopts: all specified options are cleared.\n");
2116 ip6_clearpktopts(opt, -1);
2118 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2121 if (!m || m->m_len == 0) {
2123 * Only turning off any previous options, regardless of
2124 * whether the opt is just created or given.
2126 free(opt, M_IP6OPT);
2130 /* set options specified by user. */
2131 if (td && !suser(td))
2133 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2134 so->so_proto->pr_protocol)) != 0) {
2135 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2136 free(opt, M_IP6OPT);
2144 * initialize ip6_pktopts. beware that there are non-zero default values in
2148 ip6_initpktopts(struct ip6_pktopts *opt)
2151 bzero(opt, sizeof(*opt));
2152 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2153 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2154 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2155 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2159 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2160 int priv, int uproto)
2162 struct ip6_pktopts *opt;
2164 if (*pktopt == NULL) {
2165 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2167 ip6_initpktopts(*pktopt);
2171 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2175 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2177 void *optdata = NULL;
2179 struct ip6_ext *ip6e;
2181 struct in6_pktinfo null_pktinfo;
2182 int deftclass = 0, on;
2183 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2184 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2188 if (pktopt && pktopt->ip6po_pktinfo)
2189 optdata = (void *)pktopt->ip6po_pktinfo;
2191 /* XXX: we don't have to do this every time... */
2192 bzero(&null_pktinfo, sizeof(null_pktinfo));
2193 optdata = (void *)&null_pktinfo;
2195 optdatalen = sizeof(struct in6_pktinfo);
2198 if (pktopt && pktopt->ip6po_tclass >= 0)
2199 optdata = (void *)&pktopt->ip6po_tclass;
2201 optdata = (void *)&deftclass;
2202 optdatalen = sizeof(int);
2205 if (pktopt && pktopt->ip6po_hbh) {
2206 optdata = (void *)pktopt->ip6po_hbh;
2207 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2208 optdatalen = (ip6e->ip6e_len + 1) << 3;
2212 if (pktopt && pktopt->ip6po_rthdr) {
2213 optdata = (void *)pktopt->ip6po_rthdr;
2214 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2215 optdatalen = (ip6e->ip6e_len + 1) << 3;
2218 case IPV6_RTHDRDSTOPTS:
2219 if (pktopt && pktopt->ip6po_dest1) {
2220 optdata = (void *)pktopt->ip6po_dest1;
2221 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2222 optdatalen = (ip6e->ip6e_len + 1) << 3;
2226 if (pktopt && pktopt->ip6po_dest2) {
2227 optdata = (void *)pktopt->ip6po_dest2;
2228 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2229 optdatalen = (ip6e->ip6e_len + 1) << 3;
2233 if (pktopt && pktopt->ip6po_nexthop) {
2234 optdata = (void *)pktopt->ip6po_nexthop;
2235 optdatalen = pktopt->ip6po_nexthop->sa_len;
2238 case IPV6_USE_MIN_MTU:
2240 optdata = (void *)&pktopt->ip6po_minmtu;
2242 optdata = (void *)&defminmtu;
2243 optdatalen = sizeof(int);
2246 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2250 optdata = (void *)&on;
2251 optdatalen = sizeof(on);
2253 case IPV6_PREFER_TEMPADDR:
2255 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2257 optdata = (void *)&defpreftemp;
2258 optdatalen = sizeof(int);
2260 default: /* should not happen */
2262 panic("ip6_getpcbopt: unexpected option\n");
2264 return (ENOPROTOOPT);
2267 error = sooptcopyout(sopt, optdata, optdatalen);
2273 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2278 if (optname == -1 || optname == IPV6_PKTINFO) {
2279 if (pktopt->ip6po_pktinfo)
2280 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2281 pktopt->ip6po_pktinfo = NULL;
2283 if (optname == -1 || optname == IPV6_HOPLIMIT)
2284 pktopt->ip6po_hlim = -1;
2285 if (optname == -1 || optname == IPV6_TCLASS)
2286 pktopt->ip6po_tclass = -1;
2287 if (optname == -1 || optname == IPV6_NEXTHOP) {
2288 if (pktopt->ip6po_nextroute.ro_rt) {
2289 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2290 pktopt->ip6po_nextroute.ro_rt = NULL;
2292 if (pktopt->ip6po_nexthop)
2293 free(pktopt->ip6po_nexthop, M_IP6OPT);
2294 pktopt->ip6po_nexthop = NULL;
2296 if (optname == -1 || optname == IPV6_HOPOPTS) {
2297 if (pktopt->ip6po_hbh)
2298 free(pktopt->ip6po_hbh, M_IP6OPT);
2299 pktopt->ip6po_hbh = NULL;
2301 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2302 if (pktopt->ip6po_dest1)
2303 free(pktopt->ip6po_dest1, M_IP6OPT);
2304 pktopt->ip6po_dest1 = NULL;
2306 if (optname == -1 || optname == IPV6_RTHDR) {
2307 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2308 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2309 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2310 if (pktopt->ip6po_route.ro_rt) {
2311 RTFREE(pktopt->ip6po_route.ro_rt);
2312 pktopt->ip6po_route.ro_rt = NULL;
2315 if (optname == -1 || optname == IPV6_DSTOPTS) {
2316 if (pktopt->ip6po_dest2)
2317 free(pktopt->ip6po_dest2, M_IP6OPT);
2318 pktopt->ip6po_dest2 = NULL;
2322 #define PKTOPT_EXTHDRCPY(type) \
2325 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2326 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2327 if (dst->type == NULL && canwait == M_NOWAIT)\
2329 bcopy(src->type, dst->type, hlen);\
2331 } while (/*CONSTCOND*/ 0)
2334 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2336 if (dst == NULL || src == NULL) {
2337 printf("ip6_clearpktopts: invalid argument\n");
2341 dst->ip6po_hlim = src->ip6po_hlim;
2342 dst->ip6po_tclass = src->ip6po_tclass;
2343 dst->ip6po_flags = src->ip6po_flags;
2344 if (src->ip6po_pktinfo) {
2345 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2347 if (dst->ip6po_pktinfo == NULL)
2349 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2351 if (src->ip6po_nexthop) {
2352 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2354 if (dst->ip6po_nexthop == NULL)
2356 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2357 src->ip6po_nexthop->sa_len);
2359 PKTOPT_EXTHDRCPY(ip6po_hbh);
2360 PKTOPT_EXTHDRCPY(ip6po_dest1);
2361 PKTOPT_EXTHDRCPY(ip6po_dest2);
2362 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2366 ip6_clearpktopts(dst, -1);
2369 #undef PKTOPT_EXTHDRCPY
2371 struct ip6_pktopts *
2372 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2375 struct ip6_pktopts *dst;
2377 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2380 ip6_initpktopts(dst);
2382 if ((error = copypktopts(dst, src, canwait)) != 0) {
2383 free(dst, M_IP6OPT);
2391 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2396 ip6_clearpktopts(pktopt, -1);
2398 free(pktopt, M_IP6OPT);
2402 * Set the IP6 multicast options in response to user setsockopt().
2405 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2408 u_int loop, ifindex;
2409 struct ipv6_mreq *mreq;
2411 struct ip6_moptions *im6o = *im6op;
2412 struct route_in6 ro;
2413 struct in6_multi_mship *imm;
2414 struct thread *td = curthread;
2418 * No multicast option buffer attached to the pcb;
2419 * allocate one and initialize to default values.
2421 im6o = (struct ip6_moptions *)
2422 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK);
2427 im6o->im6o_multicast_ifp = NULL;
2428 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2429 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2430 LIST_INIT(&im6o->im6o_memberships);
2435 case IPV6_MULTICAST_IF:
2437 * Select the interface for outgoing multicast packets.
2439 if (m == NULL || m->m_len != sizeof(u_int)) {
2443 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2444 if (ifindex < 0 || if_index < ifindex) {
2445 error = ENXIO; /* XXX EINVAL? */
2448 ifp = ifnet_byindex(ifindex);
2449 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2450 error = EADDRNOTAVAIL;
2453 im6o->im6o_multicast_ifp = ifp;
2456 case IPV6_MULTICAST_HOPS:
2459 * Set the IP6 hoplimit for outgoing multicast packets.
2462 if (m == NULL || m->m_len != sizeof(int)) {
2466 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2467 if (optval < -1 || optval >= 256)
2469 else if (optval == -1)
2470 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2472 im6o->im6o_multicast_hlim = optval;
2476 case IPV6_MULTICAST_LOOP:
2478 * Set the loopback flag for outgoing multicast packets.
2479 * Must be zero or one.
2481 if (m == NULL || m->m_len != sizeof(u_int)) {
2485 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2490 im6o->im6o_multicast_loop = loop;
2493 case IPV6_JOIN_GROUP:
2495 * Add a multicast group membership.
2496 * Group must be a valid IP6 multicast address.
2498 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2502 mreq = mtod(m, struct ipv6_mreq *);
2504 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2506 * We use the unspecified address to specify to accept
2507 * all multicast addresses. Only super user is allowed
2514 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2520 * If no interface was explicitly specified, choose an
2521 * appropriate one according to the given multicast address.
2523 if (mreq->ipv6mr_interface == 0) {
2524 struct sockaddr_in6 *dst;
2527 * Look up the routing table for the
2528 * address, and choose the outgoing interface.
2529 * XXX: is it a good approach?
2532 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2533 bzero(dst, sizeof(*dst));
2534 dst->sin6_family = AF_INET6;
2535 dst->sin6_len = sizeof(*dst);
2536 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2537 rtalloc((struct route *)&ro);
2538 if (ro.ro_rt == NULL) {
2539 error = EADDRNOTAVAIL;
2542 ifp = ro.ro_rt->rt_ifp;
2546 * If the interface is specified, validate it.
2548 if (mreq->ipv6mr_interface < 0 ||
2549 if_index < mreq->ipv6mr_interface) {
2550 error = ENXIO; /* XXX EINVAL? */
2553 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2555 error = ENXIO; /* XXX EINVAL? */
2561 * See if we found an interface, and confirm that it
2562 * supports multicast
2564 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2565 error = EADDRNOTAVAIL;
2569 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2570 error = EADDRNOTAVAIL; /* XXX: should not happen */
2575 * See if the membership already exists.
2577 for (imm = im6o->im6o_memberships.lh_first;
2578 imm != NULL; imm = imm->i6mm_chain.le_next)
2579 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2580 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2581 &mreq->ipv6mr_multiaddr))
2588 * Everything looks good; add a new record to the multicast
2589 * address list for the given interface.
2591 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2594 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2597 case IPV6_LEAVE_GROUP:
2599 * Drop a multicast group membership.
2600 * Group must be a valid IP6 multicast address.
2602 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2606 mreq = mtod(m, struct ipv6_mreq *);
2609 * If an interface address was specified, get a pointer
2610 * to its ifnet structure.
2612 if (mreq->ipv6mr_interface < 0 ||
2613 if_index < mreq->ipv6mr_interface) {
2614 error = ENXIO; /* XXX EINVAL? */
2617 if (mreq->ipv6mr_interface == 0)
2620 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2622 /* Fill in the scope zone ID */
2624 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2625 /* XXX: should not happen */
2626 error = EADDRNOTAVAIL;
2629 } else if (mreq->ipv6mr_interface != 0) {
2631 * This case happens when the (positive) index is in
2632 * the valid range, but the corresponding interface has
2633 * been detached dynamically (XXX).
2635 error = EADDRNOTAVAIL;
2637 } else { /* ipv6mr_interface == 0 */
2638 struct sockaddr_in6 sa6_mc;
2641 * The API spec says as follows:
2642 * If the interface index is specified as 0, the
2643 * system may choose a multicast group membership to
2644 * drop by matching the multicast address only.
2645 * On the other hand, we cannot disambiguate the scope
2646 * zone unless an interface is provided. Thus, we
2647 * check if there's ambiguity with the default scope
2648 * zone as the last resort.
2650 bzero(&sa6_mc, sizeof(sa6_mc));
2651 sa6_mc.sin6_family = AF_INET6;
2652 sa6_mc.sin6_len = sizeof(sa6_mc);
2653 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2654 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2657 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2661 * Find the membership in the membership list.
2663 for (imm = im6o->im6o_memberships.lh_first;
2664 imm != NULL; imm = imm->i6mm_chain.le_next) {
2665 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2666 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2667 &mreq->ipv6mr_multiaddr))
2671 /* Unable to resolve interface */
2672 error = EADDRNOTAVAIL;
2676 * Give up the multicast address record to which the
2677 * membership points.
2679 LIST_REMOVE(imm, i6mm_chain);
2680 in6_delmulti(imm->i6mm_maddr);
2681 free(imm, M_IP6MADDR);
2690 * If all options have default values, no need to keep the mbuf.
2692 if (im6o->im6o_multicast_ifp == NULL &&
2693 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2694 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2695 im6o->im6o_memberships.lh_first == NULL) {
2696 free(*im6op, M_IP6MOPTS);
2704 * Return the IP6 multicast options in response to user getsockopt().
2707 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2709 u_int *hlim, *loop, *ifindex;
2711 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2715 case IPV6_MULTICAST_IF:
2716 ifindex = mtod(*mp, u_int *);
2717 (*mp)->m_len = sizeof(u_int);
2718 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2721 *ifindex = im6o->im6o_multicast_ifp->if_index;
2724 case IPV6_MULTICAST_HOPS:
2725 hlim = mtod(*mp, u_int *);
2726 (*mp)->m_len = sizeof(u_int);
2728 *hlim = ip6_defmcasthlim;
2730 *hlim = im6o->im6o_multicast_hlim;
2733 case IPV6_MULTICAST_LOOP:
2734 loop = mtod(*mp, u_int *);
2735 (*mp)->m_len = sizeof(u_int);
2737 *loop = ip6_defmcasthlim;
2739 *loop = im6o->im6o_multicast_loop;
2743 return (EOPNOTSUPP);
2748 * Discard the IP6 multicast options.
2751 ip6_freemoptions(struct ip6_moptions *im6o)
2753 struct in6_multi_mship *imm;
2758 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2759 LIST_REMOVE(imm, i6mm_chain);
2760 if (imm->i6mm_maddr)
2761 in6_delmulti(imm->i6mm_maddr);
2762 free(imm, M_IP6MADDR);
2764 free(im6o, M_IP6MOPTS);
2768 * Set IPv6 outgoing packet options based on advanced API.
2771 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2772 struct ip6_pktopts *stickyopt, int priv, int uproto)
2774 struct cmsghdr *cm = 0;
2776 if (control == NULL || opt == NULL)
2779 ip6_initpktopts(opt);
2784 * If stickyopt is provided, make a local copy of the options
2785 * for this particular packet, then override them by ancillary
2787 * XXX: copypktopts() does not copy the cached route to a next
2788 * hop (if any). This is not very good in terms of efficiency,
2789 * but we can allow this since this option should be rarely
2792 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2797 * XXX: Currently, we assume all the optional information is stored
2800 if (control->m_next)
2803 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2804 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2807 if (control->m_len < CMSG_LEN(0))
2810 cm = mtod(control, struct cmsghdr *);
2811 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2813 if (cm->cmsg_level != IPPROTO_IPV6)
2816 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2817 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2826 * Set a particular packet option, as a sticky option or an ancillary data
2827 * item. "len" can be 0 only when it's a sticky option.
2828 * We have 4 cases of combination of "sticky" and "cmsg":
2829 * "sticky=0, cmsg=0": impossible
2830 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2831 * "sticky=1, cmsg=0": RFC3542 socket option
2832 * "sticky=1, cmsg=1": RFC2292 socket option
2835 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2836 int priv, int sticky, int cmsg, int uproto)
2838 int minmtupolicy, preftemp;
2840 if (!sticky && !cmsg) {
2842 printf("ip6_setpktopt: impossible case\n");
2848 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2849 * not be specified in the context of RFC3542. Conversely,
2850 * RFC3542 types should not be specified in the context of RFC2292.
2854 case IPV6_2292PKTINFO:
2855 case IPV6_2292HOPLIMIT:
2856 case IPV6_2292NEXTHOP:
2857 case IPV6_2292HOPOPTS:
2858 case IPV6_2292DSTOPTS:
2859 case IPV6_2292RTHDR:
2860 case IPV6_2292PKTOPTIONS:
2861 return (ENOPROTOOPT);
2864 if (sticky && cmsg) {
2871 case IPV6_RTHDRDSTOPTS:
2873 case IPV6_USE_MIN_MTU:
2876 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2877 return (ENOPROTOOPT);
2882 case IPV6_2292PKTINFO:
2885 struct ifnet *ifp = NULL;
2886 struct in6_pktinfo *pktinfo;
2888 if (len != sizeof(struct in6_pktinfo))
2891 pktinfo = (struct in6_pktinfo *)buf;
2894 * An application can clear any sticky IPV6_PKTINFO option by
2895 * doing a "regular" setsockopt with ipi6_addr being
2896 * in6addr_any and ipi6_ifindex being zero.
2897 * [RFC 3542, Section 6]
2899 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2900 pktinfo->ipi6_ifindex == 0 &&
2901 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2902 ip6_clearpktopts(opt, optname);
2906 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2907 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2911 /* validate the interface index if specified. */
2912 if (pktinfo->ipi6_ifindex > if_index ||
2913 pktinfo->ipi6_ifindex < 0) {
2916 if (pktinfo->ipi6_ifindex) {
2917 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2923 * We store the address anyway, and let in6_selectsrc()
2924 * validate the specified address. This is because ipi6_addr
2925 * may not have enough information about its scope zone, and
2926 * we may need additional information (such as outgoing
2927 * interface or the scope zone of a destination address) to
2928 * disambiguate the scope.
2929 * XXX: the delay of the validation may confuse the
2930 * application when it is used as a sticky option.
2932 if (opt->ip6po_pktinfo == NULL) {
2933 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2934 M_IP6OPT, M_NOWAIT);
2935 if (opt->ip6po_pktinfo == NULL)
2938 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2942 case IPV6_2292HOPLIMIT:
2948 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2949 * to simplify the ordering among hoplimit options.
2951 if (optname == IPV6_HOPLIMIT && sticky)
2952 return (ENOPROTOOPT);
2954 if (len != sizeof(int))
2957 if (*hlimp < -1 || *hlimp > 255)
2960 opt->ip6po_hlim = *hlimp;
2968 if (len != sizeof(int))
2970 tclass = *(int *)buf;
2971 if (tclass < -1 || tclass > 255)
2974 opt->ip6po_tclass = tclass;
2978 case IPV6_2292NEXTHOP:
2983 if (len == 0) { /* just remove the option */
2984 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2988 /* check if cmsg_len is large enough for sa_len */
2989 if (len < sizeof(struct sockaddr) || len < *buf)
2992 switch (((struct sockaddr *)buf)->sa_family) {
2995 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2998 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3001 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3002 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3005 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3011 case AF_LINK: /* should eventually be supported */
3013 return (EAFNOSUPPORT);
3016 /* turn off the previous option, then set the new option. */
3017 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3018 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3019 if (opt->ip6po_nexthop == NULL)
3021 bcopy(buf, opt->ip6po_nexthop, *buf);
3024 case IPV6_2292HOPOPTS:
3027 struct ip6_hbh *hbh;
3031 * XXX: We don't allow a non-privileged user to set ANY HbH
3032 * options, since per-option restriction has too much
3039 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3040 break; /* just remove the option */
3043 /* message length validation */
3044 if (len < sizeof(struct ip6_hbh))
3046 hbh = (struct ip6_hbh *)buf;
3047 hbhlen = (hbh->ip6h_len + 1) << 3;
3051 /* turn off the previous option, then set the new option. */
3052 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3053 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3054 if (opt->ip6po_hbh == NULL)
3056 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3061 case IPV6_2292DSTOPTS:
3063 case IPV6_RTHDRDSTOPTS:
3065 struct ip6_dest *dest, **newdest = NULL;
3068 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3072 ip6_clearpktopts(opt, optname);
3073 break; /* just remove the option */
3076 /* message length validation */
3077 if (len < sizeof(struct ip6_dest))
3079 dest = (struct ip6_dest *)buf;
3080 destlen = (dest->ip6d_len + 1) << 3;
3085 * Determine the position that the destination options header
3086 * should be inserted; before or after the routing header.
3089 case IPV6_2292DSTOPTS:
3091 * The old advacned API is ambiguous on this point.
3092 * Our approach is to determine the position based
3093 * according to the existence of a routing header.
3094 * Note, however, that this depends on the order of the
3095 * extension headers in the ancillary data; the 1st
3096 * part of the destination options header must appear
3097 * before the routing header in the ancillary data,
3099 * RFC3542 solved the ambiguity by introducing
3100 * separate ancillary data or option types.
3102 if (opt->ip6po_rthdr == NULL)
3103 newdest = &opt->ip6po_dest1;
3105 newdest = &opt->ip6po_dest2;
3107 case IPV6_RTHDRDSTOPTS:
3108 newdest = &opt->ip6po_dest1;
3111 newdest = &opt->ip6po_dest2;
3115 /* turn off the previous option, then set the new option. */
3116 ip6_clearpktopts(opt, optname);
3117 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3118 if (*newdest == NULL)
3120 bcopy(dest, *newdest, destlen);
3125 case IPV6_2292RTHDR:
3128 struct ip6_rthdr *rth;
3132 ip6_clearpktopts(opt, IPV6_RTHDR);
3133 break; /* just remove the option */
3136 /* message length validation */
3137 if (len < sizeof(struct ip6_rthdr))
3139 rth = (struct ip6_rthdr *)buf;
3140 rthlen = (rth->ip6r_len + 1) << 3;
3144 switch (rth->ip6r_type) {
3145 case IPV6_RTHDR_TYPE_0:
3146 if (rth->ip6r_len == 0) /* must contain one addr */
3148 if (rth->ip6r_len % 2) /* length must be even */
3150 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3154 return (EINVAL); /* not supported */
3157 /* turn off the previous option */
3158 ip6_clearpktopts(opt, IPV6_RTHDR);
3159 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3160 if (opt->ip6po_rthdr == NULL)
3162 bcopy(rth, opt->ip6po_rthdr, rthlen);
3167 case IPV6_USE_MIN_MTU:
3168 if (len != sizeof(int))
3170 minmtupolicy = *(int *)buf;
3171 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3172 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3173 minmtupolicy != IP6PO_MINMTU_ALL) {
3176 opt->ip6po_minmtu = minmtupolicy;
3180 if (len != sizeof(int))
3183 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3185 * we ignore this option for TCP sockets.
3186 * (RFC3542 leaves this case unspecified.)
3188 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3190 opt->ip6po_flags |= IP6PO_DONTFRAG;
3193 case IPV6_PREFER_TEMPADDR:
3194 if (len != sizeof(int))
3196 preftemp = *(int *)buf;
3197 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3198 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3199 preftemp != IP6PO_TEMPADDR_PREFER) {
3202 opt->ip6po_prefer_tempaddr = preftemp;
3206 return (ENOPROTOOPT);
3207 } /* end of switch */
3213 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3214 * packet to the input queue of a specified interface. Note that this
3215 * calls the output routine of the loopback "driver", but with an interface
3216 * pointer that might NOT be &loif -- easier than replicating that code here.
3219 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3222 struct ip6_hdr *ip6;
3224 copym = m_copy(m, 0, M_COPYALL);
3229 * Make sure to deep-copy IPv6 header portion in case the data
3230 * is in an mbuf cluster, so that we can safely override the IPv6
3231 * header portion later.
3233 if ((copym->m_flags & M_EXT) != 0 ||
3234 copym->m_len < sizeof(struct ip6_hdr)) {
3235 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3241 if (copym->m_len < sizeof(*ip6)) {
3247 ip6 = mtod(copym, struct ip6_hdr *);
3249 * clear embedded scope identifiers if necessary.
3250 * in6_clearscope will touch the addresses only when necessary.
3252 in6_clearscope(&ip6->ip6_src);
3253 in6_clearscope(&ip6->ip6_dst);
3255 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3259 * Chop IPv6 header off from the payload.
3262 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3265 struct ip6_hdr *ip6;
3267 ip6 = mtod(m, struct ip6_hdr *);
3268 if (m->m_len > sizeof(*ip6)) {
3269 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3274 M_MOVE_PKTHDR(mh, m);
3275 MH_ALIGN(mh, sizeof(*ip6));
3276 m->m_len -= sizeof(*ip6);
3277 m->m_data += sizeof(*ip6);
3280 m->m_len = sizeof(*ip6);
3281 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3283 exthdrs->ip6e_ip6 = m;
3288 * Compute IPv6 extension header length.
3291 ip6_optlen(struct in6pcb *in6p)
3295 if (!in6p->in6p_outputopts)
3300 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3302 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3303 if (in6p->in6p_outputopts->ip6po_rthdr)
3304 /* dest1 is valid with rthdr only */
3305 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3306 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3307 len += elen(in6p->in6p_outputopts->ip6po_dest2);