ip6_output.c revision 1.153 1 /* $NetBSD: ip6_output.c,v 1.153 2013/06/05 19:01:26 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.153 2013/06/05 19:01:26 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 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 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 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 /* 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 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 /* 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 /*
909 * ip6f must be valid if error is 0. But how
910 * can a compiler be expected to infer this?
911 */
912 ip6f = NULL;
913 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
914 if (error) {
915 IP6_STATINC(IP6_STAT_ODROPPED);
916 goto sendorfree;
917 }
918 ip6f->ip6f_offlg = htons((u_int16_t)((off - hlen) & ~7));
919 if (off + len >= tlen)
920 len = tlen - off;
921 else
922 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
923 mhip6->ip6_plen = htons((u_int16_t)(len + hlen +
924 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
925 if ((m_frgpart = m_copy(m0, off, len)) == 0) {
926 error = ENOBUFS;
927 IP6_STATINC(IP6_STAT_ODROPPED);
928 goto sendorfree;
929 }
930 for (mlast = m; mlast->m_next; mlast = mlast->m_next)
931 ;
932 mlast->m_next = m_frgpart;
933 m->m_pkthdr.len = len + hlen + sizeof(*ip6f);
934 m->m_pkthdr.rcvif = NULL;
935 ip6f->ip6f_reserved = 0;
936 ip6f->ip6f_ident = id;
937 ip6f->ip6f_nxt = nextproto;
938 IP6_STATINC(IP6_STAT_OFRAGMENTS);
939 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
940 }
941
942 in6_ifstat_inc(ifp, ifs6_out_fragok);
943 }
944
945 /*
946 * Remove leading garbages.
947 */
948 sendorfree:
949 m = m0->m_nextpkt;
950 m0->m_nextpkt = 0;
951 m_freem(m0);
952 for (m0 = m; m; m = m0) {
953 m0 = m->m_nextpkt;
954 m->m_nextpkt = 0;
955 if (error == 0) {
956 struct in6_ifaddr *ia6;
957 ip6 = mtod(m, struct ip6_hdr *);
958 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
959 if (ia6) {
960 /*
961 * Record statistics for this interface
962 * address.
963 */
964 ia6->ia_ifa.ifa_data.ifad_outbytes +=
965 m->m_pkthdr.len;
966 }
967 KASSERT(dst != NULL);
968 error = nd6_output(ifp, origifp, m, dst, rt);
969 } else
970 m_freem(m);
971 }
972
973 if (error == 0)
974 IP6_STATINC(IP6_STAT_FRAGMENTED);
975
976 done:
977 rtcache_free(&ip6route);
978
979 #ifdef IPSEC
980 if (sp != NULL)
981 KEY_FREESP(&sp);
982 #endif /* IPSEC */
983
984
985 return (error);
986
987 freehdrs:
988 m_freem(exthdrs.ip6e_hbh); /* m_freem will check if mbuf is 0 */
989 m_freem(exthdrs.ip6e_dest1);
990 m_freem(exthdrs.ip6e_rthdr);
991 m_freem(exthdrs.ip6e_dest2);
992 /* FALLTHROUGH */
993 bad:
994 m_freem(m);
995 goto done;
996 badscope:
997 IP6_STATINC(IP6_STAT_BADSCOPE);
998 in6_ifstat_inc(origifp, ifs6_out_discard);
999 if (error == 0)
1000 error = EHOSTUNREACH; /* XXX */
1001 goto bad;
1002 }
1003
1004 static int
1005 ip6_copyexthdr(struct mbuf **mp, void *hdr, int hlen)
1006 {
1007 struct mbuf *m;
1008
1009 if (hlen > MCLBYTES)
1010 return (ENOBUFS); /* XXX */
1011
1012 MGET(m, M_DONTWAIT, MT_DATA);
1013 if (!m)
1014 return (ENOBUFS);
1015
1016 if (hlen > MLEN) {
1017 MCLGET(m, M_DONTWAIT);
1018 if ((m->m_flags & M_EXT) == 0) {
1019 m_free(m);
1020 return (ENOBUFS);
1021 }
1022 }
1023 m->m_len = hlen;
1024 if (hdr)
1025 bcopy(hdr, mtod(m, void *), hlen);
1026
1027 *mp = m;
1028 return (0);
1029 }
1030
1031 /*
1032 * Process a delayed payload checksum calculation.
1033 */
1034 void
1035 in6_delayed_cksum(struct mbuf *m)
1036 {
1037 uint16_t csum, offset;
1038
1039 KASSERT((m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1040 KASSERT((~m->m_pkthdr.csum_flags & (M_CSUM_UDPv6|M_CSUM_TCPv6)) != 0);
1041 KASSERT((m->m_pkthdr.csum_flags
1042 & (M_CSUM_UDPv4|M_CSUM_TCPv4|M_CSUM_TSOv4)) == 0);
1043
1044 offset = M_CSUM_DATA_IPv6_HL(m->m_pkthdr.csum_data);
1045 csum = in6_cksum(m, 0, offset, m->m_pkthdr.len - offset);
1046 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv6) != 0) {
1047 csum = 0xffff;
1048 }
1049
1050 offset += M_CSUM_DATA_IPv6_OFFSET(m->m_pkthdr.csum_data);
1051 if ((offset + sizeof(csum)) > m->m_len) {
1052 m_copyback(m, offset, sizeof(csum), &csum);
1053 } else {
1054 *(uint16_t *)(mtod(m, char *) + offset) = csum;
1055 }
1056 }
1057
1058 /*
1059 * Insert jumbo payload option.
1060 */
1061 static int
1062 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1063 {
1064 struct mbuf *mopt;
1065 u_int8_t *optbuf;
1066 u_int32_t v;
1067
1068 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1069
1070 /*
1071 * If there is no hop-by-hop options header, allocate new one.
1072 * If there is one but it doesn't have enough space to store the
1073 * jumbo payload option, allocate a cluster to store the whole options.
1074 * Otherwise, use it to store the options.
1075 */
1076 if (exthdrs->ip6e_hbh == 0) {
1077 MGET(mopt, M_DONTWAIT, MT_DATA);
1078 if (mopt == 0)
1079 return (ENOBUFS);
1080 mopt->m_len = JUMBOOPTLEN;
1081 optbuf = mtod(mopt, u_int8_t *);
1082 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1083 exthdrs->ip6e_hbh = mopt;
1084 } else {
1085 struct ip6_hbh *hbh;
1086
1087 mopt = exthdrs->ip6e_hbh;
1088 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1089 /*
1090 * XXX assumption:
1091 * - exthdrs->ip6e_hbh is not referenced from places
1092 * other than exthdrs.
1093 * - exthdrs->ip6e_hbh is not an mbuf chain.
1094 */
1095 int oldoptlen = mopt->m_len;
1096 struct mbuf *n;
1097
1098 /*
1099 * XXX: give up if the whole (new) hbh header does
1100 * not fit even in an mbuf cluster.
1101 */
1102 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1103 return (ENOBUFS);
1104
1105 /*
1106 * As a consequence, we must always prepare a cluster
1107 * at this point.
1108 */
1109 MGET(n, M_DONTWAIT, MT_DATA);
1110 if (n) {
1111 MCLGET(n, M_DONTWAIT);
1112 if ((n->m_flags & M_EXT) == 0) {
1113 m_freem(n);
1114 n = NULL;
1115 }
1116 }
1117 if (!n)
1118 return (ENOBUFS);
1119 n->m_len = oldoptlen + JUMBOOPTLEN;
1120 bcopy(mtod(mopt, void *), mtod(n, void *),
1121 oldoptlen);
1122 optbuf = mtod(n, u_int8_t *) + oldoptlen;
1123 m_freem(mopt);
1124 mopt = exthdrs->ip6e_hbh = n;
1125 } else {
1126 optbuf = mtod(mopt, u_int8_t *) + mopt->m_len;
1127 mopt->m_len += JUMBOOPTLEN;
1128 }
1129 optbuf[0] = IP6OPT_PADN;
1130 optbuf[1] = 0;
1131
1132 /*
1133 * Adjust the header length according to the pad and
1134 * the jumbo payload option.
1135 */
1136 hbh = mtod(mopt, struct ip6_hbh *);
1137 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1138 }
1139
1140 /* fill in the option. */
1141 optbuf[2] = IP6OPT_JUMBO;
1142 optbuf[3] = 4;
1143 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1144 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1145
1146 /* finally, adjust the packet header length */
1147 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1148
1149 return (0);
1150 #undef JUMBOOPTLEN
1151 }
1152
1153 /*
1154 * Insert fragment header and copy unfragmentable header portions.
1155 *
1156 * *frghdrp will not be read, and it is guaranteed that either an
1157 * error is returned or that *frghdrp will point to space allocated
1158 * for the fragment header.
1159 */
1160 static int
1161 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1162 struct ip6_frag **frghdrp)
1163 {
1164 struct mbuf *n, *mlast;
1165
1166 if (hlen > sizeof(struct ip6_hdr)) {
1167 n = m_copym(m0, sizeof(struct ip6_hdr),
1168 hlen - sizeof(struct ip6_hdr), M_DONTWAIT);
1169 if (n == 0)
1170 return (ENOBUFS);
1171 m->m_next = n;
1172 } else
1173 n = m;
1174
1175 /* Search for the last mbuf of unfragmentable part. */
1176 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1177 ;
1178
1179 if ((mlast->m_flags & M_EXT) == 0 &&
1180 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1181 /* use the trailing space of the last mbuf for the fragment hdr */
1182 *frghdrp = (struct ip6_frag *)(mtod(mlast, char *) +
1183 mlast->m_len);
1184 mlast->m_len += sizeof(struct ip6_frag);
1185 m->m_pkthdr.len += sizeof(struct ip6_frag);
1186 } else {
1187 /* allocate a new mbuf for the fragment header */
1188 struct mbuf *mfrg;
1189
1190 MGET(mfrg, M_DONTWAIT, MT_DATA);
1191 if (mfrg == 0)
1192 return (ENOBUFS);
1193 mfrg->m_len = sizeof(struct ip6_frag);
1194 *frghdrp = mtod(mfrg, struct ip6_frag *);
1195 mlast->m_next = mfrg;
1196 }
1197
1198 return (0);
1199 }
1200
1201 static int
1202 ip6_getpmtu(struct route *ro_pmtu, struct route *ro, struct ifnet *ifp,
1203 const struct in6_addr *dst, u_long *mtup, int *alwaysfragp)
1204 {
1205 struct rtentry *rt;
1206 u_int32_t mtu = 0;
1207 int alwaysfrag = 0;
1208 int error = 0;
1209
1210 if (ro_pmtu != ro) {
1211 union {
1212 struct sockaddr dst;
1213 struct sockaddr_in6 dst6;
1214 } u;
1215
1216 /* The first hop and the final destination may differ. */
1217 sockaddr_in6_init(&u.dst6, dst, 0, 0, 0);
1218 rt = rtcache_lookup(ro_pmtu, &u.dst);
1219 } else
1220 rt = rtcache_validate(ro_pmtu);
1221 if (rt != NULL) {
1222 u_int32_t ifmtu;
1223
1224 if (ifp == NULL)
1225 ifp = rt->rt_ifp;
1226 ifmtu = IN6_LINKMTU(ifp);
1227 mtu = rt->rt_rmx.rmx_mtu;
1228 if (mtu == 0)
1229 mtu = ifmtu;
1230 else if (mtu < IPV6_MMTU) {
1231 /*
1232 * RFC2460 section 5, last paragraph:
1233 * if we record ICMPv6 too big message with
1234 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1235 * or smaller, with fragment header attached.
1236 * (fragment header is needed regardless from the
1237 * packet size, for translators to identify packets)
1238 */
1239 alwaysfrag = 1;
1240 mtu = IPV6_MMTU;
1241 } else if (mtu > ifmtu) {
1242 /*
1243 * The MTU on the route is larger than the MTU on
1244 * the interface! This shouldn't happen, unless the
1245 * MTU of the interface has been changed after the
1246 * interface was brought up. Change the MTU in the
1247 * route to match the interface MTU (as long as the
1248 * field isn't locked).
1249 */
1250 mtu = ifmtu;
1251 if (!(rt->rt_rmx.rmx_locks & RTV_MTU))
1252 rt->rt_rmx.rmx_mtu = mtu;
1253 }
1254 } else if (ifp) {
1255 mtu = IN6_LINKMTU(ifp);
1256 } else
1257 error = EHOSTUNREACH; /* XXX */
1258
1259 *mtup = mtu;
1260 if (alwaysfragp)
1261 *alwaysfragp = alwaysfrag;
1262 return (error);
1263 }
1264
1265 /*
1266 * IP6 socket option processing.
1267 */
1268 int
1269 ip6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1270 {
1271 int optdatalen, uproto;
1272 void *optdata;
1273 struct in6pcb *in6p = sotoin6pcb(so);
1274 int error, optval;
1275 int level, optname;
1276
1277 KASSERT(sopt != NULL);
1278
1279 level = sopt->sopt_level;
1280 optname = sopt->sopt_name;
1281
1282 error = optval = 0;
1283 uproto = (int)so->so_proto->pr_protocol;
1284
1285 if (level != IPPROTO_IPV6) {
1286 return ENOPROTOOPT;
1287 }
1288 switch (op) {
1289 case PRCO_SETOPT:
1290 switch (optname) {
1291 #ifdef RFC2292
1292 case IPV6_2292PKTOPTIONS:
1293 error = ip6_pcbopts(&in6p->in6p_outputopts, so, sopt);
1294 break;
1295 #endif
1296
1297 /*
1298 * Use of some Hop-by-Hop options or some
1299 * Destination options, might require special
1300 * privilege. That is, normal applications
1301 * (without special privilege) might be forbidden
1302 * from setting certain options in outgoing packets,
1303 * and might never see certain options in received
1304 * packets. [RFC 2292 Section 6]
1305 * KAME specific note:
1306 * KAME prevents non-privileged users from sending or
1307 * receiving ANY hbh/dst options in order to avoid
1308 * overhead of parsing options in the kernel.
1309 */
1310 case IPV6_RECVHOPOPTS:
1311 case IPV6_RECVDSTOPTS:
1312 case IPV6_RECVRTHDRDSTOPTS:
1313 error = kauth_authorize_network(kauth_cred_get(),
1314 KAUTH_NETWORK_IPV6, KAUTH_REQ_NETWORK_IPV6_HOPBYHOP,
1315 NULL, NULL, NULL);
1316 if (error)
1317 break;
1318 /* FALLTHROUGH */
1319 case IPV6_UNICAST_HOPS:
1320 case IPV6_HOPLIMIT:
1321 case IPV6_FAITH:
1322
1323 case IPV6_RECVPKTINFO:
1324 case IPV6_RECVHOPLIMIT:
1325 case IPV6_RECVRTHDR:
1326 case IPV6_RECVPATHMTU:
1327 case IPV6_RECVTCLASS:
1328 case IPV6_V6ONLY:
1329 error = sockopt_getint(sopt, &optval);
1330 if (error)
1331 break;
1332 switch (optname) {
1333 case IPV6_UNICAST_HOPS:
1334 if (optval < -1 || optval >= 256)
1335 error = EINVAL;
1336 else {
1337 /* -1 = kernel default */
1338 in6p->in6p_hops = optval;
1339 }
1340 break;
1341 #define OPTSET(bit) \
1342 do { \
1343 if (optval) \
1344 in6p->in6p_flags |= (bit); \
1345 else \
1346 in6p->in6p_flags &= ~(bit); \
1347 } while (/*CONSTCOND*/ 0)
1348
1349 #ifdef RFC2292
1350 #define OPTSET2292(bit) \
1351 do { \
1352 in6p->in6p_flags |= IN6P_RFC2292; \
1353 if (optval) \
1354 in6p->in6p_flags |= (bit); \
1355 else \
1356 in6p->in6p_flags &= ~(bit); \
1357 } while (/*CONSTCOND*/ 0)
1358 #endif
1359
1360 #define OPTBIT(bit) (in6p->in6p_flags & (bit) ? 1 : 0)
1361
1362 case IPV6_RECVPKTINFO:
1363 #ifdef RFC2292
1364 /* cannot mix with RFC2292 */
1365 if (OPTBIT(IN6P_RFC2292)) {
1366 error = EINVAL;
1367 break;
1368 }
1369 #endif
1370 OPTSET(IN6P_PKTINFO);
1371 break;
1372
1373 case IPV6_HOPLIMIT:
1374 {
1375 struct ip6_pktopts **optp;
1376
1377 #ifdef RFC2292
1378 /* cannot mix with RFC2292 */
1379 if (OPTBIT(IN6P_RFC2292)) {
1380 error = EINVAL;
1381 break;
1382 }
1383 #endif
1384 optp = &in6p->in6p_outputopts;
1385 error = ip6_pcbopt(IPV6_HOPLIMIT,
1386 (u_char *)&optval,
1387 sizeof(optval),
1388 optp,
1389 kauth_cred_get(), uproto);
1390 break;
1391 }
1392
1393 case IPV6_RECVHOPLIMIT:
1394 #ifdef RFC2292
1395 /* cannot mix with RFC2292 */
1396 if (OPTBIT(IN6P_RFC2292)) {
1397 error = EINVAL;
1398 break;
1399 }
1400 #endif
1401 OPTSET(IN6P_HOPLIMIT);
1402 break;
1403
1404 case IPV6_RECVHOPOPTS:
1405 #ifdef RFC2292
1406 /* cannot mix with RFC2292 */
1407 if (OPTBIT(IN6P_RFC2292)) {
1408 error = EINVAL;
1409 break;
1410 }
1411 #endif
1412 OPTSET(IN6P_HOPOPTS);
1413 break;
1414
1415 case IPV6_RECVDSTOPTS:
1416 #ifdef RFC2292
1417 /* cannot mix with RFC2292 */
1418 if (OPTBIT(IN6P_RFC2292)) {
1419 error = EINVAL;
1420 break;
1421 }
1422 #endif
1423 OPTSET(IN6P_DSTOPTS);
1424 break;
1425
1426 case IPV6_RECVRTHDRDSTOPTS:
1427 #ifdef RFC2292
1428 /* cannot mix with RFC2292 */
1429 if (OPTBIT(IN6P_RFC2292)) {
1430 error = EINVAL;
1431 break;
1432 }
1433 #endif
1434 OPTSET(IN6P_RTHDRDSTOPTS);
1435 break;
1436
1437 case IPV6_RECVRTHDR:
1438 #ifdef RFC2292
1439 /* cannot mix with RFC2292 */
1440 if (OPTBIT(IN6P_RFC2292)) {
1441 error = EINVAL;
1442 break;
1443 }
1444 #endif
1445 OPTSET(IN6P_RTHDR);
1446 break;
1447
1448 case IPV6_FAITH:
1449 OPTSET(IN6P_FAITH);
1450 break;
1451
1452 case IPV6_RECVPATHMTU:
1453 /*
1454 * We ignore this option for TCP
1455 * sockets.
1456 * (RFC3542 leaves this case
1457 * unspecified.)
1458 */
1459 if (uproto != IPPROTO_TCP)
1460 OPTSET(IN6P_MTU);
1461 break;
1462
1463 case IPV6_V6ONLY:
1464 /*
1465 * make setsockopt(IPV6_V6ONLY)
1466 * available only prior to bind(2).
1467 * see ipng mailing list, Jun 22 2001.
1468 */
1469 if (in6p->in6p_lport ||
1470 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1471 error = EINVAL;
1472 break;
1473 }
1474 #ifdef INET6_BINDV6ONLY
1475 if (!optval)
1476 error = EINVAL;
1477 #else
1478 OPTSET(IN6P_IPV6_V6ONLY);
1479 #endif
1480 break;
1481 case IPV6_RECVTCLASS:
1482 #ifdef RFC2292
1483 /* cannot mix with RFC2292 XXX */
1484 if (OPTBIT(IN6P_RFC2292)) {
1485 error = EINVAL;
1486 break;
1487 }
1488 #endif
1489 OPTSET(IN6P_TCLASS);
1490 break;
1491
1492 }
1493 break;
1494
1495 case IPV6_OTCLASS:
1496 {
1497 struct ip6_pktopts **optp;
1498 u_int8_t tclass;
1499
1500 error = sockopt_get(sopt, &tclass, sizeof(tclass));
1501 if (error)
1502 break;
1503 optp = &in6p->in6p_outputopts;
1504 error = ip6_pcbopt(optname,
1505 (u_char *)&tclass,
1506 sizeof(tclass),
1507 optp,
1508 kauth_cred_get(), uproto);
1509 break;
1510 }
1511
1512 case IPV6_TCLASS:
1513 case IPV6_DONTFRAG:
1514 case IPV6_USE_MIN_MTU:
1515 error = sockopt_getint(sopt, &optval);
1516 if (error)
1517 break;
1518 {
1519 struct ip6_pktopts **optp;
1520 optp = &in6p->in6p_outputopts;
1521 error = ip6_pcbopt(optname,
1522 (u_char *)&optval,
1523 sizeof(optval),
1524 optp,
1525 kauth_cred_get(), uproto);
1526 break;
1527 }
1528
1529 #ifdef RFC2292
1530 case IPV6_2292PKTINFO:
1531 case IPV6_2292HOPLIMIT:
1532 case IPV6_2292HOPOPTS:
1533 case IPV6_2292DSTOPTS:
1534 case IPV6_2292RTHDR:
1535 /* RFC 2292 */
1536 error = sockopt_getint(sopt, &optval);
1537 if (error)
1538 break;
1539
1540 switch (optname) {
1541 case IPV6_2292PKTINFO:
1542 OPTSET2292(IN6P_PKTINFO);
1543 break;
1544 case IPV6_2292HOPLIMIT:
1545 OPTSET2292(IN6P_HOPLIMIT);
1546 break;
1547 case IPV6_2292HOPOPTS:
1548 /*
1549 * Check super-user privilege.
1550 * See comments for IPV6_RECVHOPOPTS.
1551 */
1552 error =
1553 kauth_authorize_network(kauth_cred_get(),
1554 KAUTH_NETWORK_IPV6,
1555 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL,
1556 NULL, NULL);
1557 if (error)
1558 return (error);
1559 OPTSET2292(IN6P_HOPOPTS);
1560 break;
1561 case IPV6_2292DSTOPTS:
1562 error =
1563 kauth_authorize_network(kauth_cred_get(),
1564 KAUTH_NETWORK_IPV6,
1565 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL,
1566 NULL, NULL);
1567 if (error)
1568 return (error);
1569 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
1570 break;
1571 case IPV6_2292RTHDR:
1572 OPTSET2292(IN6P_RTHDR);
1573 break;
1574 }
1575 break;
1576 #endif
1577 case IPV6_PKTINFO:
1578 case IPV6_HOPOPTS:
1579 case IPV6_RTHDR:
1580 case IPV6_DSTOPTS:
1581 case IPV6_RTHDRDSTOPTS:
1582 case IPV6_NEXTHOP: {
1583 /* new advanced API (RFC3542) */
1584 void *optbuf;
1585 int optbuflen;
1586 struct ip6_pktopts **optp;
1587
1588 #ifdef RFC2292
1589 /* cannot mix with RFC2292 */
1590 if (OPTBIT(IN6P_RFC2292)) {
1591 error = EINVAL;
1592 break;
1593 }
1594 #endif
1595
1596 optbuflen = sopt->sopt_size;
1597 optbuf = malloc(optbuflen, M_IP6OPT, M_NOWAIT);
1598 if (optbuf == NULL) {
1599 error = ENOBUFS;
1600 break;
1601 }
1602
1603 sockopt_get(sopt, optbuf, optbuflen);
1604 optp = &in6p->in6p_outputopts;
1605 error = ip6_pcbopt(optname, optbuf, optbuflen,
1606 optp, kauth_cred_get(), uproto);
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 error = ipsec6_set_policy(in6p, optname,
1658 sopt->sopt_data, sopt->sopt_size, kauth_cred_get());
1659 break;
1660 #endif /* IPSEC */
1661
1662 default:
1663 error = ENOPROTOOPT;
1664 break;
1665 }
1666 break;
1667
1668 case PRCO_GETOPT:
1669 switch (optname) {
1670 #ifdef RFC2292
1671 case IPV6_2292PKTOPTIONS:
1672 /*
1673 * RFC3542 (effectively) deprecated the
1674 * semantics of the 2292-style pktoptions.
1675 * Since it was not reliable in nature (i.e.,
1676 * applications had to expect the lack of some
1677 * information after all), it would make sense
1678 * to simplify this part by always returning
1679 * empty data.
1680 */
1681 break;
1682 #endif
1683
1684 case IPV6_RECVHOPOPTS:
1685 case IPV6_RECVDSTOPTS:
1686 case IPV6_RECVRTHDRDSTOPTS:
1687 case IPV6_UNICAST_HOPS:
1688 case IPV6_RECVPKTINFO:
1689 case IPV6_RECVHOPLIMIT:
1690 case IPV6_RECVRTHDR:
1691 case IPV6_RECVPATHMTU:
1692
1693 case IPV6_FAITH:
1694 case IPV6_V6ONLY:
1695 case IPV6_PORTRANGE:
1696 case IPV6_RECVTCLASS:
1697 switch (optname) {
1698
1699 case IPV6_RECVHOPOPTS:
1700 optval = OPTBIT(IN6P_HOPOPTS);
1701 break;
1702
1703 case IPV6_RECVDSTOPTS:
1704 optval = OPTBIT(IN6P_DSTOPTS);
1705 break;
1706
1707 case IPV6_RECVRTHDRDSTOPTS:
1708 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
1709 break;
1710
1711 case IPV6_UNICAST_HOPS:
1712 optval = in6p->in6p_hops;
1713 break;
1714
1715 case IPV6_RECVPKTINFO:
1716 optval = OPTBIT(IN6P_PKTINFO);
1717 break;
1718
1719 case IPV6_RECVHOPLIMIT:
1720 optval = OPTBIT(IN6P_HOPLIMIT);
1721 break;
1722
1723 case IPV6_RECVRTHDR:
1724 optval = OPTBIT(IN6P_RTHDR);
1725 break;
1726
1727 case IPV6_RECVPATHMTU:
1728 optval = OPTBIT(IN6P_MTU);
1729 break;
1730
1731 case IPV6_FAITH:
1732 optval = OPTBIT(IN6P_FAITH);
1733 break;
1734
1735 case IPV6_V6ONLY:
1736 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1737 break;
1738
1739 case IPV6_PORTRANGE:
1740 {
1741 int flags;
1742 flags = in6p->in6p_flags;
1743 if (flags & IN6P_HIGHPORT)
1744 optval = IPV6_PORTRANGE_HIGH;
1745 else if (flags & IN6P_LOWPORT)
1746 optval = IPV6_PORTRANGE_LOW;
1747 else
1748 optval = 0;
1749 break;
1750 }
1751 case IPV6_RECVTCLASS:
1752 optval = OPTBIT(IN6P_TCLASS);
1753 break;
1754
1755 }
1756 if (error)
1757 break;
1758 error = sockopt_setint(sopt, optval);
1759 break;
1760
1761 case IPV6_PATHMTU:
1762 {
1763 u_long pmtu = 0;
1764 struct ip6_mtuinfo mtuinfo;
1765 struct route *ro = &in6p->in6p_route;
1766
1767 if (!(so->so_state & SS_ISCONNECTED))
1768 return (ENOTCONN);
1769 /*
1770 * XXX: we dot not consider the case of source
1771 * routing, or optional information to specify
1772 * the outgoing interface.
1773 */
1774 error = ip6_getpmtu(ro, NULL, NULL,
1775 &in6p->in6p_faddr, &pmtu, NULL);
1776 if (error)
1777 break;
1778 if (pmtu > IPV6_MAXPACKET)
1779 pmtu = IPV6_MAXPACKET;
1780
1781 memset(&mtuinfo, 0, sizeof(mtuinfo));
1782 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
1783 optdata = (void *)&mtuinfo;
1784 optdatalen = sizeof(mtuinfo);
1785 if (optdatalen > MCLBYTES)
1786 return (EMSGSIZE); /* XXX */
1787 error = sockopt_set(sopt, optdata, optdatalen);
1788 break;
1789 }
1790
1791 #ifdef RFC2292
1792 case IPV6_2292PKTINFO:
1793 case IPV6_2292HOPLIMIT:
1794 case IPV6_2292HOPOPTS:
1795 case IPV6_2292RTHDR:
1796 case IPV6_2292DSTOPTS:
1797 switch (optname) {
1798 case IPV6_2292PKTINFO:
1799 optval = OPTBIT(IN6P_PKTINFO);
1800 break;
1801 case IPV6_2292HOPLIMIT:
1802 optval = OPTBIT(IN6P_HOPLIMIT);
1803 break;
1804 case IPV6_2292HOPOPTS:
1805 optval = OPTBIT(IN6P_HOPOPTS);
1806 break;
1807 case IPV6_2292RTHDR:
1808 optval = OPTBIT(IN6P_RTHDR);
1809 break;
1810 case IPV6_2292DSTOPTS:
1811 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
1812 break;
1813 }
1814 error = sockopt_setint(sopt, optval);
1815 break;
1816 #endif
1817 case IPV6_PKTINFO:
1818 case IPV6_HOPOPTS:
1819 case IPV6_RTHDR:
1820 case IPV6_DSTOPTS:
1821 case IPV6_RTHDRDSTOPTS:
1822 case IPV6_NEXTHOP:
1823 case IPV6_OTCLASS:
1824 case IPV6_TCLASS:
1825 case IPV6_DONTFRAG:
1826 case IPV6_USE_MIN_MTU:
1827 error = ip6_getpcbopt(in6p->in6p_outputopts,
1828 optname, sopt);
1829 break;
1830
1831 case IPV6_MULTICAST_IF:
1832 case IPV6_MULTICAST_HOPS:
1833 case IPV6_MULTICAST_LOOP:
1834 case IPV6_JOIN_GROUP:
1835 case IPV6_LEAVE_GROUP:
1836 error = ip6_getmoptions(sopt, in6p->in6p_moptions);
1837 break;
1838
1839 case IPV6_PORTALGO:
1840 optval = ((struct inpcb_hdr *)in6p)->inph_portalgo;
1841 error = sockopt_setint(sopt, optval);
1842 break;
1843
1844 #if defined(IPSEC)
1845 case IPV6_IPSEC_POLICY:
1846 {
1847 struct mbuf *m = NULL;
1848
1849 /* XXX this will return EINVAL as sopt is empty */
1850 error = ipsec6_get_policy(in6p, sopt->sopt_data,
1851 sopt->sopt_size, &m);
1852 if (!error)
1853 error = sockopt_setmbuf(sopt, m);
1854
1855 break;
1856 }
1857 #endif /* IPSEC */
1858
1859 default:
1860 error = ENOPROTOOPT;
1861 break;
1862 }
1863 break;
1864 }
1865 return (error);
1866 }
1867
1868 int
1869 ip6_raw_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1870 {
1871 int error = 0, optval;
1872 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
1873 struct in6pcb *in6p = sotoin6pcb(so);
1874 int level, optname;
1875
1876 KASSERT(sopt != NULL);
1877
1878 level = sopt->sopt_level;
1879 optname = sopt->sopt_name;
1880
1881 if (level != IPPROTO_IPV6) {
1882 return ENOPROTOOPT;
1883 }
1884
1885 switch (optname) {
1886 case IPV6_CHECKSUM:
1887 /*
1888 * For ICMPv6 sockets, no modification allowed for checksum
1889 * offset, permit "no change" values to help existing apps.
1890 *
1891 * XXX RFC3542 says: "An attempt to set IPV6_CHECKSUM
1892 * for an ICMPv6 socket will fail." The current
1893 * behavior does not meet RFC3542.
1894 */
1895 switch (op) {
1896 case PRCO_SETOPT:
1897 error = sockopt_getint(sopt, &optval);
1898 if (error)
1899 break;
1900 if ((optval % 2) != 0) {
1901 /* the API assumes even offset values */
1902 error = EINVAL;
1903 } else if (so->so_proto->pr_protocol ==
1904 IPPROTO_ICMPV6) {
1905 if (optval != icmp6off)
1906 error = EINVAL;
1907 } else
1908 in6p->in6p_cksum = optval;
1909 break;
1910
1911 case PRCO_GETOPT:
1912 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
1913 optval = icmp6off;
1914 else
1915 optval = in6p->in6p_cksum;
1916
1917 error = sockopt_setint(sopt, optval);
1918 break;
1919
1920 default:
1921 error = EINVAL;
1922 break;
1923 }
1924 break;
1925
1926 default:
1927 error = ENOPROTOOPT;
1928 break;
1929 }
1930
1931 return (error);
1932 }
1933
1934 #ifdef RFC2292
1935 /*
1936 * Set up IP6 options in pcb for insertion in output packets or
1937 * specifying behavior of outgoing packets.
1938 */
1939 static int
1940 ip6_pcbopts(struct ip6_pktopts **pktopt, struct socket *so,
1941 struct sockopt *sopt)
1942 {
1943 struct ip6_pktopts *opt = *pktopt;
1944 struct mbuf *m;
1945 int error = 0;
1946
1947 /* turn off any old options. */
1948 if (opt) {
1949 #ifdef DIAGNOSTIC
1950 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
1951 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
1952 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
1953 printf("ip6_pcbopts: all specified options are cleared.\n");
1954 #endif
1955 ip6_clearpktopts(opt, -1);
1956 } else {
1957 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
1958 if (opt == NULL)
1959 return (ENOBUFS);
1960 }
1961 *pktopt = NULL;
1962
1963 if (sopt == NULL || sopt->sopt_size == 0) {
1964 /*
1965 * Only turning off any previous options, regardless of
1966 * whether the opt is just created or given.
1967 */
1968 free(opt, M_IP6OPT);
1969 return (0);
1970 }
1971
1972 /* set options specified by user. */
1973 m = sockopt_getmbuf(sopt);
1974 if (m == NULL) {
1975 free(opt, M_IP6OPT);
1976 return (ENOBUFS);
1977 }
1978
1979 error = ip6_setpktopts(m, opt, NULL, kauth_cred_get(),
1980 so->so_proto->pr_protocol);
1981 m_freem(m);
1982 if (error != 0) {
1983 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
1984 free(opt, M_IP6OPT);
1985 return (error);
1986 }
1987 *pktopt = opt;
1988 return (0);
1989 }
1990 #endif
1991
1992 /*
1993 * initialize ip6_pktopts. beware that there are non-zero default values in
1994 * the struct.
1995 */
1996 void
1997 ip6_initpktopts(struct ip6_pktopts *opt)
1998 {
1999
2000 memset(opt, 0, sizeof(*opt));
2001 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2002 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2003 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2004 }
2005
2006 #define sin6tosa(sin6) ((struct sockaddr *)(sin6)) /* XXX */
2007 static int
2008 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2009 kauth_cred_t cred, int uproto)
2010 {
2011 struct ip6_pktopts *opt;
2012
2013 if (*pktopt == NULL) {
2014 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2015 M_NOWAIT);
2016 if (*pktopt == NULL)
2017 return (ENOBUFS);
2018
2019 ip6_initpktopts(*pktopt);
2020 }
2021 opt = *pktopt;
2022
2023 return (ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto));
2024 }
2025
2026 static int
2027 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2028 {
2029 void *optdata = NULL;
2030 int optdatalen = 0;
2031 struct ip6_ext *ip6e;
2032 int error = 0;
2033 struct in6_pktinfo null_pktinfo;
2034 int deftclass = 0, on;
2035 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2036
2037 switch (optname) {
2038 case IPV6_PKTINFO:
2039 if (pktopt && pktopt->ip6po_pktinfo)
2040 optdata = (void *)pktopt->ip6po_pktinfo;
2041 else {
2042 /* XXX: we don't have to do this every time... */
2043 memset(&null_pktinfo, 0, sizeof(null_pktinfo));
2044 optdata = (void *)&null_pktinfo;
2045 }
2046 optdatalen = sizeof(struct in6_pktinfo);
2047 break;
2048 case IPV6_OTCLASS:
2049 /* XXX */
2050 return (EINVAL);
2051 case IPV6_TCLASS:
2052 if (pktopt && pktopt->ip6po_tclass >= 0)
2053 optdata = (void *)&pktopt->ip6po_tclass;
2054 else
2055 optdata = (void *)&deftclass;
2056 optdatalen = sizeof(int);
2057 break;
2058 case IPV6_HOPOPTS:
2059 if (pktopt && pktopt->ip6po_hbh) {
2060 optdata = (void *)pktopt->ip6po_hbh;
2061 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2062 optdatalen = (ip6e->ip6e_len + 1) << 3;
2063 }
2064 break;
2065 case IPV6_RTHDR:
2066 if (pktopt && pktopt->ip6po_rthdr) {
2067 optdata = (void *)pktopt->ip6po_rthdr;
2068 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2069 optdatalen = (ip6e->ip6e_len + 1) << 3;
2070 }
2071 break;
2072 case IPV6_RTHDRDSTOPTS:
2073 if (pktopt && pktopt->ip6po_dest1) {
2074 optdata = (void *)pktopt->ip6po_dest1;
2075 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2076 optdatalen = (ip6e->ip6e_len + 1) << 3;
2077 }
2078 break;
2079 case IPV6_DSTOPTS:
2080 if (pktopt && pktopt->ip6po_dest2) {
2081 optdata = (void *)pktopt->ip6po_dest2;
2082 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2083 optdatalen = (ip6e->ip6e_len + 1) << 3;
2084 }
2085 break;
2086 case IPV6_NEXTHOP:
2087 if (pktopt && pktopt->ip6po_nexthop) {
2088 optdata = (void *)pktopt->ip6po_nexthop;
2089 optdatalen = pktopt->ip6po_nexthop->sa_len;
2090 }
2091 break;
2092 case IPV6_USE_MIN_MTU:
2093 if (pktopt)
2094 optdata = (void *)&pktopt->ip6po_minmtu;
2095 else
2096 optdata = (void *)&defminmtu;
2097 optdatalen = sizeof(int);
2098 break;
2099 case IPV6_DONTFRAG:
2100 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2101 on = 1;
2102 else
2103 on = 0;
2104 optdata = (void *)&on;
2105 optdatalen = sizeof(on);
2106 break;
2107 default: /* should not happen */
2108 #ifdef DIAGNOSTIC
2109 panic("ip6_getpcbopt: unexpected option\n");
2110 #endif
2111 return (ENOPROTOOPT);
2112 }
2113
2114 error = sockopt_set(sopt, optdata, optdatalen);
2115
2116 return (error);
2117 }
2118
2119 void
2120 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2121 {
2122 if (optname == -1 || optname == IPV6_PKTINFO) {
2123 if (pktopt->ip6po_pktinfo)
2124 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2125 pktopt->ip6po_pktinfo = NULL;
2126 }
2127 if (optname == -1 || optname == IPV6_HOPLIMIT)
2128 pktopt->ip6po_hlim = -1;
2129 if (optname == -1 || optname == IPV6_TCLASS)
2130 pktopt->ip6po_tclass = -1;
2131 if (optname == -1 || optname == IPV6_NEXTHOP) {
2132 rtcache_free(&pktopt->ip6po_nextroute);
2133 if (pktopt->ip6po_nexthop)
2134 free(pktopt->ip6po_nexthop, M_IP6OPT);
2135 pktopt->ip6po_nexthop = NULL;
2136 }
2137 if (optname == -1 || optname == IPV6_HOPOPTS) {
2138 if (pktopt->ip6po_hbh)
2139 free(pktopt->ip6po_hbh, M_IP6OPT);
2140 pktopt->ip6po_hbh = NULL;
2141 }
2142 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2143 if (pktopt->ip6po_dest1)
2144 free(pktopt->ip6po_dest1, M_IP6OPT);
2145 pktopt->ip6po_dest1 = NULL;
2146 }
2147 if (optname == -1 || optname == IPV6_RTHDR) {
2148 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2149 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2150 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2151 rtcache_free(&pktopt->ip6po_route);
2152 }
2153 if (optname == -1 || optname == IPV6_DSTOPTS) {
2154 if (pktopt->ip6po_dest2)
2155 free(pktopt->ip6po_dest2, M_IP6OPT);
2156 pktopt->ip6po_dest2 = NULL;
2157 }
2158 }
2159
2160 #define PKTOPT_EXTHDRCPY(type) \
2161 do { \
2162 if (src->type) { \
2163 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2164 dst->type = malloc(hlen, M_IP6OPT, canwait); \
2165 if (dst->type == NULL) \
2166 goto bad; \
2167 memcpy(dst->type, src->type, hlen); \
2168 } \
2169 } while (/*CONSTCOND*/ 0)
2170
2171 static int
2172 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2173 {
2174 dst->ip6po_hlim = src->ip6po_hlim;
2175 dst->ip6po_tclass = src->ip6po_tclass;
2176 dst->ip6po_flags = src->ip6po_flags;
2177 if (src->ip6po_pktinfo) {
2178 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2179 M_IP6OPT, canwait);
2180 if (dst->ip6po_pktinfo == NULL)
2181 goto bad;
2182 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2183 }
2184 if (src->ip6po_nexthop) {
2185 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2186 M_IP6OPT, canwait);
2187 if (dst->ip6po_nexthop == NULL)
2188 goto bad;
2189 memcpy(dst->ip6po_nexthop, src->ip6po_nexthop,
2190 src->ip6po_nexthop->sa_len);
2191 }
2192 PKTOPT_EXTHDRCPY(ip6po_hbh);
2193 PKTOPT_EXTHDRCPY(ip6po_dest1);
2194 PKTOPT_EXTHDRCPY(ip6po_dest2);
2195 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2196 return (0);
2197
2198 bad:
2199 if (dst->ip6po_pktinfo) free(dst->ip6po_pktinfo, M_IP6OPT);
2200 if (dst->ip6po_nexthop) free(dst->ip6po_nexthop, M_IP6OPT);
2201 if (dst->ip6po_hbh) free(dst->ip6po_hbh, M_IP6OPT);
2202 if (dst->ip6po_dest1) free(dst->ip6po_dest1, M_IP6OPT);
2203 if (dst->ip6po_dest2) free(dst->ip6po_dest2, M_IP6OPT);
2204 if (dst->ip6po_rthdr) free(dst->ip6po_rthdr, M_IP6OPT);
2205
2206 return (ENOBUFS);
2207 }
2208 #undef PKTOPT_EXTHDRCPY
2209
2210 struct ip6_pktopts *
2211 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2212 {
2213 int error;
2214 struct ip6_pktopts *dst;
2215
2216 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2217 if (dst == NULL)
2218 return (NULL);
2219 ip6_initpktopts(dst);
2220
2221 if ((error = copypktopts(dst, src, canwait)) != 0) {
2222 free(dst, M_IP6OPT);
2223 return (NULL);
2224 }
2225
2226 return (dst);
2227 }
2228
2229 void
2230 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2231 {
2232 if (pktopt == NULL)
2233 return;
2234
2235 ip6_clearpktopts(pktopt, -1);
2236
2237 free(pktopt, M_IP6OPT);
2238 }
2239
2240 /*
2241 * Set the IP6 multicast options in response to user setsockopt().
2242 */
2243 static int
2244 ip6_setmoptions(const struct sockopt *sopt, struct ip6_moptions **im6op)
2245 {
2246 int error = 0;
2247 u_int loop, ifindex;
2248 struct ipv6_mreq mreq;
2249 struct ifnet *ifp;
2250 struct ip6_moptions *im6o = *im6op;
2251 struct route ro;
2252 struct in6_multi_mship *imm;
2253 struct lwp *l = curlwp; /* XXX */
2254
2255 if (im6o == NULL) {
2256 /*
2257 * No multicast option buffer attached to the pcb;
2258 * allocate one and initialize to default values.
2259 */
2260 im6o = malloc(sizeof(*im6o), M_IPMOPTS, M_NOWAIT);
2261 if (im6o == NULL)
2262 return (ENOBUFS);
2263
2264 *im6op = im6o;
2265 im6o->im6o_multicast_ifp = NULL;
2266 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2267 im6o->im6o_multicast_loop = IPV6_DEFAULT_MULTICAST_LOOP;
2268 LIST_INIT(&im6o->im6o_memberships);
2269 }
2270
2271 switch (sopt->sopt_name) {
2272
2273 case IPV6_MULTICAST_IF:
2274 /*
2275 * Select the interface for outgoing multicast packets.
2276 */
2277 error = sockopt_get(sopt, &ifindex, sizeof(ifindex));
2278 if (error != 0)
2279 break;
2280
2281 if (ifindex != 0) {
2282 if (if_indexlim <= ifindex || !ifindex2ifnet[ifindex]) {
2283 error = ENXIO; /* XXX EINVAL? */
2284 break;
2285 }
2286 ifp = ifindex2ifnet[ifindex];
2287 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
2288 error = EADDRNOTAVAIL;
2289 break;
2290 }
2291 } else
2292 ifp = NULL;
2293 im6o->im6o_multicast_ifp = ifp;
2294 break;
2295
2296 case IPV6_MULTICAST_HOPS:
2297 {
2298 /*
2299 * Set the IP6 hoplimit for outgoing multicast packets.
2300 */
2301 int optval;
2302
2303 error = sockopt_getint(sopt, &optval);
2304 if (error != 0)
2305 break;
2306
2307 if (optval < -1 || optval >= 256)
2308 error = EINVAL;
2309 else if (optval == -1)
2310 im6o->im6o_multicast_hlim = ip6_defmcasthlim;
2311 else
2312 im6o->im6o_multicast_hlim = optval;
2313 break;
2314 }
2315
2316 case IPV6_MULTICAST_LOOP:
2317 /*
2318 * Set the loopback flag for outgoing multicast packets.
2319 * Must be zero or one.
2320 */
2321 error = sockopt_get(sopt, &loop, sizeof(loop));
2322 if (error != 0)
2323 break;
2324 if (loop > 1) {
2325 error = EINVAL;
2326 break;
2327 }
2328 im6o->im6o_multicast_loop = loop;
2329 break;
2330
2331 case IPV6_JOIN_GROUP:
2332 /*
2333 * Add a multicast group membership.
2334 * Group must be a valid IP6 multicast address.
2335 */
2336 error = sockopt_get(sopt, &mreq, sizeof(mreq));
2337 if (error != 0)
2338 break;
2339
2340 if (IN6_IS_ADDR_UNSPECIFIED(&mreq.ipv6mr_multiaddr)) {
2341 /*
2342 * We use the unspecified address to specify to accept
2343 * all multicast addresses. Only super user is allowed
2344 * to do this.
2345 */
2346 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_IPV6,
2347 KAUTH_REQ_NETWORK_IPV6_JOIN_MULTICAST, NULL, NULL, NULL))
2348 {
2349 error = EACCES;
2350 break;
2351 }
2352 } else if (!IN6_IS_ADDR_MULTICAST(&mreq.ipv6mr_multiaddr)) {
2353 error = EINVAL;
2354 break;
2355 }
2356
2357 /*
2358 * If no interface was explicitly specified, choose an
2359 * appropriate one according to the given multicast address.
2360 */
2361 if (mreq.ipv6mr_interface == 0) {
2362 struct rtentry *rt;
2363 union {
2364 struct sockaddr dst;
2365 struct sockaddr_in6 dst6;
2366 } u;
2367
2368 /*
2369 * Look up the routing table for the
2370 * address, and choose the outgoing interface.
2371 * XXX: is it a good approach?
2372 */
2373 memset(&ro, 0, sizeof(ro));
2374 sockaddr_in6_init(&u.dst6, &mreq.ipv6mr_multiaddr, 0,
2375 0, 0);
2376 rtcache_setdst(&ro, &u.dst);
2377 ifp = (rt = rtcache_init(&ro)) != NULL ? rt->rt_ifp
2378 : NULL;
2379 rtcache_free(&ro);
2380 } else {
2381 /*
2382 * If the interface is specified, validate it.
2383 */
2384 if (if_indexlim <= mreq.ipv6mr_interface ||
2385 !ifindex2ifnet[mreq.ipv6mr_interface]) {
2386 error = ENXIO; /* XXX EINVAL? */
2387 break;
2388 }
2389 ifp = ifindex2ifnet[mreq.ipv6mr_interface];
2390 }
2391
2392 /*
2393 * See if we found an interface, and confirm that it
2394 * supports multicast
2395 */
2396 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
2397 error = EADDRNOTAVAIL;
2398 break;
2399 }
2400
2401 if (in6_setscope(&mreq.ipv6mr_multiaddr, ifp, NULL)) {
2402 error = EADDRNOTAVAIL; /* XXX: should not happen */
2403 break;
2404 }
2405
2406 /*
2407 * See if the membership already exists.
2408 */
2409 for (imm = im6o->im6o_memberships.lh_first;
2410 imm != NULL; imm = imm->i6mm_chain.le_next)
2411 if (imm->i6mm_maddr->in6m_ifp == ifp &&
2412 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2413 &mreq.ipv6mr_multiaddr))
2414 break;
2415 if (imm != NULL) {
2416 error = EADDRINUSE;
2417 break;
2418 }
2419 /*
2420 * Everything looks good; add a new record to the multicast
2421 * address list for the given interface.
2422 */
2423 imm = in6_joingroup(ifp, &mreq.ipv6mr_multiaddr, &error, 0);
2424 if (imm == NULL)
2425 break;
2426 LIST_INSERT_HEAD(&im6o->im6o_memberships, imm, i6mm_chain);
2427 break;
2428
2429 case IPV6_LEAVE_GROUP:
2430 /*
2431 * Drop a multicast group membership.
2432 * Group must be a valid IP6 multicast address.
2433 */
2434 error = sockopt_get(sopt, &mreq, sizeof(mreq));
2435 if (error != 0)
2436 break;
2437
2438 /*
2439 * If an interface address was specified, get a pointer
2440 * to its ifnet structure.
2441 */
2442 if (mreq.ipv6mr_interface != 0) {
2443 if (if_indexlim <= mreq.ipv6mr_interface ||
2444 !ifindex2ifnet[mreq.ipv6mr_interface]) {
2445 error = ENXIO; /* XXX EINVAL? */
2446 break;
2447 }
2448 ifp = ifindex2ifnet[mreq.ipv6mr_interface];
2449 } else
2450 ifp = NULL;
2451
2452 /* Fill in the scope zone ID */
2453 if (ifp) {
2454 if (in6_setscope(&mreq.ipv6mr_multiaddr, ifp, NULL)) {
2455 /* XXX: should not happen */
2456 error = EADDRNOTAVAIL;
2457 break;
2458 }
2459 } else if (mreq.ipv6mr_interface != 0) {
2460 /*
2461 * XXX: This case would happens when the (positive)
2462 * index is in the valid range, but the corresponding
2463 * interface has been detached dynamically. The above
2464 * check probably avoids such case to happen here, but
2465 * we check it explicitly for safety.
2466 */
2467 error = EADDRNOTAVAIL;
2468 break;
2469 } else { /* ipv6mr_interface == 0 */
2470 struct sockaddr_in6 sa6_mc;
2471
2472 /*
2473 * The API spec says as follows:
2474 * If the interface index is specified as 0, the
2475 * system may choose a multicast group membership to
2476 * drop by matching the multicast address only.
2477 * On the other hand, we cannot disambiguate the scope
2478 * zone unless an interface is provided. Thus, we
2479 * check if there's ambiguity with the default scope
2480 * zone as the last resort.
2481 */
2482 sockaddr_in6_init(&sa6_mc, &mreq.ipv6mr_multiaddr,
2483 0, 0, 0);
2484 error = sa6_embedscope(&sa6_mc, ip6_use_defzone);
2485 if (error != 0)
2486 break;
2487 mreq.ipv6mr_multiaddr = sa6_mc.sin6_addr;
2488 }
2489
2490 /*
2491 * Find the membership in the membership list.
2492 */
2493 for (imm = im6o->im6o_memberships.lh_first;
2494 imm != NULL; imm = imm->i6mm_chain.le_next) {
2495 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
2496 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
2497 &mreq.ipv6mr_multiaddr))
2498 break;
2499 }
2500 if (imm == NULL) {
2501 /* Unable to resolve interface */
2502 error = EADDRNOTAVAIL;
2503 break;
2504 }
2505 /*
2506 * Give up the multicast address record to which the
2507 * membership points.
2508 */
2509 LIST_REMOVE(imm, i6mm_chain);
2510 in6_leavegroup(imm);
2511 break;
2512
2513 default:
2514 error = EOPNOTSUPP;
2515 break;
2516 }
2517
2518 /*
2519 * If all options have default values, no need to keep the mbuf.
2520 */
2521 if (im6o->im6o_multicast_ifp == NULL &&
2522 im6o->im6o_multicast_hlim == ip6_defmcasthlim &&
2523 im6o->im6o_multicast_loop == IPV6_DEFAULT_MULTICAST_LOOP &&
2524 im6o->im6o_memberships.lh_first == NULL) {
2525 free(*im6op, M_IPMOPTS);
2526 *im6op = NULL;
2527 }
2528
2529 return (error);
2530 }
2531
2532 /*
2533 * Return the IP6 multicast options in response to user getsockopt().
2534 */
2535 static int
2536 ip6_getmoptions(struct sockopt *sopt, struct ip6_moptions *im6o)
2537 {
2538 u_int optval;
2539 int error;
2540
2541 switch (sopt->sopt_name) {
2542 case IPV6_MULTICAST_IF:
2543 if (im6o == NULL || im6o->im6o_multicast_ifp == NULL)
2544 optval = 0;
2545 else
2546 optval = im6o->im6o_multicast_ifp->if_index;
2547
2548 error = sockopt_set(sopt, &optval, sizeof(optval));
2549 break;
2550
2551 case IPV6_MULTICAST_HOPS:
2552 if (im6o == NULL)
2553 optval = ip6_defmcasthlim;
2554 else
2555 optval = im6o->im6o_multicast_hlim;
2556
2557 error = sockopt_set(sopt, &optval, sizeof(optval));
2558 break;
2559
2560 case IPV6_MULTICAST_LOOP:
2561 if (im6o == NULL)
2562 optval = IPV6_DEFAULT_MULTICAST_LOOP;
2563 else
2564 optval = im6o->im6o_multicast_loop;
2565
2566 error = sockopt_set(sopt, &optval, sizeof(optval));
2567 break;
2568
2569 default:
2570 error = EOPNOTSUPP;
2571 }
2572
2573 return (error);
2574 }
2575
2576 /*
2577 * Discard the IP6 multicast options.
2578 */
2579 void
2580 ip6_freemoptions(struct ip6_moptions *im6o)
2581 {
2582 struct in6_multi_mship *imm;
2583
2584 if (im6o == NULL)
2585 return;
2586
2587 while ((imm = im6o->im6o_memberships.lh_first) != NULL) {
2588 LIST_REMOVE(imm, i6mm_chain);
2589 in6_leavegroup(imm);
2590 }
2591 free(im6o, M_IPMOPTS);
2592 }
2593
2594 /*
2595 * Set IPv6 outgoing packet options based on advanced API.
2596 */
2597 int
2598 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2599 struct ip6_pktopts *stickyopt, kauth_cred_t cred, int uproto)
2600 {
2601 struct cmsghdr *cm = 0;
2602
2603 if (control == NULL || opt == NULL)
2604 return (EINVAL);
2605
2606 ip6_initpktopts(opt);
2607 if (stickyopt) {
2608 int error;
2609
2610 /*
2611 * If stickyopt is provided, make a local copy of the options
2612 * for this particular packet, then override them by ancillary
2613 * objects.
2614 * XXX: copypktopts() does not copy the cached route to a next
2615 * hop (if any). This is not very good in terms of efficiency,
2616 * but we can allow this since this option should be rarely
2617 * used.
2618 */
2619 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2620 return (error);
2621 }
2622
2623 /*
2624 * XXX: Currently, we assume all the optional information is stored
2625 * in a single mbuf.
2626 */
2627 if (control->m_next)
2628 return (EINVAL);
2629
2630 /* XXX if cm->cmsg_len is not aligned, control->m_len can become <0 */
2631 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2632 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2633 int error;
2634
2635 if (control->m_len < CMSG_LEN(0))
2636 return (EINVAL);
2637
2638 cm = mtod(control, struct cmsghdr *);
2639 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2640 return (EINVAL);
2641 if (cm->cmsg_level != IPPROTO_IPV6)
2642 continue;
2643
2644 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2645 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2646 if (error)
2647 return (error);
2648 }
2649
2650 return (0);
2651 }
2652
2653 /*
2654 * Set a particular packet option, as a sticky option or an ancillary data
2655 * item. "len" can be 0 only when it's a sticky option.
2656 * We have 4 cases of combination of "sticky" and "cmsg":
2657 * "sticky=0, cmsg=0": impossible
2658 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2659 * "sticky=1, cmsg=0": RFC3542 socket option
2660 * "sticky=1, cmsg=1": RFC2292 socket option
2661 */
2662 static int
2663 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2664 kauth_cred_t cred, int sticky, int cmsg, int uproto)
2665 {
2666 int minmtupolicy;
2667 int error;
2668
2669 if (!sticky && !cmsg) {
2670 #ifdef DIAGNOSTIC
2671 printf("ip6_setpktopt: impossible case\n");
2672 #endif
2673 return (EINVAL);
2674 }
2675
2676 /*
2677 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2678 * not be specified in the context of RFC3542. Conversely,
2679 * RFC3542 types should not be specified in the context of RFC2292.
2680 */
2681 if (!cmsg) {
2682 switch (optname) {
2683 case IPV6_2292PKTINFO:
2684 case IPV6_2292HOPLIMIT:
2685 case IPV6_2292NEXTHOP:
2686 case IPV6_2292HOPOPTS:
2687 case IPV6_2292DSTOPTS:
2688 case IPV6_2292RTHDR:
2689 case IPV6_2292PKTOPTIONS:
2690 return (ENOPROTOOPT);
2691 }
2692 }
2693 if (sticky && cmsg) {
2694 switch (optname) {
2695 case IPV6_PKTINFO:
2696 case IPV6_HOPLIMIT:
2697 case IPV6_NEXTHOP:
2698 case IPV6_HOPOPTS:
2699 case IPV6_DSTOPTS:
2700 case IPV6_RTHDRDSTOPTS:
2701 case IPV6_RTHDR:
2702 case IPV6_USE_MIN_MTU:
2703 case IPV6_DONTFRAG:
2704 case IPV6_OTCLASS:
2705 case IPV6_TCLASS:
2706 return (ENOPROTOOPT);
2707 }
2708 }
2709
2710 switch (optname) {
2711 #ifdef RFC2292
2712 case IPV6_2292PKTINFO:
2713 #endif
2714 case IPV6_PKTINFO:
2715 {
2716 struct ifnet *ifp = NULL;
2717 struct in6_pktinfo *pktinfo;
2718
2719 if (len != sizeof(struct in6_pktinfo))
2720 return (EINVAL);
2721
2722 pktinfo = (struct in6_pktinfo *)buf;
2723
2724 /*
2725 * An application can clear any sticky IPV6_PKTINFO option by
2726 * doing a "regular" setsockopt with ipi6_addr being
2727 * in6addr_any and ipi6_ifindex being zero.
2728 * [RFC 3542, Section 6]
2729 */
2730 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2731 pktinfo->ipi6_ifindex == 0 &&
2732 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2733 ip6_clearpktopts(opt, optname);
2734 break;
2735 }
2736
2737 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2738 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2739 return (EINVAL);
2740 }
2741
2742 /* validate the interface index if specified. */
2743 if (pktinfo->ipi6_ifindex >= if_indexlim) {
2744 return (ENXIO);
2745 }
2746 if (pktinfo->ipi6_ifindex) {
2747 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
2748 if (ifp == NULL)
2749 return (ENXIO);
2750 }
2751
2752 /*
2753 * We store the address anyway, and let in6_selectsrc()
2754 * validate the specified address. This is because ipi6_addr
2755 * may not have enough information about its scope zone, and
2756 * we may need additional information (such as outgoing
2757 * interface or the scope zone of a destination address) to
2758 * disambiguate the scope.
2759 * XXX: the delay of the validation may confuse the
2760 * application when it is used as a sticky option.
2761 */
2762 if (opt->ip6po_pktinfo == NULL) {
2763 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2764 M_IP6OPT, M_NOWAIT);
2765 if (opt->ip6po_pktinfo == NULL)
2766 return (ENOBUFS);
2767 }
2768 memcpy(opt->ip6po_pktinfo, pktinfo, sizeof(*pktinfo));
2769 break;
2770 }
2771
2772 #ifdef RFC2292
2773 case IPV6_2292HOPLIMIT:
2774 #endif
2775 case IPV6_HOPLIMIT:
2776 {
2777 int *hlimp;
2778
2779 /*
2780 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
2781 * to simplify the ordering among hoplimit options.
2782 */
2783 if (optname == IPV6_HOPLIMIT && sticky)
2784 return (ENOPROTOOPT);
2785
2786 if (len != sizeof(int))
2787 return (EINVAL);
2788 hlimp = (int *)buf;
2789 if (*hlimp < -1 || *hlimp > 255)
2790 return (EINVAL);
2791
2792 opt->ip6po_hlim = *hlimp;
2793 break;
2794 }
2795
2796 case IPV6_OTCLASS:
2797 if (len != sizeof(u_int8_t))
2798 return (EINVAL);
2799
2800 opt->ip6po_tclass = *(u_int8_t *)buf;
2801 break;
2802
2803 case IPV6_TCLASS:
2804 {
2805 int tclass;
2806
2807 if (len != sizeof(int))
2808 return (EINVAL);
2809 tclass = *(int *)buf;
2810 if (tclass < -1 || tclass > 255)
2811 return (EINVAL);
2812
2813 opt->ip6po_tclass = tclass;
2814 break;
2815 }
2816
2817 #ifdef RFC2292
2818 case IPV6_2292NEXTHOP:
2819 #endif
2820 case IPV6_NEXTHOP:
2821 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6,
2822 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL);
2823 if (error)
2824 return (error);
2825
2826 if (len == 0) { /* just remove the option */
2827 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2828 break;
2829 }
2830
2831 /* check if cmsg_len is large enough for sa_len */
2832 if (len < sizeof(struct sockaddr) || len < *buf)
2833 return (EINVAL);
2834
2835 switch (((struct sockaddr *)buf)->sa_family) {
2836 case AF_INET6:
2837 {
2838 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
2839
2840 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
2841 return (EINVAL);
2842
2843 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
2844 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
2845 return (EINVAL);
2846 }
2847 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
2848 != 0) {
2849 return (error);
2850 }
2851 break;
2852 }
2853 case AF_LINK: /* eventually be supported? */
2854 default:
2855 return (EAFNOSUPPORT);
2856 }
2857
2858 /* turn off the previous option, then set the new option. */
2859 ip6_clearpktopts(opt, IPV6_NEXTHOP);
2860 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
2861 if (opt->ip6po_nexthop == NULL)
2862 return (ENOBUFS);
2863 memcpy(opt->ip6po_nexthop, buf, *buf);
2864 break;
2865
2866 #ifdef RFC2292
2867 case IPV6_2292HOPOPTS:
2868 #endif
2869 case IPV6_HOPOPTS:
2870 {
2871 struct ip6_hbh *hbh;
2872 int hbhlen;
2873
2874 /*
2875 * XXX: We don't allow a non-privileged user to set ANY HbH
2876 * options, since per-option restriction has too much
2877 * overhead.
2878 */
2879 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6,
2880 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL);
2881 if (error)
2882 return (error);
2883
2884 if (len == 0) {
2885 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2886 break; /* just remove the option */
2887 }
2888
2889 /* message length validation */
2890 if (len < sizeof(struct ip6_hbh))
2891 return (EINVAL);
2892 hbh = (struct ip6_hbh *)buf;
2893 hbhlen = (hbh->ip6h_len + 1) << 3;
2894 if (len != hbhlen)
2895 return (EINVAL);
2896
2897 /* turn off the previous option, then set the new option. */
2898 ip6_clearpktopts(opt, IPV6_HOPOPTS);
2899 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
2900 if (opt->ip6po_hbh == NULL)
2901 return (ENOBUFS);
2902 memcpy(opt->ip6po_hbh, hbh, hbhlen);
2903
2904 break;
2905 }
2906
2907 #ifdef RFC2292
2908 case IPV6_2292DSTOPTS:
2909 #endif
2910 case IPV6_DSTOPTS:
2911 case IPV6_RTHDRDSTOPTS:
2912 {
2913 struct ip6_dest *dest, **newdest = NULL;
2914 int destlen;
2915
2916 /* XXX: see the comment for IPV6_HOPOPTS */
2917 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPV6,
2918 KAUTH_REQ_NETWORK_IPV6_HOPBYHOP, NULL, NULL, NULL);
2919 if (error)
2920 return (error);
2921
2922 if (len == 0) {
2923 ip6_clearpktopts(opt, optname);
2924 break; /* just remove the option */
2925 }
2926
2927 /* message length validation */
2928 if (len < sizeof(struct ip6_dest))
2929 return (EINVAL);
2930 dest = (struct ip6_dest *)buf;
2931 destlen = (dest->ip6d_len + 1) << 3;
2932 if (len != destlen)
2933 return (EINVAL);
2934 /*
2935 * Determine the position that the destination options header
2936 * should be inserted; before or after the routing header.
2937 */
2938 switch (optname) {
2939 case IPV6_2292DSTOPTS:
2940 /*
2941 * The old advanced API is ambiguous on this point.
2942 * Our approach is to determine the position based
2943 * according to the existence of a routing header.
2944 * Note, however, that this depends on the order of the
2945 * extension headers in the ancillary data; the 1st
2946 * part of the destination options header must appear
2947 * before the routing header in the ancillary data,
2948 * too.
2949 * RFC3542 solved the ambiguity by introducing
2950 * separate ancillary data or option types.
2951 */
2952 if (opt->ip6po_rthdr == NULL)
2953 newdest = &opt->ip6po_dest1;
2954 else
2955 newdest = &opt->ip6po_dest2;
2956 break;
2957 case IPV6_RTHDRDSTOPTS:
2958 newdest = &opt->ip6po_dest1;
2959 break;
2960 case IPV6_DSTOPTS:
2961 newdest = &opt->ip6po_dest2;
2962 break;
2963 }
2964
2965 /* turn off the previous option, then set the new option. */
2966 ip6_clearpktopts(opt, optname);
2967 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
2968 if (*newdest == NULL)
2969 return (ENOBUFS);
2970 memcpy(*newdest, dest, destlen);
2971
2972 break;
2973 }
2974
2975 #ifdef RFC2292
2976 case IPV6_2292RTHDR:
2977 #endif
2978 case IPV6_RTHDR:
2979 {
2980 struct ip6_rthdr *rth;
2981 int rthlen;
2982
2983 if (len == 0) {
2984 ip6_clearpktopts(opt, IPV6_RTHDR);
2985 break; /* just remove the option */
2986 }
2987
2988 /* message length validation */
2989 if (len < sizeof(struct ip6_rthdr))
2990 return (EINVAL);
2991 rth = (struct ip6_rthdr *)buf;
2992 rthlen = (rth->ip6r_len + 1) << 3;
2993 if (len != rthlen)
2994 return (EINVAL);
2995 switch (rth->ip6r_type) {
2996 case IPV6_RTHDR_TYPE_0:
2997 if (rth->ip6r_len == 0) /* must contain one addr */
2998 return (EINVAL);
2999 if (rth->ip6r_len % 2) /* length must be even */
3000 return (EINVAL);
3001 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3002 return (EINVAL);
3003 break;
3004 default:
3005 return (EINVAL); /* not supported */
3006 }
3007 /* turn off the previous option */
3008 ip6_clearpktopts(opt, IPV6_RTHDR);
3009 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3010 if (opt->ip6po_rthdr == NULL)
3011 return (ENOBUFS);
3012 memcpy(opt->ip6po_rthdr, rth, rthlen);
3013 break;
3014 }
3015
3016 case IPV6_USE_MIN_MTU:
3017 if (len != sizeof(int))
3018 return (EINVAL);
3019 minmtupolicy = *(int *)buf;
3020 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3021 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3022 minmtupolicy != IP6PO_MINMTU_ALL) {
3023 return (EINVAL);
3024 }
3025 opt->ip6po_minmtu = minmtupolicy;
3026 break;
3027
3028 case IPV6_DONTFRAG:
3029 if (len != sizeof(int))
3030 return (EINVAL);
3031
3032 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3033 /*
3034 * we ignore this option for TCP sockets.
3035 * (RFC3542 leaves this case unspecified.)
3036 */
3037 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3038 } else
3039 opt->ip6po_flags |= IP6PO_DONTFRAG;
3040 break;
3041
3042 default:
3043 return (ENOPROTOOPT);
3044 } /* end of switch */
3045
3046 return (0);
3047 }
3048
3049 /*
3050 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3051 * packet to the input queue of a specified interface. Note that this
3052 * calls the output routine of the loopback "driver", but with an interface
3053 * pointer that might NOT be lo0ifp -- easier than replicating that code here.
3054 */
3055 void
3056 ip6_mloopback(struct ifnet *ifp, struct mbuf *m,
3057 const struct sockaddr_in6 *dst)
3058 {
3059 struct mbuf *copym;
3060 struct ip6_hdr *ip6;
3061
3062 copym = m_copy(m, 0, M_COPYALL);
3063 if (copym == NULL)
3064 return;
3065
3066 /*
3067 * Make sure to deep-copy IPv6 header portion in case the data
3068 * is in an mbuf cluster, so that we can safely override the IPv6
3069 * header portion later.
3070 */
3071 if ((copym->m_flags & M_EXT) != 0 ||
3072 copym->m_len < sizeof(struct ip6_hdr)) {
3073 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3074 if (copym == NULL)
3075 return;
3076 }
3077
3078 #ifdef DIAGNOSTIC
3079 if (copym->m_len < sizeof(*ip6)) {
3080 m_freem(copym);
3081 return;
3082 }
3083 #endif
3084
3085 ip6 = mtod(copym, struct ip6_hdr *);
3086 /*
3087 * clear embedded scope identifiers if necessary.
3088 * in6_clearscope will touch the addresses only when necessary.
3089 */
3090 in6_clearscope(&ip6->ip6_src);
3091 in6_clearscope(&ip6->ip6_dst);
3092
3093 (void)looutput(ifp, copym, (const struct sockaddr *)dst, NULL);
3094 }
3095
3096 /*
3097 * Chop IPv6 header off from the payload.
3098 */
3099 static int
3100 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3101 {
3102 struct mbuf *mh;
3103 struct ip6_hdr *ip6;
3104
3105 ip6 = mtod(m, struct ip6_hdr *);
3106 if (m->m_len > sizeof(*ip6)) {
3107 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
3108 if (mh == 0) {
3109 m_freem(m);
3110 return ENOBUFS;
3111 }
3112 M_MOVE_PKTHDR(mh, m);
3113 MH_ALIGN(mh, sizeof(*ip6));
3114 m->m_len -= sizeof(*ip6);
3115 m->m_data += sizeof(*ip6);
3116 mh->m_next = m;
3117 m = mh;
3118 m->m_len = sizeof(*ip6);
3119 bcopy((void *)ip6, mtod(m, void *), sizeof(*ip6));
3120 }
3121 exthdrs->ip6e_ip6 = m;
3122 return 0;
3123 }
3124
3125 /*
3126 * Compute IPv6 extension header length.
3127 */
3128 int
3129 ip6_optlen(struct in6pcb *in6p)
3130 {
3131 int len;
3132
3133 if (!in6p->in6p_outputopts)
3134 return 0;
3135
3136 len = 0;
3137 #define elen(x) \
3138 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3139
3140 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3141 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3142 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3143 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3144 return len;
3145 #undef elen
3146 }
3147