2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/errno.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/ucred.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 <netipsec/ipsec.h>
99 #include <netipsec/ipsec6.h>
100 #include <netipsec/key.h>
101 #include <netinet6/ip6_ipsec.h>
104 #include <netinet6/ip6protosw.h>
105 #include <netinet6/scope6_var.h>
107 static MALLOC_DEFINE(M_IP6MOPTS, "ip6_moptions", "internet multicast options");
110 struct mbuf *ip6e_ip6;
111 struct mbuf *ip6e_hbh;
112 struct mbuf *ip6e_dest1;
113 struct mbuf *ip6e_rthdr;
114 struct mbuf *ip6e_dest2;
117 static int ip6_pcbopt __P((int, u_char *, int, struct ip6_pktopts **,
118 struct ucred *, int));
119 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
120 struct socket *, struct sockopt *));
121 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
122 static int ip6_setpktopt __P((int, u_char *, int, struct ip6_pktopts *,
123 struct ucred *, int, int, int));
125 static int ip6_setmoptions(int, struct ip6_moptions **, struct mbuf *);
126 static int ip6_getmoptions(int, struct ip6_moptions *, struct mbuf **);
127 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
128 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
129 struct ip6_frag **));
130 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
131 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
132 static int ip6_getpmtu __P((struct route_in6 *, struct route_in6 *,
133 struct ifnet *, struct in6_addr *, u_long *, int *));
134 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
138 * Make an extension header from option data. hp is the source, and
139 * mp is the destination.
141 #define MAKE_EXTHDR(hp, mp) \
144 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
145 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
146 ((eh)->ip6e_len + 1) << 3); \
150 } while (/*CONSTCOND*/ 0)
153 * Form a chain of extension headers.
154 * m is the extension header mbuf
155 * mp is the previous mbuf in the chain
156 * p is the next header
157 * i is the type of option.
159 #define MAKE_CHAIN(m, mp, p, i)\
163 panic("assumption failed: hdr not split"); \
164 *mtod((m), u_char *) = *(p);\
166 p = mtod((m), u_char *);\
167 (m)->m_next = (mp)->m_next;\
171 } while (/*CONSTCOND*/ 0)
174 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
175 * header (with pri, len, nxt, hlim, src, dst).
176 * This function may modify ver and hlim only.
177 * The mbuf chain containing the packet will be freed.
178 * The mbuf opt, if present, will not be freed.
180 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
181 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
182 * which is rt_rmx.rmx_mtu.
184 * ifpp - XXX: just for statistics
187 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
188 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
189 struct ifnet **ifpp, struct inpcb *inp)
191 struct ip6_hdr *ip6, *mhip6;
192 struct ifnet *ifp, *origifp;
194 struct mbuf *mprev = NULL;
195 int hlen, tlen, len, off;
196 struct route_in6 ip6route;
197 struct rtentry *rt = NULL;
198 struct sockaddr_in6 *dst, src_sa, dst_sa;
199 struct in6_addr odst;
201 struct in6_ifaddr *ia = NULL;
203 int alwaysfrag, dontfrag;
204 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
205 struct ip6_exthdrs exthdrs;
206 struct in6_addr finaldst, src0, dst0;
208 struct route_in6 *ro_pmtu = NULL;
212 struct ipsec_output_state state;
213 struct ip6_rthdr *rh = NULL;
214 int needipsectun = 0;
216 struct secpolicy *sp = NULL;
219 ip6 = mtod(m, struct ip6_hdr *);
221 printf ("ip6 is NULL");
225 finaldst = ip6->ip6_dst;
227 bzero(&exthdrs, sizeof(exthdrs));
230 /* Hop-by-Hop options header */
231 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
232 /* Destination options header(1st part) */
233 if (opt->ip6po_rthdr) {
235 * Destination options header(1st part)
236 * This only makes sense with a routing header.
237 * See Section 9.2 of RFC 3542.
238 * Disabling this part just for MIP6 convenience is
239 * a bad idea. We need to think carefully about a
240 * way to make the advanced API coexist with MIP6
241 * options, which might automatically be inserted in
244 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
247 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
248 /* Destination options header(2nd part) */
249 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
253 * IPSec checking which handles several cases.
254 * FAST IPSEC: We re-injected the packet.
257 switch(ip6_ipsec_output(&m, inp, &flags, &error, &ifp, &sp))
259 case 1: /* Bad packet */
261 case -1: /* Do IPSec */
263 case 0: /* No IPSec */
270 * Calculate the total length of the extension header chain.
271 * Keep the length of the unfragmentable part for fragmentation.
274 if (exthdrs.ip6e_hbh)
275 optlen += exthdrs.ip6e_hbh->m_len;
276 if (exthdrs.ip6e_dest1)
277 optlen += exthdrs.ip6e_dest1->m_len;
278 if (exthdrs.ip6e_rthdr)
279 optlen += exthdrs.ip6e_rthdr->m_len;
280 unfragpartlen = optlen + sizeof(struct ip6_hdr);
282 /* NOTE: we don't add AH/ESP length here. do that later. */
283 if (exthdrs.ip6e_dest2)
284 optlen += exthdrs.ip6e_dest2->m_len;
287 * If we need IPsec, or there is at least one extension header,
288 * separate IP6 header from the payload.
290 if ((needipsec || optlen) && !hdrsplit) {
291 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
295 m = exthdrs.ip6e_ip6;
300 ip6 = mtod(m, struct ip6_hdr *);
302 /* adjust mbuf packet header length */
303 m->m_pkthdr.len += optlen;
304 plen = m->m_pkthdr.len - sizeof(*ip6);
306 /* If this is a jumbo payload, insert a jumbo payload option. */
307 if (plen > IPV6_MAXPACKET) {
309 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
313 m = exthdrs.ip6e_ip6;
317 ip6 = mtod(m, struct ip6_hdr *);
318 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
322 ip6->ip6_plen = htons(plen);
325 * Concatenate headers and fill in next header fields.
326 * Here we have, on "m"
328 * and we insert headers accordingly. Finally, we should be getting:
329 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
331 * during the header composing process, "m" points to IPv6 header.
332 * "mprev" points to an extension header prior to esp.
334 u_char *nexthdrp = &ip6->ip6_nxt;
338 * we treat dest2 specially. this makes IPsec processing
339 * much easier. the goal here is to make mprev point the
340 * mbuf prior to dest2.
342 * result: IPv6 dest2 payload
343 * m and mprev will point to IPv6 header.
345 if (exthdrs.ip6e_dest2) {
347 panic("assumption failed: hdr not split");
348 exthdrs.ip6e_dest2->m_next = m->m_next;
349 m->m_next = exthdrs.ip6e_dest2;
350 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
351 ip6->ip6_nxt = IPPROTO_DSTOPTS;
355 * result: IPv6 hbh dest1 rthdr dest2 payload
356 * m will point to IPv6 header. mprev will point to the
357 * extension header prior to dest2 (rthdr in the above case).
359 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
360 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
362 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
370 * pointers after IPsec headers are not valid any more.
371 * other pointers need a great care too.
372 * (IPsec routines should not mangle mbufs prior to AH/ESP)
374 exthdrs.ip6e_dest2 = NULL;
376 if (exthdrs.ip6e_rthdr) {
377 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
378 segleft_org = rh->ip6r_segleft;
379 rh->ip6r_segleft = 0;
382 bzero(&state, sizeof(state));
384 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
387 if (error == EJUSTRETURN) {
389 * We had a SP with a level of 'use' and no SA. We
390 * will just continue to process the packet without
395 /* mbuf is already reclaimed in ipsec6_output_trans. */
405 printf("[%s:%d] (ipsec): error code %d\n",
406 __func__, __LINE__, error);
409 /* don't show these error codes to the user */
414 } else if (!needipsectun) {
416 * In the FAST IPSec case we have already
417 * re-injected the packet and it has been freed
418 * by the ipsec_done() function. So, just clean
419 * up after ourselves.
424 if (exthdrs.ip6e_rthdr) {
425 /* ah6_output doesn't modify mbuf chain */
426 rh->ip6r_segleft = segleft_org;
432 * If there is a routing header, replace the destination address field
433 * with the first hop of the routing header.
435 if (exthdrs.ip6e_rthdr) {
436 struct ip6_rthdr *rh =
437 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
438 struct ip6_rthdr *));
439 struct ip6_rthdr0 *rh0;
440 struct in6_addr *addr;
441 struct sockaddr_in6 sa;
443 switch (rh->ip6r_type) {
444 case IPV6_RTHDR_TYPE_0:
445 rh0 = (struct ip6_rthdr0 *)rh;
446 addr = (struct in6_addr *)(rh0 + 1);
449 * construct a sockaddr_in6 form of
452 * XXX: we may not have enough
453 * information about its scope zone;
454 * there is no standard API to pass
455 * the information from the
458 bzero(&sa, sizeof(sa));
459 sa.sin6_family = AF_INET6;
460 sa.sin6_len = sizeof(sa);
461 sa.sin6_addr = addr[0];
462 if ((error = sa6_embedscope(&sa,
463 ip6_use_defzone)) != 0) {
466 ip6->ip6_dst = sa.sin6_addr;
467 bcopy(&addr[1], &addr[0], sizeof(struct in6_addr)
468 * (rh0->ip6r0_segleft - 1));
469 addr[rh0->ip6r0_segleft - 1] = finaldst;
471 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
473 default: /* is it possible? */
479 /* Source address validation */
480 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
481 (flags & IPV6_UNSPECSRC) == 0) {
483 ip6stat.ip6s_badscope++;
486 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
488 ip6stat.ip6s_badscope++;
492 ip6stat.ip6s_localout++;
499 bzero((caddr_t)ro, sizeof(*ro));
502 if (opt && opt->ip6po_rthdr)
503 ro = &opt->ip6po_route;
504 dst = (struct sockaddr_in6 *)&ro->ro_dst;
508 * if specified, try to fill in the traffic class field.
509 * do not override if a non-zero value is already set.
510 * we check the diffserv field and the ecn field separately.
512 if (opt && opt->ip6po_tclass >= 0) {
515 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
517 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
520 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
523 /* fill in or override the hop limit field, if necessary. */
524 if (opt && opt->ip6po_hlim != -1)
525 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
526 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
528 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
530 ip6->ip6_hlim = ip6_defmcasthlim;
535 * We may re-inject packets into the stack here.
537 if (needipsec && needipsectun) {
538 struct ipsec_output_state state;
541 * All the extension headers will become inaccessible
542 * (since they can be encrypted).
543 * Don't panic, we need no more updates to extension headers
544 * on inner IPv6 packet (since they are now encapsulated).
546 * IPv6 [ESP|AH] IPv6 [extension headers] payload
548 bzero(&exthdrs, sizeof(exthdrs));
549 exthdrs.ip6e_ip6 = m;
551 bzero(&state, sizeof(state));
553 state.ro = (struct route *)ro;
554 state.dst = (struct sockaddr *)dst;
556 error = ipsec6_output_tunnel(&state, sp, flags);
559 ro = (struct route_in6 *)state.ro;
560 dst = (struct sockaddr_in6 *)state.dst;
561 if (error == EJUSTRETURN) {
563 * We had a SP with a level of 'use' and no SA. We
564 * will just continue to process the packet without
569 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
580 printf("[%s:%d] (ipsec): error code %d\n",
581 __func__, __LINE__, error);
584 /* don't show these error codes to the user */
591 * In the FAST IPSec case we have already
592 * re-injected the packet and it has been freed
593 * by the ipsec_done() function. So, just clean
594 * up after ourselves.
600 exthdrs.ip6e_ip6 = m;
605 ip6 = mtod(m, struct ip6_hdr *);
607 bzero(&dst_sa, sizeof(dst_sa));
608 dst_sa.sin6_family = AF_INET6;
609 dst_sa.sin6_len = sizeof(dst_sa);
610 dst_sa.sin6_addr = ip6->ip6_dst;
611 if ((error = in6_selectroute(&dst_sa, opt, im6o, ro,
612 &ifp, &rt, 0)) != 0) {
615 ip6stat.ip6s_noroute++;
619 break; /* XXX statistics? */
622 in6_ifstat_inc(ifp, ifs6_out_discard);
627 * If in6_selectroute() does not return a route entry,
628 * dst may not have been updated.
630 *dst = dst_sa; /* XXX */
634 * then rt (for unicast) and ifp must be non-NULL valid values.
636 if ((flags & IPV6_FORWARDING) == 0) {
637 /* XXX: the FORWARDING flag can be set for mrouting. */
638 in6_ifstat_inc(ifp, ifs6_out_request);
641 ia = (struct in6_ifaddr *)(rt->rt_ifa);
646 * The outgoing interface must be in the zone of source and
647 * destination addresses. We should use ia_ifp to support the
648 * case of sending packets to an address of our own.
650 if (ia != NULL && ia->ia_ifp)
651 origifp = ia->ia_ifp;
656 if (in6_setscope(&src0, origifp, &zone))
658 bzero(&src_sa, sizeof(src_sa));
659 src_sa.sin6_family = AF_INET6;
660 src_sa.sin6_len = sizeof(src_sa);
661 src_sa.sin6_addr = ip6->ip6_src;
662 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
666 if (in6_setscope(&dst0, origifp, &zone))
668 /* re-initialize to be sure */
669 bzero(&dst_sa, sizeof(dst_sa));
670 dst_sa.sin6_family = AF_INET6;
671 dst_sa.sin6_len = sizeof(dst_sa);
672 dst_sa.sin6_addr = ip6->ip6_dst;
673 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id) {
677 /* scope check is done. */
681 ip6stat.ip6s_badscope++;
682 in6_ifstat_inc(origifp, ifs6_out_discard);
684 error = EHOSTUNREACH; /* XXX */
688 if (rt && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
689 if (opt && opt->ip6po_nextroute.ro_rt) {
691 * The nexthop is explicitly specified by the
692 * application. We assume the next hop is an IPv6
695 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
697 else if ((rt->rt_flags & RTF_GATEWAY))
698 dst = (struct sockaddr_in6 *)rt->rt_gateway;
701 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
702 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
704 struct in6_multi *in6m;
706 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
708 in6_ifstat_inc(ifp, ifs6_out_mcast);
711 * Confirm that the outgoing interface supports multicast.
713 if (!(ifp->if_flags & IFF_MULTICAST)) {
714 ip6stat.ip6s_noroute++;
715 in6_ifstat_inc(ifp, ifs6_out_discard);
719 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
721 (im6o == NULL || im6o->im6o_multicast_loop)) {
723 * If we belong to the destination multicast group
724 * on the outgoing interface, and the caller did not
725 * forbid loopback, loop back a copy.
727 ip6_mloopback(ifp, m, dst);
730 * If we are acting as a multicast router, perform
731 * multicast forwarding as if the packet had just
732 * arrived on the interface to which we are about
733 * to send. The multicast forwarding function
734 * recursively calls this function, using the
735 * IPV6_FORWARDING flag to prevent infinite recursion.
737 * Multicasts that are looped back by ip6_mloopback(),
738 * above, will be forwarded by the ip6_input() routine,
741 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
743 * XXX: ip6_mforward expects that rcvif is NULL
744 * when it is called from the originating path.
745 * However, it is not always the case, since
746 * some versions of MGETHDR() does not
747 * initialize the field.
749 m->m_pkthdr.rcvif = NULL;
750 if (ip6_mforward(ip6, ifp, m) != 0) {
757 * Multicasts with a hoplimit of zero may be looped back,
758 * above, but must not be transmitted on a network.
759 * Also, multicasts addressed to the loopback interface
760 * are not sent -- the above call to ip6_mloopback() will
761 * loop back a copy if this host actually belongs to the
762 * destination group on the loopback interface.
764 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
765 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
772 * Fill the outgoing inteface to tell the upper layer
773 * to increment per-interface statistics.
778 /* Determine path MTU. */
779 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
784 * The caller of this function may specify to use the minimum MTU
786 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
787 * setting. The logic is a bit complicated; by default, unicast
788 * packets will follow path MTU while multicast packets will be sent at
789 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
790 * including unicast ones will be sent at the minimum MTU. Multicast
791 * packets will always be sent at the minimum MTU unless
792 * IP6PO_MINMTU_DISABLE is explicitly specified.
793 * See RFC 3542 for more details.
795 if (mtu > IPV6_MMTU) {
796 if ((flags & IPV6_MINMTU))
798 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
800 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
802 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
808 * clear embedded scope identifiers if necessary.
809 * in6_clearscope will touch the addresses only when necessary.
811 in6_clearscope(&ip6->ip6_src);
812 in6_clearscope(&ip6->ip6_dst);
815 * If the outgoing packet contains a hop-by-hop options header,
816 * it must be examined and processed even by the source node.
817 * (RFC 2460, section 4.)
819 if (exthdrs.ip6e_hbh) {
820 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
821 u_int32_t dummy; /* XXX unused */
822 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
825 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
826 panic("ip6e_hbh is not continuous");
829 * XXX: if we have to send an ICMPv6 error to the sender,
830 * we need the M_LOOP flag since icmp6_error() expects
831 * the IPv6 and the hop-by-hop options header are
832 * continuous unless the flag is set.
834 m->m_flags |= M_LOOP;
835 m->m_pkthdr.rcvif = ifp;
836 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
837 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
838 &dummy, &plen) < 0) {
839 /* m was already freed at this point */
840 error = EINVAL;/* better error? */
843 m->m_flags &= ~M_LOOP; /* XXX */
844 m->m_pkthdr.rcvif = NULL;
847 /* Jump over all PFIL processing if hooks are not active. */
848 if (!PFIL_HOOKED(&inet6_pfil_hook))
852 /* Run through list of hooks for output packets. */
853 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT, inp);
854 if (error != 0 || m == NULL)
856 ip6 = mtod(m, struct ip6_hdr *);
858 /* See if destination IP address was changed by packet filter. */
859 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
860 m->m_flags |= M_SKIP_FIREWALL;
861 /* If destination is now ourself drop to ip6_input(). */
862 if (in6_localaddr(&ip6->ip6_dst)) {
863 if (m->m_pkthdr.rcvif == NULL)
864 m->m_pkthdr.rcvif = loif;
865 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
866 m->m_pkthdr.csum_flags |=
867 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
868 m->m_pkthdr.csum_data = 0xffff;
870 m->m_pkthdr.csum_flags |=
871 CSUM_IP_CHECKED | CSUM_IP_VALID;
872 error = netisr_queue(NETISR_IPV6, m);
875 goto again; /* Redo the routing table lookup. */
878 /* XXX: IPFIREWALL_FORWARD */
882 * Send the packet to the outgoing interface.
883 * If necessary, do IPv6 fragmentation before sending.
885 * the logic here is rather complex:
886 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
887 * 1-a: send as is if tlen <= path mtu
888 * 1-b: fragment if tlen > path mtu
890 * 2: if user asks us not to fragment (dontfrag == 1)
891 * 2-a: send as is if tlen <= interface mtu
892 * 2-b: error if tlen > interface mtu
894 * 3: if we always need to attach fragment header (alwaysfrag == 1)
897 * 4: if dontfrag == 1 && alwaysfrag == 1
898 * error, as we cannot handle this conflicting request
900 tlen = m->m_pkthdr.len;
902 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
906 if (dontfrag && alwaysfrag) { /* case 4 */
907 /* conflicting request - can't transmit */
911 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
913 * Even if the DONTFRAG option is specified, we cannot send the
914 * packet when the data length is larger than the MTU of the
915 * outgoing interface.
916 * Notify the error by sending IPV6_PATHMTU ancillary data as
917 * well as returning an error code (the latter is not described
921 struct ip6ctlparam ip6cp;
923 mtu32 = (u_int32_t)mtu;
924 bzero(&ip6cp, sizeof(ip6cp));
925 ip6cp.ip6c_cmdarg = (void *)&mtu32;
926 pfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
934 * transmit packet without fragmentation
936 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
937 struct in6_ifaddr *ia6;
939 ip6 = mtod(m, struct ip6_hdr *);
940 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
942 /* Record statistics for this interface address. */
943 ia6->ia_ifa.if_opackets++;
944 ia6->ia_ifa.if_obytes += m->m_pkthdr.len;
946 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
951 * try to fragment the packet. case 1-b and 3
953 if (mtu < IPV6_MMTU) {
954 /* path MTU cannot be less than IPV6_MMTU */
956 in6_ifstat_inc(ifp, ifs6_out_fragfail);
958 } else if (ip6->ip6_plen == 0) {
959 /* jumbo payload cannot be fragmented */
961 in6_ifstat_inc(ifp, ifs6_out_fragfail);
964 struct mbuf **mnext, *m_frgpart;
965 struct ip6_frag *ip6f;
966 u_int32_t id = htonl(ip6_randomid());
969 int qslots = ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len;
972 * Too large for the destination or interface;
973 * fragment if possible.
974 * Must be able to put at least 8 bytes per fragment.
976 hlen = unfragpartlen;
977 if (mtu > IPV6_MAXPACKET)
978 mtu = IPV6_MAXPACKET;
980 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
983 in6_ifstat_inc(ifp, ifs6_out_fragfail);
988 * Verify that we have any chance at all of being able to queue
989 * the packet or packet fragments
991 if (qslots <= 0 || ((u_int)qslots * (mtu - hlen)
992 < tlen /* - hlen */)) {
994 ip6stat.ip6s_odropped++;
998 mnext = &m->m_nextpkt;
1001 * Change the next header field of the last header in the
1002 * unfragmentable part.
1004 if (exthdrs.ip6e_rthdr) {
1005 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1006 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1007 } else if (exthdrs.ip6e_dest1) {
1008 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1009 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1010 } else if (exthdrs.ip6e_hbh) {
1011 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1012 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1014 nextproto = ip6->ip6_nxt;
1015 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1019 * Loop through length of segment after first fragment,
1020 * make new header and copy data of each part and link onto
1024 for (off = hlen; off < tlen; off += len) {
1025 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1028 ip6stat.ip6s_odropped++;
1031 m->m_pkthdr.rcvif = NULL;
1032 m->m_flags = m0->m_flags & M_COPYFLAGS;
1034 mnext = &m->m_nextpkt;
1035 m->m_data += max_linkhdr;
1036 mhip6 = mtod(m, struct ip6_hdr *);
1038 m->m_len = sizeof(*mhip6);
1039 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1041 ip6stat.ip6s_odropped++;
1044 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1045 if (off + len >= tlen)
1048 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1049 mhip6->ip6_plen = htons((u_short)(len + hlen +
1050 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1051 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1053 ip6stat.ip6s_odropped++;
1056 m_cat(m, m_frgpart);
1057 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1058 m->m_pkthdr.rcvif = NULL;
1059 ip6f->ip6f_reserved = 0;
1060 ip6f->ip6f_ident = id;
1061 ip6f->ip6f_nxt = nextproto;
1062 ip6stat.ip6s_ofragments++;
1063 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1066 in6_ifstat_inc(ifp, ifs6_out_fragok);
1070 * Remove leading garbages.
1076 for (m0 = m; m; m = m0) {
1080 /* Record statistics for this interface address. */
1082 ia->ia_ifa.if_opackets++;
1083 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1085 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1091 ip6stat.ip6s_fragmented++;
1094 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1096 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1097 RTFREE(ro_pmtu->ro_rt);
1107 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1108 m_freem(exthdrs.ip6e_dest1);
1109 m_freem(exthdrs.ip6e_rthdr);
1110 m_freem(exthdrs.ip6e_dest2);
1119 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1123 if (hlen > MCLBYTES)
1124 return (ENOBUFS); /* XXX */
1126 MGET(m, M_DONTWAIT, MT_DATA);
1131 MCLGET(m, M_DONTWAIT);
1132 if ((m->m_flags & M_EXT) == 0) {
1139 bcopy(hdr, mtod(m, caddr_t), hlen);
1146 * Insert jumbo payload option.
1149 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1155 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1158 * If there is no hop-by-hop options header, allocate new one.
1159 * If there is one but it doesn't have enough space to store the
1160 * jumbo payload option, allocate a cluster to store the whole options.
1161 * Otherwise, use it to store the options.
1163 if (exthdrs->ip6e_hbh == 0) {
1164 MGET(mopt, M_DONTWAIT, MT_DATA);
1167 mopt->m_len = JUMBOOPTLEN;
1168 optbuf = mtod(mopt, u_char *);
1169 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1170 exthdrs->ip6e_hbh = mopt;
1172 struct ip6_hbh *hbh;
1174 mopt = exthdrs->ip6e_hbh;
1175 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1178 * - exthdrs->ip6e_hbh is not referenced from places
1179 * other than exthdrs.
1180 * - exthdrs->ip6e_hbh is not an mbuf chain.
1182 int oldoptlen = mopt->m_len;
1186 * XXX: give up if the whole (new) hbh header does
1187 * not fit even in an mbuf cluster.
1189 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1193 * As a consequence, we must always prepare a cluster
1196 MGET(n, M_DONTWAIT, MT_DATA);
1198 MCLGET(n, M_DONTWAIT);
1199 if ((n->m_flags & M_EXT) == 0) {
1206 n->m_len = oldoptlen + JUMBOOPTLEN;
1207 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1209 optbuf = mtod(n, caddr_t) + oldoptlen;
1211 mopt = exthdrs->ip6e_hbh = n;
1213 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1214 mopt->m_len += JUMBOOPTLEN;
1216 optbuf[0] = IP6OPT_PADN;
1220 * Adjust the header length according to the pad and
1221 * the jumbo payload option.
1223 hbh = mtod(mopt, struct ip6_hbh *);
1224 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1227 /* fill in the option. */
1228 optbuf[2] = IP6OPT_JUMBO;
1230 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1231 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1233 /* finally, adjust the packet header length */
1234 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1241 * Insert fragment header and copy unfragmentable header portions.
1244 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1245 struct ip6_frag **frghdrp)
1247 struct mbuf *n, *mlast;
1249 if (hlen > sizeof(struct ip6_hdr)) {
1250 n = m_copym(m0, sizeof(struct ip6_hdr),
1251 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1258 /* Search for the last mbuf of unfragmentable part. */
1259 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1262 if ((mlast->m_flags & M_EXT) == 0 &&
1263 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1264 /* use the trailing space of the last mbuf for the fragment hdr */
1265 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1267 mlast->m_len += sizeof(struct ip6_frag);
1268 m->m_pkthdr.len += sizeof(struct ip6_frag);
1270 /* allocate a new mbuf for the fragment header */
1273 MGET(mfrg, M_DONTWAIT, MT_DATA);
1276 mfrg->m_len = sizeof(struct ip6_frag);
1277 *frghdrp = mtod(mfrg, struct ip6_frag *);
1278 mlast->m_next = mfrg;
1285 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1286 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1293 if (ro_pmtu != ro) {
1294 /* The first hop and the final destination may differ. */
1295 struct sockaddr_in6 *sa6_dst =
1296 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1297 if (ro_pmtu->ro_rt &&
1298 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1299 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1300 RTFREE(ro_pmtu->ro_rt);
1301 ro_pmtu->ro_rt = (struct rtentry *)NULL;
1303 if (ro_pmtu->ro_rt == NULL) {
1304 bzero(sa6_dst, sizeof(*sa6_dst));
1305 sa6_dst->sin6_family = AF_INET6;
1306 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1307 sa6_dst->sin6_addr = *dst;
1309 rtalloc((struct route *)ro_pmtu);
1312 if (ro_pmtu->ro_rt) {
1314 struct in_conninfo inc;
1316 bzero(&inc, sizeof(inc));
1317 inc.inc_flags = 1; /* IPv6 */
1318 inc.inc6_faddr = *dst;
1321 ifp = ro_pmtu->ro_rt->rt_ifp;
1322 ifmtu = IN6_LINKMTU(ifp);
1323 mtu = tcp_hc_getmtu(&inc);
1325 mtu = min(mtu, ro_pmtu->ro_rt->rt_rmx.rmx_mtu);
1327 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1330 else if (mtu < IPV6_MMTU) {
1332 * RFC2460 section 5, last paragraph:
1333 * if we record ICMPv6 too big message with
1334 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1335 * or smaller, with framgent header attached.
1336 * (fragment header is needed regardless from the
1337 * packet size, for translators to identify packets)
1341 } else if (mtu > ifmtu) {
1343 * The MTU on the route is larger than the MTU on
1344 * the interface! This shouldn't happen, unless the
1345 * MTU of the interface has been changed after the
1346 * interface was brought up. Change the MTU in the
1347 * route to match the interface MTU (as long as the
1348 * field isn't locked).
1351 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1354 mtu = IN6_LINKMTU(ifp);
1356 error = EHOSTUNREACH; /* XXX */
1360 *alwaysfragp = alwaysfrag;
1365 * IP6 socket option processing.
1368 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1370 int optdatalen, uproto;
1372 struct inpcb *in6p = sotoinpcb(so);
1374 int level, op, optname;
1379 level = sopt->sopt_level;
1380 op = sopt->sopt_dir;
1381 optname = sopt->sopt_name;
1382 optlen = sopt->sopt_valsize;
1385 panic("ip6_ctloutput: arg soopt is NULL");
1389 uproto = (int)so->so_proto->pr_protocol;
1391 if (level == IPPROTO_IPV6) {
1396 case IPV6_2292PKTOPTIONS:
1397 #ifdef IPV6_PKTOPTIONS
1398 case IPV6_PKTOPTIONS:
1403 error = soopt_getm(sopt, &m); /* XXX */
1406 error = soopt_mcopyin(sopt, m); /* XXX */
1409 error = ip6_pcbopts(&in6p->in6p_outputopts,
1411 m_freem(m); /* XXX */
1416 * Use of some Hop-by-Hop options or some
1417 * Destination options, might require special
1418 * privilege. That is, normal applications
1419 * (without special privilege) might be forbidden
1420 * from setting certain options in outgoing packets,
1421 * and might never see certain options in received
1422 * packets. [RFC 2292 Section 6]
1423 * KAME specific note:
1424 * KAME prevents non-privileged users from sending or
1425 * receiving ANY hbh/dst options in order to avoid
1426 * overhead of parsing options in the kernel.
1428 case IPV6_RECVHOPOPTS:
1429 case IPV6_RECVDSTOPTS:
1430 case IPV6_RECVRTHDRDSTOPTS:
1432 error = priv_check(td,
1433 PRIV_NETINET_SETHDROPTS);
1438 case IPV6_UNICAST_HOPS:
1442 case IPV6_RECVPKTINFO:
1443 case IPV6_RECVHOPLIMIT:
1444 case IPV6_RECVRTHDR:
1445 case IPV6_RECVPATHMTU:
1446 case IPV6_RECVTCLASS:
1448 case IPV6_AUTOFLOWLABEL:
1449 if (optlen != sizeof(int)) {
1453 error = sooptcopyin(sopt, &optval,
1454 sizeof optval, sizeof optval);
1459 case IPV6_UNICAST_HOPS:
1460 if (optval < -1 || optval >= 256)
1463 /* -1 = kernel default */
1464 in6p->in6p_hops = optval;
1465 if ((in6p->in6p_vflag &
1467 in6p->inp_ip_ttl = optval;
1470 #define OPTSET(bit) \
1473 in6p->in6p_flags |= (bit); \
1475 in6p->in6p_flags &= ~(bit); \
1476 } while (/*CONSTCOND*/ 0)
1477 #define OPTSET2292(bit) \
1479 in6p->in6p_flags |= IN6P_RFC2292; \
1481 in6p->in6p_flags |= (bit); \
1483 in6p->in6p_flags &= ~(bit); \
1484 } while (/*CONSTCOND*/ 0)
1485 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1487 case IPV6_RECVPKTINFO:
1488 /* cannot mix with RFC2292 */
1489 if (OPTBIT(IN6P_RFC2292)) {
1493 OPTSET(IN6P_PKTINFO);
1498 struct ip6_pktopts **optp;
1500 /* cannot mix with RFC2292 */
1501 if (OPTBIT(IN6P_RFC2292)) {
1505 optp = &in6p->in6p_outputopts;
1506 error = ip6_pcbopt(IPV6_HOPLIMIT,
1507 (u_char *)&optval, sizeof(optval),
1508 optp, (td != NULL) ? td->td_ucred :
1513 case IPV6_RECVHOPLIMIT:
1514 /* cannot mix with RFC2292 */
1515 if (OPTBIT(IN6P_RFC2292)) {
1519 OPTSET(IN6P_HOPLIMIT);
1522 case IPV6_RECVHOPOPTS:
1523 /* cannot mix with RFC2292 */
1524 if (OPTBIT(IN6P_RFC2292)) {
1528 OPTSET(IN6P_HOPOPTS);
1531 case IPV6_RECVDSTOPTS:
1532 /* cannot mix with RFC2292 */
1533 if (OPTBIT(IN6P_RFC2292)) {
1537 OPTSET(IN6P_DSTOPTS);
1540 case IPV6_RECVRTHDRDSTOPTS:
1541 /* cannot mix with RFC2292 */
1542 if (OPTBIT(IN6P_RFC2292)) {
1546 OPTSET(IN6P_RTHDRDSTOPTS);
1549 case IPV6_RECVRTHDR:
1550 /* cannot mix with RFC2292 */
1551 if (OPTBIT(IN6P_RFC2292)) {
1562 case IPV6_RECVPATHMTU:
1564 * We ignore this option for TCP
1566 * (RFC3542 leaves this case
1569 if (uproto != IPPROTO_TCP)
1575 * make setsockopt(IPV6_V6ONLY)
1576 * available only prior to bind(2).
1577 * see ipng mailing list, Jun 22 2001.
1579 if (in6p->in6p_lport ||
1580 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1584 OPTSET(IN6P_IPV6_V6ONLY);
1586 in6p->in6p_vflag &= ~INP_IPV4;
1588 in6p->in6p_vflag |= INP_IPV4;
1590 case IPV6_RECVTCLASS:
1591 /* cannot mix with RFC2292 XXX */
1592 if (OPTBIT(IN6P_RFC2292)) {
1596 OPTSET(IN6P_TCLASS);
1598 case IPV6_AUTOFLOWLABEL:
1599 OPTSET(IN6P_AUTOFLOWLABEL);
1607 case IPV6_USE_MIN_MTU:
1608 case IPV6_PREFER_TEMPADDR:
1609 if (optlen != sizeof(optval)) {
1613 error = sooptcopyin(sopt, &optval,
1614 sizeof optval, sizeof optval);
1618 struct ip6_pktopts **optp;
1619 optp = &in6p->in6p_outputopts;
1620 error = ip6_pcbopt(optname,
1621 (u_char *)&optval, sizeof(optval),
1622 optp, (td != NULL) ? td->td_ucred :
1627 case IPV6_2292PKTINFO:
1628 case IPV6_2292HOPLIMIT:
1629 case IPV6_2292HOPOPTS:
1630 case IPV6_2292DSTOPTS:
1631 case IPV6_2292RTHDR:
1633 if (optlen != sizeof(int)) {
1637 error = sooptcopyin(sopt, &optval,
1638 sizeof optval, sizeof optval);
1642 case IPV6_2292PKTINFO:
1643 OPTSET2292(IN6P_PKTINFO);
1645 case IPV6_2292HOPLIMIT:
1646 OPTSET2292(IN6P_HOPLIMIT);
1648 case IPV6_2292HOPOPTS:
1650 * Check super-user privilege.
1651 * See comments for IPV6_RECVHOPOPTS.
1654 error = priv_check(td,
1655 PRIV_NETINET_SETHDROPTS);
1659 OPTSET2292(IN6P_HOPOPTS);
1661 case IPV6_2292DSTOPTS:
1663 error = priv_check(td,
1664 PRIV_NETINET_SETHDROPTS);
1668 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1670 case IPV6_2292RTHDR:
1671 OPTSET2292(IN6P_RTHDR);
1679 case IPV6_RTHDRDSTOPTS:
1682 /* new advanced API (RFC3542) */
1684 u_char optbuf_storage[MCLBYTES];
1686 struct ip6_pktopts **optp;
1688 /* cannot mix with RFC2292 */
1689 if (OPTBIT(IN6P_RFC2292)) {
1695 * We only ensure valsize is not too large
1696 * here. Further validation will be done
1699 error = sooptcopyin(sopt, optbuf_storage,
1700 sizeof(optbuf_storage), 0);
1703 optlen = sopt->sopt_valsize;
1704 optbuf = optbuf_storage;
1705 optp = &in6p->in6p_outputopts;
1706 error = ip6_pcbopt(optname, optbuf, optlen,
1707 optp, (td != NULL) ? td->td_ucred : NULL,
1713 case IPV6_MULTICAST_IF:
1714 case IPV6_MULTICAST_HOPS:
1715 case IPV6_MULTICAST_LOOP:
1716 case IPV6_JOIN_GROUP:
1717 case IPV6_LEAVE_GROUP:
1719 if (sopt->sopt_valsize > MLEN) {
1729 if (sopt->sopt_valsize > MCLBYTES) {
1734 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1739 if (sopt->sopt_valsize > MLEN) {
1740 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1741 if ((m->m_flags & M_EXT) == 0) {
1747 m->m_len = sopt->sopt_valsize;
1748 error = sooptcopyin(sopt, mtod(m, char *),
1749 m->m_len, m->m_len);
1754 error = ip6_setmoptions(sopt->sopt_name,
1755 &in6p->in6p_moptions,
1761 case IPV6_PORTRANGE:
1762 error = sooptcopyin(sopt, &optval,
1763 sizeof optval, sizeof optval);
1768 case IPV6_PORTRANGE_DEFAULT:
1769 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1770 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1773 case IPV6_PORTRANGE_HIGH:
1774 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1775 in6p->in6p_flags |= IN6P_HIGHPORT;
1778 case IPV6_PORTRANGE_LOW:
1779 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1780 in6p->in6p_flags |= IN6P_LOWPORT;
1790 case IPV6_IPSEC_POLICY:
1795 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1797 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1799 req = mtod(m, caddr_t);
1800 error = ipsec6_set_policy(in6p, optname, req,
1801 m->m_len, (sopt->sopt_td != NULL) ?
1802 sopt->sopt_td->td_ucred : NULL);
1809 error = ENOPROTOOPT;
1817 case IPV6_2292PKTOPTIONS:
1818 #ifdef IPV6_PKTOPTIONS
1819 case IPV6_PKTOPTIONS:
1822 * RFC3542 (effectively) deprecated the
1823 * semantics of the 2292-style pktoptions.
1824 * Since it was not reliable in nature (i.e.,
1825 * applications had to expect the lack of some
1826 * information after all), it would make sense
1827 * to simplify this part by always returning
1830 sopt->sopt_valsize = 0;
1833 case IPV6_RECVHOPOPTS:
1834 case IPV6_RECVDSTOPTS:
1835 case IPV6_RECVRTHDRDSTOPTS:
1836 case IPV6_UNICAST_HOPS:
1837 case IPV6_RECVPKTINFO:
1838 case IPV6_RECVHOPLIMIT:
1839 case IPV6_RECVRTHDR:
1840 case IPV6_RECVPATHMTU:
1844 case IPV6_PORTRANGE:
1845 case IPV6_RECVTCLASS:
1846 case IPV6_AUTOFLOWLABEL:
1849 case IPV6_RECVHOPOPTS:
1850 optval = OPTBIT(IN6P_HOPOPTS);
1853 case IPV6_RECVDSTOPTS:
1854 optval = OPTBIT(IN6P_DSTOPTS);
1857 case IPV6_RECVRTHDRDSTOPTS:
1858 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1861 case IPV6_UNICAST_HOPS:
1862 optval = in6p->in6p_hops;
1865 case IPV6_RECVPKTINFO:
1866 optval = OPTBIT(IN6P_PKTINFO);
1869 case IPV6_RECVHOPLIMIT:
1870 optval = OPTBIT(IN6P_HOPLIMIT);
1873 case IPV6_RECVRTHDR:
1874 optval = OPTBIT(IN6P_RTHDR);
1877 case IPV6_RECVPATHMTU:
1878 optval = OPTBIT(IN6P_MTU);
1882 optval = OPTBIT(IN6P_FAITH);
1886 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1889 case IPV6_PORTRANGE:
1892 flags = in6p->in6p_flags;
1893 if (flags & IN6P_HIGHPORT)
1894 optval = IPV6_PORTRANGE_HIGH;
1895 else if (flags & IN6P_LOWPORT)
1896 optval = IPV6_PORTRANGE_LOW;
1901 case IPV6_RECVTCLASS:
1902 optval = OPTBIT(IN6P_TCLASS);
1905 case IPV6_AUTOFLOWLABEL:
1906 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1911 error = sooptcopyout(sopt, &optval,
1918 struct ip6_mtuinfo mtuinfo;
1919 struct route_in6 sro;
1921 bzero(&sro, sizeof(sro));
1923 if (!(so->so_state & SS_ISCONNECTED))
1926 * XXX: we dot not consider the case of source
1927 * routing, or optional information to specify
1928 * the outgoing interface.
1930 error = ip6_getpmtu(&sro, NULL, NULL,
1931 &in6p->in6p_faddr, &pmtu, NULL);
1936 if (pmtu > IPV6_MAXPACKET)
1937 pmtu = IPV6_MAXPACKET;
1939 bzero(&mtuinfo, sizeof(mtuinfo));
1940 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1941 optdata = (void *)&mtuinfo;
1942 optdatalen = sizeof(mtuinfo);
1943 error = sooptcopyout(sopt, optdata,
1948 case IPV6_2292PKTINFO:
1949 case IPV6_2292HOPLIMIT:
1950 case IPV6_2292HOPOPTS:
1951 case IPV6_2292RTHDR:
1952 case IPV6_2292DSTOPTS:
1954 case IPV6_2292PKTINFO:
1955 optval = OPTBIT(IN6P_PKTINFO);
1957 case IPV6_2292HOPLIMIT:
1958 optval = OPTBIT(IN6P_HOPLIMIT);
1960 case IPV6_2292HOPOPTS:
1961 optval = OPTBIT(IN6P_HOPOPTS);
1963 case IPV6_2292RTHDR:
1964 optval = OPTBIT(IN6P_RTHDR);
1966 case IPV6_2292DSTOPTS:
1967 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1970 error = sooptcopyout(sopt, &optval,
1977 case IPV6_RTHDRDSTOPTS:
1981 case IPV6_USE_MIN_MTU:
1982 case IPV6_PREFER_TEMPADDR:
1983 error = ip6_getpcbopt(in6p->in6p_outputopts,
1987 case IPV6_MULTICAST_IF:
1988 case IPV6_MULTICAST_HOPS:
1989 case IPV6_MULTICAST_LOOP:
1990 case IPV6_JOIN_GROUP:
1991 case IPV6_LEAVE_GROUP:
1994 error = ip6_getmoptions(sopt->sopt_name,
1995 in6p->in6p_moptions, &m);
1997 error = sooptcopyout(sopt,
1998 mtod(m, char *), m->m_len);
2004 case IPV6_IPSEC_POLICY:
2008 struct mbuf *m = NULL;
2009 struct mbuf **mp = &m;
2010 size_t ovalsize = sopt->sopt_valsize;
2011 caddr_t oval = (caddr_t)sopt->sopt_val;
2013 error = soopt_getm(sopt, &m); /* XXX */
2016 error = soopt_mcopyin(sopt, m); /* XXX */
2019 sopt->sopt_valsize = ovalsize;
2020 sopt->sopt_val = oval;
2022 req = mtod(m, caddr_t);
2025 error = ipsec6_get_policy(in6p, req, len, mp);
2027 error = soopt_mcopyout(sopt, m); /* XXX */
2028 if (error == 0 && m)
2035 error = ENOPROTOOPT;
2040 } else { /* level != IPPROTO_IPV6 */
2047 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2049 int error = 0, optval, optlen;
2050 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2051 struct in6pcb *in6p = sotoin6pcb(so);
2052 int level, op, optname;
2055 level = sopt->sopt_level;
2056 op = sopt->sopt_dir;
2057 optname = sopt->sopt_name;
2058 optlen = sopt->sopt_valsize;
2060 panic("ip6_raw_ctloutput: arg soopt is NULL");
2062 if (level != IPPROTO_IPV6) {
2069 * For ICMPv6 sockets, no modification allowed for checksum
2070 * offset, permit "no change" values to help existing apps.
2072 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2073 * for an ICMPv6 socket will fail."
2074 * The current behavior does not meet RFC3542.
2078 if (optlen != sizeof(int)) {
2082 error = sooptcopyin(sopt, &optval, sizeof(optval),
2086 if ((optval % 2) != 0) {
2087 /* the API assumes even offset values */
2089 } else if (so->so_proto->pr_protocol ==
2091 if (optval != icmp6off)
2094 in6p->in6p_cksum = optval;
2098 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2101 optval = in6p->in6p_cksum;
2103 error = sooptcopyout(sopt, &optval, sizeof(optval));
2113 error = ENOPROTOOPT;
2121 * Set up IP6 options in pcb for insertion in output packets or
2122 * specifying behavior of outgoing packets.
2125 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2126 struct socket *so, struct sockopt *sopt)
2128 struct ip6_pktopts *opt = *pktopt;
2130 struct thread *td = sopt->sopt_td;
2132 /* turn off any old options. */
2135 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2136 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2137 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2138 printf("ip6_pcbopts: all specified options are cleared.\n");
2140 ip6_clearpktopts(opt, -1);
2142 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2145 if (!m || m->m_len == 0) {
2147 * Only turning off any previous options, regardless of
2148 * whether the opt is just created or given.
2150 free(opt, M_IP6OPT);
2154 /* set options specified by user. */
2155 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2156 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2157 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2158 free(opt, M_IP6OPT);
2166 * initialize ip6_pktopts. beware that there are non-zero default values in
2170 ip6_initpktopts(struct ip6_pktopts *opt)
2173 bzero(opt, sizeof(*opt));
2174 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2175 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2176 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2177 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2181 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2182 struct ucred *cred, int uproto)
2184 struct ip6_pktopts *opt;
2186 if (*pktopt == NULL) {
2187 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2189 ip6_initpktopts(*pktopt);
2193 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2197 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2199 void *optdata = NULL;
2201 struct ip6_ext *ip6e;
2203 struct in6_pktinfo null_pktinfo;
2204 int deftclass = 0, on;
2205 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2206 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2210 if (pktopt && pktopt->ip6po_pktinfo)
2211 optdata = (void *)pktopt->ip6po_pktinfo;
2213 /* XXX: we don't have to do this every time... */
2214 bzero(&null_pktinfo, sizeof(null_pktinfo));
2215 optdata = (void *)&null_pktinfo;
2217 optdatalen = sizeof(struct in6_pktinfo);
2220 if (pktopt && pktopt->ip6po_tclass >= 0)
2221 optdata = (void *)&pktopt->ip6po_tclass;
2223 optdata = (void *)&deftclass;
2224 optdatalen = sizeof(int);
2227 if (pktopt && pktopt->ip6po_hbh) {
2228 optdata = (void *)pktopt->ip6po_hbh;
2229 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2230 optdatalen = (ip6e->ip6e_len + 1) << 3;
2234 if (pktopt && pktopt->ip6po_rthdr) {
2235 optdata = (void *)pktopt->ip6po_rthdr;
2236 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2237 optdatalen = (ip6e->ip6e_len + 1) << 3;
2240 case IPV6_RTHDRDSTOPTS:
2241 if (pktopt && pktopt->ip6po_dest1) {
2242 optdata = (void *)pktopt->ip6po_dest1;
2243 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2244 optdatalen = (ip6e->ip6e_len + 1) << 3;
2248 if (pktopt && pktopt->ip6po_dest2) {
2249 optdata = (void *)pktopt->ip6po_dest2;
2250 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2251 optdatalen = (ip6e->ip6e_len + 1) << 3;
2255 if (pktopt && pktopt->ip6po_nexthop) {
2256 optdata = (void *)pktopt->ip6po_nexthop;
2257 optdatalen = pktopt->ip6po_nexthop->sa_len;
2260 case IPV6_USE_MIN_MTU:
2262 optdata = (void *)&pktopt->ip6po_minmtu;
2264 optdata = (void *)&defminmtu;
2265 optdatalen = sizeof(int);
2268 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2272 optdata = (void *)&on;
2273 optdatalen = sizeof(on);
2275 case IPV6_PREFER_TEMPADDR:
2277 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2279 optdata = (void *)&defpreftemp;
2280 optdatalen = sizeof(int);
2282 default: /* should not happen */
2284 panic("ip6_getpcbopt: unexpected option\n");
2286 return (ENOPROTOOPT);
2289 error = sooptcopyout(sopt, optdata, optdatalen);
2295 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2300 if (optname == -1 || optname == IPV6_PKTINFO) {
2301 if (pktopt->ip6po_pktinfo)
2302 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2303 pktopt->ip6po_pktinfo = NULL;
2305 if (optname == -1 || optname == IPV6_HOPLIMIT)
2306 pktopt->ip6po_hlim = -1;
2307 if (optname == -1 || optname == IPV6_TCLASS)
2308 pktopt->ip6po_tclass = -1;
2309 if (optname == -1 || optname == IPV6_NEXTHOP) {
2310 if (pktopt->ip6po_nextroute.ro_rt) {
2311 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2312 pktopt->ip6po_nextroute.ro_rt = NULL;
2314 if (pktopt->ip6po_nexthop)
2315 free(pktopt->ip6po_nexthop, M_IP6OPT);
2316 pktopt->ip6po_nexthop = NULL;
2318 if (optname == -1 || optname == IPV6_HOPOPTS) {
2319 if (pktopt->ip6po_hbh)
2320 free(pktopt->ip6po_hbh, M_IP6OPT);
2321 pktopt->ip6po_hbh = NULL;
2323 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2324 if (pktopt->ip6po_dest1)
2325 free(pktopt->ip6po_dest1, M_IP6OPT);
2326 pktopt->ip6po_dest1 = NULL;
2328 if (optname == -1 || optname == IPV6_RTHDR) {
2329 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2330 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2331 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2332 if (pktopt->ip6po_route.ro_rt) {
2333 RTFREE(pktopt->ip6po_route.ro_rt);
2334 pktopt->ip6po_route.ro_rt = NULL;
2337 if (optname == -1 || optname == IPV6_DSTOPTS) {
2338 if (pktopt->ip6po_dest2)
2339 free(pktopt->ip6po_dest2, M_IP6OPT);
2340 pktopt->ip6po_dest2 = NULL;
2344 #define PKTOPT_EXTHDRCPY(type) \
2347 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2348 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2349 if (dst->type == NULL && canwait == M_NOWAIT)\
2351 bcopy(src->type, dst->type, hlen);\
2353 } while (/*CONSTCOND*/ 0)
2356 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2358 if (dst == NULL || src == NULL) {
2359 printf("ip6_clearpktopts: invalid argument\n");
2363 dst->ip6po_hlim = src->ip6po_hlim;
2364 dst->ip6po_tclass = src->ip6po_tclass;
2365 dst->ip6po_flags = src->ip6po_flags;
2366 if (src->ip6po_pktinfo) {
2367 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2369 if (dst->ip6po_pktinfo == NULL)
2371 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2373 if (src->ip6po_nexthop) {
2374 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2376 if (dst->ip6po_nexthop == NULL)
2378 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2379 src->ip6po_nexthop->sa_len);
2381 PKTOPT_EXTHDRCPY(ip6po_hbh);
2382 PKTOPT_EXTHDRCPY(ip6po_dest1);
2383 PKTOPT_EXTHDRCPY(ip6po_dest2);
2384 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2388 ip6_clearpktopts(dst, -1);
2391 #undef PKTOPT_EXTHDRCPY
2393 struct ip6_pktopts *
2394 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2397 struct ip6_pktopts *dst;
2399 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2402 ip6_initpktopts(dst);
2404 if ((error = copypktopts(dst, src, canwait)) != 0) {
2405 free(dst, M_IP6OPT);
2413 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2418 ip6_clearpktopts(pktopt, -1);
2420 free(pktopt, M_IP6OPT);
2424 * Set the IP6 multicast options in response to user setsockopt().
2427 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2430 u_int loop, ifindex;
2431 struct ipv6_mreq *mreq;
2433 struct ip6_moptions *im6o = *im6op;
2434 struct route_in6 ro;
2435 struct in6_multi_mship *imm;
2439 * No multicast option buffer attached to the pcb;
2440 * allocate one and initialize to default values.
2442 im6o = (struct ip6_moptions *)
2443 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK);
2448 im6o->im6o_multicast_ifp = NULL;
2449 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2450 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2451 LIST_INIT(&im6o->im6o_memberships);
2456 case IPV6_MULTICAST_IF:
2458 * Select the interface for outgoing multicast packets.
2460 if (m == NULL || m->m_len != sizeof(u_int)) {
2464 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2465 if (ifindex < 0 || if_index < ifindex) {
2466 error = ENXIO; /* XXX EINVAL? */
2469 ifp = ifnet_byindex(ifindex);
2470 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2471 error = EADDRNOTAVAIL;
2474 im6o->im6o_multicast_ifp = ifp;
2477 case IPV6_MULTICAST_HOPS:
2480 * Set the IP6 hoplimit for outgoing multicast packets.
2483 if (m == NULL || m->m_len != sizeof(int)) {
2487 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2488 if (optval < -1 || optval >= 256)
2490 else if (optval == -1)
2491 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2493 im6o->im6o_multicast_hlim = optval;
2497 case IPV6_MULTICAST_LOOP:
2499 * Set the loopback flag for outgoing multicast packets.
2500 * Must be zero or one.
2502 if (m == NULL || m->m_len != sizeof(u_int)) {
2506 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2511 im6o->im6o_multicast_loop = loop;
2514 case IPV6_JOIN_GROUP:
2516 * Add a multicast group membership.
2517 * Group must be a valid IP6 multicast address.
2519 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2523 mreq = mtod(m, struct ipv6_mreq *);
2525 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2527 * We use the unspecified address to specify to accept
2528 * all multicast addresses. Only super user is allowed
2531 /* XXX-BZ might need a better PRIV_NETINET_x for this */
2532 error = priv_check(curthread, PRIV_NETINET_MROUTE);
2535 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2541 * If no interface was explicitly specified, choose an
2542 * appropriate one according to the given multicast address.
2544 if (mreq->ipv6mr_interface == 0) {
2545 struct sockaddr_in6 *dst;
2548 * Look up the routing table for the
2549 * address, and choose the outgoing interface.
2550 * XXX: is it a good approach?
2553 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2554 bzero(dst, sizeof(*dst));
2555 dst->sin6_family = AF_INET6;
2556 dst->sin6_len = sizeof(*dst);
2557 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2558 rtalloc((struct route *)&ro);
2559 if (ro.ro_rt == NULL) {
2560 error = EADDRNOTAVAIL;
2563 ifp = ro.ro_rt->rt_ifp;
2567 * If the interface is specified, validate it.
2569 if (mreq->ipv6mr_interface < 0 ||
2570 if_index < mreq->ipv6mr_interface) {
2571 error = ENXIO; /* XXX EINVAL? */
2574 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2576 error = ENXIO; /* XXX EINVAL? */
2582 * See if we found an interface, and confirm that it
2583 * supports multicast
2585 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2586 error = EADDRNOTAVAIL;
2590 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2591 error = EADDRNOTAVAIL; /* XXX: should not happen */
2596 * See if the membership already exists.
2598 for (imm = im6o->im6o_memberships.lh_first;
2599 imm != NULL; imm = imm->i6mm_chain.le_next)
2600 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2601 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2602 &mreq->ipv6mr_multiaddr))
2609 * Everything looks good; add a new record to the multicast
2610 * address list for the given interface.
2612 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2615 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2618 case IPV6_LEAVE_GROUP:
2620 * Drop a multicast group membership.
2621 * Group must be a valid IP6 multicast address.
2623 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2627 mreq = mtod(m, struct ipv6_mreq *);
2630 * If an interface address was specified, get a pointer
2631 * to its ifnet structure.
2633 if (mreq->ipv6mr_interface < 0 ||
2634 if_index < mreq->ipv6mr_interface) {
2635 error = ENXIO; /* XXX EINVAL? */
2638 if (mreq->ipv6mr_interface == 0)
2641 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2643 /* Fill in the scope zone ID */
2645 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2646 /* XXX: should not happen */
2647 error = EADDRNOTAVAIL;
2650 } else if (mreq->ipv6mr_interface != 0) {
2652 * This case happens when the (positive) index is in
2653 * the valid range, but the corresponding interface has
2654 * been detached dynamically (XXX).
2656 error = EADDRNOTAVAIL;
2658 } else { /* ipv6mr_interface == 0 */
2659 struct sockaddr_in6 sa6_mc;
2662 * The API spec says as follows:
2663 * If the interface index is specified as 0, the
2664 * system may choose a multicast group membership to
2665 * drop by matching the multicast address only.
2666 * On the other hand, we cannot disambiguate the scope
2667 * zone unless an interface is provided. Thus, we
2668 * check if there's ambiguity with the default scope
2669 * zone as the last resort.
2671 bzero(&sa6_mc, sizeof(sa6_mc));
2672 sa6_mc.sin6_family = AF_INET6;
2673 sa6_mc.sin6_len = sizeof(sa6_mc);
2674 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2675 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2678 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2682 * Find the membership in the membership list.
2684 for (imm = im6o->im6o_memberships.lh_first;
2685 imm != NULL; imm = imm->i6mm_chain.le_next) {
2686 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2687 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2688 &mreq->ipv6mr_multiaddr))
2692 /* Unable to resolve interface */
2693 error = EADDRNOTAVAIL;
2697 * Give up the multicast address record to which the
2698 * membership points.
2700 LIST_REMOVE(imm, i6mm_chain);
2701 in6_delmulti(imm->i6mm_maddr);
2702 free(imm, M_IP6MADDR);
2711 * If all options have default values, no need to keep the mbuf.
2713 if (im6o->im6o_multicast_ifp == NULL &&
2714 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2715 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2716 im6o->im6o_memberships.lh_first == NULL) {
2717 free(*im6op, M_IP6MOPTS);
2725 * Return the IP6 multicast options in response to user getsockopt().
2728 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2730 u_int *hlim, *loop, *ifindex;
2732 *mp = m_get(M_WAIT, MT_HEADER); /* XXX */
2736 case IPV6_MULTICAST_IF:
2737 ifindex = mtod(*mp, u_int *);
2738 (*mp)->m_len = sizeof(u_int);
2739 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2742 *ifindex = im6o->im6o_multicast_ifp->if_index;
2745 case IPV6_MULTICAST_HOPS:
2746 hlim = mtod(*mp, u_int *);
2747 (*mp)->m_len = sizeof(u_int);
2749 *hlim = ip6_defmcasthlim;
2751 *hlim = im6o->im6o_multicast_hlim;
2754 case IPV6_MULTICAST_LOOP:
2755 loop = mtod(*mp, u_int *);
2756 (*mp)->m_len = sizeof(u_int);
2758 *loop = ip6_defmcasthlim;
2760 *loop = im6o->im6o_multicast_loop;
2764 return (EOPNOTSUPP);
2769 * Discard the IP6 multicast options.
2772 ip6_freemoptions(struct ip6_moptions *im6o)
2774 struct in6_multi_mship *imm;
2779 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2780 LIST_REMOVE(imm, i6mm_chain);
2781 if (imm->i6mm_maddr)
2782 in6_delmulti(imm->i6mm_maddr);
2783 free(imm, M_IP6MADDR);
2785 free(im6o, M_IP6MOPTS);
2789 * Set IPv6 outgoing packet options based on advanced API.
2792 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2793 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2795 struct cmsghdr *cm = 0;
2797 if (control == NULL || opt == NULL)
2800 ip6_initpktopts(opt);
2805 * If stickyopt is provided, make a local copy of the options
2806 * for this particular packet, then override them by ancillary
2808 * XXX: copypktopts() does not copy the cached route to a next
2809 * hop (if any). This is not very good in terms of efficiency,
2810 * but we can allow this since this option should be rarely
2813 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2818 * XXX: Currently, we assume all the optional information is stored
2821 if (control->m_next)
2824 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2825 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2828 if (control->m_len < CMSG_LEN(0))
2831 cm = mtod(control, struct cmsghdr *);
2832 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2834 if (cm->cmsg_level != IPPROTO_IPV6)
2837 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2838 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2847 * Set a particular packet option, as a sticky option or an ancillary data
2848 * item. "len" can be 0 only when it's a sticky option.
2849 * We have 4 cases of combination of "sticky" and "cmsg":
2850 * "sticky=0, cmsg=0": impossible
2851 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2852 * "sticky=1, cmsg=0": RFC3542 socket option
2853 * "sticky=1, cmsg=1": RFC2292 socket option
2856 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2857 struct ucred *cred, int sticky, int cmsg, int uproto)
2859 int minmtupolicy, preftemp;
2862 if (!sticky && !cmsg) {
2864 printf("ip6_setpktopt: impossible case\n");
2870 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2871 * not be specified in the context of RFC3542. Conversely,
2872 * RFC3542 types should not be specified in the context of RFC2292.
2876 case IPV6_2292PKTINFO:
2877 case IPV6_2292HOPLIMIT:
2878 case IPV6_2292NEXTHOP:
2879 case IPV6_2292HOPOPTS:
2880 case IPV6_2292DSTOPTS:
2881 case IPV6_2292RTHDR:
2882 case IPV6_2292PKTOPTIONS:
2883 return (ENOPROTOOPT);
2886 if (sticky && cmsg) {
2893 case IPV6_RTHDRDSTOPTS:
2895 case IPV6_USE_MIN_MTU:
2898 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2899 return (ENOPROTOOPT);
2904 case IPV6_2292PKTINFO:
2907 struct ifnet *ifp = NULL;
2908 struct in6_pktinfo *pktinfo;
2910 if (len != sizeof(struct in6_pktinfo))
2913 pktinfo = (struct in6_pktinfo *)buf;
2916 * An application can clear any sticky IPV6_PKTINFO option by
2917 * doing a "regular" setsockopt with ipi6_addr being
2918 * in6addr_any and ipi6_ifindex being zero.
2919 * [RFC 3542, Section 6]
2921 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2922 pktinfo->ipi6_ifindex == 0 &&
2923 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2924 ip6_clearpktopts(opt, optname);
2928 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2929 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2933 /* validate the interface index if specified. */
2934 if (pktinfo->ipi6_ifindex > if_index ||
2935 pktinfo->ipi6_ifindex < 0) {
2938 if (pktinfo->ipi6_ifindex) {
2939 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2945 * We store the address anyway, and let in6_selectsrc()
2946 * validate the specified address. This is because ipi6_addr
2947 * may not have enough information about its scope zone, and
2948 * we may need additional information (such as outgoing
2949 * interface or the scope zone of a destination address) to
2950 * disambiguate the scope.
2951 * XXX: the delay of the validation may confuse the
2952 * application when it is used as a sticky option.
2954 if (opt->ip6po_pktinfo == NULL) {
2955 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2956 M_IP6OPT, M_NOWAIT);
2957 if (opt->ip6po_pktinfo == NULL)
2960 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2964 case IPV6_2292HOPLIMIT:
2970 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2971 * to simplify the ordering among hoplimit options.
2973 if (optname == IPV6_HOPLIMIT && sticky)
2974 return (ENOPROTOOPT);
2976 if (len != sizeof(int))
2979 if (*hlimp < -1 || *hlimp > 255)
2982 opt->ip6po_hlim = *hlimp;
2990 if (len != sizeof(int))
2992 tclass = *(int *)buf;
2993 if (tclass < -1 || tclass > 255)
2996 opt->ip6po_tclass = tclass;
3000 case IPV6_2292NEXTHOP:
3003 error = priv_check_cred(cred,
3004 PRIV_NETINET_SETHDROPTS, 0);
3009 if (len == 0) { /* just remove the option */
3010 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3014 /* check if cmsg_len is large enough for sa_len */
3015 if (len < sizeof(struct sockaddr) || len < *buf)
3018 switch (((struct sockaddr *)buf)->sa_family) {
3021 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3024 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3027 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3028 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3031 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3037 case AF_LINK: /* should eventually be supported */
3039 return (EAFNOSUPPORT);
3042 /* turn off the previous option, then set the new option. */
3043 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3044 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3045 if (opt->ip6po_nexthop == NULL)
3047 bcopy(buf, opt->ip6po_nexthop, *buf);
3050 case IPV6_2292HOPOPTS:
3053 struct ip6_hbh *hbh;
3057 * XXX: We don't allow a non-privileged user to set ANY HbH
3058 * options, since per-option restriction has too much
3062 error = priv_check_cred(cred,
3063 PRIV_NETINET_SETHDROPTS, 0);
3069 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3070 break; /* just remove the option */
3073 /* message length validation */
3074 if (len < sizeof(struct ip6_hbh))
3076 hbh = (struct ip6_hbh *)buf;
3077 hbhlen = (hbh->ip6h_len + 1) << 3;
3081 /* turn off the previous option, then set the new option. */
3082 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3083 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3084 if (opt->ip6po_hbh == NULL)
3086 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3091 case IPV6_2292DSTOPTS:
3093 case IPV6_RTHDRDSTOPTS:
3095 struct ip6_dest *dest, **newdest = NULL;
3098 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3099 error = priv_check_cred(cred,
3100 PRIV_NETINET_SETHDROPTS, 0);
3106 ip6_clearpktopts(opt, optname);
3107 break; /* just remove the option */
3110 /* message length validation */
3111 if (len < sizeof(struct ip6_dest))
3113 dest = (struct ip6_dest *)buf;
3114 destlen = (dest->ip6d_len + 1) << 3;
3119 * Determine the position that the destination options header
3120 * should be inserted; before or after the routing header.
3123 case IPV6_2292DSTOPTS:
3125 * The old advacned API is ambiguous on this point.
3126 * Our approach is to determine the position based
3127 * according to the existence of a routing header.
3128 * Note, however, that this depends on the order of the
3129 * extension headers in the ancillary data; the 1st
3130 * part of the destination options header must appear
3131 * before the routing header in the ancillary data,
3133 * RFC3542 solved the ambiguity by introducing
3134 * separate ancillary data or option types.
3136 if (opt->ip6po_rthdr == NULL)
3137 newdest = &opt->ip6po_dest1;
3139 newdest = &opt->ip6po_dest2;
3141 case IPV6_RTHDRDSTOPTS:
3142 newdest = &opt->ip6po_dest1;
3145 newdest = &opt->ip6po_dest2;
3149 /* turn off the previous option, then set the new option. */
3150 ip6_clearpktopts(opt, optname);
3151 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3152 if (*newdest == NULL)
3154 bcopy(dest, *newdest, destlen);
3159 case IPV6_2292RTHDR:
3162 struct ip6_rthdr *rth;
3166 ip6_clearpktopts(opt, IPV6_RTHDR);
3167 break; /* just remove the option */
3170 /* message length validation */
3171 if (len < sizeof(struct ip6_rthdr))
3173 rth = (struct ip6_rthdr *)buf;
3174 rthlen = (rth->ip6r_len + 1) << 3;
3178 switch (rth->ip6r_type) {
3179 case IPV6_RTHDR_TYPE_0:
3180 if (rth->ip6r_len == 0) /* must contain one addr */
3182 if (rth->ip6r_len % 2) /* length must be even */
3184 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3188 return (EINVAL); /* not supported */
3191 /* turn off the previous option */
3192 ip6_clearpktopts(opt, IPV6_RTHDR);
3193 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3194 if (opt->ip6po_rthdr == NULL)
3196 bcopy(rth, opt->ip6po_rthdr, rthlen);
3201 case IPV6_USE_MIN_MTU:
3202 if (len != sizeof(int))
3204 minmtupolicy = *(int *)buf;
3205 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3206 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3207 minmtupolicy != IP6PO_MINMTU_ALL) {
3210 opt->ip6po_minmtu = minmtupolicy;
3214 if (len != sizeof(int))
3217 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3219 * we ignore this option for TCP sockets.
3220 * (RFC3542 leaves this case unspecified.)
3222 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3224 opt->ip6po_flags |= IP6PO_DONTFRAG;
3227 case IPV6_PREFER_TEMPADDR:
3228 if (len != sizeof(int))
3230 preftemp = *(int *)buf;
3231 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3232 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3233 preftemp != IP6PO_TEMPADDR_PREFER) {
3236 opt->ip6po_prefer_tempaddr = preftemp;
3240 return (ENOPROTOOPT);
3241 } /* end of switch */
3247 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3248 * packet to the input queue of a specified interface. Note that this
3249 * calls the output routine of the loopback "driver", but with an interface
3250 * pointer that might NOT be &loif -- easier than replicating that code here.
3253 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3256 struct ip6_hdr *ip6;
3258 copym = m_copy(m, 0, M_COPYALL);
3263 * Make sure to deep-copy IPv6 header portion in case the data
3264 * is in an mbuf cluster, so that we can safely override the IPv6
3265 * header portion later.
3267 if ((copym->m_flags & M_EXT) != 0 ||
3268 copym->m_len < sizeof(struct ip6_hdr)) {
3269 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3275 if (copym->m_len < sizeof(*ip6)) {
3281 ip6 = mtod(copym, struct ip6_hdr *);
3283 * clear embedded scope identifiers if necessary.
3284 * in6_clearscope will touch the addresses only when necessary.
3286 in6_clearscope(&ip6->ip6_src);
3287 in6_clearscope(&ip6->ip6_dst);
3289 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3293 * Chop IPv6 header off from the payload.
3296 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3299 struct ip6_hdr *ip6;
3301 ip6 = mtod(m, struct ip6_hdr *);
3302 if (m->m_len > sizeof(*ip6)) {
3303 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3308 M_MOVE_PKTHDR(mh, m);
3309 MH_ALIGN(mh, sizeof(*ip6));
3310 m->m_len -= sizeof(*ip6);
3311 m->m_data += sizeof(*ip6);
3314 m->m_len = sizeof(*ip6);
3315 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3317 exthdrs->ip6e_ip6 = m;
3322 * Compute IPv6 extension header length.
3325 ip6_optlen(struct in6pcb *in6p)
3329 if (!in6p->in6p_outputopts)
3334 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3336 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3337 if (in6p->in6p_outputopts->ip6po_rthdr)
3338 /* dest1 is valid with rthdr only */
3339 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3340 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3341 len += elen(in6p->in6p_outputopts->ip6po_dest2);