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