1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $KAME: ip6_output.c,v 1.279 2002/01/26 06:12:30 jinmei Exp $
32 */
33
34 /*-
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
63 */
64
65 #include <sys/cdefs.h>
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69 #include "opt_kern_tls.h"
70 #include "opt_ratelimit.h"
71 #include "opt_route.h"
72 #include "opt_rss.h"
73 #include "opt_sctp.h"
74
75 #include <sys/param.h>
76 #include <sys/kernel.h>
77 #include <sys/ktls.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/errno.h>
81 #include <sys/priv.h>
82 #include <sys/proc.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/syslog.h>
87 #include <sys/ucred.h>
88
89 #include <machine/in_cksum.h>
90
91 #include <net/if.h>
92 #include <net/if_var.h>
93 #include <net/if_private.h>
94 #include <net/if_vlan_var.h>
95 #include <net/if_llatbl.h>
96 #include <net/ethernet.h>
97 #include <net/netisr.h>
98 #include <net/route.h>
99 #include <net/route/nhop.h>
100 #include <net/pfil.h>
101 #include <net/rss_config.h>
102 #include <net/vnet.h>
103
104 #include <netinet/in.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
107 #include <netinet6/in6_fib.h>
108 #include <netinet6/in6_var.h>
109 #include <netinet/ip6.h>
110 #include <netinet/icmp6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet/tcp_var.h>
114 #include <netinet6/nd6.h>
115 #include <netinet6/in6_rss.h>
116
117 #include <netipsec/ipsec_support.h>
118 #if defined(SCTP) || defined(SCTP_SUPPORT)
119 #include <netinet/sctp.h>
120 #include <netinet/sctp_crc32.h>
121 #endif
122
123 #include <netinet6/scope6_var.h>
124
125 extern int in6_mcast_loop;
126
127 struct ip6_exthdrs {
128 struct mbuf *ip6e_ip6;
129 struct mbuf *ip6e_hbh;
130 struct mbuf *ip6e_dest1;
131 struct mbuf *ip6e_rthdr;
132 struct mbuf *ip6e_dest2;
133 };
134
135 static MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
136
137 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **,
138 struct ucred *, int);
139 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *,
140 struct socket *, struct sockopt *);
141 static int ip6_getpcbopt(struct inpcb *, int, struct sockopt *);
142 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *,
143 struct ucred *, int, int, int);
144
145 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
146 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
147 struct ip6_frag **);
148 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
149 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
150 static int ip6_getpmtu(struct route_in6 *, int,
151 struct ifnet *, const struct in6_addr *, u_long *, int *, u_int,
152 u_int);
153 static int ip6_calcmtu(struct ifnet *, const struct in6_addr *, u_long,
154 u_long *, int *, u_int);
155 static int ip6_getpmtu_ctl(u_int, const struct in6_addr *, u_long *);
156 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
157
158 /*
159 * Make an extension header from option data. hp is the source,
160 * mp is the destination, and _ol is the optlen.
161 */
162 #define MAKE_EXTHDR(hp, mp, _ol) \
163 do { \
164 if (hp) { \
165 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
166 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
167 ((eh)->ip6e_len + 1) << 3); \
168 if (error) \
169 goto freehdrs; \
170 (_ol) += (*(mp))->m_len; \
171 } \
172 } while (/*CONSTCOND*/ 0)
173
174 /*
175 * Form a chain of extension headers.
176 * m is the extension header mbuf
177 * mp is the previous mbuf in the chain
178 * p is the next header
179 * i is the type of option.
180 */
181 #define MAKE_CHAIN(m, mp, p, i)\
182 do {\
183 if (m) {\
184 if (!hdrsplit) \
185 panic("%s:%d: assumption failed: "\
186 "hdr not split: hdrsplit %d exthdrs %p",\
187 __func__, __LINE__, hdrsplit, &exthdrs);\
188 *mtod((m), u_char *) = *(p);\
189 *(p) = (i);\
190 p = mtod((m), u_char *);\
191 (m)->m_next = (mp)->m_next;\
192 (mp)->m_next = (m);\
193 (mp) = (m);\
194 }\
195 } while (/*CONSTCOND*/ 0)
196
197 void
in6_delayed_cksum(struct mbuf * m,uint32_t plen,u_short offset)198 in6_delayed_cksum(struct mbuf *m, uint32_t plen, u_short offset)
199 {
200 u_short csum;
201
202 csum = in_cksum_skip(m, offset + plen, offset);
203 if (m->m_pkthdr.csum_flags & CSUM_UDP_IPV6 && csum == 0)
204 csum = 0xffff;
205 offset += m->m_pkthdr.csum_data; /* checksum offset */
206
207 if (offset + sizeof(csum) > m->m_len)
208 m_copyback(m, offset, sizeof(csum), (caddr_t)&csum);
209 else
210 *(u_short *)mtodo(m, offset) = csum;
211 }
212
213 static void
ip6_output_delayed_csum(struct mbuf * m,struct ifnet * ifp,int csum_flags,int plen,int optlen)214 ip6_output_delayed_csum(struct mbuf *m, struct ifnet *ifp, int csum_flags,
215 int plen, int optlen)
216 {
217
218 KASSERT((plen >= optlen), ("%s:%d: plen %d < optlen %d, m %p, ifp %p "
219 "csum_flags %#x",
220 __func__, __LINE__, plen, optlen, m, ifp, csum_flags));
221
222 if (csum_flags & CSUM_DELAY_DATA_IPV6) {
223 in6_delayed_cksum(m, plen - optlen,
224 sizeof(struct ip6_hdr) + optlen);
225 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA_IPV6;
226 }
227 #if defined(SCTP) || defined(SCTP_SUPPORT)
228 if (csum_flags & CSUM_SCTP_IPV6) {
229 sctp_delayed_cksum(m, sizeof(struct ip6_hdr) + optlen);
230 m->m_pkthdr.csum_flags &= ~CSUM_SCTP_IPV6;
231 }
232 #endif
233 }
234
235 int
ip6_fragment(struct ifnet * ifp,struct mbuf * m0,int hlen,u_char nextproto,int fraglen,uint32_t id)236 ip6_fragment(struct ifnet *ifp, struct mbuf *m0, int hlen, u_char nextproto,
237 int fraglen , uint32_t id)
238 {
239 struct mbuf *m, **mnext, *m_frgpart;
240 struct ip6_hdr *ip6, *mhip6;
241 struct ip6_frag *ip6f;
242 int off;
243 int error;
244 int tlen = m0->m_pkthdr.len;
245
246 KASSERT((fraglen % 8 == 0), ("Fragment length must be a multiple of 8"));
247
248 m = m0;
249 ip6 = mtod(m, struct ip6_hdr *);
250 mnext = &m->m_nextpkt;
251
252 for (off = hlen; off < tlen; off += fraglen) {
253 m = m_gethdr(M_NOWAIT, MT_DATA);
254 if (!m) {
255 IP6STAT_INC(ip6s_odropped);
256 return (ENOBUFS);
257 }
258
259 /*
260 * Make sure the complete packet header gets copied
261 * from the originating mbuf to the newly created
262 * mbuf. This also ensures that existing firewall
263 * classification(s), VLAN tags and so on get copied
264 * to the resulting fragmented packet(s):
265 */
266 if (m_dup_pkthdr(m, m0, M_NOWAIT) == 0) {
267 m_free(m);
268 IP6STAT_INC(ip6s_odropped);
269 return (ENOBUFS);
270 }
271
272 *mnext = m;
273 mnext = &m->m_nextpkt;
274 m->m_data += max_linkhdr;
275 mhip6 = mtod(m, struct ip6_hdr *);
276 *mhip6 = *ip6;
277 m->m_len = sizeof(*mhip6);
278 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
279 if (error) {
280 IP6STAT_INC(ip6s_odropped);
281 return (error);
282 }
283 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
284 if (off + fraglen >= tlen)
285 fraglen = tlen - off;
286 else
287 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
288 mhip6->ip6_plen = htons((u_short)(fraglen + hlen +
289 sizeof(*ip6f) - sizeof(struct ip6_hdr)));
290 if ((m_frgpart = m_copym(m0, off, fraglen, M_NOWAIT)) == NULL) {
291 IP6STAT_INC(ip6s_odropped);
292 return (ENOBUFS);
293 }
294 m_cat(m, m_frgpart);
295 m->m_pkthdr.len = fraglen + hlen + sizeof(*ip6f);
296 ip6f->ip6f_reserved = 0;
297 ip6f->ip6f_ident = id;
298 ip6f->ip6f_nxt = nextproto;
299 IP6STAT_INC(ip6s_ofragments);
300 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
301 }
302
303 return (0);
304 }
305
306 static int
ip6_output_send(struct inpcb * inp,struct ifnet * ifp,struct ifnet * origifp,struct mbuf * m,struct sockaddr_in6 * dst,struct route_in6 * ro,bool stamp_tag)307 ip6_output_send(struct inpcb *inp, struct ifnet *ifp, struct ifnet *origifp,
308 struct mbuf *m, struct sockaddr_in6 *dst, struct route_in6 *ro,
309 bool stamp_tag)
310 {
311 #ifdef KERN_TLS
312 struct ktls_session *tls = NULL;
313 #endif
314 struct m_snd_tag *mst;
315 int error;
316
317 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0);
318 mst = NULL;
319
320 #ifdef KERN_TLS
321 /*
322 * If this is an unencrypted TLS record, save a reference to
323 * the record. This local reference is used to call
324 * ktls_output_eagain after the mbuf has been freed (thus
325 * dropping the mbuf's reference) in if_output.
326 */
327 if (m->m_next != NULL && mbuf_has_tls_session(m->m_next)) {
328 tls = ktls_hold(m->m_next->m_epg_tls);
329 mst = tls->snd_tag;
330
331 /*
332 * If a TLS session doesn't have a valid tag, it must
333 * have had an earlier ifp mismatch, so drop this
334 * packet.
335 */
336 if (mst == NULL) {
337 m_freem(m);
338 error = EAGAIN;
339 goto done;
340 }
341 /*
342 * Always stamp tags that include NIC ktls.
343 */
344 stamp_tag = true;
345 }
346 #endif
347 #ifdef RATELIMIT
348 if (inp != NULL && mst == NULL) {
349 if ((inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) != 0 ||
350 (inp->inp_snd_tag != NULL &&
351 inp->inp_snd_tag->ifp != ifp))
352 in_pcboutput_txrtlmt(inp, ifp, m);
353
354 if (inp->inp_snd_tag != NULL)
355 mst = inp->inp_snd_tag;
356 }
357 #endif
358 if (stamp_tag && mst != NULL) {
359 KASSERT(m->m_pkthdr.rcvif == NULL,
360 ("trying to add a send tag to a forwarded packet"));
361 if (mst->ifp != ifp) {
362 m_freem(m);
363 error = EAGAIN;
364 goto done;
365 }
366
367 /* stamp send tag on mbuf */
368 m->m_pkthdr.snd_tag = m_snd_tag_ref(mst);
369 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
370 }
371
372 error = nd6_output_ifp(ifp, origifp, m, dst, (struct route *)ro);
373
374 done:
375 /* Check for route change invalidating send tags. */
376 #ifdef KERN_TLS
377 if (tls != NULL) {
378 if (error == EAGAIN)
379 error = ktls_output_eagain(inp, tls);
380 ktls_free(tls);
381 }
382 #endif
383 #ifdef RATELIMIT
384 if (error == EAGAIN)
385 in_pcboutput_eagain(inp);
386 #endif
387 return (error);
388 }
389
390 /*
391 * IP6 output.
392 * The packet in mbuf chain m contains a skeletal IP6 header (with pri, len,
393 * nxt, hlim, src, dst).
394 * This function may modify ver and hlim only.
395 * The mbuf chain containing the packet will be freed.
396 * The mbuf opt, if present, will not be freed.
397 * If route_in6 ro is present and has ro_nh initialized, route lookup would be
398 * skipped and ro->ro_nh would be used. If ro is present but ro->ro_nh is NULL,
399 * then result of route lookup is stored in ro->ro_nh.
400 *
401 * Type of "mtu": rt_mtu is u_long, ifnet.ifr_mtu is int, and nd_ifinfo.linkmtu
402 * is uint32_t. So we use u_long to hold largest one, which is rt_mtu.
403 *
404 * ifpp - XXX: just for statistics
405 */
406 int
ip6_output(struct mbuf * m0,struct ip6_pktopts * opt,struct route_in6 * ro,int flags,struct ip6_moptions * im6o,struct ifnet ** ifpp,struct inpcb * inp)407 ip6_output(struct mbuf *m0, struct ip6_pktopts *opt,
408 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
409 struct ifnet **ifpp, struct inpcb *inp)
410 {
411 struct ip6_hdr *ip6;
412 struct ifnet *ifp, *origifp;
413 struct mbuf *m = m0;
414 struct mbuf *mprev;
415 struct route_in6 *ro_pmtu;
416 struct nhop_object *nh;
417 struct sockaddr_in6 *dst, sin6, src_sa, dst_sa;
418 struct in6_addr odst;
419 u_char *nexthdrp;
420 int tlen, len;
421 int error = 0;
422 int vlan_pcp = -1;
423 struct in6_ifaddr *ia = NULL;
424 u_long mtu;
425 int alwaysfrag, dontfrag;
426 u_int32_t optlen, plen = 0, unfragpartlen;
427 struct ip6_exthdrs exthdrs;
428 struct in6_addr src0, dst0;
429 u_int32_t zone;
430 bool hdrsplit;
431 int sw_csum, tso;
432 int needfiblookup;
433 uint32_t fibnum;
434 struct m_tag *fwd_tag = NULL;
435 uint32_t id;
436
437 NET_EPOCH_ASSERT();
438
439 if (inp != NULL) {
440 INP_LOCK_ASSERT(inp);
441 M_SETFIB(m, inp->inp_inc.inc_fibnum);
442 if ((flags & IP_NODEFAULTFLOWID) == 0) {
443 /* Unconditionally set flowid. */
444 m->m_pkthdr.flowid = inp->inp_flowid;
445 M_HASHTYPE_SET(m, inp->inp_flowtype);
446 }
447 if ((inp->inp_flags2 & INP_2PCP_SET) != 0)
448 vlan_pcp = (inp->inp_flags2 & INP_2PCP_MASK) >>
449 INP_2PCP_SHIFT;
450 #ifdef NUMA
451 m->m_pkthdr.numa_domain = inp->inp_numa_domain;
452 #endif
453 }
454
455 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
456 /*
457 * IPSec checking which handles several cases.
458 * FAST IPSEC: We re-injected the packet.
459 * XXX: need scope argument.
460 */
461 if (IPSEC_ENABLED(ipv6)) {
462 struct mbuf *m1;
463
464 error = mb_unmapped_to_ext(m, &m1);
465 if (error != 0) {
466 if (error == ENOMEM) {
467 IP6STAT_INC(ip6s_odropped);
468 error = ENOBUFS;
469 goto bad;
470 }
471 /* XXXKIB */
472 goto no_ipsec;
473 }
474 m = m1;
475 if ((error = IPSEC_OUTPUT(ipv6, m, inp)) != 0) {
476 if (error == EINPROGRESS)
477 error = 0;
478 goto done;
479 }
480 no_ipsec:;
481 }
482 #endif /* IPSEC */
483
484 /* Source address validation. */
485 ip6 = mtod(m, struct ip6_hdr *);
486 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
487 (flags & IPV6_UNSPECSRC) == 0) {
488 error = EOPNOTSUPP;
489 IP6STAT_INC(ip6s_badscope);
490 goto bad;
491 }
492 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
493 error = EOPNOTSUPP;
494 IP6STAT_INC(ip6s_badscope);
495 goto bad;
496 }
497
498 /*
499 * If we are given packet options to add extension headers prepare them.
500 * Calculate the total length of the extension header chain.
501 * Keep the length of the unfragmentable part for fragmentation.
502 */
503 bzero(&exthdrs, sizeof(exthdrs));
504 optlen = 0;
505 unfragpartlen = sizeof(struct ip6_hdr);
506 if (opt) {
507 /* Hop-by-Hop options header. */
508 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh, optlen);
509
510 /* Destination options header (1st part). */
511 if (opt->ip6po_rthdr) {
512 #ifndef RTHDR_SUPPORT_IMPLEMENTED
513 /*
514 * If there is a routing header, discard the packet
515 * right away here. RH0/1 are obsolete and we do not
516 * currently support RH2/3/4.
517 * People trying to use RH253/254 may want to disable
518 * this check.
519 * The moment we do support any routing header (again)
520 * this block should check the routing type more
521 * selectively.
522 */
523 error = EINVAL;
524 goto bad;
525 #endif
526
527 /*
528 * Destination options header (1st part).
529 * This only makes sense with a routing header.
530 * See Section 9.2 of RFC 3542.
531 * Disabling this part just for MIP6 convenience is
532 * a bad idea. We need to think carefully about a
533 * way to make the advanced API coexist with MIP6
534 * options, which might automatically be inserted in
535 * the kernel.
536 */
537 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1,
538 optlen);
539 }
540 /* Routing header. */
541 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr, optlen);
542
543 unfragpartlen += optlen;
544
545 /*
546 * NOTE: we don't add AH/ESP length here (done in
547 * ip6_ipsec_output()).
548 */
549
550 /* Destination options header (2nd part). */
551 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2, optlen);
552 }
553
554 /*
555 * If there is at least one extension header,
556 * separate IP6 header from the payload.
557 */
558 hdrsplit = false;
559 if (optlen) {
560 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
561 m = NULL;
562 goto freehdrs;
563 }
564 m = exthdrs.ip6e_ip6;
565 ip6 = mtod(m, struct ip6_hdr *);
566 hdrsplit = true;
567 }
568
569 /* Adjust mbuf packet header length. */
570 m->m_pkthdr.len += optlen;
571 plen = m->m_pkthdr.len - sizeof(*ip6);
572
573 /* If this is a jumbo payload, insert a jumbo payload option. */
574 if (plen > IPV6_MAXPACKET) {
575 if (!hdrsplit) {
576 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
577 m = NULL;
578 goto freehdrs;
579 }
580 m = exthdrs.ip6e_ip6;
581 ip6 = mtod(m, struct ip6_hdr *);
582 hdrsplit = true;
583 }
584 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
585 goto freehdrs;
586 ip6->ip6_plen = 0;
587 } else
588 ip6->ip6_plen = htons(plen);
589 nexthdrp = &ip6->ip6_nxt;
590
591 if (optlen) {
592 /*
593 * Concatenate headers and fill in next header fields.
594 * Here we have, on "m"
595 * IPv6 payload
596 * and we insert headers accordingly.
597 * Finally, we should be getting:
598 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload].
599 *
600 * During the header composing process "m" points to IPv6
601 * header. "mprev" points to an extension header prior to esp.
602 */
603 mprev = m;
604
605 /*
606 * We treat dest2 specially. This makes IPsec processing
607 * much easier. The goal here is to make mprev point the
608 * mbuf prior to dest2.
609 *
610 * Result: IPv6 dest2 payload.
611 * m and mprev will point to IPv6 header.
612 */
613 if (exthdrs.ip6e_dest2) {
614 if (!hdrsplit)
615 panic("%s:%d: assumption failed: "
616 "hdr not split: hdrsplit %d exthdrs %p",
617 __func__, __LINE__, hdrsplit, &exthdrs);
618 exthdrs.ip6e_dest2->m_next = m->m_next;
619 m->m_next = exthdrs.ip6e_dest2;
620 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
621 ip6->ip6_nxt = IPPROTO_DSTOPTS;
622 }
623
624 /*
625 * Result: IPv6 hbh dest1 rthdr dest2 payload.
626 * m will point to IPv6 header. mprev will point to the
627 * extension header prior to dest2 (rthdr in the above case).
628 */
629 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
630 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp,
631 IPPROTO_DSTOPTS);
632 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp,
633 IPPROTO_ROUTING);
634 }
635
636 IP6STAT_INC(ip6s_localout);
637
638 /* Route packet. */
639 ro_pmtu = ro;
640 if (opt && opt->ip6po_rthdr)
641 ro = &opt->ip6po_route;
642 if (ro != NULL)
643 dst = (struct sockaddr_in6 *)&ro->ro_dst;
644 else
645 dst = &sin6;
646 fibnum = (inp != NULL) ? inp->inp_inc.inc_fibnum : M_GETFIB(m);
647
648 again:
649 /*
650 * If specified, try to fill in the traffic class field.
651 * Do not override if a non-zero value is already set.
652 * We check the diffserv field and the ECN field separately.
653 */
654 if (opt && opt->ip6po_tclass >= 0) {
655 int mask = 0;
656
657 if (IPV6_DSCP(ip6) == 0)
658 mask |= 0xfc;
659 if (IPV6_ECN(ip6) == 0)
660 mask |= 0x03;
661 if (mask != 0)
662 ip6->ip6_flow |= htonl((opt->ip6po_tclass & mask) << 20);
663 }
664
665 /* Fill in or override the hop limit field, if necessary. */
666 if (opt && opt->ip6po_hlim != -1)
667 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
668 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
669 if (im6o != NULL)
670 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
671 else
672 ip6->ip6_hlim = V_ip6_defmcasthlim;
673 }
674
675 if (ro == NULL || ro->ro_nh == NULL) {
676 bzero(dst, sizeof(*dst));
677 dst->sin6_family = AF_INET6;
678 dst->sin6_len = sizeof(*dst);
679 dst->sin6_addr = ip6->ip6_dst;
680 }
681 /*
682 * Validate route against routing table changes.
683 * Make sure that the address family is set in route.
684 */
685 nh = NULL;
686 ifp = NULL;
687 mtu = 0;
688 if (ro != NULL) {
689 if (ro->ro_nh != NULL && inp != NULL) {
690 ro->ro_dst.sin6_family = AF_INET6; /* XXX KASSERT? */
691 NH_VALIDATE((struct route *)ro, &inp->inp_rt_cookie,
692 fibnum);
693 }
694 if (ro->ro_nh != NULL && fwd_tag == NULL &&
695 (!NH_IS_VALID(ro->ro_nh) ||
696 ro->ro_dst.sin6_family != AF_INET6 ||
697 !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)))
698 RO_INVALIDATE_CACHE(ro);
699
700 if (ro->ro_nh != NULL && fwd_tag == NULL &&
701 ro->ro_dst.sin6_family == AF_INET6 &&
702 IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, &ip6->ip6_dst)) {
703 /* Nexthop is valid and contains valid ifp */
704 nh = ro->ro_nh;
705 } else {
706 if (ro->ro_lle)
707 LLE_FREE(ro->ro_lle); /* zeros ro_lle */
708 ro->ro_lle = NULL;
709 if (fwd_tag == NULL) {
710 bzero(&dst_sa, sizeof(dst_sa));
711 dst_sa.sin6_family = AF_INET6;
712 dst_sa.sin6_len = sizeof(dst_sa);
713 dst_sa.sin6_addr = ip6->ip6_dst;
714 }
715 error = in6_selectroute(&dst_sa, opt, im6o, ro, &ifp,
716 &nh, fibnum, m->m_pkthdr.flowid);
717 if (error != 0) {
718 IP6STAT_INC(ip6s_noroute);
719 if (ifp != NULL)
720 in6_ifstat_inc(ifp, ifs6_out_discard);
721 goto bad;
722 }
723 /*
724 * At this point at least @ifp is not NULL
725 * Can be the case when dst is multicast, link-local or
726 * interface is explicitly specificed by the caller.
727 */
728 }
729 if (nh == NULL) {
730 /*
731 * If in6_selectroute() does not return a nexthop
732 * dst may not have been updated.
733 */
734 *dst = dst_sa; /* XXX */
735 origifp = ifp;
736 mtu = ifp->if_mtu;
737 } else {
738 ifp = nh->nh_ifp;
739 origifp = nh->nh_aifp;
740 ia = (struct in6_ifaddr *)(nh->nh_ifa);
741 counter_u64_add(nh->nh_pksent, 1);
742 }
743 } else {
744 struct nhop_object *nh;
745 struct in6_addr kdst;
746 uint32_t scopeid;
747
748 if (fwd_tag == NULL) {
749 bzero(&dst_sa, sizeof(dst_sa));
750 dst_sa.sin6_family = AF_INET6;
751 dst_sa.sin6_len = sizeof(dst_sa);
752 dst_sa.sin6_addr = ip6->ip6_dst;
753 }
754
755 if (IN6_IS_ADDR_MULTICAST(&dst_sa.sin6_addr) &&
756 im6o != NULL &&
757 (ifp = im6o->im6o_multicast_ifp) != NULL) {
758 /* We do not need a route lookup. */
759 *dst = dst_sa; /* XXX */
760 origifp = ifp;
761 goto nonh6lookup;
762 }
763
764 in6_splitscope(&dst_sa.sin6_addr, &kdst, &scopeid);
765
766 if (IN6_IS_ADDR_MC_LINKLOCAL(&dst_sa.sin6_addr) ||
767 IN6_IS_ADDR_MC_NODELOCAL(&dst_sa.sin6_addr)) {
768 if (scopeid > 0) {
769 ifp = in6_getlinkifnet(scopeid);
770 if (ifp == NULL) {
771 error = EHOSTUNREACH;
772 goto bad;
773 }
774 *dst = dst_sa; /* XXX */
775 origifp = ifp;
776 goto nonh6lookup;
777 }
778 }
779
780 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE,
781 m->m_pkthdr.flowid);
782 if (nh == NULL) {
783 IP6STAT_INC(ip6s_noroute);
784 /* No ifp in6_ifstat_inc(ifp, ifs6_out_discard); */
785 error = EHOSTUNREACH;
786 goto bad;
787 }
788
789 ifp = nh->nh_ifp;
790 origifp = nh->nh_aifp;
791 ia = ifatoia6(nh->nh_ifa);
792 if (nh->nh_flags & NHF_GATEWAY)
793 dst->sin6_addr = nh->gw6_sa.sin6_addr;
794 else if (fwd_tag != NULL)
795 dst->sin6_addr = dst_sa.sin6_addr;
796 nonh6lookup:
797 ;
798 }
799 /*
800 * At this point ifp MUST be pointing to the valid transmit ifp.
801 * origifp MUST be valid and pointing to either the same ifp or,
802 * in case of loopback output, to the interface which ip6_src
803 * belongs to.
804 * Examples:
805 * fe80::1%em0 -> fe80::2%em0 -> ifp=em0, origifp=em0
806 * fe80::1%em0 -> fe80::1%em0 -> ifp=lo0, origifp=em0
807 * ::1 -> ::1 -> ifp=lo0, origifp=lo0
808 *
809 * mtu can be 0 and will be refined later.
810 */
811 KASSERT((ifp != NULL), ("output interface must not be NULL"));
812 KASSERT((origifp != NULL), ("output address interface must not be NULL"));
813
814 if ((flags & IPV6_FORWARDING) == 0) {
815 /* XXX: the FORWARDING flag can be set for mrouting. */
816 in6_ifstat_inc(ifp, ifs6_out_request);
817 }
818
819 /* Setup data structures for scope ID checks. */
820 src0 = ip6->ip6_src;
821 bzero(&src_sa, sizeof(src_sa));
822 src_sa.sin6_family = AF_INET6;
823 src_sa.sin6_len = sizeof(src_sa);
824 src_sa.sin6_addr = ip6->ip6_src;
825
826 dst0 = ip6->ip6_dst;
827 /* Re-initialize to be sure. */
828 bzero(&dst_sa, sizeof(dst_sa));
829 dst_sa.sin6_family = AF_INET6;
830 dst_sa.sin6_len = sizeof(dst_sa);
831 dst_sa.sin6_addr = ip6->ip6_dst;
832
833 /* Check for valid scope ID. */
834 if (in6_setscope(&src0, origifp, &zone) == 0 &&
835 sa6_recoverscope(&src_sa) == 0 && zone == src_sa.sin6_scope_id &&
836 in6_setscope(&dst0, origifp, &zone) == 0 &&
837 sa6_recoverscope(&dst_sa) == 0 && zone == dst_sa.sin6_scope_id) {
838 /*
839 * The outgoing interface is in the zone of the source
840 * and destination addresses.
841 *
842 */
843 } else if ((origifp->if_flags & IFF_LOOPBACK) == 0 ||
844 sa6_recoverscope(&src_sa) != 0 ||
845 sa6_recoverscope(&dst_sa) != 0 ||
846 dst_sa.sin6_scope_id == 0 ||
847 (src_sa.sin6_scope_id != 0 &&
848 src_sa.sin6_scope_id != dst_sa.sin6_scope_id) ||
849 ifnet_byindex(dst_sa.sin6_scope_id) == NULL) {
850 /*
851 * If the destination network interface is not a
852 * loopback interface, or the destination network
853 * address has no scope ID, or the source address has
854 * a scope ID set which is different from the
855 * destination address one, or there is no network
856 * interface representing this scope ID, the address
857 * pair is considered invalid.
858 */
859 IP6STAT_INC(ip6s_badscope);
860 in6_ifstat_inc(origifp, ifs6_out_discard);
861 if (error == 0)
862 error = EHOSTUNREACH; /* XXX */
863 goto bad;
864 }
865 /* All scope ID checks are successful. */
866
867 if (nh && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
868 if (opt && opt->ip6po_nextroute.ro_nh) {
869 /*
870 * The nexthop is explicitly specified by the
871 * application. We assume the next hop is an IPv6
872 * address.
873 */
874 dst = (struct sockaddr_in6 *)opt->ip6po_nexthop;
875 }
876 else if ((nh->nh_flags & NHF_GATEWAY))
877 dst = &nh->gw6_sa;
878 }
879
880 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
881 m->m_flags &= ~(M_BCAST | M_MCAST); /* Just in case. */
882 } else {
883 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
884 in6_ifstat_inc(ifp, ifs6_out_mcast);
885
886 /* Confirm that the outgoing interface supports multicast. */
887 if (!(ifp->if_flags & IFF_MULTICAST)) {
888 IP6STAT_INC(ip6s_noroute);
889 in6_ifstat_inc(ifp, ifs6_out_discard);
890 error = ENETUNREACH;
891 goto bad;
892 }
893 if ((im6o == NULL && in6_mcast_loop) ||
894 (im6o && im6o->im6o_multicast_loop)) {
895 /*
896 * Loop back multicast datagram if not expressly
897 * forbidden to do so, even if we have not joined
898 * the address; protocols will filter it later,
899 * thus deferring a hash lookup and lock acquisition
900 * at the expense of an m_copym().
901 */
902 ip6_mloopback(ifp, m);
903 } else {
904 /*
905 * If we are acting as a multicast router, perform
906 * multicast forwarding as if the packet had just
907 * arrived on the interface to which we are about
908 * to send. The multicast forwarding function
909 * recursively calls this function, using the
910 * IPV6_FORWARDING flag to prevent infinite recursion.
911 *
912 * Multicasts that are looped back by ip6_mloopback(),
913 * above, will be forwarded by the ip6_input() routine,
914 * if necessary.
915 */
916 if (V_ip6_mrouter && (flags & IPV6_FORWARDING) == 0) {
917 /*
918 * XXX: ip6_mforward expects that rcvif is NULL
919 * when it is called from the originating path.
920 * However, it may not always be the case.
921 */
922 m->m_pkthdr.rcvif = NULL;
923 if (ip6_mforward(ip6, ifp, m) != 0) {
924 m_freem(m);
925 goto done;
926 }
927 }
928 }
929 /*
930 * Multicasts with a hoplimit of zero may be looped back,
931 * above, but must not be transmitted on a network.
932 * Also, multicasts addressed to the loopback interface
933 * are not sent -- the above call to ip6_mloopback() will
934 * loop back a copy if this host actually belongs to the
935 * destination group on the loopback interface.
936 */
937 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
938 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
939 m_freem(m);
940 goto done;
941 }
942 }
943
944 /*
945 * Fill the outgoing inteface to tell the upper layer
946 * to increment per-interface statistics.
947 */
948 if (ifpp)
949 *ifpp = ifp;
950
951 /* Determine path MTU. */
952 if ((error = ip6_getpmtu(ro_pmtu, ro != ro_pmtu, ifp, &ip6->ip6_dst,
953 &mtu, &alwaysfrag, fibnum, *nexthdrp)) != 0)
954 goto bad;
955 KASSERT(mtu > 0, ("%s:%d: mtu %ld, ro_pmtu %p ro %p ifp %p "
956 "alwaysfrag %d fibnum %u\n", __func__, __LINE__, mtu, ro_pmtu, ro,
957 ifp, alwaysfrag, fibnum));
958
959 /*
960 * The caller of this function may specify to use the minimum MTU
961 * in some cases.
962 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
963 * setting. The logic is a bit complicated; by default, unicast
964 * packets will follow path MTU while multicast packets will be sent at
965 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
966 * including unicast ones will be sent at the minimum MTU. Multicast
967 * packets will always be sent at the minimum MTU unless
968 * IP6PO_MINMTU_DISABLE is explicitly specified.
969 * See RFC 3542 for more details.
970 */
971 if (mtu > IPV6_MMTU) {
972 if ((flags & IPV6_MINMTU))
973 mtu = IPV6_MMTU;
974 else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL)
975 mtu = IPV6_MMTU;
976 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
977 (opt == NULL ||
978 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
979 mtu = IPV6_MMTU;
980 }
981 }
982
983 /*
984 * Clear embedded scope identifiers if necessary.
985 * in6_clearscope() will touch the addresses only when necessary.
986 */
987 in6_clearscope(&ip6->ip6_src);
988 in6_clearscope(&ip6->ip6_dst);
989
990 /*
991 * If the outgoing packet contains a hop-by-hop options header,
992 * it must be examined and processed even by the source node.
993 * (RFC 2460, section 4.)
994 */
995 if (exthdrs.ip6e_hbh) {
996 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
997 u_int32_t dummy; /* XXX unused */
998 u_int32_t plen = 0; /* XXX: ip6_process will check the value */
999
1000 #ifdef DIAGNOSTIC
1001 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
1002 panic("ip6e_hbh is not contiguous");
1003 #endif
1004 /*
1005 * XXX: if we have to send an ICMPv6 error to the sender,
1006 * we need the M_LOOP flag since icmp6_error() expects
1007 * the IPv6 and the hop-by-hop options header are
1008 * contiguous unless the flag is set.
1009 */
1010 m->m_flags |= M_LOOP;
1011 m->m_pkthdr.rcvif = ifp;
1012 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1013 ((hbh->ip6h_len + 1) << 3) - sizeof(struct ip6_hbh),
1014 &dummy, &plen) < 0) {
1015 /* m was already freed at this point. */
1016 error = EINVAL;/* better error? */
1017 goto done;
1018 }
1019 m->m_flags &= ~M_LOOP; /* XXX */
1020 m->m_pkthdr.rcvif = NULL;
1021 }
1022
1023 /* Jump over all PFIL processing if hooks are not active. */
1024 if (!PFIL_HOOKED_OUT(V_inet6_pfil_head))
1025 goto passout;
1026
1027 odst = ip6->ip6_dst;
1028 /* Run through list of hooks for output packets. */
1029 switch (pfil_mbuf_out(V_inet6_pfil_head, &m, ifp, inp)) {
1030 case PFIL_PASS:
1031 ip6 = mtod(m, struct ip6_hdr *);
1032 break;
1033 case PFIL_DROPPED:
1034 error = EACCES;
1035 /* FALLTHROUGH */
1036 case PFIL_CONSUMED:
1037 goto done;
1038 }
1039
1040 needfiblookup = 0;
1041 /* See if destination IP address was changed by packet filter. */
1042 if (!IN6_ARE_ADDR_EQUAL(&odst, &ip6->ip6_dst)) {
1043 m->m_flags |= M_SKIP_FIREWALL;
1044 /* If destination is now ourself drop to ip6_input(). */
1045 if (in6_localip(&ip6->ip6_dst)) {
1046 m->m_flags |= M_FASTFWD_OURS;
1047 if (m->m_pkthdr.rcvif == NULL)
1048 m->m_pkthdr.rcvif = V_loif;
1049 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1050 m->m_pkthdr.csum_flags |=
1051 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1052 m->m_pkthdr.csum_data = 0xffff;
1053 }
1054 #if defined(SCTP) || defined(SCTP_SUPPORT)
1055 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1056 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1057 #endif
1058 error = netisr_queue(NETISR_IPV6, m);
1059 goto done;
1060 } else {
1061 if (ro != NULL)
1062 RO_INVALIDATE_CACHE(ro);
1063 needfiblookup = 1; /* Redo the routing table lookup. */
1064 }
1065 }
1066 /* See if fib was changed by packet filter. */
1067 if (fibnum != M_GETFIB(m)) {
1068 m->m_flags |= M_SKIP_FIREWALL;
1069 fibnum = M_GETFIB(m);
1070 if (ro != NULL)
1071 RO_INVALIDATE_CACHE(ro);
1072 needfiblookup = 1;
1073 }
1074 if (needfiblookup)
1075 goto again;
1076
1077 /* See if local, if yes, send it to netisr. */
1078 if (m->m_flags & M_FASTFWD_OURS) {
1079 if (m->m_pkthdr.rcvif == NULL)
1080 m->m_pkthdr.rcvif = V_loif;
1081 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
1082 m->m_pkthdr.csum_flags |=
1083 CSUM_DATA_VALID_IPV6 | CSUM_PSEUDO_HDR;
1084 m->m_pkthdr.csum_data = 0xffff;
1085 }
1086 #if defined(SCTP) || defined(SCTP_SUPPORT)
1087 if (m->m_pkthdr.csum_flags & CSUM_SCTP_IPV6)
1088 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID;
1089 #endif
1090 error = netisr_queue(NETISR_IPV6, m);
1091 goto done;
1092 }
1093 /* Or forward to some other address? */
1094 if ((m->m_flags & M_IP6_NEXTHOP) &&
1095 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
1096 if (ro != NULL)
1097 dst = (struct sockaddr_in6 *)&ro->ro_dst;
1098 else
1099 dst = &sin6;
1100 bcopy((fwd_tag+1), &dst_sa, sizeof(struct sockaddr_in6));
1101 m->m_flags |= M_SKIP_FIREWALL;
1102 m->m_flags &= ~M_IP6_NEXTHOP;
1103 m_tag_delete(m, fwd_tag);
1104 goto again;
1105 }
1106
1107 passout:
1108 if (vlan_pcp > -1)
1109 EVL_APPLY_PRI(m, vlan_pcp);
1110
1111 /* Ensure the packet data is mapped if the interface requires it. */
1112 if ((ifp->if_capenable & IFCAP_MEXTPG) == 0) {
1113 struct mbuf *m1;
1114
1115 error = mb_unmapped_to_ext(m, &m1);
1116 if (error != 0) {
1117 if (error == EINVAL) {
1118 if_printf(ifp, "TLS packet\n");
1119 /* XXXKIB */
1120 } else if (error == ENOMEM) {
1121 error = ENOBUFS;
1122 }
1123 IP6STAT_INC(ip6s_odropped);
1124 return (error);
1125 } else {
1126 m = m1;
1127 }
1128 }
1129
1130 /*
1131 * Send the packet to the outgoing interface.
1132 * If necessary, do IPv6 fragmentation before sending.
1133 *
1134 * The logic here is rather complex:
1135 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1136 * 1-a: send as is if tlen <= path mtu
1137 * 1-b: fragment if tlen > path mtu
1138 *
1139 * 2: if user asks us not to fragment (dontfrag == 1)
1140 * 2-a: send as is if tlen <= interface mtu
1141 * 2-b: error if tlen > interface mtu
1142 *
1143 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1144 * always fragment
1145 *
1146 * 4: if dontfrag == 1 && alwaysfrag == 1
1147 * error, as we cannot handle this conflicting request.
1148 */
1149 sw_csum = m->m_pkthdr.csum_flags;
1150 if (!hdrsplit) {
1151 tso = ((sw_csum & ifp->if_hwassist &
1152 (CSUM_TSO | CSUM_INNER_TSO)) != 0) ? 1 : 0;
1153 sw_csum &= ~ifp->if_hwassist;
1154 } else
1155 tso = 0;
1156 /*
1157 * If we added extension headers, we will not do TSO and calculate the
1158 * checksums ourselves for now.
1159 * XXX-BZ Need a framework to know when the NIC can handle it, even
1160 * with ext. hdrs.
1161 */
1162 ip6_output_delayed_csum(m, ifp, sw_csum, plen, optlen);
1163 /* XXX-BZ m->m_pkthdr.csum_flags &= ~ifp->if_hwassist; */
1164 tlen = m->m_pkthdr.len;
1165
1166 if ((opt && (opt->ip6po_flags & IP6PO_DONTFRAG)) || tso)
1167 dontfrag = 1;
1168 else
1169 dontfrag = 0;
1170 if (dontfrag && alwaysfrag) { /* Case 4. */
1171 /* Conflicting request - can't transmit. */
1172 error = EMSGSIZE;
1173 goto bad;
1174 }
1175 if (dontfrag && tlen > IN6_LINKMTU(ifp) && !tso) { /* Case 2-b. */
1176 /*
1177 * Even if the DONTFRAG option is specified, we cannot send the
1178 * packet when the data length is larger than the MTU of the
1179 * outgoing interface.
1180 * Notify the error by sending IPV6_PATHMTU ancillary data if
1181 * application wanted to know the MTU value. Also return an
1182 * error code (this is not described in the API spec).
1183 */
1184 if (inp != NULL)
1185 ip6_notify_pmtu(inp, &dst_sa, (u_int32_t)mtu);
1186 error = EMSGSIZE;
1187 goto bad;
1188 }
1189
1190 /* Transmit packet without fragmentation. */
1191 if (dontfrag || (!alwaysfrag && tlen <= mtu)) { /* Cases 1-a and 2-a. */
1192 struct in6_ifaddr *ia6;
1193
1194 ip6 = mtod(m, struct ip6_hdr *);
1195 ia6 = in6_ifawithifp(ifp, &ip6->ip6_src);
1196 if (ia6) {
1197 /* Record statistics for this interface address. */
1198 counter_u64_add(ia6->ia_ifa.ifa_opackets, 1);
1199 counter_u64_add(ia6->ia_ifa.ifa_obytes,
1200 m->m_pkthdr.len);
1201 }
1202 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1203 (flags & IP_NO_SND_TAG_RL) ? false : true);
1204 goto done;
1205 }
1206
1207 /* Try to fragment the packet. Cases 1-b and 3. */
1208 if (mtu < IPV6_MMTU) {
1209 /* Path MTU cannot be less than IPV6_MMTU. */
1210 error = EMSGSIZE;
1211 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1212 goto bad;
1213 } else if (ip6->ip6_plen == 0) {
1214 /* Jumbo payload cannot be fragmented. */
1215 error = EMSGSIZE;
1216 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1217 goto bad;
1218 } else {
1219 u_char nextproto;
1220
1221 /*
1222 * Too large for the destination or interface;
1223 * fragment if possible.
1224 * Must be able to put at least 8 bytes per fragment.
1225 */
1226 if (mtu > IPV6_MAXPACKET)
1227 mtu = IPV6_MAXPACKET;
1228
1229 len = (mtu - unfragpartlen - sizeof(struct ip6_frag)) & ~7;
1230 if (len < 8) {
1231 error = EMSGSIZE;
1232 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1233 goto bad;
1234 }
1235
1236 /*
1237 * If the interface will not calculate checksums on
1238 * fragmented packets, then do it here.
1239 * XXX-BZ handle the hw offloading case. Need flags.
1240 */
1241 ip6_output_delayed_csum(m, ifp, m->m_pkthdr.csum_flags, plen,
1242 optlen);
1243
1244 /*
1245 * Change the next header field of the last header in the
1246 * unfragmentable part.
1247 */
1248 if (exthdrs.ip6e_rthdr) {
1249 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1250 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1251 } else if (exthdrs.ip6e_dest1) {
1252 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1253 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1254 } else if (exthdrs.ip6e_hbh) {
1255 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1256 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1257 } else {
1258 ip6 = mtod(m, struct ip6_hdr *);
1259 nextproto = ip6->ip6_nxt;
1260 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1261 }
1262
1263 /*
1264 * Loop through length of segment after first fragment,
1265 * make new header and copy data of each part and link onto
1266 * chain.
1267 */
1268 m0 = m;
1269 id = htonl(ip6_randomid());
1270 error = ip6_fragment(ifp, m, unfragpartlen, nextproto,len, id);
1271 if (error != 0)
1272 goto sendorfree;
1273
1274 in6_ifstat_inc(ifp, ifs6_out_fragok);
1275 }
1276
1277 /* Remove leading garbage. */
1278 sendorfree:
1279 m = m0->m_nextpkt;
1280 m0->m_nextpkt = 0;
1281 m_freem(m0);
1282 for (; m; m = m0) {
1283 m0 = m->m_nextpkt;
1284 m->m_nextpkt = 0;
1285 if (error == 0) {
1286 /* Record statistics for this interface address. */
1287 if (ia) {
1288 counter_u64_add(ia->ia_ifa.ifa_opackets, 1);
1289 counter_u64_add(ia->ia_ifa.ifa_obytes,
1290 m->m_pkthdr.len);
1291 }
1292 if (vlan_pcp > -1)
1293 EVL_APPLY_PRI(m, vlan_pcp);
1294 error = ip6_output_send(inp, ifp, origifp, m, dst, ro,
1295 true);
1296 } else
1297 m_freem(m);
1298 }
1299
1300 if (error == 0)
1301 IP6STAT_INC(ip6s_fragmented);
1302
1303 done:
1304 return (error);
1305
1306 freehdrs:
1307 m_freem(exthdrs.ip6e_hbh); /* m_freem() checks if mbuf is NULL. */
1308 m_freem(exthdrs.ip6e_dest1);
1309 m_freem(exthdrs.ip6e_rthdr);
1310 m_freem(exthdrs.ip6e_dest2);
1311 /* FALLTHROUGH */
1312 bad:
1313 if (m)
1314 m_freem(m);
1315 goto done;
1316 }
1317
1318 static int
ip6_copyexthdr(struct mbuf ** mp,caddr_t hdr,int hlen)1319 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1320 {
1321 struct mbuf *m;
1322
1323 if (hlen > MCLBYTES)
1324 return (ENOBUFS); /* XXX */
1325
1326 if (hlen > MLEN)
1327 m = m_getcl(M_NOWAIT, MT_DATA, 0);
1328 else
1329 m = m_get(M_NOWAIT, MT_DATA);
1330 if (m == NULL)
1331 return (ENOBUFS);
1332 m->m_len = hlen;
1333 if (hdr)
1334 bcopy(hdr, mtod(m, caddr_t), hlen);
1335
1336 *mp = m;
1337 return (0);
1338 }
1339
1340 /*
1341 * Insert jumbo payload option.
1342 */
1343 static int
ip6_insert_jumboopt(struct ip6_exthdrs * exthdrs,u_int32_t plen)1344 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1345 {
1346 struct mbuf *mopt;
1347 u_char *optbuf;
1348 u_int32_t v;
1349
1350 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1351
1352 /*
1353 * If there is no hop-by-hop options header, allocate new one.
1354 * If there is one but it doesn't have enough space to store the
1355 * jumbo payload option, allocate a cluster to store the whole options.
1356 * Otherwise, use it to store the options.
1357 */
1358 if (exthdrs->ip6e_hbh == NULL) {
1359 mopt = m_get(M_NOWAIT, MT_DATA);
1360 if (mopt == NULL)
1361 return (ENOBUFS);
1362 mopt->m_len = JUMBOOPTLEN;
1363 optbuf = mtod(mopt, u_char *);
1364 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1365 exthdrs->ip6e_hbh = mopt;
1366 } else {
1367 struct ip6_hbh *hbh;
1368
1369 mopt = exthdrs->ip6e_hbh;
1370 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
1371 /*
1372 * XXX assumption:
1373 * - exthdrs->ip6e_hbh is not referenced from places
1374 * other than exthdrs.
1375 * - exthdrs->ip6e_hbh is not an mbuf chain.
1376 */
1377 int oldoptlen = mopt->m_len;
1378 struct mbuf *n;
1379
1380 /*
1381 * XXX: give up if the whole (new) hbh header does
1382 * not fit even in an mbuf cluster.
1383 */
1384 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
1385 return (ENOBUFS);
1386
1387 /*
1388 * As a consequence, we must always prepare a cluster
1389 * at this point.
1390 */
1391 n = m_getcl(M_NOWAIT, MT_DATA, 0);
1392 if (n == NULL)
1393 return (ENOBUFS);
1394 n->m_len = oldoptlen + JUMBOOPTLEN;
1395 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1396 oldoptlen);
1397 optbuf = mtod(n, caddr_t) + oldoptlen;
1398 m_freem(mopt);
1399 mopt = exthdrs->ip6e_hbh = n;
1400 } else {
1401 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1402 mopt->m_len += JUMBOOPTLEN;
1403 }
1404 optbuf[0] = IP6OPT_PADN;
1405 optbuf[1] = 1;
1406
1407 /*
1408 * Adjust the header length according to the pad and
1409 * the jumbo payload option.
1410 */
1411 hbh = mtod(mopt, struct ip6_hbh *);
1412 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1413 }
1414
1415 /* fill in the option. */
1416 optbuf[2] = IP6OPT_JUMBO;
1417 optbuf[3] = 4;
1418 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1419 bcopy(&v, &optbuf[4], sizeof(u_int32_t));
1420
1421 /* finally, adjust the packet header length */
1422 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1423
1424 return (0);
1425 #undef JUMBOOPTLEN
1426 }
1427
1428 /*
1429 * Insert fragment header and copy unfragmentable header portions.
1430 */
1431 static int
ip6_insertfraghdr(struct mbuf * m0,struct mbuf * m,int hlen,struct ip6_frag ** frghdrp)1432 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1433 struct ip6_frag **frghdrp)
1434 {
1435 struct mbuf *n, *mlast;
1436
1437 if (hlen > sizeof(struct ip6_hdr)) {
1438 n = m_copym(m0, sizeof(struct ip6_hdr),
1439 hlen - sizeof(struct ip6_hdr), M_NOWAIT);
1440 if (n == NULL)
1441 return (ENOBUFS);
1442 m->m_next = n;
1443 } else
1444 n = m;
1445
1446 /* Search for the last mbuf of unfragmentable part. */
1447 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1448 ;
1449
1450 if (M_WRITABLE(mlast) &&
1451 M_TRAILINGSPACE(mlast) >= sizeof(struct ip6_frag)) {
1452 /* use the trailing space of the last mbuf for the fragment hdr */
1453 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1454 mlast->m_len);
1455 mlast->m_len += sizeof(struct ip6_frag);
1456 m->m_pkthdr.len += sizeof(struct ip6_frag);
1457 } else {
1458 /* allocate a new mbuf for the fragment header */
1459 struct mbuf *mfrg;
1460
1461 mfrg = m_get(M_NOWAIT, MT_DATA);
1462 if (mfrg == NULL)
1463 return (ENOBUFS);
1464 mfrg->m_len = sizeof(struct ip6_frag);
1465 *frghdrp = mtod(mfrg, struct ip6_frag *);
1466 mlast->m_next = mfrg;
1467 }
1468
1469 return (0);
1470 }
1471
1472 /*
1473 * Calculates IPv6 path mtu for destination @dst.
1474 * Resulting MTU is stored in @mtup.
1475 *
1476 * Returns 0 on success.
1477 */
1478 static int
ip6_getpmtu_ctl(u_int fibnum,const struct in6_addr * dst,u_long * mtup)1479 ip6_getpmtu_ctl(u_int fibnum, const struct in6_addr *dst, u_long *mtup)
1480 {
1481 struct epoch_tracker et;
1482 struct nhop_object *nh;
1483 struct in6_addr kdst;
1484 uint32_t scopeid;
1485 int error;
1486
1487 in6_splitscope(dst, &kdst, &scopeid);
1488
1489 NET_EPOCH_ENTER(et);
1490 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1491 if (nh != NULL)
1492 error = ip6_calcmtu(nh->nh_ifp, dst, nh->nh_mtu, mtup, NULL, 0);
1493 else
1494 error = EHOSTUNREACH;
1495 NET_EPOCH_EXIT(et);
1496
1497 return (error);
1498 }
1499
1500 /*
1501 * Calculates IPv6 path MTU for @dst based on transmit @ifp,
1502 * and cached data in @ro_pmtu.
1503 * MTU from (successful) route lookup is saved (along with dst)
1504 * inside @ro_pmtu to avoid subsequent route lookups after packet
1505 * filter processing.
1506 *
1507 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1508 * Returns 0 on success.
1509 */
1510 static int
ip6_getpmtu(struct route_in6 * ro_pmtu,int do_lookup,struct ifnet * ifp,const struct in6_addr * dst,u_long * mtup,int * alwaysfragp,u_int fibnum,u_int proto)1511 ip6_getpmtu(struct route_in6 *ro_pmtu, int do_lookup,
1512 struct ifnet *ifp, const struct in6_addr *dst, u_long *mtup,
1513 int *alwaysfragp, u_int fibnum, u_int proto)
1514 {
1515 struct nhop_object *nh;
1516 struct in6_addr kdst;
1517 uint32_t scopeid;
1518 struct sockaddr_in6 *sa6_dst, sin6;
1519 u_long mtu;
1520
1521 NET_EPOCH_ASSERT();
1522
1523 mtu = 0;
1524 if (ro_pmtu == NULL || do_lookup) {
1525 /*
1526 * Here ro_pmtu has final destination address, while
1527 * ro might represent immediate destination.
1528 * Use ro_pmtu destination since mtu might differ.
1529 */
1530 if (ro_pmtu != NULL) {
1531 sa6_dst = (struct sockaddr_in6 *)&ro_pmtu->ro_dst;
1532 if (!IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1533 ro_pmtu->ro_mtu = 0;
1534 } else
1535 sa6_dst = &sin6;
1536
1537 if (ro_pmtu == NULL || ro_pmtu->ro_mtu == 0) {
1538 bzero(sa6_dst, sizeof(*sa6_dst));
1539 sa6_dst->sin6_family = AF_INET6;
1540 sa6_dst->sin6_len = sizeof(struct sockaddr_in6);
1541 sa6_dst->sin6_addr = *dst;
1542
1543 in6_splitscope(dst, &kdst, &scopeid);
1544 nh = fib6_lookup(fibnum, &kdst, scopeid, NHR_NONE, 0);
1545 if (nh != NULL) {
1546 mtu = nh->nh_mtu;
1547 if (ro_pmtu != NULL)
1548 ro_pmtu->ro_mtu = mtu;
1549 }
1550 } else
1551 mtu = ro_pmtu->ro_mtu;
1552 }
1553
1554 if (ro_pmtu != NULL && ro_pmtu->ro_nh != NULL)
1555 mtu = ro_pmtu->ro_nh->nh_mtu;
1556
1557 return (ip6_calcmtu(ifp, dst, mtu, mtup, alwaysfragp, proto));
1558 }
1559
1560 /*
1561 * Calculate MTU based on transmit @ifp, route mtu @rt_mtu and
1562 * hostcache data for @dst.
1563 * Stores mtu and always-frag value into @mtup and @alwaysfragp.
1564 *
1565 * Returns 0 on success.
1566 */
1567 static int
ip6_calcmtu(struct ifnet * ifp,const struct in6_addr * dst,u_long rt_mtu,u_long * mtup,int * alwaysfragp,u_int proto)1568 ip6_calcmtu(struct ifnet *ifp, const struct in6_addr *dst, u_long rt_mtu,
1569 u_long *mtup, int *alwaysfragp, u_int proto)
1570 {
1571 u_long mtu = 0;
1572 int alwaysfrag = 0;
1573 int error = 0;
1574
1575 if (rt_mtu > 0) {
1576 u_int32_t ifmtu;
1577 struct in_conninfo inc;
1578
1579 bzero(&inc, sizeof(inc));
1580 inc.inc_flags |= INC_ISIPV6;
1581 inc.inc6_faddr = *dst;
1582
1583 ifmtu = IN6_LINKMTU(ifp);
1584
1585 /* TCP is known to react to pmtu changes so skip hc */
1586 if (proto != IPPROTO_TCP)
1587 mtu = tcp_hc_getmtu(&inc);
1588
1589 if (mtu)
1590 mtu = min(mtu, rt_mtu);
1591 else
1592 mtu = rt_mtu;
1593 if (mtu == 0)
1594 mtu = ifmtu;
1595 else if (mtu < IPV6_MMTU) {
1596 /*
1597 * RFC2460 section 5, last paragraph:
1598 * if we record ICMPv6 too big message with
1599 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
1600 * or smaller, with framgent header attached.
1601 * (fragment header is needed regardless from the
1602 * packet size, for translators to identify packets)
1603 */
1604 alwaysfrag = 1;
1605 mtu = IPV6_MMTU;
1606 }
1607 } else if (ifp) {
1608 mtu = IN6_LINKMTU(ifp);
1609 } else
1610 error = EHOSTUNREACH; /* XXX */
1611
1612 *mtup = mtu;
1613 if (alwaysfragp)
1614 *alwaysfragp = alwaysfrag;
1615 return (error);
1616 }
1617
1618 /*
1619 * IP6 socket option processing.
1620 */
1621 int
ip6_ctloutput(struct socket * so,struct sockopt * sopt)1622 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1623 {
1624 int optdatalen, uproto;
1625 void *optdata;
1626 struct inpcb *inp = sotoinpcb(so);
1627 int error, optval;
1628 int level, op, optname;
1629 int optlen;
1630 struct thread *td;
1631 #ifdef RSS
1632 uint32_t rss_bucket;
1633 int retval;
1634 #endif
1635
1636 /*
1637 * Don't use more than a quarter of mbuf clusters. N.B.:
1638 * nmbclusters is an int, but nmbclusters * MCLBYTES may overflow
1639 * on LP64 architectures, so cast to u_long to avoid undefined
1640 * behavior. ILP32 architectures cannot have nmbclusters
1641 * large enough to overflow for other reasons.
1642 */
1643 #define IPV6_PKTOPTIONS_MBUF_LIMIT ((u_long)nmbclusters * MCLBYTES / 4)
1644
1645 level = sopt->sopt_level;
1646 op = sopt->sopt_dir;
1647 optname = sopt->sopt_name;
1648 optlen = sopt->sopt_valsize;
1649 td = sopt->sopt_td;
1650 error = 0;
1651 optval = 0;
1652 uproto = (int)so->so_proto->pr_protocol;
1653
1654 if (level != IPPROTO_IPV6) {
1655 error = EINVAL;
1656
1657 if (sopt->sopt_level == SOL_SOCKET &&
1658 sopt->sopt_dir == SOPT_SET) {
1659 switch (sopt->sopt_name) {
1660 case SO_SETFIB:
1661 error = sooptcopyin(sopt, &optval,
1662 sizeof(optval), sizeof(optval));
1663 if (error != 0)
1664 break;
1665
1666 INP_WLOCK(inp);
1667 if ((inp->inp_flags & INP_BOUNDFIB) != 0 &&
1668 optval != so->so_fibnum) {
1669 INP_WUNLOCK(inp);
1670 error = EISCONN;
1671 break;
1672 }
1673 error = sosetfib(inp->inp_socket, optval);
1674 if (error == 0)
1675 inp->inp_inc.inc_fibnum = optval;
1676 INP_WUNLOCK(inp);
1677 break;
1678 case SO_MAX_PACING_RATE:
1679 #ifdef RATELIMIT
1680 INP_WLOCK(inp);
1681 inp->inp_flags2 |= INP_RATE_LIMIT_CHANGED;
1682 INP_WUNLOCK(inp);
1683 error = 0;
1684 #else
1685 error = EOPNOTSUPP;
1686 #endif
1687 break;
1688 default:
1689 break;
1690 }
1691 }
1692 } else { /* level == IPPROTO_IPV6 */
1693 switch (op) {
1694 case SOPT_SET:
1695 switch (optname) {
1696 case IPV6_2292PKTOPTIONS:
1697 #ifdef IPV6_PKTOPTIONS
1698 case IPV6_PKTOPTIONS:
1699 #endif
1700 {
1701 struct mbuf *m;
1702
1703 if (optlen > IPV6_PKTOPTIONS_MBUF_LIMIT) {
1704 printf("ip6_ctloutput: mbuf limit hit\n");
1705 error = ENOBUFS;
1706 break;
1707 }
1708
1709 error = soopt_getm(sopt, &m); /* XXX */
1710 if (error != 0)
1711 break;
1712 error = soopt_mcopyin(sopt, m); /* XXX */
1713 if (error != 0)
1714 break;
1715 INP_WLOCK(inp);
1716 error = ip6_pcbopts(&inp->in6p_outputopts, m,
1717 so, sopt);
1718 INP_WUNLOCK(inp);
1719 m_freem(m); /* XXX */
1720 break;
1721 }
1722
1723 /*
1724 * Use of some Hop-by-Hop options or some
1725 * Destination options, might require special
1726 * privilege. That is, normal applications
1727 * (without special privilege) might be forbidden
1728 * from setting certain options in outgoing packets,
1729 * and might never see certain options in received
1730 * packets. [RFC 2292 Section 6]
1731 * KAME specific note:
1732 * KAME prevents non-privileged users from sending or
1733 * receiving ANY hbh/dst options in order to avoid
1734 * overhead of parsing options in the kernel.
1735 */
1736 case IPV6_RECVHOPOPTS:
1737 case IPV6_RECVDSTOPTS:
1738 case IPV6_RECVRTHDRDSTOPTS:
1739 if (td != NULL) {
1740 error = priv_check(td,
1741 PRIV_NETINET_SETHDROPTS);
1742 if (error)
1743 break;
1744 }
1745 /* FALLTHROUGH */
1746 case IPV6_UNICAST_HOPS:
1747 case IPV6_HOPLIMIT:
1748
1749 case IPV6_RECVPKTINFO:
1750 case IPV6_RECVHOPLIMIT:
1751 case IPV6_RECVRTHDR:
1752 case IPV6_RECVPATHMTU:
1753 case IPV6_RECVTCLASS:
1754 case IPV6_RECVFLOWID:
1755 #ifdef RSS
1756 case IPV6_RECVRSSBUCKETID:
1757 #endif
1758 case IPV6_V6ONLY:
1759 case IPV6_AUTOFLOWLABEL:
1760 case IPV6_ORIGDSTADDR:
1761 case IPV6_BINDANY:
1762 case IPV6_VLAN_PCP:
1763 if (optname == IPV6_BINDANY && td != NULL) {
1764 error = priv_check(td,
1765 PRIV_NETINET_BINDANY);
1766 if (error)
1767 break;
1768 }
1769
1770 if (optlen != sizeof(int)) {
1771 error = EINVAL;
1772 break;
1773 }
1774 error = sooptcopyin(sopt, &optval,
1775 sizeof optval, sizeof optval);
1776 if (error)
1777 break;
1778 switch (optname) {
1779 case IPV6_UNICAST_HOPS:
1780 if (optval < -1 || optval >= 256)
1781 error = EINVAL;
1782 else {
1783 /* -1 = kernel default */
1784 inp->in6p_hops = optval;
1785 if ((inp->inp_vflag &
1786 INP_IPV4) != 0)
1787 inp->inp_ip_ttl = optval;
1788 }
1789 break;
1790 #define OPTSET(bit) \
1791 do { \
1792 INP_WLOCK(inp); \
1793 if (optval) \
1794 inp->inp_flags |= (bit); \
1795 else \
1796 inp->inp_flags &= ~(bit); \
1797 INP_WUNLOCK(inp); \
1798 } while (/*CONSTCOND*/ 0)
1799 #define OPTSET2292(bit) \
1800 do { \
1801 INP_WLOCK(inp); \
1802 inp->inp_flags |= IN6P_RFC2292; \
1803 if (optval) \
1804 inp->inp_flags |= (bit); \
1805 else \
1806 inp->inp_flags &= ~(bit); \
1807 INP_WUNLOCK(inp); \
1808 } while (/*CONSTCOND*/ 0)
1809 #define OPTBIT(bit) (inp->inp_flags & (bit) ? 1 : 0)
1810
1811 #define OPTSET2_N(bit, val) do { \
1812 if (val) \
1813 inp->inp_flags2 |= bit; \
1814 else \
1815 inp->inp_flags2 &= ~bit; \
1816 } while (0)
1817 #define OPTSET2(bit, val) do { \
1818 INP_WLOCK(inp); \
1819 OPTSET2_N(bit, val); \
1820 INP_WUNLOCK(inp); \
1821 } while (0)
1822 #define OPTBIT2(bit) (inp->inp_flags2 & (bit) ? 1 : 0)
1823 #define OPTSET2292_EXCLUSIVE(bit) \
1824 do { \
1825 INP_WLOCK(inp); \
1826 if (OPTBIT(IN6P_RFC2292)) { \
1827 error = EINVAL; \
1828 } else { \
1829 if (optval) \
1830 inp->inp_flags |= (bit); \
1831 else \
1832 inp->inp_flags &= ~(bit); \
1833 } \
1834 INP_WUNLOCK(inp); \
1835 } while (/*CONSTCOND*/ 0)
1836
1837 case IPV6_RECVPKTINFO:
1838 OPTSET2292_EXCLUSIVE(IN6P_PKTINFO);
1839 break;
1840
1841 case IPV6_HOPLIMIT:
1842 {
1843 struct ip6_pktopts **optp;
1844
1845 /* cannot mix with RFC2292 */
1846 if (OPTBIT(IN6P_RFC2292)) {
1847 error = EINVAL;
1848 break;
1849 }
1850 INP_WLOCK(inp);
1851 if (inp->inp_flags & INP_DROPPED) {
1852 INP_WUNLOCK(inp);
1853 return (ECONNRESET);
1854 }
1855 optp = &inp->in6p_outputopts;
1856 error = ip6_pcbopt(IPV6_HOPLIMIT,
1857 (u_char *)&optval, sizeof(optval),
1858 optp, (td != NULL) ? td->td_ucred :
1859 NULL, uproto);
1860 INP_WUNLOCK(inp);
1861 break;
1862 }
1863
1864 case IPV6_RECVHOPLIMIT:
1865 OPTSET2292_EXCLUSIVE(IN6P_HOPLIMIT);
1866 break;
1867
1868 case IPV6_RECVHOPOPTS:
1869 OPTSET2292_EXCLUSIVE(IN6P_HOPOPTS);
1870 break;
1871
1872 case IPV6_RECVDSTOPTS:
1873 OPTSET2292_EXCLUSIVE(IN6P_DSTOPTS);
1874 break;
1875
1876 case IPV6_RECVRTHDRDSTOPTS:
1877 OPTSET2292_EXCLUSIVE(IN6P_RTHDRDSTOPTS);
1878 break;
1879
1880 case IPV6_RECVRTHDR:
1881 OPTSET2292_EXCLUSIVE(IN6P_RTHDR);
1882 break;
1883
1884 case IPV6_RECVPATHMTU:
1885 /*
1886 * We ignore this option for TCP
1887 * sockets.
1888 * (RFC3542 leaves this case
1889 * unspecified.)
1890 */
1891 if (uproto != IPPROTO_TCP)
1892 OPTSET(IN6P_MTU);
1893 break;
1894
1895 case IPV6_RECVFLOWID:
1896 OPTSET2(INP_RECVFLOWID, optval);
1897 break;
1898
1899 #ifdef RSS
1900 case IPV6_RECVRSSBUCKETID:
1901 OPTSET2(INP_RECVRSSBUCKETID, optval);
1902 break;
1903 #endif
1904
1905 case IPV6_V6ONLY:
1906 INP_WLOCK(inp);
1907 if (inp->inp_lport ||
1908 !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1909 /*
1910 * The socket is already bound.
1911 */
1912 INP_WUNLOCK(inp);
1913 error = EINVAL;
1914 break;
1915 }
1916 if (optval) {
1917 inp->inp_flags |= IN6P_IPV6_V6ONLY;
1918 inp->inp_vflag &= ~INP_IPV4;
1919 } else {
1920 inp->inp_flags &= ~IN6P_IPV6_V6ONLY;
1921 inp->inp_vflag |= INP_IPV4;
1922 }
1923 INP_WUNLOCK(inp);
1924 break;
1925 case IPV6_RECVTCLASS:
1926 /* cannot mix with RFC2292 XXX */
1927 OPTSET2292_EXCLUSIVE(IN6P_TCLASS);
1928 break;
1929 case IPV6_AUTOFLOWLABEL:
1930 OPTSET(IN6P_AUTOFLOWLABEL);
1931 break;
1932
1933 case IPV6_ORIGDSTADDR:
1934 OPTSET2(INP_ORIGDSTADDR, optval);
1935 break;
1936 case IPV6_BINDANY:
1937 OPTSET(INP_BINDANY);
1938 break;
1939 case IPV6_VLAN_PCP:
1940 if ((optval >= -1) && (optval <=
1941 (INP_2PCP_MASK >> INP_2PCP_SHIFT))) {
1942 if (optval == -1) {
1943 INP_WLOCK(inp);
1944 inp->inp_flags2 &=
1945 ~(INP_2PCP_SET |
1946 INP_2PCP_MASK);
1947 INP_WUNLOCK(inp);
1948 } else {
1949 INP_WLOCK(inp);
1950 inp->inp_flags2 |=
1951 INP_2PCP_SET;
1952 inp->inp_flags2 &=
1953 ~INP_2PCP_MASK;
1954 inp->inp_flags2 |=
1955 optval <<
1956 INP_2PCP_SHIFT;
1957 INP_WUNLOCK(inp);
1958 }
1959 } else
1960 error = EINVAL;
1961 break;
1962 }
1963 break;
1964
1965 case IPV6_TCLASS:
1966 case IPV6_DONTFRAG:
1967 case IPV6_USE_MIN_MTU:
1968 case IPV6_PREFER_TEMPADDR:
1969 if (optlen != sizeof(optval)) {
1970 error = EINVAL;
1971 break;
1972 }
1973 error = sooptcopyin(sopt, &optval,
1974 sizeof optval, sizeof optval);
1975 if (error)
1976 break;
1977 {
1978 struct ip6_pktopts **optp;
1979 INP_WLOCK(inp);
1980 if (inp->inp_flags & INP_DROPPED) {
1981 INP_WUNLOCK(inp);
1982 return (ECONNRESET);
1983 }
1984 optp = &inp->in6p_outputopts;
1985 error = ip6_pcbopt(optname,
1986 (u_char *)&optval, sizeof(optval),
1987 optp, (td != NULL) ? td->td_ucred :
1988 NULL, uproto);
1989 INP_WUNLOCK(inp);
1990 break;
1991 }
1992
1993 case IPV6_2292PKTINFO:
1994 case IPV6_2292HOPLIMIT:
1995 case IPV6_2292HOPOPTS:
1996 case IPV6_2292DSTOPTS:
1997 case IPV6_2292RTHDR:
1998 /* RFC 2292 */
1999 if (optlen != sizeof(int)) {
2000 error = EINVAL;
2001 break;
2002 }
2003 error = sooptcopyin(sopt, &optval,
2004 sizeof optval, sizeof optval);
2005 if (error)
2006 break;
2007 switch (optname) {
2008 case IPV6_2292PKTINFO:
2009 OPTSET2292(IN6P_PKTINFO);
2010 break;
2011 case IPV6_2292HOPLIMIT:
2012 OPTSET2292(IN6P_HOPLIMIT);
2013 break;
2014 case IPV6_2292HOPOPTS:
2015 /*
2016 * Check super-user privilege.
2017 * See comments for IPV6_RECVHOPOPTS.
2018 */
2019 if (td != NULL) {
2020 error = priv_check(td,
2021 PRIV_NETINET_SETHDROPTS);
2022 if (error)
2023 return (error);
2024 }
2025 OPTSET2292(IN6P_HOPOPTS);
2026 break;
2027 case IPV6_2292DSTOPTS:
2028 if (td != NULL) {
2029 error = priv_check(td,
2030 PRIV_NETINET_SETHDROPTS);
2031 if (error)
2032 return (error);
2033 }
2034 OPTSET2292(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS); /* XXX */
2035 break;
2036 case IPV6_2292RTHDR:
2037 OPTSET2292(IN6P_RTHDR);
2038 break;
2039 }
2040 break;
2041 case IPV6_PKTINFO:
2042 case IPV6_HOPOPTS:
2043 case IPV6_RTHDR:
2044 case IPV6_DSTOPTS:
2045 case IPV6_RTHDRDSTOPTS:
2046 case IPV6_NEXTHOP:
2047 {
2048 /* new advanced API (RFC3542) */
2049 u_char *optbuf;
2050 u_char optbuf_storage[MCLBYTES];
2051 int optlen;
2052 struct ip6_pktopts **optp;
2053
2054 /* cannot mix with RFC2292 */
2055 if (OPTBIT(IN6P_RFC2292)) {
2056 error = EINVAL;
2057 break;
2058 }
2059
2060 /*
2061 * We only ensure valsize is not too large
2062 * here. Further validation will be done
2063 * later.
2064 */
2065 error = sooptcopyin(sopt, optbuf_storage,
2066 sizeof(optbuf_storage), 0);
2067 if (error)
2068 break;
2069 optlen = sopt->sopt_valsize;
2070 optbuf = optbuf_storage;
2071 INP_WLOCK(inp);
2072 if (inp->inp_flags & INP_DROPPED) {
2073 INP_WUNLOCK(inp);
2074 return (ECONNRESET);
2075 }
2076 optp = &inp->in6p_outputopts;
2077 error = ip6_pcbopt(optname, optbuf, optlen,
2078 optp, (td != NULL) ? td->td_ucred : NULL,
2079 uproto);
2080 INP_WUNLOCK(inp);
2081 break;
2082 }
2083 #undef OPTSET
2084
2085 case IPV6_MULTICAST_IF:
2086 case IPV6_MULTICAST_HOPS:
2087 case IPV6_MULTICAST_LOOP:
2088 case IPV6_JOIN_GROUP:
2089 case IPV6_LEAVE_GROUP:
2090 case IPV6_MSFILTER:
2091 case MCAST_BLOCK_SOURCE:
2092 case MCAST_UNBLOCK_SOURCE:
2093 case MCAST_JOIN_GROUP:
2094 case MCAST_LEAVE_GROUP:
2095 case MCAST_JOIN_SOURCE_GROUP:
2096 case MCAST_LEAVE_SOURCE_GROUP:
2097 error = ip6_setmoptions(inp, sopt);
2098 break;
2099
2100 case IPV6_PORTRANGE:
2101 error = sooptcopyin(sopt, &optval,
2102 sizeof optval, sizeof optval);
2103 if (error)
2104 break;
2105
2106 INP_WLOCK(inp);
2107 switch (optval) {
2108 case IPV6_PORTRANGE_DEFAULT:
2109 inp->inp_flags &= ~(INP_LOWPORT);
2110 inp->inp_flags &= ~(INP_HIGHPORT);
2111 break;
2112
2113 case IPV6_PORTRANGE_HIGH:
2114 inp->inp_flags &= ~(INP_LOWPORT);
2115 inp->inp_flags |= INP_HIGHPORT;
2116 break;
2117
2118 case IPV6_PORTRANGE_LOW:
2119 inp->inp_flags &= ~(INP_HIGHPORT);
2120 inp->inp_flags |= INP_LOWPORT;
2121 break;
2122
2123 default:
2124 error = EINVAL;
2125 break;
2126 }
2127 INP_WUNLOCK(inp);
2128 break;
2129
2130 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2131 case IPV6_IPSEC_POLICY:
2132 if (IPSEC_ENABLED(ipv6)) {
2133 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2134 break;
2135 }
2136 /* FALLTHROUGH */
2137 #endif /* IPSEC */
2138
2139 default:
2140 error = ENOPROTOOPT;
2141 break;
2142 }
2143 break;
2144
2145 case SOPT_GET:
2146 switch (optname) {
2147 case IPV6_2292PKTOPTIONS:
2148 #ifdef IPV6_PKTOPTIONS
2149 case IPV6_PKTOPTIONS:
2150 #endif
2151 /*
2152 * RFC3542 (effectively) deprecated the
2153 * semantics of the 2292-style pktoptions.
2154 * Since it was not reliable in nature (i.e.,
2155 * applications had to expect the lack of some
2156 * information after all), it would make sense
2157 * to simplify this part by always returning
2158 * empty data.
2159 */
2160 sopt->sopt_valsize = 0;
2161 break;
2162
2163 case IPV6_RECVHOPOPTS:
2164 case IPV6_RECVDSTOPTS:
2165 case IPV6_RECVRTHDRDSTOPTS:
2166 case IPV6_UNICAST_HOPS:
2167 case IPV6_RECVPKTINFO:
2168 case IPV6_RECVHOPLIMIT:
2169 case IPV6_RECVRTHDR:
2170 case IPV6_RECVPATHMTU:
2171
2172 case IPV6_V6ONLY:
2173 case IPV6_PORTRANGE:
2174 case IPV6_RECVTCLASS:
2175 case IPV6_AUTOFLOWLABEL:
2176 case IPV6_BINDANY:
2177 case IPV6_FLOWID:
2178 case IPV6_FLOWTYPE:
2179 case IPV6_RECVFLOWID:
2180 #ifdef RSS
2181 case IPV6_RSSBUCKETID:
2182 case IPV6_RECVRSSBUCKETID:
2183 #endif
2184 case IPV6_VLAN_PCP:
2185 switch (optname) {
2186 case IPV6_RECVHOPOPTS:
2187 optval = OPTBIT(IN6P_HOPOPTS);
2188 break;
2189
2190 case IPV6_RECVDSTOPTS:
2191 optval = OPTBIT(IN6P_DSTOPTS);
2192 break;
2193
2194 case IPV6_RECVRTHDRDSTOPTS:
2195 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2196 break;
2197
2198 case IPV6_UNICAST_HOPS:
2199 optval = inp->in6p_hops;
2200 break;
2201
2202 case IPV6_RECVPKTINFO:
2203 optval = OPTBIT(IN6P_PKTINFO);
2204 break;
2205
2206 case IPV6_RECVHOPLIMIT:
2207 optval = OPTBIT(IN6P_HOPLIMIT);
2208 break;
2209
2210 case IPV6_RECVRTHDR:
2211 optval = OPTBIT(IN6P_RTHDR);
2212 break;
2213
2214 case IPV6_RECVPATHMTU:
2215 optval = OPTBIT(IN6P_MTU);
2216 break;
2217
2218 case IPV6_V6ONLY:
2219 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2220 break;
2221
2222 case IPV6_PORTRANGE:
2223 {
2224 int flags;
2225 flags = inp->inp_flags;
2226 if (flags & INP_HIGHPORT)
2227 optval = IPV6_PORTRANGE_HIGH;
2228 else if (flags & INP_LOWPORT)
2229 optval = IPV6_PORTRANGE_LOW;
2230 else
2231 optval = 0;
2232 break;
2233 }
2234 case IPV6_RECVTCLASS:
2235 optval = OPTBIT(IN6P_TCLASS);
2236 break;
2237
2238 case IPV6_AUTOFLOWLABEL:
2239 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2240 break;
2241
2242 case IPV6_ORIGDSTADDR:
2243 optval = OPTBIT2(INP_ORIGDSTADDR);
2244 break;
2245
2246 case IPV6_BINDANY:
2247 optval = OPTBIT(INP_BINDANY);
2248 break;
2249
2250 case IPV6_FLOWID:
2251 optval = inp->inp_flowid;
2252 break;
2253
2254 case IPV6_FLOWTYPE:
2255 optval = inp->inp_flowtype;
2256 break;
2257
2258 case IPV6_RECVFLOWID:
2259 optval = OPTBIT2(INP_RECVFLOWID);
2260 break;
2261 #ifdef RSS
2262 case IPV6_RSSBUCKETID:
2263 retval =
2264 rss_hash2bucket(inp->inp_flowid,
2265 inp->inp_flowtype,
2266 &rss_bucket);
2267 if (retval == 0)
2268 optval = rss_bucket;
2269 else
2270 error = EINVAL;
2271 break;
2272
2273 case IPV6_RECVRSSBUCKETID:
2274 optval = OPTBIT2(INP_RECVRSSBUCKETID);
2275 break;
2276 #endif
2277
2278
2279 case IPV6_VLAN_PCP:
2280 if (OPTBIT2(INP_2PCP_SET)) {
2281 optval = (inp->inp_flags2 &
2282 INP_2PCP_MASK) >>
2283 INP_2PCP_SHIFT;
2284 } else {
2285 optval = -1;
2286 }
2287 break;
2288 }
2289
2290 if (error)
2291 break;
2292 error = sooptcopyout(sopt, &optval,
2293 sizeof optval);
2294 break;
2295
2296 case IPV6_PATHMTU:
2297 {
2298 u_long pmtu = 0;
2299 struct ip6_mtuinfo mtuinfo;
2300 struct in6_addr addr;
2301
2302 if (!(so->so_state & SS_ISCONNECTED))
2303 return (ENOTCONN);
2304 /*
2305 * XXX: we dot not consider the case of source
2306 * routing, or optional information to specify
2307 * the outgoing interface.
2308 * Copy faddr out of inp to avoid holding lock
2309 * on inp during route lookup.
2310 */
2311 INP_RLOCK(inp);
2312 bcopy(&inp->in6p_faddr, &addr, sizeof(addr));
2313 INP_RUNLOCK(inp);
2314 error = ip6_getpmtu_ctl(so->so_fibnum,
2315 &addr, &pmtu);
2316 if (error)
2317 break;
2318 if (pmtu > IPV6_MAXPACKET)
2319 pmtu = IPV6_MAXPACKET;
2320
2321 bzero(&mtuinfo, sizeof(mtuinfo));
2322 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2323 optdata = (void *)&mtuinfo;
2324 optdatalen = sizeof(mtuinfo);
2325 error = sooptcopyout(sopt, optdata,
2326 optdatalen);
2327 break;
2328 }
2329
2330 case IPV6_2292PKTINFO:
2331 case IPV6_2292HOPLIMIT:
2332 case IPV6_2292HOPOPTS:
2333 case IPV6_2292RTHDR:
2334 case IPV6_2292DSTOPTS:
2335 switch (optname) {
2336 case IPV6_2292PKTINFO:
2337 optval = OPTBIT(IN6P_PKTINFO);
2338 break;
2339 case IPV6_2292HOPLIMIT:
2340 optval = OPTBIT(IN6P_HOPLIMIT);
2341 break;
2342 case IPV6_2292HOPOPTS:
2343 optval = OPTBIT(IN6P_HOPOPTS);
2344 break;
2345 case IPV6_2292RTHDR:
2346 optval = OPTBIT(IN6P_RTHDR);
2347 break;
2348 case IPV6_2292DSTOPTS:
2349 optval = OPTBIT(IN6P_DSTOPTS|IN6P_RTHDRDSTOPTS);
2350 break;
2351 }
2352 error = sooptcopyout(sopt, &optval,
2353 sizeof optval);
2354 break;
2355 case IPV6_PKTINFO:
2356 case IPV6_HOPOPTS:
2357 case IPV6_RTHDR:
2358 case IPV6_DSTOPTS:
2359 case IPV6_RTHDRDSTOPTS:
2360 case IPV6_NEXTHOP:
2361 case IPV6_TCLASS:
2362 case IPV6_DONTFRAG:
2363 case IPV6_USE_MIN_MTU:
2364 case IPV6_PREFER_TEMPADDR:
2365 error = ip6_getpcbopt(inp, optname, sopt);
2366 break;
2367
2368 case IPV6_MULTICAST_IF:
2369 case IPV6_MULTICAST_HOPS:
2370 case IPV6_MULTICAST_LOOP:
2371 case IPV6_MSFILTER:
2372 error = ip6_getmoptions(inp, sopt);
2373 break;
2374
2375 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
2376 case IPV6_IPSEC_POLICY:
2377 if (IPSEC_ENABLED(ipv6)) {
2378 error = IPSEC_PCBCTL(ipv6, inp, sopt);
2379 break;
2380 }
2381 /* FALLTHROUGH */
2382 #endif /* IPSEC */
2383 default:
2384 error = ENOPROTOOPT;
2385 break;
2386 }
2387 break;
2388 }
2389 }
2390 return (error);
2391 }
2392
2393 int
ip6_raw_ctloutput(struct socket * so,struct sockopt * sopt)2394 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2395 {
2396 int error = 0, optval, optlen;
2397 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2398 struct inpcb *inp = sotoinpcb(so);
2399 int level, op, optname;
2400
2401 level = sopt->sopt_level;
2402 op = sopt->sopt_dir;
2403 optname = sopt->sopt_name;
2404 optlen = sopt->sopt_valsize;
2405
2406 if (level != IPPROTO_IPV6) {
2407 return (EINVAL);
2408 }
2409
2410 switch (optname) {
2411 case IPV6_CHECKSUM:
2412 /*
2413 * For ICMPv6 sockets, no modification allowed for checksum
2414 * offset, permit "no change" values to help existing apps.
2415 *
2416 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2417 * for an ICMPv6 socket will fail."
2418 * The current behavior does not meet RFC3542.
2419 */
2420 switch (op) {
2421 case SOPT_SET:
2422 if (optlen != sizeof(int)) {
2423 error = EINVAL;
2424 break;
2425 }
2426 error = sooptcopyin(sopt, &optval, sizeof(optval),
2427 sizeof(optval));
2428 if (error)
2429 break;
2430 if (optval < -1 || (optval % 2) != 0) {
2431 /*
2432 * The API assumes non-negative even offset
2433 * values or -1 as a special value.
2434 */
2435 error = EINVAL;
2436 } else if (inp->inp_ip_p == IPPROTO_ICMPV6) {
2437 if (optval != icmp6off)
2438 error = EINVAL;
2439 } else
2440 inp->in6p_cksum = optval;
2441 break;
2442
2443 case SOPT_GET:
2444 if (inp->inp_ip_p == IPPROTO_ICMPV6)
2445 optval = icmp6off;
2446 else
2447 optval = inp->in6p_cksum;
2448
2449 error = sooptcopyout(sopt, &optval, sizeof(optval));
2450 break;
2451
2452 default:
2453 error = EINVAL;
2454 break;
2455 }
2456 break;
2457
2458 default:
2459 error = ENOPROTOOPT;
2460 break;
2461 }
2462
2463 return (error);
2464 }
2465
2466 /*
2467 * Set up IP6 options in pcb for insertion in output packets or
2468 * specifying behavior of outgoing packets.
2469 */
2470 static int
ip6_pcbopts(struct ip6_pktopts ** pktopt,struct mbuf * m,struct socket * so,struct sockopt * sopt)2471 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m,
2472 struct socket *so, struct sockopt *sopt)
2473 {
2474 struct ip6_pktopts *opt = *pktopt;
2475 int error = 0;
2476 struct thread *td = sopt->sopt_td;
2477 struct epoch_tracker et;
2478
2479 /* turn off any old options. */
2480 if (opt) {
2481 #ifdef DIAGNOSTIC
2482 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2483 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2484 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
2485 printf("ip6_pcbopts: all specified options are cleared.\n");
2486 #endif
2487 ip6_clearpktopts(opt, -1);
2488 } else {
2489 opt = malloc(sizeof(*opt), M_IP6OPT, M_NOWAIT);
2490 if (opt == NULL)
2491 return (ENOMEM);
2492 }
2493 *pktopt = NULL;
2494
2495 if (!m || m->m_len == 0) {
2496 /*
2497 * Only turning off any previous options, regardless of
2498 * whether the opt is just created or given.
2499 */
2500 free(opt, M_IP6OPT);
2501 return (0);
2502 }
2503
2504 /* set options specified by user. */
2505 NET_EPOCH_ENTER(et);
2506 if ((error = ip6_setpktopts(m, opt, NULL, (td != NULL) ?
2507 td->td_ucred : NULL, so->so_proto->pr_protocol)) != 0) {
2508 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2509 free(opt, M_IP6OPT);
2510 NET_EPOCH_EXIT(et);
2511 return (error);
2512 }
2513 NET_EPOCH_EXIT(et);
2514 *pktopt = opt;
2515 return (0);
2516 }
2517
2518 /*
2519 * initialize ip6_pktopts. beware that there are non-zero default values in
2520 * the struct.
2521 */
2522 void
ip6_initpktopts(struct ip6_pktopts * opt)2523 ip6_initpktopts(struct ip6_pktopts *opt)
2524 {
2525
2526 bzero(opt, sizeof(*opt));
2527 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2528 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2529 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2530 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2531 }
2532
2533 static int
ip6_pcbopt(int optname,u_char * buf,int len,struct ip6_pktopts ** pktopt,struct ucred * cred,int uproto)2534 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2535 struct ucred *cred, int uproto)
2536 {
2537 struct epoch_tracker et;
2538 struct ip6_pktopts *opt;
2539 int ret;
2540
2541 if (*pktopt == NULL) {
2542 *pktopt = malloc(sizeof(struct ip6_pktopts), M_IP6OPT,
2543 M_NOWAIT);
2544 if (*pktopt == NULL)
2545 return (ENOBUFS);
2546 ip6_initpktopts(*pktopt);
2547 }
2548 opt = *pktopt;
2549
2550 NET_EPOCH_ENTER(et);
2551 ret = ip6_setpktopt(optname, buf, len, opt, cred, 1, 0, uproto);
2552 NET_EPOCH_EXIT(et);
2553
2554 return (ret);
2555 }
2556
2557 #define GET_PKTOPT_VAR(field, lenexpr) do { \
2558 if (pktopt && pktopt->field) { \
2559 INP_RUNLOCK(inp); \
2560 optdata = malloc(sopt->sopt_valsize, M_TEMP, M_WAITOK); \
2561 malloc_optdata = true; \
2562 INP_RLOCK(inp); \
2563 if (inp->inp_flags & INP_DROPPED) { \
2564 INP_RUNLOCK(inp); \
2565 free(optdata, M_TEMP); \
2566 return (ECONNRESET); \
2567 } \
2568 pktopt = inp->in6p_outputopts; \
2569 if (pktopt && pktopt->field) { \
2570 optdatalen = min(lenexpr, sopt->sopt_valsize); \
2571 bcopy(pktopt->field, optdata, optdatalen); \
2572 } else { \
2573 free(optdata, M_TEMP); \
2574 optdata = NULL; \
2575 malloc_optdata = false; \
2576 } \
2577 } \
2578 } while(0)
2579
2580 #define GET_PKTOPT_EXT_HDR(field) GET_PKTOPT_VAR(field, \
2581 (((struct ip6_ext *)pktopt->field)->ip6e_len + 1) << 3)
2582
2583 #define GET_PKTOPT_SOCKADDR(field) GET_PKTOPT_VAR(field, \
2584 pktopt->field->sa_len)
2585
2586 static int
ip6_getpcbopt(struct inpcb * inp,int optname,struct sockopt * sopt)2587 ip6_getpcbopt(struct inpcb *inp, int optname, struct sockopt *sopt)
2588 {
2589 void *optdata = NULL;
2590 bool malloc_optdata = false;
2591 int optdatalen = 0;
2592 int error = 0;
2593 struct in6_pktinfo null_pktinfo;
2594 int deftclass = 0, on;
2595 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2596 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2597 struct ip6_pktopts *pktopt;
2598
2599 INP_RLOCK(inp);
2600 pktopt = inp->in6p_outputopts;
2601
2602 switch (optname) {
2603 case IPV6_PKTINFO:
2604 optdata = (void *)&null_pktinfo;
2605 if (pktopt && pktopt->ip6po_pktinfo) {
2606 bcopy(pktopt->ip6po_pktinfo, &null_pktinfo,
2607 sizeof(null_pktinfo));
2608 in6_clearscope(&null_pktinfo.ipi6_addr);
2609 } else {
2610 /* XXX: we don't have to do this every time... */
2611 bzero(&null_pktinfo, sizeof(null_pktinfo));
2612 }
2613 optdatalen = sizeof(struct in6_pktinfo);
2614 break;
2615 case IPV6_TCLASS:
2616 if (pktopt && pktopt->ip6po_tclass >= 0)
2617 deftclass = pktopt->ip6po_tclass;
2618 optdata = (void *)&deftclass;
2619 optdatalen = sizeof(int);
2620 break;
2621 case IPV6_HOPOPTS:
2622 GET_PKTOPT_EXT_HDR(ip6po_hbh);
2623 break;
2624 case IPV6_RTHDR:
2625 GET_PKTOPT_EXT_HDR(ip6po_rthdr);
2626 break;
2627 case IPV6_RTHDRDSTOPTS:
2628 GET_PKTOPT_EXT_HDR(ip6po_dest1);
2629 break;
2630 case IPV6_DSTOPTS:
2631 GET_PKTOPT_EXT_HDR(ip6po_dest2);
2632 break;
2633 case IPV6_NEXTHOP:
2634 GET_PKTOPT_SOCKADDR(ip6po_nexthop);
2635 break;
2636 case IPV6_USE_MIN_MTU:
2637 if (pktopt)
2638 defminmtu = pktopt->ip6po_minmtu;
2639 optdata = (void *)&defminmtu;
2640 optdatalen = sizeof(int);
2641 break;
2642 case IPV6_DONTFRAG:
2643 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2644 on = 1;
2645 else
2646 on = 0;
2647 optdata = (void *)&on;
2648 optdatalen = sizeof(on);
2649 break;
2650 case IPV6_PREFER_TEMPADDR:
2651 if (pktopt)
2652 defpreftemp = pktopt->ip6po_prefer_tempaddr;
2653 optdata = (void *)&defpreftemp;
2654 optdatalen = sizeof(int);
2655 break;
2656 default: /* should not happen */
2657 #ifdef DIAGNOSTIC
2658 panic("ip6_getpcbopt: unexpected option\n");
2659 #endif
2660 INP_RUNLOCK(inp);
2661 return (ENOPROTOOPT);
2662 }
2663 INP_RUNLOCK(inp);
2664
2665 error = sooptcopyout(sopt, optdata, optdatalen);
2666 if (malloc_optdata)
2667 free(optdata, M_TEMP);
2668
2669 return (error);
2670 }
2671
2672 void
ip6_clearpktopts(struct ip6_pktopts * pktopt,int optname)2673 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2674 {
2675 if (pktopt == NULL)
2676 return;
2677
2678 if (optname == -1 || optname == IPV6_PKTINFO) {
2679 if (pktopt->ip6po_pktinfo)
2680 free(pktopt->ip6po_pktinfo, M_IP6OPT);
2681 pktopt->ip6po_pktinfo = NULL;
2682 }
2683 if (optname == -1 || optname == IPV6_HOPLIMIT)
2684 pktopt->ip6po_hlim = -1;
2685 if (optname == -1 || optname == IPV6_TCLASS)
2686 pktopt->ip6po_tclass = -1;
2687 if (optname == -1 || optname == IPV6_NEXTHOP) {
2688 if (pktopt->ip6po_nextroute.ro_nh) {
2689 NH_FREE(pktopt->ip6po_nextroute.ro_nh);
2690 pktopt->ip6po_nextroute.ro_nh = NULL;
2691 }
2692 if (pktopt->ip6po_nexthop)
2693 free(pktopt->ip6po_nexthop, M_IP6OPT);
2694 pktopt->ip6po_nexthop = NULL;
2695 }
2696 if (optname == -1 || optname == IPV6_HOPOPTS) {
2697 if (pktopt->ip6po_hbh)
2698 free(pktopt->ip6po_hbh, M_IP6OPT);
2699 pktopt->ip6po_hbh = NULL;
2700 }
2701 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
2702 if (pktopt->ip6po_dest1)
2703 free(pktopt->ip6po_dest1, M_IP6OPT);
2704 pktopt->ip6po_dest1 = NULL;
2705 }
2706 if (optname == -1 || optname == IPV6_RTHDR) {
2707 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
2708 free(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
2709 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
2710 if (pktopt->ip6po_route.ro_nh) {
2711 NH_FREE(pktopt->ip6po_route.ro_nh);
2712 pktopt->ip6po_route.ro_nh = NULL;
2713 }
2714 }
2715 if (optname == -1 || optname == IPV6_DSTOPTS) {
2716 if (pktopt->ip6po_dest2)
2717 free(pktopt->ip6po_dest2, M_IP6OPT);
2718 pktopt->ip6po_dest2 = NULL;
2719 }
2720 }
2721
2722 #define PKTOPT_EXTHDRCPY(type) \
2723 do {\
2724 if (src->type) {\
2725 int hlen = (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3;\
2726 dst->type = malloc(hlen, M_IP6OPT, canwait);\
2727 if (dst->type == NULL)\
2728 goto bad;\
2729 bcopy(src->type, dst->type, hlen);\
2730 }\
2731 } while (/*CONSTCOND*/ 0)
2732
2733 static int
copypktopts(struct ip6_pktopts * dst,struct ip6_pktopts * src,int canwait)2734 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
2735 {
2736 if (dst == NULL || src == NULL) {
2737 printf("ip6_clearpktopts: invalid argument\n");
2738 return (EINVAL);
2739 }
2740
2741 dst->ip6po_hlim = src->ip6po_hlim;
2742 dst->ip6po_tclass = src->ip6po_tclass;
2743 dst->ip6po_flags = src->ip6po_flags;
2744 dst->ip6po_minmtu = src->ip6po_minmtu;
2745 dst->ip6po_prefer_tempaddr = src->ip6po_prefer_tempaddr;
2746 if (src->ip6po_pktinfo) {
2747 dst->ip6po_pktinfo = malloc(sizeof(*dst->ip6po_pktinfo),
2748 M_IP6OPT, canwait);
2749 if (dst->ip6po_pktinfo == NULL)
2750 goto bad;
2751 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
2752 }
2753 if (src->ip6po_nexthop) {
2754 dst->ip6po_nexthop = malloc(src->ip6po_nexthop->sa_len,
2755 M_IP6OPT, canwait);
2756 if (dst->ip6po_nexthop == NULL)
2757 goto bad;
2758 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
2759 src->ip6po_nexthop->sa_len);
2760 }
2761 PKTOPT_EXTHDRCPY(ip6po_hbh);
2762 PKTOPT_EXTHDRCPY(ip6po_dest1);
2763 PKTOPT_EXTHDRCPY(ip6po_dest2);
2764 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
2765 return (0);
2766
2767 bad:
2768 ip6_clearpktopts(dst, -1);
2769 return (ENOBUFS);
2770 }
2771 #undef PKTOPT_EXTHDRCPY
2772
2773 struct ip6_pktopts *
ip6_copypktopts(struct ip6_pktopts * src,int canwait)2774 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
2775 {
2776 int error;
2777 struct ip6_pktopts *dst;
2778
2779 dst = malloc(sizeof(*dst), M_IP6OPT, canwait);
2780 if (dst == NULL)
2781 return (NULL);
2782 ip6_initpktopts(dst);
2783
2784 if ((error = copypktopts(dst, src, canwait)) != 0) {
2785 free(dst, M_IP6OPT);
2786 return (NULL);
2787 }
2788
2789 return (dst);
2790 }
2791
2792 void
ip6_freepcbopts(struct ip6_pktopts * pktopt)2793 ip6_freepcbopts(struct ip6_pktopts *pktopt)
2794 {
2795 if (pktopt == NULL)
2796 return;
2797
2798 ip6_clearpktopts(pktopt, -1);
2799
2800 free(pktopt, M_IP6OPT);
2801 }
2802
2803 /*
2804 * Set IPv6 outgoing packet options based on advanced API.
2805 */
2806 int
ip6_setpktopts(struct mbuf * control,struct ip6_pktopts * opt,struct ip6_pktopts * stickyopt,struct ucred * cred,int uproto)2807 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
2808 struct ip6_pktopts *stickyopt, struct ucred *cred, int uproto)
2809 {
2810 struct cmsghdr *cm = NULL;
2811
2812 if (control == NULL || opt == NULL)
2813 return (EINVAL);
2814
2815 /*
2816 * ip6_setpktopt can call ifnet_byindex(), so it's imperative that we
2817 * are in the network epoch here.
2818 */
2819 NET_EPOCH_ASSERT();
2820
2821 ip6_initpktopts(opt);
2822 if (stickyopt) {
2823 int error;
2824
2825 /*
2826 * If stickyopt is provided, make a local copy of the options
2827 * for this particular packet, then override them by ancillary
2828 * objects.
2829 * XXX: copypktopts() does not copy the cached route to a next
2830 * hop (if any). This is not very good in terms of efficiency,
2831 * but we can allow this since this option should be rarely
2832 * used.
2833 */
2834 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
2835 return (error);
2836 }
2837
2838 /*
2839 * XXX: Currently, we assume all the optional information is stored
2840 * in a single mbuf.
2841 */
2842 if (control->m_next)
2843 return (EINVAL);
2844
2845 for (; control->m_len > 0; control->m_data += CMSG_ALIGN(cm->cmsg_len),
2846 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
2847 int error;
2848
2849 if (control->m_len < CMSG_LEN(0))
2850 return (EINVAL);
2851
2852 cm = mtod(control, struct cmsghdr *);
2853 if (cm->cmsg_len == 0 || cm->cmsg_len > control->m_len)
2854 return (EINVAL);
2855 if (cm->cmsg_level != IPPROTO_IPV6)
2856 continue;
2857
2858 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
2859 cm->cmsg_len - CMSG_LEN(0), opt, cred, 0, 1, uproto);
2860 if (error)
2861 return (error);
2862 }
2863
2864 return (0);
2865 }
2866
2867 /*
2868 * Set a particular packet option, as a sticky option or an ancillary data
2869 * item. "len" can be 0 only when it's a sticky option.
2870 * We have 4 cases of combination of "sticky" and "cmsg":
2871 * "sticky=0, cmsg=0": impossible
2872 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
2873 * "sticky=1, cmsg=0": RFC3542 socket option
2874 * "sticky=1, cmsg=1": RFC2292 socket option
2875 */
2876 static int
ip6_setpktopt(int optname,u_char * buf,int len,struct ip6_pktopts * opt,struct ucred * cred,int sticky,int cmsg,int uproto)2877 ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
2878 struct ucred *cred, int sticky, int cmsg, int uproto)
2879 {
2880 int minmtupolicy, preftemp;
2881 int error;
2882
2883 NET_EPOCH_ASSERT();
2884
2885 if (!sticky && !cmsg) {
2886 #ifdef DIAGNOSTIC
2887 printf("ip6_setpktopt: impossible case\n");
2888 #endif
2889 return (EINVAL);
2890 }
2891
2892 /*
2893 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
2894 * not be specified in the context of RFC3542. Conversely,
2895 * RFC3542 types should not be specified in the context of RFC2292.
2896 */
2897 if (!cmsg) {
2898 switch (optname) {
2899 case IPV6_2292PKTINFO:
2900 case IPV6_2292HOPLIMIT:
2901 case IPV6_2292NEXTHOP:
2902 case IPV6_2292HOPOPTS:
2903 case IPV6_2292DSTOPTS:
2904 case IPV6_2292RTHDR:
2905 case IPV6_2292PKTOPTIONS:
2906 return (ENOPROTOOPT);
2907 }
2908 }
2909 if (sticky && cmsg) {
2910 switch (optname) {
2911 case IPV6_PKTINFO:
2912 case IPV6_HOPLIMIT:
2913 case IPV6_NEXTHOP:
2914 case IPV6_HOPOPTS:
2915 case IPV6_DSTOPTS:
2916 case IPV6_RTHDRDSTOPTS:
2917 case IPV6_RTHDR:
2918 case IPV6_USE_MIN_MTU:
2919 case IPV6_DONTFRAG:
2920 case IPV6_TCLASS:
2921 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
2922 return (ENOPROTOOPT);
2923 }
2924 }
2925
2926 switch (optname) {
2927 case IPV6_2292PKTINFO:
2928 case IPV6_PKTINFO:
2929 {
2930 struct ifnet *ifp = NULL;
2931 struct in6_pktinfo *pktinfo;
2932
2933 if (len != sizeof(struct in6_pktinfo))
2934 return (EINVAL);
2935
2936 pktinfo = (struct in6_pktinfo *)buf;
2937
2938 /*
2939 * An application can clear any sticky IPV6_PKTINFO option by
2940 * doing a "regular" setsockopt with ipi6_addr being
2941 * in6addr_any and ipi6_ifindex being zero.
2942 * [RFC 3542, Section 6]
2943 */
2944 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
2945 pktinfo->ipi6_ifindex == 0 &&
2946 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2947 ip6_clearpktopts(opt, optname);
2948 break;
2949 }
2950
2951 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
2952 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2953 return (EINVAL);
2954 }
2955 if (IN6_IS_ADDR_MULTICAST(&pktinfo->ipi6_addr))
2956 return (EINVAL);
2957 /* validate the interface index if specified. */
2958 if (pktinfo->ipi6_ifindex) {
2959 ifp = ifnet_byindex(pktinfo->ipi6_ifindex);
2960 if (ifp == NULL)
2961 return (ENXIO);
2962 }
2963 if (ifp != NULL && (ifp->if_afdata[AF_INET6] == NULL ||
2964 (ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) != 0))
2965 return (ENETDOWN);
2966
2967 if (ifp != NULL &&
2968 !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
2969 struct in6_ifaddr *ia;
2970
2971 in6_setscope(&pktinfo->ipi6_addr, ifp, NULL);
2972 ia = in6ifa_ifpwithaddr(ifp, &pktinfo->ipi6_addr);
2973 if (ia == NULL)
2974 return (EADDRNOTAVAIL);
2975 ifa_free(&ia->ia_ifa);
2976 }
2977 /*
2978 * We store the address anyway, and let in6_selectsrc()
2979 * validate the specified address. This is because ipi6_addr
2980 * may not have enough information about its scope zone, and
2981 * we may need additional information (such as outgoing
2982 * interface or the scope zone of a destination address) to
2983 * disambiguate the scope.
2984 * XXX: the delay of the validation may confuse the
2985 * application when it is used as a sticky option.
2986 */
2987 if (opt->ip6po_pktinfo == NULL) {
2988 opt->ip6po_pktinfo = malloc(sizeof(*pktinfo),
2989 M_IP6OPT, M_NOWAIT);
2990 if (opt->ip6po_pktinfo == NULL)
2991 return (ENOBUFS);
2992 }
2993 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof(*pktinfo));
2994 break;
2995 }
2996
2997 case IPV6_2292HOPLIMIT:
2998 case IPV6_HOPLIMIT:
2999 {
3000 int *hlimp;
3001
3002 /*
3003 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3004 * to simplify the ordering among hoplimit options.
3005 */
3006 if (optname == IPV6_HOPLIMIT && sticky)
3007 return (ENOPROTOOPT);
3008
3009 if (len != sizeof(int))
3010 return (EINVAL);
3011 hlimp = (int *)buf;
3012 if (*hlimp < -1 || *hlimp > 255)
3013 return (EINVAL);
3014
3015 opt->ip6po_hlim = *hlimp;
3016 break;
3017 }
3018
3019 case IPV6_TCLASS:
3020 {
3021 int tclass;
3022
3023 if (len != sizeof(int))
3024 return (EINVAL);
3025 tclass = *(int *)buf;
3026 if (tclass < -1 || tclass > 255)
3027 return (EINVAL);
3028
3029 opt->ip6po_tclass = tclass;
3030 break;
3031 }
3032
3033 case IPV6_2292NEXTHOP:
3034 case IPV6_NEXTHOP:
3035 if (cred != NULL) {
3036 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3037 if (error)
3038 return (error);
3039 }
3040
3041 if (len == 0) { /* just remove the option */
3042 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3043 break;
3044 }
3045
3046 /* check if cmsg_len is large enough for sa_len */
3047 if (len < sizeof(struct sockaddr) || len < *buf)
3048 return (EINVAL);
3049
3050 switch (((struct sockaddr *)buf)->sa_family) {
3051 case AF_INET6:
3052 {
3053 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)buf;
3054 int error;
3055
3056 if (sa6->sin6_len != sizeof(struct sockaddr_in6))
3057 return (EINVAL);
3058
3059 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3060 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3061 return (EINVAL);
3062 }
3063 if ((error = sa6_embedscope(sa6, V_ip6_use_defzone))
3064 != 0) {
3065 return (error);
3066 }
3067 break;
3068 }
3069 case AF_LINK: /* should eventually be supported */
3070 default:
3071 return (EAFNOSUPPORT);
3072 }
3073
3074 /* turn off the previous option, then set the new option. */
3075 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3076 opt->ip6po_nexthop = malloc(*buf, M_IP6OPT, M_NOWAIT);
3077 if (opt->ip6po_nexthop == NULL)
3078 return (ENOBUFS);
3079 bcopy(buf, opt->ip6po_nexthop, *buf);
3080 break;
3081
3082 case IPV6_2292HOPOPTS:
3083 case IPV6_HOPOPTS:
3084 {
3085 struct ip6_hbh *hbh;
3086 int hbhlen;
3087
3088 /*
3089 * XXX: We don't allow a non-privileged user to set ANY HbH
3090 * options, since per-option restriction has too much
3091 * overhead.
3092 */
3093 if (cred != NULL) {
3094 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3095 if (error)
3096 return (error);
3097 }
3098
3099 if (len == 0) {
3100 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3101 break; /* just remove the option */
3102 }
3103
3104 /* message length validation */
3105 if (len < sizeof(struct ip6_hbh))
3106 return (EINVAL);
3107 hbh = (struct ip6_hbh *)buf;
3108 hbhlen = (hbh->ip6h_len + 1) << 3;
3109 if (len != hbhlen)
3110 return (EINVAL);
3111
3112 /* turn off the previous option, then set the new option. */
3113 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3114 opt->ip6po_hbh = malloc(hbhlen, M_IP6OPT, M_NOWAIT);
3115 if (opt->ip6po_hbh == NULL)
3116 return (ENOBUFS);
3117 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3118
3119 break;
3120 }
3121
3122 case IPV6_2292DSTOPTS:
3123 case IPV6_DSTOPTS:
3124 case IPV6_RTHDRDSTOPTS:
3125 {
3126 struct ip6_dest *dest, **newdest = NULL;
3127 int destlen;
3128
3129 if (cred != NULL) { /* XXX: see the comment for IPV6_HOPOPTS */
3130 error = priv_check_cred(cred, PRIV_NETINET_SETHDROPTS);
3131 if (error)
3132 return (error);
3133 }
3134
3135 if (len == 0) {
3136 ip6_clearpktopts(opt, optname);
3137 break; /* just remove the option */
3138 }
3139
3140 /* message length validation */
3141 if (len < sizeof(struct ip6_dest))
3142 return (EINVAL);
3143 dest = (struct ip6_dest *)buf;
3144 destlen = (dest->ip6d_len + 1) << 3;
3145 if (len != destlen)
3146 return (EINVAL);
3147
3148 /*
3149 * Determine the position that the destination options header
3150 * should be inserted; before or after the routing header.
3151 */
3152 switch (optname) {
3153 case IPV6_2292DSTOPTS:
3154 /*
3155 * The old advacned API is ambiguous on this point.
3156 * Our approach is to determine the position based
3157 * according to the existence of a routing header.
3158 * Note, however, that this depends on the order of the
3159 * extension headers in the ancillary data; the 1st
3160 * part of the destination options header must appear
3161 * before the routing header in the ancillary data,
3162 * too.
3163 * RFC3542 solved the ambiguity by introducing
3164 * separate ancillary data or option types.
3165 */
3166 if (opt->ip6po_rthdr == NULL)
3167 newdest = &opt->ip6po_dest1;
3168 else
3169 newdest = &opt->ip6po_dest2;
3170 break;
3171 case IPV6_RTHDRDSTOPTS:
3172 newdest = &opt->ip6po_dest1;
3173 break;
3174 case IPV6_DSTOPTS:
3175 newdest = &opt->ip6po_dest2;
3176 break;
3177 }
3178
3179 /* turn off the previous option, then set the new option. */
3180 ip6_clearpktopts(opt, optname);
3181 *newdest = malloc(destlen, M_IP6OPT, M_NOWAIT);
3182 if (*newdest == NULL)
3183 return (ENOBUFS);
3184 bcopy(dest, *newdest, destlen);
3185
3186 break;
3187 }
3188
3189 case IPV6_2292RTHDR:
3190 case IPV6_RTHDR:
3191 {
3192 struct ip6_rthdr *rth;
3193 int rthlen;
3194
3195 if (len == 0) {
3196 ip6_clearpktopts(opt, IPV6_RTHDR);
3197 break; /* just remove the option */
3198 }
3199
3200 /* message length validation */
3201 if (len < sizeof(struct ip6_rthdr))
3202 return (EINVAL);
3203 rth = (struct ip6_rthdr *)buf;
3204 rthlen = (rth->ip6r_len + 1) << 3;
3205 if (len != rthlen)
3206 return (EINVAL);
3207
3208 switch (rth->ip6r_type) {
3209 case IPV6_RTHDR_TYPE_0:
3210 if (rth->ip6r_len == 0) /* must contain one addr */
3211 return (EINVAL);
3212 if (rth->ip6r_len % 2) /* length must be even */
3213 return (EINVAL);
3214 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3215 return (EINVAL);
3216 break;
3217 default:
3218 return (EINVAL); /* not supported */
3219 }
3220
3221 /* turn off the previous option */
3222 ip6_clearpktopts(opt, IPV6_RTHDR);
3223 opt->ip6po_rthdr = malloc(rthlen, M_IP6OPT, M_NOWAIT);
3224 if (opt->ip6po_rthdr == NULL)
3225 return (ENOBUFS);
3226 bcopy(rth, opt->ip6po_rthdr, rthlen);
3227
3228 break;
3229 }
3230
3231 case IPV6_USE_MIN_MTU:
3232 if (len != sizeof(int))
3233 return (EINVAL);
3234 minmtupolicy = *(int *)buf;
3235 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3236 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3237 minmtupolicy != IP6PO_MINMTU_ALL) {
3238 return (EINVAL);
3239 }
3240 opt->ip6po_minmtu = minmtupolicy;
3241 break;
3242
3243 case IPV6_DONTFRAG:
3244 if (len != sizeof(int))
3245 return (EINVAL);
3246
3247 if (uproto == IPPROTO_TCP || *(int *)buf == 0) {
3248 /*
3249 * we ignore this option for TCP sockets.
3250 * (RFC3542 leaves this case unspecified.)
3251 */
3252 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3253 } else
3254 opt->ip6po_flags |= IP6PO_DONTFRAG;
3255 break;
3256
3257 case IPV6_PREFER_TEMPADDR:
3258 if (len != sizeof(int))
3259 return (EINVAL);
3260 preftemp = *(int *)buf;
3261 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3262 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3263 preftemp != IP6PO_TEMPADDR_PREFER) {
3264 return (EINVAL);
3265 }
3266 opt->ip6po_prefer_tempaddr = preftemp;
3267 break;
3268
3269 default:
3270 return (ENOPROTOOPT);
3271 } /* end of switch */
3272
3273 return (0);
3274 }
3275
3276 /*
3277 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3278 * packet to the input queue of a specified interface. Note that this
3279 * calls the output routine of the loopback "driver", but with an interface
3280 * pointer that might NOT be &loif -- easier than replicating that code here.
3281 */
3282 void
ip6_mloopback(struct ifnet * ifp,struct mbuf * m)3283 ip6_mloopback(struct ifnet *ifp, struct mbuf *m)
3284 {
3285 struct mbuf *copym;
3286 struct ip6_hdr *ip6;
3287
3288 copym = m_copym(m, 0, M_COPYALL, M_NOWAIT);
3289 if (copym == NULL)
3290 return;
3291
3292 /*
3293 * Make sure to deep-copy IPv6 header portion in case the data
3294 * is in an mbuf cluster, so that we can safely override the IPv6
3295 * header portion later.
3296 */
3297 if (!M_WRITABLE(copym) ||
3298 copym->m_len < sizeof(struct ip6_hdr)) {
3299 copym = m_pullup(copym, sizeof(struct ip6_hdr));
3300 if (copym == NULL)
3301 return;
3302 }
3303 ip6 = mtod(copym, struct ip6_hdr *);
3304 /*
3305 * clear embedded scope identifiers if necessary.
3306 * in6_clearscope will touch the addresses only when necessary.
3307 */
3308 in6_clearscope(&ip6->ip6_src);
3309 in6_clearscope(&ip6->ip6_dst);
3310 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA_IPV6) {
3311 copym->m_pkthdr.csum_flags |= CSUM_DATA_VALID_IPV6 |
3312 CSUM_PSEUDO_HDR;
3313 copym->m_pkthdr.csum_data = 0xffff;
3314 }
3315 if_simloop(ifp, copym, AF_INET6, 0);
3316 }
3317
3318 /*
3319 * Chop IPv6 header off from the payload.
3320 */
3321 static int
ip6_splithdr(struct mbuf * m,struct ip6_exthdrs * exthdrs)3322 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3323 {
3324 struct mbuf *mh;
3325 struct ip6_hdr *ip6;
3326
3327 ip6 = mtod(m, struct ip6_hdr *);
3328 if (m->m_len > sizeof(*ip6)) {
3329 mh = m_gethdr(M_NOWAIT, MT_DATA);
3330 if (mh == NULL) {
3331 m_freem(m);
3332 return ENOBUFS;
3333 }
3334 m_move_pkthdr(mh, m);
3335 M_ALIGN(mh, sizeof(*ip6));
3336 m->m_len -= sizeof(*ip6);
3337 m->m_data += sizeof(*ip6);
3338 mh->m_next = m;
3339 m = mh;
3340 m->m_len = sizeof(*ip6);
3341 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof(*ip6));
3342 }
3343 exthdrs->ip6e_ip6 = m;
3344 return 0;
3345 }
3346
3347 /*
3348 * Compute IPv6 extension header length.
3349 */
3350 int
ip6_optlen(struct inpcb * inp)3351 ip6_optlen(struct inpcb *inp)
3352 {
3353 int len;
3354
3355 if (!inp->in6p_outputopts)
3356 return 0;
3357
3358 len = 0;
3359 #define elen(x) \
3360 (((struct ip6_ext *)(x)) ? (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3361
3362 len += elen(inp->in6p_outputopts->ip6po_hbh);
3363 if (inp->in6p_outputopts->ip6po_rthdr)
3364 /* dest1 is valid with rthdr only */
3365 len += elen(inp->in6p_outputopts->ip6po_dest1);
3366 len += elen(inp->in6p_outputopts->ip6po_rthdr);
3367 len += elen(inp->in6p_outputopts->ip6po_dest2);
3368 return len;
3369 #undef elen
3370 }
3371