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