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