1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* 26 * Copyright (c) 1990 Mentat Inc. 27 */ 28 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 #include <sys/types.h> 32 #include <sys/stream.h> 33 #include <sys/dlpi.h> 34 #include <sys/stropts.h> 35 #include <sys/sysmacros.h> 36 #include <sys/strsun.h> 37 #include <sys/strlog.h> 38 #include <sys/strsubr.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/ddi.h> 42 #include <sys/sunddi.h> 43 #include <sys/cmn_err.h> 44 #include <sys/debug.h> 45 #include <sys/kobj.h> 46 #include <sys/zone.h> 47 48 #include <sys/kmem.h> 49 #include <sys/systm.h> 50 #include <sys/param.h> 51 #include <sys/socket.h> 52 #include <sys/vtrace.h> 53 #include <sys/isa_defs.h> 54 #include <sys/atomic.h> 55 #include <sys/iphada.h> 56 #include <sys/policy.h> 57 #include <net/if.h> 58 #include <net/if_types.h> 59 #include <net/route.h> 60 #include <net/if_dl.h> 61 #include <sys/sockio.h> 62 #include <netinet/in.h> 63 #include <netinet/ip6.h> 64 #include <netinet/icmp6.h> 65 #include <netinet/sctp.h> 66 67 #include <inet/common.h> 68 #include <inet/mi.h> 69 #include <inet/mib2.h> 70 #include <inet/nd.h> 71 #include <inet/arp.h> 72 73 #include <inet/ip.h> 74 #include <inet/ip_impl.h> 75 #include <inet/ip6.h> 76 #include <inet/ip6_asp.h> 77 #include <inet/tcp.h> 78 #include <inet/tcp_impl.h> 79 #include <inet/udp_impl.h> 80 #include <inet/ipp_common.h> 81 82 #include <inet/ip_multi.h> 83 #include <inet/ip_if.h> 84 #include <inet/ip_ire.h> 85 #include <inet/ip_rts.h> 86 #include <inet/optcom.h> 87 #include <inet/ip_ndp.h> 88 #include <net/pfkeyv2.h> 89 #include <inet/ipsec_info.h> 90 #include <inet/sadb.h> 91 #include <inet/ipsec_impl.h> 92 #include <inet/tun.h> 93 #include <inet/sctp_ip.h> 94 #include <sys/pattr.h> 95 #include <inet/ipclassifier.h> 96 #include <inet/ipsecah.h> 97 #include <inet/udp_impl.h> 98 #include <sys/squeue.h> 99 100 #include <sys/tsol/label.h> 101 #include <sys/tsol/tnet.h> 102 103 #include <rpc/pmap_prot.h> 104 105 extern squeue_func_t ip_input_proc; 106 107 /* 108 * IP statistics. 109 */ 110 #define IP6_STAT(x) (ip6_statistics.x.value.ui64++) 111 #define IP6_STAT_UPDATE(x, n) (ip6_statistics.x.value.ui64 += (n)) 112 113 typedef struct ip6_stat { 114 kstat_named_t ip6_udp_fast_path; 115 kstat_named_t ip6_udp_slow_path; 116 kstat_named_t ip6_udp_fannorm; 117 kstat_named_t ip6_udp_fanmb; 118 kstat_named_t ip6_out_sw_cksum; 119 kstat_named_t ip6_in_sw_cksum; 120 kstat_named_t ip6_tcp_in_full_hw_cksum_err; 121 kstat_named_t ip6_tcp_in_part_hw_cksum_err; 122 kstat_named_t ip6_tcp_in_sw_cksum_err; 123 kstat_named_t ip6_tcp_out_sw_cksum_bytes; 124 kstat_named_t ip6_udp_in_full_hw_cksum_err; 125 kstat_named_t ip6_udp_in_part_hw_cksum_err; 126 kstat_named_t ip6_udp_in_sw_cksum_err; 127 kstat_named_t ip6_udp_out_sw_cksum_bytes; 128 kstat_named_t ip6_frag_mdt_pkt_out; 129 kstat_named_t ip6_frag_mdt_discarded; 130 kstat_named_t ip6_frag_mdt_allocfail; 131 kstat_named_t ip6_frag_mdt_addpdescfail; 132 kstat_named_t ip6_frag_mdt_allocd; 133 } ip6_stat_t; 134 135 static ip6_stat_t ip6_statistics = { 136 { "ip6_udp_fast_path", KSTAT_DATA_UINT64 }, 137 { "ip6_udp_slow_path", KSTAT_DATA_UINT64 }, 138 { "ip6_udp_fannorm", KSTAT_DATA_UINT64 }, 139 { "ip6_udp_fanmb", KSTAT_DATA_UINT64 }, 140 { "ip6_out_sw_cksum", KSTAT_DATA_UINT64 }, 141 { "ip6_in_sw_cksum", KSTAT_DATA_UINT64 }, 142 { "ip6_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 143 { "ip6_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 144 { "ip6_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 145 { "ip6_tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 146 { "ip6_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 }, 147 { "ip6_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 }, 148 { "ip6_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 }, 149 { "ip6_udp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 150 { "ip6_frag_mdt_pkt_out", KSTAT_DATA_UINT64 }, 151 { "ip6_frag_mdt_discarded", KSTAT_DATA_UINT64 }, 152 { "ip6_frag_mdt_allocfail", KSTAT_DATA_UINT64 }, 153 { "ip6_frag_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 154 { "ip6_frag_mdt_allocd", KSTAT_DATA_UINT64 }, 155 }; 156 157 static kstat_t *ip6_kstat; 158 159 /* 160 * Naming conventions: 161 * These rules should be judiciously applied 162 * if there is a need to identify something as IPv6 versus IPv4 163 * IPv6 funcions will end with _v6 in the ip module. 164 * IPv6 funcions will end with _ipv6 in the transport modules. 165 * IPv6 macros: 166 * Some macros end with _V6; e.g. ILL_FRAG_HASH_V6 167 * Some macros start with V6_; e.g. V6_OR_V4_INADDR_ANY 168 * And then there are ..V4_PART_OF_V6. 169 * The intent is that macros in the ip module end with _V6. 170 * IPv6 global variables will start with ipv6_ 171 * IPv6 structures will start with ipv6 172 * IPv6 defined constants should start with IPV6_ 173 * (but then there are NDP_DEFAULT_VERS_PRI_AND_FLOW, etc) 174 */ 175 176 /* 177 * IPv6 mibs when the interface (ill) is not known. 178 * When the ill is known the per-interface mib in the ill is used. 179 */ 180 mib2_ipv6IfStatsEntry_t ip6_mib; 181 mib2_ipv6IfIcmpEntry_t icmp6_mib; 182 183 /* 184 * ip6opt_ls is used to enable IPv6 (via /etc/system on TX systems). 185 * We need to do this because we didn't obtain the IP6OPT_LS (0x0a) 186 * from IANA. This mechanism will remain in effect until an official 187 * number is obtained. 188 */ 189 uchar_t ip6opt_ls; 190 191 uint_t ipv6_ire_default_count; /* Number of IPv6 IRE_DEFAULT entries */ 192 uint_t ipv6_ire_default_index; /* Walking IPv6 index used to mod in */ 193 194 const in6_addr_t ipv6_all_ones = 195 { 0xffffffffU, 0xffffffffU, 0xffffffffU, 0xffffffffU }; 196 const in6_addr_t ipv6_all_zeros = { 0, 0, 0, 0 }; 197 198 #ifdef _BIG_ENDIAN 199 const in6_addr_t ipv6_unspecified_group = { 0xff000000U, 0, 0, 0 }; 200 #else /* _BIG_ENDIAN */ 201 const in6_addr_t ipv6_unspecified_group = { 0x000000ffU, 0, 0, 0 }; 202 #endif /* _BIG_ENDIAN */ 203 204 #ifdef _BIG_ENDIAN 205 const in6_addr_t ipv6_loopback = { 0, 0, 0, 0x00000001U }; 206 #else /* _BIG_ENDIAN */ 207 const in6_addr_t ipv6_loopback = { 0, 0, 0, 0x01000000U }; 208 #endif /* _BIG_ENDIAN */ 209 210 #ifdef _BIG_ENDIAN 211 const in6_addr_t ipv6_all_hosts_mcast = { 0xff020000U, 0, 0, 0x00000001U }; 212 #else /* _BIG_ENDIAN */ 213 const in6_addr_t ipv6_all_hosts_mcast = { 0x000002ffU, 0, 0, 0x01000000U }; 214 #endif /* _BIG_ENDIAN */ 215 216 #ifdef _BIG_ENDIAN 217 const in6_addr_t ipv6_all_rtrs_mcast = { 0xff020000U, 0, 0, 0x00000002U }; 218 #else /* _BIG_ENDIAN */ 219 const in6_addr_t ipv6_all_rtrs_mcast = { 0x000002ffU, 0, 0, 0x02000000U }; 220 #endif /* _BIG_ENDIAN */ 221 222 #ifdef _BIG_ENDIAN 223 const in6_addr_t ipv6_all_v2rtrs_mcast = { 0xff020000U, 0, 0, 0x00000016U }; 224 #else /* _BIG_ENDIAN */ 225 const in6_addr_t ipv6_all_v2rtrs_mcast = { 0x000002ffU, 0, 0, 0x16000000U }; 226 #endif /* _BIG_ENDIAN */ 227 228 #ifdef _BIG_ENDIAN 229 const in6_addr_t ipv6_solicited_node_mcast = 230 { 0xff020000U, 0, 0x00000001U, 0xff000000U }; 231 #else /* _BIG_ENDIAN */ 232 const in6_addr_t ipv6_solicited_node_mcast = 233 { 0x000002ffU, 0, 0x01000000U, 0x000000ffU }; 234 #endif /* _BIG_ENDIAN */ 235 236 /* 237 * Used by icmp_send_redirect_v6 for picking random src. 238 */ 239 uint_t icmp_redirect_v6_src_index; 240 241 /* Leave room for ip_newroute to tack on the src and target addresses */ 242 #define OK_RESOLVER_MP_V6(mp) \ 243 ((mp) && ((mp)->b_wptr - (mp)->b_rptr) >= (2 * IPV6_ADDR_LEN)) 244 245 static void icmp_inbound_too_big_v6(queue_t *, mblk_t *, ill_t *ill, 246 boolean_t, zoneid_t); 247 static void icmp_pkt_v6(queue_t *, mblk_t *, void *, size_t, 248 const in6_addr_t *, boolean_t); 249 static void icmp_redirect_v6(queue_t *, mblk_t *, ill_t *ill); 250 static boolean_t icmp_redirect_ok_v6(ill_t *ill, mblk_t *mp); 251 static int ip_bind_connected_v6(conn_t *, mblk_t *, in6_addr_t *, 252 uint16_t, const in6_addr_t *, ip6_pkt_t *, uint16_t, 253 boolean_t, boolean_t, boolean_t, boolean_t); 254 static boolean_t ip_bind_insert_ire_v6(mblk_t *, ire_t *, const in6_addr_t *, 255 iulp_t *); 256 static int ip_bind_laddr_v6(conn_t *, mblk_t *, const in6_addr_t *, 257 uint16_t, boolean_t, boolean_t, boolean_t); 258 static void ip_fanout_proto_v6(queue_t *, mblk_t *, ip6_t *, ill_t *, 259 ill_t *, uint8_t, uint_t, uint_t, boolean_t, zoneid_t); 260 static void ip_fanout_tcp_v6(queue_t *, mblk_t *, ip6_t *, ill_t *, 261 ill_t *, uint_t, uint_t, boolean_t, zoneid_t); 262 static void ip_fanout_udp_v6(queue_t *, mblk_t *, ip6_t *, uint32_t, 263 ill_t *, ill_t *, uint_t, boolean_t, zoneid_t); 264 static int ip_process_options_v6(queue_t *, mblk_t *, ip6_t *, 265 uint8_t *, uint_t, uint8_t); 266 static mblk_t *ip_rput_frag_v6(queue_t *, mblk_t *, ip6_t *, 267 ip6_frag_t *, uint_t, uint_t *, uint32_t *, uint16_t *); 268 static boolean_t ip_source_routed_v6(ip6_t *, mblk_t *); 269 static void ip_wput_ire_v6(queue_t *, mblk_t *, ire_t *, int, int, 270 conn_t *, int, int, int); 271 static boolean_t ip_ulp_cando_pkt2big(int); 272 273 static void ip_rput_v6(queue_t *, mblk_t *); 274 static void ip_wput_v6(queue_t *, mblk_t *); 275 276 /* 277 * A template for an IPv6 AR_ENTRY_QUERY 278 */ 279 static areq_t ipv6_areq_template = { 280 AR_ENTRY_QUERY, /* cmd */ 281 sizeof (areq_t)+(2*IPV6_ADDR_LEN), /* name offset */ 282 sizeof (areq_t), /* name len (filled by ill_arp_alloc) */ 283 IP6_DL_SAP, /* protocol, from arps perspective */ 284 sizeof (areq_t), /* target addr offset */ 285 IPV6_ADDR_LEN, /* target addr_length */ 286 0, /* flags */ 287 sizeof (areq_t) + IPV6_ADDR_LEN, /* sender addr offset */ 288 IPV6_ADDR_LEN, /* sender addr length */ 289 6, /* xmit_count */ 290 1000, /* (re)xmit_interval in milliseconds */ 291 4 /* max # of requests to buffer */ 292 /* anything else filled in by the code */ 293 }; 294 295 struct qinit rinit_ipv6 = { 296 (pfi_t)ip_rput_v6, 297 NULL, 298 ip_open, 299 ip_close, 300 NULL, 301 &ip_mod_info 302 }; 303 304 struct qinit winit_ipv6 = { 305 (pfi_t)ip_wput_v6, 306 (pfi_t)ip_wsrv, 307 ip_open, 308 ip_close, 309 NULL, 310 &ip_mod_info 311 }; 312 313 /* 314 * Handle IPv6 ICMP packets sent to us. Consume the mblk passed in. 315 * The message has already been checksummed and if needed, 316 * a copy has been made to be sent any interested ICMP client (conn) 317 * Note that this is different than icmp_inbound() which does the fanout 318 * to conn's as well as local processing of the ICMP packets. 319 * 320 * All error messages are passed to the matching transport stream. 321 * 322 * Zones notes: 323 * The packet is only processed in the context of the specified zone: typically 324 * only this zone will reply to an echo request. This means that the caller must 325 * call icmp_inbound_v6() for each relevant zone. 326 */ 327 static void 328 icmp_inbound_v6(queue_t *q, mblk_t *mp, ill_t *ill, uint_t hdr_length, 329 boolean_t mctl_present, uint_t flags, zoneid_t zoneid) 330 { 331 icmp6_t *icmp6; 332 ip6_t *ip6h; 333 boolean_t interested; 334 ip6i_t *ip6i; 335 in6_addr_t origsrc; 336 ire_t *ire; 337 mblk_t *first_mp; 338 ipsec_in_t *ii; 339 340 ASSERT(ill != NULL); 341 first_mp = mp; 342 if (mctl_present) { 343 mp = first_mp->b_cont; 344 ASSERT(mp != NULL); 345 346 ii = (ipsec_in_t *)first_mp->b_rptr; 347 ASSERT(ii->ipsec_in_type == IPSEC_IN); 348 } 349 350 ip6h = (ip6_t *)mp->b_rptr; 351 352 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInMsgs); 353 354 if ((mp->b_wptr - mp->b_rptr) < (hdr_length + ICMP6_MINLEN)) { 355 if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) { 356 ip1dbg(("icmp_inbound_v6: pullupmsg failed\n")); 357 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors); 358 freemsg(first_mp); 359 return; 360 } 361 ip6h = (ip6_t *)mp->b_rptr; 362 } 363 if (icmp_accept_clear_messages == 0) { 364 first_mp = ipsec_check_global_policy(first_mp, NULL, 365 NULL, ip6h, mctl_present); 366 if (first_mp == NULL) 367 return; 368 } 369 370 /* 371 * On a labeled system, we have to check whether the zone itself is 372 * permitted to receive raw traffic. 373 */ 374 if (is_system_labeled()) { 375 if (zoneid == ALL_ZONES) 376 zoneid = tsol_packet_to_zoneid(mp); 377 if (!tsol_can_accept_raw(mp, B_FALSE)) { 378 ip1dbg(("icmp_inbound_v6: zone %d can't receive raw", 379 zoneid)); 380 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors); 381 freemsg(first_mp); 382 return; 383 } 384 } 385 386 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 387 ip2dbg(("icmp_inbound_v6: type %d code %d\n", icmp6->icmp6_type, 388 icmp6->icmp6_code)); 389 interested = !(icmp6->icmp6_type & ICMP6_INFOMSG_MASK); 390 391 /* Initiate IPPF processing here */ 392 if (IP6_IN_IPP(flags)) { 393 394 /* 395 * If the ifindex changes due to SIOCSLIFINDEX 396 * packet may return to IP on the wrong ill. 397 */ 398 ip_process(IPP_LOCAL_IN, &mp, ill->ill_phyint->phyint_ifindex); 399 if (mp == NULL) { 400 if (mctl_present) { 401 freeb(first_mp); 402 } 403 return; 404 } 405 } 406 407 switch (icmp6->icmp6_type) { 408 case ICMP6_DST_UNREACH: 409 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInDestUnreachs); 410 if (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN) 411 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInAdminProhibs); 412 break; 413 414 case ICMP6_TIME_EXCEEDED: 415 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInTimeExcds); 416 break; 417 418 case ICMP6_PARAM_PROB: 419 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInParmProblems); 420 break; 421 422 case ICMP6_PACKET_TOO_BIG: 423 icmp_inbound_too_big_v6(q, first_mp, ill, mctl_present, 424 zoneid); 425 return; 426 case ICMP6_ECHO_REQUEST: 427 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInEchos); 428 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) && 429 !ipv6_resp_echo_mcast) 430 break; 431 432 /* 433 * We must have exclusive use of the mblk to convert it to 434 * a response. 435 * If not, we copy it. 436 */ 437 if (mp->b_datap->db_ref > 1) { 438 mblk_t *mp1; 439 440 mp1 = copymsg(mp); 441 freemsg(mp); 442 if (mp1 == NULL) { 443 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 444 if (mctl_present) 445 freeb(first_mp); 446 return; 447 } 448 mp = mp1; 449 ip6h = (ip6_t *)mp->b_rptr; 450 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 451 if (mctl_present) 452 first_mp->b_cont = mp; 453 else 454 first_mp = mp; 455 } 456 457 /* 458 * Turn the echo into an echo reply. 459 * Remove any extension headers (do not reverse a source route) 460 * and clear the flow id (keep traffic class for now). 461 */ 462 if (hdr_length != IPV6_HDR_LEN) { 463 int i; 464 465 for (i = 0; i < IPV6_HDR_LEN; i++) 466 mp->b_rptr[hdr_length - i - 1] = 467 mp->b_rptr[IPV6_HDR_LEN - i - 1]; 468 mp->b_rptr += (hdr_length - IPV6_HDR_LEN); 469 ip6h = (ip6_t *)mp->b_rptr; 470 ip6h->ip6_nxt = IPPROTO_ICMPV6; 471 hdr_length = IPV6_HDR_LEN; 472 } 473 ip6h->ip6_vcf &= ~IPV6_FLOWINFO_FLOWLABEL; 474 icmp6->icmp6_type = ICMP6_ECHO_REPLY; 475 476 ip6h->ip6_plen = 477 htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN)); 478 origsrc = ip6h->ip6_src; 479 /* 480 * Reverse the source and destination addresses. 481 * If the return address is a multicast, zero out the source 482 * (ip_wput_v6 will set an address). 483 */ 484 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 485 ip6h->ip6_src = ipv6_all_zeros; 486 ip6h->ip6_dst = origsrc; 487 } else { 488 ip6h->ip6_src = ip6h->ip6_dst; 489 ip6h->ip6_dst = origsrc; 490 } 491 492 /* set the hop limit */ 493 ip6h->ip6_hops = ipv6_def_hops; 494 495 /* 496 * Prepare for checksum by putting icmp length in the icmp 497 * checksum field. The checksum is calculated in ip_wput_v6. 498 */ 499 icmp6->icmp6_cksum = ip6h->ip6_plen; 500 /* 501 * ICMP echo replies should go out on the same interface 502 * the request came on as probes used by in.mpathd for 503 * detecting NIC failures are ECHO packets. We turn-off load 504 * spreading by allocating a ip6i and setting ip6i_attach_if 505 * to B_TRUE which is handled both by ip_wput_v6 and 506 * ip_newroute_v6. If we don't turnoff load spreading, 507 * the packets might get dropped if there are no 508 * non-FAILED/INACTIVE interfaces for it to go out on and 509 * in.mpathd would wrongly detect a failure or mis-detect 510 * a NIC failure as a link failure. As load spreading can 511 * happen only if ill_group is not NULL, we do only for 512 * that case and this does not affect the normal case. 513 * 514 * We force this only on echo packets that came from on-link 515 * hosts. We restrict this to link-local addresses which 516 * is used by in.mpathd for probing. In the IPv6 case, 517 * default routes typically have an ire_ipif pointer and 518 * hence a MATCH_IRE_ILL later in ip_newroute_v6/ip_wput_v6 519 * might work. As a default route out of this interface 520 * may not be present, enforcing this packet to go out in 521 * this case may not work. 522 */ 523 if (ill->ill_group != NULL && 524 IN6_IS_ADDR_LINKLOCAL(&origsrc)) { 525 /* 526 * If we are sending replies to ourselves, don't 527 * set ATTACH_IF as we may not be able to find 528 * the IRE_LOCAL on this ill i.e setting ATTACH_IF 529 * causes ip_wput_v6 to look for an IRE_LOCAL on 530 * "ill" which it may not find and will try to 531 * create an IRE_CACHE for our local address. Once 532 * we do this, we will try to forward all packets 533 * meant to our LOCAL address. 534 */ 535 ire = ire_cache_lookup_v6(&ip6h->ip6_dst, ALL_ZONES, 536 NULL); 537 if (ire == NULL || ire->ire_type != IRE_LOCAL) { 538 mp = ip_add_info_v6(mp, NULL, &ip6h->ip6_dst); 539 if (mp == NULL) { 540 BUMP_MIB(ill->ill_icmp6_mib, 541 ipv6IfIcmpInErrors); 542 if (ire != NULL) 543 ire_refrele(ire); 544 if (mctl_present) 545 freeb(first_mp); 546 return; 547 } else if (mctl_present) { 548 first_mp->b_cont = mp; 549 } else { 550 first_mp = mp; 551 } 552 ip6i = (ip6i_t *)mp->b_rptr; 553 ip6i->ip6i_flags = IP6I_ATTACH_IF; 554 ip6i->ip6i_ifindex = 555 ill->ill_phyint->phyint_ifindex; 556 } 557 if (ire != NULL) 558 ire_refrele(ire); 559 } 560 561 if (!mctl_present) { 562 /* 563 * This packet should go out the same way as it 564 * came in i.e in clear. To make sure that global 565 * policy will not be applied to this in ip_wput, 566 * we attach a IPSEC_IN mp and clear ipsec_in_secure. 567 */ 568 ASSERT(first_mp == mp); 569 if ((first_mp = ipsec_in_alloc(B_FALSE)) == NULL) { 570 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 571 freemsg(mp); 572 return; 573 } 574 ii = (ipsec_in_t *)first_mp->b_rptr; 575 576 /* This is not a secure packet */ 577 ii->ipsec_in_secure = B_FALSE; 578 first_mp->b_cont = mp; 579 } 580 ii->ipsec_in_zoneid = zoneid; 581 ASSERT(zoneid != ALL_ZONES); 582 if (!ipsec_in_to_out(first_mp, NULL, ip6h)) { 583 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 584 return; 585 } 586 put(WR(q), first_mp); 587 return; 588 589 case ICMP6_ECHO_REPLY: 590 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInEchoReplies); 591 break; 592 593 case ND_ROUTER_SOLICIT: 594 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInRouterSolicits); 595 break; 596 597 case ND_ROUTER_ADVERT: 598 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInRouterAdvertisements); 599 break; 600 601 case ND_NEIGHBOR_SOLICIT: 602 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInNeighborSolicits); 603 if (mctl_present) 604 freeb(first_mp); 605 /* XXX may wish to pass first_mp up to ndp_input someday. */ 606 ndp_input(ill, mp); 607 return; 608 609 case ND_NEIGHBOR_ADVERT: 610 BUMP_MIB(ill->ill_icmp6_mib, 611 ipv6IfIcmpInNeighborAdvertisements); 612 if (mctl_present) 613 freeb(first_mp); 614 /* XXX may wish to pass first_mp up to ndp_input someday. */ 615 ndp_input(ill, mp); 616 return; 617 618 case ND_REDIRECT: { 619 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInRedirects); 620 621 if (ipv6_ignore_redirect) 622 break; 623 624 /* 625 * As there is no upper client to deliver, we don't 626 * need the first_mp any more. 627 */ 628 if (mctl_present) 629 freeb(first_mp); 630 if (!pullupmsg(mp, -1) || 631 !icmp_redirect_ok_v6(ill, mp)) { 632 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects); 633 break; 634 } 635 icmp_redirect_v6(q, mp, ill); 636 return; 637 } 638 639 /* 640 * The next three icmp messages will be handled by MLD. 641 * Pass all valid MLD packets up to any process(es) 642 * listening on a raw ICMP socket. MLD messages are 643 * freed by mld_input function. 644 */ 645 case MLD_LISTENER_QUERY: 646 case MLD_LISTENER_REPORT: 647 case MLD_LISTENER_REDUCTION: 648 if (mctl_present) 649 freeb(first_mp); 650 mld_input(q, mp, ill); 651 return; 652 default: 653 break; 654 } 655 if (interested) { 656 icmp_inbound_error_fanout_v6(q, first_mp, ip6h, icmp6, ill, 657 mctl_present, zoneid); 658 } else { 659 freemsg(first_mp); 660 } 661 } 662 663 /* 664 * Process received IPv6 ICMP Packet too big. 665 * After updating any IRE it does the fanout to any matching transport streams. 666 * Assumes the IPv6 plus ICMPv6 headers have been pulled up but nothing else. 667 */ 668 /* ARGSUSED */ 669 static void 670 icmp_inbound_too_big_v6(queue_t *q, mblk_t *mp, ill_t *ill, 671 boolean_t mctl_present, zoneid_t zoneid) 672 { 673 ip6_t *ip6h; 674 ip6_t *inner_ip6h; 675 icmp6_t *icmp6; 676 uint16_t hdr_length; 677 uint32_t mtu; 678 ire_t *ire, *first_ire; 679 mblk_t *first_mp; 680 681 first_mp = mp; 682 if (mctl_present) 683 mp = first_mp->b_cont; 684 /* 685 * We must have exclusive use of the mblk to update the MTU 686 * in the packet. 687 * If not, we copy it. 688 * 689 * If there's an M_CTL present, we know that allocated first_mp 690 * earlier in this function, so we know first_mp has refcnt of one. 691 */ 692 ASSERT(!mctl_present || first_mp->b_datap->db_ref == 1); 693 if (mp->b_datap->db_ref > 1) { 694 mblk_t *mp1; 695 696 mp1 = copymsg(mp); 697 freemsg(mp); 698 if (mp1 == NULL) { 699 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 700 if (mctl_present) 701 freeb(first_mp); 702 return; 703 } 704 mp = mp1; 705 if (mctl_present) 706 first_mp->b_cont = mp; 707 else 708 first_mp = mp; 709 } 710 ip6h = (ip6_t *)mp->b_rptr; 711 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 712 hdr_length = ip_hdr_length_v6(mp, ip6h); 713 else 714 hdr_length = IPV6_HDR_LEN; 715 716 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 717 ASSERT((size_t)(mp->b_wptr - mp->b_rptr) >= hdr_length + ICMP6_MINLEN); 718 inner_ip6h = (ip6_t *)&icmp6[1]; /* Packet in error */ 719 if ((uchar_t *)&inner_ip6h[1] > mp->b_wptr) { 720 if (!pullupmsg(mp, (uchar_t *)&inner_ip6h[1] - mp->b_rptr)) { 721 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 722 freemsg(first_mp); 723 return; 724 } 725 ip6h = (ip6_t *)mp->b_rptr; 726 icmp6 = (icmp6_t *)&mp->b_rptr[hdr_length]; 727 inner_ip6h = (ip6_t *)&icmp6[1]; 728 } 729 730 /* 731 * For link local destinations matching simply on IRE type is not 732 * sufficient. Same link local addresses for different ILL's is 733 * possible. 734 */ 735 736 if (IN6_IS_ADDR_LINKLOCAL(&inner_ip6h->ip6_dst)) { 737 first_ire = ire_ctable_lookup_v6(&inner_ip6h->ip6_dst, NULL, 738 IRE_CACHE, ill->ill_ipif, ALL_ZONES, NULL, 739 MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP); 740 741 if (first_ire == NULL) { 742 if (ip_debug > 2) { 743 /* ip1dbg */ 744 pr_addr_dbg("icmp_inbound_too_big_v6:" 745 "no ire for dst %s\n", AF_INET6, 746 &inner_ip6h->ip6_dst); 747 } 748 freemsg(first_mp); 749 return; 750 } 751 752 mtu = ntohl(icmp6->icmp6_mtu); 753 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER); 754 for (ire = first_ire; ire != NULL && 755 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &inner_ip6h->ip6_dst); 756 ire = ire->ire_next) { 757 mutex_enter(&ire->ire_lock); 758 if (mtu < IPV6_MIN_MTU) { 759 ip1dbg(("Received mtu less than IPv6 " 760 "min mtu %d: %d\n", IPV6_MIN_MTU, mtu)); 761 mtu = IPV6_MIN_MTU; 762 /* 763 * If an mtu less than IPv6 min mtu is received, 764 * we must include a fragment header in 765 * subsequent packets. 766 */ 767 ire->ire_frag_flag |= IPH_FRAG_HDR; 768 } 769 ip1dbg(("Received mtu from router: %d\n", mtu)); 770 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 771 /* Record the new max frag size for the ULP. */ 772 if (ire->ire_frag_flag & IPH_FRAG_HDR) { 773 /* 774 * If we need a fragment header in every packet 775 * (above case or multirouting), make sure the 776 * ULP takes it into account when computing the 777 * payload size. 778 */ 779 icmp6->icmp6_mtu = htonl(ire->ire_max_frag - 780 sizeof (ip6_frag_t)); 781 } else { 782 icmp6->icmp6_mtu = htonl(ire->ire_max_frag); 783 } 784 mutex_exit(&ire->ire_lock); 785 } 786 rw_exit(&first_ire->ire_bucket->irb_lock); 787 ire_refrele(first_ire); 788 } else { 789 irb_t *irb = NULL; 790 /* 791 * for non-link local destinations we match only on the IRE type 792 */ 793 ire = ire_ctable_lookup_v6(&inner_ip6h->ip6_dst, NULL, 794 IRE_CACHE, ill->ill_ipif, ALL_ZONES, NULL, MATCH_IRE_TYPE); 795 if (ire == NULL) { 796 if (ip_debug > 2) { 797 /* ip1dbg */ 798 pr_addr_dbg("icmp_inbound_too_big_v6:" 799 "no ire for dst %s\n", 800 AF_INET6, &inner_ip6h->ip6_dst); 801 } 802 freemsg(first_mp); 803 return; 804 } 805 irb = ire->ire_bucket; 806 ire_refrele(ire); 807 rw_enter(&irb->irb_lock, RW_READER); 808 for (ire = irb->irb_ire; ire != NULL; ire = ire->ire_next) { 809 if (IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, 810 &inner_ip6h->ip6_dst)) { 811 mtu = ntohl(icmp6->icmp6_mtu); 812 mutex_enter(&ire->ire_lock); 813 if (mtu < IPV6_MIN_MTU) { 814 ip1dbg(("Received mtu less than IPv6" 815 "min mtu %d: %d\n", 816 IPV6_MIN_MTU, mtu)); 817 mtu = IPV6_MIN_MTU; 818 /* 819 * If an mtu less than IPv6 min mtu is 820 * received, we must include a fragment 821 * header in subsequent packets. 822 */ 823 ire->ire_frag_flag |= IPH_FRAG_HDR; 824 } 825 826 ip1dbg(("Received mtu from router: %d\n", mtu)); 827 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu); 828 /* Record the new max frag size for the ULP. */ 829 if (ire->ire_frag_flag & IPH_FRAG_HDR) { 830 /* 831 * If we need a fragment header in 832 * every packet (above case or 833 * multirouting), make sure the ULP 834 * takes it into account when computing 835 * the payload size. 836 */ 837 icmp6->icmp6_mtu = 838 htonl(ire->ire_max_frag - 839 sizeof (ip6_frag_t)); 840 } else { 841 icmp6->icmp6_mtu = 842 htonl(ire->ire_max_frag); 843 } 844 mutex_exit(&ire->ire_lock); 845 } 846 } 847 rw_exit(&irb->irb_lock); 848 } 849 icmp_inbound_error_fanout_v6(q, first_mp, ip6h, icmp6, ill, 850 mctl_present, zoneid); 851 } 852 853 static void 854 pkt_too_big(conn_t *connp, void *arg) 855 { 856 mblk_t *mp; 857 858 if (!connp->conn_ipv6_recvpathmtu) 859 return; 860 861 /* create message and drop it on this connections read queue */ 862 if ((mp = dupb((mblk_t *)arg)) == NULL) { 863 return; 864 } 865 mp->b_datap->db_type = M_CTL; 866 867 putnext(connp->conn_rq, mp); 868 } 869 870 /* 871 * Fanout received ICMPv6 error packets to the transports. 872 * Assumes the IPv6 plus ICMPv6 headers have been pulled up but nothing else. 873 */ 874 void 875 icmp_inbound_error_fanout_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, 876 icmp6_t *icmp6, ill_t *ill, boolean_t mctl_present, zoneid_t zoneid) 877 { 878 uint16_t *up; /* Pointer to ports in ULP header */ 879 uint32_t ports; /* reversed ports for fanout */ 880 ip6_t rip6h; /* With reversed addresses */ 881 uint16_t hdr_length; 882 uint8_t *nexthdrp; 883 uint8_t nexthdr; 884 mblk_t *first_mp; 885 ipsec_in_t *ii; 886 tcpha_t *tcpha; 887 conn_t *connp; 888 889 first_mp = mp; 890 if (mctl_present) { 891 mp = first_mp->b_cont; 892 ASSERT(mp != NULL); 893 894 ii = (ipsec_in_t *)first_mp->b_rptr; 895 ASSERT(ii->ipsec_in_type == IPSEC_IN); 896 } else { 897 ii = NULL; 898 } 899 900 hdr_length = (uint16_t)((uchar_t *)icmp6 - (uchar_t *)ip6h); 901 ASSERT((size_t)(mp->b_wptr - (uchar_t *)icmp6) >= ICMP6_MINLEN); 902 903 /* 904 * Need to pullup everything in order to use 905 * ip_hdr_length_nexthdr_v6() 906 */ 907 if (mp->b_cont != NULL) { 908 if (!pullupmsg(mp, -1)) { 909 ip1dbg(("icmp_inbound_error_fanout_v6: " 910 "pullupmsg failed\n")); 911 goto drop_pkt; 912 } 913 ip6h = (ip6_t *)mp->b_rptr; 914 icmp6 = (icmp6_t *)(&mp->b_rptr[hdr_length]); 915 } 916 917 ip6h = (ip6_t *)&icmp6[1]; /* Packet in error */ 918 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 919 goto drop_pkt; 920 921 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) 922 goto drop_pkt; 923 nexthdr = *nexthdrp; 924 925 /* Set message type, must be done after pullups */ 926 mp->b_datap->db_type = M_CTL; 927 928 if (icmp6->icmp6_type == ICMP6_PACKET_TOO_BIG) { 929 /* 930 * Deliver indication of ICMP6_PACKET_TOO_BIG to interested 931 * sockets. 932 * 933 * Note I don't like walking every connection to deliver 934 * this information to a set of listeners. A separate 935 * list could be kept to keep the cost of this down. 936 */ 937 ipcl_walk(pkt_too_big, (void *)mp); 938 } 939 940 /* Try to pass the ICMP message to clients who need it */ 941 switch (nexthdr) { 942 case IPPROTO_UDP: { 943 /* 944 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 945 * UDP header to get the port information. 946 */ 947 if ((uchar_t *)ip6h + hdr_length + ICMP_MIN_TP_HDR_LEN > 948 mp->b_wptr) { 949 break; 950 } 951 /* 952 * Attempt to find a client stream based on port. 953 * Note that we do a reverse lookup since the header is 954 * in the form we sent it out. 955 * The rip6h header is only used for the IPCL_UDP_MATCH_V6 956 * and we only set the src and dst addresses and nexthdr. 957 */ 958 up = (uint16_t *)((uchar_t *)ip6h + hdr_length); 959 rip6h.ip6_src = ip6h->ip6_dst; 960 rip6h.ip6_dst = ip6h->ip6_src; 961 rip6h.ip6_nxt = nexthdr; 962 ((uint16_t *)&ports)[0] = up[1]; 963 ((uint16_t *)&ports)[1] = up[0]; 964 965 ip_fanout_udp_v6(q, first_mp, &rip6h, ports, ill, ill, 966 IP6_NO_IPPOLICY, mctl_present, zoneid); 967 return; 968 } 969 case IPPROTO_TCP: { 970 /* 971 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 972 * the TCP header to get the port information. 973 */ 974 if ((uchar_t *)ip6h + hdr_length + ICMP_MIN_TP_HDR_LEN > 975 mp->b_wptr) { 976 break; 977 } 978 979 /* 980 * Attempt to find a client stream based on port. 981 * Note that we do a reverse lookup since the header is 982 * in the form we sent it out. 983 * The rip6h header is only used for the IP_TCP_*MATCH_V6 and 984 * we only set the src and dst addresses and nexthdr. 985 */ 986 987 tcpha = (tcpha_t *)((char *)ip6h + hdr_length); 988 connp = ipcl_tcp_lookup_reversed_ipv6(ip6h, tcpha, 989 TCPS_LISTEN, ill->ill_phyint->phyint_ifindex); 990 if (connp == NULL) { 991 goto drop_pkt; 992 } 993 994 squeue_fill(connp->conn_sqp, first_mp, tcp_input, 995 connp, SQTAG_TCP6_INPUT_ICMP_ERR); 996 return; 997 998 } 999 case IPPROTO_SCTP: 1000 /* 1001 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of 1002 * the SCTP header to get the port information. 1003 */ 1004 if ((uchar_t *)ip6h + hdr_length + ICMP_MIN_TP_HDR_LEN > 1005 mp->b_wptr) { 1006 break; 1007 } 1008 1009 up = (uint16_t *)((uchar_t *)ip6h + hdr_length); 1010 ((uint16_t *)&ports)[0] = up[1]; 1011 ((uint16_t *)&ports)[1] = up[0]; 1012 ip_fanout_sctp(mp, ill, (ipha_t *)ip6h, ports, 0, mctl_present, 1013 IP6_NO_IPPOLICY, 0, zoneid); 1014 return; 1015 case IPPROTO_ESP: 1016 case IPPROTO_AH: { 1017 int ipsec_rc; 1018 1019 /* 1020 * We need a IPSEC_IN in the front to fanout to AH/ESP. 1021 * We will re-use the IPSEC_IN if it is already present as 1022 * AH/ESP will not affect any fields in the IPSEC_IN for 1023 * ICMP errors. If there is no IPSEC_IN, allocate a new 1024 * one and attach it in the front. 1025 */ 1026 if (ii != NULL) { 1027 /* 1028 * ip_fanout_proto_again converts the ICMP errors 1029 * that come back from AH/ESP to M_DATA so that 1030 * if it is non-AH/ESP and we do a pullupmsg in 1031 * this function, it would work. Convert it back 1032 * to M_CTL before we send up as this is a ICMP 1033 * error. This could have been generated locally or 1034 * by some router. Validate the inner IPSEC 1035 * headers. 1036 * 1037 * NOTE : ill_index is used by ip_fanout_proto_again 1038 * to locate the ill. 1039 */ 1040 ASSERT(ill != NULL); 1041 ii->ipsec_in_ill_index = 1042 ill->ill_phyint->phyint_ifindex; 1043 ii->ipsec_in_rill_index = ii->ipsec_in_ill_index; 1044 first_mp->b_cont->b_datap->db_type = M_CTL; 1045 } else { 1046 /* 1047 * IPSEC_IN is not present. We attach a ipsec_in 1048 * message and send up to IPSEC for validating 1049 * and removing the IPSEC headers. Clear 1050 * ipsec_in_secure so that when we return 1051 * from IPSEC, we don't mistakenly think that this 1052 * is a secure packet came from the network. 1053 * 1054 * NOTE : ill_index is used by ip_fanout_proto_again 1055 * to locate the ill. 1056 */ 1057 ASSERT(first_mp == mp); 1058 first_mp = ipsec_in_alloc(B_FALSE); 1059 if (first_mp == NULL) { 1060 freemsg(mp); 1061 BUMP_MIB(&ip_mib, ipInDiscards); 1062 return; 1063 } 1064 ii = (ipsec_in_t *)first_mp->b_rptr; 1065 1066 /* This is not a secure packet */ 1067 ii->ipsec_in_secure = B_FALSE; 1068 first_mp->b_cont = mp; 1069 mp->b_datap->db_type = M_CTL; 1070 ASSERT(ill != NULL); 1071 ii->ipsec_in_ill_index = 1072 ill->ill_phyint->phyint_ifindex; 1073 ii->ipsec_in_rill_index = ii->ipsec_in_ill_index; 1074 } 1075 1076 if (!ipsec_loaded()) { 1077 ip_proto_not_sup(q, first_mp, 0, zoneid); 1078 return; 1079 } 1080 1081 if (nexthdr == IPPROTO_ESP) 1082 ipsec_rc = ipsecesp_icmp_error(first_mp); 1083 else 1084 ipsec_rc = ipsecah_icmp_error(first_mp); 1085 if (ipsec_rc == IPSEC_STATUS_FAILED) 1086 return; 1087 1088 ip_fanout_proto_again(first_mp, ill, ill, NULL); 1089 return; 1090 } 1091 case IPPROTO_ENCAP: 1092 case IPPROTO_IPV6: 1093 if ((uint8_t *)ip6h + hdr_length + 1094 (nexthdr == IPPROTO_ENCAP ? sizeof (ipha_t) : 1095 sizeof (ip6_t)) > mp->b_wptr) 1096 goto drop_pkt; 1097 1098 if (nexthdr == IPPROTO_ENCAP || 1099 !IN6_ARE_ADDR_EQUAL( 1100 &((ip6_t *)(((uint8_t *)ip6h) + hdr_length))->ip6_src, 1101 &ip6h->ip6_src) || 1102 !IN6_ARE_ADDR_EQUAL( 1103 &((ip6_t *)(((uint8_t *)ip6h) + hdr_length))->ip6_dst, 1104 &ip6h->ip6_dst)) { 1105 /* 1106 * For tunnels that have used IPsec protection, 1107 * we need to adjust the MTU to take into account 1108 * the IPsec overhead. 1109 */ 1110 if (ii != NULL) 1111 icmp6->icmp6_mtu = htons( 1112 ntohs(icmp6->icmp6_mtu) - 1113 ipsec_in_extra_length(first_mp)); 1114 } else { 1115 /* 1116 * Self-encapsulated case. As in the ipv4 case, 1117 * we need to strip the 2nd IP header. Since mp 1118 * is already pulled-up, we can simply bcopy 1119 * the 3rd header + data over the 2nd header. 1120 */ 1121 uint16_t unused_len; 1122 ip6_t *inner_ip6h = (ip6_t *) 1123 ((uchar_t *)ip6h + hdr_length); 1124 1125 /* 1126 * Make sure we don't do recursion more than once. 1127 */ 1128 if (!ip_hdr_length_nexthdr_v6(mp, inner_ip6h, 1129 &unused_len, &nexthdrp) || 1130 *nexthdrp == IPPROTO_IPV6) { 1131 goto drop_pkt; 1132 } 1133 1134 /* 1135 * We are about to modify the packet. Make a copy if 1136 * someone else has a reference to it. 1137 */ 1138 if (DB_REF(mp) > 1) { 1139 mblk_t *mp1; 1140 uint16_t icmp6_offset; 1141 1142 mp1 = copymsg(mp); 1143 if (mp1 == NULL) { 1144 goto drop_pkt; 1145 } 1146 icmp6_offset = (uint16_t) 1147 ((uchar_t *)icmp6 - mp->b_rptr); 1148 freemsg(mp); 1149 mp = mp1; 1150 1151 icmp6 = (icmp6_t *)(mp->b_rptr + icmp6_offset); 1152 ip6h = (ip6_t *)&icmp6[1]; 1153 inner_ip6h = (ip6_t *) 1154 ((uchar_t *)ip6h + hdr_length); 1155 1156 if (mctl_present) 1157 first_mp->b_cont = mp; 1158 else 1159 first_mp = mp; 1160 } 1161 1162 /* 1163 * Need to set db_type back to M_DATA before 1164 * refeeding mp into this function. 1165 */ 1166 DB_TYPE(mp) = M_DATA; 1167 1168 /* 1169 * Copy the 3rd header + remaining data on top 1170 * of the 2nd header. 1171 */ 1172 bcopy(inner_ip6h, ip6h, 1173 mp->b_wptr - (uchar_t *)inner_ip6h); 1174 1175 /* 1176 * Subtract length of the 2nd header. 1177 */ 1178 mp->b_wptr -= hdr_length; 1179 1180 /* 1181 * Now recurse, and see what I _really_ should be 1182 * doing here. 1183 */ 1184 icmp_inbound_error_fanout_v6(q, first_mp, 1185 (ip6_t *)mp->b_rptr, icmp6, ill, mctl_present, 1186 zoneid); 1187 return; 1188 } 1189 /* FALLTHRU */ 1190 default: 1191 /* 1192 * The rip6h header is only used for the lookup and we 1193 * only set the src and dst addresses and nexthdr. 1194 */ 1195 rip6h.ip6_src = ip6h->ip6_dst; 1196 rip6h.ip6_dst = ip6h->ip6_src; 1197 rip6h.ip6_nxt = nexthdr; 1198 ip_fanout_proto_v6(q, first_mp, &rip6h, ill, ill, nexthdr, 0, 1199 IP6_NO_IPPOLICY, mctl_present, zoneid); 1200 return; 1201 } 1202 /* NOTREACHED */ 1203 drop_pkt: 1204 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInErrors); 1205 ip1dbg(("icmp_inbound_error_fanout_v6: drop pkt\n")); 1206 freemsg(first_mp); 1207 } 1208 1209 /* 1210 * Validate the incoming redirect message, if valid redirect 1211 * processing is done later. This is separated from the actual 1212 * redirect processing to avoid becoming single threaded when not 1213 * necessary. (i.e invalid packet) 1214 * Assumes that any AH or ESP headers have already been removed. 1215 * The mp has already been pulled up. 1216 */ 1217 boolean_t 1218 icmp_redirect_ok_v6(ill_t *ill, mblk_t *mp) 1219 { 1220 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 1221 nd_redirect_t *rd; 1222 ire_t *ire; 1223 uint16_t len; 1224 uint16_t hdr_length; 1225 1226 ASSERT(mp->b_cont == NULL); 1227 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 1228 hdr_length = ip_hdr_length_v6(mp, ip6h); 1229 else 1230 hdr_length = IPV6_HDR_LEN; 1231 rd = (nd_redirect_t *)&mp->b_rptr[hdr_length]; 1232 len = mp->b_wptr - mp->b_rptr - hdr_length; 1233 if (!IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src) || 1234 (ip6h->ip6_hops != IPV6_MAX_HOPS) || 1235 (rd->nd_rd_code != 0) || 1236 (len < sizeof (nd_redirect_t)) || 1237 (IN6_IS_ADDR_V4MAPPED(&rd->nd_rd_dst)) || 1238 (IN6_IS_ADDR_MULTICAST(&rd->nd_rd_dst))) { 1239 return (B_FALSE); 1240 } 1241 if (!(IN6_IS_ADDR_LINKLOCAL(&rd->nd_rd_target) || 1242 IN6_ARE_ADDR_EQUAL(&rd->nd_rd_target, &rd->nd_rd_dst))) { 1243 return (B_FALSE); 1244 } 1245 1246 /* 1247 * Verify that the IP source address of the redirect is 1248 * the same as the current first-hop router for the specified 1249 * ICMP destination address. Just to be cautious, this test 1250 * will be done again before we add the redirect, in case 1251 * router goes away between now and then. 1252 */ 1253 ire = ire_route_lookup_v6(&rd->nd_rd_dst, 0, 1254 &ip6h->ip6_src, 0, ill->ill_ipif, NULL, ALL_ZONES, NULL, 1255 MATCH_IRE_GW | MATCH_IRE_ILL_GROUP); 1256 if (ire == NULL) 1257 return (B_FALSE); 1258 ire_refrele(ire); 1259 if (len > sizeof (nd_redirect_t)) { 1260 if (!ndp_verify_optlen((nd_opt_hdr_t *)&rd[1], 1261 len - sizeof (nd_redirect_t))) 1262 return (B_FALSE); 1263 } 1264 return (B_TRUE); 1265 } 1266 1267 /* 1268 * Process received IPv6 ICMP Redirect messages. 1269 * Assumes that the icmp packet has already been verfied to be 1270 * valid, aligned and in a single mblk all done in icmp_redirect_ok_v6(). 1271 */ 1272 /* ARGSUSED */ 1273 static void 1274 icmp_redirect_v6(queue_t *q, mblk_t *mp, ill_t *ill) 1275 { 1276 ip6_t *ip6h; 1277 uint16_t hdr_length; 1278 nd_redirect_t *rd; 1279 ire_t *ire; 1280 ire_t *prev_ire; 1281 ire_t *redir_ire; 1282 in6_addr_t *src, *dst, *gateway; 1283 nd_opt_hdr_t *opt; 1284 nce_t *nce; 1285 int nce_flags = 0; 1286 int err = 0; 1287 boolean_t redirect_to_router = B_FALSE; 1288 int len; 1289 iulp_t ulp_info = { 0 }; 1290 ill_t *prev_ire_ill; 1291 ipif_t *ipif; 1292 1293 ip6h = (ip6_t *)mp->b_rptr; 1294 if (ip6h->ip6_nxt != IPPROTO_ICMPV6) 1295 hdr_length = ip_hdr_length_v6(mp, ip6h); 1296 else 1297 hdr_length = IPV6_HDR_LEN; 1298 1299 rd = (nd_redirect_t *)&mp->b_rptr[hdr_length]; 1300 src = &ip6h->ip6_src; 1301 dst = &rd->nd_rd_dst; 1302 gateway = &rd->nd_rd_target; 1303 if (!IN6_ARE_ADDR_EQUAL(gateway, dst)) { 1304 redirect_to_router = B_TRUE; 1305 nce_flags |= NCE_F_ISROUTER; 1306 } 1307 /* 1308 * Make sure we had a route for the dest in question and that 1309 * route was pointing to the old gateway (the source of the 1310 * redirect packet.) 1311 */ 1312 ipif = ipif_get_next_ipif(NULL, ill); 1313 if (ipif == NULL) { 1314 freemsg(mp); 1315 return; 1316 } 1317 prev_ire = ire_route_lookup_v6(dst, 0, src, 0, ipif, NULL, 1318 ALL_ZONES, NULL, MATCH_IRE_GW | MATCH_IRE_ILL_GROUP); 1319 ipif_refrele(ipif); 1320 /* 1321 * Check that 1322 * the redirect was not from ourselves 1323 * old gateway is still directly reachable 1324 */ 1325 if (prev_ire == NULL || 1326 prev_ire->ire_type == IRE_LOCAL) { 1327 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInBadRedirects); 1328 goto fail_redirect; 1329 } 1330 prev_ire_ill = ire_to_ill(prev_ire); 1331 ASSERT(prev_ire_ill != NULL); 1332 if (prev_ire_ill->ill_flags & ILLF_NONUD) 1333 nce_flags |= NCE_F_NONUD; 1334 1335 /* 1336 * Should we use the old ULP info to create the new gateway? From 1337 * a user's perspective, we should inherit the info so that it 1338 * is a "smooth" transition. If we do not do that, then new 1339 * connections going thru the new gateway will have no route metrics, 1340 * which is counter-intuitive to user. From a network point of 1341 * view, this may or may not make sense even though the new gateway 1342 * is still directly connected to us so the route metrics should not 1343 * change much. 1344 * 1345 * But if the old ire_uinfo is not initialized, we do another 1346 * recursive lookup on the dest using the new gateway. There may 1347 * be a route to that. If so, use it to initialize the redirect 1348 * route. 1349 */ 1350 if (prev_ire->ire_uinfo.iulp_set) { 1351 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 1352 } else if (redirect_to_router) { 1353 /* 1354 * Only do the following if the redirection is really to 1355 * a router. 1356 */ 1357 ire_t *tmp_ire; 1358 ire_t *sire; 1359 1360 tmp_ire = ire_ftable_lookup_v6(dst, 0, gateway, 0, NULL, &sire, 1361 ALL_ZONES, 0, NULL, 1362 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT)); 1363 if (sire != NULL) { 1364 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t)); 1365 ASSERT(tmp_ire != NULL); 1366 ire_refrele(tmp_ire); 1367 ire_refrele(sire); 1368 } else if (tmp_ire != NULL) { 1369 bcopy(&tmp_ire->ire_uinfo, &ulp_info, 1370 sizeof (iulp_t)); 1371 ire_refrele(tmp_ire); 1372 } 1373 } 1374 1375 len = mp->b_wptr - mp->b_rptr - hdr_length - sizeof (nd_redirect_t); 1376 opt = (nd_opt_hdr_t *)&rd[1]; 1377 opt = ndp_get_option(opt, len, ND_OPT_TARGET_LINKADDR); 1378 if (opt != NULL) { 1379 err = ndp_lookup_then_add(ill, 1380 (uchar_t *)&opt[1], /* Link layer address */ 1381 gateway, 1382 &ipv6_all_ones, /* prefix mask */ 1383 &ipv6_all_zeros, /* Mapping mask */ 1384 0, 1385 nce_flags, 1386 ND_STALE, 1387 &nce); 1388 switch (err) { 1389 case 0: 1390 NCE_REFRELE(nce); 1391 break; 1392 case EEXIST: 1393 /* 1394 * Check to see if link layer address has changed and 1395 * process the nce_state accordingly. 1396 */ 1397 ndp_process(nce, (uchar_t *)&opt[1], 0, B_FALSE); 1398 NCE_REFRELE(nce); 1399 break; 1400 default: 1401 ip1dbg(("icmp_redirect_v6: NCE create failed %d\n", 1402 err)); 1403 goto fail_redirect; 1404 } 1405 } 1406 if (redirect_to_router) { 1407 /* icmp_redirect_ok_v6() must have already verified this */ 1408 ASSERT(IN6_IS_ADDR_LINKLOCAL(gateway)); 1409 1410 /* 1411 * Create a Route Association. This will allow us to remember 1412 * a router told us to use the particular gateway. 1413 */ 1414 ire = ire_create_v6( 1415 dst, 1416 &ipv6_all_ones, /* mask */ 1417 &prev_ire->ire_src_addr_v6, /* source addr */ 1418 gateway, /* gateway addr */ 1419 &prev_ire->ire_max_frag, /* max frag */ 1420 NULL, /* Fast Path header */ 1421 NULL, /* no rfq */ 1422 NULL, /* no stq */ 1423 IRE_HOST_REDIRECT, 1424 NULL, 1425 prev_ire->ire_ipif, 1426 NULL, 1427 0, 1428 0, 1429 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 1430 &ulp_info, 1431 NULL, 1432 NULL); 1433 } else { 1434 /* 1435 * Just create an on link entry, may or may not be a router 1436 * If there is no link layer address option ire_add() won't 1437 * add this. 1438 */ 1439 ire = ire_create_v6( 1440 dst, /* gateway == dst */ 1441 &ipv6_all_ones, /* mask */ 1442 &prev_ire->ire_src_addr_v6, /* source addr */ 1443 &ipv6_all_zeros, /* gateway addr */ 1444 &prev_ire->ire_max_frag, /* max frag */ 1445 NULL, /* Fast Path header */ 1446 prev_ire->ire_rfq, /* ire rfq */ 1447 prev_ire->ire_stq, /* ire stq */ 1448 IRE_CACHE, 1449 NULL, 1450 prev_ire->ire_ipif, 1451 &ipv6_all_ones, 1452 0, 1453 0, 1454 0, 1455 &ulp_info, 1456 NULL, 1457 NULL); 1458 } 1459 if (ire == NULL) 1460 goto fail_redirect; 1461 1462 /* 1463 * XXX If there is no nce i.e there is no target link layer address 1464 * option with the redirect message, ire_add will fail. In that 1465 * case we never add the IRE_CACHE/IRE_HOST_REDIRECT. We need 1466 * to fix this. 1467 */ 1468 if (ire_add(&ire, NULL, NULL, NULL) == 0) { 1469 1470 /* tell routing sockets that we received a redirect */ 1471 ip_rts_change_v6(RTM_REDIRECT, 1472 &rd->nd_rd_dst, 1473 &rd->nd_rd_target, 1474 &ipv6_all_ones, 0, &ire->ire_src_addr_v6, 1475 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0, 1476 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR)); 1477 1478 /* 1479 * Delete any existing IRE_HOST_REDIRECT for this destination. 1480 * This together with the added IRE has the effect of 1481 * modifying an existing redirect. 1482 */ 1483 redir_ire = ire_ftable_lookup_v6(dst, 0, src, IRE_HOST_REDIRECT, 1484 ire->ire_ipif, NULL, ALL_ZONES, 0, NULL, 1485 (MATCH_IRE_GW | MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP)); 1486 1487 ire_refrele(ire); /* Held in ire_add_v6 */ 1488 1489 if (redir_ire != NULL) { 1490 ire_delete(redir_ire); 1491 ire_refrele(redir_ire); 1492 } 1493 } 1494 1495 if (prev_ire->ire_type == IRE_CACHE) 1496 ire_delete(prev_ire); 1497 ire_refrele(prev_ire); 1498 prev_ire = NULL; 1499 1500 fail_redirect: 1501 if (prev_ire != NULL) 1502 ire_refrele(prev_ire); 1503 freemsg(mp); 1504 } 1505 1506 static ill_t * 1507 ip_queue_to_ill_v6(queue_t *q) 1508 { 1509 ill_t *ill; 1510 1511 ASSERT(WR(q) == q); 1512 1513 if (q->q_next != NULL) { 1514 ill = (ill_t *)q->q_ptr; 1515 if (ILL_CAN_LOOKUP(ill)) 1516 ill_refhold(ill); 1517 else 1518 ill = NULL; 1519 } else { 1520 ill = ill_lookup_on_name(ipif_loopback_name, B_FALSE, B_TRUE, 1521 NULL, NULL, NULL, NULL, NULL); 1522 } 1523 if (ill == NULL) 1524 ip0dbg(("ip_queue_to_ill_v6: no ill\n")); 1525 return (ill); 1526 } 1527 1528 /* 1529 * Assigns an appropriate source address to the packet. 1530 * If origdst is one of our IP addresses that use it as the source. 1531 * If the queue is an ill queue then select a source from that ill. 1532 * Otherwise pick a source based on a route lookup back to the origsrc. 1533 * 1534 * src is the return parameter. Returns a pointer to src or NULL if failure. 1535 */ 1536 static in6_addr_t * 1537 icmp_pick_source_v6(queue_t *wq, in6_addr_t *origsrc, in6_addr_t *origdst, 1538 in6_addr_t *src) 1539 { 1540 ill_t *ill; 1541 ire_t *ire; 1542 ipif_t *ipif; 1543 zoneid_t zoneid; 1544 1545 ASSERT(!(wq->q_flag & QREADR)); 1546 if (wq->q_next != NULL) { 1547 ill = (ill_t *)wq->q_ptr; 1548 zoneid = GLOBAL_ZONEID; 1549 } else { 1550 ill = NULL; 1551 zoneid = Q_TO_CONN(wq)->conn_zoneid; 1552 } 1553 1554 ire = ire_route_lookup_v6(origdst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK), 1555 NULL, NULL, zoneid, NULL, (MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY)); 1556 if (ire != NULL) { 1557 /* Destined to one of our addresses */ 1558 *src = *origdst; 1559 ire_refrele(ire); 1560 return (src); 1561 } 1562 if (ire != NULL) { 1563 ire_refrele(ire); 1564 ire = NULL; 1565 } 1566 if (ill == NULL) { 1567 /* What is the route back to the original source? */ 1568 ire = ire_route_lookup_v6(origsrc, 0, 0, 0, 1569 NULL, NULL, zoneid, NULL, 1570 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE)); 1571 if (ire == NULL) { 1572 BUMP_MIB(&ip6_mib, ipv6OutNoRoutes); 1573 return (NULL); 1574 } 1575 /* 1576 * Does not matter whether we use ire_stq or ire_ipif here. 1577 * Just pick an ill for ICMP replies. 1578 */ 1579 ASSERT(ire->ire_ipif != NULL); 1580 ill = ire->ire_ipif->ipif_ill; 1581 ire_refrele(ire); 1582 } 1583 ipif = ipif_select_source_v6(ill, origsrc, RESTRICT_TO_NONE, 1584 IPV6_PREFER_SRC_DEFAULT, zoneid); 1585 if (ipif != NULL) { 1586 *src = ipif->ipif_v6src_addr; 1587 ipif_refrele(ipif); 1588 return (src); 1589 } 1590 /* 1591 * Unusual case - can't find a usable source address to reach the 1592 * original source. Use what in the route to the source. 1593 */ 1594 ire = ire_route_lookup_v6(origsrc, 0, 0, 0, 1595 NULL, NULL, zoneid, NULL, 1596 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE)); 1597 if (ire == NULL) { 1598 BUMP_MIB(&ip6_mib, ipv6OutNoRoutes); 1599 return (NULL); 1600 } 1601 ASSERT(ire != NULL); 1602 *src = ire->ire_src_addr_v6; 1603 ire_refrele(ire); 1604 return (src); 1605 } 1606 1607 /* 1608 * Build and ship an IPv6 ICMP message using the packet data in mp, 1609 * and the ICMP header pointed to by "stuff". (May be called as 1610 * writer.) 1611 * Note: assumes that icmp_pkt_err_ok_v6 has been called to 1612 * verify that an icmp error packet can be sent. 1613 * 1614 * If q is an ill write side queue (which is the case when packets 1615 * arrive from ip_rput) then ip_wput code will ensure that packets to 1616 * link-local destinations are sent out that ill. 1617 * 1618 * If v6src_ptr is set use it as a source. Otherwise select a reasonable 1619 * source address (see above function). 1620 */ 1621 static void 1622 icmp_pkt_v6(queue_t *q, mblk_t *mp, void *stuff, size_t len, 1623 const in6_addr_t *v6src_ptr, boolean_t mctl_present) 1624 { 1625 ip6_t *ip6h; 1626 in6_addr_t v6dst; 1627 size_t len_needed; 1628 size_t msg_len; 1629 mblk_t *mp1; 1630 icmp6_t *icmp6; 1631 ill_t *ill; 1632 in6_addr_t v6src; 1633 mblk_t *ipsec_mp; 1634 ipsec_out_t *io; 1635 1636 ill = ip_queue_to_ill_v6(q); 1637 if (ill == NULL) { 1638 freemsg(mp); 1639 return; 1640 } 1641 1642 if (mctl_present) { 1643 /* 1644 * If it is : 1645 * 1646 * 1) a IPSEC_OUT, then this is caused by outbound 1647 * datagram originating on this host. IPSEC processing 1648 * may or may not have been done. Refer to comments above 1649 * icmp_inbound_error_fanout for details. 1650 * 1651 * 2) a IPSEC_IN if we are generating a icmp_message 1652 * for an incoming datagram destined for us i.e called 1653 * from ip_fanout_send_icmp. 1654 */ 1655 ipsec_info_t *in; 1656 1657 ipsec_mp = mp; 1658 mp = ipsec_mp->b_cont; 1659 1660 in = (ipsec_info_t *)ipsec_mp->b_rptr; 1661 ip6h = (ip6_t *)mp->b_rptr; 1662 1663 ASSERT(in->ipsec_info_type == IPSEC_OUT || 1664 in->ipsec_info_type == IPSEC_IN); 1665 1666 if (in->ipsec_info_type == IPSEC_IN) { 1667 /* 1668 * Convert the IPSEC_IN to IPSEC_OUT. 1669 */ 1670 if (!ipsec_in_to_out(ipsec_mp, NULL, ip6h)) { 1671 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 1672 ill_refrele(ill); 1673 return; 1674 } 1675 } else { 1676 ASSERT(in->ipsec_info_type == IPSEC_OUT); 1677 io = (ipsec_out_t *)in; 1678 /* 1679 * Clear out ipsec_out_proc_begin, so we do a fresh 1680 * ire lookup. 1681 */ 1682 io->ipsec_out_proc_begin = B_FALSE; 1683 } 1684 } else { 1685 /* 1686 * This is in clear. The icmp message we are building 1687 * here should go out in clear. 1688 */ 1689 ipsec_in_t *ii; 1690 ASSERT(mp->b_datap->db_type == M_DATA); 1691 if ((ipsec_mp = ipsec_in_alloc(B_FALSE)) == NULL) { 1692 freemsg(mp); 1693 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 1694 ill_refrele(ill); 1695 return; 1696 } 1697 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 1698 1699 /* This is not a secure packet */ 1700 ii->ipsec_in_secure = B_FALSE; 1701 ipsec_mp->b_cont = mp; 1702 ip6h = (ip6_t *)mp->b_rptr; 1703 /* 1704 * Convert the IPSEC_IN to IPSEC_OUT. 1705 */ 1706 if (!ipsec_in_to_out(ipsec_mp, NULL, ip6h)) { 1707 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 1708 ill_refrele(ill); 1709 return; 1710 } 1711 } 1712 io = (ipsec_out_t *)ipsec_mp->b_rptr; 1713 1714 if (v6src_ptr != NULL) { 1715 v6src = *v6src_ptr; 1716 } else { 1717 if (icmp_pick_source_v6(q, &ip6h->ip6_src, &ip6h->ip6_dst, 1718 &v6src) == NULL) { 1719 freemsg(ipsec_mp); 1720 ill_refrele(ill); 1721 return; 1722 } 1723 } 1724 v6dst = ip6h->ip6_src; 1725 len_needed = ipv6_icmp_return - IPV6_HDR_LEN - len; 1726 msg_len = msgdsize(mp); 1727 if (msg_len > len_needed) { 1728 if (!adjmsg(mp, len_needed - msg_len)) { 1729 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutErrors); 1730 freemsg(ipsec_mp); 1731 ill_refrele(ill); 1732 return; 1733 } 1734 msg_len = len_needed; 1735 } 1736 mp1 = allocb(IPV6_HDR_LEN + len, BPRI_HI); 1737 if (mp1 == NULL) { 1738 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutErrors); 1739 freemsg(ipsec_mp); 1740 ill_refrele(ill); 1741 return; 1742 } 1743 ill_refrele(ill); 1744 mp1->b_cont = mp; 1745 mp = mp1; 1746 ASSERT(ipsec_mp->b_datap->db_type == M_CTL && 1747 io->ipsec_out_type == IPSEC_OUT); 1748 ipsec_mp->b_cont = mp; 1749 1750 /* 1751 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this 1752 * node generates be accepted in peace by all on-host destinations. 1753 * If we do NOT assume that all on-host destinations trust 1754 * self-generated ICMP messages, then rework here, ip.c, and spd.c. 1755 * (Look for ipsec_out_icmp_loopback). 1756 */ 1757 io->ipsec_out_icmp_loopback = B_TRUE; 1758 1759 ip6h = (ip6_t *)mp->b_rptr; 1760 mp1->b_wptr = (uchar_t *)ip6h + (IPV6_HDR_LEN + len); 1761 1762 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 1763 ip6h->ip6_nxt = IPPROTO_ICMPV6; 1764 ip6h->ip6_hops = ipv6_def_hops; 1765 ip6h->ip6_dst = v6dst; 1766 ip6h->ip6_src = v6src; 1767 msg_len += IPV6_HDR_LEN + len; 1768 if (msg_len > IP_MAXPACKET + IPV6_HDR_LEN) { 1769 (void) adjmsg(mp, IP_MAXPACKET + IPV6_HDR_LEN - msg_len); 1770 msg_len = IP_MAXPACKET + IPV6_HDR_LEN; 1771 } 1772 ip6h->ip6_plen = htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN)); 1773 icmp6 = (icmp6_t *)&ip6h[1]; 1774 bcopy(stuff, (char *)icmp6, len); 1775 /* 1776 * Prepare for checksum by putting icmp length in the icmp 1777 * checksum field. The checksum is calculated in ip_wput_v6. 1778 */ 1779 icmp6->icmp6_cksum = ip6h->ip6_plen; 1780 if (icmp6->icmp6_type == ND_REDIRECT) { 1781 ip6h->ip6_hops = IPV6_MAX_HOPS; 1782 } 1783 /* Send to V6 writeside put routine */ 1784 put(q, ipsec_mp); 1785 } 1786 1787 /* 1788 * Update the output mib when ICMPv6 packets are sent. 1789 */ 1790 static void 1791 icmp_update_out_mib_v6(ill_t *ill, icmp6_t *icmp6) 1792 { 1793 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutMsgs); 1794 1795 switch (icmp6->icmp6_type) { 1796 case ICMP6_DST_UNREACH: 1797 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutDestUnreachs); 1798 if (icmp6->icmp6_code == ICMP6_DST_UNREACH_ADMIN) 1799 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutAdminProhibs); 1800 break; 1801 1802 case ICMP6_TIME_EXCEEDED: 1803 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutTimeExcds); 1804 break; 1805 1806 case ICMP6_PARAM_PROB: 1807 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutParmProblems); 1808 break; 1809 1810 case ICMP6_PACKET_TOO_BIG: 1811 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutPktTooBigs); 1812 break; 1813 1814 case ICMP6_ECHO_REQUEST: 1815 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutEchos); 1816 break; 1817 1818 case ICMP6_ECHO_REPLY: 1819 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutEchoReplies); 1820 break; 1821 1822 case ND_ROUTER_SOLICIT: 1823 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutRouterSolicits); 1824 break; 1825 1826 case ND_ROUTER_ADVERT: 1827 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutRouterAdvertisements); 1828 break; 1829 1830 case ND_NEIGHBOR_SOLICIT: 1831 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutNeighborSolicits); 1832 break; 1833 1834 case ND_NEIGHBOR_ADVERT: 1835 BUMP_MIB(ill->ill_icmp6_mib, 1836 ipv6IfIcmpOutNeighborAdvertisements); 1837 break; 1838 1839 case ND_REDIRECT: 1840 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutRedirects); 1841 break; 1842 1843 case MLD_LISTENER_QUERY: 1844 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutGroupMembQueries); 1845 break; 1846 1847 case MLD_LISTENER_REPORT: 1848 case MLD_V2_LISTENER_REPORT: 1849 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutGroupMembResponses); 1850 break; 1851 1852 case MLD_LISTENER_REDUCTION: 1853 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpOutGroupMembReductions); 1854 break; 1855 } 1856 } 1857 1858 /* 1859 * Check if it is ok to send an ICMPv6 error packet in 1860 * response to the IP packet in mp. 1861 * Free the message and return null if no 1862 * ICMP error packet should be sent. 1863 */ 1864 static mblk_t * 1865 icmp_pkt_err_ok_v6(queue_t *q, mblk_t *mp, 1866 boolean_t llbcast, boolean_t mcast_ok) 1867 { 1868 ip6_t *ip6h; 1869 1870 if (!mp) 1871 return (NULL); 1872 1873 ip6h = (ip6_t *)mp->b_rptr; 1874 1875 /* Check if source address uniquely identifies the host */ 1876 1877 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src) || 1878 IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_src) || 1879 IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) { 1880 freemsg(mp); 1881 return (NULL); 1882 } 1883 1884 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 1885 size_t len_needed = IPV6_HDR_LEN + ICMP6_MINLEN; 1886 icmp6_t *icmp6; 1887 1888 if (mp->b_wptr - mp->b_rptr < len_needed) { 1889 if (!pullupmsg(mp, len_needed)) { 1890 ill_t *ill; 1891 1892 ill = ip_queue_to_ill_v6(q); 1893 if (ill == NULL) { 1894 BUMP_MIB(&icmp6_mib, 1895 ipv6IfIcmpInErrors); 1896 } else { 1897 BUMP_MIB(ill->ill_icmp6_mib, 1898 ipv6IfIcmpInErrors); 1899 ill_refrele(ill); 1900 } 1901 freemsg(mp); 1902 return (NULL); 1903 } 1904 ip6h = (ip6_t *)mp->b_rptr; 1905 } 1906 icmp6 = (icmp6_t *)&ip6h[1]; 1907 /* Explicitly do not generate errors in response to redirects */ 1908 if (ICMP6_IS_ERROR(icmp6->icmp6_type) || 1909 icmp6->icmp6_type == ND_REDIRECT) { 1910 freemsg(mp); 1911 return (NULL); 1912 } 1913 } 1914 /* 1915 * Check that the destination is not multicast and that the packet 1916 * was not sent on link layer broadcast or multicast. (Exception 1917 * is Packet too big message as per the draft - when mcast_ok is set.) 1918 */ 1919 if (!mcast_ok && 1920 (llbcast || IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst))) { 1921 freemsg(mp); 1922 return (NULL); 1923 } 1924 if (icmp_err_rate_limit()) { 1925 /* 1926 * Only send ICMP error packets every so often. 1927 * This should be done on a per port/source basis, 1928 * but for now this will suffice. 1929 */ 1930 freemsg(mp); 1931 return (NULL); 1932 } 1933 return (mp); 1934 } 1935 1936 /* 1937 * Generate an ICMPv6 redirect message. 1938 * Include target link layer address option if it exits. 1939 * Always include redirect header. 1940 */ 1941 static void 1942 icmp_send_redirect_v6(queue_t *q, mblk_t *mp, in6_addr_t *targetp, 1943 in6_addr_t *dest, ill_t *ill, boolean_t llbcast) 1944 { 1945 nd_redirect_t *rd; 1946 nd_opt_rd_hdr_t *rdh; 1947 uchar_t *buf; 1948 nce_t *nce = NULL; 1949 nd_opt_hdr_t *opt; 1950 int len; 1951 int ll_opt_len = 0; 1952 int max_redir_hdr_data_len; 1953 int pkt_len; 1954 in6_addr_t *srcp; 1955 1956 /* 1957 * We are called from ip_rput where we could 1958 * not have attached an IPSEC_IN. 1959 */ 1960 ASSERT(mp->b_datap->db_type == M_DATA); 1961 1962 mp = icmp_pkt_err_ok_v6(q, mp, llbcast, B_FALSE); 1963 if (mp == NULL) 1964 return; 1965 nce = ndp_lookup(ill, targetp, B_FALSE); 1966 if (nce != NULL && nce->nce_state != ND_INCOMPLETE) { 1967 ll_opt_len = (sizeof (nd_opt_hdr_t) + 1968 ill->ill_phys_addr_length + 7)/8 * 8; 1969 } 1970 len = sizeof (nd_redirect_t) + sizeof (nd_opt_rd_hdr_t) + ll_opt_len; 1971 ASSERT(len % 4 == 0); 1972 buf = kmem_alloc(len, KM_NOSLEEP); 1973 if (buf == NULL) { 1974 if (nce != NULL) 1975 NCE_REFRELE(nce); 1976 freemsg(mp); 1977 return; 1978 } 1979 1980 rd = (nd_redirect_t *)buf; 1981 rd->nd_rd_type = (uint8_t)ND_REDIRECT; 1982 rd->nd_rd_code = 0; 1983 rd->nd_rd_reserved = 0; 1984 rd->nd_rd_target = *targetp; 1985 rd->nd_rd_dst = *dest; 1986 1987 opt = (nd_opt_hdr_t *)(buf + sizeof (nd_redirect_t)); 1988 if (nce != NULL && ll_opt_len != 0) { 1989 opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; 1990 opt->nd_opt_len = ll_opt_len/8; 1991 bcopy((char *)nce->nce_res_mp->b_rptr + 1992 NCE_LL_ADDR_OFFSET(ill), &opt[1], 1993 ill->ill_phys_addr_length); 1994 } 1995 if (nce != NULL) 1996 NCE_REFRELE(nce); 1997 rdh = (nd_opt_rd_hdr_t *)(buf + sizeof (nd_redirect_t) + ll_opt_len); 1998 rdh->nd_opt_rh_type = (uint8_t)ND_OPT_REDIRECTED_HEADER; 1999 /* max_redir_hdr_data_len and nd_opt_rh_len must be multiple of 8 */ 2000 max_redir_hdr_data_len = (ipv6_icmp_return - IPV6_HDR_LEN - len)/8*8; 2001 pkt_len = msgdsize(mp); 2002 /* Make sure mp is 8 byte aligned */ 2003 if (pkt_len > max_redir_hdr_data_len) { 2004 rdh->nd_opt_rh_len = (max_redir_hdr_data_len + 2005 sizeof (nd_opt_rd_hdr_t))/8; 2006 (void) adjmsg(mp, max_redir_hdr_data_len - pkt_len); 2007 } else { 2008 rdh->nd_opt_rh_len = (pkt_len + sizeof (nd_opt_rd_hdr_t))/8; 2009 (void) adjmsg(mp, -(pkt_len % 8)); 2010 } 2011 rdh->nd_opt_rh_reserved1 = 0; 2012 rdh->nd_opt_rh_reserved2 = 0; 2013 /* ipif_v6src_addr contains the link-local source address */ 2014 rw_enter(&ill_g_lock, RW_READER); 2015 if (ill->ill_group != NULL) { 2016 /* 2017 * The receiver of the redirect will verify whether it 2018 * had a route through us (srcp that we will use in 2019 * the redirect) or not. As we load spread even link-locals, 2020 * we don't know which source address the receiver of 2021 * redirect has in its route for communicating with us. 2022 * Thus we randomly choose a source here and finally we 2023 * should get to the right one and it will eventually 2024 * accept the redirect from us. We can't call 2025 * ip_lookup_scope_v6 because we don't have the right 2026 * link-local address here. Thus we randomly choose one. 2027 */ 2028 int cnt = ill->ill_group->illgrp_ill_count; 2029 2030 ill = ill->ill_group->illgrp_ill; 2031 cnt = ++icmp_redirect_v6_src_index % cnt; 2032 while (cnt--) 2033 ill = ill->ill_group_next; 2034 srcp = &ill->ill_ipif->ipif_v6src_addr; 2035 } else { 2036 srcp = &ill->ill_ipif->ipif_v6src_addr; 2037 } 2038 rw_exit(&ill_g_lock); 2039 icmp_pkt_v6(q, mp, buf, len, srcp, B_FALSE); 2040 kmem_free(buf, len); 2041 } 2042 2043 2044 /* Generate an ICMP time exceeded message. (May be called as writer.) */ 2045 void 2046 icmp_time_exceeded_v6(queue_t *q, mblk_t *mp, uint8_t code, 2047 boolean_t llbcast, boolean_t mcast_ok) 2048 { 2049 icmp6_t icmp6; 2050 boolean_t mctl_present; 2051 mblk_t *first_mp; 2052 2053 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 2054 2055 mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok); 2056 if (mp == NULL) { 2057 if (mctl_present) 2058 freeb(first_mp); 2059 return; 2060 } 2061 bzero(&icmp6, sizeof (icmp6_t)); 2062 icmp6.icmp6_type = ICMP6_TIME_EXCEEDED; 2063 icmp6.icmp6_code = code; 2064 icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present); 2065 } 2066 2067 /* 2068 * Generate an ICMP unreachable message. 2069 */ 2070 void 2071 icmp_unreachable_v6(queue_t *q, mblk_t *mp, uint8_t code, 2072 boolean_t llbcast, boolean_t mcast_ok) 2073 { 2074 icmp6_t icmp6; 2075 boolean_t mctl_present; 2076 mblk_t *first_mp; 2077 2078 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 2079 2080 mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok); 2081 if (mp == NULL) { 2082 if (mctl_present) 2083 freeb(first_mp); 2084 return; 2085 } 2086 bzero(&icmp6, sizeof (icmp6_t)); 2087 icmp6.icmp6_type = ICMP6_DST_UNREACH; 2088 icmp6.icmp6_code = code; 2089 icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present); 2090 } 2091 2092 /* 2093 * Generate an ICMP pkt too big message. 2094 */ 2095 static void 2096 icmp_pkt2big_v6(queue_t *q, mblk_t *mp, uint32_t mtu, 2097 boolean_t llbcast, boolean_t mcast_ok) 2098 { 2099 icmp6_t icmp6; 2100 mblk_t *first_mp; 2101 boolean_t mctl_present; 2102 2103 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 2104 2105 mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok); 2106 if (mp == NULL) { 2107 if (mctl_present) 2108 freeb(first_mp); 2109 return; 2110 } 2111 bzero(&icmp6, sizeof (icmp6_t)); 2112 icmp6.icmp6_type = ICMP6_PACKET_TOO_BIG; 2113 icmp6.icmp6_code = 0; 2114 icmp6.icmp6_mtu = htonl(mtu); 2115 2116 icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present); 2117 } 2118 2119 /* 2120 * Generate an ICMP parameter problem message. (May be called as writer.) 2121 * 'offset' is the offset from the beginning of the packet in error. 2122 */ 2123 static void 2124 icmp_param_problem_v6(queue_t *q, mblk_t *mp, uint8_t code, 2125 uint32_t offset, boolean_t llbcast, boolean_t mcast_ok) 2126 { 2127 icmp6_t icmp6; 2128 boolean_t mctl_present; 2129 mblk_t *first_mp; 2130 2131 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 2132 2133 mp = icmp_pkt_err_ok_v6(q, mp, llbcast, mcast_ok); 2134 if (mp == NULL) { 2135 if (mctl_present) 2136 freeb(first_mp); 2137 return; 2138 } 2139 bzero((char *)&icmp6, sizeof (icmp6_t)); 2140 icmp6.icmp6_type = ICMP6_PARAM_PROB; 2141 icmp6.icmp6_code = code; 2142 icmp6.icmp6_pptr = htonl(offset); 2143 icmp_pkt_v6(q, first_mp, &icmp6, sizeof (icmp6_t), NULL, mctl_present); 2144 } 2145 2146 /* 2147 * This code will need to take into account the possibility of binding 2148 * to a link local address on a multi-homed host, in which case the 2149 * outgoing interface (from the conn) will need to be used when getting 2150 * an ire for the dst. Going through proper outgoing interface and 2151 * choosing the source address corresponding to the outgoing interface 2152 * is necessary when the destination address is a link-local address and 2153 * IPV6_BOUND_IF or IPV6_PKTINFO or scope_id has been set. 2154 * This can happen when active connection is setup; thus ipp pointer 2155 * is passed here from tcp_connect_*() routines, in non-TCP cases NULL 2156 * pointer is passed as ipp pointer. 2157 */ 2158 mblk_t * 2159 ip_bind_v6(queue_t *q, mblk_t *mp, conn_t *connp, ip6_pkt_t *ipp) 2160 { 2161 ssize_t len; 2162 int protocol; 2163 struct T_bind_req *tbr; 2164 sin6_t *sin6; 2165 ipa6_conn_t *ac6; 2166 in6_addr_t *v6srcp; 2167 in6_addr_t *v6dstp; 2168 uint16_t lport; 2169 uint16_t fport; 2170 uchar_t *ucp; 2171 mblk_t *mp1; 2172 boolean_t ire_requested; 2173 boolean_t ipsec_policy_set; 2174 int error = 0; 2175 boolean_t local_bind; 2176 boolean_t orig_pkt_isv6 = connp->conn_pkt_isv6; 2177 ipa6_conn_x_t *acx6; 2178 boolean_t verify_dst; 2179 2180 ASSERT(connp->conn_af_isv6); 2181 len = mp->b_wptr - mp->b_rptr; 2182 if (len < (sizeof (*tbr) + 1)) { 2183 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE, 2184 "ip_bind_v6: bogus msg, len %ld", len); 2185 goto bad_addr; 2186 } 2187 /* Back up and extract the protocol identifier. */ 2188 mp->b_wptr--; 2189 tbr = (struct T_bind_req *)mp->b_rptr; 2190 /* Reset the message type in preparation for shipping it back. */ 2191 mp->b_datap->db_type = M_PCPROTO; 2192 2193 protocol = *mp->b_wptr & 0xFF; 2194 connp->conn_ulp = (uint8_t)protocol; 2195 2196 /* 2197 * Check for a zero length address. This is from a protocol that 2198 * wants to register to receive all packets of its type. 2199 */ 2200 if (tbr->ADDR_length == 0) { 2201 if ((protocol == IPPROTO_TCP || protocol == IPPROTO_SCTP || 2202 protocol == IPPROTO_ESP || protocol == IPPROTO_AH) && 2203 ipcl_proto_fanout_v6[protocol].connf_head != NULL) { 2204 /* 2205 * TCP, SCTP, AH, and ESP have single protocol fanouts. 2206 * Do not allow others to bind to these. 2207 */ 2208 goto bad_addr; 2209 } 2210 2211 /* 2212 * 2213 * The udp module never sends down a zero-length address, 2214 * and allowing this on a labeled system will break MLP 2215 * functionality. 2216 */ 2217 if (is_system_labeled() && protocol == IPPROTO_UDP) 2218 goto bad_addr; 2219 2220 /* Allow ipsec plumbing */ 2221 if (connp->conn_mac_exempt && protocol != IPPROTO_AH && 2222 protocol != IPPROTO_ESP) 2223 goto bad_addr; 2224 2225 connp->conn_srcv6 = ipv6_all_zeros; 2226 ipcl_proto_insert_v6(connp, protocol); 2227 2228 tbr->PRIM_type = T_BIND_ACK; 2229 return (mp); 2230 } 2231 2232 /* Extract the address pointer from the message. */ 2233 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset, 2234 tbr->ADDR_length); 2235 if (ucp == NULL) { 2236 ip1dbg(("ip_bind_v6: no address\n")); 2237 goto bad_addr; 2238 } 2239 if (!OK_32PTR(ucp)) { 2240 ip1dbg(("ip_bind_v6: unaligned address\n")); 2241 goto bad_addr; 2242 } 2243 mp1 = mp->b_cont; /* trailing mp if any */ 2244 ire_requested = (mp1 && mp1->b_datap->db_type == IRE_DB_REQ_TYPE); 2245 ipsec_policy_set = (mp1 && mp1->b_datap->db_type == IPSEC_POLICY_SET); 2246 2247 switch (tbr->ADDR_length) { 2248 default: 2249 ip1dbg(("ip_bind_v6: bad address length %d\n", 2250 (int)tbr->ADDR_length)); 2251 goto bad_addr; 2252 2253 case IPV6_ADDR_LEN: 2254 /* Verification of local address only */ 2255 v6srcp = (in6_addr_t *)ucp; 2256 lport = 0; 2257 local_bind = B_TRUE; 2258 break; 2259 2260 case sizeof (sin6_t): 2261 sin6 = (sin6_t *)ucp; 2262 v6srcp = &sin6->sin6_addr; 2263 lport = sin6->sin6_port; 2264 local_bind = B_TRUE; 2265 break; 2266 2267 case sizeof (ipa6_conn_t): 2268 /* 2269 * Verify that both the source and destination addresses 2270 * are valid. 2271 * Note that we allow connect to broadcast and multicast 2272 * addresses when ire_requested is set. Thus the ULP 2273 * has to check for IRE_BROADCAST and multicast. 2274 */ 2275 ac6 = (ipa6_conn_t *)ucp; 2276 v6srcp = &ac6->ac6_laddr; 2277 v6dstp = &ac6->ac6_faddr; 2278 fport = ac6->ac6_fport; 2279 /* For raw socket, the local port is not set. */ 2280 lport = ac6->ac6_lport != 0 ? ac6->ac6_lport : 2281 connp->conn_lport; 2282 local_bind = B_FALSE; 2283 /* Always verify destination reachability. */ 2284 verify_dst = B_TRUE; 2285 break; 2286 2287 case sizeof (ipa6_conn_x_t): 2288 /* 2289 * Verify that the source address is valid. 2290 * Note that we allow connect to broadcast and multicast 2291 * addresses when ire_requested is set. Thus the ULP 2292 * has to check for IRE_BROADCAST and multicast. 2293 */ 2294 acx6 = (ipa6_conn_x_t *)ucp; 2295 ac6 = &acx6->ac6x_conn; 2296 v6srcp = &ac6->ac6_laddr; 2297 v6dstp = &ac6->ac6_faddr; 2298 fport = ac6->ac6_fport; 2299 lport = ac6->ac6_lport; 2300 local_bind = B_FALSE; 2301 /* 2302 * Client that passed ipa6_conn_x_t to us specifies whether to 2303 * verify destination reachability. 2304 */ 2305 verify_dst = (acx6->ac6x_flags & ACX_VERIFY_DST) != 0; 2306 break; 2307 } 2308 if (local_bind) { 2309 if (IN6_IS_ADDR_V4MAPPED(v6srcp) && !connp->conn_ipv6_v6only) { 2310 /* Bind to IPv4 address */ 2311 ipaddr_t v4src; 2312 2313 IN6_V4MAPPED_TO_IPADDR(v6srcp, v4src); 2314 2315 error = ip_bind_laddr(connp, mp, v4src, lport, 2316 ire_requested, ipsec_policy_set, 2317 tbr->ADDR_length != IPV6_ADDR_LEN); 2318 if (error != 0) 2319 goto bad_addr; 2320 connp->conn_pkt_isv6 = B_FALSE; 2321 } else { 2322 if (IN6_IS_ADDR_V4MAPPED(v6srcp)) { 2323 error = 0; 2324 goto bad_addr; 2325 } 2326 error = ip_bind_laddr_v6(connp, mp, v6srcp, lport, 2327 ire_requested, ipsec_policy_set, 2328 (tbr->ADDR_length != IPV6_ADDR_LEN)); 2329 if (error != 0) 2330 goto bad_addr; 2331 connp->conn_pkt_isv6 = B_TRUE; 2332 } 2333 if (protocol == IPPROTO_TCP) 2334 connp->conn_recv = tcp_conn_request; 2335 } else { 2336 /* 2337 * Bind to local and remote address. Local might be 2338 * unspecified in which case it will be extracted from 2339 * ire_src_addr_v6 2340 */ 2341 if (IN6_IS_ADDR_V4MAPPED(v6dstp) && !connp->conn_ipv6_v6only) { 2342 /* Connect to IPv4 address */ 2343 ipaddr_t v4src; 2344 ipaddr_t v4dst; 2345 2346 /* Is the source unspecified or mapped? */ 2347 if (!IN6_IS_ADDR_V4MAPPED(v6srcp) && 2348 !IN6_IS_ADDR_UNSPECIFIED(v6srcp)) { 2349 ip1dbg(("ip_bind_v6: " 2350 "dst is mapped, but not the src\n")); 2351 goto bad_addr; 2352 } 2353 IN6_V4MAPPED_TO_IPADDR(v6srcp, v4src); 2354 IN6_V4MAPPED_TO_IPADDR(v6dstp, v4dst); 2355 2356 /* 2357 * XXX Fix needed. Need to pass ipsec_policy_set 2358 * instead of B_FALSE. 2359 */ 2360 2361 /* Always verify destination reachability. */ 2362 error = ip_bind_connected(connp, mp, &v4src, lport, 2363 v4dst, fport, ire_requested, ipsec_policy_set, 2364 B_TRUE, B_TRUE); 2365 if (error != 0) 2366 goto bad_addr; 2367 IN6_IPADDR_TO_V4MAPPED(v4src, v6srcp); 2368 connp->conn_pkt_isv6 = B_FALSE; 2369 } else if (IN6_IS_ADDR_V4MAPPED(v6srcp)) { 2370 ip1dbg(("ip_bind_v6: " 2371 "src is mapped, but not the dst\n")); 2372 goto bad_addr; 2373 } else { 2374 error = ip_bind_connected_v6(connp, mp, v6srcp, 2375 lport, v6dstp, ipp, fport, ire_requested, 2376 ipsec_policy_set, B_TRUE, verify_dst); 2377 if (error != 0) 2378 goto bad_addr; 2379 connp->conn_pkt_isv6 = B_TRUE; 2380 } 2381 if (protocol == IPPROTO_TCP) 2382 connp->conn_recv = tcp_input; 2383 } 2384 /* Update qinfo if v4/v6 changed */ 2385 if ((orig_pkt_isv6 != connp->conn_pkt_isv6) && 2386 !(IPCL_IS_TCP(connp) || IPCL_IS_UDP(connp))) { 2387 if (connp->conn_pkt_isv6) 2388 ip_setqinfo(RD(q), IPV6_MINOR, B_TRUE); 2389 else 2390 ip_setqinfo(RD(q), IPV4_MINOR, B_TRUE); 2391 } 2392 2393 /* 2394 * Pass the IPSEC headers size in ire_ipsec_overhead. 2395 * We can't do this in ip_bind_insert_ire because the policy 2396 * may not have been inherited at that point in time and hence 2397 * conn_out_enforce_policy may not be set. 2398 */ 2399 mp1 = mp->b_cont; 2400 if (ire_requested && connp->conn_out_enforce_policy && 2401 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) { 2402 ire_t *ire = (ire_t *)mp1->b_rptr; 2403 ASSERT(MBLKL(mp1) >= sizeof (ire_t)); 2404 ire->ire_ipsec_overhead = (conn_ipsec_length(connp)); 2405 } 2406 2407 /* Send it home. */ 2408 mp->b_datap->db_type = M_PCPROTO; 2409 tbr->PRIM_type = T_BIND_ACK; 2410 return (mp); 2411 2412 bad_addr: 2413 if (error == EINPROGRESS) 2414 return (NULL); 2415 if (error > 0) 2416 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 2417 else 2418 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 2419 return (mp); 2420 } 2421 2422 /* 2423 * Here address is verified to be a valid local address. 2424 * If the IRE_DB_REQ_TYPE mp is present, a multicast 2425 * address is also considered a valid local address. 2426 * In the case of a multicast address, however, the 2427 * upper protocol is expected to reset the src address 2428 * to 0 if it sees an ire with IN6_IS_ADDR_MULTICAST returned so that 2429 * no packets are emitted with multicast address as 2430 * source address. 2431 * The addresses valid for bind are: 2432 * (1) - in6addr_any 2433 * (2) - IP address of an UP interface 2434 * (3) - IP address of a DOWN interface 2435 * (4) - a multicast address. In this case 2436 * the conn will only receive packets destined to 2437 * the specified multicast address. Note: the 2438 * application still has to issue an 2439 * IPV6_JOIN_GROUP socket option. 2440 * 2441 * In all the above cases, the bound address must be valid in the current zone. 2442 * When the address is loopback or multicast, there might be many matching IREs 2443 * so bind has to look up based on the zone. 2444 */ 2445 static int 2446 ip_bind_laddr_v6(conn_t *connp, mblk_t *mp, const in6_addr_t *v6src, 2447 uint16_t lport, boolean_t ire_requested, boolean_t ipsec_policy_set, 2448 boolean_t fanout_insert) 2449 { 2450 int error = 0; 2451 ire_t *src_ire = NULL; 2452 ipif_t *ipif = NULL; 2453 mblk_t *policy_mp; 2454 zoneid_t zoneid; 2455 2456 if (ipsec_policy_set) 2457 policy_mp = mp->b_cont; 2458 2459 /* 2460 * If it was previously connected, conn_fully_bound would have 2461 * been set. 2462 */ 2463 connp->conn_fully_bound = B_FALSE; 2464 2465 zoneid = connp->conn_zoneid; 2466 2467 if (!IN6_IS_ADDR_UNSPECIFIED(v6src)) { 2468 src_ire = ire_route_lookup_v6(v6src, 0, 0, 2469 0, NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY); 2470 /* 2471 * If an address other than in6addr_any is requested, 2472 * we verify that it is a valid address for bind 2473 * Note: Following code is in if-else-if form for 2474 * readability compared to a condition check. 2475 */ 2476 ASSERT(src_ire == NULL || !(src_ire->ire_type & IRE_BROADCAST)); 2477 if (IRE_IS_LOCAL(src_ire)) { 2478 /* 2479 * (2) Bind to address of local UP interface 2480 */ 2481 ipif = src_ire->ire_ipif; 2482 } else if (IN6_IS_ADDR_MULTICAST(v6src)) { 2483 ipif_t *multi_ipif = NULL; 2484 ire_t *save_ire; 2485 /* 2486 * (4) bind to multicast address. 2487 * Fake out the IRE returned to upper 2488 * layer to be a broadcast IRE in 2489 * ip_bind_insert_ire_v6(). 2490 * Pass other information that matches 2491 * the ipif (e.g. the source address). 2492 * conn_multicast_ill is only used for 2493 * IPv6 packets 2494 */ 2495 mutex_enter(&connp->conn_lock); 2496 if (connp->conn_multicast_ill != NULL) { 2497 (void) ipif_lookup_zoneid( 2498 connp->conn_multicast_ill, zoneid, 0, 2499 &multi_ipif); 2500 } else { 2501 /* 2502 * Look for default like 2503 * ip_wput_v6 2504 */ 2505 multi_ipif = ipif_lookup_group_v6( 2506 &ipv6_unspecified_group, zoneid); 2507 } 2508 mutex_exit(&connp->conn_lock); 2509 save_ire = src_ire; 2510 src_ire = NULL; 2511 if (multi_ipif == NULL || !ire_requested || 2512 (src_ire = ipif_to_ire_v6(multi_ipif)) == NULL) { 2513 src_ire = save_ire; 2514 error = EADDRNOTAVAIL; 2515 } else { 2516 ASSERT(src_ire != NULL); 2517 if (save_ire != NULL) 2518 ire_refrele(save_ire); 2519 } 2520 if (multi_ipif != NULL) 2521 ipif_refrele(multi_ipif); 2522 } else { 2523 *mp->b_wptr++ = (char)connp->conn_ulp; 2524 ipif = ipif_lookup_addr_v6(v6src, NULL, zoneid, 2525 CONNP_TO_WQ(connp), mp, ip_wput_nondata, &error); 2526 if (ipif == NULL) { 2527 if (error == EINPROGRESS) { 2528 if (src_ire != NULL) 2529 ire_refrele(src_ire); 2530 return (error); 2531 } 2532 /* 2533 * Not a valid address for bind 2534 */ 2535 error = EADDRNOTAVAIL; 2536 } else { 2537 ipif_refrele(ipif); 2538 } 2539 /* 2540 * Just to keep it consistent with the processing in 2541 * ip_bind_v6(). 2542 */ 2543 mp->b_wptr--; 2544 } 2545 2546 if (error != 0) { 2547 /* Red Alert! Attempting to be a bogon! */ 2548 if (ip_debug > 2) { 2549 /* ip1dbg */ 2550 pr_addr_dbg("ip_bind_laddr_v6: bad src" 2551 " address %s\n", AF_INET6, v6src); 2552 } 2553 goto bad_addr; 2554 } 2555 } 2556 2557 /* 2558 * Allow setting new policies. For example, disconnects come 2559 * down as ipa_t bind. As we would have set conn_policy_cached 2560 * to B_TRUE before, we should set it to B_FALSE, so that policy 2561 * can change after the disconnect. 2562 */ 2563 connp->conn_policy_cached = B_FALSE; 2564 2565 /* If not fanout_insert this was just an address verification */ 2566 if (fanout_insert) { 2567 /* 2568 * The addresses have been verified. Time to insert in 2569 * the correct fanout list. 2570 */ 2571 connp->conn_srcv6 = *v6src; 2572 connp->conn_remv6 = ipv6_all_zeros; 2573 connp->conn_lport = lport; 2574 connp->conn_fport = 0; 2575 error = ipcl_bind_insert_v6(connp, *mp->b_wptr, v6src, lport); 2576 } 2577 if (error == 0) { 2578 if (ire_requested) { 2579 if (!ip_bind_insert_ire_v6(mp, src_ire, v6src, NULL)) { 2580 error = -1; 2581 goto bad_addr; 2582 } 2583 } else if (ipsec_policy_set) { 2584 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 2585 error = -1; 2586 goto bad_addr; 2587 } 2588 } 2589 } 2590 bad_addr: 2591 if (error != 0) { 2592 if (connp->conn_anon_port) { 2593 (void) tsol_mlp_anon(crgetzone(connp->conn_cred), 2594 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport), 2595 B_FALSE); 2596 } 2597 connp->conn_mlp_type = mlptSingle; 2598 } 2599 2600 if (src_ire != NULL) 2601 ire_refrele(src_ire); 2602 2603 if (ipsec_policy_set) { 2604 ASSERT(policy_mp != NULL); 2605 freeb(policy_mp); 2606 /* 2607 * As of now assume that nothing else accompanies 2608 * IPSEC_POLICY_SET. 2609 */ 2610 mp->b_cont = NULL; 2611 } 2612 return (error); 2613 } 2614 2615 /* ARGSUSED */ 2616 static void 2617 ip_bind_connected_resume_v6(ipsq_t *ipsq, queue_t *q, mblk_t *mp, 2618 void *dummy_arg) 2619 { 2620 conn_t *connp = NULL; 2621 t_scalar_t prim; 2622 2623 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 2624 2625 if (CONN_Q(q)) 2626 connp = Q_TO_CONN(q); 2627 ASSERT(connp != NULL); 2628 2629 prim = ((union T_primitives *)mp->b_rptr)->type; 2630 ASSERT(prim == O_T_BIND_REQ || prim == T_BIND_REQ); 2631 2632 if (IPCL_IS_TCP(connp)) { 2633 /* Pass sticky_ipp for scope_id and pktinfo */ 2634 mp = ip_bind_v6(q, mp, connp, &connp->conn_tcp->tcp_sticky_ipp); 2635 } else { 2636 /* For UDP and ICMP */ 2637 mp = ip_bind_v6(q, mp, connp, NULL); 2638 } 2639 if (mp != NULL) { 2640 if (IPCL_IS_TCP(connp)) { 2641 CONN_INC_REF(connp); 2642 squeue_fill(connp->conn_sqp, mp, ip_resume_tcp_bind, 2643 connp, SQTAG_TCP_RPUTOTHER); 2644 } else if (IPCL_IS_UDP(connp)) { 2645 udp_resume_bind(connp, mp); 2646 } else { 2647 qreply(q, mp); 2648 CONN_OPER_PENDING_DONE(connp); 2649 } 2650 } 2651 } 2652 2653 /* 2654 * Verify that both the source and destination addresses 2655 * are valid. If verify_dst, then destination address must also be reachable, 2656 * i.e. have a route. Protocols like TCP want this. Tunnels do not. 2657 * It takes ip6_pkt_t * as one of the arguments to determine correct 2658 * source address when IPV6_PKTINFO or scope_id is set along with a link-local 2659 * destination address. Note that parameter ipp is only useful for TCP connect 2660 * when scope_id is set or IPV6_PKTINFO option is set with an ifindex. For all 2661 * non-TCP cases, it is NULL and for all other tcp cases it is not useful. 2662 * 2663 */ 2664 static int 2665 ip_bind_connected_v6(conn_t *connp, mblk_t *mp, in6_addr_t *v6src, 2666 uint16_t lport, const in6_addr_t *v6dst, ip6_pkt_t *ipp, uint16_t fport, 2667 boolean_t ire_requested, boolean_t ipsec_policy_set, 2668 boolean_t fanout_insert, boolean_t verify_dst) 2669 { 2670 ire_t *src_ire; 2671 ire_t *dst_ire; 2672 int error = 0; 2673 int protocol; 2674 mblk_t *policy_mp; 2675 ire_t *sire = NULL; 2676 ire_t *md_dst_ire = NULL; 2677 ill_t *md_ill = NULL; 2678 ill_t *dst_ill = NULL; 2679 ipif_t *src_ipif = NULL; 2680 zoneid_t zoneid; 2681 boolean_t ill_held = B_FALSE; 2682 2683 src_ire = dst_ire = NULL; 2684 /* 2685 * NOTE: The protocol is beyond the wptr because that's how 2686 * the undocumented transport<-->IP T_BIND_REQ behavior works. 2687 */ 2688 protocol = *mp->b_wptr & 0xFF; 2689 2690 /* 2691 * If we never got a disconnect before, clear it now. 2692 */ 2693 connp->conn_fully_bound = B_FALSE; 2694 2695 if (ipsec_policy_set) { 2696 policy_mp = mp->b_cont; 2697 } 2698 2699 zoneid = connp->conn_zoneid; 2700 2701 if (IN6_IS_ADDR_MULTICAST(v6dst)) { 2702 ipif_t *ipif; 2703 2704 /* 2705 * Use an "emulated" IRE_BROADCAST to tell the transport it 2706 * is a multicast. 2707 * Pass other information that matches 2708 * the ipif (e.g. the source address). 2709 * 2710 * conn_multicast_ill is only used for IPv6 packets 2711 */ 2712 mutex_enter(&connp->conn_lock); 2713 if (connp->conn_multicast_ill != NULL) { 2714 (void) ipif_lookup_zoneid(connp->conn_multicast_ill, 2715 zoneid, 0, &ipif); 2716 } else { 2717 /* Look for default like ip_wput_v6 */ 2718 ipif = ipif_lookup_group_v6(v6dst, zoneid); 2719 } 2720 mutex_exit(&connp->conn_lock); 2721 if (ipif == NULL || !ire_requested || 2722 (dst_ire = ipif_to_ire_v6(ipif)) == NULL) { 2723 if (ipif != NULL) 2724 ipif_refrele(ipif); 2725 if (ip_debug > 2) { 2726 /* ip1dbg */ 2727 pr_addr_dbg("ip_bind_connected_v6: bad " 2728 "connected multicast %s\n", AF_INET6, 2729 v6dst); 2730 } 2731 error = ENETUNREACH; 2732 goto bad_addr; 2733 } 2734 if (ipif != NULL) 2735 ipif_refrele(ipif); 2736 } else { 2737 dst_ire = ire_route_lookup_v6(v6dst, NULL, NULL, 0, 2738 NULL, &sire, zoneid, MBLK_GETLABEL(mp), 2739 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2740 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_SECATTR); 2741 /* 2742 * We also prevent ire's with src address INADDR_ANY to 2743 * be used, which are created temporarily for 2744 * sending out packets from endpoints that have 2745 * conn_unspec_src set. 2746 */ 2747 if (dst_ire == NULL || 2748 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) || 2749 IN6_IS_ADDR_UNSPECIFIED(&dst_ire->ire_src_addr_v6)) { 2750 /* 2751 * When verifying destination reachability, we always 2752 * complain. 2753 * 2754 * When not verifying destination reachability but we 2755 * found an IRE, i.e. the destination is reachable, 2756 * then the other tests still apply and we complain. 2757 */ 2758 if (verify_dst || (dst_ire != NULL)) { 2759 if (ip_debug > 2) { 2760 /* ip1dbg */ 2761 pr_addr_dbg("ip_bind_connected_v6: bad" 2762 " connected dst %s\n", AF_INET6, 2763 v6dst); 2764 } 2765 if (dst_ire == NULL || 2766 !(dst_ire->ire_type & IRE_HOST)) { 2767 error = ENETUNREACH; 2768 } else { 2769 error = EHOSTUNREACH; 2770 } 2771 goto bad_addr; 2772 } 2773 } 2774 } 2775 2776 /* 2777 * We now know that routing will allow us to reach the destination. 2778 * Check whether Trusted Solaris policy allows communication with this 2779 * host, and pretend that the destination is unreachable if not. 2780 * 2781 * This is never a problem for TCP, since that transport is known to 2782 * compute the label properly as part of the tcp_rput_other T_BIND_ACK 2783 * handling. If the remote is unreachable, it will be detected at that 2784 * point, so there's no reason to check it here. 2785 * 2786 * Note that for sendto (and other datagram-oriented friends), this 2787 * check is done as part of the data path label computation instead. 2788 * The check here is just to make non-TCP connect() report the right 2789 * error. 2790 */ 2791 if (dst_ire != NULL && is_system_labeled() && 2792 !IPCL_IS_TCP(connp) && 2793 tsol_compute_label_v6(DB_CREDDEF(mp, connp->conn_cred), v6dst, NULL, 2794 connp->conn_mac_exempt) != 0) { 2795 error = EHOSTUNREACH; 2796 if (ip_debug > 2) { 2797 pr_addr_dbg("ip_bind_connected: no label for dst %s\n", 2798 AF_INET6, v6dst); 2799 } 2800 goto bad_addr; 2801 } 2802 2803 /* 2804 * If the app does a connect(), it means that it will most likely 2805 * send more than 1 packet to the destination. It makes sense 2806 * to clear the temporary flag. 2807 */ 2808 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE && 2809 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) { 2810 irb_t *irb = dst_ire->ire_bucket; 2811 2812 rw_enter(&irb->irb_lock, RW_WRITER); 2813 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY; 2814 irb->irb_tmp_ire_cnt--; 2815 rw_exit(&irb->irb_lock); 2816 } 2817 2818 ASSERT(dst_ire == NULL || dst_ire->ire_ipversion == IPV6_VERSION); 2819 2820 /* 2821 * See if we should notify ULP about MDT; we do this whether or not 2822 * ire_requested is TRUE, in order to handle active connects; MDT 2823 * eligibility tests for passive connects are handled separately 2824 * through tcp_adapt_ire(). We do this before the source address 2825 * selection, because dst_ire may change after a call to 2826 * ipif_select_source_v6(). This is a best-effort check, as the 2827 * packet for this connection may not actually go through 2828 * dst_ire->ire_stq, and the exact IRE can only be known after 2829 * calling ip_newroute_v6(). This is why we further check on the 2830 * IRE during Multidata packet transmission in tcp_multisend(). 2831 */ 2832 if (ip_multidata_outbound && !ipsec_policy_set && dst_ire != NULL && 2833 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) && 2834 (md_ill = ire_to_ill(dst_ire), md_ill != NULL) && 2835 ILL_MDT_CAPABLE(md_ill)) { 2836 md_dst_ire = dst_ire; 2837 IRE_REFHOLD(md_dst_ire); 2838 } 2839 2840 if (dst_ire != NULL && 2841 dst_ire->ire_type == IRE_LOCAL && 2842 dst_ire->ire_zoneid != zoneid && 2843 dst_ire->ire_zoneid != ALL_ZONES) { 2844 src_ire = ire_ftable_lookup_v6(v6dst, 0, 0, 0, NULL, NULL, 2845 zoneid, 0, NULL, 2846 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2847 MATCH_IRE_RJ_BHOLE); 2848 if (src_ire == NULL) { 2849 error = EHOSTUNREACH; 2850 goto bad_addr; 2851 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) { 2852 if (!(src_ire->ire_type & IRE_HOST)) 2853 error = ENETUNREACH; 2854 else 2855 error = EHOSTUNREACH; 2856 goto bad_addr; 2857 } 2858 if (IN6_IS_ADDR_UNSPECIFIED(v6src)) { 2859 src_ipif = src_ire->ire_ipif; 2860 ipif_refhold(src_ipif); 2861 *v6src = src_ipif->ipif_v6lcl_addr; 2862 } 2863 ire_refrele(src_ire); 2864 src_ire = NULL; 2865 } else if (IN6_IS_ADDR_UNSPECIFIED(v6src) && dst_ire != NULL) { 2866 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 2867 *v6src = sire->ire_src_addr_v6; 2868 ire_refrele(dst_ire); 2869 dst_ire = sire; 2870 sire = NULL; 2871 } else if (dst_ire->ire_type == IRE_CACHE && 2872 (dst_ire->ire_flags & RTF_SETSRC)) { 2873 ASSERT(dst_ire->ire_zoneid == zoneid || 2874 dst_ire->ire_zoneid == ALL_ZONES); 2875 *v6src = dst_ire->ire_src_addr_v6; 2876 } else { 2877 /* 2878 * Pick a source address so that a proper inbound load 2879 * spreading would happen. Use dst_ill specified by the 2880 * app. when socket option or scopeid is set. 2881 */ 2882 int err; 2883 2884 if (ipp != NULL && ipp->ipp_ifindex != 0) { 2885 uint_t if_index; 2886 2887 /* 2888 * Scope id or IPV6_PKTINFO 2889 */ 2890 2891 if_index = ipp->ipp_ifindex; 2892 dst_ill = ill_lookup_on_ifindex( 2893 if_index, B_TRUE, NULL, NULL, NULL, NULL); 2894 if (dst_ill == NULL) { 2895 ip1dbg(("ip_bind_connected_v6:" 2896 " bad ifindex %d\n", if_index)); 2897 error = EADDRNOTAVAIL; 2898 goto bad_addr; 2899 } 2900 ill_held = B_TRUE; 2901 } else if (connp->conn_outgoing_ill != NULL) { 2902 /* 2903 * For IPV6_BOUND_IF socket option, 2904 * conn_outgoing_ill should be set 2905 * already in TCP or UDP/ICMP. 2906 */ 2907 dst_ill = conn_get_held_ill(connp, 2908 &connp->conn_outgoing_ill, &err); 2909 if (err == ILL_LOOKUP_FAILED) { 2910 ip1dbg(("ip_bind_connected_v6:" 2911 "no ill for bound_if\n")); 2912 error = EADDRNOTAVAIL; 2913 goto bad_addr; 2914 } 2915 ill_held = B_TRUE; 2916 } else if (dst_ire->ire_stq != NULL) { 2917 /* No need to hold ill here */ 2918 dst_ill = (ill_t *)dst_ire->ire_stq->q_ptr; 2919 } else { 2920 /* No need to hold ill here */ 2921 dst_ill = dst_ire->ire_ipif->ipif_ill; 2922 } 2923 if (!ip6_asp_can_lookup()) { 2924 *mp->b_wptr++ = (char)protocol; 2925 ip6_asp_pending_op(CONNP_TO_WQ(connp), mp, 2926 ip_bind_connected_resume_v6); 2927 error = EINPROGRESS; 2928 goto refrele_and_quit; 2929 } 2930 src_ipif = ipif_select_source_v6(dst_ill, v6dst, 2931 RESTRICT_TO_NONE, connp->conn_src_preferences, 2932 zoneid); 2933 ip6_asp_table_refrele(); 2934 if (src_ipif == NULL) { 2935 pr_addr_dbg("ip_bind_connected_v6: " 2936 "no usable source address for " 2937 "connection to %s\n", AF_INET6, v6dst); 2938 error = EADDRNOTAVAIL; 2939 goto bad_addr; 2940 } 2941 *v6src = src_ipif->ipif_v6lcl_addr; 2942 } 2943 } 2944 2945 /* 2946 * We do ire_route_lookup_v6() here (and not an interface lookup) 2947 * as we assert that v6src should only come from an 2948 * UP interface for hard binding. 2949 */ 2950 src_ire = ire_route_lookup_v6(v6src, 0, 0, 0, NULL, 2951 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY); 2952 2953 /* src_ire must be a local|loopback */ 2954 if (!IRE_IS_LOCAL(src_ire)) { 2955 if (ip_debug > 2) { 2956 /* ip1dbg */ 2957 pr_addr_dbg("ip_bind_connected_v6: bad " 2958 "connected src %s\n", AF_INET6, v6src); 2959 } 2960 error = EADDRNOTAVAIL; 2961 goto bad_addr; 2962 } 2963 2964 /* 2965 * If the source address is a loopback address, the 2966 * destination had best be local or multicast. 2967 * The transports that can't handle multicast will reject 2968 * those addresses. 2969 */ 2970 if (src_ire->ire_type == IRE_LOOPBACK && 2971 !(IRE_IS_LOCAL(dst_ire) || IN6_IS_ADDR_MULTICAST(v6dst) || 2972 IN6_IS_ADDR_V4MAPPED_CLASSD(v6dst))) { 2973 ip1dbg(("ip_bind_connected_v6: bad connected loopback\n")); 2974 error = -1; 2975 goto bad_addr; 2976 } 2977 /* 2978 * Allow setting new policies. For example, disconnects come 2979 * down as ipa_t bind. As we would have set conn_policy_cached 2980 * to B_TRUE before, we should set it to B_FALSE, so that policy 2981 * can change after the disconnect. 2982 */ 2983 connp->conn_policy_cached = B_FALSE; 2984 2985 /* 2986 * The addresses have been verified. Initialize the conn 2987 * before calling the policy as they expect the conns 2988 * initialized. 2989 */ 2990 connp->conn_srcv6 = *v6src; 2991 connp->conn_remv6 = *v6dst; 2992 connp->conn_lport = lport; 2993 connp->conn_fport = fport; 2994 2995 ASSERT(!(ipsec_policy_set && ire_requested)); 2996 if (ire_requested) { 2997 iulp_t *ulp_info = NULL; 2998 2999 /* 3000 * Note that sire will not be NULL if this is an off-link 3001 * connection and there is not cache for that dest yet. 3002 * 3003 * XXX Because of an existing bug, if there are multiple 3004 * default routes, the IRE returned now may not be the actual 3005 * default route used (default routes are chosen in a 3006 * round robin fashion). So if the metrics for different 3007 * default routes are different, we may return the wrong 3008 * metrics. This will not be a problem if the existing 3009 * bug is fixed. 3010 */ 3011 if (sire != NULL) 3012 ulp_info = &(sire->ire_uinfo); 3013 3014 if (!ip_bind_insert_ire_v6(mp, dst_ire, v6dst, ulp_info)) { 3015 error = -1; 3016 goto bad_addr; 3017 } 3018 } else if (ipsec_policy_set) { 3019 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) { 3020 error = -1; 3021 goto bad_addr; 3022 } 3023 } 3024 3025 /* 3026 * Cache IPsec policy in this conn. If we have per-socket policy, 3027 * we'll cache that. If we don't, we'll inherit global policy. 3028 * 3029 * We can't insert until the conn reflects the policy. Note that 3030 * conn_policy_cached is set by ipsec_conn_cache_policy() even for 3031 * connections where we don't have a policy. This is to prevent 3032 * global policy lookups in the inbound path. 3033 * 3034 * If we insert before we set conn_policy_cached, 3035 * CONN_INBOUND_POLICY_PRESENT_V6() check can still evaluate true 3036 * because global policy cound be non-empty. We normally call 3037 * ipsec_check_policy() for conn_policy_cached connections only if 3038 * conn_in_enforce_policy is set. But in this case, 3039 * conn_policy_cached can get set anytime since we made the 3040 * CONN_INBOUND_POLICY_PRESENT_V6() check and ipsec_check_policy() 3041 * is called, which will make the above assumption false. Thus, we 3042 * need to insert after we set conn_policy_cached. 3043 */ 3044 if ((error = ipsec_conn_cache_policy(connp, B_FALSE)) != 0) 3045 goto bad_addr; 3046 3047 /* If not fanout_insert this was just an address verification */ 3048 if (fanout_insert) { 3049 /* 3050 * The addresses have been verified. Time to insert in 3051 * the correct fanout list. 3052 */ 3053 error = ipcl_conn_insert_v6(connp, protocol, v6src, v6dst, 3054 connp->conn_ports, 3055 IPCL_IS_TCP(connp) ? connp->conn_tcp->tcp_bound_if : 0); 3056 } 3057 if (error == 0) { 3058 connp->conn_fully_bound = B_TRUE; 3059 /* 3060 * Our initial checks for MDT have passed; the IRE is not 3061 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to 3062 * be supporting MDT. Pass the IRE, IPC and ILL into 3063 * ip_mdinfo_return(), which performs further checks 3064 * against them and upon success, returns the MDT info 3065 * mblk which we will attach to the bind acknowledgment. 3066 */ 3067 if (md_dst_ire != NULL) { 3068 mblk_t *mdinfo_mp; 3069 3070 ASSERT(md_ill != NULL); 3071 ASSERT(md_ill->ill_mdt_capab != NULL); 3072 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp, 3073 md_ill->ill_name, md_ill->ill_mdt_capab)) != NULL) 3074 linkb(mp, mdinfo_mp); 3075 } 3076 } 3077 bad_addr: 3078 if (ipsec_policy_set) { 3079 ASSERT(policy_mp != NULL); 3080 freeb(policy_mp); 3081 /* 3082 * As of now assume that nothing else accompanies 3083 * IPSEC_POLICY_SET. 3084 */ 3085 mp->b_cont = NULL; 3086 } 3087 refrele_and_quit: 3088 if (src_ire != NULL) 3089 IRE_REFRELE(src_ire); 3090 if (dst_ire != NULL) 3091 IRE_REFRELE(dst_ire); 3092 if (sire != NULL) 3093 IRE_REFRELE(sire); 3094 if (src_ipif != NULL) 3095 ipif_refrele(src_ipif); 3096 if (md_dst_ire != NULL) 3097 IRE_REFRELE(md_dst_ire); 3098 if (ill_held && dst_ill != NULL) 3099 ill_refrele(dst_ill); 3100 return (error); 3101 } 3102 3103 /* 3104 * Insert the ire in b_cont. Returns false if it fails (due to lack of space). 3105 * Makes the IRE be IRE_BROADCAST if dst is a multicast address. 3106 */ 3107 static boolean_t 3108 ip_bind_insert_ire_v6(mblk_t *mp, ire_t *ire, const in6_addr_t *dst, 3109 iulp_t *ulp_info) 3110 { 3111 mblk_t *mp1; 3112 ire_t *ret_ire; 3113 3114 mp1 = mp->b_cont; 3115 ASSERT(mp1 != NULL); 3116 3117 if (ire != NULL) { 3118 /* 3119 * mp1 initialized above to IRE_DB_REQ_TYPE 3120 * appended mblk. Its <upper protocol>'s 3121 * job to make sure there is room. 3122 */ 3123 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t)) 3124 return (B_FALSE); 3125 3126 mp1->b_datap->db_type = IRE_DB_TYPE; 3127 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t); 3128 bcopy(ire, mp1->b_rptr, sizeof (ire_t)); 3129 ret_ire = (ire_t *)mp1->b_rptr; 3130 if (IN6_IS_ADDR_MULTICAST(dst) || 3131 IN6_IS_ADDR_V4MAPPED_CLASSD(dst)) { 3132 ret_ire->ire_type = IRE_BROADCAST; 3133 ret_ire->ire_addr_v6 = *dst; 3134 } 3135 if (ulp_info != NULL) { 3136 bcopy(ulp_info, &(ret_ire->ire_uinfo), 3137 sizeof (iulp_t)); 3138 } 3139 ret_ire->ire_mp = mp1; 3140 } else { 3141 /* 3142 * No IRE was found. Remove IRE mblk. 3143 */ 3144 mp->b_cont = mp1->b_cont; 3145 freeb(mp1); 3146 } 3147 return (B_TRUE); 3148 } 3149 3150 /* 3151 * Add an ip6i_t header to the front of the mblk. 3152 * Inline if possible else allocate a separate mblk containing only the ip6i_t. 3153 * Returns NULL if allocation fails (and frees original message). 3154 * Used in outgoing path when going through ip_newroute_*v6(). 3155 * Used in incoming path to pass ifindex to transports. 3156 */ 3157 mblk_t * 3158 ip_add_info_v6(mblk_t *mp, ill_t *ill, const in6_addr_t *dst) 3159 { 3160 mblk_t *mp1; 3161 ip6i_t *ip6i; 3162 ip6_t *ip6h; 3163 3164 ip6h = (ip6_t *)mp->b_rptr; 3165 ip6i = (ip6i_t *)(mp->b_rptr - sizeof (ip6i_t)); 3166 if ((uchar_t *)ip6i < mp->b_datap->db_base || 3167 mp->b_datap->db_ref > 1) { 3168 mp1 = allocb(sizeof (ip6i_t), BPRI_MED); 3169 if (mp1 == NULL) { 3170 freemsg(mp); 3171 return (NULL); 3172 } 3173 mp1->b_wptr = mp1->b_rptr = mp1->b_datap->db_lim; 3174 mp1->b_cont = mp; 3175 mp = mp1; 3176 ip6i = (ip6i_t *)(mp->b_rptr - sizeof (ip6i_t)); 3177 } 3178 mp->b_rptr = (uchar_t *)ip6i; 3179 ip6i->ip6i_vcf = ip6h->ip6_vcf; 3180 ip6i->ip6i_nxt = IPPROTO_RAW; 3181 if (ill != NULL) { 3182 ip6i->ip6i_flags = IP6I_IFINDEX; 3183 ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex; 3184 } else { 3185 ip6i->ip6i_flags = 0; 3186 } 3187 ip6i->ip6i_nexthop = *dst; 3188 return (mp); 3189 } 3190 3191 /* 3192 * Handle protocols with which IP is less intimate. There 3193 * can be more than one stream bound to a particular 3194 * protocol. When this is the case, normally each one gets a copy 3195 * of any incoming packets. 3196 * However, if the packet was tunneled and not multicast we only send to it 3197 * the first match. 3198 * 3199 * Zones notes: 3200 * Packets will be distributed to streams in all zones. This is really only 3201 * useful for ICMPv6 as only applications in the global zone can create raw 3202 * sockets for other protocols. 3203 */ 3204 static void 3205 ip_fanout_proto_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, ill_t *ill, 3206 ill_t *inill, uint8_t nexthdr, uint_t nexthdr_offset, uint_t flags, 3207 boolean_t mctl_present, zoneid_t zoneid) 3208 { 3209 queue_t *rq; 3210 mblk_t *mp1, *first_mp1; 3211 in6_addr_t dst = ip6h->ip6_dst; 3212 in6_addr_t src = ip6h->ip6_src; 3213 boolean_t one_only; 3214 mblk_t *first_mp = mp; 3215 boolean_t secure, shared_addr; 3216 conn_t *connp, *first_connp, *next_connp; 3217 connf_t *connfp; 3218 3219 if (mctl_present) { 3220 mp = first_mp->b_cont; 3221 secure = ipsec_in_is_secure(first_mp); 3222 ASSERT(mp != NULL); 3223 } else { 3224 secure = B_FALSE; 3225 } 3226 3227 /* 3228 * If the packet was tunneled and not multicast we only send to it 3229 * the first match. 3230 */ 3231 one_only = ((nexthdr == IPPROTO_ENCAP || nexthdr == IPPROTO_IPV6) && 3232 !IN6_IS_ADDR_MULTICAST(&dst)); 3233 3234 shared_addr = (zoneid == ALL_ZONES); 3235 if (shared_addr) { 3236 /* 3237 * We don't allow multilevel ports for raw IP, so no need to 3238 * check for that here. 3239 */ 3240 zoneid = tsol_packet_to_zoneid(mp); 3241 } 3242 3243 connfp = &ipcl_proto_fanout_v6[nexthdr]; 3244 mutex_enter(&connfp->connf_lock); 3245 connp = connfp->connf_head; 3246 for (connp = connfp->connf_head; connp != NULL; 3247 connp = connp->conn_next) { 3248 if (IPCL_PROTO_MATCH_V6(connp, nexthdr, ip6h, ill, flags, 3249 zoneid) && 3250 (!is_system_labeled() || 3251 tsol_receive_local(mp, &dst, IPV6_VERSION, shared_addr, 3252 connp))) 3253 break; 3254 } 3255 3256 if (connp == NULL || connp->conn_upq == NULL) { 3257 /* 3258 * No one bound to this port. Is 3259 * there a client that wants all 3260 * unclaimed datagrams? 3261 */ 3262 mutex_exit(&connfp->connf_lock); 3263 if (ip_fanout_send_icmp_v6(q, first_mp, flags, 3264 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER, 3265 nexthdr_offset, mctl_present, zoneid)) { 3266 BUMP_MIB(ill->ill_ip6_mib, ipv6InUnknownProtos); 3267 } 3268 3269 return; 3270 } 3271 3272 CONN_INC_REF(connp); 3273 first_connp = connp; 3274 3275 /* 3276 * XXX: Fix the multiple protocol listeners case. We should not 3277 * be walking the conn->next list here. 3278 */ 3279 if (one_only) { 3280 /* 3281 * Only send message to one tunnel driver by immediately 3282 * terminating the loop. 3283 */ 3284 connp = NULL; 3285 } else { 3286 connp = connp->conn_next; 3287 3288 } 3289 for (;;) { 3290 while (connp != NULL) { 3291 if (IPCL_PROTO_MATCH_V6(connp, nexthdr, ip6h, ill, 3292 flags, zoneid) && 3293 (!is_system_labeled() || 3294 tsol_receive_local(mp, &dst, IPV6_VERSION, 3295 shared_addr, connp))) 3296 break; 3297 connp = connp->conn_next; 3298 } 3299 3300 /* 3301 * Just copy the data part alone. The mctl part is 3302 * needed just for verifying policy and it is never 3303 * sent up. 3304 */ 3305 if (connp == NULL || connp->conn_upq == NULL || 3306 (((first_mp1 = dupmsg(first_mp)) == NULL) && 3307 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) { 3308 /* 3309 * No more intested clients or memory 3310 * allocation failed 3311 */ 3312 connp = first_connp; 3313 break; 3314 } 3315 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 3316 CONN_INC_REF(connp); 3317 mutex_exit(&connfp->connf_lock); 3318 rq = connp->conn_rq; 3319 /* 3320 * For link-local always add ifindex so that transport can set 3321 * sin6_scope_id. Avoid it for ICMP error fanout. 3322 */ 3323 if ((connp->conn_ipv6_recvpktinfo || 3324 IN6_IS_ADDR_LINKLOCAL(&src)) && 3325 (flags & IP_FF_IP6INFO)) { 3326 /* Add header */ 3327 mp1 = ip_add_info_v6(mp1, inill, &dst); 3328 } 3329 if (mp1 == NULL) { 3330 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3331 } else if (!canputnext(rq)) { 3332 if (flags & IP_FF_RAWIP) { 3333 BUMP_MIB(ill->ill_ip6_mib, rawipInOverflows); 3334 } else { 3335 BUMP_MIB(ill->ill_icmp6_mib, 3336 ipv6IfIcmpInOverflows); 3337 } 3338 3339 freemsg(mp1); 3340 } else { 3341 if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) { 3342 first_mp1 = ipsec_check_inbound_policy 3343 (first_mp1, connp, NULL, ip6h, 3344 mctl_present); 3345 } 3346 if (first_mp1 != NULL) { 3347 if (mctl_present) 3348 freeb(first_mp1); 3349 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 3350 putnext(rq, mp1); 3351 } 3352 } 3353 mutex_enter(&connfp->connf_lock); 3354 /* Follow the next pointer before releasing the conn. */ 3355 next_connp = connp->conn_next; 3356 CONN_DEC_REF(connp); 3357 connp = next_connp; 3358 } 3359 3360 /* Last one. Send it upstream. */ 3361 mutex_exit(&connfp->connf_lock); 3362 3363 /* Initiate IPPF processing */ 3364 if (IP6_IN_IPP(flags)) { 3365 uint_t ifindex; 3366 3367 mutex_enter(&ill->ill_lock); 3368 ifindex = ill->ill_phyint->phyint_ifindex; 3369 mutex_exit(&ill->ill_lock); 3370 ip_process(IPP_LOCAL_IN, &mp, ifindex); 3371 if (mp == NULL) { 3372 CONN_DEC_REF(connp); 3373 if (mctl_present) 3374 freeb(first_mp); 3375 return; 3376 } 3377 } 3378 3379 /* 3380 * For link-local always add ifindex so that transport can set 3381 * sin6_scope_id. Avoid it for ICMP error fanout. 3382 */ 3383 if ((connp->conn_ipv6_recvpktinfo || IN6_IS_ADDR_LINKLOCAL(&src)) && 3384 (flags & IP_FF_IP6INFO)) { 3385 /* Add header */ 3386 mp = ip_add_info_v6(mp, inill, &dst); 3387 if (mp == NULL) { 3388 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3389 CONN_DEC_REF(connp); 3390 if (mctl_present) 3391 freeb(first_mp); 3392 return; 3393 } else if (mctl_present) { 3394 first_mp->b_cont = mp; 3395 } else { 3396 first_mp = mp; 3397 } 3398 } 3399 3400 rq = connp->conn_rq; 3401 if (!canputnext(rq)) { 3402 if (flags & IP_FF_RAWIP) { 3403 BUMP_MIB(ill->ill_ip6_mib, rawipInOverflows); 3404 } else { 3405 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows); 3406 } 3407 3408 freemsg(first_mp); 3409 } else { 3410 if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) { 3411 first_mp = ipsec_check_inbound_policy(first_mp, connp, 3412 NULL, ip6h, mctl_present); 3413 if (first_mp == NULL) { 3414 CONN_DEC_REF(connp); 3415 return; 3416 } 3417 } 3418 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 3419 putnext(rq, mp); 3420 if (mctl_present) 3421 freeb(first_mp); 3422 } 3423 CONN_DEC_REF(connp); 3424 } 3425 3426 /* 3427 * Send an ICMP error after patching up the packet appropriately. Returns 3428 * non-zero if the appropriate MIB should be bumped; zero otherwise. 3429 */ 3430 int 3431 ip_fanout_send_icmp_v6(queue_t *q, mblk_t *mp, uint_t flags, 3432 uint_t icmp_type, uint8_t icmp_code, uint_t nexthdr_offset, 3433 boolean_t mctl_present, zoneid_t zoneid) 3434 { 3435 ip6_t *ip6h; 3436 mblk_t *first_mp; 3437 boolean_t secure; 3438 unsigned char db_type; 3439 3440 first_mp = mp; 3441 if (mctl_present) { 3442 mp = mp->b_cont; 3443 secure = ipsec_in_is_secure(first_mp); 3444 ASSERT(mp != NULL); 3445 } else { 3446 /* 3447 * If this is an ICMP error being reported - which goes 3448 * up as M_CTLs, we need to convert them to M_DATA till 3449 * we finish checking with global policy because 3450 * ipsec_check_global_policy() assumes M_DATA as clear 3451 * and M_CTL as secure. 3452 */ 3453 db_type = mp->b_datap->db_type; 3454 mp->b_datap->db_type = M_DATA; 3455 secure = B_FALSE; 3456 } 3457 /* 3458 * We are generating an icmp error for some inbound packet. 3459 * Called from all ip_fanout_(udp, tcp, proto) functions. 3460 * Before we generate an error, check with global policy 3461 * to see whether this is allowed to enter the system. As 3462 * there is no "conn", we are checking with global policy. 3463 */ 3464 ip6h = (ip6_t *)mp->b_rptr; 3465 if (secure || ipsec_inbound_v6_policy_present) { 3466 first_mp = ipsec_check_global_policy(first_mp, NULL, 3467 NULL, ip6h, mctl_present); 3468 if (first_mp == NULL) 3469 return (0); 3470 } 3471 3472 if (!mctl_present) 3473 mp->b_datap->db_type = db_type; 3474 3475 if (flags & IP_FF_SEND_ICMP) { 3476 if (flags & IP_FF_HDR_COMPLETE) { 3477 if (ip_hdr_complete_v6(ip6h, zoneid)) { 3478 freemsg(first_mp); 3479 return (1); 3480 } 3481 } 3482 switch (icmp_type) { 3483 case ICMP6_DST_UNREACH: 3484 icmp_unreachable_v6(WR(q), first_mp, icmp_code, 3485 B_FALSE, B_FALSE); 3486 break; 3487 case ICMP6_PARAM_PROB: 3488 icmp_param_problem_v6(WR(q), first_mp, icmp_code, 3489 nexthdr_offset, B_FALSE, B_FALSE); 3490 break; 3491 default: 3492 #ifdef DEBUG 3493 panic("ip_fanout_send_icmp_v6: wrong type"); 3494 /*NOTREACHED*/ 3495 #else 3496 freemsg(first_mp); 3497 break; 3498 #endif 3499 } 3500 } else { 3501 freemsg(first_mp); 3502 return (0); 3503 } 3504 3505 return (1); 3506 } 3507 3508 3509 /* 3510 * Fanout for TCP packets 3511 * The caller puts <fport, lport> in the ports parameter. 3512 */ 3513 static void 3514 ip_fanout_tcp_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, ill_t *ill, ill_t *inill, 3515 uint_t flags, uint_t hdr_len, boolean_t mctl_present, zoneid_t zoneid) 3516 { 3517 mblk_t *first_mp; 3518 boolean_t secure; 3519 conn_t *connp; 3520 tcph_t *tcph; 3521 boolean_t syn_present = B_FALSE; 3522 3523 first_mp = mp; 3524 if (mctl_present) { 3525 mp = first_mp->b_cont; 3526 secure = ipsec_in_is_secure(first_mp); 3527 ASSERT(mp != NULL); 3528 } else { 3529 secure = B_FALSE; 3530 } 3531 3532 connp = ipcl_classify_v6(mp, IPPROTO_TCP, hdr_len, zoneid); 3533 3534 if (connp == NULL || 3535 !conn_wantpacket_v6(connp, ill, ip6h, flags, zoneid)) { 3536 /* 3537 * No hard-bound match. Send Reset. 3538 */ 3539 dblk_t *dp = mp->b_datap; 3540 uint32_t ill_index; 3541 3542 ASSERT((dp->db_struioflag & STRUIO_IP) == 0); 3543 3544 /* Initiate IPPf processing, if needed. */ 3545 if (IPP_ENABLED(IPP_LOCAL_IN) && (flags & IP6_NO_IPPOLICY)) { 3546 ill_index = ill->ill_phyint->phyint_ifindex; 3547 ip_process(IPP_LOCAL_IN, &first_mp, ill_index); 3548 if (first_mp == NULL) { 3549 if (connp != NULL) 3550 CONN_DEC_REF(connp); 3551 return; 3552 } 3553 } 3554 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 3555 tcp_xmit_listeners_reset(first_mp, hdr_len); 3556 if (connp != NULL) 3557 CONN_DEC_REF(connp); 3558 return; 3559 } 3560 3561 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 3562 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 3563 if (connp->conn_flags & IPCL_TCP) { 3564 squeue_t *sqp; 3565 3566 /* 3567 * For fused tcp loopback, assign the eager's 3568 * squeue to be that of the active connect's. 3569 */ 3570 if ((flags & IP_FF_LOOPBACK) && do_tcp_fusion && 3571 !CONN_INBOUND_POLICY_PRESENT_V6(connp) && !secure && 3572 !IP6_IN_IPP(flags)) { 3573 ASSERT(Q_TO_CONN(q) != NULL); 3574 sqp = Q_TO_CONN(q)->conn_sqp; 3575 } else { 3576 sqp = IP_SQUEUE_GET(lbolt); 3577 } 3578 3579 mp->b_datap->db_struioflag |= STRUIO_EAGER; 3580 DB_CKSUMSTART(mp) = (intptr_t)sqp; 3581 3582 /* 3583 * db_cksumstuff is unused in the incoming 3584 * path; Thus store the ifindex here. It will 3585 * be cleared in tcp_conn_create_v6(). 3586 */ 3587 DB_CKSUMSTUFF(mp) = 3588 (intptr_t)ill->ill_phyint->phyint_ifindex; 3589 syn_present = B_TRUE; 3590 } 3591 } 3592 3593 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) { 3594 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF; 3595 if ((flags & TH_RST) || (flags & TH_URG)) { 3596 CONN_DEC_REF(connp); 3597 freemsg(first_mp); 3598 return; 3599 } 3600 if (flags & TH_ACK) { 3601 tcp_xmit_listeners_reset(first_mp, hdr_len); 3602 CONN_DEC_REF(connp); 3603 return; 3604 } 3605 3606 CONN_DEC_REF(connp); 3607 freemsg(first_mp); 3608 return; 3609 } 3610 3611 if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) { 3612 first_mp = ipsec_check_inbound_policy(first_mp, connp, 3613 NULL, ip6h, mctl_present); 3614 if (first_mp == NULL) { 3615 CONN_DEC_REF(connp); 3616 return; 3617 } 3618 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) { 3619 ASSERT(syn_present); 3620 if (mctl_present) { 3621 ASSERT(first_mp != mp); 3622 first_mp->b_datap->db_struioflag |= 3623 STRUIO_POLICY; 3624 } else { 3625 ASSERT(first_mp == mp); 3626 mp->b_datap->db_struioflag &= 3627 ~STRUIO_EAGER; 3628 mp->b_datap->db_struioflag |= 3629 STRUIO_POLICY; 3630 } 3631 } else { 3632 /* 3633 * Discard first_mp early since we're dealing with a 3634 * fully-connected conn_t and tcp doesn't do policy in 3635 * this case. Also, if someone is bound to IPPROTO_TCP 3636 * over raw IP, they don't expect to see a M_CTL. 3637 */ 3638 if (mctl_present) { 3639 freeb(first_mp); 3640 mctl_present = B_FALSE; 3641 } 3642 first_mp = mp; 3643 } 3644 } 3645 3646 /* Initiate IPPF processing */ 3647 if (IP6_IN_IPP(flags)) { 3648 uint_t ifindex; 3649 3650 mutex_enter(&ill->ill_lock); 3651 ifindex = ill->ill_phyint->phyint_ifindex; 3652 mutex_exit(&ill->ill_lock); 3653 ip_process(IPP_LOCAL_IN, &mp, ifindex); 3654 if (mp == NULL) { 3655 CONN_DEC_REF(connp); 3656 if (mctl_present) { 3657 freeb(first_mp); 3658 } 3659 return; 3660 } else if (mctl_present) { 3661 /* 3662 * ip_add_info_v6 might return a new mp. 3663 */ 3664 ASSERT(first_mp != mp); 3665 first_mp->b_cont = mp; 3666 } else { 3667 first_mp = mp; 3668 } 3669 } 3670 3671 /* 3672 * For link-local always add ifindex so that TCP can bind to that 3673 * interface. Avoid it for ICMP error fanout. 3674 */ 3675 if (!syn_present && ((connp->conn_ipv6_recvpktinfo || 3676 IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) && 3677 (flags & IP_FF_IP6INFO))) { 3678 /* Add header */ 3679 mp = ip_add_info_v6(mp, inill, &ip6h->ip6_dst); 3680 if (mp == NULL) { 3681 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3682 CONN_DEC_REF(connp); 3683 if (mctl_present) 3684 freeb(first_mp); 3685 return; 3686 } else if (mctl_present) { 3687 ASSERT(first_mp != mp); 3688 first_mp->b_cont = mp; 3689 } else { 3690 first_mp = mp; 3691 } 3692 } 3693 3694 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 3695 if (IPCL_IS_TCP(connp)) { 3696 (*ip_input_proc)(connp->conn_sqp, first_mp, 3697 connp->conn_recv, connp, SQTAG_IP6_TCP_INPUT); 3698 } else { 3699 putnext(connp->conn_rq, first_mp); 3700 CONN_DEC_REF(connp); 3701 } 3702 } 3703 3704 /* 3705 * Fanout for UDP packets. 3706 * The caller puts <fport, lport> in the ports parameter. 3707 * ire_type must be IRE_BROADCAST for multicast and broadcast packets. 3708 * 3709 * If SO_REUSEADDR is set all multicast and broadcast packets 3710 * will be delivered to all streams bound to the same port. 3711 * 3712 * Zones notes: 3713 * Multicast packets will be distributed to streams in all zones. 3714 */ 3715 static void 3716 ip_fanout_udp_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, uint32_t ports, 3717 ill_t *ill, ill_t *inill, uint_t flags, boolean_t mctl_present, 3718 zoneid_t zoneid) 3719 { 3720 uint32_t dstport, srcport; 3721 in6_addr_t dst; 3722 mblk_t *first_mp; 3723 boolean_t secure; 3724 conn_t *connp; 3725 connf_t *connfp; 3726 conn_t *first_conn; 3727 conn_t *next_conn; 3728 mblk_t *mp1, *first_mp1; 3729 in6_addr_t src; 3730 boolean_t shared_addr; 3731 3732 first_mp = mp; 3733 if (mctl_present) { 3734 mp = first_mp->b_cont; 3735 secure = ipsec_in_is_secure(first_mp); 3736 ASSERT(mp != NULL); 3737 } else { 3738 secure = B_FALSE; 3739 } 3740 3741 /* Extract ports in net byte order */ 3742 dstport = htons(ntohl(ports) & 0xFFFF); 3743 srcport = htons(ntohl(ports) >> 16); 3744 dst = ip6h->ip6_dst; 3745 src = ip6h->ip6_src; 3746 3747 shared_addr = (zoneid == ALL_ZONES); 3748 if (shared_addr) { 3749 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport); 3750 /* 3751 * If no shared MLP is found, tsol_mlp_findzone returns 3752 * ALL_ZONES. In that case, we assume it's SLP, and 3753 * search for the zone based on the packet label. 3754 * That will also return ALL_ZONES on failure, but 3755 * we never allow conn_zoneid to be set to ALL_ZONES. 3756 */ 3757 if (zoneid == ALL_ZONES) 3758 zoneid = tsol_packet_to_zoneid(mp); 3759 } 3760 3761 /* Attempt to find a client stream based on destination port. */ 3762 connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(dstport)]; 3763 mutex_enter(&connfp->connf_lock); 3764 connp = connfp->connf_head; 3765 if (!IN6_IS_ADDR_MULTICAST(&dst)) { 3766 /* 3767 * Not multicast. Send to the one (first) client we find. 3768 */ 3769 while (connp != NULL) { 3770 if (IPCL_UDP_MATCH_V6(connp, dstport, dst, srcport, 3771 src) && connp->conn_zoneid == zoneid && 3772 conn_wantpacket_v6(connp, ill, ip6h, 3773 flags, zoneid)) { 3774 break; 3775 } 3776 connp = connp->conn_next; 3777 } 3778 if (connp == NULL || connp->conn_upq == NULL) 3779 goto notfound; 3780 3781 if (is_system_labeled() && 3782 !tsol_receive_local(mp, &dst, IPV6_VERSION, shared_addr, 3783 connp)) 3784 goto notfound; 3785 3786 /* Found a client */ 3787 CONN_INC_REF(connp); 3788 mutex_exit(&connfp->connf_lock); 3789 3790 if (CONN_UDP_FLOWCTLD(connp)) { 3791 freemsg(first_mp); 3792 CONN_DEC_REF(connp); 3793 return; 3794 } 3795 if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) { 3796 first_mp = ipsec_check_inbound_policy(first_mp, 3797 connp, NULL, ip6h, mctl_present); 3798 if (first_mp == NULL) { 3799 CONN_DEC_REF(connp); 3800 return; 3801 } 3802 } 3803 /* Initiate IPPF processing */ 3804 if (IP6_IN_IPP(flags)) { 3805 uint_t ifindex; 3806 3807 mutex_enter(&ill->ill_lock); 3808 ifindex = ill->ill_phyint->phyint_ifindex; 3809 mutex_exit(&ill->ill_lock); 3810 ip_process(IPP_LOCAL_IN, &mp, ifindex); 3811 if (mp == NULL) { 3812 CONN_DEC_REF(connp); 3813 if (mctl_present) 3814 freeb(first_mp); 3815 return; 3816 } 3817 } 3818 /* 3819 * For link-local always add ifindex so that 3820 * transport can set sin6_scope_id. Avoid it for 3821 * ICMP error fanout. 3822 */ 3823 if ((connp->conn_ipv6_recvpktinfo || 3824 IN6_IS_ADDR_LINKLOCAL(&src)) && 3825 (flags & IP_FF_IP6INFO)) { 3826 /* Add header */ 3827 mp = ip_add_info_v6(mp, inill, &dst); 3828 if (mp == NULL) { 3829 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3830 CONN_DEC_REF(connp); 3831 if (mctl_present) 3832 freeb(first_mp); 3833 return; 3834 } else if (mctl_present) { 3835 first_mp->b_cont = mp; 3836 } else { 3837 first_mp = mp; 3838 } 3839 } 3840 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 3841 3842 /* Send it upstream */ 3843 CONN_UDP_RECV(connp, mp); 3844 3845 IP6_STAT(ip6_udp_fannorm); 3846 CONN_DEC_REF(connp); 3847 if (mctl_present) 3848 freeb(first_mp); 3849 return; 3850 } 3851 3852 while (connp != NULL) { 3853 if ((IPCL_UDP_MATCH_V6(connp, dstport, dst, srcport, src)) && 3854 conn_wantpacket_v6(connp, ill, ip6h, flags, zoneid) && 3855 (!is_system_labeled() || 3856 tsol_receive_local(mp, &dst, IPV6_VERSION, shared_addr, 3857 connp))) 3858 break; 3859 connp = connp->conn_next; 3860 } 3861 3862 if (connp == NULL || connp->conn_upq == NULL) 3863 goto notfound; 3864 3865 first_conn = connp; 3866 3867 CONN_INC_REF(connp); 3868 connp = connp->conn_next; 3869 for (;;) { 3870 while (connp != NULL) { 3871 if (IPCL_UDP_MATCH_V6(connp, dstport, dst, srcport, 3872 src) && conn_wantpacket_v6(connp, ill, ip6h, 3873 flags, zoneid) && 3874 (!is_system_labeled() || 3875 tsol_receive_local(mp, &dst, IPV6_VERSION, 3876 shared_addr, connp))) 3877 break; 3878 connp = connp->conn_next; 3879 } 3880 /* 3881 * Just copy the data part alone. The mctl part is 3882 * needed just for verifying policy and it is never 3883 * sent up. 3884 */ 3885 if (connp == NULL || 3886 (((first_mp1 = dupmsg(first_mp)) == NULL) && 3887 ((first_mp1 = ip_copymsg(first_mp)) 3888 == NULL))) { 3889 /* 3890 * No more interested clients or memory 3891 * allocation failed 3892 */ 3893 connp = first_conn; 3894 break; 3895 } 3896 mp1 = mctl_present ? first_mp1->b_cont : first_mp1; 3897 CONN_INC_REF(connp); 3898 mutex_exit(&connfp->connf_lock); 3899 /* 3900 * For link-local always add ifindex so that transport 3901 * can set sin6_scope_id. Avoid it for ICMP error 3902 * fanout. 3903 */ 3904 if ((connp->conn_ipv6_recvpktinfo || 3905 IN6_IS_ADDR_LINKLOCAL(&src)) && 3906 (flags & IP_FF_IP6INFO)) { 3907 /* Add header */ 3908 mp1 = ip_add_info_v6(mp1, inill, &dst); 3909 } 3910 /* mp1 could have changed */ 3911 if (mctl_present) 3912 first_mp1->b_cont = mp1; 3913 else 3914 first_mp1 = mp1; 3915 if (mp1 == NULL) { 3916 if (mctl_present) 3917 freeb(first_mp1); 3918 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3919 goto next_one; 3920 } 3921 if (CONN_UDP_FLOWCTLD(connp)) { 3922 BUMP_MIB(ill->ill_ip6_mib, udpInOverflows); 3923 freemsg(first_mp1); 3924 goto next_one; 3925 } 3926 3927 if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || 3928 secure) { 3929 first_mp1 = ipsec_check_inbound_policy 3930 (first_mp1, connp, NULL, ip6h, 3931 mctl_present); 3932 } 3933 if (first_mp1 != NULL) { 3934 if (mctl_present) 3935 freeb(first_mp1); 3936 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 3937 3938 /* Send it upstream */ 3939 CONN_UDP_RECV(connp, mp1); 3940 } 3941 next_one: 3942 mutex_enter(&connfp->connf_lock); 3943 /* Follow the next pointer before releasing the conn. */ 3944 next_conn = connp->conn_next; 3945 IP6_STAT(ip6_udp_fanmb); 3946 CONN_DEC_REF(connp); 3947 connp = next_conn; 3948 } 3949 3950 /* Last one. Send it upstream. */ 3951 mutex_exit(&connfp->connf_lock); 3952 3953 /* Initiate IPPF processing */ 3954 if (IP6_IN_IPP(flags)) { 3955 uint_t ifindex; 3956 3957 mutex_enter(&ill->ill_lock); 3958 ifindex = ill->ill_phyint->phyint_ifindex; 3959 mutex_exit(&ill->ill_lock); 3960 ip_process(IPP_LOCAL_IN, &mp, ifindex); 3961 if (mp == NULL) { 3962 CONN_DEC_REF(connp); 3963 if (mctl_present) { 3964 freeb(first_mp); 3965 } 3966 return; 3967 } 3968 } 3969 3970 /* 3971 * For link-local always add ifindex so that transport can set 3972 * sin6_scope_id. Avoid it for ICMP error fanout. 3973 */ 3974 if ((connp->conn_ipv6_recvpktinfo || 3975 IN6_IS_ADDR_LINKLOCAL(&src)) && (flags & IP_FF_IP6INFO)) { 3976 /* Add header */ 3977 mp = ip_add_info_v6(mp, inill, &dst); 3978 if (mp == NULL) { 3979 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3980 CONN_DEC_REF(connp); 3981 if (mctl_present) 3982 freeb(first_mp); 3983 return; 3984 } else if (mctl_present) { 3985 first_mp->b_cont = mp; 3986 } else { 3987 first_mp = mp; 3988 } 3989 } 3990 if (CONN_UDP_FLOWCTLD(connp)) { 3991 BUMP_MIB(ill->ill_ip6_mib, udpInOverflows); 3992 freemsg(mp); 3993 } else { 3994 if (CONN_INBOUND_POLICY_PRESENT_V6(connp) || secure) { 3995 first_mp = ipsec_check_inbound_policy(first_mp, 3996 connp, NULL, ip6h, mctl_present); 3997 if (first_mp == NULL) { 3998 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 3999 CONN_DEC_REF(connp); 4000 return; 4001 } 4002 } 4003 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 4004 4005 /* Send it upstream */ 4006 CONN_UDP_RECV(connp, mp); 4007 } 4008 IP6_STAT(ip6_udp_fanmb); 4009 CONN_DEC_REF(connp); 4010 if (mctl_present) 4011 freeb(first_mp); 4012 return; 4013 4014 notfound: 4015 mutex_exit(&connfp->connf_lock); 4016 /* 4017 * No one bound to this port. Is 4018 * there a client that wants all 4019 * unclaimed datagrams? 4020 */ 4021 if (ipcl_proto_fanout_v6[IPPROTO_UDP].connf_head != NULL) { 4022 ip_fanout_proto_v6(q, first_mp, ip6h, ill, inill, IPPROTO_UDP, 4023 0, flags | IP_FF_RAWIP | IP_FF_IP6INFO, mctl_present, 4024 zoneid); 4025 } else { 4026 if (ip_fanout_send_icmp_v6(q, first_mp, flags, 4027 ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0, 4028 mctl_present, zoneid)) { 4029 BUMP_MIB(&ip_mib, udpNoPorts); 4030 } 4031 } 4032 } 4033 4034 /* 4035 * int ip_find_hdr_v6() 4036 * 4037 * This routine is used by the upper layer protocols and the IP tunnel 4038 * module to: 4039 * - Set extension header pointers to appropriate locations 4040 * - Determine IPv6 header length and return it 4041 * - Return a pointer to the last nexthdr value 4042 * 4043 * The caller must initialize ipp_fields. 4044 * 4045 * NOTE: If multiple extension headers of the same type are present, 4046 * ip_find_hdr_v6() will set the respective extension header pointers 4047 * to the first one that it encounters in the IPv6 header. It also 4048 * skips fragment headers. This routine deals with malformed packets 4049 * of various sorts in which case the returned length is up to the 4050 * malformed part. 4051 */ 4052 int 4053 ip_find_hdr_v6(mblk_t *mp, ip6_t *ip6h, ip6_pkt_t *ipp, uint8_t *nexthdrp) 4054 { 4055 uint_t length, ehdrlen; 4056 uint8_t nexthdr; 4057 uint8_t *whereptr, *endptr; 4058 ip6_dest_t *tmpdstopts; 4059 ip6_rthdr_t *tmprthdr; 4060 ip6_hbh_t *tmphopopts; 4061 ip6_frag_t *tmpfraghdr; 4062 4063 length = IPV6_HDR_LEN; 4064 whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */ 4065 endptr = mp->b_wptr; 4066 4067 nexthdr = ip6h->ip6_nxt; 4068 while (whereptr < endptr) { 4069 /* Is there enough left for len + nexthdr? */ 4070 if (whereptr + MIN_EHDR_LEN > endptr) 4071 goto done; 4072 4073 switch (nexthdr) { 4074 case IPPROTO_HOPOPTS: 4075 tmphopopts = (ip6_hbh_t *)whereptr; 4076 ehdrlen = 8 * (tmphopopts->ip6h_len + 1); 4077 if ((uchar_t *)tmphopopts + ehdrlen > endptr) 4078 goto done; 4079 nexthdr = tmphopopts->ip6h_nxt; 4080 /* return only 1st hbh */ 4081 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) { 4082 ipp->ipp_fields |= IPPF_HOPOPTS; 4083 ipp->ipp_hopopts = tmphopopts; 4084 ipp->ipp_hopoptslen = ehdrlen; 4085 } 4086 break; 4087 case IPPROTO_DSTOPTS: 4088 tmpdstopts = (ip6_dest_t *)whereptr; 4089 ehdrlen = 8 * (tmpdstopts->ip6d_len + 1); 4090 if ((uchar_t *)tmpdstopts + ehdrlen > endptr) 4091 goto done; 4092 nexthdr = tmpdstopts->ip6d_nxt; 4093 /* 4094 * ipp_dstopts is set to the destination header after a 4095 * routing header. 4096 * Assume it is a post-rthdr destination header 4097 * and adjust when we find an rthdr. 4098 */ 4099 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) { 4100 ipp->ipp_fields |= IPPF_DSTOPTS; 4101 ipp->ipp_dstopts = tmpdstopts; 4102 ipp->ipp_dstoptslen = ehdrlen; 4103 } 4104 break; 4105 case IPPROTO_ROUTING: 4106 tmprthdr = (ip6_rthdr_t *)whereptr; 4107 ehdrlen = 8 * (tmprthdr->ip6r_len + 1); 4108 if ((uchar_t *)tmprthdr + ehdrlen > endptr) 4109 goto done; 4110 nexthdr = tmprthdr->ip6r_nxt; 4111 /* return only 1st rthdr */ 4112 if (!(ipp->ipp_fields & IPPF_RTHDR)) { 4113 ipp->ipp_fields |= IPPF_RTHDR; 4114 ipp->ipp_rthdr = tmprthdr; 4115 ipp->ipp_rthdrlen = ehdrlen; 4116 } 4117 /* 4118 * Make any destination header we've seen be a 4119 * pre-rthdr destination header. 4120 */ 4121 if (ipp->ipp_fields & IPPF_DSTOPTS) { 4122 ipp->ipp_fields &= ~IPPF_DSTOPTS; 4123 ipp->ipp_fields |= IPPF_RTDSTOPTS; 4124 ipp->ipp_rtdstopts = ipp->ipp_dstopts; 4125 ipp->ipp_dstopts = NULL; 4126 ipp->ipp_rtdstoptslen = ipp->ipp_dstoptslen; 4127 ipp->ipp_dstoptslen = 0; 4128 } 4129 break; 4130 case IPPROTO_FRAGMENT: 4131 /* 4132 * Fragment headers are skipped. Currently, only 4133 * IP cares for their existence. If anyone other 4134 * than IP ever has the need to know about the 4135 * location of fragment headers, support can be 4136 * added to the ip6_pkt_t at that time. 4137 */ 4138 tmpfraghdr = (ip6_frag_t *)whereptr; 4139 ehdrlen = sizeof (ip6_frag_t); 4140 if ((uchar_t *)tmpfraghdr + ehdrlen > endptr) 4141 goto done; 4142 nexthdr = tmpfraghdr->ip6f_nxt; 4143 break; 4144 case IPPROTO_NONE: 4145 default: 4146 goto done; 4147 } 4148 length += ehdrlen; 4149 whereptr += ehdrlen; 4150 } 4151 done: 4152 if (nexthdrp != NULL) 4153 *nexthdrp = nexthdr; 4154 return (length); 4155 } 4156 4157 int 4158 ip_hdr_complete_v6(ip6_t *ip6h, zoneid_t zoneid) 4159 { 4160 ire_t *ire; 4161 4162 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) { 4163 ire = ire_lookup_local_v6(zoneid); 4164 if (ire == NULL) { 4165 ip1dbg(("ip_hdr_complete_v6: no source IRE\n")); 4166 return (1); 4167 } 4168 ip6h->ip6_src = ire->ire_addr_v6; 4169 ire_refrele(ire); 4170 } 4171 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 4172 ip6h->ip6_hops = ipv6_def_hops; 4173 return (0); 4174 } 4175 4176 /* 4177 * Try to determine where and what are the IPv6 header length and 4178 * pointer to nexthdr value for the upper layer protocol (or an 4179 * unknown next hdr). 4180 * 4181 * Parameters returns a pointer to the nexthdr value; 4182 * Must handle malformed packets of various sorts. 4183 * Function returns failure for malformed cases. 4184 */ 4185 boolean_t 4186 ip_hdr_length_nexthdr_v6(mblk_t *mp, ip6_t *ip6h, uint16_t *hdr_length_ptr, 4187 uint8_t **nexthdrpp) 4188 { 4189 uint16_t length; 4190 uint_t ehdrlen; 4191 uint8_t *nexthdrp; 4192 uint8_t *whereptr; 4193 uint8_t *endptr; 4194 ip6_dest_t *desthdr; 4195 ip6_rthdr_t *rthdr; 4196 ip6_frag_t *fraghdr; 4197 4198 length = IPV6_HDR_LEN; 4199 whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */ 4200 endptr = mp->b_wptr; 4201 4202 nexthdrp = &ip6h->ip6_nxt; 4203 while (whereptr < endptr) { 4204 /* Is there enough left for len + nexthdr? */ 4205 if (whereptr + MIN_EHDR_LEN > endptr) 4206 break; 4207 4208 switch (*nexthdrp) { 4209 case IPPROTO_HOPOPTS: 4210 case IPPROTO_DSTOPTS: 4211 /* Assumes the headers are identical for hbh and dst */ 4212 desthdr = (ip6_dest_t *)whereptr; 4213 ehdrlen = 8 * (desthdr->ip6d_len + 1); 4214 if ((uchar_t *)desthdr + ehdrlen > endptr) 4215 return (B_FALSE); 4216 nexthdrp = &desthdr->ip6d_nxt; 4217 break; 4218 case IPPROTO_ROUTING: 4219 rthdr = (ip6_rthdr_t *)whereptr; 4220 ehdrlen = 8 * (rthdr->ip6r_len + 1); 4221 if ((uchar_t *)rthdr + ehdrlen > endptr) 4222 return (B_FALSE); 4223 nexthdrp = &rthdr->ip6r_nxt; 4224 break; 4225 case IPPROTO_FRAGMENT: 4226 fraghdr = (ip6_frag_t *)whereptr; 4227 ehdrlen = sizeof (ip6_frag_t); 4228 if ((uchar_t *)&fraghdr[1] > endptr) 4229 return (B_FALSE); 4230 nexthdrp = &fraghdr->ip6f_nxt; 4231 break; 4232 case IPPROTO_NONE: 4233 /* No next header means we're finished */ 4234 default: 4235 *hdr_length_ptr = length; 4236 *nexthdrpp = nexthdrp; 4237 return (B_TRUE); 4238 } 4239 length += ehdrlen; 4240 whereptr += ehdrlen; 4241 *hdr_length_ptr = length; 4242 *nexthdrpp = nexthdrp; 4243 } 4244 switch (*nexthdrp) { 4245 case IPPROTO_HOPOPTS: 4246 case IPPROTO_DSTOPTS: 4247 case IPPROTO_ROUTING: 4248 case IPPROTO_FRAGMENT: 4249 /* 4250 * If any know extension headers are still to be processed, 4251 * the packet's malformed (or at least all the IP header(s) are 4252 * not in the same mblk - and that should never happen. 4253 */ 4254 return (B_FALSE); 4255 4256 default: 4257 /* 4258 * If we get here, we know that all of the IP headers were in 4259 * the same mblk, even if the ULP header is in the next mblk. 4260 */ 4261 *hdr_length_ptr = length; 4262 *nexthdrpp = nexthdrp; 4263 return (B_TRUE); 4264 } 4265 } 4266 4267 /* 4268 * Return the length of the IPv6 related headers (including extension headers) 4269 * Returns a length even if the packet is malformed. 4270 */ 4271 int 4272 ip_hdr_length_v6(mblk_t *mp, ip6_t *ip6h) 4273 { 4274 uint16_t hdr_len; 4275 uint8_t *nexthdrp; 4276 4277 (void) ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_len, &nexthdrp); 4278 return (hdr_len); 4279 } 4280 4281 /* 4282 * Select an ill for the packet by considering load spreading across 4283 * a different ill in the group if dst_ill is part of some group. 4284 */ 4285 static ill_t * 4286 ip_newroute_get_dst_ill_v6(ill_t *dst_ill) 4287 { 4288 ill_t *ill; 4289 4290 /* 4291 * We schedule irrespective of whether the source address is 4292 * INADDR_UNSPECIED or not. 4293 */ 4294 ill = illgrp_scheduler(dst_ill); 4295 if (ill == NULL) 4296 return (NULL); 4297 4298 /* 4299 * For groups with names ip_sioctl_groupname ensures that all 4300 * ills are of same type. For groups without names, ifgrp_insert 4301 * ensures this. 4302 */ 4303 ASSERT(dst_ill->ill_type == ill->ill_type); 4304 4305 return (ill); 4306 } 4307 4308 /* 4309 * IPv6 - 4310 * ip_newroute_v6 is called by ip_rput_data_v6 or ip_wput_v6 whenever we need 4311 * to send out a packet to a destination address for which we do not have 4312 * specific routing information. 4313 * 4314 * Handle non-multicast packets. If ill is non-NULL the match is done 4315 * for that ill. 4316 * 4317 * When a specific ill is specified (using IPV6_PKTINFO, 4318 * IPV6_MULTICAST_IF, or IPV6_BOUND_IF) we will only match 4319 * on routing entries (ftable and ctable) that have a matching 4320 * ire->ire_ipif->ipif_ill. Thus this can only be used 4321 * for destinations that are on-link for the specific ill 4322 * and that can appear on multiple links. Thus it is useful 4323 * for multicast destinations, link-local destinations, and 4324 * at some point perhaps for site-local destinations (if the 4325 * node sits at a site boundary). 4326 * We create the cache entries in the regular ctable since 4327 * it can not "confuse" things for other destinations. 4328 * table. 4329 * 4330 * When ill is part of a ill group, we subject the packets 4331 * to load spreading even if the ill is specified by the 4332 * means described above. We disable only for IPV6_BOUND_PIF 4333 * and for the cases where IP6I_ATTACH_IF is set i.e NS/NA/ 4334 * Echo replies to link-local destinations have IP6I_ATTACH_IF 4335 * set. 4336 * 4337 * NOTE : These are the scopes of some of the variables that point at IRE, 4338 * which needs to be followed while making any future modifications 4339 * to avoid memory leaks. 4340 * 4341 * - ire and sire are the entries looked up initially by 4342 * ire_ftable_lookup_v6. 4343 * - ipif_ire is used to hold the interface ire associated with 4344 * the new cache ire. But it's scope is limited, so we always REFRELE 4345 * it before branching out to error paths. 4346 * - save_ire is initialized before ire_create, so that ire returned 4347 * by ire_create will not over-write the ire. We REFRELE save_ire 4348 * before breaking out of the switch. 4349 * 4350 * Thus on failures, we have to REFRELE only ire and sire, if they 4351 * are not NULL. 4352 * 4353 * v6srcp may be used in the future. Currently unused. 4354 */ 4355 /* ARGSUSED */ 4356 void 4357 ip_newroute_v6(queue_t *q, mblk_t *mp, const in6_addr_t *v6dstp, 4358 const in6_addr_t *v6srcp, ill_t *ill, zoneid_t zoneid) 4359 { 4360 in6_addr_t v6gw; 4361 in6_addr_t dst; 4362 ire_t *ire = NULL; 4363 ipif_t *src_ipif = NULL; 4364 ill_t *dst_ill = NULL; 4365 ire_t *sire = NULL; 4366 ire_t *save_ire; 4367 mblk_t *dlureq_mp; 4368 ip6_t *ip6h; 4369 int err = 0; 4370 mblk_t *first_mp; 4371 ipsec_out_t *io; 4372 ill_t *attach_ill = NULL; 4373 ushort_t ire_marks = 0; 4374 int match_flags; 4375 boolean_t ip6i_present; 4376 ire_t *first_sire = NULL; 4377 mblk_t *copy_mp = NULL; 4378 mblk_t *xmit_mp = NULL; 4379 in6_addr_t save_dst; 4380 uint32_t multirt_flags = 4381 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP; 4382 boolean_t multirt_is_resolvable; 4383 boolean_t multirt_resolve_next; 4384 boolean_t need_rele = B_FALSE; 4385 boolean_t do_attach_ill = B_FALSE; 4386 boolean_t ip6_asp_table_held = B_FALSE; 4387 tsol_ire_gw_secattr_t *attrp = NULL; 4388 tsol_gcgrp_t *gcgrp = NULL; 4389 tsol_gcgrp_addr_t ga; 4390 4391 ASSERT(!IN6_IS_ADDR_MULTICAST(v6dstp)); 4392 4393 first_mp = mp; 4394 if (mp->b_datap->db_type == M_CTL) { 4395 mp = mp->b_cont; 4396 io = (ipsec_out_t *)first_mp->b_rptr; 4397 ASSERT(io->ipsec_out_type == IPSEC_OUT); 4398 } else { 4399 io = NULL; 4400 } 4401 4402 /* 4403 * If this end point is bound to IPIF_NOFAILOVER, set bnf_ill and 4404 * bind_to_nofailover B_TRUE. We can't use conn to determine as it 4405 * could be NULL. 4406 * 4407 * This information can appear either in an ip6i_t or an IPSEC_OUT 4408 * message. 4409 */ 4410 ip6h = (ip6_t *)mp->b_rptr; 4411 ip6i_present = (ip6h->ip6_nxt == IPPROTO_RAW); 4412 if (ip6i_present || (io != NULL && io->ipsec_out_attach_if)) { 4413 if (!ip6i_present || 4414 ((ip6i_t *)ip6h)->ip6i_flags & IP6I_ATTACH_IF) { 4415 attach_ill = ip_grab_attach_ill(ill, first_mp, 4416 (ip6i_present ? ((ip6i_t *)ip6h)->ip6i_ifindex : 4417 io->ipsec_out_ill_index), B_TRUE); 4418 /* Failure case frees things for us. */ 4419 if (attach_ill == NULL) 4420 return; 4421 4422 /* 4423 * Check if we need an ire that will not be 4424 * looked up by anybody else i.e. HIDDEN. 4425 */ 4426 if (ill_is_probeonly(attach_ill)) 4427 ire_marks = IRE_MARK_HIDDEN; 4428 } 4429 } 4430 4431 if (IN6_IS_ADDR_LOOPBACK(v6dstp)) { 4432 ip1dbg(("ip_newroute_v6: dst with loopback addr\n")); 4433 goto icmp_err_ret; 4434 } else if ((v6srcp != NULL) && IN6_IS_ADDR_LOOPBACK(v6srcp)) { 4435 ip1dbg(("ip_newroute_v6: src with loopback addr\n")); 4436 goto icmp_err_ret; 4437 } 4438 4439 /* 4440 * If this IRE is created for forwarding or it is not for 4441 * TCP traffic, mark it as temporary. 4442 * 4443 * Is it sufficient just to check the next header?? 4444 */ 4445 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ip6h->ip6_nxt)) 4446 ire_marks |= IRE_MARK_TEMPORARY; 4447 4448 /* 4449 * Get what we can from ire_ftable_lookup_v6 which will follow an IRE 4450 * chain until it gets the most specific information available. 4451 * For example, we know that there is no IRE_CACHE for this dest, 4452 * but there may be an IRE_OFFSUBNET which specifies a gateway. 4453 * ire_ftable_lookup_v6 will look up the gateway, etc. 4454 */ 4455 4456 if (ill == NULL) { 4457 match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4458 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE | MATCH_IRE_SECATTR; 4459 ire = ire_ftable_lookup_v6(v6dstp, 0, 0, 0, 4460 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp), 4461 match_flags); 4462 /* 4463 * ire_add_then_send -> ip_newroute_v6 in the CGTP case passes 4464 * in a NULL ill, but the packet could be a neighbor 4465 * solicitation/advertisment and could have a valid attach_ill. 4466 */ 4467 if (attach_ill != NULL) 4468 ill_refrele(attach_ill); 4469 } else { 4470 if (attach_ill != NULL) { 4471 /* 4472 * attach_ill is set only for communicating with 4473 * on-link hosts. So, don't look for DEFAULT. 4474 * ip_wput_v6 passes the right ill in this case and 4475 * hence we can assert. 4476 */ 4477 ASSERT(ill == attach_ill); 4478 ill_refrele(attach_ill); 4479 do_attach_ill = B_TRUE; 4480 match_flags = MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL; 4481 } else { 4482 match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 4483 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL_GROUP; 4484 } 4485 match_flags |= MATCH_IRE_PARENT | MATCH_IRE_SECATTR; 4486 ire = ire_ftable_lookup_v6(v6dstp, NULL, NULL, 0, ill->ill_ipif, 4487 &sire, zoneid, 0, MBLK_GETLABEL(mp), match_flags); 4488 } 4489 4490 ip3dbg(("ip_newroute_v6: ire_ftable_lookup_v6() " 4491 "returned ire %p, sire %p\n", (void *)ire, (void *)sire)); 4492 4493 if (zoneid == ALL_ZONES && ire != NULL) { 4494 /* 4495 * In the forwarding case, we can use a route from any zone 4496 * since we won't change the source address. We can easily 4497 * assert that the source address is already set when there's no 4498 * ip6_info header - otherwise we'd have to call pullupmsg(). 4499 */ 4500 ASSERT(ip6i_present || 4501 !IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)); 4502 zoneid = ire->ire_zoneid; 4503 } 4504 4505 /* 4506 * We enter a loop that will be run only once in most cases. 4507 * The loop is re-entered in the case where the destination 4508 * can be reached through multiple RTF_MULTIRT-flagged routes. 4509 * The intention is to compute multiple routes to a single 4510 * destination in a single ip_newroute_v6 call. 4511 * The information is contained in sire->ire_flags. 4512 */ 4513 do { 4514 multirt_resolve_next = B_FALSE; 4515 4516 if (dst_ill != NULL) { 4517 ill_refrele(dst_ill); 4518 dst_ill = NULL; 4519 } 4520 if (src_ipif != NULL) { 4521 ipif_refrele(src_ipif); 4522 src_ipif = NULL; 4523 } 4524 if ((sire != NULL) && sire->ire_flags & RTF_MULTIRT) { 4525 ip3dbg(("ip_newroute_v6: starting new resolution " 4526 "with first_mp %p, tag %d\n", 4527 (void *)first_mp, MULTIRT_DEBUG_TAGGED(first_mp))); 4528 4529 /* 4530 * We check if there are trailing unresolved routes for 4531 * the destination contained in sire. 4532 */ 4533 multirt_is_resolvable = ire_multirt_lookup_v6(&ire, 4534 &sire, multirt_flags, MBLK_GETLABEL(mp)); 4535 4536 ip3dbg(("ip_newroute_v6: multirt_is_resolvable %d, " 4537 "ire %p, sire %p\n", 4538 multirt_is_resolvable, (void *)ire, (void *)sire)); 4539 4540 if (!multirt_is_resolvable) { 4541 /* 4542 * No more multirt routes to resolve; give up 4543 * (all routes resolved or no more resolvable 4544 * routes). 4545 */ 4546 if (ire != NULL) { 4547 ire_refrele(ire); 4548 ire = NULL; 4549 } 4550 } else { 4551 ASSERT(sire != NULL); 4552 ASSERT(ire != NULL); 4553 /* 4554 * We simply use first_sire as a flag that 4555 * indicates if a resolvable multirt route has 4556 * already been found during the preceding 4557 * loops. If it is not the case, we may have 4558 * to send an ICMP error to report that the 4559 * destination is unreachable. We do not 4560 * IRE_REFHOLD first_sire. 4561 */ 4562 if (first_sire == NULL) { 4563 first_sire = sire; 4564 } 4565 } 4566 } 4567 if ((ire == NULL) || (ire == sire)) { 4568 /* 4569 * either ire == NULL (the destination cannot be 4570 * resolved) or ire == sire (the gateway cannot be 4571 * resolved). At this point, there are no more routes 4572 * to resolve for the destination, thus we exit. 4573 */ 4574 if (ip_debug > 3) { 4575 /* ip2dbg */ 4576 pr_addr_dbg("ip_newroute_v6: " 4577 "can't resolve %s\n", AF_INET6, v6dstp); 4578 } 4579 ip3dbg(("ip_newroute_v6: " 4580 "ire %p, sire %p, first_sire %p\n", 4581 (void *)ire, (void *)sire, (void *)first_sire)); 4582 4583 if (sire != NULL) { 4584 ire_refrele(sire); 4585 sire = NULL; 4586 } 4587 4588 if (first_sire != NULL) { 4589 /* 4590 * At least one multirt route has been found 4591 * in the same ip_newroute() call; there is no 4592 * need to report an ICMP error. 4593 * first_sire was not IRE_REFHOLDed. 4594 */ 4595 MULTIRT_DEBUG_UNTAG(first_mp); 4596 freemsg(first_mp); 4597 return; 4598 } 4599 ip_rts_change_v6(RTM_MISS, v6dstp, 0, 0, 0, 0, 0, 0, 4600 RTA_DST); 4601 goto icmp_err_ret; 4602 } 4603 4604 ASSERT(ire->ire_ipversion == IPV6_VERSION); 4605 4606 /* 4607 * Verify that the returned IRE does not have either the 4608 * RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is 4609 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER. 4610 */ 4611 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) || 4612 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) 4613 goto icmp_err_ret; 4614 4615 /* 4616 * Increment the ire_ob_pkt_count field for ire if it is an 4617 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and 4618 * increment the same for the parent IRE, sire, if it is some 4619 * sort of prefix IRE (which includes DEFAULT, PREFIX, HOST 4620 * and HOST_REDIRECT). 4621 */ 4622 if ((ire->ire_type & IRE_INTERFACE) != 0) { 4623 UPDATE_OB_PKT_COUNT(ire); 4624 ire->ire_last_used_time = lbolt; 4625 } 4626 4627 if (sire != NULL) { 4628 mutex_enter(&sire->ire_lock); 4629 v6gw = sire->ire_gateway_addr_v6; 4630 mutex_exit(&sire->ire_lock); 4631 ASSERT((sire->ire_type & (IRE_CACHETABLE | 4632 IRE_INTERFACE)) == 0); 4633 UPDATE_OB_PKT_COUNT(sire); 4634 sire->ire_last_used_time = lbolt; 4635 } else { 4636 v6gw = ipv6_all_zeros; 4637 } 4638 4639 /* 4640 * We have a route to reach the destination. 4641 * 4642 * 1) If the interface is part of ill group, try to get a new 4643 * ill taking load spreading into account. 4644 * 4645 * 2) After selecting the ill, get a source address that might 4646 * create good inbound load spreading and that matches the 4647 * right scope. ipif_select_source_v6 does this for us. 4648 * 4649 * If the application specified the ill (ifindex), we still 4650 * load spread. Only if the packets needs to go out specifically 4651 * on a given ill e.g. bind to IPIF_NOFAILOVER address, 4652 * IPV6_BOUND_PIF we don't try to use a different ill for load 4653 * spreading. 4654 */ 4655 if (!do_attach_ill) { 4656 /* 4657 * If the interface belongs to an interface group, 4658 * make sure the next possible interface in the group 4659 * is used. This encourages load spreading among 4660 * peers in an interface group. However, in the case 4661 * of multirouting, load spreading is not used, as we 4662 * actually want to replicate outgoing packets through 4663 * particular interfaces. 4664 * 4665 * Note: While we pick a dst_ill we are really only 4666 * interested in the ill for load spreading. 4667 * The source ipif is determined by source address 4668 * selection below. 4669 */ 4670 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) { 4671 dst_ill = ire->ire_ipif->ipif_ill; 4672 /* For uniformity do a refhold */ 4673 ill_refhold(dst_ill); 4674 } else { 4675 /* 4676 * If we are here trying to create an IRE_CACHE 4677 * for an offlink destination and have the 4678 * IRE_CACHE for the next hop and the latter is 4679 * using virtual IP source address selection i.e 4680 * it's ire->ire_ipif is pointing to a virtual 4681 * network interface (vni) then 4682 * ip_newroute_get_dst_ll() will return the vni 4683 * interface as the dst_ill. Since the vni is 4684 * virtual i.e not associated with any physical 4685 * interface, it cannot be the dst_ill, hence 4686 * in such a case call ip_newroute_get_dst_ll() 4687 * with the stq_ill instead of the ire_ipif ILL. 4688 * The function returns a refheld ill. 4689 */ 4690 if ((ire->ire_type == IRE_CACHE) && 4691 IS_VNI(ire->ire_ipif->ipif_ill)) 4692 dst_ill = ip_newroute_get_dst_ill_v6( 4693 ire->ire_stq->q_ptr); 4694 else 4695 dst_ill = ip_newroute_get_dst_ill_v6( 4696 ire->ire_ipif->ipif_ill); 4697 } 4698 if (dst_ill == NULL) { 4699 if (ip_debug > 2) { 4700 pr_addr_dbg("ip_newroute_v6 : no dst " 4701 "ill for dst %s\n", 4702 AF_INET6, v6dstp); 4703 } 4704 goto icmp_err_ret; 4705 } else if (dst_ill->ill_group == NULL && ill != NULL && 4706 dst_ill != ill) { 4707 /* 4708 * If "ill" is not part of any group, we should 4709 * have found a route matching "ill" as we 4710 * called ire_ftable_lookup_v6 with 4711 * MATCH_IRE_ILL_GROUP. 4712 * Rather than asserting when there is a 4713 * mismatch, we just drop the packet. 4714 */ 4715 ip0dbg(("ip_newroute_v6: BOUND_IF failed : " 4716 "dst_ill %s ill %s\n", 4717 dst_ill->ill_name, 4718 ill->ill_name)); 4719 goto icmp_err_ret; 4720 } 4721 } else { 4722 dst_ill = ire->ire_ipif->ipif_ill; 4723 /* For uniformity do refhold */ 4724 ill_refhold(dst_ill); 4725 /* 4726 * We should have found a route matching ill as we 4727 * called ire_ftable_lookup_v6 with MATCH_IRE_ILL. 4728 * Rather than asserting, while there is a mismatch, 4729 * we just drop the packet. 4730 */ 4731 if (dst_ill != ill) { 4732 ip0dbg(("ip_newroute_v6: Packet dropped as " 4733 "IP6I_ATTACH_IF ill is %s, " 4734 "ire->ire_ipif->ipif_ill is %s\n", 4735 ill->ill_name, 4736 dst_ill->ill_name)); 4737 goto icmp_err_ret; 4738 } 4739 } 4740 /* 4741 * Pick a source address which matches the scope of the 4742 * destination address. 4743 * For RTF_SETSRC routes, the source address is imposed by the 4744 * parent ire (sire). 4745 */ 4746 ASSERT(src_ipif == NULL); 4747 if (ire->ire_type == IRE_IF_RESOLVER && 4748 !IN6_IS_ADDR_UNSPECIFIED(&v6gw) && 4749 ip6_asp_can_lookup()) { 4750 /* 4751 * The ire cache entry we're adding is for the 4752 * gateway itself. The source address in this case 4753 * is relative to the gateway's address. 4754 */ 4755 ip6_asp_table_held = B_TRUE; 4756 src_ipif = ipif_select_source_v6(dst_ill, &v6gw, 4757 RESTRICT_TO_GROUP, IPV6_PREFER_SRC_DEFAULT, zoneid); 4758 if (src_ipif != NULL) 4759 ire_marks |= IRE_MARK_USESRC_CHECK; 4760 } else { 4761 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) { 4762 /* 4763 * Check that the ipif matching the requested 4764 * source address still exists. 4765 */ 4766 src_ipif = ipif_lookup_addr_v6( 4767 &sire->ire_src_addr_v6, NULL, zoneid, 4768 NULL, NULL, NULL, NULL); 4769 } 4770 if (src_ipif == NULL && ip6_asp_can_lookup()) { 4771 ip6_asp_table_held = B_TRUE; 4772 src_ipif = ipif_select_source_v6(dst_ill, 4773 v6dstp, RESTRICT_TO_NONE, 4774 IPV6_PREFER_SRC_DEFAULT, zoneid); 4775 if (src_ipif != NULL) 4776 ire_marks |= IRE_MARK_USESRC_CHECK; 4777 } 4778 } 4779 4780 if (src_ipif == NULL) { 4781 if (ip_debug > 2) { 4782 /* ip1dbg */ 4783 pr_addr_dbg("ip_newroute_v6: no src for " 4784 "dst %s\n, ", AF_INET6, v6dstp); 4785 printf("ip_newroute_v6: interface name %s\n", 4786 dst_ill->ill_name); 4787 } 4788 goto icmp_err_ret; 4789 } 4790 4791 if (ip_debug > 3) { 4792 /* ip2dbg */ 4793 pr_addr_dbg("ip_newroute_v6: first hop %s\n", 4794 AF_INET6, &v6gw); 4795 } 4796 ip2dbg(("\tire type %s (%d)\n", 4797 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 4798 4799 /* 4800 * At this point in ip_newroute_v6(), ire is either the 4801 * IRE_CACHE of the next-hop gateway for an off-subnet 4802 * destination or an IRE_INTERFACE type that should be used 4803 * to resolve an on-subnet destination or an on-subnet 4804 * next-hop gateway. 4805 * 4806 * In the IRE_CACHE case, we have the following : 4807 * 4808 * 1) src_ipif - used for getting a source address. 4809 * 4810 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 4811 * means packets using this IRE_CACHE will go out on dst_ill. 4812 * 4813 * 3) The IRE sire will point to the prefix that is the longest 4814 * matching route for the destination. These prefix types 4815 * include IRE_DEFAULT, IRE_PREFIX, IRE_HOST, and 4816 * IRE_HOST_REDIRECT. 4817 * 4818 * The newly created IRE_CACHE entry for the off-subnet 4819 * destination is tied to both the prefix route and the 4820 * interface route used to resolve the next-hop gateway 4821 * via the ire_phandle and ire_ihandle fields, respectively. 4822 * 4823 * In the IRE_INTERFACE case, we have the following : 4824 * 4825 * 1) src_ipif - used for getting a source address. 4826 * 4827 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This 4828 * means packets using the IRE_CACHE that we will build 4829 * here will go out on dst_ill. 4830 * 4831 * 3) sire may or may not be NULL. But, the IRE_CACHE that is 4832 * to be created will only be tied to the IRE_INTERFACE that 4833 * was derived from the ire_ihandle field. 4834 * 4835 * If sire is non-NULL, it means the destination is off-link 4836 * and we will first create the IRE_CACHE for the gateway. 4837 * Next time through ip_newroute_v6, we will create the 4838 * IRE_CACHE for the final destination as described above. 4839 */ 4840 save_ire = ire; 4841 switch (ire->ire_type) { 4842 case IRE_CACHE: { 4843 ire_t *ipif_ire; 4844 4845 ASSERT(sire != NULL); 4846 if (IN6_IS_ADDR_UNSPECIFIED(&v6gw)) { 4847 mutex_enter(&ire->ire_lock); 4848 v6gw = ire->ire_gateway_addr_v6; 4849 mutex_exit(&ire->ire_lock); 4850 } 4851 /* 4852 * We need 3 ire's to create a new cache ire for an 4853 * off-link destination from the cache ire of the 4854 * gateway. 4855 * 4856 * 1. The prefix ire 'sire' 4857 * 2. The cache ire of the gateway 'ire' 4858 * 3. The interface ire 'ipif_ire' 4859 * 4860 * We have (1) and (2). We lookup (3) below. 4861 * 4862 * If there is no interface route to the gateway, 4863 * it is a race condition, where we found the cache 4864 * but the inteface route has been deleted. 4865 */ 4866 ipif_ire = ire_ihandle_lookup_offlink_v6(ire, sire); 4867 if (ipif_ire == NULL) { 4868 ip1dbg(("ip_newroute_v6:" 4869 "ire_ihandle_lookup_offlink_v6 failed\n")); 4870 goto icmp_err_ret; 4871 } 4872 /* 4873 * Assume DL_UNITDATA_REQ is same for all physical 4874 * interfaces in the ifgrp. If it isn't, this code will 4875 * have to be seriously rewhacked to allow the 4876 * fastpath probing (such that I cache the link 4877 * header in the IRE_CACHE) to work over ifgrps. 4878 * We have what we need to build an IRE_CACHE. 4879 */ 4880 /* 4881 * Note: the new ire inherits RTF_SETSRC 4882 * and RTF_MULTIRT to propagate these flags from prefix 4883 * to cache. 4884 */ 4885 4886 /* 4887 * Check cached gateway IRE for any security 4888 * attributes; if found, associate the gateway 4889 * credentials group to the destination IRE. 4890 */ 4891 if ((attrp = save_ire->ire_gw_secattr) != NULL) { 4892 mutex_enter(&attrp->igsa_lock); 4893 if ((gcgrp = attrp->igsa_gcgrp) != NULL) 4894 GCGRP_REFHOLD(gcgrp); 4895 mutex_exit(&attrp->igsa_lock); 4896 } 4897 4898 ire = ire_create_v6( 4899 v6dstp, /* dest address */ 4900 &ipv6_all_ones, /* mask */ 4901 &src_ipif->ipif_v6src_addr, /* source address */ 4902 &v6gw, /* gateway address */ 4903 &save_ire->ire_max_frag, 4904 NULL, /* Fast Path header */ 4905 dst_ill->ill_rq, /* recv-from queue */ 4906 dst_ill->ill_wq, /* send-to queue */ 4907 IRE_CACHE, 4908 NULL, 4909 src_ipif, 4910 &sire->ire_mask_v6, /* Parent mask */ 4911 sire->ire_phandle, /* Parent handle */ 4912 ipif_ire->ire_ihandle, /* Interface handle */ 4913 sire->ire_flags & /* flags if any */ 4914 (RTF_SETSRC | RTF_MULTIRT), 4915 &(sire->ire_uinfo), 4916 NULL, 4917 gcgrp); 4918 4919 if (ire == NULL) { 4920 if (gcgrp != NULL) { 4921 GCGRP_REFRELE(gcgrp); 4922 gcgrp = NULL; 4923 } 4924 ire_refrele(save_ire); 4925 ire_refrele(ipif_ire); 4926 break; 4927 } 4928 4929 /* reference now held by IRE */ 4930 gcgrp = NULL; 4931 4932 ire->ire_marks |= ire_marks; 4933 4934 /* 4935 * Prevent sire and ipif_ire from getting deleted. The 4936 * newly created ire is tied to both of them via the 4937 * phandle and ihandle respectively. 4938 */ 4939 IRB_REFHOLD(sire->ire_bucket); 4940 /* Has it been removed already ? */ 4941 if (sire->ire_marks & IRE_MARK_CONDEMNED) { 4942 IRB_REFRELE(sire->ire_bucket); 4943 ire_refrele(ipif_ire); 4944 ire_refrele(save_ire); 4945 break; 4946 } 4947 4948 IRB_REFHOLD(ipif_ire->ire_bucket); 4949 /* Has it been removed already ? */ 4950 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) { 4951 IRB_REFRELE(ipif_ire->ire_bucket); 4952 IRB_REFRELE(sire->ire_bucket); 4953 ire_refrele(ipif_ire); 4954 ire_refrele(save_ire); 4955 break; 4956 } 4957 4958 xmit_mp = first_mp; 4959 if (ire->ire_flags & RTF_MULTIRT) { 4960 copy_mp = copymsg(first_mp); 4961 if (copy_mp != NULL) { 4962 xmit_mp = copy_mp; 4963 MULTIRT_DEBUG_TAG(first_mp); 4964 } 4965 } 4966 ire_add_then_send(q, ire, xmit_mp); 4967 if (ip6_asp_table_held) { 4968 ip6_asp_table_refrele(); 4969 ip6_asp_table_held = B_FALSE; 4970 } 4971 ire_refrele(save_ire); 4972 4973 /* Assert that sire is not deleted yet. */ 4974 ASSERT(sire->ire_ptpn != NULL); 4975 IRB_REFRELE(sire->ire_bucket); 4976 4977 /* Assert that ipif_ire is not deleted yet. */ 4978 ASSERT(ipif_ire->ire_ptpn != NULL); 4979 IRB_REFRELE(ipif_ire->ire_bucket); 4980 ire_refrele(ipif_ire); 4981 4982 if (copy_mp != NULL) { 4983 /* 4984 * Search for the next unresolved 4985 * multirt route. 4986 */ 4987 copy_mp = NULL; 4988 ipif_ire = NULL; 4989 ire = NULL; 4990 /* re-enter the loop */ 4991 multirt_resolve_next = B_TRUE; 4992 continue; 4993 } 4994 ire_refrele(sire); 4995 ill_refrele(dst_ill); 4996 ipif_refrele(src_ipif); 4997 return; 4998 } 4999 case IRE_IF_NORESOLVER: 5000 /* 5001 * We have what we need to build an IRE_CACHE. 5002 * 5003 * Create a new dlureq_mp with the IPv6 gateway 5004 * address in destination address in the DLPI hdr 5005 * if the physical length is exactly 16 bytes. 5006 */ 5007 if (dst_ill->ill_phys_addr_length == IPV6_ADDR_LEN) { 5008 const in6_addr_t *addr; 5009 5010 if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw)) 5011 addr = &v6gw; 5012 else 5013 addr = v6dstp; 5014 5015 dlureq_mp = ill_dlur_gen((uchar_t *)addr, 5016 dst_ill->ill_phys_addr_length, 5017 dst_ill->ill_sap, 5018 dst_ill->ill_sap_length); 5019 } else { 5020 dlureq_mp = ire->ire_dlureq_mp; 5021 } 5022 if (dlureq_mp == NULL) 5023 break; 5024 /* 5025 * TSol note: We are creating the ire cache for the 5026 * destination 'dst'. If 'dst' is offlink, going 5027 * through the first hop 'gw', the security attributes 5028 * of 'dst' must be set to point to the gateway 5029 * credentials of gateway 'gw'. If 'dst' is onlink, it 5030 * is possible that 'dst' is a potential gateway that is 5031 * referenced by some route that has some security 5032 * attributes. Thus in the former case, we need to do a 5033 * gcgrp_lookup of 'gw' while in the latter case we 5034 * need to do gcgrp_lookup of 'dst' itself. 5035 */ 5036 ga.ga_af = AF_INET6; 5037 if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw)) 5038 ga.ga_addr = v6gw; 5039 else 5040 ga.ga_addr = *v6dstp; 5041 gcgrp = gcgrp_lookup(&ga, B_FALSE); 5042 5043 /* 5044 * Note: the new ire inherits sire flags RTF_SETSRC 5045 * and RTF_MULTIRT to propagate those rules from prefix 5046 * to cache. 5047 */ 5048 ire = ire_create_v6( 5049 v6dstp, /* dest address */ 5050 &ipv6_all_ones, /* mask */ 5051 &src_ipif->ipif_v6src_addr, /* source address */ 5052 &v6gw, /* gateway address */ 5053 &save_ire->ire_max_frag, 5054 NULL, /* Fast Path header */ 5055 dst_ill->ill_rq, /* recv-from queue */ 5056 dst_ill->ill_wq, /* send-to queue */ 5057 IRE_CACHE, 5058 dlureq_mp, 5059 src_ipif, 5060 &save_ire->ire_mask_v6, /* Parent mask */ 5061 (sire != NULL) ? /* Parent handle */ 5062 sire->ire_phandle : 0, 5063 save_ire->ire_ihandle, /* Interface handle */ 5064 (sire != NULL) ? /* flags if any */ 5065 sire->ire_flags & 5066 (RTF_SETSRC | RTF_MULTIRT) : 0, 5067 &(save_ire->ire_uinfo), 5068 NULL, 5069 gcgrp); 5070 5071 if (dst_ill->ill_phys_addr_length == IPV6_ADDR_LEN) 5072 freeb(dlureq_mp); 5073 5074 if (ire == NULL) { 5075 if (gcgrp != NULL) { 5076 GCGRP_REFRELE(gcgrp); 5077 gcgrp = NULL; 5078 } 5079 ire_refrele(save_ire); 5080 break; 5081 } 5082 5083 /* reference now held by IRE */ 5084 gcgrp = NULL; 5085 5086 ire->ire_marks |= ire_marks; 5087 5088 if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw)) 5089 dst = v6gw; 5090 else 5091 dst = *v6dstp; 5092 err = ndp_noresolver(dst_ill, &dst); 5093 if (err != 0) { 5094 ire_refrele(save_ire); 5095 break; 5096 } 5097 5098 /* Prevent save_ire from getting deleted */ 5099 IRB_REFHOLD(save_ire->ire_bucket); 5100 /* Has it been removed already ? */ 5101 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 5102 IRB_REFRELE(save_ire->ire_bucket); 5103 ire_refrele(save_ire); 5104 break; 5105 } 5106 5107 xmit_mp = first_mp; 5108 /* 5109 * In case of MULTIRT, a copy of the current packet 5110 * to send is made to further re-enter the 5111 * loop and attempt another route resolution 5112 */ 5113 if ((sire != NULL) && sire->ire_flags & RTF_MULTIRT) { 5114 copy_mp = copymsg(first_mp); 5115 if (copy_mp != NULL) { 5116 xmit_mp = copy_mp; 5117 MULTIRT_DEBUG_TAG(first_mp); 5118 } 5119 } 5120 ire_add_then_send(q, ire, xmit_mp); 5121 if (ip6_asp_table_held) { 5122 ip6_asp_table_refrele(); 5123 ip6_asp_table_held = B_FALSE; 5124 } 5125 5126 /* Assert that it is not deleted yet. */ 5127 ASSERT(save_ire->ire_ptpn != NULL); 5128 IRB_REFRELE(save_ire->ire_bucket); 5129 ire_refrele(save_ire); 5130 5131 if (copy_mp != NULL) { 5132 /* 5133 * If we found a (no)resolver, we ignore any 5134 * trailing top priority IRE_CACHE in 5135 * further loops. This ensures that we do not 5136 * omit any (no)resolver despite the priority 5137 * in this call. 5138 * IRE_CACHE, if any, will be processed 5139 * by another thread entering ip_newroute(), 5140 * (on resolver response, for example). 5141 * We use this to force multiple parallel 5142 * resolution as soon as a packet needs to be 5143 * sent. The result is, after one packet 5144 * emission all reachable routes are generally 5145 * resolved. 5146 * Otherwise, complete resolution of MULTIRT 5147 * routes would require several emissions as 5148 * side effect. 5149 */ 5150 multirt_flags &= ~MULTIRT_CACHEGW; 5151 5152 /* 5153 * Search for the next unresolved multirt 5154 * route. 5155 */ 5156 copy_mp = NULL; 5157 save_ire = NULL; 5158 ire = NULL; 5159 /* re-enter the loop */ 5160 multirt_resolve_next = B_TRUE; 5161 continue; 5162 } 5163 5164 /* Don't need sire anymore */ 5165 if (sire != NULL) 5166 ire_refrele(sire); 5167 ill_refrele(dst_ill); 5168 ipif_refrele(src_ipif); 5169 return; 5170 5171 case IRE_IF_RESOLVER: 5172 /* 5173 * We can't build an IRE_CACHE yet, but at least we 5174 * found a resolver that can help. 5175 */ 5176 dst = *v6dstp; 5177 5178 /* 5179 * To be at this point in the code with a non-zero gw 5180 * means that dst is reachable through a gateway that 5181 * we have never resolved. By changing dst to the gw 5182 * addr we resolve the gateway first. When 5183 * ire_add_then_send() tries to put the IP dg to dst, 5184 * it will reenter ip_newroute() at which time we will 5185 * find the IRE_CACHE for the gw and create another 5186 * IRE_CACHE above (for dst itself). 5187 */ 5188 if (!IN6_IS_ADDR_UNSPECIFIED(&v6gw)) { 5189 save_dst = dst; 5190 dst = v6gw; 5191 v6gw = ipv6_all_zeros; 5192 } 5193 if (dst_ill->ill_flags & ILLF_XRESOLV) { 5194 /* 5195 * Ask the external resolver to do its thing. 5196 * Make an mblk chain in the following form: 5197 * ARQ_REQ_MBLK-->IRE_MBLK-->packet 5198 */ 5199 mblk_t *ire_mp; 5200 mblk_t *areq_mp; 5201 areq_t *areq; 5202 in6_addr_t *addrp; 5203 5204 ip1dbg(("ip_newroute_v6:ILLF_XRESOLV\n")); 5205 if (ip6_asp_table_held) { 5206 ip6_asp_table_refrele(); 5207 ip6_asp_table_held = B_FALSE; 5208 } 5209 ire = ire_create_mp_v6( 5210 &dst, /* dest address */ 5211 &ipv6_all_ones, /* mask */ 5212 &src_ipif->ipif_v6src_addr, 5213 /* source address */ 5214 &v6gw, /* gateway address */ 5215 NULL, /* Fast Path header */ 5216 dst_ill->ill_rq, /* recv-from queue */ 5217 dst_ill->ill_wq, /* send-to queue */ 5218 IRE_CACHE, 5219 NULL, 5220 src_ipif, 5221 &save_ire->ire_mask_v6, 5222 /* Parent mask */ 5223 0, 5224 save_ire->ire_ihandle, 5225 /* Interface handle */ 5226 0, /* flags if any */ 5227 &(save_ire->ire_uinfo), 5228 NULL, 5229 NULL); 5230 5231 ire_refrele(save_ire); 5232 if (ire == NULL) { 5233 ip1dbg(("ip_newroute_v6:" 5234 "ire is NULL\n")); 5235 break; 5236 } 5237 5238 if ((sire != NULL) && 5239 (sire->ire_flags & RTF_MULTIRT)) { 5240 /* 5241 * processing a copy of the packet to 5242 * send for further resolution loops 5243 */ 5244 copy_mp = copymsg(first_mp); 5245 if (copy_mp != NULL) 5246 MULTIRT_DEBUG_TAG(copy_mp); 5247 } 5248 ire->ire_marks |= ire_marks; 5249 ire_mp = ire->ire_mp; 5250 /* 5251 * Now create or find an nce for this interface. 5252 * The hw addr will need to to be set from 5253 * the reply to the AR_ENTRY_QUERY that 5254 * we're about to send. This will be done in 5255 * ire_add_v6(). 5256 */ 5257 err = ndp_resolver(dst_ill, &dst, mp, zoneid); 5258 switch (err) { 5259 case 0: 5260 /* 5261 * New cache entry created. 5262 * Break, then ask the external 5263 * resolver. 5264 */ 5265 break; 5266 case EINPROGRESS: 5267 /* 5268 * Resolution in progress; 5269 * packet has been queued by 5270 * ndp_resolver(). 5271 */ 5272 ire_delete(ire); 5273 ire = NULL; 5274 /* 5275 * Check if another multirt 5276 * route must be resolved. 5277 */ 5278 if (copy_mp != NULL) { 5279 /* 5280 * If we found a resolver, we 5281 * ignore any trailing top 5282 * priority IRE_CACHE in 5283 * further loops. The reason is 5284 * the same as for noresolver. 5285 */ 5286 multirt_flags &= 5287 ~MULTIRT_CACHEGW; 5288 /* 5289 * Search for the next 5290 * unresolved multirt route. 5291 */ 5292 first_mp = copy_mp; 5293 copy_mp = NULL; 5294 mp = first_mp; 5295 if (mp->b_datap->db_type == 5296 M_CTL) { 5297 mp = mp->b_cont; 5298 } 5299 ASSERT(sire != NULL); 5300 dst = save_dst; 5301 /* 5302 * re-enter the loop 5303 */ 5304 multirt_resolve_next = 5305 B_TRUE; 5306 continue; 5307 } 5308 5309 if (sire != NULL) 5310 ire_refrele(sire); 5311 ill_refrele(dst_ill); 5312 ipif_refrele(src_ipif); 5313 return; 5314 default: 5315 /* 5316 * Transient error; packet will be 5317 * freed. 5318 */ 5319 ire_delete(ire); 5320 ire = NULL; 5321 break; 5322 } 5323 if (err != 0) 5324 break; 5325 /* 5326 * Now set up the AR_ENTRY_QUERY and send it. 5327 */ 5328 areq_mp = ill_arp_alloc(dst_ill, 5329 (uchar_t *)&ipv6_areq_template, 5330 (caddr_t)&dst); 5331 if (areq_mp == NULL) { 5332 ip1dbg(("ip_newroute_v6:" 5333 "areq_mp is NULL\n")); 5334 freemsg(ire_mp); 5335 break; 5336 } 5337 areq = (areq_t *)areq_mp->b_rptr; 5338 addrp = (in6_addr_t *)((char *)areq + 5339 areq->areq_target_addr_offset); 5340 *addrp = dst; 5341 addrp = (in6_addr_t *)((char *)areq + 5342 areq->areq_sender_addr_offset); 5343 *addrp = src_ipif->ipif_v6src_addr; 5344 /* 5345 * link the chain, then send up to the resolver. 5346 */ 5347 linkb(areq_mp, ire_mp); 5348 linkb(areq_mp, mp); 5349 ip1dbg(("ip_newroute_v6:" 5350 "putnext to resolver\n")); 5351 putnext(dst_ill->ill_rq, areq_mp); 5352 /* 5353 * Check if another multirt route 5354 * must be resolved. 5355 */ 5356 ire = NULL; 5357 if (copy_mp != NULL) { 5358 /* 5359 * If we find a resolver, we ignore any 5360 * trailing top priority IRE_CACHE in 5361 * further loops. The reason is the 5362 * same as for noresolver. 5363 */ 5364 multirt_flags &= ~MULTIRT_CACHEGW; 5365 /* 5366 * Search for the next unresolved 5367 * multirt route. 5368 */ 5369 first_mp = copy_mp; 5370 copy_mp = NULL; 5371 mp = first_mp; 5372 if (mp->b_datap->db_type == M_CTL) { 5373 mp = mp->b_cont; 5374 } 5375 ASSERT(sire != NULL); 5376 dst = save_dst; 5377 /* 5378 * re-enter the loop 5379 */ 5380 multirt_resolve_next = B_TRUE; 5381 continue; 5382 } 5383 5384 if (sire != NULL) 5385 ire_refrele(sire); 5386 ill_refrele(dst_ill); 5387 ipif_refrele(src_ipif); 5388 return; 5389 } 5390 /* 5391 * Non-external resolver case. 5392 * 5393 * TSol note: Please see the note above the 5394 * IRE_IF_NORESOLVER case. 5395 */ 5396 ga.ga_af = AF_INET6; 5397 ga.ga_addr = dst; 5398 gcgrp = gcgrp_lookup(&ga, B_FALSE); 5399 5400 ire = ire_create_v6( 5401 &dst, /* dest address */ 5402 &ipv6_all_ones, /* mask */ 5403 &src_ipif->ipif_v6src_addr, /* source address */ 5404 &v6gw, /* gateway address */ 5405 &save_ire->ire_max_frag, 5406 NULL, /* Fast Path header */ 5407 dst_ill->ill_rq, /* recv-from queue */ 5408 dst_ill->ill_wq, /* send-to queue */ 5409 IRE_CACHE, 5410 NULL, 5411 src_ipif, 5412 &save_ire->ire_mask_v6, /* Parent mask */ 5413 0, 5414 save_ire->ire_ihandle, /* Interface handle */ 5415 0, /* flags if any */ 5416 &(save_ire->ire_uinfo), 5417 NULL, 5418 gcgrp); 5419 5420 if (ire == NULL) { 5421 if (gcgrp != NULL) { 5422 GCGRP_REFRELE(gcgrp); 5423 gcgrp = NULL; 5424 } 5425 ire_refrele(save_ire); 5426 break; 5427 } 5428 5429 /* reference now held by IRE */ 5430 gcgrp = NULL; 5431 5432 if ((sire != NULL) && 5433 (sire->ire_flags & RTF_MULTIRT)) { 5434 copy_mp = copymsg(first_mp); 5435 if (copy_mp != NULL) 5436 MULTIRT_DEBUG_TAG(copy_mp); 5437 } 5438 5439 ire->ire_marks |= ire_marks; 5440 err = ndp_resolver(dst_ill, &dst, first_mp, zoneid); 5441 switch (err) { 5442 case 0: 5443 /* Prevent save_ire from getting deleted */ 5444 IRB_REFHOLD(save_ire->ire_bucket); 5445 /* Has it been removed already ? */ 5446 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 5447 IRB_REFRELE(save_ire->ire_bucket); 5448 ire_refrele(save_ire); 5449 break; 5450 } 5451 5452 /* 5453 * We have a resolved cache entry, 5454 * add in the IRE. 5455 */ 5456 ire_add_then_send(q, ire, first_mp); 5457 if (ip6_asp_table_held) { 5458 ip6_asp_table_refrele(); 5459 ip6_asp_table_held = B_FALSE; 5460 } 5461 5462 /* Assert that it is not deleted yet. */ 5463 ASSERT(save_ire->ire_ptpn != NULL); 5464 IRB_REFRELE(save_ire->ire_bucket); 5465 ire_refrele(save_ire); 5466 /* 5467 * Check if another multirt route 5468 * must be resolved. 5469 */ 5470 ire = NULL; 5471 if (copy_mp != NULL) { 5472 /* 5473 * If we find a resolver, we ignore any 5474 * trailing top priority IRE_CACHE in 5475 * further loops. The reason is the 5476 * same as for noresolver. 5477 */ 5478 multirt_flags &= ~MULTIRT_CACHEGW; 5479 /* 5480 * Search for the next unresolved 5481 * multirt route. 5482 */ 5483 first_mp = copy_mp; 5484 copy_mp = NULL; 5485 mp = first_mp; 5486 if (mp->b_datap->db_type == M_CTL) { 5487 mp = mp->b_cont; 5488 } 5489 ASSERT(sire != NULL); 5490 dst = save_dst; 5491 /* 5492 * re-enter the loop 5493 */ 5494 multirt_resolve_next = B_TRUE; 5495 continue; 5496 } 5497 5498 if (sire != NULL) 5499 ire_refrele(sire); 5500 ill_refrele(dst_ill); 5501 ipif_refrele(src_ipif); 5502 return; 5503 5504 case EINPROGRESS: 5505 /* 5506 * mp was consumed - presumably queued. 5507 * No need for ire, presumably resolution is 5508 * in progress, and ire will be added when the 5509 * address is resolved. 5510 */ 5511 if (ip6_asp_table_held) { 5512 ip6_asp_table_refrele(); 5513 ip6_asp_table_held = B_FALSE; 5514 } 5515 ASSERT(ire->ire_nce == NULL); 5516 ire_delete(ire); 5517 ire_refrele(save_ire); 5518 /* 5519 * Check if another multirt route 5520 * must be resolved. 5521 */ 5522 ire = NULL; 5523 if (copy_mp != NULL) { 5524 /* 5525 * If we find a resolver, we ignore any 5526 * trailing top priority IRE_CACHE in 5527 * further loops. The reason is the 5528 * same as for noresolver. 5529 */ 5530 multirt_flags &= ~MULTIRT_CACHEGW; 5531 /* 5532 * Search for the next unresolved 5533 * multirt route. 5534 */ 5535 first_mp = copy_mp; 5536 copy_mp = NULL; 5537 mp = first_mp; 5538 if (mp->b_datap->db_type == M_CTL) { 5539 mp = mp->b_cont; 5540 } 5541 ASSERT(sire != NULL); 5542 dst = save_dst; 5543 /* 5544 * re-enter the loop 5545 */ 5546 multirt_resolve_next = B_TRUE; 5547 continue; 5548 } 5549 if (sire != NULL) 5550 ire_refrele(sire); 5551 ill_refrele(dst_ill); 5552 ipif_refrele(src_ipif); 5553 return; 5554 default: 5555 /* Some transient error */ 5556 ASSERT(ire->ire_nce == NULL); 5557 ire_refrele(save_ire); 5558 break; 5559 } 5560 break; 5561 default: 5562 break; 5563 } 5564 if (ip6_asp_table_held) { 5565 ip6_asp_table_refrele(); 5566 ip6_asp_table_held = B_FALSE; 5567 } 5568 } while (multirt_resolve_next); 5569 5570 err_ret: 5571 ip1dbg(("ip_newroute_v6: dropped\n")); 5572 if (src_ipif != NULL) 5573 ipif_refrele(src_ipif); 5574 if (dst_ill != NULL) { 5575 need_rele = B_TRUE; 5576 ill = dst_ill; 5577 } 5578 if (ill != NULL) { 5579 if (mp->b_prev != NULL) { 5580 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 5581 } else { 5582 BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards); 5583 } 5584 5585 if (need_rele) 5586 ill_refrele(ill); 5587 } else { 5588 if (mp->b_prev != NULL) { 5589 BUMP_MIB(&ip6_mib, ipv6InDiscards); 5590 } else { 5591 BUMP_MIB(&ip6_mib, ipv6OutDiscards); 5592 } 5593 } 5594 /* Did this packet originate externally? */ 5595 if (mp->b_prev) { 5596 mp->b_next = NULL; 5597 mp->b_prev = NULL; 5598 } 5599 if (copy_mp != NULL) { 5600 MULTIRT_DEBUG_UNTAG(copy_mp); 5601 freemsg(copy_mp); 5602 } 5603 MULTIRT_DEBUG_UNTAG(first_mp); 5604 freemsg(first_mp); 5605 if (ire != NULL) 5606 ire_refrele(ire); 5607 if (sire != NULL) 5608 ire_refrele(sire); 5609 return; 5610 5611 icmp_err_ret: 5612 if (ip6_asp_table_held) 5613 ip6_asp_table_refrele(); 5614 if (src_ipif != NULL) 5615 ipif_refrele(src_ipif); 5616 if (dst_ill != NULL) { 5617 need_rele = B_TRUE; 5618 ill = dst_ill; 5619 } 5620 ip1dbg(("ip_newroute_v6: no route\n")); 5621 if (sire != NULL) 5622 ire_refrele(sire); 5623 /* 5624 * We need to set sire to NULL to avoid double freeing if we 5625 * ever goto err_ret from below. 5626 */ 5627 sire = NULL; 5628 ip6h = (ip6_t *)mp->b_rptr; 5629 /* Skip ip6i_t header if present */ 5630 if (ip6h->ip6_nxt == IPPROTO_RAW) { 5631 /* Make sure the IPv6 header is present */ 5632 if ((mp->b_wptr - (uchar_t *)ip6h) < 5633 sizeof (ip6i_t) + IPV6_HDR_LEN) { 5634 if (!pullupmsg(mp, sizeof (ip6i_t) + IPV6_HDR_LEN)) { 5635 ip1dbg(("ip_newroute_v6: pullupmsg failed\n")); 5636 goto err_ret; 5637 } 5638 } 5639 mp->b_rptr += sizeof (ip6i_t); 5640 ip6h = (ip6_t *)mp->b_rptr; 5641 } 5642 /* Did this packet originate externally? */ 5643 if (mp->b_prev) { 5644 if (ill != NULL) { 5645 BUMP_MIB(ill->ill_ip6_mib, ipv6InNoRoutes); 5646 } else { 5647 BUMP_MIB(&ip6_mib, ipv6InNoRoutes); 5648 } 5649 mp->b_next = NULL; 5650 mp->b_prev = NULL; 5651 q = WR(q); 5652 } else { 5653 if (ill != NULL) { 5654 BUMP_MIB(ill->ill_ip6_mib, ipv6OutNoRoutes); 5655 } else { 5656 BUMP_MIB(&ip6_mib, ipv6OutNoRoutes); 5657 } 5658 if (ip_hdr_complete_v6(ip6h, zoneid)) { 5659 /* Failed */ 5660 if (copy_mp != NULL) { 5661 MULTIRT_DEBUG_UNTAG(copy_mp); 5662 freemsg(copy_mp); 5663 } 5664 MULTIRT_DEBUG_UNTAG(first_mp); 5665 freemsg(first_mp); 5666 if (ire != NULL) 5667 ire_refrele(ire); 5668 if (need_rele) 5669 ill_refrele(ill); 5670 return; 5671 } 5672 } 5673 5674 if (need_rele) 5675 ill_refrele(ill); 5676 5677 /* 5678 * At this point we will have ire only if RTF_BLACKHOLE 5679 * or RTF_REJECT flags are set on the IRE. It will not 5680 * generate ICMP6_DST_UNREACH_NOROUTE if RTF_BLACKHOLE is set. 5681 */ 5682 if (ire != NULL) { 5683 if (ire->ire_flags & RTF_BLACKHOLE) { 5684 ire_refrele(ire); 5685 if (copy_mp != NULL) { 5686 MULTIRT_DEBUG_UNTAG(copy_mp); 5687 freemsg(copy_mp); 5688 } 5689 MULTIRT_DEBUG_UNTAG(first_mp); 5690 freemsg(first_mp); 5691 return; 5692 } 5693 ire_refrele(ire); 5694 } 5695 if (ip_debug > 3) { 5696 /* ip2dbg */ 5697 pr_addr_dbg("ip_newroute_v6: no route to %s\n", 5698 AF_INET6, v6dstp); 5699 } 5700 icmp_unreachable_v6(WR(q), first_mp, ICMP6_DST_UNREACH_NOROUTE, 5701 B_FALSE, B_FALSE); 5702 } 5703 5704 /* 5705 * ip_newroute_ipif_v6 is called by ip_wput_v6 and ip_wput_ipsec_out_v6 whenever 5706 * we need to send out a packet to a destination address for which we do not 5707 * have specific routing information. It is only used for multicast packets. 5708 * 5709 * If unspec_src we allow creating an IRE with source address zero. 5710 * ire_send_v6() will delete it after the packet is sent. 5711 */ 5712 void 5713 ip_newroute_ipif_v6(queue_t *q, mblk_t *mp, ipif_t *ipif, 5714 in6_addr_t v6dst, int unspec_src, zoneid_t zoneid) 5715 { 5716 ire_t *ire = NULL; 5717 ipif_t *src_ipif = NULL; 5718 int err = 0; 5719 ill_t *dst_ill = NULL; 5720 ire_t *save_ire; 5721 ushort_t ire_marks = 0; 5722 ipsec_out_t *io; 5723 ill_t *attach_ill = NULL; 5724 ill_t *ill; 5725 ip6_t *ip6h; 5726 mblk_t *first_mp; 5727 boolean_t ip6i_present; 5728 ire_t *fire = NULL; 5729 mblk_t *copy_mp = NULL; 5730 boolean_t multirt_resolve_next; 5731 in6_addr_t *v6dstp = &v6dst; 5732 boolean_t ipif_held = B_FALSE; 5733 boolean_t ill_held = B_FALSE; 5734 boolean_t ip6_asp_table_held = B_FALSE; 5735 5736 /* 5737 * This loop is run only once in most cases. 5738 * We loop to resolve further routes only when the destination 5739 * can be reached through multiple RTF_MULTIRT-flagged ires. 5740 */ 5741 do { 5742 multirt_resolve_next = B_FALSE; 5743 if (dst_ill != NULL) { 5744 ill_refrele(dst_ill); 5745 dst_ill = NULL; 5746 } 5747 5748 if (src_ipif != NULL) { 5749 ipif_refrele(src_ipif); 5750 src_ipif = NULL; 5751 } 5752 ASSERT(ipif != NULL); 5753 ill = ipif->ipif_ill; 5754 5755 ASSERT(!IN6_IS_ADDR_V4MAPPED(v6dstp)); 5756 if (ip_debug > 2) { 5757 /* ip1dbg */ 5758 pr_addr_dbg("ip_newroute_ipif_v6: v6dst %s\n", 5759 AF_INET6, v6dstp); 5760 printf("ip_newroute_ipif_v6: if %s, v6 %d\n", 5761 ill->ill_name, ipif->ipif_isv6); 5762 } 5763 5764 first_mp = mp; 5765 if (mp->b_datap->db_type == M_CTL) { 5766 mp = mp->b_cont; 5767 io = (ipsec_out_t *)first_mp->b_rptr; 5768 ASSERT(io->ipsec_out_type == IPSEC_OUT); 5769 } else { 5770 io = NULL; 5771 } 5772 5773 /* 5774 * If the interface is a pt-pt interface we look for an 5775 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the 5776 * local_address and the pt-pt destination address. 5777 * Otherwise we just match the local address. 5778 */ 5779 if (!(ill->ill_flags & ILLF_MULTICAST)) { 5780 goto err_ret; 5781 } 5782 /* 5783 * If this end point is bound to IPIF_NOFAILOVER, set bnf_ill 5784 * and bind_to_nofailover B_TRUE. We can't use conn to determine 5785 * as it could be NULL. 5786 * 5787 * This information can appear either in an ip6i_t or an 5788 * IPSEC_OUT message. 5789 */ 5790 ip6h = (ip6_t *)mp->b_rptr; 5791 ip6i_present = (ip6h->ip6_nxt == IPPROTO_RAW); 5792 if (ip6i_present || (io != NULL && io->ipsec_out_attach_if)) { 5793 if (!ip6i_present || 5794 ((ip6i_t *)ip6h)->ip6i_flags & IP6I_ATTACH_IF) { 5795 attach_ill = ip_grab_attach_ill(ill, first_mp, 5796 (ip6i_present ? 5797 ((ip6i_t *)ip6h)->ip6i_ifindex : 5798 io->ipsec_out_ill_index), B_TRUE); 5799 /* Failure case frees things for us. */ 5800 if (attach_ill == NULL) 5801 return; 5802 5803 /* 5804 * Check if we need an ire that will not be 5805 * looked up by anybody else i.e. HIDDEN. 5806 */ 5807 if (ill_is_probeonly(attach_ill)) 5808 ire_marks = IRE_MARK_HIDDEN; 5809 } 5810 } 5811 5812 /* 5813 * We check if an IRE_OFFSUBNET for the addr that goes through 5814 * ipif exists. We need it to determine if the RTF_SETSRC and/or 5815 * RTF_MULTIRT flags must be honored. 5816 */ 5817 fire = ipif_lookup_multi_ire_v6(ipif, v6dstp); 5818 ip2dbg(("ip_newroute_ipif_v6: " 5819 "ipif_lookup_multi_ire_v6(" 5820 "ipif %p, dst %08x) = fire %p\n", 5821 (void *)ipif, ntohl(V4_PART_OF_V6((*v6dstp))), 5822 (void *)fire)); 5823 5824 /* 5825 * If the application specified the ill (ifindex), we still 5826 * load spread. Only if the packets needs to go out specifically 5827 * on a given ill e.g. binding to IPIF_NOFAILOVER address or 5828 * IPV6_BOUND_PIF, or there is a parent ire entry that specified 5829 * multirouting, then we don't try to use a different ill for 5830 * load spreading. 5831 */ 5832 if (attach_ill == NULL) { 5833 /* 5834 * If the interface belongs to an interface group, 5835 * make sure the next possible interface in the group 5836 * is used. This encourages load spreading among peers 5837 * in an interface group. 5838 * 5839 * Note: While we pick a dst_ill we are really only 5840 * interested in the ill for load spreading. The source 5841 * ipif is determined by source address selection below. 5842 */ 5843 if ((fire != NULL) && (fire->ire_flags & RTF_MULTIRT)) { 5844 dst_ill = ipif->ipif_ill; 5845 /* For uniformity do a refhold */ 5846 ill_refhold(dst_ill); 5847 } else { 5848 /* refheld by ip_newroute_get_dst_ill_v6 */ 5849 dst_ill = 5850 ip_newroute_get_dst_ill_v6(ipif->ipif_ill); 5851 } 5852 if (dst_ill == NULL) { 5853 if (ip_debug > 2) { 5854 pr_addr_dbg("ip_newroute_ipif_v6: " 5855 "no dst ill for dst %s\n", 5856 AF_INET6, v6dstp); 5857 } 5858 goto err_ret; 5859 } 5860 } else { 5861 dst_ill = ipif->ipif_ill; 5862 /* 5863 * ip_wput_v6 passes the right ipif for IPIF_NOFAILOVER 5864 * and IPV6_BOUND_PIF case. 5865 */ 5866 ASSERT(dst_ill == attach_ill); 5867 /* attach_ill is already refheld */ 5868 } 5869 /* 5870 * Pick a source address which matches the scope of the 5871 * destination address. 5872 * For RTF_SETSRC routes, the source address is imposed by the 5873 * parent ire (fire). 5874 */ 5875 ASSERT(src_ipif == NULL); 5876 if ((fire != NULL) && (fire->ire_flags & RTF_SETSRC)) { 5877 /* 5878 * Check that the ipif matching the requested source 5879 * address still exists. 5880 */ 5881 src_ipif = 5882 ipif_lookup_addr_v6(&fire->ire_src_addr_v6, 5883 NULL, zoneid, NULL, NULL, NULL, NULL); 5884 } 5885 if (src_ipif == NULL && ip6_asp_can_lookup()) { 5886 ip6_asp_table_held = B_TRUE; 5887 src_ipif = ipif_select_source_v6(dst_ill, v6dstp, 5888 RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT, zoneid); 5889 } 5890 5891 if (src_ipif == NULL) { 5892 if (!unspec_src) { 5893 if (ip_debug > 2) { 5894 /* ip1dbg */ 5895 pr_addr_dbg("ip_newroute_ipif_v6: " 5896 "no src for dst %s\n,", 5897 AF_INET6, v6dstp); 5898 printf(" through interface %s\n", 5899 dst_ill->ill_name); 5900 } 5901 goto err_ret; 5902 } 5903 /* Use any ipif for source */ 5904 for (src_ipif = dst_ill->ill_ipif; src_ipif != NULL; 5905 src_ipif = src_ipif->ipif_next) { 5906 if ((src_ipif->ipif_flags & IPIF_UP) && 5907 IN6_IS_ADDR_UNSPECIFIED( 5908 &src_ipif->ipif_v6src_addr)) 5909 break; 5910 } 5911 if (src_ipif == NULL) { 5912 if (ip_debug > 2) { 5913 /* ip1dbg */ 5914 pr_addr_dbg("ip_newroute_ipif_v6: " 5915 "no src for dst %s\n ", 5916 AF_INET6, v6dstp); 5917 printf("ip_newroute_ipif_v6: if %s" 5918 "(UNSPEC_SRC)\n", 5919 dst_ill->ill_name); 5920 } 5921 goto err_ret; 5922 } 5923 src_ipif = ipif; 5924 ipif_refhold(src_ipif); 5925 } 5926 ire = ipif_to_ire_v6(ipif); 5927 if (ire == NULL) { 5928 if (ip_debug > 2) { 5929 /* ip1dbg */ 5930 pr_addr_dbg("ip_newroute_ipif_v6: v6src %s\n", 5931 AF_INET6, &ipif->ipif_v6lcl_addr); 5932 printf("ip_newroute_ipif_v6: " 5933 "if %s\n", dst_ill->ill_name); 5934 } 5935 goto err_ret; 5936 } 5937 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) 5938 goto err_ret; 5939 5940 ASSERT(ire->ire_ipversion == IPV6_VERSION); 5941 5942 ip1dbg(("ip_newroute_ipif_v6: interface type %s (%d),", 5943 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type)); 5944 if (ip_debug > 2) { 5945 /* ip1dbg */ 5946 pr_addr_dbg(" address %s\n", 5947 AF_INET6, &ire->ire_src_addr_v6); 5948 } 5949 save_ire = ire; 5950 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p\n", 5951 (void *)ire, (void *)ipif)); 5952 5953 if ((fire != NULL) && (fire->ire_flags & RTF_MULTIRT)) { 5954 /* 5955 * an IRE_OFFSUBET was looked up 5956 * on that interface. 5957 * this ire has RTF_MULTIRT flag, 5958 * so the resolution loop 5959 * will be re-entered to resolve 5960 * additional routes on other 5961 * interfaces. For that purpose, 5962 * a copy of the packet is 5963 * made at this point. 5964 */ 5965 fire->ire_last_used_time = lbolt; 5966 copy_mp = copymsg(first_mp); 5967 if (copy_mp) { 5968 MULTIRT_DEBUG_TAG(copy_mp); 5969 } 5970 } 5971 5972 ASSERT((attach_ill == NULL) || (dst_ill == attach_ill)); 5973 switch (ire->ire_type) { 5974 case IRE_IF_NORESOLVER: { 5975 /* We have what we need to build an IRE_CACHE. */ 5976 mblk_t *dlureq_mp; 5977 5978 /* 5979 * Create a new dlureq_mp with the 5980 * IPv6 gateway address in destination address in the 5981 * DLPI hdr if the physical length is exactly 16 bytes. 5982 */ 5983 ASSERT(dst_ill->ill_isv6); 5984 if (dst_ill->ill_phys_addr_length == IPV6_ADDR_LEN) { 5985 dlureq_mp = ill_dlur_gen((uchar_t *)v6dstp, 5986 dst_ill->ill_phys_addr_length, 5987 dst_ill->ill_sap, 5988 dst_ill->ill_sap_length); 5989 } else { 5990 dlureq_mp = ire->ire_dlureq_mp; 5991 } 5992 5993 if (dlureq_mp == NULL) 5994 break; 5995 /* 5996 * The newly created ire will inherit the flags of the 5997 * parent ire, if any. 5998 */ 5999 ire = ire_create_v6( 6000 v6dstp, /* dest address */ 6001 &ipv6_all_ones, /* mask */ 6002 &src_ipif->ipif_v6src_addr, /* source address */ 6003 NULL, /* gateway address */ 6004 &save_ire->ire_max_frag, 6005 NULL, /* Fast Path header */ 6006 dst_ill->ill_rq, /* recv-from queue */ 6007 dst_ill->ill_wq, /* send-to queue */ 6008 IRE_CACHE, 6009 dlureq_mp, 6010 src_ipif, 6011 NULL, 6012 (fire != NULL) ? /* Parent handle */ 6013 fire->ire_phandle : 0, 6014 save_ire->ire_ihandle, /* Interface handle */ 6015 (fire != NULL) ? 6016 (fire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) : 6017 0, 6018 &ire_uinfo_null, 6019 NULL, 6020 NULL); 6021 6022 if (dst_ill->ill_phys_addr_length == IPV6_ADDR_LEN) 6023 freeb(dlureq_mp); 6024 6025 if (ire == NULL) { 6026 ire_refrele(save_ire); 6027 break; 6028 } 6029 6030 ire->ire_marks |= ire_marks; 6031 6032 err = ndp_noresolver(dst_ill, v6dstp); 6033 if (err != 0) { 6034 ire_refrele(save_ire); 6035 break; 6036 } 6037 6038 /* Prevent save_ire from getting deleted */ 6039 IRB_REFHOLD(save_ire->ire_bucket); 6040 /* Has it been removed already ? */ 6041 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 6042 IRB_REFRELE(save_ire->ire_bucket); 6043 ire_refrele(save_ire); 6044 break; 6045 } 6046 6047 ire_add_then_send(q, ire, first_mp); 6048 if (ip6_asp_table_held) { 6049 ip6_asp_table_refrele(); 6050 ip6_asp_table_held = B_FALSE; 6051 } 6052 6053 /* Assert that it is not deleted yet. */ 6054 ASSERT(save_ire->ire_ptpn != NULL); 6055 IRB_REFRELE(save_ire->ire_bucket); 6056 ire_refrele(save_ire); 6057 if (fire != NULL) { 6058 ire_refrele(fire); 6059 fire = NULL; 6060 } 6061 6062 /* 6063 * The resolution loop is re-entered if we 6064 * actually are in a multirouting case. 6065 */ 6066 if (copy_mp != NULL) { 6067 boolean_t need_resolve = 6068 ire_multirt_need_resolve_v6(v6dstp, 6069 MBLK_GETLABEL(copy_mp)); 6070 if (!need_resolve) { 6071 MULTIRT_DEBUG_UNTAG(copy_mp); 6072 freemsg(copy_mp); 6073 copy_mp = NULL; 6074 } else { 6075 /* 6076 * ipif_lookup_group_v6() calls 6077 * ire_lookup_multi_v6() that uses 6078 * ire_ftable_lookup_v6() to find 6079 * an IRE_INTERFACE for the group. 6080 * In the multirt case, 6081 * ire_lookup_multi_v6() then invokes 6082 * ire_multirt_lookup_v6() to find 6083 * the next resolvable ire. 6084 * As a result, we obtain a new 6085 * interface, derived from the 6086 * next ire. 6087 */ 6088 if (ipif_held) { 6089 ipif_refrele(ipif); 6090 ipif_held = B_FALSE; 6091 } 6092 ipif = ipif_lookup_group_v6(v6dstp, 6093 zoneid); 6094 ip2dbg(("ip_newroute_ipif: " 6095 "multirt dst %08x, ipif %p\n", 6096 ntohl(V4_PART_OF_V6((*v6dstp))), 6097 (void *)ipif)); 6098 if (ipif != NULL) { 6099 ipif_held = B_TRUE; 6100 mp = copy_mp; 6101 copy_mp = NULL; 6102 multirt_resolve_next = 6103 B_TRUE; 6104 continue; 6105 } else { 6106 freemsg(copy_mp); 6107 } 6108 } 6109 } 6110 ill_refrele(dst_ill); 6111 if (ipif_held) { 6112 ipif_refrele(ipif); 6113 ipif_held = B_FALSE; 6114 } 6115 if (src_ipif != NULL) 6116 ipif_refrele(src_ipif); 6117 return; 6118 } 6119 case IRE_IF_RESOLVER: { 6120 6121 ASSERT(dst_ill->ill_isv6); 6122 6123 /* 6124 * We obtain a partial IRE_CACHE which we will pass 6125 * along with the resolver query. When the response 6126 * comes back it will be there ready for us to add. 6127 */ 6128 /* 6129 * the newly created ire will inherit the flags of the 6130 * parent ire, if any. 6131 */ 6132 ire = ire_create_v6( 6133 v6dstp, /* dest address */ 6134 &ipv6_all_ones, /* mask */ 6135 &src_ipif->ipif_v6src_addr, /* source address */ 6136 NULL, /* gateway address */ 6137 &save_ire->ire_max_frag, 6138 NULL, /* Fast Path header */ 6139 dst_ill->ill_rq, /* recv-from queue */ 6140 dst_ill->ill_wq, /* send-to queue */ 6141 IRE_CACHE, 6142 NULL, 6143 src_ipif, 6144 NULL, 6145 (fire != NULL) ? /* Parent handle */ 6146 fire->ire_phandle : 0, 6147 save_ire->ire_ihandle, /* Interface handle */ 6148 (fire != NULL) ? 6149 (fire->ire_flags & (RTF_SETSRC | RTF_MULTIRT)) : 6150 0, 6151 &ire_uinfo_null, 6152 NULL, 6153 NULL); 6154 6155 if (ire == NULL) { 6156 ire_refrele(save_ire); 6157 break; 6158 } 6159 6160 ire->ire_marks |= ire_marks; 6161 6162 /* Resolve and add ire to the ctable */ 6163 err = ndp_resolver(dst_ill, v6dstp, first_mp, zoneid); 6164 switch (err) { 6165 case 0: 6166 /* Prevent save_ire from getting deleted */ 6167 IRB_REFHOLD(save_ire->ire_bucket); 6168 /* Has it been removed already ? */ 6169 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) { 6170 IRB_REFRELE(save_ire->ire_bucket); 6171 ire_refrele(save_ire); 6172 break; 6173 } 6174 /* 6175 * We have a resolved cache entry, 6176 * add in the IRE. 6177 */ 6178 ire_add_then_send(q, ire, first_mp); 6179 if (ip6_asp_table_held) { 6180 ip6_asp_table_refrele(); 6181 ip6_asp_table_held = B_FALSE; 6182 } 6183 6184 /* Assert that it is not deleted yet. */ 6185 ASSERT(save_ire->ire_ptpn != NULL); 6186 IRB_REFRELE(save_ire->ire_bucket); 6187 ire_refrele(save_ire); 6188 if (fire != NULL) { 6189 ire_refrele(fire); 6190 fire = NULL; 6191 } 6192 6193 /* 6194 * The resolution loop is re-entered if we 6195 * actually are in a multirouting case. 6196 */ 6197 if (copy_mp != NULL) { 6198 boolean_t need_resolve = 6199 ire_multirt_need_resolve_v6(v6dstp, 6200 MBLK_GETLABEL(copy_mp)); 6201 if (!need_resolve) { 6202 MULTIRT_DEBUG_UNTAG(copy_mp); 6203 freemsg(copy_mp); 6204 copy_mp = NULL; 6205 } else { 6206 /* 6207 * ipif_lookup_group_v6() calls 6208 * ire_lookup_multi_v6() that 6209 * uses ire_ftable_lookup_v6() 6210 * to find an IRE_INTERFACE for 6211 * the group. In the multirt 6212 * case, ire_lookup_multi_v6() 6213 * then invokes 6214 * ire_multirt_lookup_v6() to 6215 * find the next resolvable ire. 6216 * As a result, we obtain a new 6217 * interface, derived from the 6218 * next ire. 6219 */ 6220 if (ipif_held) { 6221 ipif_refrele(ipif); 6222 ipif_held = B_FALSE; 6223 } 6224 ipif = ipif_lookup_group_v6( 6225 v6dstp, zoneid); 6226 ip2dbg(("ip_newroute_ipif: " 6227 "multirt dst %08x, " 6228 "ipif %p\n", 6229 ntohl(V4_PART_OF_V6( 6230 (*v6dstp))), 6231 (void *)ipif)); 6232 if (ipif != NULL) { 6233 ipif_held = B_TRUE; 6234 mp = copy_mp; 6235 copy_mp = NULL; 6236 multirt_resolve_next = 6237 B_TRUE; 6238 continue; 6239 } else { 6240 freemsg(copy_mp); 6241 } 6242 } 6243 } 6244 ill_refrele(dst_ill); 6245 if (ipif_held) { 6246 ipif_refrele(ipif); 6247 ipif_held = B_FALSE; 6248 } 6249 if (src_ipif != NULL) 6250 ipif_refrele(src_ipif); 6251 return; 6252 6253 case EINPROGRESS: 6254 /* 6255 * mp was consumed - presumably queued. 6256 * No need for ire, presumably resolution is 6257 * in progress, and ire will be added when the 6258 * address is resolved. 6259 */ 6260 if (ip6_asp_table_held) { 6261 ip6_asp_table_refrele(); 6262 ip6_asp_table_held = B_FALSE; 6263 } 6264 ire_delete(ire); 6265 ire_refrele(save_ire); 6266 if (fire != NULL) { 6267 ire_refrele(fire); 6268 fire = NULL; 6269 } 6270 6271 /* 6272 * The resolution loop is re-entered if we 6273 * actually are in a multirouting case. 6274 */ 6275 if (copy_mp != NULL) { 6276 boolean_t need_resolve = 6277 ire_multirt_need_resolve_v6(v6dstp, 6278 MBLK_GETLABEL(copy_mp)); 6279 if (!need_resolve) { 6280 MULTIRT_DEBUG_UNTAG(copy_mp); 6281 freemsg(copy_mp); 6282 copy_mp = NULL; 6283 } else { 6284 /* 6285 * ipif_lookup_group_v6() calls 6286 * ire_lookup_multi_v6() that 6287 * uses ire_ftable_lookup_v6() 6288 * to find an IRE_INTERFACE for 6289 * the group. In the multirt 6290 * case, ire_lookup_multi_v6() 6291 * then invokes 6292 * ire_multirt_lookup_v6() to 6293 * find the next resolvable ire. 6294 * As a result, we obtain a new 6295 * interface, derived from the 6296 * next ire. 6297 */ 6298 if (ipif_held) { 6299 ipif_refrele(ipif); 6300 ipif_held = B_FALSE; 6301 } 6302 ipif = ipif_lookup_group_v6( 6303 v6dstp, zoneid); 6304 ip2dbg(("ip_newroute_ipif: " 6305 "multirt dst %08x, " 6306 "ipif %p\n", 6307 ntohl(V4_PART_OF_V6( 6308 (*v6dstp))), 6309 (void *)ipif)); 6310 if (ipif != NULL) { 6311 ipif_held = B_TRUE; 6312 mp = copy_mp; 6313 copy_mp = NULL; 6314 multirt_resolve_next = 6315 B_TRUE; 6316 continue; 6317 } else { 6318 freemsg(copy_mp); 6319 } 6320 } 6321 } 6322 ill_refrele(dst_ill); 6323 if (ipif_held) { 6324 ipif_refrele(ipif); 6325 ipif_held = B_FALSE; 6326 } 6327 if (src_ipif != NULL) 6328 ipif_refrele(src_ipif); 6329 return; 6330 default: 6331 /* Some transient error */ 6332 ire_refrele(save_ire); 6333 break; 6334 } 6335 break; 6336 } 6337 default: 6338 break; 6339 } 6340 if (ip6_asp_table_held) { 6341 ip6_asp_table_refrele(); 6342 ip6_asp_table_held = B_FALSE; 6343 } 6344 } while (multirt_resolve_next); 6345 6346 err_ret: 6347 if (ip6_asp_table_held) 6348 ip6_asp_table_refrele(); 6349 if (ire != NULL) 6350 ire_refrele(ire); 6351 if (fire != NULL) 6352 ire_refrele(fire); 6353 if (ipif != NULL && ipif_held) 6354 ipif_refrele(ipif); 6355 if (src_ipif != NULL) 6356 ipif_refrele(src_ipif); 6357 /* Multicast - no point in trying to generate ICMP error */ 6358 ASSERT((attach_ill == NULL) || (dst_ill == attach_ill)); 6359 if (dst_ill != NULL) { 6360 ill = dst_ill; 6361 ill_held = B_TRUE; 6362 } 6363 if (mp->b_prev || mp->b_next) { 6364 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6365 } else { 6366 BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards); 6367 } 6368 ip1dbg(("ip_newroute_ipif_v6: dropped\n")); 6369 mp->b_next = NULL; 6370 mp->b_prev = NULL; 6371 freemsg(first_mp); 6372 if (ill_held) 6373 ill_refrele(ill); 6374 } 6375 6376 /* 6377 * Parse and process any hop-by-hop or destination options. 6378 * 6379 * Assumes that q is an ill read queue so that ICMP errors for link-local 6380 * destinations are sent out the correct interface. 6381 * 6382 * Returns -1 if there was an error and mp has been consumed. 6383 * Returns 0 if no special action is needed. 6384 * Returns 1 if the packet contained a router alert option for this node 6385 * which is verified to be "interesting/known" for our implementation. 6386 * 6387 * XXX Note: In future as more hbh or dest options are defined, 6388 * it may be better to have different routines for hbh and dest 6389 * options as opt_type fields other than IP6OPT_PAD1 and IP6OPT_PADN 6390 * may have same value in different namespaces. Or is it same namespace ?? 6391 * Current code checks for each opt_type (other than pads) if it is in 6392 * the expected nexthdr (hbh or dest) 6393 */ 6394 static int 6395 ip_process_options_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, 6396 uint8_t *optptr, uint_t optlen, uint8_t hdr_type) 6397 { 6398 uint8_t opt_type; 6399 uint_t optused; 6400 int ret = 0; 6401 mblk_t *first_mp; 6402 const char *errtype; 6403 6404 first_mp = mp; 6405 if (mp->b_datap->db_type == M_CTL) { 6406 mp = mp->b_cont; 6407 } 6408 6409 while (optlen != 0) { 6410 opt_type = *optptr; 6411 if (opt_type == IP6OPT_PAD1) { 6412 optused = 1; 6413 } else { 6414 if (optlen < 2) 6415 goto bad_opt; 6416 errtype = "malformed"; 6417 if (opt_type == ip6opt_ls) { 6418 optused = 2 + optptr[1]; 6419 if (optused > optlen) 6420 goto bad_opt; 6421 } else switch (opt_type) { 6422 case IP6OPT_PADN: 6423 /* 6424 * Note:We don't verify that (N-2) pad octets 6425 * are zero as required by spec. Adhere to 6426 * "be liberal in what you accept..." part of 6427 * implementation philosophy (RFC791,RFC1122) 6428 */ 6429 optused = 2 + optptr[1]; 6430 if (optused > optlen) 6431 goto bad_opt; 6432 break; 6433 6434 case IP6OPT_JUMBO: 6435 if (hdr_type != IPPROTO_HOPOPTS) 6436 goto opt_error; 6437 goto opt_error; /* XXX Not implemented! */ 6438 6439 case IP6OPT_ROUTER_ALERT: { 6440 struct ip6_opt_router *or; 6441 6442 if (hdr_type != IPPROTO_HOPOPTS) 6443 goto opt_error; 6444 optused = 2 + optptr[1]; 6445 if (optused > optlen) 6446 goto bad_opt; 6447 or = (struct ip6_opt_router *)optptr; 6448 /* Check total length and alignment */ 6449 if (optused != sizeof (*or) || 6450 ((uintptr_t)or->ip6or_value & 0x1) != 0) 6451 goto opt_error; 6452 /* Check value */ 6453 switch (*((uint16_t *)or->ip6or_value)) { 6454 case IP6_ALERT_MLD: 6455 case IP6_ALERT_RSVP: 6456 ret = 1; 6457 } 6458 break; 6459 } 6460 case IP6OPT_HOME_ADDRESS: { 6461 /* 6462 * Minimal support for the home address option 6463 * (which is required by all IPv6 nodes). 6464 * Implement by just swapping the home address 6465 * and source address. 6466 * XXX Note: this has IPsec implications since 6467 * AH needs to take this into account. 6468 * Also, when IPsec is used we need to ensure 6469 * that this is only processed once 6470 * in the received packet (to avoid swapping 6471 * back and forth). 6472 * NOTE:This option processing is considered 6473 * to be unsafe and prone to a denial of 6474 * service attack. 6475 * The current processing is not safe even with 6476 * IPsec secured IP packets. Since the home 6477 * address option processing requirement still 6478 * is in the IETF draft and in the process of 6479 * being redefined for its usage, it has been 6480 * decided to turn off the option by default. 6481 * If this section of code needs to be executed, 6482 * ndd variable ip6_ignore_home_address_opt 6483 * should be set to 0 at the user's own risk. 6484 */ 6485 struct ip6_opt_home_address *oh; 6486 in6_addr_t tmp; 6487 6488 if (ipv6_ignore_home_address_opt) 6489 goto opt_error; 6490 6491 if (hdr_type != IPPROTO_DSTOPTS) 6492 goto opt_error; 6493 optused = 2 + optptr[1]; 6494 if (optused > optlen) 6495 goto bad_opt; 6496 6497 /* 6498 * We did this dest. opt the first time 6499 * around (i.e. before AH processing). 6500 * If we've done AH... stop now. 6501 */ 6502 if (first_mp != mp) { 6503 ipsec_in_t *ii; 6504 6505 ii = (ipsec_in_t *)first_mp->b_rptr; 6506 if (ii->ipsec_in_ah_sa != NULL) 6507 break; 6508 } 6509 6510 oh = (struct ip6_opt_home_address *)optptr; 6511 /* Check total length and alignment */ 6512 if (optused < sizeof (*oh) || 6513 ((uintptr_t)oh->ip6oh_addr & 0x7) != 0) 6514 goto opt_error; 6515 /* Swap ip6_src and the home address */ 6516 tmp = ip6h->ip6_src; 6517 /* XXX Note: only 8 byte alignment option */ 6518 ip6h->ip6_src = *(in6_addr_t *)oh->ip6oh_addr; 6519 *(in6_addr_t *)oh->ip6oh_addr = tmp; 6520 break; 6521 } 6522 6523 case IP6OPT_TUNNEL_LIMIT: 6524 if (hdr_type != IPPROTO_DSTOPTS) { 6525 goto opt_error; 6526 } 6527 optused = 2 + optptr[1]; 6528 if (optused > optlen) { 6529 goto bad_opt; 6530 } 6531 if (optused != 3) { 6532 goto opt_error; 6533 } 6534 break; 6535 6536 default: 6537 errtype = "unknown"; 6538 /* FALLTHROUGH */ 6539 opt_error: 6540 switch (IP6OPT_TYPE(opt_type)) { 6541 case IP6OPT_TYPE_SKIP: 6542 optused = 2 + optptr[1]; 6543 if (optused > optlen) 6544 goto bad_opt; 6545 ip1dbg(("ip_process_options_v6: %s " 6546 "opt 0x%x skipped\n", 6547 errtype, opt_type)); 6548 break; 6549 case IP6OPT_TYPE_DISCARD: 6550 ip1dbg(("ip_process_options_v6: %s " 6551 "opt 0x%x; packet dropped\n", 6552 errtype, opt_type)); 6553 freemsg(first_mp); 6554 return (-1); 6555 case IP6OPT_TYPE_ICMP: 6556 icmp_param_problem_v6(WR(q), first_mp, 6557 ICMP6_PARAMPROB_OPTION, 6558 (uint32_t)(optptr - 6559 (uint8_t *)ip6h), 6560 B_FALSE, B_FALSE); 6561 return (-1); 6562 case IP6OPT_TYPE_FORCEICMP: 6563 icmp_param_problem_v6(WR(q), first_mp, 6564 ICMP6_PARAMPROB_OPTION, 6565 (uint32_t)(optptr - 6566 (uint8_t *)ip6h), 6567 B_FALSE, B_TRUE); 6568 return (-1); 6569 default: 6570 ASSERT(0); 6571 } 6572 } 6573 } 6574 optlen -= optused; 6575 optptr += optused; 6576 } 6577 return (ret); 6578 6579 bad_opt: 6580 icmp_param_problem_v6(WR(q), first_mp, ICMP6_PARAMPROB_OPTION, 6581 (uint32_t)(optptr - (uint8_t *)ip6h), 6582 B_FALSE, B_FALSE); 6583 return (-1); 6584 } 6585 6586 /* 6587 * Process a routing header that is not yet empty. 6588 * Only handles type 0 routing headers. 6589 */ 6590 static void 6591 ip_process_rthdr(queue_t *q, mblk_t *mp, ip6_t *ip6h, ip6_rthdr_t *rth, 6592 ill_t *ill, uint_t flags, mblk_t *hada_mp) 6593 { 6594 ip6_rthdr0_t *rthdr; 6595 uint_t ehdrlen; 6596 uint_t numaddr; 6597 in6_addr_t *addrptr; 6598 in6_addr_t tmp; 6599 6600 ASSERT(rth->ip6r_segleft != 0); 6601 6602 if (!ipv6_forward_src_routed) { 6603 /* XXX Check for source routed out same interface? */ 6604 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 6605 BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors); 6606 freemsg(hada_mp); 6607 freemsg(mp); 6608 return; 6609 } 6610 6611 if (rth->ip6r_type != 0) { 6612 if (hada_mp != NULL) 6613 goto hada_drop; 6614 icmp_param_problem_v6(WR(q), mp, 6615 ICMP6_PARAMPROB_HEADER, 6616 (uint32_t)((uchar_t *)&rth->ip6r_type - (uchar_t *)ip6h), 6617 B_FALSE, B_FALSE); 6618 return; 6619 } 6620 rthdr = (ip6_rthdr0_t *)rth; 6621 ehdrlen = 8 * (rthdr->ip6r0_len + 1); 6622 ASSERT(mp->b_rptr + ehdrlen <= mp->b_wptr); 6623 addrptr = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr)); 6624 /* rthdr->ip6r0_len is twice the number of addresses in the header */ 6625 if (rthdr->ip6r0_len & 0x1) { 6626 /* An odd length is impossible */ 6627 if (hada_mp != NULL) 6628 goto hada_drop; 6629 icmp_param_problem_v6(WR(q), mp, 6630 ICMP6_PARAMPROB_HEADER, 6631 (uint32_t)((uchar_t *)&rthdr->ip6r0_len - (uchar_t *)ip6h), 6632 B_FALSE, B_FALSE); 6633 return; 6634 } 6635 numaddr = rthdr->ip6r0_len / 2; 6636 if (rthdr->ip6r0_segleft > numaddr) { 6637 /* segleft exceeds number of addresses in routing header */ 6638 if (hada_mp != NULL) 6639 goto hada_drop; 6640 icmp_param_problem_v6(WR(q), mp, 6641 ICMP6_PARAMPROB_HEADER, 6642 (uint32_t)((uchar_t *)&rthdr->ip6r0_segleft - 6643 (uchar_t *)ip6h), 6644 B_FALSE, B_FALSE); 6645 return; 6646 } 6647 addrptr += (numaddr - rthdr->ip6r0_segleft); 6648 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) || 6649 IN6_IS_ADDR_MULTICAST(addrptr)) { 6650 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6651 freemsg(hada_mp); 6652 freemsg(mp); 6653 return; 6654 } 6655 /* Swap */ 6656 tmp = *addrptr; 6657 *addrptr = ip6h->ip6_dst; 6658 ip6h->ip6_dst = tmp; 6659 rthdr->ip6r0_segleft--; 6660 /* Don't allow any mapped addresses - ip_wput_v6 can't handle them */ 6661 if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_dst)) { 6662 if (hada_mp != NULL) 6663 goto hada_drop; 6664 icmp_unreachable_v6(WR(q), mp, ICMP6_DST_UNREACH_NOROUTE, 6665 B_FALSE, B_FALSE); 6666 return; 6667 } 6668 ip_rput_data_v6(q, ill, mp, ip6h, flags, hada_mp); 6669 return; 6670 hada_drop: 6671 /* IPsec kstats: bean counter? */ 6672 freemsg(hada_mp); 6673 freemsg(mp); 6674 } 6675 6676 /* 6677 * Read side put procedure for IPv6 module. 6678 */ 6679 static void 6680 ip_rput_v6(queue_t *q, mblk_t *mp) 6681 { 6682 mblk_t *mp1, *first_mp, *hada_mp = NULL; 6683 ip6_t *ip6h; 6684 boolean_t ll_multicast = B_FALSE, mctl_present = B_FALSE; 6685 ill_t *ill; 6686 struct iocblk *iocp; 6687 uint_t flags = 0; 6688 6689 ill = (ill_t *)q->q_ptr; 6690 if (ill->ill_state_flags & ILL_CONDEMNED) { 6691 union DL_primitives *dl; 6692 6693 dl = (union DL_primitives *)mp->b_rptr; 6694 /* 6695 * Things are opening or closing - only accept DLPI 6696 * ack messages. If the stream is closing and ip_wsrv 6697 * has completed, ip_close is out of the qwait, but has 6698 * not yet completed qprocsoff. Don't proceed any further 6699 * because the ill has been cleaned up and things hanging 6700 * off the ill have been freed. 6701 */ 6702 if ((mp->b_datap->db_type != M_PCPROTO) || 6703 (dl->dl_primitive == DL_UNITDATA_IND)) { 6704 inet_freemsg(mp); 6705 return; 6706 } 6707 } 6708 6709 switch (mp->b_datap->db_type) { 6710 case M_DATA: 6711 break; 6712 6713 case M_PROTO: 6714 case M_PCPROTO: 6715 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive != 6716 DL_UNITDATA_IND) { 6717 /* Go handle anything other than data elsewhere. */ 6718 ip_rput_dlpi(q, mp); 6719 return; 6720 } 6721 #define dlur ((dl_unitdata_ind_t *)mp->b_rptr) 6722 ll_multicast = dlur->dl_group_address; 6723 #undef dlur 6724 /* Ditch the DLPI header. */ 6725 mp1 = mp; 6726 mp = mp->b_cont; 6727 freeb(mp1); 6728 break; 6729 case M_BREAK: 6730 panic("ip_rput_v6: got an M_BREAK"); 6731 /*NOTREACHED*/ 6732 case M_IOCACK: 6733 iocp = (struct iocblk *)mp->b_rptr; 6734 switch (iocp->ioc_cmd) { 6735 case DL_IOC_HDR_INFO: 6736 ill = (ill_t *)q->q_ptr; 6737 ill_fastpath_ack(ill, mp); 6738 return; 6739 case SIOCSTUNPARAM: 6740 case SIOCGTUNPARAM: 6741 case OSIOCSTUNPARAM: 6742 case OSIOCGTUNPARAM: 6743 /* Go through qwriter */ 6744 break; 6745 default: 6746 putnext(q, mp); 6747 return; 6748 } 6749 /* FALLTHRU */ 6750 case M_ERROR: 6751 case M_HANGUP: 6752 mutex_enter(&ill->ill_lock); 6753 if (ill->ill_state_flags & ILL_CONDEMNED) { 6754 mutex_exit(&ill->ill_lock); 6755 freemsg(mp); 6756 return; 6757 } 6758 ill_refhold_locked(ill); 6759 mutex_exit(&ill->ill_lock); 6760 qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, B_FALSE); 6761 return; 6762 case M_CTL: { 6763 if ((MBLKL(mp) > sizeof (int)) && 6764 ((da_ipsec_t *)mp->b_rptr)->da_type == IPHADA_M_CTL) { 6765 ASSERT(MBLKL(mp) >= sizeof (da_ipsec_t)); 6766 mctl_present = B_TRUE; 6767 break; 6768 } 6769 putnext(q, mp); 6770 return; 6771 } 6772 case M_IOCNAK: 6773 iocp = (struct iocblk *)mp->b_rptr; 6774 switch (iocp->ioc_cmd) { 6775 case DL_IOC_HDR_INFO: 6776 case SIOCSTUNPARAM: 6777 case SIOCGTUNPARAM: 6778 case OSIOCSTUNPARAM: 6779 case OSIOCGTUNPARAM: 6780 mutex_enter(&ill->ill_lock); 6781 if (ill->ill_state_flags & ILL_CONDEMNED) { 6782 mutex_exit(&ill->ill_lock); 6783 freemsg(mp); 6784 return; 6785 } 6786 ill_refhold_locked(ill); 6787 mutex_exit(&ill->ill_lock); 6788 qwriter_ip(NULL, ill, q, mp, ip_rput_other, CUR_OP, 6789 B_FALSE); 6790 return; 6791 default: 6792 break; 6793 } 6794 /* FALLTHRU */ 6795 default: 6796 putnext(q, mp); 6797 return; 6798 } 6799 6800 BUMP_MIB(ill->ill_ip6_mib, ipv6InReceives); 6801 /* 6802 * if db_ref > 1 then copymsg and free original. Packet may be 6803 * changed and do not want other entity who has a reference to this 6804 * message to trip over the changes. This is a blind change because 6805 * trying to catch all places that might change packet is too 6806 * difficult (since it may be a module above this one). 6807 */ 6808 if (mp->b_datap->db_ref > 1) { 6809 mblk_t *mp1; 6810 6811 mp1 = copymsg(mp); 6812 freemsg(mp); 6813 if (mp1 == NULL) { 6814 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6815 return; 6816 } 6817 mp = mp1; 6818 } 6819 first_mp = mp; 6820 if (mctl_present) { 6821 hada_mp = first_mp; 6822 mp = first_mp->b_cont; 6823 } 6824 6825 ip6h = (ip6_t *)mp->b_rptr; 6826 6827 /* check for alignment and full IPv6 header */ 6828 if (!OK_32PTR((uchar_t *)ip6h) || 6829 (mp->b_wptr - (uchar_t *)ip6h) < IPV6_HDR_LEN) { 6830 if (!pullupmsg(mp, IPV6_HDR_LEN)) { 6831 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6832 ip1dbg(("ip_rput_v6: pullupmsg failed\n")); 6833 freemsg(first_mp); 6834 return; 6835 } 6836 ip6h = (ip6_t *)mp->b_rptr; 6837 } 6838 if ((ip6h->ip6_vcf & IPV6_VERS_AND_FLOW_MASK) == 6839 IPV6_DEFAULT_VERS_AND_FLOW) { 6840 /* 6841 * It may be a bit too expensive to do this mapped address 6842 * check here, but in the interest of robustness, it seems 6843 * like the correct place. 6844 * TODO: Avoid this check for e.g. connected TCP sockets 6845 */ 6846 if (IN6_IS_ADDR_V4MAPPED(&ip6h->ip6_src)) { 6847 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6848 ip1dbg(("ip_rput_v6: pkt with mapped src addr\n")); 6849 freemsg(first_mp); 6850 return; 6851 } 6852 6853 if (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src)) { 6854 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6855 ip1dbg(("ip_rput_v6: pkt with loopback src")); 6856 freemsg(first_mp); 6857 return; 6858 } else if (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst)) { 6859 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6860 ip1dbg(("ip_rput_v6: pkt with loopback dst")); 6861 freemsg(first_mp); 6862 return; 6863 } 6864 6865 flags |= (ll_multicast ? IP6_IN_LLMCAST : 0); 6866 ip_rput_data_v6(q, ill, mp, ip6h, flags, hada_mp); 6867 } else { 6868 BUMP_MIB(ill->ill_ip6_mib, ipv6InIPv4); 6869 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6870 freemsg(first_mp); 6871 } 6872 } 6873 6874 /* 6875 * Walk through the IPv6 packet in mp and see if there's an AH header 6876 * in it. See if the AH header needs to get done before other headers in 6877 * the packet. (Worker function for ipsec_early_ah_v6().) 6878 */ 6879 #define IPSEC_HDR_DONT_PROCESS 0 6880 #define IPSEC_HDR_PROCESS 1 6881 #define IPSEC_MEMORY_ERROR 2 6882 static int 6883 ipsec_needs_processing_v6(mblk_t *mp, uint8_t *nexthdr) 6884 { 6885 uint_t length; 6886 uint_t ehdrlen; 6887 uint8_t *whereptr; 6888 uint8_t *endptr; 6889 uint8_t *nexthdrp; 6890 ip6_dest_t *desthdr; 6891 ip6_rthdr_t *rthdr; 6892 ip6_t *ip6h; 6893 6894 /* 6895 * For now just pullup everything. In general, the less pullups, 6896 * the better, but there's so much squirrelling through anyway, 6897 * it's just easier this way. 6898 */ 6899 if (!pullupmsg(mp, -1)) { 6900 return (IPSEC_MEMORY_ERROR); 6901 } 6902 6903 ip6h = (ip6_t *)mp->b_rptr; 6904 length = IPV6_HDR_LEN; 6905 whereptr = ((uint8_t *)&ip6h[1]); /* point to next hdr */ 6906 endptr = mp->b_wptr; 6907 6908 /* 6909 * We can't just use the argument nexthdr in the place 6910 * of nexthdrp becaue we don't dereference nexthdrp 6911 * till we confirm whether it is a valid address. 6912 */ 6913 nexthdrp = &ip6h->ip6_nxt; 6914 while (whereptr < endptr) { 6915 /* Is there enough left for len + nexthdr? */ 6916 if (whereptr + MIN_EHDR_LEN > endptr) 6917 return (IPSEC_MEMORY_ERROR); 6918 6919 switch (*nexthdrp) { 6920 case IPPROTO_HOPOPTS: 6921 case IPPROTO_DSTOPTS: 6922 /* Assumes the headers are identical for hbh and dst */ 6923 desthdr = (ip6_dest_t *)whereptr; 6924 ehdrlen = 8 * (desthdr->ip6d_len + 1); 6925 if ((uchar_t *)desthdr + ehdrlen > endptr) 6926 return (IPSEC_MEMORY_ERROR); 6927 /* 6928 * Return DONT_PROCESS because of potential Mobile IPv6 6929 * cruft for destination options. 6930 */ 6931 if (*nexthdrp == IPPROTO_DSTOPTS) 6932 return (IPSEC_HDR_DONT_PROCESS); 6933 nexthdrp = &desthdr->ip6d_nxt; 6934 break; 6935 case IPPROTO_ROUTING: 6936 rthdr = (ip6_rthdr_t *)whereptr; 6937 6938 /* 6939 * If there's more hops left on the routing header, 6940 * return now with DON'T PROCESS. 6941 */ 6942 if (rthdr->ip6r_segleft > 0) 6943 return (IPSEC_HDR_DONT_PROCESS); 6944 6945 ehdrlen = 8 * (rthdr->ip6r_len + 1); 6946 if ((uchar_t *)rthdr + ehdrlen > endptr) 6947 return (IPSEC_MEMORY_ERROR); 6948 nexthdrp = &rthdr->ip6r_nxt; 6949 break; 6950 case IPPROTO_FRAGMENT: 6951 /* Wait for reassembly */ 6952 return (IPSEC_HDR_DONT_PROCESS); 6953 case IPPROTO_AH: 6954 *nexthdr = IPPROTO_AH; 6955 return (IPSEC_HDR_PROCESS); 6956 case IPPROTO_NONE: 6957 /* No next header means we're finished */ 6958 default: 6959 return (IPSEC_HDR_DONT_PROCESS); 6960 } 6961 length += ehdrlen; 6962 whereptr += ehdrlen; 6963 } 6964 panic("ipsec_needs_processing_v6"); 6965 /*NOTREACHED*/ 6966 } 6967 6968 /* 6969 * Path for AH if options are present. If this is the first time we are 6970 * sending a datagram to AH, allocate a IPSEC_IN message and prepend it. 6971 * Otherwise, just fanout. Return value answers the boolean question: 6972 * "Did I consume the mblk you sent me?" 6973 * 6974 * Sometimes AH needs to be done before other IPv6 headers for security 6975 * reasons. This function (and its ipsec_needs_processing_v6() above) 6976 * indicates if that is so, and fans out to the appropriate IPsec protocol 6977 * for the datagram passed in. 6978 */ 6979 static boolean_t 6980 ipsec_early_ah_v6(queue_t *q, mblk_t *first_mp, boolean_t mctl_present, 6981 ill_t *ill, ire_t *ire, mblk_t *hada_mp, zoneid_t zoneid) 6982 { 6983 mblk_t *mp; 6984 uint8_t nexthdr; 6985 ipsec_in_t *ii = NULL; 6986 ah_t *ah; 6987 ipsec_status_t ipsec_rc; 6988 6989 ASSERT((hada_mp == NULL) || (!mctl_present)); 6990 6991 switch (ipsec_needs_processing_v6( 6992 (mctl_present ? first_mp->b_cont : first_mp), &nexthdr)) { 6993 case IPSEC_MEMORY_ERROR: 6994 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 6995 freemsg(hada_mp); 6996 freemsg(first_mp); 6997 return (B_TRUE); 6998 case IPSEC_HDR_DONT_PROCESS: 6999 return (B_FALSE); 7000 } 7001 7002 /* Default means send it to AH! */ 7003 ASSERT(nexthdr == IPPROTO_AH); 7004 if (!mctl_present) { 7005 mp = first_mp; 7006 if ((first_mp = ipsec_in_alloc(B_FALSE)) == NULL) { 7007 ip1dbg(("ipsec_early_ah_v6: IPSEC_IN " 7008 "allocation failure.\n")); 7009 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 7010 freemsg(hada_mp); 7011 freemsg(mp); 7012 return (B_TRUE); 7013 } 7014 /* 7015 * Store the ill_index so that when we come back 7016 * from IPSEC we ride on the same queue. 7017 */ 7018 ii = (ipsec_in_t *)first_mp->b_rptr; 7019 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex; 7020 ii->ipsec_in_rill_index = ii->ipsec_in_ill_index; 7021 first_mp->b_cont = mp; 7022 } 7023 /* 7024 * Cache hardware acceleration info. 7025 */ 7026 if (hada_mp != NULL) { 7027 ASSERT(ii != NULL); 7028 IPSECHW_DEBUG(IPSECHW_PKT, ("ipsec_early_ah_v6: " 7029 "caching data attr.\n")); 7030 ii->ipsec_in_accelerated = B_TRUE; 7031 ii->ipsec_in_da = hada_mp; 7032 } 7033 7034 if (!ipsec_loaded()) { 7035 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, zoneid); 7036 return (B_TRUE); 7037 } 7038 7039 ah = ipsec_inbound_ah_sa(first_mp); 7040 if (ah == NULL) 7041 return (B_TRUE); 7042 ASSERT(ii->ipsec_in_ah_sa != NULL); 7043 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != NULL); 7044 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(first_mp, ah); 7045 7046 switch (ipsec_rc) { 7047 case IPSEC_STATUS_SUCCESS: 7048 /* we're done with IPsec processing, send it up */ 7049 ip_fanout_proto_again(first_mp, ill, ill, ire); 7050 break; 7051 case IPSEC_STATUS_FAILED: 7052 BUMP_MIB(&ip6_mib, ipv6InDiscards); 7053 break; 7054 case IPSEC_STATUS_PENDING: 7055 /* no action needed */ 7056 break; 7057 } 7058 return (B_TRUE); 7059 } 7060 7061 /* 7062 * ip_rput_data_v6 -- received IPv6 packets in M_DATA messages show up here. 7063 * ip_rput_v6 has already verified alignment, the min length, the version, 7064 * and db_ref = 1. 7065 * 7066 * The ill passed in (the arg named inill) is the ill that the packet 7067 * actually arrived on. We need to remember this when saving the 7068 * input interface index into potential IPV6_PKTINFO data in 7069 * ip_add_info_v6(). 7070 */ 7071 void 7072 ip_rput_data_v6(queue_t *q, ill_t *inill, mblk_t *mp, ip6_t *ip6h, 7073 uint_t flags, mblk_t *hada_mp) 7074 { 7075 ire_t *ire = NULL; 7076 queue_t *rq; 7077 ill_t *ill = inill; 7078 ipif_t *ipif; 7079 uint8_t *whereptr; 7080 uint8_t nexthdr; 7081 uint16_t remlen; 7082 uint_t prev_nexthdr_offset; 7083 uint_t used; 7084 size_t pkt_len; 7085 uint16_t ip6_len; 7086 uint_t hdr_len; 7087 boolean_t mctl_present; 7088 mblk_t *first_mp; 7089 mblk_t *first_mp1; 7090 boolean_t no_forward; 7091 ip6_hbh_t *hbhhdr; 7092 boolean_t ll_multicast = (flags & IP6_IN_LLMCAST); 7093 conn_t *connp; 7094 ilm_t *ilm; 7095 uint32_t ports; 7096 uint_t ipif_id = 0; 7097 zoneid_t zoneid = GLOBAL_ZONEID; 7098 uint16_t hck_flags, reass_hck_flags; 7099 uint32_t reass_sum; 7100 boolean_t cksum_err; 7101 mblk_t *mp1; 7102 7103 EXTRACT_PKT_MP(mp, first_mp, mctl_present); 7104 7105 if (hada_mp != NULL) { 7106 /* 7107 * It's an IPsec accelerated packet. 7108 * Keep a pointer to the data attributes around until 7109 * we allocate the ipsecinfo structure. 7110 */ 7111 IPSECHW_DEBUG(IPSECHW_PKT, 7112 ("ip_rput_data_v6: inbound HW accelerated IPsec pkt\n")); 7113 hada_mp->b_cont = NULL; 7114 /* 7115 * Since it is accelerated, it came directly from 7116 * the ill. 7117 */ 7118 ASSERT(mctl_present == B_FALSE); 7119 ASSERT(mp->b_datap->db_type != M_CTL); 7120 } 7121 7122 ASSERT(OK_32PTR((uchar_t *)ip6h) && 7123 (mp->b_wptr - (uchar_t *)ip6h) >= IPV6_HDR_LEN); 7124 7125 if (mp->b_cont == NULL) 7126 pkt_len = mp->b_wptr - mp->b_rptr; 7127 else 7128 pkt_len = msgdsize(mp); 7129 ip6_len = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN; 7130 7131 /* 7132 * Check for bogus (too short packet) and packet which 7133 * was padded by the link layer. 7134 */ 7135 if (ip6_len != pkt_len) { 7136 ssize_t diff; 7137 7138 if (ip6_len > pkt_len) { 7139 ip1dbg(("ip_rput_data_v6: packet too short %d %lu\n", 7140 ip6_len, pkt_len)); 7141 BUMP_MIB(ill->ill_ip6_mib, ipv6InTruncatedPkts); 7142 freemsg(hada_mp); 7143 freemsg(first_mp); 7144 return; 7145 } 7146 diff = (ssize_t)(pkt_len - ip6_len); 7147 7148 if (!adjmsg(mp, -diff)) { 7149 ip1dbg(("ip_rput_data_v6: adjmsg failed\n")); 7150 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 7151 freemsg(hada_mp); 7152 freemsg(first_mp); 7153 return; 7154 } 7155 pkt_len -= diff; 7156 } 7157 7158 if (ILL_HCKSUM_CAPABLE(ill) && !mctl_present && dohwcksum) 7159 hck_flags = DB_CKSUMFLAGS(mp); 7160 else 7161 hck_flags = 0; 7162 7163 /* Clear checksum flags in case we need to forward */ 7164 DB_CKSUMFLAGS(mp) = 0; 7165 reass_sum = reass_hck_flags = 0; 7166 7167 nexthdr = ip6h->ip6_nxt; 7168 7169 prev_nexthdr_offset = (uint_t)((uchar_t *)&ip6h->ip6_nxt - 7170 (uchar_t *)ip6h); 7171 whereptr = (uint8_t *)&ip6h[1]; 7172 remlen = pkt_len - IPV6_HDR_LEN; /* Track how much is left */ 7173 7174 /* Process hop by hop header options */ 7175 if (nexthdr == IPPROTO_HOPOPTS) { 7176 uint_t ehdrlen; 7177 uint8_t *optptr; 7178 7179 if (remlen < MIN_EHDR_LEN) 7180 goto pkt_too_short; 7181 if (mp->b_cont != NULL && 7182 whereptr + MIN_EHDR_LEN > mp->b_wptr) { 7183 if (!pullupmsg(mp, IPV6_HDR_LEN + MIN_EHDR_LEN)) { 7184 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 7185 freemsg(hada_mp); 7186 freemsg(first_mp); 7187 return; 7188 } 7189 ip6h = (ip6_t *)mp->b_rptr; 7190 whereptr = (uint8_t *)ip6h + pkt_len - remlen; 7191 } 7192 hbhhdr = (ip6_hbh_t *)whereptr; 7193 nexthdr = hbhhdr->ip6h_nxt; 7194 prev_nexthdr_offset = (uint_t)(whereptr - (uint8_t *)ip6h); 7195 ehdrlen = 8 * (hbhhdr->ip6h_len + 1); 7196 7197 if (remlen < ehdrlen) 7198 goto pkt_too_short; 7199 if (mp->b_cont != NULL && 7200 whereptr + ehdrlen > mp->b_wptr) { 7201 if (!pullupmsg(mp, IPV6_HDR_LEN + ehdrlen)) { 7202 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 7203 freemsg(hada_mp); 7204 freemsg(first_mp); 7205 return; 7206 } 7207 ip6h = (ip6_t *)mp->b_rptr; 7208 whereptr = (uint8_t *)ip6h + pkt_len - remlen; 7209 hbhhdr = (ip6_hbh_t *)whereptr; 7210 } 7211 7212 optptr = whereptr + 2; 7213 whereptr += ehdrlen; 7214 remlen -= ehdrlen; 7215 switch (ip_process_options_v6(q, first_mp, ip6h, optptr, 7216 ehdrlen - 2, IPPROTO_HOPOPTS)) { 7217 case -1: 7218 /* 7219 * Packet has been consumed and any 7220 * needed ICMP messages sent. 7221 */ 7222 BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors); 7223 freemsg(hada_mp); 7224 return; 7225 case 0: 7226 /* no action needed */ 7227 break; 7228 case 1: 7229 /* Known router alert */ 7230 goto ipv6forus; 7231 } 7232 } 7233 7234 /* 7235 * Attach any necessary label information to this packet. 7236 */ 7237 if (is_system_labeled() && !tsol_get_pkt_label(mp, IPV6_VERSION)) { 7238 if (ip6opt_ls != 0) 7239 ip0dbg(("tsol_get_pkt_label v6 failed\n")); 7240 BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors); 7241 freemsg(hada_mp); 7242 freemsg(first_mp); 7243 return; 7244 } 7245 7246 /* 7247 * On incoming v6 multicast packets we will bypass the ire table, 7248 * and assume that the read queue corresponds to the targetted 7249 * interface. 7250 * 7251 * The effect of this is the same as the IPv4 original code, but is 7252 * much cleaner I think. See ip_rput for how that was done. 7253 */ 7254 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 7255 BUMP_MIB(ill->ill_ip6_mib, ipv6InMcastPkts); 7256 /* 7257 * XXX TODO Give to mrouted to for multicast forwarding. 7258 */ 7259 ILM_WALKER_HOLD(ill); 7260 ilm = ilm_lookup_ill_v6(ill, &ip6h->ip6_dst, ALL_ZONES); 7261 ILM_WALKER_RELE(ill); 7262 if (ilm == NULL) { 7263 if (ip_debug > 3) { 7264 /* ip2dbg */ 7265 pr_addr_dbg("ip_rput_data_v6: got mcast packet" 7266 " which is not for us: %s\n", AF_INET6, 7267 &ip6h->ip6_dst); 7268 } 7269 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 7270 freemsg(hada_mp); 7271 freemsg(first_mp); 7272 return; 7273 } 7274 if (ip_debug > 3) { 7275 /* ip2dbg */ 7276 pr_addr_dbg("ip_rput_data_v6: multicast for us: %s\n", 7277 AF_INET6, &ip6h->ip6_dst); 7278 } 7279 rq = ill->ill_rq; 7280 zoneid = GLOBAL_ZONEID; 7281 goto ipv6forus; 7282 } 7283 7284 ipif = ill->ill_ipif; 7285 7286 /* 7287 * If a packet was received on an interface that is a 6to4 tunnel, 7288 * incoming IPv6 packets, with a 6to4 addressed IPv6 destination, must 7289 * be checked to have a 6to4 prefix (2002:V4ADDR::/48) that is equal to 7290 * the 6to4 prefix of the address configured on the receiving interface. 7291 * Otherwise, the packet was delivered to this interface in error and 7292 * the packet must be dropped. 7293 */ 7294 if ((ill->ill_is_6to4tun) && IN6_IS_ADDR_6TO4(&ip6h->ip6_dst)) { 7295 7296 if (!IN6_ARE_6TO4_PREFIX_EQUAL(&ipif->ipif_v6lcl_addr, 7297 &ip6h->ip6_dst)) { 7298 if (ip_debug > 2) { 7299 /* ip1dbg */ 7300 pr_addr_dbg("ip_rput_data_v6: received 6to4 " 7301 "addressed packet which is not for us: " 7302 "%s\n", AF_INET6, &ip6h->ip6_dst); 7303 } 7304 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 7305 freemsg(first_mp); 7306 return; 7307 } 7308 } 7309 7310 /* 7311 * Find an ire that matches destination. For link-local addresses 7312 * we have to match the ill. 7313 * TBD for site local addresses. 7314 */ 7315 if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst)) { 7316 ire = ire_ctable_lookup_v6(&ip6h->ip6_dst, NULL, 7317 IRE_CACHE|IRE_LOCAL, ill->ill_ipif, ALL_ZONES, NULL, 7318 MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP); 7319 } else { 7320 ire = ire_cache_lookup_v6(&ip6h->ip6_dst, ALL_ZONES, 7321 MBLK_GETLABEL(mp)); 7322 } 7323 if (ire == NULL) { 7324 /* 7325 * No matching IRE found. Mark this packet as having 7326 * originated externally. 7327 */ 7328 if (!(ill->ill_flags & ILLF_ROUTER) || ll_multicast) { 7329 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 7330 if (!(ill->ill_flags & ILLF_ROUTER)) 7331 BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors); 7332 freemsg(hada_mp); 7333 freemsg(first_mp); 7334 return; 7335 } 7336 if (ip6h->ip6_hops <= 1) { 7337 if (hada_mp != NULL) 7338 goto hada_drop; 7339 icmp_time_exceeded_v6(WR(q), first_mp, 7340 ICMP6_TIME_EXCEED_TRANSIT, ll_multicast, B_FALSE); 7341 return; 7342 } 7343 /* 7344 * Per RFC 3513 section 2.5.2, we must not forward packets with 7345 * an unspecified source address. 7346 */ 7347 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) { 7348 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 7349 freemsg(hada_mp); 7350 freemsg(first_mp); 7351 return; 7352 } 7353 mp->b_prev = (mblk_t *)(uintptr_t) 7354 ill->ill_phyint->phyint_ifindex; 7355 ip_newroute_v6(q, mp, &ip6h->ip6_dst, &ip6h->ip6_src, 7356 IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ? ill : NULL, 7357 ALL_ZONES); 7358 return; 7359 } 7360 ipif_id = ire->ire_ipif->ipif_seqid; 7361 /* we have a matching IRE */ 7362 if (ire->ire_stq != NULL) { 7363 ill_group_t *ill_group; 7364 ill_group_t *ire_group; 7365 7366 /* 7367 * To be quicker, we may wish not to chase pointers 7368 * (ire->ire_ipif->ipif_ill...) and instead store the 7369 * forwarding policy in the ire. An unfortunate side- 7370 * effect of this would be requiring an ire flush whenever 7371 * the ILLF_ROUTER flag changes. For now, chase pointers 7372 * once and store in the boolean no_forward. 7373 * 7374 * This appears twice to keep it out of the non-forwarding, 7375 * yes-it's-for-us-on-the-right-interface case. 7376 */ 7377 no_forward = ((ill->ill_flags & 7378 ire->ire_ipif->ipif_ill->ill_flags & ILLF_ROUTER) == 0); 7379 7380 7381 ASSERT(first_mp == mp); 7382 /* 7383 * This ire has a send-to queue - forward the packet. 7384 */ 7385 if (no_forward || ll_multicast || (hada_mp != NULL)) { 7386 freemsg(hada_mp); 7387 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 7388 if (no_forward) 7389 BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors); 7390 freemsg(mp); 7391 ire_refrele(ire); 7392 return; 7393 } 7394 if (ip6h->ip6_hops <= 1) { 7395 ip1dbg(("ip_rput_data_v6: hop limit expired.\n")); 7396 icmp_time_exceeded_v6(WR(q), mp, 7397 ICMP6_TIME_EXCEED_TRANSIT, ll_multicast, B_FALSE); 7398 ire_refrele(ire); 7399 return; 7400 } 7401 /* 7402 * Per RFC 3513 section 2.5.2, we must not forward packets with 7403 * an unspecified source address. 7404 */ 7405 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)) { 7406 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 7407 freemsg(mp); 7408 ire_refrele(ire); 7409 return; 7410 } 7411 7412 if (is_system_labeled()) { 7413 mblk_t *mp1; 7414 7415 if ((mp1 = tsol_ip_forward(ire, mp)) == NULL) { 7416 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 7417 freemsg(mp); 7418 ire_refrele(ire); 7419 return; 7420 } 7421 /* Size may have changed */ 7422 mp = mp1; 7423 ip6h = (ip6_t *)mp->b_rptr; 7424 pkt_len = msgdsize(mp); 7425 } 7426 7427 if (pkt_len > ire->ire_max_frag) { 7428 BUMP_MIB(ill->ill_ip6_mib, ipv6InTooBigErrors); 7429 icmp_pkt2big_v6(WR(q), mp, ire->ire_max_frag, 7430 ll_multicast, B_TRUE); 7431 ire_refrele(ire); 7432 return; 7433 } 7434 7435 /* 7436 * Check to see if we're forwarding the packet to a 7437 * different link from which it came. If so, check the 7438 * source and destination addresses since routers must not 7439 * forward any packets with link-local source or 7440 * destination addresses to other links. Otherwise (if 7441 * we're forwarding onto the same link), conditionally send 7442 * a redirect message. 7443 */ 7444 ill_group = ill->ill_group; 7445 ire_group = ((ill_t *)(ire->ire_rfq)->q_ptr)->ill_group; 7446 if (ire->ire_rfq != q && (ill_group == NULL || 7447 ill_group != ire_group)) { 7448 if (IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) || 7449 IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) { 7450 BUMP_MIB(ill->ill_ip6_mib, ipv6InAddrErrors); 7451 freemsg(mp); 7452 ire_refrele(ire); 7453 return; 7454 } 7455 /* TBD add site-local check at site boundary? */ 7456 } else if (ipv6_send_redirects) { 7457 in6_addr_t *v6targ; 7458 in6_addr_t gw_addr_v6; 7459 ire_t *src_ire_v6 = NULL; 7460 7461 /* 7462 * Don't send a redirect when forwarding a source 7463 * routed packet. 7464 */ 7465 if (ip_source_routed_v6(ip6h, mp)) 7466 goto forward; 7467 7468 mutex_enter(&ire->ire_lock); 7469 gw_addr_v6 = ire->ire_gateway_addr_v6; 7470 mutex_exit(&ire->ire_lock); 7471 if (!IN6_IS_ADDR_UNSPECIFIED(&gw_addr_v6)) { 7472 v6targ = &gw_addr_v6; 7473 /* 7474 * We won't send redirects to a router 7475 * that doesn't have a link local 7476 * address, but will forward. 7477 */ 7478 if (!IN6_IS_ADDR_LINKLOCAL(v6targ)) { 7479 BUMP_MIB(ill->ill_ip6_mib, 7480 ipv6InAddrErrors); 7481 goto forward; 7482 } 7483 } else { 7484 v6targ = &ip6h->ip6_dst; 7485 } 7486 7487 src_ire_v6 = ire_ftable_lookup_v6(&ip6h->ip6_src, 7488 NULL, NULL, IRE_INTERFACE, ire->ire_ipif, NULL, 7489 ALL_ZONES, 0, NULL, 7490 MATCH_IRE_IPIF | MATCH_IRE_TYPE); 7491 7492 if (src_ire_v6 != NULL) { 7493 /* 7494 * The source is directly connected. 7495 */ 7496 mp1 = copymsg(mp); 7497 if (mp1 != NULL) { 7498 icmp_send_redirect_v6(WR(q), 7499 mp1, v6targ, &ip6h->ip6_dst, 7500 ill, B_FALSE); 7501 } 7502 ire_refrele(src_ire_v6); 7503 } 7504 } 7505 7506 forward: 7507 /* Hoplimit verified above */ 7508 ip6h->ip6_hops--; 7509 UPDATE_IB_PKT_COUNT(ire); 7510 ire->ire_last_used_time = lbolt; 7511 BUMP_MIB(ill->ill_ip6_mib, ipv6OutForwDatagrams); 7512 ip_xmit_v6(mp, ire, 0, NULL, B_FALSE, NULL); 7513 IRE_REFRELE(ire); 7514 return; 7515 } 7516 rq = ire->ire_rfq; 7517 7518 /* 7519 * Need to put on correct queue for reassembly to find it. 7520 * No need to use put() since reassembly has its own locks. 7521 * Note: multicast packets and packets destined to addresses 7522 * assigned to loopback (ire_rfq is NULL) will be reassembled on 7523 * the arriving ill. 7524 */ 7525 if (rq != q) { 7526 boolean_t check_multi = B_TRUE; 7527 ill_group_t *ill_group = NULL; 7528 ill_group_t *ire_group = NULL; 7529 ill_t *ire_ill = NULL; 7530 uint_t ill_ifindex = ill->ill_usesrc_ifindex; 7531 7532 /* 7533 * To be quicker, we may wish not to chase pointers 7534 * (ire->ire_ipif->ipif_ill...) and instead store the 7535 * forwarding policy in the ire. An unfortunate side- 7536 * effect of this would be requiring an ire flush whenever 7537 * the ILLF_ROUTER flag changes. For now, chase pointers 7538 * once and store in the boolean no_forward. 7539 */ 7540 no_forward = ((ill->ill_flags & 7541 ire->ire_ipif->ipif_ill->ill_flags & ILLF_ROUTER) == 0); 7542 7543 ill_group = ill->ill_group; 7544 if (rq != NULL) { 7545 ire_ill = (ill_t *)(rq->q_ptr); 7546 ire_group = ire_ill->ill_group; 7547 } 7548 7549 /* 7550 * If it's part of the same IPMP group, or if it's a legal 7551 * address on the 'usesrc' interface, then bypass strict 7552 * checks. 7553 */ 7554 if (ill_group != NULL && ill_group == ire_group) { 7555 check_multi = B_FALSE; 7556 } else if (ill_ifindex != 0 && ire_ill != NULL && 7557 ill_ifindex == ire_ill->ill_phyint->phyint_ifindex) { 7558 check_multi = B_FALSE; 7559 } 7560 7561 ASSERT(!IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)); 7562 if (check_multi && ipv6_strict_dst_multihoming && no_forward) { 7563 /* 7564 * This packet came in on an interface other than the 7565 * one associated with the destination address 7566 * and we are strict about matches. 7567 * 7568 * As long as the ills belong to the same group, 7569 * we don't consider them to arriving on the wrong 7570 * interface. Thus, when the switch is doing inbound 7571 * load spreading, we won't drop packets when we 7572 * are doing strict multihoming checks. 7573 */ 7574 BUMP_MIB(ill->ill_ip6_mib, ipv6ForwProhibits); 7575 freemsg(hada_mp); 7576 freemsg(first_mp); 7577 ire_refrele(ire); 7578 return; 7579 } 7580 7581 if (rq != NULL) 7582 q = rq; 7583 7584 ill = (ill_t *)q->q_ptr; 7585 ASSERT(ill); 7586 } 7587 7588 zoneid = ire->ire_zoneid; 7589 UPDATE_IB_PKT_COUNT(ire); 7590 ire->ire_last_used_time = lbolt; 7591 /* Don't use the ire after this point. */ 7592 ire_refrele(ire); 7593 ipv6forus: 7594 /* 7595 * Looks like this packet is for us one way or another. 7596 * This is where we'll process destination headers etc. 7597 */ 7598 for (; ; ) { 7599 switch (nexthdr) { 7600 case IPPROTO_TCP: { 7601 uint16_t *up; 7602 uint32_t sum; 7603 int offset; 7604 7605 hdr_len = pkt_len - remlen; 7606 7607 if (hada_mp != NULL) { 7608 ip0dbg(("tcp hada drop\n")); 7609 goto hada_drop; 7610 } 7611 7612 7613 /* TCP needs all of the TCP header */ 7614 if (remlen < TCP_MIN_HEADER_LENGTH) 7615 goto pkt_too_short; 7616 if (mp->b_cont != NULL && 7617 whereptr + TCP_MIN_HEADER_LENGTH > mp->b_wptr) { 7618 if (!pullupmsg(mp, 7619 hdr_len + TCP_MIN_HEADER_LENGTH)) { 7620 BUMP_MIB(ill->ill_ip6_mib, 7621 ipv6InDiscards); 7622 freemsg(first_mp); 7623 return; 7624 } 7625 hck_flags = 0; 7626 ip6h = (ip6_t *)mp->b_rptr; 7627 whereptr = (uint8_t *)ip6h + hdr_len; 7628 } 7629 /* 7630 * Extract the offset field from the TCP header. 7631 */ 7632 offset = ((uchar_t *)ip6h)[hdr_len + 12] >> 4; 7633 if (offset != 5) { 7634 if (offset < 5) { 7635 ip1dbg(("ip_rput_data_v6: short " 7636 "TCP data offset")); 7637 BUMP_MIB(ill->ill_ip6_mib, 7638 ipv6InDiscards); 7639 freemsg(first_mp); 7640 return; 7641 } 7642 /* 7643 * There must be TCP options. 7644 * Make sure we can grab them. 7645 */ 7646 offset <<= 2; 7647 if (remlen < offset) 7648 goto pkt_too_short; 7649 if (mp->b_cont != NULL && 7650 whereptr + offset > mp->b_wptr) { 7651 if (!pullupmsg(mp, 7652 hdr_len + offset)) { 7653 BUMP_MIB(ill->ill_ip6_mib, 7654 ipv6InDiscards); 7655 freemsg(first_mp); 7656 return; 7657 } 7658 hck_flags = 0; 7659 ip6h = (ip6_t *)mp->b_rptr; 7660 whereptr = (uint8_t *)ip6h + hdr_len; 7661 } 7662 } 7663 7664 up = (uint16_t *)&ip6h->ip6_src; 7665 /* 7666 * TCP checksum calculation. First sum up the 7667 * pseudo-header fields: 7668 * - Source IPv6 address 7669 * - Destination IPv6 address 7670 * - TCP payload length 7671 * - TCP protocol ID 7672 */ 7673 sum = htons(IPPROTO_TCP + remlen) + 7674 up[0] + up[1] + up[2] + up[3] + 7675 up[4] + up[5] + up[6] + up[7] + 7676 up[8] + up[9] + up[10] + up[11] + 7677 up[12] + up[13] + up[14] + up[15]; 7678 7679 /* Fold initial sum */ 7680 sum = (sum & 0xffff) + (sum >> 16); 7681 7682 mp1 = mp->b_cont; 7683 7684 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 7685 IP6_STAT(ip6_in_sw_cksum); 7686 7687 IP_CKSUM_RECV(hck_flags, sum, (uchar_t *) 7688 ((uchar_t *)mp->b_rptr + DB_CKSUMSTART(mp)), 7689 (int32_t)(whereptr - (uchar_t *)mp->b_rptr), 7690 mp, mp1, cksum_err); 7691 7692 if (cksum_err) { 7693 BUMP_MIB(&ip_mib, tcpInErrs); 7694 7695 if (hck_flags & HCK_FULLCKSUM) 7696 IP6_STAT(ip6_tcp_in_full_hw_cksum_err); 7697 else if (hck_flags & HCK_PARTIALCKSUM) 7698 IP6_STAT(ip6_tcp_in_part_hw_cksum_err); 7699 else 7700 IP6_STAT(ip6_tcp_in_sw_cksum_err); 7701 7702 freemsg(first_mp); 7703 return; 7704 } 7705 tcp_fanout: 7706 ip_fanout_tcp_v6(q, first_mp, ip6h, ill, inill, 7707 (flags|IP_FF_SEND_ICMP|IP_FF_SYN_ADDIRE| 7708 IP_FF_IP6INFO), hdr_len, mctl_present, zoneid); 7709 return; 7710 } 7711 case IPPROTO_SCTP: 7712 { 7713 sctp_hdr_t *sctph; 7714 uint32_t calcsum, pktsum; 7715 uint_t hdr_len = pkt_len - remlen; 7716 7717 /* SCTP needs all of the SCTP header */ 7718 if (remlen < sizeof (*sctph)) { 7719 goto pkt_too_short; 7720 } 7721 if (whereptr + sizeof (*sctph) > mp->b_wptr) { 7722 ASSERT(mp->b_cont != NULL); 7723 if (!pullupmsg(mp, hdr_len + sizeof (*sctph))) { 7724 BUMP_MIB(ill->ill_ip6_mib, 7725 ipv6InDiscards); 7726 freemsg(mp); 7727 return; 7728 } 7729 ip6h = (ip6_t *)mp->b_rptr; 7730 whereptr = (uint8_t *)ip6h + hdr_len; 7731 } 7732 7733 sctph = (sctp_hdr_t *)(mp->b_rptr + hdr_len); 7734 /* checksum */ 7735 pktsum = sctph->sh_chksum; 7736 sctph->sh_chksum = 0; 7737 calcsum = sctp_cksum(mp, hdr_len); 7738 if (calcsum != pktsum) { 7739 BUMP_MIB(&sctp_mib, sctpChecksumError); 7740 freemsg(mp); 7741 return; 7742 } 7743 sctph->sh_chksum = pktsum; 7744 ports = *(uint32_t *)(mp->b_rptr + hdr_len); 7745 if ((connp = sctp_fanout(&ip6h->ip6_src, &ip6h->ip6_dst, 7746 ports, ipif_id, zoneid, mp)) == NULL) { 7747 ip_fanout_sctp_raw(first_mp, ill, 7748 (ipha_t *)ip6h, B_FALSE, ports, 7749 mctl_present, 7750 (flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO), 7751 B_TRUE, ipif_id, zoneid); 7752 return; 7753 } 7754 BUMP_MIB(&ip_mib, ipInDelivers); 7755 sctp_input(connp, (ipha_t *)ip6h, mp, first_mp, ill, 7756 B_FALSE, mctl_present); 7757 return; 7758 } 7759 case IPPROTO_UDP: { 7760 uint16_t *up; 7761 uint32_t sum; 7762 7763 hdr_len = pkt_len - remlen; 7764 7765 if (hada_mp != NULL) { 7766 ip0dbg(("udp hada drop\n")); 7767 goto hada_drop; 7768 } 7769 7770 /* Verify that at least the ports are present */ 7771 if (remlen < UDPH_SIZE) 7772 goto pkt_too_short; 7773 if (mp->b_cont != NULL && 7774 whereptr + UDPH_SIZE > mp->b_wptr) { 7775 if (!pullupmsg(mp, hdr_len + UDPH_SIZE)) { 7776 BUMP_MIB(ill->ill_ip6_mib, 7777 ipv6InDiscards); 7778 freemsg(first_mp); 7779 return; 7780 } 7781 hck_flags = 0; 7782 ip6h = (ip6_t *)mp->b_rptr; 7783 whereptr = (uint8_t *)ip6h + hdr_len; 7784 } 7785 7786 /* 7787 * Before going through the regular checksum 7788 * calculation, make sure the received checksum 7789 * is non-zero. RFC 2460 says, a 0x0000 checksum 7790 * in a UDP packet (within IPv6 packet) is invalid 7791 * and should be replaced by 0xffff. This makes 7792 * sense as regular checksum calculation will 7793 * pass for both the cases i.e. 0x0000 and 0xffff. 7794 * Removing one of the case makes error detection 7795 * stronger. 7796 */ 7797 7798 if (((udpha_t *)whereptr)->uha_checksum == 0) { 7799 /* 0x0000 checksum is invalid */ 7800 ip1dbg(("ip_rput_data_v6: Invalid UDP " 7801 "checksum value 0x0000\n")); 7802 BUMP_MIB(ill->ill_ip6_mib, udpInCksumErrs); 7803 freemsg(first_mp); 7804 return; 7805 } 7806 7807 up = (uint16_t *)&ip6h->ip6_src; 7808 7809 /* 7810 * UDP checksum calculation. First sum up the 7811 * pseudo-header fields: 7812 * - Source IPv6 address 7813 * - Destination IPv6 address 7814 * - UDP payload length 7815 * - UDP protocol ID 7816 */ 7817 7818 sum = htons(IPPROTO_UDP + remlen) + 7819 up[0] + up[1] + up[2] + up[3] + 7820 up[4] + up[5] + up[6] + up[7] + 7821 up[8] + up[9] + up[10] + up[11] + 7822 up[12] + up[13] + up[14] + up[15]; 7823 7824 /* Fold initial sum */ 7825 sum = (sum & 0xffff) + (sum >> 16); 7826 7827 if (reass_hck_flags != 0) { 7828 hck_flags = reass_hck_flags; 7829 7830 IP_CKSUM_RECV_REASS(hck_flags, 7831 (int32_t)(whereptr - (uchar_t *)mp->b_rptr), 7832 sum, reass_sum, cksum_err); 7833 } else { 7834 mp1 = mp->b_cont; 7835 7836 IP_CKSUM_RECV(hck_flags, sum, (uchar_t *) 7837 ((uchar_t *)mp->b_rptr + DB_CKSUMSTART(mp)), 7838 (int32_t)(whereptr - (uchar_t *)mp->b_rptr), 7839 mp, mp1, cksum_err); 7840 } 7841 7842 if ((hck_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)) == 0) 7843 IP6_STAT(ip6_in_sw_cksum); 7844 7845 if (cksum_err) { 7846 BUMP_MIB(ill->ill_ip6_mib, udpInCksumErrs); 7847 7848 if (hck_flags & HCK_FULLCKSUM) 7849 IP6_STAT(ip6_udp_in_full_hw_cksum_err); 7850 else if (hck_flags & HCK_PARTIALCKSUM) 7851 IP6_STAT(ip6_udp_in_part_hw_cksum_err); 7852 else 7853 IP6_STAT(ip6_udp_in_sw_cksum_err); 7854 7855 freemsg(first_mp); 7856 return; 7857 } 7858 goto udp_fanout; 7859 } 7860 case IPPROTO_ICMPV6: { 7861 uint16_t *up; 7862 uint32_t sum; 7863 uint_t hdr_len = pkt_len - remlen; 7864 7865 if (hada_mp != NULL) { 7866 ip0dbg(("icmp hada drop\n")); 7867 goto hada_drop; 7868 } 7869 7870 up = (uint16_t *)&ip6h->ip6_src; 7871 sum = htons(IPPROTO_ICMPV6 + remlen) + 7872 up[0] + up[1] + up[2] + up[3] + 7873 up[4] + up[5] + up[6] + up[7] + 7874 up[8] + up[9] + up[10] + up[11] + 7875 up[12] + up[13] + up[14] + up[15]; 7876 sum = (sum & 0xffff) + (sum >> 16); 7877 sum = IP_CSUM(mp, hdr_len, sum); 7878 if (sum != 0) { 7879 /* IPv6 ICMP checksum failed */ 7880 ip1dbg(("ip_rput_data_v6: ICMPv6 checksum " 7881 "failed %x\n", 7882 sum)); 7883 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInMsgs); 7884 BUMP_MIB(ill->ill_icmp6_mib, 7885 ipv6IfIcmpInErrors); 7886 freemsg(first_mp); 7887 return; 7888 } 7889 7890 icmp_fanout: 7891 /* Check variable for testing applications */ 7892 if (ipv6_drop_inbound_icmpv6) { 7893 freemsg(first_mp); 7894 return; 7895 } 7896 /* 7897 * Assume that there is always at least one conn for 7898 * ICMPv6 (in.ndpd) i.e. don't optimize the case 7899 * where there is no conn. 7900 */ 7901 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 7902 ASSERT(!(ill->ill_phyint->phyint_flags & 7903 PHYI_LOOPBACK)); 7904 /* 7905 * In the multicast case, applications may have 7906 * joined the group from different zones, so we 7907 * need to deliver the packet to each of them. 7908 * Loop through the multicast memberships 7909 * structures (ilm) on the receive ill and send 7910 * a copy of the packet up each matching one. 7911 */ 7912 ILM_WALKER_HOLD(ill); 7913 for (ilm = ill->ill_ilm; ilm != NULL; 7914 ilm = ilm->ilm_next) { 7915 if (ilm->ilm_flags & ILM_DELETED) 7916 continue; 7917 if (!IN6_ARE_ADDR_EQUAL( 7918 &ilm->ilm_v6addr, &ip6h->ip6_dst)) 7919 continue; 7920 if (!ipif_lookup_zoneid(ill, 7921 ilm->ilm_zoneid, IPIF_UP, NULL)) 7922 continue; 7923 7924 first_mp1 = ip_copymsg(first_mp); 7925 if (first_mp1 == NULL) 7926 continue; 7927 icmp_inbound_v6(q, first_mp1, ill, 7928 hdr_len, mctl_present, 0, 7929 ilm->ilm_zoneid); 7930 } 7931 ILM_WALKER_RELE(ill); 7932 } else { 7933 first_mp1 = ip_copymsg(first_mp); 7934 if (first_mp1 != NULL) 7935 icmp_inbound_v6(q, first_mp1, ill, 7936 hdr_len, mctl_present, 0, zoneid); 7937 } 7938 } 7939 /* FALLTHRU */ 7940 default: { 7941 /* 7942 * Handle protocols with which IPv6 is less intimate. 7943 */ 7944 uint_t proto_flags = IP_FF_RAWIP|IP_FF_IP6INFO; 7945 7946 if (hada_mp != NULL) { 7947 ip0dbg(("default hada drop\n")); 7948 goto hada_drop; 7949 } 7950 7951 /* 7952 * Enable sending ICMP for "Unknown" nexthdr 7953 * case. i.e. where we did not FALLTHRU from 7954 * IPPROTO_ICMPV6 processing case above. 7955 * If we did FALLTHRU, then the packet has already been 7956 * processed for IPPF, don't process it again in 7957 * ip_fanout_proto_v6; set IP6_NO_IPPOLICY in the 7958 * flags 7959 */ 7960 if (nexthdr != IPPROTO_ICMPV6) 7961 proto_flags |= IP_FF_SEND_ICMP; 7962 else 7963 proto_flags |= IP6_NO_IPPOLICY; 7964 7965 ip_fanout_proto_v6(q, first_mp, ip6h, ill, inill, 7966 nexthdr, prev_nexthdr_offset, (flags|proto_flags), 7967 mctl_present, zoneid); 7968 return; 7969 } 7970 7971 case IPPROTO_DSTOPTS: { 7972 uint_t ehdrlen; 7973 uint8_t *optptr; 7974 ip6_dest_t *desthdr; 7975 7976 /* Check if AH is present. */ 7977 if (ipsec_early_ah_v6(q, first_mp, mctl_present, ill, 7978 ire, hada_mp, zoneid)) { 7979 ip0dbg(("dst early hada drop\n")); 7980 return; 7981 } 7982 7983 /* 7984 * Reinitialize pointers, as ipsec_early_ah_v6() does 7985 * complete pullups. We don't have to do more pullups 7986 * as a result. 7987 */ 7988 whereptr = (uint8_t *)((uintptr_t)mp->b_rptr + 7989 (uintptr_t)(whereptr - ((uint8_t *)ip6h))); 7990 ip6h = (ip6_t *)mp->b_rptr; 7991 7992 if (remlen < MIN_EHDR_LEN) 7993 goto pkt_too_short; 7994 7995 desthdr = (ip6_dest_t *)whereptr; 7996 nexthdr = desthdr->ip6d_nxt; 7997 prev_nexthdr_offset = (uint_t)(whereptr - 7998 (uint8_t *)ip6h); 7999 ehdrlen = 8 * (desthdr->ip6d_len + 1); 8000 if (remlen < ehdrlen) 8001 goto pkt_too_short; 8002 optptr = whereptr + 2; 8003 /* 8004 * Note: XXX This code does not seem to make 8005 * distinction between Destination Options Header 8006 * being before/after Routing Header which can 8007 * happen if we are at the end of source route. 8008 * This may become significant in future. 8009 * (No real significant Destination Options are 8010 * defined/implemented yet ). 8011 */ 8012 switch (ip_process_options_v6(q, first_mp, ip6h, optptr, 8013 ehdrlen - 2, IPPROTO_DSTOPTS)) { 8014 case -1: 8015 /* 8016 * Packet has been consumed and any needed 8017 * ICMP errors sent. 8018 */ 8019 BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors); 8020 freemsg(hada_mp); 8021 return; 8022 case 0: 8023 /* No action needed continue */ 8024 break; 8025 case 1: 8026 /* 8027 * Unnexpected return value 8028 * (Router alert is a Hop-by-Hop option) 8029 */ 8030 #ifdef DEBUG 8031 panic("ip_rput_data_v6: router " 8032 "alert hbh opt indication in dest opt"); 8033 /*NOTREACHED*/ 8034 #else 8035 freemsg(hada_mp); 8036 freemsg(first_mp); 8037 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 8038 return; 8039 #endif 8040 } 8041 used = ehdrlen; 8042 break; 8043 } 8044 case IPPROTO_FRAGMENT: { 8045 ip6_frag_t *fraghdr; 8046 size_t no_frag_hdr_len; 8047 8048 if (hada_mp != NULL) { 8049 ip0dbg(("frag hada drop\n")); 8050 goto hada_drop; 8051 } 8052 8053 ASSERT(first_mp == mp); 8054 if (remlen < sizeof (ip6_frag_t)) 8055 goto pkt_too_short; 8056 8057 if (mp->b_cont != NULL && 8058 whereptr + sizeof (ip6_frag_t) > mp->b_wptr) { 8059 if (!pullupmsg(mp, 8060 pkt_len - remlen + sizeof (ip6_frag_t))) { 8061 BUMP_MIB(ill->ill_ip6_mib, 8062 ipv6InDiscards); 8063 freemsg(mp); 8064 return; 8065 } 8066 hck_flags = 0; 8067 ip6h = (ip6_t *)mp->b_rptr; 8068 whereptr = (uint8_t *)ip6h + pkt_len - remlen; 8069 } 8070 8071 fraghdr = (ip6_frag_t *)whereptr; 8072 used = (uint_t)sizeof (ip6_frag_t); 8073 BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmReqds); 8074 8075 /* 8076 * Invoke the CGTP (multirouting) filtering module to 8077 * process the incoming packet. Packets identified as 8078 * duplicates must be discarded. Filtering is active 8079 * only if the the ip_cgtp_filter ndd variable is 8080 * non-zero. 8081 */ 8082 if (ip_cgtp_filter && (ip_cgtp_filter_ops != NULL)) { 8083 int cgtp_flt_pkt = 8084 ip_cgtp_filter_ops->cfo_filter_v6( 8085 inill->ill_rq, ip6h, fraghdr); 8086 if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) { 8087 freemsg(mp); 8088 return; 8089 } 8090 } 8091 8092 /* Restore the flags */ 8093 DB_CKSUMFLAGS(mp) = hck_flags; 8094 8095 mp = ip_rput_frag_v6(q, mp, ip6h, fraghdr, 8096 remlen - used, &prev_nexthdr_offset, 8097 &reass_sum, &reass_hck_flags); 8098 if (mp == NULL) { 8099 /* Reassembly is still pending */ 8100 return; 8101 } 8102 /* The first mblk are the headers before the frag hdr */ 8103 BUMP_MIB(ill->ill_ip6_mib, ipv6ReasmOKs); 8104 8105 first_mp = mp; /* mp has most likely changed! */ 8106 no_frag_hdr_len = mp->b_wptr - mp->b_rptr; 8107 ip6h = (ip6_t *)mp->b_rptr; 8108 nexthdr = ((char *)ip6h)[prev_nexthdr_offset]; 8109 whereptr = mp->b_rptr + no_frag_hdr_len; 8110 remlen = ntohs(ip6h->ip6_plen) + 8111 (uint16_t)(IPV6_HDR_LEN - no_frag_hdr_len); 8112 pkt_len = msgdsize(mp); 8113 used = 0; 8114 break; 8115 } 8116 case IPPROTO_HOPOPTS: 8117 if (hada_mp != NULL) { 8118 ip0dbg(("hop hada drop\n")); 8119 goto hada_drop; 8120 } 8121 /* 8122 * Illegal header sequence. 8123 * (Hop-by-hop headers are processed above 8124 * and required to immediately follow IPv6 header) 8125 */ 8126 icmp_param_problem_v6(WR(q), first_mp, 8127 ICMP6_PARAMPROB_NEXTHEADER, 8128 prev_nexthdr_offset, 8129 B_FALSE, B_FALSE); 8130 return; 8131 8132 case IPPROTO_ROUTING: { 8133 uint_t ehdrlen; 8134 ip6_rthdr_t *rthdr; 8135 8136 /* Check if AH is present. */ 8137 if (ipsec_early_ah_v6(q, first_mp, mctl_present, ill, 8138 ire, hada_mp, zoneid)) { 8139 ip0dbg(("routing hada drop\n")); 8140 return; 8141 } 8142 8143 /* 8144 * Reinitialize pointers, as ipsec_early_ah_v6() does 8145 * complete pullups. We don't have to do more pullups 8146 * as a result. 8147 */ 8148 whereptr = (uint8_t *)((uintptr_t)mp->b_rptr + 8149 (uintptr_t)(whereptr - ((uint8_t *)ip6h))); 8150 ip6h = (ip6_t *)mp->b_rptr; 8151 8152 if (remlen < MIN_EHDR_LEN) 8153 goto pkt_too_short; 8154 rthdr = (ip6_rthdr_t *)whereptr; 8155 nexthdr = rthdr->ip6r_nxt; 8156 prev_nexthdr_offset = (uint_t)(whereptr - 8157 (uint8_t *)ip6h); 8158 ehdrlen = 8 * (rthdr->ip6r_len + 1); 8159 if (remlen < ehdrlen) 8160 goto pkt_too_short; 8161 if (rthdr->ip6r_segleft != 0) { 8162 /* Not end of source route */ 8163 if (ll_multicast) { 8164 BUMP_MIB(ill->ill_ip6_mib, 8165 ipv6ForwProhibits); 8166 freemsg(hada_mp); 8167 freemsg(mp); 8168 return; 8169 } 8170 ip_process_rthdr(q, mp, ip6h, rthdr, ill, 8171 flags, hada_mp); 8172 return; 8173 } 8174 used = ehdrlen; 8175 break; 8176 } 8177 case IPPROTO_AH: 8178 case IPPROTO_ESP: { 8179 /* 8180 * Fast path for AH/ESP. If this is the first time 8181 * we are sending a datagram to AH/ESP, allocate 8182 * a IPSEC_IN message and prepend it. Otherwise, 8183 * just fanout. 8184 */ 8185 8186 ipsec_in_t *ii; 8187 int ipsec_rc; 8188 8189 if (!mctl_present) { 8190 ASSERT(first_mp == mp); 8191 if ((first_mp = ipsec_in_alloc(B_FALSE)) == 8192 NULL) { 8193 ip1dbg(("ip_rput_data_v6: IPSEC_IN " 8194 "allocation failure.\n")); 8195 BUMP_MIB(ill->ill_ip6_mib, 8196 ipv6InDiscards); 8197 freemsg(mp); 8198 return; 8199 } 8200 /* 8201 * Store the ill_index so that when we come back 8202 * from IPSEC we ride on the same queue. 8203 */ 8204 ii = (ipsec_in_t *)first_mp->b_rptr; 8205 ii->ipsec_in_ill_index = 8206 ill->ill_phyint->phyint_ifindex; 8207 ii->ipsec_in_rill_index = 8208 ii->ipsec_in_ill_index; 8209 first_mp->b_cont = mp; 8210 /* 8211 * Cache hardware acceleration info. 8212 */ 8213 if (hada_mp != NULL) { 8214 IPSECHW_DEBUG(IPSECHW_PKT, 8215 ("ip_rput_data_v6: " 8216 "caching data attr.\n")); 8217 ii->ipsec_in_accelerated = B_TRUE; 8218 ii->ipsec_in_da = hada_mp; 8219 hada_mp = NULL; 8220 } 8221 } else { 8222 ii = (ipsec_in_t *)first_mp->b_rptr; 8223 } 8224 8225 if (!ipsec_loaded()) { 8226 ip_proto_not_sup(q, first_mp, IP_FF_SEND_ICMP, 8227 ire->ire_zoneid); 8228 return; 8229 } 8230 8231 /* select inbound SA and have IPsec process the pkt */ 8232 if (nexthdr == IPPROTO_ESP) { 8233 esph_t *esph = ipsec_inbound_esp_sa(first_mp); 8234 if (esph == NULL) 8235 return; 8236 ASSERT(ii->ipsec_in_esp_sa != NULL); 8237 ASSERT(ii->ipsec_in_esp_sa->ipsa_input_func != 8238 NULL); 8239 ipsec_rc = ii->ipsec_in_esp_sa->ipsa_input_func( 8240 first_mp, esph); 8241 } else { 8242 ah_t *ah = ipsec_inbound_ah_sa(first_mp); 8243 if (ah == NULL) 8244 return; 8245 ASSERT(ii->ipsec_in_ah_sa != NULL); 8246 ASSERT(ii->ipsec_in_ah_sa->ipsa_input_func != 8247 NULL); 8248 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func( 8249 first_mp, ah); 8250 } 8251 8252 switch (ipsec_rc) { 8253 case IPSEC_STATUS_SUCCESS: 8254 break; 8255 case IPSEC_STATUS_FAILED: 8256 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 8257 /* FALLTHRU */ 8258 case IPSEC_STATUS_PENDING: 8259 return; 8260 } 8261 /* we're done with IPsec processing, send it up */ 8262 ip_fanout_proto_again(first_mp, ill, inill, ire); 8263 return; 8264 } 8265 case IPPROTO_NONE: 8266 /* All processing is done. Count as "delivered". */ 8267 freemsg(hada_mp); 8268 freemsg(first_mp); 8269 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 8270 return; 8271 } 8272 whereptr += used; 8273 ASSERT(remlen >= used); 8274 remlen -= used; 8275 } 8276 /* NOTREACHED */ 8277 8278 pkt_too_short: 8279 ip1dbg(("ip_rput_data_v6: packet too short %d %lu %d\n", 8280 ip6_len, pkt_len, remlen)); 8281 BUMP_MIB(ill->ill_ip6_mib, ipv6InTruncatedPkts); 8282 freemsg(hada_mp); 8283 freemsg(first_mp); 8284 return; 8285 udp_fanout: 8286 if (mctl_present || IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 8287 connp = NULL; 8288 } else { 8289 connp = ipcl_classify_v6(mp, IPPROTO_UDP, hdr_len, zoneid); 8290 if ((connp != NULL) && (connp->conn_upq == NULL)) { 8291 CONN_DEC_REF(connp); 8292 connp = NULL; 8293 } 8294 } 8295 8296 if (connp == NULL) { 8297 uint32_t ports; 8298 8299 ports = *(uint32_t *)(mp->b_rptr + hdr_len + 8300 UDP_PORTS_OFFSET); 8301 IP6_STAT(ip6_udp_slow_path); 8302 ip_fanout_udp_v6(q, first_mp, ip6h, ports, ill, inill, 8303 (flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO), mctl_present, 8304 zoneid); 8305 return; 8306 } 8307 8308 if (CONN_UDP_FLOWCTLD(connp)) { 8309 freemsg(first_mp); 8310 BUMP_MIB(ill->ill_ip6_mib, udpInOverflows); 8311 CONN_DEC_REF(connp); 8312 return; 8313 } 8314 8315 /* Initiate IPPF processing */ 8316 if (IP6_IN_IPP(flags)) { 8317 ip_process(IPP_LOCAL_IN, &mp, ill->ill_phyint->phyint_ifindex); 8318 if (mp == NULL) { 8319 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 8320 CONN_DEC_REF(connp); 8321 return; 8322 } 8323 } 8324 8325 if (connp->conn_ipv6_recvpktinfo || 8326 IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src)) { 8327 mp = ip_add_info_v6(mp, inill, &ip6h->ip6_dst); 8328 if (mp == NULL) { 8329 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 8330 CONN_DEC_REF(connp); 8331 return; 8332 } 8333 } 8334 8335 IP6_STAT(ip6_udp_fast_path); 8336 BUMP_MIB(ill->ill_ip6_mib, ipv6InReceives); 8337 BUMP_MIB(ill->ill_ip6_mib, ipv6InDelivers); 8338 8339 /* Send it upstream */ 8340 CONN_UDP_RECV(connp, mp); 8341 8342 CONN_DEC_REF(connp); 8343 freemsg(hada_mp); 8344 return; 8345 8346 hada_drop: 8347 ip1dbg(("ip_rput_data_v6: malformed accelerated packet\n")); 8348 /* IPsec kstats: bump counter here */ 8349 freemsg(hada_mp); 8350 freemsg(first_mp); 8351 } 8352 8353 /* 8354 * Reassemble fragment. 8355 * When it returns a completed message the first mblk will only contain 8356 * the headers prior to the fragment header. 8357 * 8358 * prev_nexthdr_offset is an offset indication of where the nexthdr field is 8359 * of the preceding header. This is needed to patch the previous header's 8360 * nexthdr field when reassembly completes. 8361 */ 8362 static mblk_t * 8363 ip_rput_frag_v6(queue_t *q, mblk_t *mp, ip6_t *ip6h, 8364 ip6_frag_t *fraghdr, uint_t remlen, uint_t *prev_nexthdr_offset, 8365 uint32_t *cksum_val, uint16_t *cksum_flags) 8366 { 8367 ill_t *ill = (ill_t *)q->q_ptr; 8368 uint32_t ident = ntohl(fraghdr->ip6f_ident); 8369 uint16_t offset; 8370 boolean_t more_frags; 8371 uint8_t nexthdr = fraghdr->ip6f_nxt; 8372 in6_addr_t *v6dst_ptr; 8373 in6_addr_t *v6src_ptr; 8374 uint_t end; 8375 uint_t hdr_length; 8376 size_t count; 8377 ipf_t *ipf; 8378 ipf_t **ipfp; 8379 ipfb_t *ipfb; 8380 mblk_t *mp1; 8381 uint8_t ecn_info = 0; 8382 size_t msg_len; 8383 mblk_t *tail_mp; 8384 mblk_t *t_mp; 8385 boolean_t pruned = B_FALSE; 8386 uint32_t sum_val; 8387 uint16_t sum_flags; 8388 8389 8390 if (cksum_val != NULL) 8391 *cksum_val = 0; 8392 if (cksum_flags != NULL) 8393 *cksum_flags = 0; 8394 8395 /* 8396 * We utilize hardware computed checksum info only for UDP since 8397 * IP fragmentation is a normal occurence for the protocol. In 8398 * addition, checksum offload support for IP fragments carrying 8399 * UDP payload is commonly implemented across network adapters. 8400 */ 8401 ASSERT(ill != NULL); 8402 if (nexthdr == IPPROTO_UDP && dohwcksum && ILL_HCKSUM_CAPABLE(ill) && 8403 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) { 8404 mblk_t *mp1 = mp->b_cont; 8405 int32_t len; 8406 8407 /* Record checksum information from the packet */ 8408 sum_val = (uint32_t)DB_CKSUM16(mp); 8409 sum_flags = DB_CKSUMFLAGS(mp); 8410 8411 /* fragmented payload offset from beginning of mblk */ 8412 offset = (uint16_t)((uchar_t *)&fraghdr[1] - mp->b_rptr); 8413 8414 if ((sum_flags & HCK_PARTIALCKSUM) && 8415 (mp1 == NULL || mp1->b_cont == NULL) && 8416 offset >= (uint16_t)DB_CKSUMSTART(mp) && 8417 ((len = offset - (uint16_t)DB_CKSUMSTART(mp)) & 1) == 0) { 8418 uint32_t adj; 8419 /* 8420 * Partial checksum has been calculated by hardware 8421 * and attached to the packet; in addition, any 8422 * prepended extraneous data is even byte aligned. 8423 * If any such data exists, we adjust the checksum; 8424 * this would also handle any postpended data. 8425 */ 8426 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp), 8427 mp, mp1, len, adj); 8428 8429 /* One's complement subtract extraneous checksum */ 8430 if (adj >= sum_val) 8431 sum_val = ~(adj - sum_val) & 0xFFFF; 8432 else 8433 sum_val -= adj; 8434 } 8435 } else { 8436 sum_val = 0; 8437 sum_flags = 0; 8438 } 8439 8440 /* Clear hardware checksumming flag */ 8441 DB_CKSUMFLAGS(mp) = 0; 8442 8443 /* 8444 * Note: Fragment offset in header is in 8-octet units. 8445 * Clearing least significant 3 bits not only extracts 8446 * it but also gets it in units of octets. 8447 */ 8448 offset = ntohs(fraghdr->ip6f_offlg) & ~7; 8449 more_frags = (fraghdr->ip6f_offlg & IP6F_MORE_FRAG); 8450 8451 /* 8452 * Is the more frags flag on and the payload length not a multiple 8453 * of eight? 8454 */ 8455 if (more_frags && (ntohs(ip6h->ip6_plen) & 7)) { 8456 BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors); 8457 icmp_param_problem_v6(WR(q), mp, ICMP6_PARAMPROB_HEADER, 8458 (uint32_t)((char *)&ip6h->ip6_plen - 8459 (char *)ip6h), B_FALSE, B_FALSE); 8460 return (NULL); 8461 } 8462 8463 v6src_ptr = &ip6h->ip6_src; 8464 v6dst_ptr = &ip6h->ip6_dst; 8465 end = remlen; 8466 8467 hdr_length = (uint_t)((char *)&fraghdr[1] - (char *)ip6h); 8468 end += offset; 8469 8470 /* 8471 * Would fragment cause reassembled packet to have a payload length 8472 * greater than IP_MAXPACKET - the max payload size? 8473 */ 8474 if (end > IP_MAXPACKET) { 8475 BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors); 8476 icmp_param_problem_v6(WR(q), mp, ICMP6_PARAMPROB_HEADER, 8477 (uint32_t)((char *)&fraghdr->ip6f_offlg - 8478 (char *)ip6h), B_FALSE, B_FALSE); 8479 return (NULL); 8480 } 8481 8482 /* 8483 * This packet just has one fragment. Reassembly not 8484 * needed. 8485 */ 8486 if (!more_frags && offset == 0) { 8487 goto reass_done; 8488 } 8489 8490 /* 8491 * Drop the fragmented as early as possible, if 8492 * we don't have resource(s) to re-assemble. 8493 */ 8494 if (ip_reass_queue_bytes == 0) { 8495 freemsg(mp); 8496 return (NULL); 8497 } 8498 8499 /* Record the ECN field info. */ 8500 ecn_info = (uint8_t)(ntohl(ip6h->ip6_vcf & htonl(~0xFFCFFFFF)) >> 20); 8501 /* 8502 * If this is not the first fragment, dump the unfragmentable 8503 * portion of the packet. 8504 */ 8505 if (offset) 8506 mp->b_rptr = (uchar_t *)&fraghdr[1]; 8507 8508 /* 8509 * Fragmentation reassembly. Each ILL has a hash table for 8510 * queueing packets undergoing reassembly for all IPIFs 8511 * associated with the ILL. The hash is based on the packet 8512 * IP ident field. The ILL frag hash table was allocated 8513 * as a timer block at the time the ILL was created. Whenever 8514 * there is anything on the reassembly queue, the timer will 8515 * be running. 8516 */ 8517 msg_len = MBLKSIZE(mp); 8518 tail_mp = mp; 8519 while (tail_mp->b_cont != NULL) { 8520 tail_mp = tail_mp->b_cont; 8521 msg_len += MBLKSIZE(tail_mp); 8522 } 8523 /* 8524 * If the reassembly list for this ILL will get too big 8525 * prune it. 8526 */ 8527 8528 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >= 8529 ip_reass_queue_bytes) { 8530 ill_frag_prune(ill, (ip_reass_queue_bytes < msg_len) ? 0 8531 : (ip_reass_queue_bytes - msg_len)); 8532 pruned = B_TRUE; 8533 } 8534 8535 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH_V6(*v6src_ptr, ident)]; 8536 mutex_enter(&ipfb->ipfb_lock); 8537 8538 ipfp = &ipfb->ipfb_ipf; 8539 /* Try to find an existing fragment queue for this packet. */ 8540 for (;;) { 8541 ipf = ipfp[0]; 8542 if (ipf) { 8543 /* 8544 * It has to match on ident, source address, and 8545 * dest address. 8546 */ 8547 if (ipf->ipf_ident == ident && 8548 IN6_ARE_ADDR_EQUAL(&ipf->ipf_v6src, v6src_ptr) && 8549 IN6_ARE_ADDR_EQUAL(&ipf->ipf_v6dst, v6dst_ptr)) { 8550 8551 /* 8552 * If we have received too many 8553 * duplicate fragments for this packet 8554 * free it. 8555 */ 8556 if (ipf->ipf_num_dups > ip_max_frag_dups) { 8557 ill_frag_free_pkts(ill, ipfb, ipf, 1); 8558 freemsg(mp); 8559 mutex_exit(&ipfb->ipfb_lock); 8560 return (NULL); 8561 } 8562 8563 break; 8564 } 8565 ipfp = &ipf->ipf_hash_next; 8566 continue; 8567 } 8568 8569 8570 /* 8571 * If we pruned the list, do we want to store this new 8572 * fragment?. We apply an optimization here based on the 8573 * fact that most fragments will be received in order. 8574 * So if the offset of this incoming fragment is zero, 8575 * it is the first fragment of a new packet. We will 8576 * keep it. Otherwise drop the fragment, as we have 8577 * probably pruned the packet already (since the 8578 * packet cannot be found). 8579 */ 8580 8581 if (pruned && offset != 0) { 8582 mutex_exit(&ipfb->ipfb_lock); 8583 freemsg(mp); 8584 return (NULL); 8585 } 8586 8587 /* New guy. Allocate a frag message. */ 8588 mp1 = allocb(sizeof (*ipf), BPRI_MED); 8589 if (!mp1) { 8590 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 8591 freemsg(mp); 8592 partial_reass_done: 8593 mutex_exit(&ipfb->ipfb_lock); 8594 return (NULL); 8595 } 8596 8597 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS) { 8598 /* 8599 * Too many fragmented packets in this hash bucket. 8600 * Free the oldest. 8601 */ 8602 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1); 8603 } 8604 8605 mp1->b_cont = mp; 8606 8607 /* Initialize the fragment header. */ 8608 ipf = (ipf_t *)mp1->b_rptr; 8609 ipf->ipf_mp = mp1; 8610 ipf->ipf_ptphn = ipfp; 8611 ipfp[0] = ipf; 8612 ipf->ipf_hash_next = NULL; 8613 ipf->ipf_ident = ident; 8614 ipf->ipf_v6src = *v6src_ptr; 8615 ipf->ipf_v6dst = *v6dst_ptr; 8616 /* Record reassembly start time. */ 8617 ipf->ipf_timestamp = gethrestime_sec(); 8618 /* Record ipf generation and account for frag header */ 8619 ipf->ipf_gen = ill->ill_ipf_gen++; 8620 ipf->ipf_count = MBLKSIZE(mp1); 8621 ipf->ipf_protocol = nexthdr; 8622 ipf->ipf_nf_hdr_len = 0; 8623 ipf->ipf_prev_nexthdr_offset = 0; 8624 ipf->ipf_last_frag_seen = B_FALSE; 8625 ipf->ipf_ecn = ecn_info; 8626 ipf->ipf_num_dups = 0; 8627 ipfb->ipfb_frag_pkts++; 8628 ipf->ipf_checksum = 0; 8629 ipf->ipf_checksum_flags = 0; 8630 8631 /* Store checksum value in fragment header */ 8632 if (sum_flags != 0) { 8633 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 8634 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 8635 ipf->ipf_checksum = sum_val; 8636 ipf->ipf_checksum_flags = sum_flags; 8637 } 8638 8639 /* 8640 * We handle reassembly two ways. In the easy case, 8641 * where all the fragments show up in order, we do 8642 * minimal bookkeeping, and just clip new pieces on 8643 * the end. If we ever see a hole, then we go off 8644 * to ip_reassemble which has to mark the pieces and 8645 * keep track of the number of holes, etc. Obviously, 8646 * the point of having both mechanisms is so we can 8647 * handle the easy case as efficiently as possible. 8648 */ 8649 if (offset == 0) { 8650 /* Easy case, in-order reassembly so far. */ 8651 /* Update the byte count */ 8652 ipf->ipf_count += msg_len; 8653 ipf->ipf_tail_mp = tail_mp; 8654 /* 8655 * Keep track of next expected offset in 8656 * ipf_end. 8657 */ 8658 ipf->ipf_end = end; 8659 ipf->ipf_nf_hdr_len = hdr_length; 8660 ipf->ipf_prev_nexthdr_offset = *prev_nexthdr_offset; 8661 } else { 8662 /* Hard case, hole at the beginning. */ 8663 ipf->ipf_tail_mp = NULL; 8664 /* 8665 * ipf_end == 0 means that we have given up 8666 * on easy reassembly. 8667 */ 8668 ipf->ipf_end = 0; 8669 8670 /* Forget checksum offload from now on */ 8671 ipf->ipf_checksum_flags = 0; 8672 8673 /* 8674 * ipf_hole_cnt is set by ip_reassemble. 8675 * ipf_count is updated by ip_reassemble. 8676 * No need to check for return value here 8677 * as we don't expect reassembly to complete or 8678 * fail for the first fragment itself. 8679 */ 8680 (void) ip_reassemble(mp, ipf, offset, more_frags, ill, 8681 msg_len); 8682 } 8683 /* Update per ipfb and ill byte counts */ 8684 ipfb->ipfb_count += ipf->ipf_count; 8685 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 8686 ill->ill_frag_count += ipf->ipf_count; 8687 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 8688 /* If the frag timer wasn't already going, start it. */ 8689 mutex_enter(&ill->ill_lock); 8690 ill_frag_timer_start(ill); 8691 mutex_exit(&ill->ill_lock); 8692 goto partial_reass_done; 8693 } 8694 8695 /* 8696 * If the packet's flag has changed (it could be coming up 8697 * from an interface different than the previous, therefore 8698 * possibly different checksum capability), then forget about 8699 * any stored checksum states. Otherwise add the value to 8700 * the existing one stored in the fragment header. 8701 */ 8702 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) { 8703 sum_val += ipf->ipf_checksum; 8704 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 8705 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16); 8706 ipf->ipf_checksum = sum_val; 8707 } else if (ipf->ipf_checksum_flags != 0) { 8708 /* Forget checksum offload from now on */ 8709 ipf->ipf_checksum_flags = 0; 8710 } 8711 8712 /* 8713 * We have a new piece of a datagram which is already being 8714 * reassembled. Update the ECN info if all IP fragments 8715 * are ECN capable. If there is one which is not, clear 8716 * all the info. If there is at least one which has CE 8717 * code point, IP needs to report that up to transport. 8718 */ 8719 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) { 8720 if (ecn_info == IPH_ECN_CE) 8721 ipf->ipf_ecn = IPH_ECN_CE; 8722 } else { 8723 ipf->ipf_ecn = IPH_ECN_NECT; 8724 } 8725 8726 if (offset && ipf->ipf_end == offset) { 8727 /* The new fragment fits at the end */ 8728 ipf->ipf_tail_mp->b_cont = mp; 8729 /* Update the byte count */ 8730 ipf->ipf_count += msg_len; 8731 /* Update per ipfb and ill byte counts */ 8732 ipfb->ipfb_count += msg_len; 8733 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 8734 ill->ill_frag_count += msg_len; 8735 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 8736 if (more_frags) { 8737 /* More to come. */ 8738 ipf->ipf_end = end; 8739 ipf->ipf_tail_mp = tail_mp; 8740 goto partial_reass_done; 8741 } 8742 } else { 8743 /* 8744 * Go do the hard cases. 8745 * Call ip_reassemble(). 8746 */ 8747 int ret; 8748 8749 if (offset == 0) { 8750 if (ipf->ipf_prev_nexthdr_offset == 0) { 8751 ipf->ipf_nf_hdr_len = hdr_length; 8752 ipf->ipf_prev_nexthdr_offset = 8753 *prev_nexthdr_offset; 8754 } 8755 } 8756 /* Save current byte count */ 8757 count = ipf->ipf_count; 8758 ret = ip_reassemble(mp, ipf, offset, more_frags, ill, msg_len); 8759 8760 /* Count of bytes added and subtracted (freeb()ed) */ 8761 count = ipf->ipf_count - count; 8762 if (count) { 8763 /* Update per ipfb and ill byte counts */ 8764 ipfb->ipfb_count += count; 8765 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */ 8766 ill->ill_frag_count += count; 8767 ASSERT(ill->ill_frag_count > 0); /* Wraparound */ 8768 } 8769 if (ret == IP_REASS_PARTIAL) { 8770 goto partial_reass_done; 8771 } else if (ret == IP_REASS_FAILED) { 8772 /* Reassembly failed. Free up all resources */ 8773 ill_frag_free_pkts(ill, ipfb, ipf, 1); 8774 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) { 8775 IP_REASS_SET_START(t_mp, 0); 8776 IP_REASS_SET_END(t_mp, 0); 8777 } 8778 freemsg(mp); 8779 goto partial_reass_done; 8780 } 8781 8782 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */ 8783 } 8784 /* 8785 * We have completed reassembly. Unhook the frag header from 8786 * the reassembly list. 8787 * 8788 * Grab the unfragmentable header length next header value out 8789 * of the first fragment 8790 */ 8791 ASSERT(ipf->ipf_nf_hdr_len != 0); 8792 hdr_length = ipf->ipf_nf_hdr_len; 8793 8794 /* 8795 * Before we free the frag header, record the ECN info 8796 * to report back to the transport. 8797 */ 8798 ecn_info = ipf->ipf_ecn; 8799 8800 /* 8801 * Store the nextheader field in the header preceding the fragment 8802 * header 8803 */ 8804 nexthdr = ipf->ipf_protocol; 8805 *prev_nexthdr_offset = ipf->ipf_prev_nexthdr_offset; 8806 ipfp = ipf->ipf_ptphn; 8807 8808 /* We need to supply these to caller */ 8809 if ((sum_flags = ipf->ipf_checksum_flags) != 0) 8810 sum_val = ipf->ipf_checksum; 8811 else 8812 sum_val = 0; 8813 8814 mp1 = ipf->ipf_mp; 8815 count = ipf->ipf_count; 8816 ipf = ipf->ipf_hash_next; 8817 if (ipf) 8818 ipf->ipf_ptphn = ipfp; 8819 ipfp[0] = ipf; 8820 ill->ill_frag_count -= count; 8821 ASSERT(ipfb->ipfb_count >= count); 8822 ipfb->ipfb_count -= count; 8823 ipfb->ipfb_frag_pkts--; 8824 mutex_exit(&ipfb->ipfb_lock); 8825 /* Ditch the frag header. */ 8826 mp = mp1->b_cont; 8827 freeb(mp1); 8828 8829 /* 8830 * Make sure the packet is good by doing some sanity 8831 * check. If bad we can silentely drop the packet. 8832 */ 8833 reass_done: 8834 if (hdr_length < sizeof (ip6_frag_t)) { 8835 BUMP_MIB(ill->ill_ip6_mib, ipv6InHdrErrors); 8836 ip1dbg(("ip_rput_frag_v6: bad packet\n")); 8837 freemsg(mp); 8838 return (NULL); 8839 } 8840 8841 /* 8842 * Remove the fragment header from the initial header by 8843 * splitting the mblk into the non-fragmentable header and 8844 * everthing after the fragment extension header. This has the 8845 * side effect of putting all the headers that need destination 8846 * processing into the b_cont block-- on return this fact is 8847 * used in order to avoid having to look at the extensions 8848 * already processed. 8849 * 8850 * Note that this code assumes that the unfragmentable portion 8851 * of the header is in the first mblk and increments 8852 * the read pointer past it. If this assumption is broken 8853 * this code fails badly. 8854 */ 8855 if (mp->b_rptr + hdr_length != mp->b_wptr) { 8856 mblk_t *nmp; 8857 8858 if (!(nmp = dupb(mp))) { 8859 BUMP_MIB(ill->ill_ip6_mib, ipv6InDiscards); 8860 ip1dbg(("ip_rput_frag_v6: dupb failed\n")); 8861 freemsg(mp); 8862 return (NULL); 8863 } 8864 nmp->b_cont = mp->b_cont; 8865 mp->b_cont = nmp; 8866 nmp->b_rptr += hdr_length; 8867 } 8868 mp->b_wptr = mp->b_rptr + hdr_length - sizeof (ip6_frag_t); 8869 8870 ip6h = (ip6_t *)mp->b_rptr; 8871 ((char *)ip6h)[*prev_nexthdr_offset] = nexthdr; 8872 8873 /* Restore original IP length in header. */ 8874 ip6h->ip6_plen = htons((uint16_t)(msgdsize(mp) - IPV6_HDR_LEN)); 8875 /* Record the ECN info. */ 8876 ip6h->ip6_vcf &= htonl(0xFFCFFFFF); 8877 ip6h->ip6_vcf |= htonl(ecn_info << 20); 8878 8879 /* Reassembly is successful; return checksum information if needed */ 8880 if (cksum_val != NULL) 8881 *cksum_val = sum_val; 8882 if (cksum_flags != NULL) 8883 *cksum_flags = sum_flags; 8884 8885 return (mp); 8886 } 8887 8888 /* 8889 * Walk through the options to see if there is a routing header. 8890 * If present get the destination which is the last address of 8891 * the option. 8892 */ 8893 in6_addr_t 8894 ip_get_dst_v6(ip6_t *ip6h, boolean_t *is_fragment) 8895 { 8896 uint8_t nexthdr; 8897 uint8_t *whereptr; 8898 ip6_hbh_t *hbhhdr; 8899 ip6_dest_t *dsthdr; 8900 ip6_rthdr0_t *rthdr; 8901 ip6_frag_t *fraghdr; 8902 int ehdrlen; 8903 int left; 8904 in6_addr_t *ap, rv; 8905 8906 if (is_fragment != NULL) 8907 *is_fragment = B_FALSE; 8908 8909 rv = ip6h->ip6_dst; 8910 8911 nexthdr = ip6h->ip6_nxt; 8912 whereptr = (uint8_t *)&ip6h[1]; 8913 for (;;) { 8914 8915 ASSERT(nexthdr != IPPROTO_RAW); 8916 switch (nexthdr) { 8917 case IPPROTO_HOPOPTS: 8918 hbhhdr = (ip6_hbh_t *)whereptr; 8919 nexthdr = hbhhdr->ip6h_nxt; 8920 ehdrlen = 8 * (hbhhdr->ip6h_len + 1); 8921 break; 8922 case IPPROTO_DSTOPTS: 8923 dsthdr = (ip6_dest_t *)whereptr; 8924 nexthdr = dsthdr->ip6d_nxt; 8925 ehdrlen = 8 * (dsthdr->ip6d_len + 1); 8926 break; 8927 case IPPROTO_ROUTING: 8928 rthdr = (ip6_rthdr0_t *)whereptr; 8929 nexthdr = rthdr->ip6r0_nxt; 8930 ehdrlen = 8 * (rthdr->ip6r0_len + 1); 8931 8932 left = rthdr->ip6r0_segleft; 8933 ap = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr)); 8934 rv = *(ap + left - 1); 8935 /* 8936 * If the caller doesn't care whether the packet 8937 * is a fragment or not, we can stop here since 8938 * we have our destination. 8939 */ 8940 if (is_fragment == NULL) 8941 goto done; 8942 break; 8943 case IPPROTO_FRAGMENT: 8944 fraghdr = (ip6_frag_t *)whereptr; 8945 nexthdr = fraghdr->ip6f_nxt; 8946 ehdrlen = sizeof (ip6_frag_t); 8947 if (is_fragment != NULL) 8948 *is_fragment = B_TRUE; 8949 goto done; 8950 default : 8951 goto done; 8952 } 8953 whereptr += ehdrlen; 8954 } 8955 8956 done: 8957 return (rv); 8958 } 8959 8960 /* 8961 * ip_source_routed_v6: 8962 * This function is called by redirect code in ip_rput_data_v6 to 8963 * know whether this packet is source routed through this node i.e 8964 * whether this node (router) is part of the journey. This 8965 * function is called under two cases : 8966 * 8967 * case 1 : Routing header was processed by this node and 8968 * ip_process_rthdr replaced ip6_dst with the next hop 8969 * and we are forwarding the packet to the next hop. 8970 * 8971 * case 2 : Routing header was not processed by this node and we 8972 * are just forwarding the packet. 8973 * 8974 * For case (1) we don't want to send redirects. For case(2) we 8975 * want to send redirects. 8976 */ 8977 static boolean_t 8978 ip_source_routed_v6(ip6_t *ip6h, mblk_t *mp) 8979 { 8980 uint8_t nexthdr; 8981 in6_addr_t *addrptr; 8982 ip6_rthdr0_t *rthdr; 8983 uint8_t numaddr; 8984 ip6_hbh_t *hbhhdr; 8985 uint_t ehdrlen; 8986 uint8_t *byteptr; 8987 8988 ip2dbg(("ip_source_routed_v6\n")); 8989 nexthdr = ip6h->ip6_nxt; 8990 ehdrlen = IPV6_HDR_LEN; 8991 8992 /* if a routing hdr is preceeded by HOPOPT or DSTOPT */ 8993 while (nexthdr == IPPROTO_HOPOPTS || 8994 nexthdr == IPPROTO_DSTOPTS) { 8995 byteptr = (uint8_t *)ip6h + ehdrlen; 8996 /* 8997 * Check if we have already processed 8998 * packets or we are just a forwarding 8999 * router which only pulled up msgs up 9000 * to IPV6HDR and one HBH ext header 9001 */ 9002 if (byteptr + MIN_EHDR_LEN > mp->b_wptr) { 9003 ip2dbg(("ip_source_routed_v6: Extension" 9004 " headers not processed\n")); 9005 return (B_FALSE); 9006 } 9007 hbhhdr = (ip6_hbh_t *)byteptr; 9008 nexthdr = hbhhdr->ip6h_nxt; 9009 ehdrlen = ehdrlen + 8 * (hbhhdr->ip6h_len + 1); 9010 } 9011 switch (nexthdr) { 9012 case IPPROTO_ROUTING: 9013 byteptr = (uint8_t *)ip6h + ehdrlen; 9014 /* 9015 * If for some reason, we haven't pulled up 9016 * the routing hdr data mblk, then we must 9017 * not have processed it at all. So for sure 9018 * we are not part of the source routed journey. 9019 */ 9020 if (byteptr + MIN_EHDR_LEN > mp->b_wptr) { 9021 ip2dbg(("ip_source_routed_v6: Routing" 9022 " header not processed\n")); 9023 return (B_FALSE); 9024 } 9025 rthdr = (ip6_rthdr0_t *)byteptr; 9026 /* 9027 * Either we are an intermediate router or the 9028 * last hop before destination and we have 9029 * already processed the routing header. 9030 * If segment_left is greater than or equal to zero, 9031 * then we must be the (numaddr - segleft) entry 9032 * of the routing header. Although ip6r0_segleft 9033 * is a unit8_t variable, we still check for zero 9034 * or greater value, if in case the data type 9035 * is changed someday in future. 9036 */ 9037 if (rthdr->ip6r0_segleft > 0 || 9038 rthdr->ip6r0_segleft == 0) { 9039 ire_t *ire = NULL; 9040 9041 numaddr = rthdr->ip6r0_len / 2; 9042 addrptr = (in6_addr_t *)((char *)rthdr + 9043 sizeof (*rthdr)); 9044 addrptr += (numaddr - (rthdr->ip6r0_segleft + 1)); 9045 if (addrptr != NULL) { 9046 ire = ire_ctable_lookup_v6(addrptr, NULL, 9047 IRE_LOCAL, NULL, ALL_ZONES, NULL, 9048 MATCH_IRE_TYPE); 9049 if (ire != NULL) { 9050 ire_refrele(ire); 9051 return (B_TRUE); 9052 } 9053 ip1dbg(("ip_source_routed_v6: No ire found\n")); 9054 } 9055 } 9056 /* FALLTHRU */ 9057 default: 9058 ip2dbg(("ip_source_routed_v6: Not source routed here\n")); 9059 return (B_FALSE); 9060 } 9061 } 9062 9063 /* 9064 * ip_wput_v6 -- Packets sent down from transport modules show up here. 9065 * Assumes that the following set of headers appear in the first 9066 * mblk: 9067 * ip6i_t (if present) CAN also appear as a separate mblk. 9068 * ip6_t 9069 * Any extension headers 9070 * TCP/UDP/SCTP header (if present) 9071 * The routine can handle an ICMPv6 header that is not in the first mblk. 9072 * 9073 * The order to determine the outgoing interface is as follows: 9074 * 1. IPV6_BOUND_PIF is set, use that ill (conn_outgoing_pill) 9075 * 2. If conn_nofailover_ill is set then use that ill. 9076 * 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. 9077 * 4. If q is an ill queue and (link local or multicast destination) then 9078 * use that ill. 9079 * 5. If IPV6_BOUND_IF has been set use that ill. 9080 * 6. For multicast: if IPV6_MULTICAST_IF has been set use it. Otherwise 9081 * look for the best IRE match for the unspecified group to determine 9082 * the ill. 9083 * 7. For unicast: Just do an IRE lookup for the best match. 9084 */ 9085 void 9086 ip_output_v6(void *arg, mblk_t *mp, void *arg2, int caller) 9087 { 9088 conn_t *connp = NULL; 9089 queue_t *q = (queue_t *)arg2; 9090 ire_t *ire = NULL; 9091 ire_t *sctp_ire = NULL; 9092 ip6_t *ip6h; 9093 in6_addr_t *v6dstp; 9094 ill_t *ill = NULL; 9095 ipif_t *ipif; 9096 ip6i_t *ip6i; 9097 int cksum_request; /* -1 => normal. */ 9098 /* 1 => Skip TCP/UDP/SCTP checksum */ 9099 /* Otherwise contains insert offset for checksum */ 9100 int unspec_src; 9101 boolean_t do_outrequests; /* Increment OutRequests? */ 9102 mib2_ipv6IfStatsEntry_t *mibptr; 9103 int match_flags = MATCH_IRE_ILL_GROUP; 9104 boolean_t attach_if = B_FALSE; 9105 mblk_t *first_mp; 9106 boolean_t mctl_present; 9107 ipsec_out_t *io; 9108 boolean_t drop_if_delayed = B_FALSE; 9109 boolean_t multirt_need_resolve = B_FALSE; 9110 mblk_t *copy_mp = NULL; 9111 int err; 9112 int ip6i_flags = 0; 9113 zoneid_t zoneid; 9114 ill_t *saved_ill = NULL; 9115 boolean_t conn_lock_held; 9116 boolean_t need_decref = B_FALSE; 9117 9118 /* 9119 * Highest bit in version field is Reachability Confirmation bit 9120 * used by NUD in ip_xmit_v6(). 9121 */ 9122 #ifdef _BIG_ENDIAN 9123 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 9124 #else 9125 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 9126 #endif 9127 9128 /* 9129 * M_CTL comes from 5 places 9130 * 9131 * 1) TCP sends down IPSEC_OUT(M_CTL) for detached connections 9132 * both V4 and V6 datagrams. 9133 * 9134 * 2) AH/ESP sends down M_CTL after doing their job with both 9135 * V4 and V6 datagrams. 9136 * 9137 * 3) NDP callbacks when nce is resolved and IPSEC_OUT has been 9138 * attached. 9139 * 9140 * 4) Notifications from an external resolver (for XRESOLV ifs) 9141 * 9142 * 5) AH/ESP send down IPSEC_CTL(M_CTL) to be relayed to hardware for 9143 * IPsec hardware acceleration support. 9144 * 9145 * We need to handle (1)'s IPv6 case and (3) here. For the 9146 * IPv4 case in (1), and (2), IPSEC processing has already 9147 * started. The code in ip_wput() already knows how to handle 9148 * continuing IPSEC processing (for IPv4 and IPv6). All other 9149 * M_CTLs (including case (4)) are passed on to ip_wput_nondata() 9150 * for handling. 9151 */ 9152 first_mp = mp; 9153 mctl_present = B_FALSE; 9154 io = NULL; 9155 9156 /* Multidata transmit? */ 9157 if (DB_TYPE(mp) == M_MULTIDATA) { 9158 /* 9159 * We should never get here, since all Multidata messages 9160 * originating from tcp should have been directed over to 9161 * tcp_multisend() in the first place. 9162 */ 9163 BUMP_MIB(&ip6_mib, ipv6OutDiscards); 9164 freemsg(mp); 9165 return; 9166 } else if (DB_TYPE(mp) == M_CTL) { 9167 uint32_t mctltype = 0; 9168 uint32_t mlen = MBLKL(first_mp); 9169 9170 mp = mp->b_cont; 9171 mctl_present = B_TRUE; 9172 io = (ipsec_out_t *)first_mp->b_rptr; 9173 9174 /* 9175 * Validate this M_CTL message. The only three types of 9176 * M_CTL messages we expect to see in this code path are 9177 * ipsec_out_t or ipsec_in_t structures (allocated as 9178 * ipsec_info_t unions), or ipsec_ctl_t structures. 9179 * The ipsec_out_type and ipsec_in_type overlap in the two 9180 * data structures, and they are either set to IPSEC_OUT 9181 * or IPSEC_IN depending on which data structure it is. 9182 * ipsec_ctl_t is an IPSEC_CTL. 9183 * 9184 * All other M_CTL messages are sent to ip_wput_nondata() 9185 * for handling. 9186 */ 9187 if (mlen >= sizeof (io->ipsec_out_type)) 9188 mctltype = io->ipsec_out_type; 9189 9190 if ((mlen == sizeof (ipsec_ctl_t)) && 9191 (mctltype == IPSEC_CTL)) { 9192 ip_output(Q_TO_CONN(q), first_mp, q, caller); 9193 return; 9194 } 9195 9196 if ((mlen < sizeof (ipsec_info_t)) || 9197 (mctltype != IPSEC_OUT && mctltype != IPSEC_IN) || 9198 mp == NULL) { 9199 ip_wput_nondata(NULL, q, first_mp, NULL); 9200 return; 9201 } 9202 /* NDP callbacks have q_next non-NULL. That's case #3. */ 9203 if (q->q_next == NULL) { 9204 ip6h = (ip6_t *)mp->b_rptr; 9205 /* 9206 * For a freshly-generated TCP dgram that needs IPV6 9207 * processing, don't call ip_wput immediately. We can 9208 * tell this by the ipsec_out_proc_begin. In-progress 9209 * IPSEC_OUT messages have proc_begin set to TRUE, 9210 * and we want to send all IPSEC_IN messages to 9211 * ip_wput() for IPsec processing or finishing. 9212 */ 9213 if (mctltype == IPSEC_IN || 9214 IPVER(ip6h) != IPV6_VERSION || 9215 io->ipsec_out_proc_begin) { 9216 mibptr = &ip6_mib; 9217 goto notv6; 9218 } 9219 } 9220 } else if (DB_TYPE(mp) != M_DATA) { 9221 ip_wput_nondata(NULL, q, mp, NULL); 9222 return; 9223 } 9224 9225 ip6h = (ip6_t *)mp->b_rptr; 9226 9227 if (IPVER(ip6h) != IPV6_VERSION) { 9228 mibptr = &ip6_mib; 9229 goto notv6; 9230 } 9231 9232 if (q->q_next != NULL) { 9233 ill = (ill_t *)q->q_ptr; 9234 /* 9235 * We don't know if this ill will be used for IPv6 9236 * until the ILLF_IPV6 flag is set via SIOCSLIFNAME. 9237 * ipif_set_values() sets the ill_isv6 flag to true if 9238 * ILLF_IPV6 is set. If the ill_isv6 flag isn't true, 9239 * just drop the packet. 9240 */ 9241 if (!ill->ill_isv6) { 9242 ip1dbg(("ip_wput_v6: Received an IPv6 packet before " 9243 "ILLF_IPV6 was set\n")); 9244 freemsg(first_mp); 9245 return; 9246 } 9247 /* For uniformity do a refhold */ 9248 mutex_enter(&ill->ill_lock); 9249 if (!ILL_CAN_LOOKUP(ill)) { 9250 mutex_exit(&ill->ill_lock); 9251 freemsg(first_mp); 9252 return; 9253 } 9254 ill_refhold_locked(ill); 9255 mutex_exit(&ill->ill_lock); 9256 mibptr = ill->ill_ip6_mib; 9257 /* 9258 * ill_ip6_mib is allocated by ipif_set_values() when 9259 * ill_isv6 is set. Thus if ill_isv6 is true, 9260 * ill_ip6_mib had better not be NULL. 9261 */ 9262 ASSERT(mibptr != NULL); 9263 unspec_src = 0; 9264 BUMP_MIB(mibptr, ipv6OutRequests); 9265 do_outrequests = B_FALSE; 9266 } else { 9267 connp = (conn_t *)arg; 9268 ASSERT(connp != NULL); 9269 9270 /* is queue flow controlled? */ 9271 if ((q->q_first || connp->conn_draining) && 9272 (caller == IP_WPUT)) { 9273 /* 9274 * 1) TCP sends down M_CTL for detached connections. 9275 * 2) AH/ESP sends down M_CTL. 9276 * 9277 * We don't flow control either of the above. Only 9278 * UDP and others are flow controlled for which we 9279 * can't have a M_CTL. 9280 */ 9281 ASSERT(first_mp == mp); 9282 (void) putq(q, mp); 9283 return; 9284 } 9285 mibptr = &ip6_mib; 9286 unspec_src = connp->conn_unspec_src; 9287 do_outrequests = B_TRUE; 9288 if (mp->b_flag & MSGHASREF) { 9289 mp->b_flag &= ~MSGHASREF; 9290 ASSERT(connp->conn_ulp == IPPROTO_SCTP); 9291 SCTP_EXTRACT_IPINFO(mp, sctp_ire); 9292 need_decref = B_TRUE; 9293 } 9294 9295 /* 9296 * If there is a policy, try to attach an ipsec_out in 9297 * the front. At the end, first_mp either points to a 9298 * M_DATA message or IPSEC_OUT message linked to a 9299 * M_DATA message. We have to do it now as we might 9300 * lose the "conn" if we go through ip_newroute. 9301 */ 9302 if (!mctl_present && 9303 (connp->conn_out_enforce_policy || 9304 connp->conn_latch != NULL)) { 9305 ASSERT(first_mp == mp); 9306 /* XXX Any better way to get the protocol fast ? */ 9307 if (((mp = ipsec_attach_ipsec_out(mp, connp, NULL, 9308 connp->conn_ulp)) == NULL)) { 9309 if (need_decref) 9310 CONN_DEC_REF(connp); 9311 return; 9312 } else { 9313 ASSERT(mp->b_datap->db_type == M_CTL); 9314 first_mp = mp; 9315 mp = mp->b_cont; 9316 mctl_present = B_TRUE; 9317 io = (ipsec_out_t *)first_mp->b_rptr; 9318 } 9319 } 9320 } 9321 9322 /* check for alignment and full IPv6 header */ 9323 if (!OK_32PTR((uchar_t *)ip6h) || 9324 (mp->b_wptr - (uchar_t *)ip6h) < IPV6_HDR_LEN) { 9325 ip0dbg(("ip_wput_v6: bad alignment or length\n")); 9326 if (do_outrequests) 9327 BUMP_MIB(mibptr, ipv6OutRequests); 9328 BUMP_MIB(mibptr, ipv6OutDiscards); 9329 freemsg(first_mp); 9330 if (ill != NULL) 9331 ill_refrele(ill); 9332 if (need_decref) 9333 CONN_DEC_REF(connp); 9334 return; 9335 } 9336 v6dstp = &ip6h->ip6_dst; 9337 cksum_request = -1; 9338 ip6i = NULL; 9339 9340 /* 9341 * Once neighbor discovery has completed, ndp_process() will provide 9342 * locally generated packets for which processing can be reattempted. 9343 * In these cases, connp is NULL and the original zone is part of a 9344 * prepended ipsec_out_t. 9345 */ 9346 if (io != NULL) { 9347 zoneid = io->ipsec_out_zoneid; 9348 ASSERT(zoneid != ALL_ZONES); 9349 } else { 9350 zoneid = (connp != NULL ? connp->conn_zoneid : ALL_ZONES); 9351 } 9352 9353 if (ip6h->ip6_nxt == IPPROTO_RAW) { 9354 /* 9355 * This is an ip6i_t header followed by an ip6_hdr. 9356 * Check which fields are set. 9357 * 9358 * When the packet comes from a transport we should have 9359 * all needed headers in the first mblk. However, when 9360 * going through ip_newroute*_v6 the ip6i might be in 9361 * a separate mblk when we return here. In that case 9362 * we pullup everything to ensure that extension and transport 9363 * headers "stay" in the first mblk. 9364 */ 9365 ip6i = (ip6i_t *)ip6h; 9366 ip6i_flags = ip6i->ip6i_flags; 9367 9368 ASSERT((mp->b_wptr - (uchar_t *)ip6i) == sizeof (ip6i_t) || 9369 ((mp->b_wptr - (uchar_t *)ip6i) >= 9370 sizeof (ip6i_t) + IPV6_HDR_LEN)); 9371 9372 if ((mp->b_wptr - (uchar_t *)ip6i) == sizeof (ip6i_t)) { 9373 if (!pullupmsg(mp, -1)) { 9374 ip1dbg(("ip_wput_v6: pullupmsg failed\n")); 9375 if (do_outrequests) 9376 BUMP_MIB(mibptr, ipv6OutRequests); 9377 BUMP_MIB(mibptr, ipv6OutDiscards); 9378 freemsg(first_mp); 9379 if (ill != NULL) 9380 ill_refrele(ill); 9381 if (need_decref) 9382 CONN_DEC_REF(connp); 9383 return; 9384 } 9385 ip6h = (ip6_t *)mp->b_rptr; 9386 v6dstp = &ip6h->ip6_dst; 9387 ip6i = (ip6i_t *)ip6h; 9388 } 9389 ip6h = (ip6_t *)&ip6i[1]; 9390 9391 /* 9392 * Advance rptr past the ip6i_t to get ready for 9393 * transmitting the packet. However, if the packet gets 9394 * passed to ip_newroute*_v6 then rptr is moved back so 9395 * that the ip6i_t header can be inspected when the 9396 * packet comes back here after passing through 9397 * ire_add_then_send. 9398 */ 9399 mp->b_rptr = (uchar_t *)ip6h; 9400 9401 /* 9402 * IP6I_ATTACH_IF is set in this function when we had a 9403 * conn and it was either bound to the IPFF_NOFAILOVER address 9404 * or IPV6_BOUND_PIF was set. These options override other 9405 * options that set the ifindex. We come here with 9406 * IP6I_ATTACH_IF set when we can't find the ire and 9407 * ip_newroute_v6 is feeding the packet for second time. 9408 */ 9409 if ((ip6i->ip6i_flags & IP6I_IFINDEX) || 9410 (ip6i->ip6i_flags & IP6I_ATTACH_IF)) { 9411 ASSERT(ip6i->ip6i_ifindex != 0); 9412 if (ill != NULL) 9413 ill_refrele(ill); 9414 ill = ill_lookup_on_ifindex(ip6i->ip6i_ifindex, 1, 9415 NULL, NULL, NULL, NULL); 9416 if (ill == NULL) { 9417 if (do_outrequests) 9418 BUMP_MIB(mibptr, ipv6OutRequests); 9419 BUMP_MIB(mibptr, ipv6OutDiscards); 9420 ip1dbg(("ip_wput_v6: bad ifindex %d\n", 9421 ip6i->ip6i_ifindex)); 9422 if (need_decref) 9423 CONN_DEC_REF(connp); 9424 freemsg(first_mp); 9425 return; 9426 } 9427 mibptr = ill->ill_ip6_mib; 9428 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 9429 /* 9430 * Preserve the index so that when we return 9431 * from IPSEC processing, we know where to 9432 * send the packet. 9433 */ 9434 if (mctl_present) { 9435 ASSERT(io != NULL); 9436 io->ipsec_out_ill_index = 9437 ip6i->ip6i_ifindex; 9438 } 9439 } 9440 if (ip6i->ip6i_flags & IP6I_ATTACH_IF) { 9441 /* 9442 * This is a multipathing probe packet that has 9443 * been delayed in ND resolution. Drop the 9444 * packet for the reasons mentioned in 9445 * nce_queue_mp() 9446 */ 9447 if ((ip6i->ip6i_flags & IP6I_DROP_IFDELAYED) && 9448 (ip6i->ip6i_flags & IP6I_ND_DELAYED)) { 9449 freemsg(first_mp); 9450 ill_refrele(ill); 9451 if (need_decref) 9452 CONN_DEC_REF(connp); 9453 return; 9454 } 9455 } 9456 } 9457 if (ip6i->ip6i_flags & IP6I_VERIFY_SRC) { 9458 cred_t *cr = DB_CREDDEF(mp, GET_QUEUE_CRED(q)); 9459 9460 ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src)); 9461 if (secpolicy_net_rawaccess(cr) != 0) { 9462 ire = ire_route_lookup_v6(&ip6h->ip6_src, 9463 0, 0, (IRE_LOCAL|IRE_LOOPBACK), NULL, 9464 NULL, zoneid, NULL, 9465 MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY); 9466 if (ire == NULL) { 9467 if (do_outrequests) 9468 BUMP_MIB(mibptr, 9469 ipv6OutRequests); 9470 BUMP_MIB(mibptr, ipv6OutDiscards); 9471 ip1dbg(("ip_wput_v6: bad source " 9472 "addr\n")); 9473 freemsg(first_mp); 9474 if (ill != NULL) 9475 ill_refrele(ill); 9476 if (need_decref) 9477 CONN_DEC_REF(connp); 9478 return; 9479 } 9480 ire_refrele(ire); 9481 } 9482 /* No need to verify again when using ip_newroute */ 9483 ip6i->ip6i_flags &= ~IP6I_VERIFY_SRC; 9484 } 9485 if (!(ip6i->ip6i_flags & IP6I_NEXTHOP)) { 9486 /* 9487 * Make sure they match since ip_newroute*_v6 etc might 9488 * (unknown to them) inspect ip6i_nexthop when 9489 * they think they access ip6_dst. 9490 */ 9491 ip6i->ip6i_nexthop = ip6h->ip6_dst; 9492 } 9493 if (ip6i->ip6i_flags & IP6I_NO_ULP_CKSUM) 9494 cksum_request = 1; 9495 if (ip6i->ip6i_flags & IP6I_RAW_CHECKSUM) 9496 cksum_request = ip6i->ip6i_checksum_off; 9497 if (ip6i->ip6i_flags & IP6I_UNSPEC_SRC) 9498 unspec_src = 1; 9499 9500 if (do_outrequests && ill != NULL) { 9501 BUMP_MIB(mibptr, ipv6OutRequests); 9502 do_outrequests = B_FALSE; 9503 } 9504 /* 9505 * Store ip6i_t info that we need after we come back 9506 * from IPSEC processing. 9507 */ 9508 if (mctl_present) { 9509 ASSERT(io != NULL); 9510 io->ipsec_out_unspec_src = unspec_src; 9511 } 9512 } 9513 if (connp != NULL && connp->conn_dontroute) 9514 ip6h->ip6_hops = 1; 9515 9516 if (IN6_IS_ADDR_MULTICAST(v6dstp)) 9517 goto ipv6multicast; 9518 9519 /* 1. IPV6_BOUND_PIF takes precedence over all the ifindex settings. */ 9520 if (connp != NULL && connp->conn_outgoing_pill != NULL) { 9521 ill_t *conn_outgoing_pill; 9522 9523 conn_outgoing_pill = conn_get_held_ill(connp, 9524 &connp->conn_outgoing_pill, &err); 9525 if (err == ILL_LOOKUP_FAILED) { 9526 if (ill != NULL) 9527 ill_refrele(ill); 9528 if (need_decref) 9529 CONN_DEC_REF(connp); 9530 freemsg(first_mp); 9531 return; 9532 } 9533 if (conn_outgoing_pill != NULL) { 9534 if (ill != NULL) 9535 ill_refrele(ill); 9536 ill = conn_outgoing_pill; 9537 attach_if = B_TRUE; 9538 match_flags = MATCH_IRE_ILL; 9539 mibptr = ill->ill_ip6_mib; 9540 9541 /* 9542 * Check if we need an ire that will not be 9543 * looked up by anybody else i.e. HIDDEN. 9544 */ 9545 if (ill_is_probeonly(ill)) 9546 match_flags |= MATCH_IRE_MARK_HIDDEN; 9547 goto send_from_ill; 9548 } 9549 } 9550 9551 /* 2. If ipc_nofailover_ill is set then use that ill. */ 9552 if (connp != NULL && connp->conn_nofailover_ill != NULL) { 9553 ill_t *conn_nofailover_ill; 9554 9555 conn_nofailover_ill = conn_get_held_ill(connp, 9556 &connp->conn_nofailover_ill, &err); 9557 if (err == ILL_LOOKUP_FAILED) { 9558 if (ill != NULL) 9559 ill_refrele(ill); 9560 if (need_decref) 9561 CONN_DEC_REF(connp); 9562 freemsg(first_mp); 9563 return; 9564 } 9565 if (conn_nofailover_ill != NULL) { 9566 if (ill != NULL) 9567 ill_refrele(ill); 9568 ill = conn_nofailover_ill; 9569 attach_if = B_TRUE; 9570 /* 9571 * Assumes that ipc_nofailover_ill is used only for 9572 * multipathing probe packets. These packets are better 9573 * dropped, if they are delayed in ND resolution, for 9574 * the reasons described in nce_queue_mp(). 9575 * IP6I_DROP_IFDELAYED will be set later on in this 9576 * function for this packet. 9577 */ 9578 drop_if_delayed = B_TRUE; 9579 match_flags = MATCH_IRE_ILL; 9580 mibptr = ill->ill_ip6_mib; 9581 9582 /* 9583 * Check if we need an ire that will not be 9584 * looked up by anybody else i.e. HIDDEN. 9585 */ 9586 if (ill_is_probeonly(ill)) 9587 match_flags |= MATCH_IRE_MARK_HIDDEN; 9588 goto send_from_ill; 9589 } 9590 } 9591 9592 /* 9593 * Redo 1. If we did not find an IRE_CACHE the first time, we should 9594 * have an ip6i_t with IP6I_ATTACH_IF if IPV6_BOUND_PIF or 9595 * bind to the IPIF_NOFAILOVER address was used on this endpoint. 9596 */ 9597 if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_ATTACH_IF)) { 9598 ASSERT(ip6i->ip6i_ifindex != 0); 9599 attach_if = B_TRUE; 9600 ASSERT(ill != NULL); 9601 match_flags = MATCH_IRE_ILL; 9602 9603 /* 9604 * Check if we need an ire that will not be 9605 * looked up by anybody else i.e. HIDDEN. 9606 */ 9607 if (ill_is_probeonly(ill)) 9608 match_flags |= MATCH_IRE_MARK_HIDDEN; 9609 goto send_from_ill; 9610 } 9611 9612 /* 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. */ 9613 if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_IFINDEX)) { 9614 ASSERT(ill != NULL); 9615 goto send_from_ill; 9616 } 9617 9618 /* 9619 * 4. If q is an ill queue and (link local or multicast destination) 9620 * then use that ill. 9621 */ 9622 if (ill != NULL && IN6_IS_ADDR_LINKLOCAL(v6dstp)) { 9623 goto send_from_ill; 9624 } 9625 9626 /* 5. If IPV6_BOUND_IF has been set use that ill. */ 9627 if (connp != NULL && connp->conn_outgoing_ill != NULL) { 9628 ill_t *conn_outgoing_ill; 9629 9630 conn_outgoing_ill = conn_get_held_ill(connp, 9631 &connp->conn_outgoing_ill, &err); 9632 if (err == ILL_LOOKUP_FAILED) { 9633 if (ill != NULL) 9634 ill_refrele(ill); 9635 if (need_decref) 9636 CONN_DEC_REF(connp); 9637 freemsg(first_mp); 9638 return; 9639 } 9640 if (ill != NULL) 9641 ill_refrele(ill); 9642 ill = conn_outgoing_ill; 9643 mibptr = ill->ill_ip6_mib; 9644 goto send_from_ill; 9645 } 9646 9647 /* 9648 * 6. For unicast: Just do an IRE lookup for the best match. 9649 * If we get here for a link-local address it is rather random 9650 * what interface we pick on a multihomed host. 9651 * *If* there is an IRE_CACHE (and the link-local address 9652 * isn't duplicated on multi links) this will find the IRE_CACHE. 9653 * Otherwise it will use one of the matching IRE_INTERFACE routes 9654 * for the link-local prefix. Hence, applications 9655 * *should* be encouraged to specify an outgoing interface when sending 9656 * to a link local address. 9657 */ 9658 if (connp == NULL || (IP_FLOW_CONTROLLED_ULP(connp->conn_ulp) && 9659 !connp->conn_fully_bound)) { 9660 /* 9661 * We cache IRE_CACHEs to avoid lookups. We don't do 9662 * this for the tcp global queue and listen end point 9663 * as it does not really have a real destination to 9664 * talk to. 9665 */ 9666 ire = ire_cache_lookup_v6(v6dstp, zoneid, MBLK_GETLABEL(mp)); 9667 } else { 9668 /* 9669 * IRE_MARK_CONDEMNED is marked in ire_delete. We don't 9670 * grab a lock here to check for CONDEMNED as it is okay 9671 * to send a packet or two with the IRE_CACHE that is going 9672 * away. 9673 */ 9674 mutex_enter(&connp->conn_lock); 9675 ire = sctp_ire != NULL ? sctp_ire : connp->conn_ire_cache; 9676 if (ire != NULL && 9677 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6dstp) && 9678 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 9679 9680 IRE_REFHOLD(ire); 9681 mutex_exit(&connp->conn_lock); 9682 9683 } else { 9684 boolean_t cached = B_FALSE; 9685 9686 connp->conn_ire_cache = NULL; 9687 mutex_exit(&connp->conn_lock); 9688 /* Release the old ire */ 9689 if (ire != NULL && sctp_ire == NULL) 9690 IRE_REFRELE_NOTR(ire); 9691 9692 ire = ire_cache_lookup_v6(v6dstp, zoneid, 9693 MBLK_GETLABEL(mp)); 9694 if (ire != NULL) { 9695 IRE_REFHOLD_NOTR(ire); 9696 9697 mutex_enter(&connp->conn_lock); 9698 if (!(connp->conn_state_flags & CONN_CLOSING) && 9699 (connp->conn_ire_cache == NULL)) { 9700 rw_enter(&ire->ire_bucket->irb_lock, 9701 RW_READER); 9702 if (!(ire->ire_marks & 9703 IRE_MARK_CONDEMNED)) { 9704 connp->conn_ire_cache = ire; 9705 cached = B_TRUE; 9706 } 9707 rw_exit(&ire->ire_bucket->irb_lock); 9708 } 9709 mutex_exit(&connp->conn_lock); 9710 9711 /* 9712 * We can continue to use the ire but since it 9713 * was not cached, we should drop the extra 9714 * reference. 9715 */ 9716 if (!cached) 9717 IRE_REFRELE_NOTR(ire); 9718 } 9719 } 9720 } 9721 9722 if (ire != NULL) { 9723 if (do_outrequests) { 9724 /* Handle IRE_LOCAL's that might appear here */ 9725 if (ire->ire_type == IRE_CACHE) { 9726 mibptr = ((ill_t *)ire->ire_stq->q_ptr)-> 9727 ill_ip6_mib; 9728 } else { 9729 mibptr = ire->ire_ipif->ipif_ill->ill_ip6_mib; 9730 } 9731 BUMP_MIB(mibptr, ipv6OutRequests); 9732 } 9733 ASSERT(!attach_if); 9734 9735 /* 9736 * Check if the ire has the RTF_MULTIRT flag, inherited 9737 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 9738 */ 9739 if (ire->ire_flags & RTF_MULTIRT) { 9740 /* 9741 * Force hop limit of multirouted packets if required. 9742 * The hop limit of such packets is bounded by the 9743 * ip_multirt_ttl ndd variable. 9744 * NDP packets must have a hop limit of 255; don't 9745 * change the hop limit in that case. 9746 */ 9747 if ((ip_multirt_ttl > 0) && 9748 (ip6h->ip6_hops > ip_multirt_ttl) && 9749 (ip6h->ip6_hops != IPV6_MAX_HOPS)) { 9750 if (ip_debug > 3) { 9751 ip2dbg(("ip_wput_v6: forcing multirt " 9752 "hop limit to %d (was %d) ", 9753 ip_multirt_ttl, ip6h->ip6_hops)); 9754 pr_addr_dbg("v6dst %s\n", AF_INET6, 9755 &ire->ire_addr_v6); 9756 } 9757 ip6h->ip6_hops = ip_multirt_ttl; 9758 } 9759 9760 /* 9761 * We look at this point if there are pending 9762 * unresolved routes. ire_multirt_need_resolve_v6() 9763 * checks in O(n) that all IRE_OFFSUBNET ire 9764 * entries for the packet's destination and 9765 * flagged RTF_MULTIRT are currently resolved. 9766 * If some remain unresolved, we do a copy 9767 * of the current message. It will be used 9768 * to initiate additional route resolutions. 9769 */ 9770 multirt_need_resolve = 9771 ire_multirt_need_resolve_v6(&ire->ire_addr_v6, 9772 MBLK_GETLABEL(first_mp)); 9773 ip2dbg(("ip_wput_v6: ire %p, " 9774 "multirt_need_resolve %d, first_mp %p\n", 9775 (void *)ire, multirt_need_resolve, 9776 (void *)first_mp)); 9777 if (multirt_need_resolve) { 9778 copy_mp = copymsg(first_mp); 9779 if (copy_mp != NULL) { 9780 MULTIRT_DEBUG_TAG(copy_mp); 9781 } 9782 } 9783 } 9784 ip_wput_ire_v6(q, first_mp, ire, unspec_src, cksum_request, 9785 connp, caller, 0, ip6i_flags); 9786 if (need_decref) { 9787 CONN_DEC_REF(connp); 9788 connp = NULL; 9789 } 9790 IRE_REFRELE(ire); 9791 9792 /* 9793 * Try to resolve another multiroute if 9794 * ire_multirt_need_resolve_v6() deemed it necessary. 9795 * copy_mp will be consumed (sent or freed) by 9796 * ip_newroute_v6(). 9797 */ 9798 if (copy_mp != NULL) { 9799 if (mctl_present) { 9800 ip6h = (ip6_t *)copy_mp->b_cont->b_rptr; 9801 } else { 9802 ip6h = (ip6_t *)copy_mp->b_rptr; 9803 } 9804 ip_newroute_v6(q, copy_mp, &ip6h->ip6_dst, 9805 &ip6h->ip6_src, NULL, zoneid); 9806 } 9807 if (ill != NULL) 9808 ill_refrele(ill); 9809 return; 9810 } 9811 9812 /* 9813 * No full IRE for this destination. Send it to 9814 * ip_newroute_v6 to see if anything else matches. 9815 * Mark this packet as having originated on this 9816 * machine. 9817 * Update rptr if there was an ip6i_t header. 9818 */ 9819 mp->b_prev = NULL; 9820 mp->b_next = NULL; 9821 if (ip6i != NULL) 9822 mp->b_rptr -= sizeof (ip6i_t); 9823 9824 if (unspec_src) { 9825 if (ip6i == NULL) { 9826 /* 9827 * Add ip6i_t header to carry unspec_src 9828 * until the packet comes back in ip_wput_v6. 9829 */ 9830 mp = ip_add_info_v6(mp, NULL, v6dstp); 9831 if (mp == NULL) { 9832 if (do_outrequests) 9833 BUMP_MIB(mibptr, ipv6OutRequests); 9834 BUMP_MIB(mibptr, ipv6OutDiscards); 9835 if (mctl_present) 9836 freeb(first_mp); 9837 if (ill != NULL) 9838 ill_refrele(ill); 9839 if (need_decref) 9840 CONN_DEC_REF(connp); 9841 return; 9842 } 9843 ip6i = (ip6i_t *)mp->b_rptr; 9844 9845 if (mctl_present) { 9846 ASSERT(first_mp != mp); 9847 first_mp->b_cont = mp; 9848 } else { 9849 first_mp = mp; 9850 } 9851 9852 if ((mp->b_wptr - (uchar_t *)ip6i) == 9853 sizeof (ip6i_t)) { 9854 /* 9855 * ndp_resolver called from ip_newroute_v6 9856 * expects pulled up message. 9857 */ 9858 if (!pullupmsg(mp, -1)) { 9859 ip1dbg(("ip_wput_v6: pullupmsg" 9860 " failed\n")); 9861 if (do_outrequests) { 9862 BUMP_MIB(mibptr, 9863 ipv6OutRequests); 9864 } 9865 BUMP_MIB(mibptr, ipv6OutDiscards); 9866 freemsg(first_mp); 9867 if (ill != NULL) 9868 ill_refrele(ill); 9869 if (need_decref) 9870 CONN_DEC_REF(connp); 9871 return; 9872 } 9873 ip6i = (ip6i_t *)mp->b_rptr; 9874 } 9875 ip6h = (ip6_t *)&ip6i[1]; 9876 v6dstp = &ip6h->ip6_dst; 9877 } 9878 ip6i->ip6i_flags |= IP6I_UNSPEC_SRC; 9879 if (mctl_present) { 9880 ASSERT(io != NULL); 9881 io->ipsec_out_unspec_src = unspec_src; 9882 } 9883 } 9884 if (do_outrequests) 9885 BUMP_MIB(mibptr, ipv6OutRequests); 9886 if (need_decref) 9887 CONN_DEC_REF(connp); 9888 ip_newroute_v6(q, first_mp, v6dstp, &ip6h->ip6_src, NULL, zoneid); 9889 if (ill != NULL) 9890 ill_refrele(ill); 9891 return; 9892 9893 9894 /* 9895 * Handle multicast packets with or without an conn. 9896 * Assumes that the transports set ip6_hops taking 9897 * IPV6_MULTICAST_HOPS (and the other ways to set the hoplimit) 9898 * into account. 9899 */ 9900 ipv6multicast: 9901 ip2dbg(("ip_wput_v6: multicast\n")); 9902 9903 /* 9904 * 1. IPV6_BOUND_PIF takes precedence over all the ifindex settings 9905 * 2. If conn_nofailover_ill is set then use that ill. 9906 * 9907 * Hold the conn_lock till we refhold the ill of interest that is 9908 * pointed to from the conn. Since we cannot do an ill/ipif_refrele 9909 * while holding any locks, postpone the refrele until after the 9910 * conn_lock is dropped. 9911 */ 9912 if (connp != NULL) { 9913 mutex_enter(&connp->conn_lock); 9914 conn_lock_held = B_TRUE; 9915 } else { 9916 conn_lock_held = B_FALSE; 9917 } 9918 if (connp != NULL && connp->conn_outgoing_pill != NULL) { 9919 err = ill_check_and_refhold(connp->conn_outgoing_pill); 9920 if (err == ILL_LOOKUP_FAILED) { 9921 ip1dbg(("ip_output_v6: multicast" 9922 " conn_outgoing_pill no ipif\n")); 9923 multicast_discard: 9924 ASSERT(saved_ill == NULL); 9925 if (conn_lock_held) 9926 mutex_exit(&connp->conn_lock); 9927 if (ill != NULL) 9928 ill_refrele(ill); 9929 freemsg(first_mp); 9930 if (do_outrequests) 9931 BUMP_MIB(mibptr, ipv6OutDiscards); 9932 if (need_decref) 9933 CONN_DEC_REF(connp); 9934 return; 9935 } 9936 saved_ill = ill; 9937 ill = connp->conn_outgoing_pill; 9938 attach_if = B_TRUE; 9939 match_flags = MATCH_IRE_ILL; 9940 mibptr = ill->ill_ip6_mib; 9941 9942 /* 9943 * Check if we need an ire that will not be 9944 * looked up by anybody else i.e. HIDDEN. 9945 */ 9946 if (ill_is_probeonly(ill)) 9947 match_flags |= MATCH_IRE_MARK_HIDDEN; 9948 } else if (connp != NULL && connp->conn_nofailover_ill != NULL) { 9949 err = ill_check_and_refhold(connp->conn_nofailover_ill); 9950 if (err == ILL_LOOKUP_FAILED) { 9951 ip1dbg(("ip_output_v6: multicast" 9952 " conn_nofailover_ill no ipif\n")); 9953 goto multicast_discard; 9954 } 9955 saved_ill = ill; 9956 ill = connp->conn_nofailover_ill; 9957 attach_if = B_TRUE; 9958 match_flags = MATCH_IRE_ILL; 9959 9960 /* 9961 * Check if we need an ire that will not be 9962 * looked up by anybody else i.e. HIDDEN. 9963 */ 9964 if (ill_is_probeonly(ill)) 9965 match_flags |= MATCH_IRE_MARK_HIDDEN; 9966 } else if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_ATTACH_IF)) { 9967 /* 9968 * Redo 1. If we did not find an IRE_CACHE the first time, 9969 * we should have an ip6i_t with IP6I_ATTACH_IF if 9970 * IPV6_BOUND_PIF or bind to the IPIF_NOFAILOVER address was 9971 * used on this endpoint. 9972 */ 9973 ASSERT(ip6i->ip6i_ifindex != 0); 9974 attach_if = B_TRUE; 9975 ASSERT(ill != NULL); 9976 match_flags = MATCH_IRE_ILL; 9977 9978 /* 9979 * Check if we need an ire that will not be 9980 * looked up by anybody else i.e. HIDDEN. 9981 */ 9982 if (ill_is_probeonly(ill)) 9983 match_flags |= MATCH_IRE_MARK_HIDDEN; 9984 } else if (ip6i != NULL && (ip6i->ip6i_flags & IP6I_IFINDEX)) { 9985 /* 3. If an ip6i_t with IP6I_IFINDEX set then use that ill. */ 9986 9987 ASSERT(ill != NULL); 9988 } else if (ill != NULL) { 9989 /* 9990 * 4. If q is an ill queue and (link local or multicast 9991 * destination) then use that ill. 9992 * We don't need the ipif initialization here. 9993 * This useless assert below is just to prevent lint from 9994 * reporting a null body if statement. 9995 */ 9996 ASSERT(ill != NULL); 9997 } else if (connp != NULL) { 9998 /* 9999 * 5. If IPV6_BOUND_IF has been set use that ill. 10000 * 10001 * 6. For multicast: if IPV6_MULTICAST_IF has been set use it. 10002 * Otherwise look for the best IRE match for the unspecified 10003 * group to determine the ill. 10004 * 10005 * conn_multicast_ill is used for only IPv6 packets. 10006 * conn_multicast_ipif is used for only IPv4 packets. 10007 * Thus a PF_INET6 socket send both IPv4 and IPv6 10008 * multicast packets using different IP*_MULTICAST_IF 10009 * interfaces. 10010 */ 10011 if (connp->conn_outgoing_ill != NULL) { 10012 err = ill_check_and_refhold(connp->conn_outgoing_ill); 10013 if (err == ILL_LOOKUP_FAILED) { 10014 ip1dbg(("ip_output_v6: multicast" 10015 " conn_outgoing_ill no ipif\n")); 10016 goto multicast_discard; 10017 } 10018 ill = connp->conn_outgoing_ill; 10019 } else if (connp->conn_multicast_ill != NULL) { 10020 err = ill_check_and_refhold(connp->conn_multicast_ill); 10021 if (err == ILL_LOOKUP_FAILED) { 10022 ip1dbg(("ip_output_v6: multicast" 10023 " conn_multicast_ill no ipif\n")); 10024 goto multicast_discard; 10025 } 10026 ill = connp->conn_multicast_ill; 10027 } else { 10028 mutex_exit(&connp->conn_lock); 10029 conn_lock_held = B_FALSE; 10030 ipif = ipif_lookup_group_v6(v6dstp, zoneid); 10031 if (ipif == NULL) { 10032 ip1dbg(("ip_output_v6: multicast no ipif\n")); 10033 goto multicast_discard; 10034 } 10035 /* 10036 * We have a ref to this ipif, so we can safely 10037 * access ipif_ill. 10038 */ 10039 ill = ipif->ipif_ill; 10040 mutex_enter(&ill->ill_lock); 10041 if (!ILL_CAN_LOOKUP(ill)) { 10042 mutex_exit(&ill->ill_lock); 10043 ipif_refrele(ipif); 10044 ill = NULL; 10045 ip1dbg(("ip_output_v6: multicast no ipif\n")); 10046 goto multicast_discard; 10047 } 10048 ill_refhold_locked(ill); 10049 mutex_exit(&ill->ill_lock); 10050 ipif_refrele(ipif); 10051 /* 10052 * Save binding until IPV6_MULTICAST_IF 10053 * changes it 10054 */ 10055 mutex_enter(&connp->conn_lock); 10056 connp->conn_multicast_ill = ill; 10057 connp->conn_orig_multicast_ifindex = 10058 ill->ill_phyint->phyint_ifindex; 10059 mutex_exit(&connp->conn_lock); 10060 } 10061 } 10062 if (conn_lock_held) 10063 mutex_exit(&connp->conn_lock); 10064 10065 if (saved_ill != NULL) 10066 ill_refrele(saved_ill); 10067 10068 ASSERT(ill != NULL); 10069 /* 10070 * For multicast loopback interfaces replace the multicast address 10071 * with a unicast address for the ire lookup. 10072 */ 10073 if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) 10074 v6dstp = &ill->ill_ipif->ipif_v6lcl_addr; 10075 10076 mibptr = ill->ill_ip6_mib; 10077 if (do_outrequests) { 10078 BUMP_MIB(mibptr, ipv6OutRequests); 10079 do_outrequests = B_FALSE; 10080 } 10081 BUMP_MIB(mibptr, ipv6OutMcastPkts); 10082 10083 /* 10084 * As we may lose the conn by the time we reach ip_wput_ire_v6 10085 * we copy conn_multicast_loop and conn_dontroute on to an 10086 * ipsec_out. In case if this datagram goes out secure, 10087 * we need the ill_index also. Copy that also into the 10088 * ipsec_out. 10089 */ 10090 if (mctl_present) { 10091 io = (ipsec_out_t *)first_mp->b_rptr; 10092 ASSERT(first_mp->b_datap->db_type == M_CTL); 10093 ASSERT(io->ipsec_out_type == IPSEC_OUT); 10094 } else { 10095 ASSERT(mp == first_mp); 10096 if ((first_mp = ipsec_alloc_ipsec_out()) == NULL) { 10097 BUMP_MIB(mibptr, ipv6OutDiscards); 10098 freemsg(mp); 10099 if (ill != NULL) 10100 ill_refrele(ill); 10101 if (need_decref) 10102 CONN_DEC_REF(connp); 10103 return; 10104 } 10105 io = (ipsec_out_t *)first_mp->b_rptr; 10106 /* This is not a secure packet */ 10107 io->ipsec_out_secure = B_FALSE; 10108 io->ipsec_out_use_global_policy = B_TRUE; 10109 io->ipsec_out_zoneid = 10110 (zoneid != ALL_ZONES ? zoneid : GLOBAL_ZONEID); 10111 first_mp->b_cont = mp; 10112 mctl_present = B_TRUE; 10113 } 10114 io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex; 10115 io->ipsec_out_unspec_src = unspec_src; 10116 if (connp != NULL) 10117 io->ipsec_out_dontroute = connp->conn_dontroute; 10118 10119 send_from_ill: 10120 ASSERT(ill != NULL); 10121 ASSERT(mibptr == ill->ill_ip6_mib); 10122 if (do_outrequests) { 10123 BUMP_MIB(mibptr, ipv6OutRequests); 10124 do_outrequests = B_FALSE; 10125 } 10126 10127 if (io != NULL) 10128 io->ipsec_out_ill_index = ill->ill_phyint->phyint_ifindex; 10129 10130 /* 10131 * When a specific ill is specified (using IPV6_PKTINFO, 10132 * IPV6_MULTICAST_IF, or IPV6_BOUND_IF) we will only match 10133 * on routing entries (ftable and ctable) that have a matching 10134 * ire->ire_ipif->ipif_ill. Thus this can only be used 10135 * for destinations that are on-link for the specific ill 10136 * and that can appear on multiple links. Thus it is useful 10137 * for multicast destinations, link-local destinations, and 10138 * at some point perhaps for site-local destinations (if the 10139 * node sits at a site boundary). 10140 * We create the cache entries in the regular ctable since 10141 * it can not "confuse" things for other destinations. 10142 * table. 10143 * 10144 * NOTE : conn_ire_cache is not used for caching ire_ctable_lookups. 10145 * It is used only when ire_cache_lookup is used above. 10146 */ 10147 ire = ire_ctable_lookup_v6(v6dstp, 0, 0, ill->ill_ipif, 10148 zoneid, MBLK_GETLABEL(mp), match_flags); 10149 if (ire != NULL) { 10150 /* 10151 * Check if the ire has the RTF_MULTIRT flag, inherited 10152 * from an IRE_OFFSUBNET ire entry in ip_newroute(). 10153 */ 10154 if (ire->ire_flags & RTF_MULTIRT) { 10155 /* 10156 * Force hop limit of multirouted packets if required. 10157 * The hop limit of such packets is bounded by the 10158 * ip_multirt_ttl ndd variable. 10159 * NDP packets must have a hop limit of 255; don't 10160 * change the hop limit in that case. 10161 */ 10162 if ((ip_multirt_ttl > 0) && 10163 (ip6h->ip6_hops > ip_multirt_ttl) && 10164 (ip6h->ip6_hops != IPV6_MAX_HOPS)) { 10165 if (ip_debug > 3) { 10166 ip2dbg(("ip_wput_v6: forcing multirt " 10167 "hop limit to %d (was %d) ", 10168 ip_multirt_ttl, ip6h->ip6_hops)); 10169 pr_addr_dbg("v6dst %s\n", AF_INET6, 10170 &ire->ire_addr_v6); 10171 } 10172 ip6h->ip6_hops = ip_multirt_ttl; 10173 } 10174 10175 /* 10176 * We look at this point if there are pending 10177 * unresolved routes. ire_multirt_need_resolve_v6() 10178 * checks in O(n) that all IRE_OFFSUBNET ire 10179 * entries for the packet's destination and 10180 * flagged RTF_MULTIRT are currently resolved. 10181 * If some remain unresolved, we make a copy 10182 * of the current message. It will be used 10183 * to initiate additional route resolutions. 10184 */ 10185 multirt_need_resolve = 10186 ire_multirt_need_resolve_v6(&ire->ire_addr_v6, 10187 MBLK_GETLABEL(first_mp)); 10188 ip2dbg(("ip_wput_v6[send_from_ill]: ire %p, " 10189 "multirt_need_resolve %d, first_mp %p\n", 10190 (void *)ire, multirt_need_resolve, 10191 (void *)first_mp)); 10192 if (multirt_need_resolve) { 10193 copy_mp = copymsg(first_mp); 10194 if (copy_mp != NULL) { 10195 MULTIRT_DEBUG_TAG(copy_mp); 10196 } 10197 } 10198 } 10199 10200 ip1dbg(("ip_wput_v6: send on %s, ire = %p, ill index = %d\n", 10201 ill->ill_name, (void *)ire, 10202 ill->ill_phyint->phyint_ifindex)); 10203 ip_wput_ire_v6(q, first_mp, ire, unspec_src, cksum_request, 10204 connp, caller, 10205 (attach_if ? ill->ill_phyint->phyint_ifindex : 0), 10206 ip6i_flags); 10207 ire_refrele(ire); 10208 if (need_decref) { 10209 CONN_DEC_REF(connp); 10210 connp = NULL; 10211 } 10212 10213 /* 10214 * Try to resolve another multiroute if 10215 * ire_multirt_need_resolve_v6() deemed it necessary. 10216 * copy_mp will be consumed (sent or freed) by 10217 * ip_newroute_[ipif_]v6(). 10218 */ 10219 if (copy_mp != NULL) { 10220 if (mctl_present) { 10221 ip6h = (ip6_t *)copy_mp->b_cont->b_rptr; 10222 } else { 10223 ip6h = (ip6_t *)copy_mp->b_rptr; 10224 } 10225 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 10226 ipif = ipif_lookup_group_v6(&ip6h->ip6_dst, 10227 zoneid); 10228 if (ipif == NULL) { 10229 ip1dbg(("ip_wput_v6: No ipif for " 10230 "multicast\n")); 10231 MULTIRT_DEBUG_UNTAG(copy_mp); 10232 freemsg(copy_mp); 10233 return; 10234 } 10235 ip_newroute_ipif_v6(q, copy_mp, ipif, 10236 ip6h->ip6_dst, unspec_src, zoneid); 10237 ipif_refrele(ipif); 10238 } else { 10239 ip_newroute_v6(q, copy_mp, &ip6h->ip6_dst, 10240 &ip6h->ip6_src, ill, zoneid); 10241 } 10242 } 10243 if (ill != NULL) 10244 ill_refrele(ill); 10245 return; 10246 } 10247 if (need_decref) { 10248 CONN_DEC_REF(connp); 10249 connp = NULL; 10250 } 10251 10252 /* Update rptr if there was an ip6i_t header. */ 10253 if (ip6i != NULL) 10254 mp->b_rptr -= sizeof (ip6i_t); 10255 if (unspec_src || attach_if) { 10256 if (ip6i == NULL) { 10257 /* 10258 * Add ip6i_t header to carry unspec_src 10259 * or attach_if until the packet comes back in 10260 * ip_wput_v6. 10261 */ 10262 if (mctl_present) { 10263 first_mp->b_cont = 10264 ip_add_info_v6(mp, NULL, v6dstp); 10265 mp = first_mp->b_cont; 10266 if (mp == NULL) 10267 freeb(first_mp); 10268 } else { 10269 first_mp = mp = ip_add_info_v6(mp, NULL, 10270 v6dstp); 10271 } 10272 if (mp == NULL) { 10273 BUMP_MIB(mibptr, ipv6OutDiscards); 10274 if (ill != NULL) 10275 ill_refrele(ill); 10276 return; 10277 } 10278 ip6i = (ip6i_t *)mp->b_rptr; 10279 if ((mp->b_wptr - (uchar_t *)ip6i) == 10280 sizeof (ip6i_t)) { 10281 /* 10282 * ndp_resolver called from ip_newroute_v6 10283 * expects a pulled up message. 10284 */ 10285 if (!pullupmsg(mp, -1)) { 10286 ip1dbg(("ip_wput_v6: pullupmsg" 10287 " failed\n")); 10288 BUMP_MIB(mibptr, ipv6OutDiscards); 10289 freemsg(first_mp); 10290 return; 10291 } 10292 ip6i = (ip6i_t *)mp->b_rptr; 10293 } 10294 ip6h = (ip6_t *)&ip6i[1]; 10295 v6dstp = &ip6h->ip6_dst; 10296 } 10297 if (unspec_src) 10298 ip6i->ip6i_flags |= IP6I_UNSPEC_SRC; 10299 if (attach_if) { 10300 /* 10301 * Bind to nofailover/BOUND_PIF overrides ifindex. 10302 */ 10303 ip6i->ip6i_flags |= IP6I_ATTACH_IF; 10304 ip6i->ip6i_flags &= ~IP6I_IFINDEX; 10305 ip6i->ip6i_ifindex = ill->ill_phyint->phyint_ifindex; 10306 if (drop_if_delayed) { 10307 /* This is a multipathing probe packet */ 10308 ip6i->ip6i_flags |= IP6I_DROP_IFDELAYED; 10309 } 10310 } 10311 if (mctl_present) { 10312 ASSERT(io != NULL); 10313 io->ipsec_out_unspec_src = unspec_src; 10314 } 10315 } 10316 if (IN6_IS_ADDR_MULTICAST(v6dstp)) { 10317 ip_newroute_ipif_v6(q, first_mp, ill->ill_ipif, *v6dstp, 10318 unspec_src, zoneid); 10319 } else { 10320 ip_newroute_v6(q, first_mp, v6dstp, &ip6h->ip6_src, ill, 10321 zoneid); 10322 } 10323 if (ill != NULL) 10324 ill_refrele(ill); 10325 return; 10326 10327 notv6: 10328 /* 10329 * XXX implement a IPv4 and IPv6 packet counter per conn and 10330 * switch when ratio exceeds e.g. 10:1 10331 */ 10332 if (q->q_next == NULL) { 10333 connp = Q_TO_CONN(q); 10334 10335 if (IPCL_IS_TCP(connp)) { 10336 /* change conn_send for the tcp_v4_connections */ 10337 connp->conn_send = ip_output; 10338 } else if (connp->conn_ulp == IPPROTO_SCTP) { 10339 /* The 'q' is the default SCTP queue */ 10340 connp = (conn_t *)arg; 10341 } else { 10342 ip_setqinfo(RD(q), IPV4_MINOR, B_TRUE); 10343 } 10344 } 10345 BUMP_MIB(mibptr, ipv6OutIPv4); 10346 (void) ip_output(connp, first_mp, q, caller); 10347 if (ill != NULL) 10348 ill_refrele(ill); 10349 } 10350 10351 static void 10352 ip_wput_v6(queue_t *q, mblk_t *mp) 10353 { 10354 ip_output_v6(Q_TO_CONN(q), mp, q, IP_WPUT); 10355 } 10356 10357 static void 10358 ipsec_out_attach_if(ipsec_out_t *io, int attach_index) 10359 { 10360 ASSERT(io->ipsec_out_type == IPSEC_OUT); 10361 io->ipsec_out_attach_if = B_TRUE; 10362 io->ipsec_out_ill_index = attach_index; 10363 } 10364 10365 /* 10366 * NULL send-to queue - packet is to be delivered locally. 10367 */ 10368 void 10369 ip_wput_local_v6(queue_t *q, ill_t *ill, ip6_t *ip6h, mblk_t *first_mp, 10370 ire_t *ire, int fanout_flags) 10371 { 10372 uint32_t ports; 10373 mblk_t *mp = first_mp, *first_mp1; 10374 boolean_t mctl_present; 10375 uint8_t nexthdr; 10376 uint16_t hdr_length; 10377 ipsec_out_t *io; 10378 mib2_ipv6IfStatsEntry_t *mibptr; 10379 ilm_t *ilm; 10380 uint_t nexthdr_offset; 10381 10382 if (DB_TYPE(mp) == M_CTL) { 10383 io = (ipsec_out_t *)mp->b_rptr; 10384 if (!io->ipsec_out_secure) { 10385 mp = mp->b_cont; 10386 freeb(first_mp); 10387 first_mp = mp; 10388 mctl_present = B_FALSE; 10389 } else { 10390 mctl_present = B_TRUE; 10391 mp = first_mp->b_cont; 10392 ipsec_out_to_in(first_mp); 10393 } 10394 } else { 10395 mctl_present = B_FALSE; 10396 } 10397 10398 nexthdr = ip6h->ip6_nxt; 10399 mibptr = ill->ill_ip6_mib; 10400 10401 /* Fastpath */ 10402 switch (nexthdr) { 10403 case IPPROTO_TCP: 10404 case IPPROTO_UDP: 10405 case IPPROTO_ICMPV6: 10406 case IPPROTO_SCTP: 10407 hdr_length = IPV6_HDR_LEN; 10408 nexthdr_offset = (uint_t)((uchar_t *)&ip6h->ip6_nxt - 10409 (uchar_t *)ip6h); 10410 break; 10411 default: { 10412 uint8_t *nexthdrp; 10413 10414 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, 10415 &hdr_length, &nexthdrp)) { 10416 /* Malformed packet */ 10417 BUMP_MIB(mibptr, ipv6OutDiscards); 10418 freemsg(first_mp); 10419 return; 10420 } 10421 nexthdr = *nexthdrp; 10422 nexthdr_offset = nexthdrp - (uint8_t *)ip6h; 10423 break; 10424 } 10425 } 10426 10427 10428 UPDATE_OB_PKT_COUNT(ire); 10429 ire->ire_last_used_time = lbolt; 10430 10431 /* 10432 * Remove reacability confirmation bit from version field 10433 * before looping back the packet. 10434 */ 10435 if (ip6h->ip6_vcf & IP_FORWARD_PROG) { 10436 ip6h->ip6_vcf &= ~IP_FORWARD_PROG; 10437 } 10438 10439 switch (nexthdr) { 10440 case IPPROTO_TCP: 10441 if (DB_TYPE(mp) == M_DATA) { 10442 /* 10443 * M_DATA mblk, so init mblk (chain) for 10444 * no struio(). 10445 */ 10446 mblk_t *mp1 = mp; 10447 10448 do { 10449 mp1->b_datap->db_struioflag = 0; 10450 } while ((mp1 = mp1->b_cont) != NULL); 10451 } 10452 ports = *(uint32_t *)(mp->b_rptr + hdr_length + 10453 TCP_PORTS_OFFSET); 10454 ip_fanout_tcp_v6(q, first_mp, ip6h, ill, ill, 10455 fanout_flags|IP_FF_SEND_ICMP|IP_FF_SYN_ADDIRE| 10456 IP_FF_IP6INFO|IP6_NO_IPPOLICY|IP_FF_LOOPBACK, 10457 hdr_length, mctl_present, ire->ire_zoneid); 10458 return; 10459 10460 case IPPROTO_UDP: 10461 ports = *(uint32_t *)(mp->b_rptr + hdr_length + 10462 UDP_PORTS_OFFSET); 10463 ip_fanout_udp_v6(q, first_mp, ip6h, ports, ill, ill, 10464 fanout_flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO| 10465 IP6_NO_IPPOLICY, mctl_present, ire->ire_zoneid); 10466 return; 10467 10468 case IPPROTO_SCTP: 10469 { 10470 uint_t ipif_seqid = ire->ire_ipif->ipif_seqid; 10471 10472 ports = *(uint32_t *)(mp->b_rptr + hdr_length); 10473 ip_fanout_sctp(mp, ill, (ipha_t *)ip6h, ports, 10474 fanout_flags|IP_FF_SEND_ICMP|IP_FF_IP6INFO, 10475 mctl_present, IP6_NO_IPPOLICY, ipif_seqid, 10476 ire->ire_zoneid); 10477 return; 10478 } 10479 case IPPROTO_ICMPV6: { 10480 icmp6_t *icmp6; 10481 10482 /* check for full IPv6+ICMPv6 header */ 10483 if ((mp->b_wptr - mp->b_rptr) < 10484 (hdr_length + ICMP6_MINLEN)) { 10485 if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) { 10486 ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg" 10487 " failed\n")); 10488 BUMP_MIB(mibptr, ipv6OutDiscards); 10489 freemsg(first_mp); 10490 return; 10491 } 10492 ip6h = (ip6_t *)mp->b_rptr; 10493 } 10494 icmp6 = (icmp6_t *)((uchar_t *)ip6h + hdr_length); 10495 10496 /* Update output mib stats */ 10497 icmp_update_out_mib_v6(ill, icmp6); 10498 10499 /* Check variable for testing applications */ 10500 if (ipv6_drop_inbound_icmpv6) { 10501 freemsg(first_mp); 10502 return; 10503 } 10504 /* 10505 * Assume that there is always at least one conn for 10506 * ICMPv6 (in.ndpd) i.e. don't optimize the case 10507 * where there is no conn. 10508 */ 10509 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst) && 10510 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 10511 /* 10512 * In the multicast case, applications may have 10513 * joined the group from different zones, so we 10514 * need to deliver the packet to each of them. 10515 * Loop through the multicast memberships 10516 * structures (ilm) on the receive ill and send 10517 * a copy of the packet up each matching one. 10518 * However, we don't do this for multicasts sent 10519 * on the loopback interface (PHYI_LOOPBACK flag 10520 * set) as they must stay in the sender's zone. 10521 */ 10522 ILM_WALKER_HOLD(ill); 10523 for (ilm = ill->ill_ilm; ilm != NULL; 10524 ilm = ilm->ilm_next) { 10525 if (ilm->ilm_flags & ILM_DELETED) 10526 continue; 10527 if (!IN6_ARE_ADDR_EQUAL( 10528 &ilm->ilm_v6addr, &ip6h->ip6_dst)) 10529 continue; 10530 if ((fanout_flags & 10531 IP_FF_NO_MCAST_LOOP) && 10532 ilm->ilm_zoneid == ire->ire_zoneid) 10533 continue; 10534 if (!ipif_lookup_zoneid(ill, 10535 ilm->ilm_zoneid, IPIF_UP, NULL)) 10536 continue; 10537 10538 first_mp1 = ip_copymsg(first_mp); 10539 if (first_mp1 == NULL) 10540 continue; 10541 icmp_inbound_v6(q, first_mp1, ill, 10542 hdr_length, mctl_present, 10543 IP6_NO_IPPOLICY, ilm->ilm_zoneid); 10544 } 10545 ILM_WALKER_RELE(ill); 10546 } else { 10547 first_mp1 = ip_copymsg(first_mp); 10548 if (first_mp1 != NULL) 10549 icmp_inbound_v6(q, first_mp1, ill, 10550 hdr_length, mctl_present, 10551 IP6_NO_IPPOLICY, ire->ire_zoneid); 10552 } 10553 } 10554 /* FALLTHRU */ 10555 default: { 10556 /* 10557 * Handle protocols with which IPv6 is less intimate. 10558 */ 10559 fanout_flags |= IP_FF_RAWIP|IP_FF_IP6INFO; 10560 10561 /* 10562 * Enable sending ICMP for "Unknown" nexthdr 10563 * case. i.e. where we did not FALLTHRU from 10564 * IPPROTO_ICMPV6 processing case above. 10565 */ 10566 if (nexthdr != IPPROTO_ICMPV6) 10567 fanout_flags |= IP_FF_SEND_ICMP; 10568 /* 10569 * Note: There can be more than one stream bound 10570 * to a particular protocol. When this is the case, 10571 * each one gets a copy of any incoming packets. 10572 */ 10573 ip_fanout_proto_v6(q, first_mp, ip6h, ill, ill, nexthdr, 10574 nexthdr_offset, fanout_flags|IP6_NO_IPPOLICY, 10575 mctl_present, ire->ire_zoneid); 10576 return; 10577 } 10578 } 10579 } 10580 10581 /* 10582 * Send packet using IRE. 10583 * Checksumming is controlled by cksum_request: 10584 * -1 => normal i.e. TCP/UDP/SCTP/ICMPv6 are checksummed and nothing else. 10585 * 1 => Skip TCP/UDP/SCTP checksum 10586 * Otherwise => checksum_request contains insert offset for checksum 10587 * 10588 * Assumes that the following set of headers appear in the first 10589 * mblk: 10590 * ip6_t 10591 * Any extension headers 10592 * TCP/UDP/SCTP header (if present) 10593 * The routine can handle an ICMPv6 header that is not in the first mblk. 10594 * 10595 * NOTE : This function does not ire_refrele the ire passed in as the 10596 * argument unlike ip_wput_ire where the REFRELE is done. 10597 * Refer to ip_wput_ire for more on this. 10598 */ 10599 static void 10600 ip_wput_ire_v6(queue_t *q, mblk_t *mp, ire_t *ire, int unspec_src, 10601 int cksum_request, conn_t *connp, int caller, int attach_index, int flags) 10602 { 10603 ip6_t *ip6h; 10604 uint8_t nexthdr; 10605 uint16_t hdr_length; 10606 uint_t reachable = 0x0; 10607 ill_t *ill; 10608 mib2_ipv6IfStatsEntry_t *mibptr; 10609 mblk_t *first_mp; 10610 boolean_t mctl_present; 10611 ipsec_out_t *io; 10612 boolean_t conn_dontroute; /* conn value for multicast */ 10613 boolean_t conn_multicast_loop; /* conn value for multicast */ 10614 boolean_t multicast_forward; /* Should we forward ? */ 10615 int max_frag; 10616 zoneid_t zoneid; 10617 10618 zoneid = (connp != NULL ? connp->conn_zoneid : ALL_ZONES); 10619 ill = ire_to_ill(ire); 10620 first_mp = mp; 10621 multicast_forward = B_FALSE; 10622 10623 if (mp->b_datap->db_type != M_CTL) { 10624 ip6h = (ip6_t *)first_mp->b_rptr; 10625 } else { 10626 io = (ipsec_out_t *)first_mp->b_rptr; 10627 ASSERT(io->ipsec_out_type == IPSEC_OUT); 10628 /* 10629 * Grab the zone id now because the M_CTL can be discarded by 10630 * ip_wput_ire_parse_ipsec_out() below. 10631 */ 10632 zoneid = io->ipsec_out_zoneid; 10633 ASSERT(zoneid != ALL_ZONES); 10634 ip6h = (ip6_t *)first_mp->b_cont->b_rptr; 10635 /* 10636 * For the multicast case, ipsec_out carries conn_dontroute and 10637 * conn_multicast_loop as conn may not be available here. We 10638 * need this for multicast loopback and forwarding which is done 10639 * later in the code. 10640 */ 10641 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 10642 conn_dontroute = io->ipsec_out_dontroute; 10643 conn_multicast_loop = io->ipsec_out_multicast_loop; 10644 /* 10645 * If conn_dontroute is not set or conn_multicast_loop 10646 * is set, we need to do forwarding/loopback. For 10647 * datagrams from ip_wput_multicast, conn_dontroute is 10648 * set to B_TRUE and conn_multicast_loop is set to 10649 * B_FALSE so that we neither do forwarding nor 10650 * loopback. 10651 */ 10652 if (!conn_dontroute || conn_multicast_loop) 10653 multicast_forward = B_TRUE; 10654 } 10655 } 10656 10657 /* 10658 * If the sender didn't supply the hop limit and there is a default 10659 * unicast hop limit associated with the output interface, we use 10660 * that if the packet is unicast. Interface specific unicast hop 10661 * limits as set via the SIOCSLIFLNKINFO ioctl. 10662 */ 10663 if (ill->ill_max_hops != 0 && !(flags & IP6I_HOPLIMIT) && 10664 !(IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst))) { 10665 ip6h->ip6_hops = ill->ill_max_hops; 10666 } 10667 10668 if (ire->ire_type == IRE_LOCAL && ire->ire_zoneid != zoneid && 10669 ire->ire_zoneid != ALL_ZONES) { 10670 /* 10671 * When a zone sends a packet to another zone, we try to deliver 10672 * the packet under the same conditions as if the destination 10673 * was a real node on the network. To do so, we look for a 10674 * matching route in the forwarding table. 10675 * RTF_REJECT and RTF_BLACKHOLE are handled just like 10676 * ip_newroute_v6() does. 10677 */ 10678 ire_t *src_ire = ire_ftable_lookup_v6(&ip6h->ip6_dst, 0, 0, 0, 10679 NULL, NULL, zoneid, 0, NULL, (MATCH_IRE_RECURSIVE | 10680 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE)); 10681 if (src_ire != NULL && 10682 !(src_ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))) { 10683 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) && 10684 !unspec_src) { 10685 ip6h->ip6_src = src_ire->ire_src_addr_v6; 10686 } 10687 ire_refrele(src_ire); 10688 } else { 10689 BUMP_MIB(ill->ill_ip6_mib, ipv6OutNoRoutes); 10690 if (src_ire != NULL) { 10691 if (src_ire->ire_flags & RTF_BLACKHOLE) { 10692 ire_refrele(src_ire); 10693 freemsg(first_mp); 10694 return; 10695 } 10696 ire_refrele(src_ire); 10697 } 10698 if (ip_hdr_complete_v6(ip6h, zoneid)) { 10699 /* Failed */ 10700 freemsg(first_mp); 10701 return; 10702 } 10703 icmp_unreachable_v6(q, first_mp, 10704 ICMP6_DST_UNREACH_NOROUTE, B_FALSE, B_FALSE); 10705 return; 10706 } 10707 } 10708 10709 if (mp->b_datap->db_type == M_CTL || ipsec_outbound_v6_policy_present) { 10710 mp = ip_wput_ire_parse_ipsec_out(first_mp, NULL, ip6h, ire, 10711 connp, unspec_src); 10712 if (mp == NULL) { 10713 return; 10714 } 10715 } 10716 10717 first_mp = mp; 10718 if (mp->b_datap->db_type == M_CTL) { 10719 io = (ipsec_out_t *)mp->b_rptr; 10720 ASSERT(io->ipsec_out_type == IPSEC_OUT); 10721 mp = mp->b_cont; 10722 mctl_present = B_TRUE; 10723 } else { 10724 mctl_present = B_FALSE; 10725 } 10726 10727 ip6h = (ip6_t *)mp->b_rptr; 10728 nexthdr = ip6h->ip6_nxt; 10729 mibptr = ill->ill_ip6_mib; 10730 10731 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) && !unspec_src) { 10732 ipif_t *ipif; 10733 10734 /* 10735 * Select the source address using ipif_select_source_v6. 10736 */ 10737 if (attach_index != 0) { 10738 ipif = ipif_select_source_v6(ill, &ip6h->ip6_dst, 10739 RESTRICT_TO_ILL, IPV6_PREFER_SRC_DEFAULT, zoneid); 10740 } else { 10741 ipif = ipif_select_source_v6(ill, &ip6h->ip6_dst, 10742 RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT, zoneid); 10743 } 10744 if (ipif == NULL) { 10745 if (ip_debug > 2) { 10746 /* ip1dbg */ 10747 pr_addr_dbg("ip_wput_ire_v6: no src for " 10748 "dst %s\n, ", AF_INET6, &ip6h->ip6_dst); 10749 printf("ip_wput_ire_v6: interface name %s\n", 10750 ill->ill_name); 10751 } 10752 freemsg(first_mp); 10753 return; 10754 } 10755 ip6h->ip6_src = ipif->ipif_v6src_addr; 10756 ipif_refrele(ipif); 10757 } 10758 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 10759 if ((connp != NULL && connp->conn_multicast_loop) || 10760 !(ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 10761 ilm_t *ilm; 10762 10763 ILM_WALKER_HOLD(ill); 10764 ilm = ilm_lookup_ill_v6(ill, &ip6h->ip6_dst, ALL_ZONES); 10765 ILM_WALKER_RELE(ill); 10766 if (ilm != NULL) { 10767 mblk_t *nmp; 10768 int fanout_flags = 0; 10769 10770 if (connp != NULL && 10771 !connp->conn_multicast_loop) { 10772 fanout_flags |= IP_FF_NO_MCAST_LOOP; 10773 } 10774 ip1dbg(("ip_wput_ire_v6: " 10775 "Loopback multicast\n")); 10776 nmp = ip_copymsg(first_mp); 10777 if (nmp != NULL) { 10778 ip6_t *nip6h; 10779 10780 if (mctl_present) { 10781 nip6h = (ip6_t *) 10782 nmp->b_cont->b_rptr; 10783 } else { 10784 nip6h = (ip6_t *)nmp->b_rptr; 10785 } 10786 /* 10787 * Deliver locally and to every local 10788 * zone, except the sending zone when 10789 * IPV6_MULTICAST_LOOP is disabled. 10790 */ 10791 ip_wput_local_v6(RD(q), ill, nip6h, nmp, 10792 ire, fanout_flags); 10793 } else { 10794 BUMP_MIB(mibptr, ipv6OutDiscards); 10795 ip1dbg(("ip_wput_ire_v6: " 10796 "copymsg failed\n")); 10797 } 10798 } 10799 } 10800 if (ip6h->ip6_hops == 0 || 10801 IN6_IS_ADDR_MC_NODELOCAL(&ip6h->ip6_dst) || 10802 (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK)) { 10803 /* 10804 * Local multicast or just loopback on loopback 10805 * interface. 10806 */ 10807 BUMP_MIB(mibptr, ipv6OutMcastPkts); 10808 ip1dbg(("ip_wput_ire_v6: local multicast only\n")); 10809 freemsg(first_mp); 10810 return; 10811 } 10812 } 10813 10814 if (ire->ire_stq != NULL) { 10815 uint32_t sum; 10816 uint_t ill_index = ((ill_t *)ire->ire_stq->q_ptr)-> 10817 ill_phyint->phyint_ifindex; 10818 queue_t *dev_q = ire->ire_stq->q_next; 10819 10820 /* 10821 * non-NULL send-to queue - packet is to be sent 10822 * out an interface. 10823 */ 10824 10825 /* Driver is flow-controlling? */ 10826 if (!IP_FLOW_CONTROLLED_ULP(nexthdr) && 10827 ((dev_q->q_next || dev_q->q_first) && !canput(dev_q))) { 10828 /* 10829 * Queue packet if we have an conn to give back 10830 * pressure. We can't queue packets intended for 10831 * hardware acceleration since we've tossed that 10832 * state already. If the packet is being fed back 10833 * from ire_send_v6, we don't know the position in 10834 * the queue to enqueue the packet and we discard 10835 * the packet. 10836 */ 10837 if (ip_output_queue && connp != NULL && 10838 !mctl_present && caller != IRE_SEND) { 10839 if (caller == IP_WSRV) { 10840 connp->conn_did_putbq = 1; 10841 (void) putbq(connp->conn_wq, mp); 10842 conn_drain_insert(connp); 10843 /* 10844 * caller == IP_WSRV implies we are 10845 * the service thread, and the 10846 * queue is already noenabled. 10847 * The check for canput and 10848 * the putbq is not atomic. 10849 * So we need to check again. 10850 */ 10851 if (canput(dev_q)) 10852 connp->conn_did_putbq = 0; 10853 } else { 10854 (void) putq(connp->conn_wq, mp); 10855 } 10856 return; 10857 } 10858 BUMP_MIB(mibptr, ipv6OutDiscards); 10859 freemsg(first_mp); 10860 return; 10861 } 10862 10863 /* 10864 * Look for reachability confirmations from the transport. 10865 */ 10866 if (ip6h->ip6_vcf & IP_FORWARD_PROG) { 10867 reachable |= IPV6_REACHABILITY_CONFIRMATION; 10868 ip6h->ip6_vcf &= ~IP_FORWARD_PROG; 10869 if (mctl_present) 10870 io->ipsec_out_reachable = B_TRUE; 10871 } 10872 /* Fastpath */ 10873 switch (nexthdr) { 10874 case IPPROTO_TCP: 10875 case IPPROTO_UDP: 10876 case IPPROTO_ICMPV6: 10877 case IPPROTO_SCTP: 10878 hdr_length = IPV6_HDR_LEN; 10879 break; 10880 default: { 10881 uint8_t *nexthdrp; 10882 10883 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, 10884 &hdr_length, &nexthdrp)) { 10885 /* Malformed packet */ 10886 BUMP_MIB(mibptr, ipv6OutDiscards); 10887 freemsg(first_mp); 10888 return; 10889 } 10890 nexthdr = *nexthdrp; 10891 break; 10892 } 10893 } 10894 10895 if (cksum_request != -1 && nexthdr != IPPROTO_ICMPV6) { 10896 uint16_t *up; 10897 uint16_t *insp; 10898 10899 /* 10900 * The packet header is processed once for all, even 10901 * in the multirouting case. We disable hardware 10902 * checksum if the packet is multirouted, as it will be 10903 * replicated via several interfaces, and not all of 10904 * them may have this capability. 10905 */ 10906 if (cksum_request == 1 && 10907 !(ire->ire_flags & RTF_MULTIRT)) { 10908 /* Skip the transport checksum */ 10909 goto cksum_done; 10910 } 10911 /* 10912 * Do user-configured raw checksum. 10913 * Compute checksum and insert at offset "cksum_request" 10914 */ 10915 10916 /* check for enough headers for checksum */ 10917 cksum_request += hdr_length; /* offset from rptr */ 10918 if ((mp->b_wptr - mp->b_rptr) < 10919 (cksum_request + sizeof (int16_t))) { 10920 if (!pullupmsg(mp, 10921 cksum_request + sizeof (int16_t))) { 10922 ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg" 10923 " failed\n")); 10924 BUMP_MIB(mibptr, ipv6OutDiscards); 10925 freemsg(first_mp); 10926 return; 10927 } 10928 ip6h = (ip6_t *)mp->b_rptr; 10929 } 10930 insp = (uint16_t *)((uchar_t *)ip6h + cksum_request); 10931 ASSERT(((uintptr_t)insp & 0x1) == 0); 10932 up = (uint16_t *)&ip6h->ip6_src; 10933 /* 10934 * icmp has placed length and routing 10935 * header adjustment in *insp. 10936 */ 10937 sum = htons(nexthdr) + 10938 up[0] + up[1] + up[2] + up[3] + 10939 up[4] + up[5] + up[6] + up[7] + 10940 up[8] + up[9] + up[10] + up[11] + 10941 up[12] + up[13] + up[14] + up[15]; 10942 sum = (sum & 0xffff) + (sum >> 16); 10943 *insp = IP_CSUM(mp, hdr_length, sum); 10944 if (*insp == 0) 10945 *insp = 0xFFFF; 10946 } else if (nexthdr == IPPROTO_TCP) { 10947 uint16_t *up; 10948 10949 /* 10950 * Check for full IPv6 header + enough TCP header 10951 * to get at the checksum field. 10952 */ 10953 if ((mp->b_wptr - mp->b_rptr) < 10954 (hdr_length + TCP_CHECKSUM_OFFSET + 10955 TCP_CHECKSUM_SIZE)) { 10956 if (!pullupmsg(mp, hdr_length + 10957 TCP_CHECKSUM_OFFSET + TCP_CHECKSUM_SIZE)) { 10958 ip1dbg(("ip_wput_v6: TCP hdr pullupmsg" 10959 " failed\n")); 10960 BUMP_MIB(mibptr, ipv6OutDiscards); 10961 freemsg(first_mp); 10962 return; 10963 } 10964 ip6h = (ip6_t *)mp->b_rptr; 10965 } 10966 10967 up = (uint16_t *)&ip6h->ip6_src; 10968 /* 10969 * Note: The TCP module has stored the length value 10970 * into the tcp checksum field, so we don't 10971 * need to explicitly sum it in here. 10972 */ 10973 sum = up[0] + up[1] + up[2] + up[3] + 10974 up[4] + up[5] + up[6] + up[7] + 10975 up[8] + up[9] + up[10] + up[11] + 10976 up[12] + up[13] + up[14] + up[15]; 10977 10978 /* Fold the initial sum */ 10979 sum = (sum & 0xffff) + (sum >> 16); 10980 10981 up = (uint16_t *)(((uchar_t *)ip6h) + 10982 hdr_length + TCP_CHECKSUM_OFFSET); 10983 10984 IP_CKSUM_XMIT(ill, ire, mp, ip6h, up, IPPROTO_TCP, 10985 hdr_length, ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 10986 ire->ire_max_frag, mctl_present, sum); 10987 10988 /* Software checksum? */ 10989 if (DB_CKSUMFLAGS(mp) == 0) { 10990 IP6_STAT(ip6_out_sw_cksum); 10991 IP6_STAT_UPDATE(ip6_tcp_out_sw_cksum_bytes, 10992 (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN) - 10993 hdr_length); 10994 } 10995 } else if (nexthdr == IPPROTO_UDP) { 10996 uint16_t *up; 10997 10998 /* 10999 * check for full IPv6 header + enough UDP header 11000 * to get at the UDP checksum field 11001 */ 11002 if ((mp->b_wptr - mp->b_rptr) < (hdr_length + 11003 UDP_CHECKSUM_OFFSET + UDP_CHECKSUM_SIZE)) { 11004 if (!pullupmsg(mp, hdr_length + 11005 UDP_CHECKSUM_OFFSET + UDP_CHECKSUM_SIZE)) { 11006 ip1dbg(("ip_wput_v6: UDP hdr pullupmsg" 11007 " failed\n")); 11008 BUMP_MIB(mibptr, ipv6OutDiscards); 11009 freemsg(first_mp); 11010 return; 11011 } 11012 ip6h = (ip6_t *)mp->b_rptr; 11013 } 11014 up = (uint16_t *)&ip6h->ip6_src; 11015 /* 11016 * Note: The UDP module has stored the length value 11017 * into the udp checksum field, so we don't 11018 * need to explicitly sum it in here. 11019 */ 11020 sum = up[0] + up[1] + up[2] + up[3] + 11021 up[4] + up[5] + up[6] + up[7] + 11022 up[8] + up[9] + up[10] + up[11] + 11023 up[12] + up[13] + up[14] + up[15]; 11024 11025 /* Fold the initial sum */ 11026 sum = (sum & 0xffff) + (sum >> 16); 11027 11028 up = (uint16_t *)(((uchar_t *)ip6h) + 11029 hdr_length + UDP_CHECKSUM_OFFSET); 11030 11031 IP_CKSUM_XMIT(ill, ire, mp, ip6h, up, IPPROTO_UDP, 11032 hdr_length, ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 11033 ire->ire_max_frag, mctl_present, sum); 11034 11035 /* Software checksum? */ 11036 if (DB_CKSUMFLAGS(mp) == 0) { 11037 IP6_STAT(ip6_out_sw_cksum); 11038 IP6_STAT_UPDATE(ip6_udp_out_sw_cksum_bytes, 11039 (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN) - 11040 hdr_length); 11041 } 11042 } else if (nexthdr == IPPROTO_ICMPV6) { 11043 uint16_t *up; 11044 icmp6_t *icmp6; 11045 11046 /* check for full IPv6+ICMPv6 header */ 11047 if ((mp->b_wptr - mp->b_rptr) < 11048 (hdr_length + ICMP6_MINLEN)) { 11049 if (!pullupmsg(mp, hdr_length + ICMP6_MINLEN)) { 11050 ip1dbg(("ip_wput_v6: ICMP hdr pullupmsg" 11051 " failed\n")); 11052 BUMP_MIB(mibptr, ipv6OutDiscards); 11053 freemsg(first_mp); 11054 return; 11055 } 11056 ip6h = (ip6_t *)mp->b_rptr; 11057 } 11058 icmp6 = (icmp6_t *)((uchar_t *)ip6h + hdr_length); 11059 up = (uint16_t *)&ip6h->ip6_src; 11060 /* 11061 * icmp has placed length and routing 11062 * header adjustment in icmp6_cksum. 11063 */ 11064 sum = htons(IPPROTO_ICMPV6) + 11065 up[0] + up[1] + up[2] + up[3] + 11066 up[4] + up[5] + up[6] + up[7] + 11067 up[8] + up[9] + up[10] + up[11] + 11068 up[12] + up[13] + up[14] + up[15]; 11069 sum = (sum & 0xffff) + (sum >> 16); 11070 icmp6->icmp6_cksum = IP_CSUM(mp, hdr_length, sum); 11071 if (icmp6->icmp6_cksum == 0) 11072 icmp6->icmp6_cksum = 0xFFFF; 11073 11074 /* Update output mib stats */ 11075 icmp_update_out_mib_v6(ill, icmp6); 11076 } else if (nexthdr == IPPROTO_SCTP) { 11077 sctp_hdr_t *sctph; 11078 11079 if (MBLKL(mp) < (hdr_length + sizeof (*sctph))) { 11080 if (!pullupmsg(mp, hdr_length + 11081 sizeof (*sctph))) { 11082 ip1dbg(("ip_wput_v6: SCTP hdr pullupmsg" 11083 " failed\n")); 11084 BUMP_MIB(ill->ill_ip6_mib, 11085 ipv6OutDiscards); 11086 freemsg(mp); 11087 return; 11088 } 11089 ip6h = (ip6_t *)mp->b_rptr; 11090 } 11091 sctph = (sctp_hdr_t *)(mp->b_rptr + hdr_length); 11092 sctph->sh_chksum = 0; 11093 sctph->sh_chksum = sctp_cksum(mp, hdr_length); 11094 } 11095 11096 cksum_done: 11097 /* 11098 * We force the insertion of a fragment header using the 11099 * IPH_FRAG_HDR flag in two cases: 11100 * - after reception of an ICMPv6 "packet too big" message 11101 * with a MTU < 1280 (cf. RFC 2460 section 5) 11102 * - for multirouted IPv6 packets, so that the receiver can 11103 * discard duplicates according to their fragment identifier 11104 * 11105 * Two flags modifed from the API can modify this behavior. 11106 * The first is IPV6_USE_MIN_MTU. With this API the user 11107 * can specify how to manage PMTUD for unicast and multicast. 11108 * 11109 * IPV6_DONTFRAG disallows fragmentation. 11110 */ 11111 max_frag = ire->ire_max_frag; 11112 switch (IP6I_USE_MIN_MTU_API(flags)) { 11113 case IPV6_USE_MIN_MTU_DEFAULT: 11114 case IPV6_USE_MIN_MTU_UNICAST: 11115 if (IN6_IS_ADDR_MULTICAST(&ip6h->ip6_dst)) { 11116 max_frag = IPV6_MIN_MTU; 11117 } 11118 break; 11119 11120 case IPV6_USE_MIN_MTU_NEVER: 11121 max_frag = IPV6_MIN_MTU; 11122 break; 11123 } 11124 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN > max_frag || 11125 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 11126 if (connp != NULL && (flags & IP6I_DONTFRAG)) { 11127 icmp_pkt2big_v6(ire->ire_stq, first_mp, 11128 max_frag, B_FALSE, B_TRUE); 11129 return; 11130 } 11131 11132 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN != 11133 (mp->b_cont ? msgdsize(mp) : 11134 mp->b_wptr - (uchar_t *)ip6h)) { 11135 ip0dbg(("Packet length mismatch: %d, %ld\n", 11136 ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN, 11137 msgdsize(mp))); 11138 freemsg(first_mp); 11139 return; 11140 } 11141 /* Do IPSEC processing first */ 11142 if (mctl_present) { 11143 if (attach_index != 0) 11144 ipsec_out_attach_if(io, attach_index); 11145 ipsec_out_process(q, first_mp, ire, ill_index); 11146 return; 11147 } 11148 ASSERT(mp->b_prev == NULL); 11149 ip2dbg(("Fragmenting Size = %d, mtu = %d\n", 11150 ntohs(ip6h->ip6_plen) + 11151 IPV6_HDR_LEN, max_frag)); 11152 ASSERT(mp == first_mp); 11153 /* Initiate IPPF processing */ 11154 if (IPP_ENABLED(IPP_LOCAL_OUT)) { 11155 ip_process(IPP_LOCAL_OUT, &mp, ill_index); 11156 if (mp == NULL) { 11157 return; 11158 } 11159 } 11160 ip_wput_frag_v6(mp, ire, reachable, connp, 11161 caller, max_frag); 11162 return; 11163 } 11164 /* Do IPSEC processing first */ 11165 if (mctl_present) { 11166 int extra_len = ipsec_out_extra_length(first_mp); 11167 11168 if (ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN + extra_len > 11169 max_frag && ip_ulp_cando_pkt2big(nexthdr)) { 11170 /* 11171 * IPsec headers will push the packet over the 11172 * MTU limit. Issue an ICMPv6 Packet Too Big 11173 * message for this packet if the upper-layer 11174 * that issued this packet will be able to 11175 * react to the icmp_pkt2big_v6() that we'll 11176 * generate. 11177 */ 11178 icmp_pkt2big_v6(ire->ire_stq, first_mp, 11179 max_frag, B_FALSE, B_TRUE); 11180 return; 11181 } 11182 if (attach_index != 0) 11183 ipsec_out_attach_if(io, attach_index); 11184 ipsec_out_process(q, first_mp, ire, ill_index); 11185 return; 11186 } 11187 /* 11188 * XXX multicast: add ip_mforward_v6() here. 11189 * Check conn_dontroute 11190 */ 11191 #ifdef lint 11192 /* 11193 * XXX The only purpose of this statement is to avoid lint 11194 * errors. See the above "XXX multicast". When that gets 11195 * fixed, remove this whole #ifdef lint section. 11196 */ 11197 ip3dbg(("multicast forward is %s.\n", 11198 (multicast_forward ? "TRUE" : "FALSE"))); 11199 #endif 11200 11201 UPDATE_OB_PKT_COUNT(ire); 11202 ire->ire_last_used_time = lbolt; 11203 ASSERT(mp == first_mp); 11204 ip_xmit_v6(mp, ire, reachable, connp, caller, NULL); 11205 } else { 11206 ip_wput_local_v6(RD(q), ill, ip6h, first_mp, ire, 0); 11207 } 11208 } 11209 11210 /* 11211 * Outbound IPv6 fragmentation routine using MDT. 11212 */ 11213 static void 11214 ip_wput_frag_mdt_v6(mblk_t *mp, ire_t *ire, size_t max_chunk, 11215 size_t unfragmentable_len, uint8_t nexthdr, uint_t prev_nexthdr_offset) 11216 { 11217 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 11218 uint_t pkts, wroff, hdr_chunk_len, pbuf_idx; 11219 mblk_t *hdr_mp, *md_mp = NULL; 11220 int i1; 11221 multidata_t *mmd; 11222 unsigned char *hdr_ptr, *pld_ptr; 11223 ip_pdescinfo_t pdi; 11224 uint32_t ident; 11225 size_t len; 11226 uint16_t offset; 11227 queue_t *stq = ire->ire_stq; 11228 ill_t *ill = (ill_t *)stq->q_ptr; 11229 11230 ASSERT(DB_TYPE(mp) == M_DATA); 11231 ASSERT(MBLKL(mp) > unfragmentable_len); 11232 11233 /* 11234 * Move read ptr past unfragmentable portion, we don't want this part 11235 * of the data in our fragments. 11236 */ 11237 mp->b_rptr += unfragmentable_len; 11238 11239 /* Calculate how many packets we will send out */ 11240 i1 = (mp->b_cont == NULL) ? MBLKL(mp) : msgsize(mp); 11241 pkts = (i1 + max_chunk - 1) / max_chunk; 11242 ASSERT(pkts > 1); 11243 11244 /* Allocate a message block which will hold all the IP Headers. */ 11245 wroff = ip_wroff_extra; 11246 hdr_chunk_len = wroff + unfragmentable_len + sizeof (ip6_frag_t); 11247 11248 i1 = pkts * hdr_chunk_len; 11249 /* 11250 * Create the header buffer, Multidata and destination address 11251 * and SAP attribute that should be associated with it. 11252 */ 11253 if ((hdr_mp = allocb(i1, BPRI_HI)) == NULL || 11254 ((hdr_mp->b_wptr += i1), 11255 (mmd = mmd_alloc(hdr_mp, &md_mp, KM_NOSLEEP)) == NULL) || 11256 !ip_md_addr_attr(mmd, NULL, ire->ire_nce->nce_res_mp)) { 11257 freemsg(mp); 11258 if (md_mp == NULL) { 11259 freemsg(hdr_mp); 11260 } else { 11261 free_mmd: IP6_STAT(ip6_frag_mdt_discarded); 11262 freemsg(md_mp); 11263 } 11264 IP6_STAT(ip6_frag_mdt_allocfail); 11265 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails); 11266 UPDATE_MIB(ill->ill_ip6_mib, ipv6OutDiscards, pkts); 11267 return; 11268 } 11269 IP6_STAT(ip6_frag_mdt_allocd); 11270 11271 /* 11272 * Add a payload buffer to the Multidata; this operation must not 11273 * fail, or otherwise our logic in this routine is broken. There 11274 * is no memory allocation done by the routine, so any returned 11275 * failure simply tells us that we've done something wrong. 11276 * 11277 * A failure tells us that either we're adding the same payload 11278 * buffer more than once, or we're trying to add more buffers than 11279 * allowed. None of the above cases should happen, and we panic 11280 * because either there's horrible heap corruption, and/or 11281 * programming mistake. 11282 */ 11283 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) { 11284 goto pbuf_panic; 11285 } 11286 11287 hdr_ptr = hdr_mp->b_rptr; 11288 pld_ptr = mp->b_rptr; 11289 11290 pdi.flags = PDESC_HBUF_REF | PDESC_PBUF_REF; 11291 11292 ident = htonl(atomic_add_32_nv(&ire->ire_ident, 1)); 11293 11294 /* 11295 * len is the total length of the fragmentable data in this 11296 * datagram. For each fragment sent, we will decrement len 11297 * by the amount of fragmentable data sent in that fragment 11298 * until len reaches zero. 11299 */ 11300 len = ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN); 11301 11302 offset = 0; 11303 prev_nexthdr_offset += wroff; 11304 11305 while (len != 0) { 11306 size_t mlen; 11307 ip6_t *fip6h; 11308 ip6_frag_t *fraghdr; 11309 int error; 11310 11311 ASSERT((hdr_ptr + hdr_chunk_len) <= hdr_mp->b_wptr); 11312 mlen = MIN(len, max_chunk); 11313 len -= mlen; 11314 11315 fip6h = (ip6_t *)(hdr_ptr + wroff); 11316 ASSERT(OK_32PTR(fip6h)); 11317 bcopy(ip6h, fip6h, unfragmentable_len); 11318 hdr_ptr[prev_nexthdr_offset] = IPPROTO_FRAGMENT; 11319 11320 fip6h->ip6_plen = htons((uint16_t)(mlen + 11321 unfragmentable_len - IPV6_HDR_LEN + sizeof (ip6_frag_t))); 11322 11323 fraghdr = (ip6_frag_t *)((unsigned char *)fip6h + 11324 unfragmentable_len); 11325 fraghdr->ip6f_nxt = nexthdr; 11326 fraghdr->ip6f_reserved = 0; 11327 fraghdr->ip6f_offlg = htons(offset) | 11328 ((len != 0) ? IP6F_MORE_FRAG : 0); 11329 fraghdr->ip6f_ident = ident; 11330 11331 /* 11332 * Record offset and size of header and data of the next packet 11333 * in the multidata message. 11334 */ 11335 PDESC_HDR_ADD(&pdi, hdr_ptr, wroff, 11336 unfragmentable_len + sizeof (ip6_frag_t), 0); 11337 PDESC_PLD_INIT(&pdi); 11338 i1 = MIN(mp->b_wptr - pld_ptr, mlen); 11339 ASSERT(i1 > 0); 11340 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, pld_ptr, i1); 11341 if (i1 == mlen) { 11342 pld_ptr += mlen; 11343 } else { 11344 i1 = mlen - i1; 11345 mp = mp->b_cont; 11346 ASSERT(mp != NULL); 11347 ASSERT(MBLKL(mp) >= i1); 11348 /* 11349 * Attach the next payload message block to the 11350 * multidata message. 11351 */ 11352 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 11353 goto pbuf_panic; 11354 PDESC_PLD_SPAN_ADD(&pdi, pbuf_idx, mp->b_rptr, i1); 11355 pld_ptr = mp->b_rptr + i1; 11356 } 11357 11358 if ((mmd_addpdesc(mmd, (pdescinfo_t *)&pdi, &error, 11359 KM_NOSLEEP)) == NULL) { 11360 /* 11361 * Any failure other than ENOMEM indicates that we 11362 * have passed in invalid pdesc info or parameters 11363 * to mmd_addpdesc, which must not happen. 11364 * 11365 * EINVAL is a result of failure on boundary checks 11366 * against the pdesc info contents. It should not 11367 * happen, and we panic because either there's 11368 * horrible heap corruption, and/or programming 11369 * mistake. 11370 */ 11371 if (error != ENOMEM) { 11372 cmn_err(CE_PANIC, "ip_wput_frag_mdt_v6: " 11373 "pdesc logic error detected for " 11374 "mmd %p pinfo %p (%d)\n", 11375 (void *)mmd, (void *)&pdi, error); 11376 /* NOTREACHED */ 11377 } 11378 IP6_STAT(ip6_frag_mdt_addpdescfail); 11379 /* Free unattached payload message blocks as well */ 11380 md_mp->b_cont = mp->b_cont; 11381 goto free_mmd; 11382 } 11383 11384 /* Advance fragment offset. */ 11385 offset += mlen; 11386 11387 /* Advance to location for next header in the buffer. */ 11388 hdr_ptr += hdr_chunk_len; 11389 11390 /* Did we reach the next payload message block? */ 11391 if (pld_ptr == mp->b_wptr && mp->b_cont != NULL) { 11392 mp = mp->b_cont; 11393 /* 11394 * Attach the next message block with payload 11395 * data to the multidata message. 11396 */ 11397 if ((pbuf_idx = mmd_addpldbuf(mmd, mp)) < 0) 11398 goto pbuf_panic; 11399 pld_ptr = mp->b_rptr; 11400 } 11401 } 11402 11403 ASSERT(hdr_mp->b_wptr == hdr_ptr); 11404 ASSERT(mp->b_wptr == pld_ptr); 11405 11406 /* Update IP statistics */ 11407 UPDATE_MIB(ill->ill_ip6_mib, ipv6OutFragCreates, pkts); 11408 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragOKs); 11409 IP6_STAT_UPDATE(ip6_frag_mdt_pkt_out, pkts); 11410 11411 ire->ire_ob_pkt_count += pkts; 11412 if (ire->ire_ipif != NULL) 11413 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, pkts); 11414 11415 ire->ire_last_used_time = lbolt; 11416 /* Send it down */ 11417 putnext(stq, md_mp); 11418 return; 11419 11420 pbuf_panic: 11421 cmn_err(CE_PANIC, "ip_wput_frag_mdt_v6: payload buffer logic " 11422 "error for mmd %p pbuf %p (%d)", (void *)mmd, (void *)mp, 11423 pbuf_idx); 11424 /* NOTREACHED */ 11425 } 11426 11427 /* 11428 * IPv6 fragmentation. Essentially the same as IPv4 fragmentation. 11429 * We have not optimized this in terms of number of mblks 11430 * allocated. For instance, for each fragment sent we always allocate a 11431 * mblk to hold the IPv6 header and fragment header. 11432 * 11433 * Assumes that all the extension headers are contained in the first mblk. 11434 * 11435 * The fragment header is inserted after an hop-by-hop options header 11436 * and after [an optional destinations header followed by] a routing header. 11437 * 11438 * NOTE : This function does not ire_refrele the ire passed in as 11439 * the argument. 11440 */ 11441 void 11442 ip_wput_frag_v6(mblk_t *mp, ire_t *ire, uint_t reachable, conn_t *connp, 11443 int caller, int max_frag) 11444 { 11445 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 11446 ip6_t *fip6h; 11447 mblk_t *hmp; 11448 mblk_t *hmp0; 11449 mblk_t *dmp; 11450 ip6_frag_t *fraghdr; 11451 size_t unfragmentable_len; 11452 size_t len; 11453 size_t mlen; 11454 size_t max_chunk; 11455 uint32_t ident; 11456 uint16_t off_flags; 11457 uint16_t offset = 0; 11458 ill_t *ill; 11459 uint8_t nexthdr; 11460 uint_t prev_nexthdr_offset; 11461 uint8_t *ptr; 11462 11463 ASSERT(ire->ire_type == IRE_CACHE); 11464 ill = (ill_t *)ire->ire_stq->q_ptr; 11465 11466 /* 11467 * Determine the length of the unfragmentable portion of this 11468 * datagram. This consists of the IPv6 header, a potential 11469 * hop-by-hop options header, a potential pre-routing-header 11470 * destination options header, and a potential routing header. 11471 */ 11472 nexthdr = ip6h->ip6_nxt; 11473 prev_nexthdr_offset = (uint8_t *)&ip6h->ip6_nxt - (uint8_t *)ip6h; 11474 ptr = (uint8_t *)&ip6h[1]; 11475 11476 if (nexthdr == IPPROTO_HOPOPTS) { 11477 ip6_hbh_t *hbh_hdr; 11478 uint_t hdr_len; 11479 11480 hbh_hdr = (ip6_hbh_t *)ptr; 11481 hdr_len = 8 * (hbh_hdr->ip6h_len + 1); 11482 nexthdr = hbh_hdr->ip6h_nxt; 11483 prev_nexthdr_offset = (uint8_t *)&hbh_hdr->ip6h_nxt 11484 - (uint8_t *)ip6h; 11485 ptr += hdr_len; 11486 } 11487 if (nexthdr == IPPROTO_DSTOPTS) { 11488 ip6_dest_t *dest_hdr; 11489 uint_t hdr_len; 11490 11491 dest_hdr = (ip6_dest_t *)ptr; 11492 if (dest_hdr->ip6d_nxt == IPPROTO_ROUTING) { 11493 hdr_len = 8 * (dest_hdr->ip6d_len + 1); 11494 nexthdr = dest_hdr->ip6d_nxt; 11495 prev_nexthdr_offset = (uint8_t *)&dest_hdr->ip6d_nxt 11496 - (uint8_t *)ip6h; 11497 ptr += hdr_len; 11498 } 11499 } 11500 if (nexthdr == IPPROTO_ROUTING) { 11501 ip6_rthdr_t *rthdr; 11502 uint_t hdr_len; 11503 11504 rthdr = (ip6_rthdr_t *)ptr; 11505 nexthdr = rthdr->ip6r_nxt; 11506 prev_nexthdr_offset = (uint8_t *)&rthdr->ip6r_nxt 11507 - (uint8_t *)ip6h; 11508 hdr_len = 8 * (rthdr->ip6r_len + 1); 11509 ptr += hdr_len; 11510 } 11511 unfragmentable_len = (uint_t)(ptr - (uint8_t *)ip6h); 11512 11513 max_chunk = (min(max_frag, ire->ire_max_frag) - unfragmentable_len - 11514 sizeof (ip6_frag_t)) & ~7; 11515 11516 /* Check if we can use MDT to send out the frags. */ 11517 ASSERT(!IRE_IS_LOCAL(ire)); 11518 if (ip_multidata_outbound && reachable == 0 && 11519 !(ire->ire_flags & RTF_MULTIRT) && ILL_MDT_CAPABLE(ill) && 11520 IP_CAN_FRAG_MDT(mp, unfragmentable_len, max_chunk)) { 11521 ip_wput_frag_mdt_v6(mp, ire, max_chunk, unfragmentable_len, 11522 nexthdr, prev_nexthdr_offset); 11523 return; 11524 } 11525 11526 /* 11527 * Allocate an mblk with enough room for the link-layer 11528 * header, the unfragmentable part of the datagram, and the 11529 * fragment header. This (or a copy) will be used as the 11530 * first mblk for each fragment we send. 11531 */ 11532 hmp = allocb(unfragmentable_len + sizeof (ip6_frag_t) + ip_wroff_extra, 11533 BPRI_HI); 11534 if (hmp == NULL) { 11535 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails); 11536 freemsg(mp); 11537 return; 11538 } 11539 hmp->b_rptr += ip_wroff_extra; 11540 hmp->b_wptr = hmp->b_rptr + unfragmentable_len + sizeof (ip6_frag_t); 11541 11542 fip6h = (ip6_t *)hmp->b_rptr; 11543 fraghdr = (ip6_frag_t *)(hmp->b_rptr + unfragmentable_len); 11544 11545 bcopy(ip6h, fip6h, unfragmentable_len); 11546 hmp->b_rptr[prev_nexthdr_offset] = IPPROTO_FRAGMENT; 11547 11548 ident = atomic_add_32_nv(&ire->ire_ident, 1); 11549 11550 fraghdr->ip6f_nxt = nexthdr; 11551 fraghdr->ip6f_reserved = 0; 11552 fraghdr->ip6f_offlg = 0; 11553 fraghdr->ip6f_ident = htonl(ident); 11554 11555 /* 11556 * len is the total length of the fragmentable data in this 11557 * datagram. For each fragment sent, we will decrement len 11558 * by the amount of fragmentable data sent in that fragment 11559 * until len reaches zero. 11560 */ 11561 len = ntohs(ip6h->ip6_plen) - (unfragmentable_len - IPV6_HDR_LEN); 11562 11563 /* 11564 * Move read ptr past unfragmentable portion, we don't want this part 11565 * of the data in our fragments. 11566 */ 11567 mp->b_rptr += unfragmentable_len; 11568 11569 while (len != 0) { 11570 mlen = MIN(len, max_chunk); 11571 len -= mlen; 11572 if (len != 0) { 11573 /* Not last */ 11574 hmp0 = copyb(hmp); 11575 if (hmp0 == NULL) { 11576 freeb(hmp); 11577 freemsg(mp); 11578 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails); 11579 ip1dbg(("ip_wput_frag_v6: copyb failed\n")); 11580 return; 11581 } 11582 off_flags = IP6F_MORE_FRAG; 11583 } else { 11584 /* Last fragment */ 11585 hmp0 = hmp; 11586 hmp = NULL; 11587 off_flags = 0; 11588 } 11589 fip6h = (ip6_t *)(hmp0->b_rptr); 11590 fraghdr = (ip6_frag_t *)(hmp0->b_rptr + unfragmentable_len); 11591 11592 fip6h->ip6_plen = htons((uint16_t)(mlen + 11593 unfragmentable_len - IPV6_HDR_LEN + sizeof (ip6_frag_t))); 11594 /* 11595 * Note: Optimization alert. 11596 * In IPv6 (and IPv4) protocol header, Fragment Offset 11597 * ("offset") is 13 bits wide and in 8-octet units. 11598 * In IPv6 protocol header (unlike IPv4) in a 16 bit field, 11599 * it occupies the most significant 13 bits. 11600 * (least significant 13 bits in IPv4). 11601 * We do not do any shifts here. Not shifting is same effect 11602 * as taking offset value in octet units, dividing by 8 and 11603 * then shifting 3 bits left to line it up in place in proper 11604 * place protocol header. 11605 */ 11606 fraghdr->ip6f_offlg = htons(offset) | off_flags; 11607 11608 if (!(dmp = ip_carve_mp(&mp, mlen))) { 11609 /* mp has already been freed by ip_carve_mp() */ 11610 if (hmp != NULL) 11611 freeb(hmp); 11612 freeb(hmp0); 11613 ip1dbg(("ip_carve_mp: failed\n")); 11614 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragFails); 11615 return; 11616 } 11617 hmp0->b_cont = dmp; 11618 /* Get the priority marking, if any */ 11619 hmp0->b_band = dmp->b_band; 11620 UPDATE_OB_PKT_COUNT(ire); 11621 ire->ire_last_used_time = lbolt; 11622 ip_xmit_v6(hmp0, ire, reachable | IP6_NO_IPPOLICY, connp, 11623 caller, NULL); 11624 reachable = 0; /* No need to redo state machine in loop */ 11625 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragCreates); 11626 offset += mlen; 11627 } 11628 BUMP_MIB(ill->ill_ip6_mib, ipv6OutFragOKs); 11629 } 11630 11631 /* 11632 * Determine if the ill and multicast aspects of that packets 11633 * "matches" the conn. 11634 */ 11635 boolean_t 11636 conn_wantpacket_v6(conn_t *connp, ill_t *ill, ip6_t *ip6h, int fanout_flags, 11637 zoneid_t zoneid) 11638 { 11639 ill_t *in_ill; 11640 boolean_t wantpacket = B_TRUE; 11641 in6_addr_t *v6dst_ptr = &ip6h->ip6_dst; 11642 in6_addr_t *v6src_ptr = &ip6h->ip6_src; 11643 11644 /* 11645 * conn_incoming_ill is set by IPV6_BOUND_IF which limits 11646 * unicast and multicast reception to conn_incoming_ill. 11647 * conn_wantpacket_v6 is called both for unicast and 11648 * multicast. 11649 * 11650 * 1) The unicast copy of the packet can come anywhere in 11651 * the ill group if it is part of the group. Thus, we 11652 * need to check to see whether the ill group matches 11653 * if in_ill is part of a group. 11654 * 11655 * 2) ip_rput does not suppress duplicate multicast packets. 11656 * If there are two interfaces in a ill group and we have 11657 * 2 applications (conns) joined a multicast group G on 11658 * both the interfaces, ilm_lookup_ill filter in ip_rput 11659 * will give us two packets because we join G on both the 11660 * interfaces rather than nominating just one interface 11661 * for receiving multicast like broadcast above. So, 11662 * we have to call ilg_lookup_ill to filter out duplicate 11663 * copies, if ill is part of a group, to supress duplicates. 11664 */ 11665 in_ill = connp->conn_incoming_ill; 11666 if (in_ill != NULL) { 11667 mutex_enter(&connp->conn_lock); 11668 in_ill = connp->conn_incoming_ill; 11669 mutex_enter(&ill->ill_lock); 11670 /* 11671 * No IPMP, and the packet did not arrive on conn_incoming_ill 11672 * OR, IPMP in use and the packet arrived on an IPMP group 11673 * different from the conn_incoming_ill's IPMP group. 11674 * Reject the packet. 11675 */ 11676 if ((in_ill->ill_group == NULL && in_ill != ill) || 11677 (in_ill->ill_group != NULL && 11678 in_ill->ill_group != ill->ill_group)) { 11679 wantpacket = B_FALSE; 11680 } 11681 mutex_exit(&ill->ill_lock); 11682 mutex_exit(&connp->conn_lock); 11683 if (!wantpacket) 11684 return (B_FALSE); 11685 } 11686 11687 if (connp->conn_multi_router) 11688 return (B_TRUE); 11689 11690 if (!IN6_IS_ADDR_MULTICAST(v6dst_ptr) && 11691 !IN6_IS_ADDR_V4MAPPED_CLASSD(v6dst_ptr)) { 11692 /* 11693 * Unicast case: we match the conn only if it's in the specified 11694 * zone. 11695 */ 11696 return (connp->conn_zoneid == zoneid || zoneid == ALL_ZONES); 11697 } 11698 11699 if ((fanout_flags & IP_FF_NO_MCAST_LOOP) && 11700 (connp->conn_zoneid == zoneid || zoneid == ALL_ZONES)) { 11701 /* 11702 * Loopback case: the sending endpoint has IP_MULTICAST_LOOP 11703 * disabled, therefore we don't dispatch the multicast packet to 11704 * the sending zone. 11705 */ 11706 return (B_FALSE); 11707 } 11708 11709 if ((ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) && 11710 connp->conn_zoneid != zoneid && zoneid != ALL_ZONES) { 11711 /* 11712 * Multicast packet on the loopback interface: we only match 11713 * conns who joined the group in the specified zone. 11714 */ 11715 return (B_FALSE); 11716 } 11717 11718 mutex_enter(&connp->conn_lock); 11719 wantpacket = 11720 ilg_lookup_ill_withsrc_v6(connp, v6dst_ptr, v6src_ptr, ill) != NULL; 11721 mutex_exit(&connp->conn_lock); 11722 11723 return (wantpacket); 11724 } 11725 11726 11727 /* 11728 * Transmit a packet and update any NUD state based on the flags 11729 * XXX need to "recover" any ip6i_t when doing putq! 11730 * 11731 * NOTE : This function does not ire_refrele the ire passed in as the 11732 * argument. 11733 */ 11734 void 11735 ip_xmit_v6(mblk_t *mp, ire_t *ire, uint_t flags, conn_t *connp, 11736 int caller, ipsec_out_t *io) 11737 { 11738 mblk_t *mp1; 11739 nce_t *nce = ire->ire_nce; 11740 ill_t *ill; 11741 uint64_t delta; 11742 ip6_t *ip6h; 11743 queue_t *stq = ire->ire_stq; 11744 ire_t *ire1 = NULL; 11745 ire_t *save_ire = ire; 11746 boolean_t multirt_send = B_FALSE; 11747 mblk_t *next_mp = NULL; 11748 11749 ip6h = (ip6_t *)mp->b_rptr; 11750 ASSERT(!IN6_IS_ADDR_V4MAPPED(&ire->ire_addr_v6)); 11751 ASSERT(ire->ire_ipversion == IPV6_VERSION); 11752 ASSERT(nce != NULL); 11753 ASSERT(mp->b_datap->db_type == M_DATA); 11754 ASSERT(stq != NULL); 11755 11756 ill = ire_to_ill(ire); 11757 if (!ill) { 11758 ip0dbg(("ip_xmit_v6: ire_to_ill failed\n")); 11759 freemsg(mp); 11760 return; 11761 } 11762 11763 /* 11764 * If a packet is to be sent out an interface that is a 6to4 11765 * tunnel, outgoing IPv6 packets, with a 6to4 addressed IPv6 11766 * destination, must be checked to have a 6to4 prefix 11767 * (2002:V4ADDR::/48) that is NOT equal to the 6to4 prefix of 11768 * address configured on the sending interface. Otherwise, 11769 * the packet was delivered to this interface in error and the 11770 * packet must be dropped. 11771 */ 11772 if ((ill->ill_is_6to4tun) && IN6_IS_ADDR_6TO4(&ip6h->ip6_dst)) { 11773 ipif_t *ipif = ill->ill_ipif; 11774 11775 if (IN6_ARE_6TO4_PREFIX_EQUAL(&ipif->ipif_v6lcl_addr, 11776 &ip6h->ip6_dst)) { 11777 if (ip_debug > 2) { 11778 /* ip1dbg */ 11779 pr_addr_dbg("ip_xmit_v6: attempting to " 11780 "send 6to4 addressed IPv6 " 11781 "destination (%s) out the wrong " 11782 "interface.\n", AF_INET6, 11783 &ip6h->ip6_dst); 11784 } 11785 BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards); 11786 freemsg(mp); 11787 return; 11788 } 11789 } 11790 11791 /* Flow-control check has been done in ip_wput_ire_v6 */ 11792 if (IP_FLOW_CONTROLLED_ULP(ip6h->ip6_nxt) || caller == IP_WPUT || 11793 caller == IP_WSRV || canput(stq->q_next)) { 11794 uint32_t ill_index; 11795 11796 /* 11797 * In most cases, the emission loop below is entered only 11798 * once. Only in the case where the ire holds the 11799 * RTF_MULTIRT flag, do we loop to process all RTF_MULTIRT 11800 * flagged ires in the bucket, and send the packet 11801 * through all crossed RTF_MULTIRT routes. 11802 */ 11803 if (ire->ire_flags & RTF_MULTIRT) { 11804 /* 11805 * Multirouting case. The bucket where ire is stored 11806 * probably holds other RTF_MULTIRT flagged ires 11807 * to the destination. In this call to ip_xmit_v6, 11808 * we attempt to send the packet through all 11809 * those ires. Thus, we first ensure that ire is the 11810 * first RTF_MULTIRT ire in the bucket, 11811 * before walking the ire list. 11812 */ 11813 ire_t *first_ire; 11814 irb_t *irb = ire->ire_bucket; 11815 ASSERT(irb != NULL); 11816 multirt_send = B_TRUE; 11817 11818 /* Make sure we do not omit any multiroute ire. */ 11819 IRB_REFHOLD(irb); 11820 for (first_ire = irb->irb_ire; 11821 first_ire != NULL; 11822 first_ire = first_ire->ire_next) { 11823 if ((first_ire->ire_flags & RTF_MULTIRT) && 11824 (IN6_ARE_ADDR_EQUAL(&first_ire->ire_addr_v6, 11825 &ire->ire_addr_v6)) && 11826 !(first_ire->ire_marks & 11827 (IRE_MARK_CONDEMNED | IRE_MARK_HIDDEN))) 11828 break; 11829 } 11830 11831 if ((first_ire != NULL) && (first_ire != ire)) { 11832 IRE_REFHOLD(first_ire); 11833 /* ire will be released by the caller */ 11834 ire = first_ire; 11835 nce = ire->ire_nce; 11836 stq = ire->ire_stq; 11837 ill = ire_to_ill(ire); 11838 } 11839 IRB_REFRELE(irb); 11840 } else if (connp != NULL && IPCL_IS_TCP(connp) && 11841 connp->conn_mdt_ok && !connp->conn_tcp->tcp_mdt && 11842 ILL_MDT_USABLE(ill)) { 11843 /* 11844 * This tcp connection was marked as MDT-capable, but 11845 * it has been turned off due changes in the interface. 11846 * Now that the interface support is back, turn it on 11847 * by notifying tcp. We don't directly modify tcp_mdt, 11848 * since we leave all the details to the tcp code that 11849 * knows better. 11850 */ 11851 mblk_t *mdimp = ip_mdinfo_alloc(ill->ill_mdt_capab); 11852 11853 if (mdimp == NULL) { 11854 ip0dbg(("ip_xmit_v6: can't re-enable MDT for " 11855 "connp %p (ENOMEM)\n", (void *)connp)); 11856 } else { 11857 CONN_INC_REF(connp); 11858 squeue_fill(connp->conn_sqp, mdimp, tcp_input, 11859 connp, SQTAG_TCP_INPUT_MCTL); 11860 } 11861 } 11862 11863 do { 11864 boolean_t qos_done = B_FALSE; 11865 11866 if (multirt_send) { 11867 irb_t *irb; 11868 /* 11869 * We are in a multiple send case, need to get 11870 * the next ire and make a duplicate of the 11871 * packet. ire1 holds here the next ire to 11872 * process in the bucket. If multirouting is 11873 * expected, any non-RTF_MULTIRT ire that has 11874 * the right destination address is ignored. 11875 */ 11876 irb = ire->ire_bucket; 11877 ASSERT(irb != NULL); 11878 11879 IRB_REFHOLD(irb); 11880 for (ire1 = ire->ire_next; 11881 ire1 != NULL; 11882 ire1 = ire1->ire_next) { 11883 if (!(ire1->ire_flags & RTF_MULTIRT)) 11884 continue; 11885 if (!IN6_ARE_ADDR_EQUAL( 11886 &ire1->ire_addr_v6, 11887 &ire->ire_addr_v6)) 11888 continue; 11889 if (ire1->ire_marks & 11890 (IRE_MARK_CONDEMNED| 11891 IRE_MARK_HIDDEN)) 11892 continue; 11893 11894 /* Got one */ 11895 if (ire1 != save_ire) { 11896 IRE_REFHOLD(ire1); 11897 } 11898 break; 11899 } 11900 IRB_REFRELE(irb); 11901 11902 if (ire1 != NULL) { 11903 next_mp = copyb(mp); 11904 if ((next_mp == NULL) || 11905 ((mp->b_cont != NULL) && 11906 ((next_mp->b_cont = 11907 dupmsg(mp->b_cont)) == 11908 NULL))) { 11909 freemsg(next_mp); 11910 next_mp = NULL; 11911 ire_refrele(ire1); 11912 ire1 = NULL; 11913 } 11914 } 11915 11916 /* Last multiroute ire; don't loop anymore. */ 11917 if (ire1 == NULL) { 11918 multirt_send = B_FALSE; 11919 } 11920 } 11921 11922 ill_index = 11923 ((ill_t *)stq->q_ptr)->ill_phyint->phyint_ifindex; 11924 11925 /* 11926 * Check for fastpath, we need to hold nce_lock to 11927 * prevent fastpath update from chaining nce_fp_mp. 11928 */ 11929 mutex_enter(&nce->nce_lock); 11930 if ((mp1 = nce->nce_fp_mp) != NULL) { 11931 uint32_t hlen; 11932 uchar_t *rptr; 11933 11934 /* Initiate IPPF processing */ 11935 if (IP6_OUT_IPP(flags)) { 11936 /* 11937 * We have to release the nce lock since 11938 * IPPF components use 11939 * ill_lookup_on_ifindex(), 11940 * which takes the ill_g_lock and the 11941 * ill_lock locks. 11942 */ 11943 mutex_exit(&nce->nce_lock); 11944 ip_process(IPP_LOCAL_OUT, &mp, 11945 ill_index); 11946 if (mp == NULL) { 11947 BUMP_MIB( 11948 ill->ill_ip6_mib, 11949 ipv6OutDiscards); 11950 if (next_mp != NULL) 11951 freemsg(next_mp); 11952 if (ire != save_ire) { 11953 ire_refrele(ire); 11954 } 11955 return; 11956 } 11957 mutex_enter(&nce->nce_lock); 11958 if ((mp1 = nce->nce_fp_mp) == NULL) { 11959 /* 11960 * Probably disappeared during 11961 * IPQoS processing. 11962 */ 11963 qos_done = B_TRUE; 11964 goto prepend_unitdata; 11965 } 11966 } 11967 hlen = MBLKL(mp1); 11968 rptr = mp->b_rptr - hlen; 11969 /* 11970 * make sure there is room for the fastpath 11971 * datalink header 11972 */ 11973 if (rptr < mp->b_datap->db_base) { 11974 mp1 = copyb(mp1); 11975 if (mp1 == NULL) { 11976 mutex_exit(&nce->nce_lock); 11977 BUMP_MIB(ill->ill_ip6_mib, 11978 ipv6OutDiscards); 11979 freemsg(mp); 11980 if (next_mp != NULL) 11981 freemsg(next_mp); 11982 if (ire != save_ire) { 11983 ire_refrele(ire); 11984 } 11985 return; 11986 } 11987 mp1->b_cont = mp; 11988 11989 /* Get the priority marking, if any */ 11990 mp1->b_band = mp->b_band; 11991 mp = mp1; 11992 } else { 11993 mp->b_rptr = rptr; 11994 /* 11995 * fastpath - pre-pend datalink 11996 * header 11997 */ 11998 bcopy(mp1->b_rptr, rptr, hlen); 11999 } 12000 12001 mutex_exit(&nce->nce_lock); 12002 12003 } else { 12004 prepend_unitdata: 12005 mutex_exit(&nce->nce_lock); 12006 mp1 = nce->nce_res_mp; 12007 if (mp1 == NULL) { 12008 ip1dbg(("ip_xmit_v6: No resolution " 12009 "block ire = %p\n", (void *)ire)); 12010 freemsg(mp); 12011 if (next_mp != NULL) 12012 freemsg(next_mp); 12013 if (ire != save_ire) { 12014 ire_refrele(ire); 12015 } 12016 return; 12017 } 12018 /* 12019 * Prepend the DL_UNITDATA_REQ. 12020 */ 12021 mp1 = copyb(mp1); 12022 if (mp1 == NULL) { 12023 BUMP_MIB(ill->ill_ip6_mib, 12024 ipv6OutDiscards); 12025 freemsg(mp); 12026 if (next_mp != NULL) 12027 freemsg(next_mp); 12028 if (ire != save_ire) { 12029 ire_refrele(ire); 12030 } 12031 return; 12032 } 12033 mp1->b_cont = mp; 12034 mp = mp1; 12035 /* 12036 * Initiate IPPF processing, if it is 12037 * already done, bypass. 12038 */ 12039 if (!qos_done && IP6_OUT_IPP(flags)) { 12040 ip_process(IPP_LOCAL_OUT, &mp, 12041 ill_index); 12042 if (mp == NULL) { 12043 BUMP_MIB(ill->ill_ip6_mib, 12044 ipv6OutDiscards); 12045 if (next_mp != NULL) 12046 freemsg(next_mp); 12047 if (ire != save_ire) { 12048 ire_refrele(ire); 12049 } 12050 return; 12051 } 12052 } 12053 } 12054 12055 /* 12056 * Update ire counters; for save_ire, this has been 12057 * done by the caller. 12058 */ 12059 if (ire != save_ire) { 12060 UPDATE_OB_PKT_COUNT(ire); 12061 ire->ire_last_used_time = lbolt; 12062 } 12063 12064 /* 12065 * Send it down. XXX Do we want to flow control AH/ESP 12066 * packets that carry TCP payloads? We don't flow 12067 * control TCP packets, but we should also not 12068 * flow-control TCP packets that have been protected. 12069 * We don't have an easy way to find out if an AH/ESP 12070 * packet was originally TCP or not currently. 12071 */ 12072 if (io == NULL) { 12073 putnext(stq, mp); 12074 } else { 12075 /* 12076 * Safety Pup says: make sure this is 12077 * going to the right interface! 12078 */ 12079 if (io->ipsec_out_capab_ill_index != 12080 ill_index) { 12081 /* IPsec kstats: bump lose counter */ 12082 freemsg(mp1); 12083 } else { 12084 ipsec_hw_putnext(stq, mp); 12085 } 12086 } 12087 12088 if (nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT)) { 12089 if (ire != save_ire) { 12090 ire_refrele(ire); 12091 } 12092 if (multirt_send) { 12093 ASSERT(ire1 != NULL); 12094 /* 12095 * Proceed with the next RTF_MULTIRT 12096 * ire, also set up the send-to queue 12097 * accordingly. 12098 */ 12099 ire = ire1; 12100 ire1 = NULL; 12101 stq = ire->ire_stq; 12102 nce = ire->ire_nce; 12103 ill = ire_to_ill(ire); 12104 mp = next_mp; 12105 next_mp = NULL; 12106 continue; 12107 } 12108 ASSERT(next_mp == NULL); 12109 ASSERT(ire1 == NULL); 12110 return; 12111 } 12112 12113 ASSERT(nce->nce_state != ND_INCOMPLETE); 12114 12115 /* 12116 * Check for upper layer advice 12117 */ 12118 if (flags & IPV6_REACHABILITY_CONFIRMATION) { 12119 /* 12120 * It should be o.k. to check the state without 12121 * a lock here, at most we lose an advice. 12122 */ 12123 nce->nce_last = TICK_TO_MSEC(lbolt64); 12124 if (nce->nce_state != ND_REACHABLE) { 12125 12126 mutex_enter(&nce->nce_lock); 12127 nce->nce_state = ND_REACHABLE; 12128 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 12129 mutex_exit(&nce->nce_lock); 12130 (void) untimeout(nce->nce_timeout_id); 12131 if (ip_debug > 2) { 12132 /* ip1dbg */ 12133 pr_addr_dbg("ip_xmit_v6: state" 12134 " for %s changed to" 12135 " REACHABLE\n", AF_INET6, 12136 &ire->ire_addr_v6); 12137 } 12138 } 12139 if (ire != save_ire) { 12140 ire_refrele(ire); 12141 } 12142 if (multirt_send) { 12143 ASSERT(ire1 != NULL); 12144 /* 12145 * Proceed with the next RTF_MULTIRT 12146 * ire, also set up the send-to queue 12147 * accordingly. 12148 */ 12149 ire = ire1; 12150 ire1 = NULL; 12151 stq = ire->ire_stq; 12152 nce = ire->ire_nce; 12153 ill = ire_to_ill(ire); 12154 mp = next_mp; 12155 next_mp = NULL; 12156 continue; 12157 } 12158 ASSERT(next_mp == NULL); 12159 ASSERT(ire1 == NULL); 12160 return; 12161 } 12162 12163 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 12164 ip1dbg(("ip_xmit_v6: delta = %" PRId64 12165 " ill_reachable_time = %d \n", delta, 12166 ill->ill_reachable_time)); 12167 if (delta > (uint64_t)ill->ill_reachable_time) { 12168 nce = ire->ire_nce; 12169 mutex_enter(&nce->nce_lock); 12170 switch (nce->nce_state) { 12171 case ND_REACHABLE: 12172 case ND_STALE: 12173 /* 12174 * ND_REACHABLE is identical to 12175 * ND_STALE in this specific case. If 12176 * reachable time has expired for this 12177 * neighbor (delta is greater than 12178 * reachable time), conceptually, the 12179 * neighbor cache is no longer in 12180 * REACHABLE state, but already in 12181 * STALE state. So the correct 12182 * transition here is to ND_DELAY. 12183 */ 12184 nce->nce_state = ND_DELAY; 12185 mutex_exit(&nce->nce_lock); 12186 NDP_RESTART_TIMER(nce, 12187 delay_first_probe_time); 12188 if (ip_debug > 3) { 12189 /* ip2dbg */ 12190 pr_addr_dbg("ip_xmit_v6: state" 12191 " for %s changed to" 12192 " DELAY\n", AF_INET6, 12193 &ire->ire_addr_v6); 12194 } 12195 break; 12196 case ND_DELAY: 12197 case ND_PROBE: 12198 mutex_exit(&nce->nce_lock); 12199 /* Timers have already started */ 12200 break; 12201 case ND_UNREACHABLE: 12202 /* 12203 * ndp timer has detected that this nce 12204 * is unreachable and initiated deleting 12205 * this nce and all its associated IREs. 12206 * This is a race where we found the 12207 * ire before it was deleted and have 12208 * just sent out a packet using this 12209 * unreachable nce. 12210 */ 12211 mutex_exit(&nce->nce_lock); 12212 break; 12213 default: 12214 ASSERT(0); 12215 } 12216 } 12217 12218 if (multirt_send) { 12219 ASSERT(ire1 != NULL); 12220 /* 12221 * Proceed with the next RTF_MULTIRT ire, 12222 * Also set up the send-to queue accordingly. 12223 */ 12224 if (ire != save_ire) { 12225 ire_refrele(ire); 12226 } 12227 ire = ire1; 12228 ire1 = NULL; 12229 stq = ire->ire_stq; 12230 nce = ire->ire_nce; 12231 ill = ire_to_ill(ire); 12232 mp = next_mp; 12233 next_mp = NULL; 12234 } 12235 } while (multirt_send); 12236 /* 12237 * In the multirouting case, release the last ire used for 12238 * emission. save_ire will be released by the caller. 12239 */ 12240 if (ire != save_ire) { 12241 ire_refrele(ire); 12242 } 12243 } else { 12244 /* 12245 * Queue packet if we have an conn to give back pressure. 12246 * We can't queue packets intended for hardware acceleration 12247 * since we've tossed that state already. If the packet is 12248 * being fed back from ire_send_v6, we don't know the 12249 * position in the queue to enqueue the packet and we discard 12250 * the packet. 12251 */ 12252 if (ip_output_queue && (connp != NULL) && (io == NULL) && 12253 (caller != IRE_SEND)) { 12254 if (caller == IP_WSRV) { 12255 connp->conn_did_putbq = 1; 12256 (void) putbq(connp->conn_wq, mp); 12257 conn_drain_insert(connp); 12258 /* 12259 * caller == IP_WSRV implies we are 12260 * the service thread, and the 12261 * queue is already noenabled. 12262 * The check for canput and 12263 * the putbq is not atomic. 12264 * So we need to check again. 12265 */ 12266 if (canput(stq->q_next)) 12267 connp->conn_did_putbq = 0; 12268 } else { 12269 (void) putq(connp->conn_wq, mp); 12270 } 12271 return; 12272 } 12273 BUMP_MIB(ill->ill_ip6_mib, ipv6OutDiscards); 12274 freemsg(mp); 12275 return; 12276 } 12277 } 12278 12279 /* 12280 * pr_addr_dbg function provides the needed buffer space to call 12281 * inet_ntop() function's 3rd argument. This function should be 12282 * used by any kernel routine which wants to save INET6_ADDRSTRLEN 12283 * stack buffer space in it's own stack frame. This function uses 12284 * a buffer from it's own stack and prints the information. 12285 * Example: pr_addr_dbg("func: no route for %s\n ", AF_INET, addr) 12286 * 12287 * Note: This function can call inet_ntop() once. 12288 */ 12289 void 12290 pr_addr_dbg(char *fmt1, int af, const void *addr) 12291 { 12292 char buf[INET6_ADDRSTRLEN]; 12293 12294 if (fmt1 == NULL) { 12295 ip0dbg(("pr_addr_dbg: Wrong arguments\n")); 12296 return; 12297 } 12298 12299 /* 12300 * This does not compare debug level and just prints 12301 * out. Thus it is the responsibility of the caller 12302 * to check the appropriate debug-level before calling 12303 * this function. 12304 */ 12305 if (ip_debug > 0) { 12306 printf(fmt1, inet_ntop(af, addr, buf, sizeof (buf))); 12307 } 12308 12309 12310 } 12311 12312 12313 /* 12314 * Return the length in bytes of the IPv6 headers (base header, ip6i_t 12315 * if needed and extension headers) that will be needed based on the 12316 * ip6_pkt_t structure passed by the caller. 12317 * 12318 * The returned length does not include the length of the upper level 12319 * protocol (ULP) header. 12320 */ 12321 int 12322 ip_total_hdrs_len_v6(ip6_pkt_t *ipp) 12323 { 12324 int len; 12325 12326 len = IPV6_HDR_LEN; 12327 if (ipp->ipp_fields & IPPF_HAS_IP6I) 12328 len += sizeof (ip6i_t); 12329 if (ipp->ipp_fields & IPPF_HOPOPTS) { 12330 ASSERT(ipp->ipp_hopoptslen != 0); 12331 len += ipp->ipp_hopoptslen; 12332 } 12333 if (ipp->ipp_fields & IPPF_RTHDR) { 12334 ASSERT(ipp->ipp_rthdrlen != 0); 12335 len += ipp->ipp_rthdrlen; 12336 } 12337 /* 12338 * En-route destination options 12339 * Only do them if there's a routing header as well 12340 */ 12341 if ((ipp->ipp_fields & (IPPF_RTDSTOPTS|IPPF_RTHDR)) == 12342 (IPPF_RTDSTOPTS|IPPF_RTHDR)) { 12343 ASSERT(ipp->ipp_rtdstoptslen != 0); 12344 len += ipp->ipp_rtdstoptslen; 12345 } 12346 if (ipp->ipp_fields & IPPF_DSTOPTS) { 12347 ASSERT(ipp->ipp_dstoptslen != 0); 12348 len += ipp->ipp_dstoptslen; 12349 } 12350 return (len); 12351 } 12352 12353 /* 12354 * All-purpose routine to build a header chain of an IPv6 header 12355 * followed by any required extension headers and a proto header, 12356 * preceeded (where necessary) by an ip6i_t private header. 12357 * 12358 * The fields of the IPv6 header that are derived from the ip6_pkt_t 12359 * will be filled in appropriately. 12360 * Thus the caller must fill in the rest of the IPv6 header, such as 12361 * traffic class/flowid, source address (if not set here), hoplimit (if not 12362 * set here) and destination address. 12363 * 12364 * The extension headers and ip6i_t header will all be fully filled in. 12365 */ 12366 void 12367 ip_build_hdrs_v6(uchar_t *ext_hdrs, uint_t ext_hdrs_len, 12368 ip6_pkt_t *ipp, uint8_t protocol) 12369 { 12370 uint8_t *nxthdr_ptr; 12371 uint8_t *cp; 12372 ip6i_t *ip6i; 12373 ip6_t *ip6h = (ip6_t *)ext_hdrs; 12374 12375 /* 12376 * If sending private ip6i_t header down (checksum info, nexthop, 12377 * or ifindex), adjust ip header pointer and set ip6i_t header pointer, 12378 * then fill it in. (The checksum info will be filled in by icmp). 12379 */ 12380 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 12381 ip6i = (ip6i_t *)ip6h; 12382 ip6h = (ip6_t *)&ip6i[1]; 12383 12384 ip6i->ip6i_flags = 0; 12385 ip6i->ip6i_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 12386 if (ipp->ipp_fields & IPPF_IFINDEX || 12387 ipp->ipp_fields & IPPF_SCOPE_ID) { 12388 ASSERT(ipp->ipp_ifindex != 0); 12389 ip6i->ip6i_flags |= IP6I_IFINDEX; 12390 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 12391 } 12392 if (ipp->ipp_fields & IPPF_ADDR) { 12393 /* 12394 * Enable per-packet source address verification if 12395 * IPV6_PKTINFO specified the source address. 12396 * ip6_src is set in the transport's _wput function. 12397 */ 12398 ASSERT(!IN6_IS_ADDR_UNSPECIFIED( 12399 &ipp->ipp_addr)); 12400 ip6i->ip6i_flags |= IP6I_VERIFY_SRC; 12401 } 12402 if (ipp->ipp_fields & IPPF_UNICAST_HOPS) { 12403 ip6h->ip6_hops = ipp->ipp_unicast_hops; 12404 /* 12405 * We need to set this flag so that IP doesn't 12406 * rewrite the IPv6 header's hoplimit with the 12407 * current default value. 12408 */ 12409 ip6i->ip6i_flags |= IP6I_HOPLIMIT; 12410 } 12411 if (ipp->ipp_fields & IPPF_NEXTHOP) { 12412 ASSERT(!IN6_IS_ADDR_UNSPECIFIED( 12413 &ipp->ipp_nexthop)); 12414 ip6i->ip6i_flags |= IP6I_NEXTHOP; 12415 ip6i->ip6i_nexthop = ipp->ipp_nexthop; 12416 } 12417 /* 12418 * tell IP this is an ip6i_t private header 12419 */ 12420 ip6i->ip6i_nxt = IPPROTO_RAW; 12421 } 12422 /* Initialize IPv6 header */ 12423 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 12424 if (ipp->ipp_fields & IPPF_TCLASS) { 12425 ip6h->ip6_vcf = (ip6h->ip6_vcf & ~IPV6_FLOWINFO_TCLASS) | 12426 (ipp->ipp_tclass << 20); 12427 } 12428 if (ipp->ipp_fields & IPPF_ADDR) 12429 ip6h->ip6_src = ipp->ipp_addr; 12430 12431 nxthdr_ptr = (uint8_t *)&ip6h->ip6_nxt; 12432 cp = (uint8_t *)&ip6h[1]; 12433 /* 12434 * Here's where we have to start stringing together 12435 * any extension headers in the right order: 12436 * Hop-by-hop, destination, routing, and final destination opts. 12437 */ 12438 if (ipp->ipp_fields & IPPF_HOPOPTS) { 12439 /* Hop-by-hop options */ 12440 ip6_hbh_t *hbh = (ip6_hbh_t *)cp; 12441 12442 *nxthdr_ptr = IPPROTO_HOPOPTS; 12443 nxthdr_ptr = &hbh->ip6h_nxt; 12444 12445 bcopy(ipp->ipp_hopopts, cp, ipp->ipp_hopoptslen); 12446 cp += ipp->ipp_hopoptslen; 12447 } 12448 /* 12449 * En-route destination options 12450 * Only do them if there's a routing header as well 12451 */ 12452 if ((ipp->ipp_fields & (IPPF_RTDSTOPTS|IPPF_RTHDR)) == 12453 (IPPF_RTDSTOPTS|IPPF_RTHDR)) { 12454 ip6_dest_t *dst = (ip6_dest_t *)cp; 12455 12456 *nxthdr_ptr = IPPROTO_DSTOPTS; 12457 nxthdr_ptr = &dst->ip6d_nxt; 12458 12459 bcopy(ipp->ipp_rtdstopts, cp, ipp->ipp_rtdstoptslen); 12460 cp += ipp->ipp_rtdstoptslen; 12461 } 12462 /* 12463 * Routing header next 12464 */ 12465 if (ipp->ipp_fields & IPPF_RTHDR) { 12466 ip6_rthdr_t *rt = (ip6_rthdr_t *)cp; 12467 12468 *nxthdr_ptr = IPPROTO_ROUTING; 12469 nxthdr_ptr = &rt->ip6r_nxt; 12470 12471 bcopy(ipp->ipp_rthdr, cp, ipp->ipp_rthdrlen); 12472 cp += ipp->ipp_rthdrlen; 12473 } 12474 /* 12475 * Do ultimate destination options 12476 */ 12477 if (ipp->ipp_fields & IPPF_DSTOPTS) { 12478 ip6_dest_t *dest = (ip6_dest_t *)cp; 12479 12480 *nxthdr_ptr = IPPROTO_DSTOPTS; 12481 nxthdr_ptr = &dest->ip6d_nxt; 12482 12483 bcopy(ipp->ipp_dstopts, cp, ipp->ipp_dstoptslen); 12484 cp += ipp->ipp_dstoptslen; 12485 } 12486 /* 12487 * Now set the last header pointer to the proto passed in 12488 */ 12489 *nxthdr_ptr = protocol; 12490 ASSERT((int)(cp - ext_hdrs) == ext_hdrs_len); 12491 } 12492 12493 /* 12494 * Return a pointer to the routing header extension header 12495 * in the IPv6 header(s) chain passed in. 12496 * If none found, return NULL 12497 * Assumes that all extension headers are in same mblk as the v6 header 12498 */ 12499 ip6_rthdr_t * 12500 ip_find_rthdr_v6(ip6_t *ip6h, uint8_t *endptr) 12501 { 12502 ip6_dest_t *desthdr; 12503 ip6_frag_t *fraghdr; 12504 uint_t hdrlen; 12505 uint8_t nexthdr; 12506 uint8_t *ptr = (uint8_t *)&ip6h[1]; 12507 12508 if (ip6h->ip6_nxt == IPPROTO_ROUTING) 12509 return ((ip6_rthdr_t *)ptr); 12510 12511 /* 12512 * The routing header will precede all extension headers 12513 * other than the hop-by-hop and destination options 12514 * extension headers, so if we see anything other than those, 12515 * we're done and didn't find it. 12516 * We could see a destination options header alone but no 12517 * routing header, in which case we'll return NULL as soon as 12518 * we see anything after that. 12519 * Hop-by-hop and destination option headers are identical, 12520 * so we can use either one we want as a template. 12521 */ 12522 nexthdr = ip6h->ip6_nxt; 12523 while (ptr < endptr) { 12524 /* Is there enough left for len + nexthdr? */ 12525 if (ptr + MIN_EHDR_LEN > endptr) 12526 return (NULL); 12527 12528 switch (nexthdr) { 12529 case IPPROTO_HOPOPTS: 12530 case IPPROTO_DSTOPTS: 12531 /* Assumes the headers are identical for hbh and dst */ 12532 desthdr = (ip6_dest_t *)ptr; 12533 hdrlen = 8 * (desthdr->ip6d_len + 1); 12534 nexthdr = desthdr->ip6d_nxt; 12535 break; 12536 12537 case IPPROTO_ROUTING: 12538 return ((ip6_rthdr_t *)ptr); 12539 12540 case IPPROTO_FRAGMENT: 12541 fraghdr = (ip6_frag_t *)ptr; 12542 hdrlen = sizeof (ip6_frag_t); 12543 nexthdr = fraghdr->ip6f_nxt; 12544 break; 12545 12546 default: 12547 return (NULL); 12548 } 12549 ptr += hdrlen; 12550 } 12551 return (NULL); 12552 } 12553 12554 /* 12555 * Called for source-routed packets originating on this node. 12556 * Manipulates the original routing header by moving every entry up 12557 * one slot, placing the first entry in the v6 header's v6_dst field, 12558 * and placing the ultimate destination in the routing header's last 12559 * slot. 12560 * 12561 * Returns the checksum diference between the ultimate destination 12562 * (last hop in the routing header when the packet is sent) and 12563 * the first hop (ip6_dst when the packet is sent) 12564 */ 12565 uint32_t 12566 ip_massage_options_v6(ip6_t *ip6h, ip6_rthdr_t *rth) 12567 { 12568 uint_t numaddr; 12569 uint_t i; 12570 in6_addr_t *addrptr; 12571 in6_addr_t tmp; 12572 ip6_rthdr0_t *rthdr = (ip6_rthdr0_t *)rth; 12573 uint32_t cksm; 12574 uint32_t addrsum = 0; 12575 uint16_t *ptr; 12576 12577 /* 12578 * Perform any processing needed for source routing. 12579 * We know that all extension headers will be in the same mblk 12580 * as the IPv6 header. 12581 */ 12582 12583 /* 12584 * If no segments left in header, or the header length field is zero, 12585 * don't move hop addresses around; 12586 * Checksum difference is zero. 12587 */ 12588 if ((rthdr->ip6r0_segleft == 0) || (rthdr->ip6r0_len == 0)) 12589 return (0); 12590 12591 ptr = (uint16_t *)&ip6h->ip6_dst; 12592 cksm = 0; 12593 for (i = 0; i < (sizeof (in6_addr_t) / sizeof (uint16_t)); i++) { 12594 cksm += ptr[i]; 12595 } 12596 cksm = (cksm & 0xFFFF) + (cksm >> 16); 12597 12598 /* 12599 * Here's where the fun begins - we have to 12600 * move all addresses up one spot, take the 12601 * first hop and make it our first ip6_dst, 12602 * and place the ultimate destination in the 12603 * newly-opened last slot. 12604 */ 12605 addrptr = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr)); 12606 numaddr = rthdr->ip6r0_len / 2; 12607 tmp = *addrptr; 12608 for (i = 0; i < (numaddr - 1); addrptr++, i++) { 12609 *addrptr = addrptr[1]; 12610 } 12611 *addrptr = ip6h->ip6_dst; 12612 ip6h->ip6_dst = tmp; 12613 12614 /* 12615 * From the checksummed ultimate destination subtract the checksummed 12616 * current ip6_dst (the first hop address). Return that number. 12617 * (In the v4 case, the second part of this is done in each routine 12618 * that calls ip_massage_options(). We do it all in this one place 12619 * for v6). 12620 */ 12621 ptr = (uint16_t *)&ip6h->ip6_dst; 12622 for (i = 0; i < (sizeof (in6_addr_t) / sizeof (uint16_t)); i++) { 12623 addrsum += ptr[i]; 12624 } 12625 cksm -= ((addrsum >> 16) + (addrsum & 0xFFFF)); 12626 if ((int)cksm < 0) 12627 cksm--; 12628 cksm = (cksm & 0xFFFF) + (cksm >> 16); 12629 12630 return (cksm); 12631 } 12632 12633 /* 12634 * See if the upper-level protocol indicated by 'proto' will be able 12635 * to do something with an ICMP_FRAGMENTATION_NEEDED (IPv4) or 12636 * ICMP6_PACKET_TOO_BIG (IPv6). 12637 */ 12638 static boolean_t 12639 ip_ulp_cando_pkt2big(int proto) 12640 { 12641 /* 12642 * For now, only TCP can handle this. 12643 * Tunnels may be able to also, but since tun isn't working over 12644 * IPv6 yet, don't worry about it for now. 12645 */ 12646 return (proto == IPPROTO_TCP); 12647 } 12648 12649 12650 /* 12651 * Propagate a multicast group membership operation (join/leave) (*fn) on 12652 * all interfaces crossed by the related multirt routes. 12653 * The call is considered successful if the operation succeeds 12654 * on at least one interface. 12655 * The function is called if the destination address in the packet to send 12656 * is multirouted. 12657 */ 12658 int 12659 ip_multirt_apply_membership_v6(int (*fn)(conn_t *, boolean_t, 12660 const in6_addr_t *, int, mcast_record_t, const in6_addr_t *, mblk_t *), 12661 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6grp, 12662 mcast_record_t fmode, const in6_addr_t *v6src, mblk_t *first_mp) 12663 { 12664 ire_t *ire_gw; 12665 irb_t *irb; 12666 int index, error = 0; 12667 opt_restart_t *or; 12668 12669 irb = ire->ire_bucket; 12670 ASSERT(irb != NULL); 12671 12672 ASSERT(DB_TYPE(first_mp) == M_CTL); 12673 or = (opt_restart_t *)first_mp->b_rptr; 12674 12675 IRB_REFHOLD(irb); 12676 for (; ire != NULL; ire = ire->ire_next) { 12677 if ((ire->ire_flags & RTF_MULTIRT) == 0) 12678 continue; 12679 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6grp)) 12680 continue; 12681 12682 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6, 0, 0, 12683 IRE_INTERFACE, NULL, NULL, ALL_ZONES, 0, NULL, 12684 MATCH_IRE_RECURSIVE | MATCH_IRE_TYPE); 12685 /* No resolver exists for the gateway; skip this ire. */ 12686 if (ire_gw == NULL) 12687 continue; 12688 index = ire_gw->ire_ipif->ipif_ill->ill_phyint->phyint_ifindex; 12689 /* 12690 * A resolver exists: we can get the interface on which we have 12691 * to apply the operation. 12692 */ 12693 error = fn(connp, checkonly, v6grp, index, fmode, v6src, 12694 first_mp); 12695 if (error == 0) 12696 or->or_private = CGTP_MCAST_SUCCESS; 12697 12698 if (ip_debug > 0) { 12699 ulong_t off; 12700 char *ksym; 12701 12702 ksym = kobj_getsymname((uintptr_t)fn, &off); 12703 ip2dbg(("ip_multirt_apply_membership_v6: " 12704 "called %s, multirt group 0x%08x via itf 0x%08x, " 12705 "error %d [success %u]\n", 12706 ksym ? ksym : "?", 12707 ntohl(V4_PART_OF_V6((*v6grp))), 12708 ntohl(V4_PART_OF_V6(ire_gw->ire_src_addr_v6)), 12709 error, or->or_private)); 12710 } 12711 12712 ire_refrele(ire_gw); 12713 if (error == EINPROGRESS) { 12714 IRB_REFRELE(irb); 12715 return (error); 12716 } 12717 } 12718 IRB_REFRELE(irb); 12719 /* 12720 * Consider the call as successful if we succeeded on at least 12721 * one interface. Otherwise, return the last encountered error. 12722 */ 12723 return (or->or_private == CGTP_MCAST_SUCCESS ? 0 : error); 12724 } 12725 12726 void 12727 ip6_kstat_init(void) 12728 { 12729 if ((ip6_kstat = kstat_create("ip", 0, "ip6stat", 12730 "net", KSTAT_TYPE_NAMED, 12731 sizeof (ip6_statistics) / sizeof (kstat_named_t), 12732 KSTAT_FLAG_VIRTUAL)) != NULL) { 12733 ip6_kstat->ks_data = &ip6_statistics; 12734 kstat_install(ip6_kstat); 12735 } 12736 } 12737 12738 /* 12739 * The following two functions set and get the value for the 12740 * IPV6_SRC_PREFERENCES socket option. 12741 */ 12742 int 12743 ip6_set_src_preferences(conn_t *connp, uint32_t prefs) 12744 { 12745 /* 12746 * We only support preferences that are covered by 12747 * IPV6_PREFER_SRC_MASK. 12748 */ 12749 if (prefs & ~IPV6_PREFER_SRC_MASK) 12750 return (EINVAL); 12751 12752 /* 12753 * Look for conflicting preferences or default preferences. If 12754 * both bits of a related pair are clear, the application wants the 12755 * system's default value for that pair. Both bits in a pair can't 12756 * be set. 12757 */ 12758 if ((prefs & IPV6_PREFER_SRC_MIPMASK) == 0) { 12759 prefs |= IPV6_PREFER_SRC_MIPDEFAULT; 12760 } else if ((prefs & IPV6_PREFER_SRC_MIPMASK) == 12761 IPV6_PREFER_SRC_MIPMASK) { 12762 return (EINVAL); 12763 } 12764 if ((prefs & IPV6_PREFER_SRC_TMPMASK) == 0) { 12765 prefs |= IPV6_PREFER_SRC_TMPDEFAULT; 12766 } else if ((prefs & IPV6_PREFER_SRC_TMPMASK) == 12767 IPV6_PREFER_SRC_TMPMASK) { 12768 return (EINVAL); 12769 } 12770 if ((prefs & IPV6_PREFER_SRC_CGAMASK) == 0) { 12771 prefs |= IPV6_PREFER_SRC_CGADEFAULT; 12772 } else if ((prefs & IPV6_PREFER_SRC_CGAMASK) == 12773 IPV6_PREFER_SRC_CGAMASK) { 12774 return (EINVAL); 12775 } 12776 12777 connp->conn_src_preferences = prefs; 12778 return (0); 12779 } 12780 12781 size_t 12782 ip6_get_src_preferences(conn_t *connp, uint32_t *val) 12783 { 12784 *val = connp->conn_src_preferences; 12785 return (sizeof (connp->conn_src_preferences)); 12786 } 12787 12788 int 12789 ip6_set_pktinfo(cred_t *cr, conn_t *connp, struct in6_pktinfo *pkti, mblk_t *mp) 12790 { 12791 ill_t *ill; 12792 ire_t *ire; 12793 int error; 12794 12795 /* 12796 * Verify the source address and ifindex. Privileged users can use 12797 * any source address. For ancillary data the source address is 12798 * checked in ip_wput_v6. 12799 */ 12800 if (pkti->ipi6_ifindex != 0) { 12801 ASSERT(connp != NULL); 12802 ill = ill_lookup_on_ifindex(pkti->ipi6_ifindex, B_TRUE, 12803 CONNP_TO_WQ(connp), mp, ip_restart_optmgmt, &error); 12804 if (ill == NULL) { 12805 /* 12806 * We just want to know if the interface exists, we 12807 * don't really care about the ill pointer itself. 12808 */ 12809 if (error != EINPROGRESS) 12810 return (error); 12811 error = 0; /* Ensure we don't use it below */ 12812 } else { 12813 ill_refrele(ill); 12814 } 12815 } 12816 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr) && 12817 secpolicy_net_rawaccess(cr) != 0) { 12818 ire = ire_route_lookup_v6(&pkti->ipi6_addr, 0, 0, 12819 (IRE_LOCAL|IRE_LOOPBACK), NULL, NULL, 12820 connp->conn_zoneid, NULL, MATCH_IRE_TYPE); 12821 if (ire != NULL) 12822 ire_refrele(ire); 12823 else 12824 return (ENXIO); 12825 } 12826 return (0); 12827 } 12828 12829 /* 12830 * Get the size of the IP options (including the IP headers size) 12831 * without including the AH header's size. If till_ah is B_FALSE, 12832 * and if AH header is present, dest options beyond AH header will 12833 * also be included in the returned size. 12834 */ 12835 int 12836 ipsec_ah_get_hdr_size_v6(mblk_t *mp, boolean_t till_ah) 12837 { 12838 ip6_t *ip6h; 12839 uint8_t nexthdr; 12840 uint8_t *whereptr; 12841 ip6_hbh_t *hbhhdr; 12842 ip6_dest_t *dsthdr; 12843 ip6_rthdr_t *rthdr; 12844 int ehdrlen; 12845 int size; 12846 ah_t *ah; 12847 12848 ip6h = (ip6_t *)mp->b_rptr; 12849 size = IPV6_HDR_LEN; 12850 nexthdr = ip6h->ip6_nxt; 12851 whereptr = (uint8_t *)&ip6h[1]; 12852 for (;;) { 12853 /* Assume IP has already stripped it */ 12854 ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW); 12855 switch (nexthdr) { 12856 case IPPROTO_HOPOPTS: 12857 hbhhdr = (ip6_hbh_t *)whereptr; 12858 nexthdr = hbhhdr->ip6h_nxt; 12859 ehdrlen = 8 * (hbhhdr->ip6h_len + 1); 12860 break; 12861 case IPPROTO_DSTOPTS: 12862 dsthdr = (ip6_dest_t *)whereptr; 12863 nexthdr = dsthdr->ip6d_nxt; 12864 ehdrlen = 8 * (dsthdr->ip6d_len + 1); 12865 break; 12866 case IPPROTO_ROUTING: 12867 rthdr = (ip6_rthdr_t *)whereptr; 12868 nexthdr = rthdr->ip6r_nxt; 12869 ehdrlen = 8 * (rthdr->ip6r_len + 1); 12870 break; 12871 default : 12872 if (till_ah) { 12873 ASSERT(nexthdr == IPPROTO_AH); 12874 return (size); 12875 } 12876 /* 12877 * If we don't have a AH header to traverse, 12878 * return now. This happens normally for 12879 * outbound datagrams where we have not inserted 12880 * the AH header. 12881 */ 12882 if (nexthdr != IPPROTO_AH) { 12883 return (size); 12884 } 12885 12886 /* 12887 * We don't include the AH header's size 12888 * to be symmetrical with other cases where 12889 * we either don't have a AH header (outbound) 12890 * or peek into the AH header yet (inbound and 12891 * not pulled up yet). 12892 */ 12893 ah = (ah_t *)whereptr; 12894 nexthdr = ah->ah_nexthdr; 12895 ehdrlen = (ah->ah_length << 2) + 8; 12896 12897 if (nexthdr == IPPROTO_DSTOPTS) { 12898 if (whereptr + ehdrlen >= mp->b_wptr) { 12899 /* 12900 * The destination options header 12901 * is not part of the first mblk. 12902 */ 12903 whereptr = mp->b_cont->b_rptr; 12904 } else { 12905 whereptr += ehdrlen; 12906 } 12907 12908 dsthdr = (ip6_dest_t *)whereptr; 12909 ehdrlen = 8 * (dsthdr->ip6d_len + 1); 12910 size += ehdrlen; 12911 } 12912 return (size); 12913 } 12914 whereptr += ehdrlen; 12915 size += ehdrlen; 12916 } 12917 } 12918