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