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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 /* Copyright (c) 1990 Mentat Inc. */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * This file contains the interface control functions for IP. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/stream.h> 35 #include <sys/dlpi.h> 36 #include <sys/stropts.h> 37 #include <sys/strsun.h> 38 #include <sys/sysmacros.h> 39 #include <sys/strlog.h> 40 #include <sys/ddi.h> 41 #include <sys/sunddi.h> 42 #include <sys/cmn_err.h> 43 #include <sys/kstat.h> 44 #include <sys/debug.h> 45 #include <sys/zone.h> 46 #include <sys/sunldi.h> 47 #include <sys/file.h> 48 49 #include <sys/kmem.h> 50 #include <sys/systm.h> 51 #include <sys/param.h> 52 #include <sys/socket.h> 53 #include <sys/isa_defs.h> 54 #include <net/if.h> 55 #include <net/if_arp.h> 56 #include <net/if_types.h> 57 #include <net/if_dl.h> 58 #include <net/route.h> 59 #include <sys/sockio.h> 60 #include <netinet/in.h> 61 #include <netinet/ip6.h> 62 #include <netinet/icmp6.h> 63 #include <netinet/igmp_var.h> 64 #include <sys/strsun.h> 65 #include <sys/policy.h> 66 #include <sys/ethernet.h> 67 68 #include <inet/common.h> /* for various inet/mi.h and inet/nd.h needs */ 69 #include <inet/mi.h> 70 #include <inet/nd.h> 71 #include <inet/arp.h> 72 #include <inet/mib2.h> 73 #include <inet/ip.h> 74 #include <inet/ip6.h> 75 #include <inet/ip6_asp.h> 76 #include <inet/tcp.h> 77 #include <inet/ip_multi.h> 78 #include <inet/ip_ire.h> 79 #include <inet/ip_ftable.h> 80 #include <inet/ip_rts.h> 81 #include <inet/ip_ndp.h> 82 #include <inet/ip_if.h> 83 #include <inet/ip_impl.h> 84 #include <inet/tun.h> 85 #include <inet/sctp_ip.h> 86 #include <inet/ip_netinfo.h> 87 #include <inet/mib2.h> 88 89 #include <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 #include <inet/sadb.h> 92 #include <inet/ipsec_impl.h> 93 #include <sys/iphada.h> 94 95 96 #include <netinet/igmp.h> 97 #include <inet/ip_listutils.h> 98 #include <inet/ipclassifier.h> 99 #include <sys/mac.h> 100 101 #include <sys/systeminfo.h> 102 #include <sys/bootconf.h> 103 104 #include <sys/tsol/tndb.h> 105 #include <sys/tsol/tnet.h> 106 107 /* The character which tells where the ill_name ends */ 108 #define IPIF_SEPARATOR_CHAR ':' 109 110 /* IP ioctl function table entry */ 111 typedef struct ipft_s { 112 int ipft_cmd; 113 pfi_t ipft_pfi; 114 int ipft_min_size; 115 int ipft_flags; 116 } ipft_t; 117 #define IPFT_F_NO_REPLY 0x1 /* IP ioctl does not expect any reply */ 118 #define IPFT_F_SELF_REPLY 0x2 /* ioctl callee does the ioctl reply */ 119 120 typedef struct ip_sock_ar_s { 121 union { 122 area_t ip_sock_area; 123 ared_t ip_sock_ared; 124 areq_t ip_sock_areq; 125 } ip_sock_ar_u; 126 queue_t *ip_sock_ar_q; 127 } ip_sock_ar_t; 128 129 static int nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *); 130 static int nd_ill_forward_set(queue_t *q, mblk_t *mp, 131 char *value, caddr_t cp, cred_t *ioc_cr); 132 133 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask); 134 static ip_m_t *ip_m_lookup(t_uscalar_t mac_type); 135 static int ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, 136 mblk_t *mp, boolean_t need_up); 137 static int ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, 138 mblk_t *mp, boolean_t need_up); 139 static int ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid, 140 queue_t *q, mblk_t *mp, boolean_t need_up); 141 static int ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, 142 mblk_t *mp, boolean_t need_up); 143 static int ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, 144 mblk_t *mp); 145 static int ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t, 146 queue_t *q, mblk_t *mp, boolean_t need_up); 147 static int ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, 148 sin_t *sin, boolean_t x_arp_ioctl, boolean_t if_arp_ioctl); 149 static ipaddr_t ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *); 150 static void ip_wput_ioctl(queue_t *q, mblk_t *mp); 151 static void ipsq_flush(ill_t *ill); 152 153 static int ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, 154 queue_t *q, mblk_t *mp, boolean_t need_up); 155 static void ipsq_delete(ipsq_t *); 156 157 static ipif_t *ipif_allocate(ill_t *ill, int id, uint_t ire_type, 158 boolean_t initialize); 159 static void ipif_check_bcast_ires(ipif_t *test_ipif); 160 static void ipif_down_delete_ire(ire_t *ire, char *ipif); 161 static void ipif_delete_cache_ire(ire_t *, char *); 162 static int ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp); 163 static void ipif_free(ipif_t *ipif); 164 static void ipif_free_tail(ipif_t *ipif); 165 static void ipif_mtu_change(ire_t *ire, char *ipif_arg); 166 static void ipif_multicast_down(ipif_t *ipif); 167 static void ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif); 168 static void ipif_set_default(ipif_t *ipif); 169 static int ipif_set_values(queue_t *q, mblk_t *mp, 170 char *interf_name, uint_t *ppa); 171 static int ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, 172 queue_t *q); 173 static ipif_t *ipif_lookup_on_name(char *name, size_t namelen, 174 boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid, 175 queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *); 176 static int ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp); 177 static void ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp); 178 179 static int ill_alloc_ppa(ill_if_t *, ill_t *); 180 static int ill_arp_off(ill_t *ill); 181 static int ill_arp_on(ill_t *ill); 182 static void ill_delete_interface_type(ill_if_t *); 183 static int ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q); 184 static void ill_dl_down(ill_t *ill); 185 static void ill_down(ill_t *ill); 186 static void ill_downi(ire_t *ire, char *ill_arg); 187 static void ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg); 188 static void ill_down_tail(ill_t *ill); 189 static void ill_free_mib(ill_t *ill); 190 static void ill_glist_delete(ill_t *); 191 static boolean_t ill_has_usable_ipif(ill_t *); 192 static int ill_lock_ipsq_ills(ipsq_t *sq, ill_t **list, int); 193 static void ill_nominate_bcast_rcv(ill_group_t *illgrp); 194 static void ill_phyint_free(ill_t *ill); 195 static void ill_phyint_reinit(ill_t *ill); 196 static void ill_set_nce_router_flags(ill_t *, boolean_t); 197 static void ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *); 198 static void ill_signal_ipsq_ills(ipsq_t *, boolean_t); 199 static boolean_t ill_split_ipsq(ipsq_t *cur_sq); 200 static void ill_stq_cache_delete(ire_t *, char *); 201 202 static boolean_t ip_ether_v6intfid(uint_t, uint8_t *, in6_addr_t *); 203 static boolean_t ip_nodef_v6intfid(uint_t, uint8_t *, in6_addr_t *); 204 static boolean_t ip_ether_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *, 205 in6_addr_t *); 206 static boolean_t ip_ether_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *, 207 ipaddr_t *); 208 static boolean_t ip_ib_v6intfid(uint_t, uint8_t *, in6_addr_t *); 209 static boolean_t ip_ib_v6mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *, 210 in6_addr_t *); 211 static boolean_t ip_ib_v4mapinfo(uint_t, uint8_t *, uint8_t *, uint32_t *, 212 ipaddr_t *); 213 214 static void ipif_save_ire(ipif_t *, ire_t *); 215 static void ipif_remove_ire(ipif_t *, ire_t *); 216 static void ip_cgtp_bcast_add(ire_t *, ire_t *, ip_stack_t *); 217 static void ip_cgtp_bcast_delete(ire_t *, ip_stack_t *); 218 219 /* 220 * Per-ill IPsec capabilities management. 221 */ 222 static ill_ipsec_capab_t *ill_ipsec_capab_alloc(void); 223 static void ill_ipsec_capab_free(ill_ipsec_capab_t *); 224 static void ill_ipsec_capab_add(ill_t *, uint_t, boolean_t); 225 static void ill_ipsec_capab_delete(ill_t *, uint_t); 226 static boolean_t ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *, int); 227 static void ill_capability_proto(ill_t *, int, mblk_t *); 228 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *, 229 boolean_t); 230 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *); 231 static void ill_capability_mdt_ack(ill_t *, mblk_t *, dl_capability_sub_t *); 232 static void ill_capability_mdt_reset(ill_t *, mblk_t **); 233 static void ill_capability_ipsec_ack(ill_t *, mblk_t *, dl_capability_sub_t *); 234 static void ill_capability_ipsec_reset(ill_t *, mblk_t **); 235 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *); 236 static void ill_capability_hcksum_reset(ill_t *, mblk_t **); 237 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *, 238 dl_capability_sub_t *); 239 static void ill_capability_zerocopy_reset(ill_t *, mblk_t **); 240 static void ill_capability_lso_ack(ill_t *, mblk_t *, dl_capability_sub_t *); 241 static void ill_capability_lso_reset(ill_t *, mblk_t **); 242 static void ill_capability_dls_ack(ill_t *, mblk_t *, dl_capability_sub_t *); 243 static mac_resource_handle_t ill_ring_add(void *, mac_resource_t *); 244 static void ill_capability_dls_reset(ill_t *, mblk_t **); 245 static void ill_capability_dls_disable(ill_t *); 246 247 static void illgrp_cache_delete(ire_t *, char *); 248 static void illgrp_delete(ill_t *ill); 249 static void illgrp_reset_schednext(ill_t *ill); 250 251 static ill_t *ill_prev_usesrc(ill_t *); 252 static int ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t); 253 static void ill_disband_usesrc_group(ill_t *); 254 255 static void conn_cleanup_stale_ire(conn_t *, caddr_t); 256 257 /* 258 * if we go over the memory footprint limit more than once in this msec 259 * interval, we'll start pruning aggressively. 260 */ 261 int ip_min_frag_prune_time = 0; 262 263 /* 264 * max # of IPsec algorithms supported. Limited to 1 byte by PF_KEY 265 * and the IPsec DOI 266 */ 267 #define MAX_IPSEC_ALGS 256 268 269 #define BITSPERBYTE 8 270 #define BITS(type) (BITSPERBYTE * (long)sizeof (type)) 271 272 #define IPSEC_ALG_ENABLE(algs, algid) \ 273 ((algs)[(algid) / BITS(ipsec_capab_elem_t)] |= \ 274 (1 << ((algid) % BITS(ipsec_capab_elem_t)))) 275 276 #define IPSEC_ALG_IS_ENABLED(algid, algs) \ 277 ((algs)[(algid) / BITS(ipsec_capab_elem_t)] & \ 278 (1 << ((algid) % BITS(ipsec_capab_elem_t)))) 279 280 typedef uint8_t ipsec_capab_elem_t; 281 282 /* 283 * Per-algorithm parameters. Note that at present, only encryption 284 * algorithms have variable keysize (IKE does not provide a way to negotiate 285 * auth algorithm keysize). 286 * 287 * All sizes here are in bits. 288 */ 289 typedef struct 290 { 291 uint16_t minkeylen; 292 uint16_t maxkeylen; 293 } ipsec_capab_algparm_t; 294 295 /* 296 * Per-ill capabilities. 297 */ 298 struct ill_ipsec_capab_s { 299 ipsec_capab_elem_t *encr_hw_algs; 300 ipsec_capab_elem_t *auth_hw_algs; 301 uint32_t algs_size; /* size of _hw_algs in bytes */ 302 /* algorithm key lengths */ 303 ipsec_capab_algparm_t *encr_algparm; 304 uint32_t encr_algparm_size; 305 uint32_t encr_algparm_end; 306 }; 307 308 /* 309 * The field values are larger than strictly necessary for simple 310 * AR_ENTRY_ADDs but the padding lets us accomodate the socket ioctls. 311 */ 312 static area_t ip_area_template = { 313 AR_ENTRY_ADD, /* area_cmd */ 314 sizeof (ip_sock_ar_t) + (IP_ADDR_LEN*2) + sizeof (struct sockaddr_dl), 315 /* area_name_offset */ 316 /* area_name_length temporarily holds this structure length */ 317 sizeof (area_t), /* area_name_length */ 318 IP_ARP_PROTO_TYPE, /* area_proto */ 319 sizeof (ip_sock_ar_t), /* area_proto_addr_offset */ 320 IP_ADDR_LEN, /* area_proto_addr_length */ 321 sizeof (ip_sock_ar_t) + IP_ADDR_LEN, 322 /* area_proto_mask_offset */ 323 0, /* area_flags */ 324 sizeof (ip_sock_ar_t) + IP_ADDR_LEN + IP_ADDR_LEN, 325 /* area_hw_addr_offset */ 326 /* Zero length hw_addr_length means 'use your idea of the address' */ 327 0 /* area_hw_addr_length */ 328 }; 329 330 /* 331 * AR_ENTRY_ADD/DELETE templates have been added for IPv6 external resolver 332 * support 333 */ 334 static area_t ip6_area_template = { 335 AR_ENTRY_ADD, /* area_cmd */ 336 sizeof (ip_sock_ar_t) + (IPV6_ADDR_LEN*2) + sizeof (sin6_t), 337 /* area_name_offset */ 338 /* area_name_length temporarily holds this structure length */ 339 sizeof (area_t), /* area_name_length */ 340 IP_ARP_PROTO_TYPE, /* area_proto */ 341 sizeof (ip_sock_ar_t), /* area_proto_addr_offset */ 342 IPV6_ADDR_LEN, /* area_proto_addr_length */ 343 sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN, 344 /* area_proto_mask_offset */ 345 0, /* area_flags */ 346 sizeof (ip_sock_ar_t) + IPV6_ADDR_LEN + IPV6_ADDR_LEN, 347 /* area_hw_addr_offset */ 348 /* Zero length hw_addr_length means 'use your idea of the address' */ 349 0 /* area_hw_addr_length */ 350 }; 351 352 static ared_t ip_ared_template = { 353 AR_ENTRY_DELETE, 354 sizeof (ared_t) + IP_ADDR_LEN, 355 sizeof (ared_t), 356 IP_ARP_PROTO_TYPE, 357 sizeof (ared_t), 358 IP_ADDR_LEN 359 }; 360 361 static ared_t ip6_ared_template = { 362 AR_ENTRY_DELETE, 363 sizeof (ared_t) + IPV6_ADDR_LEN, 364 sizeof (ared_t), 365 IP_ARP_PROTO_TYPE, 366 sizeof (ared_t), 367 IPV6_ADDR_LEN 368 }; 369 370 /* 371 * A template for an IPv6 AR_ENTRY_QUERY template has not been created, as 372 * as the areq doesn't include an IP address in ill_dl_up() (the only place a 373 * areq is used). 374 */ 375 static areq_t ip_areq_template = { 376 AR_ENTRY_QUERY, /* cmd */ 377 sizeof (areq_t)+(2*IP_ADDR_LEN), /* name offset */ 378 sizeof (areq_t), /* name len (filled by ill_arp_alloc) */ 379 IP_ARP_PROTO_TYPE, /* protocol, from arps perspective */ 380 sizeof (areq_t), /* target addr offset */ 381 IP_ADDR_LEN, /* target addr_length */ 382 0, /* flags */ 383 sizeof (areq_t) + IP_ADDR_LEN, /* sender addr offset */ 384 IP_ADDR_LEN, /* sender addr length */ 385 AR_EQ_DEFAULT_XMIT_COUNT, /* xmit_count */ 386 AR_EQ_DEFAULT_XMIT_INTERVAL, /* (re)xmit_interval in milliseconds */ 387 AR_EQ_DEFAULT_MAX_BUFFERED /* max # of requests to buffer */ 388 /* anything else filled in by the code */ 389 }; 390 391 static arc_t ip_aru_template = { 392 AR_INTERFACE_UP, 393 sizeof (arc_t), /* Name offset */ 394 sizeof (arc_t) /* Name length (set by ill_arp_alloc) */ 395 }; 396 397 static arc_t ip_ard_template = { 398 AR_INTERFACE_DOWN, 399 sizeof (arc_t), /* Name offset */ 400 sizeof (arc_t) /* Name length (set by ill_arp_alloc) */ 401 }; 402 403 static arc_t ip_aron_template = { 404 AR_INTERFACE_ON, 405 sizeof (arc_t), /* Name offset */ 406 sizeof (arc_t) /* Name length (set by ill_arp_alloc) */ 407 }; 408 409 static arc_t ip_aroff_template = { 410 AR_INTERFACE_OFF, 411 sizeof (arc_t), /* Name offset */ 412 sizeof (arc_t) /* Name length (set by ill_arp_alloc) */ 413 }; 414 415 416 static arma_t ip_arma_multi_template = { 417 AR_MAPPING_ADD, 418 sizeof (arma_t) + 3*IP_ADDR_LEN + IP_MAX_HW_LEN, 419 /* Name offset */ 420 sizeof (arma_t), /* Name length (set by ill_arp_alloc) */ 421 IP_ARP_PROTO_TYPE, 422 sizeof (arma_t), /* proto_addr_offset */ 423 IP_ADDR_LEN, /* proto_addr_length */ 424 sizeof (arma_t) + IP_ADDR_LEN, /* proto_mask_offset */ 425 sizeof (arma_t) + 2*IP_ADDR_LEN, /* proto_extract_mask_offset */ 426 ACE_F_PERMANENT | ACE_F_MAPPING, /* flags */ 427 sizeof (arma_t) + 3*IP_ADDR_LEN, /* hw_addr_offset */ 428 IP_MAX_HW_LEN, /* hw_addr_length */ 429 0, /* hw_mapping_start */ 430 }; 431 432 static ipft_t ip_ioctl_ftbl[] = { 433 { IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 }, 434 { IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t), 435 IPFT_F_NO_REPLY }, 436 { IP_IOC_IRE_ADVISE_NO_REPLY, ip_ire_advise, sizeof (ipic_t), 437 IPFT_F_NO_REPLY }, 438 { IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY }, 439 { 0 } 440 }; 441 442 /* Simple ICMP IP Header Template */ 443 static ipha_t icmp_ipha = { 444 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP 445 }; 446 447 /* Flag descriptors for ip_ipif_report */ 448 static nv_t ipif_nv_tbl[] = { 449 { IPIF_UP, "UP" }, 450 { IPIF_BROADCAST, "BROADCAST" }, 451 { ILLF_DEBUG, "DEBUG" }, 452 { PHYI_LOOPBACK, "LOOPBACK" }, 453 { IPIF_POINTOPOINT, "POINTOPOINT" }, 454 { ILLF_NOTRAILERS, "NOTRAILERS" }, 455 { PHYI_RUNNING, "RUNNING" }, 456 { ILLF_NOARP, "NOARP" }, 457 { PHYI_PROMISC, "PROMISC" }, 458 { PHYI_ALLMULTI, "ALLMULTI" }, 459 { PHYI_INTELLIGENT, "INTELLIGENT" }, 460 { ILLF_MULTICAST, "MULTICAST" }, 461 { PHYI_MULTI_BCAST, "MULTI_BCAST" }, 462 { IPIF_UNNUMBERED, "UNNUMBERED" }, 463 { IPIF_DHCPRUNNING, "DHCP" }, 464 { IPIF_PRIVATE, "PRIVATE" }, 465 { IPIF_NOXMIT, "NOXMIT" }, 466 { IPIF_NOLOCAL, "NOLOCAL" }, 467 { IPIF_DEPRECATED, "DEPRECATED" }, 468 { IPIF_PREFERRED, "PREFERRED" }, 469 { IPIF_TEMPORARY, "TEMPORARY" }, 470 { IPIF_ADDRCONF, "ADDRCONF" }, 471 { PHYI_VIRTUAL, "VIRTUAL" }, 472 { ILLF_ROUTER, "ROUTER" }, 473 { ILLF_NONUD, "NONUD" }, 474 { IPIF_ANYCAST, "ANYCAST" }, 475 { ILLF_NORTEXCH, "NORTEXCH" }, 476 { ILLF_IPV4, "IPV4" }, 477 { ILLF_IPV6, "IPV6" }, 478 { IPIF_MIPRUNNING, "MIP" }, 479 { IPIF_NOFAILOVER, "NOFAILOVER" }, 480 { PHYI_FAILED, "FAILED" }, 481 { PHYI_STANDBY, "STANDBY" }, 482 { PHYI_INACTIVE, "INACTIVE" }, 483 { PHYI_OFFLINE, "OFFLINE" }, 484 }; 485 486 static uchar_t ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 487 488 static ip_m_t ip_m_tbl[] = { 489 { DL_ETHER, IFT_ETHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo, 490 ip_ether_v6intfid }, 491 { DL_CSMACD, IFT_ISO88023, ip_ether_v4mapinfo, ip_ether_v6mapinfo, 492 ip_nodef_v6intfid }, 493 { DL_TPB, IFT_ISO88024, ip_ether_v4mapinfo, ip_ether_v6mapinfo, 494 ip_nodef_v6intfid }, 495 { DL_TPR, IFT_ISO88025, ip_ether_v4mapinfo, ip_ether_v6mapinfo, 496 ip_nodef_v6intfid }, 497 { DL_FDDI, IFT_FDDI, ip_ether_v4mapinfo, ip_ether_v6mapinfo, 498 ip_ether_v6intfid }, 499 { DL_IB, IFT_IB, ip_ib_v4mapinfo, ip_ib_v6mapinfo, 500 ip_ib_v6intfid }, 501 { SUNW_DL_VNI, IFT_OTHER, NULL, NULL, NULL}, 502 { DL_OTHER, IFT_OTHER, ip_ether_v4mapinfo, ip_ether_v6mapinfo, 503 ip_nodef_v6intfid } 504 }; 505 506 static ill_t ill_null; /* Empty ILL for init. */ 507 char ipif_loopback_name[] = "lo0"; 508 static char *ipv4_forward_suffix = ":ip_forwarding"; 509 static char *ipv6_forward_suffix = ":ip6_forwarding"; 510 static sin6_t sin6_null; /* Zero address for quick clears */ 511 static sin_t sin_null; /* Zero address for quick clears */ 512 513 /* When set search for unused ipif_seqid */ 514 static ipif_t ipif_zero; 515 516 /* 517 * ppa arena is created after these many 518 * interfaces have been plumbed. 519 */ 520 uint_t ill_no_arena = 12; /* Setable in /etc/system */ 521 522 /* 523 * Enable soft rings if ip_squeue_soft_ring or ip_squeue_fanout 524 * is set and ip_soft_rings_cnt > 0. ip_squeue_soft_ring is 525 * set through platform specific code (Niagara/Ontario). 526 */ 527 #define SOFT_RINGS_ENABLED() (ip_soft_rings_cnt ? \ 528 (ip_squeue_soft_ring || ip_squeue_fanout) : B_FALSE) 529 530 #define ILL_CAPAB_DLS (ILL_CAPAB_SOFT_RING | ILL_CAPAB_POLL) 531 532 static uint_t 533 ipif_rand(ip_stack_t *ipst) 534 { 535 ipst->ips_ipif_src_random = ipst->ips_ipif_src_random * 1103515245 + 536 12345; 537 return ((ipst->ips_ipif_src_random >> 16) & 0x7fff); 538 } 539 540 /* 541 * Allocate per-interface mibs. 542 * Returns true if ok. False otherwise. 543 * ipsq may not yet be allocated (loopback case ). 544 */ 545 static boolean_t 546 ill_allocate_mibs(ill_t *ill) 547 { 548 /* Already allocated? */ 549 if (ill->ill_ip_mib != NULL) { 550 if (ill->ill_isv6) 551 ASSERT(ill->ill_icmp6_mib != NULL); 552 return (B_TRUE); 553 } 554 555 ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib), 556 KM_NOSLEEP); 557 if (ill->ill_ip_mib == NULL) { 558 return (B_FALSE); 559 } 560 561 /* Setup static information */ 562 SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize, 563 sizeof (mib2_ipIfStatsEntry_t)); 564 if (ill->ill_isv6) { 565 ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6; 566 SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize, 567 sizeof (mib2_ipv6AddrEntry_t)); 568 SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize, 569 sizeof (mib2_ipv6RouteEntry_t)); 570 SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize, 571 sizeof (mib2_ipv6NetToMediaEntry_t)); 572 SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize, 573 sizeof (ipv6_member_t)); 574 SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize, 575 sizeof (ipv6_grpsrc_t)); 576 } else { 577 ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4; 578 SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize, 579 sizeof (mib2_ipAddrEntry_t)); 580 SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize, 581 sizeof (mib2_ipRouteEntry_t)); 582 SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize, 583 sizeof (mib2_ipNetToMediaEntry_t)); 584 SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize, 585 sizeof (ip_member_t)); 586 SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize, 587 sizeof (ip_grpsrc_t)); 588 589 /* 590 * For a v4 ill, we are done at this point, because per ill 591 * icmp mibs are only used for v6. 592 */ 593 return (B_TRUE); 594 } 595 596 ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib), 597 KM_NOSLEEP); 598 if (ill->ill_icmp6_mib == NULL) { 599 kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib)); 600 ill->ill_ip_mib = NULL; 601 return (B_FALSE); 602 } 603 /* static icmp info */ 604 ill->ill_icmp6_mib->ipv6IfIcmpEntrySize = 605 sizeof (mib2_ipv6IfIcmpEntry_t); 606 /* 607 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later 608 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert 609 * -> ill_phyint_reinit 610 */ 611 return (B_TRUE); 612 } 613 614 /* 615 * Common code for preparation of ARP commands. Two points to remember: 616 * 1) The ill_name is tacked on at the end of the allocated space so 617 * the templates name_offset field must contain the total space 618 * to allocate less the name length. 619 * 620 * 2) The templates name_length field should contain the *template* 621 * length. We use it as a parameter to bcopy() and then write 622 * the real ill_name_length into the name_length field of the copy. 623 * (Always called as writer.) 624 */ 625 mblk_t * 626 ill_arp_alloc(ill_t *ill, uchar_t *template, caddr_t addr) 627 { 628 arc_t *arc = (arc_t *)template; 629 char *cp; 630 int len; 631 mblk_t *mp; 632 uint_t name_length = ill->ill_name_length; 633 uint_t template_len = arc->arc_name_length; 634 635 len = arc->arc_name_offset + name_length; 636 mp = allocb(len, BPRI_HI); 637 if (mp == NULL) 638 return (NULL); 639 cp = (char *)mp->b_rptr; 640 mp->b_wptr = (uchar_t *)&cp[len]; 641 if (template_len) 642 bcopy(template, cp, template_len); 643 if (len > template_len) 644 bzero(&cp[template_len], len - template_len); 645 mp->b_datap->db_type = M_PROTO; 646 647 arc = (arc_t *)cp; 648 arc->arc_name_length = name_length; 649 cp = (char *)arc + arc->arc_name_offset; 650 bcopy(ill->ill_name, cp, name_length); 651 652 if (addr) { 653 area_t *area = (area_t *)mp->b_rptr; 654 655 cp = (char *)area + area->area_proto_addr_offset; 656 bcopy(addr, cp, area->area_proto_addr_length); 657 if (area->area_cmd == AR_ENTRY_ADD) { 658 cp = (char *)area; 659 len = area->area_proto_addr_length; 660 if (area->area_proto_mask_offset) 661 cp += area->area_proto_mask_offset; 662 else 663 cp += area->area_proto_addr_offset + len; 664 while (len-- > 0) 665 *cp++ = (char)~0; 666 } 667 } 668 return (mp); 669 } 670 671 mblk_t * 672 ipif_area_alloc(ipif_t *ipif) 673 { 674 return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_area_template, 675 (char *)&ipif->ipif_lcl_addr)); 676 } 677 678 mblk_t * 679 ipif_ared_alloc(ipif_t *ipif) 680 { 681 return (ill_arp_alloc(ipif->ipif_ill, (uchar_t *)&ip_ared_template, 682 (char *)&ipif->ipif_lcl_addr)); 683 } 684 685 mblk_t * 686 ill_ared_alloc(ill_t *ill, ipaddr_t addr) 687 { 688 return (ill_arp_alloc(ill, (uchar_t *)&ip_ared_template, 689 (char *)&addr)); 690 } 691 692 /* 693 * Completely vaporize a lower level tap and all associated interfaces. 694 * ill_delete is called only out of ip_close when the device control 695 * stream is being closed. 696 */ 697 void 698 ill_delete(ill_t *ill) 699 { 700 ipif_t *ipif; 701 ill_t *prev_ill; 702 ip_stack_t *ipst = ill->ill_ipst; 703 704 /* 705 * ill_delete may be forcibly entering the ipsq. The previous 706 * ioctl may not have completed and may need to be aborted. 707 * ipsq_flush takes care of it. If we don't need to enter the 708 * the ipsq forcibly, the 2nd invocation of ipsq_flush in 709 * ill_delete_tail is sufficient. 710 */ 711 ipsq_flush(ill); 712 713 /* 714 * Nuke all interfaces. ipif_free will take down the interface, 715 * remove it from the list, and free the data structure. 716 * Walk down the ipif list and remove the logical interfaces 717 * first before removing the main ipif. We can't unplumb 718 * zeroth interface first in the case of IPv6 as reset_conn_ill 719 * -> ip_ll_delmulti_v6 de-references ill_ipif for checking 720 * POINTOPOINT. 721 * 722 * If ill_ipif was not properly initialized (i.e low on memory), 723 * then no interfaces to clean up. In this case just clean up the 724 * ill. 725 */ 726 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) 727 ipif_free(ipif); 728 729 /* 730 * Used only by ill_arp_on and ill_arp_off, which are writers. 731 * So nobody can be using this mp now. Free the mp allocated for 732 * honoring ILLF_NOARP 733 */ 734 freemsg(ill->ill_arp_on_mp); 735 ill->ill_arp_on_mp = NULL; 736 737 /* Clean up msgs on pending upcalls for mrouted */ 738 reset_mrt_ill(ill); 739 740 /* 741 * ipif_free -> reset_conn_ipif will remove all multicast 742 * references for IPv4. For IPv6, we need to do it here as 743 * it points only at ills. 744 */ 745 reset_conn_ill(ill); 746 747 /* 748 * ill_down will arrange to blow off any IRE's dependent on this 749 * ILL, and shut down fragmentation reassembly. 750 */ 751 ill_down(ill); 752 753 /* Let SCTP know, so that it can remove this from its list. */ 754 sctp_update_ill(ill, SCTP_ILL_REMOVE); 755 756 /* 757 * If an address on this ILL is being used as a source address then 758 * clear out the pointers in other ILLs that point to this ILL. 759 */ 760 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER); 761 if (ill->ill_usesrc_grp_next != NULL) { 762 if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */ 763 ill_disband_usesrc_group(ill); 764 } else { /* consumer of the usesrc ILL */ 765 prev_ill = ill_prev_usesrc(ill); 766 prev_ill->ill_usesrc_grp_next = 767 ill->ill_usesrc_grp_next; 768 } 769 } 770 rw_exit(&ipst->ips_ill_g_usesrc_lock); 771 } 772 773 static void 774 ipif_non_duplicate(ipif_t *ipif) 775 { 776 ill_t *ill = ipif->ipif_ill; 777 mutex_enter(&ill->ill_lock); 778 if (ipif->ipif_flags & IPIF_DUPLICATE) { 779 ipif->ipif_flags &= ~IPIF_DUPLICATE; 780 ASSERT(ill->ill_ipif_dup_count > 0); 781 ill->ill_ipif_dup_count--; 782 } 783 mutex_exit(&ill->ill_lock); 784 } 785 786 /* 787 * ill_delete_tail is called from ip_modclose after all references 788 * to the closing ill are gone. The wait is done in ip_modclose 789 */ 790 void 791 ill_delete_tail(ill_t *ill) 792 { 793 mblk_t **mpp; 794 ipif_t *ipif; 795 ip_stack_t *ipst = ill->ill_ipst; 796 797 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 798 ipif_non_duplicate(ipif); 799 ipif_down_tail(ipif); 800 } 801 802 ASSERT(ill->ill_ipif_dup_count == 0 && 803 ill->ill_arp_down_mp == NULL && 804 ill->ill_arp_del_mapping_mp == NULL); 805 806 /* 807 * If polling capability is enabled (which signifies direct 808 * upcall into IP and driver has ill saved as a handle), 809 * we need to make sure that unbind has completed before we 810 * let the ill disappear and driver no longer has any reference 811 * to this ill. 812 */ 813 mutex_enter(&ill->ill_lock); 814 while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS) 815 cv_wait(&ill->ill_cv, &ill->ill_lock); 816 mutex_exit(&ill->ill_lock); 817 818 /* 819 * Clean up polling and soft ring capabilities 820 */ 821 if (ill->ill_capabilities & (ILL_CAPAB_POLL|ILL_CAPAB_SOFT_RING)) 822 ill_capability_dls_disable(ill); 823 824 if (ill->ill_net_type != IRE_LOOPBACK) 825 qprocsoff(ill->ill_rq); 826 827 /* 828 * We do an ipsq_flush once again now. New messages could have 829 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls 830 * could also have landed up if an ioctl thread had looked up 831 * the ill before we set the ILL_CONDEMNED flag, but not yet 832 * enqueued the ioctl when we did the ipsq_flush last time. 833 */ 834 ipsq_flush(ill); 835 836 /* 837 * Free capabilities. 838 */ 839 if (ill->ill_ipsec_capab_ah != NULL) { 840 ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_AH); 841 ill_ipsec_capab_free(ill->ill_ipsec_capab_ah); 842 ill->ill_ipsec_capab_ah = NULL; 843 } 844 845 if (ill->ill_ipsec_capab_esp != NULL) { 846 ill_ipsec_capab_delete(ill, DL_CAPAB_IPSEC_ESP); 847 ill_ipsec_capab_free(ill->ill_ipsec_capab_esp); 848 ill->ill_ipsec_capab_esp = NULL; 849 } 850 851 if (ill->ill_mdt_capab != NULL) { 852 kmem_free(ill->ill_mdt_capab, sizeof (ill_mdt_capab_t)); 853 ill->ill_mdt_capab = NULL; 854 } 855 856 if (ill->ill_hcksum_capab != NULL) { 857 kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t)); 858 ill->ill_hcksum_capab = NULL; 859 } 860 861 if (ill->ill_zerocopy_capab != NULL) { 862 kmem_free(ill->ill_zerocopy_capab, 863 sizeof (ill_zerocopy_capab_t)); 864 ill->ill_zerocopy_capab = NULL; 865 } 866 867 if (ill->ill_lso_capab != NULL) { 868 kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t)); 869 ill->ill_lso_capab = NULL; 870 } 871 872 if (ill->ill_dls_capab != NULL) { 873 CONN_DEC_REF(ill->ill_dls_capab->ill_unbind_conn); 874 ill->ill_dls_capab->ill_unbind_conn = NULL; 875 kmem_free(ill->ill_dls_capab, 876 sizeof (ill_dls_capab_t) + 877 (sizeof (ill_rx_ring_t) * ILL_MAX_RINGS)); 878 ill->ill_dls_capab = NULL; 879 } 880 881 ASSERT(!(ill->ill_capabilities & ILL_CAPAB_POLL)); 882 883 while (ill->ill_ipif != NULL) 884 ipif_free_tail(ill->ill_ipif); 885 886 ill_down_tail(ill); 887 888 /* 889 * We have removed all references to ilm from conn and the ones joined 890 * within the kernel. 891 * 892 * We don't walk conns, mrts and ires because 893 * 894 * 1) reset_conn_ill and reset_mrt_ill cleans up conns and mrts. 895 * 2) ill_down ->ill_downi walks all the ires and cleans up 896 * ill references. 897 */ 898 ASSERT(ilm_walk_ill(ill) == 0); 899 /* 900 * Take us out of the list of ILLs. ill_glist_delete -> ill_phyint_free 901 * could free the phyint. No more reference to the phyint after this 902 * point. 903 */ 904 (void) ill_glist_delete(ill); 905 906 rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER); 907 if (ill->ill_ndd_name != NULL) 908 nd_unload(&ipst->ips_ip_g_nd, ill->ill_ndd_name); 909 rw_exit(&ipst->ips_ip_g_nd_lock); 910 911 912 if (ill->ill_frag_ptr != NULL) { 913 uint_t count; 914 915 for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) { 916 mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock); 917 } 918 mi_free(ill->ill_frag_ptr); 919 ill->ill_frag_ptr = NULL; 920 ill->ill_frag_hash_tbl = NULL; 921 } 922 923 freemsg(ill->ill_nd_lla_mp); 924 /* Free all retained control messages. */ 925 mpp = &ill->ill_first_mp_to_free; 926 do { 927 while (mpp[0]) { 928 mblk_t *mp; 929 mblk_t *mp1; 930 931 mp = mpp[0]; 932 mpp[0] = mp->b_next; 933 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) { 934 mp1->b_next = NULL; 935 mp1->b_prev = NULL; 936 } 937 freemsg(mp); 938 } 939 } while (mpp++ != &ill->ill_last_mp_to_free); 940 941 ill_free_mib(ill); 942 /* Drop refcnt here */ 943 netstack_rele(ill->ill_ipst->ips_netstack); 944 ill->ill_ipst = NULL; 945 946 ILL_TRACE_CLEANUP(ill); 947 } 948 949 static void 950 ill_free_mib(ill_t *ill) 951 { 952 ip_stack_t *ipst = ill->ill_ipst; 953 954 /* 955 * MIB statistics must not be lost, so when an interface 956 * goes away the counter values will be added to the global 957 * MIBs. 958 */ 959 if (ill->ill_ip_mib != NULL) { 960 if (ill->ill_isv6) { 961 ip_mib2_add_ip_stats(&ipst->ips_ip6_mib, 962 ill->ill_ip_mib); 963 } else { 964 ip_mib2_add_ip_stats(&ipst->ips_ip_mib, 965 ill->ill_ip_mib); 966 } 967 968 kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib)); 969 ill->ill_ip_mib = NULL; 970 } 971 if (ill->ill_icmp6_mib != NULL) { 972 ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib, 973 ill->ill_icmp6_mib); 974 kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib)); 975 ill->ill_icmp6_mib = NULL; 976 } 977 } 978 979 /* 980 * Concatenate together a physical address and a sap. 981 * 982 * Sap_lengths are interpreted as follows: 983 * sap_length == 0 ==> no sap 984 * sap_length > 0 ==> sap is at the head of the dlpi address 985 * sap_length < 0 ==> sap is at the tail of the dlpi address 986 */ 987 static void 988 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length, 989 t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst) 990 { 991 uint16_t sap_addr = (uint16_t)sap_src; 992 993 if (sap_length == 0) { 994 if (phys_src == NULL) 995 bzero(dst, phys_length); 996 else 997 bcopy(phys_src, dst, phys_length); 998 } else if (sap_length < 0) { 999 if (phys_src == NULL) 1000 bzero(dst, phys_length); 1001 else 1002 bcopy(phys_src, dst, phys_length); 1003 bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr)); 1004 } else { 1005 bcopy(&sap_addr, dst, sizeof (sap_addr)); 1006 if (phys_src == NULL) 1007 bzero((char *)dst + sap_length, phys_length); 1008 else 1009 bcopy(phys_src, (char *)dst + sap_length, phys_length); 1010 } 1011 } 1012 1013 /* 1014 * Generate a dl_unitdata_req mblk for the device and address given. 1015 * addr_length is the length of the physical portion of the address. 1016 * If addr is NULL include an all zero address of the specified length. 1017 * TRUE? In any case, addr_length is taken to be the entire length of the 1018 * dlpi address, including the absolute value of sap_length. 1019 */ 1020 mblk_t * 1021 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap, 1022 t_scalar_t sap_length) 1023 { 1024 dl_unitdata_req_t *dlur; 1025 mblk_t *mp; 1026 t_scalar_t abs_sap_length; /* absolute value */ 1027 1028 abs_sap_length = ABS(sap_length); 1029 mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length, 1030 DL_UNITDATA_REQ); 1031 if (mp == NULL) 1032 return (NULL); 1033 dlur = (dl_unitdata_req_t *)mp->b_rptr; 1034 /* HACK: accomodate incompatible DLPI drivers */ 1035 if (addr_length == 8) 1036 addr_length = 6; 1037 dlur->dl_dest_addr_length = addr_length + abs_sap_length; 1038 dlur->dl_dest_addr_offset = sizeof (*dlur); 1039 dlur->dl_priority.dl_min = 0; 1040 dlur->dl_priority.dl_max = 0; 1041 ill_dlur_copy_address(addr, addr_length, sap, sap_length, 1042 (uchar_t *)&dlur[1]); 1043 return (mp); 1044 } 1045 1046 /* 1047 * Add the 'mp' to the list of pending mp's headed by ill_pending_mp 1048 * Return an error if we already have 1 or more ioctls in progress. 1049 * This is used only for non-exclusive ioctls. Currently this is used 1050 * for SIOC*ARP and SIOCGTUNPARAM ioctls. Most set ioctls are exclusive 1051 * and thus need to use ipsq_pending_mp_add. 1052 */ 1053 boolean_t 1054 ill_pending_mp_add(ill_t *ill, conn_t *connp, mblk_t *add_mp) 1055 { 1056 ASSERT(MUTEX_HELD(&ill->ill_lock)); 1057 ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL)); 1058 /* 1059 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls. 1060 */ 1061 ASSERT((add_mp->b_datap->db_type == M_IOCDATA) || 1062 (add_mp->b_datap->db_type == M_IOCTL)); 1063 1064 ASSERT(MUTEX_HELD(&connp->conn_lock)); 1065 /* 1066 * Return error if the conn has started closing. The conn 1067 * could have finished cleaning up the pending mp list, 1068 * If so we should not add another mp to the list negating 1069 * the cleanup. 1070 */ 1071 if (connp->conn_state_flags & CONN_CLOSING) 1072 return (B_FALSE); 1073 /* 1074 * Add the pending mp to the head of the list, chained by b_next. 1075 * Note down the conn on which the ioctl request came, in b_prev. 1076 * This will be used to later get the conn, when we get a response 1077 * on the ill queue, from some other module (typically arp) 1078 */ 1079 add_mp->b_next = (void *)ill->ill_pending_mp; 1080 add_mp->b_queue = CONNP_TO_WQ(connp); 1081 ill->ill_pending_mp = add_mp; 1082 if (connp != NULL) 1083 connp->conn_oper_pending_ill = ill; 1084 return (B_TRUE); 1085 } 1086 1087 /* 1088 * Retrieve the ill_pending_mp and return it. We have to walk the list 1089 * of mblks starting at ill_pending_mp, and match based on the ioc_id. 1090 */ 1091 mblk_t * 1092 ill_pending_mp_get(ill_t *ill, conn_t **connpp, uint_t ioc_id) 1093 { 1094 mblk_t *prev = NULL; 1095 mblk_t *curr = NULL; 1096 uint_t id; 1097 conn_t *connp; 1098 1099 /* 1100 * When the conn closes, conn_ioctl_cleanup needs to clean 1101 * up the pending mp, but it does not know the ioc_id and 1102 * passes in a zero for it. 1103 */ 1104 mutex_enter(&ill->ill_lock); 1105 if (ioc_id != 0) 1106 *connpp = NULL; 1107 1108 /* Search the list for the appropriate ioctl based on ioc_id */ 1109 for (prev = NULL, curr = ill->ill_pending_mp; curr != NULL; 1110 prev = curr, curr = curr->b_next) { 1111 id = ((struct iocblk *)curr->b_rptr)->ioc_id; 1112 connp = Q_TO_CONN(curr->b_queue); 1113 /* Match based on the ioc_id or based on the conn */ 1114 if ((id == ioc_id) || (ioc_id == 0 && connp == *connpp)) 1115 break; 1116 } 1117 1118 if (curr != NULL) { 1119 /* Unlink the mblk from the pending mp list */ 1120 if (prev != NULL) { 1121 prev->b_next = curr->b_next; 1122 } else { 1123 ASSERT(ill->ill_pending_mp == curr); 1124 ill->ill_pending_mp = curr->b_next; 1125 } 1126 1127 /* 1128 * conn refcnt must have been bumped up at the start of 1129 * the ioctl. So we can safely access the conn. 1130 */ 1131 ASSERT(CONN_Q(curr->b_queue)); 1132 *connpp = Q_TO_CONN(curr->b_queue); 1133 curr->b_next = NULL; 1134 curr->b_queue = NULL; 1135 } 1136 1137 mutex_exit(&ill->ill_lock); 1138 1139 return (curr); 1140 } 1141 1142 /* 1143 * Add the pending mp to the list. There can be only 1 pending mp 1144 * in the list. Any exclusive ioctl that needs to wait for a response 1145 * from another module or driver needs to use this function to set 1146 * the ipsq_pending_mp to the ioctl mblk and wait for the response from 1147 * the other module/driver. This is also used while waiting for the 1148 * ipif/ill/ire refcnts to drop to zero in bringing down an ipif. 1149 */ 1150 boolean_t 1151 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp, 1152 int waitfor) 1153 { 1154 ipsq_t *ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq; 1155 1156 ASSERT(IAM_WRITER_IPIF(ipif)); 1157 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 1158 ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL)); 1159 ASSERT(ipsq->ipsq_pending_mp == NULL); 1160 /* 1161 * The caller may be using a different ipif than the one passed into 1162 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4 1163 * ill needs to wait for the V6 ill to quiesce). So we can't ASSERT 1164 * that `ipsq_current_ipif == ipif'. 1165 */ 1166 ASSERT(ipsq->ipsq_current_ipif != NULL); 1167 1168 /* 1169 * M_IOCDATA from ioctls, M_IOCTL from tunnel ioctls, 1170 * M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the driver. 1171 */ 1172 ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_IOCTL) || 1173 (DB_TYPE(add_mp) == M_ERROR) || (DB_TYPE(add_mp) == M_HANGUP) || 1174 (DB_TYPE(add_mp) == M_PROTO) || (DB_TYPE(add_mp) == M_PCPROTO)); 1175 1176 if (connp != NULL) { 1177 ASSERT(MUTEX_HELD(&connp->conn_lock)); 1178 /* 1179 * Return error if the conn has started closing. The conn 1180 * could have finished cleaning up the pending mp list, 1181 * If so we should not add another mp to the list negating 1182 * the cleanup. 1183 */ 1184 if (connp->conn_state_flags & CONN_CLOSING) 1185 return (B_FALSE); 1186 } 1187 mutex_enter(&ipsq->ipsq_lock); 1188 ipsq->ipsq_pending_ipif = ipif; 1189 /* 1190 * Note down the queue in b_queue. This will be returned by 1191 * ipsq_pending_mp_get. Caller will then use these values to restart 1192 * the processing 1193 */ 1194 add_mp->b_next = NULL; 1195 add_mp->b_queue = q; 1196 ipsq->ipsq_pending_mp = add_mp; 1197 ipsq->ipsq_waitfor = waitfor; 1198 1199 if (connp != NULL) 1200 connp->conn_oper_pending_ill = ipif->ipif_ill; 1201 mutex_exit(&ipsq->ipsq_lock); 1202 return (B_TRUE); 1203 } 1204 1205 /* 1206 * Retrieve the ipsq_pending_mp and return it. There can be only 1 mp 1207 * queued in the list. 1208 */ 1209 mblk_t * 1210 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp) 1211 { 1212 mblk_t *curr = NULL; 1213 1214 mutex_enter(&ipsq->ipsq_lock); 1215 *connpp = NULL; 1216 if (ipsq->ipsq_pending_mp == NULL) { 1217 mutex_exit(&ipsq->ipsq_lock); 1218 return (NULL); 1219 } 1220 1221 /* There can be only 1 such excl message */ 1222 curr = ipsq->ipsq_pending_mp; 1223 ASSERT(curr != NULL && curr->b_next == NULL); 1224 ipsq->ipsq_pending_ipif = NULL; 1225 ipsq->ipsq_pending_mp = NULL; 1226 ipsq->ipsq_waitfor = 0; 1227 mutex_exit(&ipsq->ipsq_lock); 1228 1229 if (CONN_Q(curr->b_queue)) { 1230 /* 1231 * This mp did a refhold on the conn, at the start of the ioctl. 1232 * So we can safely return a pointer to the conn to the caller. 1233 */ 1234 *connpp = Q_TO_CONN(curr->b_queue); 1235 } else { 1236 *connpp = NULL; 1237 } 1238 curr->b_next = NULL; 1239 curr->b_prev = NULL; 1240 return (curr); 1241 } 1242 1243 /* 1244 * Cleanup the ioctl mp queued in ipsq_pending_mp 1245 * - Called in the ill_delete path 1246 * - Called in the M_ERROR or M_HANGUP path on the ill. 1247 * - Called in the conn close path. 1248 */ 1249 boolean_t 1250 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp) 1251 { 1252 mblk_t *mp; 1253 ipsq_t *ipsq; 1254 queue_t *q; 1255 ipif_t *ipif; 1256 1257 ASSERT(IAM_WRITER_ILL(ill)); 1258 ipsq = ill->ill_phyint->phyint_ipsq; 1259 mutex_enter(&ipsq->ipsq_lock); 1260 /* 1261 * If connp is null, unconditionally clean up the ipsq_pending_mp. 1262 * This happens in M_ERROR/M_HANGUP. We need to abort the current ioctl 1263 * even if it is meant for another ill, since we have to enqueue 1264 * a new mp now in ipsq_pending_mp to complete the ipif_down. 1265 * If connp is non-null we are called from the conn close path. 1266 */ 1267 mp = ipsq->ipsq_pending_mp; 1268 if (mp == NULL || (connp != NULL && 1269 mp->b_queue != CONNP_TO_WQ(connp))) { 1270 mutex_exit(&ipsq->ipsq_lock); 1271 return (B_FALSE); 1272 } 1273 /* Now remove from the ipsq_pending_mp */ 1274 ipsq->ipsq_pending_mp = NULL; 1275 q = mp->b_queue; 1276 mp->b_next = NULL; 1277 mp->b_prev = NULL; 1278 mp->b_queue = NULL; 1279 1280 /* If MOVE was in progress, clear the move_in_progress fields also. */ 1281 ill = ipsq->ipsq_pending_ipif->ipif_ill; 1282 if (ill->ill_move_in_progress) { 1283 ILL_CLEAR_MOVE(ill); 1284 } else if (ill->ill_up_ipifs) { 1285 ill_group_cleanup(ill); 1286 } 1287 1288 ipif = ipsq->ipsq_pending_ipif; 1289 ipsq->ipsq_pending_ipif = NULL; 1290 ipsq->ipsq_waitfor = 0; 1291 ipsq->ipsq_current_ipif = NULL; 1292 ipsq->ipsq_current_ioctl = 0; 1293 mutex_exit(&ipsq->ipsq_lock); 1294 1295 if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) { 1296 if (connp == NULL) { 1297 ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL); 1298 } else { 1299 ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL); 1300 mutex_enter(&ipif->ipif_ill->ill_lock); 1301 ipif->ipif_state_flags &= ~IPIF_CHANGING; 1302 mutex_exit(&ipif->ipif_ill->ill_lock); 1303 } 1304 } else { 1305 /* 1306 * IP-MT XXX In the case of TLI/XTI bind / optmgmt this can't 1307 * be just inet_freemsg. we have to restart it 1308 * otherwise the thread will be stuck. 1309 */ 1310 inet_freemsg(mp); 1311 } 1312 return (B_TRUE); 1313 } 1314 1315 /* 1316 * The ill is closing. Cleanup all the pending mps. Called exclusively 1317 * towards the end of ill_delete. The refcount has gone to 0. So nobody 1318 * knows this ill, and hence nobody can add an mp to this list 1319 */ 1320 static void 1321 ill_pending_mp_cleanup(ill_t *ill) 1322 { 1323 mblk_t *mp; 1324 queue_t *q; 1325 1326 ASSERT(IAM_WRITER_ILL(ill)); 1327 1328 mutex_enter(&ill->ill_lock); 1329 /* 1330 * Every mp on the pending mp list originating from an ioctl 1331 * added 1 to the conn refcnt, at the start of the ioctl. 1332 * So bump it down now. See comments in ip_wput_nondata() 1333 */ 1334 while (ill->ill_pending_mp != NULL) { 1335 mp = ill->ill_pending_mp; 1336 ill->ill_pending_mp = mp->b_next; 1337 mutex_exit(&ill->ill_lock); 1338 1339 q = mp->b_queue; 1340 ASSERT(CONN_Q(q)); 1341 mp->b_next = NULL; 1342 mp->b_prev = NULL; 1343 mp->b_queue = NULL; 1344 ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL); 1345 mutex_enter(&ill->ill_lock); 1346 } 1347 ill->ill_pending_ipif = NULL; 1348 1349 mutex_exit(&ill->ill_lock); 1350 } 1351 1352 /* 1353 * Called in the conn close path and ill delete path 1354 */ 1355 static void 1356 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp) 1357 { 1358 ipsq_t *ipsq; 1359 mblk_t *prev; 1360 mblk_t *curr; 1361 mblk_t *next; 1362 queue_t *q; 1363 mblk_t *tmp_list = NULL; 1364 1365 ASSERT(IAM_WRITER_ILL(ill)); 1366 if (connp != NULL) 1367 q = CONNP_TO_WQ(connp); 1368 else 1369 q = ill->ill_wq; 1370 1371 ipsq = ill->ill_phyint->phyint_ipsq; 1372 /* 1373 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any. 1374 * In the case of ioctl from a conn, there can be only 1 mp 1375 * queued on the ipsq. If an ill is being unplumbed, only messages 1376 * related to this ill are flushed, like M_ERROR or M_HANGUP message. 1377 * ioctls meant for this ill form conn's are not flushed. They will 1378 * be processed during ipsq_exit and will not find the ill and will 1379 * return error. 1380 */ 1381 mutex_enter(&ipsq->ipsq_lock); 1382 for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL; 1383 curr = next) { 1384 next = curr->b_next; 1385 if (curr->b_queue == q || curr->b_queue == RD(q)) { 1386 /* Unlink the mblk from the pending mp list */ 1387 if (prev != NULL) { 1388 prev->b_next = curr->b_next; 1389 } else { 1390 ASSERT(ipsq->ipsq_xopq_mphead == curr); 1391 ipsq->ipsq_xopq_mphead = curr->b_next; 1392 } 1393 if (ipsq->ipsq_xopq_mptail == curr) 1394 ipsq->ipsq_xopq_mptail = prev; 1395 /* 1396 * Create a temporary list and release the ipsq lock 1397 * New elements are added to the head of the tmp_list 1398 */ 1399 curr->b_next = tmp_list; 1400 tmp_list = curr; 1401 } else { 1402 prev = curr; 1403 } 1404 } 1405 mutex_exit(&ipsq->ipsq_lock); 1406 1407 while (tmp_list != NULL) { 1408 curr = tmp_list; 1409 tmp_list = curr->b_next; 1410 curr->b_next = NULL; 1411 curr->b_prev = NULL; 1412 curr->b_queue = NULL; 1413 if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) { 1414 ip_ioctl_finish(q, curr, ENXIO, connp != NULL ? 1415 CONN_CLOSE : NO_COPYOUT, NULL); 1416 } else { 1417 /* 1418 * IP-MT XXX In the case of TLI/XTI bind / optmgmt 1419 * this can't be just inet_freemsg. we have to 1420 * restart it otherwise the thread will be stuck. 1421 */ 1422 inet_freemsg(curr); 1423 } 1424 } 1425 } 1426 1427 /* 1428 * This conn has started closing. Cleanup any pending ioctl from this conn. 1429 * STREAMS ensures that there can be at most 1 ioctl pending on a stream. 1430 */ 1431 void 1432 conn_ioctl_cleanup(conn_t *connp) 1433 { 1434 mblk_t *curr; 1435 ipsq_t *ipsq; 1436 ill_t *ill; 1437 boolean_t refheld; 1438 1439 /* 1440 * Is any exclusive ioctl pending ? If so clean it up. If the 1441 * ioctl has not yet started, the mp is pending in the list headed by 1442 * ipsq_xopq_head. If the ioctl has started the mp could be present in 1443 * ipsq_pending_mp. If the ioctl timed out in the streamhead but 1444 * is currently executing now the mp is not queued anywhere but 1445 * conn_oper_pending_ill is null. The conn close will wait 1446 * till the conn_ref drops to zero. 1447 */ 1448 mutex_enter(&connp->conn_lock); 1449 ill = connp->conn_oper_pending_ill; 1450 if (ill == NULL) { 1451 mutex_exit(&connp->conn_lock); 1452 return; 1453 } 1454 1455 curr = ill_pending_mp_get(ill, &connp, 0); 1456 if (curr != NULL) { 1457 mutex_exit(&connp->conn_lock); 1458 CONN_DEC_REF(connp); 1459 inet_freemsg(curr); 1460 return; 1461 } 1462 /* 1463 * We may not be able to refhold the ill if the ill/ipif 1464 * is changing. But we need to make sure that the ill will 1465 * not vanish. So we just bump up the ill_waiter count. 1466 */ 1467 refheld = ill_waiter_inc(ill); 1468 mutex_exit(&connp->conn_lock); 1469 if (refheld) { 1470 if (ipsq_enter(ill, B_TRUE)) { 1471 ill_waiter_dcr(ill); 1472 /* 1473 * Check whether this ioctl has started and is 1474 * pending now in ipsq_pending_mp. If it is not 1475 * found there then check whether this ioctl has 1476 * not even started and is in the ipsq_xopq list. 1477 */ 1478 if (!ipsq_pending_mp_cleanup(ill, connp)) 1479 ipsq_xopq_mp_cleanup(ill, connp); 1480 ipsq = ill->ill_phyint->phyint_ipsq; 1481 ipsq_exit(ipsq, B_TRUE, B_TRUE); 1482 return; 1483 } 1484 } 1485 1486 /* 1487 * The ill is also closing and we could not bump up the 1488 * ill_waiter_count or we could not enter the ipsq. Leave 1489 * the cleanup to ill_delete 1490 */ 1491 mutex_enter(&connp->conn_lock); 1492 while (connp->conn_oper_pending_ill != NULL) 1493 cv_wait(&connp->conn_refcv, &connp->conn_lock); 1494 mutex_exit(&connp->conn_lock); 1495 if (refheld) 1496 ill_waiter_dcr(ill); 1497 } 1498 1499 /* 1500 * ipcl_walk function for cleaning up conn_*_ill fields. 1501 */ 1502 static void 1503 conn_cleanup_ill(conn_t *connp, caddr_t arg) 1504 { 1505 ill_t *ill = (ill_t *)arg; 1506 ire_t *ire; 1507 1508 mutex_enter(&connp->conn_lock); 1509 if (connp->conn_multicast_ill == ill) { 1510 /* Revert to late binding */ 1511 connp->conn_multicast_ill = NULL; 1512 connp->conn_orig_multicast_ifindex = 0; 1513 } 1514 if (connp->conn_incoming_ill == ill) 1515 connp->conn_incoming_ill = NULL; 1516 if (connp->conn_outgoing_ill == ill) 1517 connp->conn_outgoing_ill = NULL; 1518 if (connp->conn_outgoing_pill == ill) 1519 connp->conn_outgoing_pill = NULL; 1520 if (connp->conn_nofailover_ill == ill) 1521 connp->conn_nofailover_ill = NULL; 1522 if (connp->conn_xmit_if_ill == ill) 1523 connp->conn_xmit_if_ill = NULL; 1524 if (connp->conn_ire_cache != NULL) { 1525 ire = connp->conn_ire_cache; 1526 /* 1527 * ip_newroute creates IRE_CACHE with ire_stq coming from 1528 * interface X and ipif coming from interface Y, if interface 1529 * X and Y are part of the same IPMPgroup. Thus whenever 1530 * interface X goes down, remove all references to it by 1531 * checking both on ire_ipif and ire_stq. 1532 */ 1533 if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) || 1534 (ire->ire_type == IRE_CACHE && 1535 ire->ire_stq == ill->ill_wq)) { 1536 connp->conn_ire_cache = NULL; 1537 mutex_exit(&connp->conn_lock); 1538 ire_refrele_notr(ire); 1539 return; 1540 } 1541 } 1542 mutex_exit(&connp->conn_lock); 1543 1544 } 1545 1546 /* ARGSUSED */ 1547 void 1548 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 1549 { 1550 ill_t *ill = q->q_ptr; 1551 ipif_t *ipif; 1552 1553 ASSERT(IAM_WRITER_IPSQ(ipsq)); 1554 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 1555 ipif_non_duplicate(ipif); 1556 ipif_down_tail(ipif); 1557 } 1558 ill_down_tail(ill); 1559 freemsg(mp); 1560 ipsq_current_finish(ipsq); 1561 } 1562 1563 /* 1564 * ill_down_start is called when we want to down this ill and bring it up again 1565 * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down 1566 * all interfaces, but don't tear down any plumbing. 1567 */ 1568 boolean_t 1569 ill_down_start(queue_t *q, mblk_t *mp) 1570 { 1571 ill_t *ill = q->q_ptr; 1572 ipif_t *ipif; 1573 1574 ASSERT(IAM_WRITER_ILL(ill)); 1575 1576 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) 1577 (void) ipif_down(ipif, NULL, NULL); 1578 1579 ill_down(ill); 1580 1581 (void) ipsq_pending_mp_cleanup(ill, NULL); 1582 1583 ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0); 1584 1585 /* 1586 * Atomically test and add the pending mp if references are active. 1587 */ 1588 mutex_enter(&ill->ill_lock); 1589 if (!ill_is_quiescent(ill)) { 1590 /* call cannot fail since `conn_t *' argument is NULL */ 1591 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq, 1592 mp, ILL_DOWN); 1593 mutex_exit(&ill->ill_lock); 1594 return (B_FALSE); 1595 } 1596 mutex_exit(&ill->ill_lock); 1597 return (B_TRUE); 1598 } 1599 1600 static void 1601 ill_down(ill_t *ill) 1602 { 1603 ip_stack_t *ipst = ill->ill_ipst; 1604 1605 /* Blow off any IREs dependent on this ILL. */ 1606 ire_walk(ill_downi, (char *)ill, ipst); 1607 1608 mutex_enter(&ipst->ips_ire_mrtun_lock); 1609 if (ipst->ips_ire_mrtun_count != 0) { 1610 mutex_exit(&ipst->ips_ire_mrtun_lock); 1611 ire_walk_ill_mrtun(0, 0, ill_downi_mrtun_srcif, 1612 (char *)ill, NULL, ipst); 1613 } else { 1614 mutex_exit(&ipst->ips_ire_mrtun_lock); 1615 } 1616 1617 /* 1618 * If any interface based forwarding table exists 1619 * Blow off the ires there dependent on this ill 1620 */ 1621 mutex_enter(&ipst->ips_ire_srcif_table_lock); 1622 if (ipst->ips_ire_srcif_table_count > 0) { 1623 mutex_exit(&ipst->ips_ire_srcif_table_lock); 1624 ire_walk_srcif_table_v4(ill_downi_mrtun_srcif, (char *)ill, 1625 ipst); 1626 } else { 1627 mutex_exit(&ipst->ips_ire_srcif_table_lock); 1628 } 1629 1630 /* Remove any conn_*_ill depending on this ill */ 1631 ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst); 1632 1633 if (ill->ill_group != NULL) { 1634 illgrp_delete(ill); 1635 } 1636 } 1637 1638 static void 1639 ill_down_tail(ill_t *ill) 1640 { 1641 int i; 1642 1643 /* Destroy ill_srcif_table if it exists */ 1644 /* Lock not reqd really because nobody should be able to access */ 1645 mutex_enter(&ill->ill_lock); 1646 if (ill->ill_srcif_table != NULL) { 1647 ill->ill_srcif_refcnt = 0; 1648 for (i = 0; i < IP_SRCIF_TABLE_SIZE; i++) { 1649 rw_destroy(&ill->ill_srcif_table[i].irb_lock); 1650 } 1651 kmem_free(ill->ill_srcif_table, 1652 IP_SRCIF_TABLE_SIZE * sizeof (irb_t)); 1653 ill->ill_srcif_table = NULL; 1654 ill->ill_srcif_refcnt = 0; 1655 ill->ill_mrtun_refcnt = 0; 1656 } 1657 mutex_exit(&ill->ill_lock); 1658 } 1659 1660 /* 1661 * ire_walk routine used to delete every IRE that depends on queues 1662 * associated with 'ill'. (Always called as writer.) 1663 */ 1664 static void 1665 ill_downi(ire_t *ire, char *ill_arg) 1666 { 1667 ill_t *ill = (ill_t *)ill_arg; 1668 1669 /* 1670 * ip_newroute creates IRE_CACHE with ire_stq coming from 1671 * interface X and ipif coming from interface Y, if interface 1672 * X and Y are part of the same IPMP group. Thus whenever interface 1673 * X goes down, remove all references to it by checking both 1674 * on ire_ipif and ire_stq. 1675 */ 1676 if ((ire->ire_ipif != NULL && ire->ire_ipif->ipif_ill == ill) || 1677 (ire->ire_type == IRE_CACHE && ire->ire_stq == ill->ill_wq)) { 1678 ire_delete(ire); 1679 } 1680 } 1681 1682 /* 1683 * A seperate routine for deleting revtun and srcif based routes 1684 * are needed because the ires only deleted when the interface 1685 * is unplumbed. Also these ires have ire_in_ill non-null as well. 1686 * we want to keep mobile IP specific code separate. 1687 */ 1688 static void 1689 ill_downi_mrtun_srcif(ire_t *ire, char *ill_arg) 1690 { 1691 ill_t *ill = (ill_t *)ill_arg; 1692 1693 ASSERT(ire->ire_in_ill != NULL); 1694 1695 if ((ire->ire_in_ill != NULL && ire->ire_in_ill == ill) || 1696 (ire->ire_stq == ill->ill_wq) || (ire->ire_stq == ill->ill_rq)) { 1697 ire_delete(ire); 1698 } 1699 } 1700 1701 /* 1702 * Remove ire/nce from the fastpath list. 1703 */ 1704 void 1705 ill_fastpath_nack(ill_t *ill) 1706 { 1707 nce_fastpath_list_dispatch(ill, NULL, NULL); 1708 } 1709 1710 /* Consume an M_IOCACK of the fastpath probe. */ 1711 void 1712 ill_fastpath_ack(ill_t *ill, mblk_t *mp) 1713 { 1714 mblk_t *mp1 = mp; 1715 1716 /* 1717 * If this was the first attempt turn on the fastpath probing. 1718 */ 1719 mutex_enter(&ill->ill_lock); 1720 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) 1721 ill->ill_dlpi_fastpath_state = IDS_OK; 1722 mutex_exit(&ill->ill_lock); 1723 1724 /* Free the M_IOCACK mblk, hold on to the data */ 1725 mp = mp->b_cont; 1726 freeb(mp1); 1727 if (mp == NULL) 1728 return; 1729 if (mp->b_cont != NULL) { 1730 /* 1731 * Update all IRE's or NCE's that are waiting for 1732 * fastpath update. 1733 */ 1734 nce_fastpath_list_dispatch(ill, ndp_fastpath_update, mp); 1735 mp1 = mp->b_cont; 1736 freeb(mp); 1737 mp = mp1; 1738 } else { 1739 ip0dbg(("ill_fastpath_ack: no b_cont\n")); 1740 } 1741 1742 freeb(mp); 1743 } 1744 1745 /* 1746 * Throw an M_IOCTL message downstream asking "do you know fastpath?" 1747 * The data portion of the request is a dl_unitdata_req_t template for 1748 * what we would send downstream in the absence of a fastpath confirmation. 1749 */ 1750 int 1751 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp) 1752 { 1753 struct iocblk *ioc; 1754 mblk_t *mp; 1755 1756 if (dlur_mp == NULL) 1757 return (EINVAL); 1758 1759 mutex_enter(&ill->ill_lock); 1760 switch (ill->ill_dlpi_fastpath_state) { 1761 case IDS_FAILED: 1762 /* 1763 * Driver NAKed the first fastpath ioctl - assume it doesn't 1764 * support it. 1765 */ 1766 mutex_exit(&ill->ill_lock); 1767 return (ENOTSUP); 1768 case IDS_UNKNOWN: 1769 /* This is the first probe */ 1770 ill->ill_dlpi_fastpath_state = IDS_INPROGRESS; 1771 break; 1772 default: 1773 break; 1774 } 1775 mutex_exit(&ill->ill_lock); 1776 1777 if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL) 1778 return (EAGAIN); 1779 1780 mp->b_cont = copyb(dlur_mp); 1781 if (mp->b_cont == NULL) { 1782 freeb(mp); 1783 return (EAGAIN); 1784 } 1785 1786 ioc = (struct iocblk *)mp->b_rptr; 1787 ioc->ioc_count = msgdsize(mp->b_cont); 1788 1789 putnext(ill->ill_wq, mp); 1790 return (0); 1791 } 1792 1793 void 1794 ill_capability_probe(ill_t *ill) 1795 { 1796 /* 1797 * Do so only if negotiation is enabled, capabilities are unknown, 1798 * and a capability negotiation is not already in progress. 1799 */ 1800 if (ill->ill_dlpi_capab_state != IDS_UNKNOWN && 1801 ill->ill_dlpi_capab_state != IDS_RENEG) 1802 return; 1803 1804 ill->ill_dlpi_capab_state = IDS_INPROGRESS; 1805 ip1dbg(("ill_capability_probe: starting capability negotiation\n")); 1806 ill_capability_proto(ill, DL_CAPABILITY_REQ, NULL); 1807 } 1808 1809 void 1810 ill_capability_reset(ill_t *ill) 1811 { 1812 mblk_t *sc_mp = NULL; 1813 mblk_t *tmp; 1814 1815 /* 1816 * Note here that we reset the state to UNKNOWN, and later send 1817 * down the DL_CAPABILITY_REQ without first setting the state to 1818 * INPROGRESS. We do this in order to distinguish the 1819 * DL_CAPABILITY_ACK response which may come back in response to 1820 * a "reset" apart from the "probe" DL_CAPABILITY_REQ. This would 1821 * also handle the case where the driver doesn't send us back 1822 * a DL_CAPABILITY_ACK in response, since the "probe" routine 1823 * requires the state to be in UNKNOWN anyway. In any case, all 1824 * features are turned off until the state reaches IDS_OK. 1825 */ 1826 ill->ill_dlpi_capab_state = IDS_UNKNOWN; 1827 1828 /* 1829 * Disable sub-capabilities and request a list of sub-capability 1830 * messages which will be sent down to the driver. Each handler 1831 * allocates the corresponding dl_capability_sub_t inside an 1832 * mblk, and links it to the existing sc_mp mblk, or return it 1833 * as sc_mp if it's the first sub-capability (the passed in 1834 * sc_mp is NULL). Upon returning from all capability handlers, 1835 * sc_mp will be pulled-up, before passing it downstream. 1836 */ 1837 ill_capability_mdt_reset(ill, &sc_mp); 1838 ill_capability_hcksum_reset(ill, &sc_mp); 1839 ill_capability_zerocopy_reset(ill, &sc_mp); 1840 ill_capability_ipsec_reset(ill, &sc_mp); 1841 ill_capability_dls_reset(ill, &sc_mp); 1842 ill_capability_lso_reset(ill, &sc_mp); 1843 1844 /* Nothing to send down in order to disable the capabilities? */ 1845 if (sc_mp == NULL) 1846 return; 1847 1848 tmp = msgpullup(sc_mp, -1); 1849 freemsg(sc_mp); 1850 if ((sc_mp = tmp) == NULL) { 1851 cmn_err(CE_WARN, "ill_capability_reset: unable to send down " 1852 "DL_CAPABILITY_REQ (ENOMEM)\n"); 1853 return; 1854 } 1855 1856 ip1dbg(("ill_capability_reset: resetting negotiated capabilities\n")); 1857 ill_capability_proto(ill, DL_CAPABILITY_REQ, sc_mp); 1858 } 1859 1860 /* 1861 * Request or set new-style hardware capabilities supported by DLS provider. 1862 */ 1863 static void 1864 ill_capability_proto(ill_t *ill, int type, mblk_t *reqp) 1865 { 1866 mblk_t *mp; 1867 dl_capability_req_t *capb; 1868 size_t size = 0; 1869 uint8_t *ptr; 1870 1871 if (reqp != NULL) 1872 size = MBLKL(reqp); 1873 1874 mp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + size, type); 1875 if (mp == NULL) { 1876 freemsg(reqp); 1877 return; 1878 } 1879 ptr = mp->b_rptr; 1880 1881 capb = (dl_capability_req_t *)ptr; 1882 ptr += sizeof (dl_capability_req_t); 1883 1884 if (reqp != NULL) { 1885 capb->dl_sub_offset = sizeof (dl_capability_req_t); 1886 capb->dl_sub_length = size; 1887 bcopy(reqp->b_rptr, ptr, size); 1888 ptr += size; 1889 mp->b_cont = reqp->b_cont; 1890 freeb(reqp); 1891 } 1892 ASSERT(ptr == mp->b_wptr); 1893 1894 ill_dlpi_send(ill, mp); 1895 } 1896 1897 static void 1898 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers) 1899 { 1900 dl_capab_id_t *id_ic; 1901 uint_t sub_dl_cap = outers->dl_cap; 1902 dl_capability_sub_t *inners; 1903 uint8_t *capend; 1904 1905 ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER); 1906 1907 /* 1908 * Note: range checks here are not absolutely sufficient to 1909 * make us robust against malformed messages sent by drivers; 1910 * this is in keeping with the rest of IP's dlpi handling. 1911 * (Remember, it's coming from something else in the kernel 1912 * address space) 1913 */ 1914 1915 capend = (uint8_t *)(outers + 1) + outers->dl_length; 1916 if (capend > mp->b_wptr) { 1917 cmn_err(CE_WARN, "ill_capability_id_ack: " 1918 "malformed sub-capability too long for mblk"); 1919 return; 1920 } 1921 1922 id_ic = (dl_capab_id_t *)(outers + 1); 1923 1924 if (outers->dl_length < sizeof (*id_ic) || 1925 (inners = &id_ic->id_subcap, 1926 inners->dl_length > (outers->dl_length - sizeof (*inners)))) { 1927 cmn_err(CE_WARN, "ill_capability_id_ack: malformed " 1928 "encapsulated capab type %d too long for mblk", 1929 inners->dl_cap); 1930 return; 1931 } 1932 1933 if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) { 1934 ip1dbg(("ill_capability_id_ack: mid token for capab type %d " 1935 "isn't as expected; pass-thru module(s) detected, " 1936 "discarding capability\n", inners->dl_cap)); 1937 return; 1938 } 1939 1940 /* Process the encapsulated sub-capability */ 1941 ill_capability_dispatch(ill, mp, inners, B_TRUE); 1942 } 1943 1944 /* 1945 * Process Multidata Transmit capability negotiation ack received from a 1946 * DLS Provider. isub must point to the sub-capability (DL_CAPAB_MDT) of a 1947 * DL_CAPABILITY_ACK message. 1948 */ 1949 static void 1950 ill_capability_mdt_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub) 1951 { 1952 mblk_t *nmp = NULL; 1953 dl_capability_req_t *oc; 1954 dl_capab_mdt_t *mdt_ic, *mdt_oc; 1955 ill_mdt_capab_t **ill_mdt_capab; 1956 uint_t sub_dl_cap = isub->dl_cap; 1957 uint8_t *capend; 1958 1959 ASSERT(sub_dl_cap == DL_CAPAB_MDT); 1960 1961 ill_mdt_capab = (ill_mdt_capab_t **)&ill->ill_mdt_capab; 1962 1963 /* 1964 * Note: range checks here are not absolutely sufficient to 1965 * make us robust against malformed messages sent by drivers; 1966 * this is in keeping with the rest of IP's dlpi handling. 1967 * (Remember, it's coming from something else in the kernel 1968 * address space) 1969 */ 1970 1971 capend = (uint8_t *)(isub + 1) + isub->dl_length; 1972 if (capend > mp->b_wptr) { 1973 cmn_err(CE_WARN, "ill_capability_mdt_ack: " 1974 "malformed sub-capability too long for mblk"); 1975 return; 1976 } 1977 1978 mdt_ic = (dl_capab_mdt_t *)(isub + 1); 1979 1980 if (mdt_ic->mdt_version != MDT_VERSION_2) { 1981 cmn_err(CE_CONT, "ill_capability_mdt_ack: " 1982 "unsupported MDT sub-capability (version %d, expected %d)", 1983 mdt_ic->mdt_version, MDT_VERSION_2); 1984 return; 1985 } 1986 1987 if (!dlcapabcheckqid(&mdt_ic->mdt_mid, ill->ill_lmod_rq)) { 1988 ip1dbg(("ill_capability_mdt_ack: mid token for MDT " 1989 "capability isn't as expected; pass-thru module(s) " 1990 "detected, discarding capability\n")); 1991 return; 1992 } 1993 1994 if (mdt_ic->mdt_flags & DL_CAPAB_MDT_ENABLE) { 1995 1996 if (*ill_mdt_capab == NULL) { 1997 *ill_mdt_capab = kmem_zalloc(sizeof (ill_mdt_capab_t), 1998 KM_NOSLEEP); 1999 2000 if (*ill_mdt_capab == NULL) { 2001 cmn_err(CE_WARN, "ill_capability_mdt_ack: " 2002 "could not enable MDT version %d " 2003 "for %s (ENOMEM)\n", MDT_VERSION_2, 2004 ill->ill_name); 2005 return; 2006 } 2007 } 2008 2009 ip1dbg(("ill_capability_mdt_ack: interface %s supports " 2010 "MDT version %d (%d bytes leading, %d bytes trailing " 2011 "header spaces, %d max pld bufs, %d span limit)\n", 2012 ill->ill_name, MDT_VERSION_2, 2013 mdt_ic->mdt_hdr_head, mdt_ic->mdt_hdr_tail, 2014 mdt_ic->mdt_max_pld, mdt_ic->mdt_span_limit)); 2015 2016 (*ill_mdt_capab)->ill_mdt_version = MDT_VERSION_2; 2017 (*ill_mdt_capab)->ill_mdt_on = 1; 2018 /* 2019 * Round the following values to the nearest 32-bit; ULP 2020 * may further adjust them to accomodate for additional 2021 * protocol headers. We pass these values to ULP during 2022 * bind time. 2023 */ 2024 (*ill_mdt_capab)->ill_mdt_hdr_head = 2025 roundup(mdt_ic->mdt_hdr_head, 4); 2026 (*ill_mdt_capab)->ill_mdt_hdr_tail = 2027 roundup(mdt_ic->mdt_hdr_tail, 4); 2028 (*ill_mdt_capab)->ill_mdt_max_pld = mdt_ic->mdt_max_pld; 2029 (*ill_mdt_capab)->ill_mdt_span_limit = mdt_ic->mdt_span_limit; 2030 2031 ill->ill_capabilities |= ILL_CAPAB_MDT; 2032 } else { 2033 uint_t size; 2034 uchar_t *rptr; 2035 2036 size = sizeof (dl_capability_req_t) + 2037 sizeof (dl_capability_sub_t) + sizeof (dl_capab_mdt_t); 2038 2039 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) { 2040 cmn_err(CE_WARN, "ill_capability_mdt_ack: " 2041 "could not enable MDT for %s (ENOMEM)\n", 2042 ill->ill_name); 2043 return; 2044 } 2045 2046 rptr = nmp->b_rptr; 2047 /* initialize dl_capability_req_t */ 2048 oc = (dl_capability_req_t *)nmp->b_rptr; 2049 oc->dl_sub_offset = sizeof (dl_capability_req_t); 2050 oc->dl_sub_length = sizeof (dl_capability_sub_t) + 2051 sizeof (dl_capab_mdt_t); 2052 nmp->b_rptr += sizeof (dl_capability_req_t); 2053 2054 /* initialize dl_capability_sub_t */ 2055 bcopy(isub, nmp->b_rptr, sizeof (*isub)); 2056 nmp->b_rptr += sizeof (*isub); 2057 2058 /* initialize dl_capab_mdt_t */ 2059 mdt_oc = (dl_capab_mdt_t *)nmp->b_rptr; 2060 bcopy(mdt_ic, mdt_oc, sizeof (*mdt_ic)); 2061 2062 nmp->b_rptr = rptr; 2063 2064 ip1dbg(("ill_capability_mdt_ack: asking interface %s " 2065 "to enable MDT version %d\n", ill->ill_name, 2066 MDT_VERSION_2)); 2067 2068 /* set ENABLE flag */ 2069 mdt_oc->mdt_flags |= DL_CAPAB_MDT_ENABLE; 2070 2071 /* nmp points to a DL_CAPABILITY_REQ message to enable MDT */ 2072 ill_dlpi_send(ill, nmp); 2073 } 2074 } 2075 2076 static void 2077 ill_capability_mdt_reset(ill_t *ill, mblk_t **sc_mp) 2078 { 2079 mblk_t *mp; 2080 dl_capab_mdt_t *mdt_subcap; 2081 dl_capability_sub_t *dl_subcap; 2082 int size; 2083 2084 if (!ILL_MDT_CAPABLE(ill)) 2085 return; 2086 2087 ASSERT(ill->ill_mdt_capab != NULL); 2088 /* 2089 * Clear the capability flag for MDT but retain the ill_mdt_capab 2090 * structure since it's possible that another thread is still 2091 * referring to it. The structure only gets deallocated when 2092 * we destroy the ill. 2093 */ 2094 ill->ill_capabilities &= ~ILL_CAPAB_MDT; 2095 2096 size = sizeof (*dl_subcap) + sizeof (*mdt_subcap); 2097 2098 mp = allocb(size, BPRI_HI); 2099 if (mp == NULL) { 2100 ip1dbg(("ill_capability_mdt_reset: unable to allocate " 2101 "request to disable MDT\n")); 2102 return; 2103 } 2104 2105 mp->b_wptr = mp->b_rptr + size; 2106 2107 dl_subcap = (dl_capability_sub_t *)mp->b_rptr; 2108 dl_subcap->dl_cap = DL_CAPAB_MDT; 2109 dl_subcap->dl_length = sizeof (*mdt_subcap); 2110 2111 mdt_subcap = (dl_capab_mdt_t *)(dl_subcap + 1); 2112 mdt_subcap->mdt_version = ill->ill_mdt_capab->ill_mdt_version; 2113 mdt_subcap->mdt_flags = 0; 2114 mdt_subcap->mdt_hdr_head = 0; 2115 mdt_subcap->mdt_hdr_tail = 0; 2116 2117 if (*sc_mp != NULL) 2118 linkb(*sc_mp, mp); 2119 else 2120 *sc_mp = mp; 2121 } 2122 2123 /* 2124 * Send a DL_NOTIFY_REQ to the specified ill to enable 2125 * DL_NOTE_PROMISC_ON/OFF_PHYS notifications. 2126 * Invoked by ill_capability_ipsec_ack() before enabling IPsec hardware 2127 * acceleration. 2128 * Returns B_TRUE on success, B_FALSE if the message could not be sent. 2129 */ 2130 static boolean_t 2131 ill_enable_promisc_notify(ill_t *ill) 2132 { 2133 mblk_t *mp; 2134 dl_notify_req_t *req; 2135 2136 IPSECHW_DEBUG(IPSECHW_PKT, ("ill_enable_promisc_notify:\n")); 2137 2138 mp = ip_dlpi_alloc(sizeof (dl_notify_req_t), DL_NOTIFY_REQ); 2139 if (mp == NULL) 2140 return (B_FALSE); 2141 2142 req = (dl_notify_req_t *)mp->b_rptr; 2143 req->dl_notifications = DL_NOTE_PROMISC_ON_PHYS | 2144 DL_NOTE_PROMISC_OFF_PHYS; 2145 2146 ill_dlpi_send(ill, mp); 2147 2148 return (B_TRUE); 2149 } 2150 2151 2152 /* 2153 * Allocate an IPsec capability request which will be filled by our 2154 * caller to turn on support for one or more algorithms. 2155 */ 2156 static mblk_t * 2157 ill_alloc_ipsec_cap_req(ill_t *ill, dl_capability_sub_t *isub) 2158 { 2159 mblk_t *nmp; 2160 dl_capability_req_t *ocap; 2161 dl_capab_ipsec_t *ocip; 2162 dl_capab_ipsec_t *icip; 2163 uint8_t *ptr; 2164 icip = (dl_capab_ipsec_t *)(isub + 1); 2165 2166 /* 2167 * The first time around, we send a DL_NOTIFY_REQ to enable 2168 * PROMISC_ON/OFF notification from the provider. We need to 2169 * do this before enabling the algorithms to avoid leakage of 2170 * cleartext packets. 2171 */ 2172 2173 if (!ill_enable_promisc_notify(ill)) 2174 return (NULL); 2175 2176 /* 2177 * Allocate new mblk which will contain a new capability 2178 * request to enable the capabilities. 2179 */ 2180 2181 nmp = ip_dlpi_alloc(sizeof (dl_capability_req_t) + 2182 sizeof (dl_capability_sub_t) + isub->dl_length, DL_CAPABILITY_REQ); 2183 if (nmp == NULL) 2184 return (NULL); 2185 2186 ptr = nmp->b_rptr; 2187 2188 /* initialize dl_capability_req_t */ 2189 ocap = (dl_capability_req_t *)ptr; 2190 ocap->dl_sub_offset = sizeof (dl_capability_req_t); 2191 ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length; 2192 ptr += sizeof (dl_capability_req_t); 2193 2194 /* initialize dl_capability_sub_t */ 2195 bcopy(isub, ptr, sizeof (*isub)); 2196 ptr += sizeof (*isub); 2197 2198 /* initialize dl_capab_ipsec_t */ 2199 ocip = (dl_capab_ipsec_t *)ptr; 2200 bcopy(icip, ocip, sizeof (*icip)); 2201 2202 nmp->b_wptr = (uchar_t *)(&ocip->cip_data[0]); 2203 return (nmp); 2204 } 2205 2206 /* 2207 * Process an IPsec capability negotiation ack received from a DLS Provider. 2208 * isub must point to the sub-capability (DL_CAPAB_IPSEC_AH or 2209 * DL_CAPAB_IPSEC_ESP) of a DL_CAPABILITY_ACK message. 2210 */ 2211 static void 2212 ill_capability_ipsec_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub) 2213 { 2214 dl_capab_ipsec_t *icip; 2215 dl_capab_ipsec_alg_t *ialg; /* ptr to input alg spec. */ 2216 dl_capab_ipsec_alg_t *oalg; /* ptr to output alg spec. */ 2217 uint_t cipher, nciphers; 2218 mblk_t *nmp; 2219 uint_t alg_len; 2220 boolean_t need_sadb_dump; 2221 uint_t sub_dl_cap = isub->dl_cap; 2222 ill_ipsec_capab_t **ill_capab; 2223 uint64_t ill_capab_flag; 2224 uint8_t *capend, *ciphend; 2225 boolean_t sadb_resync; 2226 2227 ASSERT(sub_dl_cap == DL_CAPAB_IPSEC_AH || 2228 sub_dl_cap == DL_CAPAB_IPSEC_ESP); 2229 2230 if (sub_dl_cap == DL_CAPAB_IPSEC_AH) { 2231 ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_ah; 2232 ill_capab_flag = ILL_CAPAB_AH; 2233 } else { 2234 ill_capab = (ill_ipsec_capab_t **)&ill->ill_ipsec_capab_esp; 2235 ill_capab_flag = ILL_CAPAB_ESP; 2236 } 2237 2238 /* 2239 * If the ill capability structure exists, then this incoming 2240 * DL_CAPABILITY_ACK is a response to a "renegotiation" cycle. 2241 * If this is so, then we'd need to resynchronize the SADB 2242 * after re-enabling the offloaded ciphers. 2243 */ 2244 sadb_resync = (*ill_capab != NULL); 2245 2246 /* 2247 * Note: range checks here are not absolutely sufficient to 2248 * make us robust against malformed messages sent by drivers; 2249 * this is in keeping with the rest of IP's dlpi handling. 2250 * (Remember, it's coming from something else in the kernel 2251 * address space) 2252 */ 2253 2254 capend = (uint8_t *)(isub + 1) + isub->dl_length; 2255 if (capend > mp->b_wptr) { 2256 cmn_err(CE_WARN, "ill_capability_ipsec_ack: " 2257 "malformed sub-capability too long for mblk"); 2258 return; 2259 } 2260 2261 /* 2262 * There are two types of acks we process here: 2263 * 1. acks in reply to a (first form) generic capability req 2264 * (no ENABLE flag set) 2265 * 2. acks in reply to a ENABLE capability req. 2266 * (ENABLE flag set) 2267 * 2268 * We process the subcapability passed as argument as follows: 2269 * 1 do initializations 2270 * 1.1 initialize nmp = NULL 2271 * 1.2 set need_sadb_dump to B_FALSE 2272 * 2 for each cipher in subcapability: 2273 * 2.1 if ENABLE flag is set: 2274 * 2.1.1 update per-ill ipsec capabilities info 2275 * 2.1.2 set need_sadb_dump to B_TRUE 2276 * 2.2 if ENABLE flag is not set: 2277 * 2.2.1 if nmp is NULL: 2278 * 2.2.1.1 allocate and initialize nmp 2279 * 2.2.1.2 init current pos in nmp 2280 * 2.2.2 copy current cipher to current pos in nmp 2281 * 2.2.3 set ENABLE flag in nmp 2282 * 2.2.4 update current pos 2283 * 3 if nmp is not equal to NULL, send enable request 2284 * 3.1 send capability request 2285 * 4 if need_sadb_dump is B_TRUE 2286 * 4.1 enable promiscuous on/off notifications 2287 * 4.2 call ill_dlpi_send(isub->dlcap) to send all 2288 * AH or ESP SA's to interface. 2289 */ 2290 2291 nmp = NULL; 2292 oalg = NULL; 2293 need_sadb_dump = B_FALSE; 2294 icip = (dl_capab_ipsec_t *)(isub + 1); 2295 ialg = (dl_capab_ipsec_alg_t *)(&icip->cip_data[0]); 2296 2297 nciphers = icip->cip_nciphers; 2298 ciphend = (uint8_t *)(ialg + icip->cip_nciphers); 2299 2300 if (ciphend > capend) { 2301 cmn_err(CE_WARN, "ill_capability_ipsec_ack: " 2302 "too many ciphers for sub-capability len"); 2303 return; 2304 } 2305 2306 for (cipher = 0; cipher < nciphers; cipher++) { 2307 alg_len = sizeof (dl_capab_ipsec_alg_t); 2308 2309 if (ialg->alg_flag & DL_CAPAB_ALG_ENABLE) { 2310 /* 2311 * TBD: when we provide a way to disable capabilities 2312 * from above, need to manage the request-pending state 2313 * and fail if we were not expecting this ACK. 2314 */ 2315 IPSECHW_DEBUG(IPSECHW_CAPAB, 2316 ("ill_capability_ipsec_ack: got ENABLE ACK\n")); 2317 2318 /* 2319 * Update IPsec capabilities for this ill 2320 */ 2321 2322 if (*ill_capab == NULL) { 2323 IPSECHW_DEBUG(IPSECHW_CAPAB, 2324 ("ill_capability_ipsec_ack: " 2325 "allocating ipsec_capab for ill\n")); 2326 *ill_capab = ill_ipsec_capab_alloc(); 2327 2328 if (*ill_capab == NULL) { 2329 cmn_err(CE_WARN, 2330 "ill_capability_ipsec_ack: " 2331 "could not enable IPsec Hardware " 2332 "acceleration for %s (ENOMEM)\n", 2333 ill->ill_name); 2334 return; 2335 } 2336 } 2337 2338 ASSERT(ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH || 2339 ialg->alg_type == DL_CAPAB_IPSEC_ALG_ENCR); 2340 2341 if (ialg->alg_prim >= MAX_IPSEC_ALGS) { 2342 cmn_err(CE_WARN, 2343 "ill_capability_ipsec_ack: " 2344 "malformed IPsec algorithm id %d", 2345 ialg->alg_prim); 2346 continue; 2347 } 2348 2349 if (ialg->alg_type == DL_CAPAB_IPSEC_ALG_AUTH) { 2350 IPSEC_ALG_ENABLE((*ill_capab)->auth_hw_algs, 2351 ialg->alg_prim); 2352 } else { 2353 ipsec_capab_algparm_t *alp; 2354 2355 IPSEC_ALG_ENABLE((*ill_capab)->encr_hw_algs, 2356 ialg->alg_prim); 2357 if (!ill_ipsec_capab_resize_algparm(*ill_capab, 2358 ialg->alg_prim)) { 2359 cmn_err(CE_WARN, 2360 "ill_capability_ipsec_ack: " 2361 "no space for IPsec alg id %d", 2362 ialg->alg_prim); 2363 continue; 2364 } 2365 alp = &((*ill_capab)->encr_algparm[ 2366 ialg->alg_prim]); 2367 alp->minkeylen = ialg->alg_minbits; 2368 alp->maxkeylen = ialg->alg_maxbits; 2369 } 2370 ill->ill_capabilities |= ill_capab_flag; 2371 /* 2372 * indicate that a capability was enabled, which 2373 * will be used below to kick off a SADB dump 2374 * to the ill. 2375 */ 2376 need_sadb_dump = B_TRUE; 2377 } else { 2378 IPSECHW_DEBUG(IPSECHW_CAPAB, 2379 ("ill_capability_ipsec_ack: enabling alg 0x%x\n", 2380 ialg->alg_prim)); 2381 2382 if (nmp == NULL) { 2383 nmp = ill_alloc_ipsec_cap_req(ill, isub); 2384 if (nmp == NULL) { 2385 /* 2386 * Sending the PROMISC_ON/OFF 2387 * notification request failed. 2388 * We cannot enable the algorithms 2389 * since the Provider will not 2390 * notify IP of promiscous mode 2391 * changes, which could lead 2392 * to leakage of packets. 2393 */ 2394 cmn_err(CE_WARN, 2395 "ill_capability_ipsec_ack: " 2396 "could not enable IPsec Hardware " 2397 "acceleration for %s (ENOMEM)\n", 2398 ill->ill_name); 2399 return; 2400 } 2401 /* ptr to current output alg specifier */ 2402 oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr; 2403 } 2404 2405 /* 2406 * Copy current alg specifier, set ENABLE 2407 * flag, and advance to next output alg. 2408 * For now we enable all IPsec capabilities. 2409 */ 2410 ASSERT(oalg != NULL); 2411 bcopy(ialg, oalg, alg_len); 2412 oalg->alg_flag |= DL_CAPAB_ALG_ENABLE; 2413 nmp->b_wptr += alg_len; 2414 oalg = (dl_capab_ipsec_alg_t *)nmp->b_wptr; 2415 } 2416 2417 /* move to next input algorithm specifier */ 2418 ialg = (dl_capab_ipsec_alg_t *) 2419 ((char *)ialg + alg_len); 2420 } 2421 2422 if (nmp != NULL) 2423 /* 2424 * nmp points to a DL_CAPABILITY_REQ message to enable 2425 * IPsec hardware acceleration. 2426 */ 2427 ill_dlpi_send(ill, nmp); 2428 2429 if (need_sadb_dump) 2430 /* 2431 * An acknowledgement corresponding to a request to 2432 * enable acceleration was received, notify SADB. 2433 */ 2434 ill_ipsec_capab_add(ill, sub_dl_cap, sadb_resync); 2435 } 2436 2437 /* 2438 * Given an mblk with enough space in it, create sub-capability entries for 2439 * DL_CAPAB_IPSEC_{AH,ESP} types which consist of previously-advertised 2440 * offloaded ciphers (both AUTH and ENCR) with their enable flags cleared, 2441 * in preparation for the reset the DL_CAPABILITY_REQ message. 2442 */ 2443 static void 2444 ill_fill_ipsec_reset(uint_t nciphers, int stype, uint_t slen, 2445 ill_ipsec_capab_t *ill_cap, mblk_t *mp) 2446 { 2447 dl_capab_ipsec_t *oipsec; 2448 dl_capab_ipsec_alg_t *oalg; 2449 dl_capability_sub_t *dl_subcap; 2450 int i, k; 2451 2452 ASSERT(nciphers > 0); 2453 ASSERT(ill_cap != NULL); 2454 ASSERT(mp != NULL); 2455 ASSERT(MBLKTAIL(mp) >= sizeof (*dl_subcap) + sizeof (*oipsec) + slen); 2456 2457 /* dl_capability_sub_t for "stype" */ 2458 dl_subcap = (dl_capability_sub_t *)mp->b_wptr; 2459 dl_subcap->dl_cap = stype; 2460 dl_subcap->dl_length = sizeof (dl_capab_ipsec_t) + slen; 2461 mp->b_wptr += sizeof (dl_capability_sub_t); 2462 2463 /* dl_capab_ipsec_t for "stype" */ 2464 oipsec = (dl_capab_ipsec_t *)mp->b_wptr; 2465 oipsec->cip_version = 1; 2466 oipsec->cip_nciphers = nciphers; 2467 mp->b_wptr = (uchar_t *)&oipsec->cip_data[0]; 2468 2469 /* create entries for "stype" AUTH ciphers */ 2470 for (i = 0; i < ill_cap->algs_size; i++) { 2471 for (k = 0; k < BITSPERBYTE; k++) { 2472 if ((ill_cap->auth_hw_algs[i] & (1 << k)) == 0) 2473 continue; 2474 2475 oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr; 2476 bzero((void *)oalg, sizeof (*oalg)); 2477 oalg->alg_type = DL_CAPAB_IPSEC_ALG_AUTH; 2478 oalg->alg_prim = k + (BITSPERBYTE * i); 2479 mp->b_wptr += sizeof (dl_capab_ipsec_alg_t); 2480 } 2481 } 2482 /* create entries for "stype" ENCR ciphers */ 2483 for (i = 0; i < ill_cap->algs_size; i++) { 2484 for (k = 0; k < BITSPERBYTE; k++) { 2485 if ((ill_cap->encr_hw_algs[i] & (1 << k)) == 0) 2486 continue; 2487 2488 oalg = (dl_capab_ipsec_alg_t *)mp->b_wptr; 2489 bzero((void *)oalg, sizeof (*oalg)); 2490 oalg->alg_type = DL_CAPAB_IPSEC_ALG_ENCR; 2491 oalg->alg_prim = k + (BITSPERBYTE * i); 2492 mp->b_wptr += sizeof (dl_capab_ipsec_alg_t); 2493 } 2494 } 2495 } 2496 2497 /* 2498 * Macro to count number of 1s in a byte (8-bit word). The total count is 2499 * accumulated into the passed-in argument (sum). We could use SPARCv9's 2500 * POPC instruction, but our macro is more flexible for an arbitrary length 2501 * of bytes, such as {auth,encr}_hw_algs. These variables are currently 2502 * 256-bits long (MAX_IPSEC_ALGS), so if we know for sure that the length 2503 * stays that way, we can reduce the number of iterations required. 2504 */ 2505 #define COUNT_1S(val, sum) { \ 2506 uint8_t x = val & 0xff; \ 2507 x = (x & 0x55) + ((x >> 1) & 0x55); \ 2508 x = (x & 0x33) + ((x >> 2) & 0x33); \ 2509 sum += (x & 0xf) + ((x >> 4) & 0xf); \ 2510 } 2511 2512 /* ARGSUSED */ 2513 static void 2514 ill_capability_ipsec_reset(ill_t *ill, mblk_t **sc_mp) 2515 { 2516 mblk_t *mp; 2517 ill_ipsec_capab_t *cap_ah = ill->ill_ipsec_capab_ah; 2518 ill_ipsec_capab_t *cap_esp = ill->ill_ipsec_capab_esp; 2519 uint64_t ill_capabilities = ill->ill_capabilities; 2520 int ah_cnt = 0, esp_cnt = 0; 2521 int ah_len = 0, esp_len = 0; 2522 int i, size = 0; 2523 2524 if (!(ill_capabilities & (ILL_CAPAB_AH | ILL_CAPAB_ESP))) 2525 return; 2526 2527 ASSERT(cap_ah != NULL || !(ill_capabilities & ILL_CAPAB_AH)); 2528 ASSERT(cap_esp != NULL || !(ill_capabilities & ILL_CAPAB_ESP)); 2529 2530 /* Find out the number of ciphers for AH */ 2531 if (cap_ah != NULL) { 2532 for (i = 0; i < cap_ah->algs_size; i++) { 2533 COUNT_1S(cap_ah->auth_hw_algs[i], ah_cnt); 2534 COUNT_1S(cap_ah->encr_hw_algs[i], ah_cnt); 2535 } 2536 if (ah_cnt > 0) { 2537 size += sizeof (dl_capability_sub_t) + 2538 sizeof (dl_capab_ipsec_t); 2539 /* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */ 2540 ah_len = (ah_cnt - 1) * sizeof (dl_capab_ipsec_alg_t); 2541 size += ah_len; 2542 } 2543 } 2544 2545 /* Find out the number of ciphers for ESP */ 2546 if (cap_esp != NULL) { 2547 for (i = 0; i < cap_esp->algs_size; i++) { 2548 COUNT_1S(cap_esp->auth_hw_algs[i], esp_cnt); 2549 COUNT_1S(cap_esp->encr_hw_algs[i], esp_cnt); 2550 } 2551 if (esp_cnt > 0) { 2552 size += sizeof (dl_capability_sub_t) + 2553 sizeof (dl_capab_ipsec_t); 2554 /* dl_capab_ipsec_t contains one dl_capab_ipsec_alg_t */ 2555 esp_len = (esp_cnt - 1) * sizeof (dl_capab_ipsec_alg_t); 2556 size += esp_len; 2557 } 2558 } 2559 2560 if (size == 0) { 2561 ip1dbg(("ill_capability_ipsec_reset: capabilities exist but " 2562 "there's nothing to reset\n")); 2563 return; 2564 } 2565 2566 mp = allocb(size, BPRI_HI); 2567 if (mp == NULL) { 2568 ip1dbg(("ill_capability_ipsec_reset: unable to allocate " 2569 "request to disable IPSEC Hardware Acceleration\n")); 2570 return; 2571 } 2572 2573 /* 2574 * Clear the capability flags for IPSec HA but retain the ill 2575 * capability structures since it's possible that another thread 2576 * is still referring to them. The structures only get deallocated 2577 * when we destroy the ill. 2578 * 2579 * Various places check the flags to see if the ill is capable of 2580 * hardware acceleration, and by clearing them we ensure that new 2581 * outbound IPSec packets are sent down encrypted. 2582 */ 2583 ill->ill_capabilities &= ~(ILL_CAPAB_AH | ILL_CAPAB_ESP); 2584 2585 /* Fill in DL_CAPAB_IPSEC_AH sub-capability entries */ 2586 if (ah_cnt > 0) { 2587 ill_fill_ipsec_reset(ah_cnt, DL_CAPAB_IPSEC_AH, ah_len, 2588 cap_ah, mp); 2589 ASSERT(mp->b_rptr + size >= mp->b_wptr); 2590 } 2591 2592 /* Fill in DL_CAPAB_IPSEC_ESP sub-capability entries */ 2593 if (esp_cnt > 0) { 2594 ill_fill_ipsec_reset(esp_cnt, DL_CAPAB_IPSEC_ESP, esp_len, 2595 cap_esp, mp); 2596 ASSERT(mp->b_rptr + size >= mp->b_wptr); 2597 } 2598 2599 /* 2600 * At this point we've composed a bunch of sub-capabilities to be 2601 * encapsulated in a DL_CAPABILITY_REQ and later sent downstream 2602 * by the caller. Upon receiving this reset message, the driver 2603 * must stop inbound decryption (by destroying all inbound SAs) 2604 * and let the corresponding packets come in encrypted. 2605 */ 2606 2607 if (*sc_mp != NULL) 2608 linkb(*sc_mp, mp); 2609 else 2610 *sc_mp = mp; 2611 } 2612 2613 static void 2614 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp, 2615 boolean_t encapsulated) 2616 { 2617 boolean_t legacy = B_FALSE; 2618 2619 /* 2620 * If this DL_CAPABILITY_ACK came in as a response to our "reset" 2621 * DL_CAPABILITY_REQ, ignore it during this cycle. We've just 2622 * instructed the driver to disable its advertised capabilities, 2623 * so there's no point in accepting any response at this moment. 2624 */ 2625 if (ill->ill_dlpi_capab_state == IDS_UNKNOWN) 2626 return; 2627 2628 /* 2629 * Note that only the following two sub-capabilities may be 2630 * considered as "legacy", since their original definitions 2631 * do not incorporate the dl_mid_t module ID token, and hence 2632 * may require the use of the wrapper sub-capability. 2633 */ 2634 switch (subp->dl_cap) { 2635 case DL_CAPAB_IPSEC_AH: 2636 case DL_CAPAB_IPSEC_ESP: 2637 legacy = B_TRUE; 2638 break; 2639 } 2640 2641 /* 2642 * For legacy sub-capabilities which don't incorporate a queue_t 2643 * pointer in their structures, discard them if we detect that 2644 * there are intermediate modules in between IP and the driver. 2645 */ 2646 if (!encapsulated && legacy && ill->ill_lmod_cnt > 1) { 2647 ip1dbg(("ill_capability_dispatch: unencapsulated capab type " 2648 "%d discarded; %d module(s) present below IP\n", 2649 subp->dl_cap, ill->ill_lmod_cnt)); 2650 return; 2651 } 2652 2653 switch (subp->dl_cap) { 2654 case DL_CAPAB_IPSEC_AH: 2655 case DL_CAPAB_IPSEC_ESP: 2656 ill_capability_ipsec_ack(ill, mp, subp); 2657 break; 2658 case DL_CAPAB_MDT: 2659 ill_capability_mdt_ack(ill, mp, subp); 2660 break; 2661 case DL_CAPAB_HCKSUM: 2662 ill_capability_hcksum_ack(ill, mp, subp); 2663 break; 2664 case DL_CAPAB_ZEROCOPY: 2665 ill_capability_zerocopy_ack(ill, mp, subp); 2666 break; 2667 case DL_CAPAB_POLL: 2668 if (!SOFT_RINGS_ENABLED()) 2669 ill_capability_dls_ack(ill, mp, subp); 2670 break; 2671 case DL_CAPAB_SOFT_RING: 2672 if (SOFT_RINGS_ENABLED()) 2673 ill_capability_dls_ack(ill, mp, subp); 2674 break; 2675 case DL_CAPAB_LSO: 2676 ill_capability_lso_ack(ill, mp, subp); 2677 break; 2678 default: 2679 ip1dbg(("ill_capability_dispatch: unknown capab type %d\n", 2680 subp->dl_cap)); 2681 } 2682 } 2683 2684 /* 2685 * As part of negotiating polling capability, the driver tells us 2686 * the default (or normal) blanking interval and packet threshold 2687 * (the receive timer fires if blanking interval is reached or 2688 * the packet threshold is reached). 2689 * 2690 * As part of manipulating the polling interval, we always use our 2691 * estimated interval (avg service time * number of packets queued 2692 * on the squeue) but we try to blank for a minimum of 2693 * rr_normal_blank_time * rr_max_blank_ratio. We disable the 2694 * packet threshold during this time. When we are not in polling mode 2695 * we set the blank interval typically lower, rr_normal_pkt_cnt * 2696 * rr_min_blank_ratio but up the packet cnt by a ratio of 2697 * rr_min_pkt_cnt_ratio so that we are still getting chains if 2698 * possible although for a shorter interval. 2699 */ 2700 #define RR_MAX_BLANK_RATIO 20 2701 #define RR_MIN_BLANK_RATIO 10 2702 #define RR_MAX_PKT_CNT_RATIO 3 2703 #define RR_MIN_PKT_CNT_RATIO 3 2704 2705 /* 2706 * These can be tuned via /etc/system. 2707 */ 2708 int rr_max_blank_ratio = RR_MAX_BLANK_RATIO; 2709 int rr_min_blank_ratio = RR_MIN_BLANK_RATIO; 2710 int rr_max_pkt_cnt_ratio = RR_MAX_PKT_CNT_RATIO; 2711 int rr_min_pkt_cnt_ratio = RR_MIN_PKT_CNT_RATIO; 2712 2713 static mac_resource_handle_t 2714 ill_ring_add(void *arg, mac_resource_t *mrp) 2715 { 2716 ill_t *ill = (ill_t *)arg; 2717 mac_rx_fifo_t *mrfp = (mac_rx_fifo_t *)mrp; 2718 ill_rx_ring_t *rx_ring; 2719 int ip_rx_index; 2720 2721 ASSERT(mrp != NULL); 2722 if (mrp->mr_type != MAC_RX_FIFO) { 2723 return (NULL); 2724 } 2725 ASSERT(ill != NULL); 2726 ASSERT(ill->ill_dls_capab != NULL); 2727 2728 mutex_enter(&ill->ill_lock); 2729 for (ip_rx_index = 0; ip_rx_index < ILL_MAX_RINGS; ip_rx_index++) { 2730 rx_ring = &ill->ill_dls_capab->ill_ring_tbl[ip_rx_index]; 2731 ASSERT(rx_ring != NULL); 2732 2733 if (rx_ring->rr_ring_state == ILL_RING_FREE) { 2734 time_t normal_blank_time = 2735 mrfp->mrf_normal_blank_time; 2736 uint_t normal_pkt_cnt = 2737 mrfp->mrf_normal_pkt_count; 2738 2739 bzero(rx_ring, sizeof (ill_rx_ring_t)); 2740 2741 rx_ring->rr_blank = mrfp->mrf_blank; 2742 rx_ring->rr_handle = mrfp->mrf_arg; 2743 rx_ring->rr_ill = ill; 2744 rx_ring->rr_normal_blank_time = normal_blank_time; 2745 rx_ring->rr_normal_pkt_cnt = normal_pkt_cnt; 2746 2747 rx_ring->rr_max_blank_time = 2748 normal_blank_time * rr_max_blank_ratio; 2749 rx_ring->rr_min_blank_time = 2750 normal_blank_time * rr_min_blank_ratio; 2751 rx_ring->rr_max_pkt_cnt = 2752 normal_pkt_cnt * rr_max_pkt_cnt_ratio; 2753 rx_ring->rr_min_pkt_cnt = 2754 normal_pkt_cnt * rr_min_pkt_cnt_ratio; 2755 2756 rx_ring->rr_ring_state = ILL_RING_INUSE; 2757 mutex_exit(&ill->ill_lock); 2758 2759 DTRACE_PROBE2(ill__ring__add, (void *), ill, 2760 (int), ip_rx_index); 2761 return ((mac_resource_handle_t)rx_ring); 2762 } 2763 } 2764 2765 /* 2766 * We ran out of ILL_MAX_RINGS worth rx_ring structures. If 2767 * we have devices which can overwhelm this limit, ILL_MAX_RING 2768 * should be made configurable. Meanwhile it cause no panic because 2769 * driver will pass ip_input a NULL handle which will make 2770 * IP allocate the default squeue and Polling mode will not 2771 * be used for this ring. 2772 */ 2773 cmn_err(CE_NOTE, "Reached maximum number of receiving rings (%d) " 2774 "for %s\n", ILL_MAX_RINGS, ill->ill_name); 2775 2776 mutex_exit(&ill->ill_lock); 2777 return (NULL); 2778 } 2779 2780 static boolean_t 2781 ill_capability_dls_init(ill_t *ill) 2782 { 2783 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 2784 conn_t *connp; 2785 size_t sz; 2786 ip_stack_t *ipst = ill->ill_ipst; 2787 2788 if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) { 2789 if (ill_dls == NULL) { 2790 cmn_err(CE_PANIC, "ill_capability_dls_init: " 2791 "soft_ring enabled for ill=%s (%p) but data " 2792 "structs uninitialized\n", ill->ill_name, 2793 (void *)ill); 2794 } 2795 return (B_TRUE); 2796 } else if (ill->ill_capabilities & ILL_CAPAB_POLL) { 2797 if (ill_dls == NULL) { 2798 cmn_err(CE_PANIC, "ill_capability_dls_init: " 2799 "polling enabled for ill=%s (%p) but data " 2800 "structs uninitialized\n", ill->ill_name, 2801 (void *)ill); 2802 } 2803 return (B_TRUE); 2804 } 2805 2806 if (ill_dls != NULL) { 2807 ill_rx_ring_t *rx_ring = ill_dls->ill_ring_tbl; 2808 /* Soft_Ring or polling is being re-enabled */ 2809 2810 connp = ill_dls->ill_unbind_conn; 2811 ASSERT(rx_ring != NULL); 2812 bzero((void *)ill_dls, sizeof (ill_dls_capab_t)); 2813 bzero((void *)rx_ring, 2814 sizeof (ill_rx_ring_t) * ILL_MAX_RINGS); 2815 ill_dls->ill_ring_tbl = rx_ring; 2816 ill_dls->ill_unbind_conn = connp; 2817 return (B_TRUE); 2818 } 2819 2820 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 2821 ipst->ips_netstack)) == NULL) 2822 return (B_FALSE); 2823 2824 sz = sizeof (ill_dls_capab_t); 2825 sz += sizeof (ill_rx_ring_t) * ILL_MAX_RINGS; 2826 2827 ill_dls = kmem_zalloc(sz, KM_NOSLEEP); 2828 if (ill_dls == NULL) { 2829 cmn_err(CE_WARN, "ill_capability_dls_init: could not " 2830 "allocate dls_capab for %s (%p)\n", ill->ill_name, 2831 (void *)ill); 2832 CONN_DEC_REF(connp); 2833 return (B_FALSE); 2834 } 2835 2836 /* Allocate space to hold ring table */ 2837 ill_dls->ill_ring_tbl = (ill_rx_ring_t *)&ill_dls[1]; 2838 ill->ill_dls_capab = ill_dls; 2839 ill_dls->ill_unbind_conn = connp; 2840 return (B_TRUE); 2841 } 2842 2843 /* 2844 * ill_capability_dls_disable: disable soft_ring and/or polling 2845 * capability. Since any of the rings might already be in use, need 2846 * to call ip_squeue_clean_all() which gets behind the squeue to disable 2847 * direct calls if necessary. 2848 */ 2849 static void 2850 ill_capability_dls_disable(ill_t *ill) 2851 { 2852 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 2853 2854 if (ill->ill_capabilities & ILL_CAPAB_DLS) { 2855 ip_squeue_clean_all(ill); 2856 ill_dls->ill_tx = NULL; 2857 ill_dls->ill_tx_handle = NULL; 2858 ill_dls->ill_dls_change_status = NULL; 2859 ill_dls->ill_dls_bind = NULL; 2860 ill_dls->ill_dls_unbind = NULL; 2861 } 2862 2863 ASSERT(!(ill->ill_capabilities & ILL_CAPAB_DLS)); 2864 } 2865 2866 static void 2867 ill_capability_dls_capable(ill_t *ill, dl_capab_dls_t *idls, 2868 dl_capability_sub_t *isub) 2869 { 2870 uint_t size; 2871 uchar_t *rptr; 2872 dl_capab_dls_t dls, *odls; 2873 ill_dls_capab_t *ill_dls; 2874 mblk_t *nmp = NULL; 2875 dl_capability_req_t *ocap; 2876 uint_t sub_dl_cap = isub->dl_cap; 2877 2878 if (!ill_capability_dls_init(ill)) 2879 return; 2880 ill_dls = ill->ill_dls_capab; 2881 2882 /* Copy locally to get the members aligned */ 2883 bcopy((void *)idls, (void *)&dls, 2884 sizeof (dl_capab_dls_t)); 2885 2886 /* Get the tx function and handle from dld */ 2887 ill_dls->ill_tx = (ip_dld_tx_t)dls.dls_tx; 2888 ill_dls->ill_tx_handle = (void *)dls.dls_tx_handle; 2889 2890 if (sub_dl_cap == DL_CAPAB_SOFT_RING) { 2891 ill_dls->ill_dls_change_status = 2892 (ip_dls_chg_soft_ring_t)dls.dls_ring_change_status; 2893 ill_dls->ill_dls_bind = (ip_dls_bind_t)dls.dls_ring_bind; 2894 ill_dls->ill_dls_unbind = 2895 (ip_dls_unbind_t)dls.dls_ring_unbind; 2896 ill_dls->ill_dls_soft_ring_cnt = ip_soft_rings_cnt; 2897 } 2898 2899 size = sizeof (dl_capability_req_t) + sizeof (dl_capability_sub_t) + 2900 isub->dl_length; 2901 2902 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) { 2903 cmn_err(CE_WARN, "ill_capability_dls_capable: could " 2904 "not allocate memory for CAPAB_REQ for %s (%p)\n", 2905 ill->ill_name, (void *)ill); 2906 return; 2907 } 2908 2909 /* initialize dl_capability_req_t */ 2910 rptr = nmp->b_rptr; 2911 ocap = (dl_capability_req_t *)rptr; 2912 ocap->dl_sub_offset = sizeof (dl_capability_req_t); 2913 ocap->dl_sub_length = sizeof (dl_capability_sub_t) + isub->dl_length; 2914 rptr += sizeof (dl_capability_req_t); 2915 2916 /* initialize dl_capability_sub_t */ 2917 bcopy(isub, rptr, sizeof (*isub)); 2918 rptr += sizeof (*isub); 2919 2920 odls = (dl_capab_dls_t *)rptr; 2921 rptr += sizeof (dl_capab_dls_t); 2922 2923 /* initialize dl_capab_dls_t to be sent down */ 2924 dls.dls_rx_handle = (uintptr_t)ill; 2925 dls.dls_rx = (uintptr_t)ip_input; 2926 dls.dls_ring_add = (uintptr_t)ill_ring_add; 2927 2928 if (sub_dl_cap == DL_CAPAB_SOFT_RING) { 2929 dls.dls_ring_cnt = ip_soft_rings_cnt; 2930 dls.dls_ring_assign = (uintptr_t)ip_soft_ring_assignment; 2931 dls.dls_flags = SOFT_RING_ENABLE; 2932 } else { 2933 dls.dls_flags = POLL_ENABLE; 2934 ip1dbg(("ill_capability_dls_capable: asking interface %s " 2935 "to enable polling\n", ill->ill_name)); 2936 } 2937 bcopy((void *)&dls, (void *)odls, 2938 sizeof (dl_capab_dls_t)); 2939 ASSERT(nmp->b_wptr == (nmp->b_rptr + size)); 2940 /* 2941 * nmp points to a DL_CAPABILITY_REQ message to 2942 * enable either soft_ring or polling 2943 */ 2944 ill_dlpi_send(ill, nmp); 2945 } 2946 2947 static void 2948 ill_capability_dls_reset(ill_t *ill, mblk_t **sc_mp) 2949 { 2950 mblk_t *mp; 2951 dl_capab_dls_t *idls; 2952 dl_capability_sub_t *dl_subcap; 2953 int size; 2954 2955 if (!(ill->ill_capabilities & ILL_CAPAB_DLS)) 2956 return; 2957 2958 ASSERT(ill->ill_dls_capab != NULL); 2959 2960 size = sizeof (*dl_subcap) + sizeof (*idls); 2961 2962 mp = allocb(size, BPRI_HI); 2963 if (mp == NULL) { 2964 ip1dbg(("ill_capability_dls_reset: unable to allocate " 2965 "request to disable soft_ring\n")); 2966 return; 2967 } 2968 2969 mp->b_wptr = mp->b_rptr + size; 2970 2971 dl_subcap = (dl_capability_sub_t *)mp->b_rptr; 2972 dl_subcap->dl_length = sizeof (*idls); 2973 if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) 2974 dl_subcap->dl_cap = DL_CAPAB_SOFT_RING; 2975 else 2976 dl_subcap->dl_cap = DL_CAPAB_POLL; 2977 2978 idls = (dl_capab_dls_t *)(dl_subcap + 1); 2979 if (ill->ill_capabilities & ILL_CAPAB_SOFT_RING) 2980 idls->dls_flags = SOFT_RING_DISABLE; 2981 else 2982 idls->dls_flags = POLL_DISABLE; 2983 2984 if (*sc_mp != NULL) 2985 linkb(*sc_mp, mp); 2986 else 2987 *sc_mp = mp; 2988 } 2989 2990 /* 2991 * Process a soft_ring/poll capability negotiation ack received 2992 * from a DLS Provider.isub must point to the sub-capability 2993 * (DL_CAPAB_SOFT_RING/DL_CAPAB_POLL) of a DL_CAPABILITY_ACK message. 2994 */ 2995 static void 2996 ill_capability_dls_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub) 2997 { 2998 dl_capab_dls_t *idls; 2999 uint_t sub_dl_cap = isub->dl_cap; 3000 uint8_t *capend; 3001 3002 ASSERT(sub_dl_cap == DL_CAPAB_SOFT_RING || 3003 sub_dl_cap == DL_CAPAB_POLL); 3004 3005 if (ill->ill_isv6) 3006 return; 3007 3008 /* 3009 * Note: range checks here are not absolutely sufficient to 3010 * make us robust against malformed messages sent by drivers; 3011 * this is in keeping with the rest of IP's dlpi handling. 3012 * (Remember, it's coming from something else in the kernel 3013 * address space) 3014 */ 3015 capend = (uint8_t *)(isub + 1) + isub->dl_length; 3016 if (capend > mp->b_wptr) { 3017 cmn_err(CE_WARN, "ill_capability_dls_ack: " 3018 "malformed sub-capability too long for mblk"); 3019 return; 3020 } 3021 3022 /* 3023 * There are two types of acks we process here: 3024 * 1. acks in reply to a (first form) generic capability req 3025 * (dls_flag will be set to SOFT_RING_CAPABLE or POLL_CAPABLE) 3026 * 2. acks in reply to a SOFT_RING_ENABLE or POLL_ENABLE 3027 * capability req. 3028 */ 3029 idls = (dl_capab_dls_t *)(isub + 1); 3030 3031 if (!dlcapabcheckqid(&idls->dls_mid, ill->ill_lmod_rq)) { 3032 ip1dbg(("ill_capability_dls_ack: mid token for dls " 3033 "capability isn't as expected; pass-thru " 3034 "module(s) detected, discarding capability\n")); 3035 if (ill->ill_capabilities & ILL_CAPAB_DLS) { 3036 /* 3037 * This is a capability renegotitation case. 3038 * The interface better be unusable at this 3039 * point other wise bad things will happen 3040 * if we disable direct calls on a running 3041 * and up interface. 3042 */ 3043 ill_capability_dls_disable(ill); 3044 } 3045 return; 3046 } 3047 3048 switch (idls->dls_flags) { 3049 default: 3050 /* Disable if unknown flag */ 3051 case SOFT_RING_DISABLE: 3052 case POLL_DISABLE: 3053 ill_capability_dls_disable(ill); 3054 break; 3055 case SOFT_RING_CAPABLE: 3056 case POLL_CAPABLE: 3057 /* 3058 * If the capability was already enabled, its safe 3059 * to disable it first to get rid of stale information 3060 * and then start enabling it again. 3061 */ 3062 ill_capability_dls_disable(ill); 3063 ill_capability_dls_capable(ill, idls, isub); 3064 break; 3065 case SOFT_RING_ENABLE: 3066 case POLL_ENABLE: 3067 mutex_enter(&ill->ill_lock); 3068 if (sub_dl_cap == DL_CAPAB_SOFT_RING && 3069 !(ill->ill_capabilities & ILL_CAPAB_SOFT_RING)) { 3070 ASSERT(ill->ill_dls_capab != NULL); 3071 ill->ill_capabilities |= ILL_CAPAB_SOFT_RING; 3072 } 3073 if (sub_dl_cap == DL_CAPAB_POLL && 3074 !(ill->ill_capabilities & ILL_CAPAB_POLL)) { 3075 ASSERT(ill->ill_dls_capab != NULL); 3076 ill->ill_capabilities |= ILL_CAPAB_POLL; 3077 ip1dbg(("ill_capability_dls_ack: interface %s " 3078 "has enabled polling\n", ill->ill_name)); 3079 } 3080 mutex_exit(&ill->ill_lock); 3081 break; 3082 } 3083 } 3084 3085 /* 3086 * Process a hardware checksum offload capability negotiation ack received 3087 * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM) 3088 * of a DL_CAPABILITY_ACK message. 3089 */ 3090 static void 3091 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub) 3092 { 3093 dl_capability_req_t *ocap; 3094 dl_capab_hcksum_t *ihck, *ohck; 3095 ill_hcksum_capab_t **ill_hcksum; 3096 mblk_t *nmp = NULL; 3097 uint_t sub_dl_cap = isub->dl_cap; 3098 uint8_t *capend; 3099 3100 ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM); 3101 3102 ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab; 3103 3104 /* 3105 * Note: range checks here are not absolutely sufficient to 3106 * make us robust against malformed messages sent by drivers; 3107 * this is in keeping with the rest of IP's dlpi handling. 3108 * (Remember, it's coming from something else in the kernel 3109 * address space) 3110 */ 3111 capend = (uint8_t *)(isub + 1) + isub->dl_length; 3112 if (capend > mp->b_wptr) { 3113 cmn_err(CE_WARN, "ill_capability_hcksum_ack: " 3114 "malformed sub-capability too long for mblk"); 3115 return; 3116 } 3117 3118 /* 3119 * There are two types of acks we process here: 3120 * 1. acks in reply to a (first form) generic capability req 3121 * (no ENABLE flag set) 3122 * 2. acks in reply to a ENABLE capability req. 3123 * (ENABLE flag set) 3124 */ 3125 ihck = (dl_capab_hcksum_t *)(isub + 1); 3126 3127 if (ihck->hcksum_version != HCKSUM_VERSION_1) { 3128 cmn_err(CE_CONT, "ill_capability_hcksum_ack: " 3129 "unsupported hardware checksum " 3130 "sub-capability (version %d, expected %d)", 3131 ihck->hcksum_version, HCKSUM_VERSION_1); 3132 return; 3133 } 3134 3135 if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) { 3136 ip1dbg(("ill_capability_hcksum_ack: mid token for hardware " 3137 "checksum capability isn't as expected; pass-thru " 3138 "module(s) detected, discarding capability\n")); 3139 return; 3140 } 3141 3142 #define CURR_HCKSUM_CAPAB \ 3143 (HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 | \ 3144 HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM) 3145 3146 if ((ihck->hcksum_txflags & HCKSUM_ENABLE) && 3147 (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) { 3148 /* do ENABLE processing */ 3149 if (*ill_hcksum == NULL) { 3150 *ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t), 3151 KM_NOSLEEP); 3152 3153 if (*ill_hcksum == NULL) { 3154 cmn_err(CE_WARN, "ill_capability_hcksum_ack: " 3155 "could not enable hcksum version %d " 3156 "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION, 3157 ill->ill_name); 3158 return; 3159 } 3160 } 3161 3162 (*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version; 3163 (*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags; 3164 ill->ill_capabilities |= ILL_CAPAB_HCKSUM; 3165 ip1dbg(("ill_capability_hcksum_ack: interface %s " 3166 "has enabled hardware checksumming\n ", 3167 ill->ill_name)); 3168 } else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) { 3169 /* 3170 * Enabling hardware checksum offload 3171 * Currently IP supports {TCP,UDP}/IPv4 3172 * partial and full cksum offload and 3173 * IPv4 header checksum offload. 3174 * Allocate new mblk which will 3175 * contain a new capability request 3176 * to enable hardware checksum offload. 3177 */ 3178 uint_t size; 3179 uchar_t *rptr; 3180 3181 size = sizeof (dl_capability_req_t) + 3182 sizeof (dl_capability_sub_t) + isub->dl_length; 3183 3184 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) { 3185 cmn_err(CE_WARN, "ill_capability_hcksum_ack: " 3186 "could not enable hardware cksum for %s (ENOMEM)\n", 3187 ill->ill_name); 3188 return; 3189 } 3190 3191 rptr = nmp->b_rptr; 3192 /* initialize dl_capability_req_t */ 3193 ocap = (dl_capability_req_t *)nmp->b_rptr; 3194 ocap->dl_sub_offset = 3195 sizeof (dl_capability_req_t); 3196 ocap->dl_sub_length = 3197 sizeof (dl_capability_sub_t) + 3198 isub->dl_length; 3199 nmp->b_rptr += sizeof (dl_capability_req_t); 3200 3201 /* initialize dl_capability_sub_t */ 3202 bcopy(isub, nmp->b_rptr, sizeof (*isub)); 3203 nmp->b_rptr += sizeof (*isub); 3204 3205 /* initialize dl_capab_hcksum_t */ 3206 ohck = (dl_capab_hcksum_t *)nmp->b_rptr; 3207 bcopy(ihck, ohck, sizeof (*ihck)); 3208 3209 nmp->b_rptr = rptr; 3210 ASSERT(nmp->b_wptr == (nmp->b_rptr + size)); 3211 3212 /* Set ENABLE flag */ 3213 ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB; 3214 ohck->hcksum_txflags |= HCKSUM_ENABLE; 3215 3216 /* 3217 * nmp points to a DL_CAPABILITY_REQ message to enable 3218 * hardware checksum acceleration. 3219 */ 3220 ill_dlpi_send(ill, nmp); 3221 } else { 3222 ip1dbg(("ill_capability_hcksum_ack: interface %s has " 3223 "advertised %x hardware checksum capability flags\n", 3224 ill->ill_name, ihck->hcksum_txflags)); 3225 } 3226 } 3227 3228 static void 3229 ill_capability_hcksum_reset(ill_t *ill, mblk_t **sc_mp) 3230 { 3231 mblk_t *mp; 3232 dl_capab_hcksum_t *hck_subcap; 3233 dl_capability_sub_t *dl_subcap; 3234 int size; 3235 3236 if (!ILL_HCKSUM_CAPABLE(ill)) 3237 return; 3238 3239 ASSERT(ill->ill_hcksum_capab != NULL); 3240 /* 3241 * Clear the capability flag for hardware checksum offload but 3242 * retain the ill_hcksum_capab structure since it's possible that 3243 * another thread is still referring to it. The structure only 3244 * gets deallocated when we destroy the ill. 3245 */ 3246 ill->ill_capabilities &= ~ILL_CAPAB_HCKSUM; 3247 3248 size = sizeof (*dl_subcap) + sizeof (*hck_subcap); 3249 3250 mp = allocb(size, BPRI_HI); 3251 if (mp == NULL) { 3252 ip1dbg(("ill_capability_hcksum_reset: unable to allocate " 3253 "request to disable hardware checksum offload\n")); 3254 return; 3255 } 3256 3257 mp->b_wptr = mp->b_rptr + size; 3258 3259 dl_subcap = (dl_capability_sub_t *)mp->b_rptr; 3260 dl_subcap->dl_cap = DL_CAPAB_HCKSUM; 3261 dl_subcap->dl_length = sizeof (*hck_subcap); 3262 3263 hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1); 3264 hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version; 3265 hck_subcap->hcksum_txflags = 0; 3266 3267 if (*sc_mp != NULL) 3268 linkb(*sc_mp, mp); 3269 else 3270 *sc_mp = mp; 3271 } 3272 3273 static void 3274 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub) 3275 { 3276 mblk_t *nmp = NULL; 3277 dl_capability_req_t *oc; 3278 dl_capab_zerocopy_t *zc_ic, *zc_oc; 3279 ill_zerocopy_capab_t **ill_zerocopy_capab; 3280 uint_t sub_dl_cap = isub->dl_cap; 3281 uint8_t *capend; 3282 3283 ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY); 3284 3285 ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab; 3286 3287 /* 3288 * Note: range checks here are not absolutely sufficient to 3289 * make us robust against malformed messages sent by drivers; 3290 * this is in keeping with the rest of IP's dlpi handling. 3291 * (Remember, it's coming from something else in the kernel 3292 * address space) 3293 */ 3294 capend = (uint8_t *)(isub + 1) + isub->dl_length; 3295 if (capend > mp->b_wptr) { 3296 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: " 3297 "malformed sub-capability too long for mblk"); 3298 return; 3299 } 3300 3301 zc_ic = (dl_capab_zerocopy_t *)(isub + 1); 3302 if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) { 3303 cmn_err(CE_CONT, "ill_capability_zerocopy_ack: " 3304 "unsupported ZEROCOPY sub-capability (version %d, " 3305 "expected %d)", zc_ic->zerocopy_version, 3306 ZEROCOPY_VERSION_1); 3307 return; 3308 } 3309 3310 if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) { 3311 ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy " 3312 "capability isn't as expected; pass-thru module(s) " 3313 "detected, discarding capability\n")); 3314 return; 3315 } 3316 3317 if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) { 3318 if (*ill_zerocopy_capab == NULL) { 3319 *ill_zerocopy_capab = 3320 kmem_zalloc(sizeof (ill_zerocopy_capab_t), 3321 KM_NOSLEEP); 3322 3323 if (*ill_zerocopy_capab == NULL) { 3324 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: " 3325 "could not enable Zero-copy version %d " 3326 "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1, 3327 ill->ill_name); 3328 return; 3329 } 3330 } 3331 3332 ip1dbg(("ill_capability_zerocopy_ack: interface %s " 3333 "supports Zero-copy version %d\n", ill->ill_name, 3334 ZEROCOPY_VERSION_1)); 3335 3336 (*ill_zerocopy_capab)->ill_zerocopy_version = 3337 zc_ic->zerocopy_version; 3338 (*ill_zerocopy_capab)->ill_zerocopy_flags = 3339 zc_ic->zerocopy_flags; 3340 3341 ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY; 3342 } else { 3343 uint_t size; 3344 uchar_t *rptr; 3345 3346 size = sizeof (dl_capability_req_t) + 3347 sizeof (dl_capability_sub_t) + 3348 sizeof (dl_capab_zerocopy_t); 3349 3350 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) { 3351 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: " 3352 "could not enable zerocopy for %s (ENOMEM)\n", 3353 ill->ill_name); 3354 return; 3355 } 3356 3357 rptr = nmp->b_rptr; 3358 /* initialize dl_capability_req_t */ 3359 oc = (dl_capability_req_t *)rptr; 3360 oc->dl_sub_offset = sizeof (dl_capability_req_t); 3361 oc->dl_sub_length = sizeof (dl_capability_sub_t) + 3362 sizeof (dl_capab_zerocopy_t); 3363 rptr += sizeof (dl_capability_req_t); 3364 3365 /* initialize dl_capability_sub_t */ 3366 bcopy(isub, rptr, sizeof (*isub)); 3367 rptr += sizeof (*isub); 3368 3369 /* initialize dl_capab_zerocopy_t */ 3370 zc_oc = (dl_capab_zerocopy_t *)rptr; 3371 *zc_oc = *zc_ic; 3372 3373 ip1dbg(("ill_capability_zerocopy_ack: asking interface %s " 3374 "to enable zero-copy version %d\n", ill->ill_name, 3375 ZEROCOPY_VERSION_1)); 3376 3377 /* set VMSAFE_MEM flag */ 3378 zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM; 3379 3380 /* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */ 3381 ill_dlpi_send(ill, nmp); 3382 } 3383 } 3384 3385 static void 3386 ill_capability_zerocopy_reset(ill_t *ill, mblk_t **sc_mp) 3387 { 3388 mblk_t *mp; 3389 dl_capab_zerocopy_t *zerocopy_subcap; 3390 dl_capability_sub_t *dl_subcap; 3391 int size; 3392 3393 if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY)) 3394 return; 3395 3396 ASSERT(ill->ill_zerocopy_capab != NULL); 3397 /* 3398 * Clear the capability flag for Zero-copy but retain the 3399 * ill_zerocopy_capab structure since it's possible that another 3400 * thread is still referring to it. The structure only gets 3401 * deallocated when we destroy the ill. 3402 */ 3403 ill->ill_capabilities &= ~ILL_CAPAB_ZEROCOPY; 3404 3405 size = sizeof (*dl_subcap) + sizeof (*zerocopy_subcap); 3406 3407 mp = allocb(size, BPRI_HI); 3408 if (mp == NULL) { 3409 ip1dbg(("ill_capability_zerocopy_reset: unable to allocate " 3410 "request to disable Zero-copy\n")); 3411 return; 3412 } 3413 3414 mp->b_wptr = mp->b_rptr + size; 3415 3416 dl_subcap = (dl_capability_sub_t *)mp->b_rptr; 3417 dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY; 3418 dl_subcap->dl_length = sizeof (*zerocopy_subcap); 3419 3420 zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1); 3421 zerocopy_subcap->zerocopy_version = 3422 ill->ill_zerocopy_capab->ill_zerocopy_version; 3423 zerocopy_subcap->zerocopy_flags = 0; 3424 3425 if (*sc_mp != NULL) 3426 linkb(*sc_mp, mp); 3427 else 3428 *sc_mp = mp; 3429 } 3430 3431 /* 3432 * Process Large Segment Offload capability negotiation ack received from a 3433 * DLS Provider. isub must point to the sub-capability (DL_CAPAB_LSO) of a 3434 * DL_CAPABILITY_ACK message. 3435 */ 3436 static void 3437 ill_capability_lso_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub) 3438 { 3439 mblk_t *nmp = NULL; 3440 dl_capability_req_t *oc; 3441 dl_capab_lso_t *lso_ic, *lso_oc; 3442 ill_lso_capab_t **ill_lso_capab; 3443 uint_t sub_dl_cap = isub->dl_cap; 3444 uint8_t *capend; 3445 3446 ASSERT(sub_dl_cap == DL_CAPAB_LSO); 3447 3448 ill_lso_capab = (ill_lso_capab_t **)&ill->ill_lso_capab; 3449 3450 /* 3451 * Note: range checks here are not absolutely sufficient to 3452 * make us robust against malformed messages sent by drivers; 3453 * this is in keeping with the rest of IP's dlpi handling. 3454 * (Remember, it's coming from something else in the kernel 3455 * address space) 3456 */ 3457 capend = (uint8_t *)(isub + 1) + isub->dl_length; 3458 if (capend > mp->b_wptr) { 3459 cmn_err(CE_WARN, "ill_capability_lso_ack: " 3460 "malformed sub-capability too long for mblk"); 3461 return; 3462 } 3463 3464 lso_ic = (dl_capab_lso_t *)(isub + 1); 3465 3466 if (lso_ic->lso_version != LSO_VERSION_1) { 3467 cmn_err(CE_CONT, "ill_capability_lso_ack: " 3468 "unsupported LSO sub-capability (version %d, expected %d)", 3469 lso_ic->lso_version, LSO_VERSION_1); 3470 return; 3471 } 3472 3473 if (!dlcapabcheckqid(&lso_ic->lso_mid, ill->ill_lmod_rq)) { 3474 ip1dbg(("ill_capability_lso_ack: mid token for LSO " 3475 "capability isn't as expected; pass-thru module(s) " 3476 "detected, discarding capability\n")); 3477 return; 3478 } 3479 3480 if ((lso_ic->lso_flags & LSO_TX_ENABLE) && 3481 (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4)) { 3482 if (*ill_lso_capab == NULL) { 3483 *ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t), 3484 KM_NOSLEEP); 3485 3486 if (*ill_lso_capab == NULL) { 3487 cmn_err(CE_WARN, "ill_capability_lso_ack: " 3488 "could not enable LSO version %d " 3489 "for %s (ENOMEM)\n", LSO_VERSION_1, 3490 ill->ill_name); 3491 return; 3492 } 3493 } 3494 3495 (*ill_lso_capab)->ill_lso_version = lso_ic->lso_version; 3496 (*ill_lso_capab)->ill_lso_flags = lso_ic->lso_flags; 3497 (*ill_lso_capab)->ill_lso_max = lso_ic->lso_max; 3498 ill->ill_capabilities |= ILL_CAPAB_LSO; 3499 3500 ip1dbg(("ill_capability_lso_ack: interface %s " 3501 "has enabled LSO\n ", ill->ill_name)); 3502 } else if (lso_ic->lso_flags & LSO_TX_BASIC_TCP_IPV4) { 3503 uint_t size; 3504 uchar_t *rptr; 3505 3506 size = sizeof (dl_capability_req_t) + 3507 sizeof (dl_capability_sub_t) + sizeof (dl_capab_lso_t); 3508 3509 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) { 3510 cmn_err(CE_WARN, "ill_capability_lso_ack: " 3511 "could not enable LSO for %s (ENOMEM)\n", 3512 ill->ill_name); 3513 return; 3514 } 3515 3516 rptr = nmp->b_rptr; 3517 /* initialize dl_capability_req_t */ 3518 oc = (dl_capability_req_t *)nmp->b_rptr; 3519 oc->dl_sub_offset = sizeof (dl_capability_req_t); 3520 oc->dl_sub_length = sizeof (dl_capability_sub_t) + 3521 sizeof (dl_capab_lso_t); 3522 nmp->b_rptr += sizeof (dl_capability_req_t); 3523 3524 /* initialize dl_capability_sub_t */ 3525 bcopy(isub, nmp->b_rptr, sizeof (*isub)); 3526 nmp->b_rptr += sizeof (*isub); 3527 3528 /* initialize dl_capab_lso_t */ 3529 lso_oc = (dl_capab_lso_t *)nmp->b_rptr; 3530 bcopy(lso_ic, lso_oc, sizeof (*lso_ic)); 3531 3532 nmp->b_rptr = rptr; 3533 ASSERT(nmp->b_wptr == (nmp->b_rptr + size)); 3534 3535 /* set ENABLE flag */ 3536 lso_oc->lso_flags |= LSO_TX_ENABLE; 3537 3538 /* nmp points to a DL_CAPABILITY_REQ message to enable LSO */ 3539 ill_dlpi_send(ill, nmp); 3540 } else { 3541 ip1dbg(("ill_capability_lso_ack: interface %s has " 3542 "advertised %x LSO capability flags\n", 3543 ill->ill_name, lso_ic->lso_flags)); 3544 } 3545 } 3546 3547 3548 static void 3549 ill_capability_lso_reset(ill_t *ill, mblk_t **sc_mp) 3550 { 3551 mblk_t *mp; 3552 dl_capab_lso_t *lso_subcap; 3553 dl_capability_sub_t *dl_subcap; 3554 int size; 3555 3556 if (!(ill->ill_capabilities & ILL_CAPAB_LSO)) 3557 return; 3558 3559 ASSERT(ill->ill_lso_capab != NULL); 3560 /* 3561 * Clear the capability flag for LSO but retain the 3562 * ill_lso_capab structure since it's possible that another 3563 * thread is still referring to it. The structure only gets 3564 * deallocated when we destroy the ill. 3565 */ 3566 ill->ill_capabilities &= ~ILL_CAPAB_LSO; 3567 3568 size = sizeof (*dl_subcap) + sizeof (*lso_subcap); 3569 3570 mp = allocb(size, BPRI_HI); 3571 if (mp == NULL) { 3572 ip1dbg(("ill_capability_lso_reset: unable to allocate " 3573 "request to disable LSO\n")); 3574 return; 3575 } 3576 3577 mp->b_wptr = mp->b_rptr + size; 3578 3579 dl_subcap = (dl_capability_sub_t *)mp->b_rptr; 3580 dl_subcap->dl_cap = DL_CAPAB_LSO; 3581 dl_subcap->dl_length = sizeof (*lso_subcap); 3582 3583 lso_subcap = (dl_capab_lso_t *)(dl_subcap + 1); 3584 lso_subcap->lso_version = ill->ill_lso_capab->ill_lso_version; 3585 lso_subcap->lso_flags = 0; 3586 3587 if (*sc_mp != NULL) 3588 linkb(*sc_mp, mp); 3589 else 3590 *sc_mp = mp; 3591 } 3592 3593 /* 3594 * Consume a new-style hardware capabilities negotiation ack. 3595 * Called from ip_rput_dlpi_writer(). 3596 */ 3597 void 3598 ill_capability_ack(ill_t *ill, mblk_t *mp) 3599 { 3600 dl_capability_ack_t *capp; 3601 dl_capability_sub_t *subp, *endp; 3602 3603 if (ill->ill_dlpi_capab_state == IDS_INPROGRESS) 3604 ill->ill_dlpi_capab_state = IDS_OK; 3605 3606 capp = (dl_capability_ack_t *)mp->b_rptr; 3607 3608 if (capp->dl_sub_length == 0) 3609 /* no new-style capabilities */ 3610 return; 3611 3612 /* make sure the driver supplied correct dl_sub_length */ 3613 if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) { 3614 ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, " 3615 "invalid dl_sub_length (%d)\n", capp->dl_sub_length)); 3616 return; 3617 } 3618 3619 #define SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset)) 3620 /* 3621 * There are sub-capabilities. Process the ones we know about. 3622 * Loop until we don't have room for another sub-cap header.. 3623 */ 3624 for (subp = SC(capp, capp->dl_sub_offset), 3625 endp = SC(subp, capp->dl_sub_length - sizeof (*subp)); 3626 subp <= endp; 3627 subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) { 3628 3629 switch (subp->dl_cap) { 3630 case DL_CAPAB_ID_WRAPPER: 3631 ill_capability_id_ack(ill, mp, subp); 3632 break; 3633 default: 3634 ill_capability_dispatch(ill, mp, subp, B_FALSE); 3635 break; 3636 } 3637 } 3638 #undef SC 3639 } 3640 3641 /* 3642 * This routine is called to scan the fragmentation reassembly table for 3643 * the specified ILL for any packets that are starting to smell. 3644 * dead_interval is the maximum time in seconds that will be tolerated. It 3645 * will either be the value specified in ip_g_frag_timeout, or zero if the 3646 * ILL is shutting down and it is time to blow everything off. 3647 * 3648 * It returns the number of seconds (as a time_t) that the next frag timer 3649 * should be scheduled for, 0 meaning that the timer doesn't need to be 3650 * re-started. Note that the method of calculating next_timeout isn't 3651 * entirely accurate since time will flow between the time we grab 3652 * current_time and the time we schedule the next timeout. This isn't a 3653 * big problem since this is the timer for sending an ICMP reassembly time 3654 * exceeded messages, and it doesn't have to be exactly accurate. 3655 * 3656 * This function is 3657 * sometimes called as writer, although this is not required. 3658 */ 3659 time_t 3660 ill_frag_timeout(ill_t *ill, time_t dead_interval) 3661 { 3662 ipfb_t *ipfb; 3663 ipfb_t *endp; 3664 ipf_t *ipf; 3665 ipf_t *ipfnext; 3666 mblk_t *mp; 3667 time_t current_time = gethrestime_sec(); 3668 time_t next_timeout = 0; 3669 uint32_t hdr_length; 3670 mblk_t *send_icmp_head; 3671 mblk_t *send_icmp_head_v6; 3672 zoneid_t zoneid; 3673 ip_stack_t *ipst = ill->ill_ipst; 3674 3675 ipfb = ill->ill_frag_hash_tbl; 3676 if (ipfb == NULL) 3677 return (B_FALSE); 3678 endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT]; 3679 /* Walk the frag hash table. */ 3680 for (; ipfb < endp; ipfb++) { 3681 send_icmp_head = NULL; 3682 send_icmp_head_v6 = NULL; 3683 mutex_enter(&ipfb->ipfb_lock); 3684 while ((ipf = ipfb->ipfb_ipf) != 0) { 3685 time_t frag_time = current_time - ipf->ipf_timestamp; 3686 time_t frag_timeout; 3687 3688 if (frag_time < dead_interval) { 3689 /* 3690 * There are some outstanding fragments 3691 * that will timeout later. Make note of 3692 * the time so that we can reschedule the 3693 * next timeout appropriately. 3694 */ 3695 frag_timeout = dead_interval - frag_time; 3696 if (next_timeout == 0 || 3697 frag_timeout < next_timeout) { 3698 next_timeout = frag_timeout; 3699 } 3700 break; 3701 } 3702 /* Time's up. Get it out of here. */ 3703 hdr_length = ipf->ipf_nf_hdr_len; 3704 ipfnext = ipf->ipf_hash_next; 3705 if (ipfnext) 3706 ipfnext->ipf_ptphn = ipf->ipf_ptphn; 3707 *ipf->ipf_ptphn = ipfnext; 3708 mp = ipf->ipf_mp->b_cont; 3709 for (; mp; mp = mp->b_cont) { 3710 /* Extra points for neatness. */ 3711 IP_REASS_SET_START(mp, 0); 3712 IP_REASS_SET_END(mp, 0); 3713 } 3714 mp = ipf->ipf_mp->b_cont; 3715 ill->ill_frag_count -= ipf->ipf_count; 3716 ASSERT(ipfb->ipfb_count >= ipf->ipf_count); 3717 ipfb->ipfb_count -= ipf->ipf_count; 3718 ASSERT(ipfb->ipfb_frag_pkts > 0); 3719 ipfb->ipfb_frag_pkts--; 3720 /* 3721 * We do not send any icmp message from here because 3722 * we currently are holding the ipfb_lock for this 3723 * hash chain. If we try and send any icmp messages 3724 * from here we may end up via a put back into ip 3725 * trying to get the same lock, causing a recursive 3726 * mutex panic. Instead we build a list and send all 3727 * the icmp messages after we have dropped the lock. 3728 */ 3729 if (ill->ill_isv6) { 3730 if (hdr_length != 0) { 3731 mp->b_next = send_icmp_head_v6; 3732 send_icmp_head_v6 = mp; 3733 } else { 3734 freemsg(mp); 3735 } 3736 } else { 3737 if (hdr_length != 0) { 3738 mp->b_next = send_icmp_head; 3739 send_icmp_head = mp; 3740 } else { 3741 freemsg(mp); 3742 } 3743 } 3744 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails); 3745 freeb(ipf->ipf_mp); 3746 } 3747 mutex_exit(&ipfb->ipfb_lock); 3748 /* 3749 * Now need to send any icmp messages that we delayed from 3750 * above. 3751 */ 3752 while (send_icmp_head_v6 != NULL) { 3753 ip6_t *ip6h; 3754 3755 mp = send_icmp_head_v6; 3756 send_icmp_head_v6 = send_icmp_head_v6->b_next; 3757 mp->b_next = NULL; 3758 if (mp->b_datap->db_type == M_CTL) 3759 ip6h = (ip6_t *)mp->b_cont->b_rptr; 3760 else 3761 ip6h = (ip6_t *)mp->b_rptr; 3762 zoneid = ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst, 3763 ill, ipst); 3764 if (zoneid == ALL_ZONES) { 3765 freemsg(mp); 3766 } else { 3767 icmp_time_exceeded_v6(ill->ill_wq, mp, 3768 ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE, 3769 B_FALSE, zoneid, ipst); 3770 } 3771 } 3772 while (send_icmp_head != NULL) { 3773 ipaddr_t dst; 3774 3775 mp = send_icmp_head; 3776 send_icmp_head = send_icmp_head->b_next; 3777 mp->b_next = NULL; 3778 3779 if (mp->b_datap->db_type == M_CTL) 3780 dst = ((ipha_t *)mp->b_cont->b_rptr)->ipha_dst; 3781 else 3782 dst = ((ipha_t *)mp->b_rptr)->ipha_dst; 3783 3784 zoneid = ipif_lookup_addr_zoneid(dst, ill, ipst); 3785 if (zoneid == ALL_ZONES) { 3786 freemsg(mp); 3787 } else { 3788 icmp_time_exceeded(ill->ill_wq, mp, 3789 ICMP_REASSEMBLY_TIME_EXCEEDED, zoneid, 3790 ipst); 3791 } 3792 } 3793 } 3794 /* 3795 * A non-dying ILL will use the return value to decide whether to 3796 * restart the frag timer, and for how long. 3797 */ 3798 return (next_timeout); 3799 } 3800 3801 /* 3802 * This routine is called when the approximate count of mblk memory used 3803 * for the specified ILL has exceeded max_count. 3804 */ 3805 void 3806 ill_frag_prune(ill_t *ill, uint_t max_count) 3807 { 3808 ipfb_t *ipfb; 3809 ipf_t *ipf; 3810 size_t count; 3811 3812 /* 3813 * If we are here within ip_min_frag_prune_time msecs remove 3814 * ill_frag_free_num_pkts oldest packets from each bucket and increment 3815 * ill_frag_free_num_pkts. 3816 */ 3817 mutex_enter(&ill->ill_lock); 3818 if (TICK_TO_MSEC(lbolt - ill->ill_last_frag_clean_time) <= 3819 (ip_min_frag_prune_time != 0 ? 3820 ip_min_frag_prune_time : msec_per_tick)) { 3821 3822 ill->ill_frag_free_num_pkts++; 3823 3824 } else { 3825 ill->ill_frag_free_num_pkts = 0; 3826 } 3827 ill->ill_last_frag_clean_time = lbolt; 3828 mutex_exit(&ill->ill_lock); 3829 3830 /* 3831 * free ill_frag_free_num_pkts oldest packets from each bucket. 3832 */ 3833 if (ill->ill_frag_free_num_pkts != 0) { 3834 int ix; 3835 3836 for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) { 3837 ipfb = &ill->ill_frag_hash_tbl[ix]; 3838 mutex_enter(&ipfb->ipfb_lock); 3839 if (ipfb->ipfb_ipf != NULL) { 3840 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 3841 ill->ill_frag_free_num_pkts); 3842 } 3843 mutex_exit(&ipfb->ipfb_lock); 3844 } 3845 } 3846 /* 3847 * While the reassembly list for this ILL is too big, prune a fragment 3848 * queue by age, oldest first. Note that the per ILL count is 3849 * approximate, while the per frag hash bucket counts are accurate. 3850 */ 3851 while (ill->ill_frag_count > max_count) { 3852 int ix; 3853 ipfb_t *oipfb = NULL; 3854 uint_t oldest = UINT_MAX; 3855 3856 count = 0; 3857 for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) { 3858 ipfb = &ill->ill_frag_hash_tbl[ix]; 3859 mutex_enter(&ipfb->ipfb_lock); 3860 ipf = ipfb->ipfb_ipf; 3861 if (ipf != NULL && ipf->ipf_gen < oldest) { 3862 oldest = ipf->ipf_gen; 3863 oipfb = ipfb; 3864 } 3865 count += ipfb->ipfb_count; 3866 mutex_exit(&ipfb->ipfb_lock); 3867 } 3868 /* Refresh the per ILL count */ 3869 ill->ill_frag_count = count; 3870 if (oipfb == NULL) { 3871 ill->ill_frag_count = 0; 3872 break; 3873 } 3874 if (count <= max_count) 3875 return; /* Somebody beat us to it, nothing to do */ 3876 mutex_enter(&oipfb->ipfb_lock); 3877 ipf = oipfb->ipfb_ipf; 3878 if (ipf != NULL) { 3879 ill_frag_free_pkts(ill, oipfb, ipf, 1); 3880 } 3881 mutex_exit(&oipfb->ipfb_lock); 3882 } 3883 } 3884 3885 /* 3886 * free 'free_cnt' fragmented packets starting at ipf. 3887 */ 3888 void 3889 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt) 3890 { 3891 size_t count; 3892 mblk_t *mp; 3893 mblk_t *tmp; 3894 ipf_t **ipfp = ipf->ipf_ptphn; 3895 3896 ASSERT(MUTEX_HELD(&ipfb->ipfb_lock)); 3897 ASSERT(ipfp != NULL); 3898 ASSERT(ipf != NULL); 3899 3900 while (ipf != NULL && free_cnt-- > 0) { 3901 count = ipf->ipf_count; 3902 mp = ipf->ipf_mp; 3903 ipf = ipf->ipf_hash_next; 3904 for (tmp = mp; tmp; tmp = tmp->b_cont) { 3905 IP_REASS_SET_START(tmp, 0); 3906 IP_REASS_SET_END(tmp, 0); 3907 } 3908 ill->ill_frag_count -= count; 3909 ASSERT(ipfb->ipfb_count >= count); 3910 ipfb->ipfb_count -= count; 3911 ASSERT(ipfb->ipfb_frag_pkts > 0); 3912 ipfb->ipfb_frag_pkts--; 3913 freemsg(mp); 3914 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails); 3915 } 3916 3917 if (ipf) 3918 ipf->ipf_ptphn = ipfp; 3919 ipfp[0] = ipf; 3920 } 3921 3922 #define ND_FORWARD_WARNING "The <if>:ip*_forwarding ndd variables are " \ 3923 "obsolete and may be removed in a future release of Solaris. Use " \ 3924 "ifconfig(1M) to manipulate the forwarding status of an interface." 3925 3926 /* 3927 * For obsolete per-interface forwarding configuration; 3928 * called in response to ND_GET. 3929 */ 3930 /* ARGSUSED */ 3931 static int 3932 nd_ill_forward_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *ioc_cr) 3933 { 3934 ill_t *ill = (ill_t *)cp; 3935 3936 cmn_err(CE_WARN, ND_FORWARD_WARNING); 3937 3938 (void) mi_mpprintf(mp, "%d", (ill->ill_flags & ILLF_ROUTER) != 0); 3939 return (0); 3940 } 3941 3942 /* 3943 * For obsolete per-interface forwarding configuration; 3944 * called in response to ND_SET. 3945 */ 3946 /* ARGSUSED */ 3947 static int 3948 nd_ill_forward_set(queue_t *q, mblk_t *mp, char *valuestr, caddr_t cp, 3949 cred_t *ioc_cr) 3950 { 3951 long value; 3952 int retval; 3953 ip_stack_t *ipst = CONNQ_TO_IPST(q); 3954 3955 cmn_err(CE_WARN, ND_FORWARD_WARNING); 3956 3957 if (ddi_strtol(valuestr, NULL, 10, &value) != 0 || 3958 value < 0 || value > 1) { 3959 return (EINVAL); 3960 } 3961 3962 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 3963 retval = ill_forward_set((ill_t *)cp, (value != 0)); 3964 rw_exit(&ipst->ips_ill_g_lock); 3965 return (retval); 3966 } 3967 3968 /* 3969 * Set an ill's ILLF_ROUTER flag appropriately. If the ill is part of an 3970 * IPMP group, make sure all ill's in the group adopt the new policy. Send 3971 * up RTS_IFINFO routing socket messages for each interface whose flags we 3972 * change. 3973 */ 3974 int 3975 ill_forward_set(ill_t *ill, boolean_t enable) 3976 { 3977 ill_group_t *illgrp; 3978 ip_stack_t *ipst = ill->ill_ipst; 3979 3980 ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock)); 3981 3982 if ((enable && (ill->ill_flags & ILLF_ROUTER)) || 3983 (!enable && !(ill->ill_flags & ILLF_ROUTER))) 3984 return (0); 3985 3986 if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) 3987 return (EINVAL); 3988 3989 /* 3990 * If the ill is in an IPMP group, set the forwarding policy on all 3991 * members of the group to the same value. 3992 */ 3993 illgrp = ill->ill_group; 3994 if (illgrp != NULL) { 3995 ill_t *tmp_ill; 3996 3997 for (tmp_ill = illgrp->illgrp_ill; tmp_ill != NULL; 3998 tmp_ill = tmp_ill->ill_group_next) { 3999 ip1dbg(("ill_forward_set: %s %s forwarding on %s", 4000 (enable ? "Enabling" : "Disabling"), 4001 (tmp_ill->ill_isv6 ? "IPv6" : "IPv4"), 4002 tmp_ill->ill_name)); 4003 mutex_enter(&tmp_ill->ill_lock); 4004 if (enable) 4005 tmp_ill->ill_flags |= ILLF_ROUTER; 4006 else 4007 tmp_ill->ill_flags &= ~ILLF_ROUTER; 4008 mutex_exit(&tmp_ill->ill_lock); 4009 if (tmp_ill->ill_isv6) 4010 ill_set_nce_router_flags(tmp_ill, enable); 4011 /* Notify routing socket listeners of this change. */ 4012 ip_rts_ifmsg(tmp_ill->ill_ipif); 4013 } 4014 } else { 4015 ip1dbg(("ill_forward_set: %s %s forwarding on %s", 4016 (enable ? "Enabling" : "Disabling"), 4017 (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name)); 4018 mutex_enter(&ill->ill_lock); 4019 if (enable) 4020 ill->ill_flags |= ILLF_ROUTER; 4021 else 4022 ill->ill_flags &= ~ILLF_ROUTER; 4023 mutex_exit(&ill->ill_lock); 4024 if (ill->ill_isv6) 4025 ill_set_nce_router_flags(ill, enable); 4026 /* Notify routing socket listeners of this change. */ 4027 ip_rts_ifmsg(ill->ill_ipif); 4028 } 4029 4030 return (0); 4031 } 4032 4033 /* 4034 * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for 4035 * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately 4036 * set or clear. 4037 */ 4038 static void 4039 ill_set_nce_router_flags(ill_t *ill, boolean_t enable) 4040 { 4041 ipif_t *ipif; 4042 nce_t *nce; 4043 4044 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 4045 nce = ndp_lookup_v6(ill, &ipif->ipif_v6lcl_addr, B_FALSE); 4046 if (nce != NULL) { 4047 mutex_enter(&nce->nce_lock); 4048 if (enable) 4049 nce->nce_flags |= NCE_F_ISROUTER; 4050 else 4051 nce->nce_flags &= ~NCE_F_ISROUTER; 4052 mutex_exit(&nce->nce_lock); 4053 NCE_REFRELE(nce); 4054 } 4055 } 4056 } 4057 4058 /* 4059 * Given an ill with a _valid_ name, add the ip_forwarding ndd variable 4060 * for this ill. Make sure the v6/v4 question has been answered about this 4061 * ill. The creation of this ndd variable is only for backwards compatibility. 4062 * The preferred way to control per-interface IP forwarding is through the 4063 * ILLF_ROUTER interface flag. 4064 */ 4065 static int 4066 ill_set_ndd_name(ill_t *ill) 4067 { 4068 char *suffix; 4069 ip_stack_t *ipst = ill->ill_ipst; 4070 4071 ASSERT(IAM_WRITER_ILL(ill)); 4072 4073 if (ill->ill_isv6) 4074 suffix = ipv6_forward_suffix; 4075 else 4076 suffix = ipv4_forward_suffix; 4077 4078 ill->ill_ndd_name = ill->ill_name + ill->ill_name_length; 4079 bcopy(ill->ill_name, ill->ill_ndd_name, ill->ill_name_length - 1); 4080 /* 4081 * Copies over the '\0'. 4082 * Note that strlen(suffix) is always bounded. 4083 */ 4084 bcopy(suffix, ill->ill_ndd_name + ill->ill_name_length - 1, 4085 strlen(suffix) + 1); 4086 4087 /* 4088 * Use of the nd table requires holding the reader lock. 4089 * Modifying the nd table thru nd_load/nd_unload requires 4090 * the writer lock. 4091 */ 4092 rw_enter(&ipst->ips_ip_g_nd_lock, RW_WRITER); 4093 if (!nd_load(&ipst->ips_ip_g_nd, ill->ill_ndd_name, nd_ill_forward_get, 4094 nd_ill_forward_set, (caddr_t)ill)) { 4095 /* 4096 * If the nd_load failed, it only meant that it could not 4097 * allocate a new bunch of room for further NDD expansion. 4098 * Because of that, the ill_ndd_name will be set to 0, and 4099 * this interface is at the mercy of the global ip_forwarding 4100 * variable. 4101 */ 4102 rw_exit(&ipst->ips_ip_g_nd_lock); 4103 ill->ill_ndd_name = NULL; 4104 return (ENOMEM); 4105 } 4106 rw_exit(&ipst->ips_ip_g_nd_lock); 4107 return (0); 4108 } 4109 4110 /* 4111 * Intializes the context structure and returns the first ill in the list 4112 * cuurently start_list and end_list can have values: 4113 * MAX_G_HEADS Traverse both IPV4 and IPV6 lists. 4114 * IP_V4_G_HEAD Traverse IPV4 list only. 4115 * IP_V6_G_HEAD Traverse IPV6 list only. 4116 */ 4117 4118 /* 4119 * We don't check for CONDEMNED ills here. Caller must do that if 4120 * necessary under the ill lock. 4121 */ 4122 ill_t * 4123 ill_first(int start_list, int end_list, ill_walk_context_t *ctx, 4124 ip_stack_t *ipst) 4125 { 4126 ill_if_t *ifp; 4127 ill_t *ill; 4128 avl_tree_t *avl_tree; 4129 4130 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock)); 4131 ASSERT(end_list <= MAX_G_HEADS && start_list >= 0); 4132 4133 /* 4134 * setup the lists to search 4135 */ 4136 if (end_list != MAX_G_HEADS) { 4137 ctx->ctx_current_list = start_list; 4138 ctx->ctx_last_list = end_list; 4139 } else { 4140 ctx->ctx_last_list = MAX_G_HEADS - 1; 4141 ctx->ctx_current_list = 0; 4142 } 4143 4144 while (ctx->ctx_current_list <= ctx->ctx_last_list) { 4145 ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst); 4146 if (ifp != (ill_if_t *) 4147 &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) { 4148 avl_tree = &ifp->illif_avl_by_ppa; 4149 ill = avl_first(avl_tree); 4150 /* 4151 * ill is guaranteed to be non NULL or ifp should have 4152 * not existed. 4153 */ 4154 ASSERT(ill != NULL); 4155 return (ill); 4156 } 4157 ctx->ctx_current_list++; 4158 } 4159 4160 return (NULL); 4161 } 4162 4163 /* 4164 * returns the next ill in the list. ill_first() must have been called 4165 * before calling ill_next() or bad things will happen. 4166 */ 4167 4168 /* 4169 * We don't check for CONDEMNED ills here. Caller must do that if 4170 * necessary under the ill lock. 4171 */ 4172 ill_t * 4173 ill_next(ill_walk_context_t *ctx, ill_t *lastill) 4174 { 4175 ill_if_t *ifp; 4176 ill_t *ill; 4177 ip_stack_t *ipst = lastill->ill_ipst; 4178 4179 ASSERT(lastill->ill_ifptr != (ill_if_t *) 4180 &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)); 4181 if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill, 4182 AVL_AFTER)) != NULL) { 4183 return (ill); 4184 } 4185 4186 /* goto next ill_ifp in the list. */ 4187 ifp = lastill->ill_ifptr->illif_next; 4188 4189 /* make sure not at end of circular list */ 4190 while (ifp == 4191 (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) { 4192 if (++ctx->ctx_current_list > ctx->ctx_last_list) 4193 return (NULL); 4194 ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst); 4195 } 4196 4197 return (avl_first(&ifp->illif_avl_by_ppa)); 4198 } 4199 4200 /* 4201 * Check interface name for correct format which is name+ppa. 4202 * name can contain characters and digits, the right most digits 4203 * make up the ppa number. use of octal is not allowed, name must contain 4204 * a ppa, return pointer to the start of ppa. 4205 * In case of error return NULL. 4206 */ 4207 static char * 4208 ill_get_ppa_ptr(char *name) 4209 { 4210 int namelen = mi_strlen(name); 4211 4212 int len = namelen; 4213 4214 name += len; 4215 while (len > 0) { 4216 name--; 4217 if (*name < '0' || *name > '9') 4218 break; 4219 len--; 4220 } 4221 4222 /* empty string, all digits, or no trailing digits */ 4223 if (len == 0 || len == (int)namelen) 4224 return (NULL); 4225 4226 name++; 4227 /* check for attempted use of octal */ 4228 if (*name == '0' && len != (int)namelen - 1) 4229 return (NULL); 4230 return (name); 4231 } 4232 4233 /* 4234 * use avl tree to locate the ill. 4235 */ 4236 static ill_t * 4237 ill_find_by_name(char *name, boolean_t isv6, queue_t *q, mblk_t *mp, 4238 ipsq_func_t func, int *error, ip_stack_t *ipst) 4239 { 4240 char *ppa_ptr = NULL; 4241 int len; 4242 uint_t ppa; 4243 ill_t *ill = NULL; 4244 ill_if_t *ifp; 4245 int list; 4246 ipsq_t *ipsq; 4247 4248 if (error != NULL) 4249 *error = 0; 4250 4251 /* 4252 * get ppa ptr 4253 */ 4254 if (isv6) 4255 list = IP_V6_G_HEAD; 4256 else 4257 list = IP_V4_G_HEAD; 4258 4259 if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) { 4260 if (error != NULL) 4261 *error = ENXIO; 4262 return (NULL); 4263 } 4264 4265 len = ppa_ptr - name + 1; 4266 4267 ppa = stoi(&ppa_ptr); 4268 4269 ifp = IP_VX_ILL_G_LIST(list, ipst); 4270 4271 while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) { 4272 /* 4273 * match is done on len - 1 as the name is not null 4274 * terminated it contains ppa in addition to the interface 4275 * name. 4276 */ 4277 if ((ifp->illif_name_len == len) && 4278 bcmp(ifp->illif_name, name, len - 1) == 0) { 4279 break; 4280 } else { 4281 ifp = ifp->illif_next; 4282 } 4283 } 4284 4285 4286 if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) { 4287 /* 4288 * Even the interface type does not exist. 4289 */ 4290 if (error != NULL) 4291 *error = ENXIO; 4292 return (NULL); 4293 } 4294 4295 ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL); 4296 if (ill != NULL) { 4297 /* 4298 * The block comment at the start of ipif_down 4299 * explains the use of the macros used below 4300 */ 4301 GRAB_CONN_LOCK(q); 4302 mutex_enter(&ill->ill_lock); 4303 if (ILL_CAN_LOOKUP(ill)) { 4304 ill_refhold_locked(ill); 4305 mutex_exit(&ill->ill_lock); 4306 RELEASE_CONN_LOCK(q); 4307 return (ill); 4308 } else if (ILL_CAN_WAIT(ill, q)) { 4309 ipsq = ill->ill_phyint->phyint_ipsq; 4310 mutex_enter(&ipsq->ipsq_lock); 4311 mutex_exit(&ill->ill_lock); 4312 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill); 4313 mutex_exit(&ipsq->ipsq_lock); 4314 RELEASE_CONN_LOCK(q); 4315 *error = EINPROGRESS; 4316 return (NULL); 4317 } 4318 mutex_exit(&ill->ill_lock); 4319 RELEASE_CONN_LOCK(q); 4320 } 4321 if (error != NULL) 4322 *error = ENXIO; 4323 return (NULL); 4324 } 4325 4326 /* 4327 * comparison function for use with avl. 4328 */ 4329 static int 4330 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr) 4331 { 4332 uint_t ppa; 4333 uint_t ill_ppa; 4334 4335 ASSERT(ppa_ptr != NULL && ill_ptr != NULL); 4336 4337 ppa = *((uint_t *)ppa_ptr); 4338 ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa; 4339 /* 4340 * We want the ill with the lowest ppa to be on the 4341 * top. 4342 */ 4343 if (ill_ppa < ppa) 4344 return (1); 4345 if (ill_ppa > ppa) 4346 return (-1); 4347 return (0); 4348 } 4349 4350 /* 4351 * remove an interface type from the global list. 4352 */ 4353 static void 4354 ill_delete_interface_type(ill_if_t *interface) 4355 { 4356 ASSERT(interface != NULL); 4357 ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0); 4358 4359 avl_destroy(&interface->illif_avl_by_ppa); 4360 if (interface->illif_ppa_arena != NULL) 4361 vmem_destroy(interface->illif_ppa_arena); 4362 4363 remque(interface); 4364 4365 mi_free(interface); 4366 } 4367 4368 /* Defined in ip_netinfo.c */ 4369 extern ddi_taskq_t *eventq_queue_nic; 4370 4371 /* 4372 * remove ill from the global list. 4373 */ 4374 static void 4375 ill_glist_delete(ill_t *ill) 4376 { 4377 char *nicname; 4378 size_t nicnamelen; 4379 hook_nic_event_t *info; 4380 ip_stack_t *ipst; 4381 4382 if (ill == NULL) 4383 return; 4384 ipst = ill->ill_ipst; 4385 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 4386 4387 if (ill->ill_name != NULL) { 4388 nicname = kmem_alloc(ill->ill_name_length, KM_NOSLEEP); 4389 if (nicname != NULL) { 4390 bcopy(ill->ill_name, nicname, ill->ill_name_length); 4391 nicnamelen = ill->ill_name_length; 4392 } 4393 } else { 4394 nicname = NULL; 4395 nicnamelen = 0; 4396 } 4397 4398 /* 4399 * If the ill was never inserted into the AVL tree 4400 * we skip the if branch. 4401 */ 4402 if (ill->ill_ifptr != NULL) { 4403 /* 4404 * remove from AVL tree and free ppa number 4405 */ 4406 avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill); 4407 4408 if (ill->ill_ifptr->illif_ppa_arena != NULL) { 4409 vmem_free(ill->ill_ifptr->illif_ppa_arena, 4410 (void *)(uintptr_t)(ill->ill_ppa+1), 1); 4411 } 4412 if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) { 4413 ill_delete_interface_type(ill->ill_ifptr); 4414 } 4415 4416 /* 4417 * Indicate ill is no longer in the list. 4418 */ 4419 ill->ill_ifptr = NULL; 4420 ill->ill_name_length = 0; 4421 ill->ill_name[0] = '\0'; 4422 ill->ill_ppa = UINT_MAX; 4423 } 4424 4425 /* 4426 * Run the unplumb hook after the NIC has disappeared from being 4427 * visible so that attempts to revalidate its existance will fail. 4428 * 4429 * This needs to be run inside the ill_g_lock perimeter to ensure 4430 * that the ordering of delivered events to listeners matches the 4431 * order of them in the kernel. 4432 */ 4433 if ((info = ill->ill_nic_event_info) != NULL) { 4434 if (info->hne_event != NE_DOWN) { 4435 ip2dbg(("ill_glist_delete: unexpected nic event %d " 4436 "attached for %s\n", info->hne_event, 4437 ill->ill_name)); 4438 if (info->hne_data != NULL) 4439 kmem_free(info->hne_data, info->hne_datalen); 4440 kmem_free(info, sizeof (hook_nic_event_t)); 4441 } else { 4442 if (ddi_taskq_dispatch(eventq_queue_nic, 4443 ip_ne_queue_func, (void *)info, DDI_SLEEP) 4444 == DDI_FAILURE) { 4445 ip2dbg(("ill_glist_delete: ddi_taskq_dispatch " 4446 "failed\n")); 4447 if (info->hne_data != NULL) 4448 kmem_free(info->hne_data, 4449 info->hne_datalen); 4450 kmem_free(info, sizeof (hook_nic_event_t)); 4451 } 4452 } 4453 } 4454 4455 /* Generate NE_UNPLUMB event for ill_name. */ 4456 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 4457 if (info != NULL) { 4458 info->hne_nic = ill->ill_phyint->phyint_ifindex; 4459 info->hne_lif = 0; 4460 info->hne_event = NE_UNPLUMB; 4461 info->hne_data = nicname; 4462 info->hne_datalen = nicnamelen; 4463 info->hne_family = ill->ill_isv6 ? 4464 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 4465 } else { 4466 ip2dbg(("ill_glist_delete: could not attach UNPLUMB nic event " 4467 "information for %s (ENOMEM)\n", ill->ill_name)); 4468 if (nicname != NULL) 4469 kmem_free(nicname, nicnamelen); 4470 } 4471 4472 ill->ill_nic_event_info = info; 4473 4474 ill_phyint_free(ill); 4475 rw_exit(&ipst->ips_ill_g_lock); 4476 } 4477 4478 /* 4479 * allocate a ppa, if the number of plumbed interfaces of this type are 4480 * less than ill_no_arena do a linear search to find a unused ppa. 4481 * When the number goes beyond ill_no_arena switch to using an arena. 4482 * Note: ppa value of zero cannot be allocated from vmem_arena as it 4483 * is the return value for an error condition, so allocation starts at one 4484 * and is decremented by one. 4485 */ 4486 static int 4487 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill) 4488 { 4489 ill_t *tmp_ill; 4490 uint_t start, end; 4491 int ppa; 4492 4493 if (ifp->illif_ppa_arena == NULL && 4494 (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) { 4495 /* 4496 * Create an arena. 4497 */ 4498 ifp->illif_ppa_arena = vmem_create(ifp->illif_name, 4499 (void *)1, UINT_MAX - 1, 1, NULL, NULL, 4500 NULL, 0, VM_SLEEP | VMC_IDENTIFIER); 4501 /* allocate what has already been assigned */ 4502 for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa); 4503 tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, 4504 tmp_ill, AVL_AFTER)) { 4505 ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena, 4506 1, /* size */ 4507 1, /* align/quantum */ 4508 0, /* phase */ 4509 0, /* nocross */ 4510 (void *)((uintptr_t)tmp_ill->ill_ppa + 1), /* minaddr */ 4511 (void *)((uintptr_t)tmp_ill->ill_ppa + 2), /* maxaddr */ 4512 VM_NOSLEEP|VM_FIRSTFIT); 4513 if (ppa == 0) { 4514 ip1dbg(("ill_alloc_ppa: ppa allocation" 4515 " failed while switching")); 4516 vmem_destroy(ifp->illif_ppa_arena); 4517 ifp->illif_ppa_arena = NULL; 4518 break; 4519 } 4520 } 4521 } 4522 4523 if (ifp->illif_ppa_arena != NULL) { 4524 if (ill->ill_ppa == UINT_MAX) { 4525 ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena, 4526 1, VM_NOSLEEP|VM_FIRSTFIT); 4527 if (ppa == 0) 4528 return (EAGAIN); 4529 ill->ill_ppa = --ppa; 4530 } else { 4531 ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena, 4532 1, /* size */ 4533 1, /* align/quantum */ 4534 0, /* phase */ 4535 0, /* nocross */ 4536 (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */ 4537 (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */ 4538 VM_NOSLEEP|VM_FIRSTFIT); 4539 /* 4540 * Most likely the allocation failed because 4541 * the requested ppa was in use. 4542 */ 4543 if (ppa == 0) 4544 return (EEXIST); 4545 } 4546 return (0); 4547 } 4548 4549 /* 4550 * No arena is in use and not enough (>ill_no_arena) interfaces have 4551 * been plumbed to create one. Do a linear search to get a unused ppa. 4552 */ 4553 if (ill->ill_ppa == UINT_MAX) { 4554 end = UINT_MAX - 1; 4555 start = 0; 4556 } else { 4557 end = start = ill->ill_ppa; 4558 } 4559 4560 tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL); 4561 while (tmp_ill != NULL && tmp_ill->ill_ppa == start) { 4562 if (start++ >= end) { 4563 if (ill->ill_ppa == UINT_MAX) 4564 return (EAGAIN); 4565 else 4566 return (EEXIST); 4567 } 4568 tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER); 4569 } 4570 ill->ill_ppa = start; 4571 return (0); 4572 } 4573 4574 /* 4575 * Insert ill into the list of configured ill's. Once this function completes, 4576 * the ill is globally visible and is available through lookups. More precisely 4577 * this happens after the caller drops the ill_g_lock. 4578 */ 4579 static int 4580 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6) 4581 { 4582 ill_if_t *ill_interface; 4583 avl_index_t where = 0; 4584 int error; 4585 int name_length; 4586 int index; 4587 boolean_t check_length = B_FALSE; 4588 ip_stack_t *ipst = ill->ill_ipst; 4589 4590 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 4591 4592 name_length = mi_strlen(name) + 1; 4593 4594 if (isv6) 4595 index = IP_V6_G_HEAD; 4596 else 4597 index = IP_V4_G_HEAD; 4598 4599 ill_interface = IP_VX_ILL_G_LIST(index, ipst); 4600 /* 4601 * Search for interface type based on name 4602 */ 4603 while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) { 4604 if ((ill_interface->illif_name_len == name_length) && 4605 (strcmp(ill_interface->illif_name, name) == 0)) { 4606 break; 4607 } 4608 ill_interface = ill_interface->illif_next; 4609 } 4610 4611 /* 4612 * Interface type not found, create one. 4613 */ 4614 if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) { 4615 4616 ill_g_head_t ghead; 4617 4618 /* 4619 * allocate ill_if_t structure 4620 */ 4621 4622 ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t)); 4623 if (ill_interface == NULL) { 4624 return (ENOMEM); 4625 } 4626 4627 4628 4629 (void) strcpy(ill_interface->illif_name, name); 4630 ill_interface->illif_name_len = name_length; 4631 4632 avl_create(&ill_interface->illif_avl_by_ppa, 4633 ill_compare_ppa, sizeof (ill_t), 4634 offsetof(struct ill_s, ill_avl_byppa)); 4635 4636 /* 4637 * link the structure in the back to maintain order 4638 * of configuration for ifconfig output. 4639 */ 4640 ghead = ipst->ips_ill_g_heads[index]; 4641 insque(ill_interface, ghead.ill_g_list_tail); 4642 4643 } 4644 4645 if (ill->ill_ppa == UINT_MAX) 4646 check_length = B_TRUE; 4647 4648 error = ill_alloc_ppa(ill_interface, ill); 4649 if (error != 0) { 4650 if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0) 4651 ill_delete_interface_type(ill->ill_ifptr); 4652 return (error); 4653 } 4654 4655 /* 4656 * When the ppa is choosen by the system, check that there is 4657 * enough space to insert ppa. if a specific ppa was passed in this 4658 * check is not required as the interface name passed in will have 4659 * the right ppa in it. 4660 */ 4661 if (check_length) { 4662 /* 4663 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars. 4664 */ 4665 char buf[sizeof (uint_t) * 3]; 4666 4667 /* 4668 * convert ppa to string to calculate the amount of space 4669 * required for it in the name. 4670 */ 4671 numtos(ill->ill_ppa, buf); 4672 4673 /* Do we have enough space to insert ppa ? */ 4674 4675 if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) { 4676 /* Free ppa and interface type struct */ 4677 if (ill_interface->illif_ppa_arena != NULL) { 4678 vmem_free(ill_interface->illif_ppa_arena, 4679 (void *)(uintptr_t)(ill->ill_ppa+1), 1); 4680 } 4681 if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 4682 0) { 4683 ill_delete_interface_type(ill->ill_ifptr); 4684 } 4685 4686 return (EINVAL); 4687 } 4688 } 4689 4690 (void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa); 4691 ill->ill_name_length = mi_strlen(ill->ill_name) + 1; 4692 4693 (void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa, 4694 &where); 4695 ill->ill_ifptr = ill_interface; 4696 avl_insert(&ill_interface->illif_avl_by_ppa, ill, where); 4697 4698 ill_phyint_reinit(ill); 4699 return (0); 4700 } 4701 4702 /* Initialize the per phyint (per IPMP group) ipsq used for serialization */ 4703 static boolean_t 4704 ipsq_init(ill_t *ill) 4705 { 4706 ipsq_t *ipsq; 4707 4708 /* Init the ipsq and impicitly enter as writer */ 4709 ill->ill_phyint->phyint_ipsq = 4710 kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP); 4711 if (ill->ill_phyint->phyint_ipsq == NULL) 4712 return (B_FALSE); 4713 ipsq = ill->ill_phyint->phyint_ipsq; 4714 ipsq->ipsq_phyint_list = ill->ill_phyint; 4715 ill->ill_phyint->phyint_ipsq_next = NULL; 4716 mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0); 4717 ipsq->ipsq_refs = 1; 4718 ipsq->ipsq_writer = curthread; 4719 ipsq->ipsq_reentry_cnt = 1; 4720 ipsq->ipsq_ipst = ill->ill_ipst; /* No netstack_hold */ 4721 #ifdef ILL_DEBUG 4722 ipsq->ipsq_depth = getpcstack((pc_t *)ipsq->ipsq_stack, IP_STACK_DEPTH); 4723 #endif 4724 (void) strcpy(ipsq->ipsq_name, ill->ill_name); 4725 return (B_TRUE); 4726 } 4727 4728 /* 4729 * ill_init is called by ip_open when a device control stream is opened. 4730 * It does a few initializations, and shoots a DL_INFO_REQ message down 4731 * to the driver. The response is later picked up in ip_rput_dlpi and 4732 * used to set up default mechanisms for talking to the driver. (Always 4733 * called as writer.) 4734 * 4735 * If this function returns error, ip_open will call ip_close which in 4736 * turn will call ill_delete to clean up any memory allocated here that 4737 * is not yet freed. 4738 */ 4739 int 4740 ill_init(queue_t *q, ill_t *ill) 4741 { 4742 int count; 4743 dl_info_req_t *dlir; 4744 mblk_t *info_mp; 4745 uchar_t *frag_ptr; 4746 4747 /* 4748 * The ill is initialized to zero by mi_alloc*(). In addition 4749 * some fields already contain valid values, initialized in 4750 * ip_open(), before we reach here. 4751 */ 4752 mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0); 4753 4754 ill->ill_rq = q; 4755 ill->ill_wq = WR(q); 4756 4757 info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)), 4758 BPRI_HI); 4759 if (info_mp == NULL) 4760 return (ENOMEM); 4761 4762 /* 4763 * Allocate sufficient space to contain our fragment hash table and 4764 * the device name. 4765 */ 4766 frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 4767 2 * LIFNAMSIZ + 5 + strlen(ipv6_forward_suffix)); 4768 if (frag_ptr == NULL) { 4769 freemsg(info_mp); 4770 return (ENOMEM); 4771 } 4772 ill->ill_frag_ptr = frag_ptr; 4773 ill->ill_frag_free_num_pkts = 0; 4774 ill->ill_last_frag_clean_time = 0; 4775 ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr; 4776 ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE); 4777 for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) { 4778 mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock, 4779 NULL, MUTEX_DEFAULT, NULL); 4780 } 4781 4782 ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t)); 4783 if (ill->ill_phyint == NULL) { 4784 freemsg(info_mp); 4785 mi_free(frag_ptr); 4786 return (ENOMEM); 4787 } 4788 4789 mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0); 4790 /* 4791 * For now pretend this is a v4 ill. We need to set phyint_ill* 4792 * at this point because of the following reason. If we can't 4793 * enter the ipsq at some point and cv_wait, the writer that 4794 * wakes us up tries to locate us using the list of all phyints 4795 * in an ipsq and the ills from the phyint thru the phyint_ill*. 4796 * If we don't set it now, we risk a missed wakeup. 4797 */ 4798 ill->ill_phyint->phyint_illv4 = ill; 4799 ill->ill_ppa = UINT_MAX; 4800 ill->ill_fastpath_list = &ill->ill_fastpath_list; 4801 4802 if (!ipsq_init(ill)) { 4803 freemsg(info_mp); 4804 mi_free(frag_ptr); 4805 mi_free(ill->ill_phyint); 4806 return (ENOMEM); 4807 } 4808 4809 ill->ill_state_flags |= ILL_LL_SUBNET_PENDING; 4810 4811 4812 /* Frag queue limit stuff */ 4813 ill->ill_frag_count = 0; 4814 ill->ill_ipf_gen = 0; 4815 4816 ill->ill_global_timer = INFINITY; 4817 ill->ill_mcast_type = IGMP_V3_ROUTER; /* == MLD_V2_ROUTER */ 4818 ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0; 4819 ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0; 4820 ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS; 4821 ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL; 4822 4823 /* 4824 * Initialize IPv6 configuration variables. The IP module is always 4825 * opened as an IPv4 module. Instead tracking down the cases where 4826 * it switches to do ipv6, we'll just initialize the IPv6 configuration 4827 * here for convenience, this has no effect until the ill is set to do 4828 * IPv6. 4829 */ 4830 ill->ill_reachable_time = ND_REACHABLE_TIME; 4831 ill->ill_reachable_retrans_time = ND_RETRANS_TIMER; 4832 ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT; 4833 ill->ill_max_buf = ND_MAX_Q; 4834 ill->ill_refcnt = 0; 4835 4836 /* Send down the Info Request to the driver. */ 4837 info_mp->b_datap->db_type = M_PCPROTO; 4838 dlir = (dl_info_req_t *)info_mp->b_rptr; 4839 info_mp->b_wptr = (uchar_t *)&dlir[1]; 4840 dlir->dl_primitive = DL_INFO_REQ; 4841 4842 ill->ill_dlpi_pending = DL_PRIM_INVAL; 4843 4844 qprocson(q); 4845 ill_dlpi_send(ill, info_mp); 4846 4847 return (0); 4848 } 4849 4850 /* 4851 * ill_dls_info 4852 * creates datalink socket info from the device. 4853 */ 4854 int 4855 ill_dls_info(struct sockaddr_dl *sdl, const ipif_t *ipif) 4856 { 4857 size_t len; 4858 ill_t *ill = ipif->ipif_ill; 4859 4860 sdl->sdl_family = AF_LINK; 4861 sdl->sdl_index = ill->ill_phyint->phyint_ifindex; 4862 sdl->sdl_type = ill->ill_type; 4863 (void) ipif_get_name(ipif, sdl->sdl_data, sizeof (sdl->sdl_data)); 4864 len = strlen(sdl->sdl_data); 4865 ASSERT(len < 256); 4866 sdl->sdl_nlen = (uchar_t)len; 4867 sdl->sdl_alen = ill->ill_phys_addr_length; 4868 sdl->sdl_slen = 0; 4869 if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL) 4870 bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen); 4871 4872 return (sizeof (struct sockaddr_dl)); 4873 } 4874 4875 /* 4876 * ill_xarp_info 4877 * creates xarp info from the device. 4878 */ 4879 static int 4880 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill) 4881 { 4882 sdl->sdl_family = AF_LINK; 4883 sdl->sdl_index = ill->ill_phyint->phyint_ifindex; 4884 sdl->sdl_type = ill->ill_type; 4885 (void) ipif_get_name(ill->ill_ipif, sdl->sdl_data, 4886 sizeof (sdl->sdl_data)); 4887 sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data); 4888 sdl->sdl_alen = ill->ill_phys_addr_length; 4889 sdl->sdl_slen = 0; 4890 return (sdl->sdl_nlen); 4891 } 4892 4893 static int 4894 loopback_kstat_update(kstat_t *ksp, int rw) 4895 { 4896 kstat_named_t *kn; 4897 netstackid_t stackid; 4898 netstack_t *ns; 4899 ip_stack_t *ipst; 4900 4901 if (ksp == NULL || ksp->ks_data == NULL) 4902 return (EIO); 4903 4904 if (rw == KSTAT_WRITE) 4905 return (EACCES); 4906 4907 kn = KSTAT_NAMED_PTR(ksp); 4908 stackid = (zoneid_t)(uintptr_t)ksp->ks_private; 4909 4910 ns = netstack_find_by_stackid(stackid); 4911 if (ns == NULL) 4912 return (-1); 4913 4914 ipst = ns->netstack_ip; 4915 if (ipst == NULL) { 4916 netstack_rele(ns); 4917 return (-1); 4918 } 4919 kn[0].value.ui32 = ipst->ips_loopback_packets; 4920 kn[1].value.ui32 = ipst->ips_loopback_packets; 4921 netstack_rele(ns); 4922 return (0); 4923 } 4924 4925 4926 /* 4927 * Has ifindex been plumbed already. 4928 * Compares both phyint_ifindex and phyint_group_ifindex. 4929 */ 4930 static boolean_t 4931 phyint_exists(uint_t index, ip_stack_t *ipst) 4932 { 4933 phyint_t *phyi; 4934 4935 ASSERT(index != 0); 4936 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock)); 4937 /* 4938 * Indexes are stored in the phyint - a common structure 4939 * to both IPv4 and IPv6. 4940 */ 4941 phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index); 4942 for (; phyi != NULL; 4943 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 4944 phyi, AVL_AFTER)) { 4945 if (phyi->phyint_ifindex == index || 4946 phyi->phyint_group_ifindex == index) 4947 return (B_TRUE); 4948 } 4949 return (B_FALSE); 4950 } 4951 4952 /* Pick a unique ifindex */ 4953 boolean_t 4954 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst) 4955 { 4956 uint_t starting_index; 4957 4958 if (!ipst->ips_ill_index_wrap) { 4959 *indexp = ipst->ips_ill_index++; 4960 if (ipst->ips_ill_index == 0) { 4961 /* Reached the uint_t limit Next time wrap */ 4962 ipst->ips_ill_index_wrap = B_TRUE; 4963 } 4964 return (B_TRUE); 4965 } 4966 4967 /* 4968 * Start reusing unused indexes. Note that we hold the ill_g_lock 4969 * at this point and don't want to call any function that attempts 4970 * to get the lock again. 4971 */ 4972 starting_index = ipst->ips_ill_index++; 4973 for (; ipst->ips_ill_index != starting_index; ipst->ips_ill_index++) { 4974 if (ipst->ips_ill_index != 0 && 4975 !phyint_exists(ipst->ips_ill_index, ipst)) { 4976 /* found unused index - use it */ 4977 *indexp = ipst->ips_ill_index; 4978 return (B_TRUE); 4979 } 4980 } 4981 4982 /* 4983 * all interface indicies are inuse. 4984 */ 4985 return (B_FALSE); 4986 } 4987 4988 /* 4989 * Assign a unique interface index for the phyint. 4990 */ 4991 static boolean_t 4992 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst) 4993 { 4994 ASSERT(phyi->phyint_ifindex == 0); 4995 return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst)); 4996 } 4997 4998 /* 4999 * Return a pointer to the ill which matches the supplied name. Note that 5000 * the ill name length includes the null termination character. (May be 5001 * called as writer.) 5002 * If do_alloc and the interface is "lo0" it will be automatically created. 5003 * Cannot bump up reference on condemned ills. So dup detect can't be done 5004 * using this func. 5005 */ 5006 ill_t * 5007 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6, 5008 queue_t *q, mblk_t *mp, ipsq_func_t func, int *error, boolean_t *did_alloc, 5009 ip_stack_t *ipst) 5010 { 5011 ill_t *ill; 5012 ipif_t *ipif; 5013 kstat_named_t *kn; 5014 boolean_t isloopback; 5015 ipsq_t *old_ipsq; 5016 in6_addr_t ov6addr; 5017 5018 isloopback = mi_strcmp(name, ipif_loopback_name) == 0; 5019 5020 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5021 ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst); 5022 rw_exit(&ipst->ips_ill_g_lock); 5023 if (ill != NULL || (error != NULL && *error == EINPROGRESS)) 5024 return (ill); 5025 5026 /* 5027 * Couldn't find it. Does this happen to be a lookup for the 5028 * loopback device and are we allowed to allocate it? 5029 */ 5030 if (!isloopback || !do_alloc) 5031 return (NULL); 5032 5033 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 5034 5035 ill = ill_find_by_name(name, isv6, q, mp, func, error, ipst); 5036 if (ill != NULL || (error != NULL && *error == EINPROGRESS)) { 5037 rw_exit(&ipst->ips_ill_g_lock); 5038 return (ill); 5039 } 5040 5041 /* Create the loopback device on demand */ 5042 ill = (ill_t *)(mi_alloc(sizeof (ill_t) + 5043 sizeof (ipif_loopback_name), BPRI_MED)); 5044 if (ill == NULL) 5045 goto done; 5046 5047 *ill = ill_null; 5048 mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL); 5049 ill->ill_ipst = ipst; 5050 netstack_hold(ipst->ips_netstack); 5051 /* 5052 * For exclusive stacks we set the zoneid to zero 5053 * to make IP operate as if in the global zone. 5054 */ 5055 ill->ill_zoneid = GLOBAL_ZONEID; 5056 5057 ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t)); 5058 if (ill->ill_phyint == NULL) 5059 goto done; 5060 5061 if (isv6) 5062 ill->ill_phyint->phyint_illv6 = ill; 5063 else 5064 ill->ill_phyint->phyint_illv4 = ill; 5065 mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0); 5066 ill->ill_max_frag = IP_LOOPBACK_MTU; 5067 /* Add room for tcp+ip headers */ 5068 if (isv6) { 5069 ill->ill_isv6 = B_TRUE; 5070 ill->ill_max_frag += IPV6_HDR_LEN + 20; /* for TCP */ 5071 } else { 5072 ill->ill_max_frag += IP_SIMPLE_HDR_LENGTH + 20; 5073 } 5074 if (!ill_allocate_mibs(ill)) 5075 goto done; 5076 ill->ill_max_mtu = ill->ill_max_frag; 5077 /* 5078 * ipif_loopback_name can't be pointed at directly because its used 5079 * by both the ipv4 and ipv6 interfaces. When the ill is removed 5080 * from the glist, ill_glist_delete() sets the first character of 5081 * ill_name to '\0'. 5082 */ 5083 ill->ill_name = (char *)ill + sizeof (*ill); 5084 (void) strcpy(ill->ill_name, ipif_loopback_name); 5085 ill->ill_name_length = sizeof (ipif_loopback_name); 5086 /* Set ill_name_set for ill_phyint_reinit to work properly */ 5087 5088 ill->ill_global_timer = INFINITY; 5089 ill->ill_mcast_type = IGMP_V3_ROUTER; /* == MLD_V2_ROUTER */ 5090 ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0; 5091 ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0; 5092 ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS; 5093 ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL; 5094 5095 /* No resolver here. */ 5096 ill->ill_net_type = IRE_LOOPBACK; 5097 5098 /* Initialize the ipsq */ 5099 if (!ipsq_init(ill)) 5100 goto done; 5101 5102 ill->ill_phyint->phyint_ipsq->ipsq_writer = NULL; 5103 ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt--; 5104 ASSERT(ill->ill_phyint->phyint_ipsq->ipsq_reentry_cnt == 0); 5105 #ifdef ILL_DEBUG 5106 ill->ill_phyint->phyint_ipsq->ipsq_depth = 0; 5107 #endif 5108 ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE); 5109 if (ipif == NULL) 5110 goto done; 5111 5112 ill->ill_flags = ILLF_MULTICAST; 5113 5114 ov6addr = ipif->ipif_v6lcl_addr; 5115 /* Set up default loopback address and mask. */ 5116 if (!isv6) { 5117 ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK); 5118 5119 IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr); 5120 ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr; 5121 V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask); 5122 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 5123 ipif->ipif_v6subnet); 5124 ill->ill_flags |= ILLF_IPV4; 5125 } else { 5126 ipif->ipif_v6lcl_addr = ipv6_loopback; 5127 ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr; 5128 ipif->ipif_v6net_mask = ipv6_all_ones; 5129 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 5130 ipif->ipif_v6subnet); 5131 ill->ill_flags |= ILLF_IPV6; 5132 } 5133 5134 /* 5135 * Chain us in at the end of the ill list. hold the ill 5136 * before we make it globally visible. 1 for the lookup. 5137 */ 5138 ill->ill_refcnt = 0; 5139 ill_refhold(ill); 5140 5141 ill->ill_frag_count = 0; 5142 ill->ill_frag_free_num_pkts = 0; 5143 ill->ill_last_frag_clean_time = 0; 5144 5145 old_ipsq = ill->ill_phyint->phyint_ipsq; 5146 5147 if (ill_glist_insert(ill, "lo", isv6) != 0) 5148 cmn_err(CE_PANIC, "cannot insert loopback interface"); 5149 5150 /* Let SCTP know so that it can add this to its list */ 5151 sctp_update_ill(ill, SCTP_ILL_INSERT); 5152 5153 /* 5154 * We have already assigned ipif_v6lcl_addr above, but we need to 5155 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which 5156 * requires to be after ill_glist_insert() since we need the 5157 * ill_index set. Pass on ipv6_loopback as the old address. 5158 */ 5159 sctp_update_ipif_addr(ipif, ov6addr); 5160 5161 /* 5162 * If the ipsq was changed in ill_phyint_reinit free the old ipsq. 5163 */ 5164 if (old_ipsq != ill->ill_phyint->phyint_ipsq) { 5165 /* Loopback ills aren't in any IPMP group */ 5166 ASSERT(!(old_ipsq->ipsq_flags & IPSQ_GROUP)); 5167 ipsq_delete(old_ipsq); 5168 } 5169 5170 /* 5171 * Delay this till the ipif is allocated as ipif_allocate 5172 * de-references ill_phyint for getting the ifindex. We 5173 * can't do this before ipif_allocate because ill_phyint_reinit 5174 * -> phyint_assign_ifindex expects ipif to be present. 5175 */ 5176 mutex_enter(&ill->ill_phyint->phyint_lock); 5177 ill->ill_phyint->phyint_flags |= PHYI_LOOPBACK | PHYI_VIRTUAL; 5178 mutex_exit(&ill->ill_phyint->phyint_lock); 5179 5180 if (ipst->ips_loopback_ksp == NULL) { 5181 /* Export loopback interface statistics */ 5182 ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0, 5183 ipif_loopback_name, "net", 5184 KSTAT_TYPE_NAMED, 2, 0, 5185 ipst->ips_netstack->netstack_stackid); 5186 if (ipst->ips_loopback_ksp != NULL) { 5187 ipst->ips_loopback_ksp->ks_update = 5188 loopback_kstat_update; 5189 kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp); 5190 kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32); 5191 kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32); 5192 ipst->ips_loopback_ksp->ks_private = 5193 (void *)(uintptr_t)ipst->ips_netstack-> 5194 netstack_stackid; 5195 kstat_install(ipst->ips_loopback_ksp); 5196 } 5197 } 5198 5199 if (error != NULL) 5200 *error = 0; 5201 *did_alloc = B_TRUE; 5202 rw_exit(&ipst->ips_ill_g_lock); 5203 return (ill); 5204 done: 5205 if (ill != NULL) { 5206 if (ill->ill_phyint != NULL) { 5207 ipsq_t *ipsq; 5208 5209 ipsq = ill->ill_phyint->phyint_ipsq; 5210 if (ipsq != NULL) { 5211 ipsq->ipsq_ipst = NULL; 5212 kmem_free(ipsq, sizeof (ipsq_t)); 5213 } 5214 mi_free(ill->ill_phyint); 5215 } 5216 ill_free_mib(ill); 5217 if (ill->ill_ipst != NULL) 5218 netstack_rele(ill->ill_ipst->ips_netstack); 5219 mi_free(ill); 5220 } 5221 rw_exit(&ipst->ips_ill_g_lock); 5222 if (error != NULL) 5223 *error = ENOMEM; 5224 return (NULL); 5225 } 5226 5227 /* 5228 * For IPP calls - use the ip_stack_t for global stack. 5229 */ 5230 ill_t * 5231 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6, 5232 queue_t *q, mblk_t *mp, ipsq_func_t func, int *err) 5233 { 5234 ip_stack_t *ipst; 5235 ill_t *ill; 5236 5237 ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip; 5238 if (ipst == NULL) { 5239 cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n"); 5240 return (NULL); 5241 } 5242 5243 ill = ill_lookup_on_ifindex(index, isv6, q, mp, func, err, ipst); 5244 netstack_rele(ipst->ips_netstack); 5245 return (ill); 5246 } 5247 5248 /* 5249 * Return a pointer to the ill which matches the index and IP version type. 5250 */ 5251 ill_t * 5252 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, queue_t *q, mblk_t *mp, 5253 ipsq_func_t func, int *err, ip_stack_t *ipst) 5254 { 5255 ill_t *ill; 5256 ipsq_t *ipsq; 5257 phyint_t *phyi; 5258 5259 ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) || 5260 (q != NULL && mp != NULL && func != NULL && err != NULL)); 5261 5262 if (err != NULL) 5263 *err = 0; 5264 5265 /* 5266 * Indexes are stored in the phyint - a common structure 5267 * to both IPv4 and IPv6. 5268 */ 5269 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5270 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 5271 (void *) &index, NULL); 5272 if (phyi != NULL) { 5273 ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4; 5274 if (ill != NULL) { 5275 /* 5276 * The block comment at the start of ipif_down 5277 * explains the use of the macros used below 5278 */ 5279 GRAB_CONN_LOCK(q); 5280 mutex_enter(&ill->ill_lock); 5281 if (ILL_CAN_LOOKUP(ill)) { 5282 ill_refhold_locked(ill); 5283 mutex_exit(&ill->ill_lock); 5284 RELEASE_CONN_LOCK(q); 5285 rw_exit(&ipst->ips_ill_g_lock); 5286 return (ill); 5287 } else if (ILL_CAN_WAIT(ill, q)) { 5288 ipsq = ill->ill_phyint->phyint_ipsq; 5289 mutex_enter(&ipsq->ipsq_lock); 5290 rw_exit(&ipst->ips_ill_g_lock); 5291 mutex_exit(&ill->ill_lock); 5292 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill); 5293 mutex_exit(&ipsq->ipsq_lock); 5294 RELEASE_CONN_LOCK(q); 5295 *err = EINPROGRESS; 5296 return (NULL); 5297 } 5298 RELEASE_CONN_LOCK(q); 5299 mutex_exit(&ill->ill_lock); 5300 } 5301 } 5302 rw_exit(&ipst->ips_ill_g_lock); 5303 if (err != NULL) 5304 *err = ENXIO; 5305 return (NULL); 5306 } 5307 5308 /* 5309 * Return the ifindex next in sequence after the passed in ifindex. 5310 * If there is no next ifindex for the given protocol, return 0. 5311 */ 5312 uint_t 5313 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst) 5314 { 5315 phyint_t *phyi; 5316 phyint_t *phyi_initial; 5317 uint_t ifindex; 5318 5319 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5320 5321 if (index == 0) { 5322 phyi = avl_first( 5323 &ipst->ips_phyint_g_list->phyint_list_avl_by_index); 5324 } else { 5325 phyi = phyi_initial = avl_find( 5326 &ipst->ips_phyint_g_list->phyint_list_avl_by_index, 5327 (void *) &index, NULL); 5328 } 5329 5330 for (; phyi != NULL; 5331 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 5332 phyi, AVL_AFTER)) { 5333 /* 5334 * If we're not returning the first interface in the tree 5335 * and we still haven't moved past the phyint_t that 5336 * corresponds to index, avl_walk needs to be called again 5337 */ 5338 if (!((index != 0) && (phyi == phyi_initial))) { 5339 if (isv6) { 5340 if ((phyi->phyint_illv6) && 5341 ILL_CAN_LOOKUP(phyi->phyint_illv6) && 5342 (phyi->phyint_illv6->ill_isv6 == 1)) 5343 break; 5344 } else { 5345 if ((phyi->phyint_illv4) && 5346 ILL_CAN_LOOKUP(phyi->phyint_illv4) && 5347 (phyi->phyint_illv4->ill_isv6 == 0)) 5348 break; 5349 } 5350 } 5351 } 5352 5353 rw_exit(&ipst->ips_ill_g_lock); 5354 5355 if (phyi != NULL) 5356 ifindex = phyi->phyint_ifindex; 5357 else 5358 ifindex = 0; 5359 5360 return (ifindex); 5361 } 5362 5363 5364 /* 5365 * Return the ifindex for the named interface. 5366 * If there is no next ifindex for the interface, return 0. 5367 */ 5368 uint_t 5369 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst) 5370 { 5371 phyint_t *phyi; 5372 avl_index_t where = 0; 5373 uint_t ifindex; 5374 5375 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5376 5377 if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name, 5378 name, &where)) == NULL) { 5379 rw_exit(&ipst->ips_ill_g_lock); 5380 return (0); 5381 } 5382 5383 ifindex = phyi->phyint_ifindex; 5384 5385 rw_exit(&ipst->ips_ill_g_lock); 5386 5387 return (ifindex); 5388 } 5389 5390 5391 /* 5392 * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt 5393 * that gives a running thread a reference to the ill. This reference must be 5394 * released by the thread when it is done accessing the ill and related 5395 * objects. ill_refcnt can not be used to account for static references 5396 * such as other structures pointing to an ill. Callers must generally 5397 * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros 5398 * or be sure that the ill is not being deleted or changing state before 5399 * calling the refhold functions. A non-zero ill_refcnt ensures that the 5400 * ill won't change any of its critical state such as address, netmask etc. 5401 */ 5402 void 5403 ill_refhold(ill_t *ill) 5404 { 5405 mutex_enter(&ill->ill_lock); 5406 ill->ill_refcnt++; 5407 ILL_TRACE_REF(ill); 5408 mutex_exit(&ill->ill_lock); 5409 } 5410 5411 void 5412 ill_refhold_locked(ill_t *ill) 5413 { 5414 ASSERT(MUTEX_HELD(&ill->ill_lock)); 5415 ill->ill_refcnt++; 5416 ILL_TRACE_REF(ill); 5417 } 5418 5419 int 5420 ill_check_and_refhold(ill_t *ill) 5421 { 5422 mutex_enter(&ill->ill_lock); 5423 if (ILL_CAN_LOOKUP(ill)) { 5424 ill_refhold_locked(ill); 5425 mutex_exit(&ill->ill_lock); 5426 return (0); 5427 } 5428 mutex_exit(&ill->ill_lock); 5429 return (ILL_LOOKUP_FAILED); 5430 } 5431 5432 /* 5433 * Must not be called while holding any locks. Otherwise if this is 5434 * the last reference to be released, there is a chance of recursive mutex 5435 * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying 5436 * to restart an ioctl. 5437 */ 5438 void 5439 ill_refrele(ill_t *ill) 5440 { 5441 mutex_enter(&ill->ill_lock); 5442 ASSERT(ill->ill_refcnt != 0); 5443 ill->ill_refcnt--; 5444 ILL_UNTRACE_REF(ill); 5445 if (ill->ill_refcnt != 0) { 5446 /* Every ire pointing to the ill adds 1 to ill_refcnt */ 5447 mutex_exit(&ill->ill_lock); 5448 return; 5449 } 5450 5451 /* Drops the ill_lock */ 5452 ipif_ill_refrele_tail(ill); 5453 } 5454 5455 /* 5456 * Obtain a weak reference count on the ill. This reference ensures the 5457 * ill won't be freed, but the ill may change any of its critical state 5458 * such as netmask, address etc. Returns an error if the ill has started 5459 * closing. 5460 */ 5461 boolean_t 5462 ill_waiter_inc(ill_t *ill) 5463 { 5464 mutex_enter(&ill->ill_lock); 5465 if (ill->ill_state_flags & ILL_CONDEMNED) { 5466 mutex_exit(&ill->ill_lock); 5467 return (B_FALSE); 5468 } 5469 ill->ill_waiters++; 5470 mutex_exit(&ill->ill_lock); 5471 return (B_TRUE); 5472 } 5473 5474 void 5475 ill_waiter_dcr(ill_t *ill) 5476 { 5477 mutex_enter(&ill->ill_lock); 5478 ill->ill_waiters--; 5479 if (ill->ill_waiters == 0) 5480 cv_broadcast(&ill->ill_cv); 5481 mutex_exit(&ill->ill_lock); 5482 } 5483 5484 /* 5485 * Named Dispatch routine to produce a formatted report on all ILLs. 5486 * This report is accessed by using the ndd utility to "get" ND variable 5487 * "ip_ill_status". 5488 */ 5489 /* ARGSUSED */ 5490 int 5491 ip_ill_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 5492 { 5493 ill_t *ill; 5494 ill_walk_context_t ctx; 5495 ip_stack_t *ipst; 5496 5497 ipst = CONNQ_TO_IPST(q); 5498 5499 (void) mi_mpprintf(mp, 5500 "ILL " MI_COL_HDRPAD_STR 5501 /* 01234567[89ABCDEF] */ 5502 "rq " MI_COL_HDRPAD_STR 5503 /* 01234567[89ABCDEF] */ 5504 "wq " MI_COL_HDRPAD_STR 5505 /* 01234567[89ABCDEF] */ 5506 "upcnt mxfrg err name"); 5507 /* 12345 12345 123 xxxxxxxx */ 5508 5509 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5510 ill = ILL_START_WALK_ALL(&ctx, ipst); 5511 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 5512 (void) mi_mpprintf(mp, 5513 MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR MI_COL_PTRFMT_STR 5514 "%05u %05u %03d %s", 5515 (void *)ill, (void *)ill->ill_rq, (void *)ill->ill_wq, 5516 ill->ill_ipif_up_count, 5517 ill->ill_max_frag, ill->ill_error, ill->ill_name); 5518 } 5519 rw_exit(&ipst->ips_ill_g_lock); 5520 5521 return (0); 5522 } 5523 5524 /* 5525 * Named Dispatch routine to produce a formatted report on all IPIFs. 5526 * This report is accessed by using the ndd utility to "get" ND variable 5527 * "ip_ipif_status". 5528 */ 5529 /* ARGSUSED */ 5530 int 5531 ip_ipif_report(queue_t *q, mblk_t *mp, caddr_t arg, cred_t *ioc_cr) 5532 { 5533 char buf1[INET6_ADDRSTRLEN]; 5534 char buf2[INET6_ADDRSTRLEN]; 5535 char buf3[INET6_ADDRSTRLEN]; 5536 char buf4[INET6_ADDRSTRLEN]; 5537 char buf5[INET6_ADDRSTRLEN]; 5538 char buf6[INET6_ADDRSTRLEN]; 5539 char buf[LIFNAMSIZ]; 5540 ill_t *ill; 5541 ipif_t *ipif; 5542 nv_t *nvp; 5543 uint64_t flags; 5544 zoneid_t zoneid; 5545 ill_walk_context_t ctx; 5546 ip_stack_t *ipst = CONNQ_TO_IPST(q); 5547 5548 (void) mi_mpprintf(mp, 5549 "IPIF metric mtu in/out/forward name zone flags...\n" 5550 "\tlocal address\n" 5551 "\tsrc address\n" 5552 "\tsubnet\n" 5553 "\tmask\n" 5554 "\tbroadcast\n" 5555 "\tp-p-dst"); 5556 5557 ASSERT(q->q_next == NULL); 5558 zoneid = Q_TO_CONN(q)->conn_zoneid; /* IP is a driver */ 5559 5560 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5561 ill = ILL_START_WALK_ALL(&ctx, ipst); 5562 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 5563 for (ipif = ill->ill_ipif; ipif != NULL; 5564 ipif = ipif->ipif_next) { 5565 if (zoneid != GLOBAL_ZONEID && 5566 zoneid != ipif->ipif_zoneid && 5567 ipif->ipif_zoneid != ALL_ZONES) 5568 continue; 5569 (void) mi_mpprintf(mp, 5570 MI_COL_PTRFMT_STR 5571 "%04u %05u %u/%u/%u %s %d", 5572 (void *)ipif, 5573 ipif->ipif_metric, ipif->ipif_mtu, 5574 ipif->ipif_ib_pkt_count, 5575 ipif->ipif_ob_pkt_count, 5576 ipif->ipif_fo_pkt_count, 5577 ipif_get_name(ipif, buf, sizeof (buf)), 5578 ipif->ipif_zoneid); 5579 5580 flags = ipif->ipif_flags | ipif->ipif_ill->ill_flags | 5581 ipif->ipif_ill->ill_phyint->phyint_flags; 5582 5583 /* Tack on text strings for any flags. */ 5584 nvp = ipif_nv_tbl; 5585 for (; nvp < A_END(ipif_nv_tbl); nvp++) { 5586 if (nvp->nv_value & flags) 5587 (void) mi_mpprintf_nr(mp, " %s", 5588 nvp->nv_name); 5589 } 5590 (void) mi_mpprintf(mp, 5591 "\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s", 5592 inet_ntop(AF_INET6, 5593 &ipif->ipif_v6lcl_addr, buf1, sizeof (buf1)), 5594 inet_ntop(AF_INET6, 5595 &ipif->ipif_v6src_addr, buf2, sizeof (buf2)), 5596 inet_ntop(AF_INET6, 5597 &ipif->ipif_v6subnet, buf3, sizeof (buf3)), 5598 inet_ntop(AF_INET6, 5599 &ipif->ipif_v6net_mask, buf4, sizeof (buf4)), 5600 inet_ntop(AF_INET6, 5601 &ipif->ipif_v6brd_addr, buf5, sizeof (buf5)), 5602 inet_ntop(AF_INET6, 5603 &ipif->ipif_v6pp_dst_addr, 5604 buf6, sizeof (buf6))); 5605 } 5606 } 5607 rw_exit(&ipst->ips_ill_g_lock); 5608 return (0); 5609 } 5610 5611 /* 5612 * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the 5613 * driver. We construct best guess defaults for lower level information that 5614 * we need. If an interface is brought up without injection of any overriding 5615 * information from outside, we have to be ready to go with these defaults. 5616 * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ) 5617 * we primarely want the dl_provider_style. 5618 * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND 5619 * at which point we assume the other part of the information is valid. 5620 */ 5621 void 5622 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp) 5623 { 5624 uchar_t *brdcst_addr; 5625 uint_t brdcst_addr_length, phys_addr_length; 5626 t_scalar_t sap_length; 5627 dl_info_ack_t *dlia; 5628 ip_m_t *ipm; 5629 dl_qos_cl_sel1_t *sel1; 5630 5631 ASSERT(IAM_WRITER_ILL(ill)); 5632 5633 /* 5634 * Till the ill is fully up ILL_CHANGING will be set and 5635 * the ill is not globally visible. So no need for a lock. 5636 */ 5637 dlia = (dl_info_ack_t *)mp->b_rptr; 5638 ill->ill_mactype = dlia->dl_mac_type; 5639 5640 ipm = ip_m_lookup(dlia->dl_mac_type); 5641 if (ipm == NULL) { 5642 ipm = ip_m_lookup(DL_OTHER); 5643 ASSERT(ipm != NULL); 5644 } 5645 ill->ill_media = ipm; 5646 5647 /* 5648 * When the new DLPI stuff is ready we'll pull lengths 5649 * from dlia. 5650 */ 5651 if (dlia->dl_version == DL_VERSION_2) { 5652 brdcst_addr_length = dlia->dl_brdcst_addr_length; 5653 brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset, 5654 brdcst_addr_length); 5655 if (brdcst_addr == NULL) { 5656 brdcst_addr_length = 0; 5657 } 5658 sap_length = dlia->dl_sap_length; 5659 phys_addr_length = dlia->dl_addr_length - ABS(sap_length); 5660 ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n", 5661 brdcst_addr_length, sap_length, phys_addr_length)); 5662 } else { 5663 brdcst_addr_length = 6; 5664 brdcst_addr = ip_six_byte_all_ones; 5665 sap_length = -2; 5666 phys_addr_length = brdcst_addr_length; 5667 } 5668 5669 ill->ill_bcast_addr_length = brdcst_addr_length; 5670 ill->ill_phys_addr_length = phys_addr_length; 5671 ill->ill_sap_length = sap_length; 5672 ill->ill_max_frag = dlia->dl_max_sdu; 5673 ill->ill_max_mtu = ill->ill_max_frag; 5674 5675 ill->ill_type = ipm->ip_m_type; 5676 5677 if (!ill->ill_dlpi_style_set) { 5678 if (dlia->dl_provider_style == DL_STYLE2) 5679 ill->ill_needs_attach = 1; 5680 5681 /* 5682 * Allocate the first ipif on this ill. We don't delay it 5683 * further as ioctl handling assumes atleast one ipif to 5684 * be present. 5685 * 5686 * At this point we don't know whether the ill is v4 or v6. 5687 * We will know this whan the SIOCSLIFNAME happens and 5688 * the correct value for ill_isv6 will be assigned in 5689 * ipif_set_values(). We need to hold the ill lock and 5690 * clear the ILL_LL_SUBNET_PENDING flag and atomically do 5691 * the wakeup. 5692 */ 5693 (void) ipif_allocate(ill, 0, IRE_LOCAL, 5694 dlia->dl_provider_style == DL_STYLE2 ? B_FALSE : B_TRUE); 5695 mutex_enter(&ill->ill_lock); 5696 ASSERT(ill->ill_dlpi_style_set == 0); 5697 ill->ill_dlpi_style_set = 1; 5698 ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING; 5699 cv_broadcast(&ill->ill_cv); 5700 mutex_exit(&ill->ill_lock); 5701 freemsg(mp); 5702 return; 5703 } 5704 ASSERT(ill->ill_ipif != NULL); 5705 /* 5706 * We know whether it is IPv4 or IPv6 now, as this is the 5707 * second DL_INFO_ACK we are recieving in response to the 5708 * DL_INFO_REQ sent in ipif_set_values. 5709 */ 5710 if (ill->ill_isv6) 5711 ill->ill_sap = IP6_DL_SAP; 5712 else 5713 ill->ill_sap = IP_DL_SAP; 5714 /* 5715 * Set ipif_mtu which is used to set the IRE's 5716 * ire_max_frag value. The driver could have sent 5717 * a different mtu from what it sent last time. No 5718 * need to call ipif_mtu_change because IREs have 5719 * not yet been created. 5720 */ 5721 ill->ill_ipif->ipif_mtu = ill->ill_max_mtu; 5722 /* 5723 * Clear all the flags that were set based on ill_bcast_addr_length 5724 * and ill_phys_addr_length (in ipif_set_values) as these could have 5725 * changed now and we need to re-evaluate. 5726 */ 5727 ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP); 5728 ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT); 5729 5730 /* 5731 * Free ill_resolver_mp and ill_bcast_mp as things could have 5732 * changed now. 5733 */ 5734 if (ill->ill_bcast_addr_length == 0) { 5735 if (ill->ill_resolver_mp != NULL) 5736 freemsg(ill->ill_resolver_mp); 5737 if (ill->ill_bcast_mp != NULL) 5738 freemsg(ill->ill_bcast_mp); 5739 if (ill->ill_flags & ILLF_XRESOLV) 5740 ill->ill_net_type = IRE_IF_RESOLVER; 5741 else 5742 ill->ill_net_type = IRE_IF_NORESOLVER; 5743 ill->ill_resolver_mp = ill_dlur_gen(NULL, 5744 ill->ill_phys_addr_length, 5745 ill->ill_sap, 5746 ill->ill_sap_length); 5747 ill->ill_bcast_mp = copymsg(ill->ill_resolver_mp); 5748 5749 if (ill->ill_isv6) 5750 /* 5751 * Note: xresolv interfaces will eventually need NOARP 5752 * set here as well, but that will require those 5753 * external resolvers to have some knowledge of 5754 * that flag and act appropriately. Not to be changed 5755 * at present. 5756 */ 5757 ill->ill_flags |= ILLF_NONUD; 5758 else 5759 ill->ill_flags |= ILLF_NOARP; 5760 5761 if (ill->ill_phys_addr_length == 0) { 5762 if (ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) { 5763 ill->ill_ipif->ipif_flags |= IPIF_NOXMIT; 5764 ill->ill_phyint->phyint_flags |= PHYI_VIRTUAL; 5765 } else { 5766 /* pt-pt supports multicast. */ 5767 ill->ill_flags |= ILLF_MULTICAST; 5768 ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT; 5769 } 5770 } 5771 } else { 5772 ill->ill_net_type = IRE_IF_RESOLVER; 5773 if (ill->ill_bcast_mp != NULL) 5774 freemsg(ill->ill_bcast_mp); 5775 ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr, 5776 ill->ill_bcast_addr_length, ill->ill_sap, 5777 ill->ill_sap_length); 5778 /* 5779 * Later detect lack of DLPI driver multicast 5780 * capability by catching DL_ENABMULTI errors in 5781 * ip_rput_dlpi. 5782 */ 5783 ill->ill_flags |= ILLF_MULTICAST; 5784 if (!ill->ill_isv6) 5785 ill->ill_ipif->ipif_flags |= IPIF_BROADCAST; 5786 } 5787 /* By default an interface does not support any CoS marking */ 5788 ill->ill_flags &= ~ILLF_COS_ENABLED; 5789 5790 /* 5791 * If we get QoS information in DL_INFO_ACK, the device supports 5792 * some form of CoS marking, set ILLF_COS_ENABLED. 5793 */ 5794 sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset, 5795 dlia->dl_qos_length); 5796 if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) { 5797 ill->ill_flags |= ILLF_COS_ENABLED; 5798 } 5799 5800 /* Clear any previous error indication. */ 5801 ill->ill_error = 0; 5802 freemsg(mp); 5803 } 5804 5805 /* 5806 * Perform various checks to verify that an address would make sense as a 5807 * local, remote, or subnet interface address. 5808 */ 5809 static boolean_t 5810 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask) 5811 { 5812 ipaddr_t net_mask; 5813 5814 /* 5815 * Don't allow all zeroes, all ones or experimental address, but allow 5816 * all ones netmask. 5817 */ 5818 if ((net_mask = ip_net_mask(addr)) == 0) 5819 return (B_FALSE); 5820 /* A given netmask overrides the "guess" netmask */ 5821 if (subnet_mask != 0) 5822 net_mask = subnet_mask; 5823 if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) || 5824 (addr == (addr | ~net_mask)))) { 5825 return (B_FALSE); 5826 } 5827 if (CLASSD(addr)) 5828 return (B_FALSE); 5829 5830 return (B_TRUE); 5831 } 5832 5833 /* 5834 * ipif_lookup_group 5835 * Returns held ipif 5836 */ 5837 ipif_t * 5838 ipif_lookup_group(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst) 5839 { 5840 ire_t *ire; 5841 ipif_t *ipif; 5842 5843 ire = ire_lookup_multi(group, zoneid, ipst); 5844 if (ire == NULL) 5845 return (NULL); 5846 ipif = ire->ire_ipif; 5847 ipif_refhold(ipif); 5848 ire_refrele(ire); 5849 return (ipif); 5850 } 5851 5852 /* 5853 * Look for an ipif with the specified interface address and destination. 5854 * The destination address is used only for matching point-to-point interfaces. 5855 */ 5856 ipif_t * 5857 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, queue_t *q, mblk_t *mp, 5858 ipsq_func_t func, int *error, ip_stack_t *ipst) 5859 { 5860 ipif_t *ipif; 5861 ill_t *ill; 5862 ill_walk_context_t ctx; 5863 ipsq_t *ipsq; 5864 5865 if (error != NULL) 5866 *error = 0; 5867 5868 /* 5869 * First match all the point-to-point interfaces 5870 * before looking at non-point-to-point interfaces. 5871 * This is done to avoid returning non-point-to-point 5872 * ipif instead of unnumbered point-to-point ipif. 5873 */ 5874 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5875 ill = ILL_START_WALK_V4(&ctx, ipst); 5876 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 5877 GRAB_CONN_LOCK(q); 5878 mutex_enter(&ill->ill_lock); 5879 for (ipif = ill->ill_ipif; ipif != NULL; 5880 ipif = ipif->ipif_next) { 5881 /* Allow the ipif to be down */ 5882 if ((ipif->ipif_flags & IPIF_POINTOPOINT) && 5883 (ipif->ipif_lcl_addr == if_addr) && 5884 (ipif->ipif_pp_dst_addr == dst)) { 5885 /* 5886 * The block comment at the start of ipif_down 5887 * explains the use of the macros used below 5888 */ 5889 if (IPIF_CAN_LOOKUP(ipif)) { 5890 ipif_refhold_locked(ipif); 5891 mutex_exit(&ill->ill_lock); 5892 RELEASE_CONN_LOCK(q); 5893 rw_exit(&ipst->ips_ill_g_lock); 5894 return (ipif); 5895 } else if (IPIF_CAN_WAIT(ipif, q)) { 5896 ipsq = ill->ill_phyint->phyint_ipsq; 5897 mutex_enter(&ipsq->ipsq_lock); 5898 mutex_exit(&ill->ill_lock); 5899 rw_exit(&ipst->ips_ill_g_lock); 5900 ipsq_enq(ipsq, q, mp, func, NEW_OP, 5901 ill); 5902 mutex_exit(&ipsq->ipsq_lock); 5903 RELEASE_CONN_LOCK(q); 5904 *error = EINPROGRESS; 5905 return (NULL); 5906 } 5907 } 5908 } 5909 mutex_exit(&ill->ill_lock); 5910 RELEASE_CONN_LOCK(q); 5911 } 5912 rw_exit(&ipst->ips_ill_g_lock); 5913 5914 /* lookup the ipif based on interface address */ 5915 ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, q, mp, func, error, 5916 ipst); 5917 ASSERT(ipif == NULL || !ipif->ipif_isv6); 5918 return (ipif); 5919 } 5920 5921 /* 5922 * Look for an ipif with the specified address. For point-point links 5923 * we look for matches on either the destination address and the local 5924 * address, but we ignore the check on the local address if IPIF_UNNUMBERED 5925 * is set. 5926 * Matches on a specific ill if match_ill is set. 5927 */ 5928 ipif_t * 5929 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid, queue_t *q, 5930 mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst) 5931 { 5932 ipif_t *ipif; 5933 ill_t *ill; 5934 boolean_t ptp = B_FALSE; 5935 ipsq_t *ipsq; 5936 ill_walk_context_t ctx; 5937 5938 if (error != NULL) 5939 *error = 0; 5940 5941 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 5942 /* 5943 * Repeat twice, first based on local addresses and 5944 * next time for pointopoint. 5945 */ 5946 repeat: 5947 ill = ILL_START_WALK_V4(&ctx, ipst); 5948 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 5949 if (match_ill != NULL && ill != match_ill) { 5950 continue; 5951 } 5952 GRAB_CONN_LOCK(q); 5953 mutex_enter(&ill->ill_lock); 5954 for (ipif = ill->ill_ipif; ipif != NULL; 5955 ipif = ipif->ipif_next) { 5956 if (zoneid != ALL_ZONES && 5957 zoneid != ipif->ipif_zoneid && 5958 ipif->ipif_zoneid != ALL_ZONES) 5959 continue; 5960 /* Allow the ipif to be down */ 5961 if ((!ptp && (ipif->ipif_lcl_addr == addr) && 5962 ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 5963 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) && 5964 (ipif->ipif_pp_dst_addr == addr))) { 5965 /* 5966 * The block comment at the start of ipif_down 5967 * explains the use of the macros used below 5968 */ 5969 if (IPIF_CAN_LOOKUP(ipif)) { 5970 ipif_refhold_locked(ipif); 5971 mutex_exit(&ill->ill_lock); 5972 RELEASE_CONN_LOCK(q); 5973 rw_exit(&ipst->ips_ill_g_lock); 5974 return (ipif); 5975 } else if (IPIF_CAN_WAIT(ipif, q)) { 5976 ipsq = ill->ill_phyint->phyint_ipsq; 5977 mutex_enter(&ipsq->ipsq_lock); 5978 mutex_exit(&ill->ill_lock); 5979 rw_exit(&ipst->ips_ill_g_lock); 5980 ipsq_enq(ipsq, q, mp, func, NEW_OP, 5981 ill); 5982 mutex_exit(&ipsq->ipsq_lock); 5983 RELEASE_CONN_LOCK(q); 5984 *error = EINPROGRESS; 5985 return (NULL); 5986 } 5987 } 5988 } 5989 mutex_exit(&ill->ill_lock); 5990 RELEASE_CONN_LOCK(q); 5991 } 5992 5993 /* If we already did the ptp case, then we are done */ 5994 if (ptp) { 5995 rw_exit(&ipst->ips_ill_g_lock); 5996 if (error != NULL) 5997 *error = ENXIO; 5998 return (NULL); 5999 } 6000 ptp = B_TRUE; 6001 goto repeat; 6002 } 6003 6004 /* 6005 * Look for an ipif with the specified address. For point-point links 6006 * we look for matches on either the destination address and the local 6007 * address, but we ignore the check on the local address if IPIF_UNNUMBERED 6008 * is set. 6009 * Matches on a specific ill if match_ill is set. 6010 * Return the zoneid for the ipif which matches. ALL_ZONES if no match. 6011 */ 6012 zoneid_t 6013 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst) 6014 { 6015 zoneid_t zoneid; 6016 ipif_t *ipif; 6017 ill_t *ill; 6018 boolean_t ptp = B_FALSE; 6019 ill_walk_context_t ctx; 6020 6021 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 6022 /* 6023 * Repeat twice, first based on local addresses and 6024 * next time for pointopoint. 6025 */ 6026 repeat: 6027 ill = ILL_START_WALK_V4(&ctx, ipst); 6028 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 6029 if (match_ill != NULL && ill != match_ill) { 6030 continue; 6031 } 6032 mutex_enter(&ill->ill_lock); 6033 for (ipif = ill->ill_ipif; ipif != NULL; 6034 ipif = ipif->ipif_next) { 6035 /* Allow the ipif to be down */ 6036 if ((!ptp && (ipif->ipif_lcl_addr == addr) && 6037 ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) || 6038 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) && 6039 (ipif->ipif_pp_dst_addr == addr)) && 6040 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) { 6041 zoneid = ipif->ipif_zoneid; 6042 mutex_exit(&ill->ill_lock); 6043 rw_exit(&ipst->ips_ill_g_lock); 6044 /* 6045 * If ipif_zoneid was ALL_ZONES then we have 6046 * a trusted extensions shared IP address. 6047 * In that case GLOBAL_ZONEID works to send. 6048 */ 6049 if (zoneid == ALL_ZONES) 6050 zoneid = GLOBAL_ZONEID; 6051 return (zoneid); 6052 } 6053 } 6054 mutex_exit(&ill->ill_lock); 6055 } 6056 6057 /* If we already did the ptp case, then we are done */ 6058 if (ptp) { 6059 rw_exit(&ipst->ips_ill_g_lock); 6060 return (ALL_ZONES); 6061 } 6062 ptp = B_TRUE; 6063 goto repeat; 6064 } 6065 6066 /* 6067 * Look for an ipif that matches the specified remote address i.e. the 6068 * ipif that would receive the specified packet. 6069 * First look for directly connected interfaces and then do a recursive 6070 * IRE lookup and pick the first ipif corresponding to the source address in the 6071 * ire. 6072 * Returns: held ipif 6073 */ 6074 ipif_t * 6075 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid) 6076 { 6077 ipif_t *ipif; 6078 ire_t *ire; 6079 ip_stack_t *ipst = ill->ill_ipst; 6080 6081 ASSERT(!ill->ill_isv6); 6082 6083 /* 6084 * Someone could be changing this ipif currently or change it 6085 * after we return this. Thus a few packets could use the old 6086 * old values. However structure updates/creates (ire, ilg, ilm etc) 6087 * will atomically be updated or cleaned up with the new value 6088 * Thus we don't need a lock to check the flags or other attrs below. 6089 */ 6090 mutex_enter(&ill->ill_lock); 6091 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 6092 if (!IPIF_CAN_LOOKUP(ipif)) 6093 continue; 6094 if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid && 6095 ipif->ipif_zoneid != ALL_ZONES) 6096 continue; 6097 /* Allow the ipif to be down */ 6098 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 6099 if ((ipif->ipif_pp_dst_addr == addr) || 6100 (!(ipif->ipif_flags & IPIF_UNNUMBERED) && 6101 ipif->ipif_lcl_addr == addr)) { 6102 ipif_refhold_locked(ipif); 6103 mutex_exit(&ill->ill_lock); 6104 return (ipif); 6105 } 6106 } else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) { 6107 ipif_refhold_locked(ipif); 6108 mutex_exit(&ill->ill_lock); 6109 return (ipif); 6110 } 6111 } 6112 mutex_exit(&ill->ill_lock); 6113 ire = ire_route_lookup(addr, 0, 0, 0, NULL, NULL, zoneid, 6114 NULL, MATCH_IRE_RECURSIVE, ipst); 6115 if (ire != NULL) { 6116 /* 6117 * The callers of this function wants to know the 6118 * interface on which they have to send the replies 6119 * back. For IRE_CACHES that have ire_stq and ire_ipif 6120 * derived from different ills, we really don't care 6121 * what we return here. 6122 */ 6123 ipif = ire->ire_ipif; 6124 if (ipif != NULL) { 6125 ipif_refhold(ipif); 6126 ire_refrele(ire); 6127 return (ipif); 6128 } 6129 ire_refrele(ire); 6130 } 6131 /* Pick the first interface */ 6132 ipif = ipif_get_next_ipif(NULL, ill); 6133 return (ipif); 6134 } 6135 6136 /* 6137 * This func does not prevent refcnt from increasing. But if 6138 * the caller has taken steps to that effect, then this func 6139 * can be used to determine whether the ill has become quiescent 6140 */ 6141 boolean_t 6142 ill_is_quiescent(ill_t *ill) 6143 { 6144 ipif_t *ipif; 6145 6146 ASSERT(MUTEX_HELD(&ill->ill_lock)); 6147 6148 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 6149 if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) { 6150 return (B_FALSE); 6151 } 6152 } 6153 if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0 || 6154 ill->ill_nce_cnt != 0 || ill->ill_srcif_refcnt != 0 || 6155 ill->ill_mrtun_refcnt != 0) { 6156 return (B_FALSE); 6157 } 6158 return (B_TRUE); 6159 } 6160 6161 /* 6162 * This func does not prevent refcnt from increasing. But if 6163 * the caller has taken steps to that effect, then this func 6164 * can be used to determine whether the ipif has become quiescent 6165 */ 6166 static boolean_t 6167 ipif_is_quiescent(ipif_t *ipif) 6168 { 6169 ill_t *ill; 6170 6171 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 6172 6173 if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) { 6174 return (B_FALSE); 6175 } 6176 6177 ill = ipif->ipif_ill; 6178 if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 || 6179 ill->ill_logical_down) { 6180 return (B_TRUE); 6181 } 6182 6183 /* This is the last ipif going down or being deleted on this ill */ 6184 if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) { 6185 return (B_FALSE); 6186 } 6187 6188 return (B_TRUE); 6189 } 6190 6191 /* 6192 * This func does not prevent refcnt from increasing. But if 6193 * the caller has taken steps to that effect, then this func 6194 * can be used to determine whether the ipifs marked with IPIF_MOVING 6195 * have become quiescent and can be moved in a failover/failback. 6196 */ 6197 static ipif_t * 6198 ill_quiescent_to_move(ill_t *ill) 6199 { 6200 ipif_t *ipif; 6201 6202 ASSERT(MUTEX_HELD(&ill->ill_lock)); 6203 6204 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 6205 if (ipif->ipif_state_flags & IPIF_MOVING) { 6206 if (ipif->ipif_refcnt != 0 || ipif->ipif_ire_cnt != 0) { 6207 return (ipif); 6208 } 6209 } 6210 } 6211 return (NULL); 6212 } 6213 6214 /* 6215 * The ipif/ill/ire has been refreled. Do the tail processing. 6216 * Determine if the ipif or ill in question has become quiescent and if so 6217 * wakeup close and/or restart any queued pending ioctl that is waiting 6218 * for the ipif_down (or ill_down) 6219 */ 6220 void 6221 ipif_ill_refrele_tail(ill_t *ill) 6222 { 6223 mblk_t *mp; 6224 conn_t *connp; 6225 ipsq_t *ipsq; 6226 ipif_t *ipif; 6227 dl_notify_ind_t *dlindp; 6228 6229 ASSERT(MUTEX_HELD(&ill->ill_lock)); 6230 6231 if ((ill->ill_state_flags & ILL_CONDEMNED) && 6232 ill_is_quiescent(ill)) { 6233 /* ill_close may be waiting */ 6234 cv_broadcast(&ill->ill_cv); 6235 } 6236 6237 /* ipsq can't change because ill_lock is held */ 6238 ipsq = ill->ill_phyint->phyint_ipsq; 6239 if (ipsq->ipsq_waitfor == 0) { 6240 /* Not waiting for anything, just return. */ 6241 mutex_exit(&ill->ill_lock); 6242 return; 6243 } 6244 ASSERT(ipsq->ipsq_pending_mp != NULL && 6245 ipsq->ipsq_pending_ipif != NULL); 6246 /* 6247 * ipif->ipif_refcnt must go down to zero for restarting REMOVEIF. 6248 * Last ipif going down needs to down the ill, so ill_ire_cnt must 6249 * be zero for restarting an ioctl that ends up downing the ill. 6250 */ 6251 ipif = ipsq->ipsq_pending_ipif; 6252 if (ipif->ipif_ill != ill) { 6253 /* The ioctl is pending on some other ill. */ 6254 mutex_exit(&ill->ill_lock); 6255 return; 6256 } 6257 6258 switch (ipsq->ipsq_waitfor) { 6259 case IPIF_DOWN: 6260 case IPIF_FREE: 6261 if (!ipif_is_quiescent(ipif)) { 6262 mutex_exit(&ill->ill_lock); 6263 return; 6264 } 6265 break; 6266 6267 case ILL_DOWN: 6268 case ILL_FREE: 6269 /* 6270 * case ILL_FREE arises only for loopback. otherwise ill_delete 6271 * waits synchronously in ip_close, and no message is queued in 6272 * ipsq_pending_mp at all in this case 6273 */ 6274 if (!ill_is_quiescent(ill)) { 6275 mutex_exit(&ill->ill_lock); 6276 return; 6277 } 6278 6279 break; 6280 6281 case ILL_MOVE_OK: 6282 if (ill_quiescent_to_move(ill) != NULL) { 6283 mutex_exit(&ill->ill_lock); 6284 return; 6285 } 6286 6287 break; 6288 default: 6289 cmn_err(CE_PANIC, "ipsq: %p unknown ipsq_waitfor %d\n", 6290 (void *)ipsq, ipsq->ipsq_waitfor); 6291 } 6292 6293 /* 6294 * Incr refcnt for the qwriter_ip call below which 6295 * does a refrele 6296 */ 6297 ill_refhold_locked(ill); 6298 mutex_exit(&ill->ill_lock); 6299 6300 mp = ipsq_pending_mp_get(ipsq, &connp); 6301 ASSERT(mp != NULL); 6302 6303 /* 6304 * NOTE: all of the qwriter_ip() calls below use CUR_OP since 6305 * we can only get here when the current operation decides it 6306 * it needs to quiesce via ipsq_pending_mp_add(). 6307 */ 6308 switch (mp->b_datap->db_type) { 6309 case M_PCPROTO: 6310 case M_PROTO: 6311 /* 6312 * For now, only DL_NOTIFY_IND messages can use this facility. 6313 */ 6314 dlindp = (dl_notify_ind_t *)mp->b_rptr; 6315 ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND); 6316 6317 switch (dlindp->dl_notification) { 6318 case DL_NOTE_PHYS_ADDR: 6319 qwriter_ip(ill, ill->ill_rq, mp, 6320 ill_set_phys_addr_tail, CUR_OP, B_TRUE); 6321 return; 6322 default: 6323 ASSERT(0); 6324 } 6325 break; 6326 6327 case M_ERROR: 6328 case M_HANGUP: 6329 qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP, 6330 B_TRUE); 6331 return; 6332 6333 case M_IOCTL: 6334 case M_IOCDATA: 6335 qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) : 6336 ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE); 6337 return; 6338 6339 default: 6340 cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p " 6341 "db_type %d\n", (void *)mp, mp->b_datap->db_type); 6342 } 6343 } 6344 6345 #ifdef ILL_DEBUG 6346 /* Reuse trace buffer from beginning (if reached the end) and record trace */ 6347 void 6348 th_trace_rrecord(th_trace_t *th_trace) 6349 { 6350 tr_buf_t *tr_buf; 6351 uint_t lastref; 6352 6353 lastref = th_trace->th_trace_lastref; 6354 lastref++; 6355 if (lastref == TR_BUF_MAX) 6356 lastref = 0; 6357 th_trace->th_trace_lastref = lastref; 6358 tr_buf = &th_trace->th_trbuf[lastref]; 6359 tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, IP_STACK_DEPTH); 6360 } 6361 6362 th_trace_t * 6363 th_trace_ipif_lookup(ipif_t *ipif) 6364 { 6365 int bucket_id; 6366 th_trace_t *th_trace; 6367 6368 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 6369 6370 bucket_id = IP_TR_HASH(curthread); 6371 ASSERT(bucket_id < IP_TR_HASH_MAX); 6372 6373 for (th_trace = ipif->ipif_trace[bucket_id]; th_trace != NULL; 6374 th_trace = th_trace->th_next) { 6375 if (th_trace->th_id == curthread) 6376 return (th_trace); 6377 } 6378 return (NULL); 6379 } 6380 6381 void 6382 ipif_trace_ref(ipif_t *ipif) 6383 { 6384 int bucket_id; 6385 th_trace_t *th_trace; 6386 6387 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 6388 6389 if (ipif->ipif_trace_disable) 6390 return; 6391 6392 /* 6393 * Attempt to locate the trace buffer for the curthread. 6394 * If it does not exist, then allocate a new trace buffer 6395 * and link it in list of trace bufs for this ipif, at the head 6396 */ 6397 th_trace = th_trace_ipif_lookup(ipif); 6398 if (th_trace == NULL) { 6399 bucket_id = IP_TR_HASH(curthread); 6400 th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t), 6401 KM_NOSLEEP); 6402 if (th_trace == NULL) { 6403 ipif->ipif_trace_disable = B_TRUE; 6404 ipif_trace_cleanup(ipif); 6405 return; 6406 } 6407 th_trace->th_id = curthread; 6408 th_trace->th_next = ipif->ipif_trace[bucket_id]; 6409 th_trace->th_prev = &ipif->ipif_trace[bucket_id]; 6410 if (th_trace->th_next != NULL) 6411 th_trace->th_next->th_prev = &th_trace->th_next; 6412 ipif->ipif_trace[bucket_id] = th_trace; 6413 } 6414 ASSERT(th_trace->th_refcnt >= 0 && 6415 th_trace->th_refcnt < TR_BUF_MAX -1); 6416 th_trace->th_refcnt++; 6417 th_trace_rrecord(th_trace); 6418 } 6419 6420 void 6421 ipif_untrace_ref(ipif_t *ipif) 6422 { 6423 th_trace_t *th_trace; 6424 6425 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 6426 6427 if (ipif->ipif_trace_disable) 6428 return; 6429 th_trace = th_trace_ipif_lookup(ipif); 6430 ASSERT(th_trace != NULL); 6431 ASSERT(th_trace->th_refcnt > 0); 6432 6433 th_trace->th_refcnt--; 6434 th_trace_rrecord(th_trace); 6435 } 6436 6437 th_trace_t * 6438 th_trace_ill_lookup(ill_t *ill) 6439 { 6440 th_trace_t *th_trace; 6441 int bucket_id; 6442 6443 ASSERT(MUTEX_HELD(&ill->ill_lock)); 6444 6445 bucket_id = IP_TR_HASH(curthread); 6446 ASSERT(bucket_id < IP_TR_HASH_MAX); 6447 6448 for (th_trace = ill->ill_trace[bucket_id]; th_trace != NULL; 6449 th_trace = th_trace->th_next) { 6450 if (th_trace->th_id == curthread) 6451 return (th_trace); 6452 } 6453 return (NULL); 6454 } 6455 6456 void 6457 ill_trace_ref(ill_t *ill) 6458 { 6459 int bucket_id; 6460 th_trace_t *th_trace; 6461 6462 ASSERT(MUTEX_HELD(&ill->ill_lock)); 6463 if (ill->ill_trace_disable) 6464 return; 6465 /* 6466 * Attempt to locate the trace buffer for the curthread. 6467 * If it does not exist, then allocate a new trace buffer 6468 * and link it in list of trace bufs for this ill, at the head 6469 */ 6470 th_trace = th_trace_ill_lookup(ill); 6471 if (th_trace == NULL) { 6472 bucket_id = IP_TR_HASH(curthread); 6473 th_trace = (th_trace_t *)kmem_zalloc(sizeof (th_trace_t), 6474 KM_NOSLEEP); 6475 if (th_trace == NULL) { 6476 ill->ill_trace_disable = B_TRUE; 6477 ill_trace_cleanup(ill); 6478 return; 6479 } 6480 th_trace->th_id = curthread; 6481 th_trace->th_next = ill->ill_trace[bucket_id]; 6482 th_trace->th_prev = &ill->ill_trace[bucket_id]; 6483 if (th_trace->th_next != NULL) 6484 th_trace->th_next->th_prev = &th_trace->th_next; 6485 ill->ill_trace[bucket_id] = th_trace; 6486 } 6487 ASSERT(th_trace->th_refcnt >= 0 && 6488 th_trace->th_refcnt < TR_BUF_MAX - 1); 6489 6490 th_trace->th_refcnt++; 6491 th_trace_rrecord(th_trace); 6492 } 6493 6494 void 6495 ill_untrace_ref(ill_t *ill) 6496 { 6497 th_trace_t *th_trace; 6498 6499 ASSERT(MUTEX_HELD(&ill->ill_lock)); 6500 6501 if (ill->ill_trace_disable) 6502 return; 6503 th_trace = th_trace_ill_lookup(ill); 6504 ASSERT(th_trace != NULL); 6505 ASSERT(th_trace->th_refcnt > 0); 6506 6507 th_trace->th_refcnt--; 6508 th_trace_rrecord(th_trace); 6509 } 6510 6511 /* 6512 * Verify that this thread has no refs to the ipif and free 6513 * the trace buffers 6514 */ 6515 /* ARGSUSED */ 6516 void 6517 ipif_thread_exit(ipif_t *ipif, void *dummy) 6518 { 6519 th_trace_t *th_trace; 6520 6521 mutex_enter(&ipif->ipif_ill->ill_lock); 6522 6523 th_trace = th_trace_ipif_lookup(ipif); 6524 if (th_trace == NULL) { 6525 mutex_exit(&ipif->ipif_ill->ill_lock); 6526 return; 6527 } 6528 ASSERT(th_trace->th_refcnt == 0); 6529 /* unlink th_trace and free it */ 6530 *th_trace->th_prev = th_trace->th_next; 6531 if (th_trace->th_next != NULL) 6532 th_trace->th_next->th_prev = th_trace->th_prev; 6533 th_trace->th_next = NULL; 6534 th_trace->th_prev = NULL; 6535 kmem_free(th_trace, sizeof (th_trace_t)); 6536 6537 mutex_exit(&ipif->ipif_ill->ill_lock); 6538 } 6539 6540 /* 6541 * Verify that this thread has no refs to the ill and free 6542 * the trace buffers 6543 */ 6544 /* ARGSUSED */ 6545 void 6546 ill_thread_exit(ill_t *ill, void *dummy) 6547 { 6548 th_trace_t *th_trace; 6549 6550 mutex_enter(&ill->ill_lock); 6551 6552 th_trace = th_trace_ill_lookup(ill); 6553 if (th_trace == NULL) { 6554 mutex_exit(&ill->ill_lock); 6555 return; 6556 } 6557 ASSERT(th_trace->th_refcnt == 0); 6558 /* unlink th_trace and free it */ 6559 *th_trace->th_prev = th_trace->th_next; 6560 if (th_trace->th_next != NULL) 6561 th_trace->th_next->th_prev = th_trace->th_prev; 6562 th_trace->th_next = NULL; 6563 th_trace->th_prev = NULL; 6564 kmem_free(th_trace, sizeof (th_trace_t)); 6565 6566 mutex_exit(&ill->ill_lock); 6567 } 6568 #endif 6569 6570 #ifdef ILL_DEBUG 6571 void 6572 ip_thread_exit_stack(ip_stack_t *ipst) 6573 { 6574 ill_t *ill; 6575 ipif_t *ipif; 6576 ill_walk_context_t ctx; 6577 6578 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 6579 ill = ILL_START_WALK_ALL(&ctx, ipst); 6580 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 6581 for (ipif = ill->ill_ipif; ipif != NULL; 6582 ipif = ipif->ipif_next) { 6583 ipif_thread_exit(ipif, NULL); 6584 } 6585 ill_thread_exit(ill, NULL); 6586 } 6587 rw_exit(&ipst->ips_ill_g_lock); 6588 6589 ire_walk(ire_thread_exit, NULL, ipst); 6590 ndp_walk_common(ipst->ips_ndp4, NULL, nce_thread_exit, NULL, B_FALSE); 6591 ndp_walk_common(ipst->ips_ndp6, NULL, nce_thread_exit, NULL, B_FALSE); 6592 } 6593 6594 /* 6595 * This is a function which is called from thread_exit 6596 * that can be used to debug reference count issues in IP. See comment in 6597 * <inet/ip.h> on how it is used. 6598 */ 6599 void 6600 ip_thread_exit(void) 6601 { 6602 netstack_t *ns; 6603 6604 ns = netstack_get_current(); 6605 if (ns != NULL) { 6606 ip_thread_exit_stack(ns->netstack_ip); 6607 netstack_rele(ns); 6608 } 6609 } 6610 6611 /* 6612 * Called when ipif is unplumbed or when memory alloc fails 6613 */ 6614 void 6615 ipif_trace_cleanup(ipif_t *ipif) 6616 { 6617 int i; 6618 th_trace_t *th_trace; 6619 th_trace_t *th_trace_next; 6620 6621 for (i = 0; i < IP_TR_HASH_MAX; i++) { 6622 for (th_trace = ipif->ipif_trace[i]; th_trace != NULL; 6623 th_trace = th_trace_next) { 6624 th_trace_next = th_trace->th_next; 6625 kmem_free(th_trace, sizeof (th_trace_t)); 6626 } 6627 ipif->ipif_trace[i] = NULL; 6628 } 6629 } 6630 6631 /* 6632 * Called when ill is unplumbed or when memory alloc fails 6633 */ 6634 void 6635 ill_trace_cleanup(ill_t *ill) 6636 { 6637 int i; 6638 th_trace_t *th_trace; 6639 th_trace_t *th_trace_next; 6640 6641 for (i = 0; i < IP_TR_HASH_MAX; i++) { 6642 for (th_trace = ill->ill_trace[i]; th_trace != NULL; 6643 th_trace = th_trace_next) { 6644 th_trace_next = th_trace->th_next; 6645 kmem_free(th_trace, sizeof (th_trace_t)); 6646 } 6647 ill->ill_trace[i] = NULL; 6648 } 6649 } 6650 6651 #else 6652 void ip_thread_exit(void) {} 6653 #endif 6654 6655 void 6656 ipif_refhold_locked(ipif_t *ipif) 6657 { 6658 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 6659 ipif->ipif_refcnt++; 6660 IPIF_TRACE_REF(ipif); 6661 } 6662 6663 void 6664 ipif_refhold(ipif_t *ipif) 6665 { 6666 ill_t *ill; 6667 6668 ill = ipif->ipif_ill; 6669 mutex_enter(&ill->ill_lock); 6670 ipif->ipif_refcnt++; 6671 IPIF_TRACE_REF(ipif); 6672 mutex_exit(&ill->ill_lock); 6673 } 6674 6675 /* 6676 * Must not be called while holding any locks. Otherwise if this is 6677 * the last reference to be released there is a chance of recursive mutex 6678 * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying 6679 * to restart an ioctl. 6680 */ 6681 void 6682 ipif_refrele(ipif_t *ipif) 6683 { 6684 ill_t *ill; 6685 6686 ill = ipif->ipif_ill; 6687 6688 mutex_enter(&ill->ill_lock); 6689 ASSERT(ipif->ipif_refcnt != 0); 6690 ipif->ipif_refcnt--; 6691 IPIF_UNTRACE_REF(ipif); 6692 if (ipif->ipif_refcnt != 0) { 6693 mutex_exit(&ill->ill_lock); 6694 return; 6695 } 6696 6697 /* Drops the ill_lock */ 6698 ipif_ill_refrele_tail(ill); 6699 } 6700 6701 ipif_t * 6702 ipif_get_next_ipif(ipif_t *curr, ill_t *ill) 6703 { 6704 ipif_t *ipif; 6705 6706 mutex_enter(&ill->ill_lock); 6707 for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next); 6708 ipif != NULL; ipif = ipif->ipif_next) { 6709 if (!IPIF_CAN_LOOKUP(ipif)) 6710 continue; 6711 ipif_refhold_locked(ipif); 6712 mutex_exit(&ill->ill_lock); 6713 return (ipif); 6714 } 6715 mutex_exit(&ill->ill_lock); 6716 return (NULL); 6717 } 6718 6719 /* 6720 * TODO: make this table extendible at run time 6721 * Return a pointer to the mac type info for 'mac_type' 6722 */ 6723 static ip_m_t * 6724 ip_m_lookup(t_uscalar_t mac_type) 6725 { 6726 ip_m_t *ipm; 6727 6728 for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++) 6729 if (ipm->ip_m_mac_type == mac_type) 6730 return (ipm); 6731 return (NULL); 6732 } 6733 6734 /* 6735 * ip_rt_add is called to add an IPv4 route to the forwarding table. 6736 * ipif_arg is passed in to associate it with the correct interface. 6737 * We may need to restart this operation if the ipif cannot be looked up 6738 * due to an exclusive operation that is currently in progress. The restart 6739 * entry point is specified by 'func' 6740 */ 6741 int 6742 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr, 6743 ipaddr_t src_addr, int flags, ipif_t *ipif_arg, ipif_t *src_ipif, 6744 ire_t **ire_arg, boolean_t ioctl_msg, queue_t *q, mblk_t *mp, 6745 ipsq_func_t func, struct rtsa_s *sp, ip_stack_t *ipst) 6746 { 6747 ire_t *ire; 6748 ire_t *gw_ire = NULL; 6749 ipif_t *ipif = NULL; 6750 boolean_t ipif_refheld = B_FALSE; 6751 uint_t type; 6752 int match_flags = MATCH_IRE_TYPE; 6753 int error; 6754 tsol_gc_t *gc = NULL; 6755 tsol_gcgrp_t *gcgrp = NULL; 6756 boolean_t gcgrp_xtraref = B_FALSE; 6757 6758 ip1dbg(("ip_rt_add:")); 6759 6760 if (ire_arg != NULL) 6761 *ire_arg = NULL; 6762 6763 /* 6764 * If this is the case of RTF_HOST being set, then we set the netmask 6765 * to all ones (regardless if one was supplied). 6766 */ 6767 if (flags & RTF_HOST) 6768 mask = IP_HOST_MASK; 6769 6770 /* 6771 * Prevent routes with a zero gateway from being created (since 6772 * interfaces can currently be plumbed and brought up no assigned 6773 * address). 6774 * For routes with RTA_SRCIFP, the gateway address can be 0.0.0.0. 6775 */ 6776 if (gw_addr == 0 && src_ipif == NULL) 6777 return (ENETUNREACH); 6778 /* 6779 * Get the ipif, if any, corresponding to the gw_addr 6780 */ 6781 if (gw_addr != 0) { 6782 ipif = ipif_lookup_interface(gw_addr, dst_addr, q, mp, func, 6783 &error, ipst); 6784 if (ipif != NULL) { 6785 if (IS_VNI(ipif->ipif_ill)) { 6786 ipif_refrele(ipif); 6787 return (EINVAL); 6788 } 6789 ipif_refheld = B_TRUE; 6790 } else if (error == EINPROGRESS) { 6791 ip1dbg(("ip_rt_add: null and EINPROGRESS")); 6792 return (EINPROGRESS); 6793 } else { 6794 error = 0; 6795 } 6796 } 6797 6798 if (ipif != NULL) { 6799 ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif nonnull")); 6800 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 6801 } else { 6802 ip1dbg(("ip_rt_add: ipif_lookup_interface done ipif is null")); 6803 } 6804 6805 /* 6806 * GateD will attempt to create routes with a loopback interface 6807 * address as the gateway and with RTF_GATEWAY set. We allow 6808 * these routes to be added, but create them as interface routes 6809 * since the gateway is an interface address. 6810 */ 6811 if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) { 6812 flags &= ~RTF_GATEWAY; 6813 if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK && 6814 mask == IP_HOST_MASK) { 6815 ire = ire_ctable_lookup(dst_addr, 0, IRE_LOOPBACK, ipif, 6816 ALL_ZONES, NULL, match_flags, ipst); 6817 if (ire != NULL) { 6818 ire_refrele(ire); 6819 if (ipif_refheld) 6820 ipif_refrele(ipif); 6821 return (EEXIST); 6822 } 6823 ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x" 6824 "for 0x%x\n", (void *)ipif, 6825 ipif->ipif_ire_type, 6826 ntohl(ipif->ipif_lcl_addr))); 6827 ire = ire_create( 6828 (uchar_t *)&dst_addr, /* dest address */ 6829 (uchar_t *)&mask, /* mask */ 6830 (uchar_t *)&ipif->ipif_src_addr, 6831 NULL, /* no gateway */ 6832 NULL, 6833 &ipif->ipif_mtu, 6834 NULL, 6835 ipif->ipif_rq, /* recv-from queue */ 6836 NULL, /* no send-to queue */ 6837 ipif->ipif_ire_type, /* LOOPBACK */ 6838 NULL, 6839 ipif, 6840 NULL, 6841 0, 6842 0, 6843 0, 6844 (ipif->ipif_flags & IPIF_PRIVATE) ? 6845 RTF_PRIVATE : 0, 6846 &ire_uinfo_null, 6847 NULL, 6848 NULL, 6849 ipst); 6850 6851 if (ire == NULL) { 6852 if (ipif_refheld) 6853 ipif_refrele(ipif); 6854 return (ENOMEM); 6855 } 6856 error = ire_add(&ire, q, mp, func, B_FALSE); 6857 if (error == 0) 6858 goto save_ire; 6859 if (ipif_refheld) 6860 ipif_refrele(ipif); 6861 return (error); 6862 6863 } 6864 } 6865 6866 /* 6867 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set 6868 * and the gateway address provided is one of the system's interface 6869 * addresses. By using the routing socket interface and supplying an 6870 * RTA_IFP sockaddr with an interface index, an alternate method of 6871 * specifying an interface route to be created is available which uses 6872 * the interface index that specifies the outgoing interface rather than 6873 * the address of an outgoing interface (which may not be able to 6874 * uniquely identify an interface). When coupled with the RTF_GATEWAY 6875 * flag, routes can be specified which not only specify the next-hop to 6876 * be used when routing to a certain prefix, but also which outgoing 6877 * interface should be used. 6878 * 6879 * Previously, interfaces would have unique addresses assigned to them 6880 * and so the address assigned to a particular interface could be used 6881 * to identify a particular interface. One exception to this was the 6882 * case of an unnumbered interface (where IPIF_UNNUMBERED was set). 6883 * 6884 * With the advent of IPv6 and its link-local addresses, this 6885 * restriction was relaxed and interfaces could share addresses between 6886 * themselves. In fact, typically all of the link-local interfaces on 6887 * an IPv6 node or router will have the same link-local address. In 6888 * order to differentiate between these interfaces, the use of an 6889 * interface index is necessary and this index can be carried inside a 6890 * RTA_IFP sockaddr (which is actually a sockaddr_dl). One restriction 6891 * of using the interface index, however, is that all of the ipif's that 6892 * are part of an ill have the same index and so the RTA_IFP sockaddr 6893 * cannot be used to differentiate between ipif's (or logical 6894 * interfaces) that belong to the same ill (physical interface). 6895 * 6896 * For example, in the following case involving IPv4 interfaces and 6897 * logical interfaces 6898 * 6899 * 192.0.2.32 255.255.255.224 192.0.2.33 U if0 6900 * 192.0.2.32 255.255.255.224 192.0.2.34 U if0:1 6901 * 192.0.2.32 255.255.255.224 192.0.2.35 U if0:2 6902 * 6903 * the ipif's corresponding to each of these interface routes can be 6904 * uniquely identified by the "gateway" (actually interface address). 6905 * 6906 * In this case involving multiple IPv6 default routes to a particular 6907 * link-local gateway, the use of RTA_IFP is necessary to specify which 6908 * default route is of interest: 6909 * 6910 * default fe80::123:4567:89ab:cdef U if0 6911 * default fe80::123:4567:89ab:cdef U if1 6912 */ 6913 6914 /* RTF_GATEWAY not set */ 6915 if (!(flags & RTF_GATEWAY)) { 6916 queue_t *stq; 6917 queue_t *rfq = NULL; 6918 ill_t *in_ill = NULL; 6919 6920 if (sp != NULL) { 6921 ip2dbg(("ip_rt_add: gateway security attributes " 6922 "cannot be set with interface route\n")); 6923 if (ipif_refheld) 6924 ipif_refrele(ipif); 6925 return (EINVAL); 6926 } 6927 6928 /* 6929 * As the interface index specified with the RTA_IFP sockaddr is 6930 * the same for all ipif's off of an ill, the matching logic 6931 * below uses MATCH_IRE_ILL if such an index was specified. 6932 * This means that routes sharing the same prefix when added 6933 * using a RTA_IFP sockaddr must have distinct interface 6934 * indices (namely, they must be on distinct ill's). 6935 * 6936 * On the other hand, since the gateway address will usually be 6937 * different for each ipif on the system, the matching logic 6938 * uses MATCH_IRE_IPIF in the case of a traditional interface 6939 * route. This means that interface routes for the same prefix 6940 * can be created if they belong to distinct ipif's and if a 6941 * RTA_IFP sockaddr is not present. 6942 */ 6943 if (ipif_arg != NULL) { 6944 if (ipif_refheld) { 6945 ipif_refrele(ipif); 6946 ipif_refheld = B_FALSE; 6947 } 6948 ipif = ipif_arg; 6949 match_flags |= MATCH_IRE_ILL; 6950 } else { 6951 /* 6952 * Check the ipif corresponding to the gw_addr 6953 */ 6954 if (ipif == NULL) 6955 return (ENETUNREACH); 6956 match_flags |= MATCH_IRE_IPIF; 6957 } 6958 ASSERT(ipif != NULL); 6959 /* 6960 * If src_ipif is not NULL, we have to create 6961 * an ire with non-null ire_in_ill value 6962 */ 6963 if (src_ipif != NULL) { 6964 in_ill = src_ipif->ipif_ill; 6965 } 6966 6967 /* 6968 * We check for an existing entry at this point. 6969 * 6970 * Since a netmask isn't passed in via the ioctl interface 6971 * (SIOCADDRT), we don't check for a matching netmask in that 6972 * case. 6973 */ 6974 if (!ioctl_msg) 6975 match_flags |= MATCH_IRE_MASK; 6976 if (src_ipif != NULL) { 6977 /* Look up in the special table */ 6978 ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE, 6979 ipif, src_ipif->ipif_ill, match_flags); 6980 } else { 6981 ire = ire_ftable_lookup(dst_addr, mask, 0, 6982 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 6983 NULL, match_flags, ipst); 6984 } 6985 if (ire != NULL) { 6986 ire_refrele(ire); 6987 if (ipif_refheld) 6988 ipif_refrele(ipif); 6989 return (EEXIST); 6990 } 6991 6992 if (src_ipif != NULL) { 6993 /* 6994 * Create the special ire for the IRE table 6995 * which hangs out of ire_in_ill. This ire 6996 * is in-between IRE_CACHE and IRE_INTERFACE. 6997 * Thus rfq is non-NULL. 6998 */ 6999 rfq = ipif->ipif_rq; 7000 } 7001 /* Create the usual interface ires */ 7002 7003 stq = (ipif->ipif_net_type == IRE_IF_RESOLVER) 7004 ? ipif->ipif_rq : ipif->ipif_wq; 7005 7006 /* 7007 * Create a copy of the IRE_LOOPBACK, 7008 * IRE_IF_NORESOLVER or IRE_IF_RESOLVER with 7009 * the modified address and netmask. 7010 */ 7011 ire = ire_create( 7012 (uchar_t *)&dst_addr, 7013 (uint8_t *)&mask, 7014 (uint8_t *)&ipif->ipif_src_addr, 7015 NULL, 7016 NULL, 7017 &ipif->ipif_mtu, 7018 NULL, 7019 rfq, 7020 stq, 7021 ipif->ipif_net_type, 7022 ipif->ipif_resolver_mp, 7023 ipif, 7024 in_ill, 7025 0, 7026 0, 7027 0, 7028 flags, 7029 &ire_uinfo_null, 7030 NULL, 7031 NULL, 7032 ipst); 7033 if (ire == NULL) { 7034 if (ipif_refheld) 7035 ipif_refrele(ipif); 7036 return (ENOMEM); 7037 } 7038 7039 /* 7040 * Some software (for example, GateD and Sun Cluster) attempts 7041 * to create (what amount to) IRE_PREFIX routes with the 7042 * loopback address as the gateway. This is primarily done to 7043 * set up prefixes with the RTF_REJECT flag set (for example, 7044 * when generating aggregate routes.) 7045 * 7046 * If the IRE type (as defined by ipif->ipif_net_type) is 7047 * IRE_LOOPBACK, then we map the request into a 7048 * IRE_IF_NORESOLVER. 7049 * 7050 * Needless to say, the real IRE_LOOPBACK is NOT created by this 7051 * routine, but rather using ire_create() directly. 7052 * 7053 */ 7054 if (ipif->ipif_net_type == IRE_LOOPBACK) 7055 ire->ire_type = IRE_IF_NORESOLVER; 7056 7057 error = ire_add(&ire, q, mp, func, B_FALSE); 7058 if (error == 0) 7059 goto save_ire; 7060 7061 /* 7062 * In the result of failure, ire_add() will have already 7063 * deleted the ire in question, so there is no need to 7064 * do that here. 7065 */ 7066 if (ipif_refheld) 7067 ipif_refrele(ipif); 7068 return (error); 7069 } 7070 if (ipif_refheld) { 7071 ipif_refrele(ipif); 7072 ipif_refheld = B_FALSE; 7073 } 7074 7075 if (src_ipif != NULL) { 7076 /* RTA_SRCIFP is not supported on RTF_GATEWAY */ 7077 ip2dbg(("ip_rt_add: SRCIF cannot be set with gateway route\n")); 7078 return (EINVAL); 7079 } 7080 /* 7081 * Get an interface IRE for the specified gateway. 7082 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the 7083 * gateway, it is currently unreachable and we fail the request 7084 * accordingly. 7085 */ 7086 ipif = ipif_arg; 7087 if (ipif_arg != NULL) 7088 match_flags |= MATCH_IRE_ILL; 7089 gw_ire = ire_ftable_lookup(gw_addr, 0, 0, IRE_INTERFACE, ipif_arg, NULL, 7090 ALL_ZONES, 0, NULL, match_flags, ipst); 7091 if (gw_ire == NULL) 7092 return (ENETUNREACH); 7093 7094 /* 7095 * We create one of three types of IREs as a result of this request 7096 * based on the netmask. A netmask of all ones (which is automatically 7097 * assumed when RTF_HOST is set) results in an IRE_HOST being created. 7098 * An all zeroes netmask implies a default route so an IRE_DEFAULT is 7099 * created. Otherwise, an IRE_PREFIX route is created for the 7100 * destination prefix. 7101 */ 7102 if (mask == IP_HOST_MASK) 7103 type = IRE_HOST; 7104 else if (mask == 0) 7105 type = IRE_DEFAULT; 7106 else 7107 type = IRE_PREFIX; 7108 7109 /* check for a duplicate entry */ 7110 ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, ipif_arg, 7111 NULL, ALL_ZONES, 0, NULL, 7112 match_flags | MATCH_IRE_MASK | MATCH_IRE_GW, ipst); 7113 if (ire != NULL) { 7114 ire_refrele(gw_ire); 7115 ire_refrele(ire); 7116 return (EEXIST); 7117 } 7118 7119 /* Security attribute exists */ 7120 if (sp != NULL) { 7121 tsol_gcgrp_addr_t ga; 7122 7123 /* find or create the gateway credentials group */ 7124 ga.ga_af = AF_INET; 7125 IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr); 7126 7127 /* we hold reference to it upon success */ 7128 gcgrp = gcgrp_lookup(&ga, B_TRUE); 7129 if (gcgrp == NULL) { 7130 ire_refrele(gw_ire); 7131 return (ENOMEM); 7132 } 7133 7134 /* 7135 * Create and add the security attribute to the group; a 7136 * reference to the group is made upon allocating a new 7137 * entry successfully. If it finds an already-existing 7138 * entry for the security attribute in the group, it simply 7139 * returns it and no new reference is made to the group. 7140 */ 7141 gc = gc_create(sp, gcgrp, &gcgrp_xtraref); 7142 if (gc == NULL) { 7143 /* release reference held by gcgrp_lookup */ 7144 GCGRP_REFRELE(gcgrp); 7145 ire_refrele(gw_ire); 7146 return (ENOMEM); 7147 } 7148 } 7149 7150 /* Create the IRE. */ 7151 ire = ire_create( 7152 (uchar_t *)&dst_addr, /* dest address */ 7153 (uchar_t *)&mask, /* mask */ 7154 /* src address assigned by the caller? */ 7155 (uchar_t *)(((src_addr != INADDR_ANY) && 7156 (flags & RTF_SETSRC)) ? &src_addr : NULL), 7157 (uchar_t *)&gw_addr, /* gateway address */ 7158 NULL, /* no in-srcaddress */ 7159 &gw_ire->ire_max_frag, 7160 NULL, /* no Fast Path header */ 7161 NULL, /* no recv-from queue */ 7162 NULL, /* no send-to queue */ 7163 (ushort_t)type, /* IRE type */ 7164 NULL, 7165 ipif_arg, 7166 NULL, 7167 0, 7168 0, 7169 0, 7170 flags, 7171 &gw_ire->ire_uinfo, /* Inherit ULP info from gw */ 7172 gc, /* security attribute */ 7173 NULL, 7174 ipst); 7175 7176 /* 7177 * The ire holds a reference to the 'gc' and the 'gc' holds a 7178 * reference to the 'gcgrp'. We can now release the extra reference 7179 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used. 7180 */ 7181 if (gcgrp_xtraref) 7182 GCGRP_REFRELE(gcgrp); 7183 if (ire == NULL) { 7184 if (gc != NULL) 7185 GC_REFRELE(gc); 7186 ire_refrele(gw_ire); 7187 return (ENOMEM); 7188 } 7189 7190 /* 7191 * POLICY: should we allow an RTF_HOST with address INADDR_ANY? 7192 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0? 7193 */ 7194 7195 /* Add the new IRE. */ 7196 error = ire_add(&ire, q, mp, func, B_FALSE); 7197 if (error != 0) { 7198 /* 7199 * In the result of failure, ire_add() will have already 7200 * deleted the ire in question, so there is no need to 7201 * do that here. 7202 */ 7203 ire_refrele(gw_ire); 7204 return (error); 7205 } 7206 7207 if (flags & RTF_MULTIRT) { 7208 /* 7209 * Invoke the CGTP (multirouting) filtering module 7210 * to add the dst address in the filtering database. 7211 * Replicated inbound packets coming from that address 7212 * will be filtered to discard the duplicates. 7213 * It is not necessary to call the CGTP filter hook 7214 * when the dst address is a broadcast or multicast, 7215 * because an IP source address cannot be a broadcast 7216 * or a multicast. 7217 */ 7218 ire_t *ire_dst = ire_ctable_lookup(ire->ire_addr, 0, 7219 IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 7220 if (ire_dst != NULL) { 7221 ip_cgtp_bcast_add(ire, ire_dst, ipst); 7222 ire_refrele(ire_dst); 7223 goto save_ire; 7224 } 7225 if ((ip_cgtp_filter_ops != NULL) && !CLASSD(ire->ire_addr) && 7226 ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) { 7227 int res = ip_cgtp_filter_ops->cfo_add_dest_v4( 7228 ire->ire_addr, 7229 ire->ire_gateway_addr, 7230 ire->ire_src_addr, 7231 gw_ire->ire_src_addr); 7232 if (res != 0) { 7233 ire_refrele(gw_ire); 7234 ire_delete(ire); 7235 return (res); 7236 } 7237 } 7238 } 7239 7240 /* 7241 * Now that the prefix IRE entry has been created, delete any 7242 * existing gateway IRE cache entries as well as any IRE caches 7243 * using the gateway, and force them to be created through 7244 * ip_newroute. 7245 */ 7246 if (gc != NULL) { 7247 ASSERT(gcgrp != NULL); 7248 ire_clookup_delete_cache_gw(gw_addr, ALL_ZONES, ipst); 7249 } 7250 7251 save_ire: 7252 if (gw_ire != NULL) { 7253 ire_refrele(gw_ire); 7254 } 7255 /* 7256 * We do not do save_ire for the routes added with RTA_SRCIFP 7257 * flag. This route is only added and deleted by mipagent. 7258 * So, for simplicity of design, we refrain from saving 7259 * ires that are created with srcif value. This may change 7260 * in future if we find more usage of srcifp feature. 7261 */ 7262 if (ipif != NULL && src_ipif == NULL) { 7263 /* 7264 * Save enough information so that we can recreate the IRE if 7265 * the interface goes down and then up. The metrics associated 7266 * with the route will be saved as well when rts_setmetrics() is 7267 * called after the IRE has been created. In the case where 7268 * memory cannot be allocated, none of this information will be 7269 * saved. 7270 */ 7271 ipif_save_ire(ipif, ire); 7272 } 7273 if (ioctl_msg) 7274 ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst); 7275 if (ire_arg != NULL) { 7276 /* 7277 * Store the ire that was successfully added into where ire_arg 7278 * points to so that callers don't have to look it up 7279 * themselves (but they are responsible for ire_refrele()ing 7280 * the ire when they are finished with it). 7281 */ 7282 *ire_arg = ire; 7283 } else { 7284 ire_refrele(ire); /* Held in ire_add */ 7285 } 7286 if (ipif_refheld) 7287 ipif_refrele(ipif); 7288 return (0); 7289 } 7290 7291 /* 7292 * ip_rt_delete is called to delete an IPv4 route. 7293 * ipif_arg is passed in to associate it with the correct interface. 7294 * src_ipif is passed to associate the incoming interface of the packet. 7295 * We may need to restart this operation if the ipif cannot be looked up 7296 * due to an exclusive operation that is currently in progress. The restart 7297 * entry point is specified by 'func' 7298 */ 7299 /* ARGSUSED4 */ 7300 int 7301 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr, 7302 uint_t rtm_addrs, int flags, ipif_t *ipif_arg, ipif_t *src_ipif, 7303 boolean_t ioctl_msg, queue_t *q, mblk_t *mp, ipsq_func_t func, 7304 ip_stack_t *ipst) 7305 { 7306 ire_t *ire = NULL; 7307 ipif_t *ipif; 7308 boolean_t ipif_refheld = B_FALSE; 7309 uint_t type; 7310 uint_t match_flags = MATCH_IRE_TYPE; 7311 int err = 0; 7312 7313 ip1dbg(("ip_rt_delete:")); 7314 /* 7315 * If this is the case of RTF_HOST being set, then we set the netmask 7316 * to all ones. Otherwise, we use the netmask if one was supplied. 7317 */ 7318 if (flags & RTF_HOST) { 7319 mask = IP_HOST_MASK; 7320 match_flags |= MATCH_IRE_MASK; 7321 } else if (rtm_addrs & RTA_NETMASK) { 7322 match_flags |= MATCH_IRE_MASK; 7323 } 7324 7325 /* 7326 * Note that RTF_GATEWAY is never set on a delete, therefore 7327 * we check if the gateway address is one of our interfaces first, 7328 * and fall back on RTF_GATEWAY routes. 7329 * 7330 * This makes it possible to delete an original 7331 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1. 7332 * 7333 * As the interface index specified with the RTA_IFP sockaddr is the 7334 * same for all ipif's off of an ill, the matching logic below uses 7335 * MATCH_IRE_ILL if such an index was specified. This means a route 7336 * sharing the same prefix and interface index as the the route 7337 * intended to be deleted might be deleted instead if a RTA_IFP sockaddr 7338 * is specified in the request. 7339 * 7340 * On the other hand, since the gateway address will usually be 7341 * different for each ipif on the system, the matching logic 7342 * uses MATCH_IRE_IPIF in the case of a traditional interface 7343 * route. This means that interface routes for the same prefix can be 7344 * uniquely identified if they belong to distinct ipif's and if a 7345 * RTA_IFP sockaddr is not present. 7346 * 7347 * For more detail on specifying routes by gateway address and by 7348 * interface index, see the comments in ip_rt_add(). 7349 * gw_addr could be zero in some cases when both RTA_SRCIFP and 7350 * RTA_IFP are specified. If RTA_SRCIFP is specified and both 7351 * RTA_IFP and gateway_addr are NULL/zero, then delete will not 7352 * succeed. 7353 */ 7354 if (src_ipif != NULL) { 7355 if (ipif_arg == NULL && gw_addr != 0) { 7356 ipif_arg = ipif_lookup_interface(gw_addr, dst_addr, 7357 q, mp, func, &err, ipst); 7358 if (ipif_arg != NULL) 7359 ipif_refheld = B_TRUE; 7360 } 7361 if (ipif_arg == NULL) { 7362 err = (err == EINPROGRESS) ? err : ESRCH; 7363 return (err); 7364 } 7365 ipif = ipif_arg; 7366 } else { 7367 ipif = ipif_lookup_interface(gw_addr, dst_addr, 7368 q, mp, func, &err, ipst); 7369 if (ipif != NULL) 7370 ipif_refheld = B_TRUE; 7371 else if (err == EINPROGRESS) 7372 return (err); 7373 else 7374 err = 0; 7375 } 7376 if (ipif != NULL) { 7377 if (ipif_arg != NULL) { 7378 if (ipif_refheld) { 7379 ipif_refrele(ipif); 7380 ipif_refheld = B_FALSE; 7381 } 7382 ipif = ipif_arg; 7383 match_flags |= MATCH_IRE_ILL; 7384 } else { 7385 match_flags |= MATCH_IRE_IPIF; 7386 } 7387 if (src_ipif != NULL) { 7388 ire = ire_srcif_table_lookup(dst_addr, IRE_INTERFACE, 7389 ipif, src_ipif->ipif_ill, match_flags); 7390 } else { 7391 if (ipif->ipif_ire_type == IRE_LOOPBACK) { 7392 ire = ire_ctable_lookup(dst_addr, 0, 7393 IRE_LOOPBACK, ipif, ALL_ZONES, NULL, 7394 match_flags, ipst); 7395 } 7396 if (ire == NULL) { 7397 ire = ire_ftable_lookup(dst_addr, mask, 0, 7398 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0, 7399 NULL, match_flags, ipst); 7400 } 7401 } 7402 } 7403 7404 if (ire == NULL) { 7405 /* 7406 * At this point, the gateway address is not one of our own 7407 * addresses or a matching interface route was not found. We 7408 * set the IRE type to lookup based on whether 7409 * this is a host route, a default route or just a prefix. 7410 * 7411 * If an ipif_arg was passed in, then the lookup is based on an 7412 * interface index so MATCH_IRE_ILL is added to match_flags. 7413 * In any case, MATCH_IRE_IPIF is cleared and MATCH_IRE_GW is 7414 * set as the route being looked up is not a traditional 7415 * interface route. 7416 * Since we do not add gateway route with srcipif, we don't 7417 * expect to find it either. 7418 */ 7419 if (src_ipif != NULL) { 7420 if (ipif_refheld) 7421 ipif_refrele(ipif); 7422 return (ESRCH); 7423 } else { 7424 match_flags &= ~MATCH_IRE_IPIF; 7425 match_flags |= MATCH_IRE_GW; 7426 if (ipif_arg != NULL) 7427 match_flags |= MATCH_IRE_ILL; 7428 if (mask == IP_HOST_MASK) 7429 type = IRE_HOST; 7430 else if (mask == 0) 7431 type = IRE_DEFAULT; 7432 else 7433 type = IRE_PREFIX; 7434 ire = ire_ftable_lookup(dst_addr, mask, gw_addr, type, 7435 ipif_arg, NULL, ALL_ZONES, 0, NULL, match_flags, 7436 ipst); 7437 } 7438 } 7439 7440 if (ipif_refheld) 7441 ipif_refrele(ipif); 7442 7443 /* ipif is not refheld anymore */ 7444 if (ire == NULL) 7445 return (ESRCH); 7446 7447 if (ire->ire_flags & RTF_MULTIRT) { 7448 /* 7449 * Invoke the CGTP (multirouting) filtering module 7450 * to remove the dst address from the filtering database. 7451 * Packets coming from that address will no longer be 7452 * filtered to remove duplicates. 7453 */ 7454 if (ip_cgtp_filter_ops != NULL && 7455 ipst->ips_netstack->netstack_stackid == GLOBAL_NETSTACKID) { 7456 err = ip_cgtp_filter_ops->cfo_del_dest_v4( 7457 ire->ire_addr, ire->ire_gateway_addr); 7458 } 7459 ip_cgtp_bcast_delete(ire, ipst); 7460 } 7461 7462 ipif = ire->ire_ipif; 7463 /* 7464 * Removing from ipif_saved_ire_mp is not necessary 7465 * when src_ipif being non-NULL. ip_rt_add does not 7466 * save the ires which src_ipif being non-NULL. 7467 */ 7468 if (ipif != NULL && src_ipif == NULL) { 7469 ipif_remove_ire(ipif, ire); 7470 } 7471 if (ioctl_msg) 7472 ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst); 7473 ire_delete(ire); 7474 ire_refrele(ire); 7475 return (err); 7476 } 7477 7478 /* 7479 * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL. 7480 */ 7481 /* ARGSUSED */ 7482 int 7483 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 7484 ip_ioctl_cmd_t *ipip, void *dummy_if_req) 7485 { 7486 ipaddr_t dst_addr; 7487 ipaddr_t gw_addr; 7488 ipaddr_t mask; 7489 int error = 0; 7490 mblk_t *mp1; 7491 struct rtentry *rt; 7492 ipif_t *ipif = NULL; 7493 ip_stack_t *ipst; 7494 7495 ASSERT(q->q_next == NULL); 7496 ipst = CONNQ_TO_IPST(q); 7497 7498 ip1dbg(("ip_siocaddrt:")); 7499 /* Existence of mp1 verified in ip_wput_nondata */ 7500 mp1 = mp->b_cont->b_cont; 7501 rt = (struct rtentry *)mp1->b_rptr; 7502 7503 dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr; 7504 gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr; 7505 7506 /* 7507 * If the RTF_HOST flag is on, this is a request to assign a gateway 7508 * to a particular host address. In this case, we set the netmask to 7509 * all ones for the particular destination address. Otherwise, 7510 * determine the netmask to be used based on dst_addr and the interfaces 7511 * in use. 7512 */ 7513 if (rt->rt_flags & RTF_HOST) { 7514 mask = IP_HOST_MASK; 7515 } else { 7516 /* 7517 * Note that ip_subnet_mask returns a zero mask in the case of 7518 * default (an all-zeroes address). 7519 */ 7520 mask = ip_subnet_mask(dst_addr, &ipif, ipst); 7521 } 7522 7523 error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL, 7524 NULL, B_TRUE, q, mp, ip_process_ioctl, NULL, ipst); 7525 if (ipif != NULL) 7526 ipif_refrele(ipif); 7527 return (error); 7528 } 7529 7530 /* 7531 * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL. 7532 */ 7533 /* ARGSUSED */ 7534 int 7535 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 7536 ip_ioctl_cmd_t *ipip, void *dummy_if_req) 7537 { 7538 ipaddr_t dst_addr; 7539 ipaddr_t gw_addr; 7540 ipaddr_t mask; 7541 int error; 7542 mblk_t *mp1; 7543 struct rtentry *rt; 7544 ipif_t *ipif = NULL; 7545 ip_stack_t *ipst; 7546 7547 ASSERT(q->q_next == NULL); 7548 ipst = CONNQ_TO_IPST(q); 7549 7550 ip1dbg(("ip_siocdelrt:")); 7551 /* Existence of mp1 verified in ip_wput_nondata */ 7552 mp1 = mp->b_cont->b_cont; 7553 rt = (struct rtentry *)mp1->b_rptr; 7554 7555 dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr; 7556 gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr; 7557 7558 /* 7559 * If the RTF_HOST flag is on, this is a request to delete a gateway 7560 * to a particular host address. In this case, we set the netmask to 7561 * all ones for the particular destination address. Otherwise, 7562 * determine the netmask to be used based on dst_addr and the interfaces 7563 * in use. 7564 */ 7565 if (rt->rt_flags & RTF_HOST) { 7566 mask = IP_HOST_MASK; 7567 } else { 7568 /* 7569 * Note that ip_subnet_mask returns a zero mask in the case of 7570 * default (an all-zeroes address). 7571 */ 7572 mask = ip_subnet_mask(dst_addr, &ipif, ipst); 7573 } 7574 7575 error = ip_rt_delete(dst_addr, mask, gw_addr, 7576 RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, NULL, 7577 B_TRUE, q, mp, ip_process_ioctl, ipst); 7578 if (ipif != NULL) 7579 ipif_refrele(ipif); 7580 return (error); 7581 } 7582 7583 /* 7584 * Enqueue the mp onto the ipsq, chained by b_next. 7585 * b_prev stores the function to be executed later, and b_queue the queue 7586 * where this mp originated. 7587 */ 7588 void 7589 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type, 7590 ill_t *pending_ill) 7591 { 7592 conn_t *connp = NULL; 7593 7594 ASSERT(MUTEX_HELD(&ipsq->ipsq_lock)); 7595 ASSERT(func != NULL); 7596 7597 mp->b_queue = q; 7598 mp->b_prev = (void *)func; 7599 mp->b_next = NULL; 7600 7601 switch (type) { 7602 case CUR_OP: 7603 if (ipsq->ipsq_mptail != NULL) { 7604 ASSERT(ipsq->ipsq_mphead != NULL); 7605 ipsq->ipsq_mptail->b_next = mp; 7606 } else { 7607 ASSERT(ipsq->ipsq_mphead == NULL); 7608 ipsq->ipsq_mphead = mp; 7609 } 7610 ipsq->ipsq_mptail = mp; 7611 break; 7612 7613 case NEW_OP: 7614 if (ipsq->ipsq_xopq_mptail != NULL) { 7615 ASSERT(ipsq->ipsq_xopq_mphead != NULL); 7616 ipsq->ipsq_xopq_mptail->b_next = mp; 7617 } else { 7618 ASSERT(ipsq->ipsq_xopq_mphead == NULL); 7619 ipsq->ipsq_xopq_mphead = mp; 7620 } 7621 ipsq->ipsq_xopq_mptail = mp; 7622 break; 7623 default: 7624 cmn_err(CE_PANIC, "ipsq_enq %d type \n", type); 7625 } 7626 7627 if (CONN_Q(q) && pending_ill != NULL) { 7628 connp = Q_TO_CONN(q); 7629 7630 ASSERT(MUTEX_HELD(&connp->conn_lock)); 7631 connp->conn_oper_pending_ill = pending_ill; 7632 } 7633 } 7634 7635 /* 7636 * Return the mp at the head of the ipsq. After emptying the ipsq 7637 * look at the next ioctl, if this ioctl is complete. Otherwise 7638 * return, we will resume when we complete the current ioctl. 7639 * The current ioctl will wait till it gets a response from the 7640 * driver below. 7641 */ 7642 static mblk_t * 7643 ipsq_dq(ipsq_t *ipsq) 7644 { 7645 mblk_t *mp; 7646 7647 ASSERT(MUTEX_HELD(&ipsq->ipsq_lock)); 7648 7649 mp = ipsq->ipsq_mphead; 7650 if (mp != NULL) { 7651 ipsq->ipsq_mphead = mp->b_next; 7652 if (ipsq->ipsq_mphead == NULL) 7653 ipsq->ipsq_mptail = NULL; 7654 mp->b_next = NULL; 7655 return (mp); 7656 } 7657 if (ipsq->ipsq_current_ipif != NULL) 7658 return (NULL); 7659 mp = ipsq->ipsq_xopq_mphead; 7660 if (mp != NULL) { 7661 ipsq->ipsq_xopq_mphead = mp->b_next; 7662 if (ipsq->ipsq_xopq_mphead == NULL) 7663 ipsq->ipsq_xopq_mptail = NULL; 7664 mp->b_next = NULL; 7665 return (mp); 7666 } 7667 return (NULL); 7668 } 7669 7670 /* 7671 * Enter the ipsq corresponding to ill, by waiting synchronously till 7672 * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq 7673 * will have to drain completely before ipsq_enter returns success. 7674 * ipsq_current_ipif will be set if some exclusive ioctl is in progress, 7675 * and the ipsq_exit logic will start the next enqueued ioctl after 7676 * completion of the current ioctl. If 'force' is used, we don't wait 7677 * for the enqueued ioctls. This is needed when a conn_close wants to 7678 * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb 7679 * of an ill can also use this option. But we dont' use it currently. 7680 */ 7681 #define ENTER_SQ_WAIT_TICKS 100 7682 boolean_t 7683 ipsq_enter(ill_t *ill, boolean_t force) 7684 { 7685 ipsq_t *ipsq; 7686 boolean_t waited_enough = B_FALSE; 7687 7688 /* 7689 * Holding the ill_lock prevents <ill-ipsq> assocs from changing. 7690 * Since the <ill-ipsq> assocs could change while we wait for the 7691 * writer, it is easier to wait on a fixed global rather than try to 7692 * cv_wait on a changing ipsq. 7693 */ 7694 mutex_enter(&ill->ill_lock); 7695 for (;;) { 7696 if (ill->ill_state_flags & ILL_CONDEMNED) { 7697 mutex_exit(&ill->ill_lock); 7698 return (B_FALSE); 7699 } 7700 7701 ipsq = ill->ill_phyint->phyint_ipsq; 7702 mutex_enter(&ipsq->ipsq_lock); 7703 if (ipsq->ipsq_writer == NULL && 7704 (ipsq->ipsq_current_ipif == NULL || waited_enough)) { 7705 break; 7706 } else if (ipsq->ipsq_writer != NULL) { 7707 mutex_exit(&ipsq->ipsq_lock); 7708 cv_wait(&ill->ill_cv, &ill->ill_lock); 7709 } else { 7710 mutex_exit(&ipsq->ipsq_lock); 7711 if (force) { 7712 (void) cv_timedwait(&ill->ill_cv, 7713 &ill->ill_lock, 7714 lbolt + ENTER_SQ_WAIT_TICKS); 7715 waited_enough = B_TRUE; 7716 continue; 7717 } else { 7718 cv_wait(&ill->ill_cv, &ill->ill_lock); 7719 } 7720 } 7721 } 7722 7723 ASSERT(ipsq->ipsq_mphead == NULL && ipsq->ipsq_mptail == NULL); 7724 ASSERT(ipsq->ipsq_reentry_cnt == 0); 7725 ipsq->ipsq_writer = curthread; 7726 ipsq->ipsq_reentry_cnt++; 7727 #ifdef ILL_DEBUG 7728 ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH); 7729 #endif 7730 mutex_exit(&ipsq->ipsq_lock); 7731 mutex_exit(&ill->ill_lock); 7732 return (B_TRUE); 7733 } 7734 7735 /* 7736 * The ipsq_t (ipsq) is the synchronization data structure used to serialize 7737 * certain critical operations like plumbing (i.e. most set ioctls), 7738 * multicast joins, igmp/mld timers, IPMP operations etc. On a non-IPMP 7739 * system there is 1 ipsq per phyint. On an IPMP system there is 1 ipsq per 7740 * IPMP group. The ipsq serializes exclusive ioctls issued by applications 7741 * on a per ipsq basis in ipsq_xopq_mphead. It also protects against multiple 7742 * threads executing in the ipsq. Responses from the driver pertain to the 7743 * current ioctl (say a DL_BIND_ACK in response to a DL_BIND_REQUEST initiated 7744 * as part of bringing up the interface) and are enqueued in ipsq_mphead. 7745 * 7746 * If a thread does not want to reenter the ipsq when it is already writer, 7747 * it must make sure that the specified reentry point to be called later 7748 * when the ipsq is empty, nor any code path starting from the specified reentry 7749 * point must never ever try to enter the ipsq again. Otherwise it can lead 7750 * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example. 7751 * When the thread that is currently exclusive finishes, it (ipsq_exit) 7752 * dequeues the requests waiting to become exclusive in ipsq_mphead and calls 7753 * the reentry point. When the list at ipsq_mphead becomes empty ipsq_exit 7754 * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next 7755 * ioctl if the current ioctl has completed. If the current ioctl is still 7756 * in progress it simply returns. The current ioctl could be waiting for 7757 * a response from another module (arp_ or the driver or could be waiting for 7758 * the ipif/ill/ire refcnts to drop to zero. In such a case the ipsq_pending_mp 7759 * and ipsq_pending_ipif are set. ipsq_current_ipif is set throughout the 7760 * execution of the ioctl and ipsq_exit does not start the next ioctl unless 7761 * ipsq_current_ipif is clear which happens only on ioctl completion. 7762 */ 7763 7764 /* 7765 * Try to enter the ipsq exclusively, corresponding to ipif or ill. (only 1 of 7766 * ipif or ill can be specified). The caller ensures ipif or ill is valid by 7767 * ref-holding it if necessary. If the ipsq cannot be entered, the mp is queued 7768 * completion. 7769 */ 7770 ipsq_t * 7771 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp, 7772 ipsq_func_t func, int type, boolean_t reentry_ok) 7773 { 7774 ipsq_t *ipsq; 7775 7776 /* Only 1 of ipif or ill can be specified */ 7777 ASSERT((ipif != NULL) ^ (ill != NULL)); 7778 if (ipif != NULL) 7779 ill = ipif->ipif_ill; 7780 7781 /* 7782 * lock ordering ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock 7783 * ipsq of an ill can't change when ill_lock is held. 7784 */ 7785 GRAB_CONN_LOCK(q); 7786 mutex_enter(&ill->ill_lock); 7787 ipsq = ill->ill_phyint->phyint_ipsq; 7788 mutex_enter(&ipsq->ipsq_lock); 7789 7790 /* 7791 * 1. Enter the ipsq if we are already writer and reentry is ok. 7792 * (Note: If the caller does not specify reentry_ok then neither 7793 * 'func' nor any of its callees must ever attempt to enter the ipsq 7794 * again. Otherwise it can lead to an infinite loop 7795 * 2. Enter the ipsq if there is no current writer and this attempted 7796 * entry is part of the current ioctl or operation 7797 * 3. Enter the ipsq if there is no current writer and this is a new 7798 * ioctl (or operation) and the ioctl (or operation) queue is 7799 * empty and there is no ioctl (or operation) currently in progress 7800 */ 7801 if ((ipsq->ipsq_writer == NULL && ((type == CUR_OP) || 7802 (type == NEW_OP && ipsq->ipsq_xopq_mphead == NULL && 7803 ipsq->ipsq_current_ipif == NULL))) || 7804 (ipsq->ipsq_writer == curthread && reentry_ok)) { 7805 /* Success. */ 7806 ipsq->ipsq_reentry_cnt++; 7807 ipsq->ipsq_writer = curthread; 7808 mutex_exit(&ipsq->ipsq_lock); 7809 mutex_exit(&ill->ill_lock); 7810 RELEASE_CONN_LOCK(q); 7811 #ifdef ILL_DEBUG 7812 ipsq->ipsq_depth = getpcstack(ipsq->ipsq_stack, IP_STACK_DEPTH); 7813 #endif 7814 return (ipsq); 7815 } 7816 7817 ipsq_enq(ipsq, q, mp, func, type, ill); 7818 7819 mutex_exit(&ipsq->ipsq_lock); 7820 mutex_exit(&ill->ill_lock); 7821 RELEASE_CONN_LOCK(q); 7822 return (NULL); 7823 } 7824 7825 /* 7826 * Try to enter the IPSQ corresponding to `ill' as writer. The caller ensures 7827 * ill is valid by refholding it if necessary; we will refrele. If the IPSQ 7828 * cannot be entered, the mp is queued for completion. 7829 */ 7830 void 7831 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type, 7832 boolean_t reentry_ok) 7833 { 7834 ipsq_t *ipsq; 7835 7836 ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok); 7837 7838 /* 7839 * Drop the caller's refhold on the ill. This is safe since we either 7840 * entered the IPSQ (and thus are exclusive), or failed to enter the 7841 * IPSQ, in which case we return without accessing ill anymore. This 7842 * is needed because func needs to see the correct refcount. 7843 * e.g. removeif can work only then. 7844 */ 7845 ill_refrele(ill); 7846 if (ipsq != NULL) { 7847 (*func)(ipsq, q, mp, NULL); 7848 ipsq_exit(ipsq, B_TRUE, B_TRUE); 7849 } 7850 } 7851 7852 /* 7853 * If there are more than ILL_GRP_CNT ills in a group, 7854 * we use kmem alloc'd buffers, else use the stack 7855 */ 7856 #define ILL_GRP_CNT 14 7857 /* 7858 * Drain the ipsq, if there are messages on it, and then leave the ipsq. 7859 * Called by a thread that is currently exclusive on this ipsq. 7860 */ 7861 void 7862 ipsq_exit(ipsq_t *ipsq, boolean_t start_igmp_timer, boolean_t start_mld_timer) 7863 { 7864 queue_t *q; 7865 mblk_t *mp; 7866 ipsq_func_t func; 7867 int next; 7868 ill_t **ill_list = NULL; 7869 size_t ill_list_size = 0; 7870 int cnt = 0; 7871 boolean_t need_ipsq_free = B_FALSE; 7872 ip_stack_t *ipst = ipsq->ipsq_ipst; 7873 7874 ASSERT(IAM_WRITER_IPSQ(ipsq)); 7875 mutex_enter(&ipsq->ipsq_lock); 7876 ASSERT(ipsq->ipsq_reentry_cnt >= 1); 7877 if (ipsq->ipsq_reentry_cnt != 1) { 7878 ipsq->ipsq_reentry_cnt--; 7879 mutex_exit(&ipsq->ipsq_lock); 7880 return; 7881 } 7882 7883 mp = ipsq_dq(ipsq); 7884 while (mp != NULL) { 7885 again: 7886 mutex_exit(&ipsq->ipsq_lock); 7887 func = (ipsq_func_t)mp->b_prev; 7888 q = (queue_t *)mp->b_queue; 7889 mp->b_prev = NULL; 7890 mp->b_queue = NULL; 7891 7892 /* 7893 * If 'q' is an conn queue, it is valid, since we did a 7894 * a refhold on the connp, at the start of the ioctl. 7895 * If 'q' is an ill queue, it is valid, since close of an 7896 * ill will clean up the 'ipsq'. 7897 */ 7898 (*func)(ipsq, q, mp, NULL); 7899 7900 mutex_enter(&ipsq->ipsq_lock); 7901 mp = ipsq_dq(ipsq); 7902 } 7903 7904 mutex_exit(&ipsq->ipsq_lock); 7905 7906 /* 7907 * Need to grab the locks in the right order. Need to 7908 * atomically check (under ipsq_lock) that there are no 7909 * messages before relinquishing the ipsq. Also need to 7910 * atomically wakeup waiters on ill_cv while holding ill_lock. 7911 * Holding ill_g_lock ensures that ipsq list of ills is stable. 7912 * If we need to call ill_split_ipsq and change <ill-ipsq> we need 7913 * to grab ill_g_lock as writer. 7914 */ 7915 rw_enter(&ipst->ips_ill_g_lock, 7916 ipsq->ipsq_split ? RW_WRITER : RW_READER); 7917 7918 /* ipsq_refs can't change while ill_g_lock is held as reader */ 7919 if (ipsq->ipsq_refs != 0) { 7920 /* At most 2 ills v4/v6 per phyint */ 7921 cnt = ipsq->ipsq_refs << 1; 7922 ill_list_size = cnt * sizeof (ill_t *); 7923 /* 7924 * If memory allocation fails, we will do the split 7925 * the next time ipsq_exit is called for whatever reason. 7926 * As long as the ipsq_split flag is set the need to 7927 * split is remembered. 7928 */ 7929 ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP); 7930 if (ill_list != NULL) 7931 cnt = ill_lock_ipsq_ills(ipsq, ill_list, cnt); 7932 } 7933 mutex_enter(&ipsq->ipsq_lock); 7934 mp = ipsq_dq(ipsq); 7935 if (mp != NULL) { 7936 /* oops, some message has landed up, we can't get out */ 7937 if (ill_list != NULL) 7938 ill_unlock_ills(ill_list, cnt); 7939 rw_exit(&ipst->ips_ill_g_lock); 7940 if (ill_list != NULL) 7941 kmem_free(ill_list, ill_list_size); 7942 ill_list = NULL; 7943 ill_list_size = 0; 7944 cnt = 0; 7945 goto again; 7946 } 7947 7948 /* 7949 * Split only if no ioctl is pending and if memory alloc succeeded 7950 * above. 7951 */ 7952 if (ipsq->ipsq_split && ipsq->ipsq_current_ipif == NULL && 7953 ill_list != NULL) { 7954 /* 7955 * No new ill can join this ipsq since we are holding the 7956 * ill_g_lock. Hence ill_split_ipsq can safely traverse the 7957 * ipsq. ill_split_ipsq may fail due to memory shortage. 7958 * If so we will retry on the next ipsq_exit. 7959 */ 7960 ipsq->ipsq_split = ill_split_ipsq(ipsq); 7961 } 7962 7963 /* 7964 * We are holding the ipsq lock, hence no new messages can 7965 * land up on the ipsq, and there are no messages currently. 7966 * Now safe to get out. Wake up waiters and relinquish ipsq 7967 * atomically while holding ill locks. 7968 */ 7969 ipsq->ipsq_writer = NULL; 7970 ipsq->ipsq_reentry_cnt--; 7971 ASSERT(ipsq->ipsq_reentry_cnt == 0); 7972 #ifdef ILL_DEBUG 7973 ipsq->ipsq_depth = 0; 7974 #endif 7975 mutex_exit(&ipsq->ipsq_lock); 7976 /* 7977 * For IPMP this should wake up all ills in this ipsq. 7978 * We need to hold the ill_lock while waking up waiters to 7979 * avoid missed wakeups. But there is no need to acquire all 7980 * the ill locks and then wakeup. If we have not acquired all 7981 * the locks (due to memory failure above) ill_signal_ipsq_ills 7982 * wakes up ills one at a time after getting the right ill_lock 7983 */ 7984 ill_signal_ipsq_ills(ipsq, ill_list != NULL); 7985 if (ill_list != NULL) 7986 ill_unlock_ills(ill_list, cnt); 7987 if (ipsq->ipsq_refs == 0) 7988 need_ipsq_free = B_TRUE; 7989 rw_exit(&ipst->ips_ill_g_lock); 7990 if (ill_list != 0) 7991 kmem_free(ill_list, ill_list_size); 7992 7993 if (need_ipsq_free) { 7994 /* 7995 * Free the ipsq. ipsq_refs can't increase because ipsq can't be 7996 * looked up. ipsq can be looked up only thru ill or phyint 7997 * and there are no ills/phyint on this ipsq. 7998 */ 7999 ipsq_delete(ipsq); 8000 } 8001 /* 8002 * Now start any igmp or mld timers that could not be started 8003 * while inside the ipsq. The timers can't be started while inside 8004 * the ipsq, since igmp_start_timers may need to call untimeout() 8005 * which can't be done while holding a lock i.e. the ipsq. Otherwise 8006 * there could be a deadlock since the timeout handlers 8007 * mld_timeout_handler / igmp_timeout_handler also synchronously 8008 * wait in ipsq_enter() trying to get the ipsq. 8009 * 8010 * However there is one exception to the above. If this thread is 8011 * itself the igmp/mld timeout handler thread, then we don't want 8012 * to start any new timer until the current handler is done. The 8013 * handler thread passes in B_FALSE for start_igmp/mld_timers, while 8014 * all others pass B_TRUE. 8015 */ 8016 if (start_igmp_timer) { 8017 mutex_enter(&ipst->ips_igmp_timer_lock); 8018 next = ipst->ips_igmp_deferred_next; 8019 ipst->ips_igmp_deferred_next = INFINITY; 8020 mutex_exit(&ipst->ips_igmp_timer_lock); 8021 8022 if (next != INFINITY) 8023 igmp_start_timers(next, ipst); 8024 } 8025 8026 if (start_mld_timer) { 8027 mutex_enter(&ipst->ips_mld_timer_lock); 8028 next = ipst->ips_mld_deferred_next; 8029 ipst->ips_mld_deferred_next = INFINITY; 8030 mutex_exit(&ipst->ips_mld_timer_lock); 8031 8032 if (next != INFINITY) 8033 mld_start_timers(next, ipst); 8034 } 8035 } 8036 8037 /* 8038 * Start the current exclusive operation on `ipsq'; associate it with `ipif' 8039 * and `ioccmd'. 8040 */ 8041 void 8042 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd) 8043 { 8044 ASSERT(IAM_WRITER_IPSQ(ipsq)); 8045 8046 mutex_enter(&ipsq->ipsq_lock); 8047 ASSERT(ipsq->ipsq_current_ipif == NULL); 8048 ASSERT(ipsq->ipsq_current_ioctl == 0); 8049 ipsq->ipsq_current_ipif = ipif; 8050 ipsq->ipsq_current_ioctl = ioccmd; 8051 mutex_exit(&ipsq->ipsq_lock); 8052 } 8053 8054 /* 8055 * Finish the current exclusive operation on `ipsq'. Note that other 8056 * operations will not be able to proceed until an ipsq_exit() is done. 8057 */ 8058 void 8059 ipsq_current_finish(ipsq_t *ipsq) 8060 { 8061 ipif_t *ipif = ipsq->ipsq_current_ipif; 8062 8063 ASSERT(IAM_WRITER_IPSQ(ipsq)); 8064 8065 /* 8066 * For SIOCSLIFREMOVEIF, the ipif has been already been blown away 8067 * (but we're careful to never set IPIF_CHANGING in that case). 8068 */ 8069 if (ipsq->ipsq_current_ioctl != SIOCLIFREMOVEIF) { 8070 mutex_enter(&ipif->ipif_ill->ill_lock); 8071 ipif->ipif_state_flags &= ~IPIF_CHANGING; 8072 8073 /* Send any queued event */ 8074 ill_nic_info_dispatch(ipif->ipif_ill); 8075 mutex_exit(&ipif->ipif_ill->ill_lock); 8076 } 8077 8078 mutex_enter(&ipsq->ipsq_lock); 8079 ASSERT(ipsq->ipsq_current_ipif != NULL); 8080 ipsq->ipsq_current_ipif = NULL; 8081 ipsq->ipsq_current_ioctl = 0; 8082 mutex_exit(&ipsq->ipsq_lock); 8083 } 8084 8085 /* 8086 * The ill is closing. Flush all messages on the ipsq that originated 8087 * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead 8088 * for this ill since ipsq_enter could not have entered until then. 8089 * New messages can't be queued since the CONDEMNED flag is set. 8090 */ 8091 static void 8092 ipsq_flush(ill_t *ill) 8093 { 8094 queue_t *q; 8095 mblk_t *prev; 8096 mblk_t *mp; 8097 mblk_t *mp_next; 8098 ipsq_t *ipsq; 8099 8100 ASSERT(IAM_WRITER_ILL(ill)); 8101 ipsq = ill->ill_phyint->phyint_ipsq; 8102 /* 8103 * Flush any messages sent up by the driver. 8104 */ 8105 mutex_enter(&ipsq->ipsq_lock); 8106 for (prev = NULL, mp = ipsq->ipsq_mphead; mp != NULL; mp = mp_next) { 8107 mp_next = mp->b_next; 8108 q = mp->b_queue; 8109 if (q == ill->ill_rq || q == ill->ill_wq) { 8110 /* Remove the mp from the ipsq */ 8111 if (prev == NULL) 8112 ipsq->ipsq_mphead = mp->b_next; 8113 else 8114 prev->b_next = mp->b_next; 8115 if (ipsq->ipsq_mptail == mp) { 8116 ASSERT(mp_next == NULL); 8117 ipsq->ipsq_mptail = prev; 8118 } 8119 inet_freemsg(mp); 8120 } else { 8121 prev = mp; 8122 } 8123 } 8124 mutex_exit(&ipsq->ipsq_lock); 8125 (void) ipsq_pending_mp_cleanup(ill, NULL); 8126 ipsq_xopq_mp_cleanup(ill, NULL); 8127 ill_pending_mp_cleanup(ill); 8128 } 8129 8130 /* ARGSUSED */ 8131 int 8132 ip_sioctl_slifoindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 8133 ip_ioctl_cmd_t *ipip, void *ifreq) 8134 { 8135 ill_t *ill; 8136 struct lifreq *lifr = (struct lifreq *)ifreq; 8137 boolean_t isv6; 8138 conn_t *connp; 8139 ip_stack_t *ipst; 8140 8141 connp = Q_TO_CONN(q); 8142 ipst = connp->conn_netstack->netstack_ip; 8143 isv6 = connp->conn_af_isv6; 8144 /* 8145 * Set original index. 8146 * Failover and failback move logical interfaces 8147 * from one physical interface to another. The 8148 * original index indicates the parent of a logical 8149 * interface, in other words, the physical interface 8150 * the logical interface will be moved back to on 8151 * failback. 8152 */ 8153 8154 /* 8155 * Don't allow the original index to be changed 8156 * for non-failover addresses, autoconfigured 8157 * addresses, or IPv6 link local addresses. 8158 */ 8159 if (((ipif->ipif_flags & (IPIF_NOFAILOVER | IPIF_ADDRCONF)) != NULL) || 8160 (isv6 && IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr))) { 8161 return (EINVAL); 8162 } 8163 /* 8164 * The new original index must be in use by some 8165 * physical interface. 8166 */ 8167 ill = ill_lookup_on_ifindex(lifr->lifr_index, isv6, NULL, NULL, 8168 NULL, NULL, ipst); 8169 if (ill == NULL) 8170 return (ENXIO); 8171 ill_refrele(ill); 8172 8173 ipif->ipif_orig_ifindex = lifr->lifr_index; 8174 /* 8175 * When this ipif gets failed back, don't 8176 * preserve the original id, as it is no 8177 * longer applicable. 8178 */ 8179 ipif->ipif_orig_ipifid = 0; 8180 /* 8181 * For IPv4, change the original index of any 8182 * multicast addresses associated with the 8183 * ipif to the new value. 8184 */ 8185 if (!isv6) { 8186 ilm_t *ilm; 8187 8188 mutex_enter(&ipif->ipif_ill->ill_lock); 8189 for (ilm = ipif->ipif_ill->ill_ilm; ilm != NULL; 8190 ilm = ilm->ilm_next) { 8191 if (ilm->ilm_ipif == ipif) { 8192 ilm->ilm_orig_ifindex = lifr->lifr_index; 8193 } 8194 } 8195 mutex_exit(&ipif->ipif_ill->ill_lock); 8196 } 8197 return (0); 8198 } 8199 8200 /* ARGSUSED */ 8201 int 8202 ip_sioctl_get_oindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 8203 ip_ioctl_cmd_t *ipip, void *ifreq) 8204 { 8205 struct lifreq *lifr = (struct lifreq *)ifreq; 8206 8207 /* 8208 * Get the original interface index i.e the one 8209 * before FAILOVER if it ever happened. 8210 */ 8211 lifr->lifr_index = ipif->ipif_orig_ifindex; 8212 return (0); 8213 } 8214 8215 /* 8216 * Parse an iftun_req structure coming down SIOC[GS]TUNPARAM ioctls, 8217 * refhold and return the associated ipif 8218 */ 8219 int 8220 ip_extract_tunreq(queue_t *q, mblk_t *mp, ipif_t **ipifp, ipsq_func_t func) 8221 { 8222 boolean_t exists; 8223 struct iftun_req *ta; 8224 ipif_t *ipif; 8225 ill_t *ill; 8226 boolean_t isv6; 8227 mblk_t *mp1; 8228 int error; 8229 conn_t *connp; 8230 ip_stack_t *ipst; 8231 8232 /* Existence verified in ip_wput_nondata */ 8233 mp1 = mp->b_cont->b_cont; 8234 ta = (struct iftun_req *)mp1->b_rptr; 8235 /* 8236 * Null terminate the string to protect against buffer 8237 * overrun. String was generated by user code and may not 8238 * be trusted. 8239 */ 8240 ta->ifta_lifr_name[LIFNAMSIZ - 1] = '\0'; 8241 8242 connp = Q_TO_CONN(q); 8243 isv6 = connp->conn_af_isv6; 8244 ipst = connp->conn_netstack->netstack_ip; 8245 8246 /* Disallows implicit create */ 8247 ipif = ipif_lookup_on_name(ta->ifta_lifr_name, 8248 mi_strlen(ta->ifta_lifr_name), B_FALSE, &exists, isv6, 8249 connp->conn_zoneid, CONNP_TO_WQ(connp), mp, func, &error, ipst); 8250 if (ipif == NULL) 8251 return (error); 8252 8253 if (ipif->ipif_id != 0) { 8254 /* 8255 * We really don't want to set/get tunnel parameters 8256 * on virtual tunnel interfaces. Only allow the 8257 * base tunnel to do these. 8258 */ 8259 ipif_refrele(ipif); 8260 return (EINVAL); 8261 } 8262 8263 /* 8264 * Send down to tunnel mod for ioctl processing. 8265 * Will finish ioctl in ip_rput_other(). 8266 */ 8267 ill = ipif->ipif_ill; 8268 if (ill->ill_net_type == IRE_LOOPBACK) { 8269 ipif_refrele(ipif); 8270 return (EOPNOTSUPP); 8271 } 8272 8273 if (ill->ill_wq == NULL) { 8274 ipif_refrele(ipif); 8275 return (ENXIO); 8276 } 8277 /* 8278 * Mark the ioctl as coming from an IPv6 interface for 8279 * tun's convenience. 8280 */ 8281 if (ill->ill_isv6) 8282 ta->ifta_flags |= 0x80000000; 8283 *ipifp = ipif; 8284 return (0); 8285 } 8286 8287 /* 8288 * Parse an ifreq or lifreq struct coming down ioctls and refhold 8289 * and return the associated ipif. 8290 * Return value: 8291 * Non zero: An error has occurred. ci may not be filled out. 8292 * zero : ci is filled out with the ioctl cmd in ci.ci_name, and 8293 * a held ipif in ci.ci_ipif. 8294 */ 8295 int 8296 ip_extract_lifreq_cmn(queue_t *q, mblk_t *mp, int cmd_type, int flags, 8297 cmd_info_t *ci, ipsq_func_t func) 8298 { 8299 sin_t *sin; 8300 sin6_t *sin6; 8301 char *name; 8302 struct ifreq *ifr; 8303 struct lifreq *lifr; 8304 ipif_t *ipif = NULL; 8305 ill_t *ill; 8306 conn_t *connp; 8307 boolean_t isv6; 8308 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 8309 boolean_t exists; 8310 int err; 8311 mblk_t *mp1; 8312 zoneid_t zoneid; 8313 ip_stack_t *ipst; 8314 8315 if (q->q_next != NULL) { 8316 ill = (ill_t *)q->q_ptr; 8317 isv6 = ill->ill_isv6; 8318 connp = NULL; 8319 zoneid = ALL_ZONES; 8320 ipst = ill->ill_ipst; 8321 } else { 8322 ill = NULL; 8323 connp = Q_TO_CONN(q); 8324 isv6 = connp->conn_af_isv6; 8325 zoneid = connp->conn_zoneid; 8326 if (zoneid == GLOBAL_ZONEID) { 8327 /* global zone can access ipifs in all zones */ 8328 zoneid = ALL_ZONES; 8329 } 8330 ipst = connp->conn_netstack->netstack_ip; 8331 } 8332 8333 /* Has been checked in ip_wput_nondata */ 8334 mp1 = mp->b_cont->b_cont; 8335 8336 8337 if (cmd_type == IF_CMD) { 8338 /* This a old style SIOC[GS]IF* command */ 8339 ifr = (struct ifreq *)mp1->b_rptr; 8340 /* 8341 * Null terminate the string to protect against buffer 8342 * overrun. String was generated by user code and may not 8343 * be trusted. 8344 */ 8345 ifr->ifr_name[IFNAMSIZ - 1] = '\0'; 8346 sin = (sin_t *)&ifr->ifr_addr; 8347 name = ifr->ifr_name; 8348 ci->ci_sin = sin; 8349 ci->ci_sin6 = NULL; 8350 ci->ci_lifr = (struct lifreq *)ifr; 8351 } else { 8352 /* This a new style SIOC[GS]LIF* command */ 8353 ASSERT(cmd_type == LIF_CMD); 8354 lifr = (struct lifreq *)mp1->b_rptr; 8355 /* 8356 * Null terminate the string to protect against buffer 8357 * overrun. String was generated by user code and may not 8358 * be trusted. 8359 */ 8360 lifr->lifr_name[LIFNAMSIZ - 1] = '\0'; 8361 name = lifr->lifr_name; 8362 sin = (sin_t *)&lifr->lifr_addr; 8363 sin6 = (sin6_t *)&lifr->lifr_addr; 8364 if (iocp->ioc_cmd == SIOCSLIFGROUPNAME) { 8365 (void) strncpy(ci->ci_groupname, lifr->lifr_groupname, 8366 LIFNAMSIZ); 8367 } 8368 ci->ci_sin = sin; 8369 ci->ci_sin6 = sin6; 8370 ci->ci_lifr = lifr; 8371 } 8372 8373 if (iocp->ioc_cmd == SIOCSLIFNAME) { 8374 /* 8375 * The ioctl will be failed if the ioctl comes down 8376 * an conn stream 8377 */ 8378 if (ill == NULL) { 8379 /* 8380 * Not an ill queue, return EINVAL same as the 8381 * old error code. 8382 */ 8383 return (ENXIO); 8384 } 8385 ipif = ill->ill_ipif; 8386 ipif_refhold(ipif); 8387 } else { 8388 ipif = ipif_lookup_on_name(name, mi_strlen(name), B_FALSE, 8389 &exists, isv6, zoneid, 8390 (connp == NULL) ? q : CONNP_TO_WQ(connp), mp, func, &err, 8391 ipst); 8392 if (ipif == NULL) { 8393 if (err == EINPROGRESS) 8394 return (err); 8395 if (iocp->ioc_cmd == SIOCLIFFAILOVER || 8396 iocp->ioc_cmd == SIOCLIFFAILBACK) { 8397 /* 8398 * Need to try both v4 and v6 since this 8399 * ioctl can come down either v4 or v6 8400 * socket. The lifreq.lifr_family passed 8401 * down by this ioctl is AF_UNSPEC. 8402 */ 8403 ipif = ipif_lookup_on_name(name, 8404 mi_strlen(name), B_FALSE, &exists, !isv6, 8405 zoneid, (connp == NULL) ? q : 8406 CONNP_TO_WQ(connp), mp, func, &err, ipst); 8407 if (err == EINPROGRESS) 8408 return (err); 8409 } 8410 err = 0; /* Ensure we don't use it below */ 8411 } 8412 } 8413 8414 /* 8415 * Old style [GS]IFCMD does not admit IPv6 ipif 8416 */ 8417 if (ipif != NULL && ipif->ipif_isv6 && cmd_type == IF_CMD) { 8418 ipif_refrele(ipif); 8419 return (ENXIO); 8420 } 8421 8422 if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL && 8423 name[0] == '\0') { 8424 /* 8425 * Handle a or a SIOC?IF* with a null name 8426 * during plumb (on the ill queue before the I_PLINK). 8427 */ 8428 ipif = ill->ill_ipif; 8429 ipif_refhold(ipif); 8430 } 8431 8432 if (ipif == NULL) 8433 return (ENXIO); 8434 8435 /* 8436 * Allow only GET operations if this ipif has been created 8437 * temporarily due to a MOVE operation. 8438 */ 8439 if (ipif->ipif_replace_zero && !(flags & IPI_REPL)) { 8440 ipif_refrele(ipif); 8441 return (EINVAL); 8442 } 8443 8444 ci->ci_ipif = ipif; 8445 return (0); 8446 } 8447 8448 /* 8449 * Return the total number of ipifs. 8450 */ 8451 static uint_t 8452 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst) 8453 { 8454 uint_t numifs = 0; 8455 ill_t *ill; 8456 ill_walk_context_t ctx; 8457 ipif_t *ipif; 8458 8459 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 8460 ill = ILL_START_WALK_V4(&ctx, ipst); 8461 8462 while (ill != NULL) { 8463 for (ipif = ill->ill_ipif; ipif != NULL; 8464 ipif = ipif->ipif_next) { 8465 if (ipif->ipif_zoneid == zoneid || 8466 ipif->ipif_zoneid == ALL_ZONES) 8467 numifs++; 8468 } 8469 ill = ill_next(&ctx, ill); 8470 } 8471 rw_exit(&ipst->ips_ill_g_lock); 8472 return (numifs); 8473 } 8474 8475 /* 8476 * Return the total number of ipifs. 8477 */ 8478 static uint_t 8479 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst) 8480 { 8481 uint_t numifs = 0; 8482 ill_t *ill; 8483 ipif_t *ipif; 8484 ill_walk_context_t ctx; 8485 8486 ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid)); 8487 8488 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 8489 if (family == AF_INET) 8490 ill = ILL_START_WALK_V4(&ctx, ipst); 8491 else if (family == AF_INET6) 8492 ill = ILL_START_WALK_V6(&ctx, ipst); 8493 else 8494 ill = ILL_START_WALK_ALL(&ctx, ipst); 8495 8496 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 8497 for (ipif = ill->ill_ipif; ipif != NULL; 8498 ipif = ipif->ipif_next) { 8499 if ((ipif->ipif_flags & IPIF_NOXMIT) && 8500 !(lifn_flags & LIFC_NOXMIT)) 8501 continue; 8502 if ((ipif->ipif_flags & IPIF_TEMPORARY) && 8503 !(lifn_flags & LIFC_TEMPORARY)) 8504 continue; 8505 if (((ipif->ipif_flags & 8506 (IPIF_NOXMIT|IPIF_NOLOCAL| 8507 IPIF_DEPRECATED)) || 8508 (ill->ill_phyint->phyint_flags & 8509 PHYI_LOOPBACK) || 8510 !(ipif->ipif_flags & IPIF_UP)) && 8511 (lifn_flags & LIFC_EXTERNAL_SOURCE)) 8512 continue; 8513 8514 if (zoneid != ipif->ipif_zoneid && 8515 ipif->ipif_zoneid != ALL_ZONES && 8516 (zoneid != GLOBAL_ZONEID || 8517 !(lifn_flags & LIFC_ALLZONES))) 8518 continue; 8519 8520 numifs++; 8521 } 8522 } 8523 rw_exit(&ipst->ips_ill_g_lock); 8524 return (numifs); 8525 } 8526 8527 uint_t 8528 ip_get_lifsrcofnum(ill_t *ill) 8529 { 8530 uint_t numifs = 0; 8531 ill_t *ill_head = ill; 8532 ip_stack_t *ipst = ill->ill_ipst; 8533 8534 /* 8535 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some 8536 * other thread may be trying to relink the ILLs in this usesrc group 8537 * and adjusting the ill_usesrc_grp_next pointers 8538 */ 8539 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER); 8540 if ((ill->ill_usesrc_ifindex == 0) && 8541 (ill->ill_usesrc_grp_next != NULL)) { 8542 for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head); 8543 ill = ill->ill_usesrc_grp_next) 8544 numifs++; 8545 } 8546 rw_exit(&ipst->ips_ill_g_usesrc_lock); 8547 8548 return (numifs); 8549 } 8550 8551 /* Null values are passed in for ipif, sin, and ifreq */ 8552 /* ARGSUSED */ 8553 int 8554 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, 8555 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq) 8556 { 8557 int *nump; 8558 conn_t *connp = Q_TO_CONN(q); 8559 8560 ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */ 8561 8562 /* Existence of b_cont->b_cont checked in ip_wput_nondata */ 8563 nump = (int *)mp->b_cont->b_cont->b_rptr; 8564 8565 *nump = ip_get_numifs(connp->conn_zoneid, 8566 connp->conn_netstack->netstack_ip); 8567 ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump)); 8568 return (0); 8569 } 8570 8571 /* Null values are passed in for ipif, sin, and ifreq */ 8572 /* ARGSUSED */ 8573 int 8574 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, 8575 queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq) 8576 { 8577 struct lifnum *lifn; 8578 mblk_t *mp1; 8579 conn_t *connp = Q_TO_CONN(q); 8580 8581 ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */ 8582 8583 /* Existence checked in ip_wput_nondata */ 8584 mp1 = mp->b_cont->b_cont; 8585 8586 lifn = (struct lifnum *)mp1->b_rptr; 8587 switch (lifn->lifn_family) { 8588 case AF_UNSPEC: 8589 case AF_INET: 8590 case AF_INET6: 8591 break; 8592 default: 8593 return (EAFNOSUPPORT); 8594 } 8595 8596 lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags, 8597 connp->conn_zoneid, connp->conn_netstack->netstack_ip); 8598 ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count)); 8599 return (0); 8600 } 8601 8602 /* ARGSUSED */ 8603 int 8604 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, 8605 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq) 8606 { 8607 STRUCT_HANDLE(ifconf, ifc); 8608 mblk_t *mp1; 8609 struct iocblk *iocp; 8610 struct ifreq *ifr; 8611 ill_walk_context_t ctx; 8612 ill_t *ill; 8613 ipif_t *ipif; 8614 struct sockaddr_in *sin; 8615 int32_t ifclen; 8616 zoneid_t zoneid; 8617 ip_stack_t *ipst = CONNQ_TO_IPST(q); 8618 8619 ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */ 8620 8621 ip1dbg(("ip_sioctl_get_ifconf")); 8622 /* Existence verified in ip_wput_nondata */ 8623 mp1 = mp->b_cont->b_cont; 8624 iocp = (struct iocblk *)mp->b_rptr; 8625 zoneid = Q_TO_CONN(q)->conn_zoneid; 8626 8627 /* 8628 * The original SIOCGIFCONF passed in a struct ifconf which specified 8629 * the user buffer address and length into which the list of struct 8630 * ifreqs was to be copied. Since AT&T Streams does not seem to 8631 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS, 8632 * the SIOCGIFCONF operation was redefined to simply provide 8633 * a large output buffer into which we are supposed to jam the ifreq 8634 * array. The same ioctl command code was used, despite the fact that 8635 * both the applications and the kernel code had to change, thus making 8636 * it impossible to support both interfaces. 8637 * 8638 * For reasons not good enough to try to explain, the following 8639 * algorithm is used for deciding what to do with one of these: 8640 * If the IOCTL comes in as an I_STR, it is assumed to be of the new 8641 * form with the output buffer coming down as the continuation message. 8642 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style, 8643 * and we have to copy in the ifconf structure to find out how big the 8644 * output buffer is and where to copy out to. Sure no problem... 8645 * 8646 */ 8647 STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL); 8648 if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) { 8649 int numifs = 0; 8650 size_t ifc_bufsize; 8651 8652 /* 8653 * Must be (better be!) continuation of a TRANSPARENT 8654 * IOCTL. We just copied in the ifconf structure. 8655 */ 8656 STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, 8657 (struct ifconf *)mp1->b_rptr); 8658 8659 /* 8660 * Allocate a buffer to hold requested information. 8661 * 8662 * If ifc_len is larger than what is needed, we only 8663 * allocate what we will use. 8664 * 8665 * If ifc_len is smaller than what is needed, return 8666 * EINVAL. 8667 * 8668 * XXX: the ill_t structure can hava 2 counters, for 8669 * v4 and v6 (not just ill_ipif_up_count) to store the 8670 * number of interfaces for a device, so we don't need 8671 * to count them here... 8672 */ 8673 numifs = ip_get_numifs(zoneid, ipst); 8674 8675 ifclen = STRUCT_FGET(ifc, ifc_len); 8676 ifc_bufsize = numifs * sizeof (struct ifreq); 8677 if (ifc_bufsize > ifclen) { 8678 if (iocp->ioc_cmd == O_SIOCGIFCONF) { 8679 /* old behaviour */ 8680 return (EINVAL); 8681 } else { 8682 ifc_bufsize = ifclen; 8683 } 8684 } 8685 8686 mp1 = mi_copyout_alloc(q, mp, 8687 STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE); 8688 if (mp1 == NULL) 8689 return (ENOMEM); 8690 8691 mp1->b_wptr = mp1->b_rptr + ifc_bufsize; 8692 } 8693 bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr); 8694 /* 8695 * the SIOCGIFCONF ioctl only knows about 8696 * IPv4 addresses, so don't try to tell 8697 * it about interfaces with IPv6-only 8698 * addresses. (Last parm 'isv6' is B_FALSE) 8699 */ 8700 8701 ifr = (struct ifreq *)mp1->b_rptr; 8702 8703 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 8704 ill = ILL_START_WALK_V4(&ctx, ipst); 8705 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 8706 for (ipif = ill->ill_ipif; ipif != NULL; 8707 ipif = ipif->ipif_next) { 8708 if (zoneid != ipif->ipif_zoneid && 8709 ipif->ipif_zoneid != ALL_ZONES) 8710 continue; 8711 if ((uchar_t *)&ifr[1] > mp1->b_wptr) { 8712 if (iocp->ioc_cmd == O_SIOCGIFCONF) { 8713 /* old behaviour */ 8714 rw_exit(&ipst->ips_ill_g_lock); 8715 return (EINVAL); 8716 } else { 8717 goto if_copydone; 8718 } 8719 } 8720 (void) ipif_get_name(ipif, 8721 ifr->ifr_name, 8722 sizeof (ifr->ifr_name)); 8723 sin = (sin_t *)&ifr->ifr_addr; 8724 *sin = sin_null; 8725 sin->sin_family = AF_INET; 8726 sin->sin_addr.s_addr = ipif->ipif_lcl_addr; 8727 ifr++; 8728 } 8729 } 8730 if_copydone: 8731 rw_exit(&ipst->ips_ill_g_lock); 8732 mp1->b_wptr = (uchar_t *)ifr; 8733 8734 if (STRUCT_BUF(ifc) != NULL) { 8735 STRUCT_FSET(ifc, ifc_len, 8736 (int)((uchar_t *)ifr - mp1->b_rptr)); 8737 } 8738 return (0); 8739 } 8740 8741 /* 8742 * Get the interfaces using the address hosted on the interface passed in, 8743 * as a source adddress 8744 */ 8745 /* ARGSUSED */ 8746 int 8747 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, 8748 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq) 8749 { 8750 mblk_t *mp1; 8751 ill_t *ill, *ill_head; 8752 ipif_t *ipif, *orig_ipif; 8753 int numlifs = 0; 8754 size_t lifs_bufsize, lifsmaxlen; 8755 struct lifreq *lifr; 8756 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 8757 uint_t ifindex; 8758 zoneid_t zoneid; 8759 int err = 0; 8760 boolean_t isv6 = B_FALSE; 8761 struct sockaddr_in *sin; 8762 struct sockaddr_in6 *sin6; 8763 STRUCT_HANDLE(lifsrcof, lifs); 8764 ip_stack_t *ipst; 8765 8766 ipst = CONNQ_TO_IPST(q); 8767 8768 ASSERT(q->q_next == NULL); 8769 8770 zoneid = Q_TO_CONN(q)->conn_zoneid; 8771 8772 /* Existence verified in ip_wput_nondata */ 8773 mp1 = mp->b_cont->b_cont; 8774 8775 /* 8776 * Must be (better be!) continuation of a TRANSPARENT 8777 * IOCTL. We just copied in the lifsrcof structure. 8778 */ 8779 STRUCT_SET_HANDLE(lifs, iocp->ioc_flag, 8780 (struct lifsrcof *)mp1->b_rptr); 8781 8782 if (MBLKL(mp1) != STRUCT_SIZE(lifs)) 8783 return (EINVAL); 8784 8785 ifindex = STRUCT_FGET(lifs, lifs_ifindex); 8786 isv6 = (Q_TO_CONN(q))->conn_af_isv6; 8787 ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, q, mp, 8788 ip_process_ioctl, &err, ipst); 8789 if (ipif == NULL) { 8790 ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n", 8791 ifindex)); 8792 return (err); 8793 } 8794 8795 8796 /* Allocate a buffer to hold requested information */ 8797 numlifs = ip_get_lifsrcofnum(ipif->ipif_ill); 8798 lifs_bufsize = numlifs * sizeof (struct lifreq); 8799 lifsmaxlen = STRUCT_FGET(lifs, lifs_maxlen); 8800 /* The actual size needed is always returned in lifs_len */ 8801 STRUCT_FSET(lifs, lifs_len, lifs_bufsize); 8802 8803 /* If the amount we need is more than what is passed in, abort */ 8804 if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) { 8805 ipif_refrele(ipif); 8806 return (0); 8807 } 8808 8809 mp1 = mi_copyout_alloc(q, mp, 8810 STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE); 8811 if (mp1 == NULL) { 8812 ipif_refrele(ipif); 8813 return (ENOMEM); 8814 } 8815 8816 mp1->b_wptr = mp1->b_rptr + lifs_bufsize; 8817 bzero(mp1->b_rptr, lifs_bufsize); 8818 8819 lifr = (struct lifreq *)mp1->b_rptr; 8820 8821 ill = ill_head = ipif->ipif_ill; 8822 orig_ipif = ipif; 8823 8824 /* ill_g_usesrc_lock protects ill_usesrc_grp_next */ 8825 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER); 8826 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 8827 8828 ill = ill->ill_usesrc_grp_next; /* start from next ill */ 8829 for (; (ill != NULL) && (ill != ill_head); 8830 ill = ill->ill_usesrc_grp_next) { 8831 8832 if ((uchar_t *)&lifr[1] > mp1->b_wptr) 8833 break; 8834 8835 ipif = ill->ill_ipif; 8836 (void) ipif_get_name(ipif, 8837 lifr->lifr_name, sizeof (lifr->lifr_name)); 8838 if (ipif->ipif_isv6) { 8839 sin6 = (sin6_t *)&lifr->lifr_addr; 8840 *sin6 = sin6_null; 8841 sin6->sin6_family = AF_INET6; 8842 sin6->sin6_addr = ipif->ipif_v6lcl_addr; 8843 lifr->lifr_addrlen = ip_mask_to_plen_v6( 8844 &ipif->ipif_v6net_mask); 8845 } else { 8846 sin = (sin_t *)&lifr->lifr_addr; 8847 *sin = sin_null; 8848 sin->sin_family = AF_INET; 8849 sin->sin_addr.s_addr = ipif->ipif_lcl_addr; 8850 lifr->lifr_addrlen = ip_mask_to_plen( 8851 ipif->ipif_net_mask); 8852 } 8853 lifr++; 8854 } 8855 rw_exit(&ipst->ips_ill_g_usesrc_lock); 8856 rw_exit(&ipst->ips_ill_g_lock); 8857 ipif_refrele(orig_ipif); 8858 mp1->b_wptr = (uchar_t *)lifr; 8859 STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr)); 8860 8861 return (0); 8862 } 8863 8864 /* ARGSUSED */ 8865 int 8866 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, 8867 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq) 8868 { 8869 mblk_t *mp1; 8870 int list; 8871 ill_t *ill; 8872 ipif_t *ipif; 8873 int flags; 8874 int numlifs = 0; 8875 size_t lifc_bufsize; 8876 struct lifreq *lifr; 8877 sa_family_t family; 8878 struct sockaddr_in *sin; 8879 struct sockaddr_in6 *sin6; 8880 ill_walk_context_t ctx; 8881 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 8882 int32_t lifclen; 8883 zoneid_t zoneid; 8884 STRUCT_HANDLE(lifconf, lifc); 8885 ip_stack_t *ipst = CONNQ_TO_IPST(q); 8886 8887 ip1dbg(("ip_sioctl_get_lifconf")); 8888 8889 ASSERT(q->q_next == NULL); 8890 8891 zoneid = Q_TO_CONN(q)->conn_zoneid; 8892 8893 /* Existence verified in ip_wput_nondata */ 8894 mp1 = mp->b_cont->b_cont; 8895 8896 /* 8897 * An extended version of SIOCGIFCONF that takes an 8898 * additional address family and flags field. 8899 * AF_UNSPEC retrieve both IPv4 and IPv6. 8900 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT 8901 * interfaces are omitted. 8902 * Similarly, IPIF_TEMPORARY interfaces are omitted 8903 * unless LIFC_TEMPORARY is specified. 8904 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT, 8905 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and 8906 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE 8907 * has priority over LIFC_NOXMIT. 8908 */ 8909 STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL); 8910 8911 if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc)) 8912 return (EINVAL); 8913 8914 /* 8915 * Must be (better be!) continuation of a TRANSPARENT 8916 * IOCTL. We just copied in the lifconf structure. 8917 */ 8918 STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr); 8919 8920 family = STRUCT_FGET(lifc, lifc_family); 8921 flags = STRUCT_FGET(lifc, lifc_flags); 8922 8923 switch (family) { 8924 case AF_UNSPEC: 8925 /* 8926 * walk all ILL's. 8927 */ 8928 list = MAX_G_HEADS; 8929 break; 8930 case AF_INET: 8931 /* 8932 * walk only IPV4 ILL's. 8933 */ 8934 list = IP_V4_G_HEAD; 8935 break; 8936 case AF_INET6: 8937 /* 8938 * walk only IPV6 ILL's. 8939 */ 8940 list = IP_V6_G_HEAD; 8941 break; 8942 default: 8943 return (EAFNOSUPPORT); 8944 } 8945 8946 /* 8947 * Allocate a buffer to hold requested information. 8948 * 8949 * If lifc_len is larger than what is needed, we only 8950 * allocate what we will use. 8951 * 8952 * If lifc_len is smaller than what is needed, return 8953 * EINVAL. 8954 */ 8955 numlifs = ip_get_numlifs(family, flags, zoneid, ipst); 8956 lifc_bufsize = numlifs * sizeof (struct lifreq); 8957 lifclen = STRUCT_FGET(lifc, lifc_len); 8958 if (lifc_bufsize > lifclen) { 8959 if (iocp->ioc_cmd == O_SIOCGLIFCONF) 8960 return (EINVAL); 8961 else 8962 lifc_bufsize = lifclen; 8963 } 8964 8965 mp1 = mi_copyout_alloc(q, mp, 8966 STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE); 8967 if (mp1 == NULL) 8968 return (ENOMEM); 8969 8970 mp1->b_wptr = mp1->b_rptr + lifc_bufsize; 8971 bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr); 8972 8973 lifr = (struct lifreq *)mp1->b_rptr; 8974 8975 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 8976 ill = ill_first(list, list, &ctx, ipst); 8977 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 8978 for (ipif = ill->ill_ipif; ipif != NULL; 8979 ipif = ipif->ipif_next) { 8980 if ((ipif->ipif_flags & IPIF_NOXMIT) && 8981 !(flags & LIFC_NOXMIT)) 8982 continue; 8983 8984 if ((ipif->ipif_flags & IPIF_TEMPORARY) && 8985 !(flags & LIFC_TEMPORARY)) 8986 continue; 8987 8988 if (((ipif->ipif_flags & 8989 (IPIF_NOXMIT|IPIF_NOLOCAL| 8990 IPIF_DEPRECATED)) || 8991 (ill->ill_phyint->phyint_flags & 8992 PHYI_LOOPBACK) || 8993 !(ipif->ipif_flags & IPIF_UP)) && 8994 (flags & LIFC_EXTERNAL_SOURCE)) 8995 continue; 8996 8997 if (zoneid != ipif->ipif_zoneid && 8998 ipif->ipif_zoneid != ALL_ZONES && 8999 (zoneid != GLOBAL_ZONEID || 9000 !(flags & LIFC_ALLZONES))) 9001 continue; 9002 9003 if ((uchar_t *)&lifr[1] > mp1->b_wptr) { 9004 if (iocp->ioc_cmd == O_SIOCGLIFCONF) { 9005 rw_exit(&ipst->ips_ill_g_lock); 9006 return (EINVAL); 9007 } else { 9008 goto lif_copydone; 9009 } 9010 } 9011 9012 (void) ipif_get_name(ipif, 9013 lifr->lifr_name, 9014 sizeof (lifr->lifr_name)); 9015 if (ipif->ipif_isv6) { 9016 sin6 = (sin6_t *)&lifr->lifr_addr; 9017 *sin6 = sin6_null; 9018 sin6->sin6_family = AF_INET6; 9019 sin6->sin6_addr = 9020 ipif->ipif_v6lcl_addr; 9021 lifr->lifr_addrlen = 9022 ip_mask_to_plen_v6( 9023 &ipif->ipif_v6net_mask); 9024 } else { 9025 sin = (sin_t *)&lifr->lifr_addr; 9026 *sin = sin_null; 9027 sin->sin_family = AF_INET; 9028 sin->sin_addr.s_addr = 9029 ipif->ipif_lcl_addr; 9030 lifr->lifr_addrlen = 9031 ip_mask_to_plen( 9032 ipif->ipif_net_mask); 9033 } 9034 lifr++; 9035 } 9036 } 9037 lif_copydone: 9038 rw_exit(&ipst->ips_ill_g_lock); 9039 9040 mp1->b_wptr = (uchar_t *)lifr; 9041 if (STRUCT_BUF(lifc) != NULL) { 9042 STRUCT_FSET(lifc, lifc_len, 9043 (int)((uchar_t *)lifr - mp1->b_rptr)); 9044 } 9045 return (0); 9046 } 9047 9048 /* ARGSUSED */ 9049 int 9050 ip_sioctl_set_ipmpfailback(ipif_t *dummy_ipif, sin_t *dummy_sin, 9051 queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq) 9052 { 9053 ip_stack_t *ipst; 9054 9055 if (q->q_next == NULL) 9056 ipst = CONNQ_TO_IPST(q); 9057 else 9058 ipst = ILLQ_TO_IPST(q); 9059 9060 /* Existence of b_cont->b_cont checked in ip_wput_nondata */ 9061 ipst->ips_ipmp_enable_failback = *(int *)mp->b_cont->b_cont->b_rptr; 9062 return (0); 9063 } 9064 9065 static void 9066 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp) 9067 { 9068 ip6_asp_t *table; 9069 size_t table_size; 9070 mblk_t *data_mp; 9071 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 9072 ip_stack_t *ipst; 9073 9074 if (q->q_next == NULL) 9075 ipst = CONNQ_TO_IPST(q); 9076 else 9077 ipst = ILLQ_TO_IPST(q); 9078 9079 /* These two ioctls are I_STR only */ 9080 if (iocp->ioc_count == TRANSPARENT) { 9081 miocnak(q, mp, 0, EINVAL); 9082 return; 9083 } 9084 9085 data_mp = mp->b_cont; 9086 if (data_mp == NULL) { 9087 /* The user passed us a NULL argument */ 9088 table = NULL; 9089 table_size = iocp->ioc_count; 9090 } else { 9091 /* 9092 * The user provided a table. The stream head 9093 * may have copied in the user data in chunks, 9094 * so make sure everything is pulled up 9095 * properly. 9096 */ 9097 if (MBLKL(data_mp) < iocp->ioc_count) { 9098 mblk_t *new_data_mp; 9099 if ((new_data_mp = msgpullup(data_mp, -1)) == 9100 NULL) { 9101 miocnak(q, mp, 0, ENOMEM); 9102 return; 9103 } 9104 freemsg(data_mp); 9105 data_mp = new_data_mp; 9106 mp->b_cont = data_mp; 9107 } 9108 table = (ip6_asp_t *)data_mp->b_rptr; 9109 table_size = iocp->ioc_count; 9110 } 9111 9112 switch (iocp->ioc_cmd) { 9113 case SIOCGIP6ADDRPOLICY: 9114 iocp->ioc_rval = ip6_asp_get(table, table_size, ipst); 9115 if (iocp->ioc_rval == -1) 9116 iocp->ioc_error = EINVAL; 9117 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4 9118 else if (table != NULL && 9119 (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) { 9120 ip6_asp_t *src = table; 9121 ip6_asp32_t *dst = (void *)table; 9122 int count = table_size / sizeof (ip6_asp_t); 9123 int i; 9124 9125 /* 9126 * We need to do an in-place shrink of the array 9127 * to match the alignment attributes of the 9128 * 32-bit ABI looking at it. 9129 */ 9130 /* LINTED: logical expression always true: op "||" */ 9131 ASSERT(sizeof (*src) > sizeof (*dst)); 9132 for (i = 1; i < count; i++) 9133 bcopy(src + i, dst + i, sizeof (*dst)); 9134 } 9135 #endif 9136 break; 9137 9138 case SIOCSIP6ADDRPOLICY: 9139 ASSERT(mp->b_prev == NULL); 9140 mp->b_prev = (void *)q; 9141 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4 9142 /* 9143 * We pass in the datamodel here so that the ip6_asp_replace() 9144 * routine can handle converting from 32-bit to native formats 9145 * where necessary. 9146 * 9147 * A better way to handle this might be to convert the inbound 9148 * data structure here, and hang it off a new 'mp'; thus the 9149 * ip6_asp_replace() logic would always be dealing with native 9150 * format data structures.. 9151 * 9152 * (An even simpler way to handle these ioctls is to just 9153 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure 9154 * and just recompile everything that depends on it.) 9155 */ 9156 #endif 9157 ip6_asp_replace(mp, table, table_size, B_FALSE, ipst, 9158 iocp->ioc_flag & IOC_MODELS); 9159 return; 9160 } 9161 9162 DB_TYPE(mp) = (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK; 9163 qreply(q, mp); 9164 } 9165 9166 static void 9167 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp) 9168 { 9169 mblk_t *data_mp; 9170 struct dstinforeq *dir; 9171 uint8_t *end, *cur; 9172 in6_addr_t *daddr, *saddr; 9173 ipaddr_t v4daddr; 9174 ire_t *ire; 9175 char *slabel, *dlabel; 9176 boolean_t isipv4; 9177 int match_ire; 9178 ill_t *dst_ill; 9179 ipif_t *src_ipif, *ire_ipif; 9180 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 9181 zoneid_t zoneid; 9182 ip_stack_t *ipst = CONNQ_TO_IPST(q); 9183 9184 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */ 9185 zoneid = Q_TO_CONN(q)->conn_zoneid; 9186 9187 /* 9188 * This ioctl is I_STR only, and must have a 9189 * data mblk following the M_IOCTL mblk. 9190 */ 9191 data_mp = mp->b_cont; 9192 if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) { 9193 miocnak(q, mp, 0, EINVAL); 9194 return; 9195 } 9196 9197 if (MBLKL(data_mp) < iocp->ioc_count) { 9198 mblk_t *new_data_mp; 9199 9200 if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) { 9201 miocnak(q, mp, 0, ENOMEM); 9202 return; 9203 } 9204 freemsg(data_mp); 9205 data_mp = new_data_mp; 9206 mp->b_cont = data_mp; 9207 } 9208 match_ire = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | MATCH_IRE_PARENT; 9209 9210 for (cur = data_mp->b_rptr, end = data_mp->b_wptr; 9211 end - cur >= sizeof (struct dstinforeq); 9212 cur += sizeof (struct dstinforeq)) { 9213 dir = (struct dstinforeq *)cur; 9214 daddr = &dir->dir_daddr; 9215 saddr = &dir->dir_saddr; 9216 9217 /* 9218 * ip_addr_scope_v6() and ip6_asp_lookup() handle 9219 * v4 mapped addresses; ire_ftable_lookup[_v6]() 9220 * and ipif_select_source[_v6]() do not. 9221 */ 9222 dir->dir_dscope = ip_addr_scope_v6(daddr); 9223 dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst); 9224 9225 isipv4 = IN6_IS_ADDR_V4MAPPED(daddr); 9226 if (isipv4) { 9227 IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr); 9228 ire = ire_ftable_lookup(v4daddr, NULL, NULL, 9229 0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst); 9230 } else { 9231 ire = ire_ftable_lookup_v6(daddr, NULL, NULL, 9232 0, NULL, NULL, zoneid, 0, NULL, match_ire, ipst); 9233 } 9234 if (ire == NULL) { 9235 dir->dir_dreachable = 0; 9236 9237 /* move on to next dst addr */ 9238 continue; 9239 } 9240 dir->dir_dreachable = 1; 9241 9242 ire_ipif = ire->ire_ipif; 9243 if (ire_ipif == NULL) 9244 goto next_dst; 9245 9246 /* 9247 * We expect to get back an interface ire or a 9248 * gateway ire cache entry. For both types, the 9249 * output interface is ire_ipif->ipif_ill. 9250 */ 9251 dst_ill = ire_ipif->ipif_ill; 9252 dir->dir_dmactype = dst_ill->ill_mactype; 9253 9254 if (isipv4) { 9255 src_ipif = ipif_select_source(dst_ill, v4daddr, zoneid); 9256 } else { 9257 src_ipif = ipif_select_source_v6(dst_ill, 9258 daddr, RESTRICT_TO_NONE, IPV6_PREFER_SRC_DEFAULT, 9259 zoneid); 9260 } 9261 if (src_ipif == NULL) 9262 goto next_dst; 9263 9264 *saddr = src_ipif->ipif_v6lcl_addr; 9265 dir->dir_sscope = ip_addr_scope_v6(saddr); 9266 slabel = ip6_asp_lookup(saddr, NULL, ipst); 9267 dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel); 9268 dir->dir_sdeprecated = 9269 (src_ipif->ipif_flags & IPIF_DEPRECATED) ? 1 : 0; 9270 ipif_refrele(src_ipif); 9271 next_dst: 9272 ire_refrele(ire); 9273 } 9274 miocack(q, mp, iocp->ioc_count, 0); 9275 } 9276 9277 9278 /* 9279 * Check if this is an address assigned to this machine. 9280 * Skips interfaces that are down by using ire checks. 9281 * Translates mapped addresses to v4 addresses and then 9282 * treats them as such, returning true if the v4 address 9283 * associated with this mapped address is configured. 9284 * Note: Applications will have to be careful what they do 9285 * with the response; use of mapped addresses limits 9286 * what can be done with the socket, especially with 9287 * respect to socket options and ioctls - neither IPv4 9288 * options nor IPv6 sticky options/ancillary data options 9289 * may be used. 9290 */ 9291 /* ARGSUSED */ 9292 int 9293 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 9294 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 9295 { 9296 struct sioc_addrreq *sia; 9297 sin_t *sin; 9298 ire_t *ire; 9299 mblk_t *mp1; 9300 zoneid_t zoneid; 9301 ip_stack_t *ipst; 9302 9303 ip1dbg(("ip_sioctl_tmyaddr")); 9304 9305 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */ 9306 zoneid = Q_TO_CONN(q)->conn_zoneid; 9307 ipst = CONNQ_TO_IPST(q); 9308 9309 /* Existence verified in ip_wput_nondata */ 9310 mp1 = mp->b_cont->b_cont; 9311 sia = (struct sioc_addrreq *)mp1->b_rptr; 9312 sin = (sin_t *)&sia->sa_addr; 9313 switch (sin->sin_family) { 9314 case AF_INET6: { 9315 sin6_t *sin6 = (sin6_t *)sin; 9316 9317 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 9318 ipaddr_t v4_addr; 9319 9320 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr, 9321 v4_addr); 9322 ire = ire_ctable_lookup(v4_addr, 0, 9323 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, 9324 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 9325 } else { 9326 in6_addr_t v6addr; 9327 9328 v6addr = sin6->sin6_addr; 9329 ire = ire_ctable_lookup_v6(&v6addr, 0, 9330 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, 9331 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 9332 } 9333 break; 9334 } 9335 case AF_INET: { 9336 ipaddr_t v4addr; 9337 9338 v4addr = sin->sin_addr.s_addr; 9339 ire = ire_ctable_lookup(v4addr, 0, 9340 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, 9341 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, ipst); 9342 break; 9343 } 9344 default: 9345 return (EAFNOSUPPORT); 9346 } 9347 if (ire != NULL) { 9348 sia->sa_res = 1; 9349 ire_refrele(ire); 9350 } else { 9351 sia->sa_res = 0; 9352 } 9353 return (0); 9354 } 9355 9356 /* 9357 * Check if this is an address assigned on-link i.e. neighbor, 9358 * and makes sure it's reachable from the current zone. 9359 * Returns true for my addresses as well. 9360 * Translates mapped addresses to v4 addresses and then 9361 * treats them as such, returning true if the v4 address 9362 * associated with this mapped address is configured. 9363 * Note: Applications will have to be careful what they do 9364 * with the response; use of mapped addresses limits 9365 * what can be done with the socket, especially with 9366 * respect to socket options and ioctls - neither IPv4 9367 * options nor IPv6 sticky options/ancillary data options 9368 * may be used. 9369 */ 9370 /* ARGSUSED */ 9371 int 9372 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 9373 ip_ioctl_cmd_t *ipip, void *duymmy_ifreq) 9374 { 9375 struct sioc_addrreq *sia; 9376 sin_t *sin; 9377 mblk_t *mp1; 9378 ire_t *ire = NULL; 9379 zoneid_t zoneid; 9380 ip_stack_t *ipst; 9381 9382 ip1dbg(("ip_sioctl_tonlink")); 9383 9384 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */ 9385 zoneid = Q_TO_CONN(q)->conn_zoneid; 9386 ipst = CONNQ_TO_IPST(q); 9387 9388 /* Existence verified in ip_wput_nondata */ 9389 mp1 = mp->b_cont->b_cont; 9390 sia = (struct sioc_addrreq *)mp1->b_rptr; 9391 sin = (sin_t *)&sia->sa_addr; 9392 9393 /* 9394 * Match addresses with a zero gateway field to avoid 9395 * routes going through a router. 9396 * Exclude broadcast and multicast addresses. 9397 */ 9398 switch (sin->sin_family) { 9399 case AF_INET6: { 9400 sin6_t *sin6 = (sin6_t *)sin; 9401 9402 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 9403 ipaddr_t v4_addr; 9404 9405 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr, 9406 v4_addr); 9407 if (!CLASSD(v4_addr)) { 9408 ire = ire_route_lookup(v4_addr, 0, 0, 0, 9409 NULL, NULL, zoneid, NULL, 9410 MATCH_IRE_GW, ipst); 9411 } 9412 } else { 9413 in6_addr_t v6addr; 9414 in6_addr_t v6gw; 9415 9416 v6addr = sin6->sin6_addr; 9417 v6gw = ipv6_all_zeros; 9418 if (!IN6_IS_ADDR_MULTICAST(&v6addr)) { 9419 ire = ire_route_lookup_v6(&v6addr, 0, 9420 &v6gw, 0, NULL, NULL, zoneid, 9421 NULL, MATCH_IRE_GW, ipst); 9422 } 9423 } 9424 break; 9425 } 9426 case AF_INET: { 9427 ipaddr_t v4addr; 9428 9429 v4addr = sin->sin_addr.s_addr; 9430 if (!CLASSD(v4addr)) { 9431 ire = ire_route_lookup(v4addr, 0, 0, 0, 9432 NULL, NULL, zoneid, NULL, 9433 MATCH_IRE_GW, ipst); 9434 } 9435 break; 9436 } 9437 default: 9438 return (EAFNOSUPPORT); 9439 } 9440 sia->sa_res = 0; 9441 if (ire != NULL) { 9442 if (ire->ire_type & (IRE_INTERFACE|IRE_CACHE| 9443 IRE_LOCAL|IRE_LOOPBACK)) { 9444 sia->sa_res = 1; 9445 } 9446 ire_refrele(ire); 9447 } 9448 return (0); 9449 } 9450 9451 /* 9452 * TBD: implement when kernel maintaines a list of site prefixes. 9453 */ 9454 /* ARGSUSED */ 9455 int 9456 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 9457 ip_ioctl_cmd_t *ipip, void *ifreq) 9458 { 9459 return (ENXIO); 9460 } 9461 9462 /* ARGSUSED */ 9463 int 9464 ip_sioctl_tunparam(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 9465 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 9466 { 9467 ill_t *ill; 9468 mblk_t *mp1; 9469 conn_t *connp; 9470 boolean_t success; 9471 9472 ip1dbg(("ip_sioctl_tunparam(%s:%u %p)\n", 9473 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 9474 /* ioctl comes down on an conn */ 9475 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL); 9476 connp = Q_TO_CONN(q); 9477 9478 mp->b_datap->db_type = M_IOCTL; 9479 9480 /* 9481 * Send down a copy. (copymsg does not copy b_next/b_prev). 9482 * The original mp contains contaminated b_next values due to 'mi', 9483 * which is needed to do the mi_copy_done. Unfortunately if we 9484 * send down the original mblk itself and if we are popped due to an 9485 * an unplumb before the response comes back from tunnel, 9486 * the streamhead (which does a freemsg) will see this contaminated 9487 * message and the assertion in freemsg about non-null b_next/b_prev 9488 * will panic a DEBUG kernel. 9489 */ 9490 mp1 = copymsg(mp); 9491 if (mp1 == NULL) 9492 return (ENOMEM); 9493 9494 ill = ipif->ipif_ill; 9495 mutex_enter(&connp->conn_lock); 9496 mutex_enter(&ill->ill_lock); 9497 if (ipip->ipi_cmd == SIOCSTUNPARAM || ipip->ipi_cmd == OSIOCSTUNPARAM) { 9498 success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), 9499 mp, 0); 9500 } else { 9501 success = ill_pending_mp_add(ill, connp, mp); 9502 } 9503 mutex_exit(&ill->ill_lock); 9504 mutex_exit(&connp->conn_lock); 9505 9506 if (success) { 9507 ip1dbg(("sending down tunparam request ")); 9508 putnext(ill->ill_wq, mp1); 9509 return (EINPROGRESS); 9510 } else { 9511 /* The conn has started closing */ 9512 freemsg(mp1); 9513 return (EINTR); 9514 } 9515 } 9516 9517 static int 9518 ip_sioctl_arp_common(ill_t *ill, queue_t *q, mblk_t *mp, sin_t *sin, 9519 boolean_t x_arp_ioctl, boolean_t if_arp_ioctl) 9520 { 9521 mblk_t *mp1; 9522 mblk_t *mp2; 9523 mblk_t *pending_mp; 9524 ipaddr_t ipaddr; 9525 area_t *area; 9526 struct iocblk *iocp; 9527 conn_t *connp; 9528 struct arpreq *ar; 9529 struct xarpreq *xar; 9530 boolean_t success; 9531 int flags, alength; 9532 char *lladdr; 9533 ip_stack_t *ipst; 9534 9535 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL); 9536 connp = Q_TO_CONN(q); 9537 ipst = connp->conn_netstack->netstack_ip; 9538 9539 iocp = (struct iocblk *)mp->b_rptr; 9540 /* 9541 * ill has already been set depending on whether 9542 * bsd style or interface style ioctl. 9543 */ 9544 ASSERT(ill != NULL); 9545 9546 /* 9547 * Is this one of the new SIOC*XARP ioctls? 9548 */ 9549 if (x_arp_ioctl) { 9550 /* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */ 9551 xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr; 9552 ar = NULL; 9553 9554 flags = xar->xarp_flags; 9555 lladdr = LLADDR(&xar->xarp_ha); 9556 /* 9557 * Validate against user's link layer address length 9558 * input and name and addr length limits. 9559 */ 9560 alength = ill->ill_phys_addr_length; 9561 if (iocp->ioc_cmd == SIOCSXARP) { 9562 if (alength != xar->xarp_ha.sdl_alen || 9563 (alength + xar->xarp_ha.sdl_nlen > 9564 sizeof (xar->xarp_ha.sdl_data))) 9565 return (EINVAL); 9566 } 9567 } else { 9568 /* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */ 9569 ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr; 9570 xar = NULL; 9571 9572 flags = ar->arp_flags; 9573 lladdr = ar->arp_ha.sa_data; 9574 /* 9575 * Theoretically, the sa_family could tell us what link 9576 * layer type this operation is trying to deal with. By 9577 * common usage AF_UNSPEC means ethernet. We'll assume 9578 * any attempt to use the SIOC?ARP ioctls is for ethernet, 9579 * for now. Our new SIOC*XARP ioctls can be used more 9580 * generally. 9581 * 9582 * If the underlying media happens to have a non 6 byte 9583 * address, arp module will fail set/get, but the del 9584 * operation will succeed. 9585 */ 9586 alength = 6; 9587 if ((iocp->ioc_cmd != SIOCDARP) && 9588 (alength != ill->ill_phys_addr_length)) { 9589 return (EINVAL); 9590 } 9591 } 9592 9593 /* 9594 * We are going to pass up to ARP a packet chain that looks 9595 * like: 9596 * 9597 * M_IOCTL-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK 9598 * 9599 * Get a copy of the original IOCTL mblk to head the chain, 9600 * to be sent up (in mp1). Also get another copy to store 9601 * in the ill_pending_mp list, for matching the response 9602 * when it comes back from ARP. 9603 */ 9604 mp1 = copyb(mp); 9605 pending_mp = copymsg(mp); 9606 if (mp1 == NULL || pending_mp == NULL) { 9607 if (mp1 != NULL) 9608 freeb(mp1); 9609 if (pending_mp != NULL) 9610 inet_freemsg(pending_mp); 9611 return (ENOMEM); 9612 } 9613 9614 ipaddr = sin->sin_addr.s_addr; 9615 9616 mp2 = ill_arp_alloc(ill, (uchar_t *)&ip_area_template, 9617 (caddr_t)&ipaddr); 9618 if (mp2 == NULL) { 9619 freeb(mp1); 9620 inet_freemsg(pending_mp); 9621 return (ENOMEM); 9622 } 9623 /* Put together the chain. */ 9624 mp1->b_cont = mp2; 9625 mp1->b_datap->db_type = M_IOCTL; 9626 mp2->b_cont = mp; 9627 mp2->b_datap->db_type = M_DATA; 9628 9629 iocp = (struct iocblk *)mp1->b_rptr; 9630 9631 /* 9632 * An M_IOCDATA's payload (struct copyresp) is mostly the same as an 9633 * M_IOCTL's payload (struct iocblk), but 'struct copyresp' has a 9634 * cp_private field (or cp_rval on 32-bit systems) in place of the 9635 * ioc_count field; set ioc_count to be correct. 9636 */ 9637 iocp->ioc_count = MBLKL(mp1->b_cont); 9638 9639 /* 9640 * Set the proper command in the ARP message. 9641 * Convert the SIOC{G|S|D}ARP calls into our 9642 * AR_ENTRY_xxx calls. 9643 */ 9644 area = (area_t *)mp2->b_rptr; 9645 switch (iocp->ioc_cmd) { 9646 case SIOCDARP: 9647 case SIOCDXARP: 9648 /* 9649 * We defer deleting the corresponding IRE until 9650 * we return from arp. 9651 */ 9652 area->area_cmd = AR_ENTRY_DELETE; 9653 area->area_proto_mask_offset = 0; 9654 break; 9655 case SIOCGARP: 9656 case SIOCGXARP: 9657 area->area_cmd = AR_ENTRY_SQUERY; 9658 area->area_proto_mask_offset = 0; 9659 break; 9660 case SIOCSARP: 9661 case SIOCSXARP: { 9662 /* 9663 * Delete the corresponding ire to make sure IP will 9664 * pick up any change from arp. 9665 */ 9666 if (!if_arp_ioctl) { 9667 (void) ip_ire_clookup_and_delete(ipaddr, NULL, ipst); 9668 break; 9669 } else { 9670 ipif_t *ipif = ipif_get_next_ipif(NULL, ill); 9671 if (ipif != NULL) { 9672 (void) ip_ire_clookup_and_delete(ipaddr, ipif, 9673 ipst); 9674 ipif_refrele(ipif); 9675 } 9676 break; 9677 } 9678 } 9679 } 9680 iocp->ioc_cmd = area->area_cmd; 9681 9682 /* 9683 * Before sending 'mp' to ARP, we have to clear the b_next 9684 * and b_prev. Otherwise if STREAMS encounters such a message 9685 * in freemsg(), (because ARP can close any time) it can cause 9686 * a panic. But mi code needs the b_next and b_prev values of 9687 * mp->b_cont, to complete the ioctl. So we store it here 9688 * in pending_mp->bcont, and restore it in ip_sioctl_iocack() 9689 * when the response comes down from ARP. 9690 */ 9691 pending_mp->b_cont->b_next = mp->b_cont->b_next; 9692 pending_mp->b_cont->b_prev = mp->b_cont->b_prev; 9693 mp->b_cont->b_next = NULL; 9694 mp->b_cont->b_prev = NULL; 9695 9696 mutex_enter(&connp->conn_lock); 9697 mutex_enter(&ill->ill_lock); 9698 /* conn has not yet started closing, hence this can't fail */ 9699 success = ill_pending_mp_add(ill, connp, pending_mp); 9700 ASSERT(success); 9701 mutex_exit(&ill->ill_lock); 9702 mutex_exit(&connp->conn_lock); 9703 9704 /* 9705 * Fill in the rest of the ARP operation fields. 9706 */ 9707 area->area_hw_addr_length = alength; 9708 bcopy(lladdr, 9709 (char *)area + area->area_hw_addr_offset, 9710 area->area_hw_addr_length); 9711 /* Translate the flags. */ 9712 if (flags & ATF_PERM) 9713 area->area_flags |= ACE_F_PERMANENT; 9714 if (flags & ATF_PUBL) 9715 area->area_flags |= ACE_F_PUBLISH; 9716 if (flags & ATF_AUTHORITY) 9717 area->area_flags |= ACE_F_AUTHORITY; 9718 9719 /* 9720 * Up to ARP it goes. The response will come 9721 * back in ip_wput as an M_IOCACK message, and 9722 * will be handed to ip_sioctl_iocack for 9723 * completion. 9724 */ 9725 putnext(ill->ill_rq, mp1); 9726 return (EINPROGRESS); 9727 } 9728 9729 /* ARGSUSED */ 9730 int 9731 ip_sioctl_xarp(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 9732 ip_ioctl_cmd_t *ipip, void *ifreq) 9733 { 9734 struct xarpreq *xar; 9735 boolean_t isv6; 9736 mblk_t *mp1; 9737 int err; 9738 conn_t *connp; 9739 int ifnamelen; 9740 ire_t *ire = NULL; 9741 ill_t *ill = NULL; 9742 struct sockaddr_in *sin; 9743 boolean_t if_arp_ioctl = B_FALSE; 9744 ip_stack_t *ipst; 9745 9746 /* ioctl comes down on an conn */ 9747 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL); 9748 connp = Q_TO_CONN(q); 9749 isv6 = connp->conn_af_isv6; 9750 ipst = connp->conn_netstack->netstack_ip; 9751 9752 /* Existance verified in ip_wput_nondata */ 9753 mp1 = mp->b_cont->b_cont; 9754 9755 ASSERT(MBLKL(mp1) >= sizeof (*xar)); 9756 xar = (struct xarpreq *)mp1->b_rptr; 9757 sin = (sin_t *)&xar->xarp_pa; 9758 9759 if (isv6 || (xar->xarp_ha.sdl_family != AF_LINK) || 9760 (xar->xarp_pa.ss_family != AF_INET)) 9761 return (ENXIO); 9762 9763 ifnamelen = xar->xarp_ha.sdl_nlen; 9764 if (ifnamelen != 0) { 9765 char *cptr, cval; 9766 9767 if (ifnamelen >= LIFNAMSIZ) 9768 return (EINVAL); 9769 9770 /* 9771 * Instead of bcopying a bunch of bytes, 9772 * null-terminate the string in-situ. 9773 */ 9774 cptr = xar->xarp_ha.sdl_data + ifnamelen; 9775 cval = *cptr; 9776 *cptr = '\0'; 9777 ill = ill_lookup_on_name(xar->xarp_ha.sdl_data, 9778 B_FALSE, isv6, CONNP_TO_WQ(connp), mp, ip_process_ioctl, 9779 &err, NULL, ipst); 9780 *cptr = cval; 9781 if (ill == NULL) 9782 return (err); 9783 if (ill->ill_net_type != IRE_IF_RESOLVER) { 9784 ill_refrele(ill); 9785 return (ENXIO); 9786 } 9787 9788 if_arp_ioctl = B_TRUE; 9789 } else { 9790 /* 9791 * PSARC 2003/088 states that if sdl_nlen == 0, it behaves 9792 * as an extended BSD ioctl. The kernel uses the IP address 9793 * to figure out the network interface. 9794 */ 9795 ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, 9796 ipst); 9797 if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) || 9798 ((ill = ire_to_ill(ire)) == NULL) || 9799 (ill->ill_net_type != IRE_IF_RESOLVER)) { 9800 if (ire != NULL) 9801 ire_refrele(ire); 9802 ire = ire_ftable_lookup(sin->sin_addr.s_addr, 9803 0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, 9804 NULL, MATCH_IRE_TYPE, ipst); 9805 if ((ire == NULL) || 9806 ((ill = ire_to_ill(ire)) == NULL)) { 9807 if (ire != NULL) 9808 ire_refrele(ire); 9809 return (ENXIO); 9810 } 9811 } 9812 ASSERT(ire != NULL && ill != NULL); 9813 } 9814 9815 err = ip_sioctl_arp_common(ill, q, mp, sin, B_TRUE, if_arp_ioctl); 9816 if (if_arp_ioctl) 9817 ill_refrele(ill); 9818 if (ire != NULL) 9819 ire_refrele(ire); 9820 9821 return (err); 9822 } 9823 9824 /* 9825 * ARP IOCTLs. 9826 * How does IP get in the business of fronting ARP configuration/queries? 9827 * Well its like this, the Berkeley ARP IOCTLs (SIOCGARP, SIOCDARP, SIOCSARP) 9828 * are by tradition passed in through a datagram socket. That lands in IP. 9829 * As it happens, this is just as well since the interface is quite crude in 9830 * that it passes in no information about protocol or hardware types, or 9831 * interface association. After making the protocol assumption, IP is in 9832 * the position to look up the name of the ILL, which ARP will need, and 9833 * format a request that can be handled by ARP. The request is passed up 9834 * stream to ARP, and the original IOCTL is completed by IP when ARP passes 9835 * back a response. ARP supports its own set of more general IOCTLs, in 9836 * case anyone is interested. 9837 */ 9838 /* ARGSUSED */ 9839 int 9840 ip_sioctl_arp(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 9841 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 9842 { 9843 struct arpreq *ar; 9844 struct sockaddr_in *sin; 9845 ire_t *ire; 9846 boolean_t isv6; 9847 mblk_t *mp1; 9848 int err; 9849 conn_t *connp; 9850 ill_t *ill; 9851 ip_stack_t *ipst; 9852 9853 /* ioctl comes down on an conn */ 9854 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL); 9855 connp = Q_TO_CONN(q); 9856 ipst = CONNQ_TO_IPST(q); 9857 isv6 = connp->conn_af_isv6; 9858 if (isv6) 9859 return (ENXIO); 9860 9861 /* Existance verified in ip_wput_nondata */ 9862 mp1 = mp->b_cont->b_cont; 9863 9864 ar = (struct arpreq *)mp1->b_rptr; 9865 sin = (sin_t *)&ar->arp_pa; 9866 9867 /* 9868 * We need to let ARP know on which interface the IP 9869 * address has an ARP mapping. In the IPMP case, a 9870 * simple forwarding table lookup will return the 9871 * IRE_IF_RESOLVER for the first interface in the group, 9872 * which might not be the interface on which the 9873 * requested IP address was resolved due to the ill 9874 * selection algorithm (see ip_newroute_get_dst_ill()). 9875 * So we do a cache table lookup first: if the IRE cache 9876 * entry for the IP address is still there, it will 9877 * contain the ill pointer for the right interface, so 9878 * we use that. If the cache entry has been flushed, we 9879 * fall back to the forwarding table lookup. This should 9880 * be rare enough since IRE cache entries have a longer 9881 * life expectancy than ARP cache entries. 9882 */ 9883 ire = ire_cache_lookup(sin->sin_addr.s_addr, ALL_ZONES, NULL, ipst); 9884 if ((ire == NULL) || (ire->ire_type == IRE_LOOPBACK) || 9885 ((ill = ire_to_ill(ire)) == NULL)) { 9886 if (ire != NULL) 9887 ire_refrele(ire); 9888 ire = ire_ftable_lookup(sin->sin_addr.s_addr, 9889 0, 0, IRE_IF_RESOLVER, NULL, NULL, ALL_ZONES, 0, 9890 NULL, MATCH_IRE_TYPE, ipst); 9891 if ((ire == NULL) || ((ill = ire_to_ill(ire)) == NULL)) { 9892 if (ire != NULL) 9893 ire_refrele(ire); 9894 return (ENXIO); 9895 } 9896 } 9897 ASSERT(ire != NULL && ill != NULL); 9898 9899 err = ip_sioctl_arp_common(ill, q, mp, sin, B_FALSE, B_FALSE); 9900 ire_refrele(ire); 9901 return (err); 9902 } 9903 9904 /* 9905 * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also 9906 * atomically set/clear the muxids. Also complete the ioctl by acking or 9907 * naking it. Note that the code is structured such that the link type, 9908 * whether it's persistent or not, is treated equally. ifconfig(1M) and 9909 * its clones use the persistent link, while pppd(1M) and perhaps many 9910 * other daemons may use non-persistent link. When combined with some 9911 * ill_t states, linking and unlinking lower streams may be used as 9912 * indicators of dynamic re-plumbing events [see PSARC/1999/348]. 9913 */ 9914 /* ARGSUSED */ 9915 void 9916 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg) 9917 { 9918 mblk_t *mp1; 9919 mblk_t *mp2; 9920 struct linkblk *li; 9921 queue_t *ipwq; 9922 char *name; 9923 struct qinit *qinfo; 9924 struct ipmx_s *ipmxp; 9925 ill_t *ill = NULL; 9926 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 9927 int err = 0; 9928 boolean_t entered_ipsq = B_FALSE; 9929 boolean_t islink; 9930 queue_t *dwq = NULL; 9931 ip_stack_t *ipst; 9932 9933 if (CONN_Q(q)) 9934 ipst = CONNQ_TO_IPST(q); 9935 else 9936 ipst = ILLQ_TO_IPST(q); 9937 9938 ASSERT(iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_PUNLINK || 9939 iocp->ioc_cmd == I_LINK || iocp->ioc_cmd == I_UNLINK); 9940 9941 islink = (iocp->ioc_cmd == I_PLINK || iocp->ioc_cmd == I_LINK) ? 9942 B_TRUE : B_FALSE; 9943 9944 mp1 = mp->b_cont; /* This is the linkblk info */ 9945 li = (struct linkblk *)mp1->b_rptr; 9946 9947 /* 9948 * ARP has added this special mblk, and the utility is asking us 9949 * to perform consistency checks, and also atomically set the 9950 * muxid. Ifconfig is an example. It achieves this by using 9951 * /dev/arp as the mux to plink the arp stream, and pushes arp on 9952 * to /dev/udp[6] stream for use as the mux when plinking the IP 9953 * stream. SIOCSLIFMUXID is not required. See ifconfig.c, arp.c 9954 * and other comments in this routine for more details. 9955 */ 9956 mp2 = mp1->b_cont; /* This is added by ARP */ 9957 9958 /* 9959 * If I_{P}LINK/I_{P}UNLINK is issued by a utility other than 9960 * ifconfig which didn't push ARP on top of the dummy mux, we won't 9961 * get the special mblk above. For backward compatibility, we just 9962 * return success. The utility will use SIOCSLIFMUXID to store 9963 * the muxids. This is not atomic, and can leave the streams 9964 * unplumbable if the utility is interrrupted, before it does the 9965 * SIOCSLIFMUXID. 9966 */ 9967 if (mp2 == NULL) { 9968 /* 9969 * At this point we don't know whether or not this is the 9970 * IP module stream or the ARP device stream. We need to 9971 * walk the lower stream in order to find this out, since 9972 * the capability negotiation is done only on the IP module 9973 * stream. IP module instance is identified by the module 9974 * name IP, non-null q_next, and it's wput not being ip_lwput. 9975 * STREAMS ensures that the lower stream (l_qbot) will not 9976 * vanish until this ioctl completes. So we can safely walk 9977 * the stream or refer to the q_ptr. 9978 */ 9979 ipwq = li->l_qbot; 9980 while (ipwq != NULL) { 9981 qinfo = ipwq->q_qinfo; 9982 name = qinfo->qi_minfo->mi_idname; 9983 if (name != NULL && name[0] != NULL && 9984 (strcmp(name, ip_mod_info.mi_idname) == 0) && 9985 ((void *)(qinfo->qi_putp) != (void *)ip_lwput) && 9986 (ipwq->q_next != NULL)) { 9987 break; 9988 } 9989 ipwq = ipwq->q_next; 9990 } 9991 /* 9992 * This looks like an IP module stream, so trigger 9993 * the capability reset or re-negotiation if necessary. 9994 */ 9995 if (ipwq != NULL) { 9996 ill = ipwq->q_ptr; 9997 ASSERT(ill != NULL); 9998 9999 if (ipsq == NULL) { 10000 ipsq = ipsq_try_enter(NULL, ill, q, mp, 10001 ip_sioctl_plink, NEW_OP, B_TRUE); 10002 if (ipsq == NULL) 10003 return; 10004 entered_ipsq = B_TRUE; 10005 } 10006 ASSERT(IAM_WRITER_ILL(ill)); 10007 /* 10008 * Store the upper read queue of the module 10009 * immediately below IP, and count the total 10010 * number of lower modules. Do this only 10011 * for I_PLINK or I_LINK event. 10012 */ 10013 ill->ill_lmod_rq = NULL; 10014 ill->ill_lmod_cnt = 0; 10015 if (islink && (dwq = ipwq->q_next) != NULL) { 10016 ill->ill_lmod_rq = RD(dwq); 10017 10018 while (dwq != NULL) { 10019 ill->ill_lmod_cnt++; 10020 dwq = dwq->q_next; 10021 } 10022 } 10023 /* 10024 * There's no point in resetting or re-negotiating if 10025 * we are not bound to the driver, so only do this if 10026 * the DLPI state is idle (up); we assume such state 10027 * since ill_ipif_up_count gets incremented in 10028 * ipif_up_done(), which is after we are bound to the 10029 * driver. Note that in the case of logical 10030 * interfaces, IP won't rebind to the driver unless 10031 * the ill_ipif_up_count is 0, meaning that all other 10032 * IP interfaces (including the main ipif) are in the 10033 * down state. Because of this, we use such counter 10034 * as an indicator, instead of relying on the IPIF_UP 10035 * flag, which is per ipif instance. 10036 */ 10037 if (ill->ill_ipif_up_count > 0) { 10038 if (islink) 10039 ill_capability_probe(ill); 10040 else 10041 ill_capability_reset(ill); 10042 } 10043 } 10044 goto done; 10045 } 10046 10047 /* 10048 * This is an I_{P}LINK sent down by ifconfig on 10049 * /dev/arp. ARP has appended this last (3rd) mblk, 10050 * giving more info. STREAMS ensures that the lower 10051 * stream (l_qbot) will not vanish until this ioctl 10052 * completes. So we can safely walk the stream or refer 10053 * to the q_ptr. 10054 */ 10055 ipmxp = (struct ipmx_s *)mp2->b_rptr; 10056 if (ipmxp->ipmx_arpdev_stream) { 10057 /* 10058 * The operation is occuring on the arp-device 10059 * stream. 10060 */ 10061 ill = ill_lookup_on_name(ipmxp->ipmx_name, B_FALSE, B_FALSE, 10062 q, mp, ip_sioctl_plink, &err, NULL, ipst); 10063 if (ill == NULL) { 10064 if (err == EINPROGRESS) { 10065 return; 10066 } else { 10067 err = EINVAL; 10068 goto done; 10069 } 10070 } 10071 10072 if (ipsq == NULL) { 10073 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink, 10074 NEW_OP, B_TRUE); 10075 if (ipsq == NULL) { 10076 ill_refrele(ill); 10077 return; 10078 } 10079 entered_ipsq = B_TRUE; 10080 } 10081 ASSERT(IAM_WRITER_ILL(ill)); 10082 ill_refrele(ill); 10083 /* 10084 * To ensure consistency between IP and ARP, 10085 * the following LIFO scheme is used in 10086 * plink/punlink. (IP first, ARP last). 10087 * This is because the muxid's are stored 10088 * in the IP stream on the ill. 10089 * 10090 * I_{P}LINK: ifconfig plinks the IP stream before 10091 * plinking the ARP stream. On an arp-dev 10092 * stream, IP checks that it is not yet 10093 * plinked, and it also checks that the 10094 * corresponding IP stream is already plinked. 10095 * 10096 * I_{P}UNLINK: ifconfig punlinks the ARP stream 10097 * before punlinking the IP stream. IP does 10098 * not allow punlink of the IP stream unless 10099 * the arp stream has been punlinked. 10100 * 10101 */ 10102 if ((islink && 10103 (ill->ill_arp_muxid != 0 || ill->ill_ip_muxid == 0)) || 10104 (!islink && 10105 ill->ill_arp_muxid != li->l_index)) { 10106 err = EINVAL; 10107 goto done; 10108 } 10109 if (islink) { 10110 ill->ill_arp_muxid = li->l_index; 10111 } else { 10112 ill->ill_arp_muxid = 0; 10113 } 10114 } else { 10115 /* 10116 * This must be the IP module stream with or 10117 * without arp. Walk the stream and locate the 10118 * IP module. An IP module instance is 10119 * identified by the module name IP, non-null 10120 * q_next, and it's wput not being ip_lwput. 10121 */ 10122 ipwq = li->l_qbot; 10123 while (ipwq != NULL) { 10124 qinfo = ipwq->q_qinfo; 10125 name = qinfo->qi_minfo->mi_idname; 10126 if (name != NULL && name[0] != NULL && 10127 (strcmp(name, ip_mod_info.mi_idname) == 0) && 10128 ((void *)(qinfo->qi_putp) != (void *)ip_lwput) && 10129 (ipwq->q_next != NULL)) { 10130 break; 10131 } 10132 ipwq = ipwq->q_next; 10133 } 10134 if (ipwq != NULL) { 10135 ill = ipwq->q_ptr; 10136 ASSERT(ill != NULL); 10137 10138 if (ipsq == NULL) { 10139 ipsq = ipsq_try_enter(NULL, ill, q, mp, 10140 ip_sioctl_plink, NEW_OP, B_TRUE); 10141 if (ipsq == NULL) 10142 return; 10143 entered_ipsq = B_TRUE; 10144 } 10145 ASSERT(IAM_WRITER_ILL(ill)); 10146 /* 10147 * Return error if the ip_mux_id is 10148 * non-zero and command is I_{P}LINK. 10149 * If command is I_{P}UNLINK, return 10150 * error if the arp-devstr is not 10151 * yet punlinked. 10152 */ 10153 if ((islink && ill->ill_ip_muxid != 0) || 10154 (!islink && ill->ill_arp_muxid != 0)) { 10155 err = EINVAL; 10156 goto done; 10157 } 10158 ill->ill_lmod_rq = NULL; 10159 ill->ill_lmod_cnt = 0; 10160 if (islink) { 10161 /* 10162 * Store the upper read queue of the module 10163 * immediately below IP, and count the total 10164 * number of lower modules. 10165 */ 10166 if ((dwq = ipwq->q_next) != NULL) { 10167 ill->ill_lmod_rq = RD(dwq); 10168 10169 while (dwq != NULL) { 10170 ill->ill_lmod_cnt++; 10171 dwq = dwq->q_next; 10172 } 10173 } 10174 ill->ill_ip_muxid = li->l_index; 10175 } else { 10176 ill->ill_ip_muxid = 0; 10177 } 10178 10179 /* 10180 * See comments above about resetting/re- 10181 * negotiating driver sub-capabilities. 10182 */ 10183 if (ill->ill_ipif_up_count > 0) { 10184 if (islink) 10185 ill_capability_probe(ill); 10186 else 10187 ill_capability_reset(ill); 10188 } 10189 } 10190 } 10191 done: 10192 iocp->ioc_count = 0; 10193 iocp->ioc_error = err; 10194 if (err == 0) 10195 mp->b_datap->db_type = M_IOCACK; 10196 else 10197 mp->b_datap->db_type = M_IOCNAK; 10198 qreply(q, mp); 10199 10200 /* Conn was refheld in ip_sioctl_copyin_setup */ 10201 if (CONN_Q(q)) 10202 CONN_OPER_PENDING_DONE(Q_TO_CONN(q)); 10203 if (entered_ipsq) 10204 ipsq_exit(ipsq, B_TRUE, B_TRUE); 10205 } 10206 10207 /* 10208 * Search the ioctl command in the ioctl tables and return a pointer 10209 * to the ioctl command information. The ioctl command tables are 10210 * static and fully populated at compile time. 10211 */ 10212 ip_ioctl_cmd_t * 10213 ip_sioctl_lookup(int ioc_cmd) 10214 { 10215 int index; 10216 ip_ioctl_cmd_t *ipip; 10217 ip_ioctl_cmd_t *ipip_end; 10218 10219 if (ioc_cmd == IPI_DONTCARE) 10220 return (NULL); 10221 10222 /* 10223 * Do a 2 step search. First search the indexed table 10224 * based on the least significant byte of the ioctl cmd. 10225 * If we don't find a match, then search the misc table 10226 * serially. 10227 */ 10228 index = ioc_cmd & 0xFF; 10229 if (index < ip_ndx_ioctl_count) { 10230 ipip = &ip_ndx_ioctl_table[index]; 10231 if (ipip->ipi_cmd == ioc_cmd) { 10232 /* Found a match in the ndx table */ 10233 return (ipip); 10234 } 10235 } 10236 10237 /* Search the misc table */ 10238 ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count]; 10239 for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) { 10240 if (ipip->ipi_cmd == ioc_cmd) 10241 /* Found a match in the misc table */ 10242 return (ipip); 10243 } 10244 10245 return (NULL); 10246 } 10247 10248 /* 10249 * Wrapper function for resuming deferred ioctl processing 10250 * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER, 10251 * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently. 10252 */ 10253 /* ARGSUSED */ 10254 void 10255 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp, 10256 void *dummy_arg) 10257 { 10258 ip_sioctl_copyin_setup(q, mp); 10259 } 10260 10261 /* 10262 * ip_sioctl_copyin_setup is called by ip_wput with any M_IOCTL message 10263 * that arrives. Most of the IOCTLs are "socket" IOCTLs which we handle 10264 * in either I_STR or TRANSPARENT form, using the mi_copy facility. 10265 * We establish here the size of the block to be copied in. mi_copyin 10266 * arranges for this to happen, an processing continues in ip_wput with 10267 * an M_IOCDATA message. 10268 */ 10269 void 10270 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp) 10271 { 10272 int copyin_size; 10273 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 10274 ip_ioctl_cmd_t *ipip; 10275 cred_t *cr; 10276 ip_stack_t *ipst; 10277 10278 if (CONN_Q(q)) 10279 ipst = CONNQ_TO_IPST(q); 10280 else 10281 ipst = ILLQ_TO_IPST(q); 10282 10283 ipip = ip_sioctl_lookup(iocp->ioc_cmd); 10284 if (ipip == NULL) { 10285 /* 10286 * The ioctl is not one we understand or own. 10287 * Pass it along to be processed down stream, 10288 * if this is a module instance of IP, else nak 10289 * the ioctl. 10290 */ 10291 if (q->q_next == NULL) { 10292 goto nak; 10293 } else { 10294 putnext(q, mp); 10295 return; 10296 } 10297 } 10298 10299 /* 10300 * If this is deferred, then we will do all the checks when we 10301 * come back. 10302 */ 10303 if ((iocp->ioc_cmd == SIOCGDSTINFO || 10304 iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) { 10305 ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume); 10306 return; 10307 } 10308 10309 /* 10310 * Only allow a very small subset of IP ioctls on this stream if 10311 * IP is a module and not a driver. Allowing ioctls to be processed 10312 * in this case may cause assert failures or data corruption. 10313 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few 10314 * ioctls allowed on an IP module stream, after which this stream 10315 * normally becomes a multiplexor (at which time the stream head 10316 * will fail all ioctls). 10317 */ 10318 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) { 10319 if (ipip->ipi_flags & IPI_PASS_DOWN) { 10320 /* 10321 * Pass common Streams ioctls which the IP 10322 * module does not own or consume along to 10323 * be processed down stream. 10324 */ 10325 putnext(q, mp); 10326 return; 10327 } else { 10328 goto nak; 10329 } 10330 } 10331 10332 /* Make sure we have ioctl data to process. */ 10333 if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT)) 10334 goto nak; 10335 10336 /* 10337 * Prefer dblk credential over ioctl credential; some synthesized 10338 * ioctls have kcred set because there's no way to crhold() 10339 * a credential in some contexts. (ioc_cr is not crfree() by 10340 * the framework; the caller of ioctl needs to hold the reference 10341 * for the duration of the call). 10342 */ 10343 cr = DB_CREDDEF(mp, iocp->ioc_cr); 10344 10345 /* Make sure normal users don't send down privileged ioctls */ 10346 if ((ipip->ipi_flags & IPI_PRIV) && 10347 (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) { 10348 /* We checked the privilege earlier but log it here */ 10349 miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE)); 10350 return; 10351 } 10352 10353 /* 10354 * The ioctl command tables can only encode fixed length 10355 * ioctl data. If the length is variable, the table will 10356 * encode the length as zero. Such special cases are handled 10357 * below in the switch. 10358 */ 10359 if (ipip->ipi_copyin_size != 0) { 10360 mi_copyin(q, mp, NULL, ipip->ipi_copyin_size); 10361 return; 10362 } 10363 10364 switch (iocp->ioc_cmd) { 10365 case O_SIOCGIFCONF: 10366 case SIOCGIFCONF: 10367 /* 10368 * This IOCTL is hilarious. See comments in 10369 * ip_sioctl_get_ifconf for the story. 10370 */ 10371 if (iocp->ioc_count == TRANSPARENT) 10372 copyin_size = SIZEOF_STRUCT(ifconf, 10373 iocp->ioc_flag); 10374 else 10375 copyin_size = iocp->ioc_count; 10376 mi_copyin(q, mp, NULL, copyin_size); 10377 return; 10378 10379 case O_SIOCGLIFCONF: 10380 case SIOCGLIFCONF: 10381 copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag); 10382 mi_copyin(q, mp, NULL, copyin_size); 10383 return; 10384 10385 case SIOCGLIFSRCOF: 10386 copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag); 10387 mi_copyin(q, mp, NULL, copyin_size); 10388 return; 10389 case SIOCGIP6ADDRPOLICY: 10390 ip_sioctl_ip6addrpolicy(q, mp); 10391 ip6_asp_table_refrele(ipst); 10392 return; 10393 10394 case SIOCSIP6ADDRPOLICY: 10395 ip_sioctl_ip6addrpolicy(q, mp); 10396 return; 10397 10398 case SIOCGDSTINFO: 10399 ip_sioctl_dstinfo(q, mp); 10400 ip6_asp_table_refrele(ipst); 10401 return; 10402 10403 case I_PLINK: 10404 case I_PUNLINK: 10405 case I_LINK: 10406 case I_UNLINK: 10407 /* 10408 * We treat non-persistent link similarly as the persistent 10409 * link case, in terms of plumbing/unplumbing, as well as 10410 * dynamic re-plumbing events indicator. See comments 10411 * in ip_sioctl_plink() for more. 10412 * 10413 * Request can be enqueued in the 'ipsq' while waiting 10414 * to become exclusive. So bump up the conn ref. 10415 */ 10416 if (CONN_Q(q)) 10417 CONN_INC_REF(Q_TO_CONN(q)); 10418 ip_sioctl_plink(NULL, q, mp, NULL); 10419 return; 10420 10421 case ND_GET: 10422 case ND_SET: 10423 /* 10424 * Use of the nd table requires holding the reader lock. 10425 * Modifying the nd table thru nd_load/nd_unload requires 10426 * the writer lock. 10427 */ 10428 rw_enter(&ipst->ips_ip_g_nd_lock, RW_READER); 10429 if (nd_getset(q, ipst->ips_ip_g_nd, mp)) { 10430 rw_exit(&ipst->ips_ip_g_nd_lock); 10431 10432 if (iocp->ioc_error) 10433 iocp->ioc_count = 0; 10434 mp->b_datap->db_type = M_IOCACK; 10435 qreply(q, mp); 10436 return; 10437 } 10438 rw_exit(&ipst->ips_ip_g_nd_lock); 10439 /* 10440 * We don't understand this subioctl of ND_GET / ND_SET. 10441 * Maybe intended for some driver / module below us 10442 */ 10443 if (q->q_next) { 10444 putnext(q, mp); 10445 } else { 10446 iocp->ioc_error = ENOENT; 10447 mp->b_datap->db_type = M_IOCNAK; 10448 iocp->ioc_count = 0; 10449 qreply(q, mp); 10450 } 10451 return; 10452 10453 case IP_IOCTL: 10454 ip_wput_ioctl(q, mp); 10455 return; 10456 default: 10457 cmn_err(CE_PANIC, "should not happen "); 10458 } 10459 nak: 10460 if (mp->b_cont != NULL) { 10461 freemsg(mp->b_cont); 10462 mp->b_cont = NULL; 10463 } 10464 iocp->ioc_error = EINVAL; 10465 mp->b_datap->db_type = M_IOCNAK; 10466 iocp->ioc_count = 0; 10467 qreply(q, mp); 10468 } 10469 10470 /* ip_wput hands off ARP IOCTL responses to us */ 10471 void 10472 ip_sioctl_iocack(queue_t *q, mblk_t *mp) 10473 { 10474 struct arpreq *ar; 10475 struct xarpreq *xar; 10476 area_t *area; 10477 mblk_t *area_mp; 10478 struct iocblk *iocp; 10479 mblk_t *orig_ioc_mp, *tmp; 10480 struct iocblk *orig_iocp; 10481 ill_t *ill; 10482 conn_t *connp = NULL; 10483 uint_t ioc_id; 10484 mblk_t *pending_mp; 10485 int x_arp_ioctl = B_FALSE, ifx_arp_ioctl = B_FALSE; 10486 int *flagsp; 10487 char *storage = NULL; 10488 sin_t *sin; 10489 ipaddr_t addr; 10490 int err; 10491 ip_stack_t *ipst; 10492 10493 ill = q->q_ptr; 10494 ASSERT(ill != NULL); 10495 ipst = ill->ill_ipst; 10496 10497 /* 10498 * We should get back from ARP a packet chain that looks like: 10499 * M_IOCACK-->ARP_op_MBLK-->ORIG_M_IOCTL-->MI_COPY_MBLK-->[X]ARPREQ_MBLK 10500 */ 10501 if (!(area_mp = mp->b_cont) || 10502 (area_mp->b_wptr - area_mp->b_rptr) < sizeof (ip_sock_ar_t) || 10503 !(orig_ioc_mp = area_mp->b_cont) || 10504 !orig_ioc_mp->b_cont || !orig_ioc_mp->b_cont->b_cont) { 10505 freemsg(mp); 10506 return; 10507 } 10508 10509 orig_iocp = (struct iocblk *)orig_ioc_mp->b_rptr; 10510 10511 tmp = (orig_ioc_mp->b_cont)->b_cont; 10512 if ((orig_iocp->ioc_cmd == SIOCGXARP) || 10513 (orig_iocp->ioc_cmd == SIOCSXARP) || 10514 (orig_iocp->ioc_cmd == SIOCDXARP)) { 10515 x_arp_ioctl = B_TRUE; 10516 xar = (struct xarpreq *)tmp->b_rptr; 10517 sin = (sin_t *)&xar->xarp_pa; 10518 flagsp = &xar->xarp_flags; 10519 storage = xar->xarp_ha.sdl_data; 10520 if (xar->xarp_ha.sdl_nlen != 0) 10521 ifx_arp_ioctl = B_TRUE; 10522 } else { 10523 ar = (struct arpreq *)tmp->b_rptr; 10524 sin = (sin_t *)&ar->arp_pa; 10525 flagsp = &ar->arp_flags; 10526 storage = ar->arp_ha.sa_data; 10527 } 10528 10529 iocp = (struct iocblk *)mp->b_rptr; 10530 10531 /* 10532 * Pick out the originating queue based on the ioc_id. 10533 */ 10534 ioc_id = iocp->ioc_id; 10535 pending_mp = ill_pending_mp_get(ill, &connp, ioc_id); 10536 if (pending_mp == NULL) { 10537 ASSERT(connp == NULL); 10538 inet_freemsg(mp); 10539 return; 10540 } 10541 ASSERT(connp != NULL); 10542 q = CONNP_TO_WQ(connp); 10543 10544 /* Uncouple the internally generated IOCTL from the original one */ 10545 area = (area_t *)area_mp->b_rptr; 10546 area_mp->b_cont = NULL; 10547 10548 /* 10549 * Restore the b_next and b_prev used by mi code. This is needed 10550 * to complete the ioctl using mi* functions. We stored them in 10551 * the pending mp prior to sending the request to ARP. 10552 */ 10553 orig_ioc_mp->b_cont->b_next = pending_mp->b_cont->b_next; 10554 orig_ioc_mp->b_cont->b_prev = pending_mp->b_cont->b_prev; 10555 inet_freemsg(pending_mp); 10556 10557 /* 10558 * We're done if there was an error or if this is not an SIOCG{X}ARP 10559 * Catch the case where there is an IRE_CACHE by no entry in the 10560 * arp table. 10561 */ 10562 addr = sin->sin_addr.s_addr; 10563 if (iocp->ioc_error && iocp->ioc_cmd == AR_ENTRY_SQUERY) { 10564 ire_t *ire; 10565 dl_unitdata_req_t *dlup; 10566 mblk_t *llmp; 10567 int addr_len; 10568 ill_t *ipsqill = NULL; 10569 10570 if (ifx_arp_ioctl) { 10571 /* 10572 * There's no need to lookup the ill, since 10573 * we've already done that when we started 10574 * processing the ioctl and sent the message 10575 * to ARP on that ill. So use the ill that 10576 * is stored in q->q_ptr. 10577 */ 10578 ipsqill = ill; 10579 ire = ire_ctable_lookup(addr, 0, IRE_CACHE, 10580 ipsqill->ill_ipif, ALL_ZONES, 10581 NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst); 10582 } else { 10583 ire = ire_ctable_lookup(addr, 0, IRE_CACHE, 10584 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 10585 if (ire != NULL) 10586 ipsqill = ire_to_ill(ire); 10587 } 10588 10589 if ((x_arp_ioctl) && (ipsqill != NULL)) 10590 storage += ill_xarp_info(&xar->xarp_ha, ipsqill); 10591 10592 if (ire != NULL) { 10593 /* 10594 * Since the ire obtained from cachetable is used for 10595 * mac addr copying below, treat an incomplete ire as if 10596 * as if we never found it. 10597 */ 10598 if (ire->ire_nce != NULL && 10599 ire->ire_nce->nce_state != ND_REACHABLE) { 10600 ire_refrele(ire); 10601 ire = NULL; 10602 ipsqill = NULL; 10603 goto errack; 10604 } 10605 *flagsp = ATF_INUSE; 10606 llmp = (ire->ire_nce != NULL ? 10607 ire->ire_nce->nce_res_mp : NULL); 10608 if (llmp != NULL && ipsqill != NULL) { 10609 uchar_t *macaddr; 10610 10611 addr_len = ipsqill->ill_phys_addr_length; 10612 if (x_arp_ioctl && ((addr_len + 10613 ipsqill->ill_name_length) > 10614 sizeof (xar->xarp_ha.sdl_data))) { 10615 ire_refrele(ire); 10616 freemsg(mp); 10617 ip_ioctl_finish(q, orig_ioc_mp, 10618 EINVAL, NO_COPYOUT, NULL); 10619 return; 10620 } 10621 *flagsp |= ATF_COM; 10622 dlup = (dl_unitdata_req_t *)llmp->b_rptr; 10623 if (ipsqill->ill_sap_length < 0) 10624 macaddr = llmp->b_rptr + 10625 dlup->dl_dest_addr_offset; 10626 else 10627 macaddr = llmp->b_rptr + 10628 dlup->dl_dest_addr_offset + 10629 ipsqill->ill_sap_length; 10630 /* 10631 * For SIOCGARP, MAC address length 10632 * validation has already been done 10633 * before the ioctl was issued to ARP to 10634 * allow it to progress only on 6 byte 10635 * addressable (ethernet like) media. Thus 10636 * the mac address copying can not overwrite 10637 * the sa_data area below. 10638 */ 10639 bcopy(macaddr, storage, addr_len); 10640 } 10641 /* Ditch the internal IOCTL. */ 10642 freemsg(mp); 10643 ire_refrele(ire); 10644 ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL); 10645 return; 10646 } 10647 } 10648 10649 /* 10650 * Delete the coresponding IRE_CACHE if any. 10651 * Reset the error if there was one (in case there was no entry 10652 * in arp.) 10653 */ 10654 if (iocp->ioc_cmd == AR_ENTRY_DELETE) { 10655 ipif_t *ipintf = NULL; 10656 10657 if (ifx_arp_ioctl) { 10658 /* 10659 * There's no need to lookup the ill, since 10660 * we've already done that when we started 10661 * processing the ioctl and sent the message 10662 * to ARP on that ill. So use the ill that 10663 * is stored in q->q_ptr. 10664 */ 10665 ipintf = ill->ill_ipif; 10666 } 10667 if (ip_ire_clookup_and_delete(addr, ipintf, ipst)) { 10668 /* 10669 * The address in "addr" may be an entry for a 10670 * router. If that's true, then any off-net 10671 * IRE_CACHE entries that go through the router 10672 * with address "addr" must be clobbered. Use 10673 * ire_walk to achieve this goal. 10674 */ 10675 if (ifx_arp_ioctl) 10676 ire_walk_ill_v4(MATCH_IRE_ILL, 0, 10677 ire_delete_cache_gw, (char *)&addr, ill); 10678 else 10679 ire_walk_v4(ire_delete_cache_gw, (char *)&addr, 10680 ALL_ZONES, ipst); 10681 iocp->ioc_error = 0; 10682 } 10683 } 10684 errack: 10685 if (iocp->ioc_error || iocp->ioc_cmd != AR_ENTRY_SQUERY) { 10686 err = iocp->ioc_error; 10687 freemsg(mp); 10688 ip_ioctl_finish(q, orig_ioc_mp, err, NO_COPYOUT, NULL); 10689 return; 10690 } 10691 10692 /* 10693 * Completion of an SIOCG{X}ARP. Translate the information from 10694 * the area_t into the struct {x}arpreq. 10695 */ 10696 if (x_arp_ioctl) { 10697 storage += ill_xarp_info(&xar->xarp_ha, ill); 10698 if ((ill->ill_phys_addr_length + ill->ill_name_length) > 10699 sizeof (xar->xarp_ha.sdl_data)) { 10700 freemsg(mp); 10701 ip_ioctl_finish(q, orig_ioc_mp, EINVAL, NO_COPYOUT, 10702 NULL); 10703 return; 10704 } 10705 } 10706 *flagsp = ATF_INUSE; 10707 if (area->area_flags & ACE_F_PERMANENT) 10708 *flagsp |= ATF_PERM; 10709 if (area->area_flags & ACE_F_PUBLISH) 10710 *flagsp |= ATF_PUBL; 10711 if (area->area_flags & ACE_F_AUTHORITY) 10712 *flagsp |= ATF_AUTHORITY; 10713 if (area->area_hw_addr_length != 0) { 10714 *flagsp |= ATF_COM; 10715 /* 10716 * For SIOCGARP, MAC address length validation has 10717 * already been done before the ioctl was issued to ARP 10718 * to allow it to progress only on 6 byte addressable 10719 * (ethernet like) media. Thus the mac address copying 10720 * can not overwrite the sa_data area below. 10721 */ 10722 bcopy((char *)area + area->area_hw_addr_offset, 10723 storage, area->area_hw_addr_length); 10724 } 10725 10726 /* Ditch the internal IOCTL. */ 10727 freemsg(mp); 10728 /* Complete the original. */ 10729 ip_ioctl_finish(q, orig_ioc_mp, 0, COPYOUT, NULL); 10730 } 10731 10732 /* 10733 * Create a new logical interface. If ipif_id is zero (i.e. not a logical 10734 * interface) create the next available logical interface for this 10735 * physical interface. 10736 * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an 10737 * ipif with the specified name. 10738 * 10739 * If the address family is not AF_UNSPEC then set the address as well. 10740 * 10741 * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout) 10742 * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer. 10743 * 10744 * Executed as a writer on the ill or ill group. 10745 * So no lock is needed to traverse the ipif chain, or examine the 10746 * phyint flags. 10747 */ 10748 /* ARGSUSED */ 10749 int 10750 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 10751 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq) 10752 { 10753 mblk_t *mp1; 10754 struct lifreq *lifr; 10755 boolean_t isv6; 10756 boolean_t exists; 10757 char *name; 10758 char *endp; 10759 char *cp; 10760 int namelen; 10761 ipif_t *ipif; 10762 long id; 10763 ipsq_t *ipsq; 10764 ill_t *ill; 10765 sin_t *sin; 10766 int err = 0; 10767 boolean_t found_sep = B_FALSE; 10768 conn_t *connp; 10769 zoneid_t zoneid; 10770 int orig_ifindex = 0; 10771 ip_stack_t *ipst = CONNQ_TO_IPST(q); 10772 10773 ASSERT(q->q_next == NULL); 10774 ip1dbg(("ip_sioctl_addif\n")); 10775 /* Existence of mp1 has been checked in ip_wput_nondata */ 10776 mp1 = mp->b_cont->b_cont; 10777 /* 10778 * Null terminate the string to protect against buffer 10779 * overrun. String was generated by user code and may not 10780 * be trusted. 10781 */ 10782 lifr = (struct lifreq *)mp1->b_rptr; 10783 lifr->lifr_name[LIFNAMSIZ - 1] = '\0'; 10784 name = lifr->lifr_name; 10785 ASSERT(CONN_Q(q)); 10786 connp = Q_TO_CONN(q); 10787 isv6 = connp->conn_af_isv6; 10788 zoneid = connp->conn_zoneid; 10789 namelen = mi_strlen(name); 10790 if (namelen == 0) 10791 return (EINVAL); 10792 10793 exists = B_FALSE; 10794 if ((namelen + 1 == sizeof (ipif_loopback_name)) && 10795 (mi_strcmp(name, ipif_loopback_name) == 0)) { 10796 /* 10797 * Allow creating lo0 using SIOCLIFADDIF. 10798 * can't be any other writer thread. So can pass null below 10799 * for the last 4 args to ipif_lookup_name. 10800 */ 10801 ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE, 10802 &exists, isv6, zoneid, NULL, NULL, NULL, NULL, ipst); 10803 /* Prevent any further action */ 10804 if (ipif == NULL) { 10805 return (ENOBUFS); 10806 } else if (!exists) { 10807 /* We created the ipif now and as writer */ 10808 ipif_refrele(ipif); 10809 return (0); 10810 } else { 10811 ill = ipif->ipif_ill; 10812 ill_refhold(ill); 10813 ipif_refrele(ipif); 10814 } 10815 } else { 10816 /* Look for a colon in the name. */ 10817 endp = &name[namelen]; 10818 for (cp = endp; --cp > name; ) { 10819 if (*cp == IPIF_SEPARATOR_CHAR) { 10820 found_sep = B_TRUE; 10821 /* 10822 * Reject any non-decimal aliases for plumbing 10823 * of logical interfaces. Aliases with leading 10824 * zeroes are also rejected as they introduce 10825 * ambiguity in the naming of the interfaces. 10826 * Comparing with "0" takes care of all such 10827 * cases. 10828 */ 10829 if ((strncmp("0", cp+1, 1)) == 0) 10830 return (EINVAL); 10831 10832 if (ddi_strtol(cp+1, &endp, 10, &id) != 0 || 10833 id <= 0 || *endp != '\0') { 10834 return (EINVAL); 10835 } 10836 *cp = '\0'; 10837 break; 10838 } 10839 } 10840 ill = ill_lookup_on_name(name, B_FALSE, isv6, 10841 CONNP_TO_WQ(connp), mp, ip_process_ioctl, &err, NULL, ipst); 10842 if (found_sep) 10843 *cp = IPIF_SEPARATOR_CHAR; 10844 if (ill == NULL) 10845 return (err); 10846 } 10847 10848 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP, 10849 B_TRUE); 10850 10851 /* 10852 * Release the refhold due to the lookup, now that we are excl 10853 * or we are just returning 10854 */ 10855 ill_refrele(ill); 10856 10857 if (ipsq == NULL) 10858 return (EINPROGRESS); 10859 10860 /* 10861 * If the interface is failed, inactive or offlined, look for a working 10862 * interface in the ill group and create the ipif there. If we can't 10863 * find a good interface, create the ipif anyway so that in.mpathd can 10864 * move it to the first repaired interface. 10865 */ 10866 if ((ill->ill_phyint->phyint_flags & 10867 (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) && 10868 ill->ill_phyint->phyint_groupname_len != 0) { 10869 phyint_t *phyi; 10870 char *groupname = ill->ill_phyint->phyint_groupname; 10871 10872 /* 10873 * We're looking for a working interface, but it doesn't matter 10874 * if it's up or down; so instead of following the group lists, 10875 * we look at each physical interface and compare the groupname. 10876 * We're only interested in interfaces with IPv4 (resp. IPv6) 10877 * plumbed when we're adding an IPv4 (resp. IPv6) ipif. 10878 * Otherwise we create the ipif on the failed interface. 10879 */ 10880 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 10881 phyi = avl_first(&ipst->ips_phyint_g_list-> 10882 phyint_list_avl_by_index); 10883 for (; phyi != NULL; 10884 phyi = avl_walk(&ipst->ips_phyint_g_list-> 10885 phyint_list_avl_by_index, 10886 phyi, AVL_AFTER)) { 10887 if (phyi->phyint_groupname_len == 0) 10888 continue; 10889 ASSERT(phyi->phyint_groupname != NULL); 10890 if (mi_strcmp(groupname, phyi->phyint_groupname) == 0 && 10891 !(phyi->phyint_flags & 10892 (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) && 10893 (ill->ill_isv6 ? (phyi->phyint_illv6 != NULL) : 10894 (phyi->phyint_illv4 != NULL))) { 10895 break; 10896 } 10897 } 10898 rw_exit(&ipst->ips_ill_g_lock); 10899 10900 if (phyi != NULL) { 10901 orig_ifindex = ill->ill_phyint->phyint_ifindex; 10902 ill = (ill->ill_isv6 ? phyi->phyint_illv6 : 10903 phyi->phyint_illv4); 10904 } 10905 } 10906 10907 /* 10908 * We are now exclusive on the ipsq, so an ill move will be serialized 10909 * before or after us. 10910 */ 10911 ASSERT(IAM_WRITER_ILL(ill)); 10912 ASSERT(ill->ill_move_in_progress == B_FALSE); 10913 10914 if (found_sep && orig_ifindex == 0) { 10915 /* Now see if there is an IPIF with this unit number. */ 10916 for (ipif = ill->ill_ipif; ipif != NULL; 10917 ipif = ipif->ipif_next) { 10918 if (ipif->ipif_id == id) { 10919 err = EEXIST; 10920 goto done; 10921 } 10922 } 10923 } 10924 10925 /* 10926 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use 10927 * of lo0. We never come here when we plumb lo0:0. It 10928 * happens in ipif_lookup_on_name. 10929 * The specified unit number is ignored when we create the ipif on a 10930 * different interface. However, we save it in ipif_orig_ipifid below so 10931 * that the ipif fails back to the right position. 10932 */ 10933 if ((ipif = ipif_allocate(ill, (found_sep && orig_ifindex == 0) ? 10934 id : -1, IRE_LOCAL, B_TRUE)) == NULL) { 10935 err = ENOBUFS; 10936 goto done; 10937 } 10938 10939 /* Return created name with ioctl */ 10940 (void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name, 10941 IPIF_SEPARATOR_CHAR, ipif->ipif_id); 10942 ip1dbg(("created %s\n", lifr->lifr_name)); 10943 10944 /* Set address */ 10945 sin = (sin_t *)&lifr->lifr_addr; 10946 if (sin->sin_family != AF_UNSPEC) { 10947 err = ip_sioctl_addr(ipif, sin, q, mp, 10948 &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr); 10949 } 10950 10951 /* Set ifindex and unit number for failback */ 10952 if (err == 0 && orig_ifindex != 0) { 10953 ipif->ipif_orig_ifindex = orig_ifindex; 10954 if (found_sep) { 10955 ipif->ipif_orig_ipifid = id; 10956 } 10957 } 10958 10959 done: 10960 ipsq_exit(ipsq, B_TRUE, B_TRUE); 10961 return (err); 10962 } 10963 10964 /* 10965 * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical 10966 * interface) delete it based on the IP address (on this physical interface). 10967 * Otherwise delete it based on the ipif_id. 10968 * Also, special handling to allow a removeif of lo0. 10969 */ 10970 /* ARGSUSED */ 10971 int 10972 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 10973 ip_ioctl_cmd_t *ipip, void *dummy_if_req) 10974 { 10975 conn_t *connp; 10976 ill_t *ill = ipif->ipif_ill; 10977 boolean_t success; 10978 ip_stack_t *ipst; 10979 10980 ipst = CONNQ_TO_IPST(q); 10981 10982 ASSERT(q->q_next == NULL); 10983 ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n", 10984 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 10985 ASSERT(IAM_WRITER_IPIF(ipif)); 10986 10987 connp = Q_TO_CONN(q); 10988 /* 10989 * Special case for unplumbing lo0 (the loopback physical interface). 10990 * If unplumbing lo0, the incoming address structure has been 10991 * initialized to all zeros. When unplumbing lo0, all its logical 10992 * interfaces must be removed too. 10993 * 10994 * Note that this interface may be called to remove a specific 10995 * loopback logical interface (eg, lo0:1). But in that case 10996 * ipif->ipif_id != 0 so that the code path for that case is the 10997 * same as any other interface (meaning it skips the code directly 10998 * below). 10999 */ 11000 if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) { 11001 if (sin->sin_family == AF_UNSPEC && 11002 (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) { 11003 /* 11004 * Mark it condemned. No new ref. will be made to ill. 11005 */ 11006 mutex_enter(&ill->ill_lock); 11007 ill->ill_state_flags |= ILL_CONDEMNED; 11008 for (ipif = ill->ill_ipif; ipif != NULL; 11009 ipif = ipif->ipif_next) { 11010 ipif->ipif_state_flags |= IPIF_CONDEMNED; 11011 } 11012 mutex_exit(&ill->ill_lock); 11013 11014 ipif = ill->ill_ipif; 11015 /* unplumb the loopback interface */ 11016 ill_delete(ill); 11017 mutex_enter(&connp->conn_lock); 11018 mutex_enter(&ill->ill_lock); 11019 ASSERT(ill->ill_group == NULL); 11020 11021 /* Are any references to this ill active */ 11022 if (ill_is_quiescent(ill)) { 11023 mutex_exit(&ill->ill_lock); 11024 mutex_exit(&connp->conn_lock); 11025 ill_delete_tail(ill); 11026 mi_free(ill); 11027 return (0); 11028 } 11029 success = ipsq_pending_mp_add(connp, ipif, 11030 CONNP_TO_WQ(connp), mp, ILL_FREE); 11031 mutex_exit(&connp->conn_lock); 11032 mutex_exit(&ill->ill_lock); 11033 if (success) 11034 return (EINPROGRESS); 11035 else 11036 return (EINTR); 11037 } 11038 } 11039 11040 /* 11041 * We are exclusive on the ipsq, so an ill move will be serialized 11042 * before or after us. 11043 */ 11044 ASSERT(ill->ill_move_in_progress == B_FALSE); 11045 11046 if (ipif->ipif_id == 0) { 11047 /* Find based on address */ 11048 if (ipif->ipif_isv6) { 11049 sin6_t *sin6; 11050 11051 if (sin->sin_family != AF_INET6) 11052 return (EAFNOSUPPORT); 11053 11054 sin6 = (sin6_t *)sin; 11055 /* We are a writer, so we should be able to lookup */ 11056 ipif = ipif_lookup_addr_v6(&sin6->sin6_addr, 11057 ill, ALL_ZONES, NULL, NULL, NULL, NULL, ipst); 11058 if (ipif == NULL) { 11059 /* 11060 * Maybe the address in on another interface in 11061 * the same IPMP group? We check this below. 11062 */ 11063 ipif = ipif_lookup_addr_v6(&sin6->sin6_addr, 11064 NULL, ALL_ZONES, NULL, NULL, NULL, NULL, 11065 ipst); 11066 } 11067 } else { 11068 ipaddr_t addr; 11069 11070 if (sin->sin_family != AF_INET) 11071 return (EAFNOSUPPORT); 11072 11073 addr = sin->sin_addr.s_addr; 11074 /* We are a writer, so we should be able to lookup */ 11075 ipif = ipif_lookup_addr(addr, ill, ALL_ZONES, NULL, 11076 NULL, NULL, NULL, ipst); 11077 if (ipif == NULL) { 11078 /* 11079 * Maybe the address in on another interface in 11080 * the same IPMP group? We check this below. 11081 */ 11082 ipif = ipif_lookup_addr(addr, NULL, ALL_ZONES, 11083 NULL, NULL, NULL, NULL, ipst); 11084 } 11085 } 11086 if (ipif == NULL) { 11087 return (EADDRNOTAVAIL); 11088 } 11089 /* 11090 * When the address to be removed is hosted on a different 11091 * interface, we check if the interface is in the same IPMP 11092 * group as the specified one; if so we proceed with the 11093 * removal. 11094 * ill->ill_group is NULL when the ill is down, so we have to 11095 * compare the group names instead. 11096 */ 11097 if (ipif->ipif_ill != ill && 11098 (ipif->ipif_ill->ill_phyint->phyint_groupname_len == 0 || 11099 ill->ill_phyint->phyint_groupname_len == 0 || 11100 mi_strcmp(ipif->ipif_ill->ill_phyint->phyint_groupname, 11101 ill->ill_phyint->phyint_groupname) != 0)) { 11102 ipif_refrele(ipif); 11103 return (EADDRNOTAVAIL); 11104 } 11105 11106 /* This is a writer */ 11107 ipif_refrele(ipif); 11108 } 11109 11110 /* 11111 * Can not delete instance zero since it is tied to the ill. 11112 */ 11113 if (ipif->ipif_id == 0) 11114 return (EBUSY); 11115 11116 mutex_enter(&ill->ill_lock); 11117 ipif->ipif_state_flags |= IPIF_CONDEMNED; 11118 mutex_exit(&ill->ill_lock); 11119 11120 ipif_free(ipif); 11121 11122 mutex_enter(&connp->conn_lock); 11123 mutex_enter(&ill->ill_lock); 11124 11125 /* Are any references to this ipif active */ 11126 if (ipif->ipif_refcnt == 0 && ipif->ipif_ire_cnt == 0) { 11127 mutex_exit(&ill->ill_lock); 11128 mutex_exit(&connp->conn_lock); 11129 ipif_non_duplicate(ipif); 11130 ipif_down_tail(ipif); 11131 ipif_free_tail(ipif); 11132 return (0); 11133 } 11134 success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp, 11135 IPIF_FREE); 11136 mutex_exit(&ill->ill_lock); 11137 mutex_exit(&connp->conn_lock); 11138 if (success) 11139 return (EINPROGRESS); 11140 else 11141 return (EINTR); 11142 } 11143 11144 /* 11145 * Restart the removeif ioctl. The refcnt has gone down to 0. 11146 * The ipif is already condemned. So can't find it thru lookups. 11147 */ 11148 /* ARGSUSED */ 11149 int 11150 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, 11151 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req) 11152 { 11153 ill_t *ill; 11154 11155 ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n", 11156 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11157 if (ipif->ipif_id == 0 && ipif->ipif_net_type == IRE_LOOPBACK) { 11158 ill = ipif->ipif_ill; 11159 ASSERT(IAM_WRITER_ILL(ill)); 11160 ASSERT((ipif->ipif_state_flags & IPIF_CONDEMNED) && 11161 (ill->ill_state_flags & IPIF_CONDEMNED)); 11162 ill_delete_tail(ill); 11163 mi_free(ill); 11164 return (0); 11165 } 11166 11167 ill = ipif->ipif_ill; 11168 ASSERT(IAM_WRITER_IPIF(ipif)); 11169 ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED); 11170 11171 ipif_non_duplicate(ipif); 11172 ipif_down_tail(ipif); 11173 ipif_free_tail(ipif); 11174 11175 ILL_UNMARK_CHANGING(ill); 11176 return (0); 11177 } 11178 11179 /* 11180 * Set the local interface address. 11181 * Allow an address of all zero when the interface is down. 11182 */ 11183 /* ARGSUSED */ 11184 int 11185 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11186 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq) 11187 { 11188 int err = 0; 11189 in6_addr_t v6addr; 11190 boolean_t need_up = B_FALSE; 11191 11192 ip1dbg(("ip_sioctl_addr(%s:%u %p)\n", 11193 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11194 11195 ASSERT(IAM_WRITER_IPIF(ipif)); 11196 11197 if (ipif->ipif_isv6) { 11198 sin6_t *sin6; 11199 ill_t *ill; 11200 phyint_t *phyi; 11201 11202 if (sin->sin_family != AF_INET6) 11203 return (EAFNOSUPPORT); 11204 11205 sin6 = (sin6_t *)sin; 11206 v6addr = sin6->sin6_addr; 11207 ill = ipif->ipif_ill; 11208 phyi = ill->ill_phyint; 11209 11210 /* 11211 * Enforce that true multicast interfaces have a link-local 11212 * address for logical unit 0. 11213 */ 11214 if (ipif->ipif_id == 0 && 11215 (ill->ill_flags & ILLF_MULTICAST) && 11216 !(ipif->ipif_flags & (IPIF_POINTOPOINT)) && 11217 !(phyi->phyint_flags & (PHYI_LOOPBACK)) && 11218 !IN6_IS_ADDR_LINKLOCAL(&v6addr)) { 11219 return (EADDRNOTAVAIL); 11220 } 11221 11222 /* 11223 * up interfaces shouldn't have the unspecified address 11224 * unless they also have the IPIF_NOLOCAL flags set and 11225 * have a subnet assigned. 11226 */ 11227 if ((ipif->ipif_flags & IPIF_UP) && 11228 IN6_IS_ADDR_UNSPECIFIED(&v6addr) && 11229 (!(ipif->ipif_flags & IPIF_NOLOCAL) || 11230 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) { 11231 return (EADDRNOTAVAIL); 11232 } 11233 11234 if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask)) 11235 return (EADDRNOTAVAIL); 11236 } else { 11237 ipaddr_t addr; 11238 11239 if (sin->sin_family != AF_INET) 11240 return (EAFNOSUPPORT); 11241 11242 addr = sin->sin_addr.s_addr; 11243 11244 /* Allow 0 as the local address. */ 11245 if (addr != 0 && !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) 11246 return (EADDRNOTAVAIL); 11247 11248 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 11249 } 11250 11251 11252 /* 11253 * Even if there is no change we redo things just to rerun 11254 * ipif_set_default. 11255 */ 11256 if (ipif->ipif_flags & IPIF_UP) { 11257 /* 11258 * Setting a new local address, make sure 11259 * we have net and subnet bcast ire's for 11260 * the old address if we need them. 11261 */ 11262 if (!ipif->ipif_isv6) 11263 ipif_check_bcast_ires(ipif); 11264 /* 11265 * If the interface is already marked up, 11266 * we call ipif_down which will take care 11267 * of ditching any IREs that have been set 11268 * up based on the old interface address. 11269 */ 11270 err = ipif_logical_down(ipif, q, mp); 11271 if (err == EINPROGRESS) 11272 return (err); 11273 ipif_down_tail(ipif); 11274 need_up = 1; 11275 } 11276 11277 err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up); 11278 return (err); 11279 } 11280 11281 int 11282 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11283 boolean_t need_up) 11284 { 11285 in6_addr_t v6addr; 11286 in6_addr_t ov6addr; 11287 ipaddr_t addr; 11288 sin6_t *sin6; 11289 int sinlen; 11290 int err = 0; 11291 ill_t *ill = ipif->ipif_ill; 11292 boolean_t need_dl_down; 11293 boolean_t need_arp_down; 11294 struct iocblk *iocp; 11295 11296 iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL; 11297 11298 ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n", 11299 ill->ill_name, ipif->ipif_id, (void *)ipif)); 11300 ASSERT(IAM_WRITER_IPIF(ipif)); 11301 11302 /* Must cancel any pending timer before taking the ill_lock */ 11303 if (ipif->ipif_recovery_id != 0) 11304 (void) untimeout(ipif->ipif_recovery_id); 11305 ipif->ipif_recovery_id = 0; 11306 11307 if (ipif->ipif_isv6) { 11308 sin6 = (sin6_t *)sin; 11309 v6addr = sin6->sin6_addr; 11310 sinlen = sizeof (struct sockaddr_in6); 11311 } else { 11312 addr = sin->sin_addr.s_addr; 11313 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 11314 sinlen = sizeof (struct sockaddr_in); 11315 } 11316 mutex_enter(&ill->ill_lock); 11317 ov6addr = ipif->ipif_v6lcl_addr; 11318 ipif->ipif_v6lcl_addr = v6addr; 11319 sctp_update_ipif_addr(ipif, ov6addr); 11320 if (ipif->ipif_flags & (IPIF_ANYCAST | IPIF_NOLOCAL)) { 11321 ipif->ipif_v6src_addr = ipv6_all_zeros; 11322 } else { 11323 ipif->ipif_v6src_addr = v6addr; 11324 } 11325 ipif->ipif_addr_ready = 0; 11326 11327 /* 11328 * If the interface was previously marked as a duplicate, then since 11329 * we've now got a "new" address, it should no longer be considered a 11330 * duplicate -- even if the "new" address is the same as the old one. 11331 * Note that if all ipifs are down, we may have a pending ARP down 11332 * event to handle. This is because we want to recover from duplicates 11333 * and thus delay tearing down ARP until the duplicates have been 11334 * removed or disabled. 11335 */ 11336 need_dl_down = need_arp_down = B_FALSE; 11337 if (ipif->ipif_flags & IPIF_DUPLICATE) { 11338 need_arp_down = !need_up; 11339 ipif->ipif_flags &= ~IPIF_DUPLICATE; 11340 if (--ill->ill_ipif_dup_count == 0 && !need_up && 11341 ill->ill_ipif_up_count == 0 && ill->ill_dl_up) { 11342 need_dl_down = B_TRUE; 11343 } 11344 } 11345 11346 if (ipif->ipif_isv6 && IN6_IS_ADDR_6TO4(&v6addr) && 11347 !ill->ill_is_6to4tun) { 11348 queue_t *wqp = ill->ill_wq; 11349 11350 /* 11351 * The local address of this interface is a 6to4 address, 11352 * check if this interface is in fact a 6to4 tunnel or just 11353 * an interface configured with a 6to4 address. We are only 11354 * interested in the former. 11355 */ 11356 if (wqp != NULL) { 11357 while ((wqp->q_next != NULL) && 11358 (wqp->q_next->q_qinfo != NULL) && 11359 (wqp->q_next->q_qinfo->qi_minfo != NULL)) { 11360 11361 if (wqp->q_next->q_qinfo->qi_minfo->mi_idnum 11362 == TUN6TO4_MODID) { 11363 /* set for use in IP */ 11364 ill->ill_is_6to4tun = 1; 11365 break; 11366 } 11367 wqp = wqp->q_next; 11368 } 11369 } 11370 } 11371 11372 ipif_set_default(ipif); 11373 11374 /* 11375 * When publishing an interface address change event, we only notify 11376 * the event listeners of the new address. It is assumed that if they 11377 * actively care about the addresses assigned that they will have 11378 * already discovered the previous address assigned (if there was one.) 11379 * 11380 * Don't attach nic event message for SIOCLIFADDIF ioctl. 11381 */ 11382 if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) { 11383 hook_nic_event_t *info; 11384 if ((info = ipif->ipif_ill->ill_nic_event_info) != NULL) { 11385 ip2dbg(("ip_sioctl_addr_tail: unexpected nic event %d " 11386 "attached for %s\n", info->hne_event, 11387 ill->ill_name)); 11388 if (info->hne_data != NULL) 11389 kmem_free(info->hne_data, info->hne_datalen); 11390 kmem_free(info, sizeof (hook_nic_event_t)); 11391 } 11392 11393 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 11394 if (info != NULL) { 11395 ip_stack_t *ipst = ill->ill_ipst; 11396 11397 info->hne_nic = 11398 ipif->ipif_ill->ill_phyint->phyint_hook_ifindex; 11399 info->hne_lif = MAP_IPIF_ID(ipif->ipif_id); 11400 info->hne_event = NE_ADDRESS_CHANGE; 11401 info->hne_family = ipif->ipif_isv6 ? 11402 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 11403 info->hne_data = kmem_alloc(sinlen, KM_NOSLEEP); 11404 if (info->hne_data != NULL) { 11405 info->hne_datalen = sinlen; 11406 bcopy(sin, info->hne_data, sinlen); 11407 } else { 11408 ip2dbg(("ip_sioctl_addr_tail: could not attach " 11409 "address information for ADDRESS_CHANGE nic" 11410 " event of %s (ENOMEM)\n", 11411 ipif->ipif_ill->ill_name)); 11412 kmem_free(info, sizeof (hook_nic_event_t)); 11413 } 11414 } else 11415 ip2dbg(("ip_sioctl_addr_tail: could not attach " 11416 "ADDRESS_CHANGE nic event information for %s " 11417 "(ENOMEM)\n", ipif->ipif_ill->ill_name)); 11418 11419 ipif->ipif_ill->ill_nic_event_info = info; 11420 } 11421 11422 mutex_exit(&ill->ill_lock); 11423 11424 if (need_up) { 11425 /* 11426 * Now bring the interface back up. If this 11427 * is the only IPIF for the ILL, ipif_up 11428 * will have to re-bind to the device, so 11429 * we may get back EINPROGRESS, in which 11430 * case, this IOCTL will get completed in 11431 * ip_rput_dlpi when we see the DL_BIND_ACK. 11432 */ 11433 err = ipif_up(ipif, q, mp); 11434 } 11435 11436 if (need_dl_down) 11437 ill_dl_down(ill); 11438 if (need_arp_down) 11439 ipif_arp_down(ipif); 11440 11441 return (err); 11442 } 11443 11444 11445 /* 11446 * Restart entry point to restart the address set operation after the 11447 * refcounts have dropped to zero. 11448 */ 11449 /* ARGSUSED */ 11450 int 11451 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11452 ip_ioctl_cmd_t *ipip, void *ifreq) 11453 { 11454 ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n", 11455 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11456 ASSERT(IAM_WRITER_IPIF(ipif)); 11457 ipif_down_tail(ipif); 11458 return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE)); 11459 } 11460 11461 /* ARGSUSED */ 11462 int 11463 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11464 ip_ioctl_cmd_t *ipip, void *if_req) 11465 { 11466 sin6_t *sin6 = (struct sockaddr_in6 *)sin; 11467 struct lifreq *lifr = (struct lifreq *)if_req; 11468 11469 ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n", 11470 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11471 /* 11472 * The net mask and address can't change since we have a 11473 * reference to the ipif. So no lock is necessary. 11474 */ 11475 if (ipif->ipif_isv6) { 11476 *sin6 = sin6_null; 11477 sin6->sin6_family = AF_INET6; 11478 sin6->sin6_addr = ipif->ipif_v6lcl_addr; 11479 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 11480 lifr->lifr_addrlen = 11481 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 11482 } else { 11483 *sin = sin_null; 11484 sin->sin_family = AF_INET; 11485 sin->sin_addr.s_addr = ipif->ipif_lcl_addr; 11486 if (ipip->ipi_cmd_type == LIF_CMD) { 11487 lifr->lifr_addrlen = 11488 ip_mask_to_plen(ipif->ipif_net_mask); 11489 } 11490 } 11491 return (0); 11492 } 11493 11494 /* 11495 * Set the destination address for a pt-pt interface. 11496 */ 11497 /* ARGSUSED */ 11498 int 11499 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11500 ip_ioctl_cmd_t *ipip, void *if_req) 11501 { 11502 int err = 0; 11503 in6_addr_t v6addr; 11504 boolean_t need_up = B_FALSE; 11505 11506 ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n", 11507 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11508 ASSERT(IAM_WRITER_IPIF(ipif)); 11509 11510 if (ipif->ipif_isv6) { 11511 sin6_t *sin6; 11512 11513 if (sin->sin_family != AF_INET6) 11514 return (EAFNOSUPPORT); 11515 11516 sin6 = (sin6_t *)sin; 11517 v6addr = sin6->sin6_addr; 11518 11519 if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask)) 11520 return (EADDRNOTAVAIL); 11521 } else { 11522 ipaddr_t addr; 11523 11524 if (sin->sin_family != AF_INET) 11525 return (EAFNOSUPPORT); 11526 11527 addr = sin->sin_addr.s_addr; 11528 if (!ip_addr_ok_v4(addr, ipif->ipif_net_mask)) 11529 return (EADDRNOTAVAIL); 11530 11531 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 11532 } 11533 11534 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr)) 11535 return (0); /* No change */ 11536 11537 if (ipif->ipif_flags & IPIF_UP) { 11538 /* 11539 * If the interface is already marked up, 11540 * we call ipif_down which will take care 11541 * of ditching any IREs that have been set 11542 * up based on the old pp dst address. 11543 */ 11544 err = ipif_logical_down(ipif, q, mp); 11545 if (err == EINPROGRESS) 11546 return (err); 11547 ipif_down_tail(ipif); 11548 need_up = B_TRUE; 11549 } 11550 /* 11551 * could return EINPROGRESS. If so ioctl will complete in 11552 * ip_rput_dlpi_writer 11553 */ 11554 err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up); 11555 return (err); 11556 } 11557 11558 static int 11559 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11560 boolean_t need_up) 11561 { 11562 in6_addr_t v6addr; 11563 ill_t *ill = ipif->ipif_ill; 11564 int err = 0; 11565 boolean_t need_dl_down; 11566 boolean_t need_arp_down; 11567 11568 ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name, 11569 ipif->ipif_id, (void *)ipif)); 11570 11571 /* Must cancel any pending timer before taking the ill_lock */ 11572 if (ipif->ipif_recovery_id != 0) 11573 (void) untimeout(ipif->ipif_recovery_id); 11574 ipif->ipif_recovery_id = 0; 11575 11576 if (ipif->ipif_isv6) { 11577 sin6_t *sin6; 11578 11579 sin6 = (sin6_t *)sin; 11580 v6addr = sin6->sin6_addr; 11581 } else { 11582 ipaddr_t addr; 11583 11584 addr = sin->sin_addr.s_addr; 11585 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 11586 } 11587 mutex_enter(&ill->ill_lock); 11588 /* Set point to point destination address. */ 11589 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) { 11590 /* 11591 * Allow this as a means of creating logical 11592 * pt-pt interfaces on top of e.g. an Ethernet. 11593 * XXX Undocumented HACK for testing. 11594 * pt-pt interfaces are created with NUD disabled. 11595 */ 11596 ipif->ipif_flags |= IPIF_POINTOPOINT; 11597 ipif->ipif_flags &= ~IPIF_BROADCAST; 11598 if (ipif->ipif_isv6) 11599 ill->ill_flags |= ILLF_NONUD; 11600 } 11601 11602 /* 11603 * If the interface was previously marked as a duplicate, then since 11604 * we've now got a "new" address, it should no longer be considered a 11605 * duplicate -- even if the "new" address is the same as the old one. 11606 * Note that if all ipifs are down, we may have a pending ARP down 11607 * event to handle. 11608 */ 11609 need_dl_down = need_arp_down = B_FALSE; 11610 if (ipif->ipif_flags & IPIF_DUPLICATE) { 11611 need_arp_down = !need_up; 11612 ipif->ipif_flags &= ~IPIF_DUPLICATE; 11613 if (--ill->ill_ipif_dup_count == 0 && !need_up && 11614 ill->ill_ipif_up_count == 0 && ill->ill_dl_up) { 11615 need_dl_down = B_TRUE; 11616 } 11617 } 11618 11619 /* Set the new address. */ 11620 ipif->ipif_v6pp_dst_addr = v6addr; 11621 /* Make sure subnet tracks pp_dst */ 11622 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr; 11623 mutex_exit(&ill->ill_lock); 11624 11625 if (need_up) { 11626 /* 11627 * Now bring the interface back up. If this 11628 * is the only IPIF for the ILL, ipif_up 11629 * will have to re-bind to the device, so 11630 * we may get back EINPROGRESS, in which 11631 * case, this IOCTL will get completed in 11632 * ip_rput_dlpi when we see the DL_BIND_ACK. 11633 */ 11634 err = ipif_up(ipif, q, mp); 11635 } 11636 11637 if (need_dl_down) 11638 ill_dl_down(ill); 11639 11640 if (need_arp_down) 11641 ipif_arp_down(ipif); 11642 return (err); 11643 } 11644 11645 /* 11646 * Restart entry point to restart the dstaddress set operation after the 11647 * refcounts have dropped to zero. 11648 */ 11649 /* ARGSUSED */ 11650 int 11651 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11652 ip_ioctl_cmd_t *ipip, void *ifreq) 11653 { 11654 ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n", 11655 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11656 ipif_down_tail(ipif); 11657 return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE)); 11658 } 11659 11660 /* ARGSUSED */ 11661 int 11662 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11663 ip_ioctl_cmd_t *ipip, void *if_req) 11664 { 11665 sin6_t *sin6 = (struct sockaddr_in6 *)sin; 11666 11667 ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n", 11668 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11669 /* 11670 * Get point to point destination address. The addresses can't 11671 * change since we hold a reference to the ipif. 11672 */ 11673 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) 11674 return (EADDRNOTAVAIL); 11675 11676 if (ipif->ipif_isv6) { 11677 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 11678 *sin6 = sin6_null; 11679 sin6->sin6_family = AF_INET6; 11680 sin6->sin6_addr = ipif->ipif_v6pp_dst_addr; 11681 } else { 11682 *sin = sin_null; 11683 sin->sin_family = AF_INET; 11684 sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr; 11685 } 11686 return (0); 11687 } 11688 11689 /* 11690 * part of ipmp, make this func return the active/inactive state and 11691 * caller can set once atomically instead of multiple mutex_enter/mutex_exit 11692 */ 11693 /* 11694 * This function either sets or clears the IFF_INACTIVE flag. 11695 * 11696 * As long as there are some addresses or multicast memberships on the 11697 * IPv4 or IPv6 interface of the "phyi" that does not belong in here, we 11698 * will consider it to be ACTIVE (clear IFF_INACTIVE) i.e the interface 11699 * will be used for outbound packets. 11700 * 11701 * Caller needs to verify the validity of setting IFF_INACTIVE. 11702 */ 11703 static void 11704 phyint_inactive(phyint_t *phyi) 11705 { 11706 ill_t *ill_v4; 11707 ill_t *ill_v6; 11708 ipif_t *ipif; 11709 ilm_t *ilm; 11710 11711 ill_v4 = phyi->phyint_illv4; 11712 ill_v6 = phyi->phyint_illv6; 11713 11714 /* 11715 * No need for a lock while traversing the list since iam 11716 * a writer 11717 */ 11718 if (ill_v4 != NULL) { 11719 ASSERT(IAM_WRITER_ILL(ill_v4)); 11720 for (ipif = ill_v4->ill_ipif; ipif != NULL; 11721 ipif = ipif->ipif_next) { 11722 if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) { 11723 mutex_enter(&phyi->phyint_lock); 11724 phyi->phyint_flags &= ~PHYI_INACTIVE; 11725 mutex_exit(&phyi->phyint_lock); 11726 return; 11727 } 11728 } 11729 for (ilm = ill_v4->ill_ilm; ilm != NULL; 11730 ilm = ilm->ilm_next) { 11731 if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) { 11732 mutex_enter(&phyi->phyint_lock); 11733 phyi->phyint_flags &= ~PHYI_INACTIVE; 11734 mutex_exit(&phyi->phyint_lock); 11735 return; 11736 } 11737 } 11738 } 11739 if (ill_v6 != NULL) { 11740 ill_v6 = phyi->phyint_illv6; 11741 for (ipif = ill_v6->ill_ipif; ipif != NULL; 11742 ipif = ipif->ipif_next) { 11743 if (ipif->ipif_orig_ifindex != phyi->phyint_ifindex) { 11744 mutex_enter(&phyi->phyint_lock); 11745 phyi->phyint_flags &= ~PHYI_INACTIVE; 11746 mutex_exit(&phyi->phyint_lock); 11747 return; 11748 } 11749 } 11750 for (ilm = ill_v6->ill_ilm; ilm != NULL; 11751 ilm = ilm->ilm_next) { 11752 if (ilm->ilm_orig_ifindex != phyi->phyint_ifindex) { 11753 mutex_enter(&phyi->phyint_lock); 11754 phyi->phyint_flags &= ~PHYI_INACTIVE; 11755 mutex_exit(&phyi->phyint_lock); 11756 return; 11757 } 11758 } 11759 } 11760 mutex_enter(&phyi->phyint_lock); 11761 phyi->phyint_flags |= PHYI_INACTIVE; 11762 mutex_exit(&phyi->phyint_lock); 11763 } 11764 11765 /* 11766 * This function is called only when the phyint flags change. Currently 11767 * called from ip_sioctl_flags. We re-do the broadcast nomination so 11768 * that we can select a good ill. 11769 */ 11770 static void 11771 ip_redo_nomination(phyint_t *phyi) 11772 { 11773 ill_t *ill_v4; 11774 11775 ill_v4 = phyi->phyint_illv4; 11776 11777 if (ill_v4 != NULL && ill_v4->ill_group != NULL) { 11778 ASSERT(IAM_WRITER_ILL(ill_v4)); 11779 if (ill_v4->ill_group->illgrp_ill_count > 1) 11780 ill_nominate_bcast_rcv(ill_v4->ill_group); 11781 } 11782 } 11783 11784 /* 11785 * Heuristic to check if ill is INACTIVE. 11786 * Checks if ill has an ipif with an usable ip address. 11787 * 11788 * Return values: 11789 * B_TRUE - ill is INACTIVE; has no usable ipif 11790 * B_FALSE - ill is not INACTIVE; ill has at least one usable ipif 11791 */ 11792 static boolean_t 11793 ill_is_inactive(ill_t *ill) 11794 { 11795 ipif_t *ipif; 11796 11797 /* Check whether it is in an IPMP group */ 11798 if (ill->ill_phyint->phyint_groupname == NULL) 11799 return (B_FALSE); 11800 11801 if (ill->ill_ipif_up_count == 0) 11802 return (B_TRUE); 11803 11804 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 11805 uint64_t flags = ipif->ipif_flags; 11806 11807 /* 11808 * This ipif is usable if it is IPIF_UP and not a 11809 * dedicated test address. A dedicated test address 11810 * is marked IPIF_NOFAILOVER *and* IPIF_DEPRECATED 11811 * (note in particular that V6 test addresses are 11812 * link-local data addresses and thus are marked 11813 * IPIF_NOFAILOVER but not IPIF_DEPRECATED). 11814 */ 11815 if ((flags & IPIF_UP) && 11816 ((flags & (IPIF_DEPRECATED|IPIF_NOFAILOVER)) != 11817 (IPIF_DEPRECATED|IPIF_NOFAILOVER))) 11818 return (B_FALSE); 11819 } 11820 return (B_TRUE); 11821 } 11822 11823 /* 11824 * Set interface flags. 11825 * Need to do special action for IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, 11826 * IPIF_NOLOCAL, ILLF_NONUD, ILLF_NOARP, IPIF_PRIVATE, IPIF_ANYCAST, 11827 * IPIF_PREFERRED, PHYI_STANDBY, PHYI_FAILED and PHYI_OFFLINE. 11828 * 11829 * NOTE : We really don't enforce that ipif_id zero should be used 11830 * for setting any flags other than IFF_LOGINT_FLAGS. This 11831 * is because applications generally does SICGLIFFLAGS and 11832 * ORs in the new flags (that affects the logical) and does a 11833 * SIOCSLIFFLAGS. Thus, "flags" below could contain bits other 11834 * than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the 11835 * flags that will be turned on is correct with respect to 11836 * ipif_id 0. For backward compatibility reasons, it is not done. 11837 */ 11838 /* ARGSUSED */ 11839 int 11840 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 11841 ip_ioctl_cmd_t *ipip, void *if_req) 11842 { 11843 uint64_t turn_on; 11844 uint64_t turn_off; 11845 int err; 11846 boolean_t need_up = B_FALSE; 11847 phyint_t *phyi; 11848 ill_t *ill; 11849 uint64_t intf_flags; 11850 boolean_t phyint_flags_modified = B_FALSE; 11851 uint64_t flags; 11852 struct ifreq *ifr; 11853 struct lifreq *lifr; 11854 boolean_t set_linklocal = B_FALSE; 11855 boolean_t zero_source = B_FALSE; 11856 ip_stack_t *ipst; 11857 11858 ip1dbg(("ip_sioctl_flags(%s:%u %p)\n", 11859 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 11860 11861 ASSERT(IAM_WRITER_IPIF(ipif)); 11862 11863 ill = ipif->ipif_ill; 11864 phyi = ill->ill_phyint; 11865 ipst = ill->ill_ipst; 11866 11867 if (ipip->ipi_cmd_type == IF_CMD) { 11868 ifr = (struct ifreq *)if_req; 11869 flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff); 11870 } else { 11871 lifr = (struct lifreq *)if_req; 11872 flags = lifr->lifr_flags; 11873 } 11874 11875 intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags; 11876 11877 /* 11878 * Has the flags been set correctly till now ? 11879 */ 11880 ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0); 11881 ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0); 11882 ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0); 11883 /* 11884 * Compare the new flags to the old, and partition 11885 * into those coming on and those going off. 11886 * For the 16 bit command keep the bits above bit 16 unchanged. 11887 */ 11888 if (ipip->ipi_cmd == SIOCSIFFLAGS) 11889 flags |= intf_flags & ~0xFFFF; 11890 11891 /* 11892 * First check which bits will change and then which will 11893 * go on and off 11894 */ 11895 turn_on = (flags ^ intf_flags) & ~IFF_CANTCHANGE; 11896 if (!turn_on) 11897 return (0); /* No change */ 11898 11899 turn_off = intf_flags & turn_on; 11900 turn_on ^= turn_off; 11901 err = 0; 11902 11903 /* 11904 * Don't allow any bits belonging to the logical interface 11905 * to be set or cleared on the replacement ipif that was 11906 * created temporarily during a MOVE. 11907 */ 11908 if (ipif->ipif_replace_zero && 11909 ((turn_on|turn_off) & IFF_LOGINT_FLAGS) != 0) { 11910 return (EINVAL); 11911 } 11912 11913 /* 11914 * Only allow the IFF_XRESOLV and IFF_TEMPORARY flags to be set on 11915 * IPv6 interfaces. 11916 */ 11917 if ((turn_on & (IFF_XRESOLV|IFF_TEMPORARY)) && !(ipif->ipif_isv6)) 11918 return (EINVAL); 11919 11920 /* 11921 * Don't allow the IFF_ROUTER flag to be turned on on loopback 11922 * interfaces. It makes no sense in that context. 11923 */ 11924 if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK)) 11925 return (EINVAL); 11926 11927 if (flags & (IFF_NOLOCAL|IFF_ANYCAST)) 11928 zero_source = B_TRUE; 11929 11930 /* 11931 * For IPv6 ipif_id 0, don't allow the interface to be up without 11932 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set. 11933 * If the link local address isn't set, and can be set, it will get 11934 * set later on in this function. 11935 */ 11936 if (ipif->ipif_id == 0 && ipif->ipif_isv6 && 11937 (flags & IFF_UP) && !zero_source && 11938 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 11939 if (ipif_cant_setlinklocal(ipif)) 11940 return (EINVAL); 11941 set_linklocal = B_TRUE; 11942 } 11943 11944 /* 11945 * ILL cannot be part of a usesrc group and and IPMP group at the 11946 * same time. No need to grab ill_g_usesrc_lock here, see 11947 * synchronization notes in ip.c 11948 */ 11949 if (turn_on & PHYI_STANDBY && 11950 ipif->ipif_ill->ill_usesrc_grp_next != NULL) { 11951 return (EINVAL); 11952 } 11953 11954 /* 11955 * If we modify physical interface flags, we'll potentially need to 11956 * send up two routing socket messages for the changes (one for the 11957 * IPv4 ill, and another for the IPv6 ill). Note that here. 11958 */ 11959 if ((turn_on|turn_off) & IFF_PHYINT_FLAGS) 11960 phyint_flags_modified = B_TRUE; 11961 11962 /* 11963 * If we are setting or clearing FAILED or STANDBY or OFFLINE, 11964 * we need to flush the IRE_CACHES belonging to this ill. 11965 * We handle this case here without doing the DOWN/UP dance 11966 * like it is done for other flags. If some other flags are 11967 * being turned on/off with FAILED/STANDBY/OFFLINE, the code 11968 * below will handle it by bringing it down and then 11969 * bringing it UP. 11970 */ 11971 if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) { 11972 ill_t *ill_v4, *ill_v6; 11973 11974 ill_v4 = phyi->phyint_illv4; 11975 ill_v6 = phyi->phyint_illv6; 11976 11977 /* 11978 * First set the INACTIVE flag if needed. Then delete the ires. 11979 * ire_add will atomically prevent creating new IRE_CACHEs 11980 * unless hidden flag is set. 11981 * PHYI_FAILED and PHYI_INACTIVE are exclusive 11982 */ 11983 if ((turn_on & PHYI_FAILED) && 11984 ((intf_flags & PHYI_STANDBY) || 11985 !ipst->ips_ipmp_enable_failback)) { 11986 /* Reset PHYI_INACTIVE when PHYI_FAILED is being set */ 11987 phyi->phyint_flags &= ~PHYI_INACTIVE; 11988 } 11989 if ((turn_off & PHYI_FAILED) && 11990 ((intf_flags & PHYI_STANDBY) || 11991 (!ipst->ips_ipmp_enable_failback && 11992 ill_is_inactive(ill)))) { 11993 phyint_inactive(phyi); 11994 } 11995 11996 if (turn_on & PHYI_STANDBY) { 11997 /* 11998 * We implicitly set INACTIVE only when STANDBY is set. 11999 * INACTIVE is also set on non-STANDBY phyint when user 12000 * disables FAILBACK using configuration file. 12001 * Do not allow STANDBY to be set on such INACTIVE 12002 * phyint 12003 */ 12004 if (phyi->phyint_flags & PHYI_INACTIVE) 12005 return (EINVAL); 12006 if (!(phyi->phyint_flags & PHYI_FAILED)) 12007 phyint_inactive(phyi); 12008 } 12009 if (turn_off & PHYI_STANDBY) { 12010 if (ipst->ips_ipmp_enable_failback) { 12011 /* 12012 * Reset PHYI_INACTIVE. 12013 */ 12014 phyi->phyint_flags &= ~PHYI_INACTIVE; 12015 } else if (ill_is_inactive(ill) && 12016 !(phyi->phyint_flags & PHYI_FAILED)) { 12017 /* 12018 * Need to set INACTIVE, when user sets 12019 * STANDBY on a non-STANDBY phyint and 12020 * later resets STANDBY 12021 */ 12022 phyint_inactive(phyi); 12023 } 12024 } 12025 /* 12026 * We should always send up a message so that the 12027 * daemons come to know of it. Note that the zeroth 12028 * interface can be down and the check below for IPIF_UP 12029 * will not make sense as we are actually setting 12030 * a phyint flag here. We assume that the ipif used 12031 * is always the zeroth ipif. (ip_rts_ifmsg does not 12032 * send up any message for non-zero ipifs). 12033 */ 12034 phyint_flags_modified = B_TRUE; 12035 12036 if (ill_v4 != NULL) { 12037 ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, 12038 IRE_CACHE, ill_stq_cache_delete, 12039 (char *)ill_v4, ill_v4); 12040 illgrp_reset_schednext(ill_v4); 12041 } 12042 if (ill_v6 != NULL) { 12043 ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE, 12044 IRE_CACHE, ill_stq_cache_delete, 12045 (char *)ill_v6, ill_v6); 12046 illgrp_reset_schednext(ill_v6); 12047 } 12048 } 12049 12050 /* 12051 * If ILLF_ROUTER changes, we need to change the ip forwarding 12052 * status of the interface and, if the interface is part of an IPMP 12053 * group, all other interfaces that are part of the same IPMP 12054 * group. 12055 */ 12056 if ((turn_on | turn_off) & ILLF_ROUTER) 12057 (void) ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0)); 12058 12059 /* 12060 * If the interface is not UP and we are not going to 12061 * bring it UP, record the flags and return. When the 12062 * interface comes UP later, the right actions will be 12063 * taken. 12064 */ 12065 if (!(ipif->ipif_flags & IPIF_UP) && 12066 !(turn_on & IPIF_UP)) { 12067 /* Record new flags in their respective places. */ 12068 mutex_enter(&ill->ill_lock); 12069 mutex_enter(&ill->ill_phyint->phyint_lock); 12070 ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS); 12071 ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS); 12072 ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS); 12073 ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS); 12074 phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS); 12075 phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS); 12076 mutex_exit(&ill->ill_lock); 12077 mutex_exit(&ill->ill_phyint->phyint_lock); 12078 12079 /* 12080 * We do the broadcast and nomination here rather 12081 * than waiting for a FAILOVER/FAILBACK to happen. In 12082 * the case of FAILBACK from INACTIVE standby to the 12083 * interface that has been repaired, PHYI_FAILED has not 12084 * been cleared yet. If there are only two interfaces in 12085 * that group, all we have is a FAILED and INACTIVE 12086 * interface. If we do the nomination soon after a failback, 12087 * the broadcast nomination code would select the 12088 * INACTIVE interface for receiving broadcasts as FAILED is 12089 * not yet cleared. As we don't want STANDBY/INACTIVE to 12090 * receive broadcast packets, we need to redo nomination 12091 * when the FAILED is cleared here. Thus, in general we 12092 * always do the nomination here for FAILED, STANDBY 12093 * and OFFLINE. 12094 */ 12095 if (((turn_on | turn_off) & 12096 (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) { 12097 ip_redo_nomination(phyi); 12098 } 12099 if (phyint_flags_modified) { 12100 if (phyi->phyint_illv4 != NULL) { 12101 ip_rts_ifmsg(phyi->phyint_illv4-> 12102 ill_ipif); 12103 } 12104 if (phyi->phyint_illv6 != NULL) { 12105 ip_rts_ifmsg(phyi->phyint_illv6-> 12106 ill_ipif); 12107 } 12108 } 12109 return (0); 12110 } else if (set_linklocal || zero_source) { 12111 mutex_enter(&ill->ill_lock); 12112 if (set_linklocal) 12113 ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL; 12114 if (zero_source) 12115 ipif->ipif_state_flags |= IPIF_ZERO_SOURCE; 12116 mutex_exit(&ill->ill_lock); 12117 } 12118 12119 /* 12120 * Disallow IPv6 interfaces coming up that have the unspecified address, 12121 * or point-to-point interfaces with an unspecified destination. We do 12122 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that 12123 * have a subnet assigned, which is how in.ndpd currently manages its 12124 * onlink prefix list when no addresses are configured with those 12125 * prefixes. 12126 */ 12127 if (ipif->ipif_isv6 && 12128 ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) && 12129 (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) || 12130 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) || 12131 ((ipif->ipif_flags & IPIF_POINTOPOINT) && 12132 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) { 12133 return (EINVAL); 12134 } 12135 12136 /* 12137 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination 12138 * from being brought up. 12139 */ 12140 if (!ipif->ipif_isv6 && 12141 ((ipif->ipif_flags & IPIF_POINTOPOINT) && 12142 ipif->ipif_pp_dst_addr == INADDR_ANY)) { 12143 return (EINVAL); 12144 } 12145 12146 /* 12147 * The only flag changes that we currently take specific action on 12148 * is IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, 12149 * ILLF_NOARP, ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, and 12150 * IPIF_PREFERRED. This is done by bring the ipif down, changing 12151 * the flags and bringing it back up again. 12152 */ 12153 if ((turn_on|turn_off) & 12154 (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP| 12155 ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED)) { 12156 /* 12157 * Taking this ipif down, make sure we have 12158 * valid net and subnet bcast ire's for other 12159 * logical interfaces, if we need them. 12160 */ 12161 if (!ipif->ipif_isv6) 12162 ipif_check_bcast_ires(ipif); 12163 12164 if (((ipif->ipif_flags | turn_on) & IPIF_UP) && 12165 !(turn_off & IPIF_UP)) { 12166 need_up = B_TRUE; 12167 if (ipif->ipif_flags & IPIF_UP) 12168 ill->ill_logical_down = 1; 12169 turn_on &= ~IPIF_UP; 12170 } 12171 err = ipif_down(ipif, q, mp); 12172 ip1dbg(("ipif_down returns %d err ", err)); 12173 if (err == EINPROGRESS) 12174 return (err); 12175 ipif_down_tail(ipif); 12176 } 12177 return (ip_sioctl_flags_tail(ipif, flags, q, mp, need_up)); 12178 } 12179 12180 static int 12181 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp, 12182 boolean_t need_up) 12183 { 12184 ill_t *ill; 12185 phyint_t *phyi; 12186 uint64_t turn_on; 12187 uint64_t turn_off; 12188 uint64_t intf_flags; 12189 boolean_t phyint_flags_modified = B_FALSE; 12190 int err = 0; 12191 boolean_t set_linklocal = B_FALSE; 12192 boolean_t zero_source = B_FALSE; 12193 12194 ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n", 12195 ipif->ipif_ill->ill_name, ipif->ipif_id)); 12196 12197 ASSERT(IAM_WRITER_IPIF(ipif)); 12198 12199 ill = ipif->ipif_ill; 12200 phyi = ill->ill_phyint; 12201 12202 intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags; 12203 turn_on = (flags ^ intf_flags) & ~(IFF_CANTCHANGE | IFF_UP); 12204 12205 turn_off = intf_flags & turn_on; 12206 turn_on ^= turn_off; 12207 12208 if ((turn_on|turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE)) 12209 phyint_flags_modified = B_TRUE; 12210 12211 /* 12212 * Now we change the flags. Track current value of 12213 * other flags in their respective places. 12214 */ 12215 mutex_enter(&ill->ill_lock); 12216 mutex_enter(&phyi->phyint_lock); 12217 ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS); 12218 ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS); 12219 ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS); 12220 ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS); 12221 phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS); 12222 phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS); 12223 if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) { 12224 set_linklocal = B_TRUE; 12225 ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL; 12226 } 12227 if (ipif->ipif_state_flags & IPIF_ZERO_SOURCE) { 12228 zero_source = B_TRUE; 12229 ipif->ipif_state_flags &= ~IPIF_ZERO_SOURCE; 12230 } 12231 mutex_exit(&ill->ill_lock); 12232 mutex_exit(&phyi->phyint_lock); 12233 12234 if (((turn_on | turn_off) & (PHYI_FAILED|PHYI_STANDBY|PHYI_OFFLINE))) 12235 ip_redo_nomination(phyi); 12236 12237 if (set_linklocal) 12238 (void) ipif_setlinklocal(ipif); 12239 12240 if (zero_source) 12241 ipif->ipif_v6src_addr = ipv6_all_zeros; 12242 else 12243 ipif->ipif_v6src_addr = ipif->ipif_v6lcl_addr; 12244 12245 if (need_up) { 12246 /* 12247 * XXX ipif_up really does not know whether a phyint flags 12248 * was modified or not. So, it sends up information on 12249 * only one routing sockets message. As we don't bring up 12250 * the interface and also set STANDBY/FAILED simultaneously 12251 * it should be okay. 12252 */ 12253 err = ipif_up(ipif, q, mp); 12254 } else { 12255 /* 12256 * Make sure routing socket sees all changes to the flags. 12257 * ipif_up_done* handles this when we use ipif_up. 12258 */ 12259 if (phyint_flags_modified) { 12260 if (phyi->phyint_illv4 != NULL) { 12261 ip_rts_ifmsg(phyi->phyint_illv4-> 12262 ill_ipif); 12263 } 12264 if (phyi->phyint_illv6 != NULL) { 12265 ip_rts_ifmsg(phyi->phyint_illv6-> 12266 ill_ipif); 12267 } 12268 } else { 12269 ip_rts_ifmsg(ipif); 12270 } 12271 /* 12272 * Update the flags in SCTP's IPIF list, ipif_up() will do 12273 * this in need_up case. 12274 */ 12275 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE); 12276 } 12277 return (err); 12278 } 12279 12280 /* 12281 * Restart entry point to restart the flags restart operation after the 12282 * refcounts have dropped to zero. 12283 */ 12284 /* ARGSUSED */ 12285 int 12286 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12287 ip_ioctl_cmd_t *ipip, void *if_req) 12288 { 12289 int err; 12290 struct ifreq *ifr = (struct ifreq *)if_req; 12291 struct lifreq *lifr = (struct lifreq *)if_req; 12292 12293 ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n", 12294 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12295 12296 ipif_down_tail(ipif); 12297 if (ipip->ipi_cmd_type == IF_CMD) { 12298 /* 12299 * Since ip_sioctl_flags expects an int and ifr_flags 12300 * is a short we need to cast ifr_flags into an int 12301 * to avoid having sign extension cause bits to get 12302 * set that should not be. 12303 */ 12304 err = ip_sioctl_flags_tail(ipif, 12305 (uint64_t)(ifr->ifr_flags & 0x0000ffff), 12306 q, mp, B_TRUE); 12307 } else { 12308 err = ip_sioctl_flags_tail(ipif, lifr->lifr_flags, 12309 q, mp, B_TRUE); 12310 } 12311 return (err); 12312 } 12313 12314 /* 12315 * Can operate on either a module or a driver queue. 12316 */ 12317 /* ARGSUSED */ 12318 int 12319 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12320 ip_ioctl_cmd_t *ipip, void *if_req) 12321 { 12322 /* 12323 * Has the flags been set correctly till now ? 12324 */ 12325 ill_t *ill = ipif->ipif_ill; 12326 phyint_t *phyi = ill->ill_phyint; 12327 12328 ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n", 12329 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12330 ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0); 12331 ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0); 12332 ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0); 12333 12334 /* 12335 * Need a lock since some flags can be set even when there are 12336 * references to the ipif. 12337 */ 12338 mutex_enter(&ill->ill_lock); 12339 if (ipip->ipi_cmd_type == IF_CMD) { 12340 struct ifreq *ifr = (struct ifreq *)if_req; 12341 12342 /* Get interface flags (low 16 only). */ 12343 ifr->ifr_flags = ((ipif->ipif_flags | 12344 ill->ill_flags | phyi->phyint_flags) & 0xffff); 12345 } else { 12346 struct lifreq *lifr = (struct lifreq *)if_req; 12347 12348 /* Get interface flags. */ 12349 lifr->lifr_flags = ipif->ipif_flags | 12350 ill->ill_flags | phyi->phyint_flags; 12351 } 12352 mutex_exit(&ill->ill_lock); 12353 return (0); 12354 } 12355 12356 /* ARGSUSED */ 12357 int 12358 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12359 ip_ioctl_cmd_t *ipip, void *if_req) 12360 { 12361 int mtu; 12362 int ip_min_mtu; 12363 struct ifreq *ifr; 12364 struct lifreq *lifr; 12365 ire_t *ire; 12366 ip_stack_t *ipst; 12367 12368 ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name, 12369 ipif->ipif_id, (void *)ipif)); 12370 if (ipip->ipi_cmd_type == IF_CMD) { 12371 ifr = (struct ifreq *)if_req; 12372 mtu = ifr->ifr_metric; 12373 } else { 12374 lifr = (struct lifreq *)if_req; 12375 mtu = lifr->lifr_mtu; 12376 } 12377 12378 if (ipif->ipif_isv6) 12379 ip_min_mtu = IPV6_MIN_MTU; 12380 else 12381 ip_min_mtu = IP_MIN_MTU; 12382 12383 if (mtu > ipif->ipif_ill->ill_max_frag || mtu < ip_min_mtu) 12384 return (EINVAL); 12385 12386 /* 12387 * Change the MTU size in all relevant ire's. 12388 * Mtu change Vs. new ire creation - protocol below. 12389 * First change ipif_mtu and the ire_max_frag of the 12390 * interface ire. Then do an ire walk and change the 12391 * ire_max_frag of all affected ires. During ire_add 12392 * under the bucket lock, set the ire_max_frag of the 12393 * new ire being created from the ipif/ire from which 12394 * it is being derived. If an mtu change happens after 12395 * the ire is added, the new ire will be cleaned up. 12396 * Conversely if the mtu change happens before the ire 12397 * is added, ire_add will see the new value of the mtu. 12398 */ 12399 ipif->ipif_mtu = mtu; 12400 ipif->ipif_flags |= IPIF_FIXEDMTU; 12401 12402 if (ipif->ipif_isv6) 12403 ire = ipif_to_ire_v6(ipif); 12404 else 12405 ire = ipif_to_ire(ipif); 12406 if (ire != NULL) { 12407 ire->ire_max_frag = ipif->ipif_mtu; 12408 ire_refrele(ire); 12409 } 12410 ipst = ipif->ipif_ill->ill_ipst; 12411 if (ipif->ipif_flags & IPIF_UP) { 12412 if (ipif->ipif_isv6) 12413 ire_walk_v6(ipif_mtu_change, (char *)ipif, ALL_ZONES, 12414 ipst); 12415 else 12416 ire_walk_v4(ipif_mtu_change, (char *)ipif, ALL_ZONES, 12417 ipst); 12418 } 12419 /* Update the MTU in SCTP's list */ 12420 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE); 12421 return (0); 12422 } 12423 12424 /* Get interface MTU. */ 12425 /* ARGSUSED */ 12426 int 12427 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12428 ip_ioctl_cmd_t *ipip, void *if_req) 12429 { 12430 struct ifreq *ifr; 12431 struct lifreq *lifr; 12432 12433 ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n", 12434 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12435 if (ipip->ipi_cmd_type == IF_CMD) { 12436 ifr = (struct ifreq *)if_req; 12437 ifr->ifr_metric = ipif->ipif_mtu; 12438 } else { 12439 lifr = (struct lifreq *)if_req; 12440 lifr->lifr_mtu = ipif->ipif_mtu; 12441 } 12442 return (0); 12443 } 12444 12445 /* Set interface broadcast address. */ 12446 /* ARGSUSED2 */ 12447 int 12448 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12449 ip_ioctl_cmd_t *ipip, void *if_req) 12450 { 12451 ipaddr_t addr; 12452 ire_t *ire; 12453 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 12454 12455 ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ipif->ipif_ill->ill_name, 12456 ipif->ipif_id)); 12457 12458 ASSERT(IAM_WRITER_IPIF(ipif)); 12459 if (!(ipif->ipif_flags & IPIF_BROADCAST)) 12460 return (EADDRNOTAVAIL); 12461 12462 ASSERT(!(ipif->ipif_isv6)); /* No IPv6 broadcast */ 12463 12464 if (sin->sin_family != AF_INET) 12465 return (EAFNOSUPPORT); 12466 12467 addr = sin->sin_addr.s_addr; 12468 if (ipif->ipif_flags & IPIF_UP) { 12469 /* 12470 * If we are already up, make sure the new 12471 * broadcast address makes sense. If it does, 12472 * there should be an IRE for it already. 12473 * Don't match on ipif, only on the ill 12474 * since we are sharing these now. Don't use 12475 * MATCH_IRE_ILL_GROUP as we are looking for 12476 * the broadcast ire on this ill and each ill 12477 * in the group has its own broadcast ire. 12478 */ 12479 ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, 12480 ipif, ALL_ZONES, NULL, 12481 (MATCH_IRE_ILL | MATCH_IRE_TYPE), ipst); 12482 if (ire == NULL) { 12483 return (EINVAL); 12484 } else { 12485 ire_refrele(ire); 12486 } 12487 } 12488 /* 12489 * Changing the broadcast addr for this ipif. 12490 * Make sure we have valid net and subnet bcast 12491 * ire's for other logical interfaces, if needed. 12492 */ 12493 if (addr != ipif->ipif_brd_addr) 12494 ipif_check_bcast_ires(ipif); 12495 IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr); 12496 return (0); 12497 } 12498 12499 /* Get interface broadcast address. */ 12500 /* ARGSUSED */ 12501 int 12502 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12503 ip_ioctl_cmd_t *ipip, void *if_req) 12504 { 12505 ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n", 12506 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12507 if (!(ipif->ipif_flags & IPIF_BROADCAST)) 12508 return (EADDRNOTAVAIL); 12509 12510 /* IPIF_BROADCAST not possible with IPv6 */ 12511 ASSERT(!ipif->ipif_isv6); 12512 *sin = sin_null; 12513 sin->sin_family = AF_INET; 12514 sin->sin_addr.s_addr = ipif->ipif_brd_addr; 12515 return (0); 12516 } 12517 12518 /* 12519 * This routine is called to handle the SIOCS*IFNETMASK IOCTL. 12520 */ 12521 /* ARGSUSED */ 12522 int 12523 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12524 ip_ioctl_cmd_t *ipip, void *if_req) 12525 { 12526 int err = 0; 12527 in6_addr_t v6mask; 12528 12529 ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n", 12530 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12531 12532 ASSERT(IAM_WRITER_IPIF(ipif)); 12533 12534 if (ipif->ipif_isv6) { 12535 sin6_t *sin6; 12536 12537 if (sin->sin_family != AF_INET6) 12538 return (EAFNOSUPPORT); 12539 12540 sin6 = (sin6_t *)sin; 12541 v6mask = sin6->sin6_addr; 12542 } else { 12543 ipaddr_t mask; 12544 12545 if (sin->sin_family != AF_INET) 12546 return (EAFNOSUPPORT); 12547 12548 mask = sin->sin_addr.s_addr; 12549 V4MASK_TO_V6(mask, v6mask); 12550 } 12551 12552 /* 12553 * No big deal if the interface isn't already up, or the mask 12554 * isn't really changing, or this is pt-pt. 12555 */ 12556 if (!(ipif->ipif_flags & IPIF_UP) || 12557 IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) || 12558 (ipif->ipif_flags & IPIF_POINTOPOINT)) { 12559 ipif->ipif_v6net_mask = v6mask; 12560 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) { 12561 V6_MASK_COPY(ipif->ipif_v6lcl_addr, 12562 ipif->ipif_v6net_mask, 12563 ipif->ipif_v6subnet); 12564 } 12565 return (0); 12566 } 12567 /* 12568 * Make sure we have valid net and subnet broadcast ire's 12569 * for the old netmask, if needed by other logical interfaces. 12570 */ 12571 if (!ipif->ipif_isv6) 12572 ipif_check_bcast_ires(ipif); 12573 12574 err = ipif_logical_down(ipif, q, mp); 12575 if (err == EINPROGRESS) 12576 return (err); 12577 ipif_down_tail(ipif); 12578 err = ip_sioctl_netmask_tail(ipif, sin, q, mp); 12579 return (err); 12580 } 12581 12582 static int 12583 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp) 12584 { 12585 in6_addr_t v6mask; 12586 int err = 0; 12587 12588 ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n", 12589 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12590 12591 if (ipif->ipif_isv6) { 12592 sin6_t *sin6; 12593 12594 sin6 = (sin6_t *)sin; 12595 v6mask = sin6->sin6_addr; 12596 } else { 12597 ipaddr_t mask; 12598 12599 mask = sin->sin_addr.s_addr; 12600 V4MASK_TO_V6(mask, v6mask); 12601 } 12602 12603 ipif->ipif_v6net_mask = v6mask; 12604 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) { 12605 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 12606 ipif->ipif_v6subnet); 12607 } 12608 err = ipif_up(ipif, q, mp); 12609 12610 if (err == 0 || err == EINPROGRESS) { 12611 /* 12612 * The interface must be DL_BOUND if this packet has to 12613 * go out on the wire. Since we only go through a logical 12614 * down and are bound with the driver during an internal 12615 * down/up that is satisfied. 12616 */ 12617 if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) { 12618 /* Potentially broadcast an address mask reply. */ 12619 ipif_mask_reply(ipif); 12620 } 12621 } 12622 return (err); 12623 } 12624 12625 /* ARGSUSED */ 12626 int 12627 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12628 ip_ioctl_cmd_t *ipip, void *if_req) 12629 { 12630 ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n", 12631 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12632 ipif_down_tail(ipif); 12633 return (ip_sioctl_netmask_tail(ipif, sin, q, mp)); 12634 } 12635 12636 /* Get interface net mask. */ 12637 /* ARGSUSED */ 12638 int 12639 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12640 ip_ioctl_cmd_t *ipip, void *if_req) 12641 { 12642 struct lifreq *lifr = (struct lifreq *)if_req; 12643 struct sockaddr_in6 *sin6 = (sin6_t *)sin; 12644 12645 ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n", 12646 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12647 12648 /* 12649 * net mask can't change since we have a reference to the ipif. 12650 */ 12651 if (ipif->ipif_isv6) { 12652 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 12653 *sin6 = sin6_null; 12654 sin6->sin6_family = AF_INET6; 12655 sin6->sin6_addr = ipif->ipif_v6net_mask; 12656 lifr->lifr_addrlen = 12657 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 12658 } else { 12659 *sin = sin_null; 12660 sin->sin_family = AF_INET; 12661 sin->sin_addr.s_addr = ipif->ipif_net_mask; 12662 if (ipip->ipi_cmd_type == LIF_CMD) { 12663 lifr->lifr_addrlen = 12664 ip_mask_to_plen(ipif->ipif_net_mask); 12665 } 12666 } 12667 return (0); 12668 } 12669 12670 /* ARGSUSED */ 12671 int 12672 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12673 ip_ioctl_cmd_t *ipip, void *if_req) 12674 { 12675 12676 ip1dbg(("ip_sioctl_metric(%s:%u %p)\n", 12677 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12678 /* 12679 * Set interface metric. We don't use this for 12680 * anything but we keep track of it in case it is 12681 * important to routing applications or such. 12682 */ 12683 if (ipip->ipi_cmd_type == IF_CMD) { 12684 struct ifreq *ifr; 12685 12686 ifr = (struct ifreq *)if_req; 12687 ipif->ipif_metric = ifr->ifr_metric; 12688 } else { 12689 struct lifreq *lifr; 12690 12691 lifr = (struct lifreq *)if_req; 12692 ipif->ipif_metric = lifr->lifr_metric; 12693 } 12694 return (0); 12695 } 12696 12697 12698 /* ARGSUSED */ 12699 int 12700 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12701 ip_ioctl_cmd_t *ipip, void *if_req) 12702 { 12703 12704 /* Get interface metric. */ 12705 ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n", 12706 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12707 if (ipip->ipi_cmd_type == IF_CMD) { 12708 struct ifreq *ifr; 12709 12710 ifr = (struct ifreq *)if_req; 12711 ifr->ifr_metric = ipif->ipif_metric; 12712 } else { 12713 struct lifreq *lifr; 12714 12715 lifr = (struct lifreq *)if_req; 12716 lifr->lifr_metric = ipif->ipif_metric; 12717 } 12718 12719 return (0); 12720 } 12721 12722 /* ARGSUSED */ 12723 int 12724 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12725 ip_ioctl_cmd_t *ipip, void *if_req) 12726 { 12727 12728 ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n", 12729 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12730 /* 12731 * Set the muxid returned from I_PLINK. 12732 */ 12733 if (ipip->ipi_cmd_type == IF_CMD) { 12734 struct ifreq *ifr = (struct ifreq *)if_req; 12735 12736 ipif->ipif_ill->ill_ip_muxid = ifr->ifr_ip_muxid; 12737 ipif->ipif_ill->ill_arp_muxid = ifr->ifr_arp_muxid; 12738 } else { 12739 struct lifreq *lifr = (struct lifreq *)if_req; 12740 12741 ipif->ipif_ill->ill_ip_muxid = lifr->lifr_ip_muxid; 12742 ipif->ipif_ill->ill_arp_muxid = lifr->lifr_arp_muxid; 12743 } 12744 return (0); 12745 } 12746 12747 /* ARGSUSED */ 12748 int 12749 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12750 ip_ioctl_cmd_t *ipip, void *if_req) 12751 { 12752 12753 ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n", 12754 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12755 /* 12756 * Get the muxid saved in ill for I_PUNLINK. 12757 */ 12758 if (ipip->ipi_cmd_type == IF_CMD) { 12759 struct ifreq *ifr = (struct ifreq *)if_req; 12760 12761 ifr->ifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid; 12762 ifr->ifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid; 12763 } else { 12764 struct lifreq *lifr = (struct lifreq *)if_req; 12765 12766 lifr->lifr_ip_muxid = ipif->ipif_ill->ill_ip_muxid; 12767 lifr->lifr_arp_muxid = ipif->ipif_ill->ill_arp_muxid; 12768 } 12769 return (0); 12770 } 12771 12772 /* 12773 * Set the subnet prefix. Does not modify the broadcast address. 12774 */ 12775 /* ARGSUSED */ 12776 int 12777 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12778 ip_ioctl_cmd_t *ipip, void *if_req) 12779 { 12780 int err = 0; 12781 in6_addr_t v6addr; 12782 in6_addr_t v6mask; 12783 boolean_t need_up = B_FALSE; 12784 int addrlen; 12785 12786 ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n", 12787 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12788 12789 ASSERT(IAM_WRITER_IPIF(ipif)); 12790 addrlen = ((struct lifreq *)if_req)->lifr_addrlen; 12791 12792 if (ipif->ipif_isv6) { 12793 sin6_t *sin6; 12794 12795 if (sin->sin_family != AF_INET6) 12796 return (EAFNOSUPPORT); 12797 12798 sin6 = (sin6_t *)sin; 12799 v6addr = sin6->sin6_addr; 12800 if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones)) 12801 return (EADDRNOTAVAIL); 12802 } else { 12803 ipaddr_t addr; 12804 12805 if (sin->sin_family != AF_INET) 12806 return (EAFNOSUPPORT); 12807 12808 addr = sin->sin_addr.s_addr; 12809 if (!ip_addr_ok_v4(addr, 0xFFFFFFFF)) 12810 return (EADDRNOTAVAIL); 12811 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 12812 /* Add 96 bits */ 12813 addrlen += IPV6_ABITS - IP_ABITS; 12814 } 12815 12816 if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL) 12817 return (EINVAL); 12818 12819 /* Check if bits in the address is set past the mask */ 12820 if (!V6_MASK_EQ(v6addr, v6mask, v6addr)) 12821 return (EINVAL); 12822 12823 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) && 12824 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask)) 12825 return (0); /* No change */ 12826 12827 if (ipif->ipif_flags & IPIF_UP) { 12828 /* 12829 * If the interface is already marked up, 12830 * we call ipif_down which will take care 12831 * of ditching any IREs that have been set 12832 * up based on the old interface address. 12833 */ 12834 err = ipif_logical_down(ipif, q, mp); 12835 if (err == EINPROGRESS) 12836 return (err); 12837 ipif_down_tail(ipif); 12838 need_up = B_TRUE; 12839 } 12840 12841 err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up); 12842 return (err); 12843 } 12844 12845 static int 12846 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask, 12847 queue_t *q, mblk_t *mp, boolean_t need_up) 12848 { 12849 ill_t *ill = ipif->ipif_ill; 12850 int err = 0; 12851 12852 ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n", 12853 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12854 12855 /* Set the new address. */ 12856 mutex_enter(&ill->ill_lock); 12857 ipif->ipif_v6net_mask = v6mask; 12858 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) { 12859 V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask, 12860 ipif->ipif_v6subnet); 12861 } 12862 mutex_exit(&ill->ill_lock); 12863 12864 if (need_up) { 12865 /* 12866 * Now bring the interface back up. If this 12867 * is the only IPIF for the ILL, ipif_up 12868 * will have to re-bind to the device, so 12869 * we may get back EINPROGRESS, in which 12870 * case, this IOCTL will get completed in 12871 * ip_rput_dlpi when we see the DL_BIND_ACK. 12872 */ 12873 err = ipif_up(ipif, q, mp); 12874 if (err == EINPROGRESS) 12875 return (err); 12876 } 12877 return (err); 12878 } 12879 12880 /* ARGSUSED */ 12881 int 12882 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12883 ip_ioctl_cmd_t *ipip, void *if_req) 12884 { 12885 int addrlen; 12886 in6_addr_t v6addr; 12887 in6_addr_t v6mask; 12888 struct lifreq *lifr = (struct lifreq *)if_req; 12889 12890 ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n", 12891 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12892 ipif_down_tail(ipif); 12893 12894 addrlen = lifr->lifr_addrlen; 12895 if (ipif->ipif_isv6) { 12896 sin6_t *sin6; 12897 12898 sin6 = (sin6_t *)sin; 12899 v6addr = sin6->sin6_addr; 12900 } else { 12901 ipaddr_t addr; 12902 12903 addr = sin->sin_addr.s_addr; 12904 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 12905 addrlen += IPV6_ABITS - IP_ABITS; 12906 } 12907 (void) ip_plen_to_mask_v6(addrlen, &v6mask); 12908 12909 return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE)); 12910 } 12911 12912 /* ARGSUSED */ 12913 int 12914 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12915 ip_ioctl_cmd_t *ipip, void *if_req) 12916 { 12917 struct lifreq *lifr = (struct lifreq *)if_req; 12918 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin; 12919 12920 ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n", 12921 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12922 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 12923 12924 if (ipif->ipif_isv6) { 12925 *sin6 = sin6_null; 12926 sin6->sin6_family = AF_INET6; 12927 sin6->sin6_addr = ipif->ipif_v6subnet; 12928 lifr->lifr_addrlen = 12929 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask); 12930 } else { 12931 *sin = sin_null; 12932 sin->sin_family = AF_INET; 12933 sin->sin_addr.s_addr = ipif->ipif_subnet; 12934 lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask); 12935 } 12936 return (0); 12937 } 12938 12939 /* 12940 * Set the IPv6 address token. 12941 */ 12942 /* ARGSUSED */ 12943 int 12944 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 12945 ip_ioctl_cmd_t *ipi, void *if_req) 12946 { 12947 ill_t *ill = ipif->ipif_ill; 12948 int err; 12949 in6_addr_t v6addr; 12950 in6_addr_t v6mask; 12951 boolean_t need_up = B_FALSE; 12952 int i; 12953 sin6_t *sin6 = (sin6_t *)sin; 12954 struct lifreq *lifr = (struct lifreq *)if_req; 12955 int addrlen; 12956 12957 ip1dbg(("ip_sioctl_token(%s:%u %p)\n", 12958 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 12959 ASSERT(IAM_WRITER_IPIF(ipif)); 12960 12961 addrlen = lifr->lifr_addrlen; 12962 /* Only allow for logical unit zero i.e. not on "le0:17" */ 12963 if (ipif->ipif_id != 0) 12964 return (EINVAL); 12965 12966 if (!ipif->ipif_isv6) 12967 return (EINVAL); 12968 12969 if (addrlen > IPV6_ABITS) 12970 return (EINVAL); 12971 12972 v6addr = sin6->sin6_addr; 12973 12974 /* 12975 * The length of the token is the length from the end. To get 12976 * the proper mask for this, compute the mask of the bits not 12977 * in the token; ie. the prefix, and then xor to get the mask. 12978 */ 12979 if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL) 12980 return (EINVAL); 12981 for (i = 0; i < 4; i++) { 12982 v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff; 12983 } 12984 12985 if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) && 12986 ill->ill_token_length == addrlen) 12987 return (0); /* No change */ 12988 12989 if (ipif->ipif_flags & IPIF_UP) { 12990 err = ipif_logical_down(ipif, q, mp); 12991 if (err == EINPROGRESS) 12992 return (err); 12993 ipif_down_tail(ipif); 12994 need_up = B_TRUE; 12995 } 12996 err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up); 12997 return (err); 12998 } 12999 13000 static int 13001 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q, 13002 mblk_t *mp, boolean_t need_up) 13003 { 13004 in6_addr_t v6addr; 13005 in6_addr_t v6mask; 13006 ill_t *ill = ipif->ipif_ill; 13007 int i; 13008 int err = 0; 13009 13010 ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n", 13011 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 13012 v6addr = sin6->sin6_addr; 13013 /* 13014 * The length of the token is the length from the end. To get 13015 * the proper mask for this, compute the mask of the bits not 13016 * in the token; ie. the prefix, and then xor to get the mask. 13017 */ 13018 (void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask); 13019 for (i = 0; i < 4; i++) 13020 v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff; 13021 13022 mutex_enter(&ill->ill_lock); 13023 V6_MASK_COPY(v6addr, v6mask, ill->ill_token); 13024 ill->ill_token_length = addrlen; 13025 mutex_exit(&ill->ill_lock); 13026 13027 if (need_up) { 13028 /* 13029 * Now bring the interface back up. If this 13030 * is the only IPIF for the ILL, ipif_up 13031 * will have to re-bind to the device, so 13032 * we may get back EINPROGRESS, in which 13033 * case, this IOCTL will get completed in 13034 * ip_rput_dlpi when we see the DL_BIND_ACK. 13035 */ 13036 err = ipif_up(ipif, q, mp); 13037 if (err == EINPROGRESS) 13038 return (err); 13039 } 13040 return (err); 13041 } 13042 13043 /* ARGSUSED */ 13044 int 13045 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 13046 ip_ioctl_cmd_t *ipi, void *if_req) 13047 { 13048 ill_t *ill; 13049 sin6_t *sin6 = (sin6_t *)sin; 13050 struct lifreq *lifr = (struct lifreq *)if_req; 13051 13052 ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n", 13053 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 13054 if (ipif->ipif_id != 0) 13055 return (EINVAL); 13056 13057 ill = ipif->ipif_ill; 13058 if (!ill->ill_isv6) 13059 return (ENXIO); 13060 13061 *sin6 = sin6_null; 13062 sin6->sin6_family = AF_INET6; 13063 ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token)); 13064 sin6->sin6_addr = ill->ill_token; 13065 lifr->lifr_addrlen = ill->ill_token_length; 13066 return (0); 13067 } 13068 13069 /* 13070 * Set (hardware) link specific information that might override 13071 * what was acquired through the DL_INFO_ACK. 13072 * The logic is as follows. 13073 * 13074 * become exclusive 13075 * set CHANGING flag 13076 * change mtu on affected IREs 13077 * clear CHANGING flag 13078 * 13079 * An ire add that occurs before the CHANGING flag is set will have its mtu 13080 * changed by the ip_sioctl_lnkinfo. 13081 * 13082 * During the time the CHANGING flag is set, no new ires will be added to the 13083 * bucket, and ire add will fail (due the CHANGING flag). 13084 * 13085 * An ire add that occurs after the CHANGING flag is set will have the right mtu 13086 * before it is added to the bucket. 13087 * 13088 * Obviously only 1 thread can set the CHANGING flag and we need to become 13089 * exclusive to set the flag. 13090 */ 13091 /* ARGSUSED */ 13092 int 13093 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 13094 ip_ioctl_cmd_t *ipi, void *if_req) 13095 { 13096 ill_t *ill = ipif->ipif_ill; 13097 ipif_t *nipif; 13098 int ip_min_mtu; 13099 boolean_t mtu_walk = B_FALSE; 13100 struct lifreq *lifr = (struct lifreq *)if_req; 13101 lif_ifinfo_req_t *lir; 13102 ire_t *ire; 13103 13104 ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n", 13105 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 13106 lir = &lifr->lifr_ifinfo; 13107 ASSERT(IAM_WRITER_IPIF(ipif)); 13108 13109 /* Only allow for logical unit zero i.e. not on "le0:17" */ 13110 if (ipif->ipif_id != 0) 13111 return (EINVAL); 13112 13113 /* Set interface MTU. */ 13114 if (ipif->ipif_isv6) 13115 ip_min_mtu = IPV6_MIN_MTU; 13116 else 13117 ip_min_mtu = IP_MIN_MTU; 13118 13119 /* 13120 * Verify values before we set anything. Allow zero to 13121 * mean unspecified. 13122 */ 13123 if (lir->lir_maxmtu != 0 && 13124 (lir->lir_maxmtu > ill->ill_max_frag || 13125 lir->lir_maxmtu < ip_min_mtu)) 13126 return (EINVAL); 13127 if (lir->lir_reachtime != 0 && 13128 lir->lir_reachtime > ND_MAX_REACHTIME) 13129 return (EINVAL); 13130 if (lir->lir_reachretrans != 0 && 13131 lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME) 13132 return (EINVAL); 13133 13134 mutex_enter(&ill->ill_lock); 13135 ill->ill_state_flags |= ILL_CHANGING; 13136 for (nipif = ill->ill_ipif; nipif != NULL; 13137 nipif = nipif->ipif_next) { 13138 nipif->ipif_state_flags |= IPIF_CHANGING; 13139 } 13140 13141 mutex_exit(&ill->ill_lock); 13142 13143 if (lir->lir_maxmtu != 0) { 13144 ill->ill_max_mtu = lir->lir_maxmtu; 13145 ill->ill_mtu_userspecified = 1; 13146 mtu_walk = B_TRUE; 13147 } 13148 13149 if (lir->lir_reachtime != 0) 13150 ill->ill_reachable_time = lir->lir_reachtime; 13151 13152 if (lir->lir_reachretrans != 0) 13153 ill->ill_reachable_retrans_time = lir->lir_reachretrans; 13154 13155 ill->ill_max_hops = lir->lir_maxhops; 13156 13157 ill->ill_max_buf = ND_MAX_Q; 13158 13159 if (mtu_walk) { 13160 /* 13161 * Set the MTU on all ipifs associated with this ill except 13162 * for those whose MTU was fixed via SIOCSLIFMTU. 13163 */ 13164 for (nipif = ill->ill_ipif; nipif != NULL; 13165 nipif = nipif->ipif_next) { 13166 if (nipif->ipif_flags & IPIF_FIXEDMTU) 13167 continue; 13168 13169 nipif->ipif_mtu = ill->ill_max_mtu; 13170 13171 if (!(nipif->ipif_flags & IPIF_UP)) 13172 continue; 13173 13174 if (nipif->ipif_isv6) 13175 ire = ipif_to_ire_v6(nipif); 13176 else 13177 ire = ipif_to_ire(nipif); 13178 if (ire != NULL) { 13179 ire->ire_max_frag = ipif->ipif_mtu; 13180 ire_refrele(ire); 13181 } 13182 if (ill->ill_isv6) { 13183 ire_walk_ill_v6(MATCH_IRE_ILL, 0, 13184 ipif_mtu_change, (char *)nipif, 13185 ill); 13186 } else { 13187 ire_walk_ill_v4(MATCH_IRE_ILL, 0, 13188 ipif_mtu_change, (char *)nipif, 13189 ill); 13190 } 13191 } 13192 } 13193 13194 mutex_enter(&ill->ill_lock); 13195 for (nipif = ill->ill_ipif; nipif != NULL; 13196 nipif = nipif->ipif_next) { 13197 nipif->ipif_state_flags &= ~IPIF_CHANGING; 13198 } 13199 ILL_UNMARK_CHANGING(ill); 13200 mutex_exit(&ill->ill_lock); 13201 13202 return (0); 13203 } 13204 13205 /* ARGSUSED */ 13206 int 13207 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 13208 ip_ioctl_cmd_t *ipi, void *if_req) 13209 { 13210 struct lif_ifinfo_req *lir; 13211 ill_t *ill = ipif->ipif_ill; 13212 13213 ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n", 13214 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 13215 if (ipif->ipif_id != 0) 13216 return (EINVAL); 13217 13218 lir = &((struct lifreq *)if_req)->lifr_ifinfo; 13219 lir->lir_maxhops = ill->ill_max_hops; 13220 lir->lir_reachtime = ill->ill_reachable_time; 13221 lir->lir_reachretrans = ill->ill_reachable_retrans_time; 13222 lir->lir_maxmtu = ill->ill_max_mtu; 13223 13224 return (0); 13225 } 13226 13227 /* 13228 * Return best guess as to the subnet mask for the specified address. 13229 * Based on the subnet masks for all the configured interfaces. 13230 * 13231 * We end up returning a zero mask in the case of default, multicast or 13232 * experimental. 13233 */ 13234 static ipaddr_t 13235 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst) 13236 { 13237 ipaddr_t net_mask; 13238 ill_t *ill; 13239 ipif_t *ipif; 13240 ill_walk_context_t ctx; 13241 ipif_t *fallback_ipif = NULL; 13242 13243 net_mask = ip_net_mask(addr); 13244 if (net_mask == 0) { 13245 *ipifp = NULL; 13246 return (0); 13247 } 13248 13249 /* Let's check to see if this is maybe a local subnet route. */ 13250 /* this function only applies to IPv4 interfaces */ 13251 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 13252 ill = ILL_START_WALK_V4(&ctx, ipst); 13253 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 13254 mutex_enter(&ill->ill_lock); 13255 for (ipif = ill->ill_ipif; ipif != NULL; 13256 ipif = ipif->ipif_next) { 13257 if (!IPIF_CAN_LOOKUP(ipif)) 13258 continue; 13259 if (!(ipif->ipif_flags & IPIF_UP)) 13260 continue; 13261 if ((ipif->ipif_subnet & net_mask) == 13262 (addr & net_mask)) { 13263 /* 13264 * Don't trust pt-pt interfaces if there are 13265 * other interfaces. 13266 */ 13267 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 13268 if (fallback_ipif == NULL) { 13269 ipif_refhold_locked(ipif); 13270 fallback_ipif = ipif; 13271 } 13272 continue; 13273 } 13274 13275 /* 13276 * Fine. Just assume the same net mask as the 13277 * directly attached subnet interface is using. 13278 */ 13279 ipif_refhold_locked(ipif); 13280 mutex_exit(&ill->ill_lock); 13281 rw_exit(&ipst->ips_ill_g_lock); 13282 if (fallback_ipif != NULL) 13283 ipif_refrele(fallback_ipif); 13284 *ipifp = ipif; 13285 return (ipif->ipif_net_mask); 13286 } 13287 } 13288 mutex_exit(&ill->ill_lock); 13289 } 13290 rw_exit(&ipst->ips_ill_g_lock); 13291 13292 *ipifp = fallback_ipif; 13293 return ((fallback_ipif != NULL) ? 13294 fallback_ipif->ipif_net_mask : net_mask); 13295 } 13296 13297 /* 13298 * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl. 13299 */ 13300 static void 13301 ip_wput_ioctl(queue_t *q, mblk_t *mp) 13302 { 13303 IOCP iocp; 13304 ipft_t *ipft; 13305 ipllc_t *ipllc; 13306 mblk_t *mp1; 13307 cred_t *cr; 13308 int error = 0; 13309 conn_t *connp; 13310 13311 ip1dbg(("ip_wput_ioctl")); 13312 iocp = (IOCP)mp->b_rptr; 13313 mp1 = mp->b_cont; 13314 if (mp1 == NULL) { 13315 iocp->ioc_error = EINVAL; 13316 mp->b_datap->db_type = M_IOCNAK; 13317 iocp->ioc_count = 0; 13318 qreply(q, mp); 13319 return; 13320 } 13321 13322 /* 13323 * These IOCTLs provide various control capabilities to 13324 * upstream agents such as ULPs and processes. There 13325 * are currently two such IOCTLs implemented. They 13326 * are used by TCP to provide update information for 13327 * existing IREs and to forcibly delete an IRE for a 13328 * host that is not responding, thereby forcing an 13329 * attempt at a new route. 13330 */ 13331 iocp->ioc_error = EINVAL; 13332 if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd))) 13333 goto done; 13334 13335 ipllc = (ipllc_t *)mp1->b_rptr; 13336 for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) { 13337 if (ipllc->ipllc_cmd == ipft->ipft_cmd) 13338 break; 13339 } 13340 /* 13341 * prefer credential from mblk over ioctl; 13342 * see ip_sioctl_copyin_setup 13343 */ 13344 cr = DB_CREDDEF(mp, iocp->ioc_cr); 13345 13346 /* 13347 * Refhold the conn in case the request gets queued up in some lookup 13348 */ 13349 ASSERT(CONN_Q(q)); 13350 connp = Q_TO_CONN(q); 13351 CONN_INC_REF(connp); 13352 if (ipft->ipft_pfi && 13353 ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size || 13354 pullupmsg(mp1, ipft->ipft_min_size))) { 13355 error = (*ipft->ipft_pfi)(q, 13356 (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr); 13357 } 13358 if (ipft->ipft_flags & IPFT_F_SELF_REPLY) { 13359 /* 13360 * CONN_OPER_PENDING_DONE happens in the function called 13361 * through ipft_pfi above. 13362 */ 13363 return; 13364 } 13365 13366 CONN_OPER_PENDING_DONE(connp); 13367 if (ipft->ipft_flags & IPFT_F_NO_REPLY) { 13368 freemsg(mp); 13369 return; 13370 } 13371 iocp->ioc_error = error; 13372 13373 done: 13374 mp->b_datap->db_type = M_IOCACK; 13375 if (iocp->ioc_error) 13376 iocp->ioc_count = 0; 13377 qreply(q, mp); 13378 } 13379 13380 /* 13381 * Lookup an ipif using the sequence id (ipif_seqid) 13382 */ 13383 ipif_t * 13384 ipif_lookup_seqid(ill_t *ill, uint_t seqid) 13385 { 13386 ipif_t *ipif; 13387 13388 ASSERT(MUTEX_HELD(&ill->ill_lock)); 13389 13390 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 13391 if (ipif->ipif_seqid == seqid && IPIF_CAN_LOOKUP(ipif)) 13392 return (ipif); 13393 } 13394 return (NULL); 13395 } 13396 13397 /* 13398 * Assign a unique id for the ipif. This is used later when we send 13399 * IRES to ARP for resolution where we initialize ire_ipif_seqid 13400 * to the value pointed by ire_ipif->ipif_seqid. Later when the 13401 * IRE is added, we verify that ipif has not disappeared. 13402 */ 13403 13404 static void 13405 ipif_assign_seqid(ipif_t *ipif) 13406 { 13407 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 13408 13409 ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1); 13410 } 13411 13412 /* 13413 * Insert the ipif, so that the list of ipifs on the ill will be sorted 13414 * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will 13415 * be inserted into the first space available in the list. The value of 13416 * ipif_id will then be set to the appropriate value for its position. 13417 */ 13418 static int 13419 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock, boolean_t acquire_ill_lock) 13420 { 13421 ill_t *ill; 13422 ipif_t *tipif; 13423 ipif_t **tipifp; 13424 int id; 13425 ip_stack_t *ipst; 13426 13427 ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK || 13428 IAM_WRITER_IPIF(ipif)); 13429 13430 ill = ipif->ipif_ill; 13431 ASSERT(ill != NULL); 13432 ipst = ill->ill_ipst; 13433 13434 /* 13435 * In the case of lo0:0 we already hold the ill_g_lock. 13436 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate -> 13437 * ipif_insert. Another such caller is ipif_move. 13438 */ 13439 if (acquire_g_lock) 13440 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 13441 if (acquire_ill_lock) 13442 mutex_enter(&ill->ill_lock); 13443 id = ipif->ipif_id; 13444 tipifp = &(ill->ill_ipif); 13445 if (id == -1) { /* need to find a real id */ 13446 id = 0; 13447 while ((tipif = *tipifp) != NULL) { 13448 ASSERT(tipif->ipif_id >= id); 13449 if (tipif->ipif_id != id) 13450 break; /* non-consecutive id */ 13451 id++; 13452 tipifp = &(tipif->ipif_next); 13453 } 13454 /* limit number of logical interfaces */ 13455 if (id >= ipst->ips_ip_addrs_per_if) { 13456 if (acquire_ill_lock) 13457 mutex_exit(&ill->ill_lock); 13458 if (acquire_g_lock) 13459 rw_exit(&ipst->ips_ill_g_lock); 13460 return (-1); 13461 } 13462 ipif->ipif_id = id; /* assign new id */ 13463 } else if (id < ipst->ips_ip_addrs_per_if) { 13464 /* we have a real id; insert ipif in the right place */ 13465 while ((tipif = *tipifp) != NULL) { 13466 ASSERT(tipif->ipif_id != id); 13467 if (tipif->ipif_id > id) 13468 break; /* found correct location */ 13469 tipifp = &(tipif->ipif_next); 13470 } 13471 } else { 13472 if (acquire_ill_lock) 13473 mutex_exit(&ill->ill_lock); 13474 if (acquire_g_lock) 13475 rw_exit(&ipst->ips_ill_g_lock); 13476 return (-1); 13477 } 13478 13479 ASSERT(tipifp != &(ill->ill_ipif) || id == 0); 13480 13481 ipif->ipif_next = tipif; 13482 *tipifp = ipif; 13483 if (acquire_ill_lock) 13484 mutex_exit(&ill->ill_lock); 13485 if (acquire_g_lock) 13486 rw_exit(&ipst->ips_ill_g_lock); 13487 return (0); 13488 } 13489 13490 static void 13491 ipif_remove(ipif_t *ipif, boolean_t acquire_ill_lock) 13492 { 13493 ipif_t **ipifp; 13494 ill_t *ill = ipif->ipif_ill; 13495 13496 ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock)); 13497 if (acquire_ill_lock) 13498 mutex_enter(&ill->ill_lock); 13499 else 13500 ASSERT(MUTEX_HELD(&ill->ill_lock)); 13501 13502 ipifp = &ill->ill_ipif; 13503 for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) { 13504 if (*ipifp == ipif) { 13505 *ipifp = ipif->ipif_next; 13506 break; 13507 } 13508 } 13509 13510 if (acquire_ill_lock) 13511 mutex_exit(&ill->ill_lock); 13512 } 13513 13514 /* 13515 * Allocate and initialize a new interface control structure. (Always 13516 * called as writer.) 13517 * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill 13518 * is not part of the global linked list of ills. ipif_seqid is unique 13519 * in the system and to preserve the uniqueness, it is assigned only 13520 * when ill becomes part of the global list. At that point ill will 13521 * have a name. If it doesn't get assigned here, it will get assigned 13522 * in ipif_set_values() as part of SIOCSLIFNAME processing. 13523 * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set 13524 * the interface flags or any other information from the DL_INFO_ACK for 13525 * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at 13526 * this point. The flags etc. will be set in ip_ll_subnet_defaults when the 13527 * second DL_INFO_ACK comes in from the driver. 13528 */ 13529 static ipif_t * 13530 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize) 13531 { 13532 ipif_t *ipif; 13533 phyint_t *phyi; 13534 13535 ip1dbg(("ipif_allocate(%s:%d ill %p)\n", 13536 ill->ill_name, id, (void *)ill)); 13537 ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill)); 13538 13539 if ((ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) 13540 return (NULL); 13541 *ipif = ipif_zero; /* start clean */ 13542 13543 ipif->ipif_ill = ill; 13544 ipif->ipif_id = id; /* could be -1 */ 13545 /* 13546 * Inherit the zoneid from the ill; for the shared stack instance 13547 * this is always the global zone 13548 */ 13549 ipif->ipif_zoneid = ill->ill_zoneid; 13550 13551 mutex_init(&ipif->ipif_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL); 13552 13553 ipif->ipif_refcnt = 0; 13554 ipif->ipif_saved_ire_cnt = 0; 13555 13556 if (ipif_insert(ipif, ire_type != IRE_LOOPBACK, B_TRUE)) { 13557 mi_free(ipif); 13558 return (NULL); 13559 } 13560 /* -1 id should have been replaced by real id */ 13561 id = ipif->ipif_id; 13562 ASSERT(id >= 0); 13563 13564 if (ill->ill_name[0] != '\0') 13565 ipif_assign_seqid(ipif); 13566 13567 /* 13568 * Keep a copy of original id in ipif_orig_ipifid. Failback 13569 * will attempt to restore the original id. The SIOCSLIFOINDEX 13570 * ioctl sets ipif_orig_ipifid to zero. 13571 */ 13572 ipif->ipif_orig_ipifid = id; 13573 13574 /* 13575 * We grab the ill_lock and phyint_lock to protect the flag changes. 13576 * The ipif is still not up and can't be looked up until the 13577 * ioctl completes and the IPIF_CHANGING flag is cleared. 13578 */ 13579 mutex_enter(&ill->ill_lock); 13580 mutex_enter(&ill->ill_phyint->phyint_lock); 13581 /* 13582 * Set the running flag when logical interface zero is created. 13583 * For subsequent logical interfaces, a DLPI link down 13584 * notification message may have cleared the running flag to 13585 * indicate the link is down, so we shouldn't just blindly set it. 13586 */ 13587 if (id == 0) 13588 ill->ill_phyint->phyint_flags |= PHYI_RUNNING; 13589 ipif->ipif_ire_type = ire_type; 13590 phyi = ill->ill_phyint; 13591 ipif->ipif_orig_ifindex = phyi->phyint_ifindex; 13592 13593 if (ipif->ipif_isv6) { 13594 ill->ill_flags |= ILLF_IPV6; 13595 } else { 13596 ipaddr_t inaddr_any = INADDR_ANY; 13597 13598 ill->ill_flags |= ILLF_IPV4; 13599 13600 /* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */ 13601 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 13602 &ipif->ipif_v6lcl_addr); 13603 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 13604 &ipif->ipif_v6src_addr); 13605 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 13606 &ipif->ipif_v6subnet); 13607 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 13608 &ipif->ipif_v6net_mask); 13609 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 13610 &ipif->ipif_v6brd_addr); 13611 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 13612 &ipif->ipif_v6pp_dst_addr); 13613 } 13614 13615 /* 13616 * Don't set the interface flags etc. now, will do it in 13617 * ip_ll_subnet_defaults. 13618 */ 13619 if (!initialize) { 13620 mutex_exit(&ill->ill_lock); 13621 mutex_exit(&ill->ill_phyint->phyint_lock); 13622 return (ipif); 13623 } 13624 ipif->ipif_mtu = ill->ill_max_mtu; 13625 13626 if (ill->ill_bcast_addr_length != 0) { 13627 /* 13628 * Later detect lack of DLPI driver multicast 13629 * capability by catching DL_ENABMULTI errors in 13630 * ip_rput_dlpi. 13631 */ 13632 ill->ill_flags |= ILLF_MULTICAST; 13633 if (!ipif->ipif_isv6) 13634 ipif->ipif_flags |= IPIF_BROADCAST; 13635 } else { 13636 if (ill->ill_net_type != IRE_LOOPBACK) { 13637 if (ipif->ipif_isv6) 13638 /* 13639 * Note: xresolv interfaces will eventually need 13640 * NOARP set here as well, but that will require 13641 * those external resolvers to have some 13642 * knowledge of that flag and act appropriately. 13643 * Not to be changed at present. 13644 */ 13645 ill->ill_flags |= ILLF_NONUD; 13646 else 13647 ill->ill_flags |= ILLF_NOARP; 13648 } 13649 if (ill->ill_phys_addr_length == 0) { 13650 if (ill->ill_media && 13651 ill->ill_media->ip_m_mac_type == SUNW_DL_VNI) { 13652 ipif->ipif_flags |= IPIF_NOXMIT; 13653 phyi->phyint_flags |= PHYI_VIRTUAL; 13654 } else { 13655 /* pt-pt supports multicast. */ 13656 ill->ill_flags |= ILLF_MULTICAST; 13657 if (ill->ill_net_type == IRE_LOOPBACK) { 13658 phyi->phyint_flags |= 13659 (PHYI_LOOPBACK | PHYI_VIRTUAL); 13660 } else { 13661 ipif->ipif_flags |= IPIF_POINTOPOINT; 13662 } 13663 } 13664 } 13665 } 13666 mutex_exit(&ill->ill_lock); 13667 mutex_exit(&ill->ill_phyint->phyint_lock); 13668 return (ipif); 13669 } 13670 13671 /* 13672 * If appropriate, send a message up to the resolver delete the entry 13673 * for the address of this interface which is going out of business. 13674 * (Always called as writer). 13675 * 13676 * NOTE : We need to check for NULL mps as some of the fields are 13677 * initialized only for some interface types. See ipif_resolver_up() 13678 * for details. 13679 */ 13680 void 13681 ipif_arp_down(ipif_t *ipif) 13682 { 13683 mblk_t *mp; 13684 ill_t *ill = ipif->ipif_ill; 13685 13686 ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 13687 ASSERT(IAM_WRITER_IPIF(ipif)); 13688 13689 /* Delete the mapping for the local address */ 13690 mp = ipif->ipif_arp_del_mp; 13691 if (mp != NULL) { 13692 ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n", 13693 *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id)); 13694 putnext(ill->ill_rq, mp); 13695 ipif->ipif_arp_del_mp = NULL; 13696 } 13697 13698 /* 13699 * If this is the last ipif that is going down and there are no 13700 * duplicate addresses we may yet attempt to re-probe, then we need to 13701 * clean up ARP completely. 13702 */ 13703 if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0) { 13704 13705 /* Send up AR_INTERFACE_DOWN message */ 13706 mp = ill->ill_arp_down_mp; 13707 if (mp != NULL) { 13708 ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n", 13709 *(unsigned *)mp->b_rptr, ill->ill_name, 13710 ipif->ipif_id)); 13711 putnext(ill->ill_rq, mp); 13712 ill->ill_arp_down_mp = NULL; 13713 } 13714 13715 /* Tell ARP to delete the multicast mappings */ 13716 mp = ill->ill_arp_del_mapping_mp; 13717 if (mp != NULL) { 13718 ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n", 13719 *(unsigned *)mp->b_rptr, ill->ill_name, 13720 ipif->ipif_id)); 13721 putnext(ill->ill_rq, mp); 13722 ill->ill_arp_del_mapping_mp = NULL; 13723 } 13724 } 13725 } 13726 13727 /* 13728 * This function sets up the multicast mappings in ARP. When ipif_resolver_up 13729 * calls this function, it passes a non-NULL arp_add_mapping_mp indicating 13730 * that it wants the add_mp allocated in this function to be returned 13731 * wihtout sending it to arp. When ip_rput_dlpi_writer calls this to 13732 * just re-do the multicast, it wants us to send the add_mp to ARP also. 13733 * ipif_resolver_up does not want us to do the "add" i.e sending to ARP, 13734 * as it does a ipif_arp_down after calling this function - which will 13735 * remove what we add here. 13736 * 13737 * Returns -1 on failures and 0 on success. 13738 */ 13739 int 13740 ipif_arp_setup_multicast(ipif_t *ipif, mblk_t **arp_add_mapping_mp) 13741 { 13742 mblk_t *del_mp = NULL; 13743 mblk_t *add_mp = NULL; 13744 mblk_t *mp; 13745 ill_t *ill = ipif->ipif_ill; 13746 phyint_t *phyi = ill->ill_phyint; 13747 ipaddr_t addr, mask, extract_mask = 0; 13748 arma_t *arma; 13749 uint8_t *maddr, *bphys_addr; 13750 uint32_t hw_start; 13751 dl_unitdata_req_t *dlur; 13752 13753 ASSERT(IAM_WRITER_IPIF(ipif)); 13754 if (ipif->ipif_flags & IPIF_POINTOPOINT) 13755 return (0); 13756 13757 /* 13758 * Delete the existing mapping from ARP. Normally ipif_down 13759 * -> ipif_arp_down should send this up to ARP. The only 13760 * reason we would find this when we are switching from 13761 * Multicast to Broadcast where we did not do a down. 13762 */ 13763 mp = ill->ill_arp_del_mapping_mp; 13764 if (mp != NULL) { 13765 ip1dbg(("ipif_arp_down: arp cmd %x for %s:%u\n", 13766 *(unsigned *)mp->b_rptr, ill->ill_name, ipif->ipif_id)); 13767 putnext(ill->ill_rq, mp); 13768 ill->ill_arp_del_mapping_mp = NULL; 13769 } 13770 13771 if (arp_add_mapping_mp != NULL) 13772 *arp_add_mapping_mp = NULL; 13773 13774 /* 13775 * Check that the address is not to long for the constant 13776 * length reserved in the template arma_t. 13777 */ 13778 if (ill->ill_phys_addr_length > IP_MAX_HW_LEN) 13779 return (-1); 13780 13781 /* Add mapping mblk */ 13782 addr = (ipaddr_t)htonl(INADDR_UNSPEC_GROUP); 13783 mask = (ipaddr_t)htonl(IN_CLASSD_NET); 13784 add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_arma_multi_template, 13785 (caddr_t)&addr); 13786 if (add_mp == NULL) 13787 return (-1); 13788 arma = (arma_t *)add_mp->b_rptr; 13789 maddr = (uint8_t *)arma + arma->arma_hw_addr_offset; 13790 bcopy(&mask, (char *)arma + arma->arma_proto_mask_offset, IP_ADDR_LEN); 13791 arma->arma_hw_addr_length = ill->ill_phys_addr_length; 13792 13793 /* 13794 * Determine the broadcast address. 13795 */ 13796 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr; 13797 if (ill->ill_sap_length < 0) 13798 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset; 13799 else 13800 bphys_addr = (uchar_t *)dlur + 13801 dlur->dl_dest_addr_offset + ill->ill_sap_length; 13802 /* 13803 * Check PHYI_MULTI_BCAST and length of physical 13804 * address to determine if we use the mapping or the 13805 * broadcast address. 13806 */ 13807 if (!(phyi->phyint_flags & PHYI_MULTI_BCAST)) 13808 if (!MEDIA_V4MINFO(ill->ill_media, ill->ill_phys_addr_length, 13809 bphys_addr, maddr, &hw_start, &extract_mask)) 13810 phyi->phyint_flags |= PHYI_MULTI_BCAST; 13811 13812 if ((phyi->phyint_flags & PHYI_MULTI_BCAST) || 13813 (ill->ill_flags & ILLF_MULTICAST)) { 13814 /* Make sure this will not match the "exact" entry. */ 13815 addr = (ipaddr_t)htonl(INADDR_ALLHOSTS_GROUP); 13816 del_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ared_template, 13817 (caddr_t)&addr); 13818 if (del_mp == NULL) { 13819 freemsg(add_mp); 13820 return (-1); 13821 } 13822 bcopy(&extract_mask, (char *)arma + 13823 arma->arma_proto_extract_mask_offset, IP_ADDR_LEN); 13824 if (phyi->phyint_flags & PHYI_MULTI_BCAST) { 13825 /* Use link-layer broadcast address for MULTI_BCAST */ 13826 bcopy(bphys_addr, maddr, ill->ill_phys_addr_length); 13827 ip2dbg(("ipif_arp_setup_multicast: adding" 13828 " MULTI_BCAST ARP setup for %s\n", ill->ill_name)); 13829 } else { 13830 arma->arma_hw_mapping_start = hw_start; 13831 ip2dbg(("ipif_arp_setup_multicast: adding multicast" 13832 " ARP setup for %s\n", ill->ill_name)); 13833 } 13834 } else { 13835 freemsg(add_mp); 13836 ASSERT(del_mp == NULL); 13837 /* It is neither MULTICAST nor MULTI_BCAST */ 13838 return (0); 13839 } 13840 ASSERT(add_mp != NULL && del_mp != NULL); 13841 ASSERT(ill->ill_arp_del_mapping_mp == NULL); 13842 ill->ill_arp_del_mapping_mp = del_mp; 13843 if (arp_add_mapping_mp != NULL) { 13844 /* The caller just wants the mblks allocated */ 13845 *arp_add_mapping_mp = add_mp; 13846 } else { 13847 /* The caller wants us to send it to arp */ 13848 putnext(ill->ill_rq, add_mp); 13849 } 13850 return (0); 13851 } 13852 13853 /* 13854 * Get the resolver set up for a new interface address. 13855 * (Always called as writer.) 13856 * Called both for IPv4 and IPv6 interfaces, 13857 * though it only sets up the resolver for v6 13858 * if it's an xresolv interface (one using an external resolver). 13859 * Honors ILLF_NOARP. 13860 * The enumerated value res_act is used to tune the behavior. 13861 * If set to Res_act_initial, then we set up all the resolver 13862 * structures for a new interface. If set to Res_act_move, then 13863 * we just send an AR_ENTRY_ADD message up to ARP for IPv4 13864 * interfaces; this is called by ip_rput_dlpi_writer() to handle 13865 * asynchronous hardware address change notification. If set to 13866 * Res_act_defend, then we tell ARP that it needs to send a single 13867 * gratuitous message in defense of the address. 13868 * Returns error on failure. 13869 */ 13870 int 13871 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act) 13872 { 13873 caddr_t addr; 13874 mblk_t *arp_up_mp = NULL; 13875 mblk_t *arp_down_mp = NULL; 13876 mblk_t *arp_add_mp = NULL; 13877 mblk_t *arp_del_mp = NULL; 13878 mblk_t *arp_add_mapping_mp = NULL; 13879 mblk_t *arp_del_mapping_mp = NULL; 13880 ill_t *ill = ipif->ipif_ill; 13881 uchar_t *area_p = NULL; 13882 uchar_t *ared_p = NULL; 13883 int err = ENOMEM; 13884 boolean_t was_dup; 13885 13886 ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n", 13887 ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags)); 13888 ASSERT(IAM_WRITER_IPIF(ipif)); 13889 13890 was_dup = B_FALSE; 13891 if (res_act == Res_act_initial) { 13892 ipif->ipif_addr_ready = 0; 13893 /* 13894 * We're bringing an interface up here. There's no way that we 13895 * should need to shut down ARP now. 13896 */ 13897 mutex_enter(&ill->ill_lock); 13898 if (ipif->ipif_flags & IPIF_DUPLICATE) { 13899 ipif->ipif_flags &= ~IPIF_DUPLICATE; 13900 ill->ill_ipif_dup_count--; 13901 was_dup = B_TRUE; 13902 } 13903 mutex_exit(&ill->ill_lock); 13904 } 13905 if (ipif->ipif_recovery_id != 0) 13906 (void) untimeout(ipif->ipif_recovery_id); 13907 ipif->ipif_recovery_id = 0; 13908 if (ill->ill_net_type != IRE_IF_RESOLVER) { 13909 ipif->ipif_addr_ready = 1; 13910 return (0); 13911 } 13912 /* NDP will set the ipif_addr_ready flag when it's ready */ 13913 if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV)) 13914 return (0); 13915 13916 if (ill->ill_isv6) { 13917 /* 13918 * External resolver for IPv6 13919 */ 13920 ASSERT(res_act == Res_act_initial); 13921 if (!IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) { 13922 addr = (caddr_t)&ipif->ipif_v6lcl_addr; 13923 area_p = (uchar_t *)&ip6_area_template; 13924 ared_p = (uchar_t *)&ip6_ared_template; 13925 } 13926 } else { 13927 /* 13928 * IPv4 arp case. If the ARP stream has already started 13929 * closing, fail this request for ARP bringup. Else 13930 * record the fact that an ARP bringup is pending. 13931 */ 13932 mutex_enter(&ill->ill_lock); 13933 if (ill->ill_arp_closing) { 13934 mutex_exit(&ill->ill_lock); 13935 err = EINVAL; 13936 goto failed; 13937 } else { 13938 if (ill->ill_ipif_up_count == 0 && 13939 ill->ill_ipif_dup_count == 0 && !was_dup) 13940 ill->ill_arp_bringup_pending = 1; 13941 mutex_exit(&ill->ill_lock); 13942 } 13943 if (ipif->ipif_lcl_addr != INADDR_ANY) { 13944 addr = (caddr_t)&ipif->ipif_lcl_addr; 13945 area_p = (uchar_t *)&ip_area_template; 13946 ared_p = (uchar_t *)&ip_ared_template; 13947 } 13948 } 13949 13950 /* 13951 * Add an entry for the local address in ARP only if it 13952 * is not UNNUMBERED and the address is not INADDR_ANY. 13953 */ 13954 if (!(ipif->ipif_flags & IPIF_UNNUMBERED) && area_p != NULL) { 13955 area_t *area; 13956 13957 /* Now ask ARP to publish our address. */ 13958 arp_add_mp = ill_arp_alloc(ill, area_p, addr); 13959 if (arp_add_mp == NULL) 13960 goto failed; 13961 area = (area_t *)arp_add_mp->b_rptr; 13962 if (res_act != Res_act_initial) { 13963 /* 13964 * Copy the new hardware address and length into 13965 * arp_add_mp to be sent to ARP. 13966 */ 13967 area->area_hw_addr_length = ill->ill_phys_addr_length; 13968 bcopy(ill->ill_phys_addr, 13969 ((char *)area + area->area_hw_addr_offset), 13970 area->area_hw_addr_length); 13971 } 13972 13973 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | 13974 ACE_F_MYADDR; 13975 13976 if (res_act == Res_act_defend) { 13977 area->area_flags |= ACE_F_DEFEND; 13978 /* 13979 * If we're just defending our address now, then 13980 * there's no need to set up ARP multicast mappings. 13981 * The publish command is enough. 13982 */ 13983 goto done; 13984 } 13985 13986 if (res_act != Res_act_initial) 13987 goto arp_setup_multicast; 13988 13989 /* 13990 * Allocate an ARP deletion message so we know we can tell ARP 13991 * when the interface goes down. 13992 */ 13993 arp_del_mp = ill_arp_alloc(ill, ared_p, addr); 13994 if (arp_del_mp == NULL) 13995 goto failed; 13996 13997 } else { 13998 if (res_act != Res_act_initial) 13999 goto done; 14000 } 14001 /* 14002 * Need to bring up ARP or setup multicast mapping only 14003 * when the first interface is coming UP. 14004 */ 14005 if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 || 14006 was_dup) { 14007 goto done; 14008 } 14009 14010 /* 14011 * Allocate an ARP down message (to be saved) and an ARP up 14012 * message. 14013 */ 14014 arp_down_mp = ill_arp_alloc(ill, (uchar_t *)&ip_ard_template, 0); 14015 if (arp_down_mp == NULL) 14016 goto failed; 14017 14018 arp_up_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aru_template, 0); 14019 if (arp_up_mp == NULL) 14020 goto failed; 14021 14022 if (ipif->ipif_flags & IPIF_POINTOPOINT) 14023 goto done; 14024 14025 arp_setup_multicast: 14026 /* 14027 * Setup the multicast mappings. This function initializes 14028 * ill_arp_del_mapping_mp also. This does not need to be done for 14029 * IPv6. 14030 */ 14031 if (!ill->ill_isv6) { 14032 err = ipif_arp_setup_multicast(ipif, &arp_add_mapping_mp); 14033 if (err != 0) 14034 goto failed; 14035 ASSERT(ill->ill_arp_del_mapping_mp != NULL); 14036 ASSERT(arp_add_mapping_mp != NULL); 14037 } 14038 14039 done: 14040 if (arp_del_mp != NULL) { 14041 ASSERT(ipif->ipif_arp_del_mp == NULL); 14042 ipif->ipif_arp_del_mp = arp_del_mp; 14043 } 14044 if (arp_down_mp != NULL) { 14045 ASSERT(ill->ill_arp_down_mp == NULL); 14046 ill->ill_arp_down_mp = arp_down_mp; 14047 } 14048 if (arp_del_mapping_mp != NULL) { 14049 ASSERT(ill->ill_arp_del_mapping_mp == NULL); 14050 ill->ill_arp_del_mapping_mp = arp_del_mapping_mp; 14051 } 14052 if (arp_up_mp != NULL) { 14053 ip1dbg(("ipif_resolver_up: ARP_UP for %s:%u\n", 14054 ill->ill_name, ipif->ipif_id)); 14055 putnext(ill->ill_rq, arp_up_mp); 14056 } 14057 if (arp_add_mp != NULL) { 14058 ip1dbg(("ipif_resolver_up: ARP_ADD for %s:%u\n", 14059 ill->ill_name, ipif->ipif_id)); 14060 /* 14061 * If it's an extended ARP implementation, then we'll wait to 14062 * hear that DAD has finished before using the interface. 14063 */ 14064 if (!ill->ill_arp_extend) 14065 ipif->ipif_addr_ready = 1; 14066 putnext(ill->ill_rq, arp_add_mp); 14067 } else { 14068 ipif->ipif_addr_ready = 1; 14069 } 14070 if (arp_add_mapping_mp != NULL) { 14071 ip1dbg(("ipif_resolver_up: MAPPING_ADD for %s:%u\n", 14072 ill->ill_name, ipif->ipif_id)); 14073 putnext(ill->ill_rq, arp_add_mapping_mp); 14074 } 14075 if (res_act != Res_act_initial) 14076 return (0); 14077 14078 if (ill->ill_flags & ILLF_NOARP) 14079 err = ill_arp_off(ill); 14080 else 14081 err = ill_arp_on(ill); 14082 if (err != 0) { 14083 ip0dbg(("ipif_resolver_up: arp_on/off failed %d\n", err)); 14084 freemsg(ipif->ipif_arp_del_mp); 14085 freemsg(ill->ill_arp_down_mp); 14086 freemsg(ill->ill_arp_del_mapping_mp); 14087 ipif->ipif_arp_del_mp = NULL; 14088 ill->ill_arp_down_mp = NULL; 14089 ill->ill_arp_del_mapping_mp = NULL; 14090 return (err); 14091 } 14092 return ((ill->ill_ipif_up_count != 0 || was_dup || 14093 ill->ill_ipif_dup_count != 0) ? 0 : EINPROGRESS); 14094 14095 failed: 14096 ip1dbg(("ipif_resolver_up: FAILED\n")); 14097 freemsg(arp_add_mp); 14098 freemsg(arp_del_mp); 14099 freemsg(arp_add_mapping_mp); 14100 freemsg(arp_up_mp); 14101 freemsg(arp_down_mp); 14102 ill->ill_arp_bringup_pending = 0; 14103 return (err); 14104 } 14105 14106 /* 14107 * This routine restarts IPv4 duplicate address detection (DAD) when a link has 14108 * just gone back up. 14109 */ 14110 static void 14111 ipif_arp_start_dad(ipif_t *ipif) 14112 { 14113 ill_t *ill = ipif->ipif_ill; 14114 mblk_t *arp_add_mp; 14115 area_t *area; 14116 14117 if (ill->ill_net_type != IRE_IF_RESOLVER || ill->ill_arp_closing || 14118 (ipif->ipif_flags & IPIF_UNNUMBERED) || 14119 ipif->ipif_lcl_addr == INADDR_ANY || 14120 (arp_add_mp = ill_arp_alloc(ill, (uchar_t *)&ip_area_template, 14121 (char *)&ipif->ipif_lcl_addr)) == NULL) { 14122 /* 14123 * If we can't contact ARP for some reason, that's not really a 14124 * problem. Just send out the routing socket notification that 14125 * DAD completion would have done, and continue. 14126 */ 14127 ipif_mask_reply(ipif); 14128 ip_rts_ifmsg(ipif); 14129 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 14130 sctp_update_ipif(ipif, SCTP_IPIF_UP); 14131 ipif->ipif_addr_ready = 1; 14132 return; 14133 } 14134 14135 /* Setting the 'unverified' flag restarts DAD */ 14136 area = (area_t *)arp_add_mp->b_rptr; 14137 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR | 14138 ACE_F_UNVERIFIED; 14139 putnext(ill->ill_rq, arp_add_mp); 14140 } 14141 14142 static void 14143 ipif_ndp_start_dad(ipif_t *ipif) 14144 { 14145 nce_t *nce; 14146 14147 nce = ndp_lookup_v6(ipif->ipif_ill, &ipif->ipif_v6lcl_addr, B_FALSE); 14148 if (nce == NULL) 14149 return; 14150 14151 if (!ndp_restart_dad(nce)) { 14152 /* 14153 * If we can't restart DAD for some reason, that's not really a 14154 * problem. Just send out the routing socket notification that 14155 * DAD completion would have done, and continue. 14156 */ 14157 ip_rts_ifmsg(ipif); 14158 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 14159 sctp_update_ipif(ipif, SCTP_IPIF_UP); 14160 ipif->ipif_addr_ready = 1; 14161 } 14162 NCE_REFRELE(nce); 14163 } 14164 14165 /* 14166 * Restart duplicate address detection on all interfaces on the given ill. 14167 * 14168 * This is called when an interface transitions from down to up 14169 * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN). 14170 * 14171 * Note that since the underlying physical link has transitioned, we must cause 14172 * at least one routing socket message to be sent here, either via DAD 14173 * completion or just by default on the first ipif. (If we don't do this, then 14174 * in.mpathd will see long delays when doing link-based failure recovery.) 14175 */ 14176 void 14177 ill_restart_dad(ill_t *ill, boolean_t went_up) 14178 { 14179 ipif_t *ipif; 14180 14181 if (ill == NULL) 14182 return; 14183 14184 /* 14185 * If layer two doesn't support duplicate address detection, then just 14186 * send the routing socket message now and be done with it. 14187 */ 14188 if ((ill->ill_isv6 && (ill->ill_flags & ILLF_XRESOLV)) || 14189 (!ill->ill_isv6 && !ill->ill_arp_extend)) { 14190 ip_rts_ifmsg(ill->ill_ipif); 14191 return; 14192 } 14193 14194 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 14195 if (went_up) { 14196 if (ipif->ipif_flags & IPIF_UP) { 14197 if (ill->ill_isv6) 14198 ipif_ndp_start_dad(ipif); 14199 else 14200 ipif_arp_start_dad(ipif); 14201 } else if (ill->ill_isv6 && 14202 (ipif->ipif_flags & IPIF_DUPLICATE)) { 14203 /* 14204 * For IPv4, the ARP module itself will 14205 * automatically start the DAD process when it 14206 * sees DL_NOTE_LINK_UP. We respond to the 14207 * AR_CN_READY at the completion of that task. 14208 * For IPv6, we must kick off the bring-up 14209 * process now. 14210 */ 14211 ndp_do_recovery(ipif); 14212 } else { 14213 /* 14214 * Unfortunately, the first ipif is "special" 14215 * and represents the underlying ill in the 14216 * routing socket messages. Thus, when this 14217 * one ipif is down, we must still notify so 14218 * that the user knows the IFF_RUNNING status 14219 * change. (If the first ipif is up, then 14220 * we'll handle eventual routing socket 14221 * notification via DAD completion.) 14222 */ 14223 if (ipif == ill->ill_ipif) 14224 ip_rts_ifmsg(ill->ill_ipif); 14225 } 14226 } else { 14227 /* 14228 * After link down, we'll need to send a new routing 14229 * message when the link comes back, so clear 14230 * ipif_addr_ready. 14231 */ 14232 ipif->ipif_addr_ready = 0; 14233 } 14234 } 14235 14236 /* 14237 * If we've torn down links, then notify the user right away. 14238 */ 14239 if (!went_up) 14240 ip_rts_ifmsg(ill->ill_ipif); 14241 } 14242 14243 /* 14244 * Wakeup all threads waiting to enter the ipsq, and sleeping 14245 * on any of the ills in this ipsq. The ill_lock of the ill 14246 * must be held so that waiters don't miss wakeups 14247 */ 14248 static void 14249 ill_signal_ipsq_ills(ipsq_t *ipsq, boolean_t caller_holds_lock) 14250 { 14251 phyint_t *phyint; 14252 14253 phyint = ipsq->ipsq_phyint_list; 14254 while (phyint != NULL) { 14255 if (phyint->phyint_illv4) { 14256 if (!caller_holds_lock) 14257 mutex_enter(&phyint->phyint_illv4->ill_lock); 14258 ASSERT(MUTEX_HELD(&phyint->phyint_illv4->ill_lock)); 14259 cv_broadcast(&phyint->phyint_illv4->ill_cv); 14260 if (!caller_holds_lock) 14261 mutex_exit(&phyint->phyint_illv4->ill_lock); 14262 } 14263 if (phyint->phyint_illv6) { 14264 if (!caller_holds_lock) 14265 mutex_enter(&phyint->phyint_illv6->ill_lock); 14266 ASSERT(MUTEX_HELD(&phyint->phyint_illv6->ill_lock)); 14267 cv_broadcast(&phyint->phyint_illv6->ill_cv); 14268 if (!caller_holds_lock) 14269 mutex_exit(&phyint->phyint_illv6->ill_lock); 14270 } 14271 phyint = phyint->phyint_ipsq_next; 14272 } 14273 } 14274 14275 static ipsq_t * 14276 ipsq_create(char *groupname, ip_stack_t *ipst) 14277 { 14278 ipsq_t *ipsq; 14279 14280 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 14281 ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP); 14282 if (ipsq == NULL) { 14283 return (NULL); 14284 } 14285 14286 if (groupname != NULL) 14287 (void) strcpy(ipsq->ipsq_name, groupname); 14288 else 14289 ipsq->ipsq_name[0] = '\0'; 14290 14291 mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, NULL); 14292 ipsq->ipsq_flags |= IPSQ_GROUP; 14293 ipsq->ipsq_next = ipst->ips_ipsq_g_head; 14294 ipst->ips_ipsq_g_head = ipsq; 14295 ipsq->ipsq_ipst = ipst; /* No netstack_hold */ 14296 return (ipsq); 14297 } 14298 14299 /* 14300 * Return an ipsq correspoding to the groupname. If 'create' is true 14301 * allocate a new ipsq if one does not exist. Usually an ipsq is associated 14302 * uniquely with an IPMP group. However during IPMP groupname operations, 14303 * multiple IPMP groups may be associated with a single ipsq. But no 14304 * IPMP group can be associated with more than 1 ipsq at any time. 14305 * For example 14306 * Interfaces IPMP grpname ipsq ipsq_name ipsq_refs 14307 * hme1, hme2 mpk17-84 ipsq1 mpk17-84 2 14308 * hme3, hme4 mpk17-85 ipsq2 mpk17-85 2 14309 * 14310 * Now the command ifconfig hme3 group mpk17-84 results in the temporary 14311 * status shown below during the execution of the above command. 14312 * hme1, hme2, hme3, hme4 mpk17-84, mpk17-85 ipsq1 mpk17-84 4 14313 * 14314 * After the completion of the above groupname command we return to the stable 14315 * state shown below. 14316 * hme1, hme2, hme3 mpk17-84 ipsq1 mpk17-84 3 14317 * hme4 mpk17-85 ipsq2 mpk17-85 1 14318 * 14319 * Because of the above, we don't search based on the ipsq_name since that 14320 * would miss the correct ipsq during certain windows as shown above. 14321 * The ipsq_name is only used during split of an ipsq to return the ipsq to its 14322 * natural state. 14323 */ 14324 static ipsq_t * 14325 ip_ipsq_lookup(char *groupname, boolean_t create, ipsq_t *exclude_ipsq, 14326 ip_stack_t *ipst) 14327 { 14328 ipsq_t *ipsq; 14329 int group_len; 14330 phyint_t *phyint; 14331 14332 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock)); 14333 14334 group_len = strlen(groupname); 14335 ASSERT(group_len != 0); 14336 group_len++; 14337 14338 for (ipsq = ipst->ips_ipsq_g_head; 14339 ipsq != NULL; 14340 ipsq = ipsq->ipsq_next) { 14341 /* 14342 * When an ipsq is being split, and ill_split_ipsq 14343 * calls this function, we exclude it from being considered. 14344 */ 14345 if (ipsq == exclude_ipsq) 14346 continue; 14347 14348 /* 14349 * Compare against the ipsq_name. The groupname change happens 14350 * in 2 phases. The 1st phase merges the from group into 14351 * the to group's ipsq, by calling ill_merge_groups and restarts 14352 * the ioctl. The 2nd phase then locates the ipsq again thru 14353 * ipsq_name. At this point the phyint_groupname has not been 14354 * updated. 14355 */ 14356 if ((group_len == strlen(ipsq->ipsq_name) + 1) && 14357 (bcmp(ipsq->ipsq_name, groupname, group_len) == 0)) { 14358 /* 14359 * Verify that an ipmp groupname is exactly 14360 * part of 1 ipsq and is not found in any other 14361 * ipsq. 14362 */ 14363 ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, ipst) == 14364 NULL); 14365 return (ipsq); 14366 } 14367 14368 /* 14369 * Comparison against ipsq_name alone is not sufficient. 14370 * In the case when groups are currently being 14371 * merged, the ipsq could hold other IPMP groups temporarily. 14372 * so we walk the phyint list and compare against the 14373 * phyint_groupname as well. 14374 */ 14375 phyint = ipsq->ipsq_phyint_list; 14376 while (phyint != NULL) { 14377 if ((group_len == phyint->phyint_groupname_len) && 14378 (bcmp(phyint->phyint_groupname, groupname, 14379 group_len) == 0)) { 14380 /* 14381 * Verify that an ipmp groupname is exactly 14382 * part of 1 ipsq and is not found in any other 14383 * ipsq. 14384 */ 14385 ASSERT(ip_ipsq_lookup(groupname, B_FALSE, ipsq, 14386 ipst) == NULL); 14387 return (ipsq); 14388 } 14389 phyint = phyint->phyint_ipsq_next; 14390 } 14391 } 14392 if (create) 14393 ipsq = ipsq_create(groupname, ipst); 14394 return (ipsq); 14395 } 14396 14397 static void 14398 ipsq_delete(ipsq_t *ipsq) 14399 { 14400 ipsq_t *nipsq; 14401 ipsq_t *pipsq = NULL; 14402 ip_stack_t *ipst = ipsq->ipsq_ipst; 14403 14404 /* 14405 * We don't hold the ipsq lock, but we are sure no new 14406 * messages can land up, since the ipsq_refs is zero. 14407 * i.e. this ipsq is unnamed and no phyint or phyint group 14408 * is associated with this ipsq. (Lookups are based on ill_name 14409 * or phyint_groupname) 14410 */ 14411 ASSERT(ipsq->ipsq_refs == 0); 14412 ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipsq->ipsq_mphead == NULL); 14413 ASSERT(ipsq->ipsq_pending_mp == NULL); 14414 if (!(ipsq->ipsq_flags & IPSQ_GROUP)) { 14415 /* 14416 * This is not the ipsq of an IPMP group. 14417 */ 14418 ipsq->ipsq_ipst = NULL; 14419 kmem_free(ipsq, sizeof (ipsq_t)); 14420 return; 14421 } 14422 14423 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 14424 14425 /* 14426 * Locate the ipsq before we can remove it from 14427 * the singly linked list of ipsq's. 14428 */ 14429 for (nipsq = ipst->ips_ipsq_g_head; nipsq != NULL; 14430 nipsq = nipsq->ipsq_next) { 14431 if (nipsq == ipsq) { 14432 break; 14433 } 14434 pipsq = nipsq; 14435 } 14436 14437 ASSERT(nipsq == ipsq); 14438 14439 /* unlink ipsq from the list */ 14440 if (pipsq != NULL) 14441 pipsq->ipsq_next = ipsq->ipsq_next; 14442 else 14443 ipst->ips_ipsq_g_head = ipsq->ipsq_next; 14444 ipsq->ipsq_ipst = NULL; 14445 kmem_free(ipsq, sizeof (ipsq_t)); 14446 rw_exit(&ipst->ips_ill_g_lock); 14447 } 14448 14449 static void 14450 ill_move_to_new_ipsq(ipsq_t *old_ipsq, ipsq_t *new_ipsq, mblk_t *current_mp, 14451 queue_t *q) 14452 { 14453 ASSERT(MUTEX_HELD(&new_ipsq->ipsq_lock)); 14454 ASSERT(old_ipsq->ipsq_mphead == NULL && old_ipsq->ipsq_mptail == NULL); 14455 ASSERT(old_ipsq->ipsq_pending_ipif == NULL); 14456 ASSERT(old_ipsq->ipsq_pending_mp == NULL); 14457 ASSERT(current_mp != NULL); 14458 14459 ipsq_enq(new_ipsq, q, current_mp, (ipsq_func_t)ip_process_ioctl, 14460 NEW_OP, NULL); 14461 14462 ASSERT(new_ipsq->ipsq_xopq_mptail != NULL && 14463 new_ipsq->ipsq_xopq_mphead != NULL); 14464 14465 /* 14466 * move from old ipsq to the new ipsq. 14467 */ 14468 new_ipsq->ipsq_xopq_mptail->b_next = old_ipsq->ipsq_xopq_mphead; 14469 if (old_ipsq->ipsq_xopq_mphead != NULL) 14470 new_ipsq->ipsq_xopq_mptail = old_ipsq->ipsq_xopq_mptail; 14471 14472 old_ipsq->ipsq_xopq_mphead = old_ipsq->ipsq_xopq_mptail = NULL; 14473 } 14474 14475 void 14476 ill_group_cleanup(ill_t *ill) 14477 { 14478 ill_t *ill_v4; 14479 ill_t *ill_v6; 14480 ipif_t *ipif; 14481 14482 ill_v4 = ill->ill_phyint->phyint_illv4; 14483 ill_v6 = ill->ill_phyint->phyint_illv6; 14484 14485 if (ill_v4 != NULL) { 14486 mutex_enter(&ill_v4->ill_lock); 14487 for (ipif = ill_v4->ill_ipif; ipif != NULL; 14488 ipif = ipif->ipif_next) { 14489 IPIF_UNMARK_MOVING(ipif); 14490 } 14491 ill_v4->ill_up_ipifs = B_FALSE; 14492 mutex_exit(&ill_v4->ill_lock); 14493 } 14494 14495 if (ill_v6 != NULL) { 14496 mutex_enter(&ill_v6->ill_lock); 14497 for (ipif = ill_v6->ill_ipif; ipif != NULL; 14498 ipif = ipif->ipif_next) { 14499 IPIF_UNMARK_MOVING(ipif); 14500 } 14501 ill_v6->ill_up_ipifs = B_FALSE; 14502 mutex_exit(&ill_v6->ill_lock); 14503 } 14504 } 14505 /* 14506 * This function is called when an ill has had a change in its group status 14507 * to bring up all the ipifs that were up before the change. 14508 */ 14509 int 14510 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp) 14511 { 14512 ipif_t *ipif; 14513 ill_t *ill_v4; 14514 ill_t *ill_v6; 14515 ill_t *from_ill; 14516 int err = 0; 14517 14518 14519 ASSERT(IAM_WRITER_ILL(ill)); 14520 14521 /* 14522 * Except for ipif_state_flags and ill_state_flags the other 14523 * fields of the ipif/ill that are modified below are protected 14524 * implicitly since we are a writer. We would have tried to down 14525 * even an ipif that was already down, in ill_down_ipifs. So we 14526 * just blindly clear the IPIF_CHANGING flag here on all ipifs. 14527 */ 14528 ill_v4 = ill->ill_phyint->phyint_illv4; 14529 ill_v6 = ill->ill_phyint->phyint_illv6; 14530 if (ill_v4 != NULL) { 14531 ill_v4->ill_up_ipifs = B_TRUE; 14532 for (ipif = ill_v4->ill_ipif; ipif != NULL; 14533 ipif = ipif->ipif_next) { 14534 mutex_enter(&ill_v4->ill_lock); 14535 ipif->ipif_state_flags &= ~IPIF_CHANGING; 14536 IPIF_UNMARK_MOVING(ipif); 14537 mutex_exit(&ill_v4->ill_lock); 14538 if (ipif->ipif_was_up) { 14539 if (!(ipif->ipif_flags & IPIF_UP)) 14540 err = ipif_up(ipif, q, mp); 14541 ipif->ipif_was_up = B_FALSE; 14542 if (err != 0) { 14543 /* 14544 * Can there be any other error ? 14545 */ 14546 ASSERT(err == EINPROGRESS); 14547 return (err); 14548 } 14549 } 14550 } 14551 mutex_enter(&ill_v4->ill_lock); 14552 ill_v4->ill_state_flags &= ~ILL_CHANGING; 14553 mutex_exit(&ill_v4->ill_lock); 14554 ill_v4->ill_up_ipifs = B_FALSE; 14555 if (ill_v4->ill_move_in_progress) { 14556 ASSERT(ill_v4->ill_move_peer != NULL); 14557 ill_v4->ill_move_in_progress = B_FALSE; 14558 from_ill = ill_v4->ill_move_peer; 14559 from_ill->ill_move_in_progress = B_FALSE; 14560 from_ill->ill_move_peer = NULL; 14561 mutex_enter(&from_ill->ill_lock); 14562 from_ill->ill_state_flags &= ~ILL_CHANGING; 14563 mutex_exit(&from_ill->ill_lock); 14564 if (ill_v6 == NULL) { 14565 if (from_ill->ill_phyint->phyint_flags & 14566 PHYI_STANDBY) { 14567 phyint_inactive(from_ill->ill_phyint); 14568 } 14569 if (ill_v4->ill_phyint->phyint_flags & 14570 PHYI_STANDBY) { 14571 phyint_inactive(ill_v4->ill_phyint); 14572 } 14573 } 14574 ill_v4->ill_move_peer = NULL; 14575 } 14576 } 14577 14578 if (ill_v6 != NULL) { 14579 ill_v6->ill_up_ipifs = B_TRUE; 14580 for (ipif = ill_v6->ill_ipif; ipif != NULL; 14581 ipif = ipif->ipif_next) { 14582 mutex_enter(&ill_v6->ill_lock); 14583 ipif->ipif_state_flags &= ~IPIF_CHANGING; 14584 IPIF_UNMARK_MOVING(ipif); 14585 mutex_exit(&ill_v6->ill_lock); 14586 if (ipif->ipif_was_up) { 14587 if (!(ipif->ipif_flags & IPIF_UP)) 14588 err = ipif_up(ipif, q, mp); 14589 ipif->ipif_was_up = B_FALSE; 14590 if (err != 0) { 14591 /* 14592 * Can there be any other error ? 14593 */ 14594 ASSERT(err == EINPROGRESS); 14595 return (err); 14596 } 14597 } 14598 } 14599 mutex_enter(&ill_v6->ill_lock); 14600 ill_v6->ill_state_flags &= ~ILL_CHANGING; 14601 mutex_exit(&ill_v6->ill_lock); 14602 ill_v6->ill_up_ipifs = B_FALSE; 14603 if (ill_v6->ill_move_in_progress) { 14604 ASSERT(ill_v6->ill_move_peer != NULL); 14605 ill_v6->ill_move_in_progress = B_FALSE; 14606 from_ill = ill_v6->ill_move_peer; 14607 from_ill->ill_move_in_progress = B_FALSE; 14608 from_ill->ill_move_peer = NULL; 14609 mutex_enter(&from_ill->ill_lock); 14610 from_ill->ill_state_flags &= ~ILL_CHANGING; 14611 mutex_exit(&from_ill->ill_lock); 14612 if (from_ill->ill_phyint->phyint_flags & PHYI_STANDBY) { 14613 phyint_inactive(from_ill->ill_phyint); 14614 } 14615 if (ill_v6->ill_phyint->phyint_flags & PHYI_STANDBY) { 14616 phyint_inactive(ill_v6->ill_phyint); 14617 } 14618 ill_v6->ill_move_peer = NULL; 14619 } 14620 } 14621 return (0); 14622 } 14623 14624 /* 14625 * bring down all the approriate ipifs. 14626 */ 14627 /* ARGSUSED */ 14628 static void 14629 ill_down_ipifs(ill_t *ill, mblk_t *mp, int index, boolean_t chk_nofailover) 14630 { 14631 ipif_t *ipif; 14632 14633 ASSERT(IAM_WRITER_ILL(ill)); 14634 14635 /* 14636 * Except for ipif_state_flags the other fields of the ipif/ill that 14637 * are modified below are protected implicitly since we are a writer 14638 */ 14639 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 14640 if (chk_nofailover && (ipif->ipif_flags & IPIF_NOFAILOVER)) 14641 continue; 14642 if (index == 0 || index == ipif->ipif_orig_ifindex) { 14643 /* 14644 * We go through the ipif_down logic even if the ipif 14645 * is already down, since routes can be added based 14646 * on down ipifs. Going through ipif_down once again 14647 * will delete any IREs created based on these routes. 14648 */ 14649 if (ipif->ipif_flags & IPIF_UP) 14650 ipif->ipif_was_up = B_TRUE; 14651 /* 14652 * If called with chk_nofailover true ipif is moving. 14653 */ 14654 mutex_enter(&ill->ill_lock); 14655 if (chk_nofailover) { 14656 ipif->ipif_state_flags |= 14657 IPIF_MOVING | IPIF_CHANGING; 14658 } else { 14659 ipif->ipif_state_flags |= IPIF_CHANGING; 14660 } 14661 mutex_exit(&ill->ill_lock); 14662 /* 14663 * Need to re-create net/subnet bcast ires if 14664 * they are dependent on ipif. 14665 */ 14666 if (!ipif->ipif_isv6) 14667 ipif_check_bcast_ires(ipif); 14668 (void) ipif_logical_down(ipif, NULL, NULL); 14669 ipif_non_duplicate(ipif); 14670 ipif_down_tail(ipif); 14671 /* 14672 * We don't do ipif_multicast_down for IPv4 in 14673 * ipif_down. We need to set this so that 14674 * ipif_multicast_up will join the 14675 * ALLHOSTS_GROUP on to_ill. 14676 */ 14677 ipif->ipif_multicast_up = B_FALSE; 14678 } 14679 } 14680 } 14681 14682 #define IPSQ_INC_REF(ipsq, ipst) { \ 14683 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); \ 14684 (ipsq)->ipsq_refs++; \ 14685 } 14686 14687 #define IPSQ_DEC_REF(ipsq, ipst) { \ 14688 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); \ 14689 (ipsq)->ipsq_refs--; \ 14690 if ((ipsq)->ipsq_refs == 0) \ 14691 (ipsq)->ipsq_name[0] = '\0'; \ 14692 } 14693 14694 /* 14695 * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to 14696 * new_ipsq. 14697 */ 14698 static void 14699 ill_merge_ipsq(ipsq_t *cur_ipsq, ipsq_t *new_ipsq, ip_stack_t *ipst) 14700 { 14701 phyint_t *phyint; 14702 phyint_t *next_phyint; 14703 14704 /* 14705 * To change the ipsq of an ill, we need to hold the ill_g_lock as 14706 * writer and the ill_lock of the ill in question. Also the dest 14707 * ipsq can't vanish while we hold the ill_g_lock as writer. 14708 */ 14709 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 14710 14711 phyint = cur_ipsq->ipsq_phyint_list; 14712 cur_ipsq->ipsq_phyint_list = NULL; 14713 while (phyint != NULL) { 14714 next_phyint = phyint->phyint_ipsq_next; 14715 IPSQ_DEC_REF(cur_ipsq, ipst); 14716 phyint->phyint_ipsq_next = new_ipsq->ipsq_phyint_list; 14717 new_ipsq->ipsq_phyint_list = phyint; 14718 IPSQ_INC_REF(new_ipsq, ipst); 14719 phyint->phyint_ipsq = new_ipsq; 14720 phyint = next_phyint; 14721 } 14722 } 14723 14724 #define SPLIT_SUCCESS 0 14725 #define SPLIT_NOT_NEEDED 1 14726 #define SPLIT_FAILED 2 14727 14728 int 14729 ill_split_to_grp_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, boolean_t need_retry, 14730 ip_stack_t *ipst) 14731 { 14732 ipsq_t *newipsq = NULL; 14733 14734 /* 14735 * Assertions denote pre-requisites for changing the ipsq of 14736 * a phyint 14737 */ 14738 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 14739 /* 14740 * <ill-phyint> assocs can't change while ill_g_lock 14741 * is held as writer. See ill_phyint_reinit() 14742 */ 14743 ASSERT(phyint->phyint_illv4 == NULL || 14744 MUTEX_HELD(&phyint->phyint_illv4->ill_lock)); 14745 ASSERT(phyint->phyint_illv6 == NULL || 14746 MUTEX_HELD(&phyint->phyint_illv6->ill_lock)); 14747 14748 if ((phyint->phyint_groupname_len != 14749 (strlen(cur_ipsq->ipsq_name) + 1) || 14750 bcmp(phyint->phyint_groupname, cur_ipsq->ipsq_name, 14751 phyint->phyint_groupname_len) != 0)) { 14752 /* 14753 * Once we fail in creating a new ipsq due to memory shortage, 14754 * don't attempt to create new ipsq again, based on another 14755 * phyint, since we want all phyints belonging to an IPMP group 14756 * to be in the same ipsq even in the event of mem alloc fails. 14757 */ 14758 newipsq = ip_ipsq_lookup(phyint->phyint_groupname, !need_retry, 14759 cur_ipsq, ipst); 14760 if (newipsq == NULL) { 14761 /* Memory allocation failure */ 14762 return (SPLIT_FAILED); 14763 } else { 14764 /* ipsq_refs protected by ill_g_lock (writer) */ 14765 IPSQ_DEC_REF(cur_ipsq, ipst); 14766 phyint->phyint_ipsq = newipsq; 14767 phyint->phyint_ipsq_next = newipsq->ipsq_phyint_list; 14768 newipsq->ipsq_phyint_list = phyint; 14769 IPSQ_INC_REF(newipsq, ipst); 14770 return (SPLIT_SUCCESS); 14771 } 14772 } 14773 return (SPLIT_NOT_NEEDED); 14774 } 14775 14776 /* 14777 * The ill locks of the phyint and the ill_g_lock (writer) must be held 14778 * to do this split 14779 */ 14780 static int 14781 ill_split_to_own_ipsq(phyint_t *phyint, ipsq_t *cur_ipsq, ip_stack_t *ipst) 14782 { 14783 ipsq_t *newipsq; 14784 14785 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 14786 /* 14787 * <ill-phyint> assocs can't change while ill_g_lock 14788 * is held as writer. See ill_phyint_reinit() 14789 */ 14790 14791 ASSERT(phyint->phyint_illv4 == NULL || 14792 MUTEX_HELD(&phyint->phyint_illv4->ill_lock)); 14793 ASSERT(phyint->phyint_illv6 == NULL || 14794 MUTEX_HELD(&phyint->phyint_illv6->ill_lock)); 14795 14796 if (!ipsq_init((phyint->phyint_illv4 != NULL) ? 14797 phyint->phyint_illv4: phyint->phyint_illv6)) { 14798 /* 14799 * ipsq_init failed due to no memory 14800 * caller will use the same ipsq 14801 */ 14802 return (SPLIT_FAILED); 14803 } 14804 14805 /* ipsq_ref is protected by ill_g_lock (writer) */ 14806 IPSQ_DEC_REF(cur_ipsq, ipst); 14807 14808 /* 14809 * This is a new ipsq that is unknown to the world. 14810 * So we don't need to hold ipsq_lock, 14811 */ 14812 newipsq = phyint->phyint_ipsq; 14813 newipsq->ipsq_writer = NULL; 14814 newipsq->ipsq_reentry_cnt--; 14815 ASSERT(newipsq->ipsq_reentry_cnt == 0); 14816 #ifdef ILL_DEBUG 14817 newipsq->ipsq_depth = 0; 14818 #endif 14819 14820 return (SPLIT_SUCCESS); 14821 } 14822 14823 /* 14824 * Change the ipsq of all the ill's whose current ipsq is 'cur_ipsq' to 14825 * ipsq's representing their individual groups or themselves. Return 14826 * whether split needs to be retried again later. 14827 */ 14828 static boolean_t 14829 ill_split_ipsq(ipsq_t *cur_ipsq) 14830 { 14831 phyint_t *phyint; 14832 phyint_t *next_phyint; 14833 int error; 14834 boolean_t need_retry = B_FALSE; 14835 ip_stack_t *ipst = cur_ipsq->ipsq_ipst; 14836 14837 phyint = cur_ipsq->ipsq_phyint_list; 14838 cur_ipsq->ipsq_phyint_list = NULL; 14839 while (phyint != NULL) { 14840 next_phyint = phyint->phyint_ipsq_next; 14841 /* 14842 * 'created' will tell us whether the callee actually 14843 * created an ipsq. Lack of memory may force the callee 14844 * to return without creating an ipsq. 14845 */ 14846 if (phyint->phyint_groupname == NULL) { 14847 error = ill_split_to_own_ipsq(phyint, cur_ipsq, ipst); 14848 } else { 14849 error = ill_split_to_grp_ipsq(phyint, cur_ipsq, 14850 need_retry, ipst); 14851 } 14852 14853 switch (error) { 14854 case SPLIT_FAILED: 14855 need_retry = B_TRUE; 14856 /* FALLTHRU */ 14857 case SPLIT_NOT_NEEDED: 14858 /* 14859 * Keep it on the list. 14860 */ 14861 phyint->phyint_ipsq_next = cur_ipsq->ipsq_phyint_list; 14862 cur_ipsq->ipsq_phyint_list = phyint; 14863 break; 14864 case SPLIT_SUCCESS: 14865 break; 14866 default: 14867 ASSERT(0); 14868 } 14869 14870 phyint = next_phyint; 14871 } 14872 return (need_retry); 14873 } 14874 14875 /* 14876 * given an ipsq 'ipsq' lock all ills associated with this ipsq. 14877 * and return the ills in the list. This list will be 14878 * needed to unlock all the ills later on by the caller. 14879 * The <ill-ipsq> associations could change between the 14880 * lock and unlock. Hence the unlock can't traverse the 14881 * ipsq to get the list of ills. 14882 */ 14883 static int 14884 ill_lock_ipsq_ills(ipsq_t *ipsq, ill_t **list, int list_max) 14885 { 14886 int cnt = 0; 14887 phyint_t *phyint; 14888 ip_stack_t *ipst = ipsq->ipsq_ipst; 14889 14890 /* 14891 * The caller holds ill_g_lock to ensure that the ill memberships 14892 * of the ipsq don't change 14893 */ 14894 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock)); 14895 14896 phyint = ipsq->ipsq_phyint_list; 14897 while (phyint != NULL) { 14898 if (phyint->phyint_illv4 != NULL) { 14899 ASSERT(cnt < list_max); 14900 list[cnt++] = phyint->phyint_illv4; 14901 } 14902 if (phyint->phyint_illv6 != NULL) { 14903 ASSERT(cnt < list_max); 14904 list[cnt++] = phyint->phyint_illv6; 14905 } 14906 phyint = phyint->phyint_ipsq_next; 14907 } 14908 ill_lock_ills(list, cnt); 14909 return (cnt); 14910 } 14911 14912 void 14913 ill_lock_ills(ill_t **list, int cnt) 14914 { 14915 int i; 14916 14917 if (cnt > 1) { 14918 boolean_t try_again; 14919 do { 14920 try_again = B_FALSE; 14921 for (i = 0; i < cnt - 1; i++) { 14922 if (list[i] < list[i + 1]) { 14923 ill_t *tmp; 14924 14925 /* swap the elements */ 14926 tmp = list[i]; 14927 list[i] = list[i + 1]; 14928 list[i + 1] = tmp; 14929 try_again = B_TRUE; 14930 } 14931 } 14932 } while (try_again); 14933 } 14934 14935 for (i = 0; i < cnt; i++) { 14936 if (i == 0) { 14937 if (list[i] != NULL) 14938 mutex_enter(&list[i]->ill_lock); 14939 else 14940 return; 14941 } else if ((list[i-1] != list[i]) && (list[i] != NULL)) { 14942 mutex_enter(&list[i]->ill_lock); 14943 } 14944 } 14945 } 14946 14947 void 14948 ill_unlock_ills(ill_t **list, int cnt) 14949 { 14950 int i; 14951 14952 for (i = 0; i < cnt; i++) { 14953 if ((i == 0) && (list[i] != NULL)) { 14954 mutex_exit(&list[i]->ill_lock); 14955 } else if ((list[i-1] != list[i]) && (list[i] != NULL)) { 14956 mutex_exit(&list[i]->ill_lock); 14957 } 14958 } 14959 } 14960 14961 /* 14962 * Merge all the ills from 1 ipsq group into another ipsq group. 14963 * The source ipsq group is specified by the ipsq associated with 14964 * 'from_ill'. The destination ipsq group is specified by the ipsq 14965 * associated with 'to_ill' or 'groupname' respectively. 14966 * Note that ipsq itself does not have a reference count mechanism 14967 * and functions don't look up an ipsq and pass it around. Instead 14968 * functions pass around an ill or groupname, and the ipsq is looked 14969 * up from the ill or groupname and the required operation performed 14970 * atomically with the lookup on the ipsq. 14971 */ 14972 static int 14973 ill_merge_groups(ill_t *from_ill, ill_t *to_ill, char *groupname, mblk_t *mp, 14974 queue_t *q) 14975 { 14976 ipsq_t *old_ipsq; 14977 ipsq_t *new_ipsq; 14978 ill_t **ill_list; 14979 int cnt; 14980 size_t ill_list_size; 14981 boolean_t became_writer_on_new_sq = B_FALSE; 14982 ip_stack_t *ipst = from_ill->ill_ipst; 14983 14984 ASSERT(to_ill == NULL || ipst == to_ill->ill_ipst); 14985 /* Exactly 1 of 'to_ill' and groupname can be specified. */ 14986 ASSERT((to_ill != NULL) ^ (groupname != NULL)); 14987 14988 /* 14989 * Need to hold ill_g_lock as writer and also the ill_lock to 14990 * change the <ill-ipsq> assoc of an ill. Need to hold the 14991 * ipsq_lock to prevent new messages from landing on an ipsq. 14992 */ 14993 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 14994 14995 old_ipsq = from_ill->ill_phyint->phyint_ipsq; 14996 if (groupname != NULL) 14997 new_ipsq = ip_ipsq_lookup(groupname, B_TRUE, NULL, ipst); 14998 else { 14999 new_ipsq = to_ill->ill_phyint->phyint_ipsq; 15000 } 15001 15002 ASSERT(old_ipsq != NULL && new_ipsq != NULL); 15003 15004 /* 15005 * both groups are on the same ipsq. 15006 */ 15007 if (old_ipsq == new_ipsq) { 15008 rw_exit(&ipst->ips_ill_g_lock); 15009 return (0); 15010 } 15011 15012 cnt = old_ipsq->ipsq_refs << 1; 15013 ill_list_size = cnt * sizeof (ill_t *); 15014 ill_list = kmem_zalloc(ill_list_size, KM_NOSLEEP); 15015 if (ill_list == NULL) { 15016 rw_exit(&ipst->ips_ill_g_lock); 15017 return (ENOMEM); 15018 } 15019 cnt = ill_lock_ipsq_ills(old_ipsq, ill_list, cnt); 15020 15021 /* Need ipsq lock to enque messages on new ipsq or to become writer */ 15022 mutex_enter(&new_ipsq->ipsq_lock); 15023 if ((new_ipsq->ipsq_writer == NULL && 15024 new_ipsq->ipsq_current_ipif == NULL) || 15025 (new_ipsq->ipsq_writer == curthread)) { 15026 new_ipsq->ipsq_writer = curthread; 15027 new_ipsq->ipsq_reentry_cnt++; 15028 became_writer_on_new_sq = B_TRUE; 15029 } 15030 15031 /* 15032 * We are holding ill_g_lock as writer and all the ill locks of 15033 * the old ipsq. So the old_ipsq can't be looked up, and hence no new 15034 * message can land up on the old ipsq even though we don't hold the 15035 * ipsq_lock of the old_ipsq. Now move all messages to the newipsq. 15036 */ 15037 ill_move_to_new_ipsq(old_ipsq, new_ipsq, mp, q); 15038 15039 /* 15040 * now change the ipsq of all ills in the 'old_ipsq' to 'new_ipsq'. 15041 * 'new_ipsq' has been looked up, and it can't change its <ill-ipsq> 15042 * assocs. till we release the ill_g_lock, and hence it can't vanish. 15043 */ 15044 ill_merge_ipsq(old_ipsq, new_ipsq, ipst); 15045 15046 /* 15047 * Mark the new ipsq as needing a split since it is currently 15048 * being shared by more than 1 IPMP group. The split will 15049 * occur at the end of ipsq_exit 15050 */ 15051 new_ipsq->ipsq_split = B_TRUE; 15052 15053 /* Now release all the locks */ 15054 mutex_exit(&new_ipsq->ipsq_lock); 15055 ill_unlock_ills(ill_list, cnt); 15056 rw_exit(&ipst->ips_ill_g_lock); 15057 15058 kmem_free(ill_list, ill_list_size); 15059 15060 /* 15061 * If we succeeded in becoming writer on the new ipsq, then 15062 * drain the new ipsq and start processing all enqueued messages 15063 * including the current ioctl we are processing which is either 15064 * a set groupname or failover/failback. 15065 */ 15066 if (became_writer_on_new_sq) 15067 ipsq_exit(new_ipsq, B_TRUE, B_TRUE); 15068 15069 /* 15070 * syncq has been changed and all the messages have been moved. 15071 */ 15072 mutex_enter(&old_ipsq->ipsq_lock); 15073 old_ipsq->ipsq_current_ipif = NULL; 15074 old_ipsq->ipsq_current_ioctl = 0; 15075 mutex_exit(&old_ipsq->ipsq_lock); 15076 return (EINPROGRESS); 15077 } 15078 15079 /* 15080 * Delete and add the loopback copy and non-loopback copy of 15081 * the BROADCAST ire corresponding to ill and addr. Used to 15082 * group broadcast ires together when ill becomes part of 15083 * a group. 15084 * 15085 * This function is also called when ill is leaving the group 15086 * so that the ires belonging to the group gets re-grouped. 15087 */ 15088 static void 15089 ill_bcast_delete_and_add(ill_t *ill, ipaddr_t addr) 15090 { 15091 ire_t *ire, *nire, *nire_next, *ire_head = NULL; 15092 ire_t **ire_ptpn = &ire_head; 15093 ip_stack_t *ipst = ill->ill_ipst; 15094 15095 /* 15096 * The loopback and non-loopback IREs are inserted in the order in which 15097 * they're found, on the basis that they are correctly ordered (loopback 15098 * first). 15099 */ 15100 for (;;) { 15101 ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif, 15102 ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst); 15103 if (ire == NULL) 15104 break; 15105 15106 /* 15107 * we are passing in KM_SLEEP because it is not easy to 15108 * go back to a sane state in case of memory failure. 15109 */ 15110 nire = kmem_cache_alloc(ire_cache, KM_SLEEP); 15111 ASSERT(nire != NULL); 15112 bzero(nire, sizeof (ire_t)); 15113 /* 15114 * Don't use ire_max_frag directly since we don't 15115 * hold on to 'ire' until we add the new ire 'nire' and 15116 * we don't want the new ire to have a dangling reference 15117 * to 'ire'. The ire_max_frag of a broadcast ire must 15118 * be in sync with the ipif_mtu of the associate ipif. 15119 * For eg. this happens as a result of SIOCSLIFNAME, 15120 * SIOCSLIFLNKINFO or a DL_NOTE_SDU_SIZE inititated by 15121 * the driver. A change in ire_max_frag triggered as 15122 * as a result of path mtu discovery, or due to an 15123 * IP_IOC_IRE_ADVISE_NOREPLY from the transport or due a 15124 * route change -mtu command does not apply to broadcast ires. 15125 * 15126 * XXX We need a recovery strategy here if ire_init fails 15127 */ 15128 if (ire_init(nire, 15129 (uchar_t *)&ire->ire_addr, 15130 (uchar_t *)&ire->ire_mask, 15131 (uchar_t *)&ire->ire_src_addr, 15132 (uchar_t *)&ire->ire_gateway_addr, 15133 (uchar_t *)&ire->ire_in_src_addr, 15134 ire->ire_stq == NULL ? &ip_loopback_mtu : 15135 &ire->ire_ipif->ipif_mtu, 15136 (ire->ire_nce != NULL ? ire->ire_nce->nce_fp_mp : NULL), 15137 ire->ire_rfq, 15138 ire->ire_stq, 15139 ire->ire_type, 15140 (ire->ire_nce != NULL? ire->ire_nce->nce_res_mp : NULL), 15141 ire->ire_ipif, 15142 ire->ire_in_ill, 15143 ire->ire_cmask, 15144 ire->ire_phandle, 15145 ire->ire_ihandle, 15146 ire->ire_flags, 15147 &ire->ire_uinfo, 15148 NULL, 15149 NULL, 15150 ipst) == NULL) { 15151 cmn_err(CE_PANIC, "ire_init() failed"); 15152 } 15153 ire_delete(ire); 15154 ire_refrele(ire); 15155 15156 /* 15157 * The newly created IREs are inserted at the tail of the list 15158 * starting with ire_head. As we've just allocated them no one 15159 * knows about them so it's safe. 15160 */ 15161 *ire_ptpn = nire; 15162 ire_ptpn = &nire->ire_next; 15163 } 15164 15165 for (nire = ire_head; nire != NULL; nire = nire_next) { 15166 int error; 15167 ire_t *oire; 15168 /* unlink the IRE from our list before calling ire_add() */ 15169 nire_next = nire->ire_next; 15170 nire->ire_next = NULL; 15171 15172 /* ire_add adds the ire at the right place in the list */ 15173 oire = nire; 15174 error = ire_add(&nire, NULL, NULL, NULL, B_FALSE); 15175 ASSERT(error == 0); 15176 ASSERT(oire == nire); 15177 ire_refrele(nire); /* Held in ire_add */ 15178 } 15179 } 15180 15181 /* 15182 * This function is usually called when an ill is inserted in 15183 * a group and all the ipifs are already UP. As all the ipifs 15184 * are already UP, the broadcast ires have already been created 15185 * and been inserted. But, ire_add_v4 would not have grouped properly. 15186 * We need to re-group for the benefit of ip_wput_ire which 15187 * expects BROADCAST ires to be grouped properly to avoid sending 15188 * more than one copy of the broadcast packet per group. 15189 * 15190 * NOTE : We don't check for ill_ipif_up_count to be non-zero here 15191 * because when ipif_up_done ends up calling this, ires have 15192 * already been added before illgrp_insert i.e before ill_group 15193 * has been initialized. 15194 */ 15195 static void 15196 ill_group_bcast_for_xmit(ill_t *ill) 15197 { 15198 ill_group_t *illgrp; 15199 ipif_t *ipif; 15200 ipaddr_t addr; 15201 ipaddr_t net_mask; 15202 ipaddr_t subnet_netmask; 15203 15204 illgrp = ill->ill_group; 15205 15206 /* 15207 * This function is called even when an ill is deleted from 15208 * the group. Hence, illgrp could be null. 15209 */ 15210 if (illgrp != NULL && illgrp->illgrp_ill_count == 1) 15211 return; 15212 15213 /* 15214 * Delete all the BROADCAST ires matching this ill and add 15215 * them back. This time, ire_add_v4 should take care of 15216 * grouping them with others because ill is part of the 15217 * group. 15218 */ 15219 ill_bcast_delete_and_add(ill, 0); 15220 ill_bcast_delete_and_add(ill, INADDR_BROADCAST); 15221 15222 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 15223 15224 if ((ipif->ipif_lcl_addr != INADDR_ANY) && 15225 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 15226 net_mask = ip_net_mask(ipif->ipif_lcl_addr); 15227 } else { 15228 net_mask = htonl(IN_CLASSA_NET); 15229 } 15230 addr = net_mask & ipif->ipif_subnet; 15231 ill_bcast_delete_and_add(ill, addr); 15232 ill_bcast_delete_and_add(ill, ~net_mask | addr); 15233 15234 subnet_netmask = ipif->ipif_net_mask; 15235 addr = ipif->ipif_subnet; 15236 ill_bcast_delete_and_add(ill, addr); 15237 ill_bcast_delete_and_add(ill, ~subnet_netmask | addr); 15238 } 15239 } 15240 15241 /* 15242 * This function is called from illgrp_delete when ill is being deleted 15243 * from the group. 15244 * 15245 * As ill is not there in the group anymore, any address belonging 15246 * to this ill should be cleared of IRE_MARK_NORECV. 15247 */ 15248 static void 15249 ill_clear_bcast_mark(ill_t *ill, ipaddr_t addr) 15250 { 15251 ire_t *ire; 15252 irb_t *irb; 15253 ip_stack_t *ipst = ill->ill_ipst; 15254 15255 ASSERT(ill->ill_group == NULL); 15256 15257 ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, ill->ill_ipif, 15258 ALL_ZONES, NULL, MATCH_IRE_TYPE | MATCH_IRE_ILL, ipst); 15259 15260 if (ire != NULL) { 15261 /* 15262 * IPMP and plumbing operations are serialized on the ipsq, so 15263 * no one will insert or delete a broadcast ire under our feet. 15264 */ 15265 irb = ire->ire_bucket; 15266 rw_enter(&irb->irb_lock, RW_READER); 15267 ire_refrele(ire); 15268 15269 for (; ire != NULL; ire = ire->ire_next) { 15270 if (ire->ire_addr != addr) 15271 break; 15272 if (ire_to_ill(ire) != ill) 15273 continue; 15274 15275 ASSERT(!(ire->ire_marks & IRE_MARK_CONDEMNED)); 15276 ire->ire_marks &= ~IRE_MARK_NORECV; 15277 } 15278 rw_exit(&irb->irb_lock); 15279 } 15280 } 15281 15282 /* 15283 * This function must be called only after the broadcast ires 15284 * have been grouped together. For a given address addr, nominate 15285 * only one of the ires whose interface is not FAILED or OFFLINE. 15286 * 15287 * This is also called when an ipif goes down, so that we can nominate 15288 * a different ire with the same address for receiving. 15289 */ 15290 static void 15291 ill_mark_bcast(ill_group_t *illgrp, ipaddr_t addr, ip_stack_t *ipst) 15292 { 15293 irb_t *irb; 15294 ire_t *ire; 15295 ire_t *ire1; 15296 ire_t *save_ire; 15297 ire_t **irep = NULL; 15298 boolean_t first = B_TRUE; 15299 ire_t *clear_ire = NULL; 15300 ire_t *start_ire = NULL; 15301 ire_t *new_lb_ire; 15302 ire_t *new_nlb_ire; 15303 boolean_t new_lb_ire_used = B_FALSE; 15304 boolean_t new_nlb_ire_used = B_FALSE; 15305 uint64_t match_flags; 15306 uint64_t phyi_flags; 15307 boolean_t fallback = B_FALSE; 15308 uint_t max_frag; 15309 15310 ire = ire_ctable_lookup(addr, 0, IRE_BROADCAST, NULL, ALL_ZONES, 15311 NULL, MATCH_IRE_TYPE, ipst); 15312 /* 15313 * We may not be able to find some ires if a previous 15314 * ire_create failed. This happens when an ipif goes 15315 * down and we are unable to create BROADCAST ires due 15316 * to memory failure. Thus, we have to check for NULL 15317 * below. This should handle the case for LOOPBACK, 15318 * POINTOPOINT and interfaces with some POINTOPOINT 15319 * logicals for which there are no BROADCAST ires. 15320 */ 15321 if (ire == NULL) 15322 return; 15323 /* 15324 * Currently IRE_BROADCASTS are deleted when an ipif 15325 * goes down which runs exclusively. Thus, setting 15326 * IRE_MARK_RCVD should not race with ire_delete marking 15327 * IRE_MARK_CONDEMNED. We grab the lock below just to 15328 * be consistent with other parts of the code that walks 15329 * a given bucket. 15330 */ 15331 save_ire = ire; 15332 irb = ire->ire_bucket; 15333 new_lb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP); 15334 if (new_lb_ire == NULL) { 15335 ire_refrele(ire); 15336 return; 15337 } 15338 new_nlb_ire = kmem_cache_alloc(ire_cache, KM_NOSLEEP); 15339 if (new_nlb_ire == NULL) { 15340 ire_refrele(ire); 15341 kmem_cache_free(ire_cache, new_lb_ire); 15342 return; 15343 } 15344 IRB_REFHOLD(irb); 15345 rw_enter(&irb->irb_lock, RW_WRITER); 15346 /* 15347 * Get to the first ire matching the address and the 15348 * group. If the address does not match we are done 15349 * as we could not find the IRE. If the address matches 15350 * we should get to the first one matching the group. 15351 */ 15352 while (ire != NULL) { 15353 if (ire->ire_addr != addr || 15354 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 15355 break; 15356 } 15357 ire = ire->ire_next; 15358 } 15359 match_flags = PHYI_FAILED | PHYI_INACTIVE; 15360 start_ire = ire; 15361 redo: 15362 while (ire != NULL && ire->ire_addr == addr && 15363 ire->ire_ipif->ipif_ill->ill_group == illgrp) { 15364 /* 15365 * The first ire for any address within a group 15366 * should always be the one with IRE_MARK_NORECV cleared 15367 * so that ip_wput_ire can avoid searching for one. 15368 * Note down the insertion point which will be used 15369 * later. 15370 */ 15371 if (first && (irep == NULL)) 15372 irep = ire->ire_ptpn; 15373 /* 15374 * PHYI_FAILED is set when the interface fails. 15375 * This interface might have become good, but the 15376 * daemon has not yet detected. We should still 15377 * not receive on this. PHYI_OFFLINE should never 15378 * be picked as this has been offlined and soon 15379 * be removed. 15380 */ 15381 phyi_flags = ire->ire_ipif->ipif_ill->ill_phyint->phyint_flags; 15382 if (phyi_flags & PHYI_OFFLINE) { 15383 ire->ire_marks |= IRE_MARK_NORECV; 15384 ire = ire->ire_next; 15385 continue; 15386 } 15387 if (phyi_flags & match_flags) { 15388 ire->ire_marks |= IRE_MARK_NORECV; 15389 ire = ire->ire_next; 15390 if ((phyi_flags & (PHYI_FAILED | PHYI_INACTIVE)) == 15391 PHYI_INACTIVE) { 15392 fallback = B_TRUE; 15393 } 15394 continue; 15395 } 15396 if (first) { 15397 /* 15398 * We will move this to the front of the list later 15399 * on. 15400 */ 15401 clear_ire = ire; 15402 ire->ire_marks &= ~IRE_MARK_NORECV; 15403 } else { 15404 ire->ire_marks |= IRE_MARK_NORECV; 15405 } 15406 first = B_FALSE; 15407 ire = ire->ire_next; 15408 } 15409 /* 15410 * If we never nominated anybody, try nominating at least 15411 * an INACTIVE, if we found one. Do it only once though. 15412 */ 15413 if (first && (match_flags == (PHYI_FAILED | PHYI_INACTIVE)) && 15414 fallback) { 15415 match_flags = PHYI_FAILED; 15416 ire = start_ire; 15417 irep = NULL; 15418 goto redo; 15419 } 15420 ire_refrele(save_ire); 15421 15422 /* 15423 * irep non-NULL indicates that we entered the while loop 15424 * above. If clear_ire is at the insertion point, we don't 15425 * have to do anything. clear_ire will be NULL if all the 15426 * interfaces are failed. 15427 * 15428 * We cannot unlink and reinsert the ire at the right place 15429 * in the list since there can be other walkers of this bucket. 15430 * Instead we delete and recreate the ire 15431 */ 15432 if (clear_ire != NULL && irep != NULL && *irep != clear_ire) { 15433 ire_t *clear_ire_stq = NULL; 15434 mblk_t *fp_mp = NULL, *res_mp = NULL; 15435 15436 bzero(new_lb_ire, sizeof (ire_t)); 15437 if (clear_ire->ire_nce != NULL) { 15438 fp_mp = clear_ire->ire_nce->nce_fp_mp; 15439 res_mp = clear_ire->ire_nce->nce_res_mp; 15440 } 15441 /* XXX We need a recovery strategy here. */ 15442 if (ire_init(new_lb_ire, 15443 (uchar_t *)&clear_ire->ire_addr, 15444 (uchar_t *)&clear_ire->ire_mask, 15445 (uchar_t *)&clear_ire->ire_src_addr, 15446 (uchar_t *)&clear_ire->ire_gateway_addr, 15447 (uchar_t *)&clear_ire->ire_in_src_addr, 15448 &clear_ire->ire_max_frag, 15449 fp_mp, 15450 clear_ire->ire_rfq, 15451 clear_ire->ire_stq, 15452 clear_ire->ire_type, 15453 res_mp, 15454 clear_ire->ire_ipif, 15455 clear_ire->ire_in_ill, 15456 clear_ire->ire_cmask, 15457 clear_ire->ire_phandle, 15458 clear_ire->ire_ihandle, 15459 clear_ire->ire_flags, 15460 &clear_ire->ire_uinfo, 15461 NULL, 15462 NULL, 15463 ipst) == NULL) 15464 cmn_err(CE_PANIC, "ire_init() failed"); 15465 if (clear_ire->ire_stq == NULL) { 15466 ire_t *ire_next = clear_ire->ire_next; 15467 if (ire_next != NULL && 15468 ire_next->ire_stq != NULL && 15469 ire_next->ire_addr == clear_ire->ire_addr && 15470 ire_next->ire_ipif->ipif_ill == 15471 clear_ire->ire_ipif->ipif_ill) { 15472 clear_ire_stq = ire_next; 15473 15474 bzero(new_nlb_ire, sizeof (ire_t)); 15475 if (clear_ire_stq->ire_nce != NULL) { 15476 fp_mp = 15477 clear_ire_stq->ire_nce->nce_fp_mp; 15478 res_mp = 15479 clear_ire_stq->ire_nce->nce_res_mp; 15480 } else { 15481 fp_mp = res_mp = NULL; 15482 } 15483 /* XXX We need a recovery strategy here. */ 15484 if (ire_init(new_nlb_ire, 15485 (uchar_t *)&clear_ire_stq->ire_addr, 15486 (uchar_t *)&clear_ire_stq->ire_mask, 15487 (uchar_t *)&clear_ire_stq->ire_src_addr, 15488 (uchar_t *)&clear_ire_stq->ire_gateway_addr, 15489 (uchar_t *)&clear_ire_stq->ire_in_src_addr, 15490 &clear_ire_stq->ire_max_frag, 15491 fp_mp, 15492 clear_ire_stq->ire_rfq, 15493 clear_ire_stq->ire_stq, 15494 clear_ire_stq->ire_type, 15495 res_mp, 15496 clear_ire_stq->ire_ipif, 15497 clear_ire_stq->ire_in_ill, 15498 clear_ire_stq->ire_cmask, 15499 clear_ire_stq->ire_phandle, 15500 clear_ire_stq->ire_ihandle, 15501 clear_ire_stq->ire_flags, 15502 &clear_ire_stq->ire_uinfo, 15503 NULL, 15504 NULL, 15505 ipst) == NULL) 15506 cmn_err(CE_PANIC, "ire_init() failed"); 15507 } 15508 } 15509 15510 /* 15511 * Delete the ire. We can't call ire_delete() since 15512 * we are holding the bucket lock. We can't release the 15513 * bucket lock since we can't allow irep to change. So just 15514 * mark it CONDEMNED. The IRB_REFRELE will delete the 15515 * ire from the list and do the refrele. 15516 */ 15517 clear_ire->ire_marks |= IRE_MARK_CONDEMNED; 15518 irb->irb_marks |= IRB_MARK_CONDEMNED; 15519 15520 if (clear_ire_stq != NULL && clear_ire_stq->ire_nce != NULL) { 15521 nce_fastpath_list_delete(clear_ire_stq->ire_nce); 15522 clear_ire_stq->ire_marks |= IRE_MARK_CONDEMNED; 15523 } 15524 15525 /* 15526 * Also take care of otherfields like ib/ob pkt count 15527 * etc. Need to dup them. ditto in ill_bcast_delete_and_add 15528 */ 15529 15530 /* Set the max_frag before adding the ire */ 15531 max_frag = *new_lb_ire->ire_max_fragp; 15532 new_lb_ire->ire_max_fragp = NULL; 15533 new_lb_ire->ire_max_frag = max_frag; 15534 15535 /* Add the new ire's. Insert at *irep */ 15536 new_lb_ire->ire_bucket = clear_ire->ire_bucket; 15537 ire1 = *irep; 15538 if (ire1 != NULL) 15539 ire1->ire_ptpn = &new_lb_ire->ire_next; 15540 new_lb_ire->ire_next = ire1; 15541 /* Link the new one in. */ 15542 new_lb_ire->ire_ptpn = irep; 15543 membar_producer(); 15544 *irep = new_lb_ire; 15545 new_lb_ire_used = B_TRUE; 15546 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, ire_stats_inserted); 15547 new_lb_ire->ire_bucket->irb_ire_cnt++; 15548 new_lb_ire->ire_ipif->ipif_ire_cnt++; 15549 15550 if (clear_ire_stq != NULL) { 15551 /* Set the max_frag before adding the ire */ 15552 max_frag = *new_nlb_ire->ire_max_fragp; 15553 new_nlb_ire->ire_max_fragp = NULL; 15554 new_nlb_ire->ire_max_frag = max_frag; 15555 15556 new_nlb_ire->ire_bucket = clear_ire->ire_bucket; 15557 irep = &new_lb_ire->ire_next; 15558 /* Add the new ire. Insert at *irep */ 15559 ire1 = *irep; 15560 if (ire1 != NULL) 15561 ire1->ire_ptpn = &new_nlb_ire->ire_next; 15562 new_nlb_ire->ire_next = ire1; 15563 /* Link the new one in. */ 15564 new_nlb_ire->ire_ptpn = irep; 15565 membar_producer(); 15566 *irep = new_nlb_ire; 15567 new_nlb_ire_used = B_TRUE; 15568 BUMP_IRE_STATS(ipst->ips_ire_stats_v4, 15569 ire_stats_inserted); 15570 new_nlb_ire->ire_bucket->irb_ire_cnt++; 15571 new_nlb_ire->ire_ipif->ipif_ire_cnt++; 15572 ((ill_t *)new_nlb_ire->ire_stq->q_ptr)->ill_ire_cnt++; 15573 } 15574 } 15575 rw_exit(&irb->irb_lock); 15576 if (!new_lb_ire_used) 15577 kmem_cache_free(ire_cache, new_lb_ire); 15578 if (!new_nlb_ire_used) 15579 kmem_cache_free(ire_cache, new_nlb_ire); 15580 IRB_REFRELE(irb); 15581 } 15582 15583 /* 15584 * Whenever an ipif goes down we have to renominate a different 15585 * broadcast ire to receive. Whenever an ipif comes up, we need 15586 * to make sure that we have only one nominated to receive. 15587 */ 15588 static void 15589 ipif_renominate_bcast(ipif_t *ipif) 15590 { 15591 ill_t *ill = ipif->ipif_ill; 15592 ipaddr_t subnet_addr; 15593 ipaddr_t net_addr; 15594 ipaddr_t net_mask = 0; 15595 ipaddr_t subnet_netmask; 15596 ipaddr_t addr; 15597 ill_group_t *illgrp; 15598 ip_stack_t *ipst = ill->ill_ipst; 15599 15600 illgrp = ill->ill_group; 15601 /* 15602 * If this is the last ipif going down, it might take 15603 * the ill out of the group. In that case ipif_down -> 15604 * illgrp_delete takes care of doing the nomination. 15605 * ipif_down does not call for this case. 15606 */ 15607 ASSERT(illgrp != NULL); 15608 15609 /* There could not have been any ires associated with this */ 15610 if (ipif->ipif_subnet == 0) 15611 return; 15612 15613 ill_mark_bcast(illgrp, 0, ipst); 15614 ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst); 15615 15616 if ((ipif->ipif_lcl_addr != INADDR_ANY) && 15617 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 15618 net_mask = ip_net_mask(ipif->ipif_lcl_addr); 15619 } else { 15620 net_mask = htonl(IN_CLASSA_NET); 15621 } 15622 addr = net_mask & ipif->ipif_subnet; 15623 ill_mark_bcast(illgrp, addr, ipst); 15624 15625 net_addr = ~net_mask | addr; 15626 ill_mark_bcast(illgrp, net_addr, ipst); 15627 15628 subnet_netmask = ipif->ipif_net_mask; 15629 addr = ipif->ipif_subnet; 15630 ill_mark_bcast(illgrp, addr, ipst); 15631 15632 subnet_addr = ~subnet_netmask | addr; 15633 ill_mark_bcast(illgrp, subnet_addr, ipst); 15634 } 15635 15636 /* 15637 * Whenever we form or delete ill groups, we need to nominate one set of 15638 * BROADCAST ires for receiving in the group. 15639 * 15640 * 1) When ipif_up_done -> ilgrp_insert calls this function, BROADCAST ires 15641 * have been added, but ill_ipif_up_count is 0. Thus, we don't assert 15642 * for ill_ipif_up_count to be non-zero. This is the only case where 15643 * ill_ipif_up_count is zero and we would still find the ires. 15644 * 15645 * 2) ip_sioctl_group_name/ifgrp_insert calls this function, at least one 15646 * ipif is UP and we just have to do the nomination. 15647 * 15648 * 3) When ill_handoff_responsibility calls us, some ill has been removed 15649 * from the group. So, we have to do the nomination. 15650 * 15651 * Because of (3), there could be just one ill in the group. But we have 15652 * to nominate still as IRE_MARK_NORCV may have been marked on this. 15653 * Thus, this function does not optimize when there is only one ill as 15654 * it is not correct for (3). 15655 */ 15656 static void 15657 ill_nominate_bcast_rcv(ill_group_t *illgrp) 15658 { 15659 ill_t *ill; 15660 ipif_t *ipif; 15661 ipaddr_t subnet_addr; 15662 ipaddr_t prev_subnet_addr = 0; 15663 ipaddr_t net_addr; 15664 ipaddr_t prev_net_addr = 0; 15665 ipaddr_t net_mask = 0; 15666 ipaddr_t subnet_netmask; 15667 ipaddr_t addr; 15668 ip_stack_t *ipst; 15669 15670 /* 15671 * When the last memeber is leaving, there is nothing to 15672 * nominate. 15673 */ 15674 if (illgrp->illgrp_ill_count == 0) { 15675 ASSERT(illgrp->illgrp_ill == NULL); 15676 return; 15677 } 15678 15679 ill = illgrp->illgrp_ill; 15680 ASSERT(!ill->ill_isv6); 15681 ipst = ill->ill_ipst; 15682 /* 15683 * We assume that ires with same address and belonging to the 15684 * same group, has been grouped together. Nominating a *single* 15685 * ill in the group for sending and receiving broadcast is done 15686 * by making sure that the first BROADCAST ire (which will be 15687 * the one returned by ire_ctable_lookup for ip_rput and the 15688 * one that will be used in ip_wput_ire) will be the one that 15689 * will not have IRE_MARK_NORECV set. 15690 * 15691 * 1) ip_rput checks and discards packets received on ires marked 15692 * with IRE_MARK_NORECV. Thus, we don't send up duplicate 15693 * broadcast packets. We need to clear IRE_MARK_NORECV on the 15694 * first ire in the group for every broadcast address in the group. 15695 * ip_rput will accept packets only on the first ire i.e only 15696 * one copy of the ill. 15697 * 15698 * 2) ip_wput_ire needs to send out just one copy of the broadcast 15699 * packet for the whole group. It needs to send out on the ill 15700 * whose ire has not been marked with IRE_MARK_NORECV. If it sends 15701 * on the one marked with IRE_MARK_NORECV, ip_rput will accept 15702 * the copy echoed back on other port where the ire is not marked 15703 * with IRE_MARK_NORECV. 15704 * 15705 * Note that we just need to have the first IRE either loopback or 15706 * non-loopback (either of them may not exist if ire_create failed 15707 * during ipif_down) with IRE_MARK_NORECV not set. ip_rput will 15708 * always hit the first one and hence will always accept one copy. 15709 * 15710 * We have a broadcast ire per ill for all the unique prefixes 15711 * hosted on that ill. As we don't have a way of knowing the 15712 * unique prefixes on a given ill and hence in the whole group, 15713 * we just call ill_mark_bcast on all the prefixes that exist 15714 * in the group. For the common case of one prefix, the code 15715 * below optimizes by remebering the last address used for 15716 * markng. In the case of multiple prefixes, this will still 15717 * optimize depending the order of prefixes. 15718 * 15719 * The only unique address across the whole group is 0.0.0.0 and 15720 * 255.255.255.255 and thus we call only once. ill_mark_bcast enables 15721 * the first ire in the bucket for receiving and disables the 15722 * others. 15723 */ 15724 ill_mark_bcast(illgrp, 0, ipst); 15725 ill_mark_bcast(illgrp, INADDR_BROADCAST, ipst); 15726 for (; ill != NULL; ill = ill->ill_group_next) { 15727 15728 for (ipif = ill->ill_ipif; ipif != NULL; 15729 ipif = ipif->ipif_next) { 15730 15731 if (!(ipif->ipif_flags & IPIF_UP) || 15732 ipif->ipif_subnet == 0) { 15733 continue; 15734 } 15735 if ((ipif->ipif_lcl_addr != INADDR_ANY) && 15736 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 15737 net_mask = ip_net_mask(ipif->ipif_lcl_addr); 15738 } else { 15739 net_mask = htonl(IN_CLASSA_NET); 15740 } 15741 addr = net_mask & ipif->ipif_subnet; 15742 if (prev_net_addr == 0 || prev_net_addr != addr) { 15743 ill_mark_bcast(illgrp, addr, ipst); 15744 net_addr = ~net_mask | addr; 15745 ill_mark_bcast(illgrp, net_addr, ipst); 15746 } 15747 prev_net_addr = addr; 15748 15749 subnet_netmask = ipif->ipif_net_mask; 15750 addr = ipif->ipif_subnet; 15751 if (prev_subnet_addr == 0 || 15752 prev_subnet_addr != addr) { 15753 ill_mark_bcast(illgrp, addr, ipst); 15754 subnet_addr = ~subnet_netmask | addr; 15755 ill_mark_bcast(illgrp, subnet_addr, ipst); 15756 } 15757 prev_subnet_addr = addr; 15758 } 15759 } 15760 } 15761 15762 /* 15763 * This function is called while forming ill groups. 15764 * 15765 * Currently, we handle only allmulti groups. We want to join 15766 * allmulti on only one of the ills in the groups. In future, 15767 * when we have link aggregation, we may have to join normal 15768 * multicast groups on multiple ills as switch does inbound load 15769 * balancing. Following are the functions that calls this 15770 * function : 15771 * 15772 * 1) ill_recover_multicast : Interface is coming back UP. 15773 * When the first ipif comes back UP, ipif_up_done/ipif_up_done_v6 15774 * will call ill_recover_multicast to recover all the multicast 15775 * groups. We need to make sure that only one member is joined 15776 * in the ill group. 15777 * 15778 * 2) ip_addmulti/ip_addmulti_v6 : ill groups has already been formed. 15779 * Somebody is joining allmulti. We need to make sure that only one 15780 * member is joined in the group. 15781 * 15782 * 3) illgrp_insert : If allmulti has already joined, we need to make 15783 * sure that only one member is joined in the group. 15784 * 15785 * 4) ip_delmulti/ip_delmulti_v6 : Somebody in the group is leaving 15786 * allmulti who we have nominated. We need to pick someother ill. 15787 * 15788 * 5) illgrp_delete : The ill we nominated is leaving the group, 15789 * we need to pick a new ill to join the group. 15790 * 15791 * For (1), (2), (5) - we just have to check whether there is 15792 * a good ill joined in the group. If we could not find any ills 15793 * joined the group, we should join. 15794 * 15795 * For (4), the one that was nominated to receive, left the group. 15796 * There could be nobody joined in the group when this function is 15797 * called. 15798 * 15799 * For (3) - we need to explicitly check whether there are multiple 15800 * ills joined in the group. 15801 * 15802 * For simplicity, we don't differentiate any of the above cases. We 15803 * just leave the group if it is joined on any of them and join on 15804 * the first good ill. 15805 */ 15806 int 15807 ill_nominate_mcast_rcv(ill_group_t *illgrp) 15808 { 15809 ilm_t *ilm; 15810 ill_t *ill; 15811 ill_t *fallback_inactive_ill = NULL; 15812 ill_t *fallback_failed_ill = NULL; 15813 int ret = 0; 15814 15815 /* 15816 * Leave the allmulti on all the ills and start fresh. 15817 */ 15818 for (ill = illgrp->illgrp_ill; ill != NULL; 15819 ill = ill->ill_group_next) { 15820 if (ill->ill_join_allmulti) 15821 (void) ip_leave_allmulti(ill->ill_ipif); 15822 } 15823 15824 /* 15825 * Choose a good ill. Fallback to inactive or failed if 15826 * none available. We need to fallback to FAILED in the 15827 * case where we have 2 interfaces in a group - where 15828 * one of them is failed and another is a good one and 15829 * the good one (not marked inactive) is leaving the group. 15830 */ 15831 ret = 0; 15832 for (ill = illgrp->illgrp_ill; ill != NULL; 15833 ill = ill->ill_group_next) { 15834 /* Never pick an offline interface */ 15835 if (ill->ill_phyint->phyint_flags & PHYI_OFFLINE) 15836 continue; 15837 15838 if (ill->ill_phyint->phyint_flags & PHYI_FAILED) { 15839 fallback_failed_ill = ill; 15840 continue; 15841 } 15842 if (ill->ill_phyint->phyint_flags & PHYI_INACTIVE) { 15843 fallback_inactive_ill = ill; 15844 continue; 15845 } 15846 for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { 15847 if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) { 15848 ret = ip_join_allmulti(ill->ill_ipif); 15849 /* 15850 * ip_join_allmulti can fail because of memory 15851 * failures. So, make sure we join at least 15852 * on one ill. 15853 */ 15854 if (ill->ill_join_allmulti) 15855 return (0); 15856 } 15857 } 15858 } 15859 if (ret != 0) { 15860 /* 15861 * If we tried nominating above and failed to do so, 15862 * return error. We might have tried multiple times. 15863 * But, return the latest error. 15864 */ 15865 return (ret); 15866 } 15867 if ((ill = fallback_inactive_ill) != NULL) { 15868 for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { 15869 if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) { 15870 ret = ip_join_allmulti(ill->ill_ipif); 15871 return (ret); 15872 } 15873 } 15874 } else if ((ill = fallback_failed_ill) != NULL) { 15875 for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { 15876 if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) { 15877 ret = ip_join_allmulti(ill->ill_ipif); 15878 return (ret); 15879 } 15880 } 15881 } 15882 return (0); 15883 } 15884 15885 /* 15886 * This function is called from illgrp_delete after it is 15887 * deleted from the group to reschedule responsibilities 15888 * to a different ill. 15889 */ 15890 static void 15891 ill_handoff_responsibility(ill_t *ill, ill_group_t *illgrp) 15892 { 15893 ilm_t *ilm; 15894 ipif_t *ipif; 15895 ipaddr_t subnet_addr; 15896 ipaddr_t net_addr; 15897 ipaddr_t net_mask = 0; 15898 ipaddr_t subnet_netmask; 15899 ipaddr_t addr; 15900 ip_stack_t *ipst = ill->ill_ipst; 15901 15902 ASSERT(ill->ill_group == NULL); 15903 /* 15904 * Broadcast Responsibility: 15905 * 15906 * 1. If this ill has been nominated for receiving broadcast 15907 * packets, we need to find a new one. Before we find a new 15908 * one, we need to re-group the ires that are part of this new 15909 * group (assumed by ill_nominate_bcast_rcv). We do this by 15910 * calling ill_group_bcast_for_xmit(ill) which will do the right 15911 * thing for us. 15912 * 15913 * 2. If this ill was not nominated for receiving broadcast 15914 * packets, we need to clear the IRE_MARK_NORECV flag 15915 * so that we continue to send up broadcast packets. 15916 */ 15917 if (!ill->ill_isv6) { 15918 /* 15919 * Case 1 above : No optimization here. Just redo the 15920 * nomination. 15921 */ 15922 ill_group_bcast_for_xmit(ill); 15923 ill_nominate_bcast_rcv(illgrp); 15924 15925 /* 15926 * Case 2 above : Lookup and clear IRE_MARK_NORECV. 15927 */ 15928 ill_clear_bcast_mark(ill, 0); 15929 ill_clear_bcast_mark(ill, INADDR_BROADCAST); 15930 15931 for (ipif = ill->ill_ipif; ipif != NULL; 15932 ipif = ipif->ipif_next) { 15933 15934 if (!(ipif->ipif_flags & IPIF_UP) || 15935 ipif->ipif_subnet == 0) { 15936 continue; 15937 } 15938 if ((ipif->ipif_lcl_addr != INADDR_ANY) && 15939 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 15940 net_mask = ip_net_mask(ipif->ipif_lcl_addr); 15941 } else { 15942 net_mask = htonl(IN_CLASSA_NET); 15943 } 15944 addr = net_mask & ipif->ipif_subnet; 15945 ill_clear_bcast_mark(ill, addr); 15946 15947 net_addr = ~net_mask | addr; 15948 ill_clear_bcast_mark(ill, net_addr); 15949 15950 subnet_netmask = ipif->ipif_net_mask; 15951 addr = ipif->ipif_subnet; 15952 ill_clear_bcast_mark(ill, addr); 15953 15954 subnet_addr = ~subnet_netmask | addr; 15955 ill_clear_bcast_mark(ill, subnet_addr); 15956 } 15957 } 15958 15959 /* 15960 * Multicast Responsibility. 15961 * 15962 * If we have joined allmulti on this one, find a new member 15963 * in the group to join allmulti. As this ill is already part 15964 * of allmulti, we don't have to join on this one. 15965 * 15966 * If we have not joined allmulti on this one, there is no 15967 * responsibility to handoff. But we need to take new 15968 * responsibility i.e, join allmulti on this one if we need 15969 * to. 15970 */ 15971 if (ill->ill_join_allmulti) { 15972 (void) ill_nominate_mcast_rcv(illgrp); 15973 } else { 15974 for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { 15975 if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) { 15976 (void) ip_join_allmulti(ill->ill_ipif); 15977 break; 15978 } 15979 } 15980 } 15981 15982 /* 15983 * We intentionally do the flushing of IRE_CACHES only matching 15984 * on the ill and not on groups. Note that we are already deleted 15985 * from the group. 15986 * 15987 * This will make sure that all IRE_CACHES whose stq is pointing 15988 * at ill_wq or ire_ipif->ipif_ill pointing at this ill will get 15989 * deleted and IRE_CACHES that are not pointing at this ill will 15990 * be left alone. 15991 */ 15992 if (ill->ill_isv6) { 15993 ire_walk_ill_v6(MATCH_IRE_ILL | MATCH_IRE_TYPE, 15994 IRE_CACHE, illgrp_cache_delete, (char *)ill, ill); 15995 } else { 15996 ire_walk_ill_v4(MATCH_IRE_ILL | MATCH_IRE_TYPE, 15997 IRE_CACHE, illgrp_cache_delete, (char *)ill, ill); 15998 } 15999 16000 /* 16001 * Some conn may have cached one of the IREs deleted above. By removing 16002 * the ire reference, we clean up the extra reference to the ill held in 16003 * ire->ire_stq. 16004 */ 16005 ipcl_walk(conn_cleanup_stale_ire, NULL, ipst); 16006 16007 /* 16008 * Re-do source address selection for all the members in the 16009 * group, if they borrowed source address from one of the ipifs 16010 * in this ill. 16011 */ 16012 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 16013 if (ill->ill_isv6) { 16014 ipif_update_other_ipifs_v6(ipif, illgrp); 16015 } else { 16016 ipif_update_other_ipifs(ipif, illgrp); 16017 } 16018 } 16019 } 16020 16021 /* 16022 * Delete the ill from the group. The caller makes sure that it is 16023 * in a group and it okay to delete from the group. So, we always 16024 * delete here. 16025 */ 16026 static void 16027 illgrp_delete(ill_t *ill) 16028 { 16029 ill_group_t *illgrp; 16030 ill_group_t *tmpg; 16031 ill_t *tmp_ill; 16032 ip_stack_t *ipst = ill->ill_ipst; 16033 16034 /* 16035 * Reset illgrp_ill_schednext if it was pointing at us. 16036 * We need to do this before we set ill_group to NULL. 16037 */ 16038 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 16039 mutex_enter(&ill->ill_lock); 16040 16041 illgrp_reset_schednext(ill); 16042 16043 illgrp = ill->ill_group; 16044 16045 /* Delete the ill from illgrp. */ 16046 if (illgrp->illgrp_ill == ill) { 16047 illgrp->illgrp_ill = ill->ill_group_next; 16048 } else { 16049 tmp_ill = illgrp->illgrp_ill; 16050 while (tmp_ill->ill_group_next != ill) { 16051 tmp_ill = tmp_ill->ill_group_next; 16052 ASSERT(tmp_ill != NULL); 16053 } 16054 tmp_ill->ill_group_next = ill->ill_group_next; 16055 } 16056 ill->ill_group = NULL; 16057 ill->ill_group_next = NULL; 16058 16059 illgrp->illgrp_ill_count--; 16060 mutex_exit(&ill->ill_lock); 16061 rw_exit(&ipst->ips_ill_g_lock); 16062 16063 /* 16064 * As this ill is leaving the group, we need to hand off 16065 * the responsibilities to the other ills in the group, if 16066 * this ill had some responsibilities. 16067 */ 16068 16069 ill_handoff_responsibility(ill, illgrp); 16070 16071 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 16072 16073 if (illgrp->illgrp_ill_count == 0) { 16074 16075 ASSERT(illgrp->illgrp_ill == NULL); 16076 if (ill->ill_isv6) { 16077 if (illgrp == ipst->ips_illgrp_head_v6) { 16078 ipst->ips_illgrp_head_v6 = illgrp->illgrp_next; 16079 } else { 16080 tmpg = ipst->ips_illgrp_head_v6; 16081 while (tmpg->illgrp_next != illgrp) { 16082 tmpg = tmpg->illgrp_next; 16083 ASSERT(tmpg != NULL); 16084 } 16085 tmpg->illgrp_next = illgrp->illgrp_next; 16086 } 16087 } else { 16088 if (illgrp == ipst->ips_illgrp_head_v4) { 16089 ipst->ips_illgrp_head_v4 = illgrp->illgrp_next; 16090 } else { 16091 tmpg = ipst->ips_illgrp_head_v4; 16092 while (tmpg->illgrp_next != illgrp) { 16093 tmpg = tmpg->illgrp_next; 16094 ASSERT(tmpg != NULL); 16095 } 16096 tmpg->illgrp_next = illgrp->illgrp_next; 16097 } 16098 } 16099 mutex_destroy(&illgrp->illgrp_lock); 16100 mi_free(illgrp); 16101 } 16102 rw_exit(&ipst->ips_ill_g_lock); 16103 16104 /* 16105 * Even though the ill is out of the group its not necessary 16106 * to set ipsq_split as TRUE as the ipifs could be down temporarily 16107 * We will split the ipsq when phyint_groupname is set to NULL. 16108 */ 16109 16110 /* 16111 * Send a routing sockets message if we are deleting from 16112 * groups with names. 16113 */ 16114 if (ill->ill_phyint->phyint_groupname_len != 0) 16115 ip_rts_ifmsg(ill->ill_ipif); 16116 } 16117 16118 /* 16119 * Re-do source address selection. This is normally called when 16120 * an ill joins the group or when a non-NOLOCAL/DEPRECATED/ANYCAST 16121 * ipif comes up. 16122 */ 16123 void 16124 ill_update_source_selection(ill_t *ill) 16125 { 16126 ipif_t *ipif; 16127 16128 ASSERT(IAM_WRITER_ILL(ill)); 16129 16130 if (ill->ill_group != NULL) 16131 ill = ill->ill_group->illgrp_ill; 16132 16133 for (; ill != NULL; ill = ill->ill_group_next) { 16134 for (ipif = ill->ill_ipif; ipif != NULL; 16135 ipif = ipif->ipif_next) { 16136 if (ill->ill_isv6) 16137 ipif_recreate_interface_routes_v6(NULL, ipif); 16138 else 16139 ipif_recreate_interface_routes(NULL, ipif); 16140 } 16141 } 16142 } 16143 16144 /* 16145 * Insert ill in a group headed by illgrp_head. The caller can either 16146 * pass a groupname in which case we search for a group with the 16147 * same name to insert in or pass a group to insert in. This function 16148 * would only search groups with names. 16149 * 16150 * NOTE : The caller should make sure that there is at least one ipif 16151 * UP on this ill so that illgrp_scheduler can pick this ill 16152 * for outbound packets. If ill_ipif_up_count is zero, we have 16153 * already sent a DL_UNBIND to the driver and we don't want to 16154 * send anymore packets. We don't assert for ipif_up_count 16155 * to be greater than zero, because ipif_up_done wants to call 16156 * this function before bumping up the ipif_up_count. See 16157 * ipif_up_done() for details. 16158 */ 16159 int 16160 illgrp_insert(ill_group_t **illgrp_head, ill_t *ill, char *groupname, 16161 ill_group_t *grp_to_insert, boolean_t ipif_is_coming_up) 16162 { 16163 ill_group_t *illgrp; 16164 ill_t *prev_ill; 16165 phyint_t *phyi; 16166 ip_stack_t *ipst = ill->ill_ipst; 16167 16168 ASSERT(ill->ill_group == NULL); 16169 16170 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 16171 mutex_enter(&ill->ill_lock); 16172 16173 if (groupname != NULL) { 16174 /* 16175 * Look for a group with a matching groupname to insert. 16176 */ 16177 for (illgrp = *illgrp_head; illgrp != NULL; 16178 illgrp = illgrp->illgrp_next) { 16179 16180 ill_t *tmp_ill; 16181 16182 /* 16183 * If we have an ill_group_t in the list which has 16184 * no ill_t assigned then we must be in the process of 16185 * removing this group. We skip this as illgrp_delete() 16186 * will remove it from the list. 16187 */ 16188 if ((tmp_ill = illgrp->illgrp_ill) == NULL) { 16189 ASSERT(illgrp->illgrp_ill_count == 0); 16190 continue; 16191 } 16192 16193 ASSERT(tmp_ill->ill_phyint != NULL); 16194 phyi = tmp_ill->ill_phyint; 16195 /* 16196 * Look at groups which has names only. 16197 */ 16198 if (phyi->phyint_groupname_len == 0) 16199 continue; 16200 /* 16201 * Names are stored in the phyint common to both 16202 * IPv4 and IPv6. 16203 */ 16204 if (mi_strcmp(phyi->phyint_groupname, 16205 groupname) == 0) { 16206 break; 16207 } 16208 } 16209 } else { 16210 /* 16211 * If the caller passes in a NULL "grp_to_insert", we 16212 * allocate one below and insert this singleton. 16213 */ 16214 illgrp = grp_to_insert; 16215 } 16216 16217 ill->ill_group_next = NULL; 16218 16219 if (illgrp == NULL) { 16220 illgrp = (ill_group_t *)mi_zalloc(sizeof (ill_group_t)); 16221 if (illgrp == NULL) { 16222 return (ENOMEM); 16223 } 16224 illgrp->illgrp_next = *illgrp_head; 16225 *illgrp_head = illgrp; 16226 illgrp->illgrp_ill = ill; 16227 illgrp->illgrp_ill_count = 1; 16228 ill->ill_group = illgrp; 16229 /* 16230 * Used in illgrp_scheduler to protect multiple threads 16231 * from traversing the list. 16232 */ 16233 mutex_init(&illgrp->illgrp_lock, NULL, MUTEX_DEFAULT, 0); 16234 } else { 16235 ASSERT(ill->ill_net_type == 16236 illgrp->illgrp_ill->ill_net_type); 16237 ASSERT(ill->ill_type == illgrp->illgrp_ill->ill_type); 16238 16239 /* Insert ill at tail of this group */ 16240 prev_ill = illgrp->illgrp_ill; 16241 while (prev_ill->ill_group_next != NULL) 16242 prev_ill = prev_ill->ill_group_next; 16243 prev_ill->ill_group_next = ill; 16244 ill->ill_group = illgrp; 16245 illgrp->illgrp_ill_count++; 16246 /* 16247 * Inherit group properties. Currently only forwarding 16248 * is the property we try to keep the same with all the 16249 * ills. When there are more, we will abstract this into 16250 * a function. 16251 */ 16252 ill->ill_flags &= ~ILLF_ROUTER; 16253 ill->ill_flags |= (illgrp->illgrp_ill->ill_flags & ILLF_ROUTER); 16254 } 16255 mutex_exit(&ill->ill_lock); 16256 rw_exit(&ipst->ips_ill_g_lock); 16257 16258 /* 16259 * 1) When ipif_up_done() calls this function, ipif_up_count 16260 * may be zero as it has not yet been bumped. But the ires 16261 * have already been added. So, we do the nomination here 16262 * itself. But, when ip_sioctl_groupname calls this, it checks 16263 * for ill_ipif_up_count != 0. Thus we don't check for 16264 * ill_ipif_up_count here while nominating broadcast ires for 16265 * receive. 16266 * 16267 * 2) Similarly, we need to call ill_group_bcast_for_xmit here 16268 * to group them properly as ire_add() has already happened 16269 * in the ipif_up_done() case. For ip_sioctl_groupname/ifgrp_insert 16270 * case, we need to do it here anyway. 16271 */ 16272 if (!ill->ill_isv6) { 16273 ill_group_bcast_for_xmit(ill); 16274 ill_nominate_bcast_rcv(illgrp); 16275 } 16276 16277 if (!ipif_is_coming_up) { 16278 /* 16279 * When ipif_up_done() calls this function, the multicast 16280 * groups have not been joined yet. So, there is no point in 16281 * nomination. ip_join_allmulti will handle groups when 16282 * ill_recover_multicast is called from ipif_up_done() later. 16283 */ 16284 (void) ill_nominate_mcast_rcv(illgrp); 16285 /* 16286 * ipif_up_done calls ill_update_source_selection 16287 * anyway. Moreover, we don't want to re-create 16288 * interface routes while ipif_up_done() still has reference 16289 * to them. Refer to ipif_up_done() for more details. 16290 */ 16291 ill_update_source_selection(ill); 16292 } 16293 16294 /* 16295 * Send a routing sockets message if we are inserting into 16296 * groups with names. 16297 */ 16298 if (groupname != NULL) 16299 ip_rts_ifmsg(ill->ill_ipif); 16300 return (0); 16301 } 16302 16303 /* 16304 * Return the first phyint matching the groupname. There could 16305 * be more than one when there are ill groups. 16306 * 16307 * If 'usable' is set, then we exclude ones that are marked with any of 16308 * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE). 16309 * Needs work: called only from ip_sioctl_groupname and from the ipmp/netinfo 16310 * emulation of ipmp. 16311 */ 16312 phyint_t * 16313 phyint_lookup_group(char *groupname, boolean_t usable, ip_stack_t *ipst) 16314 { 16315 phyint_t *phyi; 16316 16317 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock)); 16318 /* 16319 * Group names are stored in the phyint - a common structure 16320 * to both IPv4 and IPv6. 16321 */ 16322 phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index); 16323 for (; phyi != NULL; 16324 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 16325 phyi, AVL_AFTER)) { 16326 if (phyi->phyint_groupname_len == 0) 16327 continue; 16328 /* 16329 * Skip the ones that should not be used since the callers 16330 * sometime use this for sending packets. 16331 */ 16332 if (usable && (phyi->phyint_flags & 16333 (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE))) 16334 continue; 16335 16336 ASSERT(phyi->phyint_groupname != NULL); 16337 if (mi_strcmp(groupname, phyi->phyint_groupname) == 0) 16338 return (phyi); 16339 } 16340 return (NULL); 16341 } 16342 16343 16344 /* 16345 * Return the first usable phyint matching the group index. By 'usable' 16346 * we exclude ones that are marked ununsable with any of 16347 * (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE). 16348 * 16349 * Used only for the ipmp/netinfo emulation of ipmp. 16350 */ 16351 phyint_t * 16352 phyint_lookup_group_ifindex(uint_t group_ifindex, ip_stack_t *ipst) 16353 { 16354 phyint_t *phyi; 16355 16356 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock)); 16357 16358 if (!ipst->ips_ipmp_hook_emulation) 16359 return (NULL); 16360 16361 /* 16362 * Group indicies are stored in the phyint - a common structure 16363 * to both IPv4 and IPv6. 16364 */ 16365 phyi = avl_first(&ipst->ips_phyint_g_list->phyint_list_avl_by_index); 16366 for (; phyi != NULL; 16367 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 16368 phyi, AVL_AFTER)) { 16369 /* Ignore the ones that do not have a group */ 16370 if (phyi->phyint_groupname_len == 0) 16371 continue; 16372 16373 ASSERT(phyi->phyint_group_ifindex != 0); 16374 /* 16375 * Skip the ones that should not be used since the callers 16376 * sometime use this for sending packets. 16377 */ 16378 if (phyi->phyint_flags & 16379 (PHYI_FAILED|PHYI_OFFLINE|PHYI_INACTIVE)) 16380 continue; 16381 if (phyi->phyint_group_ifindex == group_ifindex) 16382 return (phyi); 16383 } 16384 return (NULL); 16385 } 16386 16387 16388 /* 16389 * MT notes on creation and deletion of IPMP groups 16390 * 16391 * Creation and deletion of IPMP groups introduce the need to merge or 16392 * split the associated serialization objects i.e the ipsq's. Normally all 16393 * the ills in an IPMP group would map to a single ipsq. If IPMP is not enabled 16394 * an ill-pair(v4, v6) i.e. phyint would map to a single ipsq. However during 16395 * the execution of the SIOCSLIFGROUPNAME command the picture changes. There 16396 * is a need to change the <ill-ipsq> association and we have to operate on both 16397 * the source and destination IPMP groups. For eg. attempting to set the 16398 * groupname of hme0 to mpk17-85 when it already belongs to mpk17-84 has to 16399 * handle 2 IPMP groups and 2 ipsqs. All the ills belonging to either of the 16400 * source or destination IPMP group are mapped to a single ipsq for executing 16401 * the SIOCSLIFGROUPNAME command. This is termed as a merge of the ipsq's. 16402 * The <ill-ipsq> mapping is restored back to normal at a later point. This is 16403 * termed as a split of the ipsq. The converse of the merge i.e. a split of the 16404 * ipsq happens while unwinding from ipsq_exit. If at least 1 set groupname 16405 * occurred on the ipsq, then the ipsq_split flag is set. This indicates the 16406 * ipsq has to be examined for redoing the <ill-ipsq> associations. 16407 * 16408 * In the above example the ioctl handling code locates the current ipsq of hme0 16409 * which is ipsq(mpk17-84). It then enters the above ipsq immediately or 16410 * eventually (after queueing the ioctl in ipsq(mpk17-84)). Then it locates 16411 * the destination ipsq which is ipsq(mpk17-85) and merges the source ipsq into 16412 * the destination ipsq. If the destination ipsq is not busy, it also enters 16413 * the destination ipsq exclusively. Now the actual groupname setting operation 16414 * can proceed. If the destination ipsq is busy, the operation is enqueued 16415 * on the destination (merged) ipsq and will be handled in the unwind from 16416 * ipsq_exit. 16417 * 16418 * To prevent other threads accessing the ill while the group name change is 16419 * in progres, we bring down the ipifs which also removes the ill from the 16420 * group. The group is changed in phyint and when the first ipif on the ill 16421 * is brought up, the ill is inserted into the right IPMP group by 16422 * illgrp_insert. 16423 */ 16424 /* ARGSUSED */ 16425 int 16426 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 16427 ip_ioctl_cmd_t *ipip, void *ifreq) 16428 { 16429 int i; 16430 char *tmp; 16431 int namelen; 16432 ill_t *ill = ipif->ipif_ill; 16433 ill_t *ill_v4, *ill_v6; 16434 int err = 0; 16435 phyint_t *phyi; 16436 phyint_t *phyi_tmp; 16437 struct lifreq *lifr; 16438 mblk_t *mp1; 16439 char *groupname; 16440 ipsq_t *ipsq; 16441 ip_stack_t *ipst = ill->ill_ipst; 16442 16443 ASSERT(IAM_WRITER_IPIF(ipif)); 16444 16445 /* Existance verified in ip_wput_nondata */ 16446 mp1 = mp->b_cont->b_cont; 16447 lifr = (struct lifreq *)mp1->b_rptr; 16448 groupname = lifr->lifr_groupname; 16449 16450 if (ipif->ipif_id != 0) 16451 return (EINVAL); 16452 16453 phyi = ill->ill_phyint; 16454 ASSERT(phyi != NULL); 16455 16456 if (phyi->phyint_flags & PHYI_VIRTUAL) 16457 return (EINVAL); 16458 16459 tmp = groupname; 16460 for (i = 0; i < LIFNAMSIZ && *tmp != '\0'; tmp++, i++) 16461 ; 16462 16463 if (i == LIFNAMSIZ) { 16464 /* no null termination */ 16465 return (EINVAL); 16466 } 16467 16468 /* 16469 * Calculate the namelen exclusive of the null 16470 * termination character. 16471 */ 16472 namelen = tmp - groupname; 16473 16474 ill_v4 = phyi->phyint_illv4; 16475 ill_v6 = phyi->phyint_illv6; 16476 16477 /* 16478 * ILL cannot be part of a usesrc group and and IPMP group at the 16479 * same time. No need to grab the ill_g_usesrc_lock here, see 16480 * synchronization notes in ip.c 16481 */ 16482 if (ipif->ipif_ill->ill_usesrc_grp_next != NULL) { 16483 return (EINVAL); 16484 } 16485 16486 /* 16487 * mark the ill as changing. 16488 * this should queue all new requests on the syncq. 16489 */ 16490 GRAB_ILL_LOCKS(ill_v4, ill_v6); 16491 16492 if (ill_v4 != NULL) 16493 ill_v4->ill_state_flags |= ILL_CHANGING; 16494 if (ill_v6 != NULL) 16495 ill_v6->ill_state_flags |= ILL_CHANGING; 16496 RELEASE_ILL_LOCKS(ill_v4, ill_v6); 16497 16498 if (namelen == 0) { 16499 /* 16500 * Null string means remove this interface from the 16501 * existing group. 16502 */ 16503 if (phyi->phyint_groupname_len == 0) { 16504 /* 16505 * Never was in a group. 16506 */ 16507 err = 0; 16508 goto done; 16509 } 16510 16511 /* 16512 * IPv4 or IPv6 may be temporarily out of the group when all 16513 * the ipifs are down. Thus, we need to check for ill_group to 16514 * be non-NULL. 16515 */ 16516 if (ill_v4 != NULL && ill_v4->ill_group != NULL) { 16517 ill_down_ipifs(ill_v4, mp, 0, B_FALSE); 16518 mutex_enter(&ill_v4->ill_lock); 16519 if (!ill_is_quiescent(ill_v4)) { 16520 /* 16521 * ipsq_pending_mp_add will not fail since 16522 * connp is NULL 16523 */ 16524 (void) ipsq_pending_mp_add(NULL, 16525 ill_v4->ill_ipif, q, mp, ILL_DOWN); 16526 mutex_exit(&ill_v4->ill_lock); 16527 err = EINPROGRESS; 16528 goto done; 16529 } 16530 mutex_exit(&ill_v4->ill_lock); 16531 } 16532 16533 if (ill_v6 != NULL && ill_v6->ill_group != NULL) { 16534 ill_down_ipifs(ill_v6, mp, 0, B_FALSE); 16535 mutex_enter(&ill_v6->ill_lock); 16536 if (!ill_is_quiescent(ill_v6)) { 16537 (void) ipsq_pending_mp_add(NULL, 16538 ill_v6->ill_ipif, q, mp, ILL_DOWN); 16539 mutex_exit(&ill_v6->ill_lock); 16540 err = EINPROGRESS; 16541 goto done; 16542 } 16543 mutex_exit(&ill_v6->ill_lock); 16544 } 16545 16546 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 16547 GRAB_ILL_LOCKS(ill_v4, ill_v6); 16548 mutex_enter(&phyi->phyint_lock); 16549 ASSERT(phyi->phyint_groupname != NULL); 16550 mi_free(phyi->phyint_groupname); 16551 phyi->phyint_groupname = NULL; 16552 phyi->phyint_groupname_len = 0; 16553 16554 /* Restore the ifindex used to be the per interface one */ 16555 phyi->phyint_group_ifindex = 0; 16556 phyi->phyint_hook_ifindex = phyi->phyint_ifindex; 16557 mutex_exit(&phyi->phyint_lock); 16558 RELEASE_ILL_LOCKS(ill_v4, ill_v6); 16559 rw_exit(&ipst->ips_ill_g_lock); 16560 err = ill_up_ipifs(ill, q, mp); 16561 16562 /* 16563 * set the split flag so that the ipsq can be split 16564 */ 16565 mutex_enter(&phyi->phyint_ipsq->ipsq_lock); 16566 phyi->phyint_ipsq->ipsq_split = B_TRUE; 16567 mutex_exit(&phyi->phyint_ipsq->ipsq_lock); 16568 16569 } else { 16570 if (phyi->phyint_groupname_len != 0) { 16571 ASSERT(phyi->phyint_groupname != NULL); 16572 /* Are we inserting in the same group ? */ 16573 if (mi_strcmp(groupname, 16574 phyi->phyint_groupname) == 0) { 16575 err = 0; 16576 goto done; 16577 } 16578 } 16579 16580 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 16581 /* 16582 * Merge ipsq for the group's. 16583 * This check is here as multiple groups/ills might be 16584 * sharing the same ipsq. 16585 * If we have to merege than the operation is restarted 16586 * on the new ipsq. 16587 */ 16588 ipsq = ip_ipsq_lookup(groupname, B_FALSE, NULL, ipst); 16589 if (phyi->phyint_ipsq != ipsq) { 16590 rw_exit(&ipst->ips_ill_g_lock); 16591 err = ill_merge_groups(ill, NULL, groupname, mp, q); 16592 goto done; 16593 } 16594 /* 16595 * Running exclusive on new ipsq. 16596 */ 16597 16598 ASSERT(ipsq != NULL); 16599 ASSERT(ipsq->ipsq_writer == curthread); 16600 16601 /* 16602 * Check whether the ill_type and ill_net_type matches before 16603 * we allocate any memory so that the cleanup is easier. 16604 * 16605 * We can't group dissimilar ones as we can't load spread 16606 * packets across the group because of potential link-level 16607 * header differences. 16608 */ 16609 phyi_tmp = phyint_lookup_group(groupname, B_FALSE, ipst); 16610 if (phyi_tmp != NULL) { 16611 if ((ill_v4 != NULL && 16612 phyi_tmp->phyint_illv4 != NULL) && 16613 ((ill_v4->ill_net_type != 16614 phyi_tmp->phyint_illv4->ill_net_type) || 16615 (ill_v4->ill_type != 16616 phyi_tmp->phyint_illv4->ill_type))) { 16617 mutex_enter(&phyi->phyint_ipsq->ipsq_lock); 16618 phyi->phyint_ipsq->ipsq_split = B_TRUE; 16619 mutex_exit(&phyi->phyint_ipsq->ipsq_lock); 16620 rw_exit(&ipst->ips_ill_g_lock); 16621 return (EINVAL); 16622 } 16623 if ((ill_v6 != NULL && 16624 phyi_tmp->phyint_illv6 != NULL) && 16625 ((ill_v6->ill_net_type != 16626 phyi_tmp->phyint_illv6->ill_net_type) || 16627 (ill_v6->ill_type != 16628 phyi_tmp->phyint_illv6->ill_type))) { 16629 mutex_enter(&phyi->phyint_ipsq->ipsq_lock); 16630 phyi->phyint_ipsq->ipsq_split = B_TRUE; 16631 mutex_exit(&phyi->phyint_ipsq->ipsq_lock); 16632 rw_exit(&ipst->ips_ill_g_lock); 16633 return (EINVAL); 16634 } 16635 } 16636 16637 rw_exit(&ipst->ips_ill_g_lock); 16638 16639 /* 16640 * bring down all v4 ipifs. 16641 */ 16642 if (ill_v4 != NULL) { 16643 ill_down_ipifs(ill_v4, mp, 0, B_FALSE); 16644 } 16645 16646 /* 16647 * bring down all v6 ipifs. 16648 */ 16649 if (ill_v6 != NULL) { 16650 ill_down_ipifs(ill_v6, mp, 0, B_FALSE); 16651 } 16652 16653 /* 16654 * make sure all ipifs are down and there are no active 16655 * references. Call to ipsq_pending_mp_add will not fail 16656 * since connp is NULL. 16657 */ 16658 if (ill_v4 != NULL) { 16659 mutex_enter(&ill_v4->ill_lock); 16660 if (!ill_is_quiescent(ill_v4)) { 16661 (void) ipsq_pending_mp_add(NULL, 16662 ill_v4->ill_ipif, q, mp, ILL_DOWN); 16663 mutex_exit(&ill_v4->ill_lock); 16664 err = EINPROGRESS; 16665 goto done; 16666 } 16667 mutex_exit(&ill_v4->ill_lock); 16668 } 16669 16670 if (ill_v6 != NULL) { 16671 mutex_enter(&ill_v6->ill_lock); 16672 if (!ill_is_quiescent(ill_v6)) { 16673 (void) ipsq_pending_mp_add(NULL, 16674 ill_v6->ill_ipif, q, mp, ILL_DOWN); 16675 mutex_exit(&ill_v6->ill_lock); 16676 err = EINPROGRESS; 16677 goto done; 16678 } 16679 mutex_exit(&ill_v6->ill_lock); 16680 } 16681 16682 /* 16683 * allocate including space for null terminator 16684 * before we insert. 16685 */ 16686 tmp = (char *)mi_alloc(namelen + 1, BPRI_MED); 16687 if (tmp == NULL) 16688 return (ENOMEM); 16689 16690 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 16691 GRAB_ILL_LOCKS(ill_v4, ill_v6); 16692 mutex_enter(&phyi->phyint_lock); 16693 if (phyi->phyint_groupname_len != 0) { 16694 ASSERT(phyi->phyint_groupname != NULL); 16695 mi_free(phyi->phyint_groupname); 16696 } 16697 16698 /* 16699 * setup the new group name. 16700 */ 16701 phyi->phyint_groupname = tmp; 16702 bcopy(groupname, phyi->phyint_groupname, namelen + 1); 16703 phyi->phyint_groupname_len = namelen + 1; 16704 16705 if (ipst->ips_ipmp_hook_emulation) { 16706 /* 16707 * If the group already exists we use the existing 16708 * group_ifindex, otherwise we pick a new index here. 16709 */ 16710 if (phyi_tmp != NULL) { 16711 phyi->phyint_group_ifindex = 16712 phyi_tmp->phyint_group_ifindex; 16713 } else { 16714 /* XXX We need a recovery strategy here. */ 16715 if (!ip_assign_ifindex( 16716 &phyi->phyint_group_ifindex, ipst)) 16717 cmn_err(CE_PANIC, 16718 "ip_assign_ifindex() failed"); 16719 } 16720 } 16721 /* 16722 * Select whether the netinfo and hook use the per-interface 16723 * or per-group ifindex. 16724 */ 16725 if (ipst->ips_ipmp_hook_emulation) 16726 phyi->phyint_hook_ifindex = phyi->phyint_group_ifindex; 16727 else 16728 phyi->phyint_hook_ifindex = phyi->phyint_ifindex; 16729 16730 if (ipst->ips_ipmp_hook_emulation && 16731 phyi_tmp != NULL) { 16732 /* First phyint in group - group PLUMB event */ 16733 ill_nic_info_plumb(ill, B_TRUE); 16734 } 16735 mutex_exit(&phyi->phyint_lock); 16736 RELEASE_ILL_LOCKS(ill_v4, ill_v6); 16737 rw_exit(&ipst->ips_ill_g_lock); 16738 16739 err = ill_up_ipifs(ill, q, mp); 16740 } 16741 16742 done: 16743 /* 16744 * normally ILL_CHANGING is cleared in ill_up_ipifs. 16745 */ 16746 if (err != EINPROGRESS) { 16747 GRAB_ILL_LOCKS(ill_v4, ill_v6); 16748 if (ill_v4 != NULL) 16749 ill_v4->ill_state_flags &= ~ILL_CHANGING; 16750 if (ill_v6 != NULL) 16751 ill_v6->ill_state_flags &= ~ILL_CHANGING; 16752 RELEASE_ILL_LOCKS(ill_v4, ill_v6); 16753 } 16754 return (err); 16755 } 16756 16757 /* ARGSUSED */ 16758 int 16759 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, 16760 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 16761 { 16762 ill_t *ill; 16763 phyint_t *phyi; 16764 struct lifreq *lifr; 16765 mblk_t *mp1; 16766 16767 /* Existence verified in ip_wput_nondata */ 16768 mp1 = mp->b_cont->b_cont; 16769 lifr = (struct lifreq *)mp1->b_rptr; 16770 ill = ipif->ipif_ill; 16771 phyi = ill->ill_phyint; 16772 16773 lifr->lifr_groupname[0] = '\0'; 16774 /* 16775 * ill_group may be null if all the interfaces 16776 * are down. But still, the phyint should always 16777 * hold the name. 16778 */ 16779 if (phyi->phyint_groupname_len != 0) { 16780 bcopy(phyi->phyint_groupname, lifr->lifr_groupname, 16781 phyi->phyint_groupname_len); 16782 } 16783 16784 return (0); 16785 } 16786 16787 16788 typedef struct conn_move_s { 16789 ill_t *cm_from_ill; 16790 ill_t *cm_to_ill; 16791 int cm_ifindex; 16792 } conn_move_t; 16793 16794 /* 16795 * ipcl_walk function for moving conn_multicast_ill for a given ill. 16796 */ 16797 static void 16798 conn_move(conn_t *connp, caddr_t arg) 16799 { 16800 conn_move_t *connm; 16801 int ifindex; 16802 int i; 16803 ill_t *from_ill; 16804 ill_t *to_ill; 16805 ilg_t *ilg; 16806 ilm_t *ret_ilm; 16807 16808 connm = (conn_move_t *)arg; 16809 ifindex = connm->cm_ifindex; 16810 from_ill = connm->cm_from_ill; 16811 to_ill = connm->cm_to_ill; 16812 16813 /* Change IP_BOUND_IF/IPV6_BOUND_IF associations. */ 16814 16815 /* All multicast fields protected by conn_lock */ 16816 mutex_enter(&connp->conn_lock); 16817 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 16818 if ((connp->conn_outgoing_ill == from_ill) && 16819 (ifindex == 0 || connp->conn_orig_bound_ifindex == ifindex)) { 16820 connp->conn_outgoing_ill = to_ill; 16821 connp->conn_incoming_ill = to_ill; 16822 } 16823 16824 /* Change IP_MULTICAST_IF/IPV6_MULTICAST_IF associations */ 16825 16826 if ((connp->conn_multicast_ill == from_ill) && 16827 (ifindex == 0 || connp->conn_orig_multicast_ifindex == ifindex)) { 16828 connp->conn_multicast_ill = connm->cm_to_ill; 16829 } 16830 16831 /* Change IP_XMIT_IF associations */ 16832 if ((connp->conn_xmit_if_ill == from_ill) && 16833 (ifindex == 0 || connp->conn_orig_xmit_ifindex == ifindex)) { 16834 connp->conn_xmit_if_ill = to_ill; 16835 } 16836 /* 16837 * Change the ilg_ill to point to the new one. This assumes 16838 * ilm_move_v6 has moved the ilms to new_ill and the driver 16839 * has been told to receive packets on this interface. 16840 * ilm_move_v6 FAILBACKS all the ilms successfully always. 16841 * But when doing a FAILOVER, it might fail with ENOMEM and so 16842 * some ilms may not have moved. We check to see whether 16843 * the ilms have moved to to_ill. We can't check on from_ill 16844 * as in the process of moving, we could have split an ilm 16845 * in to two - which has the same orig_ifindex and v6group. 16846 * 16847 * For IPv4, ilg_ipif moves implicitly. The code below really 16848 * does not do anything for IPv4 as ilg_ill is NULL for IPv4. 16849 */ 16850 for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) { 16851 ilg = &connp->conn_ilg[i]; 16852 if ((ilg->ilg_ill == from_ill) && 16853 (ifindex == 0 || ilg->ilg_orig_ifindex == ifindex)) { 16854 /* ifindex != 0 indicates failback */ 16855 if (ifindex != 0) { 16856 connp->conn_ilg[i].ilg_ill = to_ill; 16857 continue; 16858 } 16859 16860 ret_ilm = ilm_lookup_ill_index_v6(to_ill, 16861 &ilg->ilg_v6group, ilg->ilg_orig_ifindex, 16862 connp->conn_zoneid); 16863 16864 if (ret_ilm != NULL) 16865 connp->conn_ilg[i].ilg_ill = to_ill; 16866 } 16867 } 16868 mutex_exit(&connp->conn_lock); 16869 } 16870 16871 static void 16872 conn_move_ill(ill_t *from_ill, ill_t *to_ill, int ifindex) 16873 { 16874 conn_move_t connm; 16875 ip_stack_t *ipst = from_ill->ill_ipst; 16876 16877 connm.cm_from_ill = from_ill; 16878 connm.cm_to_ill = to_ill; 16879 connm.cm_ifindex = ifindex; 16880 16881 ipcl_walk(conn_move, (caddr_t)&connm, ipst); 16882 } 16883 16884 /* 16885 * ilm has been moved from from_ill to to_ill. 16886 * Send DL_DISABMULTI_REQ to ill and DL_ENABMULTI_REQ on to_ill. 16887 * appropriately. 16888 * 16889 * NOTE : We can't reuse the code in ip_ll_addmulti/delmulti because 16890 * the code there de-references ipif_ill to get the ill to 16891 * send multicast requests. It does not work as ipif is on its 16892 * move and already moved when this function is called. 16893 * Thus, we need to use from_ill and to_ill send down multicast 16894 * requests. 16895 */ 16896 static void 16897 ilm_send_multicast_reqs(ill_t *from_ill, ill_t *to_ill) 16898 { 16899 ipif_t *ipif; 16900 ilm_t *ilm; 16901 16902 /* 16903 * See whether we need to send down DL_ENABMULTI_REQ on 16904 * to_ill as ilm has just been added. 16905 */ 16906 ASSERT(IAM_WRITER_ILL(to_ill)); 16907 ASSERT(IAM_WRITER_ILL(from_ill)); 16908 16909 ILM_WALKER_HOLD(to_ill); 16910 for (ilm = to_ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { 16911 16912 if (!ilm->ilm_is_new || (ilm->ilm_flags & ILM_DELETED)) 16913 continue; 16914 /* 16915 * no locks held, ill/ipif cannot dissappear as long 16916 * as we are writer. 16917 */ 16918 ipif = to_ill->ill_ipif; 16919 /* 16920 * No need to hold any lock as we are the writer and this 16921 * can only be changed by a writer. 16922 */ 16923 ilm->ilm_is_new = B_FALSE; 16924 16925 if (to_ill->ill_net_type != IRE_IF_RESOLVER || 16926 ipif->ipif_flags & IPIF_POINTOPOINT) { 16927 ip1dbg(("ilm_send_multicast_reqs: to_ill not " 16928 "resolver\n")); 16929 continue; /* Must be IRE_IF_NORESOLVER */ 16930 } 16931 16932 16933 if (to_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) { 16934 ip1dbg(("ilm_send_multicast_reqs: " 16935 "to_ill MULTI_BCAST\n")); 16936 goto from; 16937 } 16938 16939 if (to_ill->ill_isv6) 16940 mld_joingroup(ilm); 16941 else 16942 igmp_joingroup(ilm); 16943 16944 if (to_ill->ill_ipif_up_count == 0) { 16945 /* 16946 * Nobody there. All multicast addresses will be 16947 * re-joined when we get the DL_BIND_ACK bringing the 16948 * interface up. 16949 */ 16950 ilm->ilm_notify_driver = B_FALSE; 16951 ip1dbg(("ilm_send_multicast_reqs: to_ill nobody up\n")); 16952 goto from; 16953 } 16954 16955 /* 16956 * For allmulti address, we want to join on only one interface. 16957 * Checking for ilm_numentries_v6 is not correct as you may 16958 * find an ilm with zero address on to_ill, but we may not 16959 * have nominated to_ill for receiving. Thus, if we have 16960 * nominated from_ill (ill_join_allmulti is set), nominate 16961 * only if to_ill is not already nominated (to_ill normally 16962 * should not have been nominated if "from_ill" has already 16963 * been nominated. As we don't prevent failovers from happening 16964 * across groups, we don't assert). 16965 */ 16966 if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) { 16967 /* 16968 * There is no need to hold ill locks as we are 16969 * writer on both ills and when ill_join_allmulti 16970 * is changed the thread is always a writer. 16971 */ 16972 if (from_ill->ill_join_allmulti && 16973 !to_ill->ill_join_allmulti) { 16974 (void) ip_join_allmulti(to_ill->ill_ipif); 16975 } 16976 } else if (ilm->ilm_notify_driver) { 16977 16978 /* 16979 * This is a newly moved ilm so we need to tell the 16980 * driver about the new group. There can be more than 16981 * one ilm's for the same group in the list each with a 16982 * different orig_ifindex. We have to inform the driver 16983 * once. In ilm_move_v[4,6] we only set the flag 16984 * ilm_notify_driver for the first ilm. 16985 */ 16986 16987 (void) ip_ll_send_enabmulti_req(to_ill, 16988 &ilm->ilm_v6addr); 16989 } 16990 16991 ilm->ilm_notify_driver = B_FALSE; 16992 16993 /* 16994 * See whether we need to send down DL_DISABMULTI_REQ on 16995 * from_ill as ilm has just been removed. 16996 */ 16997 from: 16998 ipif = from_ill->ill_ipif; 16999 if (from_ill->ill_net_type != IRE_IF_RESOLVER || 17000 ipif->ipif_flags & IPIF_POINTOPOINT) { 17001 ip1dbg(("ilm_send_multicast_reqs: " 17002 "from_ill not resolver\n")); 17003 continue; /* Must be IRE_IF_NORESOLVER */ 17004 } 17005 17006 if (from_ill->ill_phyint->phyint_flags & PHYI_MULTI_BCAST) { 17007 ip1dbg(("ilm_send_multicast_reqs: " 17008 "from_ill MULTI_BCAST\n")); 17009 continue; 17010 } 17011 17012 if (IN6_IS_ADDR_UNSPECIFIED(&ilm->ilm_v6addr)) { 17013 if (from_ill->ill_join_allmulti) 17014 (void) ip_leave_allmulti(from_ill->ill_ipif); 17015 } else if (ilm_numentries_v6(from_ill, &ilm->ilm_v6addr) == 0) { 17016 (void) ip_ll_send_disabmulti_req(from_ill, 17017 &ilm->ilm_v6addr); 17018 } 17019 } 17020 ILM_WALKER_RELE(to_ill); 17021 } 17022 17023 /* 17024 * This function is called when all multicast memberships needs 17025 * to be moved from "from_ill" to "to_ill" for IPv6. This function is 17026 * called only once unlike the IPv4 counterpart where it is called after 17027 * every logical interface is moved. The reason is due to multicast 17028 * memberships are joined using an interface address in IPv4 while in 17029 * IPv6, interface index is used. 17030 */ 17031 static void 17032 ilm_move_v6(ill_t *from_ill, ill_t *to_ill, int ifindex) 17033 { 17034 ilm_t *ilm; 17035 ilm_t *ilm_next; 17036 ilm_t *new_ilm; 17037 ilm_t **ilmp; 17038 int count; 17039 char buf[INET6_ADDRSTRLEN]; 17040 in6_addr_t ipv6_snm = ipv6_solicited_node_mcast; 17041 ip_stack_t *ipst = from_ill->ill_ipst; 17042 17043 ASSERT(MUTEX_HELD(&to_ill->ill_lock)); 17044 ASSERT(MUTEX_HELD(&from_ill->ill_lock)); 17045 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 17046 17047 if (ifindex == 0) { 17048 /* 17049 * Form the solicited node mcast address which is used later. 17050 */ 17051 ipif_t *ipif; 17052 17053 ipif = from_ill->ill_ipif; 17054 ASSERT(ipif->ipif_id == 0); 17055 17056 ipv6_snm.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3]; 17057 } 17058 17059 ilmp = &from_ill->ill_ilm; 17060 for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) { 17061 ilm_next = ilm->ilm_next; 17062 17063 if (ilm->ilm_flags & ILM_DELETED) { 17064 ilmp = &ilm->ilm_next; 17065 continue; 17066 } 17067 17068 new_ilm = ilm_lookup_ill_index_v6(to_ill, &ilm->ilm_v6addr, 17069 ilm->ilm_orig_ifindex, ilm->ilm_zoneid); 17070 ASSERT(ilm->ilm_orig_ifindex != 0); 17071 if (ilm->ilm_orig_ifindex == ifindex) { 17072 /* 17073 * We are failing back multicast memberships. 17074 * If the same ilm exists in to_ill, it means somebody 17075 * has joined the same group there e.g. ff02::1 17076 * is joined within the kernel when the interfaces 17077 * came UP. 17078 */ 17079 ASSERT(ilm->ilm_ipif == NULL); 17080 if (new_ilm != NULL) { 17081 new_ilm->ilm_refcnt += ilm->ilm_refcnt; 17082 if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE || 17083 !SLIST_IS_EMPTY(new_ilm->ilm_filter)) { 17084 new_ilm->ilm_is_new = B_TRUE; 17085 } 17086 } else { 17087 /* 17088 * check if we can just move the ilm 17089 */ 17090 if (from_ill->ill_ilm_walker_cnt != 0) { 17091 /* 17092 * We have walkers we cannot move 17093 * the ilm, so allocate a new ilm, 17094 * this (old) ilm will be marked 17095 * ILM_DELETED at the end of the loop 17096 * and will be freed when the 17097 * last walker exits. 17098 */ 17099 new_ilm = (ilm_t *)mi_zalloc 17100 (sizeof (ilm_t)); 17101 if (new_ilm == NULL) { 17102 ip0dbg(("ilm_move_v6: " 17103 "FAILBACK of IPv6" 17104 " multicast address %s : " 17105 "from %s to" 17106 " %s failed : ENOMEM \n", 17107 inet_ntop(AF_INET6, 17108 &ilm->ilm_v6addr, buf, 17109 sizeof (buf)), 17110 from_ill->ill_name, 17111 to_ill->ill_name)); 17112 17113 ilmp = &ilm->ilm_next; 17114 continue; 17115 } 17116 *new_ilm = *ilm; 17117 /* 17118 * we don't want new_ilm linked to 17119 * ilm's filter list. 17120 */ 17121 new_ilm->ilm_filter = NULL; 17122 } else { 17123 /* 17124 * No walkers we can move the ilm. 17125 * lets take it out of the list. 17126 */ 17127 *ilmp = ilm->ilm_next; 17128 ilm->ilm_next = NULL; 17129 new_ilm = ilm; 17130 } 17131 17132 /* 17133 * if this is the first ilm for the group 17134 * set ilm_notify_driver so that we notify the 17135 * driver in ilm_send_multicast_reqs. 17136 */ 17137 if (ilm_lookup_ill_v6(to_ill, 17138 &new_ilm->ilm_v6addr, ALL_ZONES) == NULL) 17139 new_ilm->ilm_notify_driver = B_TRUE; 17140 17141 new_ilm->ilm_ill = to_ill; 17142 /* Add to the to_ill's list */ 17143 new_ilm->ilm_next = to_ill->ill_ilm; 17144 to_ill->ill_ilm = new_ilm; 17145 /* 17146 * set the flag so that mld_joingroup is 17147 * called in ilm_send_multicast_reqs(). 17148 */ 17149 new_ilm->ilm_is_new = B_TRUE; 17150 } 17151 goto bottom; 17152 } else if (ifindex != 0) { 17153 /* 17154 * If this is FAILBACK (ifindex != 0) and the ifindex 17155 * has not matched above, look at the next ilm. 17156 */ 17157 ilmp = &ilm->ilm_next; 17158 continue; 17159 } 17160 /* 17161 * If we are here, it means ifindex is 0. Failover 17162 * everything. 17163 * 17164 * We need to handle solicited node mcast address 17165 * and all_nodes mcast address differently as they 17166 * are joined witin the kenrel (ipif_multicast_up) 17167 * and potentially from the userland. We are called 17168 * after the ipifs of from_ill has been moved. 17169 * If we still find ilms on ill with solicited node 17170 * mcast address or all_nodes mcast address, it must 17171 * belong to the UP interface that has not moved e.g. 17172 * ipif_id 0 with the link local prefix does not move. 17173 * We join this on the new ill accounting for all the 17174 * userland memberships so that applications don't 17175 * see any failure. 17176 * 17177 * We need to make sure that we account only for the 17178 * solicited node and all node multicast addresses 17179 * that was brought UP on these. In the case of 17180 * a failover from A to B, we might have ilms belonging 17181 * to A (ilm_orig_ifindex pointing at A) on B accounting 17182 * for the membership from the userland. If we are failing 17183 * over from B to C now, we will find the ones belonging 17184 * to A on B. These don't account for the ill_ipif_up_count. 17185 * They just move from B to C. The check below on 17186 * ilm_orig_ifindex ensures that. 17187 */ 17188 if ((ilm->ilm_orig_ifindex == 17189 from_ill->ill_phyint->phyint_ifindex) && 17190 (IN6_ARE_ADDR_EQUAL(&ipv6_snm, &ilm->ilm_v6addr) || 17191 IN6_ARE_ADDR_EQUAL(&ipv6_all_hosts_mcast, 17192 &ilm->ilm_v6addr))) { 17193 ASSERT(ilm->ilm_refcnt > 0); 17194 count = ilm->ilm_refcnt - from_ill->ill_ipif_up_count; 17195 /* 17196 * For indentation reasons, we are not using a 17197 * "else" here. 17198 */ 17199 if (count == 0) { 17200 ilmp = &ilm->ilm_next; 17201 continue; 17202 } 17203 ilm->ilm_refcnt -= count; 17204 if (new_ilm != NULL) { 17205 /* 17206 * Can find one with the same 17207 * ilm_orig_ifindex, if we are failing 17208 * over to a STANDBY. This happens 17209 * when somebody wants to join a group 17210 * on a STANDBY interface and we 17211 * internally join on a different one. 17212 * If we had joined on from_ill then, a 17213 * failover now will find a new ilm 17214 * with this index. 17215 */ 17216 ip1dbg(("ilm_move_v6: FAILOVER, found" 17217 " new ilm on %s, group address %s\n", 17218 to_ill->ill_name, 17219 inet_ntop(AF_INET6, 17220 &ilm->ilm_v6addr, buf, 17221 sizeof (buf)))); 17222 new_ilm->ilm_refcnt += count; 17223 if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE || 17224 !SLIST_IS_EMPTY(new_ilm->ilm_filter)) { 17225 new_ilm->ilm_is_new = B_TRUE; 17226 } 17227 } else { 17228 new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t)); 17229 if (new_ilm == NULL) { 17230 ip0dbg(("ilm_move_v6: FAILOVER of IPv6" 17231 " multicast address %s : from %s to" 17232 " %s failed : ENOMEM \n", 17233 inet_ntop(AF_INET6, 17234 &ilm->ilm_v6addr, buf, 17235 sizeof (buf)), from_ill->ill_name, 17236 to_ill->ill_name)); 17237 ilmp = &ilm->ilm_next; 17238 continue; 17239 } 17240 *new_ilm = *ilm; 17241 new_ilm->ilm_filter = NULL; 17242 new_ilm->ilm_refcnt = count; 17243 new_ilm->ilm_timer = INFINITY; 17244 new_ilm->ilm_rtx.rtx_timer = INFINITY; 17245 new_ilm->ilm_is_new = B_TRUE; 17246 /* 17247 * If the to_ill has not joined this 17248 * group we need to tell the driver in 17249 * ill_send_multicast_reqs. 17250 */ 17251 if (ilm_lookup_ill_v6(to_ill, 17252 &new_ilm->ilm_v6addr, ALL_ZONES) == NULL) 17253 new_ilm->ilm_notify_driver = B_TRUE; 17254 17255 new_ilm->ilm_ill = to_ill; 17256 /* Add to the to_ill's list */ 17257 new_ilm->ilm_next = to_ill->ill_ilm; 17258 to_ill->ill_ilm = new_ilm; 17259 ASSERT(new_ilm->ilm_ipif == NULL); 17260 } 17261 if (ilm->ilm_refcnt == 0) { 17262 goto bottom; 17263 } else { 17264 new_ilm->ilm_fmode = MODE_IS_EXCLUDE; 17265 CLEAR_SLIST(new_ilm->ilm_filter); 17266 ilmp = &ilm->ilm_next; 17267 } 17268 continue; 17269 } else { 17270 /* 17271 * ifindex = 0 means, move everything pointing at 17272 * from_ill. We are doing this becuase ill has 17273 * either FAILED or became INACTIVE. 17274 * 17275 * As we would like to move things later back to 17276 * from_ill, we want to retain the identity of this 17277 * ilm. Thus, we don't blindly increment the reference 17278 * count on the ilms matching the address alone. We 17279 * need to match on the ilm_orig_index also. new_ilm 17280 * was obtained by matching ilm_orig_index also. 17281 */ 17282 if (new_ilm != NULL) { 17283 /* 17284 * This is possible only if a previous restore 17285 * was incomplete i.e restore to 17286 * ilm_orig_ifindex left some ilms because 17287 * of some failures. Thus when we are failing 17288 * again, we might find our old friends there. 17289 */ 17290 ip1dbg(("ilm_move_v6: FAILOVER, found new ilm" 17291 " on %s, group address %s\n", 17292 to_ill->ill_name, 17293 inet_ntop(AF_INET6, 17294 &ilm->ilm_v6addr, buf, 17295 sizeof (buf)))); 17296 new_ilm->ilm_refcnt += ilm->ilm_refcnt; 17297 if (new_ilm->ilm_fmode != MODE_IS_EXCLUDE || 17298 !SLIST_IS_EMPTY(new_ilm->ilm_filter)) { 17299 new_ilm->ilm_is_new = B_TRUE; 17300 } 17301 } else { 17302 if (from_ill->ill_ilm_walker_cnt != 0) { 17303 new_ilm = (ilm_t *) 17304 mi_zalloc(sizeof (ilm_t)); 17305 if (new_ilm == NULL) { 17306 ip0dbg(("ilm_move_v6: " 17307 "FAILOVER of IPv6" 17308 " multicast address %s : " 17309 "from %s to" 17310 " %s failed : ENOMEM \n", 17311 inet_ntop(AF_INET6, 17312 &ilm->ilm_v6addr, buf, 17313 sizeof (buf)), 17314 from_ill->ill_name, 17315 to_ill->ill_name)); 17316 17317 ilmp = &ilm->ilm_next; 17318 continue; 17319 } 17320 *new_ilm = *ilm; 17321 new_ilm->ilm_filter = NULL; 17322 } else { 17323 *ilmp = ilm->ilm_next; 17324 new_ilm = ilm; 17325 } 17326 /* 17327 * If the to_ill has not joined this 17328 * group we need to tell the driver in 17329 * ill_send_multicast_reqs. 17330 */ 17331 if (ilm_lookup_ill_v6(to_ill, 17332 &new_ilm->ilm_v6addr, ALL_ZONES) == NULL) 17333 new_ilm->ilm_notify_driver = B_TRUE; 17334 17335 /* Add to the to_ill's list */ 17336 new_ilm->ilm_next = to_ill->ill_ilm; 17337 to_ill->ill_ilm = new_ilm; 17338 ASSERT(ilm->ilm_ipif == NULL); 17339 new_ilm->ilm_ill = to_ill; 17340 new_ilm->ilm_is_new = B_TRUE; 17341 } 17342 17343 } 17344 17345 bottom: 17346 /* 17347 * Revert multicast filter state to (EXCLUDE, NULL). 17348 * new_ilm->ilm_is_new should already be set if needed. 17349 */ 17350 new_ilm->ilm_fmode = MODE_IS_EXCLUDE; 17351 CLEAR_SLIST(new_ilm->ilm_filter); 17352 /* 17353 * We allocated/got a new ilm, free the old one. 17354 */ 17355 if (new_ilm != ilm) { 17356 if (from_ill->ill_ilm_walker_cnt == 0) { 17357 *ilmp = ilm->ilm_next; 17358 ilm->ilm_next = NULL; 17359 FREE_SLIST(ilm->ilm_filter); 17360 FREE_SLIST(ilm->ilm_pendsrcs); 17361 FREE_SLIST(ilm->ilm_rtx.rtx_allow); 17362 FREE_SLIST(ilm->ilm_rtx.rtx_block); 17363 mi_free((char *)ilm); 17364 } else { 17365 ilm->ilm_flags |= ILM_DELETED; 17366 from_ill->ill_ilm_cleanup_reqd = 1; 17367 ilmp = &ilm->ilm_next; 17368 } 17369 } 17370 } 17371 } 17372 17373 /* 17374 * Move all the multicast memberships to to_ill. Called when 17375 * an ipif moves from "from_ill" to "to_ill". This function is slightly 17376 * different from IPv6 counterpart as multicast memberships are associated 17377 * with ills in IPv6. This function is called after every ipif is moved 17378 * unlike IPv6, where it is moved only once. 17379 */ 17380 static void 17381 ilm_move_v4(ill_t *from_ill, ill_t *to_ill, ipif_t *ipif) 17382 { 17383 ilm_t *ilm; 17384 ilm_t *ilm_next; 17385 ilm_t *new_ilm; 17386 ilm_t **ilmp; 17387 ip_stack_t *ipst = from_ill->ill_ipst; 17388 17389 ASSERT(MUTEX_HELD(&to_ill->ill_lock)); 17390 ASSERT(MUTEX_HELD(&from_ill->ill_lock)); 17391 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 17392 17393 ilmp = &from_ill->ill_ilm; 17394 for (ilm = from_ill->ill_ilm; ilm != NULL; ilm = ilm_next) { 17395 ilm_next = ilm->ilm_next; 17396 17397 if (ilm->ilm_flags & ILM_DELETED) { 17398 ilmp = &ilm->ilm_next; 17399 continue; 17400 } 17401 17402 ASSERT(ilm->ilm_ipif != NULL); 17403 17404 if (ilm->ilm_ipif != ipif) { 17405 ilmp = &ilm->ilm_next; 17406 continue; 17407 } 17408 17409 if (V4_PART_OF_V6(ilm->ilm_v6addr) == 17410 htonl(INADDR_ALLHOSTS_GROUP)) { 17411 /* 17412 * We joined this in ipif_multicast_up 17413 * and we never did an ipif_multicast_down 17414 * for IPv4. If nobody else from the userland 17415 * has reference, we free the ilm, and later 17416 * when this ipif comes up on the new ill, 17417 * we will join this again. 17418 */ 17419 if (--ilm->ilm_refcnt == 0) 17420 goto delete_ilm; 17421 17422 new_ilm = ilm_lookup_ipif(ipif, 17423 V4_PART_OF_V6(ilm->ilm_v6addr)); 17424 if (new_ilm != NULL) { 17425 new_ilm->ilm_refcnt += ilm->ilm_refcnt; 17426 /* 17427 * We still need to deal with the from_ill. 17428 */ 17429 new_ilm->ilm_is_new = B_TRUE; 17430 new_ilm->ilm_fmode = MODE_IS_EXCLUDE; 17431 CLEAR_SLIST(new_ilm->ilm_filter); 17432 goto delete_ilm; 17433 } 17434 /* 17435 * If we could not find one e.g. ipif is 17436 * still down on to_ill, we add this ilm 17437 * on ill_new to preserve the reference 17438 * count. 17439 */ 17440 } 17441 /* 17442 * When ipifs move, ilms always move with it 17443 * to the NEW ill. Thus we should never be 17444 * able to find ilm till we really move it here. 17445 */ 17446 ASSERT(ilm_lookup_ipif(ipif, 17447 V4_PART_OF_V6(ilm->ilm_v6addr)) == NULL); 17448 17449 if (from_ill->ill_ilm_walker_cnt != 0) { 17450 new_ilm = (ilm_t *)mi_zalloc(sizeof (ilm_t)); 17451 if (new_ilm == NULL) { 17452 char buf[INET6_ADDRSTRLEN]; 17453 ip0dbg(("ilm_move_v4: FAILBACK of IPv4" 17454 " multicast address %s : " 17455 "from %s to" 17456 " %s failed : ENOMEM \n", 17457 inet_ntop(AF_INET, 17458 &ilm->ilm_v6addr, buf, 17459 sizeof (buf)), 17460 from_ill->ill_name, 17461 to_ill->ill_name)); 17462 17463 ilmp = &ilm->ilm_next; 17464 continue; 17465 } 17466 *new_ilm = *ilm; 17467 /* We don't want new_ilm linked to ilm's filter list */ 17468 new_ilm->ilm_filter = NULL; 17469 } else { 17470 /* Remove from the list */ 17471 *ilmp = ilm->ilm_next; 17472 new_ilm = ilm; 17473 } 17474 17475 /* 17476 * If we have never joined this group on the to_ill 17477 * make sure we tell the driver. 17478 */ 17479 if (ilm_lookup_ill_v6(to_ill, &new_ilm->ilm_v6addr, 17480 ALL_ZONES) == NULL) 17481 new_ilm->ilm_notify_driver = B_TRUE; 17482 17483 /* Add to the to_ill's list */ 17484 new_ilm->ilm_next = to_ill->ill_ilm; 17485 to_ill->ill_ilm = new_ilm; 17486 new_ilm->ilm_is_new = B_TRUE; 17487 17488 /* 17489 * Revert multicast filter state to (EXCLUDE, NULL) 17490 */ 17491 new_ilm->ilm_fmode = MODE_IS_EXCLUDE; 17492 CLEAR_SLIST(new_ilm->ilm_filter); 17493 17494 /* 17495 * Delete only if we have allocated a new ilm. 17496 */ 17497 if (new_ilm != ilm) { 17498 delete_ilm: 17499 if (from_ill->ill_ilm_walker_cnt == 0) { 17500 /* Remove from the list */ 17501 *ilmp = ilm->ilm_next; 17502 ilm->ilm_next = NULL; 17503 FREE_SLIST(ilm->ilm_filter); 17504 FREE_SLIST(ilm->ilm_pendsrcs); 17505 FREE_SLIST(ilm->ilm_rtx.rtx_allow); 17506 FREE_SLIST(ilm->ilm_rtx.rtx_block); 17507 mi_free((char *)ilm); 17508 } else { 17509 ilm->ilm_flags |= ILM_DELETED; 17510 from_ill->ill_ilm_cleanup_reqd = 1; 17511 ilmp = &ilm->ilm_next; 17512 } 17513 } 17514 } 17515 } 17516 17517 static uint_t 17518 ipif_get_id(ill_t *ill, uint_t id) 17519 { 17520 uint_t unit; 17521 ipif_t *tipif; 17522 boolean_t found = B_FALSE; 17523 ip_stack_t *ipst = ill->ill_ipst; 17524 17525 /* 17526 * During failback, we want to go back to the same id 17527 * instead of the smallest id so that the original 17528 * configuration is maintained. id is non-zero in that 17529 * case. 17530 */ 17531 if (id != 0) { 17532 /* 17533 * While failing back, if we still have an ipif with 17534 * MAX_ADDRS_PER_IF, it means this will be replaced 17535 * as soon as we return from this function. It was 17536 * to set to MAX_ADDRS_PER_IF by the caller so that 17537 * we can choose the smallest id. Thus we return zero 17538 * in that case ignoring the hint. 17539 */ 17540 if (ill->ill_ipif->ipif_id == MAX_ADDRS_PER_IF) 17541 return (0); 17542 for (tipif = ill->ill_ipif; tipif != NULL; 17543 tipif = tipif->ipif_next) { 17544 if (tipif->ipif_id == id) { 17545 found = B_TRUE; 17546 break; 17547 } 17548 } 17549 /* 17550 * If somebody already plumbed another logical 17551 * with the same id, we won't be able to find it. 17552 */ 17553 if (!found) 17554 return (id); 17555 } 17556 for (unit = 0; unit <= ipst->ips_ip_addrs_per_if; unit++) { 17557 found = B_FALSE; 17558 for (tipif = ill->ill_ipif; tipif != NULL; 17559 tipif = tipif->ipif_next) { 17560 if (tipif->ipif_id == unit) { 17561 found = B_TRUE; 17562 break; 17563 } 17564 } 17565 if (!found) 17566 break; 17567 } 17568 return (unit); 17569 } 17570 17571 /* ARGSUSED */ 17572 static int 17573 ipif_move(ipif_t *ipif, ill_t *to_ill, queue_t *q, mblk_t *mp, 17574 ipif_t **rep_ipif_ptr) 17575 { 17576 ill_t *from_ill; 17577 ipif_t *rep_ipif; 17578 uint_t unit; 17579 int err = 0; 17580 ipif_t *to_ipif; 17581 struct iocblk *iocp; 17582 boolean_t failback_cmd; 17583 boolean_t remove_ipif; 17584 int rc; 17585 ip_stack_t *ipst; 17586 17587 ASSERT(IAM_WRITER_ILL(to_ill)); 17588 ASSERT(IAM_WRITER_IPIF(ipif)); 17589 17590 iocp = (struct iocblk *)mp->b_rptr; 17591 failback_cmd = (iocp->ioc_cmd == SIOCLIFFAILBACK); 17592 remove_ipif = B_FALSE; 17593 17594 from_ill = ipif->ipif_ill; 17595 ipst = from_ill->ill_ipst; 17596 17597 ASSERT(MUTEX_HELD(&to_ill->ill_lock)); 17598 ASSERT(MUTEX_HELD(&from_ill->ill_lock)); 17599 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 17600 17601 /* 17602 * Don't move LINK LOCAL addresses as they are tied to 17603 * physical interface. 17604 */ 17605 if (from_ill->ill_isv6 && 17606 IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6lcl_addr)) { 17607 ipif->ipif_was_up = B_FALSE; 17608 IPIF_UNMARK_MOVING(ipif); 17609 return (0); 17610 } 17611 17612 /* 17613 * We set the ipif_id to maximum so that the search for 17614 * ipif_id will pick the lowest number i.e 0 in the 17615 * following 2 cases : 17616 * 17617 * 1) We have a replacement ipif at the head of to_ill. 17618 * We can't remove it yet as we can exceed ip_addrs_per_if 17619 * on to_ill and hence the MOVE might fail. We want to 17620 * remove it only if we could move the ipif. Thus, by 17621 * setting it to the MAX value, we make the search in 17622 * ipif_get_id return the zeroth id. 17623 * 17624 * 2) When DR pulls out the NIC and re-plumbs the interface, 17625 * we might just have a zero address plumbed on the ipif 17626 * with zero id in the case of IPv4. We remove that while 17627 * doing the failback. We want to remove it only if we 17628 * could move the ipif. Thus, by setting it to the MAX 17629 * value, we make the search in ipif_get_id return the 17630 * zeroth id. 17631 * 17632 * Both (1) and (2) are done only when when we are moving 17633 * an ipif (either due to failover/failback) which originally 17634 * belonged to this interface i.e the ipif_orig_ifindex is 17635 * the same as to_ill's ifindex. This is needed so that 17636 * FAILOVER from A -> B ( A failed) followed by FAILOVER 17637 * from B -> A (B is being removed from the group) and 17638 * FAILBACK from A -> B restores the original configuration. 17639 * Without the check for orig_ifindex, the second FAILOVER 17640 * could make the ipif belonging to B replace the A's zeroth 17641 * ipif and the subsequent failback re-creating the replacement 17642 * ipif again. 17643 * 17644 * NOTE : We created the replacement ipif when we did a 17645 * FAILOVER (See below). We could check for FAILBACK and 17646 * then look for replacement ipif to be removed. But we don't 17647 * want to do that because we wan't to allow the possibility 17648 * of a FAILOVER from A -> B (which creates the replacement ipif), 17649 * followed by a *FAILOVER* from B -> A instead of a FAILBACK 17650 * from B -> A. 17651 */ 17652 to_ipif = to_ill->ill_ipif; 17653 if ((to_ill->ill_phyint->phyint_ifindex == 17654 ipif->ipif_orig_ifindex) && 17655 IPIF_REPL_CHECK(to_ipif, failback_cmd)) { 17656 ASSERT(to_ipif->ipif_id == 0); 17657 remove_ipif = B_TRUE; 17658 to_ipif->ipif_id = MAX_ADDRS_PER_IF; 17659 } 17660 /* 17661 * Find the lowest logical unit number on the to_ill. 17662 * If we are failing back, try to get the original id 17663 * rather than the lowest one so that the original 17664 * configuration is maintained. 17665 * 17666 * XXX need a better scheme for this. 17667 */ 17668 if (failback_cmd) { 17669 unit = ipif_get_id(to_ill, ipif->ipif_orig_ipifid); 17670 } else { 17671 unit = ipif_get_id(to_ill, 0); 17672 } 17673 17674 /* Reset back to zero in case we fail below */ 17675 if (to_ipif->ipif_id == MAX_ADDRS_PER_IF) 17676 to_ipif->ipif_id = 0; 17677 17678 if (unit == ipst->ips_ip_addrs_per_if) { 17679 ipif->ipif_was_up = B_FALSE; 17680 IPIF_UNMARK_MOVING(ipif); 17681 return (EINVAL); 17682 } 17683 17684 /* 17685 * ipif is ready to move from "from_ill" to "to_ill". 17686 * 17687 * 1) If we are moving ipif with id zero, create a 17688 * replacement ipif for this ipif on from_ill. If this fails 17689 * fail the MOVE operation. 17690 * 17691 * 2) Remove the replacement ipif on to_ill if any. 17692 * We could remove the replacement ipif when we are moving 17693 * the ipif with id zero. But what if somebody already 17694 * unplumbed it ? Thus we always remove it if it is present. 17695 * We want to do it only if we are sure we are going to 17696 * move the ipif to to_ill which is why there are no 17697 * returns due to error till ipif is linked to to_ill. 17698 * Note that the first ipif that we failback will always 17699 * be zero if it is present. 17700 */ 17701 if (ipif->ipif_id == 0) { 17702 ipaddr_t inaddr_any = INADDR_ANY; 17703 17704 rep_ipif = (ipif_t *)mi_alloc(sizeof (ipif_t), BPRI_MED); 17705 if (rep_ipif == NULL) { 17706 ipif->ipif_was_up = B_FALSE; 17707 IPIF_UNMARK_MOVING(ipif); 17708 return (ENOMEM); 17709 } 17710 *rep_ipif = ipif_zero; 17711 /* 17712 * Before we put the ipif on the list, store the addresses 17713 * as mapped addresses as some of the ioctls e.g SIOCGIFADDR 17714 * assumes so. This logic is not any different from what 17715 * ipif_allocate does. 17716 */ 17717 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 17718 &rep_ipif->ipif_v6lcl_addr); 17719 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 17720 &rep_ipif->ipif_v6src_addr); 17721 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 17722 &rep_ipif->ipif_v6subnet); 17723 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 17724 &rep_ipif->ipif_v6net_mask); 17725 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 17726 &rep_ipif->ipif_v6brd_addr); 17727 IN6_IPADDR_TO_V4MAPPED(inaddr_any, 17728 &rep_ipif->ipif_v6pp_dst_addr); 17729 /* 17730 * We mark IPIF_NOFAILOVER so that this can never 17731 * move. 17732 */ 17733 rep_ipif->ipif_flags = ipif->ipif_flags | IPIF_NOFAILOVER; 17734 rep_ipif->ipif_flags &= ~IPIF_UP & ~IPIF_DUPLICATE; 17735 rep_ipif->ipif_replace_zero = B_TRUE; 17736 mutex_init(&rep_ipif->ipif_saved_ire_lock, NULL, 17737 MUTEX_DEFAULT, NULL); 17738 rep_ipif->ipif_id = 0; 17739 rep_ipif->ipif_ire_type = ipif->ipif_ire_type; 17740 rep_ipif->ipif_ill = from_ill; 17741 rep_ipif->ipif_orig_ifindex = 17742 from_ill->ill_phyint->phyint_ifindex; 17743 /* Insert at head */ 17744 rep_ipif->ipif_next = from_ill->ill_ipif; 17745 from_ill->ill_ipif = rep_ipif; 17746 /* 17747 * We don't really care to let apps know about 17748 * this interface. 17749 */ 17750 } 17751 17752 if (remove_ipif) { 17753 /* 17754 * We set to a max value above for this case to get 17755 * id zero. ASSERT that we did get one. 17756 */ 17757 ASSERT((to_ipif->ipif_id == 0) && (unit == 0)); 17758 rep_ipif = to_ipif; 17759 to_ill->ill_ipif = rep_ipif->ipif_next; 17760 rep_ipif->ipif_next = NULL; 17761 /* 17762 * If some apps scanned and find this interface, 17763 * it is time to let them know, so that they can 17764 * delete it. 17765 */ 17766 17767 *rep_ipif_ptr = rep_ipif; 17768 } 17769 17770 /* Get it out of the ILL interface list. */ 17771 ipif_remove(ipif, B_FALSE); 17772 17773 /* Assign the new ill */ 17774 ipif->ipif_ill = to_ill; 17775 ipif->ipif_id = unit; 17776 /* id has already been checked */ 17777 rc = ipif_insert(ipif, B_FALSE, B_FALSE); 17778 ASSERT(rc == 0); 17779 /* Let SCTP update its list */ 17780 sctp_move_ipif(ipif, from_ill, to_ill); 17781 /* 17782 * Handle the failover and failback of ipif_t between 17783 * ill_t that have differing maximum mtu values. 17784 */ 17785 if (ipif->ipif_mtu > to_ill->ill_max_mtu) { 17786 if (ipif->ipif_saved_mtu == 0) { 17787 /* 17788 * As this ipif_t is moving to an ill_t 17789 * that has a lower ill_max_mtu, its 17790 * ipif_mtu needs to be saved so it can 17791 * be restored during failback or during 17792 * failover to an ill_t which has a 17793 * higher ill_max_mtu. 17794 */ 17795 ipif->ipif_saved_mtu = ipif->ipif_mtu; 17796 ipif->ipif_mtu = to_ill->ill_max_mtu; 17797 } else { 17798 /* 17799 * The ipif_t is, once again, moving to 17800 * an ill_t that has a lower maximum mtu 17801 * value. 17802 */ 17803 ipif->ipif_mtu = to_ill->ill_max_mtu; 17804 } 17805 } else if (ipif->ipif_mtu < to_ill->ill_max_mtu && 17806 ipif->ipif_saved_mtu != 0) { 17807 /* 17808 * The mtu of this ipif_t had to be reduced 17809 * during an earlier failover; this is an 17810 * opportunity for it to be increased (either as 17811 * part of another failover or a failback). 17812 */ 17813 if (ipif->ipif_saved_mtu <= to_ill->ill_max_mtu) { 17814 ipif->ipif_mtu = ipif->ipif_saved_mtu; 17815 ipif->ipif_saved_mtu = 0; 17816 } else { 17817 ipif->ipif_mtu = to_ill->ill_max_mtu; 17818 } 17819 } 17820 17821 /* 17822 * We preserve all the other fields of the ipif including 17823 * ipif_saved_ire_mp. The routes that are saved here will 17824 * be recreated on the new interface and back on the old 17825 * interface when we move back. 17826 */ 17827 ASSERT(ipif->ipif_arp_del_mp == NULL); 17828 17829 return (err); 17830 } 17831 17832 static int 17833 ipif_move_all(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp, 17834 int ifindex, ipif_t **rep_ipif_ptr) 17835 { 17836 ipif_t *mipif; 17837 ipif_t *ipif_next; 17838 int err; 17839 17840 /* 17841 * We don't really try to MOVE back things if some of the 17842 * operations fail. The daemon will take care of moving again 17843 * later on. 17844 */ 17845 for (mipif = from_ill->ill_ipif; mipif != NULL; mipif = ipif_next) { 17846 ipif_next = mipif->ipif_next; 17847 if (!(mipif->ipif_flags & IPIF_NOFAILOVER) && 17848 (ifindex == 0 || ifindex == mipif->ipif_orig_ifindex)) { 17849 17850 err = ipif_move(mipif, to_ill, q, mp, rep_ipif_ptr); 17851 17852 /* 17853 * When the MOVE fails, it is the job of the 17854 * application to take care of this properly 17855 * i.e try again if it is ENOMEM. 17856 */ 17857 if (mipif->ipif_ill != from_ill) { 17858 /* 17859 * ipif has moved. 17860 * 17861 * Move the multicast memberships associated 17862 * with this ipif to the new ill. For IPv6, we 17863 * do it once after all the ipifs are moved 17864 * (in ill_move) as they are not associated 17865 * with ipifs. 17866 * 17867 * We need to move the ilms as the ipif has 17868 * already been moved to a new ill even 17869 * in the case of errors. Neither 17870 * ilm_free(ipif) will find the ilm 17871 * when somebody unplumbs this ipif nor 17872 * ilm_delete(ilm) will be able to find the 17873 * ilm, if we don't move now. 17874 */ 17875 if (!from_ill->ill_isv6) 17876 ilm_move_v4(from_ill, to_ill, mipif); 17877 } 17878 17879 if (err != 0) 17880 return (err); 17881 } 17882 } 17883 return (0); 17884 } 17885 17886 static int 17887 ill_move(ill_t *from_ill, ill_t *to_ill, queue_t *q, mblk_t *mp) 17888 { 17889 int ifindex; 17890 int err; 17891 struct iocblk *iocp; 17892 ipif_t *ipif; 17893 ipif_t *rep_ipif_ptr = NULL; 17894 ipif_t *from_ipif = NULL; 17895 boolean_t check_rep_if = B_FALSE; 17896 ip_stack_t *ipst = from_ill->ill_ipst; 17897 17898 iocp = (struct iocblk *)mp->b_rptr; 17899 if (iocp->ioc_cmd == SIOCLIFFAILOVER) { 17900 /* 17901 * Move everything pointing at from_ill to to_ill. 17902 * We acheive this by passing in 0 as ifindex. 17903 */ 17904 ifindex = 0; 17905 } else { 17906 /* 17907 * Move everything pointing at from_ill whose original 17908 * ifindex of connp, ipif, ilm points at to_ill->ill_index. 17909 * We acheive this by passing in ifindex rather than 0. 17910 * Multicast vifs, ilgs move implicitly because ipifs move. 17911 */ 17912 ASSERT(iocp->ioc_cmd == SIOCLIFFAILBACK); 17913 ifindex = to_ill->ill_phyint->phyint_ifindex; 17914 } 17915 17916 /* 17917 * Determine if there is at least one ipif that would move from 17918 * 'from_ill' to 'to_ill'. If so, it is possible that the replacement 17919 * ipif (if it exists) on the to_ill would be consumed as a result of 17920 * the move, in which case we need to quiesce the replacement ipif also. 17921 */ 17922 for (from_ipif = from_ill->ill_ipif; from_ipif != NULL; 17923 from_ipif = from_ipif->ipif_next) { 17924 if (((ifindex == 0) || 17925 (ifindex == from_ipif->ipif_orig_ifindex)) && 17926 !(from_ipif->ipif_flags & IPIF_NOFAILOVER)) { 17927 check_rep_if = B_TRUE; 17928 break; 17929 } 17930 } 17931 17932 17933 ill_down_ipifs(from_ill, mp, ifindex, B_TRUE); 17934 17935 GRAB_ILL_LOCKS(from_ill, to_ill); 17936 if ((ipif = ill_quiescent_to_move(from_ill)) != NULL) { 17937 (void) ipsq_pending_mp_add(NULL, ipif, q, 17938 mp, ILL_MOVE_OK); 17939 RELEASE_ILL_LOCKS(from_ill, to_ill); 17940 return (EINPROGRESS); 17941 } 17942 17943 /* Check if the replacement ipif is quiescent to delete */ 17944 if (check_rep_if && IPIF_REPL_CHECK(to_ill->ill_ipif, 17945 (iocp->ioc_cmd == SIOCLIFFAILBACK))) { 17946 to_ill->ill_ipif->ipif_state_flags |= 17947 IPIF_MOVING | IPIF_CHANGING; 17948 if ((ipif = ill_quiescent_to_move(to_ill)) != NULL) { 17949 (void) ipsq_pending_mp_add(NULL, ipif, q, 17950 mp, ILL_MOVE_OK); 17951 RELEASE_ILL_LOCKS(from_ill, to_ill); 17952 return (EINPROGRESS); 17953 } 17954 } 17955 RELEASE_ILL_LOCKS(from_ill, to_ill); 17956 17957 ASSERT(!MUTEX_HELD(&to_ill->ill_lock)); 17958 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 17959 GRAB_ILL_LOCKS(from_ill, to_ill); 17960 err = ipif_move_all(from_ill, to_ill, q, mp, ifindex, &rep_ipif_ptr); 17961 17962 /* ilm_move is done inside ipif_move for IPv4 */ 17963 if (err == 0 && from_ill->ill_isv6) 17964 ilm_move_v6(from_ill, to_ill, ifindex); 17965 17966 RELEASE_ILL_LOCKS(from_ill, to_ill); 17967 rw_exit(&ipst->ips_ill_g_lock); 17968 17969 /* 17970 * send rts messages and multicast messages. 17971 */ 17972 if (rep_ipif_ptr != NULL) { 17973 if (rep_ipif_ptr->ipif_recovery_id != 0) { 17974 (void) untimeout(rep_ipif_ptr->ipif_recovery_id); 17975 rep_ipif_ptr->ipif_recovery_id = 0; 17976 } 17977 ip_rts_ifmsg(rep_ipif_ptr); 17978 ip_rts_newaddrmsg(RTM_DELETE, 0, rep_ipif_ptr); 17979 IPIF_TRACE_CLEANUP(rep_ipif_ptr); 17980 mi_free(rep_ipif_ptr); 17981 } 17982 17983 conn_move_ill(from_ill, to_ill, ifindex); 17984 17985 return (err); 17986 } 17987 17988 /* 17989 * Used to extract arguments for FAILOVER/FAILBACK ioctls. 17990 * Also checks for the validity of the arguments. 17991 * Note: We are already exclusive inside the from group. 17992 * It is upto the caller to release refcnt on the to_ill's. 17993 */ 17994 static int 17995 ip_extract_move_args(queue_t *q, mblk_t *mp, ill_t **ill_from_v4, 17996 ill_t **ill_from_v6, ill_t **ill_to_v4, ill_t **ill_to_v6) 17997 { 17998 int dst_index; 17999 ipif_t *ipif_v4, *ipif_v6; 18000 struct lifreq *lifr; 18001 mblk_t *mp1; 18002 boolean_t exists; 18003 sin_t *sin; 18004 int err = 0; 18005 ip_stack_t *ipst; 18006 18007 if (CONN_Q(q)) 18008 ipst = CONNQ_TO_IPST(q); 18009 else 18010 ipst = ILLQ_TO_IPST(q); 18011 18012 18013 if ((mp1 = mp->b_cont) == NULL) 18014 return (EPROTO); 18015 18016 if ((mp1 = mp1->b_cont) == NULL) 18017 return (EPROTO); 18018 18019 lifr = (struct lifreq *)mp1->b_rptr; 18020 sin = (sin_t *)&lifr->lifr_addr; 18021 18022 /* 18023 * We operate on both IPv4 and IPv6. Thus, we don't allow IPv4/IPv6 18024 * specific operations. 18025 */ 18026 if (sin->sin_family != AF_UNSPEC) 18027 return (EINVAL); 18028 18029 /* 18030 * Get ipif with id 0. We are writer on the from ill. So we can pass 18031 * NULLs for the last 4 args and we know the lookup won't fail 18032 * with EINPROGRESS. 18033 */ 18034 ipif_v4 = ipif_lookup_on_name(lifr->lifr_name, 18035 mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_FALSE, 18036 ALL_ZONES, NULL, NULL, NULL, NULL, ipst); 18037 ipif_v6 = ipif_lookup_on_name(lifr->lifr_name, 18038 mi_strlen(lifr->lifr_name), B_FALSE, &exists, B_TRUE, 18039 ALL_ZONES, NULL, NULL, NULL, NULL, ipst); 18040 18041 if (ipif_v4 == NULL && ipif_v6 == NULL) 18042 return (ENXIO); 18043 18044 if (ipif_v4 != NULL) { 18045 ASSERT(ipif_v4->ipif_refcnt != 0); 18046 if (ipif_v4->ipif_id != 0) { 18047 err = EINVAL; 18048 goto done; 18049 } 18050 18051 ASSERT(IAM_WRITER_IPIF(ipif_v4)); 18052 *ill_from_v4 = ipif_v4->ipif_ill; 18053 } 18054 18055 if (ipif_v6 != NULL) { 18056 ASSERT(ipif_v6->ipif_refcnt != 0); 18057 if (ipif_v6->ipif_id != 0) { 18058 err = EINVAL; 18059 goto done; 18060 } 18061 18062 ASSERT(IAM_WRITER_IPIF(ipif_v6)); 18063 *ill_from_v6 = ipif_v6->ipif_ill; 18064 } 18065 18066 err = 0; 18067 dst_index = lifr->lifr_movetoindex; 18068 *ill_to_v4 = ill_lookup_on_ifindex(dst_index, B_FALSE, 18069 q, mp, ip_process_ioctl, &err, ipst); 18070 if (err != 0) { 18071 /* 18072 * There could be only v6. 18073 */ 18074 if (err != ENXIO) 18075 goto done; 18076 err = 0; 18077 } 18078 18079 *ill_to_v6 = ill_lookup_on_ifindex(dst_index, B_TRUE, 18080 q, mp, ip_process_ioctl, &err, ipst); 18081 if (err != 0) { 18082 if (err != ENXIO) 18083 goto done; 18084 if (*ill_to_v4 == NULL) { 18085 err = ENXIO; 18086 goto done; 18087 } 18088 err = 0; 18089 } 18090 18091 /* 18092 * If we have something to MOVE i.e "from" not NULL, 18093 * "to" should be non-NULL. 18094 */ 18095 if ((*ill_from_v4 != NULL && *ill_to_v4 == NULL) || 18096 (*ill_from_v6 != NULL && *ill_to_v6 == NULL)) { 18097 err = EINVAL; 18098 } 18099 18100 done: 18101 if (ipif_v4 != NULL) 18102 ipif_refrele(ipif_v4); 18103 if (ipif_v6 != NULL) 18104 ipif_refrele(ipif_v6); 18105 return (err); 18106 } 18107 18108 /* 18109 * FAILOVER and FAILBACK are modelled as MOVE operations. 18110 * 18111 * We don't check whether the MOVE is within the same group or 18112 * not, because this ioctl can be used as a generic mechanism 18113 * to failover from interface A to B, though things will function 18114 * only if they are really part of the same group. Moreover, 18115 * all ipifs may be down and hence temporarily out of the group. 18116 * 18117 * ipif's that need to be moved are first brought down; V4 ipifs are brought 18118 * down first and then V6. For each we wait for the ipif's to become quiescent. 18119 * Bringing down the ipifs ensures that all ires pointing to these ipifs's 18120 * have been deleted and there are no active references. Once quiescent the 18121 * ipif's are moved and brought up on the new ill. 18122 * 18123 * Normally the source ill and destination ill belong to the same IPMP group 18124 * and hence the same ipsq_t. In the event they don't belong to the same 18125 * same group the two ipsq's are first merged into one ipsq - that of the 18126 * to_ill. The multicast memberships on the source and destination ill cannot 18127 * change during the move operation since multicast joins/leaves also have to 18128 * execute on the same ipsq and are hence serialized. 18129 */ 18130 /* ARGSUSED */ 18131 int 18132 ip_sioctl_move(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 18133 ip_ioctl_cmd_t *ipip, void *ifreq) 18134 { 18135 ill_t *ill_to_v4 = NULL; 18136 ill_t *ill_to_v6 = NULL; 18137 ill_t *ill_from_v4 = NULL; 18138 ill_t *ill_from_v6 = NULL; 18139 int err = 0; 18140 18141 /* 18142 * setup from and to ill's, we can get EINPROGRESS only for 18143 * to_ill's. 18144 */ 18145 err = ip_extract_move_args(q, mp, &ill_from_v4, &ill_from_v6, 18146 &ill_to_v4, &ill_to_v6); 18147 18148 if (err != 0) { 18149 ip0dbg(("ip_sioctl_move: extract args failed\n")); 18150 goto done; 18151 } 18152 18153 /* 18154 * nothing to do. 18155 */ 18156 if ((ill_from_v4 != NULL) && (ill_from_v4 == ill_to_v4)) { 18157 goto done; 18158 } 18159 18160 /* 18161 * nothing to do. 18162 */ 18163 if ((ill_from_v6 != NULL) && (ill_from_v6 == ill_to_v6)) { 18164 goto done; 18165 } 18166 18167 /* 18168 * Mark the ill as changing. 18169 * ILL_CHANGING flag is cleared when the ipif's are brought up 18170 * in ill_up_ipifs in case of error they are cleared below. 18171 */ 18172 18173 GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6); 18174 if (ill_from_v4 != NULL) 18175 ill_from_v4->ill_state_flags |= ILL_CHANGING; 18176 if (ill_from_v6 != NULL) 18177 ill_from_v6->ill_state_flags |= ILL_CHANGING; 18178 RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6); 18179 18180 /* 18181 * Make sure that both src and dst are 18182 * in the same syncq group. If not make it happen. 18183 * We are not holding any locks because we are the writer 18184 * on the from_ipsq and we will hold locks in ill_merge_groups 18185 * to protect to_ipsq against changing. 18186 */ 18187 if (ill_from_v4 != NULL) { 18188 if (ill_from_v4->ill_phyint->phyint_ipsq != 18189 ill_to_v4->ill_phyint->phyint_ipsq) { 18190 err = ill_merge_groups(ill_from_v4, ill_to_v4, 18191 NULL, mp, q); 18192 goto err_ret; 18193 18194 } 18195 ASSERT(!MUTEX_HELD(&ill_to_v4->ill_lock)); 18196 } else { 18197 18198 if (ill_from_v6->ill_phyint->phyint_ipsq != 18199 ill_to_v6->ill_phyint->phyint_ipsq) { 18200 err = ill_merge_groups(ill_from_v6, ill_to_v6, 18201 NULL, mp, q); 18202 goto err_ret; 18203 18204 } 18205 ASSERT(!MUTEX_HELD(&ill_to_v6->ill_lock)); 18206 } 18207 18208 /* 18209 * Now that the ipsq's have been merged and we are the writer 18210 * lets mark to_ill as changing as well. 18211 */ 18212 18213 GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6); 18214 if (ill_to_v4 != NULL) 18215 ill_to_v4->ill_state_flags |= ILL_CHANGING; 18216 if (ill_to_v6 != NULL) 18217 ill_to_v6->ill_state_flags |= ILL_CHANGING; 18218 RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6); 18219 18220 /* 18221 * Its ok for us to proceed with the move even if 18222 * ill_pending_mp is non null on one of the from ill's as the reply 18223 * should not be looking at the ipif, it should only care about the 18224 * ill itself. 18225 */ 18226 18227 /* 18228 * lets move ipv4 first. 18229 */ 18230 if (ill_from_v4 != NULL) { 18231 ASSERT(IAM_WRITER_ILL(ill_to_v4)); 18232 ill_from_v4->ill_move_in_progress = B_TRUE; 18233 ill_to_v4->ill_move_in_progress = B_TRUE; 18234 ill_to_v4->ill_move_peer = ill_from_v4; 18235 ill_from_v4->ill_move_peer = ill_to_v4; 18236 err = ill_move(ill_from_v4, ill_to_v4, q, mp); 18237 } 18238 18239 /* 18240 * Now lets move ipv6. 18241 */ 18242 if (err == 0 && ill_from_v6 != NULL) { 18243 ASSERT(IAM_WRITER_ILL(ill_to_v6)); 18244 ill_from_v6->ill_move_in_progress = B_TRUE; 18245 ill_to_v6->ill_move_in_progress = B_TRUE; 18246 ill_to_v6->ill_move_peer = ill_from_v6; 18247 ill_from_v6->ill_move_peer = ill_to_v6; 18248 err = ill_move(ill_from_v6, ill_to_v6, q, mp); 18249 } 18250 18251 err_ret: 18252 /* 18253 * EINPROGRESS means we are waiting for the ipif's that need to be 18254 * moved to become quiescent. 18255 */ 18256 if (err == EINPROGRESS) { 18257 goto done; 18258 } 18259 18260 /* 18261 * if err is set ill_up_ipifs will not be called 18262 * lets clear the flags. 18263 */ 18264 18265 GRAB_ILL_LOCKS(ill_to_v4, ill_to_v6); 18266 GRAB_ILL_LOCKS(ill_from_v4, ill_from_v6); 18267 /* 18268 * Some of the clearing may be redundant. But it is simple 18269 * not making any extra checks. 18270 */ 18271 if (ill_from_v6 != NULL) { 18272 ill_from_v6->ill_move_in_progress = B_FALSE; 18273 ill_from_v6->ill_move_peer = NULL; 18274 ill_from_v6->ill_state_flags &= ~ILL_CHANGING; 18275 } 18276 if (ill_from_v4 != NULL) { 18277 ill_from_v4->ill_move_in_progress = B_FALSE; 18278 ill_from_v4->ill_move_peer = NULL; 18279 ill_from_v4->ill_state_flags &= ~ILL_CHANGING; 18280 } 18281 if (ill_to_v6 != NULL) { 18282 ill_to_v6->ill_move_in_progress = B_FALSE; 18283 ill_to_v6->ill_move_peer = NULL; 18284 ill_to_v6->ill_state_flags &= ~ILL_CHANGING; 18285 } 18286 if (ill_to_v4 != NULL) { 18287 ill_to_v4->ill_move_in_progress = B_FALSE; 18288 ill_to_v4->ill_move_peer = NULL; 18289 ill_to_v4->ill_state_flags &= ~ILL_CHANGING; 18290 } 18291 18292 /* 18293 * Check for setting INACTIVE, if STANDBY is set and FAILED is not set. 18294 * Do this always to maintain proper state i.e even in case of errors. 18295 * As phyint_inactive looks at both v4 and v6 interfaces, 18296 * we need not call on both v4 and v6 interfaces. 18297 */ 18298 if (ill_from_v4 != NULL) { 18299 if ((ill_from_v4->ill_phyint->phyint_flags & 18300 (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) { 18301 phyint_inactive(ill_from_v4->ill_phyint); 18302 } 18303 } else if (ill_from_v6 != NULL) { 18304 if ((ill_from_v6->ill_phyint->phyint_flags & 18305 (PHYI_STANDBY | PHYI_FAILED)) == PHYI_STANDBY) { 18306 phyint_inactive(ill_from_v6->ill_phyint); 18307 } 18308 } 18309 18310 if (ill_to_v4 != NULL) { 18311 if (ill_to_v4->ill_phyint->phyint_flags & PHYI_INACTIVE) { 18312 ill_to_v4->ill_phyint->phyint_flags &= ~PHYI_INACTIVE; 18313 } 18314 } else if (ill_to_v6 != NULL) { 18315 if (ill_to_v6->ill_phyint->phyint_flags & PHYI_INACTIVE) { 18316 ill_to_v6->ill_phyint->phyint_flags &= ~PHYI_INACTIVE; 18317 } 18318 } 18319 18320 RELEASE_ILL_LOCKS(ill_to_v4, ill_to_v6); 18321 RELEASE_ILL_LOCKS(ill_from_v4, ill_from_v6); 18322 18323 no_err: 18324 /* 18325 * lets bring the interfaces up on the to_ill. 18326 */ 18327 if (err == 0) { 18328 err = ill_up_ipifs(ill_to_v4 == NULL ? ill_to_v6:ill_to_v4, 18329 q, mp); 18330 } 18331 18332 if (err == 0) { 18333 if (ill_from_v4 != NULL && ill_to_v4 != NULL) 18334 ilm_send_multicast_reqs(ill_from_v4, ill_to_v4); 18335 18336 if (ill_from_v6 != NULL && ill_to_v6 != NULL) 18337 ilm_send_multicast_reqs(ill_from_v6, ill_to_v6); 18338 } 18339 done: 18340 18341 if (ill_to_v4 != NULL) { 18342 ill_refrele(ill_to_v4); 18343 } 18344 if (ill_to_v6 != NULL) { 18345 ill_refrele(ill_to_v6); 18346 } 18347 18348 return (err); 18349 } 18350 18351 static void 18352 ill_dl_down(ill_t *ill) 18353 { 18354 /* 18355 * The ill is down; unbind but stay attached since we're still 18356 * associated with a PPA. If we have negotiated DLPI capabilites 18357 * with the data link service provider (IDS_OK) then reset them. 18358 * The interval between unbinding and rebinding is potentially 18359 * unbounded hence we cannot assume things will be the same. 18360 * The DLPI capabilities will be probed again when the data link 18361 * is brought up. 18362 */ 18363 mblk_t *mp = ill->ill_unbind_mp; 18364 hook_nic_event_t *info; 18365 18366 ip1dbg(("ill_dl_down(%s)\n", ill->ill_name)); 18367 18368 ill->ill_unbind_mp = NULL; 18369 if (mp != NULL) { 18370 ip1dbg(("ill_dl_down: %s (%u) for %s\n", 18371 dlpi_prim_str(*(int *)mp->b_rptr), *(int *)mp->b_rptr, 18372 ill->ill_name)); 18373 mutex_enter(&ill->ill_lock); 18374 ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS; 18375 mutex_exit(&ill->ill_lock); 18376 if (ill->ill_dlpi_capab_state == IDS_OK) 18377 ill_capability_reset(ill); 18378 ill_dlpi_send(ill, mp); 18379 } 18380 18381 /* 18382 * Toss all of our multicast memberships. We could keep them, but 18383 * then we'd have to do bookkeeping of any joins and leaves performed 18384 * by the application while the the interface is down (we can't just 18385 * issue them because arp cannot currently process AR_ENTRY_SQUERY's 18386 * on a downed interface). 18387 */ 18388 ill_leave_multicast(ill); 18389 18390 mutex_enter(&ill->ill_lock); 18391 18392 ill->ill_dl_up = 0; 18393 18394 if ((info = ill->ill_nic_event_info) != NULL) { 18395 ip2dbg(("ill_dl_down:unexpected nic event %d attached for %s\n", 18396 info->hne_event, ill->ill_name)); 18397 if (info->hne_data != NULL) 18398 kmem_free(info->hne_data, info->hne_datalen); 18399 kmem_free(info, sizeof (hook_nic_event_t)); 18400 } 18401 18402 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 18403 if (info != NULL) { 18404 ip_stack_t *ipst = ill->ill_ipst; 18405 18406 info->hne_nic = ill->ill_phyint->phyint_hook_ifindex; 18407 info->hne_lif = 0; 18408 info->hne_event = NE_DOWN; 18409 info->hne_data = NULL; 18410 info->hne_datalen = 0; 18411 info->hne_family = ill->ill_isv6 ? 18412 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 18413 } else 18414 ip2dbg(("ill_dl_down: could not attach DOWN nic event " 18415 "information for %s (ENOMEM)\n", ill->ill_name)); 18416 18417 ill->ill_nic_event_info = info; 18418 18419 mutex_exit(&ill->ill_lock); 18420 } 18421 18422 static void 18423 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp) 18424 { 18425 union DL_primitives *dlp; 18426 t_uscalar_t prim; 18427 18428 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 18429 18430 dlp = (union DL_primitives *)mp->b_rptr; 18431 prim = dlp->dl_primitive; 18432 18433 ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n", 18434 dlpi_prim_str(prim), prim, ill->ill_name)); 18435 18436 switch (prim) { 18437 case DL_PHYS_ADDR_REQ: 18438 { 18439 dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr; 18440 ill->ill_phys_addr_pend = dlpap->dl_addr_type; 18441 break; 18442 } 18443 case DL_BIND_REQ: 18444 mutex_enter(&ill->ill_lock); 18445 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS; 18446 mutex_exit(&ill->ill_lock); 18447 break; 18448 } 18449 18450 /* 18451 * Except for the ACKs for the M_PCPROTO messages, all other ACKs 18452 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore 18453 * we only wait for the ACK of the DL_UNBIND_REQ. 18454 */ 18455 mutex_enter(&ill->ill_lock); 18456 if (!(ill->ill_state_flags & ILL_CONDEMNED) || 18457 (prim == DL_UNBIND_REQ)) { 18458 ill->ill_dlpi_pending = prim; 18459 } 18460 mutex_exit(&ill->ill_lock); 18461 18462 putnext(ill->ill_wq, mp); 18463 } 18464 18465 /* 18466 * Helper function for ill_dlpi_send(). 18467 */ 18468 /* ARGSUSED */ 18469 static void 18470 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg) 18471 { 18472 ill_dlpi_send((ill_t *)q->q_ptr, mp); 18473 } 18474 18475 /* 18476 * Send a DLPI control message to the driver but make sure there 18477 * is only one outstanding message. Uses ill_dlpi_pending to tell 18478 * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done() 18479 * when an ACK or a NAK is received to process the next queued message. 18480 */ 18481 void 18482 ill_dlpi_send(ill_t *ill, mblk_t *mp) 18483 { 18484 mblk_t **mpp; 18485 18486 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO); 18487 18488 /* 18489 * To ensure that any DLPI requests for current exclusive operation 18490 * are always completely sent before any DLPI messages for other 18491 * operations, require writer access before enqueuing. 18492 */ 18493 if (!IAM_WRITER_ILL(ill)) { 18494 ill_refhold(ill); 18495 /* qwriter_ip() does the ill_refrele() */ 18496 qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer, 18497 NEW_OP, B_TRUE); 18498 return; 18499 } 18500 18501 mutex_enter(&ill->ill_lock); 18502 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) { 18503 /* Must queue message. Tail insertion */ 18504 mpp = &ill->ill_dlpi_deferred; 18505 while (*mpp != NULL) 18506 mpp = &((*mpp)->b_next); 18507 18508 ip1dbg(("ill_dlpi_send: deferring request for %s\n", 18509 ill->ill_name)); 18510 18511 *mpp = mp; 18512 mutex_exit(&ill->ill_lock); 18513 return; 18514 } 18515 mutex_exit(&ill->ill_lock); 18516 ill_dlpi_dispatch(ill, mp); 18517 } 18518 18519 /* 18520 * Send all deferred DLPI messages without waiting for their ACKs. 18521 */ 18522 void 18523 ill_dlpi_send_deferred(ill_t *ill) 18524 { 18525 mblk_t *mp, *nextmp; 18526 18527 /* 18528 * Clear ill_dlpi_pending so that the message is not queued in 18529 * ill_dlpi_send(). 18530 */ 18531 mutex_enter(&ill->ill_lock); 18532 ill->ill_dlpi_pending = DL_PRIM_INVAL; 18533 mp = ill->ill_dlpi_deferred; 18534 ill->ill_dlpi_deferred = NULL; 18535 mutex_exit(&ill->ill_lock); 18536 18537 for (; mp != NULL; mp = nextmp) { 18538 nextmp = mp->b_next; 18539 mp->b_next = NULL; 18540 ill_dlpi_send(ill, mp); 18541 } 18542 } 18543 18544 /* 18545 * Check if the DLPI primitive `prim' is pending; print a warning if not. 18546 */ 18547 boolean_t 18548 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim) 18549 { 18550 t_uscalar_t prim_pending; 18551 18552 mutex_enter(&ill->ill_lock); 18553 prim_pending = ill->ill_dlpi_pending; 18554 mutex_exit(&ill->ill_lock); 18555 18556 /* 18557 * During teardown, ill_dlpi_send_deferred() will send requests 18558 * without waiting; don't bother printing any warnings in that case. 18559 */ 18560 if (!(ill->ill_flags & ILL_CONDEMNED) && prim_pending != prim) { 18561 if (prim_pending == DL_PRIM_INVAL) { 18562 (void) mi_strlog(ill->ill_rq, 1, 18563 SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received " 18564 "unsolicited ack for %s on %s\n", 18565 dlpi_prim_str(prim), ill->ill_name); 18566 } else { 18567 (void) mi_strlog(ill->ill_rq, 1, 18568 SL_CONSOLE|SL_ERROR|SL_TRACE, "ip: received " 18569 "unexpected ack for %s on %s (expecting %s)\n", 18570 dlpi_prim_str(prim), ill->ill_name, 18571 dlpi_prim_str(prim_pending)); 18572 } 18573 } 18574 return (prim_pending == prim); 18575 } 18576 18577 /* 18578 * Called when an DLPI control message has been acked or nacked to 18579 * send down the next queued message (if any). 18580 */ 18581 void 18582 ill_dlpi_done(ill_t *ill, t_uscalar_t prim) 18583 { 18584 mblk_t *mp; 18585 18586 ASSERT(IAM_WRITER_ILL(ill)); 18587 mutex_enter(&ill->ill_lock); 18588 18589 ASSERT(prim != DL_PRIM_INVAL); 18590 ASSERT(ill->ill_dlpi_pending == prim); 18591 18592 ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name, 18593 dlpi_prim_str(ill->ill_dlpi_pending), ill->ill_dlpi_pending)); 18594 18595 if ((mp = ill->ill_dlpi_deferred) == NULL) { 18596 ill->ill_dlpi_pending = DL_PRIM_INVAL; 18597 cv_signal(&ill->ill_cv); 18598 mutex_exit(&ill->ill_lock); 18599 return; 18600 } 18601 18602 ill->ill_dlpi_deferred = mp->b_next; 18603 mp->b_next = NULL; 18604 mutex_exit(&ill->ill_lock); 18605 18606 ill_dlpi_dispatch(ill, mp); 18607 } 18608 18609 void 18610 conn_delete_ire(conn_t *connp, caddr_t arg) 18611 { 18612 ipif_t *ipif = (ipif_t *)arg; 18613 ire_t *ire; 18614 18615 /* 18616 * Look at the cached ires on conns which has pointers to ipifs. 18617 * We just call ire_refrele which clears up the reference 18618 * to ire. Called when a conn closes. Also called from ipif_free 18619 * to cleanup indirect references to the stale ipif via the cached ire. 18620 */ 18621 mutex_enter(&connp->conn_lock); 18622 ire = connp->conn_ire_cache; 18623 if (ire != NULL && (ipif == NULL || ire->ire_ipif == ipif)) { 18624 connp->conn_ire_cache = NULL; 18625 mutex_exit(&connp->conn_lock); 18626 IRE_REFRELE_NOTR(ire); 18627 return; 18628 } 18629 mutex_exit(&connp->conn_lock); 18630 18631 } 18632 18633 /* 18634 * Some operations (illgrp_delete(), ipif_down()) conditionally delete a number 18635 * of IREs. Those IREs may have been previously cached in the conn structure. 18636 * This ipcl_walk() walker function releases all references to such IREs based 18637 * on the condemned flag. 18638 */ 18639 /* ARGSUSED */ 18640 void 18641 conn_cleanup_stale_ire(conn_t *connp, caddr_t arg) 18642 { 18643 ire_t *ire; 18644 18645 mutex_enter(&connp->conn_lock); 18646 ire = connp->conn_ire_cache; 18647 if (ire != NULL && (ire->ire_marks & IRE_MARK_CONDEMNED)) { 18648 connp->conn_ire_cache = NULL; 18649 mutex_exit(&connp->conn_lock); 18650 IRE_REFRELE_NOTR(ire); 18651 return; 18652 } 18653 mutex_exit(&connp->conn_lock); 18654 } 18655 18656 /* 18657 * Take down a specific interface, but don't lose any information about it. 18658 * Also delete interface from its interface group (ifgrp). 18659 * (Always called as writer.) 18660 * This function goes through the down sequence even if the interface is 18661 * already down. There are 2 reasons. 18662 * a. Currently we permit interface routes that depend on down interfaces 18663 * to be added. This behaviour itself is questionable. However it appears 18664 * that both Solaris and 4.3 BSD have exhibited this behaviour for a long 18665 * time. We go thru the cleanup in order to remove these routes. 18666 * b. The bringup of the interface could fail in ill_dl_up i.e. we get 18667 * DL_ERROR_ACK in response to the the DL_BIND request. The interface is 18668 * down, but we need to cleanup i.e. do ill_dl_down and 18669 * ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down. 18670 * 18671 * IP-MT notes: 18672 * 18673 * Model of reference to interfaces. 18674 * 18675 * The following members in ipif_t track references to the ipif. 18676 * int ipif_refcnt; Active reference count 18677 * uint_t ipif_ire_cnt; Number of ire's referencing this ipif 18678 * The following members in ill_t track references to the ill. 18679 * int ill_refcnt; active refcnt 18680 * uint_t ill_ire_cnt; Number of ires referencing ill 18681 * uint_t ill_nce_cnt; Number of nces referencing ill 18682 * 18683 * Reference to an ipif or ill can be obtained in any of the following ways. 18684 * 18685 * Through the lookup functions ipif_lookup_* / ill_lookup_* functions 18686 * Pointers to ipif / ill from other data structures viz ire and conn. 18687 * Implicit reference to the ipif / ill by holding a reference to the ire. 18688 * 18689 * The ipif/ill lookup functions return a reference held ipif / ill. 18690 * ipif_refcnt and ill_refcnt track the reference counts respectively. 18691 * This is a purely dynamic reference count associated with threads holding 18692 * references to the ipif / ill. Pointers from other structures do not 18693 * count towards this reference count. 18694 * 18695 * ipif_ire_cnt/ill_ire_cnt is the number of ire's associated with the 18696 * ipif/ill. This is incremented whenever a new ire is created referencing the 18697 * ipif/ill. This is done atomically inside ire_add_v[46] where the ire is 18698 * actually added to the ire hash table. The count is decremented in 18699 * ire_inactive where the ire is destroyed. 18700 * 18701 * nce's reference ill's thru nce_ill and the count of nce's associated with 18702 * an ill is recorded in ill_nce_cnt. This is incremented atomically in 18703 * ndp_add() where the nce is actually added to the table. Similarly it is 18704 * decremented in ndp_inactive where the nce is destroyed. 18705 * 18706 * Flow of ioctls involving interface down/up 18707 * 18708 * The following is the sequence of an attempt to set some critical flags on an 18709 * up interface. 18710 * ip_sioctl_flags 18711 * ipif_down 18712 * wait for ipif to be quiescent 18713 * ipif_down_tail 18714 * ip_sioctl_flags_tail 18715 * 18716 * All set ioctls that involve down/up sequence would have a skeleton similar 18717 * to the above. All the *tail functions are called after the refcounts have 18718 * dropped to the appropriate values. 18719 * 18720 * The mechanism to quiesce an ipif is as follows. 18721 * 18722 * Mark the ipif as IPIF_CHANGING. No more lookups will be allowed 18723 * on the ipif. Callers either pass a flag requesting wait or the lookup 18724 * functions will return NULL. 18725 * 18726 * Delete all ires referencing this ipif 18727 * 18728 * Any thread attempting to do an ipif_refhold on an ipif that has been 18729 * obtained thru a cached pointer will first make sure that 18730 * the ipif can be refheld using the macro IPIF_CAN_LOOKUP and only then 18731 * increment the refcount. 18732 * 18733 * The above guarantees that the ipif refcount will eventually come down to 18734 * zero and the ipif will quiesce, once all threads that currently hold a 18735 * reference to the ipif refrelease the ipif. The ipif is quiescent after the 18736 * ipif_refcount has dropped to zero and all ire's associated with this ipif 18737 * have also been ire_inactive'd. i.e. when ipif_ire_cnt and ipif_refcnt both 18738 * drop to zero. 18739 * 18740 * Lookups during the IPIF_CHANGING/ILL_CHANGING interval. 18741 * 18742 * Threads trying to lookup an ipif or ill can pass a flag requesting 18743 * wait and restart if the ipif / ill cannot be looked up currently. 18744 * For eg. bind, and route operations (Eg. route add / delete) cannot return 18745 * failure if the ipif is currently undergoing an exclusive operation, and 18746 * hence pass the flag. The mblk is then enqueued in the ipsq and the operation 18747 * is restarted by ipsq_exit() when the currently exclusive ioctl completes. 18748 * The lookup and enqueue is atomic using the ill_lock and ipsq_lock. The 18749 * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't 18750 * change while the ill_lock is held. Before dropping the ill_lock we acquire 18751 * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish 18752 * until we release the ipsq_lock, even though the the ill/ipif state flags 18753 * can change after we drop the ill_lock. 18754 * 18755 * An attempt to send out a packet using an ipif that is currently 18756 * IPIF_CHANGING will fail. No attempt is made in this case to enqueue this 18757 * operation and restart it later when the exclusive condition on the ipif ends. 18758 * This is an example of not passing the wait flag to the lookup functions. For 18759 * example an attempt to refhold and use conn->conn_multicast_ipif and send 18760 * out a multicast packet on that ipif will fail while the ipif is 18761 * IPIF_CHANGING. An attempt to create an IRE_CACHE using an ipif that is 18762 * currently IPIF_CHANGING will also fail. 18763 */ 18764 int 18765 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp) 18766 { 18767 ill_t *ill = ipif->ipif_ill; 18768 phyint_t *phyi; 18769 conn_t *connp; 18770 boolean_t success; 18771 boolean_t ipif_was_up = B_FALSE; 18772 ip_stack_t *ipst = ill->ill_ipst; 18773 18774 ASSERT(IAM_WRITER_IPIF(ipif)); 18775 18776 ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 18777 18778 if (ipif->ipif_flags & IPIF_UP) { 18779 mutex_enter(&ill->ill_lock); 18780 ipif->ipif_flags &= ~IPIF_UP; 18781 ASSERT(ill->ill_ipif_up_count > 0); 18782 --ill->ill_ipif_up_count; 18783 mutex_exit(&ill->ill_lock); 18784 ipif_was_up = B_TRUE; 18785 /* Update status in SCTP's list */ 18786 sctp_update_ipif(ipif, SCTP_IPIF_DOWN); 18787 } 18788 18789 /* 18790 * Blow away v6 memberships we established in ipif_multicast_up(); the 18791 * v4 ones are left alone (as is the ipif_multicast_up flag, so we 18792 * know not to rejoin when the interface is brought back up). 18793 */ 18794 if (ipif->ipif_isv6) 18795 ipif_multicast_down(ipif); 18796 /* 18797 * Remove from the mapping for __sin6_src_id. We insert only 18798 * when the address is not INADDR_ANY. As IPv4 addresses are 18799 * stored as mapped addresses, we need to check for mapped 18800 * INADDR_ANY also. 18801 */ 18802 if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) && 18803 !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) && 18804 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 18805 int err; 18806 18807 err = ip_srcid_remove(&ipif->ipif_v6lcl_addr, 18808 ipif->ipif_zoneid, ipst); 18809 if (err != 0) { 18810 ip0dbg(("ipif_down: srcid_remove %d\n", err)); 18811 } 18812 } 18813 18814 /* 18815 * Before we delete the ill from the group (if any), we need 18816 * to make sure that we delete all the routes dependent on 18817 * this and also any ipifs dependent on this ipif for 18818 * source address. We need to do before we delete from 18819 * the group because 18820 * 18821 * 1) ipif_down_delete_ire de-references ill->ill_group. 18822 * 18823 * 2) ipif_update_other_ipifs needs to walk the whole group 18824 * for re-doing source address selection. Note that 18825 * ipif_select_source[_v6] called from 18826 * ipif_update_other_ipifs[_v6] will not pick this ipif 18827 * because we have already marked down here i.e cleared 18828 * IPIF_UP. 18829 */ 18830 if (ipif->ipif_isv6) { 18831 ire_walk_v6(ipif_down_delete_ire, (char *)ipif, ALL_ZONES, 18832 ipst); 18833 } else { 18834 ire_walk_v4(ipif_down_delete_ire, (char *)ipif, ALL_ZONES, 18835 ipst); 18836 } 18837 18838 /* 18839 * Need to add these also to be saved and restored when the 18840 * ipif is brought down and up 18841 */ 18842 mutex_enter(&ipst->ips_ire_mrtun_lock); 18843 if (ipst->ips_ire_mrtun_count != 0) { 18844 mutex_exit(&ipst->ips_ire_mrtun_lock); 18845 ire_walk_ill_mrtun(0, 0, ipif_down_delete_ire, 18846 (char *)ipif, NULL, ipst); 18847 } else { 18848 mutex_exit(&ipst->ips_ire_mrtun_lock); 18849 } 18850 18851 mutex_enter(&ipst->ips_ire_srcif_table_lock); 18852 if (ipst->ips_ire_srcif_table_count > 0) { 18853 mutex_exit(&ipst->ips_ire_srcif_table_lock); 18854 ire_walk_srcif_table_v4(ipif_down_delete_ire, (char *)ipif, 18855 ipst); 18856 } else { 18857 mutex_exit(&ipst->ips_ire_srcif_table_lock); 18858 } 18859 18860 /* 18861 * Cleaning up the conn_ire_cache or conns must be done only after the 18862 * ires have been deleted above. Otherwise a thread could end up 18863 * caching an ire in a conn after we have finished the cleanup of the 18864 * conn. The caching is done after making sure that the ire is not yet 18865 * condemned. Also documented in the block comment above ip_output 18866 */ 18867 ipcl_walk(conn_cleanup_stale_ire, NULL, ipst); 18868 /* Also, delete the ires cached in SCTP */ 18869 sctp_ire_cache_flush(ipif); 18870 18871 /* Resolve any IPsec/IKE NAT-T instances that depend on this ipif. */ 18872 nattymod_clean_ipif(ipif); 18873 18874 /* 18875 * Update any other ipifs which have used "our" local address as 18876 * a source address. This entails removing and recreating IRE_INTERFACE 18877 * entries for such ipifs. 18878 */ 18879 if (ipif->ipif_isv6) 18880 ipif_update_other_ipifs_v6(ipif, ill->ill_group); 18881 else 18882 ipif_update_other_ipifs(ipif, ill->ill_group); 18883 18884 if (ipif_was_up) { 18885 /* 18886 * Check whether it is last ipif to leave this group. 18887 * If this is the last ipif to leave, we should remove 18888 * this ill from the group as ipif_select_source will not 18889 * be able to find any useful ipifs if this ill is selected 18890 * for load balancing. 18891 * 18892 * For nameless groups, we should call ifgrp_delete if this 18893 * belongs to some group. As this ipif is going down, we may 18894 * need to reconstruct groups. 18895 */ 18896 phyi = ill->ill_phyint; 18897 /* 18898 * If the phyint_groupname_len is 0, it may or may not 18899 * be in the nameless group. If the phyint_groupname_len is 18900 * not 0, then this ill should be part of some group. 18901 * As we always insert this ill in the group if 18902 * phyint_groupname_len is not zero when the first ipif 18903 * comes up (in ipif_up_done), it should be in a group 18904 * when the namelen is not 0. 18905 * 18906 * NOTE : When we delete the ill from the group,it will 18907 * blow away all the IRE_CACHES pointing either at this ipif or 18908 * ill_wq (illgrp_cache_delete does this). Thus, no IRES 18909 * should be pointing at this ill. 18910 */ 18911 ASSERT(phyi->phyint_groupname_len == 0 || 18912 (phyi->phyint_groupname != NULL && ill->ill_group != NULL)); 18913 18914 if (phyi->phyint_groupname_len != 0) { 18915 if (ill->ill_ipif_up_count == 0) 18916 illgrp_delete(ill); 18917 } 18918 18919 /* 18920 * If we have deleted some of the broadcast ires associated 18921 * with this ipif, we need to re-nominate somebody else if 18922 * the ires that we deleted were the nominated ones. 18923 */ 18924 if (ill->ill_group != NULL && !ill->ill_isv6) 18925 ipif_renominate_bcast(ipif); 18926 } 18927 18928 /* 18929 * neighbor-discovery or arp entries for this interface. 18930 */ 18931 ipif_ndp_down(ipif); 18932 18933 /* 18934 * If mp is NULL the caller will wait for the appropriate refcnt. 18935 * Eg. ip_sioctl_removeif -> ipif_free -> ipif_down 18936 * and ill_delete -> ipif_free -> ipif_down 18937 */ 18938 if (mp == NULL) { 18939 ASSERT(q == NULL); 18940 return (0); 18941 } 18942 18943 if (CONN_Q(q)) { 18944 connp = Q_TO_CONN(q); 18945 mutex_enter(&connp->conn_lock); 18946 } else { 18947 connp = NULL; 18948 } 18949 mutex_enter(&ill->ill_lock); 18950 /* 18951 * Are there any ire's pointing to this ipif that are still active ? 18952 * If this is the last ipif going down, are there any ire's pointing 18953 * to this ill that are still active ? 18954 */ 18955 if (ipif_is_quiescent(ipif)) { 18956 mutex_exit(&ill->ill_lock); 18957 if (connp != NULL) 18958 mutex_exit(&connp->conn_lock); 18959 return (0); 18960 } 18961 18962 ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p", 18963 ill->ill_name, (void *)ill)); 18964 /* 18965 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount 18966 * drops down, the operation will be restarted by ipif_ill_refrele_tail 18967 * which in turn is called by the last refrele on the ipif/ill/ire. 18968 */ 18969 success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN); 18970 if (!success) { 18971 /* The conn is closing. So just return */ 18972 ASSERT(connp != NULL); 18973 mutex_exit(&ill->ill_lock); 18974 mutex_exit(&connp->conn_lock); 18975 return (EINTR); 18976 } 18977 18978 mutex_exit(&ill->ill_lock); 18979 if (connp != NULL) 18980 mutex_exit(&connp->conn_lock); 18981 return (EINPROGRESS); 18982 } 18983 18984 void 18985 ipif_down_tail(ipif_t *ipif) 18986 { 18987 ill_t *ill = ipif->ipif_ill; 18988 18989 /* 18990 * Skip any loopback interface (null wq). 18991 * If this is the last logical interface on the ill 18992 * have ill_dl_down tell the driver we are gone (unbind) 18993 * Note that lun 0 can ipif_down even though 18994 * there are other logical units that are up. 18995 * This occurs e.g. when we change a "significant" IFF_ flag. 18996 */ 18997 if (ill->ill_wq != NULL && !ill->ill_logical_down && 18998 ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 && 18999 ill->ill_dl_up) { 19000 ill_dl_down(ill); 19001 } 19002 ill->ill_logical_down = 0; 19003 19004 /* 19005 * Have to be after removing the routes in ipif_down_delete_ire. 19006 */ 19007 if (ipif->ipif_isv6) { 19008 if (ill->ill_flags & ILLF_XRESOLV) 19009 ipif_arp_down(ipif); 19010 } else { 19011 ipif_arp_down(ipif); 19012 } 19013 19014 ip_rts_ifmsg(ipif); 19015 ip_rts_newaddrmsg(RTM_DELETE, 0, ipif); 19016 } 19017 19018 /* 19019 * Bring interface logically down without bringing the physical interface 19020 * down e.g. when the netmask is changed. This avoids long lasting link 19021 * negotiations between an ethernet interface and a certain switches. 19022 */ 19023 static int 19024 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp) 19025 { 19026 /* 19027 * The ill_logical_down flag is a transient flag. It is set here 19028 * and is cleared once the down has completed in ipif_down_tail. 19029 * This flag does not indicate whether the ill stream is in the 19030 * DL_BOUND state with the driver. Instead this flag is used by 19031 * ipif_down_tail to determine whether to DL_UNBIND the stream with 19032 * the driver. The state of the ill stream i.e. whether it is 19033 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag. 19034 */ 19035 ipif->ipif_ill->ill_logical_down = 1; 19036 return (ipif_down(ipif, q, mp)); 19037 } 19038 19039 /* 19040 * This is called when the SIOCSLIFUSESRC ioctl is processed in IP. 19041 * If the usesrc client ILL is already part of a usesrc group or not, 19042 * in either case a ire_stq with the matching usesrc client ILL will 19043 * locate the IRE's that need to be deleted. We want IREs to be created 19044 * with the new source address. 19045 */ 19046 static void 19047 ipif_delete_cache_ire(ire_t *ire, char *ill_arg) 19048 { 19049 ill_t *ucill = (ill_t *)ill_arg; 19050 19051 ASSERT(IAM_WRITER_ILL(ucill)); 19052 19053 if (ire->ire_stq == NULL) 19054 return; 19055 19056 if ((ire->ire_type == IRE_CACHE) && 19057 ((ill_t *)ire->ire_stq->q_ptr == ucill)) 19058 ire_delete(ire); 19059 } 19060 19061 /* 19062 * ire_walk routine to delete every IRE dependent on the interface 19063 * address that is going down. (Always called as writer.) 19064 * Works for both v4 and v6. 19065 * In addition for checking for ire_ipif matches it also checks for 19066 * IRE_CACHE entries which have the same source address as the 19067 * disappearing ipif since ipif_select_source might have picked 19068 * that source. Note that ipif_down/ipif_update_other_ipifs takes 19069 * care of any IRE_INTERFACE with the disappearing source address. 19070 */ 19071 static void 19072 ipif_down_delete_ire(ire_t *ire, char *ipif_arg) 19073 { 19074 ipif_t *ipif = (ipif_t *)ipif_arg; 19075 ill_t *ire_ill; 19076 ill_t *ipif_ill; 19077 19078 ASSERT(IAM_WRITER_IPIF(ipif)); 19079 if (ire->ire_ipif == NULL) 19080 return; 19081 19082 /* 19083 * For IPv4, we derive source addresses for an IRE from ipif's 19084 * belonging to the same IPMP group as the IRE's outgoing 19085 * interface. If an IRE's outgoing interface isn't in the 19086 * same IPMP group as a particular ipif, then that ipif 19087 * couldn't have been used as a source address for this IRE. 19088 * 19089 * For IPv6, source addresses are only restricted to the IPMP group 19090 * if the IRE is for a link-local address or a multicast address. 19091 * Otherwise, source addresses for an IRE can be chosen from 19092 * interfaces other than the the outgoing interface for that IRE. 19093 * 19094 * For source address selection details, see ipif_select_source() 19095 * and ipif_select_source_v6(). 19096 */ 19097 if (ire->ire_ipversion == IPV4_VERSION || 19098 IN6_IS_ADDR_LINKLOCAL(&ire->ire_addr_v6) || 19099 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 19100 ire_ill = ire->ire_ipif->ipif_ill; 19101 ipif_ill = ipif->ipif_ill; 19102 19103 if (ire_ill->ill_group != ipif_ill->ill_group) { 19104 return; 19105 } 19106 } 19107 19108 19109 if (ire->ire_ipif != ipif) { 19110 /* 19111 * Look for a matching source address. 19112 */ 19113 if (ire->ire_type != IRE_CACHE) 19114 return; 19115 if (ipif->ipif_flags & IPIF_NOLOCAL) 19116 return; 19117 19118 if (ire->ire_ipversion == IPV4_VERSION) { 19119 if (ire->ire_src_addr != ipif->ipif_src_addr) 19120 return; 19121 } else { 19122 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_src_addr_v6, 19123 &ipif->ipif_v6lcl_addr)) 19124 return; 19125 } 19126 ire_delete(ire); 19127 return; 19128 } 19129 /* 19130 * ire_delete() will do an ire_flush_cache which will delete 19131 * all ire_ipif matches 19132 */ 19133 ire_delete(ire); 19134 } 19135 19136 /* 19137 * ire_walk_ill function for deleting all IRE_CACHE entries for an ill when 19138 * 1) an ipif (on that ill) changes the IPIF_DEPRECATED flags, or 19139 * 2) when an interface is brought up or down (on that ill). 19140 * This ensures that the IRE_CACHE entries don't retain stale source 19141 * address selection results. 19142 */ 19143 void 19144 ill_ipif_cache_delete(ire_t *ire, char *ill_arg) 19145 { 19146 ill_t *ill = (ill_t *)ill_arg; 19147 ill_t *ipif_ill; 19148 19149 ASSERT(IAM_WRITER_ILL(ill)); 19150 /* 19151 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4. 19152 * Hence this should be IRE_CACHE. 19153 */ 19154 ASSERT(ire->ire_type == IRE_CACHE); 19155 19156 /* 19157 * We are called for IRE_CACHES whose ire_ipif matches ill. 19158 * We are only interested in IRE_CACHES that has borrowed 19159 * the source address from ill_arg e.g. ipif_up_done[_v6] 19160 * for which we need to look at ire_ipif->ipif_ill match 19161 * with ill. 19162 */ 19163 ASSERT(ire->ire_ipif != NULL); 19164 ipif_ill = ire->ire_ipif->ipif_ill; 19165 if (ipif_ill == ill || (ill->ill_group != NULL && 19166 ipif_ill->ill_group == ill->ill_group)) { 19167 ire_delete(ire); 19168 } 19169 } 19170 19171 /* 19172 * Delete all the ire whose stq references ill_arg. 19173 */ 19174 static void 19175 ill_stq_cache_delete(ire_t *ire, char *ill_arg) 19176 { 19177 ill_t *ill = (ill_t *)ill_arg; 19178 ill_t *ire_ill; 19179 19180 ASSERT(IAM_WRITER_ILL(ill)); 19181 /* 19182 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4. 19183 * Hence this should be IRE_CACHE. 19184 */ 19185 ASSERT(ire->ire_type == IRE_CACHE); 19186 19187 /* 19188 * We are called for IRE_CACHES whose ire_stq and ire_ipif 19189 * matches ill. We are only interested in IRE_CACHES that 19190 * has ire_stq->q_ptr pointing at ill_arg. Thus we do the 19191 * filtering here. 19192 */ 19193 ire_ill = (ill_t *)ire->ire_stq->q_ptr; 19194 19195 if (ire_ill == ill) 19196 ire_delete(ire); 19197 } 19198 19199 /* 19200 * This is called when an ill leaves the group. We want to delete 19201 * all IRE_CACHES whose stq is pointing at ill_wq or ire_ipif is 19202 * pointing at ill. 19203 */ 19204 static void 19205 illgrp_cache_delete(ire_t *ire, char *ill_arg) 19206 { 19207 ill_t *ill = (ill_t *)ill_arg; 19208 19209 ASSERT(IAM_WRITER_ILL(ill)); 19210 ASSERT(ill->ill_group == NULL); 19211 /* 19212 * We use MATCH_IRE_TYPE/IRE_CACHE while calling ire_walk_ill_v4. 19213 * Hence this should be IRE_CACHE. 19214 */ 19215 ASSERT(ire->ire_type == IRE_CACHE); 19216 /* 19217 * We are called for IRE_CACHES whose ire_stq and ire_ipif 19218 * matches ill. We are interested in both. 19219 */ 19220 ASSERT((ill == (ill_t *)ire->ire_stq->q_ptr) || 19221 (ire->ire_ipif->ipif_ill == ill)); 19222 19223 ire_delete(ire); 19224 } 19225 19226 /* 19227 * Initiate deallocate of an IPIF. Always called as writer. Called by 19228 * ill_delete or ip_sioctl_removeif. 19229 */ 19230 static void 19231 ipif_free(ipif_t *ipif) 19232 { 19233 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 19234 19235 ASSERT(IAM_WRITER_IPIF(ipif)); 19236 19237 if (ipif->ipif_recovery_id != 0) 19238 (void) untimeout(ipif->ipif_recovery_id); 19239 ipif->ipif_recovery_id = 0; 19240 19241 /* Remove conn references */ 19242 reset_conn_ipif(ipif); 19243 19244 /* 19245 * Make sure we have valid net and subnet broadcast ire's for the 19246 * other ipif's which share them with this ipif. 19247 */ 19248 if (!ipif->ipif_isv6) 19249 ipif_check_bcast_ires(ipif); 19250 19251 /* 19252 * Take down the interface. We can be called either from ill_delete 19253 * or from ip_sioctl_removeif. 19254 */ 19255 (void) ipif_down(ipif, NULL, NULL); 19256 19257 /* 19258 * Now that the interface is down, there's no chance it can still 19259 * become a duplicate. Cancel any timer that may have been set while 19260 * tearing down. 19261 */ 19262 if (ipif->ipif_recovery_id != 0) 19263 (void) untimeout(ipif->ipif_recovery_id); 19264 ipif->ipif_recovery_id = 0; 19265 19266 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 19267 /* Remove pointers to this ill in the multicast routing tables */ 19268 reset_mrt_vif_ipif(ipif); 19269 rw_exit(&ipst->ips_ill_g_lock); 19270 } 19271 19272 /* 19273 * Warning: this is not the only function that calls mi_free on an ipif_t. See 19274 * also ill_move(). 19275 */ 19276 static void 19277 ipif_free_tail(ipif_t *ipif) 19278 { 19279 mblk_t *mp; 19280 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 19281 19282 /* 19283 * Free state for addition IRE_IF_[NO]RESOLVER ire's. 19284 */ 19285 mutex_enter(&ipif->ipif_saved_ire_lock); 19286 mp = ipif->ipif_saved_ire_mp; 19287 ipif->ipif_saved_ire_mp = NULL; 19288 mutex_exit(&ipif->ipif_saved_ire_lock); 19289 freemsg(mp); 19290 19291 /* 19292 * Need to hold both ill_g_lock and ill_lock while 19293 * inserting or removing an ipif from the linked list 19294 * of ipifs hanging off the ill. 19295 */ 19296 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 19297 /* 19298 * Remove all multicast memberships on the interface now. 19299 * This removes IPv4 multicast memberships joined within 19300 * the kernel as ipif_down does not do ipif_multicast_down 19301 * for IPv4. IPv6 is not handled here as the multicast memberships 19302 * are based on ill and not on ipif. 19303 */ 19304 ilm_free(ipif); 19305 19306 /* 19307 * Since we held the ill_g_lock while doing the ilm_free above, 19308 * we can assert the ilms were really deleted and not just marked 19309 * ILM_DELETED. 19310 */ 19311 ASSERT(ilm_walk_ipif(ipif) == 0); 19312 19313 IPIF_TRACE_CLEANUP(ipif); 19314 19315 /* Ask SCTP to take it out of it list */ 19316 sctp_update_ipif(ipif, SCTP_IPIF_REMOVE); 19317 19318 /* Get it out of the ILL interface list. */ 19319 ipif_remove(ipif, B_TRUE); 19320 rw_exit(&ipst->ips_ill_g_lock); 19321 19322 mutex_destroy(&ipif->ipif_saved_ire_lock); 19323 19324 ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE))); 19325 ASSERT(ipif->ipif_recovery_id == 0); 19326 19327 /* Free the memory. */ 19328 mi_free(ipif); 19329 } 19330 19331 /* 19332 * Returns an ipif name in the form "ill_name/unit" if ipif_id is not zero, 19333 * "ill_name" otherwise. 19334 */ 19335 char * 19336 ipif_get_name(const ipif_t *ipif, char *buf, int len) 19337 { 19338 char lbuf[32]; 19339 char *name; 19340 size_t name_len; 19341 19342 buf[0] = '\0'; 19343 if (!ipif) 19344 return (buf); 19345 name = ipif->ipif_ill->ill_name; 19346 name_len = ipif->ipif_ill->ill_name_length; 19347 if (ipif->ipif_id != 0) { 19348 (void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR, 19349 ipif->ipif_id); 19350 name = lbuf; 19351 name_len = mi_strlen(name) + 1; 19352 } 19353 len -= 1; 19354 buf[len] = '\0'; 19355 len = MIN(len, name_len); 19356 bcopy(name, buf, len); 19357 return (buf); 19358 } 19359 19360 /* 19361 * Find an IPIF based on the name passed in. Names can be of the 19362 * form <phys> (e.g., le0), <phys>:<#> (e.g., le0:1), 19363 * The <phys> string can have forms like <dev><#> (e.g., le0), 19364 * <dev><#>.<module> (e.g. le0.foo), or <dev>.<module><#> (e.g. ip.tun3). 19365 * When there is no colon, the implied unit id is zero. <phys> must 19366 * correspond to the name of an ILL. (May be called as writer.) 19367 */ 19368 static ipif_t * 19369 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc, 19370 boolean_t *exists, boolean_t isv6, zoneid_t zoneid, queue_t *q, 19371 mblk_t *mp, ipsq_func_t func, int *error, ip_stack_t *ipst) 19372 { 19373 char *cp; 19374 char *endp; 19375 long id; 19376 ill_t *ill; 19377 ipif_t *ipif; 19378 uint_t ire_type; 19379 boolean_t did_alloc = B_FALSE; 19380 ipsq_t *ipsq; 19381 19382 if (error != NULL) 19383 *error = 0; 19384 19385 /* 19386 * If the caller wants to us to create the ipif, make sure we have a 19387 * valid zoneid 19388 */ 19389 ASSERT(!do_alloc || zoneid != ALL_ZONES); 19390 19391 if (namelen == 0) { 19392 if (error != NULL) 19393 *error = ENXIO; 19394 return (NULL); 19395 } 19396 19397 *exists = B_FALSE; 19398 /* Look for a colon in the name. */ 19399 endp = &name[namelen]; 19400 for (cp = endp; --cp > name; ) { 19401 if (*cp == IPIF_SEPARATOR_CHAR) 19402 break; 19403 } 19404 19405 if (*cp == IPIF_SEPARATOR_CHAR) { 19406 /* 19407 * Reject any non-decimal aliases for logical 19408 * interfaces. Aliases with leading zeroes 19409 * are also rejected as they introduce ambiguity 19410 * in the naming of the interfaces. 19411 * In order to confirm with existing semantics, 19412 * and to not break any programs/script relying 19413 * on that behaviour, if<0>:0 is considered to be 19414 * a valid interface. 19415 * 19416 * If alias has two or more digits and the first 19417 * is zero, fail. 19418 */ 19419 if (&cp[2] < endp && cp[1] == '0') 19420 return (NULL); 19421 } 19422 19423 if (cp <= name) { 19424 cp = endp; 19425 } else { 19426 *cp = '\0'; 19427 } 19428 19429 /* 19430 * Look up the ILL, based on the portion of the name 19431 * before the slash. ill_lookup_on_name returns a held ill. 19432 * Temporary to check whether ill exists already. If so 19433 * ill_lookup_on_name will clear it. 19434 */ 19435 ill = ill_lookup_on_name(name, do_alloc, isv6, 19436 q, mp, func, error, &did_alloc, ipst); 19437 if (cp != endp) 19438 *cp = IPIF_SEPARATOR_CHAR; 19439 if (ill == NULL) 19440 return (NULL); 19441 19442 /* Establish the unit number in the name. */ 19443 id = 0; 19444 if (cp < endp && *endp == '\0') { 19445 /* If there was a colon, the unit number follows. */ 19446 cp++; 19447 if (ddi_strtol(cp, NULL, 0, &id) != 0) { 19448 ill_refrele(ill); 19449 if (error != NULL) 19450 *error = ENXIO; 19451 return (NULL); 19452 } 19453 } 19454 19455 GRAB_CONN_LOCK(q); 19456 mutex_enter(&ill->ill_lock); 19457 /* Now see if there is an IPIF with this unit number. */ 19458 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 19459 if (ipif->ipif_id == id) { 19460 if (zoneid != ALL_ZONES && 19461 zoneid != ipif->ipif_zoneid && 19462 ipif->ipif_zoneid != ALL_ZONES) { 19463 mutex_exit(&ill->ill_lock); 19464 RELEASE_CONN_LOCK(q); 19465 ill_refrele(ill); 19466 if (error != NULL) 19467 *error = ENXIO; 19468 return (NULL); 19469 } 19470 /* 19471 * The block comment at the start of ipif_down 19472 * explains the use of the macros used below 19473 */ 19474 if (IPIF_CAN_LOOKUP(ipif)) { 19475 ipif_refhold_locked(ipif); 19476 mutex_exit(&ill->ill_lock); 19477 if (!did_alloc) 19478 *exists = B_TRUE; 19479 /* 19480 * Drop locks before calling ill_refrele 19481 * since it can potentially call into 19482 * ipif_ill_refrele_tail which can end up 19483 * in trying to acquire any lock. 19484 */ 19485 RELEASE_CONN_LOCK(q); 19486 ill_refrele(ill); 19487 return (ipif); 19488 } else if (IPIF_CAN_WAIT(ipif, q)) { 19489 ipsq = ill->ill_phyint->phyint_ipsq; 19490 mutex_enter(&ipsq->ipsq_lock); 19491 mutex_exit(&ill->ill_lock); 19492 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill); 19493 mutex_exit(&ipsq->ipsq_lock); 19494 RELEASE_CONN_LOCK(q); 19495 ill_refrele(ill); 19496 *error = EINPROGRESS; 19497 return (NULL); 19498 } 19499 } 19500 } 19501 RELEASE_CONN_LOCK(q); 19502 19503 if (!do_alloc) { 19504 mutex_exit(&ill->ill_lock); 19505 ill_refrele(ill); 19506 if (error != NULL) 19507 *error = ENXIO; 19508 return (NULL); 19509 } 19510 19511 /* 19512 * If none found, atomically allocate and return a new one. 19513 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL 19514 * to support "receive only" use of lo0:1 etc. as is still done 19515 * below as an initial guess. 19516 * However, this is now likely to be overriden later in ipif_up_done() 19517 * when we know for sure what address has been configured on the 19518 * interface, since we might have more than one loopback interface 19519 * with a loopback address, e.g. in the case of zones, and all the 19520 * interfaces with loopback addresses need to be marked IRE_LOOPBACK. 19521 */ 19522 if (ill->ill_net_type == IRE_LOOPBACK && id == 0) 19523 ire_type = IRE_LOOPBACK; 19524 else 19525 ire_type = IRE_LOCAL; 19526 ipif = ipif_allocate(ill, id, ire_type, B_TRUE); 19527 if (ipif != NULL) 19528 ipif_refhold_locked(ipif); 19529 else if (error != NULL) 19530 *error = ENOMEM; 19531 mutex_exit(&ill->ill_lock); 19532 ill_refrele(ill); 19533 return (ipif); 19534 } 19535 19536 /* 19537 * This routine is called whenever a new address comes up on an ipif. If 19538 * we are configured to respond to address mask requests, then we are supposed 19539 * to broadcast an address mask reply at this time. This routine is also 19540 * called if we are already up, but a netmask change is made. This is legal 19541 * but might not make the system manager very popular. (May be called 19542 * as writer.) 19543 */ 19544 void 19545 ipif_mask_reply(ipif_t *ipif) 19546 { 19547 icmph_t *icmph; 19548 ipha_t *ipha; 19549 mblk_t *mp; 19550 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 19551 19552 #define REPLY_LEN (sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN) 19553 19554 if (!ipst->ips_ip_respond_to_address_mask_broadcast) 19555 return; 19556 19557 /* ICMP mask reply is IPv4 only */ 19558 ASSERT(!ipif->ipif_isv6); 19559 /* ICMP mask reply is not for a loopback interface */ 19560 ASSERT(ipif->ipif_ill->ill_wq != NULL); 19561 19562 mp = allocb(REPLY_LEN, BPRI_HI); 19563 if (mp == NULL) 19564 return; 19565 mp->b_wptr = mp->b_rptr + REPLY_LEN; 19566 19567 ipha = (ipha_t *)mp->b_rptr; 19568 bzero(ipha, REPLY_LEN); 19569 *ipha = icmp_ipha; 19570 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl; 19571 ipha->ipha_src = ipif->ipif_src_addr; 19572 ipha->ipha_dst = ipif->ipif_brd_addr; 19573 ipha->ipha_length = htons(REPLY_LEN); 19574 ipha->ipha_ident = 0; 19575 19576 icmph = (icmph_t *)&ipha[1]; 19577 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY; 19578 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN); 19579 icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0); 19580 19581 put(ipif->ipif_wq, mp); 19582 19583 #undef REPLY_LEN 19584 } 19585 19586 /* 19587 * When the mtu in the ipif changes, we call this routine through ire_walk 19588 * to update all the relevant IREs. 19589 * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq. 19590 */ 19591 static void 19592 ipif_mtu_change(ire_t *ire, char *ipif_arg) 19593 { 19594 ipif_t *ipif = (ipif_t *)ipif_arg; 19595 19596 if (ire->ire_stq == NULL || ire->ire_ipif != ipif) 19597 return; 19598 ire->ire_max_frag = MIN(ipif->ipif_mtu, IP_MAXPACKET); 19599 } 19600 19601 /* 19602 * When the mtu in the ill changes, we call this routine through ire_walk 19603 * to update all the relevant IREs. 19604 * Skip IRE_LOCAL and "loopback" IRE_BROADCAST by checking ire_stq. 19605 */ 19606 void 19607 ill_mtu_change(ire_t *ire, char *ill_arg) 19608 { 19609 ill_t *ill = (ill_t *)ill_arg; 19610 19611 if (ire->ire_stq == NULL || ire->ire_ipif->ipif_ill != ill) 19612 return; 19613 ire->ire_max_frag = ire->ire_ipif->ipif_mtu; 19614 } 19615 19616 /* 19617 * Join the ipif specific multicast groups. 19618 * Must be called after a mapping has been set up in the resolver. (Always 19619 * called as writer.) 19620 */ 19621 void 19622 ipif_multicast_up(ipif_t *ipif) 19623 { 19624 int err, index; 19625 ill_t *ill; 19626 19627 ASSERT(IAM_WRITER_IPIF(ipif)); 19628 19629 ill = ipif->ipif_ill; 19630 index = ill->ill_phyint->phyint_ifindex; 19631 19632 ip1dbg(("ipif_multicast_up\n")); 19633 if (!(ill->ill_flags & ILLF_MULTICAST) || ipif->ipif_multicast_up) 19634 return; 19635 19636 if (ipif->ipif_isv6) { 19637 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) 19638 return; 19639 19640 /* Join the all hosts multicast address */ 19641 ip1dbg(("ipif_multicast_up - addmulti\n")); 19642 /* 19643 * Passing B_TRUE means we have to join the multicast 19644 * membership on this interface even though this is 19645 * FAILED. If we join on a different one in the group, 19646 * we will not be able to delete the membership later 19647 * as we currently don't track where we join when we 19648 * join within the kernel unlike applications where 19649 * we have ilg/ilg_orig_index. See ip_addmulti_v6 19650 * for more on this. 19651 */ 19652 err = ip_addmulti_v6(&ipv6_all_hosts_mcast, ill, index, 19653 ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL); 19654 if (err != 0) { 19655 ip0dbg(("ipif_multicast_up: " 19656 "all_hosts_mcast failed %d\n", 19657 err)); 19658 return; 19659 } 19660 /* 19661 * Enable multicast for the solicited node multicast address 19662 */ 19663 if (!(ipif->ipif_flags & IPIF_NOLOCAL)) { 19664 in6_addr_t ipv6_multi = ipv6_solicited_node_mcast; 19665 19666 ipv6_multi.s6_addr32[3] |= 19667 ipif->ipif_v6lcl_addr.s6_addr32[3]; 19668 19669 err = ip_addmulti_v6(&ipv6_multi, ill, index, 19670 ipif->ipif_zoneid, ILGSTAT_NONE, MODE_IS_EXCLUDE, 19671 NULL); 19672 if (err != 0) { 19673 ip0dbg(("ipif_multicast_up: solicited MC" 19674 " failed %d\n", err)); 19675 (void) ip_delmulti_v6(&ipv6_all_hosts_mcast, 19676 ill, ill->ill_phyint->phyint_ifindex, 19677 ipif->ipif_zoneid, B_TRUE, B_TRUE); 19678 return; 19679 } 19680 } 19681 } else { 19682 if (ipif->ipif_lcl_addr == INADDR_ANY) 19683 return; 19684 19685 /* Join the all hosts multicast address */ 19686 ip1dbg(("ipif_multicast_up - addmulti\n")); 19687 err = ip_addmulti(htonl(INADDR_ALLHOSTS_GROUP), ipif, 19688 ILGSTAT_NONE, MODE_IS_EXCLUDE, NULL); 19689 if (err) { 19690 ip0dbg(("ipif_multicast_up: failed %d\n", err)); 19691 return; 19692 } 19693 } 19694 ipif->ipif_multicast_up = 1; 19695 } 19696 19697 /* 19698 * Blow away any IPv6 multicast groups that we joined in ipif_multicast_up(); 19699 * any explicit memberships are blown away in ill_leave_multicast() when the 19700 * ill is brought down. 19701 */ 19702 static void 19703 ipif_multicast_down(ipif_t *ipif) 19704 { 19705 int err; 19706 19707 ASSERT(IAM_WRITER_IPIF(ipif)); 19708 19709 ip1dbg(("ipif_multicast_down\n")); 19710 if (!ipif->ipif_multicast_up) 19711 return; 19712 19713 ASSERT(ipif->ipif_isv6); 19714 19715 ip1dbg(("ipif_multicast_down - delmulti\n")); 19716 19717 /* 19718 * Leave the all hosts multicast address. Similar to ip_addmulti_v6, 19719 * we should look for ilms on this ill rather than the ones that have 19720 * been failed over here. They are here temporarily. As 19721 * ipif_multicast_up has joined on this ill, we should delete only 19722 * from this ill. 19723 */ 19724 err = ip_delmulti_v6(&ipv6_all_hosts_mcast, ipif->ipif_ill, 19725 ipif->ipif_ill->ill_phyint->phyint_ifindex, ipif->ipif_zoneid, 19726 B_TRUE, B_TRUE); 19727 if (err != 0) { 19728 ip0dbg(("ipif_multicast_down: all_hosts_mcast failed %d\n", 19729 err)); 19730 } 19731 /* 19732 * Disable multicast for the solicited node multicast address 19733 */ 19734 if (!(ipif->ipif_flags & IPIF_NOLOCAL)) { 19735 in6_addr_t ipv6_multi = ipv6_solicited_node_mcast; 19736 19737 ipv6_multi.s6_addr32[3] |= 19738 ipif->ipif_v6lcl_addr.s6_addr32[3]; 19739 19740 err = ip_delmulti_v6(&ipv6_multi, ipif->ipif_ill, 19741 ipif->ipif_ill->ill_phyint->phyint_ifindex, 19742 ipif->ipif_zoneid, B_TRUE, B_TRUE); 19743 19744 if (err != 0) { 19745 ip0dbg(("ipif_multicast_down: sol MC failed %d\n", 19746 err)); 19747 } 19748 } 19749 19750 ipif->ipif_multicast_up = 0; 19751 } 19752 19753 /* 19754 * Used when an interface comes up to recreate any extra routes on this 19755 * interface. 19756 */ 19757 static ire_t ** 19758 ipif_recover_ire(ipif_t *ipif) 19759 { 19760 mblk_t *mp; 19761 ire_t **ipif_saved_irep; 19762 ire_t **irep; 19763 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 19764 19765 ip1dbg(("ipif_recover_ire(%s:%u)", ipif->ipif_ill->ill_name, 19766 ipif->ipif_id)); 19767 19768 mutex_enter(&ipif->ipif_saved_ire_lock); 19769 ipif_saved_irep = (ire_t **)kmem_zalloc(sizeof (ire_t *) * 19770 ipif->ipif_saved_ire_cnt, KM_NOSLEEP); 19771 if (ipif_saved_irep == NULL) { 19772 mutex_exit(&ipif->ipif_saved_ire_lock); 19773 return (NULL); 19774 } 19775 19776 irep = ipif_saved_irep; 19777 for (mp = ipif->ipif_saved_ire_mp; mp != NULL; mp = mp->b_cont) { 19778 ire_t *ire; 19779 queue_t *rfq; 19780 queue_t *stq; 19781 ifrt_t *ifrt; 19782 uchar_t *src_addr; 19783 uchar_t *gateway_addr; 19784 mblk_t *resolver_mp; 19785 ushort_t type; 19786 19787 /* 19788 * When the ire was initially created and then added in 19789 * ip_rt_add(), it was created either using ipif->ipif_net_type 19790 * in the case of a traditional interface route, or as one of 19791 * the IRE_OFFSUBNET types (with the exception of 19792 * IRE_HOST types ire which is created by icmp_redirect() and 19793 * which we don't need to save or recover). In the case where 19794 * ipif->ipif_net_type was IRE_LOOPBACK, ip_rt_add() will update 19795 * the ire_type to IRE_IF_NORESOLVER before calling ire_add() 19796 * to satisfy software like GateD and Sun Cluster which creates 19797 * routes using the the loopback interface's address as a 19798 * gateway. 19799 * 19800 * As ifrt->ifrt_type reflects the already updated ire_type and 19801 * since ire_create() expects that IRE_IF_NORESOLVER will have 19802 * a valid nce_res_mp field (which doesn't make sense for a 19803 * IRE_LOOPBACK), ire_create() will be called in the same way 19804 * here as in ip_rt_add(), namely using ipif->ipif_net_type when 19805 * the route looks like a traditional interface route (where 19806 * ifrt->ifrt_type & IRE_INTERFACE is true) and otherwise using 19807 * the saved ifrt->ifrt_type. This means that in the case where 19808 * ipif->ipif_net_type is IRE_LOOPBACK, the ire created by 19809 * ire_create() will be an IRE_LOOPBACK, it will then be turned 19810 * into an IRE_IF_NORESOLVER and then added by ire_add(). 19811 */ 19812 ifrt = (ifrt_t *)mp->b_rptr; 19813 if (ifrt->ifrt_type & IRE_INTERFACE) { 19814 rfq = NULL; 19815 stq = (ipif->ipif_net_type == IRE_IF_RESOLVER) 19816 ? ipif->ipif_rq : ipif->ipif_wq; 19817 src_addr = (ifrt->ifrt_flags & RTF_SETSRC) 19818 ? (uint8_t *)&ifrt->ifrt_src_addr 19819 : (uint8_t *)&ipif->ipif_src_addr; 19820 gateway_addr = NULL; 19821 resolver_mp = ipif->ipif_resolver_mp; 19822 type = ipif->ipif_net_type; 19823 } else if (ifrt->ifrt_type & IRE_BROADCAST) { 19824 /* Recover multiroute broadcast IRE. */ 19825 rfq = ipif->ipif_rq; 19826 stq = ipif->ipif_wq; 19827 src_addr = (ifrt->ifrt_flags & RTF_SETSRC) 19828 ? (uint8_t *)&ifrt->ifrt_src_addr 19829 : (uint8_t *)&ipif->ipif_src_addr; 19830 gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr; 19831 resolver_mp = ipif->ipif_bcast_mp; 19832 type = ifrt->ifrt_type; 19833 } else { 19834 rfq = NULL; 19835 stq = NULL; 19836 src_addr = (ifrt->ifrt_flags & RTF_SETSRC) 19837 ? (uint8_t *)&ifrt->ifrt_src_addr : NULL; 19838 gateway_addr = (uint8_t *)&ifrt->ifrt_gateway_addr; 19839 resolver_mp = NULL; 19840 type = ifrt->ifrt_type; 19841 } 19842 19843 /* 19844 * Create a copy of the IRE with the saved address and netmask. 19845 */ 19846 ip1dbg(("ipif_recover_ire: creating IRE %s (%d) for " 19847 "0x%x/0x%x\n", 19848 ip_nv_lookup(ire_nv_tbl, ifrt->ifrt_type), ifrt->ifrt_type, 19849 ntohl(ifrt->ifrt_addr), 19850 ntohl(ifrt->ifrt_mask))); 19851 ire = ire_create( 19852 (uint8_t *)&ifrt->ifrt_addr, 19853 (uint8_t *)&ifrt->ifrt_mask, 19854 src_addr, 19855 gateway_addr, 19856 NULL, 19857 &ifrt->ifrt_max_frag, 19858 NULL, 19859 rfq, 19860 stq, 19861 type, 19862 resolver_mp, 19863 ipif, 19864 NULL, 19865 0, 19866 0, 19867 0, 19868 ifrt->ifrt_flags, 19869 &ifrt->ifrt_iulp_info, 19870 NULL, 19871 NULL, 19872 ipst); 19873 19874 if (ire == NULL) { 19875 mutex_exit(&ipif->ipif_saved_ire_lock); 19876 kmem_free(ipif_saved_irep, 19877 ipif->ipif_saved_ire_cnt * sizeof (ire_t *)); 19878 return (NULL); 19879 } 19880 19881 /* 19882 * Some software (for example, GateD and Sun Cluster) attempts 19883 * to create (what amount to) IRE_PREFIX routes with the 19884 * loopback address as the gateway. This is primarily done to 19885 * set up prefixes with the RTF_REJECT flag set (for example, 19886 * when generating aggregate routes.) 19887 * 19888 * If the IRE type (as defined by ipif->ipif_net_type) is 19889 * IRE_LOOPBACK, then we map the request into a 19890 * IRE_IF_NORESOLVER. 19891 */ 19892 if (ipif->ipif_net_type == IRE_LOOPBACK) 19893 ire->ire_type = IRE_IF_NORESOLVER; 19894 /* 19895 * ire held by ire_add, will be refreled' towards the 19896 * the end of ipif_up_done 19897 */ 19898 (void) ire_add(&ire, NULL, NULL, NULL, B_FALSE); 19899 *irep = ire; 19900 irep++; 19901 ip1dbg(("ipif_recover_ire: added ire %p\n", (void *)ire)); 19902 } 19903 mutex_exit(&ipif->ipif_saved_ire_lock); 19904 return (ipif_saved_irep); 19905 } 19906 19907 /* 19908 * Used to set the netmask and broadcast address to default values when the 19909 * interface is brought up. (Always called as writer.) 19910 */ 19911 static void 19912 ipif_set_default(ipif_t *ipif) 19913 { 19914 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 19915 19916 if (!ipif->ipif_isv6) { 19917 /* 19918 * Interface holds an IPv4 address. Default 19919 * mask is the natural netmask. 19920 */ 19921 if (!ipif->ipif_net_mask) { 19922 ipaddr_t v4mask; 19923 19924 v4mask = ip_net_mask(ipif->ipif_lcl_addr); 19925 V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask); 19926 } 19927 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 19928 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */ 19929 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr; 19930 } else { 19931 V6_MASK_COPY(ipif->ipif_v6lcl_addr, 19932 ipif->ipif_v6net_mask, ipif->ipif_v6subnet); 19933 } 19934 /* 19935 * NOTE: SunOS 4.X does this even if the broadcast address 19936 * has been already set thus we do the same here. 19937 */ 19938 if (ipif->ipif_flags & IPIF_BROADCAST) { 19939 ipaddr_t v4addr; 19940 19941 v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask; 19942 IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr); 19943 } 19944 } else { 19945 /* 19946 * Interface holds an IPv6-only address. Default 19947 * mask is all-ones. 19948 */ 19949 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask)) 19950 ipif->ipif_v6net_mask = ipv6_all_ones; 19951 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 19952 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */ 19953 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr; 19954 } else { 19955 V6_MASK_COPY(ipif->ipif_v6lcl_addr, 19956 ipif->ipif_v6net_mask, ipif->ipif_v6subnet); 19957 } 19958 } 19959 } 19960 19961 /* 19962 * Return 0 if this address can be used as local address without causing 19963 * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address 19964 * is already up on a different ill, and EADDRINUSE if it's up on the same ill. 19965 * Special checks are needed to allow the same IPv6 link-local address 19966 * on different ills. 19967 * TODO: allowing the same site-local address on different ill's. 19968 */ 19969 int 19970 ip_addr_availability_check(ipif_t *new_ipif) 19971 { 19972 in6_addr_t our_v6addr; 19973 ill_t *ill; 19974 ipif_t *ipif; 19975 ill_walk_context_t ctx; 19976 ip_stack_t *ipst = new_ipif->ipif_ill->ill_ipst; 19977 19978 ASSERT(IAM_WRITER_IPIF(new_ipif)); 19979 ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock)); 19980 ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock)); 19981 19982 new_ipif->ipif_flags &= ~IPIF_UNNUMBERED; 19983 if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) || 19984 IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr)) 19985 return (0); 19986 19987 our_v6addr = new_ipif->ipif_v6lcl_addr; 19988 19989 if (new_ipif->ipif_isv6) 19990 ill = ILL_START_WALK_V6(&ctx, ipst); 19991 else 19992 ill = ILL_START_WALK_V4(&ctx, ipst); 19993 19994 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 19995 for (ipif = ill->ill_ipif; ipif != NULL; 19996 ipif = ipif->ipif_next) { 19997 if ((ipif == new_ipif) || 19998 !(ipif->ipif_flags & IPIF_UP) || 19999 (ipif->ipif_flags & IPIF_UNNUMBERED)) 20000 continue; 20001 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr, 20002 &our_v6addr)) { 20003 if (new_ipif->ipif_flags & IPIF_POINTOPOINT) 20004 new_ipif->ipif_flags |= IPIF_UNNUMBERED; 20005 else if (ipif->ipif_flags & IPIF_POINTOPOINT) 20006 ipif->ipif_flags |= IPIF_UNNUMBERED; 20007 else if (IN6_IS_ADDR_LINKLOCAL(&our_v6addr) && 20008 new_ipif->ipif_ill != ill) 20009 continue; 20010 else if (IN6_IS_ADDR_SITELOCAL(&our_v6addr) && 20011 new_ipif->ipif_ill != ill) 20012 continue; 20013 else if (new_ipif->ipif_zoneid != 20014 ipif->ipif_zoneid && 20015 ipif->ipif_zoneid != ALL_ZONES && 20016 (ill->ill_phyint->phyint_flags & 20017 PHYI_LOOPBACK)) 20018 continue; 20019 else if (new_ipif->ipif_ill == ill) 20020 return (EADDRINUSE); 20021 else 20022 return (EADDRNOTAVAIL); 20023 } 20024 } 20025 } 20026 20027 return (0); 20028 } 20029 20030 /* 20031 * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add 20032 * IREs for the ipif. 20033 * When the routine returns EINPROGRESS then mp has been consumed and 20034 * the ioctl will be acked from ip_rput_dlpi. 20035 */ 20036 static int 20037 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp) 20038 { 20039 ill_t *ill = ipif->ipif_ill; 20040 boolean_t isv6 = ipif->ipif_isv6; 20041 int err = 0; 20042 boolean_t success; 20043 20044 ASSERT(IAM_WRITER_IPIF(ipif)); 20045 20046 ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id)); 20047 20048 /* Shouldn't get here if it is already up. */ 20049 if (ipif->ipif_flags & IPIF_UP) 20050 return (EALREADY); 20051 20052 /* Skip arp/ndp for any loopback interface. */ 20053 if (ill->ill_wq != NULL) { 20054 conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL; 20055 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq; 20056 20057 if (!ill->ill_dl_up) { 20058 /* 20059 * ill_dl_up is not yet set. i.e. we are yet to 20060 * DL_BIND with the driver and this is the first 20061 * logical interface on the ill to become "up". 20062 * Tell the driver to get going (via DL_BIND_REQ). 20063 * Note that changing "significant" IFF_ flags 20064 * address/netmask etc cause a down/up dance, but 20065 * does not cause an unbind (DL_UNBIND) with the driver 20066 */ 20067 return (ill_dl_up(ill, ipif, mp, q)); 20068 } 20069 20070 /* 20071 * ipif_resolver_up may end up sending an 20072 * AR_INTERFACE_UP message to ARP, which would, in 20073 * turn send a DLPI message to the driver. ioctls are 20074 * serialized and so we cannot send more than one 20075 * interface up message at a time. If ipif_resolver_up 20076 * does send an interface up message to ARP, we get 20077 * EINPROGRESS and we will complete in ip_arp_done. 20078 */ 20079 20080 ASSERT(connp != NULL || !CONN_Q(q)); 20081 ASSERT(ipsq->ipsq_pending_mp == NULL); 20082 if (connp != NULL) 20083 mutex_enter(&connp->conn_lock); 20084 mutex_enter(&ill->ill_lock); 20085 success = ipsq_pending_mp_add(connp, ipif, q, mp, 0); 20086 mutex_exit(&ill->ill_lock); 20087 if (connp != NULL) 20088 mutex_exit(&connp->conn_lock); 20089 if (!success) 20090 return (EINTR); 20091 20092 /* 20093 * Crank up IPv6 neighbor discovery 20094 * Unlike ARP, this should complete when 20095 * ipif_ndp_up returns. However, for 20096 * ILLF_XRESOLV interfaces we also send a 20097 * AR_INTERFACE_UP to the external resolver. 20098 * That ioctl will complete in ip_rput. 20099 */ 20100 if (isv6) { 20101 err = ipif_ndp_up(ipif, &ipif->ipif_v6lcl_addr); 20102 if (err != 0) { 20103 if (err != EINPROGRESS) 20104 mp = ipsq_pending_mp_get(ipsq, &connp); 20105 return (err); 20106 } 20107 } 20108 /* Now, ARP */ 20109 err = ipif_resolver_up(ipif, Res_act_initial); 20110 if (err == EINPROGRESS) { 20111 /* We will complete it in ip_arp_done */ 20112 return (err); 20113 } 20114 mp = ipsq_pending_mp_get(ipsq, &connp); 20115 ASSERT(mp != NULL); 20116 if (err != 0) 20117 return (err); 20118 } else { 20119 /* 20120 * Interfaces without underlying hardware don't do duplicate 20121 * address detection. 20122 */ 20123 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE)); 20124 ipif->ipif_addr_ready = 1; 20125 } 20126 return (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif)); 20127 } 20128 20129 /* 20130 * Perform a bind for the physical device. 20131 * When the routine returns EINPROGRESS then mp has been consumed and 20132 * the ioctl will be acked from ip_rput_dlpi. 20133 * Allocate an unbind message and save it until ipif_down. 20134 */ 20135 static int 20136 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q) 20137 { 20138 areq_t *areq; 20139 mblk_t *areq_mp = NULL; 20140 mblk_t *bind_mp = NULL; 20141 mblk_t *unbind_mp = NULL; 20142 conn_t *connp; 20143 boolean_t success; 20144 uint16_t sap_addr; 20145 20146 ip1dbg(("ill_dl_up(%s)\n", ill->ill_name)); 20147 ASSERT(IAM_WRITER_ILL(ill)); 20148 ASSERT(mp != NULL); 20149 20150 /* Create a resolver cookie for ARP */ 20151 if (!ill->ill_isv6 && ill->ill_net_type == IRE_IF_RESOLVER) { 20152 areq_mp = ill_arp_alloc(ill, (uchar_t *)&ip_areq_template, 0); 20153 if (areq_mp == NULL) 20154 return (ENOMEM); 20155 20156 freemsg(ill->ill_resolver_mp); 20157 ill->ill_resolver_mp = areq_mp; 20158 areq = (areq_t *)areq_mp->b_rptr; 20159 sap_addr = ill->ill_sap; 20160 bcopy(&sap_addr, areq->areq_sap, sizeof (sap_addr)); 20161 } 20162 bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long), 20163 DL_BIND_REQ); 20164 if (bind_mp == NULL) 20165 goto bad; 20166 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap; 20167 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS; 20168 20169 unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t), DL_UNBIND_REQ); 20170 if (unbind_mp == NULL) 20171 goto bad; 20172 20173 /* 20174 * Record state needed to complete this operation when the 20175 * DL_BIND_ACK shows up. Also remember the pre-allocated mblks. 20176 */ 20177 ASSERT(WR(q)->q_next == NULL); 20178 connp = Q_TO_CONN(q); 20179 20180 mutex_enter(&connp->conn_lock); 20181 mutex_enter(&ipif->ipif_ill->ill_lock); 20182 success = ipsq_pending_mp_add(connp, ipif, q, mp, 0); 20183 mutex_exit(&ipif->ipif_ill->ill_lock); 20184 mutex_exit(&connp->conn_lock); 20185 if (!success) 20186 goto bad; 20187 20188 /* 20189 * Save the unbind message for ill_dl_down(); it will be consumed when 20190 * the interface goes down. 20191 */ 20192 ASSERT(ill->ill_unbind_mp == NULL); 20193 ill->ill_unbind_mp = unbind_mp; 20194 20195 ill_dlpi_send(ill, bind_mp); 20196 /* Send down link-layer capabilities probe if not already done. */ 20197 ill_capability_probe(ill); 20198 20199 /* 20200 * Sysid used to rely on the fact that netboots set domainname 20201 * and the like. Now that miniroot boots aren't strictly netboots 20202 * and miniroot network configuration is driven from userland 20203 * these things still need to be set. This situation can be detected 20204 * by comparing the interface being configured here to the one 20205 * dhcack was set to reference by the boot loader. Once sysid is 20206 * converted to use dhcp_ipc_getinfo() this call can go away. 20207 */ 20208 if ((ipif->ipif_flags & IPIF_DHCPRUNNING) && (dhcack != NULL) && 20209 (strcmp(ill->ill_name, dhcack) == 0) && 20210 (strlen(srpc_domain) == 0)) { 20211 if (dhcpinit() != 0) 20212 cmn_err(CE_WARN, "no cached dhcp response"); 20213 } 20214 20215 /* 20216 * This operation will complete in ip_rput_dlpi with either 20217 * a DL_BIND_ACK or DL_ERROR_ACK. 20218 */ 20219 return (EINPROGRESS); 20220 bad: 20221 ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name)); 20222 /* 20223 * We don't have to check for possible removal from illgrp 20224 * as we have not yet inserted in illgrp. For groups 20225 * without names, this ipif is still not UP and hence 20226 * this could not have possibly had any influence in forming 20227 * groups. 20228 */ 20229 20230 freemsg(bind_mp); 20231 freemsg(unbind_mp); 20232 return (ENOMEM); 20233 } 20234 20235 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20; 20236 20237 /* 20238 * DLPI and ARP is up. 20239 * Create all the IREs associated with an interface bring up multicast. 20240 * Set the interface flag and finish other initialization 20241 * that potentially had to be differed to after DL_BIND_ACK. 20242 */ 20243 int 20244 ipif_up_done(ipif_t *ipif) 20245 { 20246 ire_t *ire_array[20]; 20247 ire_t **irep = ire_array; 20248 ire_t **irep1; 20249 ipaddr_t net_mask = 0; 20250 ipaddr_t subnet_mask, route_mask; 20251 ill_t *ill = ipif->ipif_ill; 20252 queue_t *stq; 20253 ipif_t *src_ipif; 20254 ipif_t *tmp_ipif; 20255 boolean_t flush_ire_cache = B_TRUE; 20256 int err = 0; 20257 phyint_t *phyi; 20258 ire_t **ipif_saved_irep = NULL; 20259 int ipif_saved_ire_cnt; 20260 int cnt; 20261 boolean_t src_ipif_held = B_FALSE; 20262 boolean_t ire_added = B_FALSE; 20263 boolean_t loopback = B_FALSE; 20264 ip_stack_t *ipst = ill->ill_ipst; 20265 20266 ip1dbg(("ipif_up_done(%s:%u)\n", 20267 ipif->ipif_ill->ill_name, ipif->ipif_id)); 20268 /* Check if this is a loopback interface */ 20269 if (ipif->ipif_ill->ill_wq == NULL) 20270 loopback = B_TRUE; 20271 20272 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 20273 /* 20274 * If all other interfaces for this ill are down or DEPRECATED, 20275 * or otherwise unsuitable for source address selection, remove 20276 * any IRE_CACHE entries for this ill to make sure source 20277 * address selection gets to take this new ipif into account. 20278 * No need to hold ill_lock while traversing the ipif list since 20279 * we are writer 20280 */ 20281 for (tmp_ipif = ill->ill_ipif; tmp_ipif; 20282 tmp_ipif = tmp_ipif->ipif_next) { 20283 if (((tmp_ipif->ipif_flags & 20284 (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) || 20285 !(tmp_ipif->ipif_flags & IPIF_UP)) || 20286 (tmp_ipif == ipif)) 20287 continue; 20288 /* first useable pre-existing interface */ 20289 flush_ire_cache = B_FALSE; 20290 break; 20291 } 20292 if (flush_ire_cache) 20293 ire_walk_ill_v4(MATCH_IRE_ILL_GROUP | MATCH_IRE_TYPE, 20294 IRE_CACHE, ill_ipif_cache_delete, (char *)ill, ill); 20295 20296 /* 20297 * Figure out which way the send-to queue should go. Only 20298 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER or IRE_LOOPBACK 20299 * should show up here. 20300 */ 20301 switch (ill->ill_net_type) { 20302 case IRE_IF_RESOLVER: 20303 stq = ill->ill_rq; 20304 break; 20305 case IRE_IF_NORESOLVER: 20306 case IRE_LOOPBACK: 20307 stq = ill->ill_wq; 20308 break; 20309 default: 20310 return (EINVAL); 20311 } 20312 20313 if (ill->ill_phyint->phyint_flags & PHYI_LOOPBACK) { 20314 /* 20315 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in 20316 * ipif_lookup_on_name(), but in the case of zones we can have 20317 * several loopback addresses on lo0. So all the interfaces with 20318 * loopback addresses need to be marked IRE_LOOPBACK. 20319 */ 20320 if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) == 20321 htonl(INADDR_LOOPBACK)) 20322 ipif->ipif_ire_type = IRE_LOOPBACK; 20323 else 20324 ipif->ipif_ire_type = IRE_LOCAL; 20325 } 20326 20327 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)) { 20328 /* 20329 * Can't use our source address. Select a different 20330 * source address for the IRE_INTERFACE and IRE_LOCAL 20331 */ 20332 src_ipif = ipif_select_source(ipif->ipif_ill, 20333 ipif->ipif_subnet, ipif->ipif_zoneid); 20334 if (src_ipif == NULL) 20335 src_ipif = ipif; /* Last resort */ 20336 else 20337 src_ipif_held = B_TRUE; 20338 } else { 20339 src_ipif = ipif; 20340 } 20341 20342 /* Create all the IREs associated with this interface */ 20343 if ((ipif->ipif_lcl_addr != INADDR_ANY) && 20344 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 20345 20346 /* 20347 * If we're on a labeled system then make sure that zone- 20348 * private addresses have proper remote host database entries. 20349 */ 20350 if (is_system_labeled() && 20351 ipif->ipif_ire_type != IRE_LOOPBACK && 20352 !tsol_check_interface_address(ipif)) 20353 return (EINVAL); 20354 20355 /* Register the source address for __sin6_src_id */ 20356 err = ip_srcid_insert(&ipif->ipif_v6lcl_addr, 20357 ipif->ipif_zoneid, ipst); 20358 if (err != 0) { 20359 ip0dbg(("ipif_up_done: srcid_insert %d\n", err)); 20360 return (err); 20361 } 20362 20363 /* If the interface address is set, create the local IRE. */ 20364 ip1dbg(("ipif_up_done: 0x%p creating IRE 0x%x for 0x%x\n", 20365 (void *)ipif, 20366 ipif->ipif_ire_type, 20367 ntohl(ipif->ipif_lcl_addr))); 20368 *irep++ = ire_create( 20369 (uchar_t *)&ipif->ipif_lcl_addr, /* dest address */ 20370 (uchar_t *)&ip_g_all_ones, /* mask */ 20371 (uchar_t *)&src_ipif->ipif_src_addr, /* source address */ 20372 NULL, /* no gateway */ 20373 NULL, 20374 &ip_loopback_mtuplus, /* max frag size */ 20375 NULL, 20376 ipif->ipif_rq, /* recv-from queue */ 20377 NULL, /* no send-to queue */ 20378 ipif->ipif_ire_type, /* LOCAL or LOOPBACK */ 20379 NULL, 20380 ipif, 20381 NULL, 20382 0, 20383 0, 20384 0, 20385 (ipif->ipif_flags & IPIF_PRIVATE) ? 20386 RTF_PRIVATE : 0, 20387 &ire_uinfo_null, 20388 NULL, 20389 NULL, 20390 ipst); 20391 } else { 20392 ip1dbg(( 20393 "ipif_up_done: not creating IRE %d for 0x%x: flags 0x%x\n", 20394 ipif->ipif_ire_type, 20395 ntohl(ipif->ipif_lcl_addr), 20396 (uint_t)ipif->ipif_flags)); 20397 } 20398 if ((ipif->ipif_lcl_addr != INADDR_ANY) && 20399 !(ipif->ipif_flags & IPIF_NOLOCAL)) { 20400 net_mask = ip_net_mask(ipif->ipif_lcl_addr); 20401 } else { 20402 net_mask = htonl(IN_CLASSA_NET); /* fallback */ 20403 } 20404 20405 subnet_mask = ipif->ipif_net_mask; 20406 20407 /* 20408 * If mask was not specified, use natural netmask of 20409 * interface address. Also, store this mask back into the 20410 * ipif struct. 20411 */ 20412 if (subnet_mask == 0) { 20413 subnet_mask = net_mask; 20414 V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask); 20415 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask, 20416 ipif->ipif_v6subnet); 20417 } 20418 20419 /* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */ 20420 if (stq != NULL && !(ipif->ipif_flags & IPIF_NOXMIT) && 20421 ipif->ipif_subnet != INADDR_ANY) { 20422 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */ 20423 20424 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 20425 route_mask = IP_HOST_MASK; 20426 } else { 20427 route_mask = subnet_mask; 20428 } 20429 20430 ip1dbg(("ipif_up_done: ipif 0x%p ill 0x%p " 20431 "creating if IRE ill_net_type 0x%x for 0x%x\n", 20432 (void *)ipif, (void *)ill, 20433 ill->ill_net_type, 20434 ntohl(ipif->ipif_subnet))); 20435 *irep++ = ire_create( 20436 (uchar_t *)&ipif->ipif_subnet, /* dest address */ 20437 (uchar_t *)&route_mask, /* mask */ 20438 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */ 20439 NULL, /* no gateway */ 20440 NULL, 20441 &ipif->ipif_mtu, /* max frag */ 20442 NULL, 20443 NULL, /* no recv queue */ 20444 stq, /* send-to queue */ 20445 ill->ill_net_type, /* IF_[NO]RESOLVER */ 20446 ill->ill_resolver_mp, /* xmit header */ 20447 ipif, 20448 NULL, 20449 0, 20450 0, 20451 0, 20452 (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE: 0, 20453 &ire_uinfo_null, 20454 NULL, 20455 NULL, 20456 ipst); 20457 } 20458 20459 /* 20460 * If the interface address is set, create the broadcast IREs. 20461 * 20462 * ire_create_bcast checks if the proposed new IRE matches 20463 * any existing IRE's with the same physical interface (ILL). 20464 * This should get rid of duplicates. 20465 * ire_create_bcast also check IPIF_NOXMIT and does not create 20466 * any broadcast ires. 20467 */ 20468 if ((ipif->ipif_subnet != INADDR_ANY) && 20469 (ipif->ipif_flags & IPIF_BROADCAST)) { 20470 ipaddr_t addr; 20471 20472 ip1dbg(("ipif_up_done: creating broadcast IRE\n")); 20473 irep = ire_check_and_create_bcast(ipif, 0, irep, 20474 (MATCH_IRE_TYPE | MATCH_IRE_ILL)); 20475 irep = ire_check_and_create_bcast(ipif, INADDR_BROADCAST, irep, 20476 (MATCH_IRE_TYPE | MATCH_IRE_ILL)); 20477 20478 /* 20479 * For backward compatibility, we need to create net 20480 * broadcast ire's based on the old "IP address class 20481 * system." The reason is that some old machines only 20482 * respond to these class derived net broadcast. 20483 * 20484 * But we should not create these net broadcast ire's if 20485 * the subnet_mask is shorter than the IP address class based 20486 * derived netmask. Otherwise, we may create a net 20487 * broadcast address which is the same as an IP address 20488 * on the subnet. Then TCP will refuse to talk to that 20489 * address. 20490 * 20491 * Nor do we need IRE_BROADCAST ire's for the interface 20492 * with the netmask as 0xFFFFFFFF, as IRE_LOCAL for that 20493 * interface is already created. Creating these broadcast 20494 * ire's will only create confusion as the "addr" is going 20495 * to be same as that of the IP address of the interface. 20496 */ 20497 if (net_mask < subnet_mask) { 20498 addr = net_mask & ipif->ipif_subnet; 20499 irep = ire_check_and_create_bcast(ipif, addr, irep, 20500 (MATCH_IRE_TYPE | MATCH_IRE_ILL)); 20501 irep = ire_check_and_create_bcast(ipif, 20502 ~net_mask | addr, irep, 20503 (MATCH_IRE_TYPE | MATCH_IRE_ILL)); 20504 } 20505 20506 if (subnet_mask != 0xFFFFFFFF) { 20507 addr = ipif->ipif_subnet; 20508 irep = ire_check_and_create_bcast(ipif, addr, irep, 20509 (MATCH_IRE_TYPE | MATCH_IRE_ILL)); 20510 irep = ire_check_and_create_bcast(ipif, 20511 ~subnet_mask|addr, irep, 20512 (MATCH_IRE_TYPE | MATCH_IRE_ILL)); 20513 } 20514 } 20515 20516 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock)); 20517 20518 /* If an earlier ire_create failed, get out now */ 20519 for (irep1 = irep; irep1 > ire_array; ) { 20520 irep1--; 20521 if (*irep1 == NULL) { 20522 ip1dbg(("ipif_up_done: NULL ire found in ire_array\n")); 20523 err = ENOMEM; 20524 goto bad; 20525 } 20526 } 20527 20528 /* 20529 * Need to atomically check for ip_addr_availablity_check 20530 * under ip_addr_avail_lock, and if it fails got bad, and remove 20531 * from group also.The ill_g_lock is grabbed as reader 20532 * just to make sure no new ills or new ipifs are being added 20533 * to the system while we are checking the uniqueness of addresses. 20534 */ 20535 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 20536 mutex_enter(&ipst->ips_ip_addr_avail_lock); 20537 /* Mark it up, and increment counters. */ 20538 ipif->ipif_flags |= IPIF_UP; 20539 ill->ill_ipif_up_count++; 20540 err = ip_addr_availability_check(ipif); 20541 mutex_exit(&ipst->ips_ip_addr_avail_lock); 20542 rw_exit(&ipst->ips_ill_g_lock); 20543 20544 if (err != 0) { 20545 /* 20546 * Our address may already be up on the same ill. In this case, 20547 * the ARP entry for our ipif replaced the one for the other 20548 * ipif. So we don't want to delete it (otherwise the other ipif 20549 * would be unable to send packets). 20550 * ip_addr_availability_check() identifies this case for us and 20551 * returns EADDRINUSE; we need to turn it into EADDRNOTAVAIL 20552 * which is the expected error code. 20553 */ 20554 if (err == EADDRINUSE) { 20555 freemsg(ipif->ipif_arp_del_mp); 20556 ipif->ipif_arp_del_mp = NULL; 20557 err = EADDRNOTAVAIL; 20558 } 20559 ill->ill_ipif_up_count--; 20560 ipif->ipif_flags &= ~IPIF_UP; 20561 goto bad; 20562 } 20563 20564 /* 20565 * Add in all newly created IREs. ire_create_bcast() has 20566 * already checked for duplicates of the IRE_BROADCAST type. 20567 * We want to add before we call ifgrp_insert which wants 20568 * to know whether IRE_IF_RESOLVER exists or not. 20569 * 20570 * NOTE : We refrele the ire though we may branch to "bad" 20571 * later on where we do ire_delete. This is okay 20572 * because nobody can delete it as we are running 20573 * exclusively. 20574 */ 20575 for (irep1 = irep; irep1 > ire_array; ) { 20576 irep1--; 20577 ASSERT(!MUTEX_HELD(&((*irep1)->ire_ipif->ipif_ill->ill_lock))); 20578 /* 20579 * refheld by ire_add. refele towards the end of the func 20580 */ 20581 (void) ire_add(irep1, NULL, NULL, NULL, B_FALSE); 20582 } 20583 ire_added = B_TRUE; 20584 /* 20585 * Form groups if possible. 20586 * 20587 * If we are supposed to be in a ill_group with a name, insert it 20588 * now as we know that at least one ipif is UP. Otherwise form 20589 * nameless groups. 20590 * 20591 * If ip_enable_group_ifs is set and ipif address is not 0, insert 20592 * this ipif into the appropriate interface group, or create a 20593 * new one. If this is already in a nameless group, we try to form 20594 * a bigger group looking at other ills potentially sharing this 20595 * ipif's prefix. 20596 */ 20597 phyi = ill->ill_phyint; 20598 if (phyi->phyint_groupname_len != 0) { 20599 ASSERT(phyi->phyint_groupname != NULL); 20600 if (ill->ill_ipif_up_count == 1) { 20601 ASSERT(ill->ill_group == NULL); 20602 err = illgrp_insert(&ipst->ips_illgrp_head_v4, ill, 20603 phyi->phyint_groupname, NULL, B_TRUE); 20604 if (err != 0) { 20605 ip1dbg(("ipif_up_done: illgrp allocation " 20606 "failed, error %d\n", err)); 20607 goto bad; 20608 } 20609 } 20610 ASSERT(ill->ill_group != NULL); 20611 } 20612 20613 /* 20614 * When this is part of group, we need to make sure that 20615 * any broadcast ires created because of this ipif coming 20616 * UP gets marked/cleared with IRE_MARK_NORECV appropriately 20617 * so that we don't receive duplicate broadcast packets. 20618 */ 20619 if (ill->ill_group != NULL && ill->ill_ipif_up_count != 0) 20620 ipif_renominate_bcast(ipif); 20621 20622 /* Recover any additional IRE_IF_[NO]RESOLVER entries for this ipif */ 20623 ipif_saved_ire_cnt = ipif->ipif_saved_ire_cnt; 20624 ipif_saved_irep = ipif_recover_ire(ipif); 20625 20626 if (!loopback) { 20627 /* 20628 * If the broadcast address has been set, make sure it makes 20629 * sense based on the interface address. 20630 * Only match on ill since we are sharing broadcast addresses. 20631 */ 20632 if ((ipif->ipif_brd_addr != INADDR_ANY) && 20633 (ipif->ipif_flags & IPIF_BROADCAST)) { 20634 ire_t *ire; 20635 20636 ire = ire_ctable_lookup(ipif->ipif_brd_addr, 0, 20637 IRE_BROADCAST, ipif, ALL_ZONES, 20638 NULL, (MATCH_IRE_TYPE | MATCH_IRE_ILL), ipst); 20639 20640 if (ire == NULL) { 20641 /* 20642 * If there isn't a matching broadcast IRE, 20643 * revert to the default for this netmask. 20644 */ 20645 ipif->ipif_v6brd_addr = ipv6_all_zeros; 20646 mutex_enter(&ipif->ipif_ill->ill_lock); 20647 ipif_set_default(ipif); 20648 mutex_exit(&ipif->ipif_ill->ill_lock); 20649 } else { 20650 ire_refrele(ire); 20651 } 20652 } 20653 20654 } 20655 20656 /* This is the first interface on this ill */ 20657 if (ipif->ipif_ipif_up_count == 1 && !loopback) { 20658 /* 20659 * Need to recover all multicast memberships in the driver. 20660 * This had to be deferred until we had attached. 20661 */ 20662 ill_recover_multicast(ill); 20663 } 20664 /* Join the allhosts multicast address */ 20665 ipif_multicast_up(ipif); 20666 20667 if (!loopback) { 20668 /* 20669 * See whether anybody else would benefit from the 20670 * new ipif that we added. We call this always rather 20671 * than while adding a non-IPIF_NOLOCAL/DEPRECATED/ANYCAST 20672 * ipif is for the benefit of illgrp_insert (done above) 20673 * which does not do source address selection as it does 20674 * not want to re-create interface routes that we are 20675 * having reference to it here. 20676 */ 20677 ill_update_source_selection(ill); 20678 } 20679 20680 for (irep1 = irep; irep1 > ire_array; ) { 20681 irep1--; 20682 if (*irep1 != NULL) { 20683 /* was held in ire_add */ 20684 ire_refrele(*irep1); 20685 } 20686 } 20687 20688 cnt = ipif_saved_ire_cnt; 20689 for (irep1 = ipif_saved_irep; cnt > 0; irep1++, cnt--) { 20690 if (*irep1 != NULL) { 20691 /* was held in ire_add */ 20692 ire_refrele(*irep1); 20693 } 20694 } 20695 20696 if (!loopback && ipif->ipif_addr_ready) { 20697 /* Broadcast an address mask reply. */ 20698 ipif_mask_reply(ipif); 20699 } 20700 if (ipif_saved_irep != NULL) { 20701 kmem_free(ipif_saved_irep, 20702 ipif_saved_ire_cnt * sizeof (ire_t *)); 20703 } 20704 if (src_ipif_held) 20705 ipif_refrele(src_ipif); 20706 20707 /* 20708 * This had to be deferred until we had bound. Tell routing sockets and 20709 * others that this interface is up if it looks like the address has 20710 * been validated. Otherwise, if it isn't ready yet, wait for 20711 * duplicate address detection to do its thing. 20712 */ 20713 if (ipif->ipif_addr_ready) { 20714 ip_rts_ifmsg(ipif); 20715 ip_rts_newaddrmsg(RTM_ADD, 0, ipif); 20716 /* Let SCTP update the status for this ipif */ 20717 sctp_update_ipif(ipif, SCTP_IPIF_UP); 20718 } 20719 return (0); 20720 20721 bad: 20722 ip1dbg(("ipif_up_done: FAILED \n")); 20723 /* 20724 * We don't have to bother removing from ill groups because 20725 * 20726 * 1) For groups with names, we insert only when the first ipif 20727 * comes up. In that case if it fails, it will not be in any 20728 * group. So, we need not try to remove for that case. 20729 * 20730 * 2) For groups without names, either we tried to insert ipif_ill 20731 * in a group as singleton or found some other group to become 20732 * a bigger group. For the former, if it fails we don't have 20733 * anything to do as ipif_ill is not in the group and for the 20734 * latter, there are no failures in illgrp_insert/illgrp_delete 20735 * (ENOMEM can't occur for this. Check ifgrp_insert). 20736 */ 20737 while (irep > ire_array) { 20738 irep--; 20739 if (*irep != NULL) { 20740 ire_delete(*irep); 20741 if (ire_added) 20742 ire_refrele(*irep); 20743 } 20744 } 20745 (void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst); 20746 20747 if (ipif_saved_irep != NULL) { 20748 kmem_free(ipif_saved_irep, 20749 ipif_saved_ire_cnt * sizeof (ire_t *)); 20750 } 20751 if (src_ipif_held) 20752 ipif_refrele(src_ipif); 20753 20754 ipif_arp_down(ipif); 20755 return (err); 20756 } 20757 20758 /* 20759 * Turn off the ARP with the ILLF_NOARP flag. 20760 */ 20761 static int 20762 ill_arp_off(ill_t *ill) 20763 { 20764 mblk_t *arp_off_mp = NULL; 20765 mblk_t *arp_on_mp = NULL; 20766 20767 ip1dbg(("ill_arp_off(%s)\n", ill->ill_name)); 20768 20769 ASSERT(IAM_WRITER_ILL(ill)); 20770 ASSERT(ill->ill_net_type == IRE_IF_RESOLVER); 20771 20772 /* 20773 * If the on message is still around we've already done 20774 * an arp_off without doing an arp_on thus there is no 20775 * work needed. 20776 */ 20777 if (ill->ill_arp_on_mp != NULL) 20778 return (0); 20779 20780 /* 20781 * Allocate an ARP on message (to be saved) and an ARP off message 20782 */ 20783 arp_off_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aroff_template, 0); 20784 if (!arp_off_mp) 20785 return (ENOMEM); 20786 20787 arp_on_mp = ill_arp_alloc(ill, (uchar_t *)&ip_aron_template, 0); 20788 if (!arp_on_mp) 20789 goto failed; 20790 20791 ASSERT(ill->ill_arp_on_mp == NULL); 20792 ill->ill_arp_on_mp = arp_on_mp; 20793 20794 /* Send an AR_INTERFACE_OFF request */ 20795 putnext(ill->ill_rq, arp_off_mp); 20796 return (0); 20797 failed: 20798 20799 if (arp_off_mp) 20800 freemsg(arp_off_mp); 20801 return (ENOMEM); 20802 } 20803 20804 /* 20805 * Turn on ARP by turning off the ILLF_NOARP flag. 20806 */ 20807 static int 20808 ill_arp_on(ill_t *ill) 20809 { 20810 mblk_t *mp; 20811 20812 ip1dbg(("ipif_arp_on(%s)\n", ill->ill_name)); 20813 20814 ASSERT(ill->ill_net_type == IRE_IF_RESOLVER); 20815 20816 ASSERT(IAM_WRITER_ILL(ill)); 20817 /* 20818 * Send an AR_INTERFACE_ON request if we have already done 20819 * an arp_off (which allocated the message). 20820 */ 20821 if (ill->ill_arp_on_mp != NULL) { 20822 mp = ill->ill_arp_on_mp; 20823 ill->ill_arp_on_mp = NULL; 20824 putnext(ill->ill_rq, mp); 20825 } 20826 return (0); 20827 } 20828 20829 /* 20830 * Called after either deleting ill from the group or when setting 20831 * FAILED or STANDBY on the interface. 20832 */ 20833 static void 20834 illgrp_reset_schednext(ill_t *ill) 20835 { 20836 ill_group_t *illgrp; 20837 ill_t *save_ill; 20838 20839 ASSERT(IAM_WRITER_ILL(ill)); 20840 /* 20841 * When called from illgrp_delete, ill_group will be non-NULL. 20842 * But when called from ip_sioctl_flags, it could be NULL if 20843 * somebody is setting FAILED/INACTIVE on some interface which 20844 * is not part of a group. 20845 */ 20846 illgrp = ill->ill_group; 20847 if (illgrp == NULL) 20848 return; 20849 if (illgrp->illgrp_ill_schednext != ill) 20850 return; 20851 20852 illgrp->illgrp_ill_schednext = NULL; 20853 save_ill = ill; 20854 /* 20855 * Choose a good ill to be the next one for 20856 * outbound traffic. As the flags FAILED/STANDBY is 20857 * not yet marked when called from ip_sioctl_flags, 20858 * we check for ill separately. 20859 */ 20860 for (ill = illgrp->illgrp_ill; ill != NULL; 20861 ill = ill->ill_group_next) { 20862 if ((ill != save_ill) && 20863 !(ill->ill_phyint->phyint_flags & 20864 (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE))) { 20865 illgrp->illgrp_ill_schednext = ill; 20866 return; 20867 } 20868 } 20869 } 20870 20871 /* 20872 * Given an ill, find the next ill in the group to be scheduled. 20873 * (This should be called by ip_newroute() before ire_create().) 20874 * The passed in ill may be pulled out of the group, after we have picked 20875 * up a different outgoing ill from the same group. However ire add will 20876 * atomically check this. 20877 */ 20878 ill_t * 20879 illgrp_scheduler(ill_t *ill) 20880 { 20881 ill_t *retill; 20882 ill_group_t *illgrp; 20883 int illcnt; 20884 int i; 20885 uint64_t flags; 20886 ip_stack_t *ipst = ill->ill_ipst; 20887 20888 /* 20889 * We don't use a lock to check for the ill_group. If this ill 20890 * is currently being inserted we may end up just returning this 20891 * ill itself. That is ok. 20892 */ 20893 if (ill->ill_group == NULL) { 20894 ill_refhold(ill); 20895 return (ill); 20896 } 20897 20898 /* 20899 * Grab the ill_g_lock as reader to make sure we are dealing with 20900 * a set of stable ills. No ill can be added or deleted or change 20901 * group while we hold the reader lock. 20902 */ 20903 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 20904 if ((illgrp = ill->ill_group) == NULL) { 20905 rw_exit(&ipst->ips_ill_g_lock); 20906 ill_refhold(ill); 20907 return (ill); 20908 } 20909 20910 illcnt = illgrp->illgrp_ill_count; 20911 mutex_enter(&illgrp->illgrp_lock); 20912 retill = illgrp->illgrp_ill_schednext; 20913 20914 if (retill == NULL) 20915 retill = illgrp->illgrp_ill; 20916 20917 /* 20918 * We do a circular search beginning at illgrp_ill_schednext 20919 * or illgrp_ill. We don't check the flags against the ill lock 20920 * since it can change anytime. The ire creation will be atomic 20921 * and will fail if the ill is FAILED or OFFLINE. 20922 */ 20923 for (i = 0; i < illcnt; i++) { 20924 flags = retill->ill_phyint->phyint_flags; 20925 20926 if (!(flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) && 20927 ILL_CAN_LOOKUP(retill)) { 20928 illgrp->illgrp_ill_schednext = retill->ill_group_next; 20929 ill_refhold(retill); 20930 break; 20931 } 20932 retill = retill->ill_group_next; 20933 if (retill == NULL) 20934 retill = illgrp->illgrp_ill; 20935 } 20936 mutex_exit(&illgrp->illgrp_lock); 20937 rw_exit(&ipst->ips_ill_g_lock); 20938 20939 return (i == illcnt ? NULL : retill); 20940 } 20941 20942 /* 20943 * Checks for availbility of a usable source address (if there is one) when the 20944 * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note 20945 * this selection is done regardless of the destination. 20946 */ 20947 boolean_t 20948 ipif_usesrc_avail(ill_t *ill, zoneid_t zoneid) 20949 { 20950 uint_t ifindex; 20951 ipif_t *ipif = NULL; 20952 ill_t *uill; 20953 boolean_t isv6; 20954 ip_stack_t *ipst = ill->ill_ipst; 20955 20956 ASSERT(ill != NULL); 20957 20958 isv6 = ill->ill_isv6; 20959 ifindex = ill->ill_usesrc_ifindex; 20960 if (ifindex != 0) { 20961 uill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, 20962 NULL, ipst); 20963 if (uill == NULL) 20964 return (NULL); 20965 mutex_enter(&uill->ill_lock); 20966 for (ipif = uill->ill_ipif; ipif != NULL; 20967 ipif = ipif->ipif_next) { 20968 if (!IPIF_CAN_LOOKUP(ipif)) 20969 continue; 20970 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) 20971 continue; 20972 if (!(ipif->ipif_flags & IPIF_UP)) 20973 continue; 20974 if (ipif->ipif_zoneid != zoneid) 20975 continue; 20976 if ((isv6 && 20977 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) || 20978 (ipif->ipif_lcl_addr == INADDR_ANY)) 20979 continue; 20980 mutex_exit(&uill->ill_lock); 20981 ill_refrele(uill); 20982 return (B_TRUE); 20983 } 20984 mutex_exit(&uill->ill_lock); 20985 ill_refrele(uill); 20986 } 20987 return (B_FALSE); 20988 } 20989 20990 /* 20991 * Determine the best source address given a destination address and an ill. 20992 * Prefers non-deprecated over deprecated but will return a deprecated 20993 * address if there is no other choice. If there is a usable source address 20994 * on the interface pointed to by ill_usesrc_ifindex then that is given 20995 * first preference. 20996 * 20997 * Returns NULL if there is no suitable source address for the ill. 20998 * This only occurs when there is no valid source address for the ill. 20999 */ 21000 ipif_t * 21001 ipif_select_source(ill_t *ill, ipaddr_t dst, zoneid_t zoneid) 21002 { 21003 ipif_t *ipif; 21004 ipif_t *ipif_dep = NULL; /* Fallback to deprecated */ 21005 ipif_t *ipif_arr[MAX_IPIF_SELECT_SOURCE]; 21006 int index = 0; 21007 boolean_t wrapped = B_FALSE; 21008 boolean_t same_subnet_only = B_FALSE; 21009 boolean_t ipif_same_found, ipif_other_found; 21010 boolean_t specific_found; 21011 ill_t *till, *usill = NULL; 21012 tsol_tpc_t *src_rhtp, *dst_rhtp; 21013 ip_stack_t *ipst = ill->ill_ipst; 21014 21015 if (ill->ill_usesrc_ifindex != 0) { 21016 usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex, 21017 B_FALSE, NULL, NULL, NULL, NULL, ipst); 21018 if (usill != NULL) 21019 ill = usill; /* Select source from usesrc ILL */ 21020 else 21021 return (NULL); 21022 } 21023 21024 /* 21025 * If we're dealing with an unlabeled destination on a labeled system, 21026 * make sure that we ignore source addresses that are incompatible with 21027 * the destination's default label. That destination's default label 21028 * must dominate the minimum label on the source address. 21029 */ 21030 dst_rhtp = NULL; 21031 if (is_system_labeled()) { 21032 dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE); 21033 if (dst_rhtp == NULL) 21034 return (NULL); 21035 if (dst_rhtp->tpc_tp.host_type != UNLABELED) { 21036 TPC_RELE(dst_rhtp); 21037 dst_rhtp = NULL; 21038 } 21039 } 21040 21041 /* 21042 * Holds the ill_g_lock as reader. This makes sure that no ipif/ill 21043 * can be deleted. But an ipif/ill can get CONDEMNED any time. 21044 * After selecting the right ipif, under ill_lock make sure ipif is 21045 * not condemned, and increment refcnt. If ipif is CONDEMNED, 21046 * we retry. Inside the loop we still need to check for CONDEMNED, 21047 * but not under a lock. 21048 */ 21049 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21050 21051 retry: 21052 till = ill; 21053 ipif_arr[0] = NULL; 21054 21055 if (till->ill_group != NULL) 21056 till = till->ill_group->illgrp_ill; 21057 21058 /* 21059 * Choose one good source address from each ill across the group. 21060 * If possible choose a source address in the same subnet as 21061 * the destination address. 21062 * 21063 * We don't check for PHYI_FAILED or PHYI_INACTIVE or PHYI_OFFLINE 21064 * This is okay because of the following. 21065 * 21066 * If PHYI_FAILED is set and we still have non-deprecated 21067 * addresses, it means the addresses have not yet been 21068 * failed over to a different interface. We potentially 21069 * select them to create IRE_CACHES, which will be later 21070 * flushed when the addresses move over. 21071 * 21072 * If PHYI_INACTIVE is set and we still have non-deprecated 21073 * addresses, it means either the user has configured them 21074 * or PHYI_INACTIVE has not been cleared after the addresses 21075 * been moved over. For the former, in.mpathd does a failover 21076 * when the interface becomes INACTIVE and hence we should 21077 * not find them. Once INACTIVE is set, we don't allow them 21078 * to create logical interfaces anymore. For the latter, a 21079 * flush will happen when INACTIVE is cleared which will 21080 * flush the IRE_CACHES. 21081 * 21082 * If PHYI_OFFLINE is set, all the addresses will be failed 21083 * over soon. We potentially select them to create IRE_CACHEs, 21084 * which will be later flushed when the addresses move over. 21085 * 21086 * NOTE : As ipif_select_source is called to borrow source address 21087 * for an ipif that is part of a group, source address selection 21088 * will be re-done whenever the group changes i.e either an 21089 * insertion/deletion in the group. 21090 * 21091 * Fill ipif_arr[] with source addresses, using these rules: 21092 * 21093 * 1. At most one source address from a given ill ends up 21094 * in ipif_arr[] -- that is, at most one of the ipif's 21095 * associated with a given ill ends up in ipif_arr[]. 21096 * 21097 * 2. If there is at least one non-deprecated ipif in the 21098 * IPMP group with a source address on the same subnet as 21099 * our destination, then fill ipif_arr[] only with 21100 * source addresses on the same subnet as our destination. 21101 * Note that because of (1), only the first 21102 * non-deprecated ipif found with a source address 21103 * matching the destination ends up in ipif_arr[]. 21104 * 21105 * 3. Otherwise, fill ipif_arr[] with non-deprecated source 21106 * addresses not in the same subnet as our destination. 21107 * Again, because of (1), only the first off-subnet source 21108 * address will be chosen. 21109 * 21110 * 4. If there are no non-deprecated ipifs, then just use 21111 * the source address associated with the last deprecated 21112 * one we find that happens to be on the same subnet, 21113 * otherwise the first one not in the same subnet. 21114 */ 21115 specific_found = B_FALSE; 21116 for (; till != NULL; till = till->ill_group_next) { 21117 ipif_same_found = B_FALSE; 21118 ipif_other_found = B_FALSE; 21119 for (ipif = till->ill_ipif; ipif != NULL; 21120 ipif = ipif->ipif_next) { 21121 if (!IPIF_CAN_LOOKUP(ipif)) 21122 continue; 21123 /* Always skip NOLOCAL and ANYCAST interfaces */ 21124 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST)) 21125 continue; 21126 if (!(ipif->ipif_flags & IPIF_UP) || 21127 !ipif->ipif_addr_ready) 21128 continue; 21129 if (ipif->ipif_zoneid != zoneid && 21130 ipif->ipif_zoneid != ALL_ZONES) 21131 continue; 21132 /* 21133 * Interfaces with 0.0.0.0 address are allowed to be UP, 21134 * but are not valid as source addresses. 21135 */ 21136 if (ipif->ipif_lcl_addr == INADDR_ANY) 21137 continue; 21138 21139 /* 21140 * Check compatibility of local address for 21141 * destination's default label if we're on a labeled 21142 * system. Incompatible addresses can't be used at 21143 * all. 21144 */ 21145 if (dst_rhtp != NULL) { 21146 boolean_t incompat; 21147 21148 src_rhtp = find_tpc(&ipif->ipif_lcl_addr, 21149 IPV4_VERSION, B_FALSE); 21150 if (src_rhtp == NULL) 21151 continue; 21152 incompat = 21153 src_rhtp->tpc_tp.host_type != SUN_CIPSO || 21154 src_rhtp->tpc_tp.tp_doi != 21155 dst_rhtp->tpc_tp.tp_doi || 21156 (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label, 21157 &src_rhtp->tpc_tp.tp_sl_range_cipso) && 21158 !blinlset(&dst_rhtp->tpc_tp.tp_def_label, 21159 src_rhtp->tpc_tp.tp_sl_set_cipso)); 21160 TPC_RELE(src_rhtp); 21161 if (incompat) 21162 continue; 21163 } 21164 21165 /* 21166 * We prefer not to use all all-zones addresses, if we 21167 * can avoid it, as they pose problems with unlabeled 21168 * destinations. 21169 */ 21170 if (ipif->ipif_zoneid != ALL_ZONES) { 21171 if (!specific_found && 21172 (!same_subnet_only || 21173 (ipif->ipif_net_mask & dst) == 21174 ipif->ipif_subnet)) { 21175 index = 0; 21176 specific_found = B_TRUE; 21177 ipif_other_found = B_FALSE; 21178 } 21179 } else { 21180 if (specific_found) 21181 continue; 21182 } 21183 if (ipif->ipif_flags & IPIF_DEPRECATED) { 21184 if (ipif_dep == NULL || 21185 (ipif->ipif_net_mask & dst) == 21186 ipif->ipif_subnet) 21187 ipif_dep = ipif; 21188 continue; 21189 } 21190 if ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet) { 21191 /* found a source address in the same subnet */ 21192 if (!same_subnet_only) { 21193 same_subnet_only = B_TRUE; 21194 index = 0; 21195 } 21196 ipif_same_found = B_TRUE; 21197 } else { 21198 if (same_subnet_only || ipif_other_found) 21199 continue; 21200 ipif_other_found = B_TRUE; 21201 } 21202 ipif_arr[index++] = ipif; 21203 if (index == MAX_IPIF_SELECT_SOURCE) { 21204 wrapped = B_TRUE; 21205 index = 0; 21206 } 21207 if (ipif_same_found) 21208 break; 21209 } 21210 } 21211 21212 if (ipif_arr[0] == NULL) { 21213 ipif = ipif_dep; 21214 } else { 21215 if (wrapped) 21216 index = MAX_IPIF_SELECT_SOURCE; 21217 ipif = ipif_arr[ipif_rand(ipst) % index]; 21218 ASSERT(ipif != NULL); 21219 } 21220 21221 if (ipif != NULL) { 21222 mutex_enter(&ipif->ipif_ill->ill_lock); 21223 if (!IPIF_CAN_LOOKUP(ipif)) { 21224 mutex_exit(&ipif->ipif_ill->ill_lock); 21225 goto retry; 21226 } 21227 ipif_refhold_locked(ipif); 21228 mutex_exit(&ipif->ipif_ill->ill_lock); 21229 } 21230 21231 rw_exit(&ipst->ips_ill_g_lock); 21232 if (usill != NULL) 21233 ill_refrele(usill); 21234 if (dst_rhtp != NULL) 21235 TPC_RELE(dst_rhtp); 21236 21237 #ifdef DEBUG 21238 if (ipif == NULL) { 21239 char buf1[INET6_ADDRSTRLEN]; 21240 21241 ip1dbg(("ipif_select_source(%s, %s) -> NULL\n", 21242 ill->ill_name, 21243 inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)))); 21244 } else { 21245 char buf1[INET6_ADDRSTRLEN]; 21246 char buf2[INET6_ADDRSTRLEN]; 21247 21248 ip1dbg(("ipif_select_source(%s, %s) -> %s\n", 21249 ipif->ipif_ill->ill_name, 21250 inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)), 21251 inet_ntop(AF_INET, &ipif->ipif_lcl_addr, 21252 buf2, sizeof (buf2)))); 21253 } 21254 #endif /* DEBUG */ 21255 return (ipif); 21256 } 21257 21258 21259 /* 21260 * If old_ipif is not NULL, see if ipif was derived from old 21261 * ipif and if so, recreate the interface route by re-doing 21262 * source address selection. This happens when ipif_down -> 21263 * ipif_update_other_ipifs calls us. 21264 * 21265 * If old_ipif is NULL, just redo the source address selection 21266 * if needed. This happens when illgrp_insert or ipif_up_done 21267 * calls us. 21268 */ 21269 static void 21270 ipif_recreate_interface_routes(ipif_t *old_ipif, ipif_t *ipif) 21271 { 21272 ire_t *ire; 21273 ire_t *ipif_ire; 21274 queue_t *stq; 21275 ipif_t *nipif; 21276 ill_t *ill; 21277 boolean_t need_rele = B_FALSE; 21278 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 21279 21280 ASSERT(old_ipif == NULL || IAM_WRITER_IPIF(old_ipif)); 21281 ASSERT(IAM_WRITER_IPIF(ipif)); 21282 21283 ill = ipif->ipif_ill; 21284 if (!(ipif->ipif_flags & 21285 (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED))) { 21286 /* 21287 * Can't possibly have borrowed the source 21288 * from old_ipif. 21289 */ 21290 return; 21291 } 21292 21293 /* 21294 * Is there any work to be done? No work if the address 21295 * is INADDR_ANY, loopback or NOLOCAL or ANYCAST ( 21296 * ipif_select_source() does not borrow addresses from 21297 * NOLOCAL and ANYCAST interfaces). 21298 */ 21299 if ((old_ipif != NULL) && 21300 ((old_ipif->ipif_lcl_addr == INADDR_ANY) || 21301 (old_ipif->ipif_ill->ill_wq == NULL) || 21302 (old_ipif->ipif_flags & 21303 (IPIF_NOLOCAL|IPIF_ANYCAST)))) { 21304 return; 21305 } 21306 21307 /* 21308 * Perform the same checks as when creating the 21309 * IRE_INTERFACE in ipif_up_done. 21310 */ 21311 if (!(ipif->ipif_flags & IPIF_UP)) 21312 return; 21313 21314 if ((ipif->ipif_flags & IPIF_NOXMIT) || 21315 (ipif->ipif_subnet == INADDR_ANY)) 21316 return; 21317 21318 ipif_ire = ipif_to_ire(ipif); 21319 if (ipif_ire == NULL) 21320 return; 21321 21322 /* 21323 * We know that ipif uses some other source for its 21324 * IRE_INTERFACE. Is it using the source of this 21325 * old_ipif? 21326 */ 21327 if (old_ipif != NULL && 21328 old_ipif->ipif_lcl_addr != ipif_ire->ire_src_addr) { 21329 ire_refrele(ipif_ire); 21330 return; 21331 } 21332 if (ip_debug > 2) { 21333 /* ip1dbg */ 21334 pr_addr_dbg("ipif_recreate_interface_routes: deleting IRE for" 21335 " src %s\n", AF_INET, &ipif_ire->ire_src_addr); 21336 } 21337 21338 stq = ipif_ire->ire_stq; 21339 21340 /* 21341 * Can't use our source address. Select a different 21342 * source address for the IRE_INTERFACE. 21343 */ 21344 nipif = ipif_select_source(ill, ipif->ipif_subnet, ipif->ipif_zoneid); 21345 if (nipif == NULL) { 21346 /* Last resort - all ipif's have IPIF_NOLOCAL */ 21347 nipif = ipif; 21348 } else { 21349 need_rele = B_TRUE; 21350 } 21351 21352 ire = ire_create( 21353 (uchar_t *)&ipif->ipif_subnet, /* dest pref */ 21354 (uchar_t *)&ipif->ipif_net_mask, /* mask */ 21355 (uchar_t *)&nipif->ipif_src_addr, /* src addr */ 21356 NULL, /* no gateway */ 21357 NULL, 21358 &ipif->ipif_mtu, /* max frag */ 21359 NULL, /* fast path header */ 21360 NULL, /* no recv from queue */ 21361 stq, /* send-to queue */ 21362 ill->ill_net_type, /* IF_[NO]RESOLVER */ 21363 ill->ill_resolver_mp, /* xmit header */ 21364 ipif, 21365 NULL, 21366 0, 21367 0, 21368 0, 21369 0, 21370 &ire_uinfo_null, 21371 NULL, 21372 NULL, 21373 ipst); 21374 21375 if (ire != NULL) { 21376 ire_t *ret_ire; 21377 int error; 21378 21379 /* 21380 * We don't need ipif_ire anymore. We need to delete 21381 * before we add so that ire_add does not detect 21382 * duplicates. 21383 */ 21384 ire_delete(ipif_ire); 21385 ret_ire = ire; 21386 error = ire_add(&ret_ire, NULL, NULL, NULL, B_FALSE); 21387 ASSERT(error == 0); 21388 ASSERT(ire == ret_ire); 21389 /* Held in ire_add */ 21390 ire_refrele(ret_ire); 21391 } 21392 /* 21393 * Either we are falling through from above or could not 21394 * allocate a replacement. 21395 */ 21396 ire_refrele(ipif_ire); 21397 if (need_rele) 21398 ipif_refrele(nipif); 21399 } 21400 21401 /* 21402 * This old_ipif is going away. 21403 * 21404 * Determine if any other ipif's is using our address as 21405 * ipif_lcl_addr (due to those being IPIF_NOLOCAL, IPIF_ANYCAST, or 21406 * IPIF_DEPRECATED). 21407 * Find the IRE_INTERFACE for such ipifs and recreate them 21408 * to use an different source address following the rules in 21409 * ipif_up_done. 21410 * 21411 * This function takes an illgrp as an argument so that illgrp_delete 21412 * can call this to update source address even after deleting the 21413 * old_ipif->ipif_ill from the ill group. 21414 */ 21415 static void 21416 ipif_update_other_ipifs(ipif_t *old_ipif, ill_group_t *illgrp) 21417 { 21418 ipif_t *ipif; 21419 ill_t *ill; 21420 char buf[INET6_ADDRSTRLEN]; 21421 21422 ASSERT(IAM_WRITER_IPIF(old_ipif)); 21423 ASSERT(illgrp == NULL || IAM_WRITER_IPIF(old_ipif)); 21424 21425 ill = old_ipif->ipif_ill; 21426 21427 ip1dbg(("ipif_update_other_ipifs(%s, %s)\n", 21428 ill->ill_name, 21429 inet_ntop(AF_INET, &old_ipif->ipif_lcl_addr, 21430 buf, sizeof (buf)))); 21431 /* 21432 * If this part of a group, look at all ills as ipif_select_source 21433 * borrows source address across all the ills in the group. 21434 */ 21435 if (illgrp != NULL) 21436 ill = illgrp->illgrp_ill; 21437 21438 for (; ill != NULL; ill = ill->ill_group_next) { 21439 for (ipif = ill->ill_ipif; ipif != NULL; 21440 ipif = ipif->ipif_next) { 21441 21442 if (ipif == old_ipif) 21443 continue; 21444 21445 ipif_recreate_interface_routes(old_ipif, ipif); 21446 } 21447 } 21448 } 21449 21450 /* ARGSUSED */ 21451 int 21452 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 21453 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 21454 { 21455 /* 21456 * ill_phyint_reinit merged the v4 and v6 into a single 21457 * ipsq. Could also have become part of a ipmp group in the 21458 * process, and we might not have been able to complete the 21459 * operation in ipif_set_values, if we could not become 21460 * exclusive. If so restart it here. 21461 */ 21462 return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q)); 21463 } 21464 21465 21466 /* 21467 * Can operate on either a module or a driver queue. 21468 * Returns an error if not a module queue. 21469 */ 21470 /* ARGSUSED */ 21471 int 21472 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 21473 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 21474 { 21475 queue_t *q1 = q; 21476 char *cp; 21477 char interf_name[LIFNAMSIZ]; 21478 uint_t ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr; 21479 21480 if (q->q_next == NULL) { 21481 ip1dbg(( 21482 "if_unitsel: IF_UNITSEL: no q_next\n")); 21483 return (EINVAL); 21484 } 21485 21486 if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0') 21487 return (EALREADY); 21488 21489 do { 21490 q1 = q1->q_next; 21491 } while (q1->q_next); 21492 cp = q1->q_qinfo->qi_minfo->mi_idname; 21493 (void) sprintf(interf_name, "%s%d", cp, ppa); 21494 21495 /* 21496 * Here we are not going to delay the ioack until after 21497 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the 21498 * original ioctl message before sending the requests. 21499 */ 21500 return (ipif_set_values(q, mp, interf_name, &ppa)); 21501 } 21502 21503 /* ARGSUSED */ 21504 int 21505 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp, 21506 ip_ioctl_cmd_t *ipip, void *dummy_ifreq) 21507 { 21508 return (ENXIO); 21509 } 21510 21511 /* 21512 * Net and subnet broadcast ire's are now specific to the particular 21513 * physical interface (ill) and not to any one locigal interface (ipif). 21514 * However, if a particular logical interface is being taken down, it's 21515 * associated ire's will be taken down as well. Hence, when we go to 21516 * take down or change the local address, broadcast address or netmask 21517 * of a specific logical interface, we must check to make sure that we 21518 * have valid net and subnet broadcast ire's for the other logical 21519 * interfaces which may have been shared with the logical interface 21520 * being brought down or changed. 21521 * 21522 * There is one set of 0.0.0.0 and 255.255.255.255 per ill. Usually it 21523 * is tied to the first interface coming UP. If that ipif is going down, 21524 * we need to recreate them on the next valid ipif. 21525 * 21526 * Note: assume that the ipif passed in is still up so that it's IRE 21527 * entries are still valid. 21528 */ 21529 static void 21530 ipif_check_bcast_ires(ipif_t *test_ipif) 21531 { 21532 ipif_t *ipif; 21533 ire_t *test_subnet_ire, *test_net_ire; 21534 ire_t *test_allzero_ire, *test_allone_ire; 21535 ire_t *ire_array[12]; 21536 ire_t **irep = &ire_array[0]; 21537 ire_t **irep1; 21538 ipaddr_t net_addr, subnet_addr, net_mask, subnet_mask; 21539 ipaddr_t test_net_addr, test_subnet_addr; 21540 ipaddr_t test_net_mask, test_subnet_mask; 21541 boolean_t need_net_bcast_ire = B_FALSE; 21542 boolean_t need_subnet_bcast_ire = B_FALSE; 21543 boolean_t allzero_bcast_ire_created = B_FALSE; 21544 boolean_t allone_bcast_ire_created = B_FALSE; 21545 boolean_t net_bcast_ire_created = B_FALSE; 21546 boolean_t subnet_bcast_ire_created = B_FALSE; 21547 21548 ipif_t *backup_ipif_net = (ipif_t *)NULL; 21549 ipif_t *backup_ipif_subnet = (ipif_t *)NULL; 21550 ipif_t *backup_ipif_allzeros = (ipif_t *)NULL; 21551 ipif_t *backup_ipif_allones = (ipif_t *)NULL; 21552 uint64_t check_flags = IPIF_DEPRECATED | IPIF_NOLOCAL | IPIF_ANYCAST; 21553 ip_stack_t *ipst = test_ipif->ipif_ill->ill_ipst; 21554 21555 ASSERT(!test_ipif->ipif_isv6); 21556 ASSERT(IAM_WRITER_IPIF(test_ipif)); 21557 21558 /* 21559 * No broadcast IREs for the LOOPBACK interface 21560 * or others such as point to point and IPIF_NOXMIT. 21561 */ 21562 if (!(test_ipif->ipif_flags & IPIF_BROADCAST) || 21563 (test_ipif->ipif_flags & IPIF_NOXMIT)) 21564 return; 21565 21566 test_allzero_ire = ire_ctable_lookup(0, 0, IRE_BROADCAST, 21567 test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF), 21568 ipst); 21569 21570 test_allone_ire = ire_ctable_lookup(INADDR_BROADCAST, 0, IRE_BROADCAST, 21571 test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF), 21572 ipst); 21573 21574 test_net_mask = ip_net_mask(test_ipif->ipif_subnet); 21575 test_subnet_mask = test_ipif->ipif_net_mask; 21576 21577 /* 21578 * If no net mask set, assume the default based on net class. 21579 */ 21580 if (test_subnet_mask == 0) 21581 test_subnet_mask = test_net_mask; 21582 21583 /* 21584 * Check if there is a network broadcast ire associated with this ipif 21585 */ 21586 test_net_addr = test_net_mask & test_ipif->ipif_subnet; 21587 test_net_ire = ire_ctable_lookup(test_net_addr, 0, IRE_BROADCAST, 21588 test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF), 21589 ipst); 21590 21591 /* 21592 * Check if there is a subnet broadcast IRE associated with this ipif 21593 */ 21594 test_subnet_addr = test_subnet_mask & test_ipif->ipif_subnet; 21595 test_subnet_ire = ire_ctable_lookup(test_subnet_addr, 0, IRE_BROADCAST, 21596 test_ipif, ALL_ZONES, NULL, (MATCH_IRE_TYPE | MATCH_IRE_IPIF), 21597 ipst); 21598 21599 /* 21600 * No broadcast ire's associated with this ipif. 21601 */ 21602 if ((test_subnet_ire == NULL) && (test_net_ire == NULL) && 21603 (test_allzero_ire == NULL) && (test_allone_ire == NULL)) { 21604 return; 21605 } 21606 21607 /* 21608 * We have established which bcast ires have to be replaced. 21609 * Next we try to locate ipifs that match there ires. 21610 * The rules are simple: If we find an ipif that matches on the subnet 21611 * address it will also match on the net address, the allzeros and 21612 * allones address. Any ipif that matches only on the net address will 21613 * also match the allzeros and allones addresses. 21614 * The other criterion is the ipif_flags. We look for non-deprecated 21615 * (and non-anycast and non-nolocal) ipifs as the best choice. 21616 * ipifs with check_flags matching (deprecated, etc) are used only 21617 * if good ipifs are not available. While looping, we save existing 21618 * deprecated ipifs as backup_ipif. 21619 * We loop through all the ipifs for this ill looking for ipifs 21620 * whose broadcast addr match the ipif passed in, but do not have 21621 * their own broadcast ires. For creating 0.0.0.0 and 21622 * 255.255.255.255 we just need an ipif on this ill to create. 21623 */ 21624 for (ipif = test_ipif->ipif_ill->ill_ipif; ipif != NULL; 21625 ipif = ipif->ipif_next) { 21626 21627 ASSERT(!ipif->ipif_isv6); 21628 /* 21629 * Already checked the ipif passed in. 21630 */ 21631 if (ipif == test_ipif) { 21632 continue; 21633 } 21634 21635 /* 21636 * We only need to recreate broadcast ires if another ipif in 21637 * the same zone uses them. The new ires must be created in the 21638 * same zone. 21639 */ 21640 if (ipif->ipif_zoneid != test_ipif->ipif_zoneid) { 21641 continue; 21642 } 21643 21644 /* 21645 * Only interested in logical interfaces with valid local 21646 * addresses or with the ability to broadcast. 21647 */ 21648 if ((ipif->ipif_subnet == 0) || 21649 !(ipif->ipif_flags & IPIF_BROADCAST) || 21650 (ipif->ipif_flags & IPIF_NOXMIT) || 21651 !(ipif->ipif_flags & IPIF_UP)) { 21652 continue; 21653 } 21654 /* 21655 * Check if there is a net broadcast ire for this 21656 * net address. If it turns out that the ipif we are 21657 * about to take down owns this ire, we must make a 21658 * new one because it is potentially going away. 21659 */ 21660 if (test_net_ire && (!net_bcast_ire_created)) { 21661 net_mask = ip_net_mask(ipif->ipif_subnet); 21662 net_addr = net_mask & ipif->ipif_subnet; 21663 if (net_addr == test_net_addr) { 21664 need_net_bcast_ire = B_TRUE; 21665 /* 21666 * Use DEPRECATED ipif only if no good 21667 * ires are available. subnet_addr is 21668 * a better match than net_addr. 21669 */ 21670 if ((ipif->ipif_flags & check_flags) && 21671 (backup_ipif_net == NULL)) { 21672 backup_ipif_net = ipif; 21673 } 21674 } 21675 } 21676 /* 21677 * Check if there is a subnet broadcast ire for this 21678 * net address. If it turns out that the ipif we are 21679 * about to take down owns this ire, we must make a 21680 * new one because it is potentially going away. 21681 */ 21682 if (test_subnet_ire && (!subnet_bcast_ire_created)) { 21683 subnet_mask = ipif->ipif_net_mask; 21684 subnet_addr = ipif->ipif_subnet; 21685 if (subnet_addr == test_subnet_addr) { 21686 need_subnet_bcast_ire = B_TRUE; 21687 if ((ipif->ipif_flags & check_flags) && 21688 (backup_ipif_subnet == NULL)) { 21689 backup_ipif_subnet = ipif; 21690 } 21691 } 21692 } 21693 21694 21695 /* Short circuit here if this ipif is deprecated */ 21696 if (ipif->ipif_flags & check_flags) { 21697 if ((test_allzero_ire != NULL) && 21698 (!allzero_bcast_ire_created) && 21699 (backup_ipif_allzeros == NULL)) { 21700 backup_ipif_allzeros = ipif; 21701 } 21702 if ((test_allone_ire != NULL) && 21703 (!allone_bcast_ire_created) && 21704 (backup_ipif_allones == NULL)) { 21705 backup_ipif_allones = ipif; 21706 } 21707 continue; 21708 } 21709 21710 /* 21711 * Found an ipif which has the same broadcast ire as the 21712 * ipif passed in and the ipif passed in "owns" the ire. 21713 * Create new broadcast ire's for this broadcast addr. 21714 */ 21715 if (need_net_bcast_ire && !net_bcast_ire_created) { 21716 irep = ire_create_bcast(ipif, net_addr, irep); 21717 irep = ire_create_bcast(ipif, 21718 ~net_mask | net_addr, irep); 21719 net_bcast_ire_created = B_TRUE; 21720 } 21721 if (need_subnet_bcast_ire && !subnet_bcast_ire_created) { 21722 irep = ire_create_bcast(ipif, subnet_addr, irep); 21723 irep = ire_create_bcast(ipif, 21724 ~subnet_mask | subnet_addr, irep); 21725 subnet_bcast_ire_created = B_TRUE; 21726 } 21727 if (test_allzero_ire != NULL && !allzero_bcast_ire_created) { 21728 irep = ire_create_bcast(ipif, 0, irep); 21729 allzero_bcast_ire_created = B_TRUE; 21730 } 21731 if (test_allone_ire != NULL && !allone_bcast_ire_created) { 21732 irep = ire_create_bcast(ipif, INADDR_BROADCAST, irep); 21733 allone_bcast_ire_created = B_TRUE; 21734 } 21735 /* 21736 * Once we have created all the appropriate ires, we 21737 * just break out of this loop to add what we have created. 21738 * This has been indented similar to ire_match_args for 21739 * readability. 21740 */ 21741 if (((test_net_ire == NULL) || 21742 (net_bcast_ire_created)) && 21743 ((test_subnet_ire == NULL) || 21744 (subnet_bcast_ire_created)) && 21745 ((test_allzero_ire == NULL) || 21746 (allzero_bcast_ire_created)) && 21747 ((test_allone_ire == NULL) || 21748 (allone_bcast_ire_created))) { 21749 break; 21750 } 21751 } 21752 21753 /* 21754 * Create bcast ires on deprecated ipifs if no non-deprecated ipifs 21755 * exist. 6 pairs of bcast ires are needed. 21756 * Note - the old ires are deleted in ipif_down. 21757 */ 21758 if (need_net_bcast_ire && !net_bcast_ire_created && backup_ipif_net) { 21759 ipif = backup_ipif_net; 21760 irep = ire_create_bcast(ipif, net_addr, irep); 21761 irep = ire_create_bcast(ipif, ~net_mask | net_addr, irep); 21762 net_bcast_ire_created = B_TRUE; 21763 } 21764 if (need_subnet_bcast_ire && !subnet_bcast_ire_created && 21765 backup_ipif_subnet) { 21766 ipif = backup_ipif_subnet; 21767 irep = ire_create_bcast(ipif, subnet_addr, irep); 21768 irep = ire_create_bcast(ipif, 21769 ~subnet_mask | subnet_addr, irep); 21770 subnet_bcast_ire_created = B_TRUE; 21771 } 21772 if (test_allzero_ire != NULL && !allzero_bcast_ire_created && 21773 backup_ipif_allzeros) { 21774 irep = ire_create_bcast(backup_ipif_allzeros, 0, irep); 21775 allzero_bcast_ire_created = B_TRUE; 21776 } 21777 if (test_allone_ire != NULL && !allone_bcast_ire_created && 21778 backup_ipif_allones) { 21779 irep = ire_create_bcast(backup_ipif_allones, 21780 INADDR_BROADCAST, irep); 21781 allone_bcast_ire_created = B_TRUE; 21782 } 21783 21784 /* 21785 * If we can't create all of them, don't add any of them. 21786 * Code in ip_wput_ire and ire_to_ill assumes that we 21787 * always have a non-loopback copy and loopback copy 21788 * for a given address. 21789 */ 21790 for (irep1 = irep; irep1 > ire_array; ) { 21791 irep1--; 21792 if (*irep1 == NULL) { 21793 ip0dbg(("ipif_check_bcast_ires: can't create " 21794 "IRE_BROADCAST, memory allocation failure\n")); 21795 while (irep > ire_array) { 21796 irep--; 21797 if (*irep != NULL) 21798 ire_delete(*irep); 21799 } 21800 goto bad; 21801 } 21802 } 21803 for (irep1 = irep; irep1 > ire_array; ) { 21804 int error; 21805 21806 irep1--; 21807 error = ire_add(irep1, NULL, NULL, NULL, B_FALSE); 21808 if (error == 0) { 21809 ire_refrele(*irep1); /* Held in ire_add */ 21810 } 21811 } 21812 bad: 21813 if (test_allzero_ire != NULL) 21814 ire_refrele(test_allzero_ire); 21815 if (test_allone_ire != NULL) 21816 ire_refrele(test_allone_ire); 21817 if (test_net_ire != NULL) 21818 ire_refrele(test_net_ire); 21819 if (test_subnet_ire != NULL) 21820 ire_refrele(test_subnet_ire); 21821 } 21822 21823 /* 21824 * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV* 21825 * from lifr_flags and the name from lifr_name. 21826 * Set IFF_IPV* and ill_isv6 prior to doing the lookup 21827 * since ipif_lookup_on_name uses the _isv6 flags when matching. 21828 * Returns EINPROGRESS when mp has been consumed by queueing it on 21829 * ill_pending_mp and the ioctl will complete in ip_rput. 21830 * 21831 * Can operate on either a module or a driver queue. 21832 * Returns an error if not a module queue. 21833 */ 21834 /* ARGSUSED */ 21835 int 21836 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 21837 ip_ioctl_cmd_t *ipip, void *if_req) 21838 { 21839 int err; 21840 ill_t *ill; 21841 struct lifreq *lifr = (struct lifreq *)if_req; 21842 21843 ASSERT(ipif != NULL); 21844 ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name)); 21845 21846 if (q->q_next == NULL) { 21847 ip1dbg(( 21848 "if_sioctl_slifname: SIOCSLIFNAME: no q_next\n")); 21849 return (EINVAL); 21850 } 21851 21852 ill = (ill_t *)q->q_ptr; 21853 /* 21854 * If we are not writer on 'q' then this interface exists already 21855 * and previous lookups (ipif_extract_lifreq_cmn) found this ipif. 21856 * So return EALREADY 21857 */ 21858 if (ill != ipif->ipif_ill) 21859 return (EALREADY); 21860 21861 if (ill->ill_name[0] != '\0') 21862 return (EALREADY); 21863 21864 /* 21865 * Set all the flags. Allows all kinds of override. Provide some 21866 * sanity checking by not allowing IFF_BROADCAST and IFF_MULTICAST 21867 * unless there is either multicast/broadcast support in the driver 21868 * or it is a pt-pt link. 21869 */ 21870 if (lifr->lifr_flags & (IFF_PROMISC|IFF_ALLMULTI)) { 21871 /* Meaningless to IP thus don't allow them to be set. */ 21872 ip1dbg(("ip_setname: EINVAL 1\n")); 21873 return (EINVAL); 21874 } 21875 /* 21876 * For a DL_STYLE2 driver (ill_needs_attach), we would not have the 21877 * ill_bcast_addr_length info. 21878 */ 21879 if (!ill->ill_needs_attach && 21880 ((lifr->lifr_flags & IFF_MULTICAST) && 21881 !(lifr->lifr_flags & IFF_POINTOPOINT) && 21882 ill->ill_bcast_addr_length == 0)) { 21883 /* Link not broadcast/pt-pt capable i.e. no multicast */ 21884 ip1dbg(("ip_setname: EINVAL 2\n")); 21885 return (EINVAL); 21886 } 21887 if ((lifr->lifr_flags & IFF_BROADCAST) && 21888 ((lifr->lifr_flags & IFF_IPV6) || 21889 (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) { 21890 /* Link not broadcast capable or IPv6 i.e. no broadcast */ 21891 ip1dbg(("ip_setname: EINVAL 3\n")); 21892 return (EINVAL); 21893 } 21894 if (lifr->lifr_flags & IFF_UP) { 21895 /* Can only be set with SIOCSLIFFLAGS */ 21896 ip1dbg(("ip_setname: EINVAL 4\n")); 21897 return (EINVAL); 21898 } 21899 if ((lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV6 && 21900 (lifr->lifr_flags & (IFF_IPV6|IFF_IPV4)) != IFF_IPV4) { 21901 ip1dbg(("ip_setname: EINVAL 5\n")); 21902 return (EINVAL); 21903 } 21904 /* 21905 * Only allow the IFF_XRESOLV flag to be set on IPv6 interfaces. 21906 */ 21907 if ((lifr->lifr_flags & IFF_XRESOLV) && 21908 !(lifr->lifr_flags & IFF_IPV6) && 21909 !(ipif->ipif_isv6)) { 21910 ip1dbg(("ip_setname: EINVAL 6\n")); 21911 return (EINVAL); 21912 } 21913 21914 /* 21915 * The user has done SIOCGLIFFLAGS prior to this ioctl and hence 21916 * we have all the flags here. So, we assign rather than we OR. 21917 * We can't OR the flags here because we don't want to set 21918 * both IFF_IPV4 and IFF_IPV6. We start off as IFF_IPV4 in 21919 * ipif_allocate and become IFF_IPV4 or IFF_IPV6 here depending 21920 * on lifr_flags value here. 21921 */ 21922 /* 21923 * This ill has not been inserted into the global list. 21924 * So we are still single threaded and don't need any lock 21925 */ 21926 ipif->ipif_flags = lifr->lifr_flags & IFF_LOGINT_FLAGS & 21927 ~IFF_DUPLICATE; 21928 ill->ill_flags = lifr->lifr_flags & IFF_PHYINTINST_FLAGS; 21929 ill->ill_phyint->phyint_flags = lifr->lifr_flags & IFF_PHYINT_FLAGS; 21930 21931 /* We started off as V4. */ 21932 if (ill->ill_flags & ILLF_IPV6) { 21933 ill->ill_phyint->phyint_illv6 = ill; 21934 ill->ill_phyint->phyint_illv4 = NULL; 21935 } 21936 err = ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa); 21937 return (err); 21938 } 21939 21940 /* ARGSUSED */ 21941 int 21942 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 21943 ip_ioctl_cmd_t *ipip, void *if_req) 21944 { 21945 /* 21946 * ill_phyint_reinit merged the v4 and v6 into a single 21947 * ipsq. Could also have become part of a ipmp group in the 21948 * process, and we might not have been able to complete the 21949 * slifname in ipif_set_values, if we could not become 21950 * exclusive. If so restart it here 21951 */ 21952 return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q)); 21953 } 21954 21955 /* 21956 * Return a pointer to the ipif which matches the index, IP version type and 21957 * zoneid. 21958 */ 21959 ipif_t * 21960 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid, 21961 queue_t *q, mblk_t *mp, ipsq_func_t func, int *err, ip_stack_t *ipst) 21962 { 21963 ill_t *ill; 21964 ipsq_t *ipsq; 21965 phyint_t *phyi; 21966 ipif_t *ipif; 21967 21968 ASSERT((q == NULL && mp == NULL && func == NULL && err == NULL) || 21969 (q != NULL && mp != NULL && func != NULL && err != NULL)); 21970 21971 if (err != NULL) 21972 *err = 0; 21973 21974 /* 21975 * Indexes are stored in the phyint - a common structure 21976 * to both IPv4 and IPv6. 21977 */ 21978 21979 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 21980 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 21981 (void *) &index, NULL); 21982 if (phyi != NULL) { 21983 ill = isv6 ? phyi->phyint_illv6 : phyi->phyint_illv4; 21984 if (ill == NULL) { 21985 rw_exit(&ipst->ips_ill_g_lock); 21986 if (err != NULL) 21987 *err = ENXIO; 21988 return (NULL); 21989 } 21990 GRAB_CONN_LOCK(q); 21991 mutex_enter(&ill->ill_lock); 21992 if (ILL_CAN_LOOKUP(ill)) { 21993 for (ipif = ill->ill_ipif; ipif != NULL; 21994 ipif = ipif->ipif_next) { 21995 if (IPIF_CAN_LOOKUP(ipif) && 21996 (zoneid == ALL_ZONES || 21997 zoneid == ipif->ipif_zoneid || 21998 ipif->ipif_zoneid == ALL_ZONES)) { 21999 ipif_refhold_locked(ipif); 22000 mutex_exit(&ill->ill_lock); 22001 RELEASE_CONN_LOCK(q); 22002 rw_exit(&ipst->ips_ill_g_lock); 22003 return (ipif); 22004 } 22005 } 22006 } else if (ILL_CAN_WAIT(ill, q)) { 22007 ipsq = ill->ill_phyint->phyint_ipsq; 22008 mutex_enter(&ipsq->ipsq_lock); 22009 rw_exit(&ipst->ips_ill_g_lock); 22010 mutex_exit(&ill->ill_lock); 22011 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill); 22012 mutex_exit(&ipsq->ipsq_lock); 22013 RELEASE_CONN_LOCK(q); 22014 *err = EINPROGRESS; 22015 return (NULL); 22016 } 22017 mutex_exit(&ill->ill_lock); 22018 RELEASE_CONN_LOCK(q); 22019 } 22020 rw_exit(&ipst->ips_ill_g_lock); 22021 if (err != NULL) 22022 *err = ENXIO; 22023 return (NULL); 22024 } 22025 22026 typedef struct conn_change_s { 22027 uint_t cc_old_ifindex; 22028 uint_t cc_new_ifindex; 22029 } conn_change_t; 22030 22031 /* 22032 * ipcl_walk function for changing interface index. 22033 */ 22034 static void 22035 conn_change_ifindex(conn_t *connp, caddr_t arg) 22036 { 22037 conn_change_t *connc; 22038 uint_t old_ifindex; 22039 uint_t new_ifindex; 22040 int i; 22041 ilg_t *ilg; 22042 22043 connc = (conn_change_t *)arg; 22044 old_ifindex = connc->cc_old_ifindex; 22045 new_ifindex = connc->cc_new_ifindex; 22046 22047 if (connp->conn_orig_bound_ifindex == old_ifindex) 22048 connp->conn_orig_bound_ifindex = new_ifindex; 22049 22050 if (connp->conn_orig_multicast_ifindex == old_ifindex) 22051 connp->conn_orig_multicast_ifindex = new_ifindex; 22052 22053 if (connp->conn_orig_xmit_ifindex == old_ifindex) 22054 connp->conn_orig_xmit_ifindex = new_ifindex; 22055 22056 for (i = connp->conn_ilg_inuse - 1; i >= 0; i--) { 22057 ilg = &connp->conn_ilg[i]; 22058 if (ilg->ilg_orig_ifindex == old_ifindex) 22059 ilg->ilg_orig_ifindex = new_ifindex; 22060 } 22061 } 22062 22063 /* 22064 * Walk all the ipifs and ilms on this ill and change the orig_ifindex 22065 * to new_index if it matches the old_index. 22066 * 22067 * Failovers typically happen within a group of ills. But somebody 22068 * can remove an ill from the group after a failover happened. If 22069 * we are setting the ifindex after this, we potentially need to 22070 * look at all the ills rather than just the ones in the group. 22071 * We cut down the work by looking at matching ill_net_types 22072 * and ill_types as we could not possibly grouped them together. 22073 */ 22074 static void 22075 ip_change_ifindex(ill_t *ill_orig, conn_change_t *connc) 22076 { 22077 ill_t *ill; 22078 ipif_t *ipif; 22079 uint_t old_ifindex; 22080 uint_t new_ifindex; 22081 ilm_t *ilm; 22082 ill_walk_context_t ctx; 22083 ip_stack_t *ipst = ill_orig->ill_ipst; 22084 22085 old_ifindex = connc->cc_old_ifindex; 22086 new_ifindex = connc->cc_new_ifindex; 22087 22088 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 22089 ill = ILL_START_WALK_ALL(&ctx, ipst); 22090 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 22091 if ((ill_orig->ill_net_type != ill->ill_net_type) || 22092 (ill_orig->ill_type != ill->ill_type)) { 22093 continue; 22094 } 22095 for (ipif = ill->ill_ipif; ipif != NULL; 22096 ipif = ipif->ipif_next) { 22097 if (ipif->ipif_orig_ifindex == old_ifindex) 22098 ipif->ipif_orig_ifindex = new_ifindex; 22099 } 22100 for (ilm = ill->ill_ilm; ilm != NULL; ilm = ilm->ilm_next) { 22101 if (ilm->ilm_orig_ifindex == old_ifindex) 22102 ilm->ilm_orig_ifindex = new_ifindex; 22103 } 22104 } 22105 rw_exit(&ipst->ips_ill_g_lock); 22106 } 22107 22108 /* 22109 * We first need to ensure that the new index is unique, and 22110 * then carry the change across both v4 and v6 ill representation 22111 * of the physical interface. 22112 */ 22113 /* ARGSUSED */ 22114 int 22115 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22116 ip_ioctl_cmd_t *ipip, void *ifreq) 22117 { 22118 ill_t *ill; 22119 ill_t *ill_other; 22120 phyint_t *phyi; 22121 int old_index; 22122 conn_change_t connc; 22123 struct ifreq *ifr = (struct ifreq *)ifreq; 22124 struct lifreq *lifr = (struct lifreq *)ifreq; 22125 uint_t index; 22126 ill_t *ill_v4; 22127 ill_t *ill_v6; 22128 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 22129 22130 if (ipip->ipi_cmd_type == IF_CMD) 22131 index = ifr->ifr_index; 22132 else 22133 index = lifr->lifr_index; 22134 22135 /* 22136 * Only allow on physical interface. Also, index zero is illegal. 22137 * 22138 * Need to check for PHYI_FAILED and PHYI_INACTIVE 22139 * 22140 * 1) If PHYI_FAILED is set, a failover could have happened which 22141 * implies a possible failback might have to happen. As failback 22142 * depends on the old index, we should fail setting the index. 22143 * 22144 * 2) If PHYI_INACTIVE is set, in.mpathd does a failover so that 22145 * any addresses or multicast memberships are failed over to 22146 * a non-STANDBY interface. As failback depends on the old 22147 * index, we should fail setting the index for this case also. 22148 * 22149 * 3) If PHYI_OFFLINE is set, a possible failover has happened. 22150 * Be consistent with PHYI_FAILED and fail the ioctl. 22151 */ 22152 ill = ipif->ipif_ill; 22153 phyi = ill->ill_phyint; 22154 if ((phyi->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE|PHYI_OFFLINE)) || 22155 ipif->ipif_id != 0 || index == 0) { 22156 return (EINVAL); 22157 } 22158 old_index = phyi->phyint_ifindex; 22159 22160 /* If the index is not changing, no work to do */ 22161 if (old_index == index) 22162 return (0); 22163 22164 /* 22165 * Use ill_lookup_on_ifindex to determine if the 22166 * new index is unused and if so allow the change. 22167 */ 22168 ill_v6 = ill_lookup_on_ifindex(index, B_TRUE, NULL, NULL, NULL, NULL, 22169 ipst); 22170 ill_v4 = ill_lookup_on_ifindex(index, B_FALSE, NULL, NULL, NULL, NULL, 22171 ipst); 22172 if (ill_v6 != NULL || ill_v4 != NULL) { 22173 if (ill_v4 != NULL) 22174 ill_refrele(ill_v4); 22175 if (ill_v6 != NULL) 22176 ill_refrele(ill_v6); 22177 return (EBUSY); 22178 } 22179 22180 /* 22181 * The new index is unused. Set it in the phyint. 22182 * Locate the other ill so that we can send a routing 22183 * sockets message. 22184 */ 22185 if (ill->ill_isv6) { 22186 ill_other = phyi->phyint_illv4; 22187 } else { 22188 ill_other = phyi->phyint_illv6; 22189 } 22190 22191 phyi->phyint_ifindex = index; 22192 22193 /* Update SCTP's ILL list */ 22194 sctp_ill_reindex(ill, old_index); 22195 22196 connc.cc_old_ifindex = old_index; 22197 connc.cc_new_ifindex = index; 22198 ip_change_ifindex(ill, &connc); 22199 ipcl_walk(conn_change_ifindex, (caddr_t)&connc, ipst); 22200 22201 /* Send the routing sockets message */ 22202 ip_rts_ifmsg(ipif); 22203 if (ill_other != NULL) 22204 ip_rts_ifmsg(ill_other->ill_ipif); 22205 22206 return (0); 22207 } 22208 22209 /* ARGSUSED */ 22210 int 22211 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22212 ip_ioctl_cmd_t *ipip, void *ifreq) 22213 { 22214 struct ifreq *ifr = (struct ifreq *)ifreq; 22215 struct lifreq *lifr = (struct lifreq *)ifreq; 22216 22217 ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n", 22218 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 22219 /* Get the interface index */ 22220 if (ipip->ipi_cmd_type == IF_CMD) { 22221 ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex; 22222 } else { 22223 lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex; 22224 } 22225 return (0); 22226 } 22227 22228 /* ARGSUSED */ 22229 int 22230 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22231 ip_ioctl_cmd_t *ipip, void *ifreq) 22232 { 22233 struct lifreq *lifr = (struct lifreq *)ifreq; 22234 22235 ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n", 22236 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 22237 /* Get the interface zone */ 22238 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 22239 lifr->lifr_zoneid = ipif->ipif_zoneid; 22240 return (0); 22241 } 22242 22243 /* 22244 * Set the zoneid of an interface. 22245 */ 22246 /* ARGSUSED */ 22247 int 22248 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22249 ip_ioctl_cmd_t *ipip, void *ifreq) 22250 { 22251 struct lifreq *lifr = (struct lifreq *)ifreq; 22252 int err = 0; 22253 boolean_t need_up = B_FALSE; 22254 zone_t *zptr; 22255 zone_status_t status; 22256 zoneid_t zoneid; 22257 22258 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 22259 if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) { 22260 if (!is_system_labeled()) 22261 return (ENOTSUP); 22262 zoneid = GLOBAL_ZONEID; 22263 } 22264 22265 /* cannot assign instance zero to a non-global zone */ 22266 if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID) 22267 return (ENOTSUP); 22268 22269 /* 22270 * Cannot assign to a zone that doesn't exist or is shutting down. In 22271 * the event of a race with the zone shutdown processing, since IP 22272 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the 22273 * interface will be cleaned up even if the zone is shut down 22274 * immediately after the status check. If the interface can't be brought 22275 * down right away, and the zone is shut down before the restart 22276 * function is called, we resolve the possible races by rechecking the 22277 * zone status in the restart function. 22278 */ 22279 if ((zptr = zone_find_by_id(zoneid)) == NULL) 22280 return (EINVAL); 22281 status = zone_status_get(zptr); 22282 zone_rele(zptr); 22283 22284 if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) 22285 return (EINVAL); 22286 22287 if (ipif->ipif_flags & IPIF_UP) { 22288 /* 22289 * If the interface is already marked up, 22290 * we call ipif_down which will take care 22291 * of ditching any IREs that have been set 22292 * up based on the old interface address. 22293 */ 22294 err = ipif_logical_down(ipif, q, mp); 22295 if (err == EINPROGRESS) 22296 return (err); 22297 ipif_down_tail(ipif); 22298 need_up = B_TRUE; 22299 } 22300 22301 err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up); 22302 return (err); 22303 } 22304 22305 static int 22306 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid, 22307 queue_t *q, mblk_t *mp, boolean_t need_up) 22308 { 22309 int err = 0; 22310 ip_stack_t *ipst; 22311 22312 ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n", 22313 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 22314 22315 if (CONN_Q(q)) 22316 ipst = CONNQ_TO_IPST(q); 22317 else 22318 ipst = ILLQ_TO_IPST(q); 22319 22320 /* 22321 * For exclusive stacks we don't allow a different zoneid than 22322 * global. 22323 */ 22324 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID && 22325 zoneid != GLOBAL_ZONEID) 22326 return (EINVAL); 22327 22328 /* Set the new zone id. */ 22329 ipif->ipif_zoneid = zoneid; 22330 22331 /* Update sctp list */ 22332 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE); 22333 22334 if (need_up) { 22335 /* 22336 * Now bring the interface back up. If this 22337 * is the only IPIF for the ILL, ipif_up 22338 * will have to re-bind to the device, so 22339 * we may get back EINPROGRESS, in which 22340 * case, this IOCTL will get completed in 22341 * ip_rput_dlpi when we see the DL_BIND_ACK. 22342 */ 22343 err = ipif_up(ipif, q, mp); 22344 } 22345 return (err); 22346 } 22347 22348 /* ARGSUSED */ 22349 int 22350 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22351 ip_ioctl_cmd_t *ipip, void *if_req) 22352 { 22353 struct lifreq *lifr = (struct lifreq *)if_req; 22354 zoneid_t zoneid; 22355 zone_t *zptr; 22356 zone_status_t status; 22357 22358 ASSERT(ipif->ipif_id != 0); 22359 ASSERT(ipip->ipi_cmd_type == LIF_CMD); 22360 if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) 22361 zoneid = GLOBAL_ZONEID; 22362 22363 ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n", 22364 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 22365 22366 /* 22367 * We recheck the zone status to resolve the following race condition: 22368 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone"; 22369 * 2) hme0:1 is up and can't be brought down right away; 22370 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued; 22371 * 3) zone "myzone" is halted; the zone status switches to 22372 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list 22373 * the interfaces to remove - hme0:1 is not returned because it's not 22374 * yet in "myzone", so it won't be removed; 22375 * 4) the restart function for SIOCSLIFZONE is called; without the 22376 * status check here, we would have hme0:1 in "myzone" after it's been 22377 * destroyed. 22378 * Note that if the status check fails, we need to bring the interface 22379 * back to its state prior to ip_sioctl_slifzone(), hence the call to 22380 * ipif_up_done[_v6](). 22381 */ 22382 status = ZONE_IS_UNINITIALIZED; 22383 if ((zptr = zone_find_by_id(zoneid)) != NULL) { 22384 status = zone_status_get(zptr); 22385 zone_rele(zptr); 22386 } 22387 if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) { 22388 if (ipif->ipif_isv6) { 22389 (void) ipif_up_done_v6(ipif); 22390 } else { 22391 (void) ipif_up_done(ipif); 22392 } 22393 return (EINVAL); 22394 } 22395 22396 ipif_down_tail(ipif); 22397 22398 return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, 22399 B_TRUE)); 22400 } 22401 22402 /* ARGSUSED */ 22403 int 22404 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22405 ip_ioctl_cmd_t *ipip, void *ifreq) 22406 { 22407 struct lifreq *lifr = ifreq; 22408 22409 ASSERT(q->q_next == NULL); 22410 ASSERT(CONN_Q(q)); 22411 22412 ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n", 22413 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif)); 22414 lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex; 22415 ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index)); 22416 22417 return (0); 22418 } 22419 22420 22421 /* Find the previous ILL in this usesrc group */ 22422 static ill_t * 22423 ill_prev_usesrc(ill_t *uill) 22424 { 22425 ill_t *ill; 22426 22427 for (ill = uill->ill_usesrc_grp_next; 22428 ASSERT(ill), ill->ill_usesrc_grp_next != uill; 22429 ill = ill->ill_usesrc_grp_next) 22430 /* do nothing */; 22431 return (ill); 22432 } 22433 22434 /* 22435 * Release all members of the usesrc group. This routine is called 22436 * from ill_delete when the interface being unplumbed is the 22437 * group head. 22438 */ 22439 static void 22440 ill_disband_usesrc_group(ill_t *uill) 22441 { 22442 ill_t *next_ill, *tmp_ill; 22443 ip_stack_t *ipst = uill->ill_ipst; 22444 22445 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock)); 22446 next_ill = uill->ill_usesrc_grp_next; 22447 22448 do { 22449 ASSERT(next_ill != NULL); 22450 tmp_ill = next_ill->ill_usesrc_grp_next; 22451 ASSERT(tmp_ill != NULL); 22452 next_ill->ill_usesrc_grp_next = NULL; 22453 next_ill->ill_usesrc_ifindex = 0; 22454 next_ill = tmp_ill; 22455 } while (next_ill->ill_usesrc_ifindex != 0); 22456 uill->ill_usesrc_grp_next = NULL; 22457 } 22458 22459 /* 22460 * Remove the client usesrc ILL from the list and relink to a new list 22461 */ 22462 int 22463 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex) 22464 { 22465 ill_t *ill, *tmp_ill; 22466 ip_stack_t *ipst = ucill->ill_ipst; 22467 22468 ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) && 22469 (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock)); 22470 22471 /* 22472 * Check if the usesrc client ILL passed in is not already 22473 * in use as a usesrc ILL i.e one whose source address is 22474 * in use OR a usesrc ILL is not already in use as a usesrc 22475 * client ILL 22476 */ 22477 if ((ucill->ill_usesrc_ifindex == 0) || 22478 (uill->ill_usesrc_ifindex != 0)) { 22479 return (-1); 22480 } 22481 22482 ill = ill_prev_usesrc(ucill); 22483 ASSERT(ill->ill_usesrc_grp_next != NULL); 22484 22485 /* Remove from the current list */ 22486 if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) { 22487 /* Only two elements in the list */ 22488 ASSERT(ill->ill_usesrc_ifindex == 0); 22489 ill->ill_usesrc_grp_next = NULL; 22490 } else { 22491 ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next; 22492 } 22493 22494 if (ifindex == 0) { 22495 ucill->ill_usesrc_ifindex = 0; 22496 ucill->ill_usesrc_grp_next = NULL; 22497 return (0); 22498 } 22499 22500 ucill->ill_usesrc_ifindex = ifindex; 22501 tmp_ill = uill->ill_usesrc_grp_next; 22502 uill->ill_usesrc_grp_next = ucill; 22503 ucill->ill_usesrc_grp_next = 22504 (tmp_ill != NULL) ? tmp_ill : uill; 22505 return (0); 22506 } 22507 22508 /* 22509 * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in 22510 * ip.c for locking details. 22511 */ 22512 /* ARGSUSED */ 22513 int 22514 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp, 22515 ip_ioctl_cmd_t *ipip, void *ifreq) 22516 { 22517 struct lifreq *lifr = (struct lifreq *)ifreq; 22518 boolean_t isv6 = B_FALSE, reset_flg = B_FALSE, 22519 ill_flag_changed = B_FALSE; 22520 ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill; 22521 int err = 0, ret; 22522 uint_t ifindex; 22523 phyint_t *us_phyint, *us_cli_phyint; 22524 ipsq_t *ipsq = NULL; 22525 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst; 22526 22527 ASSERT(IAM_WRITER_IPIF(ipif)); 22528 ASSERT(q->q_next == NULL); 22529 ASSERT(CONN_Q(q)); 22530 22531 isv6 = (Q_TO_CONN(q))->conn_af_isv6; 22532 us_cli_phyint = usesrc_cli_ill->ill_phyint; 22533 22534 ASSERT(us_cli_phyint != NULL); 22535 22536 /* 22537 * If the client ILL is being used for IPMP, abort. 22538 * Note, this can be done before ipsq_try_enter since we are already 22539 * exclusive on this ILL 22540 */ 22541 if ((us_cli_phyint->phyint_groupname != NULL) || 22542 (us_cli_phyint->phyint_flags & PHYI_STANDBY)) { 22543 return (EINVAL); 22544 } 22545 22546 ifindex = lifr->lifr_index; 22547 if (ifindex == 0) { 22548 if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) { 22549 /* non usesrc group interface, nothing to reset */ 22550 return (0); 22551 } 22552 ifindex = usesrc_cli_ill->ill_usesrc_ifindex; 22553 /* valid reset request */ 22554 reset_flg = B_TRUE; 22555 } 22556 22557 usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, q, mp, 22558 ip_process_ioctl, &err, ipst); 22559 22560 if (usesrc_ill == NULL) { 22561 return (err); 22562 } 22563 22564 /* 22565 * The usesrc_cli_ill or the usesrc_ill cannot be part of an IPMP 22566 * group nor can either of the interfaces be used for standy. So 22567 * to guarantee mutual exclusion with ip_sioctl_flags (which sets 22568 * PHYI_STANDBY) and ip_sioctl_groupname (which sets the groupname) 22569 * we need to be exclusive on the ipsq belonging to the usesrc_ill. 22570 * We are already exlusive on this ipsq i.e ipsq corresponding to 22571 * the usesrc_cli_ill 22572 */ 22573 ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl, 22574 NEW_OP, B_TRUE); 22575 if (ipsq == NULL) { 22576 err = EINPROGRESS; 22577 /* Operation enqueued on the ipsq of the usesrc ILL */ 22578 goto done; 22579 } 22580 22581 /* Check if the usesrc_ill is used for IPMP */ 22582 us_phyint = usesrc_ill->ill_phyint; 22583 if ((us_phyint->phyint_groupname != NULL) || 22584 (us_phyint->phyint_flags & PHYI_STANDBY)) { 22585 err = EINVAL; 22586 goto done; 22587 } 22588 22589 /* 22590 * If the client is already in use as a usesrc_ill or a usesrc_ill is 22591 * already a client then return EINVAL 22592 */ 22593 if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) { 22594 err = EINVAL; 22595 goto done; 22596 } 22597 22598 /* 22599 * If the ill_usesrc_ifindex field is already set to what it needs to 22600 * be then this is a duplicate operation. 22601 */ 22602 if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) { 22603 err = 0; 22604 goto done; 22605 } 22606 22607 ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s," 22608 " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name, 22609 usesrc_ill->ill_isv6)); 22610 22611 /* 22612 * The next step ensures that no new ires will be created referencing 22613 * the client ill, until the ILL_CHANGING flag is cleared. Then 22614 * we go through an ire walk deleting all ire caches that reference 22615 * the client ill. New ires referencing the client ill that are added 22616 * to the ire table before the ILL_CHANGING flag is set, will be 22617 * cleaned up by the ire walk below. Attempt to add new ires referencing 22618 * the client ill while the ILL_CHANGING flag is set will be failed 22619 * during the ire_add in ire_atomic_start. ire_atomic_start atomically 22620 * checks (under the ill_g_usesrc_lock) that the ire being added 22621 * is not stale, i.e the ire_stq and ire_ipif are consistent and 22622 * belong to the same usesrc group. 22623 */ 22624 mutex_enter(&usesrc_cli_ill->ill_lock); 22625 usesrc_cli_ill->ill_state_flags |= ILL_CHANGING; 22626 mutex_exit(&usesrc_cli_ill->ill_lock); 22627 ill_flag_changed = B_TRUE; 22628 22629 if (ipif->ipif_isv6) 22630 ire_walk_v6(ipif_delete_cache_ire, (char *)usesrc_cli_ill, 22631 ALL_ZONES, ipst); 22632 else 22633 ire_walk_v4(ipif_delete_cache_ire, (char *)usesrc_cli_ill, 22634 ALL_ZONES, ipst); 22635 22636 /* 22637 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next 22638 * and the ill_usesrc_ifindex fields 22639 */ 22640 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER); 22641 22642 if (reset_flg) { 22643 ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0); 22644 if (ret != 0) { 22645 err = EINVAL; 22646 } 22647 rw_exit(&ipst->ips_ill_g_usesrc_lock); 22648 goto done; 22649 } 22650 22651 /* 22652 * Four possibilities to consider: 22653 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp 22654 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't 22655 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't 22656 * 4. Both are part of their respective usesrc groups 22657 */ 22658 if ((usesrc_ill->ill_usesrc_grp_next == NULL) && 22659 (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) { 22660 ASSERT(usesrc_ill->ill_usesrc_ifindex == 0); 22661 usesrc_cli_ill->ill_usesrc_ifindex = ifindex; 22662 usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill; 22663 usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill; 22664 } else if ((usesrc_ill->ill_usesrc_grp_next != NULL) && 22665 (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) { 22666 usesrc_cli_ill->ill_usesrc_ifindex = ifindex; 22667 /* Insert at head of list */ 22668 usesrc_cli_ill->ill_usesrc_grp_next = 22669 usesrc_ill->ill_usesrc_grp_next; 22670 usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill; 22671 } else { 22672 ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 22673 ifindex); 22674 if (ret != 0) 22675 err = EINVAL; 22676 } 22677 rw_exit(&ipst->ips_ill_g_usesrc_lock); 22678 22679 done: 22680 if (ill_flag_changed) { 22681 mutex_enter(&usesrc_cli_ill->ill_lock); 22682 usesrc_cli_ill->ill_state_flags &= ~ILL_CHANGING; 22683 mutex_exit(&usesrc_cli_ill->ill_lock); 22684 } 22685 if (ipsq != NULL) 22686 ipsq_exit(ipsq, B_TRUE, B_TRUE); 22687 /* The refrele on the lifr_name ipif is done by ip_process_ioctl */ 22688 ill_refrele(usesrc_ill); 22689 return (err); 22690 } 22691 22692 /* 22693 * comparison function used by avl. 22694 */ 22695 static int 22696 ill_phyint_compare_index(const void *index_ptr, const void *phyip) 22697 { 22698 22699 uint_t index; 22700 22701 ASSERT(phyip != NULL && index_ptr != NULL); 22702 22703 index = *((uint_t *)index_ptr); 22704 /* 22705 * let the phyint with the lowest index be on top. 22706 */ 22707 if (((phyint_t *)phyip)->phyint_ifindex < index) 22708 return (1); 22709 if (((phyint_t *)phyip)->phyint_ifindex > index) 22710 return (-1); 22711 return (0); 22712 } 22713 22714 /* 22715 * comparison function used by avl. 22716 */ 22717 static int 22718 ill_phyint_compare_name(const void *name_ptr, const void *phyip) 22719 { 22720 ill_t *ill; 22721 int res = 0; 22722 22723 ASSERT(phyip != NULL && name_ptr != NULL); 22724 22725 if (((phyint_t *)phyip)->phyint_illv4) 22726 ill = ((phyint_t *)phyip)->phyint_illv4; 22727 else 22728 ill = ((phyint_t *)phyip)->phyint_illv6; 22729 ASSERT(ill != NULL); 22730 22731 res = strcmp(ill->ill_name, (char *)name_ptr); 22732 if (res > 0) 22733 return (1); 22734 else if (res < 0) 22735 return (-1); 22736 return (0); 22737 } 22738 /* 22739 * This function is called from ill_delete when the ill is being 22740 * unplumbed. We remove the reference from the phyint and we also 22741 * free the phyint when there are no more references to it. 22742 */ 22743 static void 22744 ill_phyint_free(ill_t *ill) 22745 { 22746 phyint_t *phyi; 22747 phyint_t *next_phyint; 22748 ipsq_t *cur_ipsq; 22749 ip_stack_t *ipst = ill->ill_ipst; 22750 22751 ASSERT(ill->ill_phyint != NULL); 22752 22753 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 22754 phyi = ill->ill_phyint; 22755 ill->ill_phyint = NULL; 22756 /* 22757 * ill_init allocates a phyint always to store the copy 22758 * of flags relevant to phyint. At that point in time, we could 22759 * not assign the name and hence phyint_illv4/v6 could not be 22760 * initialized. Later in ipif_set_values, we assign the name to 22761 * the ill, at which point in time we assign phyint_illv4/v6. 22762 * Thus we don't rely on phyint_illv6 to be initialized always. 22763 */ 22764 if (ill->ill_flags & ILLF_IPV6) { 22765 phyi->phyint_illv6 = NULL; 22766 } else { 22767 phyi->phyint_illv4 = NULL; 22768 } 22769 /* 22770 * ipif_down removes it from the group when the last ipif goes 22771 * down. 22772 */ 22773 ASSERT(ill->ill_group == NULL); 22774 22775 if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) 22776 return; 22777 22778 /* 22779 * Make sure this phyint was put in the list. 22780 */ 22781 if (phyi->phyint_ifindex > 0) { 22782 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 22783 phyi); 22784 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name, 22785 phyi); 22786 } 22787 /* 22788 * remove phyint from the ipsq list. 22789 */ 22790 cur_ipsq = phyi->phyint_ipsq; 22791 if (phyi == cur_ipsq->ipsq_phyint_list) { 22792 cur_ipsq->ipsq_phyint_list = phyi->phyint_ipsq_next; 22793 } else { 22794 next_phyint = cur_ipsq->ipsq_phyint_list; 22795 while (next_phyint != NULL) { 22796 if (next_phyint->phyint_ipsq_next == phyi) { 22797 next_phyint->phyint_ipsq_next = 22798 phyi->phyint_ipsq_next; 22799 break; 22800 } 22801 next_phyint = next_phyint->phyint_ipsq_next; 22802 } 22803 ASSERT(next_phyint != NULL); 22804 } 22805 IPSQ_DEC_REF(cur_ipsq, ipst); 22806 22807 if (phyi->phyint_groupname_len != 0) { 22808 ASSERT(phyi->phyint_groupname != NULL); 22809 mi_free(phyi->phyint_groupname); 22810 } 22811 mi_free(phyi); 22812 } 22813 22814 /* 22815 * Attach the ill to the phyint structure which can be shared by both 22816 * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This 22817 * function is called from ipif_set_values and ill_lookup_on_name (for 22818 * loopback) where we know the name of the ill. We lookup the ill and if 22819 * there is one present already with the name use that phyint. Otherwise 22820 * reuse the one allocated by ill_init. 22821 */ 22822 static void 22823 ill_phyint_reinit(ill_t *ill) 22824 { 22825 boolean_t isv6 = ill->ill_isv6; 22826 phyint_t *phyi_old; 22827 phyint_t *phyi; 22828 avl_index_t where = 0; 22829 ill_t *ill_other = NULL; 22830 ipsq_t *ipsq; 22831 ip_stack_t *ipst = ill->ill_ipst; 22832 22833 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock)); 22834 22835 phyi_old = ill->ill_phyint; 22836 ASSERT(isv6 || (phyi_old->phyint_illv4 == ill && 22837 phyi_old->phyint_illv6 == NULL)); 22838 ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill && 22839 phyi_old->phyint_illv4 == NULL)); 22840 ASSERT(phyi_old->phyint_ifindex == 0); 22841 22842 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name, 22843 ill->ill_name, &where); 22844 22845 /* 22846 * 1. We grabbed the ill_g_lock before inserting this ill into 22847 * the global list of ills. So no other thread could have located 22848 * this ill and hence the ipsq of this ill is guaranteed to be empty. 22849 * 2. Now locate the other protocol instance of this ill. 22850 * 3. Now grab both ill locks in the right order, and the phyint lock of 22851 * the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq 22852 * of neither ill can change. 22853 * 4. Merge the phyint and thus the ipsq as well of this ill onto the 22854 * other ill. 22855 * 5. Release all locks. 22856 */ 22857 22858 /* 22859 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if 22860 * we are initializing IPv4. 22861 */ 22862 if (phyi != NULL) { 22863 ill_other = (isv6) ? phyi->phyint_illv4 : 22864 phyi->phyint_illv6; 22865 ASSERT(ill_other->ill_phyint != NULL); 22866 ASSERT((isv6 && !ill_other->ill_isv6) || 22867 (!isv6 && ill_other->ill_isv6)); 22868 GRAB_ILL_LOCKS(ill, ill_other); 22869 /* 22870 * We are potentially throwing away phyint_flags which 22871 * could be different from the one that we obtain from 22872 * ill_other->ill_phyint. But it is okay as we are assuming 22873 * that the state maintained within IP is correct. 22874 */ 22875 mutex_enter(&phyi->phyint_lock); 22876 if (isv6) { 22877 ASSERT(phyi->phyint_illv6 == NULL); 22878 phyi->phyint_illv6 = ill; 22879 } else { 22880 ASSERT(phyi->phyint_illv4 == NULL); 22881 phyi->phyint_illv4 = ill; 22882 } 22883 /* 22884 * This is a new ill, currently undergoing SLIFNAME 22885 * So we could not have joined an IPMP group until now. 22886 */ 22887 ASSERT(phyi_old->phyint_ipsq_next == NULL && 22888 phyi_old->phyint_groupname == NULL); 22889 22890 /* 22891 * This phyi_old is going away. Decref ipsq_refs and 22892 * assert it is zero. The ipsq itself will be freed in 22893 * ipsq_exit 22894 */ 22895 ipsq = phyi_old->phyint_ipsq; 22896 IPSQ_DEC_REF(ipsq, ipst); 22897 ASSERT(ipsq->ipsq_refs == 0); 22898 /* Get the singleton phyint out of the ipsq list */ 22899 ASSERT(phyi_old->phyint_ipsq_next == NULL); 22900 ipsq->ipsq_phyint_list = NULL; 22901 phyi_old->phyint_illv4 = NULL; 22902 phyi_old->phyint_illv6 = NULL; 22903 mi_free(phyi_old); 22904 } else { 22905 mutex_enter(&ill->ill_lock); 22906 /* 22907 * We don't need to acquire any lock, since 22908 * the ill is not yet visible globally and we 22909 * have not yet released the ill_g_lock. 22910 */ 22911 phyi = phyi_old; 22912 mutex_enter(&phyi->phyint_lock); 22913 /* XXX We need a recovery strategy here. */ 22914 if (!phyint_assign_ifindex(phyi, ipst)) 22915 cmn_err(CE_PANIC, "phyint_assign_ifindex() failed"); 22916 22917 /* No IPMP group yet, thus the hook uses the ifindex */ 22918 phyi->phyint_hook_ifindex = phyi->phyint_ifindex; 22919 22920 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name, 22921 (void *)phyi, where); 22922 22923 (void) avl_find(&ipst->ips_phyint_g_list-> 22924 phyint_list_avl_by_index, 22925 &phyi->phyint_ifindex, &where); 22926 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 22927 (void *)phyi, where); 22928 } 22929 22930 /* 22931 * Reassigning ill_phyint automatically reassigns the ipsq also. 22932 * pending mp is not affected because that is per ill basis. 22933 */ 22934 ill->ill_phyint = phyi; 22935 22936 /* 22937 * Keep the index on ipif_orig_index to be used by FAILOVER. 22938 * We do this here as when the first ipif was allocated, 22939 * ipif_allocate does not know the right interface index. 22940 */ 22941 22942 ill->ill_ipif->ipif_orig_ifindex = ill->ill_phyint->phyint_ifindex; 22943 /* 22944 * Now that the phyint's ifindex has been assigned, complete the 22945 * remaining 22946 */ 22947 22948 ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex; 22949 if (ill->ill_isv6) { 22950 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex = 22951 ill->ill_phyint->phyint_ifindex; 22952 } 22953 22954 /* 22955 * Generate an event within the hooks framework to indicate that 22956 * a new interface has just been added to IP. For this event to 22957 * be generated, the network interface must, at least, have an 22958 * ifindex assigned to it. 22959 * 22960 * This needs to be run inside the ill_g_lock perimeter to ensure 22961 * that the ordering of delivered events to listeners matches the 22962 * order of them in the kernel. 22963 * 22964 * This function could be called from ill_lookup_on_name. In that case 22965 * the interface is loopback "lo", which will not generate a NIC event. 22966 */ 22967 if (ill->ill_name_length <= 2 || 22968 ill->ill_name[0] != 'l' || ill->ill_name[1] != 'o') { 22969 /* 22970 * Generate nic plumb event for ill_name even if 22971 * ipmp_hook_emulation is set. That avoids generating events 22972 * for the ill_names should ipmp_hook_emulation be turned on 22973 * later. 22974 */ 22975 ill_nic_info_plumb(ill, B_FALSE); 22976 } 22977 RELEASE_ILL_LOCKS(ill, ill_other); 22978 mutex_exit(&phyi->phyint_lock); 22979 } 22980 22981 /* 22982 * Allocate a NE_PLUMB nic info event and store in the ill. 22983 * If 'group' is set we do it for the group name, otherwise the ill name. 22984 * It will be sent when we leave the ipsq. 22985 */ 22986 void 22987 ill_nic_info_plumb(ill_t *ill, boolean_t group) 22988 { 22989 phyint_t *phyi = ill->ill_phyint; 22990 ip_stack_t *ipst = ill->ill_ipst; 22991 hook_nic_event_t *info; 22992 char *name; 22993 int namelen; 22994 22995 ASSERT(MUTEX_HELD(&ill->ill_lock)); 22996 22997 if ((info = ill->ill_nic_event_info) != NULL) { 22998 ip2dbg(("ill_nic_info_plumb: unexpected nic event %d " 22999 "attached for %s\n", info->hne_event, 23000 ill->ill_name)); 23001 if (info->hne_data != NULL) 23002 kmem_free(info->hne_data, info->hne_datalen); 23003 kmem_free(info, sizeof (hook_nic_event_t)); 23004 ill->ill_nic_event_info = NULL; 23005 } 23006 23007 info = kmem_alloc(sizeof (hook_nic_event_t), KM_NOSLEEP); 23008 if (info == NULL) { 23009 ip2dbg(("ill_nic_info_plumb: could not attach PLUMB nic " 23010 "event information for %s (ENOMEM)\n", 23011 ill->ill_name)); 23012 return; 23013 } 23014 23015 if (group) { 23016 ASSERT(phyi->phyint_groupname_len != 0); 23017 namelen = phyi->phyint_groupname_len; 23018 name = phyi->phyint_groupname; 23019 } else { 23020 namelen = ill->ill_name_length; 23021 name = ill->ill_name; 23022 } 23023 23024 info->hne_nic = phyi->phyint_hook_ifindex; 23025 info->hne_lif = 0; 23026 info->hne_event = NE_PLUMB; 23027 info->hne_family = ill->ill_isv6 ? 23028 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data; 23029 23030 info->hne_data = kmem_alloc(namelen, KM_NOSLEEP); 23031 if (info->hne_data != NULL) { 23032 info->hne_datalen = namelen; 23033 bcopy(name, info->hne_data, info->hne_datalen); 23034 } else { 23035 ip2dbg(("ill_nic_info_plumb: could not attach " 23036 "name information for PLUMB nic event " 23037 "of %s (ENOMEM)\n", name)); 23038 kmem_free(info, sizeof (hook_nic_event_t)); 23039 info = NULL; 23040 } 23041 ill->ill_nic_event_info = info; 23042 } 23043 23044 /* 23045 * Unhook the nic event message from the ill and enqueue it 23046 * into the nic event taskq. 23047 */ 23048 void 23049 ill_nic_info_dispatch(ill_t *ill) 23050 { 23051 hook_nic_event_t *info; 23052 23053 ASSERT(MUTEX_HELD(&ill->ill_lock)); 23054 23055 if ((info = ill->ill_nic_event_info) != NULL) { 23056 if (ddi_taskq_dispatch(eventq_queue_nic, 23057 ip_ne_queue_func, info, DDI_SLEEP) == DDI_FAILURE) { 23058 ip2dbg(("ill_nic_info_dispatch: " 23059 "ddi_taskq_dispatch failed\n")); 23060 if (info->hne_data != NULL) 23061 kmem_free(info->hne_data, info->hne_datalen); 23062 kmem_free(info, sizeof (hook_nic_event_t)); 23063 } 23064 ill->ill_nic_event_info = NULL; 23065 } 23066 } 23067 23068 /* 23069 * Notify any downstream modules of the name of this interface. 23070 * An M_IOCTL is used even though we don't expect a successful reply. 23071 * Any reply message from the driver (presumably an M_IOCNAK) will 23072 * eventually get discarded somewhere upstream. The message format is 23073 * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig 23074 * to IP. 23075 */ 23076 static void 23077 ip_ifname_notify(ill_t *ill, queue_t *q) 23078 { 23079 mblk_t *mp1, *mp2; 23080 struct iocblk *iocp; 23081 struct lifreq *lifr; 23082 23083 mp1 = mkiocb(SIOCSLIFNAME); 23084 if (mp1 == NULL) 23085 return; 23086 mp2 = allocb(sizeof (struct lifreq), BPRI_HI); 23087 if (mp2 == NULL) { 23088 freeb(mp1); 23089 return; 23090 } 23091 23092 mp1->b_cont = mp2; 23093 iocp = (struct iocblk *)mp1->b_rptr; 23094 iocp->ioc_count = sizeof (struct lifreq); 23095 23096 lifr = (struct lifreq *)mp2->b_rptr; 23097 mp2->b_wptr += sizeof (struct lifreq); 23098 bzero(lifr, sizeof (struct lifreq)); 23099 23100 (void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ); 23101 lifr->lifr_ppa = ill->ill_ppa; 23102 lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)); 23103 23104 putnext(q, mp1); 23105 } 23106 23107 static int 23108 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q) 23109 { 23110 int err; 23111 ip_stack_t *ipst = ill->ill_ipst; 23112 23113 /* Set the obsolete NDD per-interface forwarding name. */ 23114 err = ill_set_ndd_name(ill); 23115 if (err != 0) { 23116 cmn_err(CE_WARN, "ipif_set_values: ill_set_ndd_name (%d)\n", 23117 err); 23118 } 23119 23120 /* Tell downstream modules where they are. */ 23121 ip_ifname_notify(ill, q); 23122 23123 /* 23124 * ill_dl_phys returns EINPROGRESS in the usual case. 23125 * Error cases are ENOMEM ... 23126 */ 23127 err = ill_dl_phys(ill, ipif, mp, q); 23128 23129 /* 23130 * If there is no IRE expiration timer running, get one started. 23131 * igmp and mld timers will be triggered by the first multicast 23132 */ 23133 if (ipst->ips_ip_ire_expire_id == 0) { 23134 /* 23135 * acquire the lock and check again. 23136 */ 23137 mutex_enter(&ipst->ips_ip_trash_timer_lock); 23138 if (ipst->ips_ip_ire_expire_id == 0) { 23139 ipst->ips_ip_ire_expire_id = timeout( 23140 ip_trash_timer_expire, ipst, 23141 MSEC_TO_TICK(ipst->ips_ip_timer_interval)); 23142 } 23143 mutex_exit(&ipst->ips_ip_trash_timer_lock); 23144 } 23145 23146 if (ill->ill_isv6) { 23147 mutex_enter(&ipst->ips_mld_slowtimeout_lock); 23148 if (ipst->ips_mld_slowtimeout_id == 0) { 23149 ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo, 23150 (void *)ipst, 23151 MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL)); 23152 } 23153 mutex_exit(&ipst->ips_mld_slowtimeout_lock); 23154 } else { 23155 mutex_enter(&ipst->ips_igmp_slowtimeout_lock); 23156 if (ipst->ips_igmp_slowtimeout_id == 0) { 23157 ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo, 23158 (void *)ipst, 23159 MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL)); 23160 } 23161 mutex_exit(&ipst->ips_igmp_slowtimeout_lock); 23162 } 23163 23164 return (err); 23165 } 23166 23167 /* 23168 * Common routine for ppa and ifname setting. Should be called exclusive. 23169 * 23170 * Returns EINPROGRESS when mp has been consumed by queueing it on 23171 * ill_pending_mp and the ioctl will complete in ip_rput. 23172 * 23173 * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return 23174 * the new name and new ppa in lifr_name and lifr_ppa respectively. 23175 * For SLIFNAME, we pass these values back to the userland. 23176 */ 23177 static int 23178 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr) 23179 { 23180 ill_t *ill; 23181 ipif_t *ipif; 23182 ipsq_t *ipsq; 23183 char *ppa_ptr; 23184 char *old_ptr; 23185 char old_char; 23186 int error; 23187 ip_stack_t *ipst; 23188 23189 ip1dbg(("ipif_set_values: interface %s\n", interf_name)); 23190 ASSERT(q->q_next != NULL); 23191 ASSERT(interf_name != NULL); 23192 23193 ill = (ill_t *)q->q_ptr; 23194 ipst = ill->ill_ipst; 23195 23196 ASSERT(ill->ill_ipst != NULL); 23197 ASSERT(ill->ill_name[0] == '\0'); 23198 ASSERT(IAM_WRITER_ILL(ill)); 23199 ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ); 23200 ASSERT(ill->ill_ppa == UINT_MAX); 23201 23202 /* The ppa is sent down by ifconfig or is chosen */ 23203 if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) { 23204 return (EINVAL); 23205 } 23206 23207 /* 23208 * make sure ppa passed in is same as ppa in the name. 23209 * This check is not made when ppa == UINT_MAX in that case ppa 23210 * in the name could be anything. System will choose a ppa and 23211 * update new_ppa_ptr and inter_name to contain the choosen ppa. 23212 */ 23213 if (*new_ppa_ptr != UINT_MAX) { 23214 /* stoi changes the pointer */ 23215 old_ptr = ppa_ptr; 23216 /* 23217 * ifconfig passed in 0 for the ppa for DLPI 1 style devices 23218 * (they don't have an externally visible ppa). We assign one 23219 * here so that we can manage the interface. Note that in 23220 * the past this value was always 0 for DLPI 1 drivers. 23221 */ 23222 if (*new_ppa_ptr == 0) 23223 *new_ppa_ptr = stoi(&old_ptr); 23224 else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr)) 23225 return (EINVAL); 23226 } 23227 /* 23228 * terminate string before ppa 23229 * save char at that location. 23230 */ 23231 old_char = ppa_ptr[0]; 23232 ppa_ptr[0] = '\0'; 23233 23234 ill->ill_ppa = *new_ppa_ptr; 23235 /* 23236 * Finish as much work now as possible before calling ill_glist_insert 23237 * which makes the ill globally visible and also merges it with the 23238 * other protocol instance of this phyint. The remaining work is 23239 * done after entering the ipsq which may happen sometime later. 23240 * ill_set_ndd_name occurs after the ill has been made globally visible. 23241 */ 23242 ipif = ill->ill_ipif; 23243 23244 /* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */ 23245 ipif_assign_seqid(ipif); 23246 23247 if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6))) 23248 ill->ill_flags |= ILLF_IPV4; 23249 23250 ASSERT(ipif->ipif_next == NULL); /* Only one ipif on ill */ 23251 ASSERT((ipif->ipif_flags & IPIF_UP) == 0); 23252 23253 if (ill->ill_flags & ILLF_IPV6) { 23254 23255 ill->ill_isv6 = B_TRUE; 23256 if (ill->ill_rq != NULL) { 23257 ill->ill_rq->q_qinfo = &rinit_ipv6; 23258 ill->ill_wq->q_qinfo = &winit_ipv6; 23259 } 23260 23261 /* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */ 23262 ipif->ipif_v6lcl_addr = ipv6_all_zeros; 23263 ipif->ipif_v6src_addr = ipv6_all_zeros; 23264 ipif->ipif_v6subnet = ipv6_all_zeros; 23265 ipif->ipif_v6net_mask = ipv6_all_zeros; 23266 ipif->ipif_v6brd_addr = ipv6_all_zeros; 23267 ipif->ipif_v6pp_dst_addr = ipv6_all_zeros; 23268 /* 23269 * point-to-point or Non-mulicast capable 23270 * interfaces won't do NUD unless explicitly 23271 * configured to do so. 23272 */ 23273 if (ipif->ipif_flags & IPIF_POINTOPOINT || 23274 !(ill->ill_flags & ILLF_MULTICAST)) { 23275 ill->ill_flags |= ILLF_NONUD; 23276 } 23277 /* Make sure IPv4 specific flag is not set on IPv6 if */ 23278 if (ill->ill_flags & ILLF_NOARP) { 23279 /* 23280 * Note: xresolv interfaces will eventually need 23281 * NOARP set here as well, but that will require 23282 * those external resolvers to have some 23283 * knowledge of that flag and act appropriately. 23284 * Not to be changed at present. 23285 */ 23286 ill->ill_flags &= ~ILLF_NOARP; 23287 } 23288 /* 23289 * Set the ILLF_ROUTER flag according to the global 23290 * IPv6 forwarding policy. 23291 */ 23292 if (ipst->ips_ipv6_forward != 0) 23293 ill->ill_flags |= ILLF_ROUTER; 23294 } else if (ill->ill_flags & ILLF_IPV4) { 23295 ill->ill_isv6 = B_FALSE; 23296 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr); 23297 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6src_addr); 23298 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet); 23299 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask); 23300 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr); 23301 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr); 23302 /* 23303 * Set the ILLF_ROUTER flag according to the global 23304 * IPv4 forwarding policy. 23305 */ 23306 if (ipst->ips_ip_g_forward != 0) 23307 ill->ill_flags |= ILLF_ROUTER; 23308 } 23309 23310 ASSERT(ill->ill_phyint != NULL); 23311 23312 /* 23313 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will 23314 * be completed in ill_glist_insert -> ill_phyint_reinit 23315 */ 23316 if (!ill_allocate_mibs(ill)) 23317 return (ENOMEM); 23318 23319 /* 23320 * Pick a default sap until we get the DL_INFO_ACK back from 23321 * the driver. 23322 */ 23323 if (ill->ill_sap == 0) { 23324 if (ill->ill_isv6) 23325 ill->ill_sap = IP6_DL_SAP; 23326 else 23327 ill->ill_sap = IP_DL_SAP; 23328 } 23329 23330 ill->ill_ifname_pending = 1; 23331 ill->ill_ifname_pending_err = 0; 23332 23333 ill_refhold(ill); 23334 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER); 23335 if ((error = ill_glist_insert(ill, interf_name, 23336 (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) { 23337 ill->ill_ppa = UINT_MAX; 23338 ill->ill_name[0] = '\0'; 23339 /* 23340 * undo null termination done above. 23341 */ 23342 ppa_ptr[0] = old_char; 23343 rw_exit(&ipst->ips_ill_g_lock); 23344 ill_refrele(ill); 23345 return (error); 23346 } 23347 23348 ASSERT(ill->ill_name_length <= LIFNAMSIZ); 23349 23350 /* 23351 * When we return the buffer pointed to by interf_name should contain 23352 * the same name as in ill_name. 23353 * If a ppa was choosen by the system (ppa passed in was UINT_MAX) 23354 * the buffer pointed to by new_ppa_ptr would not contain the right ppa 23355 * so copy full name and update the ppa ptr. 23356 * When ppa passed in != UINT_MAX all values are correct just undo 23357 * null termination, this saves a bcopy. 23358 */ 23359 if (*new_ppa_ptr == UINT_MAX) { 23360 bcopy(ill->ill_name, interf_name, ill->ill_name_length); 23361 *new_ppa_ptr = ill->ill_ppa; 23362 } else { 23363 /* 23364 * undo null termination done above. 23365 */ 23366 ppa_ptr[0] = old_char; 23367 } 23368 23369 /* Let SCTP know about this ILL */ 23370 sctp_update_ill(ill, SCTP_ILL_INSERT); 23371 23372 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_reprocess_ioctl, NEW_OP, 23373 B_TRUE); 23374 23375 rw_exit(&ipst->ips_ill_g_lock); 23376 ill_refrele(ill); 23377 if (ipsq == NULL) 23378 return (EINPROGRESS); 23379 23380 /* 23381 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq. 23382 */ 23383 if (ipsq->ipsq_current_ipif == NULL) 23384 ipsq_current_start(ipsq, ipif, SIOCSLIFNAME); 23385 else 23386 ASSERT(ipsq->ipsq_current_ipif == ipif); 23387 23388 error = ipif_set_values_tail(ill, ipif, mp, q); 23389 ipsq_exit(ipsq, B_TRUE, B_TRUE); 23390 if (error != 0 && error != EINPROGRESS) { 23391 /* 23392 * restore previous values 23393 */ 23394 ill->ill_isv6 = B_FALSE; 23395 } 23396 return (error); 23397 } 23398 23399 23400 void 23401 ipif_init(ip_stack_t *ipst) 23402 { 23403 hrtime_t hrt; 23404 int i; 23405 23406 /* 23407 * Can't call drv_getparm here as it is too early in the boot. 23408 * As we use ipif_src_random just for picking a different 23409 * source address everytime, this need not be really random. 23410 */ 23411 hrt = gethrtime(); 23412 ipst->ips_ipif_src_random = 23413 ((hrt >> 32) & 0xffffffff) * (hrt & 0xffffffff); 23414 23415 for (i = 0; i < MAX_G_HEADS; i++) { 23416 ipst->ips_ill_g_heads[i].ill_g_list_head = 23417 (ill_if_t *)&ipst->ips_ill_g_heads[i]; 23418 ipst->ips_ill_g_heads[i].ill_g_list_tail = 23419 (ill_if_t *)&ipst->ips_ill_g_heads[i]; 23420 } 23421 23422 avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, 23423 ill_phyint_compare_index, 23424 sizeof (phyint_t), 23425 offsetof(struct phyint, phyint_avl_by_index)); 23426 avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name, 23427 ill_phyint_compare_name, 23428 sizeof (phyint_t), 23429 offsetof(struct phyint, phyint_avl_by_name)); 23430 } 23431 23432 /* 23433 * This is called by ip_rt_add when src_addr value is other than zero. 23434 * src_addr signifies the source address of the incoming packet. For 23435 * reverse tunnel route we need to create a source addr based routing 23436 * table. This routine creates ip_mrtun_table if it's empty and then 23437 * it adds the route entry hashed by source address. It verifies that 23438 * the outgoing interface is always a non-resolver interface (tunnel). 23439 */ 23440 int 23441 ip_mrtun_rt_add(ipaddr_t in_src_addr, int flags, ipif_t *ipif_arg, 23442 ipif_t *src_ipif, ire_t **ire_arg, queue_t *q, mblk_t *mp, ipsq_func_t func, 23443 ip_stack_t *ipst) 23444 { 23445 ire_t *ire; 23446 ire_t *save_ire; 23447 ipif_t *ipif; 23448 ill_t *in_ill = NULL; 23449 ill_t *out_ill; 23450 queue_t *stq; 23451 mblk_t *dlureq_mp; 23452 int error; 23453 23454 if (ire_arg != NULL) 23455 *ire_arg = NULL; 23456 ASSERT(in_src_addr != INADDR_ANY); 23457 23458 ipif = ipif_arg; 23459 if (ipif != NULL) { 23460 out_ill = ipif->ipif_ill; 23461 } else { 23462 ip1dbg(("ip_mrtun_rt_add: ipif is NULL\n")); 23463 return (EINVAL); 23464 } 23465 23466 if (src_ipif == NULL) { 23467 ip1dbg(("ip_mrtun_rt_add: src_ipif is NULL\n")); 23468 return (EINVAL); 23469 } 23470 in_ill = src_ipif->ipif_ill; 23471 23472 /* 23473 * Check for duplicates. We don't need to 23474 * match out_ill, because the uniqueness of 23475 * a route is only dependent on src_addr and 23476 * in_ill. 23477 */ 23478 ire = ire_mrtun_lookup(in_src_addr, in_ill); 23479 if (ire != NULL) { 23480 ire_refrele(ire); 23481 return (EEXIST); 23482 } 23483 if (ipif->ipif_net_type != IRE_IF_NORESOLVER) { 23484 ip2dbg(("ip_mrtun_rt_add: outgoing interface is type %d\n", 23485 ipif->ipif_net_type)); 23486 return (EINVAL); 23487 } 23488 23489 stq = ipif->ipif_wq; 23490 ASSERT(stq != NULL); 23491 23492 /* 23493 * The outgoing interface must be non-resolver 23494 * interface. 23495 */ 23496 dlureq_mp = ill_dlur_gen(NULL, 23497 out_ill->ill_phys_addr_length, out_ill->ill_sap, 23498 out_ill->ill_sap_length); 23499 23500 if (dlureq_mp == NULL) { 23501 ip1dbg(("ip_newroute: dlureq_mp NULL\n")); 23502 return (ENOMEM); 23503 } 23504 23505 /* Create the IRE. */ 23506 23507 ire = ire_create( 23508 NULL, /* Zero dst addr */ 23509 NULL, /* Zero mask */ 23510 NULL, /* Zero gateway addr */ 23511 NULL, /* Zero ipif_src addr */ 23512 (uint8_t *)&in_src_addr, /* in_src-addr */ 23513 &ipif->ipif_mtu, 23514 NULL, 23515 NULL, /* rfq */ 23516 stq, 23517 IRE_MIPRTUN, 23518 dlureq_mp, 23519 ipif, 23520 in_ill, 23521 0, 23522 0, 23523 0, 23524 flags, 23525 &ire_uinfo_null, 23526 NULL, 23527 NULL, 23528 ipst); 23529 23530 if (ire == NULL) { 23531 freeb(dlureq_mp); 23532 return (ENOMEM); 23533 } 23534 ip2dbg(("ip_mrtun_rt_add: mrtun route is created with type %d\n", 23535 ire->ire_type)); 23536 save_ire = ire; 23537 ASSERT(save_ire != NULL); 23538 error = ire_add_mrtun(&ire, q, mp, func); 23539 /* 23540 * If ire_add_mrtun() failed, the ire passed in was freed 23541 * so there is no need to do so here. 23542 */ 23543 if (error != 0) { 23544 return (error); 23545 } 23546 23547 /* Duplicate check */ 23548 if (ire != save_ire) { 23549 /* route already exists by now */ 23550 ire_refrele(ire); 23551 return (EEXIST); 23552 } 23553 23554 if (ire_arg != NULL) { 23555 /* 23556 * Store the ire that was just added. the caller 23557 * ip_rts_request responsible for doing ire_refrele() 23558 * on it. 23559 */ 23560 *ire_arg = ire; 23561 } else { 23562 ire_refrele(ire); /* held in ire_add_mrtun */ 23563 } 23564 23565 return (0); 23566 } 23567 23568 /* 23569 * It is called by ip_rt_delete() only when mipagent requests to delete 23570 * a reverse tunnel route that was added by ip_mrtun_rt_add() before. 23571 */ 23572 23573 int 23574 ip_mrtun_rt_delete(ipaddr_t in_src_addr, ipif_t *src_ipif) 23575 { 23576 ire_t *ire = NULL; 23577 23578 if (in_src_addr == INADDR_ANY) 23579 return (EINVAL); 23580 if (src_ipif == NULL) 23581 return (EINVAL); 23582 23583 /* search if this route exists in the ip_mrtun_table */ 23584 ire = ire_mrtun_lookup(in_src_addr, src_ipif->ipif_ill); 23585 if (ire == NULL) { 23586 ip2dbg(("ip_mrtun_rt_delete: ire not found\n")); 23587 return (ESRCH); 23588 } 23589 ire_delete(ire); 23590 ire_refrele(ire); 23591 return (0); 23592 } 23593 23594 /* 23595 * Lookup the ipif corresponding to the onlink destination address. For 23596 * point-to-point interfaces, it matches with remote endpoint destination 23597 * address. For point-to-multipoint interfaces it only tries to match the 23598 * destination with the interface's subnet address. The longest, most specific 23599 * match is found to take care of such rare network configurations like - 23600 * le0: 129.146.1.1/16 23601 * le1: 129.146.2.2/24 23602 * It is used only by SO_DONTROUTE at the moment. 23603 */ 23604 ipif_t * 23605 ipif_lookup_onlink_addr(ipaddr_t addr, zoneid_t zoneid, ip_stack_t *ipst) 23606 { 23607 ipif_t *ipif, *best_ipif; 23608 ill_t *ill; 23609 ill_walk_context_t ctx; 23610 23611 ASSERT(zoneid != ALL_ZONES); 23612 best_ipif = NULL; 23613 23614 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 23615 ill = ILL_START_WALK_V4(&ctx, ipst); 23616 for (; ill != NULL; ill = ill_next(&ctx, ill)) { 23617 mutex_enter(&ill->ill_lock); 23618 for (ipif = ill->ill_ipif; ipif != NULL; 23619 ipif = ipif->ipif_next) { 23620 if (!IPIF_CAN_LOOKUP(ipif)) 23621 continue; 23622 if (ipif->ipif_zoneid != zoneid && 23623 ipif->ipif_zoneid != ALL_ZONES) 23624 continue; 23625 /* 23626 * Point-to-point case. Look for exact match with 23627 * destination address. 23628 */ 23629 if (ipif->ipif_flags & IPIF_POINTOPOINT) { 23630 if (ipif->ipif_pp_dst_addr == addr) { 23631 ipif_refhold_locked(ipif); 23632 mutex_exit(&ill->ill_lock); 23633 rw_exit(&ipst->ips_ill_g_lock); 23634 if (best_ipif != NULL) 23635 ipif_refrele(best_ipif); 23636 return (ipif); 23637 } 23638 } else if (ipif->ipif_subnet == (addr & 23639 ipif->ipif_net_mask)) { 23640 /* 23641 * Point-to-multipoint case. Looping through to 23642 * find the most specific match. If there are 23643 * multiple best match ipif's then prefer ipif's 23644 * that are UP. If there is only one best match 23645 * ipif and it is DOWN we must still return it. 23646 */ 23647 if ((best_ipif == NULL) || 23648 (ipif->ipif_net_mask > 23649 best_ipif->ipif_net_mask) || 23650 ((ipif->ipif_net_mask == 23651 best_ipif->ipif_net_mask) && 23652 ((ipif->ipif_flags & IPIF_UP) && 23653 (!(best_ipif->ipif_flags & IPIF_UP))))) { 23654 ipif_refhold_locked(ipif); 23655 mutex_exit(&ill->ill_lock); 23656 rw_exit(&ipst->ips_ill_g_lock); 23657 if (best_ipif != NULL) 23658 ipif_refrele(best_ipif); 23659 best_ipif = ipif; 23660 rw_enter(&ipst->ips_ill_g_lock, 23661 RW_READER); 23662 mutex_enter(&ill->ill_lock); 23663 } 23664 } 23665 } 23666 mutex_exit(&ill->ill_lock); 23667 } 23668 rw_exit(&ipst->ips_ill_g_lock); 23669 return (best_ipif); 23670 } 23671 23672 23673 /* 23674 * Save enough information so that we can recreate the IRE if 23675 * the interface goes down and then up. 23676 */ 23677 static void 23678 ipif_save_ire(ipif_t *ipif, ire_t *ire) 23679 { 23680 mblk_t *save_mp; 23681 23682 save_mp = allocb(sizeof (ifrt_t), BPRI_MED); 23683 if (save_mp != NULL) { 23684 ifrt_t *ifrt; 23685 23686 save_mp->b_wptr += sizeof (ifrt_t); 23687 ifrt = (ifrt_t *)save_mp->b_rptr; 23688 bzero(ifrt, sizeof (ifrt_t)); 23689 ifrt->ifrt_type = ire->ire_type; 23690 ifrt->ifrt_addr = ire->ire_addr; 23691 ifrt->ifrt_gateway_addr = ire->ire_gateway_addr; 23692 ifrt->ifrt_src_addr = ire->ire_src_addr; 23693 ifrt->ifrt_mask = ire->ire_mask; 23694 ifrt->ifrt_flags = ire->ire_flags; 23695 ifrt->ifrt_max_frag = ire->ire_max_frag; 23696 mutex_enter(&ipif->ipif_saved_ire_lock); 23697 save_mp->b_cont = ipif->ipif_saved_ire_mp; 23698 ipif->ipif_saved_ire_mp = save_mp; 23699 ipif->ipif_saved_ire_cnt++; 23700 mutex_exit(&ipif->ipif_saved_ire_lock); 23701 } 23702 } 23703 23704 23705 static void 23706 ipif_remove_ire(ipif_t *ipif, ire_t *ire) 23707 { 23708 mblk_t **mpp; 23709 mblk_t *mp; 23710 ifrt_t *ifrt; 23711 23712 /* Remove from ipif_saved_ire_mp list if it is there */ 23713 mutex_enter(&ipif->ipif_saved_ire_lock); 23714 for (mpp = &ipif->ipif_saved_ire_mp; *mpp != NULL; 23715 mpp = &(*mpp)->b_cont) { 23716 /* 23717 * On a given ipif, the triple of address, gateway and 23718 * mask is unique for each saved IRE (in the case of 23719 * ordinary interface routes, the gateway address is 23720 * all-zeroes). 23721 */ 23722 mp = *mpp; 23723 ifrt = (ifrt_t *)mp->b_rptr; 23724 if (ifrt->ifrt_addr == ire->ire_addr && 23725 ifrt->ifrt_gateway_addr == ire->ire_gateway_addr && 23726 ifrt->ifrt_mask == ire->ire_mask) { 23727 *mpp = mp->b_cont; 23728 ipif->ipif_saved_ire_cnt--; 23729 freeb(mp); 23730 break; 23731 } 23732 } 23733 mutex_exit(&ipif->ipif_saved_ire_lock); 23734 } 23735 23736 23737 /* 23738 * IP multirouting broadcast routes handling 23739 * Append CGTP broadcast IREs to regular ones created 23740 * at ifconfig time. 23741 */ 23742 static void 23743 ip_cgtp_bcast_add(ire_t *ire, ire_t *ire_dst, ip_stack_t *ipst) 23744 { 23745 ire_t *ire_prim; 23746 23747 ASSERT(ire != NULL); 23748 ASSERT(ire_dst != NULL); 23749 23750 ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0, 23751 IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 23752 if (ire_prim != NULL) { 23753 /* 23754 * We are in the special case of broadcasts for 23755 * CGTP. We add an IRE_BROADCAST that holds 23756 * the RTF_MULTIRT flag, the destination 23757 * address of ire_dst and the low level 23758 * info of ire_prim. In other words, CGTP 23759 * broadcast is added to the redundant ipif. 23760 */ 23761 ipif_t *ipif_prim; 23762 ire_t *bcast_ire; 23763 23764 ipif_prim = ire_prim->ire_ipif; 23765 23766 ip2dbg(("ip_cgtp_filter_bcast_add: " 23767 "ire_dst %p, ire_prim %p, ipif_prim %p\n", 23768 (void *)ire_dst, (void *)ire_prim, 23769 (void *)ipif_prim)); 23770 23771 bcast_ire = ire_create( 23772 (uchar_t *)&ire->ire_addr, 23773 (uchar_t *)&ip_g_all_ones, 23774 (uchar_t *)&ire_dst->ire_src_addr, 23775 (uchar_t *)&ire->ire_gateway_addr, 23776 NULL, 23777 &ipif_prim->ipif_mtu, 23778 NULL, 23779 ipif_prim->ipif_rq, 23780 ipif_prim->ipif_wq, 23781 IRE_BROADCAST, 23782 ipif_prim->ipif_bcast_mp, 23783 ipif_prim, 23784 NULL, 23785 0, 23786 0, 23787 0, 23788 ire->ire_flags, 23789 &ire_uinfo_null, 23790 NULL, 23791 NULL, 23792 ipst); 23793 23794 if (bcast_ire != NULL) { 23795 23796 if (ire_add(&bcast_ire, NULL, NULL, NULL, 23797 B_FALSE) == 0) { 23798 ip2dbg(("ip_cgtp_filter_bcast_add: " 23799 "added bcast_ire %p\n", 23800 (void *)bcast_ire)); 23801 23802 ipif_save_ire(bcast_ire->ire_ipif, 23803 bcast_ire); 23804 ire_refrele(bcast_ire); 23805 } 23806 } 23807 ire_refrele(ire_prim); 23808 } 23809 } 23810 23811 23812 /* 23813 * IP multirouting broadcast routes handling 23814 * Remove the broadcast ire 23815 */ 23816 static void 23817 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst) 23818 { 23819 ire_t *ire_dst; 23820 23821 ASSERT(ire != NULL); 23822 ire_dst = ire_ctable_lookup(ire->ire_addr, 0, IRE_BROADCAST, 23823 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 23824 if (ire_dst != NULL) { 23825 ire_t *ire_prim; 23826 23827 ire_prim = ire_ctable_lookup(ire->ire_gateway_addr, 0, 23828 IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst); 23829 if (ire_prim != NULL) { 23830 ipif_t *ipif_prim; 23831 ire_t *bcast_ire; 23832 23833 ipif_prim = ire_prim->ire_ipif; 23834 23835 ip2dbg(("ip_cgtp_filter_bcast_delete: " 23836 "ire_dst %p, ire_prim %p, ipif_prim %p\n", 23837 (void *)ire_dst, (void *)ire_prim, 23838 (void *)ipif_prim)); 23839 23840 bcast_ire = ire_ctable_lookup(ire->ire_addr, 23841 ire->ire_gateway_addr, 23842 IRE_BROADCAST, 23843 ipif_prim, ALL_ZONES, 23844 NULL, 23845 MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_IPIF | 23846 MATCH_IRE_MASK, ipst); 23847 23848 if (bcast_ire != NULL) { 23849 ip2dbg(("ip_cgtp_filter_bcast_delete: " 23850 "looked up bcast_ire %p\n", 23851 (void *)bcast_ire)); 23852 ipif_remove_ire(bcast_ire->ire_ipif, 23853 bcast_ire); 23854 ire_delete(bcast_ire); 23855 } 23856 ire_refrele(ire_prim); 23857 } 23858 ire_refrele(ire_dst); 23859 } 23860 } 23861 23862 /* 23863 * IPsec hardware acceleration capabilities related functions. 23864 */ 23865 23866 /* 23867 * Free a per-ill IPsec capabilities structure. 23868 */ 23869 static void 23870 ill_ipsec_capab_free(ill_ipsec_capab_t *capab) 23871 { 23872 if (capab->auth_hw_algs != NULL) 23873 kmem_free(capab->auth_hw_algs, capab->algs_size); 23874 if (capab->encr_hw_algs != NULL) 23875 kmem_free(capab->encr_hw_algs, capab->algs_size); 23876 if (capab->encr_algparm != NULL) 23877 kmem_free(capab->encr_algparm, capab->encr_algparm_size); 23878 kmem_free(capab, sizeof (ill_ipsec_capab_t)); 23879 } 23880 23881 /* 23882 * Allocate a new per-ill IPsec capabilities structure. This structure 23883 * is specific to an IPsec protocol (AH or ESP). It is implemented as 23884 * an array which specifies, for each algorithm, whether this algorithm 23885 * is supported by the ill or not. 23886 */ 23887 static ill_ipsec_capab_t * 23888 ill_ipsec_capab_alloc(void) 23889 { 23890 ill_ipsec_capab_t *capab; 23891 uint_t nelems; 23892 23893 capab = kmem_zalloc(sizeof (ill_ipsec_capab_t), KM_NOSLEEP); 23894 if (capab == NULL) 23895 return (NULL); 23896 23897 /* we need one bit per algorithm */ 23898 nelems = MAX_IPSEC_ALGS / BITS(ipsec_capab_elem_t); 23899 capab->algs_size = nelems * sizeof (ipsec_capab_elem_t); 23900 23901 /* allocate memory to store algorithm flags */ 23902 capab->encr_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP); 23903 if (capab->encr_hw_algs == NULL) 23904 goto nomem; 23905 capab->auth_hw_algs = kmem_zalloc(capab->algs_size, KM_NOSLEEP); 23906 if (capab->auth_hw_algs == NULL) 23907 goto nomem; 23908 /* 23909 * Leave encr_algparm NULL for now since we won't need it half 23910 * the time 23911 */ 23912 return (capab); 23913 23914 nomem: 23915 ill_ipsec_capab_free(capab); 23916 return (NULL); 23917 } 23918 23919 /* 23920 * Resize capability array. Since we're exclusive, this is OK. 23921 */ 23922 static boolean_t 23923 ill_ipsec_capab_resize_algparm(ill_ipsec_capab_t *capab, int algid) 23924 { 23925 ipsec_capab_algparm_t *nalp, *oalp; 23926 uint32_t olen, nlen; 23927 23928 oalp = capab->encr_algparm; 23929 olen = capab->encr_algparm_size; 23930 23931 if (oalp != NULL) { 23932 if (algid < capab->encr_algparm_end) 23933 return (B_TRUE); 23934 } 23935 23936 nlen = (algid + 1) * sizeof (*nalp); 23937 nalp = kmem_zalloc(nlen, KM_NOSLEEP); 23938 if (nalp == NULL) 23939 return (B_FALSE); 23940 23941 if (oalp != NULL) { 23942 bcopy(oalp, nalp, olen); 23943 kmem_free(oalp, olen); 23944 } 23945 capab->encr_algparm = nalp; 23946 capab->encr_algparm_size = nlen; 23947 capab->encr_algparm_end = algid + 1; 23948 23949 return (B_TRUE); 23950 } 23951 23952 /* 23953 * Compare the capabilities of the specified ill with the protocol 23954 * and algorithms specified by the SA passed as argument. 23955 * If they match, returns B_TRUE, B_FALSE if they do not match. 23956 * 23957 * The ill can be passed as a pointer to it, or by specifying its index 23958 * and whether it is an IPv6 ill (ill_index and ill_isv6 arguments). 23959 * 23960 * Called by ipsec_out_is_accelerated() do decide whether an outbound 23961 * packet is eligible for hardware acceleration, and by 23962 * ill_ipsec_capab_send_all() to decide whether a SA must be sent down 23963 * to a particular ill. 23964 */ 23965 boolean_t 23966 ipsec_capab_match(ill_t *ill, uint_t ill_index, boolean_t ill_isv6, 23967 ipsa_t *sa, netstack_t *ns) 23968 { 23969 boolean_t sa_isv6; 23970 uint_t algid; 23971 struct ill_ipsec_capab_s *cpp; 23972 boolean_t need_refrele = B_FALSE; 23973 ip_stack_t *ipst = ns->netstack_ip; 23974 23975 if (ill == NULL) { 23976 ill = ill_lookup_on_ifindex(ill_index, ill_isv6, NULL, 23977 NULL, NULL, NULL, ipst); 23978 if (ill == NULL) { 23979 ip0dbg(("ipsec_capab_match: ill doesn't exist\n")); 23980 return (B_FALSE); 23981 } 23982 need_refrele = B_TRUE; 23983 } 23984 23985 /* 23986 * Use the address length specified by the SA to determine 23987 * if it corresponds to a IPv6 address, and fail the matching 23988 * if the isv6 flag passed as argument does not match. 23989 * Note: this check is used for SADB capability checking before 23990 * sending SA information to an ill. 23991 */ 23992 sa_isv6 = (sa->ipsa_addrfam == AF_INET6); 23993 if (sa_isv6 != ill_isv6) 23994 /* protocol mismatch */ 23995 goto done; 23996 23997 /* 23998 * Check if the ill supports the protocol, algorithm(s) and 23999 * key size(s) specified by the SA, and get the pointers to 24000 * the algorithms supported by the ill. 24001 */ 24002 switch (sa->ipsa_type) { 24003 24004 case SADB_SATYPE_ESP: 24005 if (!(ill->ill_capabilities & ILL_CAPAB_ESP)) 24006 /* ill does not support ESP acceleration */ 24007 goto done; 24008 cpp = ill->ill_ipsec_capab_esp; 24009 algid = sa->ipsa_auth_alg; 24010 if (!IPSEC_ALG_IS_ENABLED(algid, cpp->auth_hw_algs)) 24011 goto done; 24012 algid = sa->ipsa_encr_alg; 24013 if (!IPSEC_ALG_IS_ENABLED(algid, cpp->encr_hw_algs)) 24014 goto done; 24015 if (algid < cpp->encr_algparm_end) { 24016 ipsec_capab_algparm_t *alp = &cpp->encr_algparm[algid]; 24017 if (sa->ipsa_encrkeybits < alp->minkeylen) 24018 goto done; 24019 if (sa->ipsa_encrkeybits > alp->maxkeylen) 24020 goto done; 24021 } 24022 break; 24023 24024 case SADB_SATYPE_AH: 24025 if (!(ill->ill_capabilities & ILL_CAPAB_AH)) 24026 /* ill does not support AH acceleration */ 24027 goto done; 24028 if (!IPSEC_ALG_IS_ENABLED(sa->ipsa_auth_alg, 24029 ill->ill_ipsec_capab_ah->auth_hw_algs)) 24030 goto done; 24031 break; 24032 } 24033 24034 if (need_refrele) 24035 ill_refrele(ill); 24036 return (B_TRUE); 24037 done: 24038 if (need_refrele) 24039 ill_refrele(ill); 24040 return (B_FALSE); 24041 } 24042 24043 24044 /* 24045 * Add a new ill to the list of IPsec capable ills. 24046 * Called from ill_capability_ipsec_ack() when an ACK was received 24047 * indicating that IPsec hardware processing was enabled for an ill. 24048 * 24049 * ill must point to the ill for which acceleration was enabled. 24050 * dl_cap must be set to DL_CAPAB_IPSEC_AH or DL_CAPAB_IPSEC_ESP. 24051 */ 24052 static void 24053 ill_ipsec_capab_add(ill_t *ill, uint_t dl_cap, boolean_t sadb_resync) 24054 { 24055 ipsec_capab_ill_t **ills, *cur_ill, *new_ill; 24056 uint_t sa_type; 24057 uint_t ipproto; 24058 ip_stack_t *ipst = ill->ill_ipst; 24059 24060 ASSERT((dl_cap == DL_CAPAB_IPSEC_AH) || 24061 (dl_cap == DL_CAPAB_IPSEC_ESP)); 24062 24063 switch (dl_cap) { 24064 case DL_CAPAB_IPSEC_AH: 24065 sa_type = SADB_SATYPE_AH; 24066 ills = &ipst->ips_ipsec_capab_ills_ah; 24067 ipproto = IPPROTO_AH; 24068 break; 24069 case DL_CAPAB_IPSEC_ESP: 24070 sa_type = SADB_SATYPE_ESP; 24071 ills = &ipst->ips_ipsec_capab_ills_esp; 24072 ipproto = IPPROTO_ESP; 24073 break; 24074 } 24075 24076 rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER); 24077 24078 /* 24079 * Add ill index to list of hardware accelerators. If 24080 * already in list, do nothing. 24081 */ 24082 for (cur_ill = *ills; cur_ill != NULL && 24083 (cur_ill->ill_index != ill->ill_phyint->phyint_ifindex || 24084 cur_ill->ill_isv6 != ill->ill_isv6); cur_ill = cur_ill->next) 24085 ; 24086 24087 if (cur_ill == NULL) { 24088 /* if this is a new entry for this ill */ 24089 new_ill = kmem_zalloc(sizeof (ipsec_capab_ill_t), KM_NOSLEEP); 24090 if (new_ill == NULL) { 24091 rw_exit(&ipst->ips_ipsec_capab_ills_lock); 24092 return; 24093 } 24094 24095 new_ill->ill_index = ill->ill_phyint->phyint_ifindex; 24096 new_ill->ill_isv6 = ill->ill_isv6; 24097 new_ill->next = *ills; 24098 *ills = new_ill; 24099 } else if (!sadb_resync) { 24100 /* not resync'ing SADB and an entry exists for this ill */ 24101 rw_exit(&ipst->ips_ipsec_capab_ills_lock); 24102 return; 24103 } 24104 24105 rw_exit(&ipst->ips_ipsec_capab_ills_lock); 24106 24107 if (ipst->ips_ipcl_proto_fanout_v6[ipproto].connf_head != NULL) 24108 /* 24109 * IPsec module for protocol loaded, initiate dump 24110 * of the SADB to this ill. 24111 */ 24112 sadb_ill_download(ill, sa_type); 24113 } 24114 24115 /* 24116 * Remove an ill from the list of IPsec capable ills. 24117 */ 24118 static void 24119 ill_ipsec_capab_delete(ill_t *ill, uint_t dl_cap) 24120 { 24121 ipsec_capab_ill_t **ills, *cur_ill, *prev_ill; 24122 ip_stack_t *ipst = ill->ill_ipst; 24123 24124 ASSERT(dl_cap == DL_CAPAB_IPSEC_AH || 24125 dl_cap == DL_CAPAB_IPSEC_ESP); 24126 24127 ills = (dl_cap == DL_CAPAB_IPSEC_AH) ? &ipst->ips_ipsec_capab_ills_ah : 24128 &ipst->ips_ipsec_capab_ills_esp; 24129 24130 rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_WRITER); 24131 24132 prev_ill = NULL; 24133 for (cur_ill = *ills; cur_ill != NULL && (cur_ill->ill_index != 24134 ill->ill_phyint->phyint_ifindex || cur_ill->ill_isv6 != 24135 ill->ill_isv6); prev_ill = cur_ill, cur_ill = cur_ill->next) 24136 ; 24137 if (cur_ill == NULL) { 24138 /* entry not found */ 24139 rw_exit(&ipst->ips_ipsec_capab_ills_lock); 24140 return; 24141 } 24142 if (prev_ill == NULL) { 24143 /* entry at front of list */ 24144 *ills = NULL; 24145 } else { 24146 prev_ill->next = cur_ill->next; 24147 } 24148 kmem_free(cur_ill, sizeof (ipsec_capab_ill_t)); 24149 rw_exit(&ipst->ips_ipsec_capab_ills_lock); 24150 } 24151 24152 /* 24153 * Called by SADB to send a DL_CONTROL_REQ message to every ill 24154 * supporting the specified IPsec protocol acceleration. 24155 * sa_type must be SADB_SATYPE_AH or SADB_SATYPE_ESP. 24156 * We free the mblk and, if sa is non-null, release the held referece. 24157 */ 24158 void 24159 ill_ipsec_capab_send_all(uint_t sa_type, mblk_t *mp, ipsa_t *sa, 24160 netstack_t *ns) 24161 { 24162 ipsec_capab_ill_t *ici, *cur_ici; 24163 ill_t *ill; 24164 mblk_t *nmp, *mp_ship_list = NULL, *next_mp; 24165 ip_stack_t *ipst = ns->netstack_ip; 24166 24167 ici = (sa_type == SADB_SATYPE_AH) ? ipst->ips_ipsec_capab_ills_ah : 24168 ipst->ips_ipsec_capab_ills_esp; 24169 24170 rw_enter(&ipst->ips_ipsec_capab_ills_lock, RW_READER); 24171 24172 for (cur_ici = ici; cur_ici != NULL; cur_ici = cur_ici->next) { 24173 ill = ill_lookup_on_ifindex(cur_ici->ill_index, 24174 cur_ici->ill_isv6, NULL, NULL, NULL, NULL, ipst); 24175 24176 /* 24177 * Handle the case where the ill goes away while the SADB is 24178 * attempting to send messages. If it's going away, it's 24179 * nuking its shadow SADB, so we don't care.. 24180 */ 24181 24182 if (ill == NULL) 24183 continue; 24184 24185 if (sa != NULL) { 24186 /* 24187 * Make sure capabilities match before 24188 * sending SA to ill. 24189 */ 24190 if (!ipsec_capab_match(ill, cur_ici->ill_index, 24191 cur_ici->ill_isv6, sa, ipst->ips_netstack)) { 24192 ill_refrele(ill); 24193 continue; 24194 } 24195 24196 mutex_enter(&sa->ipsa_lock); 24197 sa->ipsa_flags |= IPSA_F_HW; 24198 mutex_exit(&sa->ipsa_lock); 24199 } 24200 24201 /* 24202 * Copy template message, and add it to the front 24203 * of the mblk ship list. We want to avoid holding 24204 * the ipsec_capab_ills_lock while sending the 24205 * message to the ills. 24206 * 24207 * The b_next and b_prev are temporarily used 24208 * to build a list of mblks to be sent down, and to 24209 * save the ill to which they must be sent. 24210 */ 24211 nmp = copymsg(mp); 24212 if (nmp == NULL) { 24213 ill_refrele(ill); 24214 continue; 24215 } 24216 ASSERT(nmp->b_next == NULL && nmp->b_prev == NULL); 24217 nmp->b_next = mp_ship_list; 24218 mp_ship_list = nmp; 24219 nmp->b_prev = (mblk_t *)ill; 24220 } 24221 24222 rw_exit(&ipst->ips_ipsec_capab_ills_lock); 24223 24224 for (nmp = mp_ship_list; nmp != NULL; nmp = next_mp) { 24225 /* restore the mblk to a sane state */ 24226 next_mp = nmp->b_next; 24227 nmp->b_next = NULL; 24228 ill = (ill_t *)nmp->b_prev; 24229 nmp->b_prev = NULL; 24230 24231 ill_dlpi_send(ill, nmp); 24232 ill_refrele(ill); 24233 } 24234 24235 if (sa != NULL) 24236 IPSA_REFRELE(sa); 24237 freemsg(mp); 24238 } 24239 24240 /* 24241 * Derive an interface id from the link layer address. 24242 * Knows about IEEE 802 and IEEE EUI-64 mappings. 24243 */ 24244 static boolean_t 24245 ip_ether_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr) 24246 { 24247 char *addr; 24248 24249 if (phys_length != ETHERADDRL) 24250 return (B_FALSE); 24251 24252 /* Form EUI-64 like address */ 24253 addr = (char *)&v6addr->s6_addr32[2]; 24254 bcopy((char *)phys_addr, addr, 3); 24255 addr[0] ^= 0x2; /* Toggle Universal/Local bit */ 24256 addr[3] = (char)0xff; 24257 addr[4] = (char)0xfe; 24258 bcopy((char *)phys_addr + 3, addr + 5, 3); 24259 return (B_TRUE); 24260 } 24261 24262 /* ARGSUSED */ 24263 static boolean_t 24264 ip_nodef_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr) 24265 { 24266 return (B_FALSE); 24267 } 24268 24269 /* ARGSUSED */ 24270 static boolean_t 24271 ip_ether_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr, 24272 uint32_t *hw_start, in6_addr_t *v6_extract_mask) 24273 { 24274 /* 24275 * Multicast address mappings used over Ethernet/802.X. 24276 * This address is used as a base for mappings. 24277 */ 24278 static uint8_t ipv6_g_phys_multi_addr[] = {0x33, 0x33, 0x00, 24279 0x00, 0x00, 0x00}; 24280 24281 /* 24282 * Extract low order 32 bits from IPv6 multicast address. 24283 * Or that into the link layer address, starting from the 24284 * second byte. 24285 */ 24286 *hw_start = 2; 24287 v6_extract_mask->s6_addr32[0] = 0; 24288 v6_extract_mask->s6_addr32[1] = 0; 24289 v6_extract_mask->s6_addr32[2] = 0; 24290 v6_extract_mask->s6_addr32[3] = 0xffffffffU; 24291 bcopy(ipv6_g_phys_multi_addr, maddr, lla_length); 24292 return (B_TRUE); 24293 } 24294 24295 /* 24296 * Indicate by return value whether multicast is supported. If not, 24297 * this code should not touch/change any parameters. 24298 */ 24299 /* ARGSUSED */ 24300 static boolean_t 24301 ip_ether_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr, 24302 uint32_t *hw_start, ipaddr_t *extract_mask) 24303 { 24304 /* 24305 * Multicast address mappings used over Ethernet/802.X. 24306 * This address is used as a base for mappings. 24307 */ 24308 static uint8_t ip_g_phys_multi_addr[] = { 0x01, 0x00, 0x5e, 24309 0x00, 0x00, 0x00 }; 24310 24311 if (phys_length != ETHERADDRL) 24312 return (B_FALSE); 24313 24314 *extract_mask = htonl(0x007fffff); 24315 *hw_start = 2; 24316 bcopy(ip_g_phys_multi_addr, maddr, ETHERADDRL); 24317 return (B_TRUE); 24318 } 24319 24320 /* 24321 * Derive IPoIB interface id from the link layer address. 24322 */ 24323 static boolean_t 24324 ip_ib_v6intfid(uint_t phys_length, uint8_t *phys_addr, in6_addr_t *v6addr) 24325 { 24326 char *addr; 24327 24328 if (phys_length != 20) 24329 return (B_FALSE); 24330 addr = (char *)&v6addr->s6_addr32[2]; 24331 bcopy(phys_addr + 12, addr, 8); 24332 /* 24333 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit 24334 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE 24335 * rules. In these cases, the IBA considers these GUIDs to be in 24336 * "Modified EUI-64" format, and thus toggling the u/l bit is not 24337 * required; vendors are required not to assign global EUI-64's 24338 * that differ only in u/l bit values, thus guaranteeing uniqueness 24339 * of the interface identifier. Whether the GUID is in modified 24340 * or proper EUI-64 format, the ipv6 identifier must have the u/l 24341 * bit set to 1. 24342 */ 24343 addr[0] |= 2; /* Set Universal/Local bit to 1 */ 24344 return (B_TRUE); 24345 } 24346 24347 /* 24348 * Note on mapping from multicast IP addresses to IPoIB multicast link 24349 * addresses. IPoIB multicast link addresses are based on IBA link addresses. 24350 * The format of an IPoIB multicast address is: 24351 * 24352 * 4 byte QPN Scope Sign. Pkey 24353 * +--------------------------------------------+ 24354 * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID | 24355 * +--------------------------------------------+ 24356 * 24357 * The Scope and Pkey components are properties of the IBA port and 24358 * network interface. They can be ascertained from the broadcast address. 24359 * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6. 24360 */ 24361 24362 static boolean_t 24363 ip_ib_v6mapinfo(uint_t lla_length, uint8_t *bphys_addr, uint8_t *maddr, 24364 uint32_t *hw_start, in6_addr_t *v6_extract_mask) 24365 { 24366 /* 24367 * Base IPoIB IPv6 multicast address used for mappings. 24368 * Does not contain the IBA scope/Pkey values. 24369 */ 24370 static uint8_t ipv6_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff, 24371 0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00, 24372 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 24373 24374 /* 24375 * Extract low order 80 bits from IPv6 multicast address. 24376 * Or that into the link layer address, starting from the 24377 * sixth byte. 24378 */ 24379 *hw_start = 6; 24380 bcopy(ipv6_g_phys_ibmulti_addr, maddr, lla_length); 24381 24382 /* 24383 * Now fill in the IBA scope/Pkey values from the broadcast address. 24384 */ 24385 *(maddr + 5) = *(bphys_addr + 5); 24386 *(maddr + 8) = *(bphys_addr + 8); 24387 *(maddr + 9) = *(bphys_addr + 9); 24388 24389 v6_extract_mask->s6_addr32[0] = 0; 24390 v6_extract_mask->s6_addr32[1] = htonl(0x0000ffff); 24391 v6_extract_mask->s6_addr32[2] = 0xffffffffU; 24392 v6_extract_mask->s6_addr32[3] = 0xffffffffU; 24393 return (B_TRUE); 24394 } 24395 24396 static boolean_t 24397 ip_ib_v4mapinfo(uint_t phys_length, uint8_t *bphys_addr, uint8_t *maddr, 24398 uint32_t *hw_start, ipaddr_t *extract_mask) 24399 { 24400 /* 24401 * Base IPoIB IPv4 multicast address used for mappings. 24402 * Does not contain the IBA scope/Pkey values. 24403 */ 24404 static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff, 24405 0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, 24406 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 24407 24408 if (phys_length != sizeof (ipv4_g_phys_ibmulti_addr)) 24409 return (B_FALSE); 24410 24411 /* 24412 * Extract low order 28 bits from IPv4 multicast address. 24413 * Or that into the link layer address, starting from the 24414 * sixteenth byte. 24415 */ 24416 *extract_mask = htonl(0x0fffffff); 24417 *hw_start = 16; 24418 bcopy(ipv4_g_phys_ibmulti_addr, maddr, phys_length); 24419 24420 /* 24421 * Now fill in the IBA scope/Pkey values from the broadcast address. 24422 */ 24423 *(maddr + 5) = *(bphys_addr + 5); 24424 *(maddr + 8) = *(bphys_addr + 8); 24425 *(maddr + 9) = *(bphys_addr + 9); 24426 return (B_TRUE); 24427 } 24428 24429 /* 24430 * Returns B_TRUE if an ipif is present in the given zone, matching some flags 24431 * (typically IPIF_UP). If ipifp is non-null, the held ipif is returned there. 24432 * This works for both IPv4 and IPv6; if the passed-in ill is v6, the ipif with 24433 * the link-local address is preferred. 24434 */ 24435 boolean_t 24436 ipif_lookup_zoneid(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp) 24437 { 24438 ipif_t *ipif; 24439 ipif_t *maybe_ipif = NULL; 24440 24441 mutex_enter(&ill->ill_lock); 24442 if (ill->ill_state_flags & ILL_CONDEMNED) { 24443 mutex_exit(&ill->ill_lock); 24444 if (ipifp != NULL) 24445 *ipifp = NULL; 24446 return (B_FALSE); 24447 } 24448 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 24449 if (!IPIF_CAN_LOOKUP(ipif)) 24450 continue; 24451 if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid && 24452 ipif->ipif_zoneid != ALL_ZONES) 24453 continue; 24454 if ((ipif->ipif_flags & flags) != flags) 24455 continue; 24456 24457 if (ipifp == NULL) { 24458 mutex_exit(&ill->ill_lock); 24459 ASSERT(maybe_ipif == NULL); 24460 return (B_TRUE); 24461 } 24462 if (!ill->ill_isv6 || 24463 IN6_IS_ADDR_LINKLOCAL(&ipif->ipif_v6src_addr)) { 24464 ipif_refhold_locked(ipif); 24465 mutex_exit(&ill->ill_lock); 24466 *ipifp = ipif; 24467 return (B_TRUE); 24468 } 24469 if (maybe_ipif == NULL) 24470 maybe_ipif = ipif; 24471 } 24472 if (ipifp != NULL) { 24473 if (maybe_ipif != NULL) 24474 ipif_refhold_locked(maybe_ipif); 24475 *ipifp = maybe_ipif; 24476 } 24477 mutex_exit(&ill->ill_lock); 24478 return (maybe_ipif != NULL); 24479 } 24480 24481 /* 24482 * Same as ipif_lookup_zoneid() but looks at all the ills in the same group. 24483 */ 24484 boolean_t 24485 ipif_lookup_zoneid_group(ill_t *ill, zoneid_t zoneid, int flags, ipif_t **ipifp) 24486 { 24487 ill_t *illg; 24488 ip_stack_t *ipst = ill->ill_ipst; 24489 24490 /* 24491 * We look at the passed-in ill first without grabbing ill_g_lock. 24492 */ 24493 if (ipif_lookup_zoneid(ill, zoneid, flags, ipifp)) { 24494 return (B_TRUE); 24495 } 24496 rw_enter(&ipst->ips_ill_g_lock, RW_READER); 24497 if (ill->ill_group == NULL) { 24498 /* ill not in a group */ 24499 rw_exit(&ipst->ips_ill_g_lock); 24500 return (B_FALSE); 24501 } 24502 24503 /* 24504 * There's no ipif in the zone on ill, however ill is part of an IPMP 24505 * group. We need to look for an ipif in the zone on all the ills in the 24506 * group. 24507 */ 24508 illg = ill->ill_group->illgrp_ill; 24509 do { 24510 /* 24511 * We don't call ipif_lookup_zoneid() on ill as we already know 24512 * that it's not there. 24513 */ 24514 if (illg != ill && 24515 ipif_lookup_zoneid(illg, zoneid, flags, ipifp)) { 24516 break; 24517 } 24518 } while ((illg = illg->ill_group_next) != NULL); 24519 rw_exit(&ipst->ips_ill_g_lock); 24520 return (illg != NULL); 24521 } 24522 24523 /* 24524 * Check if this ill is only being used to send ICMP probes for IPMP 24525 */ 24526 boolean_t 24527 ill_is_probeonly(ill_t *ill) 24528 { 24529 /* 24530 * Check if the interface is FAILED, or INACTIVE 24531 */ 24532 if (ill->ill_phyint->phyint_flags & (PHYI_FAILED|PHYI_INACTIVE)) 24533 return (B_TRUE); 24534 24535 return (B_FALSE); 24536 } 24537 24538 /* 24539 * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id) 24540 * If a pointer to an ipif_t is returned then the caller will need to do 24541 * an ill_refrele(). 24542 * 24543 * If there is no real interface which matches the ifindex, then it looks 24544 * for a group that has a matching index. In the case of a group match the 24545 * lifidx must be zero. We don't need emulate the logical interfaces 24546 * since IP Filter's use of netinfo doesn't use that. 24547 */ 24548 ipif_t * 24549 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6, 24550 ip_stack_t *ipst) 24551 { 24552 ipif_t *ipif; 24553 ill_t *ill; 24554 24555 ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL, 24556 ipst); 24557 24558 if (ill == NULL) { 24559 /* Fallback to group names only if hook_emulation set */ 24560 if (!ipst->ips_ipmp_hook_emulation) 24561 return (NULL); 24562 24563 if (lifidx != 0) 24564 return (NULL); 24565 ill = ill_group_lookup_on_ifindex(ifindex, isv6, ipst); 24566 if (ill == NULL) 24567 return (NULL); 24568 } 24569 24570 mutex_enter(&ill->ill_lock); 24571 if (ill->ill_state_flags & ILL_CONDEMNED) { 24572 mutex_exit(&ill->ill_lock); 24573 ill_refrele(ill); 24574 return (NULL); 24575 } 24576 24577 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) { 24578 if (!IPIF_CAN_LOOKUP(ipif)) 24579 continue; 24580 if (lifidx == ipif->ipif_id) { 24581 ipif_refhold_locked(ipif); 24582 break; 24583 } 24584 } 24585 24586 mutex_exit(&ill->ill_lock); 24587 ill_refrele(ill); 24588 return (ipif); 24589 } 24590 24591 /* 24592 * Flush the fastpath by deleting any nce's that are waiting for the fastpath, 24593 * There is one exceptions IRE_BROADCAST are difficult to recreate, 24594 * so instead we just nuke their nce_fp_mp's; see ndp_fastpath_flush() 24595 * for details. 24596 */ 24597 void 24598 ill_fastpath_flush(ill_t *ill) 24599 { 24600 ip_stack_t *ipst = ill->ill_ipst; 24601 24602 nce_fastpath_list_dispatch(ill, NULL, NULL); 24603 ndp_walk_common((ill->ill_isv6 ? ipst->ips_ndp6 : ipst->ips_ndp4), 24604 ill, (pfi_t)ndp_fastpath_flush, NULL, B_TRUE); 24605 } 24606 24607 /* 24608 * Set the physical address information for `ill' to the contents of the 24609 * dl_notify_ind_t pointed to by `mp'. Must be called as writer, and will be 24610 * asynchronous if `ill' cannot immediately be quiesced -- in which case 24611 * EINPROGRESS will be returned. 24612 */ 24613 int 24614 ill_set_phys_addr(ill_t *ill, mblk_t *mp) 24615 { 24616 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq; 24617 dl_notify_ind_t *dlindp = (dl_notify_ind_t *)mp->b_rptr; 24618 24619 ASSERT(IAM_WRITER_IPSQ(ipsq)); 24620 24621 if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR && 24622 dlindp->dl_data != DL_CURR_PHYS_ADDR) { 24623 /* Changing DL_IPV6_TOKEN is not yet supported */ 24624 return (0); 24625 } 24626 24627 /* 24628 * We need to store up to two copies of `mp' in `ill'. Due to the 24629 * design of ipsq_pending_mp_add(), we can't pass them as separate 24630 * arguments to ill_set_phys_addr_tail(). Instead, chain them 24631 * together here, then pull 'em apart in ill_set_phys_addr_tail(). 24632 */ 24633 if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) { 24634 freemsg(mp); 24635 return (ENOMEM); 24636 } 24637 24638 ipsq_current_start(ipsq, ill->ill_ipif, 0); 24639 24640 /* 24641 * If we can quiesce the ill, then set the address. If not, then 24642 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail(). 24643 */ 24644 ill_down_ipifs(ill, NULL, 0, B_FALSE); 24645 mutex_enter(&ill->ill_lock); 24646 if (!ill_is_quiescent(ill)) { 24647 /* call cannot fail since `conn_t *' argument is NULL */ 24648 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq, 24649 mp, ILL_DOWN); 24650 mutex_exit(&ill->ill_lock); 24651 return (EINPROGRESS); 24652 } 24653 mutex_exit(&ill->ill_lock); 24654 24655 ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL); 24656 return (0); 24657 } 24658 24659 /* 24660 * Once the ill associated with `q' has quiesced, set its physical address 24661 * information to the values in `addrmp'. Note that two copies of `addrmp' 24662 * are passed (linked by b_cont), since we sometimes need to save two distinct 24663 * copies in the ill_t, and our context doesn't permit sleeping or allocation 24664 * failure (we'll free the other copy if it's not needed). Since the ill_t 24665 * is quiesced, we know any stale IREs with the old address information have 24666 * already been removed, so we don't need to call ill_fastpath_flush(). 24667 */ 24668 /* ARGSUSED */ 24669 static void 24670 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy) 24671 { 24672 ill_t *ill = q->q_ptr; 24673 mblk_t *addrmp2 = unlinkb(addrmp); 24674 dl_notify_ind_t *dlindp = (dl_notify_ind_t *)addrmp->b_rptr; 24675 uint_t addrlen, addroff; 24676 24677 ASSERT(IAM_WRITER_IPSQ(ipsq)); 24678 24679 addroff = dlindp->dl_addr_offset; 24680 addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length); 24681 24682 switch (dlindp->dl_data) { 24683 case DL_IPV6_LINK_LAYER_ADDR: 24684 ill_set_ndmp(ill, addrmp, addroff, addrlen); 24685 freemsg(addrmp2); 24686 break; 24687 24688 case DL_CURR_PHYS_ADDR: 24689 freemsg(ill->ill_phys_addr_mp); 24690 ill->ill_phys_addr = addrmp->b_rptr + addroff; 24691 ill->ill_phys_addr_mp = addrmp; 24692 ill->ill_phys_addr_length = addrlen; 24693 24694 if (ill->ill_isv6 && !(ill->ill_flags & ILLF_XRESOLV)) 24695 ill_set_ndmp(ill, addrmp2, addroff, addrlen); 24696 else 24697 freemsg(addrmp2); 24698 break; 24699 default: 24700 ASSERT(0); 24701 } 24702 24703 /* 24704 * If there are ipifs to bring up, ill_up_ipifs() will return 24705 * EINPROGRESS, and ipsq_current_finish() will be called by 24706 * ip_rput_dlpi_writer() or ip_arp_done() when the last ipif is 24707 * brought up. 24708 */ 24709 if (ill_up_ipifs(ill, q, addrmp) != EINPROGRESS) 24710 ipsq_current_finish(ipsq); 24711 } 24712 24713 /* 24714 * Helper routine for setting the ill_nd_lla fields. 24715 */ 24716 void 24717 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen) 24718 { 24719 freemsg(ill->ill_nd_lla_mp); 24720 ill->ill_nd_lla = ndmp->b_rptr + addroff; 24721 ill->ill_nd_lla_mp = ndmp; 24722 ill->ill_nd_lla_len = addrlen; 24723 } 24724 24725 major_t IP_MAJ; 24726 #define IP "ip" 24727 24728 #define UDP6DEV "/devices/pseudo/udp6@0:udp6" 24729 #define UDPDEV "/devices/pseudo/udp@0:udp" 24730 24731 /* 24732 * Issue REMOVEIF ioctls to have the loopback interfaces 24733 * go away. Other interfaces are either I_LINKed or I_PLINKed; 24734 * the former going away when the user-level processes in the zone 24735 * are killed * and the latter are cleaned up by the stream head 24736 * str_stack_shutdown callback that undoes all I_PLINKs. 24737 */ 24738 void 24739 ip_loopback_cleanup(ip_stack_t *ipst) 24740 { 24741 int error; 24742 ldi_handle_t lh = NULL; 24743 ldi_ident_t li = NULL; 24744 int rval; 24745 cred_t *cr; 24746 struct strioctl iocb; 24747 struct lifreq lifreq; 24748 24749 IP_MAJ = ddi_name_to_major(IP); 24750 24751 #ifdef NS_DEBUG 24752 (void) printf("ip_loopback_cleanup() stackid %d\n", 24753 ipst->ips_netstack->netstack_stackid); 24754 #endif 24755 24756 bzero(&lifreq, sizeof (lifreq)); 24757 (void) strcpy(lifreq.lifr_name, ipif_loopback_name); 24758 24759 error = ldi_ident_from_major(IP_MAJ, &li); 24760 if (error) { 24761 #ifdef DEBUG 24762 printf("ip_loopback_cleanup: lyr ident get failed error %d\n", 24763 error); 24764 #endif 24765 return; 24766 } 24767 24768 cr = zone_get_kcred(netstackid_to_zoneid( 24769 ipst->ips_netstack->netstack_stackid)); 24770 ASSERT(cr != NULL); 24771 error = ldi_open_by_name(UDP6DEV, FREAD|FWRITE, cr, &lh, li); 24772 if (error) { 24773 #ifdef DEBUG 24774 printf("ip_loopback_cleanup: open of UDP6DEV failed error %d\n", 24775 error); 24776 #endif 24777 goto out; 24778 } 24779 iocb.ic_cmd = SIOCLIFREMOVEIF; 24780 iocb.ic_timout = 15; 24781 iocb.ic_len = sizeof (lifreq); 24782 iocb.ic_dp = (char *)&lifreq; 24783 24784 error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval); 24785 /* LINTED - statement has no consequent */ 24786 if (error) { 24787 #ifdef NS_DEBUG 24788 printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on " 24789 "UDP6 error %d\n", error); 24790 #endif 24791 } 24792 (void) ldi_close(lh, FREAD|FWRITE, cr); 24793 lh = NULL; 24794 24795 error = ldi_open_by_name(UDPDEV, FREAD|FWRITE, cr, &lh, li); 24796 if (error) { 24797 #ifdef NS_DEBUG 24798 printf("ip_loopback_cleanup: open of UDPDEV failed error %d\n", 24799 error); 24800 #endif 24801 goto out; 24802 } 24803 24804 iocb.ic_cmd = SIOCLIFREMOVEIF; 24805 iocb.ic_timout = 15; 24806 iocb.ic_len = sizeof (lifreq); 24807 iocb.ic_dp = (char *)&lifreq; 24808 24809 error = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval); 24810 /* LINTED - statement has no consequent */ 24811 if (error) { 24812 #ifdef NS_DEBUG 24813 printf("ip_loopback_cleanup: ioctl SIOCLIFREMOVEIF failed on " 24814 "UDP error %d\n", error); 24815 #endif 24816 } 24817 (void) ldi_close(lh, FREAD|FWRITE, cr); 24818 lh = NULL; 24819 24820 out: 24821 /* Close layered handles */ 24822 if (lh) 24823 (void) ldi_close(lh, FREAD|FWRITE, cr); 24824 if (li) 24825 ldi_ident_release(li); 24826 24827 crfree(cr); 24828 } 24829