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