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