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