Print this page
11553 Want pluggable TCP congestion control algorithms
Portions contributed by: Cody Peter Mello <cody.mello@joyent.com>
Reviewed by: Dan McDonald <danmcd@joyent.com>
Reviewed by: Robert Mustacchi <robert.mustacchi@joyent.com>
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
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) 2017 OmniTI Computer Consulting, Inc. All rights reserved.
26 26 * Copyright (c) 2016 by Delphix. All rights reserved.
27 27 * Copyright (c) 2019 Joyent, Inc. All rights reserved.
28 28 */
29 29
30 30 #include <sys/types.h>
31 31 #include <sys/stream.h>
32 32 #include <sys/dlpi.h>
33 33 #include <sys/stropts.h>
34 34 #include <sys/sysmacros.h>
35 35 #include <sys/strsubr.h>
36 36 #include <sys/strlog.h>
37 37 #include <sys/strsun.h>
38 38 #include <sys/zone.h>
39 39 #define _SUN_TPI_VERSION 2
40 40 #include <sys/tihdr.h>
41 41 #include <sys/xti_inet.h>
42 42 #include <sys/ddi.h>
43 43 #include <sys/suntpi.h>
44 44 #include <sys/cmn_err.h>
45 45 #include <sys/debug.h>
46 46 #include <sys/kobj.h>
47 47 #include <sys/modctl.h>
48 48 #include <sys/atomic.h>
49 49 #include <sys/policy.h>
50 50 #include <sys/priv.h>
51 51 #include <sys/taskq.h>
52 52
53 53 #include <sys/systm.h>
54 54 #include <sys/param.h>
55 55 #include <sys/kmem.h>
56 56 #include <sys/sdt.h>
57 57 #include <sys/socket.h>
58 58 #include <sys/vtrace.h>
59 59 #include <sys/isa_defs.h>
60 60 #include <sys/mac.h>
61 61 #include <net/if.h>
62 62 #include <net/if_arp.h>
63 63 #include <net/route.h>
64 64 #include <sys/sockio.h>
65 65 #include <netinet/in.h>
66 66 #include <net/if_dl.h>
67 67
68 68 #include <inet/common.h>
69 69 #include <inet/mi.h>
70 70 #include <inet/mib2.h>
71 71 #include <inet/nd.h>
72 72 #include <inet/arp.h>
73 73 #include <inet/snmpcom.h>
74 74 #include <inet/optcom.h>
75 75 #include <inet/kstatcom.h>
76 76
77 77 #include <netinet/igmp_var.h>
78 78 #include <netinet/ip6.h>
79 79 #include <netinet/icmp6.h>
80 80 #include <netinet/sctp.h>
81 81
82 82 #include <inet/ip.h>
83 83 #include <inet/ip_impl.h>
84 84 #include <inet/ip6.h>
85 85 #include <inet/ip6_asp.h>
86 86 #include <inet/tcp.h>
87 87 #include <inet/tcp_impl.h>
↓ open down ↓ |
87 lines elided |
↑ open up ↑ |
88 88 #include <inet/ip_multi.h>
89 89 #include <inet/ip_if.h>
90 90 #include <inet/ip_ire.h>
91 91 #include <inet/ip_ftable.h>
92 92 #include <inet/ip_rts.h>
93 93 #include <inet/ip_ndp.h>
94 94 #include <inet/ip_listutils.h>
95 95 #include <netinet/igmp.h>
96 96 #include <netinet/ip_mroute.h>
97 97 #include <inet/ipp_common.h>
98 +#include <inet/cc.h>
98 99
99 100 #include <net/pfkeyv2.h>
100 101 #include <inet/sadb.h>
101 102 #include <inet/ipsec_impl.h>
102 103 #include <inet/iptun/iptun_impl.h>
103 104 #include <inet/ipdrop.h>
104 105 #include <inet/ip_netinfo.h>
105 106 #include <inet/ilb_ip.h>
106 107
107 108 #include <sys/ethernet.h>
108 109 #include <net/if_types.h>
109 110 #include <sys/cpuvar.h>
110 111
111 112 #include <ipp/ipp.h>
112 113 #include <ipp/ipp_impl.h>
113 114 #include <ipp/ipgpc/ipgpc.h>
114 115
115 116 #include <sys/pattr.h>
116 117 #include <inet/ipclassifier.h>
117 118 #include <inet/sctp_ip.h>
118 119 #include <inet/sctp/sctp_impl.h>
119 120 #include <inet/udp_impl.h>
120 121 #include <inet/rawip_impl.h>
121 122 #include <inet/rts_impl.h>
122 123
123 124 #include <sys/tsol/label.h>
124 125 #include <sys/tsol/tnet.h>
125 126
126 127 #include <sys/squeue_impl.h>
127 128 #include <inet/ip_arp.h>
128 129
129 130 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */
130 131
131 132 /*
132 133 * Values for squeue switch:
133 134 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
134 135 * IP_SQUEUE_ENTER: SQ_PROCESS
135 136 * IP_SQUEUE_FILL: SQ_FILL
136 137 */
137 138 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */
138 139
139 140 int ip_squeue_flag;
140 141
141 142 /*
142 143 * Setable in /etc/system
143 144 */
144 145 int ip_poll_normal_ms = 100;
145 146 int ip_poll_normal_ticks = 0;
146 147 int ip_modclose_ackwait_ms = 3000;
147 148
148 149 /*
149 150 * It would be nice to have these present only in DEBUG systems, but the
150 151 * current design of the global symbol checking logic requires them to be
151 152 * unconditionally present.
152 153 */
153 154 uint_t ip_thread_data; /* TSD key for debug support */
154 155 krwlock_t ip_thread_rwlock;
155 156 list_t ip_thread_list;
156 157
157 158 /*
158 159 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
159 160 */
160 161
161 162 struct listptr_s {
162 163 mblk_t *lp_head; /* pointer to the head of the list */
163 164 mblk_t *lp_tail; /* pointer to the tail of the list */
164 165 };
165 166
166 167 typedef struct listptr_s listptr_t;
167 168
168 169 /*
169 170 * This is used by ip_snmp_get_mib2_ip_route_media and
170 171 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
171 172 */
172 173 typedef struct iproutedata_s {
173 174 uint_t ird_idx;
174 175 uint_t ird_flags; /* see below */
175 176 listptr_t ird_route; /* ipRouteEntryTable */
176 177 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
177 178 listptr_t ird_attrs; /* ipRouteAttributeTable */
178 179 } iproutedata_t;
179 180
180 181 /* Include ire_testhidden and IRE_IF_CLONE routes */
181 182 #define IRD_REPORT_ALL 0x01
182 183
183 184 /*
184 185 * Cluster specific hooks. These should be NULL when booted as a non-cluster
185 186 */
186 187
187 188 /*
188 189 * Hook functions to enable cluster networking
189 190 * On non-clustered systems these vectors must always be NULL.
190 191 *
191 192 * Hook function to Check ip specified ip address is a shared ip address
192 193 * in the cluster
193 194 *
194 195 */
195 196 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
196 197 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
197 198
198 199 /*
199 200 * Hook function to generate cluster wide ip fragment identifier
200 201 */
201 202 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
202 203 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
203 204 void *args) = NULL;
204 205
205 206 /*
206 207 * Hook function to generate cluster wide SPI.
207 208 */
208 209 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
209 210 void *) = NULL;
210 211
211 212 /*
212 213 * Hook function to verify if the SPI is already utlized.
213 214 */
214 215
215 216 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
216 217
217 218 /*
218 219 * Hook function to delete the SPI from the cluster wide repository.
219 220 */
220 221
221 222 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
222 223
223 224 /*
224 225 * Hook function to inform the cluster when packet received on an IDLE SA
225 226 */
226 227
227 228 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
228 229 in6_addr_t, in6_addr_t, void *) = NULL;
229 230
230 231 /*
231 232 * Synchronization notes:
232 233 *
233 234 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
234 235 * MT level protection given by STREAMS. IP uses a combination of its own
235 236 * internal serialization mechanism and standard Solaris locking techniques.
236 237 * The internal serialization is per phyint. This is used to serialize
237 238 * plumbing operations, IPMP operations, most set ioctls, etc.
238 239 *
239 240 * Plumbing is a long sequence of operations involving message
240 241 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
241 242 * involved in plumbing operations. A natural model is to serialize these
242 243 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
243 244 * parallel without any interference. But various set ioctls on hme0 are best
244 245 * serialized, along with IPMP operations and processing of DLPI control
245 246 * messages received from drivers on a per phyint basis. This serialization is
246 247 * provided by the ipsq_t and primitives operating on this. Details can
247 248 * be found in ip_if.c above the core primitives operating on ipsq_t.
248 249 *
249 250 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
250 251 * Simiarly lookup of an ire by a thread also returns a refheld ire.
251 252 * In addition ipif's and ill's referenced by the ire are also indirectly
252 253 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
253 254 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
254 255 * address of an ipif has to go through the ipsq_t. This ensures that only
255 256 * one such exclusive operation proceeds at any time on the ipif. It then
256 257 * waits for all refcnts
257 258 * associated with this ipif to come down to zero. The address is changed
258 259 * only after the ipif has been quiesced. Then the ipif is brought up again.
259 260 * More details are described above the comment in ip_sioctl_flags.
260 261 *
261 262 * Packet processing is based mostly on IREs and are fully multi-threaded
262 263 * using standard Solaris MT techniques.
263 264 *
264 265 * There are explicit locks in IP to handle:
265 266 * - The ip_g_head list maintained by mi_open_link() and friends.
266 267 *
267 268 * - The reassembly data structures (one lock per hash bucket)
268 269 *
269 270 * - conn_lock is meant to protect conn_t fields. The fields actually
270 271 * protected by conn_lock are documented in the conn_t definition.
271 272 *
272 273 * - ire_lock to protect some of the fields of the ire, IRE tables
273 274 * (one lock per hash bucket). Refer to ip_ire.c for details.
274 275 *
275 276 * - ndp_g_lock and ncec_lock for protecting NCEs.
276 277 *
277 278 * - ill_lock protects fields of the ill and ipif. Details in ip.h
278 279 *
279 280 * - ill_g_lock: This is a global reader/writer lock. Protects the following
280 281 * * The AVL tree based global multi list of all ills.
281 282 * * The linked list of all ipifs of an ill
282 283 * * The <ipsq-xop> mapping
283 284 * * <ill-phyint> association
284 285 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
285 286 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the
286 287 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
287 288 * writer for the actual duration of the insertion/deletion/change.
288 289 *
289 290 * - ill_lock: This is a per ill mutex.
290 291 * It protects some members of the ill_t struct; see ip.h for details.
291 292 * It also protects the <ill-phyint> assoc.
292 293 * It also protects the list of ipifs hanging off the ill.
293 294 *
294 295 * - ipsq_lock: This is a per ipsq_t mutex lock.
295 296 * This protects some members of the ipsq_t struct; see ip.h for details.
296 297 * It also protects the <ipsq-ipxop> mapping
297 298 *
298 299 * - ipx_lock: This is a per ipxop_t mutex lock.
299 300 * This protects some members of the ipxop_t struct; see ip.h for details.
300 301 *
301 302 * - phyint_lock: This is a per phyint mutex lock. Protects just the
302 303 * phyint_flags
303 304 *
304 305 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
305 306 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
306 307 * uniqueness check also done atomically.
307 308 *
308 309 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
309 310 * group list linked by ill_usesrc_grp_next. It also protects the
310 311 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
311 312 * group is being added or deleted. This lock is taken as a reader when
312 313 * walking the list/group(eg: to get the number of members in a usesrc group).
313 314 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
314 315 * field is changing state i.e from NULL to non-NULL or vice-versa. For
315 316 * example, it is not necessary to take this lock in the initial portion
316 317 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
317 318 * operations are executed exclusively and that ensures that the "usesrc
318 319 * group state" cannot change. The "usesrc group state" change can happen
319 320 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
320 321 *
321 322 * Changing <ill-phyint>, <ipsq-xop> assocications:
322 323 *
323 324 * To change the <ill-phyint> association, the ill_g_lock must be held
324 325 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
325 326 * must be held.
326 327 *
327 328 * To change the <ipsq-xop> association, the ill_g_lock must be held as
328 329 * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
329 330 * This is only done when ills are added or removed from IPMP groups.
330 331 *
331 332 * To add or delete an ipif from the list of ipifs hanging off the ill,
332 333 * ill_g_lock (writer) and ill_lock must be held and the thread must be
333 334 * a writer on the associated ipsq.
334 335 *
335 336 * To add or delete an ill to the system, the ill_g_lock must be held as
336 337 * writer and the thread must be a writer on the associated ipsq.
337 338 *
338 339 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
339 340 * must be a writer on the associated ipsq.
340 341 *
341 342 * Lock hierarchy
342 343 *
343 344 * Some lock hierarchy scenarios are listed below.
344 345 *
345 346 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
346 347 * ill_g_lock -> ill_lock(s) -> phyint_lock
347 348 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
348 349 * ill_g_lock -> ip_addr_avail_lock
349 350 * conn_lock -> irb_lock -> ill_lock -> ire_lock
350 351 * ill_g_lock -> ip_g_nd_lock
351 352 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
352 353 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
353 354 * arl_lock -> ill_lock
354 355 * ips_ire_dep_lock -> irb_lock
355 356 *
356 357 * When more than 1 ill lock is needed to be held, all ill lock addresses
357 358 * are sorted on address and locked starting from highest addressed lock
358 359 * downward.
359 360 *
360 361 * Multicast scenarios
361 362 * ips_ill_g_lock -> ill_mcast_lock
362 363 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
363 364 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
364 365 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
365 366 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
366 367 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
367 368 *
368 369 * IPsec scenarios
369 370 *
370 371 * ipsa_lock -> ill_g_lock -> ill_lock
371 372 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
372 373 *
373 374 * Trusted Solaris scenarios
374 375 *
375 376 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
376 377 * igsa_lock -> gcdb_lock
377 378 * gcgrp_rwlock -> ire_lock
378 379 * gcgrp_rwlock -> gcdb_lock
379 380 *
380 381 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
381 382 *
382 383 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
383 384 * sq_lock -> conn_lock -> QLOCK(q)
384 385 * ill_lock -> ft_lock -> fe_lock
385 386 *
386 387 * Routing/forwarding table locking notes:
387 388 *
388 389 * Lock acquisition order: Radix tree lock, irb_lock.
389 390 * Requirements:
390 391 * i. Walker must not hold any locks during the walker callback.
391 392 * ii Walker must not see a truncated tree during the walk because of any node
392 393 * deletion.
393 394 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
394 395 * in many places in the code to walk the irb list. Thus even if all the
395 396 * ires in a bucket have been deleted, we still can't free the radix node
396 397 * until the ires have actually been inactive'd (freed).
397 398 *
398 399 * Tree traversal - Need to hold the global tree lock in read mode.
399 400 * Before dropping the global tree lock, need to either increment the ire_refcnt
400 401 * to ensure that the radix node can't be deleted.
401 402 *
402 403 * Tree add - Need to hold the global tree lock in write mode to add a
403 404 * radix node. To prevent the node from being deleted, increment the
404 405 * irb_refcnt, after the node is added to the tree. The ire itself is
405 406 * added later while holding the irb_lock, but not the tree lock.
406 407 *
407 408 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
408 409 * All associated ires must be inactive (i.e. freed), and irb_refcnt
409 410 * must be zero.
410 411 *
411 412 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
412 413 * global tree lock (read mode) for traversal.
413 414 *
414 415 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
415 416 * hence we will acquire irb_lock while holding ips_ire_dep_lock.
416 417 *
417 418 * IPsec notes :
418 419 *
419 420 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
420 421 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
421 422 * ip_xmit_attr_t has the
422 423 * information used by the IPsec code for applying the right level of
423 424 * protection. The information initialized by IP in the ip_xmit_attr_t
424 425 * is determined by the per-socket policy or global policy in the system.
425 426 * For inbound datagrams, the ip_recv_attr_t
426 427 * starts out with nothing in it. It gets filled
427 428 * with the right information if it goes through the AH/ESP code, which
428 429 * happens if the incoming packet is secure. The information initialized
429 430 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
430 431 * the policy requirements needed by per-socket policy or global policy
431 432 * is met or not.
432 433 *
433 434 * For fully connected sockets i.e dst, src [addr, port] is known,
434 435 * conn_policy_cached is set indicating that policy has been cached.
435 436 * conn_in_enforce_policy may or may not be set depending on whether
436 437 * there is a global policy match or per-socket policy match.
437 438 * Policy inheriting happpens in ip_policy_set once the destination is known.
438 439 * Once the right policy is set on the conn_t, policy cannot change for
439 440 * this socket. This makes life simpler for TCP (UDP ?) where
440 441 * re-transmissions go out with the same policy. For symmetry, policy
441 442 * is cached for fully connected UDP sockets also. Thus if policy is cached,
442 443 * it also implies that policy is latched i.e policy cannot change
443 444 * on these sockets. As we have the right policy on the conn, we don't
444 445 * have to lookup global policy for every outbound and inbound datagram
445 446 * and thus serving as an optimization. Note that a global policy change
446 447 * does not affect fully connected sockets if they have policy. If fully
447 448 * connected sockets did not have any policy associated with it, global
448 449 * policy change may affect them.
449 450 *
450 451 * IP Flow control notes:
451 452 * ---------------------
452 453 * Non-TCP streams are flow controlled by IP. The way this is accomplished
453 454 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
454 455 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
455 456 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
456 457 * functions.
457 458 *
458 459 * Per Tx ring udp flow control:
459 460 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
460 461 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
461 462 *
462 463 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
463 464 * To achieve best performance, outgoing traffic need to be fanned out among
464 465 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
465 466 * traffic out of the NIC and it takes a fanout hint. UDP connections pass
466 467 * the address of connp as fanout hint to mac_tx(). Under flow controlled
467 468 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
468 469 * cookie points to a specific Tx ring that is blocked. The cookie is used to
469 470 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
470 471 * point to drain_lists (idl_t's). These drain list will store the blocked UDP
471 472 * connp's. The drain list is not a single list but a configurable number of
472 473 * lists.
473 474 *
474 475 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
475 476 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
476 477 * which is equal to 128. This array in turn contains a pointer to idl_t[],
477 478 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
478 479 * list will point to the list of connp's that are flow controlled.
479 480 *
480 481 * --------------- ------- ------- -------
481 482 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
482 483 * | --------------- ------- ------- -------
483 484 * | --------------- ------- ------- -------
484 485 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
485 486 * ---------------- | --------------- ------- ------- -------
486 487 * |idl_tx_list[0]|->| --------------- ------- ------- -------
487 488 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
488 489 * | --------------- ------- ------- -------
489 490 * . . . . .
490 491 * | --------------- ------- ------- -------
491 492 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
492 493 * --------------- ------- ------- -------
493 494 * --------------- ------- ------- -------
494 495 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
495 496 * | --------------- ------- ------- -------
496 497 * | --------------- ------- ------- -------
497 498 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
498 499 * |idl_tx_list[1]|->| --------------- ------- ------- -------
499 500 * ---------------- | . . . .
500 501 * | --------------- ------- ------- -------
501 502 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
502 503 * --------------- ------- ------- -------
503 504 * .....
504 505 * ----------------
505 506 * |idl_tx_list[n]|-> ...
506 507 * ----------------
507 508 *
508 509 * When mac_tx() returns a cookie, the cookie is hashed into an index into
509 510 * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
510 511 * to insert the conn onto. conn_drain_insert() asserts flow control for the
511 512 * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
512 513 * Further, conn_blocked is set to indicate that the conn is blocked.
513 514 *
514 515 * GLDv3 calls ill_flow_enable() when flow control is relieved. The cookie
515 516 * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
516 517 * is again hashed to locate the appropriate idl_tx_list, which is then
517 518 * drained via conn_walk_drain(). conn_walk_drain() goes through each conn in
518 519 * the drain list and calls conn_drain_remove() to clear flow control (via
519 520 * calling su_txq_full() or clearing QFULL), and remove the conn from the
520 521 * drain list.
521 522 *
522 523 * Note that the drain list is not a single list but a (configurable) array of
523 524 * lists (8 elements by default). Synchronization between drain insertion and
524 525 * flow control wakeup is handled by using idl_txl->txl_lock, and only
525 526 * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
526 527 *
527 528 * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
528 529 * On the send side, if the packet cannot be sent down to the driver by IP
529 530 * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
530 531 * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
531 532 * the 0'th drain list. When ip_wsrv() runs on the ill_wq because flow
532 533 * control has been relieved, the blocked conns in the 0'th drain list are
533 534 * drained as in the non-STREAMS case.
534 535 *
535 536 * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
536 537 * is done when the conn is inserted into the drain list (conn_drain_insert())
537 538 * and cleared when the conn is removed from the it (conn_drain_remove()).
538 539 *
539 540 * IPQOS notes:
540 541 *
541 542 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
542 543 * and IPQoS modules. IPPF includes hooks in IP at different control points
543 544 * (callout positions) which direct packets to IPQoS modules for policy
544 545 * processing. Policies, if present, are global.
545 546 *
546 547 * The callout positions are located in the following paths:
547 548 * o local_in (packets destined for this host)
548 549 * o local_out (packets orginating from this host )
549 550 * o fwd_in (packets forwarded by this m/c - inbound)
550 551 * o fwd_out (packets forwarded by this m/c - outbound)
551 552 * Hooks at these callout points can be enabled/disabled using the ndd variable
552 553 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
553 554 * By default all the callout positions are enabled.
554 555 *
555 556 * Outbound (local_out)
556 557 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
557 558 *
558 559 * Inbound (local_in)
559 560 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
560 561 *
561 562 * Forwarding (in and out)
562 563 * Hooks are placed in ire_recv_forward_v4/v6.
563 564 *
564 565 * IP Policy Framework processing (IPPF processing)
565 566 * Policy processing for a packet is initiated by ip_process, which ascertains
566 567 * that the classifier (ipgpc) is loaded and configured, failing which the
567 568 * packet resumes normal processing in IP. If the clasifier is present, the
568 569 * packet is acted upon by one or more IPQoS modules (action instances), per
569 570 * filters configured in ipgpc and resumes normal IP processing thereafter.
570 571 * An action instance can drop a packet in course of its processing.
571 572 *
572 573 * Zones notes:
573 574 *
574 575 * The partitioning rules for networking are as follows:
575 576 * 1) Packets coming from a zone must have a source address belonging to that
576 577 * zone.
577 578 * 2) Packets coming from a zone can only be sent on a physical interface on
578 579 * which the zone has an IP address.
579 580 * 3) Between two zones on the same machine, packet delivery is only allowed if
580 581 * there's a matching route for the destination and zone in the forwarding
581 582 * table.
582 583 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
583 584 * different zones can bind to the same port with the wildcard address
584 585 * (INADDR_ANY).
585 586 *
586 587 * The granularity of interface partitioning is at the logical interface level.
587 588 * Therefore, every zone has its own IP addresses, and incoming packets can be
588 589 * attributed to a zone unambiguously. A logical interface is placed into a zone
589 590 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
590 591 * structure. Rule (1) is implemented by modifying the source address selection
591 592 * algorithm so that the list of eligible addresses is filtered based on the
592 593 * sending process zone.
593 594 *
594 595 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
595 596 * across all zones, depending on their type. Here is the break-up:
596 597 *
597 598 * IRE type Shared/exclusive
598 599 * -------- ----------------
599 600 * IRE_BROADCAST Exclusive
600 601 * IRE_DEFAULT (default routes) Shared (*)
601 602 * IRE_LOCAL Exclusive (x)
602 603 * IRE_LOOPBACK Exclusive
603 604 * IRE_PREFIX (net routes) Shared (*)
604 605 * IRE_IF_NORESOLVER (interface routes) Exclusive
605 606 * IRE_IF_RESOLVER (interface routes) Exclusive
606 607 * IRE_IF_CLONE (interface routes) Exclusive
607 608 * IRE_HOST (host routes) Shared (*)
608 609 *
609 610 * (*) A zone can only use a default or off-subnet route if the gateway is
610 611 * directly reachable from the zone, that is, if the gateway's address matches
611 612 * one of the zone's logical interfaces.
612 613 *
613 614 * (x) IRE_LOCAL are handled a bit differently.
614 615 * When ip_restrict_interzone_loopback is set (the default),
615 616 * ire_route_recursive restricts loopback using an IRE_LOCAL
616 617 * between zone to the case when L2 would have conceptually looped the packet
617 618 * back, i.e. the loopback which is required since neither Ethernet drivers
618 619 * nor Ethernet hardware loops them back. This is the case when the normal
619 620 * routes (ignoring IREs with different zoneids) would send out the packet on
620 621 * the same ill as the ill with which is IRE_LOCAL is associated.
621 622 *
622 623 * Multiple zones can share a common broadcast address; typically all zones
623 624 * share the 255.255.255.255 address. Incoming as well as locally originated
624 625 * broadcast packets must be dispatched to all the zones on the broadcast
625 626 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
626 627 * since some zones may not be on the 10.16.72/24 network. To handle this, each
627 628 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
628 629 * sent to every zone that has an IRE_BROADCAST entry for the destination
629 630 * address on the input ill, see ip_input_broadcast().
630 631 *
631 632 * Applications in different zones can join the same multicast group address.
632 633 * The same logic applies for multicast as for broadcast. ip_input_multicast
633 634 * dispatches packets to all zones that have members on the physical interface.
634 635 */
635 636
636 637 /*
637 638 * Squeue Fanout flags:
638 639 * 0: No fanout.
639 640 * 1: Fanout across all squeues
640 641 */
641 642 boolean_t ip_squeue_fanout = 0;
642 643
643 644 /*
644 645 * Maximum dups allowed per packet.
645 646 */
646 647 uint_t ip_max_frag_dups = 10;
647 648
648 649 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
649 650 cred_t *credp, boolean_t isv6);
650 651 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
651 652
652 653 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
653 654 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
654 655 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
655 656 ip_recv_attr_t *);
656 657 static void icmp_options_update(ipha_t *);
657 658 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *);
658 659 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
659 660 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
660 661 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
661 662 ip_recv_attr_t *);
662 663 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
663 664 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
664 665 ip_recv_attr_t *);
665 666
666 667 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
667 668 char *ip_dot_addr(ipaddr_t, char *);
668 669 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
669 670 static char *ip_dot_saddr(uchar_t *, char *);
670 671 static int ip_lrput(queue_t *, mblk_t *);
671 672 ipaddr_t ip_net_mask(ipaddr_t);
672 673 char *ip_nv_lookup(nv_t *, int);
673 674 int ip_rput(queue_t *, mblk_t *);
674 675 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
675 676 void *dummy_arg);
676 677 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
677 678 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
678 679 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
679 680 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
680 681 ip_stack_t *, boolean_t);
681 682 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
682 683 boolean_t);
683 684 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
684 685 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
685 686 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
686 687 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
687 688 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
688 689 ip_stack_t *ipst, boolean_t);
689 690 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
690 691 ip_stack_t *ipst, boolean_t);
691 692 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
692 693 ip_stack_t *ipst);
693 694 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
694 695 ip_stack_t *ipst);
695 696 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
696 697 ip_stack_t *ipst);
697 698 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
698 699 ip_stack_t *ipst);
699 700 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
700 701 ip_stack_t *ipst);
701 702 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
702 703 ip_stack_t *ipst);
703 704 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
704 705 ip_stack_t *ipst);
705 706 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
706 707 ip_stack_t *ipst);
707 708 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
708 709 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
709 710 static void ip_snmp_get2_v4_media(ncec_t *, void *);
710 711 static void ip_snmp_get2_v6_media(ncec_t *, void *);
711 712 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
712 713
713 714 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
714 715 mblk_t *);
715 716
716 717 static void conn_drain_init(ip_stack_t *);
717 718 static void conn_drain_fini(ip_stack_t *);
718 719 static void conn_drain(conn_t *connp, boolean_t closing);
719 720
720 721 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
721 722 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
722 723
723 724 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
724 725 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
725 726 static void ip_stack_fini(netstackid_t stackid, void *arg);
726 727
727 728 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
728 729 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
729 730 ire_t *, conn_t *, boolean_t, const in6_addr_t *, mcast_record_t,
730 731 const in6_addr_t *);
731 732
732 733 static int ip_squeue_switch(int);
733 734
734 735 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
735 736 static void ip_kstat_fini(netstackid_t, kstat_t *);
736 737 static int ip_kstat_update(kstat_t *kp, int rw);
737 738 static void *icmp_kstat_init(netstackid_t);
738 739 static void icmp_kstat_fini(netstackid_t, kstat_t *);
739 740 static int icmp_kstat_update(kstat_t *kp, int rw);
740 741 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
741 742 static void ip_kstat2_fini(netstackid_t, kstat_t *);
742 743
743 744 static void ipobs_init(ip_stack_t *);
744 745 static void ipobs_fini(ip_stack_t *);
745 746
746 747 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
747 748
748 749 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
749 750
750 751 static long ip_rput_pullups;
751 752 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
752 753
753 754 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
754 755 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
755 756
756 757 int ip_debug;
757 758
758 759 /*
759 760 * Multirouting/CGTP stuff
760 761 */
761 762 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
762 763
763 764 /*
764 765 * IP tunables related declarations. Definitions are in ip_tunables.c
765 766 */
766 767 extern mod_prop_info_t ip_propinfo_tbl[];
767 768 extern int ip_propinfo_count;
768 769
769 770 /*
770 771 * Table of IP ioctls encoding the various properties of the ioctl and
771 772 * indexed based on the last byte of the ioctl command. Occasionally there
772 773 * is a clash, and there is more than 1 ioctl with the same last byte.
773 774 * In such a case 1 ioctl is encoded in the ndx table and the remaining
774 775 * ioctls are encoded in the misc table. An entry in the ndx table is
775 776 * retrieved by indexing on the last byte of the ioctl command and comparing
776 777 * the ioctl command with the value in the ndx table. In the event of a
777 778 * mismatch the misc table is then searched sequentially for the desired
778 779 * ioctl command.
779 780 *
780 781 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
781 782 */
782 783 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
783 784 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
784 785 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
785 786 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
786 787 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 788 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 789 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 790 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 791 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 792 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 793 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
793 794
794 795 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
795 796 MISC_CMD, ip_siocaddrt, NULL },
796 797 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
797 798 MISC_CMD, ip_siocdelrt, NULL },
798 799
799 800 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
800 801 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
801 802 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
802 803 IF_CMD, ip_sioctl_get_addr, NULL },
803 804
804 805 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
805 806 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
806 807 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
807 808 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
808 809
809 810 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
810 811 IPI_PRIV | IPI_WR,
811 812 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
812 813 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
813 814 IPI_MODOK | IPI_GET_CMD,
814 815 IF_CMD, ip_sioctl_get_flags, NULL },
815 816
816 817 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
817 818 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
818 819
819 820 /* copyin size cannot be coded for SIOCGIFCONF */
820 821 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
821 822 MISC_CMD, ip_sioctl_get_ifconf, NULL },
822 823
823 824 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
824 825 IF_CMD, ip_sioctl_mtu, NULL },
825 826 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
826 827 IF_CMD, ip_sioctl_get_mtu, NULL },
827 828 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
828 829 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
829 830 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
830 831 IF_CMD, ip_sioctl_brdaddr, NULL },
831 832 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
832 833 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
833 834 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
834 835 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
835 836 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
836 837 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
837 838 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
838 839 IF_CMD, ip_sioctl_metric, NULL },
839 840 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
840 841
841 842 /* See 166-168 below for extended SIOC*XARP ioctls */
842 843 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
843 844 ARP_CMD, ip_sioctl_arp, NULL },
844 845 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
845 846 ARP_CMD, ip_sioctl_arp, NULL },
846 847 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
847 848 ARP_CMD, ip_sioctl_arp, NULL },
848 849
849 850 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
850 851 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
851 852 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
852 853 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
853 854 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
854 855 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
855 856 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
856 857 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
857 858 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
858 859 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
859 860 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
860 861 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
861 862 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 863 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 864 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 865 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 866 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 867 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 868 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 869 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 870 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
870 871
871 872 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
872 873 MISC_CMD, if_unitsel, if_unitsel_restart },
873 874
874 875 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
875 876 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
876 877 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
877 878 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
878 879 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
879 880 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
880 881 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
881 882 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
882 883 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
883 884 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
884 885 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
885 886 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
886 887 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
887 888 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
888 889 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
889 890 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
890 891 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
891 892 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
892 893
893 894 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
894 895 IPI_PRIV | IPI_WR | IPI_MODOK,
895 896 IF_CMD, ip_sioctl_sifname, NULL },
896 897
897 898 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
898 899 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
899 900 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
900 901 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
901 902 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
902 903 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
903 904 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
904 905 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
905 906 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
906 907 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
907 908 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
908 909 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
909 910 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
910 911
911 912 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
912 913 MISC_CMD, ip_sioctl_get_ifnum, NULL },
913 914 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
914 915 IF_CMD, ip_sioctl_get_muxid, NULL },
915 916 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
916 917 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
917 918
918 919 /* Both if and lif variants share same func */
919 920 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
920 921 IF_CMD, ip_sioctl_get_lifindex, NULL },
921 922 /* Both if and lif variants share same func */
922 923 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
923 924 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
924 925
925 926 /* copyin size cannot be coded for SIOCGIFCONF */
926 927 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
927 928 MISC_CMD, ip_sioctl_get_ifconf, NULL },
928 929 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
929 930 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
930 931 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
931 932 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
932 933 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
933 934 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
934 935 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
935 936 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
936 937 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
937 938 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
938 939 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
939 940 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
940 941 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
941 942 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
942 943 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
943 944 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 945 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
945 946
946 947 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
947 948 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
948 949 ip_sioctl_removeif_restart },
949 950 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
950 951 IPI_GET_CMD | IPI_PRIV | IPI_WR,
951 952 LIF_CMD, ip_sioctl_addif, NULL },
952 953 #define SIOCLIFADDR_NDX 112
953 954 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
954 955 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
955 956 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
956 957 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
957 958 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
958 959 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
959 960 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
960 961 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
961 962 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
962 963 IPI_PRIV | IPI_WR,
963 964 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
964 965 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
965 966 IPI_GET_CMD | IPI_MODOK,
966 967 LIF_CMD, ip_sioctl_get_flags, NULL },
967 968
968 969 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
969 970 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
970 971
971 972 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
972 973 ip_sioctl_get_lifconf, NULL },
973 974 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
974 975 LIF_CMD, ip_sioctl_mtu, NULL },
975 976 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
976 977 LIF_CMD, ip_sioctl_get_mtu, NULL },
977 978 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
978 979 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
979 980 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
980 981 LIF_CMD, ip_sioctl_brdaddr, NULL },
981 982 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
982 983 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
983 984 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
984 985 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
985 986 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
986 987 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
987 988 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
988 989 LIF_CMD, ip_sioctl_metric, NULL },
989 990 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
990 991 IPI_PRIV | IPI_WR | IPI_MODOK,
991 992 LIF_CMD, ip_sioctl_slifname,
992 993 ip_sioctl_slifname_restart },
993 994
994 995 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
995 996 MISC_CMD, ip_sioctl_get_lifnum, NULL },
996 997 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
997 998 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
998 999 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
999 1000 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
1000 1001 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1001 1002 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1002 1003 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1003 1004 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1004 1005 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1005 1006 LIF_CMD, ip_sioctl_token, NULL },
1006 1007 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1007 1008 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1008 1009 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1009 1010 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1010 1011 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1011 1012 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1012 1013 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1013 1014 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1014 1015
1015 1016 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1016 1017 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1017 1018 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1018 1019 LIF_CMD, ip_siocdelndp_v6, NULL },
1019 1020 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1020 1021 LIF_CMD, ip_siocqueryndp_v6, NULL },
1021 1022 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1022 1023 LIF_CMD, ip_siocsetndp_v6, NULL },
1023 1024 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1024 1025 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1025 1026 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1026 1027 MISC_CMD, ip_sioctl_tonlink, NULL },
1027 1028 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1028 1029 MISC_CMD, ip_sioctl_tmysite, NULL },
1029 1030 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1030 1031 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1031 1032
1032 1033 /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
1033 1034 /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034 1035 /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1035 1036 /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 1037 /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 1038
1038 1039 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1039 1040
1040 1041 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1041 1042 LIF_CMD, ip_sioctl_get_binding, NULL },
1042 1043 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1043 1044 IPI_PRIV | IPI_WR,
1044 1045 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1045 1046 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1046 1047 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1047 1048 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1048 1049 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1049 1050
1050 1051 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1051 1052 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1052 1053 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 1054 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 1055
1055 1056 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1056 1057
1057 1058 /* These are handled in ip_sioctl_copyin_setup itself */
1058 1059 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1059 1060 MISC_CMD, NULL, NULL },
1060 1061 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1061 1062 MISC_CMD, NULL, NULL },
1062 1063 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1063 1064
1064 1065 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1065 1066 ip_sioctl_get_lifconf, NULL },
1066 1067
1067 1068 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1068 1069 XARP_CMD, ip_sioctl_arp, NULL },
1069 1070 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1070 1071 XARP_CMD, ip_sioctl_arp, NULL },
1071 1072 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1072 1073 XARP_CMD, ip_sioctl_arp, NULL },
1073 1074
1074 1075 /* SIOCPOPSOCKFS is not handled by IP */
1075 1076 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1076 1077
1077 1078 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1078 1079 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1079 1080 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1080 1081 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1081 1082 ip_sioctl_slifzone_restart },
1082 1083 /* 172-174 are SCTP ioctls and not handled by IP */
1083 1084 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1084 1085 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1085 1086 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1086 1087 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1087 1088 IPI_GET_CMD, LIF_CMD,
1088 1089 ip_sioctl_get_lifusesrc, 0 },
1089 1090 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1090 1091 IPI_PRIV | IPI_WR,
1091 1092 LIF_CMD, ip_sioctl_slifusesrc,
1092 1093 NULL },
1093 1094 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1094 1095 ip_sioctl_get_lifsrcof, NULL },
1095 1096 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1096 1097 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1097 1098 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1098 1099 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1099 1100 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1100 1101 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1101 1102 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1102 1103 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1103 1104 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1104 1105 /* SIOCSENABLESDP is handled by SDP */
1105 1106 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1106 1107 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1107 1108 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1108 1109 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1109 1110 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1110 1111 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1111 1112 ip_sioctl_ilb_cmd, NULL },
1112 1113 /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1113 1114 /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1114 1115 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1115 1116 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1116 1117 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1117 1118 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1118 1119 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1119 1120 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1120 1121 };
1121 1122
1122 1123 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1123 1124
1124 1125 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1125 1126 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1126 1127 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1127 1128 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1128 1129 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1129 1130 { ND_GET, 0, 0, 0, NULL, NULL },
1130 1131 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1131 1132 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1132 1133 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1133 1134 MISC_CMD, mrt_ioctl},
1134 1135 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1135 1136 MISC_CMD, mrt_ioctl},
1136 1137 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1137 1138 MISC_CMD, mrt_ioctl}
1138 1139 };
1139 1140
1140 1141 int ip_misc_ioctl_count =
1141 1142 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1142 1143
1143 1144 int conn_drain_nthreads; /* Number of drainers reqd. */
1144 1145 /* Settable in /etc/system */
1145 1146 /* Defined in ip_ire.c */
1146 1147 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1147 1148 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1148 1149 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1149 1150
1150 1151 static nv_t ire_nv_arr[] = {
1151 1152 { IRE_BROADCAST, "BROADCAST" },
1152 1153 { IRE_LOCAL, "LOCAL" },
1153 1154 { IRE_LOOPBACK, "LOOPBACK" },
1154 1155 { IRE_DEFAULT, "DEFAULT" },
1155 1156 { IRE_PREFIX, "PREFIX" },
1156 1157 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1157 1158 { IRE_IF_RESOLVER, "IF_RESOLV" },
1158 1159 { IRE_IF_CLONE, "IF_CLONE" },
1159 1160 { IRE_HOST, "HOST" },
1160 1161 { IRE_MULTICAST, "MULTICAST" },
1161 1162 { IRE_NOROUTE, "NOROUTE" },
1162 1163 { 0 }
1163 1164 };
1164 1165
1165 1166 nv_t *ire_nv_tbl = ire_nv_arr;
1166 1167
1167 1168 /* Simple ICMP IP Header Template */
1168 1169 static ipha_t icmp_ipha = {
1169 1170 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1170 1171 };
1171 1172
1172 1173 struct module_info ip_mod_info = {
1173 1174 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1174 1175 IP_MOD_LOWAT
1175 1176 };
1176 1177
1177 1178 /*
1178 1179 * Duplicate static symbols within a module confuses mdb; so we avoid the
1179 1180 * problem by making the symbols here distinct from those in udp.c.
1180 1181 */
1181 1182
1182 1183 /*
1183 1184 * Entry points for IP as a device and as a module.
1184 1185 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1185 1186 */
1186 1187 static struct qinit iprinitv4 = {
1187 1188 ip_rput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1188 1189 };
1189 1190
1190 1191 struct qinit iprinitv6 = {
1191 1192 ip_rput_v6, NULL, ip_openv6, ip_close, NULL, &ip_mod_info
1192 1193 };
1193 1194
1194 1195 static struct qinit ipwinit = {
1195 1196 ip_wput_nondata, ip_wsrv, NULL, NULL, NULL, &ip_mod_info
1196 1197 };
1197 1198
1198 1199 static struct qinit iplrinit = {
1199 1200 ip_lrput, NULL, ip_openv4, ip_close, NULL, &ip_mod_info
1200 1201 };
1201 1202
1202 1203 static struct qinit iplwinit = {
1203 1204 ip_lwput, NULL, NULL, NULL, NULL, &ip_mod_info
1204 1205 };
1205 1206
1206 1207 /* For AF_INET aka /dev/ip */
1207 1208 struct streamtab ipinfov4 = {
1208 1209 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1209 1210 };
1210 1211
1211 1212 /* For AF_INET6 aka /dev/ip6 */
1212 1213 struct streamtab ipinfov6 = {
1213 1214 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1214 1215 };
1215 1216
1216 1217 #ifdef DEBUG
1217 1218 boolean_t skip_sctp_cksum = B_FALSE;
1218 1219 #endif
1219 1220
1220 1221 /*
1221 1222 * Generate an ICMP fragmentation needed message.
1222 1223 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1223 1224 * constructed by the caller.
1224 1225 */
1225 1226 void
1226 1227 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1227 1228 {
1228 1229 icmph_t icmph;
1229 1230 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1230 1231
1231 1232 mp = icmp_pkt_err_ok(mp, ira);
1232 1233 if (mp == NULL)
1233 1234 return;
1234 1235
1235 1236 bzero(&icmph, sizeof (icmph_t));
1236 1237 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1237 1238 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1238 1239 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1239 1240 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1240 1241 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1241 1242
1242 1243 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1243 1244 }
1244 1245
1245 1246 /*
1246 1247 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1247 1248 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1248 1249 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1249 1250 * Likewise, if the ICMP error is misformed (too short, etc), then it
1250 1251 * returns NULL. The caller uses this to determine whether or not to send
1251 1252 * to raw sockets.
1252 1253 *
1253 1254 * All error messages are passed to the matching transport stream.
1254 1255 *
1255 1256 * The following cases are handled by icmp_inbound:
1256 1257 * 1) It needs to send a reply back and possibly delivering it
1257 1258 * to the "interested" upper clients.
1258 1259 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1259 1260 * 3) It needs to change some values in IP only.
1260 1261 * 4) It needs to change some values in IP and upper layers e.g TCP
1261 1262 * by delivering an error to the upper layers.
1262 1263 *
1263 1264 * We handle the above three cases in the context of IPsec in the
1264 1265 * following way :
1265 1266 *
1266 1267 * 1) Send the reply back in the same way as the request came in.
1267 1268 * If it came in encrypted, it goes out encrypted. If it came in
1268 1269 * clear, it goes out in clear. Thus, this will prevent chosen
1269 1270 * plain text attack.
1270 1271 * 2) The client may or may not expect things to come in secure.
1271 1272 * If it comes in secure, the policy constraints are checked
1272 1273 * before delivering it to the upper layers. If it comes in
1273 1274 * clear, ipsec_inbound_accept_clear will decide whether to
1274 1275 * accept this in clear or not. In both the cases, if the returned
1275 1276 * message (IP header + 8 bytes) that caused the icmp message has
1276 1277 * AH/ESP headers, it is sent up to AH/ESP for validation before
1277 1278 * sending up. If there are only 8 bytes of returned message, then
1278 1279 * upper client will not be notified.
1279 1280 * 3) Check with global policy to see whether it matches the constaints.
1280 1281 * But this will be done only if icmp_accept_messages_in_clear is
1281 1282 * zero.
1282 1283 * 4) If we need to change both in IP and ULP, then the decision taken
1283 1284 * while affecting the values in IP and while delivering up to TCP
1284 1285 * should be the same.
1285 1286 *
1286 1287 * There are two cases.
1287 1288 *
1288 1289 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1289 1290 * failed), we will not deliver it to the ULP, even though they
1290 1291 * are *willing* to accept in *clear*. This is fine as our global
1291 1292 * disposition to icmp messages asks us reject the datagram.
1292 1293 *
1293 1294 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1294 1295 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1295 1296 * to deliver it to ULP (policy failed), it can lead to
1296 1297 * consistency problems. The cases known at this time are
1297 1298 * ICMP_DESTINATION_UNREACHABLE messages with following code
1298 1299 * values :
1299 1300 *
1300 1301 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1301 1302 * and Upper layer rejects. Then the communication will
1302 1303 * come to a stop. This is solved by making similar decisions
1303 1304 * at both levels. Currently, when we are unable to deliver
1304 1305 * to the Upper Layer (due to policy failures) while IP has
1305 1306 * adjusted dce_pmtu, the next outbound datagram would
1306 1307 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1307 1308 * will be with the right level of protection. Thus the right
1308 1309 * value will be communicated even if we are not able to
1309 1310 * communicate when we get from the wire initially. But this
1310 1311 * assumes there would be at least one outbound datagram after
1311 1312 * IP has adjusted its dce_pmtu value. To make things
1312 1313 * simpler, we accept in clear after the validation of
1313 1314 * AH/ESP headers.
1314 1315 *
1315 1316 * - Other ICMP ERRORS : We may not be able to deliver it to the
1316 1317 * upper layer depending on the level of protection the upper
1317 1318 * layer expects and the disposition in ipsec_inbound_accept_clear().
1318 1319 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1319 1320 * should be accepted in clear when the Upper layer expects secure.
1320 1321 * Thus the communication may get aborted by some bad ICMP
1321 1322 * packets.
1322 1323 */
1323 1324 mblk_t *
1324 1325 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1325 1326 {
1326 1327 icmph_t *icmph;
1327 1328 ipha_t *ipha; /* Outer header */
1328 1329 int ip_hdr_length; /* Outer header length */
1329 1330 boolean_t interested;
1330 1331 ipif_t *ipif;
1331 1332 uint32_t ts;
1332 1333 uint32_t *tsp;
1333 1334 timestruc_t now;
1334 1335 ill_t *ill = ira->ira_ill;
1335 1336 ip_stack_t *ipst = ill->ill_ipst;
1336 1337 zoneid_t zoneid = ira->ira_zoneid;
1337 1338 int len_needed;
1338 1339 mblk_t *mp_ret = NULL;
1339 1340
1340 1341 ipha = (ipha_t *)mp->b_rptr;
1341 1342
1342 1343 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1343 1344
1344 1345 ip_hdr_length = ira->ira_ip_hdr_length;
1345 1346 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1346 1347 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1347 1348 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1348 1349 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1349 1350 freemsg(mp);
1350 1351 return (NULL);
1351 1352 }
1352 1353 /* Last chance to get real. */
1353 1354 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1354 1355 if (ipha == NULL) {
1355 1356 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1356 1357 freemsg(mp);
1357 1358 return (NULL);
1358 1359 }
1359 1360 }
1360 1361
1361 1362 /* The IP header will always be a multiple of four bytes */
1362 1363 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1363 1364 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1364 1365 icmph->icmph_code));
1365 1366
1366 1367 /*
1367 1368 * We will set "interested" to "true" if we should pass a copy to
1368 1369 * the transport or if we handle the packet locally.
1369 1370 */
1370 1371 interested = B_FALSE;
1371 1372 switch (icmph->icmph_type) {
1372 1373 case ICMP_ECHO_REPLY:
1373 1374 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1374 1375 break;
1375 1376 case ICMP_DEST_UNREACHABLE:
1376 1377 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1377 1378 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1378 1379 interested = B_TRUE; /* Pass up to transport */
1379 1380 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1380 1381 break;
1381 1382 case ICMP_SOURCE_QUENCH:
1382 1383 interested = B_TRUE; /* Pass up to transport */
1383 1384 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1384 1385 break;
1385 1386 case ICMP_REDIRECT:
1386 1387 if (!ipst->ips_ip_ignore_redirect)
1387 1388 interested = B_TRUE;
1388 1389 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1389 1390 break;
1390 1391 case ICMP_ECHO_REQUEST:
1391 1392 /*
1392 1393 * Whether to respond to echo requests that come in as IP
1393 1394 * broadcasts or as IP multicast is subject to debate
1394 1395 * (what isn't?). We aim to please, you pick it.
1395 1396 * Default is do it.
1396 1397 */
1397 1398 if (ira->ira_flags & IRAF_MULTICAST) {
1398 1399 /* multicast: respond based on tunable */
1399 1400 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1400 1401 } else if (ira->ira_flags & IRAF_BROADCAST) {
1401 1402 /* broadcast: respond based on tunable */
1402 1403 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1403 1404 } else {
1404 1405 /* unicast: always respond */
1405 1406 interested = B_TRUE;
1406 1407 }
1407 1408 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1408 1409 if (!interested) {
1409 1410 /* We never pass these to RAW sockets */
1410 1411 freemsg(mp);
1411 1412 return (NULL);
1412 1413 }
1413 1414
1414 1415 /* Check db_ref to make sure we can modify the packet. */
1415 1416 if (mp->b_datap->db_ref > 1) {
1416 1417 mblk_t *mp1;
1417 1418
1418 1419 mp1 = copymsg(mp);
1419 1420 freemsg(mp);
1420 1421 if (!mp1) {
1421 1422 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1422 1423 return (NULL);
1423 1424 }
1424 1425 mp = mp1;
1425 1426 ipha = (ipha_t *)mp->b_rptr;
1426 1427 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1427 1428 }
1428 1429 icmph->icmph_type = ICMP_ECHO_REPLY;
1429 1430 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1430 1431 icmp_send_reply_v4(mp, ipha, icmph, ira);
1431 1432 return (NULL);
1432 1433
1433 1434 case ICMP_ROUTER_ADVERTISEMENT:
1434 1435 case ICMP_ROUTER_SOLICITATION:
1435 1436 break;
1436 1437 case ICMP_TIME_EXCEEDED:
1437 1438 interested = B_TRUE; /* Pass up to transport */
1438 1439 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1439 1440 break;
1440 1441 case ICMP_PARAM_PROBLEM:
1441 1442 interested = B_TRUE; /* Pass up to transport */
1442 1443 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1443 1444 break;
1444 1445 case ICMP_TIME_STAMP_REQUEST:
1445 1446 /* Response to Time Stamp Requests is local policy. */
1446 1447 if (ipst->ips_ip_g_resp_to_timestamp) {
1447 1448 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1448 1449 interested =
1449 1450 ipst->ips_ip_g_resp_to_timestamp_bcast;
1450 1451 else
1451 1452 interested = B_TRUE;
1452 1453 }
1453 1454 if (!interested) {
1454 1455 /* We never pass these to RAW sockets */
1455 1456 freemsg(mp);
1456 1457 return (NULL);
1457 1458 }
1458 1459
1459 1460 /* Make sure we have enough of the packet */
1460 1461 len_needed = ip_hdr_length + ICMPH_SIZE +
1461 1462 3 * sizeof (uint32_t);
1462 1463
1463 1464 if (mp->b_wptr - mp->b_rptr < len_needed) {
1464 1465 ipha = ip_pullup(mp, len_needed, ira);
1465 1466 if (ipha == NULL) {
1466 1467 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1467 1468 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1468 1469 mp, ill);
1469 1470 freemsg(mp);
1470 1471 return (NULL);
1471 1472 }
1472 1473 /* Refresh following the pullup. */
1473 1474 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1474 1475 }
1475 1476 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1476 1477 /* Check db_ref to make sure we can modify the packet. */
1477 1478 if (mp->b_datap->db_ref > 1) {
1478 1479 mblk_t *mp1;
1479 1480
1480 1481 mp1 = copymsg(mp);
1481 1482 freemsg(mp);
1482 1483 if (!mp1) {
1483 1484 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1484 1485 return (NULL);
1485 1486 }
1486 1487 mp = mp1;
1487 1488 ipha = (ipha_t *)mp->b_rptr;
1488 1489 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1489 1490 }
1490 1491 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1491 1492 tsp = (uint32_t *)&icmph[1];
1492 1493 tsp++; /* Skip past 'originate time' */
1493 1494 /* Compute # of milliseconds since midnight */
1494 1495 gethrestime(&now);
1495 1496 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1496 1497 NSEC2MSEC(now.tv_nsec);
1497 1498 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1498 1499 *tsp++ = htonl(ts); /* Lay in 'send time' */
1499 1500 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1500 1501 icmp_send_reply_v4(mp, ipha, icmph, ira);
1501 1502 return (NULL);
1502 1503
1503 1504 case ICMP_TIME_STAMP_REPLY:
1504 1505 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1505 1506 break;
1506 1507 case ICMP_INFO_REQUEST:
1507 1508 /* Per RFC 1122 3.2.2.7, ignore this. */
1508 1509 case ICMP_INFO_REPLY:
1509 1510 break;
1510 1511 case ICMP_ADDRESS_MASK_REQUEST:
1511 1512 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1512 1513 interested =
1513 1514 ipst->ips_ip_respond_to_address_mask_broadcast;
1514 1515 } else {
1515 1516 interested = B_TRUE;
1516 1517 }
1517 1518 if (!interested) {
1518 1519 /* We never pass these to RAW sockets */
1519 1520 freemsg(mp);
1520 1521 return (NULL);
1521 1522 }
1522 1523 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1523 1524 if (mp->b_wptr - mp->b_rptr < len_needed) {
1524 1525 ipha = ip_pullup(mp, len_needed, ira);
1525 1526 if (ipha == NULL) {
1526 1527 BUMP_MIB(ill->ill_ip_mib,
1527 1528 ipIfStatsInTruncatedPkts);
1528 1529 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1529 1530 ill);
1530 1531 freemsg(mp);
1531 1532 return (NULL);
1532 1533 }
1533 1534 /* Refresh following the pullup. */
1534 1535 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1535 1536 }
1536 1537 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1537 1538 /* Check db_ref to make sure we can modify the packet. */
1538 1539 if (mp->b_datap->db_ref > 1) {
1539 1540 mblk_t *mp1;
1540 1541
1541 1542 mp1 = copymsg(mp);
1542 1543 freemsg(mp);
1543 1544 if (!mp1) {
1544 1545 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1545 1546 return (NULL);
1546 1547 }
1547 1548 mp = mp1;
1548 1549 ipha = (ipha_t *)mp->b_rptr;
1549 1550 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1550 1551 }
1551 1552 /*
1552 1553 * Need the ipif with the mask be the same as the source
1553 1554 * address of the mask reply. For unicast we have a specific
1554 1555 * ipif. For multicast/broadcast we only handle onlink
1555 1556 * senders, and use the source address to pick an ipif.
1556 1557 */
1557 1558 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1558 1559 if (ipif == NULL) {
1559 1560 /* Broadcast or multicast */
1560 1561 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1561 1562 if (ipif == NULL) {
1562 1563 freemsg(mp);
1563 1564 return (NULL);
1564 1565 }
1565 1566 }
1566 1567 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1567 1568 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1568 1569 ipif_refrele(ipif);
1569 1570 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1570 1571 icmp_send_reply_v4(mp, ipha, icmph, ira);
1571 1572 return (NULL);
1572 1573
1573 1574 case ICMP_ADDRESS_MASK_REPLY:
1574 1575 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1575 1576 break;
1576 1577 default:
1577 1578 interested = B_TRUE; /* Pass up to transport */
1578 1579 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1579 1580 break;
1580 1581 }
1581 1582 /*
1582 1583 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1583 1584 * if there isn't one.
1584 1585 */
1585 1586 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1586 1587 /* If there is an ICMP client and we want one too, copy it. */
1587 1588
1588 1589 if (!interested) {
1589 1590 /* Caller will deliver to RAW sockets */
1590 1591 return (mp);
1591 1592 }
1592 1593 mp_ret = copymsg(mp);
1593 1594 if (mp_ret == NULL) {
1594 1595 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1595 1596 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1596 1597 }
1597 1598 } else if (!interested) {
1598 1599 /* Neither we nor raw sockets are interested. Drop packet now */
1599 1600 freemsg(mp);
1600 1601 return (NULL);
1601 1602 }
1602 1603
1603 1604 /*
1604 1605 * ICMP error or redirect packet. Make sure we have enough of
1605 1606 * the header and that db_ref == 1 since we might end up modifying
1606 1607 * the packet.
1607 1608 */
1608 1609 if (mp->b_cont != NULL) {
1609 1610 if (ip_pullup(mp, -1, ira) == NULL) {
1610 1611 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1611 1612 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1612 1613 mp, ill);
1613 1614 freemsg(mp);
1614 1615 return (mp_ret);
1615 1616 }
1616 1617 }
1617 1618
1618 1619 if (mp->b_datap->db_ref > 1) {
1619 1620 mblk_t *mp1;
1620 1621
1621 1622 mp1 = copymsg(mp);
1622 1623 if (mp1 == NULL) {
1623 1624 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1624 1625 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1625 1626 freemsg(mp);
1626 1627 return (mp_ret);
1627 1628 }
1628 1629 freemsg(mp);
1629 1630 mp = mp1;
1630 1631 }
1631 1632
1632 1633 /*
1633 1634 * In case mp has changed, verify the message before any further
1634 1635 * processes.
1635 1636 */
1636 1637 ipha = (ipha_t *)mp->b_rptr;
1637 1638 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1638 1639 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1639 1640 freemsg(mp);
1640 1641 return (mp_ret);
1641 1642 }
1642 1643
1643 1644 switch (icmph->icmph_type) {
1644 1645 case ICMP_REDIRECT:
1645 1646 icmp_redirect_v4(mp, ipha, icmph, ira);
1646 1647 break;
1647 1648 case ICMP_DEST_UNREACHABLE:
1648 1649 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1649 1650 /* Update DCE and adjust MTU is icmp header if needed */
1650 1651 icmp_inbound_too_big_v4(icmph, ira);
1651 1652 }
1652 1653 /* FALLTHROUGH */
1653 1654 default:
1654 1655 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1655 1656 break;
1656 1657 }
1657 1658 return (mp_ret);
1658 1659 }
1659 1660
1660 1661 /*
1661 1662 * Send an ICMP echo, timestamp or address mask reply.
1662 1663 * The caller has already updated the payload part of the packet.
1663 1664 * We handle the ICMP checksum, IP source address selection and feed
1664 1665 * the packet into ip_output_simple.
1665 1666 */
1666 1667 static void
1667 1668 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1668 1669 ip_recv_attr_t *ira)
1669 1670 {
1670 1671 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1671 1672 ill_t *ill = ira->ira_ill;
1672 1673 ip_stack_t *ipst = ill->ill_ipst;
1673 1674 ip_xmit_attr_t ixas;
1674 1675
1675 1676 /* Send out an ICMP packet */
1676 1677 icmph->icmph_checksum = 0;
1677 1678 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1678 1679 /* Reset time to live. */
1679 1680 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1680 1681 {
1681 1682 /* Swap source and destination addresses */
1682 1683 ipaddr_t tmp;
1683 1684
1684 1685 tmp = ipha->ipha_src;
1685 1686 ipha->ipha_src = ipha->ipha_dst;
1686 1687 ipha->ipha_dst = tmp;
1687 1688 }
1688 1689 ipha->ipha_ident = 0;
1689 1690 if (!IS_SIMPLE_IPH(ipha))
1690 1691 icmp_options_update(ipha);
1691 1692
1692 1693 bzero(&ixas, sizeof (ixas));
1693 1694 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1694 1695 ixas.ixa_zoneid = ira->ira_zoneid;
1695 1696 ixas.ixa_cred = kcred;
1696 1697 ixas.ixa_cpid = NOPID;
1697 1698 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
1698 1699 ixas.ixa_ifindex = 0;
1699 1700 ixas.ixa_ipst = ipst;
1700 1701 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1701 1702
1702 1703 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1703 1704 /*
1704 1705 * This packet should go out the same way as it
1705 1706 * came in i.e in clear, independent of the IPsec policy
1706 1707 * for transmitting packets.
1707 1708 */
1708 1709 ixas.ixa_flags |= IXAF_NO_IPSEC;
1709 1710 } else {
1710 1711 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1711 1712 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1712 1713 /* Note: mp already consumed and ip_drop_packet done */
1713 1714 return;
1714 1715 }
1715 1716 }
1716 1717 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1717 1718 /*
1718 1719 * Not one or our addresses (IRE_LOCALs), thus we let
1719 1720 * ip_output_simple pick the source.
1720 1721 */
1721 1722 ipha->ipha_src = INADDR_ANY;
1722 1723 ixas.ixa_flags |= IXAF_SET_SOURCE;
1723 1724 }
1724 1725 /* Should we send with DF and use dce_pmtu? */
1725 1726 if (ipst->ips_ipv4_icmp_return_pmtu) {
1726 1727 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1727 1728 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1728 1729 }
1729 1730
1730 1731 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1731 1732
1732 1733 (void) ip_output_simple(mp, &ixas);
1733 1734 ixa_cleanup(&ixas);
1734 1735 }
1735 1736
1736 1737 /*
1737 1738 * Verify the ICMP messages for either for ICMP error or redirect packet.
1738 1739 * The caller should have fully pulled up the message. If it's a redirect
1739 1740 * packet, only basic checks on IP header will be done; otherwise, verify
1740 1741 * the packet by looking at the included ULP header.
1741 1742 *
1742 1743 * Called before icmp_inbound_error_fanout_v4 is called.
1743 1744 */
1744 1745 static boolean_t
1745 1746 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1746 1747 {
1747 1748 ill_t *ill = ira->ira_ill;
1748 1749 int hdr_length;
1749 1750 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1750 1751 conn_t *connp;
1751 1752 ipha_t *ipha; /* Inner IP header */
1752 1753
1753 1754 ipha = (ipha_t *)&icmph[1];
1754 1755 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1755 1756 goto truncated;
1756 1757
1757 1758 hdr_length = IPH_HDR_LENGTH(ipha);
1758 1759
1759 1760 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1760 1761 goto discard_pkt;
1761 1762
1762 1763 if (hdr_length < sizeof (ipha_t))
1763 1764 goto truncated;
1764 1765
1765 1766 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1766 1767 goto truncated;
1767 1768
1768 1769 /*
1769 1770 * Stop here for ICMP_REDIRECT.
1770 1771 */
1771 1772 if (icmph->icmph_type == ICMP_REDIRECT)
1772 1773 return (B_TRUE);
1773 1774
1774 1775 /*
1775 1776 * ICMP errors only.
1776 1777 */
1777 1778 switch (ipha->ipha_protocol) {
1778 1779 case IPPROTO_UDP:
1779 1780 /*
1780 1781 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1781 1782 * transport header.
1782 1783 */
1783 1784 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1784 1785 mp->b_wptr)
1785 1786 goto truncated;
1786 1787 break;
1787 1788 case IPPROTO_TCP: {
1788 1789 tcpha_t *tcpha;
1789 1790
1790 1791 /*
1791 1792 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1792 1793 * transport header.
1793 1794 */
1794 1795 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1795 1796 mp->b_wptr)
1796 1797 goto truncated;
1797 1798
1798 1799 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1799 1800 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1800 1801 ipst);
1801 1802 if (connp == NULL)
1802 1803 goto discard_pkt;
1803 1804
1804 1805 if ((connp->conn_verifyicmp != NULL) &&
1805 1806 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1806 1807 CONN_DEC_REF(connp);
1807 1808 goto discard_pkt;
1808 1809 }
1809 1810 CONN_DEC_REF(connp);
1810 1811 break;
1811 1812 }
1812 1813 case IPPROTO_SCTP:
1813 1814 /*
1814 1815 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1815 1816 * transport header.
1816 1817 */
1817 1818 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1818 1819 mp->b_wptr)
1819 1820 goto truncated;
1820 1821 break;
1821 1822 case IPPROTO_ESP:
1822 1823 case IPPROTO_AH:
1823 1824 break;
1824 1825 case IPPROTO_ENCAP:
1825 1826 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1826 1827 mp->b_wptr)
1827 1828 goto truncated;
1828 1829 break;
1829 1830 default:
1830 1831 break;
1831 1832 }
1832 1833
1833 1834 return (B_TRUE);
1834 1835
1835 1836 discard_pkt:
1836 1837 /* Bogus ICMP error. */
1837 1838 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1838 1839 return (B_FALSE);
1839 1840
1840 1841 truncated:
1841 1842 /* We pulled up everthing already. Must be truncated */
1842 1843 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1843 1844 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1844 1845 return (B_FALSE);
1845 1846 }
1846 1847
1847 1848 /* Table from RFC 1191 */
1848 1849 static int icmp_frag_size_table[] =
1849 1850 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1850 1851
1851 1852 /*
1852 1853 * Process received ICMP Packet too big.
1853 1854 * Just handles the DCE create/update, including using the above table of
1854 1855 * PMTU guesses. The caller is responsible for validating the packet before
1855 1856 * passing it in and also to fanout the ICMP error to any matching transport
1856 1857 * conns. Assumes the message has been fully pulled up and verified.
1857 1858 *
1858 1859 * Before getting here, the caller has called icmp_inbound_verify_v4()
1859 1860 * that should have verified with ULP to prevent undoing the changes we're
1860 1861 * going to make to DCE. For example, TCP might have verified that the packet
1861 1862 * which generated error is in the send window.
1862 1863 *
1863 1864 * In some cases modified this MTU in the ICMP header packet; the caller
1864 1865 * should pass to the matching ULP after this returns.
1865 1866 */
1866 1867 static void
1867 1868 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1868 1869 {
1869 1870 dce_t *dce;
1870 1871 int old_mtu;
1871 1872 int mtu, orig_mtu;
1872 1873 ipaddr_t dst;
1873 1874 boolean_t disable_pmtud;
1874 1875 ill_t *ill = ira->ira_ill;
1875 1876 ip_stack_t *ipst = ill->ill_ipst;
1876 1877 uint_t hdr_length;
1877 1878 ipha_t *ipha;
1878 1879
1879 1880 /* Caller already pulled up everything. */
1880 1881 ipha = (ipha_t *)&icmph[1];
1881 1882 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1882 1883 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1883 1884 ASSERT(ill != NULL);
1884 1885
1885 1886 hdr_length = IPH_HDR_LENGTH(ipha);
1886 1887
1887 1888 /*
1888 1889 * We handle path MTU for source routed packets since the DCE
1889 1890 * is looked up using the final destination.
1890 1891 */
1891 1892 dst = ip_get_dst(ipha);
1892 1893
1893 1894 dce = dce_lookup_and_add_v4(dst, ipst);
1894 1895 if (dce == NULL) {
1895 1896 /* Couldn't add a unique one - ENOMEM */
1896 1897 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1897 1898 ntohl(dst)));
1898 1899 return;
1899 1900 }
1900 1901
1901 1902 /* Check for MTU discovery advice as described in RFC 1191 */
1902 1903 mtu = ntohs(icmph->icmph_du_mtu);
1903 1904 orig_mtu = mtu;
1904 1905 disable_pmtud = B_FALSE;
1905 1906
1906 1907 mutex_enter(&dce->dce_lock);
1907 1908 if (dce->dce_flags & DCEF_PMTU)
1908 1909 old_mtu = dce->dce_pmtu;
1909 1910 else
1910 1911 old_mtu = ill->ill_mtu;
1911 1912
1912 1913 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1913 1914 uint32_t length;
1914 1915 int i;
1915 1916
1916 1917 /*
1917 1918 * Use the table from RFC 1191 to figure out
1918 1919 * the next "plateau" based on the length in
1919 1920 * the original IP packet.
1920 1921 */
1921 1922 length = ntohs(ipha->ipha_length);
1922 1923 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1923 1924 uint32_t, length);
1924 1925 if (old_mtu <= length &&
1925 1926 old_mtu >= length - hdr_length) {
1926 1927 /*
1927 1928 * Handle broken BSD 4.2 systems that
1928 1929 * return the wrong ipha_length in ICMP
1929 1930 * errors.
1930 1931 */
1931 1932 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1932 1933 length, old_mtu));
1933 1934 length -= hdr_length;
1934 1935 }
1935 1936 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1936 1937 if (length > icmp_frag_size_table[i])
1937 1938 break;
1938 1939 }
1939 1940 if (i == A_CNT(icmp_frag_size_table)) {
1940 1941 /* Smaller than IP_MIN_MTU! */
1941 1942 ip1dbg(("Too big for packet size %d\n",
1942 1943 length));
1943 1944 disable_pmtud = B_TRUE;
1944 1945 mtu = ipst->ips_ip_pmtu_min;
1945 1946 } else {
1946 1947 mtu = icmp_frag_size_table[i];
1947 1948 ip1dbg(("Calculated mtu %d, packet size %d, "
1948 1949 "before %d\n", mtu, length, old_mtu));
1949 1950 if (mtu < ipst->ips_ip_pmtu_min) {
1950 1951 mtu = ipst->ips_ip_pmtu_min;
1951 1952 disable_pmtud = B_TRUE;
1952 1953 }
1953 1954 }
1954 1955 }
1955 1956 if (disable_pmtud)
1956 1957 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1957 1958 else
1958 1959 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1959 1960
1960 1961 dce->dce_pmtu = MIN(old_mtu, mtu);
1961 1962 /* Prepare to send the new max frag size for the ULP. */
1962 1963 icmph->icmph_du_zero = 0;
1963 1964 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1964 1965 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1965 1966 dce, int, orig_mtu, int, mtu);
1966 1967
1967 1968 /* We now have a PMTU for sure */
1968 1969 dce->dce_flags |= DCEF_PMTU;
1969 1970 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1970 1971 mutex_exit(&dce->dce_lock);
1971 1972 /*
1972 1973 * After dropping the lock the new value is visible to everyone.
1973 1974 * Then we bump the generation number so any cached values reinspect
1974 1975 * the dce_t.
1975 1976 */
1976 1977 dce_increment_generation(dce);
1977 1978 dce_refrele(dce);
1978 1979 }
1979 1980
1980 1981 /*
1981 1982 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1982 1983 * calls this function.
1983 1984 */
1984 1985 static mblk_t *
1985 1986 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1986 1987 {
1987 1988 int length;
1988 1989
1989 1990 ASSERT(mp->b_datap->db_type == M_DATA);
1990 1991
1991 1992 /* icmp_inbound_v4 has already pulled up the whole error packet */
1992 1993 ASSERT(mp->b_cont == NULL);
1993 1994
1994 1995 /*
1995 1996 * The length that we want to overlay is the inner header
1996 1997 * and what follows it.
1997 1998 */
1998 1999 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
1999 2000
2000 2001 /*
2001 2002 * Overlay the inner header and whatever follows it over the
2002 2003 * outer header.
2003 2004 */
2004 2005 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2005 2006
2006 2007 /* Adjust for what we removed */
2007 2008 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2008 2009 return (mp);
2009 2010 }
2010 2011
2011 2012 /*
2012 2013 * Try to pass the ICMP message upstream in case the ULP cares.
2013 2014 *
2014 2015 * If the packet that caused the ICMP error is secure, we send
2015 2016 * it to AH/ESP to make sure that the attached packet has a
2016 2017 * valid association. ipha in the code below points to the
2017 2018 * IP header of the packet that caused the error.
2018 2019 *
2019 2020 * For IPsec cases, we let the next-layer-up (which has access to
2020 2021 * cached policy on the conn_t, or can query the SPD directly)
2021 2022 * subtract out any IPsec overhead if they must. We therefore make no
2022 2023 * adjustments here for IPsec overhead.
2023 2024 *
2024 2025 * IFN could have been generated locally or by some router.
2025 2026 *
2026 2027 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2027 2028 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2028 2029 * This happens because IP adjusted its value of MTU on an
2029 2030 * earlier IFN message and could not tell the upper layer,
2030 2031 * the new adjusted value of MTU e.g. Packet was encrypted
2031 2032 * or there was not enough information to fanout to upper
2032 2033 * layers. Thus on the next outbound datagram, ire_send_wire
2033 2034 * generates the IFN, where IPsec processing has *not* been
2034 2035 * done.
2035 2036 *
2036 2037 * Note that we retain ixa_fragsize across IPsec thus once
2037 2038 * we have picking ixa_fragsize and entered ipsec_out_process we do
2038 2039 * no change the fragsize even if the path MTU changes before
2039 2040 * we reach ip_output_post_ipsec.
2040 2041 *
2041 2042 * In the local case, IRAF_LOOPBACK will be set indicating
2042 2043 * that IFN was generated locally.
2043 2044 *
2044 2045 * ROUTER : IFN could be secure or non-secure.
2045 2046 *
2046 2047 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2047 2048 * packet in error has AH/ESP headers to validate the AH/ESP
2048 2049 * headers. AH/ESP will verify whether there is a valid SA or
2049 2050 * not and send it back. We will fanout again if we have more
2050 2051 * data in the packet.
2051 2052 *
2052 2053 * If the packet in error does not have AH/ESP, we handle it
2053 2054 * like any other case.
2054 2055 *
2055 2056 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2056 2057 * up to AH/ESP for validation. AH/ESP will verify whether there is a
2057 2058 * valid SA or not and send it back. We will fanout again if
2058 2059 * we have more data in the packet.
2059 2060 *
2060 2061 * If the packet in error does not have AH/ESP, we handle it
2061 2062 * like any other case.
2062 2063 *
2063 2064 * The caller must have called icmp_inbound_verify_v4.
2064 2065 */
2065 2066 static void
2066 2067 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2067 2068 {
2068 2069 uint16_t *up; /* Pointer to ports in ULP header */
2069 2070 uint32_t ports; /* reversed ports for fanout */
2070 2071 ipha_t ripha; /* With reversed addresses */
2071 2072 ipha_t *ipha; /* Inner IP header */
2072 2073 uint_t hdr_length; /* Inner IP header length */
2073 2074 tcpha_t *tcpha;
2074 2075 conn_t *connp;
2075 2076 ill_t *ill = ira->ira_ill;
2076 2077 ip_stack_t *ipst = ill->ill_ipst;
2077 2078 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2078 2079 ill_t *rill = ira->ira_rill;
2079 2080
2080 2081 /* Caller already pulled up everything. */
2081 2082 ipha = (ipha_t *)&icmph[1];
2082 2083 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2083 2084 ASSERT(mp->b_cont == NULL);
2084 2085
2085 2086 hdr_length = IPH_HDR_LENGTH(ipha);
2086 2087 ira->ira_protocol = ipha->ipha_protocol;
2087 2088
2088 2089 /*
2089 2090 * We need a separate IP header with the source and destination
2090 2091 * addresses reversed to do fanout/classification because the ipha in
2091 2092 * the ICMP error is in the form we sent it out.
2092 2093 */
2093 2094 ripha.ipha_src = ipha->ipha_dst;
2094 2095 ripha.ipha_dst = ipha->ipha_src;
2095 2096 ripha.ipha_protocol = ipha->ipha_protocol;
2096 2097 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2097 2098
2098 2099 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2099 2100 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2100 2101 ntohl(ipha->ipha_dst),
2101 2102 icmph->icmph_type, icmph->icmph_code));
2102 2103
2103 2104 switch (ipha->ipha_protocol) {
2104 2105 case IPPROTO_UDP:
2105 2106 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2106 2107
2107 2108 /* Attempt to find a client stream based on port. */
2108 2109 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2109 2110 ntohs(up[0]), ntohs(up[1])));
2110 2111
2111 2112 /* Note that we send error to all matches. */
2112 2113 ira->ira_flags |= IRAF_ICMP_ERROR;
2113 2114 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2114 2115 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2115 2116 return;
2116 2117
2117 2118 case IPPROTO_TCP:
2118 2119 /*
2119 2120 * Find a TCP client stream for this packet.
2120 2121 * Note that we do a reverse lookup since the header is
2121 2122 * in the form we sent it out.
2122 2123 */
2123 2124 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2124 2125 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2125 2126 ipst);
2126 2127 if (connp == NULL)
2127 2128 goto discard_pkt;
2128 2129
2129 2130 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2130 2131 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2131 2132 mp = ipsec_check_inbound_policy(mp, connp,
2132 2133 ipha, NULL, ira);
2133 2134 if (mp == NULL) {
2134 2135 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2135 2136 /* Note that mp is NULL */
2136 2137 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2137 2138 CONN_DEC_REF(connp);
2138 2139 return;
2139 2140 }
2140 2141 }
2141 2142
2142 2143 ira->ira_flags |= IRAF_ICMP_ERROR;
2143 2144 ira->ira_ill = ira->ira_rill = NULL;
2144 2145 if (IPCL_IS_TCP(connp)) {
2145 2146 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2146 2147 connp->conn_recvicmp, connp, ira, SQ_FILL,
2147 2148 SQTAG_TCP_INPUT_ICMP_ERR);
2148 2149 } else {
2149 2150 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2150 2151 (connp->conn_recv)(connp, mp, NULL, ira);
2151 2152 CONN_DEC_REF(connp);
2152 2153 }
2153 2154 ira->ira_ill = ill;
2154 2155 ira->ira_rill = rill;
2155 2156 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2156 2157 return;
2157 2158
2158 2159 case IPPROTO_SCTP:
2159 2160 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2160 2161 /* Find a SCTP client stream for this packet. */
2161 2162 ((uint16_t *)&ports)[0] = up[1];
2162 2163 ((uint16_t *)&ports)[1] = up[0];
2163 2164
2164 2165 ira->ira_flags |= IRAF_ICMP_ERROR;
2165 2166 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2166 2167 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2167 2168 return;
2168 2169
2169 2170 case IPPROTO_ESP:
2170 2171 case IPPROTO_AH:
2171 2172 if (!ipsec_loaded(ipss)) {
2172 2173 ip_proto_not_sup(mp, ira);
2173 2174 return;
2174 2175 }
2175 2176
2176 2177 if (ipha->ipha_protocol == IPPROTO_ESP)
2177 2178 mp = ipsecesp_icmp_error(mp, ira);
2178 2179 else
2179 2180 mp = ipsecah_icmp_error(mp, ira);
2180 2181 if (mp == NULL)
2181 2182 return;
2182 2183
2183 2184 /* Just in case ipsec didn't preserve the NULL b_cont */
2184 2185 if (mp->b_cont != NULL) {
2185 2186 if (!pullupmsg(mp, -1))
2186 2187 goto discard_pkt;
2187 2188 }
2188 2189
2189 2190 /*
2190 2191 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2191 2192 * correct, but we don't use them any more here.
2192 2193 *
2193 2194 * If succesful, the mp has been modified to not include
2194 2195 * the ESP/AH header so we can fanout to the ULP's icmp
2195 2196 * error handler.
2196 2197 */
2197 2198 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2198 2199 goto truncated;
2199 2200
2200 2201 /* Verify the modified message before any further processes. */
2201 2202 ipha = (ipha_t *)mp->b_rptr;
2202 2203 hdr_length = IPH_HDR_LENGTH(ipha);
2203 2204 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2204 2205 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2205 2206 freemsg(mp);
2206 2207 return;
2207 2208 }
2208 2209
2209 2210 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2210 2211 return;
2211 2212
2212 2213 case IPPROTO_ENCAP: {
2213 2214 /* Look for self-encapsulated packets that caused an error */
2214 2215 ipha_t *in_ipha;
2215 2216
2216 2217 /*
2217 2218 * Caller has verified that length has to be
2218 2219 * at least the size of IP header.
2219 2220 */
2220 2221 ASSERT(hdr_length >= sizeof (ipha_t));
2221 2222 /*
2222 2223 * Check the sanity of the inner IP header like
2223 2224 * we did for the outer header.
2224 2225 */
2225 2226 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2226 2227 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2227 2228 goto discard_pkt;
2228 2229 }
2229 2230 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2230 2231 goto discard_pkt;
2231 2232 }
2232 2233 /* Check for Self-encapsulated tunnels */
2233 2234 if (in_ipha->ipha_src == ipha->ipha_src &&
2234 2235 in_ipha->ipha_dst == ipha->ipha_dst) {
2235 2236
2236 2237 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2237 2238 in_ipha);
2238 2239 if (mp == NULL)
2239 2240 goto discard_pkt;
2240 2241
2241 2242 /*
2242 2243 * Just in case self_encap didn't preserve the NULL
2243 2244 * b_cont
2244 2245 */
2245 2246 if (mp->b_cont != NULL) {
2246 2247 if (!pullupmsg(mp, -1))
2247 2248 goto discard_pkt;
2248 2249 }
2249 2250 /*
2250 2251 * Note that ira_pktlen and ira_ip_hdr_length are no
2251 2252 * longer correct, but we don't use them any more here.
2252 2253 */
2253 2254 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2254 2255 goto truncated;
2255 2256
2256 2257 /*
2257 2258 * Verify the modified message before any further
2258 2259 * processes.
2259 2260 */
2260 2261 ipha = (ipha_t *)mp->b_rptr;
2261 2262 hdr_length = IPH_HDR_LENGTH(ipha);
2262 2263 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2263 2264 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2264 2265 freemsg(mp);
2265 2266 return;
2266 2267 }
2267 2268
2268 2269 /*
2269 2270 * The packet in error is self-encapsualted.
2270 2271 * And we are finding it further encapsulated
2271 2272 * which we could not have possibly generated.
2272 2273 */
2273 2274 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2274 2275 goto discard_pkt;
2275 2276 }
2276 2277 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2277 2278 return;
2278 2279 }
2279 2280 /* No self-encapsulated */
2280 2281 }
2281 2282 /* FALLTHROUGH */
2282 2283 case IPPROTO_IPV6:
2283 2284 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2284 2285 &ripha.ipha_dst, ipst)) != NULL) {
2285 2286 ira->ira_flags |= IRAF_ICMP_ERROR;
2286 2287 connp->conn_recvicmp(connp, mp, NULL, ira);
2287 2288 CONN_DEC_REF(connp);
2288 2289 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2289 2290 return;
2290 2291 }
2291 2292 /*
2292 2293 * No IP tunnel is interested, fallthrough and see
2293 2294 * if a raw socket will want it.
2294 2295 */
2295 2296 /* FALLTHROUGH */
2296 2297 default:
2297 2298 ira->ira_flags |= IRAF_ICMP_ERROR;
2298 2299 ip_fanout_proto_v4(mp, &ripha, ira);
2299 2300 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2300 2301 return;
2301 2302 }
2302 2303 /* NOTREACHED */
2303 2304 discard_pkt:
2304 2305 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2305 2306 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2306 2307 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2307 2308 freemsg(mp);
2308 2309 return;
2309 2310
2310 2311 truncated:
2311 2312 /* We pulled up everthing already. Must be truncated */
2312 2313 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2313 2314 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2314 2315 freemsg(mp);
2315 2316 }
2316 2317
2317 2318 /*
2318 2319 * Common IP options parser.
2319 2320 *
2320 2321 * Setup routine: fill in *optp with options-parsing state, then
2321 2322 * tail-call ipoptp_next to return the first option.
2322 2323 */
2323 2324 uint8_t
2324 2325 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2325 2326 {
2326 2327 uint32_t totallen; /* total length of all options */
2327 2328
2328 2329 totallen = ipha->ipha_version_and_hdr_length -
2329 2330 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2330 2331 totallen <<= 2;
2331 2332 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2332 2333 optp->ipoptp_end = optp->ipoptp_next + totallen;
2333 2334 optp->ipoptp_flags = 0;
2334 2335 return (ipoptp_next(optp));
2335 2336 }
2336 2337
2337 2338 /* Like above but without an ipha_t */
2338 2339 uint8_t
2339 2340 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2340 2341 {
2341 2342 optp->ipoptp_next = opt;
2342 2343 optp->ipoptp_end = optp->ipoptp_next + totallen;
2343 2344 optp->ipoptp_flags = 0;
2344 2345 return (ipoptp_next(optp));
2345 2346 }
2346 2347
2347 2348 /*
2348 2349 * Common IP options parser: extract next option.
2349 2350 */
2350 2351 uint8_t
2351 2352 ipoptp_next(ipoptp_t *optp)
2352 2353 {
2353 2354 uint8_t *end = optp->ipoptp_end;
2354 2355 uint8_t *cur = optp->ipoptp_next;
2355 2356 uint8_t opt, len, pointer;
2356 2357
2357 2358 /*
2358 2359 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2359 2360 * has been corrupted.
2360 2361 */
2361 2362 ASSERT(cur <= end);
2362 2363
2363 2364 if (cur == end)
2364 2365 return (IPOPT_EOL);
2365 2366
2366 2367 opt = cur[IPOPT_OPTVAL];
2367 2368
2368 2369 /*
2369 2370 * Skip any NOP options.
2370 2371 */
2371 2372 while (opt == IPOPT_NOP) {
2372 2373 cur++;
2373 2374 if (cur == end)
2374 2375 return (IPOPT_EOL);
2375 2376 opt = cur[IPOPT_OPTVAL];
2376 2377 }
2377 2378
2378 2379 if (opt == IPOPT_EOL)
2379 2380 return (IPOPT_EOL);
2380 2381
2381 2382 /*
2382 2383 * Option requiring a length.
2383 2384 */
2384 2385 if ((cur + 1) >= end) {
2385 2386 optp->ipoptp_flags |= IPOPTP_ERROR;
2386 2387 return (IPOPT_EOL);
2387 2388 }
2388 2389 len = cur[IPOPT_OLEN];
2389 2390 if (len < 2) {
2390 2391 optp->ipoptp_flags |= IPOPTP_ERROR;
2391 2392 return (IPOPT_EOL);
2392 2393 }
2393 2394 optp->ipoptp_cur = cur;
2394 2395 optp->ipoptp_len = len;
2395 2396 optp->ipoptp_next = cur + len;
2396 2397 if (cur + len > end) {
2397 2398 optp->ipoptp_flags |= IPOPTP_ERROR;
2398 2399 return (IPOPT_EOL);
2399 2400 }
2400 2401
2401 2402 /*
2402 2403 * For the options which require a pointer field, make sure
2403 2404 * its there, and make sure it points to either something
2404 2405 * inside this option, or the end of the option.
2405 2406 */
2406 2407 switch (opt) {
2407 2408 case IPOPT_RR:
2408 2409 case IPOPT_TS:
2409 2410 case IPOPT_LSRR:
2410 2411 case IPOPT_SSRR:
2411 2412 if (len <= IPOPT_OFFSET) {
2412 2413 optp->ipoptp_flags |= IPOPTP_ERROR;
2413 2414 return (opt);
2414 2415 }
2415 2416 pointer = cur[IPOPT_OFFSET];
2416 2417 if (pointer - 1 > len) {
2417 2418 optp->ipoptp_flags |= IPOPTP_ERROR;
2418 2419 return (opt);
2419 2420 }
2420 2421 break;
2421 2422 }
2422 2423
2423 2424 /*
2424 2425 * Sanity check the pointer field based on the type of the
2425 2426 * option.
2426 2427 */
2427 2428 switch (opt) {
2428 2429 case IPOPT_RR:
2429 2430 case IPOPT_SSRR:
2430 2431 case IPOPT_LSRR:
2431 2432 if (pointer < IPOPT_MINOFF_SR)
2432 2433 optp->ipoptp_flags |= IPOPTP_ERROR;
2433 2434 break;
2434 2435 case IPOPT_TS:
2435 2436 if (pointer < IPOPT_MINOFF_IT)
2436 2437 optp->ipoptp_flags |= IPOPTP_ERROR;
2437 2438 /*
2438 2439 * Note that the Internet Timestamp option also
2439 2440 * contains two four bit fields (the Overflow field,
2440 2441 * and the Flag field), which follow the pointer
2441 2442 * field. We don't need to check that these fields
2442 2443 * fall within the length of the option because this
2443 2444 * was implicitely done above. We've checked that the
2444 2445 * pointer value is at least IPOPT_MINOFF_IT, and that
2445 2446 * it falls within the option. Since IPOPT_MINOFF_IT >
2446 2447 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2447 2448 */
2448 2449 ASSERT(len > IPOPT_POS_OV_FLG);
2449 2450 break;
2450 2451 }
2451 2452
2452 2453 return (opt);
2453 2454 }
2454 2455
2455 2456 /*
2456 2457 * Use the outgoing IP header to create an IP_OPTIONS option the way
2457 2458 * it was passed down from the application.
2458 2459 *
2459 2460 * This is compatible with BSD in that it returns
2460 2461 * the reverse source route with the final destination
2461 2462 * as the last entry. The first 4 bytes of the option
2462 2463 * will contain the final destination.
2463 2464 */
2464 2465 int
2465 2466 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2466 2467 {
2467 2468 ipoptp_t opts;
2468 2469 uchar_t *opt;
2469 2470 uint8_t optval;
2470 2471 uint8_t optlen;
2471 2472 uint32_t len = 0;
2472 2473 uchar_t *buf1 = buf;
2473 2474 uint32_t totallen;
2474 2475 ipaddr_t dst;
2475 2476 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2476 2477
2477 2478 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2478 2479 return (0);
2479 2480
2480 2481 totallen = ipp->ipp_ipv4_options_len;
2481 2482 if (totallen & 0x3)
2482 2483 return (0);
2483 2484
2484 2485 buf += IP_ADDR_LEN; /* Leave room for final destination */
2485 2486 len += IP_ADDR_LEN;
2486 2487 bzero(buf1, IP_ADDR_LEN);
2487 2488
2488 2489 dst = connp->conn_faddr_v4;
2489 2490
2490 2491 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2491 2492 optval != IPOPT_EOL;
2492 2493 optval = ipoptp_next(&opts)) {
2493 2494 int off;
2494 2495
2495 2496 opt = opts.ipoptp_cur;
2496 2497 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2497 2498 break;
2498 2499 }
2499 2500 optlen = opts.ipoptp_len;
2500 2501
2501 2502 switch (optval) {
2502 2503 case IPOPT_SSRR:
2503 2504 case IPOPT_LSRR:
2504 2505
2505 2506 /*
2506 2507 * Insert destination as the first entry in the source
2507 2508 * route and move down the entries on step.
2508 2509 * The last entry gets placed at buf1.
2509 2510 */
2510 2511 buf[IPOPT_OPTVAL] = optval;
2511 2512 buf[IPOPT_OLEN] = optlen;
2512 2513 buf[IPOPT_OFFSET] = optlen;
2513 2514
2514 2515 off = optlen - IP_ADDR_LEN;
2515 2516 if (off < 0) {
2516 2517 /* No entries in source route */
2517 2518 break;
2518 2519 }
2519 2520 /* Last entry in source route if not already set */
2520 2521 if (dst == INADDR_ANY)
2521 2522 bcopy(opt + off, buf1, IP_ADDR_LEN);
2522 2523 off -= IP_ADDR_LEN;
2523 2524
2524 2525 while (off > 0) {
2525 2526 bcopy(opt + off,
2526 2527 buf + off + IP_ADDR_LEN,
2527 2528 IP_ADDR_LEN);
2528 2529 off -= IP_ADDR_LEN;
2529 2530 }
2530 2531 /* ipha_dst into first slot */
2531 2532 bcopy(&dst, buf + off + IP_ADDR_LEN,
2532 2533 IP_ADDR_LEN);
2533 2534 buf += optlen;
2534 2535 len += optlen;
2535 2536 break;
2536 2537
2537 2538 default:
2538 2539 bcopy(opt, buf, optlen);
2539 2540 buf += optlen;
2540 2541 len += optlen;
2541 2542 break;
2542 2543 }
2543 2544 }
2544 2545 done:
2545 2546 /* Pad the resulting options */
2546 2547 while (len & 0x3) {
2547 2548 *buf++ = IPOPT_EOL;
2548 2549 len++;
2549 2550 }
2550 2551 return (len);
2551 2552 }
2552 2553
2553 2554 /*
2554 2555 * Update any record route or timestamp options to include this host.
2555 2556 * Reverse any source route option.
2556 2557 * This routine assumes that the options are well formed i.e. that they
2557 2558 * have already been checked.
2558 2559 */
2559 2560 static void
2560 2561 icmp_options_update(ipha_t *ipha)
2561 2562 {
2562 2563 ipoptp_t opts;
2563 2564 uchar_t *opt;
2564 2565 uint8_t optval;
2565 2566 ipaddr_t src; /* Our local address */
2566 2567 ipaddr_t dst;
2567 2568
2568 2569 ip2dbg(("icmp_options_update\n"));
2569 2570 src = ipha->ipha_src;
2570 2571 dst = ipha->ipha_dst;
2571 2572
2572 2573 for (optval = ipoptp_first(&opts, ipha);
2573 2574 optval != IPOPT_EOL;
2574 2575 optval = ipoptp_next(&opts)) {
2575 2576 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2576 2577 opt = opts.ipoptp_cur;
2577 2578 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2578 2579 optval, opts.ipoptp_len));
2579 2580 switch (optval) {
2580 2581 int off1, off2;
2581 2582 case IPOPT_SSRR:
2582 2583 case IPOPT_LSRR:
2583 2584 /*
2584 2585 * Reverse the source route. The first entry
2585 2586 * should be the next to last one in the current
2586 2587 * source route (the last entry is our address).
2587 2588 * The last entry should be the final destination.
2588 2589 */
2589 2590 off1 = IPOPT_MINOFF_SR - 1;
2590 2591 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2591 2592 if (off2 < 0) {
2592 2593 /* No entries in source route */
2593 2594 ip1dbg((
2594 2595 "icmp_options_update: bad src route\n"));
2595 2596 break;
2596 2597 }
2597 2598 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2598 2599 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2599 2600 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2600 2601 off2 -= IP_ADDR_LEN;
2601 2602
2602 2603 while (off1 < off2) {
2603 2604 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2604 2605 bcopy((char *)opt + off2, (char *)opt + off1,
2605 2606 IP_ADDR_LEN);
2606 2607 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2607 2608 off1 += IP_ADDR_LEN;
2608 2609 off2 -= IP_ADDR_LEN;
2609 2610 }
2610 2611 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2611 2612 break;
2612 2613 }
2613 2614 }
2614 2615 }
2615 2616
2616 2617 /*
2617 2618 * Process received ICMP Redirect messages.
2618 2619 * Assumes the caller has verified that the headers are in the pulled up mblk.
2619 2620 * Consumes mp.
2620 2621 */
2621 2622 static void
2622 2623 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2623 2624 {
2624 2625 ire_t *ire, *nire;
2625 2626 ire_t *prev_ire;
2626 2627 ipaddr_t src, dst, gateway;
2627 2628 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2628 2629 ipha_t *inner_ipha; /* Inner IP header */
2629 2630
2630 2631 /* Caller already pulled up everything. */
2631 2632 inner_ipha = (ipha_t *)&icmph[1];
2632 2633 src = ipha->ipha_src;
2633 2634 dst = inner_ipha->ipha_dst;
2634 2635 gateway = icmph->icmph_rd_gateway;
2635 2636 /* Make sure the new gateway is reachable somehow. */
2636 2637 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2637 2638 ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2638 2639 /*
2639 2640 * Make sure we had a route for the dest in question and that
2640 2641 * that route was pointing to the old gateway (the source of the
2641 2642 * redirect packet.)
2642 2643 * We do longest match and then compare ire_gateway_addr below.
2643 2644 */
2644 2645 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2645 2646 NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2646 2647 /*
2647 2648 * Check that
2648 2649 * the redirect was not from ourselves
2649 2650 * the new gateway and the old gateway are directly reachable
2650 2651 */
2651 2652 if (prev_ire == NULL || ire == NULL ||
2652 2653 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2653 2654 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2654 2655 !(ire->ire_type & IRE_IF_ALL) ||
2655 2656 prev_ire->ire_gateway_addr != src) {
2656 2657 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2657 2658 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2658 2659 freemsg(mp);
2659 2660 if (ire != NULL)
2660 2661 ire_refrele(ire);
2661 2662 if (prev_ire != NULL)
2662 2663 ire_refrele(prev_ire);
2663 2664 return;
2664 2665 }
2665 2666
2666 2667 ire_refrele(prev_ire);
2667 2668 ire_refrele(ire);
2668 2669
2669 2670 /*
2670 2671 * TODO: more precise handling for cases 0, 2, 3, the latter two
2671 2672 * require TOS routing
2672 2673 */
2673 2674 switch (icmph->icmph_code) {
2674 2675 case 0:
2675 2676 case 1:
2676 2677 /* TODO: TOS specificity for cases 2 and 3 */
2677 2678 case 2:
2678 2679 case 3:
2679 2680 break;
2680 2681 default:
2681 2682 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2682 2683 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2683 2684 freemsg(mp);
2684 2685 return;
2685 2686 }
2686 2687 /*
2687 2688 * Create a Route Association. This will allow us to remember that
2688 2689 * someone we believe told us to use the particular gateway.
2689 2690 */
2690 2691 ire = ire_create(
2691 2692 (uchar_t *)&dst, /* dest addr */
2692 2693 (uchar_t *)&ip_g_all_ones, /* mask */
2693 2694 (uchar_t *)&gateway, /* gateway addr */
2694 2695 IRE_HOST,
2695 2696 NULL, /* ill */
2696 2697 ALL_ZONES,
2697 2698 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2698 2699 NULL, /* tsol_gc_t */
2699 2700 ipst);
2700 2701
2701 2702 if (ire == NULL) {
2702 2703 freemsg(mp);
2703 2704 return;
2704 2705 }
2705 2706 nire = ire_add(ire);
2706 2707 /* Check if it was a duplicate entry */
2707 2708 if (nire != NULL && nire != ire) {
2708 2709 ASSERT(nire->ire_identical_ref > 1);
2709 2710 ire_delete(nire);
2710 2711 ire_refrele(nire);
2711 2712 nire = NULL;
2712 2713 }
2713 2714 ire = nire;
2714 2715 if (ire != NULL) {
2715 2716 ire_refrele(ire); /* Held in ire_add */
2716 2717
2717 2718 /* tell routing sockets that we received a redirect */
2718 2719 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2719 2720 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2720 2721 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2721 2722 }
2722 2723
2723 2724 /*
2724 2725 * Delete any existing IRE_HOST type redirect ires for this destination.
2725 2726 * This together with the added IRE has the effect of
2726 2727 * modifying an existing redirect.
2727 2728 */
2728 2729 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2729 2730 ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2730 2731 if (prev_ire != NULL) {
2731 2732 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2732 2733 ire_delete(prev_ire);
2733 2734 ire_refrele(prev_ire);
2734 2735 }
2735 2736
2736 2737 freemsg(mp);
2737 2738 }
2738 2739
2739 2740 /*
2740 2741 * Generate an ICMP parameter problem message.
2741 2742 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2742 2743 * constructed by the caller.
2743 2744 */
2744 2745 static void
2745 2746 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2746 2747 {
2747 2748 icmph_t icmph;
2748 2749 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2749 2750
2750 2751 mp = icmp_pkt_err_ok(mp, ira);
2751 2752 if (mp == NULL)
2752 2753 return;
2753 2754
2754 2755 bzero(&icmph, sizeof (icmph_t));
2755 2756 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2756 2757 icmph.icmph_pp_ptr = ptr;
2757 2758 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2758 2759 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2759 2760 }
2760 2761
2761 2762 /*
2762 2763 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2763 2764 * the ICMP header pointed to by "stuff". (May be called as writer.)
2764 2765 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2765 2766 * an icmp error packet can be sent.
2766 2767 * Assigns an appropriate source address to the packet. If ipha_dst is
2767 2768 * one of our addresses use it for source. Otherwise let ip_output_simple
2768 2769 * pick the source address.
2769 2770 */
2770 2771 static void
2771 2772 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2772 2773 {
2773 2774 ipaddr_t dst;
2774 2775 icmph_t *icmph;
2775 2776 ipha_t *ipha;
2776 2777 uint_t len_needed;
2777 2778 size_t msg_len;
2778 2779 mblk_t *mp1;
2779 2780 ipaddr_t src;
2780 2781 ire_t *ire;
2781 2782 ip_xmit_attr_t ixas;
2782 2783 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2783 2784
2784 2785 ipha = (ipha_t *)mp->b_rptr;
2785 2786
2786 2787 bzero(&ixas, sizeof (ixas));
2787 2788 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2788 2789 ixas.ixa_zoneid = ira->ira_zoneid;
2789 2790 ixas.ixa_ifindex = 0;
2790 2791 ixas.ixa_ipst = ipst;
2791 2792 ixas.ixa_cred = kcred;
2792 2793 ixas.ixa_cpid = NOPID;
2793 2794 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2794 2795 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2795 2796
2796 2797 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2797 2798 /*
2798 2799 * Apply IPsec based on how IPsec was applied to
2799 2800 * the packet that had the error.
2800 2801 *
2801 2802 * If it was an outbound packet that caused the ICMP
2802 2803 * error, then the caller will have setup the IRA
2803 2804 * appropriately.
2804 2805 */
2805 2806 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2806 2807 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2807 2808 /* Note: mp already consumed and ip_drop_packet done */
2808 2809 return;
2809 2810 }
2810 2811 } else {
2811 2812 /*
2812 2813 * This is in clear. The icmp message we are building
2813 2814 * here should go out in clear, independent of our policy.
2814 2815 */
2815 2816 ixas.ixa_flags |= IXAF_NO_IPSEC;
2816 2817 }
2817 2818
2818 2819 /* Remember our eventual destination */
2819 2820 dst = ipha->ipha_src;
2820 2821
2821 2822 /*
2822 2823 * If the packet was for one of our unicast addresses, make
2823 2824 * sure we respond with that as the source. Otherwise
2824 2825 * have ip_output_simple pick the source address.
2825 2826 */
2826 2827 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2827 2828 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2828 2829 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2829 2830 if (ire != NULL) {
2830 2831 ire_refrele(ire);
2831 2832 src = ipha->ipha_dst;
2832 2833 } else {
2833 2834 src = INADDR_ANY;
2834 2835 ixas.ixa_flags |= IXAF_SET_SOURCE;
2835 2836 }
2836 2837
2837 2838 /*
2838 2839 * Check if we can send back more then 8 bytes in addition to
2839 2840 * the IP header. We try to send 64 bytes of data and the internal
2840 2841 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2841 2842 */
2842 2843 len_needed = IPH_HDR_LENGTH(ipha);
2843 2844 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2844 2845 ipha->ipha_protocol == IPPROTO_IPV6) {
2845 2846 if (!pullupmsg(mp, -1)) {
2846 2847 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2847 2848 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2848 2849 freemsg(mp);
2849 2850 return;
2850 2851 }
2851 2852 ipha = (ipha_t *)mp->b_rptr;
2852 2853
2853 2854 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2854 2855 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2855 2856 len_needed));
2856 2857 } else {
2857 2858 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2858 2859
2859 2860 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2860 2861 len_needed += ip_hdr_length_v6(mp, ip6h);
2861 2862 }
2862 2863 }
2863 2864 len_needed += ipst->ips_ip_icmp_return;
2864 2865 msg_len = msgdsize(mp);
2865 2866 if (msg_len > len_needed) {
2866 2867 (void) adjmsg(mp, len_needed - msg_len);
2867 2868 msg_len = len_needed;
2868 2869 }
2869 2870 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2870 2871 if (mp1 == NULL) {
2871 2872 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2872 2873 freemsg(mp);
2873 2874 return;
2874 2875 }
2875 2876 mp1->b_cont = mp;
2876 2877 mp = mp1;
2877 2878
2878 2879 /*
2879 2880 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2880 2881 * node generates be accepted in peace by all on-host destinations.
2881 2882 * If we do NOT assume that all on-host destinations trust
2882 2883 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2883 2884 * (Look for IXAF_TRUSTED_ICMP).
2884 2885 */
2885 2886 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2886 2887
2887 2888 ipha = (ipha_t *)mp->b_rptr;
2888 2889 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2889 2890 *ipha = icmp_ipha;
2890 2891 ipha->ipha_src = src;
2891 2892 ipha->ipha_dst = dst;
2892 2893 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2893 2894 msg_len += sizeof (icmp_ipha) + len;
2894 2895 if (msg_len > IP_MAXPACKET) {
2895 2896 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2896 2897 msg_len = IP_MAXPACKET;
2897 2898 }
2898 2899 ipha->ipha_length = htons((uint16_t)msg_len);
2899 2900 icmph = (icmph_t *)&ipha[1];
2900 2901 bcopy(stuff, icmph, len);
2901 2902 icmph->icmph_checksum = 0;
2902 2903 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2903 2904 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2904 2905
2905 2906 (void) ip_output_simple(mp, &ixas);
2906 2907 ixa_cleanup(&ixas);
2907 2908 }
2908 2909
2909 2910 /*
2910 2911 * Determine if an ICMP error packet can be sent given the rate limit.
2911 2912 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2912 2913 * in milliseconds) and a burst size. Burst size number of packets can
2913 2914 * be sent arbitrarely closely spaced.
2914 2915 * The state is tracked using two variables to implement an approximate
2915 2916 * token bucket filter:
2916 2917 * icmp_pkt_err_last - lbolt value when the last burst started
2917 2918 * icmp_pkt_err_sent - number of packets sent in current burst
2918 2919 */
2919 2920 boolean_t
2920 2921 icmp_err_rate_limit(ip_stack_t *ipst)
2921 2922 {
2922 2923 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2923 2924 uint_t refilled; /* Number of packets refilled in tbf since last */
2924 2925 /* Guard against changes by loading into local variable */
2925 2926 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2926 2927
2927 2928 if (err_interval == 0)
2928 2929 return (B_FALSE);
2929 2930
2930 2931 if (ipst->ips_icmp_pkt_err_last > now) {
2931 2932 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2932 2933 ipst->ips_icmp_pkt_err_last = 0;
2933 2934 ipst->ips_icmp_pkt_err_sent = 0;
2934 2935 }
2935 2936 /*
2936 2937 * If we are in a burst update the token bucket filter.
2937 2938 * Update the "last" time to be close to "now" but make sure
2938 2939 * we don't loose precision.
2939 2940 */
2940 2941 if (ipst->ips_icmp_pkt_err_sent != 0) {
2941 2942 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2942 2943 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2943 2944 ipst->ips_icmp_pkt_err_sent = 0;
2944 2945 } else {
2945 2946 ipst->ips_icmp_pkt_err_sent -= refilled;
2946 2947 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2947 2948 }
2948 2949 }
2949 2950 if (ipst->ips_icmp_pkt_err_sent == 0) {
2950 2951 /* Start of new burst */
2951 2952 ipst->ips_icmp_pkt_err_last = now;
2952 2953 }
2953 2954 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2954 2955 ipst->ips_icmp_pkt_err_sent++;
2955 2956 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2956 2957 ipst->ips_icmp_pkt_err_sent));
2957 2958 return (B_FALSE);
2958 2959 }
2959 2960 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2960 2961 return (B_TRUE);
2961 2962 }
2962 2963
2963 2964 /*
2964 2965 * Check if it is ok to send an IPv4 ICMP error packet in
2965 2966 * response to the IPv4 packet in mp.
2966 2967 * Free the message and return null if no
2967 2968 * ICMP error packet should be sent.
2968 2969 */
2969 2970 static mblk_t *
2970 2971 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2971 2972 {
2972 2973 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2973 2974 icmph_t *icmph;
2974 2975 ipha_t *ipha;
2975 2976 uint_t len_needed;
2976 2977
2977 2978 if (!mp)
2978 2979 return (NULL);
2979 2980 ipha = (ipha_t *)mp->b_rptr;
2980 2981 if (ip_csum_hdr(ipha)) {
2981 2982 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2982 2983 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2983 2984 freemsg(mp);
2984 2985 return (NULL);
2985 2986 }
2986 2987 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2987 2988 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2988 2989 CLASSD(ipha->ipha_dst) ||
2989 2990 CLASSD(ipha->ipha_src) ||
2990 2991 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2991 2992 /* Note: only errors to the fragment with offset 0 */
2992 2993 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2993 2994 freemsg(mp);
2994 2995 return (NULL);
2995 2996 }
2996 2997 if (ipha->ipha_protocol == IPPROTO_ICMP) {
2997 2998 /*
2998 2999 * Check the ICMP type. RFC 1122 sez: don't send ICMP
2999 3000 * errors in response to any ICMP errors.
3000 3001 */
3001 3002 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3002 3003 if (mp->b_wptr - mp->b_rptr < len_needed) {
3003 3004 if (!pullupmsg(mp, len_needed)) {
3004 3005 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3005 3006 freemsg(mp);
3006 3007 return (NULL);
3007 3008 }
3008 3009 ipha = (ipha_t *)mp->b_rptr;
3009 3010 }
3010 3011 icmph = (icmph_t *)
3011 3012 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3012 3013 switch (icmph->icmph_type) {
3013 3014 case ICMP_DEST_UNREACHABLE:
3014 3015 case ICMP_SOURCE_QUENCH:
3015 3016 case ICMP_TIME_EXCEEDED:
3016 3017 case ICMP_PARAM_PROBLEM:
3017 3018 case ICMP_REDIRECT:
3018 3019 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3019 3020 freemsg(mp);
3020 3021 return (NULL);
3021 3022 default:
3022 3023 break;
3023 3024 }
3024 3025 }
3025 3026 /*
3026 3027 * If this is a labeled system, then check to see if we're allowed to
3027 3028 * send a response to this particular sender. If not, then just drop.
3028 3029 */
3029 3030 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3030 3031 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3031 3032 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3032 3033 freemsg(mp);
3033 3034 return (NULL);
3034 3035 }
3035 3036 if (icmp_err_rate_limit(ipst)) {
3036 3037 /*
3037 3038 * Only send ICMP error packets every so often.
3038 3039 * This should be done on a per port/source basis,
3039 3040 * but for now this will suffice.
3040 3041 */
3041 3042 freemsg(mp);
3042 3043 return (NULL);
3043 3044 }
3044 3045 return (mp);
3045 3046 }
3046 3047
3047 3048 /*
3048 3049 * Called when a packet was sent out the same link that it arrived on.
3049 3050 * Check if it is ok to send a redirect and then send it.
3050 3051 */
3051 3052 void
3052 3053 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3053 3054 ip_recv_attr_t *ira)
3054 3055 {
3055 3056 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3056 3057 ipaddr_t src, nhop;
3057 3058 mblk_t *mp1;
3058 3059 ire_t *nhop_ire;
3059 3060
3060 3061 /*
3061 3062 * Check the source address to see if it originated
3062 3063 * on the same logical subnet it is going back out on.
3063 3064 * If so, we should be able to send it a redirect.
3064 3065 * Avoid sending a redirect if the destination
3065 3066 * is directly connected (i.e., we matched an IRE_ONLINK),
3066 3067 * or if the packet was source routed out this interface.
3067 3068 *
3068 3069 * We avoid sending a redirect if the
3069 3070 * destination is directly connected
3070 3071 * because it is possible that multiple
3071 3072 * IP subnets may have been configured on
3072 3073 * the link, and the source may not
3073 3074 * be on the same subnet as ip destination,
3074 3075 * even though they are on the same
3075 3076 * physical link.
3076 3077 */
3077 3078 if ((ire->ire_type & IRE_ONLINK) ||
3078 3079 ip_source_routed(ipha, ipst))
3079 3080 return;
3080 3081
3081 3082 nhop_ire = ire_nexthop(ire);
3082 3083 if (nhop_ire == NULL)
3083 3084 return;
3084 3085
3085 3086 nhop = nhop_ire->ire_addr;
3086 3087
3087 3088 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3088 3089 ire_t *ire2;
3089 3090
3090 3091 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3091 3092 mutex_enter(&nhop_ire->ire_lock);
3092 3093 ire2 = nhop_ire->ire_dep_parent;
3093 3094 if (ire2 != NULL)
3094 3095 ire_refhold(ire2);
3095 3096 mutex_exit(&nhop_ire->ire_lock);
3096 3097 ire_refrele(nhop_ire);
3097 3098 nhop_ire = ire2;
3098 3099 }
3099 3100 if (nhop_ire == NULL)
3100 3101 return;
3101 3102
3102 3103 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3103 3104
3104 3105 src = ipha->ipha_src;
3105 3106
3106 3107 /*
3107 3108 * We look at the interface ire for the nexthop,
3108 3109 * to see if ipha_src is in the same subnet
3109 3110 * as the nexthop.
3110 3111 */
3111 3112 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3112 3113 /*
3113 3114 * The source is directly connected.
3114 3115 */
3115 3116 mp1 = copymsg(mp);
3116 3117 if (mp1 != NULL) {
3117 3118 icmp_send_redirect(mp1, nhop, ira);
3118 3119 }
3119 3120 }
3120 3121 ire_refrele(nhop_ire);
3121 3122 }
3122 3123
3123 3124 /*
3124 3125 * Generate an ICMP redirect message.
3125 3126 */
3126 3127 static void
3127 3128 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3128 3129 {
3129 3130 icmph_t icmph;
3130 3131 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3131 3132
3132 3133 mp = icmp_pkt_err_ok(mp, ira);
3133 3134 if (mp == NULL)
3134 3135 return;
3135 3136
3136 3137 bzero(&icmph, sizeof (icmph_t));
3137 3138 icmph.icmph_type = ICMP_REDIRECT;
3138 3139 icmph.icmph_code = 1;
3139 3140 icmph.icmph_rd_gateway = gateway;
3140 3141 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3141 3142 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3142 3143 }
3143 3144
3144 3145 /*
3145 3146 * Generate an ICMP time exceeded message.
3146 3147 */
3147 3148 void
3148 3149 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3149 3150 {
3150 3151 icmph_t icmph;
3151 3152 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3152 3153
3153 3154 mp = icmp_pkt_err_ok(mp, ira);
3154 3155 if (mp == NULL)
3155 3156 return;
3156 3157
3157 3158 bzero(&icmph, sizeof (icmph_t));
3158 3159 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3159 3160 icmph.icmph_code = code;
3160 3161 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3161 3162 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3162 3163 }
3163 3164
3164 3165 /*
3165 3166 * Generate an ICMP unreachable message.
3166 3167 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3167 3168 * constructed by the caller.
3168 3169 */
3169 3170 void
3170 3171 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3171 3172 {
3172 3173 icmph_t icmph;
3173 3174 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3174 3175
3175 3176 mp = icmp_pkt_err_ok(mp, ira);
3176 3177 if (mp == NULL)
3177 3178 return;
3178 3179
3179 3180 bzero(&icmph, sizeof (icmph_t));
3180 3181 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3181 3182 icmph.icmph_code = code;
3182 3183 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3183 3184 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3184 3185 }
3185 3186
3186 3187 /*
3187 3188 * Latch in the IPsec state for a stream based the policy in the listener
3188 3189 * and the actions in the ip_recv_attr_t.
3189 3190 * Called directly from TCP and SCTP.
3190 3191 */
3191 3192 boolean_t
3192 3193 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3193 3194 {
3194 3195 ASSERT(lconnp->conn_policy != NULL);
3195 3196 ASSERT(connp->conn_policy == NULL);
3196 3197
3197 3198 IPPH_REFHOLD(lconnp->conn_policy);
3198 3199 connp->conn_policy = lconnp->conn_policy;
3199 3200
3200 3201 if (ira->ira_ipsec_action != NULL) {
3201 3202 if (connp->conn_latch == NULL) {
3202 3203 connp->conn_latch = iplatch_create();
3203 3204 if (connp->conn_latch == NULL)
3204 3205 return (B_FALSE);
3205 3206 }
3206 3207 ipsec_latch_inbound(connp, ira);
3207 3208 }
3208 3209 return (B_TRUE);
3209 3210 }
3210 3211
3211 3212 /*
3212 3213 * Verify whether or not the IP address is a valid local address.
3213 3214 * Could be a unicast, including one for a down interface.
3214 3215 * If allow_mcbc then a multicast or broadcast address is also
3215 3216 * acceptable.
3216 3217 *
3217 3218 * In the case of a broadcast/multicast address, however, the
3218 3219 * upper protocol is expected to reset the src address
3219 3220 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3220 3221 * no packets are emitted with broadcast/multicast address as
3221 3222 * source address (that violates hosts requirements RFC 1122)
3222 3223 * The addresses valid for bind are:
3223 3224 * (1) - INADDR_ANY (0)
3224 3225 * (2) - IP address of an UP interface
3225 3226 * (3) - IP address of a DOWN interface
3226 3227 * (4) - valid local IP broadcast addresses. In this case
3227 3228 * the conn will only receive packets destined to
3228 3229 * the specified broadcast address.
3229 3230 * (5) - a multicast address. In this case
3230 3231 * the conn will only receive packets destined to
3231 3232 * the specified multicast address. Note: the
3232 3233 * application still has to issue an
3233 3234 * IP_ADD_MEMBERSHIP socket option.
3234 3235 *
3235 3236 * In all the above cases, the bound address must be valid in the current zone.
3236 3237 * When the address is loopback, multicast or broadcast, there might be many
3237 3238 * matching IREs so bind has to look up based on the zone.
3238 3239 */
3239 3240 ip_laddr_t
3240 3241 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3241 3242 ip_stack_t *ipst, boolean_t allow_mcbc)
3242 3243 {
3243 3244 ire_t *src_ire;
3244 3245
3245 3246 ASSERT(src_addr != INADDR_ANY);
3246 3247
3247 3248 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3248 3249 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3249 3250
3250 3251 /*
3251 3252 * If an address other than in6addr_any is requested,
3252 3253 * we verify that it is a valid address for bind
3253 3254 * Note: Following code is in if-else-if form for
3254 3255 * readability compared to a condition check.
3255 3256 */
3256 3257 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3257 3258 /*
3258 3259 * (2) Bind to address of local UP interface
3259 3260 */
3260 3261 ire_refrele(src_ire);
3261 3262 return (IPVL_UNICAST_UP);
3262 3263 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3263 3264 /*
3264 3265 * (4) Bind to broadcast address
3265 3266 */
3266 3267 ire_refrele(src_ire);
3267 3268 if (allow_mcbc)
3268 3269 return (IPVL_BCAST);
3269 3270 else
3270 3271 return (IPVL_BAD);
3271 3272 } else if (CLASSD(src_addr)) {
3272 3273 /* (5) bind to multicast address. */
3273 3274 if (src_ire != NULL)
3274 3275 ire_refrele(src_ire);
3275 3276
3276 3277 if (allow_mcbc)
3277 3278 return (IPVL_MCAST);
3278 3279 else
3279 3280 return (IPVL_BAD);
3280 3281 } else {
3281 3282 ipif_t *ipif;
3282 3283
3283 3284 /*
3284 3285 * (3) Bind to address of local DOWN interface?
3285 3286 * (ipif_lookup_addr() looks up all interfaces
3286 3287 * but we do not get here for UP interfaces
3287 3288 * - case (2) above)
3288 3289 */
3289 3290 if (src_ire != NULL)
3290 3291 ire_refrele(src_ire);
3291 3292
3292 3293 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3293 3294 if (ipif == NULL)
3294 3295 return (IPVL_BAD);
3295 3296
3296 3297 /* Not a useful source? */
3297 3298 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3298 3299 ipif_refrele(ipif);
3299 3300 return (IPVL_BAD);
3300 3301 }
3301 3302 ipif_refrele(ipif);
3302 3303 return (IPVL_UNICAST_DOWN);
3303 3304 }
3304 3305 }
3305 3306
3306 3307 /*
3307 3308 * Insert in the bind fanout for IPv4 and IPv6.
3308 3309 * The caller should already have used ip_laddr_verify_v*() before calling
3309 3310 * this.
3310 3311 */
3311 3312 int
3312 3313 ip_laddr_fanout_insert(conn_t *connp)
3313 3314 {
3314 3315 int error;
3315 3316
3316 3317 /*
3317 3318 * Allow setting new policies. For example, disconnects result
3318 3319 * in us being called. As we would have set conn_policy_cached
3319 3320 * to B_TRUE before, we should set it to B_FALSE, so that policy
3320 3321 * can change after the disconnect.
3321 3322 */
3322 3323 connp->conn_policy_cached = B_FALSE;
3323 3324
3324 3325 error = ipcl_bind_insert(connp);
3325 3326 if (error != 0) {
3326 3327 if (connp->conn_anon_port) {
3327 3328 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3328 3329 connp->conn_mlp_type, connp->conn_proto,
3329 3330 ntohs(connp->conn_lport), B_FALSE);
3330 3331 }
3331 3332 connp->conn_mlp_type = mlptSingle;
3332 3333 }
3333 3334 return (error);
3334 3335 }
3335 3336
3336 3337 /*
3337 3338 * Verify that both the source and destination addresses are valid. If
3338 3339 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3339 3340 * i.e. have no route to it. Protocols like TCP want to verify destination
3340 3341 * reachability, while tunnels do not.
3341 3342 *
3342 3343 * Determine the route, the interface, and (optionally) the source address
3343 3344 * to use to reach a given destination.
3344 3345 * Note that we allow connect to broadcast and multicast addresses when
3345 3346 * IPDF_ALLOW_MCBC is set.
3346 3347 * first_hop and dst_addr are normally the same, but if source routing
3347 3348 * they will differ; in that case the first_hop is what we'll use for the
3348 3349 * routing lookup but the dce and label checks will be done on dst_addr,
3349 3350 *
3350 3351 * If uinfo is set, then we fill in the best available information
3351 3352 * we have for the destination. This is based on (in priority order) any
3352 3353 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3353 3354 * ill_mtu/ill_mc_mtu.
3354 3355 *
3355 3356 * Tsol note: If we have a source route then dst_addr != firsthop. But we
3356 3357 * always do the label check on dst_addr.
3357 3358 */
3358 3359 int
3359 3360 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3360 3361 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3361 3362 {
3362 3363 ire_t *ire = NULL;
3363 3364 int error = 0;
3364 3365 ipaddr_t setsrc; /* RTF_SETSRC */
3365 3366 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3366 3367 ip_stack_t *ipst = ixa->ixa_ipst;
3367 3368 dce_t *dce;
3368 3369 uint_t pmtu;
3369 3370 uint_t generation;
3370 3371 nce_t *nce;
3371 3372 ill_t *ill = NULL;
3372 3373 boolean_t multirt = B_FALSE;
3373 3374
3374 3375 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3375 3376
3376 3377 /*
3377 3378 * We never send to zero; the ULPs map it to the loopback address.
3378 3379 * We can't allow it since we use zero to mean unitialized in some
3379 3380 * places.
3380 3381 */
3381 3382 ASSERT(dst_addr != INADDR_ANY);
3382 3383
3383 3384 if (is_system_labeled()) {
3384 3385 ts_label_t *tsl = NULL;
3385 3386
3386 3387 error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3387 3388 mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3388 3389 if (error != 0)
3389 3390 return (error);
3390 3391 if (tsl != NULL) {
3391 3392 /* Update the label */
3392 3393 ip_xmit_attr_replace_tsl(ixa, tsl);
3393 3394 }
3394 3395 }
3395 3396
3396 3397 setsrc = INADDR_ANY;
3397 3398 /*
3398 3399 * Select a route; For IPMP interfaces, we would only select
3399 3400 * a "hidden" route (i.e., going through a specific under_ill)
3400 3401 * if ixa_ifindex has been specified.
3401 3402 */
3402 3403 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3403 3404 &generation, &setsrc, &error, &multirt);
3404 3405 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3405 3406 if (error != 0)
3406 3407 goto bad_addr;
3407 3408
3408 3409 /*
3409 3410 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3410 3411 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3411 3412 * Otherwise the destination needn't be reachable.
3412 3413 *
3413 3414 * If we match on a reject or black hole, then we've got a
3414 3415 * local failure. May as well fail out the connect() attempt,
3415 3416 * since it's never going to succeed.
3416 3417 */
3417 3418 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3418 3419 /*
3419 3420 * If we're verifying destination reachability, we always want
3420 3421 * to complain here.
3421 3422 *
3422 3423 * If we're not verifying destination reachability but the
3423 3424 * destination has a route, we still want to fail on the
3424 3425 * temporary address and broadcast address tests.
3425 3426 *
3426 3427 * In both cases do we let the code continue so some reasonable
3427 3428 * information is returned to the caller. That enables the
3428 3429 * caller to use (and even cache) the IRE. conn_ip_ouput will
3429 3430 * use the generation mismatch path to check for the unreachable
3430 3431 * case thereby avoiding any specific check in the main path.
3431 3432 */
3432 3433 ASSERT(generation == IRE_GENERATION_VERIFY);
3433 3434 if (flags & IPDF_VERIFY_DST) {
3434 3435 /*
3435 3436 * Set errno but continue to set up ixa_ire to be
3436 3437 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3437 3438 * That allows callers to use ip_output to get an
3438 3439 * ICMP error back.
3439 3440 */
3440 3441 if (!(ire->ire_type & IRE_HOST))
3441 3442 error = ENETUNREACH;
3442 3443 else
3443 3444 error = EHOSTUNREACH;
3444 3445 }
3445 3446 }
3446 3447
3447 3448 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3448 3449 !(flags & IPDF_ALLOW_MCBC)) {
3449 3450 ire_refrele(ire);
3450 3451 ire = ire_reject(ipst, B_FALSE);
3451 3452 generation = IRE_GENERATION_VERIFY;
3452 3453 error = ENETUNREACH;
3453 3454 }
3454 3455
3455 3456 /* Cache things */
3456 3457 if (ixa->ixa_ire != NULL)
3457 3458 ire_refrele_notr(ixa->ixa_ire);
3458 3459 #ifdef DEBUG
3459 3460 ire_refhold_notr(ire);
3460 3461 ire_refrele(ire);
3461 3462 #endif
3462 3463 ixa->ixa_ire = ire;
3463 3464 ixa->ixa_ire_generation = generation;
3464 3465
3465 3466 /*
3466 3467 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3467 3468 * since some callers will send a packet to conn_ip_output() even if
3468 3469 * there's an error.
3469 3470 */
3470 3471 if (flags & IPDF_UNIQUE_DCE) {
3471 3472 /* Fallback to the default dce if allocation fails */
3472 3473 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3473 3474 if (dce != NULL)
3474 3475 generation = dce->dce_generation;
3475 3476 else
3476 3477 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3477 3478 } else {
3478 3479 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3479 3480 }
3480 3481 ASSERT(dce != NULL);
3481 3482 if (ixa->ixa_dce != NULL)
3482 3483 dce_refrele_notr(ixa->ixa_dce);
3483 3484 #ifdef DEBUG
3484 3485 dce_refhold_notr(dce);
3485 3486 dce_refrele(dce);
3486 3487 #endif
3487 3488 ixa->ixa_dce = dce;
3488 3489 ixa->ixa_dce_generation = generation;
3489 3490
3490 3491 /*
3491 3492 * For multicast with multirt we have a flag passed back from
3492 3493 * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3493 3494 * possible multicast address.
3494 3495 * We also need a flag for multicast since we can't check
3495 3496 * whether RTF_MULTIRT is set in ixa_ire for multicast.
3496 3497 */
3497 3498 if (multirt) {
3498 3499 ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3499 3500 ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3500 3501 } else {
3501 3502 ixa->ixa_postfragfn = ire->ire_postfragfn;
3502 3503 ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3503 3504 }
3504 3505 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3505 3506 /* Get an nce to cache. */
3506 3507 nce = ire_to_nce(ire, firsthop, NULL);
3507 3508 if (nce == NULL) {
3508 3509 /* Allocation failure? */
3509 3510 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3510 3511 } else {
3511 3512 if (ixa->ixa_nce != NULL)
3512 3513 nce_refrele(ixa->ixa_nce);
3513 3514 ixa->ixa_nce = nce;
3514 3515 }
3515 3516 }
3516 3517
3517 3518 /*
3518 3519 * If the source address is a loopback address, the
3519 3520 * destination had best be local or multicast.
3520 3521 * If we are sending to an IRE_LOCAL using a loopback source then
3521 3522 * it had better be the same zoneid.
3522 3523 */
3523 3524 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3524 3525 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3525 3526 ire = NULL; /* Stored in ixa_ire */
3526 3527 error = EADDRNOTAVAIL;
3527 3528 goto bad_addr;
3528 3529 }
3529 3530 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3530 3531 ire = NULL; /* Stored in ixa_ire */
3531 3532 error = EADDRNOTAVAIL;
3532 3533 goto bad_addr;
3533 3534 }
3534 3535 }
3535 3536 if (ire->ire_type & IRE_BROADCAST) {
3536 3537 /*
3537 3538 * If the ULP didn't have a specified source, then we
3538 3539 * make sure we reselect the source when sending
3539 3540 * broadcasts out different interfaces.
3540 3541 */
3541 3542 if (flags & IPDF_SELECT_SRC)
3542 3543 ixa->ixa_flags |= IXAF_SET_SOURCE;
3543 3544 else
3544 3545 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3545 3546 }
3546 3547
3547 3548 /*
3548 3549 * Does the caller want us to pick a source address?
3549 3550 */
3550 3551 if (flags & IPDF_SELECT_SRC) {
3551 3552 ipaddr_t src_addr;
3552 3553
3553 3554 /*
3554 3555 * We use use ire_nexthop_ill to avoid the under ipmp
3555 3556 * interface for source address selection. Note that for ipmp
3556 3557 * probe packets, ixa_ifindex would have been specified, and
3557 3558 * the ip_select_route() invocation would have picked an ire
3558 3559 * will ire_ill pointing at an under interface.
3559 3560 */
3560 3561 ill = ire_nexthop_ill(ire);
3561 3562
3562 3563 /* If unreachable we have no ill but need some source */
3563 3564 if (ill == NULL) {
3564 3565 src_addr = htonl(INADDR_LOOPBACK);
3565 3566 /* Make sure we look for a better source address */
3566 3567 generation = SRC_GENERATION_VERIFY;
3567 3568 } else {
3568 3569 error = ip_select_source_v4(ill, setsrc, dst_addr,
3569 3570 ixa->ixa_multicast_ifaddr, zoneid,
3570 3571 ipst, &src_addr, &generation, NULL);
3571 3572 if (error != 0) {
3572 3573 ire = NULL; /* Stored in ixa_ire */
3573 3574 goto bad_addr;
3574 3575 }
3575 3576 }
3576 3577
3577 3578 /*
3578 3579 * We allow the source address to to down.
3579 3580 * However, we check that we don't use the loopback address
3580 3581 * as a source when sending out on the wire.
3581 3582 */
3582 3583 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3583 3584 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3584 3585 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3585 3586 ire = NULL; /* Stored in ixa_ire */
3586 3587 error = EADDRNOTAVAIL;
3587 3588 goto bad_addr;
3588 3589 }
3589 3590
3590 3591 *src_addrp = src_addr;
3591 3592 ixa->ixa_src_generation = generation;
3592 3593 }
3593 3594
3594 3595 /*
3595 3596 * Make sure we don't leave an unreachable ixa_nce in place
3596 3597 * since ip_select_route is used when we unplumb i.e., remove
3597 3598 * references on ixa_ire, ixa_nce, and ixa_dce.
3598 3599 */
3599 3600 nce = ixa->ixa_nce;
3600 3601 if (nce != NULL && nce->nce_is_condemned) {
3601 3602 nce_refrele(nce);
3602 3603 ixa->ixa_nce = NULL;
3603 3604 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3604 3605 }
3605 3606
3606 3607 /*
3607 3608 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3608 3609 * However, we can't do it for IPv4 multicast or broadcast.
3609 3610 */
3610 3611 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3611 3612 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3612 3613
3613 3614 /*
3614 3615 * Set initial value for fragmentation limit. Either conn_ip_output
3615 3616 * or ULP might updates it when there are routing changes.
3616 3617 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3617 3618 */
3618 3619 pmtu = ip_get_pmtu(ixa);
3619 3620 ixa->ixa_fragsize = pmtu;
3620 3621 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3621 3622 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3622 3623 ixa->ixa_pmtu = pmtu;
3623 3624
3624 3625 /*
3625 3626 * Extract information useful for some transports.
3626 3627 * First we look for DCE metrics. Then we take what we have in
3627 3628 * the metrics in the route, where the offlink is used if we have
3628 3629 * one.
3629 3630 */
3630 3631 if (uinfo != NULL) {
3631 3632 bzero(uinfo, sizeof (*uinfo));
3632 3633
3633 3634 if (dce->dce_flags & DCEF_UINFO)
3634 3635 *uinfo = dce->dce_uinfo;
3635 3636
3636 3637 rts_merge_metrics(uinfo, &ire->ire_metrics);
3637 3638
3638 3639 /* Allow ire_metrics to decrease the path MTU from above */
3639 3640 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3640 3641 uinfo->iulp_mtu = pmtu;
3641 3642
3642 3643 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3643 3644 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3644 3645 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3645 3646 }
3646 3647
3647 3648 if (ill != NULL)
3648 3649 ill_refrele(ill);
3649 3650
3650 3651 return (error);
3651 3652
3652 3653 bad_addr:
3653 3654 if (ire != NULL)
3654 3655 ire_refrele(ire);
3655 3656
3656 3657 if (ill != NULL)
3657 3658 ill_refrele(ill);
3658 3659
3659 3660 /*
3660 3661 * Make sure we don't leave an unreachable ixa_nce in place
3661 3662 * since ip_select_route is used when we unplumb i.e., remove
3662 3663 * references on ixa_ire, ixa_nce, and ixa_dce.
3663 3664 */
3664 3665 nce = ixa->ixa_nce;
3665 3666 if (nce != NULL && nce->nce_is_condemned) {
3666 3667 nce_refrele(nce);
3667 3668 ixa->ixa_nce = NULL;
3668 3669 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3669 3670 }
3670 3671
3671 3672 return (error);
3672 3673 }
3673 3674
3674 3675
3675 3676 /*
3676 3677 * Get the base MTU for the case when path MTU discovery is not used.
3677 3678 * Takes the MTU of the IRE into account.
3678 3679 */
3679 3680 uint_t
3680 3681 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3681 3682 {
3682 3683 uint_t mtu;
3683 3684 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3684 3685
3685 3686 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3686 3687 mtu = ill->ill_mc_mtu;
3687 3688 else
3688 3689 mtu = ill->ill_mtu;
3689 3690
3690 3691 if (iremtu != 0 && iremtu < mtu)
3691 3692 mtu = iremtu;
3692 3693
3693 3694 return (mtu);
3694 3695 }
3695 3696
3696 3697 /*
3697 3698 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3698 3699 * Assumes that ixa_ire, dce, and nce have already been set up.
3699 3700 *
3700 3701 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3701 3702 * We avoid path MTU discovery if it is disabled with ndd.
3702 3703 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3703 3704 *
3704 3705 * NOTE: We also used to turn it off for source routed packets. That
3705 3706 * is no longer required since the dce is per final destination.
3706 3707 */
3707 3708 uint_t
3708 3709 ip_get_pmtu(ip_xmit_attr_t *ixa)
3709 3710 {
3710 3711 ip_stack_t *ipst = ixa->ixa_ipst;
3711 3712 dce_t *dce;
3712 3713 nce_t *nce;
3713 3714 ire_t *ire;
3714 3715 uint_t pmtu;
3715 3716
3716 3717 ire = ixa->ixa_ire;
3717 3718 dce = ixa->ixa_dce;
3718 3719 nce = ixa->ixa_nce;
3719 3720
3720 3721 /*
3721 3722 * If path MTU discovery has been turned off by ndd, then we ignore
3722 3723 * any dce_pmtu and for IPv4 we will not set DF.
3723 3724 */
3724 3725 if (!ipst->ips_ip_path_mtu_discovery)
3725 3726 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3726 3727
3727 3728 pmtu = IP_MAXPACKET;
3728 3729 /*
3729 3730 * Decide whether whether IPv4 sets DF
3730 3731 * For IPv6 "no DF" means to use the 1280 mtu
3731 3732 */
3732 3733 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3733 3734 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3734 3735 } else {
3735 3736 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3736 3737 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3737 3738 pmtu = IPV6_MIN_MTU;
3738 3739 }
3739 3740
3740 3741 /* Check if the PMTU is to old before we use it */
3741 3742 if ((dce->dce_flags & DCEF_PMTU) &&
3742 3743 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3743 3744 ipst->ips_ip_pathmtu_interval) {
3744 3745 /*
3745 3746 * Older than 20 minutes. Drop the path MTU information.
3746 3747 */
3747 3748 mutex_enter(&dce->dce_lock);
3748 3749 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3749 3750 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3750 3751 mutex_exit(&dce->dce_lock);
3751 3752 dce_increment_generation(dce);
3752 3753 }
3753 3754
3754 3755 /* The metrics on the route can lower the path MTU */
3755 3756 if (ire->ire_metrics.iulp_mtu != 0 &&
3756 3757 ire->ire_metrics.iulp_mtu < pmtu)
3757 3758 pmtu = ire->ire_metrics.iulp_mtu;
3758 3759
3759 3760 /*
3760 3761 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3761 3762 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3762 3763 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3763 3764 */
3764 3765 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3765 3766 if (dce->dce_flags & DCEF_PMTU) {
3766 3767 if (dce->dce_pmtu < pmtu)
3767 3768 pmtu = dce->dce_pmtu;
3768 3769
3769 3770 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3770 3771 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3771 3772 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3772 3773 } else {
3773 3774 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3774 3775 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3775 3776 }
3776 3777 } else {
3777 3778 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3778 3779 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3779 3780 }
3780 3781 }
3781 3782
3782 3783 /*
3783 3784 * If we have an IRE_LOCAL we use the loopback mtu instead of
3784 3785 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3785 3786 * mtu as IRE_LOOPBACK.
3786 3787 */
3787 3788 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3788 3789 uint_t loopback_mtu;
3789 3790
3790 3791 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3791 3792 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3792 3793
3793 3794 if (loopback_mtu < pmtu)
3794 3795 pmtu = loopback_mtu;
3795 3796 } else if (nce != NULL) {
3796 3797 /*
3797 3798 * Make sure we don't exceed the interface MTU.
3798 3799 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3799 3800 * an ill. We'd use the above IP_MAXPACKET in that case just
3800 3801 * to tell the transport something larger than zero.
3801 3802 */
3802 3803 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3803 3804 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3804 3805 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3805 3806 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3806 3807 nce->nce_ill->ill_mc_mtu < pmtu) {
3807 3808 /*
3808 3809 * for interfaces in an IPMP group, the mtu of
3809 3810 * the nce_ill (under_ill) could be different
3810 3811 * from the mtu of the ncec_ill, so we take the
3811 3812 * min of the two.
3812 3813 */
3813 3814 pmtu = nce->nce_ill->ill_mc_mtu;
3814 3815 }
3815 3816 } else {
3816 3817 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3817 3818 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3818 3819 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3819 3820 nce->nce_ill->ill_mtu < pmtu) {
3820 3821 /*
3821 3822 * for interfaces in an IPMP group, the mtu of
3822 3823 * the nce_ill (under_ill) could be different
3823 3824 * from the mtu of the ncec_ill, so we take the
3824 3825 * min of the two.
3825 3826 */
3826 3827 pmtu = nce->nce_ill->ill_mtu;
3827 3828 }
3828 3829 }
3829 3830 }
3830 3831
3831 3832 /*
3832 3833 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3833 3834 * Only applies to IPv6.
3834 3835 */
3835 3836 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3836 3837 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3837 3838 switch (ixa->ixa_use_min_mtu) {
3838 3839 case IPV6_USE_MIN_MTU_MULTICAST:
3839 3840 if (ire->ire_type & IRE_MULTICAST)
3840 3841 pmtu = IPV6_MIN_MTU;
3841 3842 break;
3842 3843 case IPV6_USE_MIN_MTU_ALWAYS:
3843 3844 pmtu = IPV6_MIN_MTU;
3844 3845 break;
3845 3846 case IPV6_USE_MIN_MTU_NEVER:
3846 3847 break;
3847 3848 }
3848 3849 } else {
3849 3850 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3850 3851 if (ire->ire_type & IRE_MULTICAST)
3851 3852 pmtu = IPV6_MIN_MTU;
3852 3853 }
3853 3854 }
3854 3855
3855 3856 /*
3856 3857 * For multirouted IPv6 packets, the IP layer will insert a 8-byte
3857 3858 * fragment header in every packet. We compensate for those cases by
3858 3859 * returning a smaller path MTU to the ULP.
3859 3860 *
3860 3861 * In the case of CGTP then ip_output will add a fragment header.
3861 3862 * Make sure there is room for it by telling a smaller number
3862 3863 * to the transport.
3863 3864 *
3864 3865 * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3865 3866 * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3866 3867 * which is the size of the packets it can send.
3867 3868 */
3868 3869 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3869 3870 if ((ire->ire_flags & RTF_MULTIRT) ||
3870 3871 (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3871 3872 pmtu -= sizeof (ip6_frag_t);
3872 3873 ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3873 3874 }
3874 3875 }
3875 3876
3876 3877 return (pmtu);
3877 3878 }
3878 3879
3879 3880 /*
3880 3881 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3881 3882 * the final piece where we don't. Return a pointer to the first mblk in the
3882 3883 * result, and update the pointer to the next mblk to chew on. If anything
3883 3884 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3884 3885 * NULL pointer.
3885 3886 */
3886 3887 mblk_t *
3887 3888 ip_carve_mp(mblk_t **mpp, ssize_t len)
3888 3889 {
3889 3890 mblk_t *mp0;
3890 3891 mblk_t *mp1;
3891 3892 mblk_t *mp2;
3892 3893
3893 3894 if (!len || !mpp || !(mp0 = *mpp))
3894 3895 return (NULL);
3895 3896 /* If we aren't going to consume the first mblk, we need a dup. */
3896 3897 if (mp0->b_wptr - mp0->b_rptr > len) {
3897 3898 mp1 = dupb(mp0);
3898 3899 if (mp1) {
3899 3900 /* Partition the data between the two mblks. */
3900 3901 mp1->b_wptr = mp1->b_rptr + len;
3901 3902 mp0->b_rptr = mp1->b_wptr;
3902 3903 /*
3903 3904 * after adjustments if mblk not consumed is now
3904 3905 * unaligned, try to align it. If this fails free
3905 3906 * all messages and let upper layer recover.
3906 3907 */
3907 3908 if (!OK_32PTR(mp0->b_rptr)) {
3908 3909 if (!pullupmsg(mp0, -1)) {
3909 3910 freemsg(mp0);
3910 3911 freemsg(mp1);
3911 3912 *mpp = NULL;
3912 3913 return (NULL);
3913 3914 }
3914 3915 }
3915 3916 }
3916 3917 return (mp1);
3917 3918 }
3918 3919 /* Eat through as many mblks as we need to get len bytes. */
3919 3920 len -= mp0->b_wptr - mp0->b_rptr;
3920 3921 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3921 3922 if (mp2->b_wptr - mp2->b_rptr > len) {
3922 3923 /*
3923 3924 * We won't consume the entire last mblk. Like
3924 3925 * above, dup and partition it.
3925 3926 */
3926 3927 mp1->b_cont = dupb(mp2);
3927 3928 mp1 = mp1->b_cont;
3928 3929 if (!mp1) {
3929 3930 /*
3930 3931 * Trouble. Rather than go to a lot of
3931 3932 * trouble to clean up, we free the messages.
3932 3933 * This won't be any worse than losing it on
3933 3934 * the wire.
3934 3935 */
3935 3936 freemsg(mp0);
3936 3937 freemsg(mp2);
3937 3938 *mpp = NULL;
3938 3939 return (NULL);
3939 3940 }
3940 3941 mp1->b_wptr = mp1->b_rptr + len;
3941 3942 mp2->b_rptr = mp1->b_wptr;
3942 3943 /*
3943 3944 * after adjustments if mblk not consumed is now
3944 3945 * unaligned, try to align it. If this fails free
3945 3946 * all messages and let upper layer recover.
3946 3947 */
3947 3948 if (!OK_32PTR(mp2->b_rptr)) {
3948 3949 if (!pullupmsg(mp2, -1)) {
3949 3950 freemsg(mp0);
3950 3951 freemsg(mp2);
3951 3952 *mpp = NULL;
3952 3953 return (NULL);
3953 3954 }
3954 3955 }
3955 3956 *mpp = mp2;
3956 3957 return (mp0);
3957 3958 }
3958 3959 /* Decrement len by the amount we just got. */
3959 3960 len -= mp2->b_wptr - mp2->b_rptr;
3960 3961 }
3961 3962 /*
3962 3963 * len should be reduced to zero now. If not our caller has
3963 3964 * screwed up.
3964 3965 */
3965 3966 if (len) {
3966 3967 /* Shouldn't happen! */
3967 3968 freemsg(mp0);
3968 3969 *mpp = NULL;
3969 3970 return (NULL);
3970 3971 }
3971 3972 /*
3972 3973 * We consumed up to exactly the end of an mblk. Detach the part
3973 3974 * we are returning from the rest of the chain.
3974 3975 */
3975 3976 mp1->b_cont = NULL;
3976 3977 *mpp = mp2;
3977 3978 return (mp0);
3978 3979 }
3979 3980
3980 3981 /* The ill stream is being unplumbed. Called from ip_close */
3981 3982 int
3982 3983 ip_modclose(ill_t *ill)
3983 3984 {
3984 3985 boolean_t success;
3985 3986 ipsq_t *ipsq;
3986 3987 ipif_t *ipif;
3987 3988 queue_t *q = ill->ill_rq;
3988 3989 ip_stack_t *ipst = ill->ill_ipst;
3989 3990 int i;
3990 3991 arl_ill_common_t *ai = ill->ill_common;
3991 3992
3992 3993 /*
3993 3994 * The punlink prior to this may have initiated a capability
3994 3995 * negotiation. But ipsq_enter will block until that finishes or
3995 3996 * times out.
3996 3997 */
3997 3998 success = ipsq_enter(ill, B_FALSE, NEW_OP);
3998 3999
3999 4000 /*
4000 4001 * Open/close/push/pop is guaranteed to be single threaded
4001 4002 * per stream by STREAMS. FS guarantees that all references
4002 4003 * from top are gone before close is called. So there can't
4003 4004 * be another close thread that has set CONDEMNED on this ill.
4004 4005 * and cause ipsq_enter to return failure.
4005 4006 */
4006 4007 ASSERT(success);
4007 4008 ipsq = ill->ill_phyint->phyint_ipsq;
4008 4009
4009 4010 /*
4010 4011 * Mark it condemned. No new reference will be made to this ill.
4011 4012 * Lookup functions will return an error. Threads that try to
4012 4013 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4013 4014 * that the refcnt will drop down to zero.
4014 4015 */
4015 4016 mutex_enter(&ill->ill_lock);
4016 4017 ill->ill_state_flags |= ILL_CONDEMNED;
4017 4018 for (ipif = ill->ill_ipif; ipif != NULL;
4018 4019 ipif = ipif->ipif_next) {
4019 4020 ipif->ipif_state_flags |= IPIF_CONDEMNED;
4020 4021 }
4021 4022 /*
4022 4023 * Wake up anybody waiting to enter the ipsq. ipsq_enter
4023 4024 * returns error if ILL_CONDEMNED is set
4024 4025 */
4025 4026 cv_broadcast(&ill->ill_cv);
4026 4027 mutex_exit(&ill->ill_lock);
4027 4028
4028 4029 /*
4029 4030 * Send all the deferred DLPI messages downstream which came in
4030 4031 * during the small window right before ipsq_enter(). We do this
4031 4032 * without waiting for the ACKs because all the ACKs for M_PROTO
4032 4033 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4033 4034 */
4034 4035 ill_dlpi_send_deferred(ill);
4035 4036
4036 4037 /*
4037 4038 * Shut down fragmentation reassembly.
4038 4039 * ill_frag_timer won't start a timer again.
4039 4040 * Now cancel any existing timer
4040 4041 */
4041 4042 (void) untimeout(ill->ill_frag_timer_id);
4042 4043 (void) ill_frag_timeout(ill, 0);
4043 4044
4044 4045 /*
4045 4046 * Call ill_delete to bring down the ipifs, ilms and ill on
4046 4047 * this ill. Then wait for the refcnts to drop to zero.
4047 4048 * ill_is_freeable checks whether the ill is really quiescent.
4048 4049 * Then make sure that threads that are waiting to enter the
4049 4050 * ipsq have seen the error returned by ipsq_enter and have
4050 4051 * gone away. Then we call ill_delete_tail which does the
4051 4052 * DL_UNBIND_REQ with the driver and then qprocsoff.
4052 4053 */
4053 4054 ill_delete(ill);
4054 4055 mutex_enter(&ill->ill_lock);
4055 4056 while (!ill_is_freeable(ill))
4056 4057 cv_wait(&ill->ill_cv, &ill->ill_lock);
4057 4058
4058 4059 while (ill->ill_waiters)
4059 4060 cv_wait(&ill->ill_cv, &ill->ill_lock);
4060 4061
4061 4062 mutex_exit(&ill->ill_lock);
4062 4063
4063 4064 /*
4064 4065 * ill_delete_tail drops reference on ill_ipst, but we need to keep
4065 4066 * it held until the end of the function since the cleanup
4066 4067 * below needs to be able to use the ip_stack_t.
4067 4068 */
4068 4069 netstack_hold(ipst->ips_netstack);
4069 4070
4070 4071 /* qprocsoff is done via ill_delete_tail */
4071 4072 ill_delete_tail(ill);
4072 4073 /*
4073 4074 * synchronously wait for arp stream to unbind. After this, we
4074 4075 * cannot get any data packets up from the driver.
4075 4076 */
4076 4077 arp_unbind_complete(ill);
4077 4078 ASSERT(ill->ill_ipst == NULL);
4078 4079
4079 4080 /*
4080 4081 * Walk through all conns and qenable those that have queued data.
4081 4082 * Close synchronization needs this to
4082 4083 * be done to ensure that all upper layers blocked
4083 4084 * due to flow control to the closing device
4084 4085 * get unblocked.
4085 4086 */
4086 4087 ip1dbg(("ip_wsrv: walking\n"));
4087 4088 for (i = 0; i < TX_FANOUT_SIZE; i++) {
4088 4089 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4089 4090 }
4090 4091
4091 4092 /*
4092 4093 * ai can be null if this is an IPv6 ill, or if the IPv4
4093 4094 * stream is being torn down before ARP was plumbed (e.g.,
4094 4095 * /sbin/ifconfig plumbing a stream twice, and encountering
4095 4096 * an error
4096 4097 */
4097 4098 if (ai != NULL) {
4098 4099 ASSERT(!ill->ill_isv6);
4099 4100 mutex_enter(&ai->ai_lock);
4100 4101 ai->ai_ill = NULL;
4101 4102 if (ai->ai_arl == NULL) {
4102 4103 mutex_destroy(&ai->ai_lock);
4103 4104 kmem_free(ai, sizeof (*ai));
4104 4105 } else {
4105 4106 cv_signal(&ai->ai_ill_unplumb_done);
4106 4107 mutex_exit(&ai->ai_lock);
4107 4108 }
4108 4109 }
4109 4110
4110 4111 mutex_enter(&ipst->ips_ip_mi_lock);
4111 4112 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4112 4113 mutex_exit(&ipst->ips_ip_mi_lock);
4113 4114
4114 4115 /*
4115 4116 * credp could be null if the open didn't succeed and ip_modopen
4116 4117 * itself calls ip_close.
4117 4118 */
4118 4119 if (ill->ill_credp != NULL)
4119 4120 crfree(ill->ill_credp);
4120 4121
4121 4122 mutex_destroy(&ill->ill_saved_ire_lock);
4122 4123 mutex_destroy(&ill->ill_lock);
4123 4124 rw_destroy(&ill->ill_mcast_lock);
4124 4125 mutex_destroy(&ill->ill_mcast_serializer);
4125 4126 list_destroy(&ill->ill_nce);
4126 4127
4127 4128 /*
4128 4129 * Now we are done with the module close pieces that
4129 4130 * need the netstack_t.
4130 4131 */
4131 4132 netstack_rele(ipst->ips_netstack);
4132 4133
4133 4134 mi_close_free((IDP)ill);
4134 4135 q->q_ptr = WR(q)->q_ptr = NULL;
4135 4136
4136 4137 ipsq_exit(ipsq);
4137 4138
4138 4139 return (0);
4139 4140 }
4140 4141
4141 4142 /*
4142 4143 * This is called as part of close() for IP, UDP, ICMP, and RTS
4143 4144 * in order to quiesce the conn.
4144 4145 */
4145 4146 void
4146 4147 ip_quiesce_conn(conn_t *connp)
4147 4148 {
4148 4149 boolean_t drain_cleanup_reqd = B_FALSE;
4149 4150 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4150 4151 boolean_t ilg_cleanup_reqd = B_FALSE;
4151 4152 ip_stack_t *ipst;
4152 4153
4153 4154 ASSERT(!IPCL_IS_TCP(connp));
4154 4155 ipst = connp->conn_netstack->netstack_ip;
4155 4156
4156 4157 /*
4157 4158 * Mark the conn as closing, and this conn must not be
4158 4159 * inserted in future into any list. Eg. conn_drain_insert(),
4159 4160 * won't insert this conn into the conn_drain_list.
4160 4161 *
4161 4162 * conn_idl, and conn_ilg cannot get set henceforth.
4162 4163 */
4163 4164 mutex_enter(&connp->conn_lock);
4164 4165 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4165 4166 connp->conn_state_flags |= CONN_CLOSING;
4166 4167 if (connp->conn_idl != NULL)
4167 4168 drain_cleanup_reqd = B_TRUE;
4168 4169 if (connp->conn_oper_pending_ill != NULL)
4169 4170 conn_ioctl_cleanup_reqd = B_TRUE;
4170 4171 if (connp->conn_dhcpinit_ill != NULL) {
4171 4172 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4172 4173 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4173 4174 ill_set_inputfn(connp->conn_dhcpinit_ill);
4174 4175 connp->conn_dhcpinit_ill = NULL;
4175 4176 }
4176 4177 if (connp->conn_ilg != NULL)
4177 4178 ilg_cleanup_reqd = B_TRUE;
4178 4179 mutex_exit(&connp->conn_lock);
4179 4180
4180 4181 if (conn_ioctl_cleanup_reqd)
4181 4182 conn_ioctl_cleanup(connp);
4182 4183
4183 4184 if (is_system_labeled() && connp->conn_anon_port) {
4184 4185 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4185 4186 connp->conn_mlp_type, connp->conn_proto,
4186 4187 ntohs(connp->conn_lport), B_FALSE);
4187 4188 connp->conn_anon_port = 0;
4188 4189 }
4189 4190 connp->conn_mlp_type = mlptSingle;
4190 4191
4191 4192 /*
4192 4193 * Remove this conn from any fanout list it is on.
4193 4194 * and then wait for any threads currently operating
4194 4195 * on this endpoint to finish
4195 4196 */
4196 4197 ipcl_hash_remove(connp);
4197 4198
4198 4199 /*
4199 4200 * Remove this conn from the drain list, and do any other cleanup that
4200 4201 * may be required. (TCP conns are never flow controlled, and
4201 4202 * conn_idl will be NULL.)
4202 4203 */
4203 4204 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4204 4205 idl_t *idl = connp->conn_idl;
4205 4206
4206 4207 mutex_enter(&idl->idl_lock);
4207 4208 conn_drain(connp, B_TRUE);
4208 4209 mutex_exit(&idl->idl_lock);
4209 4210 }
4210 4211
4211 4212 if (connp == ipst->ips_ip_g_mrouter)
4212 4213 (void) ip_mrouter_done(ipst);
4213 4214
4214 4215 if (ilg_cleanup_reqd)
4215 4216 ilg_delete_all(connp);
4216 4217
4217 4218 /*
4218 4219 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4219 4220 * callers from write side can't be there now because close
4220 4221 * is in progress. The only other caller is ipcl_walk
4221 4222 * which checks for the condemned flag.
4222 4223 */
4223 4224 mutex_enter(&connp->conn_lock);
4224 4225 connp->conn_state_flags |= CONN_CONDEMNED;
4225 4226 while (connp->conn_ref != 1)
4226 4227 cv_wait(&connp->conn_cv, &connp->conn_lock);
4227 4228 connp->conn_state_flags |= CONN_QUIESCED;
4228 4229 mutex_exit(&connp->conn_lock);
4229 4230 }
4230 4231
4231 4232 /* ARGSUSED */
4232 4233 int
4233 4234 ip_close(queue_t *q, int flags, cred_t *credp __unused)
4234 4235 {
4235 4236 conn_t *connp;
4236 4237
4237 4238 /*
4238 4239 * Call the appropriate delete routine depending on whether this is
4239 4240 * a module or device.
4240 4241 */
4241 4242 if (WR(q)->q_next != NULL) {
4242 4243 /* This is a module close */
4243 4244 return (ip_modclose((ill_t *)q->q_ptr));
4244 4245 }
4245 4246
4246 4247 connp = q->q_ptr;
4247 4248 ip_quiesce_conn(connp);
4248 4249
4249 4250 qprocsoff(q);
4250 4251
4251 4252 /*
4252 4253 * Now we are truly single threaded on this stream, and can
4253 4254 * delete the things hanging off the connp, and finally the connp.
4254 4255 * We removed this connp from the fanout list, it cannot be
4255 4256 * accessed thru the fanouts, and we already waited for the
4256 4257 * conn_ref to drop to 0. We are already in close, so
4257 4258 * there cannot be any other thread from the top. qprocsoff
4258 4259 * has completed, and service has completed or won't run in
4259 4260 * future.
4260 4261 */
4261 4262 ASSERT(connp->conn_ref == 1);
4262 4263
4263 4264 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4264 4265
4265 4266 connp->conn_ref--;
4266 4267 ipcl_conn_destroy(connp);
4267 4268
4268 4269 q->q_ptr = WR(q)->q_ptr = NULL;
4269 4270 return (0);
4270 4271 }
4271 4272
4272 4273 /*
4273 4274 * Wapper around putnext() so that ip_rts_request can merely use
4274 4275 * conn_recv.
4275 4276 */
4276 4277 /*ARGSUSED2*/
4277 4278 static void
4278 4279 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4279 4280 {
4280 4281 conn_t *connp = (conn_t *)arg1;
4281 4282
4282 4283 putnext(connp->conn_rq, mp);
4283 4284 }
4284 4285
4285 4286 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4286 4287 /* ARGSUSED */
4287 4288 static void
4288 4289 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4289 4290 {
4290 4291 freemsg(mp);
4291 4292 }
4292 4293
4293 4294 /*
4294 4295 * Called when the module is about to be unloaded
4295 4296 */
4296 4297 void
4297 4298 ip_ddi_destroy(void)
4298 4299 {
4299 4300 /* This needs to be called before destroying any transports. */
4300 4301 mutex_enter(&cpu_lock);
4301 4302 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4302 4303 mutex_exit(&cpu_lock);
4303 4304
4304 4305 tnet_fini();
4305 4306
4306 4307 icmp_ddi_g_destroy();
4307 4308 rts_ddi_g_destroy();
4308 4309 udp_ddi_g_destroy();
4309 4310 sctp_ddi_g_destroy();
4310 4311 tcp_ddi_g_destroy();
4311 4312 ilb_ddi_g_destroy();
4312 4313 dce_g_destroy();
4313 4314 ipsec_policy_g_destroy();
4314 4315 ipcl_g_destroy();
4315 4316 ip_net_g_destroy();
4316 4317 ip_ire_g_fini();
4317 4318 inet_minor_destroy(ip_minor_arena_sa);
4318 4319 #if defined(_LP64)
4319 4320 inet_minor_destroy(ip_minor_arena_la);
4320 4321 #endif
4321 4322
4322 4323 #ifdef DEBUG
4323 4324 list_destroy(&ip_thread_list);
4324 4325 rw_destroy(&ip_thread_rwlock);
4325 4326 tsd_destroy(&ip_thread_data);
4326 4327 #endif
4327 4328
4328 4329 netstack_unregister(NS_IP);
4329 4330 }
4330 4331
4331 4332 /*
4332 4333 * First step in cleanup.
4333 4334 */
4334 4335 /* ARGSUSED */
4335 4336 static void
4336 4337 ip_stack_shutdown(netstackid_t stackid, void *arg)
4337 4338 {
4338 4339 ip_stack_t *ipst = (ip_stack_t *)arg;
4339 4340 kt_did_t ktid;
4340 4341
4341 4342 #ifdef NS_DEBUG
4342 4343 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4343 4344 #endif
4344 4345
4345 4346 /*
4346 4347 * Perform cleanup for special interfaces (loopback and IPMP).
4347 4348 */
4348 4349 ip_interface_cleanup(ipst);
4349 4350
4350 4351 /*
4351 4352 * The *_hook_shutdown()s start the process of notifying any
4352 4353 * consumers that things are going away.... nothing is destroyed.
4353 4354 */
4354 4355 ipv4_hook_shutdown(ipst);
4355 4356 ipv6_hook_shutdown(ipst);
4356 4357 arp_hook_shutdown(ipst);
4357 4358
4358 4359 mutex_enter(&ipst->ips_capab_taskq_lock);
4359 4360 ktid = ipst->ips_capab_taskq_thread->t_did;
4360 4361 ipst->ips_capab_taskq_quit = B_TRUE;
4361 4362 cv_signal(&ipst->ips_capab_taskq_cv);
4362 4363 mutex_exit(&ipst->ips_capab_taskq_lock);
4363 4364
4364 4365 /*
4365 4366 * In rare occurrences, particularly on virtual hardware where CPUs can
4366 4367 * be de-scheduled, the thread that we just signaled will not run until
4367 4368 * after we have gotten through parts of ip_stack_fini. If that happens
4368 4369 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4369 4370 * from cv_wait which no longer exists.
4370 4371 */
4371 4372 thread_join(ktid);
4372 4373 }
4373 4374
4374 4375 /*
4375 4376 * Free the IP stack instance.
4376 4377 */
4377 4378 static void
4378 4379 ip_stack_fini(netstackid_t stackid, void *arg)
4379 4380 {
4380 4381 ip_stack_t *ipst = (ip_stack_t *)arg;
4381 4382 int ret;
4382 4383
4383 4384 #ifdef NS_DEBUG
4384 4385 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4385 4386 #endif
4386 4387 /*
4387 4388 * At this point, all of the notifications that the events and
4388 4389 * protocols are going away have been run, meaning that we can
4389 4390 * now set about starting to clean things up.
4390 4391 */
4391 4392 ipobs_fini(ipst);
4392 4393 ipv4_hook_destroy(ipst);
4393 4394 ipv6_hook_destroy(ipst);
4394 4395 arp_hook_destroy(ipst);
4395 4396 ip_net_destroy(ipst);
4396 4397
4397 4398 ipmp_destroy(ipst);
4398 4399
4399 4400 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4400 4401 ipst->ips_ip_mibkp = NULL;
4401 4402 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4402 4403 ipst->ips_icmp_mibkp = NULL;
4403 4404 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4404 4405 ipst->ips_ip_kstat = NULL;
4405 4406 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4406 4407 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4407 4408 ipst->ips_ip6_kstat = NULL;
4408 4409 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4409 4410
4410 4411 kmem_free(ipst->ips_propinfo_tbl,
4411 4412 ip_propinfo_count * sizeof (mod_prop_info_t));
4412 4413 ipst->ips_propinfo_tbl = NULL;
4413 4414
4414 4415 dce_stack_destroy(ipst);
4415 4416 ip_mrouter_stack_destroy(ipst);
4416 4417
4417 4418 /*
4418 4419 * Quiesce all of our timers. Note we set the quiesce flags before we
4419 4420 * call untimeout. The slowtimers may actually kick off another instance
4420 4421 * of the non-slow timers.
4421 4422 */
4422 4423 mutex_enter(&ipst->ips_igmp_timer_lock);
4423 4424 ipst->ips_igmp_timer_quiesce = B_TRUE;
4424 4425 mutex_exit(&ipst->ips_igmp_timer_lock);
4425 4426
4426 4427 mutex_enter(&ipst->ips_mld_timer_lock);
4427 4428 ipst->ips_mld_timer_quiesce = B_TRUE;
4428 4429 mutex_exit(&ipst->ips_mld_timer_lock);
4429 4430
4430 4431 mutex_enter(&ipst->ips_igmp_slowtimeout_lock);
4431 4432 ipst->ips_igmp_slowtimeout_quiesce = B_TRUE;
4432 4433 mutex_exit(&ipst->ips_igmp_slowtimeout_lock);
4433 4434
4434 4435 mutex_enter(&ipst->ips_mld_slowtimeout_lock);
4435 4436 ipst->ips_mld_slowtimeout_quiesce = B_TRUE;
4436 4437 mutex_exit(&ipst->ips_mld_slowtimeout_lock);
4437 4438
4438 4439 ret = untimeout(ipst->ips_igmp_timeout_id);
4439 4440 if (ret == -1) {
4440 4441 ASSERT(ipst->ips_igmp_timeout_id == 0);
4441 4442 } else {
4442 4443 ASSERT(ipst->ips_igmp_timeout_id != 0);
4443 4444 ipst->ips_igmp_timeout_id = 0;
4444 4445 }
4445 4446 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4446 4447 if (ret == -1) {
4447 4448 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4448 4449 } else {
4449 4450 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4450 4451 ipst->ips_igmp_slowtimeout_id = 0;
4451 4452 }
4452 4453 ret = untimeout(ipst->ips_mld_timeout_id);
4453 4454 if (ret == -1) {
4454 4455 ASSERT(ipst->ips_mld_timeout_id == 0);
4455 4456 } else {
4456 4457 ASSERT(ipst->ips_mld_timeout_id != 0);
4457 4458 ipst->ips_mld_timeout_id = 0;
4458 4459 }
4459 4460 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4460 4461 if (ret == -1) {
4461 4462 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4462 4463 } else {
4463 4464 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4464 4465 ipst->ips_mld_slowtimeout_id = 0;
4465 4466 }
4466 4467
4467 4468 ip_ire_fini(ipst);
4468 4469 ip6_asp_free(ipst);
4469 4470 conn_drain_fini(ipst);
4470 4471 ipcl_destroy(ipst);
4471 4472
4472 4473 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4473 4474 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4474 4475 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4475 4476 ipst->ips_ndp4 = NULL;
4476 4477 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4477 4478 ipst->ips_ndp6 = NULL;
4478 4479
4479 4480 if (ipst->ips_loopback_ksp != NULL) {
4480 4481 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4481 4482 ipst->ips_loopback_ksp = NULL;
4482 4483 }
4483 4484
4484 4485 mutex_destroy(&ipst->ips_capab_taskq_lock);
4485 4486 cv_destroy(&ipst->ips_capab_taskq_cv);
4486 4487
4487 4488 rw_destroy(&ipst->ips_srcid_lock);
4488 4489
4489 4490 mutex_destroy(&ipst->ips_ip_mi_lock);
4490 4491 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4491 4492
4492 4493 mutex_destroy(&ipst->ips_igmp_timer_lock);
4493 4494 mutex_destroy(&ipst->ips_mld_timer_lock);
4494 4495 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4495 4496 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4496 4497 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4497 4498 rw_destroy(&ipst->ips_ill_g_lock);
4498 4499
4499 4500 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4500 4501 ipst->ips_phyint_g_list = NULL;
4501 4502 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4502 4503 ipst->ips_ill_g_heads = NULL;
4503 4504
4504 4505 ldi_ident_release(ipst->ips_ldi_ident);
4505 4506 kmem_free(ipst, sizeof (*ipst));
4506 4507 }
4507 4508
4508 4509 /*
4509 4510 * This function is called from the TSD destructor, and is used to debug
4510 4511 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4511 4512 * details.
4512 4513 */
4513 4514 static void
4514 4515 ip_thread_exit(void *phash)
4515 4516 {
4516 4517 th_hash_t *thh = phash;
4517 4518
4518 4519 rw_enter(&ip_thread_rwlock, RW_WRITER);
4519 4520 list_remove(&ip_thread_list, thh);
4520 4521 rw_exit(&ip_thread_rwlock);
4521 4522 mod_hash_destroy_hash(thh->thh_hash);
4522 4523 kmem_free(thh, sizeof (*thh));
4523 4524 }
4524 4525
4525 4526 /*
4526 4527 * Called when the IP kernel module is loaded into the kernel
4527 4528 */
4528 4529 void
4529 4530 ip_ddi_init(void)
4530 4531 {
4531 4532 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4532 4533
4533 4534 /*
4534 4535 * For IP and TCP the minor numbers should start from 2 since we have 4
4535 4536 * initial devices: ip, ip6, tcp, tcp6.
4536 4537 */
4537 4538 /*
4538 4539 * If this is a 64-bit kernel, then create two separate arenas -
4539 4540 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4540 4541 * other for socket apps in the range 2^^18 through 2^^32-1.
4541 4542 */
4542 4543 ip_minor_arena_la = NULL;
4543 4544 ip_minor_arena_sa = NULL;
4544 4545 #if defined(_LP64)
4545 4546 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4546 4547 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4547 4548 cmn_err(CE_PANIC,
4548 4549 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4549 4550 }
4550 4551 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4551 4552 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4552 4553 cmn_err(CE_PANIC,
4553 4554 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4554 4555 }
4555 4556 #else
4556 4557 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4557 4558 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4558 4559 cmn_err(CE_PANIC,
4559 4560 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4560 4561 }
4561 4562 #endif
4562 4563 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4563 4564
4564 4565 ipcl_g_init();
4565 4566 ip_ire_g_init();
4566 4567 ip_net_g_init();
4567 4568
4568 4569 #ifdef DEBUG
4569 4570 tsd_create(&ip_thread_data, ip_thread_exit);
4570 4571 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4571 4572 list_create(&ip_thread_list, sizeof (th_hash_t),
4572 4573 offsetof(th_hash_t, thh_link));
4573 4574 #endif
4574 4575 ipsec_policy_g_init();
4575 4576 tcp_ddi_g_init();
4576 4577 sctp_ddi_g_init();
4577 4578 dce_g_init();
4578 4579
4579 4580 /*
4580 4581 * We want to be informed each time a stack is created or
4581 4582 * destroyed in the kernel, so we can maintain the
4582 4583 * set of udp_stack_t's.
4583 4584 */
4584 4585 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4585 4586 ip_stack_fini);
4586 4587
4587 4588 tnet_init();
4588 4589
4589 4590 udp_ddi_g_init();
4590 4591 rts_ddi_g_init();
4591 4592 icmp_ddi_g_init();
4592 4593 ilb_ddi_g_init();
4593 4594
4594 4595 /* This needs to be called after all transports are initialized. */
4595 4596 mutex_enter(&cpu_lock);
4596 4597 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4597 4598 mutex_exit(&cpu_lock);
4598 4599 }
4599 4600
4600 4601 /*
4601 4602 * Initialize the IP stack instance.
4602 4603 */
4603 4604 static void *
4604 4605 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4605 4606 {
4606 4607 ip_stack_t *ipst;
4607 4608 size_t arrsz;
4608 4609 major_t major;
4609 4610
4610 4611 #ifdef NS_DEBUG
4611 4612 printf("ip_stack_init(stack %d)\n", stackid);
4612 4613 #endif
4613 4614
4614 4615 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4615 4616 ipst->ips_netstack = ns;
4616 4617
4617 4618 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4618 4619 KM_SLEEP);
4619 4620 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4620 4621 KM_SLEEP);
4621 4622 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4622 4623 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4623 4624 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4624 4625 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4625 4626
4626 4627 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4627 4628 ipst->ips_igmp_deferred_next = INFINITY;
4628 4629 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4629 4630 ipst->ips_mld_deferred_next = INFINITY;
4630 4631 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4631 4632 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4632 4633 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4633 4634 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4634 4635 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4635 4636 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4636 4637
4637 4638 ipcl_init(ipst);
4638 4639 ip_ire_init(ipst);
4639 4640 ip6_asp_init(ipst);
4640 4641 ipif_init(ipst);
4641 4642 conn_drain_init(ipst);
4642 4643 ip_mrouter_stack_init(ipst);
4643 4644 dce_stack_init(ipst);
4644 4645
4645 4646 ipst->ips_ip_multirt_log_interval = 1000;
4646 4647
4647 4648 ipst->ips_ill_index = 1;
4648 4649
4649 4650 ipst->ips_saved_ip_forwarding = -1;
4650 4651 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4651 4652
4652 4653 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4653 4654 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4654 4655 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4655 4656
4656 4657 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4657 4658 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4658 4659 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4659 4660 ipst->ips_ip6_kstat =
4660 4661 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4661 4662
4662 4663 ipst->ips_ip_src_id = 1;
4663 4664 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4664 4665
4665 4666 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4666 4667
4667 4668 ip_net_init(ipst, ns);
4668 4669 ipv4_hook_init(ipst);
4669 4670 ipv6_hook_init(ipst);
4670 4671 arp_hook_init(ipst);
4671 4672 ipmp_init(ipst);
4672 4673 ipobs_init(ipst);
4673 4674
4674 4675 /*
4675 4676 * Create the taskq dispatcher thread and initialize related stuff.
4676 4677 */
4677 4678 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4678 4679 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4679 4680 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4680 4681 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4681 4682
4682 4683 major = mod_name_to_major(INET_NAME);
4683 4684 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4684 4685 return (ipst);
4685 4686 }
4686 4687
4687 4688 /*
4688 4689 * Allocate and initialize a DLPI template of the specified length. (May be
4689 4690 * called as writer.)
4690 4691 */
4691 4692 mblk_t *
4692 4693 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4693 4694 {
4694 4695 mblk_t *mp;
4695 4696
4696 4697 mp = allocb(len, BPRI_MED);
4697 4698 if (!mp)
4698 4699 return (NULL);
4699 4700
4700 4701 /*
4701 4702 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4702 4703 * of which we don't seem to use) are sent with M_PCPROTO, and
4703 4704 * that other DLPI are M_PROTO.
4704 4705 */
4705 4706 if (prim == DL_INFO_REQ) {
4706 4707 mp->b_datap->db_type = M_PCPROTO;
4707 4708 } else {
4708 4709 mp->b_datap->db_type = M_PROTO;
4709 4710 }
4710 4711
4711 4712 mp->b_wptr = mp->b_rptr + len;
4712 4713 bzero(mp->b_rptr, len);
4713 4714 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4714 4715 return (mp);
4715 4716 }
4716 4717
4717 4718 /*
4718 4719 * Allocate and initialize a DLPI notification. (May be called as writer.)
4719 4720 */
4720 4721 mblk_t *
4721 4722 ip_dlnotify_alloc(uint_t notification, uint_t data)
4722 4723 {
4723 4724 dl_notify_ind_t *notifyp;
4724 4725 mblk_t *mp;
4725 4726
4726 4727 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4727 4728 return (NULL);
4728 4729
4729 4730 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4730 4731 notifyp->dl_notification = notification;
4731 4732 notifyp->dl_data = data;
4732 4733 return (mp);
4733 4734 }
4734 4735
4735 4736 mblk_t *
4736 4737 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4737 4738 {
4738 4739 dl_notify_ind_t *notifyp;
4739 4740 mblk_t *mp;
4740 4741
4741 4742 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4742 4743 return (NULL);
4743 4744
4744 4745 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4745 4746 notifyp->dl_notification = notification;
4746 4747 notifyp->dl_data1 = data1;
4747 4748 notifyp->dl_data2 = data2;
4748 4749 return (mp);
4749 4750 }
4750 4751
4751 4752 /*
4752 4753 * Debug formatting routine. Returns a character string representation of the
4753 4754 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4754 4755 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4755 4756 *
4756 4757 * Once the ndd table-printing interfaces are removed, this can be changed to
4757 4758 * standard dotted-decimal form.
4758 4759 */
4759 4760 char *
4760 4761 ip_dot_addr(ipaddr_t addr, char *buf)
4761 4762 {
4762 4763 uint8_t *ap = (uint8_t *)&addr;
4763 4764
4764 4765 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4765 4766 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4766 4767 return (buf);
4767 4768 }
4768 4769
4769 4770 /*
4770 4771 * Write the given MAC address as a printable string in the usual colon-
4771 4772 * separated format.
4772 4773 */
4773 4774 const char *
4774 4775 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4775 4776 {
4776 4777 char *bp;
4777 4778
4778 4779 if (alen == 0 || buflen < 4)
4779 4780 return ("?");
4780 4781 bp = buf;
4781 4782 for (;;) {
4782 4783 /*
4783 4784 * If there are more MAC address bytes available, but we won't
4784 4785 * have any room to print them, then add "..." to the string
4785 4786 * instead. See below for the 'magic number' explanation.
4786 4787 */
4787 4788 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4788 4789 (void) strcpy(bp, "...");
4789 4790 break;
4790 4791 }
4791 4792 (void) sprintf(bp, "%02x", *addr++);
4792 4793 bp += 2;
4793 4794 if (--alen == 0)
4794 4795 break;
4795 4796 *bp++ = ':';
4796 4797 buflen -= 3;
4797 4798 /*
4798 4799 * At this point, based on the first 'if' statement above,
4799 4800 * either alen == 1 and buflen >= 3, or alen > 1 and
4800 4801 * buflen >= 4. The first case leaves room for the final "xx"
4801 4802 * number and trailing NUL byte. The second leaves room for at
4802 4803 * least "...". Thus the apparently 'magic' numbers chosen for
4803 4804 * that statement.
4804 4805 */
4805 4806 }
4806 4807 return (buf);
4807 4808 }
4808 4809
4809 4810 /*
4810 4811 * Called when it is conceptually a ULP that would sent the packet
4811 4812 * e.g., port unreachable and protocol unreachable. Check that the packet
4812 4813 * would have passed the IPsec global policy before sending the error.
4813 4814 *
4814 4815 * Send an ICMP error after patching up the packet appropriately.
4815 4816 * Uses ip_drop_input and bumps the appropriate MIB.
4816 4817 */
4817 4818 void
4818 4819 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4819 4820 ip_recv_attr_t *ira)
4820 4821 {
4821 4822 ipha_t *ipha;
4822 4823 boolean_t secure;
4823 4824 ill_t *ill = ira->ira_ill;
4824 4825 ip_stack_t *ipst = ill->ill_ipst;
4825 4826 netstack_t *ns = ipst->ips_netstack;
4826 4827 ipsec_stack_t *ipss = ns->netstack_ipsec;
4827 4828
4828 4829 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4829 4830
4830 4831 /*
4831 4832 * We are generating an icmp error for some inbound packet.
4832 4833 * Called from all ip_fanout_(udp, tcp, proto) functions.
4833 4834 * Before we generate an error, check with global policy
4834 4835 * to see whether this is allowed to enter the system. As
4835 4836 * there is no "conn", we are checking with global policy.
4836 4837 */
4837 4838 ipha = (ipha_t *)mp->b_rptr;
4838 4839 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4839 4840 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4840 4841 if (mp == NULL)
4841 4842 return;
4842 4843 }
4843 4844
4844 4845 /* We never send errors for protocols that we do implement */
4845 4846 if (ira->ira_protocol == IPPROTO_ICMP ||
4846 4847 ira->ira_protocol == IPPROTO_IGMP) {
4847 4848 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4848 4849 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4849 4850 freemsg(mp);
4850 4851 return;
4851 4852 }
4852 4853 /*
4853 4854 * Have to correct checksum since
4854 4855 * the packet might have been
4855 4856 * fragmented and the reassembly code in ip_rput
4856 4857 * does not restore the IP checksum.
4857 4858 */
4858 4859 ipha->ipha_hdr_checksum = 0;
4859 4860 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4860 4861
4861 4862 switch (icmp_type) {
4862 4863 case ICMP_DEST_UNREACHABLE:
4863 4864 switch (icmp_code) {
4864 4865 case ICMP_PROTOCOL_UNREACHABLE:
4865 4866 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4866 4867 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4867 4868 break;
4868 4869 case ICMP_PORT_UNREACHABLE:
4869 4870 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4870 4871 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4871 4872 break;
4872 4873 }
4873 4874
4874 4875 icmp_unreachable(mp, icmp_code, ira);
4875 4876 break;
4876 4877 default:
4877 4878 #ifdef DEBUG
4878 4879 panic("ip_fanout_send_icmp_v4: wrong type");
4879 4880 /*NOTREACHED*/
4880 4881 #else
4881 4882 freemsg(mp);
4882 4883 break;
4883 4884 #endif
4884 4885 }
4885 4886 }
4886 4887
4887 4888 /*
4888 4889 * Used to send an ICMP error message when a packet is received for
4889 4890 * a protocol that is not supported. The mblk passed as argument
4890 4891 * is consumed by this function.
4891 4892 */
4892 4893 void
4893 4894 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4894 4895 {
4895 4896 ipha_t *ipha;
4896 4897
4897 4898 ipha = (ipha_t *)mp->b_rptr;
4898 4899 if (ira->ira_flags & IRAF_IS_IPV4) {
4899 4900 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4900 4901 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4901 4902 ICMP_PROTOCOL_UNREACHABLE, ira);
4902 4903 } else {
4903 4904 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4904 4905 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4905 4906 ICMP6_PARAMPROB_NEXTHEADER, ira);
4906 4907 }
4907 4908 }
4908 4909
4909 4910 /*
4910 4911 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4911 4912 * Handles IPv4 and IPv6.
4912 4913 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4913 4914 * Caller is responsible for dropping references to the conn.
4914 4915 */
4915 4916 void
4916 4917 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4917 4918 ip_recv_attr_t *ira)
4918 4919 {
4919 4920 ill_t *ill = ira->ira_ill;
4920 4921 ip_stack_t *ipst = ill->ill_ipst;
4921 4922 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4922 4923 boolean_t secure;
4923 4924 uint_t protocol = ira->ira_protocol;
4924 4925 iaflags_t iraflags = ira->ira_flags;
4925 4926 queue_t *rq;
4926 4927
4927 4928 secure = iraflags & IRAF_IPSEC_SECURE;
4928 4929
4929 4930 rq = connp->conn_rq;
4930 4931 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4931 4932 switch (protocol) {
4932 4933 case IPPROTO_ICMPV6:
4933 4934 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4934 4935 break;
4935 4936 case IPPROTO_ICMP:
4936 4937 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4937 4938 break;
4938 4939 default:
4939 4940 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4940 4941 break;
4941 4942 }
4942 4943 freemsg(mp);
4943 4944 return;
4944 4945 }
4945 4946
4946 4947 ASSERT(!(IPCL_IS_IPTUN(connp)));
4947 4948
4948 4949 if (((iraflags & IRAF_IS_IPV4) ?
4949 4950 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4950 4951 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4951 4952 secure) {
4952 4953 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4953 4954 ip6h, ira);
4954 4955 if (mp == NULL) {
4955 4956 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4956 4957 /* Note that mp is NULL */
4957 4958 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4958 4959 return;
4959 4960 }
4960 4961 }
4961 4962
4962 4963 if (iraflags & IRAF_ICMP_ERROR) {
4963 4964 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4964 4965 } else {
4965 4966 ill_t *rill = ira->ira_rill;
4966 4967
4967 4968 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4968 4969 ira->ira_ill = ira->ira_rill = NULL;
4969 4970 /* Send it upstream */
4970 4971 (connp->conn_recv)(connp, mp, NULL, ira);
4971 4972 ira->ira_ill = ill;
4972 4973 ira->ira_rill = rill;
4973 4974 }
4974 4975 }
4975 4976
4976 4977 /*
4977 4978 * Handle protocols with which IP is less intimate. There
4978 4979 * can be more than one stream bound to a particular
4979 4980 * protocol. When this is the case, normally each one gets a copy
4980 4981 * of any incoming packets.
4981 4982 *
4982 4983 * IPsec NOTE :
4983 4984 *
4984 4985 * Don't allow a secure packet going up a non-secure connection.
4985 4986 * We don't allow this because
4986 4987 *
4987 4988 * 1) Reply might go out in clear which will be dropped at
4988 4989 * the sending side.
4989 4990 * 2) If the reply goes out in clear it will give the
4990 4991 * adversary enough information for getting the key in
4991 4992 * most of the cases.
4992 4993 *
4993 4994 * Moreover getting a secure packet when we expect clear
4994 4995 * implies that SA's were added without checking for
4995 4996 * policy on both ends. This should not happen once ISAKMP
4996 4997 * is used to negotiate SAs as SAs will be added only after
4997 4998 * verifying the policy.
4998 4999 *
4999 5000 * Zones notes:
5000 5001 * Earlier in ip_input on a system with multiple shared-IP zones we
5001 5002 * duplicate the multicast and broadcast packets and send them up
5002 5003 * with each explicit zoneid that exists on that ill.
5003 5004 * This means that here we can match the zoneid with SO_ALLZONES being special.
5004 5005 */
5005 5006 void
5006 5007 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
5007 5008 {
5008 5009 mblk_t *mp1;
5009 5010 ipaddr_t laddr;
5010 5011 conn_t *connp, *first_connp, *next_connp;
5011 5012 connf_t *connfp;
5012 5013 ill_t *ill = ira->ira_ill;
5013 5014 ip_stack_t *ipst = ill->ill_ipst;
5014 5015
5015 5016 laddr = ipha->ipha_dst;
5016 5017
5017 5018 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
5018 5019 mutex_enter(&connfp->connf_lock);
5019 5020 connp = connfp->connf_head;
5020 5021 for (connp = connfp->connf_head; connp != NULL;
5021 5022 connp = connp->conn_next) {
5022 5023 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5023 5024 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5024 5025 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5025 5026 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
5026 5027 break;
5027 5028 }
5028 5029 }
5029 5030
5030 5031 if (connp == NULL) {
5031 5032 /*
5032 5033 * No one bound to these addresses. Is
5033 5034 * there a client that wants all
5034 5035 * unclaimed datagrams?
5035 5036 */
5036 5037 mutex_exit(&connfp->connf_lock);
5037 5038 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5038 5039 ICMP_PROTOCOL_UNREACHABLE, ira);
5039 5040 return;
5040 5041 }
5041 5042
5042 5043 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5043 5044
5044 5045 CONN_INC_REF(connp);
5045 5046 first_connp = connp;
5046 5047 connp = connp->conn_next;
5047 5048
5048 5049 for (;;) {
5049 5050 while (connp != NULL) {
5050 5051 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5051 5052 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5052 5053 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5053 5054 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5054 5055 ira, connp)))
5055 5056 break;
5056 5057 connp = connp->conn_next;
5057 5058 }
5058 5059
5059 5060 if (connp == NULL) {
5060 5061 /* No more interested clients */
5061 5062 connp = first_connp;
5062 5063 break;
5063 5064 }
5064 5065 if (((mp1 = dupmsg(mp)) == NULL) &&
5065 5066 ((mp1 = copymsg(mp)) == NULL)) {
5066 5067 /* Memory allocation failed */
5067 5068 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5068 5069 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5069 5070 connp = first_connp;
5070 5071 break;
5071 5072 }
5072 5073
5073 5074 CONN_INC_REF(connp);
5074 5075 mutex_exit(&connfp->connf_lock);
5075 5076
5076 5077 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5077 5078 ira);
5078 5079
5079 5080 mutex_enter(&connfp->connf_lock);
5080 5081 /* Follow the next pointer before releasing the conn. */
5081 5082 next_connp = connp->conn_next;
5082 5083 CONN_DEC_REF(connp);
5083 5084 connp = next_connp;
5084 5085 }
5085 5086
5086 5087 /* Last one. Send it upstream. */
5087 5088 mutex_exit(&connfp->connf_lock);
5088 5089
5089 5090 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5090 5091
5091 5092 CONN_DEC_REF(connp);
5092 5093 }
5093 5094
5094 5095 /*
5095 5096 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5096 5097 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
5097 5098 * is not consumed.
5098 5099 *
5099 5100 * One of three things can happen, all of which affect the passed-in mblk:
5100 5101 *
5101 5102 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
5102 5103 *
5103 5104 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5104 5105 * ESP packet, and is passed along to ESP for consumption. Return NULL.
5105 5106 *
5106 5107 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
5107 5108 */
5108 5109 mblk_t *
5109 5110 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5110 5111 {
5111 5112 int shift, plen, iph_len;
5112 5113 ipha_t *ipha;
5113 5114 udpha_t *udpha;
5114 5115 uint32_t *spi;
5115 5116 uint32_t esp_ports;
5116 5117 uint8_t *orptr;
5117 5118 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
5118 5119 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5119 5120
5120 5121 ipha = (ipha_t *)mp->b_rptr;
5121 5122 iph_len = ira->ira_ip_hdr_length;
5122 5123 plen = ira->ira_pktlen;
5123 5124
5124 5125 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5125 5126 /*
5126 5127 * Most likely a keepalive for the benefit of an intervening
5127 5128 * NAT. These aren't for us, per se, so drop it.
5128 5129 *
5129 5130 * RFC 3947/8 doesn't say for sure what to do for 2-3
5130 5131 * byte packets (keepalives are 1-byte), but we'll drop them
5131 5132 * also.
5132 5133 */
5133 5134 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5134 5135 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5135 5136 return (NULL);
5136 5137 }
5137 5138
5138 5139 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5139 5140 /* might as well pull it all up - it might be ESP. */
5140 5141 if (!pullupmsg(mp, -1)) {
5141 5142 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5142 5143 DROPPER(ipss, ipds_esp_nomem),
5143 5144 &ipss->ipsec_dropper);
5144 5145 return (NULL);
5145 5146 }
5146 5147
5147 5148 ipha = (ipha_t *)mp->b_rptr;
5148 5149 }
5149 5150 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5150 5151 if (*spi == 0) {
5151 5152 /* UDP packet - remove 0-spi. */
5152 5153 shift = sizeof (uint32_t);
5153 5154 } else {
5154 5155 /* ESP-in-UDP packet - reduce to ESP. */
5155 5156 ipha->ipha_protocol = IPPROTO_ESP;
5156 5157 shift = sizeof (udpha_t);
5157 5158 }
5158 5159
5159 5160 /* Fix IP header */
5160 5161 ira->ira_pktlen = (plen - shift);
5161 5162 ipha->ipha_length = htons(ira->ira_pktlen);
5162 5163 ipha->ipha_hdr_checksum = 0;
5163 5164
5164 5165 orptr = mp->b_rptr;
5165 5166 mp->b_rptr += shift;
5166 5167
5167 5168 udpha = (udpha_t *)(orptr + iph_len);
5168 5169 if (*spi == 0) {
5169 5170 ASSERT((uint8_t *)ipha == orptr);
5170 5171 udpha->uha_length = htons(plen - shift - iph_len);
5171 5172 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5172 5173 esp_ports = 0;
5173 5174 } else {
5174 5175 esp_ports = *((uint32_t *)udpha);
5175 5176 ASSERT(esp_ports != 0);
5176 5177 }
5177 5178 ovbcopy(orptr, orptr + shift, iph_len);
5178 5179 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5179 5180 ipha = (ipha_t *)(orptr + shift);
5180 5181
5181 5182 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5182 5183 ira->ira_esp_udp_ports = esp_ports;
5183 5184 ip_fanout_v4(mp, ipha, ira);
5184 5185 return (NULL);
5185 5186 }
5186 5187 return (mp);
5187 5188 }
5188 5189
5189 5190 /*
5190 5191 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5191 5192 * Handles IPv4 and IPv6.
5192 5193 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5193 5194 * Caller is responsible for dropping references to the conn.
5194 5195 */
5195 5196 void
5196 5197 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5197 5198 ip_recv_attr_t *ira)
5198 5199 {
5199 5200 ill_t *ill = ira->ira_ill;
5200 5201 ip_stack_t *ipst = ill->ill_ipst;
5201 5202 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5202 5203 boolean_t secure;
5203 5204 iaflags_t iraflags = ira->ira_flags;
5204 5205
5205 5206 secure = iraflags & IRAF_IPSEC_SECURE;
5206 5207
5207 5208 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5208 5209 !canputnext(connp->conn_rq)) {
5209 5210 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5210 5211 freemsg(mp);
5211 5212 return;
5212 5213 }
5213 5214
5214 5215 if (((iraflags & IRAF_IS_IPV4) ?
5215 5216 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5216 5217 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5217 5218 secure) {
5218 5219 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5219 5220 ip6h, ira);
5220 5221 if (mp == NULL) {
5221 5222 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5222 5223 /* Note that mp is NULL */
5223 5224 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5224 5225 return;
5225 5226 }
5226 5227 }
5227 5228
5228 5229 /*
5229 5230 * Since this code is not used for UDP unicast we don't need a NAT_T
5230 5231 * check. Only ip_fanout_v4 has that check.
5231 5232 */
5232 5233 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5233 5234 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5234 5235 } else {
5235 5236 ill_t *rill = ira->ira_rill;
5236 5237
5237 5238 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5238 5239 ira->ira_ill = ira->ira_rill = NULL;
5239 5240 /* Send it upstream */
5240 5241 (connp->conn_recv)(connp, mp, NULL, ira);
5241 5242 ira->ira_ill = ill;
5242 5243 ira->ira_rill = rill;
5243 5244 }
5244 5245 }
5245 5246
5246 5247 /*
5247 5248 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5248 5249 * (Unicast fanout is handled in ip_input_v4.)
5249 5250 *
5250 5251 * If SO_REUSEADDR is set all multicast and broadcast packets
5251 5252 * will be delivered to all conns bound to the same port.
5252 5253 *
5253 5254 * If there is at least one matching AF_INET receiver, then we will
5254 5255 * ignore any AF_INET6 receivers.
5255 5256 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5256 5257 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5257 5258 * packets.
5258 5259 *
5259 5260 * Zones notes:
5260 5261 * Earlier in ip_input on a system with multiple shared-IP zones we
5261 5262 * duplicate the multicast and broadcast packets and send them up
5262 5263 * with each explicit zoneid that exists on that ill.
5263 5264 * This means that here we can match the zoneid with SO_ALLZONES being special.
5264 5265 */
5265 5266 void
5266 5267 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5267 5268 ip_recv_attr_t *ira)
5268 5269 {
5269 5270 ipaddr_t laddr;
5270 5271 in6_addr_t v6faddr;
5271 5272 conn_t *connp;
5272 5273 connf_t *connfp;
5273 5274 ipaddr_t faddr;
5274 5275 ill_t *ill = ira->ira_ill;
5275 5276 ip_stack_t *ipst = ill->ill_ipst;
5276 5277
5277 5278 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5278 5279
5279 5280 laddr = ipha->ipha_dst;
5280 5281 faddr = ipha->ipha_src;
5281 5282
5282 5283 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5283 5284 mutex_enter(&connfp->connf_lock);
5284 5285 connp = connfp->connf_head;
5285 5286
5286 5287 /*
5287 5288 * If SO_REUSEADDR has been set on the first we send the
5288 5289 * packet to all clients that have joined the group and
5289 5290 * match the port.
5290 5291 */
5291 5292 while (connp != NULL) {
5292 5293 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5293 5294 conn_wantpacket(connp, ira, ipha) &&
5294 5295 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5295 5296 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5296 5297 break;
5297 5298 connp = connp->conn_next;
5298 5299 }
5299 5300
5300 5301 if (connp == NULL)
5301 5302 goto notfound;
5302 5303
5303 5304 CONN_INC_REF(connp);
5304 5305
5305 5306 if (connp->conn_reuseaddr) {
5306 5307 conn_t *first_connp = connp;
5307 5308 conn_t *next_connp;
5308 5309 mblk_t *mp1;
5309 5310
5310 5311 connp = connp->conn_next;
5311 5312 for (;;) {
5312 5313 while (connp != NULL) {
5313 5314 if (IPCL_UDP_MATCH(connp, lport, laddr,
5314 5315 fport, faddr) &&
5315 5316 conn_wantpacket(connp, ira, ipha) &&
5316 5317 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5317 5318 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5318 5319 ira, connp)))
5319 5320 break;
5320 5321 connp = connp->conn_next;
5321 5322 }
5322 5323 if (connp == NULL) {
5323 5324 /* No more interested clients */
5324 5325 connp = first_connp;
5325 5326 break;
5326 5327 }
5327 5328 if (((mp1 = dupmsg(mp)) == NULL) &&
5328 5329 ((mp1 = copymsg(mp)) == NULL)) {
5329 5330 /* Memory allocation failed */
5330 5331 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5331 5332 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5332 5333 connp = first_connp;
5333 5334 break;
5334 5335 }
5335 5336 CONN_INC_REF(connp);
5336 5337 mutex_exit(&connfp->connf_lock);
5337 5338
5338 5339 IP_STAT(ipst, ip_udp_fanmb);
5339 5340 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5340 5341 NULL, ira);
5341 5342 mutex_enter(&connfp->connf_lock);
5342 5343 /* Follow the next pointer before releasing the conn */
5343 5344 next_connp = connp->conn_next;
5344 5345 CONN_DEC_REF(connp);
5345 5346 connp = next_connp;
5346 5347 }
5347 5348 }
5348 5349
5349 5350 /* Last one. Send it upstream. */
5350 5351 mutex_exit(&connfp->connf_lock);
5351 5352 IP_STAT(ipst, ip_udp_fanmb);
5352 5353 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5353 5354 CONN_DEC_REF(connp);
5354 5355 return;
5355 5356
5356 5357 notfound:
5357 5358 mutex_exit(&connfp->connf_lock);
5358 5359 /*
5359 5360 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5360 5361 * have already been matched above, since they live in the IPv4
5361 5362 * fanout tables. This implies we only need to
5362 5363 * check for IPv6 in6addr_any endpoints here.
5363 5364 * Thus we compare using ipv6_all_zeros instead of the destination
5364 5365 * address, except for the multicast group membership lookup which
5365 5366 * uses the IPv4 destination.
5366 5367 */
5367 5368 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5368 5369 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5369 5370 mutex_enter(&connfp->connf_lock);
5370 5371 connp = connfp->connf_head;
5371 5372 /*
5372 5373 * IPv4 multicast packet being delivered to an AF_INET6
5373 5374 * in6addr_any endpoint.
5374 5375 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5375 5376 * and not conn_wantpacket_v6() since any multicast membership is
5376 5377 * for an IPv4-mapped multicast address.
5377 5378 */
5378 5379 while (connp != NULL) {
5379 5380 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5380 5381 fport, v6faddr) &&
5381 5382 conn_wantpacket(connp, ira, ipha) &&
5382 5383 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5383 5384 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5384 5385 break;
5385 5386 connp = connp->conn_next;
5386 5387 }
5387 5388
5388 5389 if (connp == NULL) {
5389 5390 /*
5390 5391 * No one bound to this port. Is
5391 5392 * there a client that wants all
5392 5393 * unclaimed datagrams?
5393 5394 */
5394 5395 mutex_exit(&connfp->connf_lock);
5395 5396
5396 5397 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5397 5398 NULL) {
5398 5399 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5399 5400 ip_fanout_proto_v4(mp, ipha, ira);
5400 5401 } else {
5401 5402 /*
5402 5403 * We used to attempt to send an icmp error here, but
5403 5404 * since this is known to be a multicast packet
5404 5405 * and we don't send icmp errors in response to
5405 5406 * multicast, just drop the packet and give up sooner.
5406 5407 */
5407 5408 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5408 5409 freemsg(mp);
5409 5410 }
5410 5411 return;
5411 5412 }
5412 5413 CONN_INC_REF(connp);
5413 5414 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5414 5415
5415 5416 /*
5416 5417 * If SO_REUSEADDR has been set on the first we send the
5417 5418 * packet to all clients that have joined the group and
5418 5419 * match the port.
5419 5420 */
5420 5421 if (connp->conn_reuseaddr) {
5421 5422 conn_t *first_connp = connp;
5422 5423 conn_t *next_connp;
5423 5424 mblk_t *mp1;
5424 5425
5425 5426 connp = connp->conn_next;
5426 5427 for (;;) {
5427 5428 while (connp != NULL) {
5428 5429 if (IPCL_UDP_MATCH_V6(connp, lport,
5429 5430 ipv6_all_zeros, fport, v6faddr) &&
5430 5431 conn_wantpacket(connp, ira, ipha) &&
5431 5432 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5432 5433 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5433 5434 ira, connp)))
5434 5435 break;
5435 5436 connp = connp->conn_next;
5436 5437 }
5437 5438 if (connp == NULL) {
5438 5439 /* No more interested clients */
5439 5440 connp = first_connp;
5440 5441 break;
5441 5442 }
5442 5443 if (((mp1 = dupmsg(mp)) == NULL) &&
5443 5444 ((mp1 = copymsg(mp)) == NULL)) {
5444 5445 /* Memory allocation failed */
5445 5446 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5446 5447 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5447 5448 connp = first_connp;
5448 5449 break;
5449 5450 }
5450 5451 CONN_INC_REF(connp);
5451 5452 mutex_exit(&connfp->connf_lock);
5452 5453
5453 5454 IP_STAT(ipst, ip_udp_fanmb);
5454 5455 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5455 5456 NULL, ira);
5456 5457 mutex_enter(&connfp->connf_lock);
5457 5458 /* Follow the next pointer before releasing the conn */
5458 5459 next_connp = connp->conn_next;
5459 5460 CONN_DEC_REF(connp);
5460 5461 connp = next_connp;
5461 5462 }
5462 5463 }
5463 5464
5464 5465 /* Last one. Send it upstream. */
5465 5466 mutex_exit(&connfp->connf_lock);
5466 5467 IP_STAT(ipst, ip_udp_fanmb);
5467 5468 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5468 5469 CONN_DEC_REF(connp);
5469 5470 }
5470 5471
5471 5472 /*
5472 5473 * Split an incoming packet's IPv4 options into the label and the other options.
5473 5474 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5474 5475 * clearing out any leftover label or options.
5475 5476 * Otherwise it just makes ipp point into the packet.
5476 5477 *
5477 5478 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5478 5479 */
5479 5480 int
5480 5481 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5481 5482 {
5482 5483 uchar_t *opt;
5483 5484 uint32_t totallen;
5484 5485 uint32_t optval;
5485 5486 uint32_t optlen;
5486 5487
5487 5488 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5488 5489 ipp->ipp_hoplimit = ipha->ipha_ttl;
5489 5490 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5490 5491 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5491 5492
5492 5493 /*
5493 5494 * Get length (in 4 byte octets) of IP header options.
5494 5495 */
5495 5496 totallen = ipha->ipha_version_and_hdr_length -
5496 5497 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5497 5498
5498 5499 if (totallen == 0) {
5499 5500 if (!allocate)
5500 5501 return (0);
5501 5502
5502 5503 /* Clear out anything from a previous packet */
5503 5504 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5504 5505 kmem_free(ipp->ipp_ipv4_options,
5505 5506 ipp->ipp_ipv4_options_len);
5506 5507 ipp->ipp_ipv4_options = NULL;
5507 5508 ipp->ipp_ipv4_options_len = 0;
5508 5509 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5509 5510 }
5510 5511 if (ipp->ipp_fields & IPPF_LABEL_V4) {
5511 5512 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5512 5513 ipp->ipp_label_v4 = NULL;
5513 5514 ipp->ipp_label_len_v4 = 0;
5514 5515 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5515 5516 }
5516 5517 return (0);
5517 5518 }
5518 5519
5519 5520 totallen <<= 2;
5520 5521 opt = (uchar_t *)&ipha[1];
5521 5522 if (!is_system_labeled()) {
5522 5523
5523 5524 copyall:
5524 5525 if (!allocate) {
5525 5526 if (totallen != 0) {
5526 5527 ipp->ipp_ipv4_options = opt;
5527 5528 ipp->ipp_ipv4_options_len = totallen;
5528 5529 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5529 5530 }
5530 5531 return (0);
5531 5532 }
5532 5533 /* Just copy all of options */
5533 5534 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5534 5535 if (totallen == ipp->ipp_ipv4_options_len) {
5535 5536 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5536 5537 return (0);
5537 5538 }
5538 5539 kmem_free(ipp->ipp_ipv4_options,
5539 5540 ipp->ipp_ipv4_options_len);
5540 5541 ipp->ipp_ipv4_options = NULL;
5541 5542 ipp->ipp_ipv4_options_len = 0;
5542 5543 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5543 5544 }
5544 5545 if (totallen == 0)
5545 5546 return (0);
5546 5547
5547 5548 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5548 5549 if (ipp->ipp_ipv4_options == NULL)
5549 5550 return (ENOMEM);
5550 5551 ipp->ipp_ipv4_options_len = totallen;
5551 5552 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5552 5553 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5553 5554 return (0);
5554 5555 }
5555 5556
5556 5557 if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5557 5558 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5558 5559 ipp->ipp_label_v4 = NULL;
5559 5560 ipp->ipp_label_len_v4 = 0;
5560 5561 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5561 5562 }
5562 5563
5563 5564 /*
5564 5565 * Search for CIPSO option.
5565 5566 * We assume CIPSO is first in options if it is present.
5566 5567 * If it isn't, then ipp_opt_ipv4_options will not include the options
5567 5568 * prior to the CIPSO option.
5568 5569 */
5569 5570 while (totallen != 0) {
5570 5571 switch (optval = opt[IPOPT_OPTVAL]) {
5571 5572 case IPOPT_EOL:
5572 5573 return (0);
5573 5574 case IPOPT_NOP:
5574 5575 optlen = 1;
5575 5576 break;
5576 5577 default:
5577 5578 if (totallen <= IPOPT_OLEN)
5578 5579 return (EINVAL);
5579 5580 optlen = opt[IPOPT_OLEN];
5580 5581 if (optlen < 2)
5581 5582 return (EINVAL);
5582 5583 }
5583 5584 if (optlen > totallen)
5584 5585 return (EINVAL);
5585 5586
5586 5587 switch (optval) {
5587 5588 case IPOPT_COMSEC:
5588 5589 if (!allocate) {
5589 5590 ipp->ipp_label_v4 = opt;
5590 5591 ipp->ipp_label_len_v4 = optlen;
5591 5592 ipp->ipp_fields |= IPPF_LABEL_V4;
5592 5593 } else {
5593 5594 ipp->ipp_label_v4 = kmem_alloc(optlen,
5594 5595 KM_NOSLEEP);
5595 5596 if (ipp->ipp_label_v4 == NULL)
5596 5597 return (ENOMEM);
5597 5598 ipp->ipp_label_len_v4 = optlen;
5598 5599 ipp->ipp_fields |= IPPF_LABEL_V4;
5599 5600 bcopy(opt, ipp->ipp_label_v4, optlen);
5600 5601 }
5601 5602 totallen -= optlen;
5602 5603 opt += optlen;
5603 5604
5604 5605 /* Skip padding bytes until we get to a multiple of 4 */
5605 5606 while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5606 5607 totallen--;
5607 5608 opt++;
5608 5609 }
5609 5610 /* Remaining as ipp_ipv4_options */
5610 5611 goto copyall;
5611 5612 }
5612 5613 totallen -= optlen;
5613 5614 opt += optlen;
5614 5615 }
5615 5616 /* No CIPSO found; return everything as ipp_ipv4_options */
5616 5617 totallen = ipha->ipha_version_and_hdr_length -
5617 5618 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5618 5619 totallen <<= 2;
5619 5620 opt = (uchar_t *)&ipha[1];
5620 5621 goto copyall;
5621 5622 }
5622 5623
5623 5624 /*
5624 5625 * Efficient versions of lookup for an IRE when we only
5625 5626 * match the address.
5626 5627 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5627 5628 * Does not handle multicast addresses.
5628 5629 */
5629 5630 uint_t
5630 5631 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5631 5632 {
5632 5633 ire_t *ire;
5633 5634 uint_t result;
5634 5635
5635 5636 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5636 5637 ASSERT(ire != NULL);
5637 5638 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5638 5639 result = IRE_NOROUTE;
5639 5640 else
5640 5641 result = ire->ire_type;
5641 5642 ire_refrele(ire);
5642 5643 return (result);
5643 5644 }
5644 5645
5645 5646 /*
5646 5647 * Efficient versions of lookup for an IRE when we only
5647 5648 * match the address.
5648 5649 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5649 5650 * Does not handle multicast addresses.
5650 5651 */
5651 5652 uint_t
5652 5653 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5653 5654 {
5654 5655 ire_t *ire;
5655 5656 uint_t result;
5656 5657
5657 5658 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5658 5659 ASSERT(ire != NULL);
5659 5660 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5660 5661 result = IRE_NOROUTE;
5661 5662 else
5662 5663 result = ire->ire_type;
5663 5664 ire_refrele(ire);
5664 5665 return (result);
5665 5666 }
5666 5667
5667 5668 /*
5668 5669 * Nobody should be sending
5669 5670 * packets up this stream
5670 5671 */
5671 5672 static int
5672 5673 ip_lrput(queue_t *q, mblk_t *mp)
5673 5674 {
5674 5675 switch (mp->b_datap->db_type) {
5675 5676 case M_FLUSH:
5676 5677 /* Turn around */
5677 5678 if (*mp->b_rptr & FLUSHW) {
5678 5679 *mp->b_rptr &= ~FLUSHR;
5679 5680 qreply(q, mp);
5680 5681 return (0);
5681 5682 }
5682 5683 break;
5683 5684 }
5684 5685 freemsg(mp);
5685 5686 return (0);
5686 5687 }
5687 5688
5688 5689 /* Nobody should be sending packets down this stream */
5689 5690 /* ARGSUSED */
5690 5691 int
5691 5692 ip_lwput(queue_t *q, mblk_t *mp)
5692 5693 {
5693 5694 freemsg(mp);
5694 5695 return (0);
5695 5696 }
5696 5697
5697 5698 /*
5698 5699 * Move the first hop in any source route to ipha_dst and remove that part of
5699 5700 * the source route. Called by other protocols. Errors in option formatting
5700 5701 * are ignored - will be handled by ip_output_options. Return the final
5701 5702 * destination (either ipha_dst or the last entry in a source route.)
5702 5703 */
5703 5704 ipaddr_t
5704 5705 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5705 5706 {
5706 5707 ipoptp_t opts;
5707 5708 uchar_t *opt;
5708 5709 uint8_t optval;
5709 5710 uint8_t optlen;
5710 5711 ipaddr_t dst;
5711 5712 int i;
5712 5713 ip_stack_t *ipst = ns->netstack_ip;
5713 5714
5714 5715 ip2dbg(("ip_massage_options\n"));
5715 5716 dst = ipha->ipha_dst;
5716 5717 for (optval = ipoptp_first(&opts, ipha);
5717 5718 optval != IPOPT_EOL;
5718 5719 optval = ipoptp_next(&opts)) {
5719 5720 opt = opts.ipoptp_cur;
5720 5721 switch (optval) {
5721 5722 uint8_t off;
5722 5723 case IPOPT_SSRR:
5723 5724 case IPOPT_LSRR:
5724 5725 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5725 5726 ip1dbg(("ip_massage_options: bad src route\n"));
5726 5727 break;
5727 5728 }
5728 5729 optlen = opts.ipoptp_len;
5729 5730 off = opt[IPOPT_OFFSET];
5730 5731 off--;
5731 5732 redo_srr:
5732 5733 if (optlen < IP_ADDR_LEN ||
5733 5734 off > optlen - IP_ADDR_LEN) {
5734 5735 /* End of source route */
5735 5736 ip1dbg(("ip_massage_options: end of SR\n"));
5736 5737 break;
5737 5738 }
5738 5739 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5739 5740 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5740 5741 ntohl(dst)));
5741 5742 /*
5742 5743 * Check if our address is present more than
5743 5744 * once as consecutive hops in source route.
5744 5745 * XXX verify per-interface ip_forwarding
5745 5746 * for source route?
5746 5747 */
5747 5748 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5748 5749 off += IP_ADDR_LEN;
5749 5750 goto redo_srr;
5750 5751 }
5751 5752 if (dst == htonl(INADDR_LOOPBACK)) {
5752 5753 ip1dbg(("ip_massage_options: loopback addr in "
5753 5754 "source route!\n"));
5754 5755 break;
5755 5756 }
5756 5757 /*
5757 5758 * Update ipha_dst to be the first hop and remove the
5758 5759 * first hop from the source route (by overwriting
5759 5760 * part of the option with NOP options).
5760 5761 */
5761 5762 ipha->ipha_dst = dst;
5762 5763 /* Put the last entry in dst */
5763 5764 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5764 5765 3;
5765 5766 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5766 5767
5767 5768 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5768 5769 ntohl(dst)));
5769 5770 /* Move down and overwrite */
5770 5771 opt[IP_ADDR_LEN] = opt[0];
5771 5772 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5772 5773 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5773 5774 for (i = 0; i < IP_ADDR_LEN; i++)
5774 5775 opt[i] = IPOPT_NOP;
5775 5776 break;
5776 5777 }
5777 5778 }
5778 5779 return (dst);
5779 5780 }
5780 5781
5781 5782 /*
5782 5783 * Return the network mask
5783 5784 * associated with the specified address.
5784 5785 */
5785 5786 ipaddr_t
5786 5787 ip_net_mask(ipaddr_t addr)
5787 5788 {
5788 5789 uchar_t *up = (uchar_t *)&addr;
5789 5790 ipaddr_t mask = 0;
5790 5791 uchar_t *maskp = (uchar_t *)&mask;
5791 5792
5792 5793 #if defined(__i386) || defined(__amd64)
5793 5794 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5794 5795 #endif
5795 5796 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5796 5797 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5797 5798 #endif
5798 5799 if (CLASSD(addr)) {
5799 5800 maskp[0] = 0xF0;
5800 5801 return (mask);
5801 5802 }
5802 5803
5803 5804 /* We assume Class E default netmask to be 32 */
5804 5805 if (CLASSE(addr))
5805 5806 return (0xffffffffU);
5806 5807
5807 5808 if (addr == 0)
5808 5809 return (0);
5809 5810 maskp[0] = 0xFF;
5810 5811 if ((up[0] & 0x80) == 0)
5811 5812 return (mask);
5812 5813
5813 5814 maskp[1] = 0xFF;
5814 5815 if ((up[0] & 0xC0) == 0x80)
5815 5816 return (mask);
5816 5817
5817 5818 maskp[2] = 0xFF;
5818 5819 if ((up[0] & 0xE0) == 0xC0)
5819 5820 return (mask);
5820 5821
5821 5822 /* Otherwise return no mask */
5822 5823 return ((ipaddr_t)0);
5823 5824 }
5824 5825
5825 5826 /* Name/Value Table Lookup Routine */
5826 5827 char *
5827 5828 ip_nv_lookup(nv_t *nv, int value)
5828 5829 {
5829 5830 if (!nv)
5830 5831 return (NULL);
5831 5832 for (; nv->nv_name; nv++) {
5832 5833 if (nv->nv_value == value)
5833 5834 return (nv->nv_name);
5834 5835 }
5835 5836 return ("unknown");
5836 5837 }
5837 5838
5838 5839 static int
5839 5840 ip_wait_for_info_ack(ill_t *ill)
5840 5841 {
5841 5842 int err;
5842 5843
5843 5844 mutex_enter(&ill->ill_lock);
5844 5845 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5845 5846 /*
5846 5847 * Return value of 0 indicates a pending signal.
5847 5848 */
5848 5849 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5849 5850 if (err == 0) {
5850 5851 mutex_exit(&ill->ill_lock);
5851 5852 return (EINTR);
5852 5853 }
5853 5854 }
5854 5855 mutex_exit(&ill->ill_lock);
5855 5856 /*
5856 5857 * ip_rput_other could have set an error in ill_error on
5857 5858 * receipt of M_ERROR.
5858 5859 */
5859 5860 return (ill->ill_error);
5860 5861 }
5861 5862
5862 5863 /*
5863 5864 * This is a module open, i.e. this is a control stream for access
5864 5865 * to a DLPI device. We allocate an ill_t as the instance data in
5865 5866 * this case.
5866 5867 */
5867 5868 static int
5868 5869 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5869 5870 {
5870 5871 ill_t *ill;
5871 5872 int err;
5872 5873 zoneid_t zoneid;
5873 5874 netstack_t *ns;
5874 5875 ip_stack_t *ipst;
5875 5876
5876 5877 /*
5877 5878 * Prevent unprivileged processes from pushing IP so that
5878 5879 * they can't send raw IP.
5879 5880 */
5880 5881 if (secpolicy_net_rawaccess(credp) != 0)
5881 5882 return (EPERM);
5882 5883
5883 5884 ns = netstack_find_by_cred(credp);
5884 5885 ASSERT(ns != NULL);
5885 5886 ipst = ns->netstack_ip;
5886 5887 ASSERT(ipst != NULL);
5887 5888
5888 5889 /*
5889 5890 * For exclusive stacks we set the zoneid to zero
5890 5891 * to make IP operate as if in the global zone.
5891 5892 */
5892 5893 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5893 5894 zoneid = GLOBAL_ZONEID;
5894 5895 else
5895 5896 zoneid = crgetzoneid(credp);
5896 5897
5897 5898 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5898 5899 q->q_ptr = WR(q)->q_ptr = ill;
5899 5900 ill->ill_ipst = ipst;
5900 5901 ill->ill_zoneid = zoneid;
5901 5902
5902 5903 /*
5903 5904 * ill_init initializes the ill fields and then sends down
5904 5905 * down a DL_INFO_REQ after calling qprocson.
5905 5906 */
5906 5907 err = ill_init(q, ill);
5907 5908
5908 5909 if (err != 0) {
5909 5910 mi_free(ill);
5910 5911 netstack_rele(ipst->ips_netstack);
5911 5912 q->q_ptr = NULL;
5912 5913 WR(q)->q_ptr = NULL;
5913 5914 return (err);
5914 5915 }
5915 5916
5916 5917 /*
5917 5918 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5918 5919 *
5919 5920 * ill_init initializes the ipsq marking this thread as
5920 5921 * writer
5921 5922 */
5922 5923 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5923 5924 err = ip_wait_for_info_ack(ill);
5924 5925 if (err == 0)
5925 5926 ill->ill_credp = credp;
5926 5927 else
5927 5928 goto fail;
5928 5929
5929 5930 crhold(credp);
5930 5931
5931 5932 mutex_enter(&ipst->ips_ip_mi_lock);
5932 5933 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5933 5934 sflag, credp);
5934 5935 mutex_exit(&ipst->ips_ip_mi_lock);
5935 5936 fail:
5936 5937 if (err) {
5937 5938 (void) ip_close(q, 0, credp);
5938 5939 return (err);
5939 5940 }
5940 5941 return (0);
5941 5942 }
5942 5943
5943 5944 /* For /dev/ip aka AF_INET open */
5944 5945 int
5945 5946 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5946 5947 {
5947 5948 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5948 5949 }
5949 5950
5950 5951 /* For /dev/ip6 aka AF_INET6 open */
5951 5952 int
5952 5953 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5953 5954 {
5954 5955 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5955 5956 }
5956 5957
5957 5958 /* IP open routine. */
5958 5959 int
5959 5960 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5960 5961 boolean_t isv6)
5961 5962 {
5962 5963 conn_t *connp;
5963 5964 major_t maj;
5964 5965 zoneid_t zoneid;
5965 5966 netstack_t *ns;
5966 5967 ip_stack_t *ipst;
5967 5968
5968 5969 /* Allow reopen. */
5969 5970 if (q->q_ptr != NULL)
5970 5971 return (0);
5971 5972
5972 5973 if (sflag & MODOPEN) {
5973 5974 /* This is a module open */
5974 5975 return (ip_modopen(q, devp, flag, sflag, credp));
5975 5976 }
5976 5977
5977 5978 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5978 5979 /*
5979 5980 * Non streams based socket looking for a stream
5980 5981 * to access IP
5981 5982 */
5982 5983 return (ip_helper_stream_setup(q, devp, flag, sflag,
5983 5984 credp, isv6));
5984 5985 }
5985 5986
5986 5987 ns = netstack_find_by_cred(credp);
5987 5988 ASSERT(ns != NULL);
5988 5989 ipst = ns->netstack_ip;
5989 5990 ASSERT(ipst != NULL);
5990 5991
5991 5992 /*
5992 5993 * For exclusive stacks we set the zoneid to zero
5993 5994 * to make IP operate as if in the global zone.
5994 5995 */
5995 5996 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5996 5997 zoneid = GLOBAL_ZONEID;
5997 5998 else
5998 5999 zoneid = crgetzoneid(credp);
5999 6000
6000 6001 /*
6001 6002 * We are opening as a device. This is an IP client stream, and we
6002 6003 * allocate an conn_t as the instance data.
6003 6004 */
6004 6005 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
6005 6006
6006 6007 /*
6007 6008 * ipcl_conn_create did a netstack_hold. Undo the hold that was
6008 6009 * done by netstack_find_by_cred()
6009 6010 */
6010 6011 netstack_rele(ipst->ips_netstack);
6011 6012
6012 6013 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
6013 6014 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
6014 6015 connp->conn_ixa->ixa_zoneid = zoneid;
6015 6016 connp->conn_zoneid = zoneid;
6016 6017
6017 6018 connp->conn_rq = q;
6018 6019 q->q_ptr = WR(q)->q_ptr = connp;
6019 6020
6020 6021 /* Minor tells us which /dev entry was opened */
6021 6022 if (isv6) {
6022 6023 connp->conn_family = AF_INET6;
6023 6024 connp->conn_ipversion = IPV6_VERSION;
6024 6025 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
6025 6026 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
6026 6027 } else {
6027 6028 connp->conn_family = AF_INET;
6028 6029 connp->conn_ipversion = IPV4_VERSION;
6029 6030 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6030 6031 }
6031 6032
6032 6033 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6033 6034 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6034 6035 connp->conn_minor_arena = ip_minor_arena_la;
6035 6036 } else {
6036 6037 /*
6037 6038 * Either minor numbers in the large arena were exhausted
6038 6039 * or a non socket application is doing the open.
6039 6040 * Try to allocate from the small arena.
6040 6041 */
6041 6042 if ((connp->conn_dev =
6042 6043 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6043 6044 /* CONN_DEC_REF takes care of netstack_rele() */
6044 6045 q->q_ptr = WR(q)->q_ptr = NULL;
6045 6046 CONN_DEC_REF(connp);
6046 6047 return (EBUSY);
6047 6048 }
6048 6049 connp->conn_minor_arena = ip_minor_arena_sa;
6049 6050 }
6050 6051
6051 6052 maj = getemajor(*devp);
6052 6053 *devp = makedevice(maj, (minor_t)connp->conn_dev);
6053 6054
6054 6055 /*
6055 6056 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6056 6057 */
6057 6058 connp->conn_cred = credp;
6058 6059 connp->conn_cpid = curproc->p_pid;
6059 6060 /* Cache things in ixa without an extra refhold */
6060 6061 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6061 6062 connp->conn_ixa->ixa_cred = connp->conn_cred;
6062 6063 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6063 6064 if (is_system_labeled())
6064 6065 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6065 6066
6066 6067 /*
6067 6068 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6068 6069 */
6069 6070 connp->conn_recv = ip_conn_input;
6070 6071 connp->conn_recvicmp = ip_conn_input_icmp;
6071 6072
6072 6073 crhold(connp->conn_cred);
6073 6074
6074 6075 /*
6075 6076 * If the caller has the process-wide flag set, then default to MAC
6076 6077 * exempt mode. This allows read-down to unlabeled hosts.
6077 6078 */
6078 6079 if (getpflags(NET_MAC_AWARE, credp) != 0)
6079 6080 connp->conn_mac_mode = CONN_MAC_AWARE;
6080 6081
6081 6082 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6082 6083
6083 6084 connp->conn_rq = q;
6084 6085 connp->conn_wq = WR(q);
6085 6086
6086 6087 /* Non-zero default values */
6087 6088 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6088 6089
6089 6090 /*
6090 6091 * Make the conn globally visible to walkers
6091 6092 */
6092 6093 ASSERT(connp->conn_ref == 1);
6093 6094 mutex_enter(&connp->conn_lock);
6094 6095 connp->conn_state_flags &= ~CONN_INCIPIENT;
6095 6096 mutex_exit(&connp->conn_lock);
6096 6097
6097 6098 qprocson(q);
6098 6099
6099 6100 return (0);
6100 6101 }
6101 6102
6102 6103 /*
6103 6104 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6104 6105 * all of them are copied to the conn_t. If the req is "zero", the policy is
6105 6106 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6106 6107 * fields.
6107 6108 * We keep only the latest setting of the policy and thus policy setting
6108 6109 * is not incremental/cumulative.
6109 6110 *
6110 6111 * Requests to set policies with multiple alternative actions will
6111 6112 * go through a different API.
6112 6113 */
6113 6114 int
6114 6115 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6115 6116 {
6116 6117 uint_t ah_req = 0;
6117 6118 uint_t esp_req = 0;
6118 6119 uint_t se_req = 0;
6119 6120 ipsec_act_t *actp = NULL;
6120 6121 uint_t nact;
6121 6122 ipsec_policy_head_t *ph;
6122 6123 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6123 6124 int error = 0;
6124 6125 netstack_t *ns = connp->conn_netstack;
6125 6126 ip_stack_t *ipst = ns->netstack_ip;
6126 6127 ipsec_stack_t *ipss = ns->netstack_ipsec;
6127 6128
6128 6129 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6129 6130
6130 6131 /*
6131 6132 * The IP_SEC_OPT option does not allow variable length parameters,
6132 6133 * hence a request cannot be NULL.
6133 6134 */
6134 6135 if (req == NULL)
6135 6136 return (EINVAL);
6136 6137
6137 6138 ah_req = req->ipsr_ah_req;
6138 6139 esp_req = req->ipsr_esp_req;
6139 6140 se_req = req->ipsr_self_encap_req;
6140 6141
6141 6142 /* Don't allow setting self-encap without one or more of AH/ESP. */
6142 6143 if (se_req != 0 && esp_req == 0 && ah_req == 0)
6143 6144 return (EINVAL);
6144 6145
6145 6146 /*
6146 6147 * Are we dealing with a request to reset the policy (i.e.
6147 6148 * zero requests).
6148 6149 */
6149 6150 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6150 6151 (esp_req & REQ_MASK) == 0 &&
6151 6152 (se_req & REQ_MASK) == 0);
6152 6153
6153 6154 if (!is_pol_reset) {
6154 6155 /*
6155 6156 * If we couldn't load IPsec, fail with "protocol
6156 6157 * not supported".
6157 6158 * IPsec may not have been loaded for a request with zero
6158 6159 * policies, so we don't fail in this case.
6159 6160 */
6160 6161 mutex_enter(&ipss->ipsec_loader_lock);
6161 6162 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6162 6163 mutex_exit(&ipss->ipsec_loader_lock);
6163 6164 return (EPROTONOSUPPORT);
6164 6165 }
6165 6166 mutex_exit(&ipss->ipsec_loader_lock);
6166 6167
6167 6168 /*
6168 6169 * Test for valid requests. Invalid algorithms
6169 6170 * need to be tested by IPsec code because new
6170 6171 * algorithms can be added dynamically.
6171 6172 */
6172 6173 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6173 6174 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6174 6175 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6175 6176 return (EINVAL);
6176 6177 }
6177 6178
6178 6179 /*
6179 6180 * Only privileged users can issue these
6180 6181 * requests.
6181 6182 */
6182 6183 if (((ah_req & IPSEC_PREF_NEVER) ||
6183 6184 (esp_req & IPSEC_PREF_NEVER) ||
6184 6185 (se_req & IPSEC_PREF_NEVER)) &&
6185 6186 secpolicy_ip_config(cr, B_FALSE) != 0) {
6186 6187 return (EPERM);
6187 6188 }
6188 6189
6189 6190 /*
6190 6191 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6191 6192 * are mutually exclusive.
6192 6193 */
6193 6194 if (((ah_req & REQ_MASK) == REQ_MASK) ||
6194 6195 ((esp_req & REQ_MASK) == REQ_MASK) ||
6195 6196 ((se_req & REQ_MASK) == REQ_MASK)) {
6196 6197 /* Both of them are set */
6197 6198 return (EINVAL);
6198 6199 }
6199 6200 }
6200 6201
6201 6202 ASSERT(MUTEX_HELD(&connp->conn_lock));
6202 6203
6203 6204 /*
6204 6205 * If we have already cached policies in conn_connect(), don't
6205 6206 * let them change now. We cache policies for connections
6206 6207 * whose src,dst [addr, port] is known.
6207 6208 */
6208 6209 if (connp->conn_policy_cached) {
6209 6210 return (EINVAL);
6210 6211 }
6211 6212
6212 6213 /*
6213 6214 * We have a zero policies, reset the connection policy if already
6214 6215 * set. This will cause the connection to inherit the
6215 6216 * global policy, if any.
6216 6217 */
6217 6218 if (is_pol_reset) {
6218 6219 if (connp->conn_policy != NULL) {
6219 6220 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6220 6221 connp->conn_policy = NULL;
6221 6222 }
6222 6223 connp->conn_in_enforce_policy = B_FALSE;
6223 6224 connp->conn_out_enforce_policy = B_FALSE;
6224 6225 return (0);
6225 6226 }
6226 6227
6227 6228 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6228 6229 ipst->ips_netstack);
6229 6230 if (ph == NULL)
6230 6231 goto enomem;
6231 6232
6232 6233 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6233 6234 if (actp == NULL)
6234 6235 goto enomem;
6235 6236
6236 6237 /*
6237 6238 * Always insert IPv4 policy entries, since they can also apply to
6238 6239 * ipv6 sockets being used in ipv4-compat mode.
6239 6240 */
6240 6241 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6241 6242 IPSEC_TYPE_INBOUND, ns))
6242 6243 goto enomem;
6243 6244 is_pol_inserted = B_TRUE;
6244 6245 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6245 6246 IPSEC_TYPE_OUTBOUND, ns))
6246 6247 goto enomem;
6247 6248
6248 6249 /*
6249 6250 * We're looking at a v6 socket, also insert the v6-specific
6250 6251 * entries.
6251 6252 */
6252 6253 if (connp->conn_family == AF_INET6) {
6253 6254 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6254 6255 IPSEC_TYPE_INBOUND, ns))
6255 6256 goto enomem;
6256 6257 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6257 6258 IPSEC_TYPE_OUTBOUND, ns))
6258 6259 goto enomem;
6259 6260 }
6260 6261
6261 6262 ipsec_actvec_free(actp, nact);
6262 6263
6263 6264 /*
6264 6265 * If the requests need security, set enforce_policy.
6265 6266 * If the requests are IPSEC_PREF_NEVER, one should
6266 6267 * still set conn_out_enforce_policy so that ip_set_destination
6267 6268 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6268 6269 * for connections that we don't cache policy in at connect time,
6269 6270 * if global policy matches in ip_output_attach_policy, we
6270 6271 * don't wrongly inherit global policy. Similarly, we need
6271 6272 * to set conn_in_enforce_policy also so that we don't verify
6272 6273 * policy wrongly.
6273 6274 */
6274 6275 if ((ah_req & REQ_MASK) != 0 ||
6275 6276 (esp_req & REQ_MASK) != 0 ||
6276 6277 (se_req & REQ_MASK) != 0) {
6277 6278 connp->conn_in_enforce_policy = B_TRUE;
6278 6279 connp->conn_out_enforce_policy = B_TRUE;
6279 6280 }
6280 6281
6281 6282 return (error);
6282 6283 #undef REQ_MASK
6283 6284
6284 6285 /*
6285 6286 * Common memory-allocation-failure exit path.
6286 6287 */
6287 6288 enomem:
6288 6289 if (actp != NULL)
6289 6290 ipsec_actvec_free(actp, nact);
6290 6291 if (is_pol_inserted)
6291 6292 ipsec_polhead_flush(ph, ns);
6292 6293 return (ENOMEM);
6293 6294 }
6294 6295
6295 6296 /*
6296 6297 * Set socket options for joining and leaving multicast groups.
6297 6298 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6298 6299 * The caller has already check that the option name is consistent with
6299 6300 * the address family of the socket.
6300 6301 */
6301 6302 int
6302 6303 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6303 6304 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6304 6305 {
6305 6306 int *i1 = (int *)invalp;
6306 6307 int error = 0;
6307 6308 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6308 6309 struct ip_mreq *v4_mreqp;
6309 6310 struct ipv6_mreq *v6_mreqp;
6310 6311 struct group_req *greqp;
6311 6312 ire_t *ire;
6312 6313 boolean_t done = B_FALSE;
6313 6314 ipaddr_t ifaddr;
6314 6315 in6_addr_t v6group;
6315 6316 uint_t ifindex;
6316 6317 boolean_t mcast_opt = B_TRUE;
6317 6318 mcast_record_t fmode;
6318 6319 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6319 6320 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6320 6321
6321 6322 switch (name) {
6322 6323 case IP_ADD_MEMBERSHIP:
6323 6324 case IPV6_JOIN_GROUP:
6324 6325 mcast_opt = B_FALSE;
6325 6326 /* FALLTHROUGH */
6326 6327 case MCAST_JOIN_GROUP:
6327 6328 fmode = MODE_IS_EXCLUDE;
6328 6329 optfn = ip_opt_add_group;
6329 6330 break;
6330 6331
6331 6332 case IP_DROP_MEMBERSHIP:
6332 6333 case IPV6_LEAVE_GROUP:
6333 6334 mcast_opt = B_FALSE;
6334 6335 /* FALLTHROUGH */
6335 6336 case MCAST_LEAVE_GROUP:
6336 6337 fmode = MODE_IS_INCLUDE;
6337 6338 optfn = ip_opt_delete_group;
6338 6339 break;
6339 6340 default:
6340 6341 ASSERT(0);
6341 6342 }
6342 6343
6343 6344 if (mcast_opt) {
6344 6345 struct sockaddr_in *sin;
6345 6346 struct sockaddr_in6 *sin6;
6346 6347
6347 6348 greqp = (struct group_req *)i1;
6348 6349 if (greqp->gr_group.ss_family == AF_INET) {
6349 6350 sin = (struct sockaddr_in *)&(greqp->gr_group);
6350 6351 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6351 6352 } else {
6352 6353 if (!inet6)
6353 6354 return (EINVAL); /* Not on INET socket */
6354 6355
6355 6356 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6356 6357 v6group = sin6->sin6_addr;
6357 6358 }
6358 6359 ifaddr = INADDR_ANY;
6359 6360 ifindex = greqp->gr_interface;
6360 6361 } else if (inet6) {
6361 6362 v6_mreqp = (struct ipv6_mreq *)i1;
6362 6363 v6group = v6_mreqp->ipv6mr_multiaddr;
6363 6364 ifaddr = INADDR_ANY;
6364 6365 ifindex = v6_mreqp->ipv6mr_interface;
6365 6366 } else {
6366 6367 v4_mreqp = (struct ip_mreq *)i1;
6367 6368 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6368 6369 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6369 6370 ifindex = 0;
6370 6371 }
6371 6372
6372 6373 /*
6373 6374 * In the multirouting case, we need to replicate
6374 6375 * the request on all interfaces that will take part
6375 6376 * in replication. We do so because multirouting is
6376 6377 * reflective, thus we will probably receive multi-
6377 6378 * casts on those interfaces.
6378 6379 * The ip_multirt_apply_membership() succeeds if
6379 6380 * the operation succeeds on at least one interface.
6380 6381 */
6381 6382 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6382 6383 ipaddr_t group;
6383 6384
6384 6385 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6385 6386
6386 6387 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6387 6388 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6388 6389 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6389 6390 } else {
6390 6391 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6391 6392 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6392 6393 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6393 6394 }
6394 6395 if (ire != NULL) {
6395 6396 if (ire->ire_flags & RTF_MULTIRT) {
6396 6397 error = ip_multirt_apply_membership(optfn, ire, connp,
6397 6398 checkonly, &v6group, fmode, &ipv6_all_zeros);
6398 6399 done = B_TRUE;
6399 6400 }
6400 6401 ire_refrele(ire);
6401 6402 }
6402 6403
6403 6404 if (!done) {
6404 6405 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6405 6406 fmode, &ipv6_all_zeros);
6406 6407 }
6407 6408 return (error);
6408 6409 }
6409 6410
6410 6411 /*
6411 6412 * Set socket options for joining and leaving multicast groups
6412 6413 * for specific sources.
6413 6414 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6414 6415 * The caller has already check that the option name is consistent with
6415 6416 * the address family of the socket.
6416 6417 */
6417 6418 int
6418 6419 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6419 6420 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6420 6421 {
6421 6422 int *i1 = (int *)invalp;
6422 6423 int error = 0;
6423 6424 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6424 6425 struct ip_mreq_source *imreqp;
6425 6426 struct group_source_req *gsreqp;
6426 6427 in6_addr_t v6group, v6src;
6427 6428 uint32_t ifindex;
6428 6429 ipaddr_t ifaddr;
6429 6430 boolean_t mcast_opt = B_TRUE;
6430 6431 mcast_record_t fmode;
6431 6432 ire_t *ire;
6432 6433 boolean_t done = B_FALSE;
6433 6434 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6434 6435 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6435 6436
6436 6437 switch (name) {
6437 6438 case IP_BLOCK_SOURCE:
6438 6439 mcast_opt = B_FALSE;
6439 6440 /* FALLTHROUGH */
6440 6441 case MCAST_BLOCK_SOURCE:
6441 6442 fmode = MODE_IS_EXCLUDE;
6442 6443 optfn = ip_opt_add_group;
6443 6444 break;
6444 6445
6445 6446 case IP_UNBLOCK_SOURCE:
6446 6447 mcast_opt = B_FALSE;
6447 6448 /* FALLTHROUGH */
6448 6449 case MCAST_UNBLOCK_SOURCE:
6449 6450 fmode = MODE_IS_EXCLUDE;
6450 6451 optfn = ip_opt_delete_group;
6451 6452 break;
6452 6453
6453 6454 case IP_ADD_SOURCE_MEMBERSHIP:
6454 6455 mcast_opt = B_FALSE;
6455 6456 /* FALLTHROUGH */
6456 6457 case MCAST_JOIN_SOURCE_GROUP:
6457 6458 fmode = MODE_IS_INCLUDE;
6458 6459 optfn = ip_opt_add_group;
6459 6460 break;
6460 6461
6461 6462 case IP_DROP_SOURCE_MEMBERSHIP:
6462 6463 mcast_opt = B_FALSE;
6463 6464 /* FALLTHROUGH */
6464 6465 case MCAST_LEAVE_SOURCE_GROUP:
6465 6466 fmode = MODE_IS_INCLUDE;
6466 6467 optfn = ip_opt_delete_group;
6467 6468 break;
6468 6469 default:
6469 6470 ASSERT(0);
6470 6471 }
6471 6472
6472 6473 if (mcast_opt) {
6473 6474 gsreqp = (struct group_source_req *)i1;
6474 6475 ifindex = gsreqp->gsr_interface;
6475 6476 if (gsreqp->gsr_group.ss_family == AF_INET) {
6476 6477 struct sockaddr_in *s;
6477 6478 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6478 6479 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6479 6480 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6480 6481 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6481 6482 } else {
6482 6483 struct sockaddr_in6 *s6;
6483 6484
6484 6485 if (!inet6)
6485 6486 return (EINVAL); /* Not on INET socket */
6486 6487
6487 6488 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6488 6489 v6group = s6->sin6_addr;
6489 6490 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6490 6491 v6src = s6->sin6_addr;
6491 6492 }
6492 6493 ifaddr = INADDR_ANY;
6493 6494 } else {
6494 6495 imreqp = (struct ip_mreq_source *)i1;
6495 6496 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6496 6497 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6497 6498 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6498 6499 ifindex = 0;
6499 6500 }
6500 6501
6501 6502 /*
6502 6503 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6503 6504 */
6504 6505 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6505 6506 v6src = ipv6_all_zeros;
6506 6507
6507 6508 /*
6508 6509 * In the multirouting case, we need to replicate
6509 6510 * the request as noted in the mcast cases above.
6510 6511 */
6511 6512 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6512 6513 ipaddr_t group;
6513 6514
6514 6515 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6515 6516
6516 6517 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6517 6518 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6518 6519 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6519 6520 } else {
6520 6521 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6521 6522 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6522 6523 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6523 6524 }
6524 6525 if (ire != NULL) {
6525 6526 if (ire->ire_flags & RTF_MULTIRT) {
6526 6527 error = ip_multirt_apply_membership(optfn, ire, connp,
6527 6528 checkonly, &v6group, fmode, &v6src);
6528 6529 done = B_TRUE;
6529 6530 }
6530 6531 ire_refrele(ire);
6531 6532 }
6532 6533 if (!done) {
6533 6534 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6534 6535 fmode, &v6src);
6535 6536 }
6536 6537 return (error);
6537 6538 }
6538 6539
6539 6540 /*
6540 6541 * Given a destination address and a pointer to where to put the information
6541 6542 * this routine fills in the mtuinfo.
6542 6543 * The socket must be connected.
6543 6544 * For sctp conn_faddr is the primary address.
6544 6545 */
6545 6546 int
6546 6547 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6547 6548 {
6548 6549 uint32_t pmtu = IP_MAXPACKET;
6549 6550 uint_t scopeid;
6550 6551
6551 6552 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6552 6553 return (-1);
6553 6554
6554 6555 /* In case we never sent or called ip_set_destination_v4/v6 */
6555 6556 if (ixa->ixa_ire != NULL)
6556 6557 pmtu = ip_get_pmtu(ixa);
6557 6558
6558 6559 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6559 6560 scopeid = ixa->ixa_scopeid;
6560 6561 else
6561 6562 scopeid = 0;
6562 6563
6563 6564 bzero(mtuinfo, sizeof (*mtuinfo));
6564 6565 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6565 6566 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6566 6567 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6567 6568 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6568 6569 mtuinfo->ip6m_mtu = pmtu;
6569 6570
6570 6571 return (sizeof (struct ip6_mtuinfo));
6571 6572 }
6572 6573
6573 6574 /*
6574 6575 * When the src multihoming is changed from weak to [strong, preferred]
6575 6576 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6576 6577 * and identify routes that were created by user-applications in the
6577 6578 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6578 6579 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6579 6580 * is selected by finding an interface route for the gateway.
6580 6581 */
6581 6582 /* ARGSUSED */
6582 6583 void
6583 6584 ip_ire_rebind_walker(ire_t *ire, void *notused)
6584 6585 {
6585 6586 if (!ire->ire_unbound || ire->ire_ill != NULL)
6586 6587 return;
6587 6588 ire_rebind(ire);
6588 6589 ire_delete(ire);
6589 6590 }
6590 6591
6591 6592 /*
6592 6593 * When the src multihoming is changed from [strong, preferred] to weak,
6593 6594 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6594 6595 * set any entries that were created by user-applications in the unbound state
6595 6596 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6596 6597 */
6597 6598 /* ARGSUSED */
6598 6599 void
6599 6600 ip_ire_unbind_walker(ire_t *ire, void *notused)
6600 6601 {
6601 6602 ire_t *new_ire;
6602 6603
6603 6604 if (!ire->ire_unbound || ire->ire_ill == NULL)
6604 6605 return;
6605 6606 if (ire->ire_ipversion == IPV6_VERSION) {
6606 6607 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6607 6608 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6608 6609 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6609 6610 } else {
6610 6611 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6611 6612 (uchar_t *)&ire->ire_mask,
6612 6613 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6613 6614 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6614 6615 }
6615 6616 if (new_ire == NULL)
6616 6617 return;
6617 6618 new_ire->ire_unbound = B_TRUE;
6618 6619 /*
6619 6620 * The bound ire must first be deleted so that we don't return
6620 6621 * the existing one on the attempt to add the unbound new_ire.
6621 6622 */
6622 6623 ire_delete(ire);
6623 6624 new_ire = ire_add(new_ire);
6624 6625 if (new_ire != NULL)
6625 6626 ire_refrele(new_ire);
6626 6627 }
6627 6628
6628 6629 /*
6629 6630 * When the settings of ip*_strict_src_multihoming tunables are changed,
6630 6631 * all cached routes need to be recomputed. This recomputation needs to be
6631 6632 * done when going from weaker to stronger modes so that the cached ire
6632 6633 * for the connection does not violate the current ip*_strict_src_multihoming
6633 6634 * setting. It also needs to be done when going from stronger to weaker modes,
6634 6635 * so that we fall back to matching on the longest-matching-route (as opposed
6635 6636 * to a shorter match that may have been selected in the strong mode
6636 6637 * to satisfy src_multihoming settings).
6637 6638 *
6638 6639 * The cached ixa_ire entires for all conn_t entries are marked as
6639 6640 * "verify" so that they will be recomputed for the next packet.
6640 6641 */
6641 6642 void
6642 6643 conn_ire_revalidate(conn_t *connp, void *arg)
6643 6644 {
6644 6645 boolean_t isv6 = (boolean_t)arg;
6645 6646
6646 6647 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6647 6648 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6648 6649 return;
6649 6650 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6650 6651 }
6651 6652
6652 6653 /*
6653 6654 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6654 6655 * When an ipf is passed here for the first time, if
6655 6656 * we already have in-order fragments on the queue, we convert from the fast-
6656 6657 * path reassembly scheme to the hard-case scheme. From then on, additional
6657 6658 * fragments are reassembled here. We keep track of the start and end offsets
6658 6659 * of each piece, and the number of holes in the chain. When the hole count
6659 6660 * goes to zero, we are done!
6660 6661 *
6661 6662 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6662 6663 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6663 6664 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6664 6665 * after the call to ip_reassemble().
6665 6666 */
6666 6667 int
6667 6668 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6668 6669 size_t msg_len)
6669 6670 {
6670 6671 uint_t end;
6671 6672 mblk_t *next_mp;
6672 6673 mblk_t *mp1;
6673 6674 uint_t offset;
6674 6675 boolean_t incr_dups = B_TRUE;
6675 6676 boolean_t offset_zero_seen = B_FALSE;
6676 6677 boolean_t pkt_boundary_checked = B_FALSE;
6677 6678
6678 6679 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6679 6680 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6680 6681
6681 6682 /* Add in byte count */
6682 6683 ipf->ipf_count += msg_len;
6683 6684 if (ipf->ipf_end) {
6684 6685 /*
6685 6686 * We were part way through in-order reassembly, but now there
6686 6687 * is a hole. We walk through messages already queued, and
6687 6688 * mark them for hard case reassembly. We know that up till
6688 6689 * now they were in order starting from offset zero.
6689 6690 */
6690 6691 offset = 0;
6691 6692 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6692 6693 IP_REASS_SET_START(mp1, offset);
6693 6694 if (offset == 0) {
6694 6695 ASSERT(ipf->ipf_nf_hdr_len != 0);
6695 6696 offset = -ipf->ipf_nf_hdr_len;
6696 6697 }
6697 6698 offset += mp1->b_wptr - mp1->b_rptr;
6698 6699 IP_REASS_SET_END(mp1, offset);
6699 6700 }
6700 6701 /* One hole at the end. */
6701 6702 ipf->ipf_hole_cnt = 1;
6702 6703 /* Brand it as a hard case, forever. */
6703 6704 ipf->ipf_end = 0;
6704 6705 }
6705 6706 /* Walk through all the new pieces. */
6706 6707 do {
6707 6708 end = start + (mp->b_wptr - mp->b_rptr);
6708 6709 /*
6709 6710 * If start is 0, decrease 'end' only for the first mblk of
6710 6711 * the fragment. Otherwise 'end' can get wrong value in the
6711 6712 * second pass of the loop if first mblk is exactly the
6712 6713 * size of ipf_nf_hdr_len.
6713 6714 */
6714 6715 if (start == 0 && !offset_zero_seen) {
6715 6716 /* First segment */
6716 6717 ASSERT(ipf->ipf_nf_hdr_len != 0);
6717 6718 end -= ipf->ipf_nf_hdr_len;
6718 6719 offset_zero_seen = B_TRUE;
6719 6720 }
6720 6721 next_mp = mp->b_cont;
6721 6722 /*
6722 6723 * We are checking to see if there is any interesing data
6723 6724 * to process. If there isn't and the mblk isn't the
6724 6725 * one which carries the unfragmentable header then we
6725 6726 * drop it. It's possible to have just the unfragmentable
6726 6727 * header come through without any data. That needs to be
6727 6728 * saved.
6728 6729 *
6729 6730 * If the assert at the top of this function holds then the
6730 6731 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6731 6732 * is infrequently traveled enough that the test is left in
6732 6733 * to protect against future code changes which break that
6733 6734 * invariant.
6734 6735 */
6735 6736 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6736 6737 /* Empty. Blast it. */
6737 6738 IP_REASS_SET_START(mp, 0);
6738 6739 IP_REASS_SET_END(mp, 0);
6739 6740 /*
6740 6741 * If the ipf points to the mblk we are about to free,
6741 6742 * update ipf to point to the next mblk (or NULL
6742 6743 * if none).
6743 6744 */
6744 6745 if (ipf->ipf_mp->b_cont == mp)
6745 6746 ipf->ipf_mp->b_cont = next_mp;
6746 6747 freeb(mp);
6747 6748 continue;
6748 6749 }
6749 6750 mp->b_cont = NULL;
6750 6751 IP_REASS_SET_START(mp, start);
6751 6752 IP_REASS_SET_END(mp, end);
6752 6753 if (!ipf->ipf_tail_mp) {
6753 6754 ipf->ipf_tail_mp = mp;
6754 6755 ipf->ipf_mp->b_cont = mp;
6755 6756 if (start == 0 || !more) {
6756 6757 ipf->ipf_hole_cnt = 1;
6757 6758 /*
6758 6759 * if the first fragment comes in more than one
6759 6760 * mblk, this loop will be executed for each
6760 6761 * mblk. Need to adjust hole count so exiting
6761 6762 * this routine will leave hole count at 1.
6762 6763 */
6763 6764 if (next_mp)
6764 6765 ipf->ipf_hole_cnt++;
6765 6766 } else
6766 6767 ipf->ipf_hole_cnt = 2;
6767 6768 continue;
6768 6769 } else if (ipf->ipf_last_frag_seen && !more &&
6769 6770 !pkt_boundary_checked) {
6770 6771 /*
6771 6772 * We check datagram boundary only if this fragment
6772 6773 * claims to be the last fragment and we have seen a
6773 6774 * last fragment in the past too. We do this only
6774 6775 * once for a given fragment.
6775 6776 *
6776 6777 * start cannot be 0 here as fragments with start=0
6777 6778 * and MF=0 gets handled as a complete packet. These
6778 6779 * fragments should not reach here.
6779 6780 */
6780 6781
6781 6782 if (start + msgdsize(mp) !=
6782 6783 IP_REASS_END(ipf->ipf_tail_mp)) {
6783 6784 /*
6784 6785 * We have two fragments both of which claim
6785 6786 * to be the last fragment but gives conflicting
6786 6787 * information about the whole datagram size.
6787 6788 * Something fishy is going on. Drop the
6788 6789 * fragment and free up the reassembly list.
6789 6790 */
6790 6791 return (IP_REASS_FAILED);
6791 6792 }
6792 6793
6793 6794 /*
6794 6795 * We shouldn't come to this code block again for this
6795 6796 * particular fragment.
6796 6797 */
6797 6798 pkt_boundary_checked = B_TRUE;
6798 6799 }
6799 6800
6800 6801 /* New stuff at or beyond tail? */
6801 6802 offset = IP_REASS_END(ipf->ipf_tail_mp);
6802 6803 if (start >= offset) {
6803 6804 if (ipf->ipf_last_frag_seen) {
6804 6805 /* current fragment is beyond last fragment */
6805 6806 return (IP_REASS_FAILED);
6806 6807 }
6807 6808 /* Link it on end. */
6808 6809 ipf->ipf_tail_mp->b_cont = mp;
6809 6810 ipf->ipf_tail_mp = mp;
6810 6811 if (more) {
6811 6812 if (start != offset)
6812 6813 ipf->ipf_hole_cnt++;
6813 6814 } else if (start == offset && next_mp == NULL)
6814 6815 ipf->ipf_hole_cnt--;
6815 6816 continue;
6816 6817 }
6817 6818 mp1 = ipf->ipf_mp->b_cont;
6818 6819 offset = IP_REASS_START(mp1);
6819 6820 /* New stuff at the front? */
6820 6821 if (start < offset) {
6821 6822 if (start == 0) {
6822 6823 if (end >= offset) {
6823 6824 /* Nailed the hole at the begining. */
6824 6825 ipf->ipf_hole_cnt--;
6825 6826 }
6826 6827 } else if (end < offset) {
6827 6828 /*
6828 6829 * A hole, stuff, and a hole where there used
6829 6830 * to be just a hole.
6830 6831 */
6831 6832 ipf->ipf_hole_cnt++;
6832 6833 }
6833 6834 mp->b_cont = mp1;
6834 6835 /* Check for overlap. */
6835 6836 while (end > offset) {
6836 6837 if (end < IP_REASS_END(mp1)) {
6837 6838 mp->b_wptr -= end - offset;
6838 6839 IP_REASS_SET_END(mp, offset);
6839 6840 BUMP_MIB(ill->ill_ip_mib,
6840 6841 ipIfStatsReasmPartDups);
6841 6842 break;
6842 6843 }
6843 6844 /* Did we cover another hole? */
6844 6845 if ((mp1->b_cont &&
6845 6846 IP_REASS_END(mp1) !=
6846 6847 IP_REASS_START(mp1->b_cont) &&
6847 6848 end >= IP_REASS_START(mp1->b_cont)) ||
6848 6849 (!ipf->ipf_last_frag_seen && !more)) {
6849 6850 ipf->ipf_hole_cnt--;
6850 6851 }
6851 6852 /* Clip out mp1. */
6852 6853 if ((mp->b_cont = mp1->b_cont) == NULL) {
6853 6854 /*
6854 6855 * After clipping out mp1, this guy
6855 6856 * is now hanging off the end.
6856 6857 */
6857 6858 ipf->ipf_tail_mp = mp;
6858 6859 }
6859 6860 IP_REASS_SET_START(mp1, 0);
6860 6861 IP_REASS_SET_END(mp1, 0);
6861 6862 /* Subtract byte count */
6862 6863 ipf->ipf_count -= mp1->b_datap->db_lim -
6863 6864 mp1->b_datap->db_base;
6864 6865 freeb(mp1);
6865 6866 BUMP_MIB(ill->ill_ip_mib,
6866 6867 ipIfStatsReasmPartDups);
6867 6868 mp1 = mp->b_cont;
6868 6869 if (!mp1)
6869 6870 break;
6870 6871 offset = IP_REASS_START(mp1);
6871 6872 }
6872 6873 ipf->ipf_mp->b_cont = mp;
6873 6874 continue;
6874 6875 }
6875 6876 /*
6876 6877 * The new piece starts somewhere between the start of the head
6877 6878 * and before the end of the tail.
6878 6879 */
6879 6880 for (; mp1; mp1 = mp1->b_cont) {
6880 6881 offset = IP_REASS_END(mp1);
6881 6882 if (start < offset) {
6882 6883 if (end <= offset) {
6883 6884 /* Nothing new. */
6884 6885 IP_REASS_SET_START(mp, 0);
6885 6886 IP_REASS_SET_END(mp, 0);
6886 6887 /* Subtract byte count */
6887 6888 ipf->ipf_count -= mp->b_datap->db_lim -
6888 6889 mp->b_datap->db_base;
6889 6890 if (incr_dups) {
6890 6891 ipf->ipf_num_dups++;
6891 6892 incr_dups = B_FALSE;
6892 6893 }
6893 6894 freeb(mp);
6894 6895 BUMP_MIB(ill->ill_ip_mib,
6895 6896 ipIfStatsReasmDuplicates);
6896 6897 break;
6897 6898 }
6898 6899 /*
6899 6900 * Trim redundant stuff off beginning of new
6900 6901 * piece.
6901 6902 */
6902 6903 IP_REASS_SET_START(mp, offset);
6903 6904 mp->b_rptr += offset - start;
6904 6905 BUMP_MIB(ill->ill_ip_mib,
6905 6906 ipIfStatsReasmPartDups);
6906 6907 start = offset;
6907 6908 if (!mp1->b_cont) {
6908 6909 /*
6909 6910 * After trimming, this guy is now
6910 6911 * hanging off the end.
6911 6912 */
6912 6913 mp1->b_cont = mp;
6913 6914 ipf->ipf_tail_mp = mp;
6914 6915 if (!more) {
6915 6916 ipf->ipf_hole_cnt--;
6916 6917 }
6917 6918 break;
6918 6919 }
6919 6920 }
6920 6921 if (start >= IP_REASS_START(mp1->b_cont))
6921 6922 continue;
6922 6923 /* Fill a hole */
6923 6924 if (start > offset)
6924 6925 ipf->ipf_hole_cnt++;
6925 6926 mp->b_cont = mp1->b_cont;
6926 6927 mp1->b_cont = mp;
6927 6928 mp1 = mp->b_cont;
6928 6929 offset = IP_REASS_START(mp1);
6929 6930 if (end >= offset) {
6930 6931 ipf->ipf_hole_cnt--;
6931 6932 /* Check for overlap. */
6932 6933 while (end > offset) {
6933 6934 if (end < IP_REASS_END(mp1)) {
6934 6935 mp->b_wptr -= end - offset;
6935 6936 IP_REASS_SET_END(mp, offset);
6936 6937 /*
6937 6938 * TODO we might bump
6938 6939 * this up twice if there is
6939 6940 * overlap at both ends.
6940 6941 */
6941 6942 BUMP_MIB(ill->ill_ip_mib,
6942 6943 ipIfStatsReasmPartDups);
6943 6944 break;
6944 6945 }
6945 6946 /* Did we cover another hole? */
6946 6947 if ((mp1->b_cont &&
6947 6948 IP_REASS_END(mp1)
6948 6949 != IP_REASS_START(mp1->b_cont) &&
6949 6950 end >=
6950 6951 IP_REASS_START(mp1->b_cont)) ||
6951 6952 (!ipf->ipf_last_frag_seen &&
6952 6953 !more)) {
6953 6954 ipf->ipf_hole_cnt--;
6954 6955 }
6955 6956 /* Clip out mp1. */
6956 6957 if ((mp->b_cont = mp1->b_cont) ==
6957 6958 NULL) {
6958 6959 /*
6959 6960 * After clipping out mp1,
6960 6961 * this guy is now hanging
6961 6962 * off the end.
6962 6963 */
6963 6964 ipf->ipf_tail_mp = mp;
6964 6965 }
6965 6966 IP_REASS_SET_START(mp1, 0);
6966 6967 IP_REASS_SET_END(mp1, 0);
6967 6968 /* Subtract byte count */
6968 6969 ipf->ipf_count -=
6969 6970 mp1->b_datap->db_lim -
6970 6971 mp1->b_datap->db_base;
6971 6972 freeb(mp1);
6972 6973 BUMP_MIB(ill->ill_ip_mib,
6973 6974 ipIfStatsReasmPartDups);
6974 6975 mp1 = mp->b_cont;
6975 6976 if (!mp1)
6976 6977 break;
6977 6978 offset = IP_REASS_START(mp1);
6978 6979 }
6979 6980 }
6980 6981 break;
6981 6982 }
6982 6983 } while (start = end, mp = next_mp);
6983 6984
6984 6985 /* Fragment just processed could be the last one. Remember this fact */
6985 6986 if (!more)
6986 6987 ipf->ipf_last_frag_seen = B_TRUE;
6987 6988
6988 6989 /* Still got holes? */
6989 6990 if (ipf->ipf_hole_cnt)
6990 6991 return (IP_REASS_PARTIAL);
6991 6992 /* Clean up overloaded fields to avoid upstream disasters. */
6992 6993 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6993 6994 IP_REASS_SET_START(mp1, 0);
6994 6995 IP_REASS_SET_END(mp1, 0);
6995 6996 }
6996 6997 return (IP_REASS_COMPLETE);
6997 6998 }
6998 6999
6999 7000 /*
7000 7001 * Fragmentation reassembly. Each ILL has a hash table for
7001 7002 * queuing packets undergoing reassembly for all IPIFs
7002 7003 * associated with the ILL. The hash is based on the packet
7003 7004 * IP ident field. The ILL frag hash table was allocated
7004 7005 * as a timer block at the time the ILL was created. Whenever
7005 7006 * there is anything on the reassembly queue, the timer will
7006 7007 * be running. Returns the reassembled packet if reassembly completes.
7007 7008 */
7008 7009 mblk_t *
7009 7010 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
7010 7011 {
7011 7012 uint32_t frag_offset_flags;
7012 7013 mblk_t *t_mp;
7013 7014 ipaddr_t dst;
7014 7015 uint8_t proto = ipha->ipha_protocol;
7015 7016 uint32_t sum_val;
7016 7017 uint16_t sum_flags;
7017 7018 ipf_t *ipf;
7018 7019 ipf_t **ipfp;
7019 7020 ipfb_t *ipfb;
7020 7021 uint16_t ident;
7021 7022 uint32_t offset;
7022 7023 ipaddr_t src;
7023 7024 uint_t hdr_length;
7024 7025 uint32_t end;
7025 7026 mblk_t *mp1;
7026 7027 mblk_t *tail_mp;
7027 7028 size_t count;
7028 7029 size_t msg_len;
7029 7030 uint8_t ecn_info = 0;
7030 7031 uint32_t packet_size;
7031 7032 boolean_t pruned = B_FALSE;
7032 7033 ill_t *ill = ira->ira_ill;
7033 7034 ip_stack_t *ipst = ill->ill_ipst;
7034 7035
7035 7036 /*
7036 7037 * Drop the fragmented as early as possible, if
7037 7038 * we don't have resource(s) to re-assemble.
7038 7039 */
7039 7040 if (ipst->ips_ip_reass_queue_bytes == 0) {
7040 7041 freemsg(mp);
7041 7042 return (NULL);
7042 7043 }
7043 7044
7044 7045 /* Check for fragmentation offset; return if there's none */
7045 7046 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7046 7047 (IPH_MF | IPH_OFFSET)) == 0)
7047 7048 return (mp);
7048 7049
7049 7050 /*
7050 7051 * We utilize hardware computed checksum info only for UDP since
7051 7052 * IP fragmentation is a normal occurrence for the protocol. In
7052 7053 * addition, checksum offload support for IP fragments carrying
7053 7054 * UDP payload is commonly implemented across network adapters.
7054 7055 */
7055 7056 ASSERT(ira->ira_rill != NULL);
7056 7057 if (proto == IPPROTO_UDP && dohwcksum &&
7057 7058 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7058 7059 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7059 7060 mblk_t *mp1 = mp->b_cont;
7060 7061 int32_t len;
7061 7062
7062 7063 /* Record checksum information from the packet */
7063 7064 sum_val = (uint32_t)DB_CKSUM16(mp);
7064 7065 sum_flags = DB_CKSUMFLAGS(mp);
7065 7066
7066 7067 /* IP payload offset from beginning of mblk */
7067 7068 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7068 7069
7069 7070 if ((sum_flags & HCK_PARTIALCKSUM) &&
7070 7071 (mp1 == NULL || mp1->b_cont == NULL) &&
7071 7072 offset >= DB_CKSUMSTART(mp) &&
7072 7073 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7073 7074 uint32_t adj;
7074 7075 /*
7075 7076 * Partial checksum has been calculated by hardware
7076 7077 * and attached to the packet; in addition, any
7077 7078 * prepended extraneous data is even byte aligned.
7078 7079 * If any such data exists, we adjust the checksum;
7079 7080 * this would also handle any postpended data.
7080 7081 */
7081 7082 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7082 7083 mp, mp1, len, adj);
7083 7084
7084 7085 /* One's complement subtract extraneous checksum */
7085 7086 if (adj >= sum_val)
7086 7087 sum_val = ~(adj - sum_val) & 0xFFFF;
7087 7088 else
7088 7089 sum_val -= adj;
7089 7090 }
7090 7091 } else {
7091 7092 sum_val = 0;
7092 7093 sum_flags = 0;
7093 7094 }
7094 7095
7095 7096 /* Clear hardware checksumming flag */
7096 7097 DB_CKSUMFLAGS(mp) = 0;
7097 7098
7098 7099 ident = ipha->ipha_ident;
7099 7100 offset = (frag_offset_flags << 3) & 0xFFFF;
7100 7101 src = ipha->ipha_src;
7101 7102 dst = ipha->ipha_dst;
7102 7103 hdr_length = IPH_HDR_LENGTH(ipha);
7103 7104 end = ntohs(ipha->ipha_length) - hdr_length;
7104 7105
7105 7106 /* If end == 0 then we have a packet with no data, so just free it */
7106 7107 if (end == 0) {
7107 7108 freemsg(mp);
7108 7109 return (NULL);
7109 7110 }
7110 7111
7111 7112 /* Record the ECN field info. */
7112 7113 ecn_info = (ipha->ipha_type_of_service & 0x3);
7113 7114 if (offset != 0) {
7114 7115 /*
7115 7116 * If this isn't the first piece, strip the header, and
7116 7117 * add the offset to the end value.
7117 7118 */
7118 7119 mp->b_rptr += hdr_length;
7119 7120 end += offset;
7120 7121 }
7121 7122
7122 7123 /* Handle vnic loopback of fragments */
7123 7124 if (mp->b_datap->db_ref > 2)
7124 7125 msg_len = 0;
7125 7126 else
7126 7127 msg_len = MBLKSIZE(mp);
7127 7128
7128 7129 tail_mp = mp;
7129 7130 while (tail_mp->b_cont != NULL) {
7130 7131 tail_mp = tail_mp->b_cont;
7131 7132 if (tail_mp->b_datap->db_ref <= 2)
7132 7133 msg_len += MBLKSIZE(tail_mp);
7133 7134 }
7134 7135
7135 7136 /* If the reassembly list for this ILL will get too big, prune it */
7136 7137 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7137 7138 ipst->ips_ip_reass_queue_bytes) {
7138 7139 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7139 7140 uint_t, ill->ill_frag_count,
7140 7141 uint_t, ipst->ips_ip_reass_queue_bytes);
7141 7142 ill_frag_prune(ill,
7142 7143 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7143 7144 (ipst->ips_ip_reass_queue_bytes - msg_len));
7144 7145 pruned = B_TRUE;
7145 7146 }
7146 7147
7147 7148 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7148 7149 mutex_enter(&ipfb->ipfb_lock);
7149 7150
7150 7151 ipfp = &ipfb->ipfb_ipf;
7151 7152 /* Try to find an existing fragment queue for this packet. */
7152 7153 for (;;) {
7153 7154 ipf = ipfp[0];
7154 7155 if (ipf != NULL) {
7155 7156 /*
7156 7157 * It has to match on ident and src/dst address.
7157 7158 */
7158 7159 if (ipf->ipf_ident == ident &&
7159 7160 ipf->ipf_src == src &&
7160 7161 ipf->ipf_dst == dst &&
7161 7162 ipf->ipf_protocol == proto) {
7162 7163 /*
7163 7164 * If we have received too many
7164 7165 * duplicate fragments for this packet
7165 7166 * free it.
7166 7167 */
7167 7168 if (ipf->ipf_num_dups > ip_max_frag_dups) {
7168 7169 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7169 7170 freemsg(mp);
7170 7171 mutex_exit(&ipfb->ipfb_lock);
7171 7172 return (NULL);
7172 7173 }
7173 7174 /* Found it. */
7174 7175 break;
7175 7176 }
7176 7177 ipfp = &ipf->ipf_hash_next;
7177 7178 continue;
7178 7179 }
7179 7180
7180 7181 /*
7181 7182 * If we pruned the list, do we want to store this new
7182 7183 * fragment?. We apply an optimization here based on the
7183 7184 * fact that most fragments will be received in order.
7184 7185 * So if the offset of this incoming fragment is zero,
7185 7186 * it is the first fragment of a new packet. We will
7186 7187 * keep it. Otherwise drop the fragment, as we have
7187 7188 * probably pruned the packet already (since the
7188 7189 * packet cannot be found).
7189 7190 */
7190 7191 if (pruned && offset != 0) {
7191 7192 mutex_exit(&ipfb->ipfb_lock);
7192 7193 freemsg(mp);
7193 7194 return (NULL);
7194 7195 }
7195 7196
7196 7197 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
7197 7198 /*
7198 7199 * Too many fragmented packets in this hash
7199 7200 * bucket. Free the oldest.
7200 7201 */
7201 7202 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7202 7203 }
7203 7204
7204 7205 /* New guy. Allocate a frag message. */
7205 7206 mp1 = allocb(sizeof (*ipf), BPRI_MED);
7206 7207 if (mp1 == NULL) {
7207 7208 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7208 7209 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7209 7210 freemsg(mp);
7210 7211 reass_done:
7211 7212 mutex_exit(&ipfb->ipfb_lock);
7212 7213 return (NULL);
7213 7214 }
7214 7215
7215 7216 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7216 7217 mp1->b_cont = mp;
7217 7218
7218 7219 /* Initialize the fragment header. */
7219 7220 ipf = (ipf_t *)mp1->b_rptr;
7220 7221 ipf->ipf_mp = mp1;
7221 7222 ipf->ipf_ptphn = ipfp;
7222 7223 ipfp[0] = ipf;
7223 7224 ipf->ipf_hash_next = NULL;
7224 7225 ipf->ipf_ident = ident;
7225 7226 ipf->ipf_protocol = proto;
7226 7227 ipf->ipf_src = src;
7227 7228 ipf->ipf_dst = dst;
7228 7229 ipf->ipf_nf_hdr_len = 0;
7229 7230 /* Record reassembly start time. */
7230 7231 ipf->ipf_timestamp = gethrestime_sec();
7231 7232 /* Record ipf generation and account for frag header */
7232 7233 ipf->ipf_gen = ill->ill_ipf_gen++;
7233 7234 ipf->ipf_count = MBLKSIZE(mp1);
7234 7235 ipf->ipf_last_frag_seen = B_FALSE;
7235 7236 ipf->ipf_ecn = ecn_info;
7236 7237 ipf->ipf_num_dups = 0;
7237 7238 ipfb->ipfb_frag_pkts++;
7238 7239 ipf->ipf_checksum = 0;
7239 7240 ipf->ipf_checksum_flags = 0;
7240 7241
7241 7242 /* Store checksum value in fragment header */
7242 7243 if (sum_flags != 0) {
7243 7244 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7244 7245 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7245 7246 ipf->ipf_checksum = sum_val;
7246 7247 ipf->ipf_checksum_flags = sum_flags;
7247 7248 }
7248 7249
7249 7250 /*
7250 7251 * We handle reassembly two ways. In the easy case,
7251 7252 * where all the fragments show up in order, we do
7252 7253 * minimal bookkeeping, and just clip new pieces on
7253 7254 * the end. If we ever see a hole, then we go off
7254 7255 * to ip_reassemble which has to mark the pieces and
7255 7256 * keep track of the number of holes, etc. Obviously,
7256 7257 * the point of having both mechanisms is so we can
7257 7258 * handle the easy case as efficiently as possible.
7258 7259 */
7259 7260 if (offset == 0) {
7260 7261 /* Easy case, in-order reassembly so far. */
7261 7262 ipf->ipf_count += msg_len;
7262 7263 ipf->ipf_tail_mp = tail_mp;
7263 7264 /*
7264 7265 * Keep track of next expected offset in
7265 7266 * ipf_end.
7266 7267 */
7267 7268 ipf->ipf_end = end;
7268 7269 ipf->ipf_nf_hdr_len = hdr_length;
7269 7270 } else {
7270 7271 /* Hard case, hole at the beginning. */
7271 7272 ipf->ipf_tail_mp = NULL;
7272 7273 /*
7273 7274 * ipf_end == 0 means that we have given up
7274 7275 * on easy reassembly.
7275 7276 */
7276 7277 ipf->ipf_end = 0;
7277 7278
7278 7279 /* Forget checksum offload from now on */
7279 7280 ipf->ipf_checksum_flags = 0;
7280 7281
7281 7282 /*
7282 7283 * ipf_hole_cnt is set by ip_reassemble.
7283 7284 * ipf_count is updated by ip_reassemble.
7284 7285 * No need to check for return value here
7285 7286 * as we don't expect reassembly to complete
7286 7287 * or fail for the first fragment itself.
7287 7288 */
7288 7289 (void) ip_reassemble(mp, ipf,
7289 7290 (frag_offset_flags & IPH_OFFSET) << 3,
7290 7291 (frag_offset_flags & IPH_MF), ill, msg_len);
7291 7292 }
7292 7293 /* Update per ipfb and ill byte counts */
7293 7294 ipfb->ipfb_count += ipf->ipf_count;
7294 7295 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7295 7296 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7296 7297 /* If the frag timer wasn't already going, start it. */
7297 7298 mutex_enter(&ill->ill_lock);
7298 7299 ill_frag_timer_start(ill);
7299 7300 mutex_exit(&ill->ill_lock);
7300 7301 goto reass_done;
7301 7302 }
7302 7303
7303 7304 /*
7304 7305 * If the packet's flag has changed (it could be coming up
7305 7306 * from an interface different than the previous, therefore
7306 7307 * possibly different checksum capability), then forget about
7307 7308 * any stored checksum states. Otherwise add the value to
7308 7309 * the existing one stored in the fragment header.
7309 7310 */
7310 7311 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7311 7312 sum_val += ipf->ipf_checksum;
7312 7313 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7313 7314 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7314 7315 ipf->ipf_checksum = sum_val;
7315 7316 } else if (ipf->ipf_checksum_flags != 0) {
7316 7317 /* Forget checksum offload from now on */
7317 7318 ipf->ipf_checksum_flags = 0;
7318 7319 }
7319 7320
7320 7321 /*
7321 7322 * We have a new piece of a datagram which is already being
7322 7323 * reassembled. Update the ECN info if all IP fragments
7323 7324 * are ECN capable. If there is one which is not, clear
7324 7325 * all the info. If there is at least one which has CE
7325 7326 * code point, IP needs to report that up to transport.
7326 7327 */
7327 7328 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7328 7329 if (ecn_info == IPH_ECN_CE)
7329 7330 ipf->ipf_ecn = IPH_ECN_CE;
7330 7331 } else {
7331 7332 ipf->ipf_ecn = IPH_ECN_NECT;
7332 7333 }
7333 7334 if (offset && ipf->ipf_end == offset) {
7334 7335 /* The new fragment fits at the end */
7335 7336 ipf->ipf_tail_mp->b_cont = mp;
7336 7337 /* Update the byte count */
7337 7338 ipf->ipf_count += msg_len;
7338 7339 /* Update per ipfb and ill byte counts */
7339 7340 ipfb->ipfb_count += msg_len;
7340 7341 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7341 7342 atomic_add_32(&ill->ill_frag_count, msg_len);
7342 7343 if (frag_offset_flags & IPH_MF) {
7343 7344 /* More to come. */
7344 7345 ipf->ipf_end = end;
7345 7346 ipf->ipf_tail_mp = tail_mp;
7346 7347 goto reass_done;
7347 7348 }
7348 7349 } else {
7349 7350 /* Go do the hard cases. */
7350 7351 int ret;
7351 7352
7352 7353 if (offset == 0)
7353 7354 ipf->ipf_nf_hdr_len = hdr_length;
7354 7355
7355 7356 /* Save current byte count */
7356 7357 count = ipf->ipf_count;
7357 7358 ret = ip_reassemble(mp, ipf,
7358 7359 (frag_offset_flags & IPH_OFFSET) << 3,
7359 7360 (frag_offset_flags & IPH_MF), ill, msg_len);
7360 7361 /* Count of bytes added and subtracted (freeb()ed) */
7361 7362 count = ipf->ipf_count - count;
7362 7363 if (count) {
7363 7364 /* Update per ipfb and ill byte counts */
7364 7365 ipfb->ipfb_count += count;
7365 7366 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7366 7367 atomic_add_32(&ill->ill_frag_count, count);
7367 7368 }
7368 7369 if (ret == IP_REASS_PARTIAL) {
7369 7370 goto reass_done;
7370 7371 } else if (ret == IP_REASS_FAILED) {
7371 7372 /* Reassembly failed. Free up all resources */
7372 7373 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7373 7374 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7374 7375 IP_REASS_SET_START(t_mp, 0);
7375 7376 IP_REASS_SET_END(t_mp, 0);
7376 7377 }
7377 7378 freemsg(mp);
7378 7379 goto reass_done;
7379 7380 }
7380 7381 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7381 7382 }
7382 7383 /*
7383 7384 * We have completed reassembly. Unhook the frag header from
7384 7385 * the reassembly list.
7385 7386 *
7386 7387 * Before we free the frag header, record the ECN info
7387 7388 * to report back to the transport.
7388 7389 */
7389 7390 ecn_info = ipf->ipf_ecn;
7390 7391 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7391 7392 ipfp = ipf->ipf_ptphn;
7392 7393
7393 7394 /* We need to supply these to caller */
7394 7395 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7395 7396 sum_val = ipf->ipf_checksum;
7396 7397 else
7397 7398 sum_val = 0;
7398 7399
7399 7400 mp1 = ipf->ipf_mp;
7400 7401 count = ipf->ipf_count;
7401 7402 ipf = ipf->ipf_hash_next;
7402 7403 if (ipf != NULL)
7403 7404 ipf->ipf_ptphn = ipfp;
7404 7405 ipfp[0] = ipf;
7405 7406 atomic_add_32(&ill->ill_frag_count, -count);
7406 7407 ASSERT(ipfb->ipfb_count >= count);
7407 7408 ipfb->ipfb_count -= count;
7408 7409 ipfb->ipfb_frag_pkts--;
7409 7410 mutex_exit(&ipfb->ipfb_lock);
7410 7411 /* Ditch the frag header. */
7411 7412 mp = mp1->b_cont;
7412 7413
7413 7414 freeb(mp1);
7414 7415
7415 7416 /* Restore original IP length in header. */
7416 7417 packet_size = (uint32_t)msgdsize(mp);
7417 7418 if (packet_size > IP_MAXPACKET) {
7418 7419 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7419 7420 ip_drop_input("Reassembled packet too large", mp, ill);
7420 7421 freemsg(mp);
7421 7422 return (NULL);
7422 7423 }
7423 7424
7424 7425 if (DB_REF(mp) > 1) {
7425 7426 mblk_t *mp2 = copymsg(mp);
7426 7427
7427 7428 if (mp2 == NULL) {
7428 7429 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7429 7430 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7430 7431 freemsg(mp);
7431 7432 return (NULL);
7432 7433 }
7433 7434 freemsg(mp);
7434 7435 mp = mp2;
7435 7436 }
7436 7437 ipha = (ipha_t *)mp->b_rptr;
7437 7438
7438 7439 ipha->ipha_length = htons((uint16_t)packet_size);
7439 7440 /* We're now complete, zip the frag state */
7440 7441 ipha->ipha_fragment_offset_and_flags = 0;
7441 7442 /* Record the ECN info. */
7442 7443 ipha->ipha_type_of_service &= 0xFC;
7443 7444 ipha->ipha_type_of_service |= ecn_info;
7444 7445
7445 7446 /* Update the receive attributes */
7446 7447 ira->ira_pktlen = packet_size;
7447 7448 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7448 7449
7449 7450 /* Reassembly is successful; set checksum information in packet */
7450 7451 DB_CKSUM16(mp) = (uint16_t)sum_val;
7451 7452 DB_CKSUMFLAGS(mp) = sum_flags;
7452 7453 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7453 7454
7454 7455 return (mp);
7455 7456 }
7456 7457
7457 7458 /*
7458 7459 * Pullup function that should be used for IP input in order to
7459 7460 * ensure we do not loose the L2 source address; we need the l2 source
7460 7461 * address for IP_RECVSLLA and for ndp_input.
7461 7462 *
7462 7463 * We return either NULL or b_rptr.
7463 7464 */
7464 7465 void *
7465 7466 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7466 7467 {
7467 7468 ill_t *ill = ira->ira_ill;
7468 7469
7469 7470 if (ip_rput_pullups++ == 0) {
7470 7471 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7471 7472 "ip_pullup: %s forced us to "
7472 7473 " pullup pkt, hdr len %ld, hdr addr %p",
7473 7474 ill->ill_name, len, (void *)mp->b_rptr);
7474 7475 }
7475 7476 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7476 7477 ip_setl2src(mp, ira, ira->ira_rill);
7477 7478 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7478 7479 if (!pullupmsg(mp, len))
7479 7480 return (NULL);
7480 7481 else
7481 7482 return (mp->b_rptr);
7482 7483 }
7483 7484
7484 7485 /*
7485 7486 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7486 7487 * When called from the ULP ira_rill will be NULL hence the caller has to
7487 7488 * pass in the ill.
7488 7489 */
7489 7490 /* ARGSUSED */
7490 7491 void
7491 7492 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7492 7493 {
7493 7494 const uchar_t *addr;
7494 7495 int alen;
7495 7496
7496 7497 if (ira->ira_flags & IRAF_L2SRC_SET)
7497 7498 return;
7498 7499
7499 7500 ASSERT(ill != NULL);
7500 7501 alen = ill->ill_phys_addr_length;
7501 7502 ASSERT(alen <= sizeof (ira->ira_l2src));
7502 7503 if (ira->ira_mhip != NULL &&
7503 7504 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7504 7505 bcopy(addr, ira->ira_l2src, alen);
7505 7506 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7506 7507 (addr = ill->ill_phys_addr) != NULL) {
7507 7508 bcopy(addr, ira->ira_l2src, alen);
7508 7509 } else {
7509 7510 bzero(ira->ira_l2src, alen);
7510 7511 }
7511 7512 ira->ira_flags |= IRAF_L2SRC_SET;
7512 7513 }
7513 7514
7514 7515 /*
7515 7516 * check ip header length and align it.
7516 7517 */
7517 7518 mblk_t *
7518 7519 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7519 7520 {
7520 7521 ill_t *ill = ira->ira_ill;
7521 7522 ssize_t len;
7522 7523
7523 7524 len = MBLKL(mp);
7524 7525
7525 7526 if (!OK_32PTR(mp->b_rptr))
7526 7527 IP_STAT(ill->ill_ipst, ip_notaligned);
7527 7528 else
7528 7529 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7529 7530
7530 7531 /* Guard against bogus device drivers */
7531 7532 if (len < 0) {
7532 7533 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7533 7534 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7534 7535 freemsg(mp);
7535 7536 return (NULL);
7536 7537 }
7537 7538
7538 7539 if (len == 0) {
7539 7540 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7540 7541 mblk_t *mp1 = mp->b_cont;
7541 7542
7542 7543 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7543 7544 ip_setl2src(mp, ira, ira->ira_rill);
7544 7545 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7545 7546
7546 7547 freeb(mp);
7547 7548 mp = mp1;
7548 7549 if (mp == NULL)
7549 7550 return (NULL);
7550 7551
7551 7552 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7552 7553 return (mp);
7553 7554 }
7554 7555 if (ip_pullup(mp, min_size, ira) == NULL) {
7555 7556 if (msgdsize(mp) < min_size) {
7556 7557 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7557 7558 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7558 7559 } else {
7559 7560 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7560 7561 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7561 7562 }
7562 7563 freemsg(mp);
7563 7564 return (NULL);
7564 7565 }
7565 7566 return (mp);
7566 7567 }
7567 7568
7568 7569 /*
7569 7570 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7570 7571 */
7571 7572 mblk_t *
7572 7573 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7573 7574 uint_t min_size, ip_recv_attr_t *ira)
7574 7575 {
7575 7576 ill_t *ill = ira->ira_ill;
7576 7577
7577 7578 /*
7578 7579 * Make sure we have data length consistent
7579 7580 * with the IP header.
7580 7581 */
7581 7582 if (mp->b_cont == NULL) {
7582 7583 /* pkt_len is based on ipha_len, not the mblk length */
7583 7584 if (pkt_len < min_size) {
7584 7585 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7585 7586 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7586 7587 freemsg(mp);
7587 7588 return (NULL);
7588 7589 }
7589 7590 if (len < 0) {
7590 7591 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7591 7592 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7592 7593 freemsg(mp);
7593 7594 return (NULL);
7594 7595 }
7595 7596 /* Drop any pad */
7596 7597 mp->b_wptr = rptr + pkt_len;
7597 7598 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7598 7599 ASSERT(pkt_len >= min_size);
7599 7600 if (pkt_len < min_size) {
7600 7601 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7601 7602 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7602 7603 freemsg(mp);
7603 7604 return (NULL);
7604 7605 }
7605 7606 if (len < 0) {
7606 7607 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7607 7608 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7608 7609 freemsg(mp);
7609 7610 return (NULL);
7610 7611 }
7611 7612 /* Drop any pad */
7612 7613 (void) adjmsg(mp, -len);
7613 7614 /*
7614 7615 * adjmsg may have freed an mblk from the chain, hence
7615 7616 * invalidate any hw checksum here. This will force IP to
7616 7617 * calculate the checksum in sw, but only for this packet.
7617 7618 */
7618 7619 DB_CKSUMFLAGS(mp) = 0;
7619 7620 IP_STAT(ill->ill_ipst, ip_multimblk);
7620 7621 }
7621 7622 return (mp);
7622 7623 }
7623 7624
7624 7625 /*
7625 7626 * Check that the IPv4 opt_len is consistent with the packet and pullup
7626 7627 * the options.
7627 7628 */
7628 7629 mblk_t *
7629 7630 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7630 7631 ip_recv_attr_t *ira)
7631 7632 {
7632 7633 ill_t *ill = ira->ira_ill;
7633 7634 ssize_t len;
7634 7635
7635 7636 /* Assume no IPv6 packets arrive over the IPv4 queue */
7636 7637 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7637 7638 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7638 7639 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7639 7640 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7640 7641 freemsg(mp);
7641 7642 return (NULL);
7642 7643 }
7643 7644
7644 7645 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7645 7646 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7646 7647 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7647 7648 freemsg(mp);
7648 7649 return (NULL);
7649 7650 }
7650 7651 /*
7651 7652 * Recompute complete header length and make sure we
7652 7653 * have access to all of it.
7653 7654 */
7654 7655 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7655 7656 if (len > (mp->b_wptr - mp->b_rptr)) {
7656 7657 if (len > pkt_len) {
7657 7658 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7658 7659 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7659 7660 freemsg(mp);
7660 7661 return (NULL);
7661 7662 }
7662 7663 if (ip_pullup(mp, len, ira) == NULL) {
7663 7664 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7664 7665 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7665 7666 freemsg(mp);
7666 7667 return (NULL);
7667 7668 }
7668 7669 }
7669 7670 return (mp);
7670 7671 }
7671 7672
7672 7673 /*
7673 7674 * Returns a new ire, or the same ire, or NULL.
7674 7675 * If a different IRE is returned, then it is held; the caller
7675 7676 * needs to release it.
7676 7677 * In no case is there any hold/release on the ire argument.
7677 7678 */
7678 7679 ire_t *
7679 7680 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7680 7681 {
7681 7682 ire_t *new_ire;
7682 7683 ill_t *ire_ill;
7683 7684 uint_t ifindex;
7684 7685 ip_stack_t *ipst = ill->ill_ipst;
7685 7686 boolean_t strict_check = B_FALSE;
7686 7687
7687 7688 /*
7688 7689 * IPMP common case: if IRE and ILL are in the same group, there's no
7689 7690 * issue (e.g. packet received on an underlying interface matched an
7690 7691 * IRE_LOCAL on its associated group interface).
7691 7692 */
7692 7693 ASSERT(ire->ire_ill != NULL);
7693 7694 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7694 7695 return (ire);
7695 7696
7696 7697 /*
7697 7698 * Do another ire lookup here, using the ingress ill, to see if the
7698 7699 * interface is in a usesrc group.
7699 7700 * As long as the ills belong to the same group, we don't consider
7700 7701 * them to be arriving on the wrong interface. Thus, if the switch
7701 7702 * is doing inbound load spreading, we won't drop packets when the
7702 7703 * ip*_strict_dst_multihoming switch is on.
7703 7704 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7704 7705 * where the local address may not be unique. In this case we were
7705 7706 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7706 7707 * actually returned. The new lookup, which is more specific, should
7707 7708 * only find the IRE_LOCAL associated with the ingress ill if one
7708 7709 * exists.
7709 7710 */
7710 7711 if (ire->ire_ipversion == IPV4_VERSION) {
7711 7712 if (ipst->ips_ip_strict_dst_multihoming)
7712 7713 strict_check = B_TRUE;
7713 7714 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7714 7715 IRE_LOCAL, ill, ALL_ZONES, NULL,
7715 7716 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7716 7717 } else {
7717 7718 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7718 7719 if (ipst->ips_ipv6_strict_dst_multihoming)
7719 7720 strict_check = B_TRUE;
7720 7721 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7721 7722 IRE_LOCAL, ill, ALL_ZONES, NULL,
7722 7723 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7723 7724 }
7724 7725 /*
7725 7726 * If the same ire that was returned in ip_input() is found then this
7726 7727 * is an indication that usesrc groups are in use. The packet
7727 7728 * arrived on a different ill in the group than the one associated with
7728 7729 * the destination address. If a different ire was found then the same
7729 7730 * IP address must be hosted on multiple ills. This is possible with
7730 7731 * unnumbered point2point interfaces. We switch to use this new ire in
7731 7732 * order to have accurate interface statistics.
7732 7733 */
7733 7734 if (new_ire != NULL) {
7734 7735 /* Note: held in one case but not the other? Caller handles */
7735 7736 if (new_ire != ire)
7736 7737 return (new_ire);
7737 7738 /* Unchanged */
7738 7739 ire_refrele(new_ire);
7739 7740 return (ire);
7740 7741 }
7741 7742
7742 7743 /*
7743 7744 * Chase pointers once and store locally.
7744 7745 */
7745 7746 ASSERT(ire->ire_ill != NULL);
7746 7747 ire_ill = ire->ire_ill;
7747 7748 ifindex = ill->ill_usesrc_ifindex;
7748 7749
7749 7750 /*
7750 7751 * Check if it's a legal address on the 'usesrc' interface.
7751 7752 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7752 7753 * can just check phyint_ifindex.
7753 7754 */
7754 7755 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7755 7756 return (ire);
7756 7757 }
7757 7758
7758 7759 /*
7759 7760 * If the ip*_strict_dst_multihoming switch is on then we can
7760 7761 * only accept this packet if the interface is marked as routing.
7761 7762 */
7762 7763 if (!(strict_check))
7763 7764 return (ire);
7764 7765
7765 7766 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7766 7767 return (ire);
7767 7768 }
7768 7769 return (NULL);
7769 7770 }
7770 7771
7771 7772 /*
7772 7773 * This function is used to construct a mac_header_info_s from a
7773 7774 * DL_UNITDATA_IND message.
7774 7775 * The address fields in the mhi structure points into the message,
7775 7776 * thus the caller can't use those fields after freeing the message.
7776 7777 *
7777 7778 * We determine whether the packet received is a non-unicast packet
7778 7779 * and in doing so, determine whether or not it is broadcast vs multicast.
7779 7780 * For it to be a broadcast packet, we must have the appropriate mblk_t
7780 7781 * hanging off the ill_t. If this is either not present or doesn't match
7781 7782 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7782 7783 * to be multicast. Thus NICs that have no broadcast address (or no
7783 7784 * capability for one, such as point to point links) cannot return as
7784 7785 * the packet being broadcast.
7785 7786 */
7786 7787 void
7787 7788 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7788 7789 {
7789 7790 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7790 7791 mblk_t *bmp;
7791 7792 uint_t extra_offset;
7792 7793
7793 7794 bzero(mhip, sizeof (struct mac_header_info_s));
7794 7795
7795 7796 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7796 7797
7797 7798 if (ill->ill_sap_length < 0)
7798 7799 extra_offset = 0;
7799 7800 else
7800 7801 extra_offset = ill->ill_sap_length;
7801 7802
7802 7803 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7803 7804 extra_offset;
7804 7805 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7805 7806 extra_offset;
7806 7807
7807 7808 if (!ind->dl_group_address)
7808 7809 return;
7809 7810
7810 7811 /* Multicast or broadcast */
7811 7812 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7812 7813
7813 7814 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7814 7815 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7815 7816 (bmp = ill->ill_bcast_mp) != NULL) {
7816 7817 dl_unitdata_req_t *dlur;
7817 7818 uint8_t *bphys_addr;
7818 7819
7819 7820 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7820 7821 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7821 7822 extra_offset;
7822 7823
7823 7824 if (bcmp(mhip->mhi_daddr, bphys_addr,
7824 7825 ind->dl_dest_addr_length) == 0)
7825 7826 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7826 7827 }
7827 7828 }
7828 7829
7829 7830 /*
7830 7831 * This function is used to construct a mac_header_info_s from a
7831 7832 * M_DATA fastpath message from a DLPI driver.
7832 7833 * The address fields in the mhi structure points into the message,
7833 7834 * thus the caller can't use those fields after freeing the message.
7834 7835 *
7835 7836 * We determine whether the packet received is a non-unicast packet
7836 7837 * and in doing so, determine whether or not it is broadcast vs multicast.
7837 7838 * For it to be a broadcast packet, we must have the appropriate mblk_t
7838 7839 * hanging off the ill_t. If this is either not present or doesn't match
7839 7840 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7840 7841 * to be multicast. Thus NICs that have no broadcast address (or no
7841 7842 * capability for one, such as point to point links) cannot return as
7842 7843 * the packet being broadcast.
7843 7844 */
7844 7845 void
7845 7846 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7846 7847 {
7847 7848 mblk_t *bmp;
7848 7849 struct ether_header *pether;
7849 7850
7850 7851 bzero(mhip, sizeof (struct mac_header_info_s));
7851 7852
7852 7853 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7853 7854
7854 7855 pether = (struct ether_header *)((char *)mp->b_rptr
7855 7856 - sizeof (struct ether_header));
7856 7857
7857 7858 /*
7858 7859 * Make sure the interface is an ethernet type, since we don't
7859 7860 * know the header format for anything but Ethernet. Also make
7860 7861 * sure we are pointing correctly above db_base.
7861 7862 */
7862 7863 if (ill->ill_type != IFT_ETHER)
7863 7864 return;
7864 7865
7865 7866 retry:
7866 7867 if ((uchar_t *)pether < mp->b_datap->db_base)
7867 7868 return;
7868 7869
7869 7870 /* Is there a VLAN tag? */
7870 7871 if (ill->ill_isv6) {
7871 7872 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7872 7873 pether = (struct ether_header *)((char *)pether - 4);
7873 7874 goto retry;
7874 7875 }
7875 7876 } else {
7876 7877 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7877 7878 pether = (struct ether_header *)((char *)pether - 4);
7878 7879 goto retry;
7879 7880 }
7880 7881 }
7881 7882 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7882 7883 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7883 7884
7884 7885 if (!(mhip->mhi_daddr[0] & 0x01))
7885 7886 return;
7886 7887
7887 7888 /* Multicast or broadcast */
7888 7889 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7889 7890
7890 7891 if ((bmp = ill->ill_bcast_mp) != NULL) {
7891 7892 dl_unitdata_req_t *dlur;
7892 7893 uint8_t *bphys_addr;
7893 7894 uint_t addrlen;
7894 7895
7895 7896 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7896 7897 addrlen = dlur->dl_dest_addr_length;
7897 7898 if (ill->ill_sap_length < 0) {
7898 7899 bphys_addr = (uchar_t *)dlur +
7899 7900 dlur->dl_dest_addr_offset;
7900 7901 addrlen += ill->ill_sap_length;
7901 7902 } else {
7902 7903 bphys_addr = (uchar_t *)dlur +
7903 7904 dlur->dl_dest_addr_offset +
7904 7905 ill->ill_sap_length;
7905 7906 addrlen -= ill->ill_sap_length;
7906 7907 }
7907 7908 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7908 7909 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7909 7910 }
7910 7911 }
7911 7912
7912 7913 /*
7913 7914 * Handle anything but M_DATA messages
7914 7915 * We see the DL_UNITDATA_IND which are part
7915 7916 * of the data path, and also the other messages from the driver.
7916 7917 */
7917 7918 void
7918 7919 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7919 7920 {
7920 7921 mblk_t *first_mp;
7921 7922 struct iocblk *iocp;
7922 7923 struct mac_header_info_s mhi;
7923 7924
7924 7925 switch (DB_TYPE(mp)) {
7925 7926 case M_PROTO:
7926 7927 case M_PCPROTO: {
7927 7928 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7928 7929 DL_UNITDATA_IND) {
7929 7930 /* Go handle anything other than data elsewhere. */
7930 7931 ip_rput_dlpi(ill, mp);
7931 7932 return;
7932 7933 }
7933 7934
7934 7935 first_mp = mp;
7935 7936 mp = first_mp->b_cont;
7936 7937 first_mp->b_cont = NULL;
7937 7938
7938 7939 if (mp == NULL) {
7939 7940 freeb(first_mp);
7940 7941 return;
7941 7942 }
7942 7943 ip_dlur_to_mhi(ill, first_mp, &mhi);
7943 7944 if (ill->ill_isv6)
7944 7945 ip_input_v6(ill, NULL, mp, &mhi);
7945 7946 else
7946 7947 ip_input(ill, NULL, mp, &mhi);
7947 7948
7948 7949 /* Ditch the DLPI header. */
7949 7950 freeb(first_mp);
7950 7951 return;
7951 7952 }
7952 7953 case M_IOCACK:
7953 7954 iocp = (struct iocblk *)mp->b_rptr;
7954 7955 switch (iocp->ioc_cmd) {
7955 7956 case DL_IOC_HDR_INFO:
7956 7957 ill_fastpath_ack(ill, mp);
7957 7958 return;
7958 7959 default:
7959 7960 putnext(ill->ill_rq, mp);
7960 7961 return;
7961 7962 }
7962 7963 /* FALLTHROUGH */
7963 7964 case M_ERROR:
7964 7965 case M_HANGUP:
7965 7966 mutex_enter(&ill->ill_lock);
7966 7967 if (ill->ill_state_flags & ILL_CONDEMNED) {
7967 7968 mutex_exit(&ill->ill_lock);
7968 7969 freemsg(mp);
7969 7970 return;
7970 7971 }
7971 7972 ill_refhold_locked(ill);
7972 7973 mutex_exit(&ill->ill_lock);
7973 7974 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7974 7975 B_FALSE);
7975 7976 return;
7976 7977 case M_CTL:
7977 7978 putnext(ill->ill_rq, mp);
7978 7979 return;
7979 7980 case M_IOCNAK:
7980 7981 ip1dbg(("got iocnak "));
7981 7982 iocp = (struct iocblk *)mp->b_rptr;
7982 7983 switch (iocp->ioc_cmd) {
7983 7984 case DL_IOC_HDR_INFO:
7984 7985 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7985 7986 return;
7986 7987 default:
7987 7988 break;
7988 7989 }
7989 7990 /* FALLTHROUGH */
7990 7991 default:
7991 7992 putnext(ill->ill_rq, mp);
7992 7993 return;
7993 7994 }
7994 7995 }
7995 7996
7996 7997 /* Read side put procedure. Packets coming from the wire arrive here. */
7997 7998 int
7998 7999 ip_rput(queue_t *q, mblk_t *mp)
7999 8000 {
8000 8001 ill_t *ill;
8001 8002 union DL_primitives *dl;
8002 8003
8003 8004 ill = (ill_t *)q->q_ptr;
8004 8005
8005 8006 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
8006 8007 /*
8007 8008 * If things are opening or closing, only accept high-priority
8008 8009 * DLPI messages. (On open ill->ill_ipif has not yet been
8009 8010 * created; on close, things hanging off the ill may have been
8010 8011 * freed already.)
8011 8012 */
8012 8013 dl = (union DL_primitives *)mp->b_rptr;
8013 8014 if (DB_TYPE(mp) != M_PCPROTO ||
8014 8015 dl->dl_primitive == DL_UNITDATA_IND) {
8015 8016 inet_freemsg(mp);
8016 8017 return (0);
8017 8018 }
8018 8019 }
8019 8020 if (DB_TYPE(mp) == M_DATA) {
8020 8021 struct mac_header_info_s mhi;
8021 8022
8022 8023 ip_mdata_to_mhi(ill, mp, &mhi);
8023 8024 ip_input(ill, NULL, mp, &mhi);
8024 8025 } else {
8025 8026 ip_rput_notdata(ill, mp);
8026 8027 }
8027 8028 return (0);
8028 8029 }
8029 8030
8030 8031 /*
8031 8032 * Move the information to a copy.
8032 8033 */
8033 8034 mblk_t *
8034 8035 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8035 8036 {
8036 8037 mblk_t *mp1;
8037 8038 ill_t *ill = ira->ira_ill;
8038 8039 ip_stack_t *ipst = ill->ill_ipst;
8039 8040
8040 8041 IP_STAT(ipst, ip_db_ref);
8041 8042
8042 8043 /* Make sure we have ira_l2src before we loose the original mblk */
8043 8044 if (!(ira->ira_flags & IRAF_L2SRC_SET))
8044 8045 ip_setl2src(mp, ira, ira->ira_rill);
8045 8046
8046 8047 mp1 = copymsg(mp);
8047 8048 if (mp1 == NULL) {
8048 8049 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8049 8050 ip_drop_input("ipIfStatsInDiscards", mp, ill);
8050 8051 freemsg(mp);
8051 8052 return (NULL);
8052 8053 }
8053 8054 /* preserve the hardware checksum flags and data, if present */
8054 8055 if (DB_CKSUMFLAGS(mp) != 0) {
8055 8056 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8056 8057 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8057 8058 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8058 8059 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8059 8060 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8060 8061 }
8061 8062 freemsg(mp);
8062 8063 return (mp1);
8063 8064 }
8064 8065
8065 8066 static void
8066 8067 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8067 8068 t_uscalar_t err)
8068 8069 {
8069 8070 if (dl_err == DL_SYSERR) {
8070 8071 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8071 8072 "%s: %s failed: DL_SYSERR (errno %u)\n",
8072 8073 ill->ill_name, dl_primstr(prim), err);
8073 8074 return;
8074 8075 }
8075 8076
8076 8077 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8077 8078 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8078 8079 dl_errstr(dl_err));
8079 8080 }
8080 8081
8081 8082 /*
8082 8083 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8083 8084 * than DL_UNITDATA_IND messages. If we need to process this message
8084 8085 * exclusively, we call qwriter_ip, in which case we also need to call
8085 8086 * ill_refhold before that, since qwriter_ip does an ill_refrele.
8086 8087 */
8087 8088 void
8088 8089 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8089 8090 {
8090 8091 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8091 8092 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8092 8093 queue_t *q = ill->ill_rq;
8093 8094 t_uscalar_t prim = dloa->dl_primitive;
8094 8095 t_uscalar_t reqprim = DL_PRIM_INVAL;
8095 8096
8096 8097 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8097 8098 char *, dl_primstr(prim), ill_t *, ill);
8098 8099 ip1dbg(("ip_rput_dlpi"));
8099 8100
8100 8101 /*
8101 8102 * If we received an ACK but didn't send a request for it, then it
8102 8103 * can't be part of any pending operation; discard up-front.
8103 8104 */
8104 8105 switch (prim) {
8105 8106 case DL_ERROR_ACK:
8106 8107 reqprim = dlea->dl_error_primitive;
8107 8108 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8108 8109 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8109 8110 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8110 8111 dlea->dl_unix_errno));
8111 8112 break;
8112 8113 case DL_OK_ACK:
8113 8114 reqprim = dloa->dl_correct_primitive;
8114 8115 break;
8115 8116 case DL_INFO_ACK:
8116 8117 reqprim = DL_INFO_REQ;
8117 8118 break;
8118 8119 case DL_BIND_ACK:
8119 8120 reqprim = DL_BIND_REQ;
8120 8121 break;
8121 8122 case DL_PHYS_ADDR_ACK:
8122 8123 reqprim = DL_PHYS_ADDR_REQ;
8123 8124 break;
8124 8125 case DL_NOTIFY_ACK:
8125 8126 reqprim = DL_NOTIFY_REQ;
8126 8127 break;
8127 8128 case DL_CAPABILITY_ACK:
8128 8129 reqprim = DL_CAPABILITY_REQ;
8129 8130 break;
8130 8131 }
8131 8132
8132 8133 if (prim != DL_NOTIFY_IND) {
8133 8134 if (reqprim == DL_PRIM_INVAL ||
8134 8135 !ill_dlpi_pending(ill, reqprim)) {
8135 8136 /* Not a DLPI message we support or expected */
8136 8137 freemsg(mp);
8137 8138 return;
8138 8139 }
8139 8140 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8140 8141 dl_primstr(reqprim)));
8141 8142 }
8142 8143
8143 8144 switch (reqprim) {
8144 8145 case DL_UNBIND_REQ:
8145 8146 /*
8146 8147 * NOTE: we mark the unbind as complete even if we got a
8147 8148 * DL_ERROR_ACK, since there's not much else we can do.
8148 8149 */
8149 8150 mutex_enter(&ill->ill_lock);
8150 8151 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8151 8152 cv_signal(&ill->ill_cv);
8152 8153 mutex_exit(&ill->ill_lock);
8153 8154 break;
8154 8155
8155 8156 case DL_ENABMULTI_REQ:
8156 8157 if (prim == DL_OK_ACK) {
8157 8158 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8158 8159 ill->ill_dlpi_multicast_state = IDS_OK;
8159 8160 }
8160 8161 break;
8161 8162 }
8162 8163
8163 8164 /*
8164 8165 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8165 8166 * need to become writer to continue to process it. Because an
8166 8167 * exclusive operation doesn't complete until replies to all queued
8167 8168 * DLPI messages have been received, we know we're in the middle of an
8168 8169 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8169 8170 *
8170 8171 * As required by qwriter_ip(), we refhold the ill; it will refrele.
8171 8172 * Since this is on the ill stream we unconditionally bump up the
8172 8173 * refcount without doing ILL_CAN_LOOKUP().
8173 8174 */
8174 8175 ill_refhold(ill);
8175 8176 if (prim == DL_NOTIFY_IND)
8176 8177 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8177 8178 else
8178 8179 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8179 8180 }
8180 8181
8181 8182 /*
8182 8183 * Handling of DLPI messages that require exclusive access to the ipsq.
8183 8184 *
8184 8185 * Need to do ipsq_pending_mp_get on ioctl completion, which could
8185 8186 * happen here. (along with mi_copy_done)
8186 8187 */
8187 8188 /* ARGSUSED */
8188 8189 static void
8189 8190 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8190 8191 {
8191 8192 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8192 8193 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8193 8194 int err = 0;
8194 8195 ill_t *ill = (ill_t *)q->q_ptr;
8195 8196 ipif_t *ipif = NULL;
8196 8197 mblk_t *mp1 = NULL;
8197 8198 conn_t *connp = NULL;
8198 8199 t_uscalar_t paddrreq;
8199 8200 mblk_t *mp_hw;
8200 8201 boolean_t success;
8201 8202 boolean_t ioctl_aborted = B_FALSE;
8202 8203 boolean_t log = B_TRUE;
8203 8204
8204 8205 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8205 8206 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8206 8207
8207 8208 ip1dbg(("ip_rput_dlpi_writer .."));
8208 8209 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8209 8210 ASSERT(IAM_WRITER_ILL(ill));
8210 8211
8211 8212 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8212 8213 /*
8213 8214 * The current ioctl could have been aborted by the user and a new
8214 8215 * ioctl to bring up another ill could have started. We could still
8215 8216 * get a response from the driver later.
8216 8217 */
8217 8218 if (ipif != NULL && ipif->ipif_ill != ill)
8218 8219 ioctl_aborted = B_TRUE;
8219 8220
8220 8221 switch (dloa->dl_primitive) {
8221 8222 case DL_ERROR_ACK:
8222 8223 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8223 8224 dl_primstr(dlea->dl_error_primitive)));
8224 8225
8225 8226 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8226 8227 char *, dl_primstr(dlea->dl_error_primitive),
8227 8228 ill_t *, ill);
8228 8229
8229 8230 switch (dlea->dl_error_primitive) {
8230 8231 case DL_DISABMULTI_REQ:
8231 8232 ill_dlpi_done(ill, dlea->dl_error_primitive);
8232 8233 break;
8233 8234 case DL_PROMISCON_REQ:
8234 8235 case DL_PROMISCOFF_REQ:
8235 8236 case DL_UNBIND_REQ:
8236 8237 case DL_ATTACH_REQ:
8237 8238 case DL_INFO_REQ:
8238 8239 ill_dlpi_done(ill, dlea->dl_error_primitive);
8239 8240 break;
8240 8241 case DL_NOTIFY_REQ:
8241 8242 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8242 8243 log = B_FALSE;
8243 8244 break;
8244 8245 case DL_PHYS_ADDR_REQ:
8245 8246 /*
8246 8247 * For IPv6 only, there are two additional
8247 8248 * phys_addr_req's sent to the driver to get the
8248 8249 * IPv6 token and lla. This allows IP to acquire
8249 8250 * the hardware address format for a given interface
8250 8251 * without having built in knowledge of the hardware
8251 8252 * address. ill_phys_addr_pend keeps track of the last
8252 8253 * DL_PAR sent so we know which response we are
8253 8254 * dealing with. ill_dlpi_done will update
8254 8255 * ill_phys_addr_pend when it sends the next req.
8255 8256 * We don't complete the IOCTL until all three DL_PARs
8256 8257 * have been attempted, so set *_len to 0 and break.
8257 8258 */
8258 8259 paddrreq = ill->ill_phys_addr_pend;
8259 8260 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8260 8261 if (paddrreq == DL_IPV6_TOKEN) {
8261 8262 ill->ill_token_length = 0;
8262 8263 log = B_FALSE;
8263 8264 break;
8264 8265 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8265 8266 ill->ill_nd_lla_len = 0;
8266 8267 log = B_FALSE;
8267 8268 break;
8268 8269 }
8269 8270 /*
8270 8271 * Something went wrong with the DL_PHYS_ADDR_REQ.
8271 8272 * We presumably have an IOCTL hanging out waiting
8272 8273 * for completion. Find it and complete the IOCTL
8273 8274 * with the error noted.
8274 8275 * However, ill_dl_phys was called on an ill queue
8275 8276 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8276 8277 * set. But the ioctl is known to be pending on ill_wq.
8277 8278 */
8278 8279 if (!ill->ill_ifname_pending)
8279 8280 break;
8280 8281 ill->ill_ifname_pending = 0;
8281 8282 if (!ioctl_aborted)
8282 8283 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8283 8284 if (mp1 != NULL) {
8284 8285 /*
8285 8286 * This operation (SIOCSLIFNAME) must have
8286 8287 * happened on the ill. Assert there is no conn
8287 8288 */
8288 8289 ASSERT(connp == NULL);
8289 8290 q = ill->ill_wq;
8290 8291 }
8291 8292 break;
8292 8293 case DL_BIND_REQ:
8293 8294 ill_dlpi_done(ill, DL_BIND_REQ);
8294 8295 if (ill->ill_ifname_pending)
8295 8296 break;
8296 8297 mutex_enter(&ill->ill_lock);
8297 8298 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8298 8299 mutex_exit(&ill->ill_lock);
8299 8300 /*
8300 8301 * Something went wrong with the bind. We presumably
8301 8302 * have an IOCTL hanging out waiting for completion.
8302 8303 * Find it, take down the interface that was coming
8303 8304 * up, and complete the IOCTL with the error noted.
8304 8305 */
8305 8306 if (!ioctl_aborted)
8306 8307 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8307 8308 if (mp1 != NULL) {
8308 8309 /*
8309 8310 * This might be a result of a DL_NOTE_REPLUMB
8310 8311 * notification. In that case, connp is NULL.
8311 8312 */
8312 8313 if (connp != NULL)
8313 8314 q = CONNP_TO_WQ(connp);
8314 8315
8315 8316 (void) ipif_down(ipif, NULL, NULL);
8316 8317 /* error is set below the switch */
8317 8318 }
8318 8319 break;
8319 8320 case DL_ENABMULTI_REQ:
8320 8321 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8321 8322
8322 8323 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8323 8324 ill->ill_dlpi_multicast_state = IDS_FAILED;
8324 8325 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8325 8326
8326 8327 printf("ip: joining multicasts failed (%d)"
8327 8328 " on %s - will use link layer "
8328 8329 "broadcasts for multicast\n",
8329 8330 dlea->dl_errno, ill->ill_name);
8330 8331
8331 8332 /*
8332 8333 * Set up for multi_bcast; We are the
8333 8334 * writer, so ok to access ill->ill_ipif
8334 8335 * without any lock.
8335 8336 */
8336 8337 mutex_enter(&ill->ill_phyint->phyint_lock);
8337 8338 ill->ill_phyint->phyint_flags |=
8338 8339 PHYI_MULTI_BCAST;
8339 8340 mutex_exit(&ill->ill_phyint->phyint_lock);
8340 8341
8341 8342 }
8342 8343 freemsg(mp); /* Don't want to pass this up */
8343 8344 return;
8344 8345 case DL_CAPABILITY_REQ:
8345 8346 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8346 8347 "DL_CAPABILITY REQ\n"));
8347 8348 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8348 8349 ill->ill_dlpi_capab_state = IDCS_FAILED;
8349 8350 ill_capability_done(ill);
8350 8351 freemsg(mp);
8351 8352 return;
8352 8353 }
8353 8354 /*
8354 8355 * Note the error for IOCTL completion (mp1 is set when
8355 8356 * ready to complete ioctl). If ill_ifname_pending_err is
8356 8357 * set, an error occured during plumbing (ill_ifname_pending),
8357 8358 * so we want to report that error.
8358 8359 *
8359 8360 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8360 8361 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8361 8362 * expected to get errack'd if the driver doesn't support
8362 8363 * these flags (e.g. ethernet). log will be set to B_FALSE
8363 8364 * if these error conditions are encountered.
8364 8365 */
8365 8366 if (mp1 != NULL) {
8366 8367 if (ill->ill_ifname_pending_err != 0) {
8367 8368 err = ill->ill_ifname_pending_err;
8368 8369 ill->ill_ifname_pending_err = 0;
8369 8370 } else {
8370 8371 err = dlea->dl_unix_errno ?
8371 8372 dlea->dl_unix_errno : ENXIO;
8372 8373 }
8373 8374 /*
8374 8375 * If we're plumbing an interface and an error hasn't already
8375 8376 * been saved, set ill_ifname_pending_err to the error passed
8376 8377 * up. Ignore the error if log is B_FALSE (see comment above).
8377 8378 */
8378 8379 } else if (log && ill->ill_ifname_pending &&
8379 8380 ill->ill_ifname_pending_err == 0) {
8380 8381 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8381 8382 dlea->dl_unix_errno : ENXIO;
8382 8383 }
8383 8384
8384 8385 if (log)
8385 8386 ip_dlpi_error(ill, dlea->dl_error_primitive,
8386 8387 dlea->dl_errno, dlea->dl_unix_errno);
8387 8388 break;
8388 8389 case DL_CAPABILITY_ACK:
8389 8390 ill_capability_ack(ill, mp);
8390 8391 /*
8391 8392 * The message has been handed off to ill_capability_ack
8392 8393 * and must not be freed below
8393 8394 */
8394 8395 mp = NULL;
8395 8396 break;
8396 8397
8397 8398 case DL_INFO_ACK:
8398 8399 /* Call a routine to handle this one. */
8399 8400 ill_dlpi_done(ill, DL_INFO_REQ);
8400 8401 ip_ll_subnet_defaults(ill, mp);
8401 8402 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8402 8403 return;
8403 8404 case DL_BIND_ACK:
8404 8405 /*
8405 8406 * We should have an IOCTL waiting on this unless
8406 8407 * sent by ill_dl_phys, in which case just return
8407 8408 */
8408 8409 ill_dlpi_done(ill, DL_BIND_REQ);
8409 8410
8410 8411 if (ill->ill_ifname_pending) {
8411 8412 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8412 8413 ill_t *, ill, mblk_t *, mp);
8413 8414 break;
8414 8415 }
8415 8416 mutex_enter(&ill->ill_lock);
8416 8417 ill->ill_dl_up = 1;
8417 8418 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8418 8419 mutex_exit(&ill->ill_lock);
8419 8420
8420 8421 if (!ioctl_aborted)
8421 8422 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8422 8423 if (mp1 == NULL) {
8423 8424 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8424 8425 break;
8425 8426 }
8426 8427 /*
8427 8428 * mp1 was added by ill_dl_up(). if that is a result of
8428 8429 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8429 8430 */
8430 8431 if (connp != NULL)
8431 8432 q = CONNP_TO_WQ(connp);
8432 8433 /*
8433 8434 * We are exclusive. So nothing can change even after
8434 8435 * we get the pending mp.
8435 8436 */
8436 8437 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8437 8438 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8438 8439 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8439 8440
8440 8441 /*
8441 8442 * Now bring up the resolver; when that is complete, we'll
8442 8443 * create IREs. Note that we intentionally mirror what
8443 8444 * ipif_up() would have done, because we got here by way of
8444 8445 * ill_dl_up(), which stopped ipif_up()'s processing.
8445 8446 */
8446 8447 if (ill->ill_isv6) {
8447 8448 /*
8448 8449 * v6 interfaces.
8449 8450 * Unlike ARP which has to do another bind
8450 8451 * and attach, once we get here we are
8451 8452 * done with NDP
8452 8453 */
8453 8454 (void) ipif_resolver_up(ipif, Res_act_initial);
8454 8455 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8455 8456 err = ipif_up_done_v6(ipif);
8456 8457 } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8457 8458 /*
8458 8459 * ARP and other v4 external resolvers.
8459 8460 * Leave the pending mblk intact so that
8460 8461 * the ioctl completes in ip_rput().
8461 8462 */
8462 8463 if (connp != NULL)
8463 8464 mutex_enter(&connp->conn_lock);
8464 8465 mutex_enter(&ill->ill_lock);
8465 8466 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8466 8467 mutex_exit(&ill->ill_lock);
8467 8468 if (connp != NULL)
8468 8469 mutex_exit(&connp->conn_lock);
8469 8470 if (success) {
8470 8471 err = ipif_resolver_up(ipif, Res_act_initial);
8471 8472 if (err == EINPROGRESS) {
8472 8473 freemsg(mp);
8473 8474 return;
8474 8475 }
8475 8476 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8476 8477 } else {
8477 8478 /* The conn has started closing */
8478 8479 err = EINTR;
8479 8480 }
8480 8481 } else {
8481 8482 /*
8482 8483 * This one is complete. Reply to pending ioctl.
8483 8484 */
8484 8485 (void) ipif_resolver_up(ipif, Res_act_initial);
8485 8486 err = ipif_up_done(ipif);
8486 8487 }
8487 8488
8488 8489 if ((err == 0) && (ill->ill_up_ipifs)) {
8489 8490 err = ill_up_ipifs(ill, q, mp1);
8490 8491 if (err == EINPROGRESS) {
8491 8492 freemsg(mp);
8492 8493 return;
8493 8494 }
8494 8495 }
8495 8496
8496 8497 /*
8497 8498 * If we have a moved ipif to bring up, and everything has
8498 8499 * succeeded to this point, bring it up on the IPMP ill.
8499 8500 * Otherwise, leave it down -- the admin can try to bring it
8500 8501 * up by hand if need be.
8501 8502 */
8502 8503 if (ill->ill_move_ipif != NULL) {
8503 8504 if (err != 0) {
8504 8505 ill->ill_move_ipif = NULL;
8505 8506 } else {
8506 8507 ipif = ill->ill_move_ipif;
8507 8508 ill->ill_move_ipif = NULL;
8508 8509 err = ipif_up(ipif, q, mp1);
8509 8510 if (err == EINPROGRESS) {
8510 8511 freemsg(mp);
8511 8512 return;
8512 8513 }
8513 8514 }
8514 8515 }
8515 8516 break;
8516 8517
8517 8518 case DL_NOTIFY_IND: {
8518 8519 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8519 8520 uint_t orig_mtu, orig_mc_mtu;
8520 8521
8521 8522 switch (notify->dl_notification) {
8522 8523 case DL_NOTE_PHYS_ADDR:
8523 8524 err = ill_set_phys_addr(ill, mp);
8524 8525 break;
8525 8526
8526 8527 case DL_NOTE_REPLUMB:
8527 8528 /*
8528 8529 * Directly return after calling ill_replumb().
8529 8530 * Note that we should not free mp as it is reused
8530 8531 * in the ill_replumb() function.
8531 8532 */
8532 8533 err = ill_replumb(ill, mp);
8533 8534 return;
8534 8535
8535 8536 case DL_NOTE_FASTPATH_FLUSH:
8536 8537 nce_flush(ill, B_FALSE);
8537 8538 break;
8538 8539
8539 8540 case DL_NOTE_SDU_SIZE:
8540 8541 case DL_NOTE_SDU_SIZE2:
8541 8542 /*
8542 8543 * The dce and fragmentation code can cope with
8543 8544 * this changing while packets are being sent.
8544 8545 * When packets are sent ip_output will discover
8545 8546 * a change.
8546 8547 *
8547 8548 * Change the MTU size of the interface.
8548 8549 */
8549 8550 mutex_enter(&ill->ill_lock);
8550 8551 orig_mtu = ill->ill_mtu;
8551 8552 orig_mc_mtu = ill->ill_mc_mtu;
8552 8553 switch (notify->dl_notification) {
8553 8554 case DL_NOTE_SDU_SIZE:
8554 8555 ill->ill_current_frag =
8555 8556 (uint_t)notify->dl_data;
8556 8557 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8557 8558 break;
8558 8559 case DL_NOTE_SDU_SIZE2:
8559 8560 ill->ill_current_frag =
8560 8561 (uint_t)notify->dl_data1;
8561 8562 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8562 8563 break;
8563 8564 }
8564 8565 if (ill->ill_current_frag > ill->ill_max_frag)
8565 8566 ill->ill_max_frag = ill->ill_current_frag;
8566 8567
8567 8568 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8568 8569 ill->ill_mtu = ill->ill_current_frag;
8569 8570
8570 8571 /*
8571 8572 * If ill_user_mtu was set (via
8572 8573 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8573 8574 */
8574 8575 if (ill->ill_user_mtu != 0 &&
8575 8576 ill->ill_user_mtu < ill->ill_mtu)
8576 8577 ill->ill_mtu = ill->ill_user_mtu;
8577 8578
8578 8579 if (ill->ill_user_mtu != 0 &&
8579 8580 ill->ill_user_mtu < ill->ill_mc_mtu)
8580 8581 ill->ill_mc_mtu = ill->ill_user_mtu;
8581 8582
8582 8583 if (ill->ill_isv6) {
8583 8584 if (ill->ill_mtu < IPV6_MIN_MTU)
8584 8585 ill->ill_mtu = IPV6_MIN_MTU;
8585 8586 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8586 8587 ill->ill_mc_mtu = IPV6_MIN_MTU;
8587 8588 } else {
8588 8589 if (ill->ill_mtu < IP_MIN_MTU)
8589 8590 ill->ill_mtu = IP_MIN_MTU;
8590 8591 if (ill->ill_mc_mtu < IP_MIN_MTU)
8591 8592 ill->ill_mc_mtu = IP_MIN_MTU;
8592 8593 }
8593 8594 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8594 8595 ill->ill_mc_mtu = ill->ill_mtu;
8595 8596 }
8596 8597
8597 8598 mutex_exit(&ill->ill_lock);
8598 8599 /*
8599 8600 * Make sure all dce_generation checks find out
8600 8601 * that ill_mtu/ill_mc_mtu has changed.
8601 8602 */
8602 8603 if (orig_mtu != ill->ill_mtu ||
8603 8604 orig_mc_mtu != ill->ill_mc_mtu) {
8604 8605 dce_increment_all_generations(ill->ill_isv6,
8605 8606 ill->ill_ipst);
8606 8607 }
8607 8608
8608 8609 /*
8609 8610 * Refresh IPMP meta-interface MTU if necessary.
8610 8611 */
8611 8612 if (IS_UNDER_IPMP(ill))
8612 8613 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8613 8614 break;
8614 8615
8615 8616 case DL_NOTE_LINK_UP:
8616 8617 case DL_NOTE_LINK_DOWN: {
8617 8618 /*
8618 8619 * We are writer. ill / phyint / ipsq assocs stable.
8619 8620 * The RUNNING flag reflects the state of the link.
8620 8621 */
8621 8622 phyint_t *phyint = ill->ill_phyint;
8622 8623 uint64_t new_phyint_flags;
8623 8624 boolean_t changed = B_FALSE;
8624 8625 boolean_t went_up;
8625 8626
8626 8627 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8627 8628 mutex_enter(&phyint->phyint_lock);
8628 8629
8629 8630 new_phyint_flags = went_up ?
8630 8631 phyint->phyint_flags | PHYI_RUNNING :
8631 8632 phyint->phyint_flags & ~PHYI_RUNNING;
8632 8633
8633 8634 if (IS_IPMP(ill)) {
8634 8635 new_phyint_flags = went_up ?
8635 8636 new_phyint_flags & ~PHYI_FAILED :
8636 8637 new_phyint_flags | PHYI_FAILED;
8637 8638 }
8638 8639
8639 8640 if (new_phyint_flags != phyint->phyint_flags) {
8640 8641 phyint->phyint_flags = new_phyint_flags;
8641 8642 changed = B_TRUE;
8642 8643 }
8643 8644 mutex_exit(&phyint->phyint_lock);
8644 8645 /*
8645 8646 * ill_restart_dad handles the DAD restart and routing
8646 8647 * socket notification logic.
8647 8648 */
8648 8649 if (changed) {
8649 8650 ill_restart_dad(phyint->phyint_illv4, went_up);
8650 8651 ill_restart_dad(phyint->phyint_illv6, went_up);
8651 8652 }
8652 8653 break;
8653 8654 }
8654 8655 case DL_NOTE_PROMISC_ON_PHYS: {
8655 8656 phyint_t *phyint = ill->ill_phyint;
8656 8657
8657 8658 mutex_enter(&phyint->phyint_lock);
8658 8659 phyint->phyint_flags |= PHYI_PROMISC;
8659 8660 mutex_exit(&phyint->phyint_lock);
8660 8661 break;
8661 8662 }
8662 8663 case DL_NOTE_PROMISC_OFF_PHYS: {
8663 8664 phyint_t *phyint = ill->ill_phyint;
8664 8665
8665 8666 mutex_enter(&phyint->phyint_lock);
8666 8667 phyint->phyint_flags &= ~PHYI_PROMISC;
8667 8668 mutex_exit(&phyint->phyint_lock);
8668 8669 break;
8669 8670 }
8670 8671 case DL_NOTE_CAPAB_RENEG:
8671 8672 /*
8672 8673 * Something changed on the driver side.
8673 8674 * It wants us to renegotiate the capabilities
8674 8675 * on this ill. One possible cause is the aggregation
8675 8676 * interface under us where a port got added or
8676 8677 * went away.
8677 8678 *
8678 8679 * If the capability negotiation is already done
8679 8680 * or is in progress, reset the capabilities and
8680 8681 * mark the ill's ill_capab_reneg to be B_TRUE,
8681 8682 * so that when the ack comes back, we can start
8682 8683 * the renegotiation process.
8683 8684 *
8684 8685 * Note that if ill_capab_reneg is already B_TRUE
8685 8686 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8686 8687 * the capability resetting request has been sent
8687 8688 * and the renegotiation has not been started yet;
8688 8689 * nothing needs to be done in this case.
8689 8690 */
8690 8691 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8691 8692 ill_capability_reset(ill, B_TRUE);
8692 8693 ipsq_current_finish(ipsq);
8693 8694 break;
8694 8695
8695 8696 case DL_NOTE_ALLOWED_IPS:
8696 8697 ill_set_allowed_ips(ill, mp);
8697 8698 break;
8698 8699 default:
8699 8700 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8700 8701 "type 0x%x for DL_NOTIFY_IND\n",
8701 8702 notify->dl_notification));
8702 8703 break;
8703 8704 }
8704 8705
8705 8706 /*
8706 8707 * As this is an asynchronous operation, we
8707 8708 * should not call ill_dlpi_done
8708 8709 */
8709 8710 break;
8710 8711 }
8711 8712 case DL_NOTIFY_ACK: {
8712 8713 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8713 8714
8714 8715 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8715 8716 ill->ill_note_link = 1;
8716 8717 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8717 8718 break;
8718 8719 }
8719 8720 case DL_PHYS_ADDR_ACK: {
8720 8721 /*
8721 8722 * As part of plumbing the interface via SIOCSLIFNAME,
8722 8723 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8723 8724 * whose answers we receive here. As each answer is received,
8724 8725 * we call ill_dlpi_done() to dispatch the next request as
8725 8726 * we're processing the current one. Once all answers have
8726 8727 * been received, we use ipsq_pending_mp_get() to dequeue the
8727 8728 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8728 8729 * is invoked from an ill queue, conn_oper_pending_ill is not
8729 8730 * available, but we know the ioctl is pending on ill_wq.)
8730 8731 */
8731 8732 uint_t paddrlen, paddroff;
8732 8733 uint8_t *addr;
8733 8734
8734 8735 paddrreq = ill->ill_phys_addr_pend;
8735 8736 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8736 8737 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8737 8738 addr = mp->b_rptr + paddroff;
8738 8739
8739 8740 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8740 8741 if (paddrreq == DL_IPV6_TOKEN) {
8741 8742 /*
8742 8743 * bcopy to low-order bits of ill_token
8743 8744 *
8744 8745 * XXX Temporary hack - currently, all known tokens
8745 8746 * are 64 bits, so I'll cheat for the moment.
8746 8747 */
8747 8748 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8748 8749 ill->ill_token_length = paddrlen;
8749 8750 break;
8750 8751 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8751 8752 ASSERT(ill->ill_nd_lla_mp == NULL);
8752 8753 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8753 8754 mp = NULL;
8754 8755 break;
8755 8756 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8756 8757 ASSERT(ill->ill_dest_addr_mp == NULL);
8757 8758 ill->ill_dest_addr_mp = mp;
8758 8759 ill->ill_dest_addr = addr;
8759 8760 mp = NULL;
8760 8761 if (ill->ill_isv6) {
8761 8762 ill_setdesttoken(ill);
8762 8763 ipif_setdestlinklocal(ill->ill_ipif);
8763 8764 }
8764 8765 break;
8765 8766 }
8766 8767
8767 8768 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8768 8769 ASSERT(ill->ill_phys_addr_mp == NULL);
8769 8770 if (!ill->ill_ifname_pending)
8770 8771 break;
8771 8772 ill->ill_ifname_pending = 0;
8772 8773 if (!ioctl_aborted)
8773 8774 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8774 8775 if (mp1 != NULL) {
8775 8776 ASSERT(connp == NULL);
8776 8777 q = ill->ill_wq;
8777 8778 }
8778 8779 /*
8779 8780 * If any error acks received during the plumbing sequence,
8780 8781 * ill_ifname_pending_err will be set. Break out and send up
8781 8782 * the error to the pending ioctl.
8782 8783 */
8783 8784 if (ill->ill_ifname_pending_err != 0) {
8784 8785 err = ill->ill_ifname_pending_err;
8785 8786 ill->ill_ifname_pending_err = 0;
8786 8787 break;
8787 8788 }
8788 8789
8789 8790 ill->ill_phys_addr_mp = mp;
8790 8791 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8791 8792 mp = NULL;
8792 8793
8793 8794 /*
8794 8795 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8795 8796 * provider doesn't support physical addresses. We check both
8796 8797 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8797 8798 * not have physical addresses, but historically adversises a
8798 8799 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8799 8800 * its DL_PHYS_ADDR_ACK.
8800 8801 */
8801 8802 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8802 8803 ill->ill_phys_addr = NULL;
8803 8804 } else if (paddrlen != ill->ill_phys_addr_length) {
8804 8805 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8805 8806 paddrlen, ill->ill_phys_addr_length));
8806 8807 err = EINVAL;
8807 8808 break;
8808 8809 }
8809 8810
8810 8811 if (ill->ill_nd_lla_mp == NULL) {
8811 8812 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8812 8813 err = ENOMEM;
8813 8814 break;
8814 8815 }
8815 8816 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8816 8817 }
8817 8818
8818 8819 if (ill->ill_isv6) {
8819 8820 ill_setdefaulttoken(ill);
8820 8821 ipif_setlinklocal(ill->ill_ipif);
8821 8822 }
8822 8823 break;
8823 8824 }
8824 8825 case DL_OK_ACK:
8825 8826 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8826 8827 dl_primstr((int)dloa->dl_correct_primitive),
8827 8828 dloa->dl_correct_primitive));
8828 8829 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8829 8830 char *, dl_primstr(dloa->dl_correct_primitive),
8830 8831 ill_t *, ill);
8831 8832
8832 8833 switch (dloa->dl_correct_primitive) {
8833 8834 case DL_ENABMULTI_REQ:
8834 8835 case DL_DISABMULTI_REQ:
8835 8836 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8836 8837 break;
8837 8838 case DL_PROMISCON_REQ:
8838 8839 case DL_PROMISCOFF_REQ:
8839 8840 case DL_UNBIND_REQ:
8840 8841 case DL_ATTACH_REQ:
8841 8842 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8842 8843 break;
8843 8844 }
8844 8845 break;
8845 8846 default:
8846 8847 break;
8847 8848 }
8848 8849
8849 8850 freemsg(mp);
8850 8851 if (mp1 == NULL)
8851 8852 return;
8852 8853
8853 8854 /*
8854 8855 * The operation must complete without EINPROGRESS since
8855 8856 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8856 8857 * the operation will be stuck forever inside the IPSQ.
8857 8858 */
8858 8859 ASSERT(err != EINPROGRESS);
8859 8860
8860 8861 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8861 8862 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8862 8863 ipif_t *, NULL);
8863 8864
8864 8865 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8865 8866 case 0:
8866 8867 ipsq_current_finish(ipsq);
8867 8868 break;
8868 8869
8869 8870 case SIOCSLIFNAME:
8870 8871 case IF_UNITSEL: {
8871 8872 ill_t *ill_other = ILL_OTHER(ill);
8872 8873
8873 8874 /*
8874 8875 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8875 8876 * ill has a peer which is in an IPMP group, then place ill
8876 8877 * into the same group. One catch: although ifconfig plumbs
8877 8878 * the appropriate IPMP meta-interface prior to plumbing this
8878 8879 * ill, it is possible for multiple ifconfig applications to
8879 8880 * race (or for another application to adjust plumbing), in
8880 8881 * which case the IPMP meta-interface we need will be missing.
8881 8882 * If so, kick the phyint out of the group.
8882 8883 */
8883 8884 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8884 8885 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8885 8886 ipmp_illgrp_t *illg;
8886 8887
8887 8888 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8888 8889 if (illg == NULL)
8889 8890 ipmp_phyint_leave_grp(ill->ill_phyint);
8890 8891 else
8891 8892 ipmp_ill_join_illgrp(ill, illg);
8892 8893 }
8893 8894
8894 8895 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8895 8896 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8896 8897 else
8897 8898 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8898 8899 break;
8899 8900 }
8900 8901 case SIOCLIFADDIF:
8901 8902 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8902 8903 break;
8903 8904
8904 8905 default:
8905 8906 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8906 8907 break;
8907 8908 }
8908 8909 }
8909 8910
8910 8911 /*
8911 8912 * ip_rput_other is called by ip_rput to handle messages modifying the global
8912 8913 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8913 8914 */
8914 8915 /* ARGSUSED */
8915 8916 void
8916 8917 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8917 8918 {
8918 8919 ill_t *ill = q->q_ptr;
8919 8920 struct iocblk *iocp;
8920 8921
8921 8922 ip1dbg(("ip_rput_other "));
8922 8923 if (ipsq != NULL) {
8923 8924 ASSERT(IAM_WRITER_IPSQ(ipsq));
8924 8925 ASSERT(ipsq->ipsq_xop ==
8925 8926 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8926 8927 }
8927 8928
8928 8929 switch (mp->b_datap->db_type) {
8929 8930 case M_ERROR:
8930 8931 case M_HANGUP:
8931 8932 /*
8932 8933 * The device has a problem. We force the ILL down. It can
8933 8934 * be brought up again manually using SIOCSIFFLAGS (via
8934 8935 * ifconfig or equivalent).
8935 8936 */
8936 8937 ASSERT(ipsq != NULL);
8937 8938 if (mp->b_rptr < mp->b_wptr)
8938 8939 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8939 8940 if (ill->ill_error == 0)
8940 8941 ill->ill_error = ENXIO;
8941 8942 if (!ill_down_start(q, mp))
8942 8943 return;
8943 8944 ipif_all_down_tail(ipsq, q, mp, NULL);
8944 8945 break;
8945 8946 case M_IOCNAK: {
8946 8947 iocp = (struct iocblk *)mp->b_rptr;
8947 8948
8948 8949 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8949 8950 /*
8950 8951 * If this was the first attempt, turn off the fastpath
8951 8952 * probing.
8952 8953 */
8953 8954 mutex_enter(&ill->ill_lock);
8954 8955 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8955 8956 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8956 8957 mutex_exit(&ill->ill_lock);
8957 8958 /*
8958 8959 * don't flush the nce_t entries: we use them
8959 8960 * as an index to the ncec itself.
8960 8961 */
8961 8962 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8962 8963 ill->ill_name));
8963 8964 } else {
8964 8965 mutex_exit(&ill->ill_lock);
8965 8966 }
8966 8967 freemsg(mp);
8967 8968 break;
8968 8969 }
8969 8970 default:
8970 8971 ASSERT(0);
8971 8972 break;
8972 8973 }
8973 8974 }
8974 8975
8975 8976 /*
8976 8977 * Update any source route, record route or timestamp options
8977 8978 * When it fails it has consumed the message and BUMPed the MIB.
8978 8979 */
8979 8980 boolean_t
8980 8981 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8981 8982 ip_recv_attr_t *ira)
8982 8983 {
8983 8984 ipoptp_t opts;
8984 8985 uchar_t *opt;
8985 8986 uint8_t optval;
8986 8987 uint8_t optlen;
8987 8988 ipaddr_t dst;
8988 8989 ipaddr_t ifaddr;
8989 8990 uint32_t ts;
8990 8991 timestruc_t now;
8991 8992 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
8992 8993
8993 8994 ip2dbg(("ip_forward_options\n"));
8994 8995 dst = ipha->ipha_dst;
8995 8996 for (optval = ipoptp_first(&opts, ipha);
8996 8997 optval != IPOPT_EOL;
8997 8998 optval = ipoptp_next(&opts)) {
8998 8999 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8999 9000 opt = opts.ipoptp_cur;
9000 9001 optlen = opts.ipoptp_len;
9001 9002 ip2dbg(("ip_forward_options: opt %d, len %d\n",
9002 9003 optval, opts.ipoptp_len));
9003 9004 switch (optval) {
9004 9005 uint32_t off;
9005 9006 case IPOPT_SSRR:
9006 9007 case IPOPT_LSRR:
9007 9008 /* Check if adminstratively disabled */
9008 9009 if (!ipst->ips_ip_forward_src_routed) {
9009 9010 BUMP_MIB(dst_ill->ill_ip_mib,
9010 9011 ipIfStatsForwProhibits);
9011 9012 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
9012 9013 mp, dst_ill);
9013 9014 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
9014 9015 ira);
9015 9016 return (B_FALSE);
9016 9017 }
9017 9018 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9018 9019 /*
9019 9020 * Must be partial since ip_input_options
9020 9021 * checked for strict.
9021 9022 */
9022 9023 break;
9023 9024 }
9024 9025 off = opt[IPOPT_OFFSET];
9025 9026 off--;
9026 9027 redo_srr:
9027 9028 if (optlen < IP_ADDR_LEN ||
9028 9029 off > optlen - IP_ADDR_LEN) {
9029 9030 /* End of source route */
9030 9031 ip1dbg((
9031 9032 "ip_forward_options: end of SR\n"));
9032 9033 break;
9033 9034 }
9034 9035 /* Pick a reasonable address on the outbound if */
9035 9036 ASSERT(dst_ill != NULL);
9036 9037 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9037 9038 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9038 9039 NULL) != 0) {
9039 9040 /* No source! Shouldn't happen */
9040 9041 ifaddr = INADDR_ANY;
9041 9042 }
9042 9043 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9043 9044 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9044 9045 ip1dbg(("ip_forward_options: next hop 0x%x\n",
9045 9046 ntohl(dst)));
9046 9047
9047 9048 /*
9048 9049 * Check if our address is present more than
9049 9050 * once as consecutive hops in source route.
9050 9051 */
9051 9052 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9052 9053 off += IP_ADDR_LEN;
9053 9054 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9054 9055 goto redo_srr;
9055 9056 }
9056 9057 ipha->ipha_dst = dst;
9057 9058 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9058 9059 break;
9059 9060 case IPOPT_RR:
9060 9061 off = opt[IPOPT_OFFSET];
9061 9062 off--;
9062 9063 if (optlen < IP_ADDR_LEN ||
9063 9064 off > optlen - IP_ADDR_LEN) {
9064 9065 /* No more room - ignore */
9065 9066 ip1dbg((
9066 9067 "ip_forward_options: end of RR\n"));
9067 9068 break;
9068 9069 }
9069 9070 /* Pick a reasonable address on the outbound if */
9070 9071 ASSERT(dst_ill != NULL);
9071 9072 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9072 9073 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9073 9074 NULL) != 0) {
9074 9075 /* No source! Shouldn't happen */
9075 9076 ifaddr = INADDR_ANY;
9076 9077 }
9077 9078 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9078 9079 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9079 9080 break;
9080 9081 case IPOPT_TS:
9081 9082 /* Insert timestamp if there is room */
9082 9083 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9083 9084 case IPOPT_TS_TSONLY:
9084 9085 off = IPOPT_TS_TIMELEN;
9085 9086 break;
9086 9087 case IPOPT_TS_PRESPEC:
9087 9088 case IPOPT_TS_PRESPEC_RFC791:
9088 9089 /* Verify that the address matched */
9089 9090 off = opt[IPOPT_OFFSET] - 1;
9090 9091 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9091 9092 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9092 9093 /* Not for us */
9093 9094 break;
9094 9095 }
9095 9096 /* FALLTHROUGH */
9096 9097 case IPOPT_TS_TSANDADDR:
9097 9098 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9098 9099 break;
9099 9100 default:
9100 9101 /*
9101 9102 * ip_*put_options should have already
9102 9103 * dropped this packet.
9103 9104 */
9104 9105 cmn_err(CE_PANIC, "ip_forward_options: "
9105 9106 "unknown IT - bug in ip_input_options?\n");
9106 9107 return (B_TRUE); /* Keep "lint" happy */
9107 9108 }
9108 9109 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9109 9110 /* Increase overflow counter */
9110 9111 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9111 9112 opt[IPOPT_POS_OV_FLG] =
9112 9113 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9113 9114 (off << 4));
9114 9115 break;
9115 9116 }
9116 9117 off = opt[IPOPT_OFFSET] - 1;
9117 9118 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9118 9119 case IPOPT_TS_PRESPEC:
9119 9120 case IPOPT_TS_PRESPEC_RFC791:
9120 9121 case IPOPT_TS_TSANDADDR:
9121 9122 /* Pick a reasonable addr on the outbound if */
9122 9123 ASSERT(dst_ill != NULL);
9123 9124 if (ip_select_source_v4(dst_ill, INADDR_ANY,
9124 9125 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9125 9126 NULL, NULL) != 0) {
9126 9127 /* No source! Shouldn't happen */
9127 9128 ifaddr = INADDR_ANY;
9128 9129 }
9129 9130 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9130 9131 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9131 9132 /* FALLTHROUGH */
9132 9133 case IPOPT_TS_TSONLY:
9133 9134 off = opt[IPOPT_OFFSET] - 1;
9134 9135 /* Compute # of milliseconds since midnight */
9135 9136 gethrestime(&now);
9136 9137 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9137 9138 NSEC2MSEC(now.tv_nsec);
9138 9139 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9139 9140 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9140 9141 break;
9141 9142 }
9142 9143 break;
9143 9144 }
9144 9145 }
9145 9146 return (B_TRUE);
9146 9147 }
9147 9148
9148 9149 /*
9149 9150 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9150 9151 * returns 'true' if there are still fragments left on the queue, in
9151 9152 * which case we restart the timer.
9152 9153 */
9153 9154 void
9154 9155 ill_frag_timer(void *arg)
9155 9156 {
9156 9157 ill_t *ill = (ill_t *)arg;
9157 9158 boolean_t frag_pending;
9158 9159 ip_stack_t *ipst = ill->ill_ipst;
9159 9160 time_t timeout;
9160 9161
9161 9162 mutex_enter(&ill->ill_lock);
9162 9163 ASSERT(!ill->ill_fragtimer_executing);
9163 9164 if (ill->ill_state_flags & ILL_CONDEMNED) {
9164 9165 ill->ill_frag_timer_id = 0;
9165 9166 mutex_exit(&ill->ill_lock);
9166 9167 return;
9167 9168 }
9168 9169 ill->ill_fragtimer_executing = 1;
9169 9170 mutex_exit(&ill->ill_lock);
9170 9171
9171 9172 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9172 9173 ipst->ips_ip_reassembly_timeout);
9173 9174
9174 9175 frag_pending = ill_frag_timeout(ill, timeout);
9175 9176
9176 9177 /*
9177 9178 * Restart the timer, if we have fragments pending or if someone
9178 9179 * wanted us to be scheduled again.
9179 9180 */
9180 9181 mutex_enter(&ill->ill_lock);
9181 9182 ill->ill_fragtimer_executing = 0;
9182 9183 ill->ill_frag_timer_id = 0;
9183 9184 if (frag_pending || ill->ill_fragtimer_needrestart)
9184 9185 ill_frag_timer_start(ill);
9185 9186 mutex_exit(&ill->ill_lock);
9186 9187 }
9187 9188
9188 9189 void
9189 9190 ill_frag_timer_start(ill_t *ill)
9190 9191 {
9191 9192 ip_stack_t *ipst = ill->ill_ipst;
9192 9193 clock_t timeo_ms;
9193 9194
9194 9195 ASSERT(MUTEX_HELD(&ill->ill_lock));
9195 9196
9196 9197 /* If the ill is closing or opening don't proceed */
9197 9198 if (ill->ill_state_flags & ILL_CONDEMNED)
9198 9199 return;
9199 9200
9200 9201 if (ill->ill_fragtimer_executing) {
9201 9202 /*
9202 9203 * ill_frag_timer is currently executing. Just record the
9203 9204 * the fact that we want the timer to be restarted.
9204 9205 * ill_frag_timer will post a timeout before it returns,
9205 9206 * ensuring it will be called again.
9206 9207 */
9207 9208 ill->ill_fragtimer_needrestart = 1;
9208 9209 return;
9209 9210 }
9210 9211
9211 9212 if (ill->ill_frag_timer_id == 0) {
9212 9213 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9213 9214 ipst->ips_ip_reassembly_timeout) * SECONDS;
9214 9215
9215 9216 /*
9216 9217 * The timer is neither running nor is the timeout handler
9217 9218 * executing. Post a timeout so that ill_frag_timer will be
9218 9219 * called
9219 9220 */
9220 9221 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9221 9222 MSEC_TO_TICK(timeo_ms >> 1));
9222 9223 ill->ill_fragtimer_needrestart = 0;
9223 9224 }
9224 9225 }
9225 9226
9226 9227 /*
9227 9228 * Update any source route, record route or timestamp options.
9228 9229 * Check that we are at end of strict source route.
9229 9230 * The options have already been checked for sanity in ip_input_options().
9230 9231 */
9231 9232 boolean_t
9232 9233 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9233 9234 {
9234 9235 ipoptp_t opts;
9235 9236 uchar_t *opt;
9236 9237 uint8_t optval;
9237 9238 uint8_t optlen;
9238 9239 ipaddr_t dst;
9239 9240 ipaddr_t ifaddr;
9240 9241 uint32_t ts;
9241 9242 timestruc_t now;
9242 9243 ill_t *ill = ira->ira_ill;
9243 9244 ip_stack_t *ipst = ill->ill_ipst;
9244 9245
9245 9246 ip2dbg(("ip_input_local_options\n"));
9246 9247
9247 9248 for (optval = ipoptp_first(&opts, ipha);
9248 9249 optval != IPOPT_EOL;
9249 9250 optval = ipoptp_next(&opts)) {
9250 9251 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9251 9252 opt = opts.ipoptp_cur;
9252 9253 optlen = opts.ipoptp_len;
9253 9254 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9254 9255 optval, optlen));
9255 9256 switch (optval) {
9256 9257 uint32_t off;
9257 9258 case IPOPT_SSRR:
9258 9259 case IPOPT_LSRR:
9259 9260 off = opt[IPOPT_OFFSET];
9260 9261 off--;
9261 9262 if (optlen < IP_ADDR_LEN ||
9262 9263 off > optlen - IP_ADDR_LEN) {
9263 9264 /* End of source route */
9264 9265 ip1dbg(("ip_input_local_options: end of SR\n"));
9265 9266 break;
9266 9267 }
9267 9268 /*
9268 9269 * This will only happen if two consecutive entries
9269 9270 * in the source route contains our address or if
9270 9271 * it is a packet with a loose source route which
9271 9272 * reaches us before consuming the whole source route
9272 9273 */
9273 9274 ip1dbg(("ip_input_local_options: not end of SR\n"));
9274 9275 if (optval == IPOPT_SSRR) {
9275 9276 goto bad_src_route;
9276 9277 }
9277 9278 /*
9278 9279 * Hack: instead of dropping the packet truncate the
9279 9280 * source route to what has been used by filling the
9280 9281 * rest with IPOPT_NOP.
9281 9282 */
9282 9283 opt[IPOPT_OLEN] = (uint8_t)off;
9283 9284 while (off < optlen) {
9284 9285 opt[off++] = IPOPT_NOP;
9285 9286 }
9286 9287 break;
9287 9288 case IPOPT_RR:
9288 9289 off = opt[IPOPT_OFFSET];
9289 9290 off--;
9290 9291 if (optlen < IP_ADDR_LEN ||
9291 9292 off > optlen - IP_ADDR_LEN) {
9292 9293 /* No more room - ignore */
9293 9294 ip1dbg((
9294 9295 "ip_input_local_options: end of RR\n"));
9295 9296 break;
9296 9297 }
9297 9298 /* Pick a reasonable address on the outbound if */
9298 9299 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9299 9300 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9300 9301 NULL) != 0) {
9301 9302 /* No source! Shouldn't happen */
9302 9303 ifaddr = INADDR_ANY;
9303 9304 }
9304 9305 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9305 9306 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9306 9307 break;
9307 9308 case IPOPT_TS:
9308 9309 /* Insert timestamp if there is romm */
9309 9310 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9310 9311 case IPOPT_TS_TSONLY:
9311 9312 off = IPOPT_TS_TIMELEN;
9312 9313 break;
9313 9314 case IPOPT_TS_PRESPEC:
9314 9315 case IPOPT_TS_PRESPEC_RFC791:
9315 9316 /* Verify that the address matched */
9316 9317 off = opt[IPOPT_OFFSET] - 1;
9317 9318 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9318 9319 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9319 9320 /* Not for us */
9320 9321 break;
9321 9322 }
9322 9323 /* FALLTHROUGH */
9323 9324 case IPOPT_TS_TSANDADDR:
9324 9325 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9325 9326 break;
9326 9327 default:
9327 9328 /*
9328 9329 * ip_*put_options should have already
9329 9330 * dropped this packet.
9330 9331 */
9331 9332 cmn_err(CE_PANIC, "ip_input_local_options: "
9332 9333 "unknown IT - bug in ip_input_options?\n");
9333 9334 return (B_TRUE); /* Keep "lint" happy */
9334 9335 }
9335 9336 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9336 9337 /* Increase overflow counter */
9337 9338 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9338 9339 opt[IPOPT_POS_OV_FLG] =
9339 9340 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9340 9341 (off << 4));
9341 9342 break;
9342 9343 }
9343 9344 off = opt[IPOPT_OFFSET] - 1;
9344 9345 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9345 9346 case IPOPT_TS_PRESPEC:
9346 9347 case IPOPT_TS_PRESPEC_RFC791:
9347 9348 case IPOPT_TS_TSANDADDR:
9348 9349 /* Pick a reasonable addr on the outbound if */
9349 9350 if (ip_select_source_v4(ill, INADDR_ANY,
9350 9351 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9351 9352 &ifaddr, NULL, NULL) != 0) {
9352 9353 /* No source! Shouldn't happen */
9353 9354 ifaddr = INADDR_ANY;
9354 9355 }
9355 9356 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9356 9357 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9357 9358 /* FALLTHROUGH */
9358 9359 case IPOPT_TS_TSONLY:
9359 9360 off = opt[IPOPT_OFFSET] - 1;
9360 9361 /* Compute # of milliseconds since midnight */
9361 9362 gethrestime(&now);
9362 9363 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9363 9364 NSEC2MSEC(now.tv_nsec);
9364 9365 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9365 9366 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9366 9367 break;
9367 9368 }
9368 9369 break;
9369 9370 }
9370 9371 }
9371 9372 return (B_TRUE);
9372 9373
9373 9374 bad_src_route:
9374 9375 /* make sure we clear any indication of a hardware checksum */
9375 9376 DB_CKSUMFLAGS(mp) = 0;
9376 9377 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9377 9378 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9378 9379 return (B_FALSE);
9379 9380
9380 9381 }
9381 9382
9382 9383 /*
9383 9384 * Process IP options in an inbound packet. Always returns the nexthop.
9384 9385 * Normally this is the passed in nexthop, but if there is an option
9385 9386 * that effects the nexthop (such as a source route) that will be returned.
9386 9387 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9387 9388 * and mp freed.
9388 9389 */
9389 9390 ipaddr_t
9390 9391 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9391 9392 ip_recv_attr_t *ira, int *errorp)
9392 9393 {
9393 9394 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9394 9395 ipoptp_t opts;
9395 9396 uchar_t *opt;
9396 9397 uint8_t optval;
9397 9398 uint8_t optlen;
9398 9399 intptr_t code = 0;
9399 9400 ire_t *ire;
9400 9401
9401 9402 ip2dbg(("ip_input_options\n"));
9402 9403 *errorp = 0;
9403 9404 for (optval = ipoptp_first(&opts, ipha);
9404 9405 optval != IPOPT_EOL;
9405 9406 optval = ipoptp_next(&opts)) {
9406 9407 opt = opts.ipoptp_cur;
9407 9408 optlen = opts.ipoptp_len;
9408 9409 ip2dbg(("ip_input_options: opt %d, len %d\n",
9409 9410 optval, optlen));
9410 9411 /*
9411 9412 * Note: we need to verify the checksum before we
9412 9413 * modify anything thus this routine only extracts the next
9413 9414 * hop dst from any source route.
9414 9415 */
9415 9416 switch (optval) {
9416 9417 uint32_t off;
9417 9418 case IPOPT_SSRR:
9418 9419 case IPOPT_LSRR:
9419 9420 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9420 9421 if (optval == IPOPT_SSRR) {
9421 9422 ip1dbg(("ip_input_options: not next"
9422 9423 " strict source route 0x%x\n",
9423 9424 ntohl(dst)));
9424 9425 code = (char *)&ipha->ipha_dst -
9425 9426 (char *)ipha;
9426 9427 goto param_prob; /* RouterReq's */
9427 9428 }
9428 9429 ip2dbg(("ip_input_options: "
9429 9430 "not next source route 0x%x\n",
9430 9431 ntohl(dst)));
9431 9432 break;
9432 9433 }
9433 9434
9434 9435 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9435 9436 ip1dbg((
9436 9437 "ip_input_options: bad option offset\n"));
9437 9438 code = (char *)&opt[IPOPT_OLEN] -
9438 9439 (char *)ipha;
9439 9440 goto param_prob;
9440 9441 }
9441 9442 off = opt[IPOPT_OFFSET];
9442 9443 off--;
9443 9444 redo_srr:
9444 9445 if (optlen < IP_ADDR_LEN ||
9445 9446 off > optlen - IP_ADDR_LEN) {
9446 9447 /* End of source route */
9447 9448 ip1dbg(("ip_input_options: end of SR\n"));
9448 9449 break;
9449 9450 }
9450 9451 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9451 9452 ip1dbg(("ip_input_options: next hop 0x%x\n",
9452 9453 ntohl(dst)));
9453 9454
9454 9455 /*
9455 9456 * Check if our address is present more than
9456 9457 * once as consecutive hops in source route.
9457 9458 * XXX verify per-interface ip_forwarding
9458 9459 * for source route?
9459 9460 */
9460 9461 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9461 9462 off += IP_ADDR_LEN;
9462 9463 goto redo_srr;
9463 9464 }
9464 9465
9465 9466 if (dst == htonl(INADDR_LOOPBACK)) {
9466 9467 ip1dbg(("ip_input_options: loopback addr in "
9467 9468 "source route!\n"));
9468 9469 goto bad_src_route;
9469 9470 }
9470 9471 /*
9471 9472 * For strict: verify that dst is directly
9472 9473 * reachable.
9473 9474 */
9474 9475 if (optval == IPOPT_SSRR) {
9475 9476 ire = ire_ftable_lookup_v4(dst, 0, 0,
9476 9477 IRE_INTERFACE, NULL, ALL_ZONES,
9477 9478 ira->ira_tsl,
9478 9479 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9479 9480 NULL);
9480 9481 if (ire == NULL) {
9481 9482 ip1dbg(("ip_input_options: SSRR not "
9482 9483 "directly reachable: 0x%x\n",
9483 9484 ntohl(dst)));
9484 9485 goto bad_src_route;
9485 9486 }
9486 9487 ire_refrele(ire);
9487 9488 }
9488 9489 /*
9489 9490 * Defer update of the offset and the record route
9490 9491 * until the packet is forwarded.
9491 9492 */
9492 9493 break;
9493 9494 case IPOPT_RR:
9494 9495 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9495 9496 ip1dbg((
9496 9497 "ip_input_options: bad option offset\n"));
9497 9498 code = (char *)&opt[IPOPT_OLEN] -
9498 9499 (char *)ipha;
9499 9500 goto param_prob;
9500 9501 }
9501 9502 break;
9502 9503 case IPOPT_TS:
9503 9504 /*
9504 9505 * Verify that length >= 5 and that there is either
9505 9506 * room for another timestamp or that the overflow
9506 9507 * counter is not maxed out.
9507 9508 */
9508 9509 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9509 9510 if (optlen < IPOPT_MINLEN_IT) {
9510 9511 goto param_prob;
9511 9512 }
9512 9513 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9513 9514 ip1dbg((
9514 9515 "ip_input_options: bad option offset\n"));
9515 9516 code = (char *)&opt[IPOPT_OFFSET] -
9516 9517 (char *)ipha;
9517 9518 goto param_prob;
9518 9519 }
9519 9520 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9520 9521 case IPOPT_TS_TSONLY:
9521 9522 off = IPOPT_TS_TIMELEN;
9522 9523 break;
9523 9524 case IPOPT_TS_TSANDADDR:
9524 9525 case IPOPT_TS_PRESPEC:
9525 9526 case IPOPT_TS_PRESPEC_RFC791:
9526 9527 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9527 9528 break;
9528 9529 default:
9529 9530 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9530 9531 (char *)ipha;
9531 9532 goto param_prob;
9532 9533 }
9533 9534 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9534 9535 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9535 9536 /*
9536 9537 * No room and the overflow counter is 15
9537 9538 * already.
9538 9539 */
9539 9540 goto param_prob;
9540 9541 }
9541 9542 break;
9542 9543 }
9543 9544 }
9544 9545
9545 9546 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9546 9547 return (dst);
9547 9548 }
9548 9549
9549 9550 ip1dbg(("ip_input_options: error processing IP options."));
9550 9551 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9551 9552
9552 9553 param_prob:
9553 9554 /* make sure we clear any indication of a hardware checksum */
9554 9555 DB_CKSUMFLAGS(mp) = 0;
9555 9556 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9556 9557 icmp_param_problem(mp, (uint8_t)code, ira);
9557 9558 *errorp = -1;
9558 9559 return (dst);
9559 9560
9560 9561 bad_src_route:
9561 9562 /* make sure we clear any indication of a hardware checksum */
9562 9563 DB_CKSUMFLAGS(mp) = 0;
9563 9564 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9564 9565 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9565 9566 *errorp = -1;
9566 9567 return (dst);
9567 9568 }
9568 9569
9569 9570 /*
9570 9571 * IP & ICMP info in >=14 msg's ...
9571 9572 * - ip fixed part (mib2_ip_t)
9572 9573 * - icmp fixed part (mib2_icmp_t)
9573 9574 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9574 9575 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9575 9576 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9576 9577 * - ipRouteAttributeTable (ip 102) labeled routes
9577 9578 * - ip multicast membership (ip_member_t)
9578 9579 * - ip multicast source filtering (ip_grpsrc_t)
9579 9580 * - igmp fixed part (struct igmpstat)
9580 9581 * - multicast routing stats (struct mrtstat)
9581 9582 * - multicast routing vifs (array of struct vifctl)
9582 9583 * - multicast routing routes (array of struct mfcctl)
9583 9584 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9584 9585 * One per ill plus one generic
9585 9586 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9586 9587 * One per ill plus one generic
9587 9588 * - ipv6RouteEntry all IPv6 IREs
9588 9589 * - ipv6RouteAttributeTable (ip6 102) labeled routes
9589 9590 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9590 9591 * - ipv6AddrEntry all IPv6 ipifs
9591 9592 * - ipv6 multicast membership (ipv6_member_t)
9592 9593 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9593 9594 *
9594 9595 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9595 9596 * already filled in by the caller.
9596 9597 * If legacy_req is true then MIB structures needs to be truncated to their
9597 9598 * legacy sizes before being returned.
9598 9599 * Return value of 0 indicates that no messages were sent and caller
9599 9600 * should free mpctl.
9600 9601 */
9601 9602 int
9602 9603 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9603 9604 {
9604 9605 ip_stack_t *ipst;
9605 9606 sctp_stack_t *sctps;
9606 9607
9607 9608 if (q->q_next != NULL) {
9608 9609 ipst = ILLQ_TO_IPST(q);
9609 9610 } else {
9610 9611 ipst = CONNQ_TO_IPST(q);
9611 9612 }
9612 9613 ASSERT(ipst != NULL);
9613 9614 sctps = ipst->ips_netstack->netstack_sctp;
9614 9615
9615 9616 if (mpctl == NULL || mpctl->b_cont == NULL) {
9616 9617 return (0);
9617 9618 }
9618 9619
9619 9620 /*
9620 9621 * For the purposes of the (broken) packet shell use
9621 9622 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9622 9623 * to make TCP and UDP appear first in the list of mib items.
9623 9624 * TBD: We could expand this and use it in netstat so that
9624 9625 * the kernel doesn't have to produce large tables (connections,
9625 9626 * routes, etc) when netstat only wants the statistics or a particular
9626 9627 * table.
9627 9628 */
9628 9629 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9629 9630 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9630 9631 return (1);
9631 9632 }
9632 9633 }
9633 9634
9634 9635 if (level != MIB2_TCP) {
9635 9636 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9636 9637 return (1);
9637 9638 }
9638 9639 if (level == MIB2_UDP) {
9639 9640 goto done;
9640 9641 }
9641 9642 }
9642 9643
9643 9644 if (level != MIB2_UDP) {
9644 9645 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9645 9646 return (1);
9646 9647 }
9647 9648 if (level == MIB2_TCP) {
9648 9649 goto done;
9649 9650 }
9650 9651 }
9651 9652
9652 9653 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9653 9654 ipst, legacy_req)) == NULL) {
9654 9655 return (1);
9655 9656 }
9656 9657
9657 9658 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9658 9659 legacy_req)) == NULL) {
9659 9660 return (1);
9660 9661 }
9661 9662
9662 9663 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9663 9664 return (1);
9664 9665 }
9665 9666
9666 9667 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9667 9668 return (1);
9668 9669 }
9669 9670
9670 9671 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9671 9672 return (1);
9672 9673 }
9673 9674
9674 9675 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9675 9676 return (1);
9676 9677 }
9677 9678
9678 9679 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9679 9680 legacy_req)) == NULL) {
9680 9681 return (1);
9681 9682 }
9682 9683
9683 9684 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9684 9685 legacy_req)) == NULL) {
9685 9686 return (1);
9686 9687 }
9687 9688
9688 9689 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9689 9690 return (1);
9690 9691 }
9691 9692
9692 9693 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9693 9694 return (1);
9694 9695 }
9695 9696
9696 9697 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9697 9698 return (1);
9698 9699 }
9699 9700
9700 9701 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9701 9702 return (1);
9702 9703 }
9703 9704
9704 9705 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9705 9706 return (1);
9706 9707 }
9707 9708
9708 9709 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9709 9710 return (1);
9710 9711 }
9711 9712
9712 9713 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9713 9714 if (mpctl == NULL)
9714 9715 return (1);
9715 9716
9716 9717 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9717 9718 if (mpctl == NULL)
9718 9719 return (1);
9719 9720
9720 9721 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9721 9722 return (1);
9722 9723 }
9723 9724 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9724 9725 return (1);
9725 9726 }
9726 9727 done:
9727 9728 freemsg(mpctl);
9728 9729 return (1);
9729 9730 }
9730 9731
9731 9732 /* Get global (legacy) IPv4 statistics */
9732 9733 static mblk_t *
9733 9734 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9734 9735 ip_stack_t *ipst, boolean_t legacy_req)
9735 9736 {
9736 9737 mib2_ip_t old_ip_mib;
9737 9738 struct opthdr *optp;
9738 9739 mblk_t *mp2ctl;
9739 9740 mib2_ipAddrEntry_t mae;
9740 9741
9741 9742 /*
9742 9743 * make a copy of the original message
9743 9744 */
9744 9745 mp2ctl = copymsg(mpctl);
9745 9746
9746 9747 /* fixed length IP structure... */
9747 9748 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9748 9749 optp->level = MIB2_IP;
9749 9750 optp->name = 0;
9750 9751 SET_MIB(old_ip_mib.ipForwarding,
9751 9752 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9752 9753 SET_MIB(old_ip_mib.ipDefaultTTL,
9753 9754 (uint32_t)ipst->ips_ip_def_ttl);
9754 9755 SET_MIB(old_ip_mib.ipReasmTimeout,
9755 9756 ipst->ips_ip_reassembly_timeout);
9756 9757 SET_MIB(old_ip_mib.ipAddrEntrySize,
9757 9758 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9758 9759 sizeof (mib2_ipAddrEntry_t));
9759 9760 SET_MIB(old_ip_mib.ipRouteEntrySize,
9760 9761 sizeof (mib2_ipRouteEntry_t));
9761 9762 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9762 9763 sizeof (mib2_ipNetToMediaEntry_t));
9763 9764 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9764 9765 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9765 9766 SET_MIB(old_ip_mib.ipRouteAttributeSize,
9766 9767 sizeof (mib2_ipAttributeEntry_t));
9767 9768 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9768 9769 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9769 9770
9770 9771 /*
9771 9772 * Grab the statistics from the new IP MIB
9772 9773 */
9773 9774 SET_MIB(old_ip_mib.ipInReceives,
9774 9775 (uint32_t)ipmib->ipIfStatsHCInReceives);
9775 9776 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9776 9777 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9777 9778 SET_MIB(old_ip_mib.ipForwDatagrams,
9778 9779 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9779 9780 SET_MIB(old_ip_mib.ipInUnknownProtos,
9780 9781 ipmib->ipIfStatsInUnknownProtos);
9781 9782 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9782 9783 SET_MIB(old_ip_mib.ipInDelivers,
9783 9784 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9784 9785 SET_MIB(old_ip_mib.ipOutRequests,
9785 9786 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9786 9787 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9787 9788 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9788 9789 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9789 9790 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9790 9791 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9791 9792 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9792 9793 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9793 9794 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9794 9795
9795 9796 /* ipRoutingDiscards is not being used */
9796 9797 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9797 9798 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9798 9799 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9799 9800 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9800 9801 SET_MIB(old_ip_mib.ipReasmDuplicates,
9801 9802 ipmib->ipIfStatsReasmDuplicates);
9802 9803 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9803 9804 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9804 9805 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9805 9806 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9806 9807 SET_MIB(old_ip_mib.rawipInOverflows,
9807 9808 ipmib->rawipIfStatsInOverflows);
9808 9809
9809 9810 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9810 9811 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9811 9812 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9812 9813 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9813 9814 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9814 9815 ipmib->ipIfStatsOutSwitchIPVersion);
9815 9816
9816 9817 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9817 9818 (int)sizeof (old_ip_mib))) {
9818 9819 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9819 9820 (uint_t)sizeof (old_ip_mib)));
9820 9821 }
9821 9822
9822 9823 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9823 9824 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9824 9825 (int)optp->level, (int)optp->name, (int)optp->len));
9825 9826 qreply(q, mpctl);
9826 9827 return (mp2ctl);
9827 9828 }
9828 9829
9829 9830 /* Per interface IPv4 statistics */
9830 9831 static mblk_t *
9831 9832 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9832 9833 boolean_t legacy_req)
9833 9834 {
9834 9835 struct opthdr *optp;
9835 9836 mblk_t *mp2ctl;
9836 9837 ill_t *ill;
9837 9838 ill_walk_context_t ctx;
9838 9839 mblk_t *mp_tail = NULL;
9839 9840 mib2_ipIfStatsEntry_t global_ip_mib;
9840 9841 mib2_ipAddrEntry_t mae;
9841 9842
9842 9843 /*
9843 9844 * Make a copy of the original message
9844 9845 */
9845 9846 mp2ctl = copymsg(mpctl);
9846 9847
9847 9848 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9848 9849 optp->level = MIB2_IP;
9849 9850 optp->name = MIB2_IP_TRAFFIC_STATS;
9850 9851 /* Include "unknown interface" ip_mib */
9851 9852 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9852 9853 ipst->ips_ip_mib.ipIfStatsIfIndex =
9853 9854 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9854 9855 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9855 9856 (ipst->ips_ip_forwarding ? 1 : 2));
9856 9857 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9857 9858 (uint32_t)ipst->ips_ip_def_ttl);
9858 9859 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9859 9860 sizeof (mib2_ipIfStatsEntry_t));
9860 9861 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9861 9862 sizeof (mib2_ipAddrEntry_t));
9862 9863 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9863 9864 sizeof (mib2_ipRouteEntry_t));
9864 9865 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9865 9866 sizeof (mib2_ipNetToMediaEntry_t));
9866 9867 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9867 9868 sizeof (ip_member_t));
9868 9869 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9869 9870 sizeof (ip_grpsrc_t));
9870 9871
9871 9872 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9872 9873
9873 9874 if (legacy_req) {
9874 9875 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9875 9876 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9876 9877 }
9877 9878
9878 9879 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9879 9880 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9880 9881 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9881 9882 "failed to allocate %u bytes\n",
9882 9883 (uint_t)sizeof (global_ip_mib)));
9883 9884 }
9884 9885
9885 9886 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9886 9887 ill = ILL_START_WALK_V4(&ctx, ipst);
9887 9888 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9888 9889 ill->ill_ip_mib->ipIfStatsIfIndex =
9889 9890 ill->ill_phyint->phyint_ifindex;
9890 9891 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9891 9892 (ipst->ips_ip_forwarding ? 1 : 2));
9892 9893 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9893 9894 (uint32_t)ipst->ips_ip_def_ttl);
9894 9895
9895 9896 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9896 9897 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9897 9898 (char *)ill->ill_ip_mib,
9898 9899 (int)sizeof (*ill->ill_ip_mib))) {
9899 9900 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9900 9901 "failed to allocate %u bytes\n",
9901 9902 (uint_t)sizeof (*ill->ill_ip_mib)));
9902 9903 }
9903 9904 }
9904 9905 rw_exit(&ipst->ips_ill_g_lock);
9905 9906
9906 9907 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9907 9908 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9908 9909 "level %d, name %d, len %d\n",
9909 9910 (int)optp->level, (int)optp->name, (int)optp->len));
9910 9911 qreply(q, mpctl);
9911 9912
9912 9913 if (mp2ctl == NULL)
9913 9914 return (NULL);
9914 9915
9915 9916 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9916 9917 legacy_req));
9917 9918 }
9918 9919
9919 9920 /* Global IPv4 ICMP statistics */
9920 9921 static mblk_t *
9921 9922 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9922 9923 {
9923 9924 struct opthdr *optp;
9924 9925 mblk_t *mp2ctl;
9925 9926
9926 9927 /*
9927 9928 * Make a copy of the original message
9928 9929 */
9929 9930 mp2ctl = copymsg(mpctl);
9930 9931
9931 9932 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9932 9933 optp->level = MIB2_ICMP;
9933 9934 optp->name = 0;
9934 9935 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9935 9936 (int)sizeof (ipst->ips_icmp_mib))) {
9936 9937 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9937 9938 (uint_t)sizeof (ipst->ips_icmp_mib)));
9938 9939 }
9939 9940 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9940 9941 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9941 9942 (int)optp->level, (int)optp->name, (int)optp->len));
9942 9943 qreply(q, mpctl);
9943 9944 return (mp2ctl);
9944 9945 }
9945 9946
9946 9947 /* Global IPv4 IGMP statistics */
9947 9948 static mblk_t *
9948 9949 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9949 9950 {
9950 9951 struct opthdr *optp;
9951 9952 mblk_t *mp2ctl;
9952 9953
9953 9954 /*
9954 9955 * make a copy of the original message
9955 9956 */
9956 9957 mp2ctl = copymsg(mpctl);
9957 9958
9958 9959 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9959 9960 optp->level = EXPER_IGMP;
9960 9961 optp->name = 0;
9961 9962 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9962 9963 (int)sizeof (ipst->ips_igmpstat))) {
9963 9964 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9964 9965 (uint_t)sizeof (ipst->ips_igmpstat)));
9965 9966 }
9966 9967 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9967 9968 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9968 9969 (int)optp->level, (int)optp->name, (int)optp->len));
9969 9970 qreply(q, mpctl);
9970 9971 return (mp2ctl);
9971 9972 }
9972 9973
9973 9974 /* Global IPv4 Multicast Routing statistics */
9974 9975 static mblk_t *
9975 9976 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9976 9977 {
9977 9978 struct opthdr *optp;
9978 9979 mblk_t *mp2ctl;
9979 9980
9980 9981 /*
9981 9982 * make a copy of the original message
9982 9983 */
9983 9984 mp2ctl = copymsg(mpctl);
9984 9985
9985 9986 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9986 9987 optp->level = EXPER_DVMRP;
9987 9988 optp->name = 0;
9988 9989 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9989 9990 ip0dbg(("ip_mroute_stats: failed\n"));
9990 9991 }
9991 9992 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9992 9993 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9993 9994 (int)optp->level, (int)optp->name, (int)optp->len));
9994 9995 qreply(q, mpctl);
9995 9996 return (mp2ctl);
9996 9997 }
9997 9998
9998 9999 /* IPv4 address information */
9999 10000 static mblk_t *
10000 10001 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10001 10002 boolean_t legacy_req)
10002 10003 {
10003 10004 struct opthdr *optp;
10004 10005 mblk_t *mp2ctl;
10005 10006 mblk_t *mp_tail = NULL;
10006 10007 ill_t *ill;
10007 10008 ipif_t *ipif;
10008 10009 uint_t bitval;
10009 10010 mib2_ipAddrEntry_t mae;
10010 10011 size_t mae_size;
10011 10012 zoneid_t zoneid;
10012 10013 ill_walk_context_t ctx;
10013 10014
10014 10015 /*
10015 10016 * make a copy of the original message
10016 10017 */
10017 10018 mp2ctl = copymsg(mpctl);
10018 10019
10019 10020 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
10020 10021 sizeof (mib2_ipAddrEntry_t);
10021 10022
10022 10023 /* ipAddrEntryTable */
10023 10024
10024 10025 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10025 10026 optp->level = MIB2_IP;
10026 10027 optp->name = MIB2_IP_ADDR;
10027 10028 zoneid = Q_TO_CONN(q)->conn_zoneid;
10028 10029
10029 10030 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10030 10031 ill = ILL_START_WALK_V4(&ctx, ipst);
10031 10032 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10032 10033 for (ipif = ill->ill_ipif; ipif != NULL;
10033 10034 ipif = ipif->ipif_next) {
10034 10035 if (ipif->ipif_zoneid != zoneid &&
10035 10036 ipif->ipif_zoneid != ALL_ZONES)
10036 10037 continue;
10037 10038 /* Sum of count from dead IRE_LO* and our current */
10038 10039 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10039 10040 if (ipif->ipif_ire_local != NULL) {
10040 10041 mae.ipAdEntInfo.ae_ibcnt +=
10041 10042 ipif->ipif_ire_local->ire_ib_pkt_count;
10042 10043 }
10043 10044 mae.ipAdEntInfo.ae_obcnt = 0;
10044 10045 mae.ipAdEntInfo.ae_focnt = 0;
10045 10046
10046 10047 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
10047 10048 OCTET_LENGTH);
10048 10049 mae.ipAdEntIfIndex.o_length =
10049 10050 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
10050 10051 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
10051 10052 mae.ipAdEntNetMask = ipif->ipif_net_mask;
10052 10053 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
10053 10054 mae.ipAdEntInfo.ae_subnet_len =
10054 10055 ip_mask_to_plen(ipif->ipif_net_mask);
10055 10056 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
10056 10057 for (bitval = 1;
10057 10058 bitval &&
10058 10059 !(bitval & ipif->ipif_brd_addr);
10059 10060 bitval <<= 1)
10060 10061 noop;
10061 10062 mae.ipAdEntBcastAddr = bitval;
10062 10063 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10063 10064 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10064 10065 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
10065 10066 mae.ipAdEntInfo.ae_broadcast_addr =
10066 10067 ipif->ipif_brd_addr;
10067 10068 mae.ipAdEntInfo.ae_pp_dst_addr =
10068 10069 ipif->ipif_pp_dst_addr;
10069 10070 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10070 10071 ill->ill_flags | ill->ill_phyint->phyint_flags;
10071 10072 mae.ipAdEntRetransmitTime =
10072 10073 ill->ill_reachable_retrans_time;
10073 10074
10074 10075 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10075 10076 (char *)&mae, (int)mae_size)) {
10076 10077 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10077 10078 "allocate %u bytes\n", (uint_t)mae_size));
10078 10079 }
10079 10080 }
10080 10081 }
10081 10082 rw_exit(&ipst->ips_ill_g_lock);
10082 10083
10083 10084 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10084 10085 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10085 10086 (int)optp->level, (int)optp->name, (int)optp->len));
10086 10087 qreply(q, mpctl);
10087 10088 return (mp2ctl);
10088 10089 }
10089 10090
10090 10091 /* IPv6 address information */
10091 10092 static mblk_t *
10092 10093 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10093 10094 boolean_t legacy_req)
10094 10095 {
10095 10096 struct opthdr *optp;
10096 10097 mblk_t *mp2ctl;
10097 10098 mblk_t *mp_tail = NULL;
10098 10099 ill_t *ill;
10099 10100 ipif_t *ipif;
10100 10101 mib2_ipv6AddrEntry_t mae6;
10101 10102 size_t mae6_size;
10102 10103 zoneid_t zoneid;
10103 10104 ill_walk_context_t ctx;
10104 10105
10105 10106 /*
10106 10107 * make a copy of the original message
10107 10108 */
10108 10109 mp2ctl = copymsg(mpctl);
10109 10110
10110 10111 mae6_size = (legacy_req) ?
10111 10112 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10112 10113 sizeof (mib2_ipv6AddrEntry_t);
10113 10114
10114 10115 /* ipv6AddrEntryTable */
10115 10116
10116 10117 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10117 10118 optp->level = MIB2_IP6;
10118 10119 optp->name = MIB2_IP6_ADDR;
10119 10120 zoneid = Q_TO_CONN(q)->conn_zoneid;
10120 10121
10121 10122 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10122 10123 ill = ILL_START_WALK_V6(&ctx, ipst);
10123 10124 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10124 10125 for (ipif = ill->ill_ipif; ipif != NULL;
10125 10126 ipif = ipif->ipif_next) {
10126 10127 if (ipif->ipif_zoneid != zoneid &&
10127 10128 ipif->ipif_zoneid != ALL_ZONES)
10128 10129 continue;
10129 10130 /* Sum of count from dead IRE_LO* and our current */
10130 10131 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10131 10132 if (ipif->ipif_ire_local != NULL) {
10132 10133 mae6.ipv6AddrInfo.ae_ibcnt +=
10133 10134 ipif->ipif_ire_local->ire_ib_pkt_count;
10134 10135 }
10135 10136 mae6.ipv6AddrInfo.ae_obcnt = 0;
10136 10137 mae6.ipv6AddrInfo.ae_focnt = 0;
10137 10138
10138 10139 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10139 10140 OCTET_LENGTH);
10140 10141 mae6.ipv6AddrIfIndex.o_length =
10141 10142 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10142 10143 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10143 10144 mae6.ipv6AddrPfxLength =
10144 10145 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10145 10146 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10146 10147 mae6.ipv6AddrInfo.ae_subnet_len =
10147 10148 mae6.ipv6AddrPfxLength;
10148 10149 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10149 10150
10150 10151 /* Type: stateless(1), stateful(2), unknown(3) */
10151 10152 if (ipif->ipif_flags & IPIF_ADDRCONF)
10152 10153 mae6.ipv6AddrType = 1;
10153 10154 else
10154 10155 mae6.ipv6AddrType = 2;
10155 10156 /* Anycast: true(1), false(2) */
10156 10157 if (ipif->ipif_flags & IPIF_ANYCAST)
10157 10158 mae6.ipv6AddrAnycastFlag = 1;
10158 10159 else
10159 10160 mae6.ipv6AddrAnycastFlag = 2;
10160 10161
10161 10162 /*
10162 10163 * Address status: preferred(1), deprecated(2),
10163 10164 * invalid(3), inaccessible(4), unknown(5)
10164 10165 */
10165 10166 if (ipif->ipif_flags & IPIF_NOLOCAL)
10166 10167 mae6.ipv6AddrStatus = 3;
10167 10168 else if (ipif->ipif_flags & IPIF_DEPRECATED)
10168 10169 mae6.ipv6AddrStatus = 2;
10169 10170 else
10170 10171 mae6.ipv6AddrStatus = 1;
10171 10172 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10172 10173 mae6.ipv6AddrInfo.ae_metric =
10173 10174 ipif->ipif_ill->ill_metric;
10174 10175 mae6.ipv6AddrInfo.ae_pp_dst_addr =
10175 10176 ipif->ipif_v6pp_dst_addr;
10176 10177 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10177 10178 ill->ill_flags | ill->ill_phyint->phyint_flags;
10178 10179 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10179 10180 mae6.ipv6AddrIdentifier = ill->ill_token;
10180 10181 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10181 10182 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10182 10183 mae6.ipv6AddrRetransmitTime =
10183 10184 ill->ill_reachable_retrans_time;
10184 10185 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10185 10186 (char *)&mae6, (int)mae6_size)) {
10186 10187 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10187 10188 "allocate %u bytes\n",
10188 10189 (uint_t)mae6_size));
10189 10190 }
10190 10191 }
10191 10192 }
10192 10193 rw_exit(&ipst->ips_ill_g_lock);
10193 10194
10194 10195 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10195 10196 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10196 10197 (int)optp->level, (int)optp->name, (int)optp->len));
10197 10198 qreply(q, mpctl);
10198 10199 return (mp2ctl);
10199 10200 }
10200 10201
10201 10202 /* IPv4 multicast group membership. */
10202 10203 static mblk_t *
10203 10204 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10204 10205 {
10205 10206 struct opthdr *optp;
10206 10207 mblk_t *mp2ctl;
10207 10208 ill_t *ill;
10208 10209 ipif_t *ipif;
10209 10210 ilm_t *ilm;
10210 10211 ip_member_t ipm;
10211 10212 mblk_t *mp_tail = NULL;
10212 10213 ill_walk_context_t ctx;
10213 10214 zoneid_t zoneid;
10214 10215
10215 10216 /*
10216 10217 * make a copy of the original message
10217 10218 */
10218 10219 mp2ctl = copymsg(mpctl);
10219 10220 zoneid = Q_TO_CONN(q)->conn_zoneid;
10220 10221
10221 10222 /* ipGroupMember table */
10222 10223 optp = (struct opthdr *)&mpctl->b_rptr[
10223 10224 sizeof (struct T_optmgmt_ack)];
10224 10225 optp->level = MIB2_IP;
10225 10226 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10226 10227
10227 10228 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10228 10229 ill = ILL_START_WALK_V4(&ctx, ipst);
10229 10230 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10230 10231 /* Make sure the ill isn't going away. */
10231 10232 if (!ill_check_and_refhold(ill))
10232 10233 continue;
10233 10234 rw_exit(&ipst->ips_ill_g_lock);
10234 10235 rw_enter(&ill->ill_mcast_lock, RW_READER);
10235 10236 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10236 10237 if (ilm->ilm_zoneid != zoneid &&
10237 10238 ilm->ilm_zoneid != ALL_ZONES)
10238 10239 continue;
10239 10240
10240 10241 /* Is there an ipif for ilm_ifaddr? */
10241 10242 for (ipif = ill->ill_ipif; ipif != NULL;
10242 10243 ipif = ipif->ipif_next) {
10243 10244 if (!IPIF_IS_CONDEMNED(ipif) &&
10244 10245 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10245 10246 ilm->ilm_ifaddr != INADDR_ANY)
10246 10247 break;
10247 10248 }
10248 10249 if (ipif != NULL) {
10249 10250 ipif_get_name(ipif,
10250 10251 ipm.ipGroupMemberIfIndex.o_bytes,
10251 10252 OCTET_LENGTH);
10252 10253 } else {
10253 10254 ill_get_name(ill,
10254 10255 ipm.ipGroupMemberIfIndex.o_bytes,
10255 10256 OCTET_LENGTH);
10256 10257 }
10257 10258 ipm.ipGroupMemberIfIndex.o_length =
10258 10259 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10259 10260
10260 10261 ipm.ipGroupMemberAddress = ilm->ilm_addr;
10261 10262 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10262 10263 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10263 10264 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10264 10265 (char *)&ipm, (int)sizeof (ipm))) {
10265 10266 ip1dbg(("ip_snmp_get_mib2_ip_group: "
10266 10267 "failed to allocate %u bytes\n",
10267 10268 (uint_t)sizeof (ipm)));
10268 10269 }
10269 10270 }
10270 10271 rw_exit(&ill->ill_mcast_lock);
10271 10272 ill_refrele(ill);
10272 10273 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10273 10274 }
10274 10275 rw_exit(&ipst->ips_ill_g_lock);
10275 10276 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10276 10277 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10277 10278 (int)optp->level, (int)optp->name, (int)optp->len));
10278 10279 qreply(q, mpctl);
10279 10280 return (mp2ctl);
10280 10281 }
10281 10282
10282 10283 /* IPv6 multicast group membership. */
10283 10284 static mblk_t *
10284 10285 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10285 10286 {
10286 10287 struct opthdr *optp;
10287 10288 mblk_t *mp2ctl;
10288 10289 ill_t *ill;
10289 10290 ilm_t *ilm;
10290 10291 ipv6_member_t ipm6;
10291 10292 mblk_t *mp_tail = NULL;
10292 10293 ill_walk_context_t ctx;
10293 10294 zoneid_t zoneid;
10294 10295
10295 10296 /*
10296 10297 * make a copy of the original message
10297 10298 */
10298 10299 mp2ctl = copymsg(mpctl);
10299 10300 zoneid = Q_TO_CONN(q)->conn_zoneid;
10300 10301
10301 10302 /* ip6GroupMember table */
10302 10303 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10303 10304 optp->level = MIB2_IP6;
10304 10305 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10305 10306
10306 10307 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10307 10308 ill = ILL_START_WALK_V6(&ctx, ipst);
10308 10309 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10309 10310 /* Make sure the ill isn't going away. */
10310 10311 if (!ill_check_and_refhold(ill))
10311 10312 continue;
10312 10313 rw_exit(&ipst->ips_ill_g_lock);
10313 10314 /*
10314 10315 * Normally we don't have any members on under IPMP interfaces.
10315 10316 * We report them as a debugging aid.
10316 10317 */
10317 10318 rw_enter(&ill->ill_mcast_lock, RW_READER);
10318 10319 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10319 10320 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10320 10321 if (ilm->ilm_zoneid != zoneid &&
10321 10322 ilm->ilm_zoneid != ALL_ZONES)
10322 10323 continue; /* not this zone */
10323 10324 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10324 10325 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10325 10326 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10326 10327 if (!snmp_append_data2(mpctl->b_cont,
10327 10328 &mp_tail,
10328 10329 (char *)&ipm6, (int)sizeof (ipm6))) {
10329 10330 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10330 10331 "failed to allocate %u bytes\n",
10331 10332 (uint_t)sizeof (ipm6)));
10332 10333 }
10333 10334 }
10334 10335 rw_exit(&ill->ill_mcast_lock);
10335 10336 ill_refrele(ill);
10336 10337 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10337 10338 }
10338 10339 rw_exit(&ipst->ips_ill_g_lock);
10339 10340
10340 10341 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10341 10342 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10342 10343 (int)optp->level, (int)optp->name, (int)optp->len));
10343 10344 qreply(q, mpctl);
10344 10345 return (mp2ctl);
10345 10346 }
10346 10347
10347 10348 /* IP multicast filtered sources */
10348 10349 static mblk_t *
10349 10350 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10350 10351 {
10351 10352 struct opthdr *optp;
10352 10353 mblk_t *mp2ctl;
10353 10354 ill_t *ill;
10354 10355 ipif_t *ipif;
10355 10356 ilm_t *ilm;
10356 10357 ip_grpsrc_t ips;
10357 10358 mblk_t *mp_tail = NULL;
10358 10359 ill_walk_context_t ctx;
10359 10360 zoneid_t zoneid;
10360 10361 int i;
10361 10362 slist_t *sl;
10362 10363
10363 10364 /*
10364 10365 * make a copy of the original message
10365 10366 */
10366 10367 mp2ctl = copymsg(mpctl);
10367 10368 zoneid = Q_TO_CONN(q)->conn_zoneid;
10368 10369
10369 10370 /* ipGroupSource table */
10370 10371 optp = (struct opthdr *)&mpctl->b_rptr[
10371 10372 sizeof (struct T_optmgmt_ack)];
10372 10373 optp->level = MIB2_IP;
10373 10374 optp->name = EXPER_IP_GROUP_SOURCES;
10374 10375
10375 10376 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10376 10377 ill = ILL_START_WALK_V4(&ctx, ipst);
10377 10378 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10378 10379 /* Make sure the ill isn't going away. */
10379 10380 if (!ill_check_and_refhold(ill))
10380 10381 continue;
10381 10382 rw_exit(&ipst->ips_ill_g_lock);
10382 10383 rw_enter(&ill->ill_mcast_lock, RW_READER);
10383 10384 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10384 10385 sl = ilm->ilm_filter;
10385 10386 if (ilm->ilm_zoneid != zoneid &&
10386 10387 ilm->ilm_zoneid != ALL_ZONES)
10387 10388 continue;
10388 10389 if (SLIST_IS_EMPTY(sl))
10389 10390 continue;
10390 10391
10391 10392 /* Is there an ipif for ilm_ifaddr? */
10392 10393 for (ipif = ill->ill_ipif; ipif != NULL;
10393 10394 ipif = ipif->ipif_next) {
10394 10395 if (!IPIF_IS_CONDEMNED(ipif) &&
10395 10396 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10396 10397 ilm->ilm_ifaddr != INADDR_ANY)
10397 10398 break;
10398 10399 }
10399 10400 if (ipif != NULL) {
10400 10401 ipif_get_name(ipif,
10401 10402 ips.ipGroupSourceIfIndex.o_bytes,
10402 10403 OCTET_LENGTH);
10403 10404 } else {
10404 10405 ill_get_name(ill,
10405 10406 ips.ipGroupSourceIfIndex.o_bytes,
10406 10407 OCTET_LENGTH);
10407 10408 }
10408 10409 ips.ipGroupSourceIfIndex.o_length =
10409 10410 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10410 10411
10411 10412 ips.ipGroupSourceGroup = ilm->ilm_addr;
10412 10413 for (i = 0; i < sl->sl_numsrc; i++) {
10413 10414 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10414 10415 continue;
10415 10416 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10416 10417 ips.ipGroupSourceAddress);
10417 10418 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10418 10419 (char *)&ips, (int)sizeof (ips)) == 0) {
10419 10420 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10420 10421 " failed to allocate %u bytes\n",
10421 10422 (uint_t)sizeof (ips)));
10422 10423 }
10423 10424 }
10424 10425 }
10425 10426 rw_exit(&ill->ill_mcast_lock);
10426 10427 ill_refrele(ill);
10427 10428 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10428 10429 }
10429 10430 rw_exit(&ipst->ips_ill_g_lock);
10430 10431 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10431 10432 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10432 10433 (int)optp->level, (int)optp->name, (int)optp->len));
10433 10434 qreply(q, mpctl);
10434 10435 return (mp2ctl);
10435 10436 }
10436 10437
10437 10438 /* IPv6 multicast filtered sources. */
10438 10439 static mblk_t *
10439 10440 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10440 10441 {
10441 10442 struct opthdr *optp;
10442 10443 mblk_t *mp2ctl;
10443 10444 ill_t *ill;
10444 10445 ilm_t *ilm;
10445 10446 ipv6_grpsrc_t ips6;
10446 10447 mblk_t *mp_tail = NULL;
10447 10448 ill_walk_context_t ctx;
10448 10449 zoneid_t zoneid;
10449 10450 int i;
10450 10451 slist_t *sl;
10451 10452
10452 10453 /*
10453 10454 * make a copy of the original message
10454 10455 */
10455 10456 mp2ctl = copymsg(mpctl);
10456 10457 zoneid = Q_TO_CONN(q)->conn_zoneid;
10457 10458
10458 10459 /* ip6GroupMember table */
10459 10460 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10460 10461 optp->level = MIB2_IP6;
10461 10462 optp->name = EXPER_IP6_GROUP_SOURCES;
10462 10463
10463 10464 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10464 10465 ill = ILL_START_WALK_V6(&ctx, ipst);
10465 10466 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10466 10467 /* Make sure the ill isn't going away. */
10467 10468 if (!ill_check_and_refhold(ill))
10468 10469 continue;
10469 10470 rw_exit(&ipst->ips_ill_g_lock);
10470 10471 /*
10471 10472 * Normally we don't have any members on under IPMP interfaces.
10472 10473 * We report them as a debugging aid.
10473 10474 */
10474 10475 rw_enter(&ill->ill_mcast_lock, RW_READER);
10475 10476 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10476 10477 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10477 10478 sl = ilm->ilm_filter;
10478 10479 if (ilm->ilm_zoneid != zoneid &&
10479 10480 ilm->ilm_zoneid != ALL_ZONES)
10480 10481 continue;
10481 10482 if (SLIST_IS_EMPTY(sl))
10482 10483 continue;
10483 10484 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10484 10485 for (i = 0; i < sl->sl_numsrc; i++) {
10485 10486 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10486 10487 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10487 10488 (char *)&ips6, (int)sizeof (ips6))) {
10488 10489 ip1dbg(("ip_snmp_get_mib2_ip6_"
10489 10490 "group_src: failed to allocate "
10490 10491 "%u bytes\n",
10491 10492 (uint_t)sizeof (ips6)));
10492 10493 }
10493 10494 }
10494 10495 }
10495 10496 rw_exit(&ill->ill_mcast_lock);
10496 10497 ill_refrele(ill);
10497 10498 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10498 10499 }
10499 10500 rw_exit(&ipst->ips_ill_g_lock);
10500 10501
10501 10502 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10502 10503 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10503 10504 (int)optp->level, (int)optp->name, (int)optp->len));
10504 10505 qreply(q, mpctl);
10505 10506 return (mp2ctl);
10506 10507 }
10507 10508
10508 10509 /* Multicast routing virtual interface table. */
10509 10510 static mblk_t *
10510 10511 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10511 10512 {
10512 10513 struct opthdr *optp;
10513 10514 mblk_t *mp2ctl;
10514 10515
10515 10516 /*
10516 10517 * make a copy of the original message
10517 10518 */
10518 10519 mp2ctl = copymsg(mpctl);
10519 10520
10520 10521 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10521 10522 optp->level = EXPER_DVMRP;
10522 10523 optp->name = EXPER_DVMRP_VIF;
10523 10524 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10524 10525 ip0dbg(("ip_mroute_vif: failed\n"));
10525 10526 }
10526 10527 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10527 10528 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10528 10529 (int)optp->level, (int)optp->name, (int)optp->len));
10529 10530 qreply(q, mpctl);
10530 10531 return (mp2ctl);
10531 10532 }
10532 10533
10533 10534 /* Multicast routing table. */
10534 10535 static mblk_t *
10535 10536 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10536 10537 {
10537 10538 struct opthdr *optp;
10538 10539 mblk_t *mp2ctl;
10539 10540
10540 10541 /*
10541 10542 * make a copy of the original message
10542 10543 */
10543 10544 mp2ctl = copymsg(mpctl);
10544 10545
10545 10546 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10546 10547 optp->level = EXPER_DVMRP;
10547 10548 optp->name = EXPER_DVMRP_MRT;
10548 10549 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10549 10550 ip0dbg(("ip_mroute_mrt: failed\n"));
10550 10551 }
10551 10552 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10552 10553 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10553 10554 (int)optp->level, (int)optp->name, (int)optp->len));
10554 10555 qreply(q, mpctl);
10555 10556 return (mp2ctl);
10556 10557 }
10557 10558
10558 10559 /*
10559 10560 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10560 10561 * in one IRE walk.
10561 10562 */
10562 10563 static mblk_t *
10563 10564 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10564 10565 ip_stack_t *ipst)
10565 10566 {
10566 10567 struct opthdr *optp;
10567 10568 mblk_t *mp2ctl; /* Returned */
10568 10569 mblk_t *mp3ctl; /* nettomedia */
10569 10570 mblk_t *mp4ctl; /* routeattrs */
10570 10571 iproutedata_t ird;
10571 10572 zoneid_t zoneid;
10572 10573
10573 10574 /*
10574 10575 * make copies of the original message
10575 10576 * - mp2ctl is returned unchanged to the caller for its use
10576 10577 * - mpctl is sent upstream as ipRouteEntryTable
10577 10578 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10578 10579 * - mp4ctl is sent upstream as ipRouteAttributeTable
10579 10580 */
10580 10581 mp2ctl = copymsg(mpctl);
10581 10582 mp3ctl = copymsg(mpctl);
10582 10583 mp4ctl = copymsg(mpctl);
10583 10584 if (mp3ctl == NULL || mp4ctl == NULL) {
10584 10585 freemsg(mp4ctl);
10585 10586 freemsg(mp3ctl);
10586 10587 freemsg(mp2ctl);
10587 10588 freemsg(mpctl);
10588 10589 return (NULL);
10589 10590 }
10590 10591
10591 10592 bzero(&ird, sizeof (ird));
10592 10593
10593 10594 ird.ird_route.lp_head = mpctl->b_cont;
10594 10595 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10595 10596 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10596 10597 /*
10597 10598 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10598 10599 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10599 10600 * intended a temporary solution until a proper MIB API is provided
10600 10601 * that provides complete filtering/caller-opt-in.
10601 10602 */
10602 10603 if (level == EXPER_IP_AND_ALL_IRES)
10603 10604 ird.ird_flags |= IRD_REPORT_ALL;
10604 10605
10605 10606 zoneid = Q_TO_CONN(q)->conn_zoneid;
10606 10607 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10607 10608
10608 10609 /* ipRouteEntryTable in mpctl */
10609 10610 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10610 10611 optp->level = MIB2_IP;
10611 10612 optp->name = MIB2_IP_ROUTE;
10612 10613 optp->len = msgdsize(ird.ird_route.lp_head);
10613 10614 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10614 10615 (int)optp->level, (int)optp->name, (int)optp->len));
10615 10616 qreply(q, mpctl);
10616 10617
10617 10618 /* ipNetToMediaEntryTable in mp3ctl */
10618 10619 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10619 10620
10620 10621 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10621 10622 optp->level = MIB2_IP;
10622 10623 optp->name = MIB2_IP_MEDIA;
10623 10624 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10624 10625 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10625 10626 (int)optp->level, (int)optp->name, (int)optp->len));
10626 10627 qreply(q, mp3ctl);
10627 10628
10628 10629 /* ipRouteAttributeTable in mp4ctl */
10629 10630 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10630 10631 optp->level = MIB2_IP;
10631 10632 optp->name = EXPER_IP_RTATTR;
10632 10633 optp->len = msgdsize(ird.ird_attrs.lp_head);
10633 10634 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10634 10635 (int)optp->level, (int)optp->name, (int)optp->len));
10635 10636 if (optp->len == 0)
10636 10637 freemsg(mp4ctl);
10637 10638 else
10638 10639 qreply(q, mp4ctl);
10639 10640
10640 10641 return (mp2ctl);
10641 10642 }
10642 10643
10643 10644 /*
10644 10645 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10645 10646 * ipv6NetToMediaEntryTable in an NDP walk.
10646 10647 */
10647 10648 static mblk_t *
10648 10649 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10649 10650 ip_stack_t *ipst)
10650 10651 {
10651 10652 struct opthdr *optp;
10652 10653 mblk_t *mp2ctl; /* Returned */
10653 10654 mblk_t *mp3ctl; /* nettomedia */
10654 10655 mblk_t *mp4ctl; /* routeattrs */
10655 10656 iproutedata_t ird;
10656 10657 zoneid_t zoneid;
10657 10658
10658 10659 /*
10659 10660 * make copies of the original message
10660 10661 * - mp2ctl is returned unchanged to the caller for its use
10661 10662 * - mpctl is sent upstream as ipv6RouteEntryTable
10662 10663 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10663 10664 * - mp4ctl is sent upstream as ipv6RouteAttributeTable
10664 10665 */
10665 10666 mp2ctl = copymsg(mpctl);
10666 10667 mp3ctl = copymsg(mpctl);
10667 10668 mp4ctl = copymsg(mpctl);
10668 10669 if (mp3ctl == NULL || mp4ctl == NULL) {
10669 10670 freemsg(mp4ctl);
10670 10671 freemsg(mp3ctl);
10671 10672 freemsg(mp2ctl);
10672 10673 freemsg(mpctl);
10673 10674 return (NULL);
10674 10675 }
10675 10676
10676 10677 bzero(&ird, sizeof (ird));
10677 10678
10678 10679 ird.ird_route.lp_head = mpctl->b_cont;
10679 10680 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10680 10681 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10681 10682 /*
10682 10683 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10683 10684 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10684 10685 * intended a temporary solution until a proper MIB API is provided
10685 10686 * that provides complete filtering/caller-opt-in.
10686 10687 */
10687 10688 if (level == EXPER_IP_AND_ALL_IRES)
10688 10689 ird.ird_flags |= IRD_REPORT_ALL;
10689 10690
10690 10691 zoneid = Q_TO_CONN(q)->conn_zoneid;
10691 10692 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10692 10693
10693 10694 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10694 10695 optp->level = MIB2_IP6;
10695 10696 optp->name = MIB2_IP6_ROUTE;
10696 10697 optp->len = msgdsize(ird.ird_route.lp_head);
10697 10698 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10698 10699 (int)optp->level, (int)optp->name, (int)optp->len));
10699 10700 qreply(q, mpctl);
10700 10701
10701 10702 /* ipv6NetToMediaEntryTable in mp3ctl */
10702 10703 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10703 10704
10704 10705 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10705 10706 optp->level = MIB2_IP6;
10706 10707 optp->name = MIB2_IP6_MEDIA;
10707 10708 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10708 10709 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10709 10710 (int)optp->level, (int)optp->name, (int)optp->len));
10710 10711 qreply(q, mp3ctl);
10711 10712
10712 10713 /* ipv6RouteAttributeTable in mp4ctl */
10713 10714 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10714 10715 optp->level = MIB2_IP6;
10715 10716 optp->name = EXPER_IP_RTATTR;
10716 10717 optp->len = msgdsize(ird.ird_attrs.lp_head);
10717 10718 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10718 10719 (int)optp->level, (int)optp->name, (int)optp->len));
10719 10720 if (optp->len == 0)
10720 10721 freemsg(mp4ctl);
10721 10722 else
10722 10723 qreply(q, mp4ctl);
10723 10724
10724 10725 return (mp2ctl);
10725 10726 }
10726 10727
10727 10728 /*
10728 10729 * IPv6 mib: One per ill
10729 10730 */
10730 10731 static mblk_t *
10731 10732 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10732 10733 boolean_t legacy_req)
10733 10734 {
10734 10735 struct opthdr *optp;
10735 10736 mblk_t *mp2ctl;
10736 10737 ill_t *ill;
10737 10738 ill_walk_context_t ctx;
10738 10739 mblk_t *mp_tail = NULL;
10739 10740 mib2_ipv6AddrEntry_t mae6;
10740 10741 mib2_ipIfStatsEntry_t *ise;
10741 10742 size_t ise_size, iae_size;
10742 10743
10743 10744 /*
10744 10745 * Make a copy of the original message
10745 10746 */
10746 10747 mp2ctl = copymsg(mpctl);
10747 10748
10748 10749 /* fixed length IPv6 structure ... */
10749 10750
10750 10751 if (legacy_req) {
10751 10752 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10752 10753 mib2_ipIfStatsEntry_t);
10753 10754 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10754 10755 } else {
10755 10756 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10756 10757 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10757 10758 }
10758 10759
10759 10760 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10760 10761 optp->level = MIB2_IP6;
10761 10762 optp->name = 0;
10762 10763 /* Include "unknown interface" ip6_mib */
10763 10764 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10764 10765 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10765 10766 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10766 10767 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10767 10768 ipst->ips_ipv6_forwarding ? 1 : 2);
10768 10769 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10769 10770 ipst->ips_ipv6_def_hops);
10770 10771 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10771 10772 sizeof (mib2_ipIfStatsEntry_t));
10772 10773 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10773 10774 sizeof (mib2_ipv6AddrEntry_t));
10774 10775 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10775 10776 sizeof (mib2_ipv6RouteEntry_t));
10776 10777 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10777 10778 sizeof (mib2_ipv6NetToMediaEntry_t));
10778 10779 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10779 10780 sizeof (ipv6_member_t));
10780 10781 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10781 10782 sizeof (ipv6_grpsrc_t));
10782 10783
10783 10784 /*
10784 10785 * Synchronize 64- and 32-bit counters
10785 10786 */
10786 10787 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10787 10788 ipIfStatsHCInReceives);
10788 10789 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10789 10790 ipIfStatsHCInDelivers);
10790 10791 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10791 10792 ipIfStatsHCOutRequests);
10792 10793 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10793 10794 ipIfStatsHCOutForwDatagrams);
10794 10795 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10795 10796 ipIfStatsHCOutMcastPkts);
10796 10797 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10797 10798 ipIfStatsHCInMcastPkts);
10798 10799
10799 10800 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10800 10801 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10801 10802 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10802 10803 (uint_t)ise_size));
10803 10804 } else if (legacy_req) {
10804 10805 /* Adjust the EntrySize fields for legacy requests. */
10805 10806 ise =
10806 10807 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10807 10808 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10808 10809 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10809 10810 }
10810 10811
10811 10812 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10812 10813 ill = ILL_START_WALK_V6(&ctx, ipst);
10813 10814 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10814 10815 ill->ill_ip_mib->ipIfStatsIfIndex =
10815 10816 ill->ill_phyint->phyint_ifindex;
10816 10817 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10817 10818 ipst->ips_ipv6_forwarding ? 1 : 2);
10818 10819 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10819 10820 ill->ill_max_hops);
10820 10821
10821 10822 /*
10822 10823 * Synchronize 64- and 32-bit counters
10823 10824 */
10824 10825 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10825 10826 ipIfStatsHCInReceives);
10826 10827 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10827 10828 ipIfStatsHCInDelivers);
10828 10829 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10829 10830 ipIfStatsHCOutRequests);
10830 10831 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10831 10832 ipIfStatsHCOutForwDatagrams);
10832 10833 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10833 10834 ipIfStatsHCOutMcastPkts);
10834 10835 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10835 10836 ipIfStatsHCInMcastPkts);
10836 10837
10837 10838 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10838 10839 (char *)ill->ill_ip_mib, (int)ise_size)) {
10839 10840 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10840 10841 "%u bytes\n", (uint_t)ise_size));
10841 10842 } else if (legacy_req) {
10842 10843 /* Adjust the EntrySize fields for legacy requests. */
10843 10844 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10844 10845 (int)ise_size);
10845 10846 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10846 10847 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10847 10848 }
10848 10849 }
10849 10850 rw_exit(&ipst->ips_ill_g_lock);
10850 10851
10851 10852 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10852 10853 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10853 10854 (int)optp->level, (int)optp->name, (int)optp->len));
10854 10855 qreply(q, mpctl);
10855 10856 return (mp2ctl);
10856 10857 }
10857 10858
10858 10859 /*
10859 10860 * ICMPv6 mib: One per ill
10860 10861 */
10861 10862 static mblk_t *
10862 10863 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10863 10864 {
10864 10865 struct opthdr *optp;
10865 10866 mblk_t *mp2ctl;
10866 10867 ill_t *ill;
10867 10868 ill_walk_context_t ctx;
10868 10869 mblk_t *mp_tail = NULL;
10869 10870 /*
10870 10871 * Make a copy of the original message
10871 10872 */
10872 10873 mp2ctl = copymsg(mpctl);
10873 10874
10874 10875 /* fixed length ICMPv6 structure ... */
10875 10876
10876 10877 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10877 10878 optp->level = MIB2_ICMP6;
10878 10879 optp->name = 0;
10879 10880 /* Include "unknown interface" icmp6_mib */
10880 10881 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10881 10882 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10882 10883 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10883 10884 sizeof (mib2_ipv6IfIcmpEntry_t);
10884 10885 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10885 10886 (char *)&ipst->ips_icmp6_mib,
10886 10887 (int)sizeof (ipst->ips_icmp6_mib))) {
10887 10888 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10888 10889 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10889 10890 }
10890 10891
10891 10892 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10892 10893 ill = ILL_START_WALK_V6(&ctx, ipst);
10893 10894 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10894 10895 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10895 10896 ill->ill_phyint->phyint_ifindex;
10896 10897 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10897 10898 (char *)ill->ill_icmp6_mib,
10898 10899 (int)sizeof (*ill->ill_icmp6_mib))) {
10899 10900 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10900 10901 "%u bytes\n",
10901 10902 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10902 10903 }
10903 10904 }
10904 10905 rw_exit(&ipst->ips_ill_g_lock);
10905 10906
10906 10907 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10907 10908 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10908 10909 (int)optp->level, (int)optp->name, (int)optp->len));
10909 10910 qreply(q, mpctl);
10910 10911 return (mp2ctl);
10911 10912 }
10912 10913
10913 10914 /*
10914 10915 * ire_walk routine to create both ipRouteEntryTable and
10915 10916 * ipRouteAttributeTable in one IRE walk
10916 10917 */
10917 10918 static void
10918 10919 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10919 10920 {
10920 10921 ill_t *ill;
10921 10922 mib2_ipRouteEntry_t *re;
10922 10923 mib2_ipAttributeEntry_t iaes;
10923 10924 tsol_ire_gw_secattr_t *attrp;
10924 10925 tsol_gc_t *gc = NULL;
10925 10926 tsol_gcgrp_t *gcgrp = NULL;
10926 10927 ip_stack_t *ipst = ire->ire_ipst;
10927 10928
10928 10929 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10929 10930
10930 10931 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10931 10932 if (ire->ire_testhidden)
10932 10933 return;
10933 10934 if (ire->ire_type & IRE_IF_CLONE)
10934 10935 return;
10935 10936 }
10936 10937
10937 10938 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10938 10939 return;
10939 10940
10940 10941 if ((attrp = ire->ire_gw_secattr) != NULL) {
10941 10942 mutex_enter(&attrp->igsa_lock);
10942 10943 if ((gc = attrp->igsa_gc) != NULL) {
10943 10944 gcgrp = gc->gc_grp;
10944 10945 ASSERT(gcgrp != NULL);
10945 10946 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10946 10947 }
10947 10948 mutex_exit(&attrp->igsa_lock);
10948 10949 }
10949 10950 /*
10950 10951 * Return all IRE types for route table... let caller pick and choose
10951 10952 */
10952 10953 re->ipRouteDest = ire->ire_addr;
10953 10954 ill = ire->ire_ill;
10954 10955 re->ipRouteIfIndex.o_length = 0;
10955 10956 if (ill != NULL) {
10956 10957 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10957 10958 re->ipRouteIfIndex.o_length =
10958 10959 mi_strlen(re->ipRouteIfIndex.o_bytes);
10959 10960 }
10960 10961 re->ipRouteMetric1 = -1;
10961 10962 re->ipRouteMetric2 = -1;
10962 10963 re->ipRouteMetric3 = -1;
10963 10964 re->ipRouteMetric4 = -1;
10964 10965
10965 10966 re->ipRouteNextHop = ire->ire_gateway_addr;
10966 10967 /* indirect(4), direct(3), or invalid(2) */
10967 10968 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10968 10969 re->ipRouteType = 2;
10969 10970 else if (ire->ire_type & IRE_ONLINK)
10970 10971 re->ipRouteType = 3;
10971 10972 else
10972 10973 re->ipRouteType = 4;
10973 10974
10974 10975 re->ipRouteProto = -1;
10975 10976 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10976 10977 re->ipRouteMask = ire->ire_mask;
10977 10978 re->ipRouteMetric5 = -1;
10978 10979 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10979 10980 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10980 10981 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10981 10982
10982 10983 re->ipRouteInfo.re_frag_flag = 0;
10983 10984 re->ipRouteInfo.re_rtt = 0;
10984 10985 re->ipRouteInfo.re_src_addr = 0;
10985 10986 re->ipRouteInfo.re_ref = ire->ire_refcnt;
10986 10987 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10987 10988 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10988 10989 re->ipRouteInfo.re_flags = ire->ire_flags;
10989 10990
10990 10991 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10991 10992 if (ire->ire_type & IRE_INTERFACE) {
10992 10993 ire_t *child;
10993 10994
10994 10995 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10995 10996 child = ire->ire_dep_children;
10996 10997 while (child != NULL) {
10997 10998 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10998 10999 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10999 11000 child = child->ire_dep_sib_next;
11000 11001 }
11001 11002 rw_exit(&ipst->ips_ire_dep_lock);
11002 11003 }
11003 11004
11004 11005 if (ire->ire_flags & RTF_DYNAMIC) {
11005 11006 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11006 11007 } else {
11007 11008 re->ipRouteInfo.re_ire_type = ire->ire_type;
11008 11009 }
11009 11010
11010 11011 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11011 11012 (char *)re, (int)sizeof (*re))) {
11012 11013 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
11013 11014 (uint_t)sizeof (*re)));
11014 11015 }
11015 11016
11016 11017 if (gc != NULL) {
11017 11018 iaes.iae_routeidx = ird->ird_idx;
11018 11019 iaes.iae_doi = gc->gc_db->gcdb_doi;
11019 11020 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11020 11021
11021 11022 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11022 11023 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11023 11024 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
11024 11025 "bytes\n", (uint_t)sizeof (iaes)));
11025 11026 }
11026 11027 }
11027 11028
11028 11029 /* bump route index for next pass */
11029 11030 ird->ird_idx++;
11030 11031
11031 11032 kmem_free(re, sizeof (*re));
11032 11033 if (gcgrp != NULL)
11033 11034 rw_exit(&gcgrp->gcgrp_rwlock);
11034 11035 }
11035 11036
11036 11037 /*
11037 11038 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
11038 11039 */
11039 11040 static void
11040 11041 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
11041 11042 {
11042 11043 ill_t *ill;
11043 11044 mib2_ipv6RouteEntry_t *re;
11044 11045 mib2_ipAttributeEntry_t iaes;
11045 11046 tsol_ire_gw_secattr_t *attrp;
11046 11047 tsol_gc_t *gc = NULL;
11047 11048 tsol_gcgrp_t *gcgrp = NULL;
11048 11049 ip_stack_t *ipst = ire->ire_ipst;
11049 11050
11050 11051 ASSERT(ire->ire_ipversion == IPV6_VERSION);
11051 11052
11052 11053 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
11053 11054 if (ire->ire_testhidden)
11054 11055 return;
11055 11056 if (ire->ire_type & IRE_IF_CLONE)
11056 11057 return;
11057 11058 }
11058 11059
11059 11060 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11060 11061 return;
11061 11062
11062 11063 if ((attrp = ire->ire_gw_secattr) != NULL) {
11063 11064 mutex_enter(&attrp->igsa_lock);
11064 11065 if ((gc = attrp->igsa_gc) != NULL) {
11065 11066 gcgrp = gc->gc_grp;
11066 11067 ASSERT(gcgrp != NULL);
11067 11068 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11068 11069 }
11069 11070 mutex_exit(&attrp->igsa_lock);
11070 11071 }
11071 11072 /*
11072 11073 * Return all IRE types for route table... let caller pick and choose
11073 11074 */
11074 11075 re->ipv6RouteDest = ire->ire_addr_v6;
11075 11076 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11076 11077 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11077 11078 re->ipv6RouteIfIndex.o_length = 0;
11078 11079 ill = ire->ire_ill;
11079 11080 if (ill != NULL) {
11080 11081 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11081 11082 re->ipv6RouteIfIndex.o_length =
11082 11083 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11083 11084 }
11084 11085
11085 11086 ASSERT(!(ire->ire_type & IRE_BROADCAST));
11086 11087
11087 11088 mutex_enter(&ire->ire_lock);
11088 11089 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11089 11090 mutex_exit(&ire->ire_lock);
11090 11091
11091 11092 /* remote(4), local(3), or discard(2) */
11092 11093 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11093 11094 re->ipv6RouteType = 2;
11094 11095 else if (ire->ire_type & IRE_ONLINK)
11095 11096 re->ipv6RouteType = 3;
11096 11097 else
11097 11098 re->ipv6RouteType = 4;
11098 11099
11099 11100 re->ipv6RouteProtocol = -1;
11100 11101 re->ipv6RoutePolicy = 0;
11101 11102 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
11102 11103 re->ipv6RouteNextHopRDI = 0;
11103 11104 re->ipv6RouteWeight = 0;
11104 11105 re->ipv6RouteMetric = 0;
11105 11106 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11106 11107 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11107 11108 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11108 11109
11109 11110 re->ipv6RouteInfo.re_frag_flag = 0;
11110 11111 re->ipv6RouteInfo.re_rtt = 0;
11111 11112 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
11112 11113 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
11113 11114 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
11114 11115 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
11115 11116 re->ipv6RouteInfo.re_flags = ire->ire_flags;
11116 11117
11117 11118 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11118 11119 if (ire->ire_type & IRE_INTERFACE) {
11119 11120 ire_t *child;
11120 11121
11121 11122 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11122 11123 child = ire->ire_dep_children;
11123 11124 while (child != NULL) {
11124 11125 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11125 11126 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11126 11127 child = child->ire_dep_sib_next;
11127 11128 }
11128 11129 rw_exit(&ipst->ips_ire_dep_lock);
11129 11130 }
11130 11131 if (ire->ire_flags & RTF_DYNAMIC) {
11131 11132 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11132 11133 } else {
11133 11134 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
11134 11135 }
11135 11136
11136 11137 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11137 11138 (char *)re, (int)sizeof (*re))) {
11138 11139 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11139 11140 (uint_t)sizeof (*re)));
11140 11141 }
11141 11142
11142 11143 if (gc != NULL) {
11143 11144 iaes.iae_routeidx = ird->ird_idx;
11144 11145 iaes.iae_doi = gc->gc_db->gcdb_doi;
11145 11146 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11146 11147
11147 11148 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11148 11149 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11149 11150 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11150 11151 "bytes\n", (uint_t)sizeof (iaes)));
11151 11152 }
11152 11153 }
11153 11154
11154 11155 /* bump route index for next pass */
11155 11156 ird->ird_idx++;
11156 11157
11157 11158 kmem_free(re, sizeof (*re));
11158 11159 if (gcgrp != NULL)
11159 11160 rw_exit(&gcgrp->gcgrp_rwlock);
11160 11161 }
11161 11162
11162 11163 /*
11163 11164 * ncec_walk routine to create ipv6NetToMediaEntryTable
11164 11165 */
11165 11166 static void
11166 11167 ip_snmp_get2_v6_media(ncec_t *ncec, void *ptr)
11167 11168 {
11168 11169 iproutedata_t *ird = ptr;
11169 11170 ill_t *ill;
11170 11171 mib2_ipv6NetToMediaEntry_t ntme;
11171 11172
11172 11173 ill = ncec->ncec_ill;
11173 11174 /* skip arpce entries, and loopback ncec entries */
11174 11175 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11175 11176 return;
11176 11177 /*
11177 11178 * Neighbor cache entry attached to IRE with on-link
11178 11179 * destination.
11179 11180 * We report all IPMP groups on ncec_ill which is normally the upper.
11180 11181 */
11181 11182 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11182 11183 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11183 11184 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11184 11185 if (ncec->ncec_lladdr != NULL) {
11185 11186 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11186 11187 ntme.ipv6NetToMediaPhysAddress.o_length);
11187 11188 }
11188 11189 /*
11189 11190 * Note: Returns ND_* states. Should be:
11190 11191 * reachable(1), stale(2), delay(3), probe(4),
11191 11192 * invalid(5), unknown(6)
11192 11193 */
11193 11194 ntme.ipv6NetToMediaState = ncec->ncec_state;
11194 11195 ntme.ipv6NetToMediaLastUpdated = 0;
11195 11196
11196 11197 /* other(1), dynamic(2), static(3), local(4) */
11197 11198 if (NCE_MYADDR(ncec)) {
11198 11199 ntme.ipv6NetToMediaType = 4;
11199 11200 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11200 11201 ntme.ipv6NetToMediaType = 1; /* proxy */
11201 11202 } else if (ncec->ncec_flags & NCE_F_STATIC) {
11202 11203 ntme.ipv6NetToMediaType = 3;
11203 11204 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11204 11205 ntme.ipv6NetToMediaType = 1;
11205 11206 } else {
11206 11207 ntme.ipv6NetToMediaType = 2;
11207 11208 }
11208 11209
11209 11210 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11210 11211 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11211 11212 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11212 11213 (uint_t)sizeof (ntme)));
11213 11214 }
11214 11215 }
11215 11216
11216 11217 int
11217 11218 nce2ace(ncec_t *ncec)
11218 11219 {
11219 11220 int flags = 0;
11220 11221
11221 11222 if (NCE_ISREACHABLE(ncec))
11222 11223 flags |= ACE_F_RESOLVED;
11223 11224 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11224 11225 flags |= ACE_F_AUTHORITY;
11225 11226 if (ncec->ncec_flags & NCE_F_PUBLISH)
11226 11227 flags |= ACE_F_PUBLISH;
11227 11228 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11228 11229 flags |= ACE_F_PERMANENT;
11229 11230 if (NCE_MYADDR(ncec))
11230 11231 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11231 11232 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11232 11233 flags |= ACE_F_UNVERIFIED;
11233 11234 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11234 11235 flags |= ACE_F_AUTHORITY;
11235 11236 if (ncec->ncec_flags & NCE_F_DELAYED)
11236 11237 flags |= ACE_F_DELAYED;
11237 11238 return (flags);
11238 11239 }
11239 11240
11240 11241 /*
11241 11242 * ncec_walk routine to create ipNetToMediaEntryTable
11242 11243 */
11243 11244 static void
11244 11245 ip_snmp_get2_v4_media(ncec_t *ncec, void *ptr)
11245 11246 {
11246 11247 iproutedata_t *ird = ptr;
11247 11248 ill_t *ill;
11248 11249 mib2_ipNetToMediaEntry_t ntme;
11249 11250 const char *name = "unknown";
11250 11251 ipaddr_t ncec_addr;
11251 11252
11252 11253 ill = ncec->ncec_ill;
11253 11254 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11254 11255 ill->ill_net_type == IRE_LOOPBACK)
11255 11256 return;
11256 11257
11257 11258 /* We report all IPMP groups on ncec_ill which is normally the upper. */
11258 11259 name = ill->ill_name;
11259 11260 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11260 11261 if (NCE_MYADDR(ncec)) {
11261 11262 ntme.ipNetToMediaType = 4;
11262 11263 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11263 11264 ntme.ipNetToMediaType = 1;
11264 11265 } else {
11265 11266 ntme.ipNetToMediaType = 3;
11266 11267 }
11267 11268 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11268 11269 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11269 11270 ntme.ipNetToMediaIfIndex.o_length);
11270 11271
11271 11272 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11272 11273 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11273 11274
11274 11275 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11275 11276 ncec_addr = INADDR_BROADCAST;
11276 11277 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11277 11278 sizeof (ncec_addr));
11278 11279 /*
11279 11280 * map all the flags to the ACE counterpart.
11280 11281 */
11281 11282 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11282 11283
11283 11284 ntme.ipNetToMediaPhysAddress.o_length =
11284 11285 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11285 11286
11286 11287 if (!NCE_ISREACHABLE(ncec))
11287 11288 ntme.ipNetToMediaPhysAddress.o_length = 0;
11288 11289 else {
11289 11290 if (ncec->ncec_lladdr != NULL) {
11290 11291 bcopy(ncec->ncec_lladdr,
11291 11292 ntme.ipNetToMediaPhysAddress.o_bytes,
11292 11293 ntme.ipNetToMediaPhysAddress.o_length);
11293 11294 }
11294 11295 }
11295 11296
11296 11297 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11297 11298 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11298 11299 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11299 11300 (uint_t)sizeof (ntme)));
11300 11301 }
11301 11302 }
11302 11303
11303 11304 /*
11304 11305 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11305 11306 */
11306 11307 /* ARGSUSED */
11307 11308 int
11308 11309 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11309 11310 {
11310 11311 switch (level) {
11311 11312 case MIB2_IP:
11312 11313 case MIB2_ICMP:
11313 11314 switch (name) {
11314 11315 default:
11315 11316 break;
11316 11317 }
11317 11318 return (1);
11318 11319 default:
11319 11320 return (1);
11320 11321 }
11321 11322 }
11322 11323
11323 11324 /*
11324 11325 * When there exists both a 64- and 32-bit counter of a particular type
11325 11326 * (i.e., InReceives), only the 64-bit counters are added.
11326 11327 */
11327 11328 void
11328 11329 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11329 11330 {
11330 11331 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11331 11332 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11332 11333 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11333 11334 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11334 11335 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11335 11336 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11336 11337 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11337 11338 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11338 11339 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11339 11340 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11340 11341 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11341 11342 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11342 11343 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11343 11344 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11344 11345 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11345 11346 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11346 11347 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11347 11348 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11348 11349 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11349 11350 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11350 11351 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11351 11352 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11352 11353 o2->ipIfStatsInWrongIPVersion);
11353 11354 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11354 11355 o2->ipIfStatsInWrongIPVersion);
11355 11356 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11356 11357 o2->ipIfStatsOutSwitchIPVersion);
11357 11358 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11358 11359 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11359 11360 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11360 11361 o2->ipIfStatsHCInForwDatagrams);
11361 11362 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11362 11363 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11363 11364 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11364 11365 o2->ipIfStatsHCOutForwDatagrams);
11365 11366 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11366 11367 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11367 11368 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11368 11369 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11369 11370 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11370 11371 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11371 11372 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11372 11373 o2->ipIfStatsHCOutMcastOctets);
11373 11374 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11374 11375 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11375 11376 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11376 11377 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11377 11378 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11378 11379 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11379 11380 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11380 11381 }
11381 11382
11382 11383 void
11383 11384 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11384 11385 {
11385 11386 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11386 11387 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11387 11388 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11388 11389 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11389 11390 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11390 11391 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11391 11392 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11392 11393 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11393 11394 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11394 11395 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11395 11396 o2->ipv6IfIcmpInRouterSolicits);
11396 11397 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11397 11398 o2->ipv6IfIcmpInRouterAdvertisements);
11398 11399 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11399 11400 o2->ipv6IfIcmpInNeighborSolicits);
11400 11401 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11401 11402 o2->ipv6IfIcmpInNeighborAdvertisements);
11402 11403 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11403 11404 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11404 11405 o2->ipv6IfIcmpInGroupMembQueries);
11405 11406 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11406 11407 o2->ipv6IfIcmpInGroupMembResponses);
11407 11408 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11408 11409 o2->ipv6IfIcmpInGroupMembReductions);
11409 11410 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11410 11411 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11411 11412 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11412 11413 o2->ipv6IfIcmpOutDestUnreachs);
11413 11414 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11414 11415 o2->ipv6IfIcmpOutAdminProhibs);
11415 11416 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11416 11417 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11417 11418 o2->ipv6IfIcmpOutParmProblems);
11418 11419 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11419 11420 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11420 11421 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11421 11422 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11422 11423 o2->ipv6IfIcmpOutRouterSolicits);
11423 11424 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11424 11425 o2->ipv6IfIcmpOutRouterAdvertisements);
11425 11426 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11426 11427 o2->ipv6IfIcmpOutNeighborSolicits);
11427 11428 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11428 11429 o2->ipv6IfIcmpOutNeighborAdvertisements);
11429 11430 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11430 11431 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11431 11432 o2->ipv6IfIcmpOutGroupMembQueries);
11432 11433 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11433 11434 o2->ipv6IfIcmpOutGroupMembResponses);
11434 11435 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11435 11436 o2->ipv6IfIcmpOutGroupMembReductions);
11436 11437 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11437 11438 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11438 11439 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11439 11440 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11440 11441 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11441 11442 o2->ipv6IfIcmpInBadNeighborSolicitations);
11442 11443 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11443 11444 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11444 11445 o2->ipv6IfIcmpInGroupMembTotal);
11445 11446 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11446 11447 o2->ipv6IfIcmpInGroupMembBadQueries);
11447 11448 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11448 11449 o2->ipv6IfIcmpInGroupMembBadReports);
11449 11450 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11450 11451 o2->ipv6IfIcmpInGroupMembOurReports);
11451 11452 }
11452 11453
11453 11454 /*
11454 11455 * Called before the options are updated to check if this packet will
11455 11456 * be source routed from here.
11456 11457 * This routine assumes that the options are well formed i.e. that they
11457 11458 * have already been checked.
11458 11459 */
11459 11460 boolean_t
11460 11461 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11461 11462 {
11462 11463 ipoptp_t opts;
11463 11464 uchar_t *opt;
11464 11465 uint8_t optval;
11465 11466 uint8_t optlen;
11466 11467 ipaddr_t dst;
11467 11468
11468 11469 if (IS_SIMPLE_IPH(ipha)) {
11469 11470 ip2dbg(("not source routed\n"));
11470 11471 return (B_FALSE);
11471 11472 }
11472 11473 dst = ipha->ipha_dst;
11473 11474 for (optval = ipoptp_first(&opts, ipha);
11474 11475 optval != IPOPT_EOL;
11475 11476 optval = ipoptp_next(&opts)) {
11476 11477 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11477 11478 opt = opts.ipoptp_cur;
11478 11479 optlen = opts.ipoptp_len;
11479 11480 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11480 11481 optval, optlen));
11481 11482 switch (optval) {
11482 11483 uint32_t off;
11483 11484 case IPOPT_SSRR:
11484 11485 case IPOPT_LSRR:
11485 11486 /*
11486 11487 * If dst is one of our addresses and there are some
11487 11488 * entries left in the source route return (true).
11488 11489 */
11489 11490 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11490 11491 ip2dbg(("ip_source_routed: not next"
11491 11492 " source route 0x%x\n",
11492 11493 ntohl(dst)));
11493 11494 return (B_FALSE);
11494 11495 }
11495 11496 off = opt[IPOPT_OFFSET];
11496 11497 off--;
11497 11498 if (optlen < IP_ADDR_LEN ||
11498 11499 off > optlen - IP_ADDR_LEN) {
11499 11500 /* End of source route */
11500 11501 ip1dbg(("ip_source_routed: end of SR\n"));
11501 11502 return (B_FALSE);
11502 11503 }
11503 11504 return (B_TRUE);
11504 11505 }
11505 11506 }
11506 11507 ip2dbg(("not source routed\n"));
11507 11508 return (B_FALSE);
11508 11509 }
11509 11510
11510 11511 /*
11511 11512 * ip_unbind is called by the transports to remove a conn from
11512 11513 * the fanout table.
11513 11514 */
11514 11515 void
11515 11516 ip_unbind(conn_t *connp)
11516 11517 {
11517 11518
11518 11519 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11519 11520
11520 11521 if (is_system_labeled() && connp->conn_anon_port) {
11521 11522 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11522 11523 connp->conn_mlp_type, connp->conn_proto,
11523 11524 ntohs(connp->conn_lport), B_FALSE);
11524 11525 connp->conn_anon_port = 0;
11525 11526 }
11526 11527 connp->conn_mlp_type = mlptSingle;
11527 11528
11528 11529 ipcl_hash_remove(connp);
11529 11530 }
11530 11531
11531 11532 /*
11532 11533 * Used for deciding the MSS size for the upper layer. Thus
11533 11534 * we need to check the outbound policy values in the conn.
11534 11535 */
11535 11536 int
11536 11537 conn_ipsec_length(conn_t *connp)
11537 11538 {
11538 11539 ipsec_latch_t *ipl;
11539 11540
11540 11541 ipl = connp->conn_latch;
11541 11542 if (ipl == NULL)
11542 11543 return (0);
11543 11544
11544 11545 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11545 11546 return (0);
11546 11547
11547 11548 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11548 11549 }
11549 11550
11550 11551 /*
11551 11552 * Returns an estimate of the IPsec headers size. This is used if
11552 11553 * we don't want to call into IPsec to get the exact size.
11553 11554 */
11554 11555 int
11555 11556 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11556 11557 {
11557 11558 ipsec_action_t *a;
11558 11559
11559 11560 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11560 11561 return (0);
11561 11562
11562 11563 a = ixa->ixa_ipsec_action;
11563 11564 if (a == NULL) {
11564 11565 ASSERT(ixa->ixa_ipsec_policy != NULL);
11565 11566 a = ixa->ixa_ipsec_policy->ipsp_act;
11566 11567 }
11567 11568 ASSERT(a != NULL);
11568 11569
11569 11570 return (a->ipa_ovhd);
11570 11571 }
11571 11572
11572 11573 /*
11573 11574 * If there are any source route options, return the true final
11574 11575 * destination. Otherwise, return the destination.
11575 11576 */
11576 11577 ipaddr_t
11577 11578 ip_get_dst(ipha_t *ipha)
11578 11579 {
11579 11580 ipoptp_t opts;
11580 11581 uchar_t *opt;
11581 11582 uint8_t optval;
11582 11583 uint8_t optlen;
11583 11584 ipaddr_t dst;
11584 11585 uint32_t off;
11585 11586
11586 11587 dst = ipha->ipha_dst;
11587 11588
11588 11589 if (IS_SIMPLE_IPH(ipha))
11589 11590 return (dst);
11590 11591
11591 11592 for (optval = ipoptp_first(&opts, ipha);
11592 11593 optval != IPOPT_EOL;
11593 11594 optval = ipoptp_next(&opts)) {
11594 11595 opt = opts.ipoptp_cur;
11595 11596 optlen = opts.ipoptp_len;
11596 11597 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11597 11598 switch (optval) {
11598 11599 case IPOPT_SSRR:
11599 11600 case IPOPT_LSRR:
11600 11601 off = opt[IPOPT_OFFSET];
11601 11602 /*
11602 11603 * If one of the conditions is true, it means
11603 11604 * end of options and dst already has the right
11604 11605 * value.
11605 11606 */
11606 11607 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11607 11608 off = optlen - IP_ADDR_LEN;
11608 11609 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11609 11610 }
11610 11611 return (dst);
11611 11612 default:
11612 11613 break;
11613 11614 }
11614 11615 }
11615 11616
11616 11617 return (dst);
11617 11618 }
11618 11619
11619 11620 /*
11620 11621 * Outbound IP fragmentation routine.
11621 11622 * Assumes the caller has checked whether or not fragmentation should
11622 11623 * be allowed. Here we copy the DF bit from the header to all the generated
11623 11624 * fragments.
11624 11625 */
11625 11626 int
11626 11627 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11627 11628 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11628 11629 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11629 11630 {
11630 11631 int i1;
11631 11632 int hdr_len;
11632 11633 mblk_t *hdr_mp;
11633 11634 ipha_t *ipha;
11634 11635 int ip_data_end;
11635 11636 int len;
11636 11637 mblk_t *mp = mp_orig;
11637 11638 int offset;
11638 11639 ill_t *ill = nce->nce_ill;
11639 11640 ip_stack_t *ipst = ill->ill_ipst;
11640 11641 mblk_t *carve_mp;
11641 11642 uint32_t frag_flag;
11642 11643 uint_t priority = mp->b_band;
11643 11644 int error = 0;
11644 11645
11645 11646 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11646 11647
11647 11648 if (pkt_len != msgdsize(mp)) {
11648 11649 ip0dbg(("Packet length mismatch: %d, %ld\n",
11649 11650 pkt_len, msgdsize(mp)));
11650 11651 freemsg(mp);
11651 11652 return (EINVAL);
11652 11653 }
11653 11654
11654 11655 if (max_frag == 0) {
11655 11656 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11656 11657 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11657 11658 ip_drop_output("FragFails: zero max_frag", mp, ill);
11658 11659 freemsg(mp);
11659 11660 return (EINVAL);
11660 11661 }
11661 11662
11662 11663 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11663 11664 ipha = (ipha_t *)mp->b_rptr;
11664 11665 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11665 11666 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11666 11667
11667 11668 /*
11668 11669 * Establish the starting offset. May not be zero if we are fragging
11669 11670 * a fragment that is being forwarded.
11670 11671 */
11671 11672 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11672 11673
11673 11674 /* TODO why is this test needed? */
11674 11675 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11675 11676 /* TODO: notify ulp somehow */
11676 11677 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11677 11678 ip_drop_output("FragFails: bad starting offset", mp, ill);
11678 11679 freemsg(mp);
11679 11680 return (EINVAL);
11680 11681 }
11681 11682
11682 11683 hdr_len = IPH_HDR_LENGTH(ipha);
11683 11684 ipha->ipha_hdr_checksum = 0;
11684 11685
11685 11686 /*
11686 11687 * Establish the number of bytes maximum per frag, after putting
11687 11688 * in the header.
11688 11689 */
11689 11690 len = (max_frag - hdr_len) & ~7;
11690 11691
11691 11692 /* Get a copy of the header for the trailing frags */
11692 11693 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11693 11694 mp);
11694 11695 if (hdr_mp == NULL) {
11695 11696 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11696 11697 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11697 11698 freemsg(mp);
11698 11699 return (ENOBUFS);
11699 11700 }
11700 11701
11701 11702 /* Store the starting offset, with the MoreFrags flag. */
11702 11703 i1 = offset | IPH_MF | frag_flag;
11703 11704 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11704 11705
11705 11706 /* Establish the ending byte offset, based on the starting offset. */
11706 11707 offset <<= 3;
11707 11708 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11708 11709
11709 11710 /* Store the length of the first fragment in the IP header. */
11710 11711 i1 = len + hdr_len;
11711 11712 ASSERT(i1 <= IP_MAXPACKET);
11712 11713 ipha->ipha_length = htons((uint16_t)i1);
11713 11714
11714 11715 /*
11715 11716 * Compute the IP header checksum for the first frag. We have to
11716 11717 * watch out that we stop at the end of the header.
11717 11718 */
11718 11719 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11719 11720
11720 11721 /*
11721 11722 * Now carve off the first frag. Note that this will include the
11722 11723 * original IP header.
11723 11724 */
11724 11725 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11725 11726 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11726 11727 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11727 11728 freeb(hdr_mp);
11728 11729 freemsg(mp_orig);
11729 11730 return (ENOBUFS);
11730 11731 }
11731 11732
11732 11733 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11733 11734
11734 11735 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11735 11736 ixa_cookie);
11736 11737 if (error != 0 && error != EWOULDBLOCK) {
11737 11738 /* No point in sending the other fragments */
11738 11739 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11739 11740 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11740 11741 freeb(hdr_mp);
11741 11742 freemsg(mp_orig);
11742 11743 return (error);
11743 11744 }
11744 11745
11745 11746 /* No need to redo state machine in loop */
11746 11747 ixaflags &= ~IXAF_REACH_CONF;
11747 11748
11748 11749 /* Advance the offset to the second frag starting point. */
11749 11750 offset += len;
11750 11751 /*
11751 11752 * Update hdr_len from the copied header - there might be less options
11752 11753 * in the later fragments.
11753 11754 */
11754 11755 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11755 11756 /* Loop until done. */
11756 11757 for (;;) {
11757 11758 uint16_t offset_and_flags;
11758 11759 uint16_t ip_len;
11759 11760
11760 11761 if (ip_data_end - offset > len) {
11761 11762 /*
11762 11763 * Carve off the appropriate amount from the original
11763 11764 * datagram.
11764 11765 */
11765 11766 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11766 11767 mp = NULL;
11767 11768 break;
11768 11769 }
11769 11770 /*
11770 11771 * More frags after this one. Get another copy
11771 11772 * of the header.
11772 11773 */
11773 11774 if (carve_mp->b_datap->db_ref == 1 &&
11774 11775 hdr_mp->b_wptr - hdr_mp->b_rptr <
11775 11776 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11776 11777 /* Inline IP header */
11777 11778 carve_mp->b_rptr -= hdr_mp->b_wptr -
11778 11779 hdr_mp->b_rptr;
11779 11780 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11780 11781 hdr_mp->b_wptr - hdr_mp->b_rptr);
11781 11782 mp = carve_mp;
11782 11783 } else {
11783 11784 if (!(mp = copyb(hdr_mp))) {
11784 11785 freemsg(carve_mp);
11785 11786 break;
11786 11787 }
11787 11788 /* Get priority marking, if any. */
11788 11789 mp->b_band = priority;
11789 11790 mp->b_cont = carve_mp;
11790 11791 }
11791 11792 ipha = (ipha_t *)mp->b_rptr;
11792 11793 offset_and_flags = IPH_MF;
11793 11794 } else {
11794 11795 /*
11795 11796 * Last frag. Consume the header. Set len to
11796 11797 * the length of this last piece.
11797 11798 */
11798 11799 len = ip_data_end - offset;
11799 11800
11800 11801 /*
11801 11802 * Carve off the appropriate amount from the original
11802 11803 * datagram.
11803 11804 */
11804 11805 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11805 11806 mp = NULL;
11806 11807 break;
11807 11808 }
11808 11809 if (carve_mp->b_datap->db_ref == 1 &&
11809 11810 hdr_mp->b_wptr - hdr_mp->b_rptr <
11810 11811 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11811 11812 /* Inline IP header */
11812 11813 carve_mp->b_rptr -= hdr_mp->b_wptr -
11813 11814 hdr_mp->b_rptr;
11814 11815 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11815 11816 hdr_mp->b_wptr - hdr_mp->b_rptr);
11816 11817 mp = carve_mp;
11817 11818 freeb(hdr_mp);
11818 11819 hdr_mp = mp;
11819 11820 } else {
11820 11821 mp = hdr_mp;
11821 11822 /* Get priority marking, if any. */
11822 11823 mp->b_band = priority;
11823 11824 mp->b_cont = carve_mp;
11824 11825 }
11825 11826 ipha = (ipha_t *)mp->b_rptr;
11826 11827 /* A frag of a frag might have IPH_MF non-zero */
11827 11828 offset_and_flags =
11828 11829 ntohs(ipha->ipha_fragment_offset_and_flags) &
11829 11830 IPH_MF;
11830 11831 }
11831 11832 offset_and_flags |= (uint16_t)(offset >> 3);
11832 11833 offset_and_flags |= (uint16_t)frag_flag;
11833 11834 /* Store the offset and flags in the IP header. */
11834 11835 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11835 11836
11836 11837 /* Store the length in the IP header. */
11837 11838 ip_len = (uint16_t)(len + hdr_len);
11838 11839 ipha->ipha_length = htons(ip_len);
11839 11840
11840 11841 /*
11841 11842 * Set the IP header checksum. Note that mp is just
11842 11843 * the header, so this is easy to pass to ip_csum.
11843 11844 */
11844 11845 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11845 11846
11846 11847 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11847 11848
11848 11849 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11849 11850 nolzid, ixa_cookie);
11850 11851 /* All done if we just consumed the hdr_mp. */
11851 11852 if (mp == hdr_mp) {
11852 11853 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11853 11854 return (error);
11854 11855 }
11855 11856 if (error != 0 && error != EWOULDBLOCK) {
11856 11857 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11857 11858 mblk_t *, hdr_mp);
11858 11859 /* No point in sending the other fragments */
11859 11860 break;
11860 11861 }
11861 11862
11862 11863 /* Otherwise, advance and loop. */
11863 11864 offset += len;
11864 11865 }
11865 11866 /* Clean up following allocation failure. */
11866 11867 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11867 11868 ip_drop_output("FragFails: loop ended", NULL, ill);
11868 11869 if (mp != hdr_mp)
11869 11870 freeb(hdr_mp);
11870 11871 if (mp != mp_orig)
11871 11872 freemsg(mp_orig);
11872 11873 return (error);
11873 11874 }
11874 11875
11875 11876 /*
11876 11877 * Copy the header plus those options which have the copy bit set
11877 11878 */
11878 11879 static mblk_t *
11879 11880 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11880 11881 mblk_t *src)
11881 11882 {
11882 11883 mblk_t *mp;
11883 11884 uchar_t *up;
11884 11885
11885 11886 /*
11886 11887 * Quick check if we need to look for options without the copy bit
11887 11888 * set
11888 11889 */
11889 11890 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11890 11891 if (!mp)
11891 11892 return (mp);
11892 11893 mp->b_rptr += ipst->ips_ip_wroff_extra;
11893 11894 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11894 11895 bcopy(rptr, mp->b_rptr, hdr_len);
11895 11896 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11896 11897 return (mp);
11897 11898 }
11898 11899 up = mp->b_rptr;
11899 11900 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11900 11901 up += IP_SIMPLE_HDR_LENGTH;
11901 11902 rptr += IP_SIMPLE_HDR_LENGTH;
11902 11903 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11903 11904 while (hdr_len > 0) {
11904 11905 uint32_t optval;
11905 11906 uint32_t optlen;
11906 11907
11907 11908 optval = *rptr;
11908 11909 if (optval == IPOPT_EOL)
11909 11910 break;
11910 11911 if (optval == IPOPT_NOP)
11911 11912 optlen = 1;
11912 11913 else
11913 11914 optlen = rptr[1];
11914 11915 if (optval & IPOPT_COPY) {
11915 11916 bcopy(rptr, up, optlen);
11916 11917 up += optlen;
11917 11918 }
11918 11919 rptr += optlen;
11919 11920 hdr_len -= optlen;
11920 11921 }
11921 11922 /*
11922 11923 * Make sure that we drop an even number of words by filling
11923 11924 * with EOL to the next word boundary.
11924 11925 */
11925 11926 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11926 11927 hdr_len & 0x3; hdr_len++)
11927 11928 *up++ = IPOPT_EOL;
11928 11929 mp->b_wptr = up;
11929 11930 /* Update header length */
11930 11931 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11931 11932 return (mp);
11932 11933 }
11933 11934
11934 11935 /*
11935 11936 * Update any source route, record route, or timestamp options when
11936 11937 * sending a packet back to ourselves.
11937 11938 * Check that we are at end of strict source route.
11938 11939 * The options have been sanity checked by ip_output_options().
11939 11940 */
11940 11941 void
11941 11942 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11942 11943 {
11943 11944 ipoptp_t opts;
11944 11945 uchar_t *opt;
11945 11946 uint8_t optval;
11946 11947 uint8_t optlen;
11947 11948 ipaddr_t dst;
11948 11949 uint32_t ts;
11949 11950 timestruc_t now;
11950 11951
11951 11952 for (optval = ipoptp_first(&opts, ipha);
11952 11953 optval != IPOPT_EOL;
11953 11954 optval = ipoptp_next(&opts)) {
11954 11955 opt = opts.ipoptp_cur;
11955 11956 optlen = opts.ipoptp_len;
11956 11957 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11957 11958 switch (optval) {
11958 11959 uint32_t off;
11959 11960 case IPOPT_SSRR:
11960 11961 case IPOPT_LSRR:
11961 11962 off = opt[IPOPT_OFFSET];
11962 11963 off--;
11963 11964 if (optlen < IP_ADDR_LEN ||
11964 11965 off > optlen - IP_ADDR_LEN) {
11965 11966 /* End of source route */
11966 11967 break;
11967 11968 }
11968 11969 /*
11969 11970 * This will only happen if two consecutive entries
11970 11971 * in the source route contains our address or if
11971 11972 * it is a packet with a loose source route which
11972 11973 * reaches us before consuming the whole source route
11973 11974 */
11974 11975
11975 11976 if (optval == IPOPT_SSRR) {
11976 11977 return;
11977 11978 }
11978 11979 /*
11979 11980 * Hack: instead of dropping the packet truncate the
11980 11981 * source route to what has been used by filling the
11981 11982 * rest with IPOPT_NOP.
11982 11983 */
11983 11984 opt[IPOPT_OLEN] = (uint8_t)off;
11984 11985 while (off < optlen) {
11985 11986 opt[off++] = IPOPT_NOP;
11986 11987 }
11987 11988 break;
11988 11989 case IPOPT_RR:
11989 11990 off = opt[IPOPT_OFFSET];
11990 11991 off--;
11991 11992 if (optlen < IP_ADDR_LEN ||
11992 11993 off > optlen - IP_ADDR_LEN) {
11993 11994 /* No more room - ignore */
11994 11995 ip1dbg((
11995 11996 "ip_output_local_options: end of RR\n"));
11996 11997 break;
11997 11998 }
11998 11999 dst = htonl(INADDR_LOOPBACK);
11999 12000 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12000 12001 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12001 12002 break;
12002 12003 case IPOPT_TS:
12003 12004 /* Insert timestamp if there is romm */
12004 12005 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12005 12006 case IPOPT_TS_TSONLY:
12006 12007 off = IPOPT_TS_TIMELEN;
12007 12008 break;
12008 12009 case IPOPT_TS_PRESPEC:
12009 12010 case IPOPT_TS_PRESPEC_RFC791:
12010 12011 /* Verify that the address matched */
12011 12012 off = opt[IPOPT_OFFSET] - 1;
12012 12013 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
12013 12014 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
12014 12015 /* Not for us */
12015 12016 break;
12016 12017 }
12017 12018 /* FALLTHROUGH */
12018 12019 case IPOPT_TS_TSANDADDR:
12019 12020 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
12020 12021 break;
12021 12022 default:
12022 12023 /*
12023 12024 * ip_*put_options should have already
12024 12025 * dropped this packet.
12025 12026 */
12026 12027 cmn_err(CE_PANIC, "ip_output_local_options: "
12027 12028 "unknown IT - bug in ip_output_options?\n");
12028 12029 return; /* Keep "lint" happy */
12029 12030 }
12030 12031 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
12031 12032 /* Increase overflow counter */
12032 12033 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
12033 12034 opt[IPOPT_POS_OV_FLG] = (uint8_t)
12034 12035 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
12035 12036 (off << 4);
12036 12037 break;
12037 12038 }
12038 12039 off = opt[IPOPT_OFFSET] - 1;
12039 12040 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12040 12041 case IPOPT_TS_PRESPEC:
12041 12042 case IPOPT_TS_PRESPEC_RFC791:
12042 12043 case IPOPT_TS_TSANDADDR:
12043 12044 dst = htonl(INADDR_LOOPBACK);
12044 12045 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12045 12046 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12046 12047 /* FALLTHROUGH */
12047 12048 case IPOPT_TS_TSONLY:
12048 12049 off = opt[IPOPT_OFFSET] - 1;
12049 12050 /* Compute # of milliseconds since midnight */
12050 12051 gethrestime(&now);
12051 12052 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
12052 12053 NSEC2MSEC(now.tv_nsec);
12053 12054 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
12054 12055 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
12055 12056 break;
12056 12057 }
12057 12058 break;
12058 12059 }
12059 12060 }
12060 12061 }
12061 12062
12062 12063 /*
12063 12064 * Prepend an M_DATA fastpath header, and if none present prepend a
12064 12065 * DL_UNITDATA_REQ. Frees the mblk on failure.
12065 12066 *
12066 12067 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12067 12068 * If there is a change to them, the nce will be deleted (condemned) and
12068 12069 * a new nce_t will be created when packets are sent. Thus we need no locks
12069 12070 * to access those fields.
12070 12071 *
12071 12072 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12072 12073 * we place b_band in dl_priority.dl_max.
12073 12074 */
12074 12075 static mblk_t *
12075 12076 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12076 12077 {
12077 12078 uint_t hlen;
12078 12079 mblk_t *mp1;
12079 12080 uint_t priority;
12080 12081 uchar_t *rptr;
12081 12082
12082 12083 rptr = mp->b_rptr;
12083 12084
12084 12085 ASSERT(DB_TYPE(mp) == M_DATA);
12085 12086 priority = mp->b_band;
12086 12087
12087 12088 ASSERT(nce != NULL);
12088 12089 if ((mp1 = nce->nce_fp_mp) != NULL) {
12089 12090 hlen = MBLKL(mp1);
12090 12091 /*
12091 12092 * Check if we have enough room to prepend fastpath
12092 12093 * header
12093 12094 */
12094 12095 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12095 12096 rptr -= hlen;
12096 12097 bcopy(mp1->b_rptr, rptr, hlen);
12097 12098 /*
12098 12099 * Set the b_rptr to the start of the link layer
12099 12100 * header
12100 12101 */
12101 12102 mp->b_rptr = rptr;
12102 12103 return (mp);
12103 12104 }
12104 12105 mp1 = copyb(mp1);
12105 12106 if (mp1 == NULL) {
12106 12107 ill_t *ill = nce->nce_ill;
12107 12108
12108 12109 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12109 12110 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12110 12111 freemsg(mp);
12111 12112 return (NULL);
12112 12113 }
12113 12114 mp1->b_band = priority;
12114 12115 mp1->b_cont = mp;
12115 12116 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12116 12117 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12117 12118 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12118 12119 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12119 12120 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12120 12121 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12121 12122 /*
12122 12123 * XXX disable ICK_VALID and compute checksum
12123 12124 * here; can happen if nce_fp_mp changes and
12124 12125 * it can't be copied now due to insufficient
12125 12126 * space. (unlikely, fp mp can change, but it
12126 12127 * does not increase in length)
12127 12128 */
12128 12129 return (mp1);
12129 12130 }
12130 12131 mp1 = copyb(nce->nce_dlur_mp);
12131 12132
12132 12133 if (mp1 == NULL) {
12133 12134 ill_t *ill = nce->nce_ill;
12134 12135
12135 12136 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12136 12137 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12137 12138 freemsg(mp);
12138 12139 return (NULL);
12139 12140 }
12140 12141 mp1->b_cont = mp;
12141 12142 if (priority != 0) {
12142 12143 mp1->b_band = priority;
12143 12144 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12144 12145 priority;
12145 12146 }
12146 12147 return (mp1);
12147 12148 }
12148 12149
12149 12150 /*
12150 12151 * Finish the outbound IPsec processing. This function is called from
12151 12152 * ipsec_out_process() if the IPsec packet was processed
12152 12153 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12153 12154 * asynchronously.
12154 12155 *
12155 12156 * This is common to IPv4 and IPv6.
12156 12157 */
12157 12158 int
12158 12159 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12159 12160 {
12160 12161 iaflags_t ixaflags = ixa->ixa_flags;
12161 12162 uint_t pktlen;
12162 12163
12163 12164
12164 12165 /* AH/ESP don't update ixa_pktlen when they modify the packet */
12165 12166 if (ixaflags & IXAF_IS_IPV4) {
12166 12167 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12167 12168
12168 12169 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12169 12170 pktlen = ntohs(ipha->ipha_length);
12170 12171 } else {
12171 12172 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12172 12173
12173 12174 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12174 12175 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12175 12176 }
12176 12177
12177 12178 /*
12178 12179 * We release any hard reference on the SAs here to make
12179 12180 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12180 12181 * on the SAs.
12181 12182 * If in the future we want the hard latching of the SAs in the
12182 12183 * ip_xmit_attr_t then we should remove this.
12183 12184 */
12184 12185 if (ixa->ixa_ipsec_esp_sa != NULL) {
12185 12186 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12186 12187 ixa->ixa_ipsec_esp_sa = NULL;
12187 12188 }
12188 12189 if (ixa->ixa_ipsec_ah_sa != NULL) {
12189 12190 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12190 12191 ixa->ixa_ipsec_ah_sa = NULL;
12191 12192 }
12192 12193
12193 12194 /* Do we need to fragment? */
12194 12195 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12195 12196 pktlen > ixa->ixa_fragsize) {
12196 12197 if (ixaflags & IXAF_IS_IPV4) {
12197 12198 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12198 12199 /*
12199 12200 * We check for the DF case in ipsec_out_process
12200 12201 * hence this only handles the non-DF case.
12201 12202 */
12202 12203 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12203 12204 pktlen, ixa->ixa_fragsize,
12204 12205 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12205 12206 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12206 12207 &ixa->ixa_cookie));
12207 12208 } else {
12208 12209 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12209 12210 if (mp == NULL) {
12210 12211 /* MIB and ip_drop_output already done */
12211 12212 return (ENOMEM);
12212 12213 }
12213 12214 pktlen += sizeof (ip6_frag_t);
12214 12215 if (pktlen > ixa->ixa_fragsize) {
12215 12216 return (ip_fragment_v6(mp, ixa->ixa_nce,
12216 12217 ixa->ixa_flags, pktlen,
12217 12218 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12218 12219 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12219 12220 ixa->ixa_postfragfn, &ixa->ixa_cookie));
12220 12221 }
12221 12222 }
12222 12223 }
12223 12224 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12224 12225 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12225 12226 ixa->ixa_no_loop_zoneid, NULL));
12226 12227 }
12227 12228
12228 12229 /*
12229 12230 * Finish the inbound IPsec processing. This function is called from
12230 12231 * ipsec_out_process() if the IPsec packet was processed
12231 12232 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12232 12233 * asynchronously.
12233 12234 *
12234 12235 * This is common to IPv4 and IPv6.
12235 12236 */
12236 12237 void
12237 12238 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12238 12239 {
12239 12240 iaflags_t iraflags = ira->ira_flags;
12240 12241
12241 12242 /* Length might have changed */
12242 12243 if (iraflags & IRAF_IS_IPV4) {
12243 12244 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12244 12245
12245 12246 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12246 12247 ira->ira_pktlen = ntohs(ipha->ipha_length);
12247 12248 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12248 12249 ira->ira_protocol = ipha->ipha_protocol;
12249 12250
12250 12251 ip_fanout_v4(mp, ipha, ira);
12251 12252 } else {
12252 12253 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12253 12254 uint8_t *nexthdrp;
12254 12255
12255 12256 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12256 12257 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12257 12258 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12258 12259 &nexthdrp)) {
12259 12260 /* Malformed packet */
12260 12261 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12261 12262 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12262 12263 freemsg(mp);
12263 12264 return;
12264 12265 }
12265 12266 ira->ira_protocol = *nexthdrp;
12266 12267 ip_fanout_v6(mp, ip6h, ira);
12267 12268 }
12268 12269 }
12269 12270
12270 12271 /*
12271 12272 * Select which AH & ESP SA's to use (if any) for the outbound packet.
12272 12273 *
12273 12274 * If this function returns B_TRUE, the requested SA's have been filled
12274 12275 * into the ixa_ipsec_*_sa pointers.
12275 12276 *
12276 12277 * If the function returns B_FALSE, the packet has been "consumed", most
12277 12278 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12278 12279 *
12279 12280 * The SA references created by the protocol-specific "select"
12280 12281 * function will be released in ip_output_post_ipsec.
12281 12282 */
12282 12283 static boolean_t
12283 12284 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12284 12285 {
12285 12286 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12286 12287 ipsec_policy_t *pp;
12287 12288 ipsec_action_t *ap;
12288 12289
12289 12290 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12290 12291 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12291 12292 (ixa->ixa_ipsec_action != NULL));
12292 12293
12293 12294 ap = ixa->ixa_ipsec_action;
12294 12295 if (ap == NULL) {
12295 12296 pp = ixa->ixa_ipsec_policy;
12296 12297 ASSERT(pp != NULL);
12297 12298 ap = pp->ipsp_act;
12298 12299 ASSERT(ap != NULL);
12299 12300 }
12300 12301
12301 12302 /*
12302 12303 * We have an action. now, let's select SA's.
12303 12304 * A side effect of setting ixa_ipsec_*_sa is that it will
12304 12305 * be cached in the conn_t.
12305 12306 */
12306 12307 if (ap->ipa_want_esp) {
12307 12308 if (ixa->ixa_ipsec_esp_sa == NULL) {
12308 12309 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12309 12310 IPPROTO_ESP);
12310 12311 }
12311 12312 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12312 12313 }
12313 12314
12314 12315 if (ap->ipa_want_ah) {
12315 12316 if (ixa->ixa_ipsec_ah_sa == NULL) {
12316 12317 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12317 12318 IPPROTO_AH);
12318 12319 }
12319 12320 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12320 12321 /*
12321 12322 * The ESP and AH processing order needs to be preserved
12322 12323 * when both protocols are required (ESP should be applied
12323 12324 * before AH for an outbound packet). Force an ESP ACQUIRE
12324 12325 * when both ESP and AH are required, and an AH ACQUIRE
12325 12326 * is needed.
12326 12327 */
12327 12328 if (ap->ipa_want_esp && need_ah_acquire)
12328 12329 need_esp_acquire = B_TRUE;
12329 12330 }
12330 12331
12331 12332 /*
12332 12333 * Send an ACQUIRE (extended, regular, or both) if we need one.
12333 12334 * Release SAs that got referenced, but will not be used until we
12334 12335 * acquire _all_ of the SAs we need.
12335 12336 */
12336 12337 if (need_ah_acquire || need_esp_acquire) {
12337 12338 if (ixa->ixa_ipsec_ah_sa != NULL) {
12338 12339 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12339 12340 ixa->ixa_ipsec_ah_sa = NULL;
12340 12341 }
12341 12342 if (ixa->ixa_ipsec_esp_sa != NULL) {
12342 12343 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12343 12344 ixa->ixa_ipsec_esp_sa = NULL;
12344 12345 }
12345 12346
12346 12347 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12347 12348 return (B_FALSE);
12348 12349 }
12349 12350
12350 12351 return (B_TRUE);
12351 12352 }
12352 12353
12353 12354 /*
12354 12355 * Handle IPsec output processing.
12355 12356 * This function is only entered once for a given packet.
12356 12357 * We try to do things synchronously, but if we need to have user-level
12357 12358 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12358 12359 * will be completed
12359 12360 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12360 12361 * - when asynchronous ESP is done it will do AH
12361 12362 *
12362 12363 * In all cases we come back in ip_output_post_ipsec() to fragment and
12363 12364 * send out the packet.
12364 12365 */
12365 12366 int
12366 12367 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12367 12368 {
12368 12369 ill_t *ill = ixa->ixa_nce->nce_ill;
12369 12370 ip_stack_t *ipst = ixa->ixa_ipst;
12370 12371 ipsec_stack_t *ipss;
12371 12372 ipsec_policy_t *pp;
12372 12373 ipsec_action_t *ap;
12373 12374
12374 12375 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12375 12376
12376 12377 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12377 12378 (ixa->ixa_ipsec_action != NULL));
12378 12379
12379 12380 ipss = ipst->ips_netstack->netstack_ipsec;
12380 12381 if (!ipsec_loaded(ipss)) {
12381 12382 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12382 12383 ip_drop_packet(mp, B_TRUE, ill,
12383 12384 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12384 12385 &ipss->ipsec_dropper);
12385 12386 return (ENOTSUP);
12386 12387 }
12387 12388
12388 12389 ap = ixa->ixa_ipsec_action;
12389 12390 if (ap == NULL) {
12390 12391 pp = ixa->ixa_ipsec_policy;
12391 12392 ASSERT(pp != NULL);
12392 12393 ap = pp->ipsp_act;
12393 12394 ASSERT(ap != NULL);
12394 12395 }
12395 12396
12396 12397 /* Handle explicit drop action and bypass. */
12397 12398 switch (ap->ipa_act.ipa_type) {
12398 12399 case IPSEC_ACT_DISCARD:
12399 12400 case IPSEC_ACT_REJECT:
12400 12401 ip_drop_packet(mp, B_FALSE, ill,
12401 12402 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12402 12403 return (EHOSTUNREACH); /* IPsec policy failure */
12403 12404 case IPSEC_ACT_BYPASS:
12404 12405 return (ip_output_post_ipsec(mp, ixa));
12405 12406 }
12406 12407
12407 12408 /*
12408 12409 * The order of processing is first insert a IP header if needed.
12409 12410 * Then insert the ESP header and then the AH header.
12410 12411 */
12411 12412 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12412 12413 /*
12413 12414 * First get the outer IP header before sending
12414 12415 * it to ESP.
12415 12416 */
12416 12417 ipha_t *oipha, *iipha;
12417 12418 mblk_t *outer_mp, *inner_mp;
12418 12419
12419 12420 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12420 12421 (void) mi_strlog(ill->ill_rq, 0,
12421 12422 SL_ERROR|SL_TRACE|SL_CONSOLE,
12422 12423 "ipsec_out_process: "
12423 12424 "Self-Encapsulation failed: Out of memory\n");
12424 12425 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12425 12426 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12426 12427 freemsg(mp);
12427 12428 return (ENOBUFS);
12428 12429 }
12429 12430 inner_mp = mp;
12430 12431 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12431 12432 oipha = (ipha_t *)outer_mp->b_rptr;
12432 12433 iipha = (ipha_t *)inner_mp->b_rptr;
12433 12434 *oipha = *iipha;
12434 12435 outer_mp->b_wptr += sizeof (ipha_t);
12435 12436 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12436 12437 sizeof (ipha_t));
12437 12438 oipha->ipha_protocol = IPPROTO_ENCAP;
12438 12439 oipha->ipha_version_and_hdr_length =
12439 12440 IP_SIMPLE_HDR_VERSION;
12440 12441 oipha->ipha_hdr_checksum = 0;
12441 12442 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12442 12443 outer_mp->b_cont = inner_mp;
12443 12444 mp = outer_mp;
12444 12445
12445 12446 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12446 12447 }
12447 12448
12448 12449 /* If we need to wait for a SA then we can't return any errno */
12449 12450 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12450 12451 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12451 12452 !ipsec_out_select_sa(mp, ixa))
12452 12453 return (0);
12453 12454
12454 12455 /*
12455 12456 * By now, we know what SA's to use. Toss over to ESP & AH
12456 12457 * to do the heavy lifting.
12457 12458 */
12458 12459 if (ap->ipa_want_esp) {
12459 12460 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12460 12461
12461 12462 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12462 12463 if (mp == NULL) {
12463 12464 /*
12464 12465 * Either it failed or is pending. In the former case
12465 12466 * ipIfStatsInDiscards was increased.
12466 12467 */
12467 12468 return (0);
12468 12469 }
12469 12470 }
12470 12471
12471 12472 if (ap->ipa_want_ah) {
12472 12473 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12473 12474
12474 12475 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12475 12476 if (mp == NULL) {
12476 12477 /*
12477 12478 * Either it failed or is pending. In the former case
12478 12479 * ipIfStatsInDiscards was increased.
12479 12480 */
12480 12481 return (0);
12481 12482 }
12482 12483 }
12483 12484 /*
12484 12485 * We are done with IPsec processing. Send it over
12485 12486 * the wire.
12486 12487 */
12487 12488 return (ip_output_post_ipsec(mp, ixa));
12488 12489 }
12489 12490
12490 12491 /*
12491 12492 * ioctls that go through a down/up sequence may need to wait for the down
12492 12493 * to complete. This involves waiting for the ire and ipif refcnts to go down
12493 12494 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12494 12495 */
12495 12496 /* ARGSUSED */
12496 12497 void
12497 12498 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12498 12499 {
12499 12500 struct iocblk *iocp;
12500 12501 mblk_t *mp1;
12501 12502 ip_ioctl_cmd_t *ipip;
12502 12503 int err;
12503 12504 sin_t *sin;
12504 12505 struct lifreq *lifr;
12505 12506 struct ifreq *ifr;
12506 12507
12507 12508 iocp = (struct iocblk *)mp->b_rptr;
12508 12509 ASSERT(ipsq != NULL);
12509 12510 /* Existence of mp1 verified in ip_wput_nondata */
12510 12511 mp1 = mp->b_cont->b_cont;
12511 12512 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12512 12513 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12513 12514 /*
12514 12515 * Special case where ipx_current_ipif is not set:
12515 12516 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12516 12517 * We are here as were not able to complete the operation in
12517 12518 * ipif_set_values because we could not become exclusive on
12518 12519 * the new ipsq.
12519 12520 */
12520 12521 ill_t *ill = q->q_ptr;
12521 12522 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12522 12523 }
12523 12524 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12524 12525
12525 12526 if (ipip->ipi_cmd_type == IF_CMD) {
12526 12527 /* This a old style SIOC[GS]IF* command */
12527 12528 ifr = (struct ifreq *)mp1->b_rptr;
12528 12529 sin = (sin_t *)&ifr->ifr_addr;
12529 12530 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12530 12531 /* This a new style SIOC[GS]LIF* command */
12531 12532 lifr = (struct lifreq *)mp1->b_rptr;
12532 12533 sin = (sin_t *)&lifr->lifr_addr;
12533 12534 } else {
12534 12535 sin = NULL;
12535 12536 }
12536 12537
12537 12538 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12538 12539 q, mp, ipip, mp1->b_rptr);
12539 12540
12540 12541 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12541 12542 int, ipip->ipi_cmd,
12542 12543 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12543 12544 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12544 12545
12545 12546 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12546 12547 }
12547 12548
12548 12549 /*
12549 12550 * ioctl processing
12550 12551 *
12551 12552 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12552 12553 * the ioctl command in the ioctl tables, determines the copyin data size
12553 12554 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12554 12555 *
12555 12556 * ioctl processing then continues when the M_IOCDATA makes its way down to
12556 12557 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12557 12558 * associated 'conn' is refheld till the end of the ioctl and the general
12558 12559 * ioctl processing function ip_process_ioctl() is called to extract the
12559 12560 * arguments and process the ioctl. To simplify extraction, ioctl commands
12560 12561 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12561 12562 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12562 12563 * is used to extract the ioctl's arguments.
12563 12564 *
12564 12565 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12565 12566 * so goes thru the serialization primitive ipsq_try_enter. Then the
12566 12567 * appropriate function to handle the ioctl is called based on the entry in
12567 12568 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12568 12569 * which also refreleases the 'conn' that was refheld at the start of the
12569 12570 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12570 12571 *
12571 12572 * Many exclusive ioctls go thru an internal down up sequence as part of
12572 12573 * the operation. For example an attempt to change the IP address of an
12573 12574 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12574 12575 * does all the cleanup such as deleting all ires that use this address.
12575 12576 * Then we need to wait till all references to the interface go away.
12576 12577 */
12577 12578 void
12578 12579 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12579 12580 {
12580 12581 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12581 12582 ip_ioctl_cmd_t *ipip = arg;
12582 12583 ip_extract_func_t *extract_funcp;
12583 12584 cmd_info_t ci;
12584 12585 int err;
12585 12586 boolean_t entered_ipsq = B_FALSE;
12586 12587
12587 12588 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12588 12589
12589 12590 if (ipip == NULL)
12590 12591 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12591 12592
12592 12593 /*
12593 12594 * SIOCLIFADDIF needs to go thru a special path since the
12594 12595 * ill may not exist yet. This happens in the case of lo0
12595 12596 * which is created using this ioctl.
12596 12597 */
12597 12598 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12598 12599 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12599 12600 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12600 12601 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12601 12602 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12602 12603 return;
12603 12604 }
12604 12605
12605 12606 ci.ci_ipif = NULL;
12606 12607 switch (ipip->ipi_cmd_type) {
12607 12608 case MISC_CMD:
12608 12609 case MSFILT_CMD:
12609 12610 /*
12610 12611 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12611 12612 */
12612 12613 if (ipip->ipi_cmd == IF_UNITSEL) {
12613 12614 /* ioctl comes down the ill */
12614 12615 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12615 12616 ipif_refhold(ci.ci_ipif);
12616 12617 }
12617 12618 err = 0;
12618 12619 ci.ci_sin = NULL;
12619 12620 ci.ci_sin6 = NULL;
12620 12621 ci.ci_lifr = NULL;
12621 12622 extract_funcp = NULL;
12622 12623 break;
12623 12624
12624 12625 case IF_CMD:
12625 12626 case LIF_CMD:
12626 12627 extract_funcp = ip_extract_lifreq;
12627 12628 break;
12628 12629
12629 12630 case ARP_CMD:
12630 12631 case XARP_CMD:
12631 12632 extract_funcp = ip_extract_arpreq;
12632 12633 break;
12633 12634
12634 12635 default:
12635 12636 ASSERT(0);
12636 12637 }
12637 12638
12638 12639 if (extract_funcp != NULL) {
12639 12640 err = (*extract_funcp)(q, mp, ipip, &ci);
12640 12641 if (err != 0) {
12641 12642 DTRACE_PROBE4(ipif__ioctl,
12642 12643 char *, "ip_process_ioctl finish err",
12643 12644 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12644 12645 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12645 12646 return;
12646 12647 }
12647 12648
12648 12649 /*
12649 12650 * All of the extraction functions return a refheld ipif.
12650 12651 */
12651 12652 ASSERT(ci.ci_ipif != NULL);
12652 12653 }
12653 12654
12654 12655 if (!(ipip->ipi_flags & IPI_WR)) {
12655 12656 /*
12656 12657 * A return value of EINPROGRESS means the ioctl is
12657 12658 * either queued and waiting for some reason or has
12658 12659 * already completed.
12659 12660 */
12660 12661 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12661 12662 ci.ci_lifr);
12662 12663 if (ci.ci_ipif != NULL) {
12663 12664 DTRACE_PROBE4(ipif__ioctl,
12664 12665 char *, "ip_process_ioctl finish RD",
12665 12666 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12666 12667 ipif_t *, ci.ci_ipif);
12667 12668 ipif_refrele(ci.ci_ipif);
12668 12669 } else {
12669 12670 DTRACE_PROBE4(ipif__ioctl,
12670 12671 char *, "ip_process_ioctl finish RD",
12671 12672 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12672 12673 }
12673 12674 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12674 12675 return;
12675 12676 }
12676 12677
12677 12678 ASSERT(ci.ci_ipif != NULL);
12678 12679
12679 12680 /*
12680 12681 * If ipsq is non-NULL, we are already being called exclusively
12681 12682 */
12682 12683 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12683 12684 if (ipsq == NULL) {
12684 12685 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12685 12686 NEW_OP, B_TRUE);
12686 12687 if (ipsq == NULL) {
12687 12688 ipif_refrele(ci.ci_ipif);
12688 12689 return;
12689 12690 }
12690 12691 entered_ipsq = B_TRUE;
12691 12692 }
12692 12693 /*
12693 12694 * Release the ipif so that ipif_down and friends that wait for
12694 12695 * references to go away are not misled about the current ipif_refcnt
12695 12696 * values. We are writer so we can access the ipif even after releasing
12696 12697 * the ipif.
12697 12698 */
12698 12699 ipif_refrele(ci.ci_ipif);
12699 12700
12700 12701 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12701 12702
12702 12703 /*
12703 12704 * A return value of EINPROGRESS means the ioctl is
12704 12705 * either queued and waiting for some reason or has
12705 12706 * already completed.
12706 12707 */
12707 12708 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12708 12709
12709 12710 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12710 12711 int, ipip->ipi_cmd,
12711 12712 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12712 12713 ipif_t *, ci.ci_ipif);
12713 12714 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12714 12715
12715 12716 if (entered_ipsq)
12716 12717 ipsq_exit(ipsq);
12717 12718 }
12718 12719
12719 12720 /*
12720 12721 * Complete the ioctl. Typically ioctls use the mi package and need to
12721 12722 * do mi_copyout/mi_copy_done.
12722 12723 */
12723 12724 void
12724 12725 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12725 12726 {
12726 12727 conn_t *connp = NULL;
12727 12728
12728 12729 if (err == EINPROGRESS)
12729 12730 return;
12730 12731
12731 12732 if (CONN_Q(q)) {
12732 12733 connp = Q_TO_CONN(q);
12733 12734 ASSERT(connp->conn_ref >= 2);
12734 12735 }
12735 12736
12736 12737 switch (mode) {
12737 12738 case COPYOUT:
12738 12739 if (err == 0)
12739 12740 mi_copyout(q, mp);
12740 12741 else
12741 12742 mi_copy_done(q, mp, err);
12742 12743 break;
12743 12744
12744 12745 case NO_COPYOUT:
12745 12746 mi_copy_done(q, mp, err);
12746 12747 break;
12747 12748
12748 12749 default:
12749 12750 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12750 12751 break;
12751 12752 }
12752 12753
12753 12754 /*
12754 12755 * The conn refhold and ioctlref placed on the conn at the start of the
12755 12756 * ioctl are released here.
12756 12757 */
12757 12758 if (connp != NULL) {
12758 12759 CONN_DEC_IOCTLREF(connp);
12759 12760 CONN_OPER_PENDING_DONE(connp);
12760 12761 }
12761 12762
12762 12763 if (ipsq != NULL)
12763 12764 ipsq_current_finish(ipsq);
12764 12765 }
12765 12766
12766 12767 /* Handles all non data messages */
12767 12768 int
12768 12769 ip_wput_nondata(queue_t *q, mblk_t *mp)
12769 12770 {
12770 12771 mblk_t *mp1;
12771 12772 struct iocblk *iocp;
12772 12773 ip_ioctl_cmd_t *ipip;
12773 12774 conn_t *connp;
12774 12775 cred_t *cr;
12775 12776 char *proto_str;
12776 12777
12777 12778 if (CONN_Q(q))
12778 12779 connp = Q_TO_CONN(q);
12779 12780 else
12780 12781 connp = NULL;
12781 12782
12782 12783 switch (DB_TYPE(mp)) {
12783 12784 case M_IOCTL:
12784 12785 /*
12785 12786 * IOCTL processing begins in ip_sioctl_copyin_setup which
12786 12787 * will arrange to copy in associated control structures.
12787 12788 */
12788 12789 ip_sioctl_copyin_setup(q, mp);
12789 12790 return (0);
12790 12791 case M_IOCDATA:
12791 12792 /*
12792 12793 * Ensure that this is associated with one of our trans-
12793 12794 * parent ioctls. If it's not ours, discard it if we're
12794 12795 * running as a driver, or pass it on if we're a module.
12795 12796 */
12796 12797 iocp = (struct iocblk *)mp->b_rptr;
12797 12798 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12798 12799 if (ipip == NULL) {
12799 12800 if (q->q_next == NULL) {
12800 12801 goto nak;
12801 12802 } else {
12802 12803 putnext(q, mp);
12803 12804 }
12804 12805 return (0);
12805 12806 }
12806 12807 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12807 12808 /*
12808 12809 * The ioctl is one we recognise, but is not consumed
12809 12810 * by IP as a module and we are a module, so we drop
12810 12811 */
12811 12812 goto nak;
12812 12813 }
12813 12814
12814 12815 /* IOCTL continuation following copyin or copyout. */
12815 12816 if (mi_copy_state(q, mp, NULL) == -1) {
12816 12817 /*
12817 12818 * The copy operation failed. mi_copy_state already
12818 12819 * cleaned up, so we're out of here.
12819 12820 */
12820 12821 return (0);
12821 12822 }
12822 12823 /*
12823 12824 * If we just completed a copy in, we become writer and
12824 12825 * continue processing in ip_sioctl_copyin_done. If it
12825 12826 * was a copy out, we call mi_copyout again. If there is
12826 12827 * nothing more to copy out, it will complete the IOCTL.
12827 12828 */
12828 12829 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12829 12830 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12830 12831 mi_copy_done(q, mp, EPROTO);
12831 12832 return (0);
12832 12833 }
12833 12834 /*
12834 12835 * Check for cases that need more copying. A return
12835 12836 * value of 0 means a second copyin has been started,
12836 12837 * so we return; a return value of 1 means no more
12837 12838 * copying is needed, so we continue.
12838 12839 */
12839 12840 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12840 12841 MI_COPY_COUNT(mp) == 1) {
12841 12842 if (ip_copyin_msfilter(q, mp) == 0)
12842 12843 return (0);
12843 12844 }
12844 12845 /*
12845 12846 * Refhold the conn, till the ioctl completes. This is
12846 12847 * needed in case the ioctl ends up in the pending mp
12847 12848 * list. Every mp in the ipx_pending_mp list must have
12848 12849 * a refhold on the conn to resume processing. The
12849 12850 * refhold is released when the ioctl completes
12850 12851 * (whether normally or abnormally). An ioctlref is also
12851 12852 * placed on the conn to prevent TCP from removing the
12852 12853 * queue needed to send the ioctl reply back.
12853 12854 * In all cases ip_ioctl_finish is called to finish
12854 12855 * the ioctl and release the refholds.
12855 12856 */
12856 12857 if (connp != NULL) {
12857 12858 /* This is not a reentry */
12858 12859 CONN_INC_REF(connp);
12859 12860 CONN_INC_IOCTLREF(connp);
12860 12861 } else {
12861 12862 if (!(ipip->ipi_flags & IPI_MODOK)) {
12862 12863 mi_copy_done(q, mp, EINVAL);
12863 12864 return (0);
12864 12865 }
12865 12866 }
12866 12867
12867 12868 ip_process_ioctl(NULL, q, mp, ipip);
12868 12869
12869 12870 } else {
12870 12871 mi_copyout(q, mp);
12871 12872 }
12872 12873 return (0);
12873 12874
12874 12875 case M_IOCNAK:
12875 12876 /*
12876 12877 * The only way we could get here is if a resolver didn't like
12877 12878 * an IOCTL we sent it. This shouldn't happen.
12878 12879 */
12879 12880 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12880 12881 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12881 12882 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12882 12883 freemsg(mp);
12883 12884 return (0);
12884 12885 case M_IOCACK:
12885 12886 /* /dev/ip shouldn't see this */
12886 12887 goto nak;
12887 12888 case M_FLUSH:
12888 12889 if (*mp->b_rptr & FLUSHW)
12889 12890 flushq(q, FLUSHALL);
12890 12891 if (q->q_next) {
12891 12892 putnext(q, mp);
12892 12893 return (0);
12893 12894 }
12894 12895 if (*mp->b_rptr & FLUSHR) {
12895 12896 *mp->b_rptr &= ~FLUSHW;
12896 12897 qreply(q, mp);
12897 12898 return (0);
12898 12899 }
12899 12900 freemsg(mp);
12900 12901 return (0);
12901 12902 case M_CTL:
12902 12903 break;
12903 12904 case M_PROTO:
12904 12905 case M_PCPROTO:
12905 12906 /*
12906 12907 * The only PROTO messages we expect are SNMP-related.
12907 12908 */
12908 12909 switch (((union T_primitives *)mp->b_rptr)->type) {
12909 12910 case T_SVR4_OPTMGMT_REQ:
12910 12911 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12911 12912 "flags %x\n",
12912 12913 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12913 12914
12914 12915 if (connp == NULL) {
12915 12916 proto_str = "T_SVR4_OPTMGMT_REQ";
12916 12917 goto protonak;
12917 12918 }
12918 12919
12919 12920 /*
12920 12921 * All Solaris components should pass a db_credp
12921 12922 * for this TPI message, hence we ASSERT.
12922 12923 * But in case there is some other M_PROTO that looks
12923 12924 * like a TPI message sent by some other kernel
12924 12925 * component, we check and return an error.
12925 12926 */
12926 12927 cr = msg_getcred(mp, NULL);
12927 12928 ASSERT(cr != NULL);
12928 12929 if (cr == NULL) {
12929 12930 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12930 12931 if (mp != NULL)
12931 12932 qreply(q, mp);
12932 12933 return (0);
12933 12934 }
12934 12935
12935 12936 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12936 12937 proto_str = "Bad SNMPCOM request?";
12937 12938 goto protonak;
12938 12939 }
12939 12940 return (0);
12940 12941 default:
12941 12942 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12942 12943 (int)*(uint_t *)mp->b_rptr));
12943 12944 freemsg(mp);
12944 12945 return (0);
12945 12946 }
12946 12947 default:
12947 12948 break;
12948 12949 }
12949 12950 if (q->q_next) {
12950 12951 putnext(q, mp);
12951 12952 } else
12952 12953 freemsg(mp);
12953 12954 return (0);
12954 12955
12955 12956 nak:
12956 12957 iocp->ioc_error = EINVAL;
12957 12958 mp->b_datap->db_type = M_IOCNAK;
12958 12959 iocp->ioc_count = 0;
12959 12960 qreply(q, mp);
12960 12961 return (0);
12961 12962
12962 12963 protonak:
12963 12964 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12964 12965 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12965 12966 qreply(q, mp);
12966 12967 return (0);
12967 12968 }
12968 12969
12969 12970 /*
12970 12971 * Process IP options in an outbound packet. Verify that the nexthop in a
12971 12972 * strict source route is onlink.
12972 12973 * Returns non-zero if something fails in which case an ICMP error has been
12973 12974 * sent and mp freed.
12974 12975 *
12975 12976 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12976 12977 */
12977 12978 int
12978 12979 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12979 12980 {
12980 12981 ipoptp_t opts;
12981 12982 uchar_t *opt;
12982 12983 uint8_t optval;
12983 12984 uint8_t optlen;
12984 12985 ipaddr_t dst;
12985 12986 intptr_t code = 0;
12986 12987 ire_t *ire;
12987 12988 ip_stack_t *ipst = ixa->ixa_ipst;
12988 12989 ip_recv_attr_t iras;
12989 12990
12990 12991 ip2dbg(("ip_output_options\n"));
12991 12992
12992 12993 dst = ipha->ipha_dst;
12993 12994 for (optval = ipoptp_first(&opts, ipha);
12994 12995 optval != IPOPT_EOL;
12995 12996 optval = ipoptp_next(&opts)) {
12996 12997 opt = opts.ipoptp_cur;
12997 12998 optlen = opts.ipoptp_len;
12998 12999 ip2dbg(("ip_output_options: opt %d, len %d\n",
12999 13000 optval, optlen));
13000 13001 switch (optval) {
13001 13002 uint32_t off;
13002 13003 case IPOPT_SSRR:
13003 13004 case IPOPT_LSRR:
13004 13005 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13005 13006 ip1dbg((
13006 13007 "ip_output_options: bad option offset\n"));
13007 13008 code = (char *)&opt[IPOPT_OLEN] -
13008 13009 (char *)ipha;
13009 13010 goto param_prob;
13010 13011 }
13011 13012 off = opt[IPOPT_OFFSET];
13012 13013 ip1dbg(("ip_output_options: next hop 0x%x\n",
13013 13014 ntohl(dst)));
13014 13015 /*
13015 13016 * For strict: verify that dst is directly
13016 13017 * reachable.
13017 13018 */
13018 13019 if (optval == IPOPT_SSRR) {
13019 13020 ire = ire_ftable_lookup_v4(dst, 0, 0,
13020 13021 IRE_INTERFACE, NULL, ALL_ZONES,
13021 13022 ixa->ixa_tsl,
13022 13023 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
13023 13024 NULL);
13024 13025 if (ire == NULL) {
13025 13026 ip1dbg(("ip_output_options: SSRR not"
13026 13027 " directly reachable: 0x%x\n",
13027 13028 ntohl(dst)));
13028 13029 goto bad_src_route;
13029 13030 }
13030 13031 ire_refrele(ire);
13031 13032 }
13032 13033 break;
13033 13034 case IPOPT_RR:
13034 13035 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13035 13036 ip1dbg((
13036 13037 "ip_output_options: bad option offset\n"));
13037 13038 code = (char *)&opt[IPOPT_OLEN] -
13038 13039 (char *)ipha;
13039 13040 goto param_prob;
13040 13041 }
13041 13042 break;
13042 13043 case IPOPT_TS:
13043 13044 /*
13044 13045 * Verify that length >=5 and that there is either
13045 13046 * room for another timestamp or that the overflow
13046 13047 * counter is not maxed out.
13047 13048 */
13048 13049 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
13049 13050 if (optlen < IPOPT_MINLEN_IT) {
13050 13051 goto param_prob;
13051 13052 }
13052 13053 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13053 13054 ip1dbg((
13054 13055 "ip_output_options: bad option offset\n"));
13055 13056 code = (char *)&opt[IPOPT_OFFSET] -
13056 13057 (char *)ipha;
13057 13058 goto param_prob;
13058 13059 }
13059 13060 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13060 13061 case IPOPT_TS_TSONLY:
13061 13062 off = IPOPT_TS_TIMELEN;
13062 13063 break;
13063 13064 case IPOPT_TS_TSANDADDR:
13064 13065 case IPOPT_TS_PRESPEC:
13065 13066 case IPOPT_TS_PRESPEC_RFC791:
13066 13067 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13067 13068 break;
13068 13069 default:
13069 13070 code = (char *)&opt[IPOPT_POS_OV_FLG] -
13070 13071 (char *)ipha;
13071 13072 goto param_prob;
13072 13073 }
13073 13074 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13074 13075 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13075 13076 /*
13076 13077 * No room and the overflow counter is 15
13077 13078 * already.
13078 13079 */
13079 13080 goto param_prob;
13080 13081 }
13081 13082 break;
13082 13083 }
13083 13084 }
13084 13085
13085 13086 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13086 13087 return (0);
13087 13088
13088 13089 ip1dbg(("ip_output_options: error processing IP options."));
13089 13090 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13090 13091
13091 13092 param_prob:
13092 13093 bzero(&iras, sizeof (iras));
13093 13094 iras.ira_ill = iras.ira_rill = ill;
13094 13095 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13095 13096 iras.ira_rifindex = iras.ira_ruifindex;
13096 13097 iras.ira_flags = IRAF_IS_IPV4;
13097 13098
13098 13099 ip_drop_output("ip_output_options", mp, ill);
13099 13100 icmp_param_problem(mp, (uint8_t)code, &iras);
13100 13101 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13101 13102 return (-1);
13102 13103
13103 13104 bad_src_route:
13104 13105 bzero(&iras, sizeof (iras));
13105 13106 iras.ira_ill = iras.ira_rill = ill;
13106 13107 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13107 13108 iras.ira_rifindex = iras.ira_ruifindex;
13108 13109 iras.ira_flags = IRAF_IS_IPV4;
13109 13110
13110 13111 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13111 13112 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13112 13113 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13113 13114 return (-1);
13114 13115 }
13115 13116
13116 13117 /*
13117 13118 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13118 13119 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13119 13120 * thru /etc/system.
13120 13121 */
13121 13122 #define CONN_MAXDRAINCNT 64
13122 13123
13123 13124 static void
13124 13125 conn_drain_init(ip_stack_t *ipst)
13125 13126 {
13126 13127 int i, j;
13127 13128 idl_tx_list_t *itl_tx;
13128 13129
13129 13130 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13130 13131
13131 13132 if ((ipst->ips_conn_drain_list_cnt == 0) ||
13132 13133 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13133 13134 /*
13134 13135 * Default value of the number of drainers is the
13135 13136 * number of cpus, subject to maximum of 8 drainers.
13136 13137 */
13137 13138 if (boot_max_ncpus != -1)
13138 13139 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13139 13140 else
13140 13141 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13141 13142 }
13142 13143
13143 13144 ipst->ips_idl_tx_list =
13144 13145 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13145 13146 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13146 13147 itl_tx = &ipst->ips_idl_tx_list[i];
13147 13148 itl_tx->txl_drain_list =
13148 13149 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13149 13150 sizeof (idl_t), KM_SLEEP);
13150 13151 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13151 13152 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13152 13153 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13153 13154 MUTEX_DEFAULT, NULL);
13154 13155 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13155 13156 }
13156 13157 }
13157 13158 }
13158 13159
13159 13160 static void
13160 13161 conn_drain_fini(ip_stack_t *ipst)
13161 13162 {
13162 13163 int i;
13163 13164 idl_tx_list_t *itl_tx;
13164 13165
13165 13166 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13166 13167 itl_tx = &ipst->ips_idl_tx_list[i];
13167 13168 kmem_free(itl_tx->txl_drain_list,
13168 13169 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13169 13170 }
13170 13171 kmem_free(ipst->ips_idl_tx_list,
13171 13172 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13172 13173 ipst->ips_idl_tx_list = NULL;
13173 13174 }
13174 13175
13175 13176 /*
13176 13177 * Flow control has blocked us from proceeding. Insert the given conn in one
13177 13178 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
13178 13179 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13179 13180 * will call conn_walk_drain(). See the flow control notes at the top of this
13180 13181 * file for more details.
13181 13182 */
13182 13183 void
13183 13184 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13184 13185 {
13185 13186 idl_t *idl = tx_list->txl_drain_list;
13186 13187 uint_t index;
13187 13188 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
13188 13189
13189 13190 mutex_enter(&connp->conn_lock);
13190 13191 if (connp->conn_state_flags & CONN_CLOSING) {
13191 13192 /*
13192 13193 * The conn is closing as a result of which CONN_CLOSING
13193 13194 * is set. Return.
13194 13195 */
13195 13196 mutex_exit(&connp->conn_lock);
13196 13197 return;
13197 13198 } else if (connp->conn_idl == NULL) {
13198 13199 /*
13199 13200 * Assign the next drain list round robin. We dont' use
13200 13201 * a lock, and thus it may not be strictly round robin.
13201 13202 * Atomicity of load/stores is enough to make sure that
13202 13203 * conn_drain_list_index is always within bounds.
13203 13204 */
13204 13205 index = tx_list->txl_drain_index;
13205 13206 ASSERT(index < ipst->ips_conn_drain_list_cnt);
13206 13207 connp->conn_idl = &tx_list->txl_drain_list[index];
13207 13208 index++;
13208 13209 if (index == ipst->ips_conn_drain_list_cnt)
13209 13210 index = 0;
13210 13211 tx_list->txl_drain_index = index;
13211 13212 } else {
13212 13213 ASSERT(connp->conn_idl->idl_itl == tx_list);
13213 13214 }
13214 13215 mutex_exit(&connp->conn_lock);
13215 13216
13216 13217 idl = connp->conn_idl;
13217 13218 mutex_enter(&idl->idl_lock);
13218 13219 if ((connp->conn_drain_prev != NULL) ||
13219 13220 (connp->conn_state_flags & CONN_CLOSING)) {
13220 13221 /*
13221 13222 * The conn is either already in the drain list or closing.
13222 13223 * (We needed to check for CONN_CLOSING again since close can
13223 13224 * sneak in between dropping conn_lock and acquiring idl_lock.)
13224 13225 */
13225 13226 mutex_exit(&idl->idl_lock);
13226 13227 return;
13227 13228 }
13228 13229
13229 13230 /*
13230 13231 * The conn is not in the drain list. Insert it at the
13231 13232 * tail of the drain list. The drain list is circular
13232 13233 * and doubly linked. idl_conn points to the 1st element
13233 13234 * in the list.
13234 13235 */
13235 13236 if (idl->idl_conn == NULL) {
13236 13237 idl->idl_conn = connp;
13237 13238 connp->conn_drain_next = connp;
13238 13239 connp->conn_drain_prev = connp;
13239 13240 } else {
13240 13241 conn_t *head = idl->idl_conn;
13241 13242
13242 13243 connp->conn_drain_next = head;
13243 13244 connp->conn_drain_prev = head->conn_drain_prev;
13244 13245 head->conn_drain_prev->conn_drain_next = connp;
13245 13246 head->conn_drain_prev = connp;
13246 13247 }
13247 13248 /*
13248 13249 * For non streams based sockets assert flow control.
13249 13250 */
13250 13251 conn_setqfull(connp, NULL);
13251 13252 mutex_exit(&idl->idl_lock);
13252 13253 }
13253 13254
13254 13255 static void
13255 13256 conn_drain_remove(conn_t *connp)
13256 13257 {
13257 13258 idl_t *idl = connp->conn_idl;
13258 13259
13259 13260 if (idl != NULL) {
13260 13261 /*
13261 13262 * Remove ourself from the drain list.
13262 13263 */
13263 13264 if (connp->conn_drain_next == connp) {
13264 13265 /* Singleton in the list */
13265 13266 ASSERT(connp->conn_drain_prev == connp);
13266 13267 idl->idl_conn = NULL;
13267 13268 } else {
13268 13269 connp->conn_drain_prev->conn_drain_next =
13269 13270 connp->conn_drain_next;
13270 13271 connp->conn_drain_next->conn_drain_prev =
13271 13272 connp->conn_drain_prev;
13272 13273 if (idl->idl_conn == connp)
13273 13274 idl->idl_conn = connp->conn_drain_next;
13274 13275 }
13275 13276
13276 13277 /*
13277 13278 * NOTE: because conn_idl is associated with a specific drain
13278 13279 * list which in turn is tied to the index the TX ring
13279 13280 * (txl_cookie) hashes to, and because the TX ring can change
13280 13281 * over the lifetime of the conn_t, we must clear conn_idl so
13281 13282 * a subsequent conn_drain_insert() will set conn_idl again
13282 13283 * based on the latest txl_cookie.
13283 13284 */
13284 13285 connp->conn_idl = NULL;
13285 13286 }
13286 13287 connp->conn_drain_next = NULL;
13287 13288 connp->conn_drain_prev = NULL;
13288 13289
13289 13290 conn_clrqfull(connp, NULL);
13290 13291 /*
13291 13292 * For streams based sockets open up flow control.
13292 13293 */
13293 13294 if (!IPCL_IS_NONSTR(connp))
13294 13295 enableok(connp->conn_wq);
13295 13296 }
13296 13297
13297 13298 /*
13298 13299 * This conn is closing, and we are called from ip_close. OR
13299 13300 * this conn is draining because flow-control on the ill has been relieved.
13300 13301 *
13301 13302 * We must also need to remove conn's on this idl from the list, and also
13302 13303 * inform the sockfs upcalls about the change in flow-control.
13303 13304 */
13304 13305 static void
13305 13306 conn_drain(conn_t *connp, boolean_t closing)
13306 13307 {
13307 13308 idl_t *idl;
13308 13309 conn_t *next_connp;
13309 13310
13310 13311 /*
13311 13312 * connp->conn_idl is stable at this point, and no lock is needed
13312 13313 * to check it. If we are called from ip_close, close has already
13313 13314 * set CONN_CLOSING, thus freezing the value of conn_idl, and
13314 13315 * called us only because conn_idl is non-null. If we are called thru
13315 13316 * service, conn_idl could be null, but it cannot change because
13316 13317 * service is single-threaded per queue, and there cannot be another
13317 13318 * instance of service trying to call conn_drain_insert on this conn
13318 13319 * now.
13319 13320 */
13320 13321 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13321 13322
13322 13323 /*
13323 13324 * If the conn doesn't exist or is not on a drain list, bail.
13324 13325 */
13325 13326 if (connp == NULL || connp->conn_idl == NULL ||
13326 13327 connp->conn_drain_prev == NULL) {
13327 13328 return;
13328 13329 }
13329 13330
13330 13331 idl = connp->conn_idl;
13331 13332 ASSERT(MUTEX_HELD(&idl->idl_lock));
13332 13333
13333 13334 if (!closing) {
13334 13335 next_connp = connp->conn_drain_next;
13335 13336 while (next_connp != connp) {
13336 13337 conn_t *delconnp = next_connp;
13337 13338
13338 13339 next_connp = next_connp->conn_drain_next;
13339 13340 conn_drain_remove(delconnp);
13340 13341 }
13341 13342 ASSERT(connp->conn_drain_next == idl->idl_conn);
13342 13343 }
13343 13344 conn_drain_remove(connp);
13344 13345 }
13345 13346
13346 13347 /*
13347 13348 * Write service routine. Shared perimeter entry point.
13348 13349 * The device queue's messages has fallen below the low water mark and STREAMS
13349 13350 * has backenabled the ill_wq. Send sockfs notification about flow-control on
13350 13351 * each waiting conn.
13351 13352 */
13352 13353 int
13353 13354 ip_wsrv(queue_t *q)
13354 13355 {
13355 13356 ill_t *ill;
13356 13357
13357 13358 ill = (ill_t *)q->q_ptr;
13358 13359 if (ill->ill_state_flags == 0) {
13359 13360 ip_stack_t *ipst = ill->ill_ipst;
13360 13361
13361 13362 /*
13362 13363 * The device flow control has opened up.
13363 13364 * Walk through conn drain lists and qenable the
13364 13365 * first conn in each list. This makes sense only
13365 13366 * if the stream is fully plumbed and setup.
13366 13367 * Hence the ill_state_flags check above.
13367 13368 */
13368 13369 ip1dbg(("ip_wsrv: walking\n"));
13369 13370 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13370 13371 enableok(ill->ill_wq);
13371 13372 }
13372 13373 return (0);
13373 13374 }
13374 13375
13375 13376 /*
13376 13377 * Callback to disable flow control in IP.
13377 13378 *
13378 13379 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13379 13380 * is enabled.
13380 13381 *
13381 13382 * When MAC_TX() is not able to send any more packets, dld sets its queue
13382 13383 * to QFULL and enable the STREAMS flow control. Later, when the underlying
13383 13384 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13384 13385 * function and wakes up corresponding mac worker threads, which in turn
13385 13386 * calls this callback function, and disables flow control.
13386 13387 */
13387 13388 void
13388 13389 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13389 13390 {
13390 13391 ill_t *ill = (ill_t *)arg;
13391 13392 ip_stack_t *ipst = ill->ill_ipst;
13392 13393 idl_tx_list_t *idl_txl;
13393 13394
13394 13395 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13395 13396 mutex_enter(&idl_txl->txl_lock);
13396 13397 /* add code to to set a flag to indicate idl_txl is enabled */
13397 13398 conn_walk_drain(ipst, idl_txl);
13398 13399 mutex_exit(&idl_txl->txl_lock);
13399 13400 }
13400 13401
13401 13402 /*
13402 13403 * Flow control has been relieved and STREAMS has backenabled us; drain
13403 13404 * all the conn lists on `tx_list'.
13404 13405 */
13405 13406 static void
13406 13407 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13407 13408 {
13408 13409 int i;
13409 13410 idl_t *idl;
13410 13411
13411 13412 IP_STAT(ipst, ip_conn_walk_drain);
13412 13413
13413 13414 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13414 13415 idl = &tx_list->txl_drain_list[i];
13415 13416 mutex_enter(&idl->idl_lock);
13416 13417 conn_drain(idl->idl_conn, B_FALSE);
13417 13418 mutex_exit(&idl->idl_lock);
13418 13419 }
13419 13420 }
13420 13421
13421 13422 /*
13422 13423 * Determine if the ill and multicast aspects of that packets
13423 13424 * "matches" the conn.
13424 13425 */
13425 13426 boolean_t
13426 13427 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13427 13428 {
13428 13429 ill_t *ill = ira->ira_rill;
13429 13430 zoneid_t zoneid = ira->ira_zoneid;
13430 13431 uint_t in_ifindex;
13431 13432 ipaddr_t dst, src;
13432 13433
13433 13434 dst = ipha->ipha_dst;
13434 13435 src = ipha->ipha_src;
13435 13436
13436 13437 /*
13437 13438 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13438 13439 * unicast, broadcast and multicast reception to
13439 13440 * conn_incoming_ifindex.
13440 13441 * conn_wantpacket is called for unicast, broadcast and
13441 13442 * multicast packets.
13442 13443 */
13443 13444 in_ifindex = connp->conn_incoming_ifindex;
13444 13445
13445 13446 /* mpathd can bind to the under IPMP interface, which we allow */
13446 13447 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13447 13448 if (!IS_UNDER_IPMP(ill))
13448 13449 return (B_FALSE);
13449 13450
13450 13451 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13451 13452 return (B_FALSE);
13452 13453 }
13453 13454
13454 13455 if (!IPCL_ZONE_MATCH(connp, zoneid))
13455 13456 return (B_FALSE);
13456 13457
13457 13458 if (!(ira->ira_flags & IRAF_MULTICAST))
13458 13459 return (B_TRUE);
13459 13460
13460 13461 if (connp->conn_multi_router) {
13461 13462 /* multicast packet and multicast router socket: send up */
13462 13463 return (B_TRUE);
13463 13464 }
13464 13465
13465 13466 if (ipha->ipha_protocol == IPPROTO_PIM ||
13466 13467 ipha->ipha_protocol == IPPROTO_RSVP)
13467 13468 return (B_TRUE);
13468 13469
13469 13470 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13470 13471 }
13471 13472
13472 13473 void
13473 13474 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13474 13475 {
13475 13476 if (IPCL_IS_NONSTR(connp)) {
13476 13477 (*connp->conn_upcalls->su_txq_full)
13477 13478 (connp->conn_upper_handle, B_TRUE);
13478 13479 if (flow_stopped != NULL)
13479 13480 *flow_stopped = B_TRUE;
13480 13481 } else {
13481 13482 queue_t *q = connp->conn_wq;
13482 13483
13483 13484 ASSERT(q != NULL);
13484 13485 if (!(q->q_flag & QFULL)) {
13485 13486 mutex_enter(QLOCK(q));
13486 13487 if (!(q->q_flag & QFULL)) {
13487 13488 /* still need to set QFULL */
13488 13489 q->q_flag |= QFULL;
13489 13490 /* set flow_stopped to true under QLOCK */
13490 13491 if (flow_stopped != NULL)
13491 13492 *flow_stopped = B_TRUE;
13492 13493 mutex_exit(QLOCK(q));
13493 13494 } else {
13494 13495 /* flow_stopped is left unchanged */
13495 13496 mutex_exit(QLOCK(q));
13496 13497 }
13497 13498 }
13498 13499 }
13499 13500 }
13500 13501
13501 13502 void
13502 13503 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13503 13504 {
13504 13505 if (IPCL_IS_NONSTR(connp)) {
13505 13506 (*connp->conn_upcalls->su_txq_full)
13506 13507 (connp->conn_upper_handle, B_FALSE);
13507 13508 if (flow_stopped != NULL)
13508 13509 *flow_stopped = B_FALSE;
13509 13510 } else {
13510 13511 queue_t *q = connp->conn_wq;
13511 13512
13512 13513 ASSERT(q != NULL);
13513 13514 if (q->q_flag & QFULL) {
13514 13515 mutex_enter(QLOCK(q));
13515 13516 if (q->q_flag & QFULL) {
13516 13517 q->q_flag &= ~QFULL;
13517 13518 /* set flow_stopped to false under QLOCK */
13518 13519 if (flow_stopped != NULL)
13519 13520 *flow_stopped = B_FALSE;
13520 13521 mutex_exit(QLOCK(q));
13521 13522 if (q->q_flag & QWANTW)
13522 13523 qbackenable(q, 0);
13523 13524 } else {
13524 13525 /* flow_stopped is left unchanged */
13525 13526 mutex_exit(QLOCK(q));
13526 13527 }
13527 13528 }
13528 13529 }
13529 13530
13530 13531 mutex_enter(&connp->conn_lock);
13531 13532 connp->conn_blocked = B_FALSE;
13532 13533 mutex_exit(&connp->conn_lock);
13533 13534 }
13534 13535
13535 13536 /*
13536 13537 * Return the length in bytes of the IPv4 headers (base header, label, and
13537 13538 * other IP options) that will be needed based on the
13538 13539 * ip_pkt_t structure passed by the caller.
13539 13540 *
13540 13541 * The returned length does not include the length of the upper level
13541 13542 * protocol (ULP) header.
13542 13543 * The caller needs to check that the length doesn't exceed the max for IPv4.
13543 13544 */
13544 13545 int
13545 13546 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13546 13547 {
13547 13548 int len;
13548 13549
13549 13550 len = IP_SIMPLE_HDR_LENGTH;
13550 13551 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13551 13552 ASSERT(ipp->ipp_label_len_v4 != 0);
13552 13553 /* We need to round up here */
13553 13554 len += (ipp->ipp_label_len_v4 + 3) & ~3;
13554 13555 }
13555 13556
13556 13557 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13557 13558 ASSERT(ipp->ipp_ipv4_options_len != 0);
13558 13559 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13559 13560 len += ipp->ipp_ipv4_options_len;
13560 13561 }
13561 13562 return (len);
13562 13563 }
13563 13564
13564 13565 /*
13565 13566 * All-purpose routine to build an IPv4 header with options based
13566 13567 * on the abstract ip_pkt_t.
13567 13568 *
13568 13569 * The caller has to set the source and destination address as well as
13569 13570 * ipha_length. The caller has to massage any source route and compensate
13570 13571 * for the ULP pseudo-header checksum due to the source route.
13571 13572 */
13572 13573 void
13573 13574 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13574 13575 uint8_t protocol)
13575 13576 {
13576 13577 ipha_t *ipha = (ipha_t *)buf;
13577 13578 uint8_t *cp;
13578 13579
13579 13580 /* Initialize IPv4 header */
13580 13581 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13581 13582 ipha->ipha_length = 0; /* Caller will set later */
13582 13583 ipha->ipha_ident = 0;
13583 13584 ipha->ipha_fragment_offset_and_flags = 0;
13584 13585 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13585 13586 ipha->ipha_protocol = protocol;
13586 13587 ipha->ipha_hdr_checksum = 0;
13587 13588
13588 13589 if ((ipp->ipp_fields & IPPF_ADDR) &&
13589 13590 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13590 13591 ipha->ipha_src = ipp->ipp_addr_v4;
13591 13592
13592 13593 cp = (uint8_t *)&ipha[1];
13593 13594 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13594 13595 ASSERT(ipp->ipp_label_len_v4 != 0);
13595 13596 bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13596 13597 cp += ipp->ipp_label_len_v4;
13597 13598 /* We need to round up here */
13598 13599 while ((uintptr_t)cp & 0x3) {
13599 13600 *cp++ = IPOPT_NOP;
13600 13601 }
13601 13602 }
13602 13603
13603 13604 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13604 13605 ASSERT(ipp->ipp_ipv4_options_len != 0);
13605 13606 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13606 13607 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13607 13608 cp += ipp->ipp_ipv4_options_len;
13608 13609 }
13609 13610 ipha->ipha_version_and_hdr_length =
13610 13611 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13611 13612
13612 13613 ASSERT((int)(cp - buf) == buf_len);
13613 13614 }
13614 13615
13615 13616 /* Allocate the private structure */
13616 13617 static int
13617 13618 ip_priv_alloc(void **bufp)
13618 13619 {
13619 13620 void *buf;
13620 13621
13621 13622 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13622 13623 return (ENOMEM);
13623 13624
13624 13625 *bufp = buf;
13625 13626 return (0);
13626 13627 }
13627 13628
13628 13629 /* Function to delete the private structure */
13629 13630 void
13630 13631 ip_priv_free(void *buf)
13631 13632 {
13632 13633 ASSERT(buf != NULL);
13633 13634 kmem_free(buf, sizeof (ip_priv_t));
13634 13635 }
13635 13636
13636 13637 /*
13637 13638 * The entry point for IPPF processing.
13638 13639 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13639 13640 * routine just returns.
13640 13641 *
13641 13642 * When called, ip_process generates an ipp_packet_t structure
13642 13643 * which holds the state information for this packet and invokes the
13643 13644 * the classifier (via ipp_packet_process). The classification, depending on
13644 13645 * configured filters, results in a list of actions for this packet. Invoking
13645 13646 * an action may cause the packet to be dropped, in which case we return NULL.
13646 13647 * proc indicates the callout position for
13647 13648 * this packet and ill is the interface this packet arrived on or will leave
13648 13649 * on (inbound and outbound resp.).
13649 13650 *
13650 13651 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13651 13652 * on the ill corrsponding to the destination IP address.
13652 13653 */
13653 13654 mblk_t *
13654 13655 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13655 13656 {
13656 13657 ip_priv_t *priv;
13657 13658 ipp_action_id_t aid;
13658 13659 int rc = 0;
13659 13660 ipp_packet_t *pp;
13660 13661
13661 13662 /* If the classifier is not loaded, return */
13662 13663 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13663 13664 return (mp);
13664 13665 }
13665 13666
13666 13667 ASSERT(mp != NULL);
13667 13668
13668 13669 /* Allocate the packet structure */
13669 13670 rc = ipp_packet_alloc(&pp, "ip", aid);
13670 13671 if (rc != 0)
13671 13672 goto drop;
13672 13673
13673 13674 /* Allocate the private structure */
13674 13675 rc = ip_priv_alloc((void **)&priv);
13675 13676 if (rc != 0) {
13676 13677 ipp_packet_free(pp);
13677 13678 goto drop;
13678 13679 }
13679 13680 priv->proc = proc;
13680 13681 priv->ill_index = ill_get_upper_ifindex(rill);
13681 13682
13682 13683 ipp_packet_set_private(pp, priv, ip_priv_free);
13683 13684 ipp_packet_set_data(pp, mp);
13684 13685
13685 13686 /* Invoke the classifier */
13686 13687 rc = ipp_packet_process(&pp);
13687 13688 if (pp != NULL) {
13688 13689 mp = ipp_packet_get_data(pp);
13689 13690 ipp_packet_free(pp);
13690 13691 if (rc != 0)
13691 13692 goto drop;
13692 13693 return (mp);
13693 13694 } else {
13694 13695 /* No mp to trace in ip_drop_input/ip_drop_output */
13695 13696 mp = NULL;
13696 13697 }
13697 13698 drop:
13698 13699 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13699 13700 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13700 13701 ip_drop_input("ip_process", mp, ill);
13701 13702 } else {
13702 13703 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13703 13704 ip_drop_output("ip_process", mp, ill);
13704 13705 }
13705 13706 freemsg(mp);
13706 13707 return (NULL);
13707 13708 }
13708 13709
13709 13710 /*
13710 13711 * Propagate a multicast group membership operation (add/drop) on
13711 13712 * all the interfaces crossed by the related multirt routes.
13712 13713 * The call is considered successful if the operation succeeds
13713 13714 * on at least one interface.
13714 13715 *
13715 13716 * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13716 13717 * multicast addresses with the ire argument being the first one.
13717 13718 * We walk the bucket to find all the of those.
13718 13719 *
13719 13720 * Common to IPv4 and IPv6.
13720 13721 */
13721 13722 static int
13722 13723 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13723 13724 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13724 13725 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13725 13726 mcast_record_t fmode, const in6_addr_t *v6src)
13726 13727 {
13727 13728 ire_t *ire_gw;
13728 13729 irb_t *irb;
13729 13730 int ifindex;
13730 13731 int error = 0;
13731 13732 int result;
13732 13733 ip_stack_t *ipst = ire->ire_ipst;
13733 13734 ipaddr_t group;
13734 13735 boolean_t isv6;
13735 13736 int match_flags;
13736 13737
13737 13738 if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13738 13739 IN6_V4MAPPED_TO_IPADDR(v6group, group);
13739 13740 isv6 = B_FALSE;
13740 13741 } else {
13741 13742 isv6 = B_TRUE;
13742 13743 }
13743 13744
13744 13745 irb = ire->ire_bucket;
13745 13746 ASSERT(irb != NULL);
13746 13747
13747 13748 result = 0;
13748 13749 irb_refhold(irb);
13749 13750 for (; ire != NULL; ire = ire->ire_next) {
13750 13751 if ((ire->ire_flags & RTF_MULTIRT) == 0)
13751 13752 continue;
13752 13753
13753 13754 /* We handle -ifp routes by matching on the ill if set */
13754 13755 match_flags = MATCH_IRE_TYPE;
13755 13756 if (ire->ire_ill != NULL)
13756 13757 match_flags |= MATCH_IRE_ILL;
13757 13758
13758 13759 if (isv6) {
13759 13760 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13760 13761 continue;
13761 13762
13762 13763 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13763 13764 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13764 13765 match_flags, 0, ipst, NULL);
13765 13766 } else {
13766 13767 if (ire->ire_addr != group)
13767 13768 continue;
13768 13769
13769 13770 ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13770 13771 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13771 13772 match_flags, 0, ipst, NULL);
13772 13773 }
13773 13774 /* No interface route exists for the gateway; skip this ire. */
13774 13775 if (ire_gw == NULL)
13775 13776 continue;
13776 13777 if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13777 13778 ire_refrele(ire_gw);
13778 13779 continue;
13779 13780 }
13780 13781 ASSERT(ire_gw->ire_ill != NULL); /* IRE_INTERFACE */
13781 13782 ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13782 13783
13783 13784 /*
13784 13785 * The operation is considered a success if
13785 13786 * it succeeds at least once on any one interface.
13786 13787 */
13787 13788 error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13788 13789 fmode, v6src);
13789 13790 if (error == 0)
13790 13791 result = CGTP_MCAST_SUCCESS;
13791 13792
13792 13793 ire_refrele(ire_gw);
13793 13794 }
13794 13795 irb_refrele(irb);
13795 13796 /*
13796 13797 * Consider the call as successful if we succeeded on at least
13797 13798 * one interface. Otherwise, return the last encountered error.
13798 13799 */
13799 13800 return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13800 13801 }
13801 13802
13802 13803 /*
13803 13804 * Return the expected CGTP hooks version number.
13804 13805 */
13805 13806 int
13806 13807 ip_cgtp_filter_supported(void)
13807 13808 {
13808 13809 return (ip_cgtp_filter_rev);
13809 13810 }
13810 13811
13811 13812 /*
13812 13813 * CGTP hooks can be registered by invoking this function.
13813 13814 * Checks that the version number matches.
13814 13815 */
13815 13816 int
13816 13817 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13817 13818 {
13818 13819 netstack_t *ns;
13819 13820 ip_stack_t *ipst;
13820 13821
13821 13822 if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13822 13823 return (ENOTSUP);
13823 13824
13824 13825 ns = netstack_find_by_stackid(stackid);
13825 13826 if (ns == NULL)
13826 13827 return (EINVAL);
13827 13828 ipst = ns->netstack_ip;
13828 13829 ASSERT(ipst != NULL);
13829 13830
13830 13831 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13831 13832 netstack_rele(ns);
13832 13833 return (EALREADY);
13833 13834 }
13834 13835
13835 13836 ipst->ips_ip_cgtp_filter_ops = ops;
13836 13837
13837 13838 ill_set_inputfn_all(ipst);
13838 13839
13839 13840 netstack_rele(ns);
13840 13841 return (0);
13841 13842 }
13842 13843
13843 13844 /*
13844 13845 * CGTP hooks can be unregistered by invoking this function.
13845 13846 * Returns ENXIO if there was no registration.
13846 13847 * Returns EBUSY if the ndd variable has not been turned off.
13847 13848 */
13848 13849 int
13849 13850 ip_cgtp_filter_unregister(netstackid_t stackid)
13850 13851 {
13851 13852 netstack_t *ns;
13852 13853 ip_stack_t *ipst;
13853 13854
13854 13855 ns = netstack_find_by_stackid(stackid);
13855 13856 if (ns == NULL)
13856 13857 return (EINVAL);
13857 13858 ipst = ns->netstack_ip;
13858 13859 ASSERT(ipst != NULL);
13859 13860
13860 13861 if (ipst->ips_ip_cgtp_filter) {
13861 13862 netstack_rele(ns);
13862 13863 return (EBUSY);
13863 13864 }
13864 13865
13865 13866 if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13866 13867 netstack_rele(ns);
13867 13868 return (ENXIO);
13868 13869 }
13869 13870 ipst->ips_ip_cgtp_filter_ops = NULL;
13870 13871
13871 13872 ill_set_inputfn_all(ipst);
13872 13873
13873 13874 netstack_rele(ns);
13874 13875 return (0);
13875 13876 }
13876 13877
13877 13878 /*
13878 13879 * Check whether there is a CGTP filter registration.
13879 13880 * Returns non-zero if there is a registration, otherwise returns zero.
13880 13881 * Note: returns zero if bad stackid.
13881 13882 */
13882 13883 int
13883 13884 ip_cgtp_filter_is_registered(netstackid_t stackid)
13884 13885 {
13885 13886 netstack_t *ns;
13886 13887 ip_stack_t *ipst;
13887 13888 int ret;
13888 13889
13889 13890 ns = netstack_find_by_stackid(stackid);
13890 13891 if (ns == NULL)
13891 13892 return (0);
13892 13893 ipst = ns->netstack_ip;
13893 13894 ASSERT(ipst != NULL);
13894 13895
13895 13896 if (ipst->ips_ip_cgtp_filter_ops != NULL)
13896 13897 ret = 1;
13897 13898 else
13898 13899 ret = 0;
13899 13900
13900 13901 netstack_rele(ns);
13901 13902 return (ret);
13902 13903 }
13903 13904
13904 13905 static int
13905 13906 ip_squeue_switch(int val)
13906 13907 {
13907 13908 int rval;
13908 13909
13909 13910 switch (val) {
13910 13911 case IP_SQUEUE_ENTER_NODRAIN:
13911 13912 rval = SQ_NODRAIN;
13912 13913 break;
13913 13914 case IP_SQUEUE_ENTER:
13914 13915 rval = SQ_PROCESS;
13915 13916 break;
13916 13917 case IP_SQUEUE_FILL:
13917 13918 default:
13918 13919 rval = SQ_FILL;
13919 13920 break;
13920 13921 }
13921 13922 return (rval);
13922 13923 }
13923 13924
13924 13925 static void *
13925 13926 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13926 13927 {
13927 13928 kstat_t *ksp;
13928 13929
13929 13930 ip_stat_t template = {
13930 13931 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13931 13932 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13932 13933 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13933 13934 { "ip_db_ref", KSTAT_DATA_UINT64 },
13934 13935 { "ip_notaligned", KSTAT_DATA_UINT64 },
13935 13936 { "ip_multimblk", KSTAT_DATA_UINT64 },
13936 13937 { "ip_opt", KSTAT_DATA_UINT64 },
13937 13938 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13938 13939 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13939 13940 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13940 13941 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13941 13942 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13942 13943 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13943 13944 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13944 13945 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13945 13946 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13946 13947 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13947 13948 { "ip_nce_mcast_reclaim_calls", KSTAT_DATA_UINT64 },
13948 13949 { "ip_nce_mcast_reclaim_deleted", KSTAT_DATA_UINT64 },
13949 13950 { "ip_nce_mcast_reclaim_tqfail", KSTAT_DATA_UINT64 },
13950 13951 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13951 13952 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13952 13953 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13953 13954 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13954 13955 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13955 13956 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13956 13957 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13957 13958 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13958 13959 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
13959 13960 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
13960 13961 { "conn_in_recvif", KSTAT_DATA_UINT64 },
13961 13962 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
13962 13963 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
13963 13964 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
13964 13965 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
13965 13966 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
13966 13967 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
13967 13968 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
13968 13969 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
13969 13970 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
13970 13971 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
13971 13972 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
13972 13973 };
13973 13974
13974 13975 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13975 13976 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13976 13977 KSTAT_FLAG_VIRTUAL, stackid);
13977 13978
13978 13979 if (ksp == NULL)
13979 13980 return (NULL);
13980 13981
13981 13982 bcopy(&template, ip_statisticsp, sizeof (template));
13982 13983 ksp->ks_data = (void *)ip_statisticsp;
13983 13984 ksp->ks_private = (void *)(uintptr_t)stackid;
13984 13985
13985 13986 kstat_install(ksp);
13986 13987 return (ksp);
13987 13988 }
13988 13989
13989 13990 static void
13990 13991 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13991 13992 {
13992 13993 if (ksp != NULL) {
13993 13994 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13994 13995 kstat_delete_netstack(ksp, stackid);
13995 13996 }
13996 13997 }
13997 13998
13998 13999 static void *
13999 14000 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
14000 14001 {
14001 14002 kstat_t *ksp;
14002 14003
14003 14004 ip_named_kstat_t template = {
14004 14005 { "forwarding", KSTAT_DATA_UINT32, 0 },
14005 14006 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
14006 14007 { "inReceives", KSTAT_DATA_UINT64, 0 },
14007 14008 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
14008 14009 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
14009 14010 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
14010 14011 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
14011 14012 { "inDiscards", KSTAT_DATA_UINT32, 0 },
14012 14013 { "inDelivers", KSTAT_DATA_UINT64, 0 },
14013 14014 { "outRequests", KSTAT_DATA_UINT64, 0 },
14014 14015 { "outDiscards", KSTAT_DATA_UINT32, 0 },
14015 14016 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
14016 14017 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
14017 14018 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
14018 14019 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
14019 14020 { "reasmFails", KSTAT_DATA_UINT32, 0 },
14020 14021 { "fragOKs", KSTAT_DATA_UINT32, 0 },
14021 14022 { "fragFails", KSTAT_DATA_UINT32, 0 },
14022 14023 { "fragCreates", KSTAT_DATA_UINT32, 0 },
14023 14024 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
14024 14025 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
14025 14026 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
14026 14027 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
14027 14028 { "inErrs", KSTAT_DATA_UINT32, 0 },
14028 14029 { "noPorts", KSTAT_DATA_UINT32, 0 },
14029 14030 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
14030 14031 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
14031 14032 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
14032 14033 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
14033 14034 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
14034 14035 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
14035 14036 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
14036 14037 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
14037 14038 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
14038 14039 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
14039 14040 { "inIPv6", KSTAT_DATA_UINT32, 0 },
14040 14041 { "outIPv6", KSTAT_DATA_UINT32, 0 },
14041 14042 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
14042 14043 };
14043 14044
14044 14045 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
14045 14046 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
14046 14047 if (ksp == NULL || ksp->ks_data == NULL)
14047 14048 return (NULL);
14048 14049
14049 14050 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
14050 14051 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
14051 14052 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14052 14053 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14053 14054 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14054 14055
14055 14056 template.netToMediaEntrySize.value.i32 =
14056 14057 sizeof (mib2_ipNetToMediaEntry_t);
14057 14058
14058 14059 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14059 14060
14060 14061 bcopy(&template, ksp->ks_data, sizeof (template));
14061 14062 ksp->ks_update = ip_kstat_update;
14062 14063 ksp->ks_private = (void *)(uintptr_t)stackid;
14063 14064
14064 14065 kstat_install(ksp);
14065 14066 return (ksp);
14066 14067 }
14067 14068
14068 14069 static void
14069 14070 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14070 14071 {
14071 14072 if (ksp != NULL) {
14072 14073 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14073 14074 kstat_delete_netstack(ksp, stackid);
14074 14075 }
14075 14076 }
14076 14077
14077 14078 static int
14078 14079 ip_kstat_update(kstat_t *kp, int rw)
14079 14080 {
14080 14081 ip_named_kstat_t *ipkp;
14081 14082 mib2_ipIfStatsEntry_t ipmib;
14082 14083 ill_walk_context_t ctx;
14083 14084 ill_t *ill;
14084 14085 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14085 14086 netstack_t *ns;
14086 14087 ip_stack_t *ipst;
14087 14088
14088 14089 if (kp->ks_data == NULL)
14089 14090 return (EIO);
14090 14091
14091 14092 if (rw == KSTAT_WRITE)
14092 14093 return (EACCES);
14093 14094
14094 14095 ns = netstack_find_by_stackid(stackid);
14095 14096 if (ns == NULL)
14096 14097 return (-1);
14097 14098 ipst = ns->netstack_ip;
14098 14099 if (ipst == NULL) {
14099 14100 netstack_rele(ns);
14100 14101 return (-1);
14101 14102 }
14102 14103 ipkp = (ip_named_kstat_t *)kp->ks_data;
14103 14104
14104 14105 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14105 14106 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14106 14107 ill = ILL_START_WALK_V4(&ctx, ipst);
14107 14108 for (; ill != NULL; ill = ill_next(&ctx, ill))
14108 14109 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14109 14110 rw_exit(&ipst->ips_ill_g_lock);
14110 14111
14111 14112 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
14112 14113 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
14113 14114 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
14114 14115 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
14115 14116 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
14116 14117 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14117 14118 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
14118 14119 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
14119 14120 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
14120 14121 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
14121 14122 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
14122 14123 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
14123 14124 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14124 14125 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
14125 14126 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
14126 14127 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
14127 14128 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
14128 14129 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
14129 14130 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
14130 14131
14131 14132 ipkp->routingDiscards.value.ui32 = 0;
14132 14133 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
14133 14134 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
14134 14135 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
14135 14136 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
14136 14137 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
14137 14138 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
14138 14139 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
14139 14140 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
14140 14141 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
14141 14142 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
14142 14143 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
14143 14144
14144 14145 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
14145 14146 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
14146 14147 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14147 14148
14148 14149 netstack_rele(ns);
14149 14150
14150 14151 return (0);
14151 14152 }
14152 14153
14153 14154 static void *
14154 14155 icmp_kstat_init(netstackid_t stackid)
14155 14156 {
14156 14157 kstat_t *ksp;
14157 14158
14158 14159 icmp_named_kstat_t template = {
14159 14160 { "inMsgs", KSTAT_DATA_UINT32 },
14160 14161 { "inErrors", KSTAT_DATA_UINT32 },
14161 14162 { "inDestUnreachs", KSTAT_DATA_UINT32 },
14162 14163 { "inTimeExcds", KSTAT_DATA_UINT32 },
14163 14164 { "inParmProbs", KSTAT_DATA_UINT32 },
14164 14165 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
14165 14166 { "inRedirects", KSTAT_DATA_UINT32 },
14166 14167 { "inEchos", KSTAT_DATA_UINT32 },
14167 14168 { "inEchoReps", KSTAT_DATA_UINT32 },
14168 14169 { "inTimestamps", KSTAT_DATA_UINT32 },
14169 14170 { "inTimestampReps", KSTAT_DATA_UINT32 },
14170 14171 { "inAddrMasks", KSTAT_DATA_UINT32 },
14171 14172 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
14172 14173 { "outMsgs", KSTAT_DATA_UINT32 },
14173 14174 { "outErrors", KSTAT_DATA_UINT32 },
14174 14175 { "outDestUnreachs", KSTAT_DATA_UINT32 },
14175 14176 { "outTimeExcds", KSTAT_DATA_UINT32 },
14176 14177 { "outParmProbs", KSTAT_DATA_UINT32 },
14177 14178 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
14178 14179 { "outRedirects", KSTAT_DATA_UINT32 },
14179 14180 { "outEchos", KSTAT_DATA_UINT32 },
14180 14181 { "outEchoReps", KSTAT_DATA_UINT32 },
14181 14182 { "outTimestamps", KSTAT_DATA_UINT32 },
14182 14183 { "outTimestampReps", KSTAT_DATA_UINT32 },
14183 14184 { "outAddrMasks", KSTAT_DATA_UINT32 },
14184 14185 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
14185 14186 { "inChksumErrs", KSTAT_DATA_UINT32 },
14186 14187 { "inUnknowns", KSTAT_DATA_UINT32 },
14187 14188 { "inFragNeeded", KSTAT_DATA_UINT32 },
14188 14189 { "outFragNeeded", KSTAT_DATA_UINT32 },
14189 14190 { "outDrops", KSTAT_DATA_UINT32 },
14190 14191 { "inOverFlows", KSTAT_DATA_UINT32 },
14191 14192 { "inBadRedirects", KSTAT_DATA_UINT32 },
14192 14193 };
14193 14194
14194 14195 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14195 14196 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14196 14197 if (ksp == NULL || ksp->ks_data == NULL)
14197 14198 return (NULL);
14198 14199
14199 14200 bcopy(&template, ksp->ks_data, sizeof (template));
14200 14201
14201 14202 ksp->ks_update = icmp_kstat_update;
14202 14203 ksp->ks_private = (void *)(uintptr_t)stackid;
14203 14204
14204 14205 kstat_install(ksp);
14205 14206 return (ksp);
14206 14207 }
14207 14208
14208 14209 static void
14209 14210 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14210 14211 {
14211 14212 if (ksp != NULL) {
14212 14213 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14213 14214 kstat_delete_netstack(ksp, stackid);
14214 14215 }
14215 14216 }
14216 14217
14217 14218 static int
14218 14219 icmp_kstat_update(kstat_t *kp, int rw)
14219 14220 {
14220 14221 icmp_named_kstat_t *icmpkp;
14221 14222 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14222 14223 netstack_t *ns;
14223 14224 ip_stack_t *ipst;
14224 14225
14225 14226 if (kp->ks_data == NULL)
14226 14227 return (EIO);
14227 14228
14228 14229 if (rw == KSTAT_WRITE)
14229 14230 return (EACCES);
14230 14231
14231 14232 ns = netstack_find_by_stackid(stackid);
14232 14233 if (ns == NULL)
14233 14234 return (-1);
14234 14235 ipst = ns->netstack_ip;
14235 14236 if (ipst == NULL) {
14236 14237 netstack_rele(ns);
14237 14238 return (-1);
14238 14239 }
14239 14240 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14240 14241
14241 14242 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
14242 14243 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
14243 14244 icmpkp->inDestUnreachs.value.ui32 =
14244 14245 ipst->ips_icmp_mib.icmpInDestUnreachs;
14245 14246 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
14246 14247 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
14247 14248 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
14248 14249 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
14249 14250 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
14250 14251 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
14251 14252 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
14252 14253 icmpkp->inTimestampReps.value.ui32 =
14253 14254 ipst->ips_icmp_mib.icmpInTimestampReps;
14254 14255 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
14255 14256 icmpkp->inAddrMaskReps.value.ui32 =
14256 14257 ipst->ips_icmp_mib.icmpInAddrMaskReps;
14257 14258 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
14258 14259 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
14259 14260 icmpkp->outDestUnreachs.value.ui32 =
14260 14261 ipst->ips_icmp_mib.icmpOutDestUnreachs;
14261 14262 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
14262 14263 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
14263 14264 icmpkp->outSrcQuenchs.value.ui32 =
14264 14265 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14265 14266 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
14266 14267 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
14267 14268 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
14268 14269 icmpkp->outTimestamps.value.ui32 =
14269 14270 ipst->ips_icmp_mib.icmpOutTimestamps;
14270 14271 icmpkp->outTimestampReps.value.ui32 =
14271 14272 ipst->ips_icmp_mib.icmpOutTimestampReps;
14272 14273 icmpkp->outAddrMasks.value.ui32 =
14273 14274 ipst->ips_icmp_mib.icmpOutAddrMasks;
14274 14275 icmpkp->outAddrMaskReps.value.ui32 =
14275 14276 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14276 14277 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
14277 14278 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
14278 14279 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
14279 14280 icmpkp->outFragNeeded.value.ui32 =
14280 14281 ipst->ips_icmp_mib.icmpOutFragNeeded;
14281 14282 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
14282 14283 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
14283 14284 icmpkp->inBadRedirects.value.ui32 =
14284 14285 ipst->ips_icmp_mib.icmpInBadRedirects;
14285 14286
14286 14287 netstack_rele(ns);
14287 14288 return (0);
14288 14289 }
14289 14290
14290 14291 /*
14291 14292 * This is the fanout function for raw socket opened for SCTP. Note
14292 14293 * that it is called after SCTP checks that there is no socket which
14293 14294 * wants a packet. Then before SCTP handles this out of the blue packet,
14294 14295 * this function is called to see if there is any raw socket for SCTP.
14295 14296 * If there is and it is bound to the correct address, the packet will
14296 14297 * be sent to that socket. Note that only one raw socket can be bound to
14297 14298 * a port. This is assured in ipcl_sctp_hash_insert();
14298 14299 */
14299 14300 void
14300 14301 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14301 14302 ip_recv_attr_t *ira)
14302 14303 {
14303 14304 conn_t *connp;
14304 14305 queue_t *rq;
14305 14306 boolean_t secure;
14306 14307 ill_t *ill = ira->ira_ill;
14307 14308 ip_stack_t *ipst = ill->ill_ipst;
14308 14309 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
14309 14310 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
14310 14311 iaflags_t iraflags = ira->ira_flags;
14311 14312 ill_t *rill = ira->ira_rill;
14312 14313
14313 14314 secure = iraflags & IRAF_IPSEC_SECURE;
14314 14315
14315 14316 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14316 14317 ira, ipst);
14317 14318 if (connp == NULL) {
14318 14319 /*
14319 14320 * Although raw sctp is not summed, OOB chunks must be.
14320 14321 * Drop the packet here if the sctp checksum failed.
14321 14322 */
14322 14323 if (iraflags & IRAF_SCTP_CSUM_ERR) {
14323 14324 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14324 14325 freemsg(mp);
14325 14326 return;
14326 14327 }
14327 14328 ira->ira_ill = ira->ira_rill = NULL;
14328 14329 sctp_ootb_input(mp, ira, ipst);
14329 14330 ira->ira_ill = ill;
14330 14331 ira->ira_rill = rill;
14331 14332 return;
14332 14333 }
14333 14334 rq = connp->conn_rq;
14334 14335 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14335 14336 CONN_DEC_REF(connp);
14336 14337 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14337 14338 freemsg(mp);
14338 14339 return;
14339 14340 }
14340 14341 if (((iraflags & IRAF_IS_IPV4) ?
14341 14342 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14342 14343 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14343 14344 secure) {
14344 14345 mp = ipsec_check_inbound_policy(mp, connp, ipha,
14345 14346 ip6h, ira);
14346 14347 if (mp == NULL) {
14347 14348 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14348 14349 /* Note that mp is NULL */
14349 14350 ip_drop_input("ipIfStatsInDiscards", mp, ill);
14350 14351 CONN_DEC_REF(connp);
14351 14352 return;
14352 14353 }
14353 14354 }
14354 14355
14355 14356 if (iraflags & IRAF_ICMP_ERROR) {
14356 14357 (connp->conn_recvicmp)(connp, mp, NULL, ira);
14357 14358 } else {
14358 14359 ill_t *rill = ira->ira_rill;
14359 14360
14360 14361 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14361 14362 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14362 14363 ira->ira_ill = ira->ira_rill = NULL;
14363 14364 (connp->conn_recv)(connp, mp, NULL, ira);
14364 14365 ira->ira_ill = ill;
14365 14366 ira->ira_rill = rill;
14366 14367 }
14367 14368 CONN_DEC_REF(connp);
14368 14369 }
14369 14370
14370 14371 /*
14371 14372 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14372 14373 * header before the ip payload.
14373 14374 */
14374 14375 static void
14375 14376 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14376 14377 {
14377 14378 int len = (mp->b_wptr - mp->b_rptr);
14378 14379 mblk_t *ip_mp;
14379 14380
14380 14381 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14381 14382 if (is_fp_mp || len != fp_mp_len) {
14382 14383 if (len > fp_mp_len) {
14383 14384 /*
14384 14385 * fastpath header and ip header in the first mblk
14385 14386 */
14386 14387 mp->b_rptr += fp_mp_len;
14387 14388 } else {
14388 14389 /*
14389 14390 * ip_xmit_attach_llhdr had to prepend an mblk to
14390 14391 * attach the fastpath header before ip header.
14391 14392 */
14392 14393 ip_mp = mp->b_cont;
14393 14394 freeb(mp);
14394 14395 mp = ip_mp;
14395 14396 mp->b_rptr += (fp_mp_len - len);
14396 14397 }
14397 14398 } else {
14398 14399 ip_mp = mp->b_cont;
14399 14400 freeb(mp);
14400 14401 mp = ip_mp;
14401 14402 }
14402 14403 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14403 14404 freemsg(mp);
14404 14405 }
14405 14406
14406 14407 /*
14407 14408 * Normal post fragmentation function.
14408 14409 *
14409 14410 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14410 14411 * using the same state machine.
14411 14412 *
14412 14413 * We return an error on failure. In particular we return EWOULDBLOCK
14413 14414 * when the driver flow controls. In that case this ensures that ip_wsrv runs
14414 14415 * (currently by canputnext failure resulting in backenabling from GLD.)
14415 14416 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14416 14417 * indication that they can flow control until ip_wsrv() tells then to restart.
14417 14418 *
14418 14419 * If the nce passed by caller is incomplete, this function
14419 14420 * queues the packet and if necessary, sends ARP request and bails.
14420 14421 * If the Neighbor Cache passed is fully resolved, we simply prepend
14421 14422 * the link-layer header to the packet, do ipsec hw acceleration
14422 14423 * work if necessary, and send the packet out on the wire.
14423 14424 */
14424 14425 /* ARGSUSED6 */
14425 14426 int
14426 14427 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14427 14428 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14428 14429 {
14429 14430 queue_t *wq;
14430 14431 ill_t *ill = nce->nce_ill;
14431 14432 ip_stack_t *ipst = ill->ill_ipst;
14432 14433 uint64_t delta;
14433 14434 boolean_t isv6 = ill->ill_isv6;
14434 14435 boolean_t fp_mp;
14435 14436 ncec_t *ncec = nce->nce_common;
14436 14437 int64_t now = LBOLT_FASTPATH64;
14437 14438 boolean_t is_probe;
14438 14439
14439 14440 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14440 14441
14441 14442 ASSERT(mp != NULL);
14442 14443 ASSERT(mp->b_datap->db_type == M_DATA);
14443 14444 ASSERT(pkt_len == msgdsize(mp));
14444 14445
14445 14446 /*
14446 14447 * If we have already been here and are coming back after ARP/ND.
14447 14448 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14448 14449 * in that case since they have seen the packet when it came here
14449 14450 * the first time.
14450 14451 */
14451 14452 if (ixaflags & IXAF_NO_TRACE)
14452 14453 goto sendit;
14453 14454
14454 14455 if (ixaflags & IXAF_IS_IPV4) {
14455 14456 ipha_t *ipha = (ipha_t *)mp->b_rptr;
14456 14457
14457 14458 ASSERT(!isv6);
14458 14459 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14459 14460 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14460 14461 !(ixaflags & IXAF_NO_PFHOOK)) {
14461 14462 int error;
14462 14463
14463 14464 FW_HOOKS(ipst->ips_ip4_physical_out_event,
14464 14465 ipst->ips_ipv4firewall_physical_out,
14465 14466 NULL, ill, ipha, mp, mp, 0, ipst, error);
14466 14467 DTRACE_PROBE1(ip4__physical__out__end,
14467 14468 mblk_t *, mp);
14468 14469 if (mp == NULL)
14469 14470 return (error);
14470 14471
14471 14472 /* The length could have changed */
14472 14473 pkt_len = msgdsize(mp);
14473 14474 }
14474 14475 if (ipst->ips_ip4_observe.he_interested) {
14475 14476 /*
14476 14477 * Note that for TX the zoneid is the sending
14477 14478 * zone, whether or not MLP is in play.
14478 14479 * Since the szone argument is the IP zoneid (i.e.,
14479 14480 * zero for exclusive-IP zones) and ipobs wants
14480 14481 * the system zoneid, we map it here.
14481 14482 */
14482 14483 szone = IP_REAL_ZONEID(szone, ipst);
14483 14484
14484 14485 /*
14485 14486 * On the outbound path the destination zone will be
14486 14487 * unknown as we're sending this packet out on the
14487 14488 * wire.
14488 14489 */
14489 14490 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14490 14491 ill, ipst);
14491 14492 }
14492 14493 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14493 14494 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
14494 14495 ipha_t *, ipha, ip6_t *, NULL, int, 0);
14495 14496 } else {
14496 14497 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14497 14498
14498 14499 ASSERT(isv6);
14499 14500 ASSERT(pkt_len ==
14500 14501 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14501 14502 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14502 14503 !(ixaflags & IXAF_NO_PFHOOK)) {
14503 14504 int error;
14504 14505
14505 14506 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14506 14507 ipst->ips_ipv6firewall_physical_out,
14507 14508 NULL, ill, ip6h, mp, mp, 0, ipst, error);
14508 14509 DTRACE_PROBE1(ip6__physical__out__end,
14509 14510 mblk_t *, mp);
14510 14511 if (mp == NULL)
14511 14512 return (error);
14512 14513
14513 14514 /* The length could have changed */
14514 14515 pkt_len = msgdsize(mp);
14515 14516 }
14516 14517 if (ipst->ips_ip6_observe.he_interested) {
14517 14518 /* See above */
14518 14519 szone = IP_REAL_ZONEID(szone, ipst);
14519 14520
14520 14521 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14521 14522 ill, ipst);
14522 14523 }
14523 14524 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14524 14525 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
14525 14526 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14526 14527 }
14527 14528
14528 14529 sendit:
14529 14530 /*
14530 14531 * We check the state without a lock because the state can never
14531 14532 * move "backwards" to initial or incomplete.
14532 14533 */
14533 14534 switch (ncec->ncec_state) {
14534 14535 case ND_REACHABLE:
14535 14536 case ND_STALE:
14536 14537 case ND_DELAY:
14537 14538 case ND_PROBE:
14538 14539 mp = ip_xmit_attach_llhdr(mp, nce);
14539 14540 if (mp == NULL) {
14540 14541 /*
14541 14542 * ip_xmit_attach_llhdr has increased
14542 14543 * ipIfStatsOutDiscards and called ip_drop_output()
14543 14544 */
14544 14545 return (ENOBUFS);
14545 14546 }
14546 14547 /*
14547 14548 * check if nce_fastpath completed and we tagged on a
14548 14549 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14549 14550 */
14550 14551 fp_mp = (mp->b_datap->db_type == M_DATA);
14551 14552
14552 14553 if (fp_mp &&
14553 14554 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14554 14555 ill_dld_direct_t *idd;
14555 14556
14556 14557 idd = &ill->ill_dld_capab->idc_direct;
14557 14558 /*
14558 14559 * Send the packet directly to DLD, where it
14559 14560 * may be queued depending on the availability
14560 14561 * of transmit resources at the media layer.
14561 14562 * Return value should be taken into
14562 14563 * account and flow control the TCP.
14563 14564 */
14564 14565 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14565 14566 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14566 14567 pkt_len);
14567 14568
14568 14569 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14569 14570 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14570 14571 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14571 14572 } else {
14572 14573 uintptr_t cookie;
14573 14574
14574 14575 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14575 14576 mp, (uintptr_t)xmit_hint, 0)) != 0) {
14576 14577 if (ixacookie != NULL)
14577 14578 *ixacookie = cookie;
14578 14579 return (EWOULDBLOCK);
14579 14580 }
14580 14581 }
14581 14582 } else {
14582 14583 wq = ill->ill_wq;
14583 14584
14584 14585 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14585 14586 !canputnext(wq)) {
14586 14587 if (ixacookie != NULL)
14587 14588 *ixacookie = 0;
14588 14589 ip_xmit_flowctl_drop(ill, mp, fp_mp,
14589 14590 nce->nce_fp_mp != NULL ?
14590 14591 MBLKL(nce->nce_fp_mp) : 0);
14591 14592 return (EWOULDBLOCK);
14592 14593 }
14593 14594 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14594 14595 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14595 14596 pkt_len);
14596 14597 putnext(wq, mp);
14597 14598 }
14598 14599
14599 14600 /*
14600 14601 * The rest of this function implements Neighbor Unreachability
14601 14602 * detection. Determine if the ncec is eligible for NUD.
14602 14603 */
14603 14604 if (ncec->ncec_flags & NCE_F_NONUD)
14604 14605 return (0);
14605 14606
14606 14607 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14607 14608
14608 14609 /*
14609 14610 * Check for upper layer advice
14610 14611 */
14611 14612 if (ixaflags & IXAF_REACH_CONF) {
14612 14613 timeout_id_t tid;
14613 14614
14614 14615 /*
14615 14616 * It should be o.k. to check the state without
14616 14617 * a lock here, at most we lose an advice.
14617 14618 */
14618 14619 ncec->ncec_last = TICK_TO_MSEC(now);
14619 14620 if (ncec->ncec_state != ND_REACHABLE) {
14620 14621 mutex_enter(&ncec->ncec_lock);
14621 14622 ncec->ncec_state = ND_REACHABLE;
14622 14623 tid = ncec->ncec_timeout_id;
14623 14624 ncec->ncec_timeout_id = 0;
14624 14625 mutex_exit(&ncec->ncec_lock);
14625 14626 (void) untimeout(tid);
14626 14627 if (ip_debug > 2) {
14627 14628 /* ip1dbg */
14628 14629 pr_addr_dbg("ip_xmit: state"
14629 14630 " for %s changed to"
14630 14631 " REACHABLE\n", AF_INET6,
14631 14632 &ncec->ncec_addr);
14632 14633 }
14633 14634 }
14634 14635 return (0);
14635 14636 }
14636 14637
14637 14638 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14638 14639 ip1dbg(("ip_xmit: delta = %" PRId64
14639 14640 " ill_reachable_time = %d \n", delta,
14640 14641 ill->ill_reachable_time));
14641 14642 if (delta > (uint64_t)ill->ill_reachable_time) {
14642 14643 mutex_enter(&ncec->ncec_lock);
14643 14644 switch (ncec->ncec_state) {
14644 14645 case ND_REACHABLE:
14645 14646 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14646 14647 /* FALLTHROUGH */
14647 14648 case ND_STALE:
14648 14649 /*
14649 14650 * ND_REACHABLE is identical to
14650 14651 * ND_STALE in this specific case. If
14651 14652 * reachable time has expired for this
14652 14653 * neighbor (delta is greater than
14653 14654 * reachable time), conceptually, the
14654 14655 * neighbor cache is no longer in
14655 14656 * REACHABLE state, but already in
14656 14657 * STALE state. So the correct
14657 14658 * transition here is to ND_DELAY.
14658 14659 */
14659 14660 ncec->ncec_state = ND_DELAY;
14660 14661 mutex_exit(&ncec->ncec_lock);
14661 14662 nce_restart_timer(ncec,
14662 14663 ipst->ips_delay_first_probe_time);
14663 14664 if (ip_debug > 3) {
14664 14665 /* ip2dbg */
14665 14666 pr_addr_dbg("ip_xmit: state"
14666 14667 " for %s changed to"
14667 14668 " DELAY\n", AF_INET6,
14668 14669 &ncec->ncec_addr);
14669 14670 }
14670 14671 break;
14671 14672 case ND_DELAY:
14672 14673 case ND_PROBE:
14673 14674 mutex_exit(&ncec->ncec_lock);
14674 14675 /* Timers have already started */
14675 14676 break;
14676 14677 case ND_UNREACHABLE:
14677 14678 /*
14678 14679 * nce_timer has detected that this ncec
14679 14680 * is unreachable and initiated deleting
14680 14681 * this ncec.
14681 14682 * This is a harmless race where we found the
14682 14683 * ncec before it was deleted and have
14683 14684 * just sent out a packet using this
14684 14685 * unreachable ncec.
14685 14686 */
14686 14687 mutex_exit(&ncec->ncec_lock);
14687 14688 break;
14688 14689 default:
14689 14690 ASSERT(0);
14690 14691 mutex_exit(&ncec->ncec_lock);
14691 14692 }
14692 14693 }
14693 14694 return (0);
14694 14695
14695 14696 case ND_INCOMPLETE:
14696 14697 /*
14697 14698 * the state could have changed since we didn't hold the lock.
14698 14699 * Re-verify state under lock.
14699 14700 */
14700 14701 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14701 14702 mutex_enter(&ncec->ncec_lock);
14702 14703 if (NCE_ISREACHABLE(ncec)) {
14703 14704 mutex_exit(&ncec->ncec_lock);
14704 14705 goto sendit;
14705 14706 }
14706 14707 /* queue the packet */
14707 14708 nce_queue_mp(ncec, mp, is_probe);
14708 14709 mutex_exit(&ncec->ncec_lock);
14709 14710 DTRACE_PROBE2(ip__xmit__incomplete,
14710 14711 (ncec_t *), ncec, (mblk_t *), mp);
14711 14712 return (0);
14712 14713
14713 14714 case ND_INITIAL:
14714 14715 /*
14715 14716 * State could have changed since we didn't hold the lock, so
14716 14717 * re-verify state.
14717 14718 */
14718 14719 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14719 14720 mutex_enter(&ncec->ncec_lock);
14720 14721 if (NCE_ISREACHABLE(ncec)) {
14721 14722 mutex_exit(&ncec->ncec_lock);
14722 14723 goto sendit;
14723 14724 }
14724 14725 nce_queue_mp(ncec, mp, is_probe);
14725 14726 if (ncec->ncec_state == ND_INITIAL) {
14726 14727 ncec->ncec_state = ND_INCOMPLETE;
14727 14728 mutex_exit(&ncec->ncec_lock);
14728 14729 /*
14729 14730 * figure out the source we want to use
14730 14731 * and resolve it.
14731 14732 */
14732 14733 ip_ndp_resolve(ncec);
14733 14734 } else {
14734 14735 mutex_exit(&ncec->ncec_lock);
14735 14736 }
14736 14737 return (0);
14737 14738
14738 14739 case ND_UNREACHABLE:
14739 14740 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14740 14741 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14741 14742 mp, ill);
14742 14743 freemsg(mp);
14743 14744 return (0);
14744 14745
14745 14746 default:
14746 14747 ASSERT(0);
14747 14748 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14748 14749 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14749 14750 mp, ill);
14750 14751 freemsg(mp);
14751 14752 return (ENETUNREACH);
14752 14753 }
14753 14754 }
14754 14755
14755 14756 /*
14756 14757 * Return B_TRUE if the buffers differ in length or content.
14757 14758 * This is used for comparing extension header buffers.
14758 14759 * Note that an extension header would be declared different
14759 14760 * even if all that changed was the next header value in that header i.e.
14760 14761 * what really changed is the next extension header.
14761 14762 */
14762 14763 boolean_t
14763 14764 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14764 14765 uint_t blen)
14765 14766 {
14766 14767 if (!b_valid)
14767 14768 blen = 0;
14768 14769
14769 14770 if (alen != blen)
14770 14771 return (B_TRUE);
14771 14772 if (alen == 0)
14772 14773 return (B_FALSE); /* Both zero length */
14773 14774 return (bcmp(abuf, bbuf, alen));
14774 14775 }
14775 14776
14776 14777 /*
14777 14778 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14778 14779 * Return B_FALSE if memory allocation fails - don't change any state!
14779 14780 */
14780 14781 boolean_t
14781 14782 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14782 14783 const void *src, uint_t srclen)
14783 14784 {
14784 14785 void *dst;
14785 14786
14786 14787 if (!src_valid)
14787 14788 srclen = 0;
14788 14789
14789 14790 ASSERT(*dstlenp == 0);
14790 14791 if (src != NULL && srclen != 0) {
14791 14792 dst = mi_alloc(srclen, BPRI_MED);
14792 14793 if (dst == NULL)
14793 14794 return (B_FALSE);
14794 14795 } else {
14795 14796 dst = NULL;
14796 14797 }
14797 14798 if (*dstp != NULL)
14798 14799 mi_free(*dstp);
14799 14800 *dstp = dst;
14800 14801 *dstlenp = dst == NULL ? 0 : srclen;
14801 14802 return (B_TRUE);
14802 14803 }
14803 14804
14804 14805 /*
14805 14806 * Replace what is in *dst, *dstlen with the source.
14806 14807 * Assumes ip_allocbuf has already been called.
14807 14808 */
14808 14809 void
14809 14810 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14810 14811 const void *src, uint_t srclen)
14811 14812 {
14812 14813 if (!src_valid)
14813 14814 srclen = 0;
14814 14815
14815 14816 ASSERT(*dstlenp == srclen);
14816 14817 if (src != NULL && srclen != 0)
14817 14818 bcopy(src, *dstp, srclen);
14818 14819 }
14819 14820
14820 14821 /*
14821 14822 * Free the storage pointed to by the members of an ip_pkt_t.
14822 14823 */
14823 14824 void
14824 14825 ip_pkt_free(ip_pkt_t *ipp)
14825 14826 {
14826 14827 uint_t fields = ipp->ipp_fields;
14827 14828
14828 14829 if (fields & IPPF_HOPOPTS) {
14829 14830 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14830 14831 ipp->ipp_hopopts = NULL;
14831 14832 ipp->ipp_hopoptslen = 0;
14832 14833 }
14833 14834 if (fields & IPPF_RTHDRDSTOPTS) {
14834 14835 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14835 14836 ipp->ipp_rthdrdstopts = NULL;
14836 14837 ipp->ipp_rthdrdstoptslen = 0;
14837 14838 }
14838 14839 if (fields & IPPF_DSTOPTS) {
14839 14840 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14840 14841 ipp->ipp_dstopts = NULL;
14841 14842 ipp->ipp_dstoptslen = 0;
14842 14843 }
14843 14844 if (fields & IPPF_RTHDR) {
14844 14845 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14845 14846 ipp->ipp_rthdr = NULL;
14846 14847 ipp->ipp_rthdrlen = 0;
14847 14848 }
14848 14849 if (fields & IPPF_IPV4_OPTIONS) {
14849 14850 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14850 14851 ipp->ipp_ipv4_options = NULL;
14851 14852 ipp->ipp_ipv4_options_len = 0;
14852 14853 }
14853 14854 if (fields & IPPF_LABEL_V4) {
14854 14855 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14855 14856 ipp->ipp_label_v4 = NULL;
14856 14857 ipp->ipp_label_len_v4 = 0;
14857 14858 }
14858 14859 if (fields & IPPF_LABEL_V6) {
14859 14860 kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14860 14861 ipp->ipp_label_v6 = NULL;
14861 14862 ipp->ipp_label_len_v6 = 0;
14862 14863 }
14863 14864 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14864 14865 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14865 14866 }
14866 14867
14867 14868 /*
14868 14869 * Copy from src to dst and allocate as needed.
14869 14870 * Returns zero or ENOMEM.
14870 14871 *
14871 14872 * The caller must initialize dst to zero.
14872 14873 */
14873 14874 int
14874 14875 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14875 14876 {
14876 14877 uint_t fields = src->ipp_fields;
14877 14878
14878 14879 /* Start with fields that don't require memory allocation */
14879 14880 dst->ipp_fields = fields &
14880 14881 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14881 14882 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14882 14883
14883 14884 dst->ipp_addr = src->ipp_addr;
14884 14885 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14885 14886 dst->ipp_hoplimit = src->ipp_hoplimit;
14886 14887 dst->ipp_tclass = src->ipp_tclass;
14887 14888 dst->ipp_type_of_service = src->ipp_type_of_service;
14888 14889
14889 14890 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14890 14891 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14891 14892 return (0);
14892 14893
14893 14894 if (fields & IPPF_HOPOPTS) {
14894 14895 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14895 14896 if (dst->ipp_hopopts == NULL) {
14896 14897 ip_pkt_free(dst);
14897 14898 return (ENOMEM);
14898 14899 }
14899 14900 dst->ipp_fields |= IPPF_HOPOPTS;
14900 14901 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14901 14902 src->ipp_hopoptslen);
14902 14903 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14903 14904 }
14904 14905 if (fields & IPPF_RTHDRDSTOPTS) {
14905 14906 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14906 14907 kmflag);
14907 14908 if (dst->ipp_rthdrdstopts == NULL) {
14908 14909 ip_pkt_free(dst);
14909 14910 return (ENOMEM);
14910 14911 }
14911 14912 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14912 14913 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14913 14914 src->ipp_rthdrdstoptslen);
14914 14915 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14915 14916 }
14916 14917 if (fields & IPPF_DSTOPTS) {
14917 14918 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14918 14919 if (dst->ipp_dstopts == NULL) {
14919 14920 ip_pkt_free(dst);
14920 14921 return (ENOMEM);
14921 14922 }
14922 14923 dst->ipp_fields |= IPPF_DSTOPTS;
14923 14924 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14924 14925 src->ipp_dstoptslen);
14925 14926 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14926 14927 }
14927 14928 if (fields & IPPF_RTHDR) {
14928 14929 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14929 14930 if (dst->ipp_rthdr == NULL) {
14930 14931 ip_pkt_free(dst);
14931 14932 return (ENOMEM);
14932 14933 }
14933 14934 dst->ipp_fields |= IPPF_RTHDR;
14934 14935 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14935 14936 src->ipp_rthdrlen);
14936 14937 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14937 14938 }
14938 14939 if (fields & IPPF_IPV4_OPTIONS) {
14939 14940 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14940 14941 kmflag);
14941 14942 if (dst->ipp_ipv4_options == NULL) {
14942 14943 ip_pkt_free(dst);
14943 14944 return (ENOMEM);
14944 14945 }
14945 14946 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14946 14947 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14947 14948 src->ipp_ipv4_options_len);
14948 14949 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14949 14950 }
14950 14951 if (fields & IPPF_LABEL_V4) {
14951 14952 dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14952 14953 if (dst->ipp_label_v4 == NULL) {
14953 14954 ip_pkt_free(dst);
14954 14955 return (ENOMEM);
14955 14956 }
14956 14957 dst->ipp_fields |= IPPF_LABEL_V4;
14957 14958 bcopy(src->ipp_label_v4, dst->ipp_label_v4,
14958 14959 src->ipp_label_len_v4);
14959 14960 dst->ipp_label_len_v4 = src->ipp_label_len_v4;
14960 14961 }
14961 14962 if (fields & IPPF_LABEL_V6) {
14962 14963 dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
14963 14964 if (dst->ipp_label_v6 == NULL) {
14964 14965 ip_pkt_free(dst);
14965 14966 return (ENOMEM);
14966 14967 }
14967 14968 dst->ipp_fields |= IPPF_LABEL_V6;
14968 14969 bcopy(src->ipp_label_v6, dst->ipp_label_v6,
14969 14970 src->ipp_label_len_v6);
14970 14971 dst->ipp_label_len_v6 = src->ipp_label_len_v6;
14971 14972 }
14972 14973 if (fields & IPPF_FRAGHDR) {
14973 14974 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14974 14975 if (dst->ipp_fraghdr == NULL) {
14975 14976 ip_pkt_free(dst);
14976 14977 return (ENOMEM);
14977 14978 }
14978 14979 dst->ipp_fields |= IPPF_FRAGHDR;
14979 14980 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14980 14981 src->ipp_fraghdrlen);
14981 14982 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14982 14983 }
14983 14984 return (0);
14984 14985 }
14985 14986
14986 14987 /*
14987 14988 * Returns INADDR_ANY if no source route
14988 14989 */
14989 14990 ipaddr_t
14990 14991 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14991 14992 {
14992 14993 ipaddr_t nexthop = INADDR_ANY;
14993 14994 ipoptp_t opts;
14994 14995 uchar_t *opt;
14995 14996 uint8_t optval;
14996 14997 uint8_t optlen;
14997 14998 uint32_t totallen;
14998 14999
14999 15000 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15000 15001 return (INADDR_ANY);
15001 15002
15002 15003 totallen = ipp->ipp_ipv4_options_len;
15003 15004 if (totallen & 0x3)
15004 15005 return (INADDR_ANY);
15005 15006
15006 15007 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15007 15008 optval != IPOPT_EOL;
15008 15009 optval = ipoptp_next(&opts)) {
15009 15010 opt = opts.ipoptp_cur;
15010 15011 switch (optval) {
15011 15012 uint8_t off;
15012 15013 case IPOPT_SSRR:
15013 15014 case IPOPT_LSRR:
15014 15015 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15015 15016 break;
15016 15017 }
15017 15018 optlen = opts.ipoptp_len;
15018 15019 off = opt[IPOPT_OFFSET];
15019 15020 off--;
15020 15021 if (optlen < IP_ADDR_LEN ||
15021 15022 off > optlen - IP_ADDR_LEN) {
15022 15023 /* End of source route */
15023 15024 break;
15024 15025 }
15025 15026 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
15026 15027 if (nexthop == htonl(INADDR_LOOPBACK)) {
15027 15028 /* Ignore */
15028 15029 nexthop = INADDR_ANY;
15029 15030 break;
15030 15031 }
15031 15032 break;
15032 15033 }
15033 15034 }
15034 15035 return (nexthop);
15035 15036 }
15036 15037
15037 15038 /*
15038 15039 * Reverse a source route.
15039 15040 */
15040 15041 void
15041 15042 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
15042 15043 {
15043 15044 ipaddr_t tmp;
15044 15045 ipoptp_t opts;
15045 15046 uchar_t *opt;
15046 15047 uint8_t optval;
15047 15048 uint32_t totallen;
15048 15049
15049 15050 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15050 15051 return;
15051 15052
15052 15053 totallen = ipp->ipp_ipv4_options_len;
15053 15054 if (totallen & 0x3)
15054 15055 return;
15055 15056
15056 15057 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15057 15058 optval != IPOPT_EOL;
15058 15059 optval = ipoptp_next(&opts)) {
15059 15060 uint8_t off1, off2;
15060 15061
15061 15062 opt = opts.ipoptp_cur;
15062 15063 switch (optval) {
15063 15064 case IPOPT_SSRR:
15064 15065 case IPOPT_LSRR:
15065 15066 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15066 15067 break;
15067 15068 }
15068 15069 off1 = IPOPT_MINOFF_SR - 1;
15069 15070 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15070 15071 while (off2 > off1) {
15071 15072 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15072 15073 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15073 15074 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15074 15075 off2 -= IP_ADDR_LEN;
15075 15076 off1 += IP_ADDR_LEN;
15076 15077 }
15077 15078 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15078 15079 break;
15079 15080 }
15080 15081 }
15081 15082 }
15082 15083
15083 15084 /*
15084 15085 * Returns NULL if no routing header
15085 15086 */
15086 15087 in6_addr_t *
15087 15088 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15088 15089 {
15089 15090 in6_addr_t *nexthop = NULL;
15090 15091 ip6_rthdr0_t *rthdr;
15091 15092
15092 15093 if (!(ipp->ipp_fields & IPPF_RTHDR))
15093 15094 return (NULL);
15094 15095
15095 15096 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15096 15097 if (rthdr->ip6r0_segleft == 0)
15097 15098 return (NULL);
15098 15099
15099 15100 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15100 15101 return (nexthop);
15101 15102 }
15102 15103
15103 15104 zoneid_t
15104 15105 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15105 15106 zoneid_t lookup_zoneid)
15106 15107 {
15107 15108 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15108 15109 ire_t *ire;
15109 15110 int ire_flags = MATCH_IRE_TYPE;
15110 15111 zoneid_t zoneid = ALL_ZONES;
15111 15112
15112 15113 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15113 15114 return (ALL_ZONES);
15114 15115
15115 15116 if (lookup_zoneid != ALL_ZONES)
15116 15117 ire_flags |= MATCH_IRE_ZONEONLY;
15117 15118 ire = ire_ftable_lookup_v4(addr, 0, 0, IRE_LOCAL | IRE_LOOPBACK,
15118 15119 NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15119 15120 if (ire != NULL) {
15120 15121 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15121 15122 ire_refrele(ire);
15122 15123 }
15123 15124 return (zoneid);
15124 15125 }
15125 15126
15126 15127 zoneid_t
15127 15128 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15128 15129 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15129 15130 {
15130 15131 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15131 15132 ire_t *ire;
15132 15133 int ire_flags = MATCH_IRE_TYPE;
15133 15134 zoneid_t zoneid = ALL_ZONES;
15134 15135
15135 15136 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15136 15137 return (ALL_ZONES);
15137 15138
15138 15139 if (IN6_IS_ADDR_LINKLOCAL(addr))
15139 15140 ire_flags |= MATCH_IRE_ILL;
15140 15141
15141 15142 if (lookup_zoneid != ALL_ZONES)
15142 15143 ire_flags |= MATCH_IRE_ZONEONLY;
15143 15144 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15144 15145 ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15145 15146 if (ire != NULL) {
15146 15147 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15147 15148 ire_refrele(ire);
15148 15149 }
15149 15150 return (zoneid);
15150 15151 }
15151 15152
15152 15153 /*
15153 15154 * IP obserability hook support functions.
15154 15155 */
15155 15156 static void
15156 15157 ipobs_init(ip_stack_t *ipst)
15157 15158 {
15158 15159 netid_t id;
15159 15160
15160 15161 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15161 15162
15162 15163 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15163 15164 VERIFY(ipst->ips_ip4_observe_pr != NULL);
15164 15165
15165 15166 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15166 15167 VERIFY(ipst->ips_ip6_observe_pr != NULL);
15167 15168 }
15168 15169
15169 15170 static void
15170 15171 ipobs_fini(ip_stack_t *ipst)
15171 15172 {
15172 15173
15173 15174 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15174 15175 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15175 15176 }
15176 15177
15177 15178 /*
15178 15179 * hook_pkt_observe_t is composed in network byte order so that the
15179 15180 * entire mblk_t chain handed into hook_run can be used as-is.
15180 15181 * The caveat is that use of the fields, such as the zone fields,
15181 15182 * requires conversion into host byte order first.
15182 15183 */
15183 15184 void
15184 15185 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15185 15186 const ill_t *ill, ip_stack_t *ipst)
15186 15187 {
15187 15188 hook_pkt_observe_t *hdr;
15188 15189 uint64_t grifindex;
15189 15190 mblk_t *imp;
15190 15191
15191 15192 imp = allocb(sizeof (*hdr), BPRI_HI);
15192 15193 if (imp == NULL)
15193 15194 return;
15194 15195
15195 15196 hdr = (hook_pkt_observe_t *)imp->b_rptr;
15196 15197 /*
15197 15198 * b_wptr is set to make the apparent size of the data in the mblk_t
15198 15199 * to exclude the pointers at the end of hook_pkt_observer_t.
15199 15200 */
15200 15201 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15201 15202 imp->b_cont = mp;
15202 15203
15203 15204 ASSERT(DB_TYPE(mp) == M_DATA);
15204 15205
15205 15206 if (IS_UNDER_IPMP(ill))
15206 15207 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15207 15208 else
15208 15209 grifindex = 0;
15209 15210
15210 15211 hdr->hpo_version = 1;
15211 15212 hdr->hpo_htype = htons(htype);
15212 15213 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15213 15214 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15214 15215 hdr->hpo_grifindex = htonl(grifindex);
15215 15216 hdr->hpo_zsrc = htonl(zsrc);
15216 15217 hdr->hpo_zdst = htonl(zdst);
15217 15218 hdr->hpo_pkt = imp;
15218 15219 hdr->hpo_ctx = ipst->ips_netstack;
15219 15220
15220 15221 if (ill->ill_isv6) {
15221 15222 hdr->hpo_family = AF_INET6;
15222 15223 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15223 15224 ipst->ips_ipv6observing, (hook_data_t)hdr);
15224 15225 } else {
15225 15226 hdr->hpo_family = AF_INET;
15226 15227 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15227 15228 ipst->ips_ipv4observing, (hook_data_t)hdr);
15228 15229 }
15229 15230
15230 15231 imp->b_cont = NULL;
15231 15232 freemsg(imp);
15232 15233 }
15233 15234
15234 15235 /*
15235 15236 * Utility routine that checks if `v4srcp' is a valid address on underlying
15236 15237 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
15237 15238 * associated with `v4srcp' on success. NOTE: if this is not called from
15238 15239 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15239 15240 * group during or after this lookup.
15240 15241 */
15241 15242 boolean_t
15242 15243 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15243 15244 {
15244 15245 ipif_t *ipif;
15245 15246
15246 15247 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15247 15248 if (ipif != NULL) {
15248 15249 if (ipifp != NULL)
15249 15250 *ipifp = ipif;
15250 15251 else
15251 15252 ipif_refrele(ipif);
15252 15253 return (B_TRUE);
15253 15254 }
15254 15255
15255 15256 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15256 15257 *v4srcp));
15257 15258 return (B_FALSE);
15258 15259 }
15259 15260
15260 15261 /*
15261 15262 * Transport protocol call back function for CPU state change.
15262 15263 */
15263 15264 /* ARGSUSED */
15264 15265 static int
15265 15266 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15266 15267 {
15267 15268 processorid_t cpu_seqid;
15268 15269 netstack_handle_t nh;
15269 15270 netstack_t *ns;
15270 15271
15271 15272 ASSERT(MUTEX_HELD(&cpu_lock));
15272 15273
15273 15274 switch (what) {
15274 15275 case CPU_CONFIG:
15275 15276 case CPU_ON:
15276 15277 case CPU_INIT:
15277 15278 case CPU_CPUPART_IN:
15278 15279 cpu_seqid = cpu[id]->cpu_seqid;
15279 15280 netstack_next_init(&nh);
15280 15281 while ((ns = netstack_next(&nh)) != NULL) {
15281 15282 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15282 15283 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15283 15284 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15284 15285 netstack_rele(ns);
15285 15286 }
15286 15287 netstack_next_fini(&nh);
15287 15288 break;
15288 15289 case CPU_UNCONFIG:
15289 15290 case CPU_OFF:
15290 15291 case CPU_CPUPART_OUT:
15291 15292 /*
15292 15293 * Nothing to do. We don't remove the per CPU stats from
15293 15294 * the IP stack even when the CPU goes offline.
15294 15295 */
15295 15296 break;
15296 15297 default:
15297 15298 break;
15298 15299 }
15299 15300 return (0);
15300 15301 }
↓ open down ↓ |
15193 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX