Print this page
5045 use atomic_{inc,dec}_* instead of atomic_add_*
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/ip/ip_if.c
+++ new/usr/src/uts/common/inet/ip/ip_if.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 1990 Mentat Inc.
24 24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 25 * Copyright (c) 2014, OmniTI Computer Consulting, Inc. All rights reserved.
26 26 */
27 27
28 28 /*
29 29 * This file contains the interface control functions for IP.
30 30 */
31 31
32 32 #include <sys/types.h>
33 33 #include <sys/stream.h>
34 34 #include <sys/dlpi.h>
35 35 #include <sys/stropts.h>
36 36 #include <sys/strsun.h>
37 37 #include <sys/sysmacros.h>
38 38 #include <sys/strsubr.h>
39 39 #include <sys/strlog.h>
40 40 #include <sys/ddi.h>
41 41 #include <sys/sunddi.h>
42 42 #include <sys/cmn_err.h>
43 43 #include <sys/kstat.h>
44 44 #include <sys/debug.h>
45 45 #include <sys/zone.h>
46 46 #include <sys/sunldi.h>
47 47 #include <sys/file.h>
48 48 #include <sys/bitmap.h>
49 49 #include <sys/cpuvar.h>
50 50 #include <sys/time.h>
51 51 #include <sys/ctype.h>
52 52 #include <sys/kmem.h>
53 53 #include <sys/systm.h>
54 54 #include <sys/param.h>
55 55 #include <sys/socket.h>
56 56 #include <sys/isa_defs.h>
57 57 #include <net/if.h>
58 58 #include <net/if_arp.h>
59 59 #include <net/if_types.h>
60 60 #include <net/if_dl.h>
61 61 #include <net/route.h>
62 62 #include <sys/sockio.h>
63 63 #include <netinet/in.h>
64 64 #include <netinet/ip6.h>
65 65 #include <netinet/icmp6.h>
66 66 #include <netinet/igmp_var.h>
67 67 #include <sys/policy.h>
68 68 #include <sys/ethernet.h>
69 69 #include <sys/callb.h>
70 70 #include <sys/md5.h>
71 71
72 72 #include <inet/common.h> /* for various inet/mi.h and inet/nd.h needs */
73 73 #include <inet/mi.h>
74 74 #include <inet/nd.h>
75 75 #include <inet/tunables.h>
76 76 #include <inet/arp.h>
77 77 #include <inet/ip_arp.h>
78 78 #include <inet/mib2.h>
79 79 #include <inet/ip.h>
80 80 #include <inet/ip6.h>
81 81 #include <inet/ip6_asp.h>
82 82 #include <inet/tcp.h>
83 83 #include <inet/ip_multi.h>
84 84 #include <inet/ip_ire.h>
85 85 #include <inet/ip_ftable.h>
86 86 #include <inet/ip_rts.h>
87 87 #include <inet/ip_ndp.h>
88 88 #include <inet/ip_if.h>
89 89 #include <inet/ip_impl.h>
90 90 #include <inet/sctp_ip.h>
91 91 #include <inet/ip_netinfo.h>
92 92 #include <inet/ilb_ip.h>
93 93
94 94 #include <netinet/igmp.h>
95 95 #include <inet/ip_listutils.h>
96 96 #include <inet/ipclassifier.h>
97 97 #include <sys/mac_client.h>
98 98 #include <sys/dld.h>
99 99 #include <sys/mac_flow.h>
100 100
101 101 #include <sys/systeminfo.h>
102 102 #include <sys/bootconf.h>
103 103
104 104 #include <sys/tsol/tndb.h>
105 105 #include <sys/tsol/tnet.h>
106 106
107 107 #include <inet/rawip_impl.h> /* needed for icmp_stack_t */
108 108 #include <inet/udp_impl.h> /* needed for udp_stack_t */
109 109
110 110 /* The character which tells where the ill_name ends */
111 111 #define IPIF_SEPARATOR_CHAR ':'
112 112
113 113 /* IP ioctl function table entry */
114 114 typedef struct ipft_s {
115 115 int ipft_cmd;
116 116 pfi_t ipft_pfi;
117 117 int ipft_min_size;
118 118 int ipft_flags;
119 119 } ipft_t;
120 120 #define IPFT_F_NO_REPLY 0x1 /* IP ioctl does not expect any reply */
121 121 #define IPFT_F_SELF_REPLY 0x2 /* ioctl callee does the ioctl reply */
122 122
123 123 static int nd_ill_forward_get(queue_t *, mblk_t *, caddr_t, cred_t *);
124 124 static int nd_ill_forward_set(queue_t *q, mblk_t *mp,
125 125 char *value, caddr_t cp, cred_t *ioc_cr);
126 126
127 127 static boolean_t ill_is_quiescent(ill_t *);
128 128 static boolean_t ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask);
129 129 static ip_m_t *ip_m_lookup(t_uscalar_t mac_type);
130 130 static int ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
131 131 mblk_t *mp, boolean_t need_up);
132 132 static int ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
133 133 mblk_t *mp, boolean_t need_up);
134 134 static int ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
135 135 queue_t *q, mblk_t *mp, boolean_t need_up);
136 136 static int ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q,
137 137 mblk_t *mp);
138 138 static int ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q,
139 139 mblk_t *mp);
140 140 static int ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t, in6_addr_t,
141 141 queue_t *q, mblk_t *mp, boolean_t need_up);
142 142 static int ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp,
143 143 int ioccmd, struct linkblk *li);
144 144 static ipaddr_t ip_subnet_mask(ipaddr_t addr, ipif_t **, ip_stack_t *);
145 145 static void ip_wput_ioctl(queue_t *q, mblk_t *mp);
146 146 static void ipsq_flush(ill_t *ill);
147 147
148 148 static int ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen,
149 149 queue_t *q, mblk_t *mp, boolean_t need_up);
150 150 static void ipsq_delete(ipsq_t *);
151 151
152 152 static ipif_t *ipif_allocate(ill_t *ill, int id, uint_t ire_type,
153 153 boolean_t initialize, boolean_t insert, int *errorp);
154 154 static ire_t **ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep);
155 155 static void ipif_delete_bcast_ires(ipif_t *ipif);
156 156 static int ipif_add_ires_v4(ipif_t *, boolean_t);
157 157 static boolean_t ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif,
158 158 boolean_t isv6);
159 159 static int ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp);
160 160 static void ipif_free(ipif_t *ipif);
161 161 static void ipif_free_tail(ipif_t *ipif);
162 162 static void ipif_set_default(ipif_t *ipif);
163 163 static int ipif_set_values(queue_t *q, mblk_t *mp,
164 164 char *interf_name, uint_t *ppa);
165 165 static int ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp,
166 166 queue_t *q);
167 167 static ipif_t *ipif_lookup_on_name(char *name, size_t namelen,
168 168 boolean_t do_alloc, boolean_t *exists, boolean_t isv6, zoneid_t zoneid,
169 169 ip_stack_t *);
170 170 static ipif_t *ipif_lookup_on_name_async(char *name, size_t namelen,
171 171 boolean_t isv6, zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func,
172 172 int *error, ip_stack_t *);
173 173
174 174 static int ill_alloc_ppa(ill_if_t *, ill_t *);
175 175 static void ill_delete_interface_type(ill_if_t *);
176 176 static int ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q);
177 177 static void ill_dl_down(ill_t *ill);
178 178 static void ill_down(ill_t *ill);
179 179 static void ill_down_ipifs(ill_t *, boolean_t);
180 180 static void ill_free_mib(ill_t *ill);
181 181 static void ill_glist_delete(ill_t *);
182 182 static void ill_phyint_reinit(ill_t *ill);
183 183 static void ill_set_nce_router_flags(ill_t *, boolean_t);
184 184 static void ill_set_phys_addr_tail(ipsq_t *, queue_t *, mblk_t *, void *);
185 185 static void ill_replumb_tail(ipsq_t *, queue_t *, mblk_t *, void *);
186 186
187 187 static ip_v6intfid_func_t ip_ether_v6intfid, ip_ib_v6intfid;
188 188 static ip_v6intfid_func_t ip_ipv4_v6intfid, ip_ipv6_v6intfid;
189 189 static ip_v6intfid_func_t ip_ipmp_v6intfid, ip_nodef_v6intfid;
190 190 static ip_v6intfid_func_t ip_ipv4_v6destintfid, ip_ipv6_v6destintfid;
191 191 static ip_v4mapinfo_func_t ip_ether_v4_mapping;
192 192 static ip_v6mapinfo_func_t ip_ether_v6_mapping;
193 193 static ip_v4mapinfo_func_t ip_ib_v4_mapping;
194 194 static ip_v6mapinfo_func_t ip_ib_v6_mapping;
195 195 static ip_v4mapinfo_func_t ip_mbcast_mapping;
196 196 static void ip_cgtp_bcast_add(ire_t *, ip_stack_t *);
197 197 static void ip_cgtp_bcast_delete(ire_t *, ip_stack_t *);
198 198 static void phyint_free(phyint_t *);
199 199
200 200 static void ill_capability_dispatch(ill_t *, mblk_t *, dl_capability_sub_t *);
201 201 static void ill_capability_id_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
202 202 static void ill_capability_vrrp_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
203 203 static void ill_capability_hcksum_ack(ill_t *, mblk_t *, dl_capability_sub_t *);
204 204 static void ill_capability_hcksum_reset_fill(ill_t *, mblk_t *);
205 205 static void ill_capability_zerocopy_ack(ill_t *, mblk_t *,
206 206 dl_capability_sub_t *);
207 207 static void ill_capability_zerocopy_reset_fill(ill_t *, mblk_t *);
208 208 static void ill_capability_dld_reset_fill(ill_t *, mblk_t *);
209 209 static void ill_capability_dld_ack(ill_t *, mblk_t *,
210 210 dl_capability_sub_t *);
211 211 static void ill_capability_dld_enable(ill_t *);
212 212 static void ill_capability_ack_thr(void *);
213 213 static void ill_capability_lso_enable(ill_t *);
214 214
215 215 static ill_t *ill_prev_usesrc(ill_t *);
216 216 static int ill_relink_usesrc_ills(ill_t *, ill_t *, uint_t);
217 217 static void ill_disband_usesrc_group(ill_t *);
218 218 static void ip_sioctl_garp_reply(mblk_t *, ill_t *, void *, int);
219 219
220 220 #ifdef DEBUG
221 221 static void ill_trace_cleanup(const ill_t *);
222 222 static void ipif_trace_cleanup(const ipif_t *);
223 223 #endif
224 224
225 225 static void ill_dlpi_clear_deferred(ill_t *ill);
226 226
227 227 /*
228 228 * if we go over the memory footprint limit more than once in this msec
229 229 * interval, we'll start pruning aggressively.
230 230 */
231 231 int ip_min_frag_prune_time = 0;
232 232
233 233 static ipft_t ip_ioctl_ftbl[] = {
234 234 { IP_IOC_IRE_DELETE, ip_ire_delete, sizeof (ipid_t), 0 },
235 235 { IP_IOC_IRE_DELETE_NO_REPLY, ip_ire_delete, sizeof (ipid_t),
236 236 IPFT_F_NO_REPLY },
237 237 { IP_IOC_RTS_REQUEST, ip_rts_request, 0, IPFT_F_SELF_REPLY },
238 238 { 0 }
239 239 };
240 240
241 241 /* Simple ICMP IP Header Template */
242 242 static ipha_t icmp_ipha = {
243 243 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
244 244 };
245 245
246 246 static uchar_t ip_six_byte_all_ones[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
247 247
248 248 static ip_m_t ip_m_tbl[] = {
249 249 { DL_ETHER, IFT_ETHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
250 250 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
251 251 ip_nodef_v6intfid },
252 252 { DL_CSMACD, IFT_ISO88023, ETHERTYPE_IP, ETHERTYPE_IPV6,
253 253 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
254 254 ip_nodef_v6intfid },
255 255 { DL_TPB, IFT_ISO88024, ETHERTYPE_IP, ETHERTYPE_IPV6,
256 256 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
257 257 ip_nodef_v6intfid },
258 258 { DL_TPR, IFT_ISO88025, ETHERTYPE_IP, ETHERTYPE_IPV6,
259 259 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
260 260 ip_nodef_v6intfid },
261 261 { DL_FDDI, IFT_FDDI, ETHERTYPE_IP, ETHERTYPE_IPV6,
262 262 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_ether_v6intfid,
263 263 ip_nodef_v6intfid },
264 264 { DL_IB, IFT_IB, ETHERTYPE_IP, ETHERTYPE_IPV6,
265 265 ip_ib_v4_mapping, ip_ib_v6_mapping, ip_ib_v6intfid,
266 266 ip_nodef_v6intfid },
267 267 { DL_IPV4, IFT_IPV4, IPPROTO_ENCAP, IPPROTO_IPV6,
268 268 ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
269 269 ip_ipv4_v6destintfid },
270 270 { DL_IPV6, IFT_IPV6, IPPROTO_ENCAP, IPPROTO_IPV6,
271 271 ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv6_v6intfid,
272 272 ip_ipv6_v6destintfid },
273 273 { DL_6TO4, IFT_6TO4, IPPROTO_ENCAP, IPPROTO_IPV6,
274 274 ip_mbcast_mapping, ip_mbcast_mapping, ip_ipv4_v6intfid,
275 275 ip_nodef_v6intfid },
276 276 { SUNW_DL_VNI, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
277 277 NULL, NULL, ip_nodef_v6intfid, ip_nodef_v6intfid },
278 278 { SUNW_DL_IPMP, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
279 279 NULL, NULL, ip_ipmp_v6intfid, ip_nodef_v6intfid },
280 280 { DL_OTHER, IFT_OTHER, ETHERTYPE_IP, ETHERTYPE_IPV6,
281 281 ip_ether_v4_mapping, ip_ether_v6_mapping, ip_nodef_v6intfid,
282 282 ip_nodef_v6intfid }
283 283 };
284 284
285 285 static ill_t ill_null; /* Empty ILL for init. */
286 286 char ipif_loopback_name[] = "lo0";
287 287
288 288 /* These are used by all IP network modules. */
289 289 sin6_t sin6_null; /* Zero address for quick clears */
290 290 sin_t sin_null; /* Zero address for quick clears */
291 291
292 292 /* When set search for unused ipif_seqid */
293 293 static ipif_t ipif_zero;
294 294
295 295 /*
296 296 * ppa arena is created after these many
297 297 * interfaces have been plumbed.
298 298 */
299 299 uint_t ill_no_arena = 12; /* Setable in /etc/system */
300 300
301 301 /*
302 302 * Allocate per-interface mibs.
303 303 * Returns true if ok. False otherwise.
304 304 * ipsq may not yet be allocated (loopback case ).
305 305 */
306 306 static boolean_t
307 307 ill_allocate_mibs(ill_t *ill)
308 308 {
309 309 /* Already allocated? */
310 310 if (ill->ill_ip_mib != NULL) {
311 311 if (ill->ill_isv6)
312 312 ASSERT(ill->ill_icmp6_mib != NULL);
313 313 return (B_TRUE);
314 314 }
315 315
316 316 ill->ill_ip_mib = kmem_zalloc(sizeof (*ill->ill_ip_mib),
317 317 KM_NOSLEEP);
318 318 if (ill->ill_ip_mib == NULL) {
319 319 return (B_FALSE);
320 320 }
321 321
322 322 /* Setup static information */
323 323 SET_MIB(ill->ill_ip_mib->ipIfStatsEntrySize,
324 324 sizeof (mib2_ipIfStatsEntry_t));
325 325 if (ill->ill_isv6) {
326 326 ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
327 327 SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
328 328 sizeof (mib2_ipv6AddrEntry_t));
329 329 SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
330 330 sizeof (mib2_ipv6RouteEntry_t));
331 331 SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
332 332 sizeof (mib2_ipv6NetToMediaEntry_t));
333 333 SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
334 334 sizeof (ipv6_member_t));
335 335 SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
336 336 sizeof (ipv6_grpsrc_t));
337 337 } else {
338 338 ill->ill_ip_mib->ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
339 339 SET_MIB(ill->ill_ip_mib->ipIfStatsAddrEntrySize,
340 340 sizeof (mib2_ipAddrEntry_t));
341 341 SET_MIB(ill->ill_ip_mib->ipIfStatsRouteEntrySize,
342 342 sizeof (mib2_ipRouteEntry_t));
343 343 SET_MIB(ill->ill_ip_mib->ipIfStatsNetToMediaEntrySize,
344 344 sizeof (mib2_ipNetToMediaEntry_t));
345 345 SET_MIB(ill->ill_ip_mib->ipIfStatsMemberEntrySize,
346 346 sizeof (ip_member_t));
347 347 SET_MIB(ill->ill_ip_mib->ipIfStatsGroupSourceEntrySize,
348 348 sizeof (ip_grpsrc_t));
349 349
350 350 /*
351 351 * For a v4 ill, we are done at this point, because per ill
352 352 * icmp mibs are only used for v6.
353 353 */
354 354 return (B_TRUE);
355 355 }
356 356
357 357 ill->ill_icmp6_mib = kmem_zalloc(sizeof (*ill->ill_icmp6_mib),
358 358 KM_NOSLEEP);
359 359 if (ill->ill_icmp6_mib == NULL) {
360 360 kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
361 361 ill->ill_ip_mib = NULL;
362 362 return (B_FALSE);
363 363 }
364 364 /* static icmp info */
365 365 ill->ill_icmp6_mib->ipv6IfIcmpEntrySize =
366 366 sizeof (mib2_ipv6IfIcmpEntry_t);
367 367 /*
368 368 * The ipIfStatsIfindex and ipv6IfIcmpIndex will be assigned later
369 369 * after the phyint merge occurs in ipif_set_values -> ill_glist_insert
370 370 * -> ill_phyint_reinit
371 371 */
372 372 return (B_TRUE);
373 373 }
374 374
375 375 /*
376 376 * Completely vaporize a lower level tap and all associated interfaces.
377 377 * ill_delete is called only out of ip_close when the device control
378 378 * stream is being closed.
379 379 */
380 380 void
381 381 ill_delete(ill_t *ill)
382 382 {
383 383 ipif_t *ipif;
384 384 ill_t *prev_ill;
385 385 ip_stack_t *ipst = ill->ill_ipst;
386 386
387 387 /*
388 388 * ill_delete may be forcibly entering the ipsq. The previous
389 389 * ioctl may not have completed and may need to be aborted.
390 390 * ipsq_flush takes care of it. If we don't need to enter the
391 391 * the ipsq forcibly, the 2nd invocation of ipsq_flush in
392 392 * ill_delete_tail is sufficient.
393 393 */
394 394 ipsq_flush(ill);
395 395
396 396 /*
397 397 * Nuke all interfaces. ipif_free will take down the interface,
398 398 * remove it from the list, and free the data structure.
399 399 * Walk down the ipif list and remove the logical interfaces
400 400 * first before removing the main ipif. We can't unplumb
401 401 * zeroth interface first in the case of IPv6 as update_conn_ill
402 402 * -> ip_ll_multireq de-references ill_ipif for checking
403 403 * POINTOPOINT.
404 404 *
405 405 * If ill_ipif was not properly initialized (i.e low on memory),
406 406 * then no interfaces to clean up. In this case just clean up the
407 407 * ill.
408 408 */
409 409 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
410 410 ipif_free(ipif);
411 411
412 412 /*
413 413 * clean out all the nce_t entries that depend on this
414 414 * ill for the ill_phys_addr.
415 415 */
416 416 nce_flush(ill, B_TRUE);
417 417
418 418 /* Clean up msgs on pending upcalls for mrouted */
419 419 reset_mrt_ill(ill);
420 420
421 421 update_conn_ill(ill, ipst);
422 422
423 423 /*
424 424 * Remove multicast references added as a result of calls to
425 425 * ip_join_allmulti().
426 426 */
427 427 ip_purge_allmulti(ill);
428 428
429 429 /*
430 430 * If the ill being deleted is under IPMP, boot it out of the illgrp.
431 431 */
432 432 if (IS_UNDER_IPMP(ill))
433 433 ipmp_ill_leave_illgrp(ill);
434 434
435 435 /*
436 436 * ill_down will arrange to blow off any IRE's dependent on this
437 437 * ILL, and shut down fragmentation reassembly.
438 438 */
439 439 ill_down(ill);
440 440
441 441 /* Let SCTP know, so that it can remove this from its list. */
442 442 sctp_update_ill(ill, SCTP_ILL_REMOVE);
443 443
444 444 /*
445 445 * Walk all CONNs that can have a reference on an ire or nce for this
446 446 * ill (we actually walk all that now have stale references).
447 447 */
448 448 ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
449 449
450 450 /* With IPv6 we have dce_ifindex. Cleanup for neatness */
451 451 if (ill->ill_isv6)
452 452 dce_cleanup(ill->ill_phyint->phyint_ifindex, ipst);
453 453
454 454 /*
455 455 * If an address on this ILL is being used as a source address then
456 456 * clear out the pointers in other ILLs that point to this ILL.
457 457 */
458 458 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
459 459 if (ill->ill_usesrc_grp_next != NULL) {
460 460 if (ill->ill_usesrc_ifindex == 0) { /* usesrc ILL ? */
461 461 ill_disband_usesrc_group(ill);
462 462 } else { /* consumer of the usesrc ILL */
463 463 prev_ill = ill_prev_usesrc(ill);
464 464 prev_ill->ill_usesrc_grp_next =
465 465 ill->ill_usesrc_grp_next;
466 466 }
467 467 }
468 468 rw_exit(&ipst->ips_ill_g_usesrc_lock);
469 469 }
470 470
471 471 static void
472 472 ipif_non_duplicate(ipif_t *ipif)
473 473 {
474 474 ill_t *ill = ipif->ipif_ill;
475 475 mutex_enter(&ill->ill_lock);
476 476 if (ipif->ipif_flags & IPIF_DUPLICATE) {
477 477 ipif->ipif_flags &= ~IPIF_DUPLICATE;
478 478 ASSERT(ill->ill_ipif_dup_count > 0);
479 479 ill->ill_ipif_dup_count--;
480 480 }
481 481 mutex_exit(&ill->ill_lock);
482 482 }
483 483
484 484 /*
485 485 * ill_delete_tail is called from ip_modclose after all references
486 486 * to the closing ill are gone. The wait is done in ip_modclose
487 487 */
488 488 void
489 489 ill_delete_tail(ill_t *ill)
490 490 {
491 491 mblk_t **mpp;
492 492 ipif_t *ipif;
493 493 ip_stack_t *ipst = ill->ill_ipst;
494 494
495 495 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
496 496 ipif_non_duplicate(ipif);
497 497 (void) ipif_down_tail(ipif);
498 498 }
499 499
500 500 ASSERT(ill->ill_ipif_dup_count == 0);
501 501
502 502 /*
503 503 * If polling capability is enabled (which signifies direct
504 504 * upcall into IP and driver has ill saved as a handle),
505 505 * we need to make sure that unbind has completed before we
506 506 * let the ill disappear and driver no longer has any reference
507 507 * to this ill.
508 508 */
509 509 mutex_enter(&ill->ill_lock);
510 510 while (ill->ill_state_flags & ILL_DL_UNBIND_IN_PROGRESS)
511 511 cv_wait(&ill->ill_cv, &ill->ill_lock);
512 512 mutex_exit(&ill->ill_lock);
513 513 ASSERT(!(ill->ill_capabilities &
514 514 (ILL_CAPAB_DLD | ILL_CAPAB_DLD_POLL | ILL_CAPAB_DLD_DIRECT)));
515 515
516 516 if (ill->ill_net_type != IRE_LOOPBACK)
517 517 qprocsoff(ill->ill_rq);
518 518
519 519 /*
520 520 * We do an ipsq_flush once again now. New messages could have
521 521 * landed up from below (M_ERROR or M_HANGUP). Similarly ioctls
522 522 * could also have landed up if an ioctl thread had looked up
523 523 * the ill before we set the ILL_CONDEMNED flag, but not yet
524 524 * enqueued the ioctl when we did the ipsq_flush last time.
525 525 */
526 526 ipsq_flush(ill);
527 527
528 528 /*
529 529 * Free capabilities.
530 530 */
531 531 if (ill->ill_hcksum_capab != NULL) {
532 532 kmem_free(ill->ill_hcksum_capab, sizeof (ill_hcksum_capab_t));
533 533 ill->ill_hcksum_capab = NULL;
534 534 }
535 535
536 536 if (ill->ill_zerocopy_capab != NULL) {
537 537 kmem_free(ill->ill_zerocopy_capab,
538 538 sizeof (ill_zerocopy_capab_t));
539 539 ill->ill_zerocopy_capab = NULL;
540 540 }
541 541
542 542 if (ill->ill_lso_capab != NULL) {
543 543 kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
544 544 ill->ill_lso_capab = NULL;
545 545 }
546 546
547 547 if (ill->ill_dld_capab != NULL) {
548 548 kmem_free(ill->ill_dld_capab, sizeof (ill_dld_capab_t));
549 549 ill->ill_dld_capab = NULL;
550 550 }
551 551
552 552 /* Clean up ill_allowed_ips* related state */
553 553 if (ill->ill_allowed_ips != NULL) {
554 554 ASSERT(ill->ill_allowed_ips_cnt > 0);
555 555 kmem_free(ill->ill_allowed_ips,
556 556 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
557 557 ill->ill_allowed_ips = NULL;
558 558 ill->ill_allowed_ips_cnt = 0;
559 559 }
560 560
561 561 while (ill->ill_ipif != NULL)
562 562 ipif_free_tail(ill->ill_ipif);
563 563
564 564 /*
565 565 * We have removed all references to ilm from conn and the ones joined
566 566 * within the kernel.
567 567 *
568 568 * We don't walk conns, mrts and ires because
569 569 *
570 570 * 1) update_conn_ill and reset_mrt_ill cleans up conns and mrts.
571 571 * 2) ill_down ->ill_downi walks all the ires and cleans up
572 572 * ill references.
573 573 */
574 574
575 575 /*
576 576 * If this ill is an IPMP meta-interface, blow away the illgrp. This
577 577 * is safe to do because the illgrp has already been unlinked from the
578 578 * group by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find it.
579 579 */
580 580 if (IS_IPMP(ill)) {
581 581 ipmp_illgrp_destroy(ill->ill_grp);
582 582 ill->ill_grp = NULL;
583 583 }
584 584
585 585 if (ill->ill_mphysaddr_list != NULL) {
586 586 multiphysaddr_t *mpa, *tmpa;
587 587
588 588 mpa = ill->ill_mphysaddr_list;
589 589 ill->ill_mphysaddr_list = NULL;
590 590 while (mpa) {
591 591 tmpa = mpa->mpa_next;
592 592 kmem_free(mpa, sizeof (*mpa));
593 593 mpa = tmpa;
594 594 }
595 595 }
596 596 /*
597 597 * Take us out of the list of ILLs. ill_glist_delete -> phyint_free
598 598 * could free the phyint. No more reference to the phyint after this
599 599 * point.
600 600 */
601 601 (void) ill_glist_delete(ill);
602 602
603 603 if (ill->ill_frag_ptr != NULL) {
604 604 uint_t count;
605 605
606 606 for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
607 607 mutex_destroy(&ill->ill_frag_hash_tbl[count].ipfb_lock);
608 608 }
609 609 mi_free(ill->ill_frag_ptr);
610 610 ill->ill_frag_ptr = NULL;
611 611 ill->ill_frag_hash_tbl = NULL;
612 612 }
613 613
614 614 freemsg(ill->ill_nd_lla_mp);
615 615 /* Free all retained control messages. */
616 616 mpp = &ill->ill_first_mp_to_free;
617 617 do {
618 618 while (mpp[0]) {
619 619 mblk_t *mp;
620 620 mblk_t *mp1;
621 621
622 622 mp = mpp[0];
623 623 mpp[0] = mp->b_next;
624 624 for (mp1 = mp; mp1 != NULL; mp1 = mp1->b_cont) {
625 625 mp1->b_next = NULL;
626 626 mp1->b_prev = NULL;
627 627 }
628 628 freemsg(mp);
629 629 }
630 630 } while (mpp++ != &ill->ill_last_mp_to_free);
631 631
632 632 ill_free_mib(ill);
633 633
634 634 #ifdef DEBUG
635 635 ill_trace_cleanup(ill);
636 636 #endif
637 637
638 638 /* The default multicast interface might have changed */
639 639 ire_increment_multicast_generation(ipst, ill->ill_isv6);
640 640
641 641 /* Drop refcnt here */
642 642 netstack_rele(ill->ill_ipst->ips_netstack);
643 643 ill->ill_ipst = NULL;
644 644 }
645 645
646 646 static void
647 647 ill_free_mib(ill_t *ill)
648 648 {
649 649 ip_stack_t *ipst = ill->ill_ipst;
650 650
651 651 /*
652 652 * MIB statistics must not be lost, so when an interface
653 653 * goes away the counter values will be added to the global
654 654 * MIBs.
655 655 */
656 656 if (ill->ill_ip_mib != NULL) {
657 657 if (ill->ill_isv6) {
658 658 ip_mib2_add_ip_stats(&ipst->ips_ip6_mib,
659 659 ill->ill_ip_mib);
660 660 } else {
661 661 ip_mib2_add_ip_stats(&ipst->ips_ip_mib,
662 662 ill->ill_ip_mib);
663 663 }
664 664
665 665 kmem_free(ill->ill_ip_mib, sizeof (*ill->ill_ip_mib));
666 666 ill->ill_ip_mib = NULL;
667 667 }
668 668 if (ill->ill_icmp6_mib != NULL) {
669 669 ip_mib2_add_icmp6_stats(&ipst->ips_icmp6_mib,
670 670 ill->ill_icmp6_mib);
671 671 kmem_free(ill->ill_icmp6_mib, sizeof (*ill->ill_icmp6_mib));
672 672 ill->ill_icmp6_mib = NULL;
673 673 }
674 674 }
675 675
676 676 /*
677 677 * Concatenate together a physical address and a sap.
678 678 *
679 679 * Sap_lengths are interpreted as follows:
680 680 * sap_length == 0 ==> no sap
681 681 * sap_length > 0 ==> sap is at the head of the dlpi address
682 682 * sap_length < 0 ==> sap is at the tail of the dlpi address
683 683 */
684 684 static void
685 685 ill_dlur_copy_address(uchar_t *phys_src, uint_t phys_length,
686 686 t_scalar_t sap_src, t_scalar_t sap_length, uchar_t *dst)
687 687 {
688 688 uint16_t sap_addr = (uint16_t)sap_src;
689 689
690 690 if (sap_length == 0) {
691 691 if (phys_src == NULL)
692 692 bzero(dst, phys_length);
693 693 else
694 694 bcopy(phys_src, dst, phys_length);
695 695 } else if (sap_length < 0) {
696 696 if (phys_src == NULL)
697 697 bzero(dst, phys_length);
698 698 else
699 699 bcopy(phys_src, dst, phys_length);
700 700 bcopy(&sap_addr, (char *)dst + phys_length, sizeof (sap_addr));
701 701 } else {
702 702 bcopy(&sap_addr, dst, sizeof (sap_addr));
703 703 if (phys_src == NULL)
704 704 bzero((char *)dst + sap_length, phys_length);
705 705 else
706 706 bcopy(phys_src, (char *)dst + sap_length, phys_length);
707 707 }
708 708 }
709 709
710 710 /*
711 711 * Generate a dl_unitdata_req mblk for the device and address given.
712 712 * addr_length is the length of the physical portion of the address.
713 713 * If addr is NULL include an all zero address of the specified length.
714 714 * TRUE? In any case, addr_length is taken to be the entire length of the
715 715 * dlpi address, including the absolute value of sap_length.
716 716 */
717 717 mblk_t *
718 718 ill_dlur_gen(uchar_t *addr, uint_t addr_length, t_uscalar_t sap,
719 719 t_scalar_t sap_length)
720 720 {
721 721 dl_unitdata_req_t *dlur;
722 722 mblk_t *mp;
723 723 t_scalar_t abs_sap_length; /* absolute value */
724 724
725 725 abs_sap_length = ABS(sap_length);
726 726 mp = ip_dlpi_alloc(sizeof (*dlur) + addr_length + abs_sap_length,
727 727 DL_UNITDATA_REQ);
728 728 if (mp == NULL)
729 729 return (NULL);
730 730 dlur = (dl_unitdata_req_t *)mp->b_rptr;
731 731 /* HACK: accomodate incompatible DLPI drivers */
732 732 if (addr_length == 8)
733 733 addr_length = 6;
734 734 dlur->dl_dest_addr_length = addr_length + abs_sap_length;
735 735 dlur->dl_dest_addr_offset = sizeof (*dlur);
736 736 dlur->dl_priority.dl_min = 0;
737 737 dlur->dl_priority.dl_max = 0;
738 738 ill_dlur_copy_address(addr, addr_length, sap, sap_length,
739 739 (uchar_t *)&dlur[1]);
740 740 return (mp);
741 741 }
742 742
743 743 /*
744 744 * Add the pending mp to the list. There can be only 1 pending mp
745 745 * in the list. Any exclusive ioctl that needs to wait for a response
746 746 * from another module or driver needs to use this function to set
747 747 * the ipx_pending_mp to the ioctl mblk and wait for the response from
748 748 * the other module/driver. This is also used while waiting for the
749 749 * ipif/ill/ire refcnts to drop to zero in bringing down an ipif.
750 750 */
751 751 boolean_t
752 752 ipsq_pending_mp_add(conn_t *connp, ipif_t *ipif, queue_t *q, mblk_t *add_mp,
753 753 int waitfor)
754 754 {
755 755 ipxop_t *ipx = ipif->ipif_ill->ill_phyint->phyint_ipsq->ipsq_xop;
756 756
757 757 ASSERT(IAM_WRITER_IPIF(ipif));
758 758 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
759 759 ASSERT((add_mp->b_next == NULL) && (add_mp->b_prev == NULL));
760 760 ASSERT(ipx->ipx_pending_mp == NULL);
761 761 /*
762 762 * The caller may be using a different ipif than the one passed into
763 763 * ipsq_current_start() (e.g., suppose an ioctl that came in on the V4
764 764 * ill needs to wait for the V6 ill to quiesce). So we can't ASSERT
765 765 * that `ipx_current_ipif == ipif'.
766 766 */
767 767 ASSERT(ipx->ipx_current_ipif != NULL);
768 768
769 769 /*
770 770 * M_IOCDATA from ioctls, M_ERROR/M_HANGUP/M_PROTO/M_PCPROTO from the
771 771 * driver.
772 772 */
773 773 ASSERT((DB_TYPE(add_mp) == M_IOCDATA) || (DB_TYPE(add_mp) == M_ERROR) ||
774 774 (DB_TYPE(add_mp) == M_HANGUP) || (DB_TYPE(add_mp) == M_PROTO) ||
775 775 (DB_TYPE(add_mp) == M_PCPROTO));
776 776
777 777 if (connp != NULL) {
778 778 ASSERT(MUTEX_HELD(&connp->conn_lock));
779 779 /*
780 780 * Return error if the conn has started closing. The conn
781 781 * could have finished cleaning up the pending mp list,
782 782 * If so we should not add another mp to the list negating
783 783 * the cleanup.
784 784 */
785 785 if (connp->conn_state_flags & CONN_CLOSING)
786 786 return (B_FALSE);
787 787 }
788 788 mutex_enter(&ipx->ipx_lock);
789 789 ipx->ipx_pending_ipif = ipif;
790 790 /*
791 791 * Note down the queue in b_queue. This will be returned by
792 792 * ipsq_pending_mp_get. Caller will then use these values to restart
793 793 * the processing
794 794 */
795 795 add_mp->b_next = NULL;
796 796 add_mp->b_queue = q;
797 797 ipx->ipx_pending_mp = add_mp;
798 798 ipx->ipx_waitfor = waitfor;
799 799 mutex_exit(&ipx->ipx_lock);
800 800
801 801 if (connp != NULL)
802 802 connp->conn_oper_pending_ill = ipif->ipif_ill;
803 803
804 804 return (B_TRUE);
805 805 }
806 806
807 807 /*
808 808 * Retrieve the ipx_pending_mp and return it. There can be only 1 mp
809 809 * queued in the list.
810 810 */
811 811 mblk_t *
812 812 ipsq_pending_mp_get(ipsq_t *ipsq, conn_t **connpp)
813 813 {
814 814 mblk_t *curr = NULL;
815 815 ipxop_t *ipx = ipsq->ipsq_xop;
816 816
817 817 *connpp = NULL;
818 818 mutex_enter(&ipx->ipx_lock);
819 819 if (ipx->ipx_pending_mp == NULL) {
820 820 mutex_exit(&ipx->ipx_lock);
821 821 return (NULL);
822 822 }
823 823
824 824 /* There can be only 1 such excl message */
825 825 curr = ipx->ipx_pending_mp;
826 826 ASSERT(curr->b_next == NULL);
827 827 ipx->ipx_pending_ipif = NULL;
828 828 ipx->ipx_pending_mp = NULL;
829 829 ipx->ipx_waitfor = 0;
830 830 mutex_exit(&ipx->ipx_lock);
831 831
832 832 if (CONN_Q(curr->b_queue)) {
833 833 /*
834 834 * This mp did a refhold on the conn, at the start of the ioctl.
835 835 * So we can safely return a pointer to the conn to the caller.
836 836 */
837 837 *connpp = Q_TO_CONN(curr->b_queue);
838 838 } else {
839 839 *connpp = NULL;
840 840 }
841 841 curr->b_next = NULL;
842 842 curr->b_prev = NULL;
843 843 return (curr);
844 844 }
845 845
846 846 /*
847 847 * Cleanup the ioctl mp queued in ipx_pending_mp
848 848 * - Called in the ill_delete path
849 849 * - Called in the M_ERROR or M_HANGUP path on the ill.
850 850 * - Called in the conn close path.
851 851 *
852 852 * Returns success on finding the pending mblk associated with the ioctl or
853 853 * exclusive operation in progress, failure otherwise.
854 854 */
855 855 boolean_t
856 856 ipsq_pending_mp_cleanup(ill_t *ill, conn_t *connp)
857 857 {
858 858 mblk_t *mp;
859 859 ipxop_t *ipx;
860 860 queue_t *q;
861 861 ipif_t *ipif;
862 862 int cmd;
863 863
864 864 ASSERT(IAM_WRITER_ILL(ill));
865 865 ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
866 866
867 867 mutex_enter(&ipx->ipx_lock);
868 868 mp = ipx->ipx_pending_mp;
869 869 if (connp != NULL) {
870 870 if (mp == NULL || mp->b_queue != CONNP_TO_WQ(connp)) {
871 871 /*
872 872 * Nothing to clean since the conn that is closing
873 873 * does not have a matching pending mblk in
874 874 * ipx_pending_mp.
875 875 */
876 876 mutex_exit(&ipx->ipx_lock);
877 877 return (B_FALSE);
878 878 }
879 879 } else {
880 880 /*
881 881 * A non-zero ill_error signifies we are called in the
882 882 * M_ERROR or M_HANGUP path and we need to unconditionally
883 883 * abort any current ioctl and do the corresponding cleanup.
884 884 * A zero ill_error means we are in the ill_delete path and
885 885 * we do the cleanup only if there is a pending mp.
886 886 */
887 887 if (mp == NULL && ill->ill_error == 0) {
888 888 mutex_exit(&ipx->ipx_lock);
889 889 return (B_FALSE);
890 890 }
891 891 }
892 892
893 893 /* Now remove from the ipx_pending_mp */
894 894 ipx->ipx_pending_mp = NULL;
895 895 ipif = ipx->ipx_pending_ipif;
896 896 ipx->ipx_pending_ipif = NULL;
897 897 ipx->ipx_waitfor = 0;
898 898 ipx->ipx_current_ipif = NULL;
899 899 cmd = ipx->ipx_current_ioctl;
900 900 ipx->ipx_current_ioctl = 0;
901 901 ipx->ipx_current_done = B_TRUE;
902 902 mutex_exit(&ipx->ipx_lock);
903 903
904 904 if (mp == NULL)
905 905 return (B_FALSE);
906 906
907 907 q = mp->b_queue;
908 908 mp->b_next = NULL;
909 909 mp->b_prev = NULL;
910 910 mp->b_queue = NULL;
911 911
912 912 if (DB_TYPE(mp) == M_IOCTL || DB_TYPE(mp) == M_IOCDATA) {
913 913 DTRACE_PROBE4(ipif__ioctl,
914 914 char *, "ipsq_pending_mp_cleanup",
915 915 int, cmd, ill_t *, ipif == NULL ? NULL : ipif->ipif_ill,
916 916 ipif_t *, ipif);
917 917 if (connp == NULL) {
918 918 ip_ioctl_finish(q, mp, ENXIO, NO_COPYOUT, NULL);
919 919 } else {
920 920 ip_ioctl_finish(q, mp, ENXIO, CONN_CLOSE, NULL);
921 921 mutex_enter(&ipif->ipif_ill->ill_lock);
922 922 ipif->ipif_state_flags &= ~IPIF_CHANGING;
923 923 mutex_exit(&ipif->ipif_ill->ill_lock);
924 924 }
925 925 } else {
926 926 inet_freemsg(mp);
927 927 }
928 928 return (B_TRUE);
929 929 }
930 930
931 931 /*
932 932 * Called in the conn close path and ill delete path
933 933 */
934 934 static void
935 935 ipsq_xopq_mp_cleanup(ill_t *ill, conn_t *connp)
936 936 {
937 937 ipsq_t *ipsq;
938 938 mblk_t *prev;
939 939 mblk_t *curr;
940 940 mblk_t *next;
941 941 queue_t *wq, *rq = NULL;
942 942 mblk_t *tmp_list = NULL;
943 943
944 944 ASSERT(IAM_WRITER_ILL(ill));
945 945 if (connp != NULL)
946 946 wq = CONNP_TO_WQ(connp);
947 947 else
948 948 wq = ill->ill_wq;
949 949
950 950 /*
951 951 * In the case of lo0 being unplumbed, ill_wq will be NULL. Guard
952 952 * against this here.
953 953 */
954 954 if (wq != NULL)
955 955 rq = RD(wq);
956 956
957 957 ipsq = ill->ill_phyint->phyint_ipsq;
958 958 /*
959 959 * Cleanup the ioctl mp's queued in ipsq_xopq_pending_mp if any.
960 960 * In the case of ioctl from a conn, there can be only 1 mp
961 961 * queued on the ipsq. If an ill is being unplumbed flush all
962 962 * the messages.
963 963 */
964 964 mutex_enter(&ipsq->ipsq_lock);
965 965 for (prev = NULL, curr = ipsq->ipsq_xopq_mphead; curr != NULL;
966 966 curr = next) {
967 967 next = curr->b_next;
968 968 if (connp == NULL ||
969 969 (curr->b_queue == wq || curr->b_queue == rq)) {
970 970 /* Unlink the mblk from the pending mp list */
971 971 if (prev != NULL) {
972 972 prev->b_next = curr->b_next;
973 973 } else {
974 974 ASSERT(ipsq->ipsq_xopq_mphead == curr);
975 975 ipsq->ipsq_xopq_mphead = curr->b_next;
976 976 }
977 977 if (ipsq->ipsq_xopq_mptail == curr)
978 978 ipsq->ipsq_xopq_mptail = prev;
979 979 /*
980 980 * Create a temporary list and release the ipsq lock
981 981 * New elements are added to the head of the tmp_list
982 982 */
983 983 curr->b_next = tmp_list;
984 984 tmp_list = curr;
985 985 } else {
986 986 prev = curr;
987 987 }
988 988 }
989 989 mutex_exit(&ipsq->ipsq_lock);
990 990
991 991 while (tmp_list != NULL) {
992 992 curr = tmp_list;
993 993 tmp_list = curr->b_next;
994 994 curr->b_next = NULL;
995 995 curr->b_prev = NULL;
996 996 wq = curr->b_queue;
997 997 curr->b_queue = NULL;
998 998 if (DB_TYPE(curr) == M_IOCTL || DB_TYPE(curr) == M_IOCDATA) {
999 999 DTRACE_PROBE4(ipif__ioctl,
1000 1000 char *, "ipsq_xopq_mp_cleanup",
1001 1001 int, 0, ill_t *, NULL, ipif_t *, NULL);
1002 1002 ip_ioctl_finish(wq, curr, ENXIO, connp != NULL ?
1003 1003 CONN_CLOSE : NO_COPYOUT, NULL);
1004 1004 } else {
1005 1005 /*
1006 1006 * IP-MT XXX In the case of TLI/XTI bind / optmgmt
1007 1007 * this can't be just inet_freemsg. we have to
1008 1008 * restart it otherwise the thread will be stuck.
1009 1009 */
1010 1010 inet_freemsg(curr);
1011 1011 }
1012 1012 }
1013 1013 }
1014 1014
1015 1015 /*
1016 1016 * This conn has started closing. Cleanup any pending ioctl from this conn.
1017 1017 * STREAMS ensures that there can be at most 1 active ioctl on a stream.
1018 1018 */
1019 1019 void
1020 1020 conn_ioctl_cleanup(conn_t *connp)
1021 1021 {
1022 1022 ipsq_t *ipsq;
1023 1023 ill_t *ill;
1024 1024 boolean_t refheld;
1025 1025
1026 1026 /*
1027 1027 * Check for a queued ioctl. If the ioctl has not yet started, the mp
1028 1028 * is pending in the list headed by ipsq_xopq_head. If the ioctl has
1029 1029 * started the mp could be present in ipx_pending_mp. Note that if
1030 1030 * conn_oper_pending_ill is NULL, the ioctl may still be in flight and
1031 1031 * not yet queued anywhere. In this case, the conn close code will wait
1032 1032 * until the conn_ref is dropped. If the stream was a tcp stream, then
1033 1033 * tcp_close will wait first until all ioctls have completed for this
1034 1034 * conn.
1035 1035 */
1036 1036 mutex_enter(&connp->conn_lock);
1037 1037 ill = connp->conn_oper_pending_ill;
1038 1038 if (ill == NULL) {
1039 1039 mutex_exit(&connp->conn_lock);
1040 1040 return;
1041 1041 }
1042 1042
1043 1043 /*
1044 1044 * We may not be able to refhold the ill if the ill/ipif
1045 1045 * is changing. But we need to make sure that the ill will
1046 1046 * not vanish. So we just bump up the ill_waiter count.
1047 1047 */
1048 1048 refheld = ill_waiter_inc(ill);
1049 1049 mutex_exit(&connp->conn_lock);
1050 1050 if (refheld) {
1051 1051 if (ipsq_enter(ill, B_TRUE, NEW_OP)) {
1052 1052 ill_waiter_dcr(ill);
1053 1053 /*
1054 1054 * Check whether this ioctl has started and is
1055 1055 * pending. If it is not found there then check
1056 1056 * whether this ioctl has not even started and is in
1057 1057 * the ipsq_xopq list.
1058 1058 */
1059 1059 if (!ipsq_pending_mp_cleanup(ill, connp))
1060 1060 ipsq_xopq_mp_cleanup(ill, connp);
1061 1061 ipsq = ill->ill_phyint->phyint_ipsq;
1062 1062 ipsq_exit(ipsq);
1063 1063 return;
1064 1064 }
1065 1065 }
1066 1066
1067 1067 /*
1068 1068 * The ill is also closing and we could not bump up the
1069 1069 * ill_waiter_count or we could not enter the ipsq. Leave
1070 1070 * the cleanup to ill_delete
1071 1071 */
1072 1072 mutex_enter(&connp->conn_lock);
1073 1073 while (connp->conn_oper_pending_ill != NULL)
1074 1074 cv_wait(&connp->conn_refcv, &connp->conn_lock);
1075 1075 mutex_exit(&connp->conn_lock);
1076 1076 if (refheld)
1077 1077 ill_waiter_dcr(ill);
1078 1078 }
1079 1079
1080 1080 /*
1081 1081 * ipcl_walk function for cleaning up conn_*_ill fields.
1082 1082 * Note that we leave ixa_multicast_ifindex, conn_incoming_ifindex, and
1083 1083 * conn_bound_if in place. We prefer dropping
1084 1084 * packets instead of sending them out the wrong interface, or accepting
1085 1085 * packets from the wrong ifindex.
1086 1086 */
1087 1087 static void
1088 1088 conn_cleanup_ill(conn_t *connp, caddr_t arg)
1089 1089 {
1090 1090 ill_t *ill = (ill_t *)arg;
1091 1091
1092 1092 mutex_enter(&connp->conn_lock);
1093 1093 if (connp->conn_dhcpinit_ill == ill) {
1094 1094 connp->conn_dhcpinit_ill = NULL;
1095 1095 ASSERT(ill->ill_dhcpinit != 0);
1096 1096 atomic_dec_32(&ill->ill_dhcpinit);
1097 1097 ill_set_inputfn(ill);
1098 1098 }
1099 1099 mutex_exit(&connp->conn_lock);
1100 1100 }
1101 1101
1102 1102 static int
1103 1103 ill_down_ipifs_tail(ill_t *ill)
1104 1104 {
1105 1105 ipif_t *ipif;
1106 1106 int err;
1107 1107
1108 1108 ASSERT(IAM_WRITER_ILL(ill));
1109 1109 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
1110 1110 ipif_non_duplicate(ipif);
1111 1111 /*
1112 1112 * ipif_down_tail will call arp_ll_down on the last ipif
1113 1113 * and typically return EINPROGRESS when the DL_UNBIND is sent.
1114 1114 */
1115 1115 if ((err = ipif_down_tail(ipif)) != 0)
1116 1116 return (err);
1117 1117 }
1118 1118 return (0);
1119 1119 }
1120 1120
1121 1121 /* ARGSUSED */
1122 1122 void
1123 1123 ipif_all_down_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
1124 1124 {
1125 1125 ASSERT(IAM_WRITER_IPSQ(ipsq));
1126 1126 (void) ill_down_ipifs_tail(q->q_ptr);
1127 1127 freemsg(mp);
1128 1128 ipsq_current_finish(ipsq);
1129 1129 }
1130 1130
1131 1131 /*
1132 1132 * ill_down_start is called when we want to down this ill and bring it up again
1133 1133 * It is called when we receive an M_ERROR / M_HANGUP. In this case we shut down
1134 1134 * all interfaces, but don't tear down any plumbing.
1135 1135 */
1136 1136 boolean_t
1137 1137 ill_down_start(queue_t *q, mblk_t *mp)
1138 1138 {
1139 1139 ill_t *ill = q->q_ptr;
1140 1140 ipif_t *ipif;
1141 1141
1142 1142 ASSERT(IAM_WRITER_ILL(ill));
1143 1143 /*
1144 1144 * It is possible that some ioctl is already in progress while we
1145 1145 * received the M_ERROR / M_HANGUP in which case, we need to abort
1146 1146 * the ioctl. ill_down_start() is being processed as CUR_OP rather
1147 1147 * than as NEW_OP since the cause of the M_ERROR / M_HANGUP may prevent
1148 1148 * the in progress ioctl from ever completing.
1149 1149 *
1150 1150 * The thread that started the ioctl (if any) must have returned,
1151 1151 * since we are now executing as writer. After the 2 calls below,
1152 1152 * the state of the ipsq and the ill would reflect no trace of any
1153 1153 * pending operation. Subsequently if there is any response to the
1154 1154 * original ioctl from the driver, it would be discarded as an
1155 1155 * unsolicited message from the driver.
1156 1156 */
1157 1157 (void) ipsq_pending_mp_cleanup(ill, NULL);
1158 1158 ill_dlpi_clear_deferred(ill);
1159 1159
1160 1160 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
1161 1161 (void) ipif_down(ipif, NULL, NULL);
1162 1162
1163 1163 ill_down(ill);
1164 1164
1165 1165 /*
1166 1166 * Walk all CONNs that can have a reference on an ire or nce for this
1167 1167 * ill (we actually walk all that now have stale references).
1168 1168 */
1169 1169 ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ill->ill_ipst);
1170 1170
1171 1171 /* With IPv6 we have dce_ifindex. Cleanup for neatness */
1172 1172 if (ill->ill_isv6)
1173 1173 dce_cleanup(ill->ill_phyint->phyint_ifindex, ill->ill_ipst);
1174 1174
1175 1175 ipsq_current_start(ill->ill_phyint->phyint_ipsq, ill->ill_ipif, 0);
1176 1176
1177 1177 /*
1178 1178 * Atomically test and add the pending mp if references are active.
1179 1179 */
1180 1180 mutex_enter(&ill->ill_lock);
1181 1181 if (!ill_is_quiescent(ill)) {
1182 1182 /* call cannot fail since `conn_t *' argument is NULL */
1183 1183 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
1184 1184 mp, ILL_DOWN);
1185 1185 mutex_exit(&ill->ill_lock);
1186 1186 return (B_FALSE);
1187 1187 }
1188 1188 mutex_exit(&ill->ill_lock);
1189 1189 return (B_TRUE);
1190 1190 }
1191 1191
1192 1192 static void
1193 1193 ill_down(ill_t *ill)
1194 1194 {
1195 1195 mblk_t *mp;
1196 1196 ip_stack_t *ipst = ill->ill_ipst;
1197 1197
1198 1198 /*
1199 1199 * Blow off any IREs dependent on this ILL.
1200 1200 * The caller needs to handle conn_ixa_cleanup
1201 1201 */
1202 1202 ill_delete_ires(ill);
1203 1203
1204 1204 ire_walk_ill(0, 0, ill_downi, ill, ill);
1205 1205
1206 1206 /* Remove any conn_*_ill depending on this ill */
1207 1207 ipcl_walk(conn_cleanup_ill, (caddr_t)ill, ipst);
1208 1208
1209 1209 /*
1210 1210 * Free state for additional IREs.
1211 1211 */
1212 1212 mutex_enter(&ill->ill_saved_ire_lock);
1213 1213 mp = ill->ill_saved_ire_mp;
1214 1214 ill->ill_saved_ire_mp = NULL;
1215 1215 ill->ill_saved_ire_cnt = 0;
1216 1216 mutex_exit(&ill->ill_saved_ire_lock);
1217 1217 freemsg(mp);
1218 1218 }
1219 1219
1220 1220 /*
1221 1221 * ire_walk routine used to delete every IRE that depends on
1222 1222 * 'ill'. (Always called as writer, and may only be called from ire_walk.)
1223 1223 *
1224 1224 * Note: since the routes added by the kernel are deleted separately,
1225 1225 * this will only be 1) IRE_IF_CLONE and 2) manually added IRE_INTERFACE.
1226 1226 *
1227 1227 * We also remove references on ire_nce_cache entries that refer to the ill.
1228 1228 */
1229 1229 void
1230 1230 ill_downi(ire_t *ire, char *ill_arg)
1231 1231 {
1232 1232 ill_t *ill = (ill_t *)ill_arg;
1233 1233 nce_t *nce;
1234 1234
1235 1235 mutex_enter(&ire->ire_lock);
1236 1236 nce = ire->ire_nce_cache;
1237 1237 if (nce != NULL && nce->nce_ill == ill)
1238 1238 ire->ire_nce_cache = NULL;
1239 1239 else
1240 1240 nce = NULL;
1241 1241 mutex_exit(&ire->ire_lock);
1242 1242 if (nce != NULL)
1243 1243 nce_refrele(nce);
1244 1244 if (ire->ire_ill == ill) {
1245 1245 /*
1246 1246 * The existing interface binding for ire must be
1247 1247 * deleted before trying to bind the route to another
1248 1248 * interface. However, since we are using the contents of the
1249 1249 * ire after ire_delete, the caller has to ensure that
1250 1250 * CONDEMNED (deleted) ire's are not removed from the list
1251 1251 * when ire_delete() returns. Currently ill_downi() is
1252 1252 * only called as part of ire_walk*() routines, so that
1253 1253 * the irb_refhold() done by ire_walk*() will ensure that
1254 1254 * ire_delete() does not lead to ire_inactive().
1255 1255 */
1256 1256 ASSERT(ire->ire_bucket->irb_refcnt > 0);
1257 1257 ire_delete(ire);
1258 1258 if (ire->ire_unbound)
1259 1259 ire_rebind(ire);
1260 1260 }
1261 1261 }
1262 1262
1263 1263 /* Remove IRE_IF_CLONE on this ill */
1264 1264 void
1265 1265 ill_downi_if_clone(ire_t *ire, char *ill_arg)
1266 1266 {
1267 1267 ill_t *ill = (ill_t *)ill_arg;
1268 1268
1269 1269 ASSERT(ire->ire_type & IRE_IF_CLONE);
1270 1270 if (ire->ire_ill == ill)
1271 1271 ire_delete(ire);
1272 1272 }
1273 1273
1274 1274 /* Consume an M_IOCACK of the fastpath probe. */
1275 1275 void
1276 1276 ill_fastpath_ack(ill_t *ill, mblk_t *mp)
1277 1277 {
1278 1278 mblk_t *mp1 = mp;
1279 1279
1280 1280 /*
1281 1281 * If this was the first attempt turn on the fastpath probing.
1282 1282 */
1283 1283 mutex_enter(&ill->ill_lock);
1284 1284 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS)
1285 1285 ill->ill_dlpi_fastpath_state = IDS_OK;
1286 1286 mutex_exit(&ill->ill_lock);
1287 1287
1288 1288 /* Free the M_IOCACK mblk, hold on to the data */
1289 1289 mp = mp->b_cont;
1290 1290 freeb(mp1);
1291 1291 if (mp == NULL)
1292 1292 return;
1293 1293 if (mp->b_cont != NULL)
1294 1294 nce_fastpath_update(ill, mp);
1295 1295 else
1296 1296 ip0dbg(("ill_fastpath_ack: no b_cont\n"));
1297 1297 freemsg(mp);
1298 1298 }
1299 1299
1300 1300 /*
1301 1301 * Throw an M_IOCTL message downstream asking "do you know fastpath?"
1302 1302 * The data portion of the request is a dl_unitdata_req_t template for
1303 1303 * what we would send downstream in the absence of a fastpath confirmation.
1304 1304 */
1305 1305 int
1306 1306 ill_fastpath_probe(ill_t *ill, mblk_t *dlur_mp)
1307 1307 {
1308 1308 struct iocblk *ioc;
1309 1309 mblk_t *mp;
1310 1310
1311 1311 if (dlur_mp == NULL)
1312 1312 return (EINVAL);
1313 1313
1314 1314 mutex_enter(&ill->ill_lock);
1315 1315 switch (ill->ill_dlpi_fastpath_state) {
1316 1316 case IDS_FAILED:
1317 1317 /*
1318 1318 * Driver NAKed the first fastpath ioctl - assume it doesn't
1319 1319 * support it.
1320 1320 */
1321 1321 mutex_exit(&ill->ill_lock);
1322 1322 return (ENOTSUP);
1323 1323 case IDS_UNKNOWN:
1324 1324 /* This is the first probe */
1325 1325 ill->ill_dlpi_fastpath_state = IDS_INPROGRESS;
1326 1326 break;
1327 1327 default:
1328 1328 break;
1329 1329 }
1330 1330 mutex_exit(&ill->ill_lock);
1331 1331
1332 1332 if ((mp = mkiocb(DL_IOC_HDR_INFO)) == NULL)
1333 1333 return (EAGAIN);
1334 1334
1335 1335 mp->b_cont = copyb(dlur_mp);
1336 1336 if (mp->b_cont == NULL) {
1337 1337 freeb(mp);
1338 1338 return (EAGAIN);
1339 1339 }
1340 1340
1341 1341 ioc = (struct iocblk *)mp->b_rptr;
1342 1342 ioc->ioc_count = msgdsize(mp->b_cont);
1343 1343
1344 1344 DTRACE_PROBE3(ill__dlpi, char *, "ill_fastpath_probe",
1345 1345 char *, "DL_IOC_HDR_INFO", ill_t *, ill);
1346 1346 putnext(ill->ill_wq, mp);
1347 1347 return (0);
1348 1348 }
1349 1349
1350 1350 void
1351 1351 ill_capability_probe(ill_t *ill)
1352 1352 {
1353 1353 mblk_t *mp;
1354 1354
1355 1355 ASSERT(IAM_WRITER_ILL(ill));
1356 1356
1357 1357 if (ill->ill_dlpi_capab_state != IDCS_UNKNOWN &&
1358 1358 ill->ill_dlpi_capab_state != IDCS_FAILED)
1359 1359 return;
1360 1360
1361 1361 /*
1362 1362 * We are starting a new cycle of capability negotiation.
1363 1363 * Free up the capab reset messages of any previous incarnation.
1364 1364 * We will do a fresh allocation when we get the response to our probe
1365 1365 */
1366 1366 if (ill->ill_capab_reset_mp != NULL) {
1367 1367 freemsg(ill->ill_capab_reset_mp);
1368 1368 ill->ill_capab_reset_mp = NULL;
1369 1369 }
1370 1370
1371 1371 ip1dbg(("ill_capability_probe: starting capability negotiation\n"));
1372 1372
1373 1373 mp = ip_dlpi_alloc(sizeof (dl_capability_req_t), DL_CAPABILITY_REQ);
1374 1374 if (mp == NULL)
1375 1375 return;
1376 1376
1377 1377 ill_capability_send(ill, mp);
1378 1378 ill->ill_dlpi_capab_state = IDCS_PROBE_SENT;
1379 1379 }
1380 1380
1381 1381 void
1382 1382 ill_capability_reset(ill_t *ill, boolean_t reneg)
1383 1383 {
1384 1384 ASSERT(IAM_WRITER_ILL(ill));
1385 1385
1386 1386 if (ill->ill_dlpi_capab_state != IDCS_OK)
1387 1387 return;
1388 1388
1389 1389 ill->ill_dlpi_capab_state = reneg ? IDCS_RENEG : IDCS_RESET_SENT;
1390 1390
1391 1391 ill_capability_send(ill, ill->ill_capab_reset_mp);
1392 1392 ill->ill_capab_reset_mp = NULL;
1393 1393 /*
1394 1394 * We turn off all capabilities except those pertaining to
1395 1395 * direct function call capabilities viz. ILL_CAPAB_DLD*
1396 1396 * which will be turned off by the corresponding reset functions.
1397 1397 */
1398 1398 ill->ill_capabilities &= ~(ILL_CAPAB_HCKSUM | ILL_CAPAB_ZEROCOPY);
1399 1399 }
1400 1400
1401 1401 static void
1402 1402 ill_capability_reset_alloc(ill_t *ill)
1403 1403 {
1404 1404 mblk_t *mp;
1405 1405 size_t size = 0;
1406 1406 int err;
1407 1407 dl_capability_req_t *capb;
1408 1408
1409 1409 ASSERT(IAM_WRITER_ILL(ill));
1410 1410 ASSERT(ill->ill_capab_reset_mp == NULL);
1411 1411
1412 1412 if (ILL_HCKSUM_CAPABLE(ill)) {
1413 1413 size += sizeof (dl_capability_sub_t) +
1414 1414 sizeof (dl_capab_hcksum_t);
1415 1415 }
1416 1416
1417 1417 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) {
1418 1418 size += sizeof (dl_capability_sub_t) +
1419 1419 sizeof (dl_capab_zerocopy_t);
1420 1420 }
1421 1421
1422 1422 if (ill->ill_capabilities & ILL_CAPAB_DLD) {
1423 1423 size += sizeof (dl_capability_sub_t) +
1424 1424 sizeof (dl_capab_dld_t);
1425 1425 }
1426 1426
1427 1427 mp = allocb_wait(size + sizeof (dl_capability_req_t), BPRI_MED,
1428 1428 STR_NOSIG, &err);
1429 1429
1430 1430 mp->b_datap->db_type = M_PROTO;
1431 1431 bzero(mp->b_rptr, size + sizeof (dl_capability_req_t));
1432 1432
1433 1433 capb = (dl_capability_req_t *)mp->b_rptr;
1434 1434 capb->dl_primitive = DL_CAPABILITY_REQ;
1435 1435 capb->dl_sub_offset = sizeof (dl_capability_req_t);
1436 1436 capb->dl_sub_length = size;
1437 1437
1438 1438 mp->b_wptr += sizeof (dl_capability_req_t);
1439 1439
1440 1440 /*
1441 1441 * Each handler fills in the corresponding dl_capability_sub_t
1442 1442 * inside the mblk,
1443 1443 */
1444 1444 ill_capability_hcksum_reset_fill(ill, mp);
1445 1445 ill_capability_zerocopy_reset_fill(ill, mp);
1446 1446 ill_capability_dld_reset_fill(ill, mp);
1447 1447
1448 1448 ill->ill_capab_reset_mp = mp;
1449 1449 }
1450 1450
1451 1451 static void
1452 1452 ill_capability_id_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *outers)
1453 1453 {
1454 1454 dl_capab_id_t *id_ic;
1455 1455 uint_t sub_dl_cap = outers->dl_cap;
1456 1456 dl_capability_sub_t *inners;
1457 1457 uint8_t *capend;
1458 1458
1459 1459 ASSERT(sub_dl_cap == DL_CAPAB_ID_WRAPPER);
1460 1460
1461 1461 /*
1462 1462 * Note: range checks here are not absolutely sufficient to
1463 1463 * make us robust against malformed messages sent by drivers;
1464 1464 * this is in keeping with the rest of IP's dlpi handling.
1465 1465 * (Remember, it's coming from something else in the kernel
1466 1466 * address space)
1467 1467 */
1468 1468
1469 1469 capend = (uint8_t *)(outers + 1) + outers->dl_length;
1470 1470 if (capend > mp->b_wptr) {
1471 1471 cmn_err(CE_WARN, "ill_capability_id_ack: "
1472 1472 "malformed sub-capability too long for mblk");
1473 1473 return;
1474 1474 }
1475 1475
1476 1476 id_ic = (dl_capab_id_t *)(outers + 1);
1477 1477
1478 1478 if (outers->dl_length < sizeof (*id_ic) ||
1479 1479 (inners = &id_ic->id_subcap,
1480 1480 inners->dl_length > (outers->dl_length - sizeof (*inners)))) {
1481 1481 cmn_err(CE_WARN, "ill_capability_id_ack: malformed "
1482 1482 "encapsulated capab type %d too long for mblk",
1483 1483 inners->dl_cap);
1484 1484 return;
1485 1485 }
1486 1486
1487 1487 if (!dlcapabcheckqid(&id_ic->id_mid, ill->ill_lmod_rq)) {
1488 1488 ip1dbg(("ill_capability_id_ack: mid token for capab type %d "
1489 1489 "isn't as expected; pass-thru module(s) detected, "
1490 1490 "discarding capability\n", inners->dl_cap));
1491 1491 return;
1492 1492 }
1493 1493
1494 1494 /* Process the encapsulated sub-capability */
1495 1495 ill_capability_dispatch(ill, mp, inners);
1496 1496 }
1497 1497
1498 1498 static void
1499 1499 ill_capability_dld_reset_fill(ill_t *ill, mblk_t *mp)
1500 1500 {
1501 1501 dl_capability_sub_t *dl_subcap;
1502 1502
1503 1503 if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
1504 1504 return;
1505 1505
1506 1506 /*
1507 1507 * The dl_capab_dld_t that follows the dl_capability_sub_t is not
1508 1508 * initialized below since it is not used by DLD.
1509 1509 */
1510 1510 dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1511 1511 dl_subcap->dl_cap = DL_CAPAB_DLD;
1512 1512 dl_subcap->dl_length = sizeof (dl_capab_dld_t);
1513 1513
1514 1514 mp->b_wptr += sizeof (dl_capability_sub_t) + sizeof (dl_capab_dld_t);
1515 1515 }
1516 1516
1517 1517 static void
1518 1518 ill_capability_dispatch(ill_t *ill, mblk_t *mp, dl_capability_sub_t *subp)
1519 1519 {
1520 1520 /*
1521 1521 * If no ipif was brought up over this ill, this DL_CAPABILITY_REQ/ACK
1522 1522 * is only to get the VRRP capability.
1523 1523 *
1524 1524 * Note that we cannot check ill_ipif_up_count here since
1525 1525 * ill_ipif_up_count is only incremented when the resolver is setup.
1526 1526 * That is done asynchronously, and can race with this function.
1527 1527 */
1528 1528 if (!ill->ill_dl_up) {
1529 1529 if (subp->dl_cap == DL_CAPAB_VRRP)
1530 1530 ill_capability_vrrp_ack(ill, mp, subp);
1531 1531 return;
1532 1532 }
1533 1533
1534 1534 switch (subp->dl_cap) {
1535 1535 case DL_CAPAB_HCKSUM:
1536 1536 ill_capability_hcksum_ack(ill, mp, subp);
1537 1537 break;
1538 1538 case DL_CAPAB_ZEROCOPY:
1539 1539 ill_capability_zerocopy_ack(ill, mp, subp);
1540 1540 break;
1541 1541 case DL_CAPAB_DLD:
1542 1542 ill_capability_dld_ack(ill, mp, subp);
1543 1543 break;
1544 1544 case DL_CAPAB_VRRP:
1545 1545 break;
1546 1546 default:
1547 1547 ip1dbg(("ill_capability_dispatch: unknown capab type %d\n",
1548 1548 subp->dl_cap));
1549 1549 }
1550 1550 }
1551 1551
1552 1552 /*
1553 1553 * Process the vrrp capability received from a DLS Provider. isub must point
1554 1554 * to the sub-capability (DL_CAPAB_VRRP) of a DL_CAPABILITY_ACK message.
1555 1555 */
1556 1556 static void
1557 1557 ill_capability_vrrp_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1558 1558 {
1559 1559 dl_capab_vrrp_t *vrrp;
1560 1560 uint_t sub_dl_cap = isub->dl_cap;
1561 1561 uint8_t *capend;
1562 1562
1563 1563 ASSERT(IAM_WRITER_ILL(ill));
1564 1564 ASSERT(sub_dl_cap == DL_CAPAB_VRRP);
1565 1565
1566 1566 /*
1567 1567 * Note: range checks here are not absolutely sufficient to
1568 1568 * make us robust against malformed messages sent by drivers;
1569 1569 * this is in keeping with the rest of IP's dlpi handling.
1570 1570 * (Remember, it's coming from something else in the kernel
1571 1571 * address space)
1572 1572 */
1573 1573 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1574 1574 if (capend > mp->b_wptr) {
1575 1575 cmn_err(CE_WARN, "ill_capability_vrrp_ack: "
1576 1576 "malformed sub-capability too long for mblk");
1577 1577 return;
1578 1578 }
1579 1579 vrrp = (dl_capab_vrrp_t *)(isub + 1);
1580 1580
1581 1581 /*
1582 1582 * Compare the IP address family and set ILLF_VRRP for the right ill.
1583 1583 */
1584 1584 if ((vrrp->vrrp_af == AF_INET6 && ill->ill_isv6) ||
1585 1585 (vrrp->vrrp_af == AF_INET && !ill->ill_isv6)) {
1586 1586 ill->ill_flags |= ILLF_VRRP;
1587 1587 }
1588 1588 }
1589 1589
1590 1590 /*
1591 1591 * Process a hardware checksum offload capability negotiation ack received
1592 1592 * from a DLS Provider.isub must point to the sub-capability (DL_CAPAB_HCKSUM)
1593 1593 * of a DL_CAPABILITY_ACK message.
1594 1594 */
1595 1595 static void
1596 1596 ill_capability_hcksum_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1597 1597 {
1598 1598 dl_capability_req_t *ocap;
1599 1599 dl_capab_hcksum_t *ihck, *ohck;
1600 1600 ill_hcksum_capab_t **ill_hcksum;
1601 1601 mblk_t *nmp = NULL;
1602 1602 uint_t sub_dl_cap = isub->dl_cap;
1603 1603 uint8_t *capend;
1604 1604
1605 1605 ASSERT(sub_dl_cap == DL_CAPAB_HCKSUM);
1606 1606
1607 1607 ill_hcksum = (ill_hcksum_capab_t **)&ill->ill_hcksum_capab;
1608 1608
1609 1609 /*
1610 1610 * Note: range checks here are not absolutely sufficient to
1611 1611 * make us robust against malformed messages sent by drivers;
1612 1612 * this is in keeping with the rest of IP's dlpi handling.
1613 1613 * (Remember, it's coming from something else in the kernel
1614 1614 * address space)
1615 1615 */
1616 1616 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1617 1617 if (capend > mp->b_wptr) {
1618 1618 cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1619 1619 "malformed sub-capability too long for mblk");
1620 1620 return;
1621 1621 }
1622 1622
1623 1623 /*
1624 1624 * There are two types of acks we process here:
1625 1625 * 1. acks in reply to a (first form) generic capability req
1626 1626 * (no ENABLE flag set)
1627 1627 * 2. acks in reply to a ENABLE capability req.
1628 1628 * (ENABLE flag set)
1629 1629 */
1630 1630 ihck = (dl_capab_hcksum_t *)(isub + 1);
1631 1631
1632 1632 if (ihck->hcksum_version != HCKSUM_VERSION_1) {
1633 1633 cmn_err(CE_CONT, "ill_capability_hcksum_ack: "
1634 1634 "unsupported hardware checksum "
1635 1635 "sub-capability (version %d, expected %d)",
1636 1636 ihck->hcksum_version, HCKSUM_VERSION_1);
1637 1637 return;
1638 1638 }
1639 1639
1640 1640 if (!dlcapabcheckqid(&ihck->hcksum_mid, ill->ill_lmod_rq)) {
1641 1641 ip1dbg(("ill_capability_hcksum_ack: mid token for hardware "
1642 1642 "checksum capability isn't as expected; pass-thru "
1643 1643 "module(s) detected, discarding capability\n"));
1644 1644 return;
1645 1645 }
1646 1646
1647 1647 #define CURR_HCKSUM_CAPAB \
1648 1648 (HCKSUM_INET_PARTIAL | HCKSUM_INET_FULL_V4 | \
1649 1649 HCKSUM_INET_FULL_V6 | HCKSUM_IPHDRCKSUM)
1650 1650
1651 1651 if ((ihck->hcksum_txflags & HCKSUM_ENABLE) &&
1652 1652 (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB)) {
1653 1653 /* do ENABLE processing */
1654 1654 if (*ill_hcksum == NULL) {
1655 1655 *ill_hcksum = kmem_zalloc(sizeof (ill_hcksum_capab_t),
1656 1656 KM_NOSLEEP);
1657 1657
1658 1658 if (*ill_hcksum == NULL) {
1659 1659 cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1660 1660 "could not enable hcksum version %d "
1661 1661 "for %s (ENOMEM)\n", HCKSUM_CURRENT_VERSION,
1662 1662 ill->ill_name);
1663 1663 return;
1664 1664 }
1665 1665 }
1666 1666
1667 1667 (*ill_hcksum)->ill_hcksum_version = ihck->hcksum_version;
1668 1668 (*ill_hcksum)->ill_hcksum_txflags = ihck->hcksum_txflags;
1669 1669 ill->ill_capabilities |= ILL_CAPAB_HCKSUM;
1670 1670 ip1dbg(("ill_capability_hcksum_ack: interface %s "
1671 1671 "has enabled hardware checksumming\n ",
1672 1672 ill->ill_name));
1673 1673 } else if (ihck->hcksum_txflags & CURR_HCKSUM_CAPAB) {
1674 1674 /*
1675 1675 * Enabling hardware checksum offload
1676 1676 * Currently IP supports {TCP,UDP}/IPv4
1677 1677 * partial and full cksum offload and
1678 1678 * IPv4 header checksum offload.
1679 1679 * Allocate new mblk which will
1680 1680 * contain a new capability request
1681 1681 * to enable hardware checksum offload.
1682 1682 */
1683 1683 uint_t size;
1684 1684 uchar_t *rptr;
1685 1685
1686 1686 size = sizeof (dl_capability_req_t) +
1687 1687 sizeof (dl_capability_sub_t) + isub->dl_length;
1688 1688
1689 1689 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1690 1690 cmn_err(CE_WARN, "ill_capability_hcksum_ack: "
1691 1691 "could not enable hardware cksum for %s (ENOMEM)\n",
1692 1692 ill->ill_name);
1693 1693 return;
1694 1694 }
1695 1695
1696 1696 rptr = nmp->b_rptr;
1697 1697 /* initialize dl_capability_req_t */
1698 1698 ocap = (dl_capability_req_t *)nmp->b_rptr;
1699 1699 ocap->dl_sub_offset =
1700 1700 sizeof (dl_capability_req_t);
1701 1701 ocap->dl_sub_length =
1702 1702 sizeof (dl_capability_sub_t) +
1703 1703 isub->dl_length;
1704 1704 nmp->b_rptr += sizeof (dl_capability_req_t);
1705 1705
1706 1706 /* initialize dl_capability_sub_t */
1707 1707 bcopy(isub, nmp->b_rptr, sizeof (*isub));
1708 1708 nmp->b_rptr += sizeof (*isub);
1709 1709
1710 1710 /* initialize dl_capab_hcksum_t */
1711 1711 ohck = (dl_capab_hcksum_t *)nmp->b_rptr;
1712 1712 bcopy(ihck, ohck, sizeof (*ihck));
1713 1713
1714 1714 nmp->b_rptr = rptr;
1715 1715 ASSERT(nmp->b_wptr == (nmp->b_rptr + size));
1716 1716
1717 1717 /* Set ENABLE flag */
1718 1718 ohck->hcksum_txflags &= CURR_HCKSUM_CAPAB;
1719 1719 ohck->hcksum_txflags |= HCKSUM_ENABLE;
1720 1720
1721 1721 /*
1722 1722 * nmp points to a DL_CAPABILITY_REQ message to enable
1723 1723 * hardware checksum acceleration.
1724 1724 */
1725 1725 ill_capability_send(ill, nmp);
1726 1726 } else {
1727 1727 ip1dbg(("ill_capability_hcksum_ack: interface %s has "
1728 1728 "advertised %x hardware checksum capability flags\n",
1729 1729 ill->ill_name, ihck->hcksum_txflags));
1730 1730 }
1731 1731 }
1732 1732
1733 1733 static void
1734 1734 ill_capability_hcksum_reset_fill(ill_t *ill, mblk_t *mp)
1735 1735 {
1736 1736 dl_capab_hcksum_t *hck_subcap;
1737 1737 dl_capability_sub_t *dl_subcap;
1738 1738
1739 1739 if (!ILL_HCKSUM_CAPABLE(ill))
1740 1740 return;
1741 1741
1742 1742 ASSERT(ill->ill_hcksum_capab != NULL);
1743 1743
1744 1744 dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1745 1745 dl_subcap->dl_cap = DL_CAPAB_HCKSUM;
1746 1746 dl_subcap->dl_length = sizeof (*hck_subcap);
1747 1747
1748 1748 hck_subcap = (dl_capab_hcksum_t *)(dl_subcap + 1);
1749 1749 hck_subcap->hcksum_version = ill->ill_hcksum_capab->ill_hcksum_version;
1750 1750 hck_subcap->hcksum_txflags = 0;
1751 1751
1752 1752 mp->b_wptr += sizeof (*dl_subcap) + sizeof (*hck_subcap);
1753 1753 }
1754 1754
1755 1755 static void
1756 1756 ill_capability_zerocopy_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1757 1757 {
1758 1758 mblk_t *nmp = NULL;
1759 1759 dl_capability_req_t *oc;
1760 1760 dl_capab_zerocopy_t *zc_ic, *zc_oc;
1761 1761 ill_zerocopy_capab_t **ill_zerocopy_capab;
1762 1762 uint_t sub_dl_cap = isub->dl_cap;
1763 1763 uint8_t *capend;
1764 1764
1765 1765 ASSERT(sub_dl_cap == DL_CAPAB_ZEROCOPY);
1766 1766
1767 1767 ill_zerocopy_capab = (ill_zerocopy_capab_t **)&ill->ill_zerocopy_capab;
1768 1768
1769 1769 /*
1770 1770 * Note: range checks here are not absolutely sufficient to
1771 1771 * make us robust against malformed messages sent by drivers;
1772 1772 * this is in keeping with the rest of IP's dlpi handling.
1773 1773 * (Remember, it's coming from something else in the kernel
1774 1774 * address space)
1775 1775 */
1776 1776 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1777 1777 if (capend > mp->b_wptr) {
1778 1778 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1779 1779 "malformed sub-capability too long for mblk");
1780 1780 return;
1781 1781 }
1782 1782
1783 1783 zc_ic = (dl_capab_zerocopy_t *)(isub + 1);
1784 1784 if (zc_ic->zerocopy_version != ZEROCOPY_VERSION_1) {
1785 1785 cmn_err(CE_CONT, "ill_capability_zerocopy_ack: "
1786 1786 "unsupported ZEROCOPY sub-capability (version %d, "
1787 1787 "expected %d)", zc_ic->zerocopy_version,
1788 1788 ZEROCOPY_VERSION_1);
1789 1789 return;
1790 1790 }
1791 1791
1792 1792 if (!dlcapabcheckqid(&zc_ic->zerocopy_mid, ill->ill_lmod_rq)) {
1793 1793 ip1dbg(("ill_capability_zerocopy_ack: mid token for zerocopy "
1794 1794 "capability isn't as expected; pass-thru module(s) "
1795 1795 "detected, discarding capability\n"));
1796 1796 return;
1797 1797 }
1798 1798
1799 1799 if ((zc_ic->zerocopy_flags & DL_CAPAB_VMSAFE_MEM) != 0) {
1800 1800 if (*ill_zerocopy_capab == NULL) {
1801 1801 *ill_zerocopy_capab =
1802 1802 kmem_zalloc(sizeof (ill_zerocopy_capab_t),
1803 1803 KM_NOSLEEP);
1804 1804
1805 1805 if (*ill_zerocopy_capab == NULL) {
1806 1806 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1807 1807 "could not enable Zero-copy version %d "
1808 1808 "for %s (ENOMEM)\n", ZEROCOPY_VERSION_1,
1809 1809 ill->ill_name);
1810 1810 return;
1811 1811 }
1812 1812 }
1813 1813
1814 1814 ip1dbg(("ill_capability_zerocopy_ack: interface %s "
1815 1815 "supports Zero-copy version %d\n", ill->ill_name,
1816 1816 ZEROCOPY_VERSION_1));
1817 1817
1818 1818 (*ill_zerocopy_capab)->ill_zerocopy_version =
1819 1819 zc_ic->zerocopy_version;
1820 1820 (*ill_zerocopy_capab)->ill_zerocopy_flags =
1821 1821 zc_ic->zerocopy_flags;
1822 1822
1823 1823 ill->ill_capabilities |= ILL_CAPAB_ZEROCOPY;
1824 1824 } else {
1825 1825 uint_t size;
1826 1826 uchar_t *rptr;
1827 1827
1828 1828 size = sizeof (dl_capability_req_t) +
1829 1829 sizeof (dl_capability_sub_t) +
1830 1830 sizeof (dl_capab_zerocopy_t);
1831 1831
1832 1832 if ((nmp = ip_dlpi_alloc(size, DL_CAPABILITY_REQ)) == NULL) {
1833 1833 cmn_err(CE_WARN, "ill_capability_zerocopy_ack: "
1834 1834 "could not enable zerocopy for %s (ENOMEM)\n",
1835 1835 ill->ill_name);
1836 1836 return;
1837 1837 }
1838 1838
1839 1839 rptr = nmp->b_rptr;
1840 1840 /* initialize dl_capability_req_t */
1841 1841 oc = (dl_capability_req_t *)rptr;
1842 1842 oc->dl_sub_offset = sizeof (dl_capability_req_t);
1843 1843 oc->dl_sub_length = sizeof (dl_capability_sub_t) +
1844 1844 sizeof (dl_capab_zerocopy_t);
1845 1845 rptr += sizeof (dl_capability_req_t);
1846 1846
1847 1847 /* initialize dl_capability_sub_t */
1848 1848 bcopy(isub, rptr, sizeof (*isub));
1849 1849 rptr += sizeof (*isub);
1850 1850
1851 1851 /* initialize dl_capab_zerocopy_t */
1852 1852 zc_oc = (dl_capab_zerocopy_t *)rptr;
1853 1853 *zc_oc = *zc_ic;
1854 1854
1855 1855 ip1dbg(("ill_capability_zerocopy_ack: asking interface %s "
1856 1856 "to enable zero-copy version %d\n", ill->ill_name,
1857 1857 ZEROCOPY_VERSION_1));
1858 1858
1859 1859 /* set VMSAFE_MEM flag */
1860 1860 zc_oc->zerocopy_flags |= DL_CAPAB_VMSAFE_MEM;
1861 1861
1862 1862 /* nmp points to a DL_CAPABILITY_REQ message to enable zcopy */
1863 1863 ill_capability_send(ill, nmp);
1864 1864 }
1865 1865 }
1866 1866
1867 1867 static void
1868 1868 ill_capability_zerocopy_reset_fill(ill_t *ill, mblk_t *mp)
1869 1869 {
1870 1870 dl_capab_zerocopy_t *zerocopy_subcap;
1871 1871 dl_capability_sub_t *dl_subcap;
1872 1872
1873 1873 if (!(ill->ill_capabilities & ILL_CAPAB_ZEROCOPY))
1874 1874 return;
1875 1875
1876 1876 ASSERT(ill->ill_zerocopy_capab != NULL);
1877 1877
1878 1878 dl_subcap = (dl_capability_sub_t *)mp->b_wptr;
1879 1879 dl_subcap->dl_cap = DL_CAPAB_ZEROCOPY;
1880 1880 dl_subcap->dl_length = sizeof (*zerocopy_subcap);
1881 1881
1882 1882 zerocopy_subcap = (dl_capab_zerocopy_t *)(dl_subcap + 1);
1883 1883 zerocopy_subcap->zerocopy_version =
1884 1884 ill->ill_zerocopy_capab->ill_zerocopy_version;
1885 1885 zerocopy_subcap->zerocopy_flags = 0;
1886 1886
1887 1887 mp->b_wptr += sizeof (*dl_subcap) + sizeof (*zerocopy_subcap);
1888 1888 }
1889 1889
1890 1890 /*
1891 1891 * DLD capability
1892 1892 * Refer to dld.h for more information regarding the purpose and usage
1893 1893 * of this capability.
1894 1894 */
1895 1895 static void
1896 1896 ill_capability_dld_ack(ill_t *ill, mblk_t *mp, dl_capability_sub_t *isub)
1897 1897 {
1898 1898 dl_capab_dld_t *dld_ic, dld;
1899 1899 uint_t sub_dl_cap = isub->dl_cap;
1900 1900 uint8_t *capend;
1901 1901 ill_dld_capab_t *idc;
1902 1902
1903 1903 ASSERT(IAM_WRITER_ILL(ill));
1904 1904 ASSERT(sub_dl_cap == DL_CAPAB_DLD);
1905 1905
1906 1906 /*
1907 1907 * Note: range checks here are not absolutely sufficient to
1908 1908 * make us robust against malformed messages sent by drivers;
1909 1909 * this is in keeping with the rest of IP's dlpi handling.
1910 1910 * (Remember, it's coming from something else in the kernel
1911 1911 * address space)
1912 1912 */
1913 1913 capend = (uint8_t *)(isub + 1) + isub->dl_length;
1914 1914 if (capend > mp->b_wptr) {
1915 1915 cmn_err(CE_WARN, "ill_capability_dld_ack: "
1916 1916 "malformed sub-capability too long for mblk");
1917 1917 return;
1918 1918 }
1919 1919 dld_ic = (dl_capab_dld_t *)(isub + 1);
1920 1920 if (dld_ic->dld_version != DLD_CURRENT_VERSION) {
1921 1921 cmn_err(CE_CONT, "ill_capability_dld_ack: "
1922 1922 "unsupported DLD sub-capability (version %d, "
1923 1923 "expected %d)", dld_ic->dld_version,
1924 1924 DLD_CURRENT_VERSION);
1925 1925 return;
1926 1926 }
1927 1927 if (!dlcapabcheckqid(&dld_ic->dld_mid, ill->ill_lmod_rq)) {
1928 1928 ip1dbg(("ill_capability_dld_ack: mid token for dld "
1929 1929 "capability isn't as expected; pass-thru module(s) "
1930 1930 "detected, discarding capability\n"));
1931 1931 return;
1932 1932 }
1933 1933
1934 1934 /*
1935 1935 * Copy locally to ensure alignment.
1936 1936 */
1937 1937 bcopy(dld_ic, &dld, sizeof (dl_capab_dld_t));
1938 1938
1939 1939 if ((idc = ill->ill_dld_capab) == NULL) {
1940 1940 idc = kmem_zalloc(sizeof (ill_dld_capab_t), KM_NOSLEEP);
1941 1941 if (idc == NULL) {
1942 1942 cmn_err(CE_WARN, "ill_capability_dld_ack: "
1943 1943 "could not enable DLD version %d "
1944 1944 "for %s (ENOMEM)\n", DLD_CURRENT_VERSION,
1945 1945 ill->ill_name);
1946 1946 return;
1947 1947 }
1948 1948 ill->ill_dld_capab = idc;
1949 1949 }
1950 1950 idc->idc_capab_df = (ip_capab_func_t)dld.dld_capab;
1951 1951 idc->idc_capab_dh = (void *)dld.dld_capab_handle;
1952 1952 ip1dbg(("ill_capability_dld_ack: interface %s "
1953 1953 "supports DLD version %d\n", ill->ill_name, DLD_CURRENT_VERSION));
1954 1954
1955 1955 ill_capability_dld_enable(ill);
1956 1956 }
1957 1957
1958 1958 /*
1959 1959 * Typically capability negotiation between IP and the driver happens via
1960 1960 * DLPI message exchange. However GLD also offers a direct function call
1961 1961 * mechanism to exchange the DLD_DIRECT_CAPAB and DLD_POLL_CAPAB capabilities,
1962 1962 * But arbitrary function calls into IP or GLD are not permitted, since both
1963 1963 * of them are protected by their own perimeter mechanism. The perimeter can
1964 1964 * be viewed as a coarse lock or serialization mechanism. The hierarchy of
1965 1965 * these perimeters is IP -> MAC. Thus for example to enable the squeue
1966 1966 * polling, IP needs to enter its perimeter, then call ill_mac_perim_enter
1967 1967 * to enter the mac perimeter and then do the direct function calls into
1968 1968 * GLD to enable squeue polling. The ring related callbacks from the mac into
1969 1969 * the stack to add, bind, quiesce, restart or cleanup a ring are all
1970 1970 * protected by the mac perimeter.
1971 1971 */
1972 1972 static void
1973 1973 ill_mac_perim_enter(ill_t *ill, mac_perim_handle_t *mphp)
1974 1974 {
1975 1975 ill_dld_capab_t *idc = ill->ill_dld_capab;
1976 1976 int err;
1977 1977
1978 1978 err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mphp,
1979 1979 DLD_ENABLE);
1980 1980 ASSERT(err == 0);
1981 1981 }
1982 1982
1983 1983 static void
1984 1984 ill_mac_perim_exit(ill_t *ill, mac_perim_handle_t mph)
1985 1985 {
1986 1986 ill_dld_capab_t *idc = ill->ill_dld_capab;
1987 1987 int err;
1988 1988
1989 1989 err = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, mph,
1990 1990 DLD_DISABLE);
1991 1991 ASSERT(err == 0);
1992 1992 }
1993 1993
1994 1994 boolean_t
1995 1995 ill_mac_perim_held(ill_t *ill)
1996 1996 {
1997 1997 ill_dld_capab_t *idc = ill->ill_dld_capab;
1998 1998
1999 1999 return (idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_PERIM, NULL,
2000 2000 DLD_QUERY));
2001 2001 }
2002 2002
2003 2003 static void
2004 2004 ill_capability_direct_enable(ill_t *ill)
2005 2005 {
2006 2006 ill_dld_capab_t *idc = ill->ill_dld_capab;
2007 2007 ill_dld_direct_t *idd = &idc->idc_direct;
2008 2008 dld_capab_direct_t direct;
2009 2009 int rc;
2010 2010
2011 2011 ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2012 2012
2013 2013 bzero(&direct, sizeof (direct));
2014 2014 direct.di_rx_cf = (uintptr_t)ip_input;
2015 2015 direct.di_rx_ch = ill;
2016 2016
2017 2017 rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT, &direct,
2018 2018 DLD_ENABLE);
2019 2019 if (rc == 0) {
2020 2020 idd->idd_tx_df = (ip_dld_tx_t)direct.di_tx_df;
2021 2021 idd->idd_tx_dh = direct.di_tx_dh;
2022 2022 idd->idd_tx_cb_df = (ip_dld_callb_t)direct.di_tx_cb_df;
2023 2023 idd->idd_tx_cb_dh = direct.di_tx_cb_dh;
2024 2024 idd->idd_tx_fctl_df = (ip_dld_fctl_t)direct.di_tx_fctl_df;
2025 2025 idd->idd_tx_fctl_dh = direct.di_tx_fctl_dh;
2026 2026 ASSERT(idd->idd_tx_cb_df != NULL);
2027 2027 ASSERT(idd->idd_tx_fctl_df != NULL);
2028 2028 ASSERT(idd->idd_tx_df != NULL);
2029 2029 /*
2030 2030 * One time registration of flow enable callback function
2031 2031 */
2032 2032 ill->ill_flownotify_mh = idd->idd_tx_cb_df(idd->idd_tx_cb_dh,
2033 2033 ill_flow_enable, ill);
2034 2034 ill->ill_capabilities |= ILL_CAPAB_DLD_DIRECT;
2035 2035 DTRACE_PROBE1(direct_on, (ill_t *), ill);
2036 2036 } else {
2037 2037 cmn_err(CE_WARN, "warning: could not enable DIRECT "
2038 2038 "capability, rc = %d\n", rc);
2039 2039 DTRACE_PROBE2(direct_off, (ill_t *), ill, (int), rc);
2040 2040 }
2041 2041 }
2042 2042
2043 2043 static void
2044 2044 ill_capability_poll_enable(ill_t *ill)
2045 2045 {
2046 2046 ill_dld_capab_t *idc = ill->ill_dld_capab;
2047 2047 dld_capab_poll_t poll;
2048 2048 int rc;
2049 2049
2050 2050 ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2051 2051
2052 2052 bzero(&poll, sizeof (poll));
2053 2053 poll.poll_ring_add_cf = (uintptr_t)ip_squeue_add_ring;
2054 2054 poll.poll_ring_remove_cf = (uintptr_t)ip_squeue_clean_ring;
2055 2055 poll.poll_ring_quiesce_cf = (uintptr_t)ip_squeue_quiesce_ring;
2056 2056 poll.poll_ring_restart_cf = (uintptr_t)ip_squeue_restart_ring;
2057 2057 poll.poll_ring_bind_cf = (uintptr_t)ip_squeue_bind_ring;
2058 2058 poll.poll_ring_ch = ill;
2059 2059 rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL, &poll,
2060 2060 DLD_ENABLE);
2061 2061 if (rc == 0) {
2062 2062 ill->ill_capabilities |= ILL_CAPAB_DLD_POLL;
2063 2063 DTRACE_PROBE1(poll_on, (ill_t *), ill);
2064 2064 } else {
2065 2065 ip1dbg(("warning: could not enable POLL "
2066 2066 "capability, rc = %d\n", rc));
2067 2067 DTRACE_PROBE2(poll_off, (ill_t *), ill, (int), rc);
2068 2068 }
2069 2069 }
2070 2070
2071 2071 /*
2072 2072 * Enable the LSO capability.
2073 2073 */
2074 2074 static void
2075 2075 ill_capability_lso_enable(ill_t *ill)
2076 2076 {
2077 2077 ill_dld_capab_t *idc = ill->ill_dld_capab;
2078 2078 dld_capab_lso_t lso;
2079 2079 int rc;
2080 2080
2081 2081 ASSERT(!ill->ill_isv6 && IAM_WRITER_ILL(ill));
2082 2082
2083 2083 if (ill->ill_lso_capab == NULL) {
2084 2084 ill->ill_lso_capab = kmem_zalloc(sizeof (ill_lso_capab_t),
2085 2085 KM_NOSLEEP);
2086 2086 if (ill->ill_lso_capab == NULL) {
2087 2087 cmn_err(CE_WARN, "ill_capability_lso_enable: "
2088 2088 "could not enable LSO for %s (ENOMEM)\n",
2089 2089 ill->ill_name);
2090 2090 return;
2091 2091 }
2092 2092 }
2093 2093
2094 2094 bzero(&lso, sizeof (lso));
2095 2095 if ((rc = idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO, &lso,
2096 2096 DLD_ENABLE)) == 0) {
2097 2097 ill->ill_lso_capab->ill_lso_flags = lso.lso_flags;
2098 2098 ill->ill_lso_capab->ill_lso_max = lso.lso_max;
2099 2099 ill->ill_capabilities |= ILL_CAPAB_LSO;
2100 2100 ip1dbg(("ill_capability_lso_enable: interface %s "
2101 2101 "has enabled LSO\n ", ill->ill_name));
2102 2102 } else {
2103 2103 kmem_free(ill->ill_lso_capab, sizeof (ill_lso_capab_t));
2104 2104 ill->ill_lso_capab = NULL;
2105 2105 DTRACE_PROBE2(lso_off, (ill_t *), ill, (int), rc);
2106 2106 }
2107 2107 }
2108 2108
2109 2109 static void
2110 2110 ill_capability_dld_enable(ill_t *ill)
2111 2111 {
2112 2112 mac_perim_handle_t mph;
2113 2113
2114 2114 ASSERT(IAM_WRITER_ILL(ill));
2115 2115
2116 2116 if (ill->ill_isv6)
2117 2117 return;
2118 2118
2119 2119 ill_mac_perim_enter(ill, &mph);
2120 2120 if (!ill->ill_isv6) {
2121 2121 ill_capability_direct_enable(ill);
2122 2122 ill_capability_poll_enable(ill);
2123 2123 ill_capability_lso_enable(ill);
2124 2124 }
2125 2125 ill->ill_capabilities |= ILL_CAPAB_DLD;
2126 2126 ill_mac_perim_exit(ill, mph);
2127 2127 }
2128 2128
2129 2129 static void
2130 2130 ill_capability_dld_disable(ill_t *ill)
2131 2131 {
2132 2132 ill_dld_capab_t *idc;
2133 2133 ill_dld_direct_t *idd;
2134 2134 mac_perim_handle_t mph;
2135 2135
2136 2136 ASSERT(IAM_WRITER_ILL(ill));
2137 2137
2138 2138 if (!(ill->ill_capabilities & ILL_CAPAB_DLD))
2139 2139 return;
2140 2140
2141 2141 ill_mac_perim_enter(ill, &mph);
2142 2142
2143 2143 idc = ill->ill_dld_capab;
2144 2144 if ((ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT) != 0) {
2145 2145 /*
2146 2146 * For performance we avoid locks in the transmit data path
2147 2147 * and don't maintain a count of the number of threads using
2148 2148 * direct calls. Thus some threads could be using direct
2149 2149 * transmit calls to GLD, even after the capability mechanism
2150 2150 * turns it off. This is still safe since the handles used in
2151 2151 * the direct calls continue to be valid until the unplumb is
2152 2152 * completed. Remove the callback that was added (1-time) at
2153 2153 * capab enable time.
2154 2154 */
2155 2155 mutex_enter(&ill->ill_lock);
2156 2156 ill->ill_capabilities &= ~ILL_CAPAB_DLD_DIRECT;
2157 2157 mutex_exit(&ill->ill_lock);
2158 2158 if (ill->ill_flownotify_mh != NULL) {
2159 2159 idd = &idc->idc_direct;
2160 2160 idd->idd_tx_cb_df(idd->idd_tx_cb_dh, NULL,
2161 2161 ill->ill_flownotify_mh);
2162 2162 ill->ill_flownotify_mh = NULL;
2163 2163 }
2164 2164 (void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_DIRECT,
2165 2165 NULL, DLD_DISABLE);
2166 2166 }
2167 2167
2168 2168 if ((ill->ill_capabilities & ILL_CAPAB_DLD_POLL) != 0) {
2169 2169 ill->ill_capabilities &= ~ILL_CAPAB_DLD_POLL;
2170 2170 ip_squeue_clean_all(ill);
2171 2171 (void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_POLL,
2172 2172 NULL, DLD_DISABLE);
2173 2173 }
2174 2174
2175 2175 if ((ill->ill_capabilities & ILL_CAPAB_LSO) != 0) {
2176 2176 ASSERT(ill->ill_lso_capab != NULL);
2177 2177 /*
2178 2178 * Clear the capability flag for LSO but retain the
2179 2179 * ill_lso_capab structure since it's possible that another
2180 2180 * thread is still referring to it. The structure only gets
2181 2181 * deallocated when we destroy the ill.
2182 2182 */
2183 2183
2184 2184 ill->ill_capabilities &= ~ILL_CAPAB_LSO;
2185 2185 (void) idc->idc_capab_df(idc->idc_capab_dh, DLD_CAPAB_LSO,
2186 2186 NULL, DLD_DISABLE);
2187 2187 }
2188 2188
2189 2189 ill->ill_capabilities &= ~ILL_CAPAB_DLD;
2190 2190 ill_mac_perim_exit(ill, mph);
2191 2191 }
2192 2192
2193 2193 /*
2194 2194 * Capability Negotiation protocol
2195 2195 *
2196 2196 * We don't wait for DLPI capability operations to finish during interface
2197 2197 * bringup or teardown. Doing so would introduce more asynchrony and the
2198 2198 * interface up/down operations will need multiple return and restarts.
2199 2199 * Instead the 'ipsq_current_ipif' of the ipsq is not cleared as long as
2200 2200 * the 'ill_dlpi_deferred' chain is non-empty. This ensures that the next
2201 2201 * exclusive operation won't start until the DLPI operations of the previous
2202 2202 * exclusive operation complete.
2203 2203 *
2204 2204 * The capability state machine is shown below.
2205 2205 *
2206 2206 * state next state event, action
2207 2207 *
2208 2208 * IDCS_UNKNOWN IDCS_PROBE_SENT ill_capability_probe
2209 2209 * IDCS_PROBE_SENT IDCS_OK ill_capability_ack
2210 2210 * IDCS_PROBE_SENT IDCS_FAILED ip_rput_dlpi_writer (nack)
2211 2211 * IDCS_OK IDCS_RENEG Receipt of DL_NOTE_CAPAB_RENEG
2212 2212 * IDCS_OK IDCS_RESET_SENT ill_capability_reset
2213 2213 * IDCS_RESET_SENT IDCS_UNKNOWN ill_capability_ack_thr
2214 2214 * IDCS_RENEG IDCS_PROBE_SENT ill_capability_ack_thr ->
2215 2215 * ill_capability_probe.
2216 2216 */
2217 2217
2218 2218 /*
2219 2219 * Dedicated thread started from ip_stack_init that handles capability
2220 2220 * disable. This thread ensures the taskq dispatch does not fail by waiting
2221 2221 * for resources using TQ_SLEEP. The taskq mechanism is used to ensure
2222 2222 * that direct calls to DLD are done in a cv_waitable context.
2223 2223 */
2224 2224 void
2225 2225 ill_taskq_dispatch(ip_stack_t *ipst)
2226 2226 {
2227 2227 callb_cpr_t cprinfo;
2228 2228 char name[64];
2229 2229 mblk_t *mp;
2230 2230
2231 2231 (void) snprintf(name, sizeof (name), "ill_taskq_dispatch_%d",
2232 2232 ipst->ips_netstack->netstack_stackid);
2233 2233 CALLB_CPR_INIT(&cprinfo, &ipst->ips_capab_taskq_lock, callb_generic_cpr,
2234 2234 name);
2235 2235 mutex_enter(&ipst->ips_capab_taskq_lock);
2236 2236
2237 2237 for (;;) {
2238 2238 mp = ipst->ips_capab_taskq_head;
2239 2239 while (mp != NULL) {
2240 2240 ipst->ips_capab_taskq_head = mp->b_next;
2241 2241 if (ipst->ips_capab_taskq_head == NULL)
2242 2242 ipst->ips_capab_taskq_tail = NULL;
2243 2243 mutex_exit(&ipst->ips_capab_taskq_lock);
2244 2244 mp->b_next = NULL;
2245 2245
2246 2246 VERIFY(taskq_dispatch(system_taskq,
2247 2247 ill_capability_ack_thr, mp, TQ_SLEEP) != 0);
2248 2248 mutex_enter(&ipst->ips_capab_taskq_lock);
2249 2249 mp = ipst->ips_capab_taskq_head;
2250 2250 }
2251 2251
2252 2252 if (ipst->ips_capab_taskq_quit)
2253 2253 break;
2254 2254 CALLB_CPR_SAFE_BEGIN(&cprinfo);
2255 2255 cv_wait(&ipst->ips_capab_taskq_cv, &ipst->ips_capab_taskq_lock);
2256 2256 CALLB_CPR_SAFE_END(&cprinfo, &ipst->ips_capab_taskq_lock);
2257 2257 }
2258 2258 VERIFY(ipst->ips_capab_taskq_head == NULL);
2259 2259 VERIFY(ipst->ips_capab_taskq_tail == NULL);
2260 2260 CALLB_CPR_EXIT(&cprinfo);
2261 2261 thread_exit();
2262 2262 }
2263 2263
2264 2264 /*
2265 2265 * Consume a new-style hardware capabilities negotiation ack.
2266 2266 * Called via taskq on receipt of DL_CAPABILITY_ACK.
2267 2267 */
2268 2268 static void
2269 2269 ill_capability_ack_thr(void *arg)
2270 2270 {
2271 2271 mblk_t *mp = arg;
2272 2272 dl_capability_ack_t *capp;
2273 2273 dl_capability_sub_t *subp, *endp;
2274 2274 ill_t *ill;
2275 2275 boolean_t reneg;
2276 2276
2277 2277 ill = (ill_t *)mp->b_prev;
2278 2278 mp->b_prev = NULL;
2279 2279
2280 2280 VERIFY(ipsq_enter(ill, B_FALSE, CUR_OP) == B_TRUE);
2281 2281
2282 2282 if (ill->ill_dlpi_capab_state == IDCS_RESET_SENT ||
2283 2283 ill->ill_dlpi_capab_state == IDCS_RENEG) {
2284 2284 /*
2285 2285 * We have received the ack for our DL_CAPAB reset request.
2286 2286 * There isnt' anything in the message that needs processing.
2287 2287 * All message based capabilities have been disabled, now
2288 2288 * do the function call based capability disable.
2289 2289 */
2290 2290 reneg = ill->ill_dlpi_capab_state == IDCS_RENEG;
2291 2291 ill_capability_dld_disable(ill);
2292 2292 ill->ill_dlpi_capab_state = IDCS_UNKNOWN;
2293 2293 if (reneg)
2294 2294 ill_capability_probe(ill);
2295 2295 goto done;
2296 2296 }
2297 2297
2298 2298 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
2299 2299 ill->ill_dlpi_capab_state = IDCS_OK;
2300 2300
2301 2301 capp = (dl_capability_ack_t *)mp->b_rptr;
2302 2302
2303 2303 if (capp->dl_sub_length == 0) {
2304 2304 /* no new-style capabilities */
2305 2305 goto done;
2306 2306 }
2307 2307
2308 2308 /* make sure the driver supplied correct dl_sub_length */
2309 2309 if ((sizeof (*capp) + capp->dl_sub_length) > MBLKL(mp)) {
2310 2310 ip0dbg(("ill_capability_ack: bad DL_CAPABILITY_ACK, "
2311 2311 "invalid dl_sub_length (%d)\n", capp->dl_sub_length));
2312 2312 goto done;
2313 2313 }
2314 2314
2315 2315 #define SC(base, offset) (dl_capability_sub_t *)(((uchar_t *)(base))+(offset))
2316 2316 /*
2317 2317 * There are sub-capabilities. Process the ones we know about.
2318 2318 * Loop until we don't have room for another sub-cap header..
2319 2319 */
2320 2320 for (subp = SC(capp, capp->dl_sub_offset),
2321 2321 endp = SC(subp, capp->dl_sub_length - sizeof (*subp));
2322 2322 subp <= endp;
2323 2323 subp = SC(subp, sizeof (dl_capability_sub_t) + subp->dl_length)) {
2324 2324
2325 2325 switch (subp->dl_cap) {
2326 2326 case DL_CAPAB_ID_WRAPPER:
2327 2327 ill_capability_id_ack(ill, mp, subp);
2328 2328 break;
2329 2329 default:
2330 2330 ill_capability_dispatch(ill, mp, subp);
2331 2331 break;
2332 2332 }
2333 2333 }
2334 2334 #undef SC
2335 2335 done:
2336 2336 inet_freemsg(mp);
2337 2337 ill_capability_done(ill);
2338 2338 ipsq_exit(ill->ill_phyint->phyint_ipsq);
2339 2339 }
2340 2340
2341 2341 /*
2342 2342 * This needs to be started in a taskq thread to provide a cv_waitable
2343 2343 * context.
2344 2344 */
2345 2345 void
2346 2346 ill_capability_ack(ill_t *ill, mblk_t *mp)
2347 2347 {
2348 2348 ip_stack_t *ipst = ill->ill_ipst;
2349 2349
2350 2350 mp->b_prev = (mblk_t *)ill;
2351 2351 ASSERT(mp->b_next == NULL);
2352 2352
2353 2353 if (taskq_dispatch(system_taskq, ill_capability_ack_thr, mp,
2354 2354 TQ_NOSLEEP) != 0)
2355 2355 return;
2356 2356
2357 2357 /*
2358 2358 * The taskq dispatch failed. Signal the ill_taskq_dispatch thread
2359 2359 * which will do the dispatch using TQ_SLEEP to guarantee success.
2360 2360 */
2361 2361 mutex_enter(&ipst->ips_capab_taskq_lock);
2362 2362 if (ipst->ips_capab_taskq_head == NULL) {
2363 2363 ASSERT(ipst->ips_capab_taskq_tail == NULL);
2364 2364 ipst->ips_capab_taskq_head = mp;
2365 2365 } else {
2366 2366 ipst->ips_capab_taskq_tail->b_next = mp;
2367 2367 }
2368 2368 ipst->ips_capab_taskq_tail = mp;
2369 2369
2370 2370 cv_signal(&ipst->ips_capab_taskq_cv);
2371 2371 mutex_exit(&ipst->ips_capab_taskq_lock);
2372 2372 }
2373 2373
2374 2374 /*
2375 2375 * This routine is called to scan the fragmentation reassembly table for
2376 2376 * the specified ILL for any packets that are starting to smell.
2377 2377 * dead_interval is the maximum time in seconds that will be tolerated. It
2378 2378 * will either be the value specified in ip_g_frag_timeout, or zero if the
2379 2379 * ILL is shutting down and it is time to blow everything off.
2380 2380 *
2381 2381 * It returns the number of seconds (as a time_t) that the next frag timer
2382 2382 * should be scheduled for, 0 meaning that the timer doesn't need to be
2383 2383 * re-started. Note that the method of calculating next_timeout isn't
2384 2384 * entirely accurate since time will flow between the time we grab
2385 2385 * current_time and the time we schedule the next timeout. This isn't a
2386 2386 * big problem since this is the timer for sending an ICMP reassembly time
2387 2387 * exceeded messages, and it doesn't have to be exactly accurate.
2388 2388 *
2389 2389 * This function is
2390 2390 * sometimes called as writer, although this is not required.
2391 2391 */
2392 2392 time_t
2393 2393 ill_frag_timeout(ill_t *ill, time_t dead_interval)
2394 2394 {
2395 2395 ipfb_t *ipfb;
2396 2396 ipfb_t *endp;
2397 2397 ipf_t *ipf;
2398 2398 ipf_t *ipfnext;
2399 2399 mblk_t *mp;
2400 2400 time_t current_time = gethrestime_sec();
2401 2401 time_t next_timeout = 0;
2402 2402 uint32_t hdr_length;
2403 2403 mblk_t *send_icmp_head;
2404 2404 mblk_t *send_icmp_head_v6;
2405 2405 ip_stack_t *ipst = ill->ill_ipst;
2406 2406 ip_recv_attr_t iras;
2407 2407
2408 2408 bzero(&iras, sizeof (iras));
2409 2409 iras.ira_flags = 0;
2410 2410 iras.ira_ill = iras.ira_rill = ill;
2411 2411 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
2412 2412 iras.ira_rifindex = iras.ira_ruifindex;
2413 2413
2414 2414 ipfb = ill->ill_frag_hash_tbl;
2415 2415 if (ipfb == NULL)
2416 2416 return (B_FALSE);
2417 2417 endp = &ipfb[ILL_FRAG_HASH_TBL_COUNT];
2418 2418 /* Walk the frag hash table. */
2419 2419 for (; ipfb < endp; ipfb++) {
2420 2420 send_icmp_head = NULL;
2421 2421 send_icmp_head_v6 = NULL;
2422 2422 mutex_enter(&ipfb->ipfb_lock);
2423 2423 while ((ipf = ipfb->ipfb_ipf) != 0) {
2424 2424 time_t frag_time = current_time - ipf->ipf_timestamp;
2425 2425 time_t frag_timeout;
2426 2426
2427 2427 if (frag_time < dead_interval) {
2428 2428 /*
2429 2429 * There are some outstanding fragments
2430 2430 * that will timeout later. Make note of
2431 2431 * the time so that we can reschedule the
2432 2432 * next timeout appropriately.
2433 2433 */
2434 2434 frag_timeout = dead_interval - frag_time;
2435 2435 if (next_timeout == 0 ||
2436 2436 frag_timeout < next_timeout) {
2437 2437 next_timeout = frag_timeout;
2438 2438 }
2439 2439 break;
2440 2440 }
2441 2441 /* Time's up. Get it out of here. */
2442 2442 hdr_length = ipf->ipf_nf_hdr_len;
2443 2443 ipfnext = ipf->ipf_hash_next;
2444 2444 if (ipfnext)
2445 2445 ipfnext->ipf_ptphn = ipf->ipf_ptphn;
2446 2446 *ipf->ipf_ptphn = ipfnext;
2447 2447 mp = ipf->ipf_mp->b_cont;
2448 2448 for (; mp; mp = mp->b_cont) {
2449 2449 /* Extra points for neatness. */
2450 2450 IP_REASS_SET_START(mp, 0);
2451 2451 IP_REASS_SET_END(mp, 0);
2452 2452 }
2453 2453 mp = ipf->ipf_mp->b_cont;
2454 2454 atomic_add_32(&ill->ill_frag_count, -ipf->ipf_count);
2455 2455 ASSERT(ipfb->ipfb_count >= ipf->ipf_count);
2456 2456 ipfb->ipfb_count -= ipf->ipf_count;
2457 2457 ASSERT(ipfb->ipfb_frag_pkts > 0);
2458 2458 ipfb->ipfb_frag_pkts--;
2459 2459 /*
2460 2460 * We do not send any icmp message from here because
2461 2461 * we currently are holding the ipfb_lock for this
2462 2462 * hash chain. If we try and send any icmp messages
2463 2463 * from here we may end up via a put back into ip
2464 2464 * trying to get the same lock, causing a recursive
2465 2465 * mutex panic. Instead we build a list and send all
2466 2466 * the icmp messages after we have dropped the lock.
2467 2467 */
2468 2468 if (ill->ill_isv6) {
2469 2469 if (hdr_length != 0) {
2470 2470 mp->b_next = send_icmp_head_v6;
2471 2471 send_icmp_head_v6 = mp;
2472 2472 } else {
2473 2473 freemsg(mp);
2474 2474 }
2475 2475 } else {
2476 2476 if (hdr_length != 0) {
2477 2477 mp->b_next = send_icmp_head;
2478 2478 send_icmp_head = mp;
2479 2479 } else {
2480 2480 freemsg(mp);
2481 2481 }
2482 2482 }
2483 2483 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2484 2484 ip_drop_input("ipIfStatsReasmFails", ipf->ipf_mp, ill);
2485 2485 freeb(ipf->ipf_mp);
2486 2486 }
2487 2487 mutex_exit(&ipfb->ipfb_lock);
2488 2488 /*
2489 2489 * Now need to send any icmp messages that we delayed from
2490 2490 * above.
2491 2491 */
2492 2492 while (send_icmp_head_v6 != NULL) {
2493 2493 ip6_t *ip6h;
2494 2494
2495 2495 mp = send_icmp_head_v6;
2496 2496 send_icmp_head_v6 = send_icmp_head_v6->b_next;
2497 2497 mp->b_next = NULL;
2498 2498 ip6h = (ip6_t *)mp->b_rptr;
2499 2499 iras.ira_flags = 0;
2500 2500 /*
2501 2501 * This will result in an incorrect ALL_ZONES zoneid
2502 2502 * for multicast packets, but we
2503 2503 * don't send ICMP errors for those in any case.
2504 2504 */
2505 2505 iras.ira_zoneid =
2506 2506 ipif_lookup_addr_zoneid_v6(&ip6h->ip6_dst,
2507 2507 ill, ipst);
2508 2508 ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2509 2509 icmp_time_exceeded_v6(mp,
2510 2510 ICMP_REASSEMBLY_TIME_EXCEEDED, B_FALSE,
2511 2511 &iras);
2512 2512 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2513 2513 }
2514 2514 while (send_icmp_head != NULL) {
2515 2515 ipaddr_t dst;
2516 2516
2517 2517 mp = send_icmp_head;
2518 2518 send_icmp_head = send_icmp_head->b_next;
2519 2519 mp->b_next = NULL;
2520 2520
2521 2521 dst = ((ipha_t *)mp->b_rptr)->ipha_dst;
2522 2522
2523 2523 iras.ira_flags = IRAF_IS_IPV4;
2524 2524 /*
2525 2525 * This will result in an incorrect ALL_ZONES zoneid
2526 2526 * for broadcast and multicast packets, but we
2527 2527 * don't send ICMP errors for those in any case.
2528 2528 */
2529 2529 iras.ira_zoneid = ipif_lookup_addr_zoneid(dst,
2530 2530 ill, ipst);
2531 2531 ip_drop_input("ICMP_TIME_EXCEEDED reass", mp, ill);
2532 2532 icmp_time_exceeded(mp,
2533 2533 ICMP_REASSEMBLY_TIME_EXCEEDED, &iras);
2534 2534 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
2535 2535 }
2536 2536 }
2537 2537 /*
2538 2538 * A non-dying ILL will use the return value to decide whether to
2539 2539 * restart the frag timer, and for how long.
2540 2540 */
2541 2541 return (next_timeout);
2542 2542 }
2543 2543
2544 2544 /*
2545 2545 * This routine is called when the approximate count of mblk memory used
2546 2546 * for the specified ILL has exceeded max_count.
2547 2547 */
2548 2548 void
2549 2549 ill_frag_prune(ill_t *ill, uint_t max_count)
2550 2550 {
2551 2551 ipfb_t *ipfb;
2552 2552 ipf_t *ipf;
2553 2553 size_t count;
2554 2554 clock_t now;
2555 2555
2556 2556 /*
2557 2557 * If we are here within ip_min_frag_prune_time msecs remove
2558 2558 * ill_frag_free_num_pkts oldest packets from each bucket and increment
2559 2559 * ill_frag_free_num_pkts.
2560 2560 */
2561 2561 mutex_enter(&ill->ill_lock);
2562 2562 now = ddi_get_lbolt();
2563 2563 if (TICK_TO_MSEC(now - ill->ill_last_frag_clean_time) <=
2564 2564 (ip_min_frag_prune_time != 0 ?
2565 2565 ip_min_frag_prune_time : msec_per_tick)) {
2566 2566
2567 2567 ill->ill_frag_free_num_pkts++;
2568 2568
2569 2569 } else {
2570 2570 ill->ill_frag_free_num_pkts = 0;
2571 2571 }
2572 2572 ill->ill_last_frag_clean_time = now;
2573 2573 mutex_exit(&ill->ill_lock);
2574 2574
2575 2575 /*
2576 2576 * free ill_frag_free_num_pkts oldest packets from each bucket.
2577 2577 */
2578 2578 if (ill->ill_frag_free_num_pkts != 0) {
2579 2579 int ix;
2580 2580
2581 2581 for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2582 2582 ipfb = &ill->ill_frag_hash_tbl[ix];
2583 2583 mutex_enter(&ipfb->ipfb_lock);
2584 2584 if (ipfb->ipfb_ipf != NULL) {
2585 2585 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf,
2586 2586 ill->ill_frag_free_num_pkts);
2587 2587 }
2588 2588 mutex_exit(&ipfb->ipfb_lock);
2589 2589 }
2590 2590 }
2591 2591 /*
2592 2592 * While the reassembly list for this ILL is too big, prune a fragment
2593 2593 * queue by age, oldest first.
2594 2594 */
2595 2595 while (ill->ill_frag_count > max_count) {
2596 2596 int ix;
2597 2597 ipfb_t *oipfb = NULL;
2598 2598 uint_t oldest = UINT_MAX;
2599 2599
2600 2600 count = 0;
2601 2601 for (ix = 0; ix < ILL_FRAG_HASH_TBL_COUNT; ix++) {
2602 2602 ipfb = &ill->ill_frag_hash_tbl[ix];
2603 2603 mutex_enter(&ipfb->ipfb_lock);
2604 2604 ipf = ipfb->ipfb_ipf;
2605 2605 if (ipf != NULL && ipf->ipf_gen < oldest) {
2606 2606 oldest = ipf->ipf_gen;
2607 2607 oipfb = ipfb;
2608 2608 }
2609 2609 count += ipfb->ipfb_count;
2610 2610 mutex_exit(&ipfb->ipfb_lock);
2611 2611 }
2612 2612 if (oipfb == NULL)
2613 2613 break;
2614 2614
2615 2615 if (count <= max_count)
2616 2616 return; /* Somebody beat us to it, nothing to do */
2617 2617 mutex_enter(&oipfb->ipfb_lock);
2618 2618 ipf = oipfb->ipfb_ipf;
2619 2619 if (ipf != NULL) {
2620 2620 ill_frag_free_pkts(ill, oipfb, ipf, 1);
2621 2621 }
2622 2622 mutex_exit(&oipfb->ipfb_lock);
2623 2623 }
2624 2624 }
2625 2625
2626 2626 /*
2627 2627 * free 'free_cnt' fragmented packets starting at ipf.
2628 2628 */
2629 2629 void
2630 2630 ill_frag_free_pkts(ill_t *ill, ipfb_t *ipfb, ipf_t *ipf, int free_cnt)
2631 2631 {
2632 2632 size_t count;
2633 2633 mblk_t *mp;
2634 2634 mblk_t *tmp;
2635 2635 ipf_t **ipfp = ipf->ipf_ptphn;
2636 2636
2637 2637 ASSERT(MUTEX_HELD(&ipfb->ipfb_lock));
2638 2638 ASSERT(ipfp != NULL);
2639 2639 ASSERT(ipf != NULL);
2640 2640
2641 2641 while (ipf != NULL && free_cnt-- > 0) {
2642 2642 count = ipf->ipf_count;
2643 2643 mp = ipf->ipf_mp;
2644 2644 ipf = ipf->ipf_hash_next;
2645 2645 for (tmp = mp; tmp; tmp = tmp->b_cont) {
2646 2646 IP_REASS_SET_START(tmp, 0);
2647 2647 IP_REASS_SET_END(tmp, 0);
2648 2648 }
2649 2649 atomic_add_32(&ill->ill_frag_count, -count);
2650 2650 ASSERT(ipfb->ipfb_count >= count);
2651 2651 ipfb->ipfb_count -= count;
2652 2652 ASSERT(ipfb->ipfb_frag_pkts > 0);
2653 2653 ipfb->ipfb_frag_pkts--;
2654 2654 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmFails);
2655 2655 ip_drop_input("ipIfStatsReasmFails", mp, ill);
2656 2656 freemsg(mp);
2657 2657 }
2658 2658
2659 2659 if (ipf)
2660 2660 ipf->ipf_ptphn = ipfp;
2661 2661 ipfp[0] = ipf;
2662 2662 }
2663 2663
2664 2664 /*
2665 2665 * Helper function for ill_forward_set().
2666 2666 */
2667 2667 static void
2668 2668 ill_forward_set_on_ill(ill_t *ill, boolean_t enable)
2669 2669 {
2670 2670 ip_stack_t *ipst = ill->ill_ipst;
2671 2671
2672 2672 ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2673 2673
2674 2674 ip1dbg(("ill_forward_set: %s %s forwarding on %s",
2675 2675 (enable ? "Enabling" : "Disabling"),
2676 2676 (ill->ill_isv6 ? "IPv6" : "IPv4"), ill->ill_name));
2677 2677 mutex_enter(&ill->ill_lock);
2678 2678 if (enable)
2679 2679 ill->ill_flags |= ILLF_ROUTER;
2680 2680 else
2681 2681 ill->ill_flags &= ~ILLF_ROUTER;
2682 2682 mutex_exit(&ill->ill_lock);
2683 2683 if (ill->ill_isv6)
2684 2684 ill_set_nce_router_flags(ill, enable);
2685 2685 /* Notify routing socket listeners of this change. */
2686 2686 if (ill->ill_ipif != NULL)
2687 2687 ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
2688 2688 }
2689 2689
2690 2690 /*
2691 2691 * Set an ill's ILLF_ROUTER flag appropriately. Send up RTS_IFINFO routing
2692 2692 * socket messages for each interface whose flags we change.
2693 2693 */
2694 2694 int
2695 2695 ill_forward_set(ill_t *ill, boolean_t enable)
2696 2696 {
2697 2697 ipmp_illgrp_t *illg;
2698 2698 ip_stack_t *ipst = ill->ill_ipst;
2699 2699
2700 2700 ASSERT(IAM_WRITER_ILL(ill) || RW_READ_HELD(&ipst->ips_ill_g_lock));
2701 2701
2702 2702 if ((enable && (ill->ill_flags & ILLF_ROUTER)) ||
2703 2703 (!enable && !(ill->ill_flags & ILLF_ROUTER)))
2704 2704 return (0);
2705 2705
2706 2706 if (IS_LOOPBACK(ill))
2707 2707 return (EINVAL);
2708 2708
2709 2709 if (enable && ill->ill_allowed_ips_cnt > 0)
2710 2710 return (EPERM);
2711 2711
2712 2712 if (IS_IPMP(ill) || IS_UNDER_IPMP(ill)) {
2713 2713 /*
2714 2714 * Update all of the interfaces in the group.
2715 2715 */
2716 2716 illg = ill->ill_grp;
2717 2717 ill = list_head(&illg->ig_if);
2718 2718 for (; ill != NULL; ill = list_next(&illg->ig_if, ill))
2719 2719 ill_forward_set_on_ill(ill, enable);
2720 2720
2721 2721 /*
2722 2722 * Update the IPMP meta-interface.
2723 2723 */
2724 2724 ill_forward_set_on_ill(ipmp_illgrp_ipmp_ill(illg), enable);
2725 2725 return (0);
2726 2726 }
2727 2727
2728 2728 ill_forward_set_on_ill(ill, enable);
2729 2729 return (0);
2730 2730 }
2731 2731
2732 2732 /*
2733 2733 * Based on the ILLF_ROUTER flag of an ill, make sure all local nce's for
2734 2734 * addresses assigned to the ill have the NCE_F_ISROUTER flag appropriately
2735 2735 * set or clear.
2736 2736 */
2737 2737 static void
2738 2738 ill_set_nce_router_flags(ill_t *ill, boolean_t enable)
2739 2739 {
2740 2740 ipif_t *ipif;
2741 2741 ncec_t *ncec;
2742 2742 nce_t *nce;
2743 2743
2744 2744 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
2745 2745 /*
2746 2746 * NOTE: we match across the illgrp because nce's for
2747 2747 * addresses on IPMP interfaces have an nce_ill that points to
2748 2748 * the bound underlying ill.
2749 2749 */
2750 2750 nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
2751 2751 if (nce != NULL) {
2752 2752 ncec = nce->nce_common;
2753 2753 mutex_enter(&ncec->ncec_lock);
2754 2754 if (enable)
2755 2755 ncec->ncec_flags |= NCE_F_ISROUTER;
2756 2756 else
2757 2757 ncec->ncec_flags &= ~NCE_F_ISROUTER;
2758 2758 mutex_exit(&ncec->ncec_lock);
2759 2759 nce_refrele(nce);
2760 2760 }
2761 2761 }
2762 2762 }
2763 2763
2764 2764 /*
2765 2765 * Intializes the context structure and returns the first ill in the list
2766 2766 * cuurently start_list and end_list can have values:
2767 2767 * MAX_G_HEADS Traverse both IPV4 and IPV6 lists.
2768 2768 * IP_V4_G_HEAD Traverse IPV4 list only.
2769 2769 * IP_V6_G_HEAD Traverse IPV6 list only.
2770 2770 */
2771 2771
2772 2772 /*
2773 2773 * We don't check for CONDEMNED ills here. Caller must do that if
2774 2774 * necessary under the ill lock.
2775 2775 */
2776 2776 ill_t *
2777 2777 ill_first(int start_list, int end_list, ill_walk_context_t *ctx,
2778 2778 ip_stack_t *ipst)
2779 2779 {
2780 2780 ill_if_t *ifp;
2781 2781 ill_t *ill;
2782 2782 avl_tree_t *avl_tree;
2783 2783
2784 2784 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
2785 2785 ASSERT(end_list <= MAX_G_HEADS && start_list >= 0);
2786 2786
2787 2787 /*
2788 2788 * setup the lists to search
2789 2789 */
2790 2790 if (end_list != MAX_G_HEADS) {
2791 2791 ctx->ctx_current_list = start_list;
2792 2792 ctx->ctx_last_list = end_list;
2793 2793 } else {
2794 2794 ctx->ctx_last_list = MAX_G_HEADS - 1;
2795 2795 ctx->ctx_current_list = 0;
2796 2796 }
2797 2797
2798 2798 while (ctx->ctx_current_list <= ctx->ctx_last_list) {
2799 2799 ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2800 2800 if (ifp != (ill_if_t *)
2801 2801 &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2802 2802 avl_tree = &ifp->illif_avl_by_ppa;
2803 2803 ill = avl_first(avl_tree);
2804 2804 /*
2805 2805 * ill is guaranteed to be non NULL or ifp should have
2806 2806 * not existed.
2807 2807 */
2808 2808 ASSERT(ill != NULL);
2809 2809 return (ill);
2810 2810 }
2811 2811 ctx->ctx_current_list++;
2812 2812 }
2813 2813
2814 2814 return (NULL);
2815 2815 }
2816 2816
2817 2817 /*
2818 2818 * returns the next ill in the list. ill_first() must have been called
2819 2819 * before calling ill_next() or bad things will happen.
2820 2820 */
2821 2821
2822 2822 /*
2823 2823 * We don't check for CONDEMNED ills here. Caller must do that if
2824 2824 * necessary under the ill lock.
2825 2825 */
2826 2826 ill_t *
2827 2827 ill_next(ill_walk_context_t *ctx, ill_t *lastill)
2828 2828 {
2829 2829 ill_if_t *ifp;
2830 2830 ill_t *ill;
2831 2831 ip_stack_t *ipst = lastill->ill_ipst;
2832 2832
2833 2833 ASSERT(lastill->ill_ifptr != (ill_if_t *)
2834 2834 &IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst));
2835 2835 if ((ill = avl_walk(&lastill->ill_ifptr->illif_avl_by_ppa, lastill,
2836 2836 AVL_AFTER)) != NULL) {
2837 2837 return (ill);
2838 2838 }
2839 2839
2840 2840 /* goto next ill_ifp in the list. */
2841 2841 ifp = lastill->ill_ifptr->illif_next;
2842 2842
2843 2843 /* make sure not at end of circular list */
2844 2844 while (ifp ==
2845 2845 (ill_if_t *)&IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst)) {
2846 2846 if (++ctx->ctx_current_list > ctx->ctx_last_list)
2847 2847 return (NULL);
2848 2848 ifp = IP_VX_ILL_G_LIST(ctx->ctx_current_list, ipst);
2849 2849 }
2850 2850
2851 2851 return (avl_first(&ifp->illif_avl_by_ppa));
2852 2852 }
2853 2853
2854 2854 /*
2855 2855 * Check interface name for correct format: [a-zA-Z]+[a-zA-Z0-9._]*[0-9]+
2856 2856 * The final number (PPA) must not have any leading zeros. Upon success, a
2857 2857 * pointer to the start of the PPA is returned; otherwise NULL is returned.
2858 2858 */
2859 2859 static char *
2860 2860 ill_get_ppa_ptr(char *name)
2861 2861 {
2862 2862 int namelen = strlen(name);
2863 2863 int end_ndx = namelen - 1;
2864 2864 int ppa_ndx, i;
2865 2865
2866 2866 /*
2867 2867 * Check that the first character is [a-zA-Z], and that the last
2868 2868 * character is [0-9].
2869 2869 */
2870 2870 if (namelen == 0 || !isalpha(name[0]) || !isdigit(name[end_ndx]))
2871 2871 return (NULL);
2872 2872
2873 2873 /*
2874 2874 * Set `ppa_ndx' to the PPA start, and check for leading zeroes.
2875 2875 */
2876 2876 for (ppa_ndx = end_ndx; ppa_ndx > 0; ppa_ndx--)
2877 2877 if (!isdigit(name[ppa_ndx - 1]))
2878 2878 break;
2879 2879
2880 2880 if (name[ppa_ndx] == '0' && ppa_ndx < end_ndx)
2881 2881 return (NULL);
2882 2882
2883 2883 /*
2884 2884 * Check that the intermediate characters are [a-z0-9.]
2885 2885 */
2886 2886 for (i = 1; i < ppa_ndx; i++) {
2887 2887 if (!isalpha(name[i]) && !isdigit(name[i]) &&
2888 2888 name[i] != '.' && name[i] != '_') {
2889 2889 return (NULL);
2890 2890 }
2891 2891 }
2892 2892
2893 2893 return (name + ppa_ndx);
2894 2894 }
2895 2895
2896 2896 /*
2897 2897 * use avl tree to locate the ill.
2898 2898 */
2899 2899 static ill_t *
2900 2900 ill_find_by_name(char *name, boolean_t isv6, ip_stack_t *ipst)
2901 2901 {
2902 2902 char *ppa_ptr = NULL;
2903 2903 int len;
2904 2904 uint_t ppa;
2905 2905 ill_t *ill = NULL;
2906 2906 ill_if_t *ifp;
2907 2907 int list;
2908 2908
2909 2909 /*
2910 2910 * get ppa ptr
2911 2911 */
2912 2912 if (isv6)
2913 2913 list = IP_V6_G_HEAD;
2914 2914 else
2915 2915 list = IP_V4_G_HEAD;
2916 2916
2917 2917 if ((ppa_ptr = ill_get_ppa_ptr(name)) == NULL) {
2918 2918 return (NULL);
2919 2919 }
2920 2920
2921 2921 len = ppa_ptr - name + 1;
2922 2922
2923 2923 ppa = stoi(&ppa_ptr);
2924 2924
2925 2925 ifp = IP_VX_ILL_G_LIST(list, ipst);
2926 2926
2927 2927 while (ifp != (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2928 2928 /*
2929 2929 * match is done on len - 1 as the name is not null
2930 2930 * terminated it contains ppa in addition to the interface
2931 2931 * name.
2932 2932 */
2933 2933 if ((ifp->illif_name_len == len) &&
2934 2934 bcmp(ifp->illif_name, name, len - 1) == 0) {
2935 2935 break;
2936 2936 } else {
2937 2937 ifp = ifp->illif_next;
2938 2938 }
2939 2939 }
2940 2940
2941 2941 if (ifp == (ill_if_t *)&IP_VX_ILL_G_LIST(list, ipst)) {
2942 2942 /*
2943 2943 * Even the interface type does not exist.
2944 2944 */
2945 2945 return (NULL);
2946 2946 }
2947 2947
2948 2948 ill = avl_find(&ifp->illif_avl_by_ppa, (void *) &ppa, NULL);
2949 2949 if (ill != NULL) {
2950 2950 mutex_enter(&ill->ill_lock);
2951 2951 if (ILL_CAN_LOOKUP(ill)) {
2952 2952 ill_refhold_locked(ill);
2953 2953 mutex_exit(&ill->ill_lock);
2954 2954 return (ill);
2955 2955 }
2956 2956 mutex_exit(&ill->ill_lock);
2957 2957 }
2958 2958 return (NULL);
2959 2959 }
2960 2960
2961 2961 /*
2962 2962 * comparison function for use with avl.
2963 2963 */
2964 2964 static int
2965 2965 ill_compare_ppa(const void *ppa_ptr, const void *ill_ptr)
2966 2966 {
2967 2967 uint_t ppa;
2968 2968 uint_t ill_ppa;
2969 2969
2970 2970 ASSERT(ppa_ptr != NULL && ill_ptr != NULL);
2971 2971
2972 2972 ppa = *((uint_t *)ppa_ptr);
2973 2973 ill_ppa = ((const ill_t *)ill_ptr)->ill_ppa;
2974 2974 /*
2975 2975 * We want the ill with the lowest ppa to be on the
2976 2976 * top.
2977 2977 */
2978 2978 if (ill_ppa < ppa)
2979 2979 return (1);
2980 2980 if (ill_ppa > ppa)
2981 2981 return (-1);
2982 2982 return (0);
2983 2983 }
2984 2984
2985 2985 /*
2986 2986 * remove an interface type from the global list.
2987 2987 */
2988 2988 static void
2989 2989 ill_delete_interface_type(ill_if_t *interface)
2990 2990 {
2991 2991 ASSERT(interface != NULL);
2992 2992 ASSERT(avl_numnodes(&interface->illif_avl_by_ppa) == 0);
2993 2993
2994 2994 avl_destroy(&interface->illif_avl_by_ppa);
2995 2995 if (interface->illif_ppa_arena != NULL)
2996 2996 vmem_destroy(interface->illif_ppa_arena);
2997 2997
2998 2998 remque(interface);
2999 2999
3000 3000 mi_free(interface);
3001 3001 }
3002 3002
3003 3003 /*
3004 3004 * remove ill from the global list.
3005 3005 */
3006 3006 static void
3007 3007 ill_glist_delete(ill_t *ill)
3008 3008 {
3009 3009 ip_stack_t *ipst;
3010 3010 phyint_t *phyi;
3011 3011
3012 3012 if (ill == NULL)
3013 3013 return;
3014 3014 ipst = ill->ill_ipst;
3015 3015 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3016 3016
3017 3017 /*
3018 3018 * If the ill was never inserted into the AVL tree
3019 3019 * we skip the if branch.
3020 3020 */
3021 3021 if (ill->ill_ifptr != NULL) {
3022 3022 /*
3023 3023 * remove from AVL tree and free ppa number
3024 3024 */
3025 3025 avl_remove(&ill->ill_ifptr->illif_avl_by_ppa, ill);
3026 3026
3027 3027 if (ill->ill_ifptr->illif_ppa_arena != NULL) {
3028 3028 vmem_free(ill->ill_ifptr->illif_ppa_arena,
3029 3029 (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3030 3030 }
3031 3031 if (avl_numnodes(&ill->ill_ifptr->illif_avl_by_ppa) == 0) {
3032 3032 ill_delete_interface_type(ill->ill_ifptr);
3033 3033 }
3034 3034
3035 3035 /*
3036 3036 * Indicate ill is no longer in the list.
3037 3037 */
3038 3038 ill->ill_ifptr = NULL;
3039 3039 ill->ill_name_length = 0;
3040 3040 ill->ill_name[0] = '\0';
3041 3041 ill->ill_ppa = UINT_MAX;
3042 3042 }
3043 3043
3044 3044 /* Generate one last event for this ill. */
3045 3045 ill_nic_event_dispatch(ill, 0, NE_UNPLUMB, ill->ill_name,
3046 3046 ill->ill_name_length);
3047 3047
3048 3048 ASSERT(ill->ill_phyint != NULL);
3049 3049 phyi = ill->ill_phyint;
3050 3050 ill->ill_phyint = NULL;
3051 3051
3052 3052 /*
3053 3053 * ill_init allocates a phyint always to store the copy
3054 3054 * of flags relevant to phyint. At that point in time, we could
3055 3055 * not assign the name and hence phyint_illv4/v6 could not be
3056 3056 * initialized. Later in ipif_set_values, we assign the name to
3057 3057 * the ill, at which point in time we assign phyint_illv4/v6.
3058 3058 * Thus we don't rely on phyint_illv6 to be initialized always.
3059 3059 */
3060 3060 if (ill->ill_flags & ILLF_IPV6)
3061 3061 phyi->phyint_illv6 = NULL;
3062 3062 else
3063 3063 phyi->phyint_illv4 = NULL;
3064 3064
3065 3065 if (phyi->phyint_illv4 != NULL || phyi->phyint_illv6 != NULL) {
3066 3066 rw_exit(&ipst->ips_ill_g_lock);
3067 3067 return;
3068 3068 }
3069 3069
3070 3070 /*
3071 3071 * There are no ills left on this phyint; pull it out of the phyint
3072 3072 * avl trees, and free it.
3073 3073 */
3074 3074 if (phyi->phyint_ifindex > 0) {
3075 3075 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3076 3076 phyi);
3077 3077 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
3078 3078 phyi);
3079 3079 }
3080 3080 rw_exit(&ipst->ips_ill_g_lock);
3081 3081
3082 3082 phyint_free(phyi);
3083 3083 }
3084 3084
3085 3085 /*
3086 3086 * allocate a ppa, if the number of plumbed interfaces of this type are
3087 3087 * less than ill_no_arena do a linear search to find a unused ppa.
3088 3088 * When the number goes beyond ill_no_arena switch to using an arena.
3089 3089 * Note: ppa value of zero cannot be allocated from vmem_arena as it
3090 3090 * is the return value for an error condition, so allocation starts at one
3091 3091 * and is decremented by one.
3092 3092 */
3093 3093 static int
3094 3094 ill_alloc_ppa(ill_if_t *ifp, ill_t *ill)
3095 3095 {
3096 3096 ill_t *tmp_ill;
3097 3097 uint_t start, end;
3098 3098 int ppa;
3099 3099
3100 3100 if (ifp->illif_ppa_arena == NULL &&
3101 3101 (avl_numnodes(&ifp->illif_avl_by_ppa) + 1 > ill_no_arena)) {
3102 3102 /*
3103 3103 * Create an arena.
3104 3104 */
3105 3105 ifp->illif_ppa_arena = vmem_create(ifp->illif_name,
3106 3106 (void *)1, UINT_MAX - 1, 1, NULL, NULL,
3107 3107 NULL, 0, VM_SLEEP | VMC_IDENTIFIER);
3108 3108 /* allocate what has already been assigned */
3109 3109 for (tmp_ill = avl_first(&ifp->illif_avl_by_ppa);
3110 3110 tmp_ill != NULL; tmp_ill = avl_walk(&ifp->illif_avl_by_ppa,
3111 3111 tmp_ill, AVL_AFTER)) {
3112 3112 ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3113 3113 1, /* size */
3114 3114 1, /* align/quantum */
3115 3115 0, /* phase */
3116 3116 0, /* nocross */
3117 3117 /* minaddr */
3118 3118 (void *)((uintptr_t)tmp_ill->ill_ppa + 1),
3119 3119 /* maxaddr */
3120 3120 (void *)((uintptr_t)tmp_ill->ill_ppa + 2),
3121 3121 VM_NOSLEEP|VM_FIRSTFIT);
3122 3122 if (ppa == 0) {
3123 3123 ip1dbg(("ill_alloc_ppa: ppa allocation"
3124 3124 " failed while switching"));
3125 3125 vmem_destroy(ifp->illif_ppa_arena);
3126 3126 ifp->illif_ppa_arena = NULL;
3127 3127 break;
3128 3128 }
3129 3129 }
3130 3130 }
3131 3131
3132 3132 if (ifp->illif_ppa_arena != NULL) {
3133 3133 if (ill->ill_ppa == UINT_MAX) {
3134 3134 ppa = (int)(uintptr_t)vmem_alloc(ifp->illif_ppa_arena,
3135 3135 1, VM_NOSLEEP|VM_FIRSTFIT);
3136 3136 if (ppa == 0)
3137 3137 return (EAGAIN);
3138 3138 ill->ill_ppa = --ppa;
3139 3139 } else {
3140 3140 ppa = (int)(uintptr_t)vmem_xalloc(ifp->illif_ppa_arena,
3141 3141 1, /* size */
3142 3142 1, /* align/quantum */
3143 3143 0, /* phase */
3144 3144 0, /* nocross */
3145 3145 (void *)(uintptr_t)(ill->ill_ppa + 1), /* minaddr */
3146 3146 (void *)(uintptr_t)(ill->ill_ppa + 2), /* maxaddr */
3147 3147 VM_NOSLEEP|VM_FIRSTFIT);
3148 3148 /*
3149 3149 * Most likely the allocation failed because
3150 3150 * the requested ppa was in use.
3151 3151 */
3152 3152 if (ppa == 0)
3153 3153 return (EEXIST);
3154 3154 }
3155 3155 return (0);
3156 3156 }
3157 3157
3158 3158 /*
3159 3159 * No arena is in use and not enough (>ill_no_arena) interfaces have
3160 3160 * been plumbed to create one. Do a linear search to get a unused ppa.
3161 3161 */
3162 3162 if (ill->ill_ppa == UINT_MAX) {
3163 3163 end = UINT_MAX - 1;
3164 3164 start = 0;
3165 3165 } else {
3166 3166 end = start = ill->ill_ppa;
3167 3167 }
3168 3168
3169 3169 tmp_ill = avl_find(&ifp->illif_avl_by_ppa, (void *)&start, NULL);
3170 3170 while (tmp_ill != NULL && tmp_ill->ill_ppa == start) {
3171 3171 if (start++ >= end) {
3172 3172 if (ill->ill_ppa == UINT_MAX)
3173 3173 return (EAGAIN);
3174 3174 else
3175 3175 return (EEXIST);
3176 3176 }
3177 3177 tmp_ill = avl_walk(&ifp->illif_avl_by_ppa, tmp_ill, AVL_AFTER);
3178 3178 }
3179 3179 ill->ill_ppa = start;
3180 3180 return (0);
3181 3181 }
3182 3182
3183 3183 /*
3184 3184 * Insert ill into the list of configured ill's. Once this function completes,
3185 3185 * the ill is globally visible and is available through lookups. More precisely
3186 3186 * this happens after the caller drops the ill_g_lock.
3187 3187 */
3188 3188 static int
3189 3189 ill_glist_insert(ill_t *ill, char *name, boolean_t isv6)
3190 3190 {
3191 3191 ill_if_t *ill_interface;
3192 3192 avl_index_t where = 0;
3193 3193 int error;
3194 3194 int name_length;
3195 3195 int index;
3196 3196 boolean_t check_length = B_FALSE;
3197 3197 ip_stack_t *ipst = ill->ill_ipst;
3198 3198
3199 3199 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
3200 3200
3201 3201 name_length = mi_strlen(name) + 1;
3202 3202
3203 3203 if (isv6)
3204 3204 index = IP_V6_G_HEAD;
3205 3205 else
3206 3206 index = IP_V4_G_HEAD;
3207 3207
3208 3208 ill_interface = IP_VX_ILL_G_LIST(index, ipst);
3209 3209 /*
3210 3210 * Search for interface type based on name
3211 3211 */
3212 3212 while (ill_interface != (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3213 3213 if ((ill_interface->illif_name_len == name_length) &&
3214 3214 (strcmp(ill_interface->illif_name, name) == 0)) {
3215 3215 break;
3216 3216 }
3217 3217 ill_interface = ill_interface->illif_next;
3218 3218 }
3219 3219
3220 3220 /*
3221 3221 * Interface type not found, create one.
3222 3222 */
3223 3223 if (ill_interface == (ill_if_t *)&IP_VX_ILL_G_LIST(index, ipst)) {
3224 3224 ill_g_head_t ghead;
3225 3225
3226 3226 /*
3227 3227 * allocate ill_if_t structure
3228 3228 */
3229 3229 ill_interface = (ill_if_t *)mi_zalloc(sizeof (ill_if_t));
3230 3230 if (ill_interface == NULL) {
3231 3231 return (ENOMEM);
3232 3232 }
3233 3233
3234 3234 (void) strcpy(ill_interface->illif_name, name);
3235 3235 ill_interface->illif_name_len = name_length;
3236 3236
3237 3237 avl_create(&ill_interface->illif_avl_by_ppa,
3238 3238 ill_compare_ppa, sizeof (ill_t),
3239 3239 offsetof(struct ill_s, ill_avl_byppa));
3240 3240
3241 3241 /*
3242 3242 * link the structure in the back to maintain order
3243 3243 * of configuration for ifconfig output.
3244 3244 */
3245 3245 ghead = ipst->ips_ill_g_heads[index];
3246 3246 insque(ill_interface, ghead.ill_g_list_tail);
3247 3247 }
3248 3248
3249 3249 if (ill->ill_ppa == UINT_MAX)
3250 3250 check_length = B_TRUE;
3251 3251
3252 3252 error = ill_alloc_ppa(ill_interface, ill);
3253 3253 if (error != 0) {
3254 3254 if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3255 3255 ill_delete_interface_type(ill->ill_ifptr);
3256 3256 return (error);
3257 3257 }
3258 3258
3259 3259 /*
3260 3260 * When the ppa is choosen by the system, check that there is
3261 3261 * enough space to insert ppa. if a specific ppa was passed in this
3262 3262 * check is not required as the interface name passed in will have
3263 3263 * the right ppa in it.
3264 3264 */
3265 3265 if (check_length) {
3266 3266 /*
3267 3267 * UINT_MAX - 1 should fit in 10 chars, alloc 12 chars.
3268 3268 */
3269 3269 char buf[sizeof (uint_t) * 3];
3270 3270
3271 3271 /*
3272 3272 * convert ppa to string to calculate the amount of space
3273 3273 * required for it in the name.
3274 3274 */
3275 3275 numtos(ill->ill_ppa, buf);
3276 3276
3277 3277 /* Do we have enough space to insert ppa ? */
3278 3278
3279 3279 if ((mi_strlen(name) + mi_strlen(buf) + 1) > LIFNAMSIZ) {
3280 3280 /* Free ppa and interface type struct */
3281 3281 if (ill_interface->illif_ppa_arena != NULL) {
3282 3282 vmem_free(ill_interface->illif_ppa_arena,
3283 3283 (void *)(uintptr_t)(ill->ill_ppa+1), 1);
3284 3284 }
3285 3285 if (avl_numnodes(&ill_interface->illif_avl_by_ppa) == 0)
3286 3286 ill_delete_interface_type(ill->ill_ifptr);
3287 3287
3288 3288 return (EINVAL);
3289 3289 }
3290 3290 }
3291 3291
3292 3292 (void) sprintf(ill->ill_name, "%s%u", name, ill->ill_ppa);
3293 3293 ill->ill_name_length = mi_strlen(ill->ill_name) + 1;
3294 3294
3295 3295 (void) avl_find(&ill_interface->illif_avl_by_ppa, &ill->ill_ppa,
3296 3296 &where);
3297 3297 ill->ill_ifptr = ill_interface;
3298 3298 avl_insert(&ill_interface->illif_avl_by_ppa, ill, where);
3299 3299
3300 3300 ill_phyint_reinit(ill);
3301 3301 return (0);
3302 3302 }
3303 3303
3304 3304 /* Initialize the per phyint ipsq used for serialization */
3305 3305 static boolean_t
3306 3306 ipsq_init(ill_t *ill, boolean_t enter)
3307 3307 {
3308 3308 ipsq_t *ipsq;
3309 3309 ipxop_t *ipx;
3310 3310
3311 3311 if ((ipsq = kmem_zalloc(sizeof (ipsq_t), KM_NOSLEEP)) == NULL)
3312 3312 return (B_FALSE);
3313 3313
3314 3314 ill->ill_phyint->phyint_ipsq = ipsq;
3315 3315 ipx = ipsq->ipsq_xop = &ipsq->ipsq_ownxop;
3316 3316 ipx->ipx_ipsq = ipsq;
3317 3317 ipsq->ipsq_next = ipsq;
3318 3318 ipsq->ipsq_phyint = ill->ill_phyint;
3319 3319 mutex_init(&ipsq->ipsq_lock, NULL, MUTEX_DEFAULT, 0);
3320 3320 mutex_init(&ipx->ipx_lock, NULL, MUTEX_DEFAULT, 0);
3321 3321 ipsq->ipsq_ipst = ill->ill_ipst; /* No netstack_hold */
3322 3322 if (enter) {
3323 3323 ipx->ipx_writer = curthread;
3324 3324 ipx->ipx_forced = B_FALSE;
3325 3325 ipx->ipx_reentry_cnt = 1;
3326 3326 #ifdef DEBUG
3327 3327 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
3328 3328 #endif
3329 3329 }
3330 3330 return (B_TRUE);
3331 3331 }
3332 3332
3333 3333 /*
3334 3334 * ill_init is called by ip_open when a device control stream is opened.
3335 3335 * It does a few initializations, and shoots a DL_INFO_REQ message down
3336 3336 * to the driver. The response is later picked up in ip_rput_dlpi and
3337 3337 * used to set up default mechanisms for talking to the driver. (Always
3338 3338 * called as writer.)
3339 3339 *
3340 3340 * If this function returns error, ip_open will call ip_close which in
3341 3341 * turn will call ill_delete to clean up any memory allocated here that
3342 3342 * is not yet freed.
3343 3343 */
3344 3344 int
3345 3345 ill_init(queue_t *q, ill_t *ill)
3346 3346 {
3347 3347 int count;
3348 3348 dl_info_req_t *dlir;
3349 3349 mblk_t *info_mp;
3350 3350 uchar_t *frag_ptr;
3351 3351
3352 3352 /*
3353 3353 * The ill is initialized to zero by mi_alloc*(). In addition
3354 3354 * some fields already contain valid values, initialized in
3355 3355 * ip_open(), before we reach here.
3356 3356 */
3357 3357 mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, 0);
3358 3358 mutex_init(&ill->ill_saved_ire_lock, NULL, MUTEX_DEFAULT, NULL);
3359 3359 ill->ill_saved_ire_cnt = 0;
3360 3360
3361 3361 ill->ill_rq = q;
3362 3362 ill->ill_wq = WR(q);
3363 3363
3364 3364 info_mp = allocb(MAX(sizeof (dl_info_req_t), sizeof (dl_info_ack_t)),
3365 3365 BPRI_HI);
3366 3366 if (info_mp == NULL)
3367 3367 return (ENOMEM);
3368 3368
3369 3369 /*
3370 3370 * Allocate sufficient space to contain our fragment hash table and
3371 3371 * the device name.
3372 3372 */
3373 3373 frag_ptr = (uchar_t *)mi_zalloc(ILL_FRAG_HASH_TBL_SIZE + 2 * LIFNAMSIZ);
3374 3374 if (frag_ptr == NULL) {
3375 3375 freemsg(info_mp);
3376 3376 return (ENOMEM);
3377 3377 }
3378 3378 ill->ill_frag_ptr = frag_ptr;
3379 3379 ill->ill_frag_free_num_pkts = 0;
3380 3380 ill->ill_last_frag_clean_time = 0;
3381 3381 ill->ill_frag_hash_tbl = (ipfb_t *)frag_ptr;
3382 3382 ill->ill_name = (char *)(frag_ptr + ILL_FRAG_HASH_TBL_SIZE);
3383 3383 for (count = 0; count < ILL_FRAG_HASH_TBL_COUNT; count++) {
3384 3384 mutex_init(&ill->ill_frag_hash_tbl[count].ipfb_lock,
3385 3385 NULL, MUTEX_DEFAULT, NULL);
3386 3386 }
3387 3387
3388 3388 ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3389 3389 if (ill->ill_phyint == NULL) {
3390 3390 freemsg(info_mp);
3391 3391 mi_free(frag_ptr);
3392 3392 return (ENOMEM);
3393 3393 }
3394 3394
3395 3395 mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3396 3396 /*
3397 3397 * For now pretend this is a v4 ill. We need to set phyint_ill*
3398 3398 * at this point because of the following reason. If we can't
3399 3399 * enter the ipsq at some point and cv_wait, the writer that
3400 3400 * wakes us up tries to locate us using the list of all phyints
3401 3401 * in an ipsq and the ills from the phyint thru the phyint_ill*.
3402 3402 * If we don't set it now, we risk a missed wakeup.
3403 3403 */
3404 3404 ill->ill_phyint->phyint_illv4 = ill;
3405 3405 ill->ill_ppa = UINT_MAX;
3406 3406 list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3407 3407
3408 3408 ill_set_inputfn(ill);
3409 3409
3410 3410 if (!ipsq_init(ill, B_TRUE)) {
3411 3411 freemsg(info_mp);
3412 3412 mi_free(frag_ptr);
3413 3413 mi_free(ill->ill_phyint);
3414 3414 return (ENOMEM);
3415 3415 }
3416 3416
3417 3417 ill->ill_state_flags |= ILL_LL_SUBNET_PENDING;
3418 3418
3419 3419 /* Frag queue limit stuff */
3420 3420 ill->ill_frag_count = 0;
3421 3421 ill->ill_ipf_gen = 0;
3422 3422
3423 3423 rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3424 3424 mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3425 3425 ill->ill_global_timer = INFINITY;
3426 3426 ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3427 3427 ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3428 3428 ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3429 3429 ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3430 3430
3431 3431 /*
3432 3432 * Initialize IPv6 configuration variables. The IP module is always
3433 3433 * opened as an IPv4 module. Instead tracking down the cases where
3434 3434 * it switches to do ipv6, we'll just initialize the IPv6 configuration
3435 3435 * here for convenience, this has no effect until the ill is set to do
3436 3436 * IPv6.
3437 3437 */
3438 3438 ill->ill_reachable_time = ND_REACHABLE_TIME;
3439 3439 ill->ill_xmit_count = ND_MAX_MULTICAST_SOLICIT;
3440 3440 ill->ill_max_buf = ND_MAX_Q;
3441 3441 ill->ill_refcnt = 0;
3442 3442
3443 3443 /* Send down the Info Request to the driver. */
3444 3444 info_mp->b_datap->db_type = M_PCPROTO;
3445 3445 dlir = (dl_info_req_t *)info_mp->b_rptr;
3446 3446 info_mp->b_wptr = (uchar_t *)&dlir[1];
3447 3447 dlir->dl_primitive = DL_INFO_REQ;
3448 3448
3449 3449 ill->ill_dlpi_pending = DL_PRIM_INVAL;
3450 3450
3451 3451 qprocson(q);
3452 3452 ill_dlpi_send(ill, info_mp);
3453 3453
3454 3454 return (0);
3455 3455 }
3456 3456
3457 3457 /*
3458 3458 * ill_dls_info
3459 3459 * creates datalink socket info from the device.
3460 3460 */
3461 3461 int
3462 3462 ill_dls_info(struct sockaddr_dl *sdl, const ill_t *ill)
3463 3463 {
3464 3464 size_t len;
3465 3465
3466 3466 sdl->sdl_family = AF_LINK;
3467 3467 sdl->sdl_index = ill_get_upper_ifindex(ill);
3468 3468 sdl->sdl_type = ill->ill_type;
3469 3469 ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3470 3470 len = strlen(sdl->sdl_data);
3471 3471 ASSERT(len < 256);
3472 3472 sdl->sdl_nlen = (uchar_t)len;
3473 3473 sdl->sdl_alen = ill->ill_phys_addr_length;
3474 3474 sdl->sdl_slen = 0;
3475 3475 if (ill->ill_phys_addr_length != 0 && ill->ill_phys_addr != NULL)
3476 3476 bcopy(ill->ill_phys_addr, &sdl->sdl_data[len], sdl->sdl_alen);
3477 3477
3478 3478 return (sizeof (struct sockaddr_dl));
3479 3479 }
3480 3480
3481 3481 /*
3482 3482 * ill_xarp_info
3483 3483 * creates xarp info from the device.
3484 3484 */
3485 3485 static int
3486 3486 ill_xarp_info(struct sockaddr_dl *sdl, ill_t *ill)
3487 3487 {
3488 3488 sdl->sdl_family = AF_LINK;
3489 3489 sdl->sdl_index = ill->ill_phyint->phyint_ifindex;
3490 3490 sdl->sdl_type = ill->ill_type;
3491 3491 ill_get_name(ill, sdl->sdl_data, sizeof (sdl->sdl_data));
3492 3492 sdl->sdl_nlen = (uchar_t)mi_strlen(sdl->sdl_data);
3493 3493 sdl->sdl_alen = ill->ill_phys_addr_length;
3494 3494 sdl->sdl_slen = 0;
3495 3495 return (sdl->sdl_nlen);
3496 3496 }
3497 3497
3498 3498 static int
3499 3499 loopback_kstat_update(kstat_t *ksp, int rw)
3500 3500 {
3501 3501 kstat_named_t *kn;
3502 3502 netstackid_t stackid;
3503 3503 netstack_t *ns;
3504 3504 ip_stack_t *ipst;
3505 3505
3506 3506 if (ksp == NULL || ksp->ks_data == NULL)
3507 3507 return (EIO);
3508 3508
3509 3509 if (rw == KSTAT_WRITE)
3510 3510 return (EACCES);
3511 3511
3512 3512 kn = KSTAT_NAMED_PTR(ksp);
3513 3513 stackid = (zoneid_t)(uintptr_t)ksp->ks_private;
3514 3514
3515 3515 ns = netstack_find_by_stackid(stackid);
3516 3516 if (ns == NULL)
3517 3517 return (-1);
3518 3518
3519 3519 ipst = ns->netstack_ip;
3520 3520 if (ipst == NULL) {
3521 3521 netstack_rele(ns);
3522 3522 return (-1);
3523 3523 }
3524 3524 kn[0].value.ui32 = ipst->ips_loopback_packets;
3525 3525 kn[1].value.ui32 = ipst->ips_loopback_packets;
3526 3526 netstack_rele(ns);
3527 3527 return (0);
3528 3528 }
3529 3529
3530 3530 /*
3531 3531 * Has ifindex been plumbed already?
3532 3532 */
3533 3533 static boolean_t
3534 3534 phyint_exists(uint_t index, ip_stack_t *ipst)
3535 3535 {
3536 3536 ASSERT(index != 0);
3537 3537 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
3538 3538
3539 3539 return (avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3540 3540 &index, NULL) != NULL);
3541 3541 }
3542 3542
3543 3543 /*
3544 3544 * Pick a unique ifindex.
3545 3545 * When the index counter passes IF_INDEX_MAX for the first time, the wrap
3546 3546 * flag is set so that next time time ip_assign_ifindex() is called, it
3547 3547 * falls through and resets the index counter back to 1, the minimum value
3548 3548 * for the interface index. The logic below assumes that ips_ill_index
3549 3549 * can hold a value of IF_INDEX_MAX+1 without there being any loss
3550 3550 * (i.e. reset back to 0.)
3551 3551 */
3552 3552 boolean_t
3553 3553 ip_assign_ifindex(uint_t *indexp, ip_stack_t *ipst)
3554 3554 {
3555 3555 uint_t loops;
3556 3556
3557 3557 if (!ipst->ips_ill_index_wrap) {
3558 3558 *indexp = ipst->ips_ill_index++;
3559 3559 if (ipst->ips_ill_index > IF_INDEX_MAX) {
3560 3560 /*
3561 3561 * Reached the maximum ifindex value, set the wrap
3562 3562 * flag to indicate that it is no longer possible
3563 3563 * to assume that a given index is unallocated.
3564 3564 */
3565 3565 ipst->ips_ill_index_wrap = B_TRUE;
3566 3566 }
3567 3567 return (B_TRUE);
3568 3568 }
3569 3569
3570 3570 if (ipst->ips_ill_index > IF_INDEX_MAX)
3571 3571 ipst->ips_ill_index = 1;
3572 3572
3573 3573 /*
3574 3574 * Start reusing unused indexes. Note that we hold the ill_g_lock
3575 3575 * at this point and don't want to call any function that attempts
3576 3576 * to get the lock again.
3577 3577 */
3578 3578 for (loops = IF_INDEX_MAX; loops > 0; loops--) {
3579 3579 if (!phyint_exists(ipst->ips_ill_index, ipst)) {
3580 3580 /* found unused index - use it */
3581 3581 *indexp = ipst->ips_ill_index;
3582 3582 return (B_TRUE);
3583 3583 }
3584 3584
3585 3585 ipst->ips_ill_index++;
3586 3586 if (ipst->ips_ill_index > IF_INDEX_MAX)
3587 3587 ipst->ips_ill_index = 1;
3588 3588 }
3589 3589
3590 3590 /*
3591 3591 * all interface indicies are inuse.
3592 3592 */
3593 3593 return (B_FALSE);
3594 3594 }
3595 3595
3596 3596 /*
3597 3597 * Assign a unique interface index for the phyint.
3598 3598 */
3599 3599 static boolean_t
3600 3600 phyint_assign_ifindex(phyint_t *phyi, ip_stack_t *ipst)
3601 3601 {
3602 3602 ASSERT(phyi->phyint_ifindex == 0);
3603 3603 return (ip_assign_ifindex(&phyi->phyint_ifindex, ipst));
3604 3604 }
3605 3605
3606 3606 /*
3607 3607 * Initialize the flags on `phyi' as per the provided mactype.
3608 3608 */
3609 3609 static void
3610 3610 phyint_flags_init(phyint_t *phyi, t_uscalar_t mactype)
3611 3611 {
3612 3612 uint64_t flags = 0;
3613 3613
3614 3614 /*
3615 3615 * Initialize PHYI_RUNNING and PHYI_FAILED. For non-IPMP interfaces,
3616 3616 * we always presume the underlying hardware is working and set
3617 3617 * PHYI_RUNNING (if it's not, the driver will subsequently send a
3618 3618 * DL_NOTE_LINK_DOWN message). For IPMP interfaces, at initialization
3619 3619 * there are no active interfaces in the group so we set PHYI_FAILED.
3620 3620 */
3621 3621 if (mactype == SUNW_DL_IPMP)
3622 3622 flags |= PHYI_FAILED;
3623 3623 else
3624 3624 flags |= PHYI_RUNNING;
3625 3625
3626 3626 switch (mactype) {
3627 3627 case SUNW_DL_VNI:
3628 3628 flags |= PHYI_VIRTUAL;
3629 3629 break;
3630 3630 case SUNW_DL_IPMP:
3631 3631 flags |= PHYI_IPMP;
3632 3632 break;
3633 3633 case DL_LOOP:
3634 3634 flags |= (PHYI_LOOPBACK | PHYI_VIRTUAL);
3635 3635 break;
3636 3636 }
3637 3637
3638 3638 mutex_enter(&phyi->phyint_lock);
3639 3639 phyi->phyint_flags |= flags;
3640 3640 mutex_exit(&phyi->phyint_lock);
3641 3641 }
3642 3642
3643 3643 /*
3644 3644 * Return a pointer to the ill which matches the supplied name. Note that
3645 3645 * the ill name length includes the null termination character. (May be
3646 3646 * called as writer.)
3647 3647 * If do_alloc and the interface is "lo0" it will be automatically created.
3648 3648 * Cannot bump up reference on condemned ills. So dup detect can't be done
3649 3649 * using this func.
3650 3650 */
3651 3651 ill_t *
3652 3652 ill_lookup_on_name(char *name, boolean_t do_alloc, boolean_t isv6,
3653 3653 boolean_t *did_alloc, ip_stack_t *ipst)
3654 3654 {
3655 3655 ill_t *ill;
3656 3656 ipif_t *ipif;
3657 3657 ipsq_t *ipsq;
3658 3658 kstat_named_t *kn;
3659 3659 boolean_t isloopback;
3660 3660 in6_addr_t ov6addr;
3661 3661
3662 3662 isloopback = mi_strcmp(name, ipif_loopback_name) == 0;
3663 3663
3664 3664 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3665 3665 ill = ill_find_by_name(name, isv6, ipst);
3666 3666 rw_exit(&ipst->ips_ill_g_lock);
3667 3667 if (ill != NULL)
3668 3668 return (ill);
3669 3669
3670 3670 /*
3671 3671 * Couldn't find it. Does this happen to be a lookup for the
3672 3672 * loopback device and are we allowed to allocate it?
3673 3673 */
3674 3674 if (!isloopback || !do_alloc)
3675 3675 return (NULL);
3676 3676
3677 3677 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
3678 3678 ill = ill_find_by_name(name, isv6, ipst);
3679 3679 if (ill != NULL) {
3680 3680 rw_exit(&ipst->ips_ill_g_lock);
3681 3681 return (ill);
3682 3682 }
3683 3683
3684 3684 /* Create the loopback device on demand */
3685 3685 ill = (ill_t *)(mi_alloc(sizeof (ill_t) +
3686 3686 sizeof (ipif_loopback_name), BPRI_MED));
3687 3687 if (ill == NULL)
3688 3688 goto done;
3689 3689
3690 3690 *ill = ill_null;
3691 3691 mutex_init(&ill->ill_lock, NULL, MUTEX_DEFAULT, NULL);
3692 3692 ill->ill_ipst = ipst;
3693 3693 list_create(&ill->ill_nce, sizeof (nce_t), offsetof(nce_t, nce_node));
3694 3694 netstack_hold(ipst->ips_netstack);
3695 3695 /*
3696 3696 * For exclusive stacks we set the zoneid to zero
3697 3697 * to make IP operate as if in the global zone.
3698 3698 */
3699 3699 ill->ill_zoneid = GLOBAL_ZONEID;
3700 3700
3701 3701 ill->ill_phyint = (phyint_t *)mi_zalloc(sizeof (phyint_t));
3702 3702 if (ill->ill_phyint == NULL)
3703 3703 goto done;
3704 3704
3705 3705 if (isv6)
3706 3706 ill->ill_phyint->phyint_illv6 = ill;
3707 3707 else
3708 3708 ill->ill_phyint->phyint_illv4 = ill;
3709 3709 mutex_init(&ill->ill_phyint->phyint_lock, NULL, MUTEX_DEFAULT, 0);
3710 3710 phyint_flags_init(ill->ill_phyint, DL_LOOP);
3711 3711
3712 3712 if (isv6) {
3713 3713 ill->ill_isv6 = B_TRUE;
3714 3714 ill->ill_max_frag = ip_loopback_mtu_v6plus;
3715 3715 } else {
3716 3716 ill->ill_max_frag = ip_loopback_mtuplus;
3717 3717 }
3718 3718 if (!ill_allocate_mibs(ill))
3719 3719 goto done;
3720 3720 ill->ill_current_frag = ill->ill_max_frag;
3721 3721 ill->ill_mtu = ill->ill_max_frag; /* Initial value */
3722 3722 ill->ill_mc_mtu = ill->ill_mtu;
3723 3723 /*
3724 3724 * ipif_loopback_name can't be pointed at directly because its used
3725 3725 * by both the ipv4 and ipv6 interfaces. When the ill is removed
3726 3726 * from the glist, ill_glist_delete() sets the first character of
3727 3727 * ill_name to '\0'.
3728 3728 */
3729 3729 ill->ill_name = (char *)ill + sizeof (*ill);
3730 3730 (void) strcpy(ill->ill_name, ipif_loopback_name);
3731 3731 ill->ill_name_length = sizeof (ipif_loopback_name);
3732 3732 /* Set ill_dlpi_pending for ipsq_current_finish() to work properly */
3733 3733 ill->ill_dlpi_pending = DL_PRIM_INVAL;
3734 3734
3735 3735 rw_init(&ill->ill_mcast_lock, NULL, RW_DEFAULT, NULL);
3736 3736 mutex_init(&ill->ill_mcast_serializer, NULL, MUTEX_DEFAULT, NULL);
3737 3737 ill->ill_global_timer = INFINITY;
3738 3738 ill->ill_mcast_v1_time = ill->ill_mcast_v2_time = 0;
3739 3739 ill->ill_mcast_v1_tset = ill->ill_mcast_v2_tset = 0;
3740 3740 ill->ill_mcast_rv = MCAST_DEF_ROBUSTNESS;
3741 3741 ill->ill_mcast_qi = MCAST_DEF_QUERY_INTERVAL;
3742 3742
3743 3743 /* No resolver here. */
3744 3744 ill->ill_net_type = IRE_LOOPBACK;
3745 3745
3746 3746 /* Initialize the ipsq */
3747 3747 if (!ipsq_init(ill, B_FALSE))
3748 3748 goto done;
3749 3749
3750 3750 ipif = ipif_allocate(ill, 0L, IRE_LOOPBACK, B_TRUE, B_TRUE, NULL);
3751 3751 if (ipif == NULL)
3752 3752 goto done;
3753 3753
3754 3754 ill->ill_flags = ILLF_MULTICAST;
3755 3755
3756 3756 ov6addr = ipif->ipif_v6lcl_addr;
3757 3757 /* Set up default loopback address and mask. */
3758 3758 if (!isv6) {
3759 3759 ipaddr_t inaddr_loopback = htonl(INADDR_LOOPBACK);
3760 3760
3761 3761 IN6_IPADDR_TO_V4MAPPED(inaddr_loopback, &ipif->ipif_v6lcl_addr);
3762 3762 V4MASK_TO_V6(htonl(IN_CLASSA_NET), ipif->ipif_v6net_mask);
3763 3763 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3764 3764 ipif->ipif_v6subnet);
3765 3765 ill->ill_flags |= ILLF_IPV4;
3766 3766 } else {
3767 3767 ipif->ipif_v6lcl_addr = ipv6_loopback;
3768 3768 ipif->ipif_v6net_mask = ipv6_all_ones;
3769 3769 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
3770 3770 ipif->ipif_v6subnet);
3771 3771 ill->ill_flags |= ILLF_IPV6;
3772 3772 }
3773 3773
3774 3774 /*
3775 3775 * Chain us in at the end of the ill list. hold the ill
3776 3776 * before we make it globally visible. 1 for the lookup.
3777 3777 */
3778 3778 ill->ill_refcnt = 0;
3779 3779 ill_refhold(ill);
3780 3780
3781 3781 ill->ill_frag_count = 0;
3782 3782 ill->ill_frag_free_num_pkts = 0;
3783 3783 ill->ill_last_frag_clean_time = 0;
3784 3784
3785 3785 ipsq = ill->ill_phyint->phyint_ipsq;
3786 3786
3787 3787 ill_set_inputfn(ill);
3788 3788
3789 3789 if (ill_glist_insert(ill, "lo", isv6) != 0)
3790 3790 cmn_err(CE_PANIC, "cannot insert loopback interface");
3791 3791
3792 3792 /* Let SCTP know so that it can add this to its list */
3793 3793 sctp_update_ill(ill, SCTP_ILL_INSERT);
3794 3794
3795 3795 /*
3796 3796 * We have already assigned ipif_v6lcl_addr above, but we need to
3797 3797 * call sctp_update_ipif_addr() after SCTP_ILL_INSERT, which
3798 3798 * requires to be after ill_glist_insert() since we need the
3799 3799 * ill_index set. Pass on ipv6_loopback as the old address.
3800 3800 */
3801 3801 sctp_update_ipif_addr(ipif, ov6addr);
3802 3802
3803 3803 ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
3804 3804
3805 3805 /*
3806 3806 * ill_glist_insert() -> ill_phyint_reinit() may have merged IPSQs.
3807 3807 * If so, free our original one.
3808 3808 */
3809 3809 if (ipsq != ill->ill_phyint->phyint_ipsq)
3810 3810 ipsq_delete(ipsq);
3811 3811
3812 3812 if (ipst->ips_loopback_ksp == NULL) {
3813 3813 /* Export loopback interface statistics */
3814 3814 ipst->ips_loopback_ksp = kstat_create_netstack("lo", 0,
3815 3815 ipif_loopback_name, "net",
3816 3816 KSTAT_TYPE_NAMED, 2, 0,
3817 3817 ipst->ips_netstack->netstack_stackid);
3818 3818 if (ipst->ips_loopback_ksp != NULL) {
3819 3819 ipst->ips_loopback_ksp->ks_update =
3820 3820 loopback_kstat_update;
3821 3821 kn = KSTAT_NAMED_PTR(ipst->ips_loopback_ksp);
3822 3822 kstat_named_init(&kn[0], "ipackets", KSTAT_DATA_UINT32);
3823 3823 kstat_named_init(&kn[1], "opackets", KSTAT_DATA_UINT32);
3824 3824 ipst->ips_loopback_ksp->ks_private =
3825 3825 (void *)(uintptr_t)ipst->ips_netstack->
3826 3826 netstack_stackid;
3827 3827 kstat_install(ipst->ips_loopback_ksp);
3828 3828 }
3829 3829 }
3830 3830
3831 3831 *did_alloc = B_TRUE;
3832 3832 rw_exit(&ipst->ips_ill_g_lock);
3833 3833 ill_nic_event_dispatch(ill, MAP_IPIF_ID(ill->ill_ipif->ipif_id),
3834 3834 NE_PLUMB, ill->ill_name, ill->ill_name_length);
3835 3835 return (ill);
3836 3836 done:
3837 3837 if (ill != NULL) {
3838 3838 if (ill->ill_phyint != NULL) {
3839 3839 ipsq = ill->ill_phyint->phyint_ipsq;
3840 3840 if (ipsq != NULL) {
3841 3841 ipsq->ipsq_phyint = NULL;
3842 3842 ipsq_delete(ipsq);
3843 3843 }
3844 3844 mi_free(ill->ill_phyint);
3845 3845 }
3846 3846 ill_free_mib(ill);
3847 3847 if (ill->ill_ipst != NULL)
3848 3848 netstack_rele(ill->ill_ipst->ips_netstack);
3849 3849 mi_free(ill);
3850 3850 }
3851 3851 rw_exit(&ipst->ips_ill_g_lock);
3852 3852 return (NULL);
3853 3853 }
3854 3854
3855 3855 /*
3856 3856 * For IPP calls - use the ip_stack_t for global stack.
3857 3857 */
3858 3858 ill_t *
3859 3859 ill_lookup_on_ifindex_global_instance(uint_t index, boolean_t isv6)
3860 3860 {
3861 3861 ip_stack_t *ipst;
3862 3862 ill_t *ill;
3863 3863
3864 3864 ipst = netstack_find_by_stackid(GLOBAL_NETSTACKID)->netstack_ip;
3865 3865 if (ipst == NULL) {
3866 3866 cmn_err(CE_WARN, "No ip_stack_t for zoneid zero!\n");
3867 3867 return (NULL);
3868 3868 }
3869 3869
3870 3870 ill = ill_lookup_on_ifindex(index, isv6, ipst);
3871 3871 netstack_rele(ipst->ips_netstack);
3872 3872 return (ill);
3873 3873 }
3874 3874
3875 3875 /*
3876 3876 * Return a pointer to the ill which matches the index and IP version type.
3877 3877 */
3878 3878 ill_t *
3879 3879 ill_lookup_on_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3880 3880 {
3881 3881 ill_t *ill;
3882 3882 phyint_t *phyi;
3883 3883
3884 3884 /*
3885 3885 * Indexes are stored in the phyint - a common structure
3886 3886 * to both IPv4 and IPv6.
3887 3887 */
3888 3888 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3889 3889 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3890 3890 (void *) &index, NULL);
3891 3891 if (phyi != NULL) {
3892 3892 ill = isv6 ? phyi->phyint_illv6: phyi->phyint_illv4;
3893 3893 if (ill != NULL) {
3894 3894 mutex_enter(&ill->ill_lock);
3895 3895 if (!ILL_IS_CONDEMNED(ill)) {
3896 3896 ill_refhold_locked(ill);
3897 3897 mutex_exit(&ill->ill_lock);
3898 3898 rw_exit(&ipst->ips_ill_g_lock);
3899 3899 return (ill);
3900 3900 }
3901 3901 mutex_exit(&ill->ill_lock);
3902 3902 }
3903 3903 }
3904 3904 rw_exit(&ipst->ips_ill_g_lock);
3905 3905 return (NULL);
3906 3906 }
3907 3907
3908 3908 /*
3909 3909 * Verify whether or not an interface index is valid for the specified zoneid
3910 3910 * to transmit packets.
3911 3911 * It can be zero (meaning "reset") or an interface index assigned
3912 3912 * to a non-VNI interface. (We don't use VNI interface to send packets.)
3913 3913 */
3914 3914 boolean_t
3915 3915 ip_xmit_ifindex_valid(uint_t ifindex, zoneid_t zoneid, boolean_t isv6,
3916 3916 ip_stack_t *ipst)
3917 3917 {
3918 3918 ill_t *ill;
3919 3919
3920 3920 if (ifindex == 0)
3921 3921 return (B_TRUE);
3922 3922
3923 3923 ill = ill_lookup_on_ifindex_zoneid(ifindex, zoneid, isv6, ipst);
3924 3924 if (ill == NULL)
3925 3925 return (B_FALSE);
3926 3926 if (IS_VNI(ill)) {
3927 3927 ill_refrele(ill);
3928 3928 return (B_FALSE);
3929 3929 }
3930 3930 ill_refrele(ill);
3931 3931 return (B_TRUE);
3932 3932 }
3933 3933
3934 3934 /*
3935 3935 * Return the ifindex next in sequence after the passed in ifindex.
3936 3936 * If there is no next ifindex for the given protocol, return 0.
3937 3937 */
3938 3938 uint_t
3939 3939 ill_get_next_ifindex(uint_t index, boolean_t isv6, ip_stack_t *ipst)
3940 3940 {
3941 3941 phyint_t *phyi;
3942 3942 phyint_t *phyi_initial;
3943 3943 uint_t ifindex;
3944 3944
3945 3945 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
3946 3946
3947 3947 if (index == 0) {
3948 3948 phyi = avl_first(
3949 3949 &ipst->ips_phyint_g_list->phyint_list_avl_by_index);
3950 3950 } else {
3951 3951 phyi = phyi_initial = avl_find(
3952 3952 &ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3953 3953 (void *) &index, NULL);
3954 3954 }
3955 3955
3956 3956 for (; phyi != NULL;
3957 3957 phyi = avl_walk(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
3958 3958 phyi, AVL_AFTER)) {
3959 3959 /*
3960 3960 * If we're not returning the first interface in the tree
3961 3961 * and we still haven't moved past the phyint_t that
3962 3962 * corresponds to index, avl_walk needs to be called again
3963 3963 */
3964 3964 if (!((index != 0) && (phyi == phyi_initial))) {
3965 3965 if (isv6) {
3966 3966 if ((phyi->phyint_illv6) &&
3967 3967 ILL_CAN_LOOKUP(phyi->phyint_illv6) &&
3968 3968 (phyi->phyint_illv6->ill_isv6 == 1))
3969 3969 break;
3970 3970 } else {
3971 3971 if ((phyi->phyint_illv4) &&
3972 3972 ILL_CAN_LOOKUP(phyi->phyint_illv4) &&
3973 3973 (phyi->phyint_illv4->ill_isv6 == 0))
3974 3974 break;
3975 3975 }
3976 3976 }
3977 3977 }
3978 3978
3979 3979 rw_exit(&ipst->ips_ill_g_lock);
3980 3980
3981 3981 if (phyi != NULL)
3982 3982 ifindex = phyi->phyint_ifindex;
3983 3983 else
3984 3984 ifindex = 0;
3985 3985
3986 3986 return (ifindex);
3987 3987 }
3988 3988
3989 3989 /*
3990 3990 * Return the ifindex for the named interface.
3991 3991 * If there is no next ifindex for the interface, return 0.
3992 3992 */
3993 3993 uint_t
3994 3994 ill_get_ifindex_by_name(char *name, ip_stack_t *ipst)
3995 3995 {
3996 3996 phyint_t *phyi;
3997 3997 avl_index_t where = 0;
3998 3998 uint_t ifindex;
3999 3999
4000 4000 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4001 4001
4002 4002 if ((phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
4003 4003 name, &where)) == NULL) {
4004 4004 rw_exit(&ipst->ips_ill_g_lock);
4005 4005 return (0);
4006 4006 }
4007 4007
4008 4008 ifindex = phyi->phyint_ifindex;
4009 4009
4010 4010 rw_exit(&ipst->ips_ill_g_lock);
4011 4011
4012 4012 return (ifindex);
4013 4013 }
4014 4014
4015 4015 /*
4016 4016 * Return the ifindex to be used by upper layer protocols for instance
4017 4017 * for IPV6_RECVPKTINFO. If IPMP this is the one for the upper ill.
4018 4018 */
4019 4019 uint_t
4020 4020 ill_get_upper_ifindex(const ill_t *ill)
4021 4021 {
4022 4022 if (IS_UNDER_IPMP(ill))
4023 4023 return (ipmp_ill_get_ipmp_ifindex(ill));
4024 4024 else
4025 4025 return (ill->ill_phyint->phyint_ifindex);
4026 4026 }
4027 4027
4028 4028
4029 4029 /*
4030 4030 * Obtain a reference to the ill. The ill_refcnt is a dynamic refcnt
4031 4031 * that gives a running thread a reference to the ill. This reference must be
4032 4032 * released by the thread when it is done accessing the ill and related
4033 4033 * objects. ill_refcnt can not be used to account for static references
4034 4034 * such as other structures pointing to an ill. Callers must generally
4035 4035 * check whether an ill can be refheld by using ILL_CAN_LOOKUP macros
4036 4036 * or be sure that the ill is not being deleted or changing state before
4037 4037 * calling the refhold functions. A non-zero ill_refcnt ensures that the
4038 4038 * ill won't change any of its critical state such as address, netmask etc.
4039 4039 */
4040 4040 void
4041 4041 ill_refhold(ill_t *ill)
4042 4042 {
4043 4043 mutex_enter(&ill->ill_lock);
4044 4044 ill->ill_refcnt++;
4045 4045 ILL_TRACE_REF(ill);
4046 4046 mutex_exit(&ill->ill_lock);
4047 4047 }
4048 4048
4049 4049 void
4050 4050 ill_refhold_locked(ill_t *ill)
4051 4051 {
4052 4052 ASSERT(MUTEX_HELD(&ill->ill_lock));
4053 4053 ill->ill_refcnt++;
4054 4054 ILL_TRACE_REF(ill);
4055 4055 }
4056 4056
4057 4057 /* Returns true if we managed to get a refhold */
4058 4058 boolean_t
4059 4059 ill_check_and_refhold(ill_t *ill)
4060 4060 {
4061 4061 mutex_enter(&ill->ill_lock);
4062 4062 if (!ILL_IS_CONDEMNED(ill)) {
4063 4063 ill_refhold_locked(ill);
4064 4064 mutex_exit(&ill->ill_lock);
4065 4065 return (B_TRUE);
4066 4066 }
4067 4067 mutex_exit(&ill->ill_lock);
4068 4068 return (B_FALSE);
4069 4069 }
4070 4070
4071 4071 /*
4072 4072 * Must not be called while holding any locks. Otherwise if this is
4073 4073 * the last reference to be released, there is a chance of recursive mutex
4074 4074 * panic due to ill_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
4075 4075 * to restart an ioctl.
4076 4076 */
4077 4077 void
4078 4078 ill_refrele(ill_t *ill)
4079 4079 {
4080 4080 mutex_enter(&ill->ill_lock);
4081 4081 ASSERT(ill->ill_refcnt != 0);
4082 4082 ill->ill_refcnt--;
4083 4083 ILL_UNTRACE_REF(ill);
4084 4084 if (ill->ill_refcnt != 0) {
4085 4085 /* Every ire pointing to the ill adds 1 to ill_refcnt */
4086 4086 mutex_exit(&ill->ill_lock);
4087 4087 return;
4088 4088 }
4089 4089
4090 4090 /* Drops the ill_lock */
4091 4091 ipif_ill_refrele_tail(ill);
4092 4092 }
4093 4093
4094 4094 /*
4095 4095 * Obtain a weak reference count on the ill. This reference ensures the
4096 4096 * ill won't be freed, but the ill may change any of its critical state
4097 4097 * such as netmask, address etc. Returns an error if the ill has started
4098 4098 * closing.
4099 4099 */
4100 4100 boolean_t
4101 4101 ill_waiter_inc(ill_t *ill)
4102 4102 {
4103 4103 mutex_enter(&ill->ill_lock);
4104 4104 if (ill->ill_state_flags & ILL_CONDEMNED) {
4105 4105 mutex_exit(&ill->ill_lock);
4106 4106 return (B_FALSE);
4107 4107 }
4108 4108 ill->ill_waiters++;
4109 4109 mutex_exit(&ill->ill_lock);
4110 4110 return (B_TRUE);
4111 4111 }
4112 4112
4113 4113 void
4114 4114 ill_waiter_dcr(ill_t *ill)
4115 4115 {
4116 4116 mutex_enter(&ill->ill_lock);
4117 4117 ill->ill_waiters--;
4118 4118 if (ill->ill_waiters == 0)
4119 4119 cv_broadcast(&ill->ill_cv);
4120 4120 mutex_exit(&ill->ill_lock);
4121 4121 }
4122 4122
4123 4123 /*
4124 4124 * ip_ll_subnet_defaults is called when we get the DL_INFO_ACK back from the
4125 4125 * driver. We construct best guess defaults for lower level information that
4126 4126 * we need. If an interface is brought up without injection of any overriding
4127 4127 * information from outside, we have to be ready to go with these defaults.
4128 4128 * When we get the first DL_INFO_ACK (from ip_open() sending a DL_INFO_REQ)
4129 4129 * we primarely want the dl_provider_style.
4130 4130 * The subsequent DL_INFO_ACK is received after doing a DL_ATTACH and DL_BIND
4131 4131 * at which point we assume the other part of the information is valid.
4132 4132 */
4133 4133 void
4134 4134 ip_ll_subnet_defaults(ill_t *ill, mblk_t *mp)
4135 4135 {
4136 4136 uchar_t *brdcst_addr;
4137 4137 uint_t brdcst_addr_length, phys_addr_length;
4138 4138 t_scalar_t sap_length;
4139 4139 dl_info_ack_t *dlia;
4140 4140 ip_m_t *ipm;
4141 4141 dl_qos_cl_sel1_t *sel1;
4142 4142 int min_mtu;
4143 4143
4144 4144 ASSERT(IAM_WRITER_ILL(ill));
4145 4145
4146 4146 /*
4147 4147 * Till the ill is fully up the ill is not globally visible.
4148 4148 * So no need for a lock.
4149 4149 */
4150 4150 dlia = (dl_info_ack_t *)mp->b_rptr;
4151 4151 ill->ill_mactype = dlia->dl_mac_type;
4152 4152
4153 4153 ipm = ip_m_lookup(dlia->dl_mac_type);
4154 4154 if (ipm == NULL) {
4155 4155 ipm = ip_m_lookup(DL_OTHER);
4156 4156 ASSERT(ipm != NULL);
4157 4157 }
4158 4158 ill->ill_media = ipm;
4159 4159
4160 4160 /*
4161 4161 * When the new DLPI stuff is ready we'll pull lengths
4162 4162 * from dlia.
4163 4163 */
4164 4164 if (dlia->dl_version == DL_VERSION_2) {
4165 4165 brdcst_addr_length = dlia->dl_brdcst_addr_length;
4166 4166 brdcst_addr = mi_offset_param(mp, dlia->dl_brdcst_addr_offset,
4167 4167 brdcst_addr_length);
4168 4168 if (brdcst_addr == NULL) {
4169 4169 brdcst_addr_length = 0;
4170 4170 }
4171 4171 sap_length = dlia->dl_sap_length;
4172 4172 phys_addr_length = dlia->dl_addr_length - ABS(sap_length);
4173 4173 ip1dbg(("ip: bcast_len %d, sap_len %d, phys_len %d\n",
4174 4174 brdcst_addr_length, sap_length, phys_addr_length));
4175 4175 } else {
4176 4176 brdcst_addr_length = 6;
4177 4177 brdcst_addr = ip_six_byte_all_ones;
4178 4178 sap_length = -2;
4179 4179 phys_addr_length = brdcst_addr_length;
4180 4180 }
4181 4181
4182 4182 ill->ill_bcast_addr_length = brdcst_addr_length;
4183 4183 ill->ill_phys_addr_length = phys_addr_length;
4184 4184 ill->ill_sap_length = sap_length;
4185 4185
4186 4186 /*
4187 4187 * Synthetic DLPI types such as SUNW_DL_IPMP specify a zero SDU,
4188 4188 * but we must ensure a minimum IP MTU is used since other bits of
4189 4189 * IP will fly apart otherwise.
4190 4190 */
4191 4191 min_mtu = ill->ill_isv6 ? IPV6_MIN_MTU : IP_MIN_MTU;
4192 4192 ill->ill_max_frag = MAX(min_mtu, dlia->dl_max_sdu);
4193 4193 ill->ill_current_frag = ill->ill_max_frag;
4194 4194 ill->ill_mtu = ill->ill_max_frag;
4195 4195 ill->ill_mc_mtu = ill->ill_mtu; /* Overridden by DL_NOTE_SDU_SIZE2 */
4196 4196
4197 4197 ill->ill_type = ipm->ip_m_type;
4198 4198
4199 4199 if (!ill->ill_dlpi_style_set) {
4200 4200 if (dlia->dl_provider_style == DL_STYLE2)
4201 4201 ill->ill_needs_attach = 1;
4202 4202
4203 4203 phyint_flags_init(ill->ill_phyint, ill->ill_mactype);
4204 4204
4205 4205 /*
4206 4206 * Allocate the first ipif on this ill. We don't delay it
4207 4207 * further as ioctl handling assumes at least one ipif exists.
4208 4208 *
4209 4209 * At this point we don't know whether the ill is v4 or v6.
4210 4210 * We will know this whan the SIOCSLIFNAME happens and
4211 4211 * the correct value for ill_isv6 will be assigned in
4212 4212 * ipif_set_values(). We need to hold the ill lock and
4213 4213 * clear the ILL_LL_SUBNET_PENDING flag and atomically do
4214 4214 * the wakeup.
4215 4215 */
4216 4216 (void) ipif_allocate(ill, 0, IRE_LOCAL,
4217 4217 dlia->dl_provider_style != DL_STYLE2, B_TRUE, NULL);
4218 4218 mutex_enter(&ill->ill_lock);
4219 4219 ASSERT(ill->ill_dlpi_style_set == 0);
4220 4220 ill->ill_dlpi_style_set = 1;
4221 4221 ill->ill_state_flags &= ~ILL_LL_SUBNET_PENDING;
4222 4222 cv_broadcast(&ill->ill_cv);
4223 4223 mutex_exit(&ill->ill_lock);
4224 4224 freemsg(mp);
4225 4225 return;
4226 4226 }
4227 4227 ASSERT(ill->ill_ipif != NULL);
4228 4228 /*
4229 4229 * We know whether it is IPv4 or IPv6 now, as this is the
4230 4230 * second DL_INFO_ACK we are recieving in response to the
4231 4231 * DL_INFO_REQ sent in ipif_set_values.
4232 4232 */
4233 4233 ill->ill_sap = (ill->ill_isv6) ? ipm->ip_m_ipv6sap : ipm->ip_m_ipv4sap;
4234 4234 /*
4235 4235 * Clear all the flags that were set based on ill_bcast_addr_length
4236 4236 * and ill_phys_addr_length (in ipif_set_values) as these could have
4237 4237 * changed now and we need to re-evaluate.
4238 4238 */
4239 4239 ill->ill_flags &= ~(ILLF_MULTICAST | ILLF_NONUD | ILLF_NOARP);
4240 4240 ill->ill_ipif->ipif_flags &= ~(IPIF_BROADCAST | IPIF_POINTOPOINT);
4241 4241
4242 4242 /*
4243 4243 * Free ill_bcast_mp as things could have changed now.
4244 4244 *
4245 4245 * NOTE: The IPMP meta-interface is special-cased because it starts
4246 4246 * with no underlying interfaces (and thus an unknown broadcast
4247 4247 * address length), but we enforce that an interface is broadcast-
4248 4248 * capable as part of allowing it to join a group.
4249 4249 */
4250 4250 if (ill->ill_bcast_addr_length == 0 && !IS_IPMP(ill)) {
4251 4251 if (ill->ill_bcast_mp != NULL)
4252 4252 freemsg(ill->ill_bcast_mp);
4253 4253 ill->ill_net_type = IRE_IF_NORESOLVER;
4254 4254
4255 4255 ill->ill_bcast_mp = ill_dlur_gen(NULL,
4256 4256 ill->ill_phys_addr_length,
4257 4257 ill->ill_sap,
4258 4258 ill->ill_sap_length);
4259 4259
4260 4260 if (ill->ill_isv6)
4261 4261 /*
4262 4262 * Note: xresolv interfaces will eventually need NOARP
4263 4263 * set here as well, but that will require those
4264 4264 * external resolvers to have some knowledge of
4265 4265 * that flag and act appropriately. Not to be changed
4266 4266 * at present.
4267 4267 */
4268 4268 ill->ill_flags |= ILLF_NONUD;
4269 4269 else
4270 4270 ill->ill_flags |= ILLF_NOARP;
4271 4271
4272 4272 if (ill->ill_mactype == SUNW_DL_VNI) {
4273 4273 ill->ill_ipif->ipif_flags |= IPIF_NOXMIT;
4274 4274 } else if (ill->ill_phys_addr_length == 0 ||
4275 4275 ill->ill_mactype == DL_IPV4 ||
4276 4276 ill->ill_mactype == DL_IPV6) {
4277 4277 /*
4278 4278 * The underying link is point-to-point, so mark the
4279 4279 * interface as such. We can do IP multicast over
4280 4280 * such a link since it transmits all network-layer
4281 4281 * packets to the remote side the same way.
4282 4282 */
4283 4283 ill->ill_flags |= ILLF_MULTICAST;
4284 4284 ill->ill_ipif->ipif_flags |= IPIF_POINTOPOINT;
4285 4285 }
4286 4286 } else {
4287 4287 ill->ill_net_type = IRE_IF_RESOLVER;
4288 4288 if (ill->ill_bcast_mp != NULL)
4289 4289 freemsg(ill->ill_bcast_mp);
4290 4290 ill->ill_bcast_mp = ill_dlur_gen(brdcst_addr,
4291 4291 ill->ill_bcast_addr_length, ill->ill_sap,
4292 4292 ill->ill_sap_length);
4293 4293 /*
4294 4294 * Later detect lack of DLPI driver multicast
4295 4295 * capability by catching DL_ENABMULTI errors in
4296 4296 * ip_rput_dlpi.
4297 4297 */
4298 4298 ill->ill_flags |= ILLF_MULTICAST;
4299 4299 if (!ill->ill_isv6)
4300 4300 ill->ill_ipif->ipif_flags |= IPIF_BROADCAST;
4301 4301 }
4302 4302
4303 4303 /* For IPMP, PHYI_IPMP should already be set by phyint_flags_init() */
4304 4304 if (ill->ill_mactype == SUNW_DL_IPMP)
4305 4305 ASSERT(ill->ill_phyint->phyint_flags & PHYI_IPMP);
4306 4306
4307 4307 /* By default an interface does not support any CoS marking */
4308 4308 ill->ill_flags &= ~ILLF_COS_ENABLED;
4309 4309
4310 4310 /*
4311 4311 * If we get QoS information in DL_INFO_ACK, the device supports
4312 4312 * some form of CoS marking, set ILLF_COS_ENABLED.
4313 4313 */
4314 4314 sel1 = (dl_qos_cl_sel1_t *)mi_offset_param(mp, dlia->dl_qos_offset,
4315 4315 dlia->dl_qos_length);
4316 4316 if ((sel1 != NULL) && (sel1->dl_qos_type == DL_QOS_CL_SEL1)) {
4317 4317 ill->ill_flags |= ILLF_COS_ENABLED;
4318 4318 }
4319 4319
4320 4320 /* Clear any previous error indication. */
4321 4321 ill->ill_error = 0;
4322 4322 freemsg(mp);
4323 4323 }
4324 4324
4325 4325 /*
4326 4326 * Perform various checks to verify that an address would make sense as a
4327 4327 * local, remote, or subnet interface address.
4328 4328 */
4329 4329 static boolean_t
4330 4330 ip_addr_ok_v4(ipaddr_t addr, ipaddr_t subnet_mask)
4331 4331 {
4332 4332 ipaddr_t net_mask;
4333 4333
4334 4334 /*
4335 4335 * Don't allow all zeroes, or all ones, but allow
4336 4336 * all ones netmask.
4337 4337 */
4338 4338 if ((net_mask = ip_net_mask(addr)) == 0)
4339 4339 return (B_FALSE);
4340 4340 /* A given netmask overrides the "guess" netmask */
4341 4341 if (subnet_mask != 0)
4342 4342 net_mask = subnet_mask;
4343 4343 if ((net_mask != ~(ipaddr_t)0) && ((addr == (addr & net_mask)) ||
4344 4344 (addr == (addr | ~net_mask)))) {
4345 4345 return (B_FALSE);
4346 4346 }
4347 4347
4348 4348 /*
4349 4349 * Even if the netmask is all ones, we do not allow address to be
4350 4350 * 255.255.255.255
4351 4351 */
4352 4352 if (addr == INADDR_BROADCAST)
4353 4353 return (B_FALSE);
4354 4354
4355 4355 if (CLASSD(addr))
4356 4356 return (B_FALSE);
4357 4357
4358 4358 return (B_TRUE);
4359 4359 }
4360 4360
4361 4361 #define V6_IPIF_LINKLOCAL(p) \
4362 4362 IN6_IS_ADDR_LINKLOCAL(&(p)->ipif_v6lcl_addr)
4363 4363
4364 4364 /*
4365 4365 * Compare two given ipifs and check if the second one is better than
4366 4366 * the first one using the order of preference (not taking deprecated
4367 4367 * into acount) specified in ipif_lookup_multicast().
4368 4368 */
4369 4369 static boolean_t
4370 4370 ipif_comp_multi(ipif_t *old_ipif, ipif_t *new_ipif, boolean_t isv6)
4371 4371 {
4372 4372 /* Check the least preferred first. */
4373 4373 if (IS_LOOPBACK(old_ipif->ipif_ill)) {
4374 4374 /* If both ipifs are the same, use the first one. */
4375 4375 if (IS_LOOPBACK(new_ipif->ipif_ill))
4376 4376 return (B_FALSE);
4377 4377 else
4378 4378 return (B_TRUE);
4379 4379 }
4380 4380
4381 4381 /* For IPv6, check for link local address. */
4382 4382 if (isv6 && V6_IPIF_LINKLOCAL(old_ipif)) {
4383 4383 if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4384 4384 V6_IPIF_LINKLOCAL(new_ipif)) {
4385 4385 /* The second one is equal or less preferred. */
4386 4386 return (B_FALSE);
4387 4387 } else {
4388 4388 return (B_TRUE);
4389 4389 }
4390 4390 }
4391 4391
4392 4392 /* Then check for point to point interface. */
4393 4393 if (old_ipif->ipif_flags & IPIF_POINTOPOINT) {
4394 4394 if (IS_LOOPBACK(new_ipif->ipif_ill) ||
4395 4395 (isv6 && V6_IPIF_LINKLOCAL(new_ipif)) ||
4396 4396 (new_ipif->ipif_flags & IPIF_POINTOPOINT)) {
4397 4397 return (B_FALSE);
4398 4398 } else {
4399 4399 return (B_TRUE);
4400 4400 }
4401 4401 }
4402 4402
4403 4403 /* old_ipif is a normal interface, so no need to use the new one. */
4404 4404 return (B_FALSE);
4405 4405 }
4406 4406
4407 4407 /*
4408 4408 * Find a mulitcast-capable ipif given an IP instance and zoneid.
4409 4409 * The ipif must be up, and its ill must multicast-capable, not
4410 4410 * condemned, not an underlying interface in an IPMP group, and
4411 4411 * not a VNI interface. Order of preference:
4412 4412 *
4413 4413 * 1a. normal
4414 4414 * 1b. normal, but deprecated
4415 4415 * 2a. point to point
4416 4416 * 2b. point to point, but deprecated
4417 4417 * 3a. link local
4418 4418 * 3b. link local, but deprecated
4419 4419 * 4. loopback.
4420 4420 */
4421 4421 static ipif_t *
4422 4422 ipif_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4423 4423 {
4424 4424 ill_t *ill;
4425 4425 ill_walk_context_t ctx;
4426 4426 ipif_t *ipif;
4427 4427 ipif_t *saved_ipif = NULL;
4428 4428 ipif_t *dep_ipif = NULL;
4429 4429
4430 4430 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4431 4431 if (isv6)
4432 4432 ill = ILL_START_WALK_V6(&ctx, ipst);
4433 4433 else
4434 4434 ill = ILL_START_WALK_V4(&ctx, ipst);
4435 4435
4436 4436 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4437 4437 mutex_enter(&ill->ill_lock);
4438 4438 if (IS_VNI(ill) || IS_UNDER_IPMP(ill) ||
4439 4439 ILL_IS_CONDEMNED(ill) ||
4440 4440 !(ill->ill_flags & ILLF_MULTICAST)) {
4441 4441 mutex_exit(&ill->ill_lock);
4442 4442 continue;
4443 4443 }
4444 4444 for (ipif = ill->ill_ipif; ipif != NULL;
4445 4445 ipif = ipif->ipif_next) {
4446 4446 if (zoneid != ipif->ipif_zoneid &&
4447 4447 zoneid != ALL_ZONES &&
4448 4448 ipif->ipif_zoneid != ALL_ZONES) {
4449 4449 continue;
4450 4450 }
4451 4451 if (!(ipif->ipif_flags & IPIF_UP) ||
4452 4452 IPIF_IS_CONDEMNED(ipif)) {
4453 4453 continue;
4454 4454 }
4455 4455
4456 4456 /*
4457 4457 * Found one candidate. If it is deprecated,
4458 4458 * remember it in dep_ipif. If it is not deprecated,
4459 4459 * remember it in saved_ipif.
4460 4460 */
4461 4461 if (ipif->ipif_flags & IPIF_DEPRECATED) {
4462 4462 if (dep_ipif == NULL) {
4463 4463 dep_ipif = ipif;
4464 4464 } else if (ipif_comp_multi(dep_ipif, ipif,
4465 4465 isv6)) {
4466 4466 /*
4467 4467 * If the previous dep_ipif does not
4468 4468 * belong to the same ill, we've done
4469 4469 * a ipif_refhold() on it. So we need
4470 4470 * to release it.
4471 4471 */
4472 4472 if (dep_ipif->ipif_ill != ill)
4473 4473 ipif_refrele(dep_ipif);
4474 4474 dep_ipif = ipif;
4475 4475 }
4476 4476 continue;
4477 4477 }
4478 4478 if (saved_ipif == NULL) {
4479 4479 saved_ipif = ipif;
4480 4480 } else {
4481 4481 if (ipif_comp_multi(saved_ipif, ipif, isv6)) {
4482 4482 if (saved_ipif->ipif_ill != ill)
4483 4483 ipif_refrele(saved_ipif);
4484 4484 saved_ipif = ipif;
4485 4485 }
4486 4486 }
4487 4487 }
4488 4488 /*
4489 4489 * Before going to the next ill, do a ipif_refhold() on the
4490 4490 * saved ones.
4491 4491 */
4492 4492 if (saved_ipif != NULL && saved_ipif->ipif_ill == ill)
4493 4493 ipif_refhold_locked(saved_ipif);
4494 4494 if (dep_ipif != NULL && dep_ipif->ipif_ill == ill)
4495 4495 ipif_refhold_locked(dep_ipif);
4496 4496 mutex_exit(&ill->ill_lock);
4497 4497 }
4498 4498 rw_exit(&ipst->ips_ill_g_lock);
4499 4499
4500 4500 /*
4501 4501 * If we have only the saved_ipif, return it. But if we have both
4502 4502 * saved_ipif and dep_ipif, check to see which one is better.
4503 4503 */
4504 4504 if (saved_ipif != NULL) {
4505 4505 if (dep_ipif != NULL) {
4506 4506 if (ipif_comp_multi(saved_ipif, dep_ipif, isv6)) {
4507 4507 ipif_refrele(saved_ipif);
4508 4508 return (dep_ipif);
4509 4509 } else {
4510 4510 ipif_refrele(dep_ipif);
4511 4511 return (saved_ipif);
4512 4512 }
4513 4513 }
4514 4514 return (saved_ipif);
4515 4515 } else {
4516 4516 return (dep_ipif);
4517 4517 }
4518 4518 }
4519 4519
4520 4520 ill_t *
4521 4521 ill_lookup_multicast(ip_stack_t *ipst, zoneid_t zoneid, boolean_t isv6)
4522 4522 {
4523 4523 ipif_t *ipif;
4524 4524 ill_t *ill;
4525 4525
4526 4526 ipif = ipif_lookup_multicast(ipst, zoneid, isv6);
4527 4527 if (ipif == NULL)
4528 4528 return (NULL);
4529 4529
4530 4530 ill = ipif->ipif_ill;
4531 4531 ill_refhold(ill);
4532 4532 ipif_refrele(ipif);
4533 4533 return (ill);
4534 4534 }
4535 4535
4536 4536 /*
4537 4537 * This function is called when an application does not specify an interface
4538 4538 * to be used for multicast traffic (joining a group/sending data). It
4539 4539 * calls ire_lookup_multi() to look for an interface route for the
4540 4540 * specified multicast group. Doing this allows the administrator to add
4541 4541 * prefix routes for multicast to indicate which interface to be used for
4542 4542 * multicast traffic in the above scenario. The route could be for all
4543 4543 * multicast (224.0/4), for a single multicast group (a /32 route) or
4544 4544 * anything in between. If there is no such multicast route, we just find
4545 4545 * any multicast capable interface and return it. The returned ipif
4546 4546 * is refhold'ed.
4547 4547 *
4548 4548 * We support MULTIRT and RTF_SETSRC on the multicast routes added to the
4549 4549 * unicast table. This is used by CGTP.
4550 4550 */
4551 4551 ill_t *
4552 4552 ill_lookup_group_v4(ipaddr_t group, zoneid_t zoneid, ip_stack_t *ipst,
4553 4553 boolean_t *multirtp, ipaddr_t *setsrcp)
4554 4554 {
4555 4555 ill_t *ill;
4556 4556
4557 4557 ill = ire_lookup_multi_ill_v4(group, zoneid, ipst, multirtp, setsrcp);
4558 4558 if (ill != NULL)
4559 4559 return (ill);
4560 4560
4561 4561 return (ill_lookup_multicast(ipst, zoneid, B_FALSE));
4562 4562 }
4563 4563
4564 4564 /*
4565 4565 * Look for an ipif with the specified interface address and destination.
4566 4566 * The destination address is used only for matching point-to-point interfaces.
4567 4567 */
4568 4568 ipif_t *
4569 4569 ipif_lookup_interface(ipaddr_t if_addr, ipaddr_t dst, ip_stack_t *ipst)
4570 4570 {
4571 4571 ipif_t *ipif;
4572 4572 ill_t *ill;
4573 4573 ill_walk_context_t ctx;
4574 4574
4575 4575 /*
4576 4576 * First match all the point-to-point interfaces
4577 4577 * before looking at non-point-to-point interfaces.
4578 4578 * This is done to avoid returning non-point-to-point
4579 4579 * ipif instead of unnumbered point-to-point ipif.
4580 4580 */
4581 4581 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4582 4582 ill = ILL_START_WALK_V4(&ctx, ipst);
4583 4583 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4584 4584 mutex_enter(&ill->ill_lock);
4585 4585 for (ipif = ill->ill_ipif; ipif != NULL;
4586 4586 ipif = ipif->ipif_next) {
4587 4587 /* Allow the ipif to be down */
4588 4588 if ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
4589 4589 (ipif->ipif_lcl_addr == if_addr) &&
4590 4590 (ipif->ipif_pp_dst_addr == dst)) {
4591 4591 if (!IPIF_IS_CONDEMNED(ipif)) {
4592 4592 ipif_refhold_locked(ipif);
4593 4593 mutex_exit(&ill->ill_lock);
4594 4594 rw_exit(&ipst->ips_ill_g_lock);
4595 4595 return (ipif);
4596 4596 }
4597 4597 }
4598 4598 }
4599 4599 mutex_exit(&ill->ill_lock);
4600 4600 }
4601 4601 rw_exit(&ipst->ips_ill_g_lock);
4602 4602
4603 4603 /* lookup the ipif based on interface address */
4604 4604 ipif = ipif_lookup_addr(if_addr, NULL, ALL_ZONES, ipst);
4605 4605 ASSERT(ipif == NULL || !ipif->ipif_isv6);
4606 4606 return (ipif);
4607 4607 }
4608 4608
4609 4609 /*
4610 4610 * Common function for ipif_lookup_addr() and ipif_lookup_addr_exact().
4611 4611 */
4612 4612 static ipif_t *
4613 4613 ipif_lookup_addr_common(ipaddr_t addr, ill_t *match_ill, uint32_t match_flags,
4614 4614 zoneid_t zoneid, ip_stack_t *ipst)
4615 4615 {
4616 4616 ipif_t *ipif;
4617 4617 ill_t *ill;
4618 4618 boolean_t ptp = B_FALSE;
4619 4619 ill_walk_context_t ctx;
4620 4620 boolean_t match_illgrp = (match_flags & IPIF_MATCH_ILLGRP);
4621 4621 boolean_t no_duplicate = (match_flags & IPIF_MATCH_NONDUP);
4622 4622
4623 4623 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4624 4624 /*
4625 4625 * Repeat twice, first based on local addresses and
4626 4626 * next time for pointopoint.
4627 4627 */
4628 4628 repeat:
4629 4629 ill = ILL_START_WALK_V4(&ctx, ipst);
4630 4630 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4631 4631 if (match_ill != NULL && ill != match_ill &&
4632 4632 (!match_illgrp || !IS_IN_SAME_ILLGRP(ill, match_ill))) {
4633 4633 continue;
4634 4634 }
4635 4635 mutex_enter(&ill->ill_lock);
4636 4636 for (ipif = ill->ill_ipif; ipif != NULL;
4637 4637 ipif = ipif->ipif_next) {
4638 4638 if (zoneid != ALL_ZONES &&
4639 4639 zoneid != ipif->ipif_zoneid &&
4640 4640 ipif->ipif_zoneid != ALL_ZONES)
4641 4641 continue;
4642 4642
4643 4643 if (no_duplicate && !(ipif->ipif_flags & IPIF_UP))
4644 4644 continue;
4645 4645
4646 4646 /* Allow the ipif to be down */
4647 4647 if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4648 4648 ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4649 4649 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4650 4650 (ipif->ipif_pp_dst_addr == addr))) {
4651 4651 if (!IPIF_IS_CONDEMNED(ipif)) {
4652 4652 ipif_refhold_locked(ipif);
4653 4653 mutex_exit(&ill->ill_lock);
4654 4654 rw_exit(&ipst->ips_ill_g_lock);
4655 4655 return (ipif);
4656 4656 }
4657 4657 }
4658 4658 }
4659 4659 mutex_exit(&ill->ill_lock);
4660 4660 }
4661 4661
4662 4662 /* If we already did the ptp case, then we are done */
4663 4663 if (ptp) {
4664 4664 rw_exit(&ipst->ips_ill_g_lock);
4665 4665 return (NULL);
4666 4666 }
4667 4667 ptp = B_TRUE;
4668 4668 goto repeat;
4669 4669 }
4670 4670
4671 4671 /*
4672 4672 * Lookup an ipif with the specified address. For point-to-point links we
4673 4673 * look for matches on either the destination address or the local address,
4674 4674 * but we skip the local address check if IPIF_UNNUMBERED is set. If the
4675 4675 * `match_ill' argument is non-NULL, the lookup is restricted to that ill
4676 4676 * (or illgrp if `match_ill' is in an IPMP group).
4677 4677 */
4678 4678 ipif_t *
4679 4679 ipif_lookup_addr(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4680 4680 ip_stack_t *ipst)
4681 4681 {
4682 4682 return (ipif_lookup_addr_common(addr, match_ill, IPIF_MATCH_ILLGRP,
4683 4683 zoneid, ipst));
4684 4684 }
4685 4685
4686 4686 /*
4687 4687 * Lookup an ipif with the specified address. Similar to ipif_lookup_addr,
4688 4688 * except that we will only return an address if it is not marked as
4689 4689 * IPIF_DUPLICATE
4690 4690 */
4691 4691 ipif_t *
4692 4692 ipif_lookup_addr_nondup(ipaddr_t addr, ill_t *match_ill, zoneid_t zoneid,
4693 4693 ip_stack_t *ipst)
4694 4694 {
4695 4695 return (ipif_lookup_addr_common(addr, match_ill,
4696 4696 (IPIF_MATCH_ILLGRP | IPIF_MATCH_NONDUP),
4697 4697 zoneid, ipst));
4698 4698 }
4699 4699
4700 4700 /*
4701 4701 * Special abbreviated version of ipif_lookup_addr() that doesn't match
4702 4702 * `match_ill' across the IPMP group. This function is only needed in some
4703 4703 * corner-cases; almost everything should use ipif_lookup_addr().
4704 4704 */
4705 4705 ipif_t *
4706 4706 ipif_lookup_addr_exact(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4707 4707 {
4708 4708 ASSERT(match_ill != NULL);
4709 4709 return (ipif_lookup_addr_common(addr, match_ill, 0, ALL_ZONES,
4710 4710 ipst));
4711 4711 }
4712 4712
4713 4713 /*
4714 4714 * Look for an ipif with the specified address. For point-point links
4715 4715 * we look for matches on either the destination address and the local
4716 4716 * address, but we ignore the check on the local address if IPIF_UNNUMBERED
4717 4717 * is set.
4718 4718 * If the `match_ill' argument is non-NULL, the lookup is restricted to that
4719 4719 * ill (or illgrp if `match_ill' is in an IPMP group).
4720 4720 * Return the zoneid for the ipif which matches. ALL_ZONES if no match.
4721 4721 */
4722 4722 zoneid_t
4723 4723 ipif_lookup_addr_zoneid(ipaddr_t addr, ill_t *match_ill, ip_stack_t *ipst)
4724 4724 {
4725 4725 zoneid_t zoneid;
4726 4726 ipif_t *ipif;
4727 4727 ill_t *ill;
4728 4728 boolean_t ptp = B_FALSE;
4729 4729 ill_walk_context_t ctx;
4730 4730
4731 4731 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
4732 4732 /*
4733 4733 * Repeat twice, first based on local addresses and
4734 4734 * next time for pointopoint.
4735 4735 */
4736 4736 repeat:
4737 4737 ill = ILL_START_WALK_V4(&ctx, ipst);
4738 4738 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
4739 4739 if (match_ill != NULL && ill != match_ill &&
4740 4740 !IS_IN_SAME_ILLGRP(ill, match_ill)) {
4741 4741 continue;
4742 4742 }
4743 4743 mutex_enter(&ill->ill_lock);
4744 4744 for (ipif = ill->ill_ipif; ipif != NULL;
4745 4745 ipif = ipif->ipif_next) {
4746 4746 /* Allow the ipif to be down */
4747 4747 if ((!ptp && (ipif->ipif_lcl_addr == addr) &&
4748 4748 ((ipif->ipif_flags & IPIF_UNNUMBERED) == 0)) ||
4749 4749 (ptp && (ipif->ipif_flags & IPIF_POINTOPOINT) &&
4750 4750 (ipif->ipif_pp_dst_addr == addr)) &&
4751 4751 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
4752 4752 zoneid = ipif->ipif_zoneid;
4753 4753 mutex_exit(&ill->ill_lock);
4754 4754 rw_exit(&ipst->ips_ill_g_lock);
4755 4755 /*
4756 4756 * If ipif_zoneid was ALL_ZONES then we have
4757 4757 * a trusted extensions shared IP address.
4758 4758 * In that case GLOBAL_ZONEID works to send.
4759 4759 */
4760 4760 if (zoneid == ALL_ZONES)
4761 4761 zoneid = GLOBAL_ZONEID;
4762 4762 return (zoneid);
4763 4763 }
4764 4764 }
4765 4765 mutex_exit(&ill->ill_lock);
4766 4766 }
4767 4767
4768 4768 /* If we already did the ptp case, then we are done */
4769 4769 if (ptp) {
4770 4770 rw_exit(&ipst->ips_ill_g_lock);
4771 4771 return (ALL_ZONES);
4772 4772 }
4773 4773 ptp = B_TRUE;
4774 4774 goto repeat;
4775 4775 }
4776 4776
4777 4777 /*
4778 4778 * Look for an ipif that matches the specified remote address i.e. the
4779 4779 * ipif that would receive the specified packet.
4780 4780 * First look for directly connected interfaces and then do a recursive
4781 4781 * IRE lookup and pick the first ipif corresponding to the source address in the
4782 4782 * ire.
4783 4783 * Returns: held ipif
4784 4784 *
4785 4785 * This is only used for ICMP_ADDRESS_MASK_REQUESTs
4786 4786 */
4787 4787 ipif_t *
4788 4788 ipif_lookup_remote(ill_t *ill, ipaddr_t addr, zoneid_t zoneid)
4789 4789 {
4790 4790 ipif_t *ipif;
4791 4791
4792 4792 ASSERT(!ill->ill_isv6);
4793 4793
4794 4794 /*
4795 4795 * Someone could be changing this ipif currently or change it
4796 4796 * after we return this. Thus a few packets could use the old
4797 4797 * old values. However structure updates/creates (ire, ilg, ilm etc)
4798 4798 * will atomically be updated or cleaned up with the new value
4799 4799 * Thus we don't need a lock to check the flags or other attrs below.
4800 4800 */
4801 4801 mutex_enter(&ill->ill_lock);
4802 4802 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4803 4803 if (IPIF_IS_CONDEMNED(ipif))
4804 4804 continue;
4805 4805 if (zoneid != ALL_ZONES && zoneid != ipif->ipif_zoneid &&
4806 4806 ipif->ipif_zoneid != ALL_ZONES)
4807 4807 continue;
4808 4808 /* Allow the ipif to be down */
4809 4809 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
4810 4810 if ((ipif->ipif_pp_dst_addr == addr) ||
4811 4811 (!(ipif->ipif_flags & IPIF_UNNUMBERED) &&
4812 4812 ipif->ipif_lcl_addr == addr)) {
4813 4813 ipif_refhold_locked(ipif);
4814 4814 mutex_exit(&ill->ill_lock);
4815 4815 return (ipif);
4816 4816 }
4817 4817 } else if (ipif->ipif_subnet == (addr & ipif->ipif_net_mask)) {
4818 4818 ipif_refhold_locked(ipif);
4819 4819 mutex_exit(&ill->ill_lock);
4820 4820 return (ipif);
4821 4821 }
4822 4822 }
4823 4823 mutex_exit(&ill->ill_lock);
4824 4824 /*
4825 4825 * For a remote destination it isn't possible to nail down a particular
4826 4826 * ipif.
4827 4827 */
4828 4828
4829 4829 /* Pick the first interface */
4830 4830 ipif = ipif_get_next_ipif(NULL, ill);
4831 4831 return (ipif);
4832 4832 }
4833 4833
4834 4834 /*
4835 4835 * This func does not prevent refcnt from increasing. But if
4836 4836 * the caller has taken steps to that effect, then this func
4837 4837 * can be used to determine whether the ill has become quiescent
4838 4838 */
4839 4839 static boolean_t
4840 4840 ill_is_quiescent(ill_t *ill)
4841 4841 {
4842 4842 ipif_t *ipif;
4843 4843
4844 4844 ASSERT(MUTEX_HELD(&ill->ill_lock));
4845 4845
4846 4846 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4847 4847 if (ipif->ipif_refcnt != 0)
4848 4848 return (B_FALSE);
4849 4849 }
4850 4850 if (!ILL_DOWN_OK(ill) || ill->ill_refcnt != 0) {
4851 4851 return (B_FALSE);
4852 4852 }
4853 4853 return (B_TRUE);
4854 4854 }
4855 4855
4856 4856 boolean_t
4857 4857 ill_is_freeable(ill_t *ill)
4858 4858 {
4859 4859 ipif_t *ipif;
4860 4860
4861 4861 ASSERT(MUTEX_HELD(&ill->ill_lock));
4862 4862
4863 4863 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
4864 4864 if (ipif->ipif_refcnt != 0) {
4865 4865 return (B_FALSE);
4866 4866 }
4867 4867 }
4868 4868 if (!ILL_FREE_OK(ill) || ill->ill_refcnt != 0) {
4869 4869 return (B_FALSE);
4870 4870 }
4871 4871 return (B_TRUE);
4872 4872 }
4873 4873
4874 4874 /*
4875 4875 * This func does not prevent refcnt from increasing. But if
4876 4876 * the caller has taken steps to that effect, then this func
4877 4877 * can be used to determine whether the ipif has become quiescent
4878 4878 */
4879 4879 static boolean_t
4880 4880 ipif_is_quiescent(ipif_t *ipif)
4881 4881 {
4882 4882 ill_t *ill;
4883 4883
4884 4884 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4885 4885
4886 4886 if (ipif->ipif_refcnt != 0)
4887 4887 return (B_FALSE);
4888 4888
4889 4889 ill = ipif->ipif_ill;
4890 4890 if (ill->ill_ipif_up_count != 0 || ill->ill_ipif_dup_count != 0 ||
4891 4891 ill->ill_logical_down) {
4892 4892 return (B_TRUE);
4893 4893 }
4894 4894
4895 4895 /* This is the last ipif going down or being deleted on this ill */
4896 4896 if (ill->ill_ire_cnt != 0 || ill->ill_refcnt != 0) {
4897 4897 return (B_FALSE);
4898 4898 }
4899 4899
4900 4900 return (B_TRUE);
4901 4901 }
4902 4902
4903 4903 /*
4904 4904 * return true if the ipif can be destroyed: the ipif has to be quiescent
4905 4905 * with zero references from ire/ilm to it.
4906 4906 */
4907 4907 static boolean_t
4908 4908 ipif_is_freeable(ipif_t *ipif)
4909 4909 {
4910 4910 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
4911 4911 ASSERT(ipif->ipif_id != 0);
4912 4912 return (ipif->ipif_refcnt == 0);
4913 4913 }
4914 4914
4915 4915 /*
4916 4916 * The ipif/ill/ire has been refreled. Do the tail processing.
4917 4917 * Determine if the ipif or ill in question has become quiescent and if so
4918 4918 * wakeup close and/or restart any queued pending ioctl that is waiting
4919 4919 * for the ipif_down (or ill_down)
4920 4920 */
4921 4921 void
4922 4922 ipif_ill_refrele_tail(ill_t *ill)
4923 4923 {
4924 4924 mblk_t *mp;
4925 4925 conn_t *connp;
4926 4926 ipsq_t *ipsq;
4927 4927 ipxop_t *ipx;
4928 4928 ipif_t *ipif;
4929 4929 dl_notify_ind_t *dlindp;
4930 4930
4931 4931 ASSERT(MUTEX_HELD(&ill->ill_lock));
4932 4932
4933 4933 if ((ill->ill_state_flags & ILL_CONDEMNED) && ill_is_freeable(ill)) {
4934 4934 /* ip_modclose() may be waiting */
4935 4935 cv_broadcast(&ill->ill_cv);
4936 4936 }
4937 4937
4938 4938 ipsq = ill->ill_phyint->phyint_ipsq;
4939 4939 mutex_enter(&ipsq->ipsq_lock);
4940 4940 ipx = ipsq->ipsq_xop;
4941 4941 mutex_enter(&ipx->ipx_lock);
4942 4942 if (ipx->ipx_waitfor == 0) /* no one's waiting; bail */
4943 4943 goto unlock;
4944 4944
4945 4945 ASSERT(ipx->ipx_pending_mp != NULL && ipx->ipx_pending_ipif != NULL);
4946 4946
4947 4947 ipif = ipx->ipx_pending_ipif;
4948 4948 if (ipif->ipif_ill != ill) /* wait is for another ill; bail */
4949 4949 goto unlock;
4950 4950
4951 4951 switch (ipx->ipx_waitfor) {
4952 4952 case IPIF_DOWN:
4953 4953 if (!ipif_is_quiescent(ipif))
4954 4954 goto unlock;
4955 4955 break;
4956 4956 case IPIF_FREE:
4957 4957 if (!ipif_is_freeable(ipif))
4958 4958 goto unlock;
4959 4959 break;
4960 4960 case ILL_DOWN:
4961 4961 if (!ill_is_quiescent(ill))
4962 4962 goto unlock;
4963 4963 break;
4964 4964 case ILL_FREE:
4965 4965 /*
4966 4966 * ILL_FREE is only for loopback; normal ill teardown waits
4967 4967 * synchronously in ip_modclose() without using ipx_waitfor,
4968 4968 * handled by the cv_broadcast() at the top of this function.
4969 4969 */
4970 4970 if (!ill_is_freeable(ill))
4971 4971 goto unlock;
4972 4972 break;
4973 4973 default:
4974 4974 cmn_err(CE_PANIC, "ipsq: %p unknown ipx_waitfor %d\n",
4975 4975 (void *)ipsq, ipx->ipx_waitfor);
4976 4976 }
4977 4977
4978 4978 ill_refhold_locked(ill); /* for qwriter_ip() call below */
4979 4979 mutex_exit(&ipx->ipx_lock);
4980 4980 mp = ipsq_pending_mp_get(ipsq, &connp);
4981 4981 mutex_exit(&ipsq->ipsq_lock);
4982 4982 mutex_exit(&ill->ill_lock);
4983 4983
4984 4984 ASSERT(mp != NULL);
4985 4985 /*
4986 4986 * NOTE: all of the qwriter_ip() calls below use CUR_OP since
4987 4987 * we can only get here when the current operation decides it
4988 4988 * it needs to quiesce via ipsq_pending_mp_add().
4989 4989 */
4990 4990 switch (mp->b_datap->db_type) {
4991 4991 case M_PCPROTO:
4992 4992 case M_PROTO:
4993 4993 /*
4994 4994 * For now, only DL_NOTIFY_IND messages can use this facility.
4995 4995 */
4996 4996 dlindp = (dl_notify_ind_t *)mp->b_rptr;
4997 4997 ASSERT(dlindp->dl_primitive == DL_NOTIFY_IND);
4998 4998
4999 4999 switch (dlindp->dl_notification) {
5000 5000 case DL_NOTE_PHYS_ADDR:
5001 5001 qwriter_ip(ill, ill->ill_rq, mp,
5002 5002 ill_set_phys_addr_tail, CUR_OP, B_TRUE);
5003 5003 return;
5004 5004 case DL_NOTE_REPLUMB:
5005 5005 qwriter_ip(ill, ill->ill_rq, mp,
5006 5006 ill_replumb_tail, CUR_OP, B_TRUE);
5007 5007 return;
5008 5008 default:
5009 5009 ASSERT(0);
5010 5010 ill_refrele(ill);
5011 5011 }
5012 5012 break;
5013 5013
5014 5014 case M_ERROR:
5015 5015 case M_HANGUP:
5016 5016 qwriter_ip(ill, ill->ill_rq, mp, ipif_all_down_tail, CUR_OP,
5017 5017 B_TRUE);
5018 5018 return;
5019 5019
5020 5020 case M_IOCTL:
5021 5021 case M_IOCDATA:
5022 5022 qwriter_ip(ill, (connp != NULL ? CONNP_TO_WQ(connp) :
5023 5023 ill->ill_wq), mp, ip_reprocess_ioctl, CUR_OP, B_TRUE);
5024 5024 return;
5025 5025
5026 5026 default:
5027 5027 cmn_err(CE_PANIC, "ipif_ill_refrele_tail mp %p "
5028 5028 "db_type %d\n", (void *)mp, mp->b_datap->db_type);
5029 5029 }
5030 5030 return;
5031 5031 unlock:
5032 5032 mutex_exit(&ipsq->ipsq_lock);
5033 5033 mutex_exit(&ipx->ipx_lock);
5034 5034 mutex_exit(&ill->ill_lock);
5035 5035 }
5036 5036
5037 5037 #ifdef DEBUG
5038 5038 /* Reuse trace buffer from beginning (if reached the end) and record trace */
5039 5039 static void
5040 5040 th_trace_rrecord(th_trace_t *th_trace)
5041 5041 {
5042 5042 tr_buf_t *tr_buf;
5043 5043 uint_t lastref;
5044 5044
5045 5045 lastref = th_trace->th_trace_lastref;
5046 5046 lastref++;
5047 5047 if (lastref == TR_BUF_MAX)
5048 5048 lastref = 0;
5049 5049 th_trace->th_trace_lastref = lastref;
5050 5050 tr_buf = &th_trace->th_trbuf[lastref];
5051 5051 tr_buf->tr_time = ddi_get_lbolt();
5052 5052 tr_buf->tr_depth = getpcstack(tr_buf->tr_stack, TR_STACK_DEPTH);
5053 5053 }
5054 5054
5055 5055 static void
5056 5056 th_trace_free(void *value)
5057 5057 {
5058 5058 th_trace_t *th_trace = value;
5059 5059
5060 5060 ASSERT(th_trace->th_refcnt == 0);
5061 5061 kmem_free(th_trace, sizeof (*th_trace));
5062 5062 }
5063 5063
5064 5064 /*
5065 5065 * Find or create the per-thread hash table used to track object references.
5066 5066 * The ipst argument is NULL if we shouldn't allocate.
5067 5067 *
5068 5068 * Accesses per-thread data, so there's no need to lock here.
5069 5069 */
5070 5070 static mod_hash_t *
5071 5071 th_trace_gethash(ip_stack_t *ipst)
5072 5072 {
5073 5073 th_hash_t *thh;
5074 5074
5075 5075 if ((thh = tsd_get(ip_thread_data)) == NULL && ipst != NULL) {
5076 5076 mod_hash_t *mh;
5077 5077 char name[256];
5078 5078 size_t objsize, rshift;
5079 5079 int retv;
5080 5080
5081 5081 if ((thh = kmem_alloc(sizeof (*thh), KM_NOSLEEP)) == NULL)
5082 5082 return (NULL);
5083 5083 (void) snprintf(name, sizeof (name), "th_trace_%p",
5084 5084 (void *)curthread);
5085 5085
5086 5086 /*
5087 5087 * We use mod_hash_create_extended here rather than the more
5088 5088 * obvious mod_hash_create_ptrhash because the latter has a
5089 5089 * hard-coded KM_SLEEP, and we'd prefer to fail rather than
5090 5090 * block.
5091 5091 */
5092 5092 objsize = MAX(MAX(sizeof (ill_t), sizeof (ipif_t)),
5093 5093 MAX(sizeof (ire_t), sizeof (ncec_t)));
5094 5094 rshift = highbit(objsize);
5095 5095 mh = mod_hash_create_extended(name, 64, mod_hash_null_keydtor,
5096 5096 th_trace_free, mod_hash_byptr, (void *)rshift,
5097 5097 mod_hash_ptrkey_cmp, KM_NOSLEEP);
5098 5098 if (mh == NULL) {
5099 5099 kmem_free(thh, sizeof (*thh));
5100 5100 return (NULL);
5101 5101 }
5102 5102 thh->thh_hash = mh;
5103 5103 thh->thh_ipst = ipst;
5104 5104 /*
5105 5105 * We trace ills, ipifs, ires, and nces. All of these are
5106 5106 * per-IP-stack, so the lock on the thread list is as well.
5107 5107 */
5108 5108 rw_enter(&ip_thread_rwlock, RW_WRITER);
5109 5109 list_insert_tail(&ip_thread_list, thh);
5110 5110 rw_exit(&ip_thread_rwlock);
5111 5111 retv = tsd_set(ip_thread_data, thh);
5112 5112 ASSERT(retv == 0);
5113 5113 }
5114 5114 return (thh != NULL ? thh->thh_hash : NULL);
5115 5115 }
5116 5116
5117 5117 boolean_t
5118 5118 th_trace_ref(const void *obj, ip_stack_t *ipst)
5119 5119 {
5120 5120 th_trace_t *th_trace;
5121 5121 mod_hash_t *mh;
5122 5122 mod_hash_val_t val;
5123 5123
5124 5124 if ((mh = th_trace_gethash(ipst)) == NULL)
5125 5125 return (B_FALSE);
5126 5126
5127 5127 /*
5128 5128 * Attempt to locate the trace buffer for this obj and thread.
5129 5129 * If it does not exist, then allocate a new trace buffer and
5130 5130 * insert into the hash.
5131 5131 */
5132 5132 if (mod_hash_find(mh, (mod_hash_key_t)obj, &val) == MH_ERR_NOTFOUND) {
5133 5133 th_trace = kmem_zalloc(sizeof (th_trace_t), KM_NOSLEEP);
5134 5134 if (th_trace == NULL)
5135 5135 return (B_FALSE);
5136 5136
5137 5137 th_trace->th_id = curthread;
5138 5138 if (mod_hash_insert(mh, (mod_hash_key_t)obj,
5139 5139 (mod_hash_val_t)th_trace) != 0) {
5140 5140 kmem_free(th_trace, sizeof (th_trace_t));
5141 5141 return (B_FALSE);
5142 5142 }
5143 5143 } else {
5144 5144 th_trace = (th_trace_t *)val;
5145 5145 }
5146 5146
5147 5147 ASSERT(th_trace->th_refcnt >= 0 &&
5148 5148 th_trace->th_refcnt < TR_BUF_MAX - 1);
5149 5149
5150 5150 th_trace->th_refcnt++;
5151 5151 th_trace_rrecord(th_trace);
5152 5152 return (B_TRUE);
5153 5153 }
5154 5154
5155 5155 /*
5156 5156 * For the purpose of tracing a reference release, we assume that global
5157 5157 * tracing is always on and that the same thread initiated the reference hold
5158 5158 * is releasing.
5159 5159 */
5160 5160 void
5161 5161 th_trace_unref(const void *obj)
5162 5162 {
5163 5163 int retv;
5164 5164 mod_hash_t *mh;
5165 5165 th_trace_t *th_trace;
5166 5166 mod_hash_val_t val;
5167 5167
5168 5168 mh = th_trace_gethash(NULL);
5169 5169 retv = mod_hash_find(mh, (mod_hash_key_t)obj, &val);
5170 5170 ASSERT(retv == 0);
5171 5171 th_trace = (th_trace_t *)val;
5172 5172
5173 5173 ASSERT(th_trace->th_refcnt > 0);
5174 5174 th_trace->th_refcnt--;
5175 5175 th_trace_rrecord(th_trace);
5176 5176 }
5177 5177
5178 5178 /*
5179 5179 * If tracing has been disabled, then we assume that the reference counts are
5180 5180 * now useless, and we clear them out before destroying the entries.
5181 5181 */
5182 5182 void
5183 5183 th_trace_cleanup(const void *obj, boolean_t trace_disable)
5184 5184 {
5185 5185 th_hash_t *thh;
5186 5186 mod_hash_t *mh;
5187 5187 mod_hash_val_t val;
5188 5188 th_trace_t *th_trace;
5189 5189 int retv;
5190 5190
5191 5191 rw_enter(&ip_thread_rwlock, RW_READER);
5192 5192 for (thh = list_head(&ip_thread_list); thh != NULL;
5193 5193 thh = list_next(&ip_thread_list, thh)) {
5194 5194 if (mod_hash_find(mh = thh->thh_hash, (mod_hash_key_t)obj,
5195 5195 &val) == 0) {
5196 5196 th_trace = (th_trace_t *)val;
5197 5197 if (trace_disable)
5198 5198 th_trace->th_refcnt = 0;
5199 5199 retv = mod_hash_destroy(mh, (mod_hash_key_t)obj);
5200 5200 ASSERT(retv == 0);
5201 5201 }
5202 5202 }
5203 5203 rw_exit(&ip_thread_rwlock);
5204 5204 }
5205 5205
5206 5206 void
5207 5207 ipif_trace_ref(ipif_t *ipif)
5208 5208 {
5209 5209 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5210 5210
5211 5211 if (ipif->ipif_trace_disable)
5212 5212 return;
5213 5213
5214 5214 if (!th_trace_ref(ipif, ipif->ipif_ill->ill_ipst)) {
5215 5215 ipif->ipif_trace_disable = B_TRUE;
5216 5216 ipif_trace_cleanup(ipif);
5217 5217 }
5218 5218 }
5219 5219
5220 5220 void
5221 5221 ipif_untrace_ref(ipif_t *ipif)
5222 5222 {
5223 5223 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5224 5224
5225 5225 if (!ipif->ipif_trace_disable)
5226 5226 th_trace_unref(ipif);
5227 5227 }
5228 5228
5229 5229 void
5230 5230 ill_trace_ref(ill_t *ill)
5231 5231 {
5232 5232 ASSERT(MUTEX_HELD(&ill->ill_lock));
5233 5233
5234 5234 if (ill->ill_trace_disable)
5235 5235 return;
5236 5236
5237 5237 if (!th_trace_ref(ill, ill->ill_ipst)) {
5238 5238 ill->ill_trace_disable = B_TRUE;
5239 5239 ill_trace_cleanup(ill);
5240 5240 }
5241 5241 }
5242 5242
5243 5243 void
5244 5244 ill_untrace_ref(ill_t *ill)
5245 5245 {
5246 5246 ASSERT(MUTEX_HELD(&ill->ill_lock));
5247 5247
5248 5248 if (!ill->ill_trace_disable)
5249 5249 th_trace_unref(ill);
5250 5250 }
5251 5251
5252 5252 /*
5253 5253 * Called when ipif is unplumbed or when memory alloc fails. Note that on
5254 5254 * failure, ipif_trace_disable is set.
5255 5255 */
5256 5256 static void
5257 5257 ipif_trace_cleanup(const ipif_t *ipif)
5258 5258 {
5259 5259 th_trace_cleanup(ipif, ipif->ipif_trace_disable);
5260 5260 }
5261 5261
5262 5262 /*
5263 5263 * Called when ill is unplumbed or when memory alloc fails. Note that on
5264 5264 * failure, ill_trace_disable is set.
5265 5265 */
5266 5266 static void
5267 5267 ill_trace_cleanup(const ill_t *ill)
5268 5268 {
5269 5269 th_trace_cleanup(ill, ill->ill_trace_disable);
5270 5270 }
5271 5271 #endif /* DEBUG */
5272 5272
5273 5273 void
5274 5274 ipif_refhold_locked(ipif_t *ipif)
5275 5275 {
5276 5276 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
5277 5277 ipif->ipif_refcnt++;
5278 5278 IPIF_TRACE_REF(ipif);
5279 5279 }
5280 5280
5281 5281 void
5282 5282 ipif_refhold(ipif_t *ipif)
5283 5283 {
5284 5284 ill_t *ill;
5285 5285
5286 5286 ill = ipif->ipif_ill;
5287 5287 mutex_enter(&ill->ill_lock);
5288 5288 ipif->ipif_refcnt++;
5289 5289 IPIF_TRACE_REF(ipif);
5290 5290 mutex_exit(&ill->ill_lock);
5291 5291 }
5292 5292
5293 5293 /*
5294 5294 * Must not be called while holding any locks. Otherwise if this is
5295 5295 * the last reference to be released there is a chance of recursive mutex
5296 5296 * panic due to ipif_refrele -> ipif_ill_refrele_tail -> qwriter_ip trying
5297 5297 * to restart an ioctl.
5298 5298 */
5299 5299 void
5300 5300 ipif_refrele(ipif_t *ipif)
5301 5301 {
5302 5302 ill_t *ill;
5303 5303
5304 5304 ill = ipif->ipif_ill;
5305 5305
5306 5306 mutex_enter(&ill->ill_lock);
5307 5307 ASSERT(ipif->ipif_refcnt != 0);
5308 5308 ipif->ipif_refcnt--;
5309 5309 IPIF_UNTRACE_REF(ipif);
5310 5310 if (ipif->ipif_refcnt != 0) {
5311 5311 mutex_exit(&ill->ill_lock);
5312 5312 return;
5313 5313 }
5314 5314
5315 5315 /* Drops the ill_lock */
5316 5316 ipif_ill_refrele_tail(ill);
5317 5317 }
5318 5318
5319 5319 ipif_t *
5320 5320 ipif_get_next_ipif(ipif_t *curr, ill_t *ill)
5321 5321 {
5322 5322 ipif_t *ipif;
5323 5323
5324 5324 mutex_enter(&ill->ill_lock);
5325 5325 for (ipif = (curr == NULL ? ill->ill_ipif : curr->ipif_next);
5326 5326 ipif != NULL; ipif = ipif->ipif_next) {
5327 5327 if (IPIF_IS_CONDEMNED(ipif))
5328 5328 continue;
5329 5329 ipif_refhold_locked(ipif);
5330 5330 mutex_exit(&ill->ill_lock);
5331 5331 return (ipif);
5332 5332 }
5333 5333 mutex_exit(&ill->ill_lock);
5334 5334 return (NULL);
5335 5335 }
5336 5336
5337 5337 /*
5338 5338 * TODO: make this table extendible at run time
5339 5339 * Return a pointer to the mac type info for 'mac_type'
5340 5340 */
5341 5341 static ip_m_t *
5342 5342 ip_m_lookup(t_uscalar_t mac_type)
5343 5343 {
5344 5344 ip_m_t *ipm;
5345 5345
5346 5346 for (ipm = ip_m_tbl; ipm < A_END(ip_m_tbl); ipm++)
5347 5347 if (ipm->ip_m_mac_type == mac_type)
5348 5348 return (ipm);
5349 5349 return (NULL);
5350 5350 }
5351 5351
5352 5352 /*
5353 5353 * Make a link layer address from the multicast IP address *addr.
5354 5354 * To form the link layer address, invoke the ip_m_v*mapping function
5355 5355 * associated with the link-layer type.
5356 5356 */
5357 5357 void
5358 5358 ip_mcast_mapping(ill_t *ill, uchar_t *addr, uchar_t *hwaddr)
5359 5359 {
5360 5360 ip_m_t *ipm;
5361 5361
5362 5362 if (ill->ill_net_type == IRE_IF_NORESOLVER)
5363 5363 return;
5364 5364
5365 5365 ASSERT(addr != NULL);
5366 5366
5367 5367 ipm = ip_m_lookup(ill->ill_mactype);
5368 5368 if (ipm == NULL ||
5369 5369 (ill->ill_isv6 && ipm->ip_m_v6mapping == NULL) ||
5370 5370 (!ill->ill_isv6 && ipm->ip_m_v4mapping == NULL)) {
5371 5371 ip0dbg(("no mapping for ill %s mactype 0x%x\n",
5372 5372 ill->ill_name, ill->ill_mactype));
5373 5373 return;
5374 5374 }
5375 5375 if (ill->ill_isv6)
5376 5376 (*ipm->ip_m_v6mapping)(ill, addr, hwaddr);
5377 5377 else
5378 5378 (*ipm->ip_m_v4mapping)(ill, addr, hwaddr);
5379 5379 }
5380 5380
5381 5381 /*
5382 5382 * Returns B_FALSE if the IPv4 netmask pointed by `mask' is non-contiguous.
5383 5383 * Otherwise returns B_TRUE.
5384 5384 *
5385 5385 * The netmask can be verified to be contiguous with 32 shifts and or
5386 5386 * operations. Take the contiguous mask (in host byte order) and compute
5387 5387 * mask | mask << 1 | mask << 2 | ... | mask << 31
5388 5388 * the result will be the same as the 'mask' for contiguous mask.
5389 5389 */
5390 5390 static boolean_t
5391 5391 ip_contiguous_mask(uint32_t mask)
5392 5392 {
5393 5393 uint32_t m = mask;
5394 5394 int i;
5395 5395
5396 5396 for (i = 1; i < 32; i++)
5397 5397 m |= (mask << i);
5398 5398
5399 5399 return (m == mask);
5400 5400 }
5401 5401
5402 5402 /*
5403 5403 * ip_rt_add is called to add an IPv4 route to the forwarding table.
5404 5404 * ill is passed in to associate it with the correct interface.
5405 5405 * If ire_arg is set, then we return the held IRE in that location.
5406 5406 */
5407 5407 int
5408 5408 ip_rt_add(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5409 5409 ipaddr_t src_addr, int flags, ill_t *ill, ire_t **ire_arg,
5410 5410 boolean_t ioctl_msg, struct rtsa_s *sp, ip_stack_t *ipst, zoneid_t zoneid)
5411 5411 {
5412 5412 ire_t *ire, *nire;
5413 5413 ire_t *gw_ire = NULL;
5414 5414 ipif_t *ipif = NULL;
5415 5415 uint_t type;
5416 5416 int match_flags = MATCH_IRE_TYPE;
5417 5417 tsol_gc_t *gc = NULL;
5418 5418 tsol_gcgrp_t *gcgrp = NULL;
5419 5419 boolean_t gcgrp_xtraref = B_FALSE;
5420 5420 boolean_t cgtp_broadcast;
5421 5421 boolean_t unbound = B_FALSE;
5422 5422
5423 5423 ip1dbg(("ip_rt_add:"));
5424 5424
5425 5425 if (ire_arg != NULL)
5426 5426 *ire_arg = NULL;
5427 5427
5428 5428 /* disallow non-contiguous netmasks */
5429 5429 if (!ip_contiguous_mask(ntohl(mask)))
5430 5430 return (ENOTSUP);
5431 5431
5432 5432 /*
5433 5433 * If this is the case of RTF_HOST being set, then we set the netmask
5434 5434 * to all ones (regardless if one was supplied).
5435 5435 */
5436 5436 if (flags & RTF_HOST)
5437 5437 mask = IP_HOST_MASK;
5438 5438
5439 5439 /*
5440 5440 * Prevent routes with a zero gateway from being created (since
5441 5441 * interfaces can currently be plumbed and brought up no assigned
5442 5442 * address).
5443 5443 */
5444 5444 if (gw_addr == 0)
5445 5445 return (ENETUNREACH);
5446 5446 /*
5447 5447 * Get the ipif, if any, corresponding to the gw_addr
5448 5448 * If -ifp was specified we restrict ourselves to the ill, otherwise
5449 5449 * we match on the gatway and destination to handle unnumbered pt-pt
5450 5450 * interfaces.
5451 5451 */
5452 5452 if (ill != NULL)
5453 5453 ipif = ipif_lookup_addr(gw_addr, ill, ALL_ZONES, ipst);
5454 5454 else
5455 5455 ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
5456 5456 if (ipif != NULL) {
5457 5457 if (IS_VNI(ipif->ipif_ill)) {
5458 5458 ipif_refrele(ipif);
5459 5459 return (EINVAL);
5460 5460 }
5461 5461 }
5462 5462
5463 5463 /*
5464 5464 * GateD will attempt to create routes with a loopback interface
5465 5465 * address as the gateway and with RTF_GATEWAY set. We allow
5466 5466 * these routes to be added, but create them as interface routes
5467 5467 * since the gateway is an interface address.
5468 5468 */
5469 5469 if ((ipif != NULL) && (ipif->ipif_ire_type == IRE_LOOPBACK)) {
5470 5470 flags &= ~RTF_GATEWAY;
5471 5471 if (gw_addr == INADDR_LOOPBACK && dst_addr == INADDR_LOOPBACK &&
5472 5472 mask == IP_HOST_MASK) {
5473 5473 ire = ire_ftable_lookup_v4(dst_addr, 0, 0, IRE_LOOPBACK,
5474 5474 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
5475 5475 NULL);
5476 5476 if (ire != NULL) {
5477 5477 ire_refrele(ire);
5478 5478 ipif_refrele(ipif);
5479 5479 return (EEXIST);
5480 5480 }
5481 5481 ip1dbg(("ip_rt_add: 0x%p creating IRE 0x%x"
5482 5482 "for 0x%x\n", (void *)ipif,
5483 5483 ipif->ipif_ire_type,
5484 5484 ntohl(ipif->ipif_lcl_addr)));
5485 5485 ire = ire_create(
5486 5486 (uchar_t *)&dst_addr, /* dest address */
5487 5487 (uchar_t *)&mask, /* mask */
5488 5488 NULL, /* no gateway */
5489 5489 ipif->ipif_ire_type, /* LOOPBACK */
5490 5490 ipif->ipif_ill,
5491 5491 zoneid,
5492 5492 (ipif->ipif_flags & IPIF_PRIVATE) ? RTF_PRIVATE : 0,
5493 5493 NULL,
5494 5494 ipst);
5495 5495
5496 5496 if (ire == NULL) {
5497 5497 ipif_refrele(ipif);
5498 5498 return (ENOMEM);
5499 5499 }
5500 5500 /* src address assigned by the caller? */
5501 5501 if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5502 5502 ire->ire_setsrc_addr = src_addr;
5503 5503
5504 5504 nire = ire_add(ire);
5505 5505 if (nire == NULL) {
5506 5506 /*
5507 5507 * In the result of failure, ire_add() will have
5508 5508 * already deleted the ire in question, so there
5509 5509 * is no need to do that here.
5510 5510 */
5511 5511 ipif_refrele(ipif);
5512 5512 return (ENOMEM);
5513 5513 }
5514 5514 /*
5515 5515 * Check if it was a duplicate entry. This handles
5516 5516 * the case of two racing route adds for the same route
5517 5517 */
5518 5518 if (nire != ire) {
5519 5519 ASSERT(nire->ire_identical_ref > 1);
5520 5520 ire_delete(nire);
5521 5521 ire_refrele(nire);
5522 5522 ipif_refrele(ipif);
5523 5523 return (EEXIST);
5524 5524 }
5525 5525 ire = nire;
5526 5526 goto save_ire;
5527 5527 }
5528 5528 }
5529 5529
5530 5530 /*
5531 5531 * The routes for multicast with CGTP are quite special in that
5532 5532 * the gateway is the local interface address, yet RTF_GATEWAY
5533 5533 * is set. We turn off RTF_GATEWAY to provide compatibility with
5534 5534 * this undocumented and unusual use of multicast routes.
5535 5535 */
5536 5536 if ((flags & RTF_MULTIRT) && ipif != NULL)
5537 5537 flags &= ~RTF_GATEWAY;
5538 5538
5539 5539 /*
5540 5540 * Traditionally, interface routes are ones where RTF_GATEWAY isn't set
5541 5541 * and the gateway address provided is one of the system's interface
5542 5542 * addresses. By using the routing socket interface and supplying an
5543 5543 * RTA_IFP sockaddr with an interface index, an alternate method of
5544 5544 * specifying an interface route to be created is available which uses
5545 5545 * the interface index that specifies the outgoing interface rather than
5546 5546 * the address of an outgoing interface (which may not be able to
5547 5547 * uniquely identify an interface). When coupled with the RTF_GATEWAY
5548 5548 * flag, routes can be specified which not only specify the next-hop to
5549 5549 * be used when routing to a certain prefix, but also which outgoing
5550 5550 * interface should be used.
5551 5551 *
5552 5552 * Previously, interfaces would have unique addresses assigned to them
5553 5553 * and so the address assigned to a particular interface could be used
5554 5554 * to identify a particular interface. One exception to this was the
5555 5555 * case of an unnumbered interface (where IPIF_UNNUMBERED was set).
5556 5556 *
5557 5557 * With the advent of IPv6 and its link-local addresses, this
5558 5558 * restriction was relaxed and interfaces could share addresses between
5559 5559 * themselves. In fact, typically all of the link-local interfaces on
5560 5560 * an IPv6 node or router will have the same link-local address. In
5561 5561 * order to differentiate between these interfaces, the use of an
5562 5562 * interface index is necessary and this index can be carried inside a
5563 5563 * RTA_IFP sockaddr (which is actually a sockaddr_dl). One restriction
5564 5564 * of using the interface index, however, is that all of the ipif's that
5565 5565 * are part of an ill have the same index and so the RTA_IFP sockaddr
5566 5566 * cannot be used to differentiate between ipif's (or logical
5567 5567 * interfaces) that belong to the same ill (physical interface).
5568 5568 *
5569 5569 * For example, in the following case involving IPv4 interfaces and
5570 5570 * logical interfaces
5571 5571 *
5572 5572 * 192.0.2.32 255.255.255.224 192.0.2.33 U if0
5573 5573 * 192.0.2.32 255.255.255.224 192.0.2.34 U if0
5574 5574 * 192.0.2.32 255.255.255.224 192.0.2.35 U if0
5575 5575 *
5576 5576 * the ipif's corresponding to each of these interface routes can be
5577 5577 * uniquely identified by the "gateway" (actually interface address).
5578 5578 *
5579 5579 * In this case involving multiple IPv6 default routes to a particular
5580 5580 * link-local gateway, the use of RTA_IFP is necessary to specify which
5581 5581 * default route is of interest:
5582 5582 *
5583 5583 * default fe80::123:4567:89ab:cdef U if0
5584 5584 * default fe80::123:4567:89ab:cdef U if1
5585 5585 */
5586 5586
5587 5587 /* RTF_GATEWAY not set */
5588 5588 if (!(flags & RTF_GATEWAY)) {
5589 5589 if (sp != NULL) {
5590 5590 ip2dbg(("ip_rt_add: gateway security attributes "
5591 5591 "cannot be set with interface route\n"));
5592 5592 if (ipif != NULL)
5593 5593 ipif_refrele(ipif);
5594 5594 return (EINVAL);
5595 5595 }
5596 5596
5597 5597 /*
5598 5598 * Whether or not ill (RTA_IFP) is set, we require that
5599 5599 * the gateway is one of our local addresses.
5600 5600 */
5601 5601 if (ipif == NULL)
5602 5602 return (ENETUNREACH);
5603 5603
5604 5604 /*
5605 5605 * We use MATCH_IRE_ILL here. If the caller specified an
5606 5606 * interface (from the RTA_IFP sockaddr) we use it, otherwise
5607 5607 * we use the ill derived from the gateway address.
5608 5608 * We can always match the gateway address since we record it
5609 5609 * in ire_gateway_addr.
5610 5610 * We don't allow RTA_IFP to specify a different ill than the
5611 5611 * one matching the ipif to make sure we can delete the route.
5612 5612 */
5613 5613 match_flags |= MATCH_IRE_GW | MATCH_IRE_ILL;
5614 5614 if (ill == NULL) {
5615 5615 ill = ipif->ipif_ill;
5616 5616 } else if (ill != ipif->ipif_ill) {
5617 5617 ipif_refrele(ipif);
5618 5618 return (EINVAL);
5619 5619 }
5620 5620
5621 5621 /*
5622 5622 * We check for an existing entry at this point.
5623 5623 *
5624 5624 * Since a netmask isn't passed in via the ioctl interface
5625 5625 * (SIOCADDRT), we don't check for a matching netmask in that
5626 5626 * case.
5627 5627 */
5628 5628 if (!ioctl_msg)
5629 5629 match_flags |= MATCH_IRE_MASK;
5630 5630 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
5631 5631 IRE_INTERFACE, ill, ALL_ZONES, NULL, match_flags, 0, ipst,
5632 5632 NULL);
5633 5633 if (ire != NULL) {
5634 5634 ire_refrele(ire);
5635 5635 ipif_refrele(ipif);
5636 5636 return (EEXIST);
5637 5637 }
5638 5638
5639 5639 /*
5640 5640 * Some software (for example, GateD and Sun Cluster) attempts
5641 5641 * to create (what amount to) IRE_PREFIX routes with the
5642 5642 * loopback address as the gateway. This is primarily done to
5643 5643 * set up prefixes with the RTF_REJECT flag set (for example,
5644 5644 * when generating aggregate routes.)
5645 5645 *
5646 5646 * If the IRE type (as defined by ill->ill_net_type) would be
5647 5647 * IRE_LOOPBACK, then we map the request into a
5648 5648 * IRE_IF_NORESOLVER. We also OR in the RTF_BLACKHOLE flag as
5649 5649 * these interface routes, by definition, can only be that.
5650 5650 *
5651 5651 * Needless to say, the real IRE_LOOPBACK is NOT created by this
5652 5652 * routine, but rather using ire_create() directly.
5653 5653 *
5654 5654 */
5655 5655 type = ill->ill_net_type;
5656 5656 if (type == IRE_LOOPBACK) {
5657 5657 type = IRE_IF_NORESOLVER;
5658 5658 flags |= RTF_BLACKHOLE;
5659 5659 }
5660 5660
5661 5661 /*
5662 5662 * Create a copy of the IRE_IF_NORESOLVER or
5663 5663 * IRE_IF_RESOLVER with the modified address, netmask, and
5664 5664 * gateway.
5665 5665 */
5666 5666 ire = ire_create(
5667 5667 (uchar_t *)&dst_addr,
5668 5668 (uint8_t *)&mask,
5669 5669 (uint8_t *)&gw_addr,
5670 5670 type,
5671 5671 ill,
5672 5672 zoneid,
5673 5673 flags,
5674 5674 NULL,
5675 5675 ipst);
5676 5676 if (ire == NULL) {
5677 5677 ipif_refrele(ipif);
5678 5678 return (ENOMEM);
5679 5679 }
5680 5680
5681 5681 /* src address assigned by the caller? */
5682 5682 if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5683 5683 ire->ire_setsrc_addr = src_addr;
5684 5684
5685 5685 nire = ire_add(ire);
5686 5686 if (nire == NULL) {
5687 5687 /*
5688 5688 * In the result of failure, ire_add() will have
5689 5689 * already deleted the ire in question, so there
5690 5690 * is no need to do that here.
5691 5691 */
5692 5692 ipif_refrele(ipif);
5693 5693 return (ENOMEM);
5694 5694 }
5695 5695 /*
5696 5696 * Check if it was a duplicate entry. This handles
5697 5697 * the case of two racing route adds for the same route
5698 5698 */
5699 5699 if (nire != ire) {
5700 5700 ire_delete(nire);
5701 5701 ire_refrele(nire);
5702 5702 ipif_refrele(ipif);
5703 5703 return (EEXIST);
5704 5704 }
5705 5705 ire = nire;
5706 5706 goto save_ire;
5707 5707 }
5708 5708
5709 5709 /*
5710 5710 * Get an interface IRE for the specified gateway.
5711 5711 * If we don't have an IRE_IF_NORESOLVER or IRE_IF_RESOLVER for the
5712 5712 * gateway, it is currently unreachable and we fail the request
5713 5713 * accordingly. We reject any RTF_GATEWAY routes where the gateway
5714 5714 * is an IRE_LOCAL or IRE_LOOPBACK.
5715 5715 * If RTA_IFP was specified we look on that particular ill.
5716 5716 */
5717 5717 if (ill != NULL)
5718 5718 match_flags |= MATCH_IRE_ILL;
5719 5719
5720 5720 /* Check whether the gateway is reachable. */
5721 5721 again:
5722 5722 type = IRE_INTERFACE | IRE_LOCAL | IRE_LOOPBACK;
5723 5723 if (flags & RTF_INDIRECT)
5724 5724 type |= IRE_OFFLINK;
5725 5725
5726 5726 gw_ire = ire_ftable_lookup_v4(gw_addr, 0, 0, type, ill,
5727 5727 ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
5728 5728 if (gw_ire == NULL) {
5729 5729 /*
5730 5730 * With IPMP, we allow host routes to influence in.mpathd's
5731 5731 * target selection. However, if the test addresses are on
5732 5732 * their own network, the above lookup will fail since the
5733 5733 * underlying IRE_INTERFACEs are marked hidden. So allow
5734 5734 * hidden test IREs to be found and try again.
5735 5735 */
5736 5736 if (!(match_flags & MATCH_IRE_TESTHIDDEN)) {
5737 5737 match_flags |= MATCH_IRE_TESTHIDDEN;
5738 5738 goto again;
5739 5739 }
5740 5740 if (ipif != NULL)
5741 5741 ipif_refrele(ipif);
5742 5742 return (ENETUNREACH);
5743 5743 }
5744 5744 if (gw_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
5745 5745 ire_refrele(gw_ire);
5746 5746 if (ipif != NULL)
5747 5747 ipif_refrele(ipif);
5748 5748 return (ENETUNREACH);
5749 5749 }
5750 5750
5751 5751 if (ill == NULL && !(flags & RTF_INDIRECT)) {
5752 5752 unbound = B_TRUE;
5753 5753 if (ipst->ips_ip_strict_src_multihoming > 0)
5754 5754 ill = gw_ire->ire_ill;
5755 5755 }
5756 5756
5757 5757 /*
5758 5758 * We create one of three types of IREs as a result of this request
5759 5759 * based on the netmask. A netmask of all ones (which is automatically
5760 5760 * assumed when RTF_HOST is set) results in an IRE_HOST being created.
5761 5761 * An all zeroes netmask implies a default route so an IRE_DEFAULT is
5762 5762 * created. Otherwise, an IRE_PREFIX route is created for the
5763 5763 * destination prefix.
5764 5764 */
5765 5765 if (mask == IP_HOST_MASK)
5766 5766 type = IRE_HOST;
5767 5767 else if (mask == 0)
5768 5768 type = IRE_DEFAULT;
5769 5769 else
5770 5770 type = IRE_PREFIX;
5771 5771
5772 5772 /* check for a duplicate entry */
5773 5773 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
5774 5774 ALL_ZONES, NULL, match_flags | MATCH_IRE_MASK | MATCH_IRE_GW,
5775 5775 0, ipst, NULL);
5776 5776 if (ire != NULL) {
5777 5777 if (ipif != NULL)
5778 5778 ipif_refrele(ipif);
5779 5779 ire_refrele(gw_ire);
5780 5780 ire_refrele(ire);
5781 5781 return (EEXIST);
5782 5782 }
5783 5783
5784 5784 /* Security attribute exists */
5785 5785 if (sp != NULL) {
5786 5786 tsol_gcgrp_addr_t ga;
5787 5787
5788 5788 /* find or create the gateway credentials group */
5789 5789 ga.ga_af = AF_INET;
5790 5790 IN6_IPADDR_TO_V4MAPPED(gw_addr, &ga.ga_addr);
5791 5791
5792 5792 /* we hold reference to it upon success */
5793 5793 gcgrp = gcgrp_lookup(&ga, B_TRUE);
5794 5794 if (gcgrp == NULL) {
5795 5795 if (ipif != NULL)
5796 5796 ipif_refrele(ipif);
5797 5797 ire_refrele(gw_ire);
5798 5798 return (ENOMEM);
5799 5799 }
5800 5800
5801 5801 /*
5802 5802 * Create and add the security attribute to the group; a
5803 5803 * reference to the group is made upon allocating a new
5804 5804 * entry successfully. If it finds an already-existing
5805 5805 * entry for the security attribute in the group, it simply
5806 5806 * returns it and no new reference is made to the group.
5807 5807 */
5808 5808 gc = gc_create(sp, gcgrp, &gcgrp_xtraref);
5809 5809 if (gc == NULL) {
5810 5810 if (ipif != NULL)
5811 5811 ipif_refrele(ipif);
5812 5812 /* release reference held by gcgrp_lookup */
5813 5813 GCGRP_REFRELE(gcgrp);
5814 5814 ire_refrele(gw_ire);
5815 5815 return (ENOMEM);
5816 5816 }
5817 5817 }
5818 5818
5819 5819 /* Create the IRE. */
5820 5820 ire = ire_create(
5821 5821 (uchar_t *)&dst_addr, /* dest address */
5822 5822 (uchar_t *)&mask, /* mask */
5823 5823 (uchar_t *)&gw_addr, /* gateway address */
5824 5824 (ushort_t)type, /* IRE type */
5825 5825 ill,
5826 5826 zoneid,
5827 5827 flags,
5828 5828 gc, /* security attribute */
5829 5829 ipst);
5830 5830
5831 5831 /*
5832 5832 * The ire holds a reference to the 'gc' and the 'gc' holds a
5833 5833 * reference to the 'gcgrp'. We can now release the extra reference
5834 5834 * the 'gcgrp' acquired in the gcgrp_lookup, if it was not used.
5835 5835 */
5836 5836 if (gcgrp_xtraref)
5837 5837 GCGRP_REFRELE(gcgrp);
5838 5838 if (ire == NULL) {
5839 5839 if (gc != NULL)
5840 5840 GC_REFRELE(gc);
5841 5841 if (ipif != NULL)
5842 5842 ipif_refrele(ipif);
5843 5843 ire_refrele(gw_ire);
5844 5844 return (ENOMEM);
5845 5845 }
5846 5846
5847 5847 /* Before we add, check if an extra CGTP broadcast is needed */
5848 5848 cgtp_broadcast = ((flags & RTF_MULTIRT) &&
5849 5849 ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST);
5850 5850
5851 5851 /* src address assigned by the caller? */
5852 5852 if ((src_addr != INADDR_ANY) && (flags & RTF_SETSRC))
5853 5853 ire->ire_setsrc_addr = src_addr;
5854 5854
5855 5855 ire->ire_unbound = unbound;
5856 5856
5857 5857 /*
5858 5858 * POLICY: should we allow an RTF_HOST with address INADDR_ANY?
5859 5859 * SUN/OS socket stuff does but do we really want to allow 0.0.0.0?
5860 5860 */
5861 5861
5862 5862 /* Add the new IRE. */
5863 5863 nire = ire_add(ire);
5864 5864 if (nire == NULL) {
5865 5865 /*
5866 5866 * In the result of failure, ire_add() will have
5867 5867 * already deleted the ire in question, so there
5868 5868 * is no need to do that here.
5869 5869 */
5870 5870 if (ipif != NULL)
5871 5871 ipif_refrele(ipif);
5872 5872 ire_refrele(gw_ire);
5873 5873 return (ENOMEM);
5874 5874 }
5875 5875 /*
5876 5876 * Check if it was a duplicate entry. This handles
5877 5877 * the case of two racing route adds for the same route
5878 5878 */
5879 5879 if (nire != ire) {
5880 5880 ire_delete(nire);
5881 5881 ire_refrele(nire);
5882 5882 if (ipif != NULL)
5883 5883 ipif_refrele(ipif);
5884 5884 ire_refrele(gw_ire);
5885 5885 return (EEXIST);
5886 5886 }
5887 5887 ire = nire;
5888 5888
5889 5889 if (flags & RTF_MULTIRT) {
5890 5890 /*
5891 5891 * Invoke the CGTP (multirouting) filtering module
5892 5892 * to add the dst address in the filtering database.
5893 5893 * Replicated inbound packets coming from that address
5894 5894 * will be filtered to discard the duplicates.
5895 5895 * It is not necessary to call the CGTP filter hook
5896 5896 * when the dst address is a broadcast or multicast,
5897 5897 * because an IP source address cannot be a broadcast
5898 5898 * or a multicast.
5899 5899 */
5900 5900 if (cgtp_broadcast) {
5901 5901 ip_cgtp_bcast_add(ire, ipst);
5902 5902 goto save_ire;
5903 5903 }
5904 5904 if (ipst->ips_ip_cgtp_filter_ops != NULL &&
5905 5905 !CLASSD(ire->ire_addr)) {
5906 5906 int res;
5907 5907 ipif_t *src_ipif;
5908 5908
5909 5909 /* Find the source address corresponding to gw_ire */
5910 5910 src_ipif = ipif_lookup_addr(gw_ire->ire_gateway_addr,
5911 5911 NULL, zoneid, ipst);
5912 5912 if (src_ipif != NULL) {
5913 5913 res = ipst->ips_ip_cgtp_filter_ops->
5914 5914 cfo_add_dest_v4(
5915 5915 ipst->ips_netstack->netstack_stackid,
5916 5916 ire->ire_addr,
5917 5917 ire->ire_gateway_addr,
5918 5918 ire->ire_setsrc_addr,
5919 5919 src_ipif->ipif_lcl_addr);
5920 5920 ipif_refrele(src_ipif);
5921 5921 } else {
5922 5922 res = EADDRNOTAVAIL;
5923 5923 }
5924 5924 if (res != 0) {
5925 5925 if (ipif != NULL)
5926 5926 ipif_refrele(ipif);
5927 5927 ire_refrele(gw_ire);
5928 5928 ire_delete(ire);
5929 5929 ire_refrele(ire); /* Held in ire_add */
5930 5930 return (res);
5931 5931 }
5932 5932 }
5933 5933 }
5934 5934
5935 5935 save_ire:
5936 5936 if (gw_ire != NULL) {
5937 5937 ire_refrele(gw_ire);
5938 5938 gw_ire = NULL;
5939 5939 }
5940 5940 if (ill != NULL) {
5941 5941 /*
5942 5942 * Save enough information so that we can recreate the IRE if
5943 5943 * the interface goes down and then up. The metrics associated
5944 5944 * with the route will be saved as well when rts_setmetrics() is
5945 5945 * called after the IRE has been created. In the case where
5946 5946 * memory cannot be allocated, none of this information will be
5947 5947 * saved.
5948 5948 */
5949 5949 ill_save_ire(ill, ire);
5950 5950 }
5951 5951 if (ioctl_msg)
5952 5952 ip_rts_rtmsg(RTM_OLDADD, ire, 0, ipst);
5953 5953 if (ire_arg != NULL) {
5954 5954 /*
5955 5955 * Store the ire that was successfully added into where ire_arg
5956 5956 * points to so that callers don't have to look it up
5957 5957 * themselves (but they are responsible for ire_refrele()ing
5958 5958 * the ire when they are finished with it).
5959 5959 */
5960 5960 *ire_arg = ire;
5961 5961 } else {
5962 5962 ire_refrele(ire); /* Held in ire_add */
5963 5963 }
5964 5964 if (ipif != NULL)
5965 5965 ipif_refrele(ipif);
5966 5966 return (0);
5967 5967 }
5968 5968
5969 5969 /*
5970 5970 * ip_rt_delete is called to delete an IPv4 route.
5971 5971 * ill is passed in to associate it with the correct interface.
5972 5972 */
5973 5973 /* ARGSUSED4 */
5974 5974 int
5975 5975 ip_rt_delete(ipaddr_t dst_addr, ipaddr_t mask, ipaddr_t gw_addr,
5976 5976 uint_t rtm_addrs, int flags, ill_t *ill, boolean_t ioctl_msg,
5977 5977 ip_stack_t *ipst, zoneid_t zoneid)
5978 5978 {
5979 5979 ire_t *ire = NULL;
5980 5980 ipif_t *ipif;
5981 5981 uint_t type;
5982 5982 uint_t match_flags = MATCH_IRE_TYPE;
5983 5983 int err = 0;
5984 5984
5985 5985 ip1dbg(("ip_rt_delete:"));
5986 5986 /*
5987 5987 * If this is the case of RTF_HOST being set, then we set the netmask
5988 5988 * to all ones. Otherwise, we use the netmask if one was supplied.
5989 5989 */
5990 5990 if (flags & RTF_HOST) {
5991 5991 mask = IP_HOST_MASK;
5992 5992 match_flags |= MATCH_IRE_MASK;
5993 5993 } else if (rtm_addrs & RTA_NETMASK) {
5994 5994 match_flags |= MATCH_IRE_MASK;
5995 5995 }
5996 5996
5997 5997 /*
5998 5998 * Note that RTF_GATEWAY is never set on a delete, therefore
5999 5999 * we check if the gateway address is one of our interfaces first,
6000 6000 * and fall back on RTF_GATEWAY routes.
6001 6001 *
6002 6002 * This makes it possible to delete an original
6003 6003 * IRE_IF_NORESOLVER/IRE_IF_RESOLVER - consistent with SunOS 4.1.
6004 6004 * However, we have RTF_KERNEL set on the ones created by ipif_up
6005 6005 * and those can not be deleted here.
6006 6006 *
6007 6007 * We use MATCH_IRE_ILL if we know the interface. If the caller
6008 6008 * specified an interface (from the RTA_IFP sockaddr) we use it,
6009 6009 * otherwise we use the ill derived from the gateway address.
6010 6010 * We can always match the gateway address since we record it
6011 6011 * in ire_gateway_addr.
6012 6012 *
6013 6013 * For more detail on specifying routes by gateway address and by
6014 6014 * interface index, see the comments in ip_rt_add().
6015 6015 */
6016 6016 ipif = ipif_lookup_interface(gw_addr, dst_addr, ipst);
6017 6017 if (ipif != NULL) {
6018 6018 ill_t *ill_match;
6019 6019
6020 6020 if (ill != NULL)
6021 6021 ill_match = ill;
6022 6022 else
6023 6023 ill_match = ipif->ipif_ill;
6024 6024
6025 6025 match_flags |= MATCH_IRE_ILL;
6026 6026 if (ipif->ipif_ire_type == IRE_LOOPBACK) {
6027 6027 ire = ire_ftable_lookup_v4(dst_addr, mask, 0,
6028 6028 IRE_LOOPBACK, ill_match, ALL_ZONES, NULL,
6029 6029 match_flags, 0, ipst, NULL);
6030 6030 }
6031 6031 if (ire == NULL) {
6032 6032 match_flags |= MATCH_IRE_GW;
6033 6033 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr,
6034 6034 IRE_INTERFACE, ill_match, ALL_ZONES, NULL,
6035 6035 match_flags, 0, ipst, NULL);
6036 6036 }
6037 6037 /* Avoid deleting routes created by kernel from an ipif */
6038 6038 if (ire != NULL && (ire->ire_flags & RTF_KERNEL)) {
6039 6039 ire_refrele(ire);
6040 6040 ire = NULL;
6041 6041 }
6042 6042
6043 6043 /* Restore in case we didn't find a match */
6044 6044 match_flags &= ~(MATCH_IRE_GW|MATCH_IRE_ILL);
6045 6045 }
6046 6046
6047 6047 if (ire == NULL) {
6048 6048 /*
6049 6049 * At this point, the gateway address is not one of our own
6050 6050 * addresses or a matching interface route was not found. We
6051 6051 * set the IRE type to lookup based on whether
6052 6052 * this is a host route, a default route or just a prefix.
6053 6053 *
6054 6054 * If an ill was passed in, then the lookup is based on an
6055 6055 * interface index so MATCH_IRE_ILL is added to match_flags.
6056 6056 */
6057 6057 match_flags |= MATCH_IRE_GW;
6058 6058 if (ill != NULL)
6059 6059 match_flags |= MATCH_IRE_ILL;
6060 6060 if (mask == IP_HOST_MASK)
6061 6061 type = IRE_HOST;
6062 6062 else if (mask == 0)
6063 6063 type = IRE_DEFAULT;
6064 6064 else
6065 6065 type = IRE_PREFIX;
6066 6066 ire = ire_ftable_lookup_v4(dst_addr, mask, gw_addr, type, ill,
6067 6067 ALL_ZONES, NULL, match_flags, 0, ipst, NULL);
6068 6068 }
6069 6069
6070 6070 if (ipif != NULL) {
6071 6071 ipif_refrele(ipif);
6072 6072 ipif = NULL;
6073 6073 }
6074 6074
6075 6075 if (ire == NULL)
6076 6076 return (ESRCH);
6077 6077
6078 6078 if (ire->ire_flags & RTF_MULTIRT) {
6079 6079 /*
6080 6080 * Invoke the CGTP (multirouting) filtering module
6081 6081 * to remove the dst address from the filtering database.
6082 6082 * Packets coming from that address will no longer be
6083 6083 * filtered to remove duplicates.
6084 6084 */
6085 6085 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
6086 6086 err = ipst->ips_ip_cgtp_filter_ops->cfo_del_dest_v4(
6087 6087 ipst->ips_netstack->netstack_stackid,
6088 6088 ire->ire_addr, ire->ire_gateway_addr);
6089 6089 }
6090 6090 ip_cgtp_bcast_delete(ire, ipst);
6091 6091 }
6092 6092
6093 6093 ill = ire->ire_ill;
6094 6094 if (ill != NULL)
6095 6095 ill_remove_saved_ire(ill, ire);
6096 6096 if (ioctl_msg)
6097 6097 ip_rts_rtmsg(RTM_OLDDEL, ire, 0, ipst);
6098 6098 ire_delete(ire);
6099 6099 ire_refrele(ire);
6100 6100 return (err);
6101 6101 }
6102 6102
6103 6103 /*
6104 6104 * ip_siocaddrt is called to complete processing of an SIOCADDRT IOCTL.
6105 6105 */
6106 6106 /* ARGSUSED */
6107 6107 int
6108 6108 ip_siocaddrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6109 6109 ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6110 6110 {
6111 6111 ipaddr_t dst_addr;
6112 6112 ipaddr_t gw_addr;
6113 6113 ipaddr_t mask;
6114 6114 int error = 0;
6115 6115 mblk_t *mp1;
6116 6116 struct rtentry *rt;
6117 6117 ipif_t *ipif = NULL;
6118 6118 ip_stack_t *ipst;
6119 6119
6120 6120 ASSERT(q->q_next == NULL);
6121 6121 ipst = CONNQ_TO_IPST(q);
6122 6122
6123 6123 ip1dbg(("ip_siocaddrt:"));
6124 6124 /* Existence of mp1 verified in ip_wput_nondata */
6125 6125 mp1 = mp->b_cont->b_cont;
6126 6126 rt = (struct rtentry *)mp1->b_rptr;
6127 6127
6128 6128 dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6129 6129 gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6130 6130
6131 6131 /*
6132 6132 * If the RTF_HOST flag is on, this is a request to assign a gateway
6133 6133 * to a particular host address. In this case, we set the netmask to
6134 6134 * all ones for the particular destination address. Otherwise,
6135 6135 * determine the netmask to be used based on dst_addr and the interfaces
6136 6136 * in use.
6137 6137 */
6138 6138 if (rt->rt_flags & RTF_HOST) {
6139 6139 mask = IP_HOST_MASK;
6140 6140 } else {
6141 6141 /*
6142 6142 * Note that ip_subnet_mask returns a zero mask in the case of
6143 6143 * default (an all-zeroes address).
6144 6144 */
6145 6145 mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6146 6146 }
6147 6147
6148 6148 error = ip_rt_add(dst_addr, mask, gw_addr, 0, rt->rt_flags, NULL, NULL,
6149 6149 B_TRUE, NULL, ipst, ALL_ZONES);
6150 6150 if (ipif != NULL)
6151 6151 ipif_refrele(ipif);
6152 6152 return (error);
6153 6153 }
6154 6154
6155 6155 /*
6156 6156 * ip_siocdelrt is called to complete processing of an SIOCDELRT IOCTL.
6157 6157 */
6158 6158 /* ARGSUSED */
6159 6159 int
6160 6160 ip_siocdelrt(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
6161 6161 ip_ioctl_cmd_t *ipip, void *dummy_if_req)
6162 6162 {
6163 6163 ipaddr_t dst_addr;
6164 6164 ipaddr_t gw_addr;
6165 6165 ipaddr_t mask;
6166 6166 int error;
6167 6167 mblk_t *mp1;
6168 6168 struct rtentry *rt;
6169 6169 ipif_t *ipif = NULL;
6170 6170 ip_stack_t *ipst;
6171 6171
6172 6172 ASSERT(q->q_next == NULL);
6173 6173 ipst = CONNQ_TO_IPST(q);
6174 6174
6175 6175 ip1dbg(("ip_siocdelrt:"));
6176 6176 /* Existence of mp1 verified in ip_wput_nondata */
6177 6177 mp1 = mp->b_cont->b_cont;
6178 6178 rt = (struct rtentry *)mp1->b_rptr;
6179 6179
6180 6180 dst_addr = ((sin_t *)&rt->rt_dst)->sin_addr.s_addr;
6181 6181 gw_addr = ((sin_t *)&rt->rt_gateway)->sin_addr.s_addr;
6182 6182
6183 6183 /*
6184 6184 * If the RTF_HOST flag is on, this is a request to delete a gateway
6185 6185 * to a particular host address. In this case, we set the netmask to
6186 6186 * all ones for the particular destination address. Otherwise,
6187 6187 * determine the netmask to be used based on dst_addr and the interfaces
6188 6188 * in use.
6189 6189 */
6190 6190 if (rt->rt_flags & RTF_HOST) {
6191 6191 mask = IP_HOST_MASK;
6192 6192 } else {
6193 6193 /*
6194 6194 * Note that ip_subnet_mask returns a zero mask in the case of
6195 6195 * default (an all-zeroes address).
6196 6196 */
6197 6197 mask = ip_subnet_mask(dst_addr, &ipif, ipst);
6198 6198 }
6199 6199
6200 6200 error = ip_rt_delete(dst_addr, mask, gw_addr,
6201 6201 RTA_DST | RTA_GATEWAY | RTA_NETMASK, rt->rt_flags, NULL, B_TRUE,
6202 6202 ipst, ALL_ZONES);
6203 6203 if (ipif != NULL)
6204 6204 ipif_refrele(ipif);
6205 6205 return (error);
6206 6206 }
6207 6207
6208 6208 /*
6209 6209 * Enqueue the mp onto the ipsq, chained by b_next.
6210 6210 * b_prev stores the function to be executed later, and b_queue the queue
6211 6211 * where this mp originated.
6212 6212 */
6213 6213 void
6214 6214 ipsq_enq(ipsq_t *ipsq, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6215 6215 ill_t *pending_ill)
6216 6216 {
6217 6217 conn_t *connp;
6218 6218 ipxop_t *ipx = ipsq->ipsq_xop;
6219 6219
6220 6220 ASSERT(MUTEX_HELD(&ipsq->ipsq_lock));
6221 6221 ASSERT(MUTEX_HELD(&ipx->ipx_lock));
6222 6222 ASSERT(func != NULL);
6223 6223
6224 6224 mp->b_queue = q;
6225 6225 mp->b_prev = (void *)func;
6226 6226 mp->b_next = NULL;
6227 6227
6228 6228 switch (type) {
6229 6229 case CUR_OP:
6230 6230 if (ipx->ipx_mptail != NULL) {
6231 6231 ASSERT(ipx->ipx_mphead != NULL);
6232 6232 ipx->ipx_mptail->b_next = mp;
6233 6233 } else {
6234 6234 ASSERT(ipx->ipx_mphead == NULL);
6235 6235 ipx->ipx_mphead = mp;
6236 6236 }
6237 6237 ipx->ipx_mptail = mp;
6238 6238 break;
6239 6239
6240 6240 case NEW_OP:
6241 6241 if (ipsq->ipsq_xopq_mptail != NULL) {
6242 6242 ASSERT(ipsq->ipsq_xopq_mphead != NULL);
6243 6243 ipsq->ipsq_xopq_mptail->b_next = mp;
6244 6244 } else {
6245 6245 ASSERT(ipsq->ipsq_xopq_mphead == NULL);
6246 6246 ipsq->ipsq_xopq_mphead = mp;
6247 6247 }
6248 6248 ipsq->ipsq_xopq_mptail = mp;
6249 6249 ipx->ipx_ipsq_queued = B_TRUE;
6250 6250 break;
6251 6251
6252 6252 case SWITCH_OP:
6253 6253 ASSERT(ipsq->ipsq_swxop != NULL);
6254 6254 /* only one switch operation is currently allowed */
6255 6255 ASSERT(ipsq->ipsq_switch_mp == NULL);
6256 6256 ipsq->ipsq_switch_mp = mp;
6257 6257 ipx->ipx_ipsq_queued = B_TRUE;
6258 6258 break;
6259 6259 default:
6260 6260 cmn_err(CE_PANIC, "ipsq_enq %d type \n", type);
6261 6261 }
6262 6262
6263 6263 if (CONN_Q(q) && pending_ill != NULL) {
6264 6264 connp = Q_TO_CONN(q);
6265 6265 ASSERT(MUTEX_HELD(&connp->conn_lock));
6266 6266 connp->conn_oper_pending_ill = pending_ill;
6267 6267 }
6268 6268 }
6269 6269
6270 6270 /*
6271 6271 * Dequeue the next message that requested exclusive access to this IPSQ's
6272 6272 * xop. Specifically:
6273 6273 *
6274 6274 * 1. If we're still processing the current operation on `ipsq', then
6275 6275 * dequeue the next message for the operation (from ipx_mphead), or
6276 6276 * return NULL if there are no queued messages for the operation.
6277 6277 * These messages are queued via CUR_OP to qwriter_ip() and friends.
6278 6278 *
6279 6279 * 2. If the current operation on `ipsq' has completed (ipx_current_ipif is
6280 6280 * not set) see if the ipsq has requested an xop switch. If so, switch
6281 6281 * `ipsq' to a different xop. Xop switches only happen when joining or
6282 6282 * leaving IPMP groups and require a careful dance -- see the comments
6283 6283 * in-line below for details. If we're leaving a group xop or if we're
6284 6284 * joining a group xop and become writer on it, then we proceed to (3).
6285 6285 * Otherwise, we return NULL and exit the xop.
6286 6286 *
6287 6287 * 3. For each IPSQ in the xop, return any switch operation stored on
6288 6288 * ipsq_switch_mp (set via SWITCH_OP); these must be processed before
6289 6289 * any other messages queued on the IPSQ. Otherwise, dequeue the next
6290 6290 * exclusive operation (queued via NEW_OP) stored on ipsq_xopq_mphead.
6291 6291 * Note that if the phyint tied to `ipsq' is not using IPMP there will
6292 6292 * only be one IPSQ in the xop. Otherwise, there will be one IPSQ for
6293 6293 * each phyint in the group, including the IPMP meta-interface phyint.
6294 6294 */
6295 6295 static mblk_t *
6296 6296 ipsq_dq(ipsq_t *ipsq)
6297 6297 {
6298 6298 ill_t *illv4, *illv6;
6299 6299 mblk_t *mp;
6300 6300 ipsq_t *xopipsq;
6301 6301 ipsq_t *leftipsq = NULL;
6302 6302 ipxop_t *ipx;
6303 6303 phyint_t *phyi = ipsq->ipsq_phyint;
6304 6304 ip_stack_t *ipst = ipsq->ipsq_ipst;
6305 6305 boolean_t emptied = B_FALSE;
6306 6306
6307 6307 /*
6308 6308 * Grab all the locks we need in the defined order (ill_g_lock ->
6309 6309 * ipsq_lock -> ipx_lock); ill_g_lock is needed to use ipsq_next.
6310 6310 */
6311 6311 rw_enter(&ipst->ips_ill_g_lock,
6312 6312 ipsq->ipsq_swxop != NULL ? RW_WRITER : RW_READER);
6313 6313 mutex_enter(&ipsq->ipsq_lock);
6314 6314 ipx = ipsq->ipsq_xop;
6315 6315 mutex_enter(&ipx->ipx_lock);
6316 6316
6317 6317 /*
6318 6318 * Dequeue the next message associated with the current exclusive
6319 6319 * operation, if any.
6320 6320 */
6321 6321 if ((mp = ipx->ipx_mphead) != NULL) {
6322 6322 ipx->ipx_mphead = mp->b_next;
6323 6323 if (ipx->ipx_mphead == NULL)
6324 6324 ipx->ipx_mptail = NULL;
6325 6325 mp->b_next = (void *)ipsq;
6326 6326 goto out;
6327 6327 }
6328 6328
6329 6329 if (ipx->ipx_current_ipif != NULL)
6330 6330 goto empty;
6331 6331
6332 6332 if (ipsq->ipsq_swxop != NULL) {
6333 6333 /*
6334 6334 * The exclusive operation that is now being completed has
6335 6335 * requested a switch to a different xop. This happens
6336 6336 * when an interface joins or leaves an IPMP group. Joins
6337 6337 * happen through SIOCSLIFGROUPNAME (ip_sioctl_groupname()).
6338 6338 * Leaves happen via SIOCSLIFGROUPNAME, interface unplumb
6339 6339 * (phyint_free()), or interface plumb for an ill type
6340 6340 * not in the IPMP group (ip_rput_dlpi_writer()).
6341 6341 *
6342 6342 * Xop switches are not allowed on the IPMP meta-interface.
6343 6343 */
6344 6344 ASSERT(phyi == NULL || !(phyi->phyint_flags & PHYI_IPMP));
6345 6345 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
6346 6346 DTRACE_PROBE1(ipsq__switch, (ipsq_t *), ipsq);
6347 6347
6348 6348 if (ipsq->ipsq_swxop == &ipsq->ipsq_ownxop) {
6349 6349 /*
6350 6350 * We're switching back to our own xop, so we have two
6351 6351 * xop's to drain/exit: our own, and the group xop
6352 6352 * that we are leaving.
6353 6353 *
6354 6354 * First, pull ourselves out of the group ipsq list.
6355 6355 * This is safe since we're writer on ill_g_lock.
6356 6356 */
6357 6357 ASSERT(ipsq->ipsq_xop != &ipsq->ipsq_ownxop);
6358 6358
6359 6359 xopipsq = ipx->ipx_ipsq;
6360 6360 while (xopipsq->ipsq_next != ipsq)
6361 6361 xopipsq = xopipsq->ipsq_next;
6362 6362
6363 6363 xopipsq->ipsq_next = ipsq->ipsq_next;
6364 6364 ipsq->ipsq_next = ipsq;
6365 6365 ipsq->ipsq_xop = ipsq->ipsq_swxop;
6366 6366 ipsq->ipsq_swxop = NULL;
6367 6367
6368 6368 /*
6369 6369 * Second, prepare to exit the group xop. The actual
6370 6370 * ipsq_exit() is done at the end of this function
6371 6371 * since we cannot hold any locks across ipsq_exit().
6372 6372 * Note that although we drop the group's ipx_lock, no
6373 6373 * threads can proceed since we're still ipx_writer.
6374 6374 */
6375 6375 leftipsq = xopipsq;
6376 6376 mutex_exit(&ipx->ipx_lock);
6377 6377
6378 6378 /*
6379 6379 * Third, set ipx to point to our own xop (which was
6380 6380 * inactive and therefore can be entered).
6381 6381 */
6382 6382 ipx = ipsq->ipsq_xop;
6383 6383 mutex_enter(&ipx->ipx_lock);
6384 6384 ASSERT(ipx->ipx_writer == NULL);
6385 6385 ASSERT(ipx->ipx_current_ipif == NULL);
6386 6386 } else {
6387 6387 /*
6388 6388 * We're switching from our own xop to a group xop.
6389 6389 * The requestor of the switch must ensure that the
6390 6390 * group xop cannot go away (e.g. by ensuring the
6391 6391 * phyint associated with the xop cannot go away).
6392 6392 *
6393 6393 * If we can become writer on our new xop, then we'll
6394 6394 * do the drain. Otherwise, the current writer of our
6395 6395 * new xop will do the drain when it exits.
6396 6396 *
6397 6397 * First, splice ourselves into the group IPSQ list.
6398 6398 * This is safe since we're writer on ill_g_lock.
6399 6399 */
6400 6400 ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6401 6401
6402 6402 xopipsq = ipsq->ipsq_swxop->ipx_ipsq;
6403 6403 while (xopipsq->ipsq_next != ipsq->ipsq_swxop->ipx_ipsq)
6404 6404 xopipsq = xopipsq->ipsq_next;
6405 6405
6406 6406 xopipsq->ipsq_next = ipsq;
6407 6407 ipsq->ipsq_next = ipsq->ipsq_swxop->ipx_ipsq;
6408 6408 ipsq->ipsq_xop = ipsq->ipsq_swxop;
6409 6409 ipsq->ipsq_swxop = NULL;
6410 6410
6411 6411 /*
6412 6412 * Second, exit our own xop, since it's now unused.
6413 6413 * This is safe since we've got the only reference.
6414 6414 */
6415 6415 ASSERT(ipx->ipx_writer == curthread);
6416 6416 ipx->ipx_writer = NULL;
6417 6417 VERIFY(--ipx->ipx_reentry_cnt == 0);
6418 6418 ipx->ipx_ipsq_queued = B_FALSE;
6419 6419 mutex_exit(&ipx->ipx_lock);
6420 6420
6421 6421 /*
6422 6422 * Third, set ipx to point to our new xop, and check
6423 6423 * if we can become writer on it. If we cannot, then
6424 6424 * the current writer will drain the IPSQ group when
6425 6425 * it exits. Our ipsq_xop is guaranteed to be stable
6426 6426 * because we're still holding ipsq_lock.
6427 6427 */
6428 6428 ipx = ipsq->ipsq_xop;
6429 6429 mutex_enter(&ipx->ipx_lock);
6430 6430 if (ipx->ipx_writer != NULL ||
6431 6431 ipx->ipx_current_ipif != NULL) {
6432 6432 goto out;
6433 6433 }
6434 6434 }
6435 6435
6436 6436 /*
6437 6437 * Fourth, become writer on our new ipx before we continue
6438 6438 * with the drain. Note that we never dropped ipsq_lock
6439 6439 * above, so no other thread could've raced with us to
6440 6440 * become writer first. Also, we're holding ipx_lock, so
6441 6441 * no other thread can examine the ipx right now.
6442 6442 */
6443 6443 ASSERT(ipx->ipx_current_ipif == NULL);
6444 6444 ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6445 6445 VERIFY(ipx->ipx_reentry_cnt++ == 0);
6446 6446 ipx->ipx_writer = curthread;
6447 6447 ipx->ipx_forced = B_FALSE;
6448 6448 #ifdef DEBUG
6449 6449 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6450 6450 #endif
6451 6451 }
6452 6452
6453 6453 xopipsq = ipsq;
6454 6454 do {
6455 6455 /*
6456 6456 * So that other operations operate on a consistent and
6457 6457 * complete phyint, a switch message on an IPSQ must be
6458 6458 * handled prior to any other operations on that IPSQ.
6459 6459 */
6460 6460 if ((mp = xopipsq->ipsq_switch_mp) != NULL) {
6461 6461 xopipsq->ipsq_switch_mp = NULL;
6462 6462 ASSERT(mp->b_next == NULL);
6463 6463 mp->b_next = (void *)xopipsq;
6464 6464 goto out;
6465 6465 }
6466 6466
6467 6467 if ((mp = xopipsq->ipsq_xopq_mphead) != NULL) {
6468 6468 xopipsq->ipsq_xopq_mphead = mp->b_next;
6469 6469 if (xopipsq->ipsq_xopq_mphead == NULL)
6470 6470 xopipsq->ipsq_xopq_mptail = NULL;
6471 6471 mp->b_next = (void *)xopipsq;
6472 6472 goto out;
6473 6473 }
6474 6474 } while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6475 6475 empty:
6476 6476 /*
6477 6477 * There are no messages. Further, we are holding ipx_lock, hence no
6478 6478 * new messages can end up on any IPSQ in the xop.
6479 6479 */
6480 6480 ipx->ipx_writer = NULL;
6481 6481 ipx->ipx_forced = B_FALSE;
6482 6482 VERIFY(--ipx->ipx_reentry_cnt == 0);
6483 6483 ipx->ipx_ipsq_queued = B_FALSE;
6484 6484 emptied = B_TRUE;
6485 6485 #ifdef DEBUG
6486 6486 ipx->ipx_depth = 0;
6487 6487 #endif
6488 6488 out:
6489 6489 mutex_exit(&ipx->ipx_lock);
6490 6490 mutex_exit(&ipsq->ipsq_lock);
6491 6491
6492 6492 /*
6493 6493 * If we completely emptied the xop, then wake up any threads waiting
6494 6494 * to enter any of the IPSQ's associated with it.
6495 6495 */
6496 6496 if (emptied) {
6497 6497 xopipsq = ipsq;
6498 6498 do {
6499 6499 if ((phyi = xopipsq->ipsq_phyint) == NULL)
6500 6500 continue;
6501 6501
6502 6502 illv4 = phyi->phyint_illv4;
6503 6503 illv6 = phyi->phyint_illv6;
6504 6504
6505 6505 GRAB_ILL_LOCKS(illv4, illv6);
6506 6506 if (illv4 != NULL)
6507 6507 cv_broadcast(&illv4->ill_cv);
6508 6508 if (illv6 != NULL)
6509 6509 cv_broadcast(&illv6->ill_cv);
6510 6510 RELEASE_ILL_LOCKS(illv4, illv6);
6511 6511 } while ((xopipsq = xopipsq->ipsq_next) != ipsq);
6512 6512 }
6513 6513 rw_exit(&ipst->ips_ill_g_lock);
6514 6514
6515 6515 /*
6516 6516 * Now that all locks are dropped, exit the IPSQ we left.
6517 6517 */
6518 6518 if (leftipsq != NULL)
6519 6519 ipsq_exit(leftipsq);
6520 6520
6521 6521 return (mp);
6522 6522 }
6523 6523
6524 6524 /*
6525 6525 * Return completion status of previously initiated DLPI operations on
6526 6526 * ills in the purview of an ipsq.
6527 6527 */
6528 6528 static boolean_t
6529 6529 ipsq_dlpi_done(ipsq_t *ipsq)
6530 6530 {
6531 6531 ipsq_t *ipsq_start;
6532 6532 phyint_t *phyi;
6533 6533 ill_t *ill;
6534 6534
6535 6535 ASSERT(RW_LOCK_HELD(&ipsq->ipsq_ipst->ips_ill_g_lock));
6536 6536 ipsq_start = ipsq;
6537 6537
6538 6538 do {
6539 6539 /*
6540 6540 * The only current users of this function are ipsq_try_enter
6541 6541 * and ipsq_enter which have made sure that ipsq_writer is
6542 6542 * NULL before we reach here. ill_dlpi_pending is modified
6543 6543 * only by an ipsq writer
6544 6544 */
6545 6545 ASSERT(ipsq->ipsq_xop->ipx_writer == NULL);
6546 6546 phyi = ipsq->ipsq_phyint;
6547 6547 /*
6548 6548 * phyi could be NULL if a phyint that is part of an
6549 6549 * IPMP group is being unplumbed. A more detailed
6550 6550 * comment is in ipmp_grp_update_kstats()
6551 6551 */
6552 6552 if (phyi != NULL) {
6553 6553 ill = phyi->phyint_illv4;
6554 6554 if (ill != NULL &&
6555 6555 (ill->ill_dlpi_pending != DL_PRIM_INVAL ||
6556 6556 ill->ill_arl_dlpi_pending))
6557 6557 return (B_FALSE);
6558 6558
6559 6559 ill = phyi->phyint_illv6;
6560 6560 if (ill != NULL &&
6561 6561 ill->ill_dlpi_pending != DL_PRIM_INVAL)
6562 6562 return (B_FALSE);
6563 6563 }
6564 6564
6565 6565 } while ((ipsq = ipsq->ipsq_next) != ipsq_start);
6566 6566
6567 6567 return (B_TRUE);
6568 6568 }
6569 6569
6570 6570 /*
6571 6571 * Enter the ipsq corresponding to ill, by waiting synchronously till
6572 6572 * we can enter the ipsq exclusively. Unless 'force' is used, the ipsq
6573 6573 * will have to drain completely before ipsq_enter returns success.
6574 6574 * ipx_current_ipif will be set if some exclusive op is in progress,
6575 6575 * and the ipsq_exit logic will start the next enqueued op after
6576 6576 * completion of the current op. If 'force' is used, we don't wait
6577 6577 * for the enqueued ops. This is needed when a conn_close wants to
6578 6578 * enter the ipsq and abort an ioctl that is somehow stuck. Unplumb
6579 6579 * of an ill can also use this option. But we dont' use it currently.
6580 6580 */
6581 6581 #define ENTER_SQ_WAIT_TICKS 100
6582 6582 boolean_t
6583 6583 ipsq_enter(ill_t *ill, boolean_t force, int type)
6584 6584 {
6585 6585 ipsq_t *ipsq;
6586 6586 ipxop_t *ipx;
6587 6587 boolean_t waited_enough = B_FALSE;
6588 6588 ip_stack_t *ipst = ill->ill_ipst;
6589 6589
6590 6590 /*
6591 6591 * Note that the relationship between ill and ipsq is fixed as long as
6592 6592 * the ill is not ILL_CONDEMNED. Holding ipsq_lock ensures the
6593 6593 * relationship between the IPSQ and xop cannot change. However,
6594 6594 * since we cannot hold ipsq_lock across the cv_wait(), it may change
6595 6595 * while we're waiting. We wait on ill_cv and rely on ipsq_exit()
6596 6596 * waking up all ills in the xop when it becomes available.
6597 6597 */
6598 6598 for (;;) {
6599 6599 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6600 6600 mutex_enter(&ill->ill_lock);
6601 6601 if (ill->ill_state_flags & ILL_CONDEMNED) {
6602 6602 mutex_exit(&ill->ill_lock);
6603 6603 rw_exit(&ipst->ips_ill_g_lock);
6604 6604 return (B_FALSE);
6605 6605 }
6606 6606
6607 6607 ipsq = ill->ill_phyint->phyint_ipsq;
6608 6608 mutex_enter(&ipsq->ipsq_lock);
6609 6609 ipx = ipsq->ipsq_xop;
6610 6610 mutex_enter(&ipx->ipx_lock);
6611 6611
6612 6612 if (ipx->ipx_writer == NULL && (type == CUR_OP ||
6613 6613 (ipx->ipx_current_ipif == NULL && ipsq_dlpi_done(ipsq)) ||
6614 6614 waited_enough))
6615 6615 break;
6616 6616
6617 6617 rw_exit(&ipst->ips_ill_g_lock);
6618 6618
6619 6619 if (!force || ipx->ipx_writer != NULL) {
6620 6620 mutex_exit(&ipx->ipx_lock);
6621 6621 mutex_exit(&ipsq->ipsq_lock);
6622 6622 cv_wait(&ill->ill_cv, &ill->ill_lock);
6623 6623 } else {
6624 6624 mutex_exit(&ipx->ipx_lock);
6625 6625 mutex_exit(&ipsq->ipsq_lock);
6626 6626 (void) cv_reltimedwait(&ill->ill_cv,
6627 6627 &ill->ill_lock, ENTER_SQ_WAIT_TICKS, TR_CLOCK_TICK);
6628 6628 waited_enough = B_TRUE;
6629 6629 }
6630 6630 mutex_exit(&ill->ill_lock);
6631 6631 }
6632 6632
6633 6633 ASSERT(ipx->ipx_mphead == NULL && ipx->ipx_mptail == NULL);
6634 6634 ASSERT(ipx->ipx_reentry_cnt == 0);
6635 6635 ipx->ipx_writer = curthread;
6636 6636 ipx->ipx_forced = (ipx->ipx_current_ipif != NULL);
6637 6637 ipx->ipx_reentry_cnt++;
6638 6638 #ifdef DEBUG
6639 6639 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6640 6640 #endif
6641 6641 mutex_exit(&ipx->ipx_lock);
6642 6642 mutex_exit(&ipsq->ipsq_lock);
6643 6643 mutex_exit(&ill->ill_lock);
6644 6644 rw_exit(&ipst->ips_ill_g_lock);
6645 6645
6646 6646 return (B_TRUE);
6647 6647 }
6648 6648
6649 6649 /*
6650 6650 * ipif_set_values() has a constraint that it cannot drop the ips_ill_g_lock
6651 6651 * across the call to the core interface ipsq_try_enter() and hence calls this
6652 6652 * function directly. This is explained more fully in ipif_set_values().
6653 6653 * In order to support the above constraint, ipsq_try_enter is implemented as
6654 6654 * a wrapper that grabs the ips_ill_g_lock and calls this function subsequently
6655 6655 */
6656 6656 static ipsq_t *
6657 6657 ipsq_try_enter_internal(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func,
6658 6658 int type, boolean_t reentry_ok)
6659 6659 {
6660 6660 ipsq_t *ipsq;
6661 6661 ipxop_t *ipx;
6662 6662 ip_stack_t *ipst = ill->ill_ipst;
6663 6663
6664 6664 /*
6665 6665 * lock ordering:
6666 6666 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock.
6667 6667 *
6668 6668 * ipx of an ipsq can't change when ipsq_lock is held.
6669 6669 */
6670 6670 ASSERT(RW_LOCK_HELD(&ipst->ips_ill_g_lock));
6671 6671 GRAB_CONN_LOCK(q);
6672 6672 mutex_enter(&ill->ill_lock);
6673 6673 ipsq = ill->ill_phyint->phyint_ipsq;
6674 6674 mutex_enter(&ipsq->ipsq_lock);
6675 6675 ipx = ipsq->ipsq_xop;
6676 6676 mutex_enter(&ipx->ipx_lock);
6677 6677
6678 6678 /*
6679 6679 * 1. Enter the ipsq if we are already writer and reentry is ok.
6680 6680 * (Note: If the caller does not specify reentry_ok then neither
6681 6681 * 'func' nor any of its callees must ever attempt to enter the ipsq
6682 6682 * again. Otherwise it can lead to an infinite loop
6683 6683 * 2. Enter the ipsq if there is no current writer and this attempted
6684 6684 * entry is part of the current operation
6685 6685 * 3. Enter the ipsq if there is no current writer and this is a new
6686 6686 * operation and the operation queue is empty and there is no
6687 6687 * operation currently in progress and if all previously initiated
6688 6688 * DLPI operations have completed.
6689 6689 */
6690 6690 if ((ipx->ipx_writer == curthread && reentry_ok) ||
6691 6691 (ipx->ipx_writer == NULL && (type == CUR_OP || (type == NEW_OP &&
6692 6692 !ipx->ipx_ipsq_queued && ipx->ipx_current_ipif == NULL &&
6693 6693 ipsq_dlpi_done(ipsq))))) {
6694 6694 /* Success. */
6695 6695 ipx->ipx_reentry_cnt++;
6696 6696 ipx->ipx_writer = curthread;
6697 6697 ipx->ipx_forced = B_FALSE;
6698 6698 mutex_exit(&ipx->ipx_lock);
6699 6699 mutex_exit(&ipsq->ipsq_lock);
6700 6700 mutex_exit(&ill->ill_lock);
6701 6701 RELEASE_CONN_LOCK(q);
6702 6702 #ifdef DEBUG
6703 6703 ipx->ipx_depth = getpcstack(ipx->ipx_stack, IPX_STACK_DEPTH);
6704 6704 #endif
6705 6705 return (ipsq);
6706 6706 }
6707 6707
6708 6708 if (func != NULL)
6709 6709 ipsq_enq(ipsq, q, mp, func, type, ill);
6710 6710
6711 6711 mutex_exit(&ipx->ipx_lock);
6712 6712 mutex_exit(&ipsq->ipsq_lock);
6713 6713 mutex_exit(&ill->ill_lock);
6714 6714 RELEASE_CONN_LOCK(q);
6715 6715 return (NULL);
6716 6716 }
6717 6717
6718 6718 /*
6719 6719 * The ipsq_t (ipsq) is the synchronization data structure used to serialize
6720 6720 * certain critical operations like plumbing (i.e. most set ioctls), etc.
6721 6721 * There is one ipsq per phyint. The ipsq
6722 6722 * serializes exclusive ioctls issued by applications on a per ipsq basis in
6723 6723 * ipsq_xopq_mphead. It also protects against multiple threads executing in
6724 6724 * the ipsq. Responses from the driver pertain to the current ioctl (say a
6725 6725 * DL_BIND_ACK in response to a DL_BIND_REQ initiated as part of bringing
6726 6726 * up the interface) and are enqueued in ipx_mphead.
6727 6727 *
6728 6728 * If a thread does not want to reenter the ipsq when it is already writer,
6729 6729 * it must make sure that the specified reentry point to be called later
6730 6730 * when the ipsq is empty, nor any code path starting from the specified reentry
6731 6731 * point must never ever try to enter the ipsq again. Otherwise it can lead
6732 6732 * to an infinite loop. The reentry point ip_rput_dlpi_writer is an example.
6733 6733 * When the thread that is currently exclusive finishes, it (ipsq_exit)
6734 6734 * dequeues the requests waiting to become exclusive in ipx_mphead and calls
6735 6735 * the reentry point. When the list at ipx_mphead becomes empty ipsq_exit
6736 6736 * proceeds to dequeue the next ioctl in ipsq_xopq_mphead and start the next
6737 6737 * ioctl if the current ioctl has completed. If the current ioctl is still
6738 6738 * in progress it simply returns. The current ioctl could be waiting for
6739 6739 * a response from another module (the driver or could be waiting for
6740 6740 * the ipif/ill/ire refcnts to drop to zero. In such a case the ipx_pending_mp
6741 6741 * and ipx_pending_ipif are set. ipx_current_ipif is set throughout the
6742 6742 * execution of the ioctl and ipsq_exit does not start the next ioctl unless
6743 6743 * ipx_current_ipif is NULL which happens only once the ioctl is complete and
6744 6744 * all associated DLPI operations have completed.
6745 6745 */
6746 6746
6747 6747 /*
6748 6748 * Try to enter the IPSQ corresponding to `ipif' or `ill' exclusively (`ipif'
6749 6749 * and `ill' cannot both be specified). Returns a pointer to the entered IPSQ
6750 6750 * on success, or NULL on failure. The caller ensures ipif/ill is valid by
6751 6751 * refholding it as necessary. If the IPSQ cannot be entered and `func' is
6752 6752 * non-NULL, then `func' will be called back with `q' and `mp' once the IPSQ
6753 6753 * can be entered. If `func' is NULL, then `q' and `mp' are ignored.
6754 6754 */
6755 6755 ipsq_t *
6756 6756 ipsq_try_enter(ipif_t *ipif, ill_t *ill, queue_t *q, mblk_t *mp,
6757 6757 ipsq_func_t func, int type, boolean_t reentry_ok)
6758 6758 {
6759 6759 ip_stack_t *ipst;
6760 6760 ipsq_t *ipsq;
6761 6761
6762 6762 /* Only 1 of ipif or ill can be specified */
6763 6763 ASSERT((ipif != NULL) ^ (ill != NULL));
6764 6764
6765 6765 if (ipif != NULL)
6766 6766 ill = ipif->ipif_ill;
6767 6767 ipst = ill->ill_ipst;
6768 6768
6769 6769 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
6770 6770 ipsq = ipsq_try_enter_internal(ill, q, mp, func, type, reentry_ok);
6771 6771 rw_exit(&ipst->ips_ill_g_lock);
6772 6772
6773 6773 return (ipsq);
6774 6774 }
6775 6775
6776 6776 /*
6777 6777 * Try to enter the IPSQ corresponding to `ill' as writer. The caller ensures
6778 6778 * ill is valid by refholding it if necessary; we will refrele. If the IPSQ
6779 6779 * cannot be entered, the mp is queued for completion.
6780 6780 */
6781 6781 void
6782 6782 qwriter_ip(ill_t *ill, queue_t *q, mblk_t *mp, ipsq_func_t func, int type,
6783 6783 boolean_t reentry_ok)
6784 6784 {
6785 6785 ipsq_t *ipsq;
6786 6786
6787 6787 ipsq = ipsq_try_enter(NULL, ill, q, mp, func, type, reentry_ok);
6788 6788
6789 6789 /*
6790 6790 * Drop the caller's refhold on the ill. This is safe since we either
6791 6791 * entered the IPSQ (and thus are exclusive), or failed to enter the
6792 6792 * IPSQ, in which case we return without accessing ill anymore. This
6793 6793 * is needed because func needs to see the correct refcount.
6794 6794 * e.g. removeif can work only then.
6795 6795 */
6796 6796 ill_refrele(ill);
6797 6797 if (ipsq != NULL) {
6798 6798 (*func)(ipsq, q, mp, NULL);
6799 6799 ipsq_exit(ipsq);
6800 6800 }
6801 6801 }
6802 6802
6803 6803 /*
6804 6804 * Exit the specified IPSQ. If this is the final exit on it then drain it
6805 6805 * prior to exiting. Caller must be writer on the specified IPSQ.
6806 6806 */
6807 6807 void
6808 6808 ipsq_exit(ipsq_t *ipsq)
6809 6809 {
6810 6810 mblk_t *mp;
6811 6811 ipsq_t *mp_ipsq;
6812 6812 queue_t *q;
6813 6813 phyint_t *phyi;
6814 6814 ipsq_func_t func;
6815 6815
6816 6816 ASSERT(IAM_WRITER_IPSQ(ipsq));
6817 6817
6818 6818 ASSERT(ipsq->ipsq_xop->ipx_reentry_cnt >= 1);
6819 6819 if (ipsq->ipsq_xop->ipx_reentry_cnt != 1) {
6820 6820 ipsq->ipsq_xop->ipx_reentry_cnt--;
6821 6821 return;
6822 6822 }
6823 6823
6824 6824 for (;;) {
6825 6825 phyi = ipsq->ipsq_phyint;
6826 6826 mp = ipsq_dq(ipsq);
6827 6827 mp_ipsq = (mp == NULL) ? NULL : (ipsq_t *)mp->b_next;
6828 6828
6829 6829 /*
6830 6830 * If we've changed to a new IPSQ, and the phyint associated
6831 6831 * with the old one has gone away, free the old IPSQ. Note
6832 6832 * that this cannot happen while the IPSQ is in a group.
6833 6833 */
6834 6834 if (mp_ipsq != ipsq && phyi == NULL) {
6835 6835 ASSERT(ipsq->ipsq_next == ipsq);
6836 6836 ASSERT(ipsq->ipsq_xop == &ipsq->ipsq_ownxop);
6837 6837 ipsq_delete(ipsq);
6838 6838 }
6839 6839
6840 6840 if (mp == NULL)
6841 6841 break;
6842 6842
6843 6843 q = mp->b_queue;
6844 6844 func = (ipsq_func_t)mp->b_prev;
6845 6845 ipsq = mp_ipsq;
6846 6846 mp->b_next = mp->b_prev = NULL;
6847 6847 mp->b_queue = NULL;
6848 6848
6849 6849 /*
6850 6850 * If 'q' is an conn queue, it is valid, since we did a
6851 6851 * a refhold on the conn at the start of the ioctl.
6852 6852 * If 'q' is an ill queue, it is valid, since close of an
6853 6853 * ill will clean up its IPSQ.
6854 6854 */
6855 6855 (*func)(ipsq, q, mp, NULL);
6856 6856 }
6857 6857 }
6858 6858
6859 6859 /*
6860 6860 * Used to start any igmp or mld timers that could not be started
6861 6861 * while holding ill_mcast_lock. The timers can't be started while holding
6862 6862 * the lock, since mld/igmp_start_timers may need to call untimeout()
6863 6863 * which can't be done while holding the lock which the timeout handler
6864 6864 * acquires. Otherwise
6865 6865 * there could be a deadlock since the timeout handlers
6866 6866 * mld_timeout_handler_per_ill/igmp_timeout_handler_per_ill also acquire
6867 6867 * ill_mcast_lock.
6868 6868 */
6869 6869 void
6870 6870 ill_mcast_timer_start(ip_stack_t *ipst)
6871 6871 {
6872 6872 int next;
6873 6873
6874 6874 mutex_enter(&ipst->ips_igmp_timer_lock);
6875 6875 next = ipst->ips_igmp_deferred_next;
6876 6876 ipst->ips_igmp_deferred_next = INFINITY;
6877 6877 mutex_exit(&ipst->ips_igmp_timer_lock);
6878 6878
6879 6879 if (next != INFINITY)
6880 6880 igmp_start_timers(next, ipst);
6881 6881
6882 6882 mutex_enter(&ipst->ips_mld_timer_lock);
6883 6883 next = ipst->ips_mld_deferred_next;
6884 6884 ipst->ips_mld_deferred_next = INFINITY;
6885 6885 mutex_exit(&ipst->ips_mld_timer_lock);
6886 6886
6887 6887 if (next != INFINITY)
6888 6888 mld_start_timers(next, ipst);
6889 6889 }
6890 6890
6891 6891 /*
6892 6892 * Start the current exclusive operation on `ipsq'; associate it with `ipif'
6893 6893 * and `ioccmd'.
6894 6894 */
6895 6895 void
6896 6896 ipsq_current_start(ipsq_t *ipsq, ipif_t *ipif, int ioccmd)
6897 6897 {
6898 6898 ill_t *ill = ipif->ipif_ill;
6899 6899 ipxop_t *ipx = ipsq->ipsq_xop;
6900 6900
6901 6901 ASSERT(IAM_WRITER_IPSQ(ipsq));
6902 6902 ASSERT(ipx->ipx_current_ipif == NULL);
6903 6903 ASSERT(ipx->ipx_current_ioctl == 0);
6904 6904
6905 6905 ipx->ipx_current_done = B_FALSE;
6906 6906 ipx->ipx_current_ioctl = ioccmd;
6907 6907 mutex_enter(&ipx->ipx_lock);
6908 6908 ipx->ipx_current_ipif = ipif;
6909 6909 mutex_exit(&ipx->ipx_lock);
6910 6910
6911 6911 /*
6912 6912 * Set IPIF_CHANGING on one or more ipifs associated with the
6913 6913 * current exclusive operation. IPIF_CHANGING prevents any new
6914 6914 * references to the ipif (so that the references will eventually
6915 6915 * drop to zero) and also prevents any "get" operations (e.g.,
6916 6916 * SIOCGLIFFLAGS) from being able to access the ipif until the
6917 6917 * operation has completed and the ipif is again in a stable state.
6918 6918 *
6919 6919 * For ioctls, IPIF_CHANGING is set on the ipif associated with the
6920 6920 * ioctl. For internal operations (where ioccmd is zero), all ipifs
6921 6921 * on the ill are marked with IPIF_CHANGING since it's unclear which
6922 6922 * ipifs will be affected.
6923 6923 *
6924 6924 * Note that SIOCLIFREMOVEIF is a special case as it sets
6925 6925 * IPIF_CONDEMNED internally after identifying the right ipif to
6926 6926 * operate on.
6927 6927 */
6928 6928 switch (ioccmd) {
6929 6929 case SIOCLIFREMOVEIF:
6930 6930 break;
6931 6931 case 0:
6932 6932 mutex_enter(&ill->ill_lock);
6933 6933 ipif = ipif->ipif_ill->ill_ipif;
6934 6934 for (; ipif != NULL; ipif = ipif->ipif_next)
6935 6935 ipif->ipif_state_flags |= IPIF_CHANGING;
6936 6936 mutex_exit(&ill->ill_lock);
6937 6937 break;
6938 6938 default:
6939 6939 mutex_enter(&ill->ill_lock);
6940 6940 ipif->ipif_state_flags |= IPIF_CHANGING;
6941 6941 mutex_exit(&ill->ill_lock);
6942 6942 }
6943 6943 }
6944 6944
6945 6945 /*
6946 6946 * Finish the current exclusive operation on `ipsq'. Usually, this will allow
6947 6947 * the next exclusive operation to begin once we ipsq_exit(). However, if
6948 6948 * pending DLPI operations remain, then we will wait for the queue to drain
6949 6949 * before allowing the next exclusive operation to begin. This ensures that
6950 6950 * DLPI operations from one exclusive operation are never improperly processed
6951 6951 * as part of a subsequent exclusive operation.
6952 6952 */
6953 6953 void
6954 6954 ipsq_current_finish(ipsq_t *ipsq)
6955 6955 {
6956 6956 ipxop_t *ipx = ipsq->ipsq_xop;
6957 6957 t_uscalar_t dlpi_pending = DL_PRIM_INVAL;
6958 6958 ipif_t *ipif = ipx->ipx_current_ipif;
6959 6959
6960 6960 ASSERT(IAM_WRITER_IPSQ(ipsq));
6961 6961
6962 6962 /*
6963 6963 * For SIOCLIFREMOVEIF, the ipif has been already been blown away
6964 6964 * (but in that case, IPIF_CHANGING will already be clear and no
6965 6965 * pending DLPI messages can remain).
6966 6966 */
6967 6967 if (ipx->ipx_current_ioctl != SIOCLIFREMOVEIF) {
6968 6968 ill_t *ill = ipif->ipif_ill;
6969 6969
6970 6970 mutex_enter(&ill->ill_lock);
6971 6971 dlpi_pending = ill->ill_dlpi_pending;
6972 6972 if (ipx->ipx_current_ioctl == 0) {
6973 6973 ipif = ill->ill_ipif;
6974 6974 for (; ipif != NULL; ipif = ipif->ipif_next)
6975 6975 ipif->ipif_state_flags &= ~IPIF_CHANGING;
6976 6976 } else {
6977 6977 ipif->ipif_state_flags &= ~IPIF_CHANGING;
6978 6978 }
6979 6979 mutex_exit(&ill->ill_lock);
6980 6980 }
6981 6981
6982 6982 ASSERT(!ipx->ipx_current_done);
6983 6983 ipx->ipx_current_done = B_TRUE;
6984 6984 ipx->ipx_current_ioctl = 0;
6985 6985 if (dlpi_pending == DL_PRIM_INVAL) {
6986 6986 mutex_enter(&ipx->ipx_lock);
6987 6987 ipx->ipx_current_ipif = NULL;
6988 6988 mutex_exit(&ipx->ipx_lock);
6989 6989 }
6990 6990 }
6991 6991
6992 6992 /*
6993 6993 * The ill is closing. Flush all messages on the ipsq that originated
6994 6994 * from this ill. Usually there wont' be any messages on the ipsq_xopq_mphead
6995 6995 * for this ill since ipsq_enter could not have entered until then.
6996 6996 * New messages can't be queued since the CONDEMNED flag is set.
6997 6997 */
6998 6998 static void
6999 6999 ipsq_flush(ill_t *ill)
7000 7000 {
7001 7001 queue_t *q;
7002 7002 mblk_t *prev;
7003 7003 mblk_t *mp;
7004 7004 mblk_t *mp_next;
7005 7005 ipxop_t *ipx = ill->ill_phyint->phyint_ipsq->ipsq_xop;
7006 7006
7007 7007 ASSERT(IAM_WRITER_ILL(ill));
7008 7008
7009 7009 /*
7010 7010 * Flush any messages sent up by the driver.
7011 7011 */
7012 7012 mutex_enter(&ipx->ipx_lock);
7013 7013 for (prev = NULL, mp = ipx->ipx_mphead; mp != NULL; mp = mp_next) {
7014 7014 mp_next = mp->b_next;
7015 7015 q = mp->b_queue;
7016 7016 if (q == ill->ill_rq || q == ill->ill_wq) {
7017 7017 /* dequeue mp */
7018 7018 if (prev == NULL)
7019 7019 ipx->ipx_mphead = mp->b_next;
7020 7020 else
7021 7021 prev->b_next = mp->b_next;
7022 7022 if (ipx->ipx_mptail == mp) {
7023 7023 ASSERT(mp_next == NULL);
7024 7024 ipx->ipx_mptail = prev;
7025 7025 }
7026 7026 inet_freemsg(mp);
7027 7027 } else {
7028 7028 prev = mp;
7029 7029 }
7030 7030 }
7031 7031 mutex_exit(&ipx->ipx_lock);
7032 7032 (void) ipsq_pending_mp_cleanup(ill, NULL);
7033 7033 ipsq_xopq_mp_cleanup(ill, NULL);
7034 7034 }
7035 7035
7036 7036 /*
7037 7037 * Parse an ifreq or lifreq struct coming down ioctls and refhold
7038 7038 * and return the associated ipif.
7039 7039 * Return value:
7040 7040 * Non zero: An error has occurred. ci may not be filled out.
7041 7041 * zero : ci is filled out with the ioctl cmd in ci.ci_name, and
7042 7042 * a held ipif in ci.ci_ipif.
7043 7043 */
7044 7044 int
7045 7045 ip_extract_lifreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
7046 7046 cmd_info_t *ci)
7047 7047 {
7048 7048 char *name;
7049 7049 struct ifreq *ifr;
7050 7050 struct lifreq *lifr;
7051 7051 ipif_t *ipif = NULL;
7052 7052 ill_t *ill;
7053 7053 conn_t *connp;
7054 7054 boolean_t isv6;
7055 7055 int err;
7056 7056 mblk_t *mp1;
7057 7057 zoneid_t zoneid;
7058 7058 ip_stack_t *ipst;
7059 7059
7060 7060 if (q->q_next != NULL) {
7061 7061 ill = (ill_t *)q->q_ptr;
7062 7062 isv6 = ill->ill_isv6;
7063 7063 connp = NULL;
7064 7064 zoneid = ALL_ZONES;
7065 7065 ipst = ill->ill_ipst;
7066 7066 } else {
7067 7067 ill = NULL;
7068 7068 connp = Q_TO_CONN(q);
7069 7069 isv6 = (connp->conn_family == AF_INET6);
7070 7070 zoneid = connp->conn_zoneid;
7071 7071 if (zoneid == GLOBAL_ZONEID) {
7072 7072 /* global zone can access ipifs in all zones */
7073 7073 zoneid = ALL_ZONES;
7074 7074 }
7075 7075 ipst = connp->conn_netstack->netstack_ip;
7076 7076 }
7077 7077
7078 7078 /* Has been checked in ip_wput_nondata */
7079 7079 mp1 = mp->b_cont->b_cont;
7080 7080
7081 7081 if (ipip->ipi_cmd_type == IF_CMD) {
7082 7082 /* This a old style SIOC[GS]IF* command */
7083 7083 ifr = (struct ifreq *)mp1->b_rptr;
7084 7084 /*
7085 7085 * Null terminate the string to protect against buffer
7086 7086 * overrun. String was generated by user code and may not
7087 7087 * be trusted.
7088 7088 */
7089 7089 ifr->ifr_name[IFNAMSIZ - 1] = '\0';
7090 7090 name = ifr->ifr_name;
7091 7091 ci->ci_sin = (sin_t *)&ifr->ifr_addr;
7092 7092 ci->ci_sin6 = NULL;
7093 7093 ci->ci_lifr = (struct lifreq *)ifr;
7094 7094 } else {
7095 7095 /* This a new style SIOC[GS]LIF* command */
7096 7096 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
7097 7097 lifr = (struct lifreq *)mp1->b_rptr;
7098 7098 /*
7099 7099 * Null terminate the string to protect against buffer
7100 7100 * overrun. String was generated by user code and may not
7101 7101 * be trusted.
7102 7102 */
7103 7103 lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
7104 7104 name = lifr->lifr_name;
7105 7105 ci->ci_sin = (sin_t *)&lifr->lifr_addr;
7106 7106 ci->ci_sin6 = (sin6_t *)&lifr->lifr_addr;
7107 7107 ci->ci_lifr = lifr;
7108 7108 }
7109 7109
7110 7110 if (ipip->ipi_cmd == SIOCSLIFNAME) {
7111 7111 /*
7112 7112 * The ioctl will be failed if the ioctl comes down
7113 7113 * an conn stream
7114 7114 */
7115 7115 if (ill == NULL) {
7116 7116 /*
7117 7117 * Not an ill queue, return EINVAL same as the
7118 7118 * old error code.
7119 7119 */
7120 7120 return (ENXIO);
7121 7121 }
7122 7122 ipif = ill->ill_ipif;
7123 7123 ipif_refhold(ipif);
7124 7124 } else {
7125 7125 /*
7126 7126 * Ensure that ioctls don't see any internal state changes
7127 7127 * caused by set ioctls by deferring them if IPIF_CHANGING is
7128 7128 * set.
7129 7129 */
7130 7130 ipif = ipif_lookup_on_name_async(name, mi_strlen(name),
7131 7131 isv6, zoneid, q, mp, ip_process_ioctl, &err, ipst);
7132 7132 if (ipif == NULL) {
7133 7133 if (err == EINPROGRESS)
7134 7134 return (err);
7135 7135 err = 0; /* Ensure we don't use it below */
7136 7136 }
7137 7137 }
7138 7138
7139 7139 /*
7140 7140 * Old style [GS]IFCMD does not admit IPv6 ipif
7141 7141 */
7142 7142 if (ipif != NULL && ipif->ipif_isv6 && ipip->ipi_cmd_type == IF_CMD) {
7143 7143 ipif_refrele(ipif);
7144 7144 return (ENXIO);
7145 7145 }
7146 7146
7147 7147 if (ipif == NULL && ill != NULL && ill->ill_ipif != NULL &&
7148 7148 name[0] == '\0') {
7149 7149 /*
7150 7150 * Handle a or a SIOC?IF* with a null name
7151 7151 * during plumb (on the ill queue before the I_PLINK).
7152 7152 */
7153 7153 ipif = ill->ill_ipif;
7154 7154 ipif_refhold(ipif);
7155 7155 }
7156 7156
7157 7157 if (ipif == NULL)
7158 7158 return (ENXIO);
7159 7159
7160 7160 DTRACE_PROBE4(ipif__ioctl, char *, "ip_extract_lifreq",
7161 7161 int, ipip->ipi_cmd, ill_t *, ipif->ipif_ill, ipif_t *, ipif);
7162 7162
7163 7163 ci->ci_ipif = ipif;
7164 7164 return (0);
7165 7165 }
7166 7166
7167 7167 /*
7168 7168 * Return the total number of ipifs.
7169 7169 */
7170 7170 static uint_t
7171 7171 ip_get_numifs(zoneid_t zoneid, ip_stack_t *ipst)
7172 7172 {
7173 7173 uint_t numifs = 0;
7174 7174 ill_t *ill;
7175 7175 ill_walk_context_t ctx;
7176 7176 ipif_t *ipif;
7177 7177
7178 7178 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7179 7179 ill = ILL_START_WALK_V4(&ctx, ipst);
7180 7180 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7181 7181 if (IS_UNDER_IPMP(ill))
7182 7182 continue;
7183 7183 for (ipif = ill->ill_ipif; ipif != NULL;
7184 7184 ipif = ipif->ipif_next) {
7185 7185 if (ipif->ipif_zoneid == zoneid ||
7186 7186 ipif->ipif_zoneid == ALL_ZONES)
7187 7187 numifs++;
7188 7188 }
7189 7189 }
7190 7190 rw_exit(&ipst->ips_ill_g_lock);
7191 7191 return (numifs);
7192 7192 }
7193 7193
7194 7194 /*
7195 7195 * Return the total number of ipifs.
7196 7196 */
7197 7197 static uint_t
7198 7198 ip_get_numlifs(int family, int lifn_flags, zoneid_t zoneid, ip_stack_t *ipst)
7199 7199 {
7200 7200 uint_t numifs = 0;
7201 7201 ill_t *ill;
7202 7202 ipif_t *ipif;
7203 7203 ill_walk_context_t ctx;
7204 7204
7205 7205 ip1dbg(("ip_get_numlifs(%d %u %d)\n", family, lifn_flags, (int)zoneid));
7206 7206
7207 7207 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7208 7208 if (family == AF_INET)
7209 7209 ill = ILL_START_WALK_V4(&ctx, ipst);
7210 7210 else if (family == AF_INET6)
7211 7211 ill = ILL_START_WALK_V6(&ctx, ipst);
7212 7212 else
7213 7213 ill = ILL_START_WALK_ALL(&ctx, ipst);
7214 7214
7215 7215 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7216 7216 if (IS_UNDER_IPMP(ill) && !(lifn_flags & LIFC_UNDER_IPMP))
7217 7217 continue;
7218 7218
7219 7219 for (ipif = ill->ill_ipif; ipif != NULL;
7220 7220 ipif = ipif->ipif_next) {
7221 7221 if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7222 7222 !(lifn_flags & LIFC_NOXMIT))
7223 7223 continue;
7224 7224 if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7225 7225 !(lifn_flags & LIFC_TEMPORARY))
7226 7226 continue;
7227 7227 if (((ipif->ipif_flags &
7228 7228 (IPIF_NOXMIT|IPIF_NOLOCAL|
7229 7229 IPIF_DEPRECATED)) ||
7230 7230 IS_LOOPBACK(ill) ||
7231 7231 !(ipif->ipif_flags & IPIF_UP)) &&
7232 7232 (lifn_flags & LIFC_EXTERNAL_SOURCE))
7233 7233 continue;
7234 7234
7235 7235 if (zoneid != ipif->ipif_zoneid &&
7236 7236 ipif->ipif_zoneid != ALL_ZONES &&
7237 7237 (zoneid != GLOBAL_ZONEID ||
7238 7238 !(lifn_flags & LIFC_ALLZONES)))
7239 7239 continue;
7240 7240
7241 7241 numifs++;
7242 7242 }
7243 7243 }
7244 7244 rw_exit(&ipst->ips_ill_g_lock);
7245 7245 return (numifs);
7246 7246 }
7247 7247
7248 7248 uint_t
7249 7249 ip_get_lifsrcofnum(ill_t *ill)
7250 7250 {
7251 7251 uint_t numifs = 0;
7252 7252 ill_t *ill_head = ill;
7253 7253 ip_stack_t *ipst = ill->ill_ipst;
7254 7254
7255 7255 /*
7256 7256 * ill_g_usesrc_lock protects ill_usesrc_grp_next, for example, some
7257 7257 * other thread may be trying to relink the ILLs in this usesrc group
7258 7258 * and adjusting the ill_usesrc_grp_next pointers
7259 7259 */
7260 7260 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7261 7261 if ((ill->ill_usesrc_ifindex == 0) &&
7262 7262 (ill->ill_usesrc_grp_next != NULL)) {
7263 7263 for (; (ill != NULL) && (ill->ill_usesrc_grp_next != ill_head);
7264 7264 ill = ill->ill_usesrc_grp_next)
7265 7265 numifs++;
7266 7266 }
7267 7267 rw_exit(&ipst->ips_ill_g_usesrc_lock);
7268 7268
7269 7269 return (numifs);
7270 7270 }
7271 7271
7272 7272 /* Null values are passed in for ipif, sin, and ifreq */
7273 7273 /* ARGSUSED */
7274 7274 int
7275 7275 ip_sioctl_get_ifnum(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7276 7276 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7277 7277 {
7278 7278 int *nump;
7279 7279 conn_t *connp = Q_TO_CONN(q);
7280 7280
7281 7281 ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7282 7282
7283 7283 /* Existence of b_cont->b_cont checked in ip_wput_nondata */
7284 7284 nump = (int *)mp->b_cont->b_cont->b_rptr;
7285 7285
7286 7286 *nump = ip_get_numifs(connp->conn_zoneid,
7287 7287 connp->conn_netstack->netstack_ip);
7288 7288 ip1dbg(("ip_sioctl_get_ifnum numifs %d", *nump));
7289 7289 return (0);
7290 7290 }
7291 7291
7292 7292 /* Null values are passed in for ipif, sin, and ifreq */
7293 7293 /* ARGSUSED */
7294 7294 int
7295 7295 ip_sioctl_get_lifnum(ipif_t *dummy_ipif, sin_t *dummy_sin,
7296 7296 queue_t *q, mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7297 7297 {
7298 7298 struct lifnum *lifn;
7299 7299 mblk_t *mp1;
7300 7300 conn_t *connp = Q_TO_CONN(q);
7301 7301
7302 7302 ASSERT(q->q_next == NULL); /* not a valid ioctl for ip as a module */
7303 7303
7304 7304 /* Existence checked in ip_wput_nondata */
7305 7305 mp1 = mp->b_cont->b_cont;
7306 7306
7307 7307 lifn = (struct lifnum *)mp1->b_rptr;
7308 7308 switch (lifn->lifn_family) {
7309 7309 case AF_UNSPEC:
7310 7310 case AF_INET:
7311 7311 case AF_INET6:
7312 7312 break;
7313 7313 default:
7314 7314 return (EAFNOSUPPORT);
7315 7315 }
7316 7316
7317 7317 lifn->lifn_count = ip_get_numlifs(lifn->lifn_family, lifn->lifn_flags,
7318 7318 connp->conn_zoneid, connp->conn_netstack->netstack_ip);
7319 7319 ip1dbg(("ip_sioctl_get_lifnum numifs %d", lifn->lifn_count));
7320 7320 return (0);
7321 7321 }
7322 7322
7323 7323 /* ARGSUSED */
7324 7324 int
7325 7325 ip_sioctl_get_ifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7326 7326 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7327 7327 {
7328 7328 STRUCT_HANDLE(ifconf, ifc);
7329 7329 mblk_t *mp1;
7330 7330 struct iocblk *iocp;
7331 7331 struct ifreq *ifr;
7332 7332 ill_walk_context_t ctx;
7333 7333 ill_t *ill;
7334 7334 ipif_t *ipif;
7335 7335 struct sockaddr_in *sin;
7336 7336 int32_t ifclen;
7337 7337 zoneid_t zoneid;
7338 7338 ip_stack_t *ipst = CONNQ_TO_IPST(q);
7339 7339
7340 7340 ASSERT(q->q_next == NULL); /* not valid ioctls for ip as a module */
7341 7341
7342 7342 ip1dbg(("ip_sioctl_get_ifconf"));
7343 7343 /* Existence verified in ip_wput_nondata */
7344 7344 mp1 = mp->b_cont->b_cont;
7345 7345 iocp = (struct iocblk *)mp->b_rptr;
7346 7346 zoneid = Q_TO_CONN(q)->conn_zoneid;
7347 7347
7348 7348 /*
7349 7349 * The original SIOCGIFCONF passed in a struct ifconf which specified
7350 7350 * the user buffer address and length into which the list of struct
7351 7351 * ifreqs was to be copied. Since AT&T Streams does not seem to
7352 7352 * allow M_COPYOUT to be used in conjunction with I_STR IOCTLS,
7353 7353 * the SIOCGIFCONF operation was redefined to simply provide
7354 7354 * a large output buffer into which we are supposed to jam the ifreq
7355 7355 * array. The same ioctl command code was used, despite the fact that
7356 7356 * both the applications and the kernel code had to change, thus making
7357 7357 * it impossible to support both interfaces.
7358 7358 *
7359 7359 * For reasons not good enough to try to explain, the following
7360 7360 * algorithm is used for deciding what to do with one of these:
7361 7361 * If the IOCTL comes in as an I_STR, it is assumed to be of the new
7362 7362 * form with the output buffer coming down as the continuation message.
7363 7363 * If it arrives as a TRANSPARENT IOCTL, it is assumed to be old style,
7364 7364 * and we have to copy in the ifconf structure to find out how big the
7365 7365 * output buffer is and where to copy out to. Sure no problem...
7366 7366 *
7367 7367 */
7368 7368 STRUCT_SET_HANDLE(ifc, iocp->ioc_flag, NULL);
7369 7369 if ((mp1->b_wptr - mp1->b_rptr) == STRUCT_SIZE(ifc)) {
7370 7370 int numifs = 0;
7371 7371 size_t ifc_bufsize;
7372 7372
7373 7373 /*
7374 7374 * Must be (better be!) continuation of a TRANSPARENT
7375 7375 * IOCTL. We just copied in the ifconf structure.
7376 7376 */
7377 7377 STRUCT_SET_HANDLE(ifc, iocp->ioc_flag,
7378 7378 (struct ifconf *)mp1->b_rptr);
7379 7379
7380 7380 /*
7381 7381 * Allocate a buffer to hold requested information.
7382 7382 *
7383 7383 * If ifc_len is larger than what is needed, we only
7384 7384 * allocate what we will use.
7385 7385 *
7386 7386 * If ifc_len is smaller than what is needed, return
7387 7387 * EINVAL.
7388 7388 *
7389 7389 * XXX: the ill_t structure can hava 2 counters, for
7390 7390 * v4 and v6 (not just ill_ipif_up_count) to store the
7391 7391 * number of interfaces for a device, so we don't need
7392 7392 * to count them here...
7393 7393 */
7394 7394 numifs = ip_get_numifs(zoneid, ipst);
7395 7395
7396 7396 ifclen = STRUCT_FGET(ifc, ifc_len);
7397 7397 ifc_bufsize = numifs * sizeof (struct ifreq);
7398 7398 if (ifc_bufsize > ifclen) {
7399 7399 if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7400 7400 /* old behaviour */
7401 7401 return (EINVAL);
7402 7402 } else {
7403 7403 ifc_bufsize = ifclen;
7404 7404 }
7405 7405 }
7406 7406
7407 7407 mp1 = mi_copyout_alloc(q, mp,
7408 7408 STRUCT_FGETP(ifc, ifc_buf), ifc_bufsize, B_FALSE);
7409 7409 if (mp1 == NULL)
7410 7410 return (ENOMEM);
7411 7411
7412 7412 mp1->b_wptr = mp1->b_rptr + ifc_bufsize;
7413 7413 }
7414 7414 bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7415 7415 /*
7416 7416 * the SIOCGIFCONF ioctl only knows about
7417 7417 * IPv4 addresses, so don't try to tell
7418 7418 * it about interfaces with IPv6-only
7419 7419 * addresses. (Last parm 'isv6' is B_FALSE)
7420 7420 */
7421 7421
7422 7422 ifr = (struct ifreq *)mp1->b_rptr;
7423 7423
7424 7424 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7425 7425 ill = ILL_START_WALK_V4(&ctx, ipst);
7426 7426 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7427 7427 if (IS_UNDER_IPMP(ill))
7428 7428 continue;
7429 7429 for (ipif = ill->ill_ipif; ipif != NULL;
7430 7430 ipif = ipif->ipif_next) {
7431 7431 if (zoneid != ipif->ipif_zoneid &&
7432 7432 ipif->ipif_zoneid != ALL_ZONES)
7433 7433 continue;
7434 7434 if ((uchar_t *)&ifr[1] > mp1->b_wptr) {
7435 7435 if (iocp->ioc_cmd == O_SIOCGIFCONF) {
7436 7436 /* old behaviour */
7437 7437 rw_exit(&ipst->ips_ill_g_lock);
7438 7438 return (EINVAL);
7439 7439 } else {
7440 7440 goto if_copydone;
7441 7441 }
7442 7442 }
7443 7443 ipif_get_name(ipif, ifr->ifr_name,
7444 7444 sizeof (ifr->ifr_name));
7445 7445 sin = (sin_t *)&ifr->ifr_addr;
7446 7446 *sin = sin_null;
7447 7447 sin->sin_family = AF_INET;
7448 7448 sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7449 7449 ifr++;
7450 7450 }
7451 7451 }
7452 7452 if_copydone:
7453 7453 rw_exit(&ipst->ips_ill_g_lock);
7454 7454 mp1->b_wptr = (uchar_t *)ifr;
7455 7455
7456 7456 if (STRUCT_BUF(ifc) != NULL) {
7457 7457 STRUCT_FSET(ifc, ifc_len,
7458 7458 (int)((uchar_t *)ifr - mp1->b_rptr));
7459 7459 }
7460 7460 return (0);
7461 7461 }
7462 7462
7463 7463 /*
7464 7464 * Get the interfaces using the address hosted on the interface passed in,
7465 7465 * as a source adddress
7466 7466 */
7467 7467 /* ARGSUSED */
7468 7468 int
7469 7469 ip_sioctl_get_lifsrcof(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7470 7470 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7471 7471 {
7472 7472 mblk_t *mp1;
7473 7473 ill_t *ill, *ill_head;
7474 7474 ipif_t *ipif, *orig_ipif;
7475 7475 int numlifs = 0;
7476 7476 size_t lifs_bufsize, lifsmaxlen;
7477 7477 struct lifreq *lifr;
7478 7478 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7479 7479 uint_t ifindex;
7480 7480 zoneid_t zoneid;
7481 7481 boolean_t isv6 = B_FALSE;
7482 7482 struct sockaddr_in *sin;
7483 7483 struct sockaddr_in6 *sin6;
7484 7484 STRUCT_HANDLE(lifsrcof, lifs);
7485 7485 ip_stack_t *ipst;
7486 7486
7487 7487 ipst = CONNQ_TO_IPST(q);
7488 7488
7489 7489 ASSERT(q->q_next == NULL);
7490 7490
7491 7491 zoneid = Q_TO_CONN(q)->conn_zoneid;
7492 7492
7493 7493 /* Existence verified in ip_wput_nondata */
7494 7494 mp1 = mp->b_cont->b_cont;
7495 7495
7496 7496 /*
7497 7497 * Must be (better be!) continuation of a TRANSPARENT
7498 7498 * IOCTL. We just copied in the lifsrcof structure.
7499 7499 */
7500 7500 STRUCT_SET_HANDLE(lifs, iocp->ioc_flag,
7501 7501 (struct lifsrcof *)mp1->b_rptr);
7502 7502
7503 7503 if (MBLKL(mp1) != STRUCT_SIZE(lifs))
7504 7504 return (EINVAL);
7505 7505
7506 7506 ifindex = STRUCT_FGET(lifs, lifs_ifindex);
7507 7507 isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
7508 7508 ipif = ipif_lookup_on_ifindex(ifindex, isv6, zoneid, ipst);
7509 7509 if (ipif == NULL) {
7510 7510 ip1dbg(("ip_sioctl_get_lifsrcof: no ipif for ifindex %d\n",
7511 7511 ifindex));
7512 7512 return (ENXIO);
7513 7513 }
7514 7514
7515 7515 /* Allocate a buffer to hold requested information */
7516 7516 numlifs = ip_get_lifsrcofnum(ipif->ipif_ill);
7517 7517 lifs_bufsize = numlifs * sizeof (struct lifreq);
7518 7518 lifsmaxlen = STRUCT_FGET(lifs, lifs_maxlen);
7519 7519 /* The actual size needed is always returned in lifs_len */
7520 7520 STRUCT_FSET(lifs, lifs_len, lifs_bufsize);
7521 7521
7522 7522 /* If the amount we need is more than what is passed in, abort */
7523 7523 if (lifs_bufsize > lifsmaxlen || lifs_bufsize == 0) {
7524 7524 ipif_refrele(ipif);
7525 7525 return (0);
7526 7526 }
7527 7527
7528 7528 mp1 = mi_copyout_alloc(q, mp,
7529 7529 STRUCT_FGETP(lifs, lifs_buf), lifs_bufsize, B_FALSE);
7530 7530 if (mp1 == NULL) {
7531 7531 ipif_refrele(ipif);
7532 7532 return (ENOMEM);
7533 7533 }
7534 7534
7535 7535 mp1->b_wptr = mp1->b_rptr + lifs_bufsize;
7536 7536 bzero(mp1->b_rptr, lifs_bufsize);
7537 7537
7538 7538 lifr = (struct lifreq *)mp1->b_rptr;
7539 7539
7540 7540 ill = ill_head = ipif->ipif_ill;
7541 7541 orig_ipif = ipif;
7542 7542
7543 7543 /* ill_g_usesrc_lock protects ill_usesrc_grp_next */
7544 7544 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_READER);
7545 7545 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7546 7546
7547 7547 ill = ill->ill_usesrc_grp_next; /* start from next ill */
7548 7548 for (; (ill != NULL) && (ill != ill_head);
7549 7549 ill = ill->ill_usesrc_grp_next) {
7550 7550
7551 7551 if ((uchar_t *)&lifr[1] > mp1->b_wptr)
7552 7552 break;
7553 7553
7554 7554 ipif = ill->ill_ipif;
7555 7555 ipif_get_name(ipif, lifr->lifr_name, sizeof (lifr->lifr_name));
7556 7556 if (ipif->ipif_isv6) {
7557 7557 sin6 = (sin6_t *)&lifr->lifr_addr;
7558 7558 *sin6 = sin6_null;
7559 7559 sin6->sin6_family = AF_INET6;
7560 7560 sin6->sin6_addr = ipif->ipif_v6lcl_addr;
7561 7561 lifr->lifr_addrlen = ip_mask_to_plen_v6(
7562 7562 &ipif->ipif_v6net_mask);
7563 7563 } else {
7564 7564 sin = (sin_t *)&lifr->lifr_addr;
7565 7565 *sin = sin_null;
7566 7566 sin->sin_family = AF_INET;
7567 7567 sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
7568 7568 lifr->lifr_addrlen = ip_mask_to_plen(
7569 7569 ipif->ipif_net_mask);
7570 7570 }
7571 7571 lifr++;
7572 7572 }
7573 7573 rw_exit(&ipst->ips_ill_g_lock);
7574 7574 rw_exit(&ipst->ips_ill_g_usesrc_lock);
7575 7575 ipif_refrele(orig_ipif);
7576 7576 mp1->b_wptr = (uchar_t *)lifr;
7577 7577 STRUCT_FSET(lifs, lifs_len, (int)((uchar_t *)lifr - mp1->b_rptr));
7578 7578
7579 7579 return (0);
7580 7580 }
7581 7581
7582 7582 /* ARGSUSED */
7583 7583 int
7584 7584 ip_sioctl_get_lifconf(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q,
7585 7585 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *ifreq)
7586 7586 {
7587 7587 mblk_t *mp1;
7588 7588 int list;
7589 7589 ill_t *ill;
7590 7590 ipif_t *ipif;
7591 7591 int flags;
7592 7592 int numlifs = 0;
7593 7593 size_t lifc_bufsize;
7594 7594 struct lifreq *lifr;
7595 7595 sa_family_t family;
7596 7596 struct sockaddr_in *sin;
7597 7597 struct sockaddr_in6 *sin6;
7598 7598 ill_walk_context_t ctx;
7599 7599 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7600 7600 int32_t lifclen;
7601 7601 zoneid_t zoneid;
7602 7602 STRUCT_HANDLE(lifconf, lifc);
7603 7603 ip_stack_t *ipst = CONNQ_TO_IPST(q);
7604 7604
7605 7605 ip1dbg(("ip_sioctl_get_lifconf"));
7606 7606
7607 7607 ASSERT(q->q_next == NULL);
7608 7608
7609 7609 zoneid = Q_TO_CONN(q)->conn_zoneid;
7610 7610
7611 7611 /* Existence verified in ip_wput_nondata */
7612 7612 mp1 = mp->b_cont->b_cont;
7613 7613
7614 7614 /*
7615 7615 * An extended version of SIOCGIFCONF that takes an
7616 7616 * additional address family and flags field.
7617 7617 * AF_UNSPEC retrieve both IPv4 and IPv6.
7618 7618 * Unless LIFC_NOXMIT is specified the IPIF_NOXMIT
7619 7619 * interfaces are omitted.
7620 7620 * Similarly, IPIF_TEMPORARY interfaces are omitted
7621 7621 * unless LIFC_TEMPORARY is specified.
7622 7622 * If LIFC_EXTERNAL_SOURCE is specified, IPIF_NOXMIT,
7623 7623 * IPIF_NOLOCAL, PHYI_LOOPBACK, IPIF_DEPRECATED and
7624 7624 * not IPIF_UP interfaces are omitted. LIFC_EXTERNAL_SOURCE
7625 7625 * has priority over LIFC_NOXMIT.
7626 7626 */
7627 7627 STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, NULL);
7628 7628
7629 7629 if ((mp1->b_wptr - mp1->b_rptr) != STRUCT_SIZE(lifc))
7630 7630 return (EINVAL);
7631 7631
7632 7632 /*
7633 7633 * Must be (better be!) continuation of a TRANSPARENT
7634 7634 * IOCTL. We just copied in the lifconf structure.
7635 7635 */
7636 7636 STRUCT_SET_HANDLE(lifc, iocp->ioc_flag, (struct lifconf *)mp1->b_rptr);
7637 7637
7638 7638 family = STRUCT_FGET(lifc, lifc_family);
7639 7639 flags = STRUCT_FGET(lifc, lifc_flags);
7640 7640
7641 7641 switch (family) {
7642 7642 case AF_UNSPEC:
7643 7643 /*
7644 7644 * walk all ILL's.
7645 7645 */
7646 7646 list = MAX_G_HEADS;
7647 7647 break;
7648 7648 case AF_INET:
7649 7649 /*
7650 7650 * walk only IPV4 ILL's.
7651 7651 */
7652 7652 list = IP_V4_G_HEAD;
7653 7653 break;
7654 7654 case AF_INET6:
7655 7655 /*
7656 7656 * walk only IPV6 ILL's.
7657 7657 */
7658 7658 list = IP_V6_G_HEAD;
7659 7659 break;
7660 7660 default:
7661 7661 return (EAFNOSUPPORT);
7662 7662 }
7663 7663
7664 7664 /*
7665 7665 * Allocate a buffer to hold requested information.
7666 7666 *
7667 7667 * If lifc_len is larger than what is needed, we only
7668 7668 * allocate what we will use.
7669 7669 *
7670 7670 * If lifc_len is smaller than what is needed, return
7671 7671 * EINVAL.
7672 7672 */
7673 7673 numlifs = ip_get_numlifs(family, flags, zoneid, ipst);
7674 7674 lifc_bufsize = numlifs * sizeof (struct lifreq);
7675 7675 lifclen = STRUCT_FGET(lifc, lifc_len);
7676 7676 if (lifc_bufsize > lifclen) {
7677 7677 if (iocp->ioc_cmd == O_SIOCGLIFCONF)
7678 7678 return (EINVAL);
7679 7679 else
7680 7680 lifc_bufsize = lifclen;
7681 7681 }
7682 7682
7683 7683 mp1 = mi_copyout_alloc(q, mp,
7684 7684 STRUCT_FGETP(lifc, lifc_buf), lifc_bufsize, B_FALSE);
7685 7685 if (mp1 == NULL)
7686 7686 return (ENOMEM);
7687 7687
7688 7688 mp1->b_wptr = mp1->b_rptr + lifc_bufsize;
7689 7689 bzero(mp1->b_rptr, mp1->b_wptr - mp1->b_rptr);
7690 7690
7691 7691 lifr = (struct lifreq *)mp1->b_rptr;
7692 7692
7693 7693 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
7694 7694 ill = ill_first(list, list, &ctx, ipst);
7695 7695 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
7696 7696 if (IS_UNDER_IPMP(ill) && !(flags & LIFC_UNDER_IPMP))
7697 7697 continue;
7698 7698
7699 7699 for (ipif = ill->ill_ipif; ipif != NULL;
7700 7700 ipif = ipif->ipif_next) {
7701 7701 if ((ipif->ipif_flags & IPIF_NOXMIT) &&
7702 7702 !(flags & LIFC_NOXMIT))
7703 7703 continue;
7704 7704
7705 7705 if ((ipif->ipif_flags & IPIF_TEMPORARY) &&
7706 7706 !(flags & LIFC_TEMPORARY))
7707 7707 continue;
7708 7708
7709 7709 if (((ipif->ipif_flags &
7710 7710 (IPIF_NOXMIT|IPIF_NOLOCAL|
7711 7711 IPIF_DEPRECATED)) ||
7712 7712 IS_LOOPBACK(ill) ||
7713 7713 !(ipif->ipif_flags & IPIF_UP)) &&
7714 7714 (flags & LIFC_EXTERNAL_SOURCE))
7715 7715 continue;
7716 7716
7717 7717 if (zoneid != ipif->ipif_zoneid &&
7718 7718 ipif->ipif_zoneid != ALL_ZONES &&
7719 7719 (zoneid != GLOBAL_ZONEID ||
7720 7720 !(flags & LIFC_ALLZONES)))
7721 7721 continue;
7722 7722
7723 7723 if ((uchar_t *)&lifr[1] > mp1->b_wptr) {
7724 7724 if (iocp->ioc_cmd == O_SIOCGLIFCONF) {
7725 7725 rw_exit(&ipst->ips_ill_g_lock);
7726 7726 return (EINVAL);
7727 7727 } else {
7728 7728 goto lif_copydone;
7729 7729 }
7730 7730 }
7731 7731
7732 7732 ipif_get_name(ipif, lifr->lifr_name,
7733 7733 sizeof (lifr->lifr_name));
7734 7734 lifr->lifr_type = ill->ill_type;
7735 7735 if (ipif->ipif_isv6) {
7736 7736 sin6 = (sin6_t *)&lifr->lifr_addr;
7737 7737 *sin6 = sin6_null;
7738 7738 sin6->sin6_family = AF_INET6;
7739 7739 sin6->sin6_addr =
7740 7740 ipif->ipif_v6lcl_addr;
7741 7741 lifr->lifr_addrlen =
7742 7742 ip_mask_to_plen_v6(
7743 7743 &ipif->ipif_v6net_mask);
7744 7744 } else {
7745 7745 sin = (sin_t *)&lifr->lifr_addr;
7746 7746 *sin = sin_null;
7747 7747 sin->sin_family = AF_INET;
7748 7748 sin->sin_addr.s_addr =
7749 7749 ipif->ipif_lcl_addr;
7750 7750 lifr->lifr_addrlen =
7751 7751 ip_mask_to_plen(
7752 7752 ipif->ipif_net_mask);
7753 7753 }
7754 7754 lifr++;
7755 7755 }
7756 7756 }
7757 7757 lif_copydone:
7758 7758 rw_exit(&ipst->ips_ill_g_lock);
7759 7759
7760 7760 mp1->b_wptr = (uchar_t *)lifr;
7761 7761 if (STRUCT_BUF(lifc) != NULL) {
7762 7762 STRUCT_FSET(lifc, lifc_len,
7763 7763 (int)((uchar_t *)lifr - mp1->b_rptr));
7764 7764 }
7765 7765 return (0);
7766 7766 }
7767 7767
7768 7768 static void
7769 7769 ip_sioctl_ip6addrpolicy(queue_t *q, mblk_t *mp)
7770 7770 {
7771 7771 ip6_asp_t *table;
7772 7772 size_t table_size;
7773 7773 mblk_t *data_mp;
7774 7774 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7775 7775 ip_stack_t *ipst;
7776 7776
7777 7777 if (q->q_next == NULL)
7778 7778 ipst = CONNQ_TO_IPST(q);
7779 7779 else
7780 7780 ipst = ILLQ_TO_IPST(q);
7781 7781
7782 7782 /* These two ioctls are I_STR only */
7783 7783 if (iocp->ioc_count == TRANSPARENT) {
7784 7784 miocnak(q, mp, 0, EINVAL);
7785 7785 return;
7786 7786 }
7787 7787
7788 7788 data_mp = mp->b_cont;
7789 7789 if (data_mp == NULL) {
7790 7790 /* The user passed us a NULL argument */
7791 7791 table = NULL;
7792 7792 table_size = iocp->ioc_count;
7793 7793 } else {
7794 7794 /*
7795 7795 * The user provided a table. The stream head
7796 7796 * may have copied in the user data in chunks,
7797 7797 * so make sure everything is pulled up
7798 7798 * properly.
7799 7799 */
7800 7800 if (MBLKL(data_mp) < iocp->ioc_count) {
7801 7801 mblk_t *new_data_mp;
7802 7802 if ((new_data_mp = msgpullup(data_mp, -1)) ==
7803 7803 NULL) {
7804 7804 miocnak(q, mp, 0, ENOMEM);
7805 7805 return;
7806 7806 }
7807 7807 freemsg(data_mp);
7808 7808 data_mp = new_data_mp;
7809 7809 mp->b_cont = data_mp;
7810 7810 }
7811 7811 table = (ip6_asp_t *)data_mp->b_rptr;
7812 7812 table_size = iocp->ioc_count;
7813 7813 }
7814 7814
7815 7815 switch (iocp->ioc_cmd) {
7816 7816 case SIOCGIP6ADDRPOLICY:
7817 7817 iocp->ioc_rval = ip6_asp_get(table, table_size, ipst);
7818 7818 if (iocp->ioc_rval == -1)
7819 7819 iocp->ioc_error = EINVAL;
7820 7820 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7821 7821 else if (table != NULL &&
7822 7822 (iocp->ioc_flag & IOC_MODELS) == IOC_ILP32) {
7823 7823 ip6_asp_t *src = table;
7824 7824 ip6_asp32_t *dst = (void *)table;
7825 7825 int count = table_size / sizeof (ip6_asp_t);
7826 7826 int i;
7827 7827
7828 7828 /*
7829 7829 * We need to do an in-place shrink of the array
7830 7830 * to match the alignment attributes of the
7831 7831 * 32-bit ABI looking at it.
7832 7832 */
7833 7833 /* LINTED: logical expression always true: op "||" */
7834 7834 ASSERT(sizeof (*src) > sizeof (*dst));
7835 7835 for (i = 1; i < count; i++)
7836 7836 bcopy(src + i, dst + i, sizeof (*dst));
7837 7837 }
7838 7838 #endif
7839 7839 break;
7840 7840
7841 7841 case SIOCSIP6ADDRPOLICY:
7842 7842 ASSERT(mp->b_prev == NULL);
7843 7843 mp->b_prev = (void *)q;
7844 7844 #if defined(_SYSCALL32_IMPL) && _LONG_LONG_ALIGNMENT_32 == 4
7845 7845 /*
7846 7846 * We pass in the datamodel here so that the ip6_asp_replace()
7847 7847 * routine can handle converting from 32-bit to native formats
7848 7848 * where necessary.
7849 7849 *
7850 7850 * A better way to handle this might be to convert the inbound
7851 7851 * data structure here, and hang it off a new 'mp'; thus the
7852 7852 * ip6_asp_replace() logic would always be dealing with native
7853 7853 * format data structures..
7854 7854 *
7855 7855 * (An even simpler way to handle these ioctls is to just
7856 7856 * add a 32-bit trailing 'pad' field to the ip6_asp_t structure
7857 7857 * and just recompile everything that depends on it.)
7858 7858 */
7859 7859 #endif
7860 7860 ip6_asp_replace(mp, table, table_size, B_FALSE, ipst,
7861 7861 iocp->ioc_flag & IOC_MODELS);
7862 7862 return;
7863 7863 }
7864 7864
7865 7865 DB_TYPE(mp) = (iocp->ioc_error == 0) ? M_IOCACK : M_IOCNAK;
7866 7866 qreply(q, mp);
7867 7867 }
7868 7868
7869 7869 static void
7870 7870 ip_sioctl_dstinfo(queue_t *q, mblk_t *mp)
7871 7871 {
7872 7872 mblk_t *data_mp;
7873 7873 struct dstinforeq *dir;
7874 7874 uint8_t *end, *cur;
7875 7875 in6_addr_t *daddr, *saddr;
7876 7876 ipaddr_t v4daddr;
7877 7877 ire_t *ire;
7878 7878 ipaddr_t v4setsrc;
7879 7879 in6_addr_t v6setsrc;
7880 7880 char *slabel, *dlabel;
7881 7881 boolean_t isipv4;
7882 7882 int match_ire;
7883 7883 ill_t *dst_ill;
7884 7884 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
7885 7885 conn_t *connp = Q_TO_CONN(q);
7886 7886 zoneid_t zoneid = IPCL_ZONEID(connp);
7887 7887 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
7888 7888 uint64_t ipif_flags;
7889 7889
7890 7890 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
7891 7891
7892 7892 /*
7893 7893 * This ioctl is I_STR only, and must have a
7894 7894 * data mblk following the M_IOCTL mblk.
7895 7895 */
7896 7896 data_mp = mp->b_cont;
7897 7897 if (iocp->ioc_count == TRANSPARENT || data_mp == NULL) {
7898 7898 miocnak(q, mp, 0, EINVAL);
7899 7899 return;
7900 7900 }
7901 7901
7902 7902 if (MBLKL(data_mp) < iocp->ioc_count) {
7903 7903 mblk_t *new_data_mp;
7904 7904
7905 7905 if ((new_data_mp = msgpullup(data_mp, -1)) == NULL) {
7906 7906 miocnak(q, mp, 0, ENOMEM);
7907 7907 return;
7908 7908 }
7909 7909 freemsg(data_mp);
7910 7910 data_mp = new_data_mp;
7911 7911 mp->b_cont = data_mp;
7912 7912 }
7913 7913 match_ire = MATCH_IRE_DSTONLY;
7914 7914
7915 7915 for (cur = data_mp->b_rptr, end = data_mp->b_wptr;
7916 7916 end - cur >= sizeof (struct dstinforeq);
7917 7917 cur += sizeof (struct dstinforeq)) {
7918 7918 dir = (struct dstinforeq *)cur;
7919 7919 daddr = &dir->dir_daddr;
7920 7920 saddr = &dir->dir_saddr;
7921 7921
7922 7922 /*
7923 7923 * ip_addr_scope_v6() and ip6_asp_lookup() handle
7924 7924 * v4 mapped addresses; ire_ftable_lookup_v6()
7925 7925 * and ip_select_source_v6() do not.
7926 7926 */
7927 7927 dir->dir_dscope = ip_addr_scope_v6(daddr);
7928 7928 dlabel = ip6_asp_lookup(daddr, &dir->dir_precedence, ipst);
7929 7929
7930 7930 isipv4 = IN6_IS_ADDR_V4MAPPED(daddr);
7931 7931 if (isipv4) {
7932 7932 IN6_V4MAPPED_TO_IPADDR(daddr, v4daddr);
7933 7933 v4setsrc = INADDR_ANY;
7934 7934 ire = ire_route_recursive_v4(v4daddr, 0, NULL, zoneid,
7935 7935 NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v4setsrc,
7936 7936 NULL, NULL);
7937 7937 } else {
7938 7938 v6setsrc = ipv6_all_zeros;
7939 7939 ire = ire_route_recursive_v6(daddr, 0, NULL, zoneid,
7940 7940 NULL, match_ire, IRR_ALLOCATE, 0, ipst, &v6setsrc,
7941 7941 NULL, NULL);
7942 7942 }
7943 7943 ASSERT(ire != NULL);
7944 7944 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
7945 7945 ire_refrele(ire);
7946 7946 dir->dir_dreachable = 0;
7947 7947
7948 7948 /* move on to next dst addr */
7949 7949 continue;
7950 7950 }
7951 7951 dir->dir_dreachable = 1;
7952 7952
7953 7953 dst_ill = ire_nexthop_ill(ire);
7954 7954 if (dst_ill == NULL) {
7955 7955 ire_refrele(ire);
7956 7956 continue;
7957 7957 }
7958 7958
7959 7959 /* With ipmp we most likely look at the ipmp ill here */
7960 7960 dir->dir_dmactype = dst_ill->ill_mactype;
7961 7961
7962 7962 if (isipv4) {
7963 7963 ipaddr_t v4saddr;
7964 7964
7965 7965 if (ip_select_source_v4(dst_ill, v4setsrc, v4daddr,
7966 7966 connp->conn_ixa->ixa_multicast_ifaddr, zoneid, ipst,
7967 7967 &v4saddr, NULL, &ipif_flags) != 0) {
7968 7968 v4saddr = INADDR_ANY;
7969 7969 ipif_flags = 0;
7970 7970 }
7971 7971 IN6_IPADDR_TO_V4MAPPED(v4saddr, saddr);
7972 7972 } else {
7973 7973 if (ip_select_source_v6(dst_ill, &v6setsrc, daddr,
7974 7974 zoneid, ipst, B_FALSE, IPV6_PREFER_SRC_DEFAULT,
7975 7975 saddr, NULL, &ipif_flags) != 0) {
7976 7976 *saddr = ipv6_all_zeros;
7977 7977 ipif_flags = 0;
7978 7978 }
7979 7979 }
7980 7980
7981 7981 dir->dir_sscope = ip_addr_scope_v6(saddr);
7982 7982 slabel = ip6_asp_lookup(saddr, NULL, ipst);
7983 7983 dir->dir_labelmatch = ip6_asp_labelcmp(dlabel, slabel);
7984 7984 dir->dir_sdeprecated = (ipif_flags & IPIF_DEPRECATED) ? 1 : 0;
7985 7985 ire_refrele(ire);
7986 7986 ill_refrele(dst_ill);
7987 7987 }
7988 7988 miocack(q, mp, iocp->ioc_count, 0);
7989 7989 }
7990 7990
7991 7991 /*
7992 7992 * Check if this is an address assigned to this machine.
7993 7993 * Skips interfaces that are down by using ire checks.
7994 7994 * Translates mapped addresses to v4 addresses and then
7995 7995 * treats them as such, returning true if the v4 address
7996 7996 * associated with this mapped address is configured.
7997 7997 * Note: Applications will have to be careful what they do
7998 7998 * with the response; use of mapped addresses limits
7999 7999 * what can be done with the socket, especially with
8000 8000 * respect to socket options and ioctls - neither IPv4
8001 8001 * options nor IPv6 sticky options/ancillary data options
8002 8002 * may be used.
8003 8003 */
8004 8004 /* ARGSUSED */
8005 8005 int
8006 8006 ip_sioctl_tmyaddr(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8007 8007 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8008 8008 {
8009 8009 struct sioc_addrreq *sia;
8010 8010 sin_t *sin;
8011 8011 ire_t *ire;
8012 8012 mblk_t *mp1;
8013 8013 zoneid_t zoneid;
8014 8014 ip_stack_t *ipst;
8015 8015
8016 8016 ip1dbg(("ip_sioctl_tmyaddr"));
8017 8017
8018 8018 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8019 8019 zoneid = Q_TO_CONN(q)->conn_zoneid;
8020 8020 ipst = CONNQ_TO_IPST(q);
8021 8021
8022 8022 /* Existence verified in ip_wput_nondata */
8023 8023 mp1 = mp->b_cont->b_cont;
8024 8024 sia = (struct sioc_addrreq *)mp1->b_rptr;
8025 8025 sin = (sin_t *)&sia->sa_addr;
8026 8026 switch (sin->sin_family) {
8027 8027 case AF_INET6: {
8028 8028 sin6_t *sin6 = (sin6_t *)sin;
8029 8029
8030 8030 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8031 8031 ipaddr_t v4_addr;
8032 8032
8033 8033 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8034 8034 v4_addr);
8035 8035 ire = ire_ftable_lookup_v4(v4_addr, 0, 0,
8036 8036 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8037 8037 MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8038 8038 } else {
8039 8039 in6_addr_t v6addr;
8040 8040
8041 8041 v6addr = sin6->sin6_addr;
8042 8042 ire = ire_ftable_lookup_v6(&v6addr, 0, 0,
8043 8043 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid, NULL,
8044 8044 MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8045 8045 }
8046 8046 break;
8047 8047 }
8048 8048 case AF_INET: {
8049 8049 ipaddr_t v4addr;
8050 8050
8051 8051 v4addr = sin->sin_addr.s_addr;
8052 8052 ire = ire_ftable_lookup_v4(v4addr, 0, 0,
8053 8053 IRE_LOCAL|IRE_LOOPBACK, NULL, zoneid,
8054 8054 NULL, MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY, 0, ipst, NULL);
8055 8055 break;
8056 8056 }
8057 8057 default:
8058 8058 return (EAFNOSUPPORT);
8059 8059 }
8060 8060 if (ire != NULL) {
8061 8061 sia->sa_res = 1;
8062 8062 ire_refrele(ire);
8063 8063 } else {
8064 8064 sia->sa_res = 0;
8065 8065 }
8066 8066 return (0);
8067 8067 }
8068 8068
8069 8069 /*
8070 8070 * Check if this is an address assigned on-link i.e. neighbor,
8071 8071 * and makes sure it's reachable from the current zone.
8072 8072 * Returns true for my addresses as well.
8073 8073 * Translates mapped addresses to v4 addresses and then
8074 8074 * treats them as such, returning true if the v4 address
8075 8075 * associated with this mapped address is configured.
8076 8076 * Note: Applications will have to be careful what they do
8077 8077 * with the response; use of mapped addresses limits
8078 8078 * what can be done with the socket, especially with
8079 8079 * respect to socket options and ioctls - neither IPv4
8080 8080 * options nor IPv6 sticky options/ancillary data options
8081 8081 * may be used.
8082 8082 */
8083 8083 /* ARGSUSED */
8084 8084 int
8085 8085 ip_sioctl_tonlink(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
8086 8086 ip_ioctl_cmd_t *ipip, void *duymmy_ifreq)
8087 8087 {
8088 8088 struct sioc_addrreq *sia;
8089 8089 sin_t *sin;
8090 8090 mblk_t *mp1;
8091 8091 ire_t *ire = NULL;
8092 8092 zoneid_t zoneid;
8093 8093 ip_stack_t *ipst;
8094 8094
8095 8095 ip1dbg(("ip_sioctl_tonlink"));
8096 8096
8097 8097 ASSERT(q->q_next == NULL); /* this ioctl not allowed if ip is module */
8098 8098 zoneid = Q_TO_CONN(q)->conn_zoneid;
8099 8099 ipst = CONNQ_TO_IPST(q);
8100 8100
8101 8101 /* Existence verified in ip_wput_nondata */
8102 8102 mp1 = mp->b_cont->b_cont;
8103 8103 sia = (struct sioc_addrreq *)mp1->b_rptr;
8104 8104 sin = (sin_t *)&sia->sa_addr;
8105 8105
8106 8106 /*
8107 8107 * We check for IRE_ONLINK and exclude IRE_BROADCAST|IRE_MULTICAST
8108 8108 * to make sure we only look at on-link unicast address.
8109 8109 */
8110 8110 switch (sin->sin_family) {
8111 8111 case AF_INET6: {
8112 8112 sin6_t *sin6 = (sin6_t *)sin;
8113 8113
8114 8114 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
8115 8115 ipaddr_t v4_addr;
8116 8116
8117 8117 IN6_V4MAPPED_TO_IPADDR(&sin6->sin6_addr,
8118 8118 v4_addr);
8119 8119 if (!CLASSD(v4_addr)) {
8120 8120 ire = ire_ftable_lookup_v4(v4_addr, 0, 0, 0,
8121 8121 NULL, zoneid, NULL, MATCH_IRE_DSTONLY,
8122 8122 0, ipst, NULL);
8123 8123 }
8124 8124 } else {
8125 8125 in6_addr_t v6addr;
8126 8126
8127 8127 v6addr = sin6->sin6_addr;
8128 8128 if (!IN6_IS_ADDR_MULTICAST(&v6addr)) {
8129 8129 ire = ire_ftable_lookup_v6(&v6addr, 0, 0, 0,
8130 8130 NULL, zoneid, NULL, MATCH_IRE_DSTONLY, 0,
8131 8131 ipst, NULL);
8132 8132 }
8133 8133 }
8134 8134 break;
8135 8135 }
8136 8136 case AF_INET: {
8137 8137 ipaddr_t v4addr;
8138 8138
8139 8139 v4addr = sin->sin_addr.s_addr;
8140 8140 if (!CLASSD(v4addr)) {
8141 8141 ire = ire_ftable_lookup_v4(v4addr, 0, 0, 0, NULL,
8142 8142 zoneid, NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
8143 8143 }
8144 8144 break;
8145 8145 }
8146 8146 default:
8147 8147 return (EAFNOSUPPORT);
8148 8148 }
8149 8149 sia->sa_res = 0;
8150 8150 if (ire != NULL) {
8151 8151 ASSERT(!(ire->ire_type & IRE_MULTICAST));
8152 8152
8153 8153 if ((ire->ire_type & IRE_ONLINK) &&
8154 8154 !(ire->ire_type & IRE_BROADCAST))
8155 8155 sia->sa_res = 1;
8156 8156 ire_refrele(ire);
8157 8157 }
8158 8158 return (0);
8159 8159 }
8160 8160
8161 8161 /*
8162 8162 * TBD: implement when kernel maintaines a list of site prefixes.
8163 8163 */
8164 8164 /* ARGSUSED */
8165 8165 int
8166 8166 ip_sioctl_tmysite(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8167 8167 ip_ioctl_cmd_t *ipip, void *ifreq)
8168 8168 {
8169 8169 return (ENXIO);
8170 8170 }
8171 8171
8172 8172 /* ARP IOCTLs. */
8173 8173 /* ARGSUSED */
8174 8174 int
8175 8175 ip_sioctl_arp(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
8176 8176 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
8177 8177 {
8178 8178 int err;
8179 8179 ipaddr_t ipaddr;
8180 8180 struct iocblk *iocp;
8181 8181 conn_t *connp;
8182 8182 struct arpreq *ar;
8183 8183 struct xarpreq *xar;
8184 8184 int arp_flags, flags, alength;
8185 8185 uchar_t *lladdr;
8186 8186 ip_stack_t *ipst;
8187 8187 ill_t *ill = ipif->ipif_ill;
8188 8188 ill_t *proxy_ill = NULL;
8189 8189 ipmp_arpent_t *entp = NULL;
8190 8190 boolean_t proxyarp = B_FALSE;
8191 8191 boolean_t if_arp_ioctl = B_FALSE;
8192 8192 ncec_t *ncec = NULL;
8193 8193 nce_t *nce;
8194 8194
8195 8195 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8196 8196 connp = Q_TO_CONN(q);
8197 8197 ipst = connp->conn_netstack->netstack_ip;
8198 8198 iocp = (struct iocblk *)mp->b_rptr;
8199 8199
8200 8200 if (ipip->ipi_cmd_type == XARP_CMD) {
8201 8201 /* We have a chain - M_IOCTL-->MI_COPY_MBLK-->XARPREQ_MBLK */
8202 8202 xar = (struct xarpreq *)mp->b_cont->b_cont->b_rptr;
8203 8203 ar = NULL;
8204 8204
8205 8205 arp_flags = xar->xarp_flags;
8206 8206 lladdr = (uchar_t *)LLADDR(&xar->xarp_ha);
8207 8207 if_arp_ioctl = (xar->xarp_ha.sdl_nlen != 0);
8208 8208 /*
8209 8209 * Validate against user's link layer address length
8210 8210 * input and name and addr length limits.
8211 8211 */
8212 8212 alength = ill->ill_phys_addr_length;
8213 8213 if (ipip->ipi_cmd == SIOCSXARP) {
8214 8214 if (alength != xar->xarp_ha.sdl_alen ||
8215 8215 (alength + xar->xarp_ha.sdl_nlen >
8216 8216 sizeof (xar->xarp_ha.sdl_data)))
8217 8217 return (EINVAL);
8218 8218 }
8219 8219 } else {
8220 8220 /* We have a chain - M_IOCTL-->MI_COPY_MBLK-->ARPREQ_MBLK */
8221 8221 ar = (struct arpreq *)mp->b_cont->b_cont->b_rptr;
8222 8222 xar = NULL;
8223 8223
8224 8224 arp_flags = ar->arp_flags;
8225 8225 lladdr = (uchar_t *)ar->arp_ha.sa_data;
8226 8226 /*
8227 8227 * Theoretically, the sa_family could tell us what link
8228 8228 * layer type this operation is trying to deal with. By
8229 8229 * common usage AF_UNSPEC means ethernet. We'll assume
8230 8230 * any attempt to use the SIOC?ARP ioctls is for ethernet,
8231 8231 * for now. Our new SIOC*XARP ioctls can be used more
8232 8232 * generally.
8233 8233 *
8234 8234 * If the underlying media happens to have a non 6 byte
8235 8235 * address, arp module will fail set/get, but the del
8236 8236 * operation will succeed.
8237 8237 */
8238 8238 alength = 6;
8239 8239 if ((ipip->ipi_cmd != SIOCDARP) &&
8240 8240 (alength != ill->ill_phys_addr_length)) {
8241 8241 return (EINVAL);
8242 8242 }
8243 8243 }
8244 8244
8245 8245 /* Translate ATF* flags to NCE* flags */
8246 8246 flags = 0;
8247 8247 if (arp_flags & ATF_AUTHORITY)
8248 8248 flags |= NCE_F_AUTHORITY;
8249 8249 if (arp_flags & ATF_PERM)
8250 8250 flags |= NCE_F_NONUD; /* not subject to aging */
8251 8251 if (arp_flags & ATF_PUBL)
8252 8252 flags |= NCE_F_PUBLISH;
8253 8253
8254 8254 /*
8255 8255 * IPMP ARP special handling:
8256 8256 *
8257 8257 * 1. Since ARP mappings must appear consistent across the group,
8258 8258 * prohibit changing ARP mappings on the underlying interfaces.
8259 8259 *
8260 8260 * 2. Since ARP mappings for IPMP data addresses are maintained by
8261 8261 * IP itself, prohibit changing them.
8262 8262 *
8263 8263 * 3. For proxy ARP, use a functioning hardware address in the group,
8264 8264 * provided one exists. If one doesn't, just add the entry as-is;
8265 8265 * ipmp_illgrp_refresh_arpent() will refresh it if things change.
8266 8266 */
8267 8267 if (IS_UNDER_IPMP(ill)) {
8268 8268 if (ipip->ipi_cmd != SIOCGARP && ipip->ipi_cmd != SIOCGXARP)
8269 8269 return (EPERM);
8270 8270 }
8271 8271 if (IS_IPMP(ill)) {
8272 8272 ipmp_illgrp_t *illg = ill->ill_grp;
8273 8273
8274 8274 switch (ipip->ipi_cmd) {
8275 8275 case SIOCSARP:
8276 8276 case SIOCSXARP:
8277 8277 proxy_ill = ipmp_illgrp_find_ill(illg, lladdr, alength);
8278 8278 if (proxy_ill != NULL) {
8279 8279 proxyarp = B_TRUE;
8280 8280 if (!ipmp_ill_is_active(proxy_ill))
8281 8281 proxy_ill = ipmp_illgrp_next_ill(illg);
8282 8282 if (proxy_ill != NULL)
8283 8283 lladdr = proxy_ill->ill_phys_addr;
8284 8284 }
8285 8285 /* FALLTHRU */
8286 8286 }
8287 8287 }
8288 8288
8289 8289 ipaddr = sin->sin_addr.s_addr;
8290 8290 /*
8291 8291 * don't match across illgrp per case (1) and (2).
8292 8292 * XXX use IS_IPMP(ill) like ndp_sioc_update?
8293 8293 */
8294 8294 nce = nce_lookup_v4(ill, &ipaddr);
8295 8295 if (nce != NULL)
8296 8296 ncec = nce->nce_common;
8297 8297
8298 8298 switch (iocp->ioc_cmd) {
8299 8299 case SIOCDARP:
8300 8300 case SIOCDXARP: {
8301 8301 /*
8302 8302 * Delete the NCE if any.
8303 8303 */
8304 8304 if (ncec == NULL) {
8305 8305 iocp->ioc_error = ENXIO;
8306 8306 break;
8307 8307 }
8308 8308 /* Don't allow changes to arp mappings of local addresses. */
8309 8309 if (NCE_MYADDR(ncec)) {
8310 8310 nce_refrele(nce);
8311 8311 return (ENOTSUP);
8312 8312 }
8313 8313 iocp->ioc_error = 0;
8314 8314
8315 8315 /*
8316 8316 * Delete the nce_common which has ncec_ill set to ipmp_ill.
8317 8317 * This will delete all the nce entries on the under_ills.
8318 8318 */
8319 8319 ncec_delete(ncec);
8320 8320 /*
8321 8321 * Once the NCE has been deleted, then the ire_dep* consistency
8322 8322 * mechanism will find any IRE which depended on the now
8323 8323 * condemned NCE (as part of sending packets).
8324 8324 * That mechanism handles redirects by deleting redirects
8325 8325 * that refer to UNREACHABLE nces.
8326 8326 */
8327 8327 break;
8328 8328 }
8329 8329 case SIOCGARP:
8330 8330 case SIOCGXARP:
8331 8331 if (ncec != NULL) {
8332 8332 lladdr = ncec->ncec_lladdr;
8333 8333 flags = ncec->ncec_flags;
8334 8334 iocp->ioc_error = 0;
8335 8335 ip_sioctl_garp_reply(mp, ncec->ncec_ill, lladdr, flags);
8336 8336 } else {
8337 8337 iocp->ioc_error = ENXIO;
8338 8338 }
8339 8339 break;
8340 8340 case SIOCSARP:
8341 8341 case SIOCSXARP:
8342 8342 /* Don't allow changes to arp mappings of local addresses. */
8343 8343 if (ncec != NULL && NCE_MYADDR(ncec)) {
8344 8344 nce_refrele(nce);
8345 8345 return (ENOTSUP);
8346 8346 }
8347 8347
8348 8348 /* static arp entries will undergo NUD if ATF_PERM is not set */
8349 8349 flags |= NCE_F_STATIC;
8350 8350 if (!if_arp_ioctl) {
8351 8351 ip_nce_lookup_and_update(&ipaddr, NULL, ipst,
8352 8352 lladdr, alength, flags);
8353 8353 } else {
8354 8354 ipif_t *ipif = ipif_get_next_ipif(NULL, ill);
8355 8355 if (ipif != NULL) {
8356 8356 ip_nce_lookup_and_update(&ipaddr, ipif, ipst,
8357 8357 lladdr, alength, flags);
8358 8358 ipif_refrele(ipif);
8359 8359 }
8360 8360 }
8361 8361 if (nce != NULL) {
8362 8362 nce_refrele(nce);
8363 8363 nce = NULL;
8364 8364 }
8365 8365 /*
8366 8366 * NCE_F_STATIC entries will be added in state ND_REACHABLE
8367 8367 * by nce_add_common()
8368 8368 */
8369 8369 err = nce_lookup_then_add_v4(ill, lladdr,
8370 8370 ill->ill_phys_addr_length, &ipaddr, flags, ND_UNCHANGED,
8371 8371 &nce);
8372 8372 if (err == EEXIST) {
8373 8373 ncec = nce->nce_common;
8374 8374 mutex_enter(&ncec->ncec_lock);
8375 8375 ncec->ncec_state = ND_REACHABLE;
8376 8376 ncec->ncec_flags = flags;
8377 8377 nce_update(ncec, ND_UNCHANGED, lladdr);
8378 8378 mutex_exit(&ncec->ncec_lock);
8379 8379 err = 0;
8380 8380 }
8381 8381 if (nce != NULL) {
8382 8382 nce_refrele(nce);
8383 8383 nce = NULL;
8384 8384 }
8385 8385 if (IS_IPMP(ill) && err == 0) {
8386 8386 entp = ipmp_illgrp_create_arpent(ill->ill_grp,
8387 8387 proxyarp, ipaddr, lladdr, ill->ill_phys_addr_length,
8388 8388 flags);
8389 8389 if (entp == NULL || (proxyarp && proxy_ill == NULL)) {
8390 8390 iocp->ioc_error = (entp == NULL ? ENOMEM : 0);
8391 8391 break;
8392 8392 }
8393 8393 }
8394 8394 iocp->ioc_error = err;
8395 8395 }
8396 8396
8397 8397 if (nce != NULL) {
8398 8398 nce_refrele(nce);
8399 8399 }
8400 8400
8401 8401 /*
8402 8402 * If we created an IPMP ARP entry, mark that we've notified ARP.
8403 8403 */
8404 8404 if (entp != NULL)
8405 8405 ipmp_illgrp_mark_arpent(ill->ill_grp, entp);
8406 8406
8407 8407 return (iocp->ioc_error);
8408 8408 }
8409 8409
8410 8410 /*
8411 8411 * Parse an [x]arpreq structure coming down SIOC[GSD][X]ARP ioctls, identify
8412 8412 * the associated sin and refhold and return the associated ipif via `ci'.
8413 8413 */
8414 8414 int
8415 8415 ip_extract_arpreq(queue_t *q, mblk_t *mp, const ip_ioctl_cmd_t *ipip,
8416 8416 cmd_info_t *ci)
8417 8417 {
8418 8418 mblk_t *mp1;
8419 8419 sin_t *sin;
8420 8420 conn_t *connp;
8421 8421 ipif_t *ipif;
8422 8422 ire_t *ire = NULL;
8423 8423 ill_t *ill = NULL;
8424 8424 boolean_t exists;
8425 8425 ip_stack_t *ipst;
8426 8426 struct arpreq *ar;
8427 8427 struct xarpreq *xar;
8428 8428 struct sockaddr_dl *sdl;
8429 8429
8430 8430 /* ioctl comes down on a conn */
8431 8431 ASSERT(!(q->q_flag & QREADR) && q->q_next == NULL);
8432 8432 connp = Q_TO_CONN(q);
8433 8433 if (connp->conn_family == AF_INET6)
8434 8434 return (ENXIO);
8435 8435
8436 8436 ipst = connp->conn_netstack->netstack_ip;
8437 8437
8438 8438 /* Verified in ip_wput_nondata */
8439 8439 mp1 = mp->b_cont->b_cont;
8440 8440
8441 8441 if (ipip->ipi_cmd_type == XARP_CMD) {
8442 8442 ASSERT(MBLKL(mp1) >= sizeof (struct xarpreq));
8443 8443 xar = (struct xarpreq *)mp1->b_rptr;
8444 8444 sin = (sin_t *)&xar->xarp_pa;
8445 8445 sdl = &xar->xarp_ha;
8446 8446
8447 8447 if (sdl->sdl_family != AF_LINK || sin->sin_family != AF_INET)
8448 8448 return (ENXIO);
8449 8449 if (sdl->sdl_nlen >= LIFNAMSIZ)
8450 8450 return (EINVAL);
8451 8451 } else {
8452 8452 ASSERT(ipip->ipi_cmd_type == ARP_CMD);
8453 8453 ASSERT(MBLKL(mp1) >= sizeof (struct arpreq));
8454 8454 ar = (struct arpreq *)mp1->b_rptr;
8455 8455 sin = (sin_t *)&ar->arp_pa;
8456 8456 }
8457 8457
8458 8458 if (ipip->ipi_cmd_type == XARP_CMD && sdl->sdl_nlen != 0) {
8459 8459 ipif = ipif_lookup_on_name(sdl->sdl_data, sdl->sdl_nlen,
8460 8460 B_FALSE, &exists, B_FALSE, ALL_ZONES, ipst);
8461 8461 if (ipif == NULL)
8462 8462 return (ENXIO);
8463 8463 if (ipif->ipif_id != 0) {
8464 8464 ipif_refrele(ipif);
8465 8465 return (ENXIO);
8466 8466 }
8467 8467 } else {
8468 8468 /*
8469 8469 * Either an SIOC[DGS]ARP or an SIOC[DGS]XARP with an sdl_nlen
8470 8470 * of 0: use the IP address to find the ipif. If the IP
8471 8471 * address is an IPMP test address, ire_ftable_lookup() will
8472 8472 * find the wrong ill, so we first do an ipif_lookup_addr().
8473 8473 */
8474 8474 ipif = ipif_lookup_addr(sin->sin_addr.s_addr, NULL, ALL_ZONES,
8475 8475 ipst);
8476 8476 if (ipif == NULL) {
8477 8477 ire = ire_ftable_lookup_v4(sin->sin_addr.s_addr,
8478 8478 0, 0, IRE_IF_RESOLVER, NULL, ALL_ZONES,
8479 8479 NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
8480 8480 if (ire == NULL || ((ill = ire->ire_ill) == NULL)) {
8481 8481 if (ire != NULL)
8482 8482 ire_refrele(ire);
8483 8483 return (ENXIO);
8484 8484 }
8485 8485 ASSERT(ire != NULL && ill != NULL);
8486 8486 ipif = ill->ill_ipif;
8487 8487 ipif_refhold(ipif);
8488 8488 ire_refrele(ire);
8489 8489 }
8490 8490 }
8491 8491
8492 8492 if (ipif->ipif_ill->ill_net_type != IRE_IF_RESOLVER) {
8493 8493 ipif_refrele(ipif);
8494 8494 return (ENXIO);
8495 8495 }
8496 8496
8497 8497 ci->ci_sin = sin;
8498 8498 ci->ci_ipif = ipif;
8499 8499 return (0);
8500 8500 }
8501 8501
8502 8502 /*
8503 8503 * Link or unlink the illgrp on IPMP meta-interface `ill' depending on the
8504 8504 * value of `ioccmd'. While an illgrp is linked to an ipmp_grp_t, it is
8505 8505 * accessible from that ipmp_grp_t, which means SIOCSLIFGROUPNAME can look it
8506 8506 * up and thus an ill can join that illgrp.
8507 8507 *
8508 8508 * We use I_PLINK/I_PUNLINK to do the link/unlink operations rather than
8509 8509 * open()/close() primarily because close() is not allowed to fail or block
8510 8510 * forever. On the other hand, I_PUNLINK *can* fail, and there's no reason
8511 8511 * why anyone should ever need to I_PUNLINK an in-use IPMP stream. To ensure
8512 8512 * symmetric behavior (e.g., doing an I_PLINK after and I_PUNLINK undoes the
8513 8513 * I_PUNLINK) we defer linking to I_PLINK. Separately, we also fail attempts
8514 8514 * to I_LINK since I_UNLINK is optional and we'd end up in an inconsistent
8515 8515 * state if I_UNLINK didn't occur.
8516 8516 *
8517 8517 * Note that for each plumb/unplumb operation, we may end up here more than
8518 8518 * once because of the way ifconfig works. However, it's OK to link the same
8519 8519 * illgrp more than once, or unlink an illgrp that's already unlinked.
8520 8520 */
8521 8521 static int
8522 8522 ip_sioctl_plink_ipmp(ill_t *ill, int ioccmd)
8523 8523 {
8524 8524 int err;
8525 8525 ip_stack_t *ipst = ill->ill_ipst;
8526 8526
8527 8527 ASSERT(IS_IPMP(ill));
8528 8528 ASSERT(IAM_WRITER_ILL(ill));
8529 8529
8530 8530 switch (ioccmd) {
8531 8531 case I_LINK:
8532 8532 return (ENOTSUP);
8533 8533
8534 8534 case I_PLINK:
8535 8535 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8536 8536 ipmp_illgrp_link_grp(ill->ill_grp, ill->ill_phyint->phyint_grp);
8537 8537 rw_exit(&ipst->ips_ipmp_lock);
8538 8538 break;
8539 8539
8540 8540 case I_PUNLINK:
8541 8541 /*
8542 8542 * Require all UP ipifs be brought down prior to unlinking the
8543 8543 * illgrp so any associated IREs (and other state) is torched.
8544 8544 */
8545 8545 if (ill->ill_ipif_up_count + ill->ill_ipif_dup_count > 0)
8546 8546 return (EBUSY);
8547 8547
8548 8548 /*
8549 8549 * NOTE: We hold ipmp_lock across the unlink to prevent a race
8550 8550 * with an SIOCSLIFGROUPNAME request from an ill trying to
8551 8551 * join this group. Specifically: ills trying to join grab
8552 8552 * ipmp_lock and bump a "pending join" counter checked by
8553 8553 * ipmp_illgrp_unlink_grp(). During the unlink no new pending
8554 8554 * joins can occur (since we have ipmp_lock). Once we drop
8555 8555 * ipmp_lock, subsequent SIOCSLIFGROUPNAME requests will not
8556 8556 * find the illgrp (since we unlinked it) and will return
8557 8557 * EAFNOSUPPORT. This will then take them back through the
8558 8558 * IPMP meta-interface plumbing logic in ifconfig, and thus
8559 8559 * back through I_PLINK above.
8560 8560 */
8561 8561 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
8562 8562 err = ipmp_illgrp_unlink_grp(ill->ill_grp);
8563 8563 rw_exit(&ipst->ips_ipmp_lock);
8564 8564 return (err);
8565 8565 default:
8566 8566 break;
8567 8567 }
8568 8568 return (0);
8569 8569 }
8570 8570
8571 8571 /*
8572 8572 * Do I_PLINK/I_LINK or I_PUNLINK/I_UNLINK with consistency checks and also
8573 8573 * atomically set/clear the muxids. Also complete the ioctl by acking or
8574 8574 * naking it. Note that the code is structured such that the link type,
8575 8575 * whether it's persistent or not, is treated equally. ifconfig(1M) and
8576 8576 * its clones use the persistent link, while pppd(1M) and perhaps many
8577 8577 * other daemons may use non-persistent link. When combined with some
8578 8578 * ill_t states, linking and unlinking lower streams may be used as
8579 8579 * indicators of dynamic re-plumbing events [see PSARC/1999/348].
8580 8580 */
8581 8581 /* ARGSUSED */
8582 8582 void
8583 8583 ip_sioctl_plink(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8584 8584 {
8585 8585 mblk_t *mp1;
8586 8586 struct linkblk *li;
8587 8587 int ioccmd = ((struct iocblk *)mp->b_rptr)->ioc_cmd;
8588 8588 int err = 0;
8589 8589
8590 8590 ASSERT(ioccmd == I_PLINK || ioccmd == I_PUNLINK ||
8591 8591 ioccmd == I_LINK || ioccmd == I_UNLINK);
8592 8592
8593 8593 mp1 = mp->b_cont; /* This is the linkblk info */
8594 8594 li = (struct linkblk *)mp1->b_rptr;
8595 8595
8596 8596 err = ip_sioctl_plink_ipmod(ipsq, q, mp, ioccmd, li);
8597 8597 if (err == EINPROGRESS)
8598 8598 return;
8599 8599 if (err == 0)
8600 8600 miocack(q, mp, 0, 0);
8601 8601 else
8602 8602 miocnak(q, mp, 0, err);
8603 8603
8604 8604 /* Conn was refheld in ip_sioctl_copyin_setup */
8605 8605 if (CONN_Q(q)) {
8606 8606 CONN_DEC_IOCTLREF(Q_TO_CONN(q));
8607 8607 CONN_OPER_PENDING_DONE(Q_TO_CONN(q));
8608 8608 }
8609 8609 }
8610 8610
8611 8611 /*
8612 8612 * Process I_{P}LINK and I_{P}UNLINK requests named by `ioccmd' and pointed to
8613 8613 * by `mp' and `li' for the IP module stream (if li->q_bot is in fact an IP
8614 8614 * module stream).
8615 8615 * Returns zero on success, EINPROGRESS if the operation is still pending, or
8616 8616 * an error code on failure.
8617 8617 */
8618 8618 static int
8619 8619 ip_sioctl_plink_ipmod(ipsq_t *ipsq, queue_t *q, mblk_t *mp, int ioccmd,
8620 8620 struct linkblk *li)
8621 8621 {
8622 8622 int err = 0;
8623 8623 ill_t *ill;
8624 8624 queue_t *ipwq, *dwq;
8625 8625 const char *name;
8626 8626 struct qinit *qinfo;
8627 8627 boolean_t islink = (ioccmd == I_PLINK || ioccmd == I_LINK);
8628 8628 boolean_t entered_ipsq = B_FALSE;
8629 8629 boolean_t is_ip = B_FALSE;
8630 8630 arl_t *arl;
8631 8631
8632 8632 /*
8633 8633 * Walk the lower stream to verify it's the IP module stream.
8634 8634 * The IP module is identified by its name, wput function,
8635 8635 * and non-NULL q_next. STREAMS ensures that the lower stream
8636 8636 * (li->l_qbot) will not vanish until this ioctl completes.
8637 8637 */
8638 8638 for (ipwq = li->l_qbot; ipwq != NULL; ipwq = ipwq->q_next) {
8639 8639 qinfo = ipwq->q_qinfo;
8640 8640 name = qinfo->qi_minfo->mi_idname;
8641 8641 if (name != NULL && strcmp(name, ip_mod_info.mi_idname) == 0 &&
8642 8642 qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8643 8643 is_ip = B_TRUE;
8644 8644 break;
8645 8645 }
8646 8646 if (name != NULL && strcmp(name, arp_mod_info.mi_idname) == 0 &&
8647 8647 qinfo->qi_putp != (pfi_t)ip_lwput && ipwq->q_next != NULL) {
8648 8648 break;
8649 8649 }
8650 8650 }
8651 8651
8652 8652 /*
8653 8653 * If this isn't an IP module stream, bail.
8654 8654 */
8655 8655 if (ipwq == NULL)
8656 8656 return (0);
8657 8657
8658 8658 if (!is_ip) {
8659 8659 arl = (arl_t *)ipwq->q_ptr;
8660 8660 ill = arl_to_ill(arl);
8661 8661 if (ill == NULL)
8662 8662 return (0);
8663 8663 } else {
8664 8664 ill = ipwq->q_ptr;
8665 8665 }
8666 8666 ASSERT(ill != NULL);
8667 8667
8668 8668 if (ipsq == NULL) {
8669 8669 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_sioctl_plink,
8670 8670 NEW_OP, B_FALSE);
8671 8671 if (ipsq == NULL) {
8672 8672 if (!is_ip)
8673 8673 ill_refrele(ill);
8674 8674 return (EINPROGRESS);
8675 8675 }
8676 8676 entered_ipsq = B_TRUE;
8677 8677 }
8678 8678 ASSERT(IAM_WRITER_ILL(ill));
8679 8679 mutex_enter(&ill->ill_lock);
8680 8680 if (!is_ip) {
8681 8681 if (islink && ill->ill_muxid == 0) {
8682 8682 /*
8683 8683 * Plumbing has to be done with IP plumbed first, arp
8684 8684 * second, but here we have arp being plumbed first.
8685 8685 */
8686 8686 mutex_exit(&ill->ill_lock);
8687 8687 if (entered_ipsq)
8688 8688 ipsq_exit(ipsq);
8689 8689 ill_refrele(ill);
8690 8690 return (EINVAL);
8691 8691 }
8692 8692 }
8693 8693 mutex_exit(&ill->ill_lock);
8694 8694 if (!is_ip) {
8695 8695 arl->arl_muxid = islink ? li->l_index : 0;
8696 8696 ill_refrele(ill);
8697 8697 goto done;
8698 8698 }
8699 8699
8700 8700 if (IS_IPMP(ill) && (err = ip_sioctl_plink_ipmp(ill, ioccmd)) != 0)
8701 8701 goto done;
8702 8702
8703 8703 /*
8704 8704 * As part of I_{P}LINKing, stash the number of downstream modules and
8705 8705 * the read queue of the module immediately below IP in the ill.
8706 8706 * These are used during the capability negotiation below.
8707 8707 */
8708 8708 ill->ill_lmod_rq = NULL;
8709 8709 ill->ill_lmod_cnt = 0;
8710 8710 if (islink && ((dwq = ipwq->q_next) != NULL)) {
8711 8711 ill->ill_lmod_rq = RD(dwq);
8712 8712 for (; dwq != NULL; dwq = dwq->q_next)
8713 8713 ill->ill_lmod_cnt++;
8714 8714 }
8715 8715
8716 8716 ill->ill_muxid = islink ? li->l_index : 0;
8717 8717
8718 8718 /*
8719 8719 * Mark the ipsq busy until the capability operations initiated below
8720 8720 * complete. The PLINK/UNLINK ioctl itself completes when our caller
8721 8721 * returns, but the capability operation may complete asynchronously
8722 8722 * much later.
8723 8723 */
8724 8724 ipsq_current_start(ipsq, ill->ill_ipif, ioccmd);
8725 8725 /*
8726 8726 * If there's at least one up ipif on this ill, then we're bound to
8727 8727 * the underlying driver via DLPI. In that case, renegotiate
8728 8728 * capabilities to account for any possible change in modules
8729 8729 * interposed between IP and the driver.
8730 8730 */
8731 8731 if (ill->ill_ipif_up_count > 0) {
8732 8732 if (islink)
8733 8733 ill_capability_probe(ill);
8734 8734 else
8735 8735 ill_capability_reset(ill, B_FALSE);
8736 8736 }
8737 8737 ipsq_current_finish(ipsq);
8738 8738 done:
8739 8739 if (entered_ipsq)
8740 8740 ipsq_exit(ipsq);
8741 8741
8742 8742 return (err);
8743 8743 }
8744 8744
8745 8745 /*
8746 8746 * Search the ioctl command in the ioctl tables and return a pointer
8747 8747 * to the ioctl command information. The ioctl command tables are
8748 8748 * static and fully populated at compile time.
8749 8749 */
8750 8750 ip_ioctl_cmd_t *
8751 8751 ip_sioctl_lookup(int ioc_cmd)
8752 8752 {
8753 8753 int index;
8754 8754 ip_ioctl_cmd_t *ipip;
8755 8755 ip_ioctl_cmd_t *ipip_end;
8756 8756
8757 8757 if (ioc_cmd == IPI_DONTCARE)
8758 8758 return (NULL);
8759 8759
8760 8760 /*
8761 8761 * Do a 2 step search. First search the indexed table
8762 8762 * based on the least significant byte of the ioctl cmd.
8763 8763 * If we don't find a match, then search the misc table
8764 8764 * serially.
8765 8765 */
8766 8766 index = ioc_cmd & 0xFF;
8767 8767 if (index < ip_ndx_ioctl_count) {
8768 8768 ipip = &ip_ndx_ioctl_table[index];
8769 8769 if (ipip->ipi_cmd == ioc_cmd) {
8770 8770 /* Found a match in the ndx table */
8771 8771 return (ipip);
8772 8772 }
8773 8773 }
8774 8774
8775 8775 /* Search the misc table */
8776 8776 ipip_end = &ip_misc_ioctl_table[ip_misc_ioctl_count];
8777 8777 for (ipip = ip_misc_ioctl_table; ipip < ipip_end; ipip++) {
8778 8778 if (ipip->ipi_cmd == ioc_cmd)
8779 8779 /* Found a match in the misc table */
8780 8780 return (ipip);
8781 8781 }
8782 8782
8783 8783 return (NULL);
8784 8784 }
8785 8785
8786 8786 /*
8787 8787 * helper function for ip_sioctl_getsetprop(), which does some sanity checks
8788 8788 */
8789 8789 static boolean_t
8790 8790 getset_ioctl_checks(mblk_t *mp)
8791 8791 {
8792 8792 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8793 8793 mblk_t *mp1 = mp->b_cont;
8794 8794 mod_ioc_prop_t *pioc;
8795 8795 uint_t flags;
8796 8796 uint_t pioc_size;
8797 8797
8798 8798 /* do sanity checks on various arguments */
8799 8799 if (mp1 == NULL || iocp->ioc_count == 0 ||
8800 8800 iocp->ioc_count == TRANSPARENT) {
8801 8801 return (B_FALSE);
8802 8802 }
8803 8803 if (msgdsize(mp1) < iocp->ioc_count) {
8804 8804 if (!pullupmsg(mp1, iocp->ioc_count))
8805 8805 return (B_FALSE);
8806 8806 }
8807 8807
8808 8808 pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8809 8809
8810 8810 /* sanity checks on mpr_valsize */
8811 8811 pioc_size = sizeof (mod_ioc_prop_t);
8812 8812 if (pioc->mpr_valsize != 0)
8813 8813 pioc_size += pioc->mpr_valsize - 1;
8814 8814
8815 8815 if (iocp->ioc_count != pioc_size)
8816 8816 return (B_FALSE);
8817 8817
8818 8818 flags = pioc->mpr_flags;
8819 8819 if (iocp->ioc_cmd == SIOCSETPROP) {
8820 8820 /*
8821 8821 * One can either reset the value to it's default value or
8822 8822 * change the current value or append/remove the value from
8823 8823 * a multi-valued properties.
8824 8824 */
8825 8825 if ((flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8826 8826 flags != MOD_PROP_ACTIVE &&
8827 8827 flags != (MOD_PROP_ACTIVE|MOD_PROP_APPEND) &&
8828 8828 flags != (MOD_PROP_ACTIVE|MOD_PROP_REMOVE))
8829 8829 return (B_FALSE);
8830 8830 } else {
8831 8831 ASSERT(iocp->ioc_cmd == SIOCGETPROP);
8832 8832
8833 8833 /*
8834 8834 * One can retrieve only one kind of property information
8835 8835 * at a time.
8836 8836 */
8837 8837 if ((flags & MOD_PROP_ACTIVE) != MOD_PROP_ACTIVE &&
8838 8838 (flags & MOD_PROP_DEFAULT) != MOD_PROP_DEFAULT &&
8839 8839 (flags & MOD_PROP_POSSIBLE) != MOD_PROP_POSSIBLE &&
8840 8840 (flags & MOD_PROP_PERM) != MOD_PROP_PERM)
8841 8841 return (B_FALSE);
8842 8842 }
8843 8843
8844 8844 return (B_TRUE);
8845 8845 }
8846 8846
8847 8847 /*
8848 8848 * process the SIOC{SET|GET}PROP ioctl's
8849 8849 */
8850 8850 /* ARGSUSED */
8851 8851 static void
8852 8852 ip_sioctl_getsetprop(queue_t *q, mblk_t *mp)
8853 8853 {
8854 8854 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8855 8855 mblk_t *mp1 = mp->b_cont;
8856 8856 mod_ioc_prop_t *pioc;
8857 8857 mod_prop_info_t *ptbl = NULL, *pinfo = NULL;
8858 8858 ip_stack_t *ipst;
8859 8859 netstack_t *stack;
8860 8860 cred_t *cr;
8861 8861 boolean_t set;
8862 8862 int err;
8863 8863
8864 8864 ASSERT(q->q_next == NULL);
8865 8865 ASSERT(CONN_Q(q));
8866 8866
8867 8867 if (!getset_ioctl_checks(mp)) {
8868 8868 miocnak(q, mp, 0, EINVAL);
8869 8869 return;
8870 8870 }
8871 8871 ipst = CONNQ_TO_IPST(q);
8872 8872 stack = ipst->ips_netstack;
8873 8873 pioc = (mod_ioc_prop_t *)mp1->b_rptr;
8874 8874
8875 8875 switch (pioc->mpr_proto) {
8876 8876 case MOD_PROTO_IP:
8877 8877 case MOD_PROTO_IPV4:
8878 8878 case MOD_PROTO_IPV6:
8879 8879 ptbl = ipst->ips_propinfo_tbl;
8880 8880 break;
8881 8881 case MOD_PROTO_RAWIP:
8882 8882 ptbl = stack->netstack_icmp->is_propinfo_tbl;
8883 8883 break;
8884 8884 case MOD_PROTO_TCP:
8885 8885 ptbl = stack->netstack_tcp->tcps_propinfo_tbl;
8886 8886 break;
8887 8887 case MOD_PROTO_UDP:
8888 8888 ptbl = stack->netstack_udp->us_propinfo_tbl;
8889 8889 break;
8890 8890 case MOD_PROTO_SCTP:
8891 8891 ptbl = stack->netstack_sctp->sctps_propinfo_tbl;
8892 8892 break;
8893 8893 default:
8894 8894 miocnak(q, mp, 0, EINVAL);
8895 8895 return;
8896 8896 }
8897 8897
8898 8898 pinfo = mod_prop_lookup(ptbl, pioc->mpr_name, pioc->mpr_proto);
8899 8899 if (pinfo == NULL) {
8900 8900 miocnak(q, mp, 0, ENOENT);
8901 8901 return;
8902 8902 }
8903 8903
8904 8904 set = (iocp->ioc_cmd == SIOCSETPROP) ? B_TRUE : B_FALSE;
8905 8905 if (set && pinfo->mpi_setf != NULL) {
8906 8906 cr = msg_getcred(mp, NULL);
8907 8907 if (cr == NULL)
8908 8908 cr = iocp->ioc_cr;
8909 8909 err = pinfo->mpi_setf(stack, cr, pinfo, pioc->mpr_ifname,
8910 8910 pioc->mpr_val, pioc->mpr_flags);
8911 8911 } else if (!set && pinfo->mpi_getf != NULL) {
8912 8912 err = pinfo->mpi_getf(stack, pinfo, pioc->mpr_ifname,
8913 8913 pioc->mpr_val, pioc->mpr_valsize, pioc->mpr_flags);
8914 8914 } else {
8915 8915 err = EPERM;
8916 8916 }
8917 8917
8918 8918 if (err != 0) {
8919 8919 miocnak(q, mp, 0, err);
8920 8920 } else {
8921 8921 if (set)
8922 8922 miocack(q, mp, 0, 0);
8923 8923 else /* For get, we need to return back the data */
8924 8924 miocack(q, mp, iocp->ioc_count, 0);
8925 8925 }
8926 8926 }
8927 8927
8928 8928 /*
8929 8929 * process the legacy ND_GET, ND_SET ioctl just for {ip|ip6}_forwarding
8930 8930 * as several routing daemons have unfortunately used this 'unpublished'
8931 8931 * but well-known ioctls.
8932 8932 */
8933 8933 /* ARGSUSED */
8934 8934 static void
8935 8935 ip_process_legacy_nddprop(queue_t *q, mblk_t *mp)
8936 8936 {
8937 8937 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
8938 8938 mblk_t *mp1 = mp->b_cont;
8939 8939 char *pname, *pval, *buf;
8940 8940 uint_t bufsize, proto;
8941 8941 mod_prop_info_t *pinfo = NULL;
8942 8942 ip_stack_t *ipst;
8943 8943 int err = 0;
8944 8944
8945 8945 ASSERT(CONN_Q(q));
8946 8946 ipst = CONNQ_TO_IPST(q);
8947 8947
8948 8948 if (iocp->ioc_count == 0 || mp1 == NULL) {
8949 8949 miocnak(q, mp, 0, EINVAL);
8950 8950 return;
8951 8951 }
8952 8952
8953 8953 mp1->b_datap->db_lim[-1] = '\0'; /* Force null termination */
8954 8954 pval = buf = pname = (char *)mp1->b_rptr;
8955 8955 bufsize = MBLKL(mp1);
8956 8956
8957 8957 if (strcmp(pname, "ip_forwarding") == 0) {
8958 8958 pname = "forwarding";
8959 8959 proto = MOD_PROTO_IPV4;
8960 8960 } else if (strcmp(pname, "ip6_forwarding") == 0) {
8961 8961 pname = "forwarding";
8962 8962 proto = MOD_PROTO_IPV6;
8963 8963 } else {
8964 8964 miocnak(q, mp, 0, EINVAL);
8965 8965 return;
8966 8966 }
8967 8967
8968 8968 pinfo = mod_prop_lookup(ipst->ips_propinfo_tbl, pname, proto);
8969 8969
8970 8970 switch (iocp->ioc_cmd) {
8971 8971 case ND_GET:
8972 8972 if ((err = pinfo->mpi_getf(ipst->ips_netstack, pinfo, NULL, buf,
8973 8973 bufsize, 0)) == 0) {
8974 8974 miocack(q, mp, iocp->ioc_count, 0);
8975 8975 return;
8976 8976 }
8977 8977 break;
8978 8978 case ND_SET:
8979 8979 /*
8980 8980 * buffer will have property name and value in the following
8981 8981 * format,
8982 8982 * <property name>'\0'<property value>'\0', extract them;
8983 8983 */
8984 8984 while (*pval++)
8985 8985 noop;
8986 8986
8987 8987 if (!*pval || pval >= (char *)mp1->b_wptr) {
8988 8988 err = EINVAL;
8989 8989 } else if ((err = pinfo->mpi_setf(ipst->ips_netstack, NULL,
8990 8990 pinfo, NULL, pval, 0)) == 0) {
8991 8991 miocack(q, mp, 0, 0);
8992 8992 return;
8993 8993 }
8994 8994 break;
8995 8995 default:
8996 8996 err = EINVAL;
8997 8997 break;
8998 8998 }
8999 8999 miocnak(q, mp, 0, err);
9000 9000 }
9001 9001
9002 9002 /*
9003 9003 * Wrapper function for resuming deferred ioctl processing
9004 9004 * Used for SIOCGDSTINFO, SIOCGIP6ADDRPOLICY, SIOCGMSFILTER,
9005 9005 * SIOCSMSFILTER, SIOCGIPMSFILTER, and SIOCSIPMSFILTER currently.
9006 9006 */
9007 9007 /* ARGSUSED */
9008 9008 void
9009 9009 ip_sioctl_copyin_resume(ipsq_t *dummy_ipsq, queue_t *q, mblk_t *mp,
9010 9010 void *dummy_arg)
9011 9011 {
9012 9012 ip_sioctl_copyin_setup(q, mp);
9013 9013 }
9014 9014
9015 9015 /*
9016 9016 * ip_sioctl_copyin_setup is called by ip_wput_nondata with any M_IOCTL message
9017 9017 * that arrives. Most of the IOCTLs are "socket" IOCTLs which we handle
9018 9018 * in either I_STR or TRANSPARENT form, using the mi_copy facility.
9019 9019 * We establish here the size of the block to be copied in. mi_copyin
9020 9020 * arranges for this to happen, an processing continues in ip_wput_nondata with
9021 9021 * an M_IOCDATA message.
9022 9022 */
9023 9023 void
9024 9024 ip_sioctl_copyin_setup(queue_t *q, mblk_t *mp)
9025 9025 {
9026 9026 int copyin_size;
9027 9027 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
9028 9028 ip_ioctl_cmd_t *ipip;
9029 9029 cred_t *cr;
9030 9030 ip_stack_t *ipst;
9031 9031
9032 9032 if (CONN_Q(q))
9033 9033 ipst = CONNQ_TO_IPST(q);
9034 9034 else
9035 9035 ipst = ILLQ_TO_IPST(q);
9036 9036
9037 9037 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
9038 9038 if (ipip == NULL) {
9039 9039 /*
9040 9040 * The ioctl is not one we understand or own.
9041 9041 * Pass it along to be processed down stream,
9042 9042 * if this is a module instance of IP, else nak
9043 9043 * the ioctl.
9044 9044 */
9045 9045 if (q->q_next == NULL) {
9046 9046 goto nak;
9047 9047 } else {
9048 9048 putnext(q, mp);
9049 9049 return;
9050 9050 }
9051 9051 }
9052 9052
9053 9053 /*
9054 9054 * If this is deferred, then we will do all the checks when we
9055 9055 * come back.
9056 9056 */
9057 9057 if ((iocp->ioc_cmd == SIOCGDSTINFO ||
9058 9058 iocp->ioc_cmd == SIOCGIP6ADDRPOLICY) && !ip6_asp_can_lookup(ipst)) {
9059 9059 ip6_asp_pending_op(q, mp, ip_sioctl_copyin_resume);
9060 9060 return;
9061 9061 }
9062 9062
9063 9063 /*
9064 9064 * Only allow a very small subset of IP ioctls on this stream if
9065 9065 * IP is a module and not a driver. Allowing ioctls to be processed
9066 9066 * in this case may cause assert failures or data corruption.
9067 9067 * Typically G[L]IFFLAGS, SLIFNAME/IF_UNITSEL are the only few
9068 9068 * ioctls allowed on an IP module stream, after which this stream
9069 9069 * normally becomes a multiplexor (at which time the stream head
9070 9070 * will fail all ioctls).
9071 9071 */
9072 9072 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
9073 9073 goto nak;
9074 9074 }
9075 9075
9076 9076 /* Make sure we have ioctl data to process. */
9077 9077 if (mp->b_cont == NULL && !(ipip->ipi_flags & IPI_NULL_BCONT))
9078 9078 goto nak;
9079 9079
9080 9080 /*
9081 9081 * Prefer dblk credential over ioctl credential; some synthesized
9082 9082 * ioctls have kcred set because there's no way to crhold()
9083 9083 * a credential in some contexts. (ioc_cr is not crfree() by
9084 9084 * the framework; the caller of ioctl needs to hold the reference
9085 9085 * for the duration of the call).
9086 9086 */
9087 9087 cr = msg_getcred(mp, NULL);
9088 9088 if (cr == NULL)
9089 9089 cr = iocp->ioc_cr;
9090 9090
9091 9091 /* Make sure normal users don't send down privileged ioctls */
9092 9092 if ((ipip->ipi_flags & IPI_PRIV) &&
9093 9093 (cr != NULL) && secpolicy_ip_config(cr, B_TRUE) != 0) {
9094 9094 /* We checked the privilege earlier but log it here */
9095 9095 miocnak(q, mp, 0, secpolicy_ip_config(cr, B_FALSE));
9096 9096 return;
9097 9097 }
9098 9098
9099 9099 /*
9100 9100 * The ioctl command tables can only encode fixed length
9101 9101 * ioctl data. If the length is variable, the table will
9102 9102 * encode the length as zero. Such special cases are handled
9103 9103 * below in the switch.
9104 9104 */
9105 9105 if (ipip->ipi_copyin_size != 0) {
9106 9106 mi_copyin(q, mp, NULL, ipip->ipi_copyin_size);
9107 9107 return;
9108 9108 }
9109 9109
9110 9110 switch (iocp->ioc_cmd) {
9111 9111 case O_SIOCGIFCONF:
9112 9112 case SIOCGIFCONF:
9113 9113 /*
9114 9114 * This IOCTL is hilarious. See comments in
9115 9115 * ip_sioctl_get_ifconf for the story.
9116 9116 */
9117 9117 if (iocp->ioc_count == TRANSPARENT)
9118 9118 copyin_size = SIZEOF_STRUCT(ifconf,
9119 9119 iocp->ioc_flag);
9120 9120 else
9121 9121 copyin_size = iocp->ioc_count;
9122 9122 mi_copyin(q, mp, NULL, copyin_size);
9123 9123 return;
9124 9124
9125 9125 case O_SIOCGLIFCONF:
9126 9126 case SIOCGLIFCONF:
9127 9127 copyin_size = SIZEOF_STRUCT(lifconf, iocp->ioc_flag);
9128 9128 mi_copyin(q, mp, NULL, copyin_size);
9129 9129 return;
9130 9130
9131 9131 case SIOCGLIFSRCOF:
9132 9132 copyin_size = SIZEOF_STRUCT(lifsrcof, iocp->ioc_flag);
9133 9133 mi_copyin(q, mp, NULL, copyin_size);
9134 9134 return;
9135 9135
9136 9136 case SIOCGIP6ADDRPOLICY:
9137 9137 ip_sioctl_ip6addrpolicy(q, mp);
9138 9138 ip6_asp_table_refrele(ipst);
9139 9139 return;
9140 9140
9141 9141 case SIOCSIP6ADDRPOLICY:
9142 9142 ip_sioctl_ip6addrpolicy(q, mp);
9143 9143 return;
9144 9144
9145 9145 case SIOCGDSTINFO:
9146 9146 ip_sioctl_dstinfo(q, mp);
9147 9147 ip6_asp_table_refrele(ipst);
9148 9148 return;
9149 9149
9150 9150 case ND_SET:
9151 9151 case ND_GET:
9152 9152 ip_process_legacy_nddprop(q, mp);
9153 9153 return;
9154 9154
9155 9155 case SIOCSETPROP:
9156 9156 case SIOCGETPROP:
9157 9157 ip_sioctl_getsetprop(q, mp);
9158 9158 return;
9159 9159
9160 9160 case I_PLINK:
9161 9161 case I_PUNLINK:
9162 9162 case I_LINK:
9163 9163 case I_UNLINK:
9164 9164 /*
9165 9165 * We treat non-persistent link similarly as the persistent
9166 9166 * link case, in terms of plumbing/unplumbing, as well as
9167 9167 * dynamic re-plumbing events indicator. See comments
9168 9168 * in ip_sioctl_plink() for more.
9169 9169 *
9170 9170 * Request can be enqueued in the 'ipsq' while waiting
9171 9171 * to become exclusive. So bump up the conn ref.
9172 9172 */
9173 9173 if (CONN_Q(q)) {
9174 9174 CONN_INC_REF(Q_TO_CONN(q));
9175 9175 CONN_INC_IOCTLREF(Q_TO_CONN(q))
9176 9176 }
9177 9177 ip_sioctl_plink(NULL, q, mp, NULL);
9178 9178 return;
9179 9179
9180 9180 case IP_IOCTL:
9181 9181 ip_wput_ioctl(q, mp);
9182 9182 return;
9183 9183
9184 9184 case SIOCILB:
9185 9185 /* The ioctl length varies depending on the ILB command. */
9186 9186 copyin_size = iocp->ioc_count;
9187 9187 if (copyin_size < sizeof (ilb_cmd_t))
9188 9188 goto nak;
9189 9189 mi_copyin(q, mp, NULL, copyin_size);
9190 9190 return;
9191 9191
9192 9192 default:
9193 9193 cmn_err(CE_WARN, "Unknown ioctl %d/0x%x slipped through.",
9194 9194 iocp->ioc_cmd, iocp->ioc_cmd);
9195 9195 /* FALLTHRU */
9196 9196 }
9197 9197 nak:
9198 9198 if (mp->b_cont != NULL) {
9199 9199 freemsg(mp->b_cont);
9200 9200 mp->b_cont = NULL;
9201 9201 }
9202 9202 iocp->ioc_error = EINVAL;
9203 9203 mp->b_datap->db_type = M_IOCNAK;
9204 9204 iocp->ioc_count = 0;
9205 9205 qreply(q, mp);
9206 9206 }
9207 9207
9208 9208 static void
9209 9209 ip_sioctl_garp_reply(mblk_t *mp, ill_t *ill, void *hwaddr, int flags)
9210 9210 {
9211 9211 struct arpreq *ar;
9212 9212 struct xarpreq *xar;
9213 9213 mblk_t *tmp;
9214 9214 struct iocblk *iocp;
9215 9215 int x_arp_ioctl = B_FALSE;
9216 9216 int *flagsp;
9217 9217 char *storage = NULL;
9218 9218
9219 9219 ASSERT(ill != NULL);
9220 9220
9221 9221 iocp = (struct iocblk *)mp->b_rptr;
9222 9222 ASSERT(iocp->ioc_cmd == SIOCGXARP || iocp->ioc_cmd == SIOCGARP);
9223 9223
9224 9224 tmp = (mp->b_cont)->b_cont; /* xarpreq/arpreq */
9225 9225 if ((iocp->ioc_cmd == SIOCGXARP) ||
9226 9226 (iocp->ioc_cmd == SIOCSXARP)) {
9227 9227 x_arp_ioctl = B_TRUE;
9228 9228 xar = (struct xarpreq *)tmp->b_rptr;
9229 9229 flagsp = &xar->xarp_flags;
9230 9230 storage = xar->xarp_ha.sdl_data;
9231 9231 } else {
9232 9232 ar = (struct arpreq *)tmp->b_rptr;
9233 9233 flagsp = &ar->arp_flags;
9234 9234 storage = ar->arp_ha.sa_data;
9235 9235 }
9236 9236
9237 9237 /*
9238 9238 * We're done if this is not an SIOCG{X}ARP
9239 9239 */
9240 9240 if (x_arp_ioctl) {
9241 9241 storage += ill_xarp_info(&xar->xarp_ha, ill);
9242 9242 if ((ill->ill_phys_addr_length + ill->ill_name_length) >
9243 9243 sizeof (xar->xarp_ha.sdl_data)) {
9244 9244 iocp->ioc_error = EINVAL;
9245 9245 return;
9246 9246 }
9247 9247 }
9248 9248 *flagsp = ATF_INUSE;
9249 9249 /*
9250 9250 * If /sbin/arp told us we are the authority using the "permanent"
9251 9251 * flag, or if this is one of my addresses print "permanent"
9252 9252 * in the /sbin/arp output.
9253 9253 */
9254 9254 if ((flags & NCE_F_MYADDR) || (flags & NCE_F_AUTHORITY))
9255 9255 *flagsp |= ATF_AUTHORITY;
9256 9256 if (flags & NCE_F_NONUD)
9257 9257 *flagsp |= ATF_PERM; /* not subject to aging */
9258 9258 if (flags & NCE_F_PUBLISH)
9259 9259 *flagsp |= ATF_PUBL;
9260 9260 if (hwaddr != NULL) {
9261 9261 *flagsp |= ATF_COM;
9262 9262 bcopy((char *)hwaddr, storage, ill->ill_phys_addr_length);
9263 9263 }
9264 9264 }
9265 9265
9266 9266 /*
9267 9267 * Create a new logical interface. If ipif_id is zero (i.e. not a logical
9268 9268 * interface) create the next available logical interface for this
9269 9269 * physical interface.
9270 9270 * If ipif is NULL (i.e. the lookup didn't find one) attempt to create an
9271 9271 * ipif with the specified name.
9272 9272 *
9273 9273 * If the address family is not AF_UNSPEC then set the address as well.
9274 9274 *
9275 9275 * If ip_sioctl_addr returns EINPROGRESS then the ioctl (the copyout)
9276 9276 * is completed when the DL_BIND_ACK arrive in ip_rput_dlpi_writer.
9277 9277 *
9278 9278 * Executed as a writer on the ill.
9279 9279 * So no lock is needed to traverse the ipif chain, or examine the
9280 9280 * phyint flags.
9281 9281 */
9282 9282 /* ARGSUSED */
9283 9283 int
9284 9284 ip_sioctl_addif(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
9285 9285 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9286 9286 {
9287 9287 mblk_t *mp1;
9288 9288 struct lifreq *lifr;
9289 9289 boolean_t isv6;
9290 9290 boolean_t exists;
9291 9291 char *name;
9292 9292 char *endp;
9293 9293 char *cp;
9294 9294 int namelen;
9295 9295 ipif_t *ipif;
9296 9296 long id;
9297 9297 ipsq_t *ipsq;
9298 9298 ill_t *ill;
9299 9299 sin_t *sin;
9300 9300 int err = 0;
9301 9301 boolean_t found_sep = B_FALSE;
9302 9302 conn_t *connp;
9303 9303 zoneid_t zoneid;
9304 9304 ip_stack_t *ipst = CONNQ_TO_IPST(q);
9305 9305
9306 9306 ASSERT(q->q_next == NULL);
9307 9307 ip1dbg(("ip_sioctl_addif\n"));
9308 9308 /* Existence of mp1 has been checked in ip_wput_nondata */
9309 9309 mp1 = mp->b_cont->b_cont;
9310 9310 /*
9311 9311 * Null terminate the string to protect against buffer
9312 9312 * overrun. String was generated by user code and may not
9313 9313 * be trusted.
9314 9314 */
9315 9315 lifr = (struct lifreq *)mp1->b_rptr;
9316 9316 lifr->lifr_name[LIFNAMSIZ - 1] = '\0';
9317 9317 name = lifr->lifr_name;
9318 9318 ASSERT(CONN_Q(q));
9319 9319 connp = Q_TO_CONN(q);
9320 9320 isv6 = (connp->conn_family == AF_INET6);
9321 9321 zoneid = connp->conn_zoneid;
9322 9322 namelen = mi_strlen(name);
9323 9323 if (namelen == 0)
9324 9324 return (EINVAL);
9325 9325
9326 9326 exists = B_FALSE;
9327 9327 if ((namelen + 1 == sizeof (ipif_loopback_name)) &&
9328 9328 (mi_strcmp(name, ipif_loopback_name) == 0)) {
9329 9329 /*
9330 9330 * Allow creating lo0 using SIOCLIFADDIF.
9331 9331 * can't be any other writer thread. So can pass null below
9332 9332 * for the last 4 args to ipif_lookup_name.
9333 9333 */
9334 9334 ipif = ipif_lookup_on_name(lifr->lifr_name, namelen, B_TRUE,
9335 9335 &exists, isv6, zoneid, ipst);
9336 9336 /* Prevent any further action */
9337 9337 if (ipif == NULL) {
9338 9338 return (ENOBUFS);
9339 9339 } else if (!exists) {
9340 9340 /* We created the ipif now and as writer */
9341 9341 ipif_refrele(ipif);
9342 9342 return (0);
9343 9343 } else {
9344 9344 ill = ipif->ipif_ill;
9345 9345 ill_refhold(ill);
9346 9346 ipif_refrele(ipif);
9347 9347 }
9348 9348 } else {
9349 9349 /* Look for a colon in the name. */
9350 9350 endp = &name[namelen];
9351 9351 for (cp = endp; --cp > name; ) {
9352 9352 if (*cp == IPIF_SEPARATOR_CHAR) {
9353 9353 found_sep = B_TRUE;
9354 9354 /*
9355 9355 * Reject any non-decimal aliases for plumbing
9356 9356 * of logical interfaces. Aliases with leading
9357 9357 * zeroes are also rejected as they introduce
9358 9358 * ambiguity in the naming of the interfaces.
9359 9359 * Comparing with "0" takes care of all such
9360 9360 * cases.
9361 9361 */
9362 9362 if ((strncmp("0", cp+1, 1)) == 0)
9363 9363 return (EINVAL);
9364 9364
9365 9365 if (ddi_strtol(cp+1, &endp, 10, &id) != 0 ||
9366 9366 id <= 0 || *endp != '\0') {
9367 9367 return (EINVAL);
9368 9368 }
9369 9369 *cp = '\0';
9370 9370 break;
9371 9371 }
9372 9372 }
9373 9373 ill = ill_lookup_on_name(name, B_FALSE, isv6, NULL, ipst);
9374 9374 if (found_sep)
9375 9375 *cp = IPIF_SEPARATOR_CHAR;
9376 9376 if (ill == NULL)
9377 9377 return (ENXIO);
9378 9378 }
9379 9379
9380 9380 ipsq = ipsq_try_enter(NULL, ill, q, mp, ip_process_ioctl, NEW_OP,
9381 9381 B_TRUE);
9382 9382
9383 9383 /*
9384 9384 * Release the refhold due to the lookup, now that we are excl
9385 9385 * or we are just returning
9386 9386 */
9387 9387 ill_refrele(ill);
9388 9388
9389 9389 if (ipsq == NULL)
9390 9390 return (EINPROGRESS);
9391 9391
9392 9392 /* We are now exclusive on the IPSQ */
9393 9393 ASSERT(IAM_WRITER_ILL(ill));
9394 9394
9395 9395 if (found_sep) {
9396 9396 /* Now see if there is an IPIF with this unit number. */
9397 9397 for (ipif = ill->ill_ipif; ipif != NULL;
9398 9398 ipif = ipif->ipif_next) {
9399 9399 if (ipif->ipif_id == id) {
9400 9400 err = EEXIST;
9401 9401 goto done;
9402 9402 }
9403 9403 }
9404 9404 }
9405 9405
9406 9406 /*
9407 9407 * We use IRE_LOCAL for lo0:1 etc. for "receive only" use
9408 9408 * of lo0. Plumbing for lo0:0 happens in ipif_lookup_on_name()
9409 9409 * instead.
9410 9410 */
9411 9411 if ((ipif = ipif_allocate(ill, found_sep ? id : -1, IRE_LOCAL,
9412 9412 B_TRUE, B_TRUE, &err)) == NULL) {
9413 9413 goto done;
9414 9414 }
9415 9415
9416 9416 /* Return created name with ioctl */
9417 9417 (void) sprintf(lifr->lifr_name, "%s%c%d", ill->ill_name,
9418 9418 IPIF_SEPARATOR_CHAR, ipif->ipif_id);
9419 9419 ip1dbg(("created %s\n", lifr->lifr_name));
9420 9420
9421 9421 /* Set address */
9422 9422 sin = (sin_t *)&lifr->lifr_addr;
9423 9423 if (sin->sin_family != AF_UNSPEC) {
9424 9424 err = ip_sioctl_addr(ipif, sin, q, mp,
9425 9425 &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], lifr);
9426 9426 }
9427 9427
9428 9428 done:
9429 9429 ipsq_exit(ipsq);
9430 9430 return (err);
9431 9431 }
9432 9432
9433 9433 /*
9434 9434 * Remove an existing logical interface. If ipif_id is zero (i.e. not a logical
9435 9435 * interface) delete it based on the IP address (on this physical interface).
9436 9436 * Otherwise delete it based on the ipif_id.
9437 9437 * Also, special handling to allow a removeif of lo0.
9438 9438 */
9439 9439 /* ARGSUSED */
9440 9440 int
9441 9441 ip_sioctl_removeif(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9442 9442 ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9443 9443 {
9444 9444 conn_t *connp;
9445 9445 ill_t *ill = ipif->ipif_ill;
9446 9446 boolean_t success;
9447 9447 ip_stack_t *ipst;
9448 9448
9449 9449 ipst = CONNQ_TO_IPST(q);
9450 9450
9451 9451 ASSERT(q->q_next == NULL);
9452 9452 ip1dbg(("ip_sioctl_remove_if(%s:%u %p)\n",
9453 9453 ill->ill_name, ipif->ipif_id, (void *)ipif));
9454 9454 ASSERT(IAM_WRITER_IPIF(ipif));
9455 9455
9456 9456 connp = Q_TO_CONN(q);
9457 9457 /*
9458 9458 * Special case for unplumbing lo0 (the loopback physical interface).
9459 9459 * If unplumbing lo0, the incoming address structure has been
9460 9460 * initialized to all zeros. When unplumbing lo0, all its logical
9461 9461 * interfaces must be removed too.
9462 9462 *
9463 9463 * Note that this interface may be called to remove a specific
9464 9464 * loopback logical interface (eg, lo0:1). But in that case
9465 9465 * ipif->ipif_id != 0 so that the code path for that case is the
9466 9466 * same as any other interface (meaning it skips the code directly
9467 9467 * below).
9468 9468 */
9469 9469 if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9470 9470 if (sin->sin_family == AF_UNSPEC &&
9471 9471 (IN6_IS_ADDR_UNSPECIFIED(&((sin6_t *)sin)->sin6_addr))) {
9472 9472 /*
9473 9473 * Mark it condemned. No new ref. will be made to ill.
9474 9474 */
9475 9475 mutex_enter(&ill->ill_lock);
9476 9476 ill->ill_state_flags |= ILL_CONDEMNED;
9477 9477 for (ipif = ill->ill_ipif; ipif != NULL;
9478 9478 ipif = ipif->ipif_next) {
9479 9479 ipif->ipif_state_flags |= IPIF_CONDEMNED;
9480 9480 }
9481 9481 mutex_exit(&ill->ill_lock);
9482 9482
9483 9483 ipif = ill->ill_ipif;
9484 9484 /* unplumb the loopback interface */
9485 9485 ill_delete(ill);
9486 9486 mutex_enter(&connp->conn_lock);
9487 9487 mutex_enter(&ill->ill_lock);
9488 9488
9489 9489 /* Are any references to this ill active */
9490 9490 if (ill_is_freeable(ill)) {
9491 9491 mutex_exit(&ill->ill_lock);
9492 9492 mutex_exit(&connp->conn_lock);
9493 9493 ill_delete_tail(ill);
9494 9494 mi_free(ill);
9495 9495 return (0);
9496 9496 }
9497 9497 success = ipsq_pending_mp_add(connp, ipif,
9498 9498 CONNP_TO_WQ(connp), mp, ILL_FREE);
9499 9499 mutex_exit(&connp->conn_lock);
9500 9500 mutex_exit(&ill->ill_lock);
9501 9501 if (success)
9502 9502 return (EINPROGRESS);
9503 9503 else
9504 9504 return (EINTR);
9505 9505 }
9506 9506 }
9507 9507
9508 9508 if (ipif->ipif_id == 0) {
9509 9509 ipsq_t *ipsq;
9510 9510
9511 9511 /* Find based on address */
9512 9512 if (ipif->ipif_isv6) {
9513 9513 sin6_t *sin6;
9514 9514
9515 9515 if (sin->sin_family != AF_INET6)
9516 9516 return (EAFNOSUPPORT);
9517 9517
9518 9518 sin6 = (sin6_t *)sin;
9519 9519 /* We are a writer, so we should be able to lookup */
9520 9520 ipif = ipif_lookup_addr_exact_v6(&sin6->sin6_addr, ill,
9521 9521 ipst);
9522 9522 } else {
9523 9523 if (sin->sin_family != AF_INET)
9524 9524 return (EAFNOSUPPORT);
9525 9525
9526 9526 /* We are a writer, so we should be able to lookup */
9527 9527 ipif = ipif_lookup_addr_exact(sin->sin_addr.s_addr, ill,
9528 9528 ipst);
9529 9529 }
9530 9530 if (ipif == NULL) {
9531 9531 return (EADDRNOTAVAIL);
9532 9532 }
9533 9533
9534 9534 /*
9535 9535 * It is possible for a user to send an SIOCLIFREMOVEIF with
9536 9536 * lifr_name of the physical interface but with an ip address
9537 9537 * lifr_addr of a logical interface plumbed over it.
9538 9538 * So update ipx_current_ipif now that ipif points to the
9539 9539 * correct one.
9540 9540 */
9541 9541 ipsq = ipif->ipif_ill->ill_phyint->phyint_ipsq;
9542 9542 ipsq->ipsq_xop->ipx_current_ipif = ipif;
9543 9543
9544 9544 /* This is a writer */
9545 9545 ipif_refrele(ipif);
9546 9546 }
9547 9547
9548 9548 /*
9549 9549 * Can not delete instance zero since it is tied to the ill.
9550 9550 */
9551 9551 if (ipif->ipif_id == 0)
9552 9552 return (EBUSY);
9553 9553
9554 9554 mutex_enter(&ill->ill_lock);
9555 9555 ipif->ipif_state_flags |= IPIF_CONDEMNED;
9556 9556 mutex_exit(&ill->ill_lock);
9557 9557
9558 9558 ipif_free(ipif);
9559 9559
9560 9560 mutex_enter(&connp->conn_lock);
9561 9561 mutex_enter(&ill->ill_lock);
9562 9562
9563 9563 /* Are any references to this ipif active */
9564 9564 if (ipif_is_freeable(ipif)) {
9565 9565 mutex_exit(&ill->ill_lock);
9566 9566 mutex_exit(&connp->conn_lock);
9567 9567 ipif_non_duplicate(ipif);
9568 9568 (void) ipif_down_tail(ipif);
9569 9569 ipif_free_tail(ipif); /* frees ipif */
9570 9570 return (0);
9571 9571 }
9572 9572 success = ipsq_pending_mp_add(connp, ipif, CONNP_TO_WQ(connp), mp,
9573 9573 IPIF_FREE);
9574 9574 mutex_exit(&ill->ill_lock);
9575 9575 mutex_exit(&connp->conn_lock);
9576 9576 if (success)
9577 9577 return (EINPROGRESS);
9578 9578 else
9579 9579 return (EINTR);
9580 9580 }
9581 9581
9582 9582 /*
9583 9583 * Restart the removeif ioctl. The refcnt has gone down to 0.
9584 9584 * The ipif is already condemned. So can't find it thru lookups.
9585 9585 */
9586 9586 /* ARGSUSED */
9587 9587 int
9588 9588 ip_sioctl_removeif_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q,
9589 9589 mblk_t *mp, ip_ioctl_cmd_t *ipip, void *dummy_if_req)
9590 9590 {
9591 9591 ill_t *ill = ipif->ipif_ill;
9592 9592
9593 9593 ASSERT(IAM_WRITER_IPIF(ipif));
9594 9594 ASSERT(ipif->ipif_state_flags & IPIF_CONDEMNED);
9595 9595
9596 9596 ip1dbg(("ip_sioctl_removeif_restart(%s:%u %p)\n",
9597 9597 ill->ill_name, ipif->ipif_id, (void *)ipif));
9598 9598
9599 9599 if (ipif->ipif_id == 0 && ill->ill_net_type == IRE_LOOPBACK) {
9600 9600 ASSERT(ill->ill_state_flags & ILL_CONDEMNED);
9601 9601 ill_delete_tail(ill);
9602 9602 mi_free(ill);
9603 9603 return (0);
9604 9604 }
9605 9605
9606 9606 ipif_non_duplicate(ipif);
9607 9607 (void) ipif_down_tail(ipif);
9608 9608 ipif_free_tail(ipif);
9609 9609
9610 9610 return (0);
9611 9611 }
9612 9612
9613 9613 /*
9614 9614 * Set the local interface address using the given prefix and ill_token.
9615 9615 */
9616 9616 /* ARGSUSED */
9617 9617 int
9618 9618 ip_sioctl_prefix(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9619 9619 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9620 9620 {
9621 9621 int err;
9622 9622 in6_addr_t v6addr;
9623 9623 sin6_t *sin6;
9624 9624 ill_t *ill;
9625 9625 int i;
9626 9626
9627 9627 ip1dbg(("ip_sioctl_prefix(%s:%u %p)\n",
9628 9628 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9629 9629
9630 9630 ASSERT(IAM_WRITER_IPIF(ipif));
9631 9631
9632 9632 if (!ipif->ipif_isv6)
9633 9633 return (EINVAL);
9634 9634
9635 9635 if (sin->sin_family != AF_INET6)
9636 9636 return (EAFNOSUPPORT);
9637 9637
9638 9638 sin6 = (sin6_t *)sin;
9639 9639 v6addr = sin6->sin6_addr;
9640 9640 ill = ipif->ipif_ill;
9641 9641
9642 9642 if (IN6_IS_ADDR_UNSPECIFIED(&v6addr) ||
9643 9643 IN6_IS_ADDR_UNSPECIFIED(&ill->ill_token))
9644 9644 return (EADDRNOTAVAIL);
9645 9645
9646 9646 for (i = 0; i < 4; i++)
9647 9647 sin6->sin6_addr.s6_addr32[i] |= ill->ill_token.s6_addr32[i];
9648 9648
9649 9649 err = ip_sioctl_addr(ipif, sin, q, mp,
9650 9650 &ip_ndx_ioctl_table[SIOCLIFADDR_NDX], dummy_ifreq);
9651 9651 return (err);
9652 9652 }
9653 9653
9654 9654 /*
9655 9655 * Restart entry point to restart the address set operation after the
9656 9656 * refcounts have dropped to zero.
9657 9657 */
9658 9658 /* ARGSUSED */
9659 9659 int
9660 9660 ip_sioctl_prefix_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9661 9661 ip_ioctl_cmd_t *ipip, void *ifreq)
9662 9662 {
9663 9663 ip1dbg(("ip_sioctl_prefix_restart(%s:%u %p)\n",
9664 9664 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9665 9665 return (ip_sioctl_addr_restart(ipif, sin, q, mp, ipip, ifreq));
9666 9666 }
9667 9667
9668 9668 /*
9669 9669 * Set the local interface address.
9670 9670 * Allow an address of all zero when the interface is down.
9671 9671 */
9672 9672 /* ARGSUSED */
9673 9673 int
9674 9674 ip_sioctl_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9675 9675 ip_ioctl_cmd_t *dummy_ipip, void *dummy_ifreq)
9676 9676 {
9677 9677 int err = 0;
9678 9678 in6_addr_t v6addr;
9679 9679 boolean_t need_up = B_FALSE;
9680 9680 ill_t *ill;
9681 9681 int i;
9682 9682
9683 9683 ip1dbg(("ip_sioctl_addr(%s:%u %p)\n",
9684 9684 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9685 9685
9686 9686 ASSERT(IAM_WRITER_IPIF(ipif));
9687 9687
9688 9688 ill = ipif->ipif_ill;
9689 9689 if (ipif->ipif_isv6) {
9690 9690 sin6_t *sin6;
9691 9691 phyint_t *phyi;
9692 9692
9693 9693 if (sin->sin_family != AF_INET6)
9694 9694 return (EAFNOSUPPORT);
9695 9695
9696 9696 sin6 = (sin6_t *)sin;
9697 9697 v6addr = sin6->sin6_addr;
9698 9698 phyi = ill->ill_phyint;
9699 9699
9700 9700 /*
9701 9701 * Enforce that true multicast interfaces have a link-local
9702 9702 * address for logical unit 0.
9703 9703 *
9704 9704 * However for those ipif's for which link-local address was
9705 9705 * not created by default, also allow setting :: as the address.
9706 9706 * This scenario would arise, when we delete an address on ipif
9707 9707 * with logical unit 0, we would want to set :: as the address.
9708 9708 */
9709 9709 if (ipif->ipif_id == 0 &&
9710 9710 (ill->ill_flags & ILLF_MULTICAST) &&
9711 9711 !(ipif->ipif_flags & (IPIF_POINTOPOINT)) &&
9712 9712 !(phyi->phyint_flags & (PHYI_LOOPBACK)) &&
9713 9713 !IN6_IS_ADDR_LINKLOCAL(&v6addr)) {
9714 9714
9715 9715 /*
9716 9716 * if default link-local was not created by kernel for
9717 9717 * this ill, allow setting :: as the address on ipif:0.
9718 9718 */
9719 9719 if (ill->ill_flags & ILLF_NOLINKLOCAL) {
9720 9720 if (!IN6_IS_ADDR_UNSPECIFIED(&v6addr))
9721 9721 return (EADDRNOTAVAIL);
9722 9722 } else {
9723 9723 return (EADDRNOTAVAIL);
9724 9724 }
9725 9725 }
9726 9726
9727 9727 /*
9728 9728 * up interfaces shouldn't have the unspecified address
9729 9729 * unless they also have the IPIF_NOLOCAL flags set and
9730 9730 * have a subnet assigned.
9731 9731 */
9732 9732 if ((ipif->ipif_flags & IPIF_UP) &&
9733 9733 IN6_IS_ADDR_UNSPECIFIED(&v6addr) &&
9734 9734 (!(ipif->ipif_flags & IPIF_NOLOCAL) ||
9735 9735 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) {
9736 9736 return (EADDRNOTAVAIL);
9737 9737 }
9738 9738
9739 9739 if (!ip_local_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9740 9740 return (EADDRNOTAVAIL);
9741 9741 } else {
9742 9742 ipaddr_t addr;
9743 9743
9744 9744 if (sin->sin_family != AF_INET)
9745 9745 return (EAFNOSUPPORT);
9746 9746
9747 9747 addr = sin->sin_addr.s_addr;
9748 9748
9749 9749 /* Allow INADDR_ANY as the local address. */
9750 9750 if (addr != INADDR_ANY &&
9751 9751 !ip_addr_ok_v4(addr, ipif->ipif_net_mask))
9752 9752 return (EADDRNOTAVAIL);
9753 9753
9754 9754 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9755 9755 }
9756 9756 /*
9757 9757 * verify that the address being configured is permitted by the
9758 9758 * ill_allowed_ips[] for the interface.
9759 9759 */
9760 9760 if (ill->ill_allowed_ips_cnt > 0) {
9761 9761 for (i = 0; i < ill->ill_allowed_ips_cnt; i++) {
9762 9762 if (IN6_ARE_ADDR_EQUAL(&ill->ill_allowed_ips[i],
9763 9763 &v6addr))
9764 9764 break;
9765 9765 }
9766 9766 if (i == ill->ill_allowed_ips_cnt) {
9767 9767 pr_addr_dbg("!allowed addr %s\n", AF_INET6, &v6addr);
9768 9768 return (EPERM);
9769 9769 }
9770 9770 }
9771 9771 /*
9772 9772 * Even if there is no change we redo things just to rerun
9773 9773 * ipif_set_default.
9774 9774 */
9775 9775 if (ipif->ipif_flags & IPIF_UP) {
9776 9776 /*
9777 9777 * Setting a new local address, make sure
9778 9778 * we have net and subnet bcast ire's for
9779 9779 * the old address if we need them.
9780 9780 */
9781 9781 /*
9782 9782 * If the interface is already marked up,
9783 9783 * we call ipif_down which will take care
9784 9784 * of ditching any IREs that have been set
9785 9785 * up based on the old interface address.
9786 9786 */
9787 9787 err = ipif_logical_down(ipif, q, mp);
9788 9788 if (err == EINPROGRESS)
9789 9789 return (err);
9790 9790 (void) ipif_down_tail(ipif);
9791 9791 need_up = 1;
9792 9792 }
9793 9793
9794 9794 err = ip_sioctl_addr_tail(ipif, sin, q, mp, need_up);
9795 9795 return (err);
9796 9796 }
9797 9797
9798 9798 int
9799 9799 ip_sioctl_addr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9800 9800 boolean_t need_up)
9801 9801 {
9802 9802 in6_addr_t v6addr;
9803 9803 in6_addr_t ov6addr;
9804 9804 ipaddr_t addr;
9805 9805 sin6_t *sin6;
9806 9806 int sinlen;
9807 9807 int err = 0;
9808 9808 ill_t *ill = ipif->ipif_ill;
9809 9809 boolean_t need_dl_down;
9810 9810 boolean_t need_arp_down;
9811 9811 struct iocblk *iocp;
9812 9812
9813 9813 iocp = (mp != NULL) ? (struct iocblk *)mp->b_rptr : NULL;
9814 9814
9815 9815 ip1dbg(("ip_sioctl_addr_tail(%s:%u %p)\n",
9816 9816 ill->ill_name, ipif->ipif_id, (void *)ipif));
9817 9817 ASSERT(IAM_WRITER_IPIF(ipif));
9818 9818
9819 9819 /* Must cancel any pending timer before taking the ill_lock */
9820 9820 if (ipif->ipif_recovery_id != 0)
9821 9821 (void) untimeout(ipif->ipif_recovery_id);
9822 9822 ipif->ipif_recovery_id = 0;
9823 9823
9824 9824 if (ipif->ipif_isv6) {
9825 9825 sin6 = (sin6_t *)sin;
9826 9826 v6addr = sin6->sin6_addr;
9827 9827 sinlen = sizeof (struct sockaddr_in6);
9828 9828 } else {
9829 9829 addr = sin->sin_addr.s_addr;
9830 9830 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
9831 9831 sinlen = sizeof (struct sockaddr_in);
9832 9832 }
9833 9833 mutex_enter(&ill->ill_lock);
9834 9834 ov6addr = ipif->ipif_v6lcl_addr;
9835 9835 ipif->ipif_v6lcl_addr = v6addr;
9836 9836 sctp_update_ipif_addr(ipif, ov6addr);
9837 9837 ipif->ipif_addr_ready = 0;
9838 9838
9839 9839 ip_rts_newaddrmsg(RTM_CHGADDR, 0, ipif, RTSQ_DEFAULT);
9840 9840
9841 9841 /*
9842 9842 * If the interface was previously marked as a duplicate, then since
9843 9843 * we've now got a "new" address, it should no longer be considered a
9844 9844 * duplicate -- even if the "new" address is the same as the old one.
9845 9845 * Note that if all ipifs are down, we may have a pending ARP down
9846 9846 * event to handle. This is because we want to recover from duplicates
9847 9847 * and thus delay tearing down ARP until the duplicates have been
9848 9848 * removed or disabled.
9849 9849 */
9850 9850 need_dl_down = need_arp_down = B_FALSE;
9851 9851 if (ipif->ipif_flags & IPIF_DUPLICATE) {
9852 9852 need_arp_down = !need_up;
9853 9853 ipif->ipif_flags &= ~IPIF_DUPLICATE;
9854 9854 if (--ill->ill_ipif_dup_count == 0 && !need_up &&
9855 9855 ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
9856 9856 need_dl_down = B_TRUE;
9857 9857 }
9858 9858 }
9859 9859
9860 9860 ipif_set_default(ipif);
9861 9861
9862 9862 /*
9863 9863 * If we've just manually set the IPv6 link-local address (0th ipif),
9864 9864 * tag the ill so that future updates to the interface ID don't result
9865 9865 * in this address getting automatically reconfigured from under the
9866 9866 * administrator.
9867 9867 */
9868 9868 if (ipif->ipif_isv6 && ipif->ipif_id == 0) {
9869 9869 if (iocp == NULL || (iocp->ioc_cmd == SIOCSLIFADDR &&
9870 9870 !IN6_IS_ADDR_UNSPECIFIED(&v6addr)))
9871 9871 ill->ill_manual_linklocal = 1;
9872 9872 }
9873 9873
9874 9874 /*
9875 9875 * When publishing an interface address change event, we only notify
9876 9876 * the event listeners of the new address. It is assumed that if they
9877 9877 * actively care about the addresses assigned that they will have
9878 9878 * already discovered the previous address assigned (if there was one.)
9879 9879 *
9880 9880 * Don't attach nic event message for SIOCLIFADDIF ioctl.
9881 9881 */
9882 9882 if (iocp != NULL && iocp->ioc_cmd != SIOCLIFADDIF) {
9883 9883 ill_nic_event_dispatch(ill, MAP_IPIF_ID(ipif->ipif_id),
9884 9884 NE_ADDRESS_CHANGE, sin, sinlen);
9885 9885 }
9886 9886
9887 9887 mutex_exit(&ill->ill_lock);
9888 9888
9889 9889 if (need_up) {
9890 9890 /*
9891 9891 * Now bring the interface back up. If this
9892 9892 * is the only IPIF for the ILL, ipif_up
9893 9893 * will have to re-bind to the device, so
9894 9894 * we may get back EINPROGRESS, in which
9895 9895 * case, this IOCTL will get completed in
9896 9896 * ip_rput_dlpi when we see the DL_BIND_ACK.
9897 9897 */
9898 9898 err = ipif_up(ipif, q, mp);
9899 9899 } else {
9900 9900 /* Perhaps ilgs should use this ill */
9901 9901 update_conn_ill(NULL, ill->ill_ipst);
9902 9902 }
9903 9903
9904 9904 if (need_dl_down)
9905 9905 ill_dl_down(ill);
9906 9906
9907 9907 if (need_arp_down && !ill->ill_isv6)
9908 9908 (void) ipif_arp_down(ipif);
9909 9909
9910 9910 /*
9911 9911 * The default multicast interface might have changed (for
9912 9912 * instance if the IPv6 scope of the address changed)
9913 9913 */
9914 9914 ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
9915 9915
9916 9916 return (err);
9917 9917 }
9918 9918
9919 9919 /*
9920 9920 * Restart entry point to restart the address set operation after the
9921 9921 * refcounts have dropped to zero.
9922 9922 */
9923 9923 /* ARGSUSED */
9924 9924 int
9925 9925 ip_sioctl_addr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9926 9926 ip_ioctl_cmd_t *ipip, void *ifreq)
9927 9927 {
9928 9928 ip1dbg(("ip_sioctl_addr_restart(%s:%u %p)\n",
9929 9929 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9930 9930 ASSERT(IAM_WRITER_IPIF(ipif));
9931 9931 (void) ipif_down_tail(ipif);
9932 9932 return (ip_sioctl_addr_tail(ipif, sin, q, mp, B_TRUE));
9933 9933 }
9934 9934
9935 9935 /* ARGSUSED */
9936 9936 int
9937 9937 ip_sioctl_get_addr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9938 9938 ip_ioctl_cmd_t *ipip, void *if_req)
9939 9939 {
9940 9940 sin6_t *sin6 = (struct sockaddr_in6 *)sin;
9941 9941 struct lifreq *lifr = (struct lifreq *)if_req;
9942 9942
9943 9943 ip1dbg(("ip_sioctl_get_addr(%s:%u %p)\n",
9944 9944 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9945 9945 /*
9946 9946 * The net mask and address can't change since we have a
9947 9947 * reference to the ipif. So no lock is necessary.
9948 9948 */
9949 9949 if (ipif->ipif_isv6) {
9950 9950 *sin6 = sin6_null;
9951 9951 sin6->sin6_family = AF_INET6;
9952 9952 sin6->sin6_addr = ipif->ipif_v6lcl_addr;
9953 9953 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
9954 9954 lifr->lifr_addrlen =
9955 9955 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
9956 9956 } else {
9957 9957 *sin = sin_null;
9958 9958 sin->sin_family = AF_INET;
9959 9959 sin->sin_addr.s_addr = ipif->ipif_lcl_addr;
9960 9960 if (ipip->ipi_cmd_type == LIF_CMD) {
9961 9961 lifr->lifr_addrlen =
9962 9962 ip_mask_to_plen(ipif->ipif_net_mask);
9963 9963 }
9964 9964 }
9965 9965 return (0);
9966 9966 }
9967 9967
9968 9968 /*
9969 9969 * Set the destination address for a pt-pt interface.
9970 9970 */
9971 9971 /* ARGSUSED */
9972 9972 int
9973 9973 ip_sioctl_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
9974 9974 ip_ioctl_cmd_t *ipip, void *if_req)
9975 9975 {
9976 9976 int err = 0;
9977 9977 in6_addr_t v6addr;
9978 9978 boolean_t need_up = B_FALSE;
9979 9979
9980 9980 ip1dbg(("ip_sioctl_dstaddr(%s:%u %p)\n",
9981 9981 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
9982 9982 ASSERT(IAM_WRITER_IPIF(ipif));
9983 9983
9984 9984 if (ipif->ipif_isv6) {
9985 9985 sin6_t *sin6;
9986 9986
9987 9987 if (sin->sin_family != AF_INET6)
9988 9988 return (EAFNOSUPPORT);
9989 9989
9990 9990 sin6 = (sin6_t *)sin;
9991 9991 v6addr = sin6->sin6_addr;
9992 9992
9993 9993 if (!ip_remote_addr_ok_v6(&v6addr, &ipif->ipif_v6net_mask))
9994 9994 return (EADDRNOTAVAIL);
9995 9995 } else {
9996 9996 ipaddr_t addr;
9997 9997
9998 9998 if (sin->sin_family != AF_INET)
9999 9999 return (EAFNOSUPPORT);
10000 10000
10001 10001 addr = sin->sin_addr.s_addr;
10002 10002 if (addr != INADDR_ANY &&
10003 10003 !ip_addr_ok_v4(addr, ipif->ipif_net_mask)) {
10004 10004 return (EADDRNOTAVAIL);
10005 10005 }
10006 10006
10007 10007 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10008 10008 }
10009 10009
10010 10010 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6pp_dst_addr, &v6addr))
10011 10011 return (0); /* No change */
10012 10012
10013 10013 if (ipif->ipif_flags & IPIF_UP) {
10014 10014 /*
10015 10015 * If the interface is already marked up,
10016 10016 * we call ipif_down which will take care
10017 10017 * of ditching any IREs that have been set
10018 10018 * up based on the old pp dst address.
10019 10019 */
10020 10020 err = ipif_logical_down(ipif, q, mp);
10021 10021 if (err == EINPROGRESS)
10022 10022 return (err);
10023 10023 (void) ipif_down_tail(ipif);
10024 10024 need_up = B_TRUE;
10025 10025 }
10026 10026 /*
10027 10027 * could return EINPROGRESS. If so ioctl will complete in
10028 10028 * ip_rput_dlpi_writer
10029 10029 */
10030 10030 err = ip_sioctl_dstaddr_tail(ipif, sin, q, mp, need_up);
10031 10031 return (err);
10032 10032 }
10033 10033
10034 10034 static int
10035 10035 ip_sioctl_dstaddr_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10036 10036 boolean_t need_up)
10037 10037 {
10038 10038 in6_addr_t v6addr;
10039 10039 ill_t *ill = ipif->ipif_ill;
10040 10040 int err = 0;
10041 10041 boolean_t need_dl_down;
10042 10042 boolean_t need_arp_down;
10043 10043
10044 10044 ip1dbg(("ip_sioctl_dstaddr_tail(%s:%u %p)\n", ill->ill_name,
10045 10045 ipif->ipif_id, (void *)ipif));
10046 10046
10047 10047 /* Must cancel any pending timer before taking the ill_lock */
10048 10048 if (ipif->ipif_recovery_id != 0)
10049 10049 (void) untimeout(ipif->ipif_recovery_id);
10050 10050 ipif->ipif_recovery_id = 0;
10051 10051
10052 10052 if (ipif->ipif_isv6) {
10053 10053 sin6_t *sin6;
10054 10054
10055 10055 sin6 = (sin6_t *)sin;
10056 10056 v6addr = sin6->sin6_addr;
10057 10057 } else {
10058 10058 ipaddr_t addr;
10059 10059
10060 10060 addr = sin->sin_addr.s_addr;
10061 10061 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
10062 10062 }
10063 10063 mutex_enter(&ill->ill_lock);
10064 10064 /* Set point to point destination address. */
10065 10065 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10066 10066 /*
10067 10067 * Allow this as a means of creating logical
10068 10068 * pt-pt interfaces on top of e.g. an Ethernet.
10069 10069 * XXX Undocumented HACK for testing.
10070 10070 * pt-pt interfaces are created with NUD disabled.
10071 10071 */
10072 10072 ipif->ipif_flags |= IPIF_POINTOPOINT;
10073 10073 ipif->ipif_flags &= ~IPIF_BROADCAST;
10074 10074 if (ipif->ipif_isv6)
10075 10075 ill->ill_flags |= ILLF_NONUD;
10076 10076 }
10077 10077
10078 10078 /*
10079 10079 * If the interface was previously marked as a duplicate, then since
10080 10080 * we've now got a "new" address, it should no longer be considered a
10081 10081 * duplicate -- even if the "new" address is the same as the old one.
10082 10082 * Note that if all ipifs are down, we may have a pending ARP down
10083 10083 * event to handle.
10084 10084 */
10085 10085 need_dl_down = need_arp_down = B_FALSE;
10086 10086 if (ipif->ipif_flags & IPIF_DUPLICATE) {
10087 10087 need_arp_down = !need_up;
10088 10088 ipif->ipif_flags &= ~IPIF_DUPLICATE;
10089 10089 if (--ill->ill_ipif_dup_count == 0 && !need_up &&
10090 10090 ill->ill_ipif_up_count == 0 && ill->ill_dl_up) {
10091 10091 need_dl_down = B_TRUE;
10092 10092 }
10093 10093 }
10094 10094
10095 10095 /*
10096 10096 * If we've just manually set the IPv6 destination link-local address
10097 10097 * (0th ipif), tag the ill so that future updates to the destination
10098 10098 * interface ID (as can happen with interfaces over IP tunnels) don't
10099 10099 * result in this address getting automatically reconfigured from
10100 10100 * under the administrator.
10101 10101 */
10102 10102 if (ipif->ipif_isv6 && ipif->ipif_id == 0)
10103 10103 ill->ill_manual_dst_linklocal = 1;
10104 10104
10105 10105 /* Set the new address. */
10106 10106 ipif->ipif_v6pp_dst_addr = v6addr;
10107 10107 /* Make sure subnet tracks pp_dst */
10108 10108 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
10109 10109 mutex_exit(&ill->ill_lock);
10110 10110
10111 10111 if (need_up) {
10112 10112 /*
10113 10113 * Now bring the interface back up. If this
10114 10114 * is the only IPIF for the ILL, ipif_up
10115 10115 * will have to re-bind to the device, so
10116 10116 * we may get back EINPROGRESS, in which
10117 10117 * case, this IOCTL will get completed in
10118 10118 * ip_rput_dlpi when we see the DL_BIND_ACK.
10119 10119 */
10120 10120 err = ipif_up(ipif, q, mp);
10121 10121 }
10122 10122
10123 10123 if (need_dl_down)
10124 10124 ill_dl_down(ill);
10125 10125 if (need_arp_down && !ipif->ipif_isv6)
10126 10126 (void) ipif_arp_down(ipif);
10127 10127
10128 10128 return (err);
10129 10129 }
10130 10130
10131 10131 /*
10132 10132 * Restart entry point to restart the dstaddress set operation after the
10133 10133 * refcounts have dropped to zero.
10134 10134 */
10135 10135 /* ARGSUSED */
10136 10136 int
10137 10137 ip_sioctl_dstaddr_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10138 10138 ip_ioctl_cmd_t *ipip, void *ifreq)
10139 10139 {
10140 10140 ip1dbg(("ip_sioctl_dstaddr_restart(%s:%u %p)\n",
10141 10141 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10142 10142 (void) ipif_down_tail(ipif);
10143 10143 return (ip_sioctl_dstaddr_tail(ipif, sin, q, mp, B_TRUE));
10144 10144 }
10145 10145
10146 10146 /* ARGSUSED */
10147 10147 int
10148 10148 ip_sioctl_get_dstaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10149 10149 ip_ioctl_cmd_t *ipip, void *if_req)
10150 10150 {
10151 10151 sin6_t *sin6 = (struct sockaddr_in6 *)sin;
10152 10152
10153 10153 ip1dbg(("ip_sioctl_get_dstaddr(%s:%u %p)\n",
10154 10154 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10155 10155 /*
10156 10156 * Get point to point destination address. The addresses can't
10157 10157 * change since we hold a reference to the ipif.
10158 10158 */
10159 10159 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0)
10160 10160 return (EADDRNOTAVAIL);
10161 10161
10162 10162 if (ipif->ipif_isv6) {
10163 10163 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
10164 10164 *sin6 = sin6_null;
10165 10165 sin6->sin6_family = AF_INET6;
10166 10166 sin6->sin6_addr = ipif->ipif_v6pp_dst_addr;
10167 10167 } else {
10168 10168 *sin = sin_null;
10169 10169 sin->sin_family = AF_INET;
10170 10170 sin->sin_addr.s_addr = ipif->ipif_pp_dst_addr;
10171 10171 }
10172 10172 return (0);
10173 10173 }
10174 10174
10175 10175 /*
10176 10176 * Check which flags will change by the given flags being set
10177 10177 * silently ignore flags which userland is not allowed to control.
10178 10178 * (Because these flags may change between SIOCGLIFFLAGS and
10179 10179 * SIOCSLIFFLAGS, and that's outside of userland's control,
10180 10180 * we need to silently ignore them rather than fail.)
10181 10181 */
10182 10182 static void
10183 10183 ip_sioctl_flags_onoff(ipif_t *ipif, uint64_t flags, uint64_t *onp,
10184 10184 uint64_t *offp)
10185 10185 {
10186 10186 ill_t *ill = ipif->ipif_ill;
10187 10187 phyint_t *phyi = ill->ill_phyint;
10188 10188 uint64_t cantchange_flags, intf_flags;
10189 10189 uint64_t turn_on, turn_off;
10190 10190
10191 10191 intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10192 10192 cantchange_flags = IFF_CANTCHANGE;
10193 10193 if (IS_IPMP(ill))
10194 10194 cantchange_flags |= IFF_IPMP_CANTCHANGE;
10195 10195 turn_on = (flags ^ intf_flags) & ~cantchange_flags;
10196 10196 turn_off = intf_flags & turn_on;
10197 10197 turn_on ^= turn_off;
10198 10198 *onp = turn_on;
10199 10199 *offp = turn_off;
10200 10200 }
10201 10201
10202 10202 /*
10203 10203 * Set interface flags. Many flags require special handling (e.g.,
10204 10204 * bringing the interface down); see below for details.
10205 10205 *
10206 10206 * NOTE : We really don't enforce that ipif_id zero should be used
10207 10207 * for setting any flags other than IFF_LOGINT_FLAGS. This
10208 10208 * is because applications generally does SICGLIFFLAGS and
10209 10209 * ORs in the new flags (that affects the logical) and does a
10210 10210 * SIOCSLIFFLAGS. Thus, "flags" below could contain bits other
10211 10211 * than IFF_LOGINT_FLAGS. One could check whether "turn_on" - the
10212 10212 * flags that will be turned on is correct with respect to
10213 10213 * ipif_id 0. For backward compatibility reasons, it is not done.
10214 10214 */
10215 10215 /* ARGSUSED */
10216 10216 int
10217 10217 ip_sioctl_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10218 10218 ip_ioctl_cmd_t *ipip, void *if_req)
10219 10219 {
10220 10220 uint64_t turn_on;
10221 10221 uint64_t turn_off;
10222 10222 int err = 0;
10223 10223 phyint_t *phyi;
10224 10224 ill_t *ill;
10225 10225 conn_t *connp;
10226 10226 uint64_t intf_flags;
10227 10227 boolean_t phyint_flags_modified = B_FALSE;
10228 10228 uint64_t flags;
10229 10229 struct ifreq *ifr;
10230 10230 struct lifreq *lifr;
10231 10231 boolean_t set_linklocal = B_FALSE;
10232 10232
10233 10233 ip1dbg(("ip_sioctl_flags(%s:%u %p)\n",
10234 10234 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10235 10235
10236 10236 ASSERT(IAM_WRITER_IPIF(ipif));
10237 10237
10238 10238 ill = ipif->ipif_ill;
10239 10239 phyi = ill->ill_phyint;
10240 10240
10241 10241 if (ipip->ipi_cmd_type == IF_CMD) {
10242 10242 ifr = (struct ifreq *)if_req;
10243 10243 flags = (uint64_t)(ifr->ifr_flags & 0x0000ffff);
10244 10244 } else {
10245 10245 lifr = (struct lifreq *)if_req;
10246 10246 flags = lifr->lifr_flags;
10247 10247 }
10248 10248
10249 10249 intf_flags = ipif->ipif_flags | ill->ill_flags | phyi->phyint_flags;
10250 10250
10251 10251 /*
10252 10252 * Have the flags been set correctly until now?
10253 10253 */
10254 10254 ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10255 10255 ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10256 10256 ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10257 10257 /*
10258 10258 * Compare the new flags to the old, and partition
10259 10259 * into those coming on and those going off.
10260 10260 * For the 16 bit command keep the bits above bit 16 unchanged.
10261 10261 */
10262 10262 if (ipip->ipi_cmd == SIOCSIFFLAGS)
10263 10263 flags |= intf_flags & ~0xFFFF;
10264 10264
10265 10265 /*
10266 10266 * Explicitly fail attempts to change flags that are always invalid on
10267 10267 * an IPMP meta-interface.
10268 10268 */
10269 10269 if (IS_IPMP(ill) && ((flags ^ intf_flags) & IFF_IPMP_INVALID))
10270 10270 return (EINVAL);
10271 10271
10272 10272 ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10273 10273 if ((turn_on|turn_off) == 0)
10274 10274 return (0); /* No change */
10275 10275
10276 10276 /*
10277 10277 * All test addresses must be IFF_DEPRECATED (to ensure source address
10278 10278 * selection avoids them) -- so force IFF_DEPRECATED on, and do not
10279 10279 * allow it to be turned off.
10280 10280 */
10281 10281 if ((turn_off & (IFF_DEPRECATED|IFF_NOFAILOVER)) == IFF_DEPRECATED &&
10282 10282 (turn_on|intf_flags) & IFF_NOFAILOVER)
10283 10283 return (EINVAL);
10284 10284
10285 10285 if ((connp = Q_TO_CONN(q)) == NULL)
10286 10286 return (EINVAL);
10287 10287
10288 10288 /*
10289 10289 * Only vrrp control socket is allowed to change IFF_UP and
10290 10290 * IFF_NOACCEPT flags when IFF_VRRP is set.
10291 10291 */
10292 10292 if ((intf_flags & IFF_VRRP) && ((turn_off | turn_on) & IFF_UP)) {
10293 10293 if (!connp->conn_isvrrp)
10294 10294 return (EINVAL);
10295 10295 }
10296 10296
10297 10297 /*
10298 10298 * The IFF_NOACCEPT flag can only be set on an IFF_VRRP IP address by
10299 10299 * VRRP control socket.
10300 10300 */
10301 10301 if ((turn_off | turn_on) & IFF_NOACCEPT) {
10302 10302 if (!connp->conn_isvrrp || !(intf_flags & IFF_VRRP))
10303 10303 return (EINVAL);
10304 10304 }
10305 10305
10306 10306 if (turn_on & IFF_NOFAILOVER) {
10307 10307 turn_on |= IFF_DEPRECATED;
10308 10308 flags |= IFF_DEPRECATED;
10309 10309 }
10310 10310
10311 10311 /*
10312 10312 * On underlying interfaces, only allow applications to manage test
10313 10313 * addresses -- otherwise, they may get confused when the address
10314 10314 * moves as part of being brought up. Likewise, prevent an
10315 10315 * application-managed test address from being converted to a data
10316 10316 * address. To prevent migration of administratively up addresses in
10317 10317 * the kernel, we don't allow them to be converted either.
10318 10318 */
10319 10319 if (IS_UNDER_IPMP(ill)) {
10320 10320 const uint64_t appflags = IFF_DHCPRUNNING | IFF_ADDRCONF;
10321 10321
10322 10322 if ((turn_on & appflags) && !(flags & IFF_NOFAILOVER))
10323 10323 return (EINVAL);
10324 10324
10325 10325 if ((turn_off & IFF_NOFAILOVER) &&
10326 10326 (flags & (appflags | IFF_UP | IFF_DUPLICATE)))
10327 10327 return (EINVAL);
10328 10328 }
10329 10329
10330 10330 /*
10331 10331 * Only allow IFF_TEMPORARY flag to be set on
10332 10332 * IPv6 interfaces.
10333 10333 */
10334 10334 if ((turn_on & IFF_TEMPORARY) && !(ipif->ipif_isv6))
10335 10335 return (EINVAL);
10336 10336
10337 10337 /*
10338 10338 * cannot turn off IFF_NOXMIT on VNI interfaces.
10339 10339 */
10340 10340 if ((turn_off & IFF_NOXMIT) && IS_VNI(ipif->ipif_ill))
10341 10341 return (EINVAL);
10342 10342
10343 10343 /*
10344 10344 * Don't allow the IFF_ROUTER flag to be turned on on loopback
10345 10345 * interfaces. It makes no sense in that context.
10346 10346 */
10347 10347 if ((turn_on & IFF_ROUTER) && (phyi->phyint_flags & PHYI_LOOPBACK))
10348 10348 return (EINVAL);
10349 10349
10350 10350 /*
10351 10351 * For IPv6 ipif_id 0, don't allow the interface to be up without
10352 10352 * a link local address if IFF_NOLOCAL or IFF_ANYCAST are not set.
10353 10353 * If the link local address isn't set, and can be set, it will get
10354 10354 * set later on in this function.
10355 10355 */
10356 10356 if (ipif->ipif_id == 0 && ipif->ipif_isv6 &&
10357 10357 (flags & IFF_UP) && !(flags & (IFF_NOLOCAL|IFF_ANYCAST)) &&
10358 10358 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr)) {
10359 10359 if (ipif_cant_setlinklocal(ipif))
10360 10360 return (EINVAL);
10361 10361 set_linklocal = B_TRUE;
10362 10362 }
10363 10363
10364 10364 /*
10365 10365 * If we modify physical interface flags, we'll potentially need to
10366 10366 * send up two routing socket messages for the changes (one for the
10367 10367 * IPv4 ill, and another for the IPv6 ill). Note that here.
10368 10368 */
10369 10369 if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10370 10370 phyint_flags_modified = B_TRUE;
10371 10371
10372 10372 /*
10373 10373 * All functioning PHYI_STANDBY interfaces start life PHYI_INACTIVE
10374 10374 * (otherwise, we'd immediately use them, defeating standby). Also,
10375 10375 * since PHYI_INACTIVE has a separate meaning when PHYI_STANDBY is not
10376 10376 * set, don't allow PHYI_STANDBY to be set if PHYI_INACTIVE is already
10377 10377 * set, and clear PHYI_INACTIVE if PHYI_STANDBY is being cleared. We
10378 10378 * also don't allow PHYI_STANDBY if VNI is enabled since its semantics
10379 10379 * will not be honored.
10380 10380 */
10381 10381 if (turn_on & PHYI_STANDBY) {
10382 10382 /*
10383 10383 * No need to grab ill_g_usesrc_lock here; see the
10384 10384 * synchronization notes in ip.c.
10385 10385 */
10386 10386 if (ill->ill_usesrc_grp_next != NULL ||
10387 10387 intf_flags & PHYI_INACTIVE)
10388 10388 return (EINVAL);
10389 10389 if (!(flags & PHYI_FAILED)) {
10390 10390 flags |= PHYI_INACTIVE;
10391 10391 turn_on |= PHYI_INACTIVE;
10392 10392 }
10393 10393 }
10394 10394
10395 10395 if (turn_off & PHYI_STANDBY) {
10396 10396 flags &= ~PHYI_INACTIVE;
10397 10397 turn_off |= PHYI_INACTIVE;
10398 10398 }
10399 10399
10400 10400 /*
10401 10401 * PHYI_FAILED and PHYI_INACTIVE are mutually exclusive; fail if both
10402 10402 * would end up on.
10403 10403 */
10404 10404 if ((flags & (PHYI_FAILED | PHYI_INACTIVE)) ==
10405 10405 (PHYI_FAILED | PHYI_INACTIVE))
10406 10406 return (EINVAL);
10407 10407
10408 10408 /*
10409 10409 * If ILLF_ROUTER changes, we need to change the ip forwarding
10410 10410 * status of the interface.
10411 10411 */
10412 10412 if ((turn_on | turn_off) & ILLF_ROUTER) {
10413 10413 err = ill_forward_set(ill, ((turn_on & ILLF_ROUTER) != 0));
10414 10414 if (err != 0)
10415 10415 return (err);
10416 10416 }
10417 10417
10418 10418 /*
10419 10419 * If the interface is not UP and we are not going to
10420 10420 * bring it UP, record the flags and return. When the
10421 10421 * interface comes UP later, the right actions will be
10422 10422 * taken.
10423 10423 */
10424 10424 if (!(ipif->ipif_flags & IPIF_UP) &&
10425 10425 !(turn_on & IPIF_UP)) {
10426 10426 /* Record new flags in their respective places. */
10427 10427 mutex_enter(&ill->ill_lock);
10428 10428 mutex_enter(&ill->ill_phyint->phyint_lock);
10429 10429 ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10430 10430 ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10431 10431 ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10432 10432 ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10433 10433 phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10434 10434 phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10435 10435 mutex_exit(&ill->ill_lock);
10436 10436 mutex_exit(&ill->ill_phyint->phyint_lock);
10437 10437
10438 10438 /*
10439 10439 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the
10440 10440 * same to the kernel: if any of them has been set by
10441 10441 * userland, the interface cannot be used for data traffic.
10442 10442 */
10443 10443 if ((turn_on|turn_off) &
10444 10444 (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10445 10445 ASSERT(!IS_IPMP(ill));
10446 10446 /*
10447 10447 * It's possible the ill is part of an "anonymous"
10448 10448 * IPMP group rather than a real group. In that case,
10449 10449 * there are no other interfaces in the group and thus
10450 10450 * no need to call ipmp_phyint_refresh_active().
10451 10451 */
10452 10452 if (IS_UNDER_IPMP(ill))
10453 10453 ipmp_phyint_refresh_active(phyi);
10454 10454 }
10455 10455
10456 10456 if (phyint_flags_modified) {
10457 10457 if (phyi->phyint_illv4 != NULL) {
10458 10458 ip_rts_ifmsg(phyi->phyint_illv4->
10459 10459 ill_ipif, RTSQ_DEFAULT);
10460 10460 }
10461 10461 if (phyi->phyint_illv6 != NULL) {
10462 10462 ip_rts_ifmsg(phyi->phyint_illv6->
10463 10463 ill_ipif, RTSQ_DEFAULT);
10464 10464 }
10465 10465 }
10466 10466 /* The default multicast interface might have changed */
10467 10467 ire_increment_multicast_generation(ill->ill_ipst,
10468 10468 ill->ill_isv6);
10469 10469
10470 10470 return (0);
10471 10471 } else if (set_linklocal) {
10472 10472 mutex_enter(&ill->ill_lock);
10473 10473 if (set_linklocal)
10474 10474 ipif->ipif_state_flags |= IPIF_SET_LINKLOCAL;
10475 10475 mutex_exit(&ill->ill_lock);
10476 10476 }
10477 10477
10478 10478 /*
10479 10479 * Disallow IPv6 interfaces coming up that have the unspecified address,
10480 10480 * or point-to-point interfaces with an unspecified destination. We do
10481 10481 * allow the address to be unspecified for IPIF_NOLOCAL interfaces that
10482 10482 * have a subnet assigned, which is how in.ndpd currently manages its
10483 10483 * onlink prefix list when no addresses are configured with those
10484 10484 * prefixes.
10485 10485 */
10486 10486 if (ipif->ipif_isv6 &&
10487 10487 ((IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
10488 10488 (!(ipif->ipif_flags & IPIF_NOLOCAL) && !(turn_on & IPIF_NOLOCAL) ||
10489 10489 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6subnet))) ||
10490 10490 ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10491 10491 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6pp_dst_addr)))) {
10492 10492 return (EINVAL);
10493 10493 }
10494 10494
10495 10495 /*
10496 10496 * Prevent IPv4 point-to-point interfaces with a 0.0.0.0 destination
10497 10497 * from being brought up.
10498 10498 */
10499 10499 if (!ipif->ipif_isv6 &&
10500 10500 ((ipif->ipif_flags & IPIF_POINTOPOINT) &&
10501 10501 ipif->ipif_pp_dst_addr == INADDR_ANY)) {
10502 10502 return (EINVAL);
10503 10503 }
10504 10504
10505 10505 /*
10506 10506 * If we are going to change one or more of the flags that are
10507 10507 * IPIF_UP, IPIF_DEPRECATED, IPIF_NOXMIT, IPIF_NOLOCAL, ILLF_NOARP,
10508 10508 * ILLF_NONUD, IPIF_PRIVATE, IPIF_ANYCAST, IPIF_PREFERRED, and
10509 10509 * IPIF_NOFAILOVER, we will take special action. This is
10510 10510 * done by bring the ipif down, changing the flags and bringing
10511 10511 * it back up again. For IPIF_NOFAILOVER, the act of bringing it
10512 10512 * back up will trigger the address to be moved.
10513 10513 *
10514 10514 * If we are going to change IFF_NOACCEPT, we need to bring
10515 10515 * all the ipifs down then bring them up again. The act of
10516 10516 * bringing all the ipifs back up will trigger the local
10517 10517 * ires being recreated with "no_accept" set/cleared.
10518 10518 *
10519 10519 * Note that ILLF_NOACCEPT is always set separately from the
10520 10520 * other flags.
10521 10521 */
10522 10522 if ((turn_on|turn_off) &
10523 10523 (IPIF_UP|IPIF_DEPRECATED|IPIF_NOXMIT|IPIF_NOLOCAL|ILLF_NOARP|
10524 10524 ILLF_NONUD|IPIF_PRIVATE|IPIF_ANYCAST|IPIF_PREFERRED|
10525 10525 IPIF_NOFAILOVER)) {
10526 10526 /*
10527 10527 * ipif_down() will ire_delete bcast ire's for the subnet,
10528 10528 * while the ire_identical_ref tracks the case of IRE_BROADCAST
10529 10529 * entries shared between multiple ipifs on the same subnet.
10530 10530 */
10531 10531 if (((ipif->ipif_flags | turn_on) & IPIF_UP) &&
10532 10532 !(turn_off & IPIF_UP)) {
10533 10533 if (ipif->ipif_flags & IPIF_UP)
10534 10534 ill->ill_logical_down = 1;
10535 10535 turn_on &= ~IPIF_UP;
10536 10536 }
10537 10537 err = ipif_down(ipif, q, mp);
10538 10538 ip1dbg(("ipif_down returns %d err ", err));
10539 10539 if (err == EINPROGRESS)
10540 10540 return (err);
10541 10541 (void) ipif_down_tail(ipif);
10542 10542 } else if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10543 10543 /*
10544 10544 * If we can quiesce the ill, then continue. If not, then
10545 10545 * ip_sioctl_flags_tail() will be called from
10546 10546 * ipif_ill_refrele_tail().
10547 10547 */
10548 10548 ill_down_ipifs(ill, B_TRUE);
10549 10549
10550 10550 mutex_enter(&connp->conn_lock);
10551 10551 mutex_enter(&ill->ill_lock);
10552 10552 if (!ill_is_quiescent(ill)) {
10553 10553 boolean_t success;
10554 10554
10555 10555 success = ipsq_pending_mp_add(connp, ill->ill_ipif,
10556 10556 q, mp, ILL_DOWN);
10557 10557 mutex_exit(&ill->ill_lock);
10558 10558 mutex_exit(&connp->conn_lock);
10559 10559 return (success ? EINPROGRESS : EINTR);
10560 10560 }
10561 10561 mutex_exit(&ill->ill_lock);
10562 10562 mutex_exit(&connp->conn_lock);
10563 10563 }
10564 10564 return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10565 10565 }
10566 10566
10567 10567 static int
10568 10568 ip_sioctl_flags_tail(ipif_t *ipif, uint64_t flags, queue_t *q, mblk_t *mp)
10569 10569 {
10570 10570 ill_t *ill;
10571 10571 phyint_t *phyi;
10572 10572 uint64_t turn_on, turn_off;
10573 10573 boolean_t phyint_flags_modified = B_FALSE;
10574 10574 int err = 0;
10575 10575 boolean_t set_linklocal = B_FALSE;
10576 10576
10577 10577 ip1dbg(("ip_sioctl_flags_tail(%s:%u)\n",
10578 10578 ipif->ipif_ill->ill_name, ipif->ipif_id));
10579 10579
10580 10580 ASSERT(IAM_WRITER_IPIF(ipif));
10581 10581
10582 10582 ill = ipif->ipif_ill;
10583 10583 phyi = ill->ill_phyint;
10584 10584
10585 10585 ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10586 10586
10587 10587 /*
10588 10588 * IFF_UP is handled separately.
10589 10589 */
10590 10590 turn_on &= ~IFF_UP;
10591 10591 turn_off &= ~IFF_UP;
10592 10592
10593 10593 if ((turn_on|turn_off) & IFF_PHYINT_FLAGS)
10594 10594 phyint_flags_modified = B_TRUE;
10595 10595
10596 10596 /*
10597 10597 * Now we change the flags. Track current value of
10598 10598 * other flags in their respective places.
10599 10599 */
10600 10600 mutex_enter(&ill->ill_lock);
10601 10601 mutex_enter(&phyi->phyint_lock);
10602 10602 ipif->ipif_flags |= (turn_on & IFF_LOGINT_FLAGS);
10603 10603 ipif->ipif_flags &= (~turn_off & IFF_LOGINT_FLAGS);
10604 10604 ill->ill_flags |= (turn_on & IFF_PHYINTINST_FLAGS);
10605 10605 ill->ill_flags &= (~turn_off & IFF_PHYINTINST_FLAGS);
10606 10606 phyi->phyint_flags |= (turn_on & IFF_PHYINT_FLAGS);
10607 10607 phyi->phyint_flags &= (~turn_off & IFF_PHYINT_FLAGS);
10608 10608 if (ipif->ipif_state_flags & IPIF_SET_LINKLOCAL) {
10609 10609 set_linklocal = B_TRUE;
10610 10610 ipif->ipif_state_flags &= ~IPIF_SET_LINKLOCAL;
10611 10611 }
10612 10612
10613 10613 mutex_exit(&ill->ill_lock);
10614 10614 mutex_exit(&phyi->phyint_lock);
10615 10615
10616 10616 if (set_linklocal)
10617 10617 (void) ipif_setlinklocal(ipif);
10618 10618
10619 10619 /*
10620 10620 * PHYI_FAILED, PHYI_INACTIVE, and PHYI_OFFLINE are all the same to
10621 10621 * the kernel: if any of them has been set by userland, the interface
10622 10622 * cannot be used for data traffic.
10623 10623 */
10624 10624 if ((turn_on|turn_off) & (PHYI_FAILED | PHYI_INACTIVE | PHYI_OFFLINE)) {
10625 10625 ASSERT(!IS_IPMP(ill));
10626 10626 /*
10627 10627 * It's possible the ill is part of an "anonymous" IPMP group
10628 10628 * rather than a real group. In that case, there are no other
10629 10629 * interfaces in the group and thus no need for us to call
10630 10630 * ipmp_phyint_refresh_active().
10631 10631 */
10632 10632 if (IS_UNDER_IPMP(ill))
10633 10633 ipmp_phyint_refresh_active(phyi);
10634 10634 }
10635 10635
10636 10636 if ((turn_on|turn_off) & ILLF_NOACCEPT) {
10637 10637 /*
10638 10638 * If the ILLF_NOACCEPT flag is changed, bring up all the
10639 10639 * ipifs that were brought down.
10640 10640 *
10641 10641 * The routing sockets messages are sent as the result
10642 10642 * of ill_up_ipifs(), further, SCTP's IPIF list was updated
10643 10643 * as well.
10644 10644 */
10645 10645 err = ill_up_ipifs(ill, q, mp);
10646 10646 } else if ((flags & IFF_UP) && !(ipif->ipif_flags & IPIF_UP)) {
10647 10647 /*
10648 10648 * XXX ipif_up really does not know whether a phyint flags
10649 10649 * was modified or not. So, it sends up information on
10650 10650 * only one routing sockets message. As we don't bring up
10651 10651 * the interface and also set PHYI_ flags simultaneously
10652 10652 * it should be okay.
10653 10653 */
10654 10654 err = ipif_up(ipif, q, mp);
10655 10655 } else {
10656 10656 /*
10657 10657 * Make sure routing socket sees all changes to the flags.
10658 10658 * ipif_up_done* handles this when we use ipif_up.
10659 10659 */
10660 10660 if (phyint_flags_modified) {
10661 10661 if (phyi->phyint_illv4 != NULL) {
10662 10662 ip_rts_ifmsg(phyi->phyint_illv4->
10663 10663 ill_ipif, RTSQ_DEFAULT);
10664 10664 }
10665 10665 if (phyi->phyint_illv6 != NULL) {
10666 10666 ip_rts_ifmsg(phyi->phyint_illv6->
10667 10667 ill_ipif, RTSQ_DEFAULT);
10668 10668 }
10669 10669 } else {
10670 10670 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
10671 10671 }
10672 10672 /*
10673 10673 * Update the flags in SCTP's IPIF list, ipif_up() will do
10674 10674 * this in need_up case.
10675 10675 */
10676 10676 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10677 10677 }
10678 10678
10679 10679 /* The default multicast interface might have changed */
10680 10680 ire_increment_multicast_generation(ill->ill_ipst, ill->ill_isv6);
10681 10681 return (err);
10682 10682 }
10683 10683
10684 10684 /*
10685 10685 * Restart the flags operation now that the refcounts have dropped to zero.
10686 10686 */
10687 10687 /* ARGSUSED */
10688 10688 int
10689 10689 ip_sioctl_flags_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10690 10690 ip_ioctl_cmd_t *ipip, void *if_req)
10691 10691 {
10692 10692 uint64_t flags;
10693 10693 struct ifreq *ifr = if_req;
10694 10694 struct lifreq *lifr = if_req;
10695 10695 uint64_t turn_on, turn_off;
10696 10696
10697 10697 ip1dbg(("ip_sioctl_flags_restart(%s:%u %p)\n",
10698 10698 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10699 10699
10700 10700 if (ipip->ipi_cmd_type == IF_CMD) {
10701 10701 /* cast to uint16_t prevents unwanted sign extension */
10702 10702 flags = (uint16_t)ifr->ifr_flags;
10703 10703 } else {
10704 10704 flags = lifr->lifr_flags;
10705 10705 }
10706 10706
10707 10707 /*
10708 10708 * If this function call is a result of the ILLF_NOACCEPT flag
10709 10709 * change, do not call ipif_down_tail(). See ip_sioctl_flags().
10710 10710 */
10711 10711 ip_sioctl_flags_onoff(ipif, flags, &turn_on, &turn_off);
10712 10712 if (!((turn_on|turn_off) & ILLF_NOACCEPT))
10713 10713 (void) ipif_down_tail(ipif);
10714 10714
10715 10715 return (ip_sioctl_flags_tail(ipif, flags, q, mp));
10716 10716 }
10717 10717
10718 10718 /*
10719 10719 * Can operate on either a module or a driver queue.
10720 10720 */
10721 10721 /* ARGSUSED */
10722 10722 int
10723 10723 ip_sioctl_get_flags(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10724 10724 ip_ioctl_cmd_t *ipip, void *if_req)
10725 10725 {
10726 10726 /*
10727 10727 * Has the flags been set correctly till now ?
10728 10728 */
10729 10729 ill_t *ill = ipif->ipif_ill;
10730 10730 phyint_t *phyi = ill->ill_phyint;
10731 10731
10732 10732 ip1dbg(("ip_sioctl_get_flags(%s:%u %p)\n",
10733 10733 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10734 10734 ASSERT((phyi->phyint_flags & ~(IFF_PHYINT_FLAGS)) == 0);
10735 10735 ASSERT((ill->ill_flags & ~(IFF_PHYINTINST_FLAGS)) == 0);
10736 10736 ASSERT((ipif->ipif_flags & ~(IFF_LOGINT_FLAGS)) == 0);
10737 10737
10738 10738 /*
10739 10739 * Need a lock since some flags can be set even when there are
10740 10740 * references to the ipif.
10741 10741 */
10742 10742 mutex_enter(&ill->ill_lock);
10743 10743 if (ipip->ipi_cmd_type == IF_CMD) {
10744 10744 struct ifreq *ifr = (struct ifreq *)if_req;
10745 10745
10746 10746 /* Get interface flags (low 16 only). */
10747 10747 ifr->ifr_flags = ((ipif->ipif_flags |
10748 10748 ill->ill_flags | phyi->phyint_flags) & 0xffff);
10749 10749 } else {
10750 10750 struct lifreq *lifr = (struct lifreq *)if_req;
10751 10751
10752 10752 /* Get interface flags. */
10753 10753 lifr->lifr_flags = ipif->ipif_flags |
10754 10754 ill->ill_flags | phyi->phyint_flags;
10755 10755 }
10756 10756 mutex_exit(&ill->ill_lock);
10757 10757 return (0);
10758 10758 }
10759 10759
10760 10760 /*
10761 10761 * We allow the MTU to be set on an ILL, but not have it be different
10762 10762 * for different IPIFs since we don't actually send packets on IPIFs.
10763 10763 */
10764 10764 /* ARGSUSED */
10765 10765 int
10766 10766 ip_sioctl_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10767 10767 ip_ioctl_cmd_t *ipip, void *if_req)
10768 10768 {
10769 10769 int mtu;
10770 10770 int ip_min_mtu;
10771 10771 struct ifreq *ifr;
10772 10772 struct lifreq *lifr;
10773 10773 ill_t *ill;
10774 10774
10775 10775 ip1dbg(("ip_sioctl_mtu(%s:%u %p)\n", ipif->ipif_ill->ill_name,
10776 10776 ipif->ipif_id, (void *)ipif));
10777 10777 if (ipip->ipi_cmd_type == IF_CMD) {
10778 10778 ifr = (struct ifreq *)if_req;
10779 10779 mtu = ifr->ifr_metric;
10780 10780 } else {
10781 10781 lifr = (struct lifreq *)if_req;
10782 10782 mtu = lifr->lifr_mtu;
10783 10783 }
10784 10784 /* Only allow for logical unit zero i.e. not on "bge0:17" */
10785 10785 if (ipif->ipif_id != 0)
10786 10786 return (EINVAL);
10787 10787
10788 10788 ill = ipif->ipif_ill;
10789 10789 if (ipif->ipif_isv6)
10790 10790 ip_min_mtu = IPV6_MIN_MTU;
10791 10791 else
10792 10792 ip_min_mtu = IP_MIN_MTU;
10793 10793
10794 10794 mutex_enter(&ill->ill_lock);
10795 10795 if (mtu > ill->ill_max_frag || mtu < ip_min_mtu) {
10796 10796 mutex_exit(&ill->ill_lock);
10797 10797 return (EINVAL);
10798 10798 }
10799 10799 /* Avoid increasing ill_mc_mtu */
10800 10800 if (ill->ill_mc_mtu > mtu)
10801 10801 ill->ill_mc_mtu = mtu;
10802 10802
10803 10803 /*
10804 10804 * The dce and fragmentation code can handle changes to ill_mtu
10805 10805 * concurrent with sending/fragmenting packets.
10806 10806 */
10807 10807 ill->ill_mtu = mtu;
10808 10808 ill->ill_flags |= ILLF_FIXEDMTU;
10809 10809 mutex_exit(&ill->ill_lock);
10810 10810
10811 10811 /*
10812 10812 * Make sure all dce_generation checks find out
10813 10813 * that ill_mtu/ill_mc_mtu has changed.
10814 10814 */
10815 10815 dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
10816 10816
10817 10817 /*
10818 10818 * Refresh IPMP meta-interface MTU if necessary.
10819 10819 */
10820 10820 if (IS_UNDER_IPMP(ill))
10821 10821 ipmp_illgrp_refresh_mtu(ill->ill_grp);
10822 10822
10823 10823 /* Update the MTU in SCTP's list */
10824 10824 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
10825 10825 return (0);
10826 10826 }
10827 10827
10828 10828 /* Get interface MTU. */
10829 10829 /* ARGSUSED */
10830 10830 int
10831 10831 ip_sioctl_get_mtu(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10832 10832 ip_ioctl_cmd_t *ipip, void *if_req)
10833 10833 {
10834 10834 struct ifreq *ifr;
10835 10835 struct lifreq *lifr;
10836 10836
10837 10837 ip1dbg(("ip_sioctl_get_mtu(%s:%u %p)\n",
10838 10838 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10839 10839
10840 10840 /*
10841 10841 * We allow a get on any logical interface even though the set
10842 10842 * can only be done on logical unit 0.
10843 10843 */
10844 10844 if (ipip->ipi_cmd_type == IF_CMD) {
10845 10845 ifr = (struct ifreq *)if_req;
10846 10846 ifr->ifr_metric = ipif->ipif_ill->ill_mtu;
10847 10847 } else {
10848 10848 lifr = (struct lifreq *)if_req;
10849 10849 lifr->lifr_mtu = ipif->ipif_ill->ill_mtu;
10850 10850 }
10851 10851 return (0);
10852 10852 }
10853 10853
10854 10854 /* Set interface broadcast address. */
10855 10855 /* ARGSUSED2 */
10856 10856 int
10857 10857 ip_sioctl_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10858 10858 ip_ioctl_cmd_t *ipip, void *if_req)
10859 10859 {
10860 10860 ipaddr_t addr;
10861 10861 ire_t *ire;
10862 10862 ill_t *ill = ipif->ipif_ill;
10863 10863 ip_stack_t *ipst = ill->ill_ipst;
10864 10864
10865 10865 ip1dbg(("ip_sioctl_brdaddr(%s:%u)\n", ill->ill_name,
10866 10866 ipif->ipif_id));
10867 10867
10868 10868 ASSERT(IAM_WRITER_IPIF(ipif));
10869 10869 if (!(ipif->ipif_flags & IPIF_BROADCAST))
10870 10870 return (EADDRNOTAVAIL);
10871 10871
10872 10872 ASSERT(!(ipif->ipif_isv6)); /* No IPv6 broadcast */
10873 10873
10874 10874 if (sin->sin_family != AF_INET)
10875 10875 return (EAFNOSUPPORT);
10876 10876
10877 10877 addr = sin->sin_addr.s_addr;
10878 10878
10879 10879 if (ipif->ipif_flags & IPIF_UP) {
10880 10880 /*
10881 10881 * If we are already up, make sure the new
10882 10882 * broadcast address makes sense. If it does,
10883 10883 * there should be an IRE for it already.
10884 10884 */
10885 10885 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_BROADCAST,
10886 10886 ill, ipif->ipif_zoneid, NULL,
10887 10887 (MATCH_IRE_ILL | MATCH_IRE_TYPE), 0, ipst, NULL);
10888 10888 if (ire == NULL) {
10889 10889 return (EINVAL);
10890 10890 } else {
10891 10891 ire_refrele(ire);
10892 10892 }
10893 10893 }
10894 10894 /*
10895 10895 * Changing the broadcast addr for this ipif. Since the IRE_BROADCAST
10896 10896 * needs to already exist we never need to change the set of
10897 10897 * IRE_BROADCASTs when we are UP.
10898 10898 */
10899 10899 if (addr != ipif->ipif_brd_addr)
10900 10900 IN6_IPADDR_TO_V4MAPPED(addr, &ipif->ipif_v6brd_addr);
10901 10901
10902 10902 return (0);
10903 10903 }
10904 10904
10905 10905 /* Get interface broadcast address. */
10906 10906 /* ARGSUSED */
10907 10907 int
10908 10908 ip_sioctl_get_brdaddr(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10909 10909 ip_ioctl_cmd_t *ipip, void *if_req)
10910 10910 {
10911 10911 ip1dbg(("ip_sioctl_get_brdaddr(%s:%u %p)\n",
10912 10912 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10913 10913 if (!(ipif->ipif_flags & IPIF_BROADCAST))
10914 10914 return (EADDRNOTAVAIL);
10915 10915
10916 10916 /* IPIF_BROADCAST not possible with IPv6 */
10917 10917 ASSERT(!ipif->ipif_isv6);
10918 10918 *sin = sin_null;
10919 10919 sin->sin_family = AF_INET;
10920 10920 sin->sin_addr.s_addr = ipif->ipif_brd_addr;
10921 10921 return (0);
10922 10922 }
10923 10923
10924 10924 /*
10925 10925 * This routine is called to handle the SIOCS*IFNETMASK IOCTL.
10926 10926 */
10927 10927 /* ARGSUSED */
10928 10928 int
10929 10929 ip_sioctl_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
10930 10930 ip_ioctl_cmd_t *ipip, void *if_req)
10931 10931 {
10932 10932 int err = 0;
10933 10933 in6_addr_t v6mask;
10934 10934
10935 10935 ip1dbg(("ip_sioctl_netmask(%s:%u %p)\n",
10936 10936 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10937 10937
10938 10938 ASSERT(IAM_WRITER_IPIF(ipif));
10939 10939
10940 10940 if (ipif->ipif_isv6) {
10941 10941 sin6_t *sin6;
10942 10942
10943 10943 if (sin->sin_family != AF_INET6)
10944 10944 return (EAFNOSUPPORT);
10945 10945
10946 10946 sin6 = (sin6_t *)sin;
10947 10947 v6mask = sin6->sin6_addr;
10948 10948 } else {
10949 10949 ipaddr_t mask;
10950 10950
10951 10951 if (sin->sin_family != AF_INET)
10952 10952 return (EAFNOSUPPORT);
10953 10953
10954 10954 mask = sin->sin_addr.s_addr;
10955 10955 if (!ip_contiguous_mask(ntohl(mask)))
10956 10956 return (ENOTSUP);
10957 10957 V4MASK_TO_V6(mask, v6mask);
10958 10958 }
10959 10959
10960 10960 /*
10961 10961 * No big deal if the interface isn't already up, or the mask
10962 10962 * isn't really changing, or this is pt-pt.
10963 10963 */
10964 10964 if (!(ipif->ipif_flags & IPIF_UP) ||
10965 10965 IN6_ARE_ADDR_EQUAL(&v6mask, &ipif->ipif_v6net_mask) ||
10966 10966 (ipif->ipif_flags & IPIF_POINTOPOINT)) {
10967 10967 ipif->ipif_v6net_mask = v6mask;
10968 10968 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
10969 10969 V6_MASK_COPY(ipif->ipif_v6lcl_addr,
10970 10970 ipif->ipif_v6net_mask,
10971 10971 ipif->ipif_v6subnet);
10972 10972 }
10973 10973 return (0);
10974 10974 }
10975 10975 /*
10976 10976 * Make sure we have valid net and subnet broadcast ire's
10977 10977 * for the old netmask, if needed by other logical interfaces.
10978 10978 */
10979 10979 err = ipif_logical_down(ipif, q, mp);
10980 10980 if (err == EINPROGRESS)
10981 10981 return (err);
10982 10982 (void) ipif_down_tail(ipif);
10983 10983 err = ip_sioctl_netmask_tail(ipif, sin, q, mp);
10984 10984 return (err);
10985 10985 }
10986 10986
10987 10987 static int
10988 10988 ip_sioctl_netmask_tail(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp)
10989 10989 {
10990 10990 in6_addr_t v6mask;
10991 10991 int err = 0;
10992 10992
10993 10993 ip1dbg(("ip_sioctl_netmask_tail(%s:%u %p)\n",
10994 10994 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
10995 10995
10996 10996 if (ipif->ipif_isv6) {
10997 10997 sin6_t *sin6;
10998 10998
10999 10999 sin6 = (sin6_t *)sin;
11000 11000 v6mask = sin6->sin6_addr;
11001 11001 } else {
11002 11002 ipaddr_t mask;
11003 11003
11004 11004 mask = sin->sin_addr.s_addr;
11005 11005 V4MASK_TO_V6(mask, v6mask);
11006 11006 }
11007 11007
11008 11008 ipif->ipif_v6net_mask = v6mask;
11009 11009 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11010 11010 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
11011 11011 ipif->ipif_v6subnet);
11012 11012 }
11013 11013 err = ipif_up(ipif, q, mp);
11014 11014
11015 11015 if (err == 0 || err == EINPROGRESS) {
11016 11016 /*
11017 11017 * The interface must be DL_BOUND if this packet has to
11018 11018 * go out on the wire. Since we only go through a logical
11019 11019 * down and are bound with the driver during an internal
11020 11020 * down/up that is satisfied.
11021 11021 */
11022 11022 if (!ipif->ipif_isv6 && ipif->ipif_ill->ill_wq != NULL) {
11023 11023 /* Potentially broadcast an address mask reply. */
11024 11024 ipif_mask_reply(ipif);
11025 11025 }
11026 11026 }
11027 11027 return (err);
11028 11028 }
11029 11029
11030 11030 /* ARGSUSED */
11031 11031 int
11032 11032 ip_sioctl_netmask_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11033 11033 ip_ioctl_cmd_t *ipip, void *if_req)
11034 11034 {
11035 11035 ip1dbg(("ip_sioctl_netmask_restart(%s:%u %p)\n",
11036 11036 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11037 11037 (void) ipif_down_tail(ipif);
11038 11038 return (ip_sioctl_netmask_tail(ipif, sin, q, mp));
11039 11039 }
11040 11040
11041 11041 /* Get interface net mask. */
11042 11042 /* ARGSUSED */
11043 11043 int
11044 11044 ip_sioctl_get_netmask(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11045 11045 ip_ioctl_cmd_t *ipip, void *if_req)
11046 11046 {
11047 11047 struct lifreq *lifr = (struct lifreq *)if_req;
11048 11048 struct sockaddr_in6 *sin6 = (sin6_t *)sin;
11049 11049
11050 11050 ip1dbg(("ip_sioctl_get_netmask(%s:%u %p)\n",
11051 11051 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11052 11052
11053 11053 /*
11054 11054 * net mask can't change since we have a reference to the ipif.
11055 11055 */
11056 11056 if (ipif->ipif_isv6) {
11057 11057 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11058 11058 *sin6 = sin6_null;
11059 11059 sin6->sin6_family = AF_INET6;
11060 11060 sin6->sin6_addr = ipif->ipif_v6net_mask;
11061 11061 lifr->lifr_addrlen =
11062 11062 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11063 11063 } else {
11064 11064 *sin = sin_null;
11065 11065 sin->sin_family = AF_INET;
11066 11066 sin->sin_addr.s_addr = ipif->ipif_net_mask;
11067 11067 if (ipip->ipi_cmd_type == LIF_CMD) {
11068 11068 lifr->lifr_addrlen =
11069 11069 ip_mask_to_plen(ipif->ipif_net_mask);
11070 11070 }
11071 11071 }
11072 11072 return (0);
11073 11073 }
11074 11074
11075 11075 /* ARGSUSED */
11076 11076 int
11077 11077 ip_sioctl_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11078 11078 ip_ioctl_cmd_t *ipip, void *if_req)
11079 11079 {
11080 11080 ip1dbg(("ip_sioctl_metric(%s:%u %p)\n",
11081 11081 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11082 11082
11083 11083 /*
11084 11084 * Since no applications should ever be setting metrics on underlying
11085 11085 * interfaces, we explicitly fail to smoke 'em out.
11086 11086 */
11087 11087 if (IS_UNDER_IPMP(ipif->ipif_ill))
11088 11088 return (EINVAL);
11089 11089
11090 11090 /*
11091 11091 * Set interface metric. We don't use this for
11092 11092 * anything but we keep track of it in case it is
11093 11093 * important to routing applications or such.
11094 11094 */
11095 11095 if (ipip->ipi_cmd_type == IF_CMD) {
11096 11096 struct ifreq *ifr;
11097 11097
11098 11098 ifr = (struct ifreq *)if_req;
11099 11099 ipif->ipif_ill->ill_metric = ifr->ifr_metric;
11100 11100 } else {
11101 11101 struct lifreq *lifr;
11102 11102
11103 11103 lifr = (struct lifreq *)if_req;
11104 11104 ipif->ipif_ill->ill_metric = lifr->lifr_metric;
11105 11105 }
11106 11106 return (0);
11107 11107 }
11108 11108
11109 11109 /* ARGSUSED */
11110 11110 int
11111 11111 ip_sioctl_get_metric(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11112 11112 ip_ioctl_cmd_t *ipip, void *if_req)
11113 11113 {
11114 11114 /* Get interface metric. */
11115 11115 ip1dbg(("ip_sioctl_get_metric(%s:%u %p)\n",
11116 11116 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11117 11117
11118 11118 if (ipip->ipi_cmd_type == IF_CMD) {
11119 11119 struct ifreq *ifr;
11120 11120
11121 11121 ifr = (struct ifreq *)if_req;
11122 11122 ifr->ifr_metric = ipif->ipif_ill->ill_metric;
11123 11123 } else {
11124 11124 struct lifreq *lifr;
11125 11125
11126 11126 lifr = (struct lifreq *)if_req;
11127 11127 lifr->lifr_metric = ipif->ipif_ill->ill_metric;
11128 11128 }
11129 11129
11130 11130 return (0);
11131 11131 }
11132 11132
11133 11133 /* ARGSUSED */
11134 11134 int
11135 11135 ip_sioctl_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11136 11136 ip_ioctl_cmd_t *ipip, void *if_req)
11137 11137 {
11138 11138 int arp_muxid;
11139 11139
11140 11140 ip1dbg(("ip_sioctl_muxid(%s:%u %p)\n",
11141 11141 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11142 11142 /*
11143 11143 * Set the muxid returned from I_PLINK.
11144 11144 */
11145 11145 if (ipip->ipi_cmd_type == IF_CMD) {
11146 11146 struct ifreq *ifr = (struct ifreq *)if_req;
11147 11147
11148 11148 ipif->ipif_ill->ill_muxid = ifr->ifr_ip_muxid;
11149 11149 arp_muxid = ifr->ifr_arp_muxid;
11150 11150 } else {
11151 11151 struct lifreq *lifr = (struct lifreq *)if_req;
11152 11152
11153 11153 ipif->ipif_ill->ill_muxid = lifr->lifr_ip_muxid;
11154 11154 arp_muxid = lifr->lifr_arp_muxid;
11155 11155 }
11156 11156 arl_set_muxid(ipif->ipif_ill, arp_muxid);
11157 11157 return (0);
11158 11158 }
11159 11159
11160 11160 /* ARGSUSED */
11161 11161 int
11162 11162 ip_sioctl_get_muxid(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11163 11163 ip_ioctl_cmd_t *ipip, void *if_req)
11164 11164 {
11165 11165 int arp_muxid = 0;
11166 11166
11167 11167 ip1dbg(("ip_sioctl_get_muxid(%s:%u %p)\n",
11168 11168 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11169 11169 /*
11170 11170 * Get the muxid saved in ill for I_PUNLINK.
11171 11171 */
11172 11172 arp_muxid = arl_get_muxid(ipif->ipif_ill);
11173 11173 if (ipip->ipi_cmd_type == IF_CMD) {
11174 11174 struct ifreq *ifr = (struct ifreq *)if_req;
11175 11175
11176 11176 ifr->ifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11177 11177 ifr->ifr_arp_muxid = arp_muxid;
11178 11178 } else {
11179 11179 struct lifreq *lifr = (struct lifreq *)if_req;
11180 11180
11181 11181 lifr->lifr_ip_muxid = ipif->ipif_ill->ill_muxid;
11182 11182 lifr->lifr_arp_muxid = arp_muxid;
11183 11183 }
11184 11184 return (0);
11185 11185 }
11186 11186
11187 11187 /*
11188 11188 * Set the subnet prefix. Does not modify the broadcast address.
11189 11189 */
11190 11190 /* ARGSUSED */
11191 11191 int
11192 11192 ip_sioctl_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11193 11193 ip_ioctl_cmd_t *ipip, void *if_req)
11194 11194 {
11195 11195 int err = 0;
11196 11196 in6_addr_t v6addr;
11197 11197 in6_addr_t v6mask;
11198 11198 boolean_t need_up = B_FALSE;
11199 11199 int addrlen;
11200 11200
11201 11201 ip1dbg(("ip_sioctl_subnet(%s:%u %p)\n",
11202 11202 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11203 11203
11204 11204 ASSERT(IAM_WRITER_IPIF(ipif));
11205 11205 addrlen = ((struct lifreq *)if_req)->lifr_addrlen;
11206 11206
11207 11207 if (ipif->ipif_isv6) {
11208 11208 sin6_t *sin6;
11209 11209
11210 11210 if (sin->sin_family != AF_INET6)
11211 11211 return (EAFNOSUPPORT);
11212 11212
11213 11213 sin6 = (sin6_t *)sin;
11214 11214 v6addr = sin6->sin6_addr;
11215 11215 if (!ip_remote_addr_ok_v6(&v6addr, &ipv6_all_ones))
11216 11216 return (EADDRNOTAVAIL);
11217 11217 } else {
11218 11218 ipaddr_t addr;
11219 11219
11220 11220 if (sin->sin_family != AF_INET)
11221 11221 return (EAFNOSUPPORT);
11222 11222
11223 11223 addr = sin->sin_addr.s_addr;
11224 11224 if (!ip_addr_ok_v4(addr, 0xFFFFFFFF))
11225 11225 return (EADDRNOTAVAIL);
11226 11226 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11227 11227 /* Add 96 bits */
11228 11228 addrlen += IPV6_ABITS - IP_ABITS;
11229 11229 }
11230 11230
11231 11231 if (ip_plen_to_mask_v6(addrlen, &v6mask) == NULL)
11232 11232 return (EINVAL);
11233 11233
11234 11234 /* Check if bits in the address is set past the mask */
11235 11235 if (!V6_MASK_EQ(v6addr, v6mask, v6addr))
11236 11236 return (EINVAL);
11237 11237
11238 11238 if (IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6subnet, &v6addr) &&
11239 11239 IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6net_mask, &v6mask))
11240 11240 return (0); /* No change */
11241 11241
11242 11242 if (ipif->ipif_flags & IPIF_UP) {
11243 11243 /*
11244 11244 * If the interface is already marked up,
11245 11245 * we call ipif_down which will take care
11246 11246 * of ditching any IREs that have been set
11247 11247 * up based on the old interface address.
11248 11248 */
11249 11249 err = ipif_logical_down(ipif, q, mp);
11250 11250 if (err == EINPROGRESS)
11251 11251 return (err);
11252 11252 (void) ipif_down_tail(ipif);
11253 11253 need_up = B_TRUE;
11254 11254 }
11255 11255
11256 11256 err = ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, need_up);
11257 11257 return (err);
11258 11258 }
11259 11259
11260 11260 static int
11261 11261 ip_sioctl_subnet_tail(ipif_t *ipif, in6_addr_t v6addr, in6_addr_t v6mask,
11262 11262 queue_t *q, mblk_t *mp, boolean_t need_up)
11263 11263 {
11264 11264 ill_t *ill = ipif->ipif_ill;
11265 11265 int err = 0;
11266 11266
11267 11267 ip1dbg(("ip_sioctl_subnet_tail(%s:%u %p)\n",
11268 11268 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11269 11269
11270 11270 /* Set the new address. */
11271 11271 mutex_enter(&ill->ill_lock);
11272 11272 ipif->ipif_v6net_mask = v6mask;
11273 11273 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
11274 11274 V6_MASK_COPY(v6addr, ipif->ipif_v6net_mask,
11275 11275 ipif->ipif_v6subnet);
11276 11276 }
11277 11277 mutex_exit(&ill->ill_lock);
11278 11278
11279 11279 if (need_up) {
11280 11280 /*
11281 11281 * Now bring the interface back up. If this
11282 11282 * is the only IPIF for the ILL, ipif_up
11283 11283 * will have to re-bind to the device, so
11284 11284 * we may get back EINPROGRESS, in which
11285 11285 * case, this IOCTL will get completed in
11286 11286 * ip_rput_dlpi when we see the DL_BIND_ACK.
11287 11287 */
11288 11288 err = ipif_up(ipif, q, mp);
11289 11289 if (err == EINPROGRESS)
11290 11290 return (err);
11291 11291 }
11292 11292 return (err);
11293 11293 }
11294 11294
11295 11295 /* ARGSUSED */
11296 11296 int
11297 11297 ip_sioctl_subnet_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11298 11298 ip_ioctl_cmd_t *ipip, void *if_req)
11299 11299 {
11300 11300 int addrlen;
11301 11301 in6_addr_t v6addr;
11302 11302 in6_addr_t v6mask;
11303 11303 struct lifreq *lifr = (struct lifreq *)if_req;
11304 11304
11305 11305 ip1dbg(("ip_sioctl_subnet_restart(%s:%u %p)\n",
11306 11306 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11307 11307 (void) ipif_down_tail(ipif);
11308 11308
11309 11309 addrlen = lifr->lifr_addrlen;
11310 11310 if (ipif->ipif_isv6) {
11311 11311 sin6_t *sin6;
11312 11312
11313 11313 sin6 = (sin6_t *)sin;
11314 11314 v6addr = sin6->sin6_addr;
11315 11315 } else {
11316 11316 ipaddr_t addr;
11317 11317
11318 11318 addr = sin->sin_addr.s_addr;
11319 11319 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr);
11320 11320 addrlen += IPV6_ABITS - IP_ABITS;
11321 11321 }
11322 11322 (void) ip_plen_to_mask_v6(addrlen, &v6mask);
11323 11323
11324 11324 return (ip_sioctl_subnet_tail(ipif, v6addr, v6mask, q, mp, B_TRUE));
11325 11325 }
11326 11326
11327 11327 /* ARGSUSED */
11328 11328 int
11329 11329 ip_sioctl_get_subnet(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11330 11330 ip_ioctl_cmd_t *ipip, void *if_req)
11331 11331 {
11332 11332 struct lifreq *lifr = (struct lifreq *)if_req;
11333 11333 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
11334 11334
11335 11335 ip1dbg(("ip_sioctl_get_subnet(%s:%u %p)\n",
11336 11336 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11337 11337 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
11338 11338
11339 11339 if (ipif->ipif_isv6) {
11340 11340 *sin6 = sin6_null;
11341 11341 sin6->sin6_family = AF_INET6;
11342 11342 sin6->sin6_addr = ipif->ipif_v6subnet;
11343 11343 lifr->lifr_addrlen =
11344 11344 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
11345 11345 } else {
11346 11346 *sin = sin_null;
11347 11347 sin->sin_family = AF_INET;
11348 11348 sin->sin_addr.s_addr = ipif->ipif_subnet;
11349 11349 lifr->lifr_addrlen = ip_mask_to_plen(ipif->ipif_net_mask);
11350 11350 }
11351 11351 return (0);
11352 11352 }
11353 11353
11354 11354 /*
11355 11355 * Set the IPv6 address token.
11356 11356 */
11357 11357 /* ARGSUSED */
11358 11358 int
11359 11359 ip_sioctl_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11360 11360 ip_ioctl_cmd_t *ipi, void *if_req)
11361 11361 {
11362 11362 ill_t *ill = ipif->ipif_ill;
11363 11363 int err;
11364 11364 in6_addr_t v6addr;
11365 11365 in6_addr_t v6mask;
11366 11366 boolean_t need_up = B_FALSE;
11367 11367 int i;
11368 11368 sin6_t *sin6 = (sin6_t *)sin;
11369 11369 struct lifreq *lifr = (struct lifreq *)if_req;
11370 11370 int addrlen;
11371 11371
11372 11372 ip1dbg(("ip_sioctl_token(%s:%u %p)\n",
11373 11373 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11374 11374 ASSERT(IAM_WRITER_IPIF(ipif));
11375 11375
11376 11376 addrlen = lifr->lifr_addrlen;
11377 11377 /* Only allow for logical unit zero i.e. not on "le0:17" */
11378 11378 if (ipif->ipif_id != 0)
11379 11379 return (EINVAL);
11380 11380
11381 11381 if (!ipif->ipif_isv6)
11382 11382 return (EINVAL);
11383 11383
11384 11384 if (addrlen > IPV6_ABITS)
11385 11385 return (EINVAL);
11386 11386
11387 11387 v6addr = sin6->sin6_addr;
11388 11388
11389 11389 /*
11390 11390 * The length of the token is the length from the end. To get
11391 11391 * the proper mask for this, compute the mask of the bits not
11392 11392 * in the token; ie. the prefix, and then xor to get the mask.
11393 11393 */
11394 11394 if (ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask) == NULL)
11395 11395 return (EINVAL);
11396 11396 for (i = 0; i < 4; i++) {
11397 11397 v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11398 11398 }
11399 11399
11400 11400 if (V6_MASK_EQ(v6addr, v6mask, ill->ill_token) &&
11401 11401 ill->ill_token_length == addrlen)
11402 11402 return (0); /* No change */
11403 11403
11404 11404 if (ipif->ipif_flags & IPIF_UP) {
11405 11405 err = ipif_logical_down(ipif, q, mp);
11406 11406 if (err == EINPROGRESS)
11407 11407 return (err);
11408 11408 (void) ipif_down_tail(ipif);
11409 11409 need_up = B_TRUE;
11410 11410 }
11411 11411 err = ip_sioctl_token_tail(ipif, sin6, addrlen, q, mp, need_up);
11412 11412 return (err);
11413 11413 }
11414 11414
11415 11415 static int
11416 11416 ip_sioctl_token_tail(ipif_t *ipif, sin6_t *sin6, int addrlen, queue_t *q,
11417 11417 mblk_t *mp, boolean_t need_up)
11418 11418 {
11419 11419 in6_addr_t v6addr;
11420 11420 in6_addr_t v6mask;
11421 11421 ill_t *ill = ipif->ipif_ill;
11422 11422 int i;
11423 11423 int err = 0;
11424 11424
11425 11425 ip1dbg(("ip_sioctl_token_tail(%s:%u %p)\n",
11426 11426 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11427 11427 v6addr = sin6->sin6_addr;
11428 11428 /*
11429 11429 * The length of the token is the length from the end. To get
11430 11430 * the proper mask for this, compute the mask of the bits not
11431 11431 * in the token; ie. the prefix, and then xor to get the mask.
11432 11432 */
11433 11433 (void) ip_plen_to_mask_v6(IPV6_ABITS - addrlen, &v6mask);
11434 11434 for (i = 0; i < 4; i++)
11435 11435 v6mask.s6_addr32[i] ^= (uint32_t)0xffffffff;
11436 11436
11437 11437 mutex_enter(&ill->ill_lock);
11438 11438 V6_MASK_COPY(v6addr, v6mask, ill->ill_token);
11439 11439 ill->ill_token_length = addrlen;
11440 11440 ill->ill_manual_token = 1;
11441 11441
11442 11442 /* Reconfigure the link-local address based on this new token */
11443 11443 ipif_setlinklocal(ill->ill_ipif);
11444 11444
11445 11445 mutex_exit(&ill->ill_lock);
11446 11446
11447 11447 if (need_up) {
11448 11448 /*
11449 11449 * Now bring the interface back up. If this
11450 11450 * is the only IPIF for the ILL, ipif_up
11451 11451 * will have to re-bind to the device, so
11452 11452 * we may get back EINPROGRESS, in which
11453 11453 * case, this IOCTL will get completed in
11454 11454 * ip_rput_dlpi when we see the DL_BIND_ACK.
11455 11455 */
11456 11456 err = ipif_up(ipif, q, mp);
11457 11457 if (err == EINPROGRESS)
11458 11458 return (err);
11459 11459 }
11460 11460 return (err);
11461 11461 }
11462 11462
11463 11463 /* ARGSUSED */
11464 11464 int
11465 11465 ip_sioctl_get_token(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11466 11466 ip_ioctl_cmd_t *ipi, void *if_req)
11467 11467 {
11468 11468 ill_t *ill;
11469 11469 sin6_t *sin6 = (sin6_t *)sin;
11470 11470 struct lifreq *lifr = (struct lifreq *)if_req;
11471 11471
11472 11472 ip1dbg(("ip_sioctl_get_token(%s:%u %p)\n",
11473 11473 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11474 11474 if (ipif->ipif_id != 0)
11475 11475 return (EINVAL);
11476 11476
11477 11477 ill = ipif->ipif_ill;
11478 11478 if (!ill->ill_isv6)
11479 11479 return (ENXIO);
11480 11480
11481 11481 *sin6 = sin6_null;
11482 11482 sin6->sin6_family = AF_INET6;
11483 11483 ASSERT(!IN6_IS_ADDR_V4MAPPED(&ill->ill_token));
11484 11484 sin6->sin6_addr = ill->ill_token;
11485 11485 lifr->lifr_addrlen = ill->ill_token_length;
11486 11486 return (0);
11487 11487 }
11488 11488
11489 11489 /*
11490 11490 * Set (hardware) link specific information that might override
11491 11491 * what was acquired through the DL_INFO_ACK.
11492 11492 */
11493 11493 /* ARGSUSED */
11494 11494 int
11495 11495 ip_sioctl_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11496 11496 ip_ioctl_cmd_t *ipi, void *if_req)
11497 11497 {
11498 11498 ill_t *ill = ipif->ipif_ill;
11499 11499 int ip_min_mtu;
11500 11500 struct lifreq *lifr = (struct lifreq *)if_req;
11501 11501 lif_ifinfo_req_t *lir;
11502 11502
11503 11503 ip1dbg(("ip_sioctl_lnkinfo(%s:%u %p)\n",
11504 11504 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11505 11505 lir = &lifr->lifr_ifinfo;
11506 11506 ASSERT(IAM_WRITER_IPIF(ipif));
11507 11507
11508 11508 /* Only allow for logical unit zero i.e. not on "bge0:17" */
11509 11509 if (ipif->ipif_id != 0)
11510 11510 return (EINVAL);
11511 11511
11512 11512 /* Set interface MTU. */
11513 11513 if (ipif->ipif_isv6)
11514 11514 ip_min_mtu = IPV6_MIN_MTU;
11515 11515 else
11516 11516 ip_min_mtu = IP_MIN_MTU;
11517 11517
11518 11518 /*
11519 11519 * Verify values before we set anything. Allow zero to
11520 11520 * mean unspecified.
11521 11521 *
11522 11522 * XXX We should be able to set the user-defined lir_mtu to some value
11523 11523 * that is greater than ill_current_frag but less than ill_max_frag- the
11524 11524 * ill_max_frag value tells us the max MTU that can be handled by the
11525 11525 * datalink, whereas the ill_current_frag is dynamically computed for
11526 11526 * some link-types like tunnels, based on the tunnel PMTU. However,
11527 11527 * since there is currently no way of distinguishing between
11528 11528 * administratively fixed link mtu values (e.g., those set via
11529 11529 * /sbin/dladm) and dynamically discovered MTUs (e.g., those discovered
11530 11530 * for tunnels) we conservatively choose the ill_current_frag as the
11531 11531 * upper-bound.
11532 11532 */
11533 11533 if (lir->lir_maxmtu != 0 &&
11534 11534 (lir->lir_maxmtu > ill->ill_current_frag ||
11535 11535 lir->lir_maxmtu < ip_min_mtu))
11536 11536 return (EINVAL);
11537 11537 if (lir->lir_reachtime != 0 &&
11538 11538 lir->lir_reachtime > ND_MAX_REACHTIME)
11539 11539 return (EINVAL);
11540 11540 if (lir->lir_reachretrans != 0 &&
11541 11541 lir->lir_reachretrans > ND_MAX_REACHRETRANSTIME)
11542 11542 return (EINVAL);
11543 11543
11544 11544 mutex_enter(&ill->ill_lock);
11545 11545 /*
11546 11546 * The dce and fragmentation code can handle changes to ill_mtu
11547 11547 * concurrent with sending/fragmenting packets.
11548 11548 */
11549 11549 if (lir->lir_maxmtu != 0)
11550 11550 ill->ill_user_mtu = lir->lir_maxmtu;
11551 11551
11552 11552 if (lir->lir_reachtime != 0)
11553 11553 ill->ill_reachable_time = lir->lir_reachtime;
11554 11554
11555 11555 if (lir->lir_reachretrans != 0)
11556 11556 ill->ill_reachable_retrans_time = lir->lir_reachretrans;
11557 11557
11558 11558 ill->ill_max_hops = lir->lir_maxhops;
11559 11559 ill->ill_max_buf = ND_MAX_Q;
11560 11560 if (!(ill->ill_flags & ILLF_FIXEDMTU) && ill->ill_user_mtu != 0) {
11561 11561 /*
11562 11562 * ill_mtu is the actual interface MTU, obtained as the min
11563 11563 * of user-configured mtu and the value announced by the
11564 11564 * driver (via DL_NOTE_SDU_SIZE/DL_INFO_ACK). Note that since
11565 11565 * we have already made the choice of requiring
11566 11566 * ill_user_mtu < ill_current_frag by the time we get here,
11567 11567 * the ill_mtu effectively gets assigned to the ill_user_mtu
11568 11568 * here.
11569 11569 */
11570 11570 ill->ill_mtu = MIN(ill->ill_current_frag, ill->ill_user_mtu);
11571 11571 ill->ill_mc_mtu = MIN(ill->ill_mc_mtu, ill->ill_user_mtu);
11572 11572 }
11573 11573 mutex_exit(&ill->ill_lock);
11574 11574
11575 11575 /*
11576 11576 * Make sure all dce_generation checks find out
11577 11577 * that ill_mtu/ill_mc_mtu has changed.
11578 11578 */
11579 11579 if (!(ill->ill_flags & ILLF_FIXEDMTU) && (lir->lir_maxmtu != 0))
11580 11580 dce_increment_all_generations(ill->ill_isv6, ill->ill_ipst);
11581 11581
11582 11582 /*
11583 11583 * Refresh IPMP meta-interface MTU if necessary.
11584 11584 */
11585 11585 if (IS_UNDER_IPMP(ill))
11586 11586 ipmp_illgrp_refresh_mtu(ill->ill_grp);
11587 11587
11588 11588 return (0);
11589 11589 }
11590 11590
11591 11591 /* ARGSUSED */
11592 11592 int
11593 11593 ip_sioctl_get_lnkinfo(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
11594 11594 ip_ioctl_cmd_t *ipi, void *if_req)
11595 11595 {
11596 11596 struct lif_ifinfo_req *lir;
11597 11597 ill_t *ill = ipif->ipif_ill;
11598 11598
11599 11599 ip1dbg(("ip_sioctl_get_lnkinfo(%s:%u %p)\n",
11600 11600 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
11601 11601 if (ipif->ipif_id != 0)
11602 11602 return (EINVAL);
11603 11603
11604 11604 lir = &((struct lifreq *)if_req)->lifr_ifinfo;
11605 11605 lir->lir_maxhops = ill->ill_max_hops;
11606 11606 lir->lir_reachtime = ill->ill_reachable_time;
11607 11607 lir->lir_reachretrans = ill->ill_reachable_retrans_time;
11608 11608 lir->lir_maxmtu = ill->ill_mtu;
11609 11609
11610 11610 return (0);
11611 11611 }
11612 11612
11613 11613 /*
11614 11614 * Return best guess as to the subnet mask for the specified address.
11615 11615 * Based on the subnet masks for all the configured interfaces.
11616 11616 *
11617 11617 * We end up returning a zero mask in the case of default, multicast or
11618 11618 * experimental.
11619 11619 */
11620 11620 static ipaddr_t
11621 11621 ip_subnet_mask(ipaddr_t addr, ipif_t **ipifp, ip_stack_t *ipst)
11622 11622 {
11623 11623 ipaddr_t net_mask;
11624 11624 ill_t *ill;
11625 11625 ipif_t *ipif;
11626 11626 ill_walk_context_t ctx;
11627 11627 ipif_t *fallback_ipif = NULL;
11628 11628
11629 11629 net_mask = ip_net_mask(addr);
11630 11630 if (net_mask == 0) {
11631 11631 *ipifp = NULL;
11632 11632 return (0);
11633 11633 }
11634 11634
11635 11635 /* Let's check to see if this is maybe a local subnet route. */
11636 11636 /* this function only applies to IPv4 interfaces */
11637 11637 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
11638 11638 ill = ILL_START_WALK_V4(&ctx, ipst);
11639 11639 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
11640 11640 mutex_enter(&ill->ill_lock);
11641 11641 for (ipif = ill->ill_ipif; ipif != NULL;
11642 11642 ipif = ipif->ipif_next) {
11643 11643 if (IPIF_IS_CONDEMNED(ipif))
11644 11644 continue;
11645 11645 if (!(ipif->ipif_flags & IPIF_UP))
11646 11646 continue;
11647 11647 if ((ipif->ipif_subnet & net_mask) ==
11648 11648 (addr & net_mask)) {
11649 11649 /*
11650 11650 * Don't trust pt-pt interfaces if there are
11651 11651 * other interfaces.
11652 11652 */
11653 11653 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
11654 11654 if (fallback_ipif == NULL) {
11655 11655 ipif_refhold_locked(ipif);
11656 11656 fallback_ipif = ipif;
11657 11657 }
11658 11658 continue;
11659 11659 }
11660 11660
11661 11661 /*
11662 11662 * Fine. Just assume the same net mask as the
11663 11663 * directly attached subnet interface is using.
11664 11664 */
11665 11665 ipif_refhold_locked(ipif);
11666 11666 mutex_exit(&ill->ill_lock);
11667 11667 rw_exit(&ipst->ips_ill_g_lock);
11668 11668 if (fallback_ipif != NULL)
11669 11669 ipif_refrele(fallback_ipif);
11670 11670 *ipifp = ipif;
11671 11671 return (ipif->ipif_net_mask);
11672 11672 }
11673 11673 }
11674 11674 mutex_exit(&ill->ill_lock);
11675 11675 }
11676 11676 rw_exit(&ipst->ips_ill_g_lock);
11677 11677
11678 11678 *ipifp = fallback_ipif;
11679 11679 return ((fallback_ipif != NULL) ?
11680 11680 fallback_ipif->ipif_net_mask : net_mask);
11681 11681 }
11682 11682
11683 11683 /*
11684 11684 * ip_sioctl_copyin_setup calls ip_wput_ioctl to process the IP_IOCTL ioctl.
11685 11685 */
11686 11686 static void
11687 11687 ip_wput_ioctl(queue_t *q, mblk_t *mp)
11688 11688 {
11689 11689 IOCP iocp;
11690 11690 ipft_t *ipft;
11691 11691 ipllc_t *ipllc;
11692 11692 mblk_t *mp1;
11693 11693 cred_t *cr;
11694 11694 int error = 0;
11695 11695 conn_t *connp;
11696 11696
11697 11697 ip1dbg(("ip_wput_ioctl"));
11698 11698 iocp = (IOCP)mp->b_rptr;
11699 11699 mp1 = mp->b_cont;
11700 11700 if (mp1 == NULL) {
11701 11701 iocp->ioc_error = EINVAL;
11702 11702 mp->b_datap->db_type = M_IOCNAK;
11703 11703 iocp->ioc_count = 0;
11704 11704 qreply(q, mp);
11705 11705 return;
11706 11706 }
11707 11707
11708 11708 /*
11709 11709 * These IOCTLs provide various control capabilities to
11710 11710 * upstream agents such as ULPs and processes. There
11711 11711 * are currently two such IOCTLs implemented. They
11712 11712 * are used by TCP to provide update information for
11713 11713 * existing IREs and to forcibly delete an IRE for a
11714 11714 * host that is not responding, thereby forcing an
11715 11715 * attempt at a new route.
11716 11716 */
11717 11717 iocp->ioc_error = EINVAL;
11718 11718 if (!pullupmsg(mp1, sizeof (ipllc->ipllc_cmd)))
11719 11719 goto done;
11720 11720
11721 11721 ipllc = (ipllc_t *)mp1->b_rptr;
11722 11722 for (ipft = ip_ioctl_ftbl; ipft->ipft_pfi; ipft++) {
11723 11723 if (ipllc->ipllc_cmd == ipft->ipft_cmd)
11724 11724 break;
11725 11725 }
11726 11726 /*
11727 11727 * prefer credential from mblk over ioctl;
11728 11728 * see ip_sioctl_copyin_setup
11729 11729 */
11730 11730 cr = msg_getcred(mp, NULL);
11731 11731 if (cr == NULL)
11732 11732 cr = iocp->ioc_cr;
11733 11733
11734 11734 /*
11735 11735 * Refhold the conn in case the request gets queued up in some lookup
11736 11736 */
11737 11737 ASSERT(CONN_Q(q));
11738 11738 connp = Q_TO_CONN(q);
11739 11739 CONN_INC_REF(connp);
11740 11740 CONN_INC_IOCTLREF(connp);
11741 11741 if (ipft->ipft_pfi &&
11742 11742 ((mp1->b_wptr - mp1->b_rptr) >= ipft->ipft_min_size ||
11743 11743 pullupmsg(mp1, ipft->ipft_min_size))) {
11744 11744 error = (*ipft->ipft_pfi)(q,
11745 11745 (ipft->ipft_flags & IPFT_F_SELF_REPLY) ? mp : mp1, cr);
11746 11746 }
11747 11747 if (ipft->ipft_flags & IPFT_F_SELF_REPLY) {
11748 11748 /*
11749 11749 * CONN_OPER_PENDING_DONE happens in the function called
11750 11750 * through ipft_pfi above.
11751 11751 */
11752 11752 return;
11753 11753 }
11754 11754
11755 11755 CONN_DEC_IOCTLREF(connp);
11756 11756 CONN_OPER_PENDING_DONE(connp);
11757 11757 if (ipft->ipft_flags & IPFT_F_NO_REPLY) {
11758 11758 freemsg(mp);
11759 11759 return;
11760 11760 }
11761 11761 iocp->ioc_error = error;
11762 11762
11763 11763 done:
11764 11764 mp->b_datap->db_type = M_IOCACK;
11765 11765 if (iocp->ioc_error)
11766 11766 iocp->ioc_count = 0;
11767 11767 qreply(q, mp);
11768 11768 }
↓ open down ↓ |
11768 lines elided |
↑ open up ↑ |
11769 11769
11770 11770 /*
11771 11771 * Assign a unique id for the ipif. This is used by sctp_addr.c
11772 11772 * Note: remove if sctp_addr.c is redone to not shadow ill/ipif data structures.
11773 11773 */
11774 11774 static void
11775 11775 ipif_assign_seqid(ipif_t *ipif)
11776 11776 {
11777 11777 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
11778 11778
11779 - ipif->ipif_seqid = atomic_add_64_nv(&ipst->ips_ipif_g_seqid, 1);
11779 + ipif->ipif_seqid = atomic_inc_64_nv(&ipst->ips_ipif_g_seqid);
11780 11780 }
11781 11781
11782 11782 /*
11783 11783 * Clone the contents of `sipif' to `dipif'. Requires that both ipifs are
11784 11784 * administratively down (i.e., no DAD), of the same type, and locked. Note
11785 11785 * that the clone is complete -- including the seqid -- and the expectation is
11786 11786 * that the caller will either free or overwrite `sipif' before it's unlocked.
11787 11787 */
11788 11788 static void
11789 11789 ipif_clone(const ipif_t *sipif, ipif_t *dipif)
11790 11790 {
11791 11791 ASSERT(MUTEX_HELD(&sipif->ipif_ill->ill_lock));
11792 11792 ASSERT(MUTEX_HELD(&dipif->ipif_ill->ill_lock));
11793 11793 ASSERT(!(sipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11794 11794 ASSERT(!(dipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)));
11795 11795 ASSERT(sipif->ipif_ire_type == dipif->ipif_ire_type);
11796 11796
11797 11797 dipif->ipif_flags = sipif->ipif_flags;
11798 11798 dipif->ipif_zoneid = sipif->ipif_zoneid;
11799 11799 dipif->ipif_v6subnet = sipif->ipif_v6subnet;
11800 11800 dipif->ipif_v6lcl_addr = sipif->ipif_v6lcl_addr;
11801 11801 dipif->ipif_v6net_mask = sipif->ipif_v6net_mask;
11802 11802 dipif->ipif_v6brd_addr = sipif->ipif_v6brd_addr;
11803 11803 dipif->ipif_v6pp_dst_addr = sipif->ipif_v6pp_dst_addr;
11804 11804
11805 11805 /*
11806 11806 * As per the comment atop the function, we assume that these sipif
11807 11807 * fields will be changed before sipif is unlocked.
11808 11808 */
11809 11809 dipif->ipif_seqid = sipif->ipif_seqid;
11810 11810 dipif->ipif_state_flags = sipif->ipif_state_flags;
11811 11811 }
11812 11812
11813 11813 /*
11814 11814 * Transfer the contents of `sipif' to `dipif', and then free (if `virgipif'
11815 11815 * is NULL) or overwrite `sipif' with `virgipif', which must be a virgin
11816 11816 * (unreferenced) ipif. Also, if `sipif' is used by the current xop, then
11817 11817 * transfer the xop to `dipif'. Requires that all ipifs are administratively
11818 11818 * down (i.e., no DAD), of the same type, and unlocked.
11819 11819 */
11820 11820 static void
11821 11821 ipif_transfer(ipif_t *sipif, ipif_t *dipif, ipif_t *virgipif)
11822 11822 {
11823 11823 ipsq_t *ipsq = sipif->ipif_ill->ill_phyint->phyint_ipsq;
11824 11824 ipxop_t *ipx = ipsq->ipsq_xop;
11825 11825
11826 11826 ASSERT(sipif != dipif);
11827 11827 ASSERT(sipif != virgipif);
11828 11828
11829 11829 /*
11830 11830 * Grab all of the locks that protect the ipif in a defined order.
11831 11831 */
11832 11832 GRAB_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11833 11833
11834 11834 ipif_clone(sipif, dipif);
11835 11835 if (virgipif != NULL) {
11836 11836 ipif_clone(virgipif, sipif);
11837 11837 mi_free(virgipif);
11838 11838 }
11839 11839
11840 11840 RELEASE_ILL_LOCKS(sipif->ipif_ill, dipif->ipif_ill);
11841 11841
11842 11842 /*
11843 11843 * Transfer ownership of the current xop, if necessary.
11844 11844 */
11845 11845 if (ipx->ipx_current_ipif == sipif) {
11846 11846 ASSERT(ipx->ipx_pending_ipif == NULL);
11847 11847 mutex_enter(&ipx->ipx_lock);
11848 11848 ipx->ipx_current_ipif = dipif;
11849 11849 mutex_exit(&ipx->ipx_lock);
11850 11850 }
11851 11851
11852 11852 if (virgipif == NULL)
11853 11853 mi_free(sipif);
11854 11854 }
11855 11855
11856 11856 /*
11857 11857 * checks if:
11858 11858 * - <ill_name>:<ipif_id> is at most LIFNAMSIZ - 1 and
11859 11859 * - logical interface is within the allowed range
11860 11860 */
11861 11861 static int
11862 11862 is_lifname_valid(ill_t *ill, unsigned int ipif_id)
11863 11863 {
11864 11864 if (snprintf(NULL, 0, "%s:%d", ill->ill_name, ipif_id) >= LIFNAMSIZ)
11865 11865 return (ENAMETOOLONG);
11866 11866
11867 11867 if (ipif_id >= ill->ill_ipst->ips_ip_addrs_per_if)
11868 11868 return (ERANGE);
11869 11869 return (0);
11870 11870 }
11871 11871
11872 11872 /*
11873 11873 * Insert the ipif, so that the list of ipifs on the ill will be sorted
11874 11874 * with respect to ipif_id. Note that an ipif with an ipif_id of -1 will
11875 11875 * be inserted into the first space available in the list. The value of
11876 11876 * ipif_id will then be set to the appropriate value for its position.
11877 11877 */
11878 11878 static int
11879 11879 ipif_insert(ipif_t *ipif, boolean_t acquire_g_lock)
11880 11880 {
11881 11881 ill_t *ill;
11882 11882 ipif_t *tipif;
11883 11883 ipif_t **tipifp;
11884 11884 int id, err;
11885 11885 ip_stack_t *ipst;
11886 11886
11887 11887 ASSERT(ipif->ipif_ill->ill_net_type == IRE_LOOPBACK ||
11888 11888 IAM_WRITER_IPIF(ipif));
11889 11889
11890 11890 ill = ipif->ipif_ill;
11891 11891 ASSERT(ill != NULL);
11892 11892 ipst = ill->ill_ipst;
11893 11893
11894 11894 /*
11895 11895 * In the case of lo0:0 we already hold the ill_g_lock.
11896 11896 * ill_lookup_on_name (acquires ill_g_lock) -> ipif_allocate ->
11897 11897 * ipif_insert.
11898 11898 */
11899 11899 if (acquire_g_lock)
11900 11900 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
11901 11901 mutex_enter(&ill->ill_lock);
11902 11902 id = ipif->ipif_id;
11903 11903 tipifp = &(ill->ill_ipif);
11904 11904 if (id == -1) { /* need to find a real id */
11905 11905 id = 0;
11906 11906 while ((tipif = *tipifp) != NULL) {
11907 11907 ASSERT(tipif->ipif_id >= id);
11908 11908 if (tipif->ipif_id != id)
11909 11909 break; /* non-consecutive id */
11910 11910 id++;
11911 11911 tipifp = &(tipif->ipif_next);
11912 11912 }
11913 11913 if ((err = is_lifname_valid(ill, id)) != 0) {
11914 11914 mutex_exit(&ill->ill_lock);
11915 11915 if (acquire_g_lock)
11916 11916 rw_exit(&ipst->ips_ill_g_lock);
11917 11917 return (err);
11918 11918 }
11919 11919 ipif->ipif_id = id; /* assign new id */
11920 11920 } else if ((err = is_lifname_valid(ill, id)) == 0) {
11921 11921 /* we have a real id; insert ipif in the right place */
11922 11922 while ((tipif = *tipifp) != NULL) {
11923 11923 ASSERT(tipif->ipif_id != id);
11924 11924 if (tipif->ipif_id > id)
11925 11925 break; /* found correct location */
11926 11926 tipifp = &(tipif->ipif_next);
11927 11927 }
11928 11928 } else {
11929 11929 mutex_exit(&ill->ill_lock);
11930 11930 if (acquire_g_lock)
11931 11931 rw_exit(&ipst->ips_ill_g_lock);
11932 11932 return (err);
11933 11933 }
11934 11934
11935 11935 ASSERT(tipifp != &(ill->ill_ipif) || id == 0);
11936 11936
11937 11937 ipif->ipif_next = tipif;
11938 11938 *tipifp = ipif;
11939 11939 mutex_exit(&ill->ill_lock);
11940 11940 if (acquire_g_lock)
11941 11941 rw_exit(&ipst->ips_ill_g_lock);
11942 11942
11943 11943 return (0);
11944 11944 }
11945 11945
11946 11946 static void
11947 11947 ipif_remove(ipif_t *ipif)
11948 11948 {
11949 11949 ipif_t **ipifp;
11950 11950 ill_t *ill = ipif->ipif_ill;
11951 11951
11952 11952 ASSERT(RW_WRITE_HELD(&ill->ill_ipst->ips_ill_g_lock));
11953 11953
11954 11954 mutex_enter(&ill->ill_lock);
11955 11955 ipifp = &ill->ill_ipif;
11956 11956 for (; *ipifp != NULL; ipifp = &ipifp[0]->ipif_next) {
11957 11957 if (*ipifp == ipif) {
11958 11958 *ipifp = ipif->ipif_next;
11959 11959 break;
11960 11960 }
11961 11961 }
11962 11962 mutex_exit(&ill->ill_lock);
11963 11963 }
11964 11964
11965 11965 /*
11966 11966 * Allocate and initialize a new interface control structure. (Always
11967 11967 * called as writer.)
11968 11968 * When ipif_allocate() is called from ip_ll_subnet_defaults, the ill
11969 11969 * is not part of the global linked list of ills. ipif_seqid is unique
11970 11970 * in the system and to preserve the uniqueness, it is assigned only
11971 11971 * when ill becomes part of the global list. At that point ill will
11972 11972 * have a name. If it doesn't get assigned here, it will get assigned
11973 11973 * in ipif_set_values() as part of SIOCSLIFNAME processing.
11974 11974 * Aditionally, if we come here from ip_ll_subnet_defaults, we don't set
11975 11975 * the interface flags or any other information from the DL_INFO_ACK for
11976 11976 * DL_STYLE2 drivers (initialize == B_FALSE), since we won't have them at
11977 11977 * this point. The flags etc. will be set in ip_ll_subnet_defaults when the
11978 11978 * second DL_INFO_ACK comes in from the driver.
11979 11979 */
11980 11980 static ipif_t *
11981 11981 ipif_allocate(ill_t *ill, int id, uint_t ire_type, boolean_t initialize,
11982 11982 boolean_t insert, int *errorp)
11983 11983 {
11984 11984 int err;
11985 11985 ipif_t *ipif;
11986 11986 ip_stack_t *ipst = ill->ill_ipst;
11987 11987
11988 11988 ip1dbg(("ipif_allocate(%s:%d ill %p)\n",
11989 11989 ill->ill_name, id, (void *)ill));
11990 11990 ASSERT(ire_type == IRE_LOOPBACK || IAM_WRITER_ILL(ill));
11991 11991
11992 11992 if (errorp != NULL)
11993 11993 *errorp = 0;
11994 11994
11995 11995 if ((ipif = mi_alloc(sizeof (ipif_t), BPRI_MED)) == NULL) {
11996 11996 if (errorp != NULL)
11997 11997 *errorp = ENOMEM;
11998 11998 return (NULL);
11999 11999 }
12000 12000 *ipif = ipif_zero; /* start clean */
12001 12001
12002 12002 ipif->ipif_ill = ill;
12003 12003 ipif->ipif_id = id; /* could be -1 */
12004 12004 /*
12005 12005 * Inherit the zoneid from the ill; for the shared stack instance
12006 12006 * this is always the global zone
12007 12007 */
12008 12008 ipif->ipif_zoneid = ill->ill_zoneid;
12009 12009
12010 12010 ipif->ipif_refcnt = 0;
12011 12011
12012 12012 if (insert) {
12013 12013 if ((err = ipif_insert(ipif, ire_type != IRE_LOOPBACK)) != 0) {
12014 12014 mi_free(ipif);
12015 12015 if (errorp != NULL)
12016 12016 *errorp = err;
12017 12017 return (NULL);
12018 12018 }
12019 12019 /* -1 id should have been replaced by real id */
12020 12020 id = ipif->ipif_id;
12021 12021 ASSERT(id >= 0);
12022 12022 }
12023 12023
12024 12024 if (ill->ill_name[0] != '\0')
12025 12025 ipif_assign_seqid(ipif);
12026 12026
12027 12027 /*
12028 12028 * If this is the zeroth ipif on the IPMP ill, create the illgrp
12029 12029 * (which must not exist yet because the zeroth ipif is created once
12030 12030 * per ill). However, do not not link it to the ipmp_grp_t until
12031 12031 * I_PLINK is called; see ip_sioctl_plink_ipmp() for details.
12032 12032 */
12033 12033 if (id == 0 && IS_IPMP(ill)) {
12034 12034 if (ipmp_illgrp_create(ill) == NULL) {
12035 12035 if (insert) {
12036 12036 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
12037 12037 ipif_remove(ipif);
12038 12038 rw_exit(&ipst->ips_ill_g_lock);
12039 12039 }
12040 12040 mi_free(ipif);
12041 12041 if (errorp != NULL)
12042 12042 *errorp = ENOMEM;
12043 12043 return (NULL);
12044 12044 }
12045 12045 }
12046 12046
12047 12047 /*
12048 12048 * We grab ill_lock to protect the flag changes. The ipif is still
12049 12049 * not up and can't be looked up until the ioctl completes and the
12050 12050 * IPIF_CHANGING flag is cleared.
12051 12051 */
12052 12052 mutex_enter(&ill->ill_lock);
12053 12053
12054 12054 ipif->ipif_ire_type = ire_type;
12055 12055
12056 12056 if (ipif->ipif_isv6) {
12057 12057 ill->ill_flags |= ILLF_IPV6;
12058 12058 } else {
12059 12059 ipaddr_t inaddr_any = INADDR_ANY;
12060 12060
12061 12061 ill->ill_flags |= ILLF_IPV4;
12062 12062
12063 12063 /* Keep the IN6_IS_ADDR_V4MAPPED assertions happy */
12064 12064 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12065 12065 &ipif->ipif_v6lcl_addr);
12066 12066 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12067 12067 &ipif->ipif_v6subnet);
12068 12068 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12069 12069 &ipif->ipif_v6net_mask);
12070 12070 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12071 12071 &ipif->ipif_v6brd_addr);
12072 12072 IN6_IPADDR_TO_V4MAPPED(inaddr_any,
12073 12073 &ipif->ipif_v6pp_dst_addr);
12074 12074 }
12075 12075
12076 12076 /*
12077 12077 * Don't set the interface flags etc. now, will do it in
12078 12078 * ip_ll_subnet_defaults.
12079 12079 */
12080 12080 if (!initialize)
12081 12081 goto out;
12082 12082
12083 12083 /*
12084 12084 * NOTE: The IPMP meta-interface is special-cased because it starts
12085 12085 * with no underlying interfaces (and thus an unknown broadcast
12086 12086 * address length), but all interfaces that can be placed into an IPMP
12087 12087 * group are required to be broadcast-capable.
12088 12088 */
12089 12089 if (ill->ill_bcast_addr_length != 0 || IS_IPMP(ill)) {
12090 12090 /*
12091 12091 * Later detect lack of DLPI driver multicast capability by
12092 12092 * catching DL_ENABMULTI_REQ errors in ip_rput_dlpi().
12093 12093 */
12094 12094 ill->ill_flags |= ILLF_MULTICAST;
12095 12095 if (!ipif->ipif_isv6)
12096 12096 ipif->ipif_flags |= IPIF_BROADCAST;
12097 12097 } else {
12098 12098 if (ill->ill_net_type != IRE_LOOPBACK) {
12099 12099 if (ipif->ipif_isv6)
12100 12100 /*
12101 12101 * Note: xresolv interfaces will eventually need
12102 12102 * NOARP set here as well, but that will require
12103 12103 * those external resolvers to have some
12104 12104 * knowledge of that flag and act appropriately.
12105 12105 * Not to be changed at present.
12106 12106 */
12107 12107 ill->ill_flags |= ILLF_NONUD;
12108 12108 else
12109 12109 ill->ill_flags |= ILLF_NOARP;
12110 12110 }
12111 12111 if (ill->ill_phys_addr_length == 0) {
12112 12112 if (IS_VNI(ill)) {
12113 12113 ipif->ipif_flags |= IPIF_NOXMIT;
12114 12114 } else {
12115 12115 /* pt-pt supports multicast. */
12116 12116 ill->ill_flags |= ILLF_MULTICAST;
12117 12117 if (ill->ill_net_type != IRE_LOOPBACK)
12118 12118 ipif->ipif_flags |= IPIF_POINTOPOINT;
12119 12119 }
12120 12120 }
12121 12121 }
12122 12122 out:
12123 12123 mutex_exit(&ill->ill_lock);
12124 12124 return (ipif);
12125 12125 }
12126 12126
12127 12127 /*
12128 12128 * Remove the neighbor cache entries associated with this logical
12129 12129 * interface.
12130 12130 */
12131 12131 int
12132 12132 ipif_arp_down(ipif_t *ipif)
12133 12133 {
12134 12134 ill_t *ill = ipif->ipif_ill;
12135 12135 int err = 0;
12136 12136
12137 12137 ip1dbg(("ipif_arp_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
12138 12138 ASSERT(IAM_WRITER_IPIF(ipif));
12139 12139
12140 12140 DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_down",
12141 12141 ill_t *, ill, ipif_t *, ipif);
12142 12142 ipif_nce_down(ipif);
12143 12143
12144 12144 /*
12145 12145 * If this is the last ipif that is going down and there are no
12146 12146 * duplicate addresses we may yet attempt to re-probe, then we need to
12147 12147 * clean up ARP completely.
12148 12148 */
12149 12149 if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
12150 12150 !ill->ill_logical_down && ill->ill_net_type == IRE_IF_RESOLVER) {
12151 12151 /*
12152 12152 * If this was the last ipif on an IPMP interface, purge any
12153 12153 * static ARP entries associated with it.
12154 12154 */
12155 12155 if (IS_IPMP(ill))
12156 12156 ipmp_illgrp_refresh_arpent(ill->ill_grp);
12157 12157
12158 12158 /* UNBIND, DETACH */
12159 12159 err = arp_ll_down(ill);
12160 12160 }
12161 12161
12162 12162 return (err);
12163 12163 }
12164 12164
12165 12165 /*
12166 12166 * Get the resolver set up for a new IP address. (Always called as writer.)
12167 12167 * Called both for IPv4 and IPv6 interfaces, though it only does some
12168 12168 * basic DAD related initialization for IPv6. Honors ILLF_NOARP.
12169 12169 *
12170 12170 * The enumerated value res_act tunes the behavior:
12171 12171 * * Res_act_initial: set up all the resolver structures for a new
12172 12172 * IP address.
12173 12173 * * Res_act_defend: tell ARP that it needs to send a single gratuitous
12174 12174 * ARP message in defense of the address.
12175 12175 * * Res_act_rebind: tell ARP to change the hardware address for an IP
12176 12176 * address (and issue gratuitous ARPs). Used by ipmp_ill_bind_ipif().
12177 12177 *
12178 12178 * Returns zero on success, or an errno upon failure.
12179 12179 */
12180 12180 int
12181 12181 ipif_resolver_up(ipif_t *ipif, enum ip_resolver_action res_act)
12182 12182 {
12183 12183 ill_t *ill = ipif->ipif_ill;
12184 12184 int err;
12185 12185 boolean_t was_dup;
12186 12186
12187 12187 ip1dbg(("ipif_resolver_up(%s:%u) flags 0x%x\n",
12188 12188 ill->ill_name, ipif->ipif_id, (uint_t)ipif->ipif_flags));
12189 12189 ASSERT(IAM_WRITER_IPIF(ipif));
12190 12190
12191 12191 was_dup = B_FALSE;
12192 12192 if (res_act == Res_act_initial) {
12193 12193 ipif->ipif_addr_ready = 0;
12194 12194 /*
12195 12195 * We're bringing an interface up here. There's no way that we
12196 12196 * should need to shut down ARP now.
12197 12197 */
12198 12198 mutex_enter(&ill->ill_lock);
12199 12199 if (ipif->ipif_flags & IPIF_DUPLICATE) {
12200 12200 ipif->ipif_flags &= ~IPIF_DUPLICATE;
12201 12201 ill->ill_ipif_dup_count--;
12202 12202 was_dup = B_TRUE;
12203 12203 }
12204 12204 mutex_exit(&ill->ill_lock);
12205 12205 }
12206 12206 if (ipif->ipif_recovery_id != 0)
12207 12207 (void) untimeout(ipif->ipif_recovery_id);
12208 12208 ipif->ipif_recovery_id = 0;
12209 12209 if (ill->ill_net_type != IRE_IF_RESOLVER) {
12210 12210 ipif->ipif_addr_ready = 1;
12211 12211 return (0);
12212 12212 }
12213 12213 /* NDP will set the ipif_addr_ready flag when it's ready */
12214 12214 if (ill->ill_isv6)
12215 12215 return (0);
12216 12216
12217 12217 err = ipif_arp_up(ipif, res_act, was_dup);
12218 12218 return (err);
12219 12219 }
12220 12220
12221 12221 /*
12222 12222 * This routine restarts IPv4/IPv6 duplicate address detection (DAD)
12223 12223 * when a link has just gone back up.
12224 12224 */
12225 12225 static void
12226 12226 ipif_nce_start_dad(ipif_t *ipif)
12227 12227 {
12228 12228 ncec_t *ncec;
12229 12229 ill_t *ill = ipif->ipif_ill;
12230 12230 boolean_t isv6 = ill->ill_isv6;
12231 12231
12232 12232 if (isv6) {
12233 12233 ncec = ncec_lookup_illgrp_v6(ipif->ipif_ill,
12234 12234 &ipif->ipif_v6lcl_addr);
12235 12235 } else {
12236 12236 ipaddr_t v4addr;
12237 12237
12238 12238 if (ill->ill_net_type != IRE_IF_RESOLVER ||
12239 12239 (ipif->ipif_flags & IPIF_UNNUMBERED) ||
12240 12240 ipif->ipif_lcl_addr == INADDR_ANY) {
12241 12241 /*
12242 12242 * If we can't contact ARP for some reason,
12243 12243 * that's not really a problem. Just send
12244 12244 * out the routing socket notification that
12245 12245 * DAD completion would have done, and continue.
12246 12246 */
12247 12247 ipif_mask_reply(ipif);
12248 12248 ipif_up_notify(ipif);
12249 12249 ipif->ipif_addr_ready = 1;
12250 12250 return;
12251 12251 }
12252 12252
12253 12253 IN6_V4MAPPED_TO_IPADDR(&ipif->ipif_v6lcl_addr, v4addr);
12254 12254 ncec = ncec_lookup_illgrp_v4(ipif->ipif_ill, &v4addr);
12255 12255 }
12256 12256
12257 12257 if (ncec == NULL) {
12258 12258 ip1dbg(("couldn't find ncec for ipif %p leaving !ready\n",
12259 12259 (void *)ipif));
12260 12260 return;
12261 12261 }
12262 12262 if (!nce_restart_dad(ncec)) {
12263 12263 /*
12264 12264 * If we can't restart DAD for some reason, that's not really a
12265 12265 * problem. Just send out the routing socket notification that
12266 12266 * DAD completion would have done, and continue.
12267 12267 */
12268 12268 ipif_up_notify(ipif);
12269 12269 ipif->ipif_addr_ready = 1;
12270 12270 }
12271 12271 ncec_refrele(ncec);
12272 12272 }
12273 12273
12274 12274 /*
12275 12275 * Restart duplicate address detection on all interfaces on the given ill.
12276 12276 *
12277 12277 * This is called when an interface transitions from down to up
12278 12278 * (DL_NOTE_LINK_UP) or up to down (DL_NOTE_LINK_DOWN).
12279 12279 *
12280 12280 * Note that since the underlying physical link has transitioned, we must cause
12281 12281 * at least one routing socket message to be sent here, either via DAD
12282 12282 * completion or just by default on the first ipif. (If we don't do this, then
12283 12283 * in.mpathd will see long delays when doing link-based failure recovery.)
12284 12284 */
12285 12285 void
12286 12286 ill_restart_dad(ill_t *ill, boolean_t went_up)
12287 12287 {
12288 12288 ipif_t *ipif;
12289 12289
12290 12290 if (ill == NULL)
12291 12291 return;
12292 12292
12293 12293 /*
12294 12294 * If layer two doesn't support duplicate address detection, then just
12295 12295 * send the routing socket message now and be done with it.
12296 12296 */
12297 12297 if (!ill->ill_isv6 && arp_no_defense) {
12298 12298 ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12299 12299 return;
12300 12300 }
12301 12301
12302 12302 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12303 12303 if (went_up) {
12304 12304
12305 12305 if (ipif->ipif_flags & IPIF_UP) {
12306 12306 ipif_nce_start_dad(ipif);
12307 12307 } else if (ipif->ipif_flags & IPIF_DUPLICATE) {
12308 12308 /*
12309 12309 * kick off the bring-up process now.
12310 12310 */
12311 12311 ipif_do_recovery(ipif);
12312 12312 } else {
12313 12313 /*
12314 12314 * Unfortunately, the first ipif is "special"
12315 12315 * and represents the underlying ill in the
12316 12316 * routing socket messages. Thus, when this
12317 12317 * one ipif is down, we must still notify so
12318 12318 * that the user knows the IFF_RUNNING status
12319 12319 * change. (If the first ipif is up, then
12320 12320 * we'll handle eventual routing socket
12321 12321 * notification via DAD completion.)
12322 12322 */
12323 12323 if (ipif == ill->ill_ipif) {
12324 12324 ip_rts_ifmsg(ill->ill_ipif,
12325 12325 RTSQ_DEFAULT);
12326 12326 }
12327 12327 }
12328 12328 } else {
12329 12329 /*
12330 12330 * After link down, we'll need to send a new routing
12331 12331 * message when the link comes back, so clear
12332 12332 * ipif_addr_ready.
12333 12333 */
12334 12334 ipif->ipif_addr_ready = 0;
12335 12335 }
12336 12336 }
12337 12337
12338 12338 /*
12339 12339 * If we've torn down links, then notify the user right away.
12340 12340 */
12341 12341 if (!went_up)
12342 12342 ip_rts_ifmsg(ill->ill_ipif, RTSQ_DEFAULT);
12343 12343 }
12344 12344
12345 12345 static void
12346 12346 ipsq_delete(ipsq_t *ipsq)
12347 12347 {
12348 12348 ipxop_t *ipx = ipsq->ipsq_xop;
12349 12349
12350 12350 ipsq->ipsq_ipst = NULL;
12351 12351 ASSERT(ipsq->ipsq_phyint == NULL);
12352 12352 ASSERT(ipsq->ipsq_xop != NULL);
12353 12353 ASSERT(ipsq->ipsq_xopq_mphead == NULL && ipx->ipx_mphead == NULL);
12354 12354 ASSERT(ipx->ipx_pending_mp == NULL);
12355 12355 kmem_free(ipsq, sizeof (ipsq_t));
12356 12356 }
12357 12357
12358 12358 static int
12359 12359 ill_up_ipifs_on_ill(ill_t *ill, queue_t *q, mblk_t *mp)
12360 12360 {
12361 12361 int err = 0;
12362 12362 ipif_t *ipif;
12363 12363
12364 12364 if (ill == NULL)
12365 12365 return (0);
12366 12366
12367 12367 ASSERT(IAM_WRITER_ILL(ill));
12368 12368 ill->ill_up_ipifs = B_TRUE;
12369 12369 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12370 12370 if (ipif->ipif_was_up) {
12371 12371 if (!(ipif->ipif_flags & IPIF_UP))
12372 12372 err = ipif_up(ipif, q, mp);
12373 12373 ipif->ipif_was_up = B_FALSE;
12374 12374 if (err != 0) {
12375 12375 ASSERT(err == EINPROGRESS);
12376 12376 return (err);
12377 12377 }
12378 12378 }
12379 12379 }
12380 12380 ill->ill_up_ipifs = B_FALSE;
12381 12381 return (0);
12382 12382 }
12383 12383
12384 12384 /*
12385 12385 * This function is called to bring up all the ipifs that were up before
12386 12386 * bringing the ill down via ill_down_ipifs().
12387 12387 */
12388 12388 int
12389 12389 ill_up_ipifs(ill_t *ill, queue_t *q, mblk_t *mp)
12390 12390 {
12391 12391 int err;
12392 12392
12393 12393 ASSERT(IAM_WRITER_ILL(ill));
12394 12394
12395 12395 if (ill->ill_replumbing) {
12396 12396 ill->ill_replumbing = 0;
12397 12397 /*
12398 12398 * Send down REPLUMB_DONE notification followed by the
12399 12399 * BIND_REQ on the arp stream.
12400 12400 */
12401 12401 if (!ill->ill_isv6)
12402 12402 arp_send_replumb_conf(ill);
12403 12403 }
12404 12404 err = ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv4, q, mp);
12405 12405 if (err != 0)
12406 12406 return (err);
12407 12407
12408 12408 return (ill_up_ipifs_on_ill(ill->ill_phyint->phyint_illv6, q, mp));
12409 12409 }
12410 12410
12411 12411 /*
12412 12412 * Bring down any IPIF_UP ipifs on ill. If "logical" is B_TRUE, we bring
12413 12413 * down the ipifs without sending DL_UNBIND_REQ to the driver.
12414 12414 */
12415 12415 static void
12416 12416 ill_down_ipifs(ill_t *ill, boolean_t logical)
12417 12417 {
12418 12418 ipif_t *ipif;
12419 12419
12420 12420 ASSERT(IAM_WRITER_ILL(ill));
12421 12421
12422 12422 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
12423 12423 /*
12424 12424 * We go through the ipif_down logic even if the ipif
12425 12425 * is already down, since routes can be added based
12426 12426 * on down ipifs. Going through ipif_down once again
12427 12427 * will delete any IREs created based on these routes.
12428 12428 */
12429 12429 if (ipif->ipif_flags & IPIF_UP)
12430 12430 ipif->ipif_was_up = B_TRUE;
12431 12431
12432 12432 if (logical) {
12433 12433 (void) ipif_logical_down(ipif, NULL, NULL);
12434 12434 ipif_non_duplicate(ipif);
12435 12435 (void) ipif_down_tail(ipif);
12436 12436 } else {
12437 12437 (void) ipif_down(ipif, NULL, NULL);
12438 12438 }
12439 12439 }
12440 12440 }
12441 12441
↓ open down ↓ |
652 lines elided |
↑ open up ↑ |
12442 12442 /*
12443 12443 * Redo source address selection. This makes IXAF_VERIFY_SOURCE take
12444 12444 * a look again at valid source addresses.
12445 12445 * This should be called each time after the set of source addresses has been
12446 12446 * changed.
12447 12447 */
12448 12448 void
12449 12449 ip_update_source_selection(ip_stack_t *ipst)
12450 12450 {
12451 12451 /* We skip past SRC_GENERATION_VERIFY */
12452 - if (atomic_add_32_nv(&ipst->ips_src_generation, 1) ==
12452 + if (atomic_inc_32_nv(&ipst->ips_src_generation) ==
12453 12453 SRC_GENERATION_VERIFY)
12454 - atomic_add_32(&ipst->ips_src_generation, 1);
12454 + atomic_inc_32(&ipst->ips_src_generation);
12455 12455 }
12456 12456
12457 12457 /*
12458 12458 * Finish the group join started in ip_sioctl_groupname().
12459 12459 */
12460 12460 /* ARGSUSED */
12461 12461 static void
12462 12462 ip_join_illgrps(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
12463 12463 {
12464 12464 ill_t *ill = q->q_ptr;
12465 12465 phyint_t *phyi = ill->ill_phyint;
12466 12466 ipmp_grp_t *grp = phyi->phyint_grp;
12467 12467 ip_stack_t *ipst = ill->ill_ipst;
12468 12468
12469 12469 /* IS_UNDER_IPMP() won't work until ipmp_ill_join_illgrp() is called */
12470 12470 ASSERT(!IS_IPMP(ill) && grp != NULL);
12471 12471 ASSERT(IAM_WRITER_IPSQ(ipsq));
12472 12472
12473 12473 if (phyi->phyint_illv4 != NULL) {
12474 12474 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12475 12475 VERIFY(grp->gr_pendv4-- > 0);
12476 12476 rw_exit(&ipst->ips_ipmp_lock);
12477 12477 ipmp_ill_join_illgrp(phyi->phyint_illv4, grp->gr_v4);
12478 12478 }
12479 12479 if (phyi->phyint_illv6 != NULL) {
12480 12480 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12481 12481 VERIFY(grp->gr_pendv6-- > 0);
12482 12482 rw_exit(&ipst->ips_ipmp_lock);
12483 12483 ipmp_ill_join_illgrp(phyi->phyint_illv6, grp->gr_v6);
12484 12484 }
12485 12485 freemsg(mp);
12486 12486 }
12487 12487
12488 12488 /*
12489 12489 * Process an SIOCSLIFGROUPNAME request.
12490 12490 */
12491 12491 /* ARGSUSED */
12492 12492 int
12493 12493 ip_sioctl_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12494 12494 ip_ioctl_cmd_t *ipip, void *ifreq)
12495 12495 {
12496 12496 struct lifreq *lifr = ifreq;
12497 12497 ill_t *ill = ipif->ipif_ill;
12498 12498 ip_stack_t *ipst = ill->ill_ipst;
12499 12499 phyint_t *phyi = ill->ill_phyint;
12500 12500 ipmp_grp_t *grp = phyi->phyint_grp;
12501 12501 mblk_t *ipsq_mp;
12502 12502 int err = 0;
12503 12503
12504 12504 /*
12505 12505 * Note that phyint_grp can only change here, where we're exclusive.
12506 12506 */
12507 12507 ASSERT(IAM_WRITER_ILL(ill));
12508 12508
12509 12509 if (ipif->ipif_id != 0 || ill->ill_usesrc_grp_next != NULL ||
12510 12510 (phyi->phyint_flags & PHYI_VIRTUAL))
12511 12511 return (EINVAL);
12512 12512
12513 12513 lifr->lifr_groupname[LIFGRNAMSIZ - 1] = '\0';
12514 12514
12515 12515 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
12516 12516
12517 12517 /*
12518 12518 * If the name hasn't changed, there's nothing to do.
12519 12519 */
12520 12520 if (grp != NULL && strcmp(grp->gr_name, lifr->lifr_groupname) == 0)
12521 12521 goto unlock;
12522 12522
12523 12523 /*
12524 12524 * Handle requests to rename an IPMP meta-interface.
12525 12525 *
12526 12526 * Note that creation of the IPMP meta-interface is handled in
12527 12527 * userland through the standard plumbing sequence. As part of the
12528 12528 * plumbing the IPMP meta-interface, its initial groupname is set to
12529 12529 * the name of the interface (see ipif_set_values_tail()).
12530 12530 */
12531 12531 if (IS_IPMP(ill)) {
12532 12532 err = ipmp_grp_rename(grp, lifr->lifr_groupname);
12533 12533 goto unlock;
12534 12534 }
12535 12535
12536 12536 /*
12537 12537 * Handle requests to add or remove an IP interface from a group.
12538 12538 */
12539 12539 if (lifr->lifr_groupname[0] != '\0') { /* add */
12540 12540 /*
12541 12541 * Moves are handled by first removing the interface from
12542 12542 * its existing group, and then adding it to another group.
12543 12543 * So, fail if it's already in a group.
12544 12544 */
12545 12545 if (IS_UNDER_IPMP(ill)) {
12546 12546 err = EALREADY;
12547 12547 goto unlock;
12548 12548 }
12549 12549
12550 12550 grp = ipmp_grp_lookup(lifr->lifr_groupname, ipst);
12551 12551 if (grp == NULL) {
12552 12552 err = ENOENT;
12553 12553 goto unlock;
12554 12554 }
12555 12555
12556 12556 /*
12557 12557 * Check if the phyint and its ills are suitable for
12558 12558 * inclusion into the group.
12559 12559 */
12560 12560 if ((err = ipmp_grp_vet_phyint(grp, phyi)) != 0)
12561 12561 goto unlock;
12562 12562
12563 12563 /*
12564 12564 * Checks pass; join the group, and enqueue the remaining
12565 12565 * illgrp joins for when we've become part of the group xop
12566 12566 * and are exclusive across its IPSQs. Since qwriter_ip()
12567 12567 * requires an mblk_t to scribble on, and since `mp' will be
12568 12568 * freed as part of completing the ioctl, allocate another.
12569 12569 */
12570 12570 if ((ipsq_mp = allocb(0, BPRI_MED)) == NULL) {
12571 12571 err = ENOMEM;
12572 12572 goto unlock;
12573 12573 }
12574 12574
12575 12575 /*
12576 12576 * Before we drop ipmp_lock, bump gr_pend* to ensure that the
12577 12577 * IPMP meta-interface ills needed by `phyi' cannot go away
12578 12578 * before ip_join_illgrps() is called back. See the comments
12579 12579 * in ip_sioctl_plink_ipmp() for more.
12580 12580 */
12581 12581 if (phyi->phyint_illv4 != NULL)
12582 12582 grp->gr_pendv4++;
12583 12583 if (phyi->phyint_illv6 != NULL)
12584 12584 grp->gr_pendv6++;
12585 12585
12586 12586 rw_exit(&ipst->ips_ipmp_lock);
12587 12587
12588 12588 ipmp_phyint_join_grp(phyi, grp);
12589 12589 ill_refhold(ill);
12590 12590 qwriter_ip(ill, ill->ill_rq, ipsq_mp, ip_join_illgrps,
12591 12591 SWITCH_OP, B_FALSE);
12592 12592 return (0);
12593 12593 } else {
12594 12594 /*
12595 12595 * Request to remove the interface from a group. If the
12596 12596 * interface is not in a group, this trivially succeeds.
12597 12597 */
12598 12598 rw_exit(&ipst->ips_ipmp_lock);
12599 12599 if (IS_UNDER_IPMP(ill))
12600 12600 ipmp_phyint_leave_grp(phyi);
12601 12601 return (0);
12602 12602 }
12603 12603 unlock:
12604 12604 rw_exit(&ipst->ips_ipmp_lock);
12605 12605 return (err);
12606 12606 }
12607 12607
12608 12608 /*
12609 12609 * Process an SIOCGLIFBINDING request.
12610 12610 */
12611 12611 /* ARGSUSED */
12612 12612 int
12613 12613 ip_sioctl_get_binding(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12614 12614 ip_ioctl_cmd_t *ipip, void *ifreq)
12615 12615 {
12616 12616 ill_t *ill;
12617 12617 struct lifreq *lifr = ifreq;
12618 12618 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
12619 12619
12620 12620 if (!IS_IPMP(ipif->ipif_ill))
12621 12621 return (EINVAL);
12622 12622
12623 12623 rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12624 12624 if ((ill = ipif->ipif_bound_ill) == NULL)
12625 12625 lifr->lifr_binding[0] = '\0';
12626 12626 else
12627 12627 (void) strlcpy(lifr->lifr_binding, ill->ill_name, LIFNAMSIZ);
12628 12628 rw_exit(&ipst->ips_ipmp_lock);
12629 12629 return (0);
12630 12630 }
12631 12631
12632 12632 /*
12633 12633 * Process an SIOCGLIFGROUPNAME request.
12634 12634 */
12635 12635 /* ARGSUSED */
12636 12636 int
12637 12637 ip_sioctl_get_groupname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12638 12638 ip_ioctl_cmd_t *ipip, void *ifreq)
12639 12639 {
12640 12640 ipmp_grp_t *grp;
12641 12641 struct lifreq *lifr = ifreq;
12642 12642 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
12643 12643
12644 12644 rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12645 12645 if ((grp = ipif->ipif_ill->ill_phyint->phyint_grp) == NULL)
12646 12646 lifr->lifr_groupname[0] = '\0';
12647 12647 else
12648 12648 (void) strlcpy(lifr->lifr_groupname, grp->gr_name, LIFGRNAMSIZ);
12649 12649 rw_exit(&ipst->ips_ipmp_lock);
12650 12650 return (0);
12651 12651 }
12652 12652
12653 12653 /*
12654 12654 * Process an SIOCGLIFGROUPINFO request.
12655 12655 */
12656 12656 /* ARGSUSED */
12657 12657 int
12658 12658 ip_sioctl_groupinfo(ipif_t *dummy_ipif, sin_t *sin, queue_t *q, mblk_t *mp,
12659 12659 ip_ioctl_cmd_t *ipip, void *dummy)
12660 12660 {
12661 12661 ipmp_grp_t *grp;
12662 12662 lifgroupinfo_t *lifgr;
12663 12663 ip_stack_t *ipst = CONNQ_TO_IPST(q);
12664 12664
12665 12665 /* ip_wput_nondata() verified mp->b_cont->b_cont */
12666 12666 lifgr = (lifgroupinfo_t *)mp->b_cont->b_cont->b_rptr;
12667 12667 lifgr->gi_grname[LIFGRNAMSIZ - 1] = '\0';
12668 12668
12669 12669 rw_enter(&ipst->ips_ipmp_lock, RW_READER);
12670 12670 if ((grp = ipmp_grp_lookup(lifgr->gi_grname, ipst)) == NULL) {
12671 12671 rw_exit(&ipst->ips_ipmp_lock);
12672 12672 return (ENOENT);
12673 12673 }
12674 12674 ipmp_grp_info(grp, lifgr);
12675 12675 rw_exit(&ipst->ips_ipmp_lock);
12676 12676 return (0);
12677 12677 }
12678 12678
12679 12679 static void
12680 12680 ill_dl_down(ill_t *ill)
12681 12681 {
12682 12682 DTRACE_PROBE2(ill__downup, char *, "ill_dl_down", ill_t *, ill);
12683 12683
12684 12684 /*
12685 12685 * The ill is down; unbind but stay attached since we're still
12686 12686 * associated with a PPA. If we have negotiated DLPI capabilites
12687 12687 * with the data link service provider (IDS_OK) then reset them.
12688 12688 * The interval between unbinding and rebinding is potentially
12689 12689 * unbounded hence we cannot assume things will be the same.
12690 12690 * The DLPI capabilities will be probed again when the data link
12691 12691 * is brought up.
12692 12692 */
12693 12693 mblk_t *mp = ill->ill_unbind_mp;
12694 12694
12695 12695 ip1dbg(("ill_dl_down(%s)\n", ill->ill_name));
12696 12696
12697 12697 if (!ill->ill_replumbing) {
12698 12698 /* Free all ilms for this ill */
12699 12699 update_conn_ill(ill, ill->ill_ipst);
12700 12700 } else {
12701 12701 ill_leave_multicast(ill);
12702 12702 }
12703 12703
12704 12704 ill->ill_unbind_mp = NULL;
12705 12705 if (mp != NULL) {
12706 12706 ip1dbg(("ill_dl_down: %s (%u) for %s\n",
12707 12707 dl_primstr(*(int *)mp->b_rptr), *(int *)mp->b_rptr,
12708 12708 ill->ill_name));
12709 12709 mutex_enter(&ill->ill_lock);
12710 12710 ill->ill_state_flags |= ILL_DL_UNBIND_IN_PROGRESS;
12711 12711 mutex_exit(&ill->ill_lock);
12712 12712 /*
12713 12713 * ip_rput does not pass up normal (M_PROTO) DLPI messages
12714 12714 * after ILL_CONDEMNED is set. So in the unplumb case, we call
12715 12715 * ill_capability_dld_disable disable rightaway. If this is not
12716 12716 * an unplumb operation then the disable happens on receipt of
12717 12717 * the capab ack via ip_rput_dlpi_writer ->
12718 12718 * ill_capability_ack_thr. In both cases the order of
12719 12719 * the operations seen by DLD is capability disable followed
12720 12720 * by DL_UNBIND. Also the DLD capability disable needs a
12721 12721 * cv_wait'able context.
12722 12722 */
12723 12723 if (ill->ill_state_flags & ILL_CONDEMNED)
12724 12724 ill_capability_dld_disable(ill);
12725 12725 ill_capability_reset(ill, B_FALSE);
12726 12726 ill_dlpi_send(ill, mp);
12727 12727 }
12728 12728 mutex_enter(&ill->ill_lock);
12729 12729 ill->ill_dl_up = 0;
12730 12730 ill_nic_event_dispatch(ill, 0, NE_DOWN, NULL, 0);
12731 12731 mutex_exit(&ill->ill_lock);
12732 12732 }
12733 12733
12734 12734 void
12735 12735 ill_dlpi_dispatch(ill_t *ill, mblk_t *mp)
12736 12736 {
12737 12737 union DL_primitives *dlp;
12738 12738 t_uscalar_t prim;
12739 12739 boolean_t waitack = B_FALSE;
12740 12740
12741 12741 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12742 12742
12743 12743 dlp = (union DL_primitives *)mp->b_rptr;
12744 12744 prim = dlp->dl_primitive;
12745 12745
12746 12746 ip1dbg(("ill_dlpi_dispatch: sending %s (%u) to %s\n",
12747 12747 dl_primstr(prim), prim, ill->ill_name));
12748 12748
12749 12749 switch (prim) {
12750 12750 case DL_PHYS_ADDR_REQ:
12751 12751 {
12752 12752 dl_phys_addr_req_t *dlpap = (dl_phys_addr_req_t *)mp->b_rptr;
12753 12753 ill->ill_phys_addr_pend = dlpap->dl_addr_type;
12754 12754 break;
12755 12755 }
12756 12756 case DL_BIND_REQ:
12757 12757 mutex_enter(&ill->ill_lock);
12758 12758 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
12759 12759 mutex_exit(&ill->ill_lock);
12760 12760 break;
12761 12761 }
12762 12762
12763 12763 /*
12764 12764 * Except for the ACKs for the M_PCPROTO messages, all other ACKs
12765 12765 * are dropped by ip_rput() if ILL_CONDEMNED is set. Therefore
12766 12766 * we only wait for the ACK of the DL_UNBIND_REQ.
12767 12767 */
12768 12768 mutex_enter(&ill->ill_lock);
12769 12769 if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
12770 12770 (prim == DL_UNBIND_REQ)) {
12771 12771 ill->ill_dlpi_pending = prim;
12772 12772 waitack = B_TRUE;
12773 12773 }
12774 12774
12775 12775 mutex_exit(&ill->ill_lock);
12776 12776 DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_dispatch",
12777 12777 char *, dl_primstr(prim), ill_t *, ill);
12778 12778 putnext(ill->ill_wq, mp);
12779 12779
12780 12780 /*
12781 12781 * There is no ack for DL_NOTIFY_CONF messages
12782 12782 */
12783 12783 if (waitack && prim == DL_NOTIFY_CONF)
12784 12784 ill_dlpi_done(ill, prim);
12785 12785 }
12786 12786
12787 12787 /*
12788 12788 * Helper function for ill_dlpi_send().
12789 12789 */
12790 12790 /* ARGSUSED */
12791 12791 static void
12792 12792 ill_dlpi_send_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12793 12793 {
12794 12794 ill_dlpi_send(q->q_ptr, mp);
12795 12795 }
12796 12796
12797 12797 /*
12798 12798 * Send a DLPI control message to the driver but make sure there
12799 12799 * is only one outstanding message. Uses ill_dlpi_pending to tell
12800 12800 * when it must queue. ip_rput_dlpi_writer calls ill_dlpi_done()
12801 12801 * when an ACK or a NAK is received to process the next queued message.
12802 12802 */
12803 12803 void
12804 12804 ill_dlpi_send(ill_t *ill, mblk_t *mp)
12805 12805 {
12806 12806 mblk_t **mpp;
12807 12807
12808 12808 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
12809 12809
12810 12810 /*
12811 12811 * To ensure that any DLPI requests for current exclusive operation
12812 12812 * are always completely sent before any DLPI messages for other
12813 12813 * operations, require writer access before enqueuing.
12814 12814 */
12815 12815 if (!IAM_WRITER_ILL(ill)) {
12816 12816 ill_refhold(ill);
12817 12817 /* qwriter_ip() does the ill_refrele() */
12818 12818 qwriter_ip(ill, ill->ill_wq, mp, ill_dlpi_send_writer,
12819 12819 NEW_OP, B_TRUE);
12820 12820 return;
12821 12821 }
12822 12822
12823 12823 mutex_enter(&ill->ill_lock);
12824 12824 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
12825 12825 /* Must queue message. Tail insertion */
12826 12826 mpp = &ill->ill_dlpi_deferred;
12827 12827 while (*mpp != NULL)
12828 12828 mpp = &((*mpp)->b_next);
12829 12829
12830 12830 ip1dbg(("ill_dlpi_send: deferring request for %s "
12831 12831 "while %s pending\n", ill->ill_name,
12832 12832 dl_primstr(ill->ill_dlpi_pending)));
12833 12833
12834 12834 *mpp = mp;
12835 12835 mutex_exit(&ill->ill_lock);
12836 12836 return;
12837 12837 }
12838 12838 mutex_exit(&ill->ill_lock);
12839 12839 ill_dlpi_dispatch(ill, mp);
12840 12840 }
12841 12841
12842 12842 void
12843 12843 ill_capability_send(ill_t *ill, mblk_t *mp)
12844 12844 {
12845 12845 ill->ill_capab_pending_cnt++;
12846 12846 ill_dlpi_send(ill, mp);
12847 12847 }
12848 12848
12849 12849 void
12850 12850 ill_capability_done(ill_t *ill)
12851 12851 {
12852 12852 ASSERT(ill->ill_capab_pending_cnt != 0);
12853 12853
12854 12854 ill_dlpi_done(ill, DL_CAPABILITY_REQ);
12855 12855
12856 12856 ill->ill_capab_pending_cnt--;
12857 12857 if (ill->ill_capab_pending_cnt == 0 &&
12858 12858 ill->ill_dlpi_capab_state == IDCS_OK)
12859 12859 ill_capability_reset_alloc(ill);
12860 12860 }
12861 12861
12862 12862 /*
12863 12863 * Send all deferred DLPI messages without waiting for their ACKs.
12864 12864 */
12865 12865 void
12866 12866 ill_dlpi_send_deferred(ill_t *ill)
12867 12867 {
12868 12868 mblk_t *mp, *nextmp;
12869 12869
12870 12870 /*
12871 12871 * Clear ill_dlpi_pending so that the message is not queued in
12872 12872 * ill_dlpi_send().
12873 12873 */
12874 12874 mutex_enter(&ill->ill_lock);
12875 12875 ill->ill_dlpi_pending = DL_PRIM_INVAL;
12876 12876 mp = ill->ill_dlpi_deferred;
12877 12877 ill->ill_dlpi_deferred = NULL;
12878 12878 mutex_exit(&ill->ill_lock);
12879 12879
12880 12880 for (; mp != NULL; mp = nextmp) {
12881 12881 nextmp = mp->b_next;
12882 12882 mp->b_next = NULL;
12883 12883 ill_dlpi_send(ill, mp);
12884 12884 }
12885 12885 }
12886 12886
12887 12887 /*
12888 12888 * Clear all the deferred DLPI messages. Called on receiving an M_ERROR
12889 12889 * or M_HANGUP
12890 12890 */
12891 12891 static void
12892 12892 ill_dlpi_clear_deferred(ill_t *ill)
12893 12893 {
12894 12894 mblk_t *mp, *nextmp;
12895 12895
12896 12896 mutex_enter(&ill->ill_lock);
12897 12897 ill->ill_dlpi_pending = DL_PRIM_INVAL;
12898 12898 mp = ill->ill_dlpi_deferred;
12899 12899 ill->ill_dlpi_deferred = NULL;
12900 12900 mutex_exit(&ill->ill_lock);
12901 12901
12902 12902 for (; mp != NULL; mp = nextmp) {
12903 12903 nextmp = mp->b_next;
12904 12904 inet_freemsg(mp);
12905 12905 }
12906 12906 }
12907 12907
12908 12908 /*
12909 12909 * Check if the DLPI primitive `prim' is pending; print a warning if not.
12910 12910 */
12911 12911 boolean_t
12912 12912 ill_dlpi_pending(ill_t *ill, t_uscalar_t prim)
12913 12913 {
12914 12914 t_uscalar_t pending;
12915 12915
12916 12916 mutex_enter(&ill->ill_lock);
12917 12917 if (ill->ill_dlpi_pending == prim) {
12918 12918 mutex_exit(&ill->ill_lock);
12919 12919 return (B_TRUE);
12920 12920 }
12921 12921
12922 12922 /*
12923 12923 * During teardown, ill_dlpi_dispatch() will send DLPI requests
12924 12924 * without waiting, so don't print any warnings in that case.
12925 12925 */
12926 12926 if (ill->ill_state_flags & ILL_CONDEMNED) {
12927 12927 mutex_exit(&ill->ill_lock);
12928 12928 return (B_FALSE);
12929 12929 }
12930 12930 pending = ill->ill_dlpi_pending;
12931 12931 mutex_exit(&ill->ill_lock);
12932 12932
12933 12933 if (pending == DL_PRIM_INVAL) {
12934 12934 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12935 12935 "received unsolicited ack for %s on %s\n",
12936 12936 dl_primstr(prim), ill->ill_name);
12937 12937 } else {
12938 12938 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
12939 12939 "received unexpected ack for %s on %s (expecting %s)\n",
12940 12940 dl_primstr(prim), ill->ill_name, dl_primstr(pending));
12941 12941 }
12942 12942 return (B_FALSE);
12943 12943 }
12944 12944
12945 12945 /*
12946 12946 * Complete the current DLPI operation associated with `prim' on `ill' and
12947 12947 * start the next queued DLPI operation (if any). If there are no queued DLPI
12948 12948 * operations and the ill's current exclusive IPSQ operation has finished
12949 12949 * (i.e., ipsq_current_finish() was called), then clear ipsq_current_ipif to
12950 12950 * allow the next exclusive IPSQ operation to begin upon ipsq_exit(). See
12951 12951 * the comments above ipsq_current_finish() for details.
12952 12952 */
12953 12953 void
12954 12954 ill_dlpi_done(ill_t *ill, t_uscalar_t prim)
12955 12955 {
12956 12956 mblk_t *mp;
12957 12957 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
12958 12958 ipxop_t *ipx = ipsq->ipsq_xop;
12959 12959
12960 12960 ASSERT(IAM_WRITER_IPSQ(ipsq));
12961 12961 mutex_enter(&ill->ill_lock);
12962 12962
12963 12963 ASSERT(prim != DL_PRIM_INVAL);
12964 12964 ASSERT(ill->ill_dlpi_pending == prim);
12965 12965
12966 12966 ip1dbg(("ill_dlpi_done: %s has completed %s (%u)\n", ill->ill_name,
12967 12967 dl_primstr(ill->ill_dlpi_pending), ill->ill_dlpi_pending));
12968 12968
12969 12969 if ((mp = ill->ill_dlpi_deferred) == NULL) {
12970 12970 ill->ill_dlpi_pending = DL_PRIM_INVAL;
12971 12971 if (ipx->ipx_current_done) {
12972 12972 mutex_enter(&ipx->ipx_lock);
12973 12973 ipx->ipx_current_ipif = NULL;
12974 12974 mutex_exit(&ipx->ipx_lock);
12975 12975 }
12976 12976 cv_signal(&ill->ill_cv);
12977 12977 mutex_exit(&ill->ill_lock);
12978 12978 return;
12979 12979 }
12980 12980
12981 12981 ill->ill_dlpi_deferred = mp->b_next;
12982 12982 mp->b_next = NULL;
12983 12983 mutex_exit(&ill->ill_lock);
12984 12984
12985 12985 ill_dlpi_dispatch(ill, mp);
12986 12986 }
12987 12987
12988 12988 /*
12989 12989 * Queue a (multicast) DLPI control message to be sent to the driver by
12990 12990 * later calling ill_dlpi_send_queued.
12991 12991 * We queue them while holding a lock (ill_mcast_lock) to ensure that they
12992 12992 * are sent in order i.e., prevent a DL_DISABMULTI_REQ and DL_ENABMULTI_REQ
12993 12993 * for the same group to race.
12994 12994 * We send DLPI control messages in order using ill_lock.
12995 12995 * For IPMP we should be called on the cast_ill.
12996 12996 */
12997 12997 void
12998 12998 ill_dlpi_queue(ill_t *ill, mblk_t *mp)
12999 12999 {
13000 13000 mblk_t **mpp;
13001 13001
13002 13002 ASSERT(DB_TYPE(mp) == M_PROTO || DB_TYPE(mp) == M_PCPROTO);
13003 13003
13004 13004 mutex_enter(&ill->ill_lock);
13005 13005 /* Must queue message. Tail insertion */
13006 13006 mpp = &ill->ill_dlpi_deferred;
13007 13007 while (*mpp != NULL)
13008 13008 mpp = &((*mpp)->b_next);
13009 13009
13010 13010 *mpp = mp;
13011 13011 mutex_exit(&ill->ill_lock);
13012 13012 }
13013 13013
13014 13014 /*
13015 13015 * Send the messages that were queued. Make sure there is only
13016 13016 * one outstanding message. ip_rput_dlpi_writer calls ill_dlpi_done()
13017 13017 * when an ACK or a NAK is received to process the next queued message.
13018 13018 * For IPMP we are called on the upper ill, but when send what is queued
13019 13019 * on the cast_ill.
13020 13020 */
13021 13021 void
13022 13022 ill_dlpi_send_queued(ill_t *ill)
13023 13023 {
13024 13024 mblk_t *mp;
13025 13025 union DL_primitives *dlp;
13026 13026 t_uscalar_t prim;
13027 13027 ill_t *release_ill = NULL;
13028 13028
13029 13029 if (IS_IPMP(ill)) {
13030 13030 /* On the upper IPMP ill. */
13031 13031 release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13032 13032 if (release_ill == NULL) {
13033 13033 /* Avoid ever sending anything down to the ipmpstub */
13034 13034 return;
13035 13035 }
13036 13036 ill = release_ill;
13037 13037 }
13038 13038 mutex_enter(&ill->ill_lock);
13039 13039 while ((mp = ill->ill_dlpi_deferred) != NULL) {
13040 13040 if (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
13041 13041 /* Can't send. Somebody else will send it */
13042 13042 mutex_exit(&ill->ill_lock);
13043 13043 goto done;
13044 13044 }
13045 13045 ill->ill_dlpi_deferred = mp->b_next;
13046 13046 mp->b_next = NULL;
13047 13047 if (!ill->ill_dl_up) {
13048 13048 /*
13049 13049 * Nobody there. All multicast addresses will be
13050 13050 * re-joined when we get the DL_BIND_ACK bringing the
13051 13051 * interface up.
13052 13052 */
13053 13053 freemsg(mp);
13054 13054 continue;
13055 13055 }
13056 13056 dlp = (union DL_primitives *)mp->b_rptr;
13057 13057 prim = dlp->dl_primitive;
13058 13058
13059 13059 if (!(ill->ill_state_flags & ILL_CONDEMNED) ||
13060 13060 (prim == DL_UNBIND_REQ)) {
13061 13061 ill->ill_dlpi_pending = prim;
13062 13062 }
13063 13063 mutex_exit(&ill->ill_lock);
13064 13064
13065 13065 DTRACE_PROBE3(ill__dlpi, char *, "ill_dlpi_send_queued",
13066 13066 char *, dl_primstr(prim), ill_t *, ill);
13067 13067 putnext(ill->ill_wq, mp);
13068 13068 mutex_enter(&ill->ill_lock);
13069 13069 }
13070 13070 mutex_exit(&ill->ill_lock);
13071 13071 done:
13072 13072 if (release_ill != NULL)
13073 13073 ill_refrele(release_ill);
13074 13074 }
13075 13075
13076 13076 /*
13077 13077 * Queue an IP (IGMP/MLD) message to be sent by IP from
13078 13078 * ill_mcast_send_queued
13079 13079 * We queue them while holding a lock (ill_mcast_lock) to ensure that they
13080 13080 * are sent in order i.e., prevent a IGMP leave and IGMP join for the same
13081 13081 * group to race.
13082 13082 * We send them in order using ill_lock.
13083 13083 * For IPMP we are called on the upper ill, but we queue on the cast_ill.
13084 13084 */
13085 13085 void
13086 13086 ill_mcast_queue(ill_t *ill, mblk_t *mp)
13087 13087 {
13088 13088 mblk_t **mpp;
13089 13089 ill_t *release_ill = NULL;
13090 13090
13091 13091 ASSERT(RW_LOCK_HELD(&ill->ill_mcast_lock));
13092 13092
13093 13093 if (IS_IPMP(ill)) {
13094 13094 /* On the upper IPMP ill. */
13095 13095 release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13096 13096 if (release_ill == NULL) {
13097 13097 /* Discard instead of queuing for the ipmp interface */
13098 13098 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13099 13099 ip_drop_output("ipIfStatsOutDiscards - no cast_ill",
13100 13100 mp, ill);
13101 13101 freemsg(mp);
13102 13102 return;
13103 13103 }
13104 13104 ill = release_ill;
13105 13105 }
13106 13106
13107 13107 mutex_enter(&ill->ill_lock);
13108 13108 /* Must queue message. Tail insertion */
13109 13109 mpp = &ill->ill_mcast_deferred;
13110 13110 while (*mpp != NULL)
13111 13111 mpp = &((*mpp)->b_next);
13112 13112
13113 13113 *mpp = mp;
13114 13114 mutex_exit(&ill->ill_lock);
13115 13115 if (release_ill != NULL)
13116 13116 ill_refrele(release_ill);
13117 13117 }
13118 13118
13119 13119 /*
13120 13120 * Send the IP packets that were queued by ill_mcast_queue.
13121 13121 * These are IGMP/MLD packets.
13122 13122 *
13123 13123 * For IPMP we are called on the upper ill, but when send what is queued
13124 13124 * on the cast_ill.
13125 13125 *
13126 13126 * Request loopback of the report if we are acting as a multicast
13127 13127 * router, so that the process-level routing demon can hear it.
13128 13128 * This will run multiple times for the same group if there are members
13129 13129 * on the same group for multiple ipif's on the same ill. The
13130 13130 * igmp_input/mld_input code will suppress this due to the loopback thus we
13131 13131 * always loopback membership report.
13132 13132 *
13133 13133 * We also need to make sure that this does not get load balanced
13134 13134 * by IPMP. We do this by passing an ill to ip_output_simple.
13135 13135 */
13136 13136 void
13137 13137 ill_mcast_send_queued(ill_t *ill)
13138 13138 {
13139 13139 mblk_t *mp;
13140 13140 ip_xmit_attr_t ixas;
13141 13141 ill_t *release_ill = NULL;
13142 13142
13143 13143 if (IS_IPMP(ill)) {
13144 13144 /* On the upper IPMP ill. */
13145 13145 release_ill = ipmp_illgrp_hold_cast_ill(ill->ill_grp);
13146 13146 if (release_ill == NULL) {
13147 13147 /*
13148 13148 * We should have no messages on the ipmp interface
13149 13149 * but no point in trying to send them.
13150 13150 */
13151 13151 return;
13152 13152 }
13153 13153 ill = release_ill;
13154 13154 }
13155 13155 bzero(&ixas, sizeof (ixas));
13156 13156 ixas.ixa_zoneid = ALL_ZONES;
13157 13157 ixas.ixa_cred = kcred;
13158 13158 ixas.ixa_cpid = NOPID;
13159 13159 ixas.ixa_tsl = NULL;
13160 13160 /*
13161 13161 * Here we set ixa_ifindex. If IPMP it will be the lower ill which
13162 13162 * makes ip_select_route pick the IRE_MULTICAST for the cast_ill.
13163 13163 * That is necessary to handle IGMP/MLD snooping switches.
13164 13164 */
13165 13165 ixas.ixa_ifindex = ill->ill_phyint->phyint_ifindex;
13166 13166 ixas.ixa_ipst = ill->ill_ipst;
13167 13167
13168 13168 mutex_enter(&ill->ill_lock);
13169 13169 while ((mp = ill->ill_mcast_deferred) != NULL) {
13170 13170 ill->ill_mcast_deferred = mp->b_next;
13171 13171 mp->b_next = NULL;
13172 13172 if (!ill->ill_dl_up) {
13173 13173 /*
13174 13174 * Nobody there. Just drop the ip packets.
13175 13175 * IGMP/MLD will resend later, if this is a replumb.
13176 13176 */
13177 13177 freemsg(mp);
13178 13178 continue;
13179 13179 }
13180 13180 mutex_enter(&ill->ill_phyint->phyint_lock);
13181 13181 if (IS_UNDER_IPMP(ill) && !ipmp_ill_is_active(ill)) {
13182 13182 /*
13183 13183 * When the ill is getting deactivated, we only want to
13184 13184 * send the DLPI messages, so drop IGMP/MLD packets.
13185 13185 * DLPI messages are handled by ill_dlpi_send_queued()
13186 13186 */
13187 13187 mutex_exit(&ill->ill_phyint->phyint_lock);
13188 13188 freemsg(mp);
13189 13189 continue;
13190 13190 }
13191 13191 mutex_exit(&ill->ill_phyint->phyint_lock);
13192 13192 mutex_exit(&ill->ill_lock);
13193 13193
13194 13194 /* Check whether we are sending IPv4 or IPv6. */
13195 13195 if (ill->ill_isv6) {
13196 13196 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
13197 13197
13198 13198 ixas.ixa_multicast_ttl = ip6h->ip6_hops;
13199 13199 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V6;
13200 13200 } else {
13201 13201 ipha_t *ipha = (ipha_t *)mp->b_rptr;
13202 13202
13203 13203 ixas.ixa_multicast_ttl = ipha->ipha_ttl;
13204 13204 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13205 13205 ixas.ixa_flags &= ~IXAF_SET_ULP_CKSUM;
13206 13206 }
13207 13207 ixas.ixa_flags &= ~IXAF_VERIFY_SOURCE;
13208 13208 ixas.ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_SOURCE;
13209 13209 (void) ip_output_simple(mp, &ixas);
13210 13210 ixa_cleanup(&ixas);
13211 13211
13212 13212 mutex_enter(&ill->ill_lock);
13213 13213 }
13214 13214 mutex_exit(&ill->ill_lock);
13215 13215
13216 13216 done:
13217 13217 if (release_ill != NULL)
13218 13218 ill_refrele(release_ill);
13219 13219 }
13220 13220
13221 13221 /*
13222 13222 * Take down a specific interface, but don't lose any information about it.
13223 13223 * (Always called as writer.)
13224 13224 * This function goes through the down sequence even if the interface is
13225 13225 * already down. There are 2 reasons.
13226 13226 * a. Currently we permit interface routes that depend on down interfaces
13227 13227 * to be added. This behaviour itself is questionable. However it appears
13228 13228 * that both Solaris and 4.3 BSD have exhibited this behaviour for a long
13229 13229 * time. We go thru the cleanup in order to remove these routes.
13230 13230 * b. The bringup of the interface could fail in ill_dl_up i.e. we get
13231 13231 * DL_ERROR_ACK in response to the DL_BIND request. The interface is
13232 13232 * down, but we need to cleanup i.e. do ill_dl_down and
13233 13233 * ip_rput_dlpi_writer (DL_ERROR_ACK) -> ipif_down.
13234 13234 *
13235 13235 * IP-MT notes:
13236 13236 *
13237 13237 * Model of reference to interfaces.
13238 13238 *
13239 13239 * The following members in ipif_t track references to the ipif.
13240 13240 * int ipif_refcnt; Active reference count
13241 13241 *
13242 13242 * The following members in ill_t track references to the ill.
13243 13243 * int ill_refcnt; active refcnt
13244 13244 * uint_t ill_ire_cnt; Number of ires referencing ill
13245 13245 * uint_t ill_ncec_cnt; Number of ncecs referencing ill
13246 13246 * uint_t ill_nce_cnt; Number of nces referencing ill
13247 13247 * uint_t ill_ilm_cnt; Number of ilms referencing ill
13248 13248 *
13249 13249 * Reference to an ipif or ill can be obtained in any of the following ways.
13250 13250 *
13251 13251 * Through the lookup functions ipif_lookup_* / ill_lookup_* functions
13252 13252 * Pointers to ipif / ill from other data structures viz ire and conn.
13253 13253 * Implicit reference to the ipif / ill by holding a reference to the ire.
13254 13254 *
13255 13255 * The ipif/ill lookup functions return a reference held ipif / ill.
13256 13256 * ipif_refcnt and ill_refcnt track the reference counts respectively.
13257 13257 * This is a purely dynamic reference count associated with threads holding
13258 13258 * references to the ipif / ill. Pointers from other structures do not
13259 13259 * count towards this reference count.
13260 13260 *
13261 13261 * ill_ire_cnt is the number of ire's associated with the
13262 13262 * ill. This is incremented whenever a new ire is created referencing the
13263 13263 * ill. This is done atomically inside ire_add_v[46] where the ire is
13264 13264 * actually added to the ire hash table. The count is decremented in
13265 13265 * ire_inactive where the ire is destroyed.
13266 13266 *
13267 13267 * ill_ncec_cnt is the number of ncec's referencing the ill thru ncec_ill.
13268 13268 * This is incremented atomically in
13269 13269 * ndp_add_v4()/ndp_add_v6() where the nce is actually added to the
13270 13270 * table. Similarly it is decremented in ncec_inactive() where the ncec
13271 13271 * is destroyed.
13272 13272 *
13273 13273 * ill_nce_cnt is the number of nce's referencing the ill thru nce_ill. This is
13274 13274 * incremented atomically in nce_add() where the nce is actually added to the
13275 13275 * ill_nce. Similarly it is decremented in nce_inactive() where the nce
13276 13276 * is destroyed.
13277 13277 *
13278 13278 * ill_ilm_cnt is the ilm's reference to the ill. It is incremented in
13279 13279 * ilm_add() and decremented before the ilm is freed in ilm_delete().
13280 13280 *
13281 13281 * Flow of ioctls involving interface down/up
13282 13282 *
13283 13283 * The following is the sequence of an attempt to set some critical flags on an
13284 13284 * up interface.
13285 13285 * ip_sioctl_flags
13286 13286 * ipif_down
13287 13287 * wait for ipif to be quiescent
13288 13288 * ipif_down_tail
13289 13289 * ip_sioctl_flags_tail
13290 13290 *
13291 13291 * All set ioctls that involve down/up sequence would have a skeleton similar
13292 13292 * to the above. All the *tail functions are called after the refcounts have
13293 13293 * dropped to the appropriate values.
13294 13294 *
13295 13295 * SIOC ioctls during the IPIF_CHANGING interval.
13296 13296 *
13297 13297 * Threads handling SIOC set ioctls serialize on the squeue, but this
13298 13298 * is not done for SIOC get ioctls. Since a set ioctl can cause several
13299 13299 * steps of internal changes to the state, some of which are visible in
13300 13300 * ipif_flags (such as IFF_UP being cleared and later set), and we want
13301 13301 * the set ioctl to be atomic related to the get ioctls, the SIOC get code
13302 13302 * will wait and restart ioctls if IPIF_CHANGING is set. The mblk is then
13303 13303 * enqueued in the ipsq and the operation is restarted by ipsq_exit() when
13304 13304 * the current exclusive operation completes. The IPIF_CHANGING check
13305 13305 * and enqueue is atomic using the ill_lock and ipsq_lock. The
13306 13306 * lookup is done holding the ill_lock. Hence the ill/ipif state flags can't
13307 13307 * change while the ill_lock is held. Before dropping the ill_lock we acquire
13308 13308 * the ipsq_lock and call ipsq_enq. This ensures that ipsq_exit can't finish
13309 13309 * until we release the ipsq_lock, even though the ill/ipif state flags
13310 13310 * can change after we drop the ill_lock.
13311 13311 */
13312 13312 int
13313 13313 ipif_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13314 13314 {
13315 13315 ill_t *ill = ipif->ipif_ill;
13316 13316 conn_t *connp;
13317 13317 boolean_t success;
13318 13318 boolean_t ipif_was_up = B_FALSE;
13319 13319 ip_stack_t *ipst = ill->ill_ipst;
13320 13320
13321 13321 ASSERT(IAM_WRITER_IPIF(ipif));
13322 13322
13323 13323 ip1dbg(("ipif_down(%s:%u)\n", ill->ill_name, ipif->ipif_id));
13324 13324
13325 13325 DTRACE_PROBE3(ipif__downup, char *, "ipif_down",
13326 13326 ill_t *, ill, ipif_t *, ipif);
13327 13327
13328 13328 if (ipif->ipif_flags & IPIF_UP) {
13329 13329 mutex_enter(&ill->ill_lock);
13330 13330 ipif->ipif_flags &= ~IPIF_UP;
13331 13331 ASSERT(ill->ill_ipif_up_count > 0);
13332 13332 --ill->ill_ipif_up_count;
13333 13333 mutex_exit(&ill->ill_lock);
13334 13334 ipif_was_up = B_TRUE;
13335 13335 /* Update status in SCTP's list */
13336 13336 sctp_update_ipif(ipif, SCTP_IPIF_DOWN);
13337 13337 ill_nic_event_dispatch(ipif->ipif_ill,
13338 13338 MAP_IPIF_ID(ipif->ipif_id), NE_LIF_DOWN, NULL, 0);
13339 13339 }
13340 13340
13341 13341 /*
13342 13342 * Removal of the last ipif from an ill may result in a DL_UNBIND
13343 13343 * being sent to the driver, and we must not send any data packets to
13344 13344 * the driver after the DL_UNBIND_REQ. To ensure this, all the
13345 13345 * ire and nce entries used in the data path will be cleaned
13346 13346 * up, and we also set the ILL_DOWN_IN_PROGRESS bit to make
13347 13347 * sure on new entries will be added until the ill is bound
13348 13348 * again. The ILL_DOWN_IN_PROGRESS bit is turned off upon
13349 13349 * receipt of a DL_BIND_ACK.
13350 13350 */
13351 13351 if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13352 13352 ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13353 13353 ill->ill_dl_up) {
13354 13354 ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
13355 13355 }
13356 13356
13357 13357 /*
13358 13358 * Blow away memberships we established in ipif_multicast_up().
13359 13359 */
13360 13360 ipif_multicast_down(ipif);
13361 13361
13362 13362 /*
13363 13363 * Remove from the mapping for __sin6_src_id. We insert only
13364 13364 * when the address is not INADDR_ANY. As IPv4 addresses are
13365 13365 * stored as mapped addresses, we need to check for mapped
13366 13366 * INADDR_ANY also.
13367 13367 */
13368 13368 if (ipif_was_up && !IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) &&
13369 13369 !IN6_IS_ADDR_V4MAPPED_ANY(&ipif->ipif_v6lcl_addr) &&
13370 13370 !(ipif->ipif_flags & IPIF_NOLOCAL)) {
13371 13371 int err;
13372 13372
13373 13373 err = ip_srcid_remove(&ipif->ipif_v6lcl_addr,
13374 13374 ipif->ipif_zoneid, ipst);
13375 13375 if (err != 0) {
13376 13376 ip0dbg(("ipif_down: srcid_remove %d\n", err));
13377 13377 }
13378 13378 }
13379 13379
13380 13380 if (ipif_was_up) {
13381 13381 /* only delete if we'd added ire's before */
13382 13382 if (ipif->ipif_isv6)
13383 13383 ipif_delete_ires_v6(ipif);
13384 13384 else
13385 13385 ipif_delete_ires_v4(ipif);
13386 13386 }
13387 13387
13388 13388 if (ipif_was_up && ill->ill_ipif_up_count == 0) {
13389 13389 /*
13390 13390 * Since the interface is now down, it may have just become
13391 13391 * inactive. Note that this needs to be done even for a
13392 13392 * lll_logical_down(), or ARP entries will not get correctly
13393 13393 * restored when the interface comes back up.
13394 13394 */
13395 13395 if (IS_UNDER_IPMP(ill))
13396 13396 ipmp_ill_refresh_active(ill);
13397 13397 }
13398 13398
13399 13399 /*
13400 13400 * neighbor-discovery or arp entries for this interface. The ipif
13401 13401 * has to be quiesced, so we walk all the nce's and delete those
13402 13402 * that point at the ipif->ipif_ill. At the same time, we also
13403 13403 * update IPMP so that ipifs for data addresses are unbound. We dont
13404 13404 * call ipif_arp_down to DL_UNBIND the arp stream itself here, but defer
13405 13405 * that for ipif_down_tail()
13406 13406 */
13407 13407 ipif_nce_down(ipif);
13408 13408
13409 13409 /*
13410 13410 * If this is the last ipif on the ill, we also need to remove
13411 13411 * any IREs with ire_ill set. Otherwise ipif_is_quiescent() will
13412 13412 * never succeed.
13413 13413 */
13414 13414 if (ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0)
13415 13415 ire_walk_ill(0, 0, ill_downi, ill, ill);
13416 13416
13417 13417 /*
13418 13418 * Walk all CONNs that can have a reference on an ire for this
13419 13419 * ipif (we actually walk all that now have stale references).
13420 13420 */
13421 13421 ipcl_walk(conn_ixa_cleanup, (void *)B_TRUE, ipst);
13422 13422
13423 13423 /*
13424 13424 * If mp is NULL the caller will wait for the appropriate refcnt.
13425 13425 * Eg. ip_sioctl_removeif -> ipif_free -> ipif_down
13426 13426 * and ill_delete -> ipif_free -> ipif_down
13427 13427 */
13428 13428 if (mp == NULL) {
13429 13429 ASSERT(q == NULL);
13430 13430 return (0);
13431 13431 }
13432 13432
13433 13433 if (CONN_Q(q)) {
13434 13434 connp = Q_TO_CONN(q);
13435 13435 mutex_enter(&connp->conn_lock);
13436 13436 } else {
13437 13437 connp = NULL;
13438 13438 }
13439 13439 mutex_enter(&ill->ill_lock);
13440 13440 /*
13441 13441 * Are there any ire's pointing to this ipif that are still active ?
13442 13442 * If this is the last ipif going down, are there any ire's pointing
13443 13443 * to this ill that are still active ?
13444 13444 */
13445 13445 if (ipif_is_quiescent(ipif)) {
13446 13446 mutex_exit(&ill->ill_lock);
13447 13447 if (connp != NULL)
13448 13448 mutex_exit(&connp->conn_lock);
13449 13449 return (0);
13450 13450 }
13451 13451
13452 13452 ip1dbg(("ipif_down: need to wait, adding pending mp %s ill %p",
13453 13453 ill->ill_name, (void *)ill));
13454 13454 /*
13455 13455 * Enqueue the mp atomically in ipsq_pending_mp. When the refcount
13456 13456 * drops down, the operation will be restarted by ipif_ill_refrele_tail
13457 13457 * which in turn is called by the last refrele on the ipif/ill/ire.
13458 13458 */
13459 13459 success = ipsq_pending_mp_add(connp, ipif, q, mp, IPIF_DOWN);
13460 13460 if (!success) {
13461 13461 /* The conn is closing. So just return */
13462 13462 ASSERT(connp != NULL);
13463 13463 mutex_exit(&ill->ill_lock);
13464 13464 mutex_exit(&connp->conn_lock);
13465 13465 return (EINTR);
13466 13466 }
13467 13467
13468 13468 mutex_exit(&ill->ill_lock);
13469 13469 if (connp != NULL)
13470 13470 mutex_exit(&connp->conn_lock);
13471 13471 return (EINPROGRESS);
13472 13472 }
13473 13473
13474 13474 int
13475 13475 ipif_down_tail(ipif_t *ipif)
13476 13476 {
13477 13477 ill_t *ill = ipif->ipif_ill;
13478 13478 int err = 0;
13479 13479
13480 13480 DTRACE_PROBE3(ipif__downup, char *, "ipif_down_tail",
13481 13481 ill_t *, ill, ipif_t *, ipif);
13482 13482
13483 13483 /*
13484 13484 * Skip any loopback interface (null wq).
13485 13485 * If this is the last logical interface on the ill
13486 13486 * have ill_dl_down tell the driver we are gone (unbind)
13487 13487 * Note that lun 0 can ipif_down even though
13488 13488 * there are other logical units that are up.
13489 13489 * This occurs e.g. when we change a "significant" IFF_ flag.
13490 13490 */
13491 13491 if (ill->ill_wq != NULL && !ill->ill_logical_down &&
13492 13492 ill->ill_ipif_up_count == 0 && ill->ill_ipif_dup_count == 0 &&
13493 13493 ill->ill_dl_up) {
13494 13494 ill_dl_down(ill);
13495 13495 }
13496 13496 if (!ipif->ipif_isv6)
13497 13497 err = ipif_arp_down(ipif);
13498 13498
13499 13499 ill->ill_logical_down = 0;
13500 13500
13501 13501 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
13502 13502 ip_rts_newaddrmsg(RTM_DELETE, 0, ipif, RTSQ_DEFAULT);
13503 13503 return (err);
13504 13504 }
13505 13505
13506 13506 /*
13507 13507 * Bring interface logically down without bringing the physical interface
13508 13508 * down e.g. when the netmask is changed. This avoids long lasting link
13509 13509 * negotiations between an ethernet interface and a certain switches.
13510 13510 */
13511 13511 static int
13512 13512 ipif_logical_down(ipif_t *ipif, queue_t *q, mblk_t *mp)
13513 13513 {
13514 13514 DTRACE_PROBE3(ipif__downup, char *, "ipif_logical_down",
13515 13515 ill_t *, ipif->ipif_ill, ipif_t *, ipif);
13516 13516
13517 13517 /*
13518 13518 * The ill_logical_down flag is a transient flag. It is set here
13519 13519 * and is cleared once the down has completed in ipif_down_tail.
13520 13520 * This flag does not indicate whether the ill stream is in the
13521 13521 * DL_BOUND state with the driver. Instead this flag is used by
13522 13522 * ipif_down_tail to determine whether to DL_UNBIND the stream with
13523 13523 * the driver. The state of the ill stream i.e. whether it is
13524 13524 * DL_BOUND with the driver or not is indicated by the ill_dl_up flag.
13525 13525 */
13526 13526 ipif->ipif_ill->ill_logical_down = 1;
13527 13527 return (ipif_down(ipif, q, mp));
13528 13528 }
13529 13529
13530 13530 /*
13531 13531 * Initiate deallocate of an IPIF. Always called as writer. Called by
13532 13532 * ill_delete or ip_sioctl_removeif.
13533 13533 */
13534 13534 static void
13535 13535 ipif_free(ipif_t *ipif)
13536 13536 {
13537 13537 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13538 13538
13539 13539 ASSERT(IAM_WRITER_IPIF(ipif));
13540 13540
13541 13541 if (ipif->ipif_recovery_id != 0)
13542 13542 (void) untimeout(ipif->ipif_recovery_id);
13543 13543 ipif->ipif_recovery_id = 0;
13544 13544
13545 13545 /*
13546 13546 * Take down the interface. We can be called either from ill_delete
13547 13547 * or from ip_sioctl_removeif.
13548 13548 */
13549 13549 (void) ipif_down(ipif, NULL, NULL);
13550 13550
13551 13551 /*
13552 13552 * Now that the interface is down, there's no chance it can still
13553 13553 * become a duplicate. Cancel any timer that may have been set while
13554 13554 * tearing down.
13555 13555 */
13556 13556 if (ipif->ipif_recovery_id != 0)
13557 13557 (void) untimeout(ipif->ipif_recovery_id);
13558 13558 ipif->ipif_recovery_id = 0;
13559 13559
13560 13560 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13561 13561 /* Remove pointers to this ill in the multicast routing tables */
13562 13562 reset_mrt_vif_ipif(ipif);
13563 13563 /* If necessary, clear the cached source ipif rotor. */
13564 13564 if (ipif->ipif_ill->ill_src_ipif == ipif)
13565 13565 ipif->ipif_ill->ill_src_ipif = NULL;
13566 13566 rw_exit(&ipst->ips_ill_g_lock);
13567 13567 }
13568 13568
13569 13569 static void
13570 13570 ipif_free_tail(ipif_t *ipif)
13571 13571 {
13572 13572 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13573 13573
13574 13574 /*
13575 13575 * Need to hold both ill_g_lock and ill_lock while
13576 13576 * inserting or removing an ipif from the linked list
13577 13577 * of ipifs hanging off the ill.
13578 13578 */
13579 13579 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
13580 13580
13581 13581 #ifdef DEBUG
13582 13582 ipif_trace_cleanup(ipif);
13583 13583 #endif
13584 13584
13585 13585 /* Ask SCTP to take it out of it list */
13586 13586 sctp_update_ipif(ipif, SCTP_IPIF_REMOVE);
13587 13587 ip_rts_newaddrmsg(RTM_FREEADDR, 0, ipif, RTSQ_DEFAULT);
13588 13588
13589 13589 /* Get it out of the ILL interface list. */
13590 13590 ipif_remove(ipif);
13591 13591 rw_exit(&ipst->ips_ill_g_lock);
13592 13592
13593 13593 ASSERT(!(ipif->ipif_flags & (IPIF_UP | IPIF_DUPLICATE)));
13594 13594 ASSERT(ipif->ipif_recovery_id == 0);
13595 13595 ASSERT(ipif->ipif_ire_local == NULL);
13596 13596 ASSERT(ipif->ipif_ire_if == NULL);
13597 13597
13598 13598 /* Free the memory. */
13599 13599 mi_free(ipif);
13600 13600 }
13601 13601
13602 13602 /*
13603 13603 * Sets `buf' to an ipif name of the form "ill_name:id", or "ill_name" if "id"
13604 13604 * is zero.
13605 13605 */
13606 13606 void
13607 13607 ipif_get_name(const ipif_t *ipif, char *buf, int len)
13608 13608 {
13609 13609 char lbuf[LIFNAMSIZ];
13610 13610 char *name;
13611 13611 size_t name_len;
13612 13612
13613 13613 buf[0] = '\0';
13614 13614 name = ipif->ipif_ill->ill_name;
13615 13615 name_len = ipif->ipif_ill->ill_name_length;
13616 13616 if (ipif->ipif_id != 0) {
13617 13617 (void) sprintf(lbuf, "%s%c%d", name, IPIF_SEPARATOR_CHAR,
13618 13618 ipif->ipif_id);
13619 13619 name = lbuf;
13620 13620 name_len = mi_strlen(name) + 1;
13621 13621 }
13622 13622 len -= 1;
13623 13623 buf[len] = '\0';
13624 13624 len = MIN(len, name_len);
13625 13625 bcopy(name, buf, len);
13626 13626 }
13627 13627
13628 13628 /*
13629 13629 * Sets `buf' to an ill name.
13630 13630 */
13631 13631 void
13632 13632 ill_get_name(const ill_t *ill, char *buf, int len)
13633 13633 {
13634 13634 char *name;
13635 13635 size_t name_len;
13636 13636
13637 13637 name = ill->ill_name;
13638 13638 name_len = ill->ill_name_length;
13639 13639 len -= 1;
13640 13640 buf[len] = '\0';
13641 13641 len = MIN(len, name_len);
13642 13642 bcopy(name, buf, len);
13643 13643 }
13644 13644
13645 13645 /*
13646 13646 * Find an IPIF based on the name passed in. Names can be of the form <phys>
13647 13647 * (e.g., le0) or <phys>:<#> (e.g., le0:1). When there is no colon, the
13648 13648 * implied unit id is zero. <phys> must correspond to the name of an ILL.
13649 13649 * (May be called as writer.)
13650 13650 */
13651 13651 static ipif_t *
13652 13652 ipif_lookup_on_name(char *name, size_t namelen, boolean_t do_alloc,
13653 13653 boolean_t *exists, boolean_t isv6, zoneid_t zoneid, ip_stack_t *ipst)
13654 13654 {
13655 13655 char *cp;
13656 13656 char *endp;
13657 13657 long id;
13658 13658 ill_t *ill;
13659 13659 ipif_t *ipif;
13660 13660 uint_t ire_type;
13661 13661 boolean_t did_alloc = B_FALSE;
13662 13662 char last;
13663 13663
13664 13664 /*
13665 13665 * If the caller wants to us to create the ipif, make sure we have a
13666 13666 * valid zoneid
13667 13667 */
13668 13668 ASSERT(!do_alloc || zoneid != ALL_ZONES);
13669 13669
13670 13670 if (namelen == 0) {
13671 13671 return (NULL);
13672 13672 }
13673 13673
13674 13674 *exists = B_FALSE;
13675 13675 /* Look for a colon in the name. */
13676 13676 endp = &name[namelen];
13677 13677 for (cp = endp; --cp > name; ) {
13678 13678 if (*cp == IPIF_SEPARATOR_CHAR)
13679 13679 break;
13680 13680 }
13681 13681
13682 13682 if (*cp == IPIF_SEPARATOR_CHAR) {
13683 13683 /*
13684 13684 * Reject any non-decimal aliases for logical
13685 13685 * interfaces. Aliases with leading zeroes
13686 13686 * are also rejected as they introduce ambiguity
13687 13687 * in the naming of the interfaces.
13688 13688 * In order to confirm with existing semantics,
13689 13689 * and to not break any programs/script relying
13690 13690 * on that behaviour, if<0>:0 is considered to be
13691 13691 * a valid interface.
13692 13692 *
13693 13693 * If alias has two or more digits and the first
13694 13694 * is zero, fail.
13695 13695 */
13696 13696 if (&cp[2] < endp && cp[1] == '0') {
13697 13697 return (NULL);
13698 13698 }
13699 13699 }
13700 13700
13701 13701 if (cp <= name) {
13702 13702 cp = endp;
13703 13703 }
13704 13704 last = *cp;
13705 13705 *cp = '\0';
13706 13706
13707 13707 /*
13708 13708 * Look up the ILL, based on the portion of the name
13709 13709 * before the slash. ill_lookup_on_name returns a held ill.
13710 13710 * Temporary to check whether ill exists already. If so
13711 13711 * ill_lookup_on_name will clear it.
13712 13712 */
13713 13713 ill = ill_lookup_on_name(name, do_alloc, isv6,
13714 13714 &did_alloc, ipst);
13715 13715 *cp = last;
13716 13716 if (ill == NULL)
13717 13717 return (NULL);
13718 13718
13719 13719 /* Establish the unit number in the name. */
13720 13720 id = 0;
13721 13721 if (cp < endp && *endp == '\0') {
13722 13722 /* If there was a colon, the unit number follows. */
13723 13723 cp++;
13724 13724 if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13725 13725 ill_refrele(ill);
13726 13726 return (NULL);
13727 13727 }
13728 13728 }
13729 13729
13730 13730 mutex_enter(&ill->ill_lock);
13731 13731 /* Now see if there is an IPIF with this unit number. */
13732 13732 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13733 13733 if (ipif->ipif_id == id) {
13734 13734 if (zoneid != ALL_ZONES &&
13735 13735 zoneid != ipif->ipif_zoneid &&
13736 13736 ipif->ipif_zoneid != ALL_ZONES) {
13737 13737 mutex_exit(&ill->ill_lock);
13738 13738 ill_refrele(ill);
13739 13739 return (NULL);
13740 13740 }
13741 13741 if (IPIF_CAN_LOOKUP(ipif)) {
13742 13742 ipif_refhold_locked(ipif);
13743 13743 mutex_exit(&ill->ill_lock);
13744 13744 if (!did_alloc)
13745 13745 *exists = B_TRUE;
13746 13746 /*
13747 13747 * Drop locks before calling ill_refrele
13748 13748 * since it can potentially call into
13749 13749 * ipif_ill_refrele_tail which can end up
13750 13750 * in trying to acquire any lock.
13751 13751 */
13752 13752 ill_refrele(ill);
13753 13753 return (ipif);
13754 13754 }
13755 13755 }
13756 13756 }
13757 13757
13758 13758 if (!do_alloc) {
13759 13759 mutex_exit(&ill->ill_lock);
13760 13760 ill_refrele(ill);
13761 13761 return (NULL);
13762 13762 }
13763 13763
13764 13764 /*
13765 13765 * If none found, atomically allocate and return a new one.
13766 13766 * Historically, we used IRE_LOOPBACK only for lun 0, and IRE_LOCAL
13767 13767 * to support "receive only" use of lo0:1 etc. as is still done
13768 13768 * below as an initial guess.
13769 13769 * However, this is now likely to be overriden later in ipif_up_done()
13770 13770 * when we know for sure what address has been configured on the
13771 13771 * interface, since we might have more than one loopback interface
13772 13772 * with a loopback address, e.g. in the case of zones, and all the
13773 13773 * interfaces with loopback addresses need to be marked IRE_LOOPBACK.
13774 13774 */
13775 13775 if (ill->ill_net_type == IRE_LOOPBACK && id == 0)
13776 13776 ire_type = IRE_LOOPBACK;
13777 13777 else
13778 13778 ire_type = IRE_LOCAL;
13779 13779 ipif = ipif_allocate(ill, id, ire_type, B_TRUE, B_TRUE, NULL);
13780 13780 if (ipif != NULL)
13781 13781 ipif_refhold_locked(ipif);
13782 13782 mutex_exit(&ill->ill_lock);
13783 13783 ill_refrele(ill);
13784 13784 return (ipif);
13785 13785 }
13786 13786
13787 13787 /*
13788 13788 * Variant of the above that queues the request on the ipsq when
13789 13789 * IPIF_CHANGING is set.
13790 13790 */
13791 13791 static ipif_t *
13792 13792 ipif_lookup_on_name_async(char *name, size_t namelen, boolean_t isv6,
13793 13793 zoneid_t zoneid, queue_t *q, mblk_t *mp, ipsq_func_t func, int *error,
13794 13794 ip_stack_t *ipst)
13795 13795 {
13796 13796 char *cp;
13797 13797 char *endp;
13798 13798 long id;
13799 13799 ill_t *ill;
13800 13800 ipif_t *ipif;
13801 13801 boolean_t did_alloc = B_FALSE;
13802 13802 ipsq_t *ipsq;
13803 13803
13804 13804 if (error != NULL)
13805 13805 *error = 0;
13806 13806
13807 13807 if (namelen == 0) {
13808 13808 if (error != NULL)
13809 13809 *error = ENXIO;
13810 13810 return (NULL);
13811 13811 }
13812 13812
13813 13813 /* Look for a colon in the name. */
13814 13814 endp = &name[namelen];
13815 13815 for (cp = endp; --cp > name; ) {
13816 13816 if (*cp == IPIF_SEPARATOR_CHAR)
13817 13817 break;
13818 13818 }
13819 13819
13820 13820 if (*cp == IPIF_SEPARATOR_CHAR) {
13821 13821 /*
13822 13822 * Reject any non-decimal aliases for logical
13823 13823 * interfaces. Aliases with leading zeroes
13824 13824 * are also rejected as they introduce ambiguity
13825 13825 * in the naming of the interfaces.
13826 13826 * In order to confirm with existing semantics,
13827 13827 * and to not break any programs/script relying
13828 13828 * on that behaviour, if<0>:0 is considered to be
13829 13829 * a valid interface.
13830 13830 *
13831 13831 * If alias has two or more digits and the first
13832 13832 * is zero, fail.
13833 13833 */
13834 13834 if (&cp[2] < endp && cp[1] == '0') {
13835 13835 if (error != NULL)
13836 13836 *error = EINVAL;
13837 13837 return (NULL);
13838 13838 }
13839 13839 }
13840 13840
13841 13841 if (cp <= name) {
13842 13842 cp = endp;
13843 13843 } else {
13844 13844 *cp = '\0';
13845 13845 }
13846 13846
13847 13847 /*
13848 13848 * Look up the ILL, based on the portion of the name
13849 13849 * before the slash. ill_lookup_on_name returns a held ill.
13850 13850 * Temporary to check whether ill exists already. If so
13851 13851 * ill_lookup_on_name will clear it.
13852 13852 */
13853 13853 ill = ill_lookup_on_name(name, B_FALSE, isv6, &did_alloc, ipst);
13854 13854 if (cp != endp)
13855 13855 *cp = IPIF_SEPARATOR_CHAR;
13856 13856 if (ill == NULL)
13857 13857 return (NULL);
13858 13858
13859 13859 /* Establish the unit number in the name. */
13860 13860 id = 0;
13861 13861 if (cp < endp && *endp == '\0') {
13862 13862 /* If there was a colon, the unit number follows. */
13863 13863 cp++;
13864 13864 if (ddi_strtol(cp, NULL, 0, &id) != 0) {
13865 13865 ill_refrele(ill);
13866 13866 if (error != NULL)
13867 13867 *error = ENXIO;
13868 13868 return (NULL);
13869 13869 }
13870 13870 }
13871 13871
13872 13872 GRAB_CONN_LOCK(q);
13873 13873 mutex_enter(&ill->ill_lock);
13874 13874 /* Now see if there is an IPIF with this unit number. */
13875 13875 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
13876 13876 if (ipif->ipif_id == id) {
13877 13877 if (zoneid != ALL_ZONES &&
13878 13878 zoneid != ipif->ipif_zoneid &&
13879 13879 ipif->ipif_zoneid != ALL_ZONES) {
13880 13880 mutex_exit(&ill->ill_lock);
13881 13881 RELEASE_CONN_LOCK(q);
13882 13882 ill_refrele(ill);
13883 13883 if (error != NULL)
13884 13884 *error = ENXIO;
13885 13885 return (NULL);
13886 13886 }
13887 13887
13888 13888 if (!(IPIF_IS_CHANGING(ipif) ||
13889 13889 IPIF_IS_CONDEMNED(ipif)) ||
13890 13890 IAM_WRITER_IPIF(ipif)) {
13891 13891 ipif_refhold_locked(ipif);
13892 13892 mutex_exit(&ill->ill_lock);
13893 13893 /*
13894 13894 * Drop locks before calling ill_refrele
13895 13895 * since it can potentially call into
13896 13896 * ipif_ill_refrele_tail which can end up
13897 13897 * in trying to acquire any lock.
13898 13898 */
13899 13899 RELEASE_CONN_LOCK(q);
13900 13900 ill_refrele(ill);
13901 13901 return (ipif);
13902 13902 } else if (q != NULL && !IPIF_IS_CONDEMNED(ipif)) {
13903 13903 ipsq = ill->ill_phyint->phyint_ipsq;
13904 13904 mutex_enter(&ipsq->ipsq_lock);
13905 13905 mutex_enter(&ipsq->ipsq_xop->ipx_lock);
13906 13906 mutex_exit(&ill->ill_lock);
13907 13907 ipsq_enq(ipsq, q, mp, func, NEW_OP, ill);
13908 13908 mutex_exit(&ipsq->ipsq_xop->ipx_lock);
13909 13909 mutex_exit(&ipsq->ipsq_lock);
13910 13910 RELEASE_CONN_LOCK(q);
13911 13911 ill_refrele(ill);
13912 13912 if (error != NULL)
13913 13913 *error = EINPROGRESS;
13914 13914 return (NULL);
13915 13915 }
13916 13916 }
13917 13917 }
13918 13918 RELEASE_CONN_LOCK(q);
13919 13919 mutex_exit(&ill->ill_lock);
13920 13920 ill_refrele(ill);
13921 13921 if (error != NULL)
13922 13922 *error = ENXIO;
13923 13923 return (NULL);
13924 13924 }
13925 13925
13926 13926 /*
13927 13927 * This routine is called whenever a new address comes up on an ipif. If
13928 13928 * we are configured to respond to address mask requests, then we are supposed
13929 13929 * to broadcast an address mask reply at this time. This routine is also
13930 13930 * called if we are already up, but a netmask change is made. This is legal
13931 13931 * but might not make the system manager very popular. (May be called
13932 13932 * as writer.)
13933 13933 */
13934 13934 void
13935 13935 ipif_mask_reply(ipif_t *ipif)
13936 13936 {
13937 13937 icmph_t *icmph;
13938 13938 ipha_t *ipha;
13939 13939 mblk_t *mp;
13940 13940 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
13941 13941 ip_xmit_attr_t ixas;
13942 13942
13943 13943 #define REPLY_LEN (sizeof (icmp_ipha) + sizeof (icmph_t) + IP_ADDR_LEN)
13944 13944
13945 13945 if (!ipst->ips_ip_respond_to_address_mask_broadcast)
13946 13946 return;
13947 13947
13948 13948 /* ICMP mask reply is IPv4 only */
13949 13949 ASSERT(!ipif->ipif_isv6);
13950 13950 /* ICMP mask reply is not for a loopback interface */
13951 13951 ASSERT(ipif->ipif_ill->ill_wq != NULL);
13952 13952
13953 13953 if (ipif->ipif_lcl_addr == INADDR_ANY)
13954 13954 return;
13955 13955
13956 13956 mp = allocb(REPLY_LEN, BPRI_HI);
13957 13957 if (mp == NULL)
13958 13958 return;
13959 13959 mp->b_wptr = mp->b_rptr + REPLY_LEN;
13960 13960
13961 13961 ipha = (ipha_t *)mp->b_rptr;
13962 13962 bzero(ipha, REPLY_LEN);
13963 13963 *ipha = icmp_ipha;
13964 13964 ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl;
13965 13965 ipha->ipha_src = ipif->ipif_lcl_addr;
13966 13966 ipha->ipha_dst = ipif->ipif_brd_addr;
13967 13967 ipha->ipha_length = htons(REPLY_LEN);
13968 13968 ipha->ipha_ident = 0;
13969 13969
13970 13970 icmph = (icmph_t *)&ipha[1];
13971 13971 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
13972 13972 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
13973 13973 icmph->icmph_checksum = IP_CSUM(mp, sizeof (ipha_t), 0);
13974 13974
13975 13975 bzero(&ixas, sizeof (ixas));
13976 13976 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
13977 13977 ixas.ixa_zoneid = ALL_ZONES;
13978 13978 ixas.ixa_ifindex = 0;
13979 13979 ixas.ixa_ipst = ipst;
13980 13980 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
13981 13981 (void) ip_output_simple(mp, &ixas);
13982 13982 ixa_cleanup(&ixas);
13983 13983 #undef REPLY_LEN
13984 13984 }
13985 13985
13986 13986 /*
13987 13987 * Join the ipif specific multicast groups.
13988 13988 * Must be called after a mapping has been set up in the resolver. (Always
13989 13989 * called as writer.)
13990 13990 */
13991 13991 void
13992 13992 ipif_multicast_up(ipif_t *ipif)
13993 13993 {
13994 13994 int err;
13995 13995 ill_t *ill;
13996 13996 ilm_t *ilm;
13997 13997
13998 13998 ASSERT(IAM_WRITER_IPIF(ipif));
13999 13999
14000 14000 ill = ipif->ipif_ill;
14001 14001
14002 14002 ip1dbg(("ipif_multicast_up\n"));
14003 14003 if (!(ill->ill_flags & ILLF_MULTICAST) ||
14004 14004 ipif->ipif_allhosts_ilm != NULL)
14005 14005 return;
14006 14006
14007 14007 if (ipif->ipif_isv6) {
14008 14008 in6_addr_t v6allmc = ipv6_all_hosts_mcast;
14009 14009 in6_addr_t v6solmc = ipv6_solicited_node_mcast;
14010 14010
14011 14011 v6solmc.s6_addr32[3] |= ipif->ipif_v6lcl_addr.s6_addr32[3];
14012 14012
14013 14013 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr))
14014 14014 return;
14015 14015
14016 14016 ip1dbg(("ipif_multicast_up - addmulti\n"));
14017 14017
14018 14018 /*
14019 14019 * Join the all hosts multicast address. We skip this for
14020 14020 * underlying IPMP interfaces since they should be invisible.
14021 14021 */
14022 14022 if (!IS_UNDER_IPMP(ill)) {
14023 14023 ilm = ip_addmulti(&v6allmc, ill, ipif->ipif_zoneid,
14024 14024 &err);
14025 14025 if (ilm == NULL) {
14026 14026 ASSERT(err != 0);
14027 14027 ip0dbg(("ipif_multicast_up: "
14028 14028 "all_hosts_mcast failed %d\n", err));
14029 14029 return;
14030 14030 }
14031 14031 ipif->ipif_allhosts_ilm = ilm;
14032 14032 }
14033 14033
14034 14034 /*
14035 14035 * Enable multicast for the solicited node multicast address.
14036 14036 * If IPMP we need to put the membership on the upper ill.
14037 14037 */
14038 14038 if (!(ipif->ipif_flags & IPIF_NOLOCAL)) {
14039 14039 ill_t *mcast_ill = NULL;
14040 14040 boolean_t need_refrele;
14041 14041
14042 14042 if (IS_UNDER_IPMP(ill) &&
14043 14043 (mcast_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL) {
14044 14044 need_refrele = B_TRUE;
14045 14045 } else {
14046 14046 mcast_ill = ill;
14047 14047 need_refrele = B_FALSE;
14048 14048 }
14049 14049
14050 14050 ilm = ip_addmulti(&v6solmc, mcast_ill,
14051 14051 ipif->ipif_zoneid, &err);
14052 14052 if (need_refrele)
14053 14053 ill_refrele(mcast_ill);
14054 14054
14055 14055 if (ilm == NULL) {
14056 14056 ASSERT(err != 0);
14057 14057 ip0dbg(("ipif_multicast_up: solicited MC"
14058 14058 " failed %d\n", err));
14059 14059 if ((ilm = ipif->ipif_allhosts_ilm) != NULL) {
14060 14060 ipif->ipif_allhosts_ilm = NULL;
14061 14061 (void) ip_delmulti(ilm);
14062 14062 }
14063 14063 return;
14064 14064 }
14065 14065 ipif->ipif_solmulti_ilm = ilm;
14066 14066 }
14067 14067 } else {
14068 14068 in6_addr_t v6group;
14069 14069
14070 14070 if (ipif->ipif_lcl_addr == INADDR_ANY || IS_UNDER_IPMP(ill))
14071 14071 return;
14072 14072
14073 14073 /* Join the all hosts multicast address */
14074 14074 ip1dbg(("ipif_multicast_up - addmulti\n"));
14075 14075 IN6_IPADDR_TO_V4MAPPED(htonl(INADDR_ALLHOSTS_GROUP), &v6group);
14076 14076
14077 14077 ilm = ip_addmulti(&v6group, ill, ipif->ipif_zoneid, &err);
14078 14078 if (ilm == NULL) {
14079 14079 ASSERT(err != 0);
14080 14080 ip0dbg(("ipif_multicast_up: failed %d\n", err));
14081 14081 return;
14082 14082 }
14083 14083 ipif->ipif_allhosts_ilm = ilm;
14084 14084 }
14085 14085 }
14086 14086
14087 14087 /*
14088 14088 * Blow away any multicast groups that we joined in ipif_multicast_up().
14089 14089 * (ilms from explicit memberships are handled in conn_update_ill.)
14090 14090 */
14091 14091 void
14092 14092 ipif_multicast_down(ipif_t *ipif)
14093 14093 {
14094 14094 ASSERT(IAM_WRITER_IPIF(ipif));
14095 14095
14096 14096 ip1dbg(("ipif_multicast_down\n"));
14097 14097
14098 14098 if (ipif->ipif_allhosts_ilm != NULL) {
14099 14099 (void) ip_delmulti(ipif->ipif_allhosts_ilm);
14100 14100 ipif->ipif_allhosts_ilm = NULL;
14101 14101 }
14102 14102 if (ipif->ipif_solmulti_ilm != NULL) {
14103 14103 (void) ip_delmulti(ipif->ipif_solmulti_ilm);
14104 14104 ipif->ipif_solmulti_ilm = NULL;
14105 14105 }
14106 14106 }
14107 14107
14108 14108 /*
14109 14109 * Used when an interface comes up to recreate any extra routes on this
14110 14110 * interface.
14111 14111 */
14112 14112 int
14113 14113 ill_recover_saved_ire(ill_t *ill)
14114 14114 {
14115 14115 mblk_t *mp;
14116 14116 ip_stack_t *ipst = ill->ill_ipst;
14117 14117
14118 14118 ip1dbg(("ill_recover_saved_ire(%s)", ill->ill_name));
14119 14119
14120 14120 mutex_enter(&ill->ill_saved_ire_lock);
14121 14121 for (mp = ill->ill_saved_ire_mp; mp != NULL; mp = mp->b_cont) {
14122 14122 ire_t *ire, *nire;
14123 14123 ifrt_t *ifrt;
14124 14124
14125 14125 ifrt = (ifrt_t *)mp->b_rptr;
14126 14126 /*
14127 14127 * Create a copy of the IRE with the saved address and netmask.
14128 14128 */
14129 14129 if (ill->ill_isv6) {
14130 14130 ire = ire_create_v6(
14131 14131 &ifrt->ifrt_v6addr,
14132 14132 &ifrt->ifrt_v6mask,
14133 14133 &ifrt->ifrt_v6gateway_addr,
14134 14134 ifrt->ifrt_type,
14135 14135 ill,
14136 14136 ifrt->ifrt_zoneid,
14137 14137 ifrt->ifrt_flags,
14138 14138 NULL,
14139 14139 ipst);
14140 14140 } else {
14141 14141 ire = ire_create(
14142 14142 (uint8_t *)&ifrt->ifrt_addr,
14143 14143 (uint8_t *)&ifrt->ifrt_mask,
14144 14144 (uint8_t *)&ifrt->ifrt_gateway_addr,
14145 14145 ifrt->ifrt_type,
14146 14146 ill,
14147 14147 ifrt->ifrt_zoneid,
14148 14148 ifrt->ifrt_flags,
14149 14149 NULL,
14150 14150 ipst);
14151 14151 }
14152 14152 if (ire == NULL) {
14153 14153 mutex_exit(&ill->ill_saved_ire_lock);
14154 14154 return (ENOMEM);
14155 14155 }
14156 14156
14157 14157 if (ifrt->ifrt_flags & RTF_SETSRC) {
14158 14158 if (ill->ill_isv6) {
14159 14159 ire->ire_setsrc_addr_v6 =
14160 14160 ifrt->ifrt_v6setsrc_addr;
14161 14161 } else {
14162 14162 ire->ire_setsrc_addr = ifrt->ifrt_setsrc_addr;
14163 14163 }
14164 14164 }
14165 14165
14166 14166 /*
14167 14167 * Some software (for example, GateD and Sun Cluster) attempts
14168 14168 * to create (what amount to) IRE_PREFIX routes with the
14169 14169 * loopback address as the gateway. This is primarily done to
14170 14170 * set up prefixes with the RTF_REJECT flag set (for example,
14171 14171 * when generating aggregate routes.)
14172 14172 *
14173 14173 * If the IRE type (as defined by ill->ill_net_type) is
14174 14174 * IRE_LOOPBACK, then we map the request into a
14175 14175 * IRE_IF_NORESOLVER.
14176 14176 */
14177 14177 if (ill->ill_net_type == IRE_LOOPBACK)
14178 14178 ire->ire_type = IRE_IF_NORESOLVER;
14179 14179
14180 14180 /*
14181 14181 * ire held by ire_add, will be refreled' towards the
14182 14182 * the end of ipif_up_done
14183 14183 */
14184 14184 nire = ire_add(ire);
14185 14185 /*
14186 14186 * Check if it was a duplicate entry. This handles
14187 14187 * the case of two racing route adds for the same route
14188 14188 */
14189 14189 if (nire == NULL) {
14190 14190 ip1dbg(("ill_recover_saved_ire: FAILED\n"));
14191 14191 } else if (nire != ire) {
14192 14192 ip1dbg(("ill_recover_saved_ire: duplicate ire %p\n",
14193 14193 (void *)nire));
14194 14194 ire_delete(nire);
14195 14195 } else {
14196 14196 ip1dbg(("ill_recover_saved_ire: added ire %p\n",
14197 14197 (void *)nire));
14198 14198 }
14199 14199 if (nire != NULL)
14200 14200 ire_refrele(nire);
14201 14201 }
14202 14202 mutex_exit(&ill->ill_saved_ire_lock);
14203 14203 return (0);
14204 14204 }
14205 14205
14206 14206 /*
14207 14207 * Used to set the netmask and broadcast address to default values when the
14208 14208 * interface is brought up. (Always called as writer.)
14209 14209 */
14210 14210 static void
14211 14211 ipif_set_default(ipif_t *ipif)
14212 14212 {
14213 14213 ASSERT(MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14214 14214
14215 14215 if (!ipif->ipif_isv6) {
14216 14216 /*
14217 14217 * Interface holds an IPv4 address. Default
14218 14218 * mask is the natural netmask.
14219 14219 */
14220 14220 if (!ipif->ipif_net_mask) {
14221 14221 ipaddr_t v4mask;
14222 14222
14223 14223 v4mask = ip_net_mask(ipif->ipif_lcl_addr);
14224 14224 V4MASK_TO_V6(v4mask, ipif->ipif_v6net_mask);
14225 14225 }
14226 14226 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14227 14227 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14228 14228 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14229 14229 } else {
14230 14230 V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14231 14231 ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14232 14232 }
14233 14233 /*
14234 14234 * NOTE: SunOS 4.X does this even if the broadcast address
14235 14235 * has been already set thus we do the same here.
14236 14236 */
14237 14237 if (ipif->ipif_flags & IPIF_BROADCAST) {
14238 14238 ipaddr_t v4addr;
14239 14239
14240 14240 v4addr = ipif->ipif_subnet | ~ipif->ipif_net_mask;
14241 14241 IN6_IPADDR_TO_V4MAPPED(v4addr, &ipif->ipif_v6brd_addr);
14242 14242 }
14243 14243 } else {
14244 14244 /*
14245 14245 * Interface holds an IPv6-only address. Default
14246 14246 * mask is all-ones.
14247 14247 */
14248 14248 if (IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6net_mask))
14249 14249 ipif->ipif_v6net_mask = ipv6_all_ones;
14250 14250 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14251 14251 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14252 14252 ipif->ipif_v6subnet = ipif->ipif_v6pp_dst_addr;
14253 14253 } else {
14254 14254 V6_MASK_COPY(ipif->ipif_v6lcl_addr,
14255 14255 ipif->ipif_v6net_mask, ipif->ipif_v6subnet);
14256 14256 }
14257 14257 }
14258 14258 }
14259 14259
14260 14260 /*
14261 14261 * Return 0 if this address can be used as local address without causing
14262 14262 * duplicate address problems. Otherwise, return EADDRNOTAVAIL if the address
14263 14263 * is already up on a different ill, and EADDRINUSE if it's up on the same ill.
14264 14264 * Note that the same IPv6 link-local address is allowed as long as the ills
14265 14265 * are not on the same link.
14266 14266 */
14267 14267 int
14268 14268 ip_addr_availability_check(ipif_t *new_ipif)
14269 14269 {
14270 14270 in6_addr_t our_v6addr;
14271 14271 ill_t *ill;
14272 14272 ipif_t *ipif;
14273 14273 ill_walk_context_t ctx;
14274 14274 ip_stack_t *ipst = new_ipif->ipif_ill->ill_ipst;
14275 14275
14276 14276 ASSERT(IAM_WRITER_IPIF(new_ipif));
14277 14277 ASSERT(MUTEX_HELD(&ipst->ips_ip_addr_avail_lock));
14278 14278 ASSERT(RW_READ_HELD(&ipst->ips_ill_g_lock));
14279 14279
14280 14280 new_ipif->ipif_flags &= ~IPIF_UNNUMBERED;
14281 14281 if (IN6_IS_ADDR_UNSPECIFIED(&new_ipif->ipif_v6lcl_addr) ||
14282 14282 IN6_IS_ADDR_V4MAPPED_ANY(&new_ipif->ipif_v6lcl_addr))
14283 14283 return (0);
14284 14284
14285 14285 our_v6addr = new_ipif->ipif_v6lcl_addr;
14286 14286
14287 14287 if (new_ipif->ipif_isv6)
14288 14288 ill = ILL_START_WALK_V6(&ctx, ipst);
14289 14289 else
14290 14290 ill = ILL_START_WALK_V4(&ctx, ipst);
14291 14291
14292 14292 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
14293 14293 for (ipif = ill->ill_ipif; ipif != NULL;
14294 14294 ipif = ipif->ipif_next) {
14295 14295 if ((ipif == new_ipif) ||
14296 14296 !(ipif->ipif_flags & IPIF_UP) ||
14297 14297 (ipif->ipif_flags & IPIF_UNNUMBERED) ||
14298 14298 !IN6_ARE_ADDR_EQUAL(&ipif->ipif_v6lcl_addr,
14299 14299 &our_v6addr))
14300 14300 continue;
14301 14301
14302 14302 if (new_ipif->ipif_flags & IPIF_POINTOPOINT)
14303 14303 new_ipif->ipif_flags |= IPIF_UNNUMBERED;
14304 14304 else if (ipif->ipif_flags & IPIF_POINTOPOINT)
14305 14305 ipif->ipif_flags |= IPIF_UNNUMBERED;
14306 14306 else if ((IN6_IS_ADDR_LINKLOCAL(&our_v6addr) ||
14307 14307 IN6_IS_ADDR_SITELOCAL(&our_v6addr)) &&
14308 14308 !IS_ON_SAME_LAN(ill, new_ipif->ipif_ill))
14309 14309 continue;
14310 14310 else if (new_ipif->ipif_zoneid != ipif->ipif_zoneid &&
14311 14311 ipif->ipif_zoneid != ALL_ZONES && IS_LOOPBACK(ill))
14312 14312 continue;
14313 14313 else if (new_ipif->ipif_ill == ill)
14314 14314 return (EADDRINUSE);
14315 14315 else
14316 14316 return (EADDRNOTAVAIL);
14317 14317 }
14318 14318 }
14319 14319
14320 14320 return (0);
14321 14321 }
14322 14322
14323 14323 /*
14324 14324 * Bring up an ipif: bring up arp/ndp, bring up the DLPI stream, and add
14325 14325 * IREs for the ipif.
14326 14326 * When the routine returns EINPROGRESS then mp has been consumed and
14327 14327 * the ioctl will be acked from ip_rput_dlpi.
14328 14328 */
14329 14329 int
14330 14330 ipif_up(ipif_t *ipif, queue_t *q, mblk_t *mp)
14331 14331 {
14332 14332 ill_t *ill = ipif->ipif_ill;
14333 14333 boolean_t isv6 = ipif->ipif_isv6;
14334 14334 int err = 0;
14335 14335 boolean_t success;
14336 14336 uint_t ipif_orig_id;
14337 14337 ip_stack_t *ipst = ill->ill_ipst;
14338 14338
14339 14339 ASSERT(IAM_WRITER_IPIF(ipif));
14340 14340
14341 14341 ip1dbg(("ipif_up(%s:%u)\n", ill->ill_name, ipif->ipif_id));
14342 14342 DTRACE_PROBE3(ipif__downup, char *, "ipif_up",
14343 14343 ill_t *, ill, ipif_t *, ipif);
14344 14344
14345 14345 /* Shouldn't get here if it is already up. */
14346 14346 if (ipif->ipif_flags & IPIF_UP)
14347 14347 return (EALREADY);
14348 14348
14349 14349 /*
14350 14350 * If this is a request to bring up a data address on an interface
14351 14351 * under IPMP, then move the address to its IPMP meta-interface and
14352 14352 * try to bring it up. One complication is that the zeroth ipif for
14353 14353 * an ill is special, in that every ill always has one, and that code
14354 14354 * throughout IP deferences ill->ill_ipif without holding any locks.
14355 14355 */
14356 14356 if (IS_UNDER_IPMP(ill) && ipmp_ipif_is_dataaddr(ipif) &&
14357 14357 (!ipif->ipif_isv6 || !V6_IPIF_LINKLOCAL(ipif))) {
14358 14358 ipif_t *stubipif = NULL, *moveipif = NULL;
14359 14359 ill_t *ipmp_ill = ipmp_illgrp_ipmp_ill(ill->ill_grp);
14360 14360
14361 14361 /*
14362 14362 * The ipif being brought up should be quiesced. If it's not,
14363 14363 * something has gone amiss and we need to bail out. (If it's
14364 14364 * quiesced, we know it will remain so via IPIF_CONDEMNED.)
14365 14365 */
14366 14366 mutex_enter(&ill->ill_lock);
14367 14367 if (!ipif_is_quiescent(ipif)) {
14368 14368 mutex_exit(&ill->ill_lock);
14369 14369 return (EINVAL);
14370 14370 }
14371 14371 mutex_exit(&ill->ill_lock);
14372 14372
14373 14373 /*
14374 14374 * If we're going to need to allocate ipifs, do it prior
14375 14375 * to starting the move (and grabbing locks).
14376 14376 */
14377 14377 if (ipif->ipif_id == 0) {
14378 14378 if ((moveipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14379 14379 B_FALSE, &err)) == NULL) {
14380 14380 return (err);
14381 14381 }
14382 14382 if ((stubipif = ipif_allocate(ill, 0, IRE_LOCAL, B_TRUE,
14383 14383 B_FALSE, &err)) == NULL) {
14384 14384 mi_free(moveipif);
14385 14385 return (err);
14386 14386 }
14387 14387 }
14388 14388
14389 14389 /*
14390 14390 * Grab or transfer the ipif to move. During the move, keep
14391 14391 * ill_g_lock held to prevent any ill walker threads from
14392 14392 * seeing things in an inconsistent state.
14393 14393 */
14394 14394 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
14395 14395 if (ipif->ipif_id != 0) {
14396 14396 ipif_remove(ipif);
14397 14397 } else {
14398 14398 ipif_transfer(ipif, moveipif, stubipif);
14399 14399 ipif = moveipif;
14400 14400 }
14401 14401
14402 14402 /*
14403 14403 * Place the ipif on the IPMP ill. If the zeroth ipif on
14404 14404 * the IPMP ill is a stub (0.0.0.0 down address) then we
14405 14405 * replace that one. Otherwise, pick the next available slot.
14406 14406 */
14407 14407 ipif->ipif_ill = ipmp_ill;
14408 14408 ipif_orig_id = ipif->ipif_id;
14409 14409
14410 14410 if (ipmp_ipif_is_stubaddr(ipmp_ill->ill_ipif)) {
14411 14411 ipif_transfer(ipif, ipmp_ill->ill_ipif, NULL);
14412 14412 ipif = ipmp_ill->ill_ipif;
14413 14413 } else {
14414 14414 ipif->ipif_id = -1;
14415 14415 if ((err = ipif_insert(ipif, B_FALSE)) != 0) {
14416 14416 /*
14417 14417 * No more available ipif_id's -- put it back
14418 14418 * on the original ill and fail the operation.
14419 14419 * Since we're writer on the ill, we can be
14420 14420 * sure our old slot is still available.
14421 14421 */
14422 14422 ipif->ipif_id = ipif_orig_id;
14423 14423 ipif->ipif_ill = ill;
14424 14424 if (ipif_orig_id == 0) {
14425 14425 ipif_transfer(ipif, ill->ill_ipif,
14426 14426 NULL);
14427 14427 } else {
14428 14428 VERIFY(ipif_insert(ipif, B_FALSE) == 0);
14429 14429 }
14430 14430 rw_exit(&ipst->ips_ill_g_lock);
14431 14431 return (err);
14432 14432 }
14433 14433 }
14434 14434 rw_exit(&ipst->ips_ill_g_lock);
14435 14435
14436 14436 /*
14437 14437 * Tell SCTP that the ipif has moved. Note that even if we
14438 14438 * had to allocate a new ipif, the original sequence id was
14439 14439 * preserved and therefore SCTP won't know.
14440 14440 */
14441 14441 sctp_move_ipif(ipif, ill, ipmp_ill);
14442 14442
14443 14443 /*
14444 14444 * If the ipif being brought up was on slot zero, then we
14445 14445 * first need to bring up the placeholder we stuck there. In
14446 14446 * ip_rput_dlpi_writer(), arp_bringup_done(), or the recursive
14447 14447 * call to ipif_up() itself, if we successfully bring up the
14448 14448 * placeholder, we'll check ill_move_ipif and bring it up too.
14449 14449 */
14450 14450 if (ipif_orig_id == 0) {
14451 14451 ASSERT(ill->ill_move_ipif == NULL);
14452 14452 ill->ill_move_ipif = ipif;
14453 14453 if ((err = ipif_up(ill->ill_ipif, q, mp)) == 0)
14454 14454 ASSERT(ill->ill_move_ipif == NULL);
14455 14455 if (err != EINPROGRESS)
14456 14456 ill->ill_move_ipif = NULL;
14457 14457 return (err);
14458 14458 }
14459 14459
14460 14460 /*
14461 14461 * Bring it up on the IPMP ill.
14462 14462 */
14463 14463 return (ipif_up(ipif, q, mp));
14464 14464 }
14465 14465
14466 14466 /* Skip arp/ndp for any loopback interface. */
14467 14467 if (ill->ill_wq != NULL) {
14468 14468 conn_t *connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14469 14469 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
14470 14470
14471 14471 if (!ill->ill_dl_up) {
14472 14472 /*
14473 14473 * ill_dl_up is not yet set. i.e. we are yet to
14474 14474 * DL_BIND with the driver and this is the first
14475 14475 * logical interface on the ill to become "up".
14476 14476 * Tell the driver to get going (via DL_BIND_REQ).
14477 14477 * Note that changing "significant" IFF_ flags
14478 14478 * address/netmask etc cause a down/up dance, but
14479 14479 * does not cause an unbind (DL_UNBIND) with the driver
14480 14480 */
14481 14481 return (ill_dl_up(ill, ipif, mp, q));
14482 14482 }
14483 14483
14484 14484 /*
14485 14485 * ipif_resolver_up may end up needeing to bind/attach
14486 14486 * the ARP stream, which in turn necessitates a
14487 14487 * DLPI message exchange with the driver. ioctls are
14488 14488 * serialized and so we cannot send more than one
14489 14489 * interface up message at a time. If ipif_resolver_up
14490 14490 * does need to wait for the DLPI handshake for the ARP stream,
14491 14491 * we get EINPROGRESS and we will complete in arp_bringup_done.
14492 14492 */
14493 14493
14494 14494 ASSERT(connp != NULL || !CONN_Q(q));
14495 14495 if (connp != NULL)
14496 14496 mutex_enter(&connp->conn_lock);
14497 14497 mutex_enter(&ill->ill_lock);
14498 14498 success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14499 14499 mutex_exit(&ill->ill_lock);
14500 14500 if (connp != NULL)
14501 14501 mutex_exit(&connp->conn_lock);
14502 14502 if (!success)
14503 14503 return (EINTR);
14504 14504
14505 14505 /*
14506 14506 * Crank up IPv6 neighbor discovery. Unlike ARP, this should
14507 14507 * complete when ipif_ndp_up returns.
14508 14508 */
14509 14509 err = ipif_resolver_up(ipif, Res_act_initial);
14510 14510 if (err == EINPROGRESS) {
14511 14511 /* We will complete it in arp_bringup_done() */
14512 14512 return (err);
14513 14513 }
14514 14514
14515 14515 if (isv6 && err == 0)
14516 14516 err = ipif_ndp_up(ipif, B_TRUE);
14517 14517
14518 14518 ASSERT(err != EINPROGRESS);
14519 14519 mp = ipsq_pending_mp_get(ipsq, &connp);
14520 14520 ASSERT(mp != NULL);
14521 14521 if (err != 0)
14522 14522 return (err);
14523 14523 } else {
14524 14524 /*
14525 14525 * Interfaces without underlying hardware don't do duplicate
14526 14526 * address detection.
14527 14527 */
14528 14528 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
14529 14529 ipif->ipif_addr_ready = 1;
14530 14530 err = ill_add_ires(ill);
14531 14531 /* allocation failure? */
14532 14532 if (err != 0)
14533 14533 return (err);
14534 14534 }
14535 14535
14536 14536 err = (isv6 ? ipif_up_done_v6(ipif) : ipif_up_done(ipif));
14537 14537 if (err == 0 && ill->ill_move_ipif != NULL) {
14538 14538 ipif = ill->ill_move_ipif;
14539 14539 ill->ill_move_ipif = NULL;
14540 14540 return (ipif_up(ipif, q, mp));
14541 14541 }
14542 14542 return (err);
14543 14543 }
14544 14544
14545 14545 /*
14546 14546 * Add any IREs tied to the ill. For now this is just an IRE_MULTICAST.
14547 14547 * The identical set of IREs need to be removed in ill_delete_ires().
14548 14548 */
14549 14549 int
14550 14550 ill_add_ires(ill_t *ill)
14551 14551 {
14552 14552 ire_t *ire;
14553 14553 in6_addr_t dummy6 = {(uint32_t)V6_MCAST, 0, 0, 1};
14554 14554 in_addr_t dummy4 = htonl(INADDR_ALLHOSTS_GROUP);
14555 14555
14556 14556 if (ill->ill_ire_multicast != NULL)
14557 14557 return (0);
14558 14558
14559 14559 /*
14560 14560 * provide some dummy ire_addr for creating the ire.
14561 14561 */
14562 14562 if (ill->ill_isv6) {
14563 14563 ire = ire_create_v6(&dummy6, 0, 0, IRE_MULTICAST, ill,
14564 14564 ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14565 14565 } else {
14566 14566 ire = ire_create((uchar_t *)&dummy4, 0, 0, IRE_MULTICAST, ill,
14567 14567 ALL_ZONES, RTF_UP, NULL, ill->ill_ipst);
14568 14568 }
14569 14569 if (ire == NULL)
14570 14570 return (ENOMEM);
14571 14571
14572 14572 ill->ill_ire_multicast = ire;
14573 14573 return (0);
14574 14574 }
14575 14575
14576 14576 void
14577 14577 ill_delete_ires(ill_t *ill)
14578 14578 {
14579 14579 if (ill->ill_ire_multicast != NULL) {
14580 14580 /*
14581 14581 * BIND/ATTACH completed; Release the ref for ill_ire_multicast
14582 14582 * which was taken without any th_tracing enabled.
14583 14583 * We also mark it as condemned (note that it was never added)
14584 14584 * so that caching conn's can move off of it.
14585 14585 */
14586 14586 ire_make_condemned(ill->ill_ire_multicast);
14587 14587 ire_refrele_notr(ill->ill_ire_multicast);
14588 14588 ill->ill_ire_multicast = NULL;
14589 14589 }
14590 14590 }
14591 14591
14592 14592 /*
14593 14593 * Perform a bind for the physical device.
14594 14594 * When the routine returns EINPROGRESS then mp has been consumed and
14595 14595 * the ioctl will be acked from ip_rput_dlpi.
14596 14596 * Allocate an unbind message and save it until ipif_down.
14597 14597 */
14598 14598 static int
14599 14599 ill_dl_up(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
14600 14600 {
14601 14601 mblk_t *bind_mp = NULL;
14602 14602 mblk_t *unbind_mp = NULL;
14603 14603 conn_t *connp;
14604 14604 boolean_t success;
14605 14605 int err;
14606 14606
14607 14607 DTRACE_PROBE2(ill__downup, char *, "ill_dl_up", ill_t *, ill);
14608 14608
14609 14609 ip1dbg(("ill_dl_up(%s)\n", ill->ill_name));
14610 14610 ASSERT(IAM_WRITER_ILL(ill));
14611 14611 ASSERT(mp != NULL);
14612 14612
14613 14613 /*
14614 14614 * Make sure we have an IRE_MULTICAST in case we immediately
14615 14615 * start receiving packets.
14616 14616 */
14617 14617 err = ill_add_ires(ill);
14618 14618 if (err != 0)
14619 14619 goto bad;
14620 14620
14621 14621 bind_mp = ip_dlpi_alloc(sizeof (dl_bind_req_t) + sizeof (long),
14622 14622 DL_BIND_REQ);
14623 14623 if (bind_mp == NULL)
14624 14624 goto bad;
14625 14625 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_sap = ill->ill_sap;
14626 14626 ((dl_bind_req_t *)bind_mp->b_rptr)->dl_service_mode = DL_CLDLS;
14627 14627
14628 14628 /*
14629 14629 * ill_unbind_mp would be non-null if the following sequence had
14630 14630 * happened:
14631 14631 * - send DL_BIND_REQ to driver, wait for response
14632 14632 * - multiple ioctls that need to bring the ipif up are encountered,
14633 14633 * but they cannot enter the ipsq due to the outstanding DL_BIND_REQ.
14634 14634 * These ioctls will then be enqueued on the ipsq
14635 14635 * - a DL_ERROR_ACK is returned for the DL_BIND_REQ
14636 14636 * At this point, the pending ioctls in the ipsq will be drained, and
14637 14637 * since ill->ill_dl_up was not set, ill_dl_up would be invoked with
14638 14638 * a non-null ill->ill_unbind_mp
14639 14639 */
14640 14640 if (ill->ill_unbind_mp == NULL) {
14641 14641 unbind_mp = ip_dlpi_alloc(sizeof (dl_unbind_req_t),
14642 14642 DL_UNBIND_REQ);
14643 14643 if (unbind_mp == NULL)
14644 14644 goto bad;
14645 14645 }
14646 14646 /*
14647 14647 * Record state needed to complete this operation when the
14648 14648 * DL_BIND_ACK shows up. Also remember the pre-allocated mblks.
14649 14649 */
14650 14650 connp = CONN_Q(q) ? Q_TO_CONN(q) : NULL;
14651 14651 ASSERT(connp != NULL || !CONN_Q(q));
14652 14652 GRAB_CONN_LOCK(q);
14653 14653 mutex_enter(&ipif->ipif_ill->ill_lock);
14654 14654 success = ipsq_pending_mp_add(connp, ipif, q, mp, 0);
14655 14655 mutex_exit(&ipif->ipif_ill->ill_lock);
14656 14656 RELEASE_CONN_LOCK(q);
14657 14657 if (!success)
14658 14658 goto bad;
14659 14659
14660 14660 /*
14661 14661 * Save the unbind message for ill_dl_down(); it will be consumed when
14662 14662 * the interface goes down.
14663 14663 */
14664 14664 if (ill->ill_unbind_mp == NULL)
14665 14665 ill->ill_unbind_mp = unbind_mp;
14666 14666
14667 14667 ill_dlpi_send(ill, bind_mp);
14668 14668 /* Send down link-layer capabilities probe if not already done. */
14669 14669 ill_capability_probe(ill);
14670 14670
14671 14671 /*
14672 14672 * Sysid used to rely on the fact that netboots set domainname
14673 14673 * and the like. Now that miniroot boots aren't strictly netboots
14674 14674 * and miniroot network configuration is driven from userland
14675 14675 * these things still need to be set. This situation can be detected
14676 14676 * by comparing the interface being configured here to the one
14677 14677 * dhcifname was set to reference by the boot loader. Once sysid is
14678 14678 * converted to use dhcp_ipc_getinfo() this call can go away.
14679 14679 */
14680 14680 if ((ipif->ipif_flags & IPIF_DHCPRUNNING) &&
14681 14681 (strcmp(ill->ill_name, dhcifname) == 0) &&
14682 14682 (strlen(srpc_domain) == 0)) {
14683 14683 if (dhcpinit() != 0)
14684 14684 cmn_err(CE_WARN, "no cached dhcp response");
14685 14685 }
14686 14686
14687 14687 /*
14688 14688 * This operation will complete in ip_rput_dlpi with either
14689 14689 * a DL_BIND_ACK or DL_ERROR_ACK.
14690 14690 */
14691 14691 return (EINPROGRESS);
14692 14692 bad:
14693 14693 ip1dbg(("ill_dl_up(%s) FAILED\n", ill->ill_name));
14694 14694
14695 14695 freemsg(bind_mp);
14696 14696 freemsg(unbind_mp);
14697 14697 return (ENOMEM);
14698 14698 }
14699 14699
14700 14700 /* Add room for tcp+ip headers */
14701 14701 uint_t ip_loopback_mtuplus = IP_LOOPBACK_MTU + IP_SIMPLE_HDR_LENGTH + 20;
14702 14702
14703 14703 /*
14704 14704 * DLPI and ARP is up.
14705 14705 * Create all the IREs associated with an interface. Bring up multicast.
14706 14706 * Set the interface flag and finish other initialization
14707 14707 * that potentially had to be deferred to after DL_BIND_ACK.
14708 14708 */
14709 14709 int
14710 14710 ipif_up_done(ipif_t *ipif)
14711 14711 {
14712 14712 ill_t *ill = ipif->ipif_ill;
14713 14713 int err = 0;
14714 14714 boolean_t loopback = B_FALSE;
14715 14715 boolean_t update_src_selection = B_TRUE;
14716 14716 ipif_t *tmp_ipif;
14717 14717
14718 14718 ip1dbg(("ipif_up_done(%s:%u)\n",
14719 14719 ipif->ipif_ill->ill_name, ipif->ipif_id));
14720 14720 DTRACE_PROBE3(ipif__downup, char *, "ipif_up_done",
14721 14721 ill_t *, ill, ipif_t *, ipif);
14722 14722
14723 14723 /* Check if this is a loopback interface */
14724 14724 if (ipif->ipif_ill->ill_wq == NULL)
14725 14725 loopback = B_TRUE;
14726 14726
14727 14727 ASSERT(!MUTEX_HELD(&ipif->ipif_ill->ill_lock));
14728 14728
14729 14729 /*
14730 14730 * If all other interfaces for this ill are down or DEPRECATED,
14731 14731 * or otherwise unsuitable for source address selection,
14732 14732 * reset the src generation numbers to make sure source
14733 14733 * address selection gets to take this new ipif into account.
14734 14734 * No need to hold ill_lock while traversing the ipif list since
14735 14735 * we are writer
14736 14736 */
14737 14737 for (tmp_ipif = ill->ill_ipif; tmp_ipif;
14738 14738 tmp_ipif = tmp_ipif->ipif_next) {
14739 14739 if (((tmp_ipif->ipif_flags &
14740 14740 (IPIF_NOXMIT|IPIF_ANYCAST|IPIF_NOLOCAL|IPIF_DEPRECATED)) ||
14741 14741 !(tmp_ipif->ipif_flags & IPIF_UP)) ||
14742 14742 (tmp_ipif == ipif))
14743 14743 continue;
14744 14744 /* first useable pre-existing interface */
14745 14745 update_src_selection = B_FALSE;
14746 14746 break;
14747 14747 }
14748 14748 if (update_src_selection)
14749 14749 ip_update_source_selection(ill->ill_ipst);
14750 14750
14751 14751 if (IS_LOOPBACK(ill) || ill->ill_net_type == IRE_IF_NORESOLVER) {
14752 14752 nce_t *loop_nce = NULL;
14753 14753 uint16_t flags = (NCE_F_MYADDR | NCE_F_AUTHORITY | NCE_F_NONUD);
14754 14754
14755 14755 /*
14756 14756 * lo0:1 and subsequent ipifs were marked IRE_LOCAL in
14757 14757 * ipif_lookup_on_name(), but in the case of zones we can have
14758 14758 * several loopback addresses on lo0. So all the interfaces with
14759 14759 * loopback addresses need to be marked IRE_LOOPBACK.
14760 14760 */
14761 14761 if (V4_PART_OF_V6(ipif->ipif_v6lcl_addr) ==
14762 14762 htonl(INADDR_LOOPBACK))
14763 14763 ipif->ipif_ire_type = IRE_LOOPBACK;
14764 14764 else
14765 14765 ipif->ipif_ire_type = IRE_LOCAL;
14766 14766 if (ill->ill_net_type != IRE_LOOPBACK)
14767 14767 flags |= NCE_F_PUBLISH;
14768 14768
14769 14769 /* add unicast nce for the local addr */
14770 14770 err = nce_lookup_then_add_v4(ill, NULL,
14771 14771 ill->ill_phys_addr_length, &ipif->ipif_lcl_addr, flags,
14772 14772 ND_REACHABLE, &loop_nce);
14773 14773 /* A shared-IP zone sees EEXIST for lo0:N */
14774 14774 if (err == 0 || err == EEXIST) {
14775 14775 ipif->ipif_added_nce = 1;
14776 14776 loop_nce->nce_ipif_cnt++;
14777 14777 nce_refrele(loop_nce);
14778 14778 err = 0;
14779 14779 } else {
14780 14780 ASSERT(loop_nce == NULL);
14781 14781 return (err);
14782 14782 }
14783 14783 }
14784 14784
14785 14785 /* Create all the IREs associated with this interface */
14786 14786 err = ipif_add_ires_v4(ipif, loopback);
14787 14787 if (err != 0) {
14788 14788 /*
14789 14789 * see comments about return value from
14790 14790 * ip_addr_availability_check() in ipif_add_ires_v4().
14791 14791 */
14792 14792 if (err != EADDRINUSE) {
14793 14793 (void) ipif_arp_down(ipif);
14794 14794 } else {
14795 14795 /*
14796 14796 * Make IPMP aware of the deleted ipif so that
14797 14797 * the needed ipmp cleanup (e.g., of ipif_bound_ill)
14798 14798 * can be completed. Note that we do not want to
14799 14799 * destroy the nce that was created on the ipmp_ill
14800 14800 * for the active copy of the duplicate address in
14801 14801 * use.
14802 14802 */
14803 14803 if (IS_IPMP(ill))
14804 14804 ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
14805 14805 err = EADDRNOTAVAIL;
14806 14806 }
14807 14807 return (err);
14808 14808 }
14809 14809
14810 14810 if (ill->ill_ipif_up_count == 1 && !loopback) {
14811 14811 /* Recover any additional IREs entries for this ill */
14812 14812 (void) ill_recover_saved_ire(ill);
14813 14813 }
14814 14814
14815 14815 if (ill->ill_need_recover_multicast) {
14816 14816 /*
14817 14817 * Need to recover all multicast memberships in the driver.
14818 14818 * This had to be deferred until we had attached. The same
14819 14819 * code exists in ipif_up_done_v6() to recover IPv6
14820 14820 * memberships.
14821 14821 *
14822 14822 * Note that it would be preferable to unconditionally do the
14823 14823 * ill_recover_multicast() in ill_dl_up(), but we cannot do
14824 14824 * that since ill_join_allmulti() depends on ill_dl_up being
14825 14825 * set, and it is not set until we receive a DL_BIND_ACK after
14826 14826 * having called ill_dl_up().
14827 14827 */
14828 14828 ill_recover_multicast(ill);
14829 14829 }
14830 14830
14831 14831 if (ill->ill_ipif_up_count == 1) {
14832 14832 /*
14833 14833 * Since the interface is now up, it may now be active.
14834 14834 */
14835 14835 if (IS_UNDER_IPMP(ill))
14836 14836 ipmp_ill_refresh_active(ill);
14837 14837
14838 14838 /*
14839 14839 * If this is an IPMP interface, we may now be able to
14840 14840 * establish ARP entries.
14841 14841 */
14842 14842 if (IS_IPMP(ill))
14843 14843 ipmp_illgrp_refresh_arpent(ill->ill_grp);
14844 14844 }
14845 14845
14846 14846 /* Join the allhosts multicast address */
14847 14847 ipif_multicast_up(ipif);
14848 14848
14849 14849 if (!loopback && !update_src_selection &&
14850 14850 !(ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST|IPIF_DEPRECATED)))
14851 14851 ip_update_source_selection(ill->ill_ipst);
14852 14852
14853 14853 if (!loopback && ipif->ipif_addr_ready) {
14854 14854 /* Broadcast an address mask reply. */
14855 14855 ipif_mask_reply(ipif);
14856 14856 }
14857 14857 /* Perhaps ilgs should use this ill */
14858 14858 update_conn_ill(NULL, ill->ill_ipst);
14859 14859
14860 14860 /*
14861 14861 * This had to be deferred until we had bound. Tell routing sockets and
14862 14862 * others that this interface is up if it looks like the address has
14863 14863 * been validated. Otherwise, if it isn't ready yet, wait for
14864 14864 * duplicate address detection to do its thing.
14865 14865 */
14866 14866 if (ipif->ipif_addr_ready)
14867 14867 ipif_up_notify(ipif);
14868 14868 return (0);
14869 14869 }
14870 14870
14871 14871 /*
14872 14872 * Add the IREs associated with the ipif.
14873 14873 * Those MUST be explicitly removed in ipif_delete_ires_v4.
14874 14874 */
14875 14875 static int
14876 14876 ipif_add_ires_v4(ipif_t *ipif, boolean_t loopback)
14877 14877 {
14878 14878 ill_t *ill = ipif->ipif_ill;
14879 14879 ip_stack_t *ipst = ill->ill_ipst;
14880 14880 ire_t *ire_array[20];
14881 14881 ire_t **irep = ire_array;
14882 14882 ire_t **irep1;
14883 14883 ipaddr_t net_mask = 0;
14884 14884 ipaddr_t subnet_mask, route_mask;
14885 14885 int err;
14886 14886 ire_t *ire_local = NULL; /* LOCAL or LOOPBACK */
14887 14887 ire_t *ire_if = NULL;
14888 14888 uchar_t *gw;
14889 14889
14890 14890 if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14891 14891 !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14892 14892 /*
14893 14893 * If we're on a labeled system then make sure that zone-
14894 14894 * private addresses have proper remote host database entries.
14895 14895 */
14896 14896 if (is_system_labeled() &&
14897 14897 ipif->ipif_ire_type != IRE_LOOPBACK &&
14898 14898 !tsol_check_interface_address(ipif))
14899 14899 return (EINVAL);
14900 14900
14901 14901 /* Register the source address for __sin6_src_id */
14902 14902 err = ip_srcid_insert(&ipif->ipif_v6lcl_addr,
14903 14903 ipif->ipif_zoneid, ipst);
14904 14904 if (err != 0) {
14905 14905 ip0dbg(("ipif_add_ires: srcid_insert %d\n", err));
14906 14906 return (err);
14907 14907 }
14908 14908
14909 14909 if (loopback)
14910 14910 gw = (uchar_t *)&ipif->ipif_lcl_addr;
14911 14911 else
14912 14912 gw = NULL;
14913 14913
14914 14914 /* If the interface address is set, create the local IRE. */
14915 14915 ire_local = ire_create(
14916 14916 (uchar_t *)&ipif->ipif_lcl_addr, /* dest address */
14917 14917 (uchar_t *)&ip_g_all_ones, /* mask */
14918 14918 gw, /* gateway */
14919 14919 ipif->ipif_ire_type, /* LOCAL or LOOPBACK */
14920 14920 ipif->ipif_ill,
14921 14921 ipif->ipif_zoneid,
14922 14922 ((ipif->ipif_flags & IPIF_PRIVATE) ?
14923 14923 RTF_PRIVATE : 0) | RTF_KERNEL,
14924 14924 NULL,
14925 14925 ipst);
14926 14926 ip1dbg(("ipif_add_ires: 0x%p creating IRE %p type 0x%x"
14927 14927 " for 0x%x\n", (void *)ipif, (void *)ire_local,
14928 14928 ipif->ipif_ire_type,
14929 14929 ntohl(ipif->ipif_lcl_addr)));
14930 14930 if (ire_local == NULL) {
14931 14931 ip1dbg(("ipif_up_done: NULL ire_local\n"));
14932 14932 err = ENOMEM;
14933 14933 goto bad;
14934 14934 }
14935 14935 } else {
14936 14936 ip1dbg((
14937 14937 "ipif_add_ires: not creating IRE %d for 0x%x: flags 0x%x\n",
14938 14938 ipif->ipif_ire_type,
14939 14939 ntohl(ipif->ipif_lcl_addr),
14940 14940 (uint_t)ipif->ipif_flags));
14941 14941 }
14942 14942 if ((ipif->ipif_lcl_addr != INADDR_ANY) &&
14943 14943 !(ipif->ipif_flags & IPIF_NOLOCAL)) {
14944 14944 net_mask = ip_net_mask(ipif->ipif_lcl_addr);
14945 14945 } else {
14946 14946 net_mask = htonl(IN_CLASSA_NET); /* fallback */
14947 14947 }
14948 14948
14949 14949 subnet_mask = ipif->ipif_net_mask;
14950 14950
14951 14951 /*
14952 14952 * If mask was not specified, use natural netmask of
14953 14953 * interface address. Also, store this mask back into the
14954 14954 * ipif struct.
14955 14955 */
14956 14956 if (subnet_mask == 0) {
14957 14957 subnet_mask = net_mask;
14958 14958 V4MASK_TO_V6(subnet_mask, ipif->ipif_v6net_mask);
14959 14959 V6_MASK_COPY(ipif->ipif_v6lcl_addr, ipif->ipif_v6net_mask,
14960 14960 ipif->ipif_v6subnet);
14961 14961 }
14962 14962
14963 14963 /* Set up the IRE_IF_RESOLVER or IRE_IF_NORESOLVER, as appropriate. */
14964 14964 if (!loopback && !(ipif->ipif_flags & IPIF_NOXMIT) &&
14965 14965 ipif->ipif_subnet != INADDR_ANY) {
14966 14966 /* ipif_subnet is ipif_pp_dst_addr for pt-pt */
14967 14967
14968 14968 if (ipif->ipif_flags & IPIF_POINTOPOINT) {
14969 14969 route_mask = IP_HOST_MASK;
14970 14970 } else {
14971 14971 route_mask = subnet_mask;
14972 14972 }
14973 14973
14974 14974 ip1dbg(("ipif_add_ires: ipif 0x%p ill 0x%p "
14975 14975 "creating if IRE ill_net_type 0x%x for 0x%x\n",
14976 14976 (void *)ipif, (void *)ill, ill->ill_net_type,
14977 14977 ntohl(ipif->ipif_subnet)));
14978 14978 ire_if = ire_create(
14979 14979 (uchar_t *)&ipif->ipif_subnet,
14980 14980 (uchar_t *)&route_mask,
14981 14981 (uchar_t *)&ipif->ipif_lcl_addr,
14982 14982 ill->ill_net_type,
14983 14983 ill,
14984 14984 ipif->ipif_zoneid,
14985 14985 ((ipif->ipif_flags & IPIF_PRIVATE) ?
14986 14986 RTF_PRIVATE: 0) | RTF_KERNEL,
14987 14987 NULL,
14988 14988 ipst);
14989 14989 if (ire_if == NULL) {
14990 14990 ip1dbg(("ipif_up_done: NULL ire_if\n"));
14991 14991 err = ENOMEM;
14992 14992 goto bad;
14993 14993 }
14994 14994 }
14995 14995
14996 14996 /*
14997 14997 * Create any necessary broadcast IREs.
14998 14998 */
14999 14999 if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15000 15000 !(ipif->ipif_flags & IPIF_NOXMIT))
15001 15001 irep = ipif_create_bcast_ires(ipif, irep);
15002 15002
15003 15003 /* If an earlier ire_create failed, get out now */
15004 15004 for (irep1 = irep; irep1 > ire_array; ) {
15005 15005 irep1--;
15006 15006 if (*irep1 == NULL) {
15007 15007 ip1dbg(("ipif_up_done: NULL ire found in ire_array\n"));
15008 15008 err = ENOMEM;
15009 15009 goto bad;
15010 15010 }
15011 15011 }
15012 15012
15013 15013 /*
15014 15014 * Need to atomically check for IP address availability under
15015 15015 * ip_addr_avail_lock. ill_g_lock is held as reader to ensure no new
15016 15016 * ills or new ipifs can be added while we are checking availability.
15017 15017 */
15018 15018 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15019 15019 mutex_enter(&ipst->ips_ip_addr_avail_lock);
15020 15020 /* Mark it up, and increment counters. */
15021 15021 ipif->ipif_flags |= IPIF_UP;
15022 15022 ill->ill_ipif_up_count++;
15023 15023 err = ip_addr_availability_check(ipif);
15024 15024 mutex_exit(&ipst->ips_ip_addr_avail_lock);
15025 15025 rw_exit(&ipst->ips_ill_g_lock);
15026 15026
15027 15027 if (err != 0) {
15028 15028 /*
15029 15029 * Our address may already be up on the same ill. In this case,
15030 15030 * the ARP entry for our ipif replaced the one for the other
15031 15031 * ipif. So we don't want to delete it (otherwise the other ipif
15032 15032 * would be unable to send packets).
15033 15033 * ip_addr_availability_check() identifies this case for us and
15034 15034 * returns EADDRINUSE; Caller should turn it into EADDRNOTAVAIL
15035 15035 * which is the expected error code.
15036 15036 */
15037 15037 ill->ill_ipif_up_count--;
15038 15038 ipif->ipif_flags &= ~IPIF_UP;
15039 15039 goto bad;
15040 15040 }
15041 15041
15042 15042 /*
15043 15043 * Add in all newly created IREs. ire_create_bcast() has
15044 15044 * already checked for duplicates of the IRE_BROADCAST type.
15045 15045 * We add the IRE_INTERFACE before the IRE_LOCAL to ensure
15046 15046 * that lookups find the IRE_LOCAL even if the IRE_INTERFACE is
15047 15047 * a /32 route.
15048 15048 */
15049 15049 if (ire_if != NULL) {
15050 15050 ire_if = ire_add(ire_if);
15051 15051 if (ire_if == NULL) {
15052 15052 err = ENOMEM;
15053 15053 goto bad2;
15054 15054 }
15055 15055 #ifdef DEBUG
15056 15056 ire_refhold_notr(ire_if);
15057 15057 ire_refrele(ire_if);
15058 15058 #endif
15059 15059 }
15060 15060 if (ire_local != NULL) {
15061 15061 ire_local = ire_add(ire_local);
15062 15062 if (ire_local == NULL) {
15063 15063 err = ENOMEM;
15064 15064 goto bad2;
15065 15065 }
15066 15066 #ifdef DEBUG
15067 15067 ire_refhold_notr(ire_local);
15068 15068 ire_refrele(ire_local);
15069 15069 #endif
15070 15070 }
15071 15071 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15072 15072 if (ire_local != NULL)
15073 15073 ipif->ipif_ire_local = ire_local;
15074 15074 if (ire_if != NULL)
15075 15075 ipif->ipif_ire_if = ire_if;
15076 15076 rw_exit(&ipst->ips_ill_g_lock);
15077 15077 ire_local = NULL;
15078 15078 ire_if = NULL;
15079 15079
15080 15080 /*
15081 15081 * We first add all of them, and if that succeeds we refrele the
15082 15082 * bunch. That enables us to delete all of them should any of the
15083 15083 * ire_adds fail.
15084 15084 */
15085 15085 for (irep1 = irep; irep1 > ire_array; ) {
15086 15086 irep1--;
15087 15087 ASSERT(!MUTEX_HELD(&((*irep1)->ire_ill->ill_lock)));
15088 15088 *irep1 = ire_add(*irep1);
15089 15089 if (*irep1 == NULL) {
15090 15090 err = ENOMEM;
15091 15091 goto bad2;
15092 15092 }
15093 15093 }
15094 15094
15095 15095 for (irep1 = irep; irep1 > ire_array; ) {
15096 15096 irep1--;
15097 15097 /* refheld by ire_add. */
15098 15098 if (*irep1 != NULL) {
15099 15099 ire_refrele(*irep1);
15100 15100 *irep1 = NULL;
15101 15101 }
15102 15102 }
15103 15103
15104 15104 if (!loopback) {
15105 15105 /*
15106 15106 * If the broadcast address has been set, make sure it makes
15107 15107 * sense based on the interface address.
15108 15108 * Only match on ill since we are sharing broadcast addresses.
15109 15109 */
15110 15110 if ((ipif->ipif_brd_addr != INADDR_ANY) &&
15111 15111 (ipif->ipif_flags & IPIF_BROADCAST)) {
15112 15112 ire_t *ire;
15113 15113
15114 15114 ire = ire_ftable_lookup_v4(ipif->ipif_brd_addr, 0, 0,
15115 15115 IRE_BROADCAST, ipif->ipif_ill, ALL_ZONES, NULL,
15116 15116 (MATCH_IRE_TYPE | MATCH_IRE_ILL), 0, ipst, NULL);
15117 15117
15118 15118 if (ire == NULL) {
15119 15119 /*
15120 15120 * If there isn't a matching broadcast IRE,
15121 15121 * revert to the default for this netmask.
15122 15122 */
15123 15123 ipif->ipif_v6brd_addr = ipv6_all_zeros;
15124 15124 mutex_enter(&ipif->ipif_ill->ill_lock);
15125 15125 ipif_set_default(ipif);
15126 15126 mutex_exit(&ipif->ipif_ill->ill_lock);
15127 15127 } else {
15128 15128 ire_refrele(ire);
15129 15129 }
15130 15130 }
15131 15131
15132 15132 }
15133 15133 return (0);
15134 15134
15135 15135 bad2:
15136 15136 ill->ill_ipif_up_count--;
15137 15137 ipif->ipif_flags &= ~IPIF_UP;
15138 15138
15139 15139 bad:
15140 15140 ip1dbg(("ipif_add_ires: FAILED \n"));
15141 15141 if (ire_local != NULL)
15142 15142 ire_delete(ire_local);
15143 15143 if (ire_if != NULL)
15144 15144 ire_delete(ire_if);
15145 15145
15146 15146 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15147 15147 ire_local = ipif->ipif_ire_local;
15148 15148 ipif->ipif_ire_local = NULL;
15149 15149 ire_if = ipif->ipif_ire_if;
15150 15150 ipif->ipif_ire_if = NULL;
15151 15151 rw_exit(&ipst->ips_ill_g_lock);
15152 15152 if (ire_local != NULL) {
15153 15153 ire_delete(ire_local);
15154 15154 ire_refrele_notr(ire_local);
15155 15155 }
15156 15156 if (ire_if != NULL) {
15157 15157 ire_delete(ire_if);
15158 15158 ire_refrele_notr(ire_if);
15159 15159 }
15160 15160
15161 15161 while (irep > ire_array) {
15162 15162 irep--;
15163 15163 if (*irep != NULL) {
15164 15164 ire_delete(*irep);
15165 15165 }
15166 15166 }
15167 15167 (void) ip_srcid_remove(&ipif->ipif_v6lcl_addr, ipif->ipif_zoneid, ipst);
15168 15168
15169 15169 return (err);
15170 15170 }
15171 15171
15172 15172 /* Remove all the IREs created by ipif_add_ires_v4 */
15173 15173 void
15174 15174 ipif_delete_ires_v4(ipif_t *ipif)
15175 15175 {
15176 15176 ill_t *ill = ipif->ipif_ill;
15177 15177 ip_stack_t *ipst = ill->ill_ipst;
15178 15178 ire_t *ire;
15179 15179
15180 15180 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15181 15181 ire = ipif->ipif_ire_local;
15182 15182 ipif->ipif_ire_local = NULL;
15183 15183 rw_exit(&ipst->ips_ill_g_lock);
15184 15184 if (ire != NULL) {
15185 15185 /*
15186 15186 * Move count to ipif so we don't loose the count due to
15187 15187 * a down/up dance.
15188 15188 */
15189 15189 atomic_add_32(&ipif->ipif_ib_pkt_count, ire->ire_ib_pkt_count);
15190 15190
15191 15191 ire_delete(ire);
15192 15192 ire_refrele_notr(ire);
15193 15193 }
15194 15194 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15195 15195 ire = ipif->ipif_ire_if;
15196 15196 ipif->ipif_ire_if = NULL;
15197 15197 rw_exit(&ipst->ips_ill_g_lock);
15198 15198 if (ire != NULL) {
15199 15199 ire_delete(ire);
15200 15200 ire_refrele_notr(ire);
15201 15201 }
15202 15202
15203 15203 /*
15204 15204 * Delete the broadcast IREs.
15205 15205 */
15206 15206 if ((ipif->ipif_flags & IPIF_BROADCAST) &&
15207 15207 !(ipif->ipif_flags & IPIF_NOXMIT))
15208 15208 ipif_delete_bcast_ires(ipif);
15209 15209 }
15210 15210
15211 15211 /*
15212 15212 * Checks for availbility of a usable source address (if there is one) when the
15213 15213 * destination ILL has the ill_usesrc_ifindex pointing to another ILL. Note
15214 15214 * this selection is done regardless of the destination.
15215 15215 */
15216 15216 boolean_t
15217 15217 ipif_zone_avail(uint_t ifindex, boolean_t isv6, zoneid_t zoneid,
15218 15218 ip_stack_t *ipst)
15219 15219 {
15220 15220 ipif_t *ipif = NULL;
15221 15221 ill_t *uill;
15222 15222
15223 15223 ASSERT(ifindex != 0);
15224 15224
15225 15225 uill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
15226 15226 if (uill == NULL)
15227 15227 return (B_FALSE);
15228 15228
15229 15229 mutex_enter(&uill->ill_lock);
15230 15230 for (ipif = uill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15231 15231 if (IPIF_IS_CONDEMNED(ipif))
15232 15232 continue;
15233 15233 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15234 15234 continue;
15235 15235 if (!(ipif->ipif_flags & IPIF_UP))
15236 15236 continue;
15237 15237 if (ipif->ipif_zoneid != zoneid)
15238 15238 continue;
15239 15239 if (isv6 ? IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15240 15240 ipif->ipif_lcl_addr == INADDR_ANY)
15241 15241 continue;
15242 15242 mutex_exit(&uill->ill_lock);
15243 15243 ill_refrele(uill);
15244 15244 return (B_TRUE);
15245 15245 }
15246 15246 mutex_exit(&uill->ill_lock);
15247 15247 ill_refrele(uill);
15248 15248 return (B_FALSE);
15249 15249 }
15250 15250
15251 15251 /*
15252 15252 * Find an ipif with a good local address on the ill+zoneid.
15253 15253 */
15254 15254 ipif_t *
15255 15255 ipif_good_addr(ill_t *ill, zoneid_t zoneid)
15256 15256 {
15257 15257 ipif_t *ipif;
15258 15258
15259 15259 mutex_enter(&ill->ill_lock);
15260 15260 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
15261 15261 if (IPIF_IS_CONDEMNED(ipif))
15262 15262 continue;
15263 15263 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15264 15264 continue;
15265 15265 if (!(ipif->ipif_flags & IPIF_UP))
15266 15266 continue;
15267 15267 if (ipif->ipif_zoneid != zoneid &&
15268 15268 ipif->ipif_zoneid != ALL_ZONES && zoneid != ALL_ZONES)
15269 15269 continue;
15270 15270 if (ill->ill_isv6 ?
15271 15271 IN6_IS_ADDR_UNSPECIFIED(&ipif->ipif_v6lcl_addr) :
15272 15272 ipif->ipif_lcl_addr == INADDR_ANY)
15273 15273 continue;
15274 15274 ipif_refhold_locked(ipif);
15275 15275 mutex_exit(&ill->ill_lock);
15276 15276 return (ipif);
15277 15277 }
15278 15278 mutex_exit(&ill->ill_lock);
15279 15279 return (NULL);
15280 15280 }
15281 15281
15282 15282 /*
15283 15283 * IP source address type, sorted from worst to best. For a given type,
15284 15284 * always prefer IP addresses on the same subnet. All-zones addresses are
15285 15285 * suboptimal because they pose problems with unlabeled destinations.
15286 15286 */
15287 15287 typedef enum {
15288 15288 IPIF_NONE,
15289 15289 IPIF_DIFFNET_DEPRECATED, /* deprecated and different subnet */
15290 15290 IPIF_SAMENET_DEPRECATED, /* deprecated and same subnet */
15291 15291 IPIF_DIFFNET_ALLZONES, /* allzones and different subnet */
15292 15292 IPIF_SAMENET_ALLZONES, /* allzones and same subnet */
15293 15293 IPIF_DIFFNET, /* normal and different subnet */
15294 15294 IPIF_SAMENET, /* normal and same subnet */
15295 15295 IPIF_LOCALADDR /* local loopback */
15296 15296 } ipif_type_t;
15297 15297
15298 15298 /*
15299 15299 * Pick the optimal ipif on `ill' for sending to destination `dst' from zone
15300 15300 * `zoneid'. We rate usable ipifs from low -> high as per the ipif_type_t
15301 15301 * enumeration, and return the highest-rated ipif. If there's a tie, we pick
15302 15302 * the first one, unless IPMP is used in which case we round-robin among them;
15303 15303 * see below for more.
15304 15304 *
15305 15305 * Returns NULL if there is no suitable source address for the ill.
15306 15306 * This only occurs when there is no valid source address for the ill.
15307 15307 */
15308 15308 ipif_t *
15309 15309 ipif_select_source_v4(ill_t *ill, ipaddr_t dst, zoneid_t zoneid,
15310 15310 boolean_t allow_usesrc, boolean_t *notreadyp)
15311 15311 {
15312 15312 ill_t *usill = NULL;
15313 15313 ill_t *ipmp_ill = NULL;
15314 15314 ipif_t *start_ipif, *next_ipif, *ipif, *best_ipif;
15315 15315 ipif_type_t type, best_type;
15316 15316 tsol_tpc_t *src_rhtp, *dst_rhtp;
15317 15317 ip_stack_t *ipst = ill->ill_ipst;
15318 15318 boolean_t samenet;
15319 15319
15320 15320 if (ill->ill_usesrc_ifindex != 0 && allow_usesrc) {
15321 15321 usill = ill_lookup_on_ifindex(ill->ill_usesrc_ifindex,
15322 15322 B_FALSE, ipst);
15323 15323 if (usill != NULL)
15324 15324 ill = usill; /* Select source from usesrc ILL */
15325 15325 else
15326 15326 return (NULL);
15327 15327 }
15328 15328
15329 15329 /*
15330 15330 * Test addresses should never be used for source address selection,
15331 15331 * so if we were passed one, switch to the IPMP meta-interface.
15332 15332 */
15333 15333 if (IS_UNDER_IPMP(ill)) {
15334 15334 if ((ipmp_ill = ipmp_ill_hold_ipmp_ill(ill)) != NULL)
15335 15335 ill = ipmp_ill; /* Select source from IPMP ill */
15336 15336 else
15337 15337 return (NULL);
15338 15338 }
15339 15339
15340 15340 /*
15341 15341 * If we're dealing with an unlabeled destination on a labeled system,
15342 15342 * make sure that we ignore source addresses that are incompatible with
15343 15343 * the destination's default label. That destination's default label
15344 15344 * must dominate the minimum label on the source address.
15345 15345 */
15346 15346 dst_rhtp = NULL;
15347 15347 if (is_system_labeled()) {
15348 15348 dst_rhtp = find_tpc(&dst, IPV4_VERSION, B_FALSE);
15349 15349 if (dst_rhtp == NULL)
15350 15350 return (NULL);
15351 15351 if (dst_rhtp->tpc_tp.host_type != UNLABELED) {
15352 15352 TPC_RELE(dst_rhtp);
15353 15353 dst_rhtp = NULL;
15354 15354 }
15355 15355 }
15356 15356
15357 15357 /*
15358 15358 * Hold the ill_g_lock as reader. This makes sure that no ipif/ill
15359 15359 * can be deleted. But an ipif/ill can get CONDEMNED any time.
15360 15360 * After selecting the right ipif, under ill_lock make sure ipif is
15361 15361 * not condemned, and increment refcnt. If ipif is CONDEMNED,
15362 15362 * we retry. Inside the loop we still need to check for CONDEMNED,
15363 15363 * but not under a lock.
15364 15364 */
15365 15365 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
15366 15366 retry:
15367 15367 /*
15368 15368 * For source address selection, we treat the ipif list as circular
15369 15369 * and continue until we get back to where we started. This allows
15370 15370 * IPMP to vary source address selection (which improves inbound load
15371 15371 * spreading) by caching its last ending point and starting from
15372 15372 * there. NOTE: we don't have to worry about ill_src_ipif changing
15373 15373 * ills since that can't happen on the IPMP ill.
15374 15374 */
15375 15375 start_ipif = ill->ill_ipif;
15376 15376 if (IS_IPMP(ill) && ill->ill_src_ipif != NULL)
15377 15377 start_ipif = ill->ill_src_ipif;
15378 15378
15379 15379 ipif = start_ipif;
15380 15380 best_ipif = NULL;
15381 15381 best_type = IPIF_NONE;
15382 15382 do {
15383 15383 if ((next_ipif = ipif->ipif_next) == NULL)
15384 15384 next_ipif = ill->ill_ipif;
15385 15385
15386 15386 if (IPIF_IS_CONDEMNED(ipif))
15387 15387 continue;
15388 15388 /* Always skip NOLOCAL and ANYCAST interfaces */
15389 15389 if (ipif->ipif_flags & (IPIF_NOLOCAL|IPIF_ANYCAST))
15390 15390 continue;
15391 15391 /* Always skip NOACCEPT interfaces */
15392 15392 if (ipif->ipif_ill->ill_flags & ILLF_NOACCEPT)
15393 15393 continue;
15394 15394 if (!(ipif->ipif_flags & IPIF_UP))
15395 15395 continue;
15396 15396
15397 15397 if (!ipif->ipif_addr_ready) {
15398 15398 if (notreadyp != NULL)
15399 15399 *notreadyp = B_TRUE;
15400 15400 continue;
15401 15401 }
15402 15402
15403 15403 if (zoneid != ALL_ZONES &&
15404 15404 ipif->ipif_zoneid != zoneid &&
15405 15405 ipif->ipif_zoneid != ALL_ZONES)
15406 15406 continue;
15407 15407
15408 15408 /*
15409 15409 * Interfaces with 0.0.0.0 address are allowed to be UP, but
15410 15410 * are not valid as source addresses.
15411 15411 */
15412 15412 if (ipif->ipif_lcl_addr == INADDR_ANY)
15413 15413 continue;
15414 15414
15415 15415 /*
15416 15416 * Check compatibility of local address for destination's
15417 15417 * default label if we're on a labeled system. Incompatible
15418 15418 * addresses can't be used at all.
15419 15419 */
15420 15420 if (dst_rhtp != NULL) {
15421 15421 boolean_t incompat;
15422 15422
15423 15423 src_rhtp = find_tpc(&ipif->ipif_lcl_addr,
15424 15424 IPV4_VERSION, B_FALSE);
15425 15425 if (src_rhtp == NULL)
15426 15426 continue;
15427 15427 incompat = src_rhtp->tpc_tp.host_type != SUN_CIPSO ||
15428 15428 src_rhtp->tpc_tp.tp_doi !=
15429 15429 dst_rhtp->tpc_tp.tp_doi ||
15430 15430 (!_blinrange(&dst_rhtp->tpc_tp.tp_def_label,
15431 15431 &src_rhtp->tpc_tp.tp_sl_range_cipso) &&
15432 15432 !blinlset(&dst_rhtp->tpc_tp.tp_def_label,
15433 15433 src_rhtp->tpc_tp.tp_sl_set_cipso));
15434 15434 TPC_RELE(src_rhtp);
15435 15435 if (incompat)
15436 15436 continue;
15437 15437 }
15438 15438
15439 15439 samenet = ((ipif->ipif_net_mask & dst) == ipif->ipif_subnet);
15440 15440
15441 15441 if (ipif->ipif_lcl_addr == dst) {
15442 15442 type = IPIF_LOCALADDR;
15443 15443 } else if (ipif->ipif_flags & IPIF_DEPRECATED) {
15444 15444 type = samenet ? IPIF_SAMENET_DEPRECATED :
15445 15445 IPIF_DIFFNET_DEPRECATED;
15446 15446 } else if (ipif->ipif_zoneid == ALL_ZONES) {
15447 15447 type = samenet ? IPIF_SAMENET_ALLZONES :
15448 15448 IPIF_DIFFNET_ALLZONES;
15449 15449 } else {
15450 15450 type = samenet ? IPIF_SAMENET : IPIF_DIFFNET;
15451 15451 }
15452 15452
15453 15453 if (type > best_type) {
15454 15454 best_type = type;
15455 15455 best_ipif = ipif;
15456 15456 if (best_type == IPIF_LOCALADDR)
15457 15457 break; /* can't get better */
15458 15458 }
15459 15459 } while ((ipif = next_ipif) != start_ipif);
15460 15460
15461 15461 if ((ipif = best_ipif) != NULL) {
15462 15462 mutex_enter(&ipif->ipif_ill->ill_lock);
15463 15463 if (IPIF_IS_CONDEMNED(ipif)) {
15464 15464 mutex_exit(&ipif->ipif_ill->ill_lock);
15465 15465 goto retry;
15466 15466 }
15467 15467 ipif_refhold_locked(ipif);
15468 15468
15469 15469 /*
15470 15470 * For IPMP, update the source ipif rotor to the next ipif,
15471 15471 * provided we can look it up. (We must not use it if it's
15472 15472 * IPIF_CONDEMNED since we may have grabbed ill_g_lock after
15473 15473 * ipif_free() checked ill_src_ipif.)
15474 15474 */
15475 15475 if (IS_IPMP(ill) && ipif != NULL) {
15476 15476 next_ipif = ipif->ipif_next;
15477 15477 if (next_ipif != NULL && !IPIF_IS_CONDEMNED(next_ipif))
15478 15478 ill->ill_src_ipif = next_ipif;
15479 15479 else
15480 15480 ill->ill_src_ipif = NULL;
15481 15481 }
15482 15482 mutex_exit(&ipif->ipif_ill->ill_lock);
15483 15483 }
15484 15484
15485 15485 rw_exit(&ipst->ips_ill_g_lock);
15486 15486 if (usill != NULL)
15487 15487 ill_refrele(usill);
15488 15488 if (ipmp_ill != NULL)
15489 15489 ill_refrele(ipmp_ill);
15490 15490 if (dst_rhtp != NULL)
15491 15491 TPC_RELE(dst_rhtp);
15492 15492
15493 15493 #ifdef DEBUG
15494 15494 if (ipif == NULL) {
15495 15495 char buf1[INET6_ADDRSTRLEN];
15496 15496
15497 15497 ip1dbg(("ipif_select_source_v4(%s, %s) -> NULL\n",
15498 15498 ill->ill_name,
15499 15499 inet_ntop(AF_INET, &dst, buf1, sizeof (buf1))));
15500 15500 } else {
15501 15501 char buf1[INET6_ADDRSTRLEN];
15502 15502 char buf2[INET6_ADDRSTRLEN];
15503 15503
15504 15504 ip1dbg(("ipif_select_source_v4(%s, %s) -> %s\n",
15505 15505 ipif->ipif_ill->ill_name,
15506 15506 inet_ntop(AF_INET, &dst, buf1, sizeof (buf1)),
15507 15507 inet_ntop(AF_INET, &ipif->ipif_lcl_addr,
15508 15508 buf2, sizeof (buf2))));
15509 15509 }
15510 15510 #endif /* DEBUG */
15511 15511 return (ipif);
15512 15512 }
15513 15513
15514 15514 /*
15515 15515 * Pick a source address based on the destination ill and an optional setsrc
15516 15516 * address.
15517 15517 * The result is stored in srcp. If generation is set, then put the source
15518 15518 * generation number there before we look for the source address (to avoid
15519 15519 * missing changes in the set of source addresses.
15520 15520 * If flagsp is set, then us it to pass back ipif_flags.
15521 15521 *
15522 15522 * If the caller wants to cache the returned source address and detect when
15523 15523 * that might be stale, the caller should pass in a generation argument,
15524 15524 * which the caller can later compare against ips_src_generation
15525 15525 *
15526 15526 * The precedence order for selecting an IPv4 source address is:
15527 15527 * - RTF_SETSRC on the offlink ire always wins.
15528 15528 * - If usrsrc is set, swap the ill to be the usesrc one.
15529 15529 * - If IPMP is used on the ill, select a random address from the most
15530 15530 * preferred ones below:
15531 15531 * 1. If onlink destination, same subnet and not deprecated, not ALL_ZONES
15532 15532 * 2. Not deprecated, not ALL_ZONES
15533 15533 * 3. If onlink destination, same subnet and not deprecated, ALL_ZONES
15534 15534 * 4. Not deprecated, ALL_ZONES
15535 15535 * 5. If onlink destination, same subnet and deprecated
15536 15536 * 6. Deprecated.
15537 15537 *
15538 15538 * We have lower preference for ALL_ZONES IP addresses,
15539 15539 * as they pose problems with unlabeled destinations.
15540 15540 *
15541 15541 * Note that when multiple IP addresses match e.g., #1 we pick
15542 15542 * the first one if IPMP is not in use. With IPMP we randomize.
15543 15543 */
15544 15544 int
15545 15545 ip_select_source_v4(ill_t *ill, ipaddr_t setsrc, ipaddr_t dst,
15546 15546 ipaddr_t multicast_ifaddr,
15547 15547 zoneid_t zoneid, ip_stack_t *ipst, ipaddr_t *srcp,
15548 15548 uint32_t *generation, uint64_t *flagsp)
15549 15549 {
15550 15550 ipif_t *ipif;
15551 15551 boolean_t notready = B_FALSE; /* Set if !ipif_addr_ready found */
15552 15552
15553 15553 if (flagsp != NULL)
15554 15554 *flagsp = 0;
15555 15555
15556 15556 /*
15557 15557 * Need to grab the generation number before we check to
15558 15558 * avoid a race with a change to the set of local addresses.
15559 15559 * No lock needed since the thread which updates the set of local
15560 15560 * addresses use ipif/ill locks and exit those (hence a store memory
15561 15561 * barrier) before doing the atomic increase of ips_src_generation.
15562 15562 */
15563 15563 if (generation != NULL) {
15564 15564 *generation = ipst->ips_src_generation;
15565 15565 }
15566 15566
15567 15567 if (CLASSD(dst) && multicast_ifaddr != INADDR_ANY) {
15568 15568 *srcp = multicast_ifaddr;
15569 15569 return (0);
15570 15570 }
15571 15571
15572 15572 /* Was RTF_SETSRC set on the first IRE in the recursive lookup? */
15573 15573 if (setsrc != INADDR_ANY) {
15574 15574 *srcp = setsrc;
15575 15575 return (0);
15576 15576 }
15577 15577 ipif = ipif_select_source_v4(ill, dst, zoneid, B_TRUE, ¬ready);
15578 15578 if (ipif == NULL) {
15579 15579 if (notready)
15580 15580 return (ENETDOWN);
15581 15581 else
15582 15582 return (EADDRNOTAVAIL);
15583 15583 }
15584 15584 *srcp = ipif->ipif_lcl_addr;
15585 15585 if (flagsp != NULL)
15586 15586 *flagsp = ipif->ipif_flags;
15587 15587 ipif_refrele(ipif);
15588 15588 return (0);
15589 15589 }
15590 15590
15591 15591 /* ARGSUSED */
15592 15592 int
15593 15593 if_unitsel_restart(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15594 15594 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15595 15595 {
15596 15596 /*
15597 15597 * ill_phyint_reinit merged the v4 and v6 into a single
15598 15598 * ipsq. We might not have been able to complete the
15599 15599 * operation in ipif_set_values, if we could not become
15600 15600 * exclusive. If so restart it here.
15601 15601 */
15602 15602 return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15603 15603 }
15604 15604
15605 15605 /*
15606 15606 * Can operate on either a module or a driver queue.
15607 15607 * Returns an error if not a module queue.
15608 15608 */
15609 15609 /* ARGSUSED */
15610 15610 int
15611 15611 if_unitsel(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15612 15612 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15613 15613 {
15614 15614 queue_t *q1 = q;
15615 15615 char *cp;
15616 15616 char interf_name[LIFNAMSIZ];
15617 15617 uint_t ppa = *(uint_t *)mp->b_cont->b_cont->b_rptr;
15618 15618
15619 15619 if (q->q_next == NULL) {
15620 15620 ip1dbg((
15621 15621 "if_unitsel: IF_UNITSEL: no q_next\n"));
15622 15622 return (EINVAL);
15623 15623 }
15624 15624
15625 15625 if (((ill_t *)(q->q_ptr))->ill_name[0] != '\0')
15626 15626 return (EALREADY);
15627 15627
15628 15628 do {
15629 15629 q1 = q1->q_next;
15630 15630 } while (q1->q_next);
15631 15631 cp = q1->q_qinfo->qi_minfo->mi_idname;
15632 15632 (void) sprintf(interf_name, "%s%d", cp, ppa);
15633 15633
15634 15634 /*
15635 15635 * Here we are not going to delay the ioack until after
15636 15636 * ACKs from DL_ATTACH_REQ/DL_BIND_REQ. So no need to save the
15637 15637 * original ioctl message before sending the requests.
15638 15638 */
15639 15639 return (ipif_set_values(q, mp, interf_name, &ppa));
15640 15640 }
15641 15641
15642 15642 /* ARGSUSED */
15643 15643 int
15644 15644 ip_sioctl_sifname(ipif_t *dummy_ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
15645 15645 ip_ioctl_cmd_t *ipip, void *dummy_ifreq)
15646 15646 {
15647 15647 return (ENXIO);
15648 15648 }
15649 15649
15650 15650 /*
15651 15651 * Create any IRE_BROADCAST entries for `ipif', and store those entries in
15652 15652 * `irep'. Returns a pointer to the next free `irep' entry
15653 15653 * A mirror exists in ipif_delete_bcast_ires().
15654 15654 *
15655 15655 * The management of any "extra" or seemingly duplicate IRE_BROADCASTs is
15656 15656 * done in ire_add.
15657 15657 */
15658 15658 static ire_t **
15659 15659 ipif_create_bcast_ires(ipif_t *ipif, ire_t **irep)
15660 15660 {
15661 15661 ipaddr_t addr;
15662 15662 ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15663 15663 ipaddr_t subnetmask = ipif->ipif_net_mask;
15664 15664 ill_t *ill = ipif->ipif_ill;
15665 15665 zoneid_t zoneid = ipif->ipif_zoneid;
15666 15666
15667 15667 ip1dbg(("ipif_create_bcast_ires: creating broadcast IREs\n"));
15668 15668
15669 15669 ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15670 15670 ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15671 15671
15672 15672 if (ipif->ipif_lcl_addr == INADDR_ANY ||
15673 15673 (ipif->ipif_flags & IPIF_NOLOCAL))
15674 15674 netmask = htonl(IN_CLASSA_NET); /* fallback */
15675 15675
15676 15676 irep = ire_create_bcast(ill, 0, zoneid, irep);
15677 15677 irep = ire_create_bcast(ill, INADDR_BROADCAST, zoneid, irep);
15678 15678
15679 15679 /*
15680 15680 * For backward compatibility, we create net broadcast IREs based on
15681 15681 * the old "IP address class system", since some old machines only
15682 15682 * respond to these class derived net broadcast. However, we must not
15683 15683 * create these net broadcast IREs if the subnetmask is shorter than
15684 15684 * the IP address class based derived netmask. Otherwise, we may
15685 15685 * create a net broadcast address which is the same as an IP address
15686 15686 * on the subnet -- and then TCP will refuse to talk to that address.
15687 15687 */
15688 15688 if (netmask < subnetmask) {
15689 15689 addr = netmask & ipif->ipif_subnet;
15690 15690 irep = ire_create_bcast(ill, addr, zoneid, irep);
15691 15691 irep = ire_create_bcast(ill, ~netmask | addr, zoneid, irep);
15692 15692 }
15693 15693
15694 15694 /*
15695 15695 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15696 15696 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15697 15697 * created. Creating these broadcast IREs will only create confusion
15698 15698 * as `addr' will be the same as the IP address.
15699 15699 */
15700 15700 if (subnetmask != 0xFFFFFFFF) {
15701 15701 addr = ipif->ipif_subnet;
15702 15702 irep = ire_create_bcast(ill, addr, zoneid, irep);
15703 15703 irep = ire_create_bcast(ill, ~subnetmask | addr, zoneid, irep);
15704 15704 }
15705 15705
15706 15706 return (irep);
15707 15707 }
15708 15708
15709 15709 /*
15710 15710 * Mirror of ipif_create_bcast_ires()
15711 15711 */
15712 15712 static void
15713 15713 ipif_delete_bcast_ires(ipif_t *ipif)
15714 15714 {
15715 15715 ipaddr_t addr;
15716 15716 ipaddr_t netmask = ip_net_mask(ipif->ipif_lcl_addr);
15717 15717 ipaddr_t subnetmask = ipif->ipif_net_mask;
15718 15718 ill_t *ill = ipif->ipif_ill;
15719 15719 zoneid_t zoneid = ipif->ipif_zoneid;
15720 15720 ire_t *ire;
15721 15721
15722 15722 ASSERT(ipif->ipif_flags & IPIF_BROADCAST);
15723 15723 ASSERT(!(ipif->ipif_flags & IPIF_NOXMIT));
15724 15724
15725 15725 if (ipif->ipif_lcl_addr == INADDR_ANY ||
15726 15726 (ipif->ipif_flags & IPIF_NOLOCAL))
15727 15727 netmask = htonl(IN_CLASSA_NET); /* fallback */
15728 15728
15729 15729 ire = ire_lookup_bcast(ill, 0, zoneid);
15730 15730 ASSERT(ire != NULL);
15731 15731 ire_delete(ire); ire_refrele(ire);
15732 15732 ire = ire_lookup_bcast(ill, INADDR_BROADCAST, zoneid);
15733 15733 ASSERT(ire != NULL);
15734 15734 ire_delete(ire); ire_refrele(ire);
15735 15735
15736 15736 /*
15737 15737 * For backward compatibility, we create net broadcast IREs based on
15738 15738 * the old "IP address class system", since some old machines only
15739 15739 * respond to these class derived net broadcast. However, we must not
15740 15740 * create these net broadcast IREs if the subnetmask is shorter than
15741 15741 * the IP address class based derived netmask. Otherwise, we may
15742 15742 * create a net broadcast address which is the same as an IP address
15743 15743 * on the subnet -- and then TCP will refuse to talk to that address.
15744 15744 */
15745 15745 if (netmask < subnetmask) {
15746 15746 addr = netmask & ipif->ipif_subnet;
15747 15747 ire = ire_lookup_bcast(ill, addr, zoneid);
15748 15748 ASSERT(ire != NULL);
15749 15749 ire_delete(ire); ire_refrele(ire);
15750 15750 ire = ire_lookup_bcast(ill, ~netmask | addr, zoneid);
15751 15751 ASSERT(ire != NULL);
15752 15752 ire_delete(ire); ire_refrele(ire);
15753 15753 }
15754 15754
15755 15755 /*
15756 15756 * Don't create IRE_BROADCAST IREs for the interface if the subnetmask
15757 15757 * is 0xFFFFFFFF, as an IRE_LOCAL for that interface is already
15758 15758 * created. Creating these broadcast IREs will only create confusion
15759 15759 * as `addr' will be the same as the IP address.
15760 15760 */
15761 15761 if (subnetmask != 0xFFFFFFFF) {
15762 15762 addr = ipif->ipif_subnet;
15763 15763 ire = ire_lookup_bcast(ill, addr, zoneid);
15764 15764 ASSERT(ire != NULL);
15765 15765 ire_delete(ire); ire_refrele(ire);
15766 15766 ire = ire_lookup_bcast(ill, ~subnetmask | addr, zoneid);
15767 15767 ASSERT(ire != NULL);
15768 15768 ire_delete(ire); ire_refrele(ire);
15769 15769 }
15770 15770 }
15771 15771
15772 15772 /*
15773 15773 * Extract both the flags (including IFF_CANTCHANGE) such as IFF_IPV*
15774 15774 * from lifr_flags and the name from lifr_name.
15775 15775 * Set IFF_IPV* and ill_isv6 prior to doing the lookup
15776 15776 * since ipif_lookup_on_name uses the _isv6 flags when matching.
15777 15777 * Returns EINPROGRESS when mp has been consumed by queueing it on
15778 15778 * ipx_pending_mp and the ioctl will complete in ip_rput.
15779 15779 *
15780 15780 * Can operate on either a module or a driver queue.
15781 15781 * Returns an error if not a module queue.
15782 15782 */
15783 15783 /* ARGSUSED */
15784 15784 int
15785 15785 ip_sioctl_slifname(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15786 15786 ip_ioctl_cmd_t *ipip, void *if_req)
15787 15787 {
15788 15788 ill_t *ill = q->q_ptr;
15789 15789 phyint_t *phyi;
15790 15790 ip_stack_t *ipst;
15791 15791 struct lifreq *lifr = if_req;
15792 15792 uint64_t new_flags;
15793 15793
15794 15794 ASSERT(ipif != NULL);
15795 15795 ip1dbg(("ip_sioctl_slifname %s\n", lifr->lifr_name));
15796 15796
15797 15797 if (q->q_next == NULL) {
15798 15798 ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: no q_next\n"));
15799 15799 return (EINVAL);
15800 15800 }
15801 15801
15802 15802 /*
15803 15803 * If we are not writer on 'q' then this interface exists already
15804 15804 * and previous lookups (ip_extract_lifreq()) found this ipif --
15805 15805 * so return EALREADY.
15806 15806 */
15807 15807 if (ill != ipif->ipif_ill)
15808 15808 return (EALREADY);
15809 15809
15810 15810 if (ill->ill_name[0] != '\0')
15811 15811 return (EALREADY);
15812 15812
15813 15813 /*
15814 15814 * If there's another ill already with the requested name, ensure
15815 15815 * that it's of the same type. Otherwise, ill_phyint_reinit() will
15816 15816 * fuse together two unrelated ills, which will cause chaos.
15817 15817 */
15818 15818 ipst = ill->ill_ipst;
15819 15819 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
15820 15820 lifr->lifr_name, NULL);
15821 15821 if (phyi != NULL) {
15822 15822 ill_t *ill_mate = phyi->phyint_illv4;
15823 15823
15824 15824 if (ill_mate == NULL)
15825 15825 ill_mate = phyi->phyint_illv6;
15826 15826 ASSERT(ill_mate != NULL);
15827 15827
15828 15828 if (ill_mate->ill_media->ip_m_mac_type !=
15829 15829 ill->ill_media->ip_m_mac_type) {
15830 15830 ip1dbg(("if_sioctl_slifname: SIOCSLIFNAME: attempt to "
15831 15831 "use the same ill name on differing media\n"));
15832 15832 return (EINVAL);
15833 15833 }
15834 15834 }
15835 15835
15836 15836 /*
15837 15837 * We start off as IFF_IPV4 in ipif_allocate and become
15838 15838 * IFF_IPV4 or IFF_IPV6 here depending on lifr_flags value.
15839 15839 * The only flags that we read from user space are IFF_IPV4,
15840 15840 * IFF_IPV6, and IFF_BROADCAST.
15841 15841 *
15842 15842 * This ill has not been inserted into the global list.
15843 15843 * So we are still single threaded and don't need any lock
15844 15844 *
15845 15845 * Saniy check the flags.
15846 15846 */
15847 15847
15848 15848 if ((lifr->lifr_flags & IFF_BROADCAST) &&
15849 15849 ((lifr->lifr_flags & IFF_IPV6) ||
15850 15850 (!ill->ill_needs_attach && ill->ill_bcast_addr_length == 0))) {
15851 15851 ip1dbg(("ip_sioctl_slifname: link not broadcast capable "
15852 15852 "or IPv6 i.e., no broadcast \n"));
15853 15853 return (EINVAL);
15854 15854 }
15855 15855
15856 15856 new_flags =
15857 15857 lifr->lifr_flags & (IFF_IPV6|IFF_IPV4|IFF_BROADCAST);
15858 15858
15859 15859 if ((new_flags ^ (IFF_IPV6|IFF_IPV4)) == 0) {
15860 15860 ip1dbg(("ip_sioctl_slifname: flags must be exactly one of "
15861 15861 "IFF_IPV4 or IFF_IPV6\n"));
15862 15862 return (EINVAL);
15863 15863 }
15864 15864
15865 15865 /*
15866 15866 * We always start off as IPv4, so only need to check for IPv6.
15867 15867 */
15868 15868 if ((new_flags & IFF_IPV6) != 0) {
15869 15869 ill->ill_flags |= ILLF_IPV6;
15870 15870 ill->ill_flags &= ~ILLF_IPV4;
15871 15871
15872 15872 if (lifr->lifr_flags & IFF_NOLINKLOCAL)
15873 15873 ill->ill_flags |= ILLF_NOLINKLOCAL;
15874 15874 }
15875 15875
15876 15876 if ((new_flags & IFF_BROADCAST) != 0)
15877 15877 ipif->ipif_flags |= IPIF_BROADCAST;
15878 15878 else
15879 15879 ipif->ipif_flags &= ~IPIF_BROADCAST;
15880 15880
15881 15881 /* We started off as V4. */
15882 15882 if (ill->ill_flags & ILLF_IPV6) {
15883 15883 ill->ill_phyint->phyint_illv6 = ill;
15884 15884 ill->ill_phyint->phyint_illv4 = NULL;
15885 15885 }
15886 15886
15887 15887 return (ipif_set_values(q, mp, lifr->lifr_name, &lifr->lifr_ppa));
15888 15888 }
15889 15889
15890 15890 /* ARGSUSED */
15891 15891 int
15892 15892 ip_sioctl_slifname_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15893 15893 ip_ioctl_cmd_t *ipip, void *if_req)
15894 15894 {
15895 15895 /*
15896 15896 * ill_phyint_reinit merged the v4 and v6 into a single
15897 15897 * ipsq. We might not have been able to complete the
15898 15898 * slifname in ipif_set_values, if we could not become
15899 15899 * exclusive. If so restart it here
15900 15900 */
15901 15901 return (ipif_set_values_tail(ipif->ipif_ill, ipif, mp, q));
15902 15902 }
15903 15903
15904 15904 /*
15905 15905 * Return a pointer to the ipif which matches the index, IP version type and
15906 15906 * zoneid.
15907 15907 */
15908 15908 ipif_t *
15909 15909 ipif_lookup_on_ifindex(uint_t index, boolean_t isv6, zoneid_t zoneid,
15910 15910 ip_stack_t *ipst)
15911 15911 {
15912 15912 ill_t *ill;
15913 15913 ipif_t *ipif = NULL;
15914 15914
15915 15915 ill = ill_lookup_on_ifindex(index, isv6, ipst);
15916 15916 if (ill != NULL) {
15917 15917 mutex_enter(&ill->ill_lock);
15918 15918 for (ipif = ill->ill_ipif; ipif != NULL;
15919 15919 ipif = ipif->ipif_next) {
15920 15920 if (!IPIF_IS_CONDEMNED(ipif) && (zoneid == ALL_ZONES ||
15921 15921 zoneid == ipif->ipif_zoneid ||
15922 15922 ipif->ipif_zoneid == ALL_ZONES)) {
15923 15923 ipif_refhold_locked(ipif);
15924 15924 break;
15925 15925 }
15926 15926 }
15927 15927 mutex_exit(&ill->ill_lock);
15928 15928 ill_refrele(ill);
15929 15929 }
15930 15930 return (ipif);
15931 15931 }
15932 15932
15933 15933 /*
15934 15934 * Change an existing physical interface's index. If the new index
15935 15935 * is acceptable we update the index and the phyint_list_avl_by_index tree.
15936 15936 * Finally, we update other systems which may have a dependence on the
15937 15937 * index value.
15938 15938 */
15939 15939 /* ARGSUSED */
15940 15940 int
15941 15941 ip_sioctl_slifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
15942 15942 ip_ioctl_cmd_t *ipip, void *ifreq)
15943 15943 {
15944 15944 ill_t *ill;
15945 15945 phyint_t *phyi;
15946 15946 struct ifreq *ifr = (struct ifreq *)ifreq;
15947 15947 struct lifreq *lifr = (struct lifreq *)ifreq;
15948 15948 uint_t old_index, index;
15949 15949 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
15950 15950 avl_index_t where;
15951 15951
15952 15952 if (ipip->ipi_cmd_type == IF_CMD)
15953 15953 index = ifr->ifr_index;
15954 15954 else
15955 15955 index = lifr->lifr_index;
15956 15956
15957 15957 /*
15958 15958 * Only allow on physical interface. Also, index zero is illegal.
15959 15959 */
15960 15960 ill = ipif->ipif_ill;
15961 15961 phyi = ill->ill_phyint;
15962 15962 if (ipif->ipif_id != 0 || index == 0 || index > IF_INDEX_MAX) {
15963 15963 return (EINVAL);
15964 15964 }
15965 15965
15966 15966 /* If the index is not changing, no work to do */
15967 15967 if (phyi->phyint_ifindex == index)
15968 15968 return (0);
15969 15969
15970 15970 /*
15971 15971 * Use phyint_exists() to determine if the new interface index
15972 15972 * is already in use. If the index is unused then we need to
15973 15973 * change the phyint's position in the phyint_list_avl_by_index
15974 15974 * tree. If we do not do this, subsequent lookups (using the new
15975 15975 * index value) will not find the phyint.
15976 15976 */
15977 15977 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
15978 15978 if (phyint_exists(index, ipst)) {
15979 15979 rw_exit(&ipst->ips_ill_g_lock);
15980 15980 return (EEXIST);
15981 15981 }
15982 15982
15983 15983 /*
15984 15984 * The new index is unused. Set it in the phyint. However we must not
15985 15985 * forget to trigger NE_IFINDEX_CHANGE event before the ifindex
15986 15986 * changes. The event must be bound to old ifindex value.
15987 15987 */
15988 15988 ill_nic_event_dispatch(ill, 0, NE_IFINDEX_CHANGE,
15989 15989 &index, sizeof (index));
15990 15990
15991 15991 old_index = phyi->phyint_ifindex;
15992 15992 phyi->phyint_ifindex = index;
15993 15993
15994 15994 avl_remove(&ipst->ips_phyint_g_list->phyint_list_avl_by_index, phyi);
15995 15995 (void) avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15996 15996 &index, &where);
15997 15997 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
15998 15998 phyi, where);
15999 15999 rw_exit(&ipst->ips_ill_g_lock);
16000 16000
16001 16001 /* Update SCTP's ILL list */
16002 16002 sctp_ill_reindex(ill, old_index);
16003 16003
16004 16004 /* Send the routing sockets message */
16005 16005 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
16006 16006 if (ILL_OTHER(ill))
16007 16007 ip_rts_ifmsg(ILL_OTHER(ill)->ill_ipif, RTSQ_DEFAULT);
16008 16008
16009 16009 /* Perhaps ilgs should use this ill */
16010 16010 update_conn_ill(NULL, ill->ill_ipst);
16011 16011 return (0);
16012 16012 }
16013 16013
16014 16014 /* ARGSUSED */
16015 16015 int
16016 16016 ip_sioctl_get_lifindex(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16017 16017 ip_ioctl_cmd_t *ipip, void *ifreq)
16018 16018 {
16019 16019 struct ifreq *ifr = (struct ifreq *)ifreq;
16020 16020 struct lifreq *lifr = (struct lifreq *)ifreq;
16021 16021
16022 16022 ip1dbg(("ip_sioctl_get_lifindex(%s:%u %p)\n",
16023 16023 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16024 16024 /* Get the interface index */
16025 16025 if (ipip->ipi_cmd_type == IF_CMD) {
16026 16026 ifr->ifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16027 16027 } else {
16028 16028 lifr->lifr_index = ipif->ipif_ill->ill_phyint->phyint_ifindex;
16029 16029 }
16030 16030 return (0);
16031 16031 }
16032 16032
16033 16033 /* ARGSUSED */
16034 16034 int
16035 16035 ip_sioctl_get_lifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16036 16036 ip_ioctl_cmd_t *ipip, void *ifreq)
16037 16037 {
16038 16038 struct lifreq *lifr = (struct lifreq *)ifreq;
16039 16039
16040 16040 ip1dbg(("ip_sioctl_get_lifzone(%s:%u %p)\n",
16041 16041 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16042 16042 /* Get the interface zone */
16043 16043 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16044 16044 lifr->lifr_zoneid = ipif->ipif_zoneid;
16045 16045 return (0);
16046 16046 }
16047 16047
16048 16048 /*
16049 16049 * Set the zoneid of an interface.
16050 16050 */
16051 16051 /* ARGSUSED */
16052 16052 int
16053 16053 ip_sioctl_slifzone(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16054 16054 ip_ioctl_cmd_t *ipip, void *ifreq)
16055 16055 {
16056 16056 struct lifreq *lifr = (struct lifreq *)ifreq;
16057 16057 int err = 0;
16058 16058 boolean_t need_up = B_FALSE;
16059 16059 zone_t *zptr;
16060 16060 zone_status_t status;
16061 16061 zoneid_t zoneid;
16062 16062
16063 16063 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16064 16064 if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES) {
16065 16065 if (!is_system_labeled())
16066 16066 return (ENOTSUP);
16067 16067 zoneid = GLOBAL_ZONEID;
16068 16068 }
16069 16069
16070 16070 /* cannot assign instance zero to a non-global zone */
16071 16071 if (ipif->ipif_id == 0 && zoneid != GLOBAL_ZONEID)
16072 16072 return (ENOTSUP);
16073 16073
16074 16074 /*
16075 16075 * Cannot assign to a zone that doesn't exist or is shutting down. In
16076 16076 * the event of a race with the zone shutdown processing, since IP
16077 16077 * serializes this ioctl and SIOCGLIFCONF/SIOCLIFREMOVEIF, we know the
16078 16078 * interface will be cleaned up even if the zone is shut down
16079 16079 * immediately after the status check. If the interface can't be brought
16080 16080 * down right away, and the zone is shut down before the restart
16081 16081 * function is called, we resolve the possible races by rechecking the
16082 16082 * zone status in the restart function.
16083 16083 */
16084 16084 if ((zptr = zone_find_by_id(zoneid)) == NULL)
16085 16085 return (EINVAL);
16086 16086 status = zone_status_get(zptr);
16087 16087 zone_rele(zptr);
16088 16088
16089 16089 if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING)
16090 16090 return (EINVAL);
16091 16091
16092 16092 if (ipif->ipif_flags & IPIF_UP) {
16093 16093 /*
16094 16094 * If the interface is already marked up,
16095 16095 * we call ipif_down which will take care
16096 16096 * of ditching any IREs that have been set
16097 16097 * up based on the old interface address.
16098 16098 */
16099 16099 err = ipif_logical_down(ipif, q, mp);
16100 16100 if (err == EINPROGRESS)
16101 16101 return (err);
16102 16102 (void) ipif_down_tail(ipif);
16103 16103 need_up = B_TRUE;
16104 16104 }
16105 16105
16106 16106 err = ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp, need_up);
16107 16107 return (err);
16108 16108 }
16109 16109
16110 16110 static int
16111 16111 ip_sioctl_slifzone_tail(ipif_t *ipif, zoneid_t zoneid,
16112 16112 queue_t *q, mblk_t *mp, boolean_t need_up)
16113 16113 {
16114 16114 int err = 0;
16115 16115 ip_stack_t *ipst;
16116 16116
16117 16117 ip1dbg(("ip_sioctl_zoneid_tail(%s:%u %p)\n",
16118 16118 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16119 16119
16120 16120 if (CONN_Q(q))
16121 16121 ipst = CONNQ_TO_IPST(q);
16122 16122 else
16123 16123 ipst = ILLQ_TO_IPST(q);
16124 16124
16125 16125 /*
16126 16126 * For exclusive stacks we don't allow a different zoneid than
16127 16127 * global.
16128 16128 */
16129 16129 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID &&
16130 16130 zoneid != GLOBAL_ZONEID)
16131 16131 return (EINVAL);
16132 16132
16133 16133 /* Set the new zone id. */
16134 16134 ipif->ipif_zoneid = zoneid;
16135 16135
16136 16136 /* Update sctp list */
16137 16137 sctp_update_ipif(ipif, SCTP_IPIF_UPDATE);
16138 16138
16139 16139 /* The default multicast interface might have changed */
16140 16140 ire_increment_multicast_generation(ipst, ipif->ipif_ill->ill_isv6);
16141 16141
16142 16142 if (need_up) {
16143 16143 /*
16144 16144 * Now bring the interface back up. If this
16145 16145 * is the only IPIF for the ILL, ipif_up
16146 16146 * will have to re-bind to the device, so
16147 16147 * we may get back EINPROGRESS, in which
16148 16148 * case, this IOCTL will get completed in
16149 16149 * ip_rput_dlpi when we see the DL_BIND_ACK.
16150 16150 */
16151 16151 err = ipif_up(ipif, q, mp);
16152 16152 }
16153 16153 return (err);
16154 16154 }
16155 16155
16156 16156 /* ARGSUSED */
16157 16157 int
16158 16158 ip_sioctl_slifzone_restart(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16159 16159 ip_ioctl_cmd_t *ipip, void *if_req)
16160 16160 {
16161 16161 struct lifreq *lifr = (struct lifreq *)if_req;
16162 16162 zoneid_t zoneid;
16163 16163 zone_t *zptr;
16164 16164 zone_status_t status;
16165 16165
16166 16166 ASSERT(ipip->ipi_cmd_type == LIF_CMD);
16167 16167 if ((zoneid = lifr->lifr_zoneid) == ALL_ZONES)
16168 16168 zoneid = GLOBAL_ZONEID;
16169 16169
16170 16170 ip1dbg(("ip_sioctl_slifzone_restart(%s:%u %p)\n",
16171 16171 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16172 16172
16173 16173 /*
16174 16174 * We recheck the zone status to resolve the following race condition:
16175 16175 * 1) process sends SIOCSLIFZONE to put hme0:1 in zone "myzone";
16176 16176 * 2) hme0:1 is up and can't be brought down right away;
16177 16177 * ip_sioctl_slifzone() returns EINPROGRESS and the request is queued;
16178 16178 * 3) zone "myzone" is halted; the zone status switches to
16179 16179 * 'shutting_down' and the zones framework sends SIOCGLIFCONF to list
16180 16180 * the interfaces to remove - hme0:1 is not returned because it's not
16181 16181 * yet in "myzone", so it won't be removed;
16182 16182 * 4) the restart function for SIOCSLIFZONE is called; without the
16183 16183 * status check here, we would have hme0:1 in "myzone" after it's been
16184 16184 * destroyed.
16185 16185 * Note that if the status check fails, we need to bring the interface
16186 16186 * back to its state prior to ip_sioctl_slifzone(), hence the call to
16187 16187 * ipif_up_done[_v6]().
16188 16188 */
16189 16189 status = ZONE_IS_UNINITIALIZED;
16190 16190 if ((zptr = zone_find_by_id(zoneid)) != NULL) {
16191 16191 status = zone_status_get(zptr);
16192 16192 zone_rele(zptr);
16193 16193 }
16194 16194 if (status != ZONE_IS_READY && status != ZONE_IS_RUNNING) {
16195 16195 if (ipif->ipif_isv6) {
16196 16196 (void) ipif_up_done_v6(ipif);
16197 16197 } else {
16198 16198 (void) ipif_up_done(ipif);
16199 16199 }
16200 16200 return (EINVAL);
16201 16201 }
16202 16202
16203 16203 (void) ipif_down_tail(ipif);
16204 16204
16205 16205 return (ip_sioctl_slifzone_tail(ipif, lifr->lifr_zoneid, q, mp,
16206 16206 B_TRUE));
16207 16207 }
16208 16208
16209 16209 /*
16210 16210 * Return the number of addresses on `ill' with one or more of the values
16211 16211 * in `set' set and all of the values in `clear' clear.
16212 16212 */
16213 16213 static uint_t
16214 16214 ill_flagaddr_cnt(const ill_t *ill, uint64_t set, uint64_t clear)
16215 16215 {
16216 16216 ipif_t *ipif;
16217 16217 uint_t cnt = 0;
16218 16218
16219 16219 ASSERT(IAM_WRITER_ILL(ill));
16220 16220
16221 16221 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next)
16222 16222 if ((ipif->ipif_flags & set) && !(ipif->ipif_flags & clear))
16223 16223 cnt++;
16224 16224
16225 16225 return (cnt);
16226 16226 }
16227 16227
16228 16228 /*
16229 16229 * Return the number of migratable addresses on `ill' that are under
16230 16230 * application control.
16231 16231 */
16232 16232 uint_t
16233 16233 ill_appaddr_cnt(const ill_t *ill)
16234 16234 {
16235 16235 return (ill_flagaddr_cnt(ill, IPIF_DHCPRUNNING | IPIF_ADDRCONF,
16236 16236 IPIF_NOFAILOVER));
16237 16237 }
16238 16238
16239 16239 /*
16240 16240 * Return the number of point-to-point addresses on `ill'.
16241 16241 */
16242 16242 uint_t
16243 16243 ill_ptpaddr_cnt(const ill_t *ill)
16244 16244 {
16245 16245 return (ill_flagaddr_cnt(ill, IPIF_POINTOPOINT, 0));
16246 16246 }
16247 16247
16248 16248 /* ARGSUSED */
16249 16249 int
16250 16250 ip_sioctl_get_lifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16251 16251 ip_ioctl_cmd_t *ipip, void *ifreq)
16252 16252 {
16253 16253 struct lifreq *lifr = ifreq;
16254 16254
16255 16255 ASSERT(q->q_next == NULL);
16256 16256 ASSERT(CONN_Q(q));
16257 16257
16258 16258 ip1dbg(("ip_sioctl_get_lifusesrc(%s:%u %p)\n",
16259 16259 ipif->ipif_ill->ill_name, ipif->ipif_id, (void *)ipif));
16260 16260 lifr->lifr_index = ipif->ipif_ill->ill_usesrc_ifindex;
16261 16261 ip1dbg(("ip_sioctl_get_lifusesrc:lifr_index = %d\n", lifr->lifr_index));
16262 16262
16263 16263 return (0);
16264 16264 }
16265 16265
16266 16266 /* Find the previous ILL in this usesrc group */
16267 16267 static ill_t *
16268 16268 ill_prev_usesrc(ill_t *uill)
16269 16269 {
16270 16270 ill_t *ill;
16271 16271
16272 16272 for (ill = uill->ill_usesrc_grp_next;
16273 16273 ASSERT(ill), ill->ill_usesrc_grp_next != uill;
16274 16274 ill = ill->ill_usesrc_grp_next)
16275 16275 /* do nothing */;
16276 16276 return (ill);
16277 16277 }
16278 16278
16279 16279 /*
16280 16280 * Release all members of the usesrc group. This routine is called
16281 16281 * from ill_delete when the interface being unplumbed is the
16282 16282 * group head.
16283 16283 *
16284 16284 * This silently clears the usesrc that ifconfig setup.
16285 16285 * An alternative would be to keep that ifindex, and drop packets on the floor
16286 16286 * since no source address can be selected.
16287 16287 * Even if we keep the current semantics, don't need a lock and a linked list.
16288 16288 * Can walk all the ills checking if they have a ill_usesrc_ifindex matching
16289 16289 * the one that is being removed. Issue is how we return the usesrc users
16290 16290 * (SIOCGLIFSRCOF). We want to be able to find the ills which have an
16291 16291 * ill_usesrc_ifindex matching a target ill. We could also do that with an
16292 16292 * ill walk, but the walker would need to insert in the ioctl response.
16293 16293 */
16294 16294 static void
16295 16295 ill_disband_usesrc_group(ill_t *uill)
16296 16296 {
16297 16297 ill_t *next_ill, *tmp_ill;
16298 16298 ip_stack_t *ipst = uill->ill_ipst;
16299 16299
16300 16300 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16301 16301 next_ill = uill->ill_usesrc_grp_next;
16302 16302
16303 16303 do {
16304 16304 ASSERT(next_ill != NULL);
16305 16305 tmp_ill = next_ill->ill_usesrc_grp_next;
16306 16306 ASSERT(tmp_ill != NULL);
16307 16307 next_ill->ill_usesrc_grp_next = NULL;
16308 16308 next_ill->ill_usesrc_ifindex = 0;
16309 16309 next_ill = tmp_ill;
16310 16310 } while (next_ill->ill_usesrc_ifindex != 0);
16311 16311 uill->ill_usesrc_grp_next = NULL;
16312 16312 }
16313 16313
16314 16314 /*
16315 16315 * Remove the client usesrc ILL from the list and relink to a new list
16316 16316 */
16317 16317 int
16318 16318 ill_relink_usesrc_ills(ill_t *ucill, ill_t *uill, uint_t ifindex)
16319 16319 {
16320 16320 ill_t *ill, *tmp_ill;
16321 16321 ip_stack_t *ipst = ucill->ill_ipst;
16322 16322
16323 16323 ASSERT((ucill != NULL) && (ucill->ill_usesrc_grp_next != NULL) &&
16324 16324 (uill != NULL) && RW_WRITE_HELD(&ipst->ips_ill_g_usesrc_lock));
16325 16325
16326 16326 /*
16327 16327 * Check if the usesrc client ILL passed in is not already
16328 16328 * in use as a usesrc ILL i.e one whose source address is
16329 16329 * in use OR a usesrc ILL is not already in use as a usesrc
16330 16330 * client ILL
16331 16331 */
16332 16332 if ((ucill->ill_usesrc_ifindex == 0) ||
16333 16333 (uill->ill_usesrc_ifindex != 0)) {
16334 16334 return (-1);
16335 16335 }
16336 16336
16337 16337 ill = ill_prev_usesrc(ucill);
16338 16338 ASSERT(ill->ill_usesrc_grp_next != NULL);
16339 16339
16340 16340 /* Remove from the current list */
16341 16341 if (ill->ill_usesrc_grp_next->ill_usesrc_grp_next == ill) {
16342 16342 /* Only two elements in the list */
16343 16343 ASSERT(ill->ill_usesrc_ifindex == 0);
16344 16344 ill->ill_usesrc_grp_next = NULL;
16345 16345 } else {
16346 16346 ill->ill_usesrc_grp_next = ucill->ill_usesrc_grp_next;
16347 16347 }
16348 16348
16349 16349 if (ifindex == 0) {
16350 16350 ucill->ill_usesrc_ifindex = 0;
16351 16351 ucill->ill_usesrc_grp_next = NULL;
16352 16352 return (0);
16353 16353 }
16354 16354
16355 16355 ucill->ill_usesrc_ifindex = ifindex;
16356 16356 tmp_ill = uill->ill_usesrc_grp_next;
16357 16357 uill->ill_usesrc_grp_next = ucill;
16358 16358 ucill->ill_usesrc_grp_next =
16359 16359 (tmp_ill != NULL) ? tmp_ill : uill;
16360 16360 return (0);
16361 16361 }
16362 16362
16363 16363 /*
16364 16364 * Set the ill_usesrc and ill_usesrc_head fields. See synchronization notes in
16365 16365 * ip.c for locking details.
16366 16366 */
16367 16367 /* ARGSUSED */
16368 16368 int
16369 16369 ip_sioctl_slifusesrc(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16370 16370 ip_ioctl_cmd_t *ipip, void *ifreq)
16371 16371 {
16372 16372 struct lifreq *lifr = (struct lifreq *)ifreq;
16373 16373 boolean_t isv6 = B_FALSE, reset_flg = B_FALSE;
16374 16374 ill_t *usesrc_ill, *usesrc_cli_ill = ipif->ipif_ill;
16375 16375 int err = 0, ret;
16376 16376 uint_t ifindex;
16377 16377 ipsq_t *ipsq = NULL;
16378 16378 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
16379 16379
16380 16380 ASSERT(IAM_WRITER_IPIF(ipif));
16381 16381 ASSERT(q->q_next == NULL);
16382 16382 ASSERT(CONN_Q(q));
16383 16383
16384 16384 isv6 = (Q_TO_CONN(q))->conn_family == AF_INET6;
16385 16385
16386 16386 ifindex = lifr->lifr_index;
16387 16387 if (ifindex == 0) {
16388 16388 if (usesrc_cli_ill->ill_usesrc_grp_next == NULL) {
16389 16389 /* non usesrc group interface, nothing to reset */
16390 16390 return (0);
16391 16391 }
16392 16392 ifindex = usesrc_cli_ill->ill_usesrc_ifindex;
16393 16393 /* valid reset request */
16394 16394 reset_flg = B_TRUE;
16395 16395 }
16396 16396
16397 16397 usesrc_ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
16398 16398 if (usesrc_ill == NULL)
16399 16399 return (ENXIO);
16400 16400 if (usesrc_ill == ipif->ipif_ill) {
16401 16401 ill_refrele(usesrc_ill);
16402 16402 return (EINVAL);
16403 16403 }
16404 16404
16405 16405 ipsq = ipsq_try_enter(NULL, usesrc_ill, q, mp, ip_process_ioctl,
16406 16406 NEW_OP, B_TRUE);
16407 16407 if (ipsq == NULL) {
16408 16408 err = EINPROGRESS;
16409 16409 /* Operation enqueued on the ipsq of the usesrc ILL */
16410 16410 goto done;
16411 16411 }
16412 16412
16413 16413 /* USESRC isn't currently supported with IPMP */
16414 16414 if (IS_IPMP(usesrc_ill) || IS_UNDER_IPMP(usesrc_ill)) {
16415 16415 err = ENOTSUP;
16416 16416 goto done;
16417 16417 }
16418 16418
16419 16419 /*
16420 16420 * USESRC isn't compatible with the STANDBY flag. (STANDBY is only
16421 16421 * used by IPMP underlying interfaces, but someone might think it's
16422 16422 * more general and try to use it independently with VNI.)
16423 16423 */
16424 16424 if (usesrc_ill->ill_phyint->phyint_flags & PHYI_STANDBY) {
16425 16425 err = ENOTSUP;
16426 16426 goto done;
16427 16427 }
16428 16428
16429 16429 /*
16430 16430 * If the client is already in use as a usesrc_ill or a usesrc_ill is
16431 16431 * already a client then return EINVAL
16432 16432 */
16433 16433 if (IS_USESRC_ILL(usesrc_cli_ill) || IS_USESRC_CLI_ILL(usesrc_ill)) {
16434 16434 err = EINVAL;
16435 16435 goto done;
16436 16436 }
16437 16437
16438 16438 /*
16439 16439 * If the ill_usesrc_ifindex field is already set to what it needs to
16440 16440 * be then this is a duplicate operation.
16441 16441 */
16442 16442 if (!reset_flg && usesrc_cli_ill->ill_usesrc_ifindex == ifindex) {
16443 16443 err = 0;
16444 16444 goto done;
16445 16445 }
16446 16446
16447 16447 ip1dbg(("ip_sioctl_slifusesrc: usesrc_cli_ill %s, usesrc_ill %s,"
16448 16448 " v6 = %d", usesrc_cli_ill->ill_name, usesrc_ill->ill_name,
16449 16449 usesrc_ill->ill_isv6));
16450 16450
16451 16451 /*
16452 16452 * ill_g_usesrc_lock global lock protects the ill_usesrc_grp_next
16453 16453 * and the ill_usesrc_ifindex fields
16454 16454 */
16455 16455 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
16456 16456
16457 16457 if (reset_flg) {
16458 16458 ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill, 0);
16459 16459 if (ret != 0) {
16460 16460 err = EINVAL;
16461 16461 }
16462 16462 rw_exit(&ipst->ips_ill_g_usesrc_lock);
16463 16463 goto done;
16464 16464 }
16465 16465
16466 16466 /*
16467 16467 * Four possibilities to consider:
16468 16468 * 1. Both usesrc_ill and usesrc_cli_ill are not part of any usesrc grp
16469 16469 * 2. usesrc_ill is part of a group but usesrc_cli_ill isn't
16470 16470 * 3. usesrc_cli_ill is part of a group but usesrc_ill isn't
16471 16471 * 4. Both are part of their respective usesrc groups
16472 16472 */
16473 16473 if ((usesrc_ill->ill_usesrc_grp_next == NULL) &&
16474 16474 (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16475 16475 ASSERT(usesrc_ill->ill_usesrc_ifindex == 0);
16476 16476 usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16477 16477 usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16478 16478 usesrc_cli_ill->ill_usesrc_grp_next = usesrc_ill;
16479 16479 } else if ((usesrc_ill->ill_usesrc_grp_next != NULL) &&
16480 16480 (usesrc_cli_ill->ill_usesrc_grp_next == NULL)) {
16481 16481 usesrc_cli_ill->ill_usesrc_ifindex = ifindex;
16482 16482 /* Insert at head of list */
16483 16483 usesrc_cli_ill->ill_usesrc_grp_next =
16484 16484 usesrc_ill->ill_usesrc_grp_next;
16485 16485 usesrc_ill->ill_usesrc_grp_next = usesrc_cli_ill;
16486 16486 } else {
16487 16487 ret = ill_relink_usesrc_ills(usesrc_cli_ill, usesrc_ill,
16488 16488 ifindex);
16489 16489 if (ret != 0)
16490 16490 err = EINVAL;
16491 16491 }
16492 16492 rw_exit(&ipst->ips_ill_g_usesrc_lock);
16493 16493
16494 16494 done:
16495 16495 if (ipsq != NULL)
16496 16496 ipsq_exit(ipsq);
16497 16497 /* The refrele on the lifr_name ipif is done by ip_process_ioctl */
16498 16498 ill_refrele(usesrc_ill);
16499 16499
16500 16500 /* Let conn_ixa caching know that source address selection changed */
16501 16501 ip_update_source_selection(ipst);
16502 16502
16503 16503 return (err);
16504 16504 }
16505 16505
16506 16506 /* ARGSUSED */
16507 16507 int
16508 16508 ip_sioctl_get_dadstate(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
16509 16509 ip_ioctl_cmd_t *ipip, void *if_req)
16510 16510 {
16511 16511 struct lifreq *lifr = (struct lifreq *)if_req;
16512 16512 ill_t *ill = ipif->ipif_ill;
16513 16513
16514 16514 /*
16515 16515 * Need a lock since IFF_UP can be set even when there are
16516 16516 * references to the ipif.
16517 16517 */
16518 16518 mutex_enter(&ill->ill_lock);
16519 16519 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_addr_ready == 0)
16520 16520 lifr->lifr_dadstate = DAD_IN_PROGRESS;
16521 16521 else
16522 16522 lifr->lifr_dadstate = DAD_DONE;
16523 16523 mutex_exit(&ill->ill_lock);
16524 16524 return (0);
16525 16525 }
16526 16526
16527 16527 /*
16528 16528 * comparison function used by avl.
16529 16529 */
16530 16530 static int
16531 16531 ill_phyint_compare_index(const void *index_ptr, const void *phyip)
16532 16532 {
16533 16533
16534 16534 uint_t index;
16535 16535
16536 16536 ASSERT(phyip != NULL && index_ptr != NULL);
16537 16537
16538 16538 index = *((uint_t *)index_ptr);
16539 16539 /*
16540 16540 * let the phyint with the lowest index be on top.
16541 16541 */
16542 16542 if (((phyint_t *)phyip)->phyint_ifindex < index)
16543 16543 return (1);
16544 16544 if (((phyint_t *)phyip)->phyint_ifindex > index)
16545 16545 return (-1);
16546 16546 return (0);
16547 16547 }
16548 16548
16549 16549 /*
16550 16550 * comparison function used by avl.
16551 16551 */
16552 16552 static int
16553 16553 ill_phyint_compare_name(const void *name_ptr, const void *phyip)
16554 16554 {
16555 16555 ill_t *ill;
16556 16556 int res = 0;
16557 16557
16558 16558 ASSERT(phyip != NULL && name_ptr != NULL);
16559 16559
16560 16560 if (((phyint_t *)phyip)->phyint_illv4)
16561 16561 ill = ((phyint_t *)phyip)->phyint_illv4;
16562 16562 else
16563 16563 ill = ((phyint_t *)phyip)->phyint_illv6;
16564 16564 ASSERT(ill != NULL);
16565 16565
16566 16566 res = strcmp(ill->ill_name, (char *)name_ptr);
16567 16567 if (res > 0)
16568 16568 return (1);
16569 16569 else if (res < 0)
16570 16570 return (-1);
16571 16571 return (0);
16572 16572 }
16573 16573
16574 16574 /*
16575 16575 * This function is called on the unplumb path via ill_glist_delete() when
16576 16576 * there are no ills left on the phyint and thus the phyint can be freed.
16577 16577 */
16578 16578 static void
16579 16579 phyint_free(phyint_t *phyi)
16580 16580 {
16581 16581 ip_stack_t *ipst = PHYINT_TO_IPST(phyi);
16582 16582
16583 16583 ASSERT(phyi->phyint_illv4 == NULL && phyi->phyint_illv6 == NULL);
16584 16584
16585 16585 /*
16586 16586 * If this phyint was an IPMP meta-interface, blow away the group.
16587 16587 * This is safe to do because all of the illgrps have already been
16588 16588 * removed by I_PUNLINK, and thus SIOCSLIFGROUPNAME cannot find us.
16589 16589 * If we're cleaning up as a result of failed initialization,
16590 16590 * phyint_grp may be NULL.
16591 16591 */
16592 16592 if ((phyi->phyint_flags & PHYI_IPMP) && (phyi->phyint_grp != NULL)) {
16593 16593 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16594 16594 ipmp_grp_destroy(phyi->phyint_grp);
16595 16595 phyi->phyint_grp = NULL;
16596 16596 rw_exit(&ipst->ips_ipmp_lock);
16597 16597 }
16598 16598
16599 16599 /*
16600 16600 * If this interface was under IPMP, take it out of the group.
16601 16601 */
16602 16602 if (phyi->phyint_grp != NULL)
16603 16603 ipmp_phyint_leave_grp(phyi);
16604 16604
16605 16605 /*
16606 16606 * Delete the phyint and disassociate its ipsq. The ipsq itself
16607 16607 * will be freed in ipsq_exit().
16608 16608 */
16609 16609 phyi->phyint_ipsq->ipsq_phyint = NULL;
16610 16610 phyi->phyint_name[0] = '\0';
16611 16611
16612 16612 mi_free(phyi);
16613 16613 }
16614 16614
16615 16615 /*
16616 16616 * Attach the ill to the phyint structure which can be shared by both
16617 16617 * IPv4 and IPv6 ill. ill_init allocates a phyint to just hold flags. This
16618 16618 * function is called from ipif_set_values and ill_lookup_on_name (for
16619 16619 * loopback) where we know the name of the ill. We lookup the ill and if
16620 16620 * there is one present already with the name use that phyint. Otherwise
16621 16621 * reuse the one allocated by ill_init.
16622 16622 */
16623 16623 static void
16624 16624 ill_phyint_reinit(ill_t *ill)
16625 16625 {
16626 16626 boolean_t isv6 = ill->ill_isv6;
16627 16627 phyint_t *phyi_old;
16628 16628 phyint_t *phyi;
16629 16629 avl_index_t where = 0;
16630 16630 ill_t *ill_other = NULL;
16631 16631 ip_stack_t *ipst = ill->ill_ipst;
16632 16632
16633 16633 ASSERT(RW_WRITE_HELD(&ipst->ips_ill_g_lock));
16634 16634
16635 16635 phyi_old = ill->ill_phyint;
16636 16636 ASSERT(isv6 || (phyi_old->phyint_illv4 == ill &&
16637 16637 phyi_old->phyint_illv6 == NULL));
16638 16638 ASSERT(!isv6 || (phyi_old->phyint_illv6 == ill &&
16639 16639 phyi_old->phyint_illv4 == NULL));
16640 16640 ASSERT(phyi_old->phyint_ifindex == 0);
16641 16641
16642 16642 /*
16643 16643 * Now that our ill has a name, set it in the phyint.
16644 16644 */
16645 16645 (void) strlcpy(ill->ill_phyint->phyint_name, ill->ill_name, LIFNAMSIZ);
16646 16646
16647 16647 phyi = avl_find(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16648 16648 ill->ill_name, &where);
16649 16649
16650 16650 /*
16651 16651 * 1. We grabbed the ill_g_lock before inserting this ill into
16652 16652 * the global list of ills. So no other thread could have located
16653 16653 * this ill and hence the ipsq of this ill is guaranteed to be empty.
16654 16654 * 2. Now locate the other protocol instance of this ill.
16655 16655 * 3. Now grab both ill locks in the right order, and the phyint lock of
16656 16656 * the new ipsq. Holding ill locks + ill_g_lock ensures that the ipsq
16657 16657 * of neither ill can change.
16658 16658 * 4. Merge the phyint and thus the ipsq as well of this ill onto the
16659 16659 * other ill.
16660 16660 * 5. Release all locks.
16661 16661 */
16662 16662
16663 16663 /*
16664 16664 * Look for IPv4 if we are initializing IPv6 or look for IPv6 if
16665 16665 * we are initializing IPv4.
16666 16666 */
16667 16667 if (phyi != NULL) {
16668 16668 ill_other = (isv6) ? phyi->phyint_illv4 : phyi->phyint_illv6;
16669 16669 ASSERT(ill_other->ill_phyint != NULL);
16670 16670 ASSERT((isv6 && !ill_other->ill_isv6) ||
16671 16671 (!isv6 && ill_other->ill_isv6));
16672 16672 GRAB_ILL_LOCKS(ill, ill_other);
16673 16673 /*
16674 16674 * We are potentially throwing away phyint_flags which
16675 16675 * could be different from the one that we obtain from
16676 16676 * ill_other->ill_phyint. But it is okay as we are assuming
16677 16677 * that the state maintained within IP is correct.
16678 16678 */
16679 16679 mutex_enter(&phyi->phyint_lock);
16680 16680 if (isv6) {
16681 16681 ASSERT(phyi->phyint_illv6 == NULL);
16682 16682 phyi->phyint_illv6 = ill;
16683 16683 } else {
16684 16684 ASSERT(phyi->phyint_illv4 == NULL);
16685 16685 phyi->phyint_illv4 = ill;
16686 16686 }
16687 16687
16688 16688 /*
16689 16689 * Delete the old phyint and make its ipsq eligible
16690 16690 * to be freed in ipsq_exit().
16691 16691 */
16692 16692 phyi_old->phyint_illv4 = NULL;
16693 16693 phyi_old->phyint_illv6 = NULL;
16694 16694 phyi_old->phyint_ipsq->ipsq_phyint = NULL;
16695 16695 phyi_old->phyint_name[0] = '\0';
16696 16696 mi_free(phyi_old);
16697 16697 } else {
16698 16698 mutex_enter(&ill->ill_lock);
16699 16699 /*
16700 16700 * We don't need to acquire any lock, since
16701 16701 * the ill is not yet visible globally and we
16702 16702 * have not yet released the ill_g_lock.
16703 16703 */
16704 16704 phyi = phyi_old;
16705 16705 mutex_enter(&phyi->phyint_lock);
16706 16706 /* XXX We need a recovery strategy here. */
16707 16707 if (!phyint_assign_ifindex(phyi, ipst))
16708 16708 cmn_err(CE_PANIC, "phyint_assign_ifindex() failed");
16709 16709
16710 16710 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
16711 16711 (void *)phyi, where);
16712 16712
16713 16713 (void) avl_find(&ipst->ips_phyint_g_list->
16714 16714 phyint_list_avl_by_index,
16715 16715 &phyi->phyint_ifindex, &where);
16716 16716 avl_insert(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
16717 16717 (void *)phyi, where);
16718 16718 }
16719 16719
16720 16720 /*
16721 16721 * Reassigning ill_phyint automatically reassigns the ipsq also.
16722 16722 * pending mp is not affected because that is per ill basis.
16723 16723 */
16724 16724 ill->ill_phyint = phyi;
16725 16725
16726 16726 /*
16727 16727 * Now that the phyint's ifindex has been assigned, complete the
16728 16728 * remaining
16729 16729 */
16730 16730 ill->ill_ip_mib->ipIfStatsIfIndex = ill->ill_phyint->phyint_ifindex;
16731 16731 if (ill->ill_isv6) {
16732 16732 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
16733 16733 ill->ill_phyint->phyint_ifindex;
16734 16734 ill->ill_mcast_type = ipst->ips_mld_max_version;
16735 16735 } else {
16736 16736 ill->ill_mcast_type = ipst->ips_igmp_max_version;
16737 16737 }
16738 16738
16739 16739 /*
16740 16740 * Generate an event within the hooks framework to indicate that
16741 16741 * a new interface has just been added to IP. For this event to
16742 16742 * be generated, the network interface must, at least, have an
16743 16743 * ifindex assigned to it. (We don't generate the event for
16744 16744 * loopback since ill_lookup_on_name() has its own NE_PLUMB event.)
16745 16745 *
16746 16746 * This needs to be run inside the ill_g_lock perimeter to ensure
16747 16747 * that the ordering of delivered events to listeners matches the
16748 16748 * order of them in the kernel.
16749 16749 */
16750 16750 if (!IS_LOOPBACK(ill)) {
16751 16751 ill_nic_event_dispatch(ill, 0, NE_PLUMB, ill->ill_name,
16752 16752 ill->ill_name_length);
16753 16753 }
16754 16754 RELEASE_ILL_LOCKS(ill, ill_other);
16755 16755 mutex_exit(&phyi->phyint_lock);
16756 16756 }
16757 16757
16758 16758 /*
16759 16759 * Notify any downstream modules of the name of this interface.
16760 16760 * An M_IOCTL is used even though we don't expect a successful reply.
16761 16761 * Any reply message from the driver (presumably an M_IOCNAK) will
16762 16762 * eventually get discarded somewhere upstream. The message format is
16763 16763 * simply an SIOCSLIFNAME ioctl just as might be sent from ifconfig
16764 16764 * to IP.
16765 16765 */
16766 16766 static void
16767 16767 ip_ifname_notify(ill_t *ill, queue_t *q)
16768 16768 {
16769 16769 mblk_t *mp1, *mp2;
16770 16770 struct iocblk *iocp;
16771 16771 struct lifreq *lifr;
16772 16772
16773 16773 mp1 = mkiocb(SIOCSLIFNAME);
16774 16774 if (mp1 == NULL)
16775 16775 return;
16776 16776 mp2 = allocb(sizeof (struct lifreq), BPRI_HI);
16777 16777 if (mp2 == NULL) {
16778 16778 freeb(mp1);
16779 16779 return;
16780 16780 }
16781 16781
16782 16782 mp1->b_cont = mp2;
16783 16783 iocp = (struct iocblk *)mp1->b_rptr;
16784 16784 iocp->ioc_count = sizeof (struct lifreq);
16785 16785
16786 16786 lifr = (struct lifreq *)mp2->b_rptr;
16787 16787 mp2->b_wptr += sizeof (struct lifreq);
16788 16788 bzero(lifr, sizeof (struct lifreq));
16789 16789
16790 16790 (void) strncpy(lifr->lifr_name, ill->ill_name, LIFNAMSIZ);
16791 16791 lifr->lifr_ppa = ill->ill_ppa;
16792 16792 lifr->lifr_flags = (ill->ill_flags & (ILLF_IPV4|ILLF_IPV6));
16793 16793
16794 16794 DTRACE_PROBE3(ill__dlpi, char *, "ip_ifname_notify",
16795 16795 char *, "SIOCSLIFNAME", ill_t *, ill);
16796 16796 putnext(q, mp1);
16797 16797 }
16798 16798
16799 16799 static int
16800 16800 ipif_set_values_tail(ill_t *ill, ipif_t *ipif, mblk_t *mp, queue_t *q)
16801 16801 {
16802 16802 int err;
16803 16803 ip_stack_t *ipst = ill->ill_ipst;
16804 16804 phyint_t *phyi = ill->ill_phyint;
16805 16805
16806 16806 /*
16807 16807 * Now that ill_name is set, the configuration for the IPMP
16808 16808 * meta-interface can be performed.
16809 16809 */
16810 16810 if (IS_IPMP(ill)) {
16811 16811 rw_enter(&ipst->ips_ipmp_lock, RW_WRITER);
16812 16812 /*
16813 16813 * If phyi->phyint_grp is NULL, then this is the first IPMP
16814 16814 * meta-interface and we need to create the IPMP group.
16815 16815 */
16816 16816 if (phyi->phyint_grp == NULL) {
16817 16817 /*
16818 16818 * If someone has renamed another IPMP group to have
16819 16819 * the same name as our interface, bail.
16820 16820 */
16821 16821 if (ipmp_grp_lookup(ill->ill_name, ipst) != NULL) {
16822 16822 rw_exit(&ipst->ips_ipmp_lock);
16823 16823 return (EEXIST);
16824 16824 }
16825 16825 phyi->phyint_grp = ipmp_grp_create(ill->ill_name, phyi);
16826 16826 if (phyi->phyint_grp == NULL) {
16827 16827 rw_exit(&ipst->ips_ipmp_lock);
16828 16828 return (ENOMEM);
16829 16829 }
16830 16830 }
16831 16831 rw_exit(&ipst->ips_ipmp_lock);
16832 16832 }
16833 16833
16834 16834 /* Tell downstream modules where they are. */
16835 16835 ip_ifname_notify(ill, q);
16836 16836
16837 16837 /*
16838 16838 * ill_dl_phys returns EINPROGRESS in the usual case.
16839 16839 * Error cases are ENOMEM ...
16840 16840 */
16841 16841 err = ill_dl_phys(ill, ipif, mp, q);
16842 16842
16843 16843 if (ill->ill_isv6) {
16844 16844 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
16845 16845 if (ipst->ips_mld_slowtimeout_id == 0) {
16846 16846 ipst->ips_mld_slowtimeout_id = timeout(mld_slowtimo,
16847 16847 (void *)ipst,
16848 16848 MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16849 16849 }
16850 16850 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
16851 16851 } else {
16852 16852 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
16853 16853 if (ipst->ips_igmp_slowtimeout_id == 0) {
16854 16854 ipst->ips_igmp_slowtimeout_id = timeout(igmp_slowtimo,
16855 16855 (void *)ipst,
16856 16856 MSEC_TO_TICK(MCAST_SLOWTIMO_INTERVAL));
16857 16857 }
16858 16858 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
16859 16859 }
16860 16860
16861 16861 return (err);
16862 16862 }
16863 16863
16864 16864 /*
16865 16865 * Common routine for ppa and ifname setting. Should be called exclusive.
16866 16866 *
16867 16867 * Returns EINPROGRESS when mp has been consumed by queueing it on
16868 16868 * ipx_pending_mp and the ioctl will complete in ip_rput.
16869 16869 *
16870 16870 * NOTE : If ppa is UNIT_MAX, we assign the next valid ppa and return
16871 16871 * the new name and new ppa in lifr_name and lifr_ppa respectively.
16872 16872 * For SLIFNAME, we pass these values back to the userland.
16873 16873 */
16874 16874 static int
16875 16875 ipif_set_values(queue_t *q, mblk_t *mp, char *interf_name, uint_t *new_ppa_ptr)
16876 16876 {
16877 16877 ill_t *ill;
16878 16878 ipif_t *ipif;
16879 16879 ipsq_t *ipsq;
16880 16880 char *ppa_ptr;
16881 16881 char *old_ptr;
16882 16882 char old_char;
16883 16883 int error;
16884 16884 ip_stack_t *ipst;
16885 16885
16886 16886 ip1dbg(("ipif_set_values: interface %s\n", interf_name));
16887 16887 ASSERT(q->q_next != NULL);
16888 16888 ASSERT(interf_name != NULL);
16889 16889
16890 16890 ill = (ill_t *)q->q_ptr;
16891 16891 ipst = ill->ill_ipst;
16892 16892
16893 16893 ASSERT(ill->ill_ipst != NULL);
16894 16894 ASSERT(ill->ill_name[0] == '\0');
16895 16895 ASSERT(IAM_WRITER_ILL(ill));
16896 16896 ASSERT((mi_strlen(interf_name) + 1) <= LIFNAMSIZ);
16897 16897 ASSERT(ill->ill_ppa == UINT_MAX);
16898 16898
16899 16899 ill->ill_defend_start = ill->ill_defend_count = 0;
16900 16900 /* The ppa is sent down by ifconfig or is chosen */
16901 16901 if ((ppa_ptr = ill_get_ppa_ptr(interf_name)) == NULL) {
16902 16902 return (EINVAL);
16903 16903 }
16904 16904
16905 16905 /*
16906 16906 * make sure ppa passed in is same as ppa in the name.
16907 16907 * This check is not made when ppa == UINT_MAX in that case ppa
16908 16908 * in the name could be anything. System will choose a ppa and
16909 16909 * update new_ppa_ptr and inter_name to contain the choosen ppa.
16910 16910 */
16911 16911 if (*new_ppa_ptr != UINT_MAX) {
16912 16912 /* stoi changes the pointer */
16913 16913 old_ptr = ppa_ptr;
16914 16914 /*
16915 16915 * ifconfig passed in 0 for the ppa for DLPI 1 style devices
16916 16916 * (they don't have an externally visible ppa). We assign one
16917 16917 * here so that we can manage the interface. Note that in
16918 16918 * the past this value was always 0 for DLPI 1 drivers.
16919 16919 */
16920 16920 if (*new_ppa_ptr == 0)
16921 16921 *new_ppa_ptr = stoi(&old_ptr);
16922 16922 else if (*new_ppa_ptr != (uint_t)stoi(&old_ptr))
16923 16923 return (EINVAL);
16924 16924 }
16925 16925 /*
16926 16926 * terminate string before ppa
16927 16927 * save char at that location.
16928 16928 */
16929 16929 old_char = ppa_ptr[0];
16930 16930 ppa_ptr[0] = '\0';
16931 16931
16932 16932 ill->ill_ppa = *new_ppa_ptr;
16933 16933 /*
16934 16934 * Finish as much work now as possible before calling ill_glist_insert
16935 16935 * which makes the ill globally visible and also merges it with the
16936 16936 * other protocol instance of this phyint. The remaining work is
16937 16937 * done after entering the ipsq which may happen sometime later.
16938 16938 */
16939 16939 ipif = ill->ill_ipif;
16940 16940
16941 16941 /* We didn't do this when we allocated ipif in ip_ll_subnet_defaults */
16942 16942 ipif_assign_seqid(ipif);
16943 16943
16944 16944 if (!(ill->ill_flags & (ILLF_IPV4|ILLF_IPV6)))
16945 16945 ill->ill_flags |= ILLF_IPV4;
16946 16946
16947 16947 ASSERT(ipif->ipif_next == NULL); /* Only one ipif on ill */
16948 16948 ASSERT((ipif->ipif_flags & IPIF_UP) == 0);
16949 16949
16950 16950 if (ill->ill_flags & ILLF_IPV6) {
16951 16951
16952 16952 ill->ill_isv6 = B_TRUE;
16953 16953 ill_set_inputfn(ill);
16954 16954 if (ill->ill_rq != NULL) {
16955 16955 ill->ill_rq->q_qinfo = &iprinitv6;
16956 16956 }
16957 16957
16958 16958 /* Keep the !IN6_IS_ADDR_V4MAPPED assertions happy */
16959 16959 ipif->ipif_v6lcl_addr = ipv6_all_zeros;
16960 16960 ipif->ipif_v6subnet = ipv6_all_zeros;
16961 16961 ipif->ipif_v6net_mask = ipv6_all_zeros;
16962 16962 ipif->ipif_v6brd_addr = ipv6_all_zeros;
16963 16963 ipif->ipif_v6pp_dst_addr = ipv6_all_zeros;
16964 16964 ill->ill_reachable_retrans_time = ND_RETRANS_TIMER;
16965 16965 /*
16966 16966 * point-to-point or Non-mulicast capable
16967 16967 * interfaces won't do NUD unless explicitly
16968 16968 * configured to do so.
16969 16969 */
16970 16970 if (ipif->ipif_flags & IPIF_POINTOPOINT ||
16971 16971 !(ill->ill_flags & ILLF_MULTICAST)) {
16972 16972 ill->ill_flags |= ILLF_NONUD;
16973 16973 }
16974 16974 /* Make sure IPv4 specific flag is not set on IPv6 if */
16975 16975 if (ill->ill_flags & ILLF_NOARP) {
16976 16976 /*
16977 16977 * Note: xresolv interfaces will eventually need
16978 16978 * NOARP set here as well, but that will require
16979 16979 * those external resolvers to have some
16980 16980 * knowledge of that flag and act appropriately.
16981 16981 * Not to be changed at present.
16982 16982 */
16983 16983 ill->ill_flags &= ~ILLF_NOARP;
16984 16984 }
16985 16985 /*
16986 16986 * Set the ILLF_ROUTER flag according to the global
16987 16987 * IPv6 forwarding policy.
16988 16988 */
16989 16989 if (ipst->ips_ipv6_forwarding != 0)
16990 16990 ill->ill_flags |= ILLF_ROUTER;
16991 16991 } else if (ill->ill_flags & ILLF_IPV4) {
16992 16992 ill->ill_isv6 = B_FALSE;
16993 16993 ill_set_inputfn(ill);
16994 16994 ill->ill_reachable_retrans_time = ARP_RETRANS_TIMER;
16995 16995 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6lcl_addr);
16996 16996 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6subnet);
16997 16997 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6net_mask);
16998 16998 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6brd_addr);
16999 16999 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &ipif->ipif_v6pp_dst_addr);
17000 17000 /*
17001 17001 * Set the ILLF_ROUTER flag according to the global
17002 17002 * IPv4 forwarding policy.
17003 17003 */
17004 17004 if (ipst->ips_ip_forwarding != 0)
17005 17005 ill->ill_flags |= ILLF_ROUTER;
17006 17006 }
17007 17007
17008 17008 ASSERT(ill->ill_phyint != NULL);
17009 17009
17010 17010 /*
17011 17011 * The ipIfStatsIfindex and ipv6IfIcmpIfIndex assignments will
17012 17012 * be completed in ill_glist_insert -> ill_phyint_reinit
17013 17013 */
17014 17014 if (!ill_allocate_mibs(ill))
17015 17015 return (ENOMEM);
17016 17016
17017 17017 /*
17018 17018 * Pick a default sap until we get the DL_INFO_ACK back from
17019 17019 * the driver.
17020 17020 */
17021 17021 ill->ill_sap = (ill->ill_isv6) ? ill->ill_media->ip_m_ipv6sap :
17022 17022 ill->ill_media->ip_m_ipv4sap;
17023 17023
17024 17024 ill->ill_ifname_pending = 1;
17025 17025 ill->ill_ifname_pending_err = 0;
17026 17026
17027 17027 /*
17028 17028 * When the first ipif comes up in ipif_up_done(), multicast groups
17029 17029 * that were joined while this ill was not bound to the DLPI link need
17030 17030 * to be recovered by ill_recover_multicast().
17031 17031 */
17032 17032 ill->ill_need_recover_multicast = 1;
17033 17033
17034 17034 ill_refhold(ill);
17035 17035 rw_enter(&ipst->ips_ill_g_lock, RW_WRITER);
17036 17036 if ((error = ill_glist_insert(ill, interf_name,
17037 17037 (ill->ill_flags & ILLF_IPV6) == ILLF_IPV6)) > 0) {
17038 17038 ill->ill_ppa = UINT_MAX;
17039 17039 ill->ill_name[0] = '\0';
17040 17040 /*
17041 17041 * undo null termination done above.
17042 17042 */
17043 17043 ppa_ptr[0] = old_char;
17044 17044 rw_exit(&ipst->ips_ill_g_lock);
17045 17045 ill_refrele(ill);
17046 17046 return (error);
17047 17047 }
17048 17048
17049 17049 ASSERT(ill->ill_name_length <= LIFNAMSIZ);
17050 17050
17051 17051 /*
17052 17052 * When we return the buffer pointed to by interf_name should contain
17053 17053 * the same name as in ill_name.
17054 17054 * If a ppa was choosen by the system (ppa passed in was UINT_MAX)
17055 17055 * the buffer pointed to by new_ppa_ptr would not contain the right ppa
17056 17056 * so copy full name and update the ppa ptr.
17057 17057 * When ppa passed in != UINT_MAX all values are correct just undo
17058 17058 * null termination, this saves a bcopy.
17059 17059 */
17060 17060 if (*new_ppa_ptr == UINT_MAX) {
17061 17061 bcopy(ill->ill_name, interf_name, ill->ill_name_length);
17062 17062 *new_ppa_ptr = ill->ill_ppa;
17063 17063 } else {
17064 17064 /*
17065 17065 * undo null termination done above.
17066 17066 */
17067 17067 ppa_ptr[0] = old_char;
17068 17068 }
17069 17069
17070 17070 /* Let SCTP know about this ILL */
17071 17071 sctp_update_ill(ill, SCTP_ILL_INSERT);
17072 17072
17073 17073 /*
17074 17074 * ill_glist_insert has made the ill visible globally, and
17075 17075 * ill_phyint_reinit could have changed the ipsq. At this point,
17076 17076 * we need to hold the ips_ill_g_lock across the call to enter the
17077 17077 * ipsq to enforce atomicity and prevent reordering. In the event
17078 17078 * the ipsq has changed, and if the new ipsq is currently busy,
17079 17079 * we need to make sure that this half-completed ioctl is ahead of
17080 17080 * any subsequent ioctl. We achieve this by not dropping the
17081 17081 * ips_ill_g_lock which prevents any ill lookup itself thereby
17082 17082 * ensuring that new ioctls can't start.
17083 17083 */
17084 17084 ipsq = ipsq_try_enter_internal(ill, q, mp, ip_reprocess_ioctl, NEW_OP,
17085 17085 B_TRUE);
17086 17086
17087 17087 rw_exit(&ipst->ips_ill_g_lock);
17088 17088 ill_refrele(ill);
17089 17089 if (ipsq == NULL)
17090 17090 return (EINPROGRESS);
17091 17091
17092 17092 /*
17093 17093 * If ill_phyint_reinit() changed our ipsq, then start on the new ipsq.
17094 17094 */
17095 17095 if (ipsq->ipsq_xop->ipx_current_ipif == NULL)
17096 17096 ipsq_current_start(ipsq, ipif, SIOCSLIFNAME);
17097 17097 else
17098 17098 ASSERT(ipsq->ipsq_xop->ipx_current_ipif == ipif);
17099 17099
17100 17100 error = ipif_set_values_tail(ill, ipif, mp, q);
17101 17101 ipsq_exit(ipsq);
17102 17102 if (error != 0 && error != EINPROGRESS) {
17103 17103 /*
17104 17104 * restore previous values
17105 17105 */
17106 17106 ill->ill_isv6 = B_FALSE;
17107 17107 ill_set_inputfn(ill);
17108 17108 }
17109 17109 return (error);
17110 17110 }
17111 17111
17112 17112 void
17113 17113 ipif_init(ip_stack_t *ipst)
17114 17114 {
17115 17115 int i;
17116 17116
17117 17117 for (i = 0; i < MAX_G_HEADS; i++) {
17118 17118 ipst->ips_ill_g_heads[i].ill_g_list_head =
17119 17119 (ill_if_t *)&ipst->ips_ill_g_heads[i];
17120 17120 ipst->ips_ill_g_heads[i].ill_g_list_tail =
17121 17121 (ill_if_t *)&ipst->ips_ill_g_heads[i];
17122 17122 }
17123 17123
17124 17124 avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_index,
17125 17125 ill_phyint_compare_index,
17126 17126 sizeof (phyint_t),
17127 17127 offsetof(struct phyint, phyint_avl_by_index));
17128 17128 avl_create(&ipst->ips_phyint_g_list->phyint_list_avl_by_name,
17129 17129 ill_phyint_compare_name,
17130 17130 sizeof (phyint_t),
17131 17131 offsetof(struct phyint, phyint_avl_by_name));
17132 17132 }
17133 17133
17134 17134 /*
17135 17135 * Save enough information so that we can recreate the IRE if
17136 17136 * the interface goes down and then up.
17137 17137 */
17138 17138 void
17139 17139 ill_save_ire(ill_t *ill, ire_t *ire)
17140 17140 {
17141 17141 mblk_t *save_mp;
17142 17142
17143 17143 save_mp = allocb(sizeof (ifrt_t), BPRI_MED);
17144 17144 if (save_mp != NULL) {
17145 17145 ifrt_t *ifrt;
17146 17146
17147 17147 save_mp->b_wptr += sizeof (ifrt_t);
17148 17148 ifrt = (ifrt_t *)save_mp->b_rptr;
17149 17149 bzero(ifrt, sizeof (ifrt_t));
17150 17150 ifrt->ifrt_type = ire->ire_type;
17151 17151 if (ire->ire_ipversion == IPV4_VERSION) {
17152 17152 ASSERT(!ill->ill_isv6);
17153 17153 ifrt->ifrt_addr = ire->ire_addr;
17154 17154 ifrt->ifrt_gateway_addr = ire->ire_gateway_addr;
17155 17155 ifrt->ifrt_setsrc_addr = ire->ire_setsrc_addr;
17156 17156 ifrt->ifrt_mask = ire->ire_mask;
17157 17157 } else {
17158 17158 ASSERT(ill->ill_isv6);
17159 17159 ifrt->ifrt_v6addr = ire->ire_addr_v6;
17160 17160 /* ire_gateway_addr_v6 can change due to RTM_CHANGE */
17161 17161 mutex_enter(&ire->ire_lock);
17162 17162 ifrt->ifrt_v6gateway_addr = ire->ire_gateway_addr_v6;
17163 17163 mutex_exit(&ire->ire_lock);
17164 17164 ifrt->ifrt_v6setsrc_addr = ire->ire_setsrc_addr_v6;
17165 17165 ifrt->ifrt_v6mask = ire->ire_mask_v6;
17166 17166 }
17167 17167 ifrt->ifrt_flags = ire->ire_flags;
17168 17168 ifrt->ifrt_zoneid = ire->ire_zoneid;
17169 17169 mutex_enter(&ill->ill_saved_ire_lock);
17170 17170 save_mp->b_cont = ill->ill_saved_ire_mp;
17171 17171 ill->ill_saved_ire_mp = save_mp;
17172 17172 ill->ill_saved_ire_cnt++;
17173 17173 mutex_exit(&ill->ill_saved_ire_lock);
17174 17174 }
17175 17175 }
17176 17176
17177 17177 /*
17178 17178 * Remove one entry from ill_saved_ire_mp.
17179 17179 */
17180 17180 void
17181 17181 ill_remove_saved_ire(ill_t *ill, ire_t *ire)
17182 17182 {
17183 17183 mblk_t **mpp;
17184 17184 mblk_t *mp;
17185 17185 ifrt_t *ifrt;
17186 17186
17187 17187 /* Remove from ill_saved_ire_mp list if it is there */
17188 17188 mutex_enter(&ill->ill_saved_ire_lock);
17189 17189 for (mpp = &ill->ill_saved_ire_mp; *mpp != NULL;
17190 17190 mpp = &(*mpp)->b_cont) {
17191 17191 in6_addr_t gw_addr_v6;
17192 17192
17193 17193 /*
17194 17194 * On a given ill, the tuple of address, gateway, mask,
17195 17195 * ire_type, and zoneid is unique for each saved IRE.
17196 17196 */
17197 17197 mp = *mpp;
17198 17198 ifrt = (ifrt_t *)mp->b_rptr;
17199 17199 /* ire_gateway_addr_v6 can change - need lock */
17200 17200 mutex_enter(&ire->ire_lock);
17201 17201 gw_addr_v6 = ire->ire_gateway_addr_v6;
17202 17202 mutex_exit(&ire->ire_lock);
17203 17203
17204 17204 if (ifrt->ifrt_zoneid != ire->ire_zoneid ||
17205 17205 ifrt->ifrt_type != ire->ire_type)
17206 17206 continue;
17207 17207
17208 17208 if (ill->ill_isv6 ?
17209 17209 (IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6addr,
17210 17210 &ire->ire_addr_v6) &&
17211 17211 IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6gateway_addr,
17212 17212 &gw_addr_v6) &&
17213 17213 IN6_ARE_ADDR_EQUAL(&ifrt->ifrt_v6mask,
17214 17214 &ire->ire_mask_v6)) :
17215 17215 (ifrt->ifrt_addr == ire->ire_addr &&
17216 17216 ifrt->ifrt_gateway_addr == ire->ire_gateway_addr &&
17217 17217 ifrt->ifrt_mask == ire->ire_mask)) {
17218 17218 *mpp = mp->b_cont;
17219 17219 ill->ill_saved_ire_cnt--;
17220 17220 freeb(mp);
17221 17221 break;
17222 17222 }
17223 17223 }
17224 17224 mutex_exit(&ill->ill_saved_ire_lock);
17225 17225 }
17226 17226
17227 17227 /*
17228 17228 * IP multirouting broadcast routes handling
17229 17229 * Append CGTP broadcast IREs to regular ones created
17230 17230 * at ifconfig time.
17231 17231 * The usage is a route add <cgtp_bc> <nic_bc> -multirt i.e., both
17232 17232 * the destination and the gateway are broadcast addresses.
17233 17233 * The caller has verified that the destination is an IRE_BROADCAST and that
17234 17234 * RTF_MULTIRT was set. Here if the gateway is a broadcast address, then
17235 17235 * we create a MULTIRT IRE_BROADCAST.
17236 17236 * Note that the IRE_HOST created by ire_rt_add doesn't get found by anything
17237 17237 * since the IRE_BROADCAST takes precedence; ire_add_v4 does head insertion.
17238 17238 */
17239 17239 static void
17240 17240 ip_cgtp_bcast_add(ire_t *ire, ip_stack_t *ipst)
17241 17241 {
17242 17242 ire_t *ire_prim;
17243 17243
17244 17244 ASSERT(ire != NULL);
17245 17245
17246 17246 ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17247 17247 IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst,
17248 17248 NULL);
17249 17249 if (ire_prim != NULL) {
17250 17250 /*
17251 17251 * We are in the special case of broadcasts for
17252 17252 * CGTP. We add an IRE_BROADCAST that holds
17253 17253 * the RTF_MULTIRT flag, the destination
17254 17254 * address and the low level
17255 17255 * info of ire_prim. In other words, CGTP
17256 17256 * broadcast is added to the redundant ipif.
17257 17257 */
17258 17258 ill_t *ill_prim;
17259 17259 ire_t *bcast_ire;
17260 17260
17261 17261 ill_prim = ire_prim->ire_ill;
17262 17262
17263 17263 ip2dbg(("ip_cgtp_filter_bcast_add: ire_prim %p, ill_prim %p\n",
17264 17264 (void *)ire_prim, (void *)ill_prim));
17265 17265
17266 17266 bcast_ire = ire_create(
17267 17267 (uchar_t *)&ire->ire_addr,
17268 17268 (uchar_t *)&ip_g_all_ones,
17269 17269 (uchar_t *)&ire->ire_gateway_addr,
17270 17270 IRE_BROADCAST,
17271 17271 ill_prim,
17272 17272 GLOBAL_ZONEID, /* CGTP is only for the global zone */
17273 17273 ire->ire_flags | RTF_KERNEL,
17274 17274 NULL,
17275 17275 ipst);
17276 17276
17277 17277 /*
17278 17278 * Here we assume that ire_add does head insertion so that
17279 17279 * the added IRE_BROADCAST comes before the existing IRE_HOST.
17280 17280 */
17281 17281 if (bcast_ire != NULL) {
17282 17282 if (ire->ire_flags & RTF_SETSRC) {
17283 17283 bcast_ire->ire_setsrc_addr =
17284 17284 ire->ire_setsrc_addr;
17285 17285 }
17286 17286 bcast_ire = ire_add(bcast_ire);
17287 17287 if (bcast_ire != NULL) {
17288 17288 ip2dbg(("ip_cgtp_filter_bcast_add: "
17289 17289 "added bcast_ire %p\n",
17290 17290 (void *)bcast_ire));
17291 17291
17292 17292 ill_save_ire(ill_prim, bcast_ire);
17293 17293 ire_refrele(bcast_ire);
17294 17294 }
17295 17295 }
17296 17296 ire_refrele(ire_prim);
17297 17297 }
17298 17298 }
17299 17299
17300 17300 /*
17301 17301 * IP multirouting broadcast routes handling
17302 17302 * Remove the broadcast ire.
17303 17303 * The usage is a route delete <cgtp_bc> <nic_bc> -multirt i.e., both
17304 17304 * the destination and the gateway are broadcast addresses.
17305 17305 * The caller has only verified that RTF_MULTIRT was set. We check
17306 17306 * that the destination is broadcast and that the gateway is a broadcast
17307 17307 * address, and if so delete the IRE added by ip_cgtp_bcast_add().
17308 17308 */
17309 17309 static void
17310 17310 ip_cgtp_bcast_delete(ire_t *ire, ip_stack_t *ipst)
17311 17311 {
17312 17312 ASSERT(ire != NULL);
17313 17313
17314 17314 if (ip_type_v4(ire->ire_addr, ipst) == IRE_BROADCAST) {
17315 17315 ire_t *ire_prim;
17316 17316
17317 17317 ire_prim = ire_ftable_lookup_v4(ire->ire_gateway_addr, 0, 0,
17318 17318 IRE_BROADCAST, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 0,
17319 17319 ipst, NULL);
17320 17320 if (ire_prim != NULL) {
17321 17321 ill_t *ill_prim;
17322 17322 ire_t *bcast_ire;
17323 17323
17324 17324 ill_prim = ire_prim->ire_ill;
17325 17325
17326 17326 ip2dbg(("ip_cgtp_filter_bcast_delete: "
17327 17327 "ire_prim %p, ill_prim %p\n",
17328 17328 (void *)ire_prim, (void *)ill_prim));
17329 17329
17330 17330 bcast_ire = ire_ftable_lookup_v4(ire->ire_addr, 0,
17331 17331 ire->ire_gateway_addr, IRE_BROADCAST,
17332 17332 ill_prim, ALL_ZONES, NULL,
17333 17333 MATCH_IRE_TYPE | MATCH_IRE_GW | MATCH_IRE_ILL |
17334 17334 MATCH_IRE_MASK, 0, ipst, NULL);
17335 17335
17336 17336 if (bcast_ire != NULL) {
17337 17337 ip2dbg(("ip_cgtp_filter_bcast_delete: "
17338 17338 "looked up bcast_ire %p\n",
17339 17339 (void *)bcast_ire));
17340 17340 ill_remove_saved_ire(bcast_ire->ire_ill,
17341 17341 bcast_ire);
17342 17342 ire_delete(bcast_ire);
17343 17343 ire_refrele(bcast_ire);
17344 17344 }
17345 17345 ire_refrele(ire_prim);
17346 17346 }
17347 17347 }
17348 17348 }
17349 17349
17350 17350 /*
17351 17351 * Derive an interface id from the link layer address.
17352 17352 * Knows about IEEE 802 and IEEE EUI-64 mappings.
17353 17353 */
17354 17354 static void
17355 17355 ip_ether_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17356 17356 {
17357 17357 char *addr;
17358 17358
17359 17359 /*
17360 17360 * Note that some IPv6 interfaces get plumbed over links that claim to
17361 17361 * be DL_ETHER, but don't actually have Ethernet MAC addresses (e.g.
17362 17362 * PPP links). The ETHERADDRL check here ensures that we only set the
17363 17363 * interface ID on IPv6 interfaces above links that actually have real
17364 17364 * Ethernet addresses.
17365 17365 */
17366 17366 if (ill->ill_phys_addr_length == ETHERADDRL) {
17367 17367 /* Form EUI-64 like address */
17368 17368 addr = (char *)&v6addr->s6_addr32[2];
17369 17369 bcopy(ill->ill_phys_addr, addr, 3);
17370 17370 addr[0] ^= 0x2; /* Toggle Universal/Local bit */
17371 17371 addr[3] = (char)0xff;
17372 17372 addr[4] = (char)0xfe;
17373 17373 bcopy(ill->ill_phys_addr + 3, addr + 5, 3);
17374 17374 }
17375 17375 }
17376 17376
17377 17377 /* ARGSUSED */
17378 17378 static void
17379 17379 ip_nodef_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17380 17380 {
17381 17381 }
17382 17382
17383 17383 typedef struct ipmp_ifcookie {
17384 17384 uint32_t ic_hostid;
17385 17385 char ic_ifname[LIFNAMSIZ];
17386 17386 char ic_zonename[ZONENAME_MAX];
17387 17387 } ipmp_ifcookie_t;
17388 17388
17389 17389 /*
17390 17390 * Construct a pseudo-random interface ID for the IPMP interface that's both
17391 17391 * predictable and (almost) guaranteed to be unique.
17392 17392 */
17393 17393 static void
17394 17394 ip_ipmp_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17395 17395 {
17396 17396 zone_t *zp;
17397 17397 uint8_t *addr;
17398 17398 uchar_t hash[16];
17399 17399 ulong_t hostid;
17400 17400 MD5_CTX ctx;
17401 17401 ipmp_ifcookie_t ic = { 0 };
17402 17402
17403 17403 ASSERT(IS_IPMP(ill));
17404 17404
17405 17405 (void) ddi_strtoul(hw_serial, NULL, 10, &hostid);
17406 17406 ic.ic_hostid = htonl((uint32_t)hostid);
17407 17407
17408 17408 (void) strlcpy(ic.ic_ifname, ill->ill_name, LIFNAMSIZ);
17409 17409
17410 17410 if ((zp = zone_find_by_id(ill->ill_zoneid)) != NULL) {
17411 17411 (void) strlcpy(ic.ic_zonename, zp->zone_name, ZONENAME_MAX);
17412 17412 zone_rele(zp);
17413 17413 }
17414 17414
17415 17415 MD5Init(&ctx);
17416 17416 MD5Update(&ctx, &ic, sizeof (ic));
17417 17417 MD5Final(hash, &ctx);
17418 17418
17419 17419 /*
17420 17420 * Map the hash to an interface ID per the basic approach in RFC3041.
17421 17421 */
17422 17422 addr = &v6addr->s6_addr8[8];
17423 17423 bcopy(hash + 8, addr, sizeof (uint64_t));
17424 17424 addr[0] &= ~0x2; /* set local bit */
17425 17425 }
17426 17426
17427 17427 /*
17428 17428 * Map the multicast in6_addr_t in m_ip6addr to the physaddr for ethernet.
17429 17429 */
17430 17430 static void
17431 17431 ip_ether_v6_mapping(ill_t *ill, uchar_t *m_ip6addr, uchar_t *m_physaddr)
17432 17432 {
17433 17433 phyint_t *phyi = ill->ill_phyint;
17434 17434
17435 17435 /*
17436 17436 * Check PHYI_MULTI_BCAST and length of physical
17437 17437 * address to determine if we use the mapping or the
17438 17438 * broadcast address.
17439 17439 */
17440 17440 if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17441 17441 ill->ill_phys_addr_length != ETHERADDRL) {
17442 17442 ip_mbcast_mapping(ill, m_ip6addr, m_physaddr);
17443 17443 return;
17444 17444 }
17445 17445 m_physaddr[0] = 0x33;
17446 17446 m_physaddr[1] = 0x33;
17447 17447 m_physaddr[2] = m_ip6addr[12];
17448 17448 m_physaddr[3] = m_ip6addr[13];
17449 17449 m_physaddr[4] = m_ip6addr[14];
17450 17450 m_physaddr[5] = m_ip6addr[15];
17451 17451 }
17452 17452
17453 17453 /*
17454 17454 * Map the multicast ipaddr_t in m_ipaddr to the physaddr for ethernet.
17455 17455 */
17456 17456 static void
17457 17457 ip_ether_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17458 17458 {
17459 17459 phyint_t *phyi = ill->ill_phyint;
17460 17460
17461 17461 /*
17462 17462 * Check PHYI_MULTI_BCAST and length of physical
17463 17463 * address to determine if we use the mapping or the
17464 17464 * broadcast address.
17465 17465 */
17466 17466 if ((phyi->phyint_flags & PHYI_MULTI_BCAST) != 0 ||
17467 17467 ill->ill_phys_addr_length != ETHERADDRL) {
17468 17468 ip_mbcast_mapping(ill, m_ipaddr, m_physaddr);
17469 17469 return;
17470 17470 }
17471 17471 m_physaddr[0] = 0x01;
17472 17472 m_physaddr[1] = 0x00;
17473 17473 m_physaddr[2] = 0x5e;
17474 17474 m_physaddr[3] = m_ipaddr[1] & 0x7f;
17475 17475 m_physaddr[4] = m_ipaddr[2];
17476 17476 m_physaddr[5] = m_ipaddr[3];
17477 17477 }
17478 17478
17479 17479 /* ARGSUSED */
17480 17480 static void
17481 17481 ip_mbcast_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17482 17482 {
17483 17483 /*
17484 17484 * for the MULTI_BCAST case and other cases when we want to
17485 17485 * use the link-layer broadcast address for multicast.
17486 17486 */
17487 17487 uint8_t *bphys_addr;
17488 17488 dl_unitdata_req_t *dlur;
17489 17489
17490 17490 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17491 17491 if (ill->ill_sap_length < 0) {
17492 17492 bphys_addr = (uchar_t *)dlur +
17493 17493 dlur->dl_dest_addr_offset;
17494 17494 } else {
17495 17495 bphys_addr = (uchar_t *)dlur +
17496 17496 dlur->dl_dest_addr_offset + ill->ill_sap_length;
17497 17497 }
17498 17498
17499 17499 bcopy(bphys_addr, m_physaddr, ill->ill_phys_addr_length);
17500 17500 }
17501 17501
17502 17502 /*
17503 17503 * Derive IPoIB interface id from the link layer address.
17504 17504 */
17505 17505 static void
17506 17506 ip_ib_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17507 17507 {
17508 17508 char *addr;
17509 17509
17510 17510 ASSERT(ill->ill_phys_addr_length == 20);
17511 17511 addr = (char *)&v6addr->s6_addr32[2];
17512 17512 bcopy(ill->ill_phys_addr + 12, addr, 8);
17513 17513 /*
17514 17514 * In IBA 1.1 timeframe, some vendors erroneously set the u/l bit
17515 17515 * in the globally assigned EUI-64 GUID to 1, in violation of IEEE
17516 17516 * rules. In these cases, the IBA considers these GUIDs to be in
17517 17517 * "Modified EUI-64" format, and thus toggling the u/l bit is not
17518 17518 * required; vendors are required not to assign global EUI-64's
17519 17519 * that differ only in u/l bit values, thus guaranteeing uniqueness
17520 17520 * of the interface identifier. Whether the GUID is in modified
17521 17521 * or proper EUI-64 format, the ipv6 identifier must have the u/l
17522 17522 * bit set to 1.
17523 17523 */
17524 17524 addr[0] |= 2; /* Set Universal/Local bit to 1 */
17525 17525 }
17526 17526
17527 17527 /*
17528 17528 * Map the multicast ipaddr_t in m_ipaddr to the physaddr for InfiniBand.
17529 17529 * Note on mapping from multicast IP addresses to IPoIB multicast link
17530 17530 * addresses. IPoIB multicast link addresses are based on IBA link addresses.
17531 17531 * The format of an IPoIB multicast address is:
17532 17532 *
17533 17533 * 4 byte QPN Scope Sign. Pkey
17534 17534 * +--------------------------------------------+
17535 17535 * | 00FFFFFF | FF | 1X | X01B | Pkey | GroupID |
17536 17536 * +--------------------------------------------+
17537 17537 *
17538 17538 * The Scope and Pkey components are properties of the IBA port and
17539 17539 * network interface. They can be ascertained from the broadcast address.
17540 17540 * The Sign. part is the signature, and is 401B for IPv4 and 601B for IPv6.
17541 17541 */
17542 17542 static void
17543 17543 ip_ib_v4_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17544 17544 {
17545 17545 static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17546 17546 0xff, 0x10, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
17547 17547 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17548 17548 uint8_t *bphys_addr;
17549 17549 dl_unitdata_req_t *dlur;
17550 17550
17551 17551 bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17552 17552
17553 17553 /*
17554 17554 * RFC 4391: IPv4 MGID is 28-bit long.
17555 17555 */
17556 17556 m_physaddr[16] = m_ipaddr[0] & 0x0f;
17557 17557 m_physaddr[17] = m_ipaddr[1];
17558 17558 m_physaddr[18] = m_ipaddr[2];
17559 17559 m_physaddr[19] = m_ipaddr[3];
17560 17560
17561 17561
17562 17562 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17563 17563 if (ill->ill_sap_length < 0) {
17564 17564 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17565 17565 } else {
17566 17566 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17567 17567 ill->ill_sap_length;
17568 17568 }
17569 17569 /*
17570 17570 * Now fill in the IBA scope/Pkey values from the broadcast address.
17571 17571 */
17572 17572 m_physaddr[5] = bphys_addr[5];
17573 17573 m_physaddr[8] = bphys_addr[8];
17574 17574 m_physaddr[9] = bphys_addr[9];
17575 17575 }
17576 17576
17577 17577 static void
17578 17578 ip_ib_v6_mapping(ill_t *ill, uchar_t *m_ipaddr, uchar_t *m_physaddr)
17579 17579 {
17580 17580 static uint8_t ipv4_g_phys_ibmulti_addr[] = { 0x00, 0xff, 0xff, 0xff,
17581 17581 0xff, 0x10, 0x60, 0x1b, 0x00, 0x00, 0x00, 0x00,
17582 17582 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
17583 17583 uint8_t *bphys_addr;
17584 17584 dl_unitdata_req_t *dlur;
17585 17585
17586 17586 bcopy(ipv4_g_phys_ibmulti_addr, m_physaddr, ill->ill_phys_addr_length);
17587 17587
17588 17588 /*
17589 17589 * RFC 4391: IPv4 MGID is 80-bit long.
17590 17590 */
17591 17591 bcopy(&m_ipaddr[6], &m_physaddr[10], 10);
17592 17592
17593 17593 dlur = (dl_unitdata_req_t *)ill->ill_bcast_mp->b_rptr;
17594 17594 if (ill->ill_sap_length < 0) {
17595 17595 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset;
17596 17596 } else {
17597 17597 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
17598 17598 ill->ill_sap_length;
17599 17599 }
17600 17600 /*
17601 17601 * Now fill in the IBA scope/Pkey values from the broadcast address.
17602 17602 */
17603 17603 m_physaddr[5] = bphys_addr[5];
17604 17604 m_physaddr[8] = bphys_addr[8];
17605 17605 m_physaddr[9] = bphys_addr[9];
17606 17606 }
17607 17607
17608 17608 /*
17609 17609 * Derive IPv6 interface id from an IPv4 link-layer address (e.g. from an IPv4
17610 17610 * tunnel). The IPv4 address simply get placed in the lower 4 bytes of the
17611 17611 * IPv6 interface id. This is a suggested mechanism described in section 3.7
17612 17612 * of RFC4213.
17613 17613 */
17614 17614 static void
17615 17615 ip_ipv4_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17616 17616 {
17617 17617 ASSERT(ill->ill_phys_addr_length == sizeof (ipaddr_t));
17618 17618 v6addr->s6_addr32[2] = 0;
17619 17619 bcopy(physaddr, &v6addr->s6_addr32[3], sizeof (ipaddr_t));
17620 17620 }
17621 17621
17622 17622 /*
17623 17623 * Derive IPv6 interface id from an IPv6 link-layer address (e.g. from an IPv6
17624 17624 * tunnel). The lower 8 bytes of the IPv6 address simply become the interface
17625 17625 * id.
17626 17626 */
17627 17627 static void
17628 17628 ip_ipv6_genv6intfid(ill_t *ill, uint8_t *physaddr, in6_addr_t *v6addr)
17629 17629 {
17630 17630 in6_addr_t *v6lladdr = (in6_addr_t *)physaddr;
17631 17631
17632 17632 ASSERT(ill->ill_phys_addr_length == sizeof (in6_addr_t));
17633 17633 bcopy(&v6lladdr->s6_addr32[2], &v6addr->s6_addr32[2], 8);
17634 17634 }
17635 17635
17636 17636 static void
17637 17637 ip_ipv6_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17638 17638 {
17639 17639 ip_ipv6_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17640 17640 }
17641 17641
17642 17642 static void
17643 17643 ip_ipv6_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17644 17644 {
17645 17645 ip_ipv6_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17646 17646 }
17647 17647
17648 17648 static void
17649 17649 ip_ipv4_v6intfid(ill_t *ill, in6_addr_t *v6addr)
17650 17650 {
17651 17651 ip_ipv4_genv6intfid(ill, ill->ill_phys_addr, v6addr);
17652 17652 }
17653 17653
17654 17654 static void
17655 17655 ip_ipv4_v6destintfid(ill_t *ill, in6_addr_t *v6addr)
17656 17656 {
17657 17657 ip_ipv4_genv6intfid(ill, ill->ill_dest_addr, v6addr);
17658 17658 }
17659 17659
17660 17660 /*
17661 17661 * Lookup an ill and verify that the zoneid has an ipif on that ill.
17662 17662 * Returns an held ill, or NULL.
17663 17663 */
17664 17664 ill_t *
17665 17665 ill_lookup_on_ifindex_zoneid(uint_t index, zoneid_t zoneid, boolean_t isv6,
17666 17666 ip_stack_t *ipst)
17667 17667 {
17668 17668 ill_t *ill;
17669 17669 ipif_t *ipif;
17670 17670
17671 17671 ill = ill_lookup_on_ifindex(index, isv6, ipst);
17672 17672 if (ill == NULL)
17673 17673 return (NULL);
17674 17674
17675 17675 mutex_enter(&ill->ill_lock);
17676 17676 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17677 17677 if (IPIF_IS_CONDEMNED(ipif))
17678 17678 continue;
17679 17679 if (zoneid != ALL_ZONES && ipif->ipif_zoneid != zoneid &&
17680 17680 ipif->ipif_zoneid != ALL_ZONES)
17681 17681 continue;
17682 17682
17683 17683 mutex_exit(&ill->ill_lock);
17684 17684 return (ill);
17685 17685 }
17686 17686 mutex_exit(&ill->ill_lock);
17687 17687 ill_refrele(ill);
17688 17688 return (NULL);
17689 17689 }
17690 17690
17691 17691 /*
17692 17692 * Return a pointer to an ipif_t given a combination of (ill_idx,ipif_id)
17693 17693 * If a pointer to an ipif_t is returned then the caller will need to do
17694 17694 * an ill_refrele().
17695 17695 */
17696 17696 ipif_t *
17697 17697 ipif_getby_indexes(uint_t ifindex, uint_t lifidx, boolean_t isv6,
17698 17698 ip_stack_t *ipst)
17699 17699 {
17700 17700 ipif_t *ipif;
17701 17701 ill_t *ill;
17702 17702
17703 17703 ill = ill_lookup_on_ifindex(ifindex, isv6, ipst);
17704 17704 if (ill == NULL)
17705 17705 return (NULL);
17706 17706
17707 17707 mutex_enter(&ill->ill_lock);
17708 17708 if (ill->ill_state_flags & ILL_CONDEMNED) {
17709 17709 mutex_exit(&ill->ill_lock);
17710 17710 ill_refrele(ill);
17711 17711 return (NULL);
17712 17712 }
17713 17713
17714 17714 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
17715 17715 if (!IPIF_CAN_LOOKUP(ipif))
17716 17716 continue;
17717 17717 if (lifidx == ipif->ipif_id) {
17718 17718 ipif_refhold_locked(ipif);
17719 17719 break;
17720 17720 }
17721 17721 }
17722 17722
17723 17723 mutex_exit(&ill->ill_lock);
17724 17724 ill_refrele(ill);
17725 17725 return (ipif);
17726 17726 }
17727 17727
17728 17728 /*
17729 17729 * Set ill_inputfn based on the current know state.
17730 17730 * This needs to be called when any of the factors taken into
17731 17731 * account changes.
17732 17732 */
17733 17733 void
17734 17734 ill_set_inputfn(ill_t *ill)
17735 17735 {
17736 17736 ip_stack_t *ipst = ill->ill_ipst;
17737 17737
17738 17738 if (ill->ill_isv6) {
17739 17739 if (is_system_labeled())
17740 17740 ill->ill_inputfn = ill_input_full_v6;
17741 17741 else
17742 17742 ill->ill_inputfn = ill_input_short_v6;
17743 17743 } else {
17744 17744 if (is_system_labeled())
17745 17745 ill->ill_inputfn = ill_input_full_v4;
17746 17746 else if (ill->ill_dhcpinit != 0)
17747 17747 ill->ill_inputfn = ill_input_full_v4;
17748 17748 else if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head
17749 17749 != NULL)
17750 17750 ill->ill_inputfn = ill_input_full_v4;
17751 17751 else if (ipst->ips_ip_cgtp_filter &&
17752 17752 ipst->ips_ip_cgtp_filter_ops != NULL)
17753 17753 ill->ill_inputfn = ill_input_full_v4;
17754 17754 else
17755 17755 ill->ill_inputfn = ill_input_short_v4;
17756 17756 }
17757 17757 }
17758 17758
17759 17759 /*
17760 17760 * Re-evaluate ill_inputfn for all the IPv4 ills.
17761 17761 * Used when RSVP and CGTP comes and goes.
17762 17762 */
17763 17763 void
17764 17764 ill_set_inputfn_all(ip_stack_t *ipst)
17765 17765 {
17766 17766 ill_walk_context_t ctx;
17767 17767 ill_t *ill;
17768 17768
17769 17769 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
17770 17770 ill = ILL_START_WALK_V4(&ctx, ipst);
17771 17771 for (; ill != NULL; ill = ill_next(&ctx, ill))
17772 17772 ill_set_inputfn(ill);
17773 17773
17774 17774 rw_exit(&ipst->ips_ill_g_lock);
17775 17775 }
17776 17776
17777 17777 /*
17778 17778 * Set the physical address information for `ill' to the contents of the
17779 17779 * dl_notify_ind_t pointed to by `mp'. Must be called as writer, and will be
17780 17780 * asynchronous if `ill' cannot immediately be quiesced -- in which case
17781 17781 * EINPROGRESS will be returned.
17782 17782 */
17783 17783 int
17784 17784 ill_set_phys_addr(ill_t *ill, mblk_t *mp)
17785 17785 {
17786 17786 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17787 17787 dl_notify_ind_t *dlindp = (dl_notify_ind_t *)mp->b_rptr;
17788 17788
17789 17789 ASSERT(IAM_WRITER_IPSQ(ipsq));
17790 17790
17791 17791 if (dlindp->dl_data != DL_IPV6_LINK_LAYER_ADDR &&
17792 17792 dlindp->dl_data != DL_CURR_DEST_ADDR &&
17793 17793 dlindp->dl_data != DL_CURR_PHYS_ADDR) {
17794 17794 /* Changing DL_IPV6_TOKEN is not yet supported */
17795 17795 return (0);
17796 17796 }
17797 17797
17798 17798 /*
17799 17799 * We need to store up to two copies of `mp' in `ill'. Due to the
17800 17800 * design of ipsq_pending_mp_add(), we can't pass them as separate
17801 17801 * arguments to ill_set_phys_addr_tail(). Instead, chain them
17802 17802 * together here, then pull 'em apart in ill_set_phys_addr_tail().
17803 17803 */
17804 17804 if ((mp = copyb(mp)) == NULL || (mp->b_cont = copyb(mp)) == NULL) {
17805 17805 freemsg(mp);
17806 17806 return (ENOMEM);
17807 17807 }
17808 17808
17809 17809 ipsq_current_start(ipsq, ill->ill_ipif, 0);
17810 17810
17811 17811 /*
17812 17812 * Since we'll only do a logical down, we can't rely on ipif_down
17813 17813 * to turn on ILL_DOWN_IN_PROGRESS, or for the DL_BIND_ACK to reset
17814 17814 * ILL_DOWN_IN_PROGRESS. We instead manage this separately for this
17815 17815 * case, to quiesce ire's and nce's for ill_is_quiescent.
17816 17816 */
17817 17817 mutex_enter(&ill->ill_lock);
17818 17818 ill->ill_state_flags |= ILL_DOWN_IN_PROGRESS;
17819 17819 /* no more ire/nce addition allowed */
17820 17820 mutex_exit(&ill->ill_lock);
17821 17821
17822 17822 /*
17823 17823 * If we can quiesce the ill, then set the address. If not, then
17824 17824 * ill_set_phys_addr_tail() will be called from ipif_ill_refrele_tail().
17825 17825 */
17826 17826 ill_down_ipifs(ill, B_TRUE);
17827 17827 mutex_enter(&ill->ill_lock);
17828 17828 if (!ill_is_quiescent(ill)) {
17829 17829 /* call cannot fail since `conn_t *' argument is NULL */
17830 17830 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17831 17831 mp, ILL_DOWN);
17832 17832 mutex_exit(&ill->ill_lock);
17833 17833 return (EINPROGRESS);
17834 17834 }
17835 17835 mutex_exit(&ill->ill_lock);
17836 17836
17837 17837 ill_set_phys_addr_tail(ipsq, ill->ill_rq, mp, NULL);
17838 17838 return (0);
17839 17839 }
17840 17840
17841 17841 /*
17842 17842 * When the allowed-ips link property is set on the datalink, IP receives a
17843 17843 * DL_NOTE_ALLOWED_IPS notification that is processed in ill_set_allowed_ips()
17844 17844 * to initialize the ill_allowed_ips[] array in the ill_t. This array is then
17845 17845 * used to vet addresses passed to ip_sioctl_addr() and to ensure that the
17846 17846 * only IP addresses configured on the ill_t are those in the ill_allowed_ips[]
17847 17847 * array.
17848 17848 */
17849 17849 void
17850 17850 ill_set_allowed_ips(ill_t *ill, mblk_t *mp)
17851 17851 {
17852 17852 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17853 17853 dl_notify_ind_t *dlip = (dl_notify_ind_t *)mp->b_rptr;
17854 17854 mac_protect_t *mrp;
17855 17855 int i;
17856 17856
17857 17857 ASSERT(IAM_WRITER_IPSQ(ipsq));
17858 17858 mrp = (mac_protect_t *)&dlip[1];
17859 17859
17860 17860 if (mrp->mp_ipaddrcnt == 0) { /* reset allowed-ips */
17861 17861 kmem_free(ill->ill_allowed_ips,
17862 17862 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17863 17863 ill->ill_allowed_ips_cnt = 0;
17864 17864 ill->ill_allowed_ips = NULL;
17865 17865 mutex_enter(&ill->ill_phyint->phyint_lock);
17866 17866 ill->ill_phyint->phyint_flags &= ~PHYI_L3PROTECT;
17867 17867 mutex_exit(&ill->ill_phyint->phyint_lock);
17868 17868 return;
17869 17869 }
17870 17870
17871 17871 if (ill->ill_allowed_ips != NULL) {
17872 17872 kmem_free(ill->ill_allowed_ips,
17873 17873 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t));
17874 17874 }
17875 17875 ill->ill_allowed_ips_cnt = mrp->mp_ipaddrcnt;
17876 17876 ill->ill_allowed_ips = kmem_alloc(
17877 17877 ill->ill_allowed_ips_cnt * sizeof (in6_addr_t), KM_SLEEP);
17878 17878 for (i = 0; i < mrp->mp_ipaddrcnt; i++)
17879 17879 ill->ill_allowed_ips[i] = mrp->mp_ipaddrs[i].ip_addr;
17880 17880
17881 17881 mutex_enter(&ill->ill_phyint->phyint_lock);
17882 17882 ill->ill_phyint->phyint_flags |= PHYI_L3PROTECT;
17883 17883 mutex_exit(&ill->ill_phyint->phyint_lock);
17884 17884 }
17885 17885
17886 17886 /*
17887 17887 * Once the ill associated with `q' has quiesced, set its physical address
17888 17888 * information to the values in `addrmp'. Note that two copies of `addrmp'
17889 17889 * are passed (linked by b_cont), since we sometimes need to save two distinct
17890 17890 * copies in the ill_t, and our context doesn't permit sleeping or allocation
17891 17891 * failure (we'll free the other copy if it's not needed). Since the ill_t
17892 17892 * is quiesced, we know any stale nce's with the old address information have
17893 17893 * already been removed, so we don't need to call nce_flush().
17894 17894 */
17895 17895 /* ARGSUSED */
17896 17896 static void
17897 17897 ill_set_phys_addr_tail(ipsq_t *ipsq, queue_t *q, mblk_t *addrmp, void *dummy)
17898 17898 {
17899 17899 ill_t *ill = q->q_ptr;
17900 17900 mblk_t *addrmp2 = unlinkb(addrmp);
17901 17901 dl_notify_ind_t *dlindp = (dl_notify_ind_t *)addrmp->b_rptr;
17902 17902 uint_t addrlen, addroff;
17903 17903 int status;
17904 17904
17905 17905 ASSERT(IAM_WRITER_IPSQ(ipsq));
17906 17906
17907 17907 addroff = dlindp->dl_addr_offset;
17908 17908 addrlen = dlindp->dl_addr_length - ABS(ill->ill_sap_length);
17909 17909
17910 17910 switch (dlindp->dl_data) {
17911 17911 case DL_IPV6_LINK_LAYER_ADDR:
17912 17912 ill_set_ndmp(ill, addrmp, addroff, addrlen);
17913 17913 freemsg(addrmp2);
17914 17914 break;
17915 17915
17916 17916 case DL_CURR_DEST_ADDR:
17917 17917 freemsg(ill->ill_dest_addr_mp);
17918 17918 ill->ill_dest_addr = addrmp->b_rptr + addroff;
17919 17919 ill->ill_dest_addr_mp = addrmp;
17920 17920 if (ill->ill_isv6) {
17921 17921 ill_setdesttoken(ill);
17922 17922 ipif_setdestlinklocal(ill->ill_ipif);
17923 17923 }
17924 17924 freemsg(addrmp2);
17925 17925 break;
17926 17926
17927 17927 case DL_CURR_PHYS_ADDR:
17928 17928 freemsg(ill->ill_phys_addr_mp);
17929 17929 ill->ill_phys_addr = addrmp->b_rptr + addroff;
17930 17930 ill->ill_phys_addr_mp = addrmp;
17931 17931 ill->ill_phys_addr_length = addrlen;
17932 17932 if (ill->ill_isv6)
17933 17933 ill_set_ndmp(ill, addrmp2, addroff, addrlen);
17934 17934 else
17935 17935 freemsg(addrmp2);
17936 17936 if (ill->ill_isv6) {
17937 17937 ill_setdefaulttoken(ill);
17938 17938 ipif_setlinklocal(ill->ill_ipif);
17939 17939 }
17940 17940 break;
17941 17941 default:
17942 17942 ASSERT(0);
17943 17943 }
17944 17944
17945 17945 /*
17946 17946 * reset ILL_DOWN_IN_PROGRESS so that we can successfully add ires
17947 17947 * as we bring the ipifs up again.
17948 17948 */
17949 17949 mutex_enter(&ill->ill_lock);
17950 17950 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
17951 17951 mutex_exit(&ill->ill_lock);
17952 17952 /*
17953 17953 * If there are ipifs to bring up, ill_up_ipifs() will return
17954 17954 * EINPROGRESS, and ipsq_current_finish() will be called by
17955 17955 * ip_rput_dlpi_writer() or arp_bringup_done() when the last ipif is
17956 17956 * brought up.
17957 17957 */
17958 17958 status = ill_up_ipifs(ill, q, addrmp);
17959 17959 if (status != EINPROGRESS)
17960 17960 ipsq_current_finish(ipsq);
17961 17961 }
17962 17962
17963 17963 /*
17964 17964 * Helper routine for setting the ill_nd_lla fields.
17965 17965 */
17966 17966 void
17967 17967 ill_set_ndmp(ill_t *ill, mblk_t *ndmp, uint_t addroff, uint_t addrlen)
17968 17968 {
17969 17969 freemsg(ill->ill_nd_lla_mp);
17970 17970 ill->ill_nd_lla = ndmp->b_rptr + addroff;
17971 17971 ill->ill_nd_lla_mp = ndmp;
17972 17972 ill->ill_nd_lla_len = addrlen;
17973 17973 }
17974 17974
17975 17975 /*
17976 17976 * Replumb the ill.
17977 17977 */
17978 17978 int
17979 17979 ill_replumb(ill_t *ill, mblk_t *mp)
17980 17980 {
17981 17981 ipsq_t *ipsq = ill->ill_phyint->phyint_ipsq;
17982 17982
17983 17983 ASSERT(IAM_WRITER_IPSQ(ipsq));
17984 17984
17985 17985 ipsq_current_start(ipsq, ill->ill_ipif, 0);
17986 17986
17987 17987 /*
17988 17988 * If we can quiesce the ill, then continue. If not, then
17989 17989 * ill_replumb_tail() will be called from ipif_ill_refrele_tail().
17990 17990 */
17991 17991 ill_down_ipifs(ill, B_FALSE);
17992 17992
17993 17993 mutex_enter(&ill->ill_lock);
17994 17994 if (!ill_is_quiescent(ill)) {
17995 17995 /* call cannot fail since `conn_t *' argument is NULL */
17996 17996 (void) ipsq_pending_mp_add(NULL, ill->ill_ipif, ill->ill_rq,
17997 17997 mp, ILL_DOWN);
17998 17998 mutex_exit(&ill->ill_lock);
17999 17999 return (EINPROGRESS);
18000 18000 }
18001 18001 mutex_exit(&ill->ill_lock);
18002 18002
18003 18003 ill_replumb_tail(ipsq, ill->ill_rq, mp, NULL);
18004 18004 return (0);
18005 18005 }
18006 18006
18007 18007 /* ARGSUSED */
18008 18008 static void
18009 18009 ill_replumb_tail(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy)
18010 18010 {
18011 18011 ill_t *ill = q->q_ptr;
18012 18012 int err;
18013 18013 conn_t *connp = NULL;
18014 18014
18015 18015 ASSERT(IAM_WRITER_IPSQ(ipsq));
18016 18016 freemsg(ill->ill_replumb_mp);
18017 18017 ill->ill_replumb_mp = copyb(mp);
18018 18018
18019 18019 if (ill->ill_replumb_mp == NULL) {
18020 18020 /* out of memory */
18021 18021 ipsq_current_finish(ipsq);
18022 18022 return;
18023 18023 }
18024 18024
18025 18025 mutex_enter(&ill->ill_lock);
18026 18026 ill->ill_up_ipifs = ipsq_pending_mp_add(NULL, ill->ill_ipif,
18027 18027 ill->ill_rq, ill->ill_replumb_mp, 0);
18028 18028 mutex_exit(&ill->ill_lock);
18029 18029
18030 18030 if (!ill->ill_up_ipifs) {
18031 18031 /* already closing */
18032 18032 ipsq_current_finish(ipsq);
18033 18033 return;
18034 18034 }
18035 18035 ill->ill_replumbing = 1;
18036 18036 err = ill_down_ipifs_tail(ill);
18037 18037
18038 18038 /*
18039 18039 * Successfully quiesced and brought down the interface, now we send
18040 18040 * the DL_NOTE_REPLUMB_DONE message down to the driver. Reuse the
18041 18041 * DL_NOTE_REPLUMB message.
18042 18042 */
18043 18043 mp = mexchange(NULL, mp, sizeof (dl_notify_conf_t), M_PROTO,
18044 18044 DL_NOTIFY_CONF);
18045 18045 ASSERT(mp != NULL);
18046 18046 ((dl_notify_conf_t *)mp->b_rptr)->dl_notification =
18047 18047 DL_NOTE_REPLUMB_DONE;
18048 18048 ill_dlpi_send(ill, mp);
18049 18049
18050 18050 /*
18051 18051 * For IPv4, we would usually get EINPROGRESS because the ETHERTYPE_ARP
18052 18052 * streams have to be unbound. When all the DLPI exchanges are done,
18053 18053 * ipsq_current_finish() will be called by arp_bringup_done(). The
18054 18054 * remainder of ipif bringup via ill_up_ipifs() will also be done in
18055 18055 * arp_bringup_done().
18056 18056 */
18057 18057 ASSERT(ill->ill_replumb_mp != NULL);
18058 18058 if (err == EINPROGRESS)
18059 18059 return;
18060 18060 else
18061 18061 ill->ill_replumb_mp = ipsq_pending_mp_get(ipsq, &connp);
18062 18062 ASSERT(connp == NULL);
18063 18063 if (err == 0 && ill->ill_replumb_mp != NULL &&
18064 18064 ill_up_ipifs(ill, q, ill->ill_replumb_mp) == EINPROGRESS) {
18065 18065 return;
18066 18066 }
18067 18067 ipsq_current_finish(ipsq);
18068 18068 }
18069 18069
18070 18070 /*
18071 18071 * Issue ioctl `cmd' on `lh'; caller provides the initial payload in `buf'
18072 18072 * which is `bufsize' bytes. On success, zero is returned and `buf' updated
18073 18073 * as per the ioctl. On failure, an errno is returned.
18074 18074 */
18075 18075 static int
18076 18076 ip_ioctl(ldi_handle_t lh, int cmd, void *buf, uint_t bufsize, cred_t *cr)
18077 18077 {
18078 18078 int rval;
18079 18079 struct strioctl iocb;
18080 18080
18081 18081 iocb.ic_cmd = cmd;
18082 18082 iocb.ic_timout = 15;
18083 18083 iocb.ic_len = bufsize;
18084 18084 iocb.ic_dp = buf;
18085 18085
18086 18086 return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, cr, &rval));
18087 18087 }
18088 18088
18089 18089 /*
18090 18090 * Issue an SIOCGLIFCONF for address family `af' and store the result into a
18091 18091 * dynamically-allocated `lifcp' that will be `bufsizep' bytes on success.
18092 18092 */
18093 18093 static int
18094 18094 ip_lifconf_ioctl(ldi_handle_t lh, int af, struct lifconf *lifcp,
18095 18095 uint_t *bufsizep, cred_t *cr)
18096 18096 {
18097 18097 int err;
18098 18098 struct lifnum lifn;
18099 18099
18100 18100 bzero(&lifn, sizeof (lifn));
18101 18101 lifn.lifn_family = af;
18102 18102 lifn.lifn_flags = LIFC_UNDER_IPMP;
18103 18103
18104 18104 if ((err = ip_ioctl(lh, SIOCGLIFNUM, &lifn, sizeof (lifn), cr)) != 0)
18105 18105 return (err);
18106 18106
18107 18107 /*
18108 18108 * Pad the interface count to account for additional interfaces that
18109 18109 * may have been configured between the SIOCGLIFNUM and SIOCGLIFCONF.
18110 18110 */
18111 18111 lifn.lifn_count += 4;
18112 18112 bzero(lifcp, sizeof (*lifcp));
18113 18113 lifcp->lifc_flags = LIFC_UNDER_IPMP;
18114 18114 lifcp->lifc_family = af;
18115 18115 lifcp->lifc_len = *bufsizep = lifn.lifn_count * sizeof (struct lifreq);
18116 18116 lifcp->lifc_buf = kmem_zalloc(*bufsizep, KM_SLEEP);
18117 18117
18118 18118 err = ip_ioctl(lh, SIOCGLIFCONF, lifcp, sizeof (*lifcp), cr);
18119 18119 if (err != 0) {
18120 18120 kmem_free(lifcp->lifc_buf, *bufsizep);
18121 18121 return (err);
18122 18122 }
18123 18123
18124 18124 return (0);
18125 18125 }
18126 18126
18127 18127 /*
18128 18128 * Helper for ip_interface_cleanup() that removes the loopback interface.
18129 18129 */
18130 18130 static void
18131 18131 ip_loopback_removeif(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18132 18132 {
18133 18133 int err;
18134 18134 struct lifreq lifr;
18135 18135
18136 18136 bzero(&lifr, sizeof (lifr));
18137 18137 (void) strcpy(lifr.lifr_name, ipif_loopback_name);
18138 18138
18139 18139 /*
18140 18140 * Attempt to remove the interface. It may legitimately not exist
18141 18141 * (e.g. the zone administrator unplumbed it), so ignore ENXIO.
18142 18142 */
18143 18143 err = ip_ioctl(lh, SIOCLIFREMOVEIF, &lifr, sizeof (lifr), cr);
18144 18144 if (err != 0 && err != ENXIO) {
18145 18145 ip0dbg(("ip_loopback_removeif: IP%s SIOCLIFREMOVEIF failed: "
18146 18146 "error %d\n", isv6 ? "v6" : "v4", err));
18147 18147 }
18148 18148 }
18149 18149
18150 18150 /*
18151 18151 * Helper for ip_interface_cleanup() that ensures no IP interfaces are in IPMP
18152 18152 * groups and that IPMP data addresses are down. These conditions must be met
18153 18153 * so that IPMP interfaces can be I_PUNLINK'd, as per ip_sioctl_plink_ipmp().
18154 18154 */
18155 18155 static void
18156 18156 ip_ipmp_cleanup(ldi_handle_t lh, boolean_t isv6, cred_t *cr)
18157 18157 {
18158 18158 int af = isv6 ? AF_INET6 : AF_INET;
18159 18159 int i, nifs;
18160 18160 int err;
18161 18161 uint_t bufsize;
18162 18162 uint_t lifrsize = sizeof (struct lifreq);
18163 18163 struct lifconf lifc;
18164 18164 struct lifreq *lifrp;
18165 18165
18166 18166 if ((err = ip_lifconf_ioctl(lh, af, &lifc, &bufsize, cr)) != 0) {
18167 18167 cmn_err(CE_WARN, "ip_ipmp_cleanup: cannot get interface list "
18168 18168 "(error %d); any IPMP interfaces cannot be shutdown", err);
18169 18169 return;
18170 18170 }
18171 18171
18172 18172 nifs = lifc.lifc_len / lifrsize;
18173 18173 for (lifrp = lifc.lifc_req, i = 0; i < nifs; i++, lifrp++) {
18174 18174 err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18175 18175 if (err != 0) {
18176 18176 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot get "
18177 18177 "flags: error %d", lifrp->lifr_name, err);
18178 18178 continue;
18179 18179 }
18180 18180
18181 18181 if (lifrp->lifr_flags & IFF_IPMP) {
18182 18182 if ((lifrp->lifr_flags & (IFF_UP|IFF_DUPLICATE)) == 0)
18183 18183 continue;
18184 18184
18185 18185 lifrp->lifr_flags &= ~IFF_UP;
18186 18186 err = ip_ioctl(lh, SIOCSLIFFLAGS, lifrp, lifrsize, cr);
18187 18187 if (err != 0) {
18188 18188 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18189 18189 "bring down (error %d); IPMP interface may "
18190 18190 "not be shutdown", lifrp->lifr_name, err);
18191 18191 }
18192 18192
18193 18193 /*
18194 18194 * Check if IFF_DUPLICATE is still set -- and if so,
18195 18195 * reset the address to clear it.
18196 18196 */
18197 18197 err = ip_ioctl(lh, SIOCGLIFFLAGS, lifrp, lifrsize, cr);
18198 18198 if (err != 0 || !(lifrp->lifr_flags & IFF_DUPLICATE))
18199 18199 continue;
18200 18200
18201 18201 err = ip_ioctl(lh, SIOCGLIFADDR, lifrp, lifrsize, cr);
18202 18202 if (err != 0 || (err = ip_ioctl(lh, SIOCGLIFADDR,
18203 18203 lifrp, lifrsize, cr)) != 0) {
18204 18204 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18205 18205 "reset DAD (error %d); IPMP interface may "
18206 18206 "not be shutdown", lifrp->lifr_name, err);
18207 18207 }
18208 18208 continue;
18209 18209 }
18210 18210
18211 18211 if (strchr(lifrp->lifr_name, IPIF_SEPARATOR_CHAR) == 0) {
18212 18212 lifrp->lifr_groupname[0] = '\0';
18213 18213 if ((err = ip_ioctl(lh, SIOCSLIFGROUPNAME, lifrp,
18214 18214 lifrsize, cr)) != 0) {
18215 18215 cmn_err(CE_WARN, "ip_ipmp_cleanup: %s: cannot "
18216 18216 "leave IPMP group (error %d); associated "
18217 18217 "IPMP interface may not be shutdown",
18218 18218 lifrp->lifr_name, err);
18219 18219 continue;
18220 18220 }
18221 18221 }
18222 18222 }
18223 18223
18224 18224 kmem_free(lifc.lifc_buf, bufsize);
18225 18225 }
18226 18226
18227 18227 #define UDPDEV "/devices/pseudo/udp@0:udp"
18228 18228 #define UDP6DEV "/devices/pseudo/udp6@0:udp6"
18229 18229
18230 18230 /*
18231 18231 * Remove the loopback interfaces and prep the IPMP interfaces to be torn down.
18232 18232 * Non-loopback interfaces are either I_LINK'd or I_PLINK'd; the former go away
18233 18233 * when the user-level processes in the zone are killed and the latter are
18234 18234 * cleaned up by str_stack_shutdown().
18235 18235 */
18236 18236 void
18237 18237 ip_interface_cleanup(ip_stack_t *ipst)
18238 18238 {
18239 18239 ldi_handle_t lh;
18240 18240 ldi_ident_t li;
18241 18241 cred_t *cr;
18242 18242 int err;
18243 18243 int i;
18244 18244 char *devs[] = { UDP6DEV, UDPDEV };
18245 18245 netstackid_t stackid = ipst->ips_netstack->netstack_stackid;
18246 18246
18247 18247 if ((err = ldi_ident_from_major(ddi_name_to_major("ip"), &li)) != 0) {
18248 18248 cmn_err(CE_WARN, "ip_interface_cleanup: cannot get ldi ident:"
18249 18249 " error %d", err);
18250 18250 return;
18251 18251 }
18252 18252
18253 18253 cr = zone_get_kcred(netstackid_to_zoneid(stackid));
18254 18254 ASSERT(cr != NULL);
18255 18255
18256 18256 /*
18257 18257 * NOTE: loop executes exactly twice and is hardcoded to know that the
18258 18258 * first iteration is IPv6. (Unrolling yields repetitious code, hence
18259 18259 * the loop.)
18260 18260 */
18261 18261 for (i = 0; i < 2; i++) {
18262 18262 err = ldi_open_by_name(devs[i], FREAD|FWRITE, cr, &lh, li);
18263 18263 if (err != 0) {
18264 18264 cmn_err(CE_WARN, "ip_interface_cleanup: cannot open %s:"
18265 18265 " error %d", devs[i], err);
18266 18266 continue;
18267 18267 }
18268 18268
18269 18269 ip_loopback_removeif(lh, i == 0, cr);
18270 18270 ip_ipmp_cleanup(lh, i == 0, cr);
18271 18271
18272 18272 (void) ldi_close(lh, FREAD|FWRITE, cr);
18273 18273 }
18274 18274
18275 18275 ldi_ident_release(li);
18276 18276 crfree(cr);
18277 18277 }
18278 18278
18279 18279 /*
18280 18280 * This needs to be in-sync with nic_event_t definition
18281 18281 */
18282 18282 static const char *
18283 18283 ill_hook_event2str(nic_event_t event)
18284 18284 {
18285 18285 switch (event) {
18286 18286 case NE_PLUMB:
18287 18287 return ("PLUMB");
18288 18288 case NE_UNPLUMB:
18289 18289 return ("UNPLUMB");
18290 18290 case NE_UP:
18291 18291 return ("UP");
18292 18292 case NE_DOWN:
18293 18293 return ("DOWN");
18294 18294 case NE_ADDRESS_CHANGE:
18295 18295 return ("ADDRESS_CHANGE");
18296 18296 case NE_LIF_UP:
18297 18297 return ("LIF_UP");
18298 18298 case NE_LIF_DOWN:
18299 18299 return ("LIF_DOWN");
18300 18300 case NE_IFINDEX_CHANGE:
18301 18301 return ("IFINDEX_CHANGE");
18302 18302 default:
18303 18303 return ("UNKNOWN");
18304 18304 }
18305 18305 }
18306 18306
18307 18307 void
18308 18308 ill_nic_event_dispatch(ill_t *ill, lif_if_t lif, nic_event_t event,
18309 18309 nic_event_data_t data, size_t datalen)
18310 18310 {
18311 18311 ip_stack_t *ipst = ill->ill_ipst;
18312 18312 hook_nic_event_int_t *info;
18313 18313 const char *str = NULL;
18314 18314
18315 18315 /* create a new nic event info */
18316 18316 if ((info = kmem_alloc(sizeof (*info), KM_NOSLEEP)) == NULL)
18317 18317 goto fail;
18318 18318
18319 18319 info->hnei_event.hne_nic = ill->ill_phyint->phyint_ifindex;
18320 18320 info->hnei_event.hne_lif = lif;
18321 18321 info->hnei_event.hne_event = event;
18322 18322 info->hnei_event.hne_protocol = ill->ill_isv6 ?
18323 18323 ipst->ips_ipv6_net_data : ipst->ips_ipv4_net_data;
18324 18324 info->hnei_event.hne_data = NULL;
18325 18325 info->hnei_event.hne_datalen = 0;
18326 18326 info->hnei_stackid = ipst->ips_netstack->netstack_stackid;
18327 18327
18328 18328 if (data != NULL && datalen != 0) {
18329 18329 info->hnei_event.hne_data = kmem_alloc(datalen, KM_NOSLEEP);
18330 18330 if (info->hnei_event.hne_data == NULL)
18331 18331 goto fail;
18332 18332 bcopy(data, info->hnei_event.hne_data, datalen);
18333 18333 info->hnei_event.hne_datalen = datalen;
18334 18334 }
18335 18335
18336 18336 if (ddi_taskq_dispatch(eventq_queue_nic, ip_ne_queue_func, info,
18337 18337 DDI_NOSLEEP) == DDI_SUCCESS)
18338 18338 return;
18339 18339
18340 18340 fail:
18341 18341 if (info != NULL) {
18342 18342 if (info->hnei_event.hne_data != NULL) {
18343 18343 kmem_free(info->hnei_event.hne_data,
18344 18344 info->hnei_event.hne_datalen);
18345 18345 }
18346 18346 kmem_free(info, sizeof (hook_nic_event_t));
18347 18347 }
18348 18348 str = ill_hook_event2str(event);
18349 18349 ip2dbg(("ill_nic_event_dispatch: could not dispatch %s nic event "
18350 18350 "information for %s (ENOMEM)\n", str, ill->ill_name));
18351 18351 }
18352 18352
18353 18353 static int
18354 18354 ipif_arp_up_done_tail(ipif_t *ipif, enum ip_resolver_action res_act)
18355 18355 {
18356 18356 int err = 0;
18357 18357 const in_addr_t *addr = NULL;
18358 18358 nce_t *nce = NULL;
18359 18359 ill_t *ill = ipif->ipif_ill;
18360 18360 ill_t *bound_ill;
18361 18361 boolean_t added_ipif = B_FALSE;
18362 18362 uint16_t state;
18363 18363 uint16_t flags;
18364 18364
18365 18365 DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up_done_tail",
18366 18366 ill_t *, ill, ipif_t *, ipif);
18367 18367 if (ipif->ipif_lcl_addr != INADDR_ANY) {
18368 18368 addr = &ipif->ipif_lcl_addr;
18369 18369 }
18370 18370
18371 18371 if ((ipif->ipif_flags & IPIF_UNNUMBERED) || addr == NULL) {
18372 18372 if (res_act != Res_act_initial)
18373 18373 return (EINVAL);
18374 18374 }
18375 18375
18376 18376 if (addr != NULL) {
18377 18377 ipmp_illgrp_t *illg = ill->ill_grp;
18378 18378
18379 18379 /* add unicast nce for the local addr */
18380 18380
18381 18381 if (IS_IPMP(ill)) {
18382 18382 /*
18383 18383 * If we're here via ipif_up(), then the ipif
18384 18384 * won't be bound yet -- add it to the group,
18385 18385 * which will bind it if possible. (We would
18386 18386 * add it in ipif_up(), but deleting on failure
18387 18387 * there is gruesome.) If we're here via
18388 18388 * ipmp_ill_bind_ipif(), then the ipif has
18389 18389 * already been added to the group and we
18390 18390 * just need to use the binding.
18391 18391 */
18392 18392 if ((bound_ill = ipmp_ipif_bound_ill(ipif)) == NULL) {
18393 18393 bound_ill = ipmp_illgrp_add_ipif(illg, ipif);
18394 18394 if (bound_ill == NULL) {
18395 18395 /*
18396 18396 * We couldn't bind the ipif to an ill
18397 18397 * yet, so we have nothing to publish.
18398 18398 * Mark the address as ready and return.
18399 18399 */
18400 18400 ipif->ipif_addr_ready = 1;
18401 18401 return (0);
18402 18402 }
18403 18403 added_ipif = B_TRUE;
18404 18404 }
18405 18405 } else {
18406 18406 bound_ill = ill;
18407 18407 }
18408 18408
18409 18409 flags = (NCE_F_MYADDR | NCE_F_PUBLISH | NCE_F_AUTHORITY |
18410 18410 NCE_F_NONUD);
18411 18411 /*
18412 18412 * If this is an initial bring-up (or the ipif was never
18413 18413 * completely brought up), do DAD. Otherwise, we're here
18414 18414 * because IPMP has rebound an address to this ill: send
18415 18415 * unsolicited advertisements (ARP announcements) to
18416 18416 * inform others.
18417 18417 */
18418 18418 if (res_act == Res_act_initial || !ipif->ipif_addr_ready) {
18419 18419 state = ND_UNCHANGED; /* compute in nce_add_common() */
18420 18420 } else {
18421 18421 state = ND_REACHABLE;
18422 18422 flags |= NCE_F_UNSOL_ADV;
18423 18423 }
18424 18424
18425 18425 retry:
18426 18426 err = nce_lookup_then_add_v4(ill,
18427 18427 bound_ill->ill_phys_addr, bound_ill->ill_phys_addr_length,
18428 18428 addr, flags, state, &nce);
18429 18429
18430 18430 /*
18431 18431 * note that we may encounter EEXIST if we are moving
18432 18432 * the nce as a result of a rebind operation.
18433 18433 */
18434 18434 switch (err) {
18435 18435 case 0:
18436 18436 ipif->ipif_added_nce = 1;
18437 18437 nce->nce_ipif_cnt++;
18438 18438 break;
18439 18439 case EEXIST:
18440 18440 ip1dbg(("ipif_arp_up: NCE already exists for %s\n",
18441 18441 ill->ill_name));
18442 18442 if (!NCE_MYADDR(nce->nce_common)) {
18443 18443 /*
18444 18444 * A leftover nce from before this address
18445 18445 * existed
18446 18446 */
18447 18447 ncec_delete(nce->nce_common);
18448 18448 nce_refrele(nce);
18449 18449 nce = NULL;
18450 18450 goto retry;
18451 18451 }
18452 18452 if ((ipif->ipif_flags & IPIF_POINTOPOINT) == 0) {
18453 18453 nce_refrele(nce);
18454 18454 nce = NULL;
18455 18455 ip1dbg(("ipif_arp_up: NCE already exists "
18456 18456 "for %s:%u\n", ill->ill_name,
18457 18457 ipif->ipif_id));
18458 18458 goto arp_up_done;
18459 18459 }
18460 18460 /*
18461 18461 * Duplicate local addresses are permissible for
18462 18462 * IPIF_POINTOPOINT interfaces which will get marked
18463 18463 * IPIF_UNNUMBERED later in
18464 18464 * ip_addr_availability_check().
18465 18465 *
18466 18466 * The nce_ipif_cnt field tracks the number of
18467 18467 * ipifs that have nce_addr as their local address.
18468 18468 */
18469 18469 ipif->ipif_addr_ready = 1;
18470 18470 ipif->ipif_added_nce = 1;
18471 18471 nce->nce_ipif_cnt++;
18472 18472 err = 0;
18473 18473 break;
18474 18474 default:
18475 18475 ASSERT(nce == NULL);
18476 18476 goto arp_up_done;
18477 18477 }
18478 18478 if (arp_no_defense) {
18479 18479 if ((ipif->ipif_flags & IPIF_UP) &&
18480 18480 !ipif->ipif_addr_ready)
18481 18481 ipif_up_notify(ipif);
18482 18482 ipif->ipif_addr_ready = 1;
18483 18483 }
18484 18484 } else {
18485 18485 /* zero address. nothing to publish */
18486 18486 ipif->ipif_addr_ready = 1;
18487 18487 }
18488 18488 if (nce != NULL)
18489 18489 nce_refrele(nce);
18490 18490 arp_up_done:
18491 18491 if (added_ipif && err != 0)
18492 18492 ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18493 18493 return (err);
18494 18494 }
18495 18495
18496 18496 int
18497 18497 ipif_arp_up(ipif_t *ipif, enum ip_resolver_action res_act, boolean_t was_dup)
18498 18498 {
18499 18499 int err = 0;
18500 18500 ill_t *ill = ipif->ipif_ill;
18501 18501 boolean_t first_interface, wait_for_dlpi = B_FALSE;
18502 18502
18503 18503 DTRACE_PROBE3(ipif__downup, char *, "ipif_arp_up",
18504 18504 ill_t *, ill, ipif_t *, ipif);
18505 18505
18506 18506 /*
18507 18507 * need to bring up ARP or setup mcast mapping only
18508 18508 * when the first interface is coming UP.
18509 18509 */
18510 18510 first_interface = (ill->ill_ipif_up_count == 0 &&
18511 18511 ill->ill_ipif_dup_count == 0 && !was_dup);
18512 18512
18513 18513 if (res_act == Res_act_initial && first_interface) {
18514 18514 /*
18515 18515 * Send ATTACH + BIND
18516 18516 */
18517 18517 err = arp_ll_up(ill);
18518 18518 if (err != EINPROGRESS && err != 0)
18519 18519 return (err);
18520 18520
18521 18521 /*
18522 18522 * Add NCE for local address. Start DAD.
18523 18523 * we'll wait to hear that DAD has finished
18524 18524 * before using the interface.
18525 18525 */
18526 18526 if (err == EINPROGRESS)
18527 18527 wait_for_dlpi = B_TRUE;
18528 18528 }
18529 18529
18530 18530 if (!wait_for_dlpi)
18531 18531 (void) ipif_arp_up_done_tail(ipif, res_act);
18532 18532
18533 18533 return (!wait_for_dlpi ? 0 : EINPROGRESS);
18534 18534 }
18535 18535
18536 18536 /*
18537 18537 * Finish processing of "arp_up" after all the DLPI message
18538 18538 * exchanges have completed between arp and the driver.
18539 18539 */
18540 18540 void
18541 18541 arp_bringup_done(ill_t *ill, int err)
18542 18542 {
18543 18543 mblk_t *mp1;
18544 18544 ipif_t *ipif;
18545 18545 conn_t *connp = NULL;
18546 18546 ipsq_t *ipsq;
18547 18547 queue_t *q;
18548 18548
18549 18549 ip1dbg(("arp_bringup_done(%s)\n", ill->ill_name));
18550 18550
18551 18551 ASSERT(IAM_WRITER_ILL(ill));
18552 18552
18553 18553 ipsq = ill->ill_phyint->phyint_ipsq;
18554 18554 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18555 18555 mp1 = ipsq_pending_mp_get(ipsq, &connp);
18556 18556 ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18557 18557 if (mp1 == NULL) /* bringup was aborted by the user */
18558 18558 return;
18559 18559
18560 18560 /*
18561 18561 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18562 18562 * must have an associated conn_t. Otherwise, we're bringing this
18563 18563 * interface back up as part of handling an asynchronous event (e.g.,
18564 18564 * physical address change).
18565 18565 */
18566 18566 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18567 18567 ASSERT(connp != NULL);
18568 18568 q = CONNP_TO_WQ(connp);
18569 18569 } else {
18570 18570 ASSERT(connp == NULL);
18571 18571 q = ill->ill_rq;
18572 18572 }
18573 18573 if (err == 0) {
18574 18574 if (ipif->ipif_isv6) {
18575 18575 if ((err = ipif_up_done_v6(ipif)) != 0)
18576 18576 ip0dbg(("arp_bringup_done: init failed\n"));
18577 18577 } else {
18578 18578 err = ipif_arp_up_done_tail(ipif, Res_act_initial);
18579 18579 if (err != 0 ||
18580 18580 (err = ipif_up_done(ipif)) != 0) {
18581 18581 ip0dbg(("arp_bringup_done: "
18582 18582 "init failed err %x\n", err));
18583 18583 (void) ipif_arp_down(ipif);
18584 18584 }
18585 18585
18586 18586 }
18587 18587 } else {
18588 18588 ip0dbg(("arp_bringup_done: DL_BIND_REQ failed\n"));
18589 18589 }
18590 18590
18591 18591 if ((err == 0) && (ill->ill_up_ipifs)) {
18592 18592 err = ill_up_ipifs(ill, q, mp1);
18593 18593 if (err == EINPROGRESS)
18594 18594 return;
18595 18595 }
18596 18596
18597 18597 /*
18598 18598 * If we have a moved ipif to bring up, and everything has succeeded
18599 18599 * to this point, bring it up on the IPMP ill. Otherwise, leave it
18600 18600 * down -- the admin can try to bring it up by hand if need be.
18601 18601 */
18602 18602 if (ill->ill_move_ipif != NULL) {
18603 18603 ipif = ill->ill_move_ipif;
18604 18604 ip1dbg(("bringing up ipif %p on ill %s\n", (void *)ipif,
18605 18605 ipif->ipif_ill->ill_name));
18606 18606 ill->ill_move_ipif = NULL;
18607 18607 if (err == 0) {
18608 18608 err = ipif_up(ipif, q, mp1);
18609 18609 if (err == EINPROGRESS)
18610 18610 return;
18611 18611 }
18612 18612 }
18613 18613
18614 18614 /*
18615 18615 * The operation must complete without EINPROGRESS since
18616 18616 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18617 18617 * Otherwise, the operation will be stuck forever in the ipsq.
18618 18618 */
18619 18619 ASSERT(err != EINPROGRESS);
18620 18620 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18621 18621 DTRACE_PROBE4(ipif__ioctl, char *, "arp_bringup_done finish",
18622 18622 int, ipsq->ipsq_xop->ipx_current_ioctl,
18623 18623 ill_t *, ill, ipif_t *, ipif);
18624 18624 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18625 18625 } else {
18626 18626 ipsq_current_finish(ipsq);
18627 18627 }
18628 18628 }
18629 18629
18630 18630 /*
18631 18631 * Finish processing of arp replumb after all the DLPI message
18632 18632 * exchanges have completed between arp and the driver.
18633 18633 */
18634 18634 void
18635 18635 arp_replumb_done(ill_t *ill, int err)
18636 18636 {
18637 18637 mblk_t *mp1;
18638 18638 ipif_t *ipif;
18639 18639 conn_t *connp = NULL;
18640 18640 ipsq_t *ipsq;
18641 18641 queue_t *q;
18642 18642
18643 18643 ASSERT(IAM_WRITER_ILL(ill));
18644 18644
18645 18645 ipsq = ill->ill_phyint->phyint_ipsq;
18646 18646 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
18647 18647 mp1 = ipsq_pending_mp_get(ipsq, &connp);
18648 18648 ASSERT(!((mp1 != NULL) ^ (ipif != NULL)));
18649 18649 if (mp1 == NULL) {
18650 18650 ip0dbg(("arp_replumb_done: bringup aborted ioctl %x\n",
18651 18651 ipsq->ipsq_xop->ipx_current_ioctl));
18652 18652 /* bringup was aborted by the user */
18653 18653 return;
18654 18654 }
18655 18655 /*
18656 18656 * If an IOCTL is waiting on this (ipsq_current_ioctl != 0), then we
18657 18657 * must have an associated conn_t. Otherwise, we're bringing this
18658 18658 * interface back up as part of handling an asynchronous event (e.g.,
18659 18659 * physical address change).
18660 18660 */
18661 18661 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18662 18662 ASSERT(connp != NULL);
18663 18663 q = CONNP_TO_WQ(connp);
18664 18664 } else {
18665 18665 ASSERT(connp == NULL);
18666 18666 q = ill->ill_rq;
18667 18667 }
18668 18668 if ((err == 0) && (ill->ill_up_ipifs)) {
18669 18669 err = ill_up_ipifs(ill, q, mp1);
18670 18670 if (err == EINPROGRESS)
18671 18671 return;
18672 18672 }
18673 18673 /*
18674 18674 * The operation must complete without EINPROGRESS since
18675 18675 * ipsq_pending_mp_get() has removed the mblk from ipsq_pending_mp.
18676 18676 * Otherwise, the operation will be stuck forever in the ipsq.
18677 18677 */
18678 18678 ASSERT(err != EINPROGRESS);
18679 18679 if (ipsq->ipsq_xop->ipx_current_ioctl != 0) {
18680 18680 DTRACE_PROBE4(ipif__ioctl, char *,
18681 18681 "arp_replumb_done finish",
18682 18682 int, ipsq->ipsq_xop->ipx_current_ioctl,
18683 18683 ill_t *, ill, ipif_t *, ipif);
18684 18684 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
18685 18685 } else {
18686 18686 ipsq_current_finish(ipsq);
18687 18687 }
18688 18688 }
18689 18689
18690 18690 void
18691 18691 ipif_up_notify(ipif_t *ipif)
18692 18692 {
18693 18693 ip_rts_ifmsg(ipif, RTSQ_DEFAULT);
18694 18694 ip_rts_newaddrmsg(RTM_ADD, 0, ipif, RTSQ_DEFAULT);
18695 18695 sctp_update_ipif(ipif, SCTP_IPIF_UP);
18696 18696 ill_nic_event_dispatch(ipif->ipif_ill, MAP_IPIF_ID(ipif->ipif_id),
18697 18697 NE_LIF_UP, NULL, 0);
18698 18698 }
18699 18699
18700 18700 /*
18701 18701 * ILB ioctl uses cv_wait (such as deleting a rule or adding a server) and
18702 18702 * this assumes the context is cv_wait'able. Hence it shouldnt' be used on
18703 18703 * TPI end points with STREAMS modules pushed above. This is assured by not
18704 18704 * having the IPI_MODOK flag for the ioctl. And IP ensures the ILB ioctl
18705 18705 * never ends up on an ipsq, otherwise we may end up processing the ioctl
18706 18706 * while unwinding from the ispq and that could be a thread from the bottom.
18707 18707 */
18708 18708 /* ARGSUSED */
18709 18709 int
18710 18710 ip_sioctl_ilb_cmd(ipif_t *ipif, sin_t *sin, queue_t *q, mblk_t *mp,
18711 18711 ip_ioctl_cmd_t *ipip, void *arg)
18712 18712 {
18713 18713 mblk_t *cmd_mp = mp->b_cont->b_cont;
18714 18714 ilb_cmd_t command = *((ilb_cmd_t *)cmd_mp->b_rptr);
18715 18715 int ret = 0;
18716 18716 int i;
18717 18717 size_t size;
18718 18718 ip_stack_t *ipst;
18719 18719 zoneid_t zoneid;
18720 18720 ilb_stack_t *ilbs;
18721 18721
18722 18722 ipst = CONNQ_TO_IPST(q);
18723 18723 ilbs = ipst->ips_netstack->netstack_ilb;
18724 18724 zoneid = Q_TO_CONN(q)->conn_zoneid;
18725 18725
18726 18726 switch (command) {
18727 18727 case ILB_CREATE_RULE: {
18728 18728 ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18729 18729
18730 18730 if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18731 18731 ret = EINVAL;
18732 18732 break;
18733 18733 }
18734 18734
18735 18735 ret = ilb_rule_add(ilbs, zoneid, cmd);
18736 18736 break;
18737 18737 }
18738 18738 case ILB_DESTROY_RULE:
18739 18739 case ILB_ENABLE_RULE:
18740 18740 case ILB_DISABLE_RULE: {
18741 18741 ilb_name_cmd_t *cmd = (ilb_name_cmd_t *)cmd_mp->b_rptr;
18742 18742
18743 18743 if (MBLKL(cmd_mp) != sizeof (ilb_name_cmd_t)) {
18744 18744 ret = EINVAL;
18745 18745 break;
18746 18746 }
18747 18747
18748 18748 if (cmd->flags & ILB_RULE_ALLRULES) {
18749 18749 if (command == ILB_DESTROY_RULE) {
18750 18750 ilb_rule_del_all(ilbs, zoneid);
18751 18751 break;
18752 18752 } else if (command == ILB_ENABLE_RULE) {
18753 18753 ilb_rule_enable_all(ilbs, zoneid);
18754 18754 break;
18755 18755 } else if (command == ILB_DISABLE_RULE) {
18756 18756 ilb_rule_disable_all(ilbs, zoneid);
18757 18757 break;
18758 18758 }
18759 18759 } else {
18760 18760 if (command == ILB_DESTROY_RULE) {
18761 18761 ret = ilb_rule_del(ilbs, zoneid, cmd->name);
18762 18762 } else if (command == ILB_ENABLE_RULE) {
18763 18763 ret = ilb_rule_enable(ilbs, zoneid, cmd->name,
18764 18764 NULL);
18765 18765 } else if (command == ILB_DISABLE_RULE) {
18766 18766 ret = ilb_rule_disable(ilbs, zoneid, cmd->name,
18767 18767 NULL);
18768 18768 }
18769 18769 }
18770 18770 break;
18771 18771 }
18772 18772 case ILB_NUM_RULES: {
18773 18773 ilb_num_rules_cmd_t *cmd;
18774 18774
18775 18775 if (MBLKL(cmd_mp) != sizeof (ilb_num_rules_cmd_t)) {
18776 18776 ret = EINVAL;
18777 18777 break;
18778 18778 }
18779 18779 cmd = (ilb_num_rules_cmd_t *)cmd_mp->b_rptr;
18780 18780 ilb_get_num_rules(ilbs, zoneid, &(cmd->num));
18781 18781 break;
18782 18782 }
18783 18783 case ILB_RULE_NAMES: {
18784 18784 ilb_rule_names_cmd_t *cmd;
18785 18785
18786 18786 cmd = (ilb_rule_names_cmd_t *)cmd_mp->b_rptr;
18787 18787 if (MBLKL(cmd_mp) < sizeof (ilb_rule_names_cmd_t) ||
18788 18788 cmd->num_names == 0) {
18789 18789 ret = EINVAL;
18790 18790 break;
18791 18791 }
18792 18792 size = cmd->num_names * ILB_RULE_NAMESZ;
18793 18793 if (cmd_mp->b_rptr + offsetof(ilb_rule_names_cmd_t, buf) +
18794 18794 size != cmd_mp->b_wptr) {
18795 18795 ret = EINVAL;
18796 18796 break;
18797 18797 }
18798 18798 ilb_get_rulenames(ilbs, zoneid, &cmd->num_names, cmd->buf);
18799 18799 break;
18800 18800 }
18801 18801 case ILB_NUM_SERVERS: {
18802 18802 ilb_num_servers_cmd_t *cmd;
18803 18803
18804 18804 if (MBLKL(cmd_mp) != sizeof (ilb_num_servers_cmd_t)) {
18805 18805 ret = EINVAL;
18806 18806 break;
18807 18807 }
18808 18808 cmd = (ilb_num_servers_cmd_t *)cmd_mp->b_rptr;
18809 18809 ret = ilb_get_num_servers(ilbs, zoneid, cmd->name,
18810 18810 &(cmd->num));
18811 18811 break;
18812 18812 }
18813 18813 case ILB_LIST_RULE: {
18814 18814 ilb_rule_cmd_t *cmd = (ilb_rule_cmd_t *)cmd_mp->b_rptr;
18815 18815
18816 18816 if (MBLKL(cmd_mp) != sizeof (ilb_rule_cmd_t)) {
18817 18817 ret = EINVAL;
18818 18818 break;
18819 18819 }
18820 18820 ret = ilb_rule_list(ilbs, zoneid, cmd);
18821 18821 break;
18822 18822 }
18823 18823 case ILB_LIST_SERVERS: {
18824 18824 ilb_servers_info_cmd_t *cmd;
18825 18825
18826 18826 cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18827 18827 if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t) ||
18828 18828 cmd->num_servers == 0) {
18829 18829 ret = EINVAL;
18830 18830 break;
18831 18831 }
18832 18832 size = cmd->num_servers * sizeof (ilb_server_info_t);
18833 18833 if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18834 18834 size != cmd_mp->b_wptr) {
18835 18835 ret = EINVAL;
18836 18836 break;
18837 18837 }
18838 18838
18839 18839 ret = ilb_get_servers(ilbs, zoneid, cmd->name, cmd->servers,
18840 18840 &cmd->num_servers);
18841 18841 break;
18842 18842 }
18843 18843 case ILB_ADD_SERVERS: {
18844 18844 ilb_servers_info_cmd_t *cmd;
18845 18845 ilb_rule_t *rule;
18846 18846
18847 18847 cmd = (ilb_servers_info_cmd_t *)cmd_mp->b_rptr;
18848 18848 if (MBLKL(cmd_mp) < sizeof (ilb_servers_info_cmd_t)) {
18849 18849 ret = EINVAL;
18850 18850 break;
18851 18851 }
18852 18852 size = cmd->num_servers * sizeof (ilb_server_info_t);
18853 18853 if (cmd_mp->b_rptr + offsetof(ilb_servers_info_cmd_t, servers) +
18854 18854 size != cmd_mp->b_wptr) {
18855 18855 ret = EINVAL;
18856 18856 break;
18857 18857 }
18858 18858 rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18859 18859 if (rule == NULL) {
18860 18860 ASSERT(ret != 0);
18861 18861 break;
18862 18862 }
18863 18863 for (i = 0; i < cmd->num_servers; i++) {
18864 18864 ilb_server_info_t *s;
18865 18865
18866 18866 s = &cmd->servers[i];
18867 18867 s->err = ilb_server_add(ilbs, rule, s);
18868 18868 }
18869 18869 ILB_RULE_REFRELE(rule);
18870 18870 break;
18871 18871 }
18872 18872 case ILB_DEL_SERVERS:
18873 18873 case ILB_ENABLE_SERVERS:
18874 18874 case ILB_DISABLE_SERVERS: {
18875 18875 ilb_servers_cmd_t *cmd;
18876 18876 ilb_rule_t *rule;
18877 18877 int (*f)();
18878 18878
18879 18879 cmd = (ilb_servers_cmd_t *)cmd_mp->b_rptr;
18880 18880 if (MBLKL(cmd_mp) < sizeof (ilb_servers_cmd_t)) {
18881 18881 ret = EINVAL;
18882 18882 break;
18883 18883 }
18884 18884 size = cmd->num_servers * sizeof (ilb_server_arg_t);
18885 18885 if (cmd_mp->b_rptr + offsetof(ilb_servers_cmd_t, servers) +
18886 18886 size != cmd_mp->b_wptr) {
18887 18887 ret = EINVAL;
18888 18888 break;
18889 18889 }
18890 18890
18891 18891 if (command == ILB_DEL_SERVERS)
18892 18892 f = ilb_server_del;
18893 18893 else if (command == ILB_ENABLE_SERVERS)
18894 18894 f = ilb_server_enable;
18895 18895 else if (command == ILB_DISABLE_SERVERS)
18896 18896 f = ilb_server_disable;
18897 18897
18898 18898 rule = ilb_find_rule(ilbs, zoneid, cmd->name, &ret);
18899 18899 if (rule == NULL) {
18900 18900 ASSERT(ret != 0);
18901 18901 break;
18902 18902 }
18903 18903
18904 18904 for (i = 0; i < cmd->num_servers; i++) {
18905 18905 ilb_server_arg_t *s;
18906 18906
18907 18907 s = &cmd->servers[i];
18908 18908 s->err = f(ilbs, zoneid, NULL, rule, &s->addr);
18909 18909 }
18910 18910 ILB_RULE_REFRELE(rule);
18911 18911 break;
18912 18912 }
18913 18913 case ILB_LIST_NAT_TABLE: {
18914 18914 ilb_list_nat_cmd_t *cmd;
18915 18915
18916 18916 cmd = (ilb_list_nat_cmd_t *)cmd_mp->b_rptr;
18917 18917 if (MBLKL(cmd_mp) < sizeof (ilb_list_nat_cmd_t)) {
18918 18918 ret = EINVAL;
18919 18919 break;
18920 18920 }
18921 18921 size = cmd->num_nat * sizeof (ilb_nat_entry_t);
18922 18922 if (cmd_mp->b_rptr + offsetof(ilb_list_nat_cmd_t, entries) +
18923 18923 size != cmd_mp->b_wptr) {
18924 18924 ret = EINVAL;
18925 18925 break;
18926 18926 }
18927 18927
18928 18928 ret = ilb_list_nat(ilbs, zoneid, cmd->entries, &cmd->num_nat,
18929 18929 &cmd->flags);
18930 18930 break;
18931 18931 }
18932 18932 case ILB_LIST_STICKY_TABLE: {
18933 18933 ilb_list_sticky_cmd_t *cmd;
18934 18934
18935 18935 cmd = (ilb_list_sticky_cmd_t *)cmd_mp->b_rptr;
18936 18936 if (MBLKL(cmd_mp) < sizeof (ilb_list_sticky_cmd_t)) {
18937 18937 ret = EINVAL;
18938 18938 break;
18939 18939 }
18940 18940 size = cmd->num_sticky * sizeof (ilb_sticky_entry_t);
18941 18941 if (cmd_mp->b_rptr + offsetof(ilb_list_sticky_cmd_t, entries) +
18942 18942 size != cmd_mp->b_wptr) {
18943 18943 ret = EINVAL;
18944 18944 break;
18945 18945 }
18946 18946
18947 18947 ret = ilb_list_sticky(ilbs, zoneid, cmd->entries,
18948 18948 &cmd->num_sticky, &cmd->flags);
18949 18949 break;
18950 18950 }
18951 18951 default:
18952 18952 ret = EINVAL;
18953 18953 break;
18954 18954 }
18955 18955 done:
18956 18956 return (ret);
18957 18957 }
18958 18958
18959 18959 /* Remove all cache entries for this logical interface */
18960 18960 void
18961 18961 ipif_nce_down(ipif_t *ipif)
18962 18962 {
18963 18963 ill_t *ill = ipif->ipif_ill;
18964 18964 nce_t *nce;
18965 18965
18966 18966 DTRACE_PROBE3(ipif__downup, char *, "ipif_nce_down",
18967 18967 ill_t *, ill, ipif_t *, ipif);
18968 18968 if (ipif->ipif_added_nce) {
18969 18969 if (ipif->ipif_isv6)
18970 18970 nce = nce_lookup_v6(ill, &ipif->ipif_v6lcl_addr);
18971 18971 else
18972 18972 nce = nce_lookup_v4(ill, &ipif->ipif_lcl_addr);
18973 18973 if (nce != NULL) {
18974 18974 if (--nce->nce_ipif_cnt == 0)
18975 18975 ncec_delete(nce->nce_common);
18976 18976 ipif->ipif_added_nce = 0;
18977 18977 nce_refrele(nce);
18978 18978 } else {
18979 18979 /*
18980 18980 * nce may already be NULL because it was already
18981 18981 * flushed, e.g., due to a call to nce_flush
18982 18982 */
18983 18983 ipif->ipif_added_nce = 0;
18984 18984 }
18985 18985 }
18986 18986 /*
18987 18987 * Make IPMP aware of the deleted data address.
18988 18988 */
18989 18989 if (IS_IPMP(ill))
18990 18990 ipmp_illgrp_del_ipif(ill->ill_grp, ipif);
18991 18991
18992 18992 /*
18993 18993 * Remove all other nces dependent on this ill when the last ipif
18994 18994 * is going away.
18995 18995 */
18996 18996 if (ill->ill_ipif_up_count == 0) {
18997 18997 ncec_walk(ill, (pfi_t)ncec_delete_per_ill,
18998 18998 (uchar_t *)ill, ill->ill_ipst);
18999 18999 if (IS_UNDER_IPMP(ill))
19000 19000 nce_flush(ill, B_TRUE);
19001 19001 }
19002 19002 }
19003 19003
19004 19004 /*
19005 19005 * find the first interface that uses usill for its source address.
19006 19006 */
19007 19007 ill_t *
19008 19008 ill_lookup_usesrc(ill_t *usill)
19009 19009 {
19010 19010 ip_stack_t *ipst = usill->ill_ipst;
19011 19011 ill_t *ill;
19012 19012
19013 19013 ASSERT(usill != NULL);
19014 19014
19015 19015 /* ill_g_usesrc_lock protects ill_usesrc_grp_next */
19016 19016 rw_enter(&ipst->ips_ill_g_usesrc_lock, RW_WRITER);
19017 19017 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
19018 19018 for (ill = usill->ill_usesrc_grp_next; ill != NULL && ill != usill;
19019 19019 ill = ill->ill_usesrc_grp_next) {
19020 19020 if (!IS_UNDER_IPMP(ill) && (ill->ill_flags & ILLF_MULTICAST) &&
19021 19021 !ILL_IS_CONDEMNED(ill)) {
19022 19022 ill_refhold(ill);
19023 19023 break;
19024 19024 }
19025 19025 }
19026 19026 rw_exit(&ipst->ips_ill_g_lock);
19027 19027 rw_exit(&ipst->ips_ill_g_usesrc_lock);
19028 19028 return (ill);
19029 19029 }
19030 19030
19031 19031 /*
19032 19032 * This comment applies to both ip_sioctl_get_ifhwaddr and
19033 19033 * ip_sioctl_get_lifhwaddr as the basic function of these two functions
19034 19034 * is the same.
19035 19035 *
19036 19036 * The goal here is to find an IP interface that corresponds to the name
19037 19037 * provided by the caller in the ifreq/lifreq structure held in the mblk_t
19038 19038 * chain and to fill out a sockaddr/sockaddr_storage structure with the
19039 19039 * mac address.
19040 19040 *
19041 19041 * The SIOCGIFHWADDR/SIOCGLIFHWADDR ioctl may return an error for a number
19042 19042 * of different reasons:
19043 19043 * ENXIO - the device name is not known to IP.
19044 19044 * EADDRNOTAVAIL - the device has no hardware address. This is indicated
19045 19045 * by ill_phys_addr not pointing to an actual address.
19046 19046 * EPFNOSUPPORT - this will indicate that a request is being made for a
19047 19047 * mac address that will not fit in the data structure supplier (struct
19048 19048 * sockaddr).
19049 19049 *
19050 19050 */
19051 19051 /* ARGSUSED */
19052 19052 int
19053 19053 ip_sioctl_get_ifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19054 19054 ip_ioctl_cmd_t *ipip, void *if_req)
19055 19055 {
19056 19056 struct sockaddr *sock;
19057 19057 struct ifreq *ifr;
19058 19058 mblk_t *mp1;
19059 19059 ill_t *ill;
19060 19060
19061 19061 ASSERT(ipif != NULL);
19062 19062 ill = ipif->ipif_ill;
19063 19063
19064 19064 if (ill->ill_phys_addr == NULL) {
19065 19065 return (EADDRNOTAVAIL);
19066 19066 }
19067 19067 if (ill->ill_phys_addr_length > sizeof (sock->sa_data)) {
19068 19068 return (EPFNOSUPPORT);
19069 19069 }
19070 19070
19071 19071 ip1dbg(("ip_sioctl_get_hwaddr(%s)\n", ill->ill_name));
19072 19072
19073 19073 /* Existence of mp1 has been checked in ip_wput_nondata */
19074 19074 mp1 = mp->b_cont->b_cont;
19075 19075 ifr = (struct ifreq *)mp1->b_rptr;
19076 19076
19077 19077 sock = &ifr->ifr_addr;
19078 19078 /*
19079 19079 * The "family" field in the returned structure is set to a value
19080 19080 * that represents the type of device to which the address belongs.
19081 19081 * The value returned may differ to that on Linux but it will still
19082 19082 * represent the correct symbol on Solaris.
19083 19083 */
19084 19084 sock->sa_family = arp_hw_type(ill->ill_mactype);
19085 19085 bcopy(ill->ill_phys_addr, &sock->sa_data, ill->ill_phys_addr_length);
19086 19086
19087 19087 return (0);
19088 19088 }
19089 19089
19090 19090 /*
19091 19091 * The expection of applications using SIOCGIFHWADDR is that data will
19092 19092 * be returned in the sa_data field of the sockaddr structure. With
19093 19093 * SIOCGLIFHWADDR, we're breaking new ground as there is no Linux
19094 19094 * equivalent. In light of this, struct sockaddr_dl is used as it
19095 19095 * offers more space for address storage in sll_data.
19096 19096 */
19097 19097 /* ARGSUSED */
19098 19098 int
19099 19099 ip_sioctl_get_lifhwaddr(ipif_t *ipif, sin_t *dummy_sin, queue_t *q, mblk_t *mp,
19100 19100 ip_ioctl_cmd_t *ipip, void *if_req)
19101 19101 {
19102 19102 struct sockaddr_dl *sock;
19103 19103 struct lifreq *lifr;
19104 19104 mblk_t *mp1;
19105 19105 ill_t *ill;
19106 19106
19107 19107 ASSERT(ipif != NULL);
19108 19108 ill = ipif->ipif_ill;
19109 19109
19110 19110 if (ill->ill_phys_addr == NULL) {
19111 19111 return (EADDRNOTAVAIL);
19112 19112 }
19113 19113 if (ill->ill_phys_addr_length > sizeof (sock->sdl_data)) {
19114 19114 return (EPFNOSUPPORT);
19115 19115 }
19116 19116
19117 19117 ip1dbg(("ip_sioctl_get_lifhwaddr(%s)\n", ill->ill_name));
19118 19118
19119 19119 /* Existence of mp1 has been checked in ip_wput_nondata */
19120 19120 mp1 = mp->b_cont->b_cont;
19121 19121 lifr = (struct lifreq *)mp1->b_rptr;
19122 19122
19123 19123 /*
19124 19124 * sockaddr_ll is used here because it is also the structure used in
19125 19125 * responding to the same ioctl in sockpfp. The only other choice is
19126 19126 * sockaddr_dl which contains fields that are not required here
19127 19127 * because its purpose is different.
19128 19128 */
19129 19129 lifr->lifr_type = ill->ill_type;
19130 19130 sock = (struct sockaddr_dl *)&lifr->lifr_addr;
19131 19131 sock->sdl_family = AF_LINK;
19132 19132 sock->sdl_index = ill->ill_phyint->phyint_ifindex;
19133 19133 sock->sdl_type = ill->ill_mactype;
19134 19134 sock->sdl_nlen = 0;
19135 19135 sock->sdl_slen = 0;
19136 19136 sock->sdl_alen = ill->ill_phys_addr_length;
19137 19137 bcopy(ill->ill_phys_addr, sock->sdl_data, ill->ill_phys_addr_length);
19138 19138
19139 19139 return (0);
19140 19140 }
↓ open down ↓ |
6676 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX