1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/ip6_output.c,v 1.4 2003/06/25 03:56:04 dillon Exp $ */
3 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
69 #include "opt_ip6fw.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
74 #include <sys/param.h>
75 #include <sys/malloc.h>
77 #include <sys/errno.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
86 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet6/in6_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet6/nd6.h>
98 #include <netinet6/ipsec.h>
100 #include <netinet6/ipsec6.h>
102 #include <netkey/key.h>
106 #include <netipsec/ipsec.h>
107 #include <netipsec/ipsec6.h>
108 #include <netipsec/key.h>
109 #endif /* FAST_IPSEC */
111 #include <netinet6/ip6_fw.h>
113 #include <net/net_osdep.h>
116 extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
118 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
121 struct mbuf *ip6e_ip6;
122 struct mbuf *ip6e_hbh;
123 struct mbuf *ip6e_dest1;
124 struct mbuf *ip6e_rthdr;
125 struct mbuf *ip6e_dest2;
128 static int ip6_pcbopts __P((struct ip6_pktopts **, struct mbuf *,
129 struct socket *, struct sockopt *sopt));
130 static int ip6_setmoptions __P((int, struct ip6_moptions **, struct mbuf *));
131 static int ip6_getmoptions __P((int, struct ip6_moptions *, struct mbuf **));
132 static int ip6_copyexthdr __P((struct mbuf **, caddr_t, int));
133 static int ip6_insertfraghdr __P((struct mbuf *, struct mbuf *, int,
134 struct ip6_frag **));
135 static int ip6_insert_jumboopt __P((struct ip6_exthdrs *, u_int32_t));
136 static int ip6_splithdr __P((struct mbuf *, struct ip6_exthdrs *));
139 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
140 * header (with pri, len, nxt, hlim, src, dst).
141 * This function may modify ver and hlim only.
142 * The mbuf chain containing the packet will be freed.
143 * The mbuf opt, if present, will not be freed.
145 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
146 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
147 * which is rt_rmx.rmx_mtu.
150 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
152 struct ip6_pktopts *opt;
153 struct route_in6 *ro;
155 struct ip6_moptions *im6o;
156 struct ifnet **ifpp; /* XXX: just for statistics */
159 struct ip6_hdr *ip6, *mhip6;
160 struct ifnet *ifp, *origifp;
162 int hlen, tlen, len, off;
163 struct route_in6 ip6route;
164 struct sockaddr_in6 *dst;
166 struct in6_ifaddr *ia = NULL;
168 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
169 struct ip6_exthdrs exthdrs;
170 struct in6_addr finaldst;
171 struct route_in6 *ro_pmtu = NULL;
175 int needipsectun = 0;
176 struct secpolicy *sp = NULL;
177 struct socket *so = inp ? inp->inp_socket : NULL;
179 ip6 = mtod(m, struct ip6_hdr *);
182 int needipsectun = 0;
183 struct secpolicy *sp = NULL;
185 ip6 = mtod(m, struct ip6_hdr *);
186 #endif /* FAST_IPSEC */
188 #define MAKE_EXTHDR(hp, mp) \
191 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
192 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
193 ((eh)->ip6e_len + 1) << 3); \
199 bzero(&exthdrs, sizeof(exthdrs));
202 /* Hop-by-Hop options header */
203 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
204 /* Destination options header(1st part) */
205 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
207 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
208 /* Destination options header(2nd part) */
209 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
213 /* get a security policy for this packet */
215 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
217 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
220 ipsec6stat.out_inval++;
227 switch (sp->policy) {
228 case IPSEC_POLICY_DISCARD:
230 * This packet is just discarded.
232 ipsec6stat.out_polvio++;
235 case IPSEC_POLICY_BYPASS:
236 case IPSEC_POLICY_NONE:
237 /* no need to do IPsec. */
241 case IPSEC_POLICY_IPSEC:
242 if (sp->req == NULL) {
243 /* acquire a policy */
244 error = key_spdacquire(sp);
250 case IPSEC_POLICY_ENTRUST:
252 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
256 /* get a security policy for this packet */
258 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
260 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
263 newipsecstat.ips_out_inval++;
270 switch (sp->policy) {
271 case IPSEC_POLICY_DISCARD:
273 * This packet is just discarded.
275 newipsecstat.ips_out_polvio++;
278 case IPSEC_POLICY_BYPASS:
279 case IPSEC_POLICY_NONE:
280 /* no need to do IPsec. */
284 case IPSEC_POLICY_IPSEC:
285 if (sp->req == NULL) {
286 /* acquire a policy */
287 error = key_spdacquire(sp);
293 case IPSEC_POLICY_ENTRUST:
295 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
297 #endif /* FAST_IPSEC */
300 * Calculate the total length of the extension header chain.
301 * Keep the length of the unfragmentable part for fragmentation.
304 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
305 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
306 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
307 unfragpartlen = optlen + sizeof(struct ip6_hdr);
308 /* NOTE: we don't add AH/ESP length here. do that later. */
309 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
312 * If we need IPsec, or there is at least one extension header,
313 * separate IP6 header from the payload.
315 if ((needipsec || optlen) && !hdrsplit) {
316 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
320 m = exthdrs.ip6e_ip6;
325 ip6 = mtod(m, struct ip6_hdr *);
327 /* adjust mbuf packet header length */
328 m->m_pkthdr.len += optlen;
329 plen = m->m_pkthdr.len - sizeof(*ip6);
331 /* If this is a jumbo payload, insert a jumbo payload option. */
332 if (plen > IPV6_MAXPACKET) {
334 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
338 m = exthdrs.ip6e_ip6;
342 ip6 = mtod(m, struct ip6_hdr *);
343 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
347 ip6->ip6_plen = htons(plen);
350 * Concatenate headers and fill in next header fields.
351 * Here we have, on "m"
353 * and we insert headers accordingly. Finally, we should be getting:
354 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
356 * during the header composing process, "m" points to IPv6 header.
357 * "mprev" points to an extension header prior to esp.
360 u_char *nexthdrp = &ip6->ip6_nxt;
361 struct mbuf *mprev = m;
364 * we treat dest2 specially. this makes IPsec processing
365 * much easier. the goal here is to make mprev point the
366 * mbuf prior to dest2.
368 * result: IPv6 dest2 payload
369 * m and mprev will point to IPv6 header.
371 if (exthdrs.ip6e_dest2) {
373 panic("assumption failed: hdr not split");
374 exthdrs.ip6e_dest2->m_next = m->m_next;
375 m->m_next = exthdrs.ip6e_dest2;
376 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
377 ip6->ip6_nxt = IPPROTO_DSTOPTS;
380 #define MAKE_CHAIN(m, mp, p, i)\
384 panic("assumption failed: hdr not split"); \
385 *mtod((m), u_char *) = *(p);\
387 p = mtod((m), u_char *);\
388 (m)->m_next = (mp)->m_next;\
394 * result: IPv6 hbh dest1 rthdr dest2 payload
395 * m will point to IPv6 header. mprev will point to the
396 * extension header prior to dest2 (rthdr in the above case).
398 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
399 nexthdrp, IPPROTO_HOPOPTS);
400 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
401 nexthdrp, IPPROTO_DSTOPTS);
402 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
403 nexthdrp, IPPROTO_ROUTING);
405 #if defined(IPSEC) || defined(FAST_IPSEC)
410 * pointers after IPsec headers are not valid any more.
411 * other pointers need a great care too.
412 * (IPsec routines should not mangle mbufs prior to AH/ESP)
414 exthdrs.ip6e_dest2 = NULL;
417 struct ip6_rthdr *rh = NULL;
419 struct ipsec_output_state state;
421 if (exthdrs.ip6e_rthdr) {
422 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
423 segleft_org = rh->ip6r_segleft;
424 rh->ip6r_segleft = 0;
427 bzero(&state, sizeof(state));
429 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
433 /* mbuf is already reclaimed in ipsec6_output_trans. */
443 printf("ip6_output (ipsec): error code %d\n", error);
446 /* don't show these error codes to the user */
452 if (exthdrs.ip6e_rthdr) {
453 /* ah6_output doesn't modify mbuf chain */
454 rh->ip6r_segleft = segleft_org;
462 * If there is a routing header, replace destination address field
463 * with the first hop of the routing header.
465 if (exthdrs.ip6e_rthdr) {
466 struct ip6_rthdr *rh =
467 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
468 struct ip6_rthdr *));
469 struct ip6_rthdr0 *rh0;
471 finaldst = ip6->ip6_dst;
472 switch (rh->ip6r_type) {
473 case IPV6_RTHDR_TYPE_0:
474 rh0 = (struct ip6_rthdr0 *)rh;
475 ip6->ip6_dst = rh0->ip6r0_addr[0];
476 bcopy((caddr_t)&rh0->ip6r0_addr[1],
477 (caddr_t)&rh0->ip6r0_addr[0],
478 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
480 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
482 default: /* is it possible? */
488 /* Source address validation */
489 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
490 (flags & IPV6_DADOUTPUT) == 0) {
492 ip6stat.ip6s_badscope++;
495 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
497 ip6stat.ip6s_badscope++;
501 ip6stat.ip6s_localout++;
508 bzero((caddr_t)ro, sizeof(*ro));
511 if (opt && opt->ip6po_rthdr)
512 ro = &opt->ip6po_route;
513 dst = (struct sockaddr_in6 *)&ro->ro_dst;
515 * If there is a cached route,
516 * check that it is to the same destination
517 * and is still up. If not, free it and try again.
519 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
520 dst->sin6_family != AF_INET6 ||
521 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
523 ro->ro_rt = (struct rtentry *)0;
525 if (ro->ro_rt == 0) {
526 bzero(dst, sizeof(*dst));
527 dst->sin6_family = AF_INET6;
528 dst->sin6_len = sizeof(struct sockaddr_in6);
529 dst->sin6_addr = ip6->ip6_dst;
531 /* XXX: sin6_scope_id should already be fixed at this point */
532 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
533 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]);
536 #if defined(IPSEC) || defined(FAST_IPSEC)
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;
562 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
573 printf("ip6_output (ipsec): error code %d\n", error);
576 /* don't show these error codes to the user */
583 exthdrs.ip6e_ip6 = m;
587 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
590 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
591 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
593 * interface selection comes here
594 * if an interface is specified from an upper layer,
597 if (ro->ro_rt == 0) {
599 * non-bsdi always clone routes, if parent is
602 rtalloc((struct route *)ro);
604 if (ro->ro_rt == 0) {
605 ip6stat.ip6s_noroute++;
606 error = EHOSTUNREACH;
607 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
610 ia = ifatoia6(ro->ro_rt->rt_ifa);
611 ifp = ro->ro_rt->rt_ifp;
613 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
614 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
615 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
617 in6_ifstat_inc(ifp, ifs6_out_request);
620 * Check if the outgoing interface conflicts with
621 * the interface specified by ifi6_ifindex (if specified).
622 * Note that loopback interface is always okay.
623 * (this may happen when we are sending a packet to one of
624 * our own addresses.)
626 if (opt && opt->ip6po_pktinfo
627 && opt->ip6po_pktinfo->ipi6_ifindex) {
628 if (!(ifp->if_flags & IFF_LOOPBACK)
629 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
630 ip6stat.ip6s_noroute++;
631 in6_ifstat_inc(ifp, ifs6_out_discard);
632 error = EHOSTUNREACH;
637 if (opt && opt->ip6po_hlim != -1)
638 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
641 struct in6_multi *in6m;
643 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
646 * See if the caller provided any multicast options
650 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
651 if (im6o->im6o_multicast_ifp != NULL)
652 ifp = im6o->im6o_multicast_ifp;
654 ip6->ip6_hlim = ip6_defmcasthlim;
657 * See if the caller provided the outgoing interface
658 * as an ancillary data.
659 * Boundary check for ifindex is assumed to be already done.
661 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
662 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
665 * If the destination is a node-local scope multicast,
666 * the packet should be loop-backed only.
668 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
670 * If the outgoing interface is already specified,
671 * it should be a loopback interface.
673 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
674 ip6stat.ip6s_badscope++;
675 error = ENETUNREACH; /* XXX: better error? */
676 /* XXX correct ifp? */
677 in6_ifstat_inc(ifp, ifs6_out_discard);
684 if (opt && opt->ip6po_hlim != -1)
685 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
688 * If caller did not provide an interface lookup a
689 * default in the routing table. This is either a
690 * default for the speicfied group (i.e. a host
691 * route), or a multicast default (a route for the
695 if (ro->ro_rt == 0) {
696 ro->ro_rt = rtalloc1((struct sockaddr *)
697 &ro->ro_dst, 0, 0UL);
699 if (ro->ro_rt == 0) {
700 ip6stat.ip6s_noroute++;
701 error = EHOSTUNREACH;
702 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
705 ia = ifatoia6(ro->ro_rt->rt_ifa);
706 ifp = ro->ro_rt->rt_ifp;
710 if ((flags & IPV6_FORWARDING) == 0)
711 in6_ifstat_inc(ifp, ifs6_out_request);
712 in6_ifstat_inc(ifp, ifs6_out_mcast);
715 * Confirm that the outgoing interface supports multicast.
717 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
718 ip6stat.ip6s_noroute++;
719 in6_ifstat_inc(ifp, ifs6_out_discard);
723 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
725 (im6o == NULL || im6o->im6o_multicast_loop)) {
727 * If we belong to the destination multicast group
728 * on the outgoing interface, and the caller did not
729 * forbid loopback, loop back a copy.
731 ip6_mloopback(ifp, m, dst);
734 * If we are acting as a multicast router, perform
735 * multicast forwarding as if the packet had just
736 * arrived on the interface to which we are about
737 * to send. The multicast forwarding function
738 * recursively calls this function, using the
739 * IPV6_FORWARDING flag to prevent infinite recursion.
741 * Multicasts that are looped back by ip6_mloopback(),
742 * above, will be forwarded by the ip6_input() routine,
745 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
746 if (ip6_mforward(ip6, ifp, m) != 0) {
753 * Multicasts with a hoplimit of zero may be looped back,
754 * above, but must not be transmitted on a network.
755 * Also, multicasts addressed to the loopback interface
756 * are not sent -- the above call to ip6_mloopback() will
757 * loop back a copy if this host actually belongs to the
758 * destination group on the loopback interface.
760 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
767 * Fill the outgoing inteface to tell the upper layer
768 * to increment per-interface statistics.
774 * Determine path MTU.
777 /* The first hop and the final destination may differ. */
778 struct sockaddr_in6 *sin6_fin =
779 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
780 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
781 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
783 RTFREE(ro_pmtu->ro_rt);
784 ro_pmtu->ro_rt = (struct rtentry *)0;
786 if (ro_pmtu->ro_rt == 0) {
787 bzero(sin6_fin, sizeof(*sin6_fin));
788 sin6_fin->sin6_family = AF_INET6;
789 sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
790 sin6_fin->sin6_addr = finaldst;
792 rtalloc((struct route *)ro_pmtu);
795 if (ro_pmtu->ro_rt != NULL) {
796 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
798 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
799 if (mtu > ifmtu || mtu == 0) {
801 * The MTU on the route is larger than the MTU on
802 * the interface! This shouldn't happen, unless the
803 * MTU of the interface has been changed after the
804 * interface was brought up. Change the MTU in the
805 * route to match the interface MTU (as long as the
806 * field isn't locked).
808 * if MTU on the route is 0, we need to fix the MTU.
809 * this case happens with path MTU discovery timeouts.
812 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
813 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
816 mtu = nd_ifinfo[ifp->if_index].linkmtu;
820 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
822 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
825 /* Fake scoped addresses */
826 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
828 * If source or destination address is a scoped address, and
829 * the packet is going to be sent to a loopback interface,
830 * we should keep the original interface.
834 * XXX: this is a very experimental and temporary solution.
835 * We eventually have sockaddr_in6 and use the sin6_scope_id
836 * field of the structure here.
837 * We rely on the consistency between two scope zone ids
838 * of source and destination, which should already be assured.
839 * Larger scopes than link will be supported in the future.
842 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
843 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
844 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
845 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
847 * XXX: origifp can be NULL even in those two cases above.
848 * For example, if we remove the (only) link-local address
849 * from the loopback interface, and try to send a link-local
850 * address without link-id information. Then the source
851 * address is ::1, and the destination address is the
852 * link-local address with its s6_addr16[1] being zero.
853 * What is worse, if the packet goes to the loopback interface
854 * by a default rejected route, the null pointer would be
855 * passed to looutput, and the kernel would hang.
856 * The following last resort would prevent such disaster.
863 #ifndef SCOPEDROUTING
865 * clear embedded scope identifiers if necessary.
866 * in6_clearscope will touch the addresses only when necessary.
868 in6_clearscope(&ip6->ip6_src);
869 in6_clearscope(&ip6->ip6_dst);
873 * Check with the firewall...
875 if (ip6_fw_enable && ip6_fw_chk_ptr) {
877 m->m_pkthdr.rcvif = NULL; /* XXX */
878 /* If ipfw says divert, we have to just drop packet */
879 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
890 * If the outgoing packet contains a hop-by-hop options header,
891 * it must be examined and processed even by the source node.
892 * (RFC 2460, section 4.)
894 if (exthdrs.ip6e_hbh) {
895 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
896 u_int32_t dummy1; /* XXX unused */
897 u_int32_t dummy2; /* XXX unused */
900 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
901 panic("ip6e_hbh is not continuous");
904 * XXX: if we have to send an ICMPv6 error to the sender,
905 * we need the M_LOOP flag since icmp6_error() expects
906 * the IPv6 and the hop-by-hop options header are
907 * continuous unless the flag is set.
909 m->m_flags |= M_LOOP;
910 m->m_pkthdr.rcvif = ifp;
911 if (ip6_process_hopopts(m,
912 (u_int8_t *)(hbh + 1),
913 ((hbh->ip6h_len + 1) << 3) -
914 sizeof(struct ip6_hbh),
915 &dummy1, &dummy2) < 0) {
916 /* m was already freed at this point */
917 error = EINVAL;/* better error? */
920 m->m_flags &= ~M_LOOP; /* XXX */
921 m->m_pkthdr.rcvif = NULL;
925 * Check if we want to allow this packet to be processed.
926 * Consider it to be bad if not.
931 if ((*fr_checkp)((struct ip *)ip6, sizeof(*ip6), ifp, 1, &m1))
936 ip6 = mtod(m, struct ip6_hdr *);
940 * Send the packet to the outgoing interface.
941 * If necessary, do IPv6 fragmentation before sending.
943 tlen = m->m_pkthdr.len;
947 * On any link that cannot convey a 1280-octet packet in one piece,
948 * link-specific fragmentation and reassembly must be provided at
949 * a layer below IPv6. [RFC 2460, sec.5]
950 * Thus if the interface has ability of link-level fragmentation,
951 * we can just send the packet even if the packet size is
952 * larger than the link's MTU.
953 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
956 || ifp->if_flags & IFF_FRAGMENTABLE
960 /* Record statistics for this interface address. */
961 if (ia && !(flags & IPV6_FORWARDING)) {
962 ia->ia_ifa.if_opackets++;
963 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
966 /* clean ipsec history once it goes out of the node */
969 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
971 } else if (mtu < IPV6_MMTU) {
973 * note that path MTU is never less than IPV6_MMTU
977 in6_ifstat_inc(ifp, ifs6_out_fragfail);
979 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
981 in6_ifstat_inc(ifp, ifs6_out_fragfail);
984 struct mbuf **mnext, *m_frgpart;
985 struct ip6_frag *ip6f;
986 u_int32_t id = htonl(ip6_id++);
990 * Too large for the destination or interface;
991 * fragment if possible.
992 * Must be able to put at least 8 bytes per fragment.
994 hlen = unfragpartlen;
995 if (mtu > IPV6_MAXPACKET)
996 mtu = IPV6_MAXPACKET;
998 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
1001 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1005 mnext = &m->m_nextpkt;
1008 * Change the next header field of the last header in the
1009 * unfragmentable part.
1011 if (exthdrs.ip6e_rthdr) {
1012 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1013 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1014 } else if (exthdrs.ip6e_dest1) {
1015 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1016 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1017 } else if (exthdrs.ip6e_hbh) {
1018 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1019 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1021 nextproto = ip6->ip6_nxt;
1022 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1026 * Loop through length of segment after first fragment,
1027 * make new header and copy data of each part and link onto
1031 for (off = hlen; off < tlen; off += len) {
1032 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1035 ip6stat.ip6s_odropped++;
1038 m->m_pkthdr.rcvif = NULL;
1039 m->m_flags = m0->m_flags & M_COPYFLAGS;
1041 mnext = &m->m_nextpkt;
1042 m->m_data += max_linkhdr;
1043 mhip6 = mtod(m, struct ip6_hdr *);
1045 m->m_len = sizeof(*mhip6);
1046 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1048 ip6stat.ip6s_odropped++;
1051 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1052 if (off + len >= tlen)
1055 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1056 mhip6->ip6_plen = htons((u_short)(len + hlen +
1058 sizeof(struct ip6_hdr)));
1059 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1061 ip6stat.ip6s_odropped++;
1064 m_cat(m, m_frgpart);
1065 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1066 m->m_pkthdr.rcvif = (struct ifnet *)0;
1067 ip6f->ip6f_reserved = 0;
1068 ip6f->ip6f_ident = id;
1069 ip6f->ip6f_nxt = nextproto;
1070 ip6stat.ip6s_ofragments++;
1071 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1074 in6_ifstat_inc(ifp, ifs6_out_fragok);
1078 * Remove leading garbages.
1084 for (m0 = m; m; m = m0) {
1088 /* Record statistics for this interface address. */
1090 ia->ia_ifa.if_opackets++;
1091 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1094 /* clean ipsec history once it goes out of the node */
1097 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1103 ip6stat.ip6s_fragmented++;
1106 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1108 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1109 RTFREE(ro_pmtu->ro_rt);
1119 #endif /* FAST_IPSEC */
1124 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1125 m_freem(exthdrs.ip6e_dest1);
1126 m_freem(exthdrs.ip6e_rthdr);
1127 m_freem(exthdrs.ip6e_dest2);
1135 ip6_copyexthdr(mp, hdr, hlen)
1142 if (hlen > MCLBYTES)
1143 return(ENOBUFS); /* XXX */
1145 MGET(m, M_DONTWAIT, MT_DATA);
1150 MCLGET(m, M_DONTWAIT);
1151 if ((m->m_flags & M_EXT) == 0) {
1158 bcopy(hdr, mtod(m, caddr_t), hlen);
1165 * Insert jumbo payload option.
1168 ip6_insert_jumboopt(exthdrs, plen)
1169 struct ip6_exthdrs *exthdrs;
1176 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1179 * If there is no hop-by-hop options header, allocate new one.
1180 * If there is one but it doesn't have enough space to store the
1181 * jumbo payload option, allocate a cluster to store the whole options.
1182 * Otherwise, use it to store the options.
1184 if (exthdrs->ip6e_hbh == 0) {
1185 MGET(mopt, M_DONTWAIT, MT_DATA);
1188 mopt->m_len = JUMBOOPTLEN;
1189 optbuf = mtod(mopt, u_char *);
1190 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1191 exthdrs->ip6e_hbh = mopt;
1193 struct ip6_hbh *hbh;
1195 mopt = exthdrs->ip6e_hbh;
1196 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1199 * - exthdrs->ip6e_hbh is not referenced from places
1200 * other than exthdrs.
1201 * - exthdrs->ip6e_hbh is not an mbuf chain.
1203 int oldoptlen = mopt->m_len;
1207 * XXX: give up if the whole (new) hbh header does
1208 * not fit even in an mbuf cluster.
1210 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1214 * As a consequence, we must always prepare a cluster
1217 MGET(n, M_DONTWAIT, MT_DATA);
1219 MCLGET(n, M_DONTWAIT);
1220 if ((n->m_flags & M_EXT) == 0) {
1227 n->m_len = oldoptlen + JUMBOOPTLEN;
1228 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1230 optbuf = mtod(n, caddr_t) + oldoptlen;
1232 mopt = exthdrs->ip6e_hbh = n;
1234 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1235 mopt->m_len += JUMBOOPTLEN;
1237 optbuf[0] = IP6OPT_PADN;
1241 * Adjust the header length according to the pad and
1242 * the jumbo payload option.
1244 hbh = mtod(mopt, struct ip6_hbh *);
1245 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1248 /* fill in the option. */
1249 optbuf[2] = IP6OPT_JUMBO;
1251 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1252 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1254 /* finally, adjust the packet header length */
1255 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1262 * Insert fragment header and copy unfragmentable header portions.
1265 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1266 struct mbuf *m0, *m;
1268 struct ip6_frag **frghdrp;
1270 struct mbuf *n, *mlast;
1272 if (hlen > sizeof(struct ip6_hdr)) {
1273 n = m_copym(m0, sizeof(struct ip6_hdr),
1274 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1281 /* Search for the last mbuf of unfragmentable part. */
1282 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1285 if ((mlast->m_flags & M_EXT) == 0 &&
1286 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1287 /* use the trailing space of the last mbuf for the fragment hdr */
1289 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1290 mlast->m_len += sizeof(struct ip6_frag);
1291 m->m_pkthdr.len += sizeof(struct ip6_frag);
1293 /* allocate a new mbuf for the fragment header */
1296 MGET(mfrg, M_DONTWAIT, MT_DATA);
1299 mfrg->m_len = sizeof(struct ip6_frag);
1300 *frghdrp = mtod(mfrg, struct ip6_frag *);
1301 mlast->m_next = mfrg;
1308 * IP6 socket option processing.
1311 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1314 struct inpcb *in6p = sotoinpcb(so);
1316 int level, op, optname;
1321 level = sopt->sopt_level;
1322 op = sopt->sopt_dir;
1323 optname = sopt->sopt_name;
1324 optlen = sopt->sopt_valsize;
1327 panic("ip6_ctloutput: arg soopt is NULL");
1333 privileged = (td == NULL || suser(td)) ? 0 : 1;
1335 if (level == IPPROTO_IPV6) {
1340 case IPV6_PKTOPTIONS:
1344 error = soopt_getm(sopt, &m); /* XXX */
1347 error = soopt_mcopyin(sopt, m); /* XXX */
1350 error = ip6_pcbopts(&in6p->in6p_outputopts,
1352 m_freem(m); /* XXX */
1357 * Use of some Hop-by-Hop options or some
1358 * Destination options, might require special
1359 * privilege. That is, normal applications
1360 * (without special privilege) might be forbidden
1361 * from setting certain options in outgoing packets,
1362 * and might never see certain options in received
1363 * packets. [RFC 2292 Section 6]
1364 * KAME specific note:
1365 * KAME prevents non-privileged users from sending or
1366 * receiving ANY hbh/dst options in order to avoid
1367 * overhead of parsing options in the kernel.
1369 case IPV6_UNICAST_HOPS:
1374 if (optlen != sizeof(int)) {
1378 error = sooptcopyin(sopt, &optval,
1379 sizeof optval, sizeof optval);
1384 case IPV6_UNICAST_HOPS:
1385 if (optval < -1 || optval >= 256)
1388 /* -1 = kernel default */
1389 in6p->in6p_hops = optval;
1391 if ((in6p->in6p_vflag &
1393 in6p->inp_ip_ttl = optval;
1396 #define OPTSET(bit) \
1399 in6p->in6p_flags |= (bit); \
1401 in6p->in6p_flags &= ~(bit); \
1403 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1406 in6p->in6p_cksum = optval;
1415 * make setsockopt(IPV6_V6ONLY)
1416 * available only prior to bind(2).
1417 * see ipng mailing list, Jun 22 2001.
1419 if (in6p->in6p_lport ||
1420 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1425 OPTSET(IN6P_IPV6_V6ONLY);
1427 in6p->in6p_vflag &= ~INP_IPV4;
1429 in6p->in6p_vflag |= INP_IPV4;
1440 if (optlen != sizeof(int)) {
1444 error = sooptcopyin(sopt, &optval,
1445 sizeof optval, sizeof optval);
1450 OPTSET(IN6P_PKTINFO);
1453 OPTSET(IN6P_HOPLIMIT);
1457 * Check super-user privilege.
1458 * See comments for IPV6_RECVHOPOPTS.
1462 OPTSET(IN6P_HOPOPTS);
1467 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1476 case IPV6_MULTICAST_IF:
1477 case IPV6_MULTICAST_HOPS:
1478 case IPV6_MULTICAST_LOOP:
1479 case IPV6_JOIN_GROUP:
1480 case IPV6_LEAVE_GROUP:
1483 if (sopt->sopt_valsize > MLEN) {
1488 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_HEADER);
1493 m->m_len = sopt->sopt_valsize;
1494 error = sooptcopyin(sopt, mtod(m, char *),
1495 m->m_len, m->m_len);
1496 error = ip6_setmoptions(sopt->sopt_name,
1497 &in6p->in6p_moptions,
1503 case IPV6_PORTRANGE:
1504 error = sooptcopyin(sopt, &optval,
1505 sizeof optval, sizeof optval);
1510 case IPV6_PORTRANGE_DEFAULT:
1511 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1512 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1515 case IPV6_PORTRANGE_HIGH:
1516 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1517 in6p->in6p_flags |= IN6P_HIGHPORT;
1520 case IPV6_PORTRANGE_LOW:
1521 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1522 in6p->in6p_flags |= IN6P_LOWPORT;
1531 #if defined(IPSEC) || defined(FAST_IPSEC)
1532 case IPV6_IPSEC_POLICY:
1538 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1540 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1543 req = mtod(m, caddr_t);
1546 error = ipsec6_set_policy(in6p, optname, req,
1551 #endif /* KAME IPSEC */
1559 struct mbuf **mp = &m;
1561 if (ip6_fw_ctl_ptr == NULL)
1564 if ((error = soopt_getm(sopt, &m)) != 0)
1567 if ((error = soopt_mcopyin(sopt, m)) != 0)
1569 error = (*ip6_fw_ctl_ptr)(optname, mp);
1575 error = ENOPROTOOPT;
1583 case IPV6_PKTOPTIONS:
1584 if (in6p->in6p_options) {
1586 m = m_copym(in6p->in6p_options,
1587 0, M_COPYALL, M_WAIT);
1588 error = soopt_mcopyout(sopt, m);
1592 sopt->sopt_valsize = 0;
1595 case IPV6_UNICAST_HOPS:
1600 case IPV6_PORTRANGE:
1603 case IPV6_UNICAST_HOPS:
1604 optval = in6p->in6p_hops;
1608 optval = in6p->in6p_cksum;
1612 optval = OPTBIT(IN6P_FAITH);
1616 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1619 case IPV6_PORTRANGE:
1622 flags = in6p->in6p_flags;
1623 if (flags & IN6P_HIGHPORT)
1624 optval = IPV6_PORTRANGE_HIGH;
1625 else if (flags & IN6P_LOWPORT)
1626 optval = IPV6_PORTRANGE_LOW;
1632 error = sooptcopyout(sopt, &optval,
1641 if (optname == IPV6_HOPOPTS ||
1642 optname == IPV6_DSTOPTS ||
1647 optval = OPTBIT(IN6P_PKTINFO);
1650 optval = OPTBIT(IN6P_HOPLIMIT);
1655 optval = OPTBIT(IN6P_HOPOPTS);
1658 optval = OPTBIT(IN6P_RTHDR);
1663 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1666 error = sooptcopyout(sopt, &optval,
1670 case IPV6_MULTICAST_IF:
1671 case IPV6_MULTICAST_HOPS:
1672 case IPV6_MULTICAST_LOOP:
1673 case IPV6_JOIN_GROUP:
1674 case IPV6_LEAVE_GROUP:
1677 error = ip6_getmoptions(sopt->sopt_name,
1678 in6p->in6p_moptions, &m);
1680 error = sooptcopyout(sopt,
1681 mtod(m, char *), m->m_len);
1686 #if defined(IPSEC) || defined(FAST_IPSEC)
1687 case IPV6_IPSEC_POLICY:
1691 struct mbuf *m = NULL;
1692 struct mbuf **mp = &m;
1694 error = soopt_getm(sopt, &m); /* XXX */
1697 error = soopt_mcopyin(sopt, m); /* XXX */
1701 req = mtod(m, caddr_t);
1704 error = ipsec6_get_policy(in6p, req, len, mp);
1706 error = soopt_mcopyout(sopt, m); /*XXX*/
1707 if (error == 0 && m)
1711 #endif /* KAME IPSEC */
1716 struct mbuf **mp = &m;
1718 if (ip6_fw_ctl_ptr == NULL)
1722 error = (*ip6_fw_ctl_ptr)(optname, mp);
1724 error = soopt_mcopyout(sopt, m); /* XXX */
1725 if (error == 0 && m)
1731 error = ENOPROTOOPT;
1743 * Set up IP6 options in pcb for insertion in output packets or
1744 * specifying behavior of outgoing packets.
1747 ip6_pcbopts(pktopt, m, so, sopt)
1748 struct ip6_pktopts **pktopt;
1751 struct sockopt *sopt;
1753 struct ip6_pktopts *opt = *pktopt;
1755 struct proc *p = sopt->sopt_p;
1758 /* turn off any old options. */
1761 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1762 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1763 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1764 printf("ip6_pcbopts: all specified options are cleared.\n");
1766 ip6_clearpktopts(opt, 1, -1);
1768 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1771 if (!m || m->m_len == 0) {
1773 * Only turning off any previous options, regardless of
1774 * whether the opt is just created or given.
1776 free(opt, M_IP6OPT);
1780 /* set options specified by user. */
1781 if (p && !suser_xxx(p->p_ucred, 0))
1783 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
1784 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
1785 free(opt, M_IP6OPT);
1793 * initialize ip6_pktopts. beware that there are non-zero default values in
1797 init_ip6pktopts(opt)
1798 struct ip6_pktopts *opt;
1801 bzero(opt, sizeof(*opt));
1802 opt->ip6po_hlim = -1; /* -1 means default hop limit */
1806 ip6_clearpktopts(pktopt, needfree, optname)
1807 struct ip6_pktopts *pktopt;
1808 int needfree, optname;
1813 if (optname == -1) {
1814 if (needfree && pktopt->ip6po_pktinfo)
1815 free(pktopt->ip6po_pktinfo, M_IP6OPT);
1816 pktopt->ip6po_pktinfo = NULL;
1819 pktopt->ip6po_hlim = -1;
1820 if (optname == -1) {
1821 if (needfree && pktopt->ip6po_nexthop)
1822 free(pktopt->ip6po_nexthop, M_IP6OPT);
1823 pktopt->ip6po_nexthop = NULL;
1825 if (optname == -1) {
1826 if (needfree && pktopt->ip6po_hbh)
1827 free(pktopt->ip6po_hbh, M_IP6OPT);
1828 pktopt->ip6po_hbh = NULL;
1830 if (optname == -1) {
1831 if (needfree && pktopt->ip6po_dest1)
1832 free(pktopt->ip6po_dest1, M_IP6OPT);
1833 pktopt->ip6po_dest1 = NULL;
1835 if (optname == -1) {
1836 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1837 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1838 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1839 if (pktopt->ip6po_route.ro_rt) {
1840 RTFREE(pktopt->ip6po_route.ro_rt);
1841 pktopt->ip6po_route.ro_rt = NULL;
1844 if (optname == -1) {
1845 if (needfree && pktopt->ip6po_dest2)
1846 free(pktopt->ip6po_dest2, M_IP6OPT);
1847 pktopt->ip6po_dest2 = NULL;
1851 #define PKTOPT_EXTHDRCPY(type) \
1855 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1856 dst->type = malloc(hlen, M_IP6OPT, canwait);\
1857 if (dst->type == NULL && canwait == M_NOWAIT)\
1859 bcopy(src->type, dst->type, hlen);\
1863 struct ip6_pktopts *
1864 ip6_copypktopts(src, canwait)
1865 struct ip6_pktopts *src;
1868 struct ip6_pktopts *dst;
1871 printf("ip6_clearpktopts: invalid argument\n");
1875 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
1876 if (dst == NULL && canwait == M_NOWAIT)
1878 bzero(dst, sizeof(*dst));
1880 dst->ip6po_hlim = src->ip6po_hlim;
1881 if (src->ip6po_pktinfo) {
1882 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
1884 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
1886 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1888 if (src->ip6po_nexthop) {
1889 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
1891 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
1893 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
1894 src->ip6po_nexthop->sa_len);
1896 PKTOPT_EXTHDRCPY(ip6po_hbh);
1897 PKTOPT_EXTHDRCPY(ip6po_dest1);
1898 PKTOPT_EXTHDRCPY(ip6po_dest2);
1899 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1903 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
1904 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
1905 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
1906 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
1907 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
1908 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
1909 free(dst, M_IP6OPT);
1912 #undef PKTOPT_EXTHDRCPY
1915 ip6_freepcbopts(pktopt)
1916 struct ip6_pktopts *pktopt;
1921 ip6_clearpktopts(pktopt, 1, -1);
1923 free(pktopt, M_IP6OPT);
1927 * Set the IP6 multicast options in response to user setsockopt().
1930 ip6_setmoptions(optname, im6op, m)
1932 struct ip6_moptions **im6op;
1936 u_int loop, ifindex;
1937 struct ipv6_mreq *mreq;
1939 struct ip6_moptions *im6o = *im6op;
1940 struct route_in6 ro;
1941 struct sockaddr_in6 *dst;
1942 struct in6_multi_mship *imm;
1943 struct proc *p = curproc; /* XXX */
1947 * No multicast option buffer attached to the pcb;
1948 * allocate one and initialize to default values.
1950 im6o = (struct ip6_moptions *)
1951 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1956 im6o->im6o_multicast_ifp = NULL;
1957 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1958 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1959 LIST_INIT(&im6o->im6o_memberships);
1964 case IPV6_MULTICAST_IF:
1966 * Select the interface for outgoing multicast packets.
1968 if (m == NULL || m->m_len != sizeof(u_int)) {
1972 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1973 if (ifindex < 0 || if_index < ifindex) {
1974 error = ENXIO; /* XXX EINVAL? */
1977 ifp = ifindex2ifnet[ifindex];
1978 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1979 error = EADDRNOTAVAIL;
1982 im6o->im6o_multicast_ifp = ifp;
1985 case IPV6_MULTICAST_HOPS:
1988 * Set the IP6 hoplimit for outgoing multicast packets.
1991 if (m == NULL || m->m_len != sizeof(int)) {
1995 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1996 if (optval < -1 || optval >= 256)
1998 else if (optval == -1)
1999 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2001 im6o->im6o_multicast_hlim = optval;
2005 case IPV6_MULTICAST_LOOP:
2007 * Set the loopback flag for outgoing multicast packets.
2008 * Must be zero or one.
2010 if (m == NULL || m->m_len != sizeof(u_int)) {
2014 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2019 im6o->im6o_multicast_loop = loop;
2022 case IPV6_JOIN_GROUP:
2024 * Add a multicast group membership.
2025 * Group must be a valid IP6 multicast address.
2027 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2031 mreq = mtod(m, struct ipv6_mreq *);
2032 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2034 * We use the unspecified address to specify to accept
2035 * all multicast addresses. Only super user is allowed
2038 if (suser_xxx(p->p_ucred, 0))
2043 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2049 * If the interface is specified, validate it.
2051 if (mreq->ipv6mr_interface < 0
2052 || if_index < mreq->ipv6mr_interface) {
2053 error = ENXIO; /* XXX EINVAL? */
2057 * If no interface was explicitly specified, choose an
2058 * appropriate one according to the given multicast address.
2060 if (mreq->ipv6mr_interface == 0) {
2062 * If the multicast address is in node-local scope,
2063 * the interface should be a loopback interface.
2064 * Otherwise, look up the routing table for the
2065 * address, and choose the outgoing interface.
2066 * XXX: is it a good approach?
2068 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2072 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2073 bzero(dst, sizeof(*dst));
2074 dst->sin6_len = sizeof(struct sockaddr_in6);
2075 dst->sin6_family = AF_INET6;
2076 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2077 rtalloc((struct route *)&ro);
2078 if (ro.ro_rt == NULL) {
2079 error = EADDRNOTAVAIL;
2082 ifp = ro.ro_rt->rt_ifp;
2086 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2089 * See if we found an interface, and confirm that it
2090 * supports multicast
2092 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2093 error = EADDRNOTAVAIL;
2097 * Put interface index into the multicast address,
2098 * if the address has link-local scope.
2100 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2101 mreq->ipv6mr_multiaddr.s6_addr16[1]
2102 = htons(mreq->ipv6mr_interface);
2105 * See if the membership already exists.
2107 for (imm = im6o->im6o_memberships.lh_first;
2108 imm != NULL; imm = imm->i6mm_chain.le_next)
2109 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2110 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2111 &mreq->ipv6mr_multiaddr))
2118 * Everything looks good; add a new record to the multicast
2119 * address list for the given interface.
2121 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2126 if ((imm->i6mm_maddr =
2127 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2128 free(imm, M_IPMADDR);
2131 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2134 case IPV6_LEAVE_GROUP:
2136 * Drop a multicast group membership.
2137 * Group must be a valid IP6 multicast address.
2139 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2143 mreq = mtod(m, struct ipv6_mreq *);
2144 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2145 if (suser_xxx(p->p_ucred, 0)) {
2149 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2154 * If an interface address was specified, get a pointer
2155 * to its ifnet structure.
2157 if (mreq->ipv6mr_interface < 0
2158 || if_index < mreq->ipv6mr_interface) {
2159 error = ENXIO; /* XXX EINVAL? */
2162 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2164 * Put interface index into the multicast address,
2165 * if the address has link-local scope.
2167 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2168 mreq->ipv6mr_multiaddr.s6_addr16[1]
2169 = htons(mreq->ipv6mr_interface);
2172 * Find the membership in the membership list.
2174 for (imm = im6o->im6o_memberships.lh_first;
2175 imm != NULL; imm = imm->i6mm_chain.le_next) {
2177 imm->i6mm_maddr->in6m_ifp == ifp) &&
2178 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2179 &mreq->ipv6mr_multiaddr))
2183 /* Unable to resolve interface */
2184 error = EADDRNOTAVAIL;
2188 * Give up the multicast address record to which the
2189 * membership points.
2191 LIST_REMOVE(imm, i6mm_chain);
2192 in6_delmulti(imm->i6mm_maddr);
2193 free(imm, M_IPMADDR);
2202 * If all options have default values, no need to keep the mbuf.
2204 if (im6o->im6o_multicast_ifp == NULL &&
2205 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2206 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2207 im6o->im6o_memberships.lh_first == NULL) {
2208 free(*im6op, M_IPMOPTS);
2216 * Return the IP6 multicast options in response to user getsockopt().
2219 ip6_getmoptions(optname, im6o, mp)
2221 struct ip6_moptions *im6o;
2224 u_int *hlim, *loop, *ifindex;
2226 *mp = m_get(M_WAIT, MT_HEADER); /* XXX */
2230 case IPV6_MULTICAST_IF:
2231 ifindex = mtod(*mp, u_int *);
2232 (*mp)->m_len = sizeof(u_int);
2233 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2236 *ifindex = im6o->im6o_multicast_ifp->if_index;
2239 case IPV6_MULTICAST_HOPS:
2240 hlim = mtod(*mp, u_int *);
2241 (*mp)->m_len = sizeof(u_int);
2243 *hlim = ip6_defmcasthlim;
2245 *hlim = im6o->im6o_multicast_hlim;
2248 case IPV6_MULTICAST_LOOP:
2249 loop = mtod(*mp, u_int *);
2250 (*mp)->m_len = sizeof(u_int);
2252 *loop = ip6_defmcasthlim;
2254 *loop = im6o->im6o_multicast_loop;
2263 * Discard the IP6 multicast options.
2266 ip6_freemoptions(im6o)
2267 struct ip6_moptions *im6o;
2269 struct in6_multi_mship *imm;
2274 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2275 LIST_REMOVE(imm, i6mm_chain);
2276 if (imm->i6mm_maddr)
2277 in6_delmulti(imm->i6mm_maddr);
2278 free(imm, M_IPMADDR);
2280 free(im6o, M_IPMOPTS);
2284 * Set IPv6 outgoing packet options based on advanced API.
2287 ip6_setpktoptions(control, opt, priv, needcopy)
2288 struct mbuf *control;
2289 struct ip6_pktopts *opt;
2292 struct cmsghdr *cm = 0;
2294 if (control == 0 || opt == 0)
2297 init_ip6pktopts(opt);
2300 * XXX: Currently, we assume all the optional information is stored
2303 if (control->m_next)
2306 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2307 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2308 cm = mtod(control, struct cmsghdr *);
2309 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2311 if (cm->cmsg_level != IPPROTO_IPV6)
2315 * XXX should check if RFC2292 API is mixed with 2292bis API
2317 switch (cm->cmsg_type) {
2319 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2322 /* XXX: Is it really WAITOK? */
2323 opt->ip6po_pktinfo =
2324 malloc(sizeof(struct in6_pktinfo),
2325 M_IP6OPT, M_WAITOK);
2326 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
2327 sizeof(struct in6_pktinfo));
2329 opt->ip6po_pktinfo =
2330 (struct in6_pktinfo *)CMSG_DATA(cm);
2331 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2332 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2333 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2334 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2336 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2337 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2342 * Check if the requested source address is indeed a
2343 * unicast address assigned to the node, and can be
2344 * used as the packet's source address.
2346 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2347 struct in6_ifaddr *ia6;
2348 struct sockaddr_in6 sin6;
2350 bzero(&sin6, sizeof(sin6));
2351 sin6.sin6_len = sizeof(sin6);
2352 sin6.sin6_family = AF_INET6;
2354 opt->ip6po_pktinfo->ipi6_addr;
2355 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2357 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2358 IN6_IFF_NOTREADY)) != 0)
2359 return(EADDRNOTAVAIL);
2364 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2367 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2368 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2376 if (cm->cmsg_len < sizeof(u_char) ||
2377 /* check if cmsg_len is large enough for sa_len */
2378 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2382 opt->ip6po_nexthop =
2383 malloc(*CMSG_DATA(cm),
2384 M_IP6OPT, M_WAITOK);
2385 bcopy(CMSG_DATA(cm),
2389 opt->ip6po_nexthop =
2390 (struct sockaddr *)CMSG_DATA(cm);
2395 struct ip6_hbh *hbh;
2398 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2400 hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2401 hbhlen = (hbh->ip6h_len + 1) << 3;
2402 if (cm->cmsg_len != CMSG_LEN(hbhlen))
2407 malloc(hbhlen, M_IP6OPT, M_WAITOK);
2408 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2410 opt->ip6po_hbh = hbh;
2416 struct ip6_dest *dest, **newdest;
2419 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2421 dest = (struct ip6_dest *)CMSG_DATA(cm);
2422 destlen = (dest->ip6d_len + 1) << 3;
2423 if (cm->cmsg_len != CMSG_LEN(destlen))
2427 * The old advacned API is ambiguous on this
2428 * point. Our approach is to determine the
2429 * position based according to the existence
2430 * of a routing header. Note, however, that
2431 * this depends on the order of the extension
2432 * headers in the ancillary data; the 1st part
2433 * of the destination options header must
2434 * appear before the routing header in the
2435 * ancillary data, too.
2436 * RFC2292bis solved the ambiguity by
2437 * introducing separate cmsg types.
2439 if (opt->ip6po_rthdr == NULL)
2440 newdest = &opt->ip6po_dest1;
2442 newdest = &opt->ip6po_dest2;
2445 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
2446 bcopy(dest, *newdest, destlen);
2455 struct ip6_rthdr *rth;
2458 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2460 rth = (struct ip6_rthdr *)CMSG_DATA(cm);
2461 rthlen = (rth->ip6r_len + 1) << 3;
2462 if (cm->cmsg_len != CMSG_LEN(rthlen))
2465 switch (rth->ip6r_type) {
2466 case IPV6_RTHDR_TYPE_0:
2467 /* must contain one addr */
2468 if (rth->ip6r_len == 0)
2470 /* length must be even */
2471 if (rth->ip6r_len % 2)
2473 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2477 return(EINVAL); /* not supported */
2481 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT,
2483 bcopy(rth, opt->ip6po_rthdr, rthlen);
2485 opt->ip6po_rthdr = rth;
2491 return(ENOPROTOOPT);
2499 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2500 * packet to the input queue of a specified interface. Note that this
2501 * calls the output routine of the loopback "driver", but with an interface
2502 * pointer that might NOT be &loif -- easier than replicating that code here.
2505 ip6_mloopback(ifp, m, dst)
2508 struct sockaddr_in6 *dst;
2511 struct ip6_hdr *ip6;
2513 copym = m_copy(m, 0, M_COPYALL);
2518 * Make sure to deep-copy IPv6 header portion in case the data
2519 * is in an mbuf cluster, so that we can safely override the IPv6
2520 * header portion later.
2522 if ((copym->m_flags & M_EXT) != 0 ||
2523 copym->m_len < sizeof(struct ip6_hdr)) {
2524 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2530 if (copym->m_len < sizeof(*ip6)) {
2536 ip6 = mtod(copym, struct ip6_hdr *);
2537 #ifndef SCOPEDROUTING
2539 * clear embedded scope identifiers if necessary.
2540 * in6_clearscope will touch the addresses only when necessary.
2542 in6_clearscope(&ip6->ip6_src);
2543 in6_clearscope(&ip6->ip6_dst);
2546 (void)if_simloop(ifp, copym, dst->sin6_family, NULL);
2550 * Chop IPv6 header off from the payload.
2553 ip6_splithdr(m, exthdrs)
2555 struct ip6_exthdrs *exthdrs;
2558 struct ip6_hdr *ip6;
2560 ip6 = mtod(m, struct ip6_hdr *);
2561 if (m->m_len > sizeof(*ip6)) {
2562 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2567 M_MOVE_PKTHDR(mh, m);
2568 MH_ALIGN(mh, sizeof(*ip6));
2569 m->m_len -= sizeof(*ip6);
2570 m->m_data += sizeof(*ip6);
2573 m->m_len = sizeof(*ip6);
2574 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2576 exthdrs->ip6e_ip6 = m;
2581 * Compute IPv6 extension header length.
2585 struct in6pcb *in6p;
2589 if (!in6p->in6p_outputopts)
2594 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2596 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2597 if (in6p->in6p_outputopts->ip6po_rthdr)
2598 /* dest1 is valid with rthdr only */
2599 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2600 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2601 len += elen(in6p->in6p_outputopts->ip6po_dest2);