kernel/msdosfs: Add pathconf(3) support for FILESIZEBITS.
[dragonfly.git] / sys / netinet6 / ip6_output.c
blobf42d86d25295981c9586de60df75b8f0a8ab13e8
1 /* $FreeBSD: src/sys/netinet6/ip6_output.c,v 1.13.2.18 2003/01/24 05:11:35 sam Exp $ */
2 /* $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
64 #include "opt_ip6fw.h"
65 #include "opt_inet.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
69 #include <sys/param.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/errno.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/proc.h>
79 #include <sys/priv.h>
81 #include <sys/thread2.h>
82 #include <sys/msgport2.h>
84 #include <net/if.h>
85 #include <net/route.h>
86 #include <net/pfil.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>
96 #include <netinet6/ip6protosw.h>
98 #ifdef IPSEC
99 #include <netinet6/ipsec.h>
100 #ifdef INET6
101 #include <netinet6/ipsec6.h>
102 #endif
103 #include <netproto/key/key.h>
104 #endif /* IPSEC */
106 #ifdef FAST_IPSEC
107 #include <netproto/ipsec/ipsec.h>
108 #include <netproto/ipsec/ipsec6.h>
109 #include <netproto/ipsec/key.h>
110 #endif
112 #include <net/ip6fw/ip6_fw.h>
114 #include <net/net_osdep.h>
116 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
118 struct ip6_exthdrs {
119 struct mbuf *ip6e_ip6;
120 struct mbuf *ip6e_hbh;
121 struct mbuf *ip6e_dest1;
122 struct mbuf *ip6e_rthdr;
123 struct mbuf *ip6e_dest2;
126 static int ip6_pcbopt (int, u_char *, int, struct ip6_pktopts **, int);
127 static int ip6_setpktoption (int, u_char *, int, struct ip6_pktopts *,
128 int, int, int, int);
129 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *,
130 struct sockopt *);
131 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
132 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
133 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
134 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
135 struct ifnet *, struct in6_addr *, u_long *, int *);
136 static int copyexthdr (void *, struct mbuf **);
137 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
138 struct ip6_frag **);
139 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
140 static struct mbuf *ip6_splithdr (struct mbuf *);
141 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
144 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
145 * header (with pri, len, nxt, hlim, src, dst).
146 * This function may modify ver and hlim only.
147 * The mbuf chain containing the packet will be freed.
148 * The mbuf opt, if present, will not be freed.
150 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
151 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
152 * which is rt_rmx.rmx_mtu.
155 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
156 int flags, struct ip6_moptions *im6o,
157 struct ifnet **ifpp, /* XXX: just for statistics */
158 struct inpcb *inp)
160 struct ip6_hdr *ip6, *mhip6;
161 struct ifnet *ifp, *origifp;
162 struct mbuf *m = m0;
163 struct mbuf *mprev;
164 u_char *nexthdrp;
165 int hlen, tlen, len, off;
166 struct route_in6 ip6route;
167 struct sockaddr_in6 *dst;
168 int error = 0;
169 struct in6_ifaddr *ia = NULL;
170 u_long mtu;
171 int alwaysfrag, dontfrag;
172 u_int32_t optlen, plen = 0, unfragpartlen;
173 struct ip6_exthdrs exthdrs;
174 struct in6_addr finaldst;
175 struct route_in6 *ro_pmtu = NULL;
176 boolean_t hdrsplit = FALSE;
177 boolean_t needipsec = FALSE;
178 #ifdef IPSEC
179 boolean_t needipsectun = FALSE;
180 struct secpolicy *sp = NULL;
181 struct socket *so = inp ? inp->inp_socket : NULL;
183 ip6 = mtod(m, struct ip6_hdr *);
184 #endif
185 #ifdef FAST_IPSEC
186 boolean_t needipsectun = FALSE;
187 struct secpolicy *sp = NULL;
189 ip6 = mtod(m, struct ip6_hdr *);
190 #endif
192 bzero(&exthdrs, sizeof exthdrs);
194 if (opt) {
195 if ((error = copyexthdr(opt->ip6po_hbh, &exthdrs.ip6e_hbh)))
196 goto freehdrs;
197 if ((error = copyexthdr(opt->ip6po_dest1, &exthdrs.ip6e_dest1)))
198 goto freehdrs;
199 if ((error = copyexthdr(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr)))
200 goto freehdrs;
201 if ((error = copyexthdr(opt->ip6po_dest2, &exthdrs.ip6e_dest2)))
202 goto freehdrs;
205 #ifdef IPSEC
206 /* get a security policy for this packet */
207 if (so == NULL)
208 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
209 else
210 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
212 if (sp == NULL) {
213 ipsec6stat.out_inval++;
214 goto freehdrs;
217 error = 0;
219 /* check policy */
220 switch (sp->policy) {
221 case IPSEC_POLICY_DISCARD:
223 * This packet is just discarded.
225 ipsec6stat.out_polvio++;
226 goto freehdrs;
228 case IPSEC_POLICY_BYPASS:
229 case IPSEC_POLICY_NONE:
230 /* no need to do IPsec. */
231 needipsec = FALSE;
232 break;
234 case IPSEC_POLICY_IPSEC:
235 if (sp->req == NULL) {
236 error = key_spdacquire(sp); /* acquire a policy */
237 goto freehdrs;
239 needipsec = TRUE;
240 break;
242 case IPSEC_POLICY_ENTRUST:
243 default:
244 kprintf("ip6_output: Invalid policy found. %d\n", sp->policy);
246 #endif /* IPSEC */
247 #ifdef FAST_IPSEC
248 /* get a security policy for this packet */
249 if (inp == NULL)
250 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 0, &error);
251 else
252 sp = ipsec_getpolicybysock(m, IPSEC_DIR_OUTBOUND, inp, &error);
254 if (sp == NULL) {
255 newipsecstat.ips_out_inval++;
256 goto freehdrs;
259 error = 0;
261 /* check policy */
262 switch (sp->policy) {
263 case IPSEC_POLICY_DISCARD:
265 * This packet is just discarded.
267 newipsecstat.ips_out_polvio++;
268 goto freehdrs;
270 case IPSEC_POLICY_BYPASS:
271 case IPSEC_POLICY_NONE:
272 /* no need to do IPsec. */
273 needipsec = FALSE;
274 break;
276 case IPSEC_POLICY_IPSEC:
277 if (sp->req == NULL) {
278 error = key_spdacquire(sp); /* acquire a policy */
279 goto freehdrs;
281 needipsec = TRUE;
282 break;
284 case IPSEC_POLICY_ENTRUST:
285 default:
286 kprintf("ip6_output: Invalid policy found. %d\n", sp->policy);
288 #endif /* FAST_IPSEC */
291 * Calculate the total length of the extension header chain.
292 * Keep the length of the unfragmentable part for fragmentation.
294 optlen = m_lengthm(exthdrs.ip6e_hbh, NULL) +
295 m_lengthm(exthdrs.ip6e_dest1, NULL) +
296 m_lengthm(exthdrs.ip6e_rthdr, NULL);
298 unfragpartlen = optlen + sizeof(struct ip6_hdr);
300 /* NOTE: we don't add AH/ESP length here. do that later. */
301 optlen += m_lengthm(exthdrs.ip6e_dest2, NULL);
304 * If we need IPsec, or there is at least one extension header,
305 * separate IP6 header from the payload.
307 if ((needipsec || optlen) && !hdrsplit) {
308 exthdrs.ip6e_ip6 = ip6_splithdr(m);
309 if (exthdrs.ip6e_ip6 == NULL) {
310 error = ENOBUFS;
311 goto freehdrs;
313 m = exthdrs.ip6e_ip6;
314 hdrsplit = TRUE;
317 /* adjust pointer */
318 ip6 = mtod(m, struct ip6_hdr *);
320 /* adjust mbuf packet header length */
321 m->m_pkthdr.len += optlen;
322 plen = m->m_pkthdr.len - sizeof(*ip6);
324 /* If this is a jumbo payload, insert a jumbo payload option. */
325 if (plen > IPV6_MAXPACKET) {
326 if (!hdrsplit) {
327 exthdrs.ip6e_ip6 = ip6_splithdr(m);
328 if (exthdrs.ip6e_ip6 == NULL) {
329 error = ENOBUFS;
330 goto freehdrs;
332 m = exthdrs.ip6e_ip6;
333 hdrsplit = TRUE;
335 /* adjust pointer */
336 ip6 = mtod(m, struct ip6_hdr *);
337 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
338 goto freehdrs;
339 ip6->ip6_plen = 0;
340 } else
341 ip6->ip6_plen = htons(plen);
344 * Concatenate headers and fill in next header fields.
345 * Here we have, on "m"
346 * IPv6 payload
347 * and we insert headers accordingly. Finally, we should be getting:
348 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
350 * during the header composing process, "m" points to IPv6 header.
351 * "mprev" points to an extension header prior to esp.
354 nexthdrp = &ip6->ip6_nxt;
355 mprev = m;
358 * we treat dest2 specially. this makes IPsec processing
359 * much easier. the goal here is to make mprev point the
360 * mbuf prior to dest2.
362 * result: IPv6 dest2 payload
363 * m and mprev will point to IPv6 header.
365 if (exthdrs.ip6e_dest2) {
366 if (!hdrsplit)
367 panic("assumption failed: hdr not split");
368 exthdrs.ip6e_dest2->m_next = m->m_next;
369 m->m_next = exthdrs.ip6e_dest2;
370 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
371 ip6->ip6_nxt = IPPROTO_DSTOPTS;
375 * Place m1 after mprev.
377 #define MAKE_CHAIN(m1, mprev, nexthdrp, i)\
378 do {\
379 if (m1) {\
380 if (!hdrsplit)\
381 panic("assumption failed: hdr not split");\
382 *mtod(m1, u_char *) = *nexthdrp;\
383 *nexthdrp = (i);\
384 nexthdrp = mtod(m1, u_char *);\
385 m1->m_next = mprev->m_next;\
386 mprev->m_next = m1;\
387 mprev = m1;\
389 } while (0)
392 * result: IPv6 hbh dest1 rthdr dest2 payload
393 * m will point to IPv6 header. mprev will point to the
394 * extension header prior to dest2 (rthdr in the above case).
396 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
397 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
398 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
400 #if defined(IPSEC) || defined(FAST_IPSEC)
401 if (needipsec) {
402 struct ipsec_output_state state;
403 int segleft_org = 0;
404 struct ip6_rthdr *rh = NULL;
407 * pointers after IPsec headers are not valid any more.
408 * other pointers need a great care too.
409 * (IPsec routines should not mangle mbufs prior to AH/ESP)
411 exthdrs.ip6e_dest2 = NULL;
413 if (exthdrs.ip6e_rthdr) {
414 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
415 segleft_org = rh->ip6r_segleft;
416 rh->ip6r_segleft = 0;
419 bzero(&state, sizeof state);
420 state.m = m;
421 error = ipsec6_output_trans(&state, nexthdrp, mprev, sp, flags,
422 &needipsectun);
423 m = state.m;
424 if (error) {
425 /* mbuf is already reclaimed in ipsec6_output_trans. */
426 m = NULL;
427 switch (error) {
428 case EHOSTUNREACH:
429 case ENETUNREACH:
430 case EMSGSIZE:
431 case ENOBUFS:
432 case ENOMEM:
433 break;
434 default:
435 kprintf("ip6_output (ipsec): error code %d\n",
436 error);
437 /* FALLTHROUGH */
438 case ENOENT:
439 /* don't show these error codes to the user */
440 error = 0;
441 break;
443 goto bad;
445 if (exthdrs.ip6e_rthdr) {
446 /* ah6_output doesn't modify mbuf chain */
447 rh->ip6r_segleft = segleft_org;
450 #endif
453 * If there is a routing header, replace the destination address field
454 * with the first hop of the routing header.
456 if (exthdrs.ip6e_rthdr) {
457 struct ip6_rthdr *rh;
459 finaldst = ip6->ip6_dst;
460 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
461 switch (rh->ip6r_type) {
462 default: /* is it possible? */
463 error = EINVAL;
464 goto bad;
468 /* Source address validation */
469 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
470 !(flags & IPV6_DADOUTPUT)) {
471 error = EOPNOTSUPP;
472 ip6stat.ip6s_badscope++;
473 goto bad;
475 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
476 error = EOPNOTSUPP;
477 ip6stat.ip6s_badscope++;
478 goto bad;
481 ip6stat.ip6s_localout++;
484 * Route packet.
486 if (ro == NULL) {
487 ro = &ip6route;
488 bzero(ro, sizeof(*ro));
490 ro_pmtu = ro;
491 if (opt && opt->ip6po_rthdr)
492 ro = &opt->ip6po_route;
493 dst = (struct sockaddr_in6 *)&ro->ro_dst;
496 * If there is a cached route,
497 * check that it is to the same destination
498 * and is still up. If not, free it and try again.
500 if (ro->ro_rt != NULL &&
501 (!(ro->ro_rt->rt_flags & RTF_UP) || dst->sin6_family != AF_INET6 ||
502 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
503 RTFREE(ro->ro_rt);
504 ro->ro_rt = NULL;
506 if (ro->ro_rt == NULL) {
507 bzero(dst, sizeof(*dst));
508 dst->sin6_family = AF_INET6;
509 dst->sin6_len = sizeof(struct sockaddr_in6);
510 dst->sin6_addr = ip6->ip6_dst;
512 #if defined(IPSEC) || defined(FAST_IPSEC)
513 if (needipsec && needipsectun) {
514 struct ipsec_output_state state;
517 * All the extension headers will become inaccessible
518 * (since they can be encrypted).
519 * Don't panic, we need no more updates to extension headers
520 * on inner IPv6 packet (since they are now encapsulated).
522 * IPv6 [ESP|AH] IPv6 [extension headers] payload
524 bzero(&exthdrs, sizeof(exthdrs));
525 exthdrs.ip6e_ip6 = m;
527 bzero(&state, sizeof(state));
528 state.m = m;
529 state.ro = (struct route *)ro;
530 state.dst = (struct sockaddr *)dst;
532 error = ipsec6_output_tunnel(&state, sp, flags);
534 m = state.m;
535 ro = (struct route_in6 *)state.ro;
536 dst = (struct sockaddr_in6 *)state.dst;
537 if (error) {
538 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
539 m0 = m = NULL;
540 m = NULL;
541 switch (error) {
542 case EHOSTUNREACH:
543 case ENETUNREACH:
544 case EMSGSIZE:
545 case ENOBUFS:
546 case ENOMEM:
547 break;
548 default:
549 kprintf("ip6_output (ipsec): error code %d\n", error);
550 /* FALLTHROUGH */
551 case ENOENT:
552 /* don't show these error codes to the user */
553 error = 0;
554 break;
556 goto bad;
559 exthdrs.ip6e_ip6 = m;
561 #endif
563 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
564 /* Unicast */
566 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
567 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
568 /* xxx
569 * interface selection comes here
570 * if an interface is specified from an upper layer,
571 * ifp must point it.
573 if (ro->ro_rt == NULL) {
575 * non-bsdi always clone routes, if parent is
576 * PRF_CLONING.
578 rtalloc((struct route *)ro);
580 if (ro->ro_rt == NULL) {
581 ip6stat.ip6s_noroute++;
582 error = EHOSTUNREACH;
583 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
584 goto bad;
586 ia = ifatoia6(ro->ro_rt->rt_ifa);
587 ifp = ro->ro_rt->rt_ifp;
588 ro->ro_rt->rt_use++;
589 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
590 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
591 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
593 in6_ifstat_inc(ifp, ifs6_out_request);
596 * Check if the outgoing interface conflicts with
597 * the interface specified by ifi6_ifindex (if specified).
598 * Note that loopback interface is always okay.
599 * (this may happen when we are sending a packet to one of
600 * our own addresses.)
602 if (opt && opt->ip6po_pktinfo
603 && opt->ip6po_pktinfo->ipi6_ifindex) {
604 if (!(ifp->if_flags & IFF_LOOPBACK)
605 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
606 ip6stat.ip6s_noroute++;
607 in6_ifstat_inc(ifp, ifs6_out_discard);
608 error = EHOSTUNREACH;
609 goto bad;
613 if (opt && opt->ip6po_hlim != -1)
614 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
615 } else {
616 /* Multicast */
617 struct in6_multi *in6m;
619 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
622 * See if the caller provided any multicast options
624 ifp = NULL;
625 if (im6o != NULL) {
626 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
627 if (im6o->im6o_multicast_ifp != NULL)
628 ifp = im6o->im6o_multicast_ifp;
629 } else
630 ip6->ip6_hlim = ip6_defmcasthlim;
633 * See if the caller provided the outgoing interface
634 * as an ancillary data.
635 * Boundary check for ifindex is assumed to be already done.
637 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
638 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
641 * If the destination is a node-local scope multicast,
642 * the packet should be loop-backed only.
644 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
646 * If the outgoing interface is already specified,
647 * it should be a loopback interface.
649 if (ifp && !(ifp->if_flags & IFF_LOOPBACK)) {
650 ip6stat.ip6s_badscope++;
651 error = ENETUNREACH; /* XXX: better error? */
652 /* XXX correct ifp? */
653 in6_ifstat_inc(ifp, ifs6_out_discard);
654 goto bad;
655 } else {
656 ifp = &loif[0];
660 if (opt && opt->ip6po_hlim != -1)
661 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
664 * If caller did not provide an interface lookup a
665 * default in the routing table. This is either a
666 * default for the speicfied group (i.e. a host
667 * route), or a multicast default (a route for the
668 * ``net'' ff00::/8).
670 if (ifp == NULL) {
671 if (ro->ro_rt == NULL) {
672 ro->ro_rt =
673 rtpurelookup((struct sockaddr *)&ro->ro_dst);
675 if (ro->ro_rt == NULL) {
676 ip6stat.ip6s_noroute++;
677 error = EHOSTUNREACH;
678 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
679 goto bad;
681 ia = ifatoia6(ro->ro_rt->rt_ifa);
682 ifp = ro->ro_rt->rt_ifp;
683 ro->ro_rt->rt_use++;
686 if (!(flags & IPV6_FORWARDING))
687 in6_ifstat_inc(ifp, ifs6_out_request);
688 in6_ifstat_inc(ifp, ifs6_out_mcast);
691 * Confirm that the outgoing interface supports multicast.
693 if (!(ifp->if_flags & IFF_MULTICAST)) {
694 ip6stat.ip6s_noroute++;
695 in6_ifstat_inc(ifp, ifs6_out_discard);
696 error = ENETUNREACH;
697 goto bad;
699 in6m = IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp);
700 if (in6m != NULL &&
701 (im6o == NULL || im6o->im6o_multicast_loop)) {
703 * If we belong to the destination multicast group
704 * on the outgoing interface, and the caller did not
705 * forbid loopback, loop back a copy.
707 ip6_mloopback(ifp, m, dst);
708 } else {
710 * If we are acting as a multicast router, perform
711 * multicast forwarding as if the packet had just
712 * arrived on the interface to which we are about
713 * to send. The multicast forwarding function
714 * recursively calls this function, using the
715 * IPV6_FORWARDING flag to prevent infinite recursion.
717 * Multicasts that are looped back by ip6_mloopback(),
718 * above, will be forwarded by the ip6_input() routine,
719 * if necessary.
721 if (ip6_mrouter && !(flags & IPV6_FORWARDING)) {
722 if (ip6_mforward(ip6, ifp, m) != 0) {
723 m_freem(m);
724 goto done;
729 * Multicasts with a hoplimit of zero may be looped back,
730 * above, but must not be transmitted on a network.
731 * Also, multicasts addressed to the loopback interface
732 * are not sent -- the above call to ip6_mloopback() will
733 * loop back a copy if this host actually belongs to the
734 * destination group on the loopback interface.
736 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
737 m_freem(m);
738 goto done;
743 * Fill the outgoing inteface to tell the upper layer
744 * to increment per-interface statistics.
746 if (ifpp)
747 *ifpp = ifp;
749 /* Determine path MTU. */
750 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
751 &alwaysfrag)) != 0)
752 goto bad;
755 * The caller of this function may specify to use the minimum MTU
756 * in some cases.
757 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
758 * setting. The logic is a bit complicated; by default, unicast
759 * packets will follow path MTU while multicast packets will be sent at
760 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
761 * including unicast ones will be sent at the minimum MTU. Multicast
762 * packets will always be sent at the minimum MTU unless
763 * IP6PO_MINMTU_DISABLE is explicitly specified.
764 * See RFC 3542 for more details.
766 if (mtu > IPV6_MMTU) {
767 if ((flags & IPV6_MINMTU))
768 mtu = IPV6_MMTU;
769 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
770 mtu = IPV6_MMTU;
771 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
772 (opt == NULL ||
773 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
774 mtu = IPV6_MMTU;
778 /* Fake scoped addresses */
779 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
781 * If source or destination address is a scoped address, and
782 * the packet is going to be sent to a loopback interface,
783 * we should keep the original interface.
787 * XXX: this is a very experimental and temporary solution.
788 * We eventually have sockaddr_in6 and use the sin6_scope_id
789 * field of the structure here.
790 * We rely on the consistency between two scope zone ids
791 * of source and destination, which should already be assured.
792 * Larger scopes than link will be supported in the future.
794 origifp = NULL;
795 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
796 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
797 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
798 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
800 * XXX: origifp can be NULL even in those two cases above.
801 * For example, if we remove the (only) link-local address
802 * from the loopback interface, and try to send a link-local
803 * address without link-id information. Then the source
804 * address is ::1, and the destination address is the
805 * link-local address with its s6_addr16[1] being zero.
806 * What is worse, if the packet goes to the loopback interface
807 * by a default rejected route, the null pointer would be
808 * passed to looutput, and the kernel would hang.
809 * The following last resort would prevent such disaster.
811 if (origifp == NULL)
812 origifp = ifp;
814 else
815 origifp = ifp;
817 * clear embedded scope identifiers if necessary.
818 * in6_clearscope will touch the addresses only when necessary.
820 in6_clearscope(&ip6->ip6_src);
821 in6_clearscope(&ip6->ip6_dst);
824 * Check with the firewall...
826 if (ip6_fw_enable && ip6_fw_chk_ptr) {
827 u_short port = 0;
829 m->m_pkthdr.rcvif = NULL; /* XXX */
830 /* If ipfw says divert, we have to just drop packet */
831 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
832 m_freem(m);
833 goto done;
835 if (!m) {
836 error = EACCES;
837 goto done;
842 * If the outgoing packet contains a hop-by-hop options header,
843 * it must be examined and processed even by the source node.
844 * (RFC 2460, section 4.)
846 if (exthdrs.ip6e_hbh) {
847 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
848 u_int32_t dummy1; /* XXX unused */
849 u_int32_t dummy2; /* XXX unused */
851 #ifdef DIAGNOSTIC
852 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
853 panic("ip6e_hbh is not continuous");
854 #endif
856 * XXX: if we have to send an ICMPv6 error to the sender,
857 * we need the M_LOOP flag since icmp6_error() expects
858 * the IPv6 and the hop-by-hop options header are
859 * continuous unless the flag is set.
861 m->m_flags |= M_LOOP;
862 m->m_pkthdr.rcvif = ifp;
863 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
864 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
865 &dummy1, &dummy2) < 0) {
866 /* m was already freed at this point */
867 error = EINVAL;/* better error? */
868 goto done;
870 m->m_flags &= ~M_LOOP; /* XXX */
871 m->m_pkthdr.rcvif = NULL;
875 * Run through list of hooks for output packets.
877 if (pfil_has_hooks(&inet6_pfil_hook)) {
878 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
879 if (error != 0 || m == NULL)
880 goto done;
881 ip6 = mtod(m, struct ip6_hdr *);
885 * Send the packet to the outgoing interface.
886 * If necessary, do IPv6 fragmentation before sending.
888 * the logic here is rather complex:
889 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
890 * 1-a: send as is if tlen <= path mtu
891 * 1-b: fragment if tlen > path mtu
893 * 2: if user asks us not to fragment (dontfrag == 1)
894 * 2-a: send as is if tlen <= interface mtu
895 * 2-b: error if tlen > interface mtu
897 * 3: if we always need to attach fragment header (alwaysfrag == 1)
898 * always fragment
900 * 4: if dontfrag == 1 && alwaysfrag == 1
901 * error, as we cannot handle this conflicting request
903 tlen = m->m_pkthdr.len;
905 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
906 dontfrag = 1;
907 else
908 dontfrag = 0;
909 if (dontfrag && alwaysfrag) { /* case 4 */
910 /* conflicting request - can't transmit */
911 error = EMSGSIZE;
912 goto bad;
914 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
916 * Even if the DONTFRAG option is specified, we cannot send the
917 * packet when the data length is larger than the MTU of the
918 * outgoing interface.
919 * Notify the error by sending IPV6_PATHMTU ancillary data as
920 * well as returning an error code (the latter is not described
921 * in the API spec.)
923 u_int32_t mtu32;
924 struct ip6ctlparam ip6cp;
926 mtu32 = (u_int32_t)mtu;
927 bzero(&ip6cp, sizeof(ip6cp));
928 ip6cp.ip6c_cmdarg = (void *)&mtu32;
929 kpfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
930 (void *)&ip6cp);
932 error = EMSGSIZE;
933 goto bad;
937 * transmit packet without fragmentation
939 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
940 struct in6_ifaddr *ia6;
942 ip6 = mtod(m, struct ip6_hdr *);
943 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
944 if (ia6) {
945 /* Record statistics for this interface address. */
946 IFA_STAT_INC(&ia6->ia_ifa, opackets, 1);
947 IFA_STAT_INC(&ia6->ia_ifa, obytes, m->m_pkthdr.len);
949 #ifdef IPSEC
950 /* clean ipsec history once it goes out of the node */
951 ipsec_delaux(m);
952 #endif
953 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
954 goto done;
958 * try to fragment the packet. case 1-b and 3
960 if (mtu < IPV6_MMTU) {
962 * note that path MTU is never less than IPV6_MMTU
963 * (see icmp6_input).
965 error = EMSGSIZE;
966 in6_ifstat_inc(ifp, ifs6_out_fragfail);
967 goto bad;
968 } else if (ip6->ip6_plen == 0) {
969 /* jumbo payload cannot be fragmented */
970 error = EMSGSIZE;
971 in6_ifstat_inc(ifp, ifs6_out_fragfail);
972 goto bad;
973 } else {
974 struct mbuf **mnext, *m_frgpart;
975 struct ip6_frag *ip6f;
976 u_int32_t id = htonl(ip6_id++);
977 u_char nextproto;
980 * Too large for the destination or interface;
981 * fragment if possible.
982 * Must be able to put at least 8 bytes per fragment.
984 hlen = unfragpartlen;
985 if (mtu > IPV6_MAXPACKET)
986 mtu = IPV6_MAXPACKET;
988 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
989 if (len < 8) {
990 error = EMSGSIZE;
991 in6_ifstat_inc(ifp, ifs6_out_fragfail);
992 goto bad;
995 mnext = &m->m_nextpkt;
998 * Change the next header field of the last header in the
999 * unfragmentable part.
1001 if (exthdrs.ip6e_rthdr) {
1002 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1003 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1004 } else if (exthdrs.ip6e_dest1) {
1005 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1006 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1007 } else if (exthdrs.ip6e_hbh) {
1008 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1009 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1010 } else {
1011 nextproto = ip6->ip6_nxt;
1012 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1016 * Loop through length of segment after first fragment,
1017 * make new header and copy data of each part and link onto
1018 * chain.
1020 m0 = m;
1021 for (off = hlen; off < tlen; off += len) {
1022 MGETHDR(m, M_NOWAIT, MT_HEADER);
1023 if (!m) {
1024 error = ENOBUFS;
1025 ip6stat.ip6s_odropped++;
1026 goto sendorfree;
1028 m->m_pkthdr.rcvif = NULL;
1029 m->m_flags = m0->m_flags & M_COPYFLAGS;
1030 *mnext = m;
1031 mnext = &m->m_nextpkt;
1032 m->m_data += max_linkhdr;
1033 mhip6 = mtod(m, struct ip6_hdr *);
1034 *mhip6 = *ip6;
1035 m->m_len = sizeof(*mhip6);
1036 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1037 if (error) {
1038 ip6stat.ip6s_odropped++;
1039 goto sendorfree;
1041 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1042 if (off + len >= tlen)
1043 len = tlen - off;
1044 else
1045 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1046 mhip6->ip6_plen = htons((u_short)(len + hlen +
1047 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
1048 if ((m_frgpart = m_copy(m0, off, len)) == NULL) {
1049 error = ENOBUFS;
1050 ip6stat.ip6s_odropped++;
1051 goto sendorfree;
1053 m_cat(m, m_frgpart);
1054 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
1055 m->m_pkthdr.rcvif = NULL;
1056 ip6f->ip6f_reserved = 0;
1057 ip6f->ip6f_ident = id;
1058 ip6f->ip6f_nxt = nextproto;
1059 ip6stat.ip6s_ofragments++;
1060 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1063 in6_ifstat_inc(ifp, ifs6_out_fragok);
1067 * Remove leading garbages.
1069 sendorfree:
1070 m = m0->m_nextpkt;
1071 m0->m_nextpkt = NULL;
1072 m_freem(m0);
1073 for (m0 = m; m; m = m0) {
1074 m0 = m->m_nextpkt;
1075 m->m_nextpkt = NULL;
1076 if (error == 0) {
1077 /* Record statistics for this interface address. */
1078 if (ia) {
1079 IFA_STAT_INC(&ia->ia_ifa, opackets, 1);
1080 IFA_STAT_INC(&ia->ia_ifa, obytes,
1081 m->m_pkthdr.len);
1083 #ifdef IPSEC
1084 /* clean ipsec history once it goes out of the node */
1085 ipsec_delaux(m);
1086 #endif
1087 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
1088 } else
1089 m_freem(m);
1092 if (error == 0)
1093 ip6stat.ip6s_fragmented++;
1095 done:
1096 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
1097 RTFREE(ro->ro_rt);
1098 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
1099 RTFREE(ro_pmtu->ro_rt);
1102 #ifdef IPSEC
1103 if (sp != NULL)
1104 key_freesp(sp);
1105 #endif
1106 #ifdef FAST_IPSEC
1107 if (sp != NULL)
1108 KEY_FREESP(&sp);
1109 #endif
1111 return (error);
1113 freehdrs:
1114 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
1115 m_freem(exthdrs.ip6e_dest1);
1116 m_freem(exthdrs.ip6e_rthdr);
1117 m_freem(exthdrs.ip6e_dest2);
1118 /* FALLTHROUGH */
1119 bad:
1120 m_freem(m);
1121 goto done;
1124 static int
1125 copyexthdr(void *h, struct mbuf **mp)
1127 struct ip6_ext *hdr = h;
1128 int hlen;
1129 struct mbuf *m;
1131 if (hdr == NULL)
1132 return 0;
1134 hlen = (hdr->ip6e_len + 1) * 8;
1135 if (hlen > MCLBYTES)
1136 return ENOBUFS; /* XXX */
1138 m = m_getb(hlen, M_NOWAIT, MT_DATA, 0);
1139 if (!m)
1140 return ENOBUFS;
1141 m->m_len = hlen;
1143 bcopy(hdr, mtod(m, caddr_t), hlen);
1145 *mp = m;
1146 return 0;
1150 * Insert jumbo payload option.
1152 static int
1153 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1155 struct mbuf *mopt;
1156 u_char *optbuf;
1157 u_int32_t v;
1159 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1162 * If there is no hop-by-hop options header, allocate new one.
1163 * If there is one but it doesn't have enough space to store the
1164 * jumbo payload option, allocate a cluster to store the whole options.
1165 * Otherwise, use it to store the options.
1167 if (exthdrs->ip6e_hbh == NULL) {
1168 MGET(mopt, M_NOWAIT, MT_DATA);
1169 if (mopt == NULL)
1170 return (ENOBUFS);
1171 mopt->m_len = JUMBOOPTLEN;
1172 optbuf = mtod(mopt, u_char *);
1173 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1174 exthdrs->ip6e_hbh = mopt;
1175 } else {
1176 struct ip6_hbh *hbh;
1178 mopt = exthdrs->ip6e_hbh;
1179 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1181 * XXX assumption:
1182 * - exthdrs->ip6e_hbh is not referenced from places
1183 * other than exthdrs.
1184 * - exthdrs->ip6e_hbh is not an mbuf chain.
1186 int oldoptlen = mopt->m_len;
1187 struct mbuf *n;
1190 * XXX: give up if the whole (new) hbh header does
1191 * not fit even in an mbuf cluster.
1193 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1194 return (ENOBUFS);
1197 * As a consequence, we must always prepare a cluster
1198 * at this point.
1200 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1201 if (!n)
1202 return (ENOBUFS);
1203 n->m_len = oldoptlen + JUMBOOPTLEN;
1204 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), oldoptlen);
1205 optbuf = mtod(n, caddr_t) + oldoptlen;
1206 m_freem(mopt);
1207 mopt = exthdrs->ip6e_hbh = n;
1208 } else {
1209 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1210 mopt->m_len += JUMBOOPTLEN;
1212 optbuf[0] = IP6OPT_PADN;
1213 optbuf[1] = 1;
1216 * Adjust the header length according to the pad and
1217 * the jumbo payload option.
1219 hbh = mtod(mopt, struct ip6_hbh *);
1220 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1223 /* fill in the option. */
1224 optbuf[2] = IP6OPT_JUMBO;
1225 optbuf[3] = 4;
1226 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1227 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1229 /* finally, adjust the packet header length */
1230 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1232 return (0);
1233 #undef JUMBOOPTLEN
1237 * Insert fragment header and copy unfragmentable header portions.
1239 static int
1240 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1241 struct ip6_frag **frghdrp)
1243 struct mbuf *n, *mlast;
1245 if (hlen > sizeof(struct ip6_hdr)) {
1246 n = m_copym(m0, sizeof(struct ip6_hdr),
1247 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1248 if (n == NULL)
1249 return (ENOBUFS);
1250 m->m_next = n;
1251 } else
1252 n = m;
1254 /* Search for the last mbuf of unfragmentable part. */
1255 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1258 if (!(mlast->m_flags & M_EXT) &&
1259 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1260 /* use the trailing space of the last mbuf for the fragment hdr */
1261 *frghdrp = (struct ip6_frag *)
1262 (mtod(mlast, caddr_t) + mlast->m_len);
1263 mlast->m_len += sizeof(struct ip6_frag);
1264 m->m_pkthdr.len += sizeof(struct ip6_frag);
1265 } else {
1266 /* allocate a new mbuf for the fragment header */
1267 struct mbuf *mfrg;
1269 MGET(mfrg, M_NOWAIT, MT_DATA);
1270 if (mfrg == NULL)
1271 return (ENOBUFS);
1272 mfrg->m_len = sizeof(struct ip6_frag);
1273 *frghdrp = mtod(mfrg, struct ip6_frag *);
1274 mlast->m_next = mfrg;
1277 return (0);
1280 static int
1281 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1282 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1283 int *alwaysfragp)
1285 u_int32_t mtu = 0;
1286 int alwaysfrag = 0;
1287 int error = 0;
1289 if (ro_pmtu != ro) {
1290 /* The first hop and the final destination may differ. */
1291 struct sockaddr_in6 *sa6_dst =
1292 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1293 if (ro_pmtu->ro_rt &&
1294 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1295 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1296 RTFREE(ro_pmtu->ro_rt);
1297 ro_pmtu->ro_rt = NULL;
1299 if (ro_pmtu->ro_rt == NULL) {
1300 bzero(sa6_dst, sizeof(*sa6_dst));
1301 sa6_dst->sin6_family = AF_INET6;
1302 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1303 sa6_dst->sin6_addr = *dst;
1305 rtalloc((struct route *)ro_pmtu);
1308 if (ro_pmtu->ro_rt) {
1309 u_int32_t ifmtu;
1311 if (ifp == NULL)
1312 ifp = ro_pmtu->ro_rt->rt_ifp;
1313 ifmtu = IN6_LINKMTU(ifp);
1314 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1315 if (mtu == 0) {
1316 mtu = ifmtu;
1317 } else if (mtu < IPV6_MMTU) {
1319 * RFC2460 section 5, last paragraph:
1320 * if we record ICMPv6 too big message with
1321 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1322 * or smaller, with framgent header attached.
1323 * (fragment header is needed regardless from the
1324 * packet size, for translators to identify packets)
1326 alwaysfrag = 1;
1327 mtu = IPV6_MMTU;
1328 } else if (mtu > ifmtu) {
1330 * The MTU on the route is larger than the MTU on
1331 * the interface! This shouldn't happen, unless the
1332 * MTU of the interface has been changed after the
1333 * interface was brought up. Change the MTU in the
1334 * route to match the interface MTU (as long as the
1335 * field isn't locked).
1337 mtu = ifmtu;
1338 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1340 } else if (ifp) {
1341 mtu = IN6_LINKMTU(ifp);
1342 } else {
1343 error = EHOSTUNREACH; /* XXX */
1346 *mtup = mtu;
1347 if (alwaysfragp)
1348 *alwaysfragp = alwaysfrag;
1349 return (error);
1353 * IP6 socket option processing.
1355 void
1356 ip6_ctloutput_dispatch(netmsg_t msg)
1358 int error;
1360 error = ip6_ctloutput(msg->ctloutput.base.nm_so,
1361 msg->ctloutput.nm_sopt);
1362 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
1366 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1368 int optdatalen,uproto;
1369 int privileged;
1370 struct inpcb *in6p = so->so_pcb;
1371 void *optdata;
1372 int error, optval;
1373 int level, op, optname;
1374 int optlen;
1375 struct thread *td;
1377 if (sopt) {
1378 level = sopt->sopt_level;
1379 op = sopt->sopt_dir;
1380 optname = sopt->sopt_name;
1381 optlen = sopt->sopt_valsize;
1382 td = sopt->sopt_td;
1383 } else {
1384 panic("ip6_ctloutput: arg soopt is NULL");
1385 /* NOT REACHED */
1386 td = NULL;
1388 error = optval = 0;
1390 uproto = (int)so->so_proto->pr_protocol;
1391 privileged = (td == NULL || priv_check(td, PRIV_ROOT)) ? 0 : 1;
1393 if (level == IPPROTO_IPV6) {
1394 switch (op) {
1396 case SOPT_SET:
1397 switch (optname) {
1398 case IPV6_2292PKTOPTIONS:
1399 #ifdef IPV6_PKTOPTIONS
1400 case IPV6_PKTOPTIONS:
1401 #endif
1403 struct mbuf *m;
1405 error = soopt_getm(sopt, &m); /* XXX */
1406 if (error != 0)
1407 break;
1408 soopt_to_mbuf(sopt, m); /* XXX */
1409 error = ip6_pcbopts(&in6p->in6p_outputopts,
1410 m, so, sopt);
1411 m_freem(m); /* XXX */
1412 break;
1416 * Use of some Hop-by-Hop options or some
1417 * Destination options, might require special
1418 * privilege. That is, normal applications
1419 * (without special privilege) might be forbidden
1420 * from setting certain options in outgoing packets,
1421 * and might never see certain options in received
1422 * packets. [RFC 2292 Section 6]
1423 * KAME specific note:
1424 * KAME prevents non-privileged users from sending or
1425 * receiving ANY hbh/dst options in order to avoid
1426 * overhead of parsing options in the kernel.
1428 case IPV6_RECVHOPOPTS:
1429 case IPV6_RECVDSTOPTS:
1430 case IPV6_RECVRTHDRDSTOPTS:
1431 if (!privileged)
1432 return (EPERM);
1433 case IPV6_RECVPKTINFO:
1434 case IPV6_RECVHOPLIMIT:
1435 case IPV6_RECVRTHDR:
1436 case IPV6_RECVPATHMTU:
1437 case IPV6_RECVTCLASS:
1438 case IPV6_AUTOFLOWLABEL:
1439 case IPV6_HOPLIMIT:
1440 /* FALLTHROUGH */
1441 case IPV6_UNICAST_HOPS:
1442 case IPV6_FAITH:
1444 case IPV6_V6ONLY:
1445 if (optlen != sizeof(int)) {
1446 error = EINVAL;
1447 break;
1449 error = soopt_to_kbuf(sopt, &optval,
1450 sizeof optval, sizeof optval);
1451 if (error)
1452 break;
1453 switch (optname) {
1455 case IPV6_UNICAST_HOPS:
1456 if (optval < -1 || optval >= 256)
1457 error = EINVAL;
1458 else {
1459 /* -1 = kernel default */
1460 in6p->in6p_hops = optval;
1462 break;
1463 #define OPTSET(bit) \
1464 do { \
1465 if (optval) \
1466 in6p->in6p_flags |= (bit); \
1467 else \
1468 in6p->in6p_flags &= ~(bit); \
1469 } while (0)
1470 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1472 * Although changed to RFC3542, It's better to also support RFC2292 API
1474 #define OPTSET2292(bit) \
1475 do { \
1476 in6p->in6p_flags |= IN6P_RFC2292; \
1477 if (optval) \
1478 in6p->in6p_flags |= (bit); \
1479 else \
1480 in6p->in6p_flags &= ~(bit); \
1481 } while (/*CONSTCOND*/ 0)
1483 case IPV6_RECVPKTINFO:
1484 /* cannot mix with RFC2292 */
1485 if (OPTBIT(IN6P_RFC2292)) {
1486 error = EINVAL;
1487 break;
1489 OPTSET(IN6P_PKTINFO);
1490 break;
1492 case IPV6_HOPLIMIT:
1494 struct ip6_pktopts **optp;
1496 /* cannot mix with RFC2292 */
1497 if (OPTBIT(IN6P_RFC2292)) {
1498 error = EINVAL;
1499 break;
1501 optp = &in6p->in6p_outputopts;
1502 error = ip6_pcbopt(IPV6_HOPLIMIT,
1503 (u_char *)&optval, sizeof(optval),
1504 optp, uproto);
1505 break;
1508 case IPV6_RECVHOPLIMIT:
1509 /* cannot mix with RFC2292 */
1510 if (OPTBIT(IN6P_RFC2292)) {
1511 error = EINVAL;
1512 break;
1514 OPTSET(IN6P_HOPLIMIT);
1515 break;
1517 case IPV6_RECVHOPOPTS:
1518 /* cannot mix with RFC2292 */
1519 if (OPTBIT(IN6P_RFC2292)) {
1520 error = EINVAL;
1521 break;
1523 OPTSET(IN6P_HOPOPTS);
1524 break;
1526 case IPV6_RECVDSTOPTS:
1527 /* cannot mix with RFC2292 */
1528 if (OPTBIT(IN6P_RFC2292)) {
1529 error = EINVAL;
1530 break;
1532 OPTSET(IN6P_DSTOPTS);
1533 break;
1535 case IPV6_RECVRTHDRDSTOPTS:
1536 /* cannot mix with RFC2292 */
1537 if (OPTBIT(IN6P_RFC2292)) {
1538 error = EINVAL;
1539 break;
1541 OPTSET(IN6P_RTHDRDSTOPTS);
1542 break;
1544 case IPV6_RECVRTHDR:
1545 /* cannot mix with RFC2292 */
1546 if (OPTBIT(IN6P_RFC2292)) {
1547 error = EINVAL;
1548 break;
1550 OPTSET(IN6P_RTHDR);
1551 break;
1553 case IPV6_RECVPATHMTU:
1555 * We ignore this option for TCP
1556 * sockets.
1557 * (RFC3542 leaves this case
1558 * unspecified.)
1560 if (uproto != IPPROTO_TCP)
1561 OPTSET(IN6P_MTU);
1562 break;
1564 case IPV6_RECVTCLASS:
1565 /* cannot mix with RFC2292 XXX */
1566 if (OPTBIT(IN6P_RFC2292)) {
1567 error = EINVAL;
1568 break;
1570 OPTSET(IN6P_TCLASS);
1571 break;
1573 case IPV6_AUTOFLOWLABEL:
1574 OPTSET(IN6P_AUTOFLOWLABEL);
1575 break;
1577 case IPV6_FAITH:
1578 OPTSET(IN6P_FAITH);
1579 break;
1581 case IPV6_V6ONLY:
1583 * make setsockopt(IPV6_V6ONLY)
1584 * available only prior to bind(2).
1586 if (in6p->in6p_lport ||
1587 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1589 error = EINVAL;
1590 break;
1592 if (!optval) {
1593 /* Don't allow v4-mapped */
1594 error = EOPNOTSUPP;
1596 break;
1598 break;
1600 case IPV6_TCLASS:
1601 case IPV6_DONTFRAG:
1602 case IPV6_USE_MIN_MTU:
1603 case IPV6_PREFER_TEMPADDR:
1604 if (optlen != sizeof(optval)) {
1605 error = EINVAL;
1606 break;
1608 error = soopt_to_kbuf(sopt, &optval,
1609 sizeof optval, sizeof optval);
1610 if (error)
1611 break;
1613 struct ip6_pktopts **optp;
1614 optp = &in6p->in6p_outputopts;
1615 error = ip6_pcbopt(optname,
1616 (u_char *)&optval, sizeof(optval),
1617 optp, uproto);
1618 break;
1621 case IPV6_2292PKTINFO:
1622 case IPV6_2292HOPLIMIT:
1623 case IPV6_2292HOPOPTS:
1624 case IPV6_2292DSTOPTS:
1625 case IPV6_2292RTHDR:
1626 /* RFC 2292 */
1627 if (optlen != sizeof(int)) {
1628 error = EINVAL;
1629 break;
1631 error = soopt_to_kbuf(sopt, &optval,
1632 sizeof optval, sizeof optval);
1633 if (error)
1634 break;
1635 switch (optname) {
1636 case IPV6_2292PKTINFO:
1637 OPTSET2292(IN6P_PKTINFO);
1638 break;
1639 case IPV6_2292HOPLIMIT:
1640 OPTSET2292(IN6P_HOPLIMIT);
1641 break;
1642 case IPV6_2292HOPOPTS:
1644 * Check super-user privilege.
1645 * See comments for IPV6_RECVHOPOPTS.
1647 if (!privileged)
1648 return (EPERM);
1649 OPTSET2292(IN6P_HOPOPTS);
1650 break;
1651 case IPV6_2292DSTOPTS:
1652 if (!privileged)
1653 return (EPERM);
1654 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1655 break;
1656 case IPV6_2292RTHDR:
1657 OPTSET2292(IN6P_RTHDR);
1658 break;
1660 break;
1662 case IPV6_PKTINFO:
1663 case IPV6_HOPOPTS:
1664 case IPV6_RTHDR:
1665 case IPV6_DSTOPTS:
1666 case IPV6_RTHDRDSTOPTS:
1667 case IPV6_NEXTHOP:
1670 * New advanced API (RFC3542)
1672 u_char *optbuf;
1673 u_char optbuf_storage[MCLBYTES];
1674 int optlen;
1675 struct ip6_pktopts **optp;
1677 /* cannot mix with RFC2292 */
1678 if (OPTBIT(IN6P_RFC2292)) {
1679 error = EINVAL;
1680 break;
1684 * We only ensure valsize is not too large
1685 * here. Further validation will be done
1686 * later.
1688 error = soopt_to_kbuf(sopt, optbuf_storage,
1689 sizeof(optbuf_storage), 0);
1690 if (error)
1691 break;
1692 optlen = sopt->sopt_valsize;
1693 optbuf = optbuf_storage;
1694 optp = &in6p->in6p_outputopts;
1695 error = ip6_pcbopt(optname, optbuf, optlen,
1696 optp, uproto);
1697 break;
1699 #undef OPTSET
1701 case IPV6_MULTICAST_IF:
1702 case IPV6_MULTICAST_HOPS:
1703 case IPV6_MULTICAST_LOOP:
1704 case IPV6_JOIN_GROUP:
1705 case IPV6_LEAVE_GROUP:
1707 struct mbuf *m;
1709 if (sopt->sopt_valsize > MLEN) {
1710 error = EMSGSIZE;
1711 break;
1713 /* XXX */
1714 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_HEADER);
1715 if (m == NULL) {
1716 error = ENOBUFS;
1717 break;
1719 m->m_len = sopt->sopt_valsize;
1720 error = soopt_to_kbuf(sopt, mtod(m, char *),
1721 m->m_len, m->m_len);
1722 error = ip6_setmoptions(sopt->sopt_name,
1723 &in6p->in6p_moptions,
1725 m_free(m);
1727 break;
1729 case IPV6_PORTRANGE:
1730 error = soopt_to_kbuf(sopt, &optval,
1731 sizeof optval, sizeof optval);
1732 if (error)
1733 break;
1735 switch (optval) {
1736 case IPV6_PORTRANGE_DEFAULT:
1737 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1738 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1739 break;
1741 case IPV6_PORTRANGE_HIGH:
1742 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1743 in6p->in6p_flags |= IN6P_HIGHPORT;
1744 break;
1746 case IPV6_PORTRANGE_LOW:
1747 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1748 in6p->in6p_flags |= IN6P_LOWPORT;
1749 break;
1751 default:
1752 error = EINVAL;
1753 break;
1755 break;
1757 #if defined(IPSEC) || defined(FAST_IPSEC)
1758 case IPV6_IPSEC_POLICY:
1760 caddr_t req = NULL;
1761 size_t len = 0;
1762 struct mbuf *m;
1764 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1765 break;
1766 soopt_to_mbuf(sopt, m); /* XXX */
1767 if (m) {
1768 req = mtod(m, caddr_t);
1769 len = m->m_len;
1771 error = ipsec6_set_policy(in6p, optname, req,
1772 len, privileged);
1773 m_freem(m);
1775 break;
1776 #endif /* KAME IPSEC */
1778 case IPV6_FW_ADD:
1779 case IPV6_FW_DEL:
1780 case IPV6_FW_FLUSH:
1781 case IPV6_FW_ZERO:
1783 struct mbuf *m;
1784 struct mbuf **mp = &m;
1786 if (ip6_fw_ctl_ptr == NULL)
1787 return EINVAL;
1788 /* XXX */
1789 if ((error = soopt_getm(sopt, &m)) != 0)
1790 break;
1791 /* XXX */
1792 soopt_to_mbuf(sopt, m);
1793 error = (*ip6_fw_ctl_ptr)(optname, mp);
1794 m = *mp;
1796 break;
1798 default:
1799 error = ENOPROTOOPT;
1800 break;
1802 break;
1804 case SOPT_GET:
1805 switch (optname) {
1806 case IPV6_2292PKTOPTIONS:
1807 #ifdef IPV6_PKTOPTIONS
1808 case IPV6_PKTOPTIONS:
1809 #endif
1811 * RFC3542 (effectively) deprecated the
1812 * semantics of the 2292-style pktoptions.
1813 * Since it was not reliable in nature (i.e.,
1814 * applications had to expect the lack of some
1815 * information after all), it would make sense
1816 * to simplify this part by always returning
1817 * empty data.
1819 if (in6p->in6p_options) {
1820 struct mbuf *m;
1821 m = m_copym(in6p->in6p_options,
1822 0, M_COPYALL, M_WAITOK);
1823 error = soopt_from_mbuf(sopt, m);
1824 if (error == 0)
1825 m_freem(m);
1826 } else
1827 sopt->sopt_valsize = 0;
1828 break;
1830 case IPV6_RECVHOPOPTS:
1831 case IPV6_RECVDSTOPTS:
1832 case IPV6_RECVRTHDRDSTOPTS:
1833 case IPV6_UNICAST_HOPS:
1834 case IPV6_RECVPKTINFO:
1835 case IPV6_RECVHOPLIMIT:
1836 case IPV6_RECVRTHDR:
1837 case IPV6_RECVPATHMTU:
1838 case IPV6_RECVTCLASS:
1839 case IPV6_AUTOFLOWLABEL:
1840 case IPV6_FAITH:
1841 case IPV6_V6ONLY:
1842 case IPV6_PORTRANGE:
1843 switch (optname) {
1845 case IPV6_RECVHOPOPTS:
1846 optval = OPTBIT(IN6P_HOPOPTS);
1847 break;
1849 case IPV6_RECVDSTOPTS:
1850 optval = OPTBIT(IN6P_DSTOPTS);
1851 break;
1853 case IPV6_RECVRTHDRDSTOPTS:
1854 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1855 break;
1857 case IPV6_RECVPKTINFO:
1858 optval = OPTBIT(IN6P_PKTINFO);
1859 break;
1861 case IPV6_RECVHOPLIMIT:
1862 optval = OPTBIT(IN6P_HOPLIMIT);
1863 break;
1865 case IPV6_RECVRTHDR:
1866 optval = OPTBIT(IN6P_RTHDR);
1867 break;
1869 case IPV6_RECVPATHMTU:
1870 optval = OPTBIT(IN6P_MTU);
1871 break;
1873 case IPV6_RECVTCLASS:
1874 optval = OPTBIT(IN6P_TCLASS);
1875 break;
1877 case IPV6_AUTOFLOWLABEL:
1878 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1879 break;
1882 case IPV6_UNICAST_HOPS:
1883 optval = in6p->in6p_hops;
1884 break;
1886 case IPV6_FAITH:
1887 optval = OPTBIT(IN6P_FAITH);
1888 break;
1890 case IPV6_V6ONLY:
1891 optval = 1;
1892 break;
1894 case IPV6_PORTRANGE:
1896 int flags;
1897 flags = in6p->in6p_flags;
1898 if (flags & IN6P_HIGHPORT)
1899 optval = IPV6_PORTRANGE_HIGH;
1900 else if (flags & IN6P_LOWPORT)
1901 optval = IPV6_PORTRANGE_LOW;
1902 else
1903 optval = 0;
1904 break;
1907 soopt_from_kbuf(sopt, &optval,
1908 sizeof optval);
1909 break;
1911 case IPV6_PATHMTU:
1913 u_long pmtu = 0;
1914 struct ip6_mtuinfo mtuinfo;
1915 struct route_in6 sro;
1917 bzero(&sro, sizeof(sro));
1919 if (!(so->so_state & SS_ISCONNECTED))
1920 return (ENOTCONN);
1922 * XXX: we dot not consider the case of source
1923 * routing, or optional information to specify
1924 * the outgoing interface.
1926 error = ip6_getpmtu(&sro, NULL, NULL,
1927 &in6p->in6p_faddr, &pmtu, NULL);
1928 if (sro.ro_rt)
1929 RTFREE(sro.ro_rt);
1930 if (error)
1931 break;
1932 if (pmtu > IPV6_MAXPACKET)
1933 pmtu = IPV6_MAXPACKET;
1935 bzero(&mtuinfo, sizeof(mtuinfo));
1936 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1937 optdata = (void *)&mtuinfo;
1938 optdatalen = sizeof(mtuinfo);
1939 soopt_from_kbuf(sopt, optdata,
1940 optdatalen);
1941 break;
1944 case IPV6_2292PKTINFO:
1945 case IPV6_2292HOPLIMIT:
1946 case IPV6_2292HOPOPTS:
1947 case IPV6_2292RTHDR:
1948 case IPV6_2292DSTOPTS:
1949 if (optname == IPV6_2292HOPOPTS ||
1950 optname == IPV6_2292DSTOPTS ||
1951 !privileged)
1952 return (EPERM);
1953 switch (optname) {
1954 case IPV6_2292PKTINFO:
1955 optval = OPTBIT(IN6P_PKTINFO);
1956 break;
1957 case IPV6_2292HOPLIMIT:
1958 optval = OPTBIT(IN6P_HOPLIMIT);
1959 break;
1960 case IPV6_2292HOPOPTS:
1961 if (!privileged)
1962 return (EPERM);
1963 optval = OPTBIT(IN6P_HOPOPTS);
1964 break;
1965 case IPV6_2292RTHDR:
1966 optval = OPTBIT(IN6P_RTHDR);
1967 break;
1968 case IPV6_2292DSTOPTS:
1969 if (!privileged)
1970 return (EPERM);
1971 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1972 break;
1974 soopt_from_kbuf(sopt, &optval,
1975 sizeof optval);
1976 break;
1978 case IPV6_PKTINFO:
1979 case IPV6_HOPOPTS:
1980 case IPV6_RTHDR:
1981 case IPV6_DSTOPTS:
1982 case IPV6_RTHDRDSTOPTS:
1983 case IPV6_NEXTHOP:
1984 case IPV6_TCLASS:
1985 case IPV6_DONTFRAG:
1986 case IPV6_USE_MIN_MTU:
1987 case IPV6_PREFER_TEMPADDR:
1988 error = ip6_getpcbopt(in6p->in6p_outputopts,
1989 optname, sopt);
1990 break;
1992 case IPV6_MULTICAST_IF:
1993 case IPV6_MULTICAST_HOPS:
1994 case IPV6_MULTICAST_LOOP:
1995 case IPV6_JOIN_GROUP:
1996 case IPV6_LEAVE_GROUP:
1998 struct mbuf *m;
1999 error = ip6_getmoptions(sopt->sopt_name,
2000 in6p->in6p_moptions, &m);
2001 if (error == 0) {
2002 soopt_from_kbuf(sopt,
2003 mtod(m, char *), m->m_len);
2005 m_freem(m);
2007 break;
2009 #if defined(IPSEC) || defined(FAST_IPSEC)
2010 case IPV6_IPSEC_POLICY:
2012 caddr_t req = NULL;
2013 size_t len = 0;
2014 struct mbuf *m = NULL;
2015 struct mbuf **mp = &m;
2017 error = soopt_getm(sopt, &m); /* XXX */
2018 if (error != 0)
2019 break;
2020 soopt_to_mbuf(sopt, m); /* XXX */
2021 if (m) {
2022 req = mtod(m, caddr_t);
2023 len = m->m_len;
2025 error = ipsec6_get_policy(in6p, req, len, mp);
2026 if (error == 0)
2027 error = soopt_from_mbuf(sopt, m);/*XXX*/
2028 if (error == 0 && m != NULL)
2029 m_freem(m);
2030 break;
2032 #endif /* KAME IPSEC */
2034 case IPV6_FW_GET:
2036 struct mbuf *m;
2037 struct mbuf **mp = &m;
2039 if (ip6_fw_ctl_ptr == NULL)
2041 return EINVAL;
2043 error = (*ip6_fw_ctl_ptr)(optname, mp);
2044 if (error == 0)
2045 error = soopt_from_mbuf(sopt, m); /* XXX */
2046 if (error == 0 && m != NULL)
2047 m_freem(m);
2049 break;
2051 default:
2052 error = ENOPROTOOPT;
2053 break;
2055 break;
2057 } else {
2058 error = EINVAL;
2060 return (error);
2064 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2066 int error = 0, optval, optlen;
2067 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2068 struct in6pcb *in6p = sotoin6pcb(so);
2069 int level, op, optname;
2071 if (sopt) {
2072 level = sopt->sopt_level;
2073 op = sopt->sopt_dir;
2074 optname = sopt->sopt_name;
2075 optlen = sopt->sopt_valsize;
2076 } else
2077 panic("ip6_raw_ctloutput: arg soopt is NULL");
2079 if (level != IPPROTO_IPV6) {
2080 return (EINVAL);
2083 switch (optname) {
2084 case IPV6_CHECKSUM:
2086 * For ICMPv6 sockets, no modification allowed for checksum
2087 * offset, permit "no change" values to help existing apps.
2089 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2090 * for an ICMPv6 socket will fail."
2091 * The current behavior does not meet RFC3542.
2093 switch (op) {
2094 case SOPT_SET:
2095 if (optlen != sizeof(int)) {
2096 error = EINVAL;
2097 break;
2099 error = soopt_to_kbuf(sopt, &optval,
2100 sizeof optval, sizeof optval);
2101 if (error)
2102 break;
2103 if ((optval % 2) != 0) {
2104 /* the API assumes even offset values */
2105 error = EINVAL;
2106 } else if (so->so_proto->pr_protocol ==
2107 IPPROTO_ICMPV6) {
2108 if (optval != icmp6off)
2109 error = EINVAL;
2110 } else
2111 in6p->in6p_cksum = optval;
2112 break;
2114 case SOPT_GET:
2115 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
2116 optval = icmp6off;
2117 else
2118 optval = in6p->in6p_cksum;
2120 soopt_from_kbuf(sopt, &optval, sizeof(optval));
2121 break;
2123 default:
2124 error = EINVAL;
2125 break;
2127 break;
2129 default:
2130 error = ENOPROTOOPT;
2131 break;
2134 return (error);
2138 * Set up IP6 options in pcb for insertion in output packets or
2139 * specifying behavior of outgoing packets.
2141 static int
2142 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2143 struct socket *so, struct sockopt *sopt)
2145 int priv = 0;
2146 struct ip6_pktopts *opt = *pktopt;
2147 int error = 0;
2149 /* turn off any old options. */
2150 if (opt) {
2151 #ifdef DIAGNOSTIC
2152 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2153 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2154 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2155 kprintf("ip6_pcbopts: all specified options are cleared.\n");
2156 #endif
2157 ip6_clearpktopts(opt, -1);
2158 } else
2159 opt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2160 *pktopt = NULL;
2162 if (!m || m->m_len == 0) {
2164 * Only turning off any previous options, regardless of
2165 * whether the opt is just created or given.
2167 kfree(opt, M_IP6OPT);
2168 return (0);
2171 /* set options specified by user. */
2172 if ((error = ip6_setpktoptions(m, opt, NULL, so->so_proto->pr_protocol, priv)) != 0) {
2173 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2174 kfree(opt, M_IP6OPT);
2175 return (error);
2177 *pktopt = opt;
2178 return (0);
2183 * Below three functions are introduced by merge to RFC3542
2186 static int
2187 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2189 void *optdata = NULL;
2190 int optdatalen = 0;
2191 struct ip6_ext *ip6e;
2192 int error = 0;
2193 struct in6_pktinfo null_pktinfo;
2194 int deftclass = 0, on;
2195 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2196 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2198 switch (optname) {
2199 case IPV6_PKTINFO:
2200 if (pktopt && pktopt->ip6po_pktinfo)
2201 optdata = (void *)pktopt->ip6po_pktinfo;
2202 else {
2203 /* XXX: we don't have to do this every time... */
2204 bzero(&null_pktinfo, sizeof(null_pktinfo));
2205 optdata = (void *)&null_pktinfo;
2207 optdatalen = sizeof(struct in6_pktinfo);
2208 break;
2209 case IPV6_TCLASS:
2210 if (pktopt && pktopt->ip6po_tclass >= 0)
2211 optdata = (void *)&pktopt->ip6po_tclass;
2212 else
2213 optdata = (void *)&deftclass;
2214 optdatalen = sizeof(int);
2215 break;
2216 case IPV6_HOPOPTS:
2217 if (pktopt && pktopt->ip6po_hbh) {
2218 optdata = (void *)pktopt->ip6po_hbh;
2219 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2220 optdatalen = (ip6e->ip6e_len + 1) << 3;
2222 break;
2223 case IPV6_RTHDR:
2224 if (pktopt && pktopt->ip6po_rthdr) {
2225 optdata = (void *)pktopt->ip6po_rthdr;
2226 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2227 optdatalen = (ip6e->ip6e_len + 1) << 3;
2229 break;
2230 case IPV6_RTHDRDSTOPTS:
2231 if (pktopt && pktopt->ip6po_dest1) {
2232 optdata = (void *)pktopt->ip6po_dest1;
2233 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2234 optdatalen = (ip6e->ip6e_len + 1) << 3;
2236 break;
2237 case IPV6_DSTOPTS:
2238 if (pktopt && pktopt->ip6po_dest2) {
2239 optdata = (void *)pktopt->ip6po_dest2;
2240 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2241 optdatalen = (ip6e->ip6e_len + 1) << 3;
2243 break;
2244 case IPV6_NEXTHOP:
2245 if (pktopt && pktopt->ip6po_nexthop) {
2246 optdata = (void *)pktopt->ip6po_nexthop;
2247 optdatalen = pktopt->ip6po_nexthop->sa_len;
2249 break;
2250 case IPV6_USE_MIN_MTU:
2251 if (pktopt)
2252 optdata = (void *)&pktopt->ip6po_minmtu;
2253 else
2254 optdata = (void *)&defminmtu;
2255 optdatalen = sizeof(int);
2256 break;
2257 case IPV6_DONTFRAG:
2258 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2259 on = 1;
2260 else
2261 on = 0;
2262 optdata = (void *)&on;
2263 optdatalen = sizeof(on);
2264 break;
2265 case IPV6_PREFER_TEMPADDR:
2266 if (pktopt)
2267 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2268 else
2269 optdata = (void *)&defpreftemp;
2270 optdatalen = sizeof(int);
2271 break;
2272 default: /* should not happen */
2273 #ifdef DIAGNOSTIC
2274 panic("ip6_getpcbopt: unexpected option");
2275 #endif
2276 return (ENOPROTOOPT);
2279 soopt_from_kbuf(sopt, optdata, optdatalen);
2281 return (error);
2285 * initialize ip6_pktopts. beware that there are non-zero default values in
2286 * the struct.
2289 static int
2290 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, int uproto)
2292 struct ip6_pktopts *opt;
2293 int priv =0;
2294 if (*pktopt == NULL) {
2295 *pktopt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2296 init_ip6pktopts(*pktopt);
2298 opt = *pktopt;
2300 return (ip6_setpktoption(optname, buf, len, opt, 1, 0, uproto, priv));
2304 * initialize ip6_pktopts. beware that there are non-zero default values in
2305 * the struct.
2307 void
2308 init_ip6pktopts(struct ip6_pktopts *opt)
2311 bzero(opt, sizeof(*opt));
2312 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2313 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2314 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2315 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2318 void
2319 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2321 if (pktopt == NULL)
2322 return;
2324 if (optname == -1 || optname == IPV6_PKTINFO) {
2325 if (pktopt->ip6po_pktinfo)
2326 kfree(pktopt->ip6po_pktinfo, M_IP6OPT);
2327 pktopt->ip6po_pktinfo = NULL;
2329 if (optname == -1 || optname == IPV6_HOPLIMIT)
2330 pktopt->ip6po_hlim = -1;
2331 if (optname == -1 || optname == IPV6_TCLASS)
2332 pktopt->ip6po_tclass = -1;
2333 if (optname == -1 || optname == IPV6_NEXTHOP) {
2334 if (pktopt->ip6po_nextroute.ro_rt) {
2335 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2336 pktopt->ip6po_nextroute.ro_rt = NULL;
2338 if (pktopt->ip6po_nexthop)
2339 kfree(pktopt->ip6po_nexthop, M_IP6OPT);
2340 pktopt->ip6po_nexthop = NULL;
2342 if (optname == -1 || optname == IPV6_HOPOPTS) {
2343 if (pktopt->ip6po_hbh)
2344 kfree(pktopt->ip6po_hbh, M_IP6OPT);
2345 pktopt->ip6po_hbh = NULL;
2347 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2348 if (pktopt->ip6po_dest1)
2349 kfree(pktopt->ip6po_dest1, M_IP6OPT);
2350 pktopt->ip6po_dest1 = NULL;
2352 if (optname == -1 || optname == IPV6_RTHDR) {
2353 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2354 kfree(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2355 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2356 if (pktopt->ip6po_route.ro_rt) {
2357 RTFREE(pktopt->ip6po_route.ro_rt);
2358 pktopt->ip6po_route.ro_rt = NULL;
2361 if (optname == -1 || optname == IPV6_DSTOPTS) {
2362 if (pktopt->ip6po_dest2)
2363 kfree(pktopt->ip6po_dest2, M_IP6OPT);
2364 pktopt->ip6po_dest2 = NULL;
2368 #define PKTOPT_EXTHDRCPY(type) \
2369 do {\
2370 if (src->type) {\
2371 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2372 dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
2373 if (dst->type == NULL)\
2374 goto bad;\
2375 bcopy(src->type, dst->type, hlen);\
2377 } while (0)
2379 struct ip6_pktopts *
2380 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2382 struct ip6_pktopts *dst;
2384 if (src == NULL) {
2385 kprintf("ip6_clearpktopts: invalid argument\n");
2386 return (NULL);
2389 dst = kmalloc(sizeof(*dst), M_IP6OPT, canwait | M_ZERO);
2390 if (dst == NULL)
2391 return (NULL);
2393 dst->ip6po_hlim = src->ip6po_hlim;
2394 if (src->ip6po_pktinfo) {
2395 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2396 M_IP6OPT, canwait);
2397 if (dst->ip6po_pktinfo == NULL)
2398 goto bad;
2399 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2401 if (src->ip6po_nexthop) {
2402 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2403 M_IP6OPT, canwait);
2404 if (dst->ip6po_nexthop == NULL)
2405 goto bad;
2406 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2407 src->ip6po_nexthop->sa_len);
2409 PKTOPT_EXTHDRCPY(ip6po_hbh);
2410 PKTOPT_EXTHDRCPY(ip6po_dest1);
2411 PKTOPT_EXTHDRCPY(ip6po_dest2);
2412 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2413 return (dst);
2415 bad:
2416 if (dst->ip6po_pktinfo) kfree(dst->ip6po_pktinfo, M_IP6OPT);
2417 if (dst->ip6po_nexthop) kfree(dst->ip6po_nexthop, M_IP6OPT);
2418 if (dst->ip6po_hbh) kfree(dst->ip6po_hbh, M_IP6OPT);
2419 if (dst->ip6po_dest1) kfree(dst->ip6po_dest1, M_IP6OPT);
2420 if (dst->ip6po_dest2) kfree(dst->ip6po_dest2, M_IP6OPT);
2421 if (dst->ip6po_rthdr) kfree(dst->ip6po_rthdr, M_IP6OPT);
2422 kfree(dst, M_IP6OPT);
2423 return (NULL);
2426 static int
2427 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2429 if (dst == NULL || src == NULL) {
2430 #ifdef DIAGNOSTIC
2431 kprintf("ip6_clearpktopts: invalid argument\n");
2432 #endif
2433 return (EINVAL);
2436 dst->ip6po_hlim = src->ip6po_hlim;
2437 dst->ip6po_tclass = src->ip6po_tclass;
2438 dst->ip6po_flags = src->ip6po_flags;
2439 if (src->ip6po_pktinfo) {
2440 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2441 M_IP6OPT, canwait);
2442 if (dst->ip6po_pktinfo == NULL)
2443 goto bad;
2444 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2446 if (src->ip6po_nexthop) {
2447 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2448 M_IP6OPT, canwait);
2449 if (dst->ip6po_nexthop == NULL)
2450 goto bad;
2451 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2452 src->ip6po_nexthop->sa_len);
2454 PKTOPT_EXTHDRCPY(ip6po_hbh);
2455 PKTOPT_EXTHDRCPY(ip6po_dest1);
2456 PKTOPT_EXTHDRCPY(ip6po_dest2);
2457 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2458 return (0);
2460 bad:
2461 ip6_clearpktopts(dst, -1);
2462 return (ENOBUFS);
2464 #undef PKTOPT_EXTHDRCPY
2466 void
2467 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2469 if (pktopt == NULL)
2470 return;
2472 ip6_clearpktopts(pktopt, -1);
2474 kfree(pktopt, M_IP6OPT);
2478 * Set the IP6 multicast options in response to user setsockopt().
2480 static int
2481 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2483 int error = 0;
2484 u_int loop, ifindex;
2485 struct ipv6_mreq *mreq;
2486 struct ifnet *ifp;
2487 struct ip6_moptions *im6o = *im6op;
2488 struct route_in6 ro;
2489 struct sockaddr_in6 *dst;
2490 struct in6_multi_mship *imm;
2491 struct thread *td = curthread;
2493 if (im6o == NULL) {
2495 * No multicast option buffer attached to the pcb;
2496 * allocate one and initialize to default values.
2498 im6o = (struct ip6_moptions *)
2499 kmalloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2501 *im6op = im6o;
2502 im6o->im6o_multicast_ifp = NULL;
2503 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2504 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2505 LIST_INIT(&im6o->im6o_memberships);
2508 switch (optname) {
2510 case IPV6_MULTICAST_IF:
2512 * Select the interface for outgoing multicast packets.
2514 if (m == NULL || m->m_len != sizeof(u_int)) {
2515 error = EINVAL;
2516 break;
2518 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2519 if (ifindex < 0 || if_index < ifindex) {
2520 error = ENXIO; /* XXX EINVAL? */
2521 break;
2523 ifp = ifindex2ifnet[ifindex];
2524 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2525 error = EADDRNOTAVAIL;
2526 break;
2528 im6o->im6o_multicast_ifp = ifp;
2529 break;
2531 case IPV6_MULTICAST_HOPS:
2534 * Set the IP6 hoplimit for outgoing multicast packets.
2536 int optval;
2537 if (m == NULL || m->m_len != sizeof(int)) {
2538 error = EINVAL;
2539 break;
2541 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2542 if (optval < -1 || optval >= 256)
2543 error = EINVAL;
2544 else if (optval == -1)
2545 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2546 else
2547 im6o->im6o_multicast_hlim = optval;
2548 break;
2551 case IPV6_MULTICAST_LOOP:
2553 * Set the loopback flag for outgoing multicast packets.
2554 * Must be zero or one.
2556 if (m == NULL || m->m_len != sizeof(u_int)) {
2557 error = EINVAL;
2558 break;
2560 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2561 if (loop > 1) {
2562 error = EINVAL;
2563 break;
2565 im6o->im6o_multicast_loop = loop;
2566 break;
2568 case IPV6_JOIN_GROUP:
2570 * Add a multicast group membership.
2571 * Group must be a valid IP6 multicast address.
2573 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2574 error = EINVAL;
2575 break;
2577 mreq = mtod(m, struct ipv6_mreq *);
2578 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2580 * We use the unspecified address to specify to accept
2581 * all multicast addresses. Only super user is allowed
2582 * to do this.
2584 if (priv_check(td, PRIV_ROOT)) {
2585 error = EACCES;
2586 break;
2588 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2589 error = EINVAL;
2590 break;
2594 * If the interface is specified, validate it.
2596 if (mreq->ipv6mr_interface < 0
2597 || if_index < mreq->ipv6mr_interface) {
2598 error = ENXIO; /* XXX EINVAL? */
2599 break;
2602 * If no interface was explicitly specified, choose an
2603 * appropriate one according to the given multicast address.
2605 if (mreq->ipv6mr_interface == 0) {
2607 * If the multicast address is in node-local scope,
2608 * the interface should be a loopback interface.
2609 * Otherwise, look up the routing table for the
2610 * address, and choose the outgoing interface.
2611 * XXX: is it a good approach?
2613 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) {
2614 ifp = &loif[0];
2615 } else {
2616 ro.ro_rt = NULL;
2617 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2618 bzero(dst, sizeof(*dst));
2619 dst->sin6_len = sizeof(struct sockaddr_in6);
2620 dst->sin6_family = AF_INET6;
2621 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2622 rtalloc((struct route *)&ro);
2623 if (ro.ro_rt == NULL) {
2624 error = EADDRNOTAVAIL;
2625 break;
2627 ifp = ro.ro_rt->rt_ifp;
2628 rtfree(ro.ro_rt);
2630 } else
2631 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2634 * See if we found an interface, and confirm that it
2635 * supports multicast
2637 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2638 error = EADDRNOTAVAIL;
2639 break;
2642 * Put interface index into the multicast address,
2643 * if the address has link-local scope.
2645 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2646 mreq->ipv6mr_multiaddr.s6_addr16[1]
2647 = htons(mreq->ipv6mr_interface);
2650 * See if the membership already exists.
2652 for (imm = im6o->im6o_memberships.lh_first;
2653 imm != NULL; imm = imm->i6mm_chain.le_next)
2654 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2655 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2656 &mreq->ipv6mr_multiaddr))
2657 break;
2658 if (imm != NULL) {
2659 error = EADDRINUSE;
2660 break;
2663 * Everything looks good; add a new record to the multicast
2664 * address list for the given interface.
2666 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2667 if ((imm->i6mm_maddr =
2668 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2669 kfree(imm, M_IPMADDR);
2670 break;
2672 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2673 break;
2675 case IPV6_LEAVE_GROUP:
2677 * Drop a multicast group membership.
2678 * Group must be a valid IP6 multicast address.
2680 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2681 error = EINVAL;
2682 break;
2684 mreq = mtod(m, struct ipv6_mreq *);
2685 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2686 if (priv_check(td, PRIV_ROOT)) {
2687 error = EACCES;
2688 break;
2690 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2691 error = EINVAL;
2692 break;
2695 * If an interface address was specified, get a pointer
2696 * to its ifnet structure.
2698 if (mreq->ipv6mr_interface < 0
2699 || if_index < mreq->ipv6mr_interface) {
2700 error = ENXIO; /* XXX EINVAL? */
2701 break;
2703 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2705 * Put interface index into the multicast address,
2706 * if the address has link-local scope.
2708 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2709 mreq->ipv6mr_multiaddr.s6_addr16[1]
2710 = htons(mreq->ipv6mr_interface);
2714 * Find the membership in the membership list.
2716 for (imm = im6o->im6o_memberships.lh_first;
2717 imm != NULL; imm = imm->i6mm_chain.le_next) {
2718 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2719 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2720 &mreq->ipv6mr_multiaddr))
2721 break;
2723 if (imm == NULL) {
2724 /* Unable to resolve interface */
2725 error = EADDRNOTAVAIL;
2726 break;
2729 * Give up the multicast address record to which the
2730 * membership points.
2732 LIST_REMOVE(imm, i6mm_chain);
2733 in6_delmulti(imm->i6mm_maddr);
2734 kfree(imm, M_IPMADDR);
2735 break;
2737 default:
2738 error = EOPNOTSUPP;
2739 break;
2743 * If all options have default values, no need to keep the mbuf.
2745 if (im6o->im6o_multicast_ifp == NULL &&
2746 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2747 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2748 im6o->im6o_memberships.lh_first == NULL) {
2749 kfree(*im6op, M_IPMOPTS);
2750 *im6op = NULL;
2753 return (error);
2757 * Return the IP6 multicast options in response to user getsockopt().
2759 static int
2760 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2762 u_int *hlim, *loop, *ifindex;
2764 *mp = m_get(M_WAITOK, MT_HEADER); /* XXX */
2766 switch (optname) {
2768 case IPV6_MULTICAST_IF:
2769 ifindex = mtod(*mp, u_int *);
2770 (*mp)->m_len = sizeof(u_int);
2771 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2772 *ifindex = 0;
2773 else
2774 *ifindex = im6o->im6o_multicast_ifp->if_index;
2775 return (0);
2777 case IPV6_MULTICAST_HOPS:
2778 hlim = mtod(*mp, u_int *);
2779 (*mp)->m_len = sizeof(u_int);
2780 if (im6o == NULL)
2781 *hlim = ip6_defmcasthlim;
2782 else
2783 *hlim = im6o->im6o_multicast_hlim;
2784 return (0);
2786 case IPV6_MULTICAST_LOOP:
2787 loop = mtod(*mp, u_int *);
2788 (*mp)->m_len = sizeof(u_int);
2789 if (im6o == NULL)
2790 *loop = ip6_defmcasthlim;
2791 else
2792 *loop = im6o->im6o_multicast_loop;
2793 return (0);
2795 default:
2796 return (EOPNOTSUPP);
2801 * Discard the IP6 multicast options.
2803 void
2804 ip6_freemoptions(struct ip6_moptions *im6o)
2806 struct in6_multi_mship *imm;
2808 if (im6o == NULL)
2809 return;
2811 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2812 LIST_REMOVE(imm, i6mm_chain);
2813 if (imm->i6mm_maddr)
2814 in6_delmulti(imm->i6mm_maddr);
2815 kfree(imm, M_IPMADDR);
2817 kfree(im6o, M_IPMOPTS);
2821 * Set a particular packet option, as a sticky option or an ancillary data
2822 * item. "len" can be 0 only when it's a sticky option.
2823 * We have 4 cases of combination of "sticky" and "cmsg":
2824 * "sticky=0, cmsg=0": impossible
2825 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2826 * "sticky=1, cmsg=0": RFC3542 socket option
2827 * "sticky=1, cmsg=1": RFC2292 socket option
2829 static int
2830 ip6_setpktoption(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2831 int sticky, int cmsg, int uproto, int priv)
2833 int minmtupolicy, preftemp;
2834 //int error;
2836 if (!sticky && !cmsg) {
2837 kprintf("ip6_setpktoption: impossible case\n");
2838 return (EINVAL);
2842 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2843 * not be specified in the context of RFC3542. Conversely,
2844 * RFC3542 types should not be specified in the context of RFC2292.
2846 if (!cmsg) {
2847 switch (optname) {
2848 case IPV6_2292PKTINFO:
2849 case IPV6_2292HOPLIMIT:
2850 case IPV6_2292NEXTHOP:
2851 case IPV6_2292HOPOPTS:
2852 case IPV6_2292DSTOPTS:
2853 case IPV6_2292RTHDR:
2854 case IPV6_2292PKTOPTIONS:
2855 return (ENOPROTOOPT);
2858 if (sticky && cmsg) {
2859 switch (optname) {
2860 case IPV6_PKTINFO:
2861 case IPV6_HOPLIMIT:
2862 case IPV6_NEXTHOP:
2863 case IPV6_HOPOPTS:
2864 case IPV6_DSTOPTS:
2865 case IPV6_RTHDRDSTOPTS:
2866 case IPV6_RTHDR:
2867 case IPV6_USE_MIN_MTU:
2868 case IPV6_DONTFRAG:
2869 case IPV6_TCLASS:
2870 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2871 return (ENOPROTOOPT);
2875 switch (optname) {
2876 case IPV6_2292PKTINFO:
2877 case IPV6_PKTINFO:
2879 struct in6_pktinfo *pktinfo;
2880 if (len != sizeof(struct in6_pktinfo))
2881 return (EINVAL);
2882 pktinfo = (struct in6_pktinfo *)buf;
2885 * An application can clear any sticky IPV6_PKTINFO option by
2886 * doing a "regular" setsockopt with ipi6_addr being
2887 * in6addr_any and ipi6_ifindex being zero.
2888 * [RFC 3542, Section 6]
2890 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2891 pktinfo->ipi6_ifindex == 0 &&
2892 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2893 ip6_clearpktopts(opt, optname);
2894 break;
2897 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2898 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2899 return (EINVAL);
2902 /* validate the interface index if specified. */
2903 if (pktinfo->ipi6_ifindex > if_index ||
2904 pktinfo->ipi6_ifindex < 0) {
2905 return (ENXIO);
2908 * Check if the requested source address is indeed a
2909 * unicast address assigned to the node, and can be
2910 * used as the packet's source address.
2912 if (opt->ip6po_pktinfo != NULL &&
2913 !IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2914 struct in6_ifaddr *ia6;
2915 struct sockaddr_in6 sin6;
2917 bzero(&sin6, sizeof(sin6));
2918 sin6.sin6_len = sizeof(sin6);
2919 sin6.sin6_family = AF_INET6;
2920 sin6.sin6_addr =
2921 opt->ip6po_pktinfo->ipi6_addr;
2922 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2923 if (ia6 == NULL ||
2924 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2925 IN6_IFF_NOTREADY)) != 0)
2926 return (EADDRNOTAVAIL);
2930 * We store the address anyway, and let in6_selectsrc()
2931 * validate the specified address. This is because ipi6_addr
2932 * may not have enough information about its scope zone, and
2933 * we may need additional information (such as outgoing
2934 * interface or the scope zone of a destination address) to
2935 * disambiguate the scope.
2936 * XXX: the delay of the validation may confuse the
2937 * application when it is used as a sticky option.
2939 if (opt->ip6po_pktinfo == NULL) {
2940 opt->ip6po_pktinfo = kmalloc(sizeof(*pktinfo),
2941 M_IP6OPT, M_NOWAIT);
2942 if (opt->ip6po_pktinfo == NULL)
2943 return (ENOBUFS);
2945 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2946 break;
2949 case IPV6_2292HOPLIMIT:
2950 case IPV6_HOPLIMIT:
2952 int *hlimp;
2955 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2956 * to simplify the ordering among hoplimit options.
2958 if (optname == IPV6_HOPLIMIT && sticky)
2959 return (ENOPROTOOPT);
2961 if (len != sizeof(int))
2962 return (EINVAL);
2963 hlimp = (int *)buf;
2964 if (*hlimp < -1 || *hlimp > 255)
2965 return (EINVAL);
2967 opt->ip6po_hlim = *hlimp;
2968 break;
2971 case IPV6_TCLASS:
2973 int tclass;
2975 if (len != sizeof(int))
2976 return (EINVAL);
2977 tclass = *(int *)buf;
2978 if (tclass < -1 || tclass > 255)
2979 return (EINVAL);
2981 opt->ip6po_tclass = tclass;
2982 break;
2985 case IPV6_2292NEXTHOP:
2986 case IPV6_NEXTHOP:
2987 if (!priv)
2988 return (EPERM);
2990 if (len == 0) { /* just remove the option */
2991 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2992 break;
2995 /* check if cmsg_len is large enough for sa_len */
2996 if (len < sizeof(struct sockaddr) || len < *buf)
2997 return (EINVAL);
2999 switch (((struct sockaddr *)buf)->sa_family) {
3000 case AF_INET6:
3002 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3003 //int error;
3005 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3006 return (EINVAL);
3008 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3009 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3010 return (EINVAL);
3012 break;
3014 case AF_LINK: /* should eventually be supported */
3015 default:
3016 return (EAFNOSUPPORT);
3019 /* turn off the previous option, then set the new option. */
3020 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3021 opt->ip6po_nexthop = kmalloc(*buf, M_IP6OPT, M_NOWAIT);
3022 if (opt->ip6po_nexthop == NULL)
3023 return (ENOBUFS);
3024 bcopy(buf, opt->ip6po_nexthop, *buf);
3025 break;
3027 case IPV6_2292HOPOPTS:
3028 case IPV6_HOPOPTS:
3030 struct ip6_hbh *hbh;
3031 int hbhlen;
3034 * XXX: We don't allow a non-privileged user to set ANY HbH
3035 * options, since per-option restriction has too much
3036 * overhead.
3038 if (!priv)
3039 return (EPERM);
3040 if (len == 0) {
3041 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3042 break; /* just remove the option */
3045 /* message length validation */
3046 if (len < sizeof(struct ip6_hbh))
3047 return (EINVAL);
3048 hbh = (struct ip6_hbh *)buf;
3049 hbhlen = (hbh->ip6h_len + 1) << 3;
3050 if (len != hbhlen)
3051 return (EINVAL);
3053 /* turn off the previous option, then set the new option. */
3054 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3055 opt->ip6po_hbh = kmalloc(hbhlen, M_IP6OPT, M_NOWAIT);
3056 if (opt->ip6po_hbh == NULL)
3057 return (ENOBUFS);
3058 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3060 break;
3063 case IPV6_2292DSTOPTS:
3064 case IPV6_DSTOPTS:
3065 case IPV6_RTHDRDSTOPTS:
3067 struct ip6_dest *dest, **newdest = NULL;
3068 int destlen;
3069 if (!priv)
3070 return (EPERM);
3072 if (len == 0) {
3073 ip6_clearpktopts(opt, optname);
3074 break; /* just remove the option */
3077 /* message length validation */
3078 if (len < sizeof(struct ip6_dest))
3079 return (EINVAL);
3080 dest = (struct ip6_dest *)buf;
3081 destlen = (dest->ip6d_len + 1) << 3;
3082 if (len != destlen)
3083 return (EINVAL);
3086 * Determine the position that the destination options header
3087 * should be inserted; before or after the routing header.
3089 switch (optname) {
3090 case IPV6_2292DSTOPTS:
3092 * The old advacned API is ambiguous on this point.
3093 * Our approach is to determine the position based
3094 * according to the existence of a routing header.
3095 * Note, however, that this depends on the order of the
3096 * extension headers in the ancillary data; the 1st
3097 * part of the destination options header must appear
3098 * before the routing header in the ancillary data,
3099 * too.
3100 * RFC3542 solved the ambiguity by introducing
3101 * separate ancillary data or option types.
3103 if (opt->ip6po_rthdr == NULL)
3104 newdest = &opt->ip6po_dest1;
3105 else
3106 newdest = &opt->ip6po_dest2;
3107 break;
3108 case IPV6_RTHDRDSTOPTS:
3109 newdest = &opt->ip6po_dest1;
3110 break;
3111 case IPV6_DSTOPTS:
3112 newdest = &opt->ip6po_dest2;
3113 break;
3116 /* turn off the previous option, then set the new option. */
3117 ip6_clearpktopts(opt, optname);
3118 *newdest = kmalloc(destlen, M_IP6OPT, M_NOWAIT);
3119 if (*newdest == NULL)
3120 return (ENOBUFS);
3121 bcopy(dest, *newdest, destlen);
3123 break;
3126 case IPV6_2292RTHDR:
3127 case IPV6_RTHDR:
3129 struct ip6_rthdr *rth;
3130 int rthlen;
3132 if (len == 0) {
3133 ip6_clearpktopts(opt, IPV6_RTHDR);
3134 break; /* just remove the option */
3137 /* message length validation */
3138 if (len < sizeof(struct ip6_rthdr))
3139 return (EINVAL);
3140 rth = (struct ip6_rthdr *)buf;
3141 rthlen = (rth->ip6r_len + 1) << 3;
3142 if (len != rthlen)
3143 return (EINVAL);
3145 switch (rth->ip6r_type) {
3146 default:
3147 return (EINVAL); /* not supported */
3150 /* turn off the previous option */
3151 ip6_clearpktopts(opt, IPV6_RTHDR);
3152 opt->ip6po_rthdr = kmalloc(rthlen, M_IP6OPT, M_NOWAIT);
3153 if (opt->ip6po_rthdr == NULL)
3154 return (ENOBUFS);
3155 bcopy(rth, opt->ip6po_rthdr, rthlen);
3157 break;
3160 case IPV6_USE_MIN_MTU:
3161 if (len != sizeof(int))
3162 return (EINVAL);
3163 minmtupolicy = *(int *)buf;
3164 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3165 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3166 minmtupolicy != IP6PO_MINMTU_ALL) {
3167 return (EINVAL);
3169 opt->ip6po_minmtu = minmtupolicy;
3170 break;
3172 case IPV6_DONTFRAG:
3173 if (len != sizeof(int))
3174 return (EINVAL);
3176 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3178 * we ignore this option for TCP sockets.
3179 * (RFC3542 leaves this case unspecified.)
3181 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3182 } else
3183 opt->ip6po_flags |= IP6PO_DONTFRAG;
3184 break;
3186 case IPV6_PREFER_TEMPADDR:
3187 if (len != sizeof(int))
3188 return (EINVAL);
3189 preftemp = *(int *)buf;
3190 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3191 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3192 preftemp != IP6PO_TEMPADDR_PREFER) {
3193 return (EINVAL);
3195 opt->ip6po_prefer_tempaddr = preftemp;
3196 break;
3198 default:
3199 return (ENOPROTOOPT);
3200 } /* end of switch */
3202 return (0);
3207 * Set IPv6 outgoing packet options based on advanced API.
3210 ip6_setpktoptions(struct mbuf *control, struct ip6_pktopts *opt,
3211 struct ip6_pktopts *stickyopt, int uproto, int priv)
3213 struct cmsghdr *cm = NULL;
3215 if (control == NULL || opt == NULL)
3216 return (EINVAL);
3218 init_ip6pktopts(opt);
3221 * XXX: Currently, we assume all the optional information is stored
3222 * in a single mbuf.
3224 if (stickyopt) {
3225 int error;
3228 * If stickyopt is provided, make a local copy of the options
3229 * for this particular packet, then override them by ancillary
3230 * objects.
3231 * XXX: copypktopts() does not copy the cached route to a next
3232 * hop (if any). This is not very good in terms of efficiency,
3233 * but we can allow this since this option should be rarely
3234 * used.
3236 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
3237 return (error);
3241 * XXX: Currently, we assume all the optional information is stored
3242 * in a single mbuf.
3244 if (control->m_next)
3245 return (EINVAL);
3247 for (; control->m_len; control->m_data += CMSG_ALIGN(cm->cmsg_len),
3248 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
3249 int error;
3251 if (control->m_len < CMSG_LEN(0))
3252 return (EINVAL);
3254 cm = mtod(control, struct cmsghdr *);
3255 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
3256 return (EINVAL);
3257 if (cm->cmsg_level != IPPROTO_IPV6)
3258 continue;
3260 error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm),
3261 cm->cmsg_len - CMSG_LEN(0), opt, 0, 1, uproto, priv);
3262 if (error)
3263 return (error);
3266 return (0);
3270 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3271 * packet to the input queue of a specified interface. Note that this
3272 * calls the output routine of the loopback "driver", but with an interface
3273 * pointer that might NOT be &loif -- easier than replicating that code here.
3275 void
3276 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3278 struct mbuf *copym;
3279 struct ip6_hdr *ip6;
3281 copym = m_copy(m, 0, M_COPYALL);
3282 if (copym == NULL)
3283 return;
3286 * Make sure to deep-copy IPv6 header portion in case the data
3287 * is in an mbuf cluster, so that we can safely override the IPv6
3288 * header portion later.
3290 if ((copym->m_flags & M_EXT) != 0 ||
3291 copym->m_len < sizeof(struct ip6_hdr)) {
3292 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3293 if (copym == NULL)
3294 return;
3297 #ifdef DIAGNOSTIC
3298 if (copym->m_len < sizeof(*ip6)) {
3299 m_freem(copym);
3300 return;
3302 #endif
3304 ip6 = mtod(copym, struct ip6_hdr *);
3306 * clear embedded scope identifiers if necessary.
3307 * in6_clearscope will touch the addresses only when necessary.
3309 in6_clearscope(&ip6->ip6_src);
3310 in6_clearscope(&ip6->ip6_dst);
3312 if_simloop(ifp, copym, dst->sin6_family, 0);
3316 * Separate the IPv6 header from the payload into its own mbuf.
3318 * Returns the new mbuf chain or the original mbuf if no payload.
3319 * Returns NULL if can't allocate new mbuf for header.
3321 static struct mbuf *
3322 ip6_splithdr(struct mbuf *m)
3324 struct mbuf *mh;
3326 if (m->m_len <= sizeof(struct ip6_hdr)) /* no payload */
3327 return (m);
3329 MGETHDR(mh, M_NOWAIT, MT_HEADER);
3330 if (mh == NULL)
3331 return (NULL);
3332 mh->m_len = sizeof(struct ip6_hdr);
3333 M_MOVE_PKTHDR(mh, m);
3334 MH_ALIGN(mh, sizeof(struct ip6_hdr));
3335 bcopy(mtod(m, caddr_t), mtod(mh, caddr_t), sizeof(struct ip6_hdr));
3336 m->m_data += sizeof(struct ip6_hdr);
3337 m->m_len -= sizeof(struct ip6_hdr);
3338 mh->m_next = m;
3339 return (mh);
3343 * Compute IPv6 extension header length.
3346 ip6_optlen(struct in6pcb *in6p)
3348 int len;
3350 if (!in6p->in6p_outputopts)
3351 return 0;
3353 len = 0;
3354 #define elen(x) \
3355 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3357 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3358 if (in6p->in6p_outputopts->ip6po_rthdr)
3359 /* dest1 is valid with rthdr only */
3360 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3361 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3362 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3363 return len;
3364 #undef elen