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