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 |= INC_ISIPV6;
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;
1378 level = sopt->sopt_level;
1379 op = sopt->sopt_dir;
1380 optname = sopt->sopt_name;
1381 optlen = sopt->sopt_valsize;
1385 uproto = (int)so->so_proto->pr_protocol;
1387 if (level == IPPROTO_IPV6) {
1392 case IPV6_2292PKTOPTIONS:
1393 #ifdef IPV6_PKTOPTIONS
1394 case IPV6_PKTOPTIONS:
1399 error = soopt_getm(sopt, &m); /* XXX */
1402 error = soopt_mcopyin(sopt, m); /* XXX */
1405 error = ip6_pcbopts(&in6p->in6p_outputopts,
1407 m_freem(m); /* XXX */
1412 * Use of some Hop-by-Hop options or some
1413 * Destination options, might require special
1414 * privilege. That is, normal applications
1415 * (without special privilege) might be forbidden
1416 * from setting certain options in outgoing packets,
1417 * and might never see certain options in received
1418 * packets. [RFC 2292 Section 6]
1419 * KAME specific note:
1420 * KAME prevents non-privileged users from sending or
1421 * receiving ANY hbh/dst options in order to avoid
1422 * overhead of parsing options in the kernel.
1424 case IPV6_RECVHOPOPTS:
1425 case IPV6_RECVDSTOPTS:
1426 case IPV6_RECVRTHDRDSTOPTS:
1428 error = priv_check(td,
1429 PRIV_NETINET_SETHDROPTS);
1434 case IPV6_UNICAST_HOPS:
1438 case IPV6_RECVPKTINFO:
1439 case IPV6_RECVHOPLIMIT:
1440 case IPV6_RECVRTHDR:
1441 case IPV6_RECVPATHMTU:
1442 case IPV6_RECVTCLASS:
1444 case IPV6_AUTOFLOWLABEL:
1445 if (optlen != sizeof(int)) {
1449 error = sooptcopyin(sopt, &optval,
1450 sizeof optval, sizeof optval);
1455 case IPV6_UNICAST_HOPS:
1456 if (optval < -1 || optval >= 256)
1459 /* -1 = kernel default */
1460 in6p->in6p_hops = optval;
1461 if ((in6p->inp_vflag &
1463 in6p->inp_ip_ttl = optval;
1466 #define OPTSET(bit) \
1469 in6p->inp_flags |= (bit); \
1471 in6p->inp_flags &= ~(bit); \
1472 } while (/*CONSTCOND*/ 0)
1473 #define OPTSET2292(bit) \
1475 in6p->inp_flags |= IN6P_RFC2292; \
1477 in6p->inp_flags |= (bit); \
1479 in6p->inp_flags &= ~(bit); \
1480 } while (/*CONSTCOND*/ 0)
1481 #define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
1483 case IPV6_RECVPKTINFO:
1484 /* cannot mix with RFC2292 */
1485 if (OPTBIT(IN6P_RFC2292)) {
1489 OPTSET(IN6P_PKTINFO);
1494 struct ip6_pktopts **optp;
1496 /* cannot mix with RFC2292 */
1497 if (OPTBIT(IN6P_RFC2292)) {
1501 optp = &in6p->in6p_outputopts;
1502 error = ip6_pcbopt(IPV6_HOPLIMIT,
1503 (u_char *)&optval, sizeof(optval),
1504 optp, (td != NULL) ? td->td_ucred :
1509 case IPV6_RECVHOPLIMIT:
1510 /* cannot mix with RFC2292 */
1511 if (OPTBIT(IN6P_RFC2292)) {
1515 OPTSET(IN6P_HOPLIMIT);
1518 case IPV6_RECVHOPOPTS:
1519 /* cannot mix with RFC2292 */
1520 if (OPTBIT(IN6P_RFC2292)) {
1524 OPTSET(IN6P_HOPOPTS);
1527 case IPV6_RECVDSTOPTS:
1528 /* cannot mix with RFC2292 */
1529 if (OPTBIT(IN6P_RFC2292)) {
1533 OPTSET(IN6P_DSTOPTS);
1536 case IPV6_RECVRTHDRDSTOPTS:
1537 /* cannot mix with RFC2292 */
1538 if (OPTBIT(IN6P_RFC2292)) {
1542 OPTSET(IN6P_RTHDRDSTOPTS);
1545 case IPV6_RECVRTHDR:
1546 /* cannot mix with RFC2292 */
1547 if (OPTBIT(IN6P_RFC2292)) {
1558 case IPV6_RECVPATHMTU:
1560 * We ignore this option for TCP
1562 * (RFC3542 leaves this case
1565 if (uproto != IPPROTO_TCP)
1571 * make setsockopt(IPV6_V6ONLY)
1572 * available only prior to bind(2).
1573 * see ipng mailing list, Jun 22 2001.
1575 if (in6p->inp_lport ||
1576 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1580 OPTSET(IN6P_IPV6_V6ONLY);
1582 in6p->inp_vflag &= ~INP_IPV4;
1584 in6p->inp_vflag |= INP_IPV4;
1586 case IPV6_RECVTCLASS:
1587 /* cannot mix with RFC2292 XXX */
1588 if (OPTBIT(IN6P_RFC2292)) {
1592 OPTSET(IN6P_TCLASS);
1594 case IPV6_AUTOFLOWLABEL:
1595 OPTSET(IN6P_AUTOFLOWLABEL);
1603 case IPV6_USE_MIN_MTU:
1604 case IPV6_PREFER_TEMPADDR:
1605 if (optlen != sizeof(optval)) {
1609 error = sooptcopyin(sopt, &optval,
1610 sizeof optval, sizeof optval);
1614 struct ip6_pktopts **optp;
1615 optp = &in6p->in6p_outputopts;
1616 error = ip6_pcbopt(optname,
1617 (u_char *)&optval, sizeof(optval),
1618 optp, (td != NULL) ? td->td_ucred :
1623 case IPV6_2292PKTINFO:
1624 case IPV6_2292HOPLIMIT:
1625 case IPV6_2292HOPOPTS:
1626 case IPV6_2292DSTOPTS:
1627 case IPV6_2292RTHDR:
1629 if (optlen != sizeof(int)) {
1633 error = sooptcopyin(sopt, &optval,
1634 sizeof optval, sizeof optval);
1638 case IPV6_2292PKTINFO:
1639 OPTSET2292(IN6P_PKTINFO);
1641 case IPV6_2292HOPLIMIT:
1642 OPTSET2292(IN6P_HOPLIMIT);
1644 case IPV6_2292HOPOPTS:
1646 * Check super-user privilege.
1647 * See comments for IPV6_RECVHOPOPTS.
1650 error = priv_check(td,
1651 PRIV_NETINET_SETHDROPTS);
1655 OPTSET2292(IN6P_HOPOPTS);
1657 case IPV6_2292DSTOPTS:
1659 error = priv_check(td,
1660 PRIV_NETINET_SETHDROPTS);
1664 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1666 case IPV6_2292RTHDR:
1667 OPTSET2292(IN6P_RTHDR);
1675 case IPV6_RTHDRDSTOPTS:
1678 /* new advanced API (RFC3542) */
1680 u_char optbuf_storage[MCLBYTES];
1682 struct ip6_pktopts **optp;
1684 /* cannot mix with RFC2292 */
1685 if (OPTBIT(IN6P_RFC2292)) {
1691 * We only ensure valsize is not too large
1692 * here. Further validation will be done
1695 error = sooptcopyin(sopt, optbuf_storage,
1696 sizeof(optbuf_storage), 0);
1699 optlen = sopt->sopt_valsize;
1700 optbuf = optbuf_storage;
1701 optp = &in6p->in6p_outputopts;
1702 error = ip6_pcbopt(optname, optbuf, optlen,
1703 optp, (td != NULL) ? td->td_ucred : NULL,
1709 case IPV6_MULTICAST_IF:
1710 case IPV6_MULTICAST_HOPS:
1711 case IPV6_MULTICAST_LOOP:
1712 case IPV6_JOIN_GROUP:
1713 case IPV6_LEAVE_GROUP:
1715 if (sopt->sopt_valsize > MLEN) {
1725 if (sopt->sopt_valsize > MCLBYTES) {
1730 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
1735 if (sopt->sopt_valsize > MLEN) {
1736 MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
1737 if ((m->m_flags & M_EXT) == 0) {
1743 m->m_len = sopt->sopt_valsize;
1744 error = sooptcopyin(sopt, mtod(m, char *),
1745 m->m_len, m->m_len);
1750 error = ip6_setmoptions(sopt->sopt_name,
1751 &in6p->in6p_moptions,
1757 case IPV6_PORTRANGE:
1758 error = sooptcopyin(sopt, &optval,
1759 sizeof optval, sizeof optval);
1764 case IPV6_PORTRANGE_DEFAULT:
1765 in6p->inp_flags &= ~(INP_LOWPORT);
1766 in6p->inp_flags &= ~(INP_HIGHPORT);
1769 case IPV6_PORTRANGE_HIGH:
1770 in6p->inp_flags &= ~(INP_LOWPORT);
1771 in6p->inp_flags |= INP_HIGHPORT;
1774 case IPV6_PORTRANGE_LOW:
1775 in6p->inp_flags &= ~(INP_HIGHPORT);
1776 in6p->inp_flags |= INP_LOWPORT;
1786 case IPV6_IPSEC_POLICY:
1791 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1793 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1795 req = mtod(m, caddr_t);
1796 error = ipsec6_set_policy(in6p, optname, req,
1797 m->m_len, (sopt->sopt_td != NULL) ?
1798 sopt->sopt_td->td_ucred : NULL);
1805 error = ENOPROTOOPT;
1813 case IPV6_2292PKTOPTIONS:
1814 #ifdef IPV6_PKTOPTIONS
1815 case IPV6_PKTOPTIONS:
1818 * RFC3542 (effectively) deprecated the
1819 * semantics of the 2292-style pktoptions.
1820 * Since it was not reliable in nature (i.e.,
1821 * applications had to expect the lack of some
1822 * information after all), it would make sense
1823 * to simplify this part by always returning
1826 sopt->sopt_valsize = 0;
1829 case IPV6_RECVHOPOPTS:
1830 case IPV6_RECVDSTOPTS:
1831 case IPV6_RECVRTHDRDSTOPTS:
1832 case IPV6_UNICAST_HOPS:
1833 case IPV6_RECVPKTINFO:
1834 case IPV6_RECVHOPLIMIT:
1835 case IPV6_RECVRTHDR:
1836 case IPV6_RECVPATHMTU:
1840 case IPV6_PORTRANGE:
1841 case IPV6_RECVTCLASS:
1842 case IPV6_AUTOFLOWLABEL:
1845 case IPV6_RECVHOPOPTS:
1846 optval = OPTBIT(IN6P_HOPOPTS);
1849 case IPV6_RECVDSTOPTS:
1850 optval = OPTBIT(IN6P_DSTOPTS);
1853 case IPV6_RECVRTHDRDSTOPTS:
1854 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1857 case IPV6_UNICAST_HOPS:
1858 optval = in6p->in6p_hops;
1861 case IPV6_RECVPKTINFO:
1862 optval = OPTBIT(IN6P_PKTINFO);
1865 case IPV6_RECVHOPLIMIT:
1866 optval = OPTBIT(IN6P_HOPLIMIT);
1869 case IPV6_RECVRTHDR:
1870 optval = OPTBIT(IN6P_RTHDR);
1873 case IPV6_RECVPATHMTU:
1874 optval = OPTBIT(IN6P_MTU);
1878 optval = OPTBIT(INP_FAITH);
1882 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1885 case IPV6_PORTRANGE:
1888 flags = in6p->inp_flags;
1889 if (flags & INP_HIGHPORT)
1890 optval = IPV6_PORTRANGE_HIGH;
1891 else if (flags & INP_LOWPORT)
1892 optval = IPV6_PORTRANGE_LOW;
1897 case IPV6_RECVTCLASS:
1898 optval = OPTBIT(IN6P_TCLASS);
1901 case IPV6_AUTOFLOWLABEL:
1902 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1907 error = sooptcopyout(sopt, &optval,
1914 struct ip6_mtuinfo mtuinfo;
1915 struct route_in6 sro;
1917 bzero(&sro, sizeof(sro));
1919 if (!(so->so_state & SS_ISCONNECTED))
1922 * XXX: we dot not consider the case of source
1923 * routing, or optional information to specify
1924 * the outgoing interface.
1926 error = ip6_getpmtu(&sro, NULL, NULL,
1927 &in6p->in6p_faddr, &pmtu, NULL);
1932 if (pmtu > IPV6_MAXPACKET)
1933 pmtu = IPV6_MAXPACKET;
1935 bzero(&mtuinfo, sizeof(mtuinfo));
1936 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1937 optdata = (void *)&mtuinfo;
1938 optdatalen = sizeof(mtuinfo);
1939 error = sooptcopyout(sopt, optdata,
1944 case IPV6_2292PKTINFO:
1945 case IPV6_2292HOPLIMIT:
1946 case IPV6_2292HOPOPTS:
1947 case IPV6_2292RTHDR:
1948 case IPV6_2292DSTOPTS:
1950 case IPV6_2292PKTINFO:
1951 optval = OPTBIT(IN6P_PKTINFO);
1953 case IPV6_2292HOPLIMIT:
1954 optval = OPTBIT(IN6P_HOPLIMIT);
1956 case IPV6_2292HOPOPTS:
1957 optval = OPTBIT(IN6P_HOPOPTS);
1959 case IPV6_2292RTHDR:
1960 optval = OPTBIT(IN6P_RTHDR);
1962 case IPV6_2292DSTOPTS:
1963 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1966 error = sooptcopyout(sopt, &optval,
1973 case IPV6_RTHDRDSTOPTS:
1977 case IPV6_USE_MIN_MTU:
1978 case IPV6_PREFER_TEMPADDR:
1979 error = ip6_getpcbopt(in6p->in6p_outputopts,
1983 case IPV6_MULTICAST_IF:
1984 case IPV6_MULTICAST_HOPS:
1985 case IPV6_MULTICAST_LOOP:
1986 case IPV6_JOIN_GROUP:
1987 case IPV6_LEAVE_GROUP:
1990 error = ip6_getmoptions(sopt->sopt_name,
1991 in6p->in6p_moptions, &m);
1993 error = sooptcopyout(sopt,
1994 mtod(m, char *), m->m_len);
2000 case IPV6_IPSEC_POLICY:
2004 struct mbuf *m = NULL;
2005 struct mbuf **mp = &m;
2006 size_t ovalsize = sopt->sopt_valsize;
2007 caddr_t oval = (caddr_t)sopt->sopt_val;
2009 error = soopt_getm(sopt, &m); /* XXX */
2012 error = soopt_mcopyin(sopt, m); /* XXX */
2015 sopt->sopt_valsize = ovalsize;
2016 sopt->sopt_val = oval;
2018 req = mtod(m, caddr_t);
2021 error = ipsec6_get_policy(in6p, req, len, mp);
2023 error = soopt_mcopyout(sopt, m); /* XXX */
2024 if (error == 0 && m)
2031 error = ENOPROTOOPT;
2036 } else { /* level != IPPROTO_IPV6 */
2043 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2045 int error = 0, optval, optlen;
2046 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2047 struct inpcb *in6p = sotoinpcb(so);
2048 int level, op, optname;
2050 level = sopt->sopt_level;
2051 op = sopt->sopt_dir;
2052 optname = sopt->sopt_name;
2053 optlen = sopt->sopt_valsize;
2055 if (level != IPPROTO_IPV6) {
2062 * For ICMPv6 sockets, no modification allowed for checksum
2063 * offset, permit "no change" values to help existing apps.
2065 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2066 * for an ICMPv6 socket will fail."
2067 * The current behavior does not meet RFC3542.
2071 if (optlen != sizeof(int)) {
2075 error = sooptcopyin(sopt, &optval, sizeof(optval),
2079 if ((optval % 2) != 0) {
2080 /* the API assumes even offset values */
2082 } else if (so->so_proto->pr_protocol ==
2084 if (optval != icmp6off)
2087 in6p->in6p_cksum = optval;
2091 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2094 optval = in6p->in6p_cksum;
2096 error = sooptcopyout(sopt, &optval, sizeof(optval));
2106 error = ENOPROTOOPT;
2114 * Set up IP6 options in pcb for insertion in output packets or
2115 * specifying behavior of outgoing packets.
2118 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2119 struct socket *so, struct sockopt *sopt)
2121 struct ip6_pktopts *opt = *pktopt;
2123 struct thread *td = sopt->sopt_td;
2125 /* turn off any old options. */
2128 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2129 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2130 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2131 printf("ip6_pcbopts: all specified options are cleared.\n");
2133 ip6_clearpktopts(opt, -1);
2135 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2138 if (!m || m->m_len == 0) {
2140 * Only turning off any previous options, regardless of
2141 * whether the opt is just created or given.
2143 free(opt, M_IP6OPT);
2147 /* set options specified by user. */
2148 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2149 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2150 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2151 free(opt, M_IP6OPT);
2159 * initialize ip6_pktopts. beware that there are non-zero default values in
2163 ip6_initpktopts(struct ip6_pktopts *opt)
2166 bzero(opt, sizeof(*opt));
2167 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2168 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2169 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2170 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2174 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2175 struct ucred *cred, int uproto)
2177 struct ip6_pktopts *opt;
2179 if (*pktopt == NULL) {
2180 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2182 ip6_initpktopts(*pktopt);
2186 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2190 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2192 void *optdata = NULL;
2194 struct ip6_ext *ip6e;
2196 struct in6_pktinfo null_pktinfo;
2197 int deftclass = 0, on;
2198 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2199 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2203 if (pktopt && pktopt->ip6po_pktinfo)
2204 optdata = (void *)pktopt->ip6po_pktinfo;
2206 /* XXX: we don't have to do this every time... */
2207 bzero(&null_pktinfo, sizeof(null_pktinfo));
2208 optdata = (void *)&null_pktinfo;
2210 optdatalen = sizeof(struct in6_pktinfo);
2213 if (pktopt && pktopt->ip6po_tclass >= 0)
2214 optdata = (void *)&pktopt->ip6po_tclass;
2216 optdata = (void *)&deftclass;
2217 optdatalen = sizeof(int);
2220 if (pktopt && pktopt->ip6po_hbh) {
2221 optdata = (void *)pktopt->ip6po_hbh;
2222 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2223 optdatalen = (ip6e->ip6e_len + 1) << 3;
2227 if (pktopt && pktopt->ip6po_rthdr) {
2228 optdata = (void *)pktopt->ip6po_rthdr;
2229 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2230 optdatalen = (ip6e->ip6e_len + 1) << 3;
2233 case IPV6_RTHDRDSTOPTS:
2234 if (pktopt && pktopt->ip6po_dest1) {
2235 optdata = (void *)pktopt->ip6po_dest1;
2236 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2237 optdatalen = (ip6e->ip6e_len + 1) << 3;
2241 if (pktopt && pktopt->ip6po_dest2) {
2242 optdata = (void *)pktopt->ip6po_dest2;
2243 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2244 optdatalen = (ip6e->ip6e_len + 1) << 3;
2248 if (pktopt && pktopt->ip6po_nexthop) {
2249 optdata = (void *)pktopt->ip6po_nexthop;
2250 optdatalen = pktopt->ip6po_nexthop->sa_len;
2253 case IPV6_USE_MIN_MTU:
2255 optdata = (void *)&pktopt->ip6po_minmtu;
2257 optdata = (void *)&defminmtu;
2258 optdatalen = sizeof(int);
2261 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2265 optdata = (void *)&on;
2266 optdatalen = sizeof(on);
2268 case IPV6_PREFER_TEMPADDR:
2270 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2272 optdata = (void *)&defpreftemp;
2273 optdatalen = sizeof(int);
2275 default: /* should not happen */
2277 panic("ip6_getpcbopt: unexpected option\n");
2279 return (ENOPROTOOPT);
2282 error = sooptcopyout(sopt, optdata, optdatalen);
2288 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2293 if (optname == -1 || optname == IPV6_PKTINFO) {
2294 if (pktopt->ip6po_pktinfo)
2295 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2296 pktopt->ip6po_pktinfo = NULL;
2298 if (optname == -1 || optname == IPV6_HOPLIMIT)
2299 pktopt->ip6po_hlim = -1;
2300 if (optname == -1 || optname == IPV6_TCLASS)
2301 pktopt->ip6po_tclass = -1;
2302 if (optname == -1 || optname == IPV6_NEXTHOP) {
2303 if (pktopt->ip6po_nextroute.ro_rt) {
2304 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2305 pktopt->ip6po_nextroute.ro_rt = NULL;
2307 if (pktopt->ip6po_nexthop)
2308 free(pktopt->ip6po_nexthop, M_IP6OPT);
2309 pktopt->ip6po_nexthop = NULL;
2311 if (optname == -1 || optname == IPV6_HOPOPTS) {
2312 if (pktopt->ip6po_hbh)
2313 free(pktopt->ip6po_hbh, M_IP6OPT);
2314 pktopt->ip6po_hbh = NULL;
2316 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2317 if (pktopt->ip6po_dest1)
2318 free(pktopt->ip6po_dest1, M_IP6OPT);
2319 pktopt->ip6po_dest1 = NULL;
2321 if (optname == -1 || optname == IPV6_RTHDR) {
2322 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2323 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2324 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2325 if (pktopt->ip6po_route.ro_rt) {
2326 RTFREE(pktopt->ip6po_route.ro_rt);
2327 pktopt->ip6po_route.ro_rt = NULL;
2330 if (optname == -1 || optname == IPV6_DSTOPTS) {
2331 if (pktopt->ip6po_dest2)
2332 free(pktopt->ip6po_dest2, M_IP6OPT);
2333 pktopt->ip6po_dest2 = NULL;
2337 #define PKTOPT_EXTHDRCPY(type) \
2340 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2341 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2342 if (dst->type == NULL && canwait == M_NOWAIT)\
2344 bcopy(src->type, dst->type, hlen);\
2346 } while (/*CONSTCOND*/ 0)
2349 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2351 if (dst == NULL || src == NULL) {
2352 printf("ip6_clearpktopts: invalid argument\n");
2356 dst->ip6po_hlim = src->ip6po_hlim;
2357 dst->ip6po_tclass = src->ip6po_tclass;
2358 dst->ip6po_flags = src->ip6po_flags;
2359 if (src->ip6po_pktinfo) {
2360 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2362 if (dst->ip6po_pktinfo == NULL)
2364 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2366 if (src->ip6po_nexthop) {
2367 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2369 if (dst->ip6po_nexthop == NULL)
2371 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2372 src->ip6po_nexthop->sa_len);
2374 PKTOPT_EXTHDRCPY(ip6po_hbh);
2375 PKTOPT_EXTHDRCPY(ip6po_dest1);
2376 PKTOPT_EXTHDRCPY(ip6po_dest2);
2377 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2381 ip6_clearpktopts(dst, -1);
2384 #undef PKTOPT_EXTHDRCPY
2386 struct ip6_pktopts *
2387 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2390 struct ip6_pktopts *dst;
2392 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2395 ip6_initpktopts(dst);
2397 if ((error = copypktopts(dst, src, canwait)) != 0) {
2398 free(dst, M_IP6OPT);
2406 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2411 ip6_clearpktopts(pktopt, -1);
2413 free(pktopt, M_IP6OPT);
2417 * Set the IP6 multicast options in response to user setsockopt().
2420 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2423 u_int loop, ifindex;
2424 struct ipv6_mreq *mreq;
2426 struct ip6_moptions *im6o = *im6op;
2427 struct route_in6 ro;
2428 struct in6_multi_mship *imm;
2432 * No multicast option buffer attached to the pcb;
2433 * allocate one and initialize to default values.
2435 im6o = (struct ip6_moptions *)
2436 malloc(sizeof(*im6o), M_IP6MOPTS, M_WAITOK);
2441 im6o->im6o_multicast_ifp = NULL;
2442 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2443 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2444 LIST_INIT(&im6o->im6o_memberships);
2449 case IPV6_MULTICAST_IF:
2451 * Select the interface for outgoing multicast packets.
2453 if (m == NULL || m->m_len != sizeof(u_int)) {
2457 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2458 if (ifindex < 0 || if_index < ifindex) {
2459 error = ENXIO; /* XXX EINVAL? */
2462 ifp = ifnet_byindex(ifindex);
2463 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2464 error = EADDRNOTAVAIL;
2467 im6o->im6o_multicast_ifp = ifp;
2470 case IPV6_MULTICAST_HOPS:
2473 * Set the IP6 hoplimit for outgoing multicast packets.
2476 if (m == NULL || m->m_len != sizeof(int)) {
2480 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2481 if (optval < -1 || optval >= 256)
2483 else if (optval == -1)
2484 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2486 im6o->im6o_multicast_hlim = optval;
2490 case IPV6_MULTICAST_LOOP:
2492 * Set the loopback flag for outgoing multicast packets.
2493 * Must be zero or one.
2495 if (m == NULL || m->m_len != sizeof(u_int)) {
2499 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2504 im6o->im6o_multicast_loop = loop;
2507 case IPV6_JOIN_GROUP:
2509 * Add a multicast group membership.
2510 * Group must be a valid IP6 multicast address.
2512 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2516 mreq = mtod(m, struct ipv6_mreq *);
2518 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2520 * We use the unspecified address to specify to accept
2521 * all multicast addresses. Only super user is allowed
2524 /* XXX-BZ might need a better PRIV_NETINET_x for this */
2525 error = priv_check(curthread, PRIV_NETINET_MROUTE);
2528 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2534 * If no interface was explicitly specified, choose an
2535 * appropriate one according to the given multicast address.
2537 if (mreq->ipv6mr_interface == 0) {
2538 struct sockaddr_in6 *dst;
2541 * Look up the routing table for the
2542 * address, and choose the outgoing interface.
2543 * XXX: is it a good approach?
2546 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2547 bzero(dst, sizeof(*dst));
2548 dst->sin6_family = AF_INET6;
2549 dst->sin6_len = sizeof(*dst);
2550 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2551 rtalloc((struct route *)&ro);
2552 if (ro.ro_rt == NULL) {
2553 error = EADDRNOTAVAIL;
2556 ifp = ro.ro_rt->rt_ifp;
2560 * If the interface is specified, validate it.
2562 if (mreq->ipv6mr_interface < 0 ||
2563 if_index < mreq->ipv6mr_interface) {
2564 error = ENXIO; /* XXX EINVAL? */
2567 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2569 error = ENXIO; /* XXX EINVAL? */
2575 * See if we found an interface, and confirm that it
2576 * supports multicast
2578 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2579 error = EADDRNOTAVAIL;
2583 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2584 error = EADDRNOTAVAIL; /* XXX: should not happen */
2589 * See if the membership already exists.
2591 for (imm = im6o->im6o_memberships.lh_first;
2592 imm != NULL; imm = imm->i6mm_chain.le_next)
2593 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2594 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2595 &mreq->ipv6mr_multiaddr))
2602 * Everything looks good; add a new record to the multicast
2603 * address list for the given interface.
2605 imm = in6_joingroup(ifp, &mreq->ipv6mr_multiaddr, &error, 0);
2608 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2611 case IPV6_LEAVE_GROUP:
2613 * Drop a multicast group membership.
2614 * Group must be a valid IP6 multicast address.
2616 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2620 mreq = mtod(m, struct ipv6_mreq *);
2623 * If an interface address was specified, get a pointer
2624 * to its ifnet structure.
2626 if (mreq->ipv6mr_interface < 0 ||
2627 if_index < mreq->ipv6mr_interface) {
2628 error = ENXIO; /* XXX EINVAL? */
2631 if (mreq->ipv6mr_interface == 0)
2634 ifp = ifnet_byindex(mreq->ipv6mr_interface);
2636 /* Fill in the scope zone ID */
2638 if (in6_setscope(&mreq->ipv6mr_multiaddr, ifp, NULL)) {
2639 /* XXX: should not happen */
2640 error = EADDRNOTAVAIL;
2643 } else if (mreq->ipv6mr_interface != 0) {
2645 * This case happens when the (positive) index is in
2646 * the valid range, but the corresponding interface has
2647 * been detached dynamically (XXX).
2649 error = EADDRNOTAVAIL;
2651 } else { /* ipv6mr_interface == 0 */
2652 struct sockaddr_in6 sa6_mc;
2655 * The API spec says as follows:
2656 * If the interface index is specified as 0, the
2657 * system may choose a multicast group membership to
2658 * drop by matching the multicast address only.
2659 * On the other hand, we cannot disambiguate the scope
2660 * zone unless an interface is provided. Thus, we
2661 * check if there's ambiguity with the default scope
2662 * zone as the last resort.
2664 bzero(&sa6_mc, sizeof(sa6_mc));
2665 sa6_mc.sin6_family = AF_INET6;
2666 sa6_mc.sin6_len = sizeof(sa6_mc);
2667 sa6_mc.sin6_addr = mreq->ipv6mr_multiaddr;
2668 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2671 mreq->ipv6mr_multiaddr = sa6_mc.sin6_addr;
2675 * Find the membership in the membership list.
2677 for (imm = im6o->im6o_memberships.lh_first;
2678 imm != NULL; imm = imm->i6mm_chain.le_next) {
2679 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2680 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2681 &mreq->ipv6mr_multiaddr))
2685 /* Unable to resolve interface */
2686 error = EADDRNOTAVAIL;
2690 * Give up the multicast address record to which the
2691 * membership points.
2693 LIST_REMOVE(imm, i6mm_chain);
2694 in6_delmulti(imm->i6mm_maddr);
2695 free(imm, M_IP6MADDR);
2704 * If all options have default values, no need to keep the mbuf.
2706 if (im6o->im6o_multicast_ifp == NULL &&
2707 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2708 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2709 im6o->im6o_memberships.lh_first == NULL) {
2710 free(*im6op, M_IP6MOPTS);
2718 * Return the IP6 multicast options in response to user getsockopt().
2721 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2723 u_int *hlim, *loop, *ifindex;
2725 *mp = m_get(M_TRYWAIT, MT_HEADER); /* XXX */
2729 case IPV6_MULTICAST_IF:
2730 ifindex = mtod(*mp, u_int *);
2731 (*mp)->m_len = sizeof(u_int);
2732 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2735 *ifindex = im6o->im6o_multicast_ifp->if_index;
2738 case IPV6_MULTICAST_HOPS:
2739 hlim = mtod(*mp, u_int *);
2740 (*mp)->m_len = sizeof(u_int);
2742 *hlim = ip6_defmcasthlim;
2744 *hlim = im6o->im6o_multicast_hlim;
2747 case IPV6_MULTICAST_LOOP:
2748 loop = mtod(*mp, u_int *);
2749 (*mp)->m_len = sizeof(u_int);
2751 *loop = ip6_defmcasthlim;
2753 *loop = im6o->im6o_multicast_loop;
2757 return (EOPNOTSUPP);
2762 * Discard the IP6 multicast options.
2765 ip6_freemoptions(struct ip6_moptions *im6o)
2767 struct in6_multi_mship *imm;
2772 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2773 LIST_REMOVE(imm, i6mm_chain);
2774 if (imm->i6mm_maddr)
2775 in6_delmulti(imm->i6mm_maddr);
2776 free(imm, M_IP6MADDR);
2778 free(im6o, M_IP6MOPTS);
2782 * Set IPv6 outgoing packet options based on advanced API.
2785 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2786 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2788 struct cmsghdr *cm = 0;
2790 if (control == NULL || opt == NULL)
2793 ip6_initpktopts(opt);
2798 * If stickyopt is provided, make a local copy of the options
2799 * for this particular packet, then override them by ancillary
2801 * XXX: copypktopts() does not copy the cached route to a next
2802 * hop (if any). This is not very good in terms of efficiency,
2803 * but we can allow this since this option should be rarely
2806 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2811 * XXX: Currently, we assume all the optional information is stored
2814 if (control->m_next)
2817 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2818 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2821 if (control->m_len < CMSG_LEN(0))
2824 cm = mtod(control, struct cmsghdr *);
2825 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2827 if (cm->cmsg_level != IPPROTO_IPV6)
2830 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2831 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2840 * Set a particular packet option, as a sticky option or an ancillary data
2841 * item. "len" can be 0 only when it's a sticky option.
2842 * We have 4 cases of combination of "sticky" and "cmsg":
2843 * "sticky=0, cmsg=0": impossible
2844 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2845 * "sticky=1, cmsg=0": RFC3542 socket option
2846 * "sticky=1, cmsg=1": RFC2292 socket option
2849 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2850 struct ucred *cred, int sticky, int cmsg, int uproto)
2852 int minmtupolicy, preftemp;
2855 if (!sticky && !cmsg) {
2857 printf("ip6_setpktopt: impossible case\n");
2863 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2864 * not be specified in the context of RFC3542. Conversely,
2865 * RFC3542 types should not be specified in the context of RFC2292.
2869 case IPV6_2292PKTINFO:
2870 case IPV6_2292HOPLIMIT:
2871 case IPV6_2292NEXTHOP:
2872 case IPV6_2292HOPOPTS:
2873 case IPV6_2292DSTOPTS:
2874 case IPV6_2292RTHDR:
2875 case IPV6_2292PKTOPTIONS:
2876 return (ENOPROTOOPT);
2879 if (sticky && cmsg) {
2886 case IPV6_RTHDRDSTOPTS:
2888 case IPV6_USE_MIN_MTU:
2891 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2892 return (ENOPROTOOPT);
2897 case IPV6_2292PKTINFO:
2900 struct ifnet *ifp = NULL;
2901 struct in6_pktinfo *pktinfo;
2903 if (len != sizeof(struct in6_pktinfo))
2906 pktinfo = (struct in6_pktinfo *)buf;
2909 * An application can clear any sticky IPV6_PKTINFO option by
2910 * doing a "regular" setsockopt with ipi6_addr being
2911 * in6addr_any and ipi6_ifindex being zero.
2912 * [RFC 3542, Section 6]
2914 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2915 pktinfo->ipi6_ifindex == 0 &&
2916 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2917 ip6_clearpktopts(opt, optname);
2921 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2922 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2926 /* validate the interface index if specified. */
2927 if (pktinfo->ipi6_ifindex > if_index ||
2928 pktinfo->ipi6_ifindex < 0) {
2931 if (pktinfo->ipi6_ifindex) {
2932 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2938 * We store the address anyway, and let in6_selectsrc()
2939 * validate the specified address. This is because ipi6_addr
2940 * may not have enough information about its scope zone, and
2941 * we may need additional information (such as outgoing
2942 * interface or the scope zone of a destination address) to
2943 * disambiguate the scope.
2944 * XXX: the delay of the validation may confuse the
2945 * application when it is used as a sticky option.
2947 if (opt->ip6po_pktinfo == NULL) {
2948 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2949 M_IP6OPT, M_NOWAIT);
2950 if (opt->ip6po_pktinfo == NULL)
2953 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2957 case IPV6_2292HOPLIMIT:
2963 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2964 * to simplify the ordering among hoplimit options.
2966 if (optname == IPV6_HOPLIMIT && sticky)
2967 return (ENOPROTOOPT);
2969 if (len != sizeof(int))
2972 if (*hlimp < -1 || *hlimp > 255)
2975 opt->ip6po_hlim = *hlimp;
2983 if (len != sizeof(int))
2985 tclass = *(int *)buf;
2986 if (tclass < -1 || tclass > 255)
2989 opt->ip6po_tclass = tclass;
2993 case IPV6_2292NEXTHOP:
2996 error = priv_check_cred(cred,
2997 PRIV_NETINET_SETHDROPTS, 0);
3002 if (len == 0) { /* just remove the option */
3003 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3007 /* check if cmsg_len is large enough for sa_len */
3008 if (len < sizeof(struct sockaddr) || len < *buf)
3011 switch (((struct sockaddr *)buf)->sa_family) {
3014 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3017 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3020 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3021 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3024 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3030 case AF_LINK: /* should eventually be supported */
3032 return (EAFNOSUPPORT);
3035 /* turn off the previous option, then set the new option. */
3036 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3037 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3038 if (opt->ip6po_nexthop == NULL)
3040 bcopy(buf, opt->ip6po_nexthop, *buf);
3043 case IPV6_2292HOPOPTS:
3046 struct ip6_hbh *hbh;
3050 * XXX: We don't allow a non-privileged user to set ANY HbH
3051 * options, since per-option restriction has too much
3055 error = priv_check_cred(cred,
3056 PRIV_NETINET_SETHDROPTS, 0);
3062 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3063 break; /* just remove the option */
3066 /* message length validation */
3067 if (len < sizeof(struct ip6_hbh))
3069 hbh = (struct ip6_hbh *)buf;
3070 hbhlen = (hbh->ip6h_len + 1) << 3;
3074 /* turn off the previous option, then set the new option. */
3075 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3076 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3077 if (opt->ip6po_hbh == NULL)
3079 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3084 case IPV6_2292DSTOPTS:
3086 case IPV6_RTHDRDSTOPTS:
3088 struct ip6_dest *dest, **newdest = NULL;
3091 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3092 error = priv_check_cred(cred,
3093 PRIV_NETINET_SETHDROPTS, 0);
3099 ip6_clearpktopts(opt, optname);
3100 break; /* just remove the option */
3103 /* message length validation */
3104 if (len < sizeof(struct ip6_dest))
3106 dest = (struct ip6_dest *)buf;
3107 destlen = (dest->ip6d_len + 1) << 3;
3112 * Determine the position that the destination options header
3113 * should be inserted; before or after the routing header.
3116 case IPV6_2292DSTOPTS:
3118 * The old advacned API is ambiguous on this point.
3119 * Our approach is to determine the position based
3120 * according to the existence of a routing header.
3121 * Note, however, that this depends on the order of the
3122 * extension headers in the ancillary data; the 1st
3123 * part of the destination options header must appear
3124 * before the routing header in the ancillary data,
3126 * RFC3542 solved the ambiguity by introducing
3127 * separate ancillary data or option types.
3129 if (opt->ip6po_rthdr == NULL)
3130 newdest = &opt->ip6po_dest1;
3132 newdest = &opt->ip6po_dest2;
3134 case IPV6_RTHDRDSTOPTS:
3135 newdest = &opt->ip6po_dest1;
3138 newdest = &opt->ip6po_dest2;
3142 /* turn off the previous option, then set the new option. */
3143 ip6_clearpktopts(opt, optname);
3144 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3145 if (*newdest == NULL)
3147 bcopy(dest, *newdest, destlen);
3152 case IPV6_2292RTHDR:
3155 struct ip6_rthdr *rth;
3159 ip6_clearpktopts(opt, IPV6_RTHDR);
3160 break; /* just remove the option */
3163 /* message length validation */
3164 if (len < sizeof(struct ip6_rthdr))
3166 rth = (struct ip6_rthdr *)buf;
3167 rthlen = (rth->ip6r_len + 1) << 3;
3171 switch (rth->ip6r_type) {
3172 case IPV6_RTHDR_TYPE_0:
3173 if (rth->ip6r_len == 0) /* must contain one addr */
3175 if (rth->ip6r_len % 2) /* length must be even */
3177 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3181 return (EINVAL); /* not supported */
3184 /* turn off the previous option */
3185 ip6_clearpktopts(opt, IPV6_RTHDR);
3186 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3187 if (opt->ip6po_rthdr == NULL)
3189 bcopy(rth, opt->ip6po_rthdr, rthlen);
3194 case IPV6_USE_MIN_MTU:
3195 if (len != sizeof(int))
3197 minmtupolicy = *(int *)buf;
3198 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3199 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3200 minmtupolicy != IP6PO_MINMTU_ALL) {
3203 opt->ip6po_minmtu = minmtupolicy;
3207 if (len != sizeof(int))
3210 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3212 * we ignore this option for TCP sockets.
3213 * (RFC3542 leaves this case unspecified.)
3215 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3217 opt->ip6po_flags |= IP6PO_DONTFRAG;
3220 case IPV6_PREFER_TEMPADDR:
3221 if (len != sizeof(int))
3223 preftemp = *(int *)buf;
3224 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3225 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3226 preftemp != IP6PO_TEMPADDR_PREFER) {
3229 opt->ip6po_prefer_tempaddr = preftemp;
3233 return (ENOPROTOOPT);
3234 } /* end of switch */
3240 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3241 * packet to the input queue of a specified interface. Note that this
3242 * calls the output routine of the loopback "driver", but with an interface
3243 * pointer that might NOT be &loif -- easier than replicating that code here.
3246 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3249 struct ip6_hdr *ip6;
3251 copym = m_copy(m, 0, M_COPYALL);
3256 * Make sure to deep-copy IPv6 header portion in case the data
3257 * is in an mbuf cluster, so that we can safely override the IPv6
3258 * header portion later.
3260 if ((copym->m_flags & M_EXT) != 0 ||
3261 copym->m_len < sizeof(struct ip6_hdr)) {
3262 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3268 if (copym->m_len < sizeof(*ip6)) {
3274 ip6 = mtod(copym, struct ip6_hdr *);
3276 * clear embedded scope identifiers if necessary.
3277 * in6_clearscope will touch the addresses only when necessary.
3279 in6_clearscope(&ip6->ip6_src);
3280 in6_clearscope(&ip6->ip6_dst);
3282 (void)if_simloop(ifp, copym, dst->sin6_family, 0);
3286 * Chop IPv6 header off from the payload.
3289 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3292 struct ip6_hdr *ip6;
3294 ip6 = mtod(m, struct ip6_hdr *);
3295 if (m->m_len > sizeof(*ip6)) {
3296 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3301 M_MOVE_PKTHDR(mh, m);
3302 MH_ALIGN(mh, sizeof(*ip6));
3303 m->m_len -= sizeof(*ip6);
3304 m->m_data += sizeof(*ip6);
3307 m->m_len = sizeof(*ip6);
3308 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3310 exthdrs->ip6e_ip6 = m;
3315 * Compute IPv6 extension header length.
3318 ip6_optlen(struct inpcb *in6p)
3322 if (!in6p->in6p_outputopts)
3327 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3329 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3330 if (in6p->in6p_outputopts->ip6po_rthdr)
3331 /* dest1 is valid with rthdr only */
3332 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3333 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3334 len += elen(in6p->in6p_outputopts->ip6po_dest2);