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$");
66 #include "opt_ip6fw.h"
68 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
71 #include <sys/param.h>
72 #include <sys/malloc.h>
75 #include <sys/errno.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/systm.h>
80 #include <sys/kernel.h>
83 #include <net/netisr.h>
84 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet6/in6_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet/icmp6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet6/nd6.h>
98 #include <netinet6/ipsec.h>
100 #include <netinet6/ipsec6.h>
102 #include <netkey/key.h>
106 #include <netipsec/ipsec.h>
107 #include <netipsec/ipsec6.h>
108 #include <netipsec/key.h>
109 #endif /* FAST_IPSEC */
111 #include <netinet6/ip6_fw.h>
113 #include <net/net_osdep.h>
115 #include <netinet6/ip6protosw.h>
116 #include <netinet6/scope6_var.h>
118 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "internet multicast options");
121 struct mbuf *ip6e_ip6;
122 struct mbuf *ip6e_hbh;
123 struct mbuf *ip6e_dest1;
124 struct mbuf *ip6e_rthdr;
125 struct mbuf *ip6e_dest2;
128 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
130 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
131 struct socket *, struct sockopt *));
132 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
133 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *, int,
136 static int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *);
137 static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **);
138 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
139 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
140 struct ip6_frag **));
141 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
142 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
143 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
144 struct ifnet *, struct in6_addr *, u_long *, int *));
145 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
149 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
150 * header (with pri, len, nxt, hlim, src, dst).
151 * This function may modify ver and hlim only.
152 * The mbuf chain containing the packet will be freed.
153 * The mbuf opt, if present, will not be freed.
155 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
156 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
157 * which is rt_rmx.rmx_mtu.
160 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
162 struct ip6_pktopts *opt;
163 struct route_in6 *ro;
165 struct ip6_moptions *im6o;
166 struct ifnet **ifpp; /* XXX: just for statistics */
169 struct ip6_hdr *ip6, *mhip6;
170 struct ifnet *ifp, *origifp;
172 int hlen, tlen, len, off;
173 struct route_in6 ip6route;
174 struct rtentry *rt = NULL;
175 struct sockaddr_in6 *dst, src_sa, dst_sa;
176 struct in6_addr odst;
178 struct in6_ifaddr *ia = NULL;
180 int alwaysfrag, dontfrag;
181 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
182 struct ip6_exthdrs exthdrs;
183 struct in6_addr finaldst, src0, dst0;
185 struct route_in6 *ro_pmtu = NULL;
188 #if defined(IPSEC) || defined(FAST_IPSEC)
189 int needipsectun = 0;
190 struct secpolicy *sp = NULL;
191 #endif /*IPSEC || FAST_IPSEC*/
193 ip6 = mtod(m, struct ip6_hdr *);
194 finaldst = ip6->ip6_dst;
196 #define MAKE_EXTHDR(hp, mp) \
199 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
200 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
201 ((eh)->ip6e_len + 1) << 3); \
205 } while (/*CONSTCOND*/ 0)
207 bzero(&exthdrs, sizeof(exthdrs));
210 /* Hop-by-Hop options header */
211 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
212 /* Destination options header(1st part) */
213 if (opt->ip6po_rthdr) {
215 * Destination options header(1st part)
216 * This only makes sence with a routing header.
217 * See Section 9.2 of RFC 3542.
218 * Disabling this part just for MIP6 convenience is
219 * a bad idea. We need to think carefully about a
220 * way to make the advanced API coexist with MIP6
221 * options, which might automatically be inserted in
224 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
227 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
228 /* Destination options header(2nd part) */
229 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
233 /* get a security policy for this packet */
235 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
237 sp = ipsec6_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error);
240 ipsec6stat.out_inval++;
247 switch (sp->policy) {
248 case IPSEC_POLICY_DISCARD:
250 * This packet is just discarded.
252 ipsec6stat.out_polvio++;
255 case IPSEC_POLICY_BYPASS:
256 case IPSEC_POLICY_NONE:
257 /* no need to do IPsec. */
261 case IPSEC_POLICY_IPSEC:
262 if (sp->req == NULL) {
263 /* acquire a policy */
264 error = key_spdacquire(sp);
270 case IPSEC_POLICY_ENTRUST:
272 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
276 /* get a security policy for this packet */
278 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
280 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
283 newipsecstat.ips_out_inval++;
290 switch (sp->policy) {
291 case IPSEC_POLICY_DISCARD:
293 * This packet is just discarded.
295 newipsecstat.ips_out_polvio++;
298 case IPSEC_POLICY_BYPASS:
299 case IPSEC_POLICY_NONE:
300 /* no need to do IPsec. */
304 case IPSEC_POLICY_IPSEC:
305 if (sp->req == NULL) {
306 /* acquire a policy */
307 error = key_spdacquire(sp);
313 case IPSEC_POLICY_ENTRUST:
315 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
317 #endif /* FAST_IPSEC */
320 * Calculate the total length of the extension header chain.
321 * Keep the length of the unfragmentable part for fragmentation.
324 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
325 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
326 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
327 unfragpartlen = optlen + sizeof(struct ip6_hdr);
328 /* NOTE: we don't add AH/ESP length here. do that later. */
329 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
332 * If we need IPsec, or there is at least one extension header,
333 * separate IP6 header from the payload.
335 if ((needipsec || optlen) && !hdrsplit) {
336 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
340 m = exthdrs.ip6e_ip6;
345 ip6 = mtod(m, struct ip6_hdr *);
347 /* adjust mbuf packet header length */
348 m->m_pkthdr.len += optlen;
349 plen = m->m_pkthdr.len - sizeof(*ip6);
351 /* If this is a jumbo payload, insert a jumbo payload option. */
352 if (plen > IPV6_MAXPACKET) {
354 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
358 m = exthdrs.ip6e_ip6;
362 ip6 = mtod(m, struct ip6_hdr *);
363 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
367 ip6->ip6_plen = htons(plen);
370 * Concatenate headers and fill in next header fields.
371 * Here we have, on "m"
373 * and we insert headers accordingly. Finally, we should be getting:
374 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
376 * during the header composing process, "m" points to IPv6 header.
377 * "mprev" points to an extension header prior to esp.
380 u_char *nexthdrp = &ip6->ip6_nxt;
381 struct mbuf *mprev = m;
384 * we treat dest2 specially. this makes IPsec processing
385 * much easier. the goal here is to make mprev point the
386 * mbuf prior to dest2.
388 * result: IPv6 dest2 payload
389 * m and mprev will point to IPv6 header.
391 if (exthdrs.ip6e_dest2) {
393 panic("assumption failed: hdr not split");
394 exthdrs.ip6e_dest2->m_next = m->m_next;
395 m->m_next = exthdrs.ip6e_dest2;
396 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
397 ip6->ip6_nxt = IPPROTO_DSTOPTS;
400 #define MAKE_CHAIN(m, mp, p, i)\
404 panic("assumption failed: hdr not split"); \
405 *mtod((m), u_char *) = *(p);\
407 p = mtod((m), u_char *);\
408 (m)->m_next = (mp)->m_next;\
412 } while (/*CONSTCOND*/ 0)
414 * result: IPv6 hbh dest1 rthdr dest2 payload
415 * m will point to IPv6 header. mprev will point to the
416 * extension header prior to dest2 (rthdr in the above case).
418 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
419 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
421 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
424 #if defined(IPSEC) || defined(FAST_IPSEC)
429 * pointers after IPsec headers are not valid any more.
430 * other pointers need a great care too.
431 * (IPsec routines should not mangle mbufs prior to AH/ESP)
433 exthdrs.ip6e_dest2 = NULL;
436 struct ip6_rthdr *rh = NULL;
438 struct ipsec_output_state state;
440 if (exthdrs.ip6e_rthdr) {
441 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
442 segleft_org = rh->ip6r_segleft;
443 rh->ip6r_segleft = 0;
446 bzero(&state, sizeof(state));
448 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
452 /* mbuf is already reclaimed in ipsec6_output_trans. */
462 printf("ip6_output (ipsec): error code %d\n", error);
465 /* don't show these error codes to the user */
471 if (exthdrs.ip6e_rthdr) {
472 /* ah6_output doesn't modify mbuf chain */
473 rh->ip6r_segleft = segleft_org;
481 * If there is a routing header, replace the destination address field
482 * with the first hop of the routing header.
484 if (exthdrs.ip6e_rthdr) {
485 struct ip6_rthdr *rh =
486 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
487 struct ip6_rthdr *));
488 struct ip6_rthdr0 *rh0;
489 struct in6_addr *addr;
490 struct sockaddr_in6 sa;
492 switch (rh->ip6r_type) {
493 case IPV6_RTHDR_TYPE_0:
494 rh0 = (struct ip6_rthdr0 *)rh;
495 addr = (struct in6_addr *)(rh0 + 1);
498 * construct a sockaddr_in6 form of
501 * XXX: we may not have enough
502 * information about its scope zone;
503 * there is no standard API to pass
504 * the information from the
507 bzero(&sa, sizeof(sa));
508 sa.sin6_family = AF_INET6;
509 sa.sin6_len = sizeof(sa);
510 sa.sin6_addr = addr[0];
511 if ((error = sa6_embedscope(&sa,
512 ip6_use_defzone)) != 0) {
515 ip6->ip6_dst = sa.sin6_addr;
516 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr)
517 * (rh0->ip6r0_segleft - 1));
518 addr[rh0->ip6r0_segleft - 1] = finaldst;
520 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
522 default: /* is it possible? */
528 /* Source address validation */
529 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
530 (flags & IPV6_UNSPECSRC) == 0) {
532 ip6stat.ip6s_badscope++;
535 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
537 ip6stat.ip6s_badscope++;
541 ip6stat.ip6s_localout++;
548 bzero((caddr_t)ro, sizeof(*ro));
551 if (opt && opt->ip6po_rthdr)
552 ro = &opt->ip6po_route;
553 dst = (struct sockaddr_in6 *)&ro->ro_dst;
557 * if specified, try to fill in the traffic class field.
558 * do not override if a non-zero value is already set.
559 * we check the diffserv field and the ecn field separately.
561 if (opt && opt->ip6po_tclass >= 0) {
564 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
566 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
569 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
572 /* fill in or override the hop limit field, if necessary. */
573 if (opt && opt->ip6po_hlim != -1)
574 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
575 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
577 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
579 ip6->ip6_hlim = ip6_defmcasthlim;
582 #if defined(IPSEC) || defined(FAST_IPSEC)
583 if (needipsec && needipsectun) {
584 struct ipsec_output_state state;
587 * All the extension headers will become inaccessible
588 * (since they can be encrypted).
589 * Don't panic, we need no more updates to extension headers
590 * on inner IPv6 packet (since they are now encapsulated).
592 * IPv6 [ESP|AH] IPv6 [extension headers] payload
594 bzero(&exthdrs, sizeof(exthdrs));
595 exthdrs.ip6e_ip6 = m;
597 bzero(&state, sizeof(state));
599 state.ro = (struct route *)ro;
600 state.dst = (struct sockaddr *)dst;
602 error = ipsec6_output_tunnel(&state, sp, flags);
605 ro = (struct route_in6 *)state.ro;
606 dst = (struct sockaddr_in6 *)state.dst;
608 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
619 printf("ip6_output (ipsec): error code %d\n", error);
622 /* don't show these error codes to the user */
629 exthdrs.ip6e_ip6 = m;
634 ip6 = mtod(m, struct ip6_hdr *);
636 bzero(&dst_sa, sizeof(dst_sa));
637 dst_sa.sin6_family = AF_INET6;
638 dst_sa.sin6_len = sizeof(dst_sa);
639 dst_sa.sin6_addr = ip6->ip6_dst;
640 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
641 &ifp, &rt, 0)) != 0) {
644 ip6stat.ip6s_noroute++;
648 break; /* XXX statistics? */
651 in6_ifstat_inc(ifp, ifs6_out_discard);
656 * If in6_selectroute() does not return a route entry,
657 * dst may not have been updated.
659 *dst = dst_sa; /* XXX */
663 * then rt (for unicast) and ifp must be non-NULL valid values.
665 if ((flags & IPV6_FORWARDING) == 0) {
666 /* XXX: the FORWARDING flag can be set for mrouting. */
667 in6_ifstat_inc(ifp, ifs6_out_request);
670 ia = (struct in6_ifaddr *)(rt->rt_ifa);
675 * The outgoing interface must be in the zone of source and
676 * destination addresses. We should use ia_ifp to support the
677 * case of sending packets to an address of our own.
679 if (ia != NULL && ia->ia_ifp)
680 origifp = ia->ia_ifp;
685 if (in6_setscope(&src0, origifp, &zone))
687 bzero(&src_sa, sizeof(src_sa));
688 src_sa.sin6_family = AF_INET6;
689 src_sa.sin6_len = sizeof(src_sa);
690 src_sa.sin6_addr = ip6->ip6_src;
691 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
695 if (in6_setscope(&dst0, origifp, &zone))
697 /* re-initialize to be sure */
698 bzero(&dst_sa, sizeof(dst_sa));
699 dst_sa.sin6_family = AF_INET6;
700 dst_sa.sin6_len = sizeof(dst_sa);
701 dst_sa.sin6_addr = ip6->ip6_dst;
702 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
706 /* scope check is done. */
710 ip6stat.ip6s_badscope++;
711 in6_ifstat_inc(origifp, ifs6_out_discard);
713 error = EHOSTUNREACH; /* XXX */
717 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
718 if (opt && opt->ip6po_nextroute.ro_rt) {
720 * The nexthop is explicitly specified by the
721 * application. We assume the next hop is an IPv6
724 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
726 else if ((rt->rt_flags & RTF_GATEWAY))
727 dst = (struct sockaddr_in6 *)rt->rt_gateway;
730 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
731 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
733 struct in6_multi *in6m;
735 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
737 in6_ifstat_inc(ifp, ifs6_out_mcast);
740 * Confirm that the outgoing interface supports multicast.
742 if (!(ifp->if_flags & IFF_MULTICAST)) {
743 ip6stat.ip6s_noroute++;
744 in6_ifstat_inc(ifp, ifs6_out_discard);
748 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
750 (im6o == NULL || im6o->im6o_multicast_loop)) {
752 * If we belong to the destination multicast group
753 * on the outgoing interface, and the caller did not
754 * forbid loopback, loop back a copy.
756 ip6_mloopback(ifp, m, dst);
759 * If we are acting as a multicast router, perform
760 * multicast forwarding as if the packet had just
761 * arrived on the interface to which we are about
762 * to send. The multicast forwarding function
763 * recursively calls this function, using the
764 * IPV6_FORWARDING flag to prevent infinite recursion.
766 * Multicasts that are looped back by ip6_mloopback(),
767 * above, will be forwarded by the ip6_input() routine,
770 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
772 * XXX: ip6_mforward expects that rcvif is NULL
773 * when it is called from the originating path.
774 * However, it is not always the case, since
775 * some versions of MGETHDR() does not
776 * initialize the field.
778 m->m_pkthdr.rcvif = NULL;
779 if (ip6_mforward(ip6, ifp, m) != 0) {
786 * Multicasts with a hoplimit of zero may be looped back,
787 * above, but must not be transmitted on a network.
788 * Also, multicasts addressed to the loopback interface
789 * are not sent -- the above call to ip6_mloopback() will
790 * loop back a copy if this host actually belongs to the
791 * destination group on the loopback interface.
793 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
794 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
801 * Fill the outgoing inteface to tell the upper layer
802 * to increment per-interface statistics.
807 /* Determine path MTU. */
808 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
813 * The caller of this function may specify to use the minimum MTU
815 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
816 * setting. The logic is a bit complicated; by default, unicast
817 * packets will follow path MTU while multicast packets will be sent at
818 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
819 * including unicast ones will be sent at the minimum MTU. Multicast
820 * packets will always be sent at the minimum MTU unless
821 * IP6PO_MINMTU_DISABLE is explicitly specified.
822 * See RFC 3542 for more details.
824 if (mtu > IPV6_MMTU) {
825 if ((flags & IPV6_MINMTU))
827 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
829 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
831 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
837 * clear embedded scope identifiers if necessary.
838 * in6_clearscope will touch the addresses only when necessary.
840 in6_clearscope(&ip6->ip6_src);
841 in6_clearscope(&ip6->ip6_dst);
844 * Check with the firewall...
846 if (ip6_fw_enable && ip6_fw_chk_ptr) {
848 m->m_pkthdr.rcvif = NULL; /* XXX */
849 /* If ipfw says divert, we have to just drop packet */
850 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
861 * If the outgoing packet contains a hop-by-hop options header,
862 * it must be examined and processed even by the source node.
863 * (RFC 2460, section 4.)
865 if (exthdrs.ip6e_hbh) {
866 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
867 u_int32_t dummy; /* XXX unused */
868 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
871 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
872 panic("ip6e_hbh is not continuous");
875 * XXX: if we have to send an ICMPv6 error to the sender,
876 * we need the M_LOOP flag since icmp6_error() expects
877 * the IPv6 and the hop-by-hop options header are
878 * continuous unless the flag is set.
880 m->m_flags |= M_LOOP;
881 m->m_pkthdr.rcvif = ifp;
882 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
883 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
884 &dummy, &plen) < 0) {
885 /* m was already freed at this point */
886 error = EINVAL;/* better error? */
889 m->m_flags &= ~M_LOOP; /* XXX */
890 m->m_pkthdr.rcvif = NULL;
893 /* Jump over all PFIL processing if hooks are not active. */
894 if (inet6_pfil_hook.ph_busy_count == -1)
898 /* Run through list of hooks for output packets. */
899 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
900 if (error != 0 || m == NULL)
902 ip6 = mtod(m, struct ip6_hdr *);
904 /* See if destination IP address was changed by packet filter. */
905 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
906 m->m_flags |= M_SKIP_FIREWALL;
907 /* If destination is now ourself drop to ip6_input(). */
908 if (in6_localaddr(&ip6->ip6_dst)) {
909 if (m->m_pkthdr.rcvif == NULL)
910 m->m_pkthdr.rcvif = loif;
911 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
912 m->m_pkthdr.csum_flags |=
913 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
914 m->m_pkthdr.csum_data = 0xffff;
916 m->m_pkthdr.csum_flags |=
917 CSUM_IP_CHECKED | CSUM_IP_VALID;
918 error = netisr_queue(NETISR_IPV6, m);
921 goto again; /* Redo the routing table lookup. */
924 /* XXX: IPFIREWALL_FORWARD */
928 * Send the packet to the outgoing interface.
929 * If necessary, do IPv6 fragmentation before sending.
931 * the logic here is rather complex:
932 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
933 * 1-a: send as is if tlen <= path mtu
934 * 1-b: fragment if tlen > path mtu
936 * 2: if user asks us not to fragment (dontfrag == 1)
937 * 2-a: send as is if tlen <= interface mtu
938 * 2-b: error if tlen > interface mtu
940 * 3: if we always need to attach fragment header (alwaysfrag == 1)
943 * 4: if dontfrag == 1 && alwaysfrag == 1
944 * error, as we cannot handle this conflicting request
946 tlen = m->m_pkthdr.len;
948 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
952 if (dontfrag && alwaysfrag) { /* case 4 */
953 /* conflicting request - can't transmit */
957 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
959 * Even if the DONTFRAG option is specified, we cannot send the
960 * packet when the data length is larger than the MTU of the
961 * outgoing interface.
962 * Notify the error by sending IPV6_PATHMTU ancillary data as
963 * well as returning an error code (the latter is not described
967 struct ip6ctlparam ip6cp;
969 mtu32 = (u_int32_t)mtu;
970 bzero(&ip6cp, sizeof(ip6cp));
971 ip6cp.ip6c_cmdarg = (void *)&mtu32;
972 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
980 * transmit packet without fragmentation
982 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
983 struct in6_ifaddr *ia6;
985 ip6 = mtod(m, struct ip6_hdr *);
986 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
988 /* Record statistics for this interface address. */
989 ia6->ia_ifa.if_opackets++;
990 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
993 /* clean ipsec history once it goes out of the node */
996 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1001 * try to fragment the packet. case 1-b and 3
1003 if (mtu < IPV6_MMTU) {
1004 /* path MTU cannot be less than IPV6_MMTU */
1006 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1008 } else if (ip6->ip6_plen == 0) {
1009 /* jumbo payload cannot be fragmented */
1011 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1014 struct mbuf **mnext, *m_frgpart;
1015 struct ip6_frag *ip6f;
1016 u_int32_t id = htonl(ip6_randomid());
1019 struct ip6ctlparam ip6cp;
1022 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
1025 * Too large for the destination or interface;
1026 * fragment if possible.
1027 * Must be able to put at least 8 bytes per fragment.
1029 hlen = unfragpartlen;
1030 if (mtu > IPV6_MAXPACKET)
1031 mtu = IPV6_MAXPACKET;
1035 * It is believed this code is a leftover from the
1036 * development of the IPV6_RECVPATHMTU sockopt and
1037 * associated work to implement RFC3542.
1038 * It's not entirely clear what the intent of the API
1039 * is at this point, so disable this code for now.
1040 * The IPV6_RECVPATHMTU sockopt and/or IPV6_DONTFRAG
1041 * will send notifications if the application requests.
1044 /* Notify a proper path MTU to applications. */
1045 mtu32 = (u_int32_t)mtu;
1046 bzero(&ip6cp, sizeof(ip6cp));
1047 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1048 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
1052 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1055 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1060 * Verify that we have any chance at all of being able to queue
1061 * the packet or packet fragments
1063 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
1064 < tlen /* - hlen */)) {
1066 ip6stat.ip6s_odropped++;
1070 mnext = &m->m_nextpkt;
1073 * Change the next header field of the last header in the
1074 * unfragmentable part.
1076 if (exthdrs.ip6e_rthdr) {
1077 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1078 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1079 } else if (exthdrs.ip6e_dest1) {
1080 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1081 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1082 } else if (exthdrs.ip6e_hbh) {
1083 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1084 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1086 nextproto = ip6->ip6_nxt;
1087 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1091 * Loop through length of segment after first fragment,
1092 * make new header and copy data of each part and link onto
1096 for (off = hlen; off < tlen; off += len) {
1097 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1100 ip6stat.ip6s_odropped++;
1103 m->m_pkthdr.rcvif = NULL;
1104 m->m_flags = m0->m_flags & M_COPYFLAGS;
1106 mnext = &m->m_nextpkt;
1107 m->m_data += max_linkhdr;
1108 mhip6 = mtod(m, struct ip6_hdr *);
1110 m->m_len = sizeof(*mhip6);
1111 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1113 ip6stat.ip6s_odropped++;
1116 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1117 if (off + len >= tlen)
1120 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1121 mhip6->ip6_plen = htons((u_short)(len + hlen +
1122 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1123 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1125 ip6stat.ip6s_odropped++;
1128 m_cat(m, m_frgpart);
1129 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1130 m->m_pkthdr.rcvif = NULL;
1131 ip6f->ip6f_reserved = 0;
1132 ip6f->ip6f_ident = id;
1133 ip6f->ip6f_nxt = nextproto;
1134 ip6stat.ip6s_ofragments++;
1135 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1138 in6_ifstat_inc(ifp, ifs6_out_fragok);
1142 * Remove leading garbages.
1148 for (m0 = m; m; m = m0) {
1152 /* Record statistics for this interface address. */
1154 ia->ia_ifa.if_opackets++;
1155 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1158 /* clean ipsec history once it goes out of the node */
1161 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1167 ip6stat.ip6s_fragmented++;
1170 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1172 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1173 RTFREE(ro_pmtu->ro_rt);
1183 #endif /* FAST_IPSEC */
1188 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1189 m_freem(exthdrs.ip6e_dest1);
1190 m_freem(exthdrs.ip6e_rthdr);
1191 m_freem(exthdrs.ip6e_dest2);
1199 ip6_copyexthdr(mp, hdr, hlen)
1206 if (hlen > MCLBYTES)
1207 return (ENOBUFS); /* XXX */
1209 MGET(m, M_DONTWAIT, MT_DATA);
1214 MCLGET(m, M_DONTWAIT);
1215 if ((m->m_flags & M_EXT) == 0) {
1222 bcopy(hdr, mtod(m, caddr_t), hlen);
1229 * Insert jumbo payload option.
1232 ip6_insert_jumboopt(exthdrs, plen)
1233 struct ip6_exthdrs *exthdrs;
1240 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1243 * If there is no hop-by-hop options header, allocate new one.
1244 * If there is one but it doesn't have enough space to store the
1245 * jumbo payload option, allocate a cluster to store the whole options.
1246 * Otherwise, use it to store the options.
1248 if (exthdrs->ip6e_hbh == 0) {
1249 MGET(mopt, M_DONTWAIT, MT_DATA);
1252 mopt->m_len = JUMBOOPTLEN;
1253 optbuf = mtod(mopt, u_char *);
1254 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1255 exthdrs->ip6e_hbh = mopt;
1257 struct ip6_hbh *hbh;
1259 mopt = exthdrs->ip6e_hbh;
1260 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1263 * - exthdrs->ip6e_hbh is not referenced from places
1264 * other than exthdrs.
1265 * - exthdrs->ip6e_hbh is not an mbuf chain.
1267 int oldoptlen = mopt->m_len;
1271 * XXX: give up if the whole (new) hbh header does
1272 * not fit even in an mbuf cluster.
1274 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1278 * As a consequence, we must always prepare a cluster
1281 MGET(n, M_DONTWAIT, MT_DATA);
1283 MCLGET(n, M_DONTWAIT);
1284 if ((n->m_flags & M_EXT) == 0) {
1291 n->m_len = oldoptlen + JUMBOOPTLEN;
1292 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1294 optbuf = mtod(n, caddr_t) + oldoptlen;
1296 mopt = exthdrs->ip6e_hbh = n;
1298 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1299 mopt->m_len += JUMBOOPTLEN;
1301 optbuf[0] = IP6OPT_PADN;
1305 * Adjust the header length according to the pad and
1306 * the jumbo payload option.
1308 hbh = mtod(mopt, struct ip6_hbh *);
1309 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1312 /* fill in the option. */
1313 optbuf[2] = IP6OPT_JUMBO;
1315 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1316 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1318 /* finally, adjust the packet header length */
1319 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1326 * Insert fragment header and copy unfragmentable header portions.
1329 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1330 struct mbuf *m0, *m;
1332 struct ip6_frag **frghdrp;
1334 struct mbuf *n, *mlast;
1336 if (hlen > sizeof(struct ip6_hdr)) {
1337 n = m_copym(m0, sizeof(struct ip6_hdr),
1338 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1345 /* Search for the last mbuf of unfragmentable part. */
1346 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1349 if ((mlast->m_flags & M_EXT) == 0 &&
1350 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1351 /* use the trailing space of the last mbuf for the fragment hdr */
1352 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1354 mlast->m_len += sizeof(struct ip6_frag);
1355 m->m_pkthdr.len += sizeof(struct ip6_frag);
1357 /* allocate a new mbuf for the fragment header */
1360 MGET(mfrg, M_DONTWAIT, MT_DATA);
1363 mfrg->m_len = sizeof(struct ip6_frag);
1364 *frghdrp = mtod(mfrg, struct ip6_frag *);
1365 mlast->m_next = mfrg;
1372 ip6_getpmtu(ro_pmtu, ro, ifp, dst, mtup, alwaysfragp)
1373 struct route_in6 *ro_pmtu, *ro;
1375 struct in6_addr *dst;
1383 if (ro_pmtu != ro) {
1384 /* The first hop and the final destination may differ. */
1385 struct sockaddr_in6 *sa6_dst =
1386 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1387 if (ro_pmtu->ro_rt &&
1388 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1389 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1390 RTFREE(ro_pmtu->ro_rt);
1391 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1393 if (ro_pmtu->ro_rt == NULL) {
1394 bzero(sa6_dst, sizeof(*sa6_dst));
1395 sa6_dst->sin6_family = AF_INET6;
1396 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1397 sa6_dst->sin6_addr = *dst;
1399 rtalloc((struct route *)ro_pmtu);
1402 if (ro_pmtu->ro_rt) {
1404 struct in_conninfo inc;
1406 bzero(&inc, sizeof(inc));
1407 inc.inc_flags = 1; /* IPv6 */
1408 inc.inc6_faddr = *dst;
1411 ifp = ro_pmtu->ro_rt->rt_ifp;
1412 ifmtu = IN6_LINKMTU(ifp);
1413 mtu = tcp_hc_getmtu(&inc);
1415 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1417 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1420 else if (mtu < IPV6_MMTU) {
1422 * RFC2460 section 5, last paragraph:
1423 * if we record ICMPv6 too big message with
1424 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1425 * or smaller, with framgent header attached.
1426 * (fragment header is needed regardless from the
1427 * packet size, for translators to identify packets)
1431 } else if (mtu > ifmtu) {
1433 * The MTU on the route is larger than the MTU on
1434 * the interface! This shouldn't happen, unless the
1435 * MTU of the interface has been changed after the
1436 * interface was brought up. Change the MTU in the
1437 * route to match the interface MTU (as long as the
1438 * field isn't locked).
1441 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1444 mtu = IN6_LINKMTU(ifp);
1446 error = EHOSTUNREACH; /* XXX */
1450 *alwaysfragp = alwaysfrag;
1455 * IP6 socket option processing.
1458 ip6_ctloutput(so, sopt)
1460 struct sockopt *sopt;
1462 int privileged, optdatalen, uproto;
1464 struct inpcb *in6p = sotoinpcb(so);
1466 int level, op, optname;
1471 level = sopt->sopt_level;
1472 op = sopt->sopt_dir;
1473 optname = sopt->sopt_name;
1474 optlen = sopt->sopt_valsize;
1477 panic("ip6_ctloutput: arg soopt is NULL");
1481 privileged = (td == 0 || suser(td)) ? 0 : 1;
1482 uproto = (int)so->so_proto->pr_protocol;
1484 if (level == IPPROTO_IPV6) {
1489 case IPV6_2292PKTOPTIONS:
1490 #ifdef IPV6_PKTOPTIONS
1491 case IPV6_PKTOPTIONS:
1496 error = soopt_getm(sopt, &m); /* XXX */
1499 error = soopt_mcopyin(sopt, m); /* XXX */
1502 error = ip6_pcbopts(&in6p->in6p_outputopts,
1504 m_freem(m); /* XXX */
1509 * Use of some Hop-by-Hop options or some
1510 * Destination options, might require special
1511 * privilege. That is, normal applications
1512 * (without special privilege) might be forbidden
1513 * from setting certain options in outgoing packets,
1514 * and might never see certain options in received
1515 * packets. [RFC 2292 Section 6]
1516 * KAME specific note:
1517 * KAME prevents non-privileged users from sending or
1518 * receiving ANY hbh/dst options in order to avoid
1519 * overhead of parsing options in the kernel.
1521 case IPV6_RECVHOPOPTS:
1522 case IPV6_RECVDSTOPTS:
1523 case IPV6_RECVRTHDRDSTOPTS:
1529 case IPV6_UNICAST_HOPS:
1533 case IPV6_RECVPKTINFO:
1534 case IPV6_RECVHOPLIMIT:
1535 case IPV6_RECVRTHDR:
1536 case IPV6_RECVPATHMTU:
1537 case IPV6_RECVTCLASS:
1539 case IPV6_AUTOFLOWLABEL:
1540 if (optlen != sizeof(int)) {
1544 error = sooptcopyin(sopt, &optval,
1545 sizeof optval, sizeof optval);
1550 case IPV6_UNICAST_HOPS:
1551 if (optval < -1 || optval >= 256)
1554 /* -1 = kernel default */
1555 in6p->in6p_hops = optval;
1556 if ((in6p->in6p_vflag &
1558 in6p->inp_ip_ttl = optval;
1561 #define OPTSET(bit) \
1564 in6p->in6p_flags |= (bit); \
1566 in6p->in6p_flags &= ~(bit); \
1567 } while (/*CONSTCOND*/ 0)
1568 #define OPTSET2292(bit) \
1570 in6p->in6p_flags |= IN6P_RFC2292; \
1572 in6p->in6p_flags |= (bit); \
1574 in6p->in6p_flags &= ~(bit); \
1575 } while (/*CONSTCOND*/ 0)
1576 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1578 case IPV6_RECVPKTINFO:
1579 /* cannot mix with RFC2292 */
1580 if (OPTBIT(IN6P_RFC2292)) {
1584 OPTSET(IN6P_PKTINFO);
1589 struct ip6_pktopts **optp;
1591 /* cannot mix with RFC2292 */
1592 if (OPTBIT(IN6P_RFC2292)) {
1596 optp = &in6p->in6p_outputopts;
1597 error = ip6_pcbopt(IPV6_HOPLIMIT,
1601 privileged, uproto);
1605 case IPV6_RECVHOPLIMIT:
1606 /* cannot mix with RFC2292 */
1607 if (OPTBIT(IN6P_RFC2292)) {
1611 OPTSET(IN6P_HOPLIMIT);
1614 case IPV6_RECVHOPOPTS:
1615 /* cannot mix with RFC2292 */
1616 if (OPTBIT(IN6P_RFC2292)) {
1620 OPTSET(IN6P_HOPOPTS);
1623 case IPV6_RECVDSTOPTS:
1624 /* cannot mix with RFC2292 */
1625 if (OPTBIT(IN6P_RFC2292)) {
1629 OPTSET(IN6P_DSTOPTS);
1632 case IPV6_RECVRTHDRDSTOPTS:
1633 /* cannot mix with RFC2292 */
1634 if (OPTBIT(IN6P_RFC2292)) {
1638 OPTSET(IN6P_RTHDRDSTOPTS);
1641 case IPV6_RECVRTHDR:
1642 /* cannot mix with RFC2292 */
1643 if (OPTBIT(IN6P_RFC2292)) {
1654 case IPV6_RECVPATHMTU:
1656 * We ignore this option for TCP
1658 * (RFC3542 leaves this case
1661 if (uproto != IPPROTO_TCP)
1667 * make setsockopt(IPV6_V6ONLY)
1668 * available only prior to bind(2).
1669 * see ipng mailing list, Jun 22 2001.
1671 if (in6p->in6p_lport ||
1672 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1676 OPTSET(IN6P_IPV6_V6ONLY);
1678 in6p->in6p_vflag &= ~INP_IPV4;
1680 in6p->in6p_vflag |= INP_IPV4;
1682 case IPV6_RECVTCLASS:
1683 /* cannot mix with RFC2292 XXX */
1684 if (OPTBIT(IN6P_RFC2292)) {
1688 OPTSET(IN6P_TCLASS);
1690 case IPV6_AUTOFLOWLABEL:
1691 OPTSET(IN6P_AUTOFLOWLABEL);
1699 case IPV6_USE_MIN_MTU:
1700 case IPV6_PREFER_TEMPADDR:
1701 if (optlen != sizeof(optval)) {
1705 error = sooptcopyin(sopt, &optval,
1706 sizeof optval, sizeof optval);
1710 struct ip6_pktopts **optp;
1711 optp = &in6p->in6p_outputopts;
1712 error = ip6_pcbopt(optname,
1716 privileged, uproto);
1720 case IPV6_2292PKTINFO:
1721 case IPV6_2292HOPLIMIT:
1722 case IPV6_2292HOPOPTS:
1723 case IPV6_2292DSTOPTS:
1724 case IPV6_2292RTHDR:
1726 if (optlen != sizeof(int)) {
1730 error = sooptcopyin(sopt, &optval,
1731 sizeof optval, sizeof optval);
1735 case IPV6_2292PKTINFO:
1736 OPTSET2292(IN6P_PKTINFO);
1738 case IPV6_2292HOPLIMIT:
1739 OPTSET2292(IN6P_HOPLIMIT);
1741 case IPV6_2292HOPOPTS:
1743 * Check super-user privilege.
1744 * See comments for IPV6_RECVHOPOPTS.
1748 OPTSET2292(IN6P_HOPOPTS);
1750 case IPV6_2292DSTOPTS:
1753 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1755 case IPV6_2292RTHDR:
1756 OPTSET2292(IN6P_RTHDR);
1764 case IPV6_RTHDRDSTOPTS:
1767 /* new advanced API (RFC3542) */
1769 u_char optbuf_storage[MCLBYTES];
1771 struct ip6_pktopts **optp;
1773 /* cannot mix with RFC2292 */
1774 if (OPTBIT(IN6P_RFC2292)) {
1780 * We only ensure valsize is not too large
1781 * here. Further validation will be done
1784 error = sooptcopyin(sopt, optbuf_storage,
1785 sizeof(optbuf_storage), 0);
1788 optlen = sopt->sopt_valsize;
1789 optbuf = optbuf_storage;
1790 optp = &in6p->in6p_outputopts;
1791 error = ip6_pcbopt(optname,
1793 optp, privileged, uproto);
1798 case IPV6_MULTICAST_IF:
1799 case IPV6_MULTICAST_HOPS:
1800 case IPV6_MULTICAST_LOOP:
1801 case IPV6_JOIN_GROUP:
1802 case IPV6_LEAVE_GROUP:
1804 if (sopt->sopt_valsize > MLEN) {
1814 if (sopt->sopt_valsize > MCLBYTES) {
1819 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1824 if (sopt->sopt_valsize > MLEN) {
1825 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1826 if ((m->m_flags & M_EXT) == 0) {
1832 m->m_len = sopt->sopt_valsize;
1833 error = sooptcopyin(sopt, mtod(m, char *),
1834 m->m_len, m->m_len);
1839 error = ip6_setmoptions(sopt->sopt_name,
1840 &in6p->in6p_moptions,
1846 case IPV6_PORTRANGE:
1847 error = sooptcopyin(sopt, &optval,
1848 sizeof optval, sizeof optval);
1853 case IPV6_PORTRANGE_DEFAULT:
1854 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1855 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1858 case IPV6_PORTRANGE_HIGH:
1859 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1860 in6p->in6p_flags |= IN6P_HIGHPORT;
1863 case IPV6_PORTRANGE_LOW:
1864 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1865 in6p->in6p_flags |= IN6P_LOWPORT;
1874 #if defined(IPSEC) || defined(FAST_IPSEC)
1875 case IPV6_IPSEC_POLICY:
1881 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1883 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1886 req = mtod(m, caddr_t);
1889 error = ipsec6_set_policy(in6p, optname, req,
1894 #endif /* KAME IPSEC */
1902 struct mbuf **mp = &m;
1904 if (ip6_fw_ctl_ptr == NULL)
1907 if ((error = soopt_getm(sopt, &m)) != 0)
1910 if ((error = soopt_mcopyin(sopt, m)) != 0)
1912 error = (*ip6_fw_ctl_ptr)(optname, mp);
1918 error = ENOPROTOOPT;
1926 case IPV6_2292PKTOPTIONS:
1927 #ifdef IPV6_PKTOPTIONS
1928 case IPV6_PKTOPTIONS:
1931 * RFC3542 (effectively) deprecated the
1932 * semantics of the 2292-style pktoptions.
1933 * Since it was not reliable in nature (i.e.,
1934 * applications had to expect the lack of some
1935 * information after all), it would make sense
1936 * to simplify this part by always returning
1939 sopt->sopt_valsize = 0;
1942 case IPV6_RECVHOPOPTS:
1943 case IPV6_RECVDSTOPTS:
1944 case IPV6_RECVRTHDRDSTOPTS:
1945 case IPV6_UNICAST_HOPS:
1946 case IPV6_RECVPKTINFO:
1947 case IPV6_RECVHOPLIMIT:
1948 case IPV6_RECVRTHDR:
1949 case IPV6_RECVPATHMTU:
1953 case IPV6_PORTRANGE:
1954 case IPV6_RECVTCLASS:
1955 case IPV6_AUTOFLOWLABEL:
1958 case IPV6_RECVHOPOPTS:
1959 optval = OPTBIT(IN6P_HOPOPTS);
1962 case IPV6_RECVDSTOPTS:
1963 optval = OPTBIT(IN6P_DSTOPTS);
1966 case IPV6_RECVRTHDRDSTOPTS:
1967 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1970 case IPV6_UNICAST_HOPS:
1971 optval = in6p->in6p_hops;
1974 case IPV6_RECVPKTINFO:
1975 optval = OPTBIT(IN6P_PKTINFO);
1978 case IPV6_RECVHOPLIMIT:
1979 optval = OPTBIT(IN6P_HOPLIMIT);
1982 case IPV6_RECVRTHDR:
1983 optval = OPTBIT(IN6P_RTHDR);
1986 case IPV6_RECVPATHMTU:
1987 optval = OPTBIT(IN6P_MTU);
1991 optval = OPTBIT(IN6P_FAITH);
1995 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1998 case IPV6_PORTRANGE:
2001 flags = in6p->in6p_flags;
2002 if (flags & IN6P_HIGHPORT)
2003 optval = IPV6_PORTRANGE_HIGH;
2004 else if (flags & IN6P_LOWPORT)
2005 optval = IPV6_PORTRANGE_LOW;
2010 case IPV6_RECVTCLASS:
2011 optval = OPTBIT(IN6P_TCLASS);
2014 case IPV6_AUTOFLOWLABEL:
2015 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2020 error = sooptcopyout(sopt, &optval,
2027 struct ip6_mtuinfo mtuinfo;
2028 struct route_in6 sro;
2030 bzero(&sro, sizeof(sro));
2032 if (!(so->so_state & SS_ISCONNECTED))
2035 * XXX: we dot not consider the case of source
2036 * routing, or optional information to specify
2037 * the outgoing interface.
2039 error = ip6_getpmtu(&sro, NULL, NULL,
2040 &in6p->in6p_faddr, &pmtu, NULL);
2045 if (pmtu > IPV6_MAXPACKET)
2046 pmtu = IPV6_MAXPACKET;
2048 bzero(&mtuinfo, sizeof(mtuinfo));
2049 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2050 optdata = (void *)&mtuinfo;
2051 optdatalen = sizeof(mtuinfo);
2052 error = sooptcopyout(sopt, optdata,
2057 case IPV6_2292PKTINFO:
2058 case IPV6_2292HOPLIMIT:
2059 case IPV6_2292HOPOPTS:
2060 case IPV6_2292RTHDR:
2061 case IPV6_2292DSTOPTS:
2063 case IPV6_2292PKTINFO:
2064 optval = OPTBIT(IN6P_PKTINFO);
2066 case IPV6_2292HOPLIMIT:
2067 optval = OPTBIT(IN6P_HOPLIMIT);
2069 case IPV6_2292HOPOPTS:
2070 optval = OPTBIT(IN6P_HOPOPTS);
2072 case IPV6_2292RTHDR:
2073 optval = OPTBIT(IN6P_RTHDR);
2075 case IPV6_2292DSTOPTS:
2076 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2079 error = sooptcopyout(sopt, &optval,
2086 case IPV6_RTHDRDSTOPTS:
2090 case IPV6_USE_MIN_MTU:
2091 case IPV6_PREFER_TEMPADDR:
2092 error = ip6_getpcbopt(in6p->in6p_outputopts,
2096 case IPV6_MULTICAST_IF:
2097 case IPV6_MULTICAST_HOPS:
2098 case IPV6_MULTICAST_LOOP:
2099 case IPV6_JOIN_GROUP:
2100 case IPV6_LEAVE_GROUP:
2103 error = ip6_getmoptions(sopt->sopt_name,
2104 in6p->in6p_moptions, &m);
2106 error = sooptcopyout(sopt,
2107 mtod(m, char *), m->m_len);
2112 #if defined(IPSEC) || defined(FAST_IPSEC)
2113 case IPV6_IPSEC_POLICY:
2117 struct mbuf *m = NULL;
2118 struct mbuf **mp = &m;
2119 size_t ovalsize = sopt->sopt_valsize;
2120 caddr_t oval = (caddr_t)sopt->sopt_val;
2122 error = soopt_getm(sopt, &m); /* XXX */
2125 error = soopt_mcopyin(sopt, m); /* XXX */
2128 sopt->sopt_valsize = ovalsize;
2129 sopt->sopt_val = oval;
2131 req = mtod(m, caddr_t);
2134 error = ipsec6_get_policy(in6p, req, len, mp);
2136 error = soopt_mcopyout(sopt, m); /* XXX */
2137 if (error == 0 && m)
2141 #endif /* KAME IPSEC */
2146 struct mbuf **mp = &m;
2148 if (ip6_fw_ctl_ptr == NULL)
2152 error = (*ip6_fw_ctl_ptr)(optname, mp);
2154 error = soopt_mcopyout(sopt, m); /* XXX */
2155 if (error == 0 && m)
2161 error = ENOPROTOOPT;
2166 } else { /* level != IPPROTO_IPV6 */
2173 ip6_raw_ctloutput(so, sopt)
2175 struct sockopt *sopt;
2177 int error = 0, optval, optlen;
2178 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2179 struct in6pcb *in6p = sotoin6pcb(so);
2180 int level, op, optname;
2183 level = sopt->sopt_level;
2184 op = sopt->sopt_dir;
2185 optname = sopt->sopt_name;
2186 optlen = sopt->sopt_valsize;
2188 panic("ip6_raw_ctloutput: arg soopt is NULL");
2190 if (level != IPPROTO_IPV6) {
2197 * For ICMPv6 sockets, no modification allowed for checksum
2198 * offset, permit "no change" values to help existing apps.
2200 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2201 * for an ICMPv6 socket will fail."
2202 * The current behavior does not meet RFC3542.
2206 if (optlen != sizeof(int)) {
2210 error = sooptcopyin(sopt, &optval, sizeof(optval),
2214 if ((optval % 2) != 0) {
2215 /* the API assumes even offset values */
2217 } else if (so->so_proto->pr_protocol ==
2219 if (optval != icmp6off)
2222 in6p->in6p_cksum = optval;
2226 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2229 optval = in6p->in6p_cksum;
2231 error = sooptcopyout(sopt, &optval, sizeof(optval));
2241 error = ENOPROTOOPT;
2249 * Set up IP6 options in pcb for insertion in output packets or
2250 * specifying behavior of outgoing packets.
2253 ip6_pcbopts(pktopt, m, so, sopt)
2254 struct ip6_pktopts **pktopt;
2257 struct sockopt *sopt;
2259 struct ip6_pktopts *opt = *pktopt;
2261 struct thread *td = sopt->sopt_td;
2264 /* turn off any old options. */
2267 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2268 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2269 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2270 printf("ip6_pcbopts: all specified options are cleared.\n");
2272 ip6_clearpktopts(opt, -1);
2274 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2277 if (!m || m->m_len == 0) {
2279 * Only turning off any previous options, regardless of
2280 * whether the opt is just created or given.
2282 free(opt, M_IP6OPT);
2286 /* set options specified by user. */
2287 if (td && !suser(td))
2289 if ((error = ip6_setpktopts(m, opt, NULL, priv,
2290 so->so_proto->pr_protocol)) != 0) {
2291 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2292 free(opt, M_IP6OPT);
2300 * initialize ip6_pktopts. beware that there are non-zero default values in
2304 ip6_initpktopts(opt)
2305 struct ip6_pktopts *opt;
2308 bzero(opt, sizeof(*opt));
2309 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2310 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2311 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2312 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2316 ip6_pcbopt(optname, buf, len, pktopt, priv, uproto)
2317 int optname, len, priv;
2319 struct ip6_pktopts **pktopt;
2322 struct ip6_pktopts *opt;
2324 if (*pktopt == NULL) {
2325 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2327 ip6_initpktopts(*pktopt);
2331 return (ip6_setpktopt(optname, buf, len, opt, priv, 1, 0, uproto));
2335 ip6_getpcbopt(pktopt, optname, sopt)
2336 struct ip6_pktopts *pktopt;
2337 struct sockopt *sopt;
2340 void *optdata = NULL;
2342 struct ip6_ext *ip6e;
2344 struct in6_pktinfo null_pktinfo;
2345 int deftclass = 0, on;
2346 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2347 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2351 if (pktopt && pktopt->ip6po_pktinfo)
2352 optdata = (void *)pktopt->ip6po_pktinfo;
2354 /* XXX: we don't have to do this every time... */
2355 bzero(&null_pktinfo, sizeof(null_pktinfo));
2356 optdata = (void *)&null_pktinfo;
2358 optdatalen = sizeof(struct in6_pktinfo);
2361 if (pktopt && pktopt->ip6po_tclass >= 0)
2362 optdata = (void *)&pktopt->ip6po_tclass;
2364 optdata = (void *)&deftclass;
2365 optdatalen = sizeof(int);
2368 if (pktopt && pktopt->ip6po_hbh) {
2369 optdata = (void *)pktopt->ip6po_hbh;
2370 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2371 optdatalen = (ip6e->ip6e_len + 1) << 3;
2375 if (pktopt && pktopt->ip6po_rthdr) {
2376 optdata = (void *)pktopt->ip6po_rthdr;
2377 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2378 optdatalen = (ip6e->ip6e_len + 1) << 3;
2381 case IPV6_RTHDRDSTOPTS:
2382 if (pktopt && pktopt->ip6po_dest1) {
2383 optdata = (void *)pktopt->ip6po_dest1;
2384 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2385 optdatalen = (ip6e->ip6e_len + 1) << 3;
2389 if (pktopt && pktopt->ip6po_dest2) {
2390 optdata = (void *)pktopt->ip6po_dest2;
2391 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2392 optdatalen = (ip6e->ip6e_len + 1) << 3;
2396 if (pktopt && pktopt->ip6po_nexthop) {
2397 optdata = (void *)pktopt->ip6po_nexthop;
2398 optdatalen = pktopt->ip6po_nexthop->sa_len;
2401 case IPV6_USE_MIN_MTU:
2403 optdata = (void *)&pktopt->ip6po_minmtu;
2405 optdata = (void *)&defminmtu;
2406 optdatalen = sizeof(int);
2409 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2413 optdata = (void *)&on;
2414 optdatalen = sizeof(on);
2416 case IPV6_PREFER_TEMPADDR:
2418 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2420 optdata = (void *)&defpreftemp;
2421 optdatalen = sizeof(int);
2423 default: /* should not happen */
2425 panic("ip6_getpcbopt: unexpected option\n");
2427 return (ENOPROTOOPT);
2430 error = sooptcopyout(sopt, optdata, optdatalen);
2436 ip6_clearpktopts(pktopt, optname)
2437 struct ip6_pktopts *pktopt;
2443 if (optname == -1 || optname == IPV6_PKTINFO) {
2444 if (pktopt->ip6po_pktinfo)
2445 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2446 pktopt->ip6po_pktinfo = NULL;
2448 if (optname == -1 || optname == IPV6_HOPLIMIT)
2449 pktopt->ip6po_hlim = -1;
2450 if (optname == -1 || optname == IPV6_TCLASS)
2451 pktopt->ip6po_tclass = -1;
2452 if (optname == -1 || optname == IPV6_NEXTHOP) {
2453 if (pktopt->ip6po_nextroute.ro_rt) {
2454 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2455 pktopt->ip6po_nextroute.ro_rt = NULL;
2457 if (pktopt->ip6po_nexthop)
2458 free(pktopt->ip6po_nexthop, M_IP6OPT);
2459 pktopt->ip6po_nexthop = NULL;
2461 if (optname == -1 || optname == IPV6_HOPOPTS) {
2462 if (pktopt->ip6po_hbh)
2463 free(pktopt->ip6po_hbh, M_IP6OPT);
2464 pktopt->ip6po_hbh = NULL;
2466 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2467 if (pktopt->ip6po_dest1)
2468 free(pktopt->ip6po_dest1, M_IP6OPT);
2469 pktopt->ip6po_dest1 = NULL;
2471 if (optname == -1 || optname == IPV6_RTHDR) {
2472 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2473 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2474 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2475 if (pktopt->ip6po_route.ro_rt) {
2476 RTFREE(pktopt->ip6po_route.ro_rt);
2477 pktopt->ip6po_route.ro_rt = NULL;
2480 if (optname == -1 || optname == IPV6_DSTOPTS) {
2481 if (pktopt->ip6po_dest2)
2482 free(pktopt->ip6po_dest2, M_IP6OPT);
2483 pktopt->ip6po_dest2 = NULL;
2487 #define PKTOPT_EXTHDRCPY(type) \
2490 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2491 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2492 if (dst->type == NULL && canwait == M_NOWAIT)\
2494 bcopy(src->type, dst->type, hlen);\
2496 } while (/*CONSTCOND*/ 0)
2499 copypktopts(dst, src, canwait)
2500 struct ip6_pktopts *dst, *src;
2503 if (dst == NULL || src == NULL) {
2504 printf("ip6_clearpktopts: invalid argument\n");
2508 dst->ip6po_hlim = src->ip6po_hlim;
2509 dst->ip6po_tclass = src->ip6po_tclass;
2510 dst->ip6po_flags = src->ip6po_flags;
2511 if (src->ip6po_pktinfo) {
2512 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2514 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
2516 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2518 if (src->ip6po_nexthop) {
2519 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2521 if (dst->ip6po_nexthop == NULL)
2523 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2524 src->ip6po_nexthop->sa_len);
2526 PKTOPT_EXTHDRCPY(ip6po_hbh);
2527 PKTOPT_EXTHDRCPY(ip6po_dest1);
2528 PKTOPT_EXTHDRCPY(ip6po_dest2);
2529 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2533 ip6_clearpktopts(dst, -1);
2536 #undef PKTOPT_EXTHDRCPY
2538 struct ip6_pktopts *
2539 ip6_copypktopts(src, canwait)
2540 struct ip6_pktopts *src;
2544 struct ip6_pktopts *dst;
2546 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2547 if (dst == NULL && canwait == M_NOWAIT)
2549 ip6_initpktopts(dst);
2551 if ((error = copypktopts(dst, src, canwait)) != 0) {
2552 free(dst, M_IP6OPT);
2560 ip6_freepcbopts(pktopt)
2561 struct ip6_pktopts *pktopt;
2566 ip6_clearpktopts(pktopt, -1);
2568 free(pktopt, M_IP6OPT);
2572 * Set the IP6 multicast options in response to user setsockopt().
2575 ip6_setmoptions(optname, im6op, m)
2577 struct ip6_moptions **im6op;
2581 u_int loop, ifindex;
2582 struct ipv6_mreq *mreq;
2584 struct ip6_moptions *im6o = *im6op;
2585 struct route_in6 ro;
2586 struct in6_multi_mship *imm;
2587 struct thread *td = curthread;
2591 * No multicast option buffer attached to the pcb;
2592 * allocate one and initialize to default values.
2594 im6o = (struct ip6_moptions *)
2595 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK);
2600 im6o->im6o_multicast_ifp = NULL;
2601 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2602 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2603 LIST_INIT(&im6o->im6o_memberships);
2608 case IPV6_MULTICAST_IF:
2610 * Select the interface for outgoing multicast packets.
2612 if (m == NULL || m->m_len != sizeof(u_int)) {
2616 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2617 if (ifindex < 0 || if_index < ifindex) {
2618 error = ENXIO; /* XXX EINVAL? */
2621 ifp = ifnet_byindex(ifindex);
2622 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2623 error = EADDRNOTAVAIL;
2626 im6o->im6o_multicast_ifp = ifp;
2629 case IPV6_MULTICAST_HOPS:
2632 * Set the IP6 hoplimit for outgoing multicast packets.
2635 if (m == NULL || m->m_len != sizeof(int)) {
2639 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2640 if (optval < -1 || optval >= 256)
2642 else if (optval == -1)
2643 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2645 im6o->im6o_multicast_hlim = optval;
2649 case IPV6_MULTICAST_LOOP:
2651 * Set the loopback flag for outgoing multicast packets.
2652 * Must be zero or one.
2654 if (m == NULL || m->m_len != sizeof(u_int)) {
2658 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2663 im6o->im6o_multicast_loop = loop;
2666 case IPV6_JOIN_GROUP:
2668 * Add a multicast group membership.
2669 * Group must be a valid IP6 multicast address.
2671 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2675 mreq = mtod(m, struct ipv6_mreq *);
2677 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2679 * We use the unspecified address to specify to accept
2680 * all multicast addresses. Only super user is allowed
2687 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2693 * If no interface was explicitly specified, choose an
2694 * appropriate one according to the given multicast address.
2696 if (mreq->ipv6mr_interface == 0) {
2697 struct sockaddr_in6 *dst;
2700 * Look up the routing table for the
2701 * address, and choose the outgoing interface.
2702 * XXX: is it a good approach?
2705 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2706 bzero(dst, sizeof(*dst));
2707 dst->sin6_family = AF_INET6;
2708 dst->sin6_len = sizeof(*dst);
2709 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2710 rtalloc((struct route *)&ro);
2711 if (ro.ro_rt == NULL) {
2712 error = EADDRNOTAVAIL;
2715 ifp = ro.ro_rt->rt_ifp;
2719 * If the interface is specified, validate it.
2721 if (mreq->ipv6mr_interface < 0 ||
2722 if_index < mreq->ipv6mr_interface) {
2723 error = ENXIO; /* XXX EINVAL? */
2726 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2728 error = ENXIO; /* XXX EINVAL? */
2734 * See if we found an interface, and confirm that it
2735 * supports multicast
2737 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2738 error = EADDRNOTAVAIL;
2742 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2743 error = EADDRNOTAVAIL; /* XXX: should not happen */
2748 * See if the membership already exists.
2750 for (imm = im6o->im6o_memberships.lh_first;
2751 imm != NULL; imm = imm->i6mm_chain.le_next)
2752 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2753 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2754 &mreq->ipv6mr_multiaddr))
2761 * Everything looks good; add a new record to the multicast
2762 * address list for the given interface.
2764 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2767 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2770 case IPV6_LEAVE_GROUP:
2772 * Drop a multicast group membership.
2773 * Group must be a valid IP6 multicast address.
2775 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2779 mreq = mtod(m, struct ipv6_mreq *);
2782 * If an interface address was specified, get a pointer
2783 * to its ifnet structure.
2785 if (mreq->ipv6mr_interface < 0 ||
2786 if_index < mreq->ipv6mr_interface) {
2787 error = ENXIO; /* XXX EINVAL? */
2790 if (mreq->ipv6mr_interface == 0)
2793 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2795 /* Fill in the scope zone ID */
2797 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2798 /* XXX: should not happen */
2799 error = EADDRNOTAVAIL;
2802 } else if (mreq->ipv6mr_interface != 0) {
2804 * This case happens when the (positive) index is in
2805 * the valid range, but the corresponding interface has
2806 * been detached dynamically (XXX).
2808 error = EADDRNOTAVAIL;
2810 } else { /* ipv6mr_interface == 0 */
2811 struct sockaddr_in6 sa6_mc;
2814 * The API spec says as follows:
2815 * If the interface index is specified as 0, the
2816 * system may choose a multicast group membership to
2817 * drop by matching the multicast address only.
2818 * On the other hand, we cannot disambiguate the scope
2819 * zone unless an interface is provided. Thus, we
2820 * check if there's ambiguity with the default scope
2821 * zone as the last resort.
2823 bzero(&sa6_mc, sizeof(sa6_mc));
2824 sa6_mc.sin6_family = AF_INET6;
2825 sa6_mc.sin6_len = sizeof(sa6_mc);
2826 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2827 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2830 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2834 * Find the membership in the membership list.
2836 for (imm = im6o->im6o_memberships.lh_first;
2837 imm != NULL; imm = imm->i6mm_chain.le_next) {
2838 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2839 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2840 &mreq->ipv6mr_multiaddr))
2844 /* Unable to resolve interface */
2845 error = EADDRNOTAVAIL;
2849 * Give up the multicast address record to which the
2850 * membership points.
2852 LIST_REMOVE(imm, i6mm_chain);
2853 in6_delmulti(imm->i6mm_maddr);
2854 free(imm, M_IP6MADDR);
2863 * If all options have default values, no need to keep the mbuf.
2865 if (im6o->im6o_multicast_ifp == NULL &&
2866 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2867 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2868 im6o->im6o_memberships.lh_first == NULL) {
2869 free(*im6op, M_IP6MOPTS);
2877 * Return the IP6 multicast options in response to user getsockopt().
2880 ip6_getmoptions(optname, im6o, mp)
2882 struct ip6_moptions *im6o;
2885 u_int *hlim, *loop, *ifindex;
2887 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2891 case IPV6_MULTICAST_IF:
2892 ifindex = mtod(*mp, u_int *);
2893 (*mp)->m_len = sizeof(u_int);
2894 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2897 *ifindex = im6o->im6o_multicast_ifp->if_index;
2900 case IPV6_MULTICAST_HOPS:
2901 hlim = mtod(*mp, u_int *);
2902 (*mp)->m_len = sizeof(u_int);
2904 *hlim = ip6_defmcasthlim;
2906 *hlim = im6o->im6o_multicast_hlim;
2909 case IPV6_MULTICAST_LOOP:
2910 loop = mtod(*mp, u_int *);
2911 (*mp)->m_len = sizeof(u_int);
2913 *loop = ip6_defmcasthlim;
2915 *loop = im6o->im6o_multicast_loop;
2919 return (EOPNOTSUPP);
2924 * Discard the IP6 multicast options.
2927 ip6_freemoptions(im6o)
2928 struct ip6_moptions *im6o;
2930 struct in6_multi_mship *imm;
2935 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2936 LIST_REMOVE(imm, i6mm_chain);
2937 if (imm->i6mm_maddr)
2938 in6_delmulti(imm->i6mm_maddr);
2939 free(imm, M_IP6MADDR);
2941 free(im6o, M_IP6MOPTS);
2945 * Set IPv6 outgoing packet options based on advanced API.
2948 ip6_setpktopts(control, opt, stickyopt, priv, uproto)
2949 struct mbuf *control;
2950 struct ip6_pktopts *opt, *stickyopt;
2953 struct cmsghdr *cm = 0;
2955 if (control == NULL || opt == NULL)
2958 ip6_initpktopts(opt);
2963 * If stickyopt is provided, make a local copy of the options
2964 * for this particular packet, then override them by ancillary
2966 * XXX: copypktopts() does not copy the cached route to a next
2967 * hop (if any). This is not very good in terms of efficiency,
2968 * but we can allow this since this option should be rarely
2971 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2976 * XXX: Currently, we assume all the optional information is stored
2979 if (control->m_next)
2982 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2983 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2986 if (control->m_len < CMSG_LEN(0))
2989 cm = mtod(control, struct cmsghdr *);
2990 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2992 if (cm->cmsg_level != IPPROTO_IPV6)
2995 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2996 cm->cmsg_len - CMSG_LEN(0), opt, priv, 0, 1, uproto);
3005 * Set a particular packet option, as a sticky option or an ancillary data
3006 * item. "len" can be 0 only when it's a sticky option.
3007 * We have 4 cases of combination of "sticky" and "cmsg":
3008 * "sticky=0, cmsg=0": impossible
3009 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3010 * "sticky=1, cmsg=0": RFC3542 socket option
3011 * "sticky=1, cmsg=1": RFC2292 socket option
3014 ip6_setpktopt(optname, buf, len, opt, priv, sticky, cmsg, uproto)
3015 int optname, len, priv, sticky, cmsg, uproto;
3017 struct ip6_pktopts *opt;
3019 int minmtupolicy, preftemp;
3021 if (!sticky && !cmsg) {
3023 printf("ip6_setpktopt: impossible case\n");
3029 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3030 * not be specified in the context of RFC3542. Conversely,
3031 * RFC3542 types should not be specified in the context of RFC2292.
3035 case IPV6_2292PKTINFO:
3036 case IPV6_2292HOPLIMIT:
3037 case IPV6_2292NEXTHOP:
3038 case IPV6_2292HOPOPTS:
3039 case IPV6_2292DSTOPTS:
3040 case IPV6_2292RTHDR:
3041 case IPV6_2292PKTOPTIONS:
3042 return (ENOPROTOOPT);
3045 if (sticky && cmsg) {
3052 case IPV6_RTHDRDSTOPTS:
3054 case IPV6_USE_MIN_MTU:
3057 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3058 return (ENOPROTOOPT);
3063 case IPV6_2292PKTINFO:
3066 struct ifnet *ifp = NULL;
3067 struct in6_pktinfo *pktinfo;
3069 if (len != sizeof(struct in6_pktinfo))
3072 pktinfo = (struct in6_pktinfo *)buf;
3075 * An application can clear any sticky IPV6_PKTINFO option by
3076 * doing a "regular" setsockopt with ipi6_addr being
3077 * in6addr_any and ipi6_ifindex being zero.
3078 * [RFC 3542, Section 6]
3080 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3081 pktinfo->ipi6_ifindex == 0 &&
3082 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3083 ip6_clearpktopts(opt, optname);
3087 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3088 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3092 /* validate the interface index if specified. */
3093 if (pktinfo->ipi6_ifindex > if_index ||
3094 pktinfo->ipi6_ifindex < 0) {
3097 if (pktinfo->ipi6_ifindex) {
3098 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
3104 * We store the address anyway, and let in6_selectsrc()
3105 * validate the specified address. This is because ipi6_addr
3106 * may not have enough information about its scope zone, and
3107 * we may need additional information (such as outgoing
3108 * interface or the scope zone of a destination address) to
3109 * disambiguate the scope.
3110 * XXX: the delay of the validation may confuse the
3111 * application when it is used as a sticky option.
3113 if (opt->ip6po_pktinfo == NULL) {
3114 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
3115 M_IP6OPT, M_NOWAIT);
3116 if (opt->ip6po_pktinfo == NULL)
3119 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
3123 case IPV6_2292HOPLIMIT:
3129 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3130 * to simplify the ordering among hoplimit options.
3132 if (optname == IPV6_HOPLIMIT && sticky)
3133 return (ENOPROTOOPT);
3135 if (len != sizeof(int))
3138 if (*hlimp < -1 || *hlimp > 255)
3141 opt->ip6po_hlim = *hlimp;
3149 if (len != sizeof(int))
3151 tclass = *(int *)buf;
3152 if (tclass < -1 || tclass > 255)
3155 opt->ip6po_tclass = tclass;
3159 case IPV6_2292NEXTHOP:
3164 if (len == 0) { /* just remove the option */
3165 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3169 /* check if cmsg_len is large enough for sa_len */
3170 if (len < sizeof(struct sockaddr) || len < *buf)
3173 switch (((struct sockaddr *)buf)->sa_family) {
3176 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3179 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3182 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3183 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3186 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3192 case AF_LINK: /* should eventually be supported */
3194 return (EAFNOSUPPORT);
3197 /* turn off the previous option, then set the new option. */
3198 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3199 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3200 if (opt->ip6po_nexthop == NULL)
3202 bcopy(buf, opt->ip6po_nexthop, *buf);
3205 case IPV6_2292HOPOPTS:
3208 struct ip6_hbh *hbh;
3212 * XXX: We don't allow a non-privileged user to set ANY HbH
3213 * options, since per-option restriction has too much
3220 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3221 break; /* just remove the option */
3224 /* message length validation */
3225 if (len < sizeof(struct ip6_hbh))
3227 hbh = (struct ip6_hbh *)buf;
3228 hbhlen = (hbh->ip6h_len + 1) << 3;
3232 /* turn off the previous option, then set the new option. */
3233 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3234 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3235 if (opt->ip6po_hbh == NULL)
3237 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3242 case IPV6_2292DSTOPTS:
3244 case IPV6_RTHDRDSTOPTS:
3246 struct ip6_dest *dest, **newdest = NULL;
3249 if (!priv) /* XXX: see the comment for IPV6_HOPOPTS */
3253 ip6_clearpktopts(opt, optname);
3254 break; /* just remove the option */
3257 /* message length validation */
3258 if (len < sizeof(struct ip6_dest))
3260 dest = (struct ip6_dest *)buf;
3261 destlen = (dest->ip6d_len + 1) << 3;
3266 * Determine the position that the destination options header
3267 * should be inserted; before or after the routing header.
3270 case IPV6_2292DSTOPTS:
3272 * The old advacned API is ambiguous on this point.
3273 * Our approach is to determine the position based
3274 * according to the existence of a routing header.
3275 * Note, however, that this depends on the order of the
3276 * extension headers in the ancillary data; the 1st
3277 * part of the destination options header must appear
3278 * before the routing header in the ancillary data,
3280 * RFC3542 solved the ambiguity by introducing
3281 * separate ancillary data or option types.
3283 if (opt->ip6po_rthdr == NULL)
3284 newdest = &opt->ip6po_dest1;
3286 newdest = &opt->ip6po_dest2;
3288 case IPV6_RTHDRDSTOPTS:
3289 newdest = &opt->ip6po_dest1;
3292 newdest = &opt->ip6po_dest2;
3296 /* turn off the previous option, then set the new option. */
3297 ip6_clearpktopts(opt, optname);
3298 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3299 if (*newdest == NULL)
3301 bcopy(dest, *newdest, destlen);
3306 case IPV6_2292RTHDR:
3309 struct ip6_rthdr *rth;
3313 ip6_clearpktopts(opt, IPV6_RTHDR);
3314 break; /* just remove the option */
3317 /* message length validation */
3318 if (len < sizeof(struct ip6_rthdr))
3320 rth = (struct ip6_rthdr *)buf;
3321 rthlen = (rth->ip6r_len + 1) << 3;
3325 switch (rth->ip6r_type) {
3326 case IPV6_RTHDR_TYPE_0:
3327 if (rth->ip6r_len == 0) /* must contain one addr */
3329 if (rth->ip6r_len % 2) /* length must be even */
3331 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3335 return (EINVAL); /* not supported */
3338 /* turn off the previous option */
3339 ip6_clearpktopts(opt, IPV6_RTHDR);
3340 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3341 if (opt->ip6po_rthdr == NULL)
3343 bcopy(rth, opt->ip6po_rthdr, rthlen);
3348 case IPV6_USE_MIN_MTU:
3349 if (len != sizeof(int))
3351 minmtupolicy = *(int *)buf;
3352 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3353 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3354 minmtupolicy != IP6PO_MINMTU_ALL) {
3357 opt->ip6po_minmtu = minmtupolicy;
3361 if (len != sizeof(int))
3364 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3366 * we ignore this option for TCP sockets.
3367 * (RFC3542 leaves this case unspecified.)
3369 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3371 opt->ip6po_flags |= IP6PO_DONTFRAG;
3374 case IPV6_PREFER_TEMPADDR:
3375 if (len != sizeof(int))
3377 preftemp = *(int *)buf;
3378 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3379 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3380 preftemp != IP6PO_TEMPADDR_PREFER) {
3383 opt->ip6po_prefer_tempaddr = preftemp;
3387 return (ENOPROTOOPT);
3388 } /* end of switch */
3394 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3395 * packet to the input queue of a specified interface. Note that this
3396 * calls the output routine of the loopback "driver", but with an interface
3397 * pointer that might NOT be &loif -- easier than replicating that code here.
3400 ip6_mloopback(ifp, m, dst)
3403 struct sockaddr_in6 *dst;
3406 struct ip6_hdr *ip6;
3408 copym = m_copy(m, 0, M_COPYALL);
3413 * Make sure to deep-copy IPv6 header portion in case the data
3414 * is in an mbuf cluster, so that we can safely override the IPv6
3415 * header portion later.
3417 if ((copym->m_flags & M_EXT) != 0 ||
3418 copym->m_len < sizeof(struct ip6_hdr)) {
3419 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3425 if (copym->m_len < sizeof(*ip6)) {
3431 ip6 = mtod(copym, struct ip6_hdr *);
3433 * clear embedded scope identifiers if necessary.
3434 * in6_clearscope will touch the addresses only when necessary.
3436 in6_clearscope(&ip6->ip6_src);
3437 in6_clearscope(&ip6->ip6_dst);
3439 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3443 * Chop IPv6 header off from the payload.
3446 ip6_splithdr(m, exthdrs)
3448 struct ip6_exthdrs *exthdrs;
3451 struct ip6_hdr *ip6;
3453 ip6 = mtod(m, struct ip6_hdr *);
3454 if (m->m_len > sizeof(*ip6)) {
3455 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3460 M_MOVE_PKTHDR(mh, m);
3461 MH_ALIGN(mh, sizeof(*ip6));
3462 m->m_len -= sizeof(*ip6);
3463 m->m_data += sizeof(*ip6);
3466 m->m_len = sizeof(*ip6);
3467 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3469 exthdrs->ip6e_ip6 = m;
3474 * Compute IPv6 extension header length.
3478 struct in6pcb *in6p;
3482 if (!in6p->in6p_outputopts)
3487 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3489 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3490 if (in6p->in6p_outputopts->ip6po_rthdr)
3491 /* dest1 is valid with rthdr only */
3492 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3493 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3494 len += elen(in6p->in6p_outputopts->ip6po_dest2);