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