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