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