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