dhcpcd: update README.DRAGONFLY
[dragonfly.git] / sys / netinet6 / ip6_output.c
blobb5ae2c90309802b8853bd6d3ad4765651feaaa55
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"
68 #include <sys/param.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/errno.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/proc.h>
78 #include <sys/caps.h>
80 #include <sys/msgport2.h>
82 #include <net/if.h>
83 #include <net/route.h>
84 #include <net/pfil.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet/icmp6.h>
91 #include <netinet6/ip6_var.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/ip6protosw.h>
96 #include <net/ip6fw/ip6_fw.h>
98 #include <net/net_osdep.h>
100 static MALLOC_DEFINE(M_IPMOPTS, "ip6_moptions", "internet multicast options");
102 struct ip6_exthdrs {
103 struct mbuf *ip6e_ip6;
104 struct mbuf *ip6e_hbh;
105 struct mbuf *ip6e_dest1;
106 struct mbuf *ip6e_rthdr;
107 struct mbuf *ip6e_dest2;
110 static int ip6_pcbopt (int, u_char *, int, struct ip6_pktopts **, int);
111 static int ip6_setpktoption (int, u_char *, int, struct ip6_pktopts *,
112 int, int, int, int);
113 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *,
114 struct sockopt *);
115 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
116 static int ip6_setmoptions (int, struct ip6_moptions **, struct mbuf *);
117 static int ip6_getmoptions (int, struct ip6_moptions *, struct mbuf **);
118 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
119 struct ifnet *, struct in6_addr *, u_long *, int *);
120 static int copyexthdr (void *, struct mbuf **);
121 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
122 struct ip6_frag **);
123 static int ip6_insert_jumboopt (struct ip6_exthdrs *, u_int32_t);
124 static struct mbuf *ip6_splithdr (struct mbuf *);
125 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
128 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
129 * header (with pri, len, nxt, hlim, src, dst).
130 * This function may modify ver and hlim only.
131 * The mbuf chain containing the packet will be freed.
132 * The mbuf opt, if present, will not be freed.
134 * type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
135 * nd_ifinfo.linkmtu is u_int32_t. so we use u_long to hold largest one,
136 * which is rt_rmx.rmx_mtu.
139 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
140 int flags, struct ip6_moptions *im6o,
141 struct ifnet **ifpp, /* XXX: just for statistics */
142 struct inpcb *inp)
144 struct ip6_hdr *ip6, *mhip6;
145 struct ifnet *ifp, *origifp;
146 struct mbuf *m = m0;
147 struct mbuf *mprev;
148 u_char *nexthdrp;
149 int hlen, tlen, len, off;
150 struct route_in6 ip6route;
151 struct sockaddr_in6 *dst;
152 int error = 0;
153 struct in6_ifaddr *ia = NULL;
154 u_long mtu;
155 int alwaysfrag, dontfrag;
156 u_int32_t optlen, plen = 0, unfragpartlen;
157 struct ip6_exthdrs exthdrs;
158 struct in6_addr finaldst;
159 struct route_in6 *ro_pmtu = NULL;
160 boolean_t hdrsplit = FALSE;
162 bzero(&exthdrs, sizeof exthdrs);
164 if (opt) {
165 if ((error = copyexthdr(opt->ip6po_hbh, &exthdrs.ip6e_hbh)))
166 goto freehdrs;
167 if ((error = copyexthdr(opt->ip6po_dest1, &exthdrs.ip6e_dest1)))
168 goto freehdrs;
169 if ((error = copyexthdr(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr)))
170 goto freehdrs;
171 if ((error = copyexthdr(opt->ip6po_dest2, &exthdrs.ip6e_dest2)))
172 goto freehdrs;
176 * Calculate the total length of the extension header chain.
177 * Keep the length of the unfragmentable part for fragmentation.
179 optlen = m_lengthm(exthdrs.ip6e_hbh, NULL) +
180 m_lengthm(exthdrs.ip6e_dest1, NULL) +
181 m_lengthm(exthdrs.ip6e_rthdr, NULL);
183 unfragpartlen = optlen + sizeof(struct ip6_hdr);
185 /* NOTE: we don't add AH/ESP length here. do that later. */
186 optlen += m_lengthm(exthdrs.ip6e_dest2, NULL);
189 * If there is at least one extension header,
190 * separate IP6 header from the payload.
192 if (optlen && !hdrsplit) {
193 exthdrs.ip6e_ip6 = ip6_splithdr(m);
194 if (exthdrs.ip6e_ip6 == NULL) {
195 error = ENOBUFS;
196 goto freehdrs;
198 m = exthdrs.ip6e_ip6;
199 hdrsplit = TRUE;
202 /* adjust pointer */
203 ip6 = mtod(m, struct ip6_hdr *);
205 /* adjust mbuf packet header length */
206 m->m_pkthdr.len += optlen;
207 plen = m->m_pkthdr.len - sizeof(*ip6);
209 /* If this is a jumbo payload, insert a jumbo payload option. */
210 if (plen > IPV6_MAXPACKET) {
211 if (!hdrsplit) {
212 exthdrs.ip6e_ip6 = ip6_splithdr(m);
213 if (exthdrs.ip6e_ip6 == NULL) {
214 error = ENOBUFS;
215 goto freehdrs;
217 m = exthdrs.ip6e_ip6;
218 hdrsplit = TRUE;
220 /* adjust pointer */
221 ip6 = mtod(m, struct ip6_hdr *);
222 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
223 goto freehdrs;
224 ip6->ip6_plen = 0;
225 } else
226 ip6->ip6_plen = htons(plen);
229 * Concatenate headers and fill in next header fields.
230 * Here we have, on "m"
231 * IPv6 payload
232 * and we insert headers accordingly. Finally, we should be getting:
233 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
235 * during the header composing process, "m" points to IPv6 header.
236 * "mprev" points to an extension header prior to esp.
239 nexthdrp = &ip6->ip6_nxt;
240 mprev = m;
243 * we treat dest2 specially. the goal here is to make mprev point the
244 * mbuf prior to dest2.
246 * result: IPv6 dest2 payload
247 * m and mprev will point to IPv6 header.
249 if (exthdrs.ip6e_dest2) {
250 if (!hdrsplit)
251 panic("assumption failed: hdr not split");
252 exthdrs.ip6e_dest2->m_next = m->m_next;
253 m->m_next = exthdrs.ip6e_dest2;
254 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
255 ip6->ip6_nxt = IPPROTO_DSTOPTS;
259 * Place m1 after mprev.
261 #define MAKE_CHAIN(m1, mprev, nexthdrp, i)\
262 do {\
263 if (m1) {\
264 if (!hdrsplit)\
265 panic("assumption failed: hdr not split");\
266 *mtod(m1, u_char *) = *nexthdrp;\
267 *nexthdrp = (i);\
268 nexthdrp = mtod(m1, u_char *);\
269 m1->m_next = mprev->m_next;\
270 mprev->m_next = m1;\
271 mprev = m1;\
273 } while (0)
276 * result: IPv6 hbh dest1 rthdr dest2 payload
277 * m will point to IPv6 header. mprev will point to the
278 * extension header prior to dest2 (rthdr in the above case).
280 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
281 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
282 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
285 * If there is a routing header, replace the destination address field
286 * with the first hop of the routing header.
288 if (exthdrs.ip6e_rthdr) {
289 struct ip6_rthdr *rh;
291 finaldst = ip6->ip6_dst;
292 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
293 switch (rh->ip6r_type) {
294 default: /* is it possible? */
295 error = EINVAL;
296 goto bad;
300 /* Source address validation */
301 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
302 !(flags & IPV6_DADOUTPUT)) {
303 error = EOPNOTSUPP;
304 ip6stat.ip6s_badscope++;
305 goto bad;
307 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
308 error = EOPNOTSUPP;
309 ip6stat.ip6s_badscope++;
310 goto bad;
313 ip6stat.ip6s_localout++;
316 * Route packet.
318 if (ro == NULL) {
319 ro = &ip6route;
320 bzero(ro, sizeof(*ro));
322 ro_pmtu = ro;
323 if (opt && opt->ip6po_rthdr)
324 ro = &opt->ip6po_route;
325 dst = (struct sockaddr_in6 *)&ro->ro_dst;
328 * If there is a cached route,
329 * check that it is to the same destination
330 * and is still up. If not, free it and try again.
332 if (ro->ro_rt != NULL &&
333 (!(ro->ro_rt->rt_flags & RTF_UP) || dst->sin6_family != AF_INET6 ||
334 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
335 RTFREE(ro->ro_rt);
336 ro->ro_rt = NULL;
338 if (ro->ro_rt == NULL) {
339 bzero(dst, sizeof(*dst));
340 dst->sin6_family = AF_INET6;
341 dst->sin6_len = sizeof(struct sockaddr_in6);
342 dst->sin6_addr = ip6->ip6_dst;
344 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
345 /* Unicast */
347 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
348 #define sin6tosa(sin6) ((struct sockaddr *)(sin6))
349 /* xxx
350 * interface selection comes here
351 * if an interface is specified from an upper layer,
352 * ifp must point it.
354 if (ro->ro_rt == NULL) {
356 * non-bsdi always clone routes, if parent is
357 * PRF_CLONING.
359 rtalloc((struct route *)ro);
361 if (ro->ro_rt == NULL) {
362 ip6stat.ip6s_noroute++;
363 error = EHOSTUNREACH;
364 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard); */
365 goto bad;
367 ia = ifatoia6(ro->ro_rt->rt_ifa);
368 ifp = ro->ro_rt->rt_ifp;
369 ro->ro_rt->rt_use++;
370 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
371 dst = (struct sockaddr_in6 *)ro->ro_rt->rt_gateway;
372 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
374 in6_ifstat_inc(ifp, ifs6_out_request);
377 * Check if the outgoing interface conflicts with
378 * the interface specified by ifi6_ifindex (if specified).
379 * Note that loopback interface is always okay.
380 * (this may happen when we are sending a packet to one of
381 * our own addresses.)
383 if (opt && opt->ip6po_pktinfo
384 && opt->ip6po_pktinfo->ipi6_ifindex) {
385 if (!(ifp->if_flags & IFF_LOOPBACK)
386 && ifp->if_index != opt->ip6po_pktinfo->ipi6_ifindex) {
387 ip6stat.ip6s_noroute++;
388 in6_ifstat_inc(ifp, ifs6_out_discard);
389 error = EHOSTUNREACH;
390 goto bad;
394 if (opt && opt->ip6po_hlim != -1)
395 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
396 } else {
397 /* Multicast */
398 struct in6_multi *in6m;
400 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
403 * See if the caller provided any multicast options
405 ifp = NULL;
406 if (im6o != NULL) {
407 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
408 if (im6o->im6o_multicast_ifp != NULL)
409 ifp = im6o->im6o_multicast_ifp;
410 } else
411 ip6->ip6_hlim = ip6_defmcasthlim;
414 * See if the caller provided the outgoing interface
415 * as an ancillary data.
416 * Boundary check for ifindex is assumed to be already done.
418 if (opt && opt->ip6po_pktinfo && opt->ip6po_pktinfo->ipi6_ifindex)
419 ifp = ifindex2ifnet[opt->ip6po_pktinfo->ipi6_ifindex];
422 * If the destination is a node-local scope multicast,
423 * the packet should be loop-backed only.
425 if (IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
427 * If the outgoing interface is already specified,
428 * it should be a loopback interface.
430 if (ifp && !(ifp->if_flags & IFF_LOOPBACK)) {
431 ip6stat.ip6s_badscope++;
432 error = ENETUNREACH; /* XXX: better error? */
433 /* XXX correct ifp? */
434 in6_ifstat_inc(ifp, ifs6_out_discard);
435 goto bad;
436 } else {
437 ifp = loif;
441 if (opt && opt->ip6po_hlim != -1)
442 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
445 * If caller did not provide an interface lookup a
446 * default in the routing table. This is either a
447 * default for the speicfied group (i.e. a host
448 * route), or a multicast default (a route for the
449 * ``net'' ff00::/8).
451 if (ifp == NULL) {
452 if (ro->ro_rt == NULL) {
453 ro->ro_rt =
454 rtpurelookup((struct sockaddr *)&ro->ro_dst);
456 if (ro->ro_rt == NULL) {
457 ip6stat.ip6s_noroute++;
458 error = EHOSTUNREACH;
459 /* XXX in6_ifstat_inc(ifp, ifs6_out_discard) */
460 goto bad;
462 ia = ifatoia6(ro->ro_rt->rt_ifa);
463 ifp = ro->ro_rt->rt_ifp;
464 ro->ro_rt->rt_use++;
467 if (!(flags & IPV6_FORWARDING))
468 in6_ifstat_inc(ifp, ifs6_out_request);
469 in6_ifstat_inc(ifp, ifs6_out_mcast);
472 * Confirm that the outgoing interface supports multicast.
474 if (!(ifp->if_flags & IFF_MULTICAST)) {
475 ip6stat.ip6s_noroute++;
476 in6_ifstat_inc(ifp, ifs6_out_discard);
477 error = ENETUNREACH;
478 goto bad;
480 in6m = IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp);
481 if (in6m != NULL &&
482 (im6o == NULL || im6o->im6o_multicast_loop)) {
484 * If we belong to the destination multicast group
485 * on the outgoing interface, and the caller did not
486 * forbid loopback, loop back a copy.
488 ip6_mloopback(ifp, m, dst);
489 } else {
491 * If we are acting as a multicast router, perform
492 * multicast forwarding as if the packet had just
493 * arrived on the interface to which we are about
494 * to send. The multicast forwarding function
495 * recursively calls this function, using the
496 * IPV6_FORWARDING flag to prevent infinite recursion.
498 * Multicasts that are looped back by ip6_mloopback(),
499 * above, will be forwarded by the ip6_input() routine,
500 * if necessary.
502 if (ip6_mrouter && !(flags & IPV6_FORWARDING)) {
503 if (ip6_mforward(ip6, ifp, m) != 0) {
504 m_freem(m);
505 goto done;
510 * Multicasts with a hoplimit of zero may be looped back,
511 * above, but must not be transmitted on a network.
512 * Also, multicasts addressed to the loopback interface
513 * are not sent -- the above call to ip6_mloopback() will
514 * loop back a copy if this host actually belongs to the
515 * destination group on the loopback interface.
517 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK)) {
518 m_freem(m);
519 goto done;
524 * Fill the outgoing inteface to tell the upper layer
525 * to increment per-interface statistics.
527 if (ifpp)
528 *ifpp = ifp;
530 /* Determine path MTU. */
531 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
532 &alwaysfrag)) != 0)
533 goto bad;
536 * The caller of this function may specify to use the minimum MTU
537 * in some cases.
538 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
539 * setting. The logic is a bit complicated; by default, unicast
540 * packets will follow path MTU while multicast packets will be sent at
541 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
542 * including unicast ones will be sent at the minimum MTU. Multicast
543 * packets will always be sent at the minimum MTU unless
544 * IP6PO_MINMTU_DISABLE is explicitly specified.
545 * See RFC 3542 for more details.
547 if (mtu > IPV6_MMTU) {
548 if ((flags & IPV6_MINMTU))
549 mtu = IPV6_MMTU;
550 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
551 mtu = IPV6_MMTU;
552 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
553 (opt == NULL ||
554 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
555 mtu = IPV6_MMTU;
559 /* Fake scoped addresses */
560 if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
562 * If source or destination address is a scoped address, and
563 * the packet is going to be sent to a loopback interface,
564 * we should keep the original interface.
568 * XXX: this is a very experimental and temporary solution.
569 * We eventually have sockaddr_in6 and use the sin6_scope_id
570 * field of the structure here.
571 * We rely on the consistency between two scope zone ids
572 * of source and destination, which should already be assured.
573 * Larger scopes than link will be supported in the future.
575 origifp = NULL;
576 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src))
577 origifp = ifindex2ifnet[ntohs(ip6->ip6_src.s6_addr16[1])];
578 else if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst))
579 origifp = ifindex2ifnet[ntohs(ip6->ip6_dst.s6_addr16[1])];
581 * XXX: origifp can be NULL even in those two cases above.
582 * For example, if we remove the (only) link-local address
583 * from the loopback interface, and try to send a link-local
584 * address without link-id information. Then the source
585 * address is ::1, and the destination address is the
586 * link-local address with its s6_addr16[1] being zero.
587 * What is worse, if the packet goes to the loopback interface
588 * by a default rejected route, the null pointer would be
589 * passed to looutput, and the kernel would hang.
590 * The following last resort would prevent such disaster.
592 if (origifp == NULL)
593 origifp = ifp;
595 else
596 origifp = ifp;
598 * clear embedded scope identifiers if necessary.
599 * in6_clearscope will touch the addresses only when necessary.
601 in6_clearscope(&ip6->ip6_src);
602 in6_clearscope(&ip6->ip6_dst);
605 * Check with the firewall...
607 if (ip6_fw_enable && ip6_fw_chk_ptr) {
608 u_short port = 0;
610 m->m_pkthdr.rcvif = NULL; /* XXX */
611 /* If ipfw says divert, we have to just drop packet */
612 if ((*ip6_fw_chk_ptr)(&ip6, ifp, &port, &m)) {
613 m_freem(m);
614 goto done;
616 if (!m) {
617 error = EACCES;
618 goto done;
623 * If the outgoing packet contains a hop-by-hop options header,
624 * it must be examined and processed even by the source node.
625 * (RFC 2460, section 4.)
627 if (exthdrs.ip6e_hbh) {
628 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
629 u_int32_t dummy1; /* XXX unused */
630 u_int32_t dummy2; /* XXX unused */
632 #ifdef DIAGNOSTIC
633 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
634 panic("ip6e_hbh is not continuous");
635 #endif
637 * XXX: if we have to send an ICMPv6 error to the sender,
638 * we need the M_LOOP flag since icmp6_error() expects
639 * the IPv6 and the hop-by-hop options header are
640 * continuous unless the flag is set.
642 m->m_flags |= M_LOOP;
643 m->m_pkthdr.rcvif = ifp;
644 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
645 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
646 &dummy1, &dummy2) < 0) {
647 /* m was already freed at this point */
648 error = EINVAL;/* better error? */
649 goto done;
651 m->m_flags &= ~M_LOOP; /* XXX */
652 m->m_pkthdr.rcvif = NULL;
656 * Run through list of hooks for output packets.
658 if (pfil_has_hooks(&inet6_pfil_hook)) {
659 error = pfil_run_hooks(&inet6_pfil_hook, &m, ifp, PFIL_OUT);
660 if (error != 0 || m == NULL)
661 goto done;
662 ip6 = mtod(m, struct ip6_hdr *);
666 * Send the packet to the outgoing interface.
667 * If necessary, do IPv6 fragmentation before sending.
669 * the logic here is rather complex:
670 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
671 * 1-a: send as is if tlen <= path mtu
672 * 1-b: fragment if tlen > path mtu
674 * 2: if user asks us not to fragment (dontfrag == 1)
675 * 2-a: send as is if tlen <= interface mtu
676 * 2-b: error if tlen > interface mtu
678 * 3: if we always need to attach fragment header (alwaysfrag == 1)
679 * always fragment
681 * 4: if dontfrag == 1 && alwaysfrag == 1
682 * error, as we cannot handle this conflicting request
684 tlen = m->m_pkthdr.len;
686 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
687 dontfrag = 1;
688 else
689 dontfrag = 0;
690 if (dontfrag && alwaysfrag) { /* case 4 */
691 /* conflicting request - can't transmit */
692 error = EMSGSIZE;
693 goto bad;
695 if (dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
697 * Even if the DONTFRAG option is specified, we cannot send the
698 * packet when the data length is larger than the MTU of the
699 * outgoing interface.
700 * Notify the error by sending IPV6_PATHMTU ancillary data as
701 * well as returning an error code (the latter is not described
702 * in the API spec.)
704 u_int32_t mtu32;
705 struct ip6ctlparam ip6cp;
707 mtu32 = (u_int32_t)mtu;
708 bzero(&ip6cp, sizeof(ip6cp));
709 ip6cp.ip6c_cmdarg = (void *)&mtu32;
710 kpfctlinput2(PRC_MSGSIZE, (struct sockaddr *)&ro_pmtu->ro_dst,
711 (void *)&ip6cp);
713 error = EMSGSIZE;
714 goto bad;
718 * transmit packet without fragmentation
720 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* case 1-a and 2-a */
721 struct in6_ifaddr *ia6;
723 ip6 = mtod(m, struct ip6_hdr *);
724 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
725 if (ia6) {
726 /* Record statistics for this interface address. */
727 IFA_STAT_INC(&ia6->ia_ifa, opackets, 1);
728 IFA_STAT_INC(&ia6->ia_ifa, obytes, m->m_pkthdr.len);
730 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
731 goto done;
735 * try to fragment the packet. case 1-b and 3
737 if (mtu < IPV6_MMTU) {
739 * note that path MTU is never less than IPV6_MMTU
740 * (see icmp6_input).
742 error = EMSGSIZE;
743 in6_ifstat_inc(ifp, ifs6_out_fragfail);
744 goto bad;
745 } else if (ip6->ip6_plen == 0) {
746 /* jumbo payload cannot be fragmented */
747 error = EMSGSIZE;
748 in6_ifstat_inc(ifp, ifs6_out_fragfail);
749 goto bad;
750 } else {
751 struct mbuf **mnext, *m_frgpart;
752 struct ip6_frag *ip6f;
753 u_int32_t id = htonl(ip6_id++);
754 u_char nextproto;
757 * Too large for the destination or interface;
758 * fragment if possible.
759 * Must be able to put at least 8 bytes per fragment.
761 hlen = unfragpartlen;
762 if (mtu > IPV6_MAXPACKET)
763 mtu = IPV6_MAXPACKET;
765 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
766 if (len < 8) {
767 error = EMSGSIZE;
768 in6_ifstat_inc(ifp, ifs6_out_fragfail);
769 goto bad;
772 mnext = &m->m_nextpkt;
775 * Change the next header field of the last header in the
776 * unfragmentable part.
778 if (exthdrs.ip6e_rthdr) {
779 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
780 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
781 } else if (exthdrs.ip6e_dest1) {
782 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
783 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
784 } else if (exthdrs.ip6e_hbh) {
785 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
786 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
787 } else {
788 nextproto = ip6->ip6_nxt;
789 ip6->ip6_nxt = IPPROTO_FRAGMENT;
793 * Loop through length of segment after first fragment,
794 * make new header and copy data of each part and link onto
795 * chain.
797 m0 = m;
798 for (off = hlen; off < tlen; off += len) {
799 MGETHDR(m, M_NOWAIT, MT_HEADER);
800 if (!m) {
801 error = ENOBUFS;
802 ip6stat.ip6s_odropped++;
803 goto sendorfree;
805 m->m_pkthdr.rcvif = NULL;
806 m->m_flags = m0->m_flags & M_COPYFLAGS;
807 *mnext = m;
808 mnext = &m->m_nextpkt;
809 m->m_data += max_linkhdr;
810 mhip6 = mtod(m, struct ip6_hdr *);
811 *mhip6 = *ip6;
812 m->m_len = sizeof(*mhip6);
813 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
814 if (error) {
815 ip6stat.ip6s_odropped++;
816 goto sendorfree;
818 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
819 if (off + len >= tlen)
820 len = tlen - off;
821 else
822 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
823 mhip6->ip6_plen = htons((u_short)(len + hlen +
824 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
825 m_frgpart = m_copym(m0, off, len, M_NOWAIT);
826 if (m_frgpart == NULL) {
827 error = ENOBUFS;
828 ip6stat.ip6s_odropped++;
829 goto sendorfree;
831 m_cat(m, m_frgpart);
832 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
833 m->m_pkthdr.rcvif = NULL;
834 ip6f->ip6f_reserved = 0;
835 ip6f->ip6f_ident = id;
836 ip6f->ip6f_nxt = nextproto;
837 ip6stat.ip6s_ofragments++;
838 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
841 in6_ifstat_inc(ifp, ifs6_out_fragok);
845 * Remove leading garbages.
847 sendorfree:
848 m = m0->m_nextpkt;
849 m0->m_nextpkt = NULL;
850 m_freem(m0);
851 for (m0 = m; m; m = m0) {
852 m0 = m->m_nextpkt;
853 m->m_nextpkt = NULL;
854 if (error == 0) {
855 /* Record statistics for this interface address. */
856 if (ia) {
857 IFA_STAT_INC(&ia->ia_ifa, opackets, 1);
858 IFA_STAT_INC(&ia->ia_ifa, obytes,
859 m->m_pkthdr.len);
861 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt);
862 } else
863 m_freem(m);
866 if (error == 0)
867 ip6stat.ip6s_fragmented++;
869 done:
870 if (ro == &ip6route && ro->ro_rt) { /* brace necessary for RTFREE */
871 RTFREE(ro->ro_rt);
872 } else if (ro_pmtu == &ip6route && ro_pmtu->ro_rt) {
873 RTFREE(ro_pmtu->ro_rt);
876 return (error);
878 freehdrs:
879 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
880 m_freem(exthdrs.ip6e_dest1);
881 m_freem(exthdrs.ip6e_rthdr);
882 m_freem(exthdrs.ip6e_dest2);
883 /* FALLTHROUGH */
884 bad:
885 m_freem(m);
886 goto done;
889 static int
890 copyexthdr(void *h, struct mbuf **mp)
892 struct ip6_ext *hdr = h;
893 int hlen;
894 struct mbuf *m;
896 if (hdr == NULL)
897 return 0;
899 hlen = (hdr->ip6e_len + 1) * 8;
900 if (hlen > MCLBYTES)
901 return ENOBUFS; /* XXX */
903 m = m_getb(hlen, M_NOWAIT, MT_DATA, 0);
904 if (!m)
905 return ENOBUFS;
906 m->m_len = hlen;
908 bcopy(hdr, mtod(m, caddr_t), hlen);
910 *mp = m;
911 return 0;
915 * Insert jumbo payload option.
917 static int
918 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
920 struct mbuf *mopt;
921 u_char *optbuf;
922 u_int32_t v;
924 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
927 * If there is no hop-by-hop options header, allocate new one.
928 * If there is one but it doesn't have enough space to store the
929 * jumbo payload option, allocate a cluster to store the whole options.
930 * Otherwise, use it to store the options.
932 if (exthdrs->ip6e_hbh == NULL) {
933 MGET(mopt, M_NOWAIT, MT_DATA);
934 if (mopt == NULL)
935 return (ENOBUFS);
936 mopt->m_len = JUMBOOPTLEN;
937 optbuf = mtod(mopt, u_char *);
938 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
939 exthdrs->ip6e_hbh = mopt;
940 } else {
941 struct ip6_hbh *hbh;
943 mopt = exthdrs->ip6e_hbh;
944 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
946 * XXX assumption:
947 * - exthdrs->ip6e_hbh is not referenced from places
948 * other than exthdrs.
949 * - exthdrs->ip6e_hbh is not an mbuf chain.
951 int oldoptlen = mopt->m_len;
952 struct mbuf *n;
955 * XXX: give up if the whole (new) hbh header does
956 * not fit even in an mbuf cluster.
958 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
959 return (ENOBUFS);
962 * As a consequence, we must always prepare a cluster
963 * at this point.
965 n = m_getcl(M_NOWAIT, MT_DATA, 0);
966 if (!n)
967 return (ENOBUFS);
968 n->m_len = oldoptlen + JUMBOOPTLEN;
969 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t), oldoptlen);
970 optbuf = mtod(n, caddr_t) + oldoptlen;
971 m_freem(mopt);
972 mopt = exthdrs->ip6e_hbh = n;
973 } else {
974 optbuf = mtod(mopt, u_char *) + mopt->m_len;
975 mopt->m_len += JUMBOOPTLEN;
977 optbuf[0] = IP6OPT_PADN;
978 optbuf[1] = 1;
981 * Adjust the header length according to the pad and
982 * the jumbo payload option.
984 hbh = mtod(mopt, struct ip6_hbh *);
985 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
988 /* fill in the option. */
989 optbuf[2] = IP6OPT_JUMBO;
990 optbuf[3] = 4;
991 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
992 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
994 /* finally, adjust the packet header length */
995 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
997 return (0);
998 #undef JUMBOOPTLEN
1002 * Insert fragment header and copy unfragmentable header portions.
1004 static int
1005 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1006 struct ip6_frag **frghdrp)
1008 struct mbuf *n, *mlast;
1010 if (hlen > sizeof(struct ip6_hdr)) {
1011 n = m_copym(m0, sizeof(struct ip6_hdr),
1012 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1013 if (n == NULL)
1014 return (ENOBUFS);
1015 m->m_next = n;
1016 } else
1017 n = m;
1019 /* Search for the last mbuf of unfragmentable part. */
1020 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1023 if (!(mlast->m_flags & M_EXT) &&
1024 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1025 /* use the trailing space of the last mbuf for the fragment hdr */
1026 *frghdrp = (struct ip6_frag *)
1027 (mtod(mlast, caddr_t) + mlast->m_len);
1028 mlast->m_len += sizeof(struct ip6_frag);
1029 m->m_pkthdr.len += sizeof(struct ip6_frag);
1030 } else {
1031 /* allocate a new mbuf for the fragment header */
1032 struct mbuf *mfrg;
1034 MGET(mfrg, M_NOWAIT, MT_DATA);
1035 if (mfrg == NULL)
1036 return (ENOBUFS);
1037 mfrg->m_len = sizeof(struct ip6_frag);
1038 *frghdrp = mtod(mfrg, struct ip6_frag *);
1039 mlast->m_next = mfrg;
1042 return (0);
1045 static int
1046 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1047 struct ifnet *ifp, struct in6_addr *dst, u_long *mtup,
1048 int *alwaysfragp)
1050 u_int32_t mtu = 0;
1051 int alwaysfrag = 0;
1052 int error = 0;
1054 if (ro_pmtu != ro) {
1055 /* The first hop and the final destination may differ. */
1056 struct sockaddr_in6 *sa6_dst =
1057 (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1058 if (ro_pmtu->ro_rt &&
1059 ((ro_pmtu->ro_rt->rt_flags & RTF_UP) == 0 ||
1060 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))) {
1061 RTFREE(ro_pmtu->ro_rt);
1062 ro_pmtu->ro_rt = NULL;
1064 if (ro_pmtu->ro_rt == NULL) {
1065 bzero(sa6_dst, sizeof(*sa6_dst));
1066 sa6_dst->sin6_family = AF_INET6;
1067 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1068 sa6_dst->sin6_addr = *dst;
1070 rtalloc((struct route *)ro_pmtu);
1073 if (ro_pmtu->ro_rt) {
1074 u_int32_t ifmtu;
1076 if (ifp == NULL)
1077 ifp = ro_pmtu->ro_rt->rt_ifp;
1078 ifmtu = IN6_LINKMTU(ifp);
1079 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
1080 if (mtu == 0) {
1081 mtu = ifmtu;
1082 } else if (mtu < IPV6_MMTU) {
1084 * RFC2460 section 5, last paragraph:
1085 * if we record ICMPv6 too big message with
1086 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1087 * or smaller, with framgent header attached.
1088 * (fragment header is needed regardless from the
1089 * packet size, for translators to identify packets)
1091 alwaysfrag = 1;
1092 mtu = IPV6_MMTU;
1093 } else if (mtu > ifmtu) {
1095 * The MTU on the route is larger than the MTU on
1096 * the interface! This shouldn't happen, unless the
1097 * MTU of the interface has been changed after the
1098 * interface was brought up. Change the MTU in the
1099 * route to match the interface MTU (as long as the
1100 * field isn't locked).
1102 mtu = ifmtu;
1103 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu;
1105 } else if (ifp) {
1106 mtu = IN6_LINKMTU(ifp);
1107 } else {
1108 error = EHOSTUNREACH; /* XXX */
1111 *mtup = mtu;
1112 if (alwaysfragp)
1113 *alwaysfragp = alwaysfrag;
1114 return (error);
1118 * IP6 socket option processing.
1120 void
1121 ip6_ctloutput_dispatch(netmsg_t msg)
1123 int error;
1125 error = ip6_ctloutput(msg->ctloutput.base.nm_so,
1126 msg->ctloutput.nm_sopt);
1127 lwkt_replymsg(&msg->ctloutput.base.lmsg, error);
1131 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1133 int optdatalen,uproto;
1134 int privileged;
1135 struct inpcb *in6p = so->so_pcb;
1136 void *optdata;
1137 int error, optval;
1138 int level, op, optname;
1139 int optlen;
1140 struct thread *td;
1142 if (sopt) {
1143 level = sopt->sopt_level;
1144 op = sopt->sopt_dir;
1145 optname = sopt->sopt_name;
1146 optlen = sopt->sopt_valsize;
1147 td = sopt->sopt_td;
1148 } else {
1149 panic("ip6_ctloutput: arg soopt is NULL");
1150 /* NOT REACHED */
1151 td = NULL;
1153 error = optval = 0;
1155 uproto = (int)so->so_proto->pr_protocol;
1156 privileged = (td == NULL ||
1157 caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT)) ?
1158 0 : 1;
1160 if (level == IPPROTO_IPV6) {
1161 switch (op) {
1163 case SOPT_SET:
1164 switch (optname) {
1165 case IPV6_2292PKTOPTIONS:
1166 #ifdef IPV6_PKTOPTIONS
1167 case IPV6_PKTOPTIONS:
1168 #endif
1170 struct mbuf *m;
1172 error = soopt_getm(sopt, &m); /* XXX */
1173 if (error != 0)
1174 break;
1175 soopt_to_mbuf(sopt, m); /* XXX */
1176 error = ip6_pcbopts(&in6p->in6p_outputopts,
1177 m, so, sopt);
1178 m_freem(m); /* XXX */
1179 break;
1183 * Use of some Hop-by-Hop options or some
1184 * Destination options, might require special
1185 * privilege. That is, normal applications
1186 * (without special privilege) might be forbidden
1187 * from setting certain options in outgoing packets,
1188 * and might never see certain options in received
1189 * packets. [RFC 2292 Section 6]
1190 * KAME specific note:
1191 * KAME prevents non-privileged users from sending or
1192 * receiving ANY hbh/dst options in order to avoid
1193 * overhead of parsing options in the kernel.
1195 case IPV6_RECVHOPOPTS:
1196 case IPV6_RECVDSTOPTS:
1197 case IPV6_RECVRTHDRDSTOPTS:
1198 if (!privileged)
1199 return (EPERM);
1200 case IPV6_RECVPKTINFO:
1201 case IPV6_RECVHOPLIMIT:
1202 case IPV6_RECVRTHDR:
1203 case IPV6_RECVPATHMTU:
1204 case IPV6_RECVTCLASS:
1205 case IPV6_AUTOFLOWLABEL:
1206 case IPV6_HOPLIMIT:
1207 /* FALLTHROUGH */
1208 case IPV6_UNICAST_HOPS:
1210 case IPV6_V6ONLY:
1211 if (optlen != sizeof(int)) {
1212 error = EINVAL;
1213 break;
1215 error = soopt_to_kbuf(sopt, &optval,
1216 sizeof optval, sizeof optval);
1217 if (error)
1218 break;
1219 switch (optname) {
1221 case IPV6_UNICAST_HOPS:
1222 if (optval < -1 || optval >= 256)
1223 error = EINVAL;
1224 else {
1225 /* -1 = kernel default */
1226 in6p->in6p_hops = optval;
1228 break;
1229 #define OPTSET(bit) \
1230 do { \
1231 if (optval) \
1232 in6p->in6p_flags |= (bit); \
1233 else \
1234 in6p->in6p_flags &= ~(bit); \
1235 } while (0)
1236 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1238 * Although changed to RFC3542, It's better to also support RFC2292 API
1240 #define OPTSET2292(bit) \
1241 do { \
1242 in6p->in6p_flags |= IN6P_RFC2292; \
1243 if (optval) \
1244 in6p->in6p_flags |= (bit); \
1245 else \
1246 in6p->in6p_flags &= ~(bit); \
1247 } while (/*CONSTCOND*/ 0)
1249 case IPV6_RECVPKTINFO:
1250 /* cannot mix with RFC2292 */
1251 if (OPTBIT(IN6P_RFC2292)) {
1252 error = EINVAL;
1253 break;
1255 OPTSET(IN6P_PKTINFO);
1256 break;
1258 case IPV6_HOPLIMIT:
1260 struct ip6_pktopts **optp;
1262 /* cannot mix with RFC2292 */
1263 if (OPTBIT(IN6P_RFC2292)) {
1264 error = EINVAL;
1265 break;
1267 optp = &in6p->in6p_outputopts;
1268 error = ip6_pcbopt(IPV6_HOPLIMIT,
1269 (u_char *)&optval, sizeof(optval),
1270 optp, uproto);
1271 break;
1274 case IPV6_RECVHOPLIMIT:
1275 /* cannot mix with RFC2292 */
1276 if (OPTBIT(IN6P_RFC2292)) {
1277 error = EINVAL;
1278 break;
1280 OPTSET(IN6P_HOPLIMIT);
1281 break;
1283 case IPV6_RECVHOPOPTS:
1284 /* cannot mix with RFC2292 */
1285 if (OPTBIT(IN6P_RFC2292)) {
1286 error = EINVAL;
1287 break;
1289 OPTSET(IN6P_HOPOPTS);
1290 break;
1292 case IPV6_RECVDSTOPTS:
1293 /* cannot mix with RFC2292 */
1294 if (OPTBIT(IN6P_RFC2292)) {
1295 error = EINVAL;
1296 break;
1298 OPTSET(IN6P_DSTOPTS);
1299 break;
1301 case IPV6_RECVRTHDRDSTOPTS:
1302 /* cannot mix with RFC2292 */
1303 if (OPTBIT(IN6P_RFC2292)) {
1304 error = EINVAL;
1305 break;
1307 OPTSET(IN6P_RTHDRDSTOPTS);
1308 break;
1310 case IPV6_RECVRTHDR:
1311 /* cannot mix with RFC2292 */
1312 if (OPTBIT(IN6P_RFC2292)) {
1313 error = EINVAL;
1314 break;
1316 OPTSET(IN6P_RTHDR);
1317 break;
1319 case IPV6_RECVPATHMTU:
1321 * We ignore this option for TCP
1322 * sockets.
1323 * (RFC3542 leaves this case
1324 * unspecified.)
1326 if (uproto != IPPROTO_TCP)
1327 OPTSET(IN6P_MTU);
1328 break;
1330 case IPV6_RECVTCLASS:
1331 /* cannot mix with RFC2292 XXX */
1332 if (OPTBIT(IN6P_RFC2292)) {
1333 error = EINVAL;
1334 break;
1336 OPTSET(IN6P_TCLASS);
1337 break;
1339 case IPV6_AUTOFLOWLABEL:
1340 OPTSET(IN6P_AUTOFLOWLABEL);
1341 break;
1343 case IPV6_V6ONLY:
1345 * make setsockopt(IPV6_V6ONLY)
1346 * available only prior to bind(2).
1348 if (in6p->in6p_lport ||
1349 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1351 error = EINVAL;
1352 break;
1356 * Since we don't support v6->v4
1357 * mapping any more this option does
1358 * nothing. But apparently some
1359 * ports and libraries (e.g. libuv)
1360 * actually try to set the value to
1361 * 0 so just silently ignore the value
1362 * entirely.
1364 * (also fixes named which uses libuv,
1365 * and a few other apps)
1367 #if 0
1368 if (!optval) {
1369 /* Don't allow v4-mapped */
1370 error = EOPNOTSUPP;
1372 #endif
1373 break;
1375 break;
1377 case IPV6_TCLASS:
1378 case IPV6_DONTFRAG:
1379 case IPV6_USE_MIN_MTU:
1380 case IPV6_PREFER_TEMPADDR:
1381 if (optlen != sizeof(optval)) {
1382 error = EINVAL;
1383 break;
1385 error = soopt_to_kbuf(sopt, &optval,
1386 sizeof optval, sizeof optval);
1387 if (error)
1388 break;
1390 struct ip6_pktopts **optp;
1391 optp = &in6p->in6p_outputopts;
1392 error = ip6_pcbopt(optname,
1393 (u_char *)&optval, sizeof(optval),
1394 optp, uproto);
1395 break;
1398 case IPV6_2292PKTINFO:
1399 case IPV6_2292HOPLIMIT:
1400 case IPV6_2292HOPOPTS:
1401 case IPV6_2292DSTOPTS:
1402 case IPV6_2292RTHDR:
1403 /* RFC 2292 */
1404 if (optlen != sizeof(int)) {
1405 error = EINVAL;
1406 break;
1408 error = soopt_to_kbuf(sopt, &optval,
1409 sizeof optval, sizeof optval);
1410 if (error)
1411 break;
1412 switch (optname) {
1413 case IPV6_2292PKTINFO:
1414 OPTSET2292(IN6P_PKTINFO);
1415 break;
1416 case IPV6_2292HOPLIMIT:
1417 OPTSET2292(IN6P_HOPLIMIT);
1418 break;
1419 case IPV6_2292HOPOPTS:
1421 * Check super-user privilege.
1422 * See comments for IPV6_RECVHOPOPTS.
1424 if (!privileged)
1425 return (EPERM);
1426 OPTSET2292(IN6P_HOPOPTS);
1427 break;
1428 case IPV6_2292DSTOPTS:
1429 if (!privileged)
1430 return (EPERM);
1431 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1432 break;
1433 case IPV6_2292RTHDR:
1434 OPTSET2292(IN6P_RTHDR);
1435 break;
1437 break;
1439 case IPV6_PKTINFO:
1440 case IPV6_HOPOPTS:
1441 case IPV6_RTHDR:
1442 case IPV6_DSTOPTS:
1443 case IPV6_RTHDRDSTOPTS:
1444 case IPV6_NEXTHOP:
1447 * New advanced API (RFC3542)
1449 u_char *optbuf;
1450 u_char optbuf_storage[MCLBYTES];
1451 int optlen;
1452 struct ip6_pktopts **optp;
1454 /* cannot mix with RFC2292 */
1455 if (OPTBIT(IN6P_RFC2292)) {
1456 error = EINVAL;
1457 break;
1461 * We only ensure valsize is not too large
1462 * here. Further validation will be done
1463 * later.
1465 error = soopt_to_kbuf(sopt, optbuf_storage,
1466 sizeof(optbuf_storage), 0);
1467 if (error)
1468 break;
1469 optlen = sopt->sopt_valsize;
1470 optbuf = optbuf_storage;
1471 optp = &in6p->in6p_outputopts;
1472 error = ip6_pcbopt(optname, optbuf, optlen,
1473 optp, uproto);
1474 break;
1476 #undef OPTSET
1478 case IPV6_MULTICAST_IF:
1479 case IPV6_MULTICAST_HOPS:
1480 case IPV6_MULTICAST_LOOP:
1481 case IPV6_JOIN_GROUP:
1482 case IPV6_LEAVE_GROUP:
1484 struct mbuf *m;
1486 if (sopt->sopt_valsize > MLEN) {
1487 error = EMSGSIZE;
1488 break;
1490 /* XXX */
1491 MGET(m, sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_HEADER);
1492 if (m == NULL) {
1493 error = ENOBUFS;
1494 break;
1496 m->m_len = sopt->sopt_valsize;
1497 error = soopt_to_kbuf(sopt, mtod(m, char *),
1498 m->m_len, m->m_len);
1499 error = ip6_setmoptions(sopt->sopt_name,
1500 &in6p->in6p_moptions,
1502 m_free(m);
1504 break;
1506 case IPV6_PORTRANGE:
1507 error = soopt_to_kbuf(sopt, &optval,
1508 sizeof optval, sizeof optval);
1509 if (error)
1510 break;
1512 switch (optval) {
1513 case IPV6_PORTRANGE_DEFAULT:
1514 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1515 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1516 break;
1518 case IPV6_PORTRANGE_HIGH:
1519 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1520 in6p->in6p_flags |= IN6P_HIGHPORT;
1521 break;
1523 case IPV6_PORTRANGE_LOW:
1524 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1525 in6p->in6p_flags |= IN6P_LOWPORT;
1526 break;
1528 default:
1529 error = EINVAL;
1530 break;
1532 break;
1534 case IPV6_FW_ADD:
1535 case IPV6_FW_DEL:
1536 case IPV6_FW_FLUSH:
1537 case IPV6_FW_ZERO:
1539 struct mbuf *m;
1540 struct mbuf **mp = &m;
1542 if (ip6_fw_ctl_ptr == NULL)
1543 return EINVAL;
1544 /* XXX */
1545 if ((error = soopt_getm(sopt, &m)) != 0)
1546 break;
1547 /* XXX */
1548 soopt_to_mbuf(sopt, m);
1549 error = (*ip6_fw_ctl_ptr)(optname, mp);
1550 m = *mp;
1552 break;
1554 default:
1555 error = ENOPROTOOPT;
1556 break;
1558 break;
1560 case SOPT_GET:
1561 switch (optname) {
1562 case IPV6_2292PKTOPTIONS:
1563 #ifdef IPV6_PKTOPTIONS
1564 case IPV6_PKTOPTIONS:
1565 #endif
1567 * RFC3542 (effectively) deprecated the
1568 * semantics of the 2292-style pktoptions.
1569 * Since it was not reliable in nature (i.e.,
1570 * applications had to expect the lack of some
1571 * information after all), it would make sense
1572 * to simplify this part by always returning
1573 * empty data.
1575 if (in6p->in6p_options) {
1576 struct mbuf *m;
1577 m = m_copym(in6p->in6p_options,
1578 0, M_COPYALL, M_WAITOK);
1579 error = soopt_from_mbuf(sopt, m);
1580 if (error == 0)
1581 m_freem(m);
1582 } else
1583 sopt->sopt_valsize = 0;
1584 break;
1586 case IPV6_RECVHOPOPTS:
1587 case IPV6_RECVDSTOPTS:
1588 case IPV6_RECVRTHDRDSTOPTS:
1589 case IPV6_UNICAST_HOPS:
1590 case IPV6_RECVPKTINFO:
1591 case IPV6_RECVHOPLIMIT:
1592 case IPV6_RECVRTHDR:
1593 case IPV6_RECVPATHMTU:
1594 case IPV6_RECVTCLASS:
1595 case IPV6_AUTOFLOWLABEL:
1596 case IPV6_V6ONLY:
1597 case IPV6_PORTRANGE:
1598 switch (optname) {
1600 case IPV6_RECVHOPOPTS:
1601 optval = OPTBIT(IN6P_HOPOPTS);
1602 break;
1604 case IPV6_RECVDSTOPTS:
1605 optval = OPTBIT(IN6P_DSTOPTS);
1606 break;
1608 case IPV6_RECVRTHDRDSTOPTS:
1609 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1610 break;
1612 case IPV6_RECVPKTINFO:
1613 optval = OPTBIT(IN6P_PKTINFO);
1614 break;
1616 case IPV6_RECVHOPLIMIT:
1617 optval = OPTBIT(IN6P_HOPLIMIT);
1618 break;
1620 case IPV6_RECVRTHDR:
1621 optval = OPTBIT(IN6P_RTHDR);
1622 break;
1624 case IPV6_RECVPATHMTU:
1625 optval = OPTBIT(IN6P_MTU);
1626 break;
1628 case IPV6_RECVTCLASS:
1629 optval = OPTBIT(IN6P_TCLASS);
1630 break;
1632 case IPV6_AUTOFLOWLABEL:
1633 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
1634 break;
1637 case IPV6_UNICAST_HOPS:
1638 optval = in6p->in6p_hops;
1639 break;
1641 case IPV6_V6ONLY:
1642 optval = 1;
1643 break;
1645 case IPV6_PORTRANGE:
1647 int flags;
1648 flags = in6p->in6p_flags;
1649 if (flags & IN6P_HIGHPORT)
1650 optval = IPV6_PORTRANGE_HIGH;
1651 else if (flags & IN6P_LOWPORT)
1652 optval = IPV6_PORTRANGE_LOW;
1653 else
1654 optval = 0;
1655 break;
1658 soopt_from_kbuf(sopt, &optval,
1659 sizeof optval);
1660 break;
1662 case IPV6_PATHMTU:
1664 u_long pmtu = 0;
1665 struct ip6_mtuinfo mtuinfo;
1666 struct route_in6 sro;
1668 bzero(&sro, sizeof(sro));
1670 if (!(so->so_state & SS_ISCONNECTED))
1671 return (ENOTCONN);
1673 * XXX: we dot not consider the case of source
1674 * routing, or optional information to specify
1675 * the outgoing interface.
1677 error = ip6_getpmtu(&sro, NULL, NULL,
1678 &in6p->in6p_faddr, &pmtu, NULL);
1679 if (sro.ro_rt)
1680 RTFREE(sro.ro_rt);
1681 if (error)
1682 break;
1683 if (pmtu > IPV6_MAXPACKET)
1684 pmtu = IPV6_MAXPACKET;
1686 bzero(&mtuinfo, sizeof(mtuinfo));
1687 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1688 optdata = (void *)&mtuinfo;
1689 optdatalen = sizeof(mtuinfo);
1690 soopt_from_kbuf(sopt, optdata,
1691 optdatalen);
1692 break;
1695 case IPV6_2292PKTINFO:
1696 case IPV6_2292HOPLIMIT:
1697 case IPV6_2292HOPOPTS:
1698 case IPV6_2292RTHDR:
1699 case IPV6_2292DSTOPTS:
1700 if (optname == IPV6_2292HOPOPTS ||
1701 optname == IPV6_2292DSTOPTS ||
1702 !privileged)
1703 return (EPERM);
1704 switch (optname) {
1705 case IPV6_2292PKTINFO:
1706 optval = OPTBIT(IN6P_PKTINFO);
1707 break;
1708 case IPV6_2292HOPLIMIT:
1709 optval = OPTBIT(IN6P_HOPLIMIT);
1710 break;
1711 case IPV6_2292HOPOPTS:
1712 if (!privileged)
1713 return (EPERM);
1714 optval = OPTBIT(IN6P_HOPOPTS);
1715 break;
1716 case IPV6_2292RTHDR:
1717 optval = OPTBIT(IN6P_RTHDR);
1718 break;
1719 case IPV6_2292DSTOPTS:
1720 if (!privileged)
1721 return (EPERM);
1722 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1723 break;
1725 soopt_from_kbuf(sopt, &optval,
1726 sizeof optval);
1727 break;
1729 case IPV6_PKTINFO:
1730 case IPV6_HOPOPTS:
1731 case IPV6_RTHDR:
1732 case IPV6_DSTOPTS:
1733 case IPV6_RTHDRDSTOPTS:
1734 case IPV6_NEXTHOP:
1735 case IPV6_TCLASS:
1736 case IPV6_DONTFRAG:
1737 case IPV6_USE_MIN_MTU:
1738 case IPV6_PREFER_TEMPADDR:
1739 error = ip6_getpcbopt(in6p->in6p_outputopts,
1740 optname, sopt);
1741 break;
1743 case IPV6_MULTICAST_IF:
1744 case IPV6_MULTICAST_HOPS:
1745 case IPV6_MULTICAST_LOOP:
1746 case IPV6_JOIN_GROUP:
1747 case IPV6_LEAVE_GROUP:
1749 struct mbuf *m;
1750 error = ip6_getmoptions(sopt->sopt_name,
1751 in6p->in6p_moptions, &m);
1752 if (error == 0) {
1753 soopt_from_kbuf(sopt,
1754 mtod(m, char *), m->m_len);
1756 m_freem(m);
1758 break;
1760 case IPV6_FW_GET:
1762 struct mbuf *m;
1763 struct mbuf **mp = &m;
1765 if (ip6_fw_ctl_ptr == NULL)
1767 return EINVAL;
1769 error = (*ip6_fw_ctl_ptr)(optname, mp);
1770 if (error == 0)
1771 error = soopt_from_mbuf(sopt, m); /* XXX */
1772 if (error == 0 && m != NULL)
1773 m_freem(m);
1775 break;
1777 default:
1778 error = ENOPROTOOPT;
1779 break;
1781 break;
1783 } else {
1784 error = EINVAL;
1786 return (error);
1790 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1792 int error = 0, optval, optlen;
1793 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1794 struct in6pcb *in6p = sotoin6pcb(so);
1795 int level, op, optname;
1797 if (sopt) {
1798 level = sopt->sopt_level;
1799 op = sopt->sopt_dir;
1800 optname = sopt->sopt_name;
1801 optlen = sopt->sopt_valsize;
1802 } else
1803 panic("ip6_raw_ctloutput: arg soopt is NULL");
1805 if (level != IPPROTO_IPV6) {
1806 return (EINVAL);
1809 switch (optname) {
1810 case IPV6_CHECKSUM:
1812 * For ICMPv6 sockets, no modification allowed for checksum
1813 * offset, permit "no change" values to help existing apps.
1815 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
1816 * for an ICMPv6 socket will fail."
1817 * The current behavior does not meet RFC3542.
1819 switch (op) {
1820 case SOPT_SET:
1821 if (optlen != sizeof(int)) {
1822 error = EINVAL;
1823 break;
1825 error = soopt_to_kbuf(sopt, &optval,
1826 sizeof optval, sizeof optval);
1827 if (error)
1828 break;
1829 if ((optval % 2) != 0) {
1830 /* the API assumes even offset values */
1831 error = EINVAL;
1832 } else if (so->so_proto->pr_protocol ==
1833 IPPROTO_ICMPV6) {
1834 if (optval != icmp6off)
1835 error = EINVAL;
1836 } else
1837 in6p->in6p_cksum = optval;
1838 break;
1840 case SOPT_GET:
1841 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
1842 optval = icmp6off;
1843 else
1844 optval = in6p->in6p_cksum;
1846 soopt_from_kbuf(sopt, &optval, sizeof(optval));
1847 break;
1849 default:
1850 error = EINVAL;
1851 break;
1853 break;
1855 default:
1856 error = ENOPROTOOPT;
1857 break;
1860 return (error);
1864 * Set up IP6 options in pcb for insertion in output packets or
1865 * specifying behavior of outgoing packets.
1867 static int
1868 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
1869 struct socket *so, struct sockopt *sopt)
1871 int priv = 0;
1872 struct ip6_pktopts *opt = *pktopt;
1873 int error = 0;
1875 /* turn off any old options. */
1876 if (opt) {
1877 #ifdef DIAGNOSTIC
1878 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1879 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1880 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1881 kprintf("ip6_pcbopts: all specified options are cleared.\n");
1882 #endif
1883 ip6_clearpktopts(opt, -1);
1884 } else
1885 opt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
1886 *pktopt = NULL;
1888 if (!m || m->m_len == 0) {
1890 * Only turning off any previous options, regardless of
1891 * whether the opt is just created or given.
1893 kfree(opt, M_IP6OPT);
1894 return (0);
1897 /* set options specified by user. */
1898 if ((error = ip6_setpktoptions(m, opt, NULL, so->so_proto->pr_protocol, priv)) != 0) {
1899 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
1900 kfree(opt, M_IP6OPT);
1901 return (error);
1903 *pktopt = opt;
1904 return (0);
1909 * Below three functions are introduced by merge to RFC3542
1912 static int
1913 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
1915 void *optdata = NULL;
1916 int optdatalen = 0;
1917 struct ip6_ext *ip6e;
1918 int error = 0;
1919 struct in6_pktinfo null_pktinfo;
1920 int deftclass = 0, on;
1921 int defminmtu = IP6PO_MINMTU_MCASTONLY;
1922 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
1924 switch (optname) {
1925 case IPV6_PKTINFO:
1926 if (pktopt && pktopt->ip6po_pktinfo)
1927 optdata = (void *)pktopt->ip6po_pktinfo;
1928 else {
1929 /* XXX: we don't have to do this every time... */
1930 bzero(&null_pktinfo, sizeof(null_pktinfo));
1931 optdata = (void *)&null_pktinfo;
1933 optdatalen = sizeof(struct in6_pktinfo);
1934 break;
1935 case IPV6_TCLASS:
1936 if (pktopt && pktopt->ip6po_tclass >= 0)
1937 optdata = (void *)&pktopt->ip6po_tclass;
1938 else
1939 optdata = (void *)&deftclass;
1940 optdatalen = sizeof(int);
1941 break;
1942 case IPV6_HOPOPTS:
1943 if (pktopt && pktopt->ip6po_hbh) {
1944 optdata = (void *)pktopt->ip6po_hbh;
1945 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
1946 optdatalen = (ip6e->ip6e_len + 1) << 3;
1948 break;
1949 case IPV6_RTHDR:
1950 if (pktopt && pktopt->ip6po_rthdr) {
1951 optdata = (void *)pktopt->ip6po_rthdr;
1952 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
1953 optdatalen = (ip6e->ip6e_len + 1) << 3;
1955 break;
1956 case IPV6_RTHDRDSTOPTS:
1957 if (pktopt && pktopt->ip6po_dest1) {
1958 optdata = (void *)pktopt->ip6po_dest1;
1959 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
1960 optdatalen = (ip6e->ip6e_len + 1) << 3;
1962 break;
1963 case IPV6_DSTOPTS:
1964 if (pktopt && pktopt->ip6po_dest2) {
1965 optdata = (void *)pktopt->ip6po_dest2;
1966 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
1967 optdatalen = (ip6e->ip6e_len + 1) << 3;
1969 break;
1970 case IPV6_NEXTHOP:
1971 if (pktopt && pktopt->ip6po_nexthop) {
1972 optdata = (void *)pktopt->ip6po_nexthop;
1973 optdatalen = pktopt->ip6po_nexthop->sa_len;
1975 break;
1976 case IPV6_USE_MIN_MTU:
1977 if (pktopt)
1978 optdata = (void *)&pktopt->ip6po_minmtu;
1979 else
1980 optdata = (void *)&defminmtu;
1981 optdatalen = sizeof(int);
1982 break;
1983 case IPV6_DONTFRAG:
1984 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
1985 on = 1;
1986 else
1987 on = 0;
1988 optdata = (void *)&on;
1989 optdatalen = sizeof(on);
1990 break;
1991 case IPV6_PREFER_TEMPADDR:
1992 if (pktopt)
1993 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
1994 else
1995 optdata = (void *)&defpreftemp;
1996 optdatalen = sizeof(int);
1997 break;
1998 default: /* should not happen */
1999 #ifdef DIAGNOSTIC
2000 panic("ip6_getpcbopt: unexpected option");
2001 #endif
2002 return (ENOPROTOOPT);
2005 soopt_from_kbuf(sopt, optdata, optdatalen);
2007 return (error);
2011 * initialize ip6_pktopts. beware that there are non-zero default values in
2012 * the struct.
2015 static int
2016 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt, int uproto)
2018 struct ip6_pktopts *opt;
2019 int priv =0;
2020 if (*pktopt == NULL) {
2021 *pktopt = kmalloc(sizeof(*opt), M_IP6OPT, M_WAITOK);
2022 init_ip6pktopts(*pktopt);
2024 opt = *pktopt;
2026 return (ip6_setpktoption(optname, buf, len, opt, 1, 0, uproto, priv));
2030 * initialize ip6_pktopts. beware that there are non-zero default values in
2031 * the struct.
2033 void
2034 init_ip6pktopts(struct ip6_pktopts *opt)
2037 bzero(opt, sizeof(*opt));
2038 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2039 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2040 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2041 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2044 void
2045 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2047 if (pktopt == NULL)
2048 return;
2050 if (optname == -1 || optname == IPV6_PKTINFO) {
2051 if (pktopt->ip6po_pktinfo)
2052 kfree(pktopt->ip6po_pktinfo, M_IP6OPT);
2053 pktopt->ip6po_pktinfo = NULL;
2055 if (optname == -1 || optname == IPV6_HOPLIMIT)
2056 pktopt->ip6po_hlim = -1;
2057 if (optname == -1 || optname == IPV6_TCLASS)
2058 pktopt->ip6po_tclass = -1;
2059 if (optname == -1 || optname == IPV6_NEXTHOP) {
2060 if (pktopt->ip6po_nextroute.ro_rt) {
2061 RTFREE(pktopt->ip6po_nextroute.ro_rt);
2062 pktopt->ip6po_nextroute.ro_rt = NULL;
2064 if (pktopt->ip6po_nexthop)
2065 kfree(pktopt->ip6po_nexthop, M_IP6OPT);
2066 pktopt->ip6po_nexthop = NULL;
2068 if (optname == -1 || optname == IPV6_HOPOPTS) {
2069 if (pktopt->ip6po_hbh)
2070 kfree(pktopt->ip6po_hbh, M_IP6OPT);
2071 pktopt->ip6po_hbh = NULL;
2073 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2074 if (pktopt->ip6po_dest1)
2075 kfree(pktopt->ip6po_dest1, M_IP6OPT);
2076 pktopt->ip6po_dest1 = NULL;
2078 if (optname == -1 || optname == IPV6_RTHDR) {
2079 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2080 kfree(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2081 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2082 if (pktopt->ip6po_route.ro_rt) {
2083 RTFREE(pktopt->ip6po_route.ro_rt);
2084 pktopt->ip6po_route.ro_rt = NULL;
2087 if (optname == -1 || optname == IPV6_DSTOPTS) {
2088 if (pktopt->ip6po_dest2)
2089 kfree(pktopt->ip6po_dest2, M_IP6OPT);
2090 pktopt->ip6po_dest2 = NULL;
2094 #define PKTOPT_EXTHDRCPY(type) \
2095 do {\
2096 if (src->type) {\
2097 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2098 dst->type = kmalloc(hlen, M_IP6OPT, canwait);\
2099 if (dst->type == NULL)\
2100 goto bad;\
2101 bcopy(src->type, dst->type, hlen);\
2103 } while (0)
2105 struct ip6_pktopts *
2106 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2108 struct ip6_pktopts *dst;
2110 if (src == NULL) {
2111 kprintf("ip6_clearpktopts: invalid argument\n");
2112 return (NULL);
2115 dst = kmalloc(sizeof(*dst), M_IP6OPT, canwait | M_ZERO);
2116 if (dst == NULL)
2117 return (NULL);
2119 dst->ip6po_hlim = src->ip6po_hlim;
2120 if (src->ip6po_pktinfo) {
2121 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2122 M_IP6OPT, canwait);
2123 if (dst->ip6po_pktinfo == NULL)
2124 goto bad;
2125 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2127 if (src->ip6po_nexthop) {
2128 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2129 M_IP6OPT, canwait);
2130 if (dst->ip6po_nexthop == NULL)
2131 goto bad;
2132 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2133 src->ip6po_nexthop->sa_len);
2135 PKTOPT_EXTHDRCPY(ip6po_hbh);
2136 PKTOPT_EXTHDRCPY(ip6po_dest1);
2137 PKTOPT_EXTHDRCPY(ip6po_dest2);
2138 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2139 return (dst);
2141 bad:
2142 if (dst->ip6po_pktinfo) kfree(dst->ip6po_pktinfo, M_IP6OPT);
2143 if (dst->ip6po_nexthop) kfree(dst->ip6po_nexthop, M_IP6OPT);
2144 if (dst->ip6po_hbh) kfree(dst->ip6po_hbh, M_IP6OPT);
2145 if (dst->ip6po_dest1) kfree(dst->ip6po_dest1, M_IP6OPT);
2146 if (dst->ip6po_dest2) kfree(dst->ip6po_dest2, M_IP6OPT);
2147 if (dst->ip6po_rthdr) kfree(dst->ip6po_rthdr, M_IP6OPT);
2148 kfree(dst, M_IP6OPT);
2149 return (NULL);
2152 static int
2153 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2155 if (dst == NULL || src == NULL) {
2156 #ifdef DIAGNOSTIC
2157 kprintf("ip6_clearpktopts: invalid argument\n");
2158 #endif
2159 return (EINVAL);
2162 dst->ip6po_hlim = src->ip6po_hlim;
2163 dst->ip6po_tclass = src->ip6po_tclass;
2164 dst->ip6po_flags = src->ip6po_flags;
2165 if (src->ip6po_pktinfo) {
2166 dst->ip6po_pktinfo = kmalloc(sizeof(*dst->ip6po_pktinfo),
2167 M_IP6OPT, canwait);
2168 if (dst->ip6po_pktinfo == NULL)
2169 goto bad;
2170 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2172 if (src->ip6po_nexthop) {
2173 dst->ip6po_nexthop = kmalloc(src->ip6po_nexthop->sa_len,
2174 M_IP6OPT, canwait);
2175 if (dst->ip6po_nexthop == NULL)
2176 goto bad;
2177 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2178 src->ip6po_nexthop->sa_len);
2180 PKTOPT_EXTHDRCPY(ip6po_hbh);
2181 PKTOPT_EXTHDRCPY(ip6po_dest1);
2182 PKTOPT_EXTHDRCPY(ip6po_dest2);
2183 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2184 return (0);
2186 bad:
2187 ip6_clearpktopts(dst, -1);
2188 return (ENOBUFS);
2190 #undef PKTOPT_EXTHDRCPY
2192 void
2193 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2195 if (pktopt == NULL)
2196 return;
2198 ip6_clearpktopts(pktopt, -1);
2200 kfree(pktopt, M_IP6OPT);
2204 * Set the IP6 multicast options in response to user setsockopt().
2206 static int
2207 ip6_setmoptions(int optname, struct ip6_moptions **im6op, struct mbuf *m)
2209 int error = 0;
2210 u_int loop, ifindex;
2211 struct ipv6_mreq *mreq;
2212 struct ifnet *ifp;
2213 struct ip6_moptions *im6o = *im6op;
2214 struct route_in6 ro;
2215 struct sockaddr_in6 *dst;
2216 struct in6_multi_mship *imm;
2218 if (im6o == NULL) {
2220 * No multicast option buffer attached to the pcb;
2221 * allocate one and initialize to default values.
2223 im6o = (struct ip6_moptions *)
2224 kmalloc(sizeof(*im6o), M_IPMOPTS, M_WAITOK);
2226 *im6op = im6o;
2227 im6o->im6o_multicast_ifp = NULL;
2228 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2229 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2230 LIST_INIT(&im6o->im6o_memberships);
2233 switch (optname) {
2235 case IPV6_MULTICAST_IF:
2237 * Select the interface for outgoing multicast packets.
2239 if (m == NULL || m->m_len != sizeof(u_int)) {
2240 error = EINVAL;
2241 break;
2243 bcopy(mtod(m, u_int *), &ifindex, sizeof(ifindex));
2244 if (ifindex < 0 || if_index < ifindex) {
2245 error = ENXIO; /* XXX EINVAL? */
2246 break;
2248 ifp = ifindex2ifnet[ifindex];
2249 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2250 error = EADDRNOTAVAIL;
2251 break;
2253 im6o->im6o_multicast_ifp = ifp;
2254 break;
2256 case IPV6_MULTICAST_HOPS:
2259 * Set the IP6 hoplimit for outgoing multicast packets.
2261 int optval;
2262 if (m == NULL || m->m_len != sizeof(int)) {
2263 error = EINVAL;
2264 break;
2266 bcopy(mtod(m, u_int *), &optval, sizeof(optval));
2267 if (optval < -1 || optval >= 256)
2268 error = EINVAL;
2269 else if (optval == -1)
2270 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2271 else
2272 im6o->im6o_multicast_hlim = optval;
2273 break;
2276 case IPV6_MULTICAST_LOOP:
2278 * Set the loopback flag for outgoing multicast packets.
2279 * Must be zero or one.
2281 if (m == NULL || m->m_len != sizeof(u_int)) {
2282 error = EINVAL;
2283 break;
2285 bcopy(mtod(m, u_int *), &loop, sizeof(loop));
2286 if (loop > 1) {
2287 error = EINVAL;
2288 break;
2290 im6o->im6o_multicast_loop = loop;
2291 break;
2293 case IPV6_JOIN_GROUP:
2295 * Add a multicast group membership.
2296 * Group must be a valid IP6 multicast address.
2298 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2299 error = EINVAL;
2300 break;
2302 mreq = mtod(m, struct ipv6_mreq *);
2303 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2305 * We use the unspecified address to specify to accept
2306 * all multicast addresses. Only super user is allowed
2307 * to do this.
2309 if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT)) {
2310 error = EACCES;
2311 break;
2313 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2314 error = EINVAL;
2315 break;
2319 * If the interface is specified, validate it.
2321 if (mreq->ipv6mr_interface < 0
2322 || if_index < mreq->ipv6mr_interface) {
2323 error = ENXIO; /* XXX EINVAL? */
2324 break;
2327 * If no interface was explicitly specified, choose an
2328 * appropriate one according to the given multicast address.
2330 if (mreq->ipv6mr_interface == 0) {
2332 * If the multicast address is in node-local scope,
2333 * the interface should be a loopback interface.
2334 * Otherwise, look up the routing table for the
2335 * address, and choose the outgoing interface.
2336 * XXX: is it a good approach?
2338 if (IN6_IS_ADDR_MC_INTFACELOCAL(&mreq->ipv6mr_multiaddr)) {
2339 ifp = loif;
2340 } else {
2341 ro.ro_rt = NULL;
2342 dst = (struct sockaddr_in6 *)&ro.ro_dst;
2343 bzero(dst, sizeof(*dst));
2344 dst->sin6_len = sizeof(struct sockaddr_in6);
2345 dst->sin6_family = AF_INET6;
2346 dst->sin6_addr = mreq->ipv6mr_multiaddr;
2347 rtalloc((struct route *)&ro);
2348 if (ro.ro_rt == NULL) {
2349 error = EADDRNOTAVAIL;
2350 break;
2352 ifp = ro.ro_rt->rt_ifp;
2353 rtfree(ro.ro_rt);
2355 } else
2356 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2359 * See if we found an interface, and confirm that it
2360 * supports multicast
2362 if (ifp == NULL || !(ifp->if_flags & IFF_MULTICAST)) {
2363 error = EADDRNOTAVAIL;
2364 break;
2367 * Put interface index into the multicast address,
2368 * if the address has link-local scope.
2370 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2371 mreq->ipv6mr_multiaddr.s6_addr16[1]
2372 = htons(mreq->ipv6mr_interface);
2375 * See if the membership already exists.
2377 for (imm = im6o->im6o_memberships.lh_first;
2378 imm != NULL; imm = imm->i6mm_chain.le_next)
2379 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2380 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2381 &mreq->ipv6mr_multiaddr))
2382 break;
2383 if (imm != NULL) {
2384 error = EADDRINUSE;
2385 break;
2388 * Everything looks good; add a new record to the multicast
2389 * address list for the given interface.
2391 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_WAITOK);
2392 if ((imm->i6mm_maddr =
2393 in6_addmulti(&mreq->ipv6mr_multiaddr, ifp, &error)) == NULL) {
2394 kfree(imm, M_IPMADDR);
2395 break;
2397 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2398 break;
2400 case IPV6_LEAVE_GROUP:
2402 * Drop a multicast group membership.
2403 * Group must be a valid IP6 multicast address.
2405 if (m == NULL || m->m_len != sizeof(struct ipv6_mreq)) {
2406 error = EINVAL;
2407 break;
2409 mreq = mtod(m, struct ipv6_mreq *);
2410 if (IN6_IS_ADDR_UNSPECIFIED(&mreq->ipv6mr_multiaddr)) {
2411 if (caps_priv_check_self(SYSCAP_RESTRICTEDROOT)) {
2412 error = EACCES;
2413 break;
2415 } else if (!IN6_IS_ADDR_MULTICAST(&mreq->ipv6mr_multiaddr)) {
2416 error = EINVAL;
2417 break;
2420 * If an interface address was specified, get a pointer
2421 * to its ifnet structure.
2423 if (mreq->ipv6mr_interface < 0
2424 || if_index < mreq->ipv6mr_interface) {
2425 error = ENXIO; /* XXX EINVAL? */
2426 break;
2428 ifp = ifindex2ifnet[mreq->ipv6mr_interface];
2430 * Put interface index into the multicast address,
2431 * if the address has link-local scope.
2433 if (IN6_IS_ADDR_MC_LINKLOCAL(&mreq->ipv6mr_multiaddr)) {
2434 mreq->ipv6mr_multiaddr.s6_addr16[1]
2435 = htons(mreq->ipv6mr_interface);
2439 * Find the membership in the membership list.
2441 for (imm = im6o->im6o_memberships.lh_first;
2442 imm != NULL; imm = imm->i6mm_chain.le_next) {
2443 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2444 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2445 &mreq->ipv6mr_multiaddr))
2446 break;
2448 if (imm == NULL) {
2449 /* Unable to resolve interface */
2450 error = EADDRNOTAVAIL;
2451 break;
2454 * Give up the multicast address record to which the
2455 * membership points.
2457 LIST_REMOVE(imm, i6mm_chain);
2458 in6_delmulti(imm->i6mm_maddr);
2459 kfree(imm, M_IPMADDR);
2460 break;
2462 default:
2463 error = EOPNOTSUPP;
2464 break;
2468 * If all options have default values, no need to keep the mbuf.
2470 if (im6o->im6o_multicast_ifp == NULL &&
2471 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2472 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2473 im6o->im6o_memberships.lh_first == NULL) {
2474 kfree(*im6op, M_IPMOPTS);
2475 *im6op = NULL;
2478 return (error);
2482 * Return the IP6 multicast options in response to user getsockopt().
2484 static int
2485 ip6_getmoptions(int optname, struct ip6_moptions *im6o, struct mbuf **mp)
2487 u_int *hlim, *loop, *ifindex;
2489 *mp = m_get(M_WAITOK, MT_HEADER); /* XXX */
2491 switch (optname) {
2493 case IPV6_MULTICAST_IF:
2494 ifindex = mtod(*mp, u_int *);
2495 (*mp)->m_len = sizeof(u_int);
2496 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2497 *ifindex = 0;
2498 else
2499 *ifindex = im6o->im6o_multicast_ifp->if_index;
2500 return (0);
2502 case IPV6_MULTICAST_HOPS:
2503 hlim = mtod(*mp, u_int *);
2504 (*mp)->m_len = sizeof(u_int);
2505 if (im6o == NULL)
2506 *hlim = ip6_defmcasthlim;
2507 else
2508 *hlim = im6o->im6o_multicast_hlim;
2509 return (0);
2511 case IPV6_MULTICAST_LOOP:
2512 loop = mtod(*mp, u_int *);
2513 (*mp)->m_len = sizeof(u_int);
2514 if (im6o == NULL)
2515 *loop = ip6_defmcasthlim;
2516 else
2517 *loop = im6o->im6o_multicast_loop;
2518 return (0);
2520 default:
2521 return (EOPNOTSUPP);
2526 * Discard the IP6 multicast options.
2528 void
2529 ip6_freemoptions(struct ip6_moptions *im6o)
2531 struct in6_multi_mship *imm;
2533 if (im6o == NULL)
2534 return;
2536 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2537 LIST_REMOVE(imm, i6mm_chain);
2538 if (imm->i6mm_maddr)
2539 in6_delmulti(imm->i6mm_maddr);
2540 kfree(imm, M_IPMADDR);
2542 kfree(im6o, M_IPMOPTS);
2546 * Set a particular packet option, as a sticky option or an ancillary data
2547 * item. "len" can be 0 only when it's a sticky option.
2548 * We have 4 cases of combination of "sticky" and "cmsg":
2549 * "sticky=0, cmsg=0": impossible
2550 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2551 * "sticky=1, cmsg=0": RFC3542 socket option
2552 * "sticky=1, cmsg=1": RFC2292 socket option
2554 static int
2555 ip6_setpktoption(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2556 int sticky, int cmsg, int uproto, int priv)
2558 int minmtupolicy, preftemp;
2559 //int error;
2561 if (!sticky && !cmsg) {
2562 kprintf("ip6_setpktoption: impossible case\n");
2563 return (EINVAL);
2567 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2568 * not be specified in the context of RFC3542. Conversely,
2569 * RFC3542 types should not be specified in the context of RFC2292.
2571 if (!cmsg) {
2572 switch (optname) {
2573 case IPV6_2292PKTINFO:
2574 case IPV6_2292HOPLIMIT:
2575 case IPV6_2292NEXTHOP:
2576 case IPV6_2292HOPOPTS:
2577 case IPV6_2292DSTOPTS:
2578 case IPV6_2292RTHDR:
2579 case IPV6_2292PKTOPTIONS:
2580 return (ENOPROTOOPT);
2583 if (sticky && cmsg) {
2584 switch (optname) {
2585 case IPV6_PKTINFO:
2586 case IPV6_HOPLIMIT:
2587 case IPV6_NEXTHOP:
2588 case IPV6_HOPOPTS:
2589 case IPV6_DSTOPTS:
2590 case IPV6_RTHDRDSTOPTS:
2591 case IPV6_RTHDR:
2592 case IPV6_USE_MIN_MTU:
2593 case IPV6_DONTFRAG:
2594 case IPV6_TCLASS:
2595 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2596 return (ENOPROTOOPT);
2600 switch (optname) {
2601 case IPV6_2292PKTINFO:
2602 case IPV6_PKTINFO:
2604 struct in6_pktinfo *pktinfo;
2605 if (len != sizeof(struct in6_pktinfo))
2606 return (EINVAL);
2607 pktinfo = (struct in6_pktinfo *)buf;
2610 * An application can clear any sticky IPV6_PKTINFO option by
2611 * doing a "regular" setsockopt with ipi6_addr being
2612 * in6addr_any and ipi6_ifindex being zero.
2613 * [RFC 3542, Section 6]
2615 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2616 pktinfo->ipi6_ifindex == 0 &&
2617 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2618 ip6_clearpktopts(opt, optname);
2619 break;
2622 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2623 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2624 return (EINVAL);
2627 /* validate the interface index if specified. */
2628 if (pktinfo->ipi6_ifindex > if_index ||
2629 pktinfo->ipi6_ifindex < 0) {
2630 return (ENXIO);
2633 * Check if the requested source address is indeed a
2634 * unicast address assigned to the node, and can be
2635 * used as the packet's source address.
2637 if (opt->ip6po_pktinfo != NULL &&
2638 !IN6_IS_ADDR_UNSPECIFIED(&opt->ip6po_pktinfo->ipi6_addr)) {
2639 struct in6_ifaddr *ia6;
2640 struct sockaddr_in6 sin6;
2642 bzero(&sin6, sizeof(sin6));
2643 sin6.sin6_len = sizeof(sin6);
2644 sin6.sin6_family = AF_INET6;
2645 sin6.sin6_addr =
2646 opt->ip6po_pktinfo->ipi6_addr;
2647 ia6 = (struct in6_ifaddr *)ifa_ifwithaddr(sin6tosa(&sin6));
2648 if (ia6 == NULL ||
2649 (ia6->ia6_flags & (IN6_IFF_ANYCAST |
2650 IN6_IFF_NOTREADY)) != 0)
2651 return (EADDRNOTAVAIL);
2655 * We store the address anyway, and let in6_selectsrc()
2656 * validate the specified address. This is because ipi6_addr
2657 * may not have enough information about its scope zone, and
2658 * we may need additional information (such as outgoing
2659 * interface or the scope zone of a destination address) to
2660 * disambiguate the scope.
2661 * XXX: the delay of the validation may confuse the
2662 * application when it is used as a sticky option.
2664 if (opt->ip6po_pktinfo == NULL) {
2665 opt->ip6po_pktinfo = kmalloc(sizeof(*pktinfo),
2666 M_IP6OPT, M_NOWAIT);
2667 if (opt->ip6po_pktinfo == NULL)
2668 return (ENOBUFS);
2670 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2671 break;
2674 case IPV6_2292HOPLIMIT:
2675 case IPV6_HOPLIMIT:
2677 int *hlimp;
2680 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2681 * to simplify the ordering among hoplimit options.
2683 if (optname == IPV6_HOPLIMIT && sticky)
2684 return (ENOPROTOOPT);
2686 if (len != sizeof(int))
2687 return (EINVAL);
2688 hlimp = (int *)buf;
2689 if (*hlimp < -1 || *hlimp > 255)
2690 return (EINVAL);
2692 opt->ip6po_hlim = *hlimp;
2693 break;
2696 case IPV6_TCLASS:
2698 int tclass;
2700 if (len != sizeof(int))
2701 return (EINVAL);
2702 tclass = *(int *)buf;
2703 if (tclass < -1 || tclass > 255)
2704 return (EINVAL);
2706 opt->ip6po_tclass = tclass;
2707 break;
2710 case IPV6_2292NEXTHOP:
2711 case IPV6_NEXTHOP:
2712 if (!priv)
2713 return (EPERM);
2715 if (len == 0) { /* just remove the option */
2716 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2717 break;
2720 /* check if cmsg_len is large enough for sa_len */
2721 if (len < sizeof(struct sockaddr) || len < *buf)
2722 return (EINVAL);
2724 switch (((struct sockaddr *)buf)->sa_family) {
2725 case AF_INET6:
2727 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2728 //int error;
2730 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2731 return (EINVAL);
2733 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2734 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2735 return (EINVAL);
2737 break;
2739 case AF_LINK: /* should eventually be supported */
2740 default:
2741 return (EAFNOSUPPORT);
2744 /* turn off the previous option, then set the new option. */
2745 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2746 opt->ip6po_nexthop = kmalloc(*buf, M_IP6OPT, M_NOWAIT);
2747 if (opt->ip6po_nexthop == NULL)
2748 return (ENOBUFS);
2749 bcopy(buf, opt->ip6po_nexthop, *buf);
2750 break;
2752 case IPV6_2292HOPOPTS:
2753 case IPV6_HOPOPTS:
2755 struct ip6_hbh *hbh;
2756 int hbhlen;
2759 * XXX: We don't allow a non-privileged user to set ANY HbH
2760 * options, since per-option restriction has too much
2761 * overhead.
2763 if (!priv)
2764 return (EPERM);
2765 if (len == 0) {
2766 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2767 break; /* just remove the option */
2770 /* message length validation */
2771 if (len < sizeof(struct ip6_hbh))
2772 return (EINVAL);
2773 hbh = (struct ip6_hbh *)buf;
2774 hbhlen = (hbh->ip6h_len + 1) << 3;
2775 if (len != hbhlen)
2776 return (EINVAL);
2778 /* turn off the previous option, then set the new option. */
2779 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2780 opt->ip6po_hbh = kmalloc(hbhlen, M_IP6OPT, M_NOWAIT);
2781 if (opt->ip6po_hbh == NULL)
2782 return (ENOBUFS);
2783 bcopy(hbh, opt->ip6po_hbh, hbhlen);
2785 break;
2788 case IPV6_2292DSTOPTS:
2789 case IPV6_DSTOPTS:
2790 case IPV6_RTHDRDSTOPTS:
2792 struct ip6_dest *dest, **newdest = NULL;
2793 int destlen;
2794 if (!priv)
2795 return (EPERM);
2797 if (len == 0) {
2798 ip6_clearpktopts(opt, optname);
2799 break; /* just remove the option */
2802 /* message length validation */
2803 if (len < sizeof(struct ip6_dest))
2804 return (EINVAL);
2805 dest = (struct ip6_dest *)buf;
2806 destlen = (dest->ip6d_len + 1) << 3;
2807 if (len != destlen)
2808 return (EINVAL);
2811 * Determine the position that the destination options header
2812 * should be inserted; before or after the routing header.
2814 switch (optname) {
2815 case IPV6_2292DSTOPTS:
2817 * The old advacned API is ambiguous on this point.
2818 * Our approach is to determine the position based
2819 * according to the existence of a routing header.
2820 * Note, however, that this depends on the order of the
2821 * extension headers in the ancillary data; the 1st
2822 * part of the destination options header must appear
2823 * before the routing header in the ancillary data,
2824 * too.
2825 * RFC3542 solved the ambiguity by introducing
2826 * separate ancillary data or option types.
2828 if (opt->ip6po_rthdr == NULL)
2829 newdest = &opt->ip6po_dest1;
2830 else
2831 newdest = &opt->ip6po_dest2;
2832 break;
2833 case IPV6_RTHDRDSTOPTS:
2834 newdest = &opt->ip6po_dest1;
2835 break;
2836 case IPV6_DSTOPTS:
2837 newdest = &opt->ip6po_dest2;
2838 break;
2841 /* turn off the previous option, then set the new option. */
2842 ip6_clearpktopts(opt, optname);
2843 *newdest = kmalloc(destlen, M_IP6OPT, M_NOWAIT);
2844 if (*newdest == NULL)
2845 return (ENOBUFS);
2846 bcopy(dest, *newdest, destlen);
2848 break;
2851 case IPV6_2292RTHDR:
2852 case IPV6_RTHDR:
2854 struct ip6_rthdr *rth;
2855 int rthlen;
2857 if (len == 0) {
2858 ip6_clearpktopts(opt, IPV6_RTHDR);
2859 break; /* just remove the option */
2862 /* message length validation */
2863 if (len < sizeof(struct ip6_rthdr))
2864 return (EINVAL);
2865 rth = (struct ip6_rthdr *)buf;
2866 rthlen = (rth->ip6r_len + 1) << 3;
2867 if (len != rthlen)
2868 return (EINVAL);
2870 switch (rth->ip6r_type) {
2871 default:
2872 return (EINVAL); /* not supported */
2875 /* turn off the previous option */
2876 ip6_clearpktopts(opt, IPV6_RTHDR);
2877 opt->ip6po_rthdr = kmalloc(rthlen, M_IP6OPT, M_NOWAIT);
2878 if (opt->ip6po_rthdr == NULL)
2879 return (ENOBUFS);
2880 bcopy(rth, opt->ip6po_rthdr, rthlen);
2882 break;
2885 case IPV6_USE_MIN_MTU:
2886 if (len != sizeof(int))
2887 return (EINVAL);
2888 minmtupolicy = *(int *)buf;
2889 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
2890 minmtupolicy != IP6PO_MINMTU_DISABLE &&
2891 minmtupolicy != IP6PO_MINMTU_ALL) {
2892 return (EINVAL);
2894 opt->ip6po_minmtu = minmtupolicy;
2895 break;
2897 case IPV6_DONTFRAG:
2898 if (len != sizeof(int))
2899 return (EINVAL);
2901 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
2903 * we ignore this option for TCP sockets.
2904 * (RFC3542 leaves this case unspecified.)
2906 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
2907 } else
2908 opt->ip6po_flags |= IP6PO_DONTFRAG;
2909 break;
2911 case IPV6_PREFER_TEMPADDR:
2912 if (len != sizeof(int))
2913 return (EINVAL);
2914 preftemp = *(int *)buf;
2915 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
2916 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
2917 preftemp != IP6PO_TEMPADDR_PREFER) {
2918 return (EINVAL);
2920 opt->ip6po_prefer_tempaddr = preftemp;
2921 break;
2923 default:
2924 return (ENOPROTOOPT);
2925 } /* end of switch */
2927 return (0);
2932 * Set IPv6 outgoing packet options based on advanced API.
2935 ip6_setpktoptions(struct mbuf *control, struct ip6_pktopts *opt,
2936 struct ip6_pktopts *stickyopt, int uproto, int priv)
2938 struct cmsghdr *cm = NULL;
2940 if (control == NULL || opt == NULL)
2941 return (EINVAL);
2943 init_ip6pktopts(opt);
2946 * XXX: Currently, we assume all the optional information is stored
2947 * in a single mbuf.
2949 if (stickyopt) {
2950 int error;
2953 * If stickyopt is provided, make a local copy of the options
2954 * for this particular packet, then override them by ancillary
2955 * objects.
2956 * XXX: copypktopts() does not copy the cached route to a next
2957 * hop (if any). This is not very good in terms of efficiency,
2958 * but we can allow this since this option should be rarely
2959 * used.
2961 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2962 return (error);
2966 * XXX: Currently, we assume all the optional information is stored
2967 * in a single mbuf.
2969 if (control->m_next)
2970 return (EINVAL);
2972 for (;;) {
2973 int error;
2975 if (control->m_len == 0)
2976 break;
2977 if (control->m_len < sizeof(*cm))
2978 return EINVAL;
2980 cm = mtod(control, struct cmsghdr *);
2981 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2982 return (EINVAL);
2983 if (cm->cmsg_level == IPPROTO_IPV6) {
2984 error = ip6_setpktoption(cm->cmsg_type, CMSG_DATA(cm),
2985 cm->cmsg_len - CMSG_LEN(0),
2986 opt, 0, 1, uproto, priv);
2987 if (error)
2988 return (error);
2992 * The cmsg fit, but the aligned step for the next one might
2993 * not. Check the case and terminate normally (allows the
2994 * cmsg_len to not be aligned).
2996 if (CMSG_ALIGN(cm->cmsg_len) >= control->m_len) {
2997 control->m_data += control->m_len;
2998 control->m_len = 0;
2999 break;
3001 control->m_data += CMSG_ALIGN(cm->cmsg_len);
3002 control->m_len -= CMSG_ALIGN(cm->cmsg_len);
3005 return (0);
3009 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3010 * packet to the input queue of a specified interface. Note that this
3011 * calls the output routine of the loopback "driver", but with an interface
3012 * pointer that might NOT be loif -- easier than replicating that code here.
3014 void
3015 ip6_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in6 *dst)
3017 struct mbuf *copym;
3018 struct ip6_hdr *ip6;
3020 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3021 if (copym == NULL)
3022 return;
3025 * Make sure to deep-copy IPv6 header portion in case the data
3026 * is in an mbuf cluster, so that we can safely override the IPv6
3027 * header portion later.
3029 if ((copym->m_flags & M_EXT) != 0 ||
3030 copym->m_len < sizeof(struct ip6_hdr)) {
3031 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3032 if (copym == NULL)
3033 return;
3036 #ifdef DIAGNOSTIC
3037 if (copym->m_len < sizeof(*ip6)) {
3038 m_freem(copym);
3039 return;
3041 #endif
3043 ip6 = mtod(copym, struct ip6_hdr *);
3045 * clear embedded scope identifiers if necessary.
3046 * in6_clearscope will touch the addresses only when necessary.
3048 in6_clearscope(&ip6->ip6_src);
3049 in6_clearscope(&ip6->ip6_dst);
3051 if_simloop(ifp, copym, dst->sin6_family, 0);
3055 * Separate the IPv6 header from the payload into its own mbuf.
3057 * Returns the new mbuf chain or the original mbuf if no payload.
3058 * Returns NULL if can't allocate new mbuf for header.
3060 static struct mbuf *
3061 ip6_splithdr(struct mbuf *m)
3063 struct mbuf *mh;
3065 if (m->m_len <= sizeof(struct ip6_hdr)) /* no payload */
3066 return (m);
3068 MGETHDR(mh, M_NOWAIT, MT_HEADER);
3069 if (mh == NULL)
3070 return (NULL);
3071 mh->m_len = sizeof(struct ip6_hdr);
3072 M_MOVE_PKTHDR(mh, m);
3073 MH_ALIGN(mh, sizeof(struct ip6_hdr));
3074 bcopy(mtod(m, caddr_t), mtod(mh, caddr_t), sizeof(struct ip6_hdr));
3075 m->m_data += sizeof(struct ip6_hdr);
3076 m->m_len -= sizeof(struct ip6_hdr);
3077 mh->m_next = m;
3078 return (mh);
3082 * Compute IPv6 extension header length.
3085 ip6_optlen(struct in6pcb *in6p)
3087 int len;
3089 if (!in6p->in6p_outputopts)
3090 return 0;
3092 len = 0;
3093 #define elen(x) \
3094 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3096 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3097 if (in6p->in6p_outputopts->ip6po_rthdr)
3098 /* dest1 is valid with rthdr only */
3099 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3100 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3101 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3102 return len;
3103 #undef elen