Print this page
7819 IPv6 Packet and MTU bug
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/inet/ip/ip.c
+++ new/usr/src/uts/common/inet/ip/ip.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
|
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
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 /*
23 23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 1990 Mentat Inc.
25 25 * Copyright (c) 2012 Joyent, Inc. All rights reserved.
26 - * Copyright (c) 2014, OmniTI Computer Consulting, Inc. All rights reserved.
26 + * Copyright (c) 2017 OmniTI Computer Consulting, Inc. All rights reserved.
27 27 */
28 28
29 29 #include <sys/types.h>
30 30 #include <sys/stream.h>
31 31 #include <sys/dlpi.h>
32 32 #include <sys/stropts.h>
33 33 #include <sys/sysmacros.h>
34 34 #include <sys/strsubr.h>
35 35 #include <sys/strlog.h>
36 36 #include <sys/strsun.h>
37 37 #include <sys/zone.h>
38 38 #define _SUN_TPI_VERSION 2
39 39 #include <sys/tihdr.h>
40 40 #include <sys/xti_inet.h>
41 41 #include <sys/ddi.h>
42 42 #include <sys/suntpi.h>
43 43 #include <sys/cmn_err.h>
44 44 #include <sys/debug.h>
45 45 #include <sys/kobj.h>
46 46 #include <sys/modctl.h>
47 47 #include <sys/atomic.h>
48 48 #include <sys/policy.h>
49 49 #include <sys/priv.h>
50 50 #include <sys/taskq.h>
51 51
52 52 #include <sys/systm.h>
53 53 #include <sys/param.h>
54 54 #include <sys/kmem.h>
55 55 #include <sys/sdt.h>
56 56 #include <sys/socket.h>
57 57 #include <sys/vtrace.h>
58 58 #include <sys/isa_defs.h>
59 59 #include <sys/mac.h>
60 60 #include <net/if.h>
61 61 #include <net/if_arp.h>
62 62 #include <net/route.h>
63 63 #include <sys/sockio.h>
64 64 #include <netinet/in.h>
65 65 #include <net/if_dl.h>
66 66
67 67 #include <inet/common.h>
68 68 #include <inet/mi.h>
69 69 #include <inet/mib2.h>
70 70 #include <inet/nd.h>
71 71 #include <inet/arp.h>
72 72 #include <inet/snmpcom.h>
73 73 #include <inet/optcom.h>
74 74 #include <inet/kstatcom.h>
75 75
76 76 #include <netinet/igmp_var.h>
77 77 #include <netinet/ip6.h>
78 78 #include <netinet/icmp6.h>
79 79 #include <netinet/sctp.h>
80 80
81 81 #include <inet/ip.h>
82 82 #include <inet/ip_impl.h>
83 83 #include <inet/ip6.h>
84 84 #include <inet/ip6_asp.h>
85 85 #include <inet/tcp.h>
86 86 #include <inet/tcp_impl.h>
87 87 #include <inet/ip_multi.h>
88 88 #include <inet/ip_if.h>
89 89 #include <inet/ip_ire.h>
90 90 #include <inet/ip_ftable.h>
91 91 #include <inet/ip_rts.h>
92 92 #include <inet/ip_ndp.h>
93 93 #include <inet/ip_listutils.h>
94 94 #include <netinet/igmp.h>
95 95 #include <netinet/ip_mroute.h>
96 96 #include <inet/ipp_common.h>
97 97
98 98 #include <net/pfkeyv2.h>
99 99 #include <inet/sadb.h>
100 100 #include <inet/ipsec_impl.h>
101 101 #include <inet/iptun/iptun_impl.h>
102 102 #include <inet/ipdrop.h>
103 103 #include <inet/ip_netinfo.h>
104 104 #include <inet/ilb_ip.h>
105 105
106 106 #include <sys/ethernet.h>
107 107 #include <net/if_types.h>
108 108 #include <sys/cpuvar.h>
109 109
110 110 #include <ipp/ipp.h>
111 111 #include <ipp/ipp_impl.h>
112 112 #include <ipp/ipgpc/ipgpc.h>
113 113
114 114 #include <sys/pattr.h>
115 115 #include <inet/ipclassifier.h>
116 116 #include <inet/sctp_ip.h>
117 117 #include <inet/sctp/sctp_impl.h>
118 118 #include <inet/udp_impl.h>
119 119 #include <inet/rawip_impl.h>
120 120 #include <inet/rts_impl.h>
121 121
122 122 #include <sys/tsol/label.h>
123 123 #include <sys/tsol/tnet.h>
124 124
125 125 #include <sys/squeue_impl.h>
126 126 #include <inet/ip_arp.h>
127 127
128 128 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */
129 129
130 130 /*
131 131 * Values for squeue switch:
132 132 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
133 133 * IP_SQUEUE_ENTER: SQ_PROCESS
134 134 * IP_SQUEUE_FILL: SQ_FILL
135 135 */
136 136 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */
137 137
138 138 int ip_squeue_flag;
139 139
140 140 /*
141 141 * Setable in /etc/system
142 142 */
143 143 int ip_poll_normal_ms = 100;
144 144 int ip_poll_normal_ticks = 0;
145 145 int ip_modclose_ackwait_ms = 3000;
146 146
147 147 /*
148 148 * It would be nice to have these present only in DEBUG systems, but the
149 149 * current design of the global symbol checking logic requires them to be
150 150 * unconditionally present.
151 151 */
152 152 uint_t ip_thread_data; /* TSD key for debug support */
153 153 krwlock_t ip_thread_rwlock;
154 154 list_t ip_thread_list;
155 155
156 156 /*
157 157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
158 158 */
159 159
160 160 struct listptr_s {
161 161 mblk_t *lp_head; /* pointer to the head of the list */
162 162 mblk_t *lp_tail; /* pointer to the tail of the list */
163 163 };
164 164
165 165 typedef struct listptr_s listptr_t;
166 166
167 167 /*
168 168 * This is used by ip_snmp_get_mib2_ip_route_media and
169 169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
170 170 */
171 171 typedef struct iproutedata_s {
172 172 uint_t ird_idx;
173 173 uint_t ird_flags; /* see below */
174 174 listptr_t ird_route; /* ipRouteEntryTable */
175 175 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
176 176 listptr_t ird_attrs; /* ipRouteAttributeTable */
177 177 } iproutedata_t;
178 178
179 179 /* Include ire_testhidden and IRE_IF_CLONE routes */
180 180 #define IRD_REPORT_ALL 0x01
181 181
182 182 /*
183 183 * Cluster specific hooks. These should be NULL when booted as a non-cluster
184 184 */
185 185
186 186 /*
187 187 * Hook functions to enable cluster networking
188 188 * On non-clustered systems these vectors must always be NULL.
189 189 *
190 190 * Hook function to Check ip specified ip address is a shared ip address
191 191 * in the cluster
192 192 *
193 193 */
194 194 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
195 195 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
196 196
197 197 /*
198 198 * Hook function to generate cluster wide ip fragment identifier
199 199 */
200 200 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
201 201 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
202 202 void *args) = NULL;
203 203
204 204 /*
205 205 * Hook function to generate cluster wide SPI.
206 206 */
207 207 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
208 208 void *) = NULL;
209 209
210 210 /*
211 211 * Hook function to verify if the SPI is already utlized.
212 212 */
213 213
214 214 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
215 215
216 216 /*
217 217 * Hook function to delete the SPI from the cluster wide repository.
218 218 */
219 219
220 220 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
221 221
222 222 /*
223 223 * Hook function to inform the cluster when packet received on an IDLE SA
224 224 */
225 225
226 226 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
227 227 in6_addr_t, in6_addr_t, void *) = NULL;
228 228
229 229 /*
230 230 * Synchronization notes:
231 231 *
232 232 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
233 233 * MT level protection given by STREAMS. IP uses a combination of its own
234 234 * internal serialization mechanism and standard Solaris locking techniques.
235 235 * The internal serialization is per phyint. This is used to serialize
236 236 * plumbing operations, IPMP operations, most set ioctls, etc.
237 237 *
238 238 * Plumbing is a long sequence of operations involving message
239 239 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
240 240 * involved in plumbing operations. A natural model is to serialize these
241 241 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
242 242 * parallel without any interference. But various set ioctls on hme0 are best
243 243 * serialized, along with IPMP operations and processing of DLPI control
244 244 * messages received from drivers on a per phyint basis. This serialization is
245 245 * provided by the ipsq_t and primitives operating on this. Details can
246 246 * be found in ip_if.c above the core primitives operating on ipsq_t.
247 247 *
248 248 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
249 249 * Simiarly lookup of an ire by a thread also returns a refheld ire.
250 250 * In addition ipif's and ill's referenced by the ire are also indirectly
251 251 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
252 252 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
253 253 * address of an ipif has to go through the ipsq_t. This ensures that only
254 254 * one such exclusive operation proceeds at any time on the ipif. It then
255 255 * waits for all refcnts
256 256 * associated with this ipif to come down to zero. The address is changed
257 257 * only after the ipif has been quiesced. Then the ipif is brought up again.
258 258 * More details are described above the comment in ip_sioctl_flags.
259 259 *
260 260 * Packet processing is based mostly on IREs and are fully multi-threaded
261 261 * using standard Solaris MT techniques.
262 262 *
263 263 * There are explicit locks in IP to handle:
264 264 * - The ip_g_head list maintained by mi_open_link() and friends.
265 265 *
266 266 * - The reassembly data structures (one lock per hash bucket)
267 267 *
268 268 * - conn_lock is meant to protect conn_t fields. The fields actually
269 269 * protected by conn_lock are documented in the conn_t definition.
270 270 *
271 271 * - ire_lock to protect some of the fields of the ire, IRE tables
272 272 * (one lock per hash bucket). Refer to ip_ire.c for details.
273 273 *
274 274 * - ndp_g_lock and ncec_lock for protecting NCEs.
275 275 *
276 276 * - ill_lock protects fields of the ill and ipif. Details in ip.h
277 277 *
278 278 * - ill_g_lock: This is a global reader/writer lock. Protects the following
279 279 * * The AVL tree based global multi list of all ills.
280 280 * * The linked list of all ipifs of an ill
281 281 * * The <ipsq-xop> mapping
282 282 * * <ill-phyint> association
283 283 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
284 284 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the
285 285 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
286 286 * writer for the actual duration of the insertion/deletion/change.
287 287 *
288 288 * - ill_lock: This is a per ill mutex.
289 289 * It protects some members of the ill_t struct; see ip.h for details.
290 290 * It also protects the <ill-phyint> assoc.
291 291 * It also protects the list of ipifs hanging off the ill.
292 292 *
293 293 * - ipsq_lock: This is a per ipsq_t mutex lock.
294 294 * This protects some members of the ipsq_t struct; see ip.h for details.
295 295 * It also protects the <ipsq-ipxop> mapping
296 296 *
297 297 * - ipx_lock: This is a per ipxop_t mutex lock.
298 298 * This protects some members of the ipxop_t struct; see ip.h for details.
299 299 *
300 300 * - phyint_lock: This is a per phyint mutex lock. Protects just the
301 301 * phyint_flags
302 302 *
303 303 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
304 304 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
305 305 * uniqueness check also done atomically.
306 306 *
307 307 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
308 308 * group list linked by ill_usesrc_grp_next. It also protects the
309 309 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
310 310 * group is being added or deleted. This lock is taken as a reader when
311 311 * walking the list/group(eg: to get the number of members in a usesrc group).
312 312 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
313 313 * field is changing state i.e from NULL to non-NULL or vice-versa. For
314 314 * example, it is not necessary to take this lock in the initial portion
315 315 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
316 316 * operations are executed exclusively and that ensures that the "usesrc
317 317 * group state" cannot change. The "usesrc group state" change can happen
318 318 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
319 319 *
320 320 * Changing <ill-phyint>, <ipsq-xop> assocications:
321 321 *
322 322 * To change the <ill-phyint> association, the ill_g_lock must be held
323 323 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
324 324 * must be held.
325 325 *
326 326 * To change the <ipsq-xop> association, the ill_g_lock must be held as
327 327 * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
328 328 * This is only done when ills are added or removed from IPMP groups.
329 329 *
330 330 * To add or delete an ipif from the list of ipifs hanging off the ill,
331 331 * ill_g_lock (writer) and ill_lock must be held and the thread must be
332 332 * a writer on the associated ipsq.
333 333 *
334 334 * To add or delete an ill to the system, the ill_g_lock must be held as
335 335 * writer and the thread must be a writer on the associated ipsq.
336 336 *
337 337 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
338 338 * must be a writer on the associated ipsq.
339 339 *
340 340 * Lock hierarchy
341 341 *
342 342 * Some lock hierarchy scenarios are listed below.
343 343 *
344 344 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
345 345 * ill_g_lock -> ill_lock(s) -> phyint_lock
346 346 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
347 347 * ill_g_lock -> ip_addr_avail_lock
348 348 * conn_lock -> irb_lock -> ill_lock -> ire_lock
349 349 * ill_g_lock -> ip_g_nd_lock
350 350 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
351 351 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
352 352 * arl_lock -> ill_lock
353 353 * ips_ire_dep_lock -> irb_lock
354 354 *
355 355 * When more than 1 ill lock is needed to be held, all ill lock addresses
356 356 * are sorted on address and locked starting from highest addressed lock
357 357 * downward.
358 358 *
359 359 * Multicast scenarios
360 360 * ips_ill_g_lock -> ill_mcast_lock
361 361 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
362 362 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
363 363 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
364 364 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
365 365 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
366 366 *
367 367 * IPsec scenarios
368 368 *
369 369 * ipsa_lock -> ill_g_lock -> ill_lock
370 370 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
371 371 *
372 372 * Trusted Solaris scenarios
373 373 *
374 374 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
375 375 * igsa_lock -> gcdb_lock
376 376 * gcgrp_rwlock -> ire_lock
377 377 * gcgrp_rwlock -> gcdb_lock
378 378 *
379 379 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
380 380 *
381 381 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
382 382 * sq_lock -> conn_lock -> QLOCK(q)
383 383 * ill_lock -> ft_lock -> fe_lock
384 384 *
385 385 * Routing/forwarding table locking notes:
386 386 *
387 387 * Lock acquisition order: Radix tree lock, irb_lock.
388 388 * Requirements:
389 389 * i. Walker must not hold any locks during the walker callback.
390 390 * ii Walker must not see a truncated tree during the walk because of any node
391 391 * deletion.
392 392 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
393 393 * in many places in the code to walk the irb list. Thus even if all the
394 394 * ires in a bucket have been deleted, we still can't free the radix node
395 395 * until the ires have actually been inactive'd (freed).
396 396 *
397 397 * Tree traversal - Need to hold the global tree lock in read mode.
398 398 * Before dropping the global tree lock, need to either increment the ire_refcnt
399 399 * to ensure that the radix node can't be deleted.
400 400 *
401 401 * Tree add - Need to hold the global tree lock in write mode to add a
402 402 * radix node. To prevent the node from being deleted, increment the
403 403 * irb_refcnt, after the node is added to the tree. The ire itself is
404 404 * added later while holding the irb_lock, but not the tree lock.
405 405 *
406 406 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
407 407 * All associated ires must be inactive (i.e. freed), and irb_refcnt
408 408 * must be zero.
409 409 *
410 410 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
411 411 * global tree lock (read mode) for traversal.
412 412 *
413 413 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
414 414 * hence we will acquire irb_lock while holding ips_ire_dep_lock.
415 415 *
416 416 * IPsec notes :
417 417 *
418 418 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
419 419 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
420 420 * ip_xmit_attr_t has the
421 421 * information used by the IPsec code for applying the right level of
422 422 * protection. The information initialized by IP in the ip_xmit_attr_t
423 423 * is determined by the per-socket policy or global policy in the system.
424 424 * For inbound datagrams, the ip_recv_attr_t
425 425 * starts out with nothing in it. It gets filled
426 426 * with the right information if it goes through the AH/ESP code, which
427 427 * happens if the incoming packet is secure. The information initialized
428 428 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
429 429 * the policy requirements needed by per-socket policy or global policy
430 430 * is met or not.
431 431 *
432 432 * For fully connected sockets i.e dst, src [addr, port] is known,
433 433 * conn_policy_cached is set indicating that policy has been cached.
434 434 * conn_in_enforce_policy may or may not be set depending on whether
435 435 * there is a global policy match or per-socket policy match.
436 436 * Policy inheriting happpens in ip_policy_set once the destination is known.
437 437 * Once the right policy is set on the conn_t, policy cannot change for
438 438 * this socket. This makes life simpler for TCP (UDP ?) where
439 439 * re-transmissions go out with the same policy. For symmetry, policy
440 440 * is cached for fully connected UDP sockets also. Thus if policy is cached,
441 441 * it also implies that policy is latched i.e policy cannot change
442 442 * on these sockets. As we have the right policy on the conn, we don't
443 443 * have to lookup global policy for every outbound and inbound datagram
444 444 * and thus serving as an optimization. Note that a global policy change
445 445 * does not affect fully connected sockets if they have policy. If fully
446 446 * connected sockets did not have any policy associated with it, global
447 447 * policy change may affect them.
448 448 *
449 449 * IP Flow control notes:
450 450 * ---------------------
451 451 * Non-TCP streams are flow controlled by IP. The way this is accomplished
452 452 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
453 453 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
454 454 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
455 455 * functions.
456 456 *
457 457 * Per Tx ring udp flow control:
458 458 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
459 459 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
460 460 *
461 461 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
462 462 * To achieve best performance, outgoing traffic need to be fanned out among
463 463 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
464 464 * traffic out of the NIC and it takes a fanout hint. UDP connections pass
465 465 * the address of connp as fanout hint to mac_tx(). Under flow controlled
466 466 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
467 467 * cookie points to a specific Tx ring that is blocked. The cookie is used to
468 468 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
469 469 * point to drain_lists (idl_t's). These drain list will store the blocked UDP
470 470 * connp's. The drain list is not a single list but a configurable number of
471 471 * lists.
472 472 *
473 473 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
474 474 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
475 475 * which is equal to 128. This array in turn contains a pointer to idl_t[],
476 476 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
477 477 * list will point to the list of connp's that are flow controlled.
478 478 *
479 479 * --------------- ------- ------- -------
480 480 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
481 481 * | --------------- ------- ------- -------
482 482 * | --------------- ------- ------- -------
483 483 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
484 484 * ---------------- | --------------- ------- ------- -------
485 485 * |idl_tx_list[0]|->| --------------- ------- ------- -------
486 486 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
487 487 * | --------------- ------- ------- -------
488 488 * . . . . .
489 489 * | --------------- ------- ------- -------
490 490 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
491 491 * --------------- ------- ------- -------
492 492 * --------------- ------- ------- -------
493 493 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
494 494 * | --------------- ------- ------- -------
495 495 * | --------------- ------- ------- -------
496 496 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
497 497 * |idl_tx_list[1]|->| --------------- ------- ------- -------
498 498 * ---------------- | . . . .
499 499 * | --------------- ------- ------- -------
500 500 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
501 501 * --------------- ------- ------- -------
502 502 * .....
503 503 * ----------------
504 504 * |idl_tx_list[n]|-> ...
505 505 * ----------------
506 506 *
507 507 * When mac_tx() returns a cookie, the cookie is hashed into an index into
508 508 * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
509 509 * to insert the conn onto. conn_drain_insert() asserts flow control for the
510 510 * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
511 511 * Further, conn_blocked is set to indicate that the conn is blocked.
512 512 *
513 513 * GLDv3 calls ill_flow_enable() when flow control is relieved. The cookie
514 514 * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
515 515 * is again hashed to locate the appropriate idl_tx_list, which is then
516 516 * drained via conn_walk_drain(). conn_walk_drain() goes through each conn in
517 517 * the drain list and calls conn_drain_remove() to clear flow control (via
518 518 * calling su_txq_full() or clearing QFULL), and remove the conn from the
519 519 * drain list.
520 520 *
521 521 * Note that the drain list is not a single list but a (configurable) array of
522 522 * lists (8 elements by default). Synchronization between drain insertion and
523 523 * flow control wakeup is handled by using idl_txl->txl_lock, and only
524 524 * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
525 525 *
526 526 * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
527 527 * On the send side, if the packet cannot be sent down to the driver by IP
528 528 * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
529 529 * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
530 530 * the 0'th drain list. When ip_wsrv() runs on the ill_wq because flow
531 531 * control has been relieved, the blocked conns in the 0'th drain list are
532 532 * drained as in the non-STREAMS case.
533 533 *
534 534 * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
535 535 * is done when the conn is inserted into the drain list (conn_drain_insert())
536 536 * and cleared when the conn is removed from the it (conn_drain_remove()).
537 537 *
538 538 * IPQOS notes:
539 539 *
540 540 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
541 541 * and IPQoS modules. IPPF includes hooks in IP at different control points
542 542 * (callout positions) which direct packets to IPQoS modules for policy
543 543 * processing. Policies, if present, are global.
544 544 *
545 545 * The callout positions are located in the following paths:
546 546 * o local_in (packets destined for this host)
547 547 * o local_out (packets orginating from this host )
548 548 * o fwd_in (packets forwarded by this m/c - inbound)
549 549 * o fwd_out (packets forwarded by this m/c - outbound)
550 550 * Hooks at these callout points can be enabled/disabled using the ndd variable
551 551 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
552 552 * By default all the callout positions are enabled.
553 553 *
554 554 * Outbound (local_out)
555 555 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
556 556 *
557 557 * Inbound (local_in)
558 558 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
559 559 *
560 560 * Forwarding (in and out)
561 561 * Hooks are placed in ire_recv_forward_v4/v6.
562 562 *
563 563 * IP Policy Framework processing (IPPF processing)
564 564 * Policy processing for a packet is initiated by ip_process, which ascertains
565 565 * that the classifier (ipgpc) is loaded and configured, failing which the
566 566 * packet resumes normal processing in IP. If the clasifier is present, the
567 567 * packet is acted upon by one or more IPQoS modules (action instances), per
568 568 * filters configured in ipgpc and resumes normal IP processing thereafter.
569 569 * An action instance can drop a packet in course of its processing.
570 570 *
571 571 * Zones notes:
572 572 *
573 573 * The partitioning rules for networking are as follows:
574 574 * 1) Packets coming from a zone must have a source address belonging to that
575 575 * zone.
576 576 * 2) Packets coming from a zone can only be sent on a physical interface on
577 577 * which the zone has an IP address.
578 578 * 3) Between two zones on the same machine, packet delivery is only allowed if
579 579 * there's a matching route for the destination and zone in the forwarding
580 580 * table.
581 581 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
582 582 * different zones can bind to the same port with the wildcard address
583 583 * (INADDR_ANY).
584 584 *
585 585 * The granularity of interface partitioning is at the logical interface level.
586 586 * Therefore, every zone has its own IP addresses, and incoming packets can be
587 587 * attributed to a zone unambiguously. A logical interface is placed into a zone
588 588 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
589 589 * structure. Rule (1) is implemented by modifying the source address selection
590 590 * algorithm so that the list of eligible addresses is filtered based on the
591 591 * sending process zone.
592 592 *
593 593 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
594 594 * across all zones, depending on their type. Here is the break-up:
595 595 *
596 596 * IRE type Shared/exclusive
597 597 * -------- ----------------
598 598 * IRE_BROADCAST Exclusive
599 599 * IRE_DEFAULT (default routes) Shared (*)
600 600 * IRE_LOCAL Exclusive (x)
601 601 * IRE_LOOPBACK Exclusive
602 602 * IRE_PREFIX (net routes) Shared (*)
603 603 * IRE_IF_NORESOLVER (interface routes) Exclusive
604 604 * IRE_IF_RESOLVER (interface routes) Exclusive
605 605 * IRE_IF_CLONE (interface routes) Exclusive
606 606 * IRE_HOST (host routes) Shared (*)
607 607 *
608 608 * (*) A zone can only use a default or off-subnet route if the gateway is
609 609 * directly reachable from the zone, that is, if the gateway's address matches
610 610 * one of the zone's logical interfaces.
611 611 *
612 612 * (x) IRE_LOCAL are handled a bit differently.
613 613 * When ip_restrict_interzone_loopback is set (the default),
614 614 * ire_route_recursive restricts loopback using an IRE_LOCAL
615 615 * between zone to the case when L2 would have conceptually looped the packet
616 616 * back, i.e. the loopback which is required since neither Ethernet drivers
617 617 * nor Ethernet hardware loops them back. This is the case when the normal
618 618 * routes (ignoring IREs with different zoneids) would send out the packet on
619 619 * the same ill as the ill with which is IRE_LOCAL is associated.
620 620 *
621 621 * Multiple zones can share a common broadcast address; typically all zones
622 622 * share the 255.255.255.255 address. Incoming as well as locally originated
623 623 * broadcast packets must be dispatched to all the zones on the broadcast
624 624 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
625 625 * since some zones may not be on the 10.16.72/24 network. To handle this, each
626 626 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
627 627 * sent to every zone that has an IRE_BROADCAST entry for the destination
628 628 * address on the input ill, see ip_input_broadcast().
629 629 *
630 630 * Applications in different zones can join the same multicast group address.
631 631 * The same logic applies for multicast as for broadcast. ip_input_multicast
632 632 * dispatches packets to all zones that have members on the physical interface.
633 633 */
634 634
635 635 /*
636 636 * Squeue Fanout flags:
637 637 * 0: No fanout.
638 638 * 1: Fanout across all squeues
639 639 */
640 640 boolean_t ip_squeue_fanout = 0;
641 641
642 642 /*
643 643 * Maximum dups allowed per packet.
644 644 */
645 645 uint_t ip_max_frag_dups = 10;
646 646
647 647 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
648 648 cred_t *credp, boolean_t isv6);
649 649 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
650 650
651 651 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
652 652 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
653 653 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
654 654 ip_recv_attr_t *);
655 655 static void icmp_options_update(ipha_t *);
656 656 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *);
657 657 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
658 658 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
659 659 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
660 660 ip_recv_attr_t *);
661 661 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
662 662 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
663 663 ip_recv_attr_t *);
664 664
665 665 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
666 666 char *ip_dot_addr(ipaddr_t, char *);
667 667 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
668 668 int ip_close(queue_t *, int);
669 669 static char *ip_dot_saddr(uchar_t *, char *);
670 670 static void ip_lrput(queue_t *, mblk_t *);
671 671 ipaddr_t ip_net_mask(ipaddr_t);
672 672 char *ip_nv_lookup(nv_t *, int);
673 673 void ip_rput(queue_t *, mblk_t *);
674 674 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
675 675 void *dummy_arg);
676 676 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
677 677 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
678 678 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
679 679 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
680 680 ip_stack_t *, boolean_t);
681 681 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
682 682 boolean_t);
683 683 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
684 684 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
685 685 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
686 686 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
687 687 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
688 688 ip_stack_t *ipst, boolean_t);
689 689 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
690 690 ip_stack_t *ipst, boolean_t);
691 691 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
692 692 ip_stack_t *ipst);
693 693 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
694 694 ip_stack_t *ipst);
695 695 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
696 696 ip_stack_t *ipst);
697 697 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
698 698 ip_stack_t *ipst);
699 699 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
700 700 ip_stack_t *ipst);
701 701 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
702 702 ip_stack_t *ipst);
703 703 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
704 704 ip_stack_t *ipst);
705 705 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
706 706 ip_stack_t *ipst);
707 707 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
708 708 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
709 709 static int ip_snmp_get2_v4_media(ncec_t *, iproutedata_t *);
710 710 static int ip_snmp_get2_v6_media(ncec_t *, iproutedata_t *);
711 711 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
712 712
713 713 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
714 714 mblk_t *);
715 715
716 716 static void conn_drain_init(ip_stack_t *);
717 717 static void conn_drain_fini(ip_stack_t *);
718 718 static void conn_drain(conn_t *connp, boolean_t closing);
719 719
720 720 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
721 721 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
722 722
723 723 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
724 724 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
725 725 static void ip_stack_fini(netstackid_t stackid, void *arg);
726 726
727 727 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
728 728 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
729 729 ire_t *, conn_t *, boolean_t, const in6_addr_t *, mcast_record_t,
730 730 const in6_addr_t *);
731 731
732 732 static int ip_squeue_switch(int);
733 733
734 734 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
735 735 static void ip_kstat_fini(netstackid_t, kstat_t *);
736 736 static int ip_kstat_update(kstat_t *kp, int rw);
737 737 static void *icmp_kstat_init(netstackid_t);
738 738 static void icmp_kstat_fini(netstackid_t, kstat_t *);
739 739 static int icmp_kstat_update(kstat_t *kp, int rw);
740 740 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
741 741 static void ip_kstat2_fini(netstackid_t, kstat_t *);
742 742
743 743 static void ipobs_init(ip_stack_t *);
744 744 static void ipobs_fini(ip_stack_t *);
745 745
746 746 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
747 747
748 748 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
749 749
750 750 static long ip_rput_pullups;
751 751 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
752 752
753 753 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
754 754 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
755 755
756 756 int ip_debug;
757 757
758 758 /*
759 759 * Multirouting/CGTP stuff
760 760 */
761 761 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
762 762
763 763 /*
764 764 * IP tunables related declarations. Definitions are in ip_tunables.c
765 765 */
766 766 extern mod_prop_info_t ip_propinfo_tbl[];
767 767 extern int ip_propinfo_count;
768 768
769 769 /*
770 770 * Table of IP ioctls encoding the various properties of the ioctl and
771 771 * indexed based on the last byte of the ioctl command. Occasionally there
772 772 * is a clash, and there is more than 1 ioctl with the same last byte.
773 773 * In such a case 1 ioctl is encoded in the ndx table and the remaining
774 774 * ioctls are encoded in the misc table. An entry in the ndx table is
775 775 * retrieved by indexing on the last byte of the ioctl command and comparing
776 776 * the ioctl command with the value in the ndx table. In the event of a
777 777 * mismatch the misc table is then searched sequentially for the desired
778 778 * ioctl command.
779 779 *
780 780 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
781 781 */
782 782 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
783 783 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
784 784 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
785 785 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
786 786 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 787 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 788 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 789 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 790 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 791 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 792 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
793 793
794 794 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
795 795 MISC_CMD, ip_siocaddrt, NULL },
796 796 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
797 797 MISC_CMD, ip_siocdelrt, NULL },
798 798
799 799 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
800 800 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
801 801 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
802 802 IF_CMD, ip_sioctl_get_addr, NULL },
803 803
804 804 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
805 805 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
806 806 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
807 807 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
808 808
809 809 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
810 810 IPI_PRIV | IPI_WR,
811 811 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
812 812 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
813 813 IPI_MODOK | IPI_GET_CMD,
814 814 IF_CMD, ip_sioctl_get_flags, NULL },
815 815
816 816 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
817 817 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
818 818
819 819 /* copyin size cannot be coded for SIOCGIFCONF */
820 820 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
821 821 MISC_CMD, ip_sioctl_get_ifconf, NULL },
822 822
823 823 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
824 824 IF_CMD, ip_sioctl_mtu, NULL },
825 825 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
826 826 IF_CMD, ip_sioctl_get_mtu, NULL },
827 827 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
828 828 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
829 829 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
830 830 IF_CMD, ip_sioctl_brdaddr, NULL },
831 831 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
832 832 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
833 833 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
834 834 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
835 835 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
836 836 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
837 837 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
838 838 IF_CMD, ip_sioctl_metric, NULL },
839 839 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
840 840
841 841 /* See 166-168 below for extended SIOC*XARP ioctls */
842 842 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
843 843 ARP_CMD, ip_sioctl_arp, NULL },
844 844 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
845 845 ARP_CMD, ip_sioctl_arp, NULL },
846 846 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
847 847 ARP_CMD, ip_sioctl_arp, NULL },
848 848
849 849 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
850 850 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
851 851 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
852 852 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
853 853 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
854 854 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
855 855 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
856 856 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
857 857 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
858 858 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
859 859 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
860 860 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
861 861 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 862 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 863 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 864 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 865 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 866 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 867 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 868 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 869 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
870 870
871 871 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
872 872 MISC_CMD, if_unitsel, if_unitsel_restart },
873 873
874 874 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
875 875 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
876 876 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
877 877 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
878 878 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
879 879 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
880 880 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
881 881 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
882 882 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
883 883 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
884 884 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
885 885 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
886 886 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
887 887 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
888 888 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
889 889 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
890 890 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
891 891 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
892 892
893 893 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
894 894 IPI_PRIV | IPI_WR | IPI_MODOK,
895 895 IF_CMD, ip_sioctl_sifname, NULL },
896 896
897 897 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
898 898 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
899 899 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
900 900 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
901 901 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
902 902 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
903 903 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
904 904 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
905 905 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
906 906 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
907 907 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
908 908 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
909 909 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
910 910
911 911 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
912 912 MISC_CMD, ip_sioctl_get_ifnum, NULL },
913 913 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
914 914 IF_CMD, ip_sioctl_get_muxid, NULL },
915 915 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
916 916 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
917 917
918 918 /* Both if and lif variants share same func */
919 919 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
920 920 IF_CMD, ip_sioctl_get_lifindex, NULL },
921 921 /* Both if and lif variants share same func */
922 922 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
923 923 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
924 924
925 925 /* copyin size cannot be coded for SIOCGIFCONF */
926 926 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
927 927 MISC_CMD, ip_sioctl_get_ifconf, NULL },
928 928 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
929 929 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
930 930 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
931 931 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
932 932 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
933 933 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
934 934 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
935 935 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
936 936 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
937 937 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
938 938 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
939 939 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
940 940 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
941 941 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
942 942 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
943 943 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 944 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
945 945
946 946 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
947 947 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
948 948 ip_sioctl_removeif_restart },
949 949 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
950 950 IPI_GET_CMD | IPI_PRIV | IPI_WR,
951 951 LIF_CMD, ip_sioctl_addif, NULL },
952 952 #define SIOCLIFADDR_NDX 112
953 953 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
954 954 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
955 955 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
956 956 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
957 957 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
958 958 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
959 959 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
960 960 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
961 961 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
962 962 IPI_PRIV | IPI_WR,
963 963 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
964 964 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
965 965 IPI_GET_CMD | IPI_MODOK,
966 966 LIF_CMD, ip_sioctl_get_flags, NULL },
967 967
968 968 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
969 969 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
970 970
971 971 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
972 972 ip_sioctl_get_lifconf, NULL },
973 973 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
974 974 LIF_CMD, ip_sioctl_mtu, NULL },
975 975 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
976 976 LIF_CMD, ip_sioctl_get_mtu, NULL },
977 977 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
978 978 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
979 979 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
980 980 LIF_CMD, ip_sioctl_brdaddr, NULL },
981 981 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
982 982 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
983 983 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
984 984 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
985 985 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
986 986 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
987 987 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
988 988 LIF_CMD, ip_sioctl_metric, NULL },
989 989 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
990 990 IPI_PRIV | IPI_WR | IPI_MODOK,
991 991 LIF_CMD, ip_sioctl_slifname,
992 992 ip_sioctl_slifname_restart },
993 993
994 994 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
995 995 MISC_CMD, ip_sioctl_get_lifnum, NULL },
996 996 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
997 997 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
998 998 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
999 999 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
1000 1000 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1001 1001 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1002 1002 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1003 1003 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1004 1004 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1005 1005 LIF_CMD, ip_sioctl_token, NULL },
1006 1006 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1007 1007 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1008 1008 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1009 1009 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1010 1010 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1011 1011 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1012 1012 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1013 1013 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1014 1014
1015 1015 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1016 1016 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1017 1017 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1018 1018 LIF_CMD, ip_siocdelndp_v6, NULL },
1019 1019 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1020 1020 LIF_CMD, ip_siocqueryndp_v6, NULL },
1021 1021 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1022 1022 LIF_CMD, ip_siocsetndp_v6, NULL },
1023 1023 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1024 1024 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1025 1025 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1026 1026 MISC_CMD, ip_sioctl_tonlink, NULL },
1027 1027 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1028 1028 MISC_CMD, ip_sioctl_tmysite, NULL },
1029 1029 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1030 1030 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1031 1031
1032 1032 /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
1033 1033 /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034 1034 /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1035 1035 /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 1036 /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 1037
1038 1038 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1039 1039
1040 1040 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1041 1041 LIF_CMD, ip_sioctl_get_binding, NULL },
1042 1042 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1043 1043 IPI_PRIV | IPI_WR,
1044 1044 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1045 1045 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1046 1046 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1047 1047 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1048 1048 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1049 1049
1050 1050 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1051 1051 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1052 1052 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 1053 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 1054
1055 1055 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1056 1056
1057 1057 /* These are handled in ip_sioctl_copyin_setup itself */
1058 1058 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1059 1059 MISC_CMD, NULL, NULL },
1060 1060 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1061 1061 MISC_CMD, NULL, NULL },
1062 1062 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1063 1063
1064 1064 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1065 1065 ip_sioctl_get_lifconf, NULL },
1066 1066
1067 1067 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1068 1068 XARP_CMD, ip_sioctl_arp, NULL },
1069 1069 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1070 1070 XARP_CMD, ip_sioctl_arp, NULL },
1071 1071 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1072 1072 XARP_CMD, ip_sioctl_arp, NULL },
1073 1073
1074 1074 /* SIOCPOPSOCKFS is not handled by IP */
1075 1075 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1076 1076
1077 1077 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1078 1078 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1079 1079 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1080 1080 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1081 1081 ip_sioctl_slifzone_restart },
1082 1082 /* 172-174 are SCTP ioctls and not handled by IP */
1083 1083 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1084 1084 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1085 1085 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1086 1086 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1087 1087 IPI_GET_CMD, LIF_CMD,
1088 1088 ip_sioctl_get_lifusesrc, 0 },
1089 1089 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1090 1090 IPI_PRIV | IPI_WR,
1091 1091 LIF_CMD, ip_sioctl_slifusesrc,
1092 1092 NULL },
1093 1093 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1094 1094 ip_sioctl_get_lifsrcof, NULL },
1095 1095 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1096 1096 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1097 1097 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1098 1098 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1099 1099 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1100 1100 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1101 1101 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1102 1102 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1103 1103 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1104 1104 /* SIOCSENABLESDP is handled by SDP */
1105 1105 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1106 1106 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1107 1107 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1108 1108 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1109 1109 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1110 1110 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1111 1111 ip_sioctl_ilb_cmd, NULL },
1112 1112 /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1113 1113 /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1114 1114 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1115 1115 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1116 1116 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1117 1117 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1118 1118 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1119 1119 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1120 1120 };
1121 1121
1122 1122 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1123 1123
1124 1124 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1125 1125 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1126 1126 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1127 1127 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1128 1128 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1129 1129 { ND_GET, 0, 0, 0, NULL, NULL },
1130 1130 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1131 1131 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1132 1132 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1133 1133 MISC_CMD, mrt_ioctl},
1134 1134 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1135 1135 MISC_CMD, mrt_ioctl},
1136 1136 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1137 1137 MISC_CMD, mrt_ioctl}
1138 1138 };
1139 1139
1140 1140 int ip_misc_ioctl_count =
1141 1141 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1142 1142
1143 1143 int conn_drain_nthreads; /* Number of drainers reqd. */
1144 1144 /* Settable in /etc/system */
1145 1145 /* Defined in ip_ire.c */
1146 1146 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1147 1147 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1148 1148 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1149 1149
1150 1150 static nv_t ire_nv_arr[] = {
1151 1151 { IRE_BROADCAST, "BROADCAST" },
1152 1152 { IRE_LOCAL, "LOCAL" },
1153 1153 { IRE_LOOPBACK, "LOOPBACK" },
1154 1154 { IRE_DEFAULT, "DEFAULT" },
1155 1155 { IRE_PREFIX, "PREFIX" },
1156 1156 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1157 1157 { IRE_IF_RESOLVER, "IF_RESOLV" },
1158 1158 { IRE_IF_CLONE, "IF_CLONE" },
1159 1159 { IRE_HOST, "HOST" },
1160 1160 { IRE_MULTICAST, "MULTICAST" },
1161 1161 { IRE_NOROUTE, "NOROUTE" },
1162 1162 { 0 }
1163 1163 };
1164 1164
1165 1165 nv_t *ire_nv_tbl = ire_nv_arr;
1166 1166
1167 1167 /* Simple ICMP IP Header Template */
1168 1168 static ipha_t icmp_ipha = {
1169 1169 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1170 1170 };
1171 1171
1172 1172 struct module_info ip_mod_info = {
1173 1173 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1174 1174 IP_MOD_LOWAT
1175 1175 };
1176 1176
1177 1177 /*
1178 1178 * Duplicate static symbols within a module confuses mdb; so we avoid the
1179 1179 * problem by making the symbols here distinct from those in udp.c.
1180 1180 */
1181 1181
1182 1182 /*
1183 1183 * Entry points for IP as a device and as a module.
1184 1184 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1185 1185 */
1186 1186 static struct qinit iprinitv4 = {
1187 1187 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL,
1188 1188 &ip_mod_info
1189 1189 };
1190 1190
1191 1191 struct qinit iprinitv6 = {
1192 1192 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL,
1193 1193 &ip_mod_info
1194 1194 };
1195 1195
1196 1196 static struct qinit ipwinit = {
1197 1197 (pfi_t)ip_wput_nondata, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1198 1198 &ip_mod_info
1199 1199 };
1200 1200
1201 1201 static struct qinit iplrinit = {
1202 1202 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL,
1203 1203 &ip_mod_info
1204 1204 };
1205 1205
1206 1206 static struct qinit iplwinit = {
1207 1207 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL,
1208 1208 &ip_mod_info
1209 1209 };
1210 1210
1211 1211 /* For AF_INET aka /dev/ip */
1212 1212 struct streamtab ipinfov4 = {
1213 1213 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1214 1214 };
1215 1215
1216 1216 /* For AF_INET6 aka /dev/ip6 */
1217 1217 struct streamtab ipinfov6 = {
1218 1218 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1219 1219 };
1220 1220
1221 1221 #ifdef DEBUG
1222 1222 boolean_t skip_sctp_cksum = B_FALSE;
1223 1223 #endif
1224 1224
1225 1225 /*
1226 1226 * Generate an ICMP fragmentation needed message.
1227 1227 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1228 1228 * constructed by the caller.
1229 1229 */
1230 1230 void
1231 1231 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1232 1232 {
1233 1233 icmph_t icmph;
1234 1234 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1235 1235
1236 1236 mp = icmp_pkt_err_ok(mp, ira);
1237 1237 if (mp == NULL)
1238 1238 return;
1239 1239
1240 1240 bzero(&icmph, sizeof (icmph_t));
1241 1241 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1242 1242 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1243 1243 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1244 1244 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1245 1245 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1246 1246
1247 1247 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1248 1248 }
1249 1249
1250 1250 /*
1251 1251 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1252 1252 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1253 1253 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1254 1254 * Likewise, if the ICMP error is misformed (too short, etc), then it
1255 1255 * returns NULL. The caller uses this to determine whether or not to send
1256 1256 * to raw sockets.
1257 1257 *
1258 1258 * All error messages are passed to the matching transport stream.
1259 1259 *
1260 1260 * The following cases are handled by icmp_inbound:
1261 1261 * 1) It needs to send a reply back and possibly delivering it
1262 1262 * to the "interested" upper clients.
1263 1263 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1264 1264 * 3) It needs to change some values in IP only.
1265 1265 * 4) It needs to change some values in IP and upper layers e.g TCP
1266 1266 * by delivering an error to the upper layers.
1267 1267 *
1268 1268 * We handle the above three cases in the context of IPsec in the
1269 1269 * following way :
1270 1270 *
1271 1271 * 1) Send the reply back in the same way as the request came in.
1272 1272 * If it came in encrypted, it goes out encrypted. If it came in
1273 1273 * clear, it goes out in clear. Thus, this will prevent chosen
1274 1274 * plain text attack.
1275 1275 * 2) The client may or may not expect things to come in secure.
1276 1276 * If it comes in secure, the policy constraints are checked
1277 1277 * before delivering it to the upper layers. If it comes in
1278 1278 * clear, ipsec_inbound_accept_clear will decide whether to
1279 1279 * accept this in clear or not. In both the cases, if the returned
1280 1280 * message (IP header + 8 bytes) that caused the icmp message has
1281 1281 * AH/ESP headers, it is sent up to AH/ESP for validation before
1282 1282 * sending up. If there are only 8 bytes of returned message, then
1283 1283 * upper client will not be notified.
1284 1284 * 3) Check with global policy to see whether it matches the constaints.
1285 1285 * But this will be done only if icmp_accept_messages_in_clear is
1286 1286 * zero.
1287 1287 * 4) If we need to change both in IP and ULP, then the decision taken
1288 1288 * while affecting the values in IP and while delivering up to TCP
1289 1289 * should be the same.
1290 1290 *
1291 1291 * There are two cases.
1292 1292 *
1293 1293 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1294 1294 * failed), we will not deliver it to the ULP, even though they
1295 1295 * are *willing* to accept in *clear*. This is fine as our global
1296 1296 * disposition to icmp messages asks us reject the datagram.
1297 1297 *
1298 1298 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1299 1299 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1300 1300 * to deliver it to ULP (policy failed), it can lead to
1301 1301 * consistency problems. The cases known at this time are
1302 1302 * ICMP_DESTINATION_UNREACHABLE messages with following code
1303 1303 * values :
1304 1304 *
1305 1305 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1306 1306 * and Upper layer rejects. Then the communication will
1307 1307 * come to a stop. This is solved by making similar decisions
1308 1308 * at both levels. Currently, when we are unable to deliver
1309 1309 * to the Upper Layer (due to policy failures) while IP has
1310 1310 * adjusted dce_pmtu, the next outbound datagram would
1311 1311 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1312 1312 * will be with the right level of protection. Thus the right
1313 1313 * value will be communicated even if we are not able to
1314 1314 * communicate when we get from the wire initially. But this
1315 1315 * assumes there would be at least one outbound datagram after
1316 1316 * IP has adjusted its dce_pmtu value. To make things
1317 1317 * simpler, we accept in clear after the validation of
1318 1318 * AH/ESP headers.
1319 1319 *
1320 1320 * - Other ICMP ERRORS : We may not be able to deliver it to the
1321 1321 * upper layer depending on the level of protection the upper
1322 1322 * layer expects and the disposition in ipsec_inbound_accept_clear().
1323 1323 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1324 1324 * should be accepted in clear when the Upper layer expects secure.
1325 1325 * Thus the communication may get aborted by some bad ICMP
1326 1326 * packets.
1327 1327 */
1328 1328 mblk_t *
1329 1329 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1330 1330 {
1331 1331 icmph_t *icmph;
1332 1332 ipha_t *ipha; /* Outer header */
1333 1333 int ip_hdr_length; /* Outer header length */
1334 1334 boolean_t interested;
1335 1335 ipif_t *ipif;
1336 1336 uint32_t ts;
1337 1337 uint32_t *tsp;
1338 1338 timestruc_t now;
1339 1339 ill_t *ill = ira->ira_ill;
1340 1340 ip_stack_t *ipst = ill->ill_ipst;
1341 1341 zoneid_t zoneid = ira->ira_zoneid;
1342 1342 int len_needed;
1343 1343 mblk_t *mp_ret = NULL;
1344 1344
1345 1345 ipha = (ipha_t *)mp->b_rptr;
1346 1346
1347 1347 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1348 1348
1349 1349 ip_hdr_length = ira->ira_ip_hdr_length;
1350 1350 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1351 1351 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1352 1352 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1353 1353 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1354 1354 freemsg(mp);
1355 1355 return (NULL);
1356 1356 }
1357 1357 /* Last chance to get real. */
1358 1358 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1359 1359 if (ipha == NULL) {
1360 1360 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1361 1361 freemsg(mp);
1362 1362 return (NULL);
1363 1363 }
1364 1364 }
1365 1365
1366 1366 /* The IP header will always be a multiple of four bytes */
1367 1367 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1368 1368 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1369 1369 icmph->icmph_code));
1370 1370
1371 1371 /*
1372 1372 * We will set "interested" to "true" if we should pass a copy to
1373 1373 * the transport or if we handle the packet locally.
1374 1374 */
1375 1375 interested = B_FALSE;
1376 1376 switch (icmph->icmph_type) {
1377 1377 case ICMP_ECHO_REPLY:
1378 1378 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1379 1379 break;
1380 1380 case ICMP_DEST_UNREACHABLE:
1381 1381 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1382 1382 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1383 1383 interested = B_TRUE; /* Pass up to transport */
1384 1384 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1385 1385 break;
1386 1386 case ICMP_SOURCE_QUENCH:
1387 1387 interested = B_TRUE; /* Pass up to transport */
1388 1388 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1389 1389 break;
1390 1390 case ICMP_REDIRECT:
1391 1391 if (!ipst->ips_ip_ignore_redirect)
1392 1392 interested = B_TRUE;
1393 1393 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1394 1394 break;
1395 1395 case ICMP_ECHO_REQUEST:
1396 1396 /*
1397 1397 * Whether to respond to echo requests that come in as IP
1398 1398 * broadcasts or as IP multicast is subject to debate
1399 1399 * (what isn't?). We aim to please, you pick it.
1400 1400 * Default is do it.
1401 1401 */
1402 1402 if (ira->ira_flags & IRAF_MULTICAST) {
1403 1403 /* multicast: respond based on tunable */
1404 1404 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1405 1405 } else if (ira->ira_flags & IRAF_BROADCAST) {
1406 1406 /* broadcast: respond based on tunable */
1407 1407 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1408 1408 } else {
1409 1409 /* unicast: always respond */
1410 1410 interested = B_TRUE;
1411 1411 }
1412 1412 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1413 1413 if (!interested) {
1414 1414 /* We never pass these to RAW sockets */
1415 1415 freemsg(mp);
1416 1416 return (NULL);
1417 1417 }
1418 1418
1419 1419 /* Check db_ref to make sure we can modify the packet. */
1420 1420 if (mp->b_datap->db_ref > 1) {
1421 1421 mblk_t *mp1;
1422 1422
1423 1423 mp1 = copymsg(mp);
1424 1424 freemsg(mp);
1425 1425 if (!mp1) {
1426 1426 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1427 1427 return (NULL);
1428 1428 }
1429 1429 mp = mp1;
1430 1430 ipha = (ipha_t *)mp->b_rptr;
1431 1431 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1432 1432 }
1433 1433 icmph->icmph_type = ICMP_ECHO_REPLY;
1434 1434 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1435 1435 icmp_send_reply_v4(mp, ipha, icmph, ira);
1436 1436 return (NULL);
1437 1437
1438 1438 case ICMP_ROUTER_ADVERTISEMENT:
1439 1439 case ICMP_ROUTER_SOLICITATION:
1440 1440 break;
1441 1441 case ICMP_TIME_EXCEEDED:
1442 1442 interested = B_TRUE; /* Pass up to transport */
1443 1443 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1444 1444 break;
1445 1445 case ICMP_PARAM_PROBLEM:
1446 1446 interested = B_TRUE; /* Pass up to transport */
1447 1447 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1448 1448 break;
1449 1449 case ICMP_TIME_STAMP_REQUEST:
1450 1450 /* Response to Time Stamp Requests is local policy. */
1451 1451 if (ipst->ips_ip_g_resp_to_timestamp) {
1452 1452 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1453 1453 interested =
1454 1454 ipst->ips_ip_g_resp_to_timestamp_bcast;
1455 1455 else
1456 1456 interested = B_TRUE;
1457 1457 }
1458 1458 if (!interested) {
1459 1459 /* We never pass these to RAW sockets */
1460 1460 freemsg(mp);
1461 1461 return (NULL);
1462 1462 }
1463 1463
1464 1464 /* Make sure we have enough of the packet */
1465 1465 len_needed = ip_hdr_length + ICMPH_SIZE +
1466 1466 3 * sizeof (uint32_t);
1467 1467
1468 1468 if (mp->b_wptr - mp->b_rptr < len_needed) {
1469 1469 ipha = ip_pullup(mp, len_needed, ira);
1470 1470 if (ipha == NULL) {
1471 1471 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1472 1472 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1473 1473 mp, ill);
1474 1474 freemsg(mp);
1475 1475 return (NULL);
1476 1476 }
1477 1477 /* Refresh following the pullup. */
1478 1478 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1479 1479 }
1480 1480 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1481 1481 /* Check db_ref to make sure we can modify the packet. */
1482 1482 if (mp->b_datap->db_ref > 1) {
1483 1483 mblk_t *mp1;
1484 1484
1485 1485 mp1 = copymsg(mp);
1486 1486 freemsg(mp);
1487 1487 if (!mp1) {
1488 1488 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1489 1489 return (NULL);
1490 1490 }
1491 1491 mp = mp1;
1492 1492 ipha = (ipha_t *)mp->b_rptr;
1493 1493 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1494 1494 }
1495 1495 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1496 1496 tsp = (uint32_t *)&icmph[1];
1497 1497 tsp++; /* Skip past 'originate time' */
1498 1498 /* Compute # of milliseconds since midnight */
1499 1499 gethrestime(&now);
1500 1500 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1501 1501 NSEC2MSEC(now.tv_nsec);
1502 1502 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1503 1503 *tsp++ = htonl(ts); /* Lay in 'send time' */
1504 1504 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1505 1505 icmp_send_reply_v4(mp, ipha, icmph, ira);
1506 1506 return (NULL);
1507 1507
1508 1508 case ICMP_TIME_STAMP_REPLY:
1509 1509 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1510 1510 break;
1511 1511 case ICMP_INFO_REQUEST:
1512 1512 /* Per RFC 1122 3.2.2.7, ignore this. */
1513 1513 case ICMP_INFO_REPLY:
1514 1514 break;
1515 1515 case ICMP_ADDRESS_MASK_REQUEST:
1516 1516 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1517 1517 interested =
1518 1518 ipst->ips_ip_respond_to_address_mask_broadcast;
1519 1519 } else {
1520 1520 interested = B_TRUE;
1521 1521 }
1522 1522 if (!interested) {
1523 1523 /* We never pass these to RAW sockets */
1524 1524 freemsg(mp);
1525 1525 return (NULL);
1526 1526 }
1527 1527 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1528 1528 if (mp->b_wptr - mp->b_rptr < len_needed) {
1529 1529 ipha = ip_pullup(mp, len_needed, ira);
1530 1530 if (ipha == NULL) {
1531 1531 BUMP_MIB(ill->ill_ip_mib,
1532 1532 ipIfStatsInTruncatedPkts);
1533 1533 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1534 1534 ill);
1535 1535 freemsg(mp);
1536 1536 return (NULL);
1537 1537 }
1538 1538 /* Refresh following the pullup. */
1539 1539 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1540 1540 }
1541 1541 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1542 1542 /* Check db_ref to make sure we can modify the packet. */
1543 1543 if (mp->b_datap->db_ref > 1) {
1544 1544 mblk_t *mp1;
1545 1545
1546 1546 mp1 = copymsg(mp);
1547 1547 freemsg(mp);
1548 1548 if (!mp1) {
1549 1549 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1550 1550 return (NULL);
1551 1551 }
1552 1552 mp = mp1;
1553 1553 ipha = (ipha_t *)mp->b_rptr;
1554 1554 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1555 1555 }
1556 1556 /*
1557 1557 * Need the ipif with the mask be the same as the source
1558 1558 * address of the mask reply. For unicast we have a specific
1559 1559 * ipif. For multicast/broadcast we only handle onlink
1560 1560 * senders, and use the source address to pick an ipif.
1561 1561 */
1562 1562 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1563 1563 if (ipif == NULL) {
1564 1564 /* Broadcast or multicast */
1565 1565 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1566 1566 if (ipif == NULL) {
1567 1567 freemsg(mp);
1568 1568 return (NULL);
1569 1569 }
1570 1570 }
1571 1571 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1572 1572 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1573 1573 ipif_refrele(ipif);
1574 1574 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1575 1575 icmp_send_reply_v4(mp, ipha, icmph, ira);
1576 1576 return (NULL);
1577 1577
1578 1578 case ICMP_ADDRESS_MASK_REPLY:
1579 1579 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1580 1580 break;
1581 1581 default:
1582 1582 interested = B_TRUE; /* Pass up to transport */
1583 1583 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1584 1584 break;
1585 1585 }
1586 1586 /*
1587 1587 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1588 1588 * if there isn't one.
1589 1589 */
1590 1590 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1591 1591 /* If there is an ICMP client and we want one too, copy it. */
1592 1592
1593 1593 if (!interested) {
1594 1594 /* Caller will deliver to RAW sockets */
1595 1595 return (mp);
1596 1596 }
1597 1597 mp_ret = copymsg(mp);
1598 1598 if (mp_ret == NULL) {
1599 1599 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1600 1600 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1601 1601 }
1602 1602 } else if (!interested) {
1603 1603 /* Neither we nor raw sockets are interested. Drop packet now */
1604 1604 freemsg(mp);
1605 1605 return (NULL);
1606 1606 }
1607 1607
1608 1608 /*
1609 1609 * ICMP error or redirect packet. Make sure we have enough of
1610 1610 * the header and that db_ref == 1 since we might end up modifying
1611 1611 * the packet.
1612 1612 */
1613 1613 if (mp->b_cont != NULL) {
1614 1614 if (ip_pullup(mp, -1, ira) == NULL) {
1615 1615 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1616 1616 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1617 1617 mp, ill);
1618 1618 freemsg(mp);
1619 1619 return (mp_ret);
1620 1620 }
1621 1621 }
1622 1622
1623 1623 if (mp->b_datap->db_ref > 1) {
1624 1624 mblk_t *mp1;
1625 1625
1626 1626 mp1 = copymsg(mp);
1627 1627 if (mp1 == NULL) {
1628 1628 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1629 1629 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1630 1630 freemsg(mp);
1631 1631 return (mp_ret);
1632 1632 }
1633 1633 freemsg(mp);
1634 1634 mp = mp1;
1635 1635 }
1636 1636
1637 1637 /*
1638 1638 * In case mp has changed, verify the message before any further
1639 1639 * processes.
1640 1640 */
1641 1641 ipha = (ipha_t *)mp->b_rptr;
1642 1642 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1643 1643 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1644 1644 freemsg(mp);
1645 1645 return (mp_ret);
1646 1646 }
1647 1647
1648 1648 switch (icmph->icmph_type) {
1649 1649 case ICMP_REDIRECT:
1650 1650 icmp_redirect_v4(mp, ipha, icmph, ira);
1651 1651 break;
1652 1652 case ICMP_DEST_UNREACHABLE:
1653 1653 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1654 1654 /* Update DCE and adjust MTU is icmp header if needed */
1655 1655 icmp_inbound_too_big_v4(icmph, ira);
1656 1656 }
1657 1657 /* FALLTHRU */
1658 1658 default:
1659 1659 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1660 1660 break;
1661 1661 }
1662 1662 return (mp_ret);
1663 1663 }
1664 1664
1665 1665 /*
1666 1666 * Send an ICMP echo, timestamp or address mask reply.
1667 1667 * The caller has already updated the payload part of the packet.
1668 1668 * We handle the ICMP checksum, IP source address selection and feed
1669 1669 * the packet into ip_output_simple.
1670 1670 */
1671 1671 static void
1672 1672 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1673 1673 ip_recv_attr_t *ira)
1674 1674 {
1675 1675 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1676 1676 ill_t *ill = ira->ira_ill;
1677 1677 ip_stack_t *ipst = ill->ill_ipst;
1678 1678 ip_xmit_attr_t ixas;
1679 1679
1680 1680 /* Send out an ICMP packet */
1681 1681 icmph->icmph_checksum = 0;
1682 1682 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1683 1683 /* Reset time to live. */
1684 1684 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1685 1685 {
1686 1686 /* Swap source and destination addresses */
1687 1687 ipaddr_t tmp;
1688 1688
1689 1689 tmp = ipha->ipha_src;
1690 1690 ipha->ipha_src = ipha->ipha_dst;
1691 1691 ipha->ipha_dst = tmp;
1692 1692 }
1693 1693 ipha->ipha_ident = 0;
1694 1694 if (!IS_SIMPLE_IPH(ipha))
1695 1695 icmp_options_update(ipha);
1696 1696
1697 1697 bzero(&ixas, sizeof (ixas));
1698 1698 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1699 1699 ixas.ixa_zoneid = ira->ira_zoneid;
1700 1700 ixas.ixa_cred = kcred;
1701 1701 ixas.ixa_cpid = NOPID;
1702 1702 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
1703 1703 ixas.ixa_ifindex = 0;
1704 1704 ixas.ixa_ipst = ipst;
1705 1705 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1706 1706
1707 1707 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1708 1708 /*
1709 1709 * This packet should go out the same way as it
1710 1710 * came in i.e in clear, independent of the IPsec policy
1711 1711 * for transmitting packets.
1712 1712 */
1713 1713 ixas.ixa_flags |= IXAF_NO_IPSEC;
1714 1714 } else {
1715 1715 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1716 1716 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1717 1717 /* Note: mp already consumed and ip_drop_packet done */
1718 1718 return;
1719 1719 }
1720 1720 }
1721 1721 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1722 1722 /*
1723 1723 * Not one or our addresses (IRE_LOCALs), thus we let
1724 1724 * ip_output_simple pick the source.
1725 1725 */
1726 1726 ipha->ipha_src = INADDR_ANY;
1727 1727 ixas.ixa_flags |= IXAF_SET_SOURCE;
1728 1728 }
1729 1729 /* Should we send with DF and use dce_pmtu? */
1730 1730 if (ipst->ips_ipv4_icmp_return_pmtu) {
1731 1731 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1732 1732 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1733 1733 }
1734 1734
1735 1735 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1736 1736
1737 1737 (void) ip_output_simple(mp, &ixas);
1738 1738 ixa_cleanup(&ixas);
1739 1739 }
1740 1740
1741 1741 /*
1742 1742 * Verify the ICMP messages for either for ICMP error or redirect packet.
1743 1743 * The caller should have fully pulled up the message. If it's a redirect
1744 1744 * packet, only basic checks on IP header will be done; otherwise, verify
1745 1745 * the packet by looking at the included ULP header.
1746 1746 *
1747 1747 * Called before icmp_inbound_error_fanout_v4 is called.
1748 1748 */
1749 1749 static boolean_t
1750 1750 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1751 1751 {
1752 1752 ill_t *ill = ira->ira_ill;
1753 1753 int hdr_length;
1754 1754 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1755 1755 conn_t *connp;
1756 1756 ipha_t *ipha; /* Inner IP header */
1757 1757
1758 1758 ipha = (ipha_t *)&icmph[1];
1759 1759 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1760 1760 goto truncated;
1761 1761
1762 1762 hdr_length = IPH_HDR_LENGTH(ipha);
1763 1763
1764 1764 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1765 1765 goto discard_pkt;
1766 1766
1767 1767 if (hdr_length < sizeof (ipha_t))
1768 1768 goto truncated;
1769 1769
1770 1770 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1771 1771 goto truncated;
1772 1772
1773 1773 /*
1774 1774 * Stop here for ICMP_REDIRECT.
1775 1775 */
1776 1776 if (icmph->icmph_type == ICMP_REDIRECT)
1777 1777 return (B_TRUE);
1778 1778
1779 1779 /*
1780 1780 * ICMP errors only.
1781 1781 */
1782 1782 switch (ipha->ipha_protocol) {
1783 1783 case IPPROTO_UDP:
1784 1784 /*
1785 1785 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1786 1786 * transport header.
1787 1787 */
1788 1788 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1789 1789 mp->b_wptr)
1790 1790 goto truncated;
1791 1791 break;
1792 1792 case IPPROTO_TCP: {
1793 1793 tcpha_t *tcpha;
1794 1794
1795 1795 /*
1796 1796 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1797 1797 * transport header.
1798 1798 */
1799 1799 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1800 1800 mp->b_wptr)
1801 1801 goto truncated;
1802 1802
1803 1803 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1804 1804 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1805 1805 ipst);
1806 1806 if (connp == NULL)
1807 1807 goto discard_pkt;
1808 1808
1809 1809 if ((connp->conn_verifyicmp != NULL) &&
1810 1810 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1811 1811 CONN_DEC_REF(connp);
1812 1812 goto discard_pkt;
1813 1813 }
1814 1814 CONN_DEC_REF(connp);
1815 1815 break;
1816 1816 }
1817 1817 case IPPROTO_SCTP:
1818 1818 /*
1819 1819 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1820 1820 * transport header.
1821 1821 */
1822 1822 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1823 1823 mp->b_wptr)
1824 1824 goto truncated;
1825 1825 break;
1826 1826 case IPPROTO_ESP:
1827 1827 case IPPROTO_AH:
1828 1828 break;
1829 1829 case IPPROTO_ENCAP:
1830 1830 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1831 1831 mp->b_wptr)
1832 1832 goto truncated;
1833 1833 break;
1834 1834 default:
1835 1835 break;
1836 1836 }
1837 1837
1838 1838 return (B_TRUE);
1839 1839
1840 1840 discard_pkt:
1841 1841 /* Bogus ICMP error. */
1842 1842 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1843 1843 return (B_FALSE);
1844 1844
1845 1845 truncated:
1846 1846 /* We pulled up everthing already. Must be truncated */
1847 1847 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1848 1848 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1849 1849 return (B_FALSE);
1850 1850 }
1851 1851
1852 1852 /* Table from RFC 1191 */
1853 1853 static int icmp_frag_size_table[] =
1854 1854 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1855 1855
1856 1856 /*
1857 1857 * Process received ICMP Packet too big.
1858 1858 * Just handles the DCE create/update, including using the above table of
1859 1859 * PMTU guesses. The caller is responsible for validating the packet before
1860 1860 * passing it in and also to fanout the ICMP error to any matching transport
1861 1861 * conns. Assumes the message has been fully pulled up and verified.
1862 1862 *
1863 1863 * Before getting here, the caller has called icmp_inbound_verify_v4()
1864 1864 * that should have verified with ULP to prevent undoing the changes we're
1865 1865 * going to make to DCE. For example, TCP might have verified that the packet
1866 1866 * which generated error is in the send window.
1867 1867 *
1868 1868 * In some cases modified this MTU in the ICMP header packet; the caller
1869 1869 * should pass to the matching ULP after this returns.
1870 1870 */
1871 1871 static void
1872 1872 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1873 1873 {
1874 1874 dce_t *dce;
1875 1875 int old_mtu;
1876 1876 int mtu, orig_mtu;
1877 1877 ipaddr_t dst;
1878 1878 boolean_t disable_pmtud;
1879 1879 ill_t *ill = ira->ira_ill;
1880 1880 ip_stack_t *ipst = ill->ill_ipst;
1881 1881 uint_t hdr_length;
1882 1882 ipha_t *ipha;
1883 1883
1884 1884 /* Caller already pulled up everything. */
1885 1885 ipha = (ipha_t *)&icmph[1];
1886 1886 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1887 1887 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1888 1888 ASSERT(ill != NULL);
1889 1889
1890 1890 hdr_length = IPH_HDR_LENGTH(ipha);
1891 1891
1892 1892 /*
1893 1893 * We handle path MTU for source routed packets since the DCE
1894 1894 * is looked up using the final destination.
1895 1895 */
1896 1896 dst = ip_get_dst(ipha);
1897 1897
1898 1898 dce = dce_lookup_and_add_v4(dst, ipst);
1899 1899 if (dce == NULL) {
1900 1900 /* Couldn't add a unique one - ENOMEM */
1901 1901 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1902 1902 ntohl(dst)));
1903 1903 return;
1904 1904 }
1905 1905
1906 1906 /* Check for MTU discovery advice as described in RFC 1191 */
1907 1907 mtu = ntohs(icmph->icmph_du_mtu);
1908 1908 orig_mtu = mtu;
1909 1909 disable_pmtud = B_FALSE;
1910 1910
1911 1911 mutex_enter(&dce->dce_lock);
1912 1912 if (dce->dce_flags & DCEF_PMTU)
1913 1913 old_mtu = dce->dce_pmtu;
1914 1914 else
1915 1915 old_mtu = ill->ill_mtu;
1916 1916
1917 1917 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1918 1918 uint32_t length;
1919 1919 int i;
1920 1920
1921 1921 /*
1922 1922 * Use the table from RFC 1191 to figure out
1923 1923 * the next "plateau" based on the length in
1924 1924 * the original IP packet.
1925 1925 */
1926 1926 length = ntohs(ipha->ipha_length);
1927 1927 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1928 1928 uint32_t, length);
1929 1929 if (old_mtu <= length &&
1930 1930 old_mtu >= length - hdr_length) {
1931 1931 /*
1932 1932 * Handle broken BSD 4.2 systems that
1933 1933 * return the wrong ipha_length in ICMP
1934 1934 * errors.
1935 1935 */
1936 1936 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1937 1937 length, old_mtu));
1938 1938 length -= hdr_length;
1939 1939 }
1940 1940 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1941 1941 if (length > icmp_frag_size_table[i])
1942 1942 break;
1943 1943 }
1944 1944 if (i == A_CNT(icmp_frag_size_table)) {
1945 1945 /* Smaller than IP_MIN_MTU! */
1946 1946 ip1dbg(("Too big for packet size %d\n",
1947 1947 length));
1948 1948 disable_pmtud = B_TRUE;
1949 1949 mtu = ipst->ips_ip_pmtu_min;
1950 1950 } else {
1951 1951 mtu = icmp_frag_size_table[i];
1952 1952 ip1dbg(("Calculated mtu %d, packet size %d, "
1953 1953 "before %d\n", mtu, length, old_mtu));
1954 1954 if (mtu < ipst->ips_ip_pmtu_min) {
1955 1955 mtu = ipst->ips_ip_pmtu_min;
1956 1956 disable_pmtud = B_TRUE;
1957 1957 }
1958 1958 }
1959 1959 }
1960 1960 if (disable_pmtud)
1961 1961 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1962 1962 else
1963 1963 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1964 1964
1965 1965 dce->dce_pmtu = MIN(old_mtu, mtu);
1966 1966 /* Prepare to send the new max frag size for the ULP. */
1967 1967 icmph->icmph_du_zero = 0;
1968 1968 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1969 1969 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1970 1970 dce, int, orig_mtu, int, mtu);
1971 1971
1972 1972 /* We now have a PMTU for sure */
1973 1973 dce->dce_flags |= DCEF_PMTU;
1974 1974 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1975 1975 mutex_exit(&dce->dce_lock);
1976 1976 /*
1977 1977 * After dropping the lock the new value is visible to everyone.
1978 1978 * Then we bump the generation number so any cached values reinspect
1979 1979 * the dce_t.
1980 1980 */
1981 1981 dce_increment_generation(dce);
1982 1982 dce_refrele(dce);
1983 1983 }
1984 1984
1985 1985 /*
1986 1986 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1987 1987 * calls this function.
1988 1988 */
1989 1989 static mblk_t *
1990 1990 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1991 1991 {
1992 1992 int length;
1993 1993
1994 1994 ASSERT(mp->b_datap->db_type == M_DATA);
1995 1995
1996 1996 /* icmp_inbound_v4 has already pulled up the whole error packet */
1997 1997 ASSERT(mp->b_cont == NULL);
1998 1998
1999 1999 /*
2000 2000 * The length that we want to overlay is the inner header
2001 2001 * and what follows it.
2002 2002 */
2003 2003 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
2004 2004
2005 2005 /*
2006 2006 * Overlay the inner header and whatever follows it over the
2007 2007 * outer header.
2008 2008 */
2009 2009 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2010 2010
2011 2011 /* Adjust for what we removed */
2012 2012 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2013 2013 return (mp);
2014 2014 }
2015 2015
2016 2016 /*
2017 2017 * Try to pass the ICMP message upstream in case the ULP cares.
2018 2018 *
2019 2019 * If the packet that caused the ICMP error is secure, we send
2020 2020 * it to AH/ESP to make sure that the attached packet has a
2021 2021 * valid association. ipha in the code below points to the
2022 2022 * IP header of the packet that caused the error.
2023 2023 *
2024 2024 * For IPsec cases, we let the next-layer-up (which has access to
2025 2025 * cached policy on the conn_t, or can query the SPD directly)
2026 2026 * subtract out any IPsec overhead if they must. We therefore make no
2027 2027 * adjustments here for IPsec overhead.
2028 2028 *
2029 2029 * IFN could have been generated locally or by some router.
2030 2030 *
2031 2031 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2032 2032 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2033 2033 * This happens because IP adjusted its value of MTU on an
2034 2034 * earlier IFN message and could not tell the upper layer,
2035 2035 * the new adjusted value of MTU e.g. Packet was encrypted
2036 2036 * or there was not enough information to fanout to upper
2037 2037 * layers. Thus on the next outbound datagram, ire_send_wire
2038 2038 * generates the IFN, where IPsec processing has *not* been
2039 2039 * done.
2040 2040 *
2041 2041 * Note that we retain ixa_fragsize across IPsec thus once
2042 2042 * we have picking ixa_fragsize and entered ipsec_out_process we do
2043 2043 * no change the fragsize even if the path MTU changes before
2044 2044 * we reach ip_output_post_ipsec.
2045 2045 *
2046 2046 * In the local case, IRAF_LOOPBACK will be set indicating
2047 2047 * that IFN was generated locally.
2048 2048 *
2049 2049 * ROUTER : IFN could be secure or non-secure.
2050 2050 *
2051 2051 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2052 2052 * packet in error has AH/ESP headers to validate the AH/ESP
2053 2053 * headers. AH/ESP will verify whether there is a valid SA or
2054 2054 * not and send it back. We will fanout again if we have more
2055 2055 * data in the packet.
2056 2056 *
2057 2057 * If the packet in error does not have AH/ESP, we handle it
2058 2058 * like any other case.
2059 2059 *
2060 2060 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2061 2061 * up to AH/ESP for validation. AH/ESP will verify whether there is a
2062 2062 * valid SA or not and send it back. We will fanout again if
2063 2063 * we have more data in the packet.
2064 2064 *
2065 2065 * If the packet in error does not have AH/ESP, we handle it
2066 2066 * like any other case.
2067 2067 *
2068 2068 * The caller must have called icmp_inbound_verify_v4.
2069 2069 */
2070 2070 static void
2071 2071 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2072 2072 {
2073 2073 uint16_t *up; /* Pointer to ports in ULP header */
2074 2074 uint32_t ports; /* reversed ports for fanout */
2075 2075 ipha_t ripha; /* With reversed addresses */
2076 2076 ipha_t *ipha; /* Inner IP header */
2077 2077 uint_t hdr_length; /* Inner IP header length */
2078 2078 tcpha_t *tcpha;
2079 2079 conn_t *connp;
2080 2080 ill_t *ill = ira->ira_ill;
2081 2081 ip_stack_t *ipst = ill->ill_ipst;
2082 2082 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2083 2083 ill_t *rill = ira->ira_rill;
2084 2084
2085 2085 /* Caller already pulled up everything. */
2086 2086 ipha = (ipha_t *)&icmph[1];
2087 2087 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2088 2088 ASSERT(mp->b_cont == NULL);
2089 2089
2090 2090 hdr_length = IPH_HDR_LENGTH(ipha);
2091 2091 ira->ira_protocol = ipha->ipha_protocol;
2092 2092
2093 2093 /*
2094 2094 * We need a separate IP header with the source and destination
2095 2095 * addresses reversed to do fanout/classification because the ipha in
2096 2096 * the ICMP error is in the form we sent it out.
2097 2097 */
2098 2098 ripha.ipha_src = ipha->ipha_dst;
2099 2099 ripha.ipha_dst = ipha->ipha_src;
2100 2100 ripha.ipha_protocol = ipha->ipha_protocol;
2101 2101 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2102 2102
2103 2103 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2104 2104 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2105 2105 ntohl(ipha->ipha_dst),
2106 2106 icmph->icmph_type, icmph->icmph_code));
2107 2107
2108 2108 switch (ipha->ipha_protocol) {
2109 2109 case IPPROTO_UDP:
2110 2110 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2111 2111
2112 2112 /* Attempt to find a client stream based on port. */
2113 2113 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2114 2114 ntohs(up[0]), ntohs(up[1])));
2115 2115
2116 2116 /* Note that we send error to all matches. */
2117 2117 ira->ira_flags |= IRAF_ICMP_ERROR;
2118 2118 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2119 2119 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2120 2120 return;
2121 2121
2122 2122 case IPPROTO_TCP:
2123 2123 /*
2124 2124 * Find a TCP client stream for this packet.
2125 2125 * Note that we do a reverse lookup since the header is
2126 2126 * in the form we sent it out.
2127 2127 */
2128 2128 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2129 2129 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2130 2130 ipst);
2131 2131 if (connp == NULL)
2132 2132 goto discard_pkt;
2133 2133
2134 2134 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2135 2135 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2136 2136 mp = ipsec_check_inbound_policy(mp, connp,
2137 2137 ipha, NULL, ira);
2138 2138 if (mp == NULL) {
2139 2139 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2140 2140 /* Note that mp is NULL */
2141 2141 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2142 2142 CONN_DEC_REF(connp);
2143 2143 return;
2144 2144 }
2145 2145 }
2146 2146
2147 2147 ira->ira_flags |= IRAF_ICMP_ERROR;
2148 2148 ira->ira_ill = ira->ira_rill = NULL;
2149 2149 if (IPCL_IS_TCP(connp)) {
2150 2150 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2151 2151 connp->conn_recvicmp, connp, ira, SQ_FILL,
2152 2152 SQTAG_TCP_INPUT_ICMP_ERR);
2153 2153 } else {
2154 2154 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2155 2155 (connp->conn_recv)(connp, mp, NULL, ira);
2156 2156 CONN_DEC_REF(connp);
2157 2157 }
2158 2158 ira->ira_ill = ill;
2159 2159 ira->ira_rill = rill;
2160 2160 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2161 2161 return;
2162 2162
2163 2163 case IPPROTO_SCTP:
2164 2164 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2165 2165 /* Find a SCTP client stream for this packet. */
2166 2166 ((uint16_t *)&ports)[0] = up[1];
2167 2167 ((uint16_t *)&ports)[1] = up[0];
2168 2168
2169 2169 ira->ira_flags |= IRAF_ICMP_ERROR;
2170 2170 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2171 2171 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2172 2172 return;
2173 2173
2174 2174 case IPPROTO_ESP:
2175 2175 case IPPROTO_AH:
2176 2176 if (!ipsec_loaded(ipss)) {
2177 2177 ip_proto_not_sup(mp, ira);
2178 2178 return;
2179 2179 }
2180 2180
2181 2181 if (ipha->ipha_protocol == IPPROTO_ESP)
2182 2182 mp = ipsecesp_icmp_error(mp, ira);
2183 2183 else
2184 2184 mp = ipsecah_icmp_error(mp, ira);
2185 2185 if (mp == NULL)
2186 2186 return;
2187 2187
2188 2188 /* Just in case ipsec didn't preserve the NULL b_cont */
2189 2189 if (mp->b_cont != NULL) {
2190 2190 if (!pullupmsg(mp, -1))
2191 2191 goto discard_pkt;
2192 2192 }
2193 2193
2194 2194 /*
2195 2195 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2196 2196 * correct, but we don't use them any more here.
2197 2197 *
2198 2198 * If succesful, the mp has been modified to not include
2199 2199 * the ESP/AH header so we can fanout to the ULP's icmp
2200 2200 * error handler.
2201 2201 */
2202 2202 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2203 2203 goto truncated;
2204 2204
2205 2205 /* Verify the modified message before any further processes. */
2206 2206 ipha = (ipha_t *)mp->b_rptr;
2207 2207 hdr_length = IPH_HDR_LENGTH(ipha);
2208 2208 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2209 2209 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2210 2210 freemsg(mp);
2211 2211 return;
2212 2212 }
2213 2213
2214 2214 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2215 2215 return;
2216 2216
2217 2217 case IPPROTO_ENCAP: {
2218 2218 /* Look for self-encapsulated packets that caused an error */
2219 2219 ipha_t *in_ipha;
2220 2220
2221 2221 /*
2222 2222 * Caller has verified that length has to be
2223 2223 * at least the size of IP header.
2224 2224 */
2225 2225 ASSERT(hdr_length >= sizeof (ipha_t));
2226 2226 /*
2227 2227 * Check the sanity of the inner IP header like
2228 2228 * we did for the outer header.
2229 2229 */
2230 2230 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2231 2231 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2232 2232 goto discard_pkt;
2233 2233 }
2234 2234 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2235 2235 goto discard_pkt;
2236 2236 }
2237 2237 /* Check for Self-encapsulated tunnels */
2238 2238 if (in_ipha->ipha_src == ipha->ipha_src &&
2239 2239 in_ipha->ipha_dst == ipha->ipha_dst) {
2240 2240
2241 2241 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2242 2242 in_ipha);
2243 2243 if (mp == NULL)
2244 2244 goto discard_pkt;
2245 2245
2246 2246 /*
2247 2247 * Just in case self_encap didn't preserve the NULL
2248 2248 * b_cont
2249 2249 */
2250 2250 if (mp->b_cont != NULL) {
2251 2251 if (!pullupmsg(mp, -1))
2252 2252 goto discard_pkt;
2253 2253 }
2254 2254 /*
2255 2255 * Note that ira_pktlen and ira_ip_hdr_length are no
2256 2256 * longer correct, but we don't use them any more here.
2257 2257 */
2258 2258 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2259 2259 goto truncated;
2260 2260
2261 2261 /*
2262 2262 * Verify the modified message before any further
2263 2263 * processes.
2264 2264 */
2265 2265 ipha = (ipha_t *)mp->b_rptr;
2266 2266 hdr_length = IPH_HDR_LENGTH(ipha);
2267 2267 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2268 2268 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2269 2269 freemsg(mp);
2270 2270 return;
2271 2271 }
2272 2272
2273 2273 /*
2274 2274 * The packet in error is self-encapsualted.
2275 2275 * And we are finding it further encapsulated
2276 2276 * which we could not have possibly generated.
2277 2277 */
2278 2278 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2279 2279 goto discard_pkt;
2280 2280 }
2281 2281 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2282 2282 return;
2283 2283 }
2284 2284 /* No self-encapsulated */
2285 2285 /* FALLTHRU */
2286 2286 }
2287 2287 case IPPROTO_IPV6:
2288 2288 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2289 2289 &ripha.ipha_dst, ipst)) != NULL) {
2290 2290 ira->ira_flags |= IRAF_ICMP_ERROR;
2291 2291 connp->conn_recvicmp(connp, mp, NULL, ira);
2292 2292 CONN_DEC_REF(connp);
2293 2293 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2294 2294 return;
2295 2295 }
2296 2296 /*
2297 2297 * No IP tunnel is interested, fallthrough and see
2298 2298 * if a raw socket will want it.
2299 2299 */
2300 2300 /* FALLTHRU */
2301 2301 default:
2302 2302 ira->ira_flags |= IRAF_ICMP_ERROR;
2303 2303 ip_fanout_proto_v4(mp, &ripha, ira);
2304 2304 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2305 2305 return;
2306 2306 }
2307 2307 /* NOTREACHED */
2308 2308 discard_pkt:
2309 2309 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2310 2310 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2311 2311 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2312 2312 freemsg(mp);
2313 2313 return;
2314 2314
2315 2315 truncated:
2316 2316 /* We pulled up everthing already. Must be truncated */
2317 2317 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2318 2318 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2319 2319 freemsg(mp);
2320 2320 }
2321 2321
2322 2322 /*
2323 2323 * Common IP options parser.
2324 2324 *
2325 2325 * Setup routine: fill in *optp with options-parsing state, then
2326 2326 * tail-call ipoptp_next to return the first option.
2327 2327 */
2328 2328 uint8_t
2329 2329 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2330 2330 {
2331 2331 uint32_t totallen; /* total length of all options */
2332 2332
2333 2333 totallen = ipha->ipha_version_and_hdr_length -
2334 2334 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2335 2335 totallen <<= 2;
2336 2336 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2337 2337 optp->ipoptp_end = optp->ipoptp_next + totallen;
2338 2338 optp->ipoptp_flags = 0;
2339 2339 return (ipoptp_next(optp));
2340 2340 }
2341 2341
2342 2342 /* Like above but without an ipha_t */
2343 2343 uint8_t
2344 2344 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2345 2345 {
2346 2346 optp->ipoptp_next = opt;
2347 2347 optp->ipoptp_end = optp->ipoptp_next + totallen;
2348 2348 optp->ipoptp_flags = 0;
2349 2349 return (ipoptp_next(optp));
2350 2350 }
2351 2351
2352 2352 /*
2353 2353 * Common IP options parser: extract next option.
2354 2354 */
2355 2355 uint8_t
2356 2356 ipoptp_next(ipoptp_t *optp)
2357 2357 {
2358 2358 uint8_t *end = optp->ipoptp_end;
2359 2359 uint8_t *cur = optp->ipoptp_next;
2360 2360 uint8_t opt, len, pointer;
2361 2361
2362 2362 /*
2363 2363 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2364 2364 * has been corrupted.
2365 2365 */
2366 2366 ASSERT(cur <= end);
2367 2367
2368 2368 if (cur == end)
2369 2369 return (IPOPT_EOL);
2370 2370
2371 2371 opt = cur[IPOPT_OPTVAL];
2372 2372
2373 2373 /*
2374 2374 * Skip any NOP options.
2375 2375 */
2376 2376 while (opt == IPOPT_NOP) {
2377 2377 cur++;
2378 2378 if (cur == end)
2379 2379 return (IPOPT_EOL);
2380 2380 opt = cur[IPOPT_OPTVAL];
2381 2381 }
2382 2382
2383 2383 if (opt == IPOPT_EOL)
2384 2384 return (IPOPT_EOL);
2385 2385
2386 2386 /*
2387 2387 * Option requiring a length.
2388 2388 */
2389 2389 if ((cur + 1) >= end) {
2390 2390 optp->ipoptp_flags |= IPOPTP_ERROR;
2391 2391 return (IPOPT_EOL);
2392 2392 }
2393 2393 len = cur[IPOPT_OLEN];
2394 2394 if (len < 2) {
2395 2395 optp->ipoptp_flags |= IPOPTP_ERROR;
2396 2396 return (IPOPT_EOL);
2397 2397 }
2398 2398 optp->ipoptp_cur = cur;
2399 2399 optp->ipoptp_len = len;
2400 2400 optp->ipoptp_next = cur + len;
2401 2401 if (cur + len > end) {
2402 2402 optp->ipoptp_flags |= IPOPTP_ERROR;
2403 2403 return (IPOPT_EOL);
2404 2404 }
2405 2405
2406 2406 /*
2407 2407 * For the options which require a pointer field, make sure
2408 2408 * its there, and make sure it points to either something
2409 2409 * inside this option, or the end of the option.
2410 2410 */
2411 2411 switch (opt) {
2412 2412 case IPOPT_RR:
2413 2413 case IPOPT_TS:
2414 2414 case IPOPT_LSRR:
2415 2415 case IPOPT_SSRR:
2416 2416 if (len <= IPOPT_OFFSET) {
2417 2417 optp->ipoptp_flags |= IPOPTP_ERROR;
2418 2418 return (opt);
2419 2419 }
2420 2420 pointer = cur[IPOPT_OFFSET];
2421 2421 if (pointer - 1 > len) {
2422 2422 optp->ipoptp_flags |= IPOPTP_ERROR;
2423 2423 return (opt);
2424 2424 }
2425 2425 break;
2426 2426 }
2427 2427
2428 2428 /*
2429 2429 * Sanity check the pointer field based on the type of the
2430 2430 * option.
2431 2431 */
2432 2432 switch (opt) {
2433 2433 case IPOPT_RR:
2434 2434 case IPOPT_SSRR:
2435 2435 case IPOPT_LSRR:
2436 2436 if (pointer < IPOPT_MINOFF_SR)
2437 2437 optp->ipoptp_flags |= IPOPTP_ERROR;
2438 2438 break;
2439 2439 case IPOPT_TS:
2440 2440 if (pointer < IPOPT_MINOFF_IT)
2441 2441 optp->ipoptp_flags |= IPOPTP_ERROR;
2442 2442 /*
2443 2443 * Note that the Internet Timestamp option also
2444 2444 * contains two four bit fields (the Overflow field,
2445 2445 * and the Flag field), which follow the pointer
2446 2446 * field. We don't need to check that these fields
2447 2447 * fall within the length of the option because this
2448 2448 * was implicitely done above. We've checked that the
2449 2449 * pointer value is at least IPOPT_MINOFF_IT, and that
2450 2450 * it falls within the option. Since IPOPT_MINOFF_IT >
2451 2451 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2452 2452 */
2453 2453 ASSERT(len > IPOPT_POS_OV_FLG);
2454 2454 break;
2455 2455 }
2456 2456
2457 2457 return (opt);
2458 2458 }
2459 2459
2460 2460 /*
2461 2461 * Use the outgoing IP header to create an IP_OPTIONS option the way
2462 2462 * it was passed down from the application.
2463 2463 *
2464 2464 * This is compatible with BSD in that it returns
2465 2465 * the reverse source route with the final destination
2466 2466 * as the last entry. The first 4 bytes of the option
2467 2467 * will contain the final destination.
2468 2468 */
2469 2469 int
2470 2470 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2471 2471 {
2472 2472 ipoptp_t opts;
2473 2473 uchar_t *opt;
2474 2474 uint8_t optval;
2475 2475 uint8_t optlen;
2476 2476 uint32_t len = 0;
2477 2477 uchar_t *buf1 = buf;
2478 2478 uint32_t totallen;
2479 2479 ipaddr_t dst;
2480 2480 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2481 2481
2482 2482 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2483 2483 return (0);
2484 2484
2485 2485 totallen = ipp->ipp_ipv4_options_len;
2486 2486 if (totallen & 0x3)
2487 2487 return (0);
2488 2488
2489 2489 buf += IP_ADDR_LEN; /* Leave room for final destination */
2490 2490 len += IP_ADDR_LEN;
2491 2491 bzero(buf1, IP_ADDR_LEN);
2492 2492
2493 2493 dst = connp->conn_faddr_v4;
2494 2494
2495 2495 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2496 2496 optval != IPOPT_EOL;
2497 2497 optval = ipoptp_next(&opts)) {
2498 2498 int off;
2499 2499
2500 2500 opt = opts.ipoptp_cur;
2501 2501 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2502 2502 break;
2503 2503 }
2504 2504 optlen = opts.ipoptp_len;
2505 2505
2506 2506 switch (optval) {
2507 2507 case IPOPT_SSRR:
2508 2508 case IPOPT_LSRR:
2509 2509
2510 2510 /*
2511 2511 * Insert destination as the first entry in the source
2512 2512 * route and move down the entries on step.
2513 2513 * The last entry gets placed at buf1.
2514 2514 */
2515 2515 buf[IPOPT_OPTVAL] = optval;
2516 2516 buf[IPOPT_OLEN] = optlen;
2517 2517 buf[IPOPT_OFFSET] = optlen;
2518 2518
2519 2519 off = optlen - IP_ADDR_LEN;
2520 2520 if (off < 0) {
2521 2521 /* No entries in source route */
2522 2522 break;
2523 2523 }
2524 2524 /* Last entry in source route if not already set */
2525 2525 if (dst == INADDR_ANY)
2526 2526 bcopy(opt + off, buf1, IP_ADDR_LEN);
2527 2527 off -= IP_ADDR_LEN;
2528 2528
2529 2529 while (off > 0) {
2530 2530 bcopy(opt + off,
2531 2531 buf + off + IP_ADDR_LEN,
2532 2532 IP_ADDR_LEN);
2533 2533 off -= IP_ADDR_LEN;
2534 2534 }
2535 2535 /* ipha_dst into first slot */
2536 2536 bcopy(&dst, buf + off + IP_ADDR_LEN,
2537 2537 IP_ADDR_LEN);
2538 2538 buf += optlen;
2539 2539 len += optlen;
2540 2540 break;
2541 2541
2542 2542 default:
2543 2543 bcopy(opt, buf, optlen);
2544 2544 buf += optlen;
2545 2545 len += optlen;
2546 2546 break;
2547 2547 }
2548 2548 }
2549 2549 done:
2550 2550 /* Pad the resulting options */
2551 2551 while (len & 0x3) {
2552 2552 *buf++ = IPOPT_EOL;
2553 2553 len++;
2554 2554 }
2555 2555 return (len);
2556 2556 }
2557 2557
2558 2558 /*
2559 2559 * Update any record route or timestamp options to include this host.
2560 2560 * Reverse any source route option.
2561 2561 * This routine assumes that the options are well formed i.e. that they
2562 2562 * have already been checked.
2563 2563 */
2564 2564 static void
2565 2565 icmp_options_update(ipha_t *ipha)
2566 2566 {
2567 2567 ipoptp_t opts;
2568 2568 uchar_t *opt;
2569 2569 uint8_t optval;
2570 2570 ipaddr_t src; /* Our local address */
2571 2571 ipaddr_t dst;
2572 2572
2573 2573 ip2dbg(("icmp_options_update\n"));
2574 2574 src = ipha->ipha_src;
2575 2575 dst = ipha->ipha_dst;
2576 2576
2577 2577 for (optval = ipoptp_first(&opts, ipha);
2578 2578 optval != IPOPT_EOL;
2579 2579 optval = ipoptp_next(&opts)) {
2580 2580 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2581 2581 opt = opts.ipoptp_cur;
2582 2582 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2583 2583 optval, opts.ipoptp_len));
2584 2584 switch (optval) {
2585 2585 int off1, off2;
2586 2586 case IPOPT_SSRR:
2587 2587 case IPOPT_LSRR:
2588 2588 /*
2589 2589 * Reverse the source route. The first entry
2590 2590 * should be the next to last one in the current
2591 2591 * source route (the last entry is our address).
2592 2592 * The last entry should be the final destination.
2593 2593 */
2594 2594 off1 = IPOPT_MINOFF_SR - 1;
2595 2595 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2596 2596 if (off2 < 0) {
2597 2597 /* No entries in source route */
2598 2598 ip1dbg((
2599 2599 "icmp_options_update: bad src route\n"));
2600 2600 break;
2601 2601 }
2602 2602 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2603 2603 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2604 2604 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2605 2605 off2 -= IP_ADDR_LEN;
2606 2606
2607 2607 while (off1 < off2) {
2608 2608 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2609 2609 bcopy((char *)opt + off2, (char *)opt + off1,
2610 2610 IP_ADDR_LEN);
2611 2611 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2612 2612 off1 += IP_ADDR_LEN;
2613 2613 off2 -= IP_ADDR_LEN;
2614 2614 }
2615 2615 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2616 2616 break;
2617 2617 }
2618 2618 }
2619 2619 }
2620 2620
2621 2621 /*
2622 2622 * Process received ICMP Redirect messages.
2623 2623 * Assumes the caller has verified that the headers are in the pulled up mblk.
2624 2624 * Consumes mp.
2625 2625 */
2626 2626 static void
2627 2627 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2628 2628 {
2629 2629 ire_t *ire, *nire;
2630 2630 ire_t *prev_ire;
2631 2631 ipaddr_t src, dst, gateway;
2632 2632 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2633 2633 ipha_t *inner_ipha; /* Inner IP header */
2634 2634
2635 2635 /* Caller already pulled up everything. */
2636 2636 inner_ipha = (ipha_t *)&icmph[1];
2637 2637 src = ipha->ipha_src;
2638 2638 dst = inner_ipha->ipha_dst;
2639 2639 gateway = icmph->icmph_rd_gateway;
2640 2640 /* Make sure the new gateway is reachable somehow. */
2641 2641 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2642 2642 ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2643 2643 /*
2644 2644 * Make sure we had a route for the dest in question and that
2645 2645 * that route was pointing to the old gateway (the source of the
2646 2646 * redirect packet.)
2647 2647 * We do longest match and then compare ire_gateway_addr below.
2648 2648 */
2649 2649 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2650 2650 NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2651 2651 /*
2652 2652 * Check that
2653 2653 * the redirect was not from ourselves
2654 2654 * the new gateway and the old gateway are directly reachable
2655 2655 */
2656 2656 if (prev_ire == NULL || ire == NULL ||
2657 2657 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2658 2658 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2659 2659 !(ire->ire_type & IRE_IF_ALL) ||
2660 2660 prev_ire->ire_gateway_addr != src) {
2661 2661 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2662 2662 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2663 2663 freemsg(mp);
2664 2664 if (ire != NULL)
2665 2665 ire_refrele(ire);
2666 2666 if (prev_ire != NULL)
2667 2667 ire_refrele(prev_ire);
2668 2668 return;
2669 2669 }
2670 2670
2671 2671 ire_refrele(prev_ire);
2672 2672 ire_refrele(ire);
2673 2673
2674 2674 /*
2675 2675 * TODO: more precise handling for cases 0, 2, 3, the latter two
2676 2676 * require TOS routing
2677 2677 */
2678 2678 switch (icmph->icmph_code) {
2679 2679 case 0:
2680 2680 case 1:
2681 2681 /* TODO: TOS specificity for cases 2 and 3 */
2682 2682 case 2:
2683 2683 case 3:
2684 2684 break;
2685 2685 default:
2686 2686 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2687 2687 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2688 2688 freemsg(mp);
2689 2689 return;
2690 2690 }
2691 2691 /*
2692 2692 * Create a Route Association. This will allow us to remember that
2693 2693 * someone we believe told us to use the particular gateway.
2694 2694 */
2695 2695 ire = ire_create(
2696 2696 (uchar_t *)&dst, /* dest addr */
2697 2697 (uchar_t *)&ip_g_all_ones, /* mask */
2698 2698 (uchar_t *)&gateway, /* gateway addr */
2699 2699 IRE_HOST,
2700 2700 NULL, /* ill */
2701 2701 ALL_ZONES,
2702 2702 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2703 2703 NULL, /* tsol_gc_t */
2704 2704 ipst);
2705 2705
2706 2706 if (ire == NULL) {
2707 2707 freemsg(mp);
2708 2708 return;
2709 2709 }
2710 2710 nire = ire_add(ire);
2711 2711 /* Check if it was a duplicate entry */
2712 2712 if (nire != NULL && nire != ire) {
2713 2713 ASSERT(nire->ire_identical_ref > 1);
2714 2714 ire_delete(nire);
2715 2715 ire_refrele(nire);
2716 2716 nire = NULL;
2717 2717 }
2718 2718 ire = nire;
2719 2719 if (ire != NULL) {
2720 2720 ire_refrele(ire); /* Held in ire_add */
2721 2721
2722 2722 /* tell routing sockets that we received a redirect */
2723 2723 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2724 2724 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2725 2725 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2726 2726 }
2727 2727
2728 2728 /*
2729 2729 * Delete any existing IRE_HOST type redirect ires for this destination.
2730 2730 * This together with the added IRE has the effect of
2731 2731 * modifying an existing redirect.
2732 2732 */
2733 2733 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2734 2734 ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2735 2735 if (prev_ire != NULL) {
2736 2736 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2737 2737 ire_delete(prev_ire);
2738 2738 ire_refrele(prev_ire);
2739 2739 }
2740 2740
2741 2741 freemsg(mp);
2742 2742 }
2743 2743
2744 2744 /*
2745 2745 * Generate an ICMP parameter problem message.
2746 2746 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2747 2747 * constructed by the caller.
2748 2748 */
2749 2749 static void
2750 2750 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2751 2751 {
2752 2752 icmph_t icmph;
2753 2753 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2754 2754
2755 2755 mp = icmp_pkt_err_ok(mp, ira);
2756 2756 if (mp == NULL)
2757 2757 return;
2758 2758
2759 2759 bzero(&icmph, sizeof (icmph_t));
2760 2760 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2761 2761 icmph.icmph_pp_ptr = ptr;
2762 2762 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2763 2763 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2764 2764 }
2765 2765
2766 2766 /*
2767 2767 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2768 2768 * the ICMP header pointed to by "stuff". (May be called as writer.)
2769 2769 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2770 2770 * an icmp error packet can be sent.
2771 2771 * Assigns an appropriate source address to the packet. If ipha_dst is
2772 2772 * one of our addresses use it for source. Otherwise let ip_output_simple
2773 2773 * pick the source address.
2774 2774 */
2775 2775 static void
2776 2776 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2777 2777 {
2778 2778 ipaddr_t dst;
2779 2779 icmph_t *icmph;
2780 2780 ipha_t *ipha;
2781 2781 uint_t len_needed;
2782 2782 size_t msg_len;
2783 2783 mblk_t *mp1;
2784 2784 ipaddr_t src;
2785 2785 ire_t *ire;
2786 2786 ip_xmit_attr_t ixas;
2787 2787 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2788 2788
2789 2789 ipha = (ipha_t *)mp->b_rptr;
2790 2790
2791 2791 bzero(&ixas, sizeof (ixas));
2792 2792 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2793 2793 ixas.ixa_zoneid = ira->ira_zoneid;
2794 2794 ixas.ixa_ifindex = 0;
2795 2795 ixas.ixa_ipst = ipst;
2796 2796 ixas.ixa_cred = kcred;
2797 2797 ixas.ixa_cpid = NOPID;
2798 2798 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2799 2799 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2800 2800
2801 2801 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2802 2802 /*
2803 2803 * Apply IPsec based on how IPsec was applied to
2804 2804 * the packet that had the error.
2805 2805 *
2806 2806 * If it was an outbound packet that caused the ICMP
2807 2807 * error, then the caller will have setup the IRA
2808 2808 * appropriately.
2809 2809 */
2810 2810 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2811 2811 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2812 2812 /* Note: mp already consumed and ip_drop_packet done */
2813 2813 return;
2814 2814 }
2815 2815 } else {
2816 2816 /*
2817 2817 * This is in clear. The icmp message we are building
2818 2818 * here should go out in clear, independent of our policy.
2819 2819 */
2820 2820 ixas.ixa_flags |= IXAF_NO_IPSEC;
2821 2821 }
2822 2822
2823 2823 /* Remember our eventual destination */
2824 2824 dst = ipha->ipha_src;
2825 2825
2826 2826 /*
2827 2827 * If the packet was for one of our unicast addresses, make
2828 2828 * sure we respond with that as the source. Otherwise
2829 2829 * have ip_output_simple pick the source address.
2830 2830 */
2831 2831 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2832 2832 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2833 2833 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2834 2834 if (ire != NULL) {
2835 2835 ire_refrele(ire);
2836 2836 src = ipha->ipha_dst;
2837 2837 } else {
2838 2838 src = INADDR_ANY;
2839 2839 ixas.ixa_flags |= IXAF_SET_SOURCE;
2840 2840 }
2841 2841
2842 2842 /*
2843 2843 * Check if we can send back more then 8 bytes in addition to
2844 2844 * the IP header. We try to send 64 bytes of data and the internal
2845 2845 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2846 2846 */
2847 2847 len_needed = IPH_HDR_LENGTH(ipha);
2848 2848 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2849 2849 ipha->ipha_protocol == IPPROTO_IPV6) {
2850 2850 if (!pullupmsg(mp, -1)) {
2851 2851 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2852 2852 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2853 2853 freemsg(mp);
2854 2854 return;
2855 2855 }
2856 2856 ipha = (ipha_t *)mp->b_rptr;
2857 2857
2858 2858 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2859 2859 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2860 2860 len_needed));
2861 2861 } else {
2862 2862 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2863 2863
2864 2864 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2865 2865 len_needed += ip_hdr_length_v6(mp, ip6h);
2866 2866 }
2867 2867 }
2868 2868 len_needed += ipst->ips_ip_icmp_return;
2869 2869 msg_len = msgdsize(mp);
2870 2870 if (msg_len > len_needed) {
2871 2871 (void) adjmsg(mp, len_needed - msg_len);
2872 2872 msg_len = len_needed;
2873 2873 }
2874 2874 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2875 2875 if (mp1 == NULL) {
2876 2876 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2877 2877 freemsg(mp);
2878 2878 return;
2879 2879 }
2880 2880 mp1->b_cont = mp;
2881 2881 mp = mp1;
2882 2882
2883 2883 /*
2884 2884 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2885 2885 * node generates be accepted in peace by all on-host destinations.
2886 2886 * If we do NOT assume that all on-host destinations trust
2887 2887 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2888 2888 * (Look for IXAF_TRUSTED_ICMP).
2889 2889 */
2890 2890 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2891 2891
2892 2892 ipha = (ipha_t *)mp->b_rptr;
2893 2893 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2894 2894 *ipha = icmp_ipha;
2895 2895 ipha->ipha_src = src;
2896 2896 ipha->ipha_dst = dst;
2897 2897 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2898 2898 msg_len += sizeof (icmp_ipha) + len;
2899 2899 if (msg_len > IP_MAXPACKET) {
2900 2900 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2901 2901 msg_len = IP_MAXPACKET;
2902 2902 }
2903 2903 ipha->ipha_length = htons((uint16_t)msg_len);
2904 2904 icmph = (icmph_t *)&ipha[1];
2905 2905 bcopy(stuff, icmph, len);
2906 2906 icmph->icmph_checksum = 0;
2907 2907 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2908 2908 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2909 2909
2910 2910 (void) ip_output_simple(mp, &ixas);
2911 2911 ixa_cleanup(&ixas);
2912 2912 }
2913 2913
2914 2914 /*
2915 2915 * Determine if an ICMP error packet can be sent given the rate limit.
2916 2916 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2917 2917 * in milliseconds) and a burst size. Burst size number of packets can
2918 2918 * be sent arbitrarely closely spaced.
2919 2919 * The state is tracked using two variables to implement an approximate
2920 2920 * token bucket filter:
2921 2921 * icmp_pkt_err_last - lbolt value when the last burst started
2922 2922 * icmp_pkt_err_sent - number of packets sent in current burst
2923 2923 */
2924 2924 boolean_t
2925 2925 icmp_err_rate_limit(ip_stack_t *ipst)
2926 2926 {
2927 2927 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2928 2928 uint_t refilled; /* Number of packets refilled in tbf since last */
2929 2929 /* Guard against changes by loading into local variable */
2930 2930 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2931 2931
2932 2932 if (err_interval == 0)
2933 2933 return (B_FALSE);
2934 2934
2935 2935 if (ipst->ips_icmp_pkt_err_last > now) {
2936 2936 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2937 2937 ipst->ips_icmp_pkt_err_last = 0;
2938 2938 ipst->ips_icmp_pkt_err_sent = 0;
2939 2939 }
2940 2940 /*
2941 2941 * If we are in a burst update the token bucket filter.
2942 2942 * Update the "last" time to be close to "now" but make sure
2943 2943 * we don't loose precision.
2944 2944 */
2945 2945 if (ipst->ips_icmp_pkt_err_sent != 0) {
2946 2946 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2947 2947 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2948 2948 ipst->ips_icmp_pkt_err_sent = 0;
2949 2949 } else {
2950 2950 ipst->ips_icmp_pkt_err_sent -= refilled;
2951 2951 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2952 2952 }
2953 2953 }
2954 2954 if (ipst->ips_icmp_pkt_err_sent == 0) {
2955 2955 /* Start of new burst */
2956 2956 ipst->ips_icmp_pkt_err_last = now;
2957 2957 }
2958 2958 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2959 2959 ipst->ips_icmp_pkt_err_sent++;
2960 2960 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2961 2961 ipst->ips_icmp_pkt_err_sent));
2962 2962 return (B_FALSE);
2963 2963 }
2964 2964 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2965 2965 return (B_TRUE);
2966 2966 }
2967 2967
2968 2968 /*
2969 2969 * Check if it is ok to send an IPv4 ICMP error packet in
2970 2970 * response to the IPv4 packet in mp.
2971 2971 * Free the message and return null if no
2972 2972 * ICMP error packet should be sent.
2973 2973 */
2974 2974 static mblk_t *
2975 2975 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2976 2976 {
2977 2977 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2978 2978 icmph_t *icmph;
2979 2979 ipha_t *ipha;
2980 2980 uint_t len_needed;
2981 2981
2982 2982 if (!mp)
2983 2983 return (NULL);
2984 2984 ipha = (ipha_t *)mp->b_rptr;
2985 2985 if (ip_csum_hdr(ipha)) {
2986 2986 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2987 2987 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2988 2988 freemsg(mp);
2989 2989 return (NULL);
2990 2990 }
2991 2991 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2992 2992 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2993 2993 CLASSD(ipha->ipha_dst) ||
2994 2994 CLASSD(ipha->ipha_src) ||
2995 2995 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2996 2996 /* Note: only errors to the fragment with offset 0 */
2997 2997 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2998 2998 freemsg(mp);
2999 2999 return (NULL);
3000 3000 }
3001 3001 if (ipha->ipha_protocol == IPPROTO_ICMP) {
3002 3002 /*
3003 3003 * Check the ICMP type. RFC 1122 sez: don't send ICMP
3004 3004 * errors in response to any ICMP errors.
3005 3005 */
3006 3006 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3007 3007 if (mp->b_wptr - mp->b_rptr < len_needed) {
3008 3008 if (!pullupmsg(mp, len_needed)) {
3009 3009 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3010 3010 freemsg(mp);
3011 3011 return (NULL);
3012 3012 }
3013 3013 ipha = (ipha_t *)mp->b_rptr;
3014 3014 }
3015 3015 icmph = (icmph_t *)
3016 3016 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3017 3017 switch (icmph->icmph_type) {
3018 3018 case ICMP_DEST_UNREACHABLE:
3019 3019 case ICMP_SOURCE_QUENCH:
3020 3020 case ICMP_TIME_EXCEEDED:
3021 3021 case ICMP_PARAM_PROBLEM:
3022 3022 case ICMP_REDIRECT:
3023 3023 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3024 3024 freemsg(mp);
3025 3025 return (NULL);
3026 3026 default:
3027 3027 break;
3028 3028 }
3029 3029 }
3030 3030 /*
3031 3031 * If this is a labeled system, then check to see if we're allowed to
3032 3032 * send a response to this particular sender. If not, then just drop.
3033 3033 */
3034 3034 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3035 3035 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3036 3036 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3037 3037 freemsg(mp);
3038 3038 return (NULL);
3039 3039 }
3040 3040 if (icmp_err_rate_limit(ipst)) {
3041 3041 /*
3042 3042 * Only send ICMP error packets every so often.
3043 3043 * This should be done on a per port/source basis,
3044 3044 * but for now this will suffice.
3045 3045 */
3046 3046 freemsg(mp);
3047 3047 return (NULL);
3048 3048 }
3049 3049 return (mp);
3050 3050 }
3051 3051
3052 3052 /*
3053 3053 * Called when a packet was sent out the same link that it arrived on.
3054 3054 * Check if it is ok to send a redirect and then send it.
3055 3055 */
3056 3056 void
3057 3057 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3058 3058 ip_recv_attr_t *ira)
3059 3059 {
3060 3060 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3061 3061 ipaddr_t src, nhop;
3062 3062 mblk_t *mp1;
3063 3063 ire_t *nhop_ire;
3064 3064
3065 3065 /*
3066 3066 * Check the source address to see if it originated
3067 3067 * on the same logical subnet it is going back out on.
3068 3068 * If so, we should be able to send it a redirect.
3069 3069 * Avoid sending a redirect if the destination
3070 3070 * is directly connected (i.e., we matched an IRE_ONLINK),
3071 3071 * or if the packet was source routed out this interface.
3072 3072 *
3073 3073 * We avoid sending a redirect if the
3074 3074 * destination is directly connected
3075 3075 * because it is possible that multiple
3076 3076 * IP subnets may have been configured on
3077 3077 * the link, and the source may not
3078 3078 * be on the same subnet as ip destination,
3079 3079 * even though they are on the same
3080 3080 * physical link.
3081 3081 */
3082 3082 if ((ire->ire_type & IRE_ONLINK) ||
3083 3083 ip_source_routed(ipha, ipst))
3084 3084 return;
3085 3085
3086 3086 nhop_ire = ire_nexthop(ire);
3087 3087 if (nhop_ire == NULL)
3088 3088 return;
3089 3089
3090 3090 nhop = nhop_ire->ire_addr;
3091 3091
3092 3092 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3093 3093 ire_t *ire2;
3094 3094
3095 3095 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3096 3096 mutex_enter(&nhop_ire->ire_lock);
3097 3097 ire2 = nhop_ire->ire_dep_parent;
3098 3098 if (ire2 != NULL)
3099 3099 ire_refhold(ire2);
3100 3100 mutex_exit(&nhop_ire->ire_lock);
3101 3101 ire_refrele(nhop_ire);
3102 3102 nhop_ire = ire2;
3103 3103 }
3104 3104 if (nhop_ire == NULL)
3105 3105 return;
3106 3106
3107 3107 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3108 3108
3109 3109 src = ipha->ipha_src;
3110 3110
3111 3111 /*
3112 3112 * We look at the interface ire for the nexthop,
3113 3113 * to see if ipha_src is in the same subnet
3114 3114 * as the nexthop.
3115 3115 */
3116 3116 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3117 3117 /*
3118 3118 * The source is directly connected.
3119 3119 */
3120 3120 mp1 = copymsg(mp);
3121 3121 if (mp1 != NULL) {
3122 3122 icmp_send_redirect(mp1, nhop, ira);
3123 3123 }
3124 3124 }
3125 3125 ire_refrele(nhop_ire);
3126 3126 }
3127 3127
3128 3128 /*
3129 3129 * Generate an ICMP redirect message.
3130 3130 */
3131 3131 static void
3132 3132 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3133 3133 {
3134 3134 icmph_t icmph;
3135 3135 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3136 3136
3137 3137 mp = icmp_pkt_err_ok(mp, ira);
3138 3138 if (mp == NULL)
3139 3139 return;
3140 3140
3141 3141 bzero(&icmph, sizeof (icmph_t));
3142 3142 icmph.icmph_type = ICMP_REDIRECT;
3143 3143 icmph.icmph_code = 1;
3144 3144 icmph.icmph_rd_gateway = gateway;
3145 3145 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3146 3146 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3147 3147 }
3148 3148
3149 3149 /*
3150 3150 * Generate an ICMP time exceeded message.
3151 3151 */
3152 3152 void
3153 3153 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3154 3154 {
3155 3155 icmph_t icmph;
3156 3156 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3157 3157
3158 3158 mp = icmp_pkt_err_ok(mp, ira);
3159 3159 if (mp == NULL)
3160 3160 return;
3161 3161
3162 3162 bzero(&icmph, sizeof (icmph_t));
3163 3163 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3164 3164 icmph.icmph_code = code;
3165 3165 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3166 3166 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3167 3167 }
3168 3168
3169 3169 /*
3170 3170 * Generate an ICMP unreachable message.
3171 3171 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3172 3172 * constructed by the caller.
3173 3173 */
3174 3174 void
3175 3175 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3176 3176 {
3177 3177 icmph_t icmph;
3178 3178 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3179 3179
3180 3180 mp = icmp_pkt_err_ok(mp, ira);
3181 3181 if (mp == NULL)
3182 3182 return;
3183 3183
3184 3184 bzero(&icmph, sizeof (icmph_t));
3185 3185 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3186 3186 icmph.icmph_code = code;
3187 3187 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3188 3188 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3189 3189 }
3190 3190
3191 3191 /*
3192 3192 * Latch in the IPsec state for a stream based the policy in the listener
3193 3193 * and the actions in the ip_recv_attr_t.
3194 3194 * Called directly from TCP and SCTP.
3195 3195 */
3196 3196 boolean_t
3197 3197 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3198 3198 {
3199 3199 ASSERT(lconnp->conn_policy != NULL);
3200 3200 ASSERT(connp->conn_policy == NULL);
3201 3201
3202 3202 IPPH_REFHOLD(lconnp->conn_policy);
3203 3203 connp->conn_policy = lconnp->conn_policy;
3204 3204
3205 3205 if (ira->ira_ipsec_action != NULL) {
3206 3206 if (connp->conn_latch == NULL) {
3207 3207 connp->conn_latch = iplatch_create();
3208 3208 if (connp->conn_latch == NULL)
3209 3209 return (B_FALSE);
3210 3210 }
3211 3211 ipsec_latch_inbound(connp, ira);
3212 3212 }
3213 3213 return (B_TRUE);
3214 3214 }
3215 3215
3216 3216 /*
3217 3217 * Verify whether or not the IP address is a valid local address.
3218 3218 * Could be a unicast, including one for a down interface.
3219 3219 * If allow_mcbc then a multicast or broadcast address is also
3220 3220 * acceptable.
3221 3221 *
3222 3222 * In the case of a broadcast/multicast address, however, the
3223 3223 * upper protocol is expected to reset the src address
3224 3224 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3225 3225 * no packets are emitted with broadcast/multicast address as
3226 3226 * source address (that violates hosts requirements RFC 1122)
3227 3227 * The addresses valid for bind are:
3228 3228 * (1) - INADDR_ANY (0)
3229 3229 * (2) - IP address of an UP interface
3230 3230 * (3) - IP address of a DOWN interface
3231 3231 * (4) - valid local IP broadcast addresses. In this case
3232 3232 * the conn will only receive packets destined to
3233 3233 * the specified broadcast address.
3234 3234 * (5) - a multicast address. In this case
3235 3235 * the conn will only receive packets destined to
3236 3236 * the specified multicast address. Note: the
3237 3237 * application still has to issue an
3238 3238 * IP_ADD_MEMBERSHIP socket option.
3239 3239 *
3240 3240 * In all the above cases, the bound address must be valid in the current zone.
3241 3241 * When the address is loopback, multicast or broadcast, there might be many
3242 3242 * matching IREs so bind has to look up based on the zone.
3243 3243 */
3244 3244 ip_laddr_t
3245 3245 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3246 3246 ip_stack_t *ipst, boolean_t allow_mcbc)
3247 3247 {
3248 3248 ire_t *src_ire;
3249 3249
3250 3250 ASSERT(src_addr != INADDR_ANY);
3251 3251
3252 3252 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3253 3253 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3254 3254
3255 3255 /*
3256 3256 * If an address other than in6addr_any is requested,
3257 3257 * we verify that it is a valid address for bind
3258 3258 * Note: Following code is in if-else-if form for
3259 3259 * readability compared to a condition check.
3260 3260 */
3261 3261 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3262 3262 /*
3263 3263 * (2) Bind to address of local UP interface
3264 3264 */
3265 3265 ire_refrele(src_ire);
3266 3266 return (IPVL_UNICAST_UP);
3267 3267 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3268 3268 /*
3269 3269 * (4) Bind to broadcast address
3270 3270 */
3271 3271 ire_refrele(src_ire);
3272 3272 if (allow_mcbc)
3273 3273 return (IPVL_BCAST);
3274 3274 else
3275 3275 return (IPVL_BAD);
3276 3276 } else if (CLASSD(src_addr)) {
3277 3277 /* (5) bind to multicast address. */
3278 3278 if (src_ire != NULL)
3279 3279 ire_refrele(src_ire);
3280 3280
3281 3281 if (allow_mcbc)
3282 3282 return (IPVL_MCAST);
3283 3283 else
3284 3284 return (IPVL_BAD);
3285 3285 } else {
3286 3286 ipif_t *ipif;
3287 3287
3288 3288 /*
3289 3289 * (3) Bind to address of local DOWN interface?
3290 3290 * (ipif_lookup_addr() looks up all interfaces
3291 3291 * but we do not get here for UP interfaces
3292 3292 * - case (2) above)
3293 3293 */
3294 3294 if (src_ire != NULL)
3295 3295 ire_refrele(src_ire);
3296 3296
3297 3297 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3298 3298 if (ipif == NULL)
3299 3299 return (IPVL_BAD);
3300 3300
3301 3301 /* Not a useful source? */
3302 3302 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3303 3303 ipif_refrele(ipif);
3304 3304 return (IPVL_BAD);
3305 3305 }
3306 3306 ipif_refrele(ipif);
3307 3307 return (IPVL_UNICAST_DOWN);
3308 3308 }
3309 3309 }
3310 3310
3311 3311 /*
3312 3312 * Insert in the bind fanout for IPv4 and IPv6.
3313 3313 * The caller should already have used ip_laddr_verify_v*() before calling
3314 3314 * this.
3315 3315 */
3316 3316 int
3317 3317 ip_laddr_fanout_insert(conn_t *connp)
3318 3318 {
3319 3319 int error;
3320 3320
3321 3321 /*
3322 3322 * Allow setting new policies. For example, disconnects result
3323 3323 * in us being called. As we would have set conn_policy_cached
3324 3324 * to B_TRUE before, we should set it to B_FALSE, so that policy
3325 3325 * can change after the disconnect.
3326 3326 */
3327 3327 connp->conn_policy_cached = B_FALSE;
3328 3328
3329 3329 error = ipcl_bind_insert(connp);
3330 3330 if (error != 0) {
3331 3331 if (connp->conn_anon_port) {
3332 3332 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3333 3333 connp->conn_mlp_type, connp->conn_proto,
3334 3334 ntohs(connp->conn_lport), B_FALSE);
3335 3335 }
3336 3336 connp->conn_mlp_type = mlptSingle;
3337 3337 }
3338 3338 return (error);
3339 3339 }
3340 3340
3341 3341 /*
3342 3342 * Verify that both the source and destination addresses are valid. If
3343 3343 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3344 3344 * i.e. have no route to it. Protocols like TCP want to verify destination
3345 3345 * reachability, while tunnels do not.
3346 3346 *
3347 3347 * Determine the route, the interface, and (optionally) the source address
3348 3348 * to use to reach a given destination.
3349 3349 * Note that we allow connect to broadcast and multicast addresses when
3350 3350 * IPDF_ALLOW_MCBC is set.
3351 3351 * first_hop and dst_addr are normally the same, but if source routing
3352 3352 * they will differ; in that case the first_hop is what we'll use for the
3353 3353 * routing lookup but the dce and label checks will be done on dst_addr,
3354 3354 *
3355 3355 * If uinfo is set, then we fill in the best available information
3356 3356 * we have for the destination. This is based on (in priority order) any
3357 3357 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3358 3358 * ill_mtu/ill_mc_mtu.
3359 3359 *
3360 3360 * Tsol note: If we have a source route then dst_addr != firsthop. But we
3361 3361 * always do the label check on dst_addr.
3362 3362 */
3363 3363 int
3364 3364 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3365 3365 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3366 3366 {
3367 3367 ire_t *ire = NULL;
3368 3368 int error = 0;
3369 3369 ipaddr_t setsrc; /* RTF_SETSRC */
3370 3370 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3371 3371 ip_stack_t *ipst = ixa->ixa_ipst;
3372 3372 dce_t *dce;
3373 3373 uint_t pmtu;
3374 3374 uint_t generation;
3375 3375 nce_t *nce;
3376 3376 ill_t *ill = NULL;
3377 3377 boolean_t multirt = B_FALSE;
3378 3378
3379 3379 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3380 3380
3381 3381 /*
3382 3382 * We never send to zero; the ULPs map it to the loopback address.
3383 3383 * We can't allow it since we use zero to mean unitialized in some
3384 3384 * places.
3385 3385 */
3386 3386 ASSERT(dst_addr != INADDR_ANY);
3387 3387
3388 3388 if (is_system_labeled()) {
3389 3389 ts_label_t *tsl = NULL;
3390 3390
3391 3391 error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3392 3392 mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3393 3393 if (error != 0)
3394 3394 return (error);
3395 3395 if (tsl != NULL) {
3396 3396 /* Update the label */
3397 3397 ip_xmit_attr_replace_tsl(ixa, tsl);
3398 3398 }
3399 3399 }
3400 3400
3401 3401 setsrc = INADDR_ANY;
3402 3402 /*
3403 3403 * Select a route; For IPMP interfaces, we would only select
3404 3404 * a "hidden" route (i.e., going through a specific under_ill)
3405 3405 * if ixa_ifindex has been specified.
3406 3406 */
3407 3407 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3408 3408 &generation, &setsrc, &error, &multirt);
3409 3409 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3410 3410 if (error != 0)
3411 3411 goto bad_addr;
3412 3412
3413 3413 /*
3414 3414 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3415 3415 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3416 3416 * Otherwise the destination needn't be reachable.
3417 3417 *
3418 3418 * If we match on a reject or black hole, then we've got a
3419 3419 * local failure. May as well fail out the connect() attempt,
3420 3420 * since it's never going to succeed.
3421 3421 */
3422 3422 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3423 3423 /*
3424 3424 * If we're verifying destination reachability, we always want
3425 3425 * to complain here.
3426 3426 *
3427 3427 * If we're not verifying destination reachability but the
3428 3428 * destination has a route, we still want to fail on the
3429 3429 * temporary address and broadcast address tests.
3430 3430 *
3431 3431 * In both cases do we let the code continue so some reasonable
3432 3432 * information is returned to the caller. That enables the
3433 3433 * caller to use (and even cache) the IRE. conn_ip_ouput will
3434 3434 * use the generation mismatch path to check for the unreachable
3435 3435 * case thereby avoiding any specific check in the main path.
3436 3436 */
3437 3437 ASSERT(generation == IRE_GENERATION_VERIFY);
3438 3438 if (flags & IPDF_VERIFY_DST) {
3439 3439 /*
3440 3440 * Set errno but continue to set up ixa_ire to be
3441 3441 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3442 3442 * That allows callers to use ip_output to get an
3443 3443 * ICMP error back.
3444 3444 */
3445 3445 if (!(ire->ire_type & IRE_HOST))
3446 3446 error = ENETUNREACH;
3447 3447 else
3448 3448 error = EHOSTUNREACH;
3449 3449 }
3450 3450 }
3451 3451
3452 3452 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3453 3453 !(flags & IPDF_ALLOW_MCBC)) {
3454 3454 ire_refrele(ire);
3455 3455 ire = ire_reject(ipst, B_FALSE);
3456 3456 generation = IRE_GENERATION_VERIFY;
3457 3457 error = ENETUNREACH;
3458 3458 }
3459 3459
3460 3460 /* Cache things */
3461 3461 if (ixa->ixa_ire != NULL)
3462 3462 ire_refrele_notr(ixa->ixa_ire);
3463 3463 #ifdef DEBUG
3464 3464 ire_refhold_notr(ire);
3465 3465 ire_refrele(ire);
3466 3466 #endif
3467 3467 ixa->ixa_ire = ire;
3468 3468 ixa->ixa_ire_generation = generation;
3469 3469
3470 3470 /*
3471 3471 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3472 3472 * since some callers will send a packet to conn_ip_output() even if
3473 3473 * there's an error.
3474 3474 */
3475 3475 if (flags & IPDF_UNIQUE_DCE) {
3476 3476 /* Fallback to the default dce if allocation fails */
3477 3477 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3478 3478 if (dce != NULL)
3479 3479 generation = dce->dce_generation;
3480 3480 else
3481 3481 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3482 3482 } else {
3483 3483 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3484 3484 }
3485 3485 ASSERT(dce != NULL);
3486 3486 if (ixa->ixa_dce != NULL)
3487 3487 dce_refrele_notr(ixa->ixa_dce);
3488 3488 #ifdef DEBUG
3489 3489 dce_refhold_notr(dce);
3490 3490 dce_refrele(dce);
3491 3491 #endif
3492 3492 ixa->ixa_dce = dce;
3493 3493 ixa->ixa_dce_generation = generation;
3494 3494
3495 3495 /*
3496 3496 * For multicast with multirt we have a flag passed back from
3497 3497 * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3498 3498 * possible multicast address.
3499 3499 * We also need a flag for multicast since we can't check
3500 3500 * whether RTF_MULTIRT is set in ixa_ire for multicast.
3501 3501 */
3502 3502 if (multirt) {
3503 3503 ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3504 3504 ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3505 3505 } else {
3506 3506 ixa->ixa_postfragfn = ire->ire_postfragfn;
3507 3507 ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3508 3508 }
3509 3509 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3510 3510 /* Get an nce to cache. */
3511 3511 nce = ire_to_nce(ire, firsthop, NULL);
3512 3512 if (nce == NULL) {
3513 3513 /* Allocation failure? */
3514 3514 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3515 3515 } else {
3516 3516 if (ixa->ixa_nce != NULL)
3517 3517 nce_refrele(ixa->ixa_nce);
3518 3518 ixa->ixa_nce = nce;
3519 3519 }
3520 3520 }
3521 3521
3522 3522 /*
3523 3523 * If the source address is a loopback address, the
3524 3524 * destination had best be local or multicast.
3525 3525 * If we are sending to an IRE_LOCAL using a loopback source then
3526 3526 * it had better be the same zoneid.
3527 3527 */
3528 3528 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3529 3529 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3530 3530 ire = NULL; /* Stored in ixa_ire */
3531 3531 error = EADDRNOTAVAIL;
3532 3532 goto bad_addr;
3533 3533 }
3534 3534 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3535 3535 ire = NULL; /* Stored in ixa_ire */
3536 3536 error = EADDRNOTAVAIL;
3537 3537 goto bad_addr;
3538 3538 }
3539 3539 }
3540 3540 if (ire->ire_type & IRE_BROADCAST) {
3541 3541 /*
3542 3542 * If the ULP didn't have a specified source, then we
3543 3543 * make sure we reselect the source when sending
3544 3544 * broadcasts out different interfaces.
3545 3545 */
3546 3546 if (flags & IPDF_SELECT_SRC)
3547 3547 ixa->ixa_flags |= IXAF_SET_SOURCE;
3548 3548 else
3549 3549 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3550 3550 }
3551 3551
3552 3552 /*
3553 3553 * Does the caller want us to pick a source address?
3554 3554 */
3555 3555 if (flags & IPDF_SELECT_SRC) {
3556 3556 ipaddr_t src_addr;
3557 3557
3558 3558 /*
3559 3559 * We use use ire_nexthop_ill to avoid the under ipmp
3560 3560 * interface for source address selection. Note that for ipmp
3561 3561 * probe packets, ixa_ifindex would have been specified, and
3562 3562 * the ip_select_route() invocation would have picked an ire
3563 3563 * will ire_ill pointing at an under interface.
3564 3564 */
3565 3565 ill = ire_nexthop_ill(ire);
3566 3566
3567 3567 /* If unreachable we have no ill but need some source */
3568 3568 if (ill == NULL) {
3569 3569 src_addr = htonl(INADDR_LOOPBACK);
3570 3570 /* Make sure we look for a better source address */
3571 3571 generation = SRC_GENERATION_VERIFY;
3572 3572 } else {
3573 3573 error = ip_select_source_v4(ill, setsrc, dst_addr,
3574 3574 ixa->ixa_multicast_ifaddr, zoneid,
3575 3575 ipst, &src_addr, &generation, NULL);
3576 3576 if (error != 0) {
3577 3577 ire = NULL; /* Stored in ixa_ire */
3578 3578 goto bad_addr;
3579 3579 }
3580 3580 }
3581 3581
3582 3582 /*
3583 3583 * We allow the source address to to down.
3584 3584 * However, we check that we don't use the loopback address
3585 3585 * as a source when sending out on the wire.
3586 3586 */
3587 3587 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3588 3588 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3589 3589 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3590 3590 ire = NULL; /* Stored in ixa_ire */
3591 3591 error = EADDRNOTAVAIL;
3592 3592 goto bad_addr;
3593 3593 }
3594 3594
3595 3595 *src_addrp = src_addr;
3596 3596 ixa->ixa_src_generation = generation;
3597 3597 }
3598 3598
3599 3599 /*
3600 3600 * Make sure we don't leave an unreachable ixa_nce in place
3601 3601 * since ip_select_route is used when we unplumb i.e., remove
3602 3602 * references on ixa_ire, ixa_nce, and ixa_dce.
3603 3603 */
3604 3604 nce = ixa->ixa_nce;
3605 3605 if (nce != NULL && nce->nce_is_condemned) {
3606 3606 nce_refrele(nce);
3607 3607 ixa->ixa_nce = NULL;
3608 3608 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3609 3609 }
3610 3610
3611 3611 /*
3612 3612 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3613 3613 * However, we can't do it for IPv4 multicast or broadcast.
3614 3614 */
3615 3615 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3616 3616 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3617 3617
3618 3618 /*
3619 3619 * Set initial value for fragmentation limit. Either conn_ip_output
3620 3620 * or ULP might updates it when there are routing changes.
3621 3621 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3622 3622 */
3623 3623 pmtu = ip_get_pmtu(ixa);
3624 3624 ixa->ixa_fragsize = pmtu;
3625 3625 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3626 3626 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3627 3627 ixa->ixa_pmtu = pmtu;
3628 3628
3629 3629 /*
3630 3630 * Extract information useful for some transports.
3631 3631 * First we look for DCE metrics. Then we take what we have in
3632 3632 * the metrics in the route, where the offlink is used if we have
3633 3633 * one.
3634 3634 */
3635 3635 if (uinfo != NULL) {
3636 3636 bzero(uinfo, sizeof (*uinfo));
3637 3637
3638 3638 if (dce->dce_flags & DCEF_UINFO)
3639 3639 *uinfo = dce->dce_uinfo;
3640 3640
3641 3641 rts_merge_metrics(uinfo, &ire->ire_metrics);
3642 3642
3643 3643 /* Allow ire_metrics to decrease the path MTU from above */
3644 3644 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3645 3645 uinfo->iulp_mtu = pmtu;
3646 3646
3647 3647 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3648 3648 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3649 3649 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3650 3650 }
3651 3651
3652 3652 if (ill != NULL)
3653 3653 ill_refrele(ill);
3654 3654
3655 3655 return (error);
3656 3656
3657 3657 bad_addr:
3658 3658 if (ire != NULL)
3659 3659 ire_refrele(ire);
3660 3660
3661 3661 if (ill != NULL)
3662 3662 ill_refrele(ill);
3663 3663
3664 3664 /*
3665 3665 * Make sure we don't leave an unreachable ixa_nce in place
3666 3666 * since ip_select_route is used when we unplumb i.e., remove
3667 3667 * references on ixa_ire, ixa_nce, and ixa_dce.
3668 3668 */
3669 3669 nce = ixa->ixa_nce;
3670 3670 if (nce != NULL && nce->nce_is_condemned) {
3671 3671 nce_refrele(nce);
3672 3672 ixa->ixa_nce = NULL;
3673 3673 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3674 3674 }
3675 3675
3676 3676 return (error);
3677 3677 }
3678 3678
3679 3679
3680 3680 /*
3681 3681 * Get the base MTU for the case when path MTU discovery is not used.
3682 3682 * Takes the MTU of the IRE into account.
3683 3683 */
3684 3684 uint_t
3685 3685 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3686 3686 {
3687 3687 uint_t mtu;
3688 3688 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3689 3689
3690 3690 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3691 3691 mtu = ill->ill_mc_mtu;
3692 3692 else
3693 3693 mtu = ill->ill_mtu;
3694 3694
3695 3695 if (iremtu != 0 && iremtu < mtu)
3696 3696 mtu = iremtu;
3697 3697
3698 3698 return (mtu);
3699 3699 }
3700 3700
3701 3701 /*
3702 3702 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3703 3703 * Assumes that ixa_ire, dce, and nce have already been set up.
3704 3704 *
3705 3705 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3706 3706 * We avoid path MTU discovery if it is disabled with ndd.
3707 3707 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3708 3708 *
3709 3709 * NOTE: We also used to turn it off for source routed packets. That
3710 3710 * is no longer required since the dce is per final destination.
3711 3711 */
3712 3712 uint_t
3713 3713 ip_get_pmtu(ip_xmit_attr_t *ixa)
3714 3714 {
3715 3715 ip_stack_t *ipst = ixa->ixa_ipst;
3716 3716 dce_t *dce;
3717 3717 nce_t *nce;
3718 3718 ire_t *ire;
3719 3719 uint_t pmtu;
3720 3720
3721 3721 ire = ixa->ixa_ire;
3722 3722 dce = ixa->ixa_dce;
3723 3723 nce = ixa->ixa_nce;
3724 3724
3725 3725 /*
3726 3726 * If path MTU discovery has been turned off by ndd, then we ignore
3727 3727 * any dce_pmtu and for IPv4 we will not set DF.
3728 3728 */
3729 3729 if (!ipst->ips_ip_path_mtu_discovery)
3730 3730 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3731 3731
3732 3732 pmtu = IP_MAXPACKET;
3733 3733 /*
3734 3734 * Decide whether whether IPv4 sets DF
3735 3735 * For IPv6 "no DF" means to use the 1280 mtu
3736 3736 */
3737 3737 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3738 3738 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3739 3739 } else {
3740 3740 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3741 3741 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3742 3742 pmtu = IPV6_MIN_MTU;
3743 3743 }
3744 3744
3745 3745 /* Check if the PMTU is to old before we use it */
3746 3746 if ((dce->dce_flags & DCEF_PMTU) &&
3747 3747 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3748 3748 ipst->ips_ip_pathmtu_interval) {
3749 3749 /*
3750 3750 * Older than 20 minutes. Drop the path MTU information.
3751 3751 */
3752 3752 mutex_enter(&dce->dce_lock);
3753 3753 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3754 3754 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3755 3755 mutex_exit(&dce->dce_lock);
3756 3756 dce_increment_generation(dce);
3757 3757 }
3758 3758
3759 3759 /* The metrics on the route can lower the path MTU */
3760 3760 if (ire->ire_metrics.iulp_mtu != 0 &&
3761 3761 ire->ire_metrics.iulp_mtu < pmtu)
3762 3762 pmtu = ire->ire_metrics.iulp_mtu;
3763 3763
3764 3764 /*
3765 3765 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3766 3766 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3767 3767 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3768 3768 */
3769 3769 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3770 3770 if (dce->dce_flags & DCEF_PMTU) {
3771 3771 if (dce->dce_pmtu < pmtu)
3772 3772 pmtu = dce->dce_pmtu;
3773 3773
3774 3774 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3775 3775 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3776 3776 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3777 3777 } else {
3778 3778 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3779 3779 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3780 3780 }
3781 3781 } else {
3782 3782 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3783 3783 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3784 3784 }
3785 3785 }
3786 3786
3787 3787 /*
3788 3788 * If we have an IRE_LOCAL we use the loopback mtu instead of
3789 3789 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3790 3790 * mtu as IRE_LOOPBACK.
3791 3791 */
3792 3792 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3793 3793 uint_t loopback_mtu;
3794 3794
3795 3795 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3796 3796 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3797 3797
3798 3798 if (loopback_mtu < pmtu)
3799 3799 pmtu = loopback_mtu;
3800 3800 } else if (nce != NULL) {
3801 3801 /*
3802 3802 * Make sure we don't exceed the interface MTU.
3803 3803 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3804 3804 * an ill. We'd use the above IP_MAXPACKET in that case just
3805 3805 * to tell the transport something larger than zero.
3806 3806 */
3807 3807 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3808 3808 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3809 3809 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3810 3810 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3811 3811 nce->nce_ill->ill_mc_mtu < pmtu) {
3812 3812 /*
3813 3813 * for interfaces in an IPMP group, the mtu of
3814 3814 * the nce_ill (under_ill) could be different
3815 3815 * from the mtu of the ncec_ill, so we take the
3816 3816 * min of the two.
3817 3817 */
3818 3818 pmtu = nce->nce_ill->ill_mc_mtu;
3819 3819 }
3820 3820 } else {
3821 3821 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3822 3822 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3823 3823 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3824 3824 nce->nce_ill->ill_mtu < pmtu) {
3825 3825 /*
3826 3826 * for interfaces in an IPMP group, the mtu of
3827 3827 * the nce_ill (under_ill) could be different
3828 3828 * from the mtu of the ncec_ill, so we take the
3829 3829 * min of the two.
3830 3830 */
3831 3831 pmtu = nce->nce_ill->ill_mtu;
3832 3832 }
3833 3833 }
3834 3834 }
3835 3835
3836 3836 /*
3837 3837 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3838 3838 * Only applies to IPv6.
3839 3839 */
3840 3840 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3841 3841 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3842 3842 switch (ixa->ixa_use_min_mtu) {
3843 3843 case IPV6_USE_MIN_MTU_MULTICAST:
3844 3844 if (ire->ire_type & IRE_MULTICAST)
3845 3845 pmtu = IPV6_MIN_MTU;
3846 3846 break;
3847 3847 case IPV6_USE_MIN_MTU_ALWAYS:
3848 3848 pmtu = IPV6_MIN_MTU;
3849 3849 break;
3850 3850 case IPV6_USE_MIN_MTU_NEVER:
|
↓ open down ↓ |
3814 lines elided |
↑ open up ↑ |
3851 3851 break;
3852 3852 }
3853 3853 } else {
3854 3854 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3855 3855 if (ire->ire_type & IRE_MULTICAST)
3856 3856 pmtu = IPV6_MIN_MTU;
3857 3857 }
3858 3858 }
3859 3859
3860 3860 /*
3861 - * After receiving an ICMPv6 "packet too big" message with a
3862 - * MTU < 1280, and for multirouted IPv6 packets, the IP layer
3863 - * will insert a 8-byte fragment header in every packet. We compensate
3864 - * for those cases by returning a smaller path MTU to the ULP.
3861 + * For multirouted IPv6 packets, the IP layer will insert a 8-byte
3862 + * fragment header in every packet. We compensate for those cases by
3863 + * returning a smaller path MTU to the ULP.
3865 3864 *
3866 3865 * In the case of CGTP then ip_output will add a fragment header.
3867 3866 * Make sure there is room for it by telling a smaller number
3868 3867 * to the transport.
3869 3868 *
3870 3869 * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3871 3870 * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3872 3871 * which is the size of the packets it can send.
3873 3872 */
3874 3873 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3875 - if ((dce->dce_flags & DCEF_TOO_SMALL_PMTU) ||
3876 - (ire->ire_flags & RTF_MULTIRT) ||
3874 + if ((ire->ire_flags & RTF_MULTIRT) ||
3877 3875 (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3878 3876 pmtu -= sizeof (ip6_frag_t);
3879 3877 ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3880 3878 }
3881 3879 }
3882 3880
3883 3881 return (pmtu);
3884 3882 }
3885 3883
3886 3884 /*
3887 3885 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3888 3886 * the final piece where we don't. Return a pointer to the first mblk in the
3889 3887 * result, and update the pointer to the next mblk to chew on. If anything
3890 3888 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3891 3889 * NULL pointer.
3892 3890 */
3893 3891 mblk_t *
3894 3892 ip_carve_mp(mblk_t **mpp, ssize_t len)
3895 3893 {
3896 3894 mblk_t *mp0;
3897 3895 mblk_t *mp1;
3898 3896 mblk_t *mp2;
3899 3897
3900 3898 if (!len || !mpp || !(mp0 = *mpp))
3901 3899 return (NULL);
3902 3900 /* If we aren't going to consume the first mblk, we need a dup. */
3903 3901 if (mp0->b_wptr - mp0->b_rptr > len) {
3904 3902 mp1 = dupb(mp0);
3905 3903 if (mp1) {
3906 3904 /* Partition the data between the two mblks. */
3907 3905 mp1->b_wptr = mp1->b_rptr + len;
3908 3906 mp0->b_rptr = mp1->b_wptr;
3909 3907 /*
3910 3908 * after adjustments if mblk not consumed is now
3911 3909 * unaligned, try to align it. If this fails free
3912 3910 * all messages and let upper layer recover.
3913 3911 */
3914 3912 if (!OK_32PTR(mp0->b_rptr)) {
3915 3913 if (!pullupmsg(mp0, -1)) {
3916 3914 freemsg(mp0);
3917 3915 freemsg(mp1);
3918 3916 *mpp = NULL;
3919 3917 return (NULL);
3920 3918 }
3921 3919 }
3922 3920 }
3923 3921 return (mp1);
3924 3922 }
3925 3923 /* Eat through as many mblks as we need to get len bytes. */
3926 3924 len -= mp0->b_wptr - mp0->b_rptr;
3927 3925 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3928 3926 if (mp2->b_wptr - mp2->b_rptr > len) {
3929 3927 /*
3930 3928 * We won't consume the entire last mblk. Like
3931 3929 * above, dup and partition it.
3932 3930 */
3933 3931 mp1->b_cont = dupb(mp2);
3934 3932 mp1 = mp1->b_cont;
3935 3933 if (!mp1) {
3936 3934 /*
3937 3935 * Trouble. Rather than go to a lot of
3938 3936 * trouble to clean up, we free the messages.
3939 3937 * This won't be any worse than losing it on
3940 3938 * the wire.
3941 3939 */
3942 3940 freemsg(mp0);
3943 3941 freemsg(mp2);
3944 3942 *mpp = NULL;
3945 3943 return (NULL);
3946 3944 }
3947 3945 mp1->b_wptr = mp1->b_rptr + len;
3948 3946 mp2->b_rptr = mp1->b_wptr;
3949 3947 /*
3950 3948 * after adjustments if mblk not consumed is now
3951 3949 * unaligned, try to align it. If this fails free
3952 3950 * all messages and let upper layer recover.
3953 3951 */
3954 3952 if (!OK_32PTR(mp2->b_rptr)) {
3955 3953 if (!pullupmsg(mp2, -1)) {
3956 3954 freemsg(mp0);
3957 3955 freemsg(mp2);
3958 3956 *mpp = NULL;
3959 3957 return (NULL);
3960 3958 }
3961 3959 }
3962 3960 *mpp = mp2;
3963 3961 return (mp0);
3964 3962 }
3965 3963 /* Decrement len by the amount we just got. */
3966 3964 len -= mp2->b_wptr - mp2->b_rptr;
3967 3965 }
3968 3966 /*
3969 3967 * len should be reduced to zero now. If not our caller has
3970 3968 * screwed up.
3971 3969 */
3972 3970 if (len) {
3973 3971 /* Shouldn't happen! */
3974 3972 freemsg(mp0);
3975 3973 *mpp = NULL;
3976 3974 return (NULL);
3977 3975 }
3978 3976 /*
3979 3977 * We consumed up to exactly the end of an mblk. Detach the part
3980 3978 * we are returning from the rest of the chain.
3981 3979 */
3982 3980 mp1->b_cont = NULL;
3983 3981 *mpp = mp2;
3984 3982 return (mp0);
3985 3983 }
3986 3984
3987 3985 /* The ill stream is being unplumbed. Called from ip_close */
3988 3986 int
3989 3987 ip_modclose(ill_t *ill)
3990 3988 {
3991 3989 boolean_t success;
3992 3990 ipsq_t *ipsq;
3993 3991 ipif_t *ipif;
3994 3992 queue_t *q = ill->ill_rq;
3995 3993 ip_stack_t *ipst = ill->ill_ipst;
3996 3994 int i;
3997 3995 arl_ill_common_t *ai = ill->ill_common;
3998 3996
3999 3997 /*
4000 3998 * The punlink prior to this may have initiated a capability
4001 3999 * negotiation. But ipsq_enter will block until that finishes or
4002 4000 * times out.
4003 4001 */
4004 4002 success = ipsq_enter(ill, B_FALSE, NEW_OP);
4005 4003
4006 4004 /*
4007 4005 * Open/close/push/pop is guaranteed to be single threaded
4008 4006 * per stream by STREAMS. FS guarantees that all references
4009 4007 * from top are gone before close is called. So there can't
4010 4008 * be another close thread that has set CONDEMNED on this ill.
4011 4009 * and cause ipsq_enter to return failure.
4012 4010 */
4013 4011 ASSERT(success);
4014 4012 ipsq = ill->ill_phyint->phyint_ipsq;
4015 4013
4016 4014 /*
4017 4015 * Mark it condemned. No new reference will be made to this ill.
4018 4016 * Lookup functions will return an error. Threads that try to
4019 4017 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4020 4018 * that the refcnt will drop down to zero.
4021 4019 */
4022 4020 mutex_enter(&ill->ill_lock);
4023 4021 ill->ill_state_flags |= ILL_CONDEMNED;
4024 4022 for (ipif = ill->ill_ipif; ipif != NULL;
4025 4023 ipif = ipif->ipif_next) {
4026 4024 ipif->ipif_state_flags |= IPIF_CONDEMNED;
4027 4025 }
4028 4026 /*
4029 4027 * Wake up anybody waiting to enter the ipsq. ipsq_enter
4030 4028 * returns error if ILL_CONDEMNED is set
4031 4029 */
4032 4030 cv_broadcast(&ill->ill_cv);
4033 4031 mutex_exit(&ill->ill_lock);
4034 4032
4035 4033 /*
4036 4034 * Send all the deferred DLPI messages downstream which came in
4037 4035 * during the small window right before ipsq_enter(). We do this
4038 4036 * without waiting for the ACKs because all the ACKs for M_PROTO
4039 4037 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4040 4038 */
4041 4039 ill_dlpi_send_deferred(ill);
4042 4040
4043 4041 /*
4044 4042 * Shut down fragmentation reassembly.
4045 4043 * ill_frag_timer won't start a timer again.
4046 4044 * Now cancel any existing timer
4047 4045 */
4048 4046 (void) untimeout(ill->ill_frag_timer_id);
4049 4047 (void) ill_frag_timeout(ill, 0);
4050 4048
4051 4049 /*
4052 4050 * Call ill_delete to bring down the ipifs, ilms and ill on
4053 4051 * this ill. Then wait for the refcnts to drop to zero.
4054 4052 * ill_is_freeable checks whether the ill is really quiescent.
4055 4053 * Then make sure that threads that are waiting to enter the
4056 4054 * ipsq have seen the error returned by ipsq_enter and have
4057 4055 * gone away. Then we call ill_delete_tail which does the
4058 4056 * DL_UNBIND_REQ with the driver and then qprocsoff.
4059 4057 */
4060 4058 ill_delete(ill);
4061 4059 mutex_enter(&ill->ill_lock);
4062 4060 while (!ill_is_freeable(ill))
4063 4061 cv_wait(&ill->ill_cv, &ill->ill_lock);
4064 4062
4065 4063 while (ill->ill_waiters)
4066 4064 cv_wait(&ill->ill_cv, &ill->ill_lock);
4067 4065
4068 4066 mutex_exit(&ill->ill_lock);
4069 4067
4070 4068 /*
4071 4069 * ill_delete_tail drops reference on ill_ipst, but we need to keep
4072 4070 * it held until the end of the function since the cleanup
4073 4071 * below needs to be able to use the ip_stack_t.
4074 4072 */
4075 4073 netstack_hold(ipst->ips_netstack);
4076 4074
4077 4075 /* qprocsoff is done via ill_delete_tail */
4078 4076 ill_delete_tail(ill);
4079 4077 /*
4080 4078 * synchronously wait for arp stream to unbind. After this, we
4081 4079 * cannot get any data packets up from the driver.
4082 4080 */
4083 4081 arp_unbind_complete(ill);
4084 4082 ASSERT(ill->ill_ipst == NULL);
4085 4083
4086 4084 /*
4087 4085 * Walk through all conns and qenable those that have queued data.
4088 4086 * Close synchronization needs this to
4089 4087 * be done to ensure that all upper layers blocked
4090 4088 * due to flow control to the closing device
4091 4089 * get unblocked.
4092 4090 */
4093 4091 ip1dbg(("ip_wsrv: walking\n"));
4094 4092 for (i = 0; i < TX_FANOUT_SIZE; i++) {
4095 4093 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4096 4094 }
4097 4095
4098 4096 /*
4099 4097 * ai can be null if this is an IPv6 ill, or if the IPv4
4100 4098 * stream is being torn down before ARP was plumbed (e.g.,
4101 4099 * /sbin/ifconfig plumbing a stream twice, and encountering
4102 4100 * an error
4103 4101 */
4104 4102 if (ai != NULL) {
4105 4103 ASSERT(!ill->ill_isv6);
4106 4104 mutex_enter(&ai->ai_lock);
4107 4105 ai->ai_ill = NULL;
4108 4106 if (ai->ai_arl == NULL) {
4109 4107 mutex_destroy(&ai->ai_lock);
4110 4108 kmem_free(ai, sizeof (*ai));
4111 4109 } else {
4112 4110 cv_signal(&ai->ai_ill_unplumb_done);
4113 4111 mutex_exit(&ai->ai_lock);
4114 4112 }
4115 4113 }
4116 4114
4117 4115 mutex_enter(&ipst->ips_ip_mi_lock);
4118 4116 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4119 4117 mutex_exit(&ipst->ips_ip_mi_lock);
4120 4118
4121 4119 /*
4122 4120 * credp could be null if the open didn't succeed and ip_modopen
4123 4121 * itself calls ip_close.
4124 4122 */
4125 4123 if (ill->ill_credp != NULL)
4126 4124 crfree(ill->ill_credp);
4127 4125
4128 4126 mutex_destroy(&ill->ill_saved_ire_lock);
4129 4127 mutex_destroy(&ill->ill_lock);
4130 4128 rw_destroy(&ill->ill_mcast_lock);
4131 4129 mutex_destroy(&ill->ill_mcast_serializer);
4132 4130 list_destroy(&ill->ill_nce);
4133 4131
4134 4132 /*
4135 4133 * Now we are done with the module close pieces that
4136 4134 * need the netstack_t.
4137 4135 */
4138 4136 netstack_rele(ipst->ips_netstack);
4139 4137
4140 4138 mi_close_free((IDP)ill);
4141 4139 q->q_ptr = WR(q)->q_ptr = NULL;
4142 4140
4143 4141 ipsq_exit(ipsq);
4144 4142
4145 4143 return (0);
4146 4144 }
4147 4145
4148 4146 /*
4149 4147 * This is called as part of close() for IP, UDP, ICMP, and RTS
4150 4148 * in order to quiesce the conn.
4151 4149 */
4152 4150 void
4153 4151 ip_quiesce_conn(conn_t *connp)
4154 4152 {
4155 4153 boolean_t drain_cleanup_reqd = B_FALSE;
4156 4154 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4157 4155 boolean_t ilg_cleanup_reqd = B_FALSE;
4158 4156 ip_stack_t *ipst;
4159 4157
4160 4158 ASSERT(!IPCL_IS_TCP(connp));
4161 4159 ipst = connp->conn_netstack->netstack_ip;
4162 4160
4163 4161 /*
4164 4162 * Mark the conn as closing, and this conn must not be
4165 4163 * inserted in future into any list. Eg. conn_drain_insert(),
4166 4164 * won't insert this conn into the conn_drain_list.
4167 4165 *
4168 4166 * conn_idl, and conn_ilg cannot get set henceforth.
4169 4167 */
4170 4168 mutex_enter(&connp->conn_lock);
4171 4169 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4172 4170 connp->conn_state_flags |= CONN_CLOSING;
4173 4171 if (connp->conn_idl != NULL)
4174 4172 drain_cleanup_reqd = B_TRUE;
4175 4173 if (connp->conn_oper_pending_ill != NULL)
4176 4174 conn_ioctl_cleanup_reqd = B_TRUE;
4177 4175 if (connp->conn_dhcpinit_ill != NULL) {
4178 4176 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4179 4177 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4180 4178 ill_set_inputfn(connp->conn_dhcpinit_ill);
4181 4179 connp->conn_dhcpinit_ill = NULL;
4182 4180 }
4183 4181 if (connp->conn_ilg != NULL)
4184 4182 ilg_cleanup_reqd = B_TRUE;
4185 4183 mutex_exit(&connp->conn_lock);
4186 4184
4187 4185 if (conn_ioctl_cleanup_reqd)
4188 4186 conn_ioctl_cleanup(connp);
4189 4187
4190 4188 if (is_system_labeled() && connp->conn_anon_port) {
4191 4189 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4192 4190 connp->conn_mlp_type, connp->conn_proto,
4193 4191 ntohs(connp->conn_lport), B_FALSE);
4194 4192 connp->conn_anon_port = 0;
4195 4193 }
4196 4194 connp->conn_mlp_type = mlptSingle;
4197 4195
4198 4196 /*
4199 4197 * Remove this conn from any fanout list it is on.
4200 4198 * and then wait for any threads currently operating
4201 4199 * on this endpoint to finish
4202 4200 */
4203 4201 ipcl_hash_remove(connp);
4204 4202
4205 4203 /*
4206 4204 * Remove this conn from the drain list, and do any other cleanup that
4207 4205 * may be required. (TCP conns are never flow controlled, and
4208 4206 * conn_idl will be NULL.)
4209 4207 */
4210 4208 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4211 4209 idl_t *idl = connp->conn_idl;
4212 4210
4213 4211 mutex_enter(&idl->idl_lock);
4214 4212 conn_drain(connp, B_TRUE);
4215 4213 mutex_exit(&idl->idl_lock);
4216 4214 }
4217 4215
4218 4216 if (connp == ipst->ips_ip_g_mrouter)
4219 4217 (void) ip_mrouter_done(ipst);
4220 4218
4221 4219 if (ilg_cleanup_reqd)
4222 4220 ilg_delete_all(connp);
4223 4221
4224 4222 /*
4225 4223 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4226 4224 * callers from write side can't be there now because close
4227 4225 * is in progress. The only other caller is ipcl_walk
4228 4226 * which checks for the condemned flag.
4229 4227 */
4230 4228 mutex_enter(&connp->conn_lock);
4231 4229 connp->conn_state_flags |= CONN_CONDEMNED;
4232 4230 while (connp->conn_ref != 1)
4233 4231 cv_wait(&connp->conn_cv, &connp->conn_lock);
4234 4232 connp->conn_state_flags |= CONN_QUIESCED;
4235 4233 mutex_exit(&connp->conn_lock);
4236 4234 }
4237 4235
4238 4236 /* ARGSUSED */
4239 4237 int
4240 4238 ip_close(queue_t *q, int flags)
4241 4239 {
4242 4240 conn_t *connp;
4243 4241
4244 4242 /*
4245 4243 * Call the appropriate delete routine depending on whether this is
4246 4244 * a module or device.
4247 4245 */
4248 4246 if (WR(q)->q_next != NULL) {
4249 4247 /* This is a module close */
4250 4248 return (ip_modclose((ill_t *)q->q_ptr));
4251 4249 }
4252 4250
4253 4251 connp = q->q_ptr;
4254 4252 ip_quiesce_conn(connp);
4255 4253
4256 4254 qprocsoff(q);
4257 4255
4258 4256 /*
4259 4257 * Now we are truly single threaded on this stream, and can
4260 4258 * delete the things hanging off the connp, and finally the connp.
4261 4259 * We removed this connp from the fanout list, it cannot be
4262 4260 * accessed thru the fanouts, and we already waited for the
4263 4261 * conn_ref to drop to 0. We are already in close, so
4264 4262 * there cannot be any other thread from the top. qprocsoff
4265 4263 * has completed, and service has completed or won't run in
4266 4264 * future.
4267 4265 */
4268 4266 ASSERT(connp->conn_ref == 1);
4269 4267
4270 4268 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4271 4269
4272 4270 connp->conn_ref--;
4273 4271 ipcl_conn_destroy(connp);
4274 4272
4275 4273 q->q_ptr = WR(q)->q_ptr = NULL;
4276 4274 return (0);
4277 4275 }
4278 4276
4279 4277 /*
4280 4278 * Wapper around putnext() so that ip_rts_request can merely use
4281 4279 * conn_recv.
4282 4280 */
4283 4281 /*ARGSUSED2*/
4284 4282 static void
4285 4283 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4286 4284 {
4287 4285 conn_t *connp = (conn_t *)arg1;
4288 4286
4289 4287 putnext(connp->conn_rq, mp);
4290 4288 }
4291 4289
4292 4290 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4293 4291 /* ARGSUSED */
4294 4292 static void
4295 4293 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4296 4294 {
4297 4295 freemsg(mp);
4298 4296 }
4299 4297
4300 4298 /*
4301 4299 * Called when the module is about to be unloaded
4302 4300 */
4303 4301 void
4304 4302 ip_ddi_destroy(void)
4305 4303 {
4306 4304 /* This needs to be called before destroying any transports. */
4307 4305 mutex_enter(&cpu_lock);
4308 4306 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4309 4307 mutex_exit(&cpu_lock);
4310 4308
4311 4309 tnet_fini();
4312 4310
4313 4311 icmp_ddi_g_destroy();
4314 4312 rts_ddi_g_destroy();
4315 4313 udp_ddi_g_destroy();
4316 4314 sctp_ddi_g_destroy();
4317 4315 tcp_ddi_g_destroy();
4318 4316 ilb_ddi_g_destroy();
4319 4317 dce_g_destroy();
4320 4318 ipsec_policy_g_destroy();
4321 4319 ipcl_g_destroy();
4322 4320 ip_net_g_destroy();
4323 4321 ip_ire_g_fini();
4324 4322 inet_minor_destroy(ip_minor_arena_sa);
4325 4323 #if defined(_LP64)
4326 4324 inet_minor_destroy(ip_minor_arena_la);
4327 4325 #endif
4328 4326
4329 4327 #ifdef DEBUG
4330 4328 list_destroy(&ip_thread_list);
4331 4329 rw_destroy(&ip_thread_rwlock);
4332 4330 tsd_destroy(&ip_thread_data);
4333 4331 #endif
4334 4332
4335 4333 netstack_unregister(NS_IP);
4336 4334 }
4337 4335
4338 4336 /*
4339 4337 * First step in cleanup.
4340 4338 */
4341 4339 /* ARGSUSED */
4342 4340 static void
4343 4341 ip_stack_shutdown(netstackid_t stackid, void *arg)
4344 4342 {
4345 4343 ip_stack_t *ipst = (ip_stack_t *)arg;
4346 4344 kt_did_t ktid;
4347 4345
4348 4346 #ifdef NS_DEBUG
4349 4347 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4350 4348 #endif
4351 4349
4352 4350 /*
4353 4351 * Perform cleanup for special interfaces (loopback and IPMP).
4354 4352 */
4355 4353 ip_interface_cleanup(ipst);
4356 4354
4357 4355 /*
4358 4356 * The *_hook_shutdown()s start the process of notifying any
4359 4357 * consumers that things are going away.... nothing is destroyed.
4360 4358 */
4361 4359 ipv4_hook_shutdown(ipst);
4362 4360 ipv6_hook_shutdown(ipst);
4363 4361 arp_hook_shutdown(ipst);
4364 4362
4365 4363 mutex_enter(&ipst->ips_capab_taskq_lock);
4366 4364 ktid = ipst->ips_capab_taskq_thread->t_did;
4367 4365 ipst->ips_capab_taskq_quit = B_TRUE;
4368 4366 cv_signal(&ipst->ips_capab_taskq_cv);
4369 4367 mutex_exit(&ipst->ips_capab_taskq_lock);
4370 4368
4371 4369 /*
4372 4370 * In rare occurrences, particularly on virtual hardware where CPUs can
4373 4371 * be de-scheduled, the thread that we just signaled will not run until
4374 4372 * after we have gotten through parts of ip_stack_fini. If that happens
4375 4373 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4376 4374 * from cv_wait which no longer exists.
4377 4375 */
4378 4376 thread_join(ktid);
4379 4377 }
4380 4378
4381 4379 /*
4382 4380 * Free the IP stack instance.
4383 4381 */
4384 4382 static void
4385 4383 ip_stack_fini(netstackid_t stackid, void *arg)
4386 4384 {
4387 4385 ip_stack_t *ipst = (ip_stack_t *)arg;
4388 4386 int ret;
4389 4387
4390 4388 #ifdef NS_DEBUG
4391 4389 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4392 4390 #endif
4393 4391 /*
4394 4392 * At this point, all of the notifications that the events and
4395 4393 * protocols are going away have been run, meaning that we can
4396 4394 * now set about starting to clean things up.
4397 4395 */
4398 4396 ipobs_fini(ipst);
4399 4397 ipv4_hook_destroy(ipst);
4400 4398 ipv6_hook_destroy(ipst);
4401 4399 arp_hook_destroy(ipst);
4402 4400 ip_net_destroy(ipst);
4403 4401
4404 4402 ipmp_destroy(ipst);
4405 4403
4406 4404 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4407 4405 ipst->ips_ip_mibkp = NULL;
4408 4406 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4409 4407 ipst->ips_icmp_mibkp = NULL;
4410 4408 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4411 4409 ipst->ips_ip_kstat = NULL;
4412 4410 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4413 4411 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4414 4412 ipst->ips_ip6_kstat = NULL;
4415 4413 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4416 4414
4417 4415 kmem_free(ipst->ips_propinfo_tbl,
4418 4416 ip_propinfo_count * sizeof (mod_prop_info_t));
4419 4417 ipst->ips_propinfo_tbl = NULL;
4420 4418
4421 4419 dce_stack_destroy(ipst);
4422 4420 ip_mrouter_stack_destroy(ipst);
4423 4421
4424 4422 /*
4425 4423 * Quiesce all of our timers. Note we set the quiesce flags before we
4426 4424 * call untimeout. The slowtimers may actually kick off another instance
4427 4425 * of the non-slow timers.
4428 4426 */
4429 4427 mutex_enter(&ipst->ips_igmp_timer_lock);
4430 4428 ipst->ips_igmp_timer_quiesce = B_TRUE;
4431 4429 mutex_exit(&ipst->ips_igmp_timer_lock);
4432 4430
4433 4431 mutex_enter(&ipst->ips_mld_timer_lock);
4434 4432 ipst->ips_mld_timer_quiesce = B_TRUE;
4435 4433 mutex_exit(&ipst->ips_mld_timer_lock);
4436 4434
4437 4435 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4438 4436 ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4439 4437 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4440 4438
4441 4439 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4442 4440 ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4443 4441 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4444 4442
4445 4443 ret = untimeout(ipst->ips_igmp_timeout_id);
4446 4444 if (ret == -1) {
4447 4445 ASSERT(ipst->ips_igmp_timeout_id == 0);
4448 4446 } else {
4449 4447 ASSERT(ipst->ips_igmp_timeout_id != 0);
4450 4448 ipst->ips_igmp_timeout_id = 0;
4451 4449 }
4452 4450 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4453 4451 if (ret == -1) {
4454 4452 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4455 4453 } else {
4456 4454 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4457 4455 ipst->ips_igmp_slowtimeout_id = 0;
4458 4456 }
4459 4457 ret = untimeout(ipst->ips_mld_timeout_id);
4460 4458 if (ret == -1) {
4461 4459 ASSERT(ipst->ips_mld_timeout_id == 0);
4462 4460 } else {
4463 4461 ASSERT(ipst->ips_mld_timeout_id != 0);
4464 4462 ipst->ips_mld_timeout_id = 0;
4465 4463 }
4466 4464 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4467 4465 if (ret == -1) {
4468 4466 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4469 4467 } else {
4470 4468 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4471 4469 ipst->ips_mld_slowtimeout_id = 0;
4472 4470 }
4473 4471
4474 4472 ip_ire_fini(ipst);
4475 4473 ip6_asp_free(ipst);
4476 4474 conn_drain_fini(ipst);
4477 4475 ipcl_destroy(ipst);
4478 4476
4479 4477 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4480 4478 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4481 4479 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4482 4480 ipst->ips_ndp4 = NULL;
4483 4481 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4484 4482 ipst->ips_ndp6 = NULL;
4485 4483
4486 4484 if (ipst->ips_loopback_ksp != NULL) {
4487 4485 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4488 4486 ipst->ips_loopback_ksp = NULL;
4489 4487 }
4490 4488
4491 4489 mutex_destroy(&ipst->ips_capab_taskq_lock);
4492 4490 cv_destroy(&ipst->ips_capab_taskq_cv);
4493 4491
4494 4492 rw_destroy(&ipst->ips_srcid_lock);
4495 4493
4496 4494 mutex_destroy(&ipst->ips_ip_mi_lock);
4497 4495 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4498 4496
4499 4497 mutex_destroy(&ipst->ips_igmp_timer_lock);
4500 4498 mutex_destroy(&ipst->ips_mld_timer_lock);
4501 4499 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4502 4500 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4503 4501 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4504 4502 rw_destroy(&ipst->ips_ill_g_lock);
4505 4503
4506 4504 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4507 4505 ipst->ips_phyint_g_list = NULL;
4508 4506 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4509 4507 ipst->ips_ill_g_heads = NULL;
4510 4508
4511 4509 ldi_ident_release(ipst->ips_ldi_ident);
4512 4510 kmem_free(ipst, sizeof (*ipst));
4513 4511 }
4514 4512
4515 4513 /*
4516 4514 * This function is called from the TSD destructor, and is used to debug
4517 4515 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4518 4516 * details.
4519 4517 */
4520 4518 static void
4521 4519 ip_thread_exit(void *phash)
4522 4520 {
4523 4521 th_hash_t *thh = phash;
4524 4522
4525 4523 rw_enter(&ip_thread_rwlock, RW_WRITER);
4526 4524 list_remove(&ip_thread_list, thh);
4527 4525 rw_exit(&ip_thread_rwlock);
4528 4526 mod_hash_destroy_hash(thh->thh_hash);
4529 4527 kmem_free(thh, sizeof (*thh));
4530 4528 }
4531 4529
4532 4530 /*
4533 4531 * Called when the IP kernel module is loaded into the kernel
4534 4532 */
4535 4533 void
4536 4534 ip_ddi_init(void)
4537 4535 {
4538 4536 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4539 4537
4540 4538 /*
4541 4539 * For IP and TCP the minor numbers should start from 2 since we have 4
4542 4540 * initial devices: ip, ip6, tcp, tcp6.
4543 4541 */
4544 4542 /*
4545 4543 * If this is a 64-bit kernel, then create two separate arenas -
4546 4544 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4547 4545 * other for socket apps in the range 2^^18 through 2^^32-1.
4548 4546 */
4549 4547 ip_minor_arena_la = NULL;
4550 4548 ip_minor_arena_sa = NULL;
4551 4549 #if defined(_LP64)
4552 4550 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4553 4551 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4554 4552 cmn_err(CE_PANIC,
4555 4553 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4556 4554 }
4557 4555 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4558 4556 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4559 4557 cmn_err(CE_PANIC,
4560 4558 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4561 4559 }
4562 4560 #else
4563 4561 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4564 4562 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4565 4563 cmn_err(CE_PANIC,
4566 4564 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4567 4565 }
4568 4566 #endif
4569 4567 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4570 4568
4571 4569 ipcl_g_init();
4572 4570 ip_ire_g_init();
4573 4571 ip_net_g_init();
4574 4572
4575 4573 #ifdef DEBUG
4576 4574 tsd_create(&ip_thread_data, ip_thread_exit);
4577 4575 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4578 4576 list_create(&ip_thread_list, sizeof (th_hash_t),
4579 4577 offsetof(th_hash_t, thh_link));
4580 4578 #endif
4581 4579 ipsec_policy_g_init();
4582 4580 tcp_ddi_g_init();
4583 4581 sctp_ddi_g_init();
4584 4582 dce_g_init();
4585 4583
4586 4584 /*
4587 4585 * We want to be informed each time a stack is created or
4588 4586 * destroyed in the kernel, so we can maintain the
4589 4587 * set of udp_stack_t's.
4590 4588 */
4591 4589 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4592 4590 ip_stack_fini);
4593 4591
4594 4592 tnet_init();
4595 4593
4596 4594 udp_ddi_g_init();
4597 4595 rts_ddi_g_init();
4598 4596 icmp_ddi_g_init();
4599 4597 ilb_ddi_g_init();
4600 4598
4601 4599 /* This needs to be called after all transports are initialized. */
4602 4600 mutex_enter(&cpu_lock);
4603 4601 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4604 4602 mutex_exit(&cpu_lock);
4605 4603 }
4606 4604
4607 4605 /*
4608 4606 * Initialize the IP stack instance.
4609 4607 */
4610 4608 static void *
4611 4609 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4612 4610 {
4613 4611 ip_stack_t *ipst;
4614 4612 size_t arrsz;
4615 4613 major_t major;
4616 4614
4617 4615 #ifdef NS_DEBUG
4618 4616 printf("ip_stack_init(stack %d)\n", stackid);
4619 4617 #endif
4620 4618
4621 4619 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4622 4620 ipst->ips_netstack = ns;
4623 4621
4624 4622 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4625 4623 KM_SLEEP);
4626 4624 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4627 4625 KM_SLEEP);
4628 4626 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4629 4627 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4630 4628 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4631 4629 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4632 4630
4633 4631 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4634 4632 ipst->ips_igmp_deferred_next = INFINITY;
4635 4633 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4636 4634 ipst->ips_mld_deferred_next = INFINITY;
4637 4635 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4638 4636 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4639 4637 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4640 4638 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4641 4639 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4642 4640 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4643 4641
4644 4642 ipcl_init(ipst);
4645 4643 ip_ire_init(ipst);
4646 4644 ip6_asp_init(ipst);
4647 4645 ipif_init(ipst);
4648 4646 conn_drain_init(ipst);
4649 4647 ip_mrouter_stack_init(ipst);
4650 4648 dce_stack_init(ipst);
4651 4649
4652 4650 ipst->ips_ip_multirt_log_interval = 1000;
4653 4651
4654 4652 ipst->ips_ill_index = 1;
4655 4653
4656 4654 ipst->ips_saved_ip_forwarding = -1;
4657 4655 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4658 4656
4659 4657 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4660 4658 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4661 4659 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4662 4660
4663 4661 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4664 4662 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4665 4663 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4666 4664 ipst->ips_ip6_kstat =
4667 4665 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4668 4666
4669 4667 ipst->ips_ip_src_id = 1;
4670 4668 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4671 4669
4672 4670 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4673 4671
4674 4672 ip_net_init(ipst, ns);
4675 4673 ipv4_hook_init(ipst);
4676 4674 ipv6_hook_init(ipst);
4677 4675 arp_hook_init(ipst);
4678 4676 ipmp_init(ipst);
4679 4677 ipobs_init(ipst);
4680 4678
4681 4679 /*
4682 4680 * Create the taskq dispatcher thread and initialize related stuff.
4683 4681 */
4684 4682 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4685 4683 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4686 4684 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4687 4685 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4688 4686
4689 4687 major = mod_name_to_major(INET_NAME);
4690 4688 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4691 4689 return (ipst);
4692 4690 }
4693 4691
4694 4692 /*
4695 4693 * Allocate and initialize a DLPI template of the specified length. (May be
4696 4694 * called as writer.)
4697 4695 */
4698 4696 mblk_t *
4699 4697 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4700 4698 {
4701 4699 mblk_t *mp;
4702 4700
4703 4701 mp = allocb(len, BPRI_MED);
4704 4702 if (!mp)
4705 4703 return (NULL);
4706 4704
4707 4705 /*
4708 4706 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4709 4707 * of which we don't seem to use) are sent with M_PCPROTO, and
4710 4708 * that other DLPI are M_PROTO.
4711 4709 */
4712 4710 if (prim == DL_INFO_REQ) {
4713 4711 mp->b_datap->db_type = M_PCPROTO;
4714 4712 } else {
4715 4713 mp->b_datap->db_type = M_PROTO;
4716 4714 }
4717 4715
4718 4716 mp->b_wptr = mp->b_rptr + len;
4719 4717 bzero(mp->b_rptr, len);
4720 4718 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4721 4719 return (mp);
4722 4720 }
4723 4721
4724 4722 /*
4725 4723 * Allocate and initialize a DLPI notification. (May be called as writer.)
4726 4724 */
4727 4725 mblk_t *
4728 4726 ip_dlnotify_alloc(uint_t notification, uint_t data)
4729 4727 {
4730 4728 dl_notify_ind_t *notifyp;
4731 4729 mblk_t *mp;
4732 4730
4733 4731 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4734 4732 return (NULL);
4735 4733
4736 4734 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4737 4735 notifyp->dl_notification = notification;
4738 4736 notifyp->dl_data = data;
4739 4737 return (mp);
4740 4738 }
4741 4739
4742 4740 mblk_t *
4743 4741 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4744 4742 {
4745 4743 dl_notify_ind_t *notifyp;
4746 4744 mblk_t *mp;
4747 4745
4748 4746 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4749 4747 return (NULL);
4750 4748
4751 4749 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4752 4750 notifyp->dl_notification = notification;
4753 4751 notifyp->dl_data1 = data1;
4754 4752 notifyp->dl_data2 = data2;
4755 4753 return (mp);
4756 4754 }
4757 4755
4758 4756 /*
4759 4757 * Debug formatting routine. Returns a character string representation of the
4760 4758 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4761 4759 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4762 4760 *
4763 4761 * Once the ndd table-printing interfaces are removed, this can be changed to
4764 4762 * standard dotted-decimal form.
4765 4763 */
4766 4764 char *
4767 4765 ip_dot_addr(ipaddr_t addr, char *buf)
4768 4766 {
4769 4767 uint8_t *ap = (uint8_t *)&addr;
4770 4768
4771 4769 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4772 4770 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4773 4771 return (buf);
4774 4772 }
4775 4773
4776 4774 /*
4777 4775 * Write the given MAC address as a printable string in the usual colon-
4778 4776 * separated format.
4779 4777 */
4780 4778 const char *
4781 4779 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4782 4780 {
4783 4781 char *bp;
4784 4782
4785 4783 if (alen == 0 || buflen < 4)
4786 4784 return ("?");
4787 4785 bp = buf;
4788 4786 for (;;) {
4789 4787 /*
4790 4788 * If there are more MAC address bytes available, but we won't
4791 4789 * have any room to print them, then add "..." to the string
4792 4790 * instead. See below for the 'magic number' explanation.
4793 4791 */
4794 4792 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4795 4793 (void) strcpy(bp, "...");
4796 4794 break;
4797 4795 }
4798 4796 (void) sprintf(bp, "%02x", *addr++);
4799 4797 bp += 2;
4800 4798 if (--alen == 0)
4801 4799 break;
4802 4800 *bp++ = ':';
4803 4801 buflen -= 3;
4804 4802 /*
4805 4803 * At this point, based on the first 'if' statement above,
4806 4804 * either alen == 1 and buflen >= 3, or alen > 1 and
4807 4805 * buflen >= 4. The first case leaves room for the final "xx"
4808 4806 * number and trailing NUL byte. The second leaves room for at
4809 4807 * least "...". Thus the apparently 'magic' numbers chosen for
4810 4808 * that statement.
4811 4809 */
4812 4810 }
4813 4811 return (buf);
4814 4812 }
4815 4813
4816 4814 /*
4817 4815 * Called when it is conceptually a ULP that would sent the packet
4818 4816 * e.g., port unreachable and protocol unreachable. Check that the packet
4819 4817 * would have passed the IPsec global policy before sending the error.
4820 4818 *
4821 4819 * Send an ICMP error after patching up the packet appropriately.
4822 4820 * Uses ip_drop_input and bumps the appropriate MIB.
4823 4821 */
4824 4822 void
4825 4823 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4826 4824 ip_recv_attr_t *ira)
4827 4825 {
4828 4826 ipha_t *ipha;
4829 4827 boolean_t secure;
4830 4828 ill_t *ill = ira->ira_ill;
4831 4829 ip_stack_t *ipst = ill->ill_ipst;
4832 4830 netstack_t *ns = ipst->ips_netstack;
4833 4831 ipsec_stack_t *ipss = ns->netstack_ipsec;
4834 4832
4835 4833 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4836 4834
4837 4835 /*
4838 4836 * We are generating an icmp error for some inbound packet.
4839 4837 * Called from all ip_fanout_(udp, tcp, proto) functions.
4840 4838 * Before we generate an error, check with global policy
4841 4839 * to see whether this is allowed to enter the system. As
4842 4840 * there is no "conn", we are checking with global policy.
4843 4841 */
4844 4842 ipha = (ipha_t *)mp->b_rptr;
4845 4843 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4846 4844 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4847 4845 if (mp == NULL)
4848 4846 return;
4849 4847 }
4850 4848
4851 4849 /* We never send errors for protocols that we do implement */
4852 4850 if (ira->ira_protocol == IPPROTO_ICMP ||
4853 4851 ira->ira_protocol == IPPROTO_IGMP) {
4854 4852 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4855 4853 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4856 4854 freemsg(mp);
4857 4855 return;
4858 4856 }
4859 4857 /*
4860 4858 * Have to correct checksum since
4861 4859 * the packet might have been
4862 4860 * fragmented and the reassembly code in ip_rput
4863 4861 * does not restore the IP checksum.
4864 4862 */
4865 4863 ipha->ipha_hdr_checksum = 0;
4866 4864 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4867 4865
4868 4866 switch (icmp_type) {
4869 4867 case ICMP_DEST_UNREACHABLE:
4870 4868 switch (icmp_code) {
4871 4869 case ICMP_PROTOCOL_UNREACHABLE:
4872 4870 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4873 4871 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4874 4872 break;
4875 4873 case ICMP_PORT_UNREACHABLE:
4876 4874 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4877 4875 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4878 4876 break;
4879 4877 }
4880 4878
4881 4879 icmp_unreachable(mp, icmp_code, ira);
4882 4880 break;
4883 4881 default:
4884 4882 #ifdef DEBUG
4885 4883 panic("ip_fanout_send_icmp_v4: wrong type");
4886 4884 /*NOTREACHED*/
4887 4885 #else
4888 4886 freemsg(mp);
4889 4887 break;
4890 4888 #endif
4891 4889 }
4892 4890 }
4893 4891
4894 4892 /*
4895 4893 * Used to send an ICMP error message when a packet is received for
4896 4894 * a protocol that is not supported. The mblk passed as argument
4897 4895 * is consumed by this function.
4898 4896 */
4899 4897 void
4900 4898 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4901 4899 {
4902 4900 ipha_t *ipha;
4903 4901
4904 4902 ipha = (ipha_t *)mp->b_rptr;
4905 4903 if (ira->ira_flags & IRAF_IS_IPV4) {
4906 4904 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4907 4905 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4908 4906 ICMP_PROTOCOL_UNREACHABLE, ira);
4909 4907 } else {
4910 4908 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4911 4909 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4912 4910 ICMP6_PARAMPROB_NEXTHEADER, ira);
4913 4911 }
4914 4912 }
4915 4913
4916 4914 /*
4917 4915 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4918 4916 * Handles IPv4 and IPv6.
4919 4917 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4920 4918 * Caller is responsible for dropping references to the conn.
4921 4919 */
4922 4920 void
4923 4921 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4924 4922 ip_recv_attr_t *ira)
4925 4923 {
4926 4924 ill_t *ill = ira->ira_ill;
4927 4925 ip_stack_t *ipst = ill->ill_ipst;
4928 4926 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4929 4927 boolean_t secure;
4930 4928 uint_t protocol = ira->ira_protocol;
4931 4929 iaflags_t iraflags = ira->ira_flags;
4932 4930 queue_t *rq;
4933 4931
4934 4932 secure = iraflags & IRAF_IPSEC_SECURE;
4935 4933
4936 4934 rq = connp->conn_rq;
4937 4935 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4938 4936 switch (protocol) {
4939 4937 case IPPROTO_ICMPV6:
4940 4938 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4941 4939 break;
4942 4940 case IPPROTO_ICMP:
4943 4941 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4944 4942 break;
4945 4943 default:
4946 4944 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4947 4945 break;
4948 4946 }
4949 4947 freemsg(mp);
4950 4948 return;
4951 4949 }
4952 4950
4953 4951 ASSERT(!(IPCL_IS_IPTUN(connp)));
4954 4952
4955 4953 if (((iraflags & IRAF_IS_IPV4) ?
4956 4954 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4957 4955 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4958 4956 secure) {
4959 4957 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4960 4958 ip6h, ira);
4961 4959 if (mp == NULL) {
4962 4960 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4963 4961 /* Note that mp is NULL */
4964 4962 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4965 4963 return;
4966 4964 }
4967 4965 }
4968 4966
4969 4967 if (iraflags & IRAF_ICMP_ERROR) {
4970 4968 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4971 4969 } else {
4972 4970 ill_t *rill = ira->ira_rill;
4973 4971
4974 4972 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4975 4973 ira->ira_ill = ira->ira_rill = NULL;
4976 4974 /* Send it upstream */
4977 4975 (connp->conn_recv)(connp, mp, NULL, ira);
4978 4976 ira->ira_ill = ill;
4979 4977 ira->ira_rill = rill;
4980 4978 }
4981 4979 }
4982 4980
4983 4981 /*
4984 4982 * Handle protocols with which IP is less intimate. There
4985 4983 * can be more than one stream bound to a particular
4986 4984 * protocol. When this is the case, normally each one gets a copy
4987 4985 * of any incoming packets.
4988 4986 *
4989 4987 * IPsec NOTE :
4990 4988 *
4991 4989 * Don't allow a secure packet going up a non-secure connection.
4992 4990 * We don't allow this because
4993 4991 *
4994 4992 * 1) Reply might go out in clear which will be dropped at
4995 4993 * the sending side.
4996 4994 * 2) If the reply goes out in clear it will give the
4997 4995 * adversary enough information for getting the key in
4998 4996 * most of the cases.
4999 4997 *
5000 4998 * Moreover getting a secure packet when we expect clear
5001 4999 * implies that SA's were added without checking for
5002 5000 * policy on both ends. This should not happen once ISAKMP
5003 5001 * is used to negotiate SAs as SAs will be added only after
5004 5002 * verifying the policy.
5005 5003 *
5006 5004 * Zones notes:
5007 5005 * Earlier in ip_input on a system with multiple shared-IP zones we
5008 5006 * duplicate the multicast and broadcast packets and send them up
5009 5007 * with each explicit zoneid that exists on that ill.
5010 5008 * This means that here we can match the zoneid with SO_ALLZONES being special.
5011 5009 */
5012 5010 void
5013 5011 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
5014 5012 {
5015 5013 mblk_t *mp1;
5016 5014 ipaddr_t laddr;
5017 5015 conn_t *connp, *first_connp, *next_connp;
5018 5016 connf_t *connfp;
5019 5017 ill_t *ill = ira->ira_ill;
5020 5018 ip_stack_t *ipst = ill->ill_ipst;
5021 5019
5022 5020 laddr = ipha->ipha_dst;
5023 5021
5024 5022 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
5025 5023 mutex_enter(&connfp->connf_lock);
5026 5024 connp = connfp->connf_head;
5027 5025 for (connp = connfp->connf_head; connp != NULL;
5028 5026 connp = connp->conn_next) {
5029 5027 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5030 5028 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5031 5029 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5032 5030 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
5033 5031 break;
5034 5032 }
5035 5033 }
5036 5034
5037 5035 if (connp == NULL) {
5038 5036 /*
5039 5037 * No one bound to these addresses. Is
5040 5038 * there a client that wants all
5041 5039 * unclaimed datagrams?
5042 5040 */
5043 5041 mutex_exit(&connfp->connf_lock);
5044 5042 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5045 5043 ICMP_PROTOCOL_UNREACHABLE, ira);
5046 5044 return;
5047 5045 }
5048 5046
5049 5047 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5050 5048
5051 5049 CONN_INC_REF(connp);
5052 5050 first_connp = connp;
5053 5051 connp = connp->conn_next;
5054 5052
5055 5053 for (;;) {
5056 5054 while (connp != NULL) {
5057 5055 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5058 5056 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5059 5057 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5060 5058 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5061 5059 ira, connp)))
5062 5060 break;
5063 5061 connp = connp->conn_next;
5064 5062 }
5065 5063
5066 5064 if (connp == NULL) {
5067 5065 /* No more interested clients */
5068 5066 connp = first_connp;
5069 5067 break;
5070 5068 }
5071 5069 if (((mp1 = dupmsg(mp)) == NULL) &&
5072 5070 ((mp1 = copymsg(mp)) == NULL)) {
5073 5071 /* Memory allocation failed */
5074 5072 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5075 5073 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5076 5074 connp = first_connp;
5077 5075 break;
5078 5076 }
5079 5077
5080 5078 CONN_INC_REF(connp);
5081 5079 mutex_exit(&connfp->connf_lock);
5082 5080
5083 5081 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5084 5082 ira);
5085 5083
5086 5084 mutex_enter(&connfp->connf_lock);
5087 5085 /* Follow the next pointer before releasing the conn. */
5088 5086 next_connp = connp->conn_next;
5089 5087 CONN_DEC_REF(connp);
5090 5088 connp = next_connp;
5091 5089 }
5092 5090
5093 5091 /* Last one. Send it upstream. */
5094 5092 mutex_exit(&connfp->connf_lock);
5095 5093
5096 5094 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5097 5095
5098 5096 CONN_DEC_REF(connp);
5099 5097 }
5100 5098
5101 5099 /*
5102 5100 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5103 5101 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
5104 5102 * is not consumed.
5105 5103 *
5106 5104 * One of three things can happen, all of which affect the passed-in mblk:
5107 5105 *
5108 5106 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
5109 5107 *
5110 5108 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5111 5109 * ESP packet, and is passed along to ESP for consumption. Return NULL.
5112 5110 *
5113 5111 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
5114 5112 */
5115 5113 mblk_t *
5116 5114 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5117 5115 {
5118 5116 int shift, plen, iph_len;
5119 5117 ipha_t *ipha;
5120 5118 udpha_t *udpha;
5121 5119 uint32_t *spi;
5122 5120 uint32_t esp_ports;
5123 5121 uint8_t *orptr;
5124 5122 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
5125 5123 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5126 5124
5127 5125 ipha = (ipha_t *)mp->b_rptr;
5128 5126 iph_len = ira->ira_ip_hdr_length;
5129 5127 plen = ira->ira_pktlen;
5130 5128
5131 5129 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5132 5130 /*
5133 5131 * Most likely a keepalive for the benefit of an intervening
5134 5132 * NAT. These aren't for us, per se, so drop it.
5135 5133 *
5136 5134 * RFC 3947/8 doesn't say for sure what to do for 2-3
5137 5135 * byte packets (keepalives are 1-byte), but we'll drop them
5138 5136 * also.
5139 5137 */
5140 5138 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5141 5139 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5142 5140 return (NULL);
5143 5141 }
5144 5142
5145 5143 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5146 5144 /* might as well pull it all up - it might be ESP. */
5147 5145 if (!pullupmsg(mp, -1)) {
5148 5146 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5149 5147 DROPPER(ipss, ipds_esp_nomem),
5150 5148 &ipss->ipsec_dropper);
5151 5149 return (NULL);
5152 5150 }
5153 5151
5154 5152 ipha = (ipha_t *)mp->b_rptr;
5155 5153 }
5156 5154 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5157 5155 if (*spi == 0) {
5158 5156 /* UDP packet - remove 0-spi. */
5159 5157 shift = sizeof (uint32_t);
5160 5158 } else {
5161 5159 /* ESP-in-UDP packet - reduce to ESP. */
5162 5160 ipha->ipha_protocol = IPPROTO_ESP;
5163 5161 shift = sizeof (udpha_t);
5164 5162 }
5165 5163
5166 5164 /* Fix IP header */
5167 5165 ira->ira_pktlen = (plen - shift);
5168 5166 ipha->ipha_length = htons(ira->ira_pktlen);
5169 5167 ipha->ipha_hdr_checksum = 0;
5170 5168
5171 5169 orptr = mp->b_rptr;
5172 5170 mp->b_rptr += shift;
5173 5171
5174 5172 udpha = (udpha_t *)(orptr + iph_len);
5175 5173 if (*spi == 0) {
5176 5174 ASSERT((uint8_t *)ipha == orptr);
5177 5175 udpha->uha_length = htons(plen - shift - iph_len);
5178 5176 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5179 5177 esp_ports = 0;
5180 5178 } else {
5181 5179 esp_ports = *((uint32_t *)udpha);
5182 5180 ASSERT(esp_ports != 0);
5183 5181 }
5184 5182 ovbcopy(orptr, orptr + shift, iph_len);
5185 5183 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5186 5184 ipha = (ipha_t *)(orptr + shift);
5187 5185
5188 5186 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5189 5187 ira->ira_esp_udp_ports = esp_ports;
5190 5188 ip_fanout_v4(mp, ipha, ira);
5191 5189 return (NULL);
5192 5190 }
5193 5191 return (mp);
5194 5192 }
5195 5193
5196 5194 /*
5197 5195 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5198 5196 * Handles IPv4 and IPv6.
5199 5197 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5200 5198 * Caller is responsible for dropping references to the conn.
5201 5199 */
5202 5200 void
5203 5201 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5204 5202 ip_recv_attr_t *ira)
5205 5203 {
5206 5204 ill_t *ill = ira->ira_ill;
5207 5205 ip_stack_t *ipst = ill->ill_ipst;
5208 5206 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5209 5207 boolean_t secure;
5210 5208 iaflags_t iraflags = ira->ira_flags;
5211 5209
5212 5210 secure = iraflags & IRAF_IPSEC_SECURE;
5213 5211
5214 5212 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5215 5213 !canputnext(connp->conn_rq)) {
5216 5214 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5217 5215 freemsg(mp);
5218 5216 return;
5219 5217 }
5220 5218
5221 5219 if (((iraflags & IRAF_IS_IPV4) ?
5222 5220 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5223 5221 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5224 5222 secure) {
5225 5223 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5226 5224 ip6h, ira);
5227 5225 if (mp == NULL) {
5228 5226 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5229 5227 /* Note that mp is NULL */
5230 5228 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5231 5229 return;
5232 5230 }
5233 5231 }
5234 5232
5235 5233 /*
5236 5234 * Since this code is not used for UDP unicast we don't need a NAT_T
5237 5235 * check. Only ip_fanout_v4 has that check.
5238 5236 */
5239 5237 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5240 5238 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5241 5239 } else {
5242 5240 ill_t *rill = ira->ira_rill;
5243 5241
5244 5242 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5245 5243 ira->ira_ill = ira->ira_rill = NULL;
5246 5244 /* Send it upstream */
5247 5245 (connp->conn_recv)(connp, mp, NULL, ira);
5248 5246 ira->ira_ill = ill;
5249 5247 ira->ira_rill = rill;
5250 5248 }
5251 5249 }
5252 5250
5253 5251 /*
5254 5252 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5255 5253 * (Unicast fanout is handled in ip_input_v4.)
5256 5254 *
5257 5255 * If SO_REUSEADDR is set all multicast and broadcast packets
5258 5256 * will be delivered to all conns bound to the same port.
5259 5257 *
5260 5258 * If there is at least one matching AF_INET receiver, then we will
5261 5259 * ignore any AF_INET6 receivers.
5262 5260 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5263 5261 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5264 5262 * packets.
5265 5263 *
5266 5264 * Zones notes:
5267 5265 * Earlier in ip_input on a system with multiple shared-IP zones we
5268 5266 * duplicate the multicast and broadcast packets and send them up
5269 5267 * with each explicit zoneid that exists on that ill.
5270 5268 * This means that here we can match the zoneid with SO_ALLZONES being special.
5271 5269 */
5272 5270 void
5273 5271 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5274 5272 ip_recv_attr_t *ira)
5275 5273 {
5276 5274 ipaddr_t laddr;
5277 5275 in6_addr_t v6faddr;
5278 5276 conn_t *connp;
5279 5277 connf_t *connfp;
5280 5278 ipaddr_t faddr;
5281 5279 ill_t *ill = ira->ira_ill;
5282 5280 ip_stack_t *ipst = ill->ill_ipst;
5283 5281
5284 5282 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5285 5283
5286 5284 laddr = ipha->ipha_dst;
5287 5285 faddr = ipha->ipha_src;
5288 5286
5289 5287 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5290 5288 mutex_enter(&connfp->connf_lock);
5291 5289 connp = connfp->connf_head;
5292 5290
5293 5291 /*
5294 5292 * If SO_REUSEADDR has been set on the first we send the
5295 5293 * packet to all clients that have joined the group and
5296 5294 * match the port.
5297 5295 */
5298 5296 while (connp != NULL) {
5299 5297 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5300 5298 conn_wantpacket(connp, ira, ipha) &&
5301 5299 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5302 5300 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5303 5301 break;
5304 5302 connp = connp->conn_next;
5305 5303 }
5306 5304
5307 5305 if (connp == NULL)
5308 5306 goto notfound;
5309 5307
5310 5308 CONN_INC_REF(connp);
5311 5309
5312 5310 if (connp->conn_reuseaddr) {
5313 5311 conn_t *first_connp = connp;
5314 5312 conn_t *next_connp;
5315 5313 mblk_t *mp1;
5316 5314
5317 5315 connp = connp->conn_next;
5318 5316 for (;;) {
5319 5317 while (connp != NULL) {
5320 5318 if (IPCL_UDP_MATCH(connp, lport, laddr,
5321 5319 fport, faddr) &&
5322 5320 conn_wantpacket(connp, ira, ipha) &&
5323 5321 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5324 5322 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5325 5323 ira, connp)))
5326 5324 break;
5327 5325 connp = connp->conn_next;
5328 5326 }
5329 5327 if (connp == NULL) {
5330 5328 /* No more interested clients */
5331 5329 connp = first_connp;
5332 5330 break;
5333 5331 }
5334 5332 if (((mp1 = dupmsg(mp)) == NULL) &&
5335 5333 ((mp1 = copymsg(mp)) == NULL)) {
5336 5334 /* Memory allocation failed */
5337 5335 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5338 5336 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5339 5337 connp = first_connp;
5340 5338 break;
5341 5339 }
5342 5340 CONN_INC_REF(connp);
5343 5341 mutex_exit(&connfp->connf_lock);
5344 5342
5345 5343 IP_STAT(ipst, ip_udp_fanmb);
5346 5344 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5347 5345 NULL, ira);
5348 5346 mutex_enter(&connfp->connf_lock);
5349 5347 /* Follow the next pointer before releasing the conn */
5350 5348 next_connp = connp->conn_next;
5351 5349 CONN_DEC_REF(connp);
5352 5350 connp = next_connp;
5353 5351 }
5354 5352 }
5355 5353
5356 5354 /* Last one. Send it upstream. */
5357 5355 mutex_exit(&connfp->connf_lock);
5358 5356 IP_STAT(ipst, ip_udp_fanmb);
5359 5357 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5360 5358 CONN_DEC_REF(connp);
5361 5359 return;
5362 5360
5363 5361 notfound:
5364 5362 mutex_exit(&connfp->connf_lock);
5365 5363 /*
5366 5364 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5367 5365 * have already been matched above, since they live in the IPv4
5368 5366 * fanout tables. This implies we only need to
5369 5367 * check for IPv6 in6addr_any endpoints here.
5370 5368 * Thus we compare using ipv6_all_zeros instead of the destination
5371 5369 * address, except for the multicast group membership lookup which
5372 5370 * uses the IPv4 destination.
5373 5371 */
5374 5372 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5375 5373 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5376 5374 mutex_enter(&connfp->connf_lock);
5377 5375 connp = connfp->connf_head;
5378 5376 /*
5379 5377 * IPv4 multicast packet being delivered to an AF_INET6
5380 5378 * in6addr_any endpoint.
5381 5379 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5382 5380 * and not conn_wantpacket_v6() since any multicast membership is
5383 5381 * for an IPv4-mapped multicast address.
5384 5382 */
5385 5383 while (connp != NULL) {
5386 5384 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5387 5385 fport, v6faddr) &&
5388 5386 conn_wantpacket(connp, ira, ipha) &&
5389 5387 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5390 5388 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5391 5389 break;
5392 5390 connp = connp->conn_next;
5393 5391 }
5394 5392
5395 5393 if (connp == NULL) {
5396 5394 /*
5397 5395 * No one bound to this port. Is
5398 5396 * there a client that wants all
5399 5397 * unclaimed datagrams?
5400 5398 */
5401 5399 mutex_exit(&connfp->connf_lock);
5402 5400
5403 5401 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5404 5402 NULL) {
5405 5403 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5406 5404 ip_fanout_proto_v4(mp, ipha, ira);
5407 5405 } else {
5408 5406 /*
5409 5407 * We used to attempt to send an icmp error here, but
5410 5408 * since this is known to be a multicast packet
5411 5409 * and we don't send icmp errors in response to
5412 5410 * multicast, just drop the packet and give up sooner.
5413 5411 */
5414 5412 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5415 5413 freemsg(mp);
5416 5414 }
5417 5415 return;
5418 5416 }
5419 5417 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5420 5418
5421 5419 /*
5422 5420 * If SO_REUSEADDR has been set on the first we send the
5423 5421 * packet to all clients that have joined the group and
5424 5422 * match the port.
5425 5423 */
5426 5424 if (connp->conn_reuseaddr) {
5427 5425 conn_t *first_connp = connp;
5428 5426 conn_t *next_connp;
5429 5427 mblk_t *mp1;
5430 5428
5431 5429 CONN_INC_REF(connp);
5432 5430 connp = connp->conn_next;
5433 5431 for (;;) {
5434 5432 while (connp != NULL) {
5435 5433 if (IPCL_UDP_MATCH_V6(connp, lport,
5436 5434 ipv6_all_zeros, fport, v6faddr) &&
5437 5435 conn_wantpacket(connp, ira, ipha) &&
5438 5436 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5439 5437 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5440 5438 ira, connp)))
5441 5439 break;
5442 5440 connp = connp->conn_next;
5443 5441 }
5444 5442 if (connp == NULL) {
5445 5443 /* No more interested clients */
5446 5444 connp = first_connp;
5447 5445 break;
5448 5446 }
5449 5447 if (((mp1 = dupmsg(mp)) == NULL) &&
5450 5448 ((mp1 = copymsg(mp)) == NULL)) {
5451 5449 /* Memory allocation failed */
5452 5450 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5453 5451 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5454 5452 connp = first_connp;
5455 5453 break;
5456 5454 }
5457 5455 CONN_INC_REF(connp);
5458 5456 mutex_exit(&connfp->connf_lock);
5459 5457
5460 5458 IP_STAT(ipst, ip_udp_fanmb);
5461 5459 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5462 5460 NULL, ira);
5463 5461 mutex_enter(&connfp->connf_lock);
5464 5462 /* Follow the next pointer before releasing the conn */
5465 5463 next_connp = connp->conn_next;
5466 5464 CONN_DEC_REF(connp);
5467 5465 connp = next_connp;
5468 5466 }
5469 5467 }
5470 5468
5471 5469 /* Last one. Send it upstream. */
5472 5470 mutex_exit(&connfp->connf_lock);
5473 5471 IP_STAT(ipst, ip_udp_fanmb);
5474 5472 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5475 5473 CONN_DEC_REF(connp);
5476 5474 }
5477 5475
5478 5476 /*
5479 5477 * Split an incoming packet's IPv4 options into the label and the other options.
5480 5478 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5481 5479 * clearing out any leftover label or options.
5482 5480 * Otherwise it just makes ipp point into the packet.
5483 5481 *
5484 5482 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5485 5483 */
5486 5484 int
5487 5485 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5488 5486 {
5489 5487 uchar_t *opt;
5490 5488 uint32_t totallen;
5491 5489 uint32_t optval;
5492 5490 uint32_t optlen;
5493 5491
5494 5492 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5495 5493 ipp->ipp_hoplimit = ipha->ipha_ttl;
5496 5494 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5497 5495 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5498 5496
5499 5497 /*
5500 5498 * Get length (in 4 byte octets) of IP header options.
5501 5499 */
5502 5500 totallen = ipha->ipha_version_and_hdr_length -
5503 5501 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5504 5502
5505 5503 if (totallen == 0) {
5506 5504 if (!allocate)
5507 5505 return (0);
5508 5506
5509 5507 /* Clear out anything from a previous packet */
5510 5508 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5511 5509 kmem_free(ipp->ipp_ipv4_options,
5512 5510 ipp->ipp_ipv4_options_len);
5513 5511 ipp->ipp_ipv4_options = NULL;
5514 5512 ipp->ipp_ipv4_options_len = 0;
5515 5513 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5516 5514 }
5517 5515 if (ipp->ipp_fields & IPPF_LABEL_V4) {
5518 5516 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5519 5517 ipp->ipp_label_v4 = NULL;
5520 5518 ipp->ipp_label_len_v4 = 0;
5521 5519 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5522 5520 }
5523 5521 return (0);
5524 5522 }
5525 5523
5526 5524 totallen <<= 2;
5527 5525 opt = (uchar_t *)&ipha[1];
5528 5526 if (!is_system_labeled()) {
5529 5527
5530 5528 copyall:
5531 5529 if (!allocate) {
5532 5530 if (totallen != 0) {
5533 5531 ipp->ipp_ipv4_options = opt;
5534 5532 ipp->ipp_ipv4_options_len = totallen;
5535 5533 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5536 5534 }
5537 5535 return (0);
5538 5536 }
5539 5537 /* Just copy all of options */
5540 5538 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5541 5539 if (totallen == ipp->ipp_ipv4_options_len) {
5542 5540 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5543 5541 return (0);
5544 5542 }
5545 5543 kmem_free(ipp->ipp_ipv4_options,
5546 5544 ipp->ipp_ipv4_options_len);
5547 5545 ipp->ipp_ipv4_options = NULL;
5548 5546 ipp->ipp_ipv4_options_len = 0;
5549 5547 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5550 5548 }
5551 5549 if (totallen == 0)
5552 5550 return (0);
5553 5551
5554 5552 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5555 5553 if (ipp->ipp_ipv4_options == NULL)
5556 5554 return (ENOMEM);
5557 5555 ipp->ipp_ipv4_options_len = totallen;
5558 5556 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5559 5557 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5560 5558 return (0);
5561 5559 }
5562 5560
5563 5561 if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5564 5562 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5565 5563 ipp->ipp_label_v4 = NULL;
5566 5564 ipp->ipp_label_len_v4 = 0;
5567 5565 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5568 5566 }
5569 5567
5570 5568 /*
5571 5569 * Search for CIPSO option.
5572 5570 * We assume CIPSO is first in options if it is present.
5573 5571 * If it isn't, then ipp_opt_ipv4_options will not include the options
5574 5572 * prior to the CIPSO option.
5575 5573 */
5576 5574 while (totallen != 0) {
5577 5575 switch (optval = opt[IPOPT_OPTVAL]) {
5578 5576 case IPOPT_EOL:
5579 5577 return (0);
5580 5578 case IPOPT_NOP:
5581 5579 optlen = 1;
5582 5580 break;
5583 5581 default:
5584 5582 if (totallen <= IPOPT_OLEN)
5585 5583 return (EINVAL);
5586 5584 optlen = opt[IPOPT_OLEN];
5587 5585 if (optlen < 2)
5588 5586 return (EINVAL);
5589 5587 }
5590 5588 if (optlen > totallen)
5591 5589 return (EINVAL);
5592 5590
5593 5591 switch (optval) {
5594 5592 case IPOPT_COMSEC:
5595 5593 if (!allocate) {
5596 5594 ipp->ipp_label_v4 = opt;
5597 5595 ipp->ipp_label_len_v4 = optlen;
5598 5596 ipp->ipp_fields |= IPPF_LABEL_V4;
5599 5597 } else {
5600 5598 ipp->ipp_label_v4 = kmem_alloc(optlen,
5601 5599 KM_NOSLEEP);
5602 5600 if (ipp->ipp_label_v4 == NULL)
5603 5601 return (ENOMEM);
5604 5602 ipp->ipp_label_len_v4 = optlen;
5605 5603 ipp->ipp_fields |= IPPF_LABEL_V4;
5606 5604 bcopy(opt, ipp->ipp_label_v4, optlen);
5607 5605 }
5608 5606 totallen -= optlen;
5609 5607 opt += optlen;
5610 5608
5611 5609 /* Skip padding bytes until we get to a multiple of 4 */
5612 5610 while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5613 5611 totallen--;
5614 5612 opt++;
5615 5613 }
5616 5614 /* Remaining as ipp_ipv4_options */
5617 5615 goto copyall;
5618 5616 }
5619 5617 totallen -= optlen;
5620 5618 opt += optlen;
5621 5619 }
5622 5620 /* No CIPSO found; return everything as ipp_ipv4_options */
5623 5621 totallen = ipha->ipha_version_and_hdr_length -
5624 5622 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5625 5623 totallen <<= 2;
5626 5624 opt = (uchar_t *)&ipha[1];
5627 5625 goto copyall;
5628 5626 }
5629 5627
5630 5628 /*
5631 5629 * Efficient versions of lookup for an IRE when we only
5632 5630 * match the address.
5633 5631 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5634 5632 * Does not handle multicast addresses.
5635 5633 */
5636 5634 uint_t
5637 5635 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5638 5636 {
5639 5637 ire_t *ire;
5640 5638 uint_t result;
5641 5639
5642 5640 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5643 5641 ASSERT(ire != NULL);
5644 5642 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5645 5643 result = IRE_NOROUTE;
5646 5644 else
5647 5645 result = ire->ire_type;
5648 5646 ire_refrele(ire);
5649 5647 return (result);
5650 5648 }
5651 5649
5652 5650 /*
5653 5651 * Efficient versions of lookup for an IRE when we only
5654 5652 * match the address.
5655 5653 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5656 5654 * Does not handle multicast addresses.
5657 5655 */
5658 5656 uint_t
5659 5657 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5660 5658 {
5661 5659 ire_t *ire;
5662 5660 uint_t result;
5663 5661
5664 5662 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5665 5663 ASSERT(ire != NULL);
5666 5664 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5667 5665 result = IRE_NOROUTE;
5668 5666 else
5669 5667 result = ire->ire_type;
5670 5668 ire_refrele(ire);
5671 5669 return (result);
5672 5670 }
5673 5671
5674 5672 /*
5675 5673 * Nobody should be sending
5676 5674 * packets up this stream
5677 5675 */
5678 5676 static void
5679 5677 ip_lrput(queue_t *q, mblk_t *mp)
5680 5678 {
5681 5679 switch (mp->b_datap->db_type) {
5682 5680 case M_FLUSH:
5683 5681 /* Turn around */
5684 5682 if (*mp->b_rptr & FLUSHW) {
5685 5683 *mp->b_rptr &= ~FLUSHR;
5686 5684 qreply(q, mp);
5687 5685 return;
5688 5686 }
5689 5687 break;
5690 5688 }
5691 5689 freemsg(mp);
5692 5690 }
5693 5691
5694 5692 /* Nobody should be sending packets down this stream */
5695 5693 /* ARGSUSED */
5696 5694 void
5697 5695 ip_lwput(queue_t *q, mblk_t *mp)
5698 5696 {
5699 5697 freemsg(mp);
5700 5698 }
5701 5699
5702 5700 /*
5703 5701 * Move the first hop in any source route to ipha_dst and remove that part of
5704 5702 * the source route. Called by other protocols. Errors in option formatting
5705 5703 * are ignored - will be handled by ip_output_options. Return the final
5706 5704 * destination (either ipha_dst or the last entry in a source route.)
5707 5705 */
5708 5706 ipaddr_t
5709 5707 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5710 5708 {
5711 5709 ipoptp_t opts;
5712 5710 uchar_t *opt;
5713 5711 uint8_t optval;
5714 5712 uint8_t optlen;
5715 5713 ipaddr_t dst;
5716 5714 int i;
5717 5715 ip_stack_t *ipst = ns->netstack_ip;
5718 5716
5719 5717 ip2dbg(("ip_massage_options\n"));
5720 5718 dst = ipha->ipha_dst;
5721 5719 for (optval = ipoptp_first(&opts, ipha);
5722 5720 optval != IPOPT_EOL;
5723 5721 optval = ipoptp_next(&opts)) {
5724 5722 opt = opts.ipoptp_cur;
5725 5723 switch (optval) {
5726 5724 uint8_t off;
5727 5725 case IPOPT_SSRR:
5728 5726 case IPOPT_LSRR:
5729 5727 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5730 5728 ip1dbg(("ip_massage_options: bad src route\n"));
5731 5729 break;
5732 5730 }
5733 5731 optlen = opts.ipoptp_len;
5734 5732 off = opt[IPOPT_OFFSET];
5735 5733 off--;
5736 5734 redo_srr:
5737 5735 if (optlen < IP_ADDR_LEN ||
5738 5736 off > optlen - IP_ADDR_LEN) {
5739 5737 /* End of source route */
5740 5738 ip1dbg(("ip_massage_options: end of SR\n"));
5741 5739 break;
5742 5740 }
5743 5741 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5744 5742 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5745 5743 ntohl(dst)));
5746 5744 /*
5747 5745 * Check if our address is present more than
5748 5746 * once as consecutive hops in source route.
5749 5747 * XXX verify per-interface ip_forwarding
5750 5748 * for source route?
5751 5749 */
5752 5750 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5753 5751 off += IP_ADDR_LEN;
5754 5752 goto redo_srr;
5755 5753 }
5756 5754 if (dst == htonl(INADDR_LOOPBACK)) {
5757 5755 ip1dbg(("ip_massage_options: loopback addr in "
5758 5756 "source route!\n"));
5759 5757 break;
5760 5758 }
5761 5759 /*
5762 5760 * Update ipha_dst to be the first hop and remove the
5763 5761 * first hop from the source route (by overwriting
5764 5762 * part of the option with NOP options).
5765 5763 */
5766 5764 ipha->ipha_dst = dst;
5767 5765 /* Put the last entry in dst */
5768 5766 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5769 5767 3;
5770 5768 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5771 5769
5772 5770 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5773 5771 ntohl(dst)));
5774 5772 /* Move down and overwrite */
5775 5773 opt[IP_ADDR_LEN] = opt[0];
5776 5774 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5777 5775 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5778 5776 for (i = 0; i < IP_ADDR_LEN; i++)
5779 5777 opt[i] = IPOPT_NOP;
5780 5778 break;
5781 5779 }
5782 5780 }
5783 5781 return (dst);
5784 5782 }
5785 5783
5786 5784 /*
5787 5785 * Return the network mask
5788 5786 * associated with the specified address.
5789 5787 */
5790 5788 ipaddr_t
5791 5789 ip_net_mask(ipaddr_t addr)
5792 5790 {
5793 5791 uchar_t *up = (uchar_t *)&addr;
5794 5792 ipaddr_t mask = 0;
5795 5793 uchar_t *maskp = (uchar_t *)&mask;
5796 5794
5797 5795 #if defined(__i386) || defined(__amd64)
5798 5796 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5799 5797 #endif
5800 5798 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5801 5799 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5802 5800 #endif
5803 5801 if (CLASSD(addr)) {
5804 5802 maskp[0] = 0xF0;
5805 5803 return (mask);
5806 5804 }
5807 5805
5808 5806 /* We assume Class E default netmask to be 32 */
5809 5807 if (CLASSE(addr))
5810 5808 return (0xffffffffU);
5811 5809
5812 5810 if (addr == 0)
5813 5811 return (0);
5814 5812 maskp[0] = 0xFF;
5815 5813 if ((up[0] & 0x80) == 0)
5816 5814 return (mask);
5817 5815
5818 5816 maskp[1] = 0xFF;
5819 5817 if ((up[0] & 0xC0) == 0x80)
5820 5818 return (mask);
5821 5819
5822 5820 maskp[2] = 0xFF;
5823 5821 if ((up[0] & 0xE0) == 0xC0)
5824 5822 return (mask);
5825 5823
5826 5824 /* Otherwise return no mask */
5827 5825 return ((ipaddr_t)0);
5828 5826 }
5829 5827
5830 5828 /* Name/Value Table Lookup Routine */
5831 5829 char *
5832 5830 ip_nv_lookup(nv_t *nv, int value)
5833 5831 {
5834 5832 if (!nv)
5835 5833 return (NULL);
5836 5834 for (; nv->nv_name; nv++) {
5837 5835 if (nv->nv_value == value)
5838 5836 return (nv->nv_name);
5839 5837 }
5840 5838 return ("unknown");
5841 5839 }
5842 5840
5843 5841 static int
5844 5842 ip_wait_for_info_ack(ill_t *ill)
5845 5843 {
5846 5844 int err;
5847 5845
5848 5846 mutex_enter(&ill->ill_lock);
5849 5847 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5850 5848 /*
5851 5849 * Return value of 0 indicates a pending signal.
5852 5850 */
5853 5851 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5854 5852 if (err == 0) {
5855 5853 mutex_exit(&ill->ill_lock);
5856 5854 return (EINTR);
5857 5855 }
5858 5856 }
5859 5857 mutex_exit(&ill->ill_lock);
5860 5858 /*
5861 5859 * ip_rput_other could have set an error in ill_error on
5862 5860 * receipt of M_ERROR.
5863 5861 */
5864 5862 return (ill->ill_error);
5865 5863 }
5866 5864
5867 5865 /*
5868 5866 * This is a module open, i.e. this is a control stream for access
5869 5867 * to a DLPI device. We allocate an ill_t as the instance data in
5870 5868 * this case.
5871 5869 */
5872 5870 static int
5873 5871 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5874 5872 {
5875 5873 ill_t *ill;
5876 5874 int err;
5877 5875 zoneid_t zoneid;
5878 5876 netstack_t *ns;
5879 5877 ip_stack_t *ipst;
5880 5878
5881 5879 /*
5882 5880 * Prevent unprivileged processes from pushing IP so that
5883 5881 * they can't send raw IP.
5884 5882 */
5885 5883 if (secpolicy_net_rawaccess(credp) != 0)
5886 5884 return (EPERM);
5887 5885
5888 5886 ns = netstack_find_by_cred(credp);
5889 5887 ASSERT(ns != NULL);
5890 5888 ipst = ns->netstack_ip;
5891 5889 ASSERT(ipst != NULL);
5892 5890
5893 5891 /*
5894 5892 * For exclusive stacks we set the zoneid to zero
5895 5893 * to make IP operate as if in the global zone.
5896 5894 */
5897 5895 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5898 5896 zoneid = GLOBAL_ZONEID;
5899 5897 else
5900 5898 zoneid = crgetzoneid(credp);
5901 5899
5902 5900 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5903 5901 q->q_ptr = WR(q)->q_ptr = ill;
5904 5902 ill->ill_ipst = ipst;
5905 5903 ill->ill_zoneid = zoneid;
5906 5904
5907 5905 /*
5908 5906 * ill_init initializes the ill fields and then sends down
5909 5907 * down a DL_INFO_REQ after calling qprocson.
5910 5908 */
5911 5909 err = ill_init(q, ill);
5912 5910
5913 5911 if (err != 0) {
5914 5912 mi_free(ill);
5915 5913 netstack_rele(ipst->ips_netstack);
5916 5914 q->q_ptr = NULL;
5917 5915 WR(q)->q_ptr = NULL;
5918 5916 return (err);
5919 5917 }
5920 5918
5921 5919 /*
5922 5920 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5923 5921 *
5924 5922 * ill_init initializes the ipsq marking this thread as
5925 5923 * writer
5926 5924 */
5927 5925 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5928 5926 err = ip_wait_for_info_ack(ill);
5929 5927 if (err == 0)
5930 5928 ill->ill_credp = credp;
5931 5929 else
5932 5930 goto fail;
5933 5931
5934 5932 crhold(credp);
5935 5933
5936 5934 mutex_enter(&ipst->ips_ip_mi_lock);
5937 5935 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5938 5936 sflag, credp);
5939 5937 mutex_exit(&ipst->ips_ip_mi_lock);
5940 5938 fail:
5941 5939 if (err) {
5942 5940 (void) ip_close(q, 0);
5943 5941 return (err);
5944 5942 }
5945 5943 return (0);
5946 5944 }
5947 5945
5948 5946 /* For /dev/ip aka AF_INET open */
5949 5947 int
5950 5948 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5951 5949 {
5952 5950 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5953 5951 }
5954 5952
5955 5953 /* For /dev/ip6 aka AF_INET6 open */
5956 5954 int
5957 5955 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5958 5956 {
5959 5957 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5960 5958 }
5961 5959
5962 5960 /* IP open routine. */
5963 5961 int
5964 5962 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5965 5963 boolean_t isv6)
5966 5964 {
5967 5965 conn_t *connp;
5968 5966 major_t maj;
5969 5967 zoneid_t zoneid;
5970 5968 netstack_t *ns;
5971 5969 ip_stack_t *ipst;
5972 5970
5973 5971 /* Allow reopen. */
5974 5972 if (q->q_ptr != NULL)
5975 5973 return (0);
5976 5974
5977 5975 if (sflag & MODOPEN) {
5978 5976 /* This is a module open */
5979 5977 return (ip_modopen(q, devp, flag, sflag, credp));
5980 5978 }
5981 5979
5982 5980 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5983 5981 /*
5984 5982 * Non streams based socket looking for a stream
5985 5983 * to access IP
5986 5984 */
5987 5985 return (ip_helper_stream_setup(q, devp, flag, sflag,
5988 5986 credp, isv6));
5989 5987 }
5990 5988
5991 5989 ns = netstack_find_by_cred(credp);
5992 5990 ASSERT(ns != NULL);
5993 5991 ipst = ns->netstack_ip;
5994 5992 ASSERT(ipst != NULL);
5995 5993
5996 5994 /*
5997 5995 * For exclusive stacks we set the zoneid to zero
5998 5996 * to make IP operate as if in the global zone.
5999 5997 */
6000 5998 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
6001 5999 zoneid = GLOBAL_ZONEID;
6002 6000 else
6003 6001 zoneid = crgetzoneid(credp);
6004 6002
6005 6003 /*
6006 6004 * We are opening as a device. This is an IP client stream, and we
6007 6005 * allocate an conn_t as the instance data.
6008 6006 */
6009 6007 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
6010 6008
6011 6009 /*
6012 6010 * ipcl_conn_create did a netstack_hold. Undo the hold that was
6013 6011 * done by netstack_find_by_cred()
6014 6012 */
6015 6013 netstack_rele(ipst->ips_netstack);
6016 6014
6017 6015 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
6018 6016 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
6019 6017 connp->conn_ixa->ixa_zoneid = zoneid;
6020 6018 connp->conn_zoneid = zoneid;
6021 6019
6022 6020 connp->conn_rq = q;
6023 6021 q->q_ptr = WR(q)->q_ptr = connp;
6024 6022
6025 6023 /* Minor tells us which /dev entry was opened */
6026 6024 if (isv6) {
6027 6025 connp->conn_family = AF_INET6;
6028 6026 connp->conn_ipversion = IPV6_VERSION;
6029 6027 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
6030 6028 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
6031 6029 } else {
6032 6030 connp->conn_family = AF_INET;
6033 6031 connp->conn_ipversion = IPV4_VERSION;
6034 6032 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6035 6033 }
6036 6034
6037 6035 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6038 6036 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6039 6037 connp->conn_minor_arena = ip_minor_arena_la;
6040 6038 } else {
6041 6039 /*
6042 6040 * Either minor numbers in the large arena were exhausted
6043 6041 * or a non socket application is doing the open.
6044 6042 * Try to allocate from the small arena.
6045 6043 */
6046 6044 if ((connp->conn_dev =
6047 6045 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6048 6046 /* CONN_DEC_REF takes care of netstack_rele() */
6049 6047 q->q_ptr = WR(q)->q_ptr = NULL;
6050 6048 CONN_DEC_REF(connp);
6051 6049 return (EBUSY);
6052 6050 }
6053 6051 connp->conn_minor_arena = ip_minor_arena_sa;
6054 6052 }
6055 6053
6056 6054 maj = getemajor(*devp);
6057 6055 *devp = makedevice(maj, (minor_t)connp->conn_dev);
6058 6056
6059 6057 /*
6060 6058 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6061 6059 */
6062 6060 connp->conn_cred = credp;
6063 6061 connp->conn_cpid = curproc->p_pid;
6064 6062 /* Cache things in ixa without an extra refhold */
6065 6063 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6066 6064 connp->conn_ixa->ixa_cred = connp->conn_cred;
6067 6065 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6068 6066 if (is_system_labeled())
6069 6067 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6070 6068
6071 6069 /*
6072 6070 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6073 6071 */
6074 6072 connp->conn_recv = ip_conn_input;
6075 6073 connp->conn_recvicmp = ip_conn_input_icmp;
6076 6074
6077 6075 crhold(connp->conn_cred);
6078 6076
6079 6077 /*
6080 6078 * If the caller has the process-wide flag set, then default to MAC
6081 6079 * exempt mode. This allows read-down to unlabeled hosts.
6082 6080 */
6083 6081 if (getpflags(NET_MAC_AWARE, credp) != 0)
6084 6082 connp->conn_mac_mode = CONN_MAC_AWARE;
6085 6083
6086 6084 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6087 6085
6088 6086 connp->conn_rq = q;
6089 6087 connp->conn_wq = WR(q);
6090 6088
6091 6089 /* Non-zero default values */
6092 6090 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6093 6091
6094 6092 /*
6095 6093 * Make the conn globally visible to walkers
6096 6094 */
6097 6095 ASSERT(connp->conn_ref == 1);
6098 6096 mutex_enter(&connp->conn_lock);
6099 6097 connp->conn_state_flags &= ~CONN_INCIPIENT;
6100 6098 mutex_exit(&connp->conn_lock);
6101 6099
6102 6100 qprocson(q);
6103 6101
6104 6102 return (0);
6105 6103 }
6106 6104
6107 6105 /*
6108 6106 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6109 6107 * all of them are copied to the conn_t. If the req is "zero", the policy is
6110 6108 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6111 6109 * fields.
6112 6110 * We keep only the latest setting of the policy and thus policy setting
6113 6111 * is not incremental/cumulative.
6114 6112 *
6115 6113 * Requests to set policies with multiple alternative actions will
6116 6114 * go through a different API.
6117 6115 */
6118 6116 int
6119 6117 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6120 6118 {
6121 6119 uint_t ah_req = 0;
6122 6120 uint_t esp_req = 0;
6123 6121 uint_t se_req = 0;
6124 6122 ipsec_act_t *actp = NULL;
6125 6123 uint_t nact;
6126 6124 ipsec_policy_head_t *ph;
6127 6125 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6128 6126 int error = 0;
6129 6127 netstack_t *ns = connp->conn_netstack;
6130 6128 ip_stack_t *ipst = ns->netstack_ip;
6131 6129 ipsec_stack_t *ipss = ns->netstack_ipsec;
6132 6130
6133 6131 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6134 6132
6135 6133 /*
6136 6134 * The IP_SEC_OPT option does not allow variable length parameters,
6137 6135 * hence a request cannot be NULL.
6138 6136 */
6139 6137 if (req == NULL)
6140 6138 return (EINVAL);
6141 6139
6142 6140 ah_req = req->ipsr_ah_req;
6143 6141 esp_req = req->ipsr_esp_req;
6144 6142 se_req = req->ipsr_self_encap_req;
6145 6143
6146 6144 /* Don't allow setting self-encap without one or more of AH/ESP. */
6147 6145 if (se_req != 0 && esp_req == 0 && ah_req == 0)
6148 6146 return (EINVAL);
6149 6147
6150 6148 /*
6151 6149 * Are we dealing with a request to reset the policy (i.e.
6152 6150 * zero requests).
6153 6151 */
6154 6152 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6155 6153 (esp_req & REQ_MASK) == 0 &&
6156 6154 (se_req & REQ_MASK) == 0);
6157 6155
6158 6156 if (!is_pol_reset) {
6159 6157 /*
6160 6158 * If we couldn't load IPsec, fail with "protocol
6161 6159 * not supported".
6162 6160 * IPsec may not have been loaded for a request with zero
6163 6161 * policies, so we don't fail in this case.
6164 6162 */
6165 6163 mutex_enter(&ipss->ipsec_loader_lock);
6166 6164 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6167 6165 mutex_exit(&ipss->ipsec_loader_lock);
6168 6166 return (EPROTONOSUPPORT);
6169 6167 }
6170 6168 mutex_exit(&ipss->ipsec_loader_lock);
6171 6169
6172 6170 /*
6173 6171 * Test for valid requests. Invalid algorithms
6174 6172 * need to be tested by IPsec code because new
6175 6173 * algorithms can be added dynamically.
6176 6174 */
6177 6175 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6178 6176 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6179 6177 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6180 6178 return (EINVAL);
6181 6179 }
6182 6180
6183 6181 /*
6184 6182 * Only privileged users can issue these
6185 6183 * requests.
6186 6184 */
6187 6185 if (((ah_req & IPSEC_PREF_NEVER) ||
6188 6186 (esp_req & IPSEC_PREF_NEVER) ||
6189 6187 (se_req & IPSEC_PREF_NEVER)) &&
6190 6188 secpolicy_ip_config(cr, B_FALSE) != 0) {
6191 6189 return (EPERM);
6192 6190 }
6193 6191
6194 6192 /*
6195 6193 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6196 6194 * are mutually exclusive.
6197 6195 */
6198 6196 if (((ah_req & REQ_MASK) == REQ_MASK) ||
6199 6197 ((esp_req & REQ_MASK) == REQ_MASK) ||
6200 6198 ((se_req & REQ_MASK) == REQ_MASK)) {
6201 6199 /* Both of them are set */
6202 6200 return (EINVAL);
6203 6201 }
6204 6202 }
6205 6203
6206 6204 ASSERT(MUTEX_HELD(&connp->conn_lock));
6207 6205
6208 6206 /*
6209 6207 * If we have already cached policies in conn_connect(), don't
6210 6208 * let them change now. We cache policies for connections
6211 6209 * whose src,dst [addr, port] is known.
6212 6210 */
6213 6211 if (connp->conn_policy_cached) {
6214 6212 return (EINVAL);
6215 6213 }
6216 6214
6217 6215 /*
6218 6216 * We have a zero policies, reset the connection policy if already
6219 6217 * set. This will cause the connection to inherit the
6220 6218 * global policy, if any.
6221 6219 */
6222 6220 if (is_pol_reset) {
6223 6221 if (connp->conn_policy != NULL) {
6224 6222 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6225 6223 connp->conn_policy = NULL;
6226 6224 }
6227 6225 connp->conn_in_enforce_policy = B_FALSE;
6228 6226 connp->conn_out_enforce_policy = B_FALSE;
6229 6227 return (0);
6230 6228 }
6231 6229
6232 6230 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6233 6231 ipst->ips_netstack);
6234 6232 if (ph == NULL)
6235 6233 goto enomem;
6236 6234
6237 6235 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6238 6236 if (actp == NULL)
6239 6237 goto enomem;
6240 6238
6241 6239 /*
6242 6240 * Always insert IPv4 policy entries, since they can also apply to
6243 6241 * ipv6 sockets being used in ipv4-compat mode.
6244 6242 */
6245 6243 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6246 6244 IPSEC_TYPE_INBOUND, ns))
6247 6245 goto enomem;
6248 6246 is_pol_inserted = B_TRUE;
6249 6247 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6250 6248 IPSEC_TYPE_OUTBOUND, ns))
6251 6249 goto enomem;
6252 6250
6253 6251 /*
6254 6252 * We're looking at a v6 socket, also insert the v6-specific
6255 6253 * entries.
6256 6254 */
6257 6255 if (connp->conn_family == AF_INET6) {
6258 6256 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6259 6257 IPSEC_TYPE_INBOUND, ns))
6260 6258 goto enomem;
6261 6259 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6262 6260 IPSEC_TYPE_OUTBOUND, ns))
6263 6261 goto enomem;
6264 6262 }
6265 6263
6266 6264 ipsec_actvec_free(actp, nact);
6267 6265
6268 6266 /*
6269 6267 * If the requests need security, set enforce_policy.
6270 6268 * If the requests are IPSEC_PREF_NEVER, one should
6271 6269 * still set conn_out_enforce_policy so that ip_set_destination
6272 6270 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6273 6271 * for connections that we don't cache policy in at connect time,
6274 6272 * if global policy matches in ip_output_attach_policy, we
6275 6273 * don't wrongly inherit global policy. Similarly, we need
6276 6274 * to set conn_in_enforce_policy also so that we don't verify
6277 6275 * policy wrongly.
6278 6276 */
6279 6277 if ((ah_req & REQ_MASK) != 0 ||
6280 6278 (esp_req & REQ_MASK) != 0 ||
6281 6279 (se_req & REQ_MASK) != 0) {
6282 6280 connp->conn_in_enforce_policy = B_TRUE;
6283 6281 connp->conn_out_enforce_policy = B_TRUE;
6284 6282 }
6285 6283
6286 6284 return (error);
6287 6285 #undef REQ_MASK
6288 6286
6289 6287 /*
6290 6288 * Common memory-allocation-failure exit path.
6291 6289 */
6292 6290 enomem:
6293 6291 if (actp != NULL)
6294 6292 ipsec_actvec_free(actp, nact);
6295 6293 if (is_pol_inserted)
6296 6294 ipsec_polhead_flush(ph, ns);
6297 6295 return (ENOMEM);
6298 6296 }
6299 6297
6300 6298 /*
6301 6299 * Set socket options for joining and leaving multicast groups.
6302 6300 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6303 6301 * The caller has already check that the option name is consistent with
6304 6302 * the address family of the socket.
6305 6303 */
6306 6304 int
6307 6305 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6308 6306 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6309 6307 {
6310 6308 int *i1 = (int *)invalp;
6311 6309 int error = 0;
6312 6310 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6313 6311 struct ip_mreq *v4_mreqp;
6314 6312 struct ipv6_mreq *v6_mreqp;
6315 6313 struct group_req *greqp;
6316 6314 ire_t *ire;
6317 6315 boolean_t done = B_FALSE;
6318 6316 ipaddr_t ifaddr;
6319 6317 in6_addr_t v6group;
6320 6318 uint_t ifindex;
6321 6319 boolean_t mcast_opt = B_TRUE;
6322 6320 mcast_record_t fmode;
6323 6321 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6324 6322 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6325 6323
6326 6324 switch (name) {
6327 6325 case IP_ADD_MEMBERSHIP:
6328 6326 case IPV6_JOIN_GROUP:
6329 6327 mcast_opt = B_FALSE;
6330 6328 /* FALLTHRU */
6331 6329 case MCAST_JOIN_GROUP:
6332 6330 fmode = MODE_IS_EXCLUDE;
6333 6331 optfn = ip_opt_add_group;
6334 6332 break;
6335 6333
6336 6334 case IP_DROP_MEMBERSHIP:
6337 6335 case IPV6_LEAVE_GROUP:
6338 6336 mcast_opt = B_FALSE;
6339 6337 /* FALLTHRU */
6340 6338 case MCAST_LEAVE_GROUP:
6341 6339 fmode = MODE_IS_INCLUDE;
6342 6340 optfn = ip_opt_delete_group;
6343 6341 break;
6344 6342 default:
6345 6343 ASSERT(0);
6346 6344 }
6347 6345
6348 6346 if (mcast_opt) {
6349 6347 struct sockaddr_in *sin;
6350 6348 struct sockaddr_in6 *sin6;
6351 6349
6352 6350 greqp = (struct group_req *)i1;
6353 6351 if (greqp->gr_group.ss_family == AF_INET) {
6354 6352 sin = (struct sockaddr_in *)&(greqp->gr_group);
6355 6353 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6356 6354 } else {
6357 6355 if (!inet6)
6358 6356 return (EINVAL); /* Not on INET socket */
6359 6357
6360 6358 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6361 6359 v6group = sin6->sin6_addr;
6362 6360 }
6363 6361 ifaddr = INADDR_ANY;
6364 6362 ifindex = greqp->gr_interface;
6365 6363 } else if (inet6) {
6366 6364 v6_mreqp = (struct ipv6_mreq *)i1;
6367 6365 v6group = v6_mreqp->ipv6mr_multiaddr;
6368 6366 ifaddr = INADDR_ANY;
6369 6367 ifindex = v6_mreqp->ipv6mr_interface;
6370 6368 } else {
6371 6369 v4_mreqp = (struct ip_mreq *)i1;
6372 6370 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6373 6371 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6374 6372 ifindex = 0;
6375 6373 }
6376 6374
6377 6375 /*
6378 6376 * In the multirouting case, we need to replicate
6379 6377 * the request on all interfaces that will take part
6380 6378 * in replication. We do so because multirouting is
6381 6379 * reflective, thus we will probably receive multi-
6382 6380 * casts on those interfaces.
6383 6381 * The ip_multirt_apply_membership() succeeds if
6384 6382 * the operation succeeds on at least one interface.
6385 6383 */
6386 6384 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6387 6385 ipaddr_t group;
6388 6386
6389 6387 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6390 6388
6391 6389 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6392 6390 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6393 6391 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6394 6392 } else {
6395 6393 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6396 6394 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6397 6395 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6398 6396 }
6399 6397 if (ire != NULL) {
6400 6398 if (ire->ire_flags & RTF_MULTIRT) {
6401 6399 error = ip_multirt_apply_membership(optfn, ire, connp,
6402 6400 checkonly, &v6group, fmode, &ipv6_all_zeros);
6403 6401 done = B_TRUE;
6404 6402 }
6405 6403 ire_refrele(ire);
6406 6404 }
6407 6405
6408 6406 if (!done) {
6409 6407 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6410 6408 fmode, &ipv6_all_zeros);
6411 6409 }
6412 6410 return (error);
6413 6411 }
6414 6412
6415 6413 /*
6416 6414 * Set socket options for joining and leaving multicast groups
6417 6415 * for specific sources.
6418 6416 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6419 6417 * The caller has already check that the option name is consistent with
6420 6418 * the address family of the socket.
6421 6419 */
6422 6420 int
6423 6421 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6424 6422 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6425 6423 {
6426 6424 int *i1 = (int *)invalp;
6427 6425 int error = 0;
6428 6426 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6429 6427 struct ip_mreq_source *imreqp;
6430 6428 struct group_source_req *gsreqp;
6431 6429 in6_addr_t v6group, v6src;
6432 6430 uint32_t ifindex;
6433 6431 ipaddr_t ifaddr;
6434 6432 boolean_t mcast_opt = B_TRUE;
6435 6433 mcast_record_t fmode;
6436 6434 ire_t *ire;
6437 6435 boolean_t done = B_FALSE;
6438 6436 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6439 6437 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6440 6438
6441 6439 switch (name) {
6442 6440 case IP_BLOCK_SOURCE:
6443 6441 mcast_opt = B_FALSE;
6444 6442 /* FALLTHRU */
6445 6443 case MCAST_BLOCK_SOURCE:
6446 6444 fmode = MODE_IS_EXCLUDE;
6447 6445 optfn = ip_opt_add_group;
6448 6446 break;
6449 6447
6450 6448 case IP_UNBLOCK_SOURCE:
6451 6449 mcast_opt = B_FALSE;
6452 6450 /* FALLTHRU */
6453 6451 case MCAST_UNBLOCK_SOURCE:
6454 6452 fmode = MODE_IS_EXCLUDE;
6455 6453 optfn = ip_opt_delete_group;
6456 6454 break;
6457 6455
6458 6456 case IP_ADD_SOURCE_MEMBERSHIP:
6459 6457 mcast_opt = B_FALSE;
6460 6458 /* FALLTHRU */
6461 6459 case MCAST_JOIN_SOURCE_GROUP:
6462 6460 fmode = MODE_IS_INCLUDE;
6463 6461 optfn = ip_opt_add_group;
6464 6462 break;
6465 6463
6466 6464 case IP_DROP_SOURCE_MEMBERSHIP:
6467 6465 mcast_opt = B_FALSE;
6468 6466 /* FALLTHRU */
6469 6467 case MCAST_LEAVE_SOURCE_GROUP:
6470 6468 fmode = MODE_IS_INCLUDE;
6471 6469 optfn = ip_opt_delete_group;
6472 6470 break;
6473 6471 default:
6474 6472 ASSERT(0);
6475 6473 }
6476 6474
6477 6475 if (mcast_opt) {
6478 6476 gsreqp = (struct group_source_req *)i1;
6479 6477 ifindex = gsreqp->gsr_interface;
6480 6478 if (gsreqp->gsr_group.ss_family == AF_INET) {
6481 6479 struct sockaddr_in *s;
6482 6480 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6483 6481 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6484 6482 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6485 6483 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6486 6484 } else {
6487 6485 struct sockaddr_in6 *s6;
6488 6486
6489 6487 if (!inet6)
6490 6488 return (EINVAL); /* Not on INET socket */
6491 6489
6492 6490 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6493 6491 v6group = s6->sin6_addr;
6494 6492 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6495 6493 v6src = s6->sin6_addr;
6496 6494 }
6497 6495 ifaddr = INADDR_ANY;
6498 6496 } else {
6499 6497 imreqp = (struct ip_mreq_source *)i1;
6500 6498 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6501 6499 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6502 6500 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6503 6501 ifindex = 0;
6504 6502 }
6505 6503
6506 6504 /*
6507 6505 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6508 6506 */
6509 6507 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6510 6508 v6src = ipv6_all_zeros;
6511 6509
6512 6510 /*
6513 6511 * In the multirouting case, we need to replicate
6514 6512 * the request as noted in the mcast cases above.
6515 6513 */
6516 6514 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6517 6515 ipaddr_t group;
6518 6516
6519 6517 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6520 6518
6521 6519 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6522 6520 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6523 6521 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6524 6522 } else {
6525 6523 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6526 6524 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6527 6525 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6528 6526 }
6529 6527 if (ire != NULL) {
6530 6528 if (ire->ire_flags & RTF_MULTIRT) {
6531 6529 error = ip_multirt_apply_membership(optfn, ire, connp,
6532 6530 checkonly, &v6group, fmode, &v6src);
6533 6531 done = B_TRUE;
6534 6532 }
6535 6533 ire_refrele(ire);
6536 6534 }
6537 6535 if (!done) {
6538 6536 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6539 6537 fmode, &v6src);
6540 6538 }
6541 6539 return (error);
6542 6540 }
6543 6541
6544 6542 /*
6545 6543 * Given a destination address and a pointer to where to put the information
6546 6544 * this routine fills in the mtuinfo.
6547 6545 * The socket must be connected.
6548 6546 * For sctp conn_faddr is the primary address.
6549 6547 */
6550 6548 int
6551 6549 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6552 6550 {
6553 6551 uint32_t pmtu = IP_MAXPACKET;
6554 6552 uint_t scopeid;
6555 6553
6556 6554 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6557 6555 return (-1);
6558 6556
6559 6557 /* In case we never sent or called ip_set_destination_v4/v6 */
6560 6558 if (ixa->ixa_ire != NULL)
6561 6559 pmtu = ip_get_pmtu(ixa);
6562 6560
6563 6561 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6564 6562 scopeid = ixa->ixa_scopeid;
6565 6563 else
6566 6564 scopeid = 0;
6567 6565
6568 6566 bzero(mtuinfo, sizeof (*mtuinfo));
6569 6567 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6570 6568 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6571 6569 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6572 6570 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6573 6571 mtuinfo->ip6m_mtu = pmtu;
6574 6572
6575 6573 return (sizeof (struct ip6_mtuinfo));
6576 6574 }
6577 6575
6578 6576 /*
6579 6577 * When the src multihoming is changed from weak to [strong, preferred]
6580 6578 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6581 6579 * and identify routes that were created by user-applications in the
6582 6580 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6583 6581 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6584 6582 * is selected by finding an interface route for the gateway.
6585 6583 */
6586 6584 /* ARGSUSED */
6587 6585 void
6588 6586 ip_ire_rebind_walker(ire_t *ire, void *notused)
6589 6587 {
6590 6588 if (!ire->ire_unbound || ire->ire_ill != NULL)
6591 6589 return;
6592 6590 ire_rebind(ire);
6593 6591 ire_delete(ire);
6594 6592 }
6595 6593
6596 6594 /*
6597 6595 * When the src multihoming is changed from [strong, preferred] to weak,
6598 6596 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6599 6597 * set any entries that were created by user-applications in the unbound state
6600 6598 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6601 6599 */
6602 6600 /* ARGSUSED */
6603 6601 void
6604 6602 ip_ire_unbind_walker(ire_t *ire, void *notused)
6605 6603 {
6606 6604 ire_t *new_ire;
6607 6605
6608 6606 if (!ire->ire_unbound || ire->ire_ill == NULL)
6609 6607 return;
6610 6608 if (ire->ire_ipversion == IPV6_VERSION) {
6611 6609 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6612 6610 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6613 6611 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6614 6612 } else {
6615 6613 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6616 6614 (uchar_t *)&ire->ire_mask,
6617 6615 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6618 6616 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6619 6617 }
6620 6618 if (new_ire == NULL)
6621 6619 return;
6622 6620 new_ire->ire_unbound = B_TRUE;
6623 6621 /*
6624 6622 * The bound ire must first be deleted so that we don't return
6625 6623 * the existing one on the attempt to add the unbound new_ire.
6626 6624 */
6627 6625 ire_delete(ire);
6628 6626 new_ire = ire_add(new_ire);
6629 6627 if (new_ire != NULL)
6630 6628 ire_refrele(new_ire);
6631 6629 }
6632 6630
6633 6631 /*
6634 6632 * When the settings of ip*_strict_src_multihoming tunables are changed,
6635 6633 * all cached routes need to be recomputed. This recomputation needs to be
6636 6634 * done when going from weaker to stronger modes so that the cached ire
6637 6635 * for the connection does not violate the current ip*_strict_src_multihoming
6638 6636 * setting. It also needs to be done when going from stronger to weaker modes,
6639 6637 * so that we fall back to matching on the longest-matching-route (as opposed
6640 6638 * to a shorter match that may have been selected in the strong mode
6641 6639 * to satisfy src_multihoming settings).
6642 6640 *
6643 6641 * The cached ixa_ire entires for all conn_t entries are marked as
6644 6642 * "verify" so that they will be recomputed for the next packet.
6645 6643 */
6646 6644 void
6647 6645 conn_ire_revalidate(conn_t *connp, void *arg)
6648 6646 {
6649 6647 boolean_t isv6 = (boolean_t)arg;
6650 6648
6651 6649 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6652 6650 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6653 6651 return;
6654 6652 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6655 6653 }
6656 6654
6657 6655 /*
6658 6656 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6659 6657 * When an ipf is passed here for the first time, if
6660 6658 * we already have in-order fragments on the queue, we convert from the fast-
6661 6659 * path reassembly scheme to the hard-case scheme. From then on, additional
6662 6660 * fragments are reassembled here. We keep track of the start and end offsets
6663 6661 * of each piece, and the number of holes in the chain. When the hole count
6664 6662 * goes to zero, we are done!
6665 6663 *
6666 6664 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6667 6665 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6668 6666 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6669 6667 * after the call to ip_reassemble().
6670 6668 */
6671 6669 int
6672 6670 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6673 6671 size_t msg_len)
6674 6672 {
6675 6673 uint_t end;
6676 6674 mblk_t *next_mp;
6677 6675 mblk_t *mp1;
6678 6676 uint_t offset;
6679 6677 boolean_t incr_dups = B_TRUE;
6680 6678 boolean_t offset_zero_seen = B_FALSE;
6681 6679 boolean_t pkt_boundary_checked = B_FALSE;
6682 6680
6683 6681 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6684 6682 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6685 6683
6686 6684 /* Add in byte count */
6687 6685 ipf->ipf_count += msg_len;
6688 6686 if (ipf->ipf_end) {
6689 6687 /*
6690 6688 * We were part way through in-order reassembly, but now there
6691 6689 * is a hole. We walk through messages already queued, and
6692 6690 * mark them for hard case reassembly. We know that up till
6693 6691 * now they were in order starting from offset zero.
6694 6692 */
6695 6693 offset = 0;
6696 6694 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6697 6695 IP_REASS_SET_START(mp1, offset);
6698 6696 if (offset == 0) {
6699 6697 ASSERT(ipf->ipf_nf_hdr_len != 0);
6700 6698 offset = -ipf->ipf_nf_hdr_len;
6701 6699 }
6702 6700 offset += mp1->b_wptr - mp1->b_rptr;
6703 6701 IP_REASS_SET_END(mp1, offset);
6704 6702 }
6705 6703 /* One hole at the end. */
6706 6704 ipf->ipf_hole_cnt = 1;
6707 6705 /* Brand it as a hard case, forever. */
6708 6706 ipf->ipf_end = 0;
6709 6707 }
6710 6708 /* Walk through all the new pieces. */
6711 6709 do {
6712 6710 end = start + (mp->b_wptr - mp->b_rptr);
6713 6711 /*
6714 6712 * If start is 0, decrease 'end' only for the first mblk of
6715 6713 * the fragment. Otherwise 'end' can get wrong value in the
6716 6714 * second pass of the loop if first mblk is exactly the
6717 6715 * size of ipf_nf_hdr_len.
6718 6716 */
6719 6717 if (start == 0 && !offset_zero_seen) {
6720 6718 /* First segment */
6721 6719 ASSERT(ipf->ipf_nf_hdr_len != 0);
6722 6720 end -= ipf->ipf_nf_hdr_len;
6723 6721 offset_zero_seen = B_TRUE;
6724 6722 }
6725 6723 next_mp = mp->b_cont;
6726 6724 /*
6727 6725 * We are checking to see if there is any interesing data
6728 6726 * to process. If there isn't and the mblk isn't the
6729 6727 * one which carries the unfragmentable header then we
6730 6728 * drop it. It's possible to have just the unfragmentable
6731 6729 * header come through without any data. That needs to be
6732 6730 * saved.
6733 6731 *
6734 6732 * If the assert at the top of this function holds then the
6735 6733 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6736 6734 * is infrequently traveled enough that the test is left in
6737 6735 * to protect against future code changes which break that
6738 6736 * invariant.
6739 6737 */
6740 6738 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6741 6739 /* Empty. Blast it. */
6742 6740 IP_REASS_SET_START(mp, 0);
6743 6741 IP_REASS_SET_END(mp, 0);
6744 6742 /*
6745 6743 * If the ipf points to the mblk we are about to free,
6746 6744 * update ipf to point to the next mblk (or NULL
6747 6745 * if none).
6748 6746 */
6749 6747 if (ipf->ipf_mp->b_cont == mp)
6750 6748 ipf->ipf_mp->b_cont = next_mp;
6751 6749 freeb(mp);
6752 6750 continue;
6753 6751 }
6754 6752 mp->b_cont = NULL;
6755 6753 IP_REASS_SET_START(mp, start);
6756 6754 IP_REASS_SET_END(mp, end);
6757 6755 if (!ipf->ipf_tail_mp) {
6758 6756 ipf->ipf_tail_mp = mp;
6759 6757 ipf->ipf_mp->b_cont = mp;
6760 6758 if (start == 0 || !more) {
6761 6759 ipf->ipf_hole_cnt = 1;
6762 6760 /*
6763 6761 * if the first fragment comes in more than one
6764 6762 * mblk, this loop will be executed for each
6765 6763 * mblk. Need to adjust hole count so exiting
6766 6764 * this routine will leave hole count at 1.
6767 6765 */
6768 6766 if (next_mp)
6769 6767 ipf->ipf_hole_cnt++;
6770 6768 } else
6771 6769 ipf->ipf_hole_cnt = 2;
6772 6770 continue;
6773 6771 } else if (ipf->ipf_last_frag_seen && !more &&
6774 6772 !pkt_boundary_checked) {
6775 6773 /*
6776 6774 * We check datagram boundary only if this fragment
6777 6775 * claims to be the last fragment and we have seen a
6778 6776 * last fragment in the past too. We do this only
6779 6777 * once for a given fragment.
6780 6778 *
6781 6779 * start cannot be 0 here as fragments with start=0
6782 6780 * and MF=0 gets handled as a complete packet. These
6783 6781 * fragments should not reach here.
6784 6782 */
6785 6783
6786 6784 if (start + msgdsize(mp) !=
6787 6785 IP_REASS_END(ipf->ipf_tail_mp)) {
6788 6786 /*
6789 6787 * We have two fragments both of which claim
6790 6788 * to be the last fragment but gives conflicting
6791 6789 * information about the whole datagram size.
6792 6790 * Something fishy is going on. Drop the
6793 6791 * fragment and free up the reassembly list.
6794 6792 */
6795 6793 return (IP_REASS_FAILED);
6796 6794 }
6797 6795
6798 6796 /*
6799 6797 * We shouldn't come to this code block again for this
6800 6798 * particular fragment.
6801 6799 */
6802 6800 pkt_boundary_checked = B_TRUE;
6803 6801 }
6804 6802
6805 6803 /* New stuff at or beyond tail? */
6806 6804 offset = IP_REASS_END(ipf->ipf_tail_mp);
6807 6805 if (start >= offset) {
6808 6806 if (ipf->ipf_last_frag_seen) {
6809 6807 /* current fragment is beyond last fragment */
6810 6808 return (IP_REASS_FAILED);
6811 6809 }
6812 6810 /* Link it on end. */
6813 6811 ipf->ipf_tail_mp->b_cont = mp;
6814 6812 ipf->ipf_tail_mp = mp;
6815 6813 if (more) {
6816 6814 if (start != offset)
6817 6815 ipf->ipf_hole_cnt++;
6818 6816 } else if (start == offset && next_mp == NULL)
6819 6817 ipf->ipf_hole_cnt--;
6820 6818 continue;
6821 6819 }
6822 6820 mp1 = ipf->ipf_mp->b_cont;
6823 6821 offset = IP_REASS_START(mp1);
6824 6822 /* New stuff at the front? */
6825 6823 if (start < offset) {
6826 6824 if (start == 0) {
6827 6825 if (end >= offset) {
6828 6826 /* Nailed the hole at the begining. */
6829 6827 ipf->ipf_hole_cnt--;
6830 6828 }
6831 6829 } else if (end < offset) {
6832 6830 /*
6833 6831 * A hole, stuff, and a hole where there used
6834 6832 * to be just a hole.
6835 6833 */
6836 6834 ipf->ipf_hole_cnt++;
6837 6835 }
6838 6836 mp->b_cont = mp1;
6839 6837 /* Check for overlap. */
6840 6838 while (end > offset) {
6841 6839 if (end < IP_REASS_END(mp1)) {
6842 6840 mp->b_wptr -= end - offset;
6843 6841 IP_REASS_SET_END(mp, offset);
6844 6842 BUMP_MIB(ill->ill_ip_mib,
6845 6843 ipIfStatsReasmPartDups);
6846 6844 break;
6847 6845 }
6848 6846 /* Did we cover another hole? */
6849 6847 if ((mp1->b_cont &&
6850 6848 IP_REASS_END(mp1) !=
6851 6849 IP_REASS_START(mp1->b_cont) &&
6852 6850 end >= IP_REASS_START(mp1->b_cont)) ||
6853 6851 (!ipf->ipf_last_frag_seen && !more)) {
6854 6852 ipf->ipf_hole_cnt--;
6855 6853 }
6856 6854 /* Clip out mp1. */
6857 6855 if ((mp->b_cont = mp1->b_cont) == NULL) {
6858 6856 /*
6859 6857 * After clipping out mp1, this guy
6860 6858 * is now hanging off the end.
6861 6859 */
6862 6860 ipf->ipf_tail_mp = mp;
6863 6861 }
6864 6862 IP_REASS_SET_START(mp1, 0);
6865 6863 IP_REASS_SET_END(mp1, 0);
6866 6864 /* Subtract byte count */
6867 6865 ipf->ipf_count -= mp1->b_datap->db_lim -
6868 6866 mp1->b_datap->db_base;
6869 6867 freeb(mp1);
6870 6868 BUMP_MIB(ill->ill_ip_mib,
6871 6869 ipIfStatsReasmPartDups);
6872 6870 mp1 = mp->b_cont;
6873 6871 if (!mp1)
6874 6872 break;
6875 6873 offset = IP_REASS_START(mp1);
6876 6874 }
6877 6875 ipf->ipf_mp->b_cont = mp;
6878 6876 continue;
6879 6877 }
6880 6878 /*
6881 6879 * The new piece starts somewhere between the start of the head
6882 6880 * and before the end of the tail.
6883 6881 */
6884 6882 for (; mp1; mp1 = mp1->b_cont) {
6885 6883 offset = IP_REASS_END(mp1);
6886 6884 if (start < offset) {
6887 6885 if (end <= offset) {
6888 6886 /* Nothing new. */
6889 6887 IP_REASS_SET_START(mp, 0);
6890 6888 IP_REASS_SET_END(mp, 0);
6891 6889 /* Subtract byte count */
6892 6890 ipf->ipf_count -= mp->b_datap->db_lim -
6893 6891 mp->b_datap->db_base;
6894 6892 if (incr_dups) {
6895 6893 ipf->ipf_num_dups++;
6896 6894 incr_dups = B_FALSE;
6897 6895 }
6898 6896 freeb(mp);
6899 6897 BUMP_MIB(ill->ill_ip_mib,
6900 6898 ipIfStatsReasmDuplicates);
6901 6899 break;
6902 6900 }
6903 6901 /*
6904 6902 * Trim redundant stuff off beginning of new
6905 6903 * piece.
6906 6904 */
6907 6905 IP_REASS_SET_START(mp, offset);
6908 6906 mp->b_rptr += offset - start;
6909 6907 BUMP_MIB(ill->ill_ip_mib,
6910 6908 ipIfStatsReasmPartDups);
6911 6909 start = offset;
6912 6910 if (!mp1->b_cont) {
6913 6911 /*
6914 6912 * After trimming, this guy is now
6915 6913 * hanging off the end.
6916 6914 */
6917 6915 mp1->b_cont = mp;
6918 6916 ipf->ipf_tail_mp = mp;
6919 6917 if (!more) {
6920 6918 ipf->ipf_hole_cnt--;
6921 6919 }
6922 6920 break;
6923 6921 }
6924 6922 }
6925 6923 if (start >= IP_REASS_START(mp1->b_cont))
6926 6924 continue;
6927 6925 /* Fill a hole */
6928 6926 if (start > offset)
6929 6927 ipf->ipf_hole_cnt++;
6930 6928 mp->b_cont = mp1->b_cont;
6931 6929 mp1->b_cont = mp;
6932 6930 mp1 = mp->b_cont;
6933 6931 offset = IP_REASS_START(mp1);
6934 6932 if (end >= offset) {
6935 6933 ipf->ipf_hole_cnt--;
6936 6934 /* Check for overlap. */
6937 6935 while (end > offset) {
6938 6936 if (end < IP_REASS_END(mp1)) {
6939 6937 mp->b_wptr -= end - offset;
6940 6938 IP_REASS_SET_END(mp, offset);
6941 6939 /*
6942 6940 * TODO we might bump
6943 6941 * this up twice if there is
6944 6942 * overlap at both ends.
6945 6943 */
6946 6944 BUMP_MIB(ill->ill_ip_mib,
6947 6945 ipIfStatsReasmPartDups);
6948 6946 break;
6949 6947 }
6950 6948 /* Did we cover another hole? */
6951 6949 if ((mp1->b_cont &&
6952 6950 IP_REASS_END(mp1)
6953 6951 != IP_REASS_START(mp1->b_cont) &&
6954 6952 end >=
6955 6953 IP_REASS_START(mp1->b_cont)) ||
6956 6954 (!ipf->ipf_last_frag_seen &&
6957 6955 !more)) {
6958 6956 ipf->ipf_hole_cnt--;
6959 6957 }
6960 6958 /* Clip out mp1. */
6961 6959 if ((mp->b_cont = mp1->b_cont) ==
6962 6960 NULL) {
6963 6961 /*
6964 6962 * After clipping out mp1,
6965 6963 * this guy is now hanging
6966 6964 * off the end.
6967 6965 */
6968 6966 ipf->ipf_tail_mp = mp;
6969 6967 }
6970 6968 IP_REASS_SET_START(mp1, 0);
6971 6969 IP_REASS_SET_END(mp1, 0);
6972 6970 /* Subtract byte count */
6973 6971 ipf->ipf_count -=
6974 6972 mp1->b_datap->db_lim -
6975 6973 mp1->b_datap->db_base;
6976 6974 freeb(mp1);
6977 6975 BUMP_MIB(ill->ill_ip_mib,
6978 6976 ipIfStatsReasmPartDups);
6979 6977 mp1 = mp->b_cont;
6980 6978 if (!mp1)
6981 6979 break;
6982 6980 offset = IP_REASS_START(mp1);
6983 6981 }
6984 6982 }
6985 6983 break;
6986 6984 }
6987 6985 } while (start = end, mp = next_mp);
6988 6986
6989 6987 /* Fragment just processed could be the last one. Remember this fact */
6990 6988 if (!more)
6991 6989 ipf->ipf_last_frag_seen = B_TRUE;
6992 6990
6993 6991 /* Still got holes? */
6994 6992 if (ipf->ipf_hole_cnt)
6995 6993 return (IP_REASS_PARTIAL);
6996 6994 /* Clean up overloaded fields to avoid upstream disasters. */
6997 6995 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6998 6996 IP_REASS_SET_START(mp1, 0);
6999 6997 IP_REASS_SET_END(mp1, 0);
7000 6998 }
7001 6999 return (IP_REASS_COMPLETE);
7002 7000 }
7003 7001
7004 7002 /*
7005 7003 * Fragmentation reassembly. Each ILL has a hash table for
7006 7004 * queuing packets undergoing reassembly for all IPIFs
7007 7005 * associated with the ILL. The hash is based on the packet
7008 7006 * IP ident field. The ILL frag hash table was allocated
7009 7007 * as a timer block at the time the ILL was created. Whenever
7010 7008 * there is anything on the reassembly queue, the timer will
7011 7009 * be running. Returns the reassembled packet if reassembly completes.
7012 7010 */
7013 7011 mblk_t *
7014 7012 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
7015 7013 {
7016 7014 uint32_t frag_offset_flags;
7017 7015 mblk_t *t_mp;
7018 7016 ipaddr_t dst;
7019 7017 uint8_t proto = ipha->ipha_protocol;
7020 7018 uint32_t sum_val;
7021 7019 uint16_t sum_flags;
7022 7020 ipf_t *ipf;
7023 7021 ipf_t **ipfp;
7024 7022 ipfb_t *ipfb;
7025 7023 uint16_t ident;
7026 7024 uint32_t offset;
7027 7025 ipaddr_t src;
7028 7026 uint_t hdr_length;
7029 7027 uint32_t end;
7030 7028 mblk_t *mp1;
7031 7029 mblk_t *tail_mp;
7032 7030 size_t count;
7033 7031 size_t msg_len;
7034 7032 uint8_t ecn_info = 0;
7035 7033 uint32_t packet_size;
7036 7034 boolean_t pruned = B_FALSE;
7037 7035 ill_t *ill = ira->ira_ill;
7038 7036 ip_stack_t *ipst = ill->ill_ipst;
7039 7037
7040 7038 /*
7041 7039 * Drop the fragmented as early as possible, if
7042 7040 * we don't have resource(s) to re-assemble.
7043 7041 */
7044 7042 if (ipst->ips_ip_reass_queue_bytes == 0) {
7045 7043 freemsg(mp);
7046 7044 return (NULL);
7047 7045 }
7048 7046
7049 7047 /* Check for fragmentation offset; return if there's none */
7050 7048 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7051 7049 (IPH_MF | IPH_OFFSET)) == 0)
7052 7050 return (mp);
7053 7051
7054 7052 /*
7055 7053 * We utilize hardware computed checksum info only for UDP since
7056 7054 * IP fragmentation is a normal occurrence for the protocol. In
7057 7055 * addition, checksum offload support for IP fragments carrying
7058 7056 * UDP payload is commonly implemented across network adapters.
7059 7057 */
7060 7058 ASSERT(ira->ira_rill != NULL);
7061 7059 if (proto == IPPROTO_UDP && dohwcksum &&
7062 7060 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7063 7061 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7064 7062 mblk_t *mp1 = mp->b_cont;
7065 7063 int32_t len;
7066 7064
7067 7065 /* Record checksum information from the packet */
7068 7066 sum_val = (uint32_t)DB_CKSUM16(mp);
7069 7067 sum_flags = DB_CKSUMFLAGS(mp);
7070 7068
7071 7069 /* IP payload offset from beginning of mblk */
7072 7070 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7073 7071
7074 7072 if ((sum_flags & HCK_PARTIALCKSUM) &&
7075 7073 (mp1 == NULL || mp1->b_cont == NULL) &&
7076 7074 offset >= DB_CKSUMSTART(mp) &&
7077 7075 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7078 7076 uint32_t adj;
7079 7077 /*
7080 7078 * Partial checksum has been calculated by hardware
7081 7079 * and attached to the packet; in addition, any
7082 7080 * prepended extraneous data is even byte aligned.
7083 7081 * If any such data exists, we adjust the checksum;
7084 7082 * this would also handle any postpended data.
7085 7083 */
7086 7084 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7087 7085 mp, mp1, len, adj);
7088 7086
7089 7087 /* One's complement subtract extraneous checksum */
7090 7088 if (adj >= sum_val)
7091 7089 sum_val = ~(adj - sum_val) & 0xFFFF;
7092 7090 else
7093 7091 sum_val -= adj;
7094 7092 }
7095 7093 } else {
7096 7094 sum_val = 0;
7097 7095 sum_flags = 0;
7098 7096 }
7099 7097
7100 7098 /* Clear hardware checksumming flag */
7101 7099 DB_CKSUMFLAGS(mp) = 0;
7102 7100
7103 7101 ident = ipha->ipha_ident;
7104 7102 offset = (frag_offset_flags << 3) & 0xFFFF;
7105 7103 src = ipha->ipha_src;
7106 7104 dst = ipha->ipha_dst;
7107 7105 hdr_length = IPH_HDR_LENGTH(ipha);
7108 7106 end = ntohs(ipha->ipha_length) - hdr_length;
7109 7107
7110 7108 /* If end == 0 then we have a packet with no data, so just free it */
7111 7109 if (end == 0) {
7112 7110 freemsg(mp);
7113 7111 return (NULL);
7114 7112 }
7115 7113
7116 7114 /* Record the ECN field info. */
7117 7115 ecn_info = (ipha->ipha_type_of_service & 0x3);
7118 7116 if (offset != 0) {
7119 7117 /*
7120 7118 * If this isn't the first piece, strip the header, and
7121 7119 * add the offset to the end value.
7122 7120 */
7123 7121 mp->b_rptr += hdr_length;
7124 7122 end += offset;
7125 7123 }
7126 7124
7127 7125 /* Handle vnic loopback of fragments */
7128 7126 if (mp->b_datap->db_ref > 2)
7129 7127 msg_len = 0;
7130 7128 else
7131 7129 msg_len = MBLKSIZE(mp);
7132 7130
7133 7131 tail_mp = mp;
7134 7132 while (tail_mp->b_cont != NULL) {
7135 7133 tail_mp = tail_mp->b_cont;
7136 7134 if (tail_mp->b_datap->db_ref <= 2)
7137 7135 msg_len += MBLKSIZE(tail_mp);
7138 7136 }
7139 7137
7140 7138 /* If the reassembly list for this ILL will get too big, prune it */
7141 7139 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7142 7140 ipst->ips_ip_reass_queue_bytes) {
7143 7141 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7144 7142 uint_t, ill->ill_frag_count,
7145 7143 uint_t, ipst->ips_ip_reass_queue_bytes);
7146 7144 ill_frag_prune(ill,
7147 7145 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7148 7146 (ipst->ips_ip_reass_queue_bytes - msg_len));
7149 7147 pruned = B_TRUE;
7150 7148 }
7151 7149
7152 7150 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7153 7151 mutex_enter(&ipfb->ipfb_lock);
7154 7152
7155 7153 ipfp = &ipfb->ipfb_ipf;
7156 7154 /* Try to find an existing fragment queue for this packet. */
7157 7155 for (;;) {
7158 7156 ipf = ipfp[0];
7159 7157 if (ipf != NULL) {
7160 7158 /*
7161 7159 * It has to match on ident and src/dst address.
7162 7160 */
7163 7161 if (ipf->ipf_ident == ident &&
7164 7162 ipf->ipf_src == src &&
7165 7163 ipf->ipf_dst == dst &&
7166 7164 ipf->ipf_protocol == proto) {
7167 7165 /*
7168 7166 * If we have received too many
7169 7167 * duplicate fragments for this packet
7170 7168 * free it.
7171 7169 */
7172 7170 if (ipf->ipf_num_dups > ip_max_frag_dups) {
7173 7171 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7174 7172 freemsg(mp);
7175 7173 mutex_exit(&ipfb->ipfb_lock);
7176 7174 return (NULL);
7177 7175 }
7178 7176 /* Found it. */
7179 7177 break;
7180 7178 }
7181 7179 ipfp = &ipf->ipf_hash_next;
7182 7180 continue;
7183 7181 }
7184 7182
7185 7183 /*
7186 7184 * If we pruned the list, do we want to store this new
7187 7185 * fragment?. We apply an optimization here based on the
7188 7186 * fact that most fragments will be received in order.
7189 7187 * So if the offset of this incoming fragment is zero,
7190 7188 * it is the first fragment of a new packet. We will
7191 7189 * keep it. Otherwise drop the fragment, as we have
7192 7190 * probably pruned the packet already (since the
7193 7191 * packet cannot be found).
7194 7192 */
7195 7193 if (pruned && offset != 0) {
7196 7194 mutex_exit(&ipfb->ipfb_lock);
7197 7195 freemsg(mp);
7198 7196 return (NULL);
7199 7197 }
7200 7198
7201 7199 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
7202 7200 /*
7203 7201 * Too many fragmented packets in this hash
7204 7202 * bucket. Free the oldest.
7205 7203 */
7206 7204 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7207 7205 }
7208 7206
7209 7207 /* New guy. Allocate a frag message. */
7210 7208 mp1 = allocb(sizeof (*ipf), BPRI_MED);
7211 7209 if (mp1 == NULL) {
7212 7210 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7213 7211 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7214 7212 freemsg(mp);
7215 7213 reass_done:
7216 7214 mutex_exit(&ipfb->ipfb_lock);
7217 7215 return (NULL);
7218 7216 }
7219 7217
7220 7218 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7221 7219 mp1->b_cont = mp;
7222 7220
7223 7221 /* Initialize the fragment header. */
7224 7222 ipf = (ipf_t *)mp1->b_rptr;
7225 7223 ipf->ipf_mp = mp1;
7226 7224 ipf->ipf_ptphn = ipfp;
7227 7225 ipfp[0] = ipf;
7228 7226 ipf->ipf_hash_next = NULL;
7229 7227 ipf->ipf_ident = ident;
7230 7228 ipf->ipf_protocol = proto;
7231 7229 ipf->ipf_src = src;
7232 7230 ipf->ipf_dst = dst;
7233 7231 ipf->ipf_nf_hdr_len = 0;
7234 7232 /* Record reassembly start time. */
7235 7233 ipf->ipf_timestamp = gethrestime_sec();
7236 7234 /* Record ipf generation and account for frag header */
7237 7235 ipf->ipf_gen = ill->ill_ipf_gen++;
7238 7236 ipf->ipf_count = MBLKSIZE(mp1);
7239 7237 ipf->ipf_last_frag_seen = B_FALSE;
7240 7238 ipf->ipf_ecn = ecn_info;
7241 7239 ipf->ipf_num_dups = 0;
7242 7240 ipfb->ipfb_frag_pkts++;
7243 7241 ipf->ipf_checksum = 0;
7244 7242 ipf->ipf_checksum_flags = 0;
7245 7243
7246 7244 /* Store checksum value in fragment header */
7247 7245 if (sum_flags != 0) {
7248 7246 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7249 7247 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7250 7248 ipf->ipf_checksum = sum_val;
7251 7249 ipf->ipf_checksum_flags = sum_flags;
7252 7250 }
7253 7251
7254 7252 /*
7255 7253 * We handle reassembly two ways. In the easy case,
7256 7254 * where all the fragments show up in order, we do
7257 7255 * minimal bookkeeping, and just clip new pieces on
7258 7256 * the end. If we ever see a hole, then we go off
7259 7257 * to ip_reassemble which has to mark the pieces and
7260 7258 * keep track of the number of holes, etc. Obviously,
7261 7259 * the point of having both mechanisms is so we can
7262 7260 * handle the easy case as efficiently as possible.
7263 7261 */
7264 7262 if (offset == 0) {
7265 7263 /* Easy case, in-order reassembly so far. */
7266 7264 ipf->ipf_count += msg_len;
7267 7265 ipf->ipf_tail_mp = tail_mp;
7268 7266 /*
7269 7267 * Keep track of next expected offset in
7270 7268 * ipf_end.
7271 7269 */
7272 7270 ipf->ipf_end = end;
7273 7271 ipf->ipf_nf_hdr_len = hdr_length;
7274 7272 } else {
7275 7273 /* Hard case, hole at the beginning. */
7276 7274 ipf->ipf_tail_mp = NULL;
7277 7275 /*
7278 7276 * ipf_end == 0 means that we have given up
7279 7277 * on easy reassembly.
7280 7278 */
7281 7279 ipf->ipf_end = 0;
7282 7280
7283 7281 /* Forget checksum offload from now on */
7284 7282 ipf->ipf_checksum_flags = 0;
7285 7283
7286 7284 /*
7287 7285 * ipf_hole_cnt is set by ip_reassemble.
7288 7286 * ipf_count is updated by ip_reassemble.
7289 7287 * No need to check for return value here
7290 7288 * as we don't expect reassembly to complete
7291 7289 * or fail for the first fragment itself.
7292 7290 */
7293 7291 (void) ip_reassemble(mp, ipf,
7294 7292 (frag_offset_flags & IPH_OFFSET) << 3,
7295 7293 (frag_offset_flags & IPH_MF), ill, msg_len);
7296 7294 }
7297 7295 /* Update per ipfb and ill byte counts */
7298 7296 ipfb->ipfb_count += ipf->ipf_count;
7299 7297 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7300 7298 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7301 7299 /* If the frag timer wasn't already going, start it. */
7302 7300 mutex_enter(&ill->ill_lock);
7303 7301 ill_frag_timer_start(ill);
7304 7302 mutex_exit(&ill->ill_lock);
7305 7303 goto reass_done;
7306 7304 }
7307 7305
7308 7306 /*
7309 7307 * If the packet's flag has changed (it could be coming up
7310 7308 * from an interface different than the previous, therefore
7311 7309 * possibly different checksum capability), then forget about
7312 7310 * any stored checksum states. Otherwise add the value to
7313 7311 * the existing one stored in the fragment header.
7314 7312 */
7315 7313 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7316 7314 sum_val += ipf->ipf_checksum;
7317 7315 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7318 7316 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7319 7317 ipf->ipf_checksum = sum_val;
7320 7318 } else if (ipf->ipf_checksum_flags != 0) {
7321 7319 /* Forget checksum offload from now on */
7322 7320 ipf->ipf_checksum_flags = 0;
7323 7321 }
7324 7322
7325 7323 /*
7326 7324 * We have a new piece of a datagram which is already being
7327 7325 * reassembled. Update the ECN info if all IP fragments
7328 7326 * are ECN capable. If there is one which is not, clear
7329 7327 * all the info. If there is at least one which has CE
7330 7328 * code point, IP needs to report that up to transport.
7331 7329 */
7332 7330 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7333 7331 if (ecn_info == IPH_ECN_CE)
7334 7332 ipf->ipf_ecn = IPH_ECN_CE;
7335 7333 } else {
7336 7334 ipf->ipf_ecn = IPH_ECN_NECT;
7337 7335 }
7338 7336 if (offset && ipf->ipf_end == offset) {
7339 7337 /* The new fragment fits at the end */
7340 7338 ipf->ipf_tail_mp->b_cont = mp;
7341 7339 /* Update the byte count */
7342 7340 ipf->ipf_count += msg_len;
7343 7341 /* Update per ipfb and ill byte counts */
7344 7342 ipfb->ipfb_count += msg_len;
7345 7343 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7346 7344 atomic_add_32(&ill->ill_frag_count, msg_len);
7347 7345 if (frag_offset_flags & IPH_MF) {
7348 7346 /* More to come. */
7349 7347 ipf->ipf_end = end;
7350 7348 ipf->ipf_tail_mp = tail_mp;
7351 7349 goto reass_done;
7352 7350 }
7353 7351 } else {
7354 7352 /* Go do the hard cases. */
7355 7353 int ret;
7356 7354
7357 7355 if (offset == 0)
7358 7356 ipf->ipf_nf_hdr_len = hdr_length;
7359 7357
7360 7358 /* Save current byte count */
7361 7359 count = ipf->ipf_count;
7362 7360 ret = ip_reassemble(mp, ipf,
7363 7361 (frag_offset_flags & IPH_OFFSET) << 3,
7364 7362 (frag_offset_flags & IPH_MF), ill, msg_len);
7365 7363 /* Count of bytes added and subtracted (freeb()ed) */
7366 7364 count = ipf->ipf_count - count;
7367 7365 if (count) {
7368 7366 /* Update per ipfb and ill byte counts */
7369 7367 ipfb->ipfb_count += count;
7370 7368 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7371 7369 atomic_add_32(&ill->ill_frag_count, count);
7372 7370 }
7373 7371 if (ret == IP_REASS_PARTIAL) {
7374 7372 goto reass_done;
7375 7373 } else if (ret == IP_REASS_FAILED) {
7376 7374 /* Reassembly failed. Free up all resources */
7377 7375 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7378 7376 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7379 7377 IP_REASS_SET_START(t_mp, 0);
7380 7378 IP_REASS_SET_END(t_mp, 0);
7381 7379 }
7382 7380 freemsg(mp);
7383 7381 goto reass_done;
7384 7382 }
7385 7383 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7386 7384 }
7387 7385 /*
7388 7386 * We have completed reassembly. Unhook the frag header from
7389 7387 * the reassembly list.
7390 7388 *
7391 7389 * Before we free the frag header, record the ECN info
7392 7390 * to report back to the transport.
7393 7391 */
7394 7392 ecn_info = ipf->ipf_ecn;
7395 7393 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7396 7394 ipfp = ipf->ipf_ptphn;
7397 7395
7398 7396 /* We need to supply these to caller */
7399 7397 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7400 7398 sum_val = ipf->ipf_checksum;
7401 7399 else
7402 7400 sum_val = 0;
7403 7401
7404 7402 mp1 = ipf->ipf_mp;
7405 7403 count = ipf->ipf_count;
7406 7404 ipf = ipf->ipf_hash_next;
7407 7405 if (ipf != NULL)
7408 7406 ipf->ipf_ptphn = ipfp;
7409 7407 ipfp[0] = ipf;
7410 7408 atomic_add_32(&ill->ill_frag_count, -count);
7411 7409 ASSERT(ipfb->ipfb_count >= count);
7412 7410 ipfb->ipfb_count -= count;
7413 7411 ipfb->ipfb_frag_pkts--;
7414 7412 mutex_exit(&ipfb->ipfb_lock);
7415 7413 /* Ditch the frag header. */
7416 7414 mp = mp1->b_cont;
7417 7415
7418 7416 freeb(mp1);
7419 7417
7420 7418 /* Restore original IP length in header. */
7421 7419 packet_size = (uint32_t)msgdsize(mp);
7422 7420 if (packet_size > IP_MAXPACKET) {
7423 7421 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7424 7422 ip_drop_input("Reassembled packet too large", mp, ill);
7425 7423 freemsg(mp);
7426 7424 return (NULL);
7427 7425 }
7428 7426
7429 7427 if (DB_REF(mp) > 1) {
7430 7428 mblk_t *mp2 = copymsg(mp);
7431 7429
7432 7430 if (mp2 == NULL) {
7433 7431 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7434 7432 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7435 7433 freemsg(mp);
7436 7434 return (NULL);
7437 7435 }
7438 7436 freemsg(mp);
7439 7437 mp = mp2;
7440 7438 }
7441 7439 ipha = (ipha_t *)mp->b_rptr;
7442 7440
7443 7441 ipha->ipha_length = htons((uint16_t)packet_size);
7444 7442 /* We're now complete, zip the frag state */
7445 7443 ipha->ipha_fragment_offset_and_flags = 0;
7446 7444 /* Record the ECN info. */
7447 7445 ipha->ipha_type_of_service &= 0xFC;
7448 7446 ipha->ipha_type_of_service |= ecn_info;
7449 7447
7450 7448 /* Update the receive attributes */
7451 7449 ira->ira_pktlen = packet_size;
7452 7450 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7453 7451
7454 7452 /* Reassembly is successful; set checksum information in packet */
7455 7453 DB_CKSUM16(mp) = (uint16_t)sum_val;
7456 7454 DB_CKSUMFLAGS(mp) = sum_flags;
7457 7455 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7458 7456
7459 7457 return (mp);
7460 7458 }
7461 7459
7462 7460 /*
7463 7461 * Pullup function that should be used for IP input in order to
7464 7462 * ensure we do not loose the L2 source address; we need the l2 source
7465 7463 * address for IP_RECVSLLA and for ndp_input.
7466 7464 *
7467 7465 * We return either NULL or b_rptr.
7468 7466 */
7469 7467 void *
7470 7468 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7471 7469 {
7472 7470 ill_t *ill = ira->ira_ill;
7473 7471
7474 7472 if (ip_rput_pullups++ == 0) {
7475 7473 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7476 7474 "ip_pullup: %s forced us to "
7477 7475 " pullup pkt, hdr len %ld, hdr addr %p",
7478 7476 ill->ill_name, len, (void *)mp->b_rptr);
7479 7477 }
7480 7478 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7481 7479 ip_setl2src(mp, ira, ira->ira_rill);
7482 7480 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7483 7481 if (!pullupmsg(mp, len))
7484 7482 return (NULL);
7485 7483 else
7486 7484 return (mp->b_rptr);
7487 7485 }
7488 7486
7489 7487 /*
7490 7488 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7491 7489 * When called from the ULP ira_rill will be NULL hence the caller has to
7492 7490 * pass in the ill.
7493 7491 */
7494 7492 /* ARGSUSED */
7495 7493 void
7496 7494 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7497 7495 {
7498 7496 const uchar_t *addr;
7499 7497 int alen;
7500 7498
7501 7499 if (ira->ira_flags & IRAF_L2SRC_SET)
7502 7500 return;
7503 7501
7504 7502 ASSERT(ill != NULL);
7505 7503 alen = ill->ill_phys_addr_length;
7506 7504 ASSERT(alen <= sizeof (ira->ira_l2src));
7507 7505 if (ira->ira_mhip != NULL &&
7508 7506 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7509 7507 bcopy(addr, ira->ira_l2src, alen);
7510 7508 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7511 7509 (addr = ill->ill_phys_addr) != NULL) {
7512 7510 bcopy(addr, ira->ira_l2src, alen);
7513 7511 } else {
7514 7512 bzero(ira->ira_l2src, alen);
7515 7513 }
7516 7514 ira->ira_flags |= IRAF_L2SRC_SET;
7517 7515 }
7518 7516
7519 7517 /*
7520 7518 * check ip header length and align it.
7521 7519 */
7522 7520 mblk_t *
7523 7521 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7524 7522 {
7525 7523 ill_t *ill = ira->ira_ill;
7526 7524 ssize_t len;
7527 7525
7528 7526 len = MBLKL(mp);
7529 7527
7530 7528 if (!OK_32PTR(mp->b_rptr))
7531 7529 IP_STAT(ill->ill_ipst, ip_notaligned);
7532 7530 else
7533 7531 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7534 7532
7535 7533 /* Guard against bogus device drivers */
7536 7534 if (len < 0) {
7537 7535 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7538 7536 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7539 7537 freemsg(mp);
7540 7538 return (NULL);
7541 7539 }
7542 7540
7543 7541 if (len == 0) {
7544 7542 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7545 7543 mblk_t *mp1 = mp->b_cont;
7546 7544
7547 7545 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7548 7546 ip_setl2src(mp, ira, ira->ira_rill);
7549 7547 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7550 7548
7551 7549 freeb(mp);
7552 7550 mp = mp1;
7553 7551 if (mp == NULL)
7554 7552 return (NULL);
7555 7553
7556 7554 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7557 7555 return (mp);
7558 7556 }
7559 7557 if (ip_pullup(mp, min_size, ira) == NULL) {
7560 7558 if (msgdsize(mp) < min_size) {
7561 7559 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7562 7560 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7563 7561 } else {
7564 7562 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7565 7563 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7566 7564 }
7567 7565 freemsg(mp);
7568 7566 return (NULL);
7569 7567 }
7570 7568 return (mp);
7571 7569 }
7572 7570
7573 7571 /*
7574 7572 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7575 7573 */
7576 7574 mblk_t *
7577 7575 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7578 7576 uint_t min_size, ip_recv_attr_t *ira)
7579 7577 {
7580 7578 ill_t *ill = ira->ira_ill;
7581 7579
7582 7580 /*
7583 7581 * Make sure we have data length consistent
7584 7582 * with the IP header.
7585 7583 */
7586 7584 if (mp->b_cont == NULL) {
7587 7585 /* pkt_len is based on ipha_len, not the mblk length */
7588 7586 if (pkt_len < min_size) {
7589 7587 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7590 7588 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7591 7589 freemsg(mp);
7592 7590 return (NULL);
7593 7591 }
7594 7592 if (len < 0) {
7595 7593 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7596 7594 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7597 7595 freemsg(mp);
7598 7596 return (NULL);
7599 7597 }
7600 7598 /* Drop any pad */
7601 7599 mp->b_wptr = rptr + pkt_len;
7602 7600 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7603 7601 ASSERT(pkt_len >= min_size);
7604 7602 if (pkt_len < min_size) {
7605 7603 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7606 7604 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7607 7605 freemsg(mp);
7608 7606 return (NULL);
7609 7607 }
7610 7608 if (len < 0) {
7611 7609 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7612 7610 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7613 7611 freemsg(mp);
7614 7612 return (NULL);
7615 7613 }
7616 7614 /* Drop any pad */
7617 7615 (void) adjmsg(mp, -len);
7618 7616 /*
7619 7617 * adjmsg may have freed an mblk from the chain, hence
7620 7618 * invalidate any hw checksum here. This will force IP to
7621 7619 * calculate the checksum in sw, but only for this packet.
7622 7620 */
7623 7621 DB_CKSUMFLAGS(mp) = 0;
7624 7622 IP_STAT(ill->ill_ipst, ip_multimblk);
7625 7623 }
7626 7624 return (mp);
7627 7625 }
7628 7626
7629 7627 /*
7630 7628 * Check that the IPv4 opt_len is consistent with the packet and pullup
7631 7629 * the options.
7632 7630 */
7633 7631 mblk_t *
7634 7632 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7635 7633 ip_recv_attr_t *ira)
7636 7634 {
7637 7635 ill_t *ill = ira->ira_ill;
7638 7636 ssize_t len;
7639 7637
7640 7638 /* Assume no IPv6 packets arrive over the IPv4 queue */
7641 7639 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7642 7640 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7643 7641 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7644 7642 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7645 7643 freemsg(mp);
7646 7644 return (NULL);
7647 7645 }
7648 7646
7649 7647 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7650 7648 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7651 7649 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7652 7650 freemsg(mp);
7653 7651 return (NULL);
7654 7652 }
7655 7653 /*
7656 7654 * Recompute complete header length and make sure we
7657 7655 * have access to all of it.
7658 7656 */
7659 7657 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7660 7658 if (len > (mp->b_wptr - mp->b_rptr)) {
7661 7659 if (len > pkt_len) {
7662 7660 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7663 7661 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7664 7662 freemsg(mp);
7665 7663 return (NULL);
7666 7664 }
7667 7665 if (ip_pullup(mp, len, ira) == NULL) {
7668 7666 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7669 7667 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7670 7668 freemsg(mp);
7671 7669 return (NULL);
7672 7670 }
7673 7671 }
7674 7672 return (mp);
7675 7673 }
7676 7674
7677 7675 /*
7678 7676 * Returns a new ire, or the same ire, or NULL.
7679 7677 * If a different IRE is returned, then it is held; the caller
7680 7678 * needs to release it.
7681 7679 * In no case is there any hold/release on the ire argument.
7682 7680 */
7683 7681 ire_t *
7684 7682 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7685 7683 {
7686 7684 ire_t *new_ire;
7687 7685 ill_t *ire_ill;
7688 7686 uint_t ifindex;
7689 7687 ip_stack_t *ipst = ill->ill_ipst;
7690 7688 boolean_t strict_check = B_FALSE;
7691 7689
7692 7690 /*
7693 7691 * IPMP common case: if IRE and ILL are in the same group, there's no
7694 7692 * issue (e.g. packet received on an underlying interface matched an
7695 7693 * IRE_LOCAL on its associated group interface).
7696 7694 */
7697 7695 ASSERT(ire->ire_ill != NULL);
7698 7696 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7699 7697 return (ire);
7700 7698
7701 7699 /*
7702 7700 * Do another ire lookup here, using the ingress ill, to see if the
7703 7701 * interface is in a usesrc group.
7704 7702 * As long as the ills belong to the same group, we don't consider
7705 7703 * them to be arriving on the wrong interface. Thus, if the switch
7706 7704 * is doing inbound load spreading, we won't drop packets when the
7707 7705 * ip*_strict_dst_multihoming switch is on.
7708 7706 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7709 7707 * where the local address may not be unique. In this case we were
7710 7708 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7711 7709 * actually returned. The new lookup, which is more specific, should
7712 7710 * only find the IRE_LOCAL associated with the ingress ill if one
7713 7711 * exists.
7714 7712 */
7715 7713 if (ire->ire_ipversion == IPV4_VERSION) {
7716 7714 if (ipst->ips_ip_strict_dst_multihoming)
7717 7715 strict_check = B_TRUE;
7718 7716 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7719 7717 IRE_LOCAL, ill, ALL_ZONES, NULL,
7720 7718 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7721 7719 } else {
7722 7720 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7723 7721 if (ipst->ips_ipv6_strict_dst_multihoming)
7724 7722 strict_check = B_TRUE;
7725 7723 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7726 7724 IRE_LOCAL, ill, ALL_ZONES, NULL,
7727 7725 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7728 7726 }
7729 7727 /*
7730 7728 * If the same ire that was returned in ip_input() is found then this
7731 7729 * is an indication that usesrc groups are in use. The packet
7732 7730 * arrived on a different ill in the group than the one associated with
7733 7731 * the destination address. If a different ire was found then the same
7734 7732 * IP address must be hosted on multiple ills. This is possible with
7735 7733 * unnumbered point2point interfaces. We switch to use this new ire in
7736 7734 * order to have accurate interface statistics.
7737 7735 */
7738 7736 if (new_ire != NULL) {
7739 7737 /* Note: held in one case but not the other? Caller handles */
7740 7738 if (new_ire != ire)
7741 7739 return (new_ire);
7742 7740 /* Unchanged */
7743 7741 ire_refrele(new_ire);
7744 7742 return (ire);
7745 7743 }
7746 7744
7747 7745 /*
7748 7746 * Chase pointers once and store locally.
7749 7747 */
7750 7748 ASSERT(ire->ire_ill != NULL);
7751 7749 ire_ill = ire->ire_ill;
7752 7750 ifindex = ill->ill_usesrc_ifindex;
7753 7751
7754 7752 /*
7755 7753 * Check if it's a legal address on the 'usesrc' interface.
7756 7754 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7757 7755 * can just check phyint_ifindex.
7758 7756 */
7759 7757 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7760 7758 return (ire);
7761 7759 }
7762 7760
7763 7761 /*
7764 7762 * If the ip*_strict_dst_multihoming switch is on then we can
7765 7763 * only accept this packet if the interface is marked as routing.
7766 7764 */
7767 7765 if (!(strict_check))
7768 7766 return (ire);
7769 7767
7770 7768 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7771 7769 return (ire);
7772 7770 }
7773 7771 return (NULL);
7774 7772 }
7775 7773
7776 7774 /*
7777 7775 * This function is used to construct a mac_header_info_s from a
7778 7776 * DL_UNITDATA_IND message.
7779 7777 * The address fields in the mhi structure points into the message,
7780 7778 * thus the caller can't use those fields after freeing the message.
7781 7779 *
7782 7780 * We determine whether the packet received is a non-unicast packet
7783 7781 * and in doing so, determine whether or not it is broadcast vs multicast.
7784 7782 * For it to be a broadcast packet, we must have the appropriate mblk_t
7785 7783 * hanging off the ill_t. If this is either not present or doesn't match
7786 7784 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7787 7785 * to be multicast. Thus NICs that have no broadcast address (or no
7788 7786 * capability for one, such as point to point links) cannot return as
7789 7787 * the packet being broadcast.
7790 7788 */
7791 7789 void
7792 7790 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7793 7791 {
7794 7792 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7795 7793 mblk_t *bmp;
7796 7794 uint_t extra_offset;
7797 7795
7798 7796 bzero(mhip, sizeof (struct mac_header_info_s));
7799 7797
7800 7798 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7801 7799
7802 7800 if (ill->ill_sap_length < 0)
7803 7801 extra_offset = 0;
7804 7802 else
7805 7803 extra_offset = ill->ill_sap_length;
7806 7804
7807 7805 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7808 7806 extra_offset;
7809 7807 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7810 7808 extra_offset;
7811 7809
7812 7810 if (!ind->dl_group_address)
7813 7811 return;
7814 7812
7815 7813 /* Multicast or broadcast */
7816 7814 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7817 7815
7818 7816 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7819 7817 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7820 7818 (bmp = ill->ill_bcast_mp) != NULL) {
7821 7819 dl_unitdata_req_t *dlur;
7822 7820 uint8_t *bphys_addr;
7823 7821
7824 7822 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7825 7823 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7826 7824 extra_offset;
7827 7825
7828 7826 if (bcmp(mhip->mhi_daddr, bphys_addr,
7829 7827 ind->dl_dest_addr_length) == 0)
7830 7828 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7831 7829 }
7832 7830 }
7833 7831
7834 7832 /*
7835 7833 * This function is used to construct a mac_header_info_s from a
7836 7834 * M_DATA fastpath message from a DLPI driver.
7837 7835 * The address fields in the mhi structure points into the message,
7838 7836 * thus the caller can't use those fields after freeing the message.
7839 7837 *
7840 7838 * We determine whether the packet received is a non-unicast packet
7841 7839 * and in doing so, determine whether or not it is broadcast vs multicast.
7842 7840 * For it to be a broadcast packet, we must have the appropriate mblk_t
7843 7841 * hanging off the ill_t. If this is either not present or doesn't match
7844 7842 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7845 7843 * to be multicast. Thus NICs that have no broadcast address (or no
7846 7844 * capability for one, such as point to point links) cannot return as
7847 7845 * the packet being broadcast.
7848 7846 */
7849 7847 void
7850 7848 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7851 7849 {
7852 7850 mblk_t *bmp;
7853 7851 struct ether_header *pether;
7854 7852
7855 7853 bzero(mhip, sizeof (struct mac_header_info_s));
7856 7854
7857 7855 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7858 7856
7859 7857 pether = (struct ether_header *)((char *)mp->b_rptr
7860 7858 - sizeof (struct ether_header));
7861 7859
7862 7860 /*
7863 7861 * Make sure the interface is an ethernet type, since we don't
7864 7862 * know the header format for anything but Ethernet. Also make
7865 7863 * sure we are pointing correctly above db_base.
7866 7864 */
7867 7865 if (ill->ill_type != IFT_ETHER)
7868 7866 return;
7869 7867
7870 7868 retry:
7871 7869 if ((uchar_t *)pether < mp->b_datap->db_base)
7872 7870 return;
7873 7871
7874 7872 /* Is there a VLAN tag? */
7875 7873 if (ill->ill_isv6) {
7876 7874 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7877 7875 pether = (struct ether_header *)((char *)pether - 4);
7878 7876 goto retry;
7879 7877 }
7880 7878 } else {
7881 7879 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7882 7880 pether = (struct ether_header *)((char *)pether - 4);
7883 7881 goto retry;
7884 7882 }
7885 7883 }
7886 7884 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7887 7885 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7888 7886
7889 7887 if (!(mhip->mhi_daddr[0] & 0x01))
7890 7888 return;
7891 7889
7892 7890 /* Multicast or broadcast */
7893 7891 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7894 7892
7895 7893 if ((bmp = ill->ill_bcast_mp) != NULL) {
7896 7894 dl_unitdata_req_t *dlur;
7897 7895 uint8_t *bphys_addr;
7898 7896 uint_t addrlen;
7899 7897
7900 7898 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7901 7899 addrlen = dlur->dl_dest_addr_length;
7902 7900 if (ill->ill_sap_length < 0) {
7903 7901 bphys_addr = (uchar_t *)dlur +
7904 7902 dlur->dl_dest_addr_offset;
7905 7903 addrlen += ill->ill_sap_length;
7906 7904 } else {
7907 7905 bphys_addr = (uchar_t *)dlur +
7908 7906 dlur->dl_dest_addr_offset +
7909 7907 ill->ill_sap_length;
7910 7908 addrlen -= ill->ill_sap_length;
7911 7909 }
7912 7910 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7913 7911 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7914 7912 }
7915 7913 }
7916 7914
7917 7915 /*
7918 7916 * Handle anything but M_DATA messages
7919 7917 * We see the DL_UNITDATA_IND which are part
7920 7918 * of the data path, and also the other messages from the driver.
7921 7919 */
7922 7920 void
7923 7921 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7924 7922 {
7925 7923 mblk_t *first_mp;
7926 7924 struct iocblk *iocp;
7927 7925 struct mac_header_info_s mhi;
7928 7926
7929 7927 switch (DB_TYPE(mp)) {
7930 7928 case M_PROTO:
7931 7929 case M_PCPROTO: {
7932 7930 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7933 7931 DL_UNITDATA_IND) {
7934 7932 /* Go handle anything other than data elsewhere. */
7935 7933 ip_rput_dlpi(ill, mp);
7936 7934 return;
7937 7935 }
7938 7936
7939 7937 first_mp = mp;
7940 7938 mp = first_mp->b_cont;
7941 7939 first_mp->b_cont = NULL;
7942 7940
7943 7941 if (mp == NULL) {
7944 7942 freeb(first_mp);
7945 7943 return;
7946 7944 }
7947 7945 ip_dlur_to_mhi(ill, first_mp, &mhi);
7948 7946 if (ill->ill_isv6)
7949 7947 ip_input_v6(ill, NULL, mp, &mhi);
7950 7948 else
7951 7949 ip_input(ill, NULL, mp, &mhi);
7952 7950
7953 7951 /* Ditch the DLPI header. */
7954 7952 freeb(first_mp);
7955 7953 return;
7956 7954 }
7957 7955 case M_IOCACK:
7958 7956 iocp = (struct iocblk *)mp->b_rptr;
7959 7957 switch (iocp->ioc_cmd) {
7960 7958 case DL_IOC_HDR_INFO:
7961 7959 ill_fastpath_ack(ill, mp);
7962 7960 return;
7963 7961 default:
7964 7962 putnext(ill->ill_rq, mp);
7965 7963 return;
7966 7964 }
7967 7965 /* FALLTHRU */
7968 7966 case M_ERROR:
7969 7967 case M_HANGUP:
7970 7968 mutex_enter(&ill->ill_lock);
7971 7969 if (ill->ill_state_flags & ILL_CONDEMNED) {
7972 7970 mutex_exit(&ill->ill_lock);
7973 7971 freemsg(mp);
7974 7972 return;
7975 7973 }
7976 7974 ill_refhold_locked(ill);
7977 7975 mutex_exit(&ill->ill_lock);
7978 7976 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7979 7977 B_FALSE);
7980 7978 return;
7981 7979 case M_CTL:
7982 7980 putnext(ill->ill_rq, mp);
7983 7981 return;
7984 7982 case M_IOCNAK:
7985 7983 ip1dbg(("got iocnak "));
7986 7984 iocp = (struct iocblk *)mp->b_rptr;
7987 7985 switch (iocp->ioc_cmd) {
7988 7986 case DL_IOC_HDR_INFO:
7989 7987 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7990 7988 return;
7991 7989 default:
7992 7990 break;
7993 7991 }
7994 7992 /* FALLTHRU */
7995 7993 default:
7996 7994 putnext(ill->ill_rq, mp);
7997 7995 return;
7998 7996 }
7999 7997 }
8000 7998
8001 7999 /* Read side put procedure. Packets coming from the wire arrive here. */
8002 8000 void
8003 8001 ip_rput(queue_t *q, mblk_t *mp)
8004 8002 {
8005 8003 ill_t *ill;
8006 8004 union DL_primitives *dl;
8007 8005
8008 8006 ill = (ill_t *)q->q_ptr;
8009 8007
8010 8008 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
8011 8009 /*
8012 8010 * If things are opening or closing, only accept high-priority
8013 8011 * DLPI messages. (On open ill->ill_ipif has not yet been
8014 8012 * created; on close, things hanging off the ill may have been
8015 8013 * freed already.)
8016 8014 */
8017 8015 dl = (union DL_primitives *)mp->b_rptr;
8018 8016 if (DB_TYPE(mp) != M_PCPROTO ||
8019 8017 dl->dl_primitive == DL_UNITDATA_IND) {
8020 8018 inet_freemsg(mp);
8021 8019 return;
8022 8020 }
8023 8021 }
8024 8022 if (DB_TYPE(mp) == M_DATA) {
8025 8023 struct mac_header_info_s mhi;
8026 8024
8027 8025 ip_mdata_to_mhi(ill, mp, &mhi);
8028 8026 ip_input(ill, NULL, mp, &mhi);
8029 8027 } else {
8030 8028 ip_rput_notdata(ill, mp);
8031 8029 }
8032 8030 }
8033 8031
8034 8032 /*
8035 8033 * Move the information to a copy.
8036 8034 */
8037 8035 mblk_t *
8038 8036 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8039 8037 {
8040 8038 mblk_t *mp1;
8041 8039 ill_t *ill = ira->ira_ill;
8042 8040 ip_stack_t *ipst = ill->ill_ipst;
8043 8041
8044 8042 IP_STAT(ipst, ip_db_ref);
8045 8043
8046 8044 /* Make sure we have ira_l2src before we loose the original mblk */
8047 8045 if (!(ira->ira_flags & IRAF_L2SRC_SET))
8048 8046 ip_setl2src(mp, ira, ira->ira_rill);
8049 8047
8050 8048 mp1 = copymsg(mp);
8051 8049 if (mp1 == NULL) {
8052 8050 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8053 8051 ip_drop_input("ipIfStatsInDiscards", mp, ill);
8054 8052 freemsg(mp);
8055 8053 return (NULL);
8056 8054 }
8057 8055 /* preserve the hardware checksum flags and data, if present */
8058 8056 if (DB_CKSUMFLAGS(mp) != 0) {
8059 8057 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8060 8058 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8061 8059 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8062 8060 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8063 8061 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8064 8062 }
8065 8063 freemsg(mp);
8066 8064 return (mp1);
8067 8065 }
8068 8066
8069 8067 static void
8070 8068 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8071 8069 t_uscalar_t err)
8072 8070 {
8073 8071 if (dl_err == DL_SYSERR) {
8074 8072 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8075 8073 "%s: %s failed: DL_SYSERR (errno %u)\n",
8076 8074 ill->ill_name, dl_primstr(prim), err);
8077 8075 return;
8078 8076 }
8079 8077
8080 8078 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8081 8079 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8082 8080 dl_errstr(dl_err));
8083 8081 }
8084 8082
8085 8083 /*
8086 8084 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8087 8085 * than DL_UNITDATA_IND messages. If we need to process this message
8088 8086 * exclusively, we call qwriter_ip, in which case we also need to call
8089 8087 * ill_refhold before that, since qwriter_ip does an ill_refrele.
8090 8088 */
8091 8089 void
8092 8090 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8093 8091 {
8094 8092 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8095 8093 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8096 8094 queue_t *q = ill->ill_rq;
8097 8095 t_uscalar_t prim = dloa->dl_primitive;
8098 8096 t_uscalar_t reqprim = DL_PRIM_INVAL;
8099 8097
8100 8098 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8101 8099 char *, dl_primstr(prim), ill_t *, ill);
8102 8100 ip1dbg(("ip_rput_dlpi"));
8103 8101
8104 8102 /*
8105 8103 * If we received an ACK but didn't send a request for it, then it
8106 8104 * can't be part of any pending operation; discard up-front.
8107 8105 */
8108 8106 switch (prim) {
8109 8107 case DL_ERROR_ACK:
8110 8108 reqprim = dlea->dl_error_primitive;
8111 8109 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8112 8110 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8113 8111 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8114 8112 dlea->dl_unix_errno));
8115 8113 break;
8116 8114 case DL_OK_ACK:
8117 8115 reqprim = dloa->dl_correct_primitive;
8118 8116 break;
8119 8117 case DL_INFO_ACK:
8120 8118 reqprim = DL_INFO_REQ;
8121 8119 break;
8122 8120 case DL_BIND_ACK:
8123 8121 reqprim = DL_BIND_REQ;
8124 8122 break;
8125 8123 case DL_PHYS_ADDR_ACK:
8126 8124 reqprim = DL_PHYS_ADDR_REQ;
8127 8125 break;
8128 8126 case DL_NOTIFY_ACK:
8129 8127 reqprim = DL_NOTIFY_REQ;
8130 8128 break;
8131 8129 case DL_CAPABILITY_ACK:
8132 8130 reqprim = DL_CAPABILITY_REQ;
8133 8131 break;
8134 8132 }
8135 8133
8136 8134 if (prim != DL_NOTIFY_IND) {
8137 8135 if (reqprim == DL_PRIM_INVAL ||
8138 8136 !ill_dlpi_pending(ill, reqprim)) {
8139 8137 /* Not a DLPI message we support or expected */
8140 8138 freemsg(mp);
8141 8139 return;
8142 8140 }
8143 8141 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8144 8142 dl_primstr(reqprim)));
8145 8143 }
8146 8144
8147 8145 switch (reqprim) {
8148 8146 case DL_UNBIND_REQ:
8149 8147 /*
8150 8148 * NOTE: we mark the unbind as complete even if we got a
8151 8149 * DL_ERROR_ACK, since there's not much else we can do.
8152 8150 */
8153 8151 mutex_enter(&ill->ill_lock);
8154 8152 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8155 8153 cv_signal(&ill->ill_cv);
8156 8154 mutex_exit(&ill->ill_lock);
8157 8155 break;
8158 8156
8159 8157 case DL_ENABMULTI_REQ:
8160 8158 if (prim == DL_OK_ACK) {
8161 8159 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8162 8160 ill->ill_dlpi_multicast_state = IDS_OK;
8163 8161 }
8164 8162 break;
8165 8163 }
8166 8164
8167 8165 /*
8168 8166 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8169 8167 * need to become writer to continue to process it. Because an
8170 8168 * exclusive operation doesn't complete until replies to all queued
8171 8169 * DLPI messages have been received, we know we're in the middle of an
8172 8170 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8173 8171 *
8174 8172 * As required by qwriter_ip(), we refhold the ill; it will refrele.
8175 8173 * Since this is on the ill stream we unconditionally bump up the
8176 8174 * refcount without doing ILL_CAN_LOOKUP().
8177 8175 */
8178 8176 ill_refhold(ill);
8179 8177 if (prim == DL_NOTIFY_IND)
8180 8178 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8181 8179 else
8182 8180 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8183 8181 }
8184 8182
8185 8183 /*
8186 8184 * Handling of DLPI messages that require exclusive access to the ipsq.
8187 8185 *
8188 8186 * Need to do ipsq_pending_mp_get on ioctl completion, which could
8189 8187 * happen here. (along with mi_copy_done)
8190 8188 */
8191 8189 /* ARGSUSED */
8192 8190 static void
8193 8191 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8194 8192 {
8195 8193 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8196 8194 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8197 8195 int err = 0;
8198 8196 ill_t *ill = (ill_t *)q->q_ptr;
8199 8197 ipif_t *ipif = NULL;
8200 8198 mblk_t *mp1 = NULL;
8201 8199 conn_t *connp = NULL;
8202 8200 t_uscalar_t paddrreq;
8203 8201 mblk_t *mp_hw;
8204 8202 boolean_t success;
8205 8203 boolean_t ioctl_aborted = B_FALSE;
8206 8204 boolean_t log = B_TRUE;
8207 8205
8208 8206 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8209 8207 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8210 8208
8211 8209 ip1dbg(("ip_rput_dlpi_writer .."));
8212 8210 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8213 8211 ASSERT(IAM_WRITER_ILL(ill));
8214 8212
8215 8213 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8216 8214 /*
8217 8215 * The current ioctl could have been aborted by the user and a new
8218 8216 * ioctl to bring up another ill could have started. We could still
8219 8217 * get a response from the driver later.
8220 8218 */
8221 8219 if (ipif != NULL && ipif->ipif_ill != ill)
8222 8220 ioctl_aborted = B_TRUE;
8223 8221
8224 8222 switch (dloa->dl_primitive) {
8225 8223 case DL_ERROR_ACK:
8226 8224 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8227 8225 dl_primstr(dlea->dl_error_primitive)));
8228 8226
8229 8227 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8230 8228 char *, dl_primstr(dlea->dl_error_primitive),
8231 8229 ill_t *, ill);
8232 8230
8233 8231 switch (dlea->dl_error_primitive) {
8234 8232 case DL_DISABMULTI_REQ:
8235 8233 ill_dlpi_done(ill, dlea->dl_error_primitive);
8236 8234 break;
8237 8235 case DL_PROMISCON_REQ:
8238 8236 case DL_PROMISCOFF_REQ:
8239 8237 case DL_UNBIND_REQ:
8240 8238 case DL_ATTACH_REQ:
8241 8239 case DL_INFO_REQ:
8242 8240 ill_dlpi_done(ill, dlea->dl_error_primitive);
8243 8241 break;
8244 8242 case DL_NOTIFY_REQ:
8245 8243 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8246 8244 log = B_FALSE;
8247 8245 break;
8248 8246 case DL_PHYS_ADDR_REQ:
8249 8247 /*
8250 8248 * For IPv6 only, there are two additional
8251 8249 * phys_addr_req's sent to the driver to get the
8252 8250 * IPv6 token and lla. This allows IP to acquire
8253 8251 * the hardware address format for a given interface
8254 8252 * without having built in knowledge of the hardware
8255 8253 * address. ill_phys_addr_pend keeps track of the last
8256 8254 * DL_PAR sent so we know which response we are
8257 8255 * dealing with. ill_dlpi_done will update
8258 8256 * ill_phys_addr_pend when it sends the next req.
8259 8257 * We don't complete the IOCTL until all three DL_PARs
8260 8258 * have been attempted, so set *_len to 0 and break.
8261 8259 */
8262 8260 paddrreq = ill->ill_phys_addr_pend;
8263 8261 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8264 8262 if (paddrreq == DL_IPV6_TOKEN) {
8265 8263 ill->ill_token_length = 0;
8266 8264 log = B_FALSE;
8267 8265 break;
8268 8266 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8269 8267 ill->ill_nd_lla_len = 0;
8270 8268 log = B_FALSE;
8271 8269 break;
8272 8270 }
8273 8271 /*
8274 8272 * Something went wrong with the DL_PHYS_ADDR_REQ.
8275 8273 * We presumably have an IOCTL hanging out waiting
8276 8274 * for completion. Find it and complete the IOCTL
8277 8275 * with the error noted.
8278 8276 * However, ill_dl_phys was called on an ill queue
8279 8277 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8280 8278 * set. But the ioctl is known to be pending on ill_wq.
8281 8279 */
8282 8280 if (!ill->ill_ifname_pending)
8283 8281 break;
8284 8282 ill->ill_ifname_pending = 0;
8285 8283 if (!ioctl_aborted)
8286 8284 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8287 8285 if (mp1 != NULL) {
8288 8286 /*
8289 8287 * This operation (SIOCSLIFNAME) must have
8290 8288 * happened on the ill. Assert there is no conn
8291 8289 */
8292 8290 ASSERT(connp == NULL);
8293 8291 q = ill->ill_wq;
8294 8292 }
8295 8293 break;
8296 8294 case DL_BIND_REQ:
8297 8295 ill_dlpi_done(ill, DL_BIND_REQ);
8298 8296 if (ill->ill_ifname_pending)
8299 8297 break;
8300 8298 mutex_enter(&ill->ill_lock);
8301 8299 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8302 8300 mutex_exit(&ill->ill_lock);
8303 8301 /*
8304 8302 * Something went wrong with the bind. We presumably
8305 8303 * have an IOCTL hanging out waiting for completion.
8306 8304 * Find it, take down the interface that was coming
8307 8305 * up, and complete the IOCTL with the error noted.
8308 8306 */
8309 8307 if (!ioctl_aborted)
8310 8308 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8311 8309 if (mp1 != NULL) {
8312 8310 /*
8313 8311 * This might be a result of a DL_NOTE_REPLUMB
8314 8312 * notification. In that case, connp is NULL.
8315 8313 */
8316 8314 if (connp != NULL)
8317 8315 q = CONNP_TO_WQ(connp);
8318 8316
8319 8317 (void) ipif_down(ipif, NULL, NULL);
8320 8318 /* error is set below the switch */
8321 8319 }
8322 8320 break;
8323 8321 case DL_ENABMULTI_REQ:
8324 8322 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8325 8323
8326 8324 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8327 8325 ill->ill_dlpi_multicast_state = IDS_FAILED;
8328 8326 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8329 8327
8330 8328 printf("ip: joining multicasts failed (%d)"
8331 8329 " on %s - will use link layer "
8332 8330 "broadcasts for multicast\n",
8333 8331 dlea->dl_errno, ill->ill_name);
8334 8332
8335 8333 /*
8336 8334 * Set up for multi_bcast; We are the
8337 8335 * writer, so ok to access ill->ill_ipif
8338 8336 * without any lock.
8339 8337 */
8340 8338 mutex_enter(&ill->ill_phyint->phyint_lock);
8341 8339 ill->ill_phyint->phyint_flags |=
8342 8340 PHYI_MULTI_BCAST;
8343 8341 mutex_exit(&ill->ill_phyint->phyint_lock);
8344 8342
8345 8343 }
8346 8344 freemsg(mp); /* Don't want to pass this up */
8347 8345 return;
8348 8346 case DL_CAPABILITY_REQ:
8349 8347 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8350 8348 "DL_CAPABILITY REQ\n"));
8351 8349 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8352 8350 ill->ill_dlpi_capab_state = IDCS_FAILED;
8353 8351 ill_capability_done(ill);
8354 8352 freemsg(mp);
8355 8353 return;
8356 8354 }
8357 8355 /*
8358 8356 * Note the error for IOCTL completion (mp1 is set when
8359 8357 * ready to complete ioctl). If ill_ifname_pending_err is
8360 8358 * set, an error occured during plumbing (ill_ifname_pending),
8361 8359 * so we want to report that error.
8362 8360 *
8363 8361 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8364 8362 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8365 8363 * expected to get errack'd if the driver doesn't support
8366 8364 * these flags (e.g. ethernet). log will be set to B_FALSE
8367 8365 * if these error conditions are encountered.
8368 8366 */
8369 8367 if (mp1 != NULL) {
8370 8368 if (ill->ill_ifname_pending_err != 0) {
8371 8369 err = ill->ill_ifname_pending_err;
8372 8370 ill->ill_ifname_pending_err = 0;
8373 8371 } else {
8374 8372 err = dlea->dl_unix_errno ?
8375 8373 dlea->dl_unix_errno : ENXIO;
8376 8374 }
8377 8375 /*
8378 8376 * If we're plumbing an interface and an error hasn't already
8379 8377 * been saved, set ill_ifname_pending_err to the error passed
8380 8378 * up. Ignore the error if log is B_FALSE (see comment above).
8381 8379 */
8382 8380 } else if (log && ill->ill_ifname_pending &&
8383 8381 ill->ill_ifname_pending_err == 0) {
8384 8382 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8385 8383 dlea->dl_unix_errno : ENXIO;
8386 8384 }
8387 8385
8388 8386 if (log)
8389 8387 ip_dlpi_error(ill, dlea->dl_error_primitive,
8390 8388 dlea->dl_errno, dlea->dl_unix_errno);
8391 8389 break;
8392 8390 case DL_CAPABILITY_ACK:
8393 8391 ill_capability_ack(ill, mp);
8394 8392 /*
8395 8393 * The message has been handed off to ill_capability_ack
8396 8394 * and must not be freed below
8397 8395 */
8398 8396 mp = NULL;
8399 8397 break;
8400 8398
8401 8399 case DL_INFO_ACK:
8402 8400 /* Call a routine to handle this one. */
8403 8401 ill_dlpi_done(ill, DL_INFO_REQ);
8404 8402 ip_ll_subnet_defaults(ill, mp);
8405 8403 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8406 8404 return;
8407 8405 case DL_BIND_ACK:
8408 8406 /*
8409 8407 * We should have an IOCTL waiting on this unless
8410 8408 * sent by ill_dl_phys, in which case just return
8411 8409 */
8412 8410 ill_dlpi_done(ill, DL_BIND_REQ);
8413 8411
8414 8412 if (ill->ill_ifname_pending) {
8415 8413 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8416 8414 ill_t *, ill, mblk_t *, mp);
8417 8415 break;
8418 8416 }
8419 8417 mutex_enter(&ill->ill_lock);
8420 8418 ill->ill_dl_up = 1;
8421 8419 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8422 8420 mutex_exit(&ill->ill_lock);
8423 8421
8424 8422 if (!ioctl_aborted)
8425 8423 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8426 8424 if (mp1 == NULL) {
8427 8425 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8428 8426 break;
8429 8427 }
8430 8428 /*
8431 8429 * mp1 was added by ill_dl_up(). if that is a result of
8432 8430 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8433 8431 */
8434 8432 if (connp != NULL)
8435 8433 q = CONNP_TO_WQ(connp);
8436 8434 /*
8437 8435 * We are exclusive. So nothing can change even after
8438 8436 * we get the pending mp.
8439 8437 */
8440 8438 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8441 8439 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8442 8440 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8443 8441
8444 8442 /*
8445 8443 * Now bring up the resolver; when that is complete, we'll
8446 8444 * create IREs. Note that we intentionally mirror what
8447 8445 * ipif_up() would have done, because we got here by way of
8448 8446 * ill_dl_up(), which stopped ipif_up()'s processing.
8449 8447 */
8450 8448 if (ill->ill_isv6) {
8451 8449 /*
8452 8450 * v6 interfaces.
8453 8451 * Unlike ARP which has to do another bind
8454 8452 * and attach, once we get here we are
8455 8453 * done with NDP
8456 8454 */
8457 8455 (void) ipif_resolver_up(ipif, Res_act_initial);
8458 8456 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8459 8457 err = ipif_up_done_v6(ipif);
8460 8458 } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8461 8459 /*
8462 8460 * ARP and other v4 external resolvers.
8463 8461 * Leave the pending mblk intact so that
8464 8462 * the ioctl completes in ip_rput().
8465 8463 */
8466 8464 if (connp != NULL)
8467 8465 mutex_enter(&connp->conn_lock);
8468 8466 mutex_enter(&ill->ill_lock);
8469 8467 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8470 8468 mutex_exit(&ill->ill_lock);
8471 8469 if (connp != NULL)
8472 8470 mutex_exit(&connp->conn_lock);
8473 8471 if (success) {
8474 8472 err = ipif_resolver_up(ipif, Res_act_initial);
8475 8473 if (err == EINPROGRESS) {
8476 8474 freemsg(mp);
8477 8475 return;
8478 8476 }
8479 8477 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8480 8478 } else {
8481 8479 /* The conn has started closing */
8482 8480 err = EINTR;
8483 8481 }
8484 8482 } else {
8485 8483 /*
8486 8484 * This one is complete. Reply to pending ioctl.
8487 8485 */
8488 8486 (void) ipif_resolver_up(ipif, Res_act_initial);
8489 8487 err = ipif_up_done(ipif);
8490 8488 }
8491 8489
8492 8490 if ((err == 0) && (ill->ill_up_ipifs)) {
8493 8491 err = ill_up_ipifs(ill, q, mp1);
8494 8492 if (err == EINPROGRESS) {
8495 8493 freemsg(mp);
8496 8494 return;
8497 8495 }
8498 8496 }
8499 8497
8500 8498 /*
8501 8499 * If we have a moved ipif to bring up, and everything has
8502 8500 * succeeded to this point, bring it up on the IPMP ill.
8503 8501 * Otherwise, leave it down -- the admin can try to bring it
8504 8502 * up by hand if need be.
8505 8503 */
8506 8504 if (ill->ill_move_ipif != NULL) {
8507 8505 if (err != 0) {
8508 8506 ill->ill_move_ipif = NULL;
8509 8507 } else {
8510 8508 ipif = ill->ill_move_ipif;
8511 8509 ill->ill_move_ipif = NULL;
8512 8510 err = ipif_up(ipif, q, mp1);
8513 8511 if (err == EINPROGRESS) {
8514 8512 freemsg(mp);
8515 8513 return;
8516 8514 }
8517 8515 }
8518 8516 }
8519 8517 break;
8520 8518
8521 8519 case DL_NOTIFY_IND: {
8522 8520 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8523 8521 uint_t orig_mtu, orig_mc_mtu;
8524 8522
8525 8523 switch (notify->dl_notification) {
8526 8524 case DL_NOTE_PHYS_ADDR:
8527 8525 err = ill_set_phys_addr(ill, mp);
8528 8526 break;
8529 8527
8530 8528 case DL_NOTE_REPLUMB:
8531 8529 /*
8532 8530 * Directly return after calling ill_replumb().
8533 8531 * Note that we should not free mp as it is reused
8534 8532 * in the ill_replumb() function.
8535 8533 */
8536 8534 err = ill_replumb(ill, mp);
8537 8535 return;
8538 8536
8539 8537 case DL_NOTE_FASTPATH_FLUSH:
8540 8538 nce_flush(ill, B_FALSE);
8541 8539 break;
8542 8540
8543 8541 case DL_NOTE_SDU_SIZE:
8544 8542 case DL_NOTE_SDU_SIZE2:
8545 8543 /*
8546 8544 * The dce and fragmentation code can cope with
8547 8545 * this changing while packets are being sent.
8548 8546 * When packets are sent ip_output will discover
8549 8547 * a change.
8550 8548 *
8551 8549 * Change the MTU size of the interface.
8552 8550 */
8553 8551 mutex_enter(&ill->ill_lock);
8554 8552 orig_mtu = ill->ill_mtu;
8555 8553 orig_mc_mtu = ill->ill_mc_mtu;
8556 8554 switch (notify->dl_notification) {
8557 8555 case DL_NOTE_SDU_SIZE:
8558 8556 ill->ill_current_frag =
8559 8557 (uint_t)notify->dl_data;
8560 8558 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8561 8559 break;
8562 8560 case DL_NOTE_SDU_SIZE2:
8563 8561 ill->ill_current_frag =
8564 8562 (uint_t)notify->dl_data1;
8565 8563 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8566 8564 break;
8567 8565 }
8568 8566 if (ill->ill_current_frag > ill->ill_max_frag)
8569 8567 ill->ill_max_frag = ill->ill_current_frag;
8570 8568
8571 8569 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8572 8570 ill->ill_mtu = ill->ill_current_frag;
8573 8571
8574 8572 /*
8575 8573 * If ill_user_mtu was set (via
8576 8574 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8577 8575 */
8578 8576 if (ill->ill_user_mtu != 0 &&
8579 8577 ill->ill_user_mtu < ill->ill_mtu)
8580 8578 ill->ill_mtu = ill->ill_user_mtu;
8581 8579
8582 8580 if (ill->ill_user_mtu != 0 &&
8583 8581 ill->ill_user_mtu < ill->ill_mc_mtu)
8584 8582 ill->ill_mc_mtu = ill->ill_user_mtu;
8585 8583
8586 8584 if (ill->ill_isv6) {
8587 8585 if (ill->ill_mtu < IPV6_MIN_MTU)
8588 8586 ill->ill_mtu = IPV6_MIN_MTU;
8589 8587 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8590 8588 ill->ill_mc_mtu = IPV6_MIN_MTU;
8591 8589 } else {
8592 8590 if (ill->ill_mtu < IP_MIN_MTU)
8593 8591 ill->ill_mtu = IP_MIN_MTU;
8594 8592 if (ill->ill_mc_mtu < IP_MIN_MTU)
8595 8593 ill->ill_mc_mtu = IP_MIN_MTU;
8596 8594 }
8597 8595 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8598 8596 ill->ill_mc_mtu = ill->ill_mtu;
8599 8597 }
8600 8598
8601 8599 mutex_exit(&ill->ill_lock);
8602 8600 /*
8603 8601 * Make sure all dce_generation checks find out
8604 8602 * that ill_mtu/ill_mc_mtu has changed.
8605 8603 */
8606 8604 if (orig_mtu != ill->ill_mtu ||
8607 8605 orig_mc_mtu != ill->ill_mc_mtu) {
8608 8606 dce_increment_all_generations(ill->ill_isv6,
8609 8607 ill->ill_ipst);
8610 8608 }
8611 8609
8612 8610 /*
8613 8611 * Refresh IPMP meta-interface MTU if necessary.
8614 8612 */
8615 8613 if (IS_UNDER_IPMP(ill))
8616 8614 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8617 8615 break;
8618 8616
8619 8617 case DL_NOTE_LINK_UP:
8620 8618 case DL_NOTE_LINK_DOWN: {
8621 8619 /*
8622 8620 * We are writer. ill / phyint / ipsq assocs stable.
8623 8621 * The RUNNING flag reflects the state of the link.
8624 8622 */
8625 8623 phyint_t *phyint = ill->ill_phyint;
8626 8624 uint64_t new_phyint_flags;
8627 8625 boolean_t changed = B_FALSE;
8628 8626 boolean_t went_up;
8629 8627
8630 8628 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8631 8629 mutex_enter(&phyint->phyint_lock);
8632 8630
8633 8631 new_phyint_flags = went_up ?
8634 8632 phyint->phyint_flags | PHYI_RUNNING :
8635 8633 phyint->phyint_flags & ~PHYI_RUNNING;
8636 8634
8637 8635 if (IS_IPMP(ill)) {
8638 8636 new_phyint_flags = went_up ?
8639 8637 new_phyint_flags & ~PHYI_FAILED :
8640 8638 new_phyint_flags | PHYI_FAILED;
8641 8639 }
8642 8640
8643 8641 if (new_phyint_flags != phyint->phyint_flags) {
8644 8642 phyint->phyint_flags = new_phyint_flags;
8645 8643 changed = B_TRUE;
8646 8644 }
8647 8645 mutex_exit(&phyint->phyint_lock);
8648 8646 /*
8649 8647 * ill_restart_dad handles the DAD restart and routing
8650 8648 * socket notification logic.
8651 8649 */
8652 8650 if (changed) {
8653 8651 ill_restart_dad(phyint->phyint_illv4, went_up);
8654 8652 ill_restart_dad(phyint->phyint_illv6, went_up);
8655 8653 }
8656 8654 break;
8657 8655 }
8658 8656 case DL_NOTE_PROMISC_ON_PHYS: {
8659 8657 phyint_t *phyint = ill->ill_phyint;
8660 8658
8661 8659 mutex_enter(&phyint->phyint_lock);
8662 8660 phyint->phyint_flags |= PHYI_PROMISC;
8663 8661 mutex_exit(&phyint->phyint_lock);
8664 8662 break;
8665 8663 }
8666 8664 case DL_NOTE_PROMISC_OFF_PHYS: {
8667 8665 phyint_t *phyint = ill->ill_phyint;
8668 8666
8669 8667 mutex_enter(&phyint->phyint_lock);
8670 8668 phyint->phyint_flags &= ~PHYI_PROMISC;
8671 8669 mutex_exit(&phyint->phyint_lock);
8672 8670 break;
8673 8671 }
8674 8672 case DL_NOTE_CAPAB_RENEG:
8675 8673 /*
8676 8674 * Something changed on the driver side.
8677 8675 * It wants us to renegotiate the capabilities
8678 8676 * on this ill. One possible cause is the aggregation
8679 8677 * interface under us where a port got added or
8680 8678 * went away.
8681 8679 *
8682 8680 * If the capability negotiation is already done
8683 8681 * or is in progress, reset the capabilities and
8684 8682 * mark the ill's ill_capab_reneg to be B_TRUE,
8685 8683 * so that when the ack comes back, we can start
8686 8684 * the renegotiation process.
8687 8685 *
8688 8686 * Note that if ill_capab_reneg is already B_TRUE
8689 8687 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8690 8688 * the capability resetting request has been sent
8691 8689 * and the renegotiation has not been started yet;
8692 8690 * nothing needs to be done in this case.
8693 8691 */
8694 8692 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8695 8693 ill_capability_reset(ill, B_TRUE);
8696 8694 ipsq_current_finish(ipsq);
8697 8695 break;
8698 8696
8699 8697 case DL_NOTE_ALLOWED_IPS:
8700 8698 ill_set_allowed_ips(ill, mp);
8701 8699 break;
8702 8700 default:
8703 8701 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8704 8702 "type 0x%x for DL_NOTIFY_IND\n",
8705 8703 notify->dl_notification));
8706 8704 break;
8707 8705 }
8708 8706
8709 8707 /*
8710 8708 * As this is an asynchronous operation, we
8711 8709 * should not call ill_dlpi_done
8712 8710 */
8713 8711 break;
8714 8712 }
8715 8713 case DL_NOTIFY_ACK: {
8716 8714 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8717 8715
8718 8716 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8719 8717 ill->ill_note_link = 1;
8720 8718 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8721 8719 break;
8722 8720 }
8723 8721 case DL_PHYS_ADDR_ACK: {
8724 8722 /*
8725 8723 * As part of plumbing the interface via SIOCSLIFNAME,
8726 8724 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8727 8725 * whose answers we receive here. As each answer is received,
8728 8726 * we call ill_dlpi_done() to dispatch the next request as
8729 8727 * we're processing the current one. Once all answers have
8730 8728 * been received, we use ipsq_pending_mp_get() to dequeue the
8731 8729 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8732 8730 * is invoked from an ill queue, conn_oper_pending_ill is not
8733 8731 * available, but we know the ioctl is pending on ill_wq.)
8734 8732 */
8735 8733 uint_t paddrlen, paddroff;
8736 8734 uint8_t *addr;
8737 8735
8738 8736 paddrreq = ill->ill_phys_addr_pend;
8739 8737 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8740 8738 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8741 8739 addr = mp->b_rptr + paddroff;
8742 8740
8743 8741 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8744 8742 if (paddrreq == DL_IPV6_TOKEN) {
8745 8743 /*
8746 8744 * bcopy to low-order bits of ill_token
8747 8745 *
8748 8746 * XXX Temporary hack - currently, all known tokens
8749 8747 * are 64 bits, so I'll cheat for the moment.
8750 8748 */
8751 8749 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8752 8750 ill->ill_token_length = paddrlen;
8753 8751 break;
8754 8752 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8755 8753 ASSERT(ill->ill_nd_lla_mp == NULL);
8756 8754 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8757 8755 mp = NULL;
8758 8756 break;
8759 8757 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8760 8758 ASSERT(ill->ill_dest_addr_mp == NULL);
8761 8759 ill->ill_dest_addr_mp = mp;
8762 8760 ill->ill_dest_addr = addr;
8763 8761 mp = NULL;
8764 8762 if (ill->ill_isv6) {
8765 8763 ill_setdesttoken(ill);
8766 8764 ipif_setdestlinklocal(ill->ill_ipif);
8767 8765 }
8768 8766 break;
8769 8767 }
8770 8768
8771 8769 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8772 8770 ASSERT(ill->ill_phys_addr_mp == NULL);
8773 8771 if (!ill->ill_ifname_pending)
8774 8772 break;
8775 8773 ill->ill_ifname_pending = 0;
8776 8774 if (!ioctl_aborted)
8777 8775 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8778 8776 if (mp1 != NULL) {
8779 8777 ASSERT(connp == NULL);
8780 8778 q = ill->ill_wq;
8781 8779 }
8782 8780 /*
8783 8781 * If any error acks received during the plumbing sequence,
8784 8782 * ill_ifname_pending_err will be set. Break out and send up
8785 8783 * the error to the pending ioctl.
8786 8784 */
8787 8785 if (ill->ill_ifname_pending_err != 0) {
8788 8786 err = ill->ill_ifname_pending_err;
8789 8787 ill->ill_ifname_pending_err = 0;
8790 8788 break;
8791 8789 }
8792 8790
8793 8791 ill->ill_phys_addr_mp = mp;
8794 8792 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8795 8793 mp = NULL;
8796 8794
8797 8795 /*
8798 8796 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8799 8797 * provider doesn't support physical addresses. We check both
8800 8798 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8801 8799 * not have physical addresses, but historically adversises a
8802 8800 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8803 8801 * its DL_PHYS_ADDR_ACK.
8804 8802 */
8805 8803 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8806 8804 ill->ill_phys_addr = NULL;
8807 8805 } else if (paddrlen != ill->ill_phys_addr_length) {
8808 8806 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8809 8807 paddrlen, ill->ill_phys_addr_length));
8810 8808 err = EINVAL;
8811 8809 break;
8812 8810 }
8813 8811
8814 8812 if (ill->ill_nd_lla_mp == NULL) {
8815 8813 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8816 8814 err = ENOMEM;
8817 8815 break;
8818 8816 }
8819 8817 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8820 8818 }
8821 8819
8822 8820 if (ill->ill_isv6) {
8823 8821 ill_setdefaulttoken(ill);
8824 8822 ipif_setlinklocal(ill->ill_ipif);
8825 8823 }
8826 8824 break;
8827 8825 }
8828 8826 case DL_OK_ACK:
8829 8827 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8830 8828 dl_primstr((int)dloa->dl_correct_primitive),
8831 8829 dloa->dl_correct_primitive));
8832 8830 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8833 8831 char *, dl_primstr(dloa->dl_correct_primitive),
8834 8832 ill_t *, ill);
8835 8833
8836 8834 switch (dloa->dl_correct_primitive) {
8837 8835 case DL_ENABMULTI_REQ:
8838 8836 case DL_DISABMULTI_REQ:
8839 8837 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8840 8838 break;
8841 8839 case DL_PROMISCON_REQ:
8842 8840 case DL_PROMISCOFF_REQ:
8843 8841 case DL_UNBIND_REQ:
8844 8842 case DL_ATTACH_REQ:
8845 8843 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8846 8844 break;
8847 8845 }
8848 8846 break;
8849 8847 default:
8850 8848 break;
8851 8849 }
8852 8850
8853 8851 freemsg(mp);
8854 8852 if (mp1 == NULL)
8855 8853 return;
8856 8854
8857 8855 /*
8858 8856 * The operation must complete without EINPROGRESS since
8859 8857 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8860 8858 * the operation will be stuck forever inside the IPSQ.
8861 8859 */
8862 8860 ASSERT(err != EINPROGRESS);
8863 8861
8864 8862 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8865 8863 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8866 8864 ipif_t *, NULL);
8867 8865
8868 8866 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8869 8867 case 0:
8870 8868 ipsq_current_finish(ipsq);
8871 8869 break;
8872 8870
8873 8871 case SIOCSLIFNAME:
8874 8872 case IF_UNITSEL: {
8875 8873 ill_t *ill_other = ILL_OTHER(ill);
8876 8874
8877 8875 /*
8878 8876 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8879 8877 * ill has a peer which is in an IPMP group, then place ill
8880 8878 * into the same group. One catch: although ifconfig plumbs
8881 8879 * the appropriate IPMP meta-interface prior to plumbing this
8882 8880 * ill, it is possible for multiple ifconfig applications to
8883 8881 * race (or for another application to adjust plumbing), in
8884 8882 * which case the IPMP meta-interface we need will be missing.
8885 8883 * If so, kick the phyint out of the group.
8886 8884 */
8887 8885 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8888 8886 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8889 8887 ipmp_illgrp_t *illg;
8890 8888
8891 8889 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8892 8890 if (illg == NULL)
8893 8891 ipmp_phyint_leave_grp(ill->ill_phyint);
8894 8892 else
8895 8893 ipmp_ill_join_illgrp(ill, illg);
8896 8894 }
8897 8895
8898 8896 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8899 8897 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8900 8898 else
8901 8899 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8902 8900 break;
8903 8901 }
8904 8902 case SIOCLIFADDIF:
8905 8903 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8906 8904 break;
8907 8905
8908 8906 default:
8909 8907 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8910 8908 break;
8911 8909 }
8912 8910 }
8913 8911
8914 8912 /*
8915 8913 * ip_rput_other is called by ip_rput to handle messages modifying the global
8916 8914 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8917 8915 */
8918 8916 /* ARGSUSED */
8919 8917 void
8920 8918 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8921 8919 {
8922 8920 ill_t *ill = q->q_ptr;
8923 8921 struct iocblk *iocp;
8924 8922
8925 8923 ip1dbg(("ip_rput_other "));
8926 8924 if (ipsq != NULL) {
8927 8925 ASSERT(IAM_WRITER_IPSQ(ipsq));
8928 8926 ASSERT(ipsq->ipsq_xop ==
8929 8927 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8930 8928 }
8931 8929
8932 8930 switch (mp->b_datap->db_type) {
8933 8931 case M_ERROR:
8934 8932 case M_HANGUP:
8935 8933 /*
8936 8934 * The device has a problem. We force the ILL down. It can
8937 8935 * be brought up again manually using SIOCSIFFLAGS (via
8938 8936 * ifconfig or equivalent).
8939 8937 */
8940 8938 ASSERT(ipsq != NULL);
8941 8939 if (mp->b_rptr < mp->b_wptr)
8942 8940 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8943 8941 if (ill->ill_error == 0)
8944 8942 ill->ill_error = ENXIO;
8945 8943 if (!ill_down_start(q, mp))
8946 8944 return;
8947 8945 ipif_all_down_tail(ipsq, q, mp, NULL);
8948 8946 break;
8949 8947 case M_IOCNAK: {
8950 8948 iocp = (struct iocblk *)mp->b_rptr;
8951 8949
8952 8950 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8953 8951 /*
8954 8952 * If this was the first attempt, turn off the fastpath
8955 8953 * probing.
8956 8954 */
8957 8955 mutex_enter(&ill->ill_lock);
8958 8956 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8959 8957 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8960 8958 mutex_exit(&ill->ill_lock);
8961 8959 /*
8962 8960 * don't flush the nce_t entries: we use them
8963 8961 * as an index to the ncec itself.
8964 8962 */
8965 8963 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8966 8964 ill->ill_name));
8967 8965 } else {
8968 8966 mutex_exit(&ill->ill_lock);
8969 8967 }
8970 8968 freemsg(mp);
8971 8969 break;
8972 8970 }
8973 8971 default:
8974 8972 ASSERT(0);
8975 8973 break;
8976 8974 }
8977 8975 }
8978 8976
8979 8977 /*
8980 8978 * Update any source route, record route or timestamp options
8981 8979 * When it fails it has consumed the message and BUMPed the MIB.
8982 8980 */
8983 8981 boolean_t
8984 8982 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8985 8983 ip_recv_attr_t *ira)
8986 8984 {
8987 8985 ipoptp_t opts;
8988 8986 uchar_t *opt;
8989 8987 uint8_t optval;
8990 8988 uint8_t optlen;
8991 8989 ipaddr_t dst;
8992 8990 ipaddr_t ifaddr;
8993 8991 uint32_t ts;
8994 8992 timestruc_t now;
8995 8993 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
8996 8994
8997 8995 ip2dbg(("ip_forward_options\n"));
8998 8996 dst = ipha->ipha_dst;
8999 8997 for (optval = ipoptp_first(&opts, ipha);
9000 8998 optval != IPOPT_EOL;
9001 8999 optval = ipoptp_next(&opts)) {
9002 9000 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9003 9001 opt = opts.ipoptp_cur;
9004 9002 optlen = opts.ipoptp_len;
9005 9003 ip2dbg(("ip_forward_options: opt %d, len %d\n",
9006 9004 optval, opts.ipoptp_len));
9007 9005 switch (optval) {
9008 9006 uint32_t off;
9009 9007 case IPOPT_SSRR:
9010 9008 case IPOPT_LSRR:
9011 9009 /* Check if adminstratively disabled */
9012 9010 if (!ipst->ips_ip_forward_src_routed) {
9013 9011 BUMP_MIB(dst_ill->ill_ip_mib,
9014 9012 ipIfStatsForwProhibits);
9015 9013 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
9016 9014 mp, dst_ill);
9017 9015 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
9018 9016 ira);
9019 9017 return (B_FALSE);
9020 9018 }
9021 9019 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9022 9020 /*
9023 9021 * Must be partial since ip_input_options
9024 9022 * checked for strict.
9025 9023 */
9026 9024 break;
9027 9025 }
9028 9026 off = opt[IPOPT_OFFSET];
9029 9027 off--;
9030 9028 redo_srr:
9031 9029 if (optlen < IP_ADDR_LEN ||
9032 9030 off > optlen - IP_ADDR_LEN) {
9033 9031 /* End of source route */
9034 9032 ip1dbg((
9035 9033 "ip_forward_options: end of SR\n"));
9036 9034 break;
9037 9035 }
9038 9036 /* Pick a reasonable address on the outbound if */
9039 9037 ASSERT(dst_ill != NULL);
9040 9038 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9041 9039 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9042 9040 NULL) != 0) {
9043 9041 /* No source! Shouldn't happen */
9044 9042 ifaddr = INADDR_ANY;
9045 9043 }
9046 9044 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9047 9045 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9048 9046 ip1dbg(("ip_forward_options: next hop 0x%x\n",
9049 9047 ntohl(dst)));
9050 9048
9051 9049 /*
9052 9050 * Check if our address is present more than
9053 9051 * once as consecutive hops in source route.
9054 9052 */
9055 9053 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9056 9054 off += IP_ADDR_LEN;
9057 9055 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9058 9056 goto redo_srr;
9059 9057 }
9060 9058 ipha->ipha_dst = dst;
9061 9059 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9062 9060 break;
9063 9061 case IPOPT_RR:
9064 9062 off = opt[IPOPT_OFFSET];
9065 9063 off--;
9066 9064 if (optlen < IP_ADDR_LEN ||
9067 9065 off > optlen - IP_ADDR_LEN) {
9068 9066 /* No more room - ignore */
9069 9067 ip1dbg((
9070 9068 "ip_forward_options: end of RR\n"));
9071 9069 break;
9072 9070 }
9073 9071 /* Pick a reasonable address on the outbound if */
9074 9072 ASSERT(dst_ill != NULL);
9075 9073 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9076 9074 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9077 9075 NULL) != 0) {
9078 9076 /* No source! Shouldn't happen */
9079 9077 ifaddr = INADDR_ANY;
9080 9078 }
9081 9079 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9082 9080 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9083 9081 break;
9084 9082 case IPOPT_TS:
9085 9083 /* Insert timestamp if there is room */
9086 9084 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9087 9085 case IPOPT_TS_TSONLY:
9088 9086 off = IPOPT_TS_TIMELEN;
9089 9087 break;
9090 9088 case IPOPT_TS_PRESPEC:
9091 9089 case IPOPT_TS_PRESPEC_RFC791:
9092 9090 /* Verify that the address matched */
9093 9091 off = opt[IPOPT_OFFSET] - 1;
9094 9092 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9095 9093 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9096 9094 /* Not for us */
9097 9095 break;
9098 9096 }
9099 9097 /* FALLTHRU */
9100 9098 case IPOPT_TS_TSANDADDR:
9101 9099 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9102 9100 break;
9103 9101 default:
9104 9102 /*
9105 9103 * ip_*put_options should have already
9106 9104 * dropped this packet.
9107 9105 */
9108 9106 cmn_err(CE_PANIC, "ip_forward_options: "
9109 9107 "unknown IT - bug in ip_input_options?\n");
9110 9108 return (B_TRUE); /* Keep "lint" happy */
9111 9109 }
9112 9110 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9113 9111 /* Increase overflow counter */
9114 9112 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9115 9113 opt[IPOPT_POS_OV_FLG] =
9116 9114 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9117 9115 (off << 4));
9118 9116 break;
9119 9117 }
9120 9118 off = opt[IPOPT_OFFSET] - 1;
9121 9119 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9122 9120 case IPOPT_TS_PRESPEC:
9123 9121 case IPOPT_TS_PRESPEC_RFC791:
9124 9122 case IPOPT_TS_TSANDADDR:
9125 9123 /* Pick a reasonable addr on the outbound if */
9126 9124 ASSERT(dst_ill != NULL);
9127 9125 if (ip_select_source_v4(dst_ill, INADDR_ANY,
9128 9126 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9129 9127 NULL, NULL) != 0) {
9130 9128 /* No source! Shouldn't happen */
9131 9129 ifaddr = INADDR_ANY;
9132 9130 }
9133 9131 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9134 9132 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9135 9133 /* FALLTHRU */
9136 9134 case IPOPT_TS_TSONLY:
9137 9135 off = opt[IPOPT_OFFSET] - 1;
9138 9136 /* Compute # of milliseconds since midnight */
9139 9137 gethrestime(&now);
9140 9138 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9141 9139 NSEC2MSEC(now.tv_nsec);
9142 9140 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9143 9141 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9144 9142 break;
9145 9143 }
9146 9144 break;
9147 9145 }
9148 9146 }
9149 9147 return (B_TRUE);
9150 9148 }
9151 9149
9152 9150 /*
9153 9151 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9154 9152 * returns 'true' if there are still fragments left on the queue, in
9155 9153 * which case we restart the timer.
9156 9154 */
9157 9155 void
9158 9156 ill_frag_timer(void *arg)
9159 9157 {
9160 9158 ill_t *ill = (ill_t *)arg;
9161 9159 boolean_t frag_pending;
9162 9160 ip_stack_t *ipst = ill->ill_ipst;
9163 9161 time_t timeout;
9164 9162
9165 9163 mutex_enter(&ill->ill_lock);
9166 9164 ASSERT(!ill->ill_fragtimer_executing);
9167 9165 if (ill->ill_state_flags & ILL_CONDEMNED) {
9168 9166 ill->ill_frag_timer_id = 0;
9169 9167 mutex_exit(&ill->ill_lock);
9170 9168 return;
9171 9169 }
9172 9170 ill->ill_fragtimer_executing = 1;
9173 9171 mutex_exit(&ill->ill_lock);
9174 9172
9175 9173 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9176 9174 ipst->ips_ip_reassembly_timeout);
9177 9175
9178 9176 frag_pending = ill_frag_timeout(ill, timeout);
9179 9177
9180 9178 /*
9181 9179 * Restart the timer, if we have fragments pending or if someone
9182 9180 * wanted us to be scheduled again.
9183 9181 */
9184 9182 mutex_enter(&ill->ill_lock);
9185 9183 ill->ill_fragtimer_executing = 0;
9186 9184 ill->ill_frag_timer_id = 0;
9187 9185 if (frag_pending || ill->ill_fragtimer_needrestart)
9188 9186 ill_frag_timer_start(ill);
9189 9187 mutex_exit(&ill->ill_lock);
9190 9188 }
9191 9189
9192 9190 void
9193 9191 ill_frag_timer_start(ill_t *ill)
9194 9192 {
9195 9193 ip_stack_t *ipst = ill->ill_ipst;
9196 9194 clock_t timeo_ms;
9197 9195
9198 9196 ASSERT(MUTEX_HELD(&ill->ill_lock));
9199 9197
9200 9198 /* If the ill is closing or opening don't proceed */
9201 9199 if (ill->ill_state_flags & ILL_CONDEMNED)
9202 9200 return;
9203 9201
9204 9202 if (ill->ill_fragtimer_executing) {
9205 9203 /*
9206 9204 * ill_frag_timer is currently executing. Just record the
9207 9205 * the fact that we want the timer to be restarted.
9208 9206 * ill_frag_timer will post a timeout before it returns,
9209 9207 * ensuring it will be called again.
9210 9208 */
9211 9209 ill->ill_fragtimer_needrestart = 1;
9212 9210 return;
9213 9211 }
9214 9212
9215 9213 if (ill->ill_frag_timer_id == 0) {
9216 9214 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9217 9215 ipst->ips_ip_reassembly_timeout) * SECONDS;
9218 9216
9219 9217 /*
9220 9218 * The timer is neither running nor is the timeout handler
9221 9219 * executing. Post a timeout so that ill_frag_timer will be
9222 9220 * called
9223 9221 */
9224 9222 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9225 9223 MSEC_TO_TICK(timeo_ms >> 1));
9226 9224 ill->ill_fragtimer_needrestart = 0;
9227 9225 }
9228 9226 }
9229 9227
9230 9228 /*
9231 9229 * Update any source route, record route or timestamp options.
9232 9230 * Check that we are at end of strict source route.
9233 9231 * The options have already been checked for sanity in ip_input_options().
9234 9232 */
9235 9233 boolean_t
9236 9234 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9237 9235 {
9238 9236 ipoptp_t opts;
9239 9237 uchar_t *opt;
9240 9238 uint8_t optval;
9241 9239 uint8_t optlen;
9242 9240 ipaddr_t dst;
9243 9241 ipaddr_t ifaddr;
9244 9242 uint32_t ts;
9245 9243 timestruc_t now;
9246 9244 ill_t *ill = ira->ira_ill;
9247 9245 ip_stack_t *ipst = ill->ill_ipst;
9248 9246
9249 9247 ip2dbg(("ip_input_local_options\n"));
9250 9248
9251 9249 for (optval = ipoptp_first(&opts, ipha);
9252 9250 optval != IPOPT_EOL;
9253 9251 optval = ipoptp_next(&opts)) {
9254 9252 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9255 9253 opt = opts.ipoptp_cur;
9256 9254 optlen = opts.ipoptp_len;
9257 9255 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9258 9256 optval, optlen));
9259 9257 switch (optval) {
9260 9258 uint32_t off;
9261 9259 case IPOPT_SSRR:
9262 9260 case IPOPT_LSRR:
9263 9261 off = opt[IPOPT_OFFSET];
9264 9262 off--;
9265 9263 if (optlen < IP_ADDR_LEN ||
9266 9264 off > optlen - IP_ADDR_LEN) {
9267 9265 /* End of source route */
9268 9266 ip1dbg(("ip_input_local_options: end of SR\n"));
9269 9267 break;
9270 9268 }
9271 9269 /*
9272 9270 * This will only happen if two consecutive entries
9273 9271 * in the source route contains our address or if
9274 9272 * it is a packet with a loose source route which
9275 9273 * reaches us before consuming the whole source route
9276 9274 */
9277 9275 ip1dbg(("ip_input_local_options: not end of SR\n"));
9278 9276 if (optval == IPOPT_SSRR) {
9279 9277 goto bad_src_route;
9280 9278 }
9281 9279 /*
9282 9280 * Hack: instead of dropping the packet truncate the
9283 9281 * source route to what has been used by filling the
9284 9282 * rest with IPOPT_NOP.
9285 9283 */
9286 9284 opt[IPOPT_OLEN] = (uint8_t)off;
9287 9285 while (off < optlen) {
9288 9286 opt[off++] = IPOPT_NOP;
9289 9287 }
9290 9288 break;
9291 9289 case IPOPT_RR:
9292 9290 off = opt[IPOPT_OFFSET];
9293 9291 off--;
9294 9292 if (optlen < IP_ADDR_LEN ||
9295 9293 off > optlen - IP_ADDR_LEN) {
9296 9294 /* No more room - ignore */
9297 9295 ip1dbg((
9298 9296 "ip_input_local_options: end of RR\n"));
9299 9297 break;
9300 9298 }
9301 9299 /* Pick a reasonable address on the outbound if */
9302 9300 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9303 9301 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9304 9302 NULL) != 0) {
9305 9303 /* No source! Shouldn't happen */
9306 9304 ifaddr = INADDR_ANY;
9307 9305 }
9308 9306 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9309 9307 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9310 9308 break;
9311 9309 case IPOPT_TS:
9312 9310 /* Insert timestamp if there is romm */
9313 9311 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9314 9312 case IPOPT_TS_TSONLY:
9315 9313 off = IPOPT_TS_TIMELEN;
9316 9314 break;
9317 9315 case IPOPT_TS_PRESPEC:
9318 9316 case IPOPT_TS_PRESPEC_RFC791:
9319 9317 /* Verify that the address matched */
9320 9318 off = opt[IPOPT_OFFSET] - 1;
9321 9319 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9322 9320 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9323 9321 /* Not for us */
9324 9322 break;
9325 9323 }
9326 9324 /* FALLTHRU */
9327 9325 case IPOPT_TS_TSANDADDR:
9328 9326 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9329 9327 break;
9330 9328 default:
9331 9329 /*
9332 9330 * ip_*put_options should have already
9333 9331 * dropped this packet.
9334 9332 */
9335 9333 cmn_err(CE_PANIC, "ip_input_local_options: "
9336 9334 "unknown IT - bug in ip_input_options?\n");
9337 9335 return (B_TRUE); /* Keep "lint" happy */
9338 9336 }
9339 9337 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9340 9338 /* Increase overflow counter */
9341 9339 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9342 9340 opt[IPOPT_POS_OV_FLG] =
9343 9341 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9344 9342 (off << 4));
9345 9343 break;
9346 9344 }
9347 9345 off = opt[IPOPT_OFFSET] - 1;
9348 9346 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9349 9347 case IPOPT_TS_PRESPEC:
9350 9348 case IPOPT_TS_PRESPEC_RFC791:
9351 9349 case IPOPT_TS_TSANDADDR:
9352 9350 /* Pick a reasonable addr on the outbound if */
9353 9351 if (ip_select_source_v4(ill, INADDR_ANY,
9354 9352 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9355 9353 &ifaddr, NULL, NULL) != 0) {
9356 9354 /* No source! Shouldn't happen */
9357 9355 ifaddr = INADDR_ANY;
9358 9356 }
9359 9357 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9360 9358 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9361 9359 /* FALLTHRU */
9362 9360 case IPOPT_TS_TSONLY:
9363 9361 off = opt[IPOPT_OFFSET] - 1;
9364 9362 /* Compute # of milliseconds since midnight */
9365 9363 gethrestime(&now);
9366 9364 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9367 9365 NSEC2MSEC(now.tv_nsec);
9368 9366 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9369 9367 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9370 9368 break;
9371 9369 }
9372 9370 break;
9373 9371 }
9374 9372 }
9375 9373 return (B_TRUE);
9376 9374
9377 9375 bad_src_route:
9378 9376 /* make sure we clear any indication of a hardware checksum */
9379 9377 DB_CKSUMFLAGS(mp) = 0;
9380 9378 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9381 9379 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9382 9380 return (B_FALSE);
9383 9381
9384 9382 }
9385 9383
9386 9384 /*
9387 9385 * Process IP options in an inbound packet. Always returns the nexthop.
9388 9386 * Normally this is the passed in nexthop, but if there is an option
9389 9387 * that effects the nexthop (such as a source route) that will be returned.
9390 9388 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9391 9389 * and mp freed.
9392 9390 */
9393 9391 ipaddr_t
9394 9392 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9395 9393 ip_recv_attr_t *ira, int *errorp)
9396 9394 {
9397 9395 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9398 9396 ipoptp_t opts;
9399 9397 uchar_t *opt;
9400 9398 uint8_t optval;
9401 9399 uint8_t optlen;
9402 9400 intptr_t code = 0;
9403 9401 ire_t *ire;
9404 9402
9405 9403 ip2dbg(("ip_input_options\n"));
9406 9404 *errorp = 0;
9407 9405 for (optval = ipoptp_first(&opts, ipha);
9408 9406 optval != IPOPT_EOL;
9409 9407 optval = ipoptp_next(&opts)) {
9410 9408 opt = opts.ipoptp_cur;
9411 9409 optlen = opts.ipoptp_len;
9412 9410 ip2dbg(("ip_input_options: opt %d, len %d\n",
9413 9411 optval, optlen));
9414 9412 /*
9415 9413 * Note: we need to verify the checksum before we
9416 9414 * modify anything thus this routine only extracts the next
9417 9415 * hop dst from any source route.
9418 9416 */
9419 9417 switch (optval) {
9420 9418 uint32_t off;
9421 9419 case IPOPT_SSRR:
9422 9420 case IPOPT_LSRR:
9423 9421 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9424 9422 if (optval == IPOPT_SSRR) {
9425 9423 ip1dbg(("ip_input_options: not next"
9426 9424 " strict source route 0x%x\n",
9427 9425 ntohl(dst)));
9428 9426 code = (char *)&ipha->ipha_dst -
9429 9427 (char *)ipha;
9430 9428 goto param_prob; /* RouterReq's */
9431 9429 }
9432 9430 ip2dbg(("ip_input_options: "
9433 9431 "not next source route 0x%x\n",
9434 9432 ntohl(dst)));
9435 9433 break;
9436 9434 }
9437 9435
9438 9436 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9439 9437 ip1dbg((
9440 9438 "ip_input_options: bad option offset\n"));
9441 9439 code = (char *)&opt[IPOPT_OLEN] -
9442 9440 (char *)ipha;
9443 9441 goto param_prob;
9444 9442 }
9445 9443 off = opt[IPOPT_OFFSET];
9446 9444 off--;
9447 9445 redo_srr:
9448 9446 if (optlen < IP_ADDR_LEN ||
9449 9447 off > optlen - IP_ADDR_LEN) {
9450 9448 /* End of source route */
9451 9449 ip1dbg(("ip_input_options: end of SR\n"));
9452 9450 break;
9453 9451 }
9454 9452 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9455 9453 ip1dbg(("ip_input_options: next hop 0x%x\n",
9456 9454 ntohl(dst)));
9457 9455
9458 9456 /*
9459 9457 * Check if our address is present more than
9460 9458 * once as consecutive hops in source route.
9461 9459 * XXX verify per-interface ip_forwarding
9462 9460 * for source route?
9463 9461 */
9464 9462 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9465 9463 off += IP_ADDR_LEN;
9466 9464 goto redo_srr;
9467 9465 }
9468 9466
9469 9467 if (dst == htonl(INADDR_LOOPBACK)) {
9470 9468 ip1dbg(("ip_input_options: loopback addr in "
9471 9469 "source route!\n"));
9472 9470 goto bad_src_route;
9473 9471 }
9474 9472 /*
9475 9473 * For strict: verify that dst is directly
9476 9474 * reachable.
9477 9475 */
9478 9476 if (optval == IPOPT_SSRR) {
9479 9477 ire = ire_ftable_lookup_v4(dst, 0, 0,
9480 9478 IRE_INTERFACE, NULL, ALL_ZONES,
9481 9479 ira->ira_tsl,
9482 9480 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9483 9481 NULL);
9484 9482 if (ire == NULL) {
9485 9483 ip1dbg(("ip_input_options: SSRR not "
9486 9484 "directly reachable: 0x%x\n",
9487 9485 ntohl(dst)));
9488 9486 goto bad_src_route;
9489 9487 }
9490 9488 ire_refrele(ire);
9491 9489 }
9492 9490 /*
9493 9491 * Defer update of the offset and the record route
9494 9492 * until the packet is forwarded.
9495 9493 */
9496 9494 break;
9497 9495 case IPOPT_RR:
9498 9496 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9499 9497 ip1dbg((
9500 9498 "ip_input_options: bad option offset\n"));
9501 9499 code = (char *)&opt[IPOPT_OLEN] -
9502 9500 (char *)ipha;
9503 9501 goto param_prob;
9504 9502 }
9505 9503 break;
9506 9504 case IPOPT_TS:
9507 9505 /*
9508 9506 * Verify that length >= 5 and that there is either
9509 9507 * room for another timestamp or that the overflow
9510 9508 * counter is not maxed out.
9511 9509 */
9512 9510 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9513 9511 if (optlen < IPOPT_MINLEN_IT) {
9514 9512 goto param_prob;
9515 9513 }
9516 9514 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9517 9515 ip1dbg((
9518 9516 "ip_input_options: bad option offset\n"));
9519 9517 code = (char *)&opt[IPOPT_OFFSET] -
9520 9518 (char *)ipha;
9521 9519 goto param_prob;
9522 9520 }
9523 9521 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9524 9522 case IPOPT_TS_TSONLY:
9525 9523 off = IPOPT_TS_TIMELEN;
9526 9524 break;
9527 9525 case IPOPT_TS_TSANDADDR:
9528 9526 case IPOPT_TS_PRESPEC:
9529 9527 case IPOPT_TS_PRESPEC_RFC791:
9530 9528 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9531 9529 break;
9532 9530 default:
9533 9531 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9534 9532 (char *)ipha;
9535 9533 goto param_prob;
9536 9534 }
9537 9535 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9538 9536 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9539 9537 /*
9540 9538 * No room and the overflow counter is 15
9541 9539 * already.
9542 9540 */
9543 9541 goto param_prob;
9544 9542 }
9545 9543 break;
9546 9544 }
9547 9545 }
9548 9546
9549 9547 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9550 9548 return (dst);
9551 9549 }
9552 9550
9553 9551 ip1dbg(("ip_input_options: error processing IP options."));
9554 9552 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9555 9553
9556 9554 param_prob:
9557 9555 /* make sure we clear any indication of a hardware checksum */
9558 9556 DB_CKSUMFLAGS(mp) = 0;
9559 9557 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9560 9558 icmp_param_problem(mp, (uint8_t)code, ira);
9561 9559 *errorp = -1;
9562 9560 return (dst);
9563 9561
9564 9562 bad_src_route:
9565 9563 /* make sure we clear any indication of a hardware checksum */
9566 9564 DB_CKSUMFLAGS(mp) = 0;
9567 9565 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9568 9566 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9569 9567 *errorp = -1;
9570 9568 return (dst);
9571 9569 }
9572 9570
9573 9571 /*
9574 9572 * IP & ICMP info in >=14 msg's ...
9575 9573 * - ip fixed part (mib2_ip_t)
9576 9574 * - icmp fixed part (mib2_icmp_t)
9577 9575 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9578 9576 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9579 9577 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9580 9578 * - ipRouteAttributeTable (ip 102) labeled routes
9581 9579 * - ip multicast membership (ip_member_t)
9582 9580 * - ip multicast source filtering (ip_grpsrc_t)
9583 9581 * - igmp fixed part (struct igmpstat)
9584 9582 * - multicast routing stats (struct mrtstat)
9585 9583 * - multicast routing vifs (array of struct vifctl)
9586 9584 * - multicast routing routes (array of struct mfcctl)
9587 9585 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9588 9586 * One per ill plus one generic
9589 9587 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9590 9588 * One per ill plus one generic
9591 9589 * - ipv6RouteEntry all IPv6 IREs
9592 9590 * - ipv6RouteAttributeTable (ip6 102) labeled routes
9593 9591 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9594 9592 * - ipv6AddrEntry all IPv6 ipifs
9595 9593 * - ipv6 multicast membership (ipv6_member_t)
9596 9594 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9597 9595 *
9598 9596 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9599 9597 * already filled in by the caller.
9600 9598 * If legacy_req is true then MIB structures needs to be truncated to their
9601 9599 * legacy sizes before being returned.
9602 9600 * Return value of 0 indicates that no messages were sent and caller
9603 9601 * should free mpctl.
9604 9602 */
9605 9603 int
9606 9604 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9607 9605 {
9608 9606 ip_stack_t *ipst;
9609 9607 sctp_stack_t *sctps;
9610 9608
9611 9609 if (q->q_next != NULL) {
9612 9610 ipst = ILLQ_TO_IPST(q);
9613 9611 } else {
9614 9612 ipst = CONNQ_TO_IPST(q);
9615 9613 }
9616 9614 ASSERT(ipst != NULL);
9617 9615 sctps = ipst->ips_netstack->netstack_sctp;
9618 9616
9619 9617 if (mpctl == NULL || mpctl->b_cont == NULL) {
9620 9618 return (0);
9621 9619 }
9622 9620
9623 9621 /*
9624 9622 * For the purposes of the (broken) packet shell use
9625 9623 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9626 9624 * to make TCP and UDP appear first in the list of mib items.
9627 9625 * TBD: We could expand this and use it in netstat so that
9628 9626 * the kernel doesn't have to produce large tables (connections,
9629 9627 * routes, etc) when netstat only wants the statistics or a particular
9630 9628 * table.
9631 9629 */
9632 9630 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9633 9631 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9634 9632 return (1);
9635 9633 }
9636 9634 }
9637 9635
9638 9636 if (level != MIB2_TCP) {
9639 9637 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9640 9638 return (1);
9641 9639 }
9642 9640 }
9643 9641
9644 9642 if (level != MIB2_UDP) {
9645 9643 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9646 9644 return (1);
9647 9645 }
9648 9646 }
9649 9647
9650 9648 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9651 9649 ipst, legacy_req)) == NULL) {
9652 9650 return (1);
9653 9651 }
9654 9652
9655 9653 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9656 9654 legacy_req)) == NULL) {
9657 9655 return (1);
9658 9656 }
9659 9657
9660 9658 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9661 9659 return (1);
9662 9660 }
9663 9661
9664 9662 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9665 9663 return (1);
9666 9664 }
9667 9665
9668 9666 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9669 9667 return (1);
9670 9668 }
9671 9669
9672 9670 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9673 9671 return (1);
9674 9672 }
9675 9673
9676 9674 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9677 9675 legacy_req)) == NULL) {
9678 9676 return (1);
9679 9677 }
9680 9678
9681 9679 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9682 9680 legacy_req)) == NULL) {
9683 9681 return (1);
9684 9682 }
9685 9683
9686 9684 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9687 9685 return (1);
9688 9686 }
9689 9687
9690 9688 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9691 9689 return (1);
9692 9690 }
9693 9691
9694 9692 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9695 9693 return (1);
9696 9694 }
9697 9695
9698 9696 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9699 9697 return (1);
9700 9698 }
9701 9699
9702 9700 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9703 9701 return (1);
9704 9702 }
9705 9703
9706 9704 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9707 9705 return (1);
9708 9706 }
9709 9707
9710 9708 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9711 9709 if (mpctl == NULL)
9712 9710 return (1);
9713 9711
9714 9712 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9715 9713 if (mpctl == NULL)
9716 9714 return (1);
9717 9715
9718 9716 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9719 9717 return (1);
9720 9718 }
9721 9719 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9722 9720 return (1);
9723 9721 }
9724 9722 freemsg(mpctl);
9725 9723 return (1);
9726 9724 }
9727 9725
9728 9726 /* Get global (legacy) IPv4 statistics */
9729 9727 static mblk_t *
9730 9728 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9731 9729 ip_stack_t *ipst, boolean_t legacy_req)
9732 9730 {
9733 9731 mib2_ip_t old_ip_mib;
9734 9732 struct opthdr *optp;
9735 9733 mblk_t *mp2ctl;
9736 9734 mib2_ipAddrEntry_t mae;
9737 9735
9738 9736 /*
9739 9737 * make a copy of the original message
9740 9738 */
9741 9739 mp2ctl = copymsg(mpctl);
9742 9740
9743 9741 /* fixed length IP structure... */
9744 9742 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9745 9743 optp->level = MIB2_IP;
9746 9744 optp->name = 0;
9747 9745 SET_MIB(old_ip_mib.ipForwarding,
9748 9746 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9749 9747 SET_MIB(old_ip_mib.ipDefaultTTL,
9750 9748 (uint32_t)ipst->ips_ip_def_ttl);
9751 9749 SET_MIB(old_ip_mib.ipReasmTimeout,
9752 9750 ipst->ips_ip_reassembly_timeout);
9753 9751 SET_MIB(old_ip_mib.ipAddrEntrySize,
9754 9752 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9755 9753 sizeof (mib2_ipAddrEntry_t));
9756 9754 SET_MIB(old_ip_mib.ipRouteEntrySize,
9757 9755 sizeof (mib2_ipRouteEntry_t));
9758 9756 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9759 9757 sizeof (mib2_ipNetToMediaEntry_t));
9760 9758 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9761 9759 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9762 9760 SET_MIB(old_ip_mib.ipRouteAttributeSize,
9763 9761 sizeof (mib2_ipAttributeEntry_t));
9764 9762 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9765 9763 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9766 9764
9767 9765 /*
9768 9766 * Grab the statistics from the new IP MIB
9769 9767 */
9770 9768 SET_MIB(old_ip_mib.ipInReceives,
9771 9769 (uint32_t)ipmib->ipIfStatsHCInReceives);
9772 9770 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9773 9771 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9774 9772 SET_MIB(old_ip_mib.ipForwDatagrams,
9775 9773 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9776 9774 SET_MIB(old_ip_mib.ipInUnknownProtos,
9777 9775 ipmib->ipIfStatsInUnknownProtos);
9778 9776 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9779 9777 SET_MIB(old_ip_mib.ipInDelivers,
9780 9778 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9781 9779 SET_MIB(old_ip_mib.ipOutRequests,
9782 9780 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9783 9781 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9784 9782 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9785 9783 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9786 9784 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9787 9785 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9788 9786 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9789 9787 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9790 9788 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9791 9789
9792 9790 /* ipRoutingDiscards is not being used */
9793 9791 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9794 9792 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9795 9793 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9796 9794 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9797 9795 SET_MIB(old_ip_mib.ipReasmDuplicates,
9798 9796 ipmib->ipIfStatsReasmDuplicates);
9799 9797 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9800 9798 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9801 9799 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9802 9800 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9803 9801 SET_MIB(old_ip_mib.rawipInOverflows,
9804 9802 ipmib->rawipIfStatsInOverflows);
9805 9803
9806 9804 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9807 9805 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9808 9806 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9809 9807 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9810 9808 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9811 9809 ipmib->ipIfStatsOutSwitchIPVersion);
9812 9810
9813 9811 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9814 9812 (int)sizeof (old_ip_mib))) {
9815 9813 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9816 9814 (uint_t)sizeof (old_ip_mib)));
9817 9815 }
9818 9816
9819 9817 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9820 9818 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9821 9819 (int)optp->level, (int)optp->name, (int)optp->len));
9822 9820 qreply(q, mpctl);
9823 9821 return (mp2ctl);
9824 9822 }
9825 9823
9826 9824 /* Per interface IPv4 statistics */
9827 9825 static mblk_t *
9828 9826 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9829 9827 boolean_t legacy_req)
9830 9828 {
9831 9829 struct opthdr *optp;
9832 9830 mblk_t *mp2ctl;
9833 9831 ill_t *ill;
9834 9832 ill_walk_context_t ctx;
9835 9833 mblk_t *mp_tail = NULL;
9836 9834 mib2_ipIfStatsEntry_t global_ip_mib;
9837 9835 mib2_ipAddrEntry_t mae;
9838 9836
9839 9837 /*
9840 9838 * Make a copy of the original message
9841 9839 */
9842 9840 mp2ctl = copymsg(mpctl);
9843 9841
9844 9842 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9845 9843 optp->level = MIB2_IP;
9846 9844 optp->name = MIB2_IP_TRAFFIC_STATS;
9847 9845 /* Include "unknown interface" ip_mib */
9848 9846 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9849 9847 ipst->ips_ip_mib.ipIfStatsIfIndex =
9850 9848 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9851 9849 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9852 9850 (ipst->ips_ip_forwarding ? 1 : 2));
9853 9851 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9854 9852 (uint32_t)ipst->ips_ip_def_ttl);
9855 9853 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9856 9854 sizeof (mib2_ipIfStatsEntry_t));
9857 9855 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9858 9856 sizeof (mib2_ipAddrEntry_t));
9859 9857 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9860 9858 sizeof (mib2_ipRouteEntry_t));
9861 9859 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9862 9860 sizeof (mib2_ipNetToMediaEntry_t));
9863 9861 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9864 9862 sizeof (ip_member_t));
9865 9863 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9866 9864 sizeof (ip_grpsrc_t));
9867 9865
9868 9866 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9869 9867
9870 9868 if (legacy_req) {
9871 9869 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9872 9870 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9873 9871 }
9874 9872
9875 9873 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9876 9874 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9877 9875 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9878 9876 "failed to allocate %u bytes\n",
9879 9877 (uint_t)sizeof (global_ip_mib)));
9880 9878 }
9881 9879
9882 9880 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9883 9881 ill = ILL_START_WALK_V4(&ctx, ipst);
9884 9882 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9885 9883 ill->ill_ip_mib->ipIfStatsIfIndex =
9886 9884 ill->ill_phyint->phyint_ifindex;
9887 9885 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9888 9886 (ipst->ips_ip_forwarding ? 1 : 2));
9889 9887 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9890 9888 (uint32_t)ipst->ips_ip_def_ttl);
9891 9889
9892 9890 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9893 9891 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9894 9892 (char *)ill->ill_ip_mib,
9895 9893 (int)sizeof (*ill->ill_ip_mib))) {
9896 9894 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9897 9895 "failed to allocate %u bytes\n",
9898 9896 (uint_t)sizeof (*ill->ill_ip_mib)));
9899 9897 }
9900 9898 }
9901 9899 rw_exit(&ipst->ips_ill_g_lock);
9902 9900
9903 9901 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9904 9902 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9905 9903 "level %d, name %d, len %d\n",
9906 9904 (int)optp->level, (int)optp->name, (int)optp->len));
9907 9905 qreply(q, mpctl);
9908 9906
9909 9907 if (mp2ctl == NULL)
9910 9908 return (NULL);
9911 9909
9912 9910 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9913 9911 legacy_req));
9914 9912 }
9915 9913
9916 9914 /* Global IPv4 ICMP statistics */
9917 9915 static mblk_t *
9918 9916 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9919 9917 {
9920 9918 struct opthdr *optp;
9921 9919 mblk_t *mp2ctl;
9922 9920
9923 9921 /*
9924 9922 * Make a copy of the original message
9925 9923 */
9926 9924 mp2ctl = copymsg(mpctl);
9927 9925
9928 9926 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9929 9927 optp->level = MIB2_ICMP;
9930 9928 optp->name = 0;
9931 9929 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9932 9930 (int)sizeof (ipst->ips_icmp_mib))) {
9933 9931 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9934 9932 (uint_t)sizeof (ipst->ips_icmp_mib)));
9935 9933 }
9936 9934 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9937 9935 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9938 9936 (int)optp->level, (int)optp->name, (int)optp->len));
9939 9937 qreply(q, mpctl);
9940 9938 return (mp2ctl);
9941 9939 }
9942 9940
9943 9941 /* Global IPv4 IGMP statistics */
9944 9942 static mblk_t *
9945 9943 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9946 9944 {
9947 9945 struct opthdr *optp;
9948 9946 mblk_t *mp2ctl;
9949 9947
9950 9948 /*
9951 9949 * make a copy of the original message
9952 9950 */
9953 9951 mp2ctl = copymsg(mpctl);
9954 9952
9955 9953 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9956 9954 optp->level = EXPER_IGMP;
9957 9955 optp->name = 0;
9958 9956 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9959 9957 (int)sizeof (ipst->ips_igmpstat))) {
9960 9958 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9961 9959 (uint_t)sizeof (ipst->ips_igmpstat)));
9962 9960 }
9963 9961 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9964 9962 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9965 9963 (int)optp->level, (int)optp->name, (int)optp->len));
9966 9964 qreply(q, mpctl);
9967 9965 return (mp2ctl);
9968 9966 }
9969 9967
9970 9968 /* Global IPv4 Multicast Routing statistics */
9971 9969 static mblk_t *
9972 9970 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9973 9971 {
9974 9972 struct opthdr *optp;
9975 9973 mblk_t *mp2ctl;
9976 9974
9977 9975 /*
9978 9976 * make a copy of the original message
9979 9977 */
9980 9978 mp2ctl = copymsg(mpctl);
9981 9979
9982 9980 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9983 9981 optp->level = EXPER_DVMRP;
9984 9982 optp->name = 0;
9985 9983 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9986 9984 ip0dbg(("ip_mroute_stats: failed\n"));
9987 9985 }
9988 9986 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9989 9987 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9990 9988 (int)optp->level, (int)optp->name, (int)optp->len));
9991 9989 qreply(q, mpctl);
9992 9990 return (mp2ctl);
9993 9991 }
9994 9992
9995 9993 /* IPv4 address information */
9996 9994 static mblk_t *
9997 9995 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9998 9996 boolean_t legacy_req)
9999 9997 {
10000 9998 struct opthdr *optp;
10001 9999 mblk_t *mp2ctl;
10002 10000 mblk_t *mp_tail = NULL;
10003 10001 ill_t *ill;
10004 10002 ipif_t *ipif;
10005 10003 uint_t bitval;
10006 10004 mib2_ipAddrEntry_t mae;
10007 10005 size_t mae_size;
10008 10006 zoneid_t zoneid;
10009 10007 ill_walk_context_t ctx;
10010 10008
10011 10009 /*
10012 10010 * make a copy of the original message
10013 10011 */
10014 10012 mp2ctl = copymsg(mpctl);
10015 10013
10016 10014 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
10017 10015 sizeof (mib2_ipAddrEntry_t);
10018 10016
10019 10017 /* ipAddrEntryTable */
10020 10018
10021 10019 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10022 10020 optp->level = MIB2_IP;
10023 10021 optp->name = MIB2_IP_ADDR;
10024 10022 zoneid = Q_TO_CONN(q)->conn_zoneid;
10025 10023
10026 10024 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10027 10025 ill = ILL_START_WALK_V4(&ctx, ipst);
10028 10026 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10029 10027 for (ipif = ill->ill_ipif; ipif != NULL;
10030 10028 ipif = ipif->ipif_next) {
10031 10029 if (ipif->ipif_zoneid != zoneid &&
10032 10030 ipif->ipif_zoneid != ALL_ZONES)
10033 10031 continue;
10034 10032 /* Sum of count from dead IRE_LO* and our current */
10035 10033 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10036 10034 if (ipif->ipif_ire_local != NULL) {
10037 10035 mae.ipAdEntInfo.ae_ibcnt +=
10038 10036 ipif->ipif_ire_local->ire_ib_pkt_count;
10039 10037 }
10040 10038 mae.ipAdEntInfo.ae_obcnt = 0;
10041 10039 mae.ipAdEntInfo.ae_focnt = 0;
10042 10040
10043 10041 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
10044 10042 OCTET_LENGTH);
10045 10043 mae.ipAdEntIfIndex.o_length =
10046 10044 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
10047 10045 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
10048 10046 mae.ipAdEntNetMask = ipif->ipif_net_mask;
10049 10047 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
10050 10048 mae.ipAdEntInfo.ae_subnet_len =
10051 10049 ip_mask_to_plen(ipif->ipif_net_mask);
10052 10050 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
10053 10051 for (bitval = 1;
10054 10052 bitval &&
10055 10053 !(bitval & ipif->ipif_brd_addr);
10056 10054 bitval <<= 1)
10057 10055 noop;
10058 10056 mae.ipAdEntBcastAddr = bitval;
10059 10057 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10060 10058 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10061 10059 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
10062 10060 mae.ipAdEntInfo.ae_broadcast_addr =
10063 10061 ipif->ipif_brd_addr;
10064 10062 mae.ipAdEntInfo.ae_pp_dst_addr =
10065 10063 ipif->ipif_pp_dst_addr;
10066 10064 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10067 10065 ill->ill_flags | ill->ill_phyint->phyint_flags;
10068 10066 mae.ipAdEntRetransmitTime =
10069 10067 ill->ill_reachable_retrans_time;
10070 10068
10071 10069 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10072 10070 (char *)&mae, (int)mae_size)) {
10073 10071 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10074 10072 "allocate %u bytes\n", (uint_t)mae_size));
10075 10073 }
10076 10074 }
10077 10075 }
10078 10076 rw_exit(&ipst->ips_ill_g_lock);
10079 10077
10080 10078 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10081 10079 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10082 10080 (int)optp->level, (int)optp->name, (int)optp->len));
10083 10081 qreply(q, mpctl);
10084 10082 return (mp2ctl);
10085 10083 }
10086 10084
10087 10085 /* IPv6 address information */
10088 10086 static mblk_t *
10089 10087 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10090 10088 boolean_t legacy_req)
10091 10089 {
10092 10090 struct opthdr *optp;
10093 10091 mblk_t *mp2ctl;
10094 10092 mblk_t *mp_tail = NULL;
10095 10093 ill_t *ill;
10096 10094 ipif_t *ipif;
10097 10095 mib2_ipv6AddrEntry_t mae6;
10098 10096 size_t mae6_size;
10099 10097 zoneid_t zoneid;
10100 10098 ill_walk_context_t ctx;
10101 10099
10102 10100 /*
10103 10101 * make a copy of the original message
10104 10102 */
10105 10103 mp2ctl = copymsg(mpctl);
10106 10104
10107 10105 mae6_size = (legacy_req) ?
10108 10106 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10109 10107 sizeof (mib2_ipv6AddrEntry_t);
10110 10108
10111 10109 /* ipv6AddrEntryTable */
10112 10110
10113 10111 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10114 10112 optp->level = MIB2_IP6;
10115 10113 optp->name = MIB2_IP6_ADDR;
10116 10114 zoneid = Q_TO_CONN(q)->conn_zoneid;
10117 10115
10118 10116 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10119 10117 ill = ILL_START_WALK_V6(&ctx, ipst);
10120 10118 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10121 10119 for (ipif = ill->ill_ipif; ipif != NULL;
10122 10120 ipif = ipif->ipif_next) {
10123 10121 if (ipif->ipif_zoneid != zoneid &&
10124 10122 ipif->ipif_zoneid != ALL_ZONES)
10125 10123 continue;
10126 10124 /* Sum of count from dead IRE_LO* and our current */
10127 10125 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10128 10126 if (ipif->ipif_ire_local != NULL) {
10129 10127 mae6.ipv6AddrInfo.ae_ibcnt +=
10130 10128 ipif->ipif_ire_local->ire_ib_pkt_count;
10131 10129 }
10132 10130 mae6.ipv6AddrInfo.ae_obcnt = 0;
10133 10131 mae6.ipv6AddrInfo.ae_focnt = 0;
10134 10132
10135 10133 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10136 10134 OCTET_LENGTH);
10137 10135 mae6.ipv6AddrIfIndex.o_length =
10138 10136 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10139 10137 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10140 10138 mae6.ipv6AddrPfxLength =
10141 10139 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10142 10140 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10143 10141 mae6.ipv6AddrInfo.ae_subnet_len =
10144 10142 mae6.ipv6AddrPfxLength;
10145 10143 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10146 10144
10147 10145 /* Type: stateless(1), stateful(2), unknown(3) */
10148 10146 if (ipif->ipif_flags & IPIF_ADDRCONF)
10149 10147 mae6.ipv6AddrType = 1;
10150 10148 else
10151 10149 mae6.ipv6AddrType = 2;
10152 10150 /* Anycast: true(1), false(2) */
10153 10151 if (ipif->ipif_flags & IPIF_ANYCAST)
10154 10152 mae6.ipv6AddrAnycastFlag = 1;
10155 10153 else
10156 10154 mae6.ipv6AddrAnycastFlag = 2;
10157 10155
10158 10156 /*
10159 10157 * Address status: preferred(1), deprecated(2),
10160 10158 * invalid(3), inaccessible(4), unknown(5)
10161 10159 */
10162 10160 if (ipif->ipif_flags & IPIF_NOLOCAL)
10163 10161 mae6.ipv6AddrStatus = 3;
10164 10162 else if (ipif->ipif_flags & IPIF_DEPRECATED)
10165 10163 mae6.ipv6AddrStatus = 2;
10166 10164 else
10167 10165 mae6.ipv6AddrStatus = 1;
10168 10166 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10169 10167 mae6.ipv6AddrInfo.ae_metric =
10170 10168 ipif->ipif_ill->ill_metric;
10171 10169 mae6.ipv6AddrInfo.ae_pp_dst_addr =
10172 10170 ipif->ipif_v6pp_dst_addr;
10173 10171 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10174 10172 ill->ill_flags | ill->ill_phyint->phyint_flags;
10175 10173 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10176 10174 mae6.ipv6AddrIdentifier = ill->ill_token;
10177 10175 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10178 10176 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10179 10177 mae6.ipv6AddrRetransmitTime =
10180 10178 ill->ill_reachable_retrans_time;
10181 10179 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10182 10180 (char *)&mae6, (int)mae6_size)) {
10183 10181 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10184 10182 "allocate %u bytes\n",
10185 10183 (uint_t)mae6_size));
10186 10184 }
10187 10185 }
10188 10186 }
10189 10187 rw_exit(&ipst->ips_ill_g_lock);
10190 10188
10191 10189 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10192 10190 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10193 10191 (int)optp->level, (int)optp->name, (int)optp->len));
10194 10192 qreply(q, mpctl);
10195 10193 return (mp2ctl);
10196 10194 }
10197 10195
10198 10196 /* IPv4 multicast group membership. */
10199 10197 static mblk_t *
10200 10198 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10201 10199 {
10202 10200 struct opthdr *optp;
10203 10201 mblk_t *mp2ctl;
10204 10202 ill_t *ill;
10205 10203 ipif_t *ipif;
10206 10204 ilm_t *ilm;
10207 10205 ip_member_t ipm;
10208 10206 mblk_t *mp_tail = NULL;
10209 10207 ill_walk_context_t ctx;
10210 10208 zoneid_t zoneid;
10211 10209
10212 10210 /*
10213 10211 * make a copy of the original message
10214 10212 */
10215 10213 mp2ctl = copymsg(mpctl);
10216 10214 zoneid = Q_TO_CONN(q)->conn_zoneid;
10217 10215
10218 10216 /* ipGroupMember table */
10219 10217 optp = (struct opthdr *)&mpctl->b_rptr[
10220 10218 sizeof (struct T_optmgmt_ack)];
10221 10219 optp->level = MIB2_IP;
10222 10220 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10223 10221
10224 10222 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10225 10223 ill = ILL_START_WALK_V4(&ctx, ipst);
10226 10224 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10227 10225 /* Make sure the ill isn't going away. */
10228 10226 if (!ill_check_and_refhold(ill))
10229 10227 continue;
10230 10228 rw_exit(&ipst->ips_ill_g_lock);
10231 10229 rw_enter(&ill->ill_mcast_lock, RW_READER);
10232 10230 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10233 10231 if (ilm->ilm_zoneid != zoneid &&
10234 10232 ilm->ilm_zoneid != ALL_ZONES)
10235 10233 continue;
10236 10234
10237 10235 /* Is there an ipif for ilm_ifaddr? */
10238 10236 for (ipif = ill->ill_ipif; ipif != NULL;
10239 10237 ipif = ipif->ipif_next) {
10240 10238 if (!IPIF_IS_CONDEMNED(ipif) &&
10241 10239 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10242 10240 ilm->ilm_ifaddr != INADDR_ANY)
10243 10241 break;
10244 10242 }
10245 10243 if (ipif != NULL) {
10246 10244 ipif_get_name(ipif,
10247 10245 ipm.ipGroupMemberIfIndex.o_bytes,
10248 10246 OCTET_LENGTH);
10249 10247 } else {
10250 10248 ill_get_name(ill,
10251 10249 ipm.ipGroupMemberIfIndex.o_bytes,
10252 10250 OCTET_LENGTH);
10253 10251 }
10254 10252 ipm.ipGroupMemberIfIndex.o_length =
10255 10253 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10256 10254
10257 10255 ipm.ipGroupMemberAddress = ilm->ilm_addr;
10258 10256 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10259 10257 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10260 10258 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10261 10259 (char *)&ipm, (int)sizeof (ipm))) {
10262 10260 ip1dbg(("ip_snmp_get_mib2_ip_group: "
10263 10261 "failed to allocate %u bytes\n",
10264 10262 (uint_t)sizeof (ipm)));
10265 10263 }
10266 10264 }
10267 10265 rw_exit(&ill->ill_mcast_lock);
10268 10266 ill_refrele(ill);
10269 10267 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10270 10268 }
10271 10269 rw_exit(&ipst->ips_ill_g_lock);
10272 10270 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10273 10271 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10274 10272 (int)optp->level, (int)optp->name, (int)optp->len));
10275 10273 qreply(q, mpctl);
10276 10274 return (mp2ctl);
10277 10275 }
10278 10276
10279 10277 /* IPv6 multicast group membership. */
10280 10278 static mblk_t *
10281 10279 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10282 10280 {
10283 10281 struct opthdr *optp;
10284 10282 mblk_t *mp2ctl;
10285 10283 ill_t *ill;
10286 10284 ilm_t *ilm;
10287 10285 ipv6_member_t ipm6;
10288 10286 mblk_t *mp_tail = NULL;
10289 10287 ill_walk_context_t ctx;
10290 10288 zoneid_t zoneid;
10291 10289
10292 10290 /*
10293 10291 * make a copy of the original message
10294 10292 */
10295 10293 mp2ctl = copymsg(mpctl);
10296 10294 zoneid = Q_TO_CONN(q)->conn_zoneid;
10297 10295
10298 10296 /* ip6GroupMember table */
10299 10297 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10300 10298 optp->level = MIB2_IP6;
10301 10299 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10302 10300
10303 10301 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10304 10302 ill = ILL_START_WALK_V6(&ctx, ipst);
10305 10303 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10306 10304 /* Make sure the ill isn't going away. */
10307 10305 if (!ill_check_and_refhold(ill))
10308 10306 continue;
10309 10307 rw_exit(&ipst->ips_ill_g_lock);
10310 10308 /*
10311 10309 * Normally we don't have any members on under IPMP interfaces.
10312 10310 * We report them as a debugging aid.
10313 10311 */
10314 10312 rw_enter(&ill->ill_mcast_lock, RW_READER);
10315 10313 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10316 10314 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10317 10315 if (ilm->ilm_zoneid != zoneid &&
10318 10316 ilm->ilm_zoneid != ALL_ZONES)
10319 10317 continue; /* not this zone */
10320 10318 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10321 10319 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10322 10320 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10323 10321 if (!snmp_append_data2(mpctl->b_cont,
10324 10322 &mp_tail,
10325 10323 (char *)&ipm6, (int)sizeof (ipm6))) {
10326 10324 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10327 10325 "failed to allocate %u bytes\n",
10328 10326 (uint_t)sizeof (ipm6)));
10329 10327 }
10330 10328 }
10331 10329 rw_exit(&ill->ill_mcast_lock);
10332 10330 ill_refrele(ill);
10333 10331 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10334 10332 }
10335 10333 rw_exit(&ipst->ips_ill_g_lock);
10336 10334
10337 10335 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10338 10336 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10339 10337 (int)optp->level, (int)optp->name, (int)optp->len));
10340 10338 qreply(q, mpctl);
10341 10339 return (mp2ctl);
10342 10340 }
10343 10341
10344 10342 /* IP multicast filtered sources */
10345 10343 static mblk_t *
10346 10344 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10347 10345 {
10348 10346 struct opthdr *optp;
10349 10347 mblk_t *mp2ctl;
10350 10348 ill_t *ill;
10351 10349 ipif_t *ipif;
10352 10350 ilm_t *ilm;
10353 10351 ip_grpsrc_t ips;
10354 10352 mblk_t *mp_tail = NULL;
10355 10353 ill_walk_context_t ctx;
10356 10354 zoneid_t zoneid;
10357 10355 int i;
10358 10356 slist_t *sl;
10359 10357
10360 10358 /*
10361 10359 * make a copy of the original message
10362 10360 */
10363 10361 mp2ctl = copymsg(mpctl);
10364 10362 zoneid = Q_TO_CONN(q)->conn_zoneid;
10365 10363
10366 10364 /* ipGroupSource table */
10367 10365 optp = (struct opthdr *)&mpctl->b_rptr[
10368 10366 sizeof (struct T_optmgmt_ack)];
10369 10367 optp->level = MIB2_IP;
10370 10368 optp->name = EXPER_IP_GROUP_SOURCES;
10371 10369
10372 10370 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10373 10371 ill = ILL_START_WALK_V4(&ctx, ipst);
10374 10372 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10375 10373 /* Make sure the ill isn't going away. */
10376 10374 if (!ill_check_and_refhold(ill))
10377 10375 continue;
10378 10376 rw_exit(&ipst->ips_ill_g_lock);
10379 10377 rw_enter(&ill->ill_mcast_lock, RW_READER);
10380 10378 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10381 10379 sl = ilm->ilm_filter;
10382 10380 if (ilm->ilm_zoneid != zoneid &&
10383 10381 ilm->ilm_zoneid != ALL_ZONES)
10384 10382 continue;
10385 10383 if (SLIST_IS_EMPTY(sl))
10386 10384 continue;
10387 10385
10388 10386 /* Is there an ipif for ilm_ifaddr? */
10389 10387 for (ipif = ill->ill_ipif; ipif != NULL;
10390 10388 ipif = ipif->ipif_next) {
10391 10389 if (!IPIF_IS_CONDEMNED(ipif) &&
10392 10390 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10393 10391 ilm->ilm_ifaddr != INADDR_ANY)
10394 10392 break;
10395 10393 }
10396 10394 if (ipif != NULL) {
10397 10395 ipif_get_name(ipif,
10398 10396 ips.ipGroupSourceIfIndex.o_bytes,
10399 10397 OCTET_LENGTH);
10400 10398 } else {
10401 10399 ill_get_name(ill,
10402 10400 ips.ipGroupSourceIfIndex.o_bytes,
10403 10401 OCTET_LENGTH);
10404 10402 }
10405 10403 ips.ipGroupSourceIfIndex.o_length =
10406 10404 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10407 10405
10408 10406 ips.ipGroupSourceGroup = ilm->ilm_addr;
10409 10407 for (i = 0; i < sl->sl_numsrc; i++) {
10410 10408 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10411 10409 continue;
10412 10410 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10413 10411 ips.ipGroupSourceAddress);
10414 10412 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10415 10413 (char *)&ips, (int)sizeof (ips)) == 0) {
10416 10414 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10417 10415 " failed to allocate %u bytes\n",
10418 10416 (uint_t)sizeof (ips)));
10419 10417 }
10420 10418 }
10421 10419 }
10422 10420 rw_exit(&ill->ill_mcast_lock);
10423 10421 ill_refrele(ill);
10424 10422 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10425 10423 }
10426 10424 rw_exit(&ipst->ips_ill_g_lock);
10427 10425 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10428 10426 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10429 10427 (int)optp->level, (int)optp->name, (int)optp->len));
10430 10428 qreply(q, mpctl);
10431 10429 return (mp2ctl);
10432 10430 }
10433 10431
10434 10432 /* IPv6 multicast filtered sources. */
10435 10433 static mblk_t *
10436 10434 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10437 10435 {
10438 10436 struct opthdr *optp;
10439 10437 mblk_t *mp2ctl;
10440 10438 ill_t *ill;
10441 10439 ilm_t *ilm;
10442 10440 ipv6_grpsrc_t ips6;
10443 10441 mblk_t *mp_tail = NULL;
10444 10442 ill_walk_context_t ctx;
10445 10443 zoneid_t zoneid;
10446 10444 int i;
10447 10445 slist_t *sl;
10448 10446
10449 10447 /*
10450 10448 * make a copy of the original message
10451 10449 */
10452 10450 mp2ctl = copymsg(mpctl);
10453 10451 zoneid = Q_TO_CONN(q)->conn_zoneid;
10454 10452
10455 10453 /* ip6GroupMember table */
10456 10454 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10457 10455 optp->level = MIB2_IP6;
10458 10456 optp->name = EXPER_IP6_GROUP_SOURCES;
10459 10457
10460 10458 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10461 10459 ill = ILL_START_WALK_V6(&ctx, ipst);
10462 10460 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10463 10461 /* Make sure the ill isn't going away. */
10464 10462 if (!ill_check_and_refhold(ill))
10465 10463 continue;
10466 10464 rw_exit(&ipst->ips_ill_g_lock);
10467 10465 /*
10468 10466 * Normally we don't have any members on under IPMP interfaces.
10469 10467 * We report them as a debugging aid.
10470 10468 */
10471 10469 rw_enter(&ill->ill_mcast_lock, RW_READER);
10472 10470 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10473 10471 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10474 10472 sl = ilm->ilm_filter;
10475 10473 if (ilm->ilm_zoneid != zoneid &&
10476 10474 ilm->ilm_zoneid != ALL_ZONES)
10477 10475 continue;
10478 10476 if (SLIST_IS_EMPTY(sl))
10479 10477 continue;
10480 10478 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10481 10479 for (i = 0; i < sl->sl_numsrc; i++) {
10482 10480 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10483 10481 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10484 10482 (char *)&ips6, (int)sizeof (ips6))) {
10485 10483 ip1dbg(("ip_snmp_get_mib2_ip6_"
10486 10484 "group_src: failed to allocate "
10487 10485 "%u bytes\n",
10488 10486 (uint_t)sizeof (ips6)));
10489 10487 }
10490 10488 }
10491 10489 }
10492 10490 rw_exit(&ill->ill_mcast_lock);
10493 10491 ill_refrele(ill);
10494 10492 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10495 10493 }
10496 10494 rw_exit(&ipst->ips_ill_g_lock);
10497 10495
10498 10496 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10499 10497 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10500 10498 (int)optp->level, (int)optp->name, (int)optp->len));
10501 10499 qreply(q, mpctl);
10502 10500 return (mp2ctl);
10503 10501 }
10504 10502
10505 10503 /* Multicast routing virtual interface table. */
10506 10504 static mblk_t *
10507 10505 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10508 10506 {
10509 10507 struct opthdr *optp;
10510 10508 mblk_t *mp2ctl;
10511 10509
10512 10510 /*
10513 10511 * make a copy of the original message
10514 10512 */
10515 10513 mp2ctl = copymsg(mpctl);
10516 10514
10517 10515 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10518 10516 optp->level = EXPER_DVMRP;
10519 10517 optp->name = EXPER_DVMRP_VIF;
10520 10518 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10521 10519 ip0dbg(("ip_mroute_vif: failed\n"));
10522 10520 }
10523 10521 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10524 10522 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10525 10523 (int)optp->level, (int)optp->name, (int)optp->len));
10526 10524 qreply(q, mpctl);
10527 10525 return (mp2ctl);
10528 10526 }
10529 10527
10530 10528 /* Multicast routing table. */
10531 10529 static mblk_t *
10532 10530 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10533 10531 {
10534 10532 struct opthdr *optp;
10535 10533 mblk_t *mp2ctl;
10536 10534
10537 10535 /*
10538 10536 * make a copy of the original message
10539 10537 */
10540 10538 mp2ctl = copymsg(mpctl);
10541 10539
10542 10540 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10543 10541 optp->level = EXPER_DVMRP;
10544 10542 optp->name = EXPER_DVMRP_MRT;
10545 10543 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10546 10544 ip0dbg(("ip_mroute_mrt: failed\n"));
10547 10545 }
10548 10546 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10549 10547 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10550 10548 (int)optp->level, (int)optp->name, (int)optp->len));
10551 10549 qreply(q, mpctl);
10552 10550 return (mp2ctl);
10553 10551 }
10554 10552
10555 10553 /*
10556 10554 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10557 10555 * in one IRE walk.
10558 10556 */
10559 10557 static mblk_t *
10560 10558 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10561 10559 ip_stack_t *ipst)
10562 10560 {
10563 10561 struct opthdr *optp;
10564 10562 mblk_t *mp2ctl; /* Returned */
10565 10563 mblk_t *mp3ctl; /* nettomedia */
10566 10564 mblk_t *mp4ctl; /* routeattrs */
10567 10565 iproutedata_t ird;
10568 10566 zoneid_t zoneid;
10569 10567
10570 10568 /*
10571 10569 * make copies of the original message
10572 10570 * - mp2ctl is returned unchanged to the caller for his use
10573 10571 * - mpctl is sent upstream as ipRouteEntryTable
10574 10572 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10575 10573 * - mp4ctl is sent upstream as ipRouteAttributeTable
10576 10574 */
10577 10575 mp2ctl = copymsg(mpctl);
10578 10576 mp3ctl = copymsg(mpctl);
10579 10577 mp4ctl = copymsg(mpctl);
10580 10578 if (mp3ctl == NULL || mp4ctl == NULL) {
10581 10579 freemsg(mp4ctl);
10582 10580 freemsg(mp3ctl);
10583 10581 freemsg(mp2ctl);
10584 10582 freemsg(mpctl);
10585 10583 return (NULL);
10586 10584 }
10587 10585
10588 10586 bzero(&ird, sizeof (ird));
10589 10587
10590 10588 ird.ird_route.lp_head = mpctl->b_cont;
10591 10589 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10592 10590 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10593 10591 /*
10594 10592 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10595 10593 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10596 10594 * intended a temporary solution until a proper MIB API is provided
10597 10595 * that provides complete filtering/caller-opt-in.
10598 10596 */
10599 10597 if (level == EXPER_IP_AND_ALL_IRES)
10600 10598 ird.ird_flags |= IRD_REPORT_ALL;
10601 10599
10602 10600 zoneid = Q_TO_CONN(q)->conn_zoneid;
10603 10601 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10604 10602
10605 10603 /* ipRouteEntryTable in mpctl */
10606 10604 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10607 10605 optp->level = MIB2_IP;
10608 10606 optp->name = MIB2_IP_ROUTE;
10609 10607 optp->len = msgdsize(ird.ird_route.lp_head);
10610 10608 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10611 10609 (int)optp->level, (int)optp->name, (int)optp->len));
10612 10610 qreply(q, mpctl);
10613 10611
10614 10612 /* ipNetToMediaEntryTable in mp3ctl */
10615 10613 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10616 10614
10617 10615 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10618 10616 optp->level = MIB2_IP;
10619 10617 optp->name = MIB2_IP_MEDIA;
10620 10618 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10621 10619 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10622 10620 (int)optp->level, (int)optp->name, (int)optp->len));
10623 10621 qreply(q, mp3ctl);
10624 10622
10625 10623 /* ipRouteAttributeTable in mp4ctl */
10626 10624 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10627 10625 optp->level = MIB2_IP;
10628 10626 optp->name = EXPER_IP_RTATTR;
10629 10627 optp->len = msgdsize(ird.ird_attrs.lp_head);
10630 10628 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10631 10629 (int)optp->level, (int)optp->name, (int)optp->len));
10632 10630 if (optp->len == 0)
10633 10631 freemsg(mp4ctl);
10634 10632 else
10635 10633 qreply(q, mp4ctl);
10636 10634
10637 10635 return (mp2ctl);
10638 10636 }
10639 10637
10640 10638 /*
10641 10639 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10642 10640 * ipv6NetToMediaEntryTable in an NDP walk.
10643 10641 */
10644 10642 static mblk_t *
10645 10643 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10646 10644 ip_stack_t *ipst)
10647 10645 {
10648 10646 struct opthdr *optp;
10649 10647 mblk_t *mp2ctl; /* Returned */
10650 10648 mblk_t *mp3ctl; /* nettomedia */
10651 10649 mblk_t *mp4ctl; /* routeattrs */
10652 10650 iproutedata_t ird;
10653 10651 zoneid_t zoneid;
10654 10652
10655 10653 /*
10656 10654 * make copies of the original message
10657 10655 * - mp2ctl is returned unchanged to the caller for his use
10658 10656 * - mpctl is sent upstream as ipv6RouteEntryTable
10659 10657 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10660 10658 * - mp4ctl is sent upstream as ipv6RouteAttributeTable
10661 10659 */
10662 10660 mp2ctl = copymsg(mpctl);
10663 10661 mp3ctl = copymsg(mpctl);
10664 10662 mp4ctl = copymsg(mpctl);
10665 10663 if (mp3ctl == NULL || mp4ctl == NULL) {
10666 10664 freemsg(mp4ctl);
10667 10665 freemsg(mp3ctl);
10668 10666 freemsg(mp2ctl);
10669 10667 freemsg(mpctl);
10670 10668 return (NULL);
10671 10669 }
10672 10670
10673 10671 bzero(&ird, sizeof (ird));
10674 10672
10675 10673 ird.ird_route.lp_head = mpctl->b_cont;
10676 10674 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10677 10675 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10678 10676 /*
10679 10677 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10680 10678 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10681 10679 * intended a temporary solution until a proper MIB API is provided
10682 10680 * that provides complete filtering/caller-opt-in.
10683 10681 */
10684 10682 if (level == EXPER_IP_AND_ALL_IRES)
10685 10683 ird.ird_flags |= IRD_REPORT_ALL;
10686 10684
10687 10685 zoneid = Q_TO_CONN(q)->conn_zoneid;
10688 10686 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10689 10687
10690 10688 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10691 10689 optp->level = MIB2_IP6;
10692 10690 optp->name = MIB2_IP6_ROUTE;
10693 10691 optp->len = msgdsize(ird.ird_route.lp_head);
10694 10692 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10695 10693 (int)optp->level, (int)optp->name, (int)optp->len));
10696 10694 qreply(q, mpctl);
10697 10695
10698 10696 /* ipv6NetToMediaEntryTable in mp3ctl */
10699 10697 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10700 10698
10701 10699 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10702 10700 optp->level = MIB2_IP6;
10703 10701 optp->name = MIB2_IP6_MEDIA;
10704 10702 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10705 10703 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10706 10704 (int)optp->level, (int)optp->name, (int)optp->len));
10707 10705 qreply(q, mp3ctl);
10708 10706
10709 10707 /* ipv6RouteAttributeTable in mp4ctl */
10710 10708 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10711 10709 optp->level = MIB2_IP6;
10712 10710 optp->name = EXPER_IP_RTATTR;
10713 10711 optp->len = msgdsize(ird.ird_attrs.lp_head);
10714 10712 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10715 10713 (int)optp->level, (int)optp->name, (int)optp->len));
10716 10714 if (optp->len == 0)
10717 10715 freemsg(mp4ctl);
10718 10716 else
10719 10717 qreply(q, mp4ctl);
10720 10718
10721 10719 return (mp2ctl);
10722 10720 }
10723 10721
10724 10722 /*
10725 10723 * IPv6 mib: One per ill
10726 10724 */
10727 10725 static mblk_t *
10728 10726 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10729 10727 boolean_t legacy_req)
10730 10728 {
10731 10729 struct opthdr *optp;
10732 10730 mblk_t *mp2ctl;
10733 10731 ill_t *ill;
10734 10732 ill_walk_context_t ctx;
10735 10733 mblk_t *mp_tail = NULL;
10736 10734 mib2_ipv6AddrEntry_t mae6;
10737 10735 mib2_ipIfStatsEntry_t *ise;
10738 10736 size_t ise_size, iae_size;
10739 10737
10740 10738 /*
10741 10739 * Make a copy of the original message
10742 10740 */
10743 10741 mp2ctl = copymsg(mpctl);
10744 10742
10745 10743 /* fixed length IPv6 structure ... */
10746 10744
10747 10745 if (legacy_req) {
10748 10746 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10749 10747 mib2_ipIfStatsEntry_t);
10750 10748 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10751 10749 } else {
10752 10750 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10753 10751 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10754 10752 }
10755 10753
10756 10754 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10757 10755 optp->level = MIB2_IP6;
10758 10756 optp->name = 0;
10759 10757 /* Include "unknown interface" ip6_mib */
10760 10758 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10761 10759 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10762 10760 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10763 10761 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10764 10762 ipst->ips_ipv6_forwarding ? 1 : 2);
10765 10763 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10766 10764 ipst->ips_ipv6_def_hops);
10767 10765 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10768 10766 sizeof (mib2_ipIfStatsEntry_t));
10769 10767 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10770 10768 sizeof (mib2_ipv6AddrEntry_t));
10771 10769 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10772 10770 sizeof (mib2_ipv6RouteEntry_t));
10773 10771 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10774 10772 sizeof (mib2_ipv6NetToMediaEntry_t));
10775 10773 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10776 10774 sizeof (ipv6_member_t));
10777 10775 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10778 10776 sizeof (ipv6_grpsrc_t));
10779 10777
10780 10778 /*
10781 10779 * Synchronize 64- and 32-bit counters
10782 10780 */
10783 10781 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10784 10782 ipIfStatsHCInReceives);
10785 10783 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10786 10784 ipIfStatsHCInDelivers);
10787 10785 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10788 10786 ipIfStatsHCOutRequests);
10789 10787 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10790 10788 ipIfStatsHCOutForwDatagrams);
10791 10789 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10792 10790 ipIfStatsHCOutMcastPkts);
10793 10791 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10794 10792 ipIfStatsHCInMcastPkts);
10795 10793
10796 10794 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10797 10795 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10798 10796 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10799 10797 (uint_t)ise_size));
10800 10798 } else if (legacy_req) {
10801 10799 /* Adjust the EntrySize fields for legacy requests. */
10802 10800 ise =
10803 10801 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10804 10802 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10805 10803 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10806 10804 }
10807 10805
10808 10806 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10809 10807 ill = ILL_START_WALK_V6(&ctx, ipst);
10810 10808 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10811 10809 ill->ill_ip_mib->ipIfStatsIfIndex =
10812 10810 ill->ill_phyint->phyint_ifindex;
10813 10811 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10814 10812 ipst->ips_ipv6_forwarding ? 1 : 2);
10815 10813 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10816 10814 ill->ill_max_hops);
10817 10815
10818 10816 /*
10819 10817 * Synchronize 64- and 32-bit counters
10820 10818 */
10821 10819 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10822 10820 ipIfStatsHCInReceives);
10823 10821 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10824 10822 ipIfStatsHCInDelivers);
10825 10823 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10826 10824 ipIfStatsHCOutRequests);
10827 10825 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10828 10826 ipIfStatsHCOutForwDatagrams);
10829 10827 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10830 10828 ipIfStatsHCOutMcastPkts);
10831 10829 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10832 10830 ipIfStatsHCInMcastPkts);
10833 10831
10834 10832 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10835 10833 (char *)ill->ill_ip_mib, (int)ise_size)) {
10836 10834 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10837 10835 "%u bytes\n", (uint_t)ise_size));
10838 10836 } else if (legacy_req) {
10839 10837 /* Adjust the EntrySize fields for legacy requests. */
10840 10838 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10841 10839 (int)ise_size);
10842 10840 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10843 10841 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10844 10842 }
10845 10843 }
10846 10844 rw_exit(&ipst->ips_ill_g_lock);
10847 10845
10848 10846 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10849 10847 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10850 10848 (int)optp->level, (int)optp->name, (int)optp->len));
10851 10849 qreply(q, mpctl);
10852 10850 return (mp2ctl);
10853 10851 }
10854 10852
10855 10853 /*
10856 10854 * ICMPv6 mib: One per ill
10857 10855 */
10858 10856 static mblk_t *
10859 10857 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10860 10858 {
10861 10859 struct opthdr *optp;
10862 10860 mblk_t *mp2ctl;
10863 10861 ill_t *ill;
10864 10862 ill_walk_context_t ctx;
10865 10863 mblk_t *mp_tail = NULL;
10866 10864 /*
10867 10865 * Make a copy of the original message
10868 10866 */
10869 10867 mp2ctl = copymsg(mpctl);
10870 10868
10871 10869 /* fixed length ICMPv6 structure ... */
10872 10870
10873 10871 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10874 10872 optp->level = MIB2_ICMP6;
10875 10873 optp->name = 0;
10876 10874 /* Include "unknown interface" icmp6_mib */
10877 10875 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10878 10876 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10879 10877 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10880 10878 sizeof (mib2_ipv6IfIcmpEntry_t);
10881 10879 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10882 10880 (char *)&ipst->ips_icmp6_mib,
10883 10881 (int)sizeof (ipst->ips_icmp6_mib))) {
10884 10882 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10885 10883 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10886 10884 }
10887 10885
10888 10886 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10889 10887 ill = ILL_START_WALK_V6(&ctx, ipst);
10890 10888 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10891 10889 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10892 10890 ill->ill_phyint->phyint_ifindex;
10893 10891 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10894 10892 (char *)ill->ill_icmp6_mib,
10895 10893 (int)sizeof (*ill->ill_icmp6_mib))) {
10896 10894 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10897 10895 "%u bytes\n",
10898 10896 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10899 10897 }
10900 10898 }
10901 10899 rw_exit(&ipst->ips_ill_g_lock);
10902 10900
10903 10901 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10904 10902 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10905 10903 (int)optp->level, (int)optp->name, (int)optp->len));
10906 10904 qreply(q, mpctl);
10907 10905 return (mp2ctl);
10908 10906 }
10909 10907
10910 10908 /*
10911 10909 * ire_walk routine to create both ipRouteEntryTable and
10912 10910 * ipRouteAttributeTable in one IRE walk
10913 10911 */
10914 10912 static void
10915 10913 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10916 10914 {
10917 10915 ill_t *ill;
10918 10916 mib2_ipRouteEntry_t *re;
10919 10917 mib2_ipAttributeEntry_t iaes;
10920 10918 tsol_ire_gw_secattr_t *attrp;
10921 10919 tsol_gc_t *gc = NULL;
10922 10920 tsol_gcgrp_t *gcgrp = NULL;
10923 10921 ip_stack_t *ipst = ire->ire_ipst;
10924 10922
10925 10923 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10926 10924
10927 10925 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10928 10926 if (ire->ire_testhidden)
10929 10927 return;
10930 10928 if (ire->ire_type & IRE_IF_CLONE)
10931 10929 return;
10932 10930 }
10933 10931
10934 10932 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10935 10933 return;
10936 10934
10937 10935 if ((attrp = ire->ire_gw_secattr) != NULL) {
10938 10936 mutex_enter(&attrp->igsa_lock);
10939 10937 if ((gc = attrp->igsa_gc) != NULL) {
10940 10938 gcgrp = gc->gc_grp;
10941 10939 ASSERT(gcgrp != NULL);
10942 10940 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10943 10941 }
10944 10942 mutex_exit(&attrp->igsa_lock);
10945 10943 }
10946 10944 /*
10947 10945 * Return all IRE types for route table... let caller pick and choose
10948 10946 */
10949 10947 re->ipRouteDest = ire->ire_addr;
10950 10948 ill = ire->ire_ill;
10951 10949 re->ipRouteIfIndex.o_length = 0;
10952 10950 if (ill != NULL) {
10953 10951 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10954 10952 re->ipRouteIfIndex.o_length =
10955 10953 mi_strlen(re->ipRouteIfIndex.o_bytes);
10956 10954 }
10957 10955 re->ipRouteMetric1 = -1;
10958 10956 re->ipRouteMetric2 = -1;
10959 10957 re->ipRouteMetric3 = -1;
10960 10958 re->ipRouteMetric4 = -1;
10961 10959
10962 10960 re->ipRouteNextHop = ire->ire_gateway_addr;
10963 10961 /* indirect(4), direct(3), or invalid(2) */
10964 10962 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10965 10963 re->ipRouteType = 2;
10966 10964 else if (ire->ire_type & IRE_ONLINK)
10967 10965 re->ipRouteType = 3;
10968 10966 else
10969 10967 re->ipRouteType = 4;
10970 10968
10971 10969 re->ipRouteProto = -1;
10972 10970 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10973 10971 re->ipRouteMask = ire->ire_mask;
10974 10972 re->ipRouteMetric5 = -1;
10975 10973 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10976 10974 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10977 10975 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10978 10976
10979 10977 re->ipRouteInfo.re_frag_flag = 0;
10980 10978 re->ipRouteInfo.re_rtt = 0;
10981 10979 re->ipRouteInfo.re_src_addr = 0;
10982 10980 re->ipRouteInfo.re_ref = ire->ire_refcnt;
10983 10981 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10984 10982 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10985 10983 re->ipRouteInfo.re_flags = ire->ire_flags;
10986 10984
10987 10985 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10988 10986 if (ire->ire_type & IRE_INTERFACE) {
10989 10987 ire_t *child;
10990 10988
10991 10989 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10992 10990 child = ire->ire_dep_children;
10993 10991 while (child != NULL) {
10994 10992 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10995 10993 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10996 10994 child = child->ire_dep_sib_next;
10997 10995 }
10998 10996 rw_exit(&ipst->ips_ire_dep_lock);
10999 10997 }
11000 10998
11001 10999 if (ire->ire_flags & RTF_DYNAMIC) {
11002 11000 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11003 11001 } else {
11004 11002 re->ipRouteInfo.re_ire_type = ire->ire_type;
11005 11003 }
11006 11004
11007 11005 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11008 11006 (char *)re, (int)sizeof (*re))) {
11009 11007 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
11010 11008 (uint_t)sizeof (*re)));
11011 11009 }
11012 11010
11013 11011 if (gc != NULL) {
11014 11012 iaes.iae_routeidx = ird->ird_idx;
11015 11013 iaes.iae_doi = gc->gc_db->gcdb_doi;
11016 11014 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11017 11015
11018 11016 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11019 11017 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11020 11018 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
11021 11019 "bytes\n", (uint_t)sizeof (iaes)));
11022 11020 }
11023 11021 }
11024 11022
11025 11023 /* bump route index for next pass */
11026 11024 ird->ird_idx++;
11027 11025
11028 11026 kmem_free(re, sizeof (*re));
11029 11027 if (gcgrp != NULL)
11030 11028 rw_exit(&gcgrp->gcgrp_rwlock);
11031 11029 }
11032 11030
11033 11031 /*
11034 11032 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
11035 11033 */
11036 11034 static void
11037 11035 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
11038 11036 {
11039 11037 ill_t *ill;
11040 11038 mib2_ipv6RouteEntry_t *re;
11041 11039 mib2_ipAttributeEntry_t iaes;
11042 11040 tsol_ire_gw_secattr_t *attrp;
11043 11041 tsol_gc_t *gc = NULL;
11044 11042 tsol_gcgrp_t *gcgrp = NULL;
11045 11043 ip_stack_t *ipst = ire->ire_ipst;
11046 11044
11047 11045 ASSERT(ire->ire_ipversion == IPV6_VERSION);
11048 11046
11049 11047 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
11050 11048 if (ire->ire_testhidden)
11051 11049 return;
11052 11050 if (ire->ire_type & IRE_IF_CLONE)
11053 11051 return;
11054 11052 }
11055 11053
11056 11054 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11057 11055 return;
11058 11056
11059 11057 if ((attrp = ire->ire_gw_secattr) != NULL) {
11060 11058 mutex_enter(&attrp->igsa_lock);
11061 11059 if ((gc = attrp->igsa_gc) != NULL) {
11062 11060 gcgrp = gc->gc_grp;
11063 11061 ASSERT(gcgrp != NULL);
11064 11062 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11065 11063 }
11066 11064 mutex_exit(&attrp->igsa_lock);
11067 11065 }
11068 11066 /*
11069 11067 * Return all IRE types for route table... let caller pick and choose
11070 11068 */
11071 11069 re->ipv6RouteDest = ire->ire_addr_v6;
11072 11070 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11073 11071 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11074 11072 re->ipv6RouteIfIndex.o_length = 0;
11075 11073 ill = ire->ire_ill;
11076 11074 if (ill != NULL) {
11077 11075 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11078 11076 re->ipv6RouteIfIndex.o_length =
11079 11077 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11080 11078 }
11081 11079
11082 11080 ASSERT(!(ire->ire_type & IRE_BROADCAST));
11083 11081
11084 11082 mutex_enter(&ire->ire_lock);
11085 11083 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11086 11084 mutex_exit(&ire->ire_lock);
11087 11085
11088 11086 /* remote(4), local(3), or discard(2) */
11089 11087 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11090 11088 re->ipv6RouteType = 2;
11091 11089 else if (ire->ire_type & IRE_ONLINK)
11092 11090 re->ipv6RouteType = 3;
11093 11091 else
11094 11092 re->ipv6RouteType = 4;
11095 11093
11096 11094 re->ipv6RouteProtocol = -1;
11097 11095 re->ipv6RoutePolicy = 0;
11098 11096 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
11099 11097 re->ipv6RouteNextHopRDI = 0;
11100 11098 re->ipv6RouteWeight = 0;
11101 11099 re->ipv6RouteMetric = 0;
11102 11100 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11103 11101 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11104 11102 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11105 11103
11106 11104 re->ipv6RouteInfo.re_frag_flag = 0;
11107 11105 re->ipv6RouteInfo.re_rtt = 0;
11108 11106 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
11109 11107 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
11110 11108 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
11111 11109 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
11112 11110 re->ipv6RouteInfo.re_flags = ire->ire_flags;
11113 11111
11114 11112 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11115 11113 if (ire->ire_type & IRE_INTERFACE) {
11116 11114 ire_t *child;
11117 11115
11118 11116 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11119 11117 child = ire->ire_dep_children;
11120 11118 while (child != NULL) {
11121 11119 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11122 11120 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11123 11121 child = child->ire_dep_sib_next;
11124 11122 }
11125 11123 rw_exit(&ipst->ips_ire_dep_lock);
11126 11124 }
11127 11125 if (ire->ire_flags & RTF_DYNAMIC) {
11128 11126 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11129 11127 } else {
11130 11128 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
11131 11129 }
11132 11130
11133 11131 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11134 11132 (char *)re, (int)sizeof (*re))) {
11135 11133 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11136 11134 (uint_t)sizeof (*re)));
11137 11135 }
11138 11136
11139 11137 if (gc != NULL) {
11140 11138 iaes.iae_routeidx = ird->ird_idx;
11141 11139 iaes.iae_doi = gc->gc_db->gcdb_doi;
11142 11140 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11143 11141
11144 11142 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11145 11143 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11146 11144 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11147 11145 "bytes\n", (uint_t)sizeof (iaes)));
11148 11146 }
11149 11147 }
11150 11148
11151 11149 /* bump route index for next pass */
11152 11150 ird->ird_idx++;
11153 11151
11154 11152 kmem_free(re, sizeof (*re));
11155 11153 if (gcgrp != NULL)
11156 11154 rw_exit(&gcgrp->gcgrp_rwlock);
11157 11155 }
11158 11156
11159 11157 /*
11160 11158 * ncec_walk routine to create ipv6NetToMediaEntryTable
11161 11159 */
11162 11160 static int
11163 11161 ip_snmp_get2_v6_media(ncec_t *ncec, iproutedata_t *ird)
11164 11162 {
11165 11163 ill_t *ill;
11166 11164 mib2_ipv6NetToMediaEntry_t ntme;
11167 11165
11168 11166 ill = ncec->ncec_ill;
11169 11167 /* skip arpce entries, and loopback ncec entries */
11170 11168 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11171 11169 return (0);
11172 11170 /*
11173 11171 * Neighbor cache entry attached to IRE with on-link
11174 11172 * destination.
11175 11173 * We report all IPMP groups on ncec_ill which is normally the upper.
11176 11174 */
11177 11175 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11178 11176 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11179 11177 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11180 11178 if (ncec->ncec_lladdr != NULL) {
11181 11179 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11182 11180 ntme.ipv6NetToMediaPhysAddress.o_length);
11183 11181 }
11184 11182 /*
11185 11183 * Note: Returns ND_* states. Should be:
11186 11184 * reachable(1), stale(2), delay(3), probe(4),
11187 11185 * invalid(5), unknown(6)
11188 11186 */
11189 11187 ntme.ipv6NetToMediaState = ncec->ncec_state;
11190 11188 ntme.ipv6NetToMediaLastUpdated = 0;
11191 11189
11192 11190 /* other(1), dynamic(2), static(3), local(4) */
11193 11191 if (NCE_MYADDR(ncec)) {
11194 11192 ntme.ipv6NetToMediaType = 4;
11195 11193 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11196 11194 ntme.ipv6NetToMediaType = 1; /* proxy */
11197 11195 } else if (ncec->ncec_flags & NCE_F_STATIC) {
11198 11196 ntme.ipv6NetToMediaType = 3;
11199 11197 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11200 11198 ntme.ipv6NetToMediaType = 1;
11201 11199 } else {
11202 11200 ntme.ipv6NetToMediaType = 2;
11203 11201 }
11204 11202
11205 11203 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11206 11204 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11207 11205 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11208 11206 (uint_t)sizeof (ntme)));
11209 11207 }
11210 11208 return (0);
11211 11209 }
11212 11210
11213 11211 int
11214 11212 nce2ace(ncec_t *ncec)
11215 11213 {
11216 11214 int flags = 0;
11217 11215
11218 11216 if (NCE_ISREACHABLE(ncec))
11219 11217 flags |= ACE_F_RESOLVED;
11220 11218 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11221 11219 flags |= ACE_F_AUTHORITY;
11222 11220 if (ncec->ncec_flags & NCE_F_PUBLISH)
11223 11221 flags |= ACE_F_PUBLISH;
11224 11222 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11225 11223 flags |= ACE_F_PERMANENT;
11226 11224 if (NCE_MYADDR(ncec))
11227 11225 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11228 11226 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11229 11227 flags |= ACE_F_UNVERIFIED;
11230 11228 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11231 11229 flags |= ACE_F_AUTHORITY;
11232 11230 if (ncec->ncec_flags & NCE_F_DELAYED)
11233 11231 flags |= ACE_F_DELAYED;
11234 11232 return (flags);
11235 11233 }
11236 11234
11237 11235 /*
11238 11236 * ncec_walk routine to create ipNetToMediaEntryTable
11239 11237 */
11240 11238 static int
11241 11239 ip_snmp_get2_v4_media(ncec_t *ncec, iproutedata_t *ird)
11242 11240 {
11243 11241 ill_t *ill;
11244 11242 mib2_ipNetToMediaEntry_t ntme;
11245 11243 const char *name = "unknown";
11246 11244 ipaddr_t ncec_addr;
11247 11245
11248 11246 ill = ncec->ncec_ill;
11249 11247 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11250 11248 ill->ill_net_type == IRE_LOOPBACK)
11251 11249 return (0);
11252 11250
11253 11251 /* We report all IPMP groups on ncec_ill which is normally the upper. */
11254 11252 name = ill->ill_name;
11255 11253 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11256 11254 if (NCE_MYADDR(ncec)) {
11257 11255 ntme.ipNetToMediaType = 4;
11258 11256 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11259 11257 ntme.ipNetToMediaType = 1;
11260 11258 } else {
11261 11259 ntme.ipNetToMediaType = 3;
11262 11260 }
11263 11261 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11264 11262 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11265 11263 ntme.ipNetToMediaIfIndex.o_length);
11266 11264
11267 11265 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11268 11266 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11269 11267
11270 11268 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11271 11269 ncec_addr = INADDR_BROADCAST;
11272 11270 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11273 11271 sizeof (ncec_addr));
11274 11272 /*
11275 11273 * map all the flags to the ACE counterpart.
11276 11274 */
11277 11275 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11278 11276
11279 11277 ntme.ipNetToMediaPhysAddress.o_length =
11280 11278 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11281 11279
11282 11280 if (!NCE_ISREACHABLE(ncec))
11283 11281 ntme.ipNetToMediaPhysAddress.o_length = 0;
11284 11282 else {
11285 11283 if (ncec->ncec_lladdr != NULL) {
11286 11284 bcopy(ncec->ncec_lladdr,
11287 11285 ntme.ipNetToMediaPhysAddress.o_bytes,
11288 11286 ntme.ipNetToMediaPhysAddress.o_length);
11289 11287 }
11290 11288 }
11291 11289
11292 11290 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11293 11291 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11294 11292 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11295 11293 (uint_t)sizeof (ntme)));
11296 11294 }
11297 11295 return (0);
11298 11296 }
11299 11297
11300 11298 /*
11301 11299 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11302 11300 */
11303 11301 /* ARGSUSED */
11304 11302 int
11305 11303 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11306 11304 {
11307 11305 switch (level) {
11308 11306 case MIB2_IP:
11309 11307 case MIB2_ICMP:
11310 11308 switch (name) {
11311 11309 default:
11312 11310 break;
11313 11311 }
11314 11312 return (1);
11315 11313 default:
11316 11314 return (1);
11317 11315 }
11318 11316 }
11319 11317
11320 11318 /*
11321 11319 * When there exists both a 64- and 32-bit counter of a particular type
11322 11320 * (i.e., InReceives), only the 64-bit counters are added.
11323 11321 */
11324 11322 void
11325 11323 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11326 11324 {
11327 11325 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11328 11326 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11329 11327 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11330 11328 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11331 11329 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11332 11330 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11333 11331 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11334 11332 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11335 11333 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11336 11334 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11337 11335 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11338 11336 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11339 11337 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11340 11338 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11341 11339 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11342 11340 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11343 11341 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11344 11342 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11345 11343 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11346 11344 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11347 11345 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11348 11346 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11349 11347 o2->ipIfStatsInWrongIPVersion);
11350 11348 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11351 11349 o2->ipIfStatsInWrongIPVersion);
11352 11350 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11353 11351 o2->ipIfStatsOutSwitchIPVersion);
11354 11352 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11355 11353 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11356 11354 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11357 11355 o2->ipIfStatsHCInForwDatagrams);
11358 11356 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11359 11357 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11360 11358 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11361 11359 o2->ipIfStatsHCOutForwDatagrams);
11362 11360 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11363 11361 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11364 11362 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11365 11363 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11366 11364 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11367 11365 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11368 11366 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11369 11367 o2->ipIfStatsHCOutMcastOctets);
11370 11368 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11371 11369 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11372 11370 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11373 11371 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11374 11372 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11375 11373 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11376 11374 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11377 11375 }
11378 11376
11379 11377 void
11380 11378 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11381 11379 {
11382 11380 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11383 11381 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11384 11382 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11385 11383 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11386 11384 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11387 11385 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11388 11386 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11389 11387 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11390 11388 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11391 11389 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11392 11390 o2->ipv6IfIcmpInRouterSolicits);
11393 11391 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11394 11392 o2->ipv6IfIcmpInRouterAdvertisements);
11395 11393 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11396 11394 o2->ipv6IfIcmpInNeighborSolicits);
11397 11395 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11398 11396 o2->ipv6IfIcmpInNeighborAdvertisements);
11399 11397 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11400 11398 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11401 11399 o2->ipv6IfIcmpInGroupMembQueries);
11402 11400 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11403 11401 o2->ipv6IfIcmpInGroupMembResponses);
11404 11402 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11405 11403 o2->ipv6IfIcmpInGroupMembReductions);
11406 11404 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11407 11405 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11408 11406 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11409 11407 o2->ipv6IfIcmpOutDestUnreachs);
11410 11408 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11411 11409 o2->ipv6IfIcmpOutAdminProhibs);
11412 11410 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11413 11411 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11414 11412 o2->ipv6IfIcmpOutParmProblems);
11415 11413 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11416 11414 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11417 11415 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11418 11416 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11419 11417 o2->ipv6IfIcmpOutRouterSolicits);
11420 11418 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11421 11419 o2->ipv6IfIcmpOutRouterAdvertisements);
11422 11420 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11423 11421 o2->ipv6IfIcmpOutNeighborSolicits);
11424 11422 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11425 11423 o2->ipv6IfIcmpOutNeighborAdvertisements);
11426 11424 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11427 11425 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11428 11426 o2->ipv6IfIcmpOutGroupMembQueries);
11429 11427 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11430 11428 o2->ipv6IfIcmpOutGroupMembResponses);
11431 11429 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11432 11430 o2->ipv6IfIcmpOutGroupMembReductions);
11433 11431 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11434 11432 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11435 11433 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11436 11434 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11437 11435 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11438 11436 o2->ipv6IfIcmpInBadNeighborSolicitations);
11439 11437 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11440 11438 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11441 11439 o2->ipv6IfIcmpInGroupMembTotal);
11442 11440 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11443 11441 o2->ipv6IfIcmpInGroupMembBadQueries);
11444 11442 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11445 11443 o2->ipv6IfIcmpInGroupMembBadReports);
11446 11444 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11447 11445 o2->ipv6IfIcmpInGroupMembOurReports);
11448 11446 }
11449 11447
11450 11448 /*
11451 11449 * Called before the options are updated to check if this packet will
11452 11450 * be source routed from here.
11453 11451 * This routine assumes that the options are well formed i.e. that they
11454 11452 * have already been checked.
11455 11453 */
11456 11454 boolean_t
11457 11455 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11458 11456 {
11459 11457 ipoptp_t opts;
11460 11458 uchar_t *opt;
11461 11459 uint8_t optval;
11462 11460 uint8_t optlen;
11463 11461 ipaddr_t dst;
11464 11462
11465 11463 if (IS_SIMPLE_IPH(ipha)) {
11466 11464 ip2dbg(("not source routed\n"));
11467 11465 return (B_FALSE);
11468 11466 }
11469 11467 dst = ipha->ipha_dst;
11470 11468 for (optval = ipoptp_first(&opts, ipha);
11471 11469 optval != IPOPT_EOL;
11472 11470 optval = ipoptp_next(&opts)) {
11473 11471 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11474 11472 opt = opts.ipoptp_cur;
11475 11473 optlen = opts.ipoptp_len;
11476 11474 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11477 11475 optval, optlen));
11478 11476 switch (optval) {
11479 11477 uint32_t off;
11480 11478 case IPOPT_SSRR:
11481 11479 case IPOPT_LSRR:
11482 11480 /*
11483 11481 * If dst is one of our addresses and there are some
11484 11482 * entries left in the source route return (true).
11485 11483 */
11486 11484 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11487 11485 ip2dbg(("ip_source_routed: not next"
11488 11486 " source route 0x%x\n",
11489 11487 ntohl(dst)));
11490 11488 return (B_FALSE);
11491 11489 }
11492 11490 off = opt[IPOPT_OFFSET];
11493 11491 off--;
11494 11492 if (optlen < IP_ADDR_LEN ||
11495 11493 off > optlen - IP_ADDR_LEN) {
11496 11494 /* End of source route */
11497 11495 ip1dbg(("ip_source_routed: end of SR\n"));
11498 11496 return (B_FALSE);
11499 11497 }
11500 11498 return (B_TRUE);
11501 11499 }
11502 11500 }
11503 11501 ip2dbg(("not source routed\n"));
11504 11502 return (B_FALSE);
11505 11503 }
11506 11504
11507 11505 /*
11508 11506 * ip_unbind is called by the transports to remove a conn from
11509 11507 * the fanout table.
11510 11508 */
11511 11509 void
11512 11510 ip_unbind(conn_t *connp)
11513 11511 {
11514 11512
11515 11513 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11516 11514
11517 11515 if (is_system_labeled() && connp->conn_anon_port) {
11518 11516 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11519 11517 connp->conn_mlp_type, connp->conn_proto,
11520 11518 ntohs(connp->conn_lport), B_FALSE);
11521 11519 connp->conn_anon_port = 0;
11522 11520 }
11523 11521 connp->conn_mlp_type = mlptSingle;
11524 11522
11525 11523 ipcl_hash_remove(connp);
11526 11524 }
11527 11525
11528 11526 /*
11529 11527 * Used for deciding the MSS size for the upper layer. Thus
11530 11528 * we need to check the outbound policy values in the conn.
11531 11529 */
11532 11530 int
11533 11531 conn_ipsec_length(conn_t *connp)
11534 11532 {
11535 11533 ipsec_latch_t *ipl;
11536 11534
11537 11535 ipl = connp->conn_latch;
11538 11536 if (ipl == NULL)
11539 11537 return (0);
11540 11538
11541 11539 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11542 11540 return (0);
11543 11541
11544 11542 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11545 11543 }
11546 11544
11547 11545 /*
11548 11546 * Returns an estimate of the IPsec headers size. This is used if
11549 11547 * we don't want to call into IPsec to get the exact size.
11550 11548 */
11551 11549 int
11552 11550 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11553 11551 {
11554 11552 ipsec_action_t *a;
11555 11553
11556 11554 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11557 11555 return (0);
11558 11556
11559 11557 a = ixa->ixa_ipsec_action;
11560 11558 if (a == NULL) {
11561 11559 ASSERT(ixa->ixa_ipsec_policy != NULL);
11562 11560 a = ixa->ixa_ipsec_policy->ipsp_act;
11563 11561 }
11564 11562 ASSERT(a != NULL);
11565 11563
11566 11564 return (a->ipa_ovhd);
11567 11565 }
11568 11566
11569 11567 /*
11570 11568 * If there are any source route options, return the true final
11571 11569 * destination. Otherwise, return the destination.
11572 11570 */
11573 11571 ipaddr_t
11574 11572 ip_get_dst(ipha_t *ipha)
11575 11573 {
11576 11574 ipoptp_t opts;
11577 11575 uchar_t *opt;
11578 11576 uint8_t optval;
11579 11577 uint8_t optlen;
11580 11578 ipaddr_t dst;
11581 11579 uint32_t off;
11582 11580
11583 11581 dst = ipha->ipha_dst;
11584 11582
11585 11583 if (IS_SIMPLE_IPH(ipha))
11586 11584 return (dst);
11587 11585
11588 11586 for (optval = ipoptp_first(&opts, ipha);
11589 11587 optval != IPOPT_EOL;
11590 11588 optval = ipoptp_next(&opts)) {
11591 11589 opt = opts.ipoptp_cur;
11592 11590 optlen = opts.ipoptp_len;
11593 11591 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11594 11592 switch (optval) {
11595 11593 case IPOPT_SSRR:
11596 11594 case IPOPT_LSRR:
11597 11595 off = opt[IPOPT_OFFSET];
11598 11596 /*
11599 11597 * If one of the conditions is true, it means
11600 11598 * end of options and dst already has the right
11601 11599 * value.
11602 11600 */
11603 11601 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11604 11602 off = optlen - IP_ADDR_LEN;
11605 11603 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11606 11604 }
11607 11605 return (dst);
11608 11606 default:
11609 11607 break;
11610 11608 }
11611 11609 }
11612 11610
11613 11611 return (dst);
11614 11612 }
11615 11613
11616 11614 /*
11617 11615 * Outbound IP fragmentation routine.
11618 11616 * Assumes the caller has checked whether or not fragmentation should
11619 11617 * be allowed. Here we copy the DF bit from the header to all the generated
11620 11618 * fragments.
11621 11619 */
11622 11620 int
11623 11621 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11624 11622 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11625 11623 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11626 11624 {
11627 11625 int i1;
11628 11626 int hdr_len;
11629 11627 mblk_t *hdr_mp;
11630 11628 ipha_t *ipha;
11631 11629 int ip_data_end;
11632 11630 int len;
11633 11631 mblk_t *mp = mp_orig;
11634 11632 int offset;
11635 11633 ill_t *ill = nce->nce_ill;
11636 11634 ip_stack_t *ipst = ill->ill_ipst;
11637 11635 mblk_t *carve_mp;
11638 11636 uint32_t frag_flag;
11639 11637 uint_t priority = mp->b_band;
11640 11638 int error = 0;
11641 11639
11642 11640 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11643 11641
11644 11642 if (pkt_len != msgdsize(mp)) {
11645 11643 ip0dbg(("Packet length mismatch: %d, %ld\n",
11646 11644 pkt_len, msgdsize(mp)));
11647 11645 freemsg(mp);
11648 11646 return (EINVAL);
11649 11647 }
11650 11648
11651 11649 if (max_frag == 0) {
11652 11650 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11653 11651 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11654 11652 ip_drop_output("FragFails: zero max_frag", mp, ill);
11655 11653 freemsg(mp);
11656 11654 return (EINVAL);
11657 11655 }
11658 11656
11659 11657 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11660 11658 ipha = (ipha_t *)mp->b_rptr;
11661 11659 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11662 11660 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11663 11661
11664 11662 /*
11665 11663 * Establish the starting offset. May not be zero if we are fragging
11666 11664 * a fragment that is being forwarded.
11667 11665 */
11668 11666 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11669 11667
11670 11668 /* TODO why is this test needed? */
11671 11669 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11672 11670 /* TODO: notify ulp somehow */
11673 11671 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11674 11672 ip_drop_output("FragFails: bad starting offset", mp, ill);
11675 11673 freemsg(mp);
11676 11674 return (EINVAL);
11677 11675 }
11678 11676
11679 11677 hdr_len = IPH_HDR_LENGTH(ipha);
11680 11678 ipha->ipha_hdr_checksum = 0;
11681 11679
11682 11680 /*
11683 11681 * Establish the number of bytes maximum per frag, after putting
11684 11682 * in the header.
11685 11683 */
11686 11684 len = (max_frag - hdr_len) & ~7;
11687 11685
11688 11686 /* Get a copy of the header for the trailing frags */
11689 11687 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11690 11688 mp);
11691 11689 if (hdr_mp == NULL) {
11692 11690 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11693 11691 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11694 11692 freemsg(mp);
11695 11693 return (ENOBUFS);
11696 11694 }
11697 11695
11698 11696 /* Store the starting offset, with the MoreFrags flag. */
11699 11697 i1 = offset | IPH_MF | frag_flag;
11700 11698 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11701 11699
11702 11700 /* Establish the ending byte offset, based on the starting offset. */
11703 11701 offset <<= 3;
11704 11702 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11705 11703
11706 11704 /* Store the length of the first fragment in the IP header. */
11707 11705 i1 = len + hdr_len;
11708 11706 ASSERT(i1 <= IP_MAXPACKET);
11709 11707 ipha->ipha_length = htons((uint16_t)i1);
11710 11708
11711 11709 /*
11712 11710 * Compute the IP header checksum for the first frag. We have to
11713 11711 * watch out that we stop at the end of the header.
11714 11712 */
11715 11713 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11716 11714
11717 11715 /*
11718 11716 * Now carve off the first frag. Note that this will include the
11719 11717 * original IP header.
11720 11718 */
11721 11719 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11722 11720 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11723 11721 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11724 11722 freeb(hdr_mp);
11725 11723 freemsg(mp_orig);
11726 11724 return (ENOBUFS);
11727 11725 }
11728 11726
11729 11727 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11730 11728
11731 11729 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11732 11730 ixa_cookie);
11733 11731 if (error != 0 && error != EWOULDBLOCK) {
11734 11732 /* No point in sending the other fragments */
11735 11733 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11736 11734 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11737 11735 freeb(hdr_mp);
11738 11736 freemsg(mp_orig);
11739 11737 return (error);
11740 11738 }
11741 11739
11742 11740 /* No need to redo state machine in loop */
11743 11741 ixaflags &= ~IXAF_REACH_CONF;
11744 11742
11745 11743 /* Advance the offset to the second frag starting point. */
11746 11744 offset += len;
11747 11745 /*
11748 11746 * Update hdr_len from the copied header - there might be less options
11749 11747 * in the later fragments.
11750 11748 */
11751 11749 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11752 11750 /* Loop until done. */
11753 11751 for (;;) {
11754 11752 uint16_t offset_and_flags;
11755 11753 uint16_t ip_len;
11756 11754
11757 11755 if (ip_data_end - offset > len) {
11758 11756 /*
11759 11757 * Carve off the appropriate amount from the original
11760 11758 * datagram.
11761 11759 */
11762 11760 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11763 11761 mp = NULL;
11764 11762 break;
11765 11763 }
11766 11764 /*
11767 11765 * More frags after this one. Get another copy
11768 11766 * of the header.
11769 11767 */
11770 11768 if (carve_mp->b_datap->db_ref == 1 &&
11771 11769 hdr_mp->b_wptr - hdr_mp->b_rptr <
11772 11770 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11773 11771 /* Inline IP header */
11774 11772 carve_mp->b_rptr -= hdr_mp->b_wptr -
11775 11773 hdr_mp->b_rptr;
11776 11774 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11777 11775 hdr_mp->b_wptr - hdr_mp->b_rptr);
11778 11776 mp = carve_mp;
11779 11777 } else {
11780 11778 if (!(mp = copyb(hdr_mp))) {
11781 11779 freemsg(carve_mp);
11782 11780 break;
11783 11781 }
11784 11782 /* Get priority marking, if any. */
11785 11783 mp->b_band = priority;
11786 11784 mp->b_cont = carve_mp;
11787 11785 }
11788 11786 ipha = (ipha_t *)mp->b_rptr;
11789 11787 offset_and_flags = IPH_MF;
11790 11788 } else {
11791 11789 /*
11792 11790 * Last frag. Consume the header. Set len to
11793 11791 * the length of this last piece.
11794 11792 */
11795 11793 len = ip_data_end - offset;
11796 11794
11797 11795 /*
11798 11796 * Carve off the appropriate amount from the original
11799 11797 * datagram.
11800 11798 */
11801 11799 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11802 11800 mp = NULL;
11803 11801 break;
11804 11802 }
11805 11803 if (carve_mp->b_datap->db_ref == 1 &&
11806 11804 hdr_mp->b_wptr - hdr_mp->b_rptr <
11807 11805 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11808 11806 /* Inline IP header */
11809 11807 carve_mp->b_rptr -= hdr_mp->b_wptr -
11810 11808 hdr_mp->b_rptr;
11811 11809 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11812 11810 hdr_mp->b_wptr - hdr_mp->b_rptr);
11813 11811 mp = carve_mp;
11814 11812 freeb(hdr_mp);
11815 11813 hdr_mp = mp;
11816 11814 } else {
11817 11815 mp = hdr_mp;
11818 11816 /* Get priority marking, if any. */
11819 11817 mp->b_band = priority;
11820 11818 mp->b_cont = carve_mp;
11821 11819 }
11822 11820 ipha = (ipha_t *)mp->b_rptr;
11823 11821 /* A frag of a frag might have IPH_MF non-zero */
11824 11822 offset_and_flags =
11825 11823 ntohs(ipha->ipha_fragment_offset_and_flags) &
11826 11824 IPH_MF;
11827 11825 }
11828 11826 offset_and_flags |= (uint16_t)(offset >> 3);
11829 11827 offset_and_flags |= (uint16_t)frag_flag;
11830 11828 /* Store the offset and flags in the IP header. */
11831 11829 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11832 11830
11833 11831 /* Store the length in the IP header. */
11834 11832 ip_len = (uint16_t)(len + hdr_len);
11835 11833 ipha->ipha_length = htons(ip_len);
11836 11834
11837 11835 /*
11838 11836 * Set the IP header checksum. Note that mp is just
11839 11837 * the header, so this is easy to pass to ip_csum.
11840 11838 */
11841 11839 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11842 11840
11843 11841 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11844 11842
11845 11843 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11846 11844 nolzid, ixa_cookie);
11847 11845 /* All done if we just consumed the hdr_mp. */
11848 11846 if (mp == hdr_mp) {
11849 11847 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11850 11848 return (error);
11851 11849 }
11852 11850 if (error != 0 && error != EWOULDBLOCK) {
11853 11851 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11854 11852 mblk_t *, hdr_mp);
11855 11853 /* No point in sending the other fragments */
11856 11854 break;
11857 11855 }
11858 11856
11859 11857 /* Otherwise, advance and loop. */
11860 11858 offset += len;
11861 11859 }
11862 11860 /* Clean up following allocation failure. */
11863 11861 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11864 11862 ip_drop_output("FragFails: loop ended", NULL, ill);
11865 11863 if (mp != hdr_mp)
11866 11864 freeb(hdr_mp);
11867 11865 if (mp != mp_orig)
11868 11866 freemsg(mp_orig);
11869 11867 return (error);
11870 11868 }
11871 11869
11872 11870 /*
11873 11871 * Copy the header plus those options which have the copy bit set
11874 11872 */
11875 11873 static mblk_t *
11876 11874 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11877 11875 mblk_t *src)
11878 11876 {
11879 11877 mblk_t *mp;
11880 11878 uchar_t *up;
11881 11879
11882 11880 /*
11883 11881 * Quick check if we need to look for options without the copy bit
11884 11882 * set
11885 11883 */
11886 11884 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11887 11885 if (!mp)
11888 11886 return (mp);
11889 11887 mp->b_rptr += ipst->ips_ip_wroff_extra;
11890 11888 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11891 11889 bcopy(rptr, mp->b_rptr, hdr_len);
11892 11890 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11893 11891 return (mp);
11894 11892 }
11895 11893 up = mp->b_rptr;
11896 11894 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11897 11895 up += IP_SIMPLE_HDR_LENGTH;
11898 11896 rptr += IP_SIMPLE_HDR_LENGTH;
11899 11897 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11900 11898 while (hdr_len > 0) {
11901 11899 uint32_t optval;
11902 11900 uint32_t optlen;
11903 11901
11904 11902 optval = *rptr;
11905 11903 if (optval == IPOPT_EOL)
11906 11904 break;
11907 11905 if (optval == IPOPT_NOP)
11908 11906 optlen = 1;
11909 11907 else
11910 11908 optlen = rptr[1];
11911 11909 if (optval & IPOPT_COPY) {
11912 11910 bcopy(rptr, up, optlen);
11913 11911 up += optlen;
11914 11912 }
11915 11913 rptr += optlen;
11916 11914 hdr_len -= optlen;
11917 11915 }
11918 11916 /*
11919 11917 * Make sure that we drop an even number of words by filling
11920 11918 * with EOL to the next word boundary.
11921 11919 */
11922 11920 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11923 11921 hdr_len & 0x3; hdr_len++)
11924 11922 *up++ = IPOPT_EOL;
11925 11923 mp->b_wptr = up;
11926 11924 /* Update header length */
11927 11925 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11928 11926 return (mp);
11929 11927 }
11930 11928
11931 11929 /*
11932 11930 * Update any source route, record route, or timestamp options when
11933 11931 * sending a packet back to ourselves.
11934 11932 * Check that we are at end of strict source route.
11935 11933 * The options have been sanity checked by ip_output_options().
11936 11934 */
11937 11935 void
11938 11936 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11939 11937 {
11940 11938 ipoptp_t opts;
11941 11939 uchar_t *opt;
11942 11940 uint8_t optval;
11943 11941 uint8_t optlen;
11944 11942 ipaddr_t dst;
11945 11943 uint32_t ts;
11946 11944 timestruc_t now;
11947 11945
11948 11946 for (optval = ipoptp_first(&opts, ipha);
11949 11947 optval != IPOPT_EOL;
11950 11948 optval = ipoptp_next(&opts)) {
11951 11949 opt = opts.ipoptp_cur;
11952 11950 optlen = opts.ipoptp_len;
11953 11951 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11954 11952 switch (optval) {
11955 11953 uint32_t off;
11956 11954 case IPOPT_SSRR:
11957 11955 case IPOPT_LSRR:
11958 11956 off = opt[IPOPT_OFFSET];
11959 11957 off--;
11960 11958 if (optlen < IP_ADDR_LEN ||
11961 11959 off > optlen - IP_ADDR_LEN) {
11962 11960 /* End of source route */
11963 11961 break;
11964 11962 }
11965 11963 /*
11966 11964 * This will only happen if two consecutive entries
11967 11965 * in the source route contains our address or if
11968 11966 * it is a packet with a loose source route which
11969 11967 * reaches us before consuming the whole source route
11970 11968 */
11971 11969
11972 11970 if (optval == IPOPT_SSRR) {
11973 11971 return;
11974 11972 }
11975 11973 /*
11976 11974 * Hack: instead of dropping the packet truncate the
11977 11975 * source route to what has been used by filling the
11978 11976 * rest with IPOPT_NOP.
11979 11977 */
11980 11978 opt[IPOPT_OLEN] = (uint8_t)off;
11981 11979 while (off < optlen) {
11982 11980 opt[off++] = IPOPT_NOP;
11983 11981 }
11984 11982 break;
11985 11983 case IPOPT_RR:
11986 11984 off = opt[IPOPT_OFFSET];
11987 11985 off--;
11988 11986 if (optlen < IP_ADDR_LEN ||
11989 11987 off > optlen - IP_ADDR_LEN) {
11990 11988 /* No more room - ignore */
11991 11989 ip1dbg((
11992 11990 "ip_output_local_options: end of RR\n"));
11993 11991 break;
11994 11992 }
11995 11993 dst = htonl(INADDR_LOOPBACK);
11996 11994 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11997 11995 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11998 11996 break;
11999 11997 case IPOPT_TS:
12000 11998 /* Insert timestamp if there is romm */
12001 11999 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12002 12000 case IPOPT_TS_TSONLY:
12003 12001 off = IPOPT_TS_TIMELEN;
12004 12002 break;
12005 12003 case IPOPT_TS_PRESPEC:
12006 12004 case IPOPT_TS_PRESPEC_RFC791:
12007 12005 /* Verify that the address matched */
12008 12006 off = opt[IPOPT_OFFSET] - 1;
12009 12007 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
12010 12008 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
12011 12009 /* Not for us */
12012 12010 break;
12013 12011 }
12014 12012 /* FALLTHRU */
12015 12013 case IPOPT_TS_TSANDADDR:
12016 12014 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
12017 12015 break;
12018 12016 default:
12019 12017 /*
12020 12018 * ip_*put_options should have already
12021 12019 * dropped this packet.
12022 12020 */
12023 12021 cmn_err(CE_PANIC, "ip_output_local_options: "
12024 12022 "unknown IT - bug in ip_output_options?\n");
12025 12023 return; /* Keep "lint" happy */
12026 12024 }
12027 12025 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
12028 12026 /* Increase overflow counter */
12029 12027 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
12030 12028 opt[IPOPT_POS_OV_FLG] = (uint8_t)
12031 12029 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
12032 12030 (off << 4);
12033 12031 break;
12034 12032 }
12035 12033 off = opt[IPOPT_OFFSET] - 1;
12036 12034 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12037 12035 case IPOPT_TS_PRESPEC:
12038 12036 case IPOPT_TS_PRESPEC_RFC791:
12039 12037 case IPOPT_TS_TSANDADDR:
12040 12038 dst = htonl(INADDR_LOOPBACK);
12041 12039 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12042 12040 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12043 12041 /* FALLTHRU */
12044 12042 case IPOPT_TS_TSONLY:
12045 12043 off = opt[IPOPT_OFFSET] - 1;
12046 12044 /* Compute # of milliseconds since midnight */
12047 12045 gethrestime(&now);
12048 12046 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
12049 12047 NSEC2MSEC(now.tv_nsec);
12050 12048 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
12051 12049 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
12052 12050 break;
12053 12051 }
12054 12052 break;
12055 12053 }
12056 12054 }
12057 12055 }
12058 12056
12059 12057 /*
12060 12058 * Prepend an M_DATA fastpath header, and if none present prepend a
12061 12059 * DL_UNITDATA_REQ. Frees the mblk on failure.
12062 12060 *
12063 12061 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12064 12062 * If there is a change to them, the nce will be deleted (condemned) and
12065 12063 * a new nce_t will be created when packets are sent. Thus we need no locks
12066 12064 * to access those fields.
12067 12065 *
12068 12066 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12069 12067 * we place b_band in dl_priority.dl_max.
12070 12068 */
12071 12069 static mblk_t *
12072 12070 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12073 12071 {
12074 12072 uint_t hlen;
12075 12073 mblk_t *mp1;
12076 12074 uint_t priority;
12077 12075 uchar_t *rptr;
12078 12076
12079 12077 rptr = mp->b_rptr;
12080 12078
12081 12079 ASSERT(DB_TYPE(mp) == M_DATA);
12082 12080 priority = mp->b_band;
12083 12081
12084 12082 ASSERT(nce != NULL);
12085 12083 if ((mp1 = nce->nce_fp_mp) != NULL) {
12086 12084 hlen = MBLKL(mp1);
12087 12085 /*
12088 12086 * Check if we have enough room to prepend fastpath
12089 12087 * header
12090 12088 */
12091 12089 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12092 12090 rptr -= hlen;
12093 12091 bcopy(mp1->b_rptr, rptr, hlen);
12094 12092 /*
12095 12093 * Set the b_rptr to the start of the link layer
12096 12094 * header
12097 12095 */
12098 12096 mp->b_rptr = rptr;
12099 12097 return (mp);
12100 12098 }
12101 12099 mp1 = copyb(mp1);
12102 12100 if (mp1 == NULL) {
12103 12101 ill_t *ill = nce->nce_ill;
12104 12102
12105 12103 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12106 12104 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12107 12105 freemsg(mp);
12108 12106 return (NULL);
12109 12107 }
12110 12108 mp1->b_band = priority;
12111 12109 mp1->b_cont = mp;
12112 12110 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12113 12111 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12114 12112 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12115 12113 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12116 12114 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12117 12115 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12118 12116 /*
12119 12117 * XXX disable ICK_VALID and compute checksum
12120 12118 * here; can happen if nce_fp_mp changes and
12121 12119 * it can't be copied now due to insufficient
12122 12120 * space. (unlikely, fp mp can change, but it
12123 12121 * does not increase in length)
12124 12122 */
12125 12123 return (mp1);
12126 12124 }
12127 12125 mp1 = copyb(nce->nce_dlur_mp);
12128 12126
12129 12127 if (mp1 == NULL) {
12130 12128 ill_t *ill = nce->nce_ill;
12131 12129
12132 12130 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12133 12131 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12134 12132 freemsg(mp);
12135 12133 return (NULL);
12136 12134 }
12137 12135 mp1->b_cont = mp;
12138 12136 if (priority != 0) {
12139 12137 mp1->b_band = priority;
12140 12138 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12141 12139 priority;
12142 12140 }
12143 12141 return (mp1);
12144 12142 }
12145 12143
12146 12144 /*
12147 12145 * Finish the outbound IPsec processing. This function is called from
12148 12146 * ipsec_out_process() if the IPsec packet was processed
12149 12147 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12150 12148 * asynchronously.
12151 12149 *
12152 12150 * This is common to IPv4 and IPv6.
12153 12151 */
12154 12152 int
12155 12153 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12156 12154 {
12157 12155 iaflags_t ixaflags = ixa->ixa_flags;
12158 12156 uint_t pktlen;
12159 12157
12160 12158
12161 12159 /* AH/ESP don't update ixa_pktlen when they modify the packet */
12162 12160 if (ixaflags & IXAF_IS_IPV4) {
12163 12161 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12164 12162
12165 12163 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12166 12164 pktlen = ntohs(ipha->ipha_length);
12167 12165 } else {
12168 12166 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12169 12167
12170 12168 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12171 12169 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12172 12170 }
12173 12171
12174 12172 /*
12175 12173 * We release any hard reference on the SAs here to make
12176 12174 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12177 12175 * on the SAs.
12178 12176 * If in the future we want the hard latching of the SAs in the
12179 12177 * ip_xmit_attr_t then we should remove this.
12180 12178 */
12181 12179 if (ixa->ixa_ipsec_esp_sa != NULL) {
12182 12180 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12183 12181 ixa->ixa_ipsec_esp_sa = NULL;
12184 12182 }
12185 12183 if (ixa->ixa_ipsec_ah_sa != NULL) {
12186 12184 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12187 12185 ixa->ixa_ipsec_ah_sa = NULL;
12188 12186 }
12189 12187
12190 12188 /* Do we need to fragment? */
12191 12189 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12192 12190 pktlen > ixa->ixa_fragsize) {
12193 12191 if (ixaflags & IXAF_IS_IPV4) {
12194 12192 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12195 12193 /*
12196 12194 * We check for the DF case in ipsec_out_process
12197 12195 * hence this only handles the non-DF case.
12198 12196 */
12199 12197 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12200 12198 pktlen, ixa->ixa_fragsize,
12201 12199 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12202 12200 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12203 12201 &ixa->ixa_cookie));
12204 12202 } else {
12205 12203 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12206 12204 if (mp == NULL) {
12207 12205 /* MIB and ip_drop_output already done */
12208 12206 return (ENOMEM);
12209 12207 }
12210 12208 pktlen += sizeof (ip6_frag_t);
12211 12209 if (pktlen > ixa->ixa_fragsize) {
12212 12210 return (ip_fragment_v6(mp, ixa->ixa_nce,
12213 12211 ixa->ixa_flags, pktlen,
12214 12212 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12215 12213 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12216 12214 ixa->ixa_postfragfn, &ixa->ixa_cookie));
12217 12215 }
12218 12216 }
12219 12217 }
12220 12218 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12221 12219 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12222 12220 ixa->ixa_no_loop_zoneid, NULL));
12223 12221 }
12224 12222
12225 12223 /*
12226 12224 * Finish the inbound IPsec processing. This function is called from
12227 12225 * ipsec_out_process() if the IPsec packet was processed
12228 12226 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12229 12227 * asynchronously.
12230 12228 *
12231 12229 * This is common to IPv4 and IPv6.
12232 12230 */
12233 12231 void
12234 12232 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12235 12233 {
12236 12234 iaflags_t iraflags = ira->ira_flags;
12237 12235
12238 12236 /* Length might have changed */
12239 12237 if (iraflags & IRAF_IS_IPV4) {
12240 12238 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12241 12239
12242 12240 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12243 12241 ira->ira_pktlen = ntohs(ipha->ipha_length);
12244 12242 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12245 12243 ira->ira_protocol = ipha->ipha_protocol;
12246 12244
12247 12245 ip_fanout_v4(mp, ipha, ira);
12248 12246 } else {
12249 12247 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12250 12248 uint8_t *nexthdrp;
12251 12249
12252 12250 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12253 12251 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12254 12252 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12255 12253 &nexthdrp)) {
12256 12254 /* Malformed packet */
12257 12255 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12258 12256 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12259 12257 freemsg(mp);
12260 12258 return;
12261 12259 }
12262 12260 ira->ira_protocol = *nexthdrp;
12263 12261 ip_fanout_v6(mp, ip6h, ira);
12264 12262 }
12265 12263 }
12266 12264
12267 12265 /*
12268 12266 * Select which AH & ESP SA's to use (if any) for the outbound packet.
12269 12267 *
12270 12268 * If this function returns B_TRUE, the requested SA's have been filled
12271 12269 * into the ixa_ipsec_*_sa pointers.
12272 12270 *
12273 12271 * If the function returns B_FALSE, the packet has been "consumed", most
12274 12272 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12275 12273 *
12276 12274 * The SA references created by the protocol-specific "select"
12277 12275 * function will be released in ip_output_post_ipsec.
12278 12276 */
12279 12277 static boolean_t
12280 12278 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12281 12279 {
12282 12280 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12283 12281 ipsec_policy_t *pp;
12284 12282 ipsec_action_t *ap;
12285 12283
12286 12284 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12287 12285 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12288 12286 (ixa->ixa_ipsec_action != NULL));
12289 12287
12290 12288 ap = ixa->ixa_ipsec_action;
12291 12289 if (ap == NULL) {
12292 12290 pp = ixa->ixa_ipsec_policy;
12293 12291 ASSERT(pp != NULL);
12294 12292 ap = pp->ipsp_act;
12295 12293 ASSERT(ap != NULL);
12296 12294 }
12297 12295
12298 12296 /*
12299 12297 * We have an action. now, let's select SA's.
12300 12298 * A side effect of setting ixa_ipsec_*_sa is that it will
12301 12299 * be cached in the conn_t.
12302 12300 */
12303 12301 if (ap->ipa_want_esp) {
12304 12302 if (ixa->ixa_ipsec_esp_sa == NULL) {
12305 12303 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12306 12304 IPPROTO_ESP);
12307 12305 }
12308 12306 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12309 12307 }
12310 12308
12311 12309 if (ap->ipa_want_ah) {
12312 12310 if (ixa->ixa_ipsec_ah_sa == NULL) {
12313 12311 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12314 12312 IPPROTO_AH);
12315 12313 }
12316 12314 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12317 12315 /*
12318 12316 * The ESP and AH processing order needs to be preserved
12319 12317 * when both protocols are required (ESP should be applied
12320 12318 * before AH for an outbound packet). Force an ESP ACQUIRE
12321 12319 * when both ESP and AH are required, and an AH ACQUIRE
12322 12320 * is needed.
12323 12321 */
12324 12322 if (ap->ipa_want_esp && need_ah_acquire)
12325 12323 need_esp_acquire = B_TRUE;
12326 12324 }
12327 12325
12328 12326 /*
12329 12327 * Send an ACQUIRE (extended, regular, or both) if we need one.
12330 12328 * Release SAs that got referenced, but will not be used until we
12331 12329 * acquire _all_ of the SAs we need.
12332 12330 */
12333 12331 if (need_ah_acquire || need_esp_acquire) {
12334 12332 if (ixa->ixa_ipsec_ah_sa != NULL) {
12335 12333 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12336 12334 ixa->ixa_ipsec_ah_sa = NULL;
12337 12335 }
12338 12336 if (ixa->ixa_ipsec_esp_sa != NULL) {
12339 12337 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12340 12338 ixa->ixa_ipsec_esp_sa = NULL;
12341 12339 }
12342 12340
12343 12341 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12344 12342 return (B_FALSE);
12345 12343 }
12346 12344
12347 12345 return (B_TRUE);
12348 12346 }
12349 12347
12350 12348 /*
12351 12349 * Handle IPsec output processing.
12352 12350 * This function is only entered once for a given packet.
12353 12351 * We try to do things synchronously, but if we need to have user-level
12354 12352 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12355 12353 * will be completed
12356 12354 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12357 12355 * - when asynchronous ESP is done it will do AH
12358 12356 *
12359 12357 * In all cases we come back in ip_output_post_ipsec() to fragment and
12360 12358 * send out the packet.
12361 12359 */
12362 12360 int
12363 12361 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12364 12362 {
12365 12363 ill_t *ill = ixa->ixa_nce->nce_ill;
12366 12364 ip_stack_t *ipst = ixa->ixa_ipst;
12367 12365 ipsec_stack_t *ipss;
12368 12366 ipsec_policy_t *pp;
12369 12367 ipsec_action_t *ap;
12370 12368
12371 12369 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12372 12370
12373 12371 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12374 12372 (ixa->ixa_ipsec_action != NULL));
12375 12373
12376 12374 ipss = ipst->ips_netstack->netstack_ipsec;
12377 12375 if (!ipsec_loaded(ipss)) {
12378 12376 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12379 12377 ip_drop_packet(mp, B_TRUE, ill,
12380 12378 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12381 12379 &ipss->ipsec_dropper);
12382 12380 return (ENOTSUP);
12383 12381 }
12384 12382
12385 12383 ap = ixa->ixa_ipsec_action;
12386 12384 if (ap == NULL) {
12387 12385 pp = ixa->ixa_ipsec_policy;
12388 12386 ASSERT(pp != NULL);
12389 12387 ap = pp->ipsp_act;
12390 12388 ASSERT(ap != NULL);
12391 12389 }
12392 12390
12393 12391 /* Handle explicit drop action and bypass. */
12394 12392 switch (ap->ipa_act.ipa_type) {
12395 12393 case IPSEC_ACT_DISCARD:
12396 12394 case IPSEC_ACT_REJECT:
12397 12395 ip_drop_packet(mp, B_FALSE, ill,
12398 12396 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12399 12397 return (EHOSTUNREACH); /* IPsec policy failure */
12400 12398 case IPSEC_ACT_BYPASS:
12401 12399 return (ip_output_post_ipsec(mp, ixa));
12402 12400 }
12403 12401
12404 12402 /*
12405 12403 * The order of processing is first insert a IP header if needed.
12406 12404 * Then insert the ESP header and then the AH header.
12407 12405 */
12408 12406 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12409 12407 /*
12410 12408 * First get the outer IP header before sending
12411 12409 * it to ESP.
12412 12410 */
12413 12411 ipha_t *oipha, *iipha;
12414 12412 mblk_t *outer_mp, *inner_mp;
12415 12413
12416 12414 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12417 12415 (void) mi_strlog(ill->ill_rq, 0,
12418 12416 SL_ERROR|SL_TRACE|SL_CONSOLE,
12419 12417 "ipsec_out_process: "
12420 12418 "Self-Encapsulation failed: Out of memory\n");
12421 12419 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12422 12420 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12423 12421 freemsg(mp);
12424 12422 return (ENOBUFS);
12425 12423 }
12426 12424 inner_mp = mp;
12427 12425 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12428 12426 oipha = (ipha_t *)outer_mp->b_rptr;
12429 12427 iipha = (ipha_t *)inner_mp->b_rptr;
12430 12428 *oipha = *iipha;
12431 12429 outer_mp->b_wptr += sizeof (ipha_t);
12432 12430 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12433 12431 sizeof (ipha_t));
12434 12432 oipha->ipha_protocol = IPPROTO_ENCAP;
12435 12433 oipha->ipha_version_and_hdr_length =
12436 12434 IP_SIMPLE_HDR_VERSION;
12437 12435 oipha->ipha_hdr_checksum = 0;
12438 12436 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12439 12437 outer_mp->b_cont = inner_mp;
12440 12438 mp = outer_mp;
12441 12439
12442 12440 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12443 12441 }
12444 12442
12445 12443 /* If we need to wait for a SA then we can't return any errno */
12446 12444 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12447 12445 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12448 12446 !ipsec_out_select_sa(mp, ixa))
12449 12447 return (0);
12450 12448
12451 12449 /*
12452 12450 * By now, we know what SA's to use. Toss over to ESP & AH
12453 12451 * to do the heavy lifting.
12454 12452 */
12455 12453 if (ap->ipa_want_esp) {
12456 12454 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12457 12455
12458 12456 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12459 12457 if (mp == NULL) {
12460 12458 /*
12461 12459 * Either it failed or is pending. In the former case
12462 12460 * ipIfStatsInDiscards was increased.
12463 12461 */
12464 12462 return (0);
12465 12463 }
12466 12464 }
12467 12465
12468 12466 if (ap->ipa_want_ah) {
12469 12467 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12470 12468
12471 12469 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12472 12470 if (mp == NULL) {
12473 12471 /*
12474 12472 * Either it failed or is pending. In the former case
12475 12473 * ipIfStatsInDiscards was increased.
12476 12474 */
12477 12475 return (0);
12478 12476 }
12479 12477 }
12480 12478 /*
12481 12479 * We are done with IPsec processing. Send it over
12482 12480 * the wire.
12483 12481 */
12484 12482 return (ip_output_post_ipsec(mp, ixa));
12485 12483 }
12486 12484
12487 12485 /*
12488 12486 * ioctls that go through a down/up sequence may need to wait for the down
12489 12487 * to complete. This involves waiting for the ire and ipif refcnts to go down
12490 12488 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12491 12489 */
12492 12490 /* ARGSUSED */
12493 12491 void
12494 12492 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12495 12493 {
12496 12494 struct iocblk *iocp;
12497 12495 mblk_t *mp1;
12498 12496 ip_ioctl_cmd_t *ipip;
12499 12497 int err;
12500 12498 sin_t *sin;
12501 12499 struct lifreq *lifr;
12502 12500 struct ifreq *ifr;
12503 12501
12504 12502 iocp = (struct iocblk *)mp->b_rptr;
12505 12503 ASSERT(ipsq != NULL);
12506 12504 /* Existence of mp1 verified in ip_wput_nondata */
12507 12505 mp1 = mp->b_cont->b_cont;
12508 12506 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12509 12507 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12510 12508 /*
12511 12509 * Special case where ipx_current_ipif is not set:
12512 12510 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12513 12511 * We are here as were not able to complete the operation in
12514 12512 * ipif_set_values because we could not become exclusive on
12515 12513 * the new ipsq.
12516 12514 */
12517 12515 ill_t *ill = q->q_ptr;
12518 12516 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12519 12517 }
12520 12518 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12521 12519
12522 12520 if (ipip->ipi_cmd_type == IF_CMD) {
12523 12521 /* This a old style SIOC[GS]IF* command */
12524 12522 ifr = (struct ifreq *)mp1->b_rptr;
12525 12523 sin = (sin_t *)&ifr->ifr_addr;
12526 12524 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12527 12525 /* This a new style SIOC[GS]LIF* command */
12528 12526 lifr = (struct lifreq *)mp1->b_rptr;
12529 12527 sin = (sin_t *)&lifr->lifr_addr;
12530 12528 } else {
12531 12529 sin = NULL;
12532 12530 }
12533 12531
12534 12532 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12535 12533 q, mp, ipip, mp1->b_rptr);
12536 12534
12537 12535 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12538 12536 int, ipip->ipi_cmd,
12539 12537 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12540 12538 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12541 12539
12542 12540 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12543 12541 }
12544 12542
12545 12543 /*
12546 12544 * ioctl processing
12547 12545 *
12548 12546 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12549 12547 * the ioctl command in the ioctl tables, determines the copyin data size
12550 12548 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12551 12549 *
12552 12550 * ioctl processing then continues when the M_IOCDATA makes its way down to
12553 12551 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12554 12552 * associated 'conn' is refheld till the end of the ioctl and the general
12555 12553 * ioctl processing function ip_process_ioctl() is called to extract the
12556 12554 * arguments and process the ioctl. To simplify extraction, ioctl commands
12557 12555 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12558 12556 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12559 12557 * is used to extract the ioctl's arguments.
12560 12558 *
12561 12559 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12562 12560 * so goes thru the serialization primitive ipsq_try_enter. Then the
12563 12561 * appropriate function to handle the ioctl is called based on the entry in
12564 12562 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12565 12563 * which also refreleases the 'conn' that was refheld at the start of the
12566 12564 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12567 12565 *
12568 12566 * Many exclusive ioctls go thru an internal down up sequence as part of
12569 12567 * the operation. For example an attempt to change the IP address of an
12570 12568 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12571 12569 * does all the cleanup such as deleting all ires that use this address.
12572 12570 * Then we need to wait till all references to the interface go away.
12573 12571 */
12574 12572 void
12575 12573 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12576 12574 {
12577 12575 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12578 12576 ip_ioctl_cmd_t *ipip = arg;
12579 12577 ip_extract_func_t *extract_funcp;
12580 12578 cmd_info_t ci;
12581 12579 int err;
12582 12580 boolean_t entered_ipsq = B_FALSE;
12583 12581
12584 12582 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12585 12583
12586 12584 if (ipip == NULL)
12587 12585 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12588 12586
12589 12587 /*
12590 12588 * SIOCLIFADDIF needs to go thru a special path since the
12591 12589 * ill may not exist yet. This happens in the case of lo0
12592 12590 * which is created using this ioctl.
12593 12591 */
12594 12592 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12595 12593 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12596 12594 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12597 12595 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12598 12596 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12599 12597 return;
12600 12598 }
12601 12599
12602 12600 ci.ci_ipif = NULL;
12603 12601 switch (ipip->ipi_cmd_type) {
12604 12602 case MISC_CMD:
12605 12603 case MSFILT_CMD:
12606 12604 /*
12607 12605 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12608 12606 */
12609 12607 if (ipip->ipi_cmd == IF_UNITSEL) {
12610 12608 /* ioctl comes down the ill */
12611 12609 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12612 12610 ipif_refhold(ci.ci_ipif);
12613 12611 }
12614 12612 err = 0;
12615 12613 ci.ci_sin = NULL;
12616 12614 ci.ci_sin6 = NULL;
12617 12615 ci.ci_lifr = NULL;
12618 12616 extract_funcp = NULL;
12619 12617 break;
12620 12618
12621 12619 case IF_CMD:
12622 12620 case LIF_CMD:
12623 12621 extract_funcp = ip_extract_lifreq;
12624 12622 break;
12625 12623
12626 12624 case ARP_CMD:
12627 12625 case XARP_CMD:
12628 12626 extract_funcp = ip_extract_arpreq;
12629 12627 break;
12630 12628
12631 12629 default:
12632 12630 ASSERT(0);
12633 12631 }
12634 12632
12635 12633 if (extract_funcp != NULL) {
12636 12634 err = (*extract_funcp)(q, mp, ipip, &ci);
12637 12635 if (err != 0) {
12638 12636 DTRACE_PROBE4(ipif__ioctl,
12639 12637 char *, "ip_process_ioctl finish err",
12640 12638 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12641 12639 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12642 12640 return;
12643 12641 }
12644 12642
12645 12643 /*
12646 12644 * All of the extraction functions return a refheld ipif.
12647 12645 */
12648 12646 ASSERT(ci.ci_ipif != NULL);
12649 12647 }
12650 12648
12651 12649 if (!(ipip->ipi_flags & IPI_WR)) {
12652 12650 /*
12653 12651 * A return value of EINPROGRESS means the ioctl is
12654 12652 * either queued and waiting for some reason or has
12655 12653 * already completed.
12656 12654 */
12657 12655 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12658 12656 ci.ci_lifr);
12659 12657 if (ci.ci_ipif != NULL) {
12660 12658 DTRACE_PROBE4(ipif__ioctl,
12661 12659 char *, "ip_process_ioctl finish RD",
12662 12660 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12663 12661 ipif_t *, ci.ci_ipif);
12664 12662 ipif_refrele(ci.ci_ipif);
12665 12663 } else {
12666 12664 DTRACE_PROBE4(ipif__ioctl,
12667 12665 char *, "ip_process_ioctl finish RD",
12668 12666 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12669 12667 }
12670 12668 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12671 12669 return;
12672 12670 }
12673 12671
12674 12672 ASSERT(ci.ci_ipif != NULL);
12675 12673
12676 12674 /*
12677 12675 * If ipsq is non-NULL, we are already being called exclusively
12678 12676 */
12679 12677 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12680 12678 if (ipsq == NULL) {
12681 12679 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12682 12680 NEW_OP, B_TRUE);
12683 12681 if (ipsq == NULL) {
12684 12682 ipif_refrele(ci.ci_ipif);
12685 12683 return;
12686 12684 }
12687 12685 entered_ipsq = B_TRUE;
12688 12686 }
12689 12687 /*
12690 12688 * Release the ipif so that ipif_down and friends that wait for
12691 12689 * references to go away are not misled about the current ipif_refcnt
12692 12690 * values. We are writer so we can access the ipif even after releasing
12693 12691 * the ipif.
12694 12692 */
12695 12693 ipif_refrele(ci.ci_ipif);
12696 12694
12697 12695 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12698 12696
12699 12697 /*
12700 12698 * A return value of EINPROGRESS means the ioctl is
12701 12699 * either queued and waiting for some reason or has
12702 12700 * already completed.
12703 12701 */
12704 12702 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12705 12703
12706 12704 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12707 12705 int, ipip->ipi_cmd,
12708 12706 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12709 12707 ipif_t *, ci.ci_ipif);
12710 12708 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12711 12709
12712 12710 if (entered_ipsq)
12713 12711 ipsq_exit(ipsq);
12714 12712 }
12715 12713
12716 12714 /*
12717 12715 * Complete the ioctl. Typically ioctls use the mi package and need to
12718 12716 * do mi_copyout/mi_copy_done.
12719 12717 */
12720 12718 void
12721 12719 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12722 12720 {
12723 12721 conn_t *connp = NULL;
12724 12722
12725 12723 if (err == EINPROGRESS)
12726 12724 return;
12727 12725
12728 12726 if (CONN_Q(q)) {
12729 12727 connp = Q_TO_CONN(q);
12730 12728 ASSERT(connp->conn_ref >= 2);
12731 12729 }
12732 12730
12733 12731 switch (mode) {
12734 12732 case COPYOUT:
12735 12733 if (err == 0)
12736 12734 mi_copyout(q, mp);
12737 12735 else
12738 12736 mi_copy_done(q, mp, err);
12739 12737 break;
12740 12738
12741 12739 case NO_COPYOUT:
12742 12740 mi_copy_done(q, mp, err);
12743 12741 break;
12744 12742
12745 12743 default:
12746 12744 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12747 12745 break;
12748 12746 }
12749 12747
12750 12748 /*
12751 12749 * The conn refhold and ioctlref placed on the conn at the start of the
12752 12750 * ioctl are released here.
12753 12751 */
12754 12752 if (connp != NULL) {
12755 12753 CONN_DEC_IOCTLREF(connp);
12756 12754 CONN_OPER_PENDING_DONE(connp);
12757 12755 }
12758 12756
12759 12757 if (ipsq != NULL)
12760 12758 ipsq_current_finish(ipsq);
12761 12759 }
12762 12760
12763 12761 /* Handles all non data messages */
12764 12762 void
12765 12763 ip_wput_nondata(queue_t *q, mblk_t *mp)
12766 12764 {
12767 12765 mblk_t *mp1;
12768 12766 struct iocblk *iocp;
12769 12767 ip_ioctl_cmd_t *ipip;
12770 12768 conn_t *connp;
12771 12769 cred_t *cr;
12772 12770 char *proto_str;
12773 12771
12774 12772 if (CONN_Q(q))
12775 12773 connp = Q_TO_CONN(q);
12776 12774 else
12777 12775 connp = NULL;
12778 12776
12779 12777 switch (DB_TYPE(mp)) {
12780 12778 case M_IOCTL:
12781 12779 /*
12782 12780 * IOCTL processing begins in ip_sioctl_copyin_setup which
12783 12781 * will arrange to copy in associated control structures.
12784 12782 */
12785 12783 ip_sioctl_copyin_setup(q, mp);
12786 12784 return;
12787 12785 case M_IOCDATA:
12788 12786 /*
12789 12787 * Ensure that this is associated with one of our trans-
12790 12788 * parent ioctls. If it's not ours, discard it if we're
12791 12789 * running as a driver, or pass it on if we're a module.
12792 12790 */
12793 12791 iocp = (struct iocblk *)mp->b_rptr;
12794 12792 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12795 12793 if (ipip == NULL) {
12796 12794 if (q->q_next == NULL) {
12797 12795 goto nak;
12798 12796 } else {
12799 12797 putnext(q, mp);
12800 12798 }
12801 12799 return;
12802 12800 }
12803 12801 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12804 12802 /*
12805 12803 * The ioctl is one we recognise, but is not consumed
12806 12804 * by IP as a module and we are a module, so we drop
12807 12805 */
12808 12806 goto nak;
12809 12807 }
12810 12808
12811 12809 /* IOCTL continuation following copyin or copyout. */
12812 12810 if (mi_copy_state(q, mp, NULL) == -1) {
12813 12811 /*
12814 12812 * The copy operation failed. mi_copy_state already
12815 12813 * cleaned up, so we're out of here.
12816 12814 */
12817 12815 return;
12818 12816 }
12819 12817 /*
12820 12818 * If we just completed a copy in, we become writer and
12821 12819 * continue processing in ip_sioctl_copyin_done. If it
12822 12820 * was a copy out, we call mi_copyout again. If there is
12823 12821 * nothing more to copy out, it will complete the IOCTL.
12824 12822 */
12825 12823 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12826 12824 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12827 12825 mi_copy_done(q, mp, EPROTO);
12828 12826 return;
12829 12827 }
12830 12828 /*
12831 12829 * Check for cases that need more copying. A return
12832 12830 * value of 0 means a second copyin has been started,
12833 12831 * so we return; a return value of 1 means no more
12834 12832 * copying is needed, so we continue.
12835 12833 */
12836 12834 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12837 12835 MI_COPY_COUNT(mp) == 1) {
12838 12836 if (ip_copyin_msfilter(q, mp) == 0)
12839 12837 return;
12840 12838 }
12841 12839 /*
12842 12840 * Refhold the conn, till the ioctl completes. This is
12843 12841 * needed in case the ioctl ends up in the pending mp
12844 12842 * list. Every mp in the ipx_pending_mp list must have
12845 12843 * a refhold on the conn to resume processing. The
12846 12844 * refhold is released when the ioctl completes
12847 12845 * (whether normally or abnormally). An ioctlref is also
12848 12846 * placed on the conn to prevent TCP from removing the
12849 12847 * queue needed to send the ioctl reply back.
12850 12848 * In all cases ip_ioctl_finish is called to finish
12851 12849 * the ioctl and release the refholds.
12852 12850 */
12853 12851 if (connp != NULL) {
12854 12852 /* This is not a reentry */
12855 12853 CONN_INC_REF(connp);
12856 12854 CONN_INC_IOCTLREF(connp);
12857 12855 } else {
12858 12856 if (!(ipip->ipi_flags & IPI_MODOK)) {
12859 12857 mi_copy_done(q, mp, EINVAL);
12860 12858 return;
12861 12859 }
12862 12860 }
12863 12861
12864 12862 ip_process_ioctl(NULL, q, mp, ipip);
12865 12863
12866 12864 } else {
12867 12865 mi_copyout(q, mp);
12868 12866 }
12869 12867 return;
12870 12868
12871 12869 case M_IOCNAK:
12872 12870 /*
12873 12871 * The only way we could get here is if a resolver didn't like
12874 12872 * an IOCTL we sent it. This shouldn't happen.
12875 12873 */
12876 12874 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12877 12875 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12878 12876 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12879 12877 freemsg(mp);
12880 12878 return;
12881 12879 case M_IOCACK:
12882 12880 /* /dev/ip shouldn't see this */
12883 12881 goto nak;
12884 12882 case M_FLUSH:
12885 12883 if (*mp->b_rptr & FLUSHW)
12886 12884 flushq(q, FLUSHALL);
12887 12885 if (q->q_next) {
12888 12886 putnext(q, mp);
12889 12887 return;
12890 12888 }
12891 12889 if (*mp->b_rptr & FLUSHR) {
12892 12890 *mp->b_rptr &= ~FLUSHW;
12893 12891 qreply(q, mp);
12894 12892 return;
12895 12893 }
12896 12894 freemsg(mp);
12897 12895 return;
12898 12896 case M_CTL:
12899 12897 break;
12900 12898 case M_PROTO:
12901 12899 case M_PCPROTO:
12902 12900 /*
12903 12901 * The only PROTO messages we expect are SNMP-related.
12904 12902 */
12905 12903 switch (((union T_primitives *)mp->b_rptr)->type) {
12906 12904 case T_SVR4_OPTMGMT_REQ:
12907 12905 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12908 12906 "flags %x\n",
12909 12907 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12910 12908
12911 12909 if (connp == NULL) {
12912 12910 proto_str = "T_SVR4_OPTMGMT_REQ";
12913 12911 goto protonak;
12914 12912 }
12915 12913
12916 12914 /*
12917 12915 * All Solaris components should pass a db_credp
12918 12916 * for this TPI message, hence we ASSERT.
12919 12917 * But in case there is some other M_PROTO that looks
12920 12918 * like a TPI message sent by some other kernel
12921 12919 * component, we check and return an error.
12922 12920 */
12923 12921 cr = msg_getcred(mp, NULL);
12924 12922 ASSERT(cr != NULL);
12925 12923 if (cr == NULL) {
12926 12924 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12927 12925 if (mp != NULL)
12928 12926 qreply(q, mp);
12929 12927 return;
12930 12928 }
12931 12929
12932 12930 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12933 12931 proto_str = "Bad SNMPCOM request?";
12934 12932 goto protonak;
12935 12933 }
12936 12934 return;
12937 12935 default:
12938 12936 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12939 12937 (int)*(uint_t *)mp->b_rptr));
12940 12938 freemsg(mp);
12941 12939 return;
12942 12940 }
12943 12941 default:
12944 12942 break;
12945 12943 }
12946 12944 if (q->q_next) {
12947 12945 putnext(q, mp);
12948 12946 } else
12949 12947 freemsg(mp);
12950 12948 return;
12951 12949
12952 12950 nak:
12953 12951 iocp->ioc_error = EINVAL;
12954 12952 mp->b_datap->db_type = M_IOCNAK;
12955 12953 iocp->ioc_count = 0;
12956 12954 qreply(q, mp);
12957 12955 return;
12958 12956
12959 12957 protonak:
12960 12958 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12961 12959 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12962 12960 qreply(q, mp);
12963 12961 }
12964 12962
12965 12963 /*
12966 12964 * Process IP options in an outbound packet. Verify that the nexthop in a
12967 12965 * strict source route is onlink.
12968 12966 * Returns non-zero if something fails in which case an ICMP error has been
12969 12967 * sent and mp freed.
12970 12968 *
12971 12969 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12972 12970 */
12973 12971 int
12974 12972 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12975 12973 {
12976 12974 ipoptp_t opts;
12977 12975 uchar_t *opt;
12978 12976 uint8_t optval;
12979 12977 uint8_t optlen;
12980 12978 ipaddr_t dst;
12981 12979 intptr_t code = 0;
12982 12980 ire_t *ire;
12983 12981 ip_stack_t *ipst = ixa->ixa_ipst;
12984 12982 ip_recv_attr_t iras;
12985 12983
12986 12984 ip2dbg(("ip_output_options\n"));
12987 12985
12988 12986 dst = ipha->ipha_dst;
12989 12987 for (optval = ipoptp_first(&opts, ipha);
12990 12988 optval != IPOPT_EOL;
12991 12989 optval = ipoptp_next(&opts)) {
12992 12990 opt = opts.ipoptp_cur;
12993 12991 optlen = opts.ipoptp_len;
12994 12992 ip2dbg(("ip_output_options: opt %d, len %d\n",
12995 12993 optval, optlen));
12996 12994 switch (optval) {
12997 12995 uint32_t off;
12998 12996 case IPOPT_SSRR:
12999 12997 case IPOPT_LSRR:
13000 12998 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13001 12999 ip1dbg((
13002 13000 "ip_output_options: bad option offset\n"));
13003 13001 code = (char *)&opt[IPOPT_OLEN] -
13004 13002 (char *)ipha;
13005 13003 goto param_prob;
13006 13004 }
13007 13005 off = opt[IPOPT_OFFSET];
13008 13006 ip1dbg(("ip_output_options: next hop 0x%x\n",
13009 13007 ntohl(dst)));
13010 13008 /*
13011 13009 * For strict: verify that dst is directly
13012 13010 * reachable.
13013 13011 */
13014 13012 if (optval == IPOPT_SSRR) {
13015 13013 ire = ire_ftable_lookup_v4(dst, 0, 0,
13016 13014 IRE_INTERFACE, NULL, ALL_ZONES,
13017 13015 ixa->ixa_tsl,
13018 13016 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
13019 13017 NULL);
13020 13018 if (ire == NULL) {
13021 13019 ip1dbg(("ip_output_options: SSRR not"
13022 13020 " directly reachable: 0x%x\n",
13023 13021 ntohl(dst)));
13024 13022 goto bad_src_route;
13025 13023 }
13026 13024 ire_refrele(ire);
13027 13025 }
13028 13026 break;
13029 13027 case IPOPT_RR:
13030 13028 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13031 13029 ip1dbg((
13032 13030 "ip_output_options: bad option offset\n"));
13033 13031 code = (char *)&opt[IPOPT_OLEN] -
13034 13032 (char *)ipha;
13035 13033 goto param_prob;
13036 13034 }
13037 13035 break;
13038 13036 case IPOPT_TS:
13039 13037 /*
13040 13038 * Verify that length >=5 and that there is either
13041 13039 * room for another timestamp or that the overflow
13042 13040 * counter is not maxed out.
13043 13041 */
13044 13042 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
13045 13043 if (optlen < IPOPT_MINLEN_IT) {
13046 13044 goto param_prob;
13047 13045 }
13048 13046 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13049 13047 ip1dbg((
13050 13048 "ip_output_options: bad option offset\n"));
13051 13049 code = (char *)&opt[IPOPT_OFFSET] -
13052 13050 (char *)ipha;
13053 13051 goto param_prob;
13054 13052 }
13055 13053 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13056 13054 case IPOPT_TS_TSONLY:
13057 13055 off = IPOPT_TS_TIMELEN;
13058 13056 break;
13059 13057 case IPOPT_TS_TSANDADDR:
13060 13058 case IPOPT_TS_PRESPEC:
13061 13059 case IPOPT_TS_PRESPEC_RFC791:
13062 13060 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13063 13061 break;
13064 13062 default:
13065 13063 code = (char *)&opt[IPOPT_POS_OV_FLG] -
13066 13064 (char *)ipha;
13067 13065 goto param_prob;
13068 13066 }
13069 13067 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13070 13068 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13071 13069 /*
13072 13070 * No room and the overflow counter is 15
13073 13071 * already.
13074 13072 */
13075 13073 goto param_prob;
13076 13074 }
13077 13075 break;
13078 13076 }
13079 13077 }
13080 13078
13081 13079 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13082 13080 return (0);
13083 13081
13084 13082 ip1dbg(("ip_output_options: error processing IP options."));
13085 13083 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13086 13084
13087 13085 param_prob:
13088 13086 bzero(&iras, sizeof (iras));
13089 13087 iras.ira_ill = iras.ira_rill = ill;
13090 13088 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13091 13089 iras.ira_rifindex = iras.ira_ruifindex;
13092 13090 iras.ira_flags = IRAF_IS_IPV4;
13093 13091
13094 13092 ip_drop_output("ip_output_options", mp, ill);
13095 13093 icmp_param_problem(mp, (uint8_t)code, &iras);
13096 13094 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13097 13095 return (-1);
13098 13096
13099 13097 bad_src_route:
13100 13098 bzero(&iras, sizeof (iras));
13101 13099 iras.ira_ill = iras.ira_rill = ill;
13102 13100 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13103 13101 iras.ira_rifindex = iras.ira_ruifindex;
13104 13102 iras.ira_flags = IRAF_IS_IPV4;
13105 13103
13106 13104 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13107 13105 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13108 13106 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13109 13107 return (-1);
13110 13108 }
13111 13109
13112 13110 /*
13113 13111 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13114 13112 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13115 13113 * thru /etc/system.
13116 13114 */
13117 13115 #define CONN_MAXDRAINCNT 64
13118 13116
13119 13117 static void
13120 13118 conn_drain_init(ip_stack_t *ipst)
13121 13119 {
13122 13120 int i, j;
13123 13121 idl_tx_list_t *itl_tx;
13124 13122
13125 13123 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13126 13124
13127 13125 if ((ipst->ips_conn_drain_list_cnt == 0) ||
13128 13126 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13129 13127 /*
13130 13128 * Default value of the number of drainers is the
13131 13129 * number of cpus, subject to maximum of 8 drainers.
13132 13130 */
13133 13131 if (boot_max_ncpus != -1)
13134 13132 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13135 13133 else
13136 13134 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13137 13135 }
13138 13136
13139 13137 ipst->ips_idl_tx_list =
13140 13138 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13141 13139 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13142 13140 itl_tx = &ipst->ips_idl_tx_list[i];
13143 13141 itl_tx->txl_drain_list =
13144 13142 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13145 13143 sizeof (idl_t), KM_SLEEP);
13146 13144 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13147 13145 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13148 13146 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13149 13147 MUTEX_DEFAULT, NULL);
13150 13148 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13151 13149 }
13152 13150 }
13153 13151 }
13154 13152
13155 13153 static void
13156 13154 conn_drain_fini(ip_stack_t *ipst)
13157 13155 {
13158 13156 int i;
13159 13157 idl_tx_list_t *itl_tx;
13160 13158
13161 13159 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13162 13160 itl_tx = &ipst->ips_idl_tx_list[i];
13163 13161 kmem_free(itl_tx->txl_drain_list,
13164 13162 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13165 13163 }
13166 13164 kmem_free(ipst->ips_idl_tx_list,
13167 13165 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13168 13166 ipst->ips_idl_tx_list = NULL;
13169 13167 }
13170 13168
13171 13169 /*
13172 13170 * Flow control has blocked us from proceeding. Insert the given conn in one
13173 13171 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
13174 13172 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13175 13173 * will call conn_walk_drain(). See the flow control notes at the top of this
13176 13174 * file for more details.
13177 13175 */
13178 13176 void
13179 13177 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13180 13178 {
13181 13179 idl_t *idl = tx_list->txl_drain_list;
13182 13180 uint_t index;
13183 13181 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
13184 13182
13185 13183 mutex_enter(&connp->conn_lock);
13186 13184 if (connp->conn_state_flags & CONN_CLOSING) {
13187 13185 /*
13188 13186 * The conn is closing as a result of which CONN_CLOSING
13189 13187 * is set. Return.
13190 13188 */
13191 13189 mutex_exit(&connp->conn_lock);
13192 13190 return;
13193 13191 } else if (connp->conn_idl == NULL) {
13194 13192 /*
13195 13193 * Assign the next drain list round robin. We dont' use
13196 13194 * a lock, and thus it may not be strictly round robin.
13197 13195 * Atomicity of load/stores is enough to make sure that
13198 13196 * conn_drain_list_index is always within bounds.
13199 13197 */
13200 13198 index = tx_list->txl_drain_index;
13201 13199 ASSERT(index < ipst->ips_conn_drain_list_cnt);
13202 13200 connp->conn_idl = &tx_list->txl_drain_list[index];
13203 13201 index++;
13204 13202 if (index == ipst->ips_conn_drain_list_cnt)
13205 13203 index = 0;
13206 13204 tx_list->txl_drain_index = index;
13207 13205 } else {
13208 13206 ASSERT(connp->conn_idl->idl_itl == tx_list);
13209 13207 }
13210 13208 mutex_exit(&connp->conn_lock);
13211 13209
13212 13210 idl = connp->conn_idl;
13213 13211 mutex_enter(&idl->idl_lock);
13214 13212 if ((connp->conn_drain_prev != NULL) ||
13215 13213 (connp->conn_state_flags & CONN_CLOSING)) {
13216 13214 /*
13217 13215 * The conn is either already in the drain list or closing.
13218 13216 * (We needed to check for CONN_CLOSING again since close can
13219 13217 * sneak in between dropping conn_lock and acquiring idl_lock.)
13220 13218 */
13221 13219 mutex_exit(&idl->idl_lock);
13222 13220 return;
13223 13221 }
13224 13222
13225 13223 /*
13226 13224 * The conn is not in the drain list. Insert it at the
13227 13225 * tail of the drain list. The drain list is circular
13228 13226 * and doubly linked. idl_conn points to the 1st element
13229 13227 * in the list.
13230 13228 */
13231 13229 if (idl->idl_conn == NULL) {
13232 13230 idl->idl_conn = connp;
13233 13231 connp->conn_drain_next = connp;
13234 13232 connp->conn_drain_prev = connp;
13235 13233 } else {
13236 13234 conn_t *head = idl->idl_conn;
13237 13235
13238 13236 connp->conn_drain_next = head;
13239 13237 connp->conn_drain_prev = head->conn_drain_prev;
13240 13238 head->conn_drain_prev->conn_drain_next = connp;
13241 13239 head->conn_drain_prev = connp;
13242 13240 }
13243 13241 /*
13244 13242 * For non streams based sockets assert flow control.
13245 13243 */
13246 13244 conn_setqfull(connp, NULL);
13247 13245 mutex_exit(&idl->idl_lock);
13248 13246 }
13249 13247
13250 13248 static void
13251 13249 conn_drain_remove(conn_t *connp)
13252 13250 {
13253 13251 idl_t *idl = connp->conn_idl;
13254 13252
13255 13253 if (idl != NULL) {
13256 13254 /*
13257 13255 * Remove ourself from the drain list.
13258 13256 */
13259 13257 if (connp->conn_drain_next == connp) {
13260 13258 /* Singleton in the list */
13261 13259 ASSERT(connp->conn_drain_prev == connp);
13262 13260 idl->idl_conn = NULL;
13263 13261 } else {
13264 13262 connp->conn_drain_prev->conn_drain_next =
13265 13263 connp->conn_drain_next;
13266 13264 connp->conn_drain_next->conn_drain_prev =
13267 13265 connp->conn_drain_prev;
13268 13266 if (idl->idl_conn == connp)
13269 13267 idl->idl_conn = connp->conn_drain_next;
13270 13268 }
13271 13269
13272 13270 /*
13273 13271 * NOTE: because conn_idl is associated with a specific drain
13274 13272 * list which in turn is tied to the index the TX ring
13275 13273 * (txl_cookie) hashes to, and because the TX ring can change
13276 13274 * over the lifetime of the conn_t, we must clear conn_idl so
13277 13275 * a subsequent conn_drain_insert() will set conn_idl again
13278 13276 * based on the latest txl_cookie.
13279 13277 */
13280 13278 connp->conn_idl = NULL;
13281 13279 }
13282 13280 connp->conn_drain_next = NULL;
13283 13281 connp->conn_drain_prev = NULL;
13284 13282
13285 13283 conn_clrqfull(connp, NULL);
13286 13284 /*
13287 13285 * For streams based sockets open up flow control.
13288 13286 */
13289 13287 if (!IPCL_IS_NONSTR(connp))
13290 13288 enableok(connp->conn_wq);
13291 13289 }
13292 13290
13293 13291 /*
13294 13292 * This conn is closing, and we are called from ip_close. OR
13295 13293 * this conn is draining because flow-control on the ill has been relieved.
13296 13294 *
13297 13295 * We must also need to remove conn's on this idl from the list, and also
13298 13296 * inform the sockfs upcalls about the change in flow-control.
13299 13297 */
13300 13298 static void
13301 13299 conn_drain(conn_t *connp, boolean_t closing)
13302 13300 {
13303 13301 idl_t *idl;
13304 13302 conn_t *next_connp;
13305 13303
13306 13304 /*
13307 13305 * connp->conn_idl is stable at this point, and no lock is needed
13308 13306 * to check it. If we are called from ip_close, close has already
13309 13307 * set CONN_CLOSING, thus freezing the value of conn_idl, and
13310 13308 * called us only because conn_idl is non-null. If we are called thru
13311 13309 * service, conn_idl could be null, but it cannot change because
13312 13310 * service is single-threaded per queue, and there cannot be another
13313 13311 * instance of service trying to call conn_drain_insert on this conn
13314 13312 * now.
13315 13313 */
13316 13314 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13317 13315
13318 13316 /*
13319 13317 * If the conn doesn't exist or is not on a drain list, bail.
13320 13318 */
13321 13319 if (connp == NULL || connp->conn_idl == NULL ||
13322 13320 connp->conn_drain_prev == NULL) {
13323 13321 return;
13324 13322 }
13325 13323
13326 13324 idl = connp->conn_idl;
13327 13325 ASSERT(MUTEX_HELD(&idl->idl_lock));
13328 13326
13329 13327 if (!closing) {
13330 13328 next_connp = connp->conn_drain_next;
13331 13329 while (next_connp != connp) {
13332 13330 conn_t *delconnp = next_connp;
13333 13331
13334 13332 next_connp = next_connp->conn_drain_next;
13335 13333 conn_drain_remove(delconnp);
13336 13334 }
13337 13335 ASSERT(connp->conn_drain_next == idl->idl_conn);
13338 13336 }
13339 13337 conn_drain_remove(connp);
13340 13338 }
13341 13339
13342 13340 /*
13343 13341 * Write service routine. Shared perimeter entry point.
13344 13342 * The device queue's messages has fallen below the low water mark and STREAMS
13345 13343 * has backenabled the ill_wq. Send sockfs notification about flow-control on
13346 13344 * each waiting conn.
13347 13345 */
13348 13346 void
13349 13347 ip_wsrv(queue_t *q)
13350 13348 {
13351 13349 ill_t *ill;
13352 13350
13353 13351 ill = (ill_t *)q->q_ptr;
13354 13352 if (ill->ill_state_flags == 0) {
13355 13353 ip_stack_t *ipst = ill->ill_ipst;
13356 13354
13357 13355 /*
13358 13356 * The device flow control has opened up.
13359 13357 * Walk through conn drain lists and qenable the
13360 13358 * first conn in each list. This makes sense only
13361 13359 * if the stream is fully plumbed and setup.
13362 13360 * Hence the ill_state_flags check above.
13363 13361 */
13364 13362 ip1dbg(("ip_wsrv: walking\n"));
13365 13363 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13366 13364 enableok(ill->ill_wq);
13367 13365 }
13368 13366 }
13369 13367
13370 13368 /*
13371 13369 * Callback to disable flow control in IP.
13372 13370 *
13373 13371 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13374 13372 * is enabled.
13375 13373 *
13376 13374 * When MAC_TX() is not able to send any more packets, dld sets its queue
13377 13375 * to QFULL and enable the STREAMS flow control. Later, when the underlying
13378 13376 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13379 13377 * function and wakes up corresponding mac worker threads, which in turn
13380 13378 * calls this callback function, and disables flow control.
13381 13379 */
13382 13380 void
13383 13381 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13384 13382 {
13385 13383 ill_t *ill = (ill_t *)arg;
13386 13384 ip_stack_t *ipst = ill->ill_ipst;
13387 13385 idl_tx_list_t *idl_txl;
13388 13386
13389 13387 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13390 13388 mutex_enter(&idl_txl->txl_lock);
13391 13389 /* add code to to set a flag to indicate idl_txl is enabled */
13392 13390 conn_walk_drain(ipst, idl_txl);
13393 13391 mutex_exit(&idl_txl->txl_lock);
13394 13392 }
13395 13393
13396 13394 /*
13397 13395 * Flow control has been relieved and STREAMS has backenabled us; drain
13398 13396 * all the conn lists on `tx_list'.
13399 13397 */
13400 13398 static void
13401 13399 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13402 13400 {
13403 13401 int i;
13404 13402 idl_t *idl;
13405 13403
13406 13404 IP_STAT(ipst, ip_conn_walk_drain);
13407 13405
13408 13406 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13409 13407 idl = &tx_list->txl_drain_list[i];
13410 13408 mutex_enter(&idl->idl_lock);
13411 13409 conn_drain(idl->idl_conn, B_FALSE);
13412 13410 mutex_exit(&idl->idl_lock);
13413 13411 }
13414 13412 }
13415 13413
13416 13414 /*
13417 13415 * Determine if the ill and multicast aspects of that packets
13418 13416 * "matches" the conn.
13419 13417 */
13420 13418 boolean_t
13421 13419 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13422 13420 {
13423 13421 ill_t *ill = ira->ira_rill;
13424 13422 zoneid_t zoneid = ira->ira_zoneid;
13425 13423 uint_t in_ifindex;
13426 13424 ipaddr_t dst, src;
13427 13425
13428 13426 dst = ipha->ipha_dst;
13429 13427 src = ipha->ipha_src;
13430 13428
13431 13429 /*
13432 13430 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13433 13431 * unicast, broadcast and multicast reception to
13434 13432 * conn_incoming_ifindex.
13435 13433 * conn_wantpacket is called for unicast, broadcast and
13436 13434 * multicast packets.
13437 13435 */
13438 13436 in_ifindex = connp->conn_incoming_ifindex;
13439 13437
13440 13438 /* mpathd can bind to the under IPMP interface, which we allow */
13441 13439 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13442 13440 if (!IS_UNDER_IPMP(ill))
13443 13441 return (B_FALSE);
13444 13442
13445 13443 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13446 13444 return (B_FALSE);
13447 13445 }
13448 13446
13449 13447 if (!IPCL_ZONE_MATCH(connp, zoneid))
13450 13448 return (B_FALSE);
13451 13449
13452 13450 if (!(ira->ira_flags & IRAF_MULTICAST))
13453 13451 return (B_TRUE);
13454 13452
13455 13453 if (connp->conn_multi_router) {
13456 13454 /* multicast packet and multicast router socket: send up */
13457 13455 return (B_TRUE);
13458 13456 }
13459 13457
13460 13458 if (ipha->ipha_protocol == IPPROTO_PIM ||
13461 13459 ipha->ipha_protocol == IPPROTO_RSVP)
13462 13460 return (B_TRUE);
13463 13461
13464 13462 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13465 13463 }
13466 13464
13467 13465 void
13468 13466 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13469 13467 {
13470 13468 if (IPCL_IS_NONSTR(connp)) {
13471 13469 (*connp->conn_upcalls->su_txq_full)
13472 13470 (connp->conn_upper_handle, B_TRUE);
13473 13471 if (flow_stopped != NULL)
13474 13472 *flow_stopped = B_TRUE;
13475 13473 } else {
13476 13474 queue_t *q = connp->conn_wq;
13477 13475
13478 13476 ASSERT(q != NULL);
13479 13477 if (!(q->q_flag & QFULL)) {
13480 13478 mutex_enter(QLOCK(q));
13481 13479 if (!(q->q_flag & QFULL)) {
13482 13480 /* still need to set QFULL */
13483 13481 q->q_flag |= QFULL;
13484 13482 /* set flow_stopped to true under QLOCK */
13485 13483 if (flow_stopped != NULL)
13486 13484 *flow_stopped = B_TRUE;
13487 13485 mutex_exit(QLOCK(q));
13488 13486 } else {
13489 13487 /* flow_stopped is left unchanged */
13490 13488 mutex_exit(QLOCK(q));
13491 13489 }
13492 13490 }
13493 13491 }
13494 13492 }
13495 13493
13496 13494 void
13497 13495 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13498 13496 {
13499 13497 if (IPCL_IS_NONSTR(connp)) {
13500 13498 (*connp->conn_upcalls->su_txq_full)
13501 13499 (connp->conn_upper_handle, B_FALSE);
13502 13500 if (flow_stopped != NULL)
13503 13501 *flow_stopped = B_FALSE;
13504 13502 } else {
13505 13503 queue_t *q = connp->conn_wq;
13506 13504
13507 13505 ASSERT(q != NULL);
13508 13506 if (q->q_flag & QFULL) {
13509 13507 mutex_enter(QLOCK(q));
13510 13508 if (q->q_flag & QFULL) {
13511 13509 q->q_flag &= ~QFULL;
13512 13510 /* set flow_stopped to false under QLOCK */
13513 13511 if (flow_stopped != NULL)
13514 13512 *flow_stopped = B_FALSE;
13515 13513 mutex_exit(QLOCK(q));
13516 13514 if (q->q_flag & QWANTW)
13517 13515 qbackenable(q, 0);
13518 13516 } else {
13519 13517 /* flow_stopped is left unchanged */
13520 13518 mutex_exit(QLOCK(q));
13521 13519 }
13522 13520 }
13523 13521 }
13524 13522
13525 13523 mutex_enter(&connp->conn_lock);
13526 13524 connp->conn_blocked = B_FALSE;
13527 13525 mutex_exit(&connp->conn_lock);
13528 13526 }
13529 13527
13530 13528 /*
13531 13529 * Return the length in bytes of the IPv4 headers (base header, label, and
13532 13530 * other IP options) that will be needed based on the
13533 13531 * ip_pkt_t structure passed by the caller.
13534 13532 *
13535 13533 * The returned length does not include the length of the upper level
13536 13534 * protocol (ULP) header.
13537 13535 * The caller needs to check that the length doesn't exceed the max for IPv4.
13538 13536 */
13539 13537 int
13540 13538 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13541 13539 {
13542 13540 int len;
13543 13541
13544 13542 len = IP_SIMPLE_HDR_LENGTH;
13545 13543 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13546 13544 ASSERT(ipp->ipp_label_len_v4 != 0);
13547 13545 /* We need to round up here */
13548 13546 len += (ipp->ipp_label_len_v4 + 3) & ~3;
13549 13547 }
13550 13548
13551 13549 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13552 13550 ASSERT(ipp->ipp_ipv4_options_len != 0);
13553 13551 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13554 13552 len += ipp->ipp_ipv4_options_len;
13555 13553 }
13556 13554 return (len);
13557 13555 }
13558 13556
13559 13557 /*
13560 13558 * All-purpose routine to build an IPv4 header with options based
13561 13559 * on the abstract ip_pkt_t.
13562 13560 *
13563 13561 * The caller has to set the source and destination address as well as
13564 13562 * ipha_length. The caller has to massage any source route and compensate
13565 13563 * for the ULP pseudo-header checksum due to the source route.
13566 13564 */
13567 13565 void
13568 13566 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13569 13567 uint8_t protocol)
13570 13568 {
13571 13569 ipha_t *ipha = (ipha_t *)buf;
13572 13570 uint8_t *cp;
13573 13571
13574 13572 /* Initialize IPv4 header */
13575 13573 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13576 13574 ipha->ipha_length = 0; /* Caller will set later */
13577 13575 ipha->ipha_ident = 0;
13578 13576 ipha->ipha_fragment_offset_and_flags = 0;
13579 13577 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13580 13578 ipha->ipha_protocol = protocol;
13581 13579 ipha->ipha_hdr_checksum = 0;
13582 13580
13583 13581 if ((ipp->ipp_fields & IPPF_ADDR) &&
13584 13582 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13585 13583 ipha->ipha_src = ipp->ipp_addr_v4;
13586 13584
13587 13585 cp = (uint8_t *)&ipha[1];
13588 13586 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13589 13587 ASSERT(ipp->ipp_label_len_v4 != 0);
13590 13588 bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13591 13589 cp += ipp->ipp_label_len_v4;
13592 13590 /* We need to round up here */
13593 13591 while ((uintptr_t)cp & 0x3) {
13594 13592 *cp++ = IPOPT_NOP;
13595 13593 }
13596 13594 }
13597 13595
13598 13596 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13599 13597 ASSERT(ipp->ipp_ipv4_options_len != 0);
13600 13598 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13601 13599 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13602 13600 cp += ipp->ipp_ipv4_options_len;
13603 13601 }
13604 13602 ipha->ipha_version_and_hdr_length =
13605 13603 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13606 13604
13607 13605 ASSERT((int)(cp - buf) == buf_len);
13608 13606 }
13609 13607
13610 13608 /* Allocate the private structure */
13611 13609 static int
13612 13610 ip_priv_alloc(void **bufp)
13613 13611 {
13614 13612 void *buf;
13615 13613
13616 13614 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13617 13615 return (ENOMEM);
13618 13616
13619 13617 *bufp = buf;
13620 13618 return (0);
13621 13619 }
13622 13620
13623 13621 /* Function to delete the private structure */
13624 13622 void
13625 13623 ip_priv_free(void *buf)
13626 13624 {
13627 13625 ASSERT(buf != NULL);
13628 13626 kmem_free(buf, sizeof (ip_priv_t));
13629 13627 }
13630 13628
13631 13629 /*
13632 13630 * The entry point for IPPF processing.
13633 13631 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13634 13632 * routine just returns.
13635 13633 *
13636 13634 * When called, ip_process generates an ipp_packet_t structure
13637 13635 * which holds the state information for this packet and invokes the
13638 13636 * the classifier (via ipp_packet_process). The classification, depending on
13639 13637 * configured filters, results in a list of actions for this packet. Invoking
13640 13638 * an action may cause the packet to be dropped, in which case we return NULL.
13641 13639 * proc indicates the callout position for
13642 13640 * this packet and ill is the interface this packet arrived on or will leave
13643 13641 * on (inbound and outbound resp.).
13644 13642 *
13645 13643 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13646 13644 * on the ill corrsponding to the destination IP address.
13647 13645 */
13648 13646 mblk_t *
13649 13647 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13650 13648 {
13651 13649 ip_priv_t *priv;
13652 13650 ipp_action_id_t aid;
13653 13651 int rc = 0;
13654 13652 ipp_packet_t *pp;
13655 13653
13656 13654 /* If the classifier is not loaded, return */
13657 13655 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13658 13656 return (mp);
13659 13657 }
13660 13658
13661 13659 ASSERT(mp != NULL);
13662 13660
13663 13661 /* Allocate the packet structure */
13664 13662 rc = ipp_packet_alloc(&pp, "ip", aid);
13665 13663 if (rc != 0)
13666 13664 goto drop;
13667 13665
13668 13666 /* Allocate the private structure */
13669 13667 rc = ip_priv_alloc((void **)&priv);
13670 13668 if (rc != 0) {
13671 13669 ipp_packet_free(pp);
13672 13670 goto drop;
13673 13671 }
13674 13672 priv->proc = proc;
13675 13673 priv->ill_index = ill_get_upper_ifindex(rill);
13676 13674
13677 13675 ipp_packet_set_private(pp, priv, ip_priv_free);
13678 13676 ipp_packet_set_data(pp, mp);
13679 13677
13680 13678 /* Invoke the classifier */
13681 13679 rc = ipp_packet_process(&pp);
13682 13680 if (pp != NULL) {
13683 13681 mp = ipp_packet_get_data(pp);
13684 13682 ipp_packet_free(pp);
13685 13683 if (rc != 0)
13686 13684 goto drop;
13687 13685 return (mp);
13688 13686 } else {
13689 13687 /* No mp to trace in ip_drop_input/ip_drop_output */
13690 13688 mp = NULL;
13691 13689 }
13692 13690 drop:
13693 13691 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13694 13692 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13695 13693 ip_drop_input("ip_process", mp, ill);
13696 13694 } else {
13697 13695 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13698 13696 ip_drop_output("ip_process", mp, ill);
13699 13697 }
13700 13698 freemsg(mp);
13701 13699 return (NULL);
13702 13700 }
13703 13701
13704 13702 /*
13705 13703 * Propagate a multicast group membership operation (add/drop) on
13706 13704 * all the interfaces crossed by the related multirt routes.
13707 13705 * The call is considered successful if the operation succeeds
13708 13706 * on at least one interface.
13709 13707 *
13710 13708 * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13711 13709 * multicast addresses with the ire argument being the first one.
13712 13710 * We walk the bucket to find all the of those.
13713 13711 *
13714 13712 * Common to IPv4 and IPv6.
13715 13713 */
13716 13714 static int
13717 13715 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13718 13716 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13719 13717 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13720 13718 mcast_record_t fmode, const in6_addr_t *v6src)
13721 13719 {
13722 13720 ire_t *ire_gw;
13723 13721 irb_t *irb;
13724 13722 int ifindex;
13725 13723 int error = 0;
13726 13724 int result;
13727 13725 ip_stack_t *ipst = ire->ire_ipst;
13728 13726 ipaddr_t group;
13729 13727 boolean_t isv6;
13730 13728 int match_flags;
13731 13729
13732 13730 if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13733 13731 IN6_V4MAPPED_TO_IPADDR(v6group, group);
13734 13732 isv6 = B_FALSE;
13735 13733 } else {
13736 13734 isv6 = B_TRUE;
13737 13735 }
13738 13736
13739 13737 irb = ire->ire_bucket;
13740 13738 ASSERT(irb != NULL);
13741 13739
13742 13740 result = 0;
13743 13741 irb_refhold(irb);
13744 13742 for (; ire != NULL; ire = ire->ire_next) {
13745 13743 if ((ire->ire_flags & RTF_MULTIRT) == 0)
13746 13744 continue;
13747 13745
13748 13746 /* We handle -ifp routes by matching on the ill if set */
13749 13747 match_flags = MATCH_IRE_TYPE;
13750 13748 if (ire->ire_ill != NULL)
13751 13749 match_flags |= MATCH_IRE_ILL;
13752 13750
13753 13751 if (isv6) {
13754 13752 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13755 13753 continue;
13756 13754
13757 13755 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13758 13756 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13759 13757 match_flags, 0, ipst, NULL);
13760 13758 } else {
13761 13759 if (ire->ire_addr != group)
13762 13760 continue;
13763 13761
13764 13762 ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13765 13763 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13766 13764 match_flags, 0, ipst, NULL);
13767 13765 }
13768 13766 /* No interface route exists for the gateway; skip this ire. */
13769 13767 if (ire_gw == NULL)
13770 13768 continue;
13771 13769 if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13772 13770 ire_refrele(ire_gw);
13773 13771 continue;
13774 13772 }
13775 13773 ASSERT(ire_gw->ire_ill != NULL); /* IRE_INTERFACE */
13776 13774 ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13777 13775
13778 13776 /*
13779 13777 * The operation is considered a success if
13780 13778 * it succeeds at least once on any one interface.
13781 13779 */
13782 13780 error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13783 13781 fmode, v6src);
13784 13782 if (error == 0)
13785 13783 result = CGTP_MCAST_SUCCESS;
13786 13784
13787 13785 ire_refrele(ire_gw);
13788 13786 }
13789 13787 irb_refrele(irb);
13790 13788 /*
13791 13789 * Consider the call as successful if we succeeded on at least
13792 13790 * one interface. Otherwise, return the last encountered error.
13793 13791 */
13794 13792 return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13795 13793 }
13796 13794
13797 13795 /*
13798 13796 * Return the expected CGTP hooks version number.
13799 13797 */
13800 13798 int
13801 13799 ip_cgtp_filter_supported(void)
13802 13800 {
13803 13801 return (ip_cgtp_filter_rev);
13804 13802 }
13805 13803
13806 13804 /*
13807 13805 * CGTP hooks can be registered by invoking this function.
13808 13806 * Checks that the version number matches.
13809 13807 */
13810 13808 int
13811 13809 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13812 13810 {
13813 13811 netstack_t *ns;
13814 13812 ip_stack_t *ipst;
13815 13813
13816 13814 if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13817 13815 return (ENOTSUP);
13818 13816
13819 13817 ns = netstack_find_by_stackid(stackid);
13820 13818 if (ns == NULL)
13821 13819 return (EINVAL);
13822 13820 ipst = ns->netstack_ip;
13823 13821 ASSERT(ipst != NULL);
13824 13822
13825 13823 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13826 13824 netstack_rele(ns);
13827 13825 return (EALREADY);
13828 13826 }
13829 13827
13830 13828 ipst->ips_ip_cgtp_filter_ops = ops;
13831 13829
13832 13830 ill_set_inputfn_all(ipst);
13833 13831
13834 13832 netstack_rele(ns);
13835 13833 return (0);
13836 13834 }
13837 13835
13838 13836 /*
13839 13837 * CGTP hooks can be unregistered by invoking this function.
13840 13838 * Returns ENXIO if there was no registration.
13841 13839 * Returns EBUSY if the ndd variable has not been turned off.
13842 13840 */
13843 13841 int
13844 13842 ip_cgtp_filter_unregister(netstackid_t stackid)
13845 13843 {
13846 13844 netstack_t *ns;
13847 13845 ip_stack_t *ipst;
13848 13846
13849 13847 ns = netstack_find_by_stackid(stackid);
13850 13848 if (ns == NULL)
13851 13849 return (EINVAL);
13852 13850 ipst = ns->netstack_ip;
13853 13851 ASSERT(ipst != NULL);
13854 13852
13855 13853 if (ipst->ips_ip_cgtp_filter) {
13856 13854 netstack_rele(ns);
13857 13855 return (EBUSY);
13858 13856 }
13859 13857
13860 13858 if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13861 13859 netstack_rele(ns);
13862 13860 return (ENXIO);
13863 13861 }
13864 13862 ipst->ips_ip_cgtp_filter_ops = NULL;
13865 13863
13866 13864 ill_set_inputfn_all(ipst);
13867 13865
13868 13866 netstack_rele(ns);
13869 13867 return (0);
13870 13868 }
13871 13869
13872 13870 /*
13873 13871 * Check whether there is a CGTP filter registration.
13874 13872 * Returns non-zero if there is a registration, otherwise returns zero.
13875 13873 * Note: returns zero if bad stackid.
13876 13874 */
13877 13875 int
13878 13876 ip_cgtp_filter_is_registered(netstackid_t stackid)
13879 13877 {
13880 13878 netstack_t *ns;
13881 13879 ip_stack_t *ipst;
13882 13880 int ret;
13883 13881
13884 13882 ns = netstack_find_by_stackid(stackid);
13885 13883 if (ns == NULL)
13886 13884 return (0);
13887 13885 ipst = ns->netstack_ip;
13888 13886 ASSERT(ipst != NULL);
13889 13887
13890 13888 if (ipst->ips_ip_cgtp_filter_ops != NULL)
13891 13889 ret = 1;
13892 13890 else
13893 13891 ret = 0;
13894 13892
13895 13893 netstack_rele(ns);
13896 13894 return (ret);
13897 13895 }
13898 13896
13899 13897 static int
13900 13898 ip_squeue_switch(int val)
13901 13899 {
13902 13900 int rval;
13903 13901
13904 13902 switch (val) {
13905 13903 case IP_SQUEUE_ENTER_NODRAIN:
13906 13904 rval = SQ_NODRAIN;
13907 13905 break;
13908 13906 case IP_SQUEUE_ENTER:
13909 13907 rval = SQ_PROCESS;
13910 13908 break;
13911 13909 case IP_SQUEUE_FILL:
13912 13910 default:
13913 13911 rval = SQ_FILL;
13914 13912 break;
13915 13913 }
13916 13914 return (rval);
13917 13915 }
13918 13916
13919 13917 static void *
13920 13918 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13921 13919 {
13922 13920 kstat_t *ksp;
13923 13921
13924 13922 ip_stat_t template = {
13925 13923 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13926 13924 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13927 13925 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13928 13926 { "ip_db_ref", KSTAT_DATA_UINT64 },
13929 13927 { "ip_notaligned", KSTAT_DATA_UINT64 },
13930 13928 { "ip_multimblk", KSTAT_DATA_UINT64 },
13931 13929 { "ip_opt", KSTAT_DATA_UINT64 },
13932 13930 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13933 13931 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13934 13932 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13935 13933 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13936 13934 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13937 13935 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13938 13936 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13939 13937 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13940 13938 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13941 13939 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13942 13940 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13943 13941 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13944 13942 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13945 13943 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13946 13944 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13947 13945 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13948 13946 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13949 13947 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13950 13948 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
13951 13949 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
13952 13950 { "conn_in_recvif", KSTAT_DATA_UINT64 },
13953 13951 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
13954 13952 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
13955 13953 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
13956 13954 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
13957 13955 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
13958 13956 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
13959 13957 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
13960 13958 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
13961 13959 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
13962 13960 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
13963 13961 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
13964 13962 };
13965 13963
13966 13964 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13967 13965 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13968 13966 KSTAT_FLAG_VIRTUAL, stackid);
13969 13967
13970 13968 if (ksp == NULL)
13971 13969 return (NULL);
13972 13970
13973 13971 bcopy(&template, ip_statisticsp, sizeof (template));
13974 13972 ksp->ks_data = (void *)ip_statisticsp;
13975 13973 ksp->ks_private = (void *)(uintptr_t)stackid;
13976 13974
13977 13975 kstat_install(ksp);
13978 13976 return (ksp);
13979 13977 }
13980 13978
13981 13979 static void
13982 13980 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13983 13981 {
13984 13982 if (ksp != NULL) {
13985 13983 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13986 13984 kstat_delete_netstack(ksp, stackid);
13987 13985 }
13988 13986 }
13989 13987
13990 13988 static void *
13991 13989 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13992 13990 {
13993 13991 kstat_t *ksp;
13994 13992
13995 13993 ip_named_kstat_t template = {
13996 13994 { "forwarding", KSTAT_DATA_UINT32, 0 },
13997 13995 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
13998 13996 { "inReceives", KSTAT_DATA_UINT64, 0 },
13999 13997 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
14000 13998 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
14001 13999 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
14002 14000 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
14003 14001 { "inDiscards", KSTAT_DATA_UINT32, 0 },
14004 14002 { "inDelivers", KSTAT_DATA_UINT64, 0 },
14005 14003 { "outRequests", KSTAT_DATA_UINT64, 0 },
14006 14004 { "outDiscards", KSTAT_DATA_UINT32, 0 },
14007 14005 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
14008 14006 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
14009 14007 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
14010 14008 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
14011 14009 { "reasmFails", KSTAT_DATA_UINT32, 0 },
14012 14010 { "fragOKs", KSTAT_DATA_UINT32, 0 },
14013 14011 { "fragFails", KSTAT_DATA_UINT32, 0 },
14014 14012 { "fragCreates", KSTAT_DATA_UINT32, 0 },
14015 14013 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
14016 14014 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
14017 14015 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
14018 14016 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
14019 14017 { "inErrs", KSTAT_DATA_UINT32, 0 },
14020 14018 { "noPorts", KSTAT_DATA_UINT32, 0 },
14021 14019 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
14022 14020 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
14023 14021 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
14024 14022 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
14025 14023 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
14026 14024 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
14027 14025 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
14028 14026 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
14029 14027 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
14030 14028 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
14031 14029 { "inIPv6", KSTAT_DATA_UINT32, 0 },
14032 14030 { "outIPv6", KSTAT_DATA_UINT32, 0 },
14033 14031 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
14034 14032 };
14035 14033
14036 14034 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
14037 14035 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
14038 14036 if (ksp == NULL || ksp->ks_data == NULL)
14039 14037 return (NULL);
14040 14038
14041 14039 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
14042 14040 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
14043 14041 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14044 14042 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14045 14043 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14046 14044
14047 14045 template.netToMediaEntrySize.value.i32 =
14048 14046 sizeof (mib2_ipNetToMediaEntry_t);
14049 14047
14050 14048 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14051 14049
14052 14050 bcopy(&template, ksp->ks_data, sizeof (template));
14053 14051 ksp->ks_update = ip_kstat_update;
14054 14052 ksp->ks_private = (void *)(uintptr_t)stackid;
14055 14053
14056 14054 kstat_install(ksp);
14057 14055 return (ksp);
14058 14056 }
14059 14057
14060 14058 static void
14061 14059 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14062 14060 {
14063 14061 if (ksp != NULL) {
14064 14062 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14065 14063 kstat_delete_netstack(ksp, stackid);
14066 14064 }
14067 14065 }
14068 14066
14069 14067 static int
14070 14068 ip_kstat_update(kstat_t *kp, int rw)
14071 14069 {
14072 14070 ip_named_kstat_t *ipkp;
14073 14071 mib2_ipIfStatsEntry_t ipmib;
14074 14072 ill_walk_context_t ctx;
14075 14073 ill_t *ill;
14076 14074 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14077 14075 netstack_t *ns;
14078 14076 ip_stack_t *ipst;
14079 14077
14080 14078 if (kp == NULL || kp->ks_data == NULL)
14081 14079 return (EIO);
14082 14080
14083 14081 if (rw == KSTAT_WRITE)
14084 14082 return (EACCES);
14085 14083
14086 14084 ns = netstack_find_by_stackid(stackid);
14087 14085 if (ns == NULL)
14088 14086 return (-1);
14089 14087 ipst = ns->netstack_ip;
14090 14088 if (ipst == NULL) {
14091 14089 netstack_rele(ns);
14092 14090 return (-1);
14093 14091 }
14094 14092 ipkp = (ip_named_kstat_t *)kp->ks_data;
14095 14093
14096 14094 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14097 14095 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14098 14096 ill = ILL_START_WALK_V4(&ctx, ipst);
14099 14097 for (; ill != NULL; ill = ill_next(&ctx, ill))
14100 14098 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14101 14099 rw_exit(&ipst->ips_ill_g_lock);
14102 14100
14103 14101 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
14104 14102 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
14105 14103 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
14106 14104 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
14107 14105 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
14108 14106 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14109 14107 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
14110 14108 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
14111 14109 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
14112 14110 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
14113 14111 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
14114 14112 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
14115 14113 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14116 14114 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
14117 14115 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
14118 14116 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
14119 14117 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
14120 14118 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
14121 14119 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
14122 14120
14123 14121 ipkp->routingDiscards.value.ui32 = 0;
14124 14122 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
14125 14123 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
14126 14124 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
14127 14125 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
14128 14126 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
14129 14127 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
14130 14128 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
14131 14129 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
14132 14130 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
14133 14131 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
14134 14132 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
14135 14133
14136 14134 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
14137 14135 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
14138 14136 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14139 14137
14140 14138 netstack_rele(ns);
14141 14139
14142 14140 return (0);
14143 14141 }
14144 14142
14145 14143 static void *
14146 14144 icmp_kstat_init(netstackid_t stackid)
14147 14145 {
14148 14146 kstat_t *ksp;
14149 14147
14150 14148 icmp_named_kstat_t template = {
14151 14149 { "inMsgs", KSTAT_DATA_UINT32 },
14152 14150 { "inErrors", KSTAT_DATA_UINT32 },
14153 14151 { "inDestUnreachs", KSTAT_DATA_UINT32 },
14154 14152 { "inTimeExcds", KSTAT_DATA_UINT32 },
14155 14153 { "inParmProbs", KSTAT_DATA_UINT32 },
14156 14154 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
14157 14155 { "inRedirects", KSTAT_DATA_UINT32 },
14158 14156 { "inEchos", KSTAT_DATA_UINT32 },
14159 14157 { "inEchoReps", KSTAT_DATA_UINT32 },
14160 14158 { "inTimestamps", KSTAT_DATA_UINT32 },
14161 14159 { "inTimestampReps", KSTAT_DATA_UINT32 },
14162 14160 { "inAddrMasks", KSTAT_DATA_UINT32 },
14163 14161 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
14164 14162 { "outMsgs", KSTAT_DATA_UINT32 },
14165 14163 { "outErrors", KSTAT_DATA_UINT32 },
14166 14164 { "outDestUnreachs", KSTAT_DATA_UINT32 },
14167 14165 { "outTimeExcds", KSTAT_DATA_UINT32 },
14168 14166 { "outParmProbs", KSTAT_DATA_UINT32 },
14169 14167 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
14170 14168 { "outRedirects", KSTAT_DATA_UINT32 },
14171 14169 { "outEchos", KSTAT_DATA_UINT32 },
14172 14170 { "outEchoReps", KSTAT_DATA_UINT32 },
14173 14171 { "outTimestamps", KSTAT_DATA_UINT32 },
14174 14172 { "outTimestampReps", KSTAT_DATA_UINT32 },
14175 14173 { "outAddrMasks", KSTAT_DATA_UINT32 },
14176 14174 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
14177 14175 { "inChksumErrs", KSTAT_DATA_UINT32 },
14178 14176 { "inUnknowns", KSTAT_DATA_UINT32 },
14179 14177 { "inFragNeeded", KSTAT_DATA_UINT32 },
14180 14178 { "outFragNeeded", KSTAT_DATA_UINT32 },
14181 14179 { "outDrops", KSTAT_DATA_UINT32 },
14182 14180 { "inOverFlows", KSTAT_DATA_UINT32 },
14183 14181 { "inBadRedirects", KSTAT_DATA_UINT32 },
14184 14182 };
14185 14183
14186 14184 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14187 14185 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14188 14186 if (ksp == NULL || ksp->ks_data == NULL)
14189 14187 return (NULL);
14190 14188
14191 14189 bcopy(&template, ksp->ks_data, sizeof (template));
14192 14190
14193 14191 ksp->ks_update = icmp_kstat_update;
14194 14192 ksp->ks_private = (void *)(uintptr_t)stackid;
14195 14193
14196 14194 kstat_install(ksp);
14197 14195 return (ksp);
14198 14196 }
14199 14197
14200 14198 static void
14201 14199 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14202 14200 {
14203 14201 if (ksp != NULL) {
14204 14202 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14205 14203 kstat_delete_netstack(ksp, stackid);
14206 14204 }
14207 14205 }
14208 14206
14209 14207 static int
14210 14208 icmp_kstat_update(kstat_t *kp, int rw)
14211 14209 {
14212 14210 icmp_named_kstat_t *icmpkp;
14213 14211 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14214 14212 netstack_t *ns;
14215 14213 ip_stack_t *ipst;
14216 14214
14217 14215 if ((kp == NULL) || (kp->ks_data == NULL))
14218 14216 return (EIO);
14219 14217
14220 14218 if (rw == KSTAT_WRITE)
14221 14219 return (EACCES);
14222 14220
14223 14221 ns = netstack_find_by_stackid(stackid);
14224 14222 if (ns == NULL)
14225 14223 return (-1);
14226 14224 ipst = ns->netstack_ip;
14227 14225 if (ipst == NULL) {
14228 14226 netstack_rele(ns);
14229 14227 return (-1);
14230 14228 }
14231 14229 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14232 14230
14233 14231 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
14234 14232 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
14235 14233 icmpkp->inDestUnreachs.value.ui32 =
14236 14234 ipst->ips_icmp_mib.icmpInDestUnreachs;
14237 14235 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
14238 14236 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
14239 14237 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
14240 14238 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
14241 14239 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
14242 14240 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
14243 14241 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
14244 14242 icmpkp->inTimestampReps.value.ui32 =
14245 14243 ipst->ips_icmp_mib.icmpInTimestampReps;
14246 14244 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
14247 14245 icmpkp->inAddrMaskReps.value.ui32 =
14248 14246 ipst->ips_icmp_mib.icmpInAddrMaskReps;
14249 14247 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
14250 14248 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
14251 14249 icmpkp->outDestUnreachs.value.ui32 =
14252 14250 ipst->ips_icmp_mib.icmpOutDestUnreachs;
14253 14251 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
14254 14252 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
14255 14253 icmpkp->outSrcQuenchs.value.ui32 =
14256 14254 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14257 14255 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
14258 14256 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
14259 14257 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
14260 14258 icmpkp->outTimestamps.value.ui32 =
14261 14259 ipst->ips_icmp_mib.icmpOutTimestamps;
14262 14260 icmpkp->outTimestampReps.value.ui32 =
14263 14261 ipst->ips_icmp_mib.icmpOutTimestampReps;
14264 14262 icmpkp->outAddrMasks.value.ui32 =
14265 14263 ipst->ips_icmp_mib.icmpOutAddrMasks;
14266 14264 icmpkp->outAddrMaskReps.value.ui32 =
14267 14265 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14268 14266 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
14269 14267 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
14270 14268 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
14271 14269 icmpkp->outFragNeeded.value.ui32 =
14272 14270 ipst->ips_icmp_mib.icmpOutFragNeeded;
14273 14271 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
14274 14272 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
14275 14273 icmpkp->inBadRedirects.value.ui32 =
14276 14274 ipst->ips_icmp_mib.icmpInBadRedirects;
14277 14275
14278 14276 netstack_rele(ns);
14279 14277 return (0);
14280 14278 }
14281 14279
14282 14280 /*
14283 14281 * This is the fanout function for raw socket opened for SCTP. Note
14284 14282 * that it is called after SCTP checks that there is no socket which
14285 14283 * wants a packet. Then before SCTP handles this out of the blue packet,
14286 14284 * this function is called to see if there is any raw socket for SCTP.
14287 14285 * If there is and it is bound to the correct address, the packet will
14288 14286 * be sent to that socket. Note that only one raw socket can be bound to
14289 14287 * a port. This is assured in ipcl_sctp_hash_insert();
14290 14288 */
14291 14289 void
14292 14290 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14293 14291 ip_recv_attr_t *ira)
14294 14292 {
14295 14293 conn_t *connp;
14296 14294 queue_t *rq;
14297 14295 boolean_t secure;
14298 14296 ill_t *ill = ira->ira_ill;
14299 14297 ip_stack_t *ipst = ill->ill_ipst;
14300 14298 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
14301 14299 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
14302 14300 iaflags_t iraflags = ira->ira_flags;
14303 14301 ill_t *rill = ira->ira_rill;
14304 14302
14305 14303 secure = iraflags & IRAF_IPSEC_SECURE;
14306 14304
14307 14305 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14308 14306 ira, ipst);
14309 14307 if (connp == NULL) {
14310 14308 /*
14311 14309 * Although raw sctp is not summed, OOB chunks must be.
14312 14310 * Drop the packet here if the sctp checksum failed.
14313 14311 */
14314 14312 if (iraflags & IRAF_SCTP_CSUM_ERR) {
14315 14313 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14316 14314 freemsg(mp);
14317 14315 return;
14318 14316 }
14319 14317 ira->ira_ill = ira->ira_rill = NULL;
14320 14318 sctp_ootb_input(mp, ira, ipst);
14321 14319 ira->ira_ill = ill;
14322 14320 ira->ira_rill = rill;
14323 14321 return;
14324 14322 }
14325 14323 rq = connp->conn_rq;
14326 14324 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14327 14325 CONN_DEC_REF(connp);
14328 14326 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14329 14327 freemsg(mp);
14330 14328 return;
14331 14329 }
14332 14330 if (((iraflags & IRAF_IS_IPV4) ?
14333 14331 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14334 14332 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14335 14333 secure) {
14336 14334 mp = ipsec_check_inbound_policy(mp, connp, ipha,
14337 14335 ip6h, ira);
14338 14336 if (mp == NULL) {
14339 14337 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14340 14338 /* Note that mp is NULL */
14341 14339 ip_drop_input("ipIfStatsInDiscards", mp, ill);
14342 14340 CONN_DEC_REF(connp);
14343 14341 return;
14344 14342 }
14345 14343 }
14346 14344
14347 14345 if (iraflags & IRAF_ICMP_ERROR) {
14348 14346 (connp->conn_recvicmp)(connp, mp, NULL, ira);
14349 14347 } else {
14350 14348 ill_t *rill = ira->ira_rill;
14351 14349
14352 14350 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14353 14351 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14354 14352 ira->ira_ill = ira->ira_rill = NULL;
14355 14353 (connp->conn_recv)(connp, mp, NULL, ira);
14356 14354 ira->ira_ill = ill;
14357 14355 ira->ira_rill = rill;
14358 14356 }
14359 14357 CONN_DEC_REF(connp);
14360 14358 }
14361 14359
14362 14360 /*
14363 14361 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14364 14362 * header before the ip payload.
14365 14363 */
14366 14364 static void
14367 14365 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14368 14366 {
14369 14367 int len = (mp->b_wptr - mp->b_rptr);
14370 14368 mblk_t *ip_mp;
14371 14369
14372 14370 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14373 14371 if (is_fp_mp || len != fp_mp_len) {
14374 14372 if (len > fp_mp_len) {
14375 14373 /*
14376 14374 * fastpath header and ip header in the first mblk
14377 14375 */
14378 14376 mp->b_rptr += fp_mp_len;
14379 14377 } else {
14380 14378 /*
14381 14379 * ip_xmit_attach_llhdr had to prepend an mblk to
14382 14380 * attach the fastpath header before ip header.
14383 14381 */
14384 14382 ip_mp = mp->b_cont;
14385 14383 freeb(mp);
14386 14384 mp = ip_mp;
14387 14385 mp->b_rptr += (fp_mp_len - len);
14388 14386 }
14389 14387 } else {
14390 14388 ip_mp = mp->b_cont;
14391 14389 freeb(mp);
14392 14390 mp = ip_mp;
14393 14391 }
14394 14392 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14395 14393 freemsg(mp);
14396 14394 }
14397 14395
14398 14396 /*
14399 14397 * Normal post fragmentation function.
14400 14398 *
14401 14399 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14402 14400 * using the same state machine.
14403 14401 *
14404 14402 * We return an error on failure. In particular we return EWOULDBLOCK
14405 14403 * when the driver flow controls. In that case this ensures that ip_wsrv runs
14406 14404 * (currently by canputnext failure resulting in backenabling from GLD.)
14407 14405 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14408 14406 * indication that they can flow control until ip_wsrv() tells then to restart.
14409 14407 *
14410 14408 * If the nce passed by caller is incomplete, this function
14411 14409 * queues the packet and if necessary, sends ARP request and bails.
14412 14410 * If the Neighbor Cache passed is fully resolved, we simply prepend
14413 14411 * the link-layer header to the packet, do ipsec hw acceleration
14414 14412 * work if necessary, and send the packet out on the wire.
14415 14413 */
14416 14414 /* ARGSUSED6 */
14417 14415 int
14418 14416 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14419 14417 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14420 14418 {
14421 14419 queue_t *wq;
14422 14420 ill_t *ill = nce->nce_ill;
14423 14421 ip_stack_t *ipst = ill->ill_ipst;
14424 14422 uint64_t delta;
14425 14423 boolean_t isv6 = ill->ill_isv6;
14426 14424 boolean_t fp_mp;
14427 14425 ncec_t *ncec = nce->nce_common;
14428 14426 int64_t now = LBOLT_FASTPATH64;
14429 14427 boolean_t is_probe;
14430 14428
14431 14429 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14432 14430
14433 14431 ASSERT(mp != NULL);
14434 14432 ASSERT(mp->b_datap->db_type == M_DATA);
14435 14433 ASSERT(pkt_len == msgdsize(mp));
14436 14434
14437 14435 /*
14438 14436 * If we have already been here and are coming back after ARP/ND.
14439 14437 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14440 14438 * in that case since they have seen the packet when it came here
14441 14439 * the first time.
14442 14440 */
14443 14441 if (ixaflags & IXAF_NO_TRACE)
14444 14442 goto sendit;
14445 14443
14446 14444 if (ixaflags & IXAF_IS_IPV4) {
14447 14445 ipha_t *ipha = (ipha_t *)mp->b_rptr;
14448 14446
14449 14447 ASSERT(!isv6);
14450 14448 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14451 14449 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14452 14450 !(ixaflags & IXAF_NO_PFHOOK)) {
14453 14451 int error;
14454 14452
14455 14453 FW_HOOKS(ipst->ips_ip4_physical_out_event,
14456 14454 ipst->ips_ipv4firewall_physical_out,
14457 14455 NULL, ill, ipha, mp, mp, 0, ipst, error);
14458 14456 DTRACE_PROBE1(ip4__physical__out__end,
14459 14457 mblk_t *, mp);
14460 14458 if (mp == NULL)
14461 14459 return (error);
14462 14460
14463 14461 /* The length could have changed */
14464 14462 pkt_len = msgdsize(mp);
14465 14463 }
14466 14464 if (ipst->ips_ip4_observe.he_interested) {
14467 14465 /*
14468 14466 * Note that for TX the zoneid is the sending
14469 14467 * zone, whether or not MLP is in play.
14470 14468 * Since the szone argument is the IP zoneid (i.e.,
14471 14469 * zero for exclusive-IP zones) and ipobs wants
14472 14470 * the system zoneid, we map it here.
14473 14471 */
14474 14472 szone = IP_REAL_ZONEID(szone, ipst);
14475 14473
14476 14474 /*
14477 14475 * On the outbound path the destination zone will be
14478 14476 * unknown as we're sending this packet out on the
14479 14477 * wire.
14480 14478 */
14481 14479 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14482 14480 ill, ipst);
14483 14481 }
14484 14482 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14485 14483 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
14486 14484 ipha_t *, ipha, ip6_t *, NULL, int, 0);
14487 14485 } else {
14488 14486 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14489 14487
14490 14488 ASSERT(isv6);
14491 14489 ASSERT(pkt_len ==
14492 14490 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14493 14491 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14494 14492 !(ixaflags & IXAF_NO_PFHOOK)) {
14495 14493 int error;
14496 14494
14497 14495 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14498 14496 ipst->ips_ipv6firewall_physical_out,
14499 14497 NULL, ill, ip6h, mp, mp, 0, ipst, error);
14500 14498 DTRACE_PROBE1(ip6__physical__out__end,
14501 14499 mblk_t *, mp);
14502 14500 if (mp == NULL)
14503 14501 return (error);
14504 14502
14505 14503 /* The length could have changed */
14506 14504 pkt_len = msgdsize(mp);
14507 14505 }
14508 14506 if (ipst->ips_ip6_observe.he_interested) {
14509 14507 /* See above */
14510 14508 szone = IP_REAL_ZONEID(szone, ipst);
14511 14509
14512 14510 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14513 14511 ill, ipst);
14514 14512 }
14515 14513 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14516 14514 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
14517 14515 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14518 14516 }
14519 14517
14520 14518 sendit:
14521 14519 /*
14522 14520 * We check the state without a lock because the state can never
14523 14521 * move "backwards" to initial or incomplete.
14524 14522 */
14525 14523 switch (ncec->ncec_state) {
14526 14524 case ND_REACHABLE:
14527 14525 case ND_STALE:
14528 14526 case ND_DELAY:
14529 14527 case ND_PROBE:
14530 14528 mp = ip_xmit_attach_llhdr(mp, nce);
14531 14529 if (mp == NULL) {
14532 14530 /*
14533 14531 * ip_xmit_attach_llhdr has increased
14534 14532 * ipIfStatsOutDiscards and called ip_drop_output()
14535 14533 */
14536 14534 return (ENOBUFS);
14537 14535 }
14538 14536 /*
14539 14537 * check if nce_fastpath completed and we tagged on a
14540 14538 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14541 14539 */
14542 14540 fp_mp = (mp->b_datap->db_type == M_DATA);
14543 14541
14544 14542 if (fp_mp &&
14545 14543 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14546 14544 ill_dld_direct_t *idd;
14547 14545
14548 14546 idd = &ill->ill_dld_capab->idc_direct;
14549 14547 /*
14550 14548 * Send the packet directly to DLD, where it
14551 14549 * may be queued depending on the availability
14552 14550 * of transmit resources at the media layer.
14553 14551 * Return value should be taken into
14554 14552 * account and flow control the TCP.
14555 14553 */
14556 14554 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14557 14555 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14558 14556 pkt_len);
14559 14557
14560 14558 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14561 14559 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14562 14560 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14563 14561 } else {
14564 14562 uintptr_t cookie;
14565 14563
14566 14564 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14567 14565 mp, (uintptr_t)xmit_hint, 0)) != 0) {
14568 14566 if (ixacookie != NULL)
14569 14567 *ixacookie = cookie;
14570 14568 return (EWOULDBLOCK);
14571 14569 }
14572 14570 }
14573 14571 } else {
14574 14572 wq = ill->ill_wq;
14575 14573
14576 14574 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14577 14575 !canputnext(wq)) {
14578 14576 if (ixacookie != NULL)
14579 14577 *ixacookie = 0;
14580 14578 ip_xmit_flowctl_drop(ill, mp, fp_mp,
14581 14579 nce->nce_fp_mp != NULL ?
14582 14580 MBLKL(nce->nce_fp_mp) : 0);
14583 14581 return (EWOULDBLOCK);
14584 14582 }
14585 14583 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14586 14584 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14587 14585 pkt_len);
14588 14586 putnext(wq, mp);
14589 14587 }
14590 14588
14591 14589 /*
14592 14590 * The rest of this function implements Neighbor Unreachability
14593 14591 * detection. Determine if the ncec is eligible for NUD.
14594 14592 */
14595 14593 if (ncec->ncec_flags & NCE_F_NONUD)
14596 14594 return (0);
14597 14595
14598 14596 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14599 14597
14600 14598 /*
14601 14599 * Check for upper layer advice
14602 14600 */
14603 14601 if (ixaflags & IXAF_REACH_CONF) {
14604 14602 timeout_id_t tid;
14605 14603
14606 14604 /*
14607 14605 * It should be o.k. to check the state without
14608 14606 * a lock here, at most we lose an advice.
14609 14607 */
14610 14608 ncec->ncec_last = TICK_TO_MSEC(now);
14611 14609 if (ncec->ncec_state != ND_REACHABLE) {
14612 14610 mutex_enter(&ncec->ncec_lock);
14613 14611 ncec->ncec_state = ND_REACHABLE;
14614 14612 tid = ncec->ncec_timeout_id;
14615 14613 ncec->ncec_timeout_id = 0;
14616 14614 mutex_exit(&ncec->ncec_lock);
14617 14615 (void) untimeout(tid);
14618 14616 if (ip_debug > 2) {
14619 14617 /* ip1dbg */
14620 14618 pr_addr_dbg("ip_xmit: state"
14621 14619 " for %s changed to"
14622 14620 " REACHABLE\n", AF_INET6,
14623 14621 &ncec->ncec_addr);
14624 14622 }
14625 14623 }
14626 14624 return (0);
14627 14625 }
14628 14626
14629 14627 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14630 14628 ip1dbg(("ip_xmit: delta = %" PRId64
14631 14629 " ill_reachable_time = %d \n", delta,
14632 14630 ill->ill_reachable_time));
14633 14631 if (delta > (uint64_t)ill->ill_reachable_time) {
14634 14632 mutex_enter(&ncec->ncec_lock);
14635 14633 switch (ncec->ncec_state) {
14636 14634 case ND_REACHABLE:
14637 14635 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14638 14636 /* FALLTHROUGH */
14639 14637 case ND_STALE:
14640 14638 /*
14641 14639 * ND_REACHABLE is identical to
14642 14640 * ND_STALE in this specific case. If
14643 14641 * reachable time has expired for this
14644 14642 * neighbor (delta is greater than
14645 14643 * reachable time), conceptually, the
14646 14644 * neighbor cache is no longer in
14647 14645 * REACHABLE state, but already in
14648 14646 * STALE state. So the correct
14649 14647 * transition here is to ND_DELAY.
14650 14648 */
14651 14649 ncec->ncec_state = ND_DELAY;
14652 14650 mutex_exit(&ncec->ncec_lock);
14653 14651 nce_restart_timer(ncec,
14654 14652 ipst->ips_delay_first_probe_time);
14655 14653 if (ip_debug > 3) {
14656 14654 /* ip2dbg */
14657 14655 pr_addr_dbg("ip_xmit: state"
14658 14656 " for %s changed to"
14659 14657 " DELAY\n", AF_INET6,
14660 14658 &ncec->ncec_addr);
14661 14659 }
14662 14660 break;
14663 14661 case ND_DELAY:
14664 14662 case ND_PROBE:
14665 14663 mutex_exit(&ncec->ncec_lock);
14666 14664 /* Timers have already started */
14667 14665 break;
14668 14666 case ND_UNREACHABLE:
14669 14667 /*
14670 14668 * nce_timer has detected that this ncec
14671 14669 * is unreachable and initiated deleting
14672 14670 * this ncec.
14673 14671 * This is a harmless race where we found the
14674 14672 * ncec before it was deleted and have
14675 14673 * just sent out a packet using this
14676 14674 * unreachable ncec.
14677 14675 */
14678 14676 mutex_exit(&ncec->ncec_lock);
14679 14677 break;
14680 14678 default:
14681 14679 ASSERT(0);
14682 14680 mutex_exit(&ncec->ncec_lock);
14683 14681 }
14684 14682 }
14685 14683 return (0);
14686 14684
14687 14685 case ND_INCOMPLETE:
14688 14686 /*
14689 14687 * the state could have changed since we didn't hold the lock.
14690 14688 * Re-verify state under lock.
14691 14689 */
14692 14690 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14693 14691 mutex_enter(&ncec->ncec_lock);
14694 14692 if (NCE_ISREACHABLE(ncec)) {
14695 14693 mutex_exit(&ncec->ncec_lock);
14696 14694 goto sendit;
14697 14695 }
14698 14696 /* queue the packet */
14699 14697 nce_queue_mp(ncec, mp, is_probe);
14700 14698 mutex_exit(&ncec->ncec_lock);
14701 14699 DTRACE_PROBE2(ip__xmit__incomplete,
14702 14700 (ncec_t *), ncec, (mblk_t *), mp);
14703 14701 return (0);
14704 14702
14705 14703 case ND_INITIAL:
14706 14704 /*
14707 14705 * State could have changed since we didn't hold the lock, so
14708 14706 * re-verify state.
14709 14707 */
14710 14708 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14711 14709 mutex_enter(&ncec->ncec_lock);
14712 14710 if (NCE_ISREACHABLE(ncec)) {
14713 14711 mutex_exit(&ncec->ncec_lock);
14714 14712 goto sendit;
14715 14713 }
14716 14714 nce_queue_mp(ncec, mp, is_probe);
14717 14715 if (ncec->ncec_state == ND_INITIAL) {
14718 14716 ncec->ncec_state = ND_INCOMPLETE;
14719 14717 mutex_exit(&ncec->ncec_lock);
14720 14718 /*
14721 14719 * figure out the source we want to use
14722 14720 * and resolve it.
14723 14721 */
14724 14722 ip_ndp_resolve(ncec);
14725 14723 } else {
14726 14724 mutex_exit(&ncec->ncec_lock);
14727 14725 }
14728 14726 return (0);
14729 14727
14730 14728 case ND_UNREACHABLE:
14731 14729 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14732 14730 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14733 14731 mp, ill);
14734 14732 freemsg(mp);
14735 14733 return (0);
14736 14734
14737 14735 default:
14738 14736 ASSERT(0);
14739 14737 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14740 14738 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14741 14739 mp, ill);
14742 14740 freemsg(mp);
14743 14741 return (ENETUNREACH);
14744 14742 }
14745 14743 }
14746 14744
14747 14745 /*
14748 14746 * Return B_TRUE if the buffers differ in length or content.
14749 14747 * This is used for comparing extension header buffers.
14750 14748 * Note that an extension header would be declared different
14751 14749 * even if all that changed was the next header value in that header i.e.
14752 14750 * what really changed is the next extension header.
14753 14751 */
14754 14752 boolean_t
14755 14753 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14756 14754 uint_t blen)
14757 14755 {
14758 14756 if (!b_valid)
14759 14757 blen = 0;
14760 14758
14761 14759 if (alen != blen)
14762 14760 return (B_TRUE);
14763 14761 if (alen == 0)
14764 14762 return (B_FALSE); /* Both zero length */
14765 14763 return (bcmp(abuf, bbuf, alen));
14766 14764 }
14767 14765
14768 14766 /*
14769 14767 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14770 14768 * Return B_FALSE if memory allocation fails - don't change any state!
14771 14769 */
14772 14770 boolean_t
14773 14771 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14774 14772 const void *src, uint_t srclen)
14775 14773 {
14776 14774 void *dst;
14777 14775
14778 14776 if (!src_valid)
14779 14777 srclen = 0;
14780 14778
14781 14779 ASSERT(*dstlenp == 0);
14782 14780 if (src != NULL && srclen != 0) {
14783 14781 dst = mi_alloc(srclen, BPRI_MED);
14784 14782 if (dst == NULL)
14785 14783 return (B_FALSE);
14786 14784 } else {
14787 14785 dst = NULL;
14788 14786 }
14789 14787 if (*dstp != NULL)
14790 14788 mi_free(*dstp);
14791 14789 *dstp = dst;
14792 14790 *dstlenp = dst == NULL ? 0 : srclen;
14793 14791 return (B_TRUE);
14794 14792 }
14795 14793
14796 14794 /*
14797 14795 * Replace what is in *dst, *dstlen with the source.
14798 14796 * Assumes ip_allocbuf has already been called.
14799 14797 */
14800 14798 void
14801 14799 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14802 14800 const void *src, uint_t srclen)
14803 14801 {
14804 14802 if (!src_valid)
14805 14803 srclen = 0;
14806 14804
14807 14805 ASSERT(*dstlenp == srclen);
14808 14806 if (src != NULL && srclen != 0)
14809 14807 bcopy(src, *dstp, srclen);
14810 14808 }
14811 14809
14812 14810 /*
14813 14811 * Free the storage pointed to by the members of an ip_pkt_t.
14814 14812 */
14815 14813 void
14816 14814 ip_pkt_free(ip_pkt_t *ipp)
14817 14815 {
14818 14816 uint_t fields = ipp->ipp_fields;
14819 14817
14820 14818 if (fields & IPPF_HOPOPTS) {
14821 14819 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14822 14820 ipp->ipp_hopopts = NULL;
14823 14821 ipp->ipp_hopoptslen = 0;
14824 14822 }
14825 14823 if (fields & IPPF_RTHDRDSTOPTS) {
14826 14824 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14827 14825 ipp->ipp_rthdrdstopts = NULL;
14828 14826 ipp->ipp_rthdrdstoptslen = 0;
14829 14827 }
14830 14828 if (fields & IPPF_DSTOPTS) {
14831 14829 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14832 14830 ipp->ipp_dstopts = NULL;
14833 14831 ipp->ipp_dstoptslen = 0;
14834 14832 }
14835 14833 if (fields & IPPF_RTHDR) {
14836 14834 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14837 14835 ipp->ipp_rthdr = NULL;
14838 14836 ipp->ipp_rthdrlen = 0;
14839 14837 }
14840 14838 if (fields & IPPF_IPV4_OPTIONS) {
14841 14839 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14842 14840 ipp->ipp_ipv4_options = NULL;
14843 14841 ipp->ipp_ipv4_options_len = 0;
14844 14842 }
14845 14843 if (fields & IPPF_LABEL_V4) {
14846 14844 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14847 14845 ipp->ipp_label_v4 = NULL;
14848 14846 ipp->ipp_label_len_v4 = 0;
14849 14847 }
14850 14848 if (fields & IPPF_LABEL_V6) {
14851 14849 kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14852 14850 ipp->ipp_label_v6 = NULL;
14853 14851 ipp->ipp_label_len_v6 = 0;
14854 14852 }
14855 14853 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14856 14854 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14857 14855 }
14858 14856
14859 14857 /*
14860 14858 * Copy from src to dst and allocate as needed.
14861 14859 * Returns zero or ENOMEM.
14862 14860 *
14863 14861 * The caller must initialize dst to zero.
14864 14862 */
14865 14863 int
14866 14864 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14867 14865 {
14868 14866 uint_t fields = src->ipp_fields;
14869 14867
14870 14868 /* Start with fields that don't require memory allocation */
14871 14869 dst->ipp_fields = fields &
14872 14870 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14873 14871 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14874 14872
14875 14873 dst->ipp_addr = src->ipp_addr;
14876 14874 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14877 14875 dst->ipp_hoplimit = src->ipp_hoplimit;
14878 14876 dst->ipp_tclass = src->ipp_tclass;
14879 14877 dst->ipp_type_of_service = src->ipp_type_of_service;
14880 14878
14881 14879 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14882 14880 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14883 14881 return (0);
14884 14882
14885 14883 if (fields & IPPF_HOPOPTS) {
14886 14884 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14887 14885 if (dst->ipp_hopopts == NULL) {
14888 14886 ip_pkt_free(dst);
14889 14887 return (ENOMEM);
14890 14888 }
14891 14889 dst->ipp_fields |= IPPF_HOPOPTS;
14892 14890 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14893 14891 src->ipp_hopoptslen);
14894 14892 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14895 14893 }
14896 14894 if (fields & IPPF_RTHDRDSTOPTS) {
14897 14895 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14898 14896 kmflag);
14899 14897 if (dst->ipp_rthdrdstopts == NULL) {
14900 14898 ip_pkt_free(dst);
14901 14899 return (ENOMEM);
14902 14900 }
14903 14901 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14904 14902 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14905 14903 src->ipp_rthdrdstoptslen);
14906 14904 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14907 14905 }
14908 14906 if (fields & IPPF_DSTOPTS) {
14909 14907 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14910 14908 if (dst->ipp_dstopts == NULL) {
14911 14909 ip_pkt_free(dst);
14912 14910 return (ENOMEM);
14913 14911 }
14914 14912 dst->ipp_fields |= IPPF_DSTOPTS;
14915 14913 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14916 14914 src->ipp_dstoptslen);
14917 14915 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14918 14916 }
14919 14917 if (fields & IPPF_RTHDR) {
14920 14918 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14921 14919 if (dst->ipp_rthdr == NULL) {
14922 14920 ip_pkt_free(dst);
14923 14921 return (ENOMEM);
14924 14922 }
14925 14923 dst->ipp_fields |= IPPF_RTHDR;
14926 14924 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14927 14925 src->ipp_rthdrlen);
14928 14926 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14929 14927 }
14930 14928 if (fields & IPPF_IPV4_OPTIONS) {
14931 14929 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14932 14930 kmflag);
14933 14931 if (dst->ipp_ipv4_options == NULL) {
14934 14932 ip_pkt_free(dst);
14935 14933 return (ENOMEM);
14936 14934 }
14937 14935 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14938 14936 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14939 14937 src->ipp_ipv4_options_len);
14940 14938 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14941 14939 }
14942 14940 if (fields & IPPF_LABEL_V4) {
14943 14941 dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14944 14942 if (dst->ipp_label_v4 == NULL) {
14945 14943 ip_pkt_free(dst);
14946 14944 return (ENOMEM);
14947 14945 }
14948 14946 dst->ipp_fields |= IPPF_LABEL_V4;
14949 14947 bcopy(src->ipp_label_v4, dst->ipp_label_v4,
14950 14948 src->ipp_label_len_v4);
14951 14949 dst->ipp_label_len_v4 = src->ipp_label_len_v4;
14952 14950 }
14953 14951 if (fields & IPPF_LABEL_V6) {
14954 14952 dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
14955 14953 if (dst->ipp_label_v6 == NULL) {
14956 14954 ip_pkt_free(dst);
14957 14955 return (ENOMEM);
14958 14956 }
14959 14957 dst->ipp_fields |= IPPF_LABEL_V6;
14960 14958 bcopy(src->ipp_label_v6, dst->ipp_label_v6,
14961 14959 src->ipp_label_len_v6);
14962 14960 dst->ipp_label_len_v6 = src->ipp_label_len_v6;
14963 14961 }
14964 14962 if (fields & IPPF_FRAGHDR) {
14965 14963 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14966 14964 if (dst->ipp_fraghdr == NULL) {
14967 14965 ip_pkt_free(dst);
14968 14966 return (ENOMEM);
14969 14967 }
14970 14968 dst->ipp_fields |= IPPF_FRAGHDR;
14971 14969 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14972 14970 src->ipp_fraghdrlen);
14973 14971 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14974 14972 }
14975 14973 return (0);
14976 14974 }
14977 14975
14978 14976 /*
14979 14977 * Returns INADDR_ANY if no source route
14980 14978 */
14981 14979 ipaddr_t
14982 14980 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14983 14981 {
14984 14982 ipaddr_t nexthop = INADDR_ANY;
14985 14983 ipoptp_t opts;
14986 14984 uchar_t *opt;
14987 14985 uint8_t optval;
14988 14986 uint8_t optlen;
14989 14987 uint32_t totallen;
14990 14988
14991 14989 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14992 14990 return (INADDR_ANY);
14993 14991
14994 14992 totallen = ipp->ipp_ipv4_options_len;
14995 14993 if (totallen & 0x3)
14996 14994 return (INADDR_ANY);
14997 14995
14998 14996 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14999 14997 optval != IPOPT_EOL;
15000 14998 optval = ipoptp_next(&opts)) {
15001 14999 opt = opts.ipoptp_cur;
15002 15000 switch (optval) {
15003 15001 uint8_t off;
15004 15002 case IPOPT_SSRR:
15005 15003 case IPOPT_LSRR:
15006 15004 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15007 15005 break;
15008 15006 }
15009 15007 optlen = opts.ipoptp_len;
15010 15008 off = opt[IPOPT_OFFSET];
15011 15009 off--;
15012 15010 if (optlen < IP_ADDR_LEN ||
15013 15011 off > optlen - IP_ADDR_LEN) {
15014 15012 /* End of source route */
15015 15013 break;
15016 15014 }
15017 15015 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
15018 15016 if (nexthop == htonl(INADDR_LOOPBACK)) {
15019 15017 /* Ignore */
15020 15018 nexthop = INADDR_ANY;
15021 15019 break;
15022 15020 }
15023 15021 break;
15024 15022 }
15025 15023 }
15026 15024 return (nexthop);
15027 15025 }
15028 15026
15029 15027 /*
15030 15028 * Reverse a source route.
15031 15029 */
15032 15030 void
15033 15031 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
15034 15032 {
15035 15033 ipaddr_t tmp;
15036 15034 ipoptp_t opts;
15037 15035 uchar_t *opt;
15038 15036 uint8_t optval;
15039 15037 uint32_t totallen;
15040 15038
15041 15039 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15042 15040 return;
15043 15041
15044 15042 totallen = ipp->ipp_ipv4_options_len;
15045 15043 if (totallen & 0x3)
15046 15044 return;
15047 15045
15048 15046 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15049 15047 optval != IPOPT_EOL;
15050 15048 optval = ipoptp_next(&opts)) {
15051 15049 uint8_t off1, off2;
15052 15050
15053 15051 opt = opts.ipoptp_cur;
15054 15052 switch (optval) {
15055 15053 case IPOPT_SSRR:
15056 15054 case IPOPT_LSRR:
15057 15055 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15058 15056 break;
15059 15057 }
15060 15058 off1 = IPOPT_MINOFF_SR - 1;
15061 15059 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15062 15060 while (off2 > off1) {
15063 15061 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15064 15062 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15065 15063 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15066 15064 off2 -= IP_ADDR_LEN;
15067 15065 off1 += IP_ADDR_LEN;
15068 15066 }
15069 15067 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15070 15068 break;
15071 15069 }
15072 15070 }
15073 15071 }
15074 15072
15075 15073 /*
15076 15074 * Returns NULL if no routing header
15077 15075 */
15078 15076 in6_addr_t *
15079 15077 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15080 15078 {
15081 15079 in6_addr_t *nexthop = NULL;
15082 15080 ip6_rthdr0_t *rthdr;
15083 15081
15084 15082 if (!(ipp->ipp_fields & IPPF_RTHDR))
15085 15083 return (NULL);
15086 15084
15087 15085 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15088 15086 if (rthdr->ip6r0_segleft == 0)
15089 15087 return (NULL);
15090 15088
15091 15089 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15092 15090 return (nexthop);
15093 15091 }
15094 15092
15095 15093 zoneid_t
15096 15094 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15097 15095 zoneid_t lookup_zoneid)
15098 15096 {
15099 15097 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15100 15098 ire_t *ire;
15101 15099 int ire_flags = MATCH_IRE_TYPE;
15102 15100 zoneid_t zoneid = ALL_ZONES;
15103 15101
15104 15102 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15105 15103 return (ALL_ZONES);
15106 15104
15107 15105 if (lookup_zoneid != ALL_ZONES)
15108 15106 ire_flags |= MATCH_IRE_ZONEONLY;
15109 15107 ire = ire_ftable_lookup_v4(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15110 15108 NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15111 15109 if (ire != NULL) {
15112 15110 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15113 15111 ire_refrele(ire);
15114 15112 }
15115 15113 return (zoneid);
15116 15114 }
15117 15115
15118 15116 zoneid_t
15119 15117 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15120 15118 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15121 15119 {
15122 15120 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15123 15121 ire_t *ire;
15124 15122 int ire_flags = MATCH_IRE_TYPE;
15125 15123 zoneid_t zoneid = ALL_ZONES;
15126 15124
15127 15125 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15128 15126 return (ALL_ZONES);
15129 15127
15130 15128 if (IN6_IS_ADDR_LINKLOCAL(addr))
15131 15129 ire_flags |= MATCH_IRE_ILL;
15132 15130
15133 15131 if (lookup_zoneid != ALL_ZONES)
15134 15132 ire_flags |= MATCH_IRE_ZONEONLY;
15135 15133 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15136 15134 ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15137 15135 if (ire != NULL) {
15138 15136 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15139 15137 ire_refrele(ire);
15140 15138 }
15141 15139 return (zoneid);
15142 15140 }
15143 15141
15144 15142 /*
15145 15143 * IP obserability hook support functions.
15146 15144 */
15147 15145 static void
15148 15146 ipobs_init(ip_stack_t *ipst)
15149 15147 {
15150 15148 netid_t id;
15151 15149
15152 15150 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15153 15151
15154 15152 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15155 15153 VERIFY(ipst->ips_ip4_observe_pr != NULL);
15156 15154
15157 15155 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15158 15156 VERIFY(ipst->ips_ip6_observe_pr != NULL);
15159 15157 }
15160 15158
15161 15159 static void
15162 15160 ipobs_fini(ip_stack_t *ipst)
15163 15161 {
15164 15162
15165 15163 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15166 15164 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15167 15165 }
15168 15166
15169 15167 /*
15170 15168 * hook_pkt_observe_t is composed in network byte order so that the
15171 15169 * entire mblk_t chain handed into hook_run can be used as-is.
15172 15170 * The caveat is that use of the fields, such as the zone fields,
15173 15171 * requires conversion into host byte order first.
15174 15172 */
15175 15173 void
15176 15174 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15177 15175 const ill_t *ill, ip_stack_t *ipst)
15178 15176 {
15179 15177 hook_pkt_observe_t *hdr;
15180 15178 uint64_t grifindex;
15181 15179 mblk_t *imp;
15182 15180
15183 15181 imp = allocb(sizeof (*hdr), BPRI_HI);
15184 15182 if (imp == NULL)
15185 15183 return;
15186 15184
15187 15185 hdr = (hook_pkt_observe_t *)imp->b_rptr;
15188 15186 /*
15189 15187 * b_wptr is set to make the apparent size of the data in the mblk_t
15190 15188 * to exclude the pointers at the end of hook_pkt_observer_t.
15191 15189 */
15192 15190 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15193 15191 imp->b_cont = mp;
15194 15192
15195 15193 ASSERT(DB_TYPE(mp) == M_DATA);
15196 15194
15197 15195 if (IS_UNDER_IPMP(ill))
15198 15196 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15199 15197 else
15200 15198 grifindex = 0;
15201 15199
15202 15200 hdr->hpo_version = 1;
15203 15201 hdr->hpo_htype = htons(htype);
15204 15202 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15205 15203 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15206 15204 hdr->hpo_grifindex = htonl(grifindex);
15207 15205 hdr->hpo_zsrc = htonl(zsrc);
15208 15206 hdr->hpo_zdst = htonl(zdst);
15209 15207 hdr->hpo_pkt = imp;
15210 15208 hdr->hpo_ctx = ipst->ips_netstack;
15211 15209
15212 15210 if (ill->ill_isv6) {
15213 15211 hdr->hpo_family = AF_INET6;
15214 15212 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15215 15213 ipst->ips_ipv6observing, (hook_data_t)hdr);
15216 15214 } else {
15217 15215 hdr->hpo_family = AF_INET;
15218 15216 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15219 15217 ipst->ips_ipv4observing, (hook_data_t)hdr);
15220 15218 }
15221 15219
15222 15220 imp->b_cont = NULL;
15223 15221 freemsg(imp);
15224 15222 }
15225 15223
15226 15224 /*
15227 15225 * Utility routine that checks if `v4srcp' is a valid address on underlying
15228 15226 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
15229 15227 * associated with `v4srcp' on success. NOTE: if this is not called from
15230 15228 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15231 15229 * group during or after this lookup.
15232 15230 */
15233 15231 boolean_t
15234 15232 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15235 15233 {
15236 15234 ipif_t *ipif;
15237 15235
15238 15236 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15239 15237 if (ipif != NULL) {
15240 15238 if (ipifp != NULL)
15241 15239 *ipifp = ipif;
15242 15240 else
15243 15241 ipif_refrele(ipif);
15244 15242 return (B_TRUE);
15245 15243 }
15246 15244
15247 15245 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15248 15246 *v4srcp));
15249 15247 return (B_FALSE);
15250 15248 }
15251 15249
15252 15250 /*
15253 15251 * Transport protocol call back function for CPU state change.
15254 15252 */
15255 15253 /* ARGSUSED */
15256 15254 static int
15257 15255 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15258 15256 {
15259 15257 processorid_t cpu_seqid;
15260 15258 netstack_handle_t nh;
15261 15259 netstack_t *ns;
15262 15260
15263 15261 ASSERT(MUTEX_HELD(&cpu_lock));
15264 15262
15265 15263 switch (what) {
15266 15264 case CPU_CONFIG:
15267 15265 case CPU_ON:
15268 15266 case CPU_INIT:
15269 15267 case CPU_CPUPART_IN:
15270 15268 cpu_seqid = cpu[id]->cpu_seqid;
15271 15269 netstack_next_init(&nh);
15272 15270 while ((ns = netstack_next(&nh)) != NULL) {
15273 15271 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15274 15272 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15275 15273 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15276 15274 netstack_rele(ns);
15277 15275 }
15278 15276 netstack_next_fini(&nh);
15279 15277 break;
15280 15278 case CPU_UNCONFIG:
15281 15279 case CPU_OFF:
15282 15280 case CPU_CPUPART_OUT:
15283 15281 /*
15284 15282 * Nothing to do. We don't remove the per CPU stats from
15285 15283 * the IP stack even when the CPU goes offline.
15286 15284 */
15287 15285 break;
15288 15286 default:
15289 15287 break;
15290 15288 }
15291 15289 return (0);
15292 15290 }
|
↓ open down ↓ |
11406 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX