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