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 * 4. 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
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
68 #include <sys/param.h>
69 #include <sys/malloc.h>
72 #include <sys/errno.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
80 #include <net/netisr.h>
81 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet6/in6_var.h>
87 #include <netinet/ip6.h>
88 #include <netinet/icmp6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/tcp_var.h>
92 #include <netinet6/nd6.h>
95 #include <netinet6/ipsec.h>
97 #include <netinet6/ipsec6.h>
99 #include <netkey/key.h>
103 #include <netipsec/ipsec.h>
104 #include <netipsec/ipsec6.h>
105 #include <netipsec/key.h>
106 #endif /* FAST_IPSEC */
108 #include <netinet6/ip6protosw.h>
109 #include <netinet6/scope6_var.h>
111 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "internet multicast options");
114 struct mbuf *ip6e_ip6;
115 struct mbuf *ip6e_hbh;
116 struct mbuf *ip6e_dest1;
117 struct mbuf *ip6e_rthdr;
118 struct mbuf *ip6e_dest2;
121 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
123 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
124 struct socket *, struct sockopt *));
125 static int ip6_getpcbopt __P((struct ip6_pktopts *, int, struct sockopt *));
126 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
129 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
130 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
131 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
132 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
133 struct ip6_frag **));
134 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
135 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
136 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
137 struct ifnet *, struct in6_addr *, u_long *, int *));
138 static int copypktopts __P((struct ip6_pktopts *, struct ip6_pktopts *, int));
142 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
143 * header (with pri, len, nxt, hlim, src, dst).
144 * This function may modify ver and hlim only.
145 * The mbuf chain containing the packet will be freed.
146 * The mbuf opt, if present, will not be freed.
148 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
149 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
150 * which is rt_rmx.rmx_mtu.
153 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
155 struct ip6_pktopts *opt;
156 struct route_in6 *ro;
158 struct ip6_moptions *im6o;
159 struct ifnet **ifpp; /* XXX: just for statistics */
162 struct ip6_hdr *ip6, *mhip6;
163 struct ifnet *ifp, *origifp;
165 int hlen, tlen, len, off;
166 struct route_in6 ip6route;
167 struct rtentry *rt = NULL;
168 struct sockaddr_in6 *dst, src_sa, dst_sa;
169 struct in6_addr odst;
171 struct in6_ifaddr *ia = NULL;
173 int alwaysfrag, dontfrag;
174 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
175 struct ip6_exthdrs exthdrs;
176 struct in6_addr finaldst, src0, dst0;
178 struct route_in6 *ro_pmtu = NULL;
181 #if defined(IPSEC) || defined(FAST_IPSEC)
182 int needipsectun = 0;
183 struct secpolicy *sp = NULL;
184 #endif /*IPSEC || FAST_IPSEC*/
186 ip6 = mtod(m, struct ip6_hdr *);
187 finaldst = ip6->ip6_dst;
189 #define MAKE_EXTHDR(hp, mp) \
192 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
193 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
194 ((eh)->ip6e_len + 1) << 3); \
198 } while (/*CONSTCOND*/ 0)
200 bzero(&exthdrs, sizeof(exthdrs));
203 /* Hop-by-Hop options header */
204 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
205 /* Destination options header(1st part) */
206 if (opt->ip6po_rthdr) {
208 * Destination options header(1st part)
209 * This only makes sence with a routing header.
210 * See Section 9.2 of RFC 3542.
211 * Disabling this part just for MIP6 convenience is
212 * a bad idea. We need to think carefully about a
213 * way to make the advanced API coexist with MIP6
214 * options, which might automatically be inserted in
217 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
220 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
221 /* Destination options header(2nd part) */
222 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
226 /* get a security policy for this packet */
228 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
230 sp = ipsec6_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
233 ipsec6stat.out_inval++;
240 switch (sp->policy) {
241 case IPSEC_POLICY_DISCARD:
243 * This packet is just discarded.
245 ipsec6stat.out_polvio++;
248 case IPSEC_POLICY_BYPASS:
249 case IPSEC_POLICY_NONE:
250 /* no need to do IPsec. */
254 case IPSEC_POLICY_IPSEC:
255 if (sp->req == NULL) {
256 /* acquire a policy */
257 error = key_spdacquire(sp);
263 case IPSEC_POLICY_ENTRUST:
265 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
269 /* get a security policy for this packet */
271 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
273 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
276 newipsecstat.ips_out_inval++;
283 switch (sp->policy) {
284 case IPSEC_POLICY_DISCARD:
286 * This packet is just discarded.
288 newipsecstat.ips_out_polvio++;
291 case IPSEC_POLICY_BYPASS:
292 case IPSEC_POLICY_NONE:
293 /* no need to do IPsec. */
297 case IPSEC_POLICY_IPSEC:
298 if (sp->req == NULL) {
299 /* acquire a policy */
300 error = key_spdacquire(sp);
306 case IPSEC_POLICY_ENTRUST:
308 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
310 #endif /* FAST_IPSEC */
313 * Calculate the total length of the extension header chain.
314 * Keep the length of the unfragmentable part for fragmentation.
317 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
318 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
319 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
320 unfragpartlen = optlen + sizeof(struct ip6_hdr);
321 /* NOTE: we don't add AH/ESP length here. do that later. */
322 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
325 * If we need IPsec, or there is at least one extension header,
326 * separate IP6 header from the payload.
328 if ((needipsec || optlen) && !hdrsplit) {
329 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
333 m = exthdrs.ip6e_ip6;
338 ip6 = mtod(m, struct ip6_hdr *);
340 /* adjust mbuf packet header length */
341 m->m_pkthdr.len += optlen;
342 plen = m->m_pkthdr.len - sizeof(*ip6);
344 /* If this is a jumbo payload, insert a jumbo payload option. */
345 if (plen > IPV6_MAXPACKET) {
347 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
351 m = exthdrs.ip6e_ip6;
355 ip6 = mtod(m, struct ip6_hdr *);
356 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
360 ip6->ip6_plen = htons(plen);
363 * Concatenate headers and fill in next header fields.
364 * Here we have, on "m"
366 * and we insert headers accordingly. Finally, we should be getting:
367 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
369 * during the header composing process, "m" points to IPv6 header.
370 * "mprev" points to an extension header prior to esp.
373 u_char *nexthdrp = &ip6->ip6_nxt;
374 struct mbuf *mprev = m;
377 * we treat dest2 specially. this makes IPsec processing
378 * much easier. the goal here is to make mprev point the
379 * mbuf prior to dest2.
381 * result: IPv6 dest2 payload
382 * m and mprev will point to IPv6 header.
384 if (exthdrs.ip6e_dest2) {
386 panic("assumption failed: hdr not split");
387 exthdrs.ip6e_dest2->m_next = m->m_next;
388 m->m_next = exthdrs.ip6e_dest2;
389 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
390 ip6->ip6_nxt = IPPROTO_DSTOPTS;
393 #define MAKE_CHAIN(m, mp, p, i)\
397 panic("assumption failed: hdr not split"); \
398 *mtod((m), u_char *) = *(p);\
400 p = mtod((m), u_char *);\
401 (m)->m_next = (mp)->m_next;\
405 } while (/*CONSTCOND*/ 0)
407 * result: IPv6 hbh dest1 rthdr dest2 payload
408 * m will point to IPv6 header. mprev will point to the
409 * extension header prior to dest2 (rthdr in the above case).
411 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
412 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
414 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
417 #if defined(IPSEC) || defined(FAST_IPSEC)
422 * pointers after IPsec headers are not valid any more.
423 * other pointers need a great care too.
424 * (IPsec routines should not mangle mbufs prior to AH/ESP)
426 exthdrs.ip6e_dest2 = NULL;
429 struct ip6_rthdr *rh = NULL;
431 struct ipsec_output_state state;
433 if (exthdrs.ip6e_rthdr) {
434 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
435 segleft_org = rh->ip6r_segleft;
436 rh->ip6r_segleft = 0;
439 bzero(&state, sizeof(state));
441 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
445 /* mbuf is already reclaimed in ipsec6_output_trans. */
455 printf("ip6_output (ipsec): error code %d\n", error);
458 /* don't show these error codes to the user */
464 if (exthdrs.ip6e_rthdr) {
465 /* ah6_output doesn't modify mbuf chain */
466 rh->ip6r_segleft = segleft_org;
474 * If there is a routing header, replace the destination address field
475 * with the first hop of the routing header.
477 if (exthdrs.ip6e_rthdr) {
478 struct ip6_rthdr *rh =
479 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
480 struct ip6_rthdr *));
481 struct ip6_rthdr0 *rh0;
482 struct in6_addr *addr;
483 struct sockaddr_in6 sa;
485 switch (rh->ip6r_type) {
486 case IPV6_RTHDR_TYPE_0:
487 rh0 = (struct ip6_rthdr0 *)rh;
488 addr = (struct in6_addr *)(rh0 + 1);
491 * construct a sockaddr_in6 form of
494 * XXX: we may not have enough
495 * information about its scope zone;
496 * there is no standard API to pass
497 * the information from the
500 bzero(&sa, sizeof(sa));
501 sa.sin6_family = AF_INET6;
502 sa.sin6_len = sizeof(sa);
503 sa.sin6_addr = addr[0];
504 if ((error = sa6_embedscope(&sa,
505 ip6_use_defzone)) != 0) {
508 ip6->ip6_dst = sa.sin6_addr;
509 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr)
510 * (rh0->ip6r0_segleft - 1));
511 addr[rh0->ip6r0_segleft - 1] = finaldst;
513 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
515 default: /* is it possible? */
521 /* Source address validation */
522 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
523 (flags & IPV6_UNSPECSRC) == 0) {
525 ip6stat.ip6s_badscope++;
528 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
530 ip6stat.ip6s_badscope++;
534 ip6stat.ip6s_localout++;
541 bzero((caddr_t)ro, sizeof(*ro));
544 if (opt && opt->ip6po_rthdr)
545 ro = &opt->ip6po_route;
546 dst = (struct sockaddr_in6 *)&ro->ro_dst;
550 * if specified, try to fill in the traffic class field.
551 * do not override if a non-zero value is already set.
552 * we check the diffserv field and the ecn field separately.
554 if (opt && opt->ip6po_tclass >= 0) {
557 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
559 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
562 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
565 /* fill in or override the hop limit field, if necessary. */
566 if (opt && opt->ip6po_hlim != -1)
567 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
568 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
570 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
572 ip6->ip6_hlim = ip6_defmcasthlim;
575 #if defined(IPSEC) || defined(FAST_IPSEC)
576 if (needipsec && needipsectun) {
577 struct ipsec_output_state state;
580 * All the extension headers will become inaccessible
581 * (since they can be encrypted).
582 * Don't panic, we need no more updates to extension headers
583 * on inner IPv6 packet (since they are now encapsulated).
585 * IPv6 [ESP|AH] IPv6 [extension headers] payload
587 bzero(&exthdrs, sizeof(exthdrs));
588 exthdrs.ip6e_ip6 = m;
590 bzero(&state, sizeof(state));
592 state.ro = (struct route *)ro;
593 state.dst = (struct sockaddr *)dst;
595 error = ipsec6_output_tunnel(&state, sp, flags);
598 ro = (struct route_in6 *)state.ro;
599 dst = (struct sockaddr_in6 *)state.dst;
601 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
612 printf("ip6_output (ipsec): error code %d\n", error);
615 /* don't show these error codes to the user */
622 exthdrs.ip6e_ip6 = m;
627 ip6 = mtod(m, struct ip6_hdr *);
629 bzero(&dst_sa, sizeof(dst_sa));
630 dst_sa.sin6_family = AF_INET6;
631 dst_sa.sin6_len = sizeof(dst_sa);
632 dst_sa.sin6_addr = ip6->ip6_dst;
633 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
634 &ifp, &rt, 0)) != 0) {
637 ip6stat.ip6s_noroute++;
641 break; /* XXX statistics? */
644 in6_ifstat_inc(ifp, ifs6_out_discard);
649 * If in6_selectroute() does not return a route entry,
650 * dst may not have been updated.
652 *dst = dst_sa; /* XXX */
656 * then rt (for unicast) and ifp must be non-NULL valid values.
658 if ((flags & IPV6_FORWARDING) == 0) {
659 /* XXX: the FORWARDING flag can be set for mrouting. */
660 in6_ifstat_inc(ifp, ifs6_out_request);
663 ia = (struct in6_ifaddr *)(rt->rt_ifa);
668 * The outgoing interface must be in the zone of source and
669 * destination addresses. We should use ia_ifp to support the
670 * case of sending packets to an address of our own.
672 if (ia != NULL && ia->ia_ifp)
673 origifp = ia->ia_ifp;
678 if (in6_setscope(&src0, origifp, &zone))
680 bzero(&src_sa, sizeof(src_sa));
681 src_sa.sin6_family = AF_INET6;
682 src_sa.sin6_len = sizeof(src_sa);
683 src_sa.sin6_addr = ip6->ip6_src;
684 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
688 if (in6_setscope(&dst0, origifp, &zone))
690 /* re-initialize to be sure */
691 bzero(&dst_sa, sizeof(dst_sa));
692 dst_sa.sin6_family = AF_INET6;
693 dst_sa.sin6_len = sizeof(dst_sa);
694 dst_sa.sin6_addr = ip6->ip6_dst;
695 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
699 /* scope check is done. */
703 ip6stat.ip6s_badscope++;
704 in6_ifstat_inc(origifp, ifs6_out_discard);
706 error = EHOSTUNREACH; /* XXX */
710 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
711 if (opt && opt->ip6po_nextroute.ro_rt) {
713 * The nexthop is explicitly specified by the
714 * application. We assume the next hop is an IPv6
717 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
719 else if ((rt->rt_flags & RTF_GATEWAY))
720 dst = (struct sockaddr_in6 *)rt->rt_gateway;
723 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
724 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
726 struct in6_multi *in6m;
728 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
730 in6_ifstat_inc(ifp, ifs6_out_mcast);
733 * Confirm that the outgoing interface supports multicast.
735 if (!(ifp->if_flags & IFF_MULTICAST)) {
736 ip6stat.ip6s_noroute++;
737 in6_ifstat_inc(ifp, ifs6_out_discard);
741 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
743 (im6o == NULL || im6o->im6o_multicast_loop)) {
745 * If we belong to the destination multicast group
746 * on the outgoing interface, and the caller did not
747 * forbid loopback, loop back a copy.
749 ip6_mloopback(ifp, m, dst);
752 * If we are acting as a multicast router, perform
753 * multicast forwarding as if the packet had just
754 * arrived on the interface to which we are about
755 * to send. The multicast forwarding function
756 * recursively calls this function, using the
757 * IPV6_FORWARDING flag to prevent infinite recursion.
759 * Multicasts that are looped back by ip6_mloopback(),
760 * above, will be forwarded by the ip6_input() routine,
763 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
765 * XXX: ip6_mforward expects that rcvif is NULL
766 * when it is called from the originating path.
767 * However, it is not always the case, since
768 * some versions of MGETHDR() does not
769 * initialize the field.
771 m->m_pkthdr.rcvif = NULL;
772 if (ip6_mforward(ip6, ifp, m) != 0) {
779 * Multicasts with a hoplimit of zero may be looped back,
780 * above, but must not be transmitted on a network.
781 * Also, multicasts addressed to the loopback interface
782 * are not sent -- the above call to ip6_mloopback() will
783 * loop back a copy if this host actually belongs to the
784 * destination group on the loopback interface.
786 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
787 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
794 * Fill the outgoing inteface to tell the upper layer
795 * to increment per-interface statistics.
800 /* Determine path MTU. */
801 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
806 * The caller of this function may specify to use the minimum MTU
808 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
809 * setting. The logic is a bit complicated; by default, unicast
810 * packets will follow path MTU while multicast packets will be sent at
811 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
812 * including unicast ones will be sent at the minimum MTU. Multicast
813 * packets will always be sent at the minimum MTU unless
814 * IP6PO_MINMTU_DISABLE is explicitly specified.
815 * See RFC 3542 for more details.
817 if (mtu > IPV6_MMTU) {
818 if ((flags & IPV6_MINMTU))
820 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
822 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
824 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
830 * clear embedded scope identifiers if necessary.
831 * in6_clearscope will touch the addresses only when necessary.
833 in6_clearscope(&ip6->ip6_src);
834 in6_clearscope(&ip6->ip6_dst);
837 * If the outgoing packet contains a hop-by-hop options header,
838 * it must be examined and processed even by the source node.
839 * (RFC 2460, section 4.)
841 if (exthdrs.ip6e_hbh) {
842 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
843 u_int32_t dummy; /* XXX unused */
844 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
847 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
848 panic("ip6e_hbh is not continuous");
851 * XXX: if we have to send an ICMPv6 error to the sender,
852 * we need the M_LOOP flag since icmp6_error() expects
853 * the IPv6 and the hop-by-hop options header are
854 * continuous unless the flag is set.
856 m->m_flags |= M_LOOP;
857 m->m_pkthdr.rcvif = ifp;
858 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
859 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
860 &dummy, &plen) < 0) {
861 /* m was already freed at this point */
862 error = EINVAL;/* better error? */
865 m->m_flags &= ~M_LOOP; /* XXX */
866 m->m_pkthdr.rcvif = NULL;
869 /* Jump over all PFIL processing if hooks are not active. */
870 if (!PFIL_HOOKED(&inet6_pfil_hook))
874 /* Run through list of hooks for output packets. */
875 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
876 if (error != 0 || m == NULL)
878 ip6 = mtod(m, struct ip6_hdr *);
880 /* See if destination IP address was changed by packet filter. */
881 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
882 m->m_flags |= M_SKIP_FIREWALL;
883 /* If destination is now ourself drop to ip6_input(). */
884 if (in6_localaddr(&ip6->ip6_dst)) {
885 if (m->m_pkthdr.rcvif == NULL)
886 m->m_pkthdr.rcvif = loif;
887 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
888 m->m_pkthdr.csum_flags |=
889 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
890 m->m_pkthdr.csum_data = 0xffff;
892 m->m_pkthdr.csum_flags |=
893 CSUM_IP_CHECKED | CSUM_IP_VALID;
894 error = netisr_queue(NETISR_IPV6, m);
897 goto again; /* Redo the routing table lookup. */
900 /* XXX: IPFIREWALL_FORWARD */
904 * Send the packet to the outgoing interface.
905 * If necessary, do IPv6 fragmentation before sending.
907 * the logic here is rather complex:
908 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
909 * 1-a: send as is if tlen <= path mtu
910 * 1-b: fragment if tlen > path mtu
912 * 2: if user asks us not to fragment (dontfrag == 1)
913 * 2-a: send as is if tlen <= interface mtu
914 * 2-b: error if tlen > interface mtu
916 * 3: if we always need to attach fragment header (alwaysfrag == 1)
919 * 4: if dontfrag == 1 && alwaysfrag == 1
920 * error, as we cannot handle this conflicting request
922 tlen = m->m_pkthdr.len;
924 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
928 if (dontfrag && alwaysfrag) { /* case 4 */
929 /* conflicting request - can't transmit */
933 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
935 * Even if the DONTFRAG option is specified, we cannot send the
936 * packet when the data length is larger than the MTU of the
937 * outgoing interface.
938 * Notify the error by sending IPV6_PATHMTU ancillary data as
939 * well as returning an error code (the latter is not described
943 struct ip6ctlparam ip6cp;
945 mtu32 = (u_int32_t)mtu;
946 bzero(&ip6cp, sizeof(ip6cp));
947 ip6cp.ip6c_cmdarg = (void *)&mtu32;
948 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
956 * transmit packet without fragmentation
958 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
959 struct in6_ifaddr *ia6;
961 ip6 = mtod(m, struct ip6_hdr *);
962 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
964 /* Record statistics for this interface address. */
965 ia6->ia_ifa.if_opackets++;
966 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
969 /* clean ipsec history once it goes out of the node */
972 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
977 * try to fragment the packet. case 1-b and 3
979 if (mtu < IPV6_MMTU) {
980 /* path MTU cannot be less than IPV6_MMTU */
982 in6_ifstat_inc(ifp, ifs6_out_fragfail);
984 } else if (ip6->ip6_plen == 0) {
985 /* jumbo payload cannot be fragmented */
987 in6_ifstat_inc(ifp, ifs6_out_fragfail);
990 struct mbuf **mnext, *m_frgpart;
991 struct ip6_frag *ip6f;
992 u_int32_t id = htonl(ip6_randomid());
995 struct ip6ctlparam ip6cp;
998 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1001 * Too large for the destination or interface;
1002 * fragment if possible.
1003 * Must be able to put at least 8 bytes per fragment.
1005 hlen = unfragpartlen;
1006 if (mtu > IPV6_MAXPACKET)
1007 mtu = IPV6_MAXPACKET;
1011 * It is believed this code is a leftover from the
1012 * development of the IPV6_RECVPATHMTU sockopt and
1013 * associated work to implement RFC3542.
1014 * It's not entirely clear what the intent of the API
1015 * is at this point, so disable this code for now.
1016 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1017 * will send notifications if the application requests.
1020 /* Notify a proper path MTU to applications. */
1021 mtu32 = (u_int32_t)mtu;
1022 bzero(&ip6cp, sizeof(ip6cp));
1023 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1024 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1028 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1031 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1036 * Verify that we have any chance at all of being able to queue
1037 * the packet or packet fragments
1039 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1040 < tlen /* - hlen */)) {
1042 ip6stat.ip6s_odropped++;
1046 mnext = &m->m_nextpkt;
1049 * Change the next header field of the last header in the
1050 * unfragmentable part.
1052 if (exthdrs.ip6e_rthdr) {
1053 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1054 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1055 } else if (exthdrs.ip6e_dest1) {
1056 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1057 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1058 } else if (exthdrs.ip6e_hbh) {
1059 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1060 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1062 nextproto = ip6->ip6_nxt;
1063 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1067 * Loop through length of segment after first fragment,
1068 * make new header and copy data of each part and link onto
1072 for (off = hlen; off < tlen; off += len) {
1073 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1076 ip6stat.ip6s_odropped++;
1079 m->m_pkthdr.rcvif = NULL;
1080 m->m_flags = m0->m_flags & M_COPYFLAGS;
1082 mnext = &m->m_nextpkt;
1083 m->m_data += max_linkhdr;
1084 mhip6 = mtod(m, struct ip6_hdr *);
1086 m->m_len = sizeof(*mhip6);
1087 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1089 ip6stat.ip6s_odropped++;
1092 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1093 if (off + len >= tlen)
1096 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1097 mhip6->ip6_plen = htons((u_short)(len + hlen +
1098 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1099 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1101 ip6stat.ip6s_odropped++;
1104 m_cat(m, m_frgpart);
1105 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1106 m->m_pkthdr.rcvif = NULL;
1107 ip6f->ip6f_reserved = 0;
1108 ip6f->ip6f_ident = id;
1109 ip6f->ip6f_nxt = nextproto;
1110 ip6stat.ip6s_ofragments++;
1111 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1114 in6_ifstat_inc(ifp, ifs6_out_fragok);
1118 * Remove leading garbages.
1124 for (m0 = m; m; m = m0) {
1128 /* Record statistics for this interface address. */
1130 ia->ia_ifa.if_opackets++;
1131 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1134 /* clean ipsec history once it goes out of the node */
1137 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1143 ip6stat.ip6s_fragmented++;
1146 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1148 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1149 RTFREE(ro_pmtu->ro_rt);
1159 #endif /* FAST_IPSEC */
1164 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1165 m_freem(exthdrs.ip6e_dest1);
1166 m_freem(exthdrs.ip6e_rthdr);
1167 m_freem(exthdrs.ip6e_dest2);
1175 ip6_copyexthdr(mp, hdr, hlen)
1182 if (hlen > MCLBYTES)
1183 return (ENOBUFS); /* XXX */
1185 MGET(m, M_DONTWAIT, MT_DATA);
1190 MCLGET(m, M_DONTWAIT);
1191 if ((m->m_flags & M_EXT) == 0) {
1198 bcopy(hdr, mtod(m, caddr_t), hlen);
1205 * Insert jumbo payload option.
1208 ip6_insert_jumboopt(exthdrs, plen)
1209 struct ip6_exthdrs *exthdrs;
1216 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1219 * If there is no hop-by-hop options header, allocate new one.
1220 * If there is one but it doesn't have enough space to store the
1221 * jumbo payload option, allocate a cluster to store the whole options.
1222 * Otherwise, use it to store the options.
1224 if (exthdrs->ip6e_hbh == 0) {
1225 MGET(mopt, M_DONTWAIT, MT_DATA);
1228 mopt->m_len = JUMBOOPTLEN;
1229 optbuf = mtod(mopt, u_char *);
1230 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1231 exthdrs->ip6e_hbh = mopt;
1233 struct ip6_hbh *hbh;
1235 mopt = exthdrs->ip6e_hbh;
1236 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1239 * - exthdrs->ip6e_hbh is not referenced from places
1240 * other than exthdrs.
1241 * - exthdrs->ip6e_hbh is not an mbuf chain.
1243 int oldoptlen = mopt->m_len;
1247 * XXX: give up if the whole (new) hbh header does
1248 * not fit even in an mbuf cluster.
1250 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1254 * As a consequence, we must always prepare a cluster
1257 MGET(n, M_DONTWAIT, MT_DATA);
1259 MCLGET(n, M_DONTWAIT);
1260 if ((n->m_flags & M_EXT) == 0) {
1267 n->m_len = oldoptlen + JUMBOOPTLEN;
1268 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1270 optbuf = mtod(n, caddr_t) + oldoptlen;
1272 mopt = exthdrs->ip6e_hbh = n;
1274 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1275 mopt->m_len += JUMBOOPTLEN;
1277 optbuf[0] = IP6OPT_PADN;
1281 * Adjust the header length according to the pad and
1282 * the jumbo payload option.
1284 hbh = mtod(mopt, struct ip6_hbh *);
1285 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1288 /* fill in the option. */
1289 optbuf[2] = IP6OPT_JUMBO;
1291 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1292 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1294 /* finally, adjust the packet header length */
1295 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1302 * Insert fragment header and copy unfragmentable header portions.
1305 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1306 struct mbuf *m0, *m;
1308 struct ip6_frag **frghdrp;
1310 struct mbuf *n, *mlast;
1312 if (hlen > sizeof(struct ip6_hdr)) {
1313 n = m_copym(m0, sizeof(struct ip6_hdr),
1314 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1321 /* Search for the last mbuf of unfragmentable part. */
1322 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1325 if ((mlast->m_flags & M_EXT) == 0 &&
1326 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1327 /* use the trailing space of the last mbuf for the fragment hdr */
1328 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1330 mlast->m_len += sizeof(struct ip6_frag);
1331 m->m_pkthdr.len += sizeof(struct ip6_frag);
1333 /* allocate a new mbuf for the fragment header */
1336 MGET(mfrg, M_DONTWAIT, MT_DATA);
1339 mfrg->m_len = sizeof(struct ip6_frag);
1340 *frghdrp = mtod(mfrg, struct ip6_frag *);
1341 mlast->m_next = mfrg;
1348 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1349 struct route_in6 *ro_pmtu, *ro;
1351 struct in6_addr *dst;
1359 if (ro_pmtu != ro) {
1360 /* The first hop and the final destination may differ. */
1361 struct sockaddr_in6 *sa6_dst =
1362 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1363 if (ro_pmtu->ro_rt &&
1364 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1365 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1366 RTFREE(ro_pmtu->ro_rt);
1367 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1369 if (ro_pmtu->ro_rt == NULL) {
1370 bzero(sa6_dst, sizeof(*sa6_dst));
1371 sa6_dst->sin6_family = AF_INET6;
1372 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1373 sa6_dst->sin6_addr = *dst;
1375 rtalloc((struct route *)ro_pmtu);
1378 if (ro_pmtu->ro_rt) {
1380 struct in_conninfo inc;
1382 bzero(&inc, sizeof(inc));
1383 inc.inc_flags = 1; /* IPv6 */
1384 inc.inc6_faddr = *dst;
1387 ifp = ro_pmtu->ro_rt->rt_ifp;
1388 ifmtu = IN6_LINKMTU(ifp);
1389 mtu = tcp_hc_getmtu(&inc);
1391 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1393 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1396 else if (mtu < IPV6_MMTU) {
1398 * RFC2460 section 5, last paragraph:
1399 * if we record ICMPv6 too big message with
1400 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1401 * or smaller, with framgent header attached.
1402 * (fragment header is needed regardless from the
1403 * packet size, for translators to identify packets)
1407 } else if (mtu > ifmtu) {
1409 * The MTU on the route is larger than the MTU on
1410 * the interface! This shouldn't happen, unless the
1411 * MTU of the interface has been changed after the
1412 * interface was brought up. Change the MTU in the
1413 * route to match the interface MTU (as long as the
1414 * field isn't locked).
1417 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1420 mtu = IN6_LINKMTU(ifp);
1422 error = EHOSTUNREACH; /* XXX */
1426 *alwaysfragp = alwaysfrag;
1431 * IP6 socket option processing.
1434 ip6_ctloutput(so, sopt)
1436 struct sockopt *sopt;
1438 int privileged, optdatalen, uproto;
1440 struct inpcb *in6p = sotoinpcb(so);
1442 int level, op, optname;
1447 level = sopt->sopt_level;
1448 op = sopt->sopt_dir;
1449 optname = sopt->sopt_name;
1450 optlen = sopt->sopt_valsize;
1453 panic("ip6_ctloutput: arg soopt is NULL");
1457 privileged = (td == 0 || suser(td)) ? 0 : 1;
1458 uproto = (int)so->so_proto->pr_protocol;
1460 if (level == IPPROTO_IPV6) {
1465 case IPV6_2292PKTOPTIONS:
1466 #ifdef IPV6_PKTOPTIONS
1467 case IPV6_PKTOPTIONS:
1472 error = soopt_getm(sopt, &m); /* XXX */
1475 error = soopt_mcopyin(sopt, m); /* XXX */
1478 error = ip6_pcbopts(&in6p->in6p_outputopts,
1480 m_freem(m); /* XXX */
1485 * Use of some Hop-by-Hop options or some
1486 * Destination options, might require special
1487 * privilege. That is, normal applications
1488 * (without special privilege) might be forbidden
1489 * from setting certain options in outgoing packets,
1490 * and might never see certain options in received
1491 * packets. [RFC 2292 Section 6]
1492 * KAME specific note:
1493 * KAME prevents non-privileged users from sending or
1494 * receiving ANY hbh/dst options in order to avoid
1495 * overhead of parsing options in the kernel.
1497 case IPV6_RECVHOPOPTS:
1498 case IPV6_RECVDSTOPTS:
1499 case IPV6_RECVRTHDRDSTOPTS:
1505 case IPV6_UNICAST_HOPS:
1509 case IPV6_RECVPKTINFO:
1510 case IPV6_RECVHOPLIMIT:
1511 case IPV6_RECVRTHDR:
1512 case IPV6_RECVPATHMTU:
1513 case IPV6_RECVTCLASS:
1515 case IPV6_AUTOFLOWLABEL:
1516 if (optlen != sizeof(int)) {
1520 error = sooptcopyin(sopt, &optval,
1521 sizeof optval, sizeof optval);
1526 case IPV6_UNICAST_HOPS:
1527 if (optval < -1 || optval >= 256)
1530 /* -1 = kernel default */
1531 in6p->in6p_hops = optval;
1532 if ((in6p->in6p_vflag &
1534 in6p->inp_ip_ttl = optval;
1537 #define OPTSET(bit) \
1540 in6p->in6p_flags |= (bit); \
1542 in6p->in6p_flags &= ~(bit); \
1543 } while (/*CONSTCOND*/ 0)
1544 #define OPTSET2292(bit) \
1546 in6p->in6p_flags |= IN6P_RFC2292; \
1548 in6p->in6p_flags |= (bit); \
1550 in6p->in6p_flags &= ~(bit); \
1551 } while (/*CONSTCOND*/ 0)
1552 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1554 case IPV6_RECVPKTINFO:
1555 /* cannot mix with RFC2292 */
1556 if (OPTBIT(IN6P_RFC2292)) {
1560 OPTSET(IN6P_PKTINFO);
1565 struct ip6_pktopts **optp;
1567 /* cannot mix with RFC2292 */
1568 if (OPTBIT(IN6P_RFC2292)) {
1572 optp = &in6p->in6p_outputopts;
1573 error = ip6_pcbopt(IPV6_HOPLIMIT,
1577 privileged, uproto);
1581 case IPV6_RECVHOPLIMIT:
1582 /* cannot mix with RFC2292 */
1583 if (OPTBIT(IN6P_RFC2292)) {
1587 OPTSET(IN6P_HOPLIMIT);
1590 case IPV6_RECVHOPOPTS:
1591 /* cannot mix with RFC2292 */
1592 if (OPTBIT(IN6P_RFC2292)) {
1596 OPTSET(IN6P_HOPOPTS);
1599 case IPV6_RECVDSTOPTS:
1600 /* cannot mix with RFC2292 */
1601 if (OPTBIT(IN6P_RFC2292)) {
1605 OPTSET(IN6P_DSTOPTS);
1608 case IPV6_RECVRTHDRDSTOPTS:
1609 /* cannot mix with RFC2292 */
1610 if (OPTBIT(IN6P_RFC2292)) {
1614 OPTSET(IN6P_RTHDRDSTOPTS);
1617 case IPV6_RECVRTHDR:
1618 /* cannot mix with RFC2292 */
1619 if (OPTBIT(IN6P_RFC2292)) {
1630 case IPV6_RECVPATHMTU:
1632 * We ignore this option for TCP
1634 * (RFC3542 leaves this case
1637 if (uproto != IPPROTO_TCP)
1643 * make setsockopt(IPV6_V6ONLY)
1644 * available only prior to bind(2).
1645 * see ipng mailing list, Jun 22 2001.
1647 if (in6p->in6p_lport ||
1648 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1652 OPTSET(IN6P_IPV6_V6ONLY);
1654 in6p->in6p_vflag &= ~INP_IPV4;
1656 in6p->in6p_vflag |= INP_IPV4;
1658 case IPV6_RECVTCLASS:
1659 /* cannot mix with RFC2292 XXX */
1660 if (OPTBIT(IN6P_RFC2292)) {
1664 OPTSET(IN6P_TCLASS);
1666 case IPV6_AUTOFLOWLABEL:
1667 OPTSET(IN6P_AUTOFLOWLABEL);
1675 case IPV6_USE_MIN_MTU:
1676 case IPV6_PREFER_TEMPADDR:
1677 if (optlen != sizeof(optval)) {
1681 error = sooptcopyin(sopt, &optval,
1682 sizeof optval, sizeof optval);
1686 struct ip6_pktopts **optp;
1687 optp = &in6p->in6p_outputopts;
1688 error = ip6_pcbopt(optname,
1692 privileged, uproto);
1696 case IPV6_2292PKTINFO:
1697 case IPV6_2292HOPLIMIT:
1698 case IPV6_2292HOPOPTS:
1699 case IPV6_2292DSTOPTS:
1700 case IPV6_2292RTHDR:
1702 if (optlen != sizeof(int)) {
1706 error = sooptcopyin(sopt, &optval,
1707 sizeof optval, sizeof optval);
1711 case IPV6_2292PKTINFO:
1712 OPTSET2292(IN6P_PKTINFO);
1714 case IPV6_2292HOPLIMIT:
1715 OPTSET2292(IN6P_HOPLIMIT);
1717 case IPV6_2292HOPOPTS:
1719 * Check super-user privilege.
1720 * See comments for IPV6_RECVHOPOPTS.
1724 OPTSET2292(IN6P_HOPOPTS);
1726 case IPV6_2292DSTOPTS:
1729 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1731 case IPV6_2292RTHDR:
1732 OPTSET2292(IN6P_RTHDR);
1740 case IPV6_RTHDRDSTOPTS:
1743 /* new advanced API (RFC3542) */
1745 u_char optbuf_storage[MCLBYTES];
1747 struct ip6_pktopts **optp;
1749 /* cannot mix with RFC2292 */
1750 if (OPTBIT(IN6P_RFC2292)) {
1756 * We only ensure valsize is not too large
1757 * here. Further validation will be done
1760 error = sooptcopyin(sopt, optbuf_storage,
1761 sizeof(optbuf_storage), 0);
1764 optlen = sopt->sopt_valsize;
1765 optbuf = optbuf_storage;
1766 optp = &in6p->in6p_outputopts;
1767 error = ip6_pcbopt(optname,
1769 optp, privileged, uproto);
1774 case IPV6_MULTICAST_IF:
1775 case IPV6_MULTICAST_HOPS:
1776 case IPV6_MULTICAST_LOOP:
1777 case IPV6_JOIN_GROUP:
1778 case IPV6_LEAVE_GROUP:
1780 if (sopt->sopt_valsize > MLEN) {
1790 if (sopt->sopt_valsize > MCLBYTES) {
1795 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1800 if (sopt->sopt_valsize > MLEN) {
1801 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1802 if ((m->m_flags & M_EXT) == 0) {
1808 m->m_len = sopt->sopt_valsize;
1809 error = sooptcopyin(sopt, mtod(m, char *),
1810 m->m_len, m->m_len);
1815 error = ip6_setmoptions(sopt->sopt_name,
1816 &in6p->in6p_moptions,
1822 case IPV6_PORTRANGE:
1823 error = sooptcopyin(sopt, &optval,
1824 sizeof optval, sizeof optval);
1829 case IPV6_PORTRANGE_DEFAULT:
1830 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1831 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1834 case IPV6_PORTRANGE_HIGH:
1835 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1836 in6p->in6p_flags |= IN6P_HIGHPORT;
1839 case IPV6_PORTRANGE_LOW:
1840 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1841 in6p->in6p_flags |= IN6P_LOWPORT;
1850 #if defined(IPSEC) || defined(FAST_IPSEC)
1851 case IPV6_IPSEC_POLICY:
1857 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1859 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1862 req = mtod(m, caddr_t);
1865 error = ipsec6_set_policy(in6p, optname, req,
1870 #endif /* KAME IPSEC */
1873 error = ENOPROTOOPT;
1881 case IPV6_2292PKTOPTIONS:
1882 #ifdef IPV6_PKTOPTIONS
1883 case IPV6_PKTOPTIONS:
1886 * RFC3542 (effectively) deprecated the
1887 * semantics of the 2292-style pktoptions.
1888 * Since it was not reliable in nature (i.e.,
1889 * applications had to expect the lack of some
1890 * information after all), it would make sense
1891 * to simplify this part by always returning
1894 sopt->sopt_valsize = 0;
1897 case IPV6_RECVHOPOPTS:
1898 case IPV6_RECVDSTOPTS:
1899 case IPV6_RECVRTHDRDSTOPTS:
1900 case IPV6_UNICAST_HOPS:
1901 case IPV6_RECVPKTINFO:
1902 case IPV6_RECVHOPLIMIT:
1903 case IPV6_RECVRTHDR:
1904 case IPV6_RECVPATHMTU:
1908 case IPV6_PORTRANGE:
1909 case IPV6_RECVTCLASS:
1910 case IPV6_AUTOFLOWLABEL:
1913 case IPV6_RECVHOPOPTS:
1914 optval = OPTBIT(IN6P_HOPOPTS);
1917 case IPV6_RECVDSTOPTS:
1918 optval = OPTBIT(IN6P_DSTOPTS);
1921 case IPV6_RECVRTHDRDSTOPTS:
1922 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1925 case IPV6_UNICAST_HOPS:
1926 optval = in6p->in6p_hops;
1929 case IPV6_RECVPKTINFO:
1930 optval = OPTBIT(IN6P_PKTINFO);
1933 case IPV6_RECVHOPLIMIT:
1934 optval = OPTBIT(IN6P_HOPLIMIT);
1937 case IPV6_RECVRTHDR:
1938 optval = OPTBIT(IN6P_RTHDR);
1941 case IPV6_RECVPATHMTU:
1942 optval = OPTBIT(IN6P_MTU);
1946 optval = OPTBIT(IN6P_FAITH);
1950 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1953 case IPV6_PORTRANGE:
1956 flags = in6p->in6p_flags;
1957 if (flags & IN6P_HIGHPORT)
1958 optval = IPV6_PORTRANGE_HIGH;
1959 else if (flags & IN6P_LOWPORT)
1960 optval = IPV6_PORTRANGE_LOW;
1965 case IPV6_RECVTCLASS:
1966 optval = OPTBIT(IN6P_TCLASS);
1969 case IPV6_AUTOFLOWLABEL:
1970 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1975 error = sooptcopyout(sopt, &optval,
1982 struct ip6_mtuinfo mtuinfo;
1983 struct route_in6 sro;
1985 bzero(&sro, sizeof(sro));
1987 if (!(so->so_state & SS_ISCONNECTED))
1990 * XXX: we dot not consider the case of source
1991 * routing, or optional information to specify
1992 * the outgoing interface.
1994 error = ip6_getpmtu(&sro, NULL, NULL,
1995 &in6p->in6p_faddr, &pmtu, NULL);
2000 if (pmtu > IPV6_MAXPACKET)
2001 pmtu = IPV6_MAXPACKET;
2003 bzero(&mtuinfo, sizeof(mtuinfo));
2004 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2005 optdata = (void *)&mtuinfo;
2006 optdatalen = sizeof(mtuinfo);
2007 error = sooptcopyout(sopt, optdata,
2012 case IPV6_2292PKTINFO:
2013 case IPV6_2292HOPLIMIT:
2014 case IPV6_2292HOPOPTS:
2015 case IPV6_2292RTHDR:
2016 case IPV6_2292DSTOPTS:
2018 case IPV6_2292PKTINFO:
2019 optval = OPTBIT(IN6P_PKTINFO);
2021 case IPV6_2292HOPLIMIT:
2022 optval = OPTBIT(IN6P_HOPLIMIT);
2024 case IPV6_2292HOPOPTS:
2025 optval = OPTBIT(IN6P_HOPOPTS);
2027 case IPV6_2292RTHDR:
2028 optval = OPTBIT(IN6P_RTHDR);
2030 case IPV6_2292DSTOPTS:
2031 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2034 error = sooptcopyout(sopt, &optval,
2041 case IPV6_RTHDRDSTOPTS:
2045 case IPV6_USE_MIN_MTU:
2046 case IPV6_PREFER_TEMPADDR:
2047 error = ip6_getpcbopt(in6p->in6p_outputopts,
2051 case IPV6_MULTICAST_IF:
2052 case IPV6_MULTICAST_HOPS:
2053 case IPV6_MULTICAST_LOOP:
2054 case IPV6_JOIN_GROUP:
2055 case IPV6_LEAVE_GROUP:
2058 error = ip6_getmoptions(sopt->sopt_name,
2059 in6p->in6p_moptions, &m);
2061 error = sooptcopyout(sopt,
2062 mtod(m, char *), m->m_len);
2067 #if defined(IPSEC) || defined(FAST_IPSEC)
2068 case IPV6_IPSEC_POLICY:
2072 struct mbuf *m = NULL;
2073 struct mbuf **mp = &m;
2074 size_t ovalsize = sopt->sopt_valsize;
2075 caddr_t oval = (caddr_t)sopt->sopt_val;
2077 error = soopt_getm(sopt, &m); /* XXX */
2080 error = soopt_mcopyin(sopt, m); /* XXX */
2083 sopt->sopt_valsize = ovalsize;
2084 sopt->sopt_val = oval;
2086 req = mtod(m, caddr_t);
2089 error = ipsec6_get_policy(in6p, req, len, mp);
2091 error = soopt_mcopyout(sopt, m); /* XXX */
2092 if (error == 0 && m)
2096 #endif /* KAME IPSEC */
2099 error = ENOPROTOOPT;
2104 } else { /* level != IPPROTO_IPV6 */
2111 ip6_raw_ctloutput(so, sopt)
2113 struct sockopt *sopt;
2115 int error = 0, optval, optlen;
2116 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2117 struct in6pcb *in6p = sotoin6pcb(so);
2118 int level, op, optname;
2121 level = sopt->sopt_level;
2122 op = sopt->sopt_dir;
2123 optname = sopt->sopt_name;
2124 optlen = sopt->sopt_valsize;
2126 panic("ip6_raw_ctloutput: arg soopt is NULL");
2128 if (level != IPPROTO_IPV6) {
2135 * For ICMPv6 sockets, no modification allowed for checksum
2136 * offset, permit "no change" values to help existing apps.
2138 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2139 * for an ICMPv6 socket will fail."
2140 * The current behavior does not meet RFC3542.
2144 if (optlen != sizeof(int)) {
2148 error = sooptcopyin(sopt, &optval, sizeof(optval),
2152 if ((optval % 2) != 0) {
2153 /* the API assumes even offset values */
2155 } else if (so->so_proto->pr_protocol ==
2157 if (optval != icmp6off)
2160 in6p->in6p_cksum = optval;
2164 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2167 optval = in6p->in6p_cksum;
2169 error = sooptcopyout(sopt, &optval, sizeof(optval));
2179 error = ENOPROTOOPT;
2187 * Set up IP6 options in pcb for insertion in output packets or
2188 * specifying behavior of outgoing packets.
2191 ip6_pcbopts(pktopt, m, so, sopt)
2192 struct ip6_pktopts **pktopt;
2195 struct sockopt *sopt;
2197 struct ip6_pktopts *opt = *pktopt;
2199 struct thread *td = sopt->sopt_td;
2202 /* turn off any old options. */
2205 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2206 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2207 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2208 printf("ip6_pcbopts: all specified options are cleared.\n");
2210 ip6_clearpktopts(opt, -1);
2212 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2215 if (!m || m->m_len == 0) {
2217 * Only turning off any previous options, regardless of
2218 * whether the opt is just created or given.
2220 free(opt, M_IP6OPT);
2224 /* set options specified by user. */
2225 if (td && !suser(td))
2227 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2228 so->so_proto->pr_protocol)) != 0) {
2229 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2230 free(opt, M_IP6OPT);
2238 * initialize ip6_pktopts. beware that there are non-zero default values in
2242 ip6_initpktopts(opt)
2243 struct ip6_pktopts *opt;
2246 bzero(opt, sizeof(*opt));
2247 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2248 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2249 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2250 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2254 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2255 int optname, len, priv;
2257 struct ip6_pktopts **pktopt;
2260 struct ip6_pktopts *opt;
2262 if (*pktopt == NULL) {
2263 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2265 ip6_initpktopts(*pktopt);
2269 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2273 ip6_getpcbopt(pktopt, optname, sopt)
2274 struct ip6_pktopts *pktopt;
2275 struct sockopt *sopt;
2278 void *optdata = NULL;
2280 struct ip6_ext *ip6e;
2282 struct in6_pktinfo null_pktinfo;
2283 int deftclass = 0, on;
2284 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2285 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2289 if (pktopt && pktopt->ip6po_pktinfo)
2290 optdata = (void *)pktopt->ip6po_pktinfo;
2292 /* XXX: we don't have to do this every time... */
2293 bzero(&null_pktinfo, sizeof(null_pktinfo));
2294 optdata = (void *)&null_pktinfo;
2296 optdatalen = sizeof(struct in6_pktinfo);
2299 if (pktopt && pktopt->ip6po_tclass >= 0)
2300 optdata = (void *)&pktopt->ip6po_tclass;
2302 optdata = (void *)&deftclass;
2303 optdatalen = sizeof(int);
2306 if (pktopt && pktopt->ip6po_hbh) {
2307 optdata = (void *)pktopt->ip6po_hbh;
2308 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2309 optdatalen = (ip6e->ip6e_len + 1) << 3;
2313 if (pktopt && pktopt->ip6po_rthdr) {
2314 optdata = (void *)pktopt->ip6po_rthdr;
2315 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2316 optdatalen = (ip6e->ip6e_len + 1) << 3;
2319 case IPV6_RTHDRDSTOPTS:
2320 if (pktopt && pktopt->ip6po_dest1) {
2321 optdata = (void *)pktopt->ip6po_dest1;
2322 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2323 optdatalen = (ip6e->ip6e_len + 1) << 3;
2327 if (pktopt && pktopt->ip6po_dest2) {
2328 optdata = (void *)pktopt->ip6po_dest2;
2329 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2330 optdatalen = (ip6e->ip6e_len + 1) << 3;
2334 if (pktopt && pktopt->ip6po_nexthop) {
2335 optdata = (void *)pktopt->ip6po_nexthop;
2336 optdatalen = pktopt->ip6po_nexthop->sa_len;
2339 case IPV6_USE_MIN_MTU:
2341 optdata = (void *)&pktopt->ip6po_minmtu;
2343 optdata = (void *)&defminmtu;
2344 optdatalen = sizeof(int);
2347 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2351 optdata = (void *)&on;
2352 optdatalen = sizeof(on);
2354 case IPV6_PREFER_TEMPADDR:
2356 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2358 optdata = (void *)&defpreftemp;
2359 optdatalen = sizeof(int);
2361 default: /* should not happen */
2363 panic("ip6_getpcbopt: unexpected option\n");
2365 return (ENOPROTOOPT);
2368 error = sooptcopyout(sopt, optdata, optdatalen);
2374 ip6_clearpktopts(pktopt, optname)
2375 struct ip6_pktopts *pktopt;
2381 if (optname == -1 || optname == IPV6_PKTINFO) {
2382 if (pktopt->ip6po_pktinfo)
2383 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2384 pktopt->ip6po_pktinfo = NULL;
2386 if (optname == -1 || optname == IPV6_HOPLIMIT)
2387 pktopt->ip6po_hlim = -1;
2388 if (optname == -1 || optname == IPV6_TCLASS)
2389 pktopt->ip6po_tclass = -1;
2390 if (optname == -1 || optname == IPV6_NEXTHOP) {
2391 if (pktopt->ip6po_nextroute.ro_rt) {
2392 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2393 pktopt->ip6po_nextroute.ro_rt = NULL;
2395 if (pktopt->ip6po_nexthop)
2396 free(pktopt->ip6po_nexthop, M_IP6OPT);
2397 pktopt->ip6po_nexthop = NULL;
2399 if (optname == -1 || optname == IPV6_HOPOPTS) {
2400 if (pktopt->ip6po_hbh)
2401 free(pktopt->ip6po_hbh, M_IP6OPT);
2402 pktopt->ip6po_hbh = NULL;
2404 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2405 if (pktopt->ip6po_dest1)
2406 free(pktopt->ip6po_dest1, M_IP6OPT);
2407 pktopt->ip6po_dest1 = NULL;
2409 if (optname == -1 || optname == IPV6_RTHDR) {
2410 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2411 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2412 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2413 if (pktopt->ip6po_route.ro_rt) {
2414 RTFREE(pktopt->ip6po_route.ro_rt);
2415 pktopt->ip6po_route.ro_rt = NULL;
2418 if (optname == -1 || optname == IPV6_DSTOPTS) {
2419 if (pktopt->ip6po_dest2)
2420 free(pktopt->ip6po_dest2, M_IP6OPT);
2421 pktopt->ip6po_dest2 = NULL;
2425 #define PKTOPT_EXTHDRCPY(type) \
2428 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2429 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2430 if (dst->type == NULL && canwait == M_NOWAIT)\
2432 bcopy(src->type, dst->type, hlen);\
2434 } while (/*CONSTCOND*/ 0)
2437 copypktopts(dst, src, canwait)
2438 struct ip6_pktopts *dst, *src;
2441 if (dst == NULL || src == NULL) {
2442 printf("ip6_clearpktopts: invalid argument\n");
2446 dst->ip6po_hlim = src->ip6po_hlim;
2447 dst->ip6po_tclass = src->ip6po_tclass;
2448 dst->ip6po_flags = src->ip6po_flags;
2449 if (src->ip6po_pktinfo) {
2450 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2452 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2454 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2456 if (src->ip6po_nexthop) {
2457 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2459 if (dst->ip6po_nexthop == NULL)
2461 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2462 src->ip6po_nexthop->sa_len);
2464 PKTOPT_EXTHDRCPY(ip6po_hbh);
2465 PKTOPT_EXTHDRCPY(ip6po_dest1);
2466 PKTOPT_EXTHDRCPY(ip6po_dest2);
2467 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2471 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2472 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2473 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2474 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2475 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2476 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2479 #undef PKTOPT_EXTHDRCPY
2481 struct ip6_pktopts *
2482 ip6_copypktopts(src, canwait)
2483 struct ip6_pktopts *src;
2487 struct ip6_pktopts *dst;
2489 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2490 if (dst == NULL && canwait == M_NOWAIT)
2492 ip6_initpktopts(dst);
2494 if ((error = copypktopts(dst, src, canwait)) != 0) {
2495 free(dst, M_IP6OPT);
2503 ip6_freepcbopts(pktopt)
2504 struct ip6_pktopts *pktopt;
2509 ip6_clearpktopts(pktopt, -1);
2511 free(pktopt, M_IP6OPT);
2515 * Set the IP6 multicast options in response to user setsockopt().
2518 ip6_setmoptions(optname, im6op, m)
2520 struct ip6_moptions **im6op;
2524 u_int loop, ifindex;
2525 struct ipv6_mreq *mreq;
2527 struct ip6_moptions *im6o = *im6op;
2528 struct route_in6 ro;
2529 struct in6_multi_mship *imm;
2530 struct thread *td = curthread;
2534 * No multicast option buffer attached to the pcb;
2535 * allocate one and initialize to default values.
2537 im6o = (struct ip6_moptions *)
2538 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK);
2543 im6o->im6o_multicast_ifp = NULL;
2544 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2545 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2546 LIST_INIT(&im6o->im6o_memberships);
2551 case IPV6_MULTICAST_IF:
2553 * Select the interface for outgoing multicast packets.
2555 if (m == NULL || m->m_len != sizeof(u_int)) {
2559 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2560 if (ifindex < 0 || if_index < ifindex) {
2561 error = ENXIO; /* XXX EINVAL? */
2564 ifp = ifnet_byindex(ifindex);
2565 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2566 error = EADDRNOTAVAIL;
2569 im6o->im6o_multicast_ifp = ifp;
2572 case IPV6_MULTICAST_HOPS:
2575 * Set the IP6 hoplimit for outgoing multicast packets.
2578 if (m == NULL || m->m_len != sizeof(int)) {
2582 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2583 if (optval < -1 || optval >= 256)
2585 else if (optval == -1)
2586 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2588 im6o->im6o_multicast_hlim = optval;
2592 case IPV6_MULTICAST_LOOP:
2594 * Set the loopback flag for outgoing multicast packets.
2595 * Must be zero or one.
2597 if (m == NULL || m->m_len != sizeof(u_int)) {
2601 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2606 im6o->im6o_multicast_loop = loop;
2609 case IPV6_JOIN_GROUP:
2611 * Add a multicast group membership.
2612 * Group must be a valid IP6 multicast address.
2614 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2618 mreq = mtod(m, struct ipv6_mreq *);
2620 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2622 * We use the unspecified address to specify to accept
2623 * all multicast addresses. Only super user is allowed
2630 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2636 * If no interface was explicitly specified, choose an
2637 * appropriate one according to the given multicast address.
2639 if (mreq->ipv6mr_interface == 0) {
2640 struct sockaddr_in6 *dst;
2643 * Look up the routing table for the
2644 * address, and choose the outgoing interface.
2645 * XXX: is it a good approach?
2648 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2649 bzero(dst, sizeof(*dst));
2650 dst->sin6_family = AF_INET6;
2651 dst->sin6_len = sizeof(*dst);
2652 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2653 rtalloc((struct route *)&ro);
2654 if (ro.ro_rt == NULL) {
2655 error = EADDRNOTAVAIL;
2658 ifp = ro.ro_rt->rt_ifp;
2662 * If the interface is specified, validate it.
2664 if (mreq->ipv6mr_interface < 0 ||
2665 if_index < mreq->ipv6mr_interface) {
2666 error = ENXIO; /* XXX EINVAL? */
2669 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2671 error = ENXIO; /* XXX EINVAL? */
2677 * See if we found an interface, and confirm that it
2678 * supports multicast
2680 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2681 error = EADDRNOTAVAIL;
2685 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2686 error = EADDRNOTAVAIL; /* XXX: should not happen */
2691 * See if the membership already exists.
2693 for (imm = im6o->im6o_memberships.lh_first;
2694 imm != NULL; imm = imm->i6mm_chain.le_next)
2695 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2696 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2697 &mreq->ipv6mr_multiaddr))
2704 * Everything looks good; add a new record to the multicast
2705 * address list for the given interface.
2707 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2710 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2713 case IPV6_LEAVE_GROUP:
2715 * Drop a multicast group membership.
2716 * Group must be a valid IP6 multicast address.
2718 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2722 mreq = mtod(m, struct ipv6_mreq *);
2725 * If an interface address was specified, get a pointer
2726 * to its ifnet structure.
2728 if (mreq->ipv6mr_interface < 0 ||
2729 if_index < mreq->ipv6mr_interface) {
2730 error = ENXIO; /* XXX EINVAL? */
2733 if (mreq->ipv6mr_interface == 0)
2736 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2738 /* Fill in the scope zone ID */
2740 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2741 /* XXX: should not happen */
2742 error = EADDRNOTAVAIL;
2745 } else if (mreq->ipv6mr_interface != 0) {
2747 * This case happens when the (positive) index is in
2748 * the valid range, but the corresponding interface has
2749 * been detached dynamically (XXX).
2751 error = EADDRNOTAVAIL;
2753 } else { /* ipv6mr_interface == 0 */
2754 struct sockaddr_in6 sa6_mc;
2757 * The API spec says as follows:
2758 * If the interface index is specified as 0, the
2759 * system may choose a multicast group membership to
2760 * drop by matching the multicast address only.
2761 * On the other hand, we cannot disambiguate the scope
2762 * zone unless an interface is provided. Thus, we
2763 * check if there's ambiguity with the default scope
2764 * zone as the last resort.
2766 bzero(&sa6_mc, sizeof(sa6_mc));
2767 sa6_mc.sin6_family = AF_INET6;
2768 sa6_mc.sin6_len = sizeof(sa6_mc);
2769 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2770 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2773 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2777 * Find the membership in the membership list.
2779 for (imm = im6o->im6o_memberships.lh_first;
2780 imm != NULL; imm = imm->i6mm_chain.le_next) {
2781 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2782 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2783 &mreq->ipv6mr_multiaddr))
2787 /* Unable to resolve interface */
2788 error = EADDRNOTAVAIL;
2792 * Give up the multicast address record to which the
2793 * membership points.
2795 LIST_REMOVE(imm, i6mm_chain);
2796 in6_delmulti(imm->i6mm_maddr);
2797 free(imm, M_IP6MADDR);
2806 * If all options have default values, no need to keep the mbuf.
2808 if (im6o->im6o_multicast_ifp == NULL &&
2809 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2810 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2811 im6o->im6o_memberships.lh_first == NULL) {
2812 free(*im6op, M_IP6MOPTS);
2820 * Return the IP6 multicast options in response to user getsockopt().
2823 ip6_getmoptions(optname, im6o, mp)
2825 struct ip6_moptions *im6o;
2828 u_int *hlim, *loop, *ifindex;
2830 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2834 case IPV6_MULTICAST_IF:
2835 ifindex = mtod(*mp, u_int *);
2836 (*mp)->m_len = sizeof(u_int);
2837 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2840 *ifindex = im6o->im6o_multicast_ifp->if_index;
2843 case IPV6_MULTICAST_HOPS:
2844 hlim = mtod(*mp, u_int *);
2845 (*mp)->m_len = sizeof(u_int);
2847 *hlim = ip6_defmcasthlim;
2849 *hlim = im6o->im6o_multicast_hlim;
2852 case IPV6_MULTICAST_LOOP:
2853 loop = mtod(*mp, u_int *);
2854 (*mp)->m_len = sizeof(u_int);
2856 *loop = ip6_defmcasthlim;
2858 *loop = im6o->im6o_multicast_loop;
2862 return (EOPNOTSUPP);
2867 * Discard the IP6 multicast options.
2870 ip6_freemoptions(im6o)
2871 struct ip6_moptions *im6o;
2873 struct in6_multi_mship *imm;
2878 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2879 LIST_REMOVE(imm, i6mm_chain);
2880 if (imm->i6mm_maddr)
2881 in6_delmulti(imm->i6mm_maddr);
2882 free(imm, M_IP6MADDR);
2884 free(im6o, M_IP6MOPTS);
2888 * Set IPv6 outgoing packet options based on advanced API.
2891 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2892 struct mbuf *control;
2893 struct ip6_pktopts *opt, *stickyopt;
2896 struct cmsghdr *cm = 0;
2898 if (control == NULL || opt == NULL)
2901 ip6_initpktopts(opt);
2906 * If stickyopt is provided, make a local copy of the options
2907 * for this particular packet, then override them by ancillary
2909 * XXX: copypktopts() does not copy the cached route to a next
2910 * hop (if any). This is not very good in terms of efficiency,
2911 * but we can allow this since this option should be rarely
2914 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2919 * XXX: Currently, we assume all the optional information is stored
2922 if (control->m_next)
2925 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2926 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2929 if (control->m_len < CMSG_LEN(0))
2932 cm = mtod(control, struct cmsghdr *);
2933 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2935 if (cm->cmsg_level != IPPROTO_IPV6)
2938 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2939 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
2948 * Set a particular packet option, as a sticky option or an ancillary data
2949 * item. "len" can be 0 only when it's a sticky option.
2950 * We have 4 cases of combination of "sticky" and "cmsg":
2951 * "sticky=0, cmsg=0": impossible
2952 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2953 * "sticky=1, cmsg=0": RFC3542 socket option
2954 * "sticky=1, cmsg=1": RFC2292 socket option
2957 ip6_setpktopt(optname, buf, len, opt, priv, sticky, cmsg, uproto)
2958 int optname, len, priv, sticky, cmsg, uproto;
2960 struct ip6_pktopts *opt;
2962 int minmtupolicy, preftemp;
2964 if (!sticky && !cmsg) {
2966 printf("ip6_setpktopt: impossible case\n");
2972 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2973 * not be specified in the context of RFC3542. Conversely,
2974 * RFC3542 types should not be specified in the context of RFC2292.
2978 case IPV6_2292PKTINFO:
2979 case IPV6_2292HOPLIMIT:
2980 case IPV6_2292NEXTHOP:
2981 case IPV6_2292HOPOPTS:
2982 case IPV6_2292DSTOPTS:
2983 case IPV6_2292RTHDR:
2984 case IPV6_2292PKTOPTIONS:
2985 return (ENOPROTOOPT);
2988 if (sticky && cmsg) {
2995 case IPV6_RTHDRDSTOPTS:
2997 case IPV6_USE_MIN_MTU:
3000 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3001 return (ENOPROTOOPT);
3006 case IPV6_2292PKTINFO:
3009 struct ifnet *ifp = NULL;
3010 struct in6_pktinfo *pktinfo;
3012 if (len != sizeof(struct in6_pktinfo))
3015 pktinfo = (struct in6_pktinfo *)buf;
3018 * An application can clear any sticky IPV6_PKTINFO option by
3019 * doing a "regular" setsockopt with ipi6_addr being
3020 * in6addr_any and ipi6_ifindex being zero.
3021 * [RFC 3542, Section 6]
3023 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3024 pktinfo->ipi6_ifindex == 0 &&
3025 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3026 ip6_clearpktopts(opt, optname);
3030 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3031 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3035 /* validate the interface index if specified. */
3036 if (pktinfo->ipi6_ifindex > if_index ||
3037 pktinfo->ipi6_ifindex < 0) {
3040 if (pktinfo->ipi6_ifindex) {
3041 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3047 * We store the address anyway, and let in6_selectsrc()
3048 * validate the specified address. This is because ipi6_addr
3049 * may not have enough information about its scope zone, and
3050 * we may need additional information (such as outgoing
3051 * interface or the scope zone of a destination address) to
3052 * disambiguate the scope.
3053 * XXX: the delay of the validation may confuse the
3054 * application when it is used as a sticky option.
3056 if (opt->ip6po_pktinfo == NULL) {
3057 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3058 M_IP6OPT, M_NOWAIT);
3059 if (opt->ip6po_pktinfo == NULL)
3062 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3066 case IPV6_2292HOPLIMIT:
3072 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3073 * to simplify the ordering among hoplimit options.
3075 if (optname == IPV6_HOPLIMIT && sticky)
3076 return (ENOPROTOOPT);
3078 if (len != sizeof(int))
3081 if (*hlimp < -1 || *hlimp > 255)
3084 opt->ip6po_hlim = *hlimp;
3092 if (len != sizeof(int))
3094 tclass = *(int *)buf;
3095 if (tclass < -1 || tclass > 255)
3098 opt->ip6po_tclass = tclass;
3102 case IPV6_2292NEXTHOP:
3107 if (len == 0) { /* just remove the option */
3108 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3112 /* check if cmsg_len is large enough for sa_len */
3113 if (len < sizeof(struct sockaddr) || len < *buf)
3116 switch (((struct sockaddr *)buf)->sa_family) {
3119 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3122 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3125 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3126 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3129 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3135 case AF_LINK: /* should eventually be supported */
3137 return (EAFNOSUPPORT);
3140 /* turn off the previous option, then set the new option. */
3141 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3142 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3143 if (opt->ip6po_nexthop == NULL)
3145 bcopy(buf, opt->ip6po_nexthop, *buf);
3148 case IPV6_2292HOPOPTS:
3151 struct ip6_hbh *hbh;
3155 * XXX: We don't allow a non-privileged user to set ANY HbH
3156 * options, since per-option restriction has too much
3163 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3164 break; /* just remove the option */
3167 /* message length validation */
3168 if (len < sizeof(struct ip6_hbh))
3170 hbh = (struct ip6_hbh *)buf;
3171 hbhlen = (hbh->ip6h_len + 1) << 3;
3175 /* turn off the previous option, then set the new option. */
3176 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3177 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3178 if (opt->ip6po_hbh == NULL)
3180 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3185 case IPV6_2292DSTOPTS:
3187 case IPV6_RTHDRDSTOPTS:
3189 struct ip6_dest *dest, **newdest = NULL;
3192 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3196 ip6_clearpktopts(opt, optname);
3197 break; /* just remove the option */
3200 /* message length validation */
3201 if (len < sizeof(struct ip6_dest))
3203 dest = (struct ip6_dest *)buf;
3204 destlen = (dest->ip6d_len + 1) << 3;
3209 * Determine the position that the destination options header
3210 * should be inserted; before or after the routing header.
3213 case IPV6_2292DSTOPTS:
3215 * The old advacned API is ambiguous on this point.
3216 * Our approach is to determine the position based
3217 * according to the existence of a routing header.
3218 * Note, however, that this depends on the order of the
3219 * extension headers in the ancillary data; the 1st
3220 * part of the destination options header must appear
3221 * before the routing header in the ancillary data,
3223 * RFC3542 solved the ambiguity by introducing
3224 * separate ancillary data or option types.
3226 if (opt->ip6po_rthdr == NULL)
3227 newdest = &opt->ip6po_dest1;
3229 newdest = &opt->ip6po_dest2;
3231 case IPV6_RTHDRDSTOPTS:
3232 newdest = &opt->ip6po_dest1;
3235 newdest = &opt->ip6po_dest2;
3239 /* turn off the previous option, then set the new option. */
3240 ip6_clearpktopts(opt, optname);
3241 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3242 if (*newdest == NULL)
3244 bcopy(dest, *newdest, destlen);
3249 case IPV6_2292RTHDR:
3252 struct ip6_rthdr *rth;
3256 ip6_clearpktopts(opt, IPV6_RTHDR);
3257 break; /* just remove the option */
3260 /* message length validation */
3261 if (len < sizeof(struct ip6_rthdr))
3263 rth = (struct ip6_rthdr *)buf;
3264 rthlen = (rth->ip6r_len + 1) << 3;
3268 switch (rth->ip6r_type) {
3269 case IPV6_RTHDR_TYPE_0:
3270 if (rth->ip6r_len == 0) /* must contain one addr */
3272 if (rth->ip6r_len % 2) /* length must be even */
3274 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3278 return (EINVAL); /* not supported */
3281 /* turn off the previous option */
3282 ip6_clearpktopts(opt, IPV6_RTHDR);
3283 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3284 if (opt->ip6po_rthdr == NULL)
3286 bcopy(rth, opt->ip6po_rthdr, rthlen);
3291 case IPV6_USE_MIN_MTU:
3292 if (len != sizeof(int))
3294 minmtupolicy = *(int *)buf;
3295 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3296 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3297 minmtupolicy != IP6PO_MINMTU_ALL) {
3300 opt->ip6po_minmtu = minmtupolicy;
3304 if (len != sizeof(int))
3307 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3309 * we ignore this option for TCP sockets.
3310 * (RFC3542 leaves this case unspecified.)
3312 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3314 opt->ip6po_flags |= IP6PO_DONTFRAG;
3317 case IPV6_PREFER_TEMPADDR:
3318 if (len != sizeof(int))
3320 preftemp = *(int *)buf;
3321 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3322 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3323 preftemp != IP6PO_TEMPADDR_PREFER) {
3326 opt->ip6po_prefer_tempaddr = preftemp;
3330 return (ENOPROTOOPT);
3331 } /* end of switch */
3337 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3338 * packet to the input queue of a specified interface. Note that this
3339 * calls the output routine of the loopback "driver", but with an interface
3340 * pointer that might NOT be &loif -- easier than replicating that code here.
3343 ip6_mloopback(ifp, m, dst)
3346 struct sockaddr_in6 *dst;
3349 struct ip6_hdr *ip6;
3351 copym = m_copy(m, 0, M_COPYALL);
3356 * Make sure to deep-copy IPv6 header portion in case the data
3357 * is in an mbuf cluster, so that we can safely override the IPv6
3358 * header portion later.
3360 if ((copym->m_flags & M_EXT) != 0 ||
3361 copym->m_len < sizeof(struct ip6_hdr)) {
3362 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3368 if (copym->m_len < sizeof(*ip6)) {
3374 ip6 = mtod(copym, struct ip6_hdr *);
3376 * clear embedded scope identifiers if necessary.
3377 * in6_clearscope will touch the addresses only when necessary.
3379 in6_clearscope(&ip6->ip6_src);
3380 in6_clearscope(&ip6->ip6_dst);
3382 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3386 * Chop IPv6 header off from the payload.
3389 ip6_splithdr(m, exthdrs)
3391 struct ip6_exthdrs *exthdrs;
3394 struct ip6_hdr *ip6;
3396 ip6 = mtod(m, struct ip6_hdr *);
3397 if (m->m_len > sizeof(*ip6)) {
3398 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3403 M_MOVE_PKTHDR(mh, m);
3404 MH_ALIGN(mh, sizeof(*ip6));
3405 m->m_len -= sizeof(*ip6);
3406 m->m_data += sizeof(*ip6);
3409 m->m_len = sizeof(*ip6);
3410 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3412 exthdrs->ip6e_ip6 = m;
3417 * Compute IPv6 extension header length.
3421 struct in6pcb *in6p;
3425 if (!in6p->in6p_outputopts)
3430 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3432 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3433 if (in6p->in6p_outputopts->ip6po_rthdr)
3434 /* dest1 is valid with rthdr only */
3435 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3436 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3437 len += elen(in6p->in6p_outputopts->ip6po_dest2);