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.9 2003/12/02 08:00:22 asmodai 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"
73 #include "opt_pfil_hooks.h"
75 #include <sys/param.h>
76 #include <sys/malloc.h>
78 #include <sys/errno.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
87 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet/icmp6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet6/nd6.h>
102 #include <netinet6/ipsec.h>
104 #include <netinet6/ipsec6.h>
106 #include <netproto/key/key.h>
112 #include <netipsec/key.h>
113 #endif /* FAST_IPSEC */
115 #include <net/ip6fw/ip6_fw.h>
117 #include <net/net_osdep.h>
119 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
122 struct mbuf *ip6e_ip6;
123 struct mbuf *ip6e_hbh;
124 struct mbuf *ip6e_dest1;
125 struct mbuf *ip6e_rthdr;
126 struct mbuf *ip6e_dest2;
129 static int ip6_pcbopts (struct ip6_pktopts **, struct mbuf *,
130 struct socket *, struct sockopt *sopt);
131 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
132 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
133 static int ip6_copyexthdr (struct mbuf **, caddr_t, int);
134 static int ip6_insertfraghdr (struct mbuf *, struct mbuf *, int,
136 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
137 static int ip6_splithdr (struct mbuf *, struct ip6_exthdrs *);
140 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
141 * header (with pri, len, nxt, hlim, src, dst).
142 * This function may modify ver and hlim only.
143 * The mbuf chain containing the packet will be freed.
144 * The mbuf opt, if present, will not be freed.
146 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
147 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
148 * which is rt_rmx.rmx_mtu.
151 ip6_output(m0, opt, ro, flags, im6o, ifpp, inp)
153 struct ip6_pktopts *opt;
154 struct route_in6 *ro;
156 struct ip6_moptions *im6o;
157 struct ifnet **ifpp; /* XXX: just for statistics */
160 struct ip6_hdr *ip6, *mhip6;
161 struct ifnet *ifp, *origifp;
163 int hlen, tlen, len, off;
164 struct route_in6 ip6route;
165 struct sockaddr_in6 *dst;
167 struct in6_ifaddr *ia = NULL;
169 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
170 struct ip6_exthdrs exthdrs;
171 struct in6_addr finaldst;
172 struct route_in6 *ro_pmtu = NULL;
176 int needipsectun = 0;
177 struct secpolicy *sp = NULL;
178 struct socket *so = inp ? inp->inp_socket : NULL;
180 ip6 = mtod(m, struct ip6_hdr *);
183 int needipsectun = 0;
184 struct secpolicy *sp = NULL;
186 ip6 = mtod(m, struct ip6_hdr *);
187 #endif /* FAST_IPSEC */
189 #define MAKE_EXTHDR(hp, mp) \
192 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
193 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
194 ((eh)->ip6e_len + 1) << 3); \
200 bzero(&exthdrs, sizeof(exthdrs));
203 /* Hop-by-Hop options header */
204 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
205 /* Destination options header(1st part) */
206 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
208 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
209 /* Destination options header(2nd part) */
210 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
214 /* get a security policy for this packet */
216 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
218 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
221 ipsec6stat.out_inval++;
228 switch (sp->policy) {
229 case IPSEC_POLICY_DISCARD:
231 * This packet is just discarded.
233 ipsec6stat.out_polvio++;
236 case IPSEC_POLICY_BYPASS:
237 case IPSEC_POLICY_NONE:
238 /* no need to do IPsec. */
242 case IPSEC_POLICY_IPSEC:
243 if (sp->req == NULL) {
244 /* acquire a policy */
245 error = key_spdacquire(sp);
251 case IPSEC_POLICY_ENTRUST:
253 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
257 /* get a security policy for this packet */
259 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
261 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
264 newipsecstat.ips_out_inval++;
271 switch (sp->policy) {
272 case IPSEC_POLICY_DISCARD:
274 * This packet is just discarded.
276 newipsecstat.ips_out_polvio++;
279 case IPSEC_POLICY_BYPASS:
280 case IPSEC_POLICY_NONE:
281 /* no need to do IPsec. */
285 case IPSEC_POLICY_IPSEC:
286 if (sp->req == NULL) {
287 /* acquire a policy */
288 error = key_spdacquire(sp);
294 case IPSEC_POLICY_ENTRUST:
296 printf("ip6_output: Invalid policy found. %d\n", sp->policy);
298 #endif /* FAST_IPSEC */
301 * Calculate the total length of the extension header chain.
302 * Keep the length of the unfragmentable part for fragmentation.
305 if (exthdrs.ip6e_hbh) optlen += exthdrs.ip6e_hbh->m_len;
306 if (exthdrs.ip6e_dest1) optlen += exthdrs.ip6e_dest1->m_len;
307 if (exthdrs.ip6e_rthdr) optlen += exthdrs.ip6e_rthdr->m_len;
308 unfragpartlen = optlen + sizeof(struct ip6_hdr);
309 /* NOTE: we don't add AH/ESP length here. do that later. */
310 if (exthdrs.ip6e_dest2) optlen += exthdrs.ip6e_dest2->m_len;
313 * If we need IPsec, or there is at least one extension header,
314 * separate IP6 header from the payload.
316 if ((needipsec || optlen) && !hdrsplit) {
317 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
321 m = exthdrs.ip6e_ip6;
326 ip6 = mtod(m, struct ip6_hdr *);
328 /* adjust mbuf packet header length */
329 m->m_pkthdr.len += optlen;
330 plen = m->m_pkthdr.len - sizeof(*ip6);
332 /* If this is a jumbo payload, insert a jumbo payload option. */
333 if (plen > IPV6_MAXPACKET) {
335 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
339 m = exthdrs.ip6e_ip6;
343 ip6 = mtod(m, struct ip6_hdr *);
344 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
348 ip6->ip6_plen = htons(plen);
351 * Concatenate headers and fill in next header fields.
352 * Here we have, on "m"
354 * and we insert headers accordingly. Finally, we should be getting:
355 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
357 * during the header composing process, "m" points to IPv6 header.
358 * "mprev" points to an extension header prior to esp.
361 u_char *nexthdrp = &ip6->ip6_nxt;
362 struct mbuf *mprev = m;
365 * we treat dest2 specially. this makes IPsec processing
366 * much easier. the goal here is to make mprev point the
367 * mbuf prior to dest2.
369 * result: IPv6 dest2 payload
370 * m and mprev will point to IPv6 header.
372 if (exthdrs.ip6e_dest2) {
374 panic("assumption failed: hdr not split");
375 exthdrs.ip6e_dest2->m_next = m->m_next;
376 m->m_next = exthdrs.ip6e_dest2;
377 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
378 ip6->ip6_nxt = IPPROTO_DSTOPTS;
381 #define MAKE_CHAIN(m, mp, p, i)\
385 panic("assumption failed: hdr not split"); \
386 *mtod((m), u_char *) = *(p);\
388 p = mtod((m), u_char *);\
389 (m)->m_next = (mp)->m_next;\
395 * result: IPv6 hbh dest1 rthdr dest2 payload
396 * m will point to IPv6 header. mprev will point to the
397 * extension header prior to dest2 (rthdr in the above case).
399 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev,
400 nexthdrp, IPPROTO_HOPOPTS);
401 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev,
402 nexthdrp, IPPROTO_DSTOPTS);
403 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev,
404 nexthdrp, IPPROTO_ROUTING);
406 #if defined(IPSEC) || defined(FAST_IPSEC)
411 * pointers after IPsec headers are not valid any more.
412 * other pointers need a great care too.
413 * (IPsec routines should not mangle mbufs prior to AH/ESP)
415 exthdrs.ip6e_dest2 = NULL;
418 struct ip6_rthdr *rh = NULL;
420 struct ipsec_output_state state;
422 if (exthdrs.ip6e_rthdr) {
423 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
424 segleft_org = rh->ip6r_segleft;
425 rh->ip6r_segleft = 0;
428 bzero(&state, sizeof(state));
430 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
434 /* mbuf is already reclaimed in ipsec6_output_trans. */
444 printf("ip6_output (ipsec): error code %d\n", error);
447 /* don't show these error codes to the user */
453 if (exthdrs.ip6e_rthdr) {
454 /* ah6_output doesn't modify mbuf chain */
455 rh->ip6r_segleft = segleft_org;
463 * If there is a routing header, replace destination address field
464 * with the first hop of the routing header.
466 if (exthdrs.ip6e_rthdr) {
467 struct ip6_rthdr *rh =
468 (struct ip6_rthdr *)(mtod(exthdrs.ip6e_rthdr,
469 struct ip6_rthdr *));
470 struct ip6_rthdr0 *rh0;
472 finaldst = ip6->ip6_dst;
473 switch (rh->ip6r_type) {
474 case IPV6_RTHDR_TYPE_0:
475 rh0 = (struct ip6_rthdr0 *)rh;
476 ip6->ip6_dst = rh0->ip6r0_addr[0];
477 bcopy((caddr_t)&rh0->ip6r0_addr[1],
478 (caddr_t)&rh0->ip6r0_addr[0],
479 sizeof(struct in6_addr)*(rh0->ip6r0_segleft - 1)
481 rh0->ip6r0_addr[rh0->ip6r0_segleft - 1] = finaldst;
483 default: /* is it possible? */
489 /* Source address validation */
490 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
491 (flags & IPV6_DADOUTPUT) == 0) {
493 ip6stat.ip6s_badscope++;
496 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
498 ip6stat.ip6s_badscope++;
502 ip6stat.ip6s_localout++;
509 bzero((caddr_t)ro, sizeof(*ro));
512 if (opt && opt->ip6po_rthdr)
513 ro = &opt->ip6po_route;
514 dst = (struct sockaddr_in6 *)&ro->ro_dst;
516 * If there is a cached route,
517 * check that it is to the same destination
518 * and is still up. If not, free it and try again.
520 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
521 dst->sin6_family != AF_INET6 ||
522 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
524 ro->ro_rt = (struct rtentry *)0;
526 if (ro->ro_rt == 0) {
527 bzero(dst, sizeof(*dst));
528 dst->sin6_family = AF_INET6;
529 dst->sin6_len = sizeof(struct sockaddr_in6);
530 dst->sin6_addr = ip6->ip6_dst;
532 /* XXX: sin6_scope_id should already be fixed at this point */
533 if (IN6_IS_SCOPE_LINKLOCAL(&dst->sin6_addr))
534 dst->sin6_scope_id = ntohs(dst->sin6_addr.s6_addr16[1]);
537 #if defined(IPSEC) || defined(FAST_IPSEC)
538 if (needipsec && needipsectun) {
539 struct ipsec_output_state state;
542 * All the extension headers will become inaccessible
543 * (since they can be encrypted).
544 * Don't panic, we need no more updates to extension headers
545 * on inner IPv6 packet (since they are now encapsulated).
547 * IPv6 [ESP|AH] IPv6 [extension headers] payload
549 bzero(&exthdrs, sizeof(exthdrs));
550 exthdrs.ip6e_ip6 = m;
552 bzero(&state, sizeof(state));
554 state.ro = (struct route *)ro;
555 state.dst = (struct sockaddr *)dst;
557 error = ipsec6_output_tunnel(&state, sp, flags);
560 ro = (struct route_in6 *)state.ro;
561 dst = (struct sockaddr_in6 *)state.dst;
563 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
574 printf("ip6_output (ipsec): error code %d\n", error);
577 /* don't show these error codes to the user */
584 exthdrs.ip6e_ip6 = m;
588 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
591 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
592 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
594 * interface selection comes here
595 * if an interface is specified from an upper layer,
598 if (ro->ro_rt == 0) {
600 * non-bsdi always clone routes, if parent is
603 rtalloc((struct route *)ro);
605 if (ro->ro_rt == 0) {
606 ip6stat.ip6s_noroute++;
607 error = EHOSTUNREACH;
608 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
611 ia = ifatoia6(ro->ro_rt->rt_ifa);
612 ifp = ro->ro_rt->rt_ifp;
614 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
615 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
616 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
618 in6_ifstat_inc(ifp, ifs6_out_request);
621 * Check if the outgoing interface conflicts with
622 * the interface specified by ifi6_ifindex (if specified).
623 * Note that loopback interface is always okay.
624 * (this may happen when we are sending a packet to one of
625 * our own addresses.)
627 if (opt && opt->ip6po_pktinfo
628 && opt->ip6po_pktinfo->ipi6_ifindex) {
629 if (!(ifp->if_flags & IFF_LOOPBACK)
630 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
631 ip6stat.ip6s_noroute++;
632 in6_ifstat_inc(ifp, ifs6_out_discard);
633 error = EHOSTUNREACH;
638 if (opt && opt->ip6po_hlim != -1)
639 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
642 struct in6_multi *in6m;
644 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
647 * See if the caller provided any multicast options
651 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
652 if (im6o->im6o_multicast_ifp != NULL)
653 ifp = im6o->im6o_multicast_ifp;
655 ip6->ip6_hlim = ip6_defmcasthlim;
658 * See if the caller provided the outgoing interface
659 * as an ancillary data.
660 * Boundary check for ifindex is assumed to be already done.
662 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
663 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
666 * If the destination is a node-local scope multicast,
667 * the packet should be loop-backed only.
669 if (IN6_IS_ADDR_MC_NODELOCAL(&ip6->ip6_dst)) {
671 * If the outgoing interface is already specified,
672 * it should be a loopback interface.
674 if (ifp && (ifp->if_flags & IFF_LOOPBACK) == 0) {
675 ip6stat.ip6s_badscope++;
676 error = ENETUNREACH; /* XXX: better error? */
677 /* XXX correct ifp? */
678 in6_ifstat_inc(ifp, ifs6_out_discard);
685 if (opt && opt->ip6po_hlim != -1)
686 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
689 * If caller did not provide an interface lookup a
690 * default in the routing table. This is either a
691 * default for the speicfied group (i.e. a host
692 * route), or a multicast default (a route for the
696 if (ro->ro_rt == 0) {
697 ro->ro_rt = rtalloc1((struct sockaddr *)
698 &ro->ro_dst, 0, 0UL);
700 if (ro->ro_rt == 0) {
701 ip6stat.ip6s_noroute++;
702 error = EHOSTUNREACH;
703 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
706 ia = ifatoia6(ro->ro_rt->rt_ifa);
707 ifp = ro->ro_rt->rt_ifp;
711 if ((flags & IPV6_FORWARDING) == 0)
712 in6_ifstat_inc(ifp, ifs6_out_request);
713 in6_ifstat_inc(ifp, ifs6_out_mcast);
716 * Confirm that the outgoing interface supports multicast.
718 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
719 ip6stat.ip6s_noroute++;
720 in6_ifstat_inc(ifp, ifs6_out_discard);
724 IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
726 (im6o == NULL || im6o->im6o_multicast_loop)) {
728 * If we belong to the destination multicast group
729 * on the outgoing interface, and the caller did not
730 * forbid loopback, loop back a copy.
732 ip6_mloopback(ifp, m, dst);
735 * If we are acting as a multicast router, perform
736 * multicast forwarding as if the packet had just
737 * arrived on the interface to which we are about
738 * to send. The multicast forwarding function
739 * recursively calls this function, using the
740 * IPV6_FORWARDING flag to prevent infinite recursion.
742 * Multicasts that are looped back by ip6_mloopback(),
743 * above, will be forwarded by the ip6_input() routine,
746 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
747 if (ip6_mforward(ip6, ifp, m) != 0) {
754 * Multicasts with a hoplimit of zero may be looped back,
755 * above, but must not be transmitted on a network.
756 * Also, multicasts addressed to the loopback interface
757 * are not sent -- the above call to ip6_mloopback() will
758 * loop back a copy if this host actually belongs to the
759 * destination group on the loopback interface.
761 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
768 * Fill the outgoing inteface to tell the upper layer
769 * to increment per-interface statistics.
775 * Determine path MTU.
778 /* The first hop and the final destination may differ. */
779 struct sockaddr_in6 *sin6_fin =
780 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
781 if (ro_pmtu->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
782 !IN6_ARE_ADDR_EQUAL(&sin6_fin->sin6_addr,
784 RTFREE(ro_pmtu->ro_rt);
785 ro_pmtu->ro_rt = (struct rtentry *)0;
787 if (ro_pmtu->ro_rt == 0) {
788 bzero(sin6_fin, sizeof(*sin6_fin));
789 sin6_fin->sin6_family = AF_INET6;
790 sin6_fin->sin6_len = sizeof(struct sockaddr_in6);
791 sin6_fin->sin6_addr = finaldst;
793 rtalloc((struct route *)ro_pmtu);
796 if (ro_pmtu->ro_rt != NULL) {
797 u_int32_t ifmtu = nd_ifinfo[ifp->if_index].linkmtu;
799 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
800 if (mtu > ifmtu || mtu == 0) {
802 * The MTU on the route is larger than the MTU on
803 * the interface! This shouldn't happen, unless the
804 * MTU of the interface has been changed after the
805 * interface was brought up. Change the MTU in the
806 * route to match the interface MTU (as long as the
807 * field isn't locked).
809 * if MTU on the route is 0, we need to fix the MTU.
810 * this case happens with path MTU discovery timeouts.
813 if ((ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0)
814 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
817 mtu = nd_ifinfo[ifp->if_index].linkmtu;
821 * advanced API (IPV6_USE_MIN_MTU) overrides mtu setting
823 if ((flags & IPV6_MINMTU) != 0 && mtu > IPV6_MMTU)
826 /* Fake scoped addresses */
827 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
829 * If source or destination address is a scoped address, and
830 * the packet is going to be sent to a loopback interface,
831 * we should keep the original interface.
835 * XXX: this is a very experimental and temporary solution.
836 * We eventually have sockaddr_in6 and use the sin6_scope_id
837 * field of the structure here.
838 * We rely on the consistency between two scope zone ids
839 * of source and destination, which should already be assured.
840 * Larger scopes than link will be supported in the future.
843 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
844 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
845 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
846 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
848 * XXX: origifp can be NULL even in those two cases above.
849 * For example, if we remove the (only) link-local address
850 * from the loopback interface, and try to send a link-local
851 * address without link-id information. Then the source
852 * address is ::1, and the destination address is the
853 * link-local address with its s6_addr16[1] being zero.
854 * What is worse, if the packet goes to the loopback interface
855 * by a default rejected route, the null pointer would be
856 * passed to looutput, and the kernel would hang.
857 * The following last resort would prevent such disaster.
864 #ifndef SCOPEDROUTING
866 * clear embedded scope identifiers if necessary.
867 * in6_clearscope will touch the addresses only when necessary.
869 in6_clearscope(&ip6->ip6_src);
870 in6_clearscope(&ip6->ip6_dst);
874 * Check with the firewall...
876 if (ip6_fw_enable && ip6_fw_chk_ptr) {
878 m->m_pkthdr.rcvif = NULL; /* XXX */
879 /* If ipfw says divert, we have to just drop packet */
880 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
891 * If the outgoing packet contains a hop-by-hop options header,
892 * it must be examined and processed even by the source node.
893 * (RFC 2460, section 4.)
895 if (exthdrs.ip6e_hbh) {
896 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
897 u_int32_t dummy1; /* XXX unused */
898 u_int32_t dummy2; /* XXX unused */
901 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
902 panic("ip6e_hbh is not continuous");
905 * XXX: if we have to send an ICMPv6 error to the sender,
906 * we need the M_LOOP flag since icmp6_error() expects
907 * the IPv6 and the hop-by-hop options header are
908 * continuous unless the flag is set.
910 m->m_flags |= M_LOOP;
911 m->m_pkthdr.rcvif = ifp;
912 if (ip6_process_hopopts(m,
913 (u_int8_t *)(hbh + 1),
914 ((hbh->ip6h_len + 1) << 3) -
915 sizeof(struct ip6_hbh),
916 &dummy1, &dummy2) < 0) {
917 /* m was already freed at this point */
918 error = EINVAL;/* better error? */
921 m->m_flags &= ~M_LOOP; /* XXX */
922 m->m_pkthdr.rcvif = NULL;
927 * Run through list of hooks for output packets.
929 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
930 if (error != 0 || m == NULL)
932 ip6 = mtod(m, struct ip6_hdr *);
933 #endif /* PFIL_HOOKS */
935 * Send the packet to the outgoing interface.
936 * If necessary, do IPv6 fragmentation before sending.
938 tlen = m->m_pkthdr.len;
942 * On any link that cannot convey a 1280-octet packet in one piece,
943 * link-specific fragmentation and reassembly must be provided at
944 * a layer below IPv6. [RFC 2460, sec.5]
945 * Thus if the interface has ability of link-level fragmentation,
946 * we can just send the packet even if the packet size is
947 * larger than the link's MTU.
948 * XXX: IFF_FRAGMENTABLE (or such) flag has not been defined yet...
951 || ifp->if_flags & IFF_FRAGMENTABLE
955 /* Record statistics for this interface address. */
956 if (ia && !(flags & IPV6_FORWARDING)) {
957 ia->ia_ifa.if_opackets++;
958 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
961 /* clean ipsec history once it goes out of the node */
964 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
966 } else if (mtu < IPV6_MMTU) {
968 * note that path MTU is never less than IPV6_MMTU
972 in6_ifstat_inc(ifp, ifs6_out_fragfail);
974 } else if (ip6->ip6_plen == 0) { /* jumbo payload cannot be fragmented */
976 in6_ifstat_inc(ifp, ifs6_out_fragfail);
979 struct mbuf **mnext, *m_frgpart;
980 struct ip6_frag *ip6f;
981 u_int32_t id = htonl(ip6_id++);
985 * Too large for the destination or interface;
986 * fragment if possible.
987 * Must be able to put at least 8 bytes per fragment.
989 hlen = unfragpartlen;
990 if (mtu > IPV6_MAXPACKET)
991 mtu = IPV6_MAXPACKET;
993 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
996 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1000 mnext = &m->m_nextpkt;
1003 * Change the next header field of the last header in the
1004 * unfragmentable part.
1006 if (exthdrs.ip6e_rthdr) {
1007 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1008 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1009 } else if (exthdrs.ip6e_dest1) {
1010 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1011 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1012 } else if (exthdrs.ip6e_hbh) {
1013 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1014 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1016 nextproto = ip6->ip6_nxt;
1017 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1021 * Loop through length of segment after first fragment,
1022 * make new header and copy data of each part and link onto
1026 for (off = hlen; off < tlen; off += len) {
1027 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1030 ip6stat.ip6s_odropped++;
1033 m->m_pkthdr.rcvif = NULL;
1034 m->m_flags = m0->m_flags & M_COPYFLAGS;
1036 mnext = &m->m_nextpkt;
1037 m->m_data += max_linkhdr;
1038 mhip6 = mtod(m, struct ip6_hdr *);
1040 m->m_len = sizeof(*mhip6);
1041 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1043 ip6stat.ip6s_odropped++;
1046 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1047 if (off + len >= tlen)
1050 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1051 mhip6->ip6_plen = htons((u_short)(len + hlen +
1053 sizeof(struct ip6_hdr)));
1054 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
1056 ip6stat.ip6s_odropped++;
1059 m_cat(m, m_frgpart);
1060 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1061 m->m_pkthdr.rcvif = (struct ifnet *)0;
1062 ip6f->ip6f_reserved = 0;
1063 ip6f->ip6f_ident = id;
1064 ip6f->ip6f_nxt = nextproto;
1065 ip6stat.ip6s_ofragments++;
1066 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1069 in6_ifstat_inc(ifp, ifs6_out_fragok);
1073 * Remove leading garbages.
1079 for (m0 = m; m; m = m0) {
1083 /* Record statistics for this interface address. */
1085 ia->ia_ifa.if_opackets++;
1086 ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1089 /* clean ipsec history once it goes out of the node */
1092 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1098 ip6stat.ip6s_fragmented++;
1101 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1103 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1104 RTFREE(ro_pmtu->ro_rt);
1114 #endif /* FAST_IPSEC */
1119 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1120 m_freem(exthdrs.ip6e_dest1);
1121 m_freem(exthdrs.ip6e_rthdr);
1122 m_freem(exthdrs.ip6e_dest2);
1130 ip6_copyexthdr(mp, hdr, hlen)
1137 if (hlen > MCLBYTES)
1138 return(ENOBUFS); /* XXX */
1140 MGET(m, M_DONTWAIT, MT_DATA);
1145 MCLGET(m, M_DONTWAIT);
1146 if ((m->m_flags & M_EXT) == 0) {
1153 bcopy(hdr, mtod(m, caddr_t), hlen);
1160 * Insert jumbo payload option.
1163 ip6_insert_jumboopt(exthdrs, plen)
1164 struct ip6_exthdrs *exthdrs;
1171 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1174 * If there is no hop-by-hop options header, allocate new one.
1175 * If there is one but it doesn't have enough space to store the
1176 * jumbo payload option, allocate a cluster to store the whole options.
1177 * Otherwise, use it to store the options.
1179 if (exthdrs->ip6e_hbh == 0) {
1180 MGET(mopt, M_DONTWAIT, MT_DATA);
1183 mopt->m_len = JUMBOOPTLEN;
1184 optbuf = mtod(mopt, u_char *);
1185 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1186 exthdrs->ip6e_hbh = mopt;
1188 struct ip6_hbh *hbh;
1190 mopt = exthdrs->ip6e_hbh;
1191 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1194 * - exthdrs->ip6e_hbh is not referenced from places
1195 * other than exthdrs.
1196 * - exthdrs->ip6e_hbh is not an mbuf chain.
1198 int oldoptlen = mopt->m_len;
1202 * XXX: give up if the whole (new) hbh header does
1203 * not fit even in an mbuf cluster.
1205 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1209 * As a consequence, we must always prepare a cluster
1212 MGET(n, M_DONTWAIT, MT_DATA);
1214 MCLGET(n, M_DONTWAIT);
1215 if ((n->m_flags & M_EXT) == 0) {
1222 n->m_len = oldoptlen + JUMBOOPTLEN;
1223 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1225 optbuf = mtod(n, caddr_t) + oldoptlen;
1227 mopt = exthdrs->ip6e_hbh = n;
1229 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1230 mopt->m_len += JUMBOOPTLEN;
1232 optbuf[0] = IP6OPT_PADN;
1236 * Adjust the header length according to the pad and
1237 * the jumbo payload option.
1239 hbh = mtod(mopt, struct ip6_hbh *);
1240 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1243 /* fill in the option. */
1244 optbuf[2] = IP6OPT_JUMBO;
1246 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1247 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1249 /* finally, adjust the packet header length */
1250 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1257 * Insert fragment header and copy unfragmentable header portions.
1260 ip6_insertfraghdr(m0, m, hlen, frghdrp)
1261 struct mbuf *m0, *m;
1263 struct ip6_frag **frghdrp;
1265 struct mbuf *n, *mlast;
1267 if (hlen > sizeof(struct ip6_hdr)) {
1268 n = m_copym(m0, sizeof(struct ip6_hdr),
1269 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1276 /* Search for the last mbuf of unfragmentable part. */
1277 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1280 if ((mlast->m_flags & M_EXT) == 0 &&
1281 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1282 /* use the trailing space of the last mbuf for the fragment hdr */
1284 (struct ip6_frag *)(mtod(mlast, caddr_t) + mlast->m_len);
1285 mlast->m_len += sizeof(struct ip6_frag);
1286 m->m_pkthdr.len += sizeof(struct ip6_frag);
1288 /* allocate a new mbuf for the fragment header */
1291 MGET(mfrg, M_DONTWAIT, MT_DATA);
1294 mfrg->m_len = sizeof(struct ip6_frag);
1295 *frghdrp = mtod(mfrg, struct ip6_frag *);
1296 mlast->m_next = mfrg;
1303 * IP6 socket option processing.
1306 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1309 struct inpcb *in6p = sotoinpcb(so);
1311 int level, op, optname;
1316 level = sopt->sopt_level;
1317 op = sopt->sopt_dir;
1318 optname = sopt->sopt_name;
1319 optlen = sopt->sopt_valsize;
1322 panic("ip6_ctloutput: arg soopt is NULL");
1328 privileged = (td == NULL || suser(td)) ? 0 : 1;
1330 if (level == IPPROTO_IPV6) {
1335 case IPV6_PKTOPTIONS:
1339 error = soopt_getm(sopt, &m); /* XXX */
1342 error = soopt_mcopyin(sopt, m); /* XXX */
1345 error = ip6_pcbopts(&in6p->in6p_outputopts,
1347 m_freem(m); /* XXX */
1352 * Use of some Hop-by-Hop options or some
1353 * Destination options, might require special
1354 * privilege. That is, normal applications
1355 * (without special privilege) might be forbidden
1356 * from setting certain options in outgoing packets,
1357 * and might never see certain options in received
1358 * packets. [RFC 2292 Section 6]
1359 * KAME specific note:
1360 * KAME prevents non-privileged users from sending or
1361 * receiving ANY hbh/dst options in order to avoid
1362 * overhead of parsing options in the kernel.
1364 case IPV6_UNICAST_HOPS:
1369 if (optlen != sizeof(int)) {
1373 error = sooptcopyin(sopt, &optval,
1374 sizeof optval, sizeof optval);
1379 case IPV6_UNICAST_HOPS:
1380 if (optval < -1 || optval >= 256)
1383 /* -1 = kernel default */
1384 in6p->in6p_hops = optval;
1386 if ((in6p->in6p_vflag &
1388 in6p->inp_ip_ttl = optval;
1391 #define OPTSET(bit) \
1394 in6p->in6p_flags |= (bit); \
1396 in6p->in6p_flags &= ~(bit); \
1398 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1401 in6p->in6p_cksum = optval;
1410 * make setsockopt(IPV6_V6ONLY)
1411 * available only prior to bind(2).
1412 * see ipng mailing list, Jun 22 2001.
1414 if (in6p->in6p_lport ||
1415 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1420 OPTSET(IN6P_IPV6_V6ONLY);
1422 in6p->in6p_vflag &= ~INP_IPV4;
1424 in6p->in6p_vflag |= INP_IPV4;
1435 if (optlen != sizeof(int)) {
1439 error = sooptcopyin(sopt, &optval,
1440 sizeof optval, sizeof optval);
1445 OPTSET(IN6P_PKTINFO);
1448 OPTSET(IN6P_HOPLIMIT);
1452 * Check super-user privilege.
1453 * See comments for IPV6_RECVHOPOPTS.
1457 OPTSET(IN6P_HOPOPTS);
1462 OPTSET(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1471 case IPV6_MULTICAST_IF:
1472 case IPV6_MULTICAST_HOPS:
1473 case IPV6_MULTICAST_LOOP:
1474 case IPV6_JOIN_GROUP:
1475 case IPV6_LEAVE_GROUP:
1478 if (sopt->sopt_valsize > MLEN) {
1483 MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_HEADER);
1488 m->m_len = sopt->sopt_valsize;
1489 error = sooptcopyin(sopt, mtod(m, char *),
1490 m->m_len, m->m_len);
1491 error = ip6_setmoptions(sopt->sopt_name,
1492 &in6p->in6p_moptions,
1498 case IPV6_PORTRANGE:
1499 error = sooptcopyin(sopt, &optval,
1500 sizeof optval, sizeof optval);
1505 case IPV6_PORTRANGE_DEFAULT:
1506 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1507 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1510 case IPV6_PORTRANGE_HIGH:
1511 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1512 in6p->in6p_flags |= IN6P_HIGHPORT;
1515 case IPV6_PORTRANGE_LOW:
1516 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1517 in6p->in6p_flags |= IN6P_LOWPORT;
1526 #if defined(IPSEC) || defined(FAST_IPSEC)
1527 case IPV6_IPSEC_POLICY:
1533 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1535 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1538 req = mtod(m, caddr_t);
1541 error = ipsec6_set_policy(in6p, optname, req,
1546 #endif /* KAME IPSEC */
1554 struct mbuf **mp = &m;
1556 if (ip6_fw_ctl_ptr == NULL)
1559 if ((error = soopt_getm(sopt, &m)) != 0)
1562 if ((error = soopt_mcopyin(sopt, m)) != 0)
1564 error = (*ip6_fw_ctl_ptr)(optname, mp);
1570 error = ENOPROTOOPT;
1578 case IPV6_PKTOPTIONS:
1579 if (in6p->in6p_options) {
1581 m = m_copym(in6p->in6p_options,
1582 0, M_COPYALL, M_WAIT);
1583 error = soopt_mcopyout(sopt, m);
1587 sopt->sopt_valsize = 0;
1590 case IPV6_UNICAST_HOPS:
1595 case IPV6_PORTRANGE:
1598 case IPV6_UNICAST_HOPS:
1599 optval = in6p->in6p_hops;
1603 optval = in6p->in6p_cksum;
1607 optval = OPTBIT(IN6P_FAITH);
1611 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1614 case IPV6_PORTRANGE:
1617 flags = in6p->in6p_flags;
1618 if (flags & IN6P_HIGHPORT)
1619 optval = IPV6_PORTRANGE_HIGH;
1620 else if (flags & IN6P_LOWPORT)
1621 optval = IPV6_PORTRANGE_LOW;
1627 error = sooptcopyout(sopt, &optval,
1636 if (optname == IPV6_HOPOPTS ||
1637 optname == IPV6_DSTOPTS ||
1642 optval = OPTBIT(IN6P_PKTINFO);
1645 optval = OPTBIT(IN6P_HOPLIMIT);
1650 optval = OPTBIT(IN6P_HOPOPTS);
1653 optval = OPTBIT(IN6P_RTHDR);
1658 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1661 error = sooptcopyout(sopt, &optval,
1665 case IPV6_MULTICAST_IF:
1666 case IPV6_MULTICAST_HOPS:
1667 case IPV6_MULTICAST_LOOP:
1668 case IPV6_JOIN_GROUP:
1669 case IPV6_LEAVE_GROUP:
1672 error = ip6_getmoptions(sopt->sopt_name,
1673 in6p->in6p_moptions, &m);
1675 error = sooptcopyout(sopt,
1676 mtod(m, char *), m->m_len);
1681 #if defined(IPSEC) || defined(FAST_IPSEC)
1682 case IPV6_IPSEC_POLICY:
1686 struct mbuf *m = NULL;
1687 struct mbuf **mp = &m;
1689 error = soopt_getm(sopt, &m); /* XXX */
1692 error = soopt_mcopyin(sopt, m); /* XXX */
1696 req = mtod(m, caddr_t);
1699 error = ipsec6_get_policy(in6p, req, len, mp);
1701 error = soopt_mcopyout(sopt, m); /*XXX*/
1702 if (error == 0 && m)
1706 #endif /* KAME IPSEC */
1711 struct mbuf **mp = &m;
1713 if (ip6_fw_ctl_ptr == NULL)
1717 error = (*ip6_fw_ctl_ptr)(optname, mp);
1719 error = soopt_mcopyout(sopt, m); /* XXX */
1720 if (error == 0 && m)
1726 error = ENOPROTOOPT;
1738 * Set up IP6 options in pcb for insertion in output packets or
1739 * specifying behavior of outgoing packets.
1742 ip6_pcbopts(pktopt, m, so, sopt)
1743 struct ip6_pktopts **pktopt;
1746 struct sockopt *sopt;
1748 struct ip6_pktopts *opt = *pktopt;
1750 struct thread *td = sopt->sopt_td;
1753 /* turn off any old options. */
1756 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1757 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1758 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1759 printf("ip6_pcbopts: all specified options are cleared.\n");
1761 ip6_clearpktopts(opt, 1, -1);
1763 opt = malloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1766 if (!m || m->m_len == 0) {
1768 * Only turning off any previous options, regardless of
1769 * whether the opt is just created or given.
1771 free(opt, M_IP6OPT);
1775 /* set options specified by user. */
1778 if ((error = ip6_setpktoptions(m, opt, priv, 1)) != 0) {
1779 ip6_clearpktopts(opt, 1, -1); /* XXX: discard all options */
1780 free(opt, M_IP6OPT);
1788 * initialize ip6_pktopts. beware that there are non-zero default values in
1792 init_ip6pktopts(opt)
1793 struct ip6_pktopts *opt;
1796 bzero(opt, sizeof(*opt));
1797 opt->ip6po_hlim = -1; /* -1 means default hop limit */
1801 ip6_clearpktopts(pktopt, needfree, optname)
1802 struct ip6_pktopts *pktopt;
1803 int needfree, optname;
1808 if (optname == -1) {
1809 if (needfree && pktopt->ip6po_pktinfo)
1810 free(pktopt->ip6po_pktinfo, M_IP6OPT);
1811 pktopt->ip6po_pktinfo = NULL;
1814 pktopt->ip6po_hlim = -1;
1815 if (optname == -1) {
1816 if (needfree && pktopt->ip6po_nexthop)
1817 free(pktopt->ip6po_nexthop, M_IP6OPT);
1818 pktopt->ip6po_nexthop = NULL;
1820 if (optname == -1) {
1821 if (needfree && pktopt->ip6po_hbh)
1822 free(pktopt->ip6po_hbh, M_IP6OPT);
1823 pktopt->ip6po_hbh = NULL;
1825 if (optname == -1) {
1826 if (needfree && pktopt->ip6po_dest1)
1827 free(pktopt->ip6po_dest1, M_IP6OPT);
1828 pktopt->ip6po_dest1 = NULL;
1830 if (optname == -1) {
1831 if (needfree && pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
1832 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1833 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
1834 if (pktopt->ip6po_route.ro_rt) {
1835 RTFREE(pktopt->ip6po_route.ro_rt);
1836 pktopt->ip6po_route.ro_rt = NULL;
1839 if (optname == -1) {
1840 if (needfree && pktopt->ip6po_dest2)
1841 free(pktopt->ip6po_dest2, M_IP6OPT);
1842 pktopt->ip6po_dest2 = NULL;
1846 #define PKTOPT_EXTHDRCPY(type) \
1850 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
1851 dst->type = malloc(hlen, M_IP6OPT, canwait);\
1852 if (dst->type == NULL && canwait == M_NOWAIT)\
1854 bcopy(src->type, dst->type, hlen);\
1858 struct ip6_pktopts *
1859 ip6_copypktopts(src, canwait)
1860 struct ip6_pktopts *src;
1863 struct ip6_pktopts *dst;
1866 printf("ip6_clearpktopts: invalid argument\n");
1870 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
1871 if (dst == NULL && canwait == M_NOWAIT)
1873 bzero(dst, sizeof(*dst));
1875 dst->ip6po_hlim = src->ip6po_hlim;
1876 if (src->ip6po_pktinfo) {
1877 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
1879 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
1881 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
1883 if (src->ip6po_nexthop) {
1884 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
1886 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
1888 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
1889 src->ip6po_nexthop->sa_len);
1891 PKTOPT_EXTHDRCPY(ip6po_hbh);
1892 PKTOPT_EXTHDRCPY(ip6po_dest1);
1893 PKTOPT_EXTHDRCPY(ip6po_dest2);
1894 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
1898 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
1899 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
1900 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
1901 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
1902 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
1903 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
1904 free(dst, M_IP6OPT);
1907 #undef PKTOPT_EXTHDRCPY
1910 ip6_freepcbopts(pktopt)
1911 struct ip6_pktopts *pktopt;
1916 ip6_clearpktopts(pktopt, 1, -1);
1918 free(pktopt, M_IP6OPT);
1922 * Set the IP6 multicast options in response to user setsockopt().
1925 ip6_setmoptions(optname, im6op, m)
1927 struct ip6_moptions **im6op;
1931 u_int loop, ifindex;
1932 struct ipv6_mreq *mreq;
1934 struct ip6_moptions *im6o = *im6op;
1935 struct route_in6 ro;
1936 struct sockaddr_in6 *dst;
1937 struct in6_multi_mship *imm;
1938 struct thread *td = curthread; /* XXX */
1942 * No multicast option buffer attached to the pcb;
1943 * allocate one and initialize to default values.
1945 im6o = (struct ip6_moptions *)
1946 malloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
1951 im6o->im6o_multicast_ifp = NULL;
1952 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1953 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
1954 LIST_INIT(&im6o->im6o_memberships);
1959 case IPV6_MULTICAST_IF:
1961 * Select the interface for outgoing multicast packets.
1963 if (m == NULL || m->m_len != sizeof(u_int)) {
1967 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
1968 if (ifindex < 0 || if_index < ifindex) {
1969 error = ENXIO; /* XXX EINVAL? */
1972 ifp = ifindex2ifnet[ifindex];
1973 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1974 error = EADDRNOTAVAIL;
1977 im6o->im6o_multicast_ifp = ifp;
1980 case IPV6_MULTICAST_HOPS:
1983 * Set the IP6 hoplimit for outgoing multicast packets.
1986 if (m == NULL || m->m_len != sizeof(int)) {
1990 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
1991 if (optval < -1 || optval >= 256)
1993 else if (optval == -1)
1994 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
1996 im6o->im6o_multicast_hlim = optval;
2000 case IPV6_MULTICAST_LOOP:
2002 * Set the loopback flag for outgoing multicast packets.
2003 * Must be zero or one.
2005 if (m == NULL || m->m_len != sizeof(u_int)) {
2009 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2014 im6o->im6o_multicast_loop = loop;
2017 case IPV6_JOIN_GROUP:
2019 * Add a multicast group membership.
2020 * Group must be a valid IP6 multicast address.
2022 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2026 mreq = mtod(m, struct ipv6_mreq *);
2027 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2029 * We use the unspecified address to specify to accept
2030 * all multicast addresses. Only super user is allowed
2038 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2044 * If the interface is specified, validate it.
2046 if (mreq->ipv6mr_interface < 0
2047 || if_index < mreq->ipv6mr_interface) {
2048 error = ENXIO; /* XXX EINVAL? */
2052 * If no interface was explicitly specified, choose an
2053 * appropriate one according to the given multicast address.
2055 if (mreq->ipv6mr_interface == 0) {
2057 * If the multicast address is in node-local scope,
2058 * the interface should be a loopback interface.
2059 * Otherwise, look up the routing table for the
2060 * address, and choose the outgoing interface.
2061 * XXX: is it a good approach?
2063 if (IN6_IS_ADDR_MC_NODELOCAL(&mreq->ipv6mr_multiaddr)) {
2067 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2068 bzero(dst, sizeof(*dst));
2069 dst->sin6_len = sizeof(struct sockaddr_in6);
2070 dst->sin6_family = AF_INET6;
2071 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2072 rtalloc((struct route *)&ro);
2073 if (ro.ro_rt == NULL) {
2074 error = EADDRNOTAVAIL;
2077 ifp = ro.ro_rt->rt_ifp;
2081 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2084 * See if we found an interface, and confirm that it
2085 * supports multicast
2087 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2088 error = EADDRNOTAVAIL;
2092 * Put interface index into the multicast address,
2093 * if the address has link-local scope.
2095 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2096 mreq->ipv6mr_multiaddr.s6_addr16[1]
2097 = htons(mreq->ipv6mr_interface);
2100 * See if the membership already exists.
2102 for (imm = im6o->im6o_memberships.lh_first;
2103 imm != NULL; imm = imm->i6mm_chain.le_next)
2104 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2105 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2106 &mreq->ipv6mr_multiaddr))
2113 * Everything looks good; add a new record to the multicast
2114 * address list for the given interface.
2116 imm = malloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2121 if ((imm->i6mm_maddr =
2122 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2123 free(imm, M_IPMADDR);
2126 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2129 case IPV6_LEAVE_GROUP:
2131 * Drop a multicast group membership.
2132 * Group must be a valid IP6 multicast address.
2134 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2138 mreq = mtod(m, struct ipv6_mreq *);
2139 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2144 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2149 * If an interface address was specified, get a pointer
2150 * to its ifnet structure.
2152 if (mreq->ipv6mr_interface < 0
2153 || if_index < mreq->ipv6mr_interface) {
2154 error = ENXIO; /* XXX EINVAL? */
2157 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2159 * Put interface index into the multicast address,
2160 * if the address has link-local scope.
2162 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2163 mreq->ipv6mr_multiaddr.s6_addr16[1]
2164 = htons(mreq->ipv6mr_interface);
2167 * Find the membership in the membership list.
2169 for (imm = im6o->im6o_memberships.lh_first;
2170 imm != NULL; imm = imm->i6mm_chain.le_next) {
2172 imm->i6mm_maddr->in6m_ifp == ifp) &&
2173 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2174 &mreq->ipv6mr_multiaddr))
2178 /* Unable to resolve interface */
2179 error = EADDRNOTAVAIL;
2183 * Give up the multicast address record to which the
2184 * membership points.
2186 LIST_REMOVE(imm, i6mm_chain);
2187 in6_delmulti(imm->i6mm_maddr);
2188 free(imm, M_IPMADDR);
2197 * If all options have default values, no need to keep the mbuf.
2199 if (im6o->im6o_multicast_ifp == NULL &&
2200 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2201 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2202 im6o->im6o_memberships.lh_first == NULL) {
2203 free(*im6op, M_IPMOPTS);
2211 * Return the IP6 multicast options in response to user getsockopt().
2214 ip6_getmoptions(optname, im6o, mp)
2216 struct ip6_moptions *im6o;
2219 u_int *hlim, *loop, *ifindex;
2221 *mp = m_get(M_WAIT, MT_HEADER); /* XXX */
2225 case IPV6_MULTICAST_IF:
2226 ifindex = mtod(*mp, u_int *);
2227 (*mp)->m_len = sizeof(u_int);
2228 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2231 *ifindex = im6o->im6o_multicast_ifp->if_index;
2234 case IPV6_MULTICAST_HOPS:
2235 hlim = mtod(*mp, u_int *);
2236 (*mp)->m_len = sizeof(u_int);
2238 *hlim = ip6_defmcasthlim;
2240 *hlim = im6o->im6o_multicast_hlim;
2243 case IPV6_MULTICAST_LOOP:
2244 loop = mtod(*mp, u_int *);
2245 (*mp)->m_len = sizeof(u_int);
2247 *loop = ip6_defmcasthlim;
2249 *loop = im6o->im6o_multicast_loop;
2258 * Discard the IP6 multicast options.
2261 ip6_freemoptions(im6o)
2262 struct ip6_moptions *im6o;
2264 struct in6_multi_mship *imm;
2269 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2270 LIST_REMOVE(imm, i6mm_chain);
2271 if (imm->i6mm_maddr)
2272 in6_delmulti(imm->i6mm_maddr);
2273 free(imm, M_IPMADDR);
2275 free(im6o, M_IPMOPTS);
2279 * Set IPv6 outgoing packet options based on advanced API.
2282 ip6_setpktoptions(control, opt, priv, needcopy)
2283 struct mbuf *control;
2284 struct ip6_pktopts *opt;
2287 struct cmsghdr *cm = 0;
2289 if (control == 0 || opt == 0)
2292 init_ip6pktopts(opt);
2295 * XXX: Currently, we assume all the optional information is stored
2298 if (control->m_next)
2301 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2302 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2303 cm = mtod(control, struct cmsghdr *);
2304 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2306 if (cm->cmsg_level != IPPROTO_IPV6)
2310 * XXX should check if RFC2292 API is mixed with 2292bis API
2312 switch (cm->cmsg_type) {
2314 if (cm->cmsg_len != CMSG_LEN(sizeof(struct in6_pktinfo)))
2317 /* XXX: Is it really WAITOK? */
2318 opt->ip6po_pktinfo =
2319 malloc(sizeof(struct in6_pktinfo),
2320 M_IP6OPT, M_WAITOK);
2321 bcopy(CMSG_DATA(cm), opt->ip6po_pktinfo,
2322 sizeof(struct in6_pktinfo));
2324 opt->ip6po_pktinfo =
2325 (struct in6_pktinfo *)CMSG_DATA(cm);
2326 if (opt->ip6po_pktinfo->ipi6_ifindex &&
2327 IN6_IS_ADDR_LINKLOCAL(&opt->ip6po_pktinfo->ipi6_addr))
2328 opt->ip6po_pktinfo->ipi6_addr.s6_addr16[1] =
2329 htons(opt->ip6po_pktinfo->ipi6_ifindex);
2331 if (opt->ip6po_pktinfo->ipi6_ifindex > if_index
2332 || opt->ip6po_pktinfo->ipi6_ifindex < 0) {
2337 * Check if the requested source address is indeed a
2338 * unicast address assigned to the node, and can be
2339 * used as the packet's source address.
2341 if (!IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2342 struct in6_ifaddr *ia6;
2343 struct sockaddr_in6 sin6;
2345 bzero(&sin6, sizeof(sin6));
2346 sin6.sin6_len = sizeof(sin6);
2347 sin6.sin6_family = AF_INET6;
2349 opt->ip6po_pktinfo->ipi6_addr;
2350 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2352 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2353 IN6_IFF_NOTREADY)) != 0)
2354 return(EADDRNOTAVAIL);
2359 if (cm->cmsg_len != CMSG_LEN(sizeof(int)))
2362 opt->ip6po_hlim = *(int *)CMSG_DATA(cm);
2363 if (opt->ip6po_hlim < -1 || opt->ip6po_hlim > 255)
2371 if (cm->cmsg_len < sizeof(u_char) ||
2372 /* check if cmsg_len is large enough for sa_len */
2373 cm->cmsg_len < CMSG_LEN(*CMSG_DATA(cm)))
2377 opt->ip6po_nexthop =
2378 malloc(*CMSG_DATA(cm),
2379 M_IP6OPT, M_WAITOK);
2380 bcopy(CMSG_DATA(cm),
2384 opt->ip6po_nexthop =
2385 (struct sockaddr *)CMSG_DATA(cm);
2390 struct ip6_hbh *hbh;
2393 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_hbh)))
2395 hbh = (struct ip6_hbh *)CMSG_DATA(cm);
2396 hbhlen = (hbh->ip6h_len + 1) << 3;
2397 if (cm->cmsg_len != CMSG_LEN(hbhlen))
2402 malloc(hbhlen, M_IP6OPT, M_WAITOK);
2403 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2405 opt->ip6po_hbh = hbh;
2411 struct ip6_dest *dest, **newdest;
2414 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_dest)))
2416 dest = (struct ip6_dest *)CMSG_DATA(cm);
2417 destlen = (dest->ip6d_len + 1) << 3;
2418 if (cm->cmsg_len != CMSG_LEN(destlen))
2422 * The old advacned API is ambiguous on this
2423 * point. Our approach is to determine the
2424 * position based according to the existence
2425 * of a routing header. Note, however, that
2426 * this depends on the order of the extension
2427 * headers in the ancillary data; the 1st part
2428 * of the destination options header must
2429 * appear before the routing header in the
2430 * ancillary data, too.
2431 * RFC2292bis solved the ambiguity by
2432 * introducing separate cmsg types.
2434 if (opt->ip6po_rthdr == NULL)
2435 newdest = &opt->ip6po_dest1;
2437 newdest = &opt->ip6po_dest2;
2440 *newdest = malloc(destlen, M_IP6OPT, M_WAITOK);
2441 bcopy(dest, *newdest, destlen);
2450 struct ip6_rthdr *rth;
2453 if (cm->cmsg_len < CMSG_LEN(sizeof(struct ip6_rthdr)))
2455 rth = (struct ip6_rthdr *)CMSG_DATA(cm);
2456 rthlen = (rth->ip6r_len + 1) << 3;
2457 if (cm->cmsg_len != CMSG_LEN(rthlen))
2460 switch (rth->ip6r_type) {
2461 case IPV6_RTHDR_TYPE_0:
2462 /* must contain one addr */
2463 if (rth->ip6r_len == 0)
2465 /* length must be even */
2466 if (rth->ip6r_len % 2)
2468 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
2472 return(EINVAL); /* not supported */
2476 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT,
2478 bcopy(rth, opt->ip6po_rthdr, rthlen);
2480 opt->ip6po_rthdr = rth;
2486 return(ENOPROTOOPT);
2494 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
2495 * packet to the input queue of a specified interface. Note that this
2496 * calls the output routine of the loopback "driver", but with an interface
2497 * pointer that might NOT be &loif -- easier than replicating that code here.
2500 ip6_mloopback(ifp, m, dst)
2503 struct sockaddr_in6 *dst;
2506 struct ip6_hdr *ip6;
2508 copym = m_copy(m, 0, M_COPYALL);
2513 * Make sure to deep-copy IPv6 header portion in case the data
2514 * is in an mbuf cluster, so that we can safely override the IPv6
2515 * header portion later.
2517 if ((copym->m_flags & M_EXT) != 0 ||
2518 copym->m_len < sizeof(struct ip6_hdr)) {
2519 copym = m_pullup(copym, sizeof(struct ip6_hdr));
2525 if (copym->m_len < sizeof(*ip6)) {
2531 ip6 = mtod(copym, struct ip6_hdr *);
2532 #ifndef SCOPEDROUTING
2534 * clear embedded scope identifiers if necessary.
2535 * in6_clearscope will touch the addresses only when necessary.
2537 in6_clearscope(&ip6->ip6_src);
2538 in6_clearscope(&ip6->ip6_dst);
2541 (void)if_simloop(ifp, copym, dst->sin6_family, NULL);
2545 * Chop IPv6 header off from the payload.
2548 ip6_splithdr(m, exthdrs)
2550 struct ip6_exthdrs *exthdrs;
2553 struct ip6_hdr *ip6;
2555 ip6 = mtod(m, struct ip6_hdr *);
2556 if (m->m_len > sizeof(*ip6)) {
2557 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
2562 M_MOVE_PKTHDR(mh, m);
2563 MH_ALIGN(mh, sizeof(*ip6));
2564 m->m_len -= sizeof(*ip6);
2565 m->m_data += sizeof(*ip6);
2568 m->m_len = sizeof(*ip6);
2569 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
2571 exthdrs->ip6e_ip6 = m;
2576 * Compute IPv6 extension header length.
2580 struct in6pcb *in6p;
2584 if (!in6p->in6p_outputopts)
2589 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
2591 len += elen(in6p->in6p_outputopts->ip6po_hbh);
2592 if (in6p->in6p_outputopts->ip6po_rthdr)
2593 /* dest1 is valid with rthdr only */
2594 len += elen(in6p->in6p_outputopts->ip6po_dest1);
2595 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
2596 len += elen(in6p->in6p_outputopts->ip6po_dest2);