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