ip6_output.c revision 1.203.2.5 1 /* $NetBSD: ip6_output.c,v 1.203.2.5 2018/09/06 06:56:45 pgoyette Exp $ */
2 /* $KAME: ip6_output.c,v 1.172 2001/03/25 09:55:56 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
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.
19 *
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.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
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.
48 *
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.
60 *
61 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: ip6_output.c,v 1.203.2.5 2018/09/06 06:56:45 pgoyette Exp $");
66
67 #ifdef _KERNEL_OPT
68 #include "opt_inet.h"
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #endif
72
73 #include <sys/param.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/errno.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/syslog.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/kauth.h>
83
84 #include <net/if.h>
85 #include <net/route.h>
86 #include <net/pfil.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip6.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/icmp6.h>
93 #include <netinet/in_offload.h>
94 #include <netinet/portalgo.h>
95 #include <netinet6/in6_offload.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/ip6_private.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet6/nd6.h>
100 #include <netinet6/ip6protosw.h>
101 #include <netinet6/scope6_var.h>
102
103 #ifdef IPSEC
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #include <netipsec/key.h>
107 #endif
108
109 extern pfil_head_t *inet6_pfil_hook; /* XXX */
110
111 struct ip6_exthdrs {
112 struct mbuf *ip6e_ip6;
113 struct mbuf *ip6e_hbh;
114 struct mbuf *ip6e_dest1;
115 struct mbuf *ip6e_rthdr;
116 struct mbuf *ip6e_dest2;
117 };
118
119 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
120 kauth_cred_t, int);
121 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
122 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, kauth_cred_t,
123 int, int, int);
124 static int ip6_setmoptions(const struct sockopt *, struct in6pcb *);
125 static int ip6_getmoptions(struct sockopt *, struct in6pcb *);
126 static int ip6_copyexthdr(struct mbuf **, void *, int);
127 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
128 struct ip6_frag **);
129 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
130 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
131 static int ip6_getpmtu(struct rtentry *, struct ifnet *, u_long *, int *);
132 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
133 static int ip6_ifaddrvalid(const struct in6_addr *, const struct in6_addr *);
134 static int ip6_handle_rthdr(struct ip6_rthdr *, struct ip6_hdr *);
135
136 #ifdef RFC2292
137 static int ip6_pcbopts(struct ip6_pktopts **, struct socket *, struct sockopt *);
138 #endif
139
140 static int
141 ip6_handle_rthdr(struct ip6_rthdr *rh, struct ip6_hdr *ip6)
142 {
143 int error = 0;
144
145 switch (rh->ip6r_type) {
146 case IPV6_RTHDR_TYPE_0:
147 /* Dropped, RFC5095. */
148 default: /* is it possible? */
149 error = EINVAL;
150 }
151
152 return error;
153 }
154
155 /*
156 * Send an IP packet to a host.
157 */
158 int
159 ip6_if_output(struct ifnet * const ifp, struct ifnet * const origifp,
160 struct mbuf * const m, const struct sockaddr_in6 * const dst,
161 const struct rtentry *rt)
162 {
163 int error = 0;
164
165 if (rt != NULL) {
166 error = rt_check_reject_route(rt, ifp);
167 if (error != 0) {
168 m_freem(m);
169 return error;
170 }
171 }
172
173 if ((ifp->if_flags & IFF_LOOPBACK) != 0)
174 error = if_output_lock(ifp, origifp, m, sin6tocsa(dst), rt);
175 else
176 error = if_output_lock(ifp, ifp, m, sin6tocsa(dst), rt);
177 return error;
178 }
179
180 /*
181 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
182 * header (with pri, len, nxt, hlim, src, dst).
183 *
184 * This function may modify ver and hlim only. The mbuf chain containing the
185 * packet will be freed. The mbuf opt, if present, will not be freed.
186 *
187 * Type of "mtu": rt_rmx.rmx_mtu is u_long, ifnet.ifr_mtu is int, and
188 * nd_ifinfo.linkmtu is u_int32_t. So we use u_long to hold largest one,
189 * which is rt_rmx.rmx_mtu.
190 */
191 int
192 ip6_output(
193 struct mbuf *m0,
194 struct ip6_pktopts *opt,
195 struct route *ro,
196 int flags,
197 struct ip6_moptions *im6o,
198 struct in6pcb *in6p,
199 struct ifnet **ifpp /* XXX: just for statistics */
200 )
201 {
202 struct ip6_hdr *ip6, *mhip6;
203 struct ifnet *ifp = NULL, *origifp = NULL;
204 struct mbuf *m = m0;
205 int tlen, len, off;
206 bool tso;
207 struct route ip6route;
208 struct rtentry *rt = NULL, *rt_pmtu;
209 const struct sockaddr_in6 *dst;
210 struct sockaddr_in6 src_sa, dst_sa;
211 int error = 0;
212 struct in6_ifaddr *ia = NULL;
213 u_long mtu;
214 int alwaysfrag, dontfrag;
215 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
216 struct ip6_exthdrs exthdrs;
217 struct in6_addr finaldst, src0, dst0;
218 u_int32_t zone;
219 struct route *ro_pmtu = NULL;
220 int hdrsplit = 0;
221 int needipsec = 0;
222 #ifdef IPSEC
223 struct secpolicy *sp = NULL;
224 #endif
225 struct psref psref, psref_ia;
226 int bound = curlwp_bind();
227 bool release_psref_ia = false;
228
229 #ifdef DIAGNOSTIC
230 if ((m->m_flags & M_PKTHDR) == 0)
231 panic("ip6_output: no HDR");
232 if ((m->m_pkthdr.csum_flags &
233 (M_CSUM_TCPv4|M_CSUM_UDPv4|M_CSUM_TSOv4)) != 0) {
234 panic("ip6_output: IPv4 checksum offload flags: %d",
235 m->m_pkthdr.csum_flags);
236 }
237 if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv6|M_CSUM_UDPv6)) ==
238 (M_CSUM_TCPv6|M_CSUM_UDPv6)) {
239 panic("ip6_output: conflicting checksum offload flags: %d",
240 m->m_pkthdr.csum_flags);
241 }
242 #endif
243
244 M_CSUM_DATA_IPv6_SET(m->m_pkthdr.csum_data, sizeof(struct ip6_hdr));
245
246 #define MAKE_EXTHDR(hp, mp) \
247 do { \
248 if (hp) { \
249 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
250 error = ip6_copyexthdr((mp), (void *)(hp), \
251 ((eh)->ip6e_len + 1) << 3); \
252 if (error) \
253 goto freehdrs; \
254 } \
255 } while (/*CONSTCOND*/ 0)
256
257 memset(&exthdrs, 0, sizeof(exthdrs));
258 if (opt) {
259 /* Hop-by-Hop options header */
260 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
261 /* Destination options header (1st part) */
262 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
263 /* Routing header */
264 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
265 /* Destination options header (2nd part) */
266 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
267 }
268
269 /*
270 * Calculate the total length of the extension header chain.
271 * Keep the length of the unfragmentable part for fragmentation.
272 */
273 optlen = 0;
274 if (exthdrs.ip6e_hbh)
275 optlen += exthdrs.ip6e_hbh->m_len;
276 if (exthdrs.ip6e_dest1)
277 optlen += exthdrs.ip6e_dest1->m_len;
278 if (exthdrs.ip6e_rthdr)
279 optlen += exthdrs.ip6e_rthdr->m_len;
280 unfragpartlen = optlen + sizeof(struct ip6_hdr);
281 /* NOTE: we don't add AH/ESP length here. do that later. */
282 if (exthdrs.ip6e_dest2)
283 optlen += exthdrs.ip6e_dest2->m_len;
284
285 #ifdef IPSEC
286 if (ipsec_used) {
287 /* Check the security policy (SP) for the packet */
288 sp = ipsec6_check_policy(m, in6p, flags, &needipsec, &error);
289 if (error != 0) {
290 /*
291 * Hack: -EINVAL is used to signal that a packet
292 * should be silently discarded. This is typically
293 * because we asked key management for an SA and
294 * it was delayed (e.g. kicked up to IKE).
295 */
296 if (error == -EINVAL)
297 error = 0;
298 goto freehdrs;
299 }
300 }
301 #endif
302
303 if (needipsec &&
304 (m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
305 in6_undefer_cksum_tcpudp(m);
306 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
307 }
308
309 /*
310 * If we need IPsec, or there is at least one extension header,
311 * separate IP6 header from the payload.
312 */
313 if ((needipsec || optlen) && !hdrsplit) {
314 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
315 m = NULL;
316 goto freehdrs;
317 }
318 m = exthdrs.ip6e_ip6;
319 hdrsplit++;
320 }
321
322 /* adjust pointer */
323 ip6 = mtod(m, struct ip6_hdr *);
324
325 /* adjust mbuf packet header length */
326 m->m_pkthdr.len += optlen;
327 plen = m->m_pkthdr.len - sizeof(*ip6);
328
329 /* If this is a jumbo payload, insert a jumbo payload option. */
330 if (plen > IPV6_MAXPACKET) {
331 if (!hdrsplit) {
332 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
333 m = NULL;
334 goto freehdrs;
335 }
336 m = exthdrs.ip6e_ip6;
337 hdrsplit++;
338 }
339 /* adjust pointer */
340 ip6 = mtod(m, struct ip6_hdr *);
341 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
342 goto freehdrs;
343 optlen += 8; /* XXX JUMBOOPTLEN */
344 ip6->ip6_plen = 0;
345 } else
346 ip6->ip6_plen = htons(plen);
347
348 /*
349 * Concatenate headers and fill in next header fields.
350 * Here we have, on "m"
351 * IPv6 payload
352 * and we insert headers accordingly. Finally, we should be getting:
353 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
354 *
355 * during the header composing process, "m" points to IPv6 header.
356 * "mprev" points to an extension header prior to esp.
357 */
358 {
359 u_char *nexthdrp = &ip6->ip6_nxt;
360 struct mbuf *mprev = m;
361
362 /*
363 * we treat dest2 specially. this makes IPsec processing
364 * much easier. the goal here is to make mprev point the
365 * mbuf prior to dest2.
366 *
367 * result: IPv6 dest2 payload
368 * m and mprev will point to IPv6 header.
369 */
370 if (exthdrs.ip6e_dest2) {
371 if (!hdrsplit)
372 panic("assumption failed: hdr not split");
373 exthdrs.ip6e_dest2->m_next = m->m_next;
374 m->m_next = exthdrs.ip6e_dest2;
375 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
376 ip6->ip6_nxt = IPPROTO_DSTOPTS;
377 }
378
379 #define MAKE_CHAIN(m, mp, p, i)\
380 do {\
381 if (m) {\
382 if (!hdrsplit) \
383 panic("assumption failed: hdr not split"); \
384 *mtod((m), u_char *) = *(p);\
385 *(p) = (i);\
386 p = mtod((m), u_char *);\
387 (m)->m_next = (mp)->m_next;\
388 (mp)->m_next = (m);\
389 (mp) = (m);\
390 }\
391 } while (/*CONSTCOND*/ 0)
392 /*
393 * result: IPv6 hbh dest1 rthdr dest2 payload
394 * m will point to IPv6 header. mprev will point to the
395 * extension header prior to dest2 (rthdr in the above case).
396 */
397 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
398 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
399 IPPROTO_DSTOPTS);
400 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
401 IPPROTO_ROUTING);
402
403 M_CSUM_DATA_IPv6_SET(m->m_pkthdr.csum_data,
404 sizeof(struct ip6_hdr) + optlen);
405 }
406
407 /* Need to save for pmtu */
408 finaldst = ip6->ip6_dst;
409
410 /*
411 * If there is a routing header, replace destination address field
412 * with the first hop of the routing header.
413 */
414 if (exthdrs.ip6e_rthdr) {
415 struct ip6_rthdr *rh;
416
417 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
418
419 error = ip6_handle_rthdr(rh, ip6);
420 if (error != 0)
421 goto bad;
422 }
423
424 /* Source address validation */
425 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
426 (flags & IPV6_UNSPECSRC) == 0) {
427 error = EOPNOTSUPP;
428 IP6_STATINC(IP6_STAT_BADSCOPE);
429 goto bad;
430 }
431 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
432 error = EOPNOTSUPP;
433 IP6_STATINC(IP6_STAT_BADSCOPE);
434 goto bad;
435 }
436
437 IP6_STATINC(IP6_STAT_LOCALOUT);
438
439 /*
440 * Route packet.
441 */
442 /* initialize cached route */
443 if (ro == NULL) {
444 memset(&ip6route, 0, sizeof(ip6route));
445 ro = &ip6route;
446 }
447 ro_pmtu = ro;
448 if (opt && opt->ip6po_rthdr)
449 ro = &opt->ip6po_route;
450
451 /*
452 * if specified, try to fill in the traffic class field.
453 * do not override if a non-zero value is already set.
454 * we check the diffserv field and the ecn field separately.
455 */
456 if (opt && opt->ip6po_tclass >= 0) {
457 int mask = 0;
458
459 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
460 mask |= 0xfc;
461 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
462 mask |= 0x03;
463 if (mask != 0)
464 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
465 }
466
467 /* fill in or override the hop limit field, if necessary. */
468 if (opt && opt->ip6po_hlim != -1)
469 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
470 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
471 if (im6o != NULL)
472 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
473 else
474 ip6->ip6_hlim = ip6_defmcasthlim;
475 }
476
477 #ifdef IPSEC
478 if (needipsec) {
479 int s = splsoftnet();
480 error = ipsec6_process_packet(m, sp->req);
481 splx(s);
482
483 /*
484 * Preserve KAME behaviour: ENOENT can be returned
485 * when an SA acquire is in progress. Don't propagate
486 * this to user-level; it confuses applications.
487 * XXX this will go away when the SADB is redone.
488 */
489 if (error == ENOENT)
490 error = 0;
491
492 goto done;
493 }
494 #endif
495
496 /* adjust pointer */
497 ip6 = mtod(m, struct ip6_hdr *);
498
499 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
500
501 /* We do not need a route for multicast */
502 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
503 struct in6_pktinfo *pi = NULL;
504
505 /*
506 * If the outgoing interface for the address is specified by
507 * the caller, use it.
508 */
509 if (opt && (pi = opt->ip6po_pktinfo) != NULL) {
510 /* XXX boundary check is assumed to be already done. */
511 ifp = if_get_byindex(pi->ipi6_ifindex, &psref);
512 } else if (im6o != NULL) {
513 ifp = if_get_byindex(im6o->im6o_multicast_if_index,
514 &psref);
515 }
516 }
517
518 if (ifp == NULL) {
519 error = in6_selectroute(&dst_sa, opt, &ro, &rt, true);
520 if (error != 0)
521 goto bad;
522 ifp = if_get_byindex(rt->rt_ifp->if_index, &psref);
523 }
524
525 if (rt == NULL) {
526 /*
527 * If in6_selectroute() does not return a route entry,
528 * dst may not have been updated.
529 */
530 error = rtcache_setdst(ro, sin6tosa(&dst_sa));
531 if (error) {
532 goto bad;
533 }
534 }
535
536 /*
537 * then rt (for unicast) and ifp must be non-NULL valid values.
538 */
539 if ((flags & IPV6_FORWARDING) == 0) {
540 /* XXX: the FORWARDING flag can be set for mrouting. */
541 in6_ifstat_inc(ifp, ifs6_out_request);
542 }
543 if (rt != NULL) {
544 ia = (struct in6_ifaddr *)(rt->rt_ifa);
545 rt->rt_use++;
546 }
547
548 /*
549 * The outgoing interface must be in the zone of source and
550 * destination addresses. We should use ia_ifp to support the
551 * case of sending packets to an address of our own.
552 */
553 if (ia != NULL && ia->ia_ifp) {
554 origifp = ia->ia_ifp;
555 if (if_is_deactivated(origifp))
556 goto bad;
557 if_acquire(origifp, &psref_ia);
558 release_psref_ia = true;
559 } else
560 origifp = ifp;
561
562 src0 = ip6->ip6_src;
563 if (in6_setscope(&src0, origifp, &zone))
564 goto badscope;
565 sockaddr_in6_init(&src_sa, &ip6->ip6_src, 0, 0, 0);
566 if (sa6_recoverscope(&src_sa) || zone != src_sa.sin6_scope_id)
567 goto badscope;
568
569 dst0 = ip6->ip6_dst;
570 if (in6_setscope(&dst0, origifp, &zone))
571 goto badscope;
572 /* re-initialize to be sure */
573 sockaddr_in6_init(&dst_sa, &ip6->ip6_dst, 0, 0, 0);
574 if (sa6_recoverscope(&dst_sa) || zone != dst_sa.sin6_scope_id)
575 goto badscope;
576
577 /* scope check is done. */
578
579 /* Ensure we only send from a valid address. */
580 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
581 (error = ip6_ifaddrvalid(&src0, &dst0)) != 0)
582 {
583 char ip6buf[INET6_ADDRSTRLEN];
584 nd6log(LOG_ERR,
585 "refusing to send from invalid address %s (pid %d)\n",
586 IN6_PRINT(ip6buf, &src0), curproc->p_pid);
587 IP6_STATINC(IP6_STAT_ODROPPED);
588 in6_ifstat_inc(origifp, ifs6_out_discard);
589 if (error == 1)
590 /*
591 * Address exists, but is tentative or detached.
592 * We can't send from it because it's invalid,
593 * so we drop the packet.
594 */
595 error = 0;
596 else
597 error = EADDRNOTAVAIL;
598 goto bad;
599 }
600
601 if (rt != NULL && (rt->rt_flags & RTF_GATEWAY) &&
602 !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
603 dst = satocsin6(rt->rt_gateway);
604 else
605 dst = satocsin6(rtcache_getdst(ro));
606
607 /*
608 * XXXXXX: original code follows:
609 */
610 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))
611 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
612 else {
613 bool ingroup;
614
615 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
616
617 in6_ifstat_inc(ifp, ifs6_out_mcast);
618
619 /*
620 * Confirm that the outgoing interface supports multicast.
621 */
622 if (!(ifp->if_flags & IFF_MULTICAST)) {
623 IP6_STATINC(IP6_STAT_NOROUTE);
624 in6_ifstat_inc(ifp, ifs6_out_discard);
625 error = ENETUNREACH;
626 goto bad;
627 }
628
629 ingroup = in6_multi_group(&ip6->ip6_dst, ifp);
630 if (ingroup && (im6o == NULL || im6o->im6o_multicast_loop)) {
631 /*
632 * If we belong to the destination multicast group
633 * on the outgoing interface, and the caller did not
634 * forbid loopback, loop back a copy.
635 */
636 KASSERT(dst != NULL);
637 ip6_mloopback(ifp, m, dst);
638 } else {
639 /*
640 * If we are acting as a multicast router, perform
641 * multicast forwarding as if the packet had just
642 * arrived on the interface to which we are about
643 * to send. The multicast forwarding function
644 * recursively calls this function, using the
645 * IPV6_FORWARDING flag to prevent infinite recursion.
646 *
647 * Multicasts that are looped back by ip6_mloopback(),
648 * above, will be forwarded by the ip6_input() routine,
649 * if necessary.
650 */
651 if (ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
652 if (ip6_mforward(ip6, ifp, m) != 0) {
653 m_freem(m);
654 goto done;
655 }
656 }
657 }
658 /*
659 * Multicasts with a hoplimit of zero may be looped back,
660 * above, but must not be transmitted on a network.
661 * Also, multicasts addressed to the loopback interface
662 * are not sent -- the above call to ip6_mloopback() will
663 * loop back a copy if this host actually belongs to the
664 * destination group on the loopback interface.
665 */
666 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
667 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
668 m_freem(m);
669 goto done;
670 }
671 }
672
673 /*
674 * Fill the outgoing inteface to tell the upper layer
675 * to increment per-interface statistics.
676 */
677 if (ifpp)
678 *ifpp = ifp;
679
680 /* Determine path MTU. */
681 /*
682 * ro_pmtu represent final destination while
683 * ro might represent immediate destination.
684 * Use ro_pmtu destination since MTU might differ.
685 */
686 if (ro_pmtu != ro) {
687 union {
688 struct sockaddr dst;
689 struct sockaddr_in6 dst6;
690 } u;
691
692 /* ro_pmtu may not have a cache */
693 sockaddr_in6_init(&u.dst6, &finaldst, 0, 0, 0);
694 rt_pmtu = rtcache_lookup(ro_pmtu, &u.dst);
695 } else
696 rt_pmtu = rt;
697 error = ip6_getpmtu(rt_pmtu, ifp, &mtu, &alwaysfrag);
698 if (rt_pmtu != NULL && rt_pmtu != rt)
699 rtcache_unref(rt_pmtu, ro_pmtu);
700 if (error != 0)
701 goto bad;
702
703 /*
704 * The caller of this function may specify to use the minimum MTU
705 * in some cases.
706 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
707 * setting. The logic is a bit complicated; by default, unicast
708 * packets will follow path MTU while multicast packets will be sent at
709 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
710 * including unicast ones will be sent at the minimum MTU. Multicast
711 * packets will always be sent at the minimum MTU unless
712 * IP6PO_MINMTU_DISABLE is explicitly specified.
713 * See RFC 3542 for more details.
714 */
715 if (mtu > IPV6_MMTU) {
716 if ((flags & IPV6_MINMTU))
717 mtu = IPV6_MMTU;
718 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
719 mtu = IPV6_MMTU;
720 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
721 (opt == NULL ||
722 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
723 mtu = IPV6_MMTU;
724 }
725 }
726
727 /*
728 * clear embedded scope identifiers if necessary.
729 * in6_clearscope will touch the addresses only when necessary.
730 */
731 in6_clearscope(&ip6->ip6_src);
732 in6_clearscope(&ip6->ip6_dst);
733
734 /*
735 * If the outgoing packet contains a hop-by-hop options header,
736 * it must be examined and processed even by the source node.
737 * (RFC 2460, section 4.)
738 *
739 * XXX Is this really necessary?
740 */
741 if (ip6->ip6_nxt == IPPROTO_HOPOPTS) {
742 u_int32_t dummy1; /* XXX unused */
743 u_int32_t dummy2; /* XXX unused */
744 int hoff = sizeof(struct ip6_hdr);
745
746 if (ip6_hopopts_input(&dummy1, &dummy2, &m, &hoff)) {
747 /* m was already freed at this point */
748 error = EINVAL;
749 goto done;
750 }
751
752 ip6 = mtod(m, struct ip6_hdr *);
753 }
754
755 /*
756 * Run through list of hooks for output packets.
757 */
758 if ((error = pfil_run_hooks(inet6_pfil_hook, &m, ifp, PFIL_OUT)) != 0)
759 goto done;
760 if (m == NULL)
761 goto done;
762 ip6 = mtod(m, struct ip6_hdr *);
763
764 /*
765 * Send the packet to the outgoing interface.
766 * If necessary, do IPv6 fragmentation before sending.
767 *
768 * the logic here is rather complex:
769 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
770 * 1-a: send as is if tlen <= path mtu
771 * 1-b: fragment if tlen > path mtu
772 *
773 * 2: if user asks us not to fragment (dontfrag == 1)
774 * 2-a: send as is if tlen <= interface mtu
775 * 2-b: error if tlen > interface mtu
776 *
777 * 3: if we always need to attach fragment header (alwaysfrag == 1)
778 * always fragment
779 *
780 * 4: if dontfrag == 1 && alwaysfrag == 1
781 * error, as we cannot handle this conflicting request
782 */
783 tlen = m->m_pkthdr.len;
784 tso = (m->m_pkthdr.csum_flags & M_CSUM_TSOv6) != 0;
785 if (opt && (opt->ip6po_flags & IP6PO_DONTFRAG))
786 dontfrag = 1;
787 else
788 dontfrag = 0;
789
790 if (dontfrag && alwaysfrag) { /* case 4 */
791 /* conflicting request - can't transmit */
792 error = EMSGSIZE;
793 goto bad;
794 }
795 if (dontfrag && (!tso && tlen > IN6_LINKMTU(ifp))) { /* case 2-b */
796 /*
797 * Even if the DONTFRAG option is specified, we cannot send the
798 * packet when the data length is larger than the MTU of the
799 * outgoing interface.
800 * Notify the error by sending IPV6_PATHMTU ancillary data as
801 * well as returning an error code (the latter is not described
802 * in the API spec.)
803 */
804 u_int32_t mtu32;
805 struct ip6ctlparam ip6cp;
806
807 mtu32 = (u_int32_t)mtu;
808 memset(&ip6cp, 0, sizeof(ip6cp));
809 ip6cp.ip6c_cmdarg = (void *)&mtu32;
810 pfctlinput2(PRC_MSGSIZE,
811 rtcache_getdst(ro_pmtu), &ip6cp);
812
813 error = EMSGSIZE;
814 goto bad;
815 }
816
817 /*
818 * transmit packet without fragmentation
819 */
820 if (dontfrag || (!alwaysfrag && (tlen <= mtu || tso))) {
821 /* case 1-a and 2-a */
822 struct in6_ifaddr *ia6;
823 int sw_csum;
824 int s;
825
826 ip6 = mtod(m, struct ip6_hdr *);
827 s = pserialize_read_enter();
828 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
829 if (ia6) {
830 /* Record statistics for this interface address. */
831 ia6->ia_ifa.ifa_data.ifad_outbytes += m->m_pkthdr.len;
832 }
833 pserialize_read_exit(s);
834
835 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx;
836 if ((sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0) {
837 if (IN6_NEED_CHECKSUM(ifp,
838 sw_csum & (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
839 in6_undefer_cksum_tcpudp(m);
840 }
841 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
842 }
843
844 KASSERT(dst != NULL);
845 if (__predict_true(!tso ||
846 (ifp->if_capenable & IFCAP_TSOv6) != 0)) {
847 error = ip6_if_output(ifp, origifp, m, dst, rt);
848 } else {
849 error = ip6_tso_output(ifp, origifp, m, dst, rt);
850 }
851 goto done;
852 }
853
854 if (tso) {
855 error = EINVAL; /* XXX */
856 goto bad;
857 }
858
859 /*
860 * try to fragment the packet. case 1-b and 3
861 */
862 if (mtu < IPV6_MMTU) {
863 /* path MTU cannot be less than IPV6_MMTU */
864 error = EMSGSIZE;
865 in6_ifstat_inc(ifp, ifs6_out_fragfail);
866 goto bad;
867 } else if (ip6->ip6_plen == 0) {
868 /* jumbo payload cannot be fragmented */
869 error = EMSGSIZE;
870 in6_ifstat_inc(ifp, ifs6_out_fragfail);
871 goto bad;
872 } else {
873 const u_int32_t id = htonl(ip6_randomid());
874 struct mbuf **mnext, *m_frgpart;
875 const int hlen = unfragpartlen;
876 struct ip6_frag *ip6f;
877 u_char nextproto;
878
879 if (mtu > IPV6_MAXPACKET)
880 mtu = IPV6_MAXPACKET;
881
882 /*
883 * Must be able to put at least 8 bytes per fragment.
884 */
885 len = (mtu - hlen - sizeof(struct ip6_frag)) & ~7;
886 if (len < 8) {
887 error = EMSGSIZE;
888 in6_ifstat_inc(ifp, ifs6_out_fragfail);
889 goto bad;
890 }
891
892 mnext = &m->m_nextpkt;
893
894 /*
895 * Change the next header field of the last header in the
896 * unfragmentable part.
897 */
898 if (exthdrs.ip6e_rthdr) {
899 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
900 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
901 } else if (exthdrs.ip6e_dest1) {
902 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
903 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
904 } else if (exthdrs.ip6e_hbh) {
905 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
906 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
907 } else {
908 nextproto = ip6->ip6_nxt;
909 ip6->ip6_nxt = IPPROTO_FRAGMENT;
910 }
911
912 if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6))
913 != 0) {
914 if (IN6_NEED_CHECKSUM(ifp,
915 m->m_pkthdr.csum_flags &
916 (M_CSUM_UDPv6|M_CSUM_TCPv6))) {
917 in6_undefer_cksum_tcpudp(m);
918 }
919 m->m_pkthdr.csum_flags &= ~(M_CSUM_UDPv6|M_CSUM_TCPv6);
920 }
921
922 /*
923 * Loop through length of segment after first fragment,
924 * make new header and copy data of each part and link onto
925 * chain.
926 */
927 m0 = m;
928 for (off = hlen; off < tlen; off += len) {
929 struct mbuf *mlast;
930
931 MGETHDR(m, M_DONTWAIT, MT_HEADER);
932 if (!m) {
933 error = ENOBUFS;
934 IP6_STATINC(IP6_STAT_ODROPPED);
935 goto sendorfree;
936 }
937 m_reset_rcvif(m);
938 m->m_flags = m0->m_flags & M_COPYFLAGS;
939 *mnext = m;
940 mnext = &m->m_nextpkt;
941 m->m_data += max_linkhdr;
942 mhip6 = mtod(m, struct ip6_hdr *);
943 *mhip6 = *ip6;
944 m->m_len = sizeof(*mhip6);
945
946 ip6f = NULL;
947 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
948 if (error) {
949 IP6_STATINC(IP6_STAT_ODROPPED);
950 goto sendorfree;
951 }
952
953 /* Fill in the Frag6 Header */
954 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
955 if (off + len >= tlen)
956 len = tlen - off;
957 else
958 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
959 ip6f->ip6f_reserved = 0;
960 ip6f->ip6f_ident = id;
961 ip6f->ip6f_nxt = nextproto;
962
963 mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
964 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
965 if ((m_frgpart = m_copym(m0, off, len, M_DONTWAIT)) == NULL) {
966 error = ENOBUFS;
967 IP6_STATINC(IP6_STAT_ODROPPED);
968 goto sendorfree;
969 }
970 for (mlast = m; mlast->m_next; mlast = mlast->m_next)
971 ;
972 mlast->m_next = m_frgpart;
973
974 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
975 m_reset_rcvif(m);
976 IP6_STATINC(IP6_STAT_OFRAGMENTS);
977 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
978 }
979
980 in6_ifstat_inc(ifp, ifs6_out_fragok);
981 }
982
983 sendorfree:
984 m = m0->m_nextpkt;
985 m0->m_nextpkt = 0;
986 m_freem(m0);
987 for (m0 = m; m; m = m0) {
988 m0 = m->m_nextpkt;
989 m->m_nextpkt = 0;
990 if (error == 0) {
991 struct in6_ifaddr *ia6;
992 int s;
993 ip6 = mtod(m, struct ip6_hdr *);
994 s = pserialize_read_enter();
995 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
996 if (ia6) {
997 /*
998 * Record statistics for this interface
999 * address.
1000 */
1001 ia6->ia_ifa.ifa_data.ifad_outbytes +=
1002 m->m_pkthdr.len;
1003 }
1004 pserialize_read_exit(s);
1005 KASSERT(dst != NULL);
1006 error = ip6_if_output(ifp, origifp, m, dst, rt);
1007 } else
1008 m_freem(m);
1009 }
1010
1011 if (error == 0)
1012 IP6_STATINC(IP6_STAT_FRAGMENTED);
1013
1014 done:
1015 rtcache_unref(rt, ro);
1016 if (ro == &ip6route)
1017 rtcache_free(&ip6route);
1018 #ifdef IPSEC
1019 if (sp != NULL)
1020 KEY_SP_UNREF(&sp);
1021 #endif
1022 if_put(ifp, &psref);
1023 if (release_psref_ia)
1024 if_put(origifp, &psref_ia);
1025 curlwp_bindx(bound);
1026
1027 return error;
1028
1029 freehdrs:
1030 m_freem(exthdrs.ip6e_hbh);
1031 m_freem(exthdrs.ip6e_dest1);
1032 m_freem(exthdrs.ip6e_rthdr);
1033 m_freem(exthdrs.ip6e_dest2);
1034 /* FALLTHROUGH */
1035 bad:
1036 m_freem(m);
1037 goto done;
1038
1039 badscope:
1040 IP6_STATINC(IP6_STAT_BADSCOPE);
1041 in6_ifstat_inc(origifp, ifs6_out_discard);
1042 if (error == 0)
1043 error = EHOSTUNREACH; /* XXX */
1044 goto bad;
1045 }
1046
1047 static int
1048 ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen)
1049 {
1050 struct mbuf *m;
1051
1052 if (hlen > MCLBYTES)
1053 return ENOBUFS; /* XXX */
1054
1055 MGET(m, M_DONTWAIT, MT_DATA);
1056 if (!m)
1057 return ENOBUFS;
1058
1059 if (hlen > MLEN) {
1060 MCLGET(m, M_DONTWAIT);
1061 if ((m->m_flags & M_EXT) == 0) {
1062 m_free(m);
1063 return ENOBUFS;
1064 }
1065 }
1066 m->m_len = hlen;
1067 if (hdr)
1068 memcpy(mtod(m, void *), hdr, hlen);
1069
1070 *mp = m;
1071 return 0;
1072 }
1073
1074 /*
1075 * Insert jumbo payload option.
1076 */
1077 static int
1078 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1079 {
1080 struct mbuf *mopt;
1081 u_int8_t *optbuf;
1082 u_int32_t v;
1083
1084 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1085
1086 /*
1087 * If there is no hop-by-hop options header, allocate new one.
1088 * If there is one but it doesn't have enough space to store the
1089 * jumbo payload option, allocate a cluster to store the whole options.
1090 * Otherwise, use it to store the options.
1091 */
1092 if (exthdrs->ip6e_hbh == NULL) {
1093 MGET(mopt, M_DONTWAIT, MT_DATA);
1094 if (mopt == 0)
1095 return (ENOBUFS);
1096 mopt->m_len = JUMBOOPTLEN;
1097 optbuf = mtod(mopt, u_int8_t *);
1098 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1099 exthdrs->ip6e_hbh = mopt;
1100 } else {
1101 struct ip6_hbh *hbh;
1102
1103 mopt = exthdrs->ip6e_hbh;
1104 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1105 const int oldoptlen = mopt->m_len;
1106 struct mbuf *n;
1107
1108 /*
1109 * Assumptions:
1110 * - exthdrs->ip6e_hbh is not referenced from places
1111 * other than exthdrs.
1112 * - exthdrs->ip6e_hbh is not an mbuf chain.
1113 */
1114 KASSERT(mopt->m_next == NULL);
1115
1116 /*
1117 * Give up if the whole (new) hbh header does not fit
1118 * even in an mbuf cluster.
1119 */
1120 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1121 return ENOBUFS;
1122
1123 /*
1124 * At this point, we must always prepare a cluster.
1125 */
1126 MGET(n, M_DONTWAIT, MT_DATA);
1127 if (n) {
1128 MCLGET(n, M_DONTWAIT);
1129 if ((n->m_flags & M_EXT) == 0) {
1130 m_freem(n);
1131 n = NULL;
1132 }
1133 }
1134 if (!n)
1135 return ENOBUFS;
1136
1137 n->m_len = oldoptlen + JUMBOOPTLEN;
1138 bcopy(mtod(mopt, void *), mtod(n, void *),
1139 oldoptlen);
1140 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1141 m_freem(mopt);
1142 mopt = exthdrs->ip6e_hbh = n;
1143 } else {
1144 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1145 mopt->m_len += JUMBOOPTLEN;
1146 }
1147 optbuf[0] = IP6OPT_PADN;
1148 optbuf[1] = 0;
1149
1150 /*
1151 * Adjust the header length according to the pad and
1152 * the jumbo payload option.
1153 */
1154 hbh = mtod(mopt, struct ip6_hbh *);
1155 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1156 }
1157
1158 /* fill in the option. */
1159 optbuf[2] = IP6OPT_JUMBO;
1160 optbuf[3] = 4;
1161 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1162 memcpy(&optbuf[4], &v, sizeof(u_int32_t));
1163
1164 /* finally, adjust the packet header length */
1165 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1166
1167 return 0;
1168 #undef JUMBOOPTLEN
1169 }
1170
1171 /*
1172 * Insert fragment header and copy unfragmentable header portions.
1173 *
1174 * *frghdrp will not be read, and it is guaranteed that either an
1175 * error is returned or that *frghdrp will point to space allocated
1176 * for the fragment header.
1177 *
1178 * On entry, m contains:
1179 * IPv6 Header
1180 * On exit, it contains:
1181 * IPv6 Header -> Unfragmentable Part -> Frag6 Header
1182 */
1183 static int
1184 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1185 struct ip6_frag **frghdrp)
1186 {
1187 struct mbuf *n, *mlast;
1188
1189 if (hlen > sizeof(struct ip6_hdr)) {
1190 n = m_copym(m0, sizeof(struct ip6_hdr),
1191 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1192 if (n == NULL)
1193 return ENOBUFS;
1194 m->m_next = n;
1195 } else
1196 n = m;
1197
1198 /* Search for the last mbuf of unfragmentable part. */
1199 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1200 ;
1201
1202 if ((mlast->m_flags & M_EXT) == 0 &&
1203 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1204 /* use the trailing space of the last mbuf for the fragment hdr */
1205 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) +
1206 mlast->m_len);
1207 mlast->m_len += sizeof(struct ip6_frag);
1208 } else {
1209 /* allocate a new mbuf for the fragment header */
1210 struct mbuf *mfrg;
1211
1212 MGET(mfrg, M_DONTWAIT, MT_DATA);
1213 if (mfrg == NULL)
1214 return ENOBUFS;
1215 mfrg->m_len = sizeof(struct ip6_frag);
1216 *frghdrp = mtod(mfrg, struct ip6_frag *);
1217 mlast->m_next = mfrg;
1218 }
1219
1220 return 0;
1221 }
1222
1223 static int
1224 ip6_getpmtu(struct rtentry *rt, struct ifnet *ifp, u_long *mtup,
1225 int *alwaysfragp)
1226 {
1227 u_int32_t mtu = 0;
1228 int alwaysfrag = 0;
1229 int error = 0;
1230
1231 if (rt != NULL) {
1232 u_int32_t ifmtu;
1233
1234 if (ifp == NULL)
1235 ifp = rt->rt_ifp;
1236 ifmtu = IN6_LINKMTU(ifp);
1237 mtu = rt->rt_rmx.rmx_mtu;
1238 if (mtu == 0)
1239 mtu = ifmtu;
1240 else if (mtu < IPV6_MMTU) {
1241 /*
1242 * RFC2460 section 5, last paragraph:
1243 * if we record ICMPv6 too big message with
1244 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1245 * or smaller, with fragment header attached.
1246 * (fragment header is needed regardless from the
1247 * packet size, for translators to identify packets)
1248 */
1249 alwaysfrag = 1;
1250 mtu = IPV6_MMTU;
1251 } else if (mtu > ifmtu) {
1252 /*
1253 * The MTU on the route is larger than the MTU on
1254 * the interface! This shouldn't happen, unless the
1255 * MTU of the interface has been changed after the
1256 * interface was brought up. Change the MTU in the
1257 * route to match the interface MTU (as long as the
1258 * field isn't locked).
1259 */
1260 mtu = ifmtu;
1261 if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
1262 rt->rt_rmx.rmx_mtu = mtu;
1263 }
1264 } else if (ifp) {
1265 mtu = IN6_LINKMTU(ifp);
1266 } else
1267 error = EHOSTUNREACH; /* XXX */
1268
1269 *mtup = mtu;
1270 if (alwaysfragp)
1271 *alwaysfragp = alwaysfrag;
1272 return (error);
1273 }
1274
1275 /*
1276 * IP6 socket option processing.
1277 */
1278 int
1279 ip6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1280 {
1281 int optdatalen, uproto;
1282 void *optdata;
1283 struct in6pcb *in6p = sotoin6pcb(so);
1284 struct ip_moptions **mopts;
1285 int error, optval;
1286 int level, optname;
1287
1288 KASSERT(solocked(so));
1289 KASSERT(sopt != NULL);
1290
1291 level = sopt->sopt_level;
1292 optname = sopt->sopt_name;
1293
1294 error = optval = 0;
1295 uproto = (int)so->so_proto->pr_protocol;
1296
1297 switch (level) {
1298 case IPPROTO_IP:
1299 switch (optname) {
1300 case IP_ADD_MEMBERSHIP:
1301 case IP_DROP_MEMBERSHIP:
1302 case IP_MULTICAST_IF:
1303 case IP_MULTICAST_LOOP:
1304 case IP_MULTICAST_TTL:
1305 mopts = &in6p->in6p_v4moptions;
1306 switch (op) {
1307 case PRCO_GETOPT:
1308 return ip_getmoptions(*mopts, sopt);
1309 case PRCO_SETOPT:
1310 return ip_setmoptions(mopts, sopt);
1311 default:
1312 return EINVAL;
1313 }
1314 default:
1315 return ENOPROTOOPT;
1316 }
1317 case IPPROTO_IPV6:
1318 break;
1319 default:
1320 return ENOPROTOOPT;
1321 }
1322 switch (op) {
1323 case PRCO_SETOPT:
1324 switch (optname) {
1325 #ifdef RFC2292
1326 case IPV6_2292PKTOPTIONS:
1327 error = ip6_pcbopts(&in6p->in6p_outputopts, so, sopt);
1328 break;
1329 #endif
1330
1331 /*
1332 * Use of some Hop-by-Hop options or some
1333 * Destination options, might require special
1334 * privilege. That is, normal applications
1335 * (without special privilege) might be forbidden
1336 * from setting certain options in outgoing packets,
1337 * and might never see certain options in received
1338 * packets. [RFC 2292 Section 6]
1339 * KAME specific note:
1340 * KAME prevents non-privileged users from sending or
1341 * receiving ANY hbh/dst options in order to avoid
1342 * overhead of parsing options in the kernel.
1343 */
1344 case IPV6_RECVHOPOPTS:
1345 case IPV6_RECVDSTOPTS:
1346 case IPV6_RECVRTHDRDSTOPTS:
1347 error = kauth_authorize_network(kauth_cred_get(),
1348 KAUTH_NETWORK_IPV6, KAUTH_REQ_NETWORK_IPV6_HOPBYHOP,
1349 NULL, NULL, NULL);
1350 if (error)
1351 break;
1352 /* FALLTHROUGH */
1353 case IPV6_UNICAST_HOPS:
1354 case IPV6_HOPLIMIT:
1355 case IPV6_FAITH:
1356
1357 case IPV6_RECVPKTINFO:
1358 case IPV6_RECVHOPLIMIT:
1359 case IPV6_RECVRTHDR:
1360 case IPV6_RECVPATHMTU:
1361 case IPV6_RECVTCLASS:
1362 case IPV6_V6ONLY:
1363 error = sockopt_getint(sopt, &optval);
1364 if (error)
1365 break;
1366 switch (optname) {
1367 case IPV6_UNICAST_HOPS:
1368 if (optval < -1 || optval >= 256)
1369 error = EINVAL;
1370 else {
1371 /* -1 = kernel default */
1372 in6p->in6p_hops = optval;
1373 }
1374 break;
1375 #define OPTSET(bit) \
1376 do { \
1377 if (optval) \
1378 in6p->in6p_flags |= (bit); \
1379 else \
1380 in6p->in6p_flags &= ~(bit); \
1381 } while (/*CONSTCOND*/ 0)
1382
1383 #ifdef RFC2292
1384 #define OPTSET2292(bit) \
1385 do { \
1386 in6p->in6p_flags |= IN6P_RFC2292; \
1387 if (optval) \
1388 in6p->in6p_flags |= (bit); \
1389 else \
1390 in6p->in6p_flags &= ~(bit); \
1391 } while (/*CONSTCOND*/ 0)
1392 #endif
1393
1394 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1395
1396 case IPV6_RECVPKTINFO:
1397 #ifdef RFC2292
1398 /* cannot mix with RFC2292 */
1399 if (OPTBIT(IN6P_RFC2292)) {
1400 error = EINVAL;
1401 break;
1402 }
1403 #endif
1404 OPTSET(IN6P_PKTINFO);
1405 break;
1406
1407 case IPV6_HOPLIMIT:
1408 {
1409 struct ip6_pktopts **optp;
1410
1411 #ifdef RFC2292
1412 /* cannot mix with RFC2292 */
1413 if (OPTBIT(IN6P_RFC2292)) {
1414 error = EINVAL;
1415 break;
1416 }
1417 #endif
1418 optp = &in6p->in6p_outputopts;
1419 error = ip6_pcbopt(IPV6_HOPLIMIT,
1420 (u_char *)&optval,
1421 sizeof(optval),
1422 optp,
1423 kauth_cred_get(), uproto);
1424 break;
1425 }
1426
1427 case IPV6_RECVHOPLIMIT:
1428 #ifdef RFC2292
1429 /* cannot mix with RFC2292 */
1430 if (OPTBIT(IN6P_RFC2292)) {
1431 error = EINVAL;
1432 break;
1433 }
1434 #endif
1435 OPTSET(IN6P_HOPLIMIT);
1436 break;
1437
1438 case IPV6_RECVHOPOPTS:
1439 #ifdef RFC2292
1440 /* cannot mix with RFC2292 */
1441 if (OPTBIT(IN6P_RFC2292)) {
1442 error = EINVAL;
1443 break;
1444 }
1445 #endif
1446 OPTSET(IN6P_HOPOPTS);
1447 break;
1448
1449 case IPV6_RECVDSTOPTS:
1450 #ifdef RFC2292
1451 /* cannot mix with RFC2292 */
1452 if (OPTBIT(IN6P_RFC2292)) {
1453 error = EINVAL;
1454 break;
1455 }
1456 #endif
1457 OPTSET(IN6P_DSTOPTS);
1458 break;
1459
1460 case IPV6_RECVRTHDRDSTOPTS:
1461 #ifdef RFC2292
1462 /* cannot mix with RFC2292 */
1463 if (OPTBIT(IN6P_RFC2292)) {
1464 error = EINVAL;
1465 break;
1466 }
1467 #endif
1468 OPTSET(IN6P_RTHDRDSTOPTS);
1469 break;
1470
1471 case IPV6_RECVRTHDR:
1472 #ifdef RFC2292
1473 /* cannot mix with RFC2292 */
1474 if (OPTBIT(IN6P_RFC2292)) {
1475 error = EINVAL;
1476 break;
1477 }
1478 #endif
1479 OPTSET(IN6P_RTHDR);
1480 break;
1481
1482 case IPV6_FAITH:
1483 OPTSET(IN6P_FAITH);
1484 break;
1485
1486 case IPV6_RECVPATHMTU:
1487 /*
1488 * We ignore this option for TCP
1489 * sockets.
1490 * (RFC3542 leaves this case
1491 * unspecified.)
1492 */
1493 if (uproto != IPPROTO_TCP)
1494 OPTSET(IN6P_MTU);
1495 break;
1496
1497 case IPV6_V6ONLY:
1498 /*
1499 * make setsockopt(IPV6_V6ONLY)
1500 * available only prior to bind(2).
1501 * see ipng mailing list, Jun 22 2001.
1502 */
1503 if (in6p->in6p_lport ||
1504 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1505 error = EINVAL;
1506 break;
1507 }
1508 #ifdef INET6_BINDV6ONLY
1509 if (!optval)
1510 error = EINVAL;
1511 #else
1512 OPTSET(IN6P_IPV6_V6ONLY);
1513 #endif
1514 break;
1515 case IPV6_RECVTCLASS:
1516 #ifdef RFC2292
1517 /* cannot mix with RFC2292 XXX */
1518 if (OPTBIT(IN6P_RFC2292)) {
1519 error = EINVAL;
1520 break;
1521 }
1522 #endif
1523 OPTSET(IN6P_TCLASS);
1524 break;
1525
1526 }
1527 break;
1528
1529 case IPV6_OTCLASS:
1530 {
1531 struct ip6_pktopts **optp;
1532 u_int8_t tclass;
1533
1534 error = sockopt_get(sopt, &tclass, sizeof(tclass));
1535 if (error)
1536 break;
1537 optp = &in6p->in6p_outputopts;
1538 error = ip6_pcbopt(optname,
1539 (u_char *)&tclass,
1540 sizeof(tclass),
1541 optp,
1542 kauth_cred_get(), uproto);
1543 break;
1544 }
1545
1546 case IPV6_TCLASS:
1547 case IPV6_DONTFRAG:
1548 case IPV6_USE_MIN_MTU:
1549 case IPV6_PREFER_TEMPADDR:
1550 error = sockopt_getint(sopt, &optval);
1551 if (error)
1552 break;
1553 {
1554 struct ip6_pktopts **optp;
1555 optp = &in6p->in6p_outputopts;
1556 error = ip6_pcbopt(optname,
1557 (u_char *)&optval,
1558 sizeof(optval),
1559 optp,
1560 kauth_cred_get(), uproto);
1561 break;
1562 }
1563
1564 #ifdef RFC2292
1565 case IPV6_2292PKTINFO:
1566 case IPV6_2292HOPLIMIT:
1567 case IPV6_2292HOPOPTS:
1568 case IPV6_2292DSTOPTS:
1569 case IPV6_2292RTHDR:
1570 /* RFC 2292 */
1571 error = sockopt_getint(sopt, &optval);
1572 if (error)
1573 break;
1574
1575 switch (optname) {
1576 case IPV6_2292PKTINFO:
1577 OPTSET2292(IN6P_PKTINFO);
1578 break;
1579 case IPV6_2292HOPLIMIT:
1580 OPTSET2292(IN6P_HOPLIMIT);
1581 break;
1582 case IPV6_2292HOPOPTS:
1583 /*
1584 * Check super-user privilege.
1585 * See comments for IPV6_RECVHOPOPTS.
1586 */
1587 error =
1588 kauth_authorize_network(kauth_cred_get(),
1589 KAUTH_NETWORK_IPV6,
1590 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL,
1591 NULL, NULL);
1592 if (error)
1593 return (error);
1594 OPTSET2292(IN6P_HOPOPTS);
1595 break;
1596 case IPV6_2292DSTOPTS:
1597 error =
1598 kauth_authorize_network(kauth_cred_get(),
1599 KAUTH_NETWORK_IPV6,
1600 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL,
1601 NULL, NULL);
1602 if (error)
1603 return (error);
1604 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1605 break;
1606 case IPV6_2292RTHDR:
1607 OPTSET2292(IN6P_RTHDR);
1608 break;
1609 }
1610 break;
1611 #endif
1612 case IPV6_PKTINFO:
1613 case IPV6_HOPOPTS:
1614 case IPV6_RTHDR:
1615 case IPV6_DSTOPTS:
1616 case IPV6_RTHDRDSTOPTS:
1617 case IPV6_NEXTHOP: {
1618 /* new advanced API (RFC3542) */
1619 void *optbuf;
1620 int optbuflen;
1621 struct ip6_pktopts **optp;
1622
1623 #ifdef RFC2292
1624 /* cannot mix with RFC2292 */
1625 if (OPTBIT(IN6P_RFC2292)) {
1626 error = EINVAL;
1627 break;
1628 }
1629 #endif
1630
1631 optbuflen = sopt->sopt_size;
1632 optbuf = malloc(optbuflen, M_IP6OPT, M_NOWAIT);
1633 if (optbuf == NULL) {
1634 error = ENOBUFS;
1635 break;
1636 }
1637
1638 error = sockopt_get(sopt, optbuf, optbuflen);
1639 if (error) {
1640 free(optbuf, M_IP6OPT);
1641 break;
1642 }
1643 optp = &in6p->in6p_outputopts;
1644 error = ip6_pcbopt(optname, optbuf, optbuflen,
1645 optp, kauth_cred_get(), uproto);
1646
1647 free(optbuf, M_IP6OPT);
1648 break;
1649 }
1650 #undef OPTSET
1651
1652 case IPV6_MULTICAST_IF:
1653 case IPV6_MULTICAST_HOPS:
1654 case IPV6_MULTICAST_LOOP:
1655 case IPV6_JOIN_GROUP:
1656 case IPV6_LEAVE_GROUP:
1657 error = ip6_setmoptions(sopt, in6p);
1658 break;
1659
1660 case IPV6_PORTRANGE:
1661 error = sockopt_getint(sopt, &optval);
1662 if (error)
1663 break;
1664
1665 switch (optval) {
1666 case IPV6_PORTRANGE_DEFAULT:
1667 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1668 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1669 break;
1670
1671 case IPV6_PORTRANGE_HIGH:
1672 in6p->in6p_flags &= ~(IN6P_LOWPORT);
1673 in6p->in6p_flags |= IN6P_HIGHPORT;
1674 break;
1675
1676 case IPV6_PORTRANGE_LOW:
1677 in6p->in6p_flags &= ~(IN6P_HIGHPORT);
1678 in6p->in6p_flags |= IN6P_LOWPORT;
1679 break;
1680
1681 default:
1682 error = EINVAL;
1683 break;
1684 }
1685 break;
1686
1687 case IPV6_PORTALGO:
1688 error = sockopt_getint(sopt, &optval);
1689 if (error)
1690 break;
1691
1692 error = portalgo_algo_index_select(
1693 (struct inpcb_hdr *)in6p, optval);
1694 break;
1695
1696 #if defined(IPSEC)
1697 case IPV6_IPSEC_POLICY:
1698 if (ipsec_enabled) {
1699 error = ipsec_set_policy(in6p,
1700 sopt->sopt_data, sopt->sopt_size,
1701 kauth_cred_get());
1702 break;
1703 }
1704 /*FALLTHROUGH*/
1705 #endif /* IPSEC */
1706
1707 default:
1708 error = ENOPROTOOPT;
1709 break;
1710 }
1711 break;
1712
1713 case PRCO_GETOPT:
1714 switch (optname) {
1715 #ifdef RFC2292
1716 case IPV6_2292PKTOPTIONS:
1717 /*
1718 * RFC3542 (effectively) deprecated the
1719 * semantics of the 2292-style pktoptions.
1720 * Since it was not reliable in nature (i.e.,
1721 * applications had to expect the lack of some
1722 * information after all), it would make sense
1723 * to simplify this part by always returning
1724 * empty data.
1725 */
1726 break;
1727 #endif
1728
1729 case IPV6_RECVHOPOPTS:
1730 case IPV6_RECVDSTOPTS:
1731 case IPV6_RECVRTHDRDSTOPTS:
1732 case IPV6_UNICAST_HOPS:
1733 case IPV6_RECVPKTINFO:
1734 case IPV6_RECVHOPLIMIT:
1735 case IPV6_RECVRTHDR:
1736 case IPV6_RECVPATHMTU:
1737
1738 case IPV6_FAITH:
1739 case IPV6_V6ONLY:
1740 case IPV6_PORTRANGE:
1741 case IPV6_RECVTCLASS:
1742 switch (optname) {
1743
1744 case IPV6_RECVHOPOPTS:
1745 optval = OPTBIT(IN6P_HOPOPTS);
1746 break;
1747
1748 case IPV6_RECVDSTOPTS:
1749 optval = OPTBIT(IN6P_DSTOPTS);
1750 break;
1751
1752 case IPV6_RECVRTHDRDSTOPTS:
1753 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1754 break;
1755
1756 case IPV6_UNICAST_HOPS:
1757 optval = in6p->in6p_hops;
1758 break;
1759
1760 case IPV6_RECVPKTINFO:
1761 optval = OPTBIT(IN6P_PKTINFO);
1762 break;
1763
1764 case IPV6_RECVHOPLIMIT:
1765 optval = OPTBIT(IN6P_HOPLIMIT);
1766 break;
1767
1768 case IPV6_RECVRTHDR:
1769 optval = OPTBIT(IN6P_RTHDR);
1770 break;
1771
1772 case IPV6_RECVPATHMTU:
1773 optval = OPTBIT(IN6P_MTU);
1774 break;
1775
1776 case IPV6_FAITH:
1777 optval = OPTBIT(IN6P_FAITH);
1778 break;
1779
1780 case IPV6_V6ONLY:
1781 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1782 break;
1783
1784 case IPV6_PORTRANGE:
1785 {
1786 int flags;
1787 flags = in6p->in6p_flags;
1788 if (flags & IN6P_HIGHPORT)
1789 optval = IPV6_PORTRANGE_HIGH;
1790 else if (flags & IN6P_LOWPORT)
1791 optval = IPV6_PORTRANGE_LOW;
1792 else
1793 optval = 0;
1794 break;
1795 }
1796 case IPV6_RECVTCLASS:
1797 optval = OPTBIT(IN6P_TCLASS);
1798 break;
1799
1800 }
1801 if (error)
1802 break;
1803 error = sockopt_setint(sopt, optval);
1804 break;
1805
1806 case IPV6_PATHMTU:
1807 {
1808 u_long pmtu = 0;
1809 struct ip6_mtuinfo mtuinfo;
1810 struct route *ro = &in6p->in6p_route;
1811 struct rtentry *rt;
1812 union {
1813 struct sockaddr dst;
1814 struct sockaddr_in6 dst6;
1815 } u;
1816
1817 if (!(so->so_state & SS_ISCONNECTED))
1818 return (ENOTCONN);
1819 /*
1820 * XXX: we dot not consider the case of source
1821 * routing, or optional information to specify
1822 * the outgoing interface.
1823 */
1824 sockaddr_in6_init(&u.dst6, &in6p->in6p_faddr, 0, 0, 0);
1825 rt = rtcache_lookup(ro, &u.dst);
1826 error = ip6_getpmtu(rt, NULL, &pmtu, NULL);
1827 rtcache_unref(rt, ro);
1828 if (error)
1829 break;
1830 if (pmtu > IPV6_MAXPACKET)
1831 pmtu = IPV6_MAXPACKET;
1832
1833 memset(&mtuinfo, 0, sizeof(mtuinfo));
1834 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1835 optdata = (void *)&mtuinfo;
1836 optdatalen = sizeof(mtuinfo);
1837 if (optdatalen > MCLBYTES)
1838 return (EMSGSIZE); /* XXX */
1839 error = sockopt_set(sopt, optdata, optdatalen);
1840 break;
1841 }
1842
1843 #ifdef RFC2292
1844 case IPV6_2292PKTINFO:
1845 case IPV6_2292HOPLIMIT:
1846 case IPV6_2292HOPOPTS:
1847 case IPV6_2292RTHDR:
1848 case IPV6_2292DSTOPTS:
1849 switch (optname) {
1850 case IPV6_2292PKTINFO:
1851 optval = OPTBIT(IN6P_PKTINFO);
1852 break;
1853 case IPV6_2292HOPLIMIT:
1854 optval = OPTBIT(IN6P_HOPLIMIT);
1855 break;
1856 case IPV6_2292HOPOPTS:
1857 optval = OPTBIT(IN6P_HOPOPTS);
1858 break;
1859 case IPV6_2292RTHDR:
1860 optval = OPTBIT(IN6P_RTHDR);
1861 break;
1862 case IPV6_2292DSTOPTS:
1863 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1864 break;
1865 }
1866 error = sockopt_setint(sopt, optval);
1867 break;
1868 #endif
1869 case IPV6_PKTINFO:
1870 case IPV6_HOPOPTS:
1871 case IPV6_RTHDR:
1872 case IPV6_DSTOPTS:
1873 case IPV6_RTHDRDSTOPTS:
1874 case IPV6_NEXTHOP:
1875 case IPV6_OTCLASS:
1876 case IPV6_TCLASS:
1877 case IPV6_DONTFRAG:
1878 case IPV6_USE_MIN_MTU:
1879 case IPV6_PREFER_TEMPADDR:
1880 error = ip6_getpcbopt(in6p->in6p_outputopts,
1881 optname, sopt);
1882 break;
1883
1884 case IPV6_MULTICAST_IF:
1885 case IPV6_MULTICAST_HOPS:
1886 case IPV6_MULTICAST_LOOP:
1887 case IPV6_JOIN_GROUP:
1888 case IPV6_LEAVE_GROUP:
1889 error = ip6_getmoptions(sopt, in6p);
1890 break;
1891
1892 case IPV6_PORTALGO:
1893 optval = ((struct inpcb_hdr *)in6p)->inph_portalgo;
1894 error = sockopt_setint(sopt, optval);
1895 break;
1896
1897 #if defined(IPSEC)
1898 case IPV6_IPSEC_POLICY:
1899 if (ipsec_used) {
1900 struct mbuf *m = NULL;
1901
1902 /*
1903 * XXX: this will return EINVAL as sopt is
1904 * empty
1905 */
1906 error = ipsec_get_policy(in6p, sopt->sopt_data,
1907 sopt->sopt_size, &m);
1908 if (!error)
1909 error = sockopt_setmbuf(sopt, m);
1910 break;
1911 }
1912 /*FALLTHROUGH*/
1913 #endif /* IPSEC */
1914
1915 default:
1916 error = ENOPROTOOPT;
1917 break;
1918 }
1919 break;
1920 }
1921 return (error);
1922 }
1923
1924 int
1925 ip6_raw_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1926 {
1927 int error = 0, optval;
1928 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1929 struct in6pcb *in6p = sotoin6pcb(so);
1930 int level, optname;
1931
1932 KASSERT(sopt != NULL);
1933
1934 level = sopt->sopt_level;
1935 optname = sopt->sopt_name;
1936
1937 if (level != IPPROTO_IPV6) {
1938 return ENOPROTOOPT;
1939 }
1940
1941 switch (optname) {
1942 case IPV6_CHECKSUM:
1943 /*
1944 * For ICMPv6 sockets, no modification allowed for checksum
1945 * offset, permit "no change" values to help existing apps.
1946 *
1947 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
1948 * for an ICMPv6 socket will fail." The current
1949 * behavior does not meet RFC3542.
1950 */
1951 switch (op) {
1952 case PRCO_SETOPT:
1953 error = sockopt_getint(sopt, &optval);
1954 if (error)
1955 break;
1956 if ((optval % 2) != 0) {
1957 /* the API assumes even offset values */
1958 error = EINVAL;
1959 } else if (so->so_proto->pr_protocol ==
1960 IPPROTO_ICMPV6) {
1961 if (optval != icmp6off)
1962 error = EINVAL;
1963 } else
1964 in6p->in6p_cksum = optval;
1965 break;
1966
1967 case PRCO_GETOPT:
1968 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
1969 optval = icmp6off;
1970 else
1971 optval = in6p->in6p_cksum;
1972
1973 error = sockopt_setint(sopt, optval);
1974 break;
1975
1976 default:
1977 error = EINVAL;
1978 break;
1979 }
1980 break;
1981
1982 default:
1983 error = ENOPROTOOPT;
1984 break;
1985 }
1986
1987 return (error);
1988 }
1989
1990 #ifdef RFC2292
1991 /*
1992 * Set up IP6 options in pcb for insertion in output packets or
1993 * specifying behavior of outgoing packets.
1994 */
1995 static int
1996 ip6_pcbopts(struct ip6_pktopts **pktopt, struct socket *so,
1997 struct sockopt *sopt)
1998 {
1999 struct ip6_pktopts *opt = *pktopt;
2000 struct mbuf *m;
2001 int error = 0;
2002
2003 KASSERT(solocked(so));
2004
2005 /* turn off any old options. */
2006 if (opt) {
2007 #ifdef DIAGNOSTIC
2008 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2009 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2010 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2011 printf("ip6_pcbopts: all specified options are cleared.\n");
2012 #endif
2013 ip6_clearpktopts(opt, -1);
2014 } else {
2015 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2016 if (opt == NULL)
2017 return (ENOBUFS);
2018 }
2019 *pktopt = NULL;
2020
2021 if (sopt == NULL || sopt->sopt_size == 0) {
2022 /*
2023 * Only turning off any previous options, regardless of
2024 * whether the opt is just created or given.
2025 */
2026 free(opt, M_IP6OPT);
2027 return (0);
2028 }
2029
2030 /* set options specified by user. */
2031 m = sockopt_getmbuf(sopt);
2032 if (m == NULL) {
2033 free(opt, M_IP6OPT);
2034 return (ENOBUFS);
2035 }
2036
2037 error = ip6_setpktopts(m, opt, NULL, kauth_cred_get(),
2038 so->so_proto->pr_protocol);
2039 m_freem(m);
2040 if (error != 0) {
2041 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2042 free(opt, M_IP6OPT);
2043 return (error);
2044 }
2045 *pktopt = opt;
2046 return (0);
2047 }
2048 #endif
2049
2050 /*
2051 * initialize ip6_pktopts. beware that there are non-zero default values in
2052 * the struct.
2053 */
2054 void
2055 ip6_initpktopts(struct ip6_pktopts *opt)
2056 {
2057
2058 memset(opt, 0, sizeof(*opt));
2059 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2060 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2061 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2062 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2063 }
2064
2065 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2066 static int
2067 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2068 kauth_cred_t cred, int uproto)
2069 {
2070 struct ip6_pktopts *opt;
2071
2072 if (*pktopt == NULL) {
2073 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2074 M_NOWAIT);
2075 if (*pktopt == NULL)
2076 return (ENOBUFS);
2077
2078 ip6_initpktopts(*pktopt);
2079 }
2080 opt = *pktopt;
2081
2082 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2083 }
2084
2085 static int
2086 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2087 {
2088 void *optdata = NULL;
2089 int optdatalen = 0;
2090 struct ip6_ext *ip6e;
2091 int error = 0;
2092 struct in6_pktinfo null_pktinfo;
2093 int deftclass = 0, on;
2094 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2095 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2096
2097 switch (optname) {
2098 case IPV6_PKTINFO:
2099 if (pktopt && pktopt->ip6po_pktinfo)
2100 optdata = (void *)pktopt->ip6po_pktinfo;
2101 else {
2102 /* XXX: we don't have to do this every time... */
2103 memset(&null_pktinfo, 0, sizeof(null_pktinfo));
2104 optdata = (void *)&null_pktinfo;
2105 }
2106 optdatalen = sizeof(struct in6_pktinfo);
2107 break;
2108 case IPV6_OTCLASS:
2109 /* XXX */
2110 return (EINVAL);
2111 case IPV6_TCLASS:
2112 if (pktopt && pktopt->ip6po_tclass >= 0)
2113 optdata = (void *)&pktopt->ip6po_tclass;
2114 else
2115 optdata = (void *)&deftclass;
2116 optdatalen = sizeof(int);
2117 break;
2118 case IPV6_HOPOPTS:
2119 if (pktopt && pktopt->ip6po_hbh) {
2120 optdata = (void *)pktopt->ip6po_hbh;
2121 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2122 optdatalen = (ip6e->ip6e_len + 1) << 3;
2123 }
2124 break;
2125 case IPV6_RTHDR:
2126 if (pktopt && pktopt->ip6po_rthdr) {
2127 optdata = (void *)pktopt->ip6po_rthdr;
2128 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2129 optdatalen = (ip6e->ip6e_len + 1) << 3;
2130 }
2131 break;
2132 case IPV6_RTHDRDSTOPTS:
2133 if (pktopt && pktopt->ip6po_dest1) {
2134 optdata = (void *)pktopt->ip6po_dest1;
2135 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2136 optdatalen = (ip6e->ip6e_len + 1) << 3;
2137 }
2138 break;
2139 case IPV6_DSTOPTS:
2140 if (pktopt && pktopt->ip6po_dest2) {
2141 optdata = (void *)pktopt->ip6po_dest2;
2142 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2143 optdatalen = (ip6e->ip6e_len + 1) << 3;
2144 }
2145 break;
2146 case IPV6_NEXTHOP:
2147 if (pktopt && pktopt->ip6po_nexthop) {
2148 optdata = (void *)pktopt->ip6po_nexthop;
2149 optdatalen = pktopt->ip6po_nexthop->sa_len;
2150 }
2151 break;
2152 case IPV6_USE_MIN_MTU:
2153 if (pktopt)
2154 optdata = (void *)&pktopt->ip6po_minmtu;
2155 else
2156 optdata = (void *)&defminmtu;
2157 optdatalen = sizeof(int);
2158 break;
2159 case IPV6_DONTFRAG:
2160 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2161 on = 1;
2162 else
2163 on = 0;
2164 optdata = (void *)&on;
2165 optdatalen = sizeof(on);
2166 break;
2167 case IPV6_PREFER_TEMPADDR:
2168 if (pktopt)
2169 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2170 else
2171 optdata = (void *)&defpreftemp;
2172 optdatalen = sizeof(int);
2173 break;
2174 default: /* should not happen */
2175 #ifdef DIAGNOSTIC
2176 panic("ip6_getpcbopt: unexpected option\n");
2177 #endif
2178 return (ENOPROTOOPT);
2179 }
2180
2181 error = sockopt_set(sopt, optdata, optdatalen);
2182
2183 return (error);
2184 }
2185
2186 void
2187 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2188 {
2189 if (optname == -1 || optname == IPV6_PKTINFO) {
2190 if (pktopt->ip6po_pktinfo)
2191 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2192 pktopt->ip6po_pktinfo = NULL;
2193 }
2194 if (optname == -1 || optname == IPV6_HOPLIMIT)
2195 pktopt->ip6po_hlim = -1;
2196 if (optname == -1 || optname == IPV6_TCLASS)
2197 pktopt->ip6po_tclass = -1;
2198 if (optname == -1 || optname == IPV6_NEXTHOP) {
2199 rtcache_free(&pktopt->ip6po_nextroute);
2200 if (pktopt->ip6po_nexthop)
2201 free(pktopt->ip6po_nexthop, M_IP6OPT);
2202 pktopt->ip6po_nexthop = NULL;
2203 }
2204 if (optname == -1 || optname == IPV6_HOPOPTS) {
2205 if (pktopt->ip6po_hbh)
2206 free(pktopt->ip6po_hbh, M_IP6OPT);
2207 pktopt->ip6po_hbh = NULL;
2208 }
2209 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2210 if (pktopt->ip6po_dest1)
2211 free(pktopt->ip6po_dest1, M_IP6OPT);
2212 pktopt->ip6po_dest1 = NULL;
2213 }
2214 if (optname == -1 || optname == IPV6_RTHDR) {
2215 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2216 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2217 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2218 rtcache_free(&pktopt->ip6po_route);
2219 }
2220 if (optname == -1 || optname == IPV6_DSTOPTS) {
2221 if (pktopt->ip6po_dest2)
2222 free(pktopt->ip6po_dest2, M_IP6OPT);
2223 pktopt->ip6po_dest2 = NULL;
2224 }
2225 }
2226
2227 #define PKTOPT_EXTHDRCPY(type) \
2228 do { \
2229 if (src->type) { \
2230 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2231 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2232 if (dst->type == NULL) \
2233 goto bad; \
2234 memcpy(dst->type, src->type, hlen); \
2235 } \
2236 } while (/*CONSTCOND*/ 0)
2237
2238 static int
2239 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2240 {
2241 dst->ip6po_hlim = src->ip6po_hlim;
2242 dst->ip6po_tclass = src->ip6po_tclass;
2243 dst->ip6po_flags = src->ip6po_flags;
2244 dst->ip6po_minmtu = src->ip6po_minmtu;
2245 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2246 if (src->ip6po_pktinfo) {
2247 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2248 M_IP6OPT, canwait);
2249 if (dst->ip6po_pktinfo == NULL)
2250 goto bad;
2251 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2252 }
2253 if (src->ip6po_nexthop) {
2254 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2255 M_IP6OPT, canwait);
2256 if (dst->ip6po_nexthop == NULL)
2257 goto bad;
2258 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2259 src->ip6po_nexthop->sa_len);
2260 }
2261 PKTOPT_EXTHDRCPY(ip6po_hbh);
2262 PKTOPT_EXTHDRCPY(ip6po_dest1);
2263 PKTOPT_EXTHDRCPY(ip6po_dest2);
2264 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2265 return (0);
2266
2267 bad:
2268 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2269 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2270 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2271 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2272 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2273 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2274
2275 return (ENOBUFS);
2276 }
2277 #undef PKTOPT_EXTHDRCPY
2278
2279 struct ip6_pktopts *
2280 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2281 {
2282 int error;
2283 struct ip6_pktopts *dst;
2284
2285 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2286 if (dst == NULL)
2287 return (NULL);
2288 ip6_initpktopts(dst);
2289
2290 if ((error = copypktopts(dst, src, canwait)) != 0) {
2291 free(dst, M_IP6OPT);
2292 return (NULL);
2293 }
2294
2295 return (dst);
2296 }
2297
2298 void
2299 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2300 {
2301 if (pktopt == NULL)
2302 return;
2303
2304 ip6_clearpktopts(pktopt, -1);
2305
2306 free(pktopt, M_IP6OPT);
2307 }
2308
2309 int
2310 ip6_get_membership(const struct sockopt *sopt, struct ifnet **ifp,
2311 struct psref *psref, void *v, size_t l)
2312 {
2313 struct ipv6_mreq mreq;
2314 int error;
2315 struct in6_addr *ia = &mreq.ipv6mr_multiaddr;
2316 struct in_addr *ia4 = (void *)&ia->s6_addr32[3];
2317
2318 error = sockopt_get(sopt, &mreq, sizeof(mreq));
2319 if (error != 0)
2320 return error;
2321
2322 if (IN6_IS_ADDR_UNSPECIFIED(ia)) {
2323 /*
2324 * We use the unspecified address to specify to accept
2325 * all multicast addresses. Only super user is allowed
2326 * to do this.
2327 */
2328 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_IPV6,
2329 KAUTH_REQ_NETWORK_IPV6_JOIN_MULTICAST, NULL, NULL, NULL))
2330 return EACCES;
2331 } else if (IN6_IS_ADDR_V4MAPPED(ia)) {
2332 // Don't bother if we are not going to use ifp.
2333 if (l == sizeof(*ia)) {
2334 memcpy(v, ia, l);
2335 return 0;
2336 }
2337 } else if (!IN6_IS_ADDR_MULTICAST(ia)) {
2338 return EINVAL;
2339 }
2340
2341 /*
2342 * If no interface was explicitly specified, choose an
2343 * appropriate one according to the given multicast address.
2344 */
2345 if (mreq.ipv6mr_interface == 0) {
2346 struct rtentry *rt;
2347 union {
2348 struct sockaddr dst;
2349 struct sockaddr_in dst4;
2350 struct sockaddr_in6 dst6;
2351 } u;
2352 struct route ro;
2353
2354 /*
2355 * Look up the routing table for the
2356 * address, and choose the outgoing interface.
2357 * XXX: is it a good approach?
2358 */
2359 memset(&ro, 0, sizeof(ro));
2360 if (IN6_IS_ADDR_V4MAPPED(ia))
2361 sockaddr_in_init(&u.dst4, ia4, 0);
2362 else
2363 sockaddr_in6_init(&u.dst6, ia, 0, 0, 0);
2364 error = rtcache_setdst(&ro, &u.dst);
2365 if (error != 0)
2366 return error;
2367 rt = rtcache_init(&ro);
2368 *ifp = rt != NULL ?
2369 if_get_byindex(rt->rt_ifp->if_index, psref) : NULL;
2370 rtcache_unref(rt, &ro);
2371 rtcache_free(&ro);
2372 } else {
2373 /*
2374 * If the interface is specified, validate it.
2375 */
2376 *ifp = if_get_byindex(mreq.ipv6mr_interface, psref);
2377 if (*ifp == NULL)
2378 return ENXIO; /* XXX EINVAL? */
2379 }
2380 if (sizeof(*ia) == l)
2381 memcpy(v, ia, l);
2382 else
2383 memcpy(v, ia4, l);
2384 return 0;
2385 }
2386
2387 /*
2388 * Set the IP6 multicast options in response to user setsockopt().
2389 */
2390 static int
2391 ip6_setmoptions(const struct sockopt *sopt, struct in6pcb *in6p)
2392 {
2393 int error = 0;
2394 u_int loop, ifindex;
2395 struct ipv6_mreq mreq;
2396 struct in6_addr ia;
2397 struct ifnet *ifp;
2398 struct ip6_moptions *im6o = in6p->in6p_moptions;
2399 struct in6_multi_mship *imm;
2400
2401 KASSERT(in6p_locked(in6p));
2402
2403 if (im6o == NULL) {
2404 /*
2405 * No multicast option buffer attached to the pcb;
2406 * allocate one and initialize to default values.
2407 */
2408 im6o = malloc(sizeof(*im6o), M_IPMOPTS, M_NOWAIT);
2409 if (im6o == NULL)
2410 return (ENOBUFS);
2411 in6p->in6p_moptions = im6o;
2412 im6o->im6o_multicast_if_index = 0;
2413 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2414 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2415 LIST_INIT(&im6o->im6o_memberships);
2416 }
2417
2418 switch (sopt->sopt_name) {
2419
2420 case IPV6_MULTICAST_IF: {
2421 int s;
2422 /*
2423 * Select the interface for outgoing multicast packets.
2424 */
2425 error = sockopt_get(sopt, &ifindex, sizeof(ifindex));
2426 if (error != 0)
2427 break;
2428
2429 s = pserialize_read_enter();
2430 if (ifindex != 0) {
2431 if ((ifp = if_byindex(ifindex)) == NULL) {
2432 pserialize_read_exit(s);
2433 error = ENXIO; /* XXX EINVAL? */
2434 break;
2435 }
2436 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2437 pserialize_read_exit(s);
2438 error = EADDRNOTAVAIL;
2439 break;
2440 }
2441 } else
2442 ifp = NULL;
2443 im6o->im6o_multicast_if_index = if_get_index(ifp);
2444 pserialize_read_exit(s);
2445 break;
2446 }
2447
2448 case IPV6_MULTICAST_HOPS:
2449 {
2450 /*
2451 * Set the IP6 hoplimit for outgoing multicast packets.
2452 */
2453 int optval;
2454
2455 error = sockopt_getint(sopt, &optval);
2456 if (error != 0)
2457 break;
2458
2459 if (optval < -1 || optval >= 256)
2460 error = EINVAL;
2461 else if (optval == -1)
2462 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2463 else
2464 im6o->im6o_multicast_hlim = optval;
2465 break;
2466 }
2467
2468 case IPV6_MULTICAST_LOOP:
2469 /*
2470 * Set the loopback flag for outgoing multicast packets.
2471 * Must be zero or one.
2472 */
2473 error = sockopt_get(sopt, &loop, sizeof(loop));
2474 if (error != 0)
2475 break;
2476 if (loop > 1) {
2477 error = EINVAL;
2478 break;
2479 }
2480 im6o->im6o_multicast_loop = loop;
2481 break;
2482
2483 case IPV6_JOIN_GROUP: {
2484 int bound;
2485 struct psref psref;
2486 /*
2487 * Add a multicast group membership.
2488 * Group must be a valid IP6 multicast address.
2489 */
2490 bound = curlwp_bind();
2491 ifp = NULL;
2492 error = ip6_get_membership(sopt, &ifp, &psref, &ia, sizeof(ia));
2493 if (error != 0) {
2494 KASSERT(ifp == NULL);
2495 curlwp_bindx(bound);
2496 return error;
2497 }
2498
2499 if (IN6_IS_ADDR_V4MAPPED(&ia)) {
2500 error = ip_setmoptions(&in6p->in6p_v4moptions, sopt);
2501 goto put_break;
2502 }
2503 /*
2504 * See if we found an interface, and confirm that it
2505 * supports multicast
2506 */
2507 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2508 error = EADDRNOTAVAIL;
2509 goto put_break;
2510 }
2511
2512 if (in6_setscope(&ia, ifp, NULL)) {
2513 error = EADDRNOTAVAIL; /* XXX: should not happen */
2514 goto put_break;
2515 }
2516
2517 /*
2518 * See if the membership already exists.
2519 */
2520 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) {
2521 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2522 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2523 &ia))
2524 goto put_break;
2525 }
2526 if (imm != NULL) {
2527 error = EADDRINUSE;
2528 goto put_break;
2529 }
2530 /*
2531 * Everything looks good; add a new record to the multicast
2532 * address list for the given interface.
2533 */
2534 IFNET_LOCK(ifp);
2535 imm = in6_joingroup(ifp, &ia, &error, 0);
2536 IFNET_UNLOCK(ifp);
2537 if (imm == NULL)
2538 goto put_break;
2539 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2540 put_break:
2541 if_put(ifp, &psref);
2542 curlwp_bindx(bound);
2543 break;
2544 }
2545
2546 case IPV6_LEAVE_GROUP: {
2547 struct ifnet *in6m_ifp;
2548 /*
2549 * Drop a multicast group membership.
2550 * Group must be a valid IP6 multicast address.
2551 */
2552 error = sockopt_get(sopt, &mreq, sizeof(mreq));
2553 if (error != 0)
2554 break;
2555
2556 if (IN6_IS_ADDR_V4MAPPED(&mreq.ipv6mr_multiaddr)) {
2557 error = ip_setmoptions(&in6p->in6p_v4moptions, sopt);
2558 break;
2559 }
2560 /*
2561 * If an interface address was specified, get a pointer
2562 * to its ifnet structure.
2563 */
2564 if (mreq.ipv6mr_interface != 0) {
2565 if ((ifp = if_byindex(mreq.ipv6mr_interface)) == NULL) {
2566 error = ENXIO; /* XXX EINVAL? */
2567 break;
2568 }
2569 } else
2570 ifp = NULL;
2571
2572 /* Fill in the scope zone ID */
2573 if (ifp) {
2574 if (in6_setscope(&mreq.ipv6mr_multiaddr, ifp, NULL)) {
2575 /* XXX: should not happen */
2576 error = EADDRNOTAVAIL;
2577 break;
2578 }
2579 } else if (mreq.ipv6mr_interface != 0) {
2580 /*
2581 * XXX: This case would happens when the (positive)
2582 * index is in the valid range, but the corresponding
2583 * interface has been detached dynamically. The above
2584 * check probably avoids such case to happen here, but
2585 * we check it explicitly for safety.
2586 */
2587 error = EADDRNOTAVAIL;
2588 break;
2589 } else { /* ipv6mr_interface == 0 */
2590 struct sockaddr_in6 sa6_mc;
2591
2592 /*
2593 * The API spec says as follows:
2594 * If the interface index is specified as 0, the
2595 * system may choose a multicast group membership to
2596 * drop by matching the multicast address only.
2597 * On the other hand, we cannot disambiguate the scope
2598 * zone unless an interface is provided. Thus, we
2599 * check if there's ambiguity with the default scope
2600 * zone as the last resort.
2601 */
2602 sockaddr_in6_init(&sa6_mc, &mreq.ipv6mr_multiaddr,
2603 0, 0, 0);
2604 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2605 if (error != 0)
2606 break;
2607 mreq.ipv6mr_multiaddr = sa6_mc.sin6_addr;
2608 }
2609
2610 /*
2611 * Find the membership in the membership list.
2612 */
2613 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) {
2614 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2615 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2616 &mreq.ipv6mr_multiaddr))
2617 break;
2618 }
2619 if (imm == NULL) {
2620 /* Unable to resolve interface */
2621 error = EADDRNOTAVAIL;
2622 break;
2623 }
2624 /*
2625 * Give up the multicast address record to which the
2626 * membership points.
2627 */
2628 LIST_REMOVE(imm, i6mm_chain);
2629 in6m_ifp = imm->i6mm_maddr->in6m_ifp;
2630 IFNET_LOCK(in6m_ifp);
2631 in6_leavegroup(imm);
2632 /* in6m_ifp should not leave thanks to in6p_lock */
2633 IFNET_UNLOCK(in6m_ifp);
2634 break;
2635 }
2636
2637 default:
2638 error = EOPNOTSUPP;
2639 break;
2640 }
2641
2642 /*
2643 * If all options have default values, no need to keep the mbuf.
2644 */
2645 if (im6o->im6o_multicast_if_index == 0 &&
2646 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2647 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2648 LIST_EMPTY(&im6o->im6o_memberships)) {
2649 free(in6p->in6p_moptions, M_IPMOPTS);
2650 in6p->in6p_moptions = NULL;
2651 }
2652
2653 return (error);
2654 }
2655
2656 /*
2657 * Return the IP6 multicast options in response to user getsockopt().
2658 */
2659 static int
2660 ip6_getmoptions(struct sockopt *sopt, struct in6pcb *in6p)
2661 {
2662 u_int optval;
2663 int error;
2664 struct ip6_moptions *im6o = in6p->in6p_moptions;
2665
2666 switch (sopt->sopt_name) {
2667 case IPV6_MULTICAST_IF:
2668 if (im6o == NULL || im6o->im6o_multicast_if_index == 0)
2669 optval = 0;
2670 else
2671 optval = im6o->im6o_multicast_if_index;
2672
2673 error = sockopt_set(sopt, &optval, sizeof(optval));
2674 break;
2675
2676 case IPV6_MULTICAST_HOPS:
2677 if (im6o == NULL)
2678 optval = ip6_defmcasthlim;
2679 else
2680 optval = im6o->im6o_multicast_hlim;
2681
2682 error = sockopt_set(sopt, &optval, sizeof(optval));
2683 break;
2684
2685 case IPV6_MULTICAST_LOOP:
2686 if (im6o == NULL)
2687 optval = IPV6_DEFAULT_MULTICAST_LOOP;
2688 else
2689 optval = im6o->im6o_multicast_loop;
2690
2691 error = sockopt_set(sopt, &optval, sizeof(optval));
2692 break;
2693
2694 default:
2695 error = EOPNOTSUPP;
2696 }
2697
2698 return (error);
2699 }
2700
2701 /*
2702 * Discard the IP6 multicast options.
2703 */
2704 void
2705 ip6_freemoptions(struct ip6_moptions *im6o)
2706 {
2707 struct in6_multi_mship *imm, *nimm;
2708
2709 if (im6o == NULL)
2710 return;
2711
2712 /* The owner of im6o (in6p) should be protected by solock */
2713 LIST_FOREACH_SAFE(imm, &im6o->im6o_memberships, i6mm_chain, nimm) {
2714 struct ifnet *ifp;
2715
2716 LIST_REMOVE(imm, i6mm_chain);
2717
2718 ifp = imm->i6mm_maddr->in6m_ifp;
2719 IFNET_LOCK(ifp);
2720 in6_leavegroup(imm);
2721 /* ifp should not leave thanks to solock */
2722 IFNET_UNLOCK(ifp);
2723 }
2724 free(im6o, M_IPMOPTS);
2725 }
2726
2727 /*
2728 * Set IPv6 outgoing packet options based on advanced API.
2729 */
2730 int
2731 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2732 struct ip6_pktopts *stickyopt, kauth_cred_t cred, int uproto)
2733 {
2734 struct cmsghdr *cm = 0;
2735
2736 if (control == NULL || opt == NULL)
2737 return (EINVAL);
2738
2739 ip6_initpktopts(opt);
2740 if (stickyopt) {
2741 int error;
2742
2743 /*
2744 * If stickyopt is provided, make a local copy of the options
2745 * for this particular packet, then override them by ancillary
2746 * objects.
2747 * XXX: copypktopts() does not copy the cached route to a next
2748 * hop (if any). This is not very good in terms of efficiency,
2749 * but we can allow this since this option should be rarely
2750 * used.
2751 */
2752 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2753 return (error);
2754 }
2755
2756 /*
2757 * XXX: Currently, we assume all the optional information is stored
2758 * in a single mbuf.
2759 */
2760 if (control->m_next)
2761 return (EINVAL);
2762
2763 /* XXX if cm->cmsg_len is not aligned, control->m_len can become <0 */
2764 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2765 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2766 int error;
2767
2768 if (control->m_len < CMSG_LEN(0))
2769 return (EINVAL);
2770
2771 cm = mtod(control, struct cmsghdr *);
2772 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2773 return (EINVAL);
2774 if (cm->cmsg_level != IPPROTO_IPV6)
2775 continue;
2776
2777 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2778 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2779 if (error)
2780 return (error);
2781 }
2782
2783 return (0);
2784 }
2785
2786 /*
2787 * Set a particular packet option, as a sticky option or an ancillary data
2788 * item. "len" can be 0 only when it's a sticky option.
2789 * We have 4 cases of combination of "sticky" and "cmsg":
2790 * "sticky=0, cmsg=0": impossible
2791 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2792 * "sticky=1, cmsg=0": RFC3542 socket option
2793 * "sticky=1, cmsg=1": RFC2292 socket option
2794 */
2795 static int
2796 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2797 kauth_cred_t cred, int sticky, int cmsg, int uproto)
2798 {
2799 int minmtupolicy;
2800 int error;
2801
2802 if (!sticky && !cmsg) {
2803 #ifdef DIAGNOSTIC
2804 printf("ip6_setpktopt: impossible case\n");
2805 #endif
2806 return (EINVAL);
2807 }
2808
2809 /*
2810 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2811 * not be specified in the context of RFC3542. Conversely,
2812 * RFC3542 types should not be specified in the context of RFC2292.
2813 */
2814 if (!cmsg) {
2815 switch (optname) {
2816 case IPV6_2292PKTINFO:
2817 case IPV6_2292HOPLIMIT:
2818 case IPV6_2292NEXTHOP:
2819 case IPV6_2292HOPOPTS:
2820 case IPV6_2292DSTOPTS:
2821 case IPV6_2292RTHDR:
2822 case IPV6_2292PKTOPTIONS:
2823 return (ENOPROTOOPT);
2824 }
2825 }
2826 if (sticky && cmsg) {
2827 switch (optname) {
2828 case IPV6_PKTINFO:
2829 case IPV6_HOPLIMIT:
2830 case IPV6_NEXTHOP:
2831 case IPV6_HOPOPTS:
2832 case IPV6_DSTOPTS:
2833 case IPV6_RTHDRDSTOPTS:
2834 case IPV6_RTHDR:
2835 case IPV6_USE_MIN_MTU:
2836 case IPV6_DONTFRAG:
2837 case IPV6_OTCLASS:
2838 case IPV6_TCLASS:
2839 case IPV6_PREFER_TEMPADDR: /* XXX not an RFC3542 option */
2840 return (ENOPROTOOPT);
2841 }
2842 }
2843
2844 switch (optname) {
2845 #ifdef RFC2292
2846 case IPV6_2292PKTINFO:
2847 #endif
2848 case IPV6_PKTINFO:
2849 {
2850 struct in6_pktinfo *pktinfo;
2851
2852 if (len != sizeof(struct in6_pktinfo))
2853 return (EINVAL);
2854
2855 pktinfo = (struct in6_pktinfo *)buf;
2856
2857 /*
2858 * An application can clear any sticky IPV6_PKTINFO option by
2859 * doing a "regular" setsockopt with ipi6_addr being
2860 * in6addr_any and ipi6_ifindex being zero.
2861 * [RFC 3542, Section 6]
2862 */
2863 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2864 pktinfo->ipi6_ifindex == 0 &&
2865 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2866 ip6_clearpktopts(opt, optname);
2867 break;
2868 }
2869
2870 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2871 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2872 return (EINVAL);
2873 }
2874
2875 /* Validate the interface index if specified. */
2876 if (pktinfo->ipi6_ifindex) {
2877 struct ifnet *ifp;
2878 int s = pserialize_read_enter();
2879 ifp = if_byindex(pktinfo->ipi6_ifindex);
2880 if (ifp == NULL) {
2881 pserialize_read_exit(s);
2882 return ENXIO;
2883 }
2884 pserialize_read_exit(s);
2885 }
2886
2887 /*
2888 * We store the address anyway, and let in6_selectsrc()
2889 * validate the specified address. This is because ipi6_addr
2890 * may not have enough information about its scope zone, and
2891 * we may need additional information (such as outgoing
2892 * interface or the scope zone of a destination address) to
2893 * disambiguate the scope.
2894 * XXX: the delay of the validation may confuse the
2895 * application when it is used as a sticky option.
2896 */
2897 if (opt->ip6po_pktinfo == NULL) {
2898 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2899 M_IP6OPT, M_NOWAIT);
2900 if (opt->ip6po_pktinfo == NULL)
2901 return (ENOBUFS);
2902 }
2903 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
2904 break;
2905 }
2906
2907 #ifdef RFC2292
2908 case IPV6_2292HOPLIMIT:
2909 #endif
2910 case IPV6_HOPLIMIT:
2911 {
2912 int *hlimp;
2913
2914 /*
2915 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2916 * to simplify the ordering among hoplimit options.
2917 */
2918 if (optname == IPV6_HOPLIMIT && sticky)
2919 return (ENOPROTOOPT);
2920
2921 if (len != sizeof(int))
2922 return (EINVAL);
2923 hlimp = (int *)buf;
2924 if (*hlimp < -1 || *hlimp > 255)
2925 return (EINVAL);
2926
2927 opt->ip6po_hlim = *hlimp;
2928 break;
2929 }
2930
2931 case IPV6_OTCLASS:
2932 if (len != sizeof(u_int8_t))
2933 return (EINVAL);
2934
2935 opt->ip6po_tclass = *(u_int8_t *)buf;
2936 break;
2937
2938 case IPV6_TCLASS:
2939 {
2940 int tclass;
2941
2942 if (len != sizeof(int))
2943 return (EINVAL);
2944 tclass = *(int *)buf;
2945 if (tclass < -1 || tclass > 255)
2946 return (EINVAL);
2947
2948 opt->ip6po_tclass = tclass;
2949 break;
2950 }
2951
2952 #ifdef RFC2292
2953 case IPV6_2292NEXTHOP:
2954 #endif
2955 case IPV6_NEXTHOP:
2956 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6,
2957 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL);
2958 if (error)
2959 return (error);
2960
2961 if (len == 0) { /* just remove the option */
2962 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2963 break;
2964 }
2965
2966 /* check if cmsg_len is large enough for sa_len */
2967 if (len < sizeof(struct sockaddr) || len < *buf)
2968 return (EINVAL);
2969
2970 switch (((struct sockaddr *)buf)->sa_family) {
2971 case AF_INET6:
2972 {
2973 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2974
2975 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2976 return (EINVAL);
2977
2978 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2979 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2980 return (EINVAL);
2981 }
2982 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
2983 != 0) {
2984 return (error);
2985 }
2986 break;
2987 }
2988 case AF_LINK: /* eventually be supported? */
2989 default:
2990 return (EAFNOSUPPORT);
2991 }
2992
2993 /* turn off the previous option, then set the new option. */
2994 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2995 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2996 if (opt->ip6po_nexthop == NULL)
2997 return (ENOBUFS);
2998 memcpy(opt->ip6po_nexthop, buf, *buf);
2999 break;
3000
3001 #ifdef RFC2292
3002 case IPV6_2292HOPOPTS:
3003 #endif
3004 case IPV6_HOPOPTS:
3005 {
3006 struct ip6_hbh *hbh;
3007 int hbhlen;
3008
3009 /*
3010 * XXX: We don't allow a non-privileged user to set ANY HbH
3011 * options, since per-option restriction has too much
3012 * overhead.
3013 */
3014 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6,
3015 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL);
3016 if (error)
3017 return (error);
3018
3019 if (len == 0) {
3020 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3021 break; /* just remove the option */
3022 }
3023
3024 /* message length validation */
3025 if (len < sizeof(struct ip6_hbh))
3026 return (EINVAL);
3027 hbh = (struct ip6_hbh *)buf;
3028 hbhlen = (hbh->ip6h_len + 1) << 3;
3029 if (len != hbhlen)
3030 return (EINVAL);
3031
3032 /* turn off the previous option, then set the new option. */
3033 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3034 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3035 if (opt->ip6po_hbh == NULL)
3036 return (ENOBUFS);
3037 memcpy(opt->ip6po_hbh, hbh, hbhlen);
3038
3039 break;
3040 }
3041
3042 #ifdef RFC2292
3043 case IPV6_2292DSTOPTS:
3044 #endif
3045 case IPV6_DSTOPTS:
3046 case IPV6_RTHDRDSTOPTS:
3047 {
3048 struct ip6_dest *dest, **newdest = NULL;
3049 int destlen;
3050
3051 /* XXX: see the comment for IPV6_HOPOPTS */
3052 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6,
3053 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL);
3054 if (error)
3055 return (error);
3056
3057 if (len == 0) {
3058 ip6_clearpktopts(opt, optname);
3059 break; /* just remove the option */
3060 }
3061
3062 /* message length validation */
3063 if (len < sizeof(struct ip6_dest))
3064 return (EINVAL);
3065 dest = (struct ip6_dest *)buf;
3066 destlen = (dest->ip6d_len + 1) << 3;
3067 if (len != destlen)
3068 return (EINVAL);
3069 /*
3070 * Determine the position that the destination options header
3071 * should be inserted; before or after the routing header.
3072 */
3073 switch (optname) {
3074 case IPV6_2292DSTOPTS:
3075 /*
3076 * The old advanced API is ambiguous on this point.
3077 * Our approach is to determine the position based
3078 * according to the existence of a routing header.
3079 * Note, however, that this depends on the order of the
3080 * extension headers in the ancillary data; the 1st
3081 * part of the destination options header must appear
3082 * before the routing header in the ancillary data,
3083 * too.
3084 * RFC3542 solved the ambiguity by introducing
3085 * separate ancillary data or option types.
3086 */
3087 if (opt->ip6po_rthdr == NULL)
3088 newdest = &opt->ip6po_dest1;
3089 else
3090 newdest = &opt->ip6po_dest2;
3091 break;
3092 case IPV6_RTHDRDSTOPTS:
3093 newdest = &opt->ip6po_dest1;
3094 break;
3095 case IPV6_DSTOPTS:
3096 newdest = &opt->ip6po_dest2;
3097 break;
3098 }
3099
3100 /* turn off the previous option, then set the new option. */
3101 ip6_clearpktopts(opt, optname);
3102 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3103 if (*newdest == NULL)
3104 return (ENOBUFS);
3105 memcpy(*newdest, dest, destlen);
3106
3107 break;
3108 }
3109
3110 #ifdef RFC2292
3111 case IPV6_2292RTHDR:
3112 #endif
3113 case IPV6_RTHDR:
3114 {
3115 struct ip6_rthdr *rth;
3116 int rthlen;
3117
3118 if (len == 0) {
3119 ip6_clearpktopts(opt, IPV6_RTHDR);
3120 break; /* just remove the option */
3121 }
3122
3123 /* message length validation */
3124 if (len < sizeof(struct ip6_rthdr))
3125 return (EINVAL);
3126 rth = (struct ip6_rthdr *)buf;
3127 rthlen = (rth->ip6r_len + 1) << 3;
3128 if (len != rthlen)
3129 return (EINVAL);
3130 switch (rth->ip6r_type) {
3131 case IPV6_RTHDR_TYPE_0:
3132 /* Dropped, RFC5095. */
3133 default:
3134 return (EINVAL); /* not supported */
3135 }
3136 /* turn off the previous option */
3137 ip6_clearpktopts(opt, IPV6_RTHDR);
3138 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3139 if (opt->ip6po_rthdr == NULL)
3140 return (ENOBUFS);
3141 memcpy(opt->ip6po_rthdr, rth, rthlen);
3142 break;
3143 }
3144
3145 case IPV6_USE_MIN_MTU:
3146 if (len != sizeof(int))
3147 return (EINVAL);
3148 minmtupolicy = *(int *)buf;
3149 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3150 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3151 minmtupolicy != IP6PO_MINMTU_ALL) {
3152 return (EINVAL);
3153 }
3154 opt->ip6po_minmtu = minmtupolicy;
3155 break;
3156
3157 case IPV6_DONTFRAG:
3158 if (len != sizeof(int))
3159 return (EINVAL);
3160
3161 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3162 /*
3163 * we ignore this option for TCP sockets.
3164 * (RFC3542 leaves this case unspecified.)
3165 */
3166 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3167 } else
3168 opt->ip6po_flags |= IP6PO_DONTFRAG;
3169 break;
3170
3171 case IPV6_PREFER_TEMPADDR:
3172 {
3173 int preftemp;
3174
3175 if (len != sizeof(int))
3176 return (EINVAL);
3177 preftemp = *(int *)buf;
3178 switch (preftemp) {
3179 case IP6PO_TEMPADDR_SYSTEM:
3180 case IP6PO_TEMPADDR_NOTPREFER:
3181 case IP6PO_TEMPADDR_PREFER:
3182 break;
3183 default:
3184 return (EINVAL);
3185 }
3186 opt->ip6po_prefer_tempaddr = preftemp;
3187 break;
3188 }
3189
3190 default:
3191 return (ENOPROTOOPT);
3192 } /* end of switch */
3193
3194 return (0);
3195 }
3196
3197 /*
3198 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3199 * packet to the input queue of a specified interface. Note that this
3200 * calls the output routine of the loopback "driver", but with an interface
3201 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3202 */
3203 void
3204 ip6_mloopback(struct ifnet *ifp, struct mbuf *m,
3205 const struct sockaddr_in6 *dst)
3206 {
3207 struct mbuf *copym;
3208 struct ip6_hdr *ip6;
3209
3210 copym = m_copypacket(m, M_DONTWAIT);
3211 if (copym == NULL)
3212 return;
3213
3214 /*
3215 * Make sure to deep-copy IPv6 header portion in case the data
3216 * is in an mbuf cluster, so that we can safely override the IPv6
3217 * header portion later.
3218 */
3219 if ((copym->m_flags & M_EXT) != 0 ||
3220 copym->m_len < sizeof(struct ip6_hdr)) {
3221 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3222 if (copym == NULL)
3223 return;
3224 }
3225
3226 #ifdef DIAGNOSTIC
3227 if (copym->m_len < sizeof(*ip6)) {
3228 m_freem(copym);
3229 return;
3230 }
3231 #endif
3232
3233 ip6 = mtod(copym, struct ip6_hdr *);
3234 /*
3235 * clear embedded scope identifiers if necessary.
3236 * in6_clearscope will touch the addresses only when necessary.
3237 */
3238 in6_clearscope(&ip6->ip6_src);
3239 in6_clearscope(&ip6->ip6_dst);
3240
3241 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL);
3242 }
3243
3244 /*
3245 * Chop IPv6 header off from the payload.
3246 */
3247 static int
3248 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3249 {
3250 struct mbuf *mh;
3251 struct ip6_hdr *ip6;
3252
3253 ip6 = mtod(m, struct ip6_hdr *);
3254 if (m->m_len > sizeof(*ip6)) {
3255 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3256 if (mh == NULL) {
3257 m_freem(m);
3258 return ENOBUFS;
3259 }
3260 M_MOVE_PKTHDR(mh, m);
3261 MH_ALIGN(mh, sizeof(*ip6));
3262 m->m_len -= sizeof(*ip6);
3263 m->m_data += sizeof(*ip6);
3264 mh->m_next = m;
3265 mh->m_len = sizeof(*ip6);
3266 memcpy(mtod(mh, void *), (void *)ip6, sizeof(*ip6));
3267 m = mh;
3268 }
3269 exthdrs->ip6e_ip6 = m;
3270 return 0;
3271 }
3272
3273 /*
3274 * Compute IPv6 extension header length.
3275 */
3276 int
3277 ip6_optlen(struct in6pcb *in6p)
3278 {
3279 int len;
3280
3281 if (!in6p->in6p_outputopts)
3282 return 0;
3283
3284 len = 0;
3285 #define elen(x) \
3286 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3287
3288 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3289 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3290 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3291 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3292 return len;
3293 #undef elen
3294 }
3295
3296 /*
3297 * Ensure sending address is valid.
3298 * Returns 0 on success, -1 if an error should be sent back or 1
3299 * if the packet could be dropped without error (protocol dependent).
3300 */
3301 static int
3302 ip6_ifaddrvalid(const struct in6_addr *src, const struct in6_addr *dst)
3303 {
3304 struct sockaddr_in6 sin6;
3305 int s, error;
3306 struct ifaddr *ifa;
3307 struct in6_ifaddr *ia6;
3308
3309 if (IN6_IS_ADDR_UNSPECIFIED(src))
3310 return 0;
3311
3312 memset(&sin6, 0, sizeof(sin6));
3313 sin6.sin6_family = AF_INET6;
3314 sin6.sin6_len = sizeof(sin6);
3315 sin6.sin6_addr = *src;
3316
3317 s = pserialize_read_enter();
3318 ifa = ifa_ifwithaddr(sin6tosa(&sin6));
3319 if ((ia6 = ifatoia6(ifa)) == NULL ||
3320 ia6->ia6_flags & (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED))
3321 error = -1;
3322 else if (ia6->ia6_flags & IN6_IFF_TENTATIVE)
3323 error = 1;
3324 else if (ia6->ia6_flags & IN6_IFF_DETACHED &&
3325 (sin6.sin6_addr = *dst, ifa_ifwithaddr(sin6tosa(&sin6)) == NULL))
3326 /* Allow internal traffic to DETACHED addresses */
3327 error = 1;
3328 else
3329 error = 0;
3330 pserialize_read_exit(s);
3331
3332 return error;
3333 }
3334